script.cpp 760 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #include "script.hpp"
  9. #include <cerrno>
  10. #include <cmath>
  11. #include <csetjmp>
  12. #include <cstdlib> // atoi, strtol, strtoll, exit
  13. #ifdef PCRE_SUPPORT
  14. #include <pcre.h> // preg_match
  15. #endif
  16. #include <common/cbasetypes.hpp>
  17. #include <common/ers.hpp> // ers_destroy
  18. #include <common/malloc.hpp>
  19. #include <common/md5calc.hpp>
  20. #include <common/nullpo.hpp>
  21. #include <common/random.hpp>
  22. #include <common/showmsg.hpp>
  23. #include <common/socket.hpp>
  24. #include <common/strlib.hpp>
  25. #include <common/timer.hpp>
  26. #include <common/utilities.hpp>
  27. #include <common/utils.hpp>
  28. #include "achievement.hpp"
  29. #include "atcommand.hpp"
  30. #include "battle.hpp"
  31. #include "battleground.hpp"
  32. #include "cashshop.hpp"
  33. #include "channel.hpp"
  34. #include "chat.hpp"
  35. #include "chrif.hpp"
  36. #include "clan.hpp"
  37. #include "clif.hpp"
  38. #include "date.hpp" // date type enum, date_get()
  39. #include "elemental.hpp"
  40. #include "guild.hpp"
  41. #include "homunculus.hpp"
  42. #include "instance.hpp"
  43. #include "intif.hpp"
  44. #include "itemdb.hpp"
  45. #include "log.hpp"
  46. #include "mail.hpp"
  47. #include "map.hpp"
  48. #include "mapreg.hpp"
  49. #include "mercenary.hpp"
  50. #include "mob.hpp"
  51. #include "npc.hpp"
  52. #include "party.hpp"
  53. #include "path.hpp"
  54. #include "pc.hpp"
  55. #include "pc_groups.hpp"
  56. #include "pet.hpp"
  57. #include "quest.hpp"
  58. #include "storage.hpp"
  59. using namespace rathena;
  60. const int64 SCRIPT_INT_MIN = INT64_MIN;
  61. const int64 SCRIPT_INT_MAX = INT64_MAX;
  62. struct eri *array_ers;
  63. DBMap *st_db;
  64. uint32 active_scripts;
  65. uint32 next_id;
  66. struct eri *st_ers;
  67. struct eri *stack_ers;
  68. static bool script_rid2sd_( struct script_state *st, map_session_data** sd, const char *func );
  69. /**
  70. * Get `sd` from a account id in `loc` param instead of attached rid
  71. * @param st Script
  72. * @param loc Location to look account id in script parameter
  73. * @param sd Variable that will be assigned
  74. * @return True if `sd` is assigned, false otherwise
  75. **/
  76. static bool script_accid2sd_(struct script_state *st, uint8 loc, map_session_data **sd, const char *func) {
  77. if (script_hasdata(st, loc)) {
  78. int32 id_ = script_getnum(st, loc);
  79. if (!(*sd = map_id2sd(id_))){
  80. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  81. return false;
  82. }else{
  83. return true;
  84. }
  85. }
  86. else
  87. return script_rid2sd_(st,sd,func);
  88. }
  89. /**
  90. * Get `sd` from a char id in `loc` param instead of attached rid
  91. * @param st Script
  92. * @param loc Location to look char id in script parameter
  93. * @param sd Variable that will be assigned
  94. * @return True if `sd` is assigned, false otherwise
  95. **/
  96. static bool script_charid2sd_(struct script_state *st, uint8 loc, map_session_data **sd, const char *func) {
  97. if (script_hasdata(st, loc)) {
  98. int32 id_ = script_getnum(st, loc);
  99. if (!(*sd = map_charid2sd(id_))){
  100. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  101. return false;
  102. }else{
  103. return true;
  104. }
  105. }
  106. else
  107. return script_rid2sd_(st,sd,func);
  108. }
  109. /**
  110. * Get `sd` from a nick in `loc` param instead of attached rid
  111. * @param st Script
  112. * @param loc Location to look nick in script parameter
  113. * @param sd Variable that will be assigned
  114. * @return True if `sd` is assigned, false otherwise
  115. **/
  116. static bool script_nick2sd_(struct script_state *st, uint8 loc, map_session_data **sd, const char *func) {
  117. if (script_hasdata(st, loc)) {
  118. const char *name_ = script_getstr(st, loc);
  119. if (!(*sd = map_nick2sd(name_,false))){
  120. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  121. return false;
  122. }else{
  123. return true;
  124. }
  125. }
  126. else
  127. return script_rid2sd_(st,sd,func);
  128. }
  129. /**
  130. * Get `sd` from a mapid in `loc` param instead of attached rid
  131. * @param st Script
  132. * @param loc Location to look mapid in script parameter
  133. * @param sd Variable that will be assigned
  134. * @return True if `sd` is assigned, false otherwise
  135. **/
  136. static bool script_mapid2sd_(struct script_state *st, uint8 loc, map_session_data **sd, const char *func) {
  137. if (script_hasdata(st, loc)) {
  138. int32 id_ = script_getnum(st, loc);
  139. if (!(*sd = map_id2sd(id_))){
  140. ShowError("%s: Player with map id '%d' is not found.\n", func, id_);
  141. return false;
  142. }else{
  143. return true;
  144. }
  145. }
  146. else
  147. return script_rid2sd_(st,sd,func);
  148. }
  149. /**
  150. * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator.
  151. * @param st Script
  152. * @param loc Location to look for ID in script parameter
  153. * @param bl Variable that will be assigned
  154. * @return True if `bl` is assigned, false otherwise
  155. **/
  156. static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) {
  157. int32 unit_id;
  158. if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0)
  159. unit_id = st->rid;
  160. *bl = map_id2bl(unit_id);
  161. if ( *bl )
  162. return true;
  163. else {
  164. ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id);
  165. return false;
  166. }
  167. }
  168. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  169. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  170. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  171. #define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__)
  172. #define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__)
  173. #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__)
  174. /// temporary buffer for passing around compiled bytecode
  175. /// @see add_scriptb, set_label, parse_script
  176. static unsigned char* script_buf = nullptr;
  177. static int32 script_pos = 0, script_size = 0;
  178. static inline int32 GETVALUE(const unsigned char* buf, int32 i)
  179. {
  180. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  181. }
  182. static inline void SETVALUE(unsigned char* buf, int32 i, int32 n)
  183. {
  184. buf[i] = GetByte(n, 0);
  185. buf[i+1] = GetByte(n, 1);
  186. buf[i+2] = GetByte(n, 2);
  187. }
  188. // String buffer structures.
  189. // str_data stores string information
  190. static struct str_data_struct {
  191. enum c_op type;
  192. int32 str;
  193. int32 backpatch;
  194. int32 label;
  195. int32 (*func)(struct script_state *st);
  196. int64 val;
  197. int32 next;
  198. const char *name;
  199. bool deprecated;
  200. } *str_data = nullptr;
  201. static int32 str_data_size = 0; // size of the data
  202. static int32 str_num = LABEL_START; // next id to be assigned
  203. // str_buf holds the strings themselves
  204. static char *str_buf;
  205. static int32 str_size = 0; // size of the buffer
  206. static int32 str_pos = 0; // next position to be assigned
  207. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  208. #define SCRIPT_HASH_SIZE 1021
  209. int32 str_hash[SCRIPT_HASH_SIZE];
  210. // Specifies which string hashing method to use
  211. //#define SCRIPT_HASH_DJB2
  212. //#define SCRIPT_HASH_SDBM
  213. #define SCRIPT_HASH_ELF
  214. static DBMap* scriptlabel_db = nullptr; // const char* label_name -> int32 script_pos
  215. static DBMap* userfunc_db = nullptr; // const char* func_name -> struct script_code*
  216. static int32 parse_options = 0;
  217. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  218. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  219. // important buildin function references for usage in scripts
  220. static int32 buildin_set_ref = 0;
  221. static int32 buildin_callsub_ref = 0;
  222. static int32 buildin_callfunc_ref = 0;
  223. static int32 buildin_getelementofarray_ref = 0;
  224. // Caches compiled autoscript item code.
  225. // Note: This is not cleared when reloading itemdb.
  226. static DBMap* autobonus_db = nullptr; // char* script -> char* bytecode
  227. struct Script_Config script_config = {
  228. 1, // warn_func_mismatch_argtypes
  229. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  230. 0, INT_MAX, // input_min_value/input_max_value
  231. // NOTE: None of these event labels should be longer than <EVENT_NAME_LENGTH> characters
  232. // PC related
  233. "OnPCDieEvent", //die_event_name
  234. "OnPCKillEvent", //kill_pc_event_name
  235. "OnNPCKillEvent", //kill_mob_event_name
  236. "OnPCLoginEvent", //login_event_name
  237. "OnPCLogoutEvent", //logout_event_name
  238. "OnPCLoadMapEvent", //loadmap_event_name
  239. "OnPCBaseLvUpEvent", //baselvup_event_name
  240. "OnPCJobLvUpEvent", //joblvup_event_name
  241. // NPC related
  242. "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves)
  243. "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area)
  244. "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area)
  245. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  246. "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc)
  247. "OnBuyItem", //onbuy_event_name (is executed when items are bought)
  248. "OnSellItem", //onsell_event_name (is executed when items are sold)
  249. // Init related
  250. "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded)
  251. "OnInterIfInit", //inter_init_event_name (is executed on inter server connection)
  252. "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection)
  253. // Guild related
  254. "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken)
  255. "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started)
  256. "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded)
  257. "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended)
  258. "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started)
  259. "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded)
  260. "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended)
  261. "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started)
  262. "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded)
  263. "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended)
  264. // Timer related
  265. "OnTimer", //timer_event_name (is executed by a timer at the specific second)
  266. "OnTimerQuit", //timer_quit_event_name (is executed when a timer is aborted)
  267. "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute)
  268. "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour)
  269. "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute)
  270. "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day)
  271. "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute)
  272. "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute)
  273. "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute)
  274. "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute)
  275. "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute)
  276. "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute)
  277. "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute)
  278. // Instance related
  279. "OnInstanceInit", //instance_init_event_name (is executed right after instance creation)
  280. "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction)
  281. "OnNaviGenerate", //navi_generate_name (is executed right before navi generation)
  282. };
  283. static jmp_buf error_jump;
  284. static char* error_msg;
  285. static const char* error_pos;
  286. static int32 error_report; // if the error should produce output
  287. // Used by disp_warning_message
  288. static const char* parser_current_src;
  289. static const char* parser_current_file;
  290. static int32 parser_current_line;
  291. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  292. // [Eoe / jA 1080, 1081, 1094, 1164]
  293. enum curly_type {
  294. TYPE_NULL = 0,
  295. TYPE_IF,
  296. TYPE_SWITCH,
  297. TYPE_WHILE,
  298. TYPE_FOR,
  299. TYPE_DO,
  300. TYPE_USERFUNC,
  301. TYPE_ARGLIST // function argument list
  302. };
  303. enum e_arglist
  304. {
  305. ARGLIST_UNDEFINED = 0,
  306. ARGLIST_NO_PAREN = 1,
  307. ARGLIST_PAREN = 2,
  308. };
  309. static struct {
  310. struct {
  311. enum curly_type type;
  312. int32 index;
  313. int32 count;
  314. int32 flag;
  315. struct linkdb_node *case_label;
  316. } curly[256]; // Information right parenthesis
  317. int32 curly_count; // The number of right brackets
  318. int32 index; // Number of the syntax used in the script
  319. } syntax;
  320. const char* parse_curly_close(const char* p);
  321. const char* parse_syntax_close(const char* p);
  322. const char* parse_syntax_close_sub(const char* p,int* flag);
  323. const char* parse_syntax(const char* p);
  324. static int32 parse_syntax_for_flag = 0;
  325. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  326. extern uint32 current_equip_combo_pos;
  327. int32 potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  328. int32 potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  329. int32 potion_target = 0;
  330. uint32 *generic_ui_array = nullptr;
  331. uint32 generic_ui_array_size = 0;
  332. c_op get_com(unsigned char *script,int32 *pos);
  333. int64 get_num(unsigned char *script,int32 *pos);
  334. typedef struct script_function {
  335. int32 (*func)(struct script_state *st);
  336. const char *name;
  337. const char *arg;
  338. const char *deprecated;
  339. } script_function;
  340. extern script_function buildin_func[];
  341. static struct linkdb_node *sleep_db; // int32 oid -> struct script_state *
  342. /*==========================================
  343. * (Only those needed) local declaration prototype
  344. *------------------------------------------*/
  345. const char* parse_subexpr(const char* p,int32 limit);
  346. int32 run_func(struct script_state *st);
  347. int32 script_instancegetid(struct script_state *st, e_instance_mode mode = IM_NONE);
  348. const char* script_op2name(int32 op)
  349. {
  350. #define RETURN_OP_NAME(type) case type: return #type
  351. switch( op )
  352. {
  353. RETURN_OP_NAME(C_NOP);
  354. RETURN_OP_NAME(C_POS);
  355. RETURN_OP_NAME(C_INT);
  356. RETURN_OP_NAME(C_PARAM);
  357. RETURN_OP_NAME(C_FUNC);
  358. RETURN_OP_NAME(C_STR);
  359. RETURN_OP_NAME(C_CONSTSTR);
  360. RETURN_OP_NAME(C_ARG);
  361. RETURN_OP_NAME(C_NAME);
  362. RETURN_OP_NAME(C_EOL);
  363. RETURN_OP_NAME(C_RETINFO);
  364. RETURN_OP_NAME(C_USERFUNC);
  365. RETURN_OP_NAME(C_USERFUNC_POS);
  366. RETURN_OP_NAME(C_REF);
  367. // operators
  368. RETURN_OP_NAME(C_OP3);
  369. RETURN_OP_NAME(C_LOR);
  370. RETURN_OP_NAME(C_LAND);
  371. RETURN_OP_NAME(C_LE);
  372. RETURN_OP_NAME(C_LT);
  373. RETURN_OP_NAME(C_GE);
  374. RETURN_OP_NAME(C_GT);
  375. RETURN_OP_NAME(C_EQ);
  376. RETURN_OP_NAME(C_NE);
  377. RETURN_OP_NAME(C_XOR);
  378. RETURN_OP_NAME(C_OR);
  379. RETURN_OP_NAME(C_AND);
  380. RETURN_OP_NAME(C_ADD);
  381. RETURN_OP_NAME(C_SUB);
  382. RETURN_OP_NAME(C_MUL);
  383. RETURN_OP_NAME(C_DIV);
  384. RETURN_OP_NAME(C_MOD);
  385. RETURN_OP_NAME(C_NEG);
  386. RETURN_OP_NAME(C_LNOT);
  387. RETURN_OP_NAME(C_NOT);
  388. RETURN_OP_NAME(C_R_SHIFT);
  389. RETURN_OP_NAME(C_L_SHIFT);
  390. RETURN_OP_NAME(C_ADD_POST);
  391. RETURN_OP_NAME(C_SUB_POST);
  392. RETURN_OP_NAME(C_ADD_PRE);
  393. RETURN_OP_NAME(C_SUB_PRE);
  394. default:
  395. ShowDebug("script_op2name: unexpected op=%d\n", op);
  396. return "???";
  397. }
  398. #undef RETURN_OP_NAME
  399. }
  400. #ifdef DEBUG_DUMP_STACK
  401. static void script_dump_stack(struct script_state* st)
  402. {
  403. int32 i;
  404. ShowMessage("\tstart = %d\n", st->start);
  405. ShowMessage("\tend = %d\n", st->end);
  406. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  407. ShowMessage("\tsp = %d\n", st->stack->sp);
  408. for( i = 0; i < st->stack->sp; ++i )
  409. {
  410. struct script_data* data = &st->stack->stack_data[i];
  411. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  412. switch( data->type )
  413. {
  414. case C_INT:
  415. case C_POS:
  416. ShowMessage(" %d\n", data->u.num);
  417. break;
  418. case C_STR:
  419. case C_CONSTSTR:
  420. ShowMessage(" \"%s\"\n", data->u.str);
  421. break;
  422. case C_NAME:
  423. ShowMessage(" \"%s\" (id=%d ref=%p subtype=%s)\n", reference_getname(data), data->u.num, data->ref, script_op2name(str_data[data->u.num].type));
  424. break;
  425. case C_RETINFO:
  426. {
  427. struct script_retinfo* ri = data->u.ri;
  428. ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp);
  429. }
  430. break;
  431. default:
  432. ShowMessage("\n");
  433. break;
  434. }
  435. }
  436. }
  437. #endif
  438. /// Reports on the console the src of a script error.
  439. static void script_reportsrc(struct script_state *st)
  440. {
  441. if( st->oid == 0 )
  442. return; //Can't report source.
  443. struct block_list* bl = map_id2bl(st->oid);
  444. if (!bl)
  445. return;
  446. switch( bl->type ) {
  447. case BL_NPC: {
  448. struct npc_data* nd = (struct npc_data*)bl;
  449. if( bl->m >= 0 )
  450. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", nd->name, map_mapid2mapname(bl->m), bl->x, bl->y);
  451. else
  452. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", nd->name);
  453. ShowDebug( "Source (NPC): %s is located in: %s\n", nd->name, nd->path );
  454. } break;
  455. default:
  456. if( bl->m >= 0 )
  457. ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status_get_name(bl), map_mapid2mapname(bl->m), bl->x, bl->y);
  458. else
  459. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  460. break;
  461. }
  462. }
  463. /// Reports on the console information about the script data.
  464. static void script_reportdata(struct script_data* data)
  465. {
  466. if( data == nullptr )
  467. return;
  468. switch( data->type ) {
  469. case C_NOP:// no value
  470. ShowDebug("Data: nothing (nil)\n");
  471. break;
  472. case C_INT:// number
  473. ShowDebug("Data: number value=%" PRId64 "\n", data->u.num);
  474. break;
  475. case C_STR:
  476. case C_CONSTSTR:// string
  477. if( data->u.str ) {
  478. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  479. } else {
  480. ShowDebug("Data: string value=nullptr\n");
  481. }
  482. break;
  483. case C_NAME:// reference
  484. if( reference_tovariable(data) ) {// variable
  485. const char* name = reference_getname(data);
  486. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  487. } else if( reference_toconstant(data) ) {// constant
  488. ShowDebug("Data: constant name='%s' value=%" PRId64 "\n", reference_getname(data), reference_getconstant(data));
  489. } else if( reference_toparam(data) ) {// param
  490. ShowDebug("Data: param name='%s' type=%" PRId64 "\n", reference_getname(data), reference_getparamtype(data));
  491. } else {// ???
  492. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  493. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  494. }
  495. break;
  496. case C_POS:// label
  497. ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num);
  498. break;
  499. default:
  500. ShowDebug("Data: %s\n", script_op2name(data->type));
  501. break;
  502. }
  503. }
  504. /// Reports on the console information about the current built-in function.
  505. static void script_reportfunc(struct script_state* st)
  506. {
  507. int32 params, id;
  508. struct script_data* data;
  509. if( !script_hasdata(st,0) )
  510. {// no stack
  511. return;
  512. }
  513. data = script_getdata(st,0);
  514. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  515. {// script currently not executing a built-in function or corrupt stack
  516. return;
  517. }
  518. id = reference_getid(data);
  519. params = script_lastdata(st)-1;
  520. if( params > 0 )
  521. {
  522. int32 i;
  523. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  524. for( i = 2; i <= script_lastdata(st); i++ )
  525. {
  526. script_reportdata(script_getdata(st,i));
  527. }
  528. }
  529. else
  530. {
  531. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  532. }
  533. }
  534. /*==========================================
  535. * Output error message
  536. *------------------------------------------*/
  537. static void disp_error_message2(const char *mes,const char *pos,int32 report)
  538. {
  539. error_msg = aStrdup(mes);
  540. error_pos = pos;
  541. error_report = report;
  542. longjmp( error_jump, 1 );
  543. }
  544. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  545. static void disp_warning_message(const char *mes, const char *pos) {
  546. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  547. }
  548. /// Checks event parameter validity
  549. static void check_event(struct script_state *st, const char *evt)
  550. {
  551. if( evt && evt[0] && !stristr(evt, "::On") )
  552. {
  553. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  554. script_reportsrc(st);
  555. }
  556. }
  557. /*==========================================
  558. * Hashes the input string
  559. *------------------------------------------*/
  560. static uint32 calc_hash(const char* p)
  561. {
  562. uint32 h;
  563. #if defined(SCRIPT_HASH_DJB2)
  564. h = 5381;
  565. while( *p ) // hash*33 + c
  566. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  567. #elif defined(SCRIPT_HASH_SDBM)
  568. h = 0;
  569. while( *p ) // hash*65599 + c
  570. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  571. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  572. h = 0;
  573. while( *p ){
  574. uint32 g;
  575. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  576. g = h & 0xF0000000;
  577. if( g )
  578. {
  579. h ^= g >> 24;
  580. h &= ~g;
  581. }
  582. }
  583. #else // athena hash
  584. h = 0;
  585. while( *p )
  586. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  587. #endif
  588. return h % SCRIPT_HASH_SIZE;
  589. }
  590. bool script_check_RegistryVariableLength(int32 pType, const char *val, size_t* vlen)
  591. {
  592. size_t len = strlen(val);
  593. if (vlen)
  594. *vlen = len;
  595. switch (pType) {
  596. case 0:
  597. return (len < 33); // key check
  598. case 1:
  599. return (len < 255); // value check
  600. default:
  601. return false;
  602. }
  603. }
  604. /*==========================================
  605. * str_data manipulation functions
  606. *------------------------------------------*/
  607. /// Looks up string using the provided id.
  608. const char* get_str(int32 id)
  609. {
  610. Assert( id >= LABEL_START && id < str_size );
  611. return str_buf+str_data[id].str;
  612. }
  613. /// Returns the uid of the string, or -1.
  614. static int32 search_str(const char* p)
  615. {
  616. int32 i;
  617. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  618. if( strcasecmp(get_str(i),p) == 0 )
  619. return i;
  620. return -1;
  621. }
  622. /// Stores a copy of the string and returns its id.
  623. /// If an identical string is already present, returns its id instead.
  624. int32 add_str(const char* p)
  625. {
  626. int32 h;
  627. int32 len;
  628. h = calc_hash(p);
  629. if( str_hash[h] == 0 )
  630. {// empty bucket, add new node here
  631. str_hash[h] = str_num;
  632. }
  633. else
  634. {// scan for end of list, or occurence of identical string
  635. int32 i;
  636. for( i = str_hash[h]; ; i = str_data[i].next )
  637. {
  638. if( strcasecmp(get_str(i),p) == 0 )
  639. return i; // string already in list
  640. if( str_data[i].next == 0 )
  641. break; // reached the end
  642. }
  643. // append node to end of list
  644. str_data[i].next = str_num;
  645. }
  646. // grow list if neccessary
  647. if( str_num >= str_data_size )
  648. {
  649. str_data_size += 128;
  650. RECREATE(str_data,struct str_data_struct,str_data_size);
  651. memset(str_data + (str_data_size - 128), '\0', 128);
  652. }
  653. len=(int)strlen(p);
  654. // grow string buffer if neccessary
  655. while( str_pos+len+1 >= str_size )
  656. {
  657. str_size += 256;
  658. RECREATE(str_buf,char,str_size);
  659. memset(str_buf + (str_size - 256), '\0', 256);
  660. }
  661. safestrncpy(str_buf+str_pos, p, len+1);
  662. str_data[str_num].type = C_NOP;
  663. str_data[str_num].str = str_pos;
  664. str_data[str_num].next = 0;
  665. str_data[str_num].func = nullptr;
  666. str_data[str_num].backpatch = -1;
  667. str_data[str_num].label = -1;
  668. str_pos += len+1;
  669. return str_num++;
  670. }
  671. /// Appends 1 byte to the script buffer.
  672. static void add_scriptb(uint8 a)
  673. {
  674. if( script_pos+1 >= script_size )
  675. {
  676. script_size += SCRIPT_BLOCK_SIZE;
  677. RECREATE(script_buf,unsigned char,script_size);
  678. }
  679. script_buf[script_pos++] = a;
  680. }
  681. /// Appends a c_op value to the script buffer.
  682. /// The value is variable-length encoded into 8-bit blocks.
  683. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  684. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  685. static void add_scriptc(int32 a)
  686. {
  687. while( a >= 0x40 )
  688. {
  689. add_scriptb((a&0x3f)|0x40);
  690. a = (a - 0x40) >> 6;
  691. }
  692. add_scriptb(a);
  693. }
  694. /// Appends an integer value to the script buffer.
  695. /// The value is variable-length encoded into 8-bit blocks.
  696. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  697. /// All blocks hold 6 bits of data.
  698. static void add_scripti(int64 a){
  699. while( a > 0x3f ){
  700. add_scriptb((a & (int64)0x3f)|(int64)0xc0);
  701. a >>= 6;
  702. }
  703. add_scriptb(((a & (int64)0x3f)|(int64)0x80));
  704. }
  705. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  706. ///
  707. /// @param l The id of the str_data entry
  708. // Maximum up to 16M
  709. static void add_scriptl(int32 l)
  710. {
  711. int32 backpatch = str_data[l].backpatch;
  712. switch(str_data[l].type){
  713. case C_POS:
  714. case C_USERFUNC_POS:
  715. add_scriptc(C_POS);
  716. add_scriptb(str_data[l].label);
  717. add_scriptb(str_data[l].label>>8);
  718. add_scriptb(str_data[l].label>>16);
  719. break;
  720. case C_NOP:
  721. case C_USERFUNC:
  722. // Embedded data backpatch there is a possibility of label
  723. add_scriptc(C_NAME);
  724. str_data[l].backpatch = script_pos;
  725. add_scriptb(backpatch);
  726. add_scriptb(backpatch>>8);
  727. add_scriptb(backpatch>>16);
  728. break;
  729. case C_INT:
  730. add_scripti(std::abs(str_data[l].val));
  731. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  732. add_scriptc(C_NEG);
  733. break;
  734. default: // assume C_NAME
  735. add_scriptc(C_NAME);
  736. add_scriptb(l);
  737. add_scriptb(l>>8);
  738. add_scriptb(l>>16);
  739. break;
  740. }
  741. }
  742. /*==========================================
  743. * Resolve the label
  744. *------------------------------------------*/
  745. void set_label(int32 l,int32 pos, const char* script_pos_cur)
  746. {
  747. int32 i;
  748. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  749. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  750. disp_error_message("set_label: invalid label name",script_pos_cur);
  751. return;
  752. }
  753. if(str_data[l].label!=-1){
  754. disp_error_message("set_label: dup label ",script_pos_cur);
  755. return;
  756. }
  757. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  758. str_data[l].label=pos;
  759. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  760. int32 next=GETVALUE(script_buf,i);
  761. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  762. SETVALUE(script_buf,i,pos);
  763. i=next;
  764. }
  765. }
  766. /// Skips spaces and/or comments.
  767. const char* skip_space(const char* p)
  768. {
  769. if( p == nullptr )
  770. return nullptr;
  771. for(;;)
  772. {
  773. while( ISSPACE(*p) )
  774. ++p;
  775. if( *p == '/' && p[1] == '/' )
  776. {// line comment
  777. while(*p && *p!='\n')
  778. ++p;
  779. }
  780. else if( *p == '/' && p[1] == '*' )
  781. {// block comment
  782. p += 2;
  783. for(;;)
  784. {
  785. if( *p == '\0' ) {
  786. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p);
  787. return p;
  788. }
  789. if( *p == '*' && p[1] == '/' )
  790. {// end of block comment
  791. p += 2;
  792. break;
  793. }
  794. ++p;
  795. }
  796. }
  797. else
  798. break;
  799. }
  800. return p;
  801. }
  802. /// Skips a word.
  803. /// A word consists of undercores and/or alphanumeric characters,
  804. /// and valid variable prefixes/postfixes.
  805. static const char* skip_word(const char* p)
  806. {
  807. // prefix
  808. switch( *p ) {
  809. case '@':// temporary char variable
  810. ++p; break;
  811. case '#':// account variable
  812. p += ( p[1] == '#' ? 2 : 1 ); break;
  813. case '\'':// instance variable
  814. ++p; break;
  815. case '.':// npc variable
  816. p += ( p[1] == '@' ? 2 : 1 ); break;
  817. case '$':// global variable
  818. p += ( p[1] == '@' ? 2 : 1 ); break;
  819. }
  820. while( ISALNUM(*p) || *p == '_' )
  821. ++p;
  822. // postfix
  823. if( *p == '$' )// string
  824. p++;
  825. return p;
  826. }
  827. /// Adds a word to str_data.
  828. /// @see skip_word
  829. /// @see add_str
  830. static int32 add_word(const char* p)
  831. {
  832. char* word;
  833. int32 i;
  834. // Check for a word
  835. size_t len = skip_word( p ) - p;
  836. if( len == 0 )
  837. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  838. // Duplicate the word
  839. word = (char*)aMalloc(len+1);
  840. memcpy(word, p, len);
  841. word[len] = 0;
  842. // add the word
  843. i = add_str(word);
  844. aFree(word);
  845. return i;
  846. }
  847. /// Parses a function call.
  848. /// The argument list can have parenthesis or not.
  849. /// The number of arguments is checked.
  850. static
  851. const char* parse_callfunc(const char* p, int32 require_paren, int32 is_custom)
  852. {
  853. const char* p2;
  854. const char* arg=nullptr;
  855. int32 func;
  856. func = add_word(p);
  857. if( str_data[func].type == C_FUNC ){
  858. // buildin function
  859. add_scriptl(func);
  860. add_scriptc(C_ARG);
  861. arg = buildin_func[str_data[func].val].arg;
  862. #if defined(SCRIPT_COMMAND_DEPRECATION)
  863. if( str_data[func].deprecated ){
  864. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  865. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  866. }
  867. #endif
  868. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  869. // script defined function
  870. add_scriptl(buildin_callsub_ref);
  871. add_scriptc(C_ARG);
  872. add_scriptl(func);
  873. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  874. if( *arg == 0 )
  875. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  876. if( *arg != '*' )
  877. ++arg; // count func as argument
  878. } else {
  879. const char* name = get_str(func);
  880. if( !is_custom && strdb_get(userfunc_db, name) == nullptr ) {
  881. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  882. } else {;
  883. add_scriptl(buildin_callfunc_ref);
  884. add_scriptc(C_ARG);
  885. add_scriptc(C_STR);
  886. while( *name ) add_scriptb(*name ++);
  887. add_scriptb(0);
  888. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  889. if( *arg != '*' ) ++ arg;
  890. }
  891. }
  892. p = skip_word(p);
  893. p = skip_space(p);
  894. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  895. syntax.curly[syntax.curly_count].count = 0;
  896. if( *p == ';' )
  897. {// <func name> ';'
  898. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  899. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  900. {// <func name> '(' ')'
  901. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  902. p = p2;
  903. /*
  904. } else if( 0 && require_paren && *p != '(' )
  905. {// <func name>
  906. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  907. */
  908. } else
  909. {// <func name> <arg list>
  910. if( require_paren ){
  911. if( *p != '(' )
  912. disp_error_message("need '('",p);
  913. ++p; // skip '('
  914. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  915. } else if( *p == '(' ){
  916. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  917. } else {
  918. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  919. }
  920. ++syntax.curly_count;
  921. while( *arg ) {
  922. p2=parse_subexpr(p,-1);
  923. if( p == p2 )
  924. break; // not an argument
  925. if( *arg != '*' )
  926. ++arg; // next argument
  927. p=skip_space(p2);
  928. if( *arg == 0 || *p != ',' )
  929. break; // no more arguments
  930. ++p; // skip comma
  931. }
  932. --syntax.curly_count;
  933. }
  934. if( *arg && *arg != '?' && *arg != '*' )
  935. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  936. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  937. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  938. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  939. if( *p != ')' )
  940. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  941. ++p;
  942. }
  943. add_scriptc(C_FUNC);
  944. return p;
  945. }
  946. /// Processes end of logical script line.
  947. /// @param first When true, only fix up scheduling data is initialized
  948. /// @param p Script position for error reporting in set_label
  949. static void parse_nextline(bool first, const char* p)
  950. {
  951. if( !first )
  952. {
  953. add_scriptc(C_EOL); // mark end of line for stack cleanup
  954. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  955. }
  956. // initialize data for new '-' label fix up scheduling
  957. str_data[LABEL_NEXTLINE].type = C_NOP;
  958. str_data[LABEL_NEXTLINE].backpatch = -1;
  959. str_data[LABEL_NEXTLINE].label = -1;
  960. }
  961. /**
  962. * Pushes a variable into stack, processing its array index if needed.
  963. * @see parse_variable
  964. */
  965. void parse_variable_sub_push(int32 word, const char *p2)
  966. {
  967. if( p2 ) { // Process the variable index
  968. const char *p3 = nullptr;
  969. // Push the getelementofarray method into the stack
  970. add_scriptl(buildin_getelementofarray_ref);
  971. add_scriptc(C_ARG);
  972. add_scriptl(word);
  973. // Process the sub-expression for this assignment
  974. p3 = parse_subexpr(p2 + 1, 1);
  975. p3 = skip_space(p3);
  976. if( *p3 != ']' ) // Closing parenthesis is required for this script
  977. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  978. // Push the closing function stack operator onto the stack
  979. add_scriptc(C_FUNC);
  980. p3++;
  981. } else // No array index, simply push the variable or value onto the stack
  982. add_scriptl(word);
  983. }
  984. /// Parse a variable assignment using the direct equals operator
  985. /// @param p script position where the function should run from
  986. /// @return nullptr if not a variable assignment, the new position otherwise
  987. const char* parse_variable(const char* p) {
  988. int32 word;
  989. c_op type = C_NOP;
  990. const char *p2 = nullptr;
  991. const char *var = p;
  992. if( p[0] == '+' && p[1] == '+' ){
  993. type = C_ADD_PRE; // pre ++
  994. }else if( p[0] == '-' && p[1] == '-' ){
  995. type = C_SUB_PRE; // pre --
  996. }
  997. if( type != C_NOP ){
  998. var = p = skip_space(&p[2]);
  999. }
  1000. // skip the variable where applicable
  1001. p = skip_word(p);
  1002. p = skip_space(p);
  1003. if( p == nullptr ) {// end of the line or invalid buffer
  1004. return nullptr;
  1005. }
  1006. if( *p == '[' ) {// array variable so process the array as appropriate
  1007. int32 i,j;
  1008. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1009. if( *p ++ == ']' && --(j) == 0 ) break;
  1010. if( *p == '[' ) ++ j;
  1011. }
  1012. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1013. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1014. }
  1015. }
  1016. if( type == C_NOP ){
  1017. if( p[0] == '=' && p[1] != '=' ){
  1018. type = C_EQ; // =
  1019. }else if( p[0] == '+' && p[1] == '=' ){
  1020. type = C_ADD; // +=
  1021. }else if( p[0] == '-' && p[1] == '=' ){
  1022. type = C_SUB; // -=
  1023. }else if( p[0] == '^' && p[1] == '=' ){
  1024. type = C_XOR; // ^=
  1025. }else if( p[0] == '|' && p[1] == '=' ){
  1026. type = C_OR; // |=
  1027. }else if( p[0] == '&' && p[1] == '=' ){
  1028. type = C_AND; // &=
  1029. }else if( p[0] == '*' && p[1] == '=' ){
  1030. type = C_MUL; // *=
  1031. }else if( p[0] == '/' && p[1] == '=' ){
  1032. type = C_DIV; // /=
  1033. }else if( p[0] == '%' && p[1] == '=' ){
  1034. type = C_MOD; // %=
  1035. }else if( p[0] == '~' && p[1] == '=' ){
  1036. type = C_NOT; // ~=
  1037. }else if( p[0] == '+' && p[1] == '+' ){
  1038. type = C_ADD_POST; // post ++
  1039. }else if( p[0] == '-' && p[1] == '-' ){
  1040. type = C_SUB_POST; // post --
  1041. }else if( p[0] == '<' && p[1] == '<' && p[2] == '=' ){
  1042. type = C_L_SHIFT; // <<=
  1043. }else if( p[0] == '>' && p[1] == '>' && p[2] == '=' ){
  1044. type = C_R_SHIFT; // >>=
  1045. }else{
  1046. // failed to find a matching operator combination so invalid
  1047. return nullptr;
  1048. }
  1049. }
  1050. switch( type ) {
  1051. case C_ADD_PRE: // pre ++
  1052. case C_SUB_PRE: // pre --
  1053. // Nothing more to skip
  1054. break;
  1055. case C_EQ: {// incremental modifier
  1056. p = skip_space( &p[1] );
  1057. }
  1058. break;
  1059. case C_L_SHIFT:
  1060. case C_R_SHIFT: {// left or right shift modifier
  1061. p = skip_space( &p[3] );
  1062. }
  1063. break;
  1064. default: {// normal incremental command
  1065. p = skip_space( &p[2] );
  1066. }
  1067. }
  1068. if( p == nullptr ) {// end of line or invalid buffer
  1069. return nullptr;
  1070. }
  1071. // push the set function onto the stack
  1072. add_scriptl(buildin_set_ref);
  1073. add_scriptc(C_ARG);
  1074. // always append parenthesis to avoid errors
  1075. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1076. syntax.curly[syntax.curly_count].count = 0;
  1077. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1078. // increment the total curly count for the position in the script
  1079. ++ syntax.curly_count;
  1080. // parse the variable currently being modified
  1081. word = add_word(var);
  1082. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS ) // cannot assign a variable which exists as a function or label
  1083. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1084. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1085. if( type != C_EQ )
  1086. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1087. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1088. add_scripti(1);
  1089. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1090. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1091. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1092. add_scripti(1);
  1093. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1094. } else { // Process the value as an expression
  1095. p = parse_subexpr(p, -1);
  1096. if( type != C_EQ )
  1097. {// push the type of modifier onto the stack
  1098. add_scriptc(type);
  1099. }
  1100. }
  1101. // decrement the curly count for the position within the script
  1102. -- syntax.curly_count;
  1103. // close the script by appending the function operator
  1104. add_scriptc(C_FUNC);
  1105. // push the buffer from the method
  1106. return p;
  1107. }
  1108. /*
  1109. * Checks whether the gives string is a number literal
  1110. *
  1111. * Mainly necessary to differentiate between number literals and NPC name
  1112. * constants, since several of those start with a digit.
  1113. *
  1114. * All this does is to check if the string begins with an optional + or - sign,
  1115. * followed by a hexadecimal or decimal number literal literal and is NOT
  1116. * followed by a underscore or letter.
  1117. *
  1118. * @author : Hercules.ws
  1119. * @param p Pointer to the string to check
  1120. * @return Whether the string is a number literal
  1121. */
  1122. bool is_number(const char *p) {
  1123. const char *np;
  1124. if (!p)
  1125. return false;
  1126. if (*p == '-' || *p == '+')
  1127. p++;
  1128. np = p;
  1129. if (*p == '0' && p[1] == 'x') {
  1130. p+=2;
  1131. np = p;
  1132. // Hexadecimal
  1133. while (ISXDIGIT(*np))
  1134. np++;
  1135. } else {
  1136. // Decimal
  1137. while (ISDIGIT(*np))
  1138. np++;
  1139. }
  1140. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1141. return true;
  1142. return false;
  1143. }
  1144. /*==========================================
  1145. * Analysis section
  1146. *------------------------------------------*/
  1147. const char* parse_simpleexpr(const char *p)
  1148. {
  1149. int64 i;
  1150. p=skip_space(p);
  1151. if(*p==';' || *p==',')
  1152. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1153. if(*p=='('){
  1154. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1155. ++syntax.curly[i].count;
  1156. p=parse_subexpr(p+1,-1);
  1157. p=skip_space(p);
  1158. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1159. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1160. ){
  1161. if( *p == ',' ){
  1162. syntax.curly[i].flag = ARGLIST_PAREN;
  1163. return p;
  1164. } else
  1165. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1166. }
  1167. if( *p != ')' )
  1168. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1169. ++p;
  1170. } else if(is_number(p)) {
  1171. char *np;
  1172. bool error;
  1173. // Skip leading zeroes
  1174. while(*p == '0' && ISDIGIT(p[1])) p++;
  1175. errno = 0;
  1176. i=strtoll(p,&np,0);
  1177. error = (errno == ERANGE);
  1178. if( i < SCRIPT_INT_MIN || ( error && i == INT64_MIN ) ){
  1179. i = SCRIPT_INT_MIN;
  1180. disp_warning_message( "parse_simpleexpr: underflow detected, capping value to SCRIPT_INT_MIN", p );
  1181. }else if( i > SCRIPT_INT_MAX || ( error && i == INT64_MAX ) ){
  1182. i = SCRIPT_INT_MAX;
  1183. disp_warning_message( "parse_simpleexpr: overflow detected, capping value to SCRIPT_INT_MAX", p );
  1184. }
  1185. add_scripti(i);
  1186. p=np;
  1187. } else if(*p=='"'){
  1188. add_scriptc(C_STR);
  1189. p++;
  1190. while( *p && *p != '"' ){
  1191. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1192. {
  1193. char buf[8];
  1194. size_t len = skip_escaped_c(p) - p;
  1195. size_t n = sv_unescape_c(buf, p, len);
  1196. if( n != 1 )
  1197. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1198. p += len;
  1199. add_scriptb(*buf);
  1200. continue;
  1201. }
  1202. else if( *p == '\n' )
  1203. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1204. add_scriptb(*p++);
  1205. }
  1206. if(!*p)
  1207. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1208. add_scriptb(0);
  1209. p++; //'"'
  1210. } else {
  1211. int32 l;
  1212. const char* pv;
  1213. // label , register , function etc
  1214. if(skip_word(p)==p)
  1215. disp_error_message("parse_simpleexpr: unexpected character",p);
  1216. l=add_word(p);
  1217. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1218. return parse_callfunc(p,1,0);
  1219. else {
  1220. const char* name = get_str(l);
  1221. if( strdb_get(userfunc_db,name) != nullptr ) {
  1222. return parse_callfunc(p,1,1);
  1223. }
  1224. }
  1225. if( (pv = parse_variable(p)) )
  1226. {// successfully processed a variable assignment
  1227. return pv;
  1228. }
  1229. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1230. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1231. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1232. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1233. if (str_data[l].name)
  1234. ShowWarning( "Please use '%s' instead!\n", str_data[l].name );
  1235. disp_warning_message("parse_simpleexpr: deprecated constant", p);
  1236. }
  1237. #endif
  1238. p=skip_word(p);
  1239. if( *p == '[' ){
  1240. // array(name[i] => getelementofarray(name,i) )
  1241. add_scriptl(buildin_getelementofarray_ref);
  1242. add_scriptc(C_ARG);
  1243. add_scriptl(l);
  1244. p=parse_subexpr(p+1,-1);
  1245. p=skip_space(p);
  1246. if( *p != ']' )
  1247. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1248. ++p;
  1249. add_scriptc(C_FUNC);
  1250. }else
  1251. add_scriptl(l);
  1252. }
  1253. return p;
  1254. }
  1255. /*==========================================
  1256. * Analysis of the expression
  1257. *------------------------------------------*/
  1258. const char* parse_subexpr(const char* p,int32 limit)
  1259. {
  1260. int32 op,opl,len;
  1261. p=skip_space(p);
  1262. if( *p == '-' ){
  1263. const char* tmpp = skip_space(p+1);
  1264. if( *tmpp == ';' || *tmpp == ',' ){
  1265. add_scriptl(LABEL_NEXTLINE);
  1266. p++;
  1267. return p;
  1268. }
  1269. }
  1270. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1271. p = parse_variable(p);
  1272. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1273. p = parse_subexpr(p + 1, 11);
  1274. add_scriptc(op);
  1275. } else
  1276. p = parse_simpleexpr(p);
  1277. p = skip_space(p);
  1278. while((
  1279. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1280. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1281. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1282. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1283. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1284. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1285. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1286. (op=C_AND,opl=5,len=1,*p=='&') ||
  1287. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1288. (op=C_OR,opl=3,len=1,*p=='|') ||
  1289. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1290. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1291. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1292. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1293. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1294. (op=C_GT,opl=7,len=1,*p=='>') ||
  1295. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1296. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1297. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1298. p+=len;
  1299. if(op == C_OP3) {
  1300. p=parse_subexpr(p,-1);
  1301. p=skip_space(p);
  1302. if( *(p++) != ':')
  1303. disp_error_message("parse_subexpr: expected ':'", p-1);
  1304. p=parse_subexpr(p,-1);
  1305. } else {
  1306. p=parse_subexpr(p,opl);
  1307. }
  1308. add_scriptc(op);
  1309. p=skip_space(p);
  1310. }
  1311. return p; /* return first untreated operator */
  1312. }
  1313. /*==========================================
  1314. * Evaluation of the expression
  1315. *------------------------------------------*/
  1316. const char* parse_expr(const char *p)
  1317. {
  1318. switch(*p){
  1319. case ')': case ';': case ':': case '[': case ']':
  1320. case '}':
  1321. disp_error_message("parse_expr: unexpected character",p);
  1322. }
  1323. p=parse_subexpr(p,-1);
  1324. return p;
  1325. }
  1326. /*==========================================
  1327. * Analysis of the line
  1328. *------------------------------------------*/
  1329. const char* parse_line(const char* p)
  1330. {
  1331. const char* p2;
  1332. p=skip_space(p);
  1333. if(*p==';') {
  1334. //Close decision for if(); for(); while();
  1335. p = parse_syntax_close(p + 1);
  1336. return p;
  1337. }
  1338. if(*p==')' && parse_syntax_for_flag)
  1339. return p+1;
  1340. p = skip_space(p);
  1341. if(p[0] == '{') {
  1342. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1343. syntax.curly[syntax.curly_count].count = -1;
  1344. syntax.curly[syntax.curly_count].index = -1;
  1345. syntax.curly_count++;
  1346. return p + 1;
  1347. } else if(p[0] == '}') {
  1348. return parse_curly_close(p);
  1349. }
  1350. // Syntax-related processing
  1351. p2 = parse_syntax(p);
  1352. if(p2 != nullptr)
  1353. return p2;
  1354. // attempt to process a variable assignment
  1355. p2 = parse_variable(p);
  1356. if( p2 != nullptr )
  1357. {// variable assignment processed so leave the method
  1358. return parse_syntax_close(p2 + 1);
  1359. }
  1360. p = parse_callfunc(p,0,0);
  1361. p = skip_space(p);
  1362. if(parse_syntax_for_flag) {
  1363. if( *p != ')' )
  1364. disp_error_message("parse_line: expected ')'",p);
  1365. } else {
  1366. if( *p != ';' )
  1367. disp_error_message("parse_line: expected ';'",p);
  1368. }
  1369. //Binding decision for if(), for(), while()
  1370. p = parse_syntax_close(p+1);
  1371. return p;
  1372. }
  1373. // { ... } Closing process
  1374. const char* parse_curly_close(const char* p)
  1375. {
  1376. if(syntax.curly_count <= 0) {
  1377. disp_error_message("parse_curly_close: unexpected string",p);
  1378. return p + 1;
  1379. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1380. syntax.curly_count--;
  1381. //Close decision if, for , while
  1382. p = parse_syntax_close(p + 1);
  1383. return p;
  1384. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1385. //Closing switch()
  1386. int32 pos = syntax.curly_count-1;
  1387. char label[256];
  1388. int32 l;
  1389. // Remove temporary variables
  1390. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1391. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1392. parse_line(label);
  1393. syntax.curly_count--;
  1394. // Go to the end pointer unconditionally
  1395. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1396. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1397. parse_line(label);
  1398. syntax.curly_count--;
  1399. // You are here labeled
  1400. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1401. l=add_str(label);
  1402. set_label(l,script_pos, p);
  1403. if(syntax.curly[pos].flag) {
  1404. //Exists default
  1405. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1406. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1407. parse_line(label);
  1408. syntax.curly_count--;
  1409. }
  1410. // Label end
  1411. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1412. l=add_str(label);
  1413. set_label(l,script_pos, p);
  1414. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1415. syntax.curly_count--;
  1416. //Closing decision if, for , while
  1417. p = parse_syntax_close(p + 1);
  1418. return p;
  1419. } else {
  1420. disp_error_message("parse_curly_close: unexpected string",p);
  1421. return p + 1;
  1422. }
  1423. }
  1424. // Syntax-related processing
  1425. // break, case, continue, default, do, for, function,
  1426. // if, switch, while ? will handle this internally.
  1427. const char* parse_syntax(const char* p)
  1428. {
  1429. const char *p2 = skip_word(p);
  1430. switch(*p) {
  1431. case 'B':
  1432. case 'b':
  1433. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1434. // break Processing
  1435. char label[256];
  1436. int32 pos = syntax.curly_count - 1;
  1437. while(pos >= 0) {
  1438. if(syntax.curly[pos].type == TYPE_DO) {
  1439. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1440. break;
  1441. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1442. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1443. break;
  1444. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1445. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1446. break;
  1447. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1448. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1449. break;
  1450. }
  1451. pos--;
  1452. }
  1453. if(pos < 0) {
  1454. disp_error_message("parse_syntax: unexpected 'break'",p);
  1455. } else {
  1456. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1457. parse_line(label);
  1458. syntax.curly_count--;
  1459. }
  1460. p = skip_space(p2);
  1461. if(*p != ';')
  1462. disp_error_message("parse_syntax: expected ';'",p);
  1463. // Closing decision if, for , while
  1464. p = parse_syntax_close(p + 1);
  1465. return p;
  1466. }
  1467. break;
  1468. case 'c':
  1469. case 'C':
  1470. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1471. //Processing case
  1472. int32 pos = syntax.curly_count-1;
  1473. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1474. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1475. return p+1;
  1476. } else {
  1477. char label[256];
  1478. int32 l;
  1479. int64 v;
  1480. char *np;
  1481. if(syntax.curly[pos].count != 1) {
  1482. //Jump for FALLTHRU
  1483. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1484. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1485. parse_line(label);
  1486. syntax.curly_count--;
  1487. // You are here labeled
  1488. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1489. l=add_str(label);
  1490. set_label(l,script_pos, p);
  1491. }
  1492. //Decision statement switch
  1493. p = skip_space(p2);
  1494. if(p == p2) {
  1495. disp_error_message("parse_syntax: expected a space ' '",p);
  1496. }
  1497. // check whether case label is integer or not
  1498. if(is_number(p)) {
  1499. //Numeric value
  1500. v = (int)strtol(p,&np,0);
  1501. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1502. p++;
  1503. p = skip_word(p);
  1504. if(np != p)
  1505. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1506. } else {
  1507. //Check for constants
  1508. p2 = skip_word(p);
  1509. v = (int)(size_t) (p2-p); // length of word at p2
  1510. memcpy(label,p,static_cast<int>(v));
  1511. label[v]='\0';
  1512. if( !script_get_constant(label, &v) )
  1513. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1514. p = skip_word(p);
  1515. }
  1516. p = skip_space(p);
  1517. if(*p != ':')
  1518. disp_error_message("parse_syntax: expect ':'",p);
  1519. sprintf(label,"if(%" PRId64 " != $@__SW%x_VAL) goto __SW%x_%x;",
  1520. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1521. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1522. // Bad I do not parse twice
  1523. p2 = parse_line(label);
  1524. parse_line(p2);
  1525. syntax.curly_count--;
  1526. if(syntax.curly[pos].count != 1) {
  1527. // Label after the completion of FALLTHRU
  1528. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1529. l=add_str(label);
  1530. set_label(l,script_pos,p);
  1531. }
  1532. // check duplication of case label [Rayce]
  1533. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != nullptr)
  1534. disp_error_message("parse_syntax: dup 'case'",p);
  1535. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1536. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1537. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1538. parse_line(label);
  1539. syntax.curly_count--;
  1540. syntax.curly[pos].count++;
  1541. }
  1542. return p + 1;
  1543. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1544. // Processing continue
  1545. char label[256];
  1546. int32 pos = syntax.curly_count - 1;
  1547. while(pos >= 0) {
  1548. if(syntax.curly[pos].type == TYPE_DO) {
  1549. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1550. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1551. break;
  1552. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1553. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1554. break;
  1555. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1556. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1557. break;
  1558. }
  1559. pos--;
  1560. }
  1561. if(pos < 0) {
  1562. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1563. } else {
  1564. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1565. parse_line(label);
  1566. syntax.curly_count--;
  1567. }
  1568. p = skip_space(p2);
  1569. if(*p != ';')
  1570. disp_error_message("parse_syntax: expected ';'",p);
  1571. //Closing decision if, for , while
  1572. p = parse_syntax_close(p + 1);
  1573. return p;
  1574. }
  1575. break;
  1576. case 'd':
  1577. case 'D':
  1578. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1579. // Switch - default processing
  1580. int32 pos = syntax.curly_count-1;
  1581. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1582. disp_error_message("parse_syntax: unexpected 'default'",p);
  1583. } else if(syntax.curly[pos].flag) {
  1584. disp_error_message("parse_syntax: dup 'default'",p);
  1585. } else {
  1586. char label[256];
  1587. int32 l;
  1588. // Put the label location
  1589. p = skip_space(p2);
  1590. if(*p != ':') {
  1591. disp_error_message("parse_syntax: expected ':'",p);
  1592. }
  1593. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1594. l=add_str(label);
  1595. set_label(l,script_pos,p);
  1596. // Skip to the next link w/o condition
  1597. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1598. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1599. parse_line(label);
  1600. syntax.curly_count--;
  1601. // The default label
  1602. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1603. l=add_str(label);
  1604. set_label(l,script_pos,p);
  1605. syntax.curly[syntax.curly_count - 1].flag = 1;
  1606. syntax.curly[pos].count++;
  1607. }
  1608. return p + 1;
  1609. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1610. int32 l;
  1611. char label[256];
  1612. p=skip_space(p2);
  1613. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1614. syntax.curly[syntax.curly_count].count = 1;
  1615. syntax.curly[syntax.curly_count].index = syntax.index++;
  1616. syntax.curly[syntax.curly_count].flag = 0;
  1617. // Label of the (do) form here
  1618. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1619. l=add_str(label);
  1620. set_label(l,script_pos,p);
  1621. syntax.curly_count++;
  1622. return p;
  1623. }
  1624. break;
  1625. case 'f':
  1626. case 'F':
  1627. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1628. int32 l;
  1629. char label[256];
  1630. int32 pos = syntax.curly_count;
  1631. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1632. syntax.curly[syntax.curly_count].count = 1;
  1633. syntax.curly[syntax.curly_count].index = syntax.index++;
  1634. syntax.curly[syntax.curly_count].flag = 0;
  1635. syntax.curly_count++;
  1636. p=skip_space(p2);
  1637. if(*p != '(')
  1638. disp_error_message("parse_syntax: expected '('",p);
  1639. p++;
  1640. // Execute the initialization statement
  1641. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1642. p=parse_line(p);
  1643. syntax.curly_count--;
  1644. // Form the start of label decision
  1645. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1646. l=add_str(label);
  1647. set_label(l,script_pos,p);
  1648. p=skip_space(p);
  1649. if(*p == ';') {
  1650. // For (; Because the pattern of always true ;)
  1651. ;
  1652. } else {
  1653. // Skip to the end point if the condition is false
  1654. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1655. add_scriptl(add_str("jump_zero"));
  1656. add_scriptc(C_ARG);
  1657. p=parse_expr(p);
  1658. p=skip_space(p);
  1659. add_scriptl(add_str(label));
  1660. add_scriptc(C_FUNC);
  1661. }
  1662. if(*p != ';')
  1663. disp_error_message("parse_syntax: expected ';'",p);
  1664. p++;
  1665. // Skip to the beginning of the loop
  1666. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1667. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1668. parse_line(label);
  1669. syntax.curly_count--;
  1670. // Labels to form the next loop
  1671. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1672. l=add_str(label);
  1673. set_label(l,script_pos,p);
  1674. // Process the next time you enter the loop
  1675. // A ')' last for; flag to be treated as'
  1676. parse_syntax_for_flag = 1;
  1677. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1678. p=parse_line(p);
  1679. syntax.curly_count--;
  1680. parse_syntax_for_flag = 0;
  1681. // Skip to the determination process conditions
  1682. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1683. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1684. parse_line(label);
  1685. syntax.curly_count--;
  1686. // Loop start labeling
  1687. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1688. l=add_str(label);
  1689. set_label(l,script_pos,p);
  1690. return p;
  1691. }
  1692. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1693. {// internal script function
  1694. const char *func_name;
  1695. func_name = skip_space(p2);
  1696. p = skip_word(func_name);
  1697. if( p == func_name )
  1698. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1699. p2 = skip_space(p);
  1700. if( *p2 == ';' )
  1701. {// function <name> ;
  1702. // function declaration - just register the name
  1703. int32 l;
  1704. l = add_word(func_name);
  1705. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1706. str_data[l].type = C_USERFUNC;
  1707. else if( str_data[l].type == C_USERFUNC )
  1708. ; // already registered
  1709. else
  1710. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1711. // Close condition of if, for, while
  1712. p = parse_syntax_close(p2 + 1);
  1713. return p;
  1714. }
  1715. else if(*p2 == '{')
  1716. {// function <name> <line/block of code>
  1717. char label[256];
  1718. int32 l;
  1719. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1720. syntax.curly[syntax.curly_count].count = 1;
  1721. syntax.curly[syntax.curly_count].index = syntax.index++;
  1722. syntax.curly[syntax.curly_count].flag = 0;
  1723. ++syntax.curly_count;
  1724. // Jump over the function code
  1725. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1726. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1727. ++syntax.curly_count;
  1728. parse_line(label);
  1729. --syntax.curly_count;
  1730. // Set the position of the function (label)
  1731. l=add_word(func_name);
  1732. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1733. {
  1734. str_data[l].type = C_USERFUNC;
  1735. set_label(l, script_pos, p);
  1736. if( parse_options&SCRIPT_USE_LABEL_DB )
  1737. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1738. }
  1739. else
  1740. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1741. return skip_space(p);
  1742. }
  1743. else
  1744. {
  1745. disp_error_message("expect ';' or '{' at function syntax",p);
  1746. }
  1747. }
  1748. break;
  1749. case 'i':
  1750. case 'I':
  1751. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1752. // If process
  1753. char label[256];
  1754. p=skip_space(p2);
  1755. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1756. disp_error_message("need '('",p);
  1757. }
  1758. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1759. syntax.curly[syntax.curly_count].count = 1;
  1760. syntax.curly[syntax.curly_count].index = syntax.index++;
  1761. syntax.curly[syntax.curly_count].flag = 0;
  1762. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1763. syntax.curly_count++;
  1764. add_scriptl(add_str("jump_zero"));
  1765. add_scriptc(C_ARG);
  1766. p=parse_expr(p);
  1767. p=skip_space(p);
  1768. add_scriptl(add_str(label));
  1769. add_scriptc(C_FUNC);
  1770. return p;
  1771. }
  1772. break;
  1773. case 's':
  1774. case 'S':
  1775. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1776. // Processing of switch ()
  1777. char label[256];
  1778. p=skip_space(p2);
  1779. if(*p != '(') {
  1780. disp_error_message("need '('",p);
  1781. }
  1782. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1783. syntax.curly[syntax.curly_count].count = 1;
  1784. syntax.curly[syntax.curly_count].index = syntax.index++;
  1785. syntax.curly[syntax.curly_count].flag = 0;
  1786. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1787. syntax.curly_count++;
  1788. add_scriptl(add_str("set"));
  1789. add_scriptc(C_ARG);
  1790. add_scriptl(add_str(label));
  1791. p=parse_expr(p);
  1792. p=skip_space(p);
  1793. if(*p != '{') {
  1794. disp_error_message("parse_syntax: expected '{'",p);
  1795. }
  1796. add_scriptc(C_FUNC);
  1797. return p + 1;
  1798. }
  1799. break;
  1800. case 'w':
  1801. case 'W':
  1802. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1803. int32 l;
  1804. char label[256];
  1805. p=skip_space(p2);
  1806. if(*p != '(') {
  1807. disp_error_message("need '('",p);
  1808. }
  1809. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1810. syntax.curly[syntax.curly_count].count = 1;
  1811. syntax.curly[syntax.curly_count].index = syntax.index++;
  1812. syntax.curly[syntax.curly_count].flag = 0;
  1813. // Form the start of label decision
  1814. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1815. l=add_str(label);
  1816. set_label(l,script_pos,p);
  1817. // Skip to the end point if the condition is false
  1818. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1819. syntax.curly_count++;
  1820. add_scriptl(add_str("jump_zero"));
  1821. add_scriptc(C_ARG);
  1822. p=parse_expr(p);
  1823. p=skip_space(p);
  1824. add_scriptl(add_str(label));
  1825. add_scriptc(C_FUNC);
  1826. return p;
  1827. }
  1828. break;
  1829. }
  1830. return nullptr;
  1831. }
  1832. const char* parse_syntax_close(const char *p) {
  1833. // If (...) for (...) hoge (); as to make sure closed closed once again
  1834. int32 flag;
  1835. do {
  1836. p = parse_syntax_close_sub(p,&flag);
  1837. } while(flag);
  1838. return p;
  1839. }
  1840. // Close judgment if, for, while, of do
  1841. // flag == 1 : closed
  1842. // flag == 0 : not closed
  1843. const char* parse_syntax_close_sub(const char* p,int* flag)
  1844. {
  1845. char label[256];
  1846. int32 pos = syntax.curly_count - 1;
  1847. int32 l;
  1848. *flag = 1;
  1849. if(syntax.curly_count <= 0) {
  1850. *flag = 0;
  1851. return p;
  1852. } else if(syntax.curly[pos].type == TYPE_IF) {
  1853. const char *bp = p;
  1854. const char *p2;
  1855. // if-block and else-block end is a new line
  1856. parse_nextline(false, p);
  1857. // Skip to the last location if
  1858. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1859. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1860. parse_line(label);
  1861. syntax.curly_count--;
  1862. // Put the label of the location
  1863. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1864. l=add_str(label);
  1865. set_label(l,script_pos,p);
  1866. syntax.curly[pos].count++;
  1867. p = skip_space(p);
  1868. p2 = skip_word(p);
  1869. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1870. // else or else - if
  1871. p = skip_space(p2);
  1872. p2 = skip_word(p);
  1873. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1874. // else - if
  1875. p=skip_space(p2);
  1876. if(*p != '(') {
  1877. disp_error_message("need '('",p);
  1878. }
  1879. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1880. add_scriptl(add_str("jump_zero"));
  1881. add_scriptc(C_ARG);
  1882. p=parse_expr(p);
  1883. p=skip_space(p);
  1884. add_scriptl(add_str(label));
  1885. add_scriptc(C_FUNC);
  1886. *flag = 0;
  1887. return p;
  1888. } else {
  1889. // else
  1890. if(!syntax.curly[pos].flag) {
  1891. syntax.curly[pos].flag = 1;
  1892. *flag = 0;
  1893. return p;
  1894. }
  1895. }
  1896. }
  1897. // Close if
  1898. syntax.curly_count--;
  1899. // Put the label of the final location
  1900. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1901. l=add_str(label);
  1902. set_label(l,script_pos,p);
  1903. if(syntax.curly[pos].flag == 1) {
  1904. // Because the position of the pointer is the same if not else for this
  1905. return bp;
  1906. }
  1907. return p;
  1908. } else if(syntax.curly[pos].type == TYPE_DO) {
  1909. int32 l2;
  1910. char label2[256];
  1911. const char *p2;
  1912. if(syntax.curly[pos].flag) {
  1913. // (Come here continue) to form the label here
  1914. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1915. l2=add_str(label2);
  1916. set_label(l2,script_pos,p);
  1917. }
  1918. // Skip to the end point if the condition is false
  1919. p = skip_space(p);
  1920. p2 = skip_word(p);
  1921. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1922. disp_error_message("parse_syntax: expected 'while'",p);
  1923. p = skip_space(p2);
  1924. if(*p != '(') {
  1925. disp_error_message("need '('",p);
  1926. }
  1927. // do-block end is a new line
  1928. parse_nextline(false, p);
  1929. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1930. add_scriptl(add_str("jump_zero"));
  1931. add_scriptc(C_ARG);
  1932. p=parse_expr(p);
  1933. p=skip_space(p);
  1934. add_scriptl(add_str(label2));
  1935. add_scriptc(C_FUNC);
  1936. // Skip to the starting point
  1937. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1938. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1939. parse_line(label2);
  1940. syntax.curly_count--;
  1941. // Form label of the end point conditions
  1942. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1943. l2=add_str(label2);
  1944. set_label(l2,script_pos,p);
  1945. p = skip_space(p);
  1946. if(*p != ';') {
  1947. disp_error_message("parse_syntax: expected ';'",p);
  1948. return p+1;
  1949. }
  1950. p++;
  1951. syntax.curly_count--;
  1952. return p;
  1953. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1954. // for-block end is a new line
  1955. parse_nextline(false, p);
  1956. // Skip to the next loop
  1957. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1958. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1959. parse_line(label);
  1960. syntax.curly_count--;
  1961. // End for labeling
  1962. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1963. l=add_str(label);
  1964. set_label(l,script_pos,p);
  1965. syntax.curly_count--;
  1966. return p;
  1967. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1968. // while-block end is a new line
  1969. parse_nextline(false, p);
  1970. // Skip to the decision while
  1971. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1972. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1973. parse_line(label);
  1974. syntax.curly_count--;
  1975. // End while labeling
  1976. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1977. l=add_str(label);
  1978. set_label(l,script_pos,p);
  1979. syntax.curly_count--;
  1980. return p;
  1981. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1982. int32 pos2 = syntax.curly_count-1;
  1983. char label2[256];
  1984. int32 l2;
  1985. // Back
  1986. sprintf(label2,"return;");
  1987. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1988. parse_line(label2);
  1989. syntax.curly_count--;
  1990. // Put the label of the location
  1991. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1992. l2=add_str(label2);
  1993. set_label(l2,script_pos,p);
  1994. syntax.curly_count--;
  1995. return p;
  1996. } else {
  1997. *flag = 0;
  1998. return p;
  1999. }
  2000. }
  2001. /*==========================================
  2002. * Added built-in functions
  2003. *------------------------------------------*/
  2004. static void add_buildin_func(void)
  2005. {
  2006. int32 i;
  2007. for( i = 0; buildin_func[i].func; i++ )
  2008. {
  2009. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2010. // 'v' - value (either string or int32 or reference)
  2011. // 's' - string
  2012. // 'i' - int32
  2013. // 'r' - reference (of a variable)
  2014. // 'l' - label
  2015. // '?' - one optional parameter
  2016. // '*' - unknown number of optional parameters
  2017. const char* p = buildin_func[i].arg;
  2018. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2019. while( *p == '?' ) ++p;
  2020. if( *p == '*' ) ++p;
  2021. if( *p != 0){
  2022. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2023. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2024. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2025. } else {
  2026. int32 n = add_str(buildin_func[i].name);
  2027. str_data[n].type = C_FUNC;
  2028. str_data[n].val = i;
  2029. str_data[n].func = buildin_func[i].func;
  2030. str_data[n].deprecated = (buildin_func[i].deprecated != nullptr);
  2031. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2032. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2033. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2034. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2035. }
  2036. }
  2037. }
  2038. /**
  2039. * String comparison with a char array to a script constant
  2040. * @param prefix: Char array to compare
  2041. * @param value: Script constant
  2042. * @return Constant name as char array or nullptr otherwise
  2043. */
  2044. const char* script_get_constant_str( const char* prefix, int64 value ){
  2045. const char* name;
  2046. for(int32 i = 0; i < str_data_size; i++ ){
  2047. // Check if it is a constant
  2048. if( str_data[i].type != C_INT ){
  2049. continue;
  2050. }
  2051. // Check if the value matches
  2052. if( str_data[i].val != value ){
  2053. continue;
  2054. }
  2055. // Look up the actual string
  2056. name = get_str(i);
  2057. // Compare the prefix
  2058. if( !strncasecmp( name, prefix, strlen(prefix) ) ){
  2059. // We found a match
  2060. return name;
  2061. }
  2062. }
  2063. return nullptr;
  2064. }
  2065. /// Retrieves the value of a constant parameter.
  2066. bool script_get_parameter(const char* name, int64* value)
  2067. {
  2068. int32 n = search_str(name);
  2069. if (n == -1 || str_data[n].type != C_PARAM)
  2070. {// not found or not a parameter
  2071. return false;
  2072. }
  2073. value[0] = str_data[n].val;
  2074. return true;
  2075. }
  2076. /// Retrieves the value of a constant.
  2077. bool script_get_constant(const char* name, int64* value)
  2078. {
  2079. int32 n = search_str(name);
  2080. if( n == -1 || str_data[n].type != C_INT )
  2081. {// not found or not a constant
  2082. return false;
  2083. }
  2084. value[0] = str_data[n].val;
  2085. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2086. if( str_data[n].deprecated ){
  2087. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2088. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2089. if (str_data[n].name)
  2090. ShowWarning( "Please use '%s' instead!\n", str_data[n].name );
  2091. }
  2092. #endif
  2093. return true;
  2094. }
  2095. /// Creates new constant or parameter with given value.
  2096. void script_set_constant_(const char* name, int64 value, const char* constant_name, bool isparameter, bool deprecated)
  2097. {
  2098. int32 n = add_str(name);
  2099. if( str_data[n].type == C_NOP )
  2100. {// new
  2101. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2102. str_data[n].val = value;
  2103. str_data[n].deprecated = deprecated;
  2104. str_data[n].name = constant_name;
  2105. }
  2106. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2107. {// existing parameter or constant
  2108. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%" PRId64 ", new value=%" PRId64 ").\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  2109. }
  2110. else
  2111. {// existing name
  2112. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2113. }
  2114. }
  2115. const std::string ConstantDatabase::getDefaultLocation(){
  2116. return std::string(db_path) + "/const.yml";
  2117. }
  2118. uint64 ConstantDatabase::parseBodyNode( const ryml::NodeRef& node ) {
  2119. std::string constant_name;
  2120. if (!this->asString( node, "Name", constant_name ))
  2121. return 0;
  2122. char name[1024];
  2123. if (sscanf(constant_name.c_str(), "%1023[A-Za-z0-9/_]", name) != 1) {
  2124. this->invalidWarning( node["Name"], "Invalid constant definition \"%s\", skipping.\n", constant_name.c_str() );
  2125. return 0;
  2126. }
  2127. int64 val;
  2128. if (!this->asInt64( node, "Value", val ))
  2129. return 0;
  2130. bool type = false;
  2131. if (this->nodeExists(node, "Parameter") && !this->asBool( node, "Parameter", type ))
  2132. return 0;
  2133. script_set_constant(name, val, type, false);
  2134. return 1;
  2135. }
  2136. ConstantDatabase constant_db;
  2137. /**
  2138. * Sets source-end constants for NPC scripts to access.
  2139. **/
  2140. void script_hardcoded_constants(void) {
  2141. #include "script_constants.hpp"
  2142. }
  2143. /*==========================================
  2144. * Display emplacement line of script
  2145. *------------------------------------------*/
  2146. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int32 line)
  2147. {
  2148. int32 i;
  2149. if( p == nullptr || !p[0] ) return nullptr;
  2150. if( line < 0 )
  2151. StringBuf_Printf(buf, "*% 5d : ", -line);
  2152. else
  2153. StringBuf_Printf(buf, " % 5d : ", line);
  2154. for(i=0;p[i] && p[i] != '\n';i++){
  2155. if(p + i != mark)
  2156. StringBuf_Printf(buf, "%c", p[i]);
  2157. else
  2158. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2159. }
  2160. StringBuf_AppendStr(buf, "\n");
  2161. return p+i+(p[i] == '\n' ? 1 : 0);
  2162. }
  2163. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int32 start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2164. // Find the line where the error occurred
  2165. int32 j;
  2166. int32 line = start_line;
  2167. const char *p;
  2168. const char *linestart[5] = { nullptr, nullptr, nullptr, nullptr, nullptr };
  2169. for(p=src;p && *p;line++){
  2170. const char *lineend=strchr(p,'\n');
  2171. if(lineend==nullptr || error_pos_cur<lineend){
  2172. break;
  2173. }
  2174. for( j = 0; j < 4; j++ ) {
  2175. linestart[j] = linestart[j+1];
  2176. }
  2177. linestart[4] = p;
  2178. p=lineend+1;
  2179. }
  2180. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2181. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2182. for(j = 0; j < 5; j++ ) {
  2183. script_print_line(buf, linestart[j], nullptr, line + j - 5);
  2184. }
  2185. p = script_print_line(buf, p, error_pos_cur, -line);
  2186. for(j = 0; j < 5; j++) {
  2187. p = script_print_line(buf, p, nullptr, line + j + 1 );
  2188. }
  2189. }
  2190. void script_error(const char* src, const char* file, int32 start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2191. StringBuf buf;
  2192. StringBuf_Init(&buf);
  2193. StringBuf_AppendStr(&buf, "\a\n");
  2194. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2195. ShowError("%s", StringBuf_Value(&buf));
  2196. StringBuf_Destroy(&buf);
  2197. }
  2198. void script_warning(const char* src, const char* file, int32 start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2199. StringBuf buf;
  2200. StringBuf_Init(&buf);
  2201. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2202. ShowWarning("%s", StringBuf_Value(&buf));
  2203. StringBuf_Destroy(&buf);
  2204. }
  2205. /*==========================================
  2206. * Analysis of the script
  2207. *------------------------------------------*/
  2208. struct script_code* parse_script_( const char *src, const char *file, int32 line, int32 options, const char* src_file, int32 src_line, const char* src_func ){
  2209. const char *p,*tmpp;
  2210. int32 i;
  2211. struct script_code* code = nullptr;
  2212. char end;
  2213. bool unresolved_names = false;
  2214. parser_current_src = src;
  2215. parser_current_file = file;
  2216. parser_current_line = line;
  2217. if( src == nullptr )
  2218. return nullptr;// empty script
  2219. memset(&syntax,0,sizeof(syntax));
  2220. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2221. script_pos=0;
  2222. script_size=SCRIPT_BLOCK_SIZE;
  2223. parse_nextline(true, nullptr);
  2224. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2225. if( options&SCRIPT_USE_LABEL_DB )
  2226. db_clear(scriptlabel_db);
  2227. parse_options = options;
  2228. if( setjmp( error_jump ) != 0 ) {
  2229. //Restore program state when script has problems. [from jA]
  2230. int32 j;
  2231. const int32 size = ARRAYLENGTH(syntax.curly);
  2232. if( error_report )
  2233. script_error(src,file,line,error_msg,error_pos);
  2234. aFree( error_msg );
  2235. aFree( script_buf );
  2236. script_pos = 0;
  2237. script_size = 0;
  2238. script_buf = nullptr;
  2239. for(j=LABEL_START;j<str_num;j++)
  2240. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2241. for(j=0; j<size; j++)
  2242. linkdb_final(&syntax.curly[j].case_label);
  2243. return nullptr;
  2244. }
  2245. parse_syntax_for_flag=0;
  2246. p=src;
  2247. p=skip_space(p);
  2248. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2249. {// does not require brackets around the script
  2250. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2251. {// empty script and can return nullptr
  2252. aFree( script_buf );
  2253. script_pos = 0;
  2254. script_size = 0;
  2255. script_buf = nullptr;
  2256. return nullptr;
  2257. }
  2258. end = '\0';
  2259. }
  2260. else
  2261. {// requires brackets around the script
  2262. if( *p != '{' )
  2263. disp_error_message("not found '{'",p);
  2264. p = skip_space(p+1);
  2265. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2266. {// empty script and can return nullptr
  2267. aFree( script_buf );
  2268. script_pos = 0;
  2269. script_size = 0;
  2270. script_buf = nullptr;
  2271. return nullptr;
  2272. }
  2273. end = '}';
  2274. }
  2275. // clear references of labels, variables and internal functions
  2276. for(i=LABEL_START;i<str_num;i++){
  2277. if(
  2278. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2279. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2280. ){
  2281. str_data[i].type=C_NOP;
  2282. str_data[i].backpatch=-1;
  2283. str_data[i].label=-1;
  2284. }
  2285. }
  2286. while( syntax.curly_count != 0 || *p != end )
  2287. {
  2288. if( *p == '\0' )
  2289. disp_error_message("unexpected end of script",p);
  2290. // Special handling only label
  2291. tmpp=skip_space(skip_word(p));
  2292. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2293. i=add_word(p);
  2294. set_label(i,script_pos,p);
  2295. if( parse_options&SCRIPT_USE_LABEL_DB )
  2296. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2297. p=tmpp+1;
  2298. p=skip_space(p);
  2299. continue;
  2300. }
  2301. // All other lumped
  2302. p=parse_line(p);
  2303. p=skip_space(p);
  2304. parse_nextline(false, p);
  2305. }
  2306. add_scriptc(C_NOP);
  2307. // trim code to size
  2308. script_size = script_pos;
  2309. RECREATE(script_buf,unsigned char,script_pos);
  2310. // default unknown references to variables
  2311. for(i=LABEL_START;i<str_num;i++){
  2312. if(str_data[i].type==C_NOP){
  2313. int32 j;
  2314. str_data[i].type=C_NAME;
  2315. str_data[i].label=i;
  2316. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2317. int32 next=GETVALUE(script_buf,j);
  2318. SETVALUE(script_buf,j,i);
  2319. j=next;
  2320. }
  2321. }
  2322. else if( str_data[i].type == C_USERFUNC )
  2323. {// 'function name;' without follow-up code
  2324. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2325. unresolved_names = true;
  2326. }
  2327. }
  2328. if( unresolved_names )
  2329. {
  2330. disp_error_message("parse_script: unresolved function references", p);
  2331. }
  2332. #ifdef DEBUG_DISP
  2333. for(i=0;i<script_pos;i++){
  2334. if((i&15)==0) ShowMessage("%04x : ",i);
  2335. ShowMessage("%02x ",script_buf[i]);
  2336. if((i&15)==15) ShowMessage("\n");
  2337. }
  2338. ShowMessage("\n");
  2339. #endif
  2340. #ifdef DEBUG_DISASM
  2341. {
  2342. int32 i = 0,j;
  2343. while(i < script_pos) {
  2344. c_op op = get_com(script_buf,&i);
  2345. ShowMessage("%06x %s", i, script_op2name(op));
  2346. j = i;
  2347. switch(op) {
  2348. case C_INT:
  2349. ShowMessage(" %d", get_num(script_buf,&i));
  2350. break;
  2351. case C_POS:
  2352. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2353. i += 3;
  2354. break;
  2355. case C_NAME:
  2356. j = (*(int*)(script_buf+i)&0xffffff);
  2357. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2358. i += 3;
  2359. break;
  2360. case C_STR:
  2361. j = strlen(script_buf + i);
  2362. ShowMessage(" %s", script_buf + i);
  2363. i += j+1;
  2364. break;
  2365. }
  2366. ShowMessage(CL_CLL"\n");
  2367. }
  2368. }
  2369. #endif
  2370. CREATE2( code, struct script_code, 1, src_file, src_line, src_func );
  2371. code->script_buf = script_buf;
  2372. code->script_size = script_size;
  2373. code->local.vars = nullptr;
  2374. code->local.arrays = nullptr;
  2375. return code;
  2376. }
  2377. /// Returns the player attached to this script, identified by the rid.
  2378. /// If there is no player attached, the script is terminated.
  2379. static bool script_rid2sd_( struct script_state *st, map_session_data** sd, const char *func ){
  2380. *sd = map_id2sd( st->rid );
  2381. if( *sd ){
  2382. return true;
  2383. }else{
  2384. ShowError("%s: fatal error ! player not attached!\n",func);
  2385. script_reportfunc(st);
  2386. script_reportsrc(st);
  2387. st->state = END;
  2388. return false;
  2389. }
  2390. }
  2391. /**
  2392. * Dereferences a variable/constant, replacing it with a copy of the value.
  2393. * @param st Script state
  2394. * @param data Variable/constant
  2395. * @param sd If nullptr, will try to use sd from st->rid (for player's variables)
  2396. */
  2397. struct script_data *get_val_(struct script_state* st, struct script_data* data, map_session_data *sd)
  2398. {
  2399. const char* name;
  2400. char prefix;
  2401. char postfix;
  2402. if( !data_isreference(data) )
  2403. return data;// not a variable/constant
  2404. name = reference_getname(data);
  2405. prefix = name[0];
  2406. postfix = name[strlen(name) - 1];
  2407. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2408. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2409. if( sd == nullptr && !script_rid2sd(sd) ) {// needs player attached
  2410. if( postfix == '$' ) {// string variable
  2411. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2412. data->type = C_CONSTSTR;
  2413. data->u.str = const_cast<char *>("");
  2414. } else {// integer variable
  2415. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2416. data->type = C_INT;
  2417. data->u.num = 0;
  2418. }
  2419. return data;
  2420. }
  2421. }
  2422. if( postfix == '$' ) {// string variable
  2423. switch( prefix ) {
  2424. case '@':
  2425. data->u.str = pc_readregstr(sd, data->u.num);
  2426. break;
  2427. case '$':
  2428. data->u.str = mapreg_readregstr(data->u.num);
  2429. break;
  2430. case '#':
  2431. if( name[1] == '#' )
  2432. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2433. else
  2434. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2435. break;
  2436. case '.':
  2437. {
  2438. struct DBMap* n = data->ref ?
  2439. data->ref->vars : name[1] == '@' ?
  2440. st->stack->scope.vars : // instance/scope variable
  2441. st->script->local.vars; // npc variable
  2442. if( n )
  2443. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2444. else
  2445. data->u.str = nullptr;
  2446. }
  2447. break;
  2448. case '\'':
  2449. {
  2450. struct DBMap* n = nullptr;
  2451. if (data->ref)
  2452. n = data->ref->vars;
  2453. else {
  2454. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2455. if (idata)
  2456. n = idata->regs.vars;
  2457. }
  2458. if (n)
  2459. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2460. else {
  2461. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2462. data->u.str = nullptr;
  2463. }
  2464. break;
  2465. }
  2466. default:
  2467. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2468. break;
  2469. }
  2470. if( data->u.str == nullptr || data->u.str[0] == '\0' ) {// empty string
  2471. data->type = C_CONSTSTR;
  2472. data->u.str = const_cast<char *>("");
  2473. } else {// duplicate string
  2474. data->type = C_STR;
  2475. data->u.str = aStrdup(data->u.str);
  2476. }
  2477. } else {// integer variable
  2478. data->type = C_INT;
  2479. if( reference_toconstant(data) ) {
  2480. data->u.num = reference_getconstant(data);
  2481. } else if( reference_toparam(data) ) {
  2482. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2483. } else
  2484. switch( prefix ) {
  2485. case '@':
  2486. data->u.num = pc_readreg(sd, data->u.num);
  2487. break;
  2488. case '$':
  2489. data->u.num = mapreg_readreg(data->u.num);
  2490. break;
  2491. case '#':
  2492. if( name[1] == '#' )
  2493. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2494. else
  2495. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2496. break;
  2497. case '.':
  2498. {
  2499. struct DBMap* n = data->ref ?
  2500. data->ref->vars : name[1] == '@' ?
  2501. st->stack->scope.vars : // instance/scope variable
  2502. st->script->local.vars; // npc variable
  2503. if( n )
  2504. data->u.num = i64db_i64get(n,reference_getuid(data));
  2505. else
  2506. data->u.num = 0;
  2507. }
  2508. break;
  2509. case '\'':
  2510. {
  2511. struct DBMap* n = nullptr;
  2512. if (data->ref)
  2513. n = data->ref->vars;
  2514. else {
  2515. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2516. if (idata)
  2517. n = idata->regs.vars;
  2518. }
  2519. if (n)
  2520. data->u.num = i64db_i64get(n,reference_getuid(data));
  2521. else {
  2522. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2523. data->u.num = 0;
  2524. }
  2525. break;
  2526. }
  2527. default:
  2528. data->u.num = pc_readglobalreg(sd, data->u.num);
  2529. break;
  2530. }
  2531. }
  2532. data->ref = nullptr;
  2533. return data;
  2534. }
  2535. struct script_data *get_val(struct script_state* st, struct script_data* data)
  2536. {
  2537. return get_val_(st,data,nullptr);
  2538. }
  2539. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2540. const char* get_val2_str( struct script_state* st, int64 uid, struct reg_db* ref ){
  2541. push_val2( st->stack, C_NAME, uid, ref );
  2542. struct script_data* data = script_getdatatop( st, -1 );
  2543. get_val( st, data );
  2544. const char* value = "";
  2545. if( data->type == C_INT ){
  2546. ShowError( "get_val2_num: Invalid call. Variable %s is a numeric type.\n", reference_getname( data ) );
  2547. }else{
  2548. value = data->u.str;
  2549. }
  2550. // Do NOT remove the value from stack here, the pointer is returned here and will be used by the caller [Lemongrass]
  2551. // script_removetop( st, -1, 0 );
  2552. return value;
  2553. }
  2554. int64 get_val2_num( struct script_state* st, int64 uid, struct reg_db* ref ){
  2555. push_val2( st->stack, C_NAME, uid, ref );
  2556. struct script_data* data = script_getdatatop( st, -1 );
  2557. get_val( st, data );
  2558. int64 value = 0;
  2559. if( data->type == C_INT ){
  2560. value = data->u.num;
  2561. }else{
  2562. ShowError( "get_val2_num: Invalid call. Variable %s is not a numeric type.\n", reference_getname( data ) );
  2563. }
  2564. script_removetop( st, -1, 0 );
  2565. return value;
  2566. }
  2567. /**
  2568. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2569. * Will be gone as soon as undefined var feature is implemented
  2570. **/
  2571. void script_array_ensure_zero(struct script_state *st, map_session_data *sd, int64 uid, struct reg_db *ref)
  2572. {
  2573. const char *name = get_str(script_getvarid(uid));
  2574. // is here st can be null pointer and st->rid is wrong?
  2575. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : nullptr, name, ref);
  2576. bool insert = false;
  2577. if (sd && !st) {
  2578. // when sd comes, st isn't available
  2579. insert = true;
  2580. } else {
  2581. if( is_string_variable(name) ) {
  2582. const char* str = get_val2_str( st, uid, ref );
  2583. if( str && *str )
  2584. insert = true;
  2585. // Remove stack entry from get_val2_str
  2586. script_removetop( st, -1, 0 );
  2587. } else {
  2588. int64 num = get_val2_num( st, uid, ref );
  2589. if( num )
  2590. insert = true;
  2591. }
  2592. }
  2593. if (src && src->arrays) {
  2594. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2595. if (sa) {
  2596. uint32 i;
  2597. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2598. if( i != sa->size ) {
  2599. if( !insert )
  2600. script_array_remove_member(src,sa,i);
  2601. return;
  2602. }
  2603. script_array_add_member(sa,0);
  2604. } else if (insert) {
  2605. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2606. }
  2607. }
  2608. }
  2609. /**
  2610. * Returns array size by ID
  2611. **/
  2612. uint32 script_array_size(struct script_state *st, map_session_data *sd, const char *name, struct reg_db *ref)
  2613. {
  2614. struct script_array *sa = nullptr;
  2615. struct reg_db *src = script_array_src(st, sd, name, ref);
  2616. if (src && src->arrays)
  2617. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2618. return sa ? sa->size : 0;
  2619. }
  2620. /**
  2621. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2622. **/
  2623. uint32 script_array_highest_key(struct script_state *st, map_session_data *sd, const char *name, struct reg_db *ref)
  2624. {
  2625. struct script_array *sa = nullptr;
  2626. struct reg_db *src = script_array_src(st, sd, name, ref);
  2627. if (src && src->arrays) {
  2628. int32 key = add_word(name);
  2629. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2630. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2631. uint32 i, highest_key = 0;
  2632. for(i = 0; i < sa->size; i++) {
  2633. if( sa->members[i] > highest_key )
  2634. highest_key = sa->members[i];
  2635. }
  2636. return sa->size ? highest_key + 1 : 0;
  2637. }
  2638. }
  2639. return SCRIPT_CMD_SUCCESS;
  2640. }
  2641. int32 script_free_array_db(DBKey key, DBData *data, va_list ap)
  2642. {
  2643. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2644. aFree(sa->members);
  2645. ers_free(array_ers, sa);
  2646. return SCRIPT_CMD_SUCCESS;
  2647. }
  2648. /**
  2649. * Clears script_array and removes it from script->array_db
  2650. **/
  2651. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2652. {
  2653. aFree(sa->members);
  2654. idb_remove(src->arrays, sa->id);
  2655. ers_free(array_ers, sa);
  2656. }
  2657. /**
  2658. * Removes a member from a script_array list
  2659. *
  2660. * @param idx the index of the member in script_array struct list, not of the actual array member
  2661. **/
  2662. void script_array_remove_member(struct reg_db *src, struct script_array *sa, uint32 idx)
  2663. {
  2664. uint32 i, cursor;
  2665. // it's the only member left, no need to do anything other than delete the array data
  2666. if( sa->size == 1 ) {
  2667. script_array_delete(src,sa);
  2668. return;
  2669. }
  2670. sa->members[idx] = UINT_MAX;
  2671. for(i = 0, cursor = 0; i < sa->size; i++) {
  2672. if( sa->members[i] == UINT_MAX )
  2673. continue;
  2674. if( i != cursor )
  2675. sa->members[cursor] = sa->members[i];
  2676. cursor++;
  2677. }
  2678. sa->size = cursor;
  2679. }
  2680. /**
  2681. * Appends a new array index to the list in script_array
  2682. *
  2683. * @param idx the index of the array member being inserted
  2684. **/
  2685. void script_array_add_member(struct script_array *sa, uint32 idx)
  2686. {
  2687. RECREATE(sa->members, uint32, ++sa->size);
  2688. sa->members[sa->size - 1] = idx;
  2689. }
  2690. /**
  2691. * Obtains the source of the array database for this type and scenario
  2692. * Initializes such database when not yet initialized.
  2693. **/
  2694. struct reg_db *script_array_src(struct script_state *st, map_session_data *sd, const char *name, struct reg_db *ref)
  2695. {
  2696. struct reg_db *src = nullptr;
  2697. switch( name[0] ) {
  2698. // from player
  2699. default: // char reg
  2700. case '@': // temp char reg
  2701. case '#': // account reg
  2702. src = &sd->regs;
  2703. break;
  2704. case '$': // map reg
  2705. src = &regs;
  2706. break;
  2707. case '.': // npc/script
  2708. if (ref)
  2709. src = ref;
  2710. else
  2711. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2712. break;
  2713. case '\'': // instance
  2714. {
  2715. if (ref)
  2716. src = ref;
  2717. else {
  2718. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2719. if (idata)
  2720. src = &idata->regs;
  2721. }
  2722. break;
  2723. }
  2724. }
  2725. if( src ) {
  2726. if( !src->arrays )
  2727. src->arrays = idb_alloc(DB_OPT_BASE);
  2728. return src;
  2729. }
  2730. return nullptr;
  2731. }
  2732. /**
  2733. * Processes a array member modification, and update data accordingly
  2734. *
  2735. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2736. * @param num[in] Variable ID
  2737. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2738. **/
  2739. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2740. {
  2741. struct script_array *sa = nullptr;
  2742. int32 id = script_getvarid(num);
  2743. uint32 index = script_getvaridx(num);
  2744. if (!src->arrays) {
  2745. src->arrays = idb_alloc(DB_OPT_BASE);
  2746. } else {
  2747. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2748. }
  2749. if( sa ) {
  2750. uint32 i;
  2751. // search
  2752. for(i = 0; i < sa->size; i++) {
  2753. if( sa->members[i] == index )
  2754. break;
  2755. }
  2756. // if existent
  2757. if( i != sa->size ) {
  2758. // if empty, we gotta remove it
  2759. if( empty ) {
  2760. script_array_remove_member(src, sa, i);
  2761. }
  2762. } else if( !empty ) { /* new entry */
  2763. script_array_add_member(sa,index);
  2764. // we do nothing if its empty, no point in modifying array data for a new empty member
  2765. }
  2766. } else if ( !empty ) { // we only move to create if not empty
  2767. sa = ers_alloc(array_ers, struct script_array);
  2768. sa->id = id;
  2769. sa->members = nullptr;
  2770. sa->size = 0;
  2771. script_array_add_member(sa,index);
  2772. idb_put(src->arrays, id, sa);
  2773. }
  2774. }
  2775. /**
  2776. * Stores the value of a script variable
  2777. *
  2778. * @param st: current script state.
  2779. * @param sd: current character data.
  2780. * @param num: variable identifier.
  2781. * @param name: variable name.
  2782. * @param value: new value.
  2783. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2784. * @return: 0 failure, 1 success.
  2785. *
  2786. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2787. *------------------------------------------*/
  2788. bool set_reg_str( struct script_state* st, map_session_data* sd, int64 num, const char* name, const char* value, struct reg_db *ref ){
  2789. char prefix = name[0];
  2790. size_t vlen = 0;
  2791. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2792. ShowError( "set_reg: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%" PRIuPTR "\n", sd ? sd->status.account_id : -1, sd ? sd->status.char_id : -1, name, vlen );
  2793. return false;
  2794. }
  2795. if( !is_string_variable( name ) ){
  2796. // integer variable
  2797. return false;
  2798. }
  2799. switch( prefix ){
  2800. case '@':
  2801. pc_setregstr( sd, num, value );
  2802. return true;
  2803. case '$':
  2804. return mapreg_setregstr( num, value );
  2805. case '#':
  2806. return ( name[1] == '#' ) ? pc_setaccountreg2str( sd, num, value ) : pc_setaccountregstr( sd, num, value );
  2807. case '.': {
  2808. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2809. if( n ){
  2810. if( value[0] ){
  2811. i64db_put( n->vars, num, aStrdup( value ) );
  2812. if( script_getvaridx( num ) ){
  2813. script_array_update( n, num, false );
  2814. }
  2815. }else{
  2816. i64db_remove( n->vars, num );
  2817. if( script_getvaridx( num ) ){
  2818. script_array_update( n, num, true );
  2819. }
  2820. }
  2821. }
  2822. }
  2823. return true;
  2824. case '\'': {
  2825. struct reg_db *src = nullptr;
  2826. if( ref ){
  2827. src = ref;
  2828. }else{
  2829. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2830. if (idata)
  2831. src = &idata->regs;
  2832. }
  2833. if( src ){
  2834. bool empty;
  2835. if( value[0] ){
  2836. i64db_put( src->vars, num, aStrdup( value ) );
  2837. empty = false;
  2838. }else{
  2839. i64db_remove( src->vars, num );
  2840. empty = true;
  2841. }
  2842. if( script_getvaridx( num ) != 0 ){
  2843. script_array_update( src, num, empty );
  2844. }
  2845. }else{
  2846. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2847. script_reportsrc( st );
  2848. }
  2849. return true;
  2850. }
  2851. default:
  2852. return pc_setglobalreg_str( sd, num, value );
  2853. }
  2854. }
  2855. bool set_reg_num( struct script_state* st, map_session_data* sd, int64 num, const char* name, int64 value, struct reg_db *ref ){
  2856. char prefix = name[0];
  2857. size_t vlen = 0;
  2858. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2859. ShowError( "set_reg: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%" PRIuPTR "\n", sd ? sd->status.account_id : -1, sd ? sd->status.char_id : -1, name, vlen );
  2860. return false;
  2861. }
  2862. if( is_string_variable( name ) ){
  2863. // string variable
  2864. return false;
  2865. }
  2866. if( str_data[script_getvarid(num)].type == C_PARAM ){
  2867. if( pc_setparam( sd, str_data[script_getvarid(num)].val, value ) == 0 ){
  2868. if( st != nullptr ) {
  2869. ShowError( "script_set_reg: failed to set param '%s' to %" PRId64 ".\n", name, value );
  2870. script_reportsrc( st );
  2871. st->state = END;
  2872. }
  2873. return false;
  2874. }
  2875. return true;
  2876. }
  2877. switch( prefix ){
  2878. case '@':
  2879. pc_setreg( sd, num, value );
  2880. return true;
  2881. case '$':
  2882. return mapreg_setreg( num, value );
  2883. case '#':
  2884. return ( name[1] == '#' ) ? pc_setaccountreg2(sd, num, value) : pc_setaccountreg( sd, num, value );
  2885. case '.': {
  2886. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2887. if( n ){
  2888. if( value != 0 ){
  2889. i64db_i64put( n->vars, num, value );
  2890. if( script_getvaridx( num ) ){
  2891. script_array_update( n, num, false );
  2892. }
  2893. }else{
  2894. i64db_remove( n->vars, num );
  2895. if( script_getvaridx( num ) ){
  2896. script_array_update( n, num, true );
  2897. }
  2898. }
  2899. }
  2900. }
  2901. return true;
  2902. case '\'': {
  2903. struct reg_db *src = nullptr;
  2904. if( ref ){
  2905. src = ref;
  2906. }else{
  2907. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2908. if (idata)
  2909. src = &idata->regs;
  2910. }
  2911. if( src ){
  2912. bool empty;
  2913. if( value != 0 ){
  2914. i64db_i64put( src->vars, num, value );
  2915. empty = false;
  2916. }else{
  2917. i64db_remove( src->vars, num );
  2918. empty = true;
  2919. }
  2920. if( script_getvaridx( num ) != 0 ){
  2921. script_array_update( src, num, empty );
  2922. }
  2923. }else{
  2924. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2925. script_reportsrc( st );
  2926. }
  2927. return true;
  2928. }
  2929. default:
  2930. return pc_setglobalreg( sd, num, value );
  2931. }
  2932. }
  2933. bool set_var_str( map_session_data* sd, const char* name, const char* val ){
  2934. return set_reg_str( nullptr, sd, reference_uid( add_str( name ), 0 ), name, val, nullptr );
  2935. }
  2936. bool clear_reg( struct script_state* st, map_session_data* sd, int64 num, const char* name, struct reg_db *ref ){
  2937. if( is_string_variable( name ) ){
  2938. return set_reg_str( st, sd, num, name, "", ref );
  2939. }else{
  2940. return set_reg_num( st, sd, num, name, 0, ref );
  2941. }
  2942. }
  2943. void setd_sub_num( struct script_state* st, map_session_data* sd, const char* varname, int32 elem, int64 value, struct reg_db* ref ){
  2944. set_reg_num( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2945. }
  2946. void setd_sub_str( struct script_state* st, map_session_data* sd, const char* varname, int32 elem, const char* value, struct reg_db* ref ){
  2947. set_reg_str( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2948. }
  2949. /**
  2950. * Converts the data to a string
  2951. * @param st
  2952. * @param data
  2953. * @param sd
  2954. */
  2955. const char* conv_str_(struct script_state* st, struct script_data* data, map_session_data *sd)
  2956. {
  2957. char* p;
  2958. get_val_(st, data, sd);
  2959. if( data_isstring(data) )
  2960. {// nothing to convert
  2961. }
  2962. else if( data_isint(data) )
  2963. {// int32 -> string
  2964. CREATE(p, char, ITEM_NAME_LENGTH);
  2965. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2966. p[ITEM_NAME_LENGTH-1] = '\0';
  2967. data->type = C_STR;
  2968. data->u.str = p;
  2969. }
  2970. else if( data_isreference(data) )
  2971. {// reference -> string
  2972. //##TODO when does this happen (check get_val) [FlavioJS] -- at getd!!
  2973. data->type = C_CONSTSTR;
  2974. data->u.str = reference_getname(data);
  2975. }
  2976. else
  2977. {// unsupported data type
  2978. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2979. script_reportdata(data);
  2980. script_reportsrc(st);
  2981. data->type = C_CONSTSTR;
  2982. data->u.str = const_cast<char *>("");
  2983. }
  2984. return data->u.str;
  2985. }
  2986. const char* conv_str(struct script_state* st, struct script_data* data)
  2987. {
  2988. return conv_str_(st, data, nullptr);
  2989. }
  2990. /**
  2991. * Converts the data to an int32
  2992. * @param st
  2993. * @param data
  2994. * @param sd
  2995. */
  2996. int64 conv_num_(struct script_state* st, struct script_data* data, map_session_data *sd)
  2997. {
  2998. get_val_(st, data, sd);
  2999. if( data_isint(data) )
  3000. {// nothing to convert
  3001. }
  3002. else if( data_isstring(data) )
  3003. {// string -> int32
  3004. // the result does not overflow or underflow, it is capped instead
  3005. // ex: 999999999999 is capped to INT_MAX (2147483647)
  3006. char* p = data->u.str;
  3007. int64 num;
  3008. bool error;
  3009. errno = 0;
  3010. num = strtoll(data->u.str, nullptr, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  3011. error = (errno == ERANGE);
  3012. if( num < SCRIPT_INT_MIN || ( error && num == INT64_MIN ) ){
  3013. num = SCRIPT_INT_MIN;
  3014. ShowError( "script:conv_num: underflow detected, capping value to %" PRId64 "", SCRIPT_INT_MIN );
  3015. script_reportdata(data);
  3016. script_reportsrc(st);
  3017. }else if( num > SCRIPT_INT_MAX || ( error && num == INT64_MAX ) ){
  3018. num = SCRIPT_INT_MAX;
  3019. ShowError( "script:conv_num: overflow detected, capping value to %" PRId64 "", SCRIPT_INT_MAX );
  3020. script_reportdata(data);
  3021. script_reportsrc(st);
  3022. }
  3023. if( data->type == C_STR )
  3024. aFree(p);
  3025. data->type = C_INT;
  3026. data->u.num = num;
  3027. }
  3028. #if 0
  3029. // FIXME this function is being used to retrieve the position of labels and
  3030. // probably other stuff [FlavioJS]
  3031. else
  3032. {// unsupported data type
  3033. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3034. script_reportdata(data);
  3035. script_reportsrc(st);
  3036. data->type = C_INT;
  3037. data->u.num = 0;
  3038. }
  3039. #endif
  3040. return data->u.num;
  3041. }
  3042. int64 conv_num64(struct script_state* st, struct script_data* data)
  3043. {
  3044. return conv_num_(st, data, nullptr);
  3045. }
  3046. int32 conv_num(struct script_state* st, struct script_data* data)
  3047. {
  3048. return static_cast<int>(conv_num_(st, data, nullptr));
  3049. }
  3050. //
  3051. // Stack operations
  3052. //
  3053. /// Increases the size of the stack
  3054. void stack_expand(struct script_stack* stack)
  3055. {
  3056. stack->sp_max += 64;
  3057. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3058. stack->sp_max * sizeof(stack->stack_data[0]) );
  3059. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3060. 64 * sizeof(stack->stack_data[0]) );
  3061. }
  3062. /// Pushes a value into the stack
  3063. #define push_val(stack,type,val) push_val2(stack, type, val, nullptr)
  3064. /// Pushes a value into the stack (with reference)
  3065. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3066. {
  3067. if( stack->sp >= stack->sp_max )
  3068. stack_expand(stack);
  3069. stack->stack_data[stack->sp].type = type;
  3070. stack->stack_data[stack->sp].u.num = val;
  3071. stack->stack_data[stack->sp].ref = ref;
  3072. stack->sp++;
  3073. return &stack->stack_data[stack->sp-1];
  3074. }
  3075. /// Pushes a string into the stack
  3076. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3077. {
  3078. if( stack->sp >= stack->sp_max )
  3079. stack_expand(stack);
  3080. stack->stack_data[stack->sp].type = type;
  3081. stack->stack_data[stack->sp].u.str = str;
  3082. stack->stack_data[stack->sp].ref = nullptr;
  3083. stack->sp++;
  3084. return &stack->stack_data[stack->sp-1];
  3085. }
  3086. /// Pushes a retinfo into the stack
  3087. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3088. {
  3089. if( stack->sp >= stack->sp_max )
  3090. stack_expand(stack);
  3091. stack->stack_data[stack->sp].type = C_RETINFO;
  3092. stack->stack_data[stack->sp].u.ri = ri;
  3093. stack->stack_data[stack->sp].ref = ref;
  3094. stack->sp++;
  3095. return &stack->stack_data[stack->sp-1];
  3096. }
  3097. /// Pushes a copy of the target position into the stack
  3098. struct script_data* push_copy(struct script_stack* stack, int32 pos)
  3099. {
  3100. switch( stack->stack_data[pos].type ) {
  3101. case C_CONSTSTR:
  3102. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3103. break;
  3104. case C_STR:
  3105. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3106. break;
  3107. case C_RETINFO:
  3108. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3109. exit(1);
  3110. break;
  3111. default:
  3112. return push_val2(
  3113. stack,stack->stack_data[pos].type,
  3114. stack->stack_data[pos].u.num,
  3115. stack->stack_data[pos].ref
  3116. );
  3117. break;
  3118. }
  3119. }
  3120. /// Removes the values in indexes [start,end] from the stack.
  3121. /// Adjusts all stack pointers.
  3122. void pop_stack(struct script_state* st, int32 start, int32 end)
  3123. {
  3124. struct script_stack* stack = st->stack;
  3125. struct script_data* data;
  3126. int32 i;
  3127. if( start < 0 )
  3128. start = 0;
  3129. if( end > stack->sp )
  3130. end = stack->sp;
  3131. if( start >= end )
  3132. return;// nothing to pop
  3133. // free stack elements
  3134. for( i = start; i < end; i++ )
  3135. {
  3136. data = &stack->stack_data[i];
  3137. if( data->type == C_STR )
  3138. aFree(data->u.str);
  3139. if( data->type == C_RETINFO ) {
  3140. struct script_retinfo* ri = data->u.ri;
  3141. if (ri->scope.vars) {
  3142. script_free_vars(ri->scope.vars);
  3143. ri->scope.vars = nullptr;
  3144. }
  3145. if (ri->scope.arrays) {
  3146. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3147. ri->scope.arrays = nullptr;
  3148. }
  3149. if( data->ref )
  3150. aFree(data->ref);
  3151. aFree(ri);
  3152. }
  3153. data->type = C_NOP;
  3154. }
  3155. // move the rest of the elements
  3156. if( stack->sp > end )
  3157. {
  3158. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3159. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3160. stack->stack_data[i].type = C_NOP;
  3161. }
  3162. // adjust stack pointers
  3163. if( st->start > end ){
  3164. st->start -= end - start;
  3165. }else if( st->start > start ){
  3166. st->start = start;
  3167. }
  3168. if( st->end > end ){
  3169. st->end -= end - start;
  3170. }else if( st->end > start ){
  3171. st->end = start;
  3172. }
  3173. if( stack->defsp > end ){
  3174. stack->defsp -= end - start;
  3175. }else if( stack->defsp > start ){
  3176. stack->defsp = start;
  3177. }
  3178. stack->sp -= end - start;
  3179. }
  3180. ///
  3181. ///
  3182. ///
  3183. /*==========================================
  3184. * Release script dependent variable, dependent variable of function
  3185. *------------------------------------------*/
  3186. void script_free_vars(struct DBMap* storage)
  3187. {
  3188. if( storage ) {
  3189. // destroy the storage construct containing the variables
  3190. db_destroy(storage);
  3191. }
  3192. }
  3193. void script_free_code(struct script_code* code)
  3194. {
  3195. nullpo_retv(code);
  3196. if (code->instances)
  3197. script_stop_scriptinstances(code);
  3198. script_free_vars(code->local.vars);
  3199. if (code->local.arrays)
  3200. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3201. aFree(code->script_buf);
  3202. aFree(code);
  3203. }
  3204. /// Creates a new script state.
  3205. ///
  3206. /// @param script Script code
  3207. /// @param pos Position in the code
  3208. /// @param rid Who is running the script (attached player)
  3209. /// @param oid Where the code is being run (npc 'object')
  3210. /// @return Script state
  3211. struct script_state* script_alloc_state(struct script_code* rootscript, int32 pos, int32 rid, int32 oid)
  3212. {
  3213. struct script_state* st;
  3214. st = ers_alloc(st_ers, struct script_state);
  3215. st->stack = ers_alloc(stack_ers, struct script_stack);
  3216. st->stack->sp = 0;
  3217. st->stack->sp_max = 64;
  3218. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3219. st->stack->defsp = st->stack->sp;
  3220. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3221. st->stack->scope.arrays = nullptr;
  3222. st->state = RUN;
  3223. st->script = rootscript;
  3224. st->pos = pos;
  3225. st->rid = rid;
  3226. st->oid = oid;
  3227. st->sleep.timer = INVALID_TIMER;
  3228. st->npc_item_flag = battle_config.item_enabled_npc;
  3229. if( st->script->instances != USHRT_MAX )
  3230. st->script->instances++;
  3231. else {
  3232. struct npc_data *nd = map_id2nd(oid);
  3233. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3234. }
  3235. if (!st->script->local.vars)
  3236. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3237. st->id = next_id++;
  3238. active_scripts++;
  3239. idb_put(st_db, st->id, st);
  3240. return st;
  3241. }
  3242. /// Frees a script state.
  3243. ///
  3244. /// @param st Script state
  3245. void script_free_state(struct script_state* st)
  3246. {
  3247. if (idb_exists(st_db, st->id)) {
  3248. map_session_data *sd = st->rid ? map_id2sd(st->rid) : nullptr;
  3249. if (st->bk_st) // backup was not restored
  3250. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3251. if (sd && sd->st == st) { // Current script is aborted.
  3252. if(sd->state.using_fake_npc) {
  3253. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  3254. sd->state.using_fake_npc = 0;
  3255. }
  3256. sd->st = nullptr;
  3257. sd->npc_id = 0;
  3258. }
  3259. if (st->sleep.timer != INVALID_TIMER)
  3260. delete_timer(st->sleep.timer, run_script_timer);
  3261. if (st->stack) {
  3262. script_free_vars(st->stack->scope.vars);
  3263. if (st->stack->scope.arrays)
  3264. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3265. pop_stack(st, 0, st->stack->sp);
  3266. aFree(st->stack->stack_data);
  3267. ers_free(stack_ers, st->stack);
  3268. st->stack = nullptr;
  3269. }
  3270. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3271. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3272. script_free_vars(st->script->local.vars);
  3273. st->script->local.vars = nullptr;
  3274. }
  3275. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3276. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3277. st->script->local.arrays = nullptr;
  3278. }
  3279. }
  3280. st->pos = -1;
  3281. idb_remove(st_db, st->id);
  3282. ers_free(st_ers, st);
  3283. if (--active_scripts == 0)
  3284. next_id = 0;
  3285. }
  3286. }
  3287. //
  3288. // Main execution unit
  3289. //
  3290. /*==========================================
  3291. * Read command
  3292. *------------------------------------------*/
  3293. c_op get_com(unsigned char *script,int32 *pos)
  3294. {
  3295. int32 i = 0, j = 0;
  3296. if(script[*pos]>=0x80){
  3297. return C_INT;
  3298. }
  3299. while(script[*pos]>=0x40){
  3300. i=script[(*pos)++]<<j;
  3301. j+=6;
  3302. }
  3303. return (c_op)(i+(script[(*pos)++]<<j));
  3304. }
  3305. /*==========================================
  3306. * Income figures
  3307. *------------------------------------------*/
  3308. int64 get_num(unsigned char *script,int32 *pos)
  3309. {
  3310. int32 i, end;
  3311. int64 value;
  3312. // Find the end of the data
  3313. for( i = 0; script[*pos+i] >= 0xc0; i++ ) ; // do nothing just find it
  3314. end = i; // store the end for later
  3315. value = 0; // initialize empty value
  3316. // If more than one byte of data exists
  3317. for( ; i > 0; i-- ){
  3318. value |= (int64)( script[*pos+i] & (int64)0x3f );
  3319. value <<= 6;
  3320. }
  3321. // Put our last piece of data into the return value
  3322. value |= (script[*pos+i] & (int64)0x3f);
  3323. *pos += end + 1; // one byte after the last byte of data
  3324. return value;
  3325. }
  3326. /// Ternary operators
  3327. /// test ? if_true : if_false
  3328. void op_3(struct script_state* st, int32 op)
  3329. {
  3330. struct script_data* data;
  3331. int32 flag = 0;
  3332. data = script_getdatatop(st, -3);
  3333. get_val(st, data);
  3334. if( data_isstring(data) )
  3335. flag = data->u.str[0];// "" -> false
  3336. else if( data_isint(data) )
  3337. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3338. else
  3339. {
  3340. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3341. script_reportdata(data);
  3342. script_reportsrc(st);
  3343. script_removetop(st, -3, 0);
  3344. script_pushnil(st);
  3345. return;
  3346. }
  3347. if( flag )
  3348. script_pushcopytop(st, -2);
  3349. else
  3350. script_pushcopytop(st, -1);
  3351. script_removetop(st, -4, -1);
  3352. }
  3353. /// Binary string operators
  3354. /// s1 EQ s2 -> i
  3355. /// s1 NE s2 -> i
  3356. /// s1 GT s2 -> i
  3357. /// s1 GE s2 -> i
  3358. /// s1 LT s2 -> i
  3359. /// s1 LE s2 -> i
  3360. /// s1 ADD s2 -> s
  3361. void op_2str(struct script_state* st, int32 op, const char* s1, const char* s2)
  3362. {
  3363. int32 a = 0;
  3364. switch(op){
  3365. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3366. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3367. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3368. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3369. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3370. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3371. case C_ADD:
  3372. {
  3373. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3374. strcpy(buf, s1);
  3375. strcat(buf, s2);
  3376. script_pushstr(st, buf);
  3377. return;
  3378. }
  3379. default:
  3380. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3381. script_reportsrc(st);
  3382. script_pushnil(st);
  3383. st->state = END;
  3384. return;
  3385. }
  3386. script_pushint(st,a);
  3387. }
  3388. /// Binary number operators
  3389. /// i OP i -> i
  3390. void op_2num( struct script_state* st, int32 op, int64 i1, int64 i2 ){
  3391. int64 ret;
  3392. switch( op ) {
  3393. case C_AND: ret = i1 & i2; break;
  3394. case C_OR: ret = i1 | i2; break;
  3395. case C_XOR: ret = i1 ^ i2; break;
  3396. case C_LAND: ret = (i1 && i2); break;
  3397. case C_LOR: ret = (i1 || i2); break;
  3398. case C_EQ: ret = (i1 == i2); break;
  3399. case C_NE: ret = (i1 != i2); break;
  3400. case C_GT: ret = (i1 > i2); break;
  3401. case C_GE: ret = (i1 >= i2); break;
  3402. case C_LT: ret = (i1 < i2); break;
  3403. case C_LE: ret = (i1 <= i2); break;
  3404. case C_R_SHIFT: ret = i1>>i2; break;
  3405. case C_L_SHIFT: ret = i1<<i2; break;
  3406. case C_DIV:
  3407. case C_MOD:
  3408. if( i2 == 0 )
  3409. {
  3410. ShowError("script:op_2num: division by zero detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3411. script_reportsrc(st);
  3412. script_pushnil(st);
  3413. st->state = END;
  3414. return;
  3415. }
  3416. else if( op == C_DIV )
  3417. ret = i1 / i2;
  3418. else//if( op == C_MOD )
  3419. ret = i1 % i2;
  3420. break;
  3421. default:
  3422. bool overflow;
  3423. // operators that can overflow/underflow
  3424. switch( op ) {
  3425. case C_ADD: overflow = util::safe_addition( i1, i2, ret ); break;
  3426. case C_SUB: overflow = util::safe_substraction( i1, i2, ret ); break;
  3427. case C_MUL: overflow = util::safe_multiplication( i1, i2, ret ); break;
  3428. default:
  3429. ShowError("script:op_2num: unexpected number operator %s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3430. script_reportsrc(st);
  3431. script_pushnil(st);
  3432. return;
  3433. }
  3434. if( overflow ){
  3435. ShowWarning("script:op_2num: overflow detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3436. script_reportsrc(st);
  3437. script_pushnil(st);
  3438. st->state = END;
  3439. return;
  3440. }
  3441. }
  3442. script_pushint(st, ret);
  3443. }
  3444. /// Binary operators
  3445. void op_2(struct script_state *st, int32 op)
  3446. {
  3447. struct script_data* left, leftref;
  3448. struct script_data* right;
  3449. leftref.type = C_NOP;
  3450. left = script_getdatatop(st, -2);
  3451. right = script_getdatatop(st, -1);
  3452. if (st->op2ref) {
  3453. if (data_isreference(left)) {
  3454. leftref = *left;
  3455. }
  3456. st->op2ref = 0;
  3457. }
  3458. get_val(st, left);
  3459. get_val(st, right);
  3460. // automatic conversions
  3461. switch( op )
  3462. {
  3463. case C_ADD:
  3464. if( data_isint(left) && data_isstring(right) )
  3465. {// convert int-string to string-string
  3466. conv_str(st, left);
  3467. }
  3468. else if( data_isstring(left) && data_isint(right) )
  3469. {// convert string-int32 to string-string
  3470. conv_str(st, right);
  3471. }
  3472. break;
  3473. }
  3474. if( data_isstring(left) && data_isstring(right) )
  3475. {// ss => op_2str
  3476. op_2str(st, op, left->u.str, right->u.str);
  3477. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3478. if (leftref.type != C_NOP)
  3479. {
  3480. if (left->type == C_STR) // don't free C_CONSTSTR
  3481. aFree(left->u.str);
  3482. *left = leftref;
  3483. }
  3484. }
  3485. else if( data_isint(left) && data_isint(right) )
  3486. {// ii => op_2num
  3487. int64 i1 = left->u.num;
  3488. int64 i2 = right->u.num;
  3489. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3490. op_2num(st, op, i1, i2);
  3491. if (leftref.type != C_NOP)
  3492. *left = leftref;
  3493. }
  3494. else
  3495. {// invalid argument
  3496. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3497. script_reportdata(left);
  3498. script_reportdata(right);
  3499. script_reportsrc(st);
  3500. script_removetop(st, -2, 0);
  3501. script_pushnil(st);
  3502. st->state = END;
  3503. }
  3504. }
  3505. /// Unary operators
  3506. /// NEG i -> i
  3507. /// NOT i -> i
  3508. /// LNOT i -> i
  3509. void op_1(struct script_state* st, int32 op)
  3510. {
  3511. struct script_data* data;
  3512. data = script_getdatatop(st, -1);
  3513. get_val(st, data);
  3514. if( !data_isint(data) )
  3515. {// not a number
  3516. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3517. script_reportdata(data);
  3518. script_reportsrc(st);
  3519. script_pushnil(st);
  3520. st->state = END;
  3521. return;
  3522. }
  3523. int64 i1 = data->u.num;
  3524. script_removetop(st, -1, 0);
  3525. switch( op )
  3526. {
  3527. case C_NEG: i1 = -i1; break;
  3528. case C_NOT: i1 = ~i1; break;
  3529. case C_LNOT: i1 = !i1; break;
  3530. default:
  3531. ShowError("script:op_1: unexpected operator %s i1=%" PRId64 "\n", script_op2name(op), i1);
  3532. script_reportsrc(st);
  3533. script_pushnil(st);
  3534. st->state = END;
  3535. return;
  3536. }
  3537. script_pushint(st, i1);
  3538. }
  3539. /// Checks the type of all arguments passed to a built-in function.
  3540. ///
  3541. /// @param st Script state whose stack arguments should be inspected.
  3542. /// @param func Built-in function for which the arguments are intended.
  3543. static void script_check_buildin_argtype(struct script_state* st, int32 func)
  3544. {
  3545. int32 idx, invalid = 0;
  3546. for( idx = 2; script_hasdata(st, idx); idx++ )
  3547. {
  3548. struct script_data* data = script_getdata(st, idx);
  3549. script_function* sf = &buildin_func[str_data[func].val];
  3550. char type = sf->arg[idx-2];
  3551. if( type == '?' || type == '*' )
  3552. {// optional argument or unknown number of optional parameters ( no types are after this )
  3553. break;
  3554. }
  3555. else if( type == 0 )
  3556. {// more arguments than necessary ( should not happen, as it is checked before )
  3557. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3558. invalid++;
  3559. break;
  3560. }
  3561. else
  3562. {
  3563. const char* name = nullptr;
  3564. if( data_isreference(data) )
  3565. {// get name for variables to determine the type they refer to
  3566. name = reference_getname(data);
  3567. }
  3568. switch( type )
  3569. {
  3570. case 'v':
  3571. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3572. {// variant
  3573. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3574. script_reportdata(data);
  3575. invalid++;
  3576. }
  3577. break;
  3578. case 's':
  3579. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3580. {// string
  3581. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3582. script_reportdata(data);
  3583. invalid++;
  3584. }
  3585. break;
  3586. case 'i':
  3587. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3588. {// int32 ( params and constants are always int32 )
  3589. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3590. script_reportdata(data);
  3591. invalid++;
  3592. }
  3593. break;
  3594. case 'r':
  3595. if( !data_isreference(data) )
  3596. {// variables
  3597. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3598. script_reportdata(data);
  3599. invalid++;
  3600. }
  3601. break;
  3602. case 'l':
  3603. if( !data_islabel(data) && !data_isfunclabel(data) )
  3604. {// label
  3605. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3606. script_reportdata(data);
  3607. invalid++;
  3608. }
  3609. break;
  3610. }
  3611. }
  3612. }
  3613. if(invalid)
  3614. {
  3615. ShowDebug("Function: %s\n", get_str(func));
  3616. script_reportsrc(st);
  3617. }
  3618. }
  3619. /// Executes a buildin command.
  3620. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3621. int32 run_func(struct script_state *st)
  3622. {
  3623. struct script_data* data;
  3624. int32 i,start_sp,end_sp,func;
  3625. end_sp = st->stack->sp;// position after the last argument
  3626. for( i = end_sp-1; i > 0 ; --i )
  3627. if( st->stack->stack_data[i].type == C_ARG )
  3628. break;
  3629. if( i == 0 ) {
  3630. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3631. st->state = END;
  3632. script_reportsrc(st);
  3633. return 1;
  3634. }
  3635. start_sp = i-1;// C_NAME of the command
  3636. st->start = start_sp;
  3637. st->end = end_sp;
  3638. data = &st->stack->stack_data[st->start];
  3639. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3640. func = (int)data->u.num;
  3641. st->funcname = reference_getname(data);
  3642. } else {
  3643. ShowError("script:run_func: not a buildin command.\n");
  3644. script_reportdata(data);
  3645. script_reportsrc(st);
  3646. st->state = END;
  3647. return 1;
  3648. }
  3649. if( script_config.warn_func_mismatch_argtypes ) {
  3650. script_check_buildin_argtype(st, func);
  3651. }
  3652. if(str_data[func].func) {
  3653. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3654. if( buildin_func[str_data[func].val].deprecated ){
  3655. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3656. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3657. script_reportsrc(st);
  3658. }
  3659. #endif
  3660. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) {
  3661. //Report error
  3662. ShowWarning("Script command '%s' returned failure.\n", get_str(func));
  3663. script_reportsrc(st);
  3664. }
  3665. } else {
  3666. ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", get_str(func), func, script_op2name(str_data[func].type));
  3667. script_reportsrc(st);
  3668. st->state = END;
  3669. }
  3670. // Stack's datum are used when re-running functions [Eoe]
  3671. if( st->state == RERUNLINE )
  3672. return 0;
  3673. pop_stack(st, st->start, st->end);
  3674. if( st->state == RETFUNC ) {// return from a user-defined function
  3675. struct script_retinfo* ri;
  3676. int32 olddefsp = st->stack->defsp;
  3677. int32 nargs;
  3678. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3679. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3680. {
  3681. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3682. script_reportsrc(st);
  3683. st->state = END;
  3684. return 1;
  3685. }
  3686. script_free_vars(st->stack->scope.vars);
  3687. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3688. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3689. nargs = ri->nargs;
  3690. st->pos = ri->pos;
  3691. st->script = ri->script;
  3692. st->stack->scope.vars = ri->scope.vars;
  3693. st->stack->scope.arrays = ri->scope.arrays;
  3694. st->stack->defsp = ri->defsp;
  3695. memset(ri, 0, sizeof(struct script_retinfo));
  3696. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3697. st->state = GOTO;
  3698. }
  3699. return 0;
  3700. }
  3701. /*==========================================
  3702. * script execution
  3703. *------------------------------------------*/
  3704. void run_script(struct script_code *rootscript, int32 pos, int32 rid, int32 oid)
  3705. {
  3706. struct script_state *st;
  3707. if( rootscript == nullptr || pos < 0 )
  3708. return;
  3709. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3710. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3711. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3712. st = script_alloc_state(rootscript, pos, rid, oid);
  3713. run_script_main(st);
  3714. }
  3715. /**
  3716. * Free all related script code
  3717. * @param code: Script code to free
  3718. */
  3719. void script_stop_scriptinstances(struct script_code *code) {
  3720. DBIterator *iter;
  3721. struct script_state* st;
  3722. if( !active_scripts )
  3723. return; // Don't even bother.
  3724. iter = db_iterator(st_db);
  3725. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3726. if( st->script == code )
  3727. script_free_state(st);
  3728. }
  3729. dbi_destroy(iter);
  3730. }
  3731. /*==========================================
  3732. * Timer function for sleep
  3733. *------------------------------------------*/
  3734. TIMER_FUNC(run_script_timer){
  3735. struct script_state *st = (struct script_state *)data;
  3736. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3737. // If it was a player before going to sleep and there is still a unit attached to the script
  3738. if( id != 0 && st->rid ){
  3739. map_session_data *sd = map_id2sd(st->rid);
  3740. // Attached player is offline(logout) or another unit type(should not happen)
  3741. if( !sd ){
  3742. st->rid = 0;
  3743. st->state = END;
  3744. // Character mismatch. Cancel execution.
  3745. }else if( sd->status.char_id != id ){
  3746. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3747. script_reportsrc(st);
  3748. st->rid = 0;
  3749. st->state = END;
  3750. }
  3751. }
  3752. while (node && st->sleep.timer != INVALID_TIMER) {
  3753. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3754. script_erase_sleepdb(node);
  3755. st->sleep.timer = INVALID_TIMER;
  3756. break;
  3757. }
  3758. node = node->next;
  3759. }
  3760. if(st->state != RERUNLINE)
  3761. st->sleep.tick = 0;
  3762. run_script_main(st);
  3763. return 0;
  3764. }
  3765. /**
  3766. * Remove sleep timers from the NPC
  3767. * @param id: NPC ID
  3768. */
  3769. void script_stop_sleeptimers(int32 id) {
  3770. for (;;) {
  3771. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3772. if (!st)
  3773. break; // No more sleep timers
  3774. if (st->oid == id)
  3775. script_free_state(st);
  3776. }
  3777. }
  3778. /**
  3779. * Delete the specified node from sleep_db
  3780. * @param n: Linked list of sleep timers
  3781. */
  3782. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3783. struct linkdb_node *retnode;
  3784. if (!n)
  3785. return nullptr;
  3786. if (!n->prev)
  3787. sleep_db = n->next;
  3788. else
  3789. n->prev->next = n->next;
  3790. if (n->next)
  3791. n->next->prev = n->prev;
  3792. retnode = n->next;
  3793. aFree(n);
  3794. return retnode;
  3795. }
  3796. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3797. ///
  3798. /// @param st Script state to detach.
  3799. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3800. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3801. {
  3802. map_session_data* sd;
  3803. if(st->rid && (sd = map_id2sd(st->rid))!=nullptr) {
  3804. if( sd->state.using_fake_npc ){
  3805. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  3806. sd->state.using_fake_npc = 0;
  3807. }
  3808. sd->st = st->bk_st;
  3809. sd->npc_id = st->bk_npcid;
  3810. sd->state.disable_atcommand_on_npc = 0;
  3811. if(st->bk_st) {
  3812. //Remove tag for removal.
  3813. st->bk_st = nullptr;
  3814. st->bk_npcid = 0;
  3815. } else if(dequeue_event) {
  3816. #ifdef SECURE_NPCTIMEOUT
  3817. /**
  3818. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3819. **/
  3820. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3821. delete_timer(sd->npc_idle_timer,npc_secure_timeout_timer);
  3822. sd->npc_idle_timer = INVALID_TIMER;
  3823. }
  3824. #endif
  3825. npc_event_dequeue(sd);
  3826. }
  3827. }
  3828. else if(st->bk_st)
  3829. {// rid was set to 0, before detaching the script state
  3830. ShowError("script_detach_state: Found previous script state without attached player (rid=%d, oid=%d, state=%d, bk_npcid=%d)\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3831. script_reportsrc(st->bk_st);
  3832. script_free_state(st->bk_st);
  3833. st->bk_st = nullptr;
  3834. }
  3835. }
  3836. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3837. ///
  3838. /// @param st Script state to attach.
  3839. void script_attach_state(struct script_state* st){
  3840. map_session_data* sd;
  3841. if(st->rid && (sd = map_id2sd(st->rid))!=nullptr)
  3842. {
  3843. if(st!=sd->st)
  3844. {
  3845. if(st->bk_st)
  3846. {// there is already a backup
  3847. ShowDebug("script_attach_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3848. }
  3849. st->bk_st = sd->st;
  3850. st->bk_npcid = sd->npc_id;
  3851. }
  3852. sd->st = st;
  3853. sd->npc_id = st->oid;
  3854. sd->npc_item_flag = st->npc_item_flag; // load default.
  3855. sd->state.disable_atcommand_on_npc = battle_config.atcommand_disable_npc && (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3856. #ifdef SECURE_NPCTIMEOUT
  3857. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  3858. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_secure_timeout_timer,sd->bl.id,0);
  3859. sd->npc_idle_tick = gettick();
  3860. #endif
  3861. }
  3862. }
  3863. /*==========================================
  3864. * The main part of the script execution
  3865. *------------------------------------------*/
  3866. void run_script_main(struct script_state *st)
  3867. {
  3868. int32 cmdcount = script_config.check_cmdcount;
  3869. int32 gotocount = script_config.check_gotocount;
  3870. TBL_PC *sd;
  3871. struct script_stack *stack = st->stack;
  3872. script_attach_state(st);
  3873. if(st->state == RERUNLINE) {
  3874. run_func(st);
  3875. if(st->state == GOTO)
  3876. st->state = RUN;
  3877. } else if(st->state != END)
  3878. st->state = RUN;
  3879. while(st->state == RUN) {
  3880. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3881. switch(c){
  3882. case C_EOL:
  3883. if( stack->defsp > stack->sp )
  3884. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3885. else
  3886. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3887. break;
  3888. case C_INT:
  3889. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3890. break;
  3891. case C_POS:
  3892. case C_NAME:
  3893. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3894. st->pos+=3;
  3895. break;
  3896. case C_ARG:
  3897. push_val(stack,c,0);
  3898. break;
  3899. case C_STR:
  3900. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3901. while(st->script->script_buf[st->pos++]);
  3902. break;
  3903. case C_FUNC:
  3904. run_func(st);
  3905. if(st->state==GOTO){
  3906. st->state = RUN;
  3907. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3908. ShowError("script:run_script_main: infinity loop !\n");
  3909. script_reportsrc(st);
  3910. st->state=END;
  3911. }
  3912. }
  3913. break;
  3914. case C_REF:
  3915. st->op2ref = 1;
  3916. break;
  3917. case C_NEG:
  3918. case C_NOT:
  3919. case C_LNOT:
  3920. op_1(st ,c);
  3921. break;
  3922. case C_ADD:
  3923. case C_SUB:
  3924. case C_MUL:
  3925. case C_DIV:
  3926. case C_MOD:
  3927. case C_EQ:
  3928. case C_NE:
  3929. case C_GT:
  3930. case C_GE:
  3931. case C_LT:
  3932. case C_LE:
  3933. case C_AND:
  3934. case C_OR:
  3935. case C_XOR:
  3936. case C_LAND:
  3937. case C_LOR:
  3938. case C_R_SHIFT:
  3939. case C_L_SHIFT:
  3940. op_2(st, c);
  3941. break;
  3942. case C_OP3:
  3943. op_3(st, c);
  3944. break;
  3945. case C_NOP:
  3946. st->state=END;
  3947. break;
  3948. default:
  3949. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3950. st->state=END;
  3951. break;
  3952. }
  3953. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3954. ShowError("script:run_script_main: infinity loop !\n");
  3955. script_reportsrc(st);
  3956. st->state=END;
  3957. }
  3958. }
  3959. if(st->sleep.tick > 0) {
  3960. //Restore previous script
  3961. script_detach_state(st, false);
  3962. //Delay execution
  3963. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3964. st->sleep.charid = sd?sd->status.char_id:0;
  3965. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3966. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3967. } else if(st->state != END && st->rid) {
  3968. //Resume later (st is already attached to player).
  3969. if(st->bk_st) {
  3970. ShowWarning("Unable to restore stack! Double continuation!\n");
  3971. //Report BOTH scripts to see if that can help somehow.
  3972. ShowDebug("Previous script (lost):\n");
  3973. script_reportsrc(st->bk_st);
  3974. ShowDebug("Current script:\n");
  3975. script_reportsrc(st);
  3976. script_free_state(st->bk_st);
  3977. st->bk_st = nullptr;
  3978. }
  3979. } else {
  3980. if (st->stack && st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp - 1].type == C_RETINFO) {
  3981. for (int32 i = 0; i < st->stack->sp; i++) {
  3982. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  3983. st->script = st->stack->stack_data[i].u.ri->script;
  3984. break;
  3985. }
  3986. }
  3987. }
  3988. //Dispose of script.
  3989. if ((sd = map_id2sd(st->rid))!=nullptr)
  3990. { //Restore previous stack and save char.
  3991. if(sd->state.using_fake_npc){
  3992. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  3993. sd->state.using_fake_npc = 0;
  3994. }
  3995. //Restore previous script if any.
  3996. script_detach_state(st, true);
  3997. if (sd->vars_dirty)
  3998. intif_saveregistry(sd);
  3999. }
  4000. script_free_state(st);
  4001. }
  4002. }
  4003. int32 script_config_read(const char *cfgName)
  4004. {
  4005. int32 i;
  4006. char line[1024],w1[1024],w2[1024];
  4007. FILE *fp;
  4008. fp=fopen(cfgName,"r");
  4009. if(fp==nullptr){
  4010. ShowError("File not found: %s\n", cfgName);
  4011. return 1;
  4012. }
  4013. while(fgets(line, sizeof(line), fp))
  4014. {
  4015. if(line[0] == '/' && line[1] == '/')
  4016. continue;
  4017. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  4018. if(i!=2)
  4019. continue;
  4020. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  4021. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  4022. }
  4023. else if(strcmpi(w1,"check_cmdcount")==0) {
  4024. script_config.check_cmdcount = config_switch(w2);
  4025. }
  4026. else if(strcmpi(w1,"check_gotocount")==0) {
  4027. script_config.check_gotocount = config_switch(w2);
  4028. }
  4029. else if(strcmpi(w1,"input_min_value")==0) {
  4030. script_config.input_min_value = config_switch(w2);
  4031. }
  4032. else if(strcmpi(w1,"input_max_value")==0) {
  4033. script_config.input_max_value = config_switch(w2);
  4034. }
  4035. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  4036. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  4037. }
  4038. else if(strcmpi(w1,"import")==0){
  4039. script_config_read(w2);
  4040. }
  4041. else {
  4042. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  4043. }
  4044. }
  4045. fclose(fp);
  4046. return 0;
  4047. }
  4048. /**
  4049. * @see DBApply
  4050. */
  4051. static int32 db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  4052. {
  4053. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  4054. if (code)
  4055. script_free_code(code);
  4056. return 0;
  4057. }
  4058. void script_run_autobonus(const char *autobonus, map_session_data *sd, uint32 pos)
  4059. {
  4060. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4061. if( script )
  4062. {
  4063. int32 j;
  4064. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4065. if( j < EQI_MAX ) {
  4066. //Single item autobonus
  4067. current_equip_item_index = sd->equip_index[j];
  4068. current_equip_combo_pos = 0;
  4069. } else {
  4070. //Combo autobonus
  4071. current_equip_item_index = -1;
  4072. current_equip_combo_pos = pos;
  4073. }
  4074. run_script(script,0,sd->bl.id,0);
  4075. }
  4076. }
  4077. void script_add_autobonus(const char *autobonus)
  4078. {
  4079. if( strdb_get(autobonus_db, autobonus) == nullptr )
  4080. {
  4081. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4082. if( script )
  4083. strdb_put(autobonus_db, autobonus, script);
  4084. }
  4085. }
  4086. void script_run_petautobonus(const std::string &autobonus, map_session_data &sd) {
  4087. std::shared_ptr<s_pet_autobonus_wrapper> script = util::umap_find(pet_autobonuses, autobonus);
  4088. if (script != nullptr) {
  4089. run_script(script->script, 0, sd.bl.id, 0);
  4090. }
  4091. }
  4092. void script_add_petautobonus(const std::string &autobonus) {
  4093. if (util::umap_find(pet_autobonuses, autobonus) == nullptr) {
  4094. script_code *script = parse_script(autobonus.c_str(), "petautobonus", 0, 0);
  4095. if (script != nullptr) {
  4096. std::shared_ptr<s_pet_autobonus_wrapper> bonus = std::make_shared<s_pet_autobonus_wrapper>();
  4097. bonus->script = script;
  4098. pet_autobonuses.emplace(autobonus, bonus);
  4099. }
  4100. }
  4101. }
  4102. /// resets a temporary character array variable to given value
  4103. void script_cleararray_pc( map_session_data* sd, const char* varname ){
  4104. struct script_array *sa = nullptr;
  4105. struct reg_db *src = nullptr;
  4106. uint32 i, *list = nullptr, size = 0;
  4107. int32 key;
  4108. key = add_str(varname);
  4109. if( !(src = script_array_src(nullptr,sd,varname,nullptr) ) )
  4110. return;
  4111. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4112. return;
  4113. size = sa->size;
  4114. list = script_array_cpy_list(sa);
  4115. for( i = 0; i < size; i++ ){
  4116. clear_reg( nullptr, sd, reference_uid( key, list[i] ), varname, nullptr );
  4117. }
  4118. }
  4119. /// sets a temporary character array variable element idx to given value
  4120. /// @param refcache Pointer to an int32 variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be nullptr if only one element is set.
  4121. void script_setarray_pc(map_session_data* sd, const char* varname, uint32 idx, int64 value, int* refcache)
  4122. {
  4123. int32 key;
  4124. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4125. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4126. return;
  4127. }
  4128. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4129. set_reg_num( nullptr, sd, reference_uid( key, idx ), varname, value, nullptr );
  4130. if( refcache ) {
  4131. // save to avoid repeated script->add_str calls
  4132. refcache[0] = key;
  4133. }
  4134. }
  4135. /**
  4136. * Clears persistent variables from memory
  4137. **/
  4138. int32 script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4139. {
  4140. struct script_reg_state *src;
  4141. if( data->type != DB_DATA_PTR ) // got no need for those!
  4142. return 0;
  4143. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4144. if( src->type ) {
  4145. struct script_reg_str *p = (struct script_reg_str *)src;
  4146. if( p->value )
  4147. aFree(p->value);
  4148. ers_free(str_reg_ers,p);
  4149. } else {
  4150. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4151. }
  4152. return 0;
  4153. }
  4154. /**
  4155. * Clears a single persistent variable
  4156. **/
  4157. void script_reg_destroy_single(map_session_data *sd, int64 reg, struct script_reg_state *data)
  4158. {
  4159. i64db_remove(sd->regs.vars, reg);
  4160. if( data->type ) {
  4161. struct script_reg_str *p = (struct script_reg_str*)data;
  4162. if( p->value )
  4163. aFree(p->value);
  4164. ers_free(str_reg_ers,p);
  4165. } else {
  4166. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4167. }
  4168. }
  4169. uint32 *script_array_cpy_list(struct script_array *sa)
  4170. {
  4171. if( sa->size > generic_ui_array_size )
  4172. script_generic_ui_array_expand(sa->size);
  4173. memcpy(generic_ui_array, sa->members, sizeof(uint32)*sa->size);
  4174. return generic_ui_array;
  4175. }
  4176. void script_generic_ui_array_expand (uint32 plus)
  4177. {
  4178. generic_ui_array_size += plus + 100;
  4179. RECREATE(generic_ui_array, uint32, generic_ui_array_size);
  4180. }
  4181. /*==========================================
  4182. * Destructor
  4183. *------------------------------------------*/
  4184. void do_final_script() {
  4185. int32 i;
  4186. DBIterator *iter;
  4187. struct script_state *st;
  4188. #ifdef DEBUG_HASH
  4189. if (battle_config.etc_log)
  4190. {
  4191. FILE *fp = fopen("hash_dump.txt","wt");
  4192. if(fp) {
  4193. int32 count[SCRIPT_HASH_SIZE];
  4194. int32 count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4195. int32 n=0;
  4196. int32 min=INT_MAX,max=0,zero=0;
  4197. double mean=0.0f;
  4198. double median=0.0f;
  4199. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4200. memset(count, 0, sizeof(count));
  4201. fprintf(fp,"num : hash : data_name\n");
  4202. fprintf(fp,"---------------------------------------------------------------\n");
  4203. for(i=LABEL_START; i<str_num; i++) {
  4204. uint32 h = calc_hash(get_str(i));
  4205. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4206. ++count[h];
  4207. }
  4208. fprintf(fp,"--------------------\n\n");
  4209. memset(count2, 0, sizeof(count2));
  4210. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4211. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4212. if(min > count[i])
  4213. min = count[i]; // minimun count of collision
  4214. if(max < count[i])
  4215. max = count[i]; // maximun count of collision
  4216. if(count[i] == 0)
  4217. zero++;
  4218. ++count2[count[i]];
  4219. }
  4220. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4221. for( i=min; i <= max; ++i ){
  4222. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4223. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4224. }
  4225. for( i=min; i <= max; ++i ){
  4226. n += count2[i];
  4227. if( n*2 >= SCRIPT_HASH_SIZE )
  4228. {
  4229. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4230. median = (i+i+1)/2.0f;
  4231. else
  4232. median = i;
  4233. break;
  4234. }
  4235. }
  4236. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4237. fclose(fp);
  4238. }
  4239. }
  4240. #endif
  4241. mapreg_final();
  4242. db_destroy(scriptlabel_db);
  4243. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4244. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4245. ers_destroy(array_ers);
  4246. if (generic_ui_array)
  4247. aFree(generic_ui_array);
  4248. iter = db_iterator(st_db);
  4249. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4250. script_free_state(st);
  4251. dbi_destroy(iter);
  4252. if (str_data)
  4253. aFree(str_data);
  4254. if (str_buf)
  4255. aFree(str_buf);
  4256. for( i = 0; i < atcmd_binding_count; i++ ) {
  4257. aFree(atcmd_binding[i]);
  4258. }
  4259. if( atcmd_binding_count != 0 )
  4260. aFree(atcmd_binding);
  4261. ers_destroy(st_ers);
  4262. ers_destroy(stack_ers);
  4263. db_destroy(st_db);
  4264. }
  4265. /*==========================================
  4266. * Initialization
  4267. *------------------------------------------*/
  4268. void do_init_script(void) {
  4269. st_db = idb_alloc(DB_OPT_BASE);
  4270. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4271. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4272. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4273. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4274. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4275. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4276. add_timer_func_list( run_script_timer, "run_script_timer" );
  4277. ers_chunk_size(st_ers, 10);
  4278. ers_chunk_size(stack_ers, 10);
  4279. active_scripts = 0;
  4280. next_id = 0;
  4281. mapreg_init();
  4282. add_buildin_func();
  4283. constant_db.load();
  4284. script_hardcoded_constants();
  4285. }
  4286. void script_reload(void) {
  4287. int32 i;
  4288. DBIterator *iter;
  4289. struct script_state *st;
  4290. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4291. db_clear(scriptlabel_db);
  4292. // @commands (script based)
  4293. // Clear bindings
  4294. for( i = 0; i < atcmd_binding_count; i++ ) {
  4295. aFree(atcmd_binding[i]);
  4296. }
  4297. if( atcmd_binding_count != 0 )
  4298. aFree(atcmd_binding);
  4299. atcmd_binding_count = 0;
  4300. iter = db_iterator(st_db);
  4301. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4302. script_free_state(st);
  4303. dbi_destroy(iter);
  4304. db_clear(st_db);
  4305. mapreg_reload();
  4306. }
  4307. //-----------------------------------------------------------------------------
  4308. // buildin functions
  4309. //
  4310. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, nullptr }
  4311. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, nullptr }
  4312. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4313. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4314. #define BUILDIN_FUNC(x) int32 buildin_ ## x (struct script_state* st)
  4315. /////////////////////////////////////////////////////////////////////
  4316. // NPC interaction
  4317. //
  4318. /// Appends a message to the npc dialog.
  4319. /// If a dialog doesn't exist yet, one is created.
  4320. ///
  4321. /// mes "<message>";
  4322. BUILDIN_FUNC(mes)
  4323. {
  4324. map_session_data* sd;
  4325. if( !script_rid2sd(sd) )
  4326. return SCRIPT_CMD_SUCCESS;
  4327. if( !script_hasdata(st, 3) )
  4328. {// only a single line detected in the script
  4329. clif_scriptmes( *sd, st->oid, script_getstr( st, 2 ) );
  4330. }
  4331. else
  4332. {// parse multiple lines as they exist
  4333. int32 i;
  4334. for( i = 2; script_hasdata(st, i); i++ )
  4335. {
  4336. // send the message to the client
  4337. clif_scriptmes( *sd, st->oid, script_getstr( st, i ) );
  4338. }
  4339. }
  4340. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4341. return SCRIPT_CMD_SUCCESS;
  4342. }
  4343. /// Displays the button 'next' in the npc dialog.
  4344. /// The dialog text is cleared and the script continues when the button is pressed.
  4345. ///
  4346. /// next;
  4347. BUILDIN_FUNC(next)
  4348. {
  4349. map_session_data* sd;
  4350. if (!st->mes_active) {
  4351. ShowWarning("buildin_next: There is no mes active.\n");
  4352. return SCRIPT_CMD_FAILURE;
  4353. }
  4354. if( !script_rid2sd(sd) )
  4355. return SCRIPT_CMD_SUCCESS;
  4356. #ifdef SECURE_NPCTIMEOUT
  4357. sd->npc_idle_type = NPCT_WAIT;
  4358. #endif
  4359. st->state = STOP;
  4360. clif_scriptnext( *sd, st->oid );
  4361. return SCRIPT_CMD_SUCCESS;
  4362. }
  4363. /// Clears the dialog and continues the script without a next button.
  4364. ///
  4365. /// clear;
  4366. BUILDIN_FUNC(clear)
  4367. {
  4368. TBL_PC* sd;
  4369. if (!st->mes_active) {
  4370. ShowWarning("buildin_clear: There is no mes active.\n");
  4371. return SCRIPT_CMD_FAILURE;
  4372. }
  4373. if (!script_rid2sd(sd))
  4374. return SCRIPT_CMD_FAILURE;
  4375. clif_scriptclear( *sd, st->oid );
  4376. return SCRIPT_CMD_SUCCESS;
  4377. }
  4378. /// Ends the script and displays the button 'close' on the npc dialog.
  4379. /// The dialog is closed when the button is pressed.
  4380. ///
  4381. /// close;
  4382. BUILDIN_FUNC(close)
  4383. {
  4384. map_session_data* sd;
  4385. if( !script_rid2sd(sd) )
  4386. return SCRIPT_CMD_SUCCESS;
  4387. npc_data* nd = map_id2nd( st->oid );
  4388. if( nd != nullptr && nd->dynamicnpc.owner_char_id != 0 ){
  4389. nd->dynamicnpc.last_interaction = gettick();
  4390. }
  4391. const char* command = script_getfuncname( st );
  4392. if( !st->mes_active ) {
  4393. st->state = END; // Keep backwards compatibility.
  4394. ShowWarning("buildin_close: Incorrect use of '%s' command!\n", command);
  4395. script_reportsrc(st);
  4396. } else {
  4397. st->state = CLOSE;
  4398. st->mes_active = 0;
  4399. }
  4400. if( !strcmp(command, "close3") ){
  4401. st->clear_cutin = true;
  4402. }
  4403. clif_scriptclose( *sd, st->oid );
  4404. return SCRIPT_CMD_SUCCESS;
  4405. }
  4406. /// Displays the button 'close' on the npc dialog.
  4407. /// The dialog is closed and the script continues when the button is pressed.
  4408. ///
  4409. /// close2;
  4410. BUILDIN_FUNC(close2)
  4411. {
  4412. TBL_PC* sd;
  4413. if( !script_rid2sd(sd) )
  4414. return SCRIPT_CMD_SUCCESS;
  4415. st->state = STOP;
  4416. if( st->mes_active )
  4417. st->mes_active = 0;
  4418. clif_scriptclose( *sd, st->oid );
  4419. return SCRIPT_CMD_SUCCESS;
  4420. }
  4421. /// Counts the number of valid and total number of options in 'str'
  4422. /// If max_count > 0 the counting stops when that valid option is reached
  4423. /// total is incremented for each option (nullptr is supported)
  4424. static int32 menu_countoptions(const char* str, int32 max_count, int* total)
  4425. {
  4426. int32 count = 0;
  4427. int32 bogus_total;
  4428. if( total == nullptr )
  4429. total = &bogus_total;
  4430. ++(*total);
  4431. // initial empty options
  4432. while( *str == ':' )
  4433. {
  4434. ++str;
  4435. ++(*total);
  4436. }
  4437. // count menu options
  4438. while( *str != '\0' )
  4439. {
  4440. ++count;
  4441. --max_count;
  4442. if( max_count == 0 )
  4443. break;
  4444. while( *str != ':' && *str != '\0' )
  4445. ++str;
  4446. while( *str == ':' )
  4447. {
  4448. ++str;
  4449. ++(*total);
  4450. }
  4451. }
  4452. return count;
  4453. }
  4454. /// Displays a menu with options and goes to the target label.
  4455. /// The script is stopped if cancel is pressed.
  4456. /// Options with no text are not displayed in the client.
  4457. ///
  4458. /// Options can be grouped together, separated by the character ':' in the text:
  4459. /// ex: menu "A:B:C",L_target;
  4460. /// All these options go to the specified target label.
  4461. ///
  4462. /// The index of the selected option is put in the variable @menu.
  4463. /// Indexes start with 1 and are consistent with grouped and empty options.
  4464. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4465. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4466. ///
  4467. /// NOTE: the client closes the npc dialog when cancel is pressed
  4468. ///
  4469. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4470. BUILDIN_FUNC(menu)
  4471. {
  4472. int32 i;
  4473. const char* text;
  4474. TBL_PC* sd;
  4475. if( !script_rid2sd(sd) )
  4476. return SCRIPT_CMD_SUCCESS;
  4477. #ifdef SECURE_NPCTIMEOUT
  4478. sd->npc_idle_type = NPCT_MENU;
  4479. #endif
  4480. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4481. if( sd->state.menu_or_input == 0 )
  4482. {
  4483. struct StringBuf buf;
  4484. if( script_lastdata(st) % 2 == 0 )
  4485. {// argument count is not even (1st argument is at index 2)
  4486. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4487. st->state = END;
  4488. return SCRIPT_CMD_FAILURE;
  4489. }
  4490. StringBuf_Init(&buf);
  4491. sd->npc_menu = 0;
  4492. for( i = 2; i < script_lastdata(st); i += 2 )
  4493. {
  4494. struct script_data* data;
  4495. // menu options
  4496. text = script_getstr(st, i);
  4497. // target label
  4498. data = script_getdata(st, i+1);
  4499. if( !data_islabel(data) )
  4500. {// not a label
  4501. StringBuf_Destroy(&buf);
  4502. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4503. script_reportdata(data);
  4504. st->state = END;
  4505. return SCRIPT_CMD_FAILURE;
  4506. }
  4507. // append option(s)
  4508. if( text[0] == '\0' )
  4509. continue;// empty string, ignore
  4510. if( sd->npc_menu > 0 )
  4511. StringBuf_AppendStr(&buf, ":");
  4512. StringBuf_AppendStr(&buf, text);
  4513. sd->npc_menu += menu_countoptions(text, 0, nullptr);
  4514. }
  4515. st->state = RERUNLINE;
  4516. sd->state.menu_or_input = 1;
  4517. /**
  4518. * menus beyond this length crash the client (see bugreport:6402)
  4519. **/
  4520. if( StringBuf_Length(&buf) >= 2047 ) {
  4521. struct npc_data * nd = map_id2nd(st->oid);
  4522. char* menu;
  4523. CREATE(menu, char, 2048);
  4524. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4525. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4526. clif_scriptmenu( *sd, st->oid, menu );
  4527. aFree(menu);
  4528. } else
  4529. clif_scriptmenu( *sd, st->oid, StringBuf_Value( &buf ) );
  4530. StringBuf_Destroy(&buf);
  4531. if( sd->npc_menu >= 0xff )
  4532. {// client supports only up to 254 entries; 0 is not used and 255 is reserved for cancel; excess entries are displayed but cause 'uint8' overflow
  4533. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4534. script_reportsrc(st);
  4535. }
  4536. }
  4537. else if( sd->npc_menu == 0xff )
  4538. {// Cancel was pressed
  4539. sd->state.menu_or_input = 0;
  4540. st->state = END;
  4541. }
  4542. else
  4543. {// goto target label
  4544. int32 menu = 0;
  4545. sd->state.menu_or_input = 0;
  4546. if( sd->npc_menu <= 0 )
  4547. {
  4548. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4549. st->state = END;
  4550. return SCRIPT_CMD_FAILURE;
  4551. }
  4552. // get target label
  4553. for( i = 2; i < script_lastdata(st); i += 2 )
  4554. {
  4555. text = script_getstr(st, i);
  4556. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4557. if( sd->npc_menu <= 0 )
  4558. break;// entry found
  4559. }
  4560. if( sd->npc_menu > 0 )
  4561. {// Invalid selection
  4562. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4563. st->state = END;
  4564. return SCRIPT_CMD_FAILURE;
  4565. }
  4566. if( !data_islabel(script_getdata(st, i + 1)) )
  4567. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4568. ShowError("buildin_menu: Unexpected data in label argument\n");
  4569. script_reportdata(script_getdata(st, i + 1));
  4570. st->state = END;
  4571. return SCRIPT_CMD_FAILURE;
  4572. }
  4573. pc_setreg(sd, add_str("@menu"), menu);
  4574. st->pos = script_getnum(st, i + 1);
  4575. st->state = GOTO;
  4576. }
  4577. return SCRIPT_CMD_SUCCESS;
  4578. }
  4579. /// Displays a menu with options and returns the selected option.
  4580. /// Behaves like 'menu' without the target labels.
  4581. ///
  4582. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4583. ///
  4584. /// @see menu
  4585. BUILDIN_FUNC(select)
  4586. {
  4587. int32 i;
  4588. const char* text;
  4589. TBL_PC* sd;
  4590. if( !script_rid2sd(sd) )
  4591. return SCRIPT_CMD_SUCCESS;
  4592. #ifdef SECURE_NPCTIMEOUT
  4593. sd->npc_idle_type = NPCT_MENU;
  4594. #endif
  4595. if( sd->state.menu_or_input == 0 ) {
  4596. struct StringBuf buf;
  4597. StringBuf_Init(&buf);
  4598. sd->npc_menu = 0;
  4599. for( i = 2; i <= script_lastdata(st); ++i ) {
  4600. text = script_getstr(st, i);
  4601. if( sd->npc_menu > 0 )
  4602. StringBuf_AppendStr(&buf, ":");
  4603. StringBuf_AppendStr(&buf, text);
  4604. sd->npc_menu += menu_countoptions(text, 0, nullptr);
  4605. }
  4606. st->state = RERUNLINE;
  4607. sd->state.menu_or_input = 1;
  4608. /**
  4609. * menus beyond this length crash the client (see bugreport:6402)
  4610. **/
  4611. if( StringBuf_Length(&buf) >= 2047 ) {
  4612. struct npc_data * nd = map_id2nd(st->oid);
  4613. char* menu;
  4614. CREATE(menu, char, 2048);
  4615. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4616. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4617. clif_scriptmenu( *sd, st->oid, menu );
  4618. aFree(menu);
  4619. } else
  4620. clif_scriptmenu( *sd, st->oid, StringBuf_Value( &buf ) );
  4621. StringBuf_Destroy(&buf);
  4622. if( sd->npc_menu >= 0xff ) {
  4623. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4624. script_reportsrc(st);
  4625. }
  4626. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4627. sd->state.menu_or_input = 0;
  4628. st->state = END;
  4629. } else {// return selected option
  4630. int32 menu = 0;
  4631. sd->state.menu_or_input = 0;
  4632. for( i = 2; i <= script_lastdata(st); ++i ) {
  4633. text = script_getstr(st, i);
  4634. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4635. if( sd->npc_menu <= 0 )
  4636. break;// entry found
  4637. }
  4638. pc_setreg(sd, add_str("@menu"), menu);
  4639. script_pushint(st, menu);
  4640. st->state = RUN;
  4641. }
  4642. return SCRIPT_CMD_SUCCESS;
  4643. }
  4644. /// Displays a menu with options and returns the selected option.
  4645. /// Behaves like 'menu' without the target labels, except when cancel is
  4646. /// pressed.
  4647. /// When cancel is pressed, the script continues and 255 is returned.
  4648. ///
  4649. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4650. ///
  4651. /// @see menu
  4652. BUILDIN_FUNC(prompt)
  4653. {
  4654. int32 i;
  4655. const char *text;
  4656. TBL_PC* sd;
  4657. if( !script_rid2sd(sd) )
  4658. return SCRIPT_CMD_SUCCESS;
  4659. #ifdef SECURE_NPCTIMEOUT
  4660. sd->npc_idle_type = NPCT_MENU;
  4661. #endif
  4662. if( sd->state.menu_or_input == 0 )
  4663. {
  4664. struct StringBuf buf;
  4665. StringBuf_Init(&buf);
  4666. sd->npc_menu = 0;
  4667. for( i = 2; i <= script_lastdata(st); ++i )
  4668. {
  4669. text = script_getstr(st, i);
  4670. if( sd->npc_menu > 0 )
  4671. StringBuf_AppendStr(&buf, ":");
  4672. StringBuf_AppendStr(&buf, text);
  4673. sd->npc_menu += menu_countoptions(text, 0, nullptr);
  4674. }
  4675. st->state = RERUNLINE;
  4676. sd->state.menu_or_input = 1;
  4677. /**
  4678. * menus beyond this length crash the client (see bugreport:6402)
  4679. **/
  4680. if( StringBuf_Length(&buf) >= 2047 ) {
  4681. struct npc_data * nd = map_id2nd(st->oid);
  4682. char* menu;
  4683. CREATE(menu, char, 2048);
  4684. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4685. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4686. clif_scriptmenu( *sd, st->oid, menu );
  4687. aFree(menu);
  4688. } else
  4689. clif_scriptmenu( *sd, st->oid, StringBuf_Value( &buf ) );
  4690. StringBuf_Destroy(&buf);
  4691. if( sd->npc_menu >= 0xff )
  4692. {
  4693. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4694. script_reportsrc(st);
  4695. }
  4696. }
  4697. else if( sd->npc_menu == 0xff )
  4698. {// Cancel was pressed
  4699. sd->state.menu_or_input = 0;
  4700. pc_setreg(sd, add_str("@menu"), 0xff);
  4701. script_pushint(st, 0xff);
  4702. st->state = RUN;
  4703. }
  4704. else
  4705. {// return selected option
  4706. int32 menu = 0;
  4707. sd->state.menu_or_input = 0;
  4708. for( i = 2; i <= script_lastdata(st); ++i )
  4709. {
  4710. text = script_getstr(st, i);
  4711. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4712. if( sd->npc_menu <= 0 )
  4713. break;// entry found
  4714. }
  4715. pc_setreg(sd, add_str("@menu"), menu);
  4716. script_pushint(st, menu);
  4717. st->state = RUN;
  4718. }
  4719. return SCRIPT_CMD_SUCCESS;
  4720. }
  4721. /////////////////////////////////////////////////////////////////////
  4722. // ...
  4723. //
  4724. /// Jumps to the target script label.
  4725. ///
  4726. /// goto <label>;
  4727. BUILDIN_FUNC(goto)
  4728. {
  4729. if( !data_islabel(script_getdata(st,2)) )
  4730. {
  4731. ShowError("buildin_goto: Not a label\n");
  4732. script_reportdata(script_getdata(st,2));
  4733. st->state = END;
  4734. return SCRIPT_CMD_FAILURE;
  4735. }
  4736. st->pos = script_getnum(st,2);
  4737. st->state = GOTO;
  4738. return SCRIPT_CMD_SUCCESS;
  4739. }
  4740. /*==========================================
  4741. * user-defined function call
  4742. *------------------------------------------*/
  4743. BUILDIN_FUNC(callfunc)
  4744. {
  4745. int32 i, j;
  4746. struct script_retinfo* ri;
  4747. struct script_code* scr;
  4748. const char* str = script_getstr(st,2);
  4749. struct reg_db *ref = nullptr;
  4750. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4751. if(!scr) {
  4752. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4753. st->state = END;
  4754. return SCRIPT_CMD_FAILURE;
  4755. }
  4756. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4757. ref[0].vars = st->stack->scope.vars;
  4758. if (!st->stack->scope.arrays)
  4759. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4760. ref[0].arrays = st->stack->scope.arrays;
  4761. ref[1].vars = st->script->local.vars;
  4762. if (!st->script->local.arrays)
  4763. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4764. ref[1].arrays = st->script->local.arrays;
  4765. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4766. struct script_data* data = push_copy(st->stack,i);
  4767. if (data_isreference(data) && !data->ref) {
  4768. const char* name = reference_getname(data);
  4769. if (name[0] == '.')
  4770. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4771. }
  4772. }
  4773. CREATE(ri, struct script_retinfo, 1);
  4774. ri->script = st->script; // script code
  4775. ri->scope.vars = st->stack->scope.vars; // scope variables
  4776. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4777. ri->pos = st->pos; // script location
  4778. ri->nargs = j; // argument count
  4779. ri->defsp = st->stack->defsp; // default stack pointer
  4780. push_retinfo(st->stack, ri, ref);
  4781. st->pos = 0;
  4782. st->script = scr;
  4783. st->stack->defsp = st->stack->sp;
  4784. st->state = GOTO;
  4785. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4786. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4787. if (!st->script->local.vars)
  4788. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4789. return SCRIPT_CMD_SUCCESS;
  4790. }
  4791. /*==========================================
  4792. * subroutine call
  4793. *------------------------------------------*/
  4794. BUILDIN_FUNC(callsub)
  4795. {
  4796. int32 i,j;
  4797. struct script_retinfo* ri;
  4798. int32 pos = script_getnum(st,2);
  4799. struct reg_db *ref = nullptr;
  4800. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4801. ShowError("buildin_callsub: Argument is not a label\n");
  4802. script_reportdata(script_getdata(st,2));
  4803. st->state = END;
  4804. return SCRIPT_CMD_FAILURE;
  4805. }
  4806. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4807. ref[0].vars = st->stack->scope.vars;
  4808. if (!st->stack->scope.arrays)
  4809. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4810. ref[0].arrays = st->stack->scope.arrays;
  4811. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4812. struct script_data* data = push_copy(st->stack,i);
  4813. if (data_isreference(data) && !data->ref) {
  4814. const char* name = reference_getname(data);
  4815. if (name[0] == '.' && name[1] == '@')
  4816. data->ref = &ref[0];
  4817. }
  4818. }
  4819. CREATE(ri, struct script_retinfo, 1);
  4820. ri->script = st->script; // script code
  4821. ri->scope.vars = st->stack->scope.vars; // scope variables
  4822. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4823. ri->pos = st->pos; // script location
  4824. ri->nargs = j; // argument count
  4825. ri->defsp = st->stack->defsp; // default stack pointer
  4826. push_retinfo(st->stack, ri, ref);
  4827. st->pos = pos;
  4828. st->stack->defsp = st->stack->sp;
  4829. st->state = GOTO;
  4830. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4831. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4832. return SCRIPT_CMD_SUCCESS;
  4833. }
  4834. /// Retrieves an argument provided to callfunc/callsub.
  4835. /// If the argument doesn't exist
  4836. ///
  4837. /// getarg(<index>{,<default_value>}) -> <value>
  4838. BUILDIN_FUNC(getarg)
  4839. {
  4840. struct script_retinfo* ri;
  4841. int32 idx;
  4842. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4843. {
  4844. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4845. st->state = END;
  4846. return SCRIPT_CMD_FAILURE;
  4847. }
  4848. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4849. idx = script_getnum(st,2);
  4850. if( idx >= 0 && idx < ri->nargs )
  4851. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4852. else if( script_hasdata(st,3) )
  4853. script_pushcopy(st, 3);
  4854. else
  4855. {
  4856. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4857. st->state = END;
  4858. return SCRIPT_CMD_FAILURE;
  4859. }
  4860. return SCRIPT_CMD_SUCCESS;
  4861. }
  4862. /// Returns from the current function, optionaly returning a value from the functions.
  4863. /// Don't use outside script functions.
  4864. ///
  4865. /// return;
  4866. /// return <value>;
  4867. BUILDIN_FUNC(return)
  4868. {
  4869. if( script_hasdata(st,2) )
  4870. {// return value
  4871. struct script_data* data;
  4872. script_pushcopy(st, 2);
  4873. data = script_getdatatop(st, -1);
  4874. if( data_isreference(data) ) {
  4875. const char* name = reference_getname(data);
  4876. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4877. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4878. get_val(st, data); // current scope, convert to value
  4879. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4880. data->ref = nullptr; // Reference to the parent scope, remove reference pointer
  4881. }
  4882. }
  4883. }
  4884. else
  4885. {// no return value
  4886. script_pushnil(st);
  4887. }
  4888. st->state = RETFUNC;
  4889. return SCRIPT_CMD_SUCCESS;
  4890. }
  4891. /// Returns a random number.
  4892. /// rand(<range>) -> <int64> in the mathematical range [0, <range - 1>]
  4893. /// rand(<min>,<max>) -> <int64> in the mathematical range [<min>, <max>]
  4894. BUILDIN_FUNC(rand)
  4895. {
  4896. int64 minimum;
  4897. int64 maximum;
  4898. // min,max
  4899. if( script_hasdata( st, 3 ) ){
  4900. minimum = script_getnum64( st, 2 );
  4901. maximum = script_getnum64( st, 3 );
  4902. if( minimum > maximum ){
  4903. ShowWarning( "buildin_rand: minimum (%" PRId64 ") is bigger than maximum (%" PRId64 ").\n", minimum, maximum );
  4904. // rnd_value already fixes this by swapping minimum and maximum automatically
  4905. }
  4906. // range
  4907. }else{
  4908. minimum = 0;
  4909. maximum = script_getnum64( st, 2 );
  4910. if( maximum < 0 ){
  4911. ShowError( "buildin_rand: range (%" PRId64 ") is negative.\n", maximum );
  4912. st->state = END;
  4913. return SCRIPT_CMD_FAILURE;
  4914. }
  4915. // The range version is exclusive maximum
  4916. maximum -= 1;
  4917. if( maximum < 1 ){
  4918. ShowError( "buildin_rand: range (%" PRId64 ") is too small. No randomness possible.\n", maximum );
  4919. st->state = END;
  4920. return SCRIPT_CMD_FAILURE;
  4921. }
  4922. }
  4923. if( minimum == maximum ){
  4924. ShowError( "buildin_rand: minimum (%" PRId64 ") and maximum (%" PRId64 ") are equal. No randomness possible.\n", minimum, maximum );
  4925. st->state = END;
  4926. return SCRIPT_CMD_FAILURE;
  4927. }
  4928. script_pushint64( st, rnd_value( minimum, maximum ) );
  4929. return SCRIPT_CMD_SUCCESS;
  4930. }
  4931. /*==========================================
  4932. * Warp sd to str,x,y or Random or SavePoint/Save
  4933. *------------------------------------------*/
  4934. BUILDIN_FUNC(warp)
  4935. {
  4936. int32 ret;
  4937. int32 x,y;
  4938. const char* str;
  4939. map_session_data* sd;
  4940. if(!script_charid2sd(5, sd))
  4941. return SCRIPT_CMD_SUCCESS;
  4942. str = script_getstr(st,2);
  4943. x = script_getnum(st,3);
  4944. y = script_getnum(st,4);
  4945. if(strcmp(str,"Random")==0)
  4946. ret = pc_randomwarp(sd,CLR_TELEPORT,true);
  4947. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4948. ret = pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  4949. else
  4950. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4951. if( ret ) {
  4952. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4953. return SCRIPT_CMD_FAILURE;
  4954. }
  4955. return SCRIPT_CMD_SUCCESS;
  4956. }
  4957. /*==========================================
  4958. * Warp a specified area
  4959. *------------------------------------------*/
  4960. static int32 buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4961. {
  4962. int32 x2,y2,x3,y3;
  4963. uint32 index = va_arg(ap,uint32);
  4964. x2 = va_arg(ap,int);
  4965. y2 = va_arg(ap,int);
  4966. x3 = va_arg(ap,int);
  4967. y3 = va_arg(ap,int);
  4968. if(index == 0)
  4969. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT,true);
  4970. else if(x3 && y3) {
  4971. int32 max, tx, ty, j = 0;
  4972. int16 m;
  4973. m = map_mapindex2mapid(index);
  4974. // choose a suitable max number of attempts
  4975. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4976. max = 1000;
  4977. // find a suitable map cell
  4978. do {
  4979. tx = rnd_value(x2, x3);
  4980. ty = rnd_value(y2, y3);
  4981. j++;
  4982. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4983. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4984. }
  4985. else
  4986. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4987. return 0;
  4988. }
  4989. BUILDIN_FUNC(areawarp)
  4990. {
  4991. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4992. uint32 index;
  4993. const char *str;
  4994. const char *mapname;
  4995. mapname = script_getstr(st,2);
  4996. x0 = script_getnum(st,3);
  4997. y0 = script_getnum(st,4);
  4998. x1 = script_getnum(st,5);
  4999. y1 = script_getnum(st,6);
  5000. str = script_getstr(st,7);
  5001. x2 = script_getnum(st,8);
  5002. y2 = script_getnum(st,9);
  5003. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  5004. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  5005. x3 = 0;
  5006. y3 = 0;
  5007. } else if( x3 && y3 ) {
  5008. // normalize x3/y3 coordinates
  5009. if( x3 < x2 ) std::swap(x3,x2);
  5010. if( y3 < y2 ) std::swap(y3,y2);
  5011. }
  5012. }
  5013. if( (m = map_mapname2mapid(mapname)) < 0 )
  5014. return SCRIPT_CMD_FAILURE;
  5015. if( strcmp(str,"Random") == 0 )
  5016. index = 0;
  5017. else if( !(index=mapindex_name2id(str)) )
  5018. return SCRIPT_CMD_FAILURE;
  5019. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  5020. return SCRIPT_CMD_SUCCESS;
  5021. }
  5022. /*==========================================
  5023. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  5024. *------------------------------------------*/
  5025. static int32 buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  5026. {
  5027. int32 hp, sp;
  5028. hp = va_arg(ap, int);
  5029. sp = va_arg(ap, int);
  5030. pc_percentheal((TBL_PC *)bl,hp,sp);
  5031. return 0;
  5032. }
  5033. BUILDIN_FUNC(areapercentheal)
  5034. {
  5035. int32 hp,sp,m;
  5036. const char *mapname;
  5037. int32 x0,y0,x1,y1;
  5038. mapname=script_getstr(st,2);
  5039. x0=script_getnum(st,3);
  5040. y0=script_getnum(st,4);
  5041. x1=script_getnum(st,5);
  5042. y1=script_getnum(st,6);
  5043. hp=script_getnum(st,7);
  5044. sp=script_getnum(st,8);
  5045. if( (m=map_mapname2mapid(mapname))< 0)
  5046. return SCRIPT_CMD_FAILURE;
  5047. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5048. return SCRIPT_CMD_SUCCESS;
  5049. }
  5050. enum e_warpparty_target{
  5051. WARPPARTY_RANDOM = 0,
  5052. WARPPARTY_SAVEPOINTALL,
  5053. WARPPARTY_SAVEPOINT,
  5054. WARPPARTY_LEADER,
  5055. WARPPARTY_RANDOMALL,
  5056. WARPPARTY_RANDOMALLAREA
  5057. };
  5058. /*==========================================
  5059. * Warpparty - [Fredzilla] [Paradox924X]
  5060. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5061. * If 'from_mapname' is specified, only the party members on that map will be warped
  5062. *------------------------------------------*/
  5063. BUILDIN_FUNC(warpparty)
  5064. {
  5065. TBL_PC *sd = nullptr;
  5066. TBL_PC *pl_sd;
  5067. struct party_data* p;
  5068. int32 mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5069. const char* str = script_getstr(st,2);
  5070. int32 x = script_getnum(st,3);
  5071. int32 y = script_getnum(st,4);
  5072. int32 p_id = script_getnum(st,5);
  5073. const char* str2 = nullptr;
  5074. if ( script_hasdata(st,6) )
  5075. str2 = script_getstr(st,6);
  5076. if (script_hasdata(st, 7))
  5077. rx = script_getnum(st, 7);
  5078. if (script_hasdata(st, 8))
  5079. ry = script_getnum(st, 8);
  5080. p = party_search(p_id);
  5081. if(!p)
  5082. return SCRIPT_CMD_SUCCESS;
  5083. enum e_warpparty_target type = ( strcmp(str,"Random")==0 ) ? WARPPARTY_RANDOM
  5084. : ( strcmp(str,"SavePointAll")==0 ) ? WARPPARTY_SAVEPOINTALL
  5085. : ( strcmp(str,"SavePoint")==0 ) ? WARPPARTY_SAVEPOINT
  5086. : ( strcmp(str,"Leader")==0 ) ? WARPPARTY_LEADER
  5087. : ( strcmp(str,"RandomAll")==0 ) ? WARPPARTY_RANDOMALL
  5088. : WARPPARTY_RANDOMALLAREA;
  5089. switch (type)
  5090. {
  5091. case WARPPARTY_SAVEPOINT:
  5092. //"SavePoint" uses save point of the currently attached player
  5093. if ( !script_rid2sd(sd) )
  5094. return SCRIPT_CMD_FAILURE;
  5095. break;
  5096. case WARPPARTY_LEADER:
  5097. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5098. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5099. return SCRIPT_CMD_FAILURE;
  5100. pl_sd = p->data[i].sd;
  5101. mapindex = pl_sd->mapindex;
  5102. m = map_mapindex2mapid(mapindex);
  5103. x = pl_sd->bl.x;
  5104. y = pl_sd->bl.y;
  5105. break;
  5106. case WARPPARTY_RANDOMALL: {
  5107. if ( !script_rid2sd(sd) )
  5108. return SCRIPT_CMD_FAILURE;
  5109. mapindex = sd->mapindex;
  5110. m = map_mapindex2mapid(mapindex);
  5111. struct map_data *mapdata = map_getmapdata(m);
  5112. if ( mapdata == nullptr || mapdata->getMapFlag(MF_NOWARP) || mapdata->getMapFlag(MF_NOTELEPORT) )
  5113. return SCRIPT_CMD_FAILURE;
  5114. i = 0;
  5115. do {
  5116. x = rnd_value(1, mapdata->xs - 1);
  5117. y = rnd_value(1, mapdata->ys - 1);
  5118. } while ((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  5119. if (i >= 1000) {
  5120. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  5121. return SCRIPT_CMD_FAILURE;
  5122. }
  5123. } break;
  5124. case WARPPARTY_RANDOMALLAREA:
  5125. mapindex = mapindex_name2id(str);
  5126. if (!mapindex) {// Invalid map
  5127. return SCRIPT_CMD_FAILURE;
  5128. }
  5129. m = map_mapindex2mapid(mapindex);
  5130. break;
  5131. }
  5132. for (i = 0; i < MAX_PARTY; i++)
  5133. {
  5134. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5135. continue;
  5136. map_data* mapdata = map_getmapdata(pl_sd->bl.m);
  5137. if( str2 && strcmp(str2, mapdata->name) != 0 )
  5138. continue;
  5139. if( pc_isdead(pl_sd) )
  5140. continue;
  5141. e_setpos ret = SETPOS_OK;
  5142. switch( type )
  5143. {
  5144. case WARPPARTY_RANDOM:
  5145. if (!mapdata->getMapFlag(MF_NOWARP))
  5146. ret = (e_setpos)pc_randomwarp(pl_sd,CLR_TELEPORT);
  5147. break;
  5148. case WARPPARTY_SAVEPOINTALL:
  5149. if (!mapdata->getMapFlag(MF_NORETURN))
  5150. ret = pc_setpos( pl_sd, mapindex_name2id( pl_sd->status.save_point.map ), pl_sd->status.save_point.x, pl_sd->status.save_point.y, CLR_TELEPORT );
  5151. break;
  5152. case WARPPARTY_SAVEPOINT:
  5153. if (!mapdata->getMapFlag(MF_NORETURN))
  5154. ret = pc_setpos( pl_sd, mapindex_name2id( sd->status.save_point.map ),sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  5155. break;
  5156. case WARPPARTY_LEADER:
  5157. if (p->party.member[i].leader)
  5158. continue;
  5159. [[fallthrough]];
  5160. case WARPPARTY_RANDOMALL:
  5161. if (pl_sd == sd) {
  5162. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5163. break;
  5164. }
  5165. [[fallthrough]];
  5166. case WARPPARTY_RANDOMALLAREA:
  5167. if(!mapdata->getMapFlag(MF_NORETURN) && !mapdata->getMapFlag(MF_NOWARP) && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pc_get_group_level(pl_sd))){
  5168. if (rx || ry) {
  5169. int32 x1 = x + rx, y1 = y + ry,
  5170. x0 = x - rx, y0 = y - ry,
  5171. nx, ny;
  5172. uint8 attempts = 10;
  5173. do {
  5174. nx = x0 + rnd_value(x0, x1);
  5175. ny = y0 + rnd_value(y0, y1);
  5176. } while ((--attempts) > 0 && !map_getcell(m, nx, ny, CELL_CHKPASS));
  5177. if (attempts != 0) { //Keep the original coordinates if fails to find a valid cell within the range
  5178. x = nx;
  5179. y = ny;
  5180. }
  5181. }
  5182. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5183. }
  5184. break;
  5185. }
  5186. if( ret != SETPOS_OK ) {
  5187. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", pl_sd->status.name, str, x, y);
  5188. if ( ( type == WARPPARTY_RANDOMALL || type == WARPPARTY_RANDOMALLAREA ) && (rx || ry) )
  5189. continue;
  5190. else
  5191. return SCRIPT_CMD_FAILURE;
  5192. }
  5193. }
  5194. return SCRIPT_CMD_SUCCESS;
  5195. }
  5196. /*==========================================
  5197. * Warpguild - [Fredzilla]
  5198. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5199. *------------------------------------------*/
  5200. BUILDIN_FUNC(warpguild)
  5201. {
  5202. TBL_PC *sd = nullptr;
  5203. TBL_PC *pl_sd;
  5204. struct s_mapiterator* iter;
  5205. int32 type, mapindex = 0, m = -1;
  5206. const char* str = script_getstr(st,2);
  5207. int32 x = script_getnum(st,3);
  5208. int32 y = script_getnum(st,4);
  5209. int32 gid = script_getnum(st,5);
  5210. auto g = guild_search(gid);
  5211. if( g == nullptr )
  5212. return SCRIPT_CMD_SUCCESS;
  5213. type = ( strcmp(str,"Random")==0 ) ? 0
  5214. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5215. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5216. : 3;
  5217. if( type == 2 && !script_rid2sd(sd) )
  5218. {// "SavePoint" uses save point of the currently attached player
  5219. return SCRIPT_CMD_SUCCESS;
  5220. }
  5221. switch (type) {
  5222. case 3:
  5223. mapindex = mapindex_name2id(str);
  5224. if (!mapindex)
  5225. return SCRIPT_CMD_FAILURE;
  5226. m = map_mapindex2mapid(mapindex);
  5227. break;
  5228. }
  5229. iter = mapit_getallusers();
  5230. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5231. {
  5232. if( pl_sd->status.guild_id != gid )
  5233. continue;
  5234. switch( type )
  5235. {
  5236. case 0: // Random
  5237. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5238. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5239. break;
  5240. case 1: // SavePointAll
  5241. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5242. pc_setpos( pl_sd, mapindex_name2id( pl_sd->status.save_point.map ), pl_sd->status.save_point.x, pl_sd->status.save_point.y, CLR_TELEPORT );
  5243. break;
  5244. case 2: // SavePoint
  5245. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5246. pc_setpos( pl_sd, mapindex_name2id( sd->status.save_point.map ),sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  5247. break;
  5248. case 3: // m,x,y
  5249. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN) && !map_getmapflag(pl_sd->bl.m, MF_NOWARP) && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pc_get_group_level(pl_sd)))
  5250. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5251. break;
  5252. }
  5253. }
  5254. mapit_free(iter);
  5255. return SCRIPT_CMD_SUCCESS;
  5256. }
  5257. /*==========================================
  5258. * Force Heal a player (hp and sp)
  5259. *------------------------------------------*/
  5260. BUILDIN_FUNC(heal)
  5261. {
  5262. TBL_PC *sd;
  5263. int32 hp,sp;
  5264. if (!script_charid2sd(4,sd))
  5265. return SCRIPT_CMD_SUCCESS;
  5266. hp=script_getnum(st,2);
  5267. sp=script_getnum(st,3);
  5268. status_heal(&sd->bl, hp, sp, 1);
  5269. return SCRIPT_CMD_SUCCESS;
  5270. }
  5271. /*==========================================
  5272. * Force Heal a player (ap)
  5273. *------------------------------------------*/
  5274. BUILDIN_FUNC(healap)
  5275. {
  5276. map_session_data* sd;
  5277. if (!script_charid2sd(3, sd))
  5278. return SCRIPT_CMD_FAILURE;
  5279. status_heal(&sd->bl, 0, 0, script_getnum(st, 2), 1);
  5280. return SCRIPT_CMD_SUCCESS;
  5281. }
  5282. /*==========================================
  5283. * Heal a player by item (get vit bonus etc)
  5284. *------------------------------------------*/
  5285. BUILDIN_FUNC(itemheal)
  5286. {
  5287. TBL_PC *sd;
  5288. int32 hp,sp;
  5289. hp=script_getnum(st,2);
  5290. sp=script_getnum(st,3);
  5291. if(potion_flag==1) {
  5292. potion_hp = hp;
  5293. potion_sp = sp;
  5294. return SCRIPT_CMD_SUCCESS;
  5295. }
  5296. if (!script_charid2sd(4,sd))
  5297. return SCRIPT_CMD_SUCCESS;
  5298. pc_itemheal(sd,sd->itemid,hp,sp);
  5299. return SCRIPT_CMD_SUCCESS;
  5300. }
  5301. /*==========================================
  5302. *
  5303. *------------------------------------------*/
  5304. BUILDIN_FUNC(percentheal)
  5305. {
  5306. int32 hp,sp;
  5307. TBL_PC* sd;
  5308. hp=script_getnum(st,2);
  5309. sp=script_getnum(st,3);
  5310. if(potion_flag==1) {
  5311. potion_per_hp = hp;
  5312. potion_per_sp = sp;
  5313. return SCRIPT_CMD_SUCCESS;
  5314. }
  5315. if (!script_charid2sd(4,sd))
  5316. return SCRIPT_CMD_SUCCESS;
  5317. #ifdef RENEWAL
  5318. if( sd->sc.getSCE(SC_EXTREMITYFIST) )
  5319. sp = 0;
  5320. #endif
  5321. if (sd->sc.getSCE(SC_NORECOVER_STATE)) {
  5322. hp = 0;
  5323. sp = 0;
  5324. }
  5325. if (sd->sc.getSCE(SC_BITESCAR))
  5326. hp = 0;
  5327. pc_percentheal(sd,hp,sp);
  5328. return SCRIPT_CMD_SUCCESS;
  5329. }
  5330. /*==========================================
  5331. *
  5332. *------------------------------------------*/
  5333. BUILDIN_FUNC(jobchange)
  5334. {
  5335. int32 job, upper=-1;
  5336. job=script_getnum(st,2);
  5337. if( script_hasdata(st,3) )
  5338. upper=script_getnum(st,3);
  5339. if (pcdb_checkid(job))
  5340. {
  5341. TBL_PC* sd;
  5342. if (!script_charid2sd(4,sd))
  5343. return SCRIPT_CMD_SUCCESS;
  5344. pc_jobchange(sd, job, upper);
  5345. }
  5346. return SCRIPT_CMD_SUCCESS;
  5347. }
  5348. /*==========================================
  5349. *
  5350. *------------------------------------------*/
  5351. BUILDIN_FUNC(jobname)
  5352. {
  5353. int32 class_=script_getnum(st,2);
  5354. script_pushconststr(st, (char*)job_name(class_));
  5355. return SCRIPT_CMD_SUCCESS;
  5356. }
  5357. /// Get input from the player.
  5358. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5359. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5360. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5361. /// shorter than 'min' and 0 otherwise.
  5362. ///
  5363. /// input(<var>{,<min>{,<max>}}) -> <int>
  5364. BUILDIN_FUNC(input)
  5365. {
  5366. TBL_PC* sd;
  5367. struct script_data* data;
  5368. int64 uid;
  5369. const char* name;
  5370. int32 min;
  5371. int32 max;
  5372. if( !script_rid2sd(sd) )
  5373. return SCRIPT_CMD_SUCCESS;
  5374. data = script_getdata(st,2);
  5375. if( !data_isreference(data) ){
  5376. ShowError("script:input: not a variable\n");
  5377. script_reportdata(data);
  5378. st->state = END;
  5379. return SCRIPT_CMD_FAILURE;
  5380. }
  5381. uid = reference_getuid(data);
  5382. name = reference_getname(data);
  5383. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5384. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5385. #ifdef SECURE_NPCTIMEOUT
  5386. sd->npc_idle_type = NPCT_WAIT;
  5387. #endif
  5388. if( !sd->state.menu_or_input )
  5389. { // first invocation, display npc input box
  5390. sd->state.menu_or_input = 1;
  5391. st->state = RERUNLINE;
  5392. if( is_string_variable(name) )
  5393. clif_scriptinputstr( *sd, st->oid );
  5394. else
  5395. clif_scriptinput( *sd, st->oid );
  5396. }
  5397. else
  5398. { // take received text/value and store it in the designated variable
  5399. sd->state.menu_or_input = 0;
  5400. if( is_string_variable(name) )
  5401. {
  5402. int32 len = (int)strlen(sd->npc_str);
  5403. set_reg_str(st, sd, uid, name, sd->npc_str, script_getref(st,2));
  5404. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5405. }
  5406. else
  5407. {
  5408. int32 amount = sd->npc_amount;
  5409. set_reg_num(st, sd, uid, name, cap_value(amount,min,max), script_getref(st,2));
  5410. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5411. }
  5412. st->state = RUN;
  5413. }
  5414. return SCRIPT_CMD_SUCCESS;
  5415. }
  5416. // declare the copyarray method here for future reference
  5417. BUILDIN_FUNC(copyarray);
  5418. /// Sets the value of a variable.
  5419. /// The value is converted to the type of the variable.
  5420. ///
  5421. /// set(<variable>,<value>{,<charid>})
  5422. BUILDIN_FUNC(setr)
  5423. {
  5424. TBL_PC* sd = nullptr;
  5425. struct script_data* data;
  5426. //struct script_data* datavalue;
  5427. int64 num;
  5428. uint8 pos = 4;
  5429. const char* name, *command = script_getfuncname(st);
  5430. char prefix;
  5431. data = script_getdata(st,2);
  5432. //datavalue = script_getdata(st,3);
  5433. if( !data_isreference(data) )
  5434. {
  5435. ShowError("script:set: not a variable\n");
  5436. script_reportdata(script_getdata(st,2));
  5437. st->state = END;
  5438. return SCRIPT_CMD_FAILURE;
  5439. }
  5440. num = reference_getuid(data);
  5441. name = reference_getname(data);
  5442. prefix = *name;
  5443. if (!strcmp(command, "setr"))
  5444. pos = 5;
  5445. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5446. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5447. return SCRIPT_CMD_FAILURE;
  5448. }
  5449. #if 0
  5450. if( data_isreference(datavalue) )
  5451. {// the value being referenced is a variable
  5452. const char* namevalue = reference_getname(datavalue);
  5453. if( !not_array_variable(*namevalue) )
  5454. {// array variable being copied into another array variable
  5455. if( sd == nullptr && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5456. {// player must be attached in order to copy a player variable
  5457. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5458. return SCRIPT_CMD_SUCCESS;
  5459. }
  5460. if( is_string_variable(namevalue) != is_string_variable(name) )
  5461. {// non-matching array value types
  5462. ShowWarning("script:set: two array variables do not match in type.\n");
  5463. return SCRIPT_CMD_SUCCESS;
  5464. }
  5465. // push the maximum number of array values to the stack
  5466. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5467. // call the copy array method directly
  5468. return buildin_copyarray(st);
  5469. }
  5470. }
  5471. #endif
  5472. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5473. if( is_string_variable(name) )
  5474. script_pushstrcopy(st,script_getstr(st, 4));
  5475. else
  5476. script_pushint(st,script_getnum64(st, 4));
  5477. } else // Return a copy of the variable reference
  5478. script_pushcopy(st, 2);
  5479. if( is_string_variable(name) )
  5480. set_reg_str( st, sd, num, name, script_getstr( st, 3 ), script_getref( st, 2 ) );
  5481. else
  5482. set_reg_num( st, sd, num, name, script_getnum64( st, 3 ), script_getref( st, 2 ) );
  5483. return SCRIPT_CMD_SUCCESS;
  5484. }
  5485. /////////////////////////////////////////////////////////////////////
  5486. /// Array variables
  5487. ///
  5488. /// Sets values of an array, from the starting index.
  5489. /// ex: setarray arr[1],1,2,3;
  5490. ///
  5491. /// setarray <array variable>,<value1>{,<value2>...};
  5492. BUILDIN_FUNC(setarray)
  5493. {
  5494. struct script_data* data;
  5495. const char* name;
  5496. uint32 start;
  5497. uint32 end;
  5498. int32 id;
  5499. int32 i;
  5500. TBL_PC* sd = nullptr;
  5501. data = script_getdata(st, 2);
  5502. if( !data_isreference(data) )
  5503. {
  5504. ShowError("script:setarray: not a variable\n");
  5505. script_reportdata(data);
  5506. st->state = END;
  5507. return SCRIPT_CMD_FAILURE;// not a variable
  5508. }
  5509. id = reference_getid(data);
  5510. start = reference_getindex(data);
  5511. name = reference_getname(data);
  5512. if( not_server_variable(*name) )
  5513. {
  5514. if( !script_rid2sd(sd) )
  5515. return SCRIPT_CMD_SUCCESS;// no player attached
  5516. }
  5517. end = start + script_lastdata(st) - 2;
  5518. if( end > SCRIPT_MAX_ARRAYSIZE )
  5519. end = SCRIPT_MAX_ARRAYSIZE;
  5520. if( is_string_variable(name) )
  5521. {// string array
  5522. for( i = 3; start < end; ++start, ++i )
  5523. set_reg_str( st, sd, reference_uid( id, start ), name, script_getstr( st, i ), reference_getref( data ) );
  5524. }
  5525. else
  5526. {// int32 array
  5527. for( i = 3; start < end; ++start, ++i )
  5528. set_reg_num( st, sd, reference_uid( id, start ), name, script_getnum64( st, i ), reference_getref( data ) );
  5529. }
  5530. return SCRIPT_CMD_SUCCESS;
  5531. }
  5532. /// Sets count values of an array, from the starting index.
  5533. /// ex: cleararray arr[0],0,1;
  5534. ///
  5535. /// cleararray <array variable>,<value>,<count>;
  5536. BUILDIN_FUNC(cleararray)
  5537. {
  5538. struct script_data* data;
  5539. const char* name;
  5540. uint32 start;
  5541. uint32 end;
  5542. int32 id;
  5543. TBL_PC* sd = nullptr;
  5544. data = script_getdata(st, 2);
  5545. if( !data_isreference(data) )
  5546. {
  5547. ShowError("script:cleararray: not a variable\n");
  5548. script_reportdata(data);
  5549. st->state = END;
  5550. return SCRIPT_CMD_FAILURE;// not a variable
  5551. }
  5552. id = reference_getid(data);
  5553. start = reference_getindex(data);
  5554. name = reference_getname(data);
  5555. if( not_server_variable(*name) )
  5556. {
  5557. if( !script_rid2sd(sd) )
  5558. return SCRIPT_CMD_SUCCESS;// no player attached
  5559. }
  5560. end = start + script_getnum(st, 4);
  5561. if( end > SCRIPT_MAX_ARRAYSIZE )
  5562. end = SCRIPT_MAX_ARRAYSIZE;
  5563. if( is_string_variable( name ) ){
  5564. const char* value = script_getstr( st, 3 );
  5565. for( ; start < end; ++start ){
  5566. set_reg_str( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5567. }
  5568. }else{
  5569. int64 value = script_getnum64( st, 3 );
  5570. for( ; start < end; ++start ){
  5571. set_reg_num( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5572. }
  5573. }
  5574. return SCRIPT_CMD_SUCCESS;
  5575. }
  5576. /// Copies data from one array to another.
  5577. /// ex: copyarray arr[0],arr[2],2;
  5578. ///
  5579. /// copyarray <destination array variable>,<source array variable>,<count>;
  5580. BUILDIN_FUNC(copyarray)
  5581. {
  5582. struct script_data* data1;
  5583. struct script_data* data2;
  5584. const char* name1;
  5585. const char* name2;
  5586. int32 idx1;
  5587. int32 idx2;
  5588. int32 id1;
  5589. int32 id2;
  5590. int32 i;
  5591. uint32 count;
  5592. TBL_PC* sd = nullptr;
  5593. data1 = script_getdata(st, 2);
  5594. data2 = script_getdata(st, 3);
  5595. if( !data_isreference(data1) || !data_isreference(data2) )
  5596. {
  5597. ShowError("script:copyarray: not a variable\n");
  5598. script_reportdata(data1);
  5599. script_reportdata(data2);
  5600. st->state = END;
  5601. return SCRIPT_CMD_FAILURE;// not a variable
  5602. }
  5603. id1 = reference_getid(data1);
  5604. id2 = reference_getid(data2);
  5605. idx1 = reference_getindex(data1);
  5606. idx2 = reference_getindex(data2);
  5607. name1 = reference_getname(data1);
  5608. name2 = reference_getname(data2);
  5609. bool is_string = is_string_variable( name1 );
  5610. if( is_string != is_string_variable( name2 ) ){
  5611. ShowError("script:copyarray: type mismatch\n");
  5612. script_reportdata(data1);
  5613. script_reportdata(data2);
  5614. st->state = END;
  5615. return SCRIPT_CMD_FAILURE;// data type mismatch
  5616. }
  5617. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5618. {
  5619. if( !script_rid2sd(sd) )
  5620. return SCRIPT_CMD_SUCCESS;// no player attached
  5621. }
  5622. count = script_getnum(st, 4);
  5623. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5624. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5625. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5626. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5627. if( id1 == id2 && idx1 > idx2 ){
  5628. // destination might be overlapping the source - copy in reverse order
  5629. for( i = count - 1; i >= 0; --i ){
  5630. if( is_string ){
  5631. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5632. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5633. // Remove stack entry from get_val2_str
  5634. script_removetop( st, -1, 0 );
  5635. }else{
  5636. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5637. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5638. }
  5639. }
  5640. }else{
  5641. // normal copy
  5642. for( i = 0; i < count; ++i ){
  5643. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ){
  5644. if( is_string ){
  5645. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5646. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5647. // Remove stack entry from get_val2_str
  5648. script_removetop( st, -1, 0 );
  5649. }else{
  5650. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5651. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5652. }
  5653. }else{
  5654. // out of range - assume ""/0
  5655. clear_reg( st, sd, reference_uid( id1, idx1 + i ), name1, reference_getref( data1 ) );
  5656. }
  5657. }
  5658. }
  5659. return SCRIPT_CMD_SUCCESS;
  5660. }
  5661. /// Returns the size of the array.
  5662. /// Assumes that everything before the starting index exists.
  5663. /// ex: getarraysize(arr[3])
  5664. ///
  5665. /// getarraysize(<array variable>) -> <int>
  5666. BUILDIN_FUNC(getarraysize)
  5667. {
  5668. struct script_data* data;
  5669. const char* name;
  5670. map_session_data* sd = nullptr;
  5671. data = script_getdata(st, 2);
  5672. if( !data_isreference(data) )
  5673. {
  5674. ShowError("script:getarraysize: not a variable\n");
  5675. script_reportdata(data);
  5676. script_pushnil(st);
  5677. st->state = END;
  5678. return SCRIPT_CMD_FAILURE;// not a variable
  5679. }
  5680. name = reference_getname(data);
  5681. if( not_server_variable(*name) ){
  5682. if (!script_rid2sd(sd))
  5683. return SCRIPT_CMD_SUCCESS;// no player attached
  5684. }
  5685. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5686. return SCRIPT_CMD_SUCCESS;
  5687. }
  5688. int32 script_array_index_cmp(const void *a, const void *b) {
  5689. return ( *(uint32*)a - *(uint32*)b );
  5690. }
  5691. /// Deletes count or all the elements in an array, from the starting index.
  5692. /// ex: deletearray arr[4],2;
  5693. ///
  5694. /// deletearray <array variable>;
  5695. /// deletearray <array variable>,<count>;
  5696. BUILDIN_FUNC(deletearray)
  5697. {
  5698. struct script_data* data;
  5699. const char* name;
  5700. uint32 start, end, i;
  5701. int32 id;
  5702. TBL_PC *sd = nullptr;
  5703. struct script_array *sa = nullptr;
  5704. struct reg_db *src = nullptr;
  5705. data = script_getdata(st, 2);
  5706. if( !data_isreference(data) ) {
  5707. ShowError("script:deletearray: not a variable\n");
  5708. script_reportdata(data);
  5709. st->state = END;
  5710. return SCRIPT_CMD_FAILURE;// not a variable
  5711. }
  5712. id = reference_getid(data);
  5713. start = reference_getindex(data);
  5714. name = reference_getname(data);
  5715. if( not_server_variable(*name) ) {
  5716. if( !script_rid2sd(sd) )
  5717. return SCRIPT_CMD_SUCCESS;// no player attached
  5718. }
  5719. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5720. ShowError("script:deletearray: not a array\n");
  5721. script_reportdata(data);
  5722. st->state = END;
  5723. return SCRIPT_CMD_FAILURE;// not a variable
  5724. }
  5725. script_array_ensure_zero(st, nullptr, data->u.num, reference_getref(data));
  5726. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5727. return SCRIPT_CMD_SUCCESS;// not a variable
  5728. }
  5729. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5730. if( start >= end )
  5731. return SCRIPT_CMD_SUCCESS;// nothing to free
  5732. if( script_hasdata(st,3) ) {
  5733. uint32 count = script_getnum(st, 3);
  5734. if( count > end - start )
  5735. count = end - start;
  5736. if( count <= 0 )
  5737. return SCRIPT_CMD_SUCCESS;// nothing to free
  5738. bool is_string = is_string_variable( name );
  5739. if( end - start < sa->size ) {
  5740. // Better to iterate directly on the array, no speed-up from using sa
  5741. for( ; start + count < end; ++start ) {
  5742. // Compact and overwrite
  5743. if( is_string ){
  5744. const char* value = get_val2_str( st, reference_uid( id, start + count ), reference_getref( data ) );
  5745. set_reg_str( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5746. // Remove stack entry from get_val2_str
  5747. script_removetop( st, -1, 0 );
  5748. }else{
  5749. int64 value = get_val2_num( st, reference_uid( id, start + count ), reference_getref( data ) );
  5750. set_reg_num( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5751. }
  5752. }
  5753. for( ; start < end; start++ ) {
  5754. // Clean up any leftovers that weren't overwritten
  5755. clear_reg( st, sd, reference_uid( id, start ), name, reference_getref( data ) );
  5756. }
  5757. } else {
  5758. // using sa to speed up
  5759. uint32 *list = nullptr, size = 0;
  5760. list = script_array_cpy_list(sa);
  5761. size = sa->size;
  5762. qsort(list, size, sizeof(uint32), script_array_index_cmp);
  5763. ARR_FIND(0, size, i, list[i] >= start);
  5764. for( ; i < size && list[i] < start + count; i++ ) {
  5765. // Clear any entries between start and start+count, if they exist
  5766. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5767. }
  5768. for( ; i < size && list[i] < end; i++ ) {
  5769. // Move back count positions any entries between start+count to fill the gaps
  5770. if( is_string ){
  5771. const char* value = get_val2_str( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5772. set_reg_str( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5773. // Remove stack entry from get_val2_str
  5774. script_removetop( st, -1, 0 );
  5775. }else{
  5776. int64 value = get_val2_num( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5777. set_reg_num( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5778. }
  5779. // Clear their originals
  5780. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5781. }
  5782. }
  5783. } else {
  5784. uint32 *list = nullptr, size = 0;
  5785. list = script_array_cpy_list(sa);
  5786. size = sa->size;
  5787. for(i = 0; i < size; i++) {
  5788. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5789. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5790. }
  5791. }
  5792. return SCRIPT_CMD_SUCCESS;
  5793. }
  5794. /// Returns a reference to the target index of the array variable.
  5795. /// Equivalent to var[index].
  5796. ///
  5797. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5798. BUILDIN_FUNC(getelementofarray)
  5799. {
  5800. struct script_data* data;
  5801. int32 id;
  5802. int64 i;
  5803. data = script_getdata(st, 2);
  5804. if( !data_isreference(data) )
  5805. {
  5806. ShowError("script:getelementofarray: not a variable\n");
  5807. script_reportdata(data);
  5808. script_pushnil(st);
  5809. st->state = END;
  5810. return SCRIPT_CMD_SUCCESS;// not a variable
  5811. }
  5812. id = reference_getid(data);
  5813. i = script_getnum(st, 3);
  5814. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5815. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5816. script_reportdata(data);
  5817. script_pushnil(st);
  5818. st->state = END;
  5819. return SCRIPT_CMD_FAILURE;// out of range
  5820. }
  5821. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5822. return SCRIPT_CMD_SUCCESS;
  5823. }
  5824. /// Return the index number of the first matching value in an array.
  5825. /// ex: inarray arr,1;
  5826. ///
  5827. /// inarray <array variable>,<value>;
  5828. BUILDIN_FUNC(inarray)
  5829. {
  5830. struct script_data *data;
  5831. const char* name;
  5832. int32 id;
  5833. map_session_data* sd = nullptr;
  5834. struct reg_db *ref = nullptr;
  5835. data = script_getdata(st, 2);
  5836. if (!data_isreference(data))
  5837. {
  5838. ShowError("buildin_inarray: not a variable\n");
  5839. script_reportdata(data);
  5840. st->state = END;
  5841. return SCRIPT_CMD_FAILURE;
  5842. }
  5843. name = reference_getname(data);
  5844. ref = reference_getref(data);
  5845. if (not_server_variable(*name) && !script_rid2sd(sd))
  5846. return SCRIPT_CMD_FAILURE;
  5847. const uint32 array_size = script_array_highest_key(st, sd, name, ref);
  5848. if (array_size == 0)
  5849. {
  5850. script_pushint(st, -1);
  5851. return SCRIPT_CMD_SUCCESS;
  5852. }
  5853. if (array_size >= SCRIPT_MAX_ARRAYSIZE)
  5854. {
  5855. ShowError("buildin_inarray: The array is too large.\n");
  5856. script_reportdata(data);
  5857. st->state = END;
  5858. return SCRIPT_CMD_FAILURE;
  5859. }
  5860. id = reference_getid(data);
  5861. if( is_string_variable( name ) ){
  5862. const char* value = script_getstr( st, 3 );
  5863. for( uint32 i = 0; i < array_size; ++i ){
  5864. const char* temp = get_val2_str( st, reference_uid( id, i ), ref );
  5865. if( !strcmp( temp, value ) ){
  5866. // Remove stack entry from get_val2_str
  5867. script_removetop( st, -1, 0 );
  5868. script_pushint( st, i );
  5869. return SCRIPT_CMD_SUCCESS;
  5870. }
  5871. // Remove stack entry from get_val2_str
  5872. script_removetop( st, -1, 0 );
  5873. }
  5874. }else{
  5875. int64 value = script_getnum64( st, 3 );
  5876. for( uint32 i = 0; i < array_size; ++i ){
  5877. int64 temp = get_val2_num( st, reference_uid( id, i ), ref );
  5878. if( temp == value ){
  5879. script_pushint( st, i );
  5880. return SCRIPT_CMD_SUCCESS;
  5881. }
  5882. }
  5883. }
  5884. script_pushint(st, -1);
  5885. return SCRIPT_CMD_SUCCESS;
  5886. }
  5887. /// Return the number of matches in two arrays.
  5888. /// ex: countinarray arr[0],arr1[0];
  5889. ///
  5890. /// countinarray <array variable>,<array variable>;
  5891. BUILDIN_FUNC(countinarray)
  5892. {
  5893. struct script_data *data1 , *data2;
  5894. const char* name1;
  5895. const char* name2;
  5896. int32 id1, id2, case_count = 0;
  5897. map_session_data* sd = nullptr;
  5898. struct reg_db *ref1 = nullptr, *ref2 = nullptr;
  5899. data1 = script_getdata(st, 2);
  5900. data2 = script_getdata(st, 3);
  5901. if (!data_isreference(data1) || !data_isreference(data2))
  5902. {
  5903. ShowError("buildin_countinarray: not a variable\n");
  5904. script_reportdata(data1);
  5905. script_reportdata(data2);
  5906. st->state = END;
  5907. return SCRIPT_CMD_FAILURE;
  5908. }
  5909. name1 = reference_getname(data1);
  5910. name2 = reference_getname(data2);
  5911. ref1 = reference_getref(data1);
  5912. ref2 = reference_getref(data2);
  5913. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5914. return SCRIPT_CMD_FAILURE;
  5915. const uint32 array_size1 = script_array_highest_key(st, sd, name1, ref1);
  5916. const uint32 array_size2 = script_array_highest_key(st, sd, name2, ref2);
  5917. if (array_size1 == 0 || array_size2 == 0)
  5918. {
  5919. script_pushint(st, 0);
  5920. return SCRIPT_CMD_SUCCESS;
  5921. }
  5922. if (array_size1 >= SCRIPT_MAX_ARRAYSIZE || array_size2 >= SCRIPT_MAX_ARRAYSIZE)
  5923. {
  5924. ShowError("buildin_countinarray: The array is too large.\n");
  5925. script_reportdata(data1);
  5926. script_reportdata(data2);
  5927. st->state = END;
  5928. return SCRIPT_CMD_FAILURE;
  5929. }
  5930. uint32 i = reference_getindex(data1);
  5931. uint32 j = reference_getindex(data2);
  5932. if (i > array_size1 - 1 || j > array_size2 - 1)
  5933. { //To prevent unintended behavior
  5934. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5935. script_reportdata(data1);
  5936. script_reportdata(data2);
  5937. st->state = END;
  5938. return SCRIPT_CMD_FAILURE;
  5939. }
  5940. id1 = reference_getid(data1);
  5941. id2 = reference_getid(data2);
  5942. if( is_string_variable( name1 ) && is_string_variable( name2 ) ){
  5943. for( ; i < array_size1; ++i ){
  5944. const char* temp1 = get_val2_str( st, reference_uid( id1, i ), ref1 );
  5945. for( j = reference_getindex( data2 ); j < array_size2; j++ ){
  5946. const char* temp2 = get_val2_str( st, reference_uid( id2, j ), ref2 );
  5947. if( !strcmp( temp1, temp2 ) ){
  5948. case_count++;
  5949. }
  5950. // Remove stack entry from get_val2_str
  5951. script_removetop( st, -1, 0 );
  5952. }
  5953. // Remove stack entry from get_val2_str
  5954. script_removetop( st, -1, 0 );
  5955. }
  5956. }else if( !is_string_variable( name1 ) && !is_string_variable( name2 ) ){
  5957. for( ; i < array_size1; ++i ){
  5958. int64 temp1 = get_val2_num( st, reference_uid( id1, i ), ref1 );
  5959. for( j = reference_getindex( data2 ); j < array_size2; j++ ){
  5960. int64 temp2 = get_val2_num( st, reference_uid( id2, j ), ref2 );
  5961. if( temp1 == temp2 ){
  5962. case_count++;
  5963. }
  5964. }
  5965. }
  5966. }else{
  5967. ShowError("buildin_countinarray: Arrays do not match , You can't compare an int32 array to a string array.\n");
  5968. script_reportdata(data1);
  5969. script_reportdata(data2);
  5970. st->state = END;
  5971. return SCRIPT_CMD_FAILURE;
  5972. }
  5973. script_pushint(st, case_count);
  5974. return SCRIPT_CMD_SUCCESS;
  5975. }
  5976. /////////////////////////////////////////////////////////////////////
  5977. /// ...
  5978. ///
  5979. /*==========================================
  5980. *
  5981. *------------------------------------------*/
  5982. BUILDIN_FUNC(setlook)
  5983. {
  5984. int32 type,val;
  5985. TBL_PC* sd;
  5986. type = script_getnum(st,2);
  5987. val = script_getnum(st,3);
  5988. if (!script_charid2sd(4,sd))
  5989. return SCRIPT_CMD_SUCCESS;
  5990. pc_changelook(sd,type,val);
  5991. return SCRIPT_CMD_SUCCESS;
  5992. }
  5993. BUILDIN_FUNC(changelook)
  5994. { // As setlook but only client side
  5995. int32 type,val;
  5996. TBL_PC* sd;
  5997. type = script_getnum(st,2);
  5998. val = script_getnum(st,3);
  5999. if (!script_charid2sd(4,sd))
  6000. return SCRIPT_CMD_SUCCESS;
  6001. switch(type) {
  6002. case LOOK_HAIR:
  6003. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  6004. break;
  6005. case LOOK_HAIR_COLOR:
  6006. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  6007. break;
  6008. case LOOK_CLOTHES_COLOR:
  6009. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  6010. break;
  6011. case LOOK_BODY2:
  6012. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  6013. break;
  6014. default:
  6015. break;
  6016. }
  6017. clif_changelook(&sd->bl, type, val);
  6018. return SCRIPT_CMD_SUCCESS;
  6019. }
  6020. /*==========================================
  6021. *
  6022. *------------------------------------------*/
  6023. BUILDIN_FUNC(cutin)
  6024. {
  6025. TBL_PC* sd;
  6026. if( !script_rid2sd(sd) )
  6027. return SCRIPT_CMD_SUCCESS;
  6028. clif_cutin( *sd, script_getstr( st, 2 ), script_getnum( st, 3 ) );
  6029. return SCRIPT_CMD_SUCCESS;
  6030. }
  6031. /*==========================================
  6032. *
  6033. *------------------------------------------*/
  6034. BUILDIN_FUNC(viewpoint)
  6035. {
  6036. int32 type,x,y,id,color;
  6037. TBL_PC* sd;
  6038. if (!script_charid2sd(7, sd)) {
  6039. st->state = END;
  6040. return SCRIPT_CMD_FAILURE;
  6041. }
  6042. type=script_getnum(st,2);
  6043. x=script_getnum(st,3);
  6044. y=script_getnum(st,4);
  6045. id=script_getnum(st,5);
  6046. color=script_getnum(st,6);
  6047. clif_viewpoint( *sd, st->oid, type, x, y, id, color );
  6048. return SCRIPT_CMD_SUCCESS;
  6049. }
  6050. static int32 buildin_viewpointmap_sub(block_list *bl, va_list ap) {
  6051. int32 oid, type, x, y, id, color;
  6052. oid = va_arg(ap, int);
  6053. type = va_arg(ap, int);
  6054. x = va_arg(ap, int);
  6055. y = va_arg(ap, int);
  6056. id = va_arg(ap, int);
  6057. color = va_arg(ap, int);
  6058. clif_viewpoint( *reinterpret_cast<map_session_data*>( bl ), oid, type, x, y, id, color );
  6059. return 0;
  6060. }
  6061. BUILDIN_FUNC(viewpointmap) {
  6062. const char* map = script_getstr(st, 2);
  6063. int16 mapid = map_mapname2mapid(map);
  6064. if (mapid < 0) {
  6065. ShowError("buildin_viewpointmap: Unknown map name %s.\n", map);
  6066. return SCRIPT_CMD_FAILURE;
  6067. }
  6068. map_foreachinmap(buildin_viewpointmap_sub, mapid, BL_PC, st->oid, script_getnum(st, 3), script_getnum(st, 4), script_getnum(st, 5), script_getnum(st, 6), script_getnum(st, 7));
  6069. return SCRIPT_CMD_SUCCESS;
  6070. }
  6071. /**
  6072. * Set random options for new item
  6073. * @param st Script state
  6074. * @param it Temporary item data
  6075. * @param funcname Function name
  6076. * @param x First position of random option id array from the script
  6077. **/
  6078. static bool script_getitem_randomoption(struct script_state *st, map_session_data* sd, struct item *it, const char *funcname, int32 x) {
  6079. int32 i, opt_id_n;
  6080. struct script_data *opt_id = script_getdata(st,x);
  6081. struct script_data *opt_val = script_getdata(st,x+1);
  6082. struct script_data *opt_param = script_getdata(st,x+2);
  6083. const char *opt_id_var = reference_getname(opt_id);
  6084. const char *opt_val_var = reference_getname(opt_val);
  6085. const char *opt_param_var = reference_getname(opt_param);
  6086. int32 opt_id_id, opt_id_idx;
  6087. int32 opt_val_id, opt_val_idx;
  6088. int32 opt_param_id, opt_param_idx;
  6089. struct reg_db *opt_id_ref = nullptr, *opt_val_ref = nullptr, *opt_param_ref = nullptr;
  6090. // Check if the variable requires a player
  6091. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  6092. // If no player is attached
  6093. if( !script_rid2sd(sd) ){
  6094. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  6095. return false;
  6096. }
  6097. }
  6098. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  6099. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  6100. return false;
  6101. }
  6102. if (is_string_variable(opt_id_var)) {
  6103. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  6104. return false;
  6105. }
  6106. // Check if the variable requires a player
  6107. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  6108. // If no player is attached
  6109. if( !script_rid2sd(sd) ){
  6110. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  6111. return false;
  6112. }
  6113. }
  6114. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  6115. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  6116. return false;
  6117. }
  6118. if (is_string_variable(opt_val_var)) {
  6119. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6120. return false;
  6121. }
  6122. // Check if the variable requires a player
  6123. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  6124. // If no player is attached
  6125. if( !script_rid2sd(sd) ){
  6126. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  6127. return false;
  6128. }
  6129. }
  6130. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  6131. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  6132. return false;
  6133. }
  6134. if (is_string_variable(opt_param_var)) {
  6135. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6136. return false;
  6137. }
  6138. opt_id_ref = reference_getref(opt_id);
  6139. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  6140. opt_val_ref = reference_getref(opt_val);
  6141. opt_param_ref = reference_getref(opt_param);
  6142. opt_id_id = reference_getid(opt_id);
  6143. opt_val_id = reference_getid(opt_val);
  6144. opt_param_id = reference_getid(opt_param);
  6145. opt_id_idx = reference_getindex(opt_id);
  6146. opt_val_idx = reference_getindex(opt_val);
  6147. opt_param_idx = reference_getindex(opt_param);
  6148. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6149. it->option[i].id = (short)get_val2_num( st, reference_uid( opt_id_id, opt_id_idx + i ), opt_id_ref );
  6150. it->option[i].value = (short)get_val2_num( st, reference_uid( opt_val_id, opt_val_idx + i ), opt_val_ref );
  6151. it->option[i].param = (char)get_val2_num( st, reference_uid( opt_param_id, opt_param_idx + i ), opt_param_ref );
  6152. }
  6153. return true;
  6154. }
  6155. /**
  6156. * Sub function for counting items
  6157. * @param items: Item array to search
  6158. * @param id: Item data to search for
  6159. * @param size: Maximum size of array
  6160. * @param expanded: If the script command has extra arguments
  6161. * @param random_option: If the struct command has random option arguments
  6162. * @param st: Script state (required for random options)
  6163. * @param sd: Player data (required for random options)
  6164. * @param rental: Whether or not to count rental items
  6165. * @return Total count of item being searched
  6166. */
  6167. static int32 script_countitem_sub(struct item *items, std::shared_ptr<item_data> id, int32 size, int32 expanded, struct script_state *st, map_session_data *sd = nullptr, bool rental = false) {
  6168. nullpo_retr(-1, items);
  6169. nullpo_retr(-1, st);
  6170. int32 count = 0;
  6171. if (!expanded) { // For non-expanded functions
  6172. t_itemid nameid = id->nameid;
  6173. for (int32 i = 0; i < size; i++) {
  6174. item *itm = &items[i];
  6175. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1)
  6176. continue;
  6177. if (itm->nameid == nameid && ((rental && itm->expire_time > 0) || (!rental && itm->expire_time == 0)))
  6178. count += itm->amount;
  6179. }
  6180. } else { // For expanded functions
  6181. item it = {};
  6182. int32 offset = 10;
  6183. it.nameid = id->nameid;
  6184. it.identify = script_getnum(st,3);
  6185. it.refine = script_getnum(st,4);
  6186. it.attribute = script_getnum(st,5);
  6187. it.card[0] = script_getnum(st,6);
  6188. it.card[1] = script_getnum(st,7);
  6189. it.card[2] = script_getnum(st,8);
  6190. it.card[3] = script_getnum(st,9);
  6191. if (expanded&4) {
  6192. it.enchantgrade = script_getnum(st,10);
  6193. offset = 11;
  6194. }
  6195. if (expanded&2) {
  6196. if (!sd) {
  6197. ShowError("buildin_countitem3: Player not attached.\n");
  6198. return -1;
  6199. }
  6200. bool res = script_getitem_randomoption(st, sd, &it, "countitem3", offset);
  6201. if (!res)
  6202. return -1;
  6203. }
  6204. for (int32 i = 0; i < size; i++) {
  6205. item *itm = &items[i];
  6206. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1)
  6207. continue;
  6208. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute || itm->enchantgrade != it.enchantgrade)
  6209. continue;
  6210. if ((!rental && itm->expire_time > 0) || (rental && itm->expire_time == 0))
  6211. continue;
  6212. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6213. continue;
  6214. if (expanded&2) {
  6215. uint8 j;
  6216. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6217. if (itm->option[j].id != it.option[j].id || itm->option[j].value != it.option[j].value || itm->option[j].param != it.option[j].param)
  6218. break;
  6219. }
  6220. if (j != MAX_ITEM_RDM_OPT)
  6221. continue;
  6222. }
  6223. count += items[i].amount;
  6224. }
  6225. }
  6226. return count;
  6227. }
  6228. /**
  6229. * Returns number of items in inventory
  6230. * countitem(<nameID>{,<accountID>})
  6231. * countitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6232. * countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6233. * countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6234. * countitem4(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6235. * countitem4("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6236. */
  6237. BUILDIN_FUNC(countitem)
  6238. {
  6239. TBL_PC *sd;
  6240. char *command = (char *)script_getfuncname(st);
  6241. int32 aid = 3;
  6242. int32 expanded = 0;
  6243. if (command[strlen(command)-1] == '2') {
  6244. expanded = 1;
  6245. aid = 10;
  6246. }
  6247. else if (command[strlen(command)-1] == '3') {
  6248. expanded = 3;
  6249. aid = 13;
  6250. }
  6251. else if (command[strlen(command)-1] == '4') {
  6252. expanded = 7;
  6253. aid = 14;
  6254. }
  6255. if (!script_accid2sd(aid, sd))
  6256. return SCRIPT_CMD_FAILURE;
  6257. std::shared_ptr<item_data> id;
  6258. if (script_isstring(st, 2)) // item name
  6259. id = item_db.searchname( script_getstr( st, 2 ) );
  6260. else // item id
  6261. id = item_db.find( script_getnum( st, 2 ) );
  6262. if (!id) {
  6263. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6264. script_pushint(st, 0);
  6265. return SCRIPT_CMD_FAILURE;
  6266. }
  6267. int32 count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, expanded, st, sd);
  6268. if (count < 0) {
  6269. st->state = END;
  6270. return SCRIPT_CMD_FAILURE;
  6271. }
  6272. script_pushint(st, count);
  6273. return SCRIPT_CMD_SUCCESS;
  6274. }
  6275. /**
  6276. * Returns number of items in cart
  6277. * cartcountitem(<nameID>{,<accountID>})
  6278. * cartcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6279. */
  6280. BUILDIN_FUNC(cartcountitem)
  6281. {
  6282. TBL_PC *sd;
  6283. char *command = (char *)script_getfuncname(st);
  6284. int32 aid = 3;
  6285. int32 expanded = 0;
  6286. if (command[strlen(command) - 1] == '2') {
  6287. expanded = 1;
  6288. aid = 10;
  6289. }
  6290. if (!script_accid2sd(aid, sd))
  6291. return SCRIPT_CMD_FAILURE;
  6292. if (!pc_iscarton(sd)) {
  6293. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6294. script_pushint(st, -1);
  6295. return SCRIPT_CMD_FAILURE;
  6296. }
  6297. std::shared_ptr<item_data> id;
  6298. if (script_isstring(st, 2)) // item name
  6299. id = item_db.searchname( script_getstr( st, 2 ) );
  6300. else // item id
  6301. id = item_db.find( script_getnum( st, 2 ) );
  6302. if (!id) {
  6303. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6304. script_pushint(st, 0);
  6305. return SCRIPT_CMD_FAILURE;
  6306. }
  6307. int32 count = script_countitem_sub(sd->cart.u.items_cart, id, MAX_CART, expanded, st);
  6308. if (count < 0) {
  6309. st->state = END;
  6310. return SCRIPT_CMD_FAILURE;
  6311. }
  6312. script_pushint(st, count);
  6313. return SCRIPT_CMD_SUCCESS;
  6314. }
  6315. /**
  6316. * Returns number of items in storage
  6317. * storagecountitem(<nameID>{,<accountID>})
  6318. * storagecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6319. */
  6320. BUILDIN_FUNC(storagecountitem)
  6321. {
  6322. TBL_PC *sd;
  6323. char *command = (char *)script_getfuncname(st);
  6324. int32 aid = 3;
  6325. int32 expanded = 0;
  6326. if (command[strlen(command) - 1] == '2') {
  6327. expanded = 1;
  6328. aid = 10;
  6329. }
  6330. if (!script_accid2sd(aid, sd))
  6331. return SCRIPT_CMD_FAILURE;
  6332. std::shared_ptr<item_data> id;
  6333. if (script_isstring(st, 2)) // item name
  6334. id = item_db.searchname( script_getstr( st, 2 ) );
  6335. else // item id
  6336. id = item_db.find( script_getnum( st, 2 ) );
  6337. if (!id) {
  6338. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6339. script_pushint(st, 0);
  6340. return SCRIPT_CMD_FAILURE;
  6341. }
  6342. if (sd->state.storage_flag != 0) {
  6343. script_pushint(st, -1);
  6344. return SCRIPT_CMD_SUCCESS;
  6345. }
  6346. int32 count = script_countitem_sub(sd->storage.u.items_storage, id, MAX_STORAGE, expanded, st);
  6347. if (count < 0) {
  6348. st->state = END;
  6349. return SCRIPT_CMD_FAILURE;
  6350. }
  6351. script_pushint(st, count);
  6352. return SCRIPT_CMD_SUCCESS;
  6353. }
  6354. /**
  6355. * Returns number of items in guild storage
  6356. * guildstoragecountitem(<nameID>{,<accountID>})
  6357. * guildstoragecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6358. */
  6359. BUILDIN_FUNC(guildstoragecountitem)
  6360. {
  6361. TBL_PC *sd;
  6362. char *command = (char *)script_getfuncname(st);
  6363. int32 aid = 3;
  6364. int32 expanded = 0;
  6365. if (command[strlen(command) - 1] == '2') {
  6366. expanded = 1;
  6367. aid = 10;
  6368. }
  6369. if (!script_accid2sd(aid, sd))
  6370. return SCRIPT_CMD_FAILURE;
  6371. std::shared_ptr<item_data> id;
  6372. if (script_isstring(st, 2)) // item name
  6373. id = item_db.searchname( script_getstr( st, 2 ) );
  6374. else // item id
  6375. id = item_db.find( script_getnum( st, 2 ) );
  6376. if (!id) {
  6377. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6378. script_pushint(st, 0);
  6379. return SCRIPT_CMD_FAILURE;
  6380. }
  6381. struct s_storage *gstor = guild2storage(sd->status.guild_id);
  6382. if (!gstor || (gstor && sd->state.storage_flag != 0)) {
  6383. script_pushint(st, -1);
  6384. return SCRIPT_CMD_SUCCESS;
  6385. }
  6386. gstor->lock = true;
  6387. int32 count = script_countitem_sub(gstor->u.items_guild, id, MAX_GUILD_STORAGE, expanded, st);
  6388. storage_guild_storageclose(sd);
  6389. gstor->lock = false;
  6390. if (count < 0) {
  6391. st->state = END;
  6392. return SCRIPT_CMD_FAILURE;
  6393. }
  6394. script_pushint(st, count);
  6395. return SCRIPT_CMD_SUCCESS;
  6396. }
  6397. /**
  6398. * Returns number of rental items in inventory
  6399. * rentalcountitem(<nameID>{,<accountID>})
  6400. * rentalcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6401. * rentalcountitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6402. * rentalcountitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6403. */
  6404. BUILDIN_FUNC(rentalcountitem)
  6405. {
  6406. char *command = (char *)script_getfuncname(st);
  6407. int32 aid = 3;
  6408. int32 expanded = 0;
  6409. if (command[strlen(command) - 1] == '2') {
  6410. expanded = 1;
  6411. aid = 10;
  6412. }
  6413. else if (command[strlen(command) - 1] == '3') {
  6414. expanded = 3;
  6415. aid = 13;
  6416. }
  6417. else if (command[strlen(command) - 1] == '4') {
  6418. expanded = 7;
  6419. aid = 14;
  6420. }
  6421. map_session_data *sd;
  6422. if (!script_accid2sd(aid, sd))
  6423. return SCRIPT_CMD_FAILURE;
  6424. std::shared_ptr<item_data> id;
  6425. if (script_isstring(st, 2)) // item name
  6426. id = item_db.searchname( script_getstr( st, 2 ) );
  6427. else // item id
  6428. id = item_db.find( script_getnum( st, 2 ) );
  6429. if (!id) {
  6430. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6431. script_pushint(st, 0);
  6432. return SCRIPT_CMD_FAILURE;
  6433. }
  6434. int32 count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, expanded, st, sd, true);
  6435. if (count < 0) {
  6436. st->state = END;
  6437. return SCRIPT_CMD_FAILURE;
  6438. }
  6439. script_pushint(st, count);
  6440. return SCRIPT_CMD_SUCCESS;
  6441. }
  6442. /*==========================================
  6443. * Check if item with this amount can fit in inventory
  6444. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6445. * Return
  6446. * 0 : fail
  6447. * 1 : success (npc side only)
  6448. *------------------------------------------*/
  6449. BUILDIN_FUNC(checkweight)
  6450. {
  6451. int32 slots = 0;
  6452. unsigned short amount2 = 0;
  6453. uint32 weight = 0, i, nbargs;
  6454. std::shared_ptr<item_data> id;
  6455. map_session_data* sd;
  6456. if( !script_rid2sd(sd) )
  6457. return SCRIPT_CMD_SUCCESS;
  6458. nbargs = script_lastdata(st)+1;
  6459. if(nbargs%2) {
  6460. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6461. script_pushint(st,0);
  6462. return SCRIPT_CMD_FAILURE;
  6463. }
  6464. slots = pc_inventoryblank(sd); //nb of empty slot
  6465. for (i = 2; i < nbargs; i += 2) {
  6466. t_itemid nameid;
  6467. unsigned short amount;
  6468. if( script_isstring(st, i) ) // item name
  6469. id = item_db.searchname( script_getstr( st, i ) );
  6470. else // item id
  6471. id = item_db.find( script_getnum( st, i ) );
  6472. if( id == nullptr ){
  6473. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6474. script_pushint(st,0);
  6475. return SCRIPT_CMD_FAILURE;
  6476. }
  6477. nameid = id->nameid;
  6478. amount = script_getnum(st,i+1);
  6479. if( amount < 1 ) {
  6480. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6481. script_pushint(st,0);
  6482. return SCRIPT_CMD_FAILURE;
  6483. }
  6484. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6485. if( weight + sd->weight > sd->max_weight )
  6486. {// too heavy
  6487. script_pushint(st,0);
  6488. return SCRIPT_CMD_SUCCESS;
  6489. }
  6490. switch( pc_checkadditem(sd, nameid, amount) ) {
  6491. case CHKADDITEM_EXIST:
  6492. // item is already in inventory, but there is still space for the requested amount
  6493. break;
  6494. case CHKADDITEM_NEW:
  6495. if( itemdb_isstackable(nameid) ) {
  6496. // stackable
  6497. amount2++;
  6498. if( slots < amount2 ) {
  6499. script_pushint(st,0);
  6500. return SCRIPT_CMD_SUCCESS;
  6501. }
  6502. } else {
  6503. // non-stackable
  6504. amount2 += amount;
  6505. if( slots < amount2) {
  6506. script_pushint(st,0);
  6507. return SCRIPT_CMD_SUCCESS;
  6508. }
  6509. }
  6510. break;
  6511. case CHKADDITEM_OVERAMOUNT:
  6512. script_pushint(st,0);
  6513. return SCRIPT_CMD_SUCCESS;
  6514. }
  6515. }
  6516. script_pushint(st,1);
  6517. return SCRIPT_CMD_SUCCESS;
  6518. }
  6519. BUILDIN_FUNC(checkweight2)
  6520. {
  6521. //variable sub checkweight
  6522. int32 i = 0, slots = 0, weight = 0;
  6523. short fail = 0;
  6524. unsigned short amount2 = 0;
  6525. //variable for array parsing
  6526. struct script_data* data_it;
  6527. struct script_data* data_nb;
  6528. const char* name_it;
  6529. const char* name_nb;
  6530. int32 id_it, id_nb;
  6531. int32 idx_it, idx_nb;
  6532. int32 nb_it, nb_nb; //array size
  6533. TBL_PC *sd;
  6534. if( !script_rid2sd(sd) )
  6535. return SCRIPT_CMD_SUCCESS;
  6536. data_it = script_getdata(st, 2);
  6537. data_nb = script_getdata(st, 3);
  6538. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6539. ShowError("buildin_checkweight2: parameter not a variable\n");
  6540. script_pushint(st,0);
  6541. return SCRIPT_CMD_FAILURE;// not a variable
  6542. }
  6543. id_it = reference_getid(data_it);
  6544. id_nb = reference_getid(data_nb);
  6545. idx_it = reference_getindex(data_it);
  6546. idx_nb = reference_getindex(data_nb);
  6547. name_it = reference_getname(data_it);
  6548. name_nb = reference_getname(data_nb);
  6549. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6550. ShowError("buildin_checkweight2: illegal type, need int\n");
  6551. script_pushint(st,0);
  6552. return SCRIPT_CMD_FAILURE;// not supported
  6553. }
  6554. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6555. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6556. if(nb_it != nb_nb) {
  6557. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6558. fail = 1;
  6559. }
  6560. slots = pc_inventoryblank(sd);
  6561. for(i=0; i<nb_it; i++) {
  6562. t_itemid nameid = (t_itemid)get_val2_num( st, reference_uid( id_it, idx_it + i ), reference_getref( data_it ) );
  6563. uint16 amount = (uint16)get_val2_num( st, reference_uid( id_nb, idx_nb + i ), reference_getref( data_nb ) );
  6564. if(fail)
  6565. continue; //cpntonie to depop rest
  6566. if (!item_db.exists(nameid)) {
  6567. ShowError("buildin_checkweight2: Invalid item '%u'.\n", nameid);
  6568. fail=1;
  6569. continue;
  6570. }
  6571. if(amount == 0 ) {
  6572. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6573. fail = 1;
  6574. continue;
  6575. }
  6576. weight += itemdb_weight(nameid)*amount;
  6577. if( weight + sd->weight > sd->max_weight ) {
  6578. fail = 1;
  6579. continue;
  6580. }
  6581. switch( pc_checkadditem(sd, nameid, amount) ) {
  6582. case CHKADDITEM_EXIST:
  6583. // item is already in inventory, but there is still space for the requested amount
  6584. break;
  6585. case CHKADDITEM_NEW:
  6586. if( itemdb_isstackable(nameid) ) {// stackable
  6587. amount2++;
  6588. if( slots < amount2 )
  6589. fail = 1;
  6590. } else {// non-stackable
  6591. amount2 += amount;
  6592. if( slots < amount2 ) {
  6593. fail = 1;
  6594. }
  6595. }
  6596. break;
  6597. case CHKADDITEM_OVERAMOUNT:
  6598. fail = 1;
  6599. } //end switch
  6600. } //end loop DO NOT break it prematurly we need to depop all stack
  6601. fail ? script_pushint(st,0) : script_pushint(st,1);
  6602. return SCRIPT_CMD_SUCCESS;
  6603. }
  6604. /*==========================================
  6605. * getitem <item id>,<amount>{,<account ID>};
  6606. * getitem "<item name>",<amount>{,<account ID>};
  6607. *
  6608. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6609. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6610. * Type:
  6611. * 0 - No bound
  6612. * 1 - Account Bound
  6613. * 2 - Guild Bound
  6614. * 3 - Party Bound
  6615. * 4 - Character Bound
  6616. *------------------------------------------*/
  6617. BUILDIN_FUNC(getitem)
  6618. {
  6619. int32 get_count, i;
  6620. t_itemid nameid;
  6621. unsigned short amount;
  6622. map_session_data* sd;
  6623. const char* command = script_getfuncname(st);
  6624. std::shared_ptr<item_data> id;
  6625. if( script_isstring(st, 2) ) {// "<item name>"
  6626. const char *name = script_getstr(st, 2);
  6627. id = item_db.searchname( name );
  6628. if( id == nullptr ){
  6629. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6630. return SCRIPT_CMD_FAILURE; //No item created.
  6631. }
  6632. nameid = id->nameid;
  6633. } else {// <item id>
  6634. nameid = script_getnum(st, 2);
  6635. id = item_db.find( nameid );
  6636. if( id == nullptr ){
  6637. ShowError("buildin_getitem: Nonexistant item %u requested.\n", nameid);
  6638. return SCRIPT_CMD_FAILURE; //No item created.
  6639. }
  6640. }
  6641. // <amount>
  6642. if( (amount = script_getnum(st,3)) <= 0)
  6643. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6644. item it = {};
  6645. it.nameid = nameid;
  6646. it.identify = 1;
  6647. it.bound = BOUND_NONE;
  6648. if( !strcmp(command,"getitembound") ) {
  6649. char bound = script_getnum(st,4);
  6650. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6651. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6652. return SCRIPT_CMD_FAILURE;
  6653. }
  6654. script_mapid2sd(5,sd);
  6655. it.bound = bound;
  6656. } else {
  6657. script_mapid2sd(4,sd);
  6658. }
  6659. if( sd == nullptr ) // no target
  6660. return SCRIPT_CMD_SUCCESS;
  6661. //Check if it's stackable.
  6662. if( !itemdb_isstackable2( id.get() ) ){
  6663. get_count = 1;
  6664. }else{
  6665. get_count = amount;
  6666. }
  6667. for (i = 0; i < amount; i += get_count)
  6668. {
  6669. // if not pet egg
  6670. if (!pet_create_egg(sd, nameid))
  6671. {
  6672. e_additem_result flag = pc_additem( sd, &it, get_count, LOG_TYPE_SCRIPT );
  6673. if( flag != ADDITEM_SUCCESS ){
  6674. clif_additem(sd, 0, 0, flag);
  6675. ShowError( "buildin_getitem: Failed to add the item to player.\n" );
  6676. return SCRIPT_CMD_FAILURE;
  6677. }
  6678. }
  6679. }
  6680. return SCRIPT_CMD_SUCCESS;
  6681. }
  6682. /*==========================================
  6683. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6684. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6685. *
  6686. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6687. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6688. *
  6689. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6690. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6691. *
  6692. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6693. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6694. *
  6695. * getitem4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6696. * getitem4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6697. *
  6698. * getitembound4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6699. * getitembound4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6700. Type:
  6701. * 0 - No bound
  6702. * 1 - Account Bound
  6703. * 2 - Guild Bound
  6704. * 3 - Party Bound
  6705. * 4 - Character Bound
  6706. *------------------------------------------*/
  6707. BUILDIN_FUNC(getitem2)
  6708. {
  6709. map_session_data* sd;
  6710. char bound = BOUND_NONE;
  6711. const char* command = script_getfuncname(st);
  6712. int32 offset = 0;
  6713. int32 grade_offset = 0;
  6714. if( !strncmp(command,"getitembound",12) ) {
  6715. int32 aid_pos = 12;
  6716. bound = script_getnum(st,11);
  6717. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6718. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6719. return SCRIPT_CMD_FAILURE;
  6720. }
  6721. if (command[strlen(command)-1] == '3') {
  6722. offset = 12;
  6723. aid_pos = 15;
  6724. }
  6725. else if (command[strlen(command)-1] == '4') {
  6726. grade_offset = 12;
  6727. offset = 13;
  6728. aid_pos = 16;
  6729. }
  6730. script_mapid2sd(aid_pos,sd);
  6731. } else {
  6732. int32 aid_pos = 11;
  6733. if (strcmpi(command,"getitem3") == 0) {
  6734. offset = 11;
  6735. aid_pos = 14;
  6736. }
  6737. if (strcmpi(command,"getitem4") == 0) {
  6738. grade_offset = 11;
  6739. offset = 12;
  6740. aid_pos = 15;
  6741. }
  6742. script_mapid2sd(aid_pos,sd);
  6743. }
  6744. if( sd == nullptr ) // no target
  6745. return SCRIPT_CMD_SUCCESS;
  6746. t_itemid nameid;
  6747. std::shared_ptr<item_data> item_data;
  6748. if( script_isstring(st, 2) ) {
  6749. const char *name = script_getstr(st, 2);
  6750. item_data = item_db.searchname( name );
  6751. if( item_data == nullptr ){
  6752. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6753. return SCRIPT_CMD_FAILURE; //No item created.
  6754. }
  6755. nameid = item_data->nameid;
  6756. } else {
  6757. nameid = script_getnum(st, 2);
  6758. item_data = item_db.find( nameid );
  6759. if( item_data == nullptr ){
  6760. ShowError("buildin_getitem2: Nonexistant item %u requested (by conv_num).\n", nameid);
  6761. return SCRIPT_CMD_FAILURE; //No item created.
  6762. }
  6763. }
  6764. int32 amount = script_getnum(st,3);
  6765. int32 iden = script_getnum(st,4);
  6766. int32 ref = script_getnum(st,5);
  6767. int32 attr = script_getnum(st,6);
  6768. t_itemid c1 = script_getnum(st,7);
  6769. t_itemid c2 = script_getnum(st,8);
  6770. t_itemid c3 = script_getnum(st,9);
  6771. t_itemid c4 = script_getnum(st,10);
  6772. item item_tmp = {};
  6773. if( item_data ) {
  6774. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6775. if(ref > MAX_REFINE)
  6776. ref = MAX_REFINE;
  6777. }
  6778. else if( item_data->type == IT_PETEGG ) {
  6779. iden = 1;
  6780. ref = 0;
  6781. }
  6782. else {
  6783. iden = 1;
  6784. ref = attr = 0;
  6785. }
  6786. item_tmp.nameid = nameid;
  6787. item_tmp.identify = iden;
  6788. item_tmp.refine = ref;
  6789. item_tmp.attribute = attr;
  6790. item_tmp.card[0] = c1;
  6791. item_tmp.card[1] = c2;
  6792. item_tmp.card[2] = c3;
  6793. item_tmp.card[3] = c4;
  6794. item_tmp.bound = bound;
  6795. if (offset != 0) {
  6796. bool res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6797. if (!res)
  6798. return SCRIPT_CMD_FAILURE;
  6799. if (grade_offset > 0) {
  6800. int32 grade = script_getnum(st, grade_offset);
  6801. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  6802. ShowError("buildin_getitem2: Not a correct grade! Grade=%d\n", grade);
  6803. return SCRIPT_CMD_FAILURE;
  6804. }
  6805. item_tmp.enchantgrade = static_cast<e_enchantgrade>(grade);
  6806. }
  6807. }
  6808. int32 get_count = 0;
  6809. //Check if it's stackable.
  6810. if( !itemdb_isstackable2( item_data.get() ) ){
  6811. get_count = 1;
  6812. }else{
  6813. get_count = amount;
  6814. }
  6815. for (int32 i = 0; i < amount; i += get_count)
  6816. {
  6817. // if not pet egg
  6818. if (!pet_create_egg(sd, nameid))
  6819. {
  6820. e_additem_result flag = pc_additem( sd, &item_tmp, get_count, LOG_TYPE_SCRIPT );
  6821. if( flag != ADDITEM_SUCCESS ){
  6822. clif_additem(sd, 0, 0, flag);
  6823. ShowError( "buildin_getitem2: Failed to add the item to player.\n" );
  6824. return SCRIPT_CMD_FAILURE;
  6825. }
  6826. }
  6827. }
  6828. }
  6829. return SCRIPT_CMD_SUCCESS;
  6830. }
  6831. /** Gives rental item to player
  6832. * rentitem <item id>,<seconds>{,<account_id>}
  6833. * rentitem "<item name>",<seconds>{,<account_id>}
  6834. */
  6835. BUILDIN_FUNC(rentitem) {
  6836. map_session_data *sd;
  6837. t_itemid nameid = 0;
  6838. unsigned char flag = 0;
  6839. if (!script_accid2sd(4,sd))
  6840. return SCRIPT_CMD_FAILURE;
  6841. if( script_isstring(st, 2) )
  6842. {
  6843. const char *name = script_getstr(st, 2);
  6844. std::shared_ptr<item_data> itd = item_db.searchname( name );
  6845. if( itd == nullptr ){
  6846. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6847. return SCRIPT_CMD_FAILURE;
  6848. }
  6849. nameid = itd->nameid;
  6850. }
  6851. else
  6852. {
  6853. nameid = script_getnum(st, 2);
  6854. if( !item_db.exists(nameid) )
  6855. {
  6856. ShowError("buildin_rentitem: Nonexistant item %u requested.\n", nameid);
  6857. return SCRIPT_CMD_FAILURE;
  6858. }
  6859. }
  6860. int32 seconds = script_getnum(st,3);
  6861. struct item it = {};
  6862. it.nameid = nameid;
  6863. it.identify = 1;
  6864. it.expire_time = (uint32)(time(nullptr) + seconds);
  6865. it.bound = BOUND_NONE;
  6866. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6867. {
  6868. clif_additem(sd, 0, 0, flag);
  6869. return SCRIPT_CMD_FAILURE;
  6870. }
  6871. return SCRIPT_CMD_SUCCESS;
  6872. }
  6873. /**
  6874. * Gives rental item to player with advanced option
  6875. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6876. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6877. *
  6878. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6879. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6880. *
  6881. * rentitem4 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6882. * rentitem4 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6883. */
  6884. BUILDIN_FUNC(rentitem2) {
  6885. map_session_data *sd;
  6886. const char *funcname = script_getfuncname(st);
  6887. if (funcname[strlen(funcname)-1] == '3') {
  6888. if (!script_accid2sd(14,sd))
  6889. return SCRIPT_CMD_FAILURE;
  6890. } else if (funcname[strlen(funcname)-1] == '4') {
  6891. if (!script_accid2sd(15,sd))
  6892. return SCRIPT_CMD_FAILURE;
  6893. } else if (!script_accid2sd(11,sd))
  6894. return SCRIPT_CMD_FAILURE;
  6895. std::shared_ptr<item_data> id;
  6896. t_itemid nameid = 0;
  6897. if( script_isstring(st, 2) ) {
  6898. const char *name = script_getstr(st, 2);
  6899. id = item_db.searchname( name );
  6900. if( id == nullptr ) {
  6901. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6902. return SCRIPT_CMD_FAILURE;
  6903. }
  6904. nameid = id->nameid;
  6905. } else {
  6906. nameid = script_getnum(st, 2);
  6907. id = item_db.find( nameid );
  6908. if( id == nullptr ){
  6909. ShowError("buildin_rentitem2: Nonexistant item %u requested.\n", nameid);
  6910. return SCRIPT_CMD_FAILURE;
  6911. }
  6912. }
  6913. int32 seconds = script_getnum(st,3);
  6914. int32 iden = script_getnum(st,4);
  6915. int32 ref = script_getnum(st,5);
  6916. int32 attr = script_getnum(st,6);
  6917. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6918. if(ref > MAX_REFINE)
  6919. ref = MAX_REFINE;
  6920. }
  6921. else if (id->type==IT_PETEGG) {
  6922. iden = 1;
  6923. ref = 0;
  6924. }
  6925. else {
  6926. iden = 1;
  6927. ref = attr = 0;
  6928. }
  6929. t_itemid c1 = script_getnum(st,7);
  6930. t_itemid c2 = script_getnum(st,8);
  6931. t_itemid c3 = script_getnum(st,9);
  6932. t_itemid c4 = script_getnum(st,10);
  6933. struct item it = {};
  6934. it.nameid = nameid;
  6935. it.identify = iden;
  6936. it.refine = ref;
  6937. it.attribute = attr;
  6938. it.card[0] = c1;
  6939. it.card[1] = c2;
  6940. it.card[2] = c3;
  6941. it.card[3] = c4;
  6942. it.expire_time = (uint32)(time(nullptr) + seconds);
  6943. it.bound = BOUND_NONE;
  6944. if (funcname[strlen(funcname)-1] == '3') {
  6945. bool res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6946. if (!res)
  6947. return SCRIPT_CMD_FAILURE;
  6948. }
  6949. else if (funcname[strlen(funcname)-1] == '4') {
  6950. bool res = script_getitem_randomoption(st, sd, &it, funcname, 12);
  6951. if (!res)
  6952. return SCRIPT_CMD_FAILURE;
  6953. int32 grade = script_getnum(st, 11);
  6954. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  6955. ShowError("buildin_rentitem2: Not a correct grade! Grade=%d\n", grade);
  6956. return SCRIPT_CMD_FAILURE;
  6957. }
  6958. it.enchantgrade = static_cast<e_enchantgrade>(grade);
  6959. }
  6960. unsigned char flag = 0;
  6961. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6962. clif_additem(sd, 0, 0, flag);
  6963. return SCRIPT_CMD_FAILURE;
  6964. }
  6965. return SCRIPT_CMD_SUCCESS;
  6966. }
  6967. /*==========================================
  6968. * gets an item with someone's name inscribed [Skotlex]
  6969. * getinscribeditem item_num, character_name
  6970. * Returned Qty is always 1, only works on equip-able
  6971. * equipment
  6972. *------------------------------------------*/
  6973. BUILDIN_FUNC(getnameditem)
  6974. {
  6975. t_itemid nameid;
  6976. struct item item_tmp;
  6977. TBL_PC *sd, *tsd;
  6978. if (!script_rid2sd(sd))
  6979. { //Player not attached!
  6980. script_pushint(st,0);
  6981. return SCRIPT_CMD_SUCCESS;
  6982. }
  6983. if( script_isstring(st, 2) ){
  6984. const char *name = script_getstr(st, 2);
  6985. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6986. // Failed
  6987. if( item_data == nullptr){
  6988. script_pushint(st,0);
  6989. return SCRIPT_CMD_SUCCESS;
  6990. }
  6991. nameid = item_data->nameid;
  6992. }else
  6993. nameid = script_getnum(st, 2);
  6994. if(!item_db.exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6995. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6996. script_pushint(st,0);
  6997. return SCRIPT_CMD_SUCCESS;
  6998. }
  6999. if( script_isstring(st, 3) ) //Char Name
  7000. tsd = map_nick2sd(script_getstr(st, 3),false);
  7001. else //Char Id was given
  7002. tsd = map_charid2sd(script_getnum(st, 3));
  7003. if( tsd == nullptr )
  7004. { //Failed
  7005. script_pushint(st,0);
  7006. return SCRIPT_CMD_SUCCESS;
  7007. }
  7008. memset(&item_tmp,0,sizeof(item_tmp));
  7009. item_tmp.nameid=nameid;
  7010. item_tmp.amount=1;
  7011. item_tmp.identify=1;
  7012. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  7013. item_tmp.card[2]=GetWord(tsd->status.char_id,0);
  7014. item_tmp.card[3]=GetWord(tsd->status.char_id,1);
  7015. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  7016. script_pushint(st,0);
  7017. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  7018. }
  7019. script_pushint(st,1);
  7020. return SCRIPT_CMD_SUCCESS;
  7021. }
  7022. /*==========================================
  7023. * gets a random item ID from an item group [Skotlex]
  7024. * groupranditem <group_num>{,<sub_group>};
  7025. *------------------------------------------*/
  7026. BUILDIN_FUNC(grouprandomitem) {
  7027. int32 sub_group = 1;
  7028. FETCH(3, sub_group);
  7029. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(script_getnum(st,2),sub_group);
  7030. if (!entry) {
  7031. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  7032. script_pushint(st,UNKNOWN_ITEM_ID);
  7033. return SCRIPT_CMD_FAILURE;
  7034. }
  7035. script_pushint(st,entry->nameid);
  7036. return SCRIPT_CMD_SUCCESS;
  7037. }
  7038. /**
  7039. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  7040. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  7041. */
  7042. BUILDIN_FUNC(makeitem) {
  7043. t_itemid nameid;
  7044. uint16 amount, flag = 0, x, y;
  7045. const char *mapname;
  7046. int32 m;
  7047. struct item item_tmp;
  7048. bool canShowEffect = false;
  7049. if( script_isstring(st, 2) ){
  7050. const char *name = script_getstr(st, 2);
  7051. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  7052. if( item_data )
  7053. nameid = item_data->nameid;
  7054. else{
  7055. ShowError( "buildin_makeitem: Unknown item %s\n", name );
  7056. return SCRIPT_CMD_FAILURE;
  7057. }
  7058. }
  7059. else {
  7060. int32 val = script_getnum( st, 2 );
  7061. if( val < 0 ){
  7062. flag = 1;
  7063. nameid = (t_itemid)( -1 * val );
  7064. }else{
  7065. nameid = (t_itemid)val;
  7066. }
  7067. if( !item_db.exists( nameid ) ){
  7068. ShowError( "buildin_makeitem: Unknown item id %u\n", nameid );
  7069. return SCRIPT_CMD_FAILURE;
  7070. }
  7071. }
  7072. amount = script_getnum(st,3);
  7073. mapname = script_getstr(st,4);
  7074. x = script_getnum(st,5);
  7075. y = script_getnum(st,6);
  7076. if (script_hasdata(st, 7))
  7077. canShowEffect = script_getnum(st, 7) != 0;
  7078. if (strcmp(mapname, "this")==0) {
  7079. TBL_PC *sd;
  7080. if (!script_rid2sd(sd))
  7081. return SCRIPT_CMD_SUCCESS; //Failed...
  7082. m = sd->bl.m;
  7083. } else
  7084. m = map_mapname2mapid(mapname);
  7085. memset(&item_tmp,0,sizeof(item_tmp));
  7086. item_tmp.nameid = nameid;
  7087. if (!flag)
  7088. item_tmp.identify = 1;
  7089. else
  7090. item_tmp.identify = itemdb_isidentified(nameid);
  7091. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7092. return SCRIPT_CMD_SUCCESS;
  7093. }
  7094. /**
  7095. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  7096. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  7097. *
  7098. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7099. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7100. *
  7101. * makeitem4 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7102. * makeitem4 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7103. */
  7104. BUILDIN_FUNC(makeitem2) {
  7105. t_itemid nameid;
  7106. const char *funcname = script_getfuncname(st);
  7107. if( script_isstring( st, 2 ) ){
  7108. const char *name = script_getstr( st, 2 );
  7109. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  7110. if( item_data ){
  7111. nameid = item_data->nameid;
  7112. }else{
  7113. ShowError( "buildin_%s: Unknown item %s\n", funcname, name );
  7114. return SCRIPT_CMD_FAILURE;
  7115. }
  7116. }else{
  7117. nameid = (t_itemid)script_getnum( st, 2 );
  7118. if( !item_db.exists( nameid ) ){
  7119. ShowError( "buildin_%s: Unknown item id %u\n", funcname, nameid );
  7120. return SCRIPT_CMD_FAILURE;
  7121. }
  7122. }
  7123. int32 amount = script_getnum(st,3);
  7124. const char *mapname = script_getstr(st,4);
  7125. int32 x = script_getnum(st,5);
  7126. int32 y = script_getnum(st,6);
  7127. int32 m;
  7128. if (strcmp(mapname,"this")==0) {
  7129. TBL_PC *sd;
  7130. if (!script_rid2sd(sd))
  7131. return SCRIPT_CMD_SUCCESS; //Failed...
  7132. m = sd->bl.m;
  7133. }
  7134. else
  7135. m = map_mapname2mapid(mapname);
  7136. struct item_data *id;
  7137. if ((id = itemdb_search(nameid))) {
  7138. struct item item_tmp = {};
  7139. item_tmp.nameid = nameid;
  7140. char iden = (char)script_getnum(st,7);
  7141. char ref = (char)script_getnum(st,8);
  7142. char attr = (char)script_getnum(st,9);
  7143. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  7144. if(ref > MAX_REFINE)
  7145. ref = MAX_REFINE;
  7146. }
  7147. else if (id->type==IT_PETEGG) {
  7148. iden = 1;
  7149. ref = 0;
  7150. }
  7151. else {
  7152. iden = 1;
  7153. ref = attr = 0;
  7154. }
  7155. item_tmp.identify = iden;
  7156. item_tmp.refine = ref;
  7157. item_tmp.attribute = attr;
  7158. item_tmp.card[0] = script_getnum(st,10);
  7159. item_tmp.card[1] = script_getnum(st,11);
  7160. item_tmp.card[2] = script_getnum(st,12);
  7161. item_tmp.card[3] = script_getnum(st,13);
  7162. bool canShowEffect = false;
  7163. if (funcname[strlen(funcname)-1] == '3' || funcname[strlen(funcname)-1] == '4') {
  7164. int32 offset = 14;
  7165. if (funcname[strlen(funcname)-1] == '4') {
  7166. offset = 15;
  7167. int32 grade = script_getnum(st, 14);
  7168. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  7169. ShowError( "buildin_%s: Not a correct grade! Grade=%d\n", funcname, grade );
  7170. return SCRIPT_CMD_FAILURE;
  7171. }
  7172. item_tmp.enchantgrade = static_cast<e_enchantgrade>(grade);
  7173. }
  7174. bool res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, offset);
  7175. if (!res)
  7176. return SCRIPT_CMD_FAILURE;
  7177. if (script_hasdata(st, offset+3)) {
  7178. if (script_getnum(st, offset+3) != 0)
  7179. canShowEffect = script_getnum(st, offset+3) != 0;
  7180. }
  7181. }
  7182. else {
  7183. if (script_hasdata(st, 14)) {
  7184. if (script_getnum(st, 14) != 0)
  7185. canShowEffect = script_getnum(st, 14) != 0;
  7186. }
  7187. }
  7188. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7189. }
  7190. else
  7191. return SCRIPT_CMD_FAILURE;
  7192. return SCRIPT_CMD_SUCCESS;
  7193. }
  7194. /// Counts / deletes the current item given by idx.
  7195. /// Used by buildin_delitem_search
  7196. /// Relies on all input data being already fully valid.
  7197. static void buildin_delitem_delete(map_session_data* sd, int32 idx, int* amount, uint8 loc, bool delete_items)
  7198. {
  7199. int32 delamount;
  7200. struct item *itm = nullptr;
  7201. struct s_storage *gstor = nullptr;
  7202. switch(loc) {
  7203. case TABLE_CART:
  7204. itm = &sd->cart.u.items_cart[idx];
  7205. break;
  7206. case TABLE_STORAGE:
  7207. itm = &sd->storage.u.items_storage[idx];
  7208. break;
  7209. case TABLE_GUILD_STORAGE:
  7210. {
  7211. gstor = guild2storage2(sd->status.guild_id);
  7212. itm = &gstor->u.items_guild[idx];
  7213. }
  7214. break;
  7215. default: // TABLE_INVENTORY
  7216. itm = &sd->inventory.u.items_inventory[idx];
  7217. break;
  7218. }
  7219. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7220. if( delete_items )
  7221. {
  7222. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7223. {// delete associated pet
  7224. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7225. }
  7226. switch(loc) {
  7227. case TABLE_CART:
  7228. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7229. break;
  7230. case TABLE_STORAGE:
  7231. storage_delitem(sd,&sd->storage,idx,delamount);
  7232. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7233. break;
  7234. case TABLE_GUILD_STORAGE:
  7235. gstor->lock = true;
  7236. storage_guild_delitem(sd, gstor, idx, delamount);
  7237. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7238. storage_guild_storageclose(sd);
  7239. gstor->lock = false;
  7240. break;
  7241. default: // TABLE_INVENTORY
  7242. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7243. break;
  7244. }
  7245. }
  7246. amount[0]-= delamount;
  7247. }
  7248. /// Searches for item(s) and checks, if there is enough of them.
  7249. /// Used by delitem and delitem2
  7250. /// Relies on all input data being already fully valid.
  7251. /// @param exact_match will also match item by specified attributes
  7252. /// 0x0: Only item id
  7253. /// 0x1: identify, attributes, cards
  7254. /// 0x2: random option
  7255. /// @return true when all items could be deleted, false when there were not enough items to delete
  7256. static bool buildin_delitem_search(map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7257. {
  7258. bool delete_items = false;
  7259. int32 i, amount, size;
  7260. struct item *items;
  7261. // prefer always non-equipped items
  7262. it->equip = 0;
  7263. // when searching for nameid only, prefer additionally
  7264. if( !exact_match )
  7265. {
  7266. // non-refined items
  7267. it->refine = 0;
  7268. // card-less items
  7269. memset(it->card, 0, sizeof(it->card));
  7270. }
  7271. switch(loc) {
  7272. case TABLE_CART:
  7273. size = MAX_CART;
  7274. items = sd->cart.u.items_cart;
  7275. break;
  7276. case TABLE_STORAGE:
  7277. size = MAX_STORAGE;
  7278. items = sd->storage.u.items_storage;
  7279. break;
  7280. case TABLE_GUILD_STORAGE:
  7281. {
  7282. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7283. size = MAX_GUILD_STORAGE;
  7284. items = gstor->u.items_guild;
  7285. }
  7286. break;
  7287. default: // TABLE_INVENTORY
  7288. size = MAX_INVENTORY;
  7289. items = sd->inventory.u.items_inventory;
  7290. break;
  7291. }
  7292. for(;;)
  7293. {
  7294. unsigned short important = 0;
  7295. amount = it->amount;
  7296. // 1st pass -- less important items / exact match
  7297. for( i = 0; amount && i < size; i++ )
  7298. {
  7299. struct item *itm = nullptr;
  7300. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7301. {// wrong/invalid item
  7302. continue;
  7303. }
  7304. if( itm->equip != it->equip || itm->refine != it->refine )
  7305. {// not matching attributes
  7306. important++;
  7307. continue;
  7308. }
  7309. if( exact_match )
  7310. {
  7311. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || itm->enchantgrade != it->enchantgrade || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7312. {// not matching exact attributes
  7313. continue;
  7314. }
  7315. if (exact_match&0x2) {
  7316. uint8 j;
  7317. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7318. if (itm->option[j].id != it->option[j].id || itm->option[j].value != it->option[j].value || itm->option[j].param != it->option[j].param)
  7319. break;
  7320. }
  7321. if (j != MAX_ITEM_RDM_OPT)
  7322. continue;
  7323. }
  7324. }
  7325. else
  7326. {
  7327. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7328. {
  7329. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7330. {// pet which cannot be deleted
  7331. continue;
  7332. }
  7333. }
  7334. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7335. {// named/carded item
  7336. important++;
  7337. continue;
  7338. }
  7339. }
  7340. // count / delete item
  7341. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7342. }
  7343. // 2nd pass -- any matching item
  7344. if( amount == 0 || important == 0 )
  7345. {// either everything was already consumed or no items were skipped
  7346. ;
  7347. }
  7348. else for( i = 0; amount && i < size; i++ )
  7349. {
  7350. struct item *itm = nullptr;
  7351. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7352. {// wrong/invalid item
  7353. continue;
  7354. }
  7355. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7356. {// pet which cannot be deleted
  7357. continue;
  7358. }
  7359. if( exact_match )
  7360. {
  7361. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || itm->enchantgrade != it->enchantgrade || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7362. {// not matching attributes
  7363. continue;
  7364. }
  7365. if (exact_match&0x2) {
  7366. uint8 j;
  7367. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7368. if (itm->option[j].id != it->option[j].id || itm->option[j].value != it->option[j].value || itm->option[j].param != it->option[j].param)
  7369. break;
  7370. }
  7371. if (j != MAX_ITEM_RDM_OPT)
  7372. continue;
  7373. }
  7374. }
  7375. // count / delete item
  7376. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7377. }
  7378. if( amount )
  7379. {// not enough items
  7380. return false;
  7381. }
  7382. else if( delete_items )
  7383. {// we are done with the work
  7384. return true;
  7385. }
  7386. else
  7387. {// get rid of the items now
  7388. delete_items = true;
  7389. }
  7390. }
  7391. }
  7392. /// Deletes items from the target/attached player.
  7393. /// Prioritizes ordinary items.
  7394. ///
  7395. /// delitem <item id>,<amount>{,<account id>}
  7396. /// delitem "<item name>",<amount>{,<account id>}
  7397. /// cartdelitem <item id>,<amount>{,<account id>}
  7398. /// cartdelitem "<item name>",<amount>{,<account id>}
  7399. /// storagedelitem <item id>,<amount>{,<account id>}
  7400. /// storagedelitem "<item name>",<amount>{,<account id>}
  7401. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7402. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7403. BUILDIN_FUNC(delitem)
  7404. {
  7405. TBL_PC *sd;
  7406. struct item it;
  7407. uint8 loc = 0;
  7408. char* command = (char*)script_getfuncname(st);
  7409. if(!strncmp(command, "cart", 4))
  7410. loc = TABLE_CART;
  7411. else if(!strncmp(command, "storage", 7))
  7412. loc = TABLE_STORAGE;
  7413. else if(!strncmp(command, "guildstorage", 12))
  7414. loc = TABLE_GUILD_STORAGE;
  7415. if( !script_accid2sd(4,sd) ){
  7416. // In any case cancel script execution
  7417. st->state = END;
  7418. return SCRIPT_CMD_SUCCESS;
  7419. }
  7420. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7421. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7422. script_pushint(st, -1);
  7423. return SCRIPT_CMD_FAILURE;
  7424. }
  7425. if (loc == TABLE_GUILD_STORAGE) {
  7426. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7427. if (gstor == nullptr || sd->state.storage_flag) {
  7428. script_pushint(st, -1);
  7429. return SCRIPT_CMD_FAILURE;
  7430. }
  7431. }
  7432. if( script_isstring(st, 2) )
  7433. {
  7434. const char* item_name = script_getstr(st, 2);
  7435. std::shared_ptr<item_data> id = item_db.searchname(item_name);
  7436. if( id == nullptr ){
  7437. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7438. st->state = END;
  7439. return SCRIPT_CMD_FAILURE;
  7440. }
  7441. it.nameid = id->nameid;// "<item name>"
  7442. }
  7443. else
  7444. {
  7445. it.nameid = script_getnum(st, 2);// <item id>
  7446. if( !item_db.exists( it.nameid ) )
  7447. {
  7448. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7449. st->state = END;
  7450. return SCRIPT_CMD_FAILURE;
  7451. }
  7452. }
  7453. it.amount=script_getnum(st,3);
  7454. if( it.amount <= 0 )
  7455. return SCRIPT_CMD_SUCCESS;// nothing to do
  7456. if( buildin_delitem_search(sd, &it, 0, loc) )
  7457. {// success
  7458. return SCRIPT_CMD_SUCCESS;
  7459. }
  7460. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7461. st->state = END;
  7462. st->mes_active = 0;
  7463. clif_scriptclose( *sd, st->oid );
  7464. return SCRIPT_CMD_FAILURE;
  7465. }
  7466. /// Deletes items from the target/attached player.
  7467. ///
  7468. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7469. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7470. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7471. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7472. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7473. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7474. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7475. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7476. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7477. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7478. /// delitem4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7479. /// delitem4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7480. BUILDIN_FUNC(delitem2)
  7481. {
  7482. TBL_PC *sd;
  7483. uint8 loc = 0;
  7484. char* command = (char*)script_getfuncname(st);
  7485. int32 aid_pos = 11;
  7486. if(!strncmp(command, "cart", 4))
  7487. loc = TABLE_CART;
  7488. else if(!strncmp(command, "storage", 7))
  7489. loc = TABLE_STORAGE;
  7490. else if(!strncmp(command, "guildstorage", 12))
  7491. loc = TABLE_GUILD_STORAGE;
  7492. if (command[strlen(command)-1] == '3')
  7493. aid_pos = 14;
  7494. else if (command[strlen(command)-1] == '4')
  7495. aid_pos = 15;
  7496. if( !script_accid2sd(aid_pos,sd) ){
  7497. // In any case cancel script execution
  7498. st->state = END;
  7499. return SCRIPT_CMD_SUCCESS;
  7500. }
  7501. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7502. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7503. script_pushint(st,-1);
  7504. return SCRIPT_CMD_FAILURE;
  7505. }
  7506. if (loc == TABLE_GUILD_STORAGE) {
  7507. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7508. if (gstor == nullptr || sd->state.storage_flag) {
  7509. script_pushint(st, -1);
  7510. return SCRIPT_CMD_FAILURE;
  7511. }
  7512. }
  7513. struct item it = {};
  7514. if( script_isstring(st, 2) )
  7515. {
  7516. const char* item_name = script_getstr(st, 2);
  7517. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  7518. if( id == nullptr ){
  7519. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7520. st->state = END;
  7521. return SCRIPT_CMD_FAILURE;
  7522. }
  7523. it.nameid = id->nameid;// "<item name>"
  7524. }
  7525. else
  7526. {
  7527. it.nameid = script_getnum(st, 2);// <item id>
  7528. if( !item_db.exists( it.nameid ) )
  7529. {
  7530. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7531. st->state = END;
  7532. return SCRIPT_CMD_FAILURE;
  7533. }
  7534. }
  7535. it.amount=script_getnum(st,3);
  7536. it.identify=script_getnum(st,4);
  7537. it.refine=script_getnum(st,5);
  7538. it.attribute=script_getnum(st,6);
  7539. it.card[0]=script_getnum(st,7);
  7540. it.card[1]=script_getnum(st,8);
  7541. it.card[2]=script_getnum(st,9);
  7542. it.card[3]=script_getnum(st,10);
  7543. uint8 flag = 0x1;
  7544. if (command[strlen(command)-1] == '3' || command[strlen(command)-1] == '4') {
  7545. int32 offset = 11;
  7546. if (command[strlen(command)-1] == '4') {
  7547. offset = 12;
  7548. int32 grade = script_getnum(st, 11);
  7549. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  7550. ShowError( "buildin_%s: Not a correct grade! Grade=%d\n", command, grade );
  7551. return SCRIPT_CMD_FAILURE;
  7552. }
  7553. it.enchantgrade = static_cast<e_enchantgrade>(grade);
  7554. }
  7555. bool res = script_getitem_randomoption(st, sd, &it, command, offset);
  7556. if (!res)
  7557. return SCRIPT_CMD_FAILURE;
  7558. flag |= 0x2;
  7559. }
  7560. if( it.amount <= 0 )
  7561. return SCRIPT_CMD_SUCCESS;// nothing to do
  7562. if( buildin_delitem_search(sd, &it, flag, loc) )
  7563. {// success
  7564. return SCRIPT_CMD_SUCCESS;
  7565. }
  7566. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7567. st->state = END;
  7568. st->mes_active = 0;
  7569. clif_scriptclose( *sd, st->oid );
  7570. return SCRIPT_CMD_FAILURE;
  7571. }
  7572. /// Deletes items from the target/attached player at given index.
  7573. /// delitemidx <index>{,<amount>{,<char id>}};
  7574. BUILDIN_FUNC(delitemidx) {
  7575. map_session_data* sd;
  7576. if (!script_charid2sd(4, sd)) {
  7577. script_pushint(st, false);
  7578. return SCRIPT_CMD_FAILURE;
  7579. }
  7580. int32 idx = script_getnum(st, 2);
  7581. if (idx < 0 || idx >= MAX_INVENTORY) {
  7582. ShowWarning("buildin_delitemidx: Index %d is out of the range 0-%d.\n", idx, MAX_INVENTORY - 1);
  7583. script_pushint(st, false);
  7584. return SCRIPT_CMD_FAILURE;
  7585. }
  7586. if (sd->inventory_data[idx] == nullptr) {
  7587. ShowWarning("buildin_delitemidx: No item can be deleted from index %d of player %s (AID: %u, CID: %u).\n", idx, sd->status.name, sd->status.account_id, sd->status.char_id);
  7588. script_pushint(st, false);
  7589. return SCRIPT_CMD_FAILURE;
  7590. }
  7591. int32 amount;
  7592. if (script_hasdata(st, 3))
  7593. amount = script_getnum(st, 3);
  7594. else
  7595. amount = sd->inventory.u.items_inventory[idx].amount;
  7596. if (amount > 0)
  7597. script_pushint(st, pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_SCRIPT) == 0);
  7598. else
  7599. script_pushint(st, false);
  7600. return SCRIPT_CMD_SUCCESS;
  7601. }
  7602. /*==========================================
  7603. * Enables/Disables use of items while in an NPC [Skotlex]
  7604. *------------------------------------------*/
  7605. BUILDIN_FUNC(enableitemuse)
  7606. {
  7607. TBL_PC *sd;
  7608. if (script_rid2sd(sd))
  7609. st->npc_item_flag = sd->npc_item_flag = 1;
  7610. return SCRIPT_CMD_SUCCESS;
  7611. }
  7612. BUILDIN_FUNC(disableitemuse)
  7613. {
  7614. TBL_PC *sd;
  7615. if (script_rid2sd(sd))
  7616. st->npc_item_flag = sd->npc_item_flag = 0;
  7617. return SCRIPT_CMD_SUCCESS;
  7618. }
  7619. /*==========================================
  7620. * Returns a character's specified stat.
  7621. * Check pc_readparam for available options.
  7622. * readparam <param>{,"<nick>"}
  7623. * readparam <param>{,<char_id>}
  7624. *------------------------------------------*/
  7625. BUILDIN_FUNC(readparam)
  7626. {
  7627. int32 value;
  7628. struct script_data *data = script_getdata(st, 2);
  7629. TBL_PC *sd = nullptr;
  7630. if( script_hasdata(st, 3) ){
  7631. if (script_isint(st, 3))
  7632. script_charid2sd(3, sd);
  7633. else
  7634. script_nick2sd(3, sd);
  7635. }else{
  7636. script_rid2sd(sd);
  7637. }
  7638. if( !sd ){
  7639. script_pushint(st, -1);
  7640. return SCRIPT_CMD_FAILURE;
  7641. }
  7642. // If you use a parameter, return the value behind it
  7643. if( reference_toparam(data) ){
  7644. get_val_(st, data, sd);
  7645. value = (int)data->u.num;
  7646. }else{
  7647. value = static_cast<int>(pc_readparam(sd,script_getnum(st, 2)));
  7648. }
  7649. script_pushint(st,value);
  7650. return SCRIPT_CMD_SUCCESS;
  7651. }
  7652. /*==========================================
  7653. * Return charid identification
  7654. * return by @num :
  7655. * 0 : char_id
  7656. * 1 : party_id
  7657. * 2 : guild_id
  7658. * 3 : account_id
  7659. * 4 : bg_id
  7660. * 5 : clan_id
  7661. *------------------------------------------*/
  7662. BUILDIN_FUNC(getcharid)
  7663. {
  7664. int32 num;
  7665. TBL_PC *sd;
  7666. num = script_getnum(st,2);
  7667. if( !script_nick2sd(3,sd) ){
  7668. script_pushint(st,0); //return 0, according docs
  7669. return SCRIPT_CMD_SUCCESS;
  7670. }
  7671. switch( num ) {
  7672. case 0: script_pushint(st,sd->status.char_id); break;
  7673. case 1: script_pushint(st,sd->status.party_id); break;
  7674. case 2: script_pushint(st,sd->status.guild_id); break;
  7675. case 3: script_pushint(st,sd->status.account_id); break;
  7676. case 4: script_pushint(st,sd->bg_id); break;
  7677. case 5: script_pushint(st,sd->status.clan_id); break;
  7678. default:
  7679. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7680. script_pushint(st,0);
  7681. break;
  7682. }
  7683. return SCRIPT_CMD_SUCCESS;
  7684. }
  7685. /*==========================================
  7686. * returns the GID of an NPC
  7687. * Returns 0 if the NPC name provided is not found.
  7688. *------------------------------------------*/
  7689. BUILDIN_FUNC(getnpcid)
  7690. {
  7691. int32 num = script_getnum(st,2);
  7692. struct npc_data* nd = nullptr;
  7693. if( script_hasdata(st,3) )
  7694. {// unique npc name
  7695. if( ( nd = npc_name2id(script_getstr(st,3)) ) == nullptr )
  7696. {
  7697. //Npc not found.
  7698. script_pushint(st,0);
  7699. return SCRIPT_CMD_SUCCESS;
  7700. }
  7701. }
  7702. switch (num) {
  7703. case 0:
  7704. script_pushint(st,nd ? nd->bl.id : st->oid);
  7705. break;
  7706. default:
  7707. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7708. script_pushint(st,0);
  7709. return SCRIPT_CMD_FAILURE;
  7710. }
  7711. return SCRIPT_CMD_SUCCESS;
  7712. }
  7713. /*==========================================
  7714. * Return the name of the party_id
  7715. * null if not found
  7716. *------------------------------------------*/
  7717. BUILDIN_FUNC(getpartyname)
  7718. {
  7719. int32 party_id;
  7720. struct party_data* p;
  7721. party_id = script_getnum(st,2);
  7722. if( ( p = party_search(party_id) ) != nullptr )
  7723. {
  7724. script_pushstrcopy(st,p->party.name);
  7725. }
  7726. else
  7727. {
  7728. script_pushconststr(st,"null");
  7729. }
  7730. return SCRIPT_CMD_SUCCESS;
  7731. }
  7732. /*==========================================
  7733. * Get the information of the members of a party by type
  7734. * @party_id, @type
  7735. * return by @type :
  7736. * - : nom des membres
  7737. * 1 : char_id des membres
  7738. * 2 : account_id des membres
  7739. *------------------------------------------*/
  7740. BUILDIN_FUNC(getpartymember)
  7741. {
  7742. struct party_data *p;
  7743. uint8 j = 0;
  7744. p = party_search(script_getnum(st,2));
  7745. if (p != nullptr) {
  7746. uint8 i, type = 0;
  7747. struct script_data *data = nullptr;
  7748. char *varname = nullptr;
  7749. if (script_hasdata(st,3))
  7750. type = script_getnum(st,3);
  7751. if (script_hasdata(st,4)) {
  7752. data = script_getdata(st, 4);
  7753. if (!data_isreference(data)) {
  7754. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7755. return SCRIPT_CMD_FAILURE;
  7756. }
  7757. varname = reference_getname(data);
  7758. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7759. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7760. return SCRIPT_CMD_FAILURE;
  7761. }
  7762. if (not_server_variable(*varname)) {
  7763. map_session_data *sd;
  7764. if (!script_rid2sd(sd)) {
  7765. ShowError("buildin_getpartymember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  7766. return SCRIPT_CMD_FAILURE;
  7767. }
  7768. }
  7769. }
  7770. for (i = 0; i < MAX_PARTY; i++) {
  7771. if (p->party.member[i].account_id) {
  7772. switch (type) {
  7773. case 2:
  7774. if (data)
  7775. setd_sub_num( st, nullptr, varname, j, p->party.member[i].account_id, data->ref );
  7776. else
  7777. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7778. break;
  7779. case 1:
  7780. if (data)
  7781. setd_sub_num( st, nullptr, varname, j, p->party.member[i].char_id, data->ref );
  7782. else
  7783. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7784. break;
  7785. default:
  7786. if (data)
  7787. setd_sub_str( st, nullptr, varname, j, p->party.member[i].name, data->ref );
  7788. else
  7789. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7790. break;
  7791. }
  7792. j++;
  7793. }
  7794. }
  7795. }
  7796. mapreg_setreg(add_str("$@partymembercount"),j);
  7797. script_pushint(st, j);
  7798. return SCRIPT_CMD_SUCCESS;
  7799. }
  7800. /*==========================================
  7801. * Retrieves party leader. if flag is specified,
  7802. * return some of the leader data. Otherwise, return name.
  7803. *------------------------------------------*/
  7804. BUILDIN_FUNC(getpartyleader)
  7805. {
  7806. int32 party_id, type = 0, i=0;
  7807. struct party_data *p;
  7808. party_id=script_getnum(st,2);
  7809. if( script_hasdata(st,3) )
  7810. type=script_getnum(st,3);
  7811. p=party_search(party_id);
  7812. if (p) //Search leader
  7813. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7814. if (!p || i == MAX_PARTY) { //leader not found
  7815. if (type)
  7816. script_pushint(st,-1);
  7817. else
  7818. script_pushconststr(st,"null");
  7819. return SCRIPT_CMD_SUCCESS;
  7820. }
  7821. switch (type) {
  7822. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7823. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7824. case 3: script_pushint(st,p->party.member[i].class_); break;
  7825. case 4: script_pushstrcopy( st, p->party.member[i].map ); break;
  7826. case 5: script_pushint(st,p->party.member[i].lv); break;
  7827. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7828. }
  7829. return SCRIPT_CMD_SUCCESS;
  7830. }
  7831. /*==========================================
  7832. * Return the name of the @guild_id
  7833. * null if not found
  7834. *------------------------------------------*/
  7835. BUILDIN_FUNC(getguildname)
  7836. {
  7837. int32 guild_id;
  7838. guild_id = script_getnum(st,2);
  7839. auto g = guild_search(guild_id);
  7840. if (g)
  7841. script_pushstrcopy(st,g->guild.name);
  7842. else
  7843. script_pushconststr(st,"null");
  7844. return SCRIPT_CMD_SUCCESS;
  7845. }
  7846. /*==========================================
  7847. * Return the name of the guild master of @guild_id
  7848. * null if not found
  7849. *------------------------------------------*/
  7850. BUILDIN_FUNC(getguildmaster)
  7851. {
  7852. int32 guild_id;
  7853. guild_id = script_getnum(st,2);
  7854. auto g = guild_search(guild_id);
  7855. if (g)
  7856. script_pushstrcopy(st,g->guild.member[0].name);
  7857. else
  7858. script_pushconststr(st,"null");
  7859. return SCRIPT_CMD_SUCCESS;
  7860. }
  7861. BUILDIN_FUNC(getguildmasterid)
  7862. {
  7863. int32 guild_id;
  7864. guild_id = script_getnum(st,2);
  7865. auto g = guild_search(guild_id);
  7866. if (g)
  7867. script_pushint(st,g->guild.member[0].char_id);
  7868. else
  7869. script_pushint(st,0);
  7870. return SCRIPT_CMD_SUCCESS;
  7871. }
  7872. /*==========================================
  7873. * Get char string information by type :
  7874. * Return by @type :
  7875. * 0 : char_name
  7876. * 1 : party_name or ""
  7877. * 2 : guild_name or ""
  7878. * 3 : map_name
  7879. * - : ""
  7880. * strcharinfo(<type>{,<char_id>})
  7881. *------------------------------------------*/
  7882. BUILDIN_FUNC(strcharinfo)
  7883. {
  7884. TBL_PC *sd;
  7885. int32 num;
  7886. struct party_data* p;
  7887. if (!script_charid2sd(3,sd)) {
  7888. script_pushconststr(st,"");
  7889. return SCRIPT_CMD_FAILURE;
  7890. }
  7891. num=script_getnum(st,2);
  7892. switch(num){
  7893. case 0:
  7894. script_pushstrcopy(st,sd->status.name);
  7895. break;
  7896. case 1:
  7897. if( ( p = party_search(sd->status.party_id) ) != nullptr ) {
  7898. script_pushstrcopy(st,p->party.name);
  7899. } else {
  7900. script_pushconststr(st,"");
  7901. }
  7902. break;
  7903. case 2:
  7904. if (sd->guild) {
  7905. script_pushstrcopy(st,sd->guild->guild.name);
  7906. } else {
  7907. script_pushconststr(st,"");
  7908. }
  7909. break;
  7910. case 3:
  7911. script_pushconststr(st,map_getmapdata(sd->bl.m)->name);
  7912. break;
  7913. default:
  7914. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7915. script_pushconststr(st,"");
  7916. break;
  7917. }
  7918. return SCRIPT_CMD_SUCCESS;
  7919. }
  7920. /*==========================================
  7921. * Get npc string information by type
  7922. * return by @type:
  7923. * 0 : name
  7924. * 1 : str#
  7925. * 2 : #str
  7926. * 3 : ::str
  7927. * 4 : map name
  7928. *------------------------------------------*/
  7929. BUILDIN_FUNC(strnpcinfo)
  7930. {
  7931. TBL_NPC* nd;
  7932. int32 num;
  7933. char *buf,*name=nullptr;
  7934. nd = map_id2nd(st->oid);
  7935. if (!nd) {
  7936. script_pushconststr(st, "");
  7937. return SCRIPT_CMD_SUCCESS;
  7938. }
  7939. num = script_getnum(st,2);
  7940. switch(num){
  7941. case 0: // display name
  7942. name = aStrdup(nd->name);
  7943. break;
  7944. case 1: // visible part of display name
  7945. if((buf = strchr(nd->name,'#')) != nullptr)
  7946. {
  7947. name = aStrdup(nd->name);
  7948. name[buf - nd->name] = 0;
  7949. } else // Return the name, there is no '#' present
  7950. name = aStrdup(nd->name);
  7951. break;
  7952. case 2: // # fragment
  7953. if((buf = strchr(nd->name,'#')) != nullptr)
  7954. name = aStrdup(buf+1);
  7955. break;
  7956. case 3: // unique name
  7957. name = aStrdup(nd->exname);
  7958. break;
  7959. case 4: // map name
  7960. if (nd->bl.m >= 0)
  7961. name = aStrdup(map_getmapdata(nd->bl.m)->name);
  7962. break;
  7963. }
  7964. if(name)
  7965. script_pushstr(st, name);
  7966. else
  7967. script_pushconststr(st, "");
  7968. return SCRIPT_CMD_SUCCESS;
  7969. }
  7970. /**
  7971. * getequipid({<equipment slot>,<char_id>})
  7972. **/
  7973. BUILDIN_FUNC(getequipid)
  7974. {
  7975. int32 i, num;
  7976. TBL_PC* sd;
  7977. if (!script_charid2sd(3, sd)) {
  7978. script_pushint(st,-1);
  7979. return SCRIPT_CMD_FAILURE;
  7980. }
  7981. if (script_hasdata(st, 2))
  7982. num = script_getnum(st, 2);
  7983. else
  7984. num = EQI_COMPOUND_ON;
  7985. if (num == EQI_COMPOUND_ON)
  7986. i = current_equip_item_index;
  7987. else if (equip_index_check(num)) // get inventory position of item
  7988. i = pc_checkequip(sd, equip_bitmask[num]);
  7989. else {
  7990. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7991. script_pushint(st,-1);
  7992. return SCRIPT_CMD_FAILURE;
  7993. }
  7994. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7995. script_pushint(st, sd->inventory_data[i]->nameid);
  7996. else
  7997. script_pushint(st, -1);
  7998. return SCRIPT_CMD_SUCCESS;
  7999. }
  8000. /*==========================================
  8001. * getequipuniqueid(<equipment slot>{,<char_id>})
  8002. *------------------------------------------*/
  8003. BUILDIN_FUNC(getequipuniqueid)
  8004. {
  8005. int32 i, num;
  8006. TBL_PC* sd;
  8007. struct item* item;
  8008. if (!script_charid2sd(3, sd)) {
  8009. script_pushconststr(st, "");
  8010. return SCRIPT_CMD_FAILURE;
  8011. }
  8012. num = script_getnum(st,2);
  8013. if ( !equip_index_check(num) ) {
  8014. script_pushconststr(st, "");
  8015. return SCRIPT_CMD_FAILURE;
  8016. }
  8017. // get inventory position of item
  8018. i = pc_checkequip(sd,equip_bitmask[num]);
  8019. if (i < 0) {
  8020. script_pushconststr(st, "");
  8021. return SCRIPT_CMD_FAILURE;
  8022. }
  8023. item = &sd->inventory.u.items_inventory[i];
  8024. if (item != 0) {
  8025. int32 maxlen = 256;
  8026. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  8027. memset(buf, 0, maxlen);
  8028. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  8029. script_pushstr(st, buf);
  8030. } else
  8031. script_pushconststr(st, "");
  8032. return SCRIPT_CMD_SUCCESS;
  8033. }
  8034. /*==========================================
  8035. * Get the equipement name at pos
  8036. * return item jname or ""
  8037. * getequipname(<equipment slot>{,<char_id>})
  8038. *------------------------------------------*/
  8039. BUILDIN_FUNC(getequipname)
  8040. {
  8041. int32 i, num;
  8042. TBL_PC* sd;
  8043. struct item_data* item;
  8044. if (!script_charid2sd(3, sd)) {
  8045. script_pushconststr(st, "");
  8046. return SCRIPT_CMD_FAILURE;
  8047. }
  8048. num = script_getnum(st,2);
  8049. if( !equip_index_check(num) )
  8050. {
  8051. script_pushconststr(st,"");
  8052. return SCRIPT_CMD_SUCCESS;
  8053. }
  8054. // get inventory position of item
  8055. i = pc_checkequip(sd,equip_bitmask[num]);
  8056. if( i < 0 )
  8057. {
  8058. script_pushconststr(st,"");
  8059. return SCRIPT_CMD_SUCCESS;
  8060. }
  8061. item = sd->inventory_data[i];
  8062. if( item != 0 )
  8063. script_pushstrcopy(st,item->ename.c_str());
  8064. else
  8065. script_pushconststr(st,"");
  8066. return SCRIPT_CMD_SUCCESS;
  8067. }
  8068. /*==========================================
  8069. * getbrokenid [Valaris]
  8070. * getbrokenid(<number>{,<char_id>})
  8071. *------------------------------------------*/
  8072. BUILDIN_FUNC(getbrokenid)
  8073. {
  8074. int32 i,num,id = 0,brokencounter = 0;
  8075. TBL_PC *sd;
  8076. if (!script_charid2sd(3, sd)) {
  8077. script_pushint(st,0);
  8078. return SCRIPT_CMD_FAILURE;
  8079. }
  8080. num = script_getnum(st,2);
  8081. for(i = 0; i < MAX_INVENTORY; i++) {
  8082. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8083. brokencounter++;
  8084. if(num == brokencounter){
  8085. id = sd->inventory.u.items_inventory[i].nameid;
  8086. break;
  8087. }
  8088. }
  8089. }
  8090. script_pushint(st,id);
  8091. return SCRIPT_CMD_SUCCESS;
  8092. }
  8093. /*==========================================
  8094. * repair [Valaris]
  8095. * repair <num>{,<char_id>};
  8096. *------------------------------------------*/
  8097. BUILDIN_FUNC(repair)
  8098. {
  8099. int32 i,num;
  8100. int32 repaircounter = 0;
  8101. TBL_PC *sd;
  8102. if (!script_charid2sd(3,sd))
  8103. return SCRIPT_CMD_FAILURE;
  8104. num = script_getnum(st,2);
  8105. for(i = 0; i < MAX_INVENTORY; i++) {
  8106. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8107. repaircounter++;
  8108. if(num == repaircounter) {
  8109. sd->inventory.u.items_inventory[i].attribute = 0;
  8110. clif_equiplist(sd);
  8111. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  8112. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  8113. break;
  8114. }
  8115. }
  8116. }
  8117. return SCRIPT_CMD_SUCCESS;
  8118. }
  8119. /*==========================================
  8120. * repairall {<char_id>}
  8121. *------------------------------------------*/
  8122. BUILDIN_FUNC(repairall)
  8123. {
  8124. int32 i, repaircounter = 0;
  8125. TBL_PC *sd;
  8126. if (!script_charid2sd(2,sd))
  8127. return SCRIPT_CMD_FAILURE;
  8128. for(i = 0; i < MAX_INVENTORY; i++)
  8129. {
  8130. if( sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8131. sd->inventory.u.items_inventory[i].attribute = 0;
  8132. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  8133. repaircounter++;
  8134. }
  8135. }
  8136. if(repaircounter)
  8137. {
  8138. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  8139. clif_equiplist(sd);
  8140. }
  8141. return SCRIPT_CMD_SUCCESS;
  8142. }
  8143. /*==========================================
  8144. * Chk if player have something equiped at pos
  8145. * getequipisequiped <pos>{,<char_id>}
  8146. *------------------------------------------*/
  8147. BUILDIN_FUNC(getequipisequiped)
  8148. {
  8149. int32 i = -1,num;
  8150. TBL_PC *sd;
  8151. num = script_getnum(st,2);
  8152. if (!script_charid2sd(3,sd))
  8153. return SCRIPT_CMD_FAILURE;
  8154. if ( equip_index_check(num) )
  8155. i=pc_checkequip(sd,equip_bitmask[num]);
  8156. if(i >= 0)
  8157. script_pushint(st,1);
  8158. else
  8159. script_pushint(st,0);
  8160. return SCRIPT_CMD_SUCCESS;
  8161. }
  8162. /*==========================================
  8163. * Chk if the player have something equiped at pos
  8164. * if so chk if this item ain't marked not refinable or rental
  8165. * return (npc)
  8166. * 1 : true
  8167. * 0 : false
  8168. * getequipisenableref(<equipment slot>{,<char_id>})
  8169. *------------------------------------------*/
  8170. BUILDIN_FUNC(getequipisenableref)
  8171. {
  8172. int32 i = -1,num;
  8173. TBL_PC *sd;
  8174. num = script_getnum(st,2);
  8175. if (!script_charid2sd(3, sd)) {
  8176. script_pushint(st,0);
  8177. return SCRIPT_CMD_FAILURE;
  8178. }
  8179. if( equip_index_check(num) )
  8180. i = pc_checkequip(sd,equip_bitmask[num]);
  8181. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  8182. script_pushint(st,1);
  8183. else
  8184. script_pushint(st,0);
  8185. return SCRIPT_CMD_SUCCESS;
  8186. }
  8187. /*==========================================
  8188. * Get the item refined value at pos
  8189. * return (npc)
  8190. * x : refine amount
  8191. * 0 : false (not refined)
  8192. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  8193. *------------------------------------------*/
  8194. BUILDIN_FUNC(getequiprefinerycnt)
  8195. {
  8196. int32 i = -1,num;
  8197. TBL_PC *sd;
  8198. num = script_getnum(st,2);
  8199. if (!script_charid2sd(3, sd)) {
  8200. script_pushint(st,0);
  8201. return SCRIPT_CMD_FAILURE;
  8202. }
  8203. if (equip_index_check(num))
  8204. i=pc_checkequip(sd,equip_bitmask[num]);
  8205. if(i >= 0)
  8206. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  8207. else
  8208. script_pushint(st,0);
  8209. return SCRIPT_CMD_SUCCESS;
  8210. }
  8211. /*==========================================
  8212. * Get the weapon level value at pos
  8213. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  8214. * return (npc)
  8215. * x : weapon level
  8216. * 0 : false
  8217. * getequipweaponlv({<equipment slot>{,<char_id>}})
  8218. *------------------------------------------*/
  8219. BUILDIN_FUNC(getequipweaponlv)
  8220. {
  8221. int32 num;
  8222. if( script_hasdata( st, 2 ) ){
  8223. num = script_getnum(st, 2);
  8224. }else{
  8225. num = EQI_COMPOUND_ON;
  8226. }
  8227. map_session_data* sd;
  8228. if (!script_charid2sd(3, sd)) {
  8229. script_pushint(st, 0);
  8230. return SCRIPT_CMD_FAILURE;
  8231. }
  8232. int32 i = -1;
  8233. if( num == EQI_COMPOUND_ON ){
  8234. if( current_equip_item_index == -1 ){
  8235. script_pushint(st, 0);
  8236. return SCRIPT_CMD_FAILURE;
  8237. }
  8238. i = current_equip_item_index;
  8239. }else if (equip_index_check(num))
  8240. i = pc_checkequip(sd, equip_bitmask[num]);
  8241. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON ){
  8242. script_pushint( st, sd->inventory_data[i]->weapon_level );
  8243. }else{
  8244. script_pushint( st, 0 );
  8245. }
  8246. return SCRIPT_CMD_SUCCESS;
  8247. }
  8248. BUILDIN_FUNC(getequiparmorlv){
  8249. int32 num;
  8250. if( script_hasdata( st, 2 ) ){
  8251. num = script_getnum(st, 2);
  8252. }else{
  8253. num = EQI_COMPOUND_ON;
  8254. }
  8255. map_session_data* sd;
  8256. if( !script_charid2sd( 3, sd ) ){
  8257. script_pushint( st, 0 );
  8258. return SCRIPT_CMD_FAILURE;
  8259. }
  8260. int32 i = -1;
  8261. if( num == EQI_COMPOUND_ON ){
  8262. if( current_equip_item_index == -1 ){
  8263. script_pushint( st, 0 );
  8264. return SCRIPT_CMD_FAILURE;
  8265. }
  8266. i = current_equip_item_index;
  8267. }else if( equip_index_check( num ) ){
  8268. i = pc_checkequip( sd, equip_bitmask[num] );
  8269. }else{
  8270. ShowError( "buildin_getequiparmorlv: unsupported equip index %d.\n", num );
  8271. script_pushint( st, 0 );
  8272. return SCRIPT_CMD_FAILURE;
  8273. }
  8274. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_ARMOR ){
  8275. script_pushint( st, sd->inventory_data[i]->armor_level );
  8276. }else{
  8277. script_pushint( st, 0 );
  8278. }
  8279. return SCRIPT_CMD_SUCCESS;
  8280. }
  8281. /*==========================================
  8282. * Get the item refine chance (from refine.txt) for item at pos
  8283. * return (npc)
  8284. * x : refine chance
  8285. * 0 : false (max refine level or unequip..)
  8286. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8287. *------------------------------------------*/
  8288. BUILDIN_FUNC(getequippercentrefinery)
  8289. {
  8290. int32 i = -1,num;
  8291. bool enriched = false;
  8292. TBL_PC *sd;
  8293. num = script_getnum(st,2);
  8294. if (script_hasdata(st, 3))
  8295. enriched = script_getnum(st, 3) != 0;
  8296. if (!script_charid2sd(4, sd)) {
  8297. script_pushint(st,0);
  8298. return SCRIPT_CMD_FAILURE;
  8299. }
  8300. if (equip_index_check(num))
  8301. i = pc_checkequip(sd,equip_bitmask[num]);
  8302. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid) {
  8303. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  8304. if( info == nullptr ){
  8305. script_pushint( st, 0 );
  8306. return SCRIPT_CMD_SUCCESS;
  8307. }
  8308. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, (uint16)( enriched ? REFINE_COST_ENRICHED : REFINE_COST_NORMAL ) );
  8309. if( cost == nullptr ){
  8310. script_pushint( st, 0 );
  8311. return SCRIPT_CMD_SUCCESS;
  8312. }
  8313. script_pushint( st, cost->chance / 100 );
  8314. }
  8315. else
  8316. script_pushint(st,0);
  8317. return SCRIPT_CMD_SUCCESS;
  8318. }
  8319. /*==========================================
  8320. * Refine +1 item at pos and log and display refine
  8321. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8322. *------------------------------------------*/
  8323. BUILDIN_FUNC(successrefitem) {
  8324. short i = -1, up = 1;
  8325. int32 pos;
  8326. TBL_PC *sd;
  8327. pos = script_getnum(st,2);
  8328. if (!script_charid2sd(4, sd)) {
  8329. script_pushint(st, -1);
  8330. return SCRIPT_CMD_FAILURE;
  8331. }
  8332. if (script_hasdata(st, 3))
  8333. up = script_getnum(st, 3);
  8334. if (equip_index_check(pos))
  8335. i = pc_checkequip(sd,equip_bitmask[pos]);
  8336. if (i >= 0) {
  8337. uint32 ep = sd->inventory.u.items_inventory[i].equip;
  8338. //Logs items, got from (N)PC scripts [Lupus]
  8339. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8340. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8341. script_pushint(st, MAX_REFINE);
  8342. return SCRIPT_CMD_SUCCESS;
  8343. }
  8344. sd->inventory.u.items_inventory[i].refine += up;
  8345. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8346. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8347. clif_refine( *sd, i, ITEMREFINING_SUCCESS );
  8348. clif_delitem( *sd, i, 1, 3 );
  8349. //Logs items, got from (N)PC scripts [Lupus]
  8350. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8351. clif_additem(sd,i,1,0);
  8352. pc_equipitem(sd,i,ep);
  8353. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  8354. if( sd->inventory_data[i]->type == IT_WEAPON ){
  8355. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->weapon_level, sd->inventory.u.items_inventory[i].refine);
  8356. }
  8357. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8358. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8359. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]) &&
  8360. sd->inventory_data[i]->type == IT_WEAPON )
  8361. { // Fame point system [DracoRPG]
  8362. switch( sd->inventory_data[i]->weapon_level ){
  8363. case 1:
  8364. pc_addfame(*sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8365. break;
  8366. case 2:
  8367. pc_addfame(*sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8368. break;
  8369. case 3:
  8370. pc_addfame(*sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8371. break;
  8372. }
  8373. }
  8374. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8375. return SCRIPT_CMD_SUCCESS;
  8376. }
  8377. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8378. script_pushint(st, -1);
  8379. return SCRIPT_CMD_FAILURE;
  8380. }
  8381. /*==========================================
  8382. * Show a failed Refine +1 attempt
  8383. * failedrefitem <equipment slot>{,<char_id>}
  8384. *------------------------------------------*/
  8385. BUILDIN_FUNC(failedrefitem) {
  8386. short i = -1;
  8387. int32 pos;
  8388. TBL_PC *sd;
  8389. pos = script_getnum(st,2);
  8390. if (!script_charid2sd(3, sd)) {
  8391. script_pushint(st, 0);
  8392. return SCRIPT_CMD_FAILURE;
  8393. }
  8394. if (equip_index_check(pos))
  8395. i = pc_checkequip(sd,equip_bitmask[pos]);
  8396. if (i >= 0) {
  8397. sd->inventory.u.items_inventory[i].refine = 0;
  8398. pc_unequipitem(sd,i,3); //recalculate bonus
  8399. clif_refine( *sd, i, ITEMREFINING_FAILURE);
  8400. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8401. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  8402. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8403. script_pushint(st, 1);
  8404. return SCRIPT_CMD_SUCCESS;
  8405. }
  8406. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8407. script_pushint(st, 0);
  8408. return SCRIPT_CMD_FAILURE;
  8409. }
  8410. /*==========================================
  8411. * Downgrades an Equipment Part by -1 . [Masao]
  8412. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8413. *------------------------------------------*/
  8414. BUILDIN_FUNC(downrefitem) {
  8415. short i = -1, down = 1;
  8416. int32 pos;
  8417. TBL_PC *sd;
  8418. if (!script_charid2sd(4, sd)) {
  8419. script_pushint(st, -1);
  8420. return SCRIPT_CMD_FAILURE;
  8421. }
  8422. pos = script_getnum(st,2);
  8423. if (script_hasdata(st, 3))
  8424. down = script_getnum(st, 3);
  8425. if (equip_index_check(pos))
  8426. i = pc_checkequip(sd,equip_bitmask[pos]);
  8427. if (i >= 0) {
  8428. uint32 ep = sd->inventory.u.items_inventory[i].equip;
  8429. //Logs items, got from (N)PC scripts [Lupus]
  8430. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8431. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8432. sd->inventory.u.items_inventory[i].refine -= down;
  8433. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8434. clif_refine( *sd, i, ITEMREFINING_DOWNGRADE );
  8435. clif_delitem( *sd, i, 1, 3 );
  8436. //Logs items, got from (N)PC scripts [Lupus]
  8437. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8438. clif_additem(sd,i,1,0);
  8439. pc_equipitem(sd,i,ep);
  8440. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  8441. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8442. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8443. return SCRIPT_CMD_SUCCESS;
  8444. }
  8445. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8446. script_pushint(st, -1);
  8447. return SCRIPT_CMD_FAILURE;
  8448. }
  8449. /**
  8450. * Delete the item equipped at pos.
  8451. * delequip <equipment slot>{,<char_id>};
  8452. **/
  8453. BUILDIN_FUNC(delequip) {
  8454. short i = -1;
  8455. int32 pos;
  8456. int8 ret;
  8457. TBL_PC *sd;
  8458. pos = script_getnum(st,2);
  8459. if (!script_charid2sd(3,sd)) {
  8460. st->state = END;
  8461. return SCRIPT_CMD_FAILURE;
  8462. }
  8463. if (equip_index_check(pos))
  8464. i = pc_checkequip(sd,equip_bitmask[pos]);
  8465. if (i >= 0) {
  8466. pc_unequipitem(sd,i,3); //recalculate bonus
  8467. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8468. }
  8469. else {
  8470. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8471. st->state = END;
  8472. return SCRIPT_CMD_FAILURE;
  8473. }
  8474. script_pushint(st,ret);
  8475. return SCRIPT_CMD_SUCCESS;
  8476. }
  8477. /**
  8478. * Break the item equipped at pos.
  8479. * breakequip <equipment slot>{,<char_id>};
  8480. **/
  8481. BUILDIN_FUNC(breakequip) {
  8482. short i = -1;
  8483. int32 pos;
  8484. TBL_PC *sd;
  8485. pos = script_getnum(st,2);
  8486. if (!script_charid2sd(3,sd))
  8487. return SCRIPT_CMD_FAILURE;
  8488. if (equip_index_check(pos))
  8489. i = pc_checkequip(sd,equip_bitmask[pos]);
  8490. if (i >= 0) {
  8491. sd->inventory.u.items_inventory[i].attribute = 1;
  8492. pc_unequipitem(sd,i,3);
  8493. clif_equiplist(sd);
  8494. script_pushint(st,1);
  8495. return SCRIPT_CMD_SUCCESS;
  8496. }
  8497. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8498. script_pushint(st,0);
  8499. return SCRIPT_CMD_FAILURE;
  8500. }
  8501. /**
  8502. * statusup <stat>{,<char_id>};
  8503. **/
  8504. BUILDIN_FUNC(statusup)
  8505. {
  8506. int32 type;
  8507. TBL_PC *sd;
  8508. type = script_getnum(st,2);
  8509. if (!script_charid2sd(3, sd))
  8510. return SCRIPT_CMD_FAILURE;
  8511. pc_statusup(sd, type, 1);
  8512. return SCRIPT_CMD_SUCCESS;
  8513. }
  8514. /**
  8515. * statusup2 <stat>,<amount>{,<char_id>};
  8516. **/
  8517. BUILDIN_FUNC(statusup2)
  8518. {
  8519. int32 type,val;
  8520. TBL_PC *sd;
  8521. type=script_getnum(st,2);
  8522. val=script_getnum(st,3);
  8523. if (!script_charid2sd(4, sd))
  8524. return SCRIPT_CMD_FAILURE;
  8525. pc_statusup2(sd,type,val);
  8526. return SCRIPT_CMD_SUCCESS;
  8527. }
  8528. /**
  8529. * traitstatusup <stat>{,<char_id>};
  8530. **/
  8531. BUILDIN_FUNC(traitstatusup)
  8532. {
  8533. map_session_data *sd;
  8534. if (!script_charid2sd(3, sd))
  8535. return SCRIPT_CMD_FAILURE;
  8536. int32 type = script_getnum( st, 2 );
  8537. if( type < SP_POW || type > SP_CRT ){
  8538. ShowError( "buildin_traitstatusup: Unknown trait type %d\n", type );
  8539. return SCRIPT_CMD_FAILURE;
  8540. }
  8541. pc_traitstatusup( sd, type, 1 );
  8542. return SCRIPT_CMD_SUCCESS;
  8543. }
  8544. /**
  8545. * traitstatusup2 <stat>,<amount>{,<char_id>};
  8546. **/
  8547. BUILDIN_FUNC(traitstatusup2)
  8548. {
  8549. map_session_data *sd;
  8550. if (!script_charid2sd(4, sd))
  8551. return SCRIPT_CMD_FAILURE;
  8552. int32 type = script_getnum( st, 2 );
  8553. if( type < SP_POW || type > SP_CRT ){
  8554. ShowError( "buildin_traitstatusup2: Unknown trait type %d\n", type );
  8555. return SCRIPT_CMD_FAILURE;
  8556. }
  8557. pc_traitstatusup2( sd, type, script_getnum( st, 3 ) );
  8558. return SCRIPT_CMD_SUCCESS;
  8559. }
  8560. /// See 'doc/item_bonus.txt'
  8561. ///
  8562. /// bonus <bonus type>,<val1>;
  8563. /// bonus2 <bonus type>,<val1>,<val2>;
  8564. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8565. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8566. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8567. BUILDIN_FUNC(bonus)
  8568. {
  8569. int32 type;
  8570. int32 val1 = 0;
  8571. int32 val2 = 0;
  8572. int32 val3 = 0;
  8573. int32 val4 = 0;
  8574. int32 val5 = 0;
  8575. TBL_PC* sd;
  8576. if( !script_rid2sd(sd) )
  8577. return SCRIPT_CMD_SUCCESS; // no player attached
  8578. type = script_getnum(st,2);
  8579. switch( type ) {
  8580. case SP_AUTOSPELL:
  8581. case SP_AUTOSPELL_WHENHIT:
  8582. case SP_AUTOSPELL_ONSKILL:
  8583. case SP_SKILL_ATK:
  8584. case SP_SKILL_HEAL:
  8585. case SP_SKILL_HEAL2:
  8586. case SP_ADD_SKILL_BLOW:
  8587. case SP_CASTRATE:
  8588. case SP_ADDEFF_ONSKILL:
  8589. case SP_SKILL_USE_SP_RATE:
  8590. case SP_SKILL_COOLDOWN:
  8591. case SP_SKILL_FIXEDCAST:
  8592. case SP_SKILL_VARIABLECAST:
  8593. case SP_VARCASTRATE:
  8594. case SP_FIXCASTRATE:
  8595. case SP_SKILL_DELAY:
  8596. case SP_SKILL_USE_SP:
  8597. case SP_SUB_SKILL:
  8598. // these bonuses support skill names
  8599. if (script_isstring(st, 3)) {
  8600. const char *name = script_getstr(st, 3);
  8601. if (!(val1 = skill_name2id(name))) {
  8602. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8603. return SCRIPT_CMD_FAILURE;
  8604. }
  8605. } else {
  8606. val1 = script_getnum(st, 3);
  8607. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8608. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8609. return SCRIPT_CMD_FAILURE;
  8610. }
  8611. }
  8612. break;
  8613. default:
  8614. if (script_hasdata(st, 3))
  8615. val1 = script_getnum(st, 3);
  8616. break;
  8617. }
  8618. switch( script_lastdata(st)-2 ) {
  8619. case 0:
  8620. case 1:
  8621. pc_bonus(sd, type, val1);
  8622. break;
  8623. case 2:
  8624. val2 = script_getnum(st,4);
  8625. pc_bonus2(sd, type, val1, val2);
  8626. break;
  8627. case 3:
  8628. val2 = script_getnum(st,4);
  8629. val3 = script_getnum(st,5);
  8630. pc_bonus3(sd, type, val1, val2, val3);
  8631. break;
  8632. case 4:
  8633. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8634. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8635. else
  8636. val2 = script_getnum(st,4);
  8637. val3 = script_getnum(st,5);
  8638. val4 = script_getnum(st,6);
  8639. pc_bonus4(sd, type, val1, val2, val3, val4);
  8640. break;
  8641. case 5:
  8642. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8643. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8644. else
  8645. val2 = script_getnum(st,4);
  8646. val3 = script_getnum(st,5);
  8647. val4 = script_getnum(st,6);
  8648. val5 = script_getnum(st,7);
  8649. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8650. break;
  8651. default:
  8652. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8653. break;
  8654. }
  8655. return SCRIPT_CMD_SUCCESS;
  8656. }
  8657. BUILDIN_FUNC(autobonus)
  8658. {
  8659. uint32 dur, pos;
  8660. short rate;
  8661. uint16 atk_type = 0;
  8662. TBL_PC* sd;
  8663. const char *bonus_script, *other_script = nullptr;
  8664. if( !script_rid2sd(sd) )
  8665. return SCRIPT_CMD_SUCCESS; // no player attached
  8666. if (current_equip_combo_pos)
  8667. pos = current_equip_combo_pos;
  8668. else
  8669. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8670. rate = script_getnum(st,3);
  8671. dur = script_getnum(st,4);
  8672. bonus_script = script_getstr(st,2);
  8673. if( !rate || !dur || !pos || !bonus_script )
  8674. return SCRIPT_CMD_SUCCESS;
  8675. if( script_hasdata(st,5) )
  8676. atk_type = script_getnum(st,5);
  8677. if( script_hasdata(st,6) )
  8678. other_script = script_getstr(st,6);
  8679. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8680. {
  8681. script_add_autobonus(bonus_script);
  8682. if( other_script )
  8683. script_add_autobonus(other_script);
  8684. }
  8685. return SCRIPT_CMD_SUCCESS;
  8686. }
  8687. BUILDIN_FUNC(autobonus2)
  8688. {
  8689. uint32 dur, pos;
  8690. short rate;
  8691. uint16 atk_type = 0;
  8692. TBL_PC* sd;
  8693. const char *bonus_script, *other_script = nullptr;
  8694. if( !script_rid2sd(sd) )
  8695. return SCRIPT_CMD_SUCCESS; // no player attached
  8696. if (current_equip_combo_pos)
  8697. pos = current_equip_combo_pos;
  8698. else
  8699. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8700. rate = script_getnum(st,3);
  8701. dur = script_getnum(st,4);
  8702. bonus_script = script_getstr(st,2);
  8703. if( !rate || !dur || !pos || !bonus_script )
  8704. return SCRIPT_CMD_SUCCESS;
  8705. if( script_hasdata(st,5) )
  8706. atk_type = script_getnum(st,5);
  8707. if( script_hasdata(st,6) )
  8708. other_script = script_getstr(st,6);
  8709. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8710. {
  8711. script_add_autobonus(bonus_script);
  8712. if( other_script )
  8713. script_add_autobonus(other_script);
  8714. }
  8715. return SCRIPT_CMD_SUCCESS;
  8716. }
  8717. BUILDIN_FUNC(autobonus3)
  8718. {
  8719. uint32 dur, pos;
  8720. short rate;
  8721. uint16 skill_id = 0;
  8722. TBL_PC* sd;
  8723. const char *bonus_script, *other_script = nullptr;
  8724. if( !script_rid2sd(sd) )
  8725. return SCRIPT_CMD_SUCCESS; // no player attached
  8726. if (current_equip_combo_pos)
  8727. pos = current_equip_combo_pos;
  8728. else
  8729. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8730. rate = script_getnum(st,3);
  8731. dur = script_getnum(st,4);
  8732. if (script_isstring(st, 5)) {
  8733. const char *name = script_getstr(st, 5);
  8734. if (!(skill_id = skill_name2id(name))) {
  8735. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8736. return SCRIPT_CMD_FAILURE;
  8737. }
  8738. } else {
  8739. skill_id = script_getnum(st, 5);
  8740. if (!skill_get_index(skill_id)) {
  8741. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8742. return SCRIPT_CMD_FAILURE;
  8743. }
  8744. }
  8745. bonus_script = script_getstr(st,2);
  8746. if( !rate || !dur || !pos || !bonus_script )
  8747. return SCRIPT_CMD_SUCCESS;
  8748. if( script_hasdata(st,6) )
  8749. other_script = script_getstr(st,6);
  8750. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8751. {
  8752. script_add_autobonus(bonus_script);
  8753. if( other_script )
  8754. script_add_autobonus(other_script);
  8755. }
  8756. return SCRIPT_CMD_SUCCESS;
  8757. }
  8758. BUILDIN_FUNC(petautobonus) {
  8759. map_session_data *sd;
  8760. if (!script_rid2sd(sd))
  8761. return SCRIPT_CMD_FAILURE; // No player attached
  8762. const char *command = script_getfuncname(st);
  8763. if (sd->pd == nullptr) {
  8764. ShowError("buildin_%s: Requires an active pet.\n", command);
  8765. return SCRIPT_CMD_FAILURE; // No pet attached to player
  8766. }
  8767. std::string bonus_script = script_getstr(st, 2);
  8768. int16 rate = script_getnum(st, 3);
  8769. uint32 dur = script_getnum(st, 4);
  8770. if (!rate || !dur || bonus_script.empty())
  8771. return SCRIPT_CMD_FAILURE;
  8772. uint16 atk_type = 0;
  8773. std::string other_script = {};
  8774. bool bonus2 = false;
  8775. if (strcmpi(command, "petautobonus2") == 0)
  8776. bonus2 = true;
  8777. if (script_hasdata(st, 5))
  8778. atk_type = script_getnum(st, 5);
  8779. if (script_hasdata(st, 6))
  8780. other_script = script_getstr(st, 6);
  8781. if (pet_addautobonus(bonus2 ? sd->pd->autobonus2 : sd->pd->autobonus, bonus_script, rate, dur, atk_type, other_script, false)) {
  8782. script_add_petautobonus(bonus_script);
  8783. if (!other_script.empty())
  8784. script_add_petautobonus(other_script);
  8785. }
  8786. return SCRIPT_CMD_SUCCESS;
  8787. }
  8788. BUILDIN_FUNC(petautobonus3) {
  8789. map_session_data *sd;
  8790. if (!script_rid2sd(sd))
  8791. return SCRIPT_CMD_SUCCESS; // No player attached
  8792. if (sd->pd == nullptr) {
  8793. ShowError("buildin_petautobonus3: Requires an active pet.\n");
  8794. return SCRIPT_CMD_FAILURE; // No pet attached to player
  8795. }
  8796. std::string bonus_script = script_getstr(st, 2);
  8797. int16 rate = script_getnum(st, 3);
  8798. uint32 dur = script_getnum(st, 4);
  8799. if (!rate || !dur || bonus_script.empty())
  8800. return SCRIPT_CMD_SUCCESS;
  8801. uint16 skill_id = 0;
  8802. std::string other_script = {};
  8803. if (script_isstring(st, 5)) {
  8804. const char *name = script_getstr(st, 5);
  8805. if (!(skill_id = skill_name2id(name))) {
  8806. ShowError("buildin_petautobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8807. return SCRIPT_CMD_FAILURE;
  8808. }
  8809. } else {
  8810. skill_id = script_getnum(st, 5);
  8811. if (!skill_get_index(skill_id)) {
  8812. ShowError("buildin_petautobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8813. return SCRIPT_CMD_FAILURE;
  8814. }
  8815. }
  8816. if (script_hasdata(st, 6))
  8817. other_script = script_getstr(st, 6);
  8818. if (pet_addautobonus(sd->pd->autobonus3, bonus_script, rate, dur, skill_id, other_script, true)) {
  8819. script_add_petautobonus(bonus_script);
  8820. if (!other_script.empty())
  8821. script_add_petautobonus(other_script);
  8822. }
  8823. return SCRIPT_CMD_SUCCESS;
  8824. }
  8825. BUILDIN_FUNC(plagiarizeskill)
  8826. {
  8827. TBL_PC *sd;
  8828. if (script_rid2sd(sd))
  8829. script_pushint(st, pc_skill_plagiarism(*sd, script_getnum(st, 2), script_getnum(st, 3)));
  8830. return SCRIPT_CMD_SUCCESS;
  8831. }
  8832. BUILDIN_FUNC(plagiarizeskillreset)
  8833. {
  8834. TBL_PC *sd;
  8835. if (script_rid2sd(sd))
  8836. script_pushint(st, pc_skill_plagiarism_reset(*sd, script_getnum(st, 2)));
  8837. return SCRIPT_CMD_SUCCESS;
  8838. }
  8839. /// Changes the level of a player skill.
  8840. /// <flag> defaults to 1
  8841. /// <flag>=0 : set the level of the skill
  8842. /// <flag>=1 : set the temporary level of the skill
  8843. /// <flag>=2 : add to the level of the skill
  8844. ///
  8845. /// skill <skill id>,<level>,<flag>
  8846. /// skill <skill id>,<level>
  8847. /// skill "<skill name>",<level>,<flag>
  8848. /// skill "<skill name>",<level>
  8849. BUILDIN_FUNC(skill)
  8850. {
  8851. int32 id;
  8852. int32 level;
  8853. int32 flag = ADDSKILL_TEMP;
  8854. TBL_PC* sd;
  8855. const char* command = script_getfuncname(st);
  8856. if( !script_rid2sd(sd) )
  8857. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8858. if (strcmpi(command, "addtoskill") == 0)
  8859. flag = ADDSKILL_TEMP_ADDLEVEL;
  8860. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8861. level = script_getnum(st,3);
  8862. if( script_hasdata(st,4) )
  8863. flag = script_getnum(st,4);
  8864. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8865. return SCRIPT_CMD_SUCCESS;
  8866. }
  8867. /// Increases the level of a guild skill.
  8868. ///
  8869. /// guildskill <skill id>,<amount>;
  8870. /// guildskill "<skill name>",<amount>;
  8871. BUILDIN_FUNC(guildskill)
  8872. {
  8873. int32 id;
  8874. int32 level;
  8875. TBL_PC* sd;
  8876. int32 i;
  8877. if( !script_rid2sd(sd) )
  8878. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8879. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8880. level = script_getnum(st,3);
  8881. for( i=0; i < level; i++ )
  8882. guild_skillup(sd, id);
  8883. return SCRIPT_CMD_SUCCESS;
  8884. }
  8885. /// Returns the level of the player skill.
  8886. ///
  8887. /// getskilllv(<skill id>) -> <level>
  8888. /// getskilllv("<skill name>") -> <level>
  8889. BUILDIN_FUNC(getskilllv)
  8890. {
  8891. int32 id;
  8892. TBL_PC* sd;
  8893. if( !script_rid2sd(sd) )
  8894. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8895. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8896. script_pushint(st, pc_checkskill(sd,id));
  8897. return SCRIPT_CMD_SUCCESS;
  8898. }
  8899. /// Returns the level of the guild skill.
  8900. ///
  8901. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8902. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8903. BUILDIN_FUNC(getgdskilllv)
  8904. {
  8905. int32 guild_id;
  8906. uint16 skill_id;
  8907. guild_id = script_getnum(st,2);
  8908. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8909. auto g = guild_search(guild_id);
  8910. if (!g)
  8911. script_pushint(st, -1);
  8912. else
  8913. script_pushint(st, guild_checkskill(g->guild,skill_id));
  8914. return SCRIPT_CMD_SUCCESS;
  8915. }
  8916. /// Returns the 'basic_skill_check' setting.
  8917. /// This config determines if the server checks the skill level of NV_BASIC
  8918. /// before allowing the basic actions.
  8919. ///
  8920. /// basicskillcheck() -> <bool>
  8921. BUILDIN_FUNC(basicskillcheck)
  8922. {
  8923. script_pushint(st, battle_config.basic_skill_check);
  8924. return SCRIPT_CMD_SUCCESS;
  8925. }
  8926. /// Returns the GM level of the player.
  8927. ///
  8928. /// getgmlevel({<char_id>}) -> <level>
  8929. BUILDIN_FUNC(getgmlevel)
  8930. {
  8931. TBL_PC* sd;
  8932. if (!script_charid2sd(2,sd))
  8933. return SCRIPT_CMD_FAILURE;
  8934. script_pushint(st, pc_get_group_level(sd));
  8935. return SCRIPT_CMD_SUCCESS;
  8936. }
  8937. /// Returns the group ID of the player.
  8938. ///
  8939. /// getgroupid({<char_id>}) -> <int>
  8940. BUILDIN_FUNC(getgroupid)
  8941. {
  8942. TBL_PC* sd;
  8943. if (!script_charid2sd(2,sd))
  8944. return SCRIPT_CMD_FAILURE;
  8945. script_pushint(st, pc_get_group_id(sd));
  8946. return SCRIPT_CMD_SUCCESS;
  8947. }
  8948. /// Terminates the execution of this script instance.
  8949. ///
  8950. /// end
  8951. BUILDIN_FUNC(end)
  8952. {
  8953. TBL_PC* sd;
  8954. sd = map_id2sd(st->rid);
  8955. st->state = END;
  8956. npc_data* nd = map_id2nd( st->oid );
  8957. if( nd != nullptr && nd->dynamicnpc.owner_char_id != 0 ){
  8958. nd->dynamicnpc.last_interaction = gettick();
  8959. }
  8960. if( st->mes_active )
  8961. st->mes_active = 0;
  8962. if (sd){
  8963. if (sd->state.callshop == 0){
  8964. // If a menu/select/prompt is active, close it.
  8965. clif_scriptclose( *sd, st->oid );
  8966. }else
  8967. sd->state.callshop = 0;
  8968. }
  8969. return SCRIPT_CMD_SUCCESS;
  8970. }
  8971. /// Checks if the player has that effect state (option).
  8972. ///
  8973. /// checkoption(<option>{,<char_id>}) -> <bool>
  8974. BUILDIN_FUNC(checkoption)
  8975. {
  8976. int32 option;
  8977. TBL_PC* sd;
  8978. if (!script_charid2sd(3,sd))
  8979. return SCRIPT_CMD_FAILURE;
  8980. option = script_getnum(st,2);
  8981. if( sd->sc.option&option )
  8982. script_pushint(st, 1);
  8983. else
  8984. script_pushint(st, 0);
  8985. return SCRIPT_CMD_SUCCESS;
  8986. }
  8987. /// Checks if the player is in that body state (opt1).
  8988. ///
  8989. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8990. BUILDIN_FUNC(checkoption1)
  8991. {
  8992. int32 opt1;
  8993. TBL_PC* sd;
  8994. if (!script_charid2sd(3,sd))
  8995. return SCRIPT_CMD_FAILURE;
  8996. opt1 = script_getnum(st,2);
  8997. if( sd->sc.opt1 == opt1 )
  8998. script_pushint(st, 1);
  8999. else
  9000. script_pushint(st, 0);
  9001. return SCRIPT_CMD_SUCCESS;
  9002. }
  9003. /// Checks if the player has that health state (opt2).
  9004. ///
  9005. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  9006. BUILDIN_FUNC(checkoption2)
  9007. {
  9008. int32 opt2;
  9009. TBL_PC* sd;
  9010. if (!script_charid2sd(3,sd))
  9011. return SCRIPT_CMD_FAILURE;
  9012. opt2 = script_getnum(st,2);
  9013. if( sd->sc.opt2&opt2 )
  9014. script_pushint(st, 1);
  9015. else
  9016. script_pushint(st, 0);
  9017. return SCRIPT_CMD_SUCCESS;
  9018. }
  9019. /// Changes the effect state (option) of the player.
  9020. /// <flag> defaults to 1
  9021. /// <flag>=0 : removes the option
  9022. /// <flag>=other : adds the option
  9023. ///
  9024. /// setoption <option>{,<flag>{,<char_id>}};
  9025. BUILDIN_FUNC(setoption)
  9026. {
  9027. int32 option;
  9028. int32 flag = 1;
  9029. TBL_PC* sd;
  9030. if (!script_charid2sd(4,sd))
  9031. return SCRIPT_CMD_FAILURE;
  9032. option = script_getnum(st,2);
  9033. if( script_hasdata(st,3) )
  9034. flag = script_getnum(st,3);
  9035. else if( !option ){// Request to remove everything.
  9036. flag = 0;
  9037. option = OPTION_FALCON|OPTION_RIDING;
  9038. #ifndef NEW_CARTS
  9039. option |= OPTION_CART;
  9040. #endif
  9041. }
  9042. if( flag ){// Add option
  9043. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  9044. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  9045. pc_setoption(sd, sd->sc.option|option);
  9046. } else// Remove option
  9047. pc_setoption(sd, sd->sc.option&~option);
  9048. return SCRIPT_CMD_SUCCESS;
  9049. }
  9050. /// Returns if the player has a cart.
  9051. ///
  9052. /// checkcart({char_id}) -> <bool>
  9053. ///
  9054. /// @author Valaris
  9055. BUILDIN_FUNC(checkcart)
  9056. {
  9057. TBL_PC* sd;
  9058. if (!script_charid2sd(2,sd))
  9059. return SCRIPT_CMD_FAILURE;
  9060. if( pc_iscarton(sd) )
  9061. script_pushint(st, 1);
  9062. else
  9063. script_pushint(st, 0);
  9064. return SCRIPT_CMD_SUCCESS;
  9065. }
  9066. /// Sets the cart of the player.
  9067. /// <type> defaults to 1
  9068. /// <type>=0 : removes the cart
  9069. /// <type>=1 : Normal cart
  9070. /// <type>=2 : Wooden cart
  9071. /// <type>=3 : Covered cart with flowers and ferns
  9072. /// <type>=4 : Wooden cart with a Panda doll on the back
  9073. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  9074. ///
  9075. /// setcart {<type>{,<char_id>}};
  9076. BUILDIN_FUNC(setcart)
  9077. {
  9078. int32 type = 1;
  9079. TBL_PC* sd;
  9080. if (!script_charid2sd(3,sd))
  9081. return SCRIPT_CMD_FAILURE;
  9082. if( script_hasdata(st,2) )
  9083. type = script_getnum(st,2);
  9084. pc_setcart(sd, type);
  9085. return SCRIPT_CMD_SUCCESS;
  9086. }
  9087. /// Returns if the player has a falcon.
  9088. ///
  9089. /// checkfalcon({char_id}) -> <bool>
  9090. ///
  9091. /// @author Valaris
  9092. BUILDIN_FUNC(checkfalcon)
  9093. {
  9094. TBL_PC* sd;
  9095. if (!script_charid2sd(2,sd))
  9096. return SCRIPT_CMD_FAILURE;
  9097. if( pc_isfalcon(sd) )
  9098. script_pushint(st, 1);
  9099. else
  9100. script_pushint(st, 0);
  9101. return SCRIPT_CMD_SUCCESS;
  9102. }
  9103. /// Sets if the player has a falcon or not.
  9104. /// <flag> defaults to 1
  9105. ///
  9106. /// setfalcon {<flag>{,<char_id>}};
  9107. BUILDIN_FUNC(setfalcon)
  9108. {
  9109. int32 flag = 1;
  9110. TBL_PC* sd;
  9111. if (!script_charid2sd(3,sd))
  9112. return SCRIPT_CMD_FAILURE;
  9113. if( script_hasdata(st,2) )
  9114. flag = script_getnum(st,2);
  9115. pc_setfalcon(sd, flag);
  9116. return SCRIPT_CMD_SUCCESS;
  9117. }
  9118. /// Returns if the player is riding.
  9119. ///
  9120. /// checkriding({char_id}) -> <bool>
  9121. ///
  9122. /// @author Valaris
  9123. BUILDIN_FUNC(checkriding)
  9124. {
  9125. TBL_PC* sd;
  9126. if (!script_charid2sd(2,sd))
  9127. return SCRIPT_CMD_FAILURE;
  9128. if( pc_isriding(sd) )
  9129. script_pushint(st, 1);
  9130. else
  9131. script_pushint(st, 0);
  9132. return SCRIPT_CMD_SUCCESS;
  9133. }
  9134. /// Sets if the player is riding.
  9135. /// <flag> defaults to 1
  9136. ///
  9137. /// setriding {<flag>{,<char_id>}};
  9138. BUILDIN_FUNC(setriding)
  9139. {
  9140. int32 flag = 1;
  9141. TBL_PC* sd;
  9142. if (!script_charid2sd(3,sd))
  9143. return SCRIPT_CMD_FAILURE;
  9144. if( script_hasdata(st,2) )
  9145. flag = script_getnum(st,2);
  9146. pc_setriding(sd, flag);
  9147. return SCRIPT_CMD_SUCCESS;
  9148. }
  9149. /// Returns if the player has a warg.
  9150. ///
  9151. /// checkwug({char_id}) -> <bool>
  9152. ///
  9153. BUILDIN_FUNC(checkwug)
  9154. {
  9155. TBL_PC* sd;
  9156. if (!script_charid2sd(2,sd))
  9157. return SCRIPT_CMD_FAILURE;
  9158. if( pc_iswug(sd) || pc_isridingwug(sd) )
  9159. script_pushint(st, 1);
  9160. else
  9161. script_pushint(st, 0);
  9162. return SCRIPT_CMD_SUCCESS;
  9163. }
  9164. /// Returns if the player is wearing MADO Gear.
  9165. ///
  9166. /// checkmadogear({<char_id>}) -> <bool>
  9167. ///
  9168. BUILDIN_FUNC(checkmadogear)
  9169. {
  9170. TBL_PC* sd;
  9171. if (!script_charid2sd(2,sd))
  9172. return SCRIPT_CMD_FAILURE;
  9173. if( pc_ismadogear(sd) )
  9174. script_pushint(st, 1);
  9175. else
  9176. script_pushint(st, 0);
  9177. return SCRIPT_CMD_SUCCESS;
  9178. }
  9179. /// Sets if the player is riding MADO Gear.
  9180. /// <flag> defaults to true
  9181. /// <type> defaults to MADO_ROBOT
  9182. ///
  9183. /// setmadogear {<flag>{,type{,<char_id>}}};
  9184. BUILDIN_FUNC(setmadogear)
  9185. {
  9186. bool flag = true;
  9187. TBL_PC* sd;
  9188. uint16 type = MADO_ROBOT;
  9189. if (!script_charid2sd(4,sd))
  9190. return SCRIPT_CMD_FAILURE;
  9191. if( script_hasdata(st,2) )
  9192. flag = script_getnum(st,2) != 0;
  9193. if (script_hasdata(st, 3)) {
  9194. type = script_getnum(st, 3);
  9195. if (type == MADO_UNUSED || type >= MADO_MAX) {
  9196. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  9197. type = MADO_ROBOT;
  9198. }
  9199. }
  9200. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  9201. return SCRIPT_CMD_SUCCESS;
  9202. }
  9203. /// Sets the save point of the player.
  9204. ///
  9205. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  9206. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  9207. BUILDIN_FUNC(savepoint)
  9208. {
  9209. int32 x, y, m, cid_pos = 5;
  9210. unsigned short map_idx;
  9211. const char* str;
  9212. TBL_PC* sd;
  9213. if (script_lastdata(st) > 5)
  9214. cid_pos = 7;
  9215. if (!script_charid2sd(cid_pos,sd))
  9216. return SCRIPT_CMD_FAILURE;// no player attached, report source
  9217. str = script_getstr(st, 2);
  9218. map_idx = mapindex_name2id(str);
  9219. if( !map_idx )
  9220. return SCRIPT_CMD_FAILURE;
  9221. x = script_getnum(st,3);
  9222. y = script_getnum(st,4);
  9223. m = map_mapindex2mapid(map_idx);
  9224. if (cid_pos == 7) {
  9225. int32 dx = script_getnum(st,5), dy = script_getnum(st,6),
  9226. x1 = x + dx, y1 = y + dy,
  9227. x0 = x - dx, y0 = y - dy;
  9228. uint8 n = 10;
  9229. do {
  9230. x = rnd_value(x0, x1);
  9231. y = rnd_value(y0, y1);
  9232. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  9233. }
  9234. // Check for valid coordinates if map in local map-server
  9235. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  9236. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  9237. return SCRIPT_CMD_FAILURE;
  9238. }
  9239. pc_setsavepoint(sd, map_idx, x, y);
  9240. return SCRIPT_CMD_SUCCESS;
  9241. }
  9242. /*==========================================
  9243. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  9244. *------------------------------------------*/
  9245. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  9246. {
  9247. int32 type;
  9248. time_t timer;
  9249. struct tm *t;
  9250. type=script_getnum(st,2);
  9251. switch(type){
  9252. case 2:
  9253. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  9254. // from the system clock.)
  9255. script_pushint(st,(int)time(nullptr));
  9256. break;
  9257. case 1:
  9258. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  9259. time(&timer);
  9260. t=localtime(&timer);
  9261. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  9262. break;
  9263. case 0:
  9264. default:
  9265. //type 0:(System Ticks)
  9266. script_pushint(st,gettick());
  9267. break;
  9268. }
  9269. return SCRIPT_CMD_SUCCESS;
  9270. }
  9271. /*==========================================
  9272. * GetTime(Type)
  9273. *
  9274. * Returns the current value of a certain date type.
  9275. * Possible types are:
  9276. * - DT_SECOND Current seconds
  9277. * - DT_MINUTE Current minute
  9278. * - DT_HOUR Current hour
  9279. * - DT_DAYOFWEEK Day of current week
  9280. * - DT_DAYOFMONTH Day of current month
  9281. * - DT_MONTH Current month
  9282. * - DT_YEAR Current year
  9283. * - DT_DAYOFYEAR Day of current year
  9284. *
  9285. * If none of the above types is supplied -1 will be returned to the script
  9286. * and the script source will be reported into the mapserver console.
  9287. *------------------------------------------*/
  9288. BUILDIN_FUNC(gettime)
  9289. {
  9290. int32 type;
  9291. type = script_getnum(st,2);
  9292. if( type <= DT_MIN || type >= DT_MAX ){
  9293. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  9294. script_reportsrc(st);
  9295. script_pushint(st,-1);
  9296. }else{
  9297. script_pushint(st,date_get((enum e_date_type)type));
  9298. }
  9299. return SCRIPT_CMD_SUCCESS;
  9300. }
  9301. /**
  9302. * Returns the current server time or the provided time in a readable format.
  9303. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  9304. */
  9305. BUILDIN_FUNC(gettimestr)
  9306. {
  9307. char *tmpstr;
  9308. const char *fmtstr;
  9309. int32 maxlen;
  9310. time_t now;
  9311. fmtstr = script_getstr(st,2);
  9312. maxlen = script_getnum(st,3);
  9313. if (script_hasdata(st, 4)) {
  9314. if (script_getnum(st, 4) < 0) {
  9315. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  9316. return SCRIPT_CMD_FAILURE;
  9317. } else
  9318. now = (time_t)script_getnum(st, 4);
  9319. } else
  9320. now = time(nullptr);
  9321. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  9322. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  9323. tmpstr[maxlen] = '\0';
  9324. script_pushstr(st,tmpstr);
  9325. return SCRIPT_CMD_SUCCESS;
  9326. }
  9327. /*==========================================
  9328. * Open player storage
  9329. *------------------------------------------*/
  9330. BUILDIN_FUNC(openstorage)
  9331. {
  9332. TBL_PC* sd;
  9333. if( !script_rid2sd(sd) )
  9334. return SCRIPT_CMD_SUCCESS;
  9335. storage_storageopen(sd);
  9336. return SCRIPT_CMD_SUCCESS;
  9337. }
  9338. BUILDIN_FUNC(guildopenstorage)
  9339. {
  9340. TBL_PC* sd;
  9341. int32 ret;
  9342. if( !script_rid2sd(sd) )
  9343. return SCRIPT_CMD_SUCCESS;
  9344. ret = storage_guild_storageopen(sd);
  9345. script_pushint(st,ret);
  9346. return SCRIPT_CMD_SUCCESS;
  9347. }
  9348. BUILDIN_FUNC(guildopenstorage_log){
  9349. #if PACKETVER < 20140205
  9350. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  9351. return SCRIPT_CMD_FAILURE;
  9352. #else
  9353. map_session_data* sd;
  9354. if( !script_charid2sd( 2, sd ) ){
  9355. return SCRIPT_CMD_FAILURE;
  9356. }
  9357. script_pushint( st, storage_guild_log_read( sd ) );
  9358. return SCRIPT_CMD_SUCCESS;
  9359. #endif
  9360. }
  9361. BUILDIN_FUNC(guild_has_permission){
  9362. map_session_data* sd;
  9363. if( !script_charid2sd( 3, sd ) ){
  9364. return SCRIPT_CMD_FAILURE;
  9365. }
  9366. int32 permission = script_getnum(st,2);
  9367. if( permission == 0 ){
  9368. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  9369. return SCRIPT_CMD_FAILURE;
  9370. }
  9371. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  9372. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  9373. return SCRIPT_CMD_FAILURE;
  9374. }
  9375. if( !sd->guild ){
  9376. script_pushint( st, false );
  9377. return SCRIPT_CMD_SUCCESS;
  9378. }
  9379. int32 position = guild_getposition(*sd);
  9380. if( position < 0 || ( sd->guild->guild.position[position].mode&permission ) != permission ){
  9381. script_pushint( st, false );
  9382. return SCRIPT_CMD_SUCCESS;
  9383. }
  9384. script_pushint( st, true );
  9385. return SCRIPT_CMD_SUCCESS;
  9386. }
  9387. /*==========================================
  9388. * Make player use a skill trought item usage
  9389. *------------------------------------------*/
  9390. /// itemskill <skill id>,<level>
  9391. /// itemskill "<skill name>",<level>
  9392. BUILDIN_FUNC(itemskill)
  9393. {
  9394. int32 id;
  9395. int32 lv;
  9396. bool keep_requirement;
  9397. TBL_PC* sd;
  9398. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9399. return SCRIPT_CMD_SUCCESS;
  9400. if (script_isstring(st, 2)) {
  9401. const char *name = script_getstr(st, 2);
  9402. if (!(id = skill_name2id(name))) {
  9403. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9404. return SCRIPT_CMD_FAILURE;
  9405. }
  9406. } else {
  9407. id = script_getnum(st, 2);
  9408. if (!skill_get_index(id)) {
  9409. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9410. return SCRIPT_CMD_FAILURE;
  9411. }
  9412. }
  9413. lv = script_getnum(st,3);
  9414. if (script_hasdata(st, 4)) {
  9415. keep_requirement = (script_getnum(st, 4) != 0);
  9416. } else {
  9417. keep_requirement = false;
  9418. }
  9419. sd->skillitem=id;
  9420. sd->skillitemlv=lv;
  9421. sd->skillitem_keep_requirement = keep_requirement;
  9422. clif_item_skill(sd,id,lv);
  9423. return SCRIPT_CMD_SUCCESS;
  9424. }
  9425. /*==========================================
  9426. * Attempt to create an item
  9427. *------------------------------------------*/
  9428. BUILDIN_FUNC(produce)
  9429. {
  9430. int32 trigger;
  9431. map_session_data* sd;
  9432. if( !script_rid2sd(sd) )
  9433. return SCRIPT_CMD_SUCCESS;
  9434. trigger=script_getnum(st,2);
  9435. clif_skill_produce_mix_list( *sd, -1, trigger );
  9436. return SCRIPT_CMD_SUCCESS;
  9437. }
  9438. /*==========================================
  9439. *
  9440. *------------------------------------------*/
  9441. BUILDIN_FUNC(cooking)
  9442. {
  9443. int32 trigger;
  9444. map_session_data* sd;
  9445. if( !script_rid2sd(sd) )
  9446. return SCRIPT_CMD_SUCCESS;
  9447. trigger=script_getnum(st,2);
  9448. clif_cooking_list( *sd, trigger, AM_PHARMACY, 1, 1 );
  9449. return SCRIPT_CMD_SUCCESS;
  9450. }
  9451. /*==========================================
  9452. * Create a pet
  9453. *------------------------------------------*/
  9454. BUILDIN_FUNC(makepet)
  9455. {
  9456. map_session_data* sd;
  9457. uint16 mob_id;
  9458. if( !script_rid2sd(sd) )
  9459. return SCRIPT_CMD_FAILURE;
  9460. mob_id = script_getnum(st,2);
  9461. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9462. if( !pet ){
  9463. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9464. return SCRIPT_CMD_FAILURE;
  9465. }
  9466. sd->catch_target_class = mob_id;
  9467. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9468. intif_create_pet( sd->status.account_id, sd->status.char_id, pet->class_, mdb->lv, pet->EggID, 0, pet->intimate, 100, 0, 1, mdb->jname.c_str() );
  9469. return SCRIPT_CMD_SUCCESS;
  9470. }
  9471. /**
  9472. * Give player exp base,job * quest_exp_rate/100
  9473. * getexp <base xp>,<job xp>{,<char_id>};
  9474. **/
  9475. BUILDIN_FUNC(getexp){
  9476. map_session_data* sd;
  9477. if( !script_charid2sd( 4, sd ) ){
  9478. return SCRIPT_CMD_FAILURE;
  9479. }
  9480. int64 base = script_getnum64( st, 2 );
  9481. if( base < 0 ){
  9482. ShowError( "buildin_getexp: Called with negative base exp.\n" );
  9483. return SCRIPT_CMD_FAILURE;
  9484. }
  9485. int64 job = script_getnum64( st, 3 );
  9486. if( job < 0 ){
  9487. ShowError( "buildin_getexp: Called with negative job exp.\n" );
  9488. return SCRIPT_CMD_FAILURE;
  9489. }
  9490. if( base == 0 && job == 0 ){
  9491. ShowError( "buildin_getexp: Called with base and job exp 0.\n" );
  9492. return SCRIPT_CMD_FAILURE;
  9493. }
  9494. // bonus for npc-given exp
  9495. double bonus = battle_config.quest_exp_rate / 100.;
  9496. if (base)
  9497. base = (int64) cap_value(base * bonus, 0, MAX_EXP);
  9498. if (job)
  9499. job = (int64) cap_value(job * bonus, 0, MAX_EXP);
  9500. pc_gainexp(sd, nullptr, base, job, 1);
  9501. #ifdef RENEWAL
  9502. if (base && sd->hd)
  9503. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9504. #endif
  9505. return SCRIPT_CMD_SUCCESS;
  9506. }
  9507. /*==========================================
  9508. * Gain guild exp [Celest]
  9509. *------------------------------------------*/
  9510. BUILDIN_FUNC(guildgetexp){
  9511. map_session_data* sd;
  9512. if( !script_rid2sd( sd ) ){
  9513. return SCRIPT_CMD_FAILURE;
  9514. }
  9515. int64 exp = script_getnum64( st, 2 );
  9516. if( exp <= 0 ){
  9517. ShowError( "buildin_guildgetexp: Called with exp <= 0.\n" );
  9518. return SCRIPT_CMD_FAILURE;
  9519. }
  9520. if( sd->status.guild_id <= 0 ){
  9521. ShowError( "buildin_guildgetexp: Called for player %s (AID: %u, CID: %u) without a guild.\n", sd->status.name, sd->status.account_id, sd->status.char_id );
  9522. return SCRIPT_CMD_FAILURE;
  9523. }
  9524. guild_getexp( sd, exp );
  9525. return SCRIPT_CMD_SUCCESS;
  9526. }
  9527. /*==========================================
  9528. * Changes the guild master of a guild [Skotlex]
  9529. *------------------------------------------*/
  9530. BUILDIN_FUNC(guildchangegm)
  9531. {
  9532. TBL_PC *sd;
  9533. int32 guild_id;
  9534. const char *name;
  9535. guild_id = script_getnum(st,2);
  9536. name = script_getstr(st,3);
  9537. sd=map_nick2sd(name,false);
  9538. if (!sd)
  9539. script_pushint(st,0);
  9540. else
  9541. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9542. return SCRIPT_CMD_SUCCESS;
  9543. }
  9544. /*==========================================
  9545. * Spawn a monster:
  9546. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9547. * *monster "<map name>",<x>,<y>,"<name to show>","<mob name>",<amount>{,"<event label>",<size>,<ai>};
  9548. *------------------------------------------*/
  9549. BUILDIN_FUNC(monster)
  9550. {
  9551. const char* mapn = script_getstr(st,2);
  9552. int32 x = script_getnum(st,3);
  9553. int32 y = script_getnum(st,4);
  9554. const char* str = script_getstr(st,5);
  9555. int32 class_;
  9556. int32 amount = script_getnum(st,7);
  9557. const char* event = "";
  9558. uint32 size = SZ_SMALL;
  9559. enum mob_ai ai = AI_NONE;
  9560. map_session_data* sd;
  9561. int16 m;
  9562. int32 i;
  9563. if( script_isstring( st, 6 ) ){
  9564. const char* name = script_getstr( st, 6 );
  9565. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  9566. if( mob == nullptr ){
  9567. ShowWarning( "buildin_monster: Attempted to spawn non-existing monster \"%s\"\n", name );
  9568. return SCRIPT_CMD_FAILURE;
  9569. }
  9570. class_ = mob->id;
  9571. }else{
  9572. class_ = script_getnum( st, 6 );
  9573. if( class_ >= 0 && !mobdb_checkid( class_ ) ){
  9574. ShowWarning( "buildin_monster: Attempted to spawn non-existing monster class %d\n", class_ );
  9575. return SCRIPT_CMD_FAILURE;
  9576. }
  9577. }
  9578. if (script_hasdata(st, 8)) {
  9579. event = script_getstr(st, 8);
  9580. check_event(st, event);
  9581. }
  9582. if (script_hasdata(st, 9)) {
  9583. size = script_getnum(st, 9);
  9584. if (size > SZ_BIG) {
  9585. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9586. return SCRIPT_CMD_FAILURE;
  9587. }
  9588. }
  9589. if (script_hasdata(st, 10)) {
  9590. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9591. if (ai >= AI_MAX) {
  9592. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9593. return SCRIPT_CMD_FAILURE;
  9594. }
  9595. }
  9596. sd = map_id2sd(st->rid);
  9597. if (sd && strcmp(mapn, "this") == 0)
  9598. m = sd->bl.m;
  9599. else
  9600. m = map_mapname2mapid(mapn);
  9601. TBL_MOB* md;
  9602. for(i = 0; i < amount; i++) { //not optimised
  9603. int32 mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9604. if (mobid > 0) {
  9605. md = map_id2md(mobid);
  9606. if (md)
  9607. md->next_walktime = INVALID_TIMER;
  9608. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9609. }
  9610. }
  9611. return SCRIPT_CMD_SUCCESS;
  9612. }
  9613. /*==========================================
  9614. * Request List of Monster Drops
  9615. *------------------------------------------*/
  9616. BUILDIN_FUNC(getmobdrops)
  9617. {
  9618. int32 class_ = script_getnum(st,2);
  9619. int32 i, j = 0;
  9620. if( !mobdb_checkid(class_) )
  9621. {
  9622. script_pushint(st, 0);
  9623. return SCRIPT_CMD_SUCCESS;
  9624. }
  9625. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9626. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9627. {
  9628. if( mob->dropitem[i].nameid == 0 )
  9629. continue;
  9630. if( !item_db.exists(mob->dropitem[i].nameid) )
  9631. continue;
  9632. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9633. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9634. mapreg_setreg(reference_uid(add_str("$@MobDrop_nosteal"), j), mob->dropitem[i].steal_protected);
  9635. mapreg_setreg(reference_uid(add_str("$@MobDrop_randomopt"), j), mob->dropitem[i].randomopt_group);
  9636. j++;
  9637. }
  9638. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9639. script_pushint(st, 1);
  9640. return SCRIPT_CMD_SUCCESS;
  9641. }
  9642. /*==========================================
  9643. * Spawn a monster in a random location
  9644. * in x0,x1,y0,y1 area.
  9645. *------------------------------------------*/
  9646. BUILDIN_FUNC(areamonster)
  9647. {
  9648. const char* mapn = script_getstr(st,2);
  9649. int32 x0 = script_getnum(st,3);
  9650. int32 y0 = script_getnum(st,4);
  9651. int32 x1 = script_getnum(st,5);
  9652. int32 y1 = script_getnum(st,6);
  9653. const char* str = script_getstr(st,7);
  9654. int32 class_;
  9655. int32 amount = script_getnum(st,9);
  9656. const char* event = "";
  9657. uint32 size = SZ_SMALL;
  9658. enum mob_ai ai = AI_NONE;
  9659. map_session_data* sd;
  9660. int16 m;
  9661. int32 i;
  9662. if( script_isstring( st, 8 ) ){
  9663. const char* name = script_getstr( st, 8 );
  9664. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  9665. if( mob == nullptr ){
  9666. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing monster \"%s\"\n", name );
  9667. return SCRIPT_CMD_FAILURE;
  9668. }
  9669. class_ = mob->id;
  9670. }else{
  9671. class_ = script_getnum( st, 8 );
  9672. if( class_ >= 0 && !mobdb_checkid( class_ ) ){
  9673. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing monster class %d\n", class_ );
  9674. return SCRIPT_CMD_FAILURE;
  9675. }
  9676. }
  9677. if (script_hasdata(st,10)) {
  9678. event = script_getstr(st, 10);
  9679. check_event(st, event);
  9680. }
  9681. if (script_hasdata(st, 11)) {
  9682. size = script_getnum(st, 11);
  9683. if (size > 3) {
  9684. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_ );
  9685. return SCRIPT_CMD_FAILURE;
  9686. }
  9687. }
  9688. if (script_hasdata(st, 12)) {
  9689. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9690. if (ai >= AI_MAX) {
  9691. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_ );
  9692. return SCRIPT_CMD_FAILURE;
  9693. }
  9694. }
  9695. sd = map_id2sd(st->rid);
  9696. if (sd && strcmp(mapn, "this") == 0)
  9697. m = sd->bl.m;
  9698. else
  9699. m = map_mapname2mapid(mapn);
  9700. TBL_MOB* md;
  9701. for(i = 0; i < amount; i++) { //not optimised
  9702. int32 mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9703. if (mobid > 0) {
  9704. md = map_id2md(mobid);
  9705. if (md)
  9706. md->next_walktime = INVALID_TIMER;
  9707. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9708. }
  9709. }
  9710. return SCRIPT_CMD_SUCCESS;
  9711. }
  9712. /*==========================================
  9713. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9714. *------------------------------------------*/
  9715. static int32 buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9716. { //same fix but with killmonster instead - stripping events from mobs.
  9717. TBL_MOB* md = (TBL_MOB*)bl;
  9718. char *event=va_arg(ap,char *);
  9719. int32 allflag=va_arg(ap,int);
  9720. md->state.npc_killmonster = 1;
  9721. if(!allflag){
  9722. if(strcmp(event,md->npc_event)==0)
  9723. status_kill(bl);
  9724. }else{
  9725. if(!md->spawn)
  9726. status_kill(bl);
  9727. }
  9728. md->state.npc_killmonster = 0;
  9729. return SCRIPT_CMD_SUCCESS;
  9730. }
  9731. static int32 buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9732. {
  9733. TBL_MOB* md = (TBL_MOB*)bl;
  9734. char *event=va_arg(ap,char *);
  9735. int32 allflag=va_arg(ap,int);
  9736. if(!allflag){
  9737. if(strcmp(event,md->npc_event)==0)
  9738. status_kill(bl);
  9739. }else{
  9740. if(!md->spawn)
  9741. status_kill(bl);
  9742. }
  9743. return SCRIPT_CMD_SUCCESS;
  9744. }
  9745. BUILDIN_FUNC(killmonster)
  9746. {
  9747. const char *mapname,*event;
  9748. int16 m,allflag=0;
  9749. mapname=script_getstr(st,2);
  9750. event=script_getstr(st,3);
  9751. if(strcmp(event,"All")==0)
  9752. allflag = 1;
  9753. else
  9754. check_event(st, event);
  9755. if( (m=map_mapname2mapid(mapname))<0 )
  9756. return SCRIPT_CMD_SUCCESS;
  9757. if( script_hasdata(st,4) ) {
  9758. if ( script_getnum(st,4) == 1 ) {
  9759. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9760. return SCRIPT_CMD_SUCCESS;
  9761. }
  9762. }
  9763. map_freeblock_lock();
  9764. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9765. map_freeblock_unlock();
  9766. return SCRIPT_CMD_SUCCESS;
  9767. }
  9768. static int32 buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9769. { //Strips the event from the mob if it's killed the old method.
  9770. mob_data* md = BL_CAST(BL_MOB, bl);
  9771. if( md != nullptr ){
  9772. strcpy( md->npc_event, "" );
  9773. }
  9774. status_kill(bl);
  9775. return 0;
  9776. }
  9777. static int32 buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9778. {
  9779. status_kill(bl);
  9780. return 0;
  9781. }
  9782. BUILDIN_FUNC(killmonsterall)
  9783. {
  9784. const char *mapname;
  9785. int16 m;
  9786. mapname=script_getstr(st,2);
  9787. if( (m = map_mapname2mapid(mapname))<0 )
  9788. return SCRIPT_CMD_SUCCESS;
  9789. if( script_hasdata(st,3) ) {
  9790. if ( script_getnum(st,3) == 1 ) {
  9791. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9792. return SCRIPT_CMD_SUCCESS;
  9793. }
  9794. }
  9795. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9796. return SCRIPT_CMD_SUCCESS;
  9797. }
  9798. /*==========================================
  9799. * Creates a clone of a player.
  9800. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9801. *------------------------------------------*/
  9802. BUILDIN_FUNC(clone)
  9803. {
  9804. TBL_PC *sd, *msd=nullptr;
  9805. uint32 char_id, master_id = 0, x, y, flag = 0;
  9806. int16 m;
  9807. enum e_mode mode = MD_NONE;
  9808. uint32 duration = 0;
  9809. const char *mapname,*event;
  9810. mapname=script_getstr(st,2);
  9811. x=script_getnum(st,3);
  9812. y=script_getnum(st,4);
  9813. event=script_getstr(st,5);
  9814. char_id=script_getnum(st,6);
  9815. if( script_hasdata(st,7) )
  9816. master_id=script_getnum(st,7);
  9817. if( script_hasdata(st,8) )
  9818. mode=static_cast<e_mode>(script_getnum(st,8));
  9819. if( script_hasdata(st,9) )
  9820. flag=script_getnum(st,9);
  9821. if( script_hasdata(st,10) )
  9822. duration=script_getnum(st,10);
  9823. check_event(st, event);
  9824. m = map_mapname2mapid(mapname);
  9825. if (m < 0)
  9826. return SCRIPT_CMD_SUCCESS;
  9827. sd = map_charid2sd(char_id);
  9828. if (master_id) {
  9829. msd = map_charid2sd(master_id);
  9830. if (msd)
  9831. master_id = msd->bl.id;
  9832. else
  9833. master_id = 0;
  9834. }
  9835. if (sd) //Return ID of newly crafted clone.
  9836. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9837. else //Failed to create clone.
  9838. script_pushint(st,0);
  9839. return SCRIPT_CMD_SUCCESS;
  9840. }
  9841. /*==========================================
  9842. *------------------------------------------*/
  9843. BUILDIN_FUNC(doevent)
  9844. {
  9845. const char* event;
  9846. map_session_data* sd;
  9847. if( !script_rid2sd(sd) )
  9848. return SCRIPT_CMD_SUCCESS;
  9849. event = script_getstr(st,2);
  9850. check_event(st, event);
  9851. npc_event(sd, event, 0);
  9852. return SCRIPT_CMD_SUCCESS;
  9853. }
  9854. /*==========================================
  9855. *------------------------------------------*/
  9856. BUILDIN_FUNC(donpcevent)
  9857. {
  9858. const char* event = script_getstr(st,2);
  9859. check_event(st, event);
  9860. if( !npc_event_do(event) ) {
  9861. struct npc_data * nd = map_id2nd(st->oid);
  9862. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9863. script_pushint(st, 0);
  9864. } else
  9865. script_pushint(st, 1);
  9866. return SCRIPT_CMD_SUCCESS;
  9867. }
  9868. /// for Aegis compatibility
  9869. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9870. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9871. {
  9872. const char* npc = script_getstr(st,2);
  9873. const char* command = script_getstr(st,3);
  9874. char event[EVENT_NAME_LENGTH];
  9875. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9876. check_event(st, event);
  9877. if( npc_event_do(event) ){
  9878. script_pushint(st, true);
  9879. }else{
  9880. struct npc_data * nd = map_id2nd(st->oid);
  9881. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9882. script_pushint(st, false);
  9883. }
  9884. return SCRIPT_CMD_SUCCESS;
  9885. }
  9886. /*==========================================
  9887. *------------------------------------------*/
  9888. BUILDIN_FUNC(addtimer)
  9889. {
  9890. int32 tick;
  9891. const char* event;
  9892. TBL_PC* sd;
  9893. if( !script_rid2sd(sd) )
  9894. return SCRIPT_CMD_SUCCESS;
  9895. tick = script_getnum(st,2);
  9896. event = script_getstr(st, 3);
  9897. check_event(st, event);
  9898. if (!pc_addeventtimer(sd,tick,event)) {
  9899. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9900. return SCRIPT_CMD_FAILURE;
  9901. }
  9902. return SCRIPT_CMD_SUCCESS;
  9903. }
  9904. /*==========================================
  9905. *------------------------------------------*/
  9906. BUILDIN_FUNC(deltimer)
  9907. {
  9908. const char *event;
  9909. TBL_PC* sd;
  9910. if( !script_rid2sd(sd) )
  9911. return SCRIPT_CMD_SUCCESS;
  9912. event=script_getstr(st, 2);
  9913. check_event(st, event);
  9914. pc_deleventtimer(sd,event);
  9915. return SCRIPT_CMD_SUCCESS;
  9916. }
  9917. /*==========================================
  9918. *------------------------------------------*/
  9919. BUILDIN_FUNC(addtimercount)
  9920. {
  9921. const char *event;
  9922. int32 tick;
  9923. TBL_PC* sd;
  9924. if( !script_rid2sd(sd) )
  9925. return SCRIPT_CMD_SUCCESS;
  9926. tick=script_getnum(st,2);
  9927. event=script_getstr(st,3);
  9928. check_event(st, event);
  9929. pc_addeventtimercount(sd,event,tick);
  9930. return SCRIPT_CMD_SUCCESS;
  9931. }
  9932. /*==========================================
  9933. *------------------------------------------*/
  9934. BUILDIN_FUNC(initnpctimer)
  9935. {
  9936. struct npc_data *nd;
  9937. int32 flag = 0;
  9938. if( script_hasdata(st,3) )
  9939. { //Two arguments: NPC name and attach flag.
  9940. nd = npc_name2id(script_getstr(st, 2));
  9941. flag = script_getnum(st,3);
  9942. }
  9943. else if( script_hasdata(st,2) )
  9944. { //Check if argument is numeric (flag) or string (npc name)
  9945. if( script_isstring(st, 2) ) //NPC name
  9946. nd = npc_name2id(script_getstr(st, 2));
  9947. else //Flag
  9948. {
  9949. nd = (struct npc_data *)map_id2bl(st->oid);
  9950. flag = script_getnum(st, 2);
  9951. }
  9952. }
  9953. else
  9954. nd = (struct npc_data *)map_id2bl(st->oid);
  9955. if( !nd )
  9956. return SCRIPT_CMD_SUCCESS;
  9957. if( flag ) //Attach
  9958. {
  9959. TBL_PC* sd;
  9960. if( !script_rid2sd(sd) )
  9961. return SCRIPT_CMD_SUCCESS;
  9962. nd->u.scr.rid = sd->bl.id;
  9963. }
  9964. nd->u.scr.timertick = 0;
  9965. npc_settimerevent_tick(nd,0);
  9966. npc_timerevent_start(nd, st->rid);
  9967. return SCRIPT_CMD_SUCCESS;
  9968. }
  9969. /*==========================================
  9970. *------------------------------------------*/
  9971. BUILDIN_FUNC(startnpctimer)
  9972. {
  9973. struct npc_data *nd;
  9974. int32 flag = 0;
  9975. if( script_hasdata(st,3) )
  9976. { //Two arguments: NPC name and attach flag.
  9977. nd = npc_name2id(script_getstr(st, 2));
  9978. flag = script_getnum(st,3);
  9979. }
  9980. else if( script_hasdata(st,2) )
  9981. { //Check if argument is numeric (flag) or string (npc name)
  9982. if( script_isstring(st, 2) ) //NPC name
  9983. nd = npc_name2id(script_getstr(st, 2));
  9984. else //Flag
  9985. {
  9986. nd = (struct npc_data *)map_id2bl(st->oid);
  9987. flag = script_getnum(st, 2);
  9988. }
  9989. }
  9990. else
  9991. nd=(struct npc_data *)map_id2bl(st->oid);
  9992. if( !nd )
  9993. return SCRIPT_CMD_SUCCESS;
  9994. if( flag ) //Attach
  9995. {
  9996. TBL_PC* sd;
  9997. if( !script_rid2sd(sd) )
  9998. return SCRIPT_CMD_SUCCESS;
  9999. nd->u.scr.rid = sd->bl.id;
  10000. }
  10001. npc_timerevent_start(nd, st->rid);
  10002. return SCRIPT_CMD_SUCCESS;
  10003. }
  10004. /*==========================================
  10005. *------------------------------------------*/
  10006. BUILDIN_FUNC(stopnpctimer)
  10007. {
  10008. struct npc_data *nd;
  10009. int32 flag = 0;
  10010. if( script_hasdata(st,3) )
  10011. { //Two arguments: NPC name and attach flag.
  10012. nd = npc_name2id(script_getstr(st, 2));
  10013. flag = script_getnum(st,3);
  10014. }
  10015. else if( script_hasdata(st,2) )
  10016. { //Check if argument is numeric (flag) or string (npc name)
  10017. if( script_isstring(st, 2) ) //NPC name
  10018. nd = npc_name2id(script_getstr(st, 2));
  10019. else //Flag
  10020. {
  10021. nd = (struct npc_data *)map_id2bl(st->oid);
  10022. flag = script_getnum(st, 2);
  10023. }
  10024. }
  10025. else
  10026. nd=(struct npc_data *)map_id2bl(st->oid);
  10027. if( !nd )
  10028. return SCRIPT_CMD_SUCCESS;
  10029. if( flag ) //Detach
  10030. nd->u.scr.rid = 0;
  10031. npc_timerevent_stop(nd);
  10032. return SCRIPT_CMD_SUCCESS;
  10033. }
  10034. /*==========================================
  10035. *------------------------------------------*/
  10036. BUILDIN_FUNC(getnpctimer)
  10037. {
  10038. struct npc_data *nd;
  10039. TBL_PC *sd;
  10040. int32 type = script_getnum(st,2);
  10041. t_tick val = 0;
  10042. if( script_hasdata(st,3) )
  10043. nd = npc_name2id(script_getstr(st,3));
  10044. else
  10045. nd = (struct npc_data *)map_id2bl(st->oid);
  10046. if( !nd || nd->bl.type != BL_NPC )
  10047. {
  10048. script_pushint(st,0);
  10049. ShowError("getnpctimer: Invalid NPC.\n");
  10050. return SCRIPT_CMD_FAILURE;
  10051. }
  10052. switch( type )
  10053. {
  10054. case 0: val = npc_gettimerevent_tick(nd); break;
  10055. case 1:
  10056. if( nd->u.scr.rid )
  10057. {
  10058. sd = map_id2sd(nd->u.scr.rid);
  10059. if( !sd )
  10060. {
  10061. ShowError("buildin_getnpctimer: Attached player not found!\n");
  10062. break;
  10063. }
  10064. val = (sd->npc_timer_id != INVALID_TIMER);
  10065. }
  10066. else
  10067. val = (nd->u.scr.timerid != INVALID_TIMER);
  10068. break;
  10069. case 2: val = nd->u.scr.timeramount; break;
  10070. }
  10071. script_pushint(st,val);
  10072. return SCRIPT_CMD_SUCCESS;
  10073. }
  10074. /*==========================================
  10075. *------------------------------------------*/
  10076. BUILDIN_FUNC(setnpctimer)
  10077. {
  10078. int32 tick;
  10079. struct npc_data *nd;
  10080. tick = script_getnum(st,2);
  10081. if( script_hasdata(st,3) )
  10082. nd = npc_name2id(script_getstr(st,3));
  10083. else
  10084. nd = (struct npc_data *)map_id2bl(st->oid);
  10085. if( !nd || nd->bl.type != BL_NPC )
  10086. {
  10087. script_pushint(st,1);
  10088. ShowError("setnpctimer: Invalid NPC.\n");
  10089. return SCRIPT_CMD_FAILURE;
  10090. }
  10091. npc_settimerevent_tick(nd,tick);
  10092. script_pushint(st,0);
  10093. return SCRIPT_CMD_SUCCESS;
  10094. }
  10095. /*==========================================
  10096. * attaches the player rid to the timer [Celest]
  10097. *------------------------------------------*/
  10098. BUILDIN_FUNC(attachnpctimer)
  10099. {
  10100. TBL_PC *sd;
  10101. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  10102. if( !nd || nd->bl.type != BL_NPC )
  10103. {
  10104. script_pushint(st,1);
  10105. ShowError("setnpctimer: Invalid NPC.\n");
  10106. return SCRIPT_CMD_FAILURE;
  10107. }
  10108. if( !script_nick2sd(2,sd) ){
  10109. script_pushint(st,1);
  10110. ShowWarning("attachnpctimer: Invalid player.\n");
  10111. return SCRIPT_CMD_FAILURE;
  10112. }
  10113. nd->u.scr.rid = sd->bl.id;
  10114. script_pushint(st,0);
  10115. return SCRIPT_CMD_SUCCESS;
  10116. }
  10117. /*==========================================
  10118. * detaches a player rid from the timer [Celest]
  10119. *------------------------------------------*/
  10120. BUILDIN_FUNC(detachnpctimer)
  10121. {
  10122. struct npc_data *nd;
  10123. if( script_hasdata(st,2) )
  10124. nd = npc_name2id(script_getstr(st,2));
  10125. else
  10126. nd = (struct npc_data *)map_id2bl(st->oid);
  10127. if( !nd || nd->bl.type != BL_NPC )
  10128. {
  10129. script_pushint(st,1);
  10130. ShowError("detachnpctimer: Invalid NPC.\n");
  10131. return SCRIPT_CMD_FAILURE;
  10132. }
  10133. nd->u.scr.rid = 0;
  10134. script_pushint(st,0);
  10135. return SCRIPT_CMD_SUCCESS;
  10136. }
  10137. /*==========================================
  10138. * To avoid "player not attached" script errors, this function is provided,
  10139. * it checks if there is a player attached to the current script. [Skotlex]
  10140. * If no, returns 0, if yes, returns the account_id of the attached player.
  10141. *------------------------------------------*/
  10142. BUILDIN_FUNC(playerattached)
  10143. {
  10144. if(st->rid == 0 || map_id2sd(st->rid) == nullptr)
  10145. script_pushint(st,0);
  10146. else
  10147. script_pushint(st,st->rid);
  10148. return SCRIPT_CMD_SUCCESS;
  10149. }
  10150. /*==========================================
  10151. *------------------------------------------*/
  10152. BUILDIN_FUNC(announce)
  10153. {
  10154. const char *mes = script_getstr(st,2);
  10155. int32 flag = script_getnum(st,3);
  10156. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : nullptr;
  10157. int32 fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  10158. int32 fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  10159. int32 fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  10160. int32 fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  10161. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  10162. {
  10163. send_target target;
  10164. struct block_list *bl;
  10165. // If bc_npc flag is set, use NPC as broadcast source
  10166. if(flag&BC_NPC){
  10167. bl = map_id2bl(st->oid);
  10168. }else{
  10169. map_session_data* sd;
  10170. if(script_charid2sd(9, sd))
  10171. bl = &sd->bl;
  10172. else
  10173. bl = nullptr;
  10174. }
  10175. if (bl == nullptr)
  10176. return SCRIPT_CMD_SUCCESS;
  10177. switch (flag&BC_TARGET_MASK) {
  10178. case BC_MAP: target = ALL_SAMEMAP; break;
  10179. case BC_AREA: target = AREA; break;
  10180. case BC_SELF: target = SELF; break;
  10181. default: target = ALL_CLIENT; break; // BC_ALL
  10182. }
  10183. if (fontColor)
  10184. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)nullptr, 0), fontType, fontSize, fontAlign, fontY, target);
  10185. else
  10186. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  10187. }
  10188. else
  10189. {
  10190. if (fontColor)
  10191. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)nullptr, 0), fontType, fontSize, fontAlign, fontY);
  10192. else
  10193. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  10194. }
  10195. return SCRIPT_CMD_SUCCESS;
  10196. }
  10197. /*==========================================
  10198. *------------------------------------------*/
  10199. static int32 buildin_announce_sub(struct block_list *bl, va_list ap)
  10200. {
  10201. char *mes = va_arg(ap, char *);
  10202. int32 len = va_arg(ap, int);
  10203. int32 type = va_arg(ap, int);
  10204. char *fontColor = va_arg(ap, char *);
  10205. short fontType = (short)va_arg(ap, int);
  10206. short fontSize = (short)va_arg(ap, int);
  10207. short fontAlign = (short)va_arg(ap, int);
  10208. short fontY = (short)va_arg(ap, int);
  10209. if (fontColor)
  10210. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)nullptr, 0), fontType, fontSize, fontAlign, fontY, SELF);
  10211. else
  10212. clif_broadcast(bl, mes, len, type, SELF);
  10213. return SCRIPT_CMD_SUCCESS;
  10214. }
  10215. BUILDIN_FUNC(mapannounce)
  10216. {
  10217. const char *mapname = script_getstr(st,2);
  10218. const char *mes = script_getstr(st,3);
  10219. int32 flag = script_getnum(st,4);
  10220. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : nullptr;
  10221. int32 fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  10222. int32 fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  10223. int32 fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  10224. int32 fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  10225. int16 m;
  10226. if ((m = map_mapname2mapid(mapname)) < 0)
  10227. return SCRIPT_CMD_SUCCESS;
  10228. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  10229. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  10230. return SCRIPT_CMD_SUCCESS;
  10231. }
  10232. /*==========================================
  10233. *------------------------------------------*/
  10234. BUILDIN_FUNC(areaannounce)
  10235. {
  10236. const char *mapname = script_getstr(st,2);
  10237. int32 x0 = script_getnum(st,3);
  10238. int32 y0 = script_getnum(st,4);
  10239. int32 x1 = script_getnum(st,5);
  10240. int32 y1 = script_getnum(st,6);
  10241. const char *mes = script_getstr(st,7);
  10242. int32 flag = script_getnum(st,8);
  10243. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : nullptr;
  10244. int32 fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  10245. int32 fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  10246. int32 fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  10247. int32 fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  10248. int16 m;
  10249. if ((m = map_mapname2mapid(mapname)) < 0)
  10250. return SCRIPT_CMD_SUCCESS;
  10251. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  10252. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  10253. return SCRIPT_CMD_SUCCESS;
  10254. }
  10255. /*==========================================
  10256. *------------------------------------------*/
  10257. BUILDIN_FUNC(getusers)
  10258. {
  10259. int32 flag, val = 0;
  10260. map_session_data* sd;
  10261. struct block_list* bl = nullptr;
  10262. flag = script_getnum(st,2);
  10263. switch(flag&0x07)
  10264. {
  10265. case 0:
  10266. if(flag&0x8)
  10267. {// npc
  10268. bl = map_id2bl(st->oid);
  10269. }
  10270. else if(script_rid2sd(sd))
  10271. {// pc
  10272. bl = &sd->bl;
  10273. }
  10274. if(bl)
  10275. {
  10276. val = map_getmapdata(bl->m)->users;
  10277. }
  10278. break;
  10279. case 1:
  10280. val = map_getusers();
  10281. break;
  10282. default:
  10283. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  10284. script_pushint(st,0);
  10285. return SCRIPT_CMD_FAILURE;
  10286. }
  10287. script_pushint(st,val);
  10288. return SCRIPT_CMD_SUCCESS;
  10289. }
  10290. /*==========================================
  10291. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  10292. *------------------------------------------*/
  10293. BUILDIN_FUNC(getmapguildusers)
  10294. {
  10295. const char *str;
  10296. int16 m;
  10297. int32 gid;
  10298. int32 c=0;
  10299. str=script_getstr(st,2);
  10300. gid=script_getnum(st,3);
  10301. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  10302. script_pushint(st,-1);
  10303. return SCRIPT_CMD_SUCCESS;
  10304. }
  10305. auto g = guild_search(gid);
  10306. if (g){
  10307. unsigned short i;
  10308. for(i = 0; i < g->guild.max_member; i++)
  10309. {
  10310. if (g->guild.member[i].sd && g->guild.member[i].sd->bl.m == m)
  10311. c++;
  10312. }
  10313. }
  10314. script_pushint(st,c);
  10315. return SCRIPT_CMD_SUCCESS;
  10316. }
  10317. /*==========================================
  10318. *------------------------------------------*/
  10319. BUILDIN_FUNC(getmapusers)
  10320. {
  10321. const char *str;
  10322. int16 m;
  10323. str=script_getstr(st,2);
  10324. if( (m=map_mapname2mapid(str))< 0){
  10325. script_pushint(st,-1);
  10326. return SCRIPT_CMD_SUCCESS;
  10327. }
  10328. script_pushint(st,map_getmapdata(m)->users);
  10329. return SCRIPT_CMD_SUCCESS;
  10330. }
  10331. /*==========================================
  10332. *------------------------------------------*/
  10333. static int32 buildin_getareausers_sub(struct block_list *bl,va_list ap)
  10334. {
  10335. int32 *users = va_arg(ap, int32 *);
  10336. (*users)++;
  10337. return SCRIPT_CMD_SUCCESS;
  10338. }
  10339. BUILDIN_FUNC(getareausers)
  10340. {
  10341. const char *str;
  10342. int16 m,x0,y0,x1,y1;
  10343. int32 users = 0;
  10344. str=script_getstr(st,2);
  10345. x0=script_getnum(st,3);
  10346. y0=script_getnum(st,4);
  10347. x1=script_getnum(st,5);
  10348. y1=script_getnum(st,6);
  10349. if( (m=map_mapname2mapid(str))< 0){
  10350. script_pushint(st,-1);
  10351. return SCRIPT_CMD_SUCCESS;
  10352. }
  10353. map_foreachinallarea(buildin_getareausers_sub,
  10354. m,x0,y0,x1,y1,BL_PC,&users);
  10355. script_pushint(st,users);
  10356. return SCRIPT_CMD_SUCCESS;
  10357. }
  10358. /*==========================================
  10359. * getunits(<type>{,<array_variable>[<first value>]})
  10360. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  10361. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  10362. *------------------------------------------*/
  10363. BUILDIN_FUNC(getunits)
  10364. {
  10365. struct block_list *bl = nullptr;
  10366. map_session_data *sd = nullptr;
  10367. struct script_data *data = nullptr;
  10368. char *command = (char *)script_getfuncname(st);
  10369. const char *str;
  10370. const char *name;
  10371. int32 type = script_getnum(st, 2);
  10372. int32 size = 0;
  10373. int32 idx, id;
  10374. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  10375. if (!strcmp(command, "getmapunits"))
  10376. {
  10377. str = script_getstr(st, 3);
  10378. if ((m = map_mapname2mapid(str)) < 0) {
  10379. script_pushint(st, -1);
  10380. st->state = END;
  10381. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10382. return SCRIPT_CMD_FAILURE;
  10383. }
  10384. if (script_hasdata(st, 4))
  10385. data = script_getdata(st, 4);
  10386. }
  10387. else if (!strcmp(command, "getareaunits"))
  10388. {
  10389. str = script_getstr(st, 3);
  10390. if ((m = map_mapname2mapid(str)) < 0) {
  10391. script_pushint(st, -1);
  10392. st->state = END;
  10393. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10394. return SCRIPT_CMD_FAILURE;
  10395. }
  10396. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  10397. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  10398. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  10399. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  10400. if (script_hasdata(st, 8))
  10401. data = script_getdata(st, 8);
  10402. }
  10403. else
  10404. {
  10405. if (script_hasdata(st, 3))
  10406. data = script_getdata(st, 3);
  10407. }
  10408. if (data)
  10409. {
  10410. if (!data_isreference(data))
  10411. {
  10412. ShowError("buildin_%s: not a variable\n", command);
  10413. script_reportdata(data);
  10414. st->state = END;
  10415. return SCRIPT_CMD_FAILURE;
  10416. }
  10417. id = reference_getid(data);
  10418. idx = reference_getindex(data);
  10419. name = reference_getname(data);
  10420. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  10421. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  10422. return SCRIPT_CMD_FAILURE;
  10423. }
  10424. }
  10425. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  10426. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  10427. {
  10428. if (m == -1 || (m == bl->m && !x0 && !y0 && !x1 && !y1) || (bl->m == m && (bl->x >= x0 && bl->y >= y0) && (bl->x <= x1 && bl->y <= y1)))
  10429. {
  10430. if( data ){
  10431. if( is_string_variable( name ) ){
  10432. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  10433. }else{
  10434. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  10435. }
  10436. }
  10437. size++;
  10438. }
  10439. }
  10440. mapit_free(iter);
  10441. script_pushint(st, size);
  10442. return SCRIPT_CMD_SUCCESS;
  10443. }
  10444. /*==========================================
  10445. *------------------------------------------*/
  10446. static int32 buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10447. {
  10448. t_itemid nameid = va_arg(ap, t_itemid);
  10449. unsigned short *amount = (unsigned short *)va_arg(ap, int32 *);
  10450. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10451. if(drop->item.nameid==nameid)
  10452. (*amount)+=drop->item.amount;
  10453. return SCRIPT_CMD_SUCCESS;
  10454. }
  10455. BUILDIN_FUNC(getareadropitem)
  10456. {
  10457. const char *str;
  10458. int16 m,x0,y0,x1,y1;
  10459. t_itemid nameid;
  10460. unsigned short amount = 0;
  10461. str=script_getstr(st,2);
  10462. x0=script_getnum(st,3);
  10463. y0=script_getnum(st,4);
  10464. x1=script_getnum(st,5);
  10465. y1=script_getnum(st,6);
  10466. if( script_isstring(st, 7) ){
  10467. const char *name = script_getstr(st, 7);
  10468. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  10469. if( item_data )
  10470. nameid=item_data->nameid;
  10471. else{
  10472. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10473. return SCRIPT_CMD_FAILURE;
  10474. }
  10475. }else{
  10476. nameid = script_getnum(st, 7);
  10477. if( !item_db.exists( nameid ) ){
  10478. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10479. return SCRIPT_CMD_FAILURE;
  10480. }
  10481. }
  10482. if( (m=map_mapname2mapid(str))< 0){
  10483. script_pushint(st,-1);
  10484. return SCRIPT_CMD_SUCCESS;
  10485. }
  10486. map_foreachinallarea(buildin_getareadropitem_sub,
  10487. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10488. script_pushint(st,amount);
  10489. return SCRIPT_CMD_SUCCESS;
  10490. }
  10491. /*==========================================
  10492. *------------------------------------------*/
  10493. BUILDIN_FUNC(enablenpc)
  10494. {
  10495. npc_data* nd;
  10496. e_npcv_status flag = NPCVIEW_DISABLE;
  10497. const char* command = script_getfuncname(st);
  10498. if (script_hasdata(st, 2))
  10499. nd = npc_name2id(script_getstr(st,2));
  10500. else
  10501. nd = map_id2nd(st->oid);
  10502. if (!strcmp(command,"enablenpc"))
  10503. flag = NPCVIEW_ENABLE;
  10504. else if (!strcmp(command,"disablenpc"))
  10505. flag = NPCVIEW_DISABLE;
  10506. else if (!strcmp(command,"hideoffnpc"))
  10507. flag = NPCVIEW_HIDEOFF;
  10508. else if (!strcmp(command,"hideonnpc"))
  10509. flag = NPCVIEW_HIDEON;
  10510. else if (!strcmp(command,"cloakoffnpc") || !strcmp(command,"cloakoffnpcself"))
  10511. flag = NPCVIEW_CLOAKOFF;
  10512. else if (!strcmp(command,"cloakonnpc") || !strcmp(command,"cloakonnpcself"))
  10513. flag = NPCVIEW_CLOAKON;
  10514. else{
  10515. ShowError( "buildin_enablenpc: Undefined command \"%s\".\n", command );
  10516. return SCRIPT_CMD_FAILURE;
  10517. }
  10518. if (!nd) {
  10519. if (script_hasdata(st, 2))
  10520. ShowError("buildin_%s: Attempted to %s a non-existing NPC '%s' (flag=%d).\n", (flag & NPCVIEW_VISIBLE) ? "show" : "hide", command, script_getstr(st,2), flag);
  10521. else
  10522. ShowError("buildin_%s: Attempted to %s a non-existing NPC (flag=%d).\n", (flag & NPCVIEW_VISIBLE) ? "show" : "hide", command, flag);
  10523. return SCRIPT_CMD_FAILURE;
  10524. }
  10525. int32 char_id = 0;
  10526. if (script_hasdata(st, 3))
  10527. char_id = script_getnum(st, 3);
  10528. else if (!strcmp(command,"cloakoffnpcself") || !strcmp(command,"cloakonnpcself")) {
  10529. map_session_data* sd;
  10530. if (!script_rid2sd(sd))
  10531. return SCRIPT_CMD_SUCCESS;
  10532. char_id = sd->status.char_id;
  10533. }
  10534. if (npc_enable_target(*nd, char_id, flag))
  10535. return SCRIPT_CMD_SUCCESS;
  10536. return SCRIPT_CMD_FAILURE;
  10537. }
  10538. /* Starts a status effect on the target unit or on the attached player.
  10539. *
  10540. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10541. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10542. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10543. * <flag>: enum e_status_change_start_flags
  10544. */
  10545. BUILDIN_FUNC(sc_start)
  10546. {
  10547. TBL_NPC * nd = map_id2nd(st->oid);
  10548. struct block_list* bl;
  10549. enum sc_type type;
  10550. int32 tick, val1, val2, val3, val4=0, rate, flag;
  10551. char start_type;
  10552. const char* command = script_getfuncname(st);
  10553. if(strstr(command, "4"))
  10554. start_type = 4;
  10555. else if(strstr(command, "2"))
  10556. start_type = 2;
  10557. else
  10558. start_type = 1;
  10559. type = (sc_type)script_getnum(st,2);
  10560. tick = script_getnum(st,3);
  10561. val1 = script_getnum(st,4);
  10562. //If from NPC we make default flag 1 to be unavoidable
  10563. if(nd && nd->bl.id == fake_nd->bl.id)
  10564. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10565. else
  10566. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10567. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10568. if(script_hasdata(st,(6+start_type)))
  10569. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10570. else
  10571. bl = map_id2bl(st->rid);
  10572. uint16 skill_id;
  10573. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && (skill_id = status_db.getSkill(type)) > 0)
  10574. {// When there isn't a duration specified, try to get it from the skill_db
  10575. tick = skill_get_time(skill_id, val1);
  10576. }
  10577. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10578. bl = map_id2bl(potion_target);
  10579. tick /= 2;// Thrown potions only last half.
  10580. val4 = 1;// Mark that this was a thrown sc_effect
  10581. }
  10582. if(!bl)
  10583. return SCRIPT_CMD_SUCCESS;
  10584. switch(start_type) {
  10585. case 1:
  10586. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10587. break;
  10588. case 2:
  10589. val2 = script_getnum(st,5);
  10590. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10591. break;
  10592. case 4:
  10593. val2 = script_getnum(st,5);
  10594. val3 = script_getnum(st,6);
  10595. val4 = script_getnum(st,7);
  10596. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10597. break;
  10598. }
  10599. return SCRIPT_CMD_SUCCESS;
  10600. }
  10601. /// Ends one or all status effects on the target unit or on the attached player.
  10602. ///
  10603. /// sc_end <effect_id>{,<unit_id>};
  10604. BUILDIN_FUNC(sc_end)
  10605. {
  10606. struct block_list* bl;
  10607. int32 type;
  10608. type = script_getnum(st, 2);
  10609. if (script_hasdata(st, 3))
  10610. bl = map_id2bl(script_getnum(st, 3));
  10611. else
  10612. bl = map_id2bl(st->rid);
  10613. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10614. bl = map_id2bl(potion_target);
  10615. if (!bl)
  10616. return SCRIPT_CMD_SUCCESS;
  10617. if (type > SC_NONE && type < SC_MAX) {
  10618. status_change *sc = status_get_sc(bl);
  10619. if (sc == nullptr)
  10620. return SCRIPT_CMD_SUCCESS;
  10621. struct status_change_entry *sce = sc->getSCE(type);
  10622. if (sce == nullptr)
  10623. return SCRIPT_CMD_SUCCESS;
  10624. std::shared_ptr<s_status_change_db> sc_db = status_db.find( type );
  10625. if( sc_db == nullptr ){
  10626. ShowError( "buildin_sc_end: Unknown status change %d.\n", type );
  10627. return SCRIPT_CMD_FAILURE;
  10628. }
  10629. if( sc_db->flag[SCF_NOCLEARBUFF] && sc_db->flag[SCF_NOFORCEDEND] ){
  10630. ShowError( "buildin_sc_end: Status %d cannot be cleared.\n", type );
  10631. return SCRIPT_CMD_FAILURE;
  10632. }
  10633. //This should help status_change_end force disabling the SC in case it has no limit.
  10634. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10635. status_change_end(bl, (sc_type)type);
  10636. } else
  10637. status_change_clear(bl, 3); // remove all effects
  10638. return SCRIPT_CMD_SUCCESS;
  10639. }
  10640. /**
  10641. * Ends all status effects from any learned skill on the attached player.
  10642. * if <job_id> was given it will end the effect of that class for the attached player
  10643. * sc_end_class {<char_id>{,<job_id>}};
  10644. */
  10645. BUILDIN_FUNC(sc_end_class)
  10646. {
  10647. map_session_data *sd;
  10648. int32 class_;
  10649. if (!script_charid2sd(2, sd))
  10650. return SCRIPT_CMD_FAILURE;
  10651. if (script_hasdata(st, 3))
  10652. class_ = script_getnum(st, 3);
  10653. else
  10654. class_ = sd->status.class_;
  10655. if (!pcdb_checkid(class_)) {
  10656. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10657. return SCRIPT_CMD_FAILURE;
  10658. }
  10659. status_db.changeSkillTree(sd, class_);
  10660. return SCRIPT_CMD_SUCCESS;
  10661. }
  10662. /*==========================================
  10663. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10664. *------------------------------------------*/
  10665. BUILDIN_FUNC(getscrate)
  10666. {
  10667. struct block_list *bl;
  10668. int32 type;
  10669. t_tick rate;
  10670. type=script_getnum(st,2);
  10671. rate=script_getnum(st,3);
  10672. if( script_hasdata(st,4) ) //get for the bl assigned
  10673. bl = map_id2bl(script_getnum(st,4));
  10674. else
  10675. bl = map_id2bl(st->rid);
  10676. if (bl)
  10677. rate = status_get_sc_def(nullptr,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10678. script_pushint(st,rate);
  10679. return SCRIPT_CMD_SUCCESS;
  10680. }
  10681. /**
  10682. * getstatus(<effect type>{,<type>{,<char_id>}});
  10683. **/
  10684. BUILDIN_FUNC(getstatus)
  10685. {
  10686. int32 id, type;
  10687. map_session_data* sd;
  10688. if (!script_charid2sd(4,sd))
  10689. return SCRIPT_CMD_FAILURE;
  10690. id = script_getnum(st, 2);
  10691. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10692. if( id <= SC_NONE || id >= SC_MAX )
  10693. {// invalid status type given
  10694. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10695. return SCRIPT_CMD_SUCCESS;
  10696. }
  10697. status_change_entry* sce = sd->sc.getSCE( id );
  10698. // Check if the status is active
  10699. if( sce == nullptr ){
  10700. script_pushint(st, 0);
  10701. return SCRIPT_CMD_SUCCESS;
  10702. }
  10703. switch( type )
  10704. {
  10705. case 1:
  10706. script_pushint( st, sce->val1 );
  10707. break;
  10708. case 2:
  10709. script_pushint( st, sce->val2 );
  10710. break;
  10711. case 3:
  10712. script_pushint( st, sce->val3 );
  10713. break;
  10714. case 4:
  10715. script_pushint( st, sce->val4 );
  10716. break;
  10717. case 5:
  10718. {
  10719. const struct TimerData* timer = get_timer( sce->timer );
  10720. if( timer )
  10721. {// return the amount of time remaining
  10722. script_pushint(st, timer->tick - gettick());
  10723. } else {
  10724. script_pushint(st, -1);
  10725. }
  10726. }
  10727. break;
  10728. default: script_pushint(st, 1); break;
  10729. }
  10730. return SCRIPT_CMD_SUCCESS;
  10731. }
  10732. /*==========================================
  10733. *
  10734. *------------------------------------------*/
  10735. BUILDIN_FUNC(debugmes)
  10736. {
  10737. const char *str;
  10738. str=script_getstr(st,2);
  10739. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10740. return SCRIPT_CMD_SUCCESS;
  10741. }
  10742. BUILDIN_FUNC( errormes ){
  10743. ShowError( "%s\n", script_getstr( st, 2 ) );
  10744. script_reportsrc( st );
  10745. return SCRIPT_CMD_SUCCESS;
  10746. }
  10747. /*==========================================
  10748. *------------------------------------------*/
  10749. BUILDIN_FUNC(catchpet)
  10750. {
  10751. int32 pet_id;
  10752. TBL_PC *sd;
  10753. if( !script_rid2sd(sd) )
  10754. return SCRIPT_CMD_SUCCESS;
  10755. pet_id= script_getnum(st,2);
  10756. pet_catch_process1(sd,pet_id);
  10757. return SCRIPT_CMD_SUCCESS;
  10758. }
  10759. /*==========================================
  10760. * [orn]
  10761. *------------------------------------------*/
  10762. BUILDIN_FUNC(homunculus_evolution)
  10763. {
  10764. TBL_PC *sd;
  10765. if( !script_rid2sd(sd) )
  10766. return SCRIPT_CMD_SUCCESS;
  10767. if(hom_is_active(sd->hd))
  10768. {
  10769. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10770. hom_evolution(sd->hd);
  10771. else
  10772. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10773. }
  10774. return SCRIPT_CMD_SUCCESS;
  10775. }
  10776. /*==========================================
  10777. * Checks for vaporized morph state
  10778. * and deletes ITEMID_STRANGE_EMBRYO.
  10779. *------------------------------------------*/
  10780. BUILDIN_FUNC(homunculus_mutate)
  10781. {
  10782. uint16 homun_id;
  10783. TBL_PC *sd;
  10784. if( !script_rid2sd(sd) || sd->hd == nullptr )
  10785. return SCRIPT_CMD_SUCCESS;
  10786. if(script_hasdata(st,2))
  10787. homun_id = script_getnum(st,2);
  10788. else
  10789. homun_id = rnd_value<uint16>(MER_EIRA, MER_ELEANOR);
  10790. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10791. int32 m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10792. int32 m_id = hom_class2mapid(homun_id);
  10793. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10794. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10795. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10796. hom_call(sd); // Respawn homunculus.
  10797. hom_mutate(sd->hd, homun_id);
  10798. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10799. script_pushint(st, 1);
  10800. return SCRIPT_CMD_SUCCESS;
  10801. } else
  10802. clif_emotion(&sd->bl, ET_SWEAT);
  10803. } else
  10804. clif_emotion(&sd->bl, ET_SWEAT);
  10805. script_pushint(st, 0);
  10806. return SCRIPT_CMD_SUCCESS;
  10807. }
  10808. /*==========================================
  10809. * Puts homunculus into morph state
  10810. * and gives ITEMID_STRANGE_EMBRYO.
  10811. *------------------------------------------*/
  10812. BUILDIN_FUNC(morphembryo)
  10813. {
  10814. struct item item_tmp;
  10815. TBL_PC *sd;
  10816. if( !script_rid2sd(sd) || sd->hd == nullptr )
  10817. return SCRIPT_CMD_SUCCESS;
  10818. if( hom_is_active(sd->hd) ) {
  10819. int32 m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10820. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10821. char i;
  10822. memset(&item_tmp, 0, sizeof(item_tmp));
  10823. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10824. item_tmp.identify = 1;
  10825. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10826. clif_additem(sd, 0, 0, i);
  10827. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10828. } else {
  10829. hom_vaporize(sd, HOM_ST_MORPH);
  10830. script_pushint(st, 1);
  10831. return SCRIPT_CMD_SUCCESS;
  10832. }
  10833. } else
  10834. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10835. } else
  10836. clif_emotion(&sd->bl, ET_SWEAT);
  10837. script_pushint(st, 0);
  10838. return SCRIPT_CMD_SUCCESS;
  10839. }
  10840. // [Zephyrus]
  10841. BUILDIN_FUNC(homunculus_shuffle)
  10842. {
  10843. TBL_PC *sd;
  10844. if( !script_rid2sd(sd) )
  10845. return SCRIPT_CMD_SUCCESS;
  10846. if(hom_is_active(sd->hd))
  10847. hom_shuffle(sd->hd);
  10848. return SCRIPT_CMD_SUCCESS;
  10849. }
  10850. /*==========================================
  10851. * Check for homunculus state.
  10852. * Return: -1 = No homunculus
  10853. * 0 = Homunculus is active
  10854. * 1 = Homunculus is vaporized (rest)
  10855. * 2 = Homunculus is in morph state
  10856. *------------------------------------------*/
  10857. BUILDIN_FUNC(checkhomcall)
  10858. {
  10859. TBL_PC *sd;
  10860. TBL_HOM *hd;
  10861. if( !script_rid2sd(sd) )
  10862. return SCRIPT_CMD_SUCCESS;
  10863. hd = sd->hd;
  10864. if( !hd )
  10865. script_pushint(st, -1);
  10866. else
  10867. script_pushint(st, hd->homunculus.vaporize);
  10868. return SCRIPT_CMD_SUCCESS;
  10869. }
  10870. //These two functions bring the eA MAPID_* class functionality to scripts.
  10871. BUILDIN_FUNC(eaclass)
  10872. {
  10873. int32 class_;
  10874. if( script_hasdata(st,2) )
  10875. class_ = script_getnum(st,2);
  10876. else {
  10877. TBL_PC *sd;
  10878. if (!script_charid2sd(3,sd)) {
  10879. script_pushint(st,-1);
  10880. return SCRIPT_CMD_SUCCESS;
  10881. }
  10882. class_ = sd->status.class_;
  10883. }
  10884. script_pushint(st,pc_jobid2mapid(class_));
  10885. return SCRIPT_CMD_SUCCESS;
  10886. }
  10887. BUILDIN_FUNC(roclass)
  10888. {
  10889. int32 class_ =script_getnum(st,2);
  10890. int32 sex;
  10891. if( script_hasdata(st,3) )
  10892. sex = script_getnum(st,3);
  10893. else {
  10894. TBL_PC *sd;
  10895. if (st->rid && script_rid2sd(sd))
  10896. sex = sd->status.sex;
  10897. else
  10898. sex = SEX_MALE; //Just use male when not found.
  10899. }
  10900. script_pushint(st,pc_mapid2jobid(class_, sex));
  10901. return SCRIPT_CMD_SUCCESS;
  10902. }
  10903. /*==========================================
  10904. * Tells client to open a hatching window, used for pet incubator
  10905. *------------------------------------------*/
  10906. BUILDIN_FUNC(birthpet)
  10907. {
  10908. TBL_PC *sd;
  10909. if( !script_rid2sd(sd) )
  10910. return SCRIPT_CMD_SUCCESS;
  10911. if( sd->status.pet_id )
  10912. {// do not send egg list, when you already have a pet
  10913. return SCRIPT_CMD_SUCCESS;
  10914. }
  10915. clif_sendegg(sd);
  10916. return SCRIPT_CMD_SUCCESS;
  10917. }
  10918. /**
  10919. * resetlvl <action type>{,<char_id>};
  10920. * @param action_type:
  10921. * 1 : make like after rebirth
  10922. * 2 : blvl,jlvl=1, skillpoint=0
  10923. * 3 : don't reset skill, blvl=1
  10924. * 4 : jlvl=0
  10925. * @author AppleGirl
  10926. **/
  10927. BUILDIN_FUNC(resetlvl)
  10928. {
  10929. TBL_PC *sd;
  10930. int32 type=script_getnum(st,2);
  10931. if (!script_charid2sd(3,sd))
  10932. return SCRIPT_CMD_FAILURE;
  10933. pc_resetlvl(sd,type);
  10934. return SCRIPT_CMD_SUCCESS;
  10935. }
  10936. /**
  10937. * Reset a player status point
  10938. * resetstatus({<char_id>});
  10939. **/
  10940. BUILDIN_FUNC(resetstatus)
  10941. {
  10942. TBL_PC *sd;
  10943. if (!script_charid2sd(2,sd))
  10944. return SCRIPT_CMD_FAILURE;
  10945. pc_resetstate(sd);
  10946. return SCRIPT_CMD_SUCCESS;
  10947. }
  10948. /**
  10949. * Reset player's skill
  10950. * resetskill({<char_id>});
  10951. **/
  10952. BUILDIN_FUNC(resetskill)
  10953. {
  10954. TBL_PC *sd;
  10955. if (!script_charid2sd(2,sd))
  10956. return SCRIPT_CMD_FAILURE;
  10957. pc_resetskill(sd,1);
  10958. return SCRIPT_CMD_SUCCESS;
  10959. }
  10960. /**
  10961. * Reset SG designated maps
  10962. * resetfeel({<char_id>});
  10963. **/
  10964. BUILDIN_FUNC(resetfeel)
  10965. {
  10966. TBL_PC *sd;
  10967. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10968. return SCRIPT_CMD_FAILURE;
  10969. pc_resetfeel(sd);
  10970. return SCRIPT_CMD_SUCCESS;
  10971. }
  10972. /**
  10973. * Reset SG designated monsters
  10974. * resethate({<char_id>});
  10975. **/
  10976. BUILDIN_FUNC(resethate)
  10977. {
  10978. TBL_PC *sd;
  10979. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10980. return SCRIPT_CMD_FAILURE;
  10981. pc_resethate(sd);
  10982. return SCRIPT_CMD_SUCCESS;
  10983. }
  10984. /**
  10985. * Counts total amount of skill points.
  10986. * skillpointcount({<char_id>})
  10987. **/
  10988. BUILDIN_FUNC(skillpointcount)
  10989. {
  10990. TBL_PC *sd;
  10991. if (!script_charid2sd(2,sd))
  10992. return SCRIPT_CMD_FAILURE;
  10993. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10994. return SCRIPT_CMD_SUCCESS;
  10995. }
  10996. /*==========================================
  10997. *
  10998. *------------------------------------------*/
  10999. BUILDIN_FUNC(changebase)
  11000. {
  11001. TBL_PC *sd=nullptr;
  11002. int32 vclass;
  11003. if( !script_mapid2sd(3,sd) )
  11004. return SCRIPT_CMD_SUCCESS;
  11005. vclass = script_getnum(st,2);
  11006. if(vclass == JOB_WEDDING)
  11007. {
  11008. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  11009. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  11010. )
  11011. return SCRIPT_CMD_SUCCESS;
  11012. }
  11013. if(!sd->disguise && vclass != sd->vd.class_) {
  11014. status_set_viewdata(&sd->bl, vclass);
  11015. //Updated client view. Base, Weapon and Cloth Colors.
  11016. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  11017. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  11018. if (sd->vd.cloth_color)
  11019. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  11020. if (sd->vd.body_style)
  11021. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  11022. clif_skillinfoblock(sd);
  11023. }
  11024. return SCRIPT_CMD_SUCCESS;
  11025. }
  11026. /**
  11027. * Change account sex and unequip all item and request for a changesex to char-serv
  11028. * changesex({<char_id>});
  11029. */
  11030. BUILDIN_FUNC(changesex)
  11031. {
  11032. int32 i;
  11033. TBL_PC *sd = nullptr;
  11034. if (!script_charid2sd(2,sd))
  11035. return SCRIPT_CMD_FAILURE;
  11036. pc_resetskill(sd,4);
  11037. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  11038. for(i = 0; i < EQI_MAX; i++) {
  11039. if (sd->equip_index[i] >= 0)
  11040. pc_unequipitem(sd, sd->equip_index[i], 3);
  11041. }
  11042. chrif_changesex(sd, true);
  11043. return SCRIPT_CMD_SUCCESS;
  11044. }
  11045. /**
  11046. * Change character's sex and unequip all item and request for a changesex to char-serv
  11047. * changecharsex({<char_id>});
  11048. */
  11049. BUILDIN_FUNC(changecharsex)
  11050. {
  11051. #if PACKETVER >= 20141016
  11052. int32 i;
  11053. TBL_PC *sd = nullptr;
  11054. if (!script_charid2sd(2,sd))
  11055. return SCRIPT_CMD_FAILURE;
  11056. pc_resetskill(sd,4);
  11057. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  11058. for (i = 0; i < EQI_MAX; i++) {
  11059. if (sd->equip_index[i] >= 0)
  11060. pc_unequipitem(sd, sd->equip_index[i], 3);
  11061. }
  11062. chrif_changesex(sd, false);
  11063. return SCRIPT_CMD_SUCCESS;
  11064. #else
  11065. return SCRIPT_CMD_FAILURE;
  11066. #endif
  11067. }
  11068. /*==========================================
  11069. * Works like 'announce' but outputs in the common chat window
  11070. *------------------------------------------*/
  11071. BUILDIN_FUNC(globalmes)
  11072. {
  11073. struct block_list *bl = map_id2bl(st->oid);
  11074. struct npc_data *nd = (struct npc_data *)bl;
  11075. const char *name=nullptr,*mes;
  11076. mes=script_getstr(st,2);
  11077. if(mes==nullptr)
  11078. return SCRIPT_CMD_SUCCESS;
  11079. if(script_hasdata(st,3)){ // npc name to display
  11080. name=script_getstr(st,3);
  11081. } else {
  11082. name=nd->name; //use current npc name
  11083. }
  11084. npc_globalmessage(name,mes); // broadcast to all players connected
  11085. return SCRIPT_CMD_SUCCESS;
  11086. }
  11087. /////////////////////////////////////////////////////////////////////
  11088. // NPC waiting room (chat room)
  11089. //
  11090. /// Creates a waiting room (chat room) for this npc.
  11091. ///
  11092. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  11093. BUILDIN_FUNC(waitingroom)
  11094. {
  11095. struct npc_data* nd;
  11096. const char* title = script_getstr(st, 2);
  11097. int32 limit = script_getnum(st, 3);
  11098. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  11099. int32 trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  11100. int32 zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  11101. int32 minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  11102. int32 maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  11103. nd = (struct npc_data *)map_id2bl(st->oid);
  11104. if( nd != nullptr )
  11105. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  11106. return SCRIPT_CMD_SUCCESS;
  11107. }
  11108. /// Removes the waiting room of the current or target npc.
  11109. ///
  11110. /// delwaitingroom "<npc_name>";
  11111. /// delwaitingroom;
  11112. BUILDIN_FUNC(delwaitingroom)
  11113. {
  11114. struct npc_data* nd;
  11115. if( script_hasdata(st,2) )
  11116. nd = npc_name2id(script_getstr(st, 2));
  11117. else
  11118. nd = (struct npc_data *)map_id2bl(st->oid);
  11119. if( nd != nullptr )
  11120. chat_deletenpcchat(nd);
  11121. return SCRIPT_CMD_SUCCESS;
  11122. }
  11123. /// Kick the specified player from the waiting room of the target npc.
  11124. ///
  11125. /// waitingroomkick "<npc_name>", <kickusername>;
  11126. BUILDIN_FUNC(waitingroomkick)
  11127. {
  11128. struct npc_data* nd;
  11129. struct chat_data* cd;
  11130. const char* kickusername;
  11131. nd = npc_name2id(script_getstr(st,2));
  11132. kickusername = script_getstr(st,3);
  11133. if( nd != nullptr && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != nullptr )
  11134. chat_npckickchat(cd, kickusername);
  11135. return SCRIPT_CMD_SUCCESS;
  11136. }
  11137. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  11138. /// Num users stored in .@waitingroom_usercount
  11139. ///
  11140. /// getwaitingroomusers "<npc_name>";
  11141. BUILDIN_FUNC(getwaitingroomusers)
  11142. {
  11143. struct npc_data* nd;
  11144. struct chat_data* cd;
  11145. int32 i, j=0;
  11146. if( script_hasdata(st,2) )
  11147. nd = npc_name2id(script_getstr(st, 2));
  11148. else
  11149. nd = (struct npc_data *)map_id2bl(st->oid);
  11150. if( nd != nullptr && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != nullptr ) {
  11151. for(i = 0; i < cd->users; ++i) {
  11152. setd_sub_num( st, nullptr, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, nullptr );
  11153. j++;
  11154. }
  11155. setd_sub_num( st, nullptr, ".@waitingroom_usercount", 0, j, nullptr );
  11156. }
  11157. return SCRIPT_CMD_SUCCESS;
  11158. }
  11159. /// Kicks all the players from the waiting room of the current or target npc.
  11160. ///
  11161. /// kickwaitingroomall "<npc_name>";
  11162. /// kickwaitingroomall;
  11163. BUILDIN_FUNC(waitingroomkickall)
  11164. {
  11165. struct npc_data* nd;
  11166. struct chat_data* cd;
  11167. if( script_hasdata(st,2) )
  11168. nd = npc_name2id(script_getstr(st,2));
  11169. else
  11170. nd = (struct npc_data *)map_id2bl(st->oid);
  11171. if( nd != nullptr && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != nullptr )
  11172. chat_npckickall(cd);
  11173. return SCRIPT_CMD_SUCCESS;
  11174. }
  11175. /// Enables the waiting room event of the current or target npc.
  11176. ///
  11177. /// enablewaitingroomevent "<npc_name>";
  11178. /// enablewaitingroomevent;
  11179. BUILDIN_FUNC(enablewaitingroomevent)
  11180. {
  11181. struct npc_data* nd;
  11182. struct chat_data* cd;
  11183. if( script_hasdata(st,2) )
  11184. nd = npc_name2id(script_getstr(st, 2));
  11185. else
  11186. nd = (struct npc_data *)map_id2bl(st->oid);
  11187. if( nd != nullptr && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != nullptr )
  11188. chat_enableevent(cd);
  11189. return SCRIPT_CMD_SUCCESS;
  11190. }
  11191. /// Disables the waiting room event of the current or target npc.
  11192. ///
  11193. /// disablewaitingroomevent "<npc_name>";
  11194. /// disablewaitingroomevent;
  11195. BUILDIN_FUNC(disablewaitingroomevent)
  11196. {
  11197. struct npc_data *nd;
  11198. struct chat_data *cd;
  11199. if( script_hasdata(st,2) )
  11200. nd = npc_name2id(script_getstr(st, 2));
  11201. else
  11202. nd = (struct npc_data *)map_id2bl(st->oid);
  11203. if( nd != nullptr && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != nullptr )
  11204. chat_disableevent(cd);
  11205. return SCRIPT_CMD_SUCCESS;
  11206. }
  11207. /// Returns info on the waiting room of the current or target npc.
  11208. /// Returns -1 if the type unknown
  11209. /// <type>=0 : current number of users
  11210. /// <type>=1 : maximum number of users allowed
  11211. /// <type>=2 : the number of users that trigger the event
  11212. /// <type>=3 : if the trigger is disabled
  11213. /// <type>=4 : the title of the waiting room
  11214. /// <type>=5 : the password of the waiting room
  11215. /// <type>=16 : the name of the waiting room event
  11216. /// <type>=32 : if the waiting room is full
  11217. /// <type>=33 : if there are enough users to trigger the event
  11218. ///
  11219. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  11220. /// getwaitingroomstate(<type>) -> <info>
  11221. BUILDIN_FUNC(getwaitingroomstate)
  11222. {
  11223. struct npc_data *nd;
  11224. struct chat_data *cd;
  11225. int32 type;
  11226. type = script_getnum(st,2);
  11227. if( script_hasdata(st,3) )
  11228. nd = npc_name2id(script_getstr(st, 3));
  11229. else
  11230. nd = (struct npc_data *)map_id2bl(st->oid);
  11231. if( nd == nullptr || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == nullptr )
  11232. {
  11233. script_pushint(st, -1);
  11234. return SCRIPT_CMD_SUCCESS;
  11235. }
  11236. switch(type)
  11237. {
  11238. case 0: script_pushint(st, cd->users); break;
  11239. case 1: script_pushint(st, cd->limit); break;
  11240. case 2: script_pushint(st, cd->trigger&0x7f); break;
  11241. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  11242. case 4: script_pushstrcopy(st, cd->title); break;
  11243. case 5: script_pushstrcopy(st, cd->pass); break;
  11244. case 16: script_pushstrcopy(st, cd->npc_event);break;
  11245. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  11246. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  11247. default: script_pushint(st, -1); break;
  11248. }
  11249. return SCRIPT_CMD_SUCCESS;
  11250. }
  11251. /// Warps the trigger or target amount of players to the target map and position.
  11252. /// Players are automatically removed from the waiting room.
  11253. /// Those waiting the longest will get warped first.
  11254. /// The target map can be "Random" for a random position in the current map,
  11255. /// and "SavePoint" for the savepoint map+position.
  11256. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  11257. ///
  11258. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  11259. /// The total number of teleported players is put into $@warpwaitingpcnum
  11260. ///
  11261. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  11262. /// warpwaitingpc "<map name>",<x>,<y>;
  11263. BUILDIN_FUNC(warpwaitingpc)
  11264. {
  11265. int32 x;
  11266. int32 y;
  11267. int32 i;
  11268. int32 n;
  11269. const char* map_name;
  11270. struct npc_data* nd;
  11271. struct chat_data* cd;
  11272. nd = (struct npc_data *)map_id2bl(st->oid);
  11273. if( nd == nullptr || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == nullptr )
  11274. return SCRIPT_CMD_SUCCESS;
  11275. map_name = script_getstr(st,2);
  11276. x = script_getnum(st,3);
  11277. y = script_getnum(st,4);
  11278. n = cd->trigger&0x7f;
  11279. if( script_hasdata(st,5) )
  11280. n = script_getnum(st,5);
  11281. for( i = 0; i < n && cd->users > 0; i++ )
  11282. {
  11283. TBL_PC* sd = cd->usersd[0];
  11284. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  11285. {// can't teleport on this map
  11286. break;
  11287. }
  11288. if( cd->zeny )
  11289. {// fee set
  11290. if( (uint32)sd->status.zeny < cd->zeny )
  11291. {// no zeny to cover set fee
  11292. break;
  11293. }
  11294. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC);
  11295. }
  11296. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  11297. if( strcmp(map_name,"Random") == 0 )
  11298. pc_randomwarp(sd,CLR_TELEPORT,true);
  11299. else if( strcmp(map_name,"SavePoint") == 0 )
  11300. pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  11301. else
  11302. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  11303. }
  11304. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  11305. return SCRIPT_CMD_SUCCESS;
  11306. }
  11307. /////////////////////////////////////////////////////////////////////
  11308. // ...
  11309. //
  11310. /// Detaches a character from a script.
  11311. ///
  11312. /// @param st Script state to detach the character from.
  11313. void script_detach_rid(struct script_state* st)
  11314. {
  11315. if(st->rid)
  11316. {
  11317. script_detach_state(st, false);
  11318. st->rid = 0;
  11319. }
  11320. }
  11321. /*=========================================================================
  11322. * Attaches a set of RIDs to the current script. [digitalhamster]
  11323. * addrid(<type>{,<flag>{,<parameters>}});
  11324. * <type>:
  11325. * 0 : All players in the server.
  11326. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  11327. * 2 : Party members of a specified party ID.
  11328. * [ Parameters: <party id> ]
  11329. * 3 : Guild members of a specified guild ID.
  11330. * [ Parameters: <guild id> ]
  11331. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  11332. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  11333. * 5 : All players in the map.
  11334. * [ Parameters: "<map name>" ]
  11335. * 6 : Battleground members of a specified battleground ID.
  11336. * [ Parameters: <battleground id> ]
  11337. * 7 : Clan members of a specified clan ID.
  11338. * [ Parameters: <clan id> ]
  11339. * Account ID: The specified account ID.
  11340. * <flag>:
  11341. * 0 : Players are always attached. (default)
  11342. * 1 : Players currently running another script will not be attached.
  11343. *-------------------------------------------------------------------------*/
  11344. static int32 buildin_addrid_sub(struct block_list *bl,va_list ap)
  11345. {
  11346. int32 forceflag;
  11347. map_session_data *sd = (TBL_PC *)bl;
  11348. struct script_state* st;
  11349. st = va_arg(ap,struct script_state*);
  11350. forceflag = va_arg(ap,int);
  11351. if(!forceflag || !sd->st)
  11352. if(sd->status.account_id != st->rid)
  11353. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11354. return 0;
  11355. }
  11356. BUILDIN_FUNC(addrid)
  11357. {
  11358. struct s_mapiterator* iter;
  11359. struct block_list *bl;
  11360. TBL_PC *sd;
  11361. if(st->rid < 1) {
  11362. st->state = END;
  11363. bl = map_id2bl(st->oid);
  11364. } else
  11365. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  11366. iter = mapit_getallusers();
  11367. int32 type = script_getnum(st,2);
  11368. bool forceflag = (script_hasdata(st,3) ? script_getnum(st,3) != 0 : 0);
  11369. switch(type) {
  11370. case 0:
  11371. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11372. if(!forceflag || !sd->st)
  11373. if(sd->status.account_id != st->rid) //attached player already runs.
  11374. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11375. }
  11376. break;
  11377. case 1:
  11378. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11379. if(!forceflag || !sd->st)
  11380. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  11381. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11382. }
  11383. break;
  11384. case 2:
  11385. if(script_getnum(st,4) == 0) {
  11386. script_pushint(st,0);
  11387. mapit_free(iter);
  11388. return SCRIPT_CMD_SUCCESS;
  11389. }
  11390. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11391. if(!forceflag || !sd->st)
  11392. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  11393. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11394. }
  11395. break;
  11396. case 3:
  11397. if(script_getnum(st,4) == 0) {
  11398. script_pushint(st,0);
  11399. mapit_free(iter);
  11400. return SCRIPT_CMD_SUCCESS;
  11401. }
  11402. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11403. if(!forceflag || !sd->st)
  11404. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  11405. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11406. }
  11407. break;
  11408. case 4:
  11409. map_foreachinallarea(buildin_addrid_sub,
  11410. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  11411. st,forceflag);//4-x0 , 5-y0 , 6-x1, 7-y1
  11412. break;
  11413. case 5:
  11414. if (script_getstr(st, 4) == nullptr) {
  11415. script_pushint(st, 0);
  11416. mapit_free(iter);
  11417. return SCRIPT_CMD_FAILURE;
  11418. }
  11419. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  11420. script_pushint(st, 0);
  11421. mapit_free(iter);
  11422. return SCRIPT_CMD_FAILURE;
  11423. }
  11424. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  11425. break;
  11426. case 6:
  11427. if (script_getnum(st, 4) == 0) {
  11428. script_pushint(st, 0);
  11429. mapit_free(iter);
  11430. return SCRIPT_CMD_FAILURE;
  11431. }
  11432. for (sd = (TBL_PC *)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC *)mapit_next(iter)) {
  11433. if (!forceflag || !sd->st)
  11434. if ((sd->status.account_id != st->rid) && (sd->bg_id == script_getnum(st, 4))) //attached player already runs.
  11435. run_script(st->script, st->pos, sd->status.account_id, st->oid);
  11436. }
  11437. break;
  11438. case 7:
  11439. if (script_getnum(st, 4) == 0) {
  11440. script_pushint(st, 0);
  11441. mapit_free(iter);
  11442. return SCRIPT_CMD_FAILURE;
  11443. }
  11444. for (sd = (TBL_PC *)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC *)mapit_next(iter)) {
  11445. if (!forceflag || !sd->st)
  11446. if ((sd->status.account_id != st->rid) && (sd->status.clan_id == script_getnum(st, 4))) //attached player already runs.
  11447. run_script(st->script, st->pos, sd->status.account_id, st->oid);
  11448. }
  11449. break;
  11450. default:
  11451. mapit_free(iter);
  11452. if((map_id2sd(type)) == nullptr) { // Player not found.
  11453. script_pushint(st,0);
  11454. return SCRIPT_CMD_SUCCESS;
  11455. }
  11456. if(!forceflag || !map_id2sd(type)->st) {
  11457. run_script(st->script,st->pos,type,st->oid);
  11458. script_pushint(st,1);
  11459. }
  11460. return SCRIPT_CMD_SUCCESS;
  11461. }
  11462. mapit_free(iter);
  11463. script_pushint(st,1);
  11464. return SCRIPT_CMD_SUCCESS;
  11465. }
  11466. /*==========================================
  11467. * Attach sd char id to script and detach current one if any
  11468. *------------------------------------------*/
  11469. BUILDIN_FUNC(attachrid)
  11470. {
  11471. int32 rid = script_getnum(st,2);
  11472. bool force;
  11473. if( script_hasdata(st,3) ){
  11474. force = script_getnum(st,3) != 0;
  11475. }else{
  11476. force = true;
  11477. }
  11478. map_session_data* sd = map_id2sd(rid);
  11479. if( sd != nullptr && ( !sd->npc_id || force ) ){
  11480. script_detach_rid(st);
  11481. st->rid = rid;
  11482. script_attach_state(st);
  11483. script_pushint(st,true);
  11484. }else{
  11485. script_pushint(st,false);
  11486. }
  11487. return SCRIPT_CMD_SUCCESS;
  11488. }
  11489. /*==========================================
  11490. * Detach script to rid
  11491. *------------------------------------------*/
  11492. BUILDIN_FUNC(detachrid)
  11493. {
  11494. script_detach_rid(st);
  11495. return SCRIPT_CMD_SUCCESS;
  11496. }
  11497. /*==========================================
  11498. * Chk if account connected, (and charid from account if specified)
  11499. *------------------------------------------*/
  11500. BUILDIN_FUNC(isloggedin)
  11501. {
  11502. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11503. if (script_hasdata(st,3) && sd &&
  11504. sd->status.char_id != script_getnum(st,3))
  11505. sd = nullptr;
  11506. push_val(st->stack,C_INT,sd!=nullptr);
  11507. return SCRIPT_CMD_SUCCESS;
  11508. }
  11509. /*==========================================
  11510. *
  11511. *------------------------------------------*/
  11512. BUILDIN_FUNC(setmapflagnosave)
  11513. {
  11514. int16 m,x,y;
  11515. unsigned short mapindex;
  11516. const char *str,*str2;
  11517. union u_mapflag_args args = {};
  11518. str=script_getstr(st,2);
  11519. str2=script_getstr(st,3);
  11520. x=script_getnum(st,4);
  11521. y=script_getnum(st,5);
  11522. m = map_mapname2mapid(str);
  11523. mapindex = mapindex_name2id(str2);
  11524. if(m < 0) {
  11525. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11526. return SCRIPT_CMD_FAILURE;
  11527. }
  11528. args.nosave.map = mapindex;
  11529. args.nosave.x = x;
  11530. args.nosave.y = y;
  11531. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11532. return SCRIPT_CMD_SUCCESS;
  11533. }
  11534. BUILDIN_FUNC(getmapflag)
  11535. {
  11536. int16 m;
  11537. int32 mf;
  11538. const char *str;
  11539. str=script_getstr(st,2);
  11540. m = map_mapname2mapid(str);
  11541. if (m < 0) {
  11542. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11543. return SCRIPT_CMD_FAILURE;
  11544. }
  11545. mf = script_getnum(st, 3);
  11546. if( mf < MF_MIN || mf > MF_MAX ){
  11547. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11548. return SCRIPT_CMD_FAILURE;
  11549. }
  11550. union u_mapflag_args args = {};
  11551. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11552. args.flag_val = SKILLDMG_MAX;
  11553. else
  11554. FETCH(4, args.flag_val);
  11555. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11556. return SCRIPT_CMD_SUCCESS;
  11557. }
  11558. BUILDIN_FUNC(setmapflag)
  11559. {
  11560. int16 m;
  11561. int32 mf;
  11562. const char *str;
  11563. str = script_getstr(st,2);
  11564. m = map_mapname2mapid(str);
  11565. if (m < 0) {
  11566. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11567. return SCRIPT_CMD_FAILURE;
  11568. }
  11569. mf = script_getnum(st, 3);
  11570. if( mf < MF_MIN || mf > MF_MAX ){
  11571. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11572. return SCRIPT_CMD_FAILURE;
  11573. }
  11574. union u_mapflag_args args = {};
  11575. switch(mf) {
  11576. case MF_SKILL_DAMAGE:
  11577. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11578. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11579. else {
  11580. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11581. return SCRIPT_CMD_FAILURE;
  11582. }
  11583. break;
  11584. case MF_SKILL_DURATION:
  11585. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11586. args.skill_duration.skill_id = script_getnum(st, 4);
  11587. if (!skill_get_index(args.skill_duration.skill_id)) {
  11588. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11589. return SCRIPT_CMD_FAILURE;
  11590. }
  11591. args.skill_duration.per = script_getnum(st, 5);
  11592. } else {
  11593. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11594. return SCRIPT_CMD_FAILURE;
  11595. }
  11596. break;
  11597. case MF_NOSAVE: // Assume setting "SavePoint"
  11598. args.nosave.map = 0;
  11599. args.nosave.x = -1;
  11600. args.nosave.y = -1;
  11601. break;
  11602. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11603. args.nightmaredrop.drop_id = -1;
  11604. args.nightmaredrop.drop_per = 300;
  11605. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11606. break;
  11607. default:
  11608. FETCH(4, args.flag_val);
  11609. break;
  11610. }
  11611. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11612. return SCRIPT_CMD_SUCCESS;
  11613. }
  11614. BUILDIN_FUNC(removemapflag)
  11615. {
  11616. int16 m;
  11617. int32 mf;
  11618. const char *str;
  11619. union u_mapflag_args args = {};
  11620. str = script_getstr(st, 2);
  11621. m = map_mapname2mapid(str);
  11622. if (m < 0) {
  11623. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11624. return SCRIPT_CMD_FAILURE;
  11625. }
  11626. mf = script_getnum(st, 3);
  11627. if( mf < MF_MIN || mf > MF_MAX ){
  11628. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11629. return SCRIPT_CMD_FAILURE;
  11630. }
  11631. FETCH(4, args.flag_val);
  11632. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11633. return SCRIPT_CMD_SUCCESS;
  11634. }
  11635. BUILDIN_FUNC(pvpon)
  11636. {
  11637. int16 m;
  11638. const char *str = script_getstr(st,2);
  11639. m = map_mapname2mapid(str);
  11640. if (m < 0) {
  11641. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11642. return SCRIPT_CMD_FAILURE;
  11643. }
  11644. if (!map_getmapflag(m, MF_PVP))
  11645. map_setmapflag(m, MF_PVP, true);
  11646. return SCRIPT_CMD_SUCCESS;
  11647. }
  11648. BUILDIN_FUNC(pvpoff)
  11649. {
  11650. int16 m;
  11651. const char *str = script_getstr(st,2);
  11652. m = map_mapname2mapid(str);
  11653. if (m < 0) {
  11654. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11655. return SCRIPT_CMD_FAILURE;
  11656. }
  11657. if (map_getmapflag(m, MF_PVP))
  11658. map_setmapflag(m, MF_PVP, false);
  11659. return SCRIPT_CMD_SUCCESS;
  11660. }
  11661. BUILDIN_FUNC(gvgon)
  11662. {
  11663. int16 m;
  11664. const char *str = script_getstr(st,2);
  11665. m = map_mapname2mapid(str);
  11666. if (m < 0) {
  11667. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11668. return SCRIPT_CMD_FAILURE;
  11669. }
  11670. if (!map_getmapflag(m, MF_GVG))
  11671. map_setmapflag(m, MF_GVG, true);
  11672. return SCRIPT_CMD_SUCCESS;
  11673. }
  11674. BUILDIN_FUNC(gvgoff)
  11675. {
  11676. int16 m;
  11677. const char *str = script_getstr(st,2);
  11678. m = map_mapname2mapid(str);
  11679. if (m < 0) {
  11680. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11681. return SCRIPT_CMD_FAILURE;
  11682. }
  11683. if (map_getmapflag(m, MF_GVG))
  11684. map_setmapflag(m, MF_GVG, false);
  11685. return SCRIPT_CMD_SUCCESS;
  11686. }
  11687. BUILDIN_FUNC(gvgon3)
  11688. {
  11689. int16 m;
  11690. const char *str = script_getstr(st,2);
  11691. m = map_mapname2mapid(str);
  11692. if (m < 0) {
  11693. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11694. return SCRIPT_CMD_FAILURE;
  11695. }
  11696. if (!map_getmapflag(m, MF_GVG_TE))
  11697. map_setmapflag(m, MF_GVG_TE, true);
  11698. return SCRIPT_CMD_SUCCESS;
  11699. }
  11700. BUILDIN_FUNC(gvgoff3)
  11701. {
  11702. int16 m;
  11703. const char *str = script_getstr(st,2);
  11704. m = map_mapname2mapid(str);
  11705. if (m < 0) {
  11706. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11707. return SCRIPT_CMD_FAILURE;
  11708. }
  11709. if (map_getmapflag(m, MF_GVG_TE))
  11710. map_setmapflag(m, MF_GVG_TE, false);
  11711. return SCRIPT_CMD_SUCCESS;
  11712. }
  11713. /**
  11714. * Shows an emotion on top of a NPC by default or the given GID
  11715. * emotion <emotion ID>{,<target ID>};
  11716. */
  11717. BUILDIN_FUNC(emotion)
  11718. {
  11719. struct block_list *bl = nullptr;
  11720. int32 type = script_getnum(st,2);
  11721. if (type < ET_SURPRISE || type >= ET_MAX) {
  11722. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11723. return SCRIPT_CMD_FAILURE;
  11724. }
  11725. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11726. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11727. return SCRIPT_CMD_FAILURE;
  11728. }
  11729. if (!bl)
  11730. bl = map_id2bl(st->oid);
  11731. clif_emotion(bl, type);
  11732. return SCRIPT_CMD_SUCCESS;
  11733. }
  11734. static int32 buildin_maprespawnguildid_sub_pc(map_session_data* sd, va_list ap)
  11735. {
  11736. int16 m=va_arg(ap,int);
  11737. int32 g_id=va_arg(ap,int);
  11738. int32 flag=va_arg(ap,int);
  11739. if(!sd || sd->bl.m != m)
  11740. return 0;
  11741. if(
  11742. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11743. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11744. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11745. )
  11746. pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  11747. return 1;
  11748. }
  11749. static int32 buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11750. {
  11751. struct mob_data *md=(struct mob_data *)bl;
  11752. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11753. status_kill(bl);
  11754. return 1;
  11755. }
  11756. /*
  11757. * Function to kick guild members out of a map and to their save points.
  11758. * m : mapid
  11759. * g_id : owner guild id
  11760. * flag & 1 : Warp out owners
  11761. * flag & 2 : Warp out outsiders
  11762. * flag & 4 : reserved for mobs
  11763. * */
  11764. BUILDIN_FUNC(maprespawnguildid)
  11765. {
  11766. const char *mapname=script_getstr(st,2);
  11767. int32 g_id=script_getnum(st,3);
  11768. int32 flag=script_getnum(st,4);
  11769. int16 m=map_mapname2mapid(mapname);
  11770. if(m == -1)
  11771. return SCRIPT_CMD_SUCCESS;
  11772. //Catch ALL players (in case some are 'between maps' on execution time)
  11773. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11774. if (flag&4) //Remove script mobs.
  11775. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11776. return SCRIPT_CMD_SUCCESS;
  11777. }
  11778. /// Siege commands
  11779. /**
  11780. * Start WoE:FE
  11781. * agitstart();
  11782. */
  11783. BUILDIN_FUNC(agitstart)
  11784. {
  11785. guild_agit_start();
  11786. return SCRIPT_CMD_SUCCESS;
  11787. }
  11788. /**
  11789. * End WoE:FE
  11790. * agitend();
  11791. */
  11792. BUILDIN_FUNC(agitend)
  11793. {
  11794. guild_agit_end();
  11795. return SCRIPT_CMD_SUCCESS;
  11796. }
  11797. /**
  11798. * Start WoE:SE
  11799. * agitstart2();
  11800. */
  11801. BUILDIN_FUNC(agitstart2)
  11802. {
  11803. guild_agit2_start();
  11804. return SCRIPT_CMD_SUCCESS;
  11805. }
  11806. /**
  11807. * End WoE:SE
  11808. * agitend();
  11809. */
  11810. BUILDIN_FUNC(agitend2)
  11811. {
  11812. guild_agit2_end();
  11813. return SCRIPT_CMD_SUCCESS;
  11814. }
  11815. /**
  11816. * Start WoE:TE
  11817. * agitstart3();
  11818. */
  11819. BUILDIN_FUNC(agitstart3)
  11820. {
  11821. guild_agit3_start();
  11822. return SCRIPT_CMD_SUCCESS;
  11823. }
  11824. /**
  11825. * End WoE:TE
  11826. * agitend3();
  11827. */
  11828. BUILDIN_FUNC(agitend3)
  11829. {
  11830. guild_agit3_end();
  11831. return SCRIPT_CMD_SUCCESS;
  11832. }
  11833. /**
  11834. * Returns whether WoE:FE is on or off.
  11835. * agitcheck();
  11836. */
  11837. BUILDIN_FUNC(agitcheck)
  11838. {
  11839. script_pushint(st, agit_flag);
  11840. return SCRIPT_CMD_SUCCESS;
  11841. }
  11842. /**
  11843. * Returns whether WoE:SE is on or off.
  11844. * agitcheck2();
  11845. */
  11846. BUILDIN_FUNC(agitcheck2)
  11847. {
  11848. script_pushint(st, agit2_flag);
  11849. return SCRIPT_CMD_SUCCESS;
  11850. }
  11851. /**
  11852. * Returns whether WoE:TE is on or off.
  11853. * agitcheck3();
  11854. */
  11855. BUILDIN_FUNC(agitcheck3)
  11856. {
  11857. script_pushint(st, agit3_flag);
  11858. return SCRIPT_CMD_SUCCESS;
  11859. }
  11860. /// Sets the guild_id of this npc.
  11861. ///
  11862. /// flagemblem <guild_id>;
  11863. BUILDIN_FUNC(flagemblem)
  11864. {
  11865. TBL_NPC* nd;
  11866. int32 g_id = script_getnum(st,2);
  11867. if(g_id < 0)
  11868. return SCRIPT_CMD_SUCCESS;
  11869. nd = (TBL_NPC*)map_id2nd(st->oid);
  11870. if( nd == nullptr ) {
  11871. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11872. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11873. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11874. } else {
  11875. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11876. nd->u.scr.guild_id = g_id;
  11877. clif_guild_emblem_area(&nd->bl);
  11878. /* guild flag caching */
  11879. if( g_id ) /* adding a id */
  11880. guild_flag_add(nd);
  11881. else if( changed ) /* removing a flag */
  11882. guild_flag_remove(nd);
  11883. }
  11884. return SCRIPT_CMD_SUCCESS;
  11885. }
  11886. BUILDIN_FUNC(getcastlename)
  11887. {
  11888. const char* mapname = mapindex_getmapname(script_getstr(st,2),nullptr);
  11889. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11890. const char* name = (gc) ? gc->castle_name : "";
  11891. script_pushstrcopy(st,name);
  11892. return SCRIPT_CMD_SUCCESS;
  11893. }
  11894. BUILDIN_FUNC(getcastledata)
  11895. {
  11896. const char *mapname = mapindex_getmapname(script_getstr(st,2),nullptr);
  11897. int32 index = script_getnum(st,3);
  11898. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11899. if (gc == nullptr) {
  11900. script_pushint(st,0);
  11901. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11902. return SCRIPT_CMD_FAILURE;
  11903. }
  11904. switch (index) {
  11905. case CD_GUILD_ID:
  11906. script_pushint(st,gc->guild_id); break;
  11907. case CD_CURRENT_ECONOMY:
  11908. script_pushint(st,gc->economy); break;
  11909. case CD_CURRENT_DEFENSE:
  11910. script_pushint(st,gc->defense); break;
  11911. case CD_INVESTED_ECONOMY:
  11912. script_pushint(st,gc->triggerE); break;
  11913. case CD_INVESTED_DEFENSE:
  11914. script_pushint(st,gc->triggerD); break;
  11915. case CD_NEXT_TIME:
  11916. script_pushint(st,gc->nextTime); break;
  11917. case CD_PAY_TIME:
  11918. script_pushint(st,gc->payTime); break;
  11919. case CD_CREATE_TIME:
  11920. script_pushint(st,gc->createTime); break;
  11921. case CD_ENABLED_KAFRA:
  11922. script_pushint(st,gc->visibleC); break;
  11923. default:
  11924. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11925. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11926. break;
  11927. }
  11928. script_pushint(st,0);
  11929. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11930. return SCRIPT_CMD_FAILURE;
  11931. }
  11932. return SCRIPT_CMD_SUCCESS;
  11933. }
  11934. BUILDIN_FUNC(setcastledata)
  11935. {
  11936. const char *mapname = mapindex_getmapname(script_getstr(st,2),nullptr);
  11937. int32 index = script_getnum(st,3);
  11938. int32 value = script_getnum(st,4);
  11939. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11940. if (gc == nullptr) {
  11941. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11942. return SCRIPT_CMD_FAILURE;
  11943. }
  11944. if (index <= CD_NONE || index >= CD_MAX) {
  11945. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11946. return SCRIPT_CMD_FAILURE;
  11947. }
  11948. guild_castledatasave(gc->castle_id, index, value);
  11949. return SCRIPT_CMD_SUCCESS;
  11950. }
  11951. /* =====================================================================
  11952. * ---------------------------------------------------------------------*/
  11953. BUILDIN_FUNC(requestguildinfo)
  11954. {
  11955. int32 guild_id=script_getnum(st,2);
  11956. const char *event=nullptr;
  11957. if( script_hasdata(st,3) ){
  11958. event=script_getstr(st,3);
  11959. check_event(st, event);
  11960. }
  11961. if(guild_id>0)
  11962. guild_npc_request_info(guild_id,event);
  11963. return SCRIPT_CMD_SUCCESS;
  11964. }
  11965. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11966. /// getequipcardcnt(<equipment slot>);
  11967. BUILDIN_FUNC(getequipcardcnt)
  11968. {
  11969. int32 i=-1,j,num;
  11970. TBL_PC *sd;
  11971. int32 count;
  11972. if( !script_rid2sd(sd) )
  11973. return SCRIPT_CMD_SUCCESS;
  11974. num=script_getnum(st,2);
  11975. if (equip_index_check(num))
  11976. i=pc_checkequip(sd,equip_bitmask[num]);
  11977. if (i < 0 || !sd->inventory_data[i]) {
  11978. script_pushint(st,0);
  11979. return SCRIPT_CMD_SUCCESS;
  11980. }
  11981. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11982. {
  11983. script_pushint(st,0);
  11984. return SCRIPT_CMD_SUCCESS;
  11985. }
  11986. count = 0;
  11987. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11988. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11989. count++;
  11990. script_pushint(st,count);
  11991. return SCRIPT_CMD_SUCCESS;
  11992. }
  11993. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11994. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11995. /// successremovecards <slot>;
  11996. BUILDIN_FUNC(successremovecards) {
  11997. int32 i=-1,c,cardflag=0;
  11998. map_session_data* sd;
  11999. int32 num;
  12000. if( !script_rid2sd(sd) )
  12001. return SCRIPT_CMD_SUCCESS;
  12002. num = script_getnum(st,2);
  12003. if (equip_index_check(num))
  12004. i=pc_checkequip(sd,equip_bitmask[num]);
  12005. if (i < 0 || !sd->inventory_data[i]) {
  12006. return SCRIPT_CMD_SUCCESS;
  12007. }
  12008. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12009. return SCRIPT_CMD_SUCCESS;
  12010. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  12011. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  12012. item item_tmp = {};
  12013. cardflag = 1;
  12014. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  12015. item_tmp.identify = 1;
  12016. e_additem_result flag = pc_additem( sd, &item_tmp, 1, LOG_TYPE_SCRIPT );
  12017. // get back the card in inventory
  12018. if( flag != ADDITEM_SUCCESS ){
  12019. clif_additem(sd,0,0,flag);
  12020. ShowError( "buildin_successremovecards: Failed to add the item to player.\n" );
  12021. return SCRIPT_CMD_FAILURE;
  12022. }
  12023. }
  12024. }
  12025. // if card was removed, replace item with no card
  12026. if(cardflag == 1) {
  12027. item item_tmp = {};
  12028. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  12029. item_tmp.identify = 1;
  12030. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  12031. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  12032. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  12033. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  12034. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  12035. for (int32 j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  12036. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  12037. for (int32 j = 0; j < MAX_ITEM_RDM_OPT; j++){
  12038. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  12039. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  12040. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  12041. }
  12042. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  12043. e_additem_result flag = pc_additem( sd, &item_tmp, 1, LOG_TYPE_SCRIPT );
  12044. // get back the card in inventory
  12045. if( flag != ADDITEM_SUCCESS ){
  12046. clif_additem(sd,0,0,flag);
  12047. ShowError( "buildin_successremovecards: Failed to add the item to player.\n" );
  12048. return SCRIPT_CMD_FAILURE;
  12049. }
  12050. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_SUCCESS );
  12051. }
  12052. return SCRIPT_CMD_SUCCESS;
  12053. }
  12054. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  12055. /// failedremovecards <slot>, <type>;
  12056. /// <type>=0 : will destroy both the item and the cards.
  12057. /// <type>=1 : will keep the item, but destroy the cards.
  12058. /// <type>=2 : will keep the cards, but destroy the item.
  12059. /// <type>=? : will just display the failure effect.
  12060. BUILDIN_FUNC(failedremovecards) {
  12061. int32 i=-1,c,cardflag=0;
  12062. map_session_data* sd;
  12063. int32 num;
  12064. int32 typefail;
  12065. if( !script_rid2sd(sd) )
  12066. return SCRIPT_CMD_SUCCESS;
  12067. num = script_getnum(st,2);
  12068. typefail = script_getnum(st,3);
  12069. if (equip_index_check(num))
  12070. i=pc_checkequip(sd,equip_bitmask[num]);
  12071. if (i < 0 || !sd->inventory_data[i])
  12072. return SCRIPT_CMD_SUCCESS;
  12073. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12074. return SCRIPT_CMD_SUCCESS;
  12075. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  12076. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  12077. cardflag = 1;
  12078. if(typefail == 2) {// add cards to inventory, clear
  12079. item item_tmp = {};
  12080. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  12081. item_tmp.identify = 1;
  12082. e_additem_result flag = pc_additem( sd, &item_tmp, 1, LOG_TYPE_SCRIPT );
  12083. if( flag != ADDITEM_SUCCESS ){
  12084. clif_additem(sd,0,0,flag);
  12085. ShowError( "failedremovecards: Failed to add the item to player.\n" );
  12086. return SCRIPT_CMD_FAILURE;
  12087. }
  12088. }
  12089. }
  12090. }
  12091. if(cardflag == 1) {
  12092. if(typefail == 0 || typefail == 2){ // destroy the item
  12093. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  12094. }else if(typefail == 1){ // destroy the card
  12095. item item_tmp = {};
  12096. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  12097. item_tmp.identify = 1;
  12098. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  12099. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  12100. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  12101. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  12102. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  12103. for (int32 j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  12104. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  12105. for (int32 j = 0; j < MAX_ITEM_RDM_OPT; j++){
  12106. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  12107. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  12108. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  12109. }
  12110. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  12111. e_additem_result flag = pc_additem( sd, &item_tmp, 1, LOG_TYPE_SCRIPT );
  12112. if( flag != ADDITEM_SUCCESS ){
  12113. clif_additem(sd,0,0,flag);
  12114. ShowError( "failedremovecards: Failed to add the item to player.\n" );
  12115. return SCRIPT_CMD_FAILURE;
  12116. }
  12117. }
  12118. clif_misceffect( sd->bl, NOTIFYEFFECT_REFINE_FAILURE );
  12119. }
  12120. return SCRIPT_CMD_SUCCESS;
  12121. }
  12122. /* ================================================================
  12123. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  12124. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  12125. * improved by [Lance]
  12126. * ================================================================*/
  12127. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  12128. {
  12129. int32 x,y,m,check_val=0,check_ID=0,i=0;
  12130. std::shared_ptr<MapGuild> g;
  12131. struct party_data *p = nullptr;
  12132. const char *str;
  12133. const char *mapname;
  12134. uint32 index;
  12135. mapname=script_getstr(st,2);
  12136. str=script_getstr(st,3);
  12137. x=script_getnum(st,4);
  12138. y=script_getnum(st,5);
  12139. if(script_hasdata(st,7)){
  12140. check_val=script_getnum(st,6);
  12141. check_ID=script_getnum(st,7);
  12142. }
  12143. if((m=map_mapname2mapid(mapname))< 0)
  12144. return SCRIPT_CMD_SUCCESS;
  12145. if(!(index=mapindex_name2id(str)))
  12146. return SCRIPT_CMD_SUCCESS;
  12147. switch(check_val){
  12148. case 1:
  12149. g = guild_search(check_ID);
  12150. if (g){
  12151. for( i=0; i < g->guild.max_member; i++)
  12152. {
  12153. if(g->guild.member[i].sd && g->guild.member[i].sd->bl.m==m){
  12154. pc_setpos(g->guild.member[i].sd,index,x,y,CLR_TELEPORT);
  12155. }
  12156. }
  12157. }
  12158. break;
  12159. case 2:
  12160. p = party_search(check_ID);
  12161. if(p){
  12162. for(i=0;i<MAX_PARTY; i++){
  12163. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  12164. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  12165. }
  12166. }
  12167. }
  12168. break;
  12169. default:
  12170. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  12171. break;
  12172. }
  12173. return SCRIPT_CMD_SUCCESS;
  12174. }
  12175. static int32 buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  12176. {
  12177. char *event=va_arg(ap,char *);
  12178. struct mob_data *md = ((struct mob_data *)bl);
  12179. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  12180. return 1;
  12181. return SCRIPT_CMD_SUCCESS;
  12182. }
  12183. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  12184. {
  12185. const char *mapname,*event;
  12186. int16 m;
  12187. mapname=script_getstr(st,2);
  12188. event=script_getstr(st,3);
  12189. if( strcmp(event, "all") == 0 )
  12190. event = nullptr;
  12191. else
  12192. check_event(st, event);
  12193. if( strcmp(mapname, "this") == 0 ) {
  12194. map_session_data *sd;
  12195. if( script_rid2sd(sd) )
  12196. m = sd->bl.m;
  12197. else {
  12198. script_pushint(st,-1);
  12199. return SCRIPT_CMD_SUCCESS;
  12200. }
  12201. }
  12202. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  12203. script_pushint(st,-1);
  12204. return SCRIPT_CMD_SUCCESS;
  12205. }
  12206. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  12207. return SCRIPT_CMD_SUCCESS;
  12208. }
  12209. BUILDIN_FUNC(marriage)
  12210. {
  12211. TBL_PC *sd;
  12212. TBL_PC *p_sd;
  12213. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  12214. script_pushint(st,0);
  12215. return SCRIPT_CMD_SUCCESS;
  12216. }
  12217. script_pushint(st,1);
  12218. return SCRIPT_CMD_SUCCESS;
  12219. }
  12220. BUILDIN_FUNC(wedding_effect)
  12221. {
  12222. TBL_PC *sd;
  12223. struct block_list *bl;
  12224. if(!script_rid2sd(sd)) {
  12225. bl=map_id2bl(st->oid);
  12226. } else
  12227. bl=&sd->bl;
  12228. clif_wedding_effect(bl);
  12229. return SCRIPT_CMD_SUCCESS;
  12230. }
  12231. /**
  12232. * divorce({<char_id>})
  12233. **/
  12234. BUILDIN_FUNC(divorce)
  12235. {
  12236. TBL_PC *sd;
  12237. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  12238. script_pushint(st,0);
  12239. return SCRIPT_CMD_FAILURE;
  12240. }
  12241. script_pushint(st,1);
  12242. return SCRIPT_CMD_SUCCESS;
  12243. }
  12244. /**
  12245. * ispartneron({<char_id>})
  12246. **/
  12247. BUILDIN_FUNC(ispartneron)
  12248. {
  12249. TBL_PC *sd;
  12250. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  12251. map_charid2sd(sd->status.partner_id) == nullptr)
  12252. {
  12253. script_pushint(st,0);
  12254. return SCRIPT_CMD_FAILURE;
  12255. }
  12256. script_pushint(st,1);
  12257. return SCRIPT_CMD_SUCCESS;
  12258. }
  12259. /**
  12260. * getpartnerid({<char_id>})
  12261. **/
  12262. BUILDIN_FUNC(getpartnerid)
  12263. {
  12264. TBL_PC *sd;
  12265. if (!script_charid2sd(2,sd)) {
  12266. script_pushint(st,0);
  12267. return SCRIPT_CMD_FAILURE;
  12268. }
  12269. script_pushint(st,sd->status.partner_id);
  12270. return SCRIPT_CMD_SUCCESS;
  12271. }
  12272. /**
  12273. * getchildid({<char_id>})
  12274. **/
  12275. BUILDIN_FUNC(getchildid)
  12276. {
  12277. TBL_PC *sd;
  12278. if (!script_charid2sd(2,sd)) {
  12279. script_pushint(st,0);
  12280. return SCRIPT_CMD_FAILURE;
  12281. }
  12282. script_pushint(st,sd->status.child);
  12283. return SCRIPT_CMD_SUCCESS;
  12284. }
  12285. /**
  12286. * getmotherid({<char_id>})
  12287. **/
  12288. BUILDIN_FUNC(getmotherid)
  12289. {
  12290. TBL_PC *sd;
  12291. if (!script_charid2sd(2,sd)) {
  12292. script_pushint(st,0);
  12293. return SCRIPT_CMD_FAILURE;
  12294. }
  12295. script_pushint(st,sd->status.mother);
  12296. return SCRIPT_CMD_SUCCESS;
  12297. }
  12298. /**
  12299. * getfatherid({<char_id>})
  12300. **/
  12301. BUILDIN_FUNC(getfatherid)
  12302. {
  12303. TBL_PC *sd;
  12304. if (!script_charid2sd(2,sd)) {
  12305. script_pushint(st,0);
  12306. return SCRIPT_CMD_FAILURE;
  12307. }
  12308. script_pushint(st,sd->status.father);
  12309. return SCRIPT_CMD_SUCCESS;
  12310. }
  12311. BUILDIN_FUNC(warppartner)
  12312. {
  12313. int32 x,y;
  12314. unsigned short mapindex;
  12315. const char *str;
  12316. TBL_PC *sd;
  12317. TBL_PC *p_sd;
  12318. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  12319. (p_sd=map_charid2sd(sd->status.partner_id)) == nullptr) {
  12320. script_pushint(st,0);
  12321. return SCRIPT_CMD_SUCCESS;
  12322. }
  12323. str=script_getstr(st,2);
  12324. x=script_getnum(st,3);
  12325. y=script_getnum(st,4);
  12326. mapindex = mapindex_name2id(str);
  12327. if (mapindex) {
  12328. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  12329. script_pushint(st,1);
  12330. } else
  12331. script_pushint(st,0);
  12332. return SCRIPT_CMD_SUCCESS;
  12333. }
  12334. /*================================================
  12335. * Script for Displaying MOB Information [Valaris]
  12336. *------------------------------------------------*/
  12337. BUILDIN_FUNC(strmobinfo)
  12338. {
  12339. int32 num=script_getnum(st,2);
  12340. int32 class_=script_getnum(st,3);
  12341. if(!mobdb_checkid(class_))
  12342. {
  12343. if (num < 3) //requested a string
  12344. script_pushconststr(st,"");
  12345. else
  12346. script_pushint(st,0);
  12347. return SCRIPT_CMD_SUCCESS;
  12348. }
  12349. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  12350. switch (num) {
  12351. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  12352. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  12353. case 3: script_pushint(st,mob->lv); break;
  12354. case 4: script_pushint(st,mob->status.max_hp); break;
  12355. case 5: script_pushint(st,mob->status.max_sp); break;
  12356. case 6: script_pushint(st,mob->base_exp); break;
  12357. case 7: script_pushint(st,mob->job_exp); break;
  12358. default:
  12359. script_pushint(st,0);
  12360. break;
  12361. }
  12362. return SCRIPT_CMD_SUCCESS;
  12363. }
  12364. /*==========================================
  12365. * Summon guardians [Valaris]
  12366. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  12367. *------------------------------------------*/
  12368. BUILDIN_FUNC(guardian)
  12369. {
  12370. int32 class_=0,x=0,y=0,guardian=0;
  12371. const char *str,*mapname,*evt="";
  12372. bool has_index = false;
  12373. mapname =script_getstr(st,2);
  12374. x =script_getnum(st,3);
  12375. y =script_getnum(st,4);
  12376. str =script_getstr(st,5);
  12377. class_=script_getnum(st,6);
  12378. if( script_hasdata(st,8) )
  12379. {// "<event label>",<guardian index>
  12380. evt=script_getstr(st,7);
  12381. guardian=script_getnum(st,8);
  12382. has_index = true;
  12383. } else if( script_hasdata(st,7) ){
  12384. if( script_isstring(st, 7) )
  12385. {// "<event label>"
  12386. evt=script_getstr(st,7);
  12387. } else
  12388. {// <guardian index>
  12389. guardian=script_getnum(st,7);
  12390. has_index = true;
  12391. }
  12392. }
  12393. check_event(st, evt);
  12394. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  12395. return SCRIPT_CMD_SUCCESS;
  12396. }
  12397. /*==========================================
  12398. * Invisible Walls [Zephyrus]
  12399. *------------------------------------------*/
  12400. BUILDIN_FUNC(setwall)
  12401. {
  12402. const char *mapname, *name;
  12403. int32 x, y, m, size, dir;
  12404. bool shootable;
  12405. mapname = script_getstr(st,2);
  12406. x = script_getnum(st,3);
  12407. y = script_getnum(st,4);
  12408. size = script_getnum(st,5);
  12409. dir = script_getnum(st,6);
  12410. shootable = script_getnum(st,7) != 0;
  12411. name = script_getstr(st,8);
  12412. if( (m = map_mapname2mapid(mapname)) < 0 )
  12413. return SCRIPT_CMD_SUCCESS; // Invalid Map
  12414. map_iwall_set(m, x, y, size, dir, shootable, name);
  12415. return SCRIPT_CMD_SUCCESS;
  12416. }
  12417. BUILDIN_FUNC(delwall)
  12418. {
  12419. const char *name = script_getstr(st,2);
  12420. if( !map_iwall_remove(name) ){
  12421. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  12422. return SCRIPT_CMD_FAILURE;
  12423. }
  12424. return SCRIPT_CMD_SUCCESS;
  12425. }
  12426. BUILDIN_FUNC(checkwall)
  12427. {
  12428. const char *wall_name = script_getstr(st, 2);
  12429. script_pushint(st, map_iwall_exist(wall_name));
  12430. return SCRIPT_CMD_SUCCESS;
  12431. }
  12432. /// Retrieves various information about the specified guardian.
  12433. ///
  12434. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  12435. /// type: 0 - whether it is deployed or not
  12436. /// 1 - maximum hp
  12437. /// 2 - current hp
  12438. ///
  12439. BUILDIN_FUNC(guardianinfo)
  12440. {
  12441. const char* mapname = mapindex_getmapname(script_getstr(st,2),nullptr);
  12442. int32 id = script_getnum(st,3);
  12443. int32 type = script_getnum(st,4);
  12444. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  12445. struct mob_data* gd;
  12446. if( gc == nullptr || id < 0 || id >= MAX_GUARDIANS )
  12447. {
  12448. script_pushint(st,-1);
  12449. return SCRIPT_CMD_SUCCESS;
  12450. }
  12451. if( type == 0 )
  12452. script_pushint(st, gc->guardian[id].visible);
  12453. else
  12454. if( !gc->guardian[id].visible )
  12455. script_pushint(st,-1);
  12456. else
  12457. if( (gd = map_id2md(gc->guardian[id].id)) == nullptr )
  12458. script_pushint(st,-1);
  12459. else
  12460. {
  12461. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  12462. else if( type == 2 ) script_pushint(st,gd->status.hp);
  12463. else
  12464. script_pushint(st,-1);
  12465. }
  12466. return SCRIPT_CMD_SUCCESS;
  12467. }
  12468. /*==========================================
  12469. * Get the item name by item_id or null
  12470. *------------------------------------------*/
  12471. BUILDIN_FUNC(getitemname)
  12472. {
  12473. std::shared_ptr<item_data> i_data;
  12474. if( script_isstring(st, 2) ){
  12475. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12476. }else{
  12477. i_data = item_db.find( script_getnum( st, 2 ) );
  12478. }
  12479. if( i_data == nullptr ){
  12480. script_pushconststr(st,"null");
  12481. return SCRIPT_CMD_SUCCESS;
  12482. }
  12483. char* item_name = (char *)aMalloc( ITEM_NAME_LENGTH * sizeof( char ) );
  12484. safestrncpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12485. script_pushstr(st,item_name);
  12486. return SCRIPT_CMD_SUCCESS;
  12487. }
  12488. /*==========================================
  12489. * Returns number of slots an item has. [Skotlex]
  12490. *------------------------------------------*/
  12491. BUILDIN_FUNC(getitemslots)
  12492. {
  12493. t_itemid item_id=script_getnum(st,2);
  12494. std::shared_ptr<item_data> i_data = item_db.find(item_id);
  12495. if (i_data != nullptr)
  12496. script_pushint(st,i_data->slots);
  12497. else
  12498. script_pushint(st,-1);
  12499. return SCRIPT_CMD_SUCCESS;
  12500. }
  12501. // TODO: add matk here if needed/once we get rid of RENEWAL
  12502. /*==========================================
  12503. * Returns some values of an item [Lupus]
  12504. * Price, Weight, etc...
  12505. *------------------------------------------*/
  12506. BUILDIN_FUNC(getiteminfo)
  12507. {
  12508. std::shared_ptr<item_data> i_data;
  12509. int32 type = script_getnum(st, 3);
  12510. if (script_isstring(st, 2))
  12511. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12512. else
  12513. i_data = item_db.find( script_getnum( st, 2 ) );
  12514. if (i_data == nullptr) {
  12515. if (type != ITEMINFO_AEGISNAME)
  12516. script_pushint(st, -1);
  12517. else
  12518. script_pushstrcopy(st, "");
  12519. return SCRIPT_CMD_SUCCESS;
  12520. }
  12521. switch( type ) {
  12522. case ITEMINFO_BUY: script_pushint(st, i_data->value_buy); break;
  12523. case ITEMINFO_SELL: script_pushint(st, i_data->value_sell); break;
  12524. case ITEMINFO_TYPE: script_pushint(st, i_data->type); break;
  12525. case ITEMINFO_MAXCHANCE: script_pushint(st, i_data->maxchance); break;
  12526. case ITEMINFO_GENDER: script_pushint(st, i_data->sex); break;
  12527. case ITEMINFO_LOCATIONS: script_pushint(st, i_data->equip); break;
  12528. case ITEMINFO_WEIGHT: script_pushint(st, i_data->weight); break;
  12529. case ITEMINFO_ATTACK: script_pushint(st, i_data->atk); break;
  12530. case ITEMINFO_DEFENSE: script_pushint(st, i_data->def); break;
  12531. case ITEMINFO_RANGE: script_pushint(st, i_data->range); break;
  12532. case ITEMINFO_SLOT: script_pushint(st, i_data->slots); break;
  12533. case ITEMINFO_VIEW:
  12534. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12535. script_pushint(st, i_data->subtype);
  12536. } else {
  12537. script_pushint(st, i_data->look);
  12538. }
  12539. break;
  12540. case ITEMINFO_EQUIPLEVELMIN: script_pushint(st, i_data->elv); break;
  12541. case ITEMINFO_WEAPONLEVEL:
  12542. if( i_data->type == IT_WEAPON ){
  12543. script_pushint( st, i_data->weapon_level );
  12544. }else{
  12545. script_pushint( st, 0 );
  12546. }
  12547. break;
  12548. case ITEMINFO_ARMORLEVEL:
  12549. if( i_data->type == IT_ARMOR ){
  12550. script_pushint( st, i_data->armor_level );
  12551. }else{
  12552. script_pushint( st, 0 );
  12553. }
  12554. break;
  12555. case ITEMINFO_ALIASNAME: script_pushint(st, i_data->view_id); break;
  12556. case ITEMINFO_EQUIPLEVELMAX: script_pushint(st, i_data->elvmax); break;
  12557. case ITEMINFO_MAGICATTACK: {
  12558. #ifdef RENEWAL
  12559. script_pushint(st, i_data->matk);
  12560. #else
  12561. script_pushint(st, 0);
  12562. #endif
  12563. break;
  12564. }
  12565. case ITEMINFO_ID: script_pushint(st, i_data->nameid); break;
  12566. case ITEMINFO_AEGISNAME: script_pushstrcopy(st, i_data->name.c_str()); break;
  12567. case ITEMINFO_SUBTYPE: script_pushint(st, i_data->subtype); break;
  12568. default:
  12569. script_pushint(st, -1);
  12570. break;
  12571. }
  12572. return SCRIPT_CMD_SUCCESS;
  12573. }
  12574. /*==========================================
  12575. * Set some values of an item [Lupus]
  12576. * Price, Weight, etc...
  12577. *------------------------------------------*/
  12578. BUILDIN_FUNC(setiteminfo)
  12579. {
  12580. std::shared_ptr<item_data> i_data;
  12581. if (script_isstring(st, 2))
  12582. i_data = item_db.search_aegisname( script_getstr( st, 2 ) );
  12583. else
  12584. i_data = item_db.find( script_getnum( st, 2 ) );
  12585. if (i_data == nullptr) {
  12586. script_pushint(st, -1);
  12587. return SCRIPT_CMD_SUCCESS;
  12588. }
  12589. int32 value = script_getnum(st,4);
  12590. switch( script_getnum(st, 3) ) {
  12591. case ITEMINFO_BUY: i_data->value_buy = static_cast<uint32>(value); break;
  12592. case ITEMINFO_SELL: i_data->value_sell = static_cast<uint32>(value); break;
  12593. case ITEMINFO_TYPE: i_data->type = static_cast<item_types>(value); break;
  12594. case ITEMINFO_MAXCHANCE: i_data->maxchance = static_cast<int>(value); break;
  12595. case ITEMINFO_GENDER: i_data->sex = static_cast<uint8>(value); break;
  12596. case ITEMINFO_LOCATIONS: i_data->equip = static_cast<uint32>(value); break;
  12597. case ITEMINFO_WEIGHT: i_data->weight = static_cast<uint32>(value); break;
  12598. case ITEMINFO_ATTACK: i_data->atk = static_cast<uint32>(value); break;
  12599. case ITEMINFO_DEFENSE: i_data->def = static_cast<uint32>(value); break;
  12600. case ITEMINFO_RANGE: i_data->range = static_cast<uint16>(value); break;
  12601. case ITEMINFO_SLOT: i_data->slots = static_cast<uint16>(value); break;
  12602. case ITEMINFO_VIEW:
  12603. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12604. i_data->subtype = static_cast<uint8>(value);
  12605. } else {
  12606. i_data->look = static_cast<uint32>(value);
  12607. }
  12608. break;
  12609. case ITEMINFO_EQUIPLEVELMIN: i_data->elv = static_cast<uint16>(value); break;
  12610. case ITEMINFO_WEAPONLEVEL:
  12611. if( i_data->type == IT_WEAPON ){
  12612. if( value > MAX_WEAPON_LEVEL ){
  12613. ShowError( "buildin_setiteminfo: weapon level %d is above maximum %d.\n", value, MAX_WEAPON_LEVEL );
  12614. script_pushint( st, -1 );
  12615. return SCRIPT_CMD_FAILURE;
  12616. }
  12617. i_data->weapon_level = static_cast<uint16>(value);
  12618. break;
  12619. }else{
  12620. ShowError( "buildin_setiteminfo: Can not set a weapon level for %d.\n", i_data->nameid );
  12621. script_pushint( st, -1 );
  12622. return SCRIPT_CMD_FAILURE;
  12623. }
  12624. case ITEMINFO_ARMORLEVEL:
  12625. if( i_data->type == IT_ARMOR ){
  12626. if( value > MAX_ARMOR_LEVEL ){
  12627. ShowError( "buildin_setiteminfo: armor level %d is above maximum %d.\n", value, MAX_ARMOR_LEVEL );
  12628. script_pushint( st, -1 );
  12629. return SCRIPT_CMD_FAILURE;
  12630. }
  12631. i_data->armor_level = static_cast<uint16>(value);
  12632. break;
  12633. }else{
  12634. ShowError( "buildin_setiteminfo: Can not set an armor level for %d.\n", i_data->nameid );
  12635. script_pushint( st, -1 );
  12636. return SCRIPT_CMD_FAILURE;
  12637. }
  12638. case ITEMINFO_ALIASNAME: i_data->view_id = static_cast<t_itemid>(value); break;
  12639. case ITEMINFO_EQUIPLEVELMAX: i_data->elvmax = static_cast<uint16>(value); break;
  12640. case ITEMINFO_MAGICATTACK: {
  12641. #ifdef RENEWAL
  12642. i_data->matk = static_cast<uint32>(value);
  12643. #else
  12644. value = 0;
  12645. #endif
  12646. break;
  12647. }
  12648. case ITEMINFO_SUBTYPE: i_data->subtype = static_cast<uint8>(value); break;
  12649. default:
  12650. script_pushint(st, -1);
  12651. break;
  12652. }
  12653. script_pushint(st, value);
  12654. return SCRIPT_CMD_SUCCESS;
  12655. }
  12656. /*==========================================
  12657. * Returns value from equipped item slot n [Lupus]
  12658. getequipcardid(num,slot)
  12659. where
  12660. num = eqip position slot
  12661. slot = 0,1,2,3 (Card Slot N)
  12662. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12663. it's useful when you want to check item cards or if it's signed
  12664. Useful for such quests as "Sign this refined item with players name" etc
  12665. Hat[0] +4 -> Player's Hat[0] +4
  12666. *------------------------------------------*/
  12667. BUILDIN_FUNC(getequipcardid)
  12668. {
  12669. int32 i=-1,num,slot;
  12670. TBL_PC *sd;
  12671. if( !script_rid2sd(sd) )
  12672. return SCRIPT_CMD_SUCCESS;
  12673. num=script_getnum(st,2);
  12674. slot=script_getnum(st,3);
  12675. if (equip_index_check(num))
  12676. i=pc_checkequip(sd,equip_bitmask[num]);
  12677. if(i >= 0 && slot>=0 && slot<4)
  12678. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12679. else
  12680. script_pushint(st,0);
  12681. return SCRIPT_CMD_SUCCESS;
  12682. }
  12683. /*==========================================
  12684. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12685. *------------------------------------------*/
  12686. BUILDIN_FUNC(petskillbonus)
  12687. {
  12688. struct pet_data *pd;
  12689. TBL_PC *sd;
  12690. if(!script_rid2sd(sd) || sd->pd == nullptr)
  12691. return SCRIPT_CMD_FAILURE;
  12692. pd = sd->pd;
  12693. if (pd->bonus)
  12694. { //Clear previous bonus
  12695. if (pd->bonus->timer != INVALID_TIMER)
  12696. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12697. } else //init
  12698. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12699. pd->bonus->type = script_getnum(st,2);
  12700. pd->bonus->val = script_getnum(st,3);
  12701. pd->bonus->duration = script_getnum(st,4);
  12702. pd->bonus->delay = script_getnum(st,5);
  12703. if (pd->state.skillbonus == 1)
  12704. pd->state.skillbonus = 0; // waiting state
  12705. // wait for timer to start
  12706. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12707. pd->bonus->timer = INVALID_TIMER;
  12708. else
  12709. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12710. return SCRIPT_CMD_SUCCESS;
  12711. }
  12712. /*==========================================
  12713. * pet looting [Valaris] //Rewritten by [Skotlex]
  12714. *------------------------------------------*/
  12715. BUILDIN_FUNC(petloot)
  12716. {
  12717. int32 max;
  12718. struct pet_data *pd;
  12719. TBL_PC *sd;
  12720. if(!script_rid2sd(sd) || sd->pd==nullptr)
  12721. return SCRIPT_CMD_SUCCESS;
  12722. max=script_getnum(st,2);
  12723. if(max < 1)
  12724. max = 1; //Let'em loot at least 1 item.
  12725. else if (max > MAX_PETLOOT_SIZE)
  12726. max = MAX_PETLOOT_SIZE;
  12727. pd = sd->pd;
  12728. if (pd->loot != nullptr)
  12729. { //Release whatever was there already and reallocate memory
  12730. pet_lootitem_drop( *pd, pd->master );
  12731. aFree(pd->loot->item);
  12732. }
  12733. else
  12734. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12735. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12736. pd->loot->max=max;
  12737. pd->loot->count = 0;
  12738. pd->loot->weight = 0;
  12739. return SCRIPT_CMD_SUCCESS;
  12740. }
  12741. /*==========================================
  12742. * Set arrays with info of all sd inventory :
  12743. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12744. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12745. * @inventorylist_card(0..3), @inventorylist_expire
  12746. * @inventorylist_count = scalar
  12747. *------------------------------------------*/
  12748. BUILDIN_FUNC(getinventorylist)
  12749. {
  12750. TBL_PC *sd;
  12751. char card_var[NAME_LENGTH], randopt_var[50];
  12752. int32 i,j=0,k;
  12753. if (!script_charid2sd(2,sd))
  12754. return SCRIPT_CMD_FAILURE;
  12755. for(i=0;i<MAX_INVENTORY;i++){
  12756. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12757. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12758. pc_setreg(sd,reference_uid(add_str("@inventorylist_idx"), j),i);
  12759. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12760. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12761. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12762. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12763. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12764. for (k = 0; k < MAX_SLOTS; k++)
  12765. {
  12766. sprintf(card_var, "@inventorylist_card%d",k+1);
  12767. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12768. }
  12769. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12770. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12771. pc_setreg(sd,reference_uid(add_str("@inventorylist_enchantgrade"), j),sd->inventory.u.items_inventory[i].enchantgrade);
  12772. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12773. {
  12774. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12775. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12776. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12777. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12778. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12779. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12780. }
  12781. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12782. pc_setreg(sd,reference_uid(add_str("@inventorylist_favorite"), j),sd->inventory.u.items_inventory[i].favorite);
  12783. j++;
  12784. }
  12785. }
  12786. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12787. return SCRIPT_CMD_SUCCESS;
  12788. }
  12789. /**
  12790. * getskilllist ({<char_id>});
  12791. **/
  12792. BUILDIN_FUNC(getskilllist)
  12793. {
  12794. TBL_PC *sd;
  12795. int32 i,j=0;
  12796. if (!script_charid2sd(2,sd))
  12797. return SCRIPT_CMD_FAILURE;
  12798. for(i=0;i<MAX_SKILL;i++){
  12799. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12800. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12801. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12802. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12803. j++;
  12804. }
  12805. }
  12806. pc_setreg(sd,add_str("@skilllist_count"),j);
  12807. return SCRIPT_CMD_SUCCESS;
  12808. }
  12809. /**
  12810. * clearitem ({<char_id>});
  12811. **/
  12812. BUILDIN_FUNC(clearitem)
  12813. {
  12814. TBL_PC *sd;
  12815. int32 i;
  12816. if (!script_charid2sd(2,sd))
  12817. return SCRIPT_CMD_FAILURE;
  12818. for (i=0; i<MAX_INVENTORY; i++) {
  12819. if (sd->inventory.u.items_inventory[i].amount) {
  12820. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12821. }
  12822. }
  12823. return SCRIPT_CMD_SUCCESS;
  12824. }
  12825. /**
  12826. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12827. * disguise <Monster ID>{,<char_id>};
  12828. **/
  12829. BUILDIN_FUNC(disguise)
  12830. {
  12831. int32 id;
  12832. TBL_PC* sd;
  12833. if (!script_charid2sd(3,sd))
  12834. return SCRIPT_CMD_FAILURE;
  12835. id = script_getnum(st,2);
  12836. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12837. pc_disguise(sd, id);
  12838. script_pushint(st,id);
  12839. } else
  12840. script_pushint(st,0);
  12841. return SCRIPT_CMD_SUCCESS;
  12842. }
  12843. /**
  12844. * Undisguise Player (returns 1 if success, 0 on fail)
  12845. * undisguise {<char_id>};
  12846. **/
  12847. BUILDIN_FUNC(undisguise)
  12848. {
  12849. TBL_PC* sd;
  12850. if (!script_charid2sd(2,sd))
  12851. return SCRIPT_CMD_FAILURE;
  12852. if (sd->disguise) {
  12853. pc_disguise(sd, 0);
  12854. script_pushint(st,0);
  12855. } else {
  12856. script_pushint(st,1);
  12857. }
  12858. return SCRIPT_CMD_SUCCESS;
  12859. }
  12860. /**
  12861. * Transform an NPC to another _class
  12862. *
  12863. * classchange(<view id>{,"<NPC name>","<flag>"});
  12864. * @param flag: Specify target
  12865. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12866. * BC_SELF - Sprite is sent only to player attached.
  12867. */
  12868. BUILDIN_FUNC(classchange)
  12869. {
  12870. int32 _class;
  12871. struct npc_data* nd = nullptr;
  12872. TBL_PC *sd = map_id2sd(st->rid);
  12873. send_target target = AREA;
  12874. _class = script_getnum(st,2);
  12875. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12876. nd = npc_name2id(script_getstr(st, 3));
  12877. else
  12878. nd = (struct npc_data *)map_id2bl(st->oid);
  12879. if (nd == nullptr)
  12880. return SCRIPT_CMD_FAILURE;
  12881. if (script_hasdata(st, 4)) {
  12882. switch(script_getnum(st, 4)) {
  12883. case BC_SELF: target = SELF; break;
  12884. case BC_AREA:
  12885. default: target = AREA; break;
  12886. }
  12887. }
  12888. if (target != SELF)
  12889. clif_class_change( nd->bl, _class );
  12890. else if (sd == nullptr)
  12891. return SCRIPT_CMD_FAILURE;
  12892. else
  12893. clif_class_change( nd->bl, _class, target, sd );
  12894. return SCRIPT_CMD_SUCCESS;
  12895. }
  12896. /*==========================================
  12897. * Display an effect
  12898. *------------------------------------------*/
  12899. BUILDIN_FUNC(misceffect)
  12900. {
  12901. int32 type;
  12902. type=script_getnum(st,2);
  12903. if( type <= EF_NONE || type >= EF_MAX ){
  12904. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12905. return SCRIPT_CMD_FAILURE;
  12906. }
  12907. if(st->oid && st->oid != fake_nd->bl.id) {
  12908. struct block_list *bl = map_id2bl(st->oid);
  12909. if (bl)
  12910. clif_specialeffect(bl,type,AREA);
  12911. } else{
  12912. TBL_PC *sd;
  12913. if(script_rid2sd(sd))
  12914. clif_specialeffect(&sd->bl,type,AREA);
  12915. }
  12916. return SCRIPT_CMD_SUCCESS;
  12917. }
  12918. /*==========================================
  12919. * Play a BGM on a single client [Rikter/Yommy]
  12920. *------------------------------------------*/
  12921. BUILDIN_FUNC(playBGM)
  12922. {
  12923. map_session_data* sd;
  12924. if( script_rid2sd(sd) ) {
  12925. clif_playBGM( *sd, script_getstr( st, 2 ) );
  12926. }
  12927. return SCRIPT_CMD_SUCCESS;
  12928. }
  12929. static int32 playBGM_sub(struct block_list* bl,va_list ap)
  12930. {
  12931. const char* name = va_arg(ap,const char*);
  12932. clif_playBGM( *BL_CAST( BL_PC, bl ), name );
  12933. return 0;
  12934. }
  12935. static int32 playBGM_foreachpc_sub(map_session_data* sd, va_list args)
  12936. {
  12937. const char* name = va_arg(args, const char*);
  12938. clif_playBGM( *sd, name );
  12939. return 0;
  12940. }
  12941. /*==========================================
  12942. * Play a BGM on multiple client [Rikter/Yommy]
  12943. *------------------------------------------*/
  12944. BUILDIN_FUNC(playBGMall)
  12945. {
  12946. const char* name;
  12947. name = script_getstr(st,2);
  12948. if( script_hasdata(st,7) ) {// specified part of map
  12949. const char* mapname = script_getstr(st,3);
  12950. int32 x0 = script_getnum(st,4);
  12951. int32 y0 = script_getnum(st,5);
  12952. int32 x1 = script_getnum(st,6);
  12953. int32 y1 = script_getnum(st,7);
  12954. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12955. }
  12956. else if( script_hasdata(st,3) ) {// entire map
  12957. const char* mapname = script_getstr(st,3);
  12958. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12959. }
  12960. else {// entire server
  12961. map_foreachpc(&playBGM_foreachpc_sub, name);
  12962. }
  12963. return SCRIPT_CMD_SUCCESS;
  12964. }
  12965. /*==========================================
  12966. * Play a .wav sound for sd
  12967. *------------------------------------------*/
  12968. BUILDIN_FUNC(soundeffect)
  12969. {
  12970. map_session_data* sd;
  12971. if(script_rid2sd(sd)){
  12972. const char* name = script_getstr(st,2);
  12973. int32 type = script_getnum(st,3);
  12974. clif_soundeffect( sd->bl, name, type, SELF );
  12975. }
  12976. return SCRIPT_CMD_SUCCESS;
  12977. }
  12978. int32 soundeffect_sub(struct block_list* bl,va_list ap)
  12979. {
  12980. char* name = va_arg(ap,char*);
  12981. int32 type = va_arg(ap,int);
  12982. clif_soundeffect( *bl, name, type, SELF );
  12983. return 0;
  12984. }
  12985. /*==========================================
  12986. * Play a sound effect (.wav) on multiple clients
  12987. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12988. *------------------------------------------*/
  12989. BUILDIN_FUNC(soundeffectall)
  12990. {
  12991. struct block_list* bl;
  12992. map_session_data* sd;
  12993. const char* name;
  12994. int32 type;
  12995. if( st->rid && script_rid2sd(sd) )
  12996. bl = &sd->bl;
  12997. else
  12998. bl = map_id2bl(st->oid);
  12999. if (!bl)
  13000. return SCRIPT_CMD_SUCCESS;
  13001. name = script_getstr(st,2);
  13002. type = script_getnum(st,3);
  13003. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  13004. if(!script_hasdata(st,4))
  13005. { // area around
  13006. clif_soundeffect( *bl, name, type, AREA );
  13007. }
  13008. else
  13009. if(!script_hasdata(st,5))
  13010. { // entire map
  13011. const char* mapname = script_getstr(st,4);
  13012. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  13013. }
  13014. else
  13015. if(script_hasdata(st,8))
  13016. { // specified part of map
  13017. const char* mapname = script_getstr(st,4);
  13018. int32 x0 = script_getnum(st,5);
  13019. int32 y0 = script_getnum(st,6);
  13020. int32 x1 = script_getnum(st,7);
  13021. int32 y1 = script_getnum(st,8);
  13022. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  13023. }
  13024. else
  13025. {
  13026. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  13027. }
  13028. return SCRIPT_CMD_SUCCESS;
  13029. }
  13030. /*==========================================
  13031. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  13032. *------------------------------------------*/
  13033. BUILDIN_FUNC(petrecovery)
  13034. {
  13035. struct pet_data *pd;
  13036. TBL_PC *sd;
  13037. int32 sc;
  13038. if(!script_rid2sd(sd) || sd->pd == nullptr)
  13039. return SCRIPT_CMD_FAILURE;
  13040. sc = script_getnum(st,2);
  13041. if (sc <= SC_NONE || sc >= SC_MAX) {
  13042. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  13043. return SCRIPT_CMD_FAILURE;
  13044. }
  13045. pd = sd->pd;
  13046. if (pd->recovery)
  13047. { //Halt previous bonus
  13048. if (pd->recovery->timer != INVALID_TIMER)
  13049. delete_timer(pd->recovery->timer, pet_recovery_timer);
  13050. } else //Init
  13051. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  13052. pd->recovery->type = (sc_type)sc;
  13053. pd->recovery->delay = script_getnum(st,3);
  13054. pd->recovery->timer = INVALID_TIMER;
  13055. return SCRIPT_CMD_SUCCESS;
  13056. }
  13057. /*==========================================
  13058. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  13059. *------------------------------------------*/
  13060. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  13061. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  13062. BUILDIN_FUNC(petskillattack)
  13063. {
  13064. struct pet_data *pd;
  13065. TBL_PC *sd;
  13066. int32 id = 0;
  13067. if(!script_rid2sd(sd) || sd->pd == nullptr)
  13068. return SCRIPT_CMD_FAILURE;
  13069. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13070. if (!id) {
  13071. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  13072. return SCRIPT_CMD_FAILURE;
  13073. }
  13074. pd = sd->pd;
  13075. if (pd->a_skill == nullptr)
  13076. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  13077. pd->a_skill->id = id;
  13078. pd->a_skill->damage = 0;
  13079. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  13080. pd->a_skill->div_ = 0;
  13081. pd->a_skill->rate = script_getnum(st,4);
  13082. pd->a_skill->bonusrate = script_getnum(st,5);
  13083. return SCRIPT_CMD_SUCCESS;
  13084. }
  13085. /*==========================================
  13086. * pet attack skills [Valaris]
  13087. *------------------------------------------*/
  13088. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  13089. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  13090. BUILDIN_FUNC(petskillattack2)
  13091. {
  13092. struct pet_data *pd;
  13093. TBL_PC *sd;
  13094. int32 id = 0;
  13095. if(!script_rid2sd(sd) || sd->pd == nullptr)
  13096. return SCRIPT_CMD_FAILURE;
  13097. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13098. if (!id) {
  13099. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  13100. return SCRIPT_CMD_FAILURE;
  13101. }
  13102. pd = sd->pd;
  13103. if (pd->a_skill == nullptr)
  13104. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  13105. pd->a_skill->id = id;
  13106. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  13107. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  13108. pd->a_skill->div_ = script_getnum(st,4);
  13109. pd->a_skill->rate = script_getnum(st,5);
  13110. pd->a_skill->bonusrate = script_getnum(st,6);
  13111. return SCRIPT_CMD_SUCCESS;
  13112. }
  13113. /*==========================================
  13114. * pet support skills [Skotlex]
  13115. *------------------------------------------*/
  13116. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  13117. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  13118. BUILDIN_FUNC(petskillsupport)
  13119. {
  13120. struct pet_data *pd;
  13121. TBL_PC *sd;
  13122. int32 id = 0;
  13123. if(!script_rid2sd(sd) || sd->pd == nullptr)
  13124. return SCRIPT_CMD_FAILURE;
  13125. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13126. if (!id) {
  13127. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  13128. return SCRIPT_CMD_FAILURE;
  13129. }
  13130. pd = sd->pd;
  13131. if (pd->s_skill)
  13132. { //Clear previous skill
  13133. if (pd->s_skill->timer != INVALID_TIMER)
  13134. {
  13135. if (pd->s_skill->id)
  13136. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  13137. else
  13138. delete_timer(pd->s_skill->timer, pet_heal_timer);
  13139. }
  13140. } else //init memory
  13141. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  13142. pd->s_skill->id = id;
  13143. pd->s_skill->lv = script_getnum(st,3);
  13144. pd->s_skill->delay = script_getnum(st,4);
  13145. pd->s_skill->hp = script_getnum(st,5);
  13146. pd->s_skill->sp = script_getnum(st,6);
  13147. //Use delay as initial offset to avoid skill/heal exploits
  13148. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  13149. pd->s_skill->timer = INVALID_TIMER;
  13150. else
  13151. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  13152. return SCRIPT_CMD_SUCCESS;
  13153. }
  13154. static inline void script_skill_effect( block_list& bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y ) {
  13155. switch (skill_get_casttype(skill_id)) {
  13156. case CAST_GROUND:
  13157. clif_skill_poseffect(&bl, skill_id, skill_lv, x, y, gettick());
  13158. break;
  13159. case CAST_NODAMAGE:
  13160. clif_skill_nodamage(&bl, bl, skill_id, skill_lv);
  13161. break;
  13162. case CAST_DAMAGE:
  13163. clif_skill_damage(&bl, &bl, gettick(), status_get_amotion(&bl), status_get_dmotion(&bl), 0, 1, skill_id, skill_lv, skill_get_hit(skill_id));
  13164. break;
  13165. }
  13166. }
  13167. /*==========================================
  13168. * Scripted skill effects [Celest]
  13169. *------------------------------------------*/
  13170. /// skilleffect <skill id>,<level>{,<game id>}
  13171. /// skilleffect "<skill name>",<level>{,<game id>}
  13172. BUILDIN_FUNC(skilleffect)
  13173. {
  13174. block_list* bl;
  13175. map_session_data* sd;
  13176. if (script_hasdata(st, 4)) {
  13177. if (!script_rid2bl(4, bl))
  13178. return SCRIPT_CMD_SUCCESS;
  13179. }
  13180. else {
  13181. // target is the character attached (default)
  13182. if (!script_rid2sd(sd))
  13183. return SCRIPT_CMD_SUCCESS;
  13184. bl = &sd->bl;
  13185. }
  13186. uint16 skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2) );
  13187. if (skill_db.find(skill_id) == nullptr) {
  13188. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  13189. return SCRIPT_CMD_FAILURE;
  13190. }
  13191. uint16 skill_lv = script_getnum(st, 3);
  13192. if (bl->type == BL_PC) {
  13193. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  13194. * which leaves the server thinking it still is sitting. */
  13195. sd = reinterpret_cast<map_session_data*>( bl );
  13196. if (pc_issit(sd) && pc_setstand(sd, false)) {
  13197. skill_sit(sd, 0);
  13198. clif_standing(sd->bl);
  13199. }
  13200. }
  13201. script_skill_effect( *bl, skill_id, skill_lv, bl->x, bl->y );
  13202. return SCRIPT_CMD_SUCCESS;
  13203. }
  13204. /*==========================================
  13205. * NPC skill effects [Valaris]
  13206. *------------------------------------------*/
  13207. /// npcskilleffect <skill id>,<level>,<x>,<y>
  13208. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  13209. BUILDIN_FUNC(npcskilleffect)
  13210. {
  13211. struct block_list *bl= map_id2bl(st->oid);
  13212. if (bl == nullptr) {
  13213. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  13214. return SCRIPT_CMD_FAILURE;
  13215. }
  13216. uint16 skill_id, skill_lv;
  13217. int16 x, y;
  13218. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  13219. skill_lv=script_getnum(st,3);
  13220. x=script_getnum(st,4);
  13221. y=script_getnum(st,5);
  13222. if (skill_db.find(skill_id) == nullptr) {
  13223. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  13224. return SCRIPT_CMD_FAILURE;
  13225. }
  13226. script_skill_effect( *bl, skill_id, skill_lv, bl->x, bl->y );
  13227. return SCRIPT_CMD_SUCCESS;
  13228. }
  13229. /*==========================================
  13230. * Special effects [Valaris]
  13231. *------------------------------------------*/
  13232. BUILDIN_FUNC(specialeffect)
  13233. {
  13234. struct block_list *bl=map_id2bl(st->oid);
  13235. int32 type = script_getnum(st,2);
  13236. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  13237. if(bl==nullptr)
  13238. return SCRIPT_CMD_SUCCESS;
  13239. if( type <= EF_NONE || type >= EF_MAX ){
  13240. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  13241. return SCRIPT_CMD_FAILURE;
  13242. }
  13243. if( script_hasdata(st,4) )
  13244. {
  13245. const char* name = script_getstr( st, 4 );
  13246. npc_data* nd = npc_name2id( name );
  13247. if(nd)
  13248. bl = &nd->bl;
  13249. else{
  13250. ShowError( "buildin_specialeffect: Unknown NPC \"%s\"\n", name );
  13251. return SCRIPT_CMD_FAILURE;
  13252. }
  13253. }
  13254. if (target == SELF) {
  13255. map_session_data* sd;
  13256. if (!script_rid2sd(sd)){
  13257. return SCRIPT_CMD_FAILURE;
  13258. }
  13259. clif_specialeffect_single(bl,type,sd->fd);
  13260. } else {
  13261. clif_specialeffect(bl, type, target);
  13262. }
  13263. return SCRIPT_CMD_SUCCESS;
  13264. }
  13265. BUILDIN_FUNC(specialeffect2)
  13266. {
  13267. TBL_PC *sd;
  13268. if( script_nick2sd(4,sd) ){
  13269. int32 type = script_getnum(st,2);
  13270. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  13271. if( type <= EF_NONE || type >= EF_MAX ){
  13272. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  13273. return SCRIPT_CMD_FAILURE;
  13274. }
  13275. clif_specialeffect(&sd->bl, type, target);
  13276. }
  13277. return SCRIPT_CMD_SUCCESS;
  13278. }
  13279. BUILDIN_FUNC(removespecialeffect)
  13280. {
  13281. const char* command = script_getfuncname(st);
  13282. #if PACKETVER < 20181002
  13283. ShowError("buildin_%s: This command is not supported for PACKETVER older than 2018-10-02.\n", command);
  13284. return SCRIPT_CMD_FAILURE;
  13285. #endif
  13286. int32 effect = script_getnum(st, 2);
  13287. if (effect <= EF_NONE || effect >= EF_MAX) {
  13288. ShowError("buildin_%s: unsupported effect id %d.\n", command, effect);
  13289. return SCRIPT_CMD_FAILURE;
  13290. }
  13291. send_target e_target = script_hasdata(st, 3) ? static_cast<send_target>(script_getnum(st, 3)) : AREA;
  13292. struct block_list *bl_src;
  13293. struct block_list *bl_target;
  13294. map_session_data *sd;
  13295. if( strcmp(command, "removespecialeffect") == 0 ) {
  13296. if (!script_hasdata(st, 4)) {
  13297. bl_src = map_id2bl(st->oid);
  13298. if (bl_src == nullptr) {
  13299. ShowError("buildin_%s: npc not attached.\n", command);
  13300. return SCRIPT_CMD_FAILURE;
  13301. }
  13302. }
  13303. else {
  13304. struct npc_data *nd = npc_name2id(script_getstr(st, 4));
  13305. if (nd == nullptr) {
  13306. ShowError("buildin_%s: unknown npc %s.\n", command, script_getstr(st, 4));
  13307. return SCRIPT_CMD_FAILURE;
  13308. }
  13309. bl_src = &nd->bl;
  13310. }
  13311. if (e_target != SELF)
  13312. bl_target = bl_src;
  13313. else {
  13314. if (!script_rid2sd(sd))
  13315. return SCRIPT_CMD_FAILURE;
  13316. bl_target = &sd->bl;
  13317. }
  13318. }else{
  13319. if (!script_nick2sd(4, sd))
  13320. return SCRIPT_CMD_FAILURE;
  13321. bl_src = bl_target = &sd->bl;
  13322. }
  13323. clif_specialeffect_remove(bl_src, effect, e_target, bl_target);
  13324. return SCRIPT_CMD_SUCCESS;
  13325. }
  13326. /**
  13327. * nude({<char_id>});
  13328. * @author [Valaris]
  13329. **/
  13330. BUILDIN_FUNC(nude)
  13331. {
  13332. TBL_PC *sd;
  13333. int32 i, calcflag = 0;
  13334. if (!script_charid2sd(2,sd))
  13335. return SCRIPT_CMD_FAILURE;
  13336. for( i = 0 ; i < EQI_MAX; i++ ) {
  13337. if( sd->equip_index[ i ] >= 0 ) {
  13338. if( !calcflag )
  13339. calcflag = 1;
  13340. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  13341. }
  13342. }
  13343. if( calcflag )
  13344. status_calc_pc(sd,SCO_NONE);
  13345. return SCRIPT_CMD_SUCCESS;
  13346. }
  13347. int32 atcommand_sub(struct script_state* st,int32 type) {
  13348. TBL_PC *sd, dummy_sd;
  13349. int32 fd;
  13350. const char *cmd;
  13351. cmd = script_getstr(st,2);
  13352. if (st->rid) {
  13353. if( !script_rid2sd(sd) )
  13354. return SCRIPT_CMD_SUCCESS;
  13355. fd = sd->fd;
  13356. } else { //Use a dummy character.
  13357. sd = &dummy_sd;
  13358. fd = 0;
  13359. memset(&dummy_sd, 0, sizeof(TBL_PC));
  13360. if (st->oid) {
  13361. struct block_list* bl = map_id2bl(st->oid);
  13362. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  13363. if (bl->type == BL_NPC)
  13364. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  13365. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : 0;
  13366. }
  13367. // Init Group ID, Level, & permissions
  13368. sd->group_id = 99;
  13369. pc_group_pc_load( sd );
  13370. }
  13371. if (!is_atcommand(fd, sd, cmd, type)) {
  13372. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  13373. script_reportsrc(st);
  13374. return SCRIPT_CMD_FAILURE;
  13375. }
  13376. return SCRIPT_CMD_SUCCESS;
  13377. }
  13378. /*==========================================
  13379. * gmcommand [MouseJstr]
  13380. *------------------------------------------*/
  13381. BUILDIN_FUNC(atcommand) {
  13382. return atcommand_sub(st,0);
  13383. }
  13384. /** Displays a message for the player only (like system messages like "you got an apple" )
  13385. * dispbottom("<message>"{,<color>{,<char_id>}})
  13386. * @param message
  13387. * @param color Hex color default (Green)
  13388. */
  13389. BUILDIN_FUNC(dispbottom)
  13390. {
  13391. TBL_PC *sd;
  13392. int32 color = 0;
  13393. const char *message;
  13394. if (!script_charid2sd(4,sd))
  13395. return SCRIPT_CMD_FAILURE;
  13396. message = script_getstr(st,2);
  13397. if (script_hasdata(st,3))
  13398. color = script_getnum(st,3); // <color>
  13399. if(sd) {
  13400. if (script_hasdata(st,3))
  13401. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  13402. else
  13403. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  13404. }
  13405. return SCRIPT_CMD_SUCCESS;
  13406. }
  13407. /*===================================
  13408. * Heal portion of recovery command
  13409. *-----------------------------------*/
  13410. int32 recovery_sub(map_session_data* sd, int32 revive)
  13411. {
  13412. if(revive&(1|4) && pc_isdead(sd)) {
  13413. status_revive(&sd->bl, 100, 100);
  13414. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  13415. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  13416. } else if(revive&(1|2) && !pc_isdead(sd)) {
  13417. status_percent_heal(&sd->bl, 100, 100);
  13418. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  13419. }
  13420. return SCRIPT_CMD_SUCCESS;
  13421. }
  13422. /*=========================================================================
  13423. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  13424. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  13425. * <type> determines <option>:
  13426. * 0 : char_id
  13427. * 1 : party_id
  13428. * 2 : guild_id
  13429. * 3 : map_name
  13430. * 4 : all characters
  13431. * <revive_flag>:
  13432. * 1 : Revive and heal all players (default)
  13433. * 2 : Heal living players only
  13434. * 4 : Revive dead players only
  13435. * <map name>:
  13436. * for types 1-2 : map_name (null = all maps)
  13437. *-------------------------------------------------------------------------*/
  13438. BUILDIN_FUNC(recovery)
  13439. {
  13440. TBL_PC *sd;
  13441. int32 map_idx = 0, type = 0, revive = 1;
  13442. type = script_getnum(st,2);
  13443. if(script_hasdata(st,4))
  13444. revive = script_getnum(st,4);
  13445. switch(type) {
  13446. case 0:
  13447. if(!script_charid2sd(3,sd))
  13448. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  13449. recovery_sub(sd, revive);
  13450. break;
  13451. case 1:
  13452. {
  13453. struct party_data* p;
  13454. //When no party given, we use invoker party
  13455. int32 p_id = 0, i;
  13456. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13457. map_idx = map_mapname2mapid(script_getstr(st,5));
  13458. if(map_idx < 1) { //But we'll check anyways
  13459. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13460. return SCRIPT_CMD_FAILURE;
  13461. }
  13462. }
  13463. if(script_hasdata(st,3))
  13464. p_id = script_getnum(st,3);
  13465. else if(script_rid2sd(sd))
  13466. p_id = sd->status.party_id;
  13467. p = party_search(p_id);
  13468. if(p == nullptr)
  13469. return SCRIPT_CMD_SUCCESS;
  13470. for (i = 0; i < MAX_PARTY; i++) {
  13471. map_session_data* pl_sd;
  13472. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  13473. || (map_idx && pl_sd->bl.m != map_idx))
  13474. continue;
  13475. recovery_sub(pl_sd, revive);
  13476. }
  13477. break;
  13478. }
  13479. case 2:
  13480. {
  13481. //When no guild given, we use invoker guild
  13482. int32 g_id = 0, i;
  13483. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13484. map_idx = map_mapname2mapid(script_getstr(st,5));
  13485. if(map_idx < 1) { //But we'll check anyways
  13486. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13487. return SCRIPT_CMD_FAILURE;
  13488. }
  13489. }
  13490. if(script_hasdata(st,3))
  13491. g_id = script_getnum(st,3);
  13492. else if(script_rid2sd(sd))
  13493. g_id = sd->status.guild_id;
  13494. auto g = guild_search(g_id);
  13495. if(g == nullptr)
  13496. return SCRIPT_CMD_SUCCESS;
  13497. for (i = 0; i < MAX_GUILD; i++) {
  13498. map_session_data* pl_sd;
  13499. if((!(pl_sd = g->guild.member[i].sd) || pl_sd->status.guild_id != g_id)
  13500. || (map_idx && pl_sd->bl.m != map_idx))
  13501. continue;
  13502. recovery_sub(pl_sd, revive);
  13503. }
  13504. break;
  13505. }
  13506. case 3:
  13507. if(script_hasdata(st,3))
  13508. map_idx = map_mapname2mapid(script_getstr(st,3));
  13509. else if(script_rid2sd(sd))
  13510. map_idx = sd->bl.m;
  13511. if(map_idx < 1)
  13512. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  13513. [[fallthrough]];
  13514. case 4:
  13515. {
  13516. struct s_mapiterator *iter;
  13517. if(script_hasdata(st,3) && script_isint(st, 3))
  13518. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  13519. iter = mapit_getallusers();
  13520. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  13521. if(type == 3 && sd->bl.m != map_idx)
  13522. continue;
  13523. recovery_sub(sd, revive);
  13524. }
  13525. mapit_free(iter);
  13526. break;
  13527. }
  13528. default:
  13529. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  13530. script_pushint(st,-1);
  13531. return SCRIPT_CMD_FAILURE;
  13532. }
  13533. script_pushint(st,1); //Successfully executed without errors
  13534. return SCRIPT_CMD_SUCCESS;
  13535. }
  13536. /*==========================================
  13537. * Get your pet info: getpetinfo <type>{,<char_id>}
  13538. * n -> 0:pet_id 1:pet_class 2:pet_name
  13539. * 3:friendly 4:hungry, 5: rename flag.6:level,
  13540. * 7:game id
  13541. *------------------------------------------*/
  13542. BUILDIN_FUNC(getpetinfo)
  13543. {
  13544. TBL_PC *sd;
  13545. TBL_PET *pd;
  13546. int32 type = script_getnum(st,2);
  13547. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  13548. if (type == 2)
  13549. script_pushconststr(st,"null");
  13550. else
  13551. script_pushint(st,0);
  13552. return SCRIPT_CMD_SUCCESS;
  13553. }
  13554. switch(type){
  13555. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13556. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13557. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13558. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13559. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13560. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13561. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13562. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13563. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13564. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13565. default:
  13566. script_pushint(st,0);
  13567. break;
  13568. }
  13569. return SCRIPT_CMD_SUCCESS;
  13570. }
  13571. /*==========================================
  13572. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13573. * type -> 0:hom_id 1:class 2:name
  13574. * 3:friendly 4:hungry, 5: rename flag.
  13575. * 6: level, 7: game id
  13576. *------------------------------------------*/
  13577. BUILDIN_FUNC(gethominfo)
  13578. {
  13579. TBL_PC *sd;
  13580. TBL_HOM *hd;
  13581. int32 type=script_getnum(st,2);
  13582. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13583. if (type == 2)
  13584. script_pushconststr(st,"null");
  13585. else
  13586. script_pushint(st,0);
  13587. return SCRIPT_CMD_SUCCESS;
  13588. }
  13589. switch(type){
  13590. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13591. case 1: script_pushint(st,hd->homunculus.class_); break;
  13592. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13593. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13594. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13595. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13596. case 6: script_pushint(st,hd->homunculus.level); break;
  13597. case 7: script_pushint(st,hd->bl.id); break;
  13598. default:
  13599. script_pushint(st,0);
  13600. break;
  13601. }
  13602. return SCRIPT_CMD_SUCCESS;
  13603. }
  13604. BUILDIN_FUNC(addhomintimacy)
  13605. {
  13606. map_session_data *sd;
  13607. homun_data *hd;
  13608. if (!script_charid2sd(3, sd) || !(hd = sd->hd))
  13609. return SCRIPT_CMD_SUCCESS;
  13610. int32 value = script_getnum(st, 2);
  13611. if (value == 0) // Nothing to change
  13612. return SCRIPT_CMD_SUCCESS;
  13613. if (value > 0)
  13614. hom_increase_intimacy(hd, (uint32)value);
  13615. else
  13616. hom_decrease_intimacy(hd, (uint32)abs(value));
  13617. clif_send_homdata( *hd, SP_INTIMATE );
  13618. return SCRIPT_CMD_SUCCESS;
  13619. }
  13620. /// Retrieves information about character's mercenary
  13621. /// getmercinfo <type>[,<char id>];
  13622. BUILDIN_FUNC(getmercinfo)
  13623. {
  13624. int32 type;
  13625. map_session_data* sd;
  13626. s_mercenary_data* md;
  13627. if( !script_charid2sd(3,sd) ){
  13628. script_pushnil(st);
  13629. return SCRIPT_CMD_FAILURE;
  13630. }
  13631. type = script_getnum(st,2);
  13632. md = sd->md;
  13633. if( md == nullptr ){
  13634. if( type == 2 )
  13635. script_pushconststr(st,"");
  13636. else
  13637. script_pushint(st,0);
  13638. return SCRIPT_CMD_SUCCESS;
  13639. }
  13640. switch( type )
  13641. {
  13642. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13643. case 1: script_pushint(st,md->mercenary.class_); break;
  13644. case 2: script_pushstrcopy(st,md->db->name.c_str()); break;
  13645. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13646. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13647. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13648. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13649. case 7: script_pushint(st,md->db->lv); break;
  13650. case 8: script_pushint(st,md->bl.id); break;
  13651. default:
  13652. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13653. script_pushnil(st);
  13654. return SCRIPT_CMD_FAILURE;
  13655. }
  13656. return SCRIPT_CMD_SUCCESS;
  13657. }
  13658. /*==========================================
  13659. * Shows wether your inventory(and equips) contain
  13660. selected card or not.
  13661. checkequipedcard(4001);
  13662. *------------------------------------------*/
  13663. BUILDIN_FUNC(checkequipedcard)
  13664. {
  13665. TBL_PC *sd;
  13666. if(script_rid2sd(sd)){
  13667. int32 n,i,c=0;
  13668. c=script_getnum(st,2);
  13669. for(i=0;i<MAX_INVENTORY;i++){
  13670. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13671. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13672. continue;
  13673. for (n=0; n < MAX_SLOTS; n++) {
  13674. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13675. script_pushint(st,1);
  13676. return SCRIPT_CMD_SUCCESS;
  13677. }
  13678. }
  13679. }
  13680. }
  13681. }
  13682. script_pushint(st,0);
  13683. return SCRIPT_CMD_SUCCESS;
  13684. }
  13685. BUILDIN_FUNC(jump_zero)
  13686. {
  13687. int64 sel=script_getnum64(st,2);
  13688. if(!sel) {
  13689. int32 pos;
  13690. if( !data_islabel(script_getdata(st,3)) ){
  13691. ShowError("script: jump_zero: not label !\n");
  13692. st->state=END;
  13693. return SCRIPT_CMD_FAILURE;
  13694. }
  13695. pos=script_getnum(st,3);
  13696. st->pos=pos;
  13697. st->state=GOTO;
  13698. }
  13699. return SCRIPT_CMD_SUCCESS;
  13700. }
  13701. /*==========================================
  13702. * movenpc [MouseJstr]
  13703. *------------------------------------------*/
  13704. BUILDIN_FUNC(movenpc)
  13705. {
  13706. TBL_NPC *nd = nullptr;
  13707. const char *npc;
  13708. int32 x,y;
  13709. npc = script_getstr(st,2);
  13710. x = script_getnum(st,3);
  13711. y = script_getnum(st,4);
  13712. if ((nd = npc_name2id(npc)) == nullptr){
  13713. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13714. return -1;
  13715. }
  13716. if (script_hasdata(st,5))
  13717. nd->ud.dir = script_getnum(st,5) % 8;
  13718. npc_movenpc(nd, x, y);
  13719. return SCRIPT_CMD_SUCCESS;
  13720. }
  13721. /*==========================================
  13722. * message [MouseJstr]
  13723. *------------------------------------------*/
  13724. BUILDIN_FUNC(message)
  13725. {
  13726. const char *msg,*player;
  13727. TBL_PC *pl_sd = nullptr;
  13728. player = script_getstr(st,2);
  13729. msg = script_getstr(st,3);
  13730. if((pl_sd=map_nick2sd((char *) player,false)) == nullptr)
  13731. return SCRIPT_CMD_SUCCESS;
  13732. clif_displaymessage(pl_sd->fd, msg);
  13733. return SCRIPT_CMD_SUCCESS;
  13734. }
  13735. /**
  13736. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13737. * @param flag: Specify target
  13738. * BC_ALL - Broadcast message is sent server-wide.
  13739. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13740. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13741. * BC_SELF - Message is sent only to player attached.
  13742. */
  13743. BUILDIN_FUNC(npctalk)
  13744. {
  13745. struct npc_data* nd = nullptr;
  13746. const char* str = script_getstr(st,2);
  13747. int32 color = 0xFFFFFF;
  13748. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13749. nd = npc_name2id(script_getstr(st, 3));
  13750. else
  13751. nd = (struct npc_data *)map_id2bl(st->oid);
  13752. if (script_hasdata(st, 5))
  13753. color = script_getnum(st, 5);
  13754. if (nd != nullptr) {
  13755. send_target target = AREA;
  13756. char message[CHAT_SIZE_MAX];
  13757. if (script_hasdata(st, 4)) {
  13758. switch(script_getnum(st, 4)) {
  13759. case BC_ALL: target = ALL_CLIENT; break;
  13760. case BC_MAP: target = ALL_SAMEMAP; break;
  13761. case BC_SELF: target = SELF; break;
  13762. case BC_AREA:
  13763. default: target = AREA; break;
  13764. }
  13765. }
  13766. safesnprintf(message, sizeof(message), "%s", str);
  13767. if (target != SELF)
  13768. clif_messagecolor(&nd->bl, color, message, true, target);
  13769. else {
  13770. TBL_PC *sd = map_id2sd(st->rid);
  13771. if (sd == nullptr)
  13772. return SCRIPT_CMD_FAILURE;
  13773. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13774. }
  13775. }
  13776. return SCRIPT_CMD_SUCCESS;
  13777. }
  13778. /**
  13779. * Sends a message to the waitingroom of the invoking NPC.
  13780. * chatmes "<message>"{,"<NPC name>"};
  13781. * @author Jey
  13782. */
  13783. BUILDIN_FUNC(chatmes)
  13784. {
  13785. struct npc_data* nd = nullptr;
  13786. const char* str = script_getstr(st,2);
  13787. if (script_hasdata(st, 3))
  13788. nd = npc_name2id(script_getstr(st, 3));
  13789. else
  13790. nd = (struct npc_data *)map_id2bl(st->oid);
  13791. if (nd != nullptr && nd->chat_id) {
  13792. char message[256];
  13793. safesnprintf(message, sizeof(message), "%s", str);
  13794. clif_GlobalMessage( *map_id2bl( nd->chat_id ), message, CHAT_WOS );
  13795. }
  13796. return SCRIPT_CMD_SUCCESS;
  13797. }
  13798. /**
  13799. * Change npc walkspeed.
  13800. * npcspeed <speed value> {,"<npc name>"};
  13801. */
  13802. BUILDIN_FUNC(npcspeed)
  13803. {
  13804. npc_data* nd;
  13805. if (script_hasdata(st, 3))
  13806. nd = npc_name2id(script_getstr(st, 3));
  13807. else
  13808. nd = map_id2nd(st->oid);
  13809. if (nd == nullptr) {
  13810. if (script_hasdata(st, 3))
  13811. ShowError("buildin_npcspeed: %s is a non-existing NPC.\n", script_getstr(st, 3));
  13812. else
  13813. ShowError("buildin_npcspeed: non-existing NPC.\n");
  13814. return SCRIPT_CMD_FAILURE;
  13815. }
  13816. int32 speed = script_getnum(st, 2);
  13817. if (speed < MIN_WALK_SPEED || speed > MAX_WALK_SPEED) {
  13818. ShowError("buildin_npcspeed: invalid speed %d (min: %d, max: %d).\n", speed, MIN_WALK_SPEED, MAX_WALK_SPEED);
  13819. return SCRIPT_CMD_FAILURE;
  13820. }
  13821. nd->speed = speed;
  13822. return SCRIPT_CMD_SUCCESS;
  13823. }
  13824. /**
  13825. * Make an npc walk to a position.
  13826. * npcwalkto <x>,<y> {,"<npc name>"} };
  13827. */
  13828. BUILDIN_FUNC(npcwalkto)
  13829. {
  13830. npc_data* nd;
  13831. if (script_hasdata(st, 4))
  13832. nd = npc_name2id(script_getstr(st, 4));
  13833. else
  13834. nd = map_id2nd(st->oid);
  13835. if (nd == nullptr) {
  13836. if (script_hasdata(st, 4))
  13837. ShowError("buildin_npcwalkto: %s is a non-existing NPC.\n", script_getstr(st, 4));
  13838. else
  13839. ShowError("buildin_npcwalkto: non-existing NPC.\n");
  13840. return SCRIPT_CMD_FAILURE;
  13841. }
  13842. if( nd->bl.m < 0 ){
  13843. ShowError( "buildin_npcwalkto: NPC is not on a map.\n" );
  13844. return SCRIPT_CMD_FAILURE;
  13845. }
  13846. struct map_data* mapdata = map_getmapdata( nd->bl.m );
  13847. int32 x = script_getnum(st, 2);
  13848. int32 y = script_getnum(st, 3);
  13849. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys ){
  13850. ShowWarning( "buildin_npcwalkto: coordinates %d/%d are out of bounds in map %s(%dx%d).\n", x, y, mapdata->name, mapdata->xs, mapdata->ys );
  13851. return SCRIPT_CMD_FAILURE;
  13852. }
  13853. if (!nd->status.hp)
  13854. status_calc_npc(nd, SCO_FIRST);
  13855. else
  13856. status_calc_npc(nd, SCO_NONE);
  13857. unit_walktoxy(&nd->bl,x,y,0);
  13858. return SCRIPT_CMD_SUCCESS;
  13859. }
  13860. /**
  13861. * Stop an npc's movement.
  13862. * npcstop {"<npc name>", {"<flag>"}};
  13863. */
  13864. BUILDIN_FUNC(npcstop)
  13865. {
  13866. npc_data* nd;
  13867. if (script_hasdata(st, 2))
  13868. nd = npc_name2id(script_getstr(st, 2));
  13869. else
  13870. nd = map_id2nd(st->oid);
  13871. if (nd == nullptr) {
  13872. if (script_hasdata(st, 2))
  13873. ShowError("buildin_npcstop: %s is a non-existing NPC.\n", script_getstr(st, 2));
  13874. else
  13875. ShowError("buildin_npcstop: non-existing NPC.\n");
  13876. return SCRIPT_CMD_FAILURE;
  13877. }
  13878. int32 flag = USW_FIXPOS | USW_MOVE_FULL_CELL | USW_FORCE_STOP;
  13879. if (script_hasdata(st, 3)) {
  13880. flag = script_getnum(st, 3);
  13881. if (flag < USW_NONE || flag > USW_ALL) {
  13882. ShowError("buildin_npcstop: invalid flag %d.\n", flag);
  13883. return SCRIPT_CMD_FAILURE;
  13884. }
  13885. if (flag & USW_FORCE_STOP)
  13886. nd->ud.state.force_walk = false;
  13887. }
  13888. unit_stop_walking( &nd->bl, flag );
  13889. return SCRIPT_CMD_SUCCESS;
  13890. }
  13891. /**
  13892. * getlook(<type>{,<char_id>})
  13893. **/
  13894. BUILDIN_FUNC(getlook)
  13895. {
  13896. int32 type,val;
  13897. TBL_PC *sd;
  13898. if (!script_charid2sd(3,sd)) {
  13899. script_pushint(st,-1);
  13900. return SCRIPT_CMD_FAILURE;
  13901. }
  13902. type=script_getnum(st,2);
  13903. val=-1;
  13904. switch(type) {
  13905. // TODO: implement LOOK_BASE as stated in script doc
  13906. case LOOK_HAIR: val=sd->status.hair; break; //1
  13907. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13908. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13909. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13910. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13911. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13912. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13913. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13914. case LOOK_SHOES: break; //9
  13915. case LOOK_ROBE: val=sd->status.robe; break; //12
  13916. case LOOK_BODY2: val=sd->status.body; break; //13
  13917. }
  13918. script_pushint(st,val);
  13919. return SCRIPT_CMD_SUCCESS;
  13920. }
  13921. /**
  13922. * getsavepoint(<information type>{,<char_id>})
  13923. * @param type 0- map name, 1- x, 2- y
  13924. **/
  13925. BUILDIN_FUNC(getsavepoint)
  13926. {
  13927. TBL_PC* sd;
  13928. int32 type;
  13929. if (!script_charid2sd(3,sd)) {
  13930. script_pushint(st,0);
  13931. return SCRIPT_CMD_SUCCESS;
  13932. }
  13933. type = script_getnum(st,2);
  13934. switch(type) {
  13935. case 0: script_pushstrcopy( st, sd->status.save_point.map ); break;
  13936. case 1: script_pushint(st,sd->status.save_point.x); break;
  13937. case 2: script_pushint(st,sd->status.save_point.y); break;
  13938. default:
  13939. script_pushint(st,0);
  13940. break;
  13941. }
  13942. return SCRIPT_CMD_SUCCESS;
  13943. }
  13944. /**
  13945. * Get position for BL objects.
  13946. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13947. * @param mapname: String variable for output map name
  13948. * @param x: Integer variable for output coord X
  13949. * @param y: Integer variable for output coord Y
  13950. * @param type: Type of object
  13951. * BL_PC - Character coord
  13952. * BL_NPC - NPC coord
  13953. * BL_PET - Pet coord
  13954. * BL_HOM - Homun coord
  13955. * BL_MER - Mercenary coord
  13956. * BL_ELEM - Elemental coord
  13957. * @param charname: Name object. If empty or "this" use the current object
  13958. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13959. */
  13960. BUILDIN_FUNC(getmapxy)
  13961. {
  13962. struct block_list *bl = nullptr;
  13963. TBL_PC *sd=nullptr;
  13964. int64 num;
  13965. const char *name;
  13966. char prefix;
  13967. int32 x,y,type = BL_PC;
  13968. char mapname[MAP_NAME_LENGTH];
  13969. if( !data_isreference(script_getdata(st,2)) ) {
  13970. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13971. script_pushint(st,-1);
  13972. return SCRIPT_CMD_FAILURE;
  13973. }
  13974. if( !data_isreference(script_getdata(st,3)) ) {
  13975. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13976. script_pushint(st,-1);
  13977. return SCRIPT_CMD_FAILURE;
  13978. }
  13979. if( !data_isreference(script_getdata(st,4)) ) {
  13980. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13981. script_pushint(st,-1);
  13982. return SCRIPT_CMD_FAILURE;
  13983. }
  13984. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13985. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13986. script_pushint(st,-1);
  13987. return SCRIPT_CMD_FAILURE;
  13988. }
  13989. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13990. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13991. script_pushint(st,-1);
  13992. return SCRIPT_CMD_FAILURE;
  13993. }
  13994. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13995. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13996. script_pushint(st,-1);
  13997. return SCRIPT_CMD_FAILURE;
  13998. }
  13999. // Possible needly check function parameters on C_STR,C_INT,C_INT
  14000. if (script_hasdata(st, 5))
  14001. type = script_getnum(st, 5);
  14002. switch (type) {
  14003. case BL_PC: //Get Character Position
  14004. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  14005. bl = &sd->bl;
  14006. break;
  14007. case BL_NPC: //Get NPC Position
  14008. if (script_hasdata(st, 6)) {
  14009. struct npc_data *nd;
  14010. if (script_isstring(st, 6))
  14011. nd = npc_name2id(script_getstr(st, 6));
  14012. else
  14013. nd = map_id2nd(script_getnum(st, 6));
  14014. if (nd)
  14015. bl = &nd->bl;
  14016. } else //In case the origin is not an NPC?
  14017. bl = map_id2bl(st->oid);
  14018. break;
  14019. case BL_PET: //Get Pet Position
  14020. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->pd)
  14021. bl = &sd->pd->bl;
  14022. break;
  14023. case BL_HOM: //Get Homun Position
  14024. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->hd)
  14025. bl = &sd->hd->bl;
  14026. break;
  14027. case BL_MER: //Get Mercenary Position
  14028. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->md)
  14029. bl = &sd->md->bl;
  14030. break;
  14031. case BL_ELEM: //Get Elemental Position
  14032. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->ed)
  14033. bl = &sd->ed->bl;
  14034. break;
  14035. default:
  14036. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  14037. script_pushint(st,-1);
  14038. return SCRIPT_CMD_FAILURE;
  14039. }
  14040. if (!bl) { //No object found.
  14041. script_pushint(st,-1);
  14042. return SCRIPT_CMD_SUCCESS;
  14043. }
  14044. x= bl->x;
  14045. y= bl->y;
  14046. if (bl->m >= 0)
  14047. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  14048. else
  14049. memset(mapname, '\0', sizeof(mapname));
  14050. //Set MapName$
  14051. num=st->stack->stack_data[st->start+2].u.num;
  14052. name=get_str(script_getvarid(num));
  14053. prefix=*name;
  14054. if(not_server_variable(prefix)){
  14055. if( !script_rid2sd(sd) ){
  14056. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  14057. return SCRIPT_CMD_FAILURE;
  14058. }
  14059. }else
  14060. sd=nullptr;
  14061. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  14062. //Set MapX
  14063. num=st->stack->stack_data[st->start+3].u.num;
  14064. name=get_str(script_getvarid(num));
  14065. prefix=*name;
  14066. if(not_server_variable(prefix)){
  14067. if( !script_rid2sd(sd) ){
  14068. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  14069. return SCRIPT_CMD_FAILURE;
  14070. }
  14071. }else
  14072. sd=nullptr;
  14073. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  14074. //Set MapY
  14075. num=st->stack->stack_data[st->start+4].u.num;
  14076. name=get_str(script_getvarid(num));
  14077. prefix=*name;
  14078. if(not_server_variable(prefix)){
  14079. if( !script_rid2sd(sd) ){
  14080. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  14081. return SCRIPT_CMD_FAILURE;
  14082. }
  14083. }else
  14084. sd=nullptr;
  14085. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  14086. //Return Success value
  14087. script_pushint(st,0);
  14088. return SCRIPT_CMD_SUCCESS;
  14089. }
  14090. /// Returns the map name of given map ID.
  14091. ///
  14092. /// mapid2name <map ID>;
  14093. BUILDIN_FUNC(mapid2name)
  14094. {
  14095. uint16 m = script_getnum(st, 2);
  14096. if (m >= MAX_MAP_PER_SERVER) {
  14097. script_pushconststr(st, "");
  14098. return SCRIPT_CMD_FAILURE;
  14099. }
  14100. script_pushstrcopy(st, map_mapid2mapname(m));
  14101. return SCRIPT_CMD_SUCCESS;
  14102. }
  14103. /*==========================================
  14104. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  14105. *------------------------------------------*/
  14106. BUILDIN_FUNC(logmes)
  14107. {
  14108. const char *str;
  14109. map_session_data* sd = map_id2sd(st->rid);
  14110. str = script_getstr(st,2);
  14111. if( sd ){
  14112. log_npc(sd,str);
  14113. }else{
  14114. struct npc_data* nd = map_id2nd(st->oid);
  14115. if( !nd ){
  14116. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  14117. return SCRIPT_CMD_FAILURE;
  14118. }
  14119. log_npc(nd,str);
  14120. }
  14121. return SCRIPT_CMD_SUCCESS;
  14122. }
  14123. BUILDIN_FUNC(summon)
  14124. {
  14125. int32 _class, timeout=0;
  14126. const char *str,*event="";
  14127. map_session_data* sd;
  14128. t_tick tick = gettick();
  14129. if (!script_rid2sd(sd))
  14130. return SCRIPT_CMD_FAILURE;
  14131. str =script_getstr(st,2);
  14132. _class=script_getnum(st,3);
  14133. if( script_hasdata(st,4) )
  14134. timeout=script_getnum(st,4);
  14135. if( script_hasdata(st,5) ){
  14136. event=script_getstr(st,5);
  14137. check_event(st, event);
  14138. }
  14139. mob_data* md = mob_once_spawn_sub( &sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE );
  14140. if( md == nullptr ){
  14141. ShowError( "buildin_summon: Invalid mob ID %d.\n", _class );
  14142. st->state = END;
  14143. return SCRIPT_CMD_FAILURE;
  14144. }
  14145. clif_skill_poseffect( &sd->bl, AM_CALLHOMUN, 1, sd->bl.x, sd->bl.y, tick );
  14146. md->master_id = sd->bl.id;
  14147. md->special_state.ai = AI_ATTACK;
  14148. if( md->deletetimer != INVALID_TIMER ){
  14149. delete_timer( md->deletetimer, mob_timer_delete );
  14150. }
  14151. md->deletetimer = add_timer( tick + ( timeout > 0 ? timeout : 60000 ), mob_timer_delete, md->bl.id, 0 );
  14152. // Now it is ready for spawning.
  14153. mob_spawn( md );
  14154. clif_specialeffect( &md->bl,EF_ENTRY2,AREA );
  14155. sc_start4( nullptr,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000 );
  14156. script_pushint( st, md->bl.id );
  14157. return SCRIPT_CMD_SUCCESS;
  14158. }
  14159. /*==========================================
  14160. * Checks whether it is daytime/nighttime
  14161. *------------------------------------------*/
  14162. BUILDIN_FUNC(isnight)
  14163. {
  14164. script_pushint(st,(night_flag == 1));
  14165. return SCRIPT_CMD_SUCCESS;
  14166. }
  14167. BUILDIN_FUNC(isday)
  14168. {
  14169. script_pushint(st,(night_flag == 0));
  14170. return SCRIPT_CMD_SUCCESS;
  14171. }
  14172. /*================================================
  14173. * Check how many items/cards in the list are
  14174. * equipped - used for 2/15's cards patch [celest]
  14175. *------------------------------------------------*/
  14176. BUILDIN_FUNC(isequippedcnt)
  14177. {
  14178. TBL_PC *sd;
  14179. if (!script_rid2sd(sd)) {
  14180. script_pushint(st,0);
  14181. return SCRIPT_CMD_SUCCESS;
  14182. }
  14183. int32 ret = 0;
  14184. int32 total = script_lastdata(st);
  14185. std::vector<int32> list(total);
  14186. for (int32 i = 2; i <= total; ++i) {
  14187. int32 id = script_getnum(st,i);
  14188. if (id <= 0)
  14189. continue;
  14190. if (std::find(list.begin(), list.end(), id) != list.end())
  14191. continue;
  14192. list.push_back(id);
  14193. for (short j = 0; j < EQI_MAX; j++) {
  14194. short index = sd->equip_index[j];
  14195. if (index < 0)
  14196. continue;
  14197. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  14198. continue;
  14199. if (!sd->inventory_data[index])
  14200. continue;
  14201. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  14202. if (sd->inventory_data[index]->nameid == id)
  14203. ret += sd->inventory.u.items_inventory[index].amount;
  14204. } else { //Count cards.
  14205. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14206. continue; //No cards
  14207. for (short k = 0; k < MAX_SLOTS; k++) {
  14208. if (sd->inventory.u.items_inventory[index].card[k] == id)
  14209. ret++; //[Lupus]
  14210. }
  14211. }
  14212. }
  14213. }
  14214. script_pushint(st,ret);
  14215. return SCRIPT_CMD_SUCCESS;
  14216. }
  14217. /*================================================
  14218. * Check whether another card has been
  14219. * equipped - used for 2/15's cards patch [celest]
  14220. * -- Items checked cannot be reused in another
  14221. * card set to prevent exploits
  14222. *------------------------------------------------*/
  14223. BUILDIN_FUNC(isequipped)
  14224. {
  14225. TBL_PC *sd;
  14226. int32 i, id = 1;
  14227. int32 ret = -1;
  14228. //Original hash to reverse it when full check fails.
  14229. uint32 setitem_hash = 0, setitem_hash2 = 0;
  14230. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  14231. script_pushint(st,0);
  14232. return SCRIPT_CMD_SUCCESS;
  14233. }
  14234. setitem_hash = sd->bonus.setitem_hash;
  14235. setitem_hash2 = sd->bonus.setitem_hash2;
  14236. for (i=0; id!=0; i++) {
  14237. int32 flag = 0;
  14238. short j;
  14239. FETCH (i+2, id) else id = 0;
  14240. if (id <= 0)
  14241. continue;
  14242. for (j=0; j<EQI_MAX; j++) {
  14243. short index = sd->equip_index[j];
  14244. if(index < 0)
  14245. continue;
  14246. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  14247. continue;
  14248. if(!sd->inventory_data[index])
  14249. continue;
  14250. if (itemdb_type(id) != IT_CARD) {
  14251. if (sd->inventory_data[index]->nameid != id)
  14252. continue;
  14253. flag = 1;
  14254. break;
  14255. } else { //Cards
  14256. short k;
  14257. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14258. continue;
  14259. for (k = 0; k < MAX_SLOTS; k++)
  14260. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  14261. uint32 hash = 0;
  14262. if (sd->inventory.u.items_inventory[index].card[k] != id)
  14263. continue;
  14264. hash = 1<<((j<5?j:j-5)*4 + k);
  14265. // check if card is already used by another set
  14266. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  14267. continue;
  14268. // We have found a match
  14269. flag = 1;
  14270. // Set hash so this card cannot be used by another
  14271. if (j<5)
  14272. sd->bonus.setitem_hash |= hash;
  14273. else
  14274. sd->bonus.setitem_hash2 |= hash;
  14275. break;
  14276. }
  14277. }
  14278. if (flag) break; //Card found
  14279. }
  14280. if (ret == -1)
  14281. ret = flag;
  14282. else
  14283. ret &= flag;
  14284. if (!ret) break;
  14285. }
  14286. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  14287. sd->bonus.setitem_hash = setitem_hash;
  14288. sd->bonus.setitem_hash2 = setitem_hash2;
  14289. }
  14290. script_pushint(st,ret);
  14291. return SCRIPT_CMD_SUCCESS;
  14292. }
  14293. /*================================================
  14294. * Check how many given inserted cards in the CURRENT
  14295. * weapon - used for 2/15's cards patch [Lupus]
  14296. *------------------------------------------------*/
  14297. BUILDIN_FUNC(cardscnt)
  14298. {
  14299. TBL_PC *sd;
  14300. int32 i, k, id = 1;
  14301. int32 ret = 0;
  14302. int32 index;
  14303. if( !script_rid2sd(sd) ){
  14304. script_pushint(st,0);
  14305. return SCRIPT_CMD_SUCCESS;
  14306. }
  14307. for (i=0; id!=0; i++) {
  14308. FETCH (i+2, id) else id = 0;
  14309. if (id <= 0)
  14310. continue;
  14311. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  14312. if(index < 0) continue;
  14313. if(!sd->inventory_data[index])
  14314. continue;
  14315. if(itemdb_type(id) != IT_CARD) {
  14316. if (sd->inventory_data[index]->nameid == id)
  14317. ret+= sd->inventory.u.items_inventory[index].amount;
  14318. } else {
  14319. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14320. continue;
  14321. for (k=0; k < MAX_SLOTS; k++) {
  14322. if (sd->inventory.u.items_inventory[index].card[k] == id)
  14323. ret++;
  14324. }
  14325. }
  14326. }
  14327. script_pushint(st,ret);
  14328. // script_pushint(st,current_equip_item_index);
  14329. return SCRIPT_CMD_SUCCESS;
  14330. }
  14331. /*=======================================================
  14332. * Returns the refined number of the current item, or an
  14333. * item with inventory index specified
  14334. *-------------------------------------------------------*/
  14335. BUILDIN_FUNC(getrefine)
  14336. {
  14337. TBL_PC *sd;
  14338. if (script_rid2sd(sd)){
  14339. if( current_equip_item_index == -1 ){
  14340. script_pushint(st, 0);
  14341. return SCRIPT_CMD_FAILURE;
  14342. }
  14343. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  14344. }else
  14345. script_pushint(st,0);
  14346. return SCRIPT_CMD_SUCCESS;
  14347. }
  14348. /*=======================================================
  14349. * Day/Night controls
  14350. *-------------------------------------------------------*/
  14351. BUILDIN_FUNC(night)
  14352. {
  14353. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  14354. return SCRIPT_CMD_SUCCESS;
  14355. }
  14356. BUILDIN_FUNC(day)
  14357. {
  14358. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  14359. return SCRIPT_CMD_SUCCESS;
  14360. }
  14361. /**
  14362. * unequip <equipment slot>{,<char_id>};
  14363. * @author [Spectre]
  14364. **/
  14365. BUILDIN_FUNC(unequip) {
  14366. int32 pos;
  14367. TBL_PC *sd;
  14368. if (!script_charid2sd(3,sd))
  14369. return SCRIPT_CMD_FAILURE;
  14370. pos = script_getnum(st,2);
  14371. if (equip_index_check(pos)) {
  14372. short i = pc_checkequip(sd,equip_bitmask[pos]);
  14373. if (i >= 0) {
  14374. pc_unequipitem(sd,i,1|2);
  14375. script_pushint(st, 1);
  14376. return SCRIPT_CMD_SUCCESS;
  14377. }
  14378. }
  14379. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  14380. script_pushint(st, 0);
  14381. return SCRIPT_CMD_FAILURE;
  14382. }
  14383. /**
  14384. * equip <item id>{,<char_id>};
  14385. **/
  14386. BUILDIN_FUNC(equip) {
  14387. TBL_PC *sd;
  14388. if (!script_charid2sd(3,sd))
  14389. return SCRIPT_CMD_FAILURE;
  14390. t_itemid nameid = script_getnum(st,2);
  14391. std::shared_ptr<item_data> id = item_db.find(nameid);
  14392. if (id != nullptr) {
  14393. int32 i;
  14394. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  14395. if (i < MAX_INVENTORY) {
  14396. pc_equipitem(sd,i,id->equip);
  14397. script_pushint(st,1);
  14398. return SCRIPT_CMD_SUCCESS;
  14399. }
  14400. }
  14401. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  14402. script_pushint(st,0);
  14403. return SCRIPT_CMD_FAILURE;
  14404. }
  14405. BUILDIN_FUNC(autoequip)
  14406. {
  14407. t_itemid nameid=script_getnum(st,2);
  14408. int32 flag=script_getnum(st,3);
  14409. std::shared_ptr<item_data> id = item_db.find(nameid);
  14410. if( id == nullptr )
  14411. {
  14412. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  14413. return SCRIPT_CMD_FAILURE;
  14414. }
  14415. if( !itemdb_isequip2(id.get()) )
  14416. {
  14417. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  14418. return SCRIPT_CMD_FAILURE;
  14419. }
  14420. id->flag.autoequip = flag>0?1:0;
  14421. return SCRIPT_CMD_SUCCESS;
  14422. }
  14423. /**
  14424. * Set the value of a given battle config
  14425. * setbattleflag "<battle flag>",<value>{,<reload>};
  14426. */
  14427. BUILDIN_FUNC(setbattleflag)
  14428. {
  14429. const char *flag, *value;
  14430. flag = script_getstr(st,2);
  14431. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  14432. if (battle_set_value(flag, value) == 0)
  14433. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  14434. else {
  14435. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  14436. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  14437. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  14438. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  14439. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  14440. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  14441. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  14442. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  14443. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  14444. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  14445. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  14446. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  14447. };
  14448. uint8 i;
  14449. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  14450. if (!strcmpi(flag, expdrop_flags[i])) {
  14451. mob_reload();
  14452. break;
  14453. }
  14454. }
  14455. }
  14456. }
  14457. return SCRIPT_CMD_SUCCESS;
  14458. }
  14459. /**
  14460. * Get the value of a given battle config
  14461. * getbattleflag("<battle flag>")
  14462. */
  14463. BUILDIN_FUNC(getbattleflag)
  14464. {
  14465. const char *flag = script_getstr(st,2);
  14466. script_pushint(st,battle_get_value(flag));
  14467. return SCRIPT_CMD_SUCCESS;
  14468. }
  14469. //=======================================================
  14470. // strlen [Valaris]
  14471. //-------------------------------------------------------
  14472. BUILDIN_FUNC(getstrlen)
  14473. {
  14474. const char *str = script_getstr(st,2);
  14475. int32 len = (str) ? (int)strlen(str) : 0;
  14476. script_pushint(st,len);
  14477. return SCRIPT_CMD_SUCCESS;
  14478. }
  14479. //=======================================================
  14480. // isalpha [Valaris]
  14481. //-------------------------------------------------------
  14482. BUILDIN_FUNC(charisalpha)
  14483. {
  14484. const char *str=script_getstr(st,2);
  14485. int32 pos=script_getnum(st,3);
  14486. int32 val = ( str && pos >= 0 && (uint32)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  14487. script_pushint(st,val);
  14488. return SCRIPT_CMD_SUCCESS;
  14489. }
  14490. //=======================================================
  14491. // charisupper <str>, <index>
  14492. //-------------------------------------------------------
  14493. BUILDIN_FUNC(charisupper)
  14494. {
  14495. const char *str = script_getstr(st,2);
  14496. int32 pos = script_getnum(st,3);
  14497. int32 val = ( str && pos >= 0 && (uint32)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  14498. script_pushint(st,val);
  14499. return SCRIPT_CMD_SUCCESS;
  14500. }
  14501. //=======================================================
  14502. // charislower <str>, <index>
  14503. //-------------------------------------------------------
  14504. BUILDIN_FUNC(charislower)
  14505. {
  14506. const char *str = script_getstr(st,2);
  14507. int32 pos = script_getnum(st,3);
  14508. int32 val = ( str && pos >= 0 && (uint32)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  14509. script_pushint(st,val);
  14510. return SCRIPT_CMD_SUCCESS;
  14511. }
  14512. //=======================================================
  14513. // charat <str>, <index>
  14514. //-------------------------------------------------------
  14515. BUILDIN_FUNC(charat) {
  14516. const char *str = script_getstr(st,2);
  14517. int32 pos = script_getnum(st,3);
  14518. if( pos >= 0 && (uint32)pos < strlen(str) ) {
  14519. char output[2];
  14520. output[0] = str[pos];
  14521. output[1] = '\0';
  14522. script_pushstrcopy(st, output);
  14523. } else
  14524. script_pushconststr(st, "");
  14525. return SCRIPT_CMD_SUCCESS;
  14526. }
  14527. //=======================================================
  14528. // setchar <string>, <char>, <index>
  14529. //-------------------------------------------------------
  14530. BUILDIN_FUNC(setchar)
  14531. {
  14532. const char *str = script_getstr(st,2);
  14533. const char *c = script_getstr(st,3);
  14534. int32 index = script_getnum(st,4);
  14535. char *output = aStrdup(str);
  14536. if(index >= 0 && index < strlen(output))
  14537. output[index] = *c;
  14538. script_pushstr(st, output);
  14539. return SCRIPT_CMD_SUCCESS;
  14540. }
  14541. //=======================================================
  14542. // insertchar <string>, <char>, <index>
  14543. //-------------------------------------------------------
  14544. BUILDIN_FUNC(insertchar)
  14545. {
  14546. const char *str = script_getstr(st,2);
  14547. const char *c = script_getstr(st,3);
  14548. size_t index = script_getnum( st, 4 );
  14549. char *output;
  14550. size_t len = strlen(str);
  14551. if(index < 0)
  14552. index = 0;
  14553. else if(index > len)
  14554. index = len;
  14555. output = (char*)aMalloc(len + 2);
  14556. memcpy(output, str, index);
  14557. output[index] = c[0];
  14558. memcpy(&output[index+1], &str[index], len - index);
  14559. output[len+1] = '\0';
  14560. script_pushstr(st, output);
  14561. return SCRIPT_CMD_SUCCESS;
  14562. }
  14563. //=======================================================
  14564. // delchar <string>, <index>
  14565. //-------------------------------------------------------
  14566. BUILDIN_FUNC(delchar)
  14567. {
  14568. const char *str = script_getstr(st,2);
  14569. int32 index = script_getnum(st,3);
  14570. char *output;
  14571. size_t len = strlen(str);
  14572. if(index < 0 || index > len) {
  14573. //return original
  14574. output = aStrdup(str);
  14575. script_pushstr(st, output);
  14576. return SCRIPT_CMD_SUCCESS;
  14577. }
  14578. output = (char*)aMalloc(len);
  14579. memcpy(output, str, index);
  14580. memcpy(&output[index], &str[index+1], len - index);
  14581. script_pushstr(st, output);
  14582. return SCRIPT_CMD_SUCCESS;
  14583. }
  14584. //=======================================================
  14585. // strtoupper <str>
  14586. //-------------------------------------------------------
  14587. BUILDIN_FUNC(strtoupper)
  14588. {
  14589. const char *str = script_getstr(st,2);
  14590. char *output = aStrdup(str);
  14591. char *cursor = output;
  14592. while (*cursor != '\0') {
  14593. *cursor = TOUPPER(*cursor);
  14594. cursor++;
  14595. }
  14596. script_pushstr(st, output);
  14597. return SCRIPT_CMD_SUCCESS;
  14598. }
  14599. //=======================================================
  14600. // strtolower <str>
  14601. //-------------------------------------------------------
  14602. BUILDIN_FUNC(strtolower)
  14603. {
  14604. const char *str = script_getstr(st,2);
  14605. char *output = aStrdup(str);
  14606. char *cursor = output;
  14607. while (*cursor != '\0') {
  14608. *cursor = TOLOWER(*cursor);
  14609. cursor++;
  14610. }
  14611. script_pushstr(st, output);
  14612. return SCRIPT_CMD_SUCCESS;
  14613. }
  14614. //=======================================================
  14615. // substr <str>, <start>, <end>
  14616. //-------------------------------------------------------
  14617. BUILDIN_FUNC(substr)
  14618. {
  14619. const char *str = script_getstr(st,2);
  14620. char *output;
  14621. int32 start = script_getnum(st,3);
  14622. int32 end = script_getnum(st,4);
  14623. int32 len = 0;
  14624. if(start >= 0 && end < strlen(str) && start <= end) {
  14625. len = end - start + 1;
  14626. output = (char*)aMalloc(len + 1);
  14627. memcpy(output, &str[start], len);
  14628. } else
  14629. output = (char*)aMalloc(1);
  14630. output[len] = '\0';
  14631. script_pushstr(st, output);
  14632. return SCRIPT_CMD_SUCCESS;
  14633. }
  14634. //=======================================================
  14635. // explode <dest_string_array>, <str>, <delimiter>
  14636. // Note: delimiter is limited to 1 char
  14637. //-------------------------------------------------------
  14638. BUILDIN_FUNC(explode)
  14639. {
  14640. struct script_data* data = script_getdata(st, 2);
  14641. const char *str = script_getstr(st,3);
  14642. const char delimiter = script_getstr(st, 4)[0];
  14643. int32 id;
  14644. size_t len = strlen(str);
  14645. int32 i = 0, j = 0;
  14646. int32 start;
  14647. char *temp;
  14648. const char* name;
  14649. TBL_PC* sd = nullptr;
  14650. temp = (char*)aMalloc(len + 1);
  14651. if( !data_isreference(data) ) {
  14652. ShowError("script:explode: not a variable\n");
  14653. script_reportdata(data);
  14654. st->state = END;
  14655. return SCRIPT_CMD_FAILURE;// not a variable
  14656. }
  14657. id = reference_getid(data);
  14658. start = reference_getindex(data);
  14659. name = reference_getname(data);
  14660. if( !is_string_variable(name) ) {
  14661. ShowError("script:explode: not string array\n");
  14662. script_reportdata(data);
  14663. st->state = END;
  14664. return SCRIPT_CMD_FAILURE;// data type mismatch
  14665. }
  14666. if( not_server_variable(*name) ) {
  14667. if( !script_rid2sd(sd) )
  14668. return SCRIPT_CMD_SUCCESS;// no player attached
  14669. }
  14670. while(str[i] != '\0') {
  14671. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14672. temp[j] = '\0';
  14673. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14674. j = 0;
  14675. ++i;
  14676. } else {
  14677. temp[j++] = str[i++];
  14678. }
  14679. }
  14680. //set last string
  14681. temp[j] = '\0';
  14682. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14683. aFree(temp);
  14684. return SCRIPT_CMD_SUCCESS;
  14685. }
  14686. //=======================================================
  14687. // implode <string_array>
  14688. // implode <string_array>, <glue>
  14689. //-------------------------------------------------------
  14690. BUILDIN_FUNC(implode)
  14691. {
  14692. struct script_data* data = script_getdata(st, 2);
  14693. if( !data_isreference(data) ) {
  14694. ShowError("script:implode: not a variable\n");
  14695. script_reportdata(data);
  14696. st->state = END;
  14697. return SCRIPT_CMD_FAILURE;// not a variable
  14698. }
  14699. uint32 id = reference_getid( data );
  14700. const char* name = reference_getname( data );
  14701. if( !is_string_variable(name) ) {
  14702. ShowError("script:implode: not string array\n");
  14703. script_reportdata(data);
  14704. st->state = END;
  14705. return SCRIPT_CMD_FAILURE;// data type mismatch
  14706. }
  14707. map_session_data* sd = nullptr;
  14708. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14709. return SCRIPT_CMD_SUCCESS;// no player attached
  14710. }
  14711. //count chars
  14712. const uint32 array_size = script_array_highest_key( st, sd, name, reference_getref( data ) );
  14713. if(array_size == 0) {
  14714. ShowWarning("script:implode: array length = 0\n");
  14715. script_pushstrcopy( st, "NULL" );
  14716. } else {
  14717. const char *glue = nullptr, *temp;
  14718. size_t len = 0, glue_len = 0, k = 0;
  14719. for( uint32 i = 0; i < array_size; ++i ){
  14720. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14721. len += strlen(temp);
  14722. // Remove stack entry from get_val2_str
  14723. script_removetop( st, -1, 0 );
  14724. }
  14725. //allocate mem
  14726. if( script_hasdata(st,3) ) {
  14727. glue = script_getstr(st,3);
  14728. glue_len = strlen(glue);
  14729. len += glue_len * (size_t)( array_size - 1 );
  14730. }
  14731. char* output = (char*)aMalloc( len + 1 );
  14732. //build output
  14733. for( uint32 i = 0; i < array_size - 1; ++i ){
  14734. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14735. len = strlen(temp);
  14736. memcpy(&output[k], temp, len);
  14737. k += len;
  14738. // Remove stack entry from get_val2_str
  14739. script_removetop( st, -1, 0 );
  14740. if(glue_len != 0) {
  14741. memcpy(&output[k], glue, glue_len);
  14742. k += glue_len;
  14743. }
  14744. }
  14745. temp = get_val2_str( st, reference_uid( id, array_size - 1), reference_getref( data ) );
  14746. len = strlen(temp);
  14747. memcpy(&output[k], temp, len);
  14748. k += len;
  14749. output[k] = '\0';
  14750. // Remove stack entry from get_val2_str
  14751. script_removetop( st, -1, 0 );
  14752. script_pushstr( st, output );
  14753. }
  14754. return SCRIPT_CMD_SUCCESS;
  14755. }
  14756. //=======================================================
  14757. // sprintf(<format>, ...);
  14758. // Implements C sprintf, except format %n. The resulting string is
  14759. // returned, instead of being saved in variable by reference.
  14760. //-------------------------------------------------------
  14761. BUILDIN_FUNC(sprintf)
  14762. {
  14763. uint32 argc = 0, arg = 0;
  14764. size_t buf2_len = 0;
  14765. const char* format;
  14766. char* p;
  14767. char* q;
  14768. char* buf = nullptr;
  14769. char* buf2 = nullptr;
  14770. struct script_data* data;
  14771. StringBuf final_buf;
  14772. // Fetch init data
  14773. format = script_getstr(st, 2);
  14774. argc = script_lastdata(st)-2;
  14775. size_t len = strlen( format );
  14776. // Skip parsing, where no parsing is required.
  14777. if(len == 0) {
  14778. script_pushconststr(st,"");
  14779. return SCRIPT_CMD_SUCCESS;
  14780. }
  14781. // Pessimistic alloc
  14782. CREATE(buf, char, len+1);
  14783. // Need not be parsed, just solve stuff like %%.
  14784. if(argc == 0) {
  14785. memcpy(buf,format,len+1);
  14786. script_pushstrcopy(st, buf);
  14787. aFree(buf);
  14788. return SCRIPT_CMD_SUCCESS;
  14789. }
  14790. safestrncpy(buf, format, len+1);
  14791. // Issue sprintf for each parameter
  14792. StringBuf_Init(&final_buf);
  14793. q = buf;
  14794. while((p = strchr(q, '%')) != nullptr) {
  14795. if(p != q) {
  14796. len = p - q + 1;
  14797. if(buf2_len < len) {
  14798. RECREATE(buf2, char, len);
  14799. buf2_len = len;
  14800. }
  14801. safestrncpy(buf2, q, len);
  14802. StringBuf_AppendStr(&final_buf, buf2);
  14803. q = p;
  14804. }
  14805. p = q + 1;
  14806. if(*p == '%') { // %%
  14807. StringBuf_AppendStr(&final_buf, "%");
  14808. q+=2;
  14809. continue;
  14810. }
  14811. if(*p == 'n') { // %n
  14812. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14813. script_reportsrc(st);
  14814. q+=2;
  14815. continue;
  14816. }
  14817. if(arg >= argc) {
  14818. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14819. if(buf) aFree(buf);
  14820. if(buf2) aFree(buf2);
  14821. StringBuf_Destroy(&final_buf);
  14822. script_pushconststr(st,"");
  14823. return SCRIPT_CMD_FAILURE;
  14824. }
  14825. if((p = strchr(q+1, '%')) == nullptr)
  14826. p = strchr(q, 0); // EOS
  14827. len = p - q + 1;
  14828. if(buf2_len < len) {
  14829. RECREATE(buf2, char, len);
  14830. buf2_len = len;
  14831. }
  14832. safestrncpy(buf2, q, len);
  14833. q = p;
  14834. // Note: This assumes the passed value being the correct
  14835. // type to the current format specifier. If not, the server
  14836. // probably crashes or returns anything else, than expected,
  14837. // but it would behave in normal code the same way so it's
  14838. // the scripter's responsibility.
  14839. data = script_getdata(st, arg+3);
  14840. if(data_isstring(data)) // String
  14841. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14842. else if(data_isint(data)) // Number
  14843. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14844. else if(data_isreference(data)) { // Variable
  14845. char* name = reference_getname(data);
  14846. if(name[strlen(name)-1]=='$') // var Str
  14847. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14848. else // var Int
  14849. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14850. } else { // Unsupported type
  14851. ShowError("buildin_sprintf: Unknown argument type!\n");
  14852. if(buf) aFree(buf);
  14853. if(buf2) aFree(buf2);
  14854. StringBuf_Destroy(&final_buf);
  14855. script_pushconststr(st,"");
  14856. return SCRIPT_CMD_FAILURE;
  14857. }
  14858. arg++;
  14859. }
  14860. // Append anything left
  14861. if(*q)
  14862. StringBuf_AppendStr(&final_buf, q);
  14863. // Passed more, than needed
  14864. if(arg < argc) {
  14865. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14866. script_reportsrc(st);
  14867. }
  14868. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14869. if(buf) aFree(buf);
  14870. if(buf2) aFree(buf2);
  14871. StringBuf_Destroy(&final_buf);
  14872. return SCRIPT_CMD_SUCCESS;
  14873. }
  14874. //=======================================================
  14875. // sscanf(<str>, <format>, ...);
  14876. // Implements C sscanf.
  14877. //-------------------------------------------------------
  14878. BUILDIN_FUNC(sscanf){
  14879. uint32 argc, arg = 0;
  14880. struct script_data* data;
  14881. map_session_data* sd = nullptr;
  14882. const char* str;
  14883. const char* format;
  14884. const char* p;
  14885. const char* q;
  14886. char* buf = nullptr;
  14887. char* buf_p;
  14888. char* ref_str = nullptr;
  14889. int32 ref_int;
  14890. // Get data
  14891. str = script_getstr(st, 2);
  14892. format = script_getstr(st, 3);
  14893. argc = script_lastdata(st)-3;
  14894. size_t len = strlen(format);
  14895. if (len != 0 && strlen(str) == 0) {
  14896. // If the source string is empty but the format string is not, we return -1
  14897. // according to the C specs. (if the format string is also empty, we shall
  14898. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14899. script_pushint(st, -1);
  14900. return SCRIPT_CMD_SUCCESS;
  14901. }
  14902. CREATE(buf, char, len*2+1);
  14903. // Issue sscanf for each parameter
  14904. *buf = 0;
  14905. q = format;
  14906. while((p = strchr(q, '%'))){
  14907. if(p!=q){
  14908. strncat(buf, q, (size_t)(p-q));
  14909. q = p;
  14910. }
  14911. p = q+1;
  14912. if(*p=='*' || *p=='%'){ // Skip
  14913. strncat(buf, q, 2);
  14914. q+=2;
  14915. continue;
  14916. }
  14917. if(arg>=argc){
  14918. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14919. script_pushint(st, -1);
  14920. if(buf) aFree(buf);
  14921. if(ref_str) aFree(ref_str);
  14922. return SCRIPT_CMD_FAILURE;
  14923. }
  14924. if((p = strchr(q+1, '%'))==nullptr){
  14925. p = strchr(q, 0); // EOS
  14926. }
  14927. len = p-q;
  14928. strncat(buf, q, len);
  14929. q = p;
  14930. // Validate output
  14931. data = script_getdata(st, arg+4);
  14932. if(!data_isreference(data) || !reference_tovariable(data)){
  14933. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14934. script_pushint(st, -1);
  14935. if(buf) aFree(buf);
  14936. if(ref_str) aFree(ref_str);
  14937. return SCRIPT_CMD_FAILURE;
  14938. }
  14939. buf_p = reference_getname(data);
  14940. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14941. script_pushint(st, -1);
  14942. if(buf) aFree(buf);
  14943. if(ref_str) aFree(ref_str);
  14944. return SCRIPT_CMD_SUCCESS;
  14945. }
  14946. // Save value if any
  14947. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14948. if(ref_str==nullptr){
  14949. CREATE(ref_str, char, strlen(str)+1);
  14950. }
  14951. if(sscanf(str, buf, ref_str)==0){
  14952. break;
  14953. }
  14954. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14955. } else { // Number
  14956. if(sscanf(str, buf, &ref_int)==0){
  14957. break;
  14958. }
  14959. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14960. }
  14961. arg++;
  14962. // Disable used format (%... -> %*...)
  14963. buf_p = strchr(buf, 0);
  14964. memmove(buf_p-len+2, buf_p-len+1, len);
  14965. *(buf_p-len+1) = '*';
  14966. }
  14967. script_pushint(st, arg);
  14968. if(buf) aFree(buf);
  14969. if(ref_str) aFree(ref_str);
  14970. return SCRIPT_CMD_SUCCESS;
  14971. }
  14972. //=======================================================
  14973. // strpos(<haystack>, <needle>)
  14974. // strpos(<haystack>, <needle>, <offset>)
  14975. //
  14976. // Implements PHP style strpos. Adapted from code from
  14977. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14978. //-------------------------------------------------------
  14979. BUILDIN_FUNC(strpos) {
  14980. const char *haystack = script_getstr(st,2);
  14981. const char *needle = script_getstr(st,3);
  14982. int32 i;
  14983. size_t len;
  14984. if( script_hasdata(st,4) )
  14985. i = script_getnum(st,4);
  14986. else
  14987. i = 0;
  14988. if (needle[0] == '\0') {
  14989. script_pushint(st, -1);
  14990. return SCRIPT_CMD_SUCCESS;
  14991. }
  14992. len = strlen(haystack);
  14993. for ( ; i < len; ++i ) {
  14994. if ( haystack[i] == *needle ) {
  14995. // matched starting char -- loop through remaining chars
  14996. const char *h, *n;
  14997. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14998. if ( *h != *n ) {
  14999. break;
  15000. }
  15001. }
  15002. if ( !*n ) { // matched all of 'needle' to null termination
  15003. script_pushint(st, i);
  15004. return SCRIPT_CMD_SUCCESS;
  15005. }
  15006. }
  15007. }
  15008. script_pushint(st, -1);
  15009. return SCRIPT_CMD_SUCCESS;
  15010. }
  15011. //===============================================================
  15012. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  15013. //
  15014. // Note: Finds all instances of <search> in <input> and replaces
  15015. // with <replace>. If specified will only replace as many
  15016. // instances as specified in <count>. By default will be case
  15017. // sensitive.
  15018. //---------------------------------------------------------------
  15019. BUILDIN_FUNC(replacestr)
  15020. {
  15021. const char *input = script_getstr(st, 2);
  15022. const char *find = script_getstr(st, 3);
  15023. const char *replace = script_getstr(st, 4);
  15024. size_t inputlen = strlen(input);
  15025. size_t findlen = strlen(find);
  15026. struct StringBuf output;
  15027. bool usecase = true;
  15028. int32 count = 0;
  15029. int32 numFinds = 0;
  15030. size_t i = 0, f = 0;
  15031. if(findlen == 0) {
  15032. ShowError("script:replacestr: Invalid search length.\n");
  15033. st->state = END;
  15034. return SCRIPT_CMD_FAILURE;
  15035. }
  15036. if(script_hasdata(st, 5)) {
  15037. if( script_isint(st, 5) )
  15038. usecase = script_getnum(st, 5) != 0;
  15039. else {
  15040. ShowError("script:replacestr: Invalid usecase value. Expected int32 got string\n");
  15041. st->state = END;
  15042. return SCRIPT_CMD_FAILURE;
  15043. }
  15044. }
  15045. if(script_hasdata(st, 6)) {
  15046. count = script_getnum(st, 6);
  15047. if(count == 0) {
  15048. ShowError("script:replacestr: Invalid count value. Expected int32 got string\n");
  15049. st->state = END;
  15050. return SCRIPT_CMD_FAILURE;
  15051. }
  15052. }
  15053. StringBuf_Init(&output);
  15054. for(; i < inputlen; i++) {
  15055. if(count && count == numFinds) { //found enough, stop looking
  15056. break;
  15057. }
  15058. for(f = 0; f <= findlen; f++) {
  15059. if(f == findlen) { //complete match
  15060. numFinds++;
  15061. StringBuf_AppendStr(&output, replace);
  15062. i += findlen - 1;
  15063. break;
  15064. } else {
  15065. if(usecase) {
  15066. if((i + f) > inputlen || input[i + f] != find[f]) {
  15067. StringBuf_Printf(&output, "%c", input[i]);
  15068. break;
  15069. }
  15070. } else {
  15071. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  15072. StringBuf_Printf(&output, "%c", input[i]);
  15073. break;
  15074. }
  15075. }
  15076. }
  15077. }
  15078. }
  15079. //append excess after enough found
  15080. if(i < inputlen)
  15081. StringBuf_AppendStr(&output, &(input[i]));
  15082. script_pushstrcopy(st, StringBuf_Value(&output));
  15083. StringBuf_Destroy(&output);
  15084. return SCRIPT_CMD_SUCCESS;
  15085. }
  15086. //========================================================
  15087. // countstr <input>, <search>{, <usecase>}
  15088. //
  15089. // Note: Counts the number of times <search> occurs in
  15090. // <input>. By default will be case sensitive.
  15091. //--------------------------------------------------------
  15092. BUILDIN_FUNC(countstr)
  15093. {
  15094. const char *input = script_getstr(st, 2);
  15095. const char *find = script_getstr(st, 3);
  15096. size_t inputlen = strlen(input);
  15097. size_t findlen = strlen(find);
  15098. bool usecase = true;
  15099. int32 numFinds = 0;
  15100. if(findlen == 0) {
  15101. ShowError("script:countstr: Invalid search length.\n");
  15102. st->state = END;
  15103. return SCRIPT_CMD_FAILURE;
  15104. }
  15105. if(script_hasdata(st, 4)) {
  15106. if( script_isint(st, 4) )
  15107. usecase = script_getnum(st, 4) != 0;
  15108. else {
  15109. ShowError("script:countstr: Invalid usecase value. Expected int32 got string\n");
  15110. st->state = END;
  15111. return SCRIPT_CMD_FAILURE;
  15112. }
  15113. }
  15114. for( size_t i = 0; i < inputlen; i++ ){
  15115. for( size_t f = 0; f <= findlen; f++ ){
  15116. if(f == findlen) { //complete match
  15117. numFinds++;
  15118. i += findlen - 1;
  15119. break;
  15120. } else {
  15121. if(usecase) {
  15122. if((i + f) > inputlen || input[i + f] != find[f]) {
  15123. break;
  15124. }
  15125. } else {
  15126. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  15127. break;
  15128. }
  15129. }
  15130. }
  15131. }
  15132. }
  15133. script_pushint(st, numFinds);
  15134. return SCRIPT_CMD_SUCCESS;
  15135. }
  15136. /// Changes the display name and/or display class of the npc.
  15137. /// Returns 0 is successful, 1 if the npc does not exist.
  15138. ///
  15139. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  15140. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  15141. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  15142. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  15143. BUILDIN_FUNC(setnpcdisplay)
  15144. {
  15145. const char* name;
  15146. const char* newname = nullptr;
  15147. int32 class_ = JT_FAKENPC, size = -1;
  15148. struct npc_data* nd;
  15149. name = script_getstr(st,2);
  15150. if( script_hasdata(st,4) )
  15151. class_ = script_getnum(st,4);
  15152. if( script_hasdata(st,5) )
  15153. size = script_getnum(st,5);
  15154. if( script_isstring(st, 3) )
  15155. newname = script_getstr(st, 3);
  15156. else
  15157. class_ = script_getnum(st, 3);
  15158. nd = npc_name2id(name);
  15159. if( nd == nullptr )
  15160. {// not found
  15161. script_pushint(st,1);
  15162. return SCRIPT_CMD_SUCCESS;
  15163. }
  15164. // update npc
  15165. if( newname )
  15166. npc_setdisplayname(nd, newname);
  15167. if( size != -1 && size != (int)nd->size )
  15168. nd->size = size;
  15169. else
  15170. size = -1;
  15171. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  15172. npc_setclass(nd, class_);
  15173. else if( size != -1 )
  15174. unit_refresh(&nd->bl); // Required to update the visual size
  15175. script_pushint(st,0);
  15176. return SCRIPT_CMD_SUCCESS;
  15177. }
  15178. BUILDIN_FUNC(atoi)
  15179. {
  15180. const char *value;
  15181. value = script_getstr(st,2);
  15182. script_pushint(st,atoi(value));
  15183. return SCRIPT_CMD_SUCCESS;
  15184. }
  15185. BUILDIN_FUNC(axtoi)
  15186. {
  15187. const char *hex = script_getstr(st,2);
  15188. long value = strtol(hex, nullptr, 16);
  15189. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  15190. value = cap_value(value, INT_MIN, INT_MAX);
  15191. #endif
  15192. script_pushint(st, (int)value);
  15193. return SCRIPT_CMD_SUCCESS;
  15194. }
  15195. BUILDIN_FUNC(strtol)
  15196. {
  15197. const char *string = script_getstr(st, 2);
  15198. int32 base = script_getnum(st, 3);
  15199. long value = strtol(string, nullptr, base);
  15200. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  15201. value = cap_value(value, INT_MIN, INT_MAX);
  15202. #endif
  15203. script_pushint(st, (int)value);
  15204. return SCRIPT_CMD_SUCCESS;
  15205. }
  15206. // case-insensitive substring search [lordalfa]
  15207. BUILDIN_FUNC(compare)
  15208. {
  15209. const char *message;
  15210. const char *cmpstring;
  15211. message = script_getstr(st,2);
  15212. cmpstring = script_getstr(st,3);
  15213. script_pushint(st,(stristr(message,cmpstring) != nullptr));
  15214. return SCRIPT_CMD_SUCCESS;
  15215. }
  15216. BUILDIN_FUNC(strcmp)
  15217. {
  15218. const char *str1;
  15219. const char *str2;
  15220. str1 = script_getstr(st,2);
  15221. str2 = script_getstr(st,3);
  15222. script_pushint(st,strcmp(str1, str2));
  15223. return SCRIPT_CMD_SUCCESS;
  15224. }
  15225. // [zBuffer] List of mathematics commands --->
  15226. BUILDIN_FUNC(sqrt)
  15227. {
  15228. double i, a;
  15229. i = script_getnum(st,2);
  15230. a = sqrt(i);
  15231. script_pushint(st,(int)a);
  15232. return SCRIPT_CMD_SUCCESS;
  15233. }
  15234. BUILDIN_FUNC(pow)
  15235. {
  15236. double i, a, b;
  15237. a = script_getnum(st,2);
  15238. b = script_getnum(st,3);
  15239. i = pow(a,b);
  15240. script_pushint(st,(int)i);
  15241. return SCRIPT_CMD_SUCCESS;
  15242. }
  15243. BUILDIN_FUNC(distance)
  15244. {
  15245. int32 x0, y0, x1, y1;
  15246. x0 = script_getnum(st,2);
  15247. y0 = script_getnum(st,3);
  15248. x1 = script_getnum(st,4);
  15249. y1 = script_getnum(st,5);
  15250. script_pushint(st,distance_xy(x0,y0,x1,y1));
  15251. return SCRIPT_CMD_SUCCESS;
  15252. }
  15253. // <--- [zBuffer] List of mathematics commands
  15254. BUILDIN_FUNC(md5)
  15255. {
  15256. const char *tmpstr;
  15257. char *md5str;
  15258. tmpstr = script_getstr(st,2);
  15259. md5str = (char *)aMalloc((32+1)*sizeof(char));
  15260. MD5_String(tmpstr, md5str);
  15261. script_pushstr(st, md5str);
  15262. return SCRIPT_CMD_SUCCESS;
  15263. }
  15264. // [zBuffer] List of dynamic var commands --->
  15265. /**
  15266. * setd "<variable name>",<value>{,<char_id>};
  15267. **/
  15268. BUILDIN_FUNC(setd)
  15269. {
  15270. TBL_PC *sd = nullptr;
  15271. char varname[100];
  15272. const char *buffer;
  15273. int32 elem;
  15274. buffer = script_getstr(st, 2);
  15275. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15276. elem = 0;
  15277. if( not_server_variable(*varname) ) {
  15278. if (!script_charid2sd(4,sd))
  15279. return SCRIPT_CMD_FAILURE;
  15280. }
  15281. if( is_string_variable(varname) ) {
  15282. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), nullptr );
  15283. } else {
  15284. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), nullptr );
  15285. }
  15286. return SCRIPT_CMD_SUCCESS;
  15287. }
  15288. int32 buildin_query_sql_sub(struct script_state* st, Sql* handle)
  15289. {
  15290. int32 i, j;
  15291. TBL_PC* sd = nullptr;
  15292. const char* query;
  15293. struct script_data* data;
  15294. const char* name;
  15295. uint32 max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  15296. int32 num_vars;
  15297. int32 num_cols;
  15298. // check target variables
  15299. for( i = 3; script_hasdata(st,i); ++i ) {
  15300. data = script_getdata(st, i);
  15301. if( data_isreference(data) ) { // it's a variable
  15302. name = reference_getname(data);
  15303. if( not_server_variable(*name) && sd == nullptr ) { // requires a player
  15304. if( !script_rid2sd(sd) ) { // no player attached
  15305. script_reportdata(data);
  15306. st->state = END;
  15307. return SCRIPT_CMD_FAILURE;
  15308. }
  15309. }
  15310. } else {
  15311. ShowError("script:query_sql: not a variable\n");
  15312. script_reportdata(data);
  15313. st->state = END;
  15314. return SCRIPT_CMD_FAILURE;
  15315. }
  15316. }
  15317. num_vars = i - 3;
  15318. // Execute the query
  15319. query = script_getstr(st,2);
  15320. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  15321. Sql_ShowDebug(handle);
  15322. script_pushint(st, -1);
  15323. return SCRIPT_CMD_FAILURE;
  15324. }
  15325. if( Sql_NumRows(handle) == 0 ) { // No data received
  15326. Sql_FreeResult(handle);
  15327. script_pushint(st, 0);
  15328. return SCRIPT_CMD_SUCCESS;
  15329. }
  15330. // Count the number of columns to store
  15331. num_cols = Sql_NumColumns(handle);
  15332. if( num_vars < num_cols ) {
  15333. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (uint32)(num_cols-num_vars));
  15334. script_reportsrc(st);
  15335. } else if( num_vars > num_cols ) {
  15336. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (uint32)(num_vars-num_cols));
  15337. script_reportsrc(st);
  15338. }
  15339. // Store data
  15340. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  15341. for( j = 0; j < num_vars; ++j ) {
  15342. char* str = nullptr;
  15343. if( j < num_cols )
  15344. Sql_GetData(handle, j, &str, nullptr);
  15345. data = script_getdata(st, j+3);
  15346. name = reference_getname(data);
  15347. if( is_string_variable(name) )
  15348. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  15349. else
  15350. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  15351. }
  15352. }
  15353. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  15354. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (uint32)Sql_NumRows(handle));
  15355. script_reportsrc(st);
  15356. }
  15357. // Free data
  15358. Sql_FreeResult(handle);
  15359. script_pushint(st, i);
  15360. return SCRIPT_CMD_SUCCESS;
  15361. }
  15362. BUILDIN_FUNC(query_sql) {
  15363. return buildin_query_sql_sub(st, qsmysql_handle);
  15364. }
  15365. BUILDIN_FUNC(query_logsql) {
  15366. if( !log_config.sql_logs ) {// logmysql_handle == nullptr
  15367. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  15368. script_pushint(st,-1);
  15369. return SCRIPT_CMD_FAILURE;
  15370. }
  15371. return buildin_query_sql_sub(st, logmysql_handle);
  15372. }
  15373. //Allows escaping of a given string.
  15374. BUILDIN_FUNC(escape_sql)
  15375. {
  15376. const char *str;
  15377. char *esc_str;
  15378. size_t len;
  15379. str = script_getstr(st,2);
  15380. len = strlen(str);
  15381. esc_str = (char*)aMalloc(len*2+1);
  15382. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  15383. script_pushstr(st, esc_str);
  15384. return SCRIPT_CMD_SUCCESS;
  15385. }
  15386. BUILDIN_FUNC(getd)
  15387. {
  15388. char varname[100];
  15389. const char *buffer;
  15390. int32 elem;
  15391. buffer = script_getstr(st, 2);
  15392. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15393. elem = 0;
  15394. // Push the 'pointer' so it's more flexible [Lance]
  15395. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  15396. return SCRIPT_CMD_SUCCESS;
  15397. }
  15398. BUILDIN_FUNC(callshop)
  15399. {
  15400. TBL_PC *sd = nullptr;
  15401. struct npc_data *nd;
  15402. const char *shopname;
  15403. int32 flag = 0;
  15404. if (!script_rid2sd(sd)) {
  15405. script_pushint(st,0);
  15406. return SCRIPT_CMD_SUCCESS;
  15407. }
  15408. shopname = script_getstr(st, 2);
  15409. if (script_hasdata(st,3))
  15410. flag = script_getnum(st,3);
  15411. nd = npc_name2id(shopname);
  15412. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP && nd->subtype != NPCTYPE_BARTER) ) {
  15413. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  15414. script_pushint(st,0);
  15415. return SCRIPT_CMD_FAILURE;
  15416. }
  15417. if (nd->subtype == NPCTYPE_SHOP) {
  15418. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15419. sd->state.callshop = 1;
  15420. switch (flag) {
  15421. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  15422. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  15423. default: clif_npcbuysell( *sd, *nd ); break; //Show menu
  15424. }
  15425. }
  15426. #if PACKETVER >= 20131223
  15427. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15428. unsigned short i;
  15429. for (i = 0; i < nd->u.shop.count; i++) {
  15430. if (nd->u.shop.shop_item[i].qty)
  15431. break;
  15432. }
  15433. if (i == nd->u.shop.count) {
  15434. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  15435. return SCRIPT_CMD_FAILURE;
  15436. }
  15437. sd->npc_shopid = nd->bl.id;
  15438. clif_npc_market_open( *sd, *nd );
  15439. script_pushint(st,1);
  15440. return SCRIPT_CMD_SUCCESS;
  15441. }
  15442. #endif
  15443. else if( nd->subtype == NPCTYPE_BARTER ){
  15444. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15445. sd->state.callshop = 1;
  15446. if( nd->u.barter.extended ){
  15447. clif_barter_extended_open( *sd, *nd );
  15448. }else{
  15449. clif_barter_open( *sd, *nd );
  15450. }
  15451. }else
  15452. clif_cashshop_show( *sd, *nd );
  15453. sd->npc_shopid = nd->bl.id;
  15454. script_pushint(st,1);
  15455. return SCRIPT_CMD_SUCCESS;
  15456. }
  15457. BUILDIN_FUNC(npcshopitem)
  15458. {
  15459. const char* npcname = script_getstr(st, 2);
  15460. struct npc_data* nd = npc_name2id(npcname);
  15461. int32 n, i;
  15462. int32 amount;
  15463. uint16 offs = 2;
  15464. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  15465. script_pushint(st,0);
  15466. return SCRIPT_CMD_SUCCESS;
  15467. }
  15468. #if PACKETVER >= 20131223
  15469. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15470. offs = 3;
  15471. npc_market_delfromsql_(nd->exname, 0, true);
  15472. }
  15473. #endif
  15474. // get the count of new entries
  15475. amount = (script_lastdata(st)-2)/offs;
  15476. // generate new shop item list
  15477. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  15478. nd->u.shop.count = 0;
  15479. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  15480. t_itemid nameid = script_getnum( st, i );
  15481. std::shared_ptr<item_data> id = item_db.find(nameid);
  15482. if( !id ){
  15483. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  15484. script_pushint( st, 0 );
  15485. return SCRIPT_CMD_FAILURE;
  15486. }
  15487. int32 price = script_getnum(st, i + 1);
  15488. if (price < 0) {
  15489. if (nd->subtype == NPCTYPE_CASHSHOP || nd->subtype == NPCTYPE_POINTSHOP || nd->subtype == NPCTYPE_ITEMSHOP) {
  15490. ShowError("builtin_npcshopitem: Invalid price in shop '%s'.\n", nd->exname);
  15491. script_pushint(st, 0);
  15492. return SCRIPT_CMD_FAILURE;
  15493. }
  15494. price = id->value_buy;
  15495. }
  15496. nd->u.shop.shop_item[n].nameid = nameid;
  15497. nd->u.shop.shop_item[n].value = price;
  15498. #if PACKETVER >= 20131223
  15499. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15500. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  15501. nd->u.shop.shop_item[n].flag = 1;
  15502. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  15503. }
  15504. #endif
  15505. nd->u.shop.count++;
  15506. }
  15507. script_pushint(st,1);
  15508. return SCRIPT_CMD_SUCCESS;
  15509. }
  15510. BUILDIN_FUNC(npcshopadditem)
  15511. {
  15512. const char* npcname = script_getstr(st,2);
  15513. struct npc_data* nd = npc_name2id(npcname);
  15514. uint16 offs = 2, amount;
  15515. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15516. script_pushint(st,0);
  15517. return SCRIPT_CMD_SUCCESS;
  15518. }
  15519. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15520. offs = 3;
  15521. // get the count of new entries
  15522. amount = (script_lastdata(st)-2)/offs;
  15523. #if PACKETVER >= 20131223
  15524. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15525. for (int32 n = 0, i = 3; n < amount; n++, i += offs) {
  15526. t_itemid nameid = script_getnum(st,i);
  15527. uint16 j;
  15528. std::shared_ptr<item_data> id = item_db.find(nameid);
  15529. if( !id ){
  15530. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15531. script_pushint( st, 0 );
  15532. return SCRIPT_CMD_FAILURE;
  15533. }
  15534. // Check existing entries
  15535. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  15536. if (j == nd->u.shop.count) {
  15537. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  15538. j = nd->u.shop.count;
  15539. nd->u.shop.shop_item[j].flag = 1;
  15540. nd->u.shop.count++;
  15541. }
  15542. int32 price = script_getnum(st, i + 1);
  15543. if (price < 0) {
  15544. price = id->value_buy;
  15545. }
  15546. int32 stock = script_getnum(st, i + 2);
  15547. if( stock < -1 ){
  15548. ShowError( "builtin_npcshopadditem: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  15549. script_pushint( st, 0 );
  15550. return SCRIPT_CMD_FAILURE;
  15551. }
  15552. nd->u.shop.shop_item[j].nameid = nameid;
  15553. nd->u.shop.shop_item[j].value = price;
  15554. nd->u.shop.shop_item[j].qty = stock;
  15555. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  15556. }
  15557. script_pushint(st,1);
  15558. return SCRIPT_CMD_SUCCESS;
  15559. }
  15560. #endif
  15561. // append new items to existing shop item list
  15562. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  15563. for (int32 n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  15564. {
  15565. t_itemid nameid = script_getnum( st, i );
  15566. std::shared_ptr<item_data> id = item_db.find(nameid);
  15567. if( !id ){
  15568. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15569. script_pushint( st, 0 );
  15570. return SCRIPT_CMD_FAILURE;
  15571. }
  15572. int32 price = script_getnum(st, i + 1);
  15573. if (price < 0) {
  15574. if (nd->subtype == NPCTYPE_CASHSHOP || nd->subtype == NPCTYPE_POINTSHOP || nd->subtype == NPCTYPE_ITEMSHOP) {
  15575. ShowError("builtin_npcshopadditem: Invalid price in shop '%s'.\n", nd->exname);
  15576. script_pushint(st, 0);
  15577. return SCRIPT_CMD_FAILURE;
  15578. }
  15579. price = id->value_buy;
  15580. }
  15581. nd->u.shop.shop_item[n].nameid = nameid;
  15582. nd->u.shop.shop_item[n].value = price;
  15583. nd->u.shop.count++;
  15584. }
  15585. script_pushint(st,1);
  15586. return SCRIPT_CMD_SUCCESS;
  15587. }
  15588. BUILDIN_FUNC(npcshopdelitem)
  15589. {
  15590. const char* npcname = script_getstr(st,2);
  15591. struct npc_data* nd = npc_name2id(npcname);
  15592. int32 n, i, size;
  15593. unsigned short amount;
  15594. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15595. script_pushint(st,0);
  15596. return SCRIPT_CMD_SUCCESS;
  15597. }
  15598. amount = script_lastdata(st)-2;
  15599. size = nd->u.shop.count;
  15600. // remove specified items from the shop item list
  15601. for( i = 3; i < 3 + amount; i++ ) {
  15602. t_itemid nameid = script_getnum(st,i);
  15603. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  15604. if( n < size ) {
  15605. if (n+1 != size)
  15606. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-(n + 1)));
  15607. #if PACKETVER >= 20131223
  15608. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15609. npc_market_delfromsql_(nd->exname, nameid, false);
  15610. #endif
  15611. size--;
  15612. }
  15613. }
  15614. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  15615. nd->u.shop.count = size;
  15616. script_pushint(st,1);
  15617. return SCRIPT_CMD_SUCCESS;
  15618. }
  15619. /**
  15620. * Sets a script to attach to a shop npc.
  15621. * npcshopattach "<npc_name>";
  15622. **/
  15623. BUILDIN_FUNC(npcshopattach)
  15624. {
  15625. const char* npcname = script_getstr(st,2);
  15626. struct npc_data* nd = npc_name2id(npcname);
  15627. int32 flag = 1;
  15628. if( script_hasdata(st,3) )
  15629. flag = script_getnum(st,3);
  15630. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15631. script_pushint(st,0);
  15632. return SCRIPT_CMD_SUCCESS;
  15633. }
  15634. if (flag)
  15635. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  15636. else
  15637. nd->master_nd = nullptr;
  15638. script_pushint(st,1);
  15639. return SCRIPT_CMD_SUCCESS;
  15640. }
  15641. /*==========================================
  15642. * Returns some values of an item [Lupus]
  15643. * Price, Weight, etc...
  15644. setitemscript(itemID,"{new item bonus script}",[n]);
  15645. Where n:
  15646. 0 - script
  15647. 1 - Equip script
  15648. 2 - Unequip script
  15649. *------------------------------------------*/
  15650. BUILDIN_FUNC(setitemscript)
  15651. {
  15652. int32 n = 0;
  15653. const char *script;
  15654. struct script_code **dstscript;
  15655. t_itemid item_id = script_getnum(st,2);
  15656. script = script_getstr(st,3);
  15657. if( script_hasdata(st,4) )
  15658. n=script_getnum(st,4);
  15659. std::shared_ptr<item_data> i_data = item_db.find(item_id);
  15660. if (!i_data || script==nullptr || ( script[0] && script[0]!='{' )) {
  15661. script_pushint(st,0);
  15662. return SCRIPT_CMD_SUCCESS;
  15663. }
  15664. switch (n) {
  15665. case 2:
  15666. dstscript = &i_data->unequip_script;
  15667. break;
  15668. case 1:
  15669. dstscript = &i_data->equip_script;
  15670. break;
  15671. default:
  15672. dstscript = &i_data->script;
  15673. break;
  15674. }
  15675. if(*dstscript)
  15676. script_free_code(*dstscript);
  15677. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : nullptr;
  15678. script_pushint(st,1);
  15679. return SCRIPT_CMD_SUCCESS;
  15680. }
  15681. /*=======================================================
  15682. * Add or Update a mob drop temporarily [Akinari]
  15683. * Original Idea By: [Lupus]
  15684. *
  15685. * addmonsterdrop <mob_id or name>,<item_id>,<rate>,{<steal protected>,{<random option group id>}};
  15686. *
  15687. * If given an item the mob already drops, the rate
  15688. * is updated to the new rate. Rate range is 1-10000.
  15689. * Returns true if succeeded (added/updated a mob drop) false otherwise.
  15690. *-------------------------------------------------------*/
  15691. BUILDIN_FUNC(addmonsterdrop)
  15692. {
  15693. std::shared_ptr<s_mob_db> mob;
  15694. if (script_isstring(st, 2))
  15695. mob = mob_db.find(mobdb_searchname(script_getstr(st, 2)));
  15696. else
  15697. mob = mob_db.find(script_getnum(st, 2));
  15698. if (mob == nullptr) {
  15699. if (script_isstring(st, 2))
  15700. ShowError("addmonsterdrop: bad mob name given %s\n", script_getstr(st, 2));
  15701. else
  15702. ShowError("addmonsterdrop: bad mob id given %d\n", script_getnum(st, 2));
  15703. script_pushint(st, false);
  15704. return SCRIPT_CMD_FAILURE;
  15705. }
  15706. t_itemid item_id = script_getnum(st, 3);
  15707. std::shared_ptr<item_data> itm = item_db.find(item_id);
  15708. if (itm == nullptr) {
  15709. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id);
  15710. script_pushint(st, false);
  15711. return SCRIPT_CMD_FAILURE;
  15712. }
  15713. int32 rate = script_getnum(st, 4);
  15714. if (rate < 1 || rate > 10000) {
  15715. ShowError("addmonsterdrop: Invalid rate %d (min 1, max 10000).\n", rate);
  15716. script_pushint(st, false);
  15717. return SCRIPT_CMD_FAILURE;
  15718. }
  15719. uint16 c = 0;
  15720. for (uint16 i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15721. if (mob->dropitem[i].nameid > 0) {
  15722. if (mob->dropitem[i].nameid == item_id) { // If it equals item_id we update that drop
  15723. c = i;
  15724. break;
  15725. }
  15726. continue;
  15727. }
  15728. if (c == 0) // Accept first available slot only
  15729. c = i;
  15730. }
  15731. if (c == 0) { // No place to put the new drop
  15732. script_pushint(st, false);
  15733. return SCRIPT_CMD_SUCCESS;
  15734. }
  15735. int32 steal_protected = 0;
  15736. int32 group = 0;
  15737. if (script_hasdata(st,5))
  15738. steal_protected = script_getnum(st, 5);
  15739. if (script_hasdata(st,6)) {
  15740. group = script_getnum(st, 6);
  15741. if (!random_option_group.exists(group)) {
  15742. ShowError("buildin_addmonsterdrop: Unknown random option group %d.\n", group);
  15743. script_pushint(st, false);
  15744. return SCRIPT_CMD_FAILURE;
  15745. }
  15746. if (itm->type != IT_WEAPON && itm->type != IT_ARMOR && itm->type != IT_SHADOWGEAR) {
  15747. ShowError("buildin_addmonsterdrop: Random option group can't be used with this type of item (item Id: %d).\n", item_id);
  15748. script_pushint(st, false);
  15749. return SCRIPT_CMD_FAILURE;
  15750. }
  15751. }
  15752. // Fill in the slot with the item and rate
  15753. mob->dropitem[c].nameid = item_id;
  15754. mob->dropitem[c].rate = rate;
  15755. mob->dropitem[c].steal_protected = steal_protected > 0;
  15756. mob->dropitem[c].randomopt_group = group;
  15757. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15758. script_pushint(st, true);
  15759. return SCRIPT_CMD_SUCCESS;
  15760. }
  15761. /*=======================================================
  15762. * Delete a mob drop temporarily [Akinari]
  15763. * Original Idea By: [Lupus]
  15764. *
  15765. * delmonsterdrop <mob_id or name>,<item_id>;
  15766. *
  15767. * Returns 1 if succeeded (deleted a mob drop)
  15768. *-------------------------------------------------------*/
  15769. BUILDIN_FUNC(delmonsterdrop)
  15770. {
  15771. std::shared_ptr<s_mob_db> mob;
  15772. if(script_isstring(st, 2))
  15773. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15774. else
  15775. mob = mob_db.find(script_getnum(st,2));
  15776. t_itemid item_id=script_getnum(st,3);
  15777. if(!item_db.exists(item_id)){
  15778. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15779. return SCRIPT_CMD_FAILURE;
  15780. }
  15781. if(mob) { //We got a valid monster, check for item drop on monster
  15782. unsigned char i;
  15783. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15784. if(mob->dropitem[i].nameid == item_id) {
  15785. mob->dropitem[i].nameid = 0;
  15786. mob->dropitem[i].rate = 0;
  15787. mob->dropitem[i].steal_protected = false;
  15788. mob->dropitem[i].randomopt_group = 0;
  15789. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15790. script_pushint(st,1);
  15791. return SCRIPT_CMD_SUCCESS;
  15792. }
  15793. }
  15794. //No drop on that monster
  15795. script_pushint(st,0);
  15796. } else {
  15797. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15798. return SCRIPT_CMD_FAILURE;
  15799. }
  15800. return SCRIPT_CMD_SUCCESS;
  15801. }
  15802. /*==========================================
  15803. * Returns a random mob_id
  15804. * type: Where to fetch from (see enum e_random_monster)
  15805. * flag: Type of checks to apply (see enum e_random_monster_flags)
  15806. * lv: Mob level to check against
  15807. *------------------------------------------*/
  15808. BUILDIN_FUNC(getrandmobid)
  15809. {
  15810. int32 type = script_getnum(st, 2);
  15811. if (type < MOBG_BRANCH_OF_DEAD_TREE || type >= MOBG_MAX) {
  15812. ShowWarning("buildin_getrandmobid: Invalid type %d.\n", type);
  15813. script_pushint(st, 0);
  15814. return SCRIPT_CMD_FAILURE;
  15815. }
  15816. int32 flag = script_hasdata(st, 3) ? script_getnum(st, 3) : RMF_MOB_NOT_BOSS;
  15817. if (flag < RMF_NONE || flag > RMF_ALL) {
  15818. ShowWarning("buildin_getrandmobid: Invalid flag %d.\n", flag);
  15819. script_pushint(st, 0);
  15820. return SCRIPT_CMD_FAILURE;
  15821. }
  15822. int32 lv;
  15823. if ( script_hasdata(st, 4) ) {
  15824. lv = script_getnum(st, 4);
  15825. if (lv <= 0) {
  15826. ShowWarning("buildin_getrandmobid: Invalid level %d.\n", lv);
  15827. script_pushint(st, 0);
  15828. return SCRIPT_CMD_FAILURE;
  15829. }
  15830. // If a level is provided, make sure it is respected
  15831. flag |= RMF_CHECK_MOB_LV;
  15832. } else {
  15833. lv = MAX_LEVEL;
  15834. }
  15835. script_pushint(st, mob_get_random_id(type, (enum e_random_monster_flags)flag, lv));
  15836. return SCRIPT_CMD_SUCCESS;
  15837. }
  15838. /*==========================================
  15839. * Returns some values of a monster [Lupus]
  15840. * Name, Level, race, size, etc...
  15841. getmonsterinfo(monsterID,queryIndex);
  15842. getmonsterinfo(monsterName,queryIndex);
  15843. *------------------------------------------*/
  15844. BUILDIN_FUNC(getmonsterinfo)
  15845. {
  15846. std::shared_ptr<s_mob_db> mob = nullptr;
  15847. if (script_isstring(st, 2))
  15848. mob = mobdb_search_aegisname(script_getstr(st, 2));
  15849. else {
  15850. uint16 mob_id = script_getnum(st, 2);
  15851. if (!mob_is_clone(mob_id)) {
  15852. mob = mob_db.find(mob_id);
  15853. }
  15854. }
  15855. if (mob == nullptr) {
  15856. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15857. if (script_getnum(st, 3) == MOB_NAME) // requested the name
  15858. script_pushconststr(st, "null");
  15859. else
  15860. script_pushint(st, -1);
  15861. return SCRIPT_CMD_SUCCESS;
  15862. }
  15863. switch ( script_getnum(st, 3) ) {
  15864. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15865. case MOB_LV: script_pushint(st,mob->lv); break;
  15866. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15867. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15868. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15869. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15870. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15871. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15872. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15873. case MOB_RES: script_pushint(st, mob->status.res); break;
  15874. case MOB_MRES: script_pushint(st, mob->status.mres); break;
  15875. case MOB_STR: script_pushint(st,mob->status.str); break;
  15876. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15877. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15878. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15879. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15880. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15881. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15882. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15883. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15884. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15885. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15886. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15887. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15888. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15889. case MOB_ID: script_pushint(st,mob->id); break;
  15890. default: script_pushint(st,-1); //wrong Index
  15891. }
  15892. return SCRIPT_CMD_SUCCESS;
  15893. }
  15894. /**
  15895. * Check player's vending/buyingstore/autotrade state
  15896. * checkvending({"<Player Name>"})
  15897. * @author [Nab4]
  15898. */
  15899. BUILDIN_FUNC(checkvending) {
  15900. TBL_PC *sd = nullptr;
  15901. if (!script_nick2sd(2,sd) ) {
  15902. script_pushint(st,0);
  15903. return SCRIPT_CMD_SUCCESS;
  15904. }
  15905. else {
  15906. int8 ret = 0;
  15907. if (sd->state.vending)
  15908. ret = 1;
  15909. else if (sd->state.buyingstore)
  15910. ret = 4;
  15911. if (sd->state.autotrade)
  15912. ret |= 2;
  15913. script_pushint(st, ret);
  15914. }
  15915. return SCRIPT_CMD_SUCCESS;
  15916. }
  15917. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15918. {
  15919. TBL_PC *sd = nullptr;
  15920. if( script_nick2sd(2,sd) )
  15921. script_pushint(st,(sd->chatID != 0));
  15922. else
  15923. script_pushint(st,0);
  15924. return SCRIPT_CMD_SUCCESS;
  15925. }
  15926. BUILDIN_FUNC(checkidle)
  15927. {
  15928. TBL_PC *sd = nullptr;
  15929. if( script_nick2sd(2,sd) )
  15930. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15931. else
  15932. script_pushint(st, 0);
  15933. return SCRIPT_CMD_SUCCESS;
  15934. }
  15935. BUILDIN_FUNC(checkidlehom)
  15936. {
  15937. TBL_PC *sd = nullptr;
  15938. if( script_nick2sd(2,sd) )
  15939. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15940. else
  15941. script_pushint(st, 0);
  15942. return SCRIPT_CMD_SUCCESS;
  15943. }
  15944. BUILDIN_FUNC(checkidlemer)
  15945. {
  15946. TBL_PC *sd;
  15947. if( script_nick2sd(2,sd) )
  15948. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_mer));
  15949. else
  15950. script_pushint(st, 0);
  15951. return SCRIPT_CMD_SUCCESS;
  15952. }
  15953. BUILDIN_FUNC(searchitem)
  15954. {
  15955. script_data* data = script_getdata(st, 2);
  15956. const char *name = reference_getname(data);
  15957. if( !data_isreference(data) )
  15958. {
  15959. ShowError("buildin_searchitem: Argument %s is not a variable.\n", name);
  15960. script_reportdata(data);
  15961. st->state = END;
  15962. return SCRIPT_CMD_FAILURE;// not a variable
  15963. }
  15964. if( is_string_variable(name) )
  15965. {// string array
  15966. ShowError("buildin_searchitem: Argument %s is not an integer array.\n", name);
  15967. script_reportdata(data);
  15968. st->state = END;
  15969. return SCRIPT_CMD_FAILURE;// not supported
  15970. }
  15971. map_session_data *sd = nullptr;
  15972. if (not_server_variable(*name) && !script_rid2sd(sd))
  15973. {
  15974. return SCRIPT_CMD_SUCCESS;// no player attached
  15975. }
  15976. const char *itemname = script_getstr(st, 3);
  15977. std::map<t_itemid, std::shared_ptr<item_data>> items;
  15978. itemdb_searchname_array(items, MAX_SEARCH, itemname);
  15979. if (!items.empty()) {
  15980. int32 id = reference_getid(data);
  15981. int32 start = reference_getindex(data);
  15982. for (const auto &it : items) { // Set array
  15983. set_reg_num(st, sd, reference_uid(id, start), name, it.first, reference_getref(data));
  15984. start++;
  15985. }
  15986. }
  15987. script_pushint64(st, items.size());
  15988. return SCRIPT_CMD_SUCCESS;
  15989. }
  15990. // [zBuffer] List of player cont commands --->
  15991. BUILDIN_FUNC(rid2name)
  15992. {
  15993. struct block_list *bl = nullptr;
  15994. int32 rid = script_getnum(st,2);
  15995. if((bl = map_id2bl(rid)))
  15996. {
  15997. switch(bl->type) {
  15998. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15999. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  16000. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  16001. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  16002. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  16003. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name.c_str()); break;
  16004. default:
  16005. ShowError("buildin_rid2name: BL type unknown.\n");
  16006. script_pushconststr(st,"");
  16007. break;
  16008. }
  16009. } else {
  16010. ShowError("buildin_rid2name: invalid RID\n");
  16011. script_pushconststr(st,"(null)");
  16012. }
  16013. return SCRIPT_CMD_SUCCESS;
  16014. }
  16015. /**
  16016. * Toggle a unit from moving.
  16017. * pcblockmove(<unit_id>,<option>);
  16018. */
  16019. BUILDIN_FUNC(pcblockmove)
  16020. {
  16021. struct block_list *bl = nullptr;
  16022. if (script_getnum(st, 2))
  16023. bl = map_id2bl(script_getnum(st,2));
  16024. else
  16025. bl = map_id2bl(st->rid);
  16026. if (bl) {
  16027. struct unit_data *ud = unit_bl2ud(bl);
  16028. if (ud)
  16029. ud->state.blockedmove = script_getnum(st,3) > 0;
  16030. }
  16031. return SCRIPT_CMD_SUCCESS;
  16032. }
  16033. /**
  16034. * Toggle a unit from casting skills.
  16035. * pcblockskill(<unit_id>,<option>);
  16036. */
  16037. BUILDIN_FUNC(pcblockskill)
  16038. {
  16039. struct block_list *bl = nullptr;
  16040. if (script_getnum(st, 2))
  16041. bl = map_id2bl(script_getnum(st,2));
  16042. else
  16043. bl = map_id2bl(st->rid);
  16044. if (bl) {
  16045. struct unit_data *ud = unit_bl2ud(bl);
  16046. if (ud)
  16047. ud->state.blockedskill = script_getnum(st, 3) > 0;
  16048. }
  16049. return SCRIPT_CMD_SUCCESS;
  16050. }
  16051. BUILDIN_FUNC(setpcblock)
  16052. {
  16053. TBL_PC *sd;
  16054. if (script_mapid2sd(4, sd)) {
  16055. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  16056. if (script_getnum(st, 3) > 0)
  16057. sd->state.block_action |= type;
  16058. else
  16059. sd->state.block_action &= ~type;
  16060. }
  16061. return SCRIPT_CMD_SUCCESS;
  16062. }
  16063. BUILDIN_FUNC(getpcblock)
  16064. {
  16065. TBL_PC *sd;
  16066. if (script_mapid2sd(2, sd))
  16067. script_pushint(st, sd->state.block_action);
  16068. else
  16069. script_pushint(st, 0);
  16070. return SCRIPT_CMD_SUCCESS;
  16071. }
  16072. BUILDIN_FUNC(pcfollow)
  16073. {
  16074. TBL_PC *sd;
  16075. if( script_mapid2sd(2,sd) )
  16076. pc_follow(sd, script_getnum(st,3));
  16077. return SCRIPT_CMD_SUCCESS;
  16078. }
  16079. BUILDIN_FUNC(pcstopfollow)
  16080. {
  16081. TBL_PC *sd;
  16082. if(script_mapid2sd(2,sd))
  16083. pc_stop_following(sd);
  16084. return SCRIPT_CMD_SUCCESS;
  16085. }
  16086. // <--- [zBuffer] List of player cont commands
  16087. // [zBuffer] List of unit control commands --->
  16088. /// Checks to see if the unit exists.
  16089. ///
  16090. /// unitexists <unit id>;
  16091. BUILDIN_FUNC(unitexists)
  16092. {
  16093. struct block_list* bl;
  16094. bl = map_id2bl(script_getnum(st, 2));
  16095. if (!bl)
  16096. script_pushint(st, false);
  16097. else
  16098. script_pushint(st, true);
  16099. return SCRIPT_CMD_SUCCESS;
  16100. }
  16101. /// Gets the type of the given Game ID.
  16102. ///
  16103. /// getunittype <unit id>;
  16104. BUILDIN_FUNC(getunittype)
  16105. {
  16106. struct block_list* bl;
  16107. int32 value = 0;
  16108. if(!script_rid2bl(2,bl))
  16109. {
  16110. script_pushint(st, -1);
  16111. return SCRIPT_CMD_FAILURE;
  16112. }
  16113. switch (bl->type) {
  16114. case BL_PC:
  16115. case BL_NPC:
  16116. case BL_PET:
  16117. case BL_MOB:
  16118. case BL_HOM:
  16119. case BL_MER:
  16120. case BL_ELEM:
  16121. value = bl->type;
  16122. break;
  16123. default:
  16124. value = -1;
  16125. break;
  16126. }
  16127. script_pushint(st, value);
  16128. return SCRIPT_CMD_SUCCESS;
  16129. }
  16130. /// Gets specific live information of a bl.
  16131. ///
  16132. /// getunitdata <unit id>,<arrayname>;
  16133. BUILDIN_FUNC(getunitdata)
  16134. {
  16135. map_session_data* sd;
  16136. struct block_list* bl;
  16137. if(!script_rid2bl(2,bl))
  16138. {
  16139. script_pushint(st, -1);
  16140. return SCRIPT_CMD_FAILURE;
  16141. }
  16142. struct script_data *data = script_getdata(st, 3);
  16143. if (!data_isreference(data)) {
  16144. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  16145. return SCRIPT_CMD_FAILURE;
  16146. }
  16147. char* name = reference_getname(data);
  16148. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  16149. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  16150. return SCRIPT_CMD_FAILURE;
  16151. }else{
  16152. sd = nullptr;
  16153. }
  16154. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  16155. switch(bl->type) {
  16156. case BL_MOB: {
  16157. mob_data* md = reinterpret_cast<mob_data*>( bl );
  16158. getunitdata_sub(UMOB_SIZE, md->status.size);
  16159. getunitdata_sub(UMOB_LEVEL, md->level);
  16160. getunitdata_sub(UMOB_HP, md->status.hp);
  16161. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  16162. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  16163. getunitdata_sub(UMOB_MAPID, md->bl.m);
  16164. getunitdata_sub(UMOB_X, md->bl.x);
  16165. getunitdata_sub(UMOB_Y, md->bl.y);
  16166. getunitdata_sub(UMOB_SPEED, md->status.speed);
  16167. getunitdata_sub(UMOB_MODE, md->status.mode);
  16168. getunitdata_sub(UMOB_AI, md->special_state.ai);
  16169. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  16170. getunitdata_sub(UMOB_SEX, md->vd->sex);
  16171. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  16172. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  16173. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  16174. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  16175. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  16176. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  16177. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  16178. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  16179. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  16180. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  16181. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  16182. getunitdata_sub(UMOB_STR, md->status.str);
  16183. getunitdata_sub(UMOB_AGI, md->status.agi);
  16184. getunitdata_sub(UMOB_VIT, md->status.vit);
  16185. getunitdata_sub(UMOB_INT, md->status.int_);
  16186. getunitdata_sub(UMOB_DEX, md->status.dex);
  16187. getunitdata_sub(UMOB_LUK, md->status.luk);
  16188. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  16189. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  16190. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  16191. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  16192. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  16193. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  16194. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  16195. getunitdata_sub(UMOB_DEF, md->status.def);
  16196. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  16197. getunitdata_sub(UMOB_HIT, md->status.hit);
  16198. getunitdata_sub(UMOB_FLEE, md->status.flee);
  16199. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  16200. getunitdata_sub(UMOB_CRIT, md->status.cri);
  16201. getunitdata_sub(UMOB_RACE, md->status.race);
  16202. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  16203. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  16204. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  16205. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  16206. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  16207. getunitdata_sub(UMOB_TARGETID, md->target_id);
  16208. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  16209. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  16210. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  16211. getunitdata_sub(UMOB_IGNORE_CELL_STACK_LIMIT, md->ud.state.ignore_cell_stack_limit);
  16212. getunitdata_sub(UMOB_RES, md->status.res);
  16213. getunitdata_sub(UMOB_MRES, md->status.mres);
  16214. getunitdata_sub(UMOB_DAMAGETAKEN, md->damagetaken);
  16215. } break;
  16216. case BL_HOM: {
  16217. homun_data* hd = reinterpret_cast<homun_data*>( bl );
  16218. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  16219. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  16220. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  16221. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  16222. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  16223. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  16224. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  16225. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  16226. getunitdata_sub(UHOM_X, hd->bl.x);
  16227. getunitdata_sub(UHOM_Y, hd->bl.y);
  16228. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  16229. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  16230. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  16231. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  16232. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  16233. getunitdata_sub(UHOM_STR, hd->base_status.str);
  16234. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  16235. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  16236. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  16237. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  16238. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  16239. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  16240. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  16241. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  16242. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  16243. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  16244. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  16245. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  16246. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  16247. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  16248. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  16249. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  16250. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  16251. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  16252. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  16253. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  16254. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  16255. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  16256. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  16257. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  16258. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  16259. } break;
  16260. case BL_PET: {
  16261. pet_data* pd = reinterpret_cast<pet_data*>( bl );
  16262. getunitdata_sub(UPET_SIZE, pd->status.size);
  16263. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  16264. getunitdata_sub(UPET_HP, pd->status.hp);
  16265. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  16266. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  16267. getunitdata_sub(UPET_MAPID, pd->bl.m);
  16268. getunitdata_sub(UPET_X, pd->bl.x);
  16269. getunitdata_sub(UPET_Y, pd->bl.y);
  16270. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  16271. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  16272. getunitdata_sub(UPET_SPEED, pd->status.speed);
  16273. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  16274. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  16275. getunitdata_sub(UPET_STR, pd->status.str);
  16276. getunitdata_sub(UPET_AGI, pd->status.agi);
  16277. getunitdata_sub(UPET_VIT, pd->status.vit);
  16278. getunitdata_sub(UPET_INT, pd->status.int_);
  16279. getunitdata_sub(UPET_DEX, pd->status.dex);
  16280. getunitdata_sub(UPET_LUK, pd->status.luk);
  16281. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  16282. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  16283. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  16284. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  16285. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  16286. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  16287. getunitdata_sub(UPET_DEF, pd->status.def);
  16288. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  16289. getunitdata_sub(UPET_HIT, pd->status.hit);
  16290. getunitdata_sub(UPET_FLEE, pd->status.flee);
  16291. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  16292. getunitdata_sub(UPET_CRIT, pd->status.cri);
  16293. getunitdata_sub(UPET_RACE, pd->status.race);
  16294. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  16295. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  16296. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  16297. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  16298. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  16299. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  16300. } break;
  16301. case BL_MER: {
  16302. s_mercenary_data* mc = reinterpret_cast<s_mercenary_data*>( bl );
  16303. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  16304. getunitdata_sub(UMER_HP, mc->base_status.hp);
  16305. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  16306. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  16307. getunitdata_sub(UMER_MAPID, mc->bl.m);
  16308. getunitdata_sub(UMER_X, mc->bl.x);
  16309. getunitdata_sub(UMER_Y, mc->bl.y);
  16310. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  16311. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  16312. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  16313. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  16314. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  16315. getunitdata_sub(UMER_STR, mc->base_status.str);
  16316. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  16317. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  16318. getunitdata_sub(UMER_INT, mc->base_status.int_);
  16319. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  16320. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  16321. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  16322. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  16323. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  16324. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  16325. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  16326. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  16327. getunitdata_sub(UMER_DEF, mc->base_status.def);
  16328. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  16329. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  16330. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  16331. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  16332. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  16333. getunitdata_sub(UMER_RACE, mc->base_status.race);
  16334. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  16335. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  16336. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  16337. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  16338. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  16339. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  16340. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  16341. } break;
  16342. case BL_ELEM: {
  16343. s_elemental_data* ed = reinterpret_cast<s_elemental_data*>( bl );
  16344. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  16345. getunitdata_sub(UELE_HP, ed->elemental.hp);
  16346. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  16347. getunitdata_sub(UELE_SP, ed->elemental.sp);
  16348. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  16349. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  16350. getunitdata_sub(UELE_MAPID, ed->bl.m);
  16351. getunitdata_sub(UELE_X, ed->bl.x);
  16352. getunitdata_sub(UELE_Y, ed->bl.y);
  16353. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  16354. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  16355. getunitdata_sub(UELE_SP, ed->base_status.speed);
  16356. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  16357. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  16358. getunitdata_sub(UELE_STR, ed->base_status.str);
  16359. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  16360. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  16361. getunitdata_sub(UELE_INT, ed->base_status.int_);
  16362. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  16363. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  16364. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  16365. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  16366. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  16367. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  16368. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  16369. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  16370. getunitdata_sub(UELE_DEF, ed->base_status.def);
  16371. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  16372. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  16373. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  16374. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  16375. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  16376. getunitdata_sub(UELE_RACE, ed->base_status.race);
  16377. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  16378. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  16379. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  16380. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  16381. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  16382. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  16383. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  16384. } break;
  16385. case BL_NPC: {
  16386. npc_data* nd = reinterpret_cast<npc_data*>( bl );
  16387. getunitdata_sub(UNPC_LEVEL, nd->level);
  16388. getunitdata_sub(UNPC_HP, nd->status.hp);
  16389. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  16390. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  16391. getunitdata_sub(UNPC_X, nd->bl.x);
  16392. getunitdata_sub(UNPC_Y, nd->bl.y);
  16393. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  16394. getunitdata_sub(UNPC_STR, nd->status.str);
  16395. getunitdata_sub(UNPC_AGI, nd->status.agi);
  16396. getunitdata_sub(UNPC_VIT, nd->status.vit);
  16397. getunitdata_sub(UNPC_INT, nd->status.int_);
  16398. getunitdata_sub(UNPC_DEX, nd->status.dex);
  16399. getunitdata_sub(UNPC_LUK, nd->status.luk);
  16400. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  16401. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  16402. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  16403. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  16404. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  16405. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  16406. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  16407. getunitdata_sub(UNPC_DEF, nd->status.def);
  16408. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  16409. getunitdata_sub(UNPC_HIT, nd->status.hit);
  16410. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  16411. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  16412. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  16413. getunitdata_sub(UNPC_RACE, nd->status.race);
  16414. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  16415. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  16416. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  16417. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  16418. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  16419. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  16420. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  16421. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  16422. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  16423. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  16424. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  16425. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  16426. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  16427. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  16428. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  16429. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  16430. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  16431. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  16432. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  16433. } break;
  16434. default:
  16435. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  16436. return SCRIPT_CMD_FAILURE;
  16437. }
  16438. #undef getunitdata_sub
  16439. return SCRIPT_CMD_SUCCESS;
  16440. }
  16441. /// Changes the live data of a bl.
  16442. ///
  16443. /// setunitdata <unit id>,<type>,<value>;
  16444. BUILDIN_FUNC(setunitdata)
  16445. {
  16446. struct block_list* bl;
  16447. if(!script_rid2bl(2,bl))
  16448. {
  16449. script_pushint(st, -1);
  16450. return SCRIPT_CMD_FAILURE;
  16451. }
  16452. int32 type = script_getnum(st, 3), value = 0;
  16453. const char *mapname = nullptr;
  16454. bool calc_status = false;
  16455. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  16456. mapname = script_getstr(st, 4);
  16457. else
  16458. value = script_getnum(st, 4);
  16459. switch (bl->type) {
  16460. case BL_MOB: {
  16461. mob_data* md = reinterpret_cast<mob_data*>( bl );
  16462. if (!md->base_status) {
  16463. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  16464. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  16465. }
  16466. // Check if the view data will be modified
  16467. switch( type ){
  16468. case UMOB_SEX:
  16469. //case UMOB_CLASS: // Called by status_set_viewdata
  16470. case UMOB_HAIRSTYLE:
  16471. case UMOB_HAIRCOLOR:
  16472. case UMOB_HEADBOTTOM:
  16473. case UMOB_HEADMIDDLE:
  16474. case UMOB_HEADTOP:
  16475. case UMOB_CLOTHCOLOR:
  16476. case UMOB_SHIELD:
  16477. case UMOB_WEAPON:
  16478. case UMOB_ROBE:
  16479. case UMOB_BODY2:
  16480. mob_set_dynamic_viewdata( md );
  16481. break;
  16482. }
  16483. switch (type) {
  16484. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  16485. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  16486. case UMOB_HP: md->base_status->hp = (uint32)value; status_set_hp(bl, (uint32)value, 0); clif_name_area(&md->bl); break;
  16487. case UMOB_MAXHP: md->base_status->hp = md->base_status->max_hp = (uint32)value; status_set_maxhp(bl, (uint32)value, 0); clif_name_area(&md->bl); break;
  16488. case UMOB_MASTERAID: md->master_id = value; break;
  16489. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16490. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16491. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  16492. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16493. case UMOB_MODE:
  16494. md->base_status->mode = (enum e_mode)value;
  16495. // Mob mode must be updated before calling unit_refresh
  16496. status_calc_bl_(&md->bl, status_db.getSCB_BATTLE());
  16497. unit_refresh(bl);
  16498. break;
  16499. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  16500. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  16501. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  16502. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  16503. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16504. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16505. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16506. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16507. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16508. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16509. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16510. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16511. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16512. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (uint32)value : 0; break;
  16513. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16514. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16515. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16516. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16517. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16518. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16519. case UMOB_SLAVECPYMSTRMD:
  16520. if (value > 0) {
  16521. TBL_MOB *md2 = nullptr;
  16522. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  16523. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  16524. break;
  16525. }
  16526. md->base_status->mode = md2->status.mode;
  16527. md->state.copy_master_mode = 1;
  16528. } else
  16529. md->state.copy_master_mode = 0;
  16530. calc_status = true;
  16531. break;
  16532. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  16533. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  16534. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  16535. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16536. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  16537. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  16538. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  16539. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  16540. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  16541. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  16542. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  16543. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  16544. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  16545. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  16546. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  16547. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  16548. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  16549. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  16550. case UMOB_TARGETID: {
  16551. if (value==0) {
  16552. mob_unlocktarget(md,gettick());
  16553. break;
  16554. }
  16555. struct block_list* target = map_id2bl(value);
  16556. if (!target) {
  16557. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  16558. return SCRIPT_CMD_FAILURE;
  16559. }
  16560. mob_target(md,target,0);
  16561. break;
  16562. }
  16563. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16564. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16565. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  16566. case UMOB_IGNORE_CELL_STACK_LIMIT: md->ud.state.ignore_cell_stack_limit = value > 0; break;
  16567. case UMOB_RES: md->base_status->res = (short)value; calc_status = true; break;
  16568. case UMOB_MRES: md->base_status->mres = (short)value; calc_status = true; break;
  16569. case UMOB_DAMAGETAKEN: md->damagetaken = (unsigned short)value; break;
  16570. default:
  16571. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  16572. return SCRIPT_CMD_FAILURE;
  16573. }
  16574. if (calc_status)
  16575. status_calc_bl_(&md->bl, status_db.getSCB_BATTLE());
  16576. } break;
  16577. case BL_HOM: {
  16578. homun_data* hd = reinterpret_cast<homun_data*>( bl );
  16579. switch (type) {
  16580. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  16581. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  16582. case UHOM_HP: hd->base_status.hp = (uint32)value; status_set_hp(bl, (uint32)value, 0); break;
  16583. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (uint32)value; status_set_maxhp(bl, (uint32)value, 0); break;
  16584. case UHOM_SP: hd->base_status.sp = (uint32)value; status_set_sp(bl, (uint32)value, 0); break;
  16585. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (uint32)value; status_set_maxsp(bl, (uint32)value, 0); break;
  16586. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  16587. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16588. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  16589. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  16590. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(*hd, SP_HUNGRY); break;
  16591. case UHOM_INTIMACY: hom_increase_intimacy(hd, (uint32)value); clif_send_homdata(*hd, SP_INTIMATE); break;
  16592. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16593. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16594. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (uint32)value : 0; break;
  16595. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16596. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16597. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16598. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16599. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16600. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16601. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  16602. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16603. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16604. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16605. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16606. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16607. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  16608. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  16609. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  16610. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  16611. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  16612. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  16613. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  16614. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16615. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16616. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  16617. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  16618. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  16619. case UHOM_TARGETID: {
  16620. if (value==0) {
  16621. unit_stop_attack(&hd->bl);
  16622. break;
  16623. }
  16624. struct block_list* target = map_id2bl(value);
  16625. if (!target) {
  16626. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  16627. return SCRIPT_CMD_FAILURE;
  16628. }
  16629. unit_attack(&hd->bl, target->id, 1);
  16630. break;
  16631. }
  16632. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  16633. default:
  16634. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  16635. return SCRIPT_CMD_FAILURE;
  16636. }
  16637. if (calc_status)
  16638. status_calc_bl_(&hd->bl, status_db.getSCB_BATTLE());
  16639. // Client information update
  16640. clif_send_homdata( *hd, SP_ACK );
  16641. } break;
  16642. case BL_PET: {
  16643. pet_data* pd = reinterpret_cast<pet_data*>( bl );
  16644. switch (type) {
  16645. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  16646. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  16647. case UPET_HP: pd->status.hp = pd->status.max_hp = (uint32)value; status_set_hp(bl, (uint32)value, 0); break;
  16648. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (uint32)value; status_set_maxhp(bl, (uint32)value, 0); break;
  16649. case UPET_MASTERAID: pd->pet.account_id = (uint32)value; break;
  16650. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16651. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, pd->bl.y, 0, 0); break;
  16652. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  16653. case UPET_HUNGER: pd->pet.hungry = cap_value((short)value, 0, 100); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  16654. case UPET_INTIMACY: pet_set_intimate(pd, (uint32)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  16655. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16656. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16657. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (uint32)value : 0; break;
  16658. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16659. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16660. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16661. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16662. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16663. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16664. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  16665. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  16666. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  16667. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  16668. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  16669. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  16670. case UPET_DEF: pd->status.def = (defType)value; break;
  16671. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  16672. case UPET_HIT: pd->status.hit = (short)value; break;
  16673. case UPET_FLEE: pd->status.flee = (short)value; break;
  16674. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16675. case UPET_CRIT: pd->status.cri = (short)value; break;
  16676. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16677. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16678. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16679. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16680. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16681. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16682. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16683. default:
  16684. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16685. return SCRIPT_CMD_FAILURE;
  16686. }
  16687. // Client information update
  16688. clif_send_petstatus( pd->master );
  16689. } break;
  16690. case BL_MER: {
  16691. s_mercenary_data* mc = reinterpret_cast<s_mercenary_data*>( bl );
  16692. switch (type) {
  16693. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16694. case UMER_HP: mc->base_status.hp = (uint32)value; status_set_hp(bl, (uint32)value, 0); break;
  16695. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (uint32)value; status_set_maxhp(bl, (uint32)value, 0); break;
  16696. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16697. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16698. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16699. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16700. case UMER_KILLCOUNT: mc->mercenary.kill_count = (uint32)value; break;
  16701. case UMER_LIFETIME: mc->mercenary.life_time = (uint32)value; break;
  16702. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16703. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16704. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (uint32)value : 0; break;
  16705. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16706. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16707. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16708. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16709. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16710. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16711. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16712. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16713. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16714. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16715. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16716. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16717. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16718. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16719. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16720. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16721. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16722. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16723. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16724. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16725. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16726. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16727. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16728. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16729. case UMER_TARGETID: {
  16730. if (value==0) {
  16731. unit_stop_attack(&mc->bl);
  16732. break;
  16733. }
  16734. struct block_list* target = map_id2bl(value);
  16735. if (!target) {
  16736. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16737. return SCRIPT_CMD_FAILURE;
  16738. }
  16739. unit_attack(&mc->bl, target->id, 1);
  16740. break;
  16741. }
  16742. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16743. default:
  16744. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16745. return SCRIPT_CMD_FAILURE;
  16746. }
  16747. if (calc_status)
  16748. status_calc_bl_(&mc->bl, status_db.getSCB_BATTLE());
  16749. // Client information update
  16750. if( map_session_data* sd = map_charid2sd( mc->mercenary.char_id ); sd != nullptr ){
  16751. clif_mercenary_info( sd );
  16752. clif_mercenary_skillblock( sd );
  16753. }
  16754. } break;
  16755. case BL_ELEM: {
  16756. s_elemental_data* ed = reinterpret_cast<s_elemental_data*>( bl );
  16757. switch (type) {
  16758. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16759. case UELE_HP: ed->base_status.hp = (uint32)value; status_set_hp(bl, (uint32)value, 0); break;
  16760. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (uint32)value; status_set_maxhp(bl, (uint32)value, 0); break;
  16761. case UELE_SP: ed->base_status.sp = (uint32)value; status_set_sp(bl, (uint32)value, 0); break;
  16762. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (uint32)value; status_set_maxsp(bl, (uint32)value, 0); break;
  16763. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16764. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16765. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16766. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16767. case UELE_LIFETIME: ed->elemental.life_time = (uint32)value; break;
  16768. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16769. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16770. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16771. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (uint32)value : 0; break;
  16772. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16773. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16774. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16775. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16776. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16777. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16778. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16779. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16780. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16781. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16782. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16783. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16784. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16785. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16786. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16787. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16788. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16789. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16790. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16791. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16792. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16793. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16794. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16795. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16796. case UELE_TARGETID: {
  16797. if (value==0) {
  16798. unit_stop_attack(&ed->bl);
  16799. break;
  16800. }
  16801. struct block_list* target = map_id2bl(value);
  16802. if (!target) {
  16803. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16804. return SCRIPT_CMD_FAILURE;
  16805. }
  16806. elemental_change_mode(ed, EL_MODE_AGGRESSIVE);
  16807. unit_attack(&ed->bl, target->id, 1);
  16808. break;
  16809. }
  16810. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16811. default:
  16812. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16813. return SCRIPT_CMD_FAILURE;
  16814. }
  16815. if (calc_status)
  16816. status_calc_bl_(&ed->bl, status_db.getSCB_BATTLE());
  16817. // Client information update
  16818. clif_elemental_info( ed->master );
  16819. } break;
  16820. case BL_NPC: {
  16821. npc_data* nd = reinterpret_cast<npc_data*>( bl );
  16822. if( nd->status.hp == 0 ){
  16823. status_calc_npc( nd, SCO_FIRST );
  16824. }else{
  16825. status_calc_npc( nd, SCO_NONE );
  16826. }
  16827. switch (type) {
  16828. case UNPC_LEVEL: nd->level = (uint32)value; break;
  16829. case UNPC_HP: nd->status.hp = (uint32)value; status_set_hp(bl, (uint32)value, 0); break;
  16830. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (uint32)value; status_set_maxhp(bl, (uint32)value, 0); break;
  16831. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16832. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16833. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16834. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16835. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16836. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16837. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16838. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16839. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16840. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16841. case UNPC_PLUSALLSTAT: nd->stat_point = (uint32)value; break;
  16842. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16843. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16844. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16845. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16846. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16847. case UNPC_DEF: nd->status.def = (defType)value; break;
  16848. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16849. case UNPC_HIT: nd->status.hit = (short)value; break;
  16850. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16851. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16852. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16853. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16854. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16855. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16856. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16857. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16858. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16859. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16860. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16861. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16862. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16863. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16864. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16865. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16866. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16867. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16868. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16869. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16870. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16871. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16872. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16873. default:
  16874. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16875. return SCRIPT_CMD_FAILURE;
  16876. }
  16877. } break;
  16878. default:
  16879. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16880. return SCRIPT_CMD_FAILURE;
  16881. }
  16882. return SCRIPT_CMD_SUCCESS;
  16883. }
  16884. /// Gets the name of a bl.
  16885. /// Supported types are [MOB|HOM|PET|NPC].
  16886. /// MER and ELEM don't support custom names.
  16887. ///
  16888. /// getunitname <unit id>;
  16889. BUILDIN_FUNC(getunitname)
  16890. {
  16891. struct block_list* bl = nullptr;
  16892. if(!script_rid2bl(2,bl)){
  16893. script_pushconststr(st, "Unknown");
  16894. return SCRIPT_CMD_FAILURE;
  16895. }
  16896. script_pushstrcopy(st, status_get_name(bl));
  16897. return SCRIPT_CMD_SUCCESS;
  16898. }
  16899. /// Changes the name of a bl.
  16900. /// Supported types are [MOB|HOM|PET].
  16901. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16902. ///
  16903. /// setunitname <unit id>,<name>;
  16904. BUILDIN_FUNC(setunitname)
  16905. {
  16906. struct block_list* bl = nullptr;
  16907. TBL_MOB* md = nullptr;
  16908. TBL_HOM* hd = nullptr;
  16909. TBL_PET* pd = nullptr;
  16910. if(!script_rid2bl(2,bl))
  16911. {
  16912. script_pushconststr(st, "Unknown");
  16913. return SCRIPT_CMD_FAILURE;
  16914. }
  16915. switch (bl->type) {
  16916. case BL_MOB: md = map_id2md(bl->id); break;
  16917. case BL_HOM: hd = map_id2hd(bl->id); break;
  16918. case BL_PET: pd = map_id2pd(bl->id); break;
  16919. default:
  16920. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16921. return SCRIPT_CMD_FAILURE;
  16922. }
  16923. switch (bl->type) {
  16924. case BL_MOB:
  16925. if (!md) {
  16926. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16927. return SCRIPT_CMD_FAILURE;
  16928. }
  16929. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16930. break;
  16931. case BL_HOM:
  16932. if (!hd) {
  16933. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16934. return SCRIPT_CMD_FAILURE;
  16935. }
  16936. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16937. break;
  16938. case BL_PET:
  16939. if (!pd) {
  16940. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16941. return SCRIPT_CMD_FAILURE;
  16942. }
  16943. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16944. break;
  16945. default:
  16946. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16947. return SCRIPT_CMD_FAILURE;
  16948. }
  16949. clif_name_area(bl); // Send update to client.
  16950. return SCRIPT_CMD_SUCCESS;
  16951. }
  16952. /**
  16953. * Sets a unit's title.
  16954. * setunittitle <GID>,<title>;
  16955. */
  16956. BUILDIN_FUNC(setunittitle)
  16957. {
  16958. int32 gid = script_getnum(st, 2);
  16959. block_list *bl = map_id2bl(gid);
  16960. if (bl == nullptr) {
  16961. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16962. return SCRIPT_CMD_FAILURE;
  16963. }
  16964. unit_data *ud = unit_bl2ud(bl);
  16965. if (ud == nullptr) {
  16966. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16967. return SCRIPT_CMD_FAILURE;
  16968. }
  16969. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16970. clif_name_area(bl);
  16971. return SCRIPT_CMD_SUCCESS;
  16972. }
  16973. /**
  16974. * Gets a unit's title.
  16975. * getunittitle <GID>;
  16976. */
  16977. BUILDIN_FUNC(getunittitle)
  16978. {
  16979. int32 gid = script_getnum(st, 2);
  16980. block_list *bl = map_id2bl(gid);
  16981. if (bl == nullptr) {
  16982. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16983. return SCRIPT_CMD_FAILURE;
  16984. }
  16985. unit_data *ud = unit_bl2ud(bl);
  16986. if (ud == nullptr) {
  16987. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16988. return SCRIPT_CMD_FAILURE;
  16989. }
  16990. script_pushstrcopy(st, ud->title);
  16991. return SCRIPT_CMD_SUCCESS;
  16992. }
  16993. /// Makes the unit walk to target position or map.
  16994. /// Returns if it was successful.
  16995. ///
  16996. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16997. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16998. BUILDIN_FUNC(unitwalk)
  16999. {
  17000. struct block_list* bl;
  17001. struct unit_data *ud = nullptr;
  17002. const char *cmd = script_getfuncname(st), *done_label = "";
  17003. uint8 off = 5;
  17004. if(!script_rid2bl(2,bl))
  17005. {
  17006. script_pushint(st, 0);
  17007. return SCRIPT_CMD_FAILURE;
  17008. }
  17009. ud = unit_bl2ud(bl);
  17010. // Unit was already forced to walk.
  17011. if (ud != nullptr && ud->state.force_walk) {
  17012. script_pushint(st, 0);
  17013. ShowWarning("buildin_%s: Unit has already been forced to walk and not reached it's destination yet.\n", cmd);
  17014. return SCRIPT_CMD_FAILURE;
  17015. }
  17016. if (bl->type == BL_NPC) {
  17017. if (!((TBL_NPC*)bl)->status.hp)
  17018. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17019. else
  17020. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17021. }
  17022. if (!strcmp(cmd,"unitwalk")) {
  17023. int32 x = script_getnum(st,3);
  17024. int32 y = script_getnum(st,4);
  17025. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0))) {
  17026. if (ud != nullptr)
  17027. ud->state.force_walk = true;
  17028. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  17029. }
  17030. } else {
  17031. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  17032. if (!tbl) {
  17033. ShowError("buildin_unitwalk: Bad target destination.\n");
  17034. script_pushint(st, 0);
  17035. return SCRIPT_CMD_FAILURE;
  17036. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, nullptr, nullptr))) {
  17037. if (ud != nullptr)
  17038. ud->state.force_walk = true;
  17039. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  17040. }
  17041. off = 4;
  17042. }
  17043. if (ud && script_hasdata(st, off)) {
  17044. done_label = script_getstr(st, off);
  17045. check_event(st, done_label);
  17046. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  17047. }
  17048. return SCRIPT_CMD_SUCCESS;
  17049. }
  17050. /// Kills the unit.
  17051. ///
  17052. /// unitkill <unit_id>;
  17053. BUILDIN_FUNC(unitkill)
  17054. {
  17055. struct block_list* bl;
  17056. if(script_rid2bl(2,bl))
  17057. status_kill(bl);
  17058. return SCRIPT_CMD_SUCCESS;
  17059. }
  17060. /// Warps the unit to the target position in the target map.
  17061. /// Returns if it was successful.
  17062. ///
  17063. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  17064. BUILDIN_FUNC(unitwarp)
  17065. {
  17066. int32 map_idx;
  17067. short x;
  17068. short y;
  17069. struct block_list* bl;
  17070. const char *mapname;
  17071. mapname = script_getstr(st, 3);
  17072. x = (short)script_getnum(st,4);
  17073. y = (short)script_getnum(st,5);
  17074. if(!script_rid2bl(2,bl))
  17075. {
  17076. script_pushint(st, 0);
  17077. return SCRIPT_CMD_SUCCESS;
  17078. }
  17079. if (!strcmp(mapname,"this"))
  17080. map_idx = bl?bl->m:-1;
  17081. else
  17082. map_idx = map_mapname2mapid(mapname);
  17083. if (map_idx >= 0 && bl != nullptr)
  17084. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  17085. else
  17086. script_pushint(st, 0);
  17087. return SCRIPT_CMD_SUCCESS;
  17088. }
  17089. /// Makes the unit attack the target.
  17090. /// If the unit is a player and <action type> is not 0, it does a continuous
  17091. /// attack instead of a single attack.
  17092. /// Returns if the request was successful.
  17093. ///
  17094. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  17095. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  17096. BUILDIN_FUNC(unitattack)
  17097. {
  17098. struct block_list* unit_bl;
  17099. struct block_list* target_bl = nullptr;
  17100. int32 actiontype = 0;
  17101. if (!script_rid2bl(2,unit_bl)) {
  17102. script_pushint(st, false);
  17103. return SCRIPT_CMD_FAILURE;
  17104. }
  17105. if (script_isstring(st, 3)) {
  17106. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  17107. if( sd != nullptr )
  17108. target_bl = &sd->bl;
  17109. } else
  17110. target_bl = map_id2bl(script_getnum(st, 3));
  17111. if (!target_bl) {
  17112. script_pushint(st, false);
  17113. return SCRIPT_CMD_FAILURE;
  17114. }
  17115. if (script_hasdata(st,4))
  17116. actiontype = script_getnum(st,4);
  17117. switch(unit_bl->type) {
  17118. case BL_PC: {
  17119. map_session_data* sd = (map_session_data*)unit_bl;
  17120. clif_parse_ActionRequest_sub( *sd, actiontype > 0 ? DMG_REPEAT : DMG_NORMAL, target_bl->id, gettick() );
  17121. script_pushint(st, sd->ud.target == target_bl->id);
  17122. return SCRIPT_CMD_SUCCESS;
  17123. }
  17124. case BL_MOB:
  17125. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  17126. break;
  17127. case BL_PET:
  17128. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  17129. break;
  17130. default:
  17131. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  17132. script_pushint(st, false);
  17133. return SCRIPT_CMD_FAILURE;
  17134. }
  17135. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  17136. return SCRIPT_CMD_SUCCESS;
  17137. }
  17138. /// Makes the unit stop attacking.
  17139. ///
  17140. /// unitstopattack <unit_id>;
  17141. BUILDIN_FUNC(unitstopattack)
  17142. {
  17143. struct block_list* bl;
  17144. if(script_rid2bl(2,bl))
  17145. {
  17146. unit_stop_attack(bl);
  17147. if (bl->type == BL_MOB)
  17148. ((TBL_MOB*)bl)->target_id = 0;
  17149. }
  17150. return SCRIPT_CMD_SUCCESS;
  17151. }
  17152. /// Makes the unit stop walking.
  17153. ///
  17154. /// unitstopwalk <unit_id>{,<flag>};
  17155. BUILDIN_FUNC(unitstopwalk)
  17156. {
  17157. struct block_list* bl;
  17158. int32 flag = USW_NONE;
  17159. if (script_hasdata(st, 3))
  17160. flag = script_getnum(st, 3);
  17161. if(script_rid2bl(2,bl)) {
  17162. unit_data *ud = unit_bl2ud(bl);
  17163. if (ud != nullptr)
  17164. ud->state.force_walk = false;
  17165. if (unit_stop_walking(bl, flag) == 0 && flag != USW_FORCE_STOP) {
  17166. ShowWarning("buildin_unitstopwalk: Unable to find unit or unit is not walking.\n");
  17167. return SCRIPT_CMD_FAILURE;
  17168. }
  17169. return SCRIPT_CMD_SUCCESS;
  17170. } else {
  17171. return SCRIPT_CMD_FAILURE;
  17172. }
  17173. }
  17174. /**
  17175. * Makes the unit say the given message.
  17176. *
  17177. * unittalk <unit_id>,"<message>"{,"<flag>"};
  17178. * @param flag: Specify target
  17179. * bc_area - Message is sent to players in the vicinity of the source (default).
  17180. * bc_self - Message is sent only to player attached.
  17181. */
  17182. BUILDIN_FUNC(unittalk)
  17183. {
  17184. const char* message;
  17185. struct block_list* bl;
  17186. message = script_getstr(st, 3);
  17187. if(script_rid2bl(2,bl))
  17188. {
  17189. send_target target = AREA;
  17190. struct StringBuf sbuf;
  17191. if (script_hasdata(st, 4)) {
  17192. if (script_getnum(st, 4) == BC_SELF) {
  17193. if (map_id2sd(bl->id) == nullptr) {
  17194. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  17195. return SCRIPT_CMD_FAILURE;
  17196. }
  17197. target = SELF;
  17198. }
  17199. }
  17200. StringBuf_Init(&sbuf);
  17201. StringBuf_Printf(&sbuf, "%s", message);
  17202. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  17203. StringBuf_Destroy(&sbuf);
  17204. }
  17205. return SCRIPT_CMD_SUCCESS;
  17206. }
  17207. /// Makes the unit cast the skill on the target or self if no target is specified.
  17208. ///
  17209. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  17210. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  17211. BUILDIN_FUNC(unitskilluseid)
  17212. {
  17213. int32 unit_id, target_id, casttime;
  17214. uint16 skill_id, skill_lv;
  17215. struct block_list* bl;
  17216. unit_id = script_getnum(st,2);
  17217. if (script_isstring(st, 3)) {
  17218. const char *name = script_getstr(st, 3);
  17219. if (!(skill_id = skill_name2id(name))) {
  17220. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  17221. return SCRIPT_CMD_FAILURE;
  17222. }
  17223. } else {
  17224. skill_id = script_getnum(st, 3);
  17225. if (!skill_get_index(skill_id)) {
  17226. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  17227. return SCRIPT_CMD_FAILURE;
  17228. }
  17229. }
  17230. skill_lv = script_getnum(st,4);
  17231. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  17232. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  17233. bool cancel = ( script_hasdata(st,7) ? script_getnum(st,7) > 0 : skill_get_castcancel(skill_id) );
  17234. int32 msg_id = (script_hasdata(st, 8) ? script_getnum(st, 8) : 0);
  17235. bool ignore_range = (script_hasdata(st, 9) ? script_getnum(st, 9) > 0 : false);
  17236. if(script_rid2bl(2,bl)){
  17237. if (msg_id > 0) {
  17238. if (bl->type != BL_MOB) {
  17239. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  17240. return SCRIPT_CMD_FAILURE;
  17241. }
  17242. TBL_MOB* md = map_id2md(bl->id);
  17243. if (md)
  17244. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  17245. }
  17246. if (bl->type == BL_NPC) {
  17247. if (!((TBL_NPC*)bl)->status.hp)
  17248. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17249. else
  17250. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17251. }
  17252. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel, ignore_range);
  17253. }
  17254. return SCRIPT_CMD_SUCCESS;
  17255. }
  17256. /// Makes the unit cast the skill on the target position.
  17257. ///
  17258. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  17259. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  17260. BUILDIN_FUNC(unitskillusepos)
  17261. {
  17262. int32 skill_x, skill_y, casttime;
  17263. uint16 skill_id, skill_lv;
  17264. struct block_list* bl;
  17265. if (script_isstring(st, 3)) {
  17266. const char *name = script_getstr(st, 3);
  17267. if (!(skill_id = skill_name2id(name))) {
  17268. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  17269. return SCRIPT_CMD_FAILURE;
  17270. }
  17271. } else {
  17272. skill_id = script_getnum(st, 3);
  17273. if (!skill_get_index(skill_id)) {
  17274. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  17275. return SCRIPT_CMD_FAILURE;
  17276. }
  17277. }
  17278. skill_lv = script_getnum(st,4);
  17279. skill_x = script_getnum(st,5);
  17280. skill_y = script_getnum(st,6);
  17281. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  17282. bool cancel = ( script_hasdata(st,8) ? script_getnum(st,8) > 0 : skill_get_castcancel(skill_id) );
  17283. int32 msg_id = (script_hasdata(st, 9) ? script_getnum(st, 9) : 0);
  17284. bool ignore_range = (script_hasdata(st, 10) ? script_getnum(st, 10) > 0 : false);
  17285. if(script_rid2bl(2,bl)){
  17286. if (msg_id > 0) {
  17287. if (bl->type != BL_MOB) {
  17288. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  17289. return SCRIPT_CMD_FAILURE;
  17290. }
  17291. TBL_MOB* md = map_id2md(bl->id);
  17292. if (md)
  17293. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  17294. }
  17295. if (bl->type == BL_NPC) {
  17296. if (!((TBL_NPC*)bl)->status.hp)
  17297. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17298. else
  17299. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17300. }
  17301. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel, ignore_range);
  17302. }
  17303. return SCRIPT_CMD_SUCCESS;
  17304. }
  17305. // <--- [zBuffer] List of unit control commands
  17306. /// Pauses the execution of the script, detaching the player
  17307. ///
  17308. /// sleep <milli seconds>;
  17309. BUILDIN_FUNC(sleep)
  17310. {
  17311. // First call(by function call)
  17312. if (st->sleep.tick == 0) {
  17313. int32 ticks;
  17314. ticks = script_getnum(st, 2);
  17315. if (ticks <= 0) {
  17316. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  17317. return SCRIPT_CMD_FAILURE;
  17318. }
  17319. // detach the player
  17320. script_detach_rid(st);
  17321. // sleep for the target amount of time
  17322. st->state = RERUNLINE;
  17323. st->sleep.tick = ticks;
  17324. // Second call(by timer after sleeping time is over)
  17325. } else {
  17326. // Continue the script
  17327. st->state = RUN;
  17328. st->sleep.tick = 0;
  17329. }
  17330. return SCRIPT_CMD_SUCCESS;
  17331. }
  17332. /// Pauses the execution of the script, keeping the unit attached
  17333. /// Stops the script if no unit is attached
  17334. ///
  17335. /// sleep2(<milli seconds>)
  17336. BUILDIN_FUNC(sleep2)
  17337. {
  17338. // First call(by function call)
  17339. if (st->sleep.tick == 0) {
  17340. int32 ticks;
  17341. ticks = script_getnum(st, 2);
  17342. if (ticks <= 0) {
  17343. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  17344. return SCRIPT_CMD_FAILURE;
  17345. }
  17346. if (map_id2bl(st->rid) == nullptr) {
  17347. ShowError( "buildin_sleep2: no unit is attached\n" );
  17348. return SCRIPT_CMD_FAILURE;
  17349. }
  17350. // sleep for the target amount of time
  17351. st->state = RERUNLINE;
  17352. st->sleep.tick = ticks;
  17353. // Second call(by timer after sleeping time is over)
  17354. } else {
  17355. // Check if the unit is still attached
  17356. // NOTE: This should never happen, since run_script_timer already checks this
  17357. if (map_id2bl(st->rid) == nullptr) {
  17358. // The unit is not attached anymore - terminate the script
  17359. st->rid = 0;
  17360. st->state = END;
  17361. } else {
  17362. // The unit is still attached - continue the script
  17363. st->state = RUN;
  17364. st->sleep.tick = 0;
  17365. }
  17366. }
  17367. return SCRIPT_CMD_SUCCESS;
  17368. }
  17369. /// Awakes all the sleep timers of the target npc
  17370. ///
  17371. /// awake "<npc name>";
  17372. BUILDIN_FUNC(awake)
  17373. {
  17374. DBIterator *iter;
  17375. struct script_state *tst;
  17376. struct npc_data* nd;
  17377. if ((nd = npc_name2id(script_getstr(st, 2))) == nullptr) {
  17378. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  17379. return SCRIPT_CMD_FAILURE;
  17380. }
  17381. int32 rid = st->rid;
  17382. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  17383. if( rid ){
  17384. script_detach_rid(st);
  17385. }
  17386. iter = db_iterator(st_db);
  17387. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  17388. if (tst->oid == nd->bl.id) {
  17389. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  17390. continue;
  17391. }
  17392. delete_timer(tst->sleep.timer, run_script_timer);
  17393. // Trigger the timer function
  17394. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  17395. }
  17396. }
  17397. dbi_destroy(iter);
  17398. // If a player had been attached, now is the time to restore it
  17399. if( rid ){
  17400. st->rid = rid;
  17401. script_attach_state(st);
  17402. }
  17403. return SCRIPT_CMD_SUCCESS;
  17404. }
  17405. /// Returns a reference to a variable of the target NPC.
  17406. /// Returns 0 if an error occurs.
  17407. ///
  17408. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  17409. BUILDIN_FUNC(getvariableofnpc)
  17410. {
  17411. struct script_data* data;
  17412. const char* name;
  17413. struct npc_data* nd;
  17414. data = script_getdata(st,2);
  17415. if( !data_isreference(data) )
  17416. {// Not a reference (aka varaible name)
  17417. ShowError("buildin_getvariableofnpc: not a variable\n");
  17418. script_reportdata(data);
  17419. script_pushnil(st);
  17420. st->state = END;
  17421. return SCRIPT_CMD_FAILURE;
  17422. }
  17423. name = reference_getname(data);
  17424. if( *name != '.' || name[1] == '@' )
  17425. {// not a npc variable
  17426. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  17427. script_reportdata(data);
  17428. script_pushnil(st);
  17429. st->state = END;
  17430. return SCRIPT_CMD_FAILURE;
  17431. }
  17432. nd = npc_name2id(script_getstr(st,3));
  17433. if( nd == nullptr || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == nullptr )
  17434. {// NPC not found or has no script
  17435. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  17436. script_pushnil(st);
  17437. st->state = END;
  17438. return SCRIPT_CMD_FAILURE;
  17439. }
  17440. if (!nd->u.scr.script->local.vars)
  17441. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  17442. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  17443. return SCRIPT_CMD_SUCCESS;
  17444. }
  17445. /// Opens a warp portal.
  17446. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  17447. ///
  17448. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  17449. ///
  17450. /// @author blackhole89
  17451. BUILDIN_FUNC(warpportal)
  17452. {
  17453. int32 spx;
  17454. int32 spy;
  17455. unsigned short mapindex;
  17456. int32 tpx;
  17457. int32 tpy;
  17458. std::shared_ptr<s_skill_unit_group> group;
  17459. struct block_list* bl;
  17460. bl = map_id2bl(st->oid);
  17461. if( bl == nullptr ) {
  17462. ShowError("buildin_warpportal: NPC is needed\n");
  17463. return SCRIPT_CMD_FAILURE;
  17464. }
  17465. spx = script_getnum(st,2);
  17466. spy = script_getnum(st,3);
  17467. mapindex = mapindex_name2id(script_getstr(st, 4));
  17468. tpx = script_getnum(st,5);
  17469. tpy = script_getnum(st,6);
  17470. if( mapindex == 0 ) {
  17471. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  17472. return SCRIPT_CMD_FAILURE;
  17473. }
  17474. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  17475. if( group == nullptr )
  17476. return SCRIPT_CMD_FAILURE;// failed
  17477. group->val1 = (group->val1<<16)|(short)0;
  17478. group->val2 = (tpx<<16) | tpy;
  17479. group->val3 = mapindex;
  17480. return SCRIPT_CMD_SUCCESS;
  17481. }
  17482. /**
  17483. * openmail({<char_id>});
  17484. **/
  17485. BUILDIN_FUNC(openmail)
  17486. {
  17487. map_session_data* sd;
  17488. if (!script_charid2sd(2,sd))
  17489. return SCRIPT_CMD_FAILURE;
  17490. #if PACKETVER < 20150513
  17491. mail_openmail(sd);
  17492. return SCRIPT_CMD_SUCCESS;
  17493. #else
  17494. ShowError( "buildin_openmail: This command is not supported for PACKETVER 2015-05-13 or newer.\n" );
  17495. return SCRIPT_CMD_FAILURE;
  17496. #endif
  17497. }
  17498. /**
  17499. * openauction({<char_id>});
  17500. **/
  17501. BUILDIN_FUNC(openauction)
  17502. {
  17503. TBL_PC* sd;
  17504. if (!script_charid2sd(2,sd))
  17505. return SCRIPT_CMD_FAILURE;
  17506. if( !battle_config.feature_auction ) {
  17507. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  17508. return SCRIPT_CMD_SUCCESS;
  17509. }
  17510. clif_Auction_openwindow(sd);
  17511. return SCRIPT_CMD_SUCCESS;
  17512. }
  17513. /// Retrieves the value of the specified flag of the specified cell.
  17514. ///
  17515. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  17516. ///
  17517. /// @see cell_chk* constants in src/map/script_constants.hpp for the types
  17518. BUILDIN_FUNC(checkcell)
  17519. {
  17520. int16 m = map_mapname2mapid(script_getstr(st,2));
  17521. int16 x = script_getnum(st,3);
  17522. int16 y = script_getnum(st,4);
  17523. cell_chk type = (cell_chk)script_getnum(st,5);
  17524. script_pushint(st, map_getcell(m, x, y, type));
  17525. return SCRIPT_CMD_SUCCESS;
  17526. }
  17527. /// Modifies flags of cells in the specified area.
  17528. ///
  17529. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  17530. ///
  17531. /// @see cell_* constants in src/map/script_constants.hpp for the types
  17532. BUILDIN_FUNC(setcell)
  17533. {
  17534. int16 m = map_mapname2mapid(script_getstr(st,2));
  17535. int16 x1 = script_getnum(st,3);
  17536. int16 y1 = script_getnum(st,4);
  17537. int16 x2 = script_getnum(st,5);
  17538. int16 y2 = script_getnum(st,6);
  17539. cell_t type = (cell_t)script_getnum(st,7);
  17540. bool flag = script_getnum(st,8) != 0;
  17541. int32 x,y;
  17542. if( x1 > x2 ) std::swap(x1,x2);
  17543. if( y1 > y2 ) std::swap(y1,y2);
  17544. for( y = y1; y <= y2; ++y )
  17545. for( x = x1; x <= x2; ++x )
  17546. map_setcell(m, x, y, type, flag);
  17547. return SCRIPT_CMD_SUCCESS;
  17548. }
  17549. /**
  17550. * Gets a free cell in the specified area.
  17551. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  17552. */
  17553. BUILDIN_FUNC(getfreecell)
  17554. {
  17555. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  17556. struct script_data
  17557. *data_x = script_getdata(st, 3),
  17558. *data_y = script_getdata(st, 4);
  17559. struct block_list* bl = map_id2bl(st->rid);
  17560. map_session_data *sd = nullptr;
  17561. int16 m, x = 0, y = 0;
  17562. int32 rx = -1, ry = -1, flag = 1;
  17563. if (!data_isreference(data_x)) {
  17564. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  17565. return SCRIPT_CMD_FAILURE;
  17566. }
  17567. if (!data_isreference(data_y)) {
  17568. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  17569. return SCRIPT_CMD_FAILURE;
  17570. }
  17571. name_x = reference_getname(data_x),
  17572. name_y = reference_getname(data_y);
  17573. if (is_string_variable(name_x)) {
  17574. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  17575. return SCRIPT_CMD_FAILURE;
  17576. }
  17577. if (is_string_variable(name_y)) {
  17578. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  17579. return SCRIPT_CMD_FAILURE;
  17580. }
  17581. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  17582. if (!script_rid2sd(sd)) {
  17583. if (not_server_variable(*name_x))
  17584. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  17585. else
  17586. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  17587. return SCRIPT_CMD_FAILURE;
  17588. }
  17589. }
  17590. if (script_hasdata(st, 5))
  17591. x = script_getnum(st, 5);
  17592. if (script_hasdata(st, 6))
  17593. y = script_getnum(st, 6);
  17594. if (script_hasdata(st, 7))
  17595. rx = script_getnum(st, 7);
  17596. if (script_hasdata(st, 8))
  17597. ry = script_getnum(st, 8);
  17598. if (script_hasdata(st, 9))
  17599. flag = script_getnum(st, 9);
  17600. if (bl && strcmp(mapn, "this") == 0)
  17601. m = bl->m;
  17602. else
  17603. m = map_mapname2mapid(mapn);
  17604. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  17605. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  17606. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  17607. return SCRIPT_CMD_SUCCESS;
  17608. }
  17609. /*==========================================
  17610. * Mercenary Commands
  17611. *------------------------------------------*/
  17612. BUILDIN_FUNC(mercenary_create)
  17613. {
  17614. map_session_data *sd;
  17615. int32 class_, contract_time;
  17616. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  17617. return SCRIPT_CMD_SUCCESS;
  17618. class_ = script_getnum(st,2);
  17619. if( !mercenary_db.exists(class_) )
  17620. return SCRIPT_CMD_SUCCESS;
  17621. contract_time = script_getnum(st,3);
  17622. mercenary_create(sd, class_, contract_time);
  17623. return SCRIPT_CMD_SUCCESS;
  17624. }
  17625. BUILDIN_FUNC(mercenary_delete)
  17626. {
  17627. map_session_data *sd;
  17628. int32 type = 0;
  17629. if( !script_charid2sd(2, sd) )
  17630. return SCRIPT_CMD_FAILURE;
  17631. if( sd->md == nullptr ) {
  17632. ShowWarning("buildin_mercenary_delete: Tried to delete a non existant mercenary from player '%s' (AID: %u, CID: %u)\n", sd->status.name, sd->status.account_id, sd->status.char_id);
  17633. return SCRIPT_CMD_FAILURE;
  17634. }
  17635. if( script_hasdata(st, 3) ) {
  17636. type = script_getnum(st, 3);
  17637. if( type < 0 || type > 3 ) {
  17638. ShowWarning("buildin_mercenary_delete: invalid type value of %d.\n", type);
  17639. return SCRIPT_CMD_FAILURE;
  17640. }
  17641. }
  17642. mercenary_delete(sd->md, type);
  17643. return SCRIPT_CMD_SUCCESS;
  17644. }
  17645. BUILDIN_FUNC(mercenary_heal)
  17646. {
  17647. map_session_data *sd;
  17648. int32 hp, sp;
  17649. if( !script_rid2sd(sd) || sd->md == nullptr )
  17650. return SCRIPT_CMD_SUCCESS;
  17651. hp = script_getnum(st,2);
  17652. sp = script_getnum(st,3);
  17653. status_heal(&sd->md->bl, hp, sp, 0);
  17654. return SCRIPT_CMD_SUCCESS;
  17655. }
  17656. BUILDIN_FUNC(mercenary_sc_start)
  17657. {
  17658. map_session_data *sd;
  17659. enum sc_type type;
  17660. int32 tick, val1;
  17661. if( !script_rid2sd(sd) || sd->md == nullptr )
  17662. return SCRIPT_CMD_SUCCESS;
  17663. type = (sc_type)script_getnum(st,2);
  17664. tick = script_getnum(st,3);
  17665. val1 = script_getnum(st,4);
  17666. status_change_start(nullptr, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  17667. return SCRIPT_CMD_SUCCESS;
  17668. }
  17669. BUILDIN_FUNC(mercenary_get_calls)
  17670. {
  17671. map_session_data *sd;
  17672. int32 guild;
  17673. if( !script_rid2sd(sd) )
  17674. return SCRIPT_CMD_SUCCESS;
  17675. guild = script_getnum(st,2);
  17676. switch( guild )
  17677. {
  17678. case ARCH_MERC_GUILD:
  17679. script_pushint(st,sd->status.arch_calls);
  17680. break;
  17681. case SPEAR_MERC_GUILD:
  17682. script_pushint(st,sd->status.spear_calls);
  17683. break;
  17684. case SWORD_MERC_GUILD:
  17685. script_pushint(st,sd->status.sword_calls);
  17686. break;
  17687. default:
  17688. script_pushint(st,0);
  17689. break;
  17690. }
  17691. return SCRIPT_CMD_SUCCESS;
  17692. }
  17693. BUILDIN_FUNC(mercenary_set_calls)
  17694. {
  17695. map_session_data *sd;
  17696. int32 guild, value, *calls;
  17697. if( !script_rid2sd(sd) )
  17698. return SCRIPT_CMD_SUCCESS;
  17699. guild = script_getnum(st,2);
  17700. value = script_getnum(st,3);
  17701. switch( guild )
  17702. {
  17703. case ARCH_MERC_GUILD:
  17704. calls = &sd->status.arch_calls;
  17705. break;
  17706. case SPEAR_MERC_GUILD:
  17707. calls = &sd->status.spear_calls;
  17708. break;
  17709. case SWORD_MERC_GUILD:
  17710. calls = &sd->status.sword_calls;
  17711. break;
  17712. default:
  17713. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  17714. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17715. }
  17716. *calls += value;
  17717. *calls = cap_value(*calls, 0, INT_MAX);
  17718. return SCRIPT_CMD_SUCCESS;
  17719. }
  17720. BUILDIN_FUNC(mercenary_get_faith)
  17721. {
  17722. map_session_data *sd;
  17723. int32 guild;
  17724. if( !script_rid2sd(sd) )
  17725. return SCRIPT_CMD_SUCCESS;
  17726. guild = script_getnum(st,2);
  17727. switch( guild )
  17728. {
  17729. case ARCH_MERC_GUILD:
  17730. script_pushint(st,sd->status.arch_faith);
  17731. break;
  17732. case SPEAR_MERC_GUILD:
  17733. script_pushint(st,sd->status.spear_faith);
  17734. break;
  17735. case SWORD_MERC_GUILD:
  17736. script_pushint(st,sd->status.sword_faith);
  17737. break;
  17738. default:
  17739. script_pushint(st,0);
  17740. break;
  17741. }
  17742. return SCRIPT_CMD_SUCCESS;
  17743. }
  17744. BUILDIN_FUNC(mercenary_set_faith)
  17745. {
  17746. map_session_data *sd;
  17747. int32 guild, value, *calls;
  17748. if( !script_rid2sd(sd) )
  17749. return SCRIPT_CMD_SUCCESS;
  17750. guild = script_getnum(st,2);
  17751. value = script_getnum(st,3);
  17752. switch( guild )
  17753. {
  17754. case ARCH_MERC_GUILD:
  17755. calls = &sd->status.arch_faith;
  17756. break;
  17757. case SPEAR_MERC_GUILD:
  17758. calls = &sd->status.spear_faith;
  17759. break;
  17760. case SWORD_MERC_GUILD:
  17761. calls = &sd->status.sword_faith;
  17762. break;
  17763. default:
  17764. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17765. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17766. }
  17767. *calls += value;
  17768. *calls = cap_value(*calls, 0, INT_MAX);
  17769. if( mercenary_get_guild(sd->md) == guild )
  17770. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17771. return SCRIPT_CMD_SUCCESS;
  17772. }
  17773. /*------------------------------------------
  17774. * Book Reading
  17775. *------------------------------------------*/
  17776. BUILDIN_FUNC(readbook)
  17777. {
  17778. map_session_data *sd;
  17779. int32 book_id, page;
  17780. if( !script_rid2sd(sd) )
  17781. return SCRIPT_CMD_SUCCESS;
  17782. book_id = script_getnum(st,2);
  17783. page = script_getnum(st,3);
  17784. clif_readbook(sd->fd, book_id, page);
  17785. return SCRIPT_CMD_SUCCESS;
  17786. }
  17787. /******************
  17788. Questlog script commands
  17789. *******************/
  17790. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17791. BUILDIN_FUNC(questinfo)
  17792. {
  17793. TBL_NPC* nd = map_id2nd(st->oid);
  17794. if (!nd || nd->bl.m == -1) {
  17795. ShowError("buildin_questinfo: No NPC attached.\n");
  17796. return SCRIPT_CMD_FAILURE;
  17797. }
  17798. int32 icon = script_getnum(st, 2);
  17799. #if PACKETVER >= 20120410
  17800. switch(icon){
  17801. case QTYPE_QUEST:
  17802. case QTYPE_QUEST2:
  17803. case QTYPE_JOB:
  17804. case QTYPE_JOB2:
  17805. case QTYPE_EVENT:
  17806. case QTYPE_EVENT2:
  17807. // Warg icons were replaced in this client
  17808. #if PACKETVER < 20170315
  17809. case QTYPE_WARG:
  17810. case QTYPE_WARG2:
  17811. #else
  17812. case QTYPE_CLICKME:
  17813. case QTYPE_DAILYQUEST:
  17814. case QTYPE_EVENT3:
  17815. case QTYPE_JOBQUEST:
  17816. case QTYPE_JUMPING_PORING:
  17817. #endif
  17818. // Leave everything as it is
  17819. break;
  17820. case QTYPE_NONE:
  17821. default:
  17822. // Default to nothing if icon id is invalid.
  17823. icon = QTYPE_NONE;
  17824. break;
  17825. }
  17826. #else
  17827. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17828. icon = QTYPE_QUEST;
  17829. else
  17830. icon = icon + 1;
  17831. #endif
  17832. int32 color = QMARK_NONE;
  17833. if (script_hasdata(st, 3)) {
  17834. color = script_getnum(st, 3);
  17835. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17836. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17837. script_reportfunc(st);
  17838. color = QMARK_NONE;
  17839. }
  17840. }
  17841. struct script_code *script = nullptr;
  17842. if (script_hasdata(st, 4)) {
  17843. const char *str = script_getstr(st, 4);
  17844. if (str) {
  17845. std::string condition(str);
  17846. if (condition.find( "achievement_condition" ) == std::string::npos)
  17847. condition = "achievement_condition( " + condition + " );";
  17848. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17849. if (!script) {
  17850. st->state = END;
  17851. return SCRIPT_CMD_FAILURE;
  17852. }
  17853. }
  17854. }
  17855. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17856. qi->icon = static_cast<e_questinfo_types>(icon);
  17857. qi->color = static_cast<e_questinfo_markcolor>(color);
  17858. qi->condition = script;
  17859. nd->qi_data.push_back(qi);
  17860. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17861. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17862. mapdata->qi_npc.push_back(nd->bl.id);
  17863. return SCRIPT_CMD_SUCCESS;
  17864. }
  17865. /**
  17866. * questinfo_refresh {<char_id>};
  17867. **/
  17868. BUILDIN_FUNC(questinfo_refresh)
  17869. {
  17870. map_session_data *sd;
  17871. if (!script_charid2sd(2, sd))
  17872. return SCRIPT_CMD_FAILURE;
  17873. pc_show_questinfo(sd);
  17874. return SCRIPT_CMD_SUCCESS;
  17875. }
  17876. /**
  17877. * setquest <ID>{,<char_id>};
  17878. **/
  17879. BUILDIN_FUNC(setquest)
  17880. {
  17881. map_session_data *sd;
  17882. int32 quest_id;
  17883. quest_id = script_getnum(st, 2);
  17884. if (!script_charid2sd(3,sd))
  17885. return SCRIPT_CMD_FAILURE;
  17886. if( quest_add(sd, quest_id) == -1 ){
  17887. script_reportsrc(st);
  17888. script_reportfunc(st);
  17889. }
  17890. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17891. pc_show_questinfo(sd);
  17892. return SCRIPT_CMD_SUCCESS;
  17893. }
  17894. /**
  17895. * erasequest <ID>{,<char_id>};
  17896. **/
  17897. BUILDIN_FUNC(erasequest)
  17898. {
  17899. map_session_data *sd;
  17900. if (!script_charid2sd(3,sd))
  17901. return SCRIPT_CMD_FAILURE;
  17902. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17903. script_reportsrc(st);
  17904. script_reportfunc(st);
  17905. }
  17906. pc_show_questinfo(sd);
  17907. return SCRIPT_CMD_SUCCESS;
  17908. }
  17909. /**
  17910. * completequest <ID>{,<char_id>};
  17911. **/
  17912. BUILDIN_FUNC(completequest)
  17913. {
  17914. map_session_data *sd;
  17915. if (!script_charid2sd(3,sd))
  17916. return SCRIPT_CMD_FAILURE;
  17917. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17918. //20120410 or 20090218
  17919. pc_show_questinfo(sd);
  17920. return SCRIPT_CMD_SUCCESS;
  17921. }
  17922. /**
  17923. * changequest <ID>,<ID2>{,<char_id>};
  17924. **/
  17925. BUILDIN_FUNC(changequest)
  17926. {
  17927. map_session_data *sd;
  17928. if (!script_charid2sd(4,sd))
  17929. return SCRIPT_CMD_FAILURE;
  17930. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17931. script_reportsrc(st);
  17932. script_reportfunc(st);
  17933. }
  17934. //20120410 or 20090218
  17935. pc_show_questinfo(sd);
  17936. return SCRIPT_CMD_SUCCESS;
  17937. }
  17938. /**
  17939. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17940. **/
  17941. BUILDIN_FUNC(checkquest)
  17942. {
  17943. map_session_data *sd;
  17944. e_quest_check_type type = HAVEQUEST;
  17945. if( script_hasdata(st, 3) )
  17946. type = (e_quest_check_type)script_getnum(st, 3);
  17947. if (!script_charid2sd(4,sd))
  17948. return SCRIPT_CMD_FAILURE;
  17949. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17950. return SCRIPT_CMD_SUCCESS;
  17951. }
  17952. /**
  17953. * isbegin_quest(<ID>{,<char_id>})
  17954. **/
  17955. BUILDIN_FUNC(isbegin_quest)
  17956. {
  17957. map_session_data *sd;
  17958. if (!script_charid2sd(3,sd))
  17959. return SCRIPT_CMD_FAILURE;
  17960. int32 i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17961. script_pushint(st, i + (i < 1));
  17962. return SCRIPT_CMD_SUCCESS;
  17963. }
  17964. /**
  17965. * showevent <icon>{,<mark color>{,<char_id>}}
  17966. **/
  17967. BUILDIN_FUNC(showevent)
  17968. {
  17969. TBL_PC *sd;
  17970. if (!script_charid2sd(4,sd))
  17971. return SCRIPT_CMD_FAILURE;
  17972. struct npc_data *nd = map_id2nd(st->oid);
  17973. if (!nd)
  17974. return SCRIPT_CMD_SUCCESS;
  17975. int32 color = QMARK_NONE;
  17976. int32 icon = script_getnum(st, 2);
  17977. if (script_hasdata(st, 3)) {
  17978. color = script_getnum(st, 3);
  17979. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17980. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17981. script_reportfunc(st);
  17982. color = QMARK_NONE;
  17983. }
  17984. }
  17985. #if PACKETVER >= 20120410
  17986. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17987. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17988. #else
  17989. if (icon < 0 || icon > 7)
  17990. icon = 0;
  17991. else
  17992. icon = icon + 1;
  17993. #endif
  17994. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17995. return SCRIPT_CMD_SUCCESS;
  17996. }
  17997. /*==========================================
  17998. * BattleGround System
  17999. *------------------------------------------*/
  18000. BUILDIN_FUNC(waitingroom2bg)
  18001. {
  18002. struct npc_data *nd;
  18003. struct chat_data *cd;
  18004. const char *map_name;
  18005. int32 mapindex = 0, bg_id;
  18006. unsigned char i,c=0;
  18007. struct s_battleground_team team;
  18008. if( script_hasdata(st,7) )
  18009. nd = npc_name2id(script_getstr(st,7));
  18010. else
  18011. nd = (struct npc_data *)map_id2bl(st->oid);
  18012. if( nd == nullptr || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == nullptr )
  18013. {
  18014. script_pushint(st,0);
  18015. return SCRIPT_CMD_SUCCESS;
  18016. }
  18017. map_name = script_getstr(st,2);
  18018. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  18019. { // Invalid Map
  18020. script_pushint(st, 0);
  18021. return SCRIPT_CMD_SUCCESS;
  18022. }
  18023. team.warp_x = script_getnum(st,3);
  18024. team.warp_y = script_getnum(st,4);
  18025. if (script_hasdata(st,5)) {
  18026. team.quit_event = script_getstr(st,5); // Logout Event
  18027. check_event(st, team.quit_event.c_str());
  18028. } else
  18029. team.quit_event = "";
  18030. if (script_hasdata(st,6)) {
  18031. team.death_event = script_getstr(st,6); // Die Event
  18032. check_event(st, team.death_event.c_str());
  18033. } else
  18034. team.death_event = "";
  18035. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  18036. { // Creation failed
  18037. script_pushint(st,0);
  18038. return SCRIPT_CMD_SUCCESS;
  18039. }
  18040. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  18041. map_session_data *sd;
  18042. if( (sd = cd->usersd[i]) != nullptr && bg_team_join(bg_id, sd, false) ){
  18043. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  18044. ++c;
  18045. }
  18046. }
  18047. mapreg_setreg(add_str("$@arenamembersnum"), c);
  18048. script_pushint(st,bg_id);
  18049. return SCRIPT_CMD_SUCCESS;
  18050. }
  18051. BUILDIN_FUNC(waitingroom2bg_single)
  18052. {
  18053. const char* map_name;
  18054. struct npc_data *nd;
  18055. struct chat_data *cd;
  18056. map_session_data *sd;
  18057. int32 x, y, mapindex, bg_id = script_getnum(st,2);
  18058. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18059. if (!bg) {
  18060. script_pushint(st, false);
  18061. return SCRIPT_CMD_SUCCESS;
  18062. }
  18063. if (script_hasdata(st, 3)) {
  18064. map_name = script_getstr(st, 3);
  18065. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  18066. script_pushint(st, false);
  18067. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18068. }
  18069. x = script_getnum(st, 4);
  18070. y = script_getnum(st, 5);
  18071. }
  18072. else {
  18073. mapindex = bg->cemetery.map;
  18074. x = bg->cemetery.x;
  18075. y = bg->cemetery.y;
  18076. }
  18077. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  18078. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  18079. script_pushint(st, false);
  18080. return SCRIPT_CMD_FAILURE;
  18081. }
  18082. nd = npc_name2id(script_getstr(st,6));
  18083. if( nd == nullptr || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == nullptr || cd->users <= 0 )
  18084. return SCRIPT_CMD_SUCCESS;
  18085. if( (sd = cd->usersd[0]) == nullptr )
  18086. return SCRIPT_CMD_SUCCESS;
  18087. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  18088. {
  18089. script_pushint(st, true);
  18090. }
  18091. else
  18092. script_pushint(st, false);
  18093. return SCRIPT_CMD_SUCCESS;
  18094. }
  18095. /// Creates an instance of battleground battle group.
  18096. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  18097. /// @author [secretdataz]
  18098. BUILDIN_FUNC(bg_create) {
  18099. const char *map_name;
  18100. int32 mapindex = 0;
  18101. struct s_battleground_team team;
  18102. map_name = script_getstr(st, 2);
  18103. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  18104. { // Invalid Map
  18105. script_pushint(st, 0);
  18106. return SCRIPT_CMD_SUCCESS;
  18107. }
  18108. team.warp_x = script_getnum(st,3);
  18109. team.warp_y = script_getnum(st,4);
  18110. if (script_hasdata(st,5)) {
  18111. team.quit_event = script_getstr(st,5); // Logout Event
  18112. check_event(st, team.quit_event.c_str());
  18113. } else
  18114. team.quit_event = "";
  18115. if (script_hasdata(st,6)) {
  18116. team.death_event = script_getstr(st,6); // Die Event
  18117. check_event(st, team.death_event.c_str());
  18118. } else
  18119. team.death_event = "";
  18120. script_pushint(st, bg_create(mapindex, &team));
  18121. return SCRIPT_CMD_SUCCESS;
  18122. }
  18123. /// Adds attached player or <char id> (if specified) to an existing
  18124. /// battleground group and warps it to the specified coordinates on
  18125. /// the given map.
  18126. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  18127. /// @author [secretdataz]
  18128. BUILDIN_FUNC(bg_join) {
  18129. const char* map_name;
  18130. map_session_data *sd;
  18131. int32 x, y, mapindex, bg_id = script_getnum(st, 2);
  18132. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18133. if (!bg) {
  18134. script_pushint(st, false);
  18135. return SCRIPT_CMD_SUCCESS;
  18136. }
  18137. if (script_hasdata(st, 3)) {
  18138. map_name = script_getstr(st, 3);
  18139. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  18140. script_pushint(st, false);
  18141. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18142. }
  18143. x = script_getnum(st, 4);
  18144. y = script_getnum(st, 5);
  18145. } else {
  18146. mapindex = bg->cemetery.map;
  18147. x = bg->cemetery.x;
  18148. y = bg->cemetery.y;
  18149. }
  18150. if (!script_charid2sd(6, sd)) {
  18151. script_pushint(st, false);
  18152. return SCRIPT_CMD_FAILURE;
  18153. }
  18154. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  18155. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  18156. script_pushint(st, false);
  18157. return SCRIPT_CMD_FAILURE;
  18158. }
  18159. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  18160. {
  18161. script_pushint(st, true);
  18162. }
  18163. else
  18164. script_pushint(st, false);
  18165. return SCRIPT_CMD_SUCCESS;
  18166. }
  18167. BUILDIN_FUNC(bg_team_setxy)
  18168. {
  18169. int32 bg_id = script_getnum(st,2);
  18170. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18171. if (bg) {
  18172. bg->cemetery.x = script_getnum(st, 3);
  18173. bg->cemetery.y = script_getnum(st, 4);
  18174. }
  18175. return SCRIPT_CMD_SUCCESS;
  18176. }
  18177. BUILDIN_FUNC(bg_warp)
  18178. {
  18179. int32 x, y, mapindex, bg_id;
  18180. const char* map_name;
  18181. bg_id = script_getnum(st,2);
  18182. map_name = script_getstr(st,3);
  18183. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  18184. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18185. x = script_getnum(st,4);
  18186. y = script_getnum(st,5);
  18187. bg_team_warp(bg_id, mapindex, x, y);
  18188. return SCRIPT_CMD_SUCCESS;
  18189. }
  18190. BUILDIN_FUNC(bg_monster)
  18191. {
  18192. int32 class_ = 0, x = 0, y = 0, bg_id = 0;
  18193. const char *str,*mapname, *evt="";
  18194. bg_id = script_getnum(st,2);
  18195. mapname = script_getstr(st,3);
  18196. x = script_getnum(st,4);
  18197. y = script_getnum(st,5);
  18198. str = script_getstr(st,6);
  18199. class_ = script_getnum(st,7);
  18200. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  18201. check_event(st, evt);
  18202. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  18203. return SCRIPT_CMD_SUCCESS;
  18204. }
  18205. BUILDIN_FUNC(bg_monster_set_team)
  18206. {
  18207. struct mob_data *md;
  18208. struct block_list *mbl;
  18209. int32 id = script_getnum(st,2),
  18210. bg_id = script_getnum(st,3);
  18211. if( id == 0 || (mbl = map_id2bl(id)) == nullptr || mbl->type != BL_MOB )
  18212. return SCRIPT_CMD_SUCCESS;
  18213. md = (TBL_MOB *)mbl;
  18214. md->bg_id = bg_id;
  18215. mob_stop_attack(md);
  18216. mob_stop_walking(md, 0);
  18217. md->target_id = md->attacked_id = 0;
  18218. clif_name_area(&md->bl);
  18219. return SCRIPT_CMD_SUCCESS;
  18220. }
  18221. BUILDIN_FUNC(bg_leave)
  18222. {
  18223. map_session_data *sd = nullptr;
  18224. bool deserter = false;
  18225. if( !script_charid2sd(2,sd) || !sd->bg_id )
  18226. return SCRIPT_CMD_SUCCESS;
  18227. if (!strcmp(script_getfuncname(st), "bg_desert"))
  18228. deserter = true;
  18229. bg_team_leave(sd, false, deserter);
  18230. return SCRIPT_CMD_SUCCESS;
  18231. }
  18232. BUILDIN_FUNC(bg_destroy)
  18233. {
  18234. int32 bg_id = script_getnum(st,2);
  18235. bg_team_delete(bg_id);
  18236. return SCRIPT_CMD_SUCCESS;
  18237. }
  18238. BUILDIN_FUNC(bg_getareausers)
  18239. {
  18240. const char *str = script_getstr(st, 3);
  18241. int16 m, x0, y0, x1, y1;
  18242. int32 bg_id = script_getnum(st, 2), c = 0;
  18243. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18244. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  18245. script_pushint(st,0);
  18246. return SCRIPT_CMD_SUCCESS;
  18247. }
  18248. x0 = script_getnum(st,4);
  18249. y0 = script_getnum(st,5);
  18250. x1 = script_getnum(st,6);
  18251. y1 = script_getnum(st,7);
  18252. for (const auto &member : bg->members) {
  18253. if( member.sd->bl.m != m || member.sd->bl.x < x0 || member.sd->bl.y < y0 || member.sd->bl.x > x1 || member.sd->bl.y > y1 )
  18254. continue;
  18255. c++;
  18256. }
  18257. script_pushint(st,c);
  18258. return SCRIPT_CMD_SUCCESS;
  18259. }
  18260. BUILDIN_FUNC(bg_updatescore)
  18261. {
  18262. const char *str;
  18263. int16 m;
  18264. str = script_getstr(st,2);
  18265. if( (m = map_mapname2mapid(str)) < 0 )
  18266. return SCRIPT_CMD_SUCCESS;
  18267. struct map_data *mapdata = map_getmapdata(m);
  18268. mapdata->bgscore_lion = script_getnum(st,3);
  18269. mapdata->bgscore_eagle = script_getnum(st,4);
  18270. clif_bg_updatescore(m);
  18271. return SCRIPT_CMD_SUCCESS;
  18272. }
  18273. BUILDIN_FUNC(bg_get_data)
  18274. {
  18275. int32 bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  18276. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18277. if (bg) {
  18278. switch (type) {
  18279. case 0:
  18280. script_pushint(st, bg->members.size());
  18281. break;
  18282. case 1:
  18283. for (const auto &member : bg->members)
  18284. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  18285. mapreg_setreg(add_str("$@arenamemberscount"), i);
  18286. script_pushint(st, i);
  18287. break;
  18288. default:
  18289. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  18290. break;
  18291. }
  18292. } else
  18293. script_pushint(st, 0);
  18294. return SCRIPT_CMD_SUCCESS;
  18295. }
  18296. /**
  18297. * Reserves a slot for the given Battleground.
  18298. * bg_reserve("<battleground_map_name>"{,<ended>});
  18299. */
  18300. BUILDIN_FUNC(bg_reserve)
  18301. {
  18302. const char *str = script_getstr(st, 2);
  18303. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  18304. if (!bg_queue_reserve(str, ended))
  18305. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  18306. return SCRIPT_CMD_SUCCESS;
  18307. }
  18308. /**
  18309. * Removes a spot for the given Battleground.
  18310. * bg_unbook("<battleground_map_name>");
  18311. */
  18312. BUILDIN_FUNC(bg_unbook)
  18313. {
  18314. const char *str = script_getstr(st, 2);
  18315. if (!bg_queue_unbook(str))
  18316. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  18317. return SCRIPT_CMD_SUCCESS;
  18318. }
  18319. /**
  18320. * Gets battleground database information.
  18321. * bg_info("<battleground name>", <type>);
  18322. */
  18323. BUILDIN_FUNC(bg_info)
  18324. {
  18325. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  18326. if (!bg) {
  18327. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  18328. return SCRIPT_CMD_FAILURE;
  18329. }
  18330. int32 type = script_getnum(st, 3);
  18331. switch (type) {
  18332. case BG_INFO_ID:
  18333. script_pushint(st, bg->id);
  18334. break;
  18335. case BG_INFO_REQUIRED_PLAYERS:
  18336. script_pushint(st, bg->required_players);
  18337. break;
  18338. case BG_INFO_MAX_PLAYERS:
  18339. script_pushint(st, bg->max_players);
  18340. break;
  18341. case BG_INFO_MIN_LEVEL:
  18342. script_pushint(st, bg->min_lvl);
  18343. break;
  18344. case BG_INFO_MAX_LEVEL:
  18345. script_pushint(st, bg->max_lvl);
  18346. break;
  18347. case BG_INFO_MAPS: {
  18348. size_t i;
  18349. for( i = 0; i < bg->maps.size(); i++ ){
  18350. setd_sub_str( st, nullptr, ".@bgmaps$", static_cast<int>( i ), mapindex_id2name( bg->maps[i].mapindex ), nullptr );
  18351. }
  18352. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  18353. script_pushint(st, i);
  18354. break;
  18355. }
  18356. case BG_INFO_DESERTER_TIME:
  18357. script_pushint(st, bg->deserter_time);
  18358. break;
  18359. default:
  18360. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  18361. return SCRIPT_CMD_FAILURE;
  18362. }
  18363. return SCRIPT_CMD_SUCCESS;
  18364. }
  18365. /*==========================================
  18366. * Instancing System
  18367. *------------------------------------------*/
  18368. /**
  18369. * Returns an Instance ID.
  18370. * @param st: Script state
  18371. * @param mode: Instance mode
  18372. * @return instance ID on success or 0 otherwise
  18373. */
  18374. int32 script_instancegetid(struct script_state* st, e_instance_mode mode)
  18375. {
  18376. int32 instance_id = 0;
  18377. if (mode == IM_NONE) {
  18378. struct npc_data *nd = map_id2nd(st->oid);
  18379. if (nd && nd->instance_id > 0)
  18380. instance_id = nd->instance_id;
  18381. } else {
  18382. map_session_data *sd = map_id2sd(st->rid);
  18383. if (sd) {
  18384. switch (mode) {
  18385. case IM_CHAR:
  18386. if (sd->instance_id > 0)
  18387. instance_id = sd->instance_id;
  18388. break;
  18389. case IM_PARTY: {
  18390. struct party_data *pd = party_search(sd->status.party_id);
  18391. if (pd && pd->instance_id > 0)
  18392. instance_id = pd->instance_id;
  18393. }
  18394. break;
  18395. case IM_GUILD: {
  18396. auto gd = guild_search(sd->status.guild_id);
  18397. if (gd && gd->instance_id > 0)
  18398. instance_id = gd->instance_id;
  18399. }
  18400. break;
  18401. case IM_CLAN: {
  18402. struct clan *cd = clan_search(sd->status.clan_id);
  18403. if (cd && cd->instance_id > 0)
  18404. instance_id = cd->instance_id;
  18405. }
  18406. break;
  18407. default: // Unsupported type
  18408. break;
  18409. }
  18410. }
  18411. }
  18412. return instance_id;
  18413. }
  18414. /*==========================================
  18415. * Creates the instance
  18416. * Returns Instance ID if created successfully
  18417. *------------------------------------------*/
  18418. BUILDIN_FUNC(instance_create)
  18419. {
  18420. e_instance_mode mode = IM_PARTY;
  18421. int32 owner_id = 0;
  18422. if (script_hasdata(st, 3)) {
  18423. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18424. if (mode < IM_NONE || mode >= IM_MAX) {
  18425. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  18426. return SCRIPT_CMD_FAILURE;
  18427. }
  18428. }
  18429. if (script_hasdata(st, 4))
  18430. owner_id = script_getnum(st, 4);
  18431. else {
  18432. // If sd is nullptr, instance_create will return -2.
  18433. map_session_data *sd = nullptr;
  18434. switch(mode) {
  18435. case IM_NONE:
  18436. owner_id = st->oid;
  18437. break;
  18438. case IM_CHAR:
  18439. if (script_rid2sd(sd))
  18440. owner_id = sd->status.char_id;
  18441. break;
  18442. case IM_PARTY:
  18443. if (script_rid2sd(sd))
  18444. owner_id = sd->status.party_id;
  18445. break;
  18446. case IM_GUILD:
  18447. if (script_rid2sd(sd))
  18448. owner_id = sd->status.guild_id;
  18449. break;
  18450. case IM_CLAN:
  18451. if (script_rid2sd(sd))
  18452. owner_id = sd->status.clan_id;
  18453. break;
  18454. default:
  18455. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  18456. return SCRIPT_CMD_FAILURE;
  18457. }
  18458. }
  18459. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  18460. return SCRIPT_CMD_SUCCESS;
  18461. }
  18462. /*==========================================
  18463. * Destroys an instance (unofficial)
  18464. * Officially instances are only destroyed by timeout
  18465. *
  18466. * instance_destroy {<instance_id>};
  18467. *------------------------------------------*/
  18468. BUILDIN_FUNC(instance_destroy)
  18469. {
  18470. int32 instance_id;
  18471. if( script_hasdata(st,2) )
  18472. instance_id = script_getnum(st,2);
  18473. else
  18474. instance_id = script_instancegetid(st);
  18475. if( instance_id == 0 ) {
  18476. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  18477. return SCRIPT_CMD_FAILURE;
  18478. }
  18479. instance_destroy(instance_id);
  18480. return SCRIPT_CMD_SUCCESS;
  18481. }
  18482. /*==========================================
  18483. * Warps player to instance
  18484. * Results:
  18485. * IE_OK: Success
  18486. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  18487. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  18488. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  18489. *------------------------------------------*/
  18490. BUILDIN_FUNC(instance_enter)
  18491. {
  18492. map_session_data *sd = nullptr;
  18493. int32 x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  18494. int32 y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  18495. int32 instance_id;
  18496. if (script_hasdata(st, 6))
  18497. instance_id = script_getnum(st, 6);
  18498. else
  18499. instance_id = script_instancegetid(st, IM_PARTY);
  18500. if (!script_charid2sd(5,sd))
  18501. return SCRIPT_CMD_FAILURE;
  18502. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  18503. return SCRIPT_CMD_SUCCESS;
  18504. }
  18505. /*==========================================
  18506. * Returns the name of a duplicated NPC
  18507. *
  18508. * instance_npcname <npc_name>{,<instance_id>};
  18509. * <npc_name> is the full name of an NPC.
  18510. *------------------------------------------*/
  18511. BUILDIN_FUNC(instance_npcname)
  18512. {
  18513. const char *str;
  18514. int32 instance_id;
  18515. struct npc_data *nd;
  18516. str = script_getstr(st,2);
  18517. if( script_hasdata(st,3) )
  18518. instance_id = script_getnum(st,3);
  18519. else
  18520. instance_id = script_instancegetid(st);
  18521. if( instance_id > 0 && (nd = npc_name2id(str)) != nullptr ) {
  18522. static char npcname[NAME_LENGTH];
  18523. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  18524. script_pushconststr(st,npcname);
  18525. } else {
  18526. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  18527. st->state = END;
  18528. return SCRIPT_CMD_FAILURE;
  18529. }
  18530. return SCRIPT_CMD_SUCCESS;
  18531. }
  18532. /*==========================================
  18533. * Returns the name of a duplicated map
  18534. *
  18535. * instance_mapname <map_name>{,<instance_id>};
  18536. *------------------------------------------*/
  18537. BUILDIN_FUNC(instance_mapname)
  18538. {
  18539. const char *str;
  18540. int16 m;
  18541. int32 instance_id;
  18542. str = script_getstr(st,2);
  18543. if( script_hasdata(st,3) )
  18544. instance_id = script_getnum(st,3);
  18545. else
  18546. instance_id = script_instancegetid(st);
  18547. // Check that instance mapname is a valid map
  18548. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  18549. script_pushconststr(st, "");
  18550. else
  18551. script_pushconststr(st, map_getmapdata(m)->name);
  18552. return SCRIPT_CMD_SUCCESS;
  18553. }
  18554. /*==========================================
  18555. * Returns an Instance ID
  18556. *------------------------------------------*/
  18557. BUILDIN_FUNC(instance_id)
  18558. {
  18559. int32 mode = IM_NONE; // Default to the attached NPC
  18560. if (script_hasdata(st, 2)) {
  18561. mode = script_getnum(st, 2);
  18562. if (mode <= IM_NONE || mode >= IM_MAX) {
  18563. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  18564. script_pushint(st, 0);
  18565. return SCRIPT_CMD_SUCCESS;
  18566. }
  18567. }
  18568. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  18569. return SCRIPT_CMD_SUCCESS;
  18570. }
  18571. /*==========================================
  18572. * Warps all players inside an instance
  18573. *
  18574. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  18575. *------------------------------------------*/
  18576. static int32 buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  18577. {
  18578. uint32 m = va_arg(ap,uint32);
  18579. int32 x = va_arg(ap,int);
  18580. int32 y = va_arg(ap,int);
  18581. int32 instance_id = va_arg(ap, int);
  18582. int32 flag = va_arg(ap, int);
  18583. map_session_data *sd;
  18584. nullpo_retr(0, bl);
  18585. if (bl->type != BL_PC)
  18586. return 0;
  18587. sd = (TBL_PC *)bl;
  18588. if ((flag & IWA_NOTDEAD) != 0 && pc_isdead(sd))
  18589. return 0;
  18590. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18591. if (!idata)
  18592. return 0;
  18593. int32 owner_id = idata->owner_id;
  18594. switch(idata->mode) {
  18595. case IM_NONE:
  18596. break;
  18597. case IM_CHAR:
  18598. if (sd->status.char_id != owner_id)
  18599. return 0;
  18600. break;
  18601. case IM_PARTY:
  18602. if (sd->status.party_id != owner_id)
  18603. return 0;
  18604. break;
  18605. case IM_GUILD:
  18606. if (sd->status.guild_id != owner_id)
  18607. return 0;
  18608. break;
  18609. case IM_CLAN:
  18610. if (sd->status.clan_id != owner_id)
  18611. return 0;
  18612. break;
  18613. }
  18614. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  18615. return 1;
  18616. }
  18617. BUILDIN_FUNC(instance_warpall)
  18618. {
  18619. int16 m;
  18620. int32 instance_id;
  18621. const char *mapn = script_getstr(st,2);
  18622. if( script_hasdata(st,5) )
  18623. instance_id = script_getnum(st,5);
  18624. else
  18625. instance_id = script_instancegetid(st, IM_PARTY);
  18626. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0) {
  18627. ShowError("buildin_instance_warpall: Instance map for instance ID %d is not found.\n", instance_id);
  18628. return SCRIPT_CMD_FAILURE;
  18629. }
  18630. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18631. if (!idata) {
  18632. ShowError("buildin_instance_warpall: Instance is not found.\n");
  18633. return SCRIPT_CMD_FAILURE;
  18634. }
  18635. int32 flag = IWA_NONE;
  18636. int32 x = script_getnum(st,3);
  18637. int32 y = script_getnum(st,4);
  18638. if( script_hasdata(st, 6) )
  18639. flag = script_getnum(st, 6);
  18640. for(const auto &it : idata->map)
  18641. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id, flag);
  18642. return SCRIPT_CMD_SUCCESS;
  18643. }
  18644. /*==========================================
  18645. * Broadcasts to all maps inside an instance
  18646. *
  18647. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  18648. * Using 0 for <instance id> will auto-detect the id.
  18649. *------------------------------------------*/
  18650. BUILDIN_FUNC(instance_announce) {
  18651. int32 instance_id = script_getnum(st,2);
  18652. const char *mes = script_getstr(st,3);
  18653. int32 flag = script_getnum(st,4);
  18654. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : nullptr;
  18655. int32 fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  18656. int32 fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  18657. int32 fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  18658. int32 fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  18659. if (instance_id <= 0)
  18660. instance_id = script_instancegetid(st);
  18661. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18662. if (instance_id <= 0 || !idata) {
  18663. ShowError("buildin_instance_announce: Instance not found.\n");
  18664. return SCRIPT_CMD_FAILURE;
  18665. }
  18666. for (const auto &it : idata->map)
  18667. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  18668. return SCRIPT_CMD_SUCCESS;
  18669. }
  18670. /*==========================================
  18671. * instance_check_party [malufett]
  18672. * Values:
  18673. * party_id : Party ID of the invoking character. [Required Parameter]
  18674. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  18675. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18676. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18677. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  18678. * Example 2: instance_check_party (getcharid(1),1,1,99);
  18679. *------------------------------------------*/
  18680. BUILDIN_FUNC(instance_check_party)
  18681. {
  18682. int32 min; // Minimum Level needed to join the Instance.
  18683. int32 max; // Maxium Level allowed to join the Instance.
  18684. int32 amount; // Amount of needed Partymembers for the Instance.
  18685. if (!script_hasdata(st, 3))
  18686. amount = 1;
  18687. else {
  18688. amount = script_getnum(st, 3);
  18689. if (amount < 1 || amount > MAX_PARTY) {
  18690. ShowError("buildin_instance_check_party: Invalid amount %d. Min: 1, max: %d.\n", amount, MAX_PARTY);
  18691. st->state = END;
  18692. return SCRIPT_CMD_FAILURE;
  18693. }
  18694. }
  18695. if (!script_hasdata(st, 4))
  18696. min = 1;
  18697. else {
  18698. min = script_getnum(st, 4);
  18699. if (min < 1 || min > MAX_LEVEL) {
  18700. ShowError("buildin_instance_check_party: Invalid min level %d. Min: 1, max: %d.\n", min, MAX_LEVEL);
  18701. st->state = END;
  18702. return SCRIPT_CMD_FAILURE;
  18703. }
  18704. }
  18705. if (!script_hasdata(st, 5))
  18706. max = MAX_LEVEL;
  18707. else {
  18708. max = script_getnum(st, 5);
  18709. if (max < 1 || max > MAX_LEVEL) {
  18710. ShowError("buildin_instance_check_party: Invalid max level %d. Min: 1, max: %d.\n", max, MAX_LEVEL);
  18711. st->state = END;
  18712. return SCRIPT_CMD_FAILURE;
  18713. }
  18714. }
  18715. int32 party_id = script_getnum(st, 2);
  18716. party_data *p = party_search( party_id );
  18717. if (p == nullptr) {
  18718. script_pushint(st, 0);
  18719. return SCRIPT_CMD_SUCCESS;
  18720. }
  18721. int32 count = 0;
  18722. for( size_t i = 0; i < MAX_PARTY; i++ ) {
  18723. map_session_data *sd = p->data[i].sd;
  18724. if (sd == nullptr){
  18725. continue;
  18726. }
  18727. if (sd->state.autotrade) {
  18728. continue;
  18729. }
  18730. if (sd->status.base_level < min || sd->status.base_level > max) {
  18731. script_pushint(st, 0);
  18732. return SCRIPT_CMD_SUCCESS;
  18733. }
  18734. count++;
  18735. }
  18736. script_pushint(st, count >= amount);
  18737. return SCRIPT_CMD_SUCCESS;
  18738. }
  18739. /*==========================================
  18740. * instance_check_guild
  18741. * Values:
  18742. * guild_id : Guild ID of the invoking character. [Required Parameter]
  18743. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  18744. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18745. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18746. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  18747. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  18748. *------------------------------------------*/
  18749. BUILDIN_FUNC(instance_check_guild)
  18750. {
  18751. int32 amount, min, max, i, guild_id = 0, c = 0;
  18752. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18753. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18754. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18755. if (min < 1 || min > MAX_LEVEL) {
  18756. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18757. return SCRIPT_CMD_FAILURE;
  18758. } else if (max < 1 || max > MAX_LEVEL) {
  18759. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18760. return SCRIPT_CMD_FAILURE;
  18761. }
  18762. if (script_hasdata(st,2))
  18763. guild_id = script_getnum(st,2);
  18764. else
  18765. return SCRIPT_CMD_FAILURE;
  18766. auto g = guild_search(guild_id);
  18767. if (!g) {
  18768. script_pushint(st, 0); // Returns false if guild does not exist.
  18769. return SCRIPT_CMD_FAILURE;
  18770. }
  18771. for(i = 0; i < MAX_GUILD; i++) {
  18772. map_session_data *pl_sd;
  18773. if ((pl_sd = g->guild.member[i].sd)) {
  18774. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18775. if (pl_sd->status.base_level < min) {
  18776. script_pushint(st, 0);
  18777. return SCRIPT_CMD_SUCCESS;
  18778. } else if (pl_sd->status.base_level > max) {
  18779. script_pushint(st, 0);
  18780. return SCRIPT_CMD_SUCCESS;
  18781. }
  18782. c++;
  18783. }
  18784. }
  18785. }
  18786. if (c < amount)
  18787. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18788. else
  18789. script_pushint(st, 1);
  18790. return SCRIPT_CMD_SUCCESS;
  18791. }
  18792. /*==========================================
  18793. * instance_check_clan
  18794. * Values:
  18795. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18796. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18797. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18798. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18799. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18800. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18801. *------------------------------------------*/
  18802. BUILDIN_FUNC(instance_check_clan)
  18803. {
  18804. int32 amount, min, max, i, clan_id = 0, c = 0;
  18805. struct clan *cd = nullptr;
  18806. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18807. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18808. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18809. if (min < 1 || min > MAX_LEVEL) {
  18810. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18811. return SCRIPT_CMD_FAILURE;
  18812. } else if (max < 1 || max > MAX_LEVEL) {
  18813. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18814. return SCRIPT_CMD_FAILURE;
  18815. }
  18816. if (script_hasdata(st,2))
  18817. clan_id = script_getnum(st,2);
  18818. else
  18819. return SCRIPT_CMD_FAILURE;
  18820. if (!(cd = clan_search(clan_id))) {
  18821. script_pushint(st, 0); // Returns false if clan does not exist.
  18822. return SCRIPT_CMD_FAILURE;
  18823. }
  18824. for(i = 0; i < MAX_CLAN; i++) {
  18825. map_session_data *pl_sd;
  18826. if ((pl_sd = cd->members[i])) {
  18827. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18828. if (pl_sd->status.base_level < min) {
  18829. script_pushint(st, 0);
  18830. return SCRIPT_CMD_SUCCESS;
  18831. } else if (pl_sd->status.base_level > max) {
  18832. script_pushint(st, 0);
  18833. return SCRIPT_CMD_SUCCESS;
  18834. }
  18835. c++;
  18836. }
  18837. }
  18838. }
  18839. if (c < amount)
  18840. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18841. else
  18842. script_pushint(st, 1);
  18843. return SCRIPT_CMD_SUCCESS;
  18844. }
  18845. /*==========================================
  18846. * instance_info
  18847. * Values:
  18848. * name : name of the instance you want to look up. [Required Parameter]
  18849. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18850. * index : index of the map in the instance. [Optional Parameter]
  18851. *------------------------------------------*/
  18852. BUILDIN_FUNC(instance_info)
  18853. {
  18854. const char* name = script_getstr(st, 2);
  18855. int32 type = script_getnum(st, 3);
  18856. int32 index = 0;
  18857. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18858. if (!db) {
  18859. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18860. script_pushint(st, -1);
  18861. return SCRIPT_CMD_FAILURE;
  18862. }
  18863. switch( type ){
  18864. case IIT_ID:
  18865. script_pushint(st, db->id);
  18866. break;
  18867. case IIT_TIME_LIMIT:
  18868. script_pushint(st, db->limit);
  18869. break;
  18870. case IIT_IDLE_TIMEOUT:
  18871. script_pushint(st, db->timeout);
  18872. break;
  18873. case IIT_ENTER_MAP:
  18874. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18875. break;
  18876. case IIT_ENTER_X:
  18877. script_pushint(st, db->enter.x);
  18878. break;
  18879. case IIT_ENTER_Y:
  18880. script_pushint(st, db->enter.y);
  18881. break;
  18882. case IIT_MAPCOUNT:
  18883. script_pushint(st, db->maplist.size());
  18884. break;
  18885. case IIT_MAP:
  18886. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18887. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18888. script_pushconststr(st, "");
  18889. return SCRIPT_CMD_FAILURE;
  18890. }
  18891. index = script_getnum(st, 4);
  18892. if( index < 0 ){
  18893. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18894. script_pushconststr(st, "");
  18895. return SCRIPT_CMD_FAILURE;
  18896. }
  18897. if( index > UINT8_MAX ){
  18898. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18899. script_pushconststr(st, "");
  18900. return SCRIPT_CMD_FAILURE;
  18901. }
  18902. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18903. break;
  18904. default:
  18905. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18906. script_pushint(st, -1);
  18907. return SCRIPT_CMD_FAILURE;
  18908. }
  18909. return SCRIPT_CMD_SUCCESS;
  18910. }
  18911. /*------------------------------------------
  18912. *instance_live_info( <info type>{, <instance id>} );
  18913. - ILI_NAME : Instance Name
  18914. - ILI_MODE : Instance Mode
  18915. - ILI_OWNER : owner id
  18916. *------------------------------------------*/
  18917. BUILDIN_FUNC(instance_live_info)
  18918. {
  18919. int32 type = script_getnum(st, 2);
  18920. int32 id = 0;
  18921. if (type < ILI_NAME || type > ILI_OWNER) {
  18922. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18923. script_pushint(st, -1);
  18924. return SCRIPT_CMD_FAILURE;
  18925. }
  18926. if (!script_hasdata(st, 3))
  18927. id = script_instancegetid(st);
  18928. else
  18929. id = script_getnum(st, 3);
  18930. std::shared_ptr<s_instance_db> db = nullptr;
  18931. std::shared_ptr<s_instance_data> im = nullptr;
  18932. if (id > 0 && id < INT_MAX) {
  18933. im = util::umap_find(instances, id);
  18934. if (im)
  18935. db = instance_db.find(im->id);
  18936. }
  18937. if (!im || !db) {
  18938. if (type == ILI_NAME)
  18939. script_pushconststr(st, "");
  18940. else
  18941. script_pushint(st, -1);
  18942. return SCRIPT_CMD_SUCCESS;
  18943. }
  18944. switch( type ) {
  18945. case ILI_NAME:
  18946. script_pushstrcopy(st, db->name.c_str());
  18947. break;
  18948. case ILI_MODE:
  18949. script_pushint(st, im->mode);
  18950. break;
  18951. case ILI_OWNER:
  18952. script_pushint(st, im->owner_id);
  18953. break;
  18954. }
  18955. return SCRIPT_CMD_SUCCESS;
  18956. }
  18957. /*==========================================
  18958. * instance_list(<"map name">{,<instance mode>});
  18959. * set '.@instance_list' to a list of the live instance ids for the map with the mode.
  18960. * return the array size of '.@instance_list'
  18961. *------------------------------------------*/
  18962. BUILDIN_FUNC(instance_list)
  18963. {
  18964. int32 src_id = map_mapname2mapid(script_getstr(st, 2));
  18965. if (src_id == 0) {
  18966. ShowError("buildin_instance_list: map '%s' doesn't exist\n", script_getstr(st, 2));
  18967. return SCRIPT_CMD_FAILURE;
  18968. }
  18969. e_instance_mode mode = IM_MAX;
  18970. if (script_hasdata(st, 3)) {
  18971. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18972. if (mode < IM_NONE || mode >= IM_MAX) {
  18973. ShowError("buildin_instance_list: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 3));
  18974. return SCRIPT_CMD_FAILURE;
  18975. }
  18976. }
  18977. int32 j = 0;
  18978. for (int32 i = instance_start; i < map_num; i++) {
  18979. struct map_data* mapdata = &map[i];
  18980. if (mapdata->instance_src_map == src_id) {
  18981. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, mapdata->instance_id);
  18982. if (idata && (mode == IM_MAX || idata->mode == mode)) {
  18983. setd_sub_num(st, nullptr, ".@instance_list", j, mapdata->instance_id, nullptr);
  18984. j++;
  18985. }
  18986. }
  18987. }
  18988. script_pushint(st, j);
  18989. return SCRIPT_CMD_SUCCESS;
  18990. }
  18991. /*==========================================
  18992. * Custom Fonts
  18993. *------------------------------------------*/
  18994. BUILDIN_FUNC(setfont)
  18995. {
  18996. map_session_data *sd;
  18997. int32 font;
  18998. if( !script_rid2sd(sd) )
  18999. return SCRIPT_CMD_SUCCESS;
  19000. font = script_getnum(st,2);
  19001. if( sd->status.font != font )
  19002. sd->status.font = font;
  19003. else
  19004. sd->status.font = 0;
  19005. clif_font(sd);
  19006. return SCRIPT_CMD_SUCCESS;
  19007. }
  19008. static int32 buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  19009. {
  19010. TBL_MOB* md = (TBL_MOB*)bl;
  19011. struct block_list *tbl;
  19012. int32 mobid = va_arg(ap,int);
  19013. uint16 skill_id = va_arg(ap,int);
  19014. uint16 skill_lv = va_arg(ap,int);
  19015. int32 casttime = va_arg(ap,int);
  19016. int32 cancel = va_arg(ap,int);
  19017. int32 emotion = va_arg(ap,int);
  19018. int32 target = va_arg(ap,int);
  19019. if( md->mob_id != mobid )
  19020. return 0;
  19021. // 0:self, 1:target, 2:master, default:random
  19022. switch( target ) {
  19023. case 0: tbl = map_id2bl(md->bl.id); break;
  19024. case 1: tbl = map_id2bl(md->target_id); break;
  19025. case 2: tbl = map_id2bl(md->master_id); break;
  19026. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  19027. }
  19028. if( !tbl )
  19029. return 0;
  19030. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  19031. unit_skillcastcancel(bl,0);
  19032. if( skill_get_casttype(skill_id) == CAST_GROUND )
  19033. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  19034. else
  19035. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  19036. clif_emotion(&md->bl, emotion);
  19037. return 1;
  19038. }
  19039. /*==========================================
  19040. * areamobuseskill "<Map Name>",<x>,<y>,<range>,"<Mob name>"/<Mob ID>,"<Skill Name>"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  19041. *------------------------------------------*/
  19042. BUILDIN_FUNC(areamobuseskill)
  19043. {
  19044. struct block_list center;
  19045. int16 m;
  19046. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  19047. ShowError("areamobuseskill: invalid map name.\n");
  19048. return SCRIPT_CMD_SUCCESS;
  19049. }
  19050. center.m = m;
  19051. center.x = script_getnum(st,3);
  19052. center.y = script_getnum(st,4);
  19053. int32 range = script_getnum( st,5 );
  19054. uint16 mobid;
  19055. if( script_isstring( st, 6 ) ){
  19056. const char* name = script_getstr( st, 6 );
  19057. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  19058. if( mob == nullptr ){
  19059. ShowWarning( "buildin_areamobuseskill: Attempted to use skill of non-existing monster \"%s\"\n", name );
  19060. return SCRIPT_CMD_FAILURE;
  19061. }
  19062. mobid = mob->id;
  19063. }else{
  19064. mobid = script_getnum( st, 6 );
  19065. if( !mob_db.exists( mobid ) ){
  19066. ShowWarning( "buildin_areamobuseskill: Attempted to use skill of non-existing monster class %d\n", mobid );
  19067. return SCRIPT_CMD_FAILURE;
  19068. }
  19069. }
  19070. int32 skill_id;
  19071. if (script_isstring(st, 7)) {
  19072. const char *name = script_getstr(st, 7);
  19073. if (!(skill_id = skill_name2id(name))) {
  19074. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  19075. return SCRIPT_CMD_FAILURE;
  19076. }
  19077. } else {
  19078. skill_id = script_getnum(st, 7);
  19079. if (!skill_get_index(skill_id)) {
  19080. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  19081. return SCRIPT_CMD_FAILURE;
  19082. }
  19083. }
  19084. int32 skill_lv = script_getnum( st, 8 );
  19085. if( skill_lv > battle_config.mob_max_skilllvl ){
  19086. skill_lv = battle_config.mob_max_skilllvl;
  19087. }
  19088. int32 casttime = script_getnum( st, 9 );
  19089. int32 cancel = script_getnum( st, 10 );
  19090. int32 emotion = script_getnum( st, 11 );
  19091. int32 target = script_getnum( st, 12 );
  19092. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  19093. return SCRIPT_CMD_SUCCESS;
  19094. }
  19095. /**
  19096. * Display a progress bar above a character
  19097. * progressbar "<color>",<seconds>;
  19098. */
  19099. BUILDIN_FUNC(progressbar)
  19100. {
  19101. map_session_data * sd;
  19102. const char * color;
  19103. uint32 second;
  19104. if( !script_rid2sd(sd) )
  19105. return SCRIPT_CMD_SUCCESS;
  19106. st->state = STOP;
  19107. color = script_getstr(st,2);
  19108. second = script_getnum(st,3);
  19109. sd->progressbar.npc_id = st->oid;
  19110. sd->progressbar.timeout = gettick() + second*1000;
  19111. sd->state.workinprogress = WIP_DISABLE_ALL;
  19112. clif_progressbar(sd, strtol(color, (char **)nullptr, 0), second);
  19113. return SCRIPT_CMD_SUCCESS;
  19114. }
  19115. /**
  19116. * Display a progress bar above an NPC
  19117. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  19118. */
  19119. BUILDIN_FUNC(progressbar_npc){
  19120. map_session_data *sd = map_id2sd(st->rid);
  19121. struct npc_data* nd = nullptr;
  19122. if( script_hasdata(st, 4) ){
  19123. const char* name = script_getstr(st, 4);
  19124. nd = npc_name2id(name);
  19125. if( !nd ){
  19126. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  19127. return SCRIPT_CMD_FAILURE;
  19128. }
  19129. }else{
  19130. nd = map_id2nd(st->oid);
  19131. }
  19132. // First call(by function call)
  19133. if( !nd->progressbar.timeout ){
  19134. const char *color;
  19135. int32 second;
  19136. color = script_getstr(st, 2);
  19137. second = script_getnum(st, 3);
  19138. if( second < 0 ){
  19139. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  19140. return SCRIPT_CMD_FAILURE;
  19141. }
  19142. if (sd) { // Player attached - keep them from doing other things
  19143. sd->state.workinprogress = WIP_DISABLE_ALL;
  19144. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  19145. }
  19146. // sleep for the target amount of time
  19147. st->state = RERUNLINE;
  19148. st->sleep.tick = second * 1000;
  19149. nd->progressbar.timeout = gettick() + second * 1000;
  19150. nd->progressbar.color = strtol(color, (char **)nullptr, 0);
  19151. clif_progressbar_npc_area(nd);
  19152. // Second call(by timer after sleeping time is over)
  19153. } else {
  19154. // Continue the script
  19155. if (sd) { // Player attached - remove restrictions
  19156. sd->state.workinprogress = WIP_DISABLE_NONE;
  19157. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  19158. }
  19159. st->state = RUN;
  19160. st->sleep.tick = 0;
  19161. nd->progressbar.timeout = nd->progressbar.color = 0;
  19162. }
  19163. return SCRIPT_CMD_SUCCESS;
  19164. }
  19165. BUILDIN_FUNC(pushpc)
  19166. {
  19167. uint8 dir;
  19168. int32 cells, dx, dy;
  19169. map_session_data* sd;
  19170. if(!script_rid2sd(sd))
  19171. {
  19172. return SCRIPT_CMD_SUCCESS;
  19173. }
  19174. dir = script_getnum(st,2);
  19175. cells = script_getnum(st,3);
  19176. if(dir >= DIR_MAX)
  19177. {
  19178. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  19179. script_reportsrc(st);
  19180. dir%= DIR_MAX; // trim spin-over
  19181. }
  19182. if(!cells)
  19183. {// zero distance
  19184. return SCRIPT_CMD_SUCCESS;
  19185. }
  19186. else if(cells<0)
  19187. {// pushing backwards
  19188. dir = direction_opposite( static_cast<enum directions>( dir ) ); // turn around
  19189. cells = -cells;
  19190. }
  19191. dx = dirx[dir];
  19192. dy = diry[dir];
  19193. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  19194. return SCRIPT_CMD_SUCCESS;
  19195. }
  19196. /// Invokes buying store preparation window
  19197. /// buyingstore <slots>;
  19198. BUILDIN_FUNC(buyingstore)
  19199. {
  19200. map_session_data* sd;
  19201. if( !script_rid2sd(sd) )
  19202. {
  19203. return SCRIPT_CMD_SUCCESS;
  19204. }
  19205. if( npc_isnear(&sd->bl) ) {
  19206. char output[150];
  19207. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  19208. clif_displaymessage(sd->fd, output);
  19209. return SCRIPT_CMD_SUCCESS;
  19210. }
  19211. buyingstore_setup(sd, script_getnum(st,2));
  19212. return SCRIPT_CMD_SUCCESS;
  19213. }
  19214. /// Invokes search store info window
  19215. /// searchstores <uses>,<effect>{,<map name>};
  19216. BUILDIN_FUNC(searchstores)
  19217. {
  19218. if (!battle_config.feature_search_stores) {
  19219. ShowError("buildin_searchstores: Search stores feature is disabled.\n");
  19220. return SCRIPT_CMD_FAILURE;
  19221. }
  19222. map_session_data* sd;
  19223. if( !script_rid2sd(sd) )
  19224. {
  19225. return SCRIPT_CMD_FAILURE;
  19226. }
  19227. int32 uses = script_getnum(st,2);
  19228. if (uses < 1 || uses > UINT8_MAX)
  19229. {
  19230. ShowError("buildin_searchstores: The amount of uses must be a number between 1 and %u.\n", UINT8_MAX);
  19231. return SCRIPT_CMD_FAILURE;
  19232. }
  19233. int32 effect = script_getnum(st,3);
  19234. if( effect < SEARCHSTORE_EFFECT_NORMAL || effect >= SEARCHSTORE_EFFECT_MAX )
  19235. {
  19236. ShowError("buildin_searchstores: Invalid effect id %d, specified.\n", effect);
  19237. return SCRIPT_CMD_FAILURE;
  19238. }
  19239. int16 m;
  19240. if (script_hasdata(st, 4)) {
  19241. const char* mapname = script_getstr(st, 4);
  19242. if (stricmp(mapname, "all") == 0)
  19243. m = 0;
  19244. else if (stricmp(mapname, "this") == 0) {
  19245. m = sd->bl.m;
  19246. }
  19247. else {
  19248. m = map_mapname2mapid(mapname);
  19249. // TODO: Support multi map-server
  19250. if (m < 0) {
  19251. ShowError("buildin_searchstores: Invalid map name %s.\n", mapname);
  19252. return SCRIPT_CMD_FAILURE;
  19253. }
  19254. }
  19255. }
  19256. else {
  19257. m = sd->bl.m;
  19258. }
  19259. searchstore_open(*sd, static_cast<uint8>(uses), static_cast<e_searchstore_effecttype>(effect), m);
  19260. return SCRIPT_CMD_SUCCESS;
  19261. }
  19262. /// Displays a number as large digital clock.
  19263. /// showdigit <value>[,<type>];
  19264. BUILDIN_FUNC(showdigit)
  19265. {
  19266. uint32 type = 0;
  19267. int32 value;
  19268. map_session_data* sd;
  19269. if( !script_rid2sd(sd) )
  19270. {
  19271. return SCRIPT_CMD_SUCCESS;
  19272. }
  19273. value = script_getnum(st,2);
  19274. if( script_hasdata(st,3) ){
  19275. type = script_getnum(st,3);
  19276. }
  19277. switch( type ){
  19278. case 0:
  19279. break;
  19280. case 1:
  19281. case 2:
  19282. // Use absolute value and then the negative value of it as starting value
  19283. // This is what gravity's client does for these counters
  19284. value = -abs(value);
  19285. break;
  19286. case 3:
  19287. value = abs(value);
  19288. if( value > 99 ){
  19289. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  19290. script_reportsrc(st);
  19291. value = 99;
  19292. }
  19293. break;
  19294. default:
  19295. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  19296. return SCRIPT_CMD_FAILURE;
  19297. }
  19298. clif_showdigit(sd, (unsigned char)type, value);
  19299. return SCRIPT_CMD_SUCCESS;
  19300. }
  19301. /**
  19302. * Rune Knight
  19303. * makerune <% success bonus>{,<char_id>};
  19304. **/
  19305. BUILDIN_FUNC(makerune) {
  19306. map_session_data* sd;
  19307. if (!script_charid2sd(3,sd))
  19308. return SCRIPT_CMD_FAILURE;
  19309. clif_skill_produce_mix_list( *sd, RK_RUNEMASTERY, 24 );
  19310. sd->itemid = script_getnum(st,2);
  19311. return SCRIPT_CMD_SUCCESS;
  19312. }
  19313. /**
  19314. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  19315. **/
  19316. BUILDIN_FUNC(checkdragon) {
  19317. TBL_PC* sd;
  19318. if (!script_charid2sd(2,sd))
  19319. return SCRIPT_CMD_FAILURE;
  19320. if( pc_isridingdragon(sd) )
  19321. script_pushint(st,1);
  19322. else
  19323. script_pushint(st,0);
  19324. return SCRIPT_CMD_SUCCESS;
  19325. }
  19326. /**
  19327. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  19328. * - Toggles the dragon on a RK if he can mount;
  19329. * @param Color - when not provided uses the green dragon;
  19330. * - 1 : Green Dragon
  19331. * - 2 : Brown Dragon
  19332. * - 3 : Gray Dragon
  19333. * - 4 : Blue Dragon
  19334. * - 5 : Red Dragon
  19335. **/
  19336. BUILDIN_FUNC(setdragon) {
  19337. TBL_PC* sd;
  19338. int32 color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  19339. if (!script_charid2sd(3,sd))
  19340. return SCRIPT_CMD_FAILURE;
  19341. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  19342. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  19343. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  19344. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  19345. script_pushint(st,1);
  19346. } else {//Not mounted; Mount now.
  19347. uint32 option = OPTION_DRAGON1;
  19348. if( color ) {
  19349. option = ( color == 1 ? OPTION_DRAGON1 :
  19350. color == 2 ? OPTION_DRAGON2 :
  19351. color == 3 ? OPTION_DRAGON3 :
  19352. color == 4 ? OPTION_DRAGON4 :
  19353. color == 5 ? OPTION_DRAGON5 : 0);
  19354. if( !option ) {
  19355. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  19356. option = OPTION_DRAGON1;
  19357. }
  19358. }
  19359. pc_setoption(sd, sd->sc.option|option);
  19360. script_pushint(st,1);
  19361. }
  19362. return SCRIPT_CMD_SUCCESS;
  19363. }
  19364. /**
  19365. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  19366. **/
  19367. BUILDIN_FUNC(ismounting) {
  19368. TBL_PC* sd;
  19369. if (!script_charid2sd(2,sd))
  19370. return SCRIPT_CMD_FAILURE;
  19371. if( sd->sc.getSCE(SC_ALL_RIDING) )
  19372. script_pushint(st,1);
  19373. else
  19374. script_pushint(st,0);
  19375. return SCRIPT_CMD_SUCCESS;
  19376. }
  19377. /**
  19378. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  19379. * - Toggles new mounts on a player when he can mount
  19380. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  19381. * - Will unmount the player is he is already mounting
  19382. **/
  19383. BUILDIN_FUNC(setmounting) {
  19384. TBL_PC* sd;
  19385. if (!script_charid2sd(2,sd))
  19386. return SCRIPT_CMD_FAILURE;
  19387. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  19388. clif_msg(sd, MSI_FAIELD_RIDING_OVERLAPPED);
  19389. script_pushint(st,0); //can't mount with one of these
  19390. } else if (sd->sc.getSCE(SC_CLOAKING) || sd->sc.getSCE(SC_CHASEWALK) || sd->sc.getSCE(SC_CLOAKINGEXCEED) || sd->sc.getSCE(SC_CAMOUFLAGE) || sd->sc.getSCE(SC_STEALTHFIELD) || sd->sc.getSCE(SC__FEINTBOMB)) {
  19391. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  19392. script_pushint(st, 0); // Silent failure
  19393. } else {
  19394. if( sd->sc.getSCE(SC_ALL_RIDING) )
  19395. status_change_end(&sd->bl, SC_ALL_RIDING); //release mount
  19396. else
  19397. sc_start(nullptr, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  19398. script_pushint(st,1);//in both cases, return 1.
  19399. }
  19400. return SCRIPT_CMD_SUCCESS;
  19401. }
  19402. /**
  19403. * Retrieves quantity of arguments provided to callfunc/callsub.
  19404. * getargcount() -> amount of arguments received in a function
  19405. **/
  19406. BUILDIN_FUNC(getargcount) {
  19407. struct script_retinfo* ri;
  19408. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  19409. ShowError("script:getargcount: used out of function or callsub label!\n");
  19410. st->state = END;
  19411. return SCRIPT_CMD_FAILURE;
  19412. }
  19413. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  19414. script_pushint(st, ri->nargs);
  19415. return SCRIPT_CMD_SUCCESS;
  19416. }
  19417. /**
  19418. * getcharip(<account ID>/<character ID>/<character name>)
  19419. **/
  19420. BUILDIN_FUNC(getcharip)
  19421. {
  19422. map_session_data* sd = nullptr;
  19423. /* check if a character name is specified */
  19424. if( script_hasdata(st, 2) )
  19425. {
  19426. if (script_isstring(st, 2))
  19427. sd = map_nick2sd(script_getstr(st, 2),false);
  19428. else
  19429. {
  19430. int32 id = script_getnum(st, 2);
  19431. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  19432. }
  19433. }
  19434. else
  19435. script_rid2sd(sd);
  19436. /* check for sd and IP */
  19437. if (!sd || !session[sd->fd]->client_addr)
  19438. {
  19439. script_pushconststr(st, "");
  19440. return SCRIPT_CMD_SUCCESS;
  19441. }
  19442. /* return the client ip_addr converted for output */
  19443. if (sd && sd->fd && session[sd->fd])
  19444. {
  19445. /* initiliaze */
  19446. const char *ip_addr = nullptr;
  19447. uint32 ip;
  19448. /* set ip, ip_addr and convert to ip and push str */
  19449. ip = session[sd->fd]->client_addr;
  19450. ip_addr = ip2str(ip, nullptr);
  19451. script_pushstrcopy(st, ip_addr);
  19452. }
  19453. return SCRIPT_CMD_SUCCESS;
  19454. }
  19455. /**
  19456. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  19457. **/
  19458. BUILDIN_FUNC(is_function) {
  19459. const char* str = script_getstr(st,2);
  19460. if( strdb_exists(userfunc_db, str) )
  19461. script_pushint(st,1);
  19462. else
  19463. script_pushint(st,0);
  19464. return SCRIPT_CMD_SUCCESS;
  19465. }
  19466. /**
  19467. * get_revision() -> retrieves the current svn revision (if available)
  19468. **/
  19469. BUILDIN_FUNC(get_revision) {
  19470. const char *svn = get_svn_revision();
  19471. if ( svn[0] != UNKNOWN_VERSION )
  19472. script_pushint(st,atoi(svn));
  19473. else
  19474. script_pushint(st,-1); //unknown
  19475. return SCRIPT_CMD_SUCCESS;
  19476. }
  19477. /* get_hash() -> retrieves the current git hash (if available)*/
  19478. BUILDIN_FUNC(get_githash) {
  19479. const char* git = get_git_hash();
  19480. char buf[CHAT_SIZE_MAX];
  19481. safestrncpy(buf,git,strlen(git)+1);
  19482. if ( git[0] != UNKNOWN_VERSION )
  19483. script_pushstrcopy(st,buf);
  19484. else
  19485. script_pushconststr(st,"Unknown"); //unknown
  19486. return SCRIPT_CMD_SUCCESS;
  19487. }
  19488. /**
  19489. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  19490. **/
  19491. BUILDIN_FUNC(freeloop) {
  19492. if( script_hasdata(st,2) ) {
  19493. if( script_getnum(st,2) )
  19494. st->freeloop = 1;
  19495. else
  19496. st->freeloop = 0;
  19497. }
  19498. script_pushint(st, st->freeloop);
  19499. return SCRIPT_CMD_SUCCESS;
  19500. }
  19501. /**
  19502. * @commands (script based)
  19503. **/
  19504. BUILDIN_FUNC(bindatcmd) {
  19505. const char* atcmd;
  19506. const char* eventName;
  19507. int32 i, level = 0, level2 = 100;
  19508. bool create = false;
  19509. atcmd = script_getstr(st,2);
  19510. eventName = script_getstr(st,3);
  19511. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19512. atcmd++;
  19513. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  19514. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  19515. if( atcmd_binding_count == 0 ) {
  19516. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  19517. create = true;
  19518. } else {
  19519. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  19520. if( i < atcmd_binding_count ) {/* update existent entry */
  19521. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19522. atcmd_binding[i]->level = level;
  19523. atcmd_binding[i]->level2 = level2;
  19524. } else
  19525. create = true;
  19526. }
  19527. if( create ) {
  19528. i = atcmd_binding_count;
  19529. if( atcmd_binding_count++ != 0 )
  19530. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  19531. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  19532. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  19533. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19534. atcmd_binding[i]->level = level;
  19535. atcmd_binding[i]->level2 = level2;
  19536. }
  19537. return SCRIPT_CMD_SUCCESS;
  19538. }
  19539. BUILDIN_FUNC(unbindatcmd) {
  19540. const char* atcmd;
  19541. int32 i = 0;
  19542. atcmd = script_getstr(st, 2);
  19543. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19544. atcmd++;
  19545. if( atcmd_binding_count == 0 ) {
  19546. script_pushint(st, 0);
  19547. return SCRIPT_CMD_SUCCESS;
  19548. }
  19549. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  19550. if( i < atcmd_binding_count ) {
  19551. int32 cursor = 0;
  19552. aFree(atcmd_binding[i]);
  19553. atcmd_binding[i] = nullptr;
  19554. /* compact the list now that we freed a slot somewhere */
  19555. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  19556. if( atcmd_binding[i] == nullptr )
  19557. continue;
  19558. if( cursor != i ) {
  19559. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  19560. }
  19561. cursor++;
  19562. }
  19563. if( (atcmd_binding_count = cursor) == 0 )
  19564. aFree(atcmd_binding);
  19565. script_pushint(st, 1);
  19566. } else
  19567. script_pushint(st, 0);/* not found */
  19568. return SCRIPT_CMD_SUCCESS;
  19569. }
  19570. BUILDIN_FUNC(useatcmd) {
  19571. return atcommand_sub(st,3);
  19572. }
  19573. BUILDIN_FUNC(checkre)
  19574. {
  19575. int32 num;
  19576. num=script_getnum(st,2);
  19577. switch(num){
  19578. case 0:
  19579. #ifdef RENEWAL
  19580. script_pushint(st, 1);
  19581. #else
  19582. script_pushint(st, 0);
  19583. #endif
  19584. break;
  19585. case 1:
  19586. #ifdef RENEWAL_CAST
  19587. script_pushint(st, 1);
  19588. #else
  19589. script_pushint(st, 0);
  19590. #endif
  19591. break;
  19592. case 2:
  19593. #ifdef RENEWAL_DROP
  19594. script_pushint(st, 1);
  19595. #else
  19596. script_pushint(st, 0);
  19597. #endif
  19598. break;
  19599. case 3:
  19600. #ifdef RENEWAL_EXP
  19601. script_pushint(st, 1);
  19602. #else
  19603. script_pushint(st, 0);
  19604. #endif
  19605. break;
  19606. case 4:
  19607. #ifdef RENEWAL_LVDMG
  19608. script_pushint(st, 1);
  19609. #else
  19610. script_pushint(st, 0);
  19611. #endif
  19612. break;
  19613. case 5:
  19614. #ifdef RENEWAL_ASPD
  19615. script_pushint(st, 1);
  19616. #else
  19617. script_pushint(st, 0);
  19618. #endif
  19619. break;
  19620. default:
  19621. ShowWarning("buildin_checkre: unknown parameter.\n");
  19622. break;
  19623. }
  19624. return SCRIPT_CMD_SUCCESS;
  19625. }
  19626. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  19627. BUILDIN_FUNC(getrandgroupitem) {
  19628. map_session_data* sd;
  19629. int32 i, get_count = 0, identify = 0;
  19630. uint16 group, qty = 0;
  19631. uint8 sub_group = 1;
  19632. if (!script_charid2sd(6, sd))
  19633. return SCRIPT_CMD_SUCCESS;
  19634. group = script_getnum(st,2);
  19635. if (!group) {
  19636. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  19637. return SCRIPT_CMD_FAILURE;
  19638. }
  19639. FETCH(3, qty);
  19640. FETCH(4, sub_group);
  19641. FETCH(5, identify);
  19642. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(group,sub_group);
  19643. if( entry == nullptr ){
  19644. ShowError( "buildin_getrandgroupitem: Unable to find a random entry in group %hu for sub group %hu.\n", group, sub_group );
  19645. return SCRIPT_CMD_FAILURE;
  19646. }
  19647. item item_tmp = {};
  19648. item_tmp.nameid = entry->nameid;
  19649. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  19650. if (!qty)
  19651. qty = entry->amount;
  19652. //Check if it's stackable.
  19653. if (!itemdb_isstackable(entry->nameid)) {
  19654. item_tmp.amount = 1;
  19655. get_count = qty;
  19656. }
  19657. else {
  19658. item_tmp.amount = qty;
  19659. get_count = 1;
  19660. }
  19661. if( pc_inventoryblank( sd ) < get_count ){
  19662. ShowError( "buildin_getrandgroupitem: Not enough free space in inventory.\n" );
  19663. return SCRIPT_CMD_FAILURE;
  19664. }
  19665. for (i = 0; i < get_count; i++) {
  19666. // if not pet egg
  19667. if (!pet_create_egg(sd, entry->nameid)) {
  19668. e_additem_result flag = pc_additem( sd, &item_tmp, item_tmp.amount, LOG_TYPE_SCRIPT );
  19669. if( flag != ADDITEM_SUCCESS ){
  19670. clif_additem(sd,0,0,flag);
  19671. ShowError( "buildin_getrandgroupitem: Failed to add the item to player.\n" );
  19672. return SCRIPT_CMD_FAILURE;
  19673. }
  19674. }
  19675. }
  19676. return SCRIPT_CMD_SUCCESS;
  19677. }
  19678. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  19679. * Gives item(s) to the attached player based on item group contents
  19680. */
  19681. BUILDIN_FUNC(getgroupitem) {
  19682. map_session_data* sd;
  19683. int32 group_id = script_getnum(st,2);
  19684. if (!script_charid2sd(4,sd))
  19685. return SCRIPT_CMD_FAILURE;
  19686. bool identify = false;
  19687. if( script_hasdata( st, 3 ) ){
  19688. identify = script_getnum( st, 3 );
  19689. }
  19690. if( itemdb_group.pc_get_itemgroup( group_id, identify, *sd ) ){
  19691. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  19692. return SCRIPT_CMD_FAILURE;
  19693. }
  19694. return SCRIPT_CMD_SUCCESS;
  19695. }
  19696. /* cleanmap <map_name>;
  19697. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  19698. static int32 atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  19699. {
  19700. nullpo_ret(bl);
  19701. map_clearflooritem(bl);
  19702. return SCRIPT_CMD_SUCCESS;
  19703. }
  19704. BUILDIN_FUNC(cleanmap)
  19705. {
  19706. const char *mapname;
  19707. int16 m;
  19708. mapname = script_getstr(st, 2);
  19709. m = map_mapname2mapid(mapname);
  19710. if (!m)
  19711. return SCRIPT_CMD_FAILURE;
  19712. if ((script_lastdata(st) - 2) < 4) {
  19713. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  19714. } else {
  19715. int16 x0 = script_getnum(st, 3);
  19716. int16 y0 = script_getnum(st, 4);
  19717. int16 x1 = script_getnum(st, 5);
  19718. int16 y1 = script_getnum(st, 6);
  19719. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  19720. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  19721. } else {
  19722. ShowError("cleanarea: invalid coordinate defined!\n");
  19723. return SCRIPT_CMD_FAILURE;
  19724. }
  19725. }
  19726. return SCRIPT_CMD_SUCCESS;
  19727. }
  19728. /* Cast a skill on the attached player.
  19729. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  19730. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  19731. BUILDIN_FUNC(npcskill)
  19732. {
  19733. uint16 skill_id;
  19734. unsigned short skill_level;
  19735. uint32 stat_point;
  19736. uint32 npc_level;
  19737. struct npc_data *nd;
  19738. map_session_data *sd;
  19739. if( !script_rid2sd(sd) )
  19740. return SCRIPT_CMD_SUCCESS;
  19741. if (script_isstring(st, 2)) {
  19742. const char *name = script_getstr(st, 2);
  19743. if (!(skill_id = skill_name2id(name))) {
  19744. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  19745. return SCRIPT_CMD_FAILURE;
  19746. }
  19747. } else {
  19748. skill_id = script_getnum(st, 2);
  19749. if (!skill_get_index(skill_id)) {
  19750. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  19751. return SCRIPT_CMD_FAILURE;
  19752. }
  19753. }
  19754. skill_level = script_getnum(st, 3);
  19755. stat_point = script_getnum(st, 4);
  19756. npc_level = script_getnum(st, 5);
  19757. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  19758. if (stat_point > battle_config.max_third_parameter) {
  19759. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  19760. return SCRIPT_CMD_FAILURE;
  19761. }
  19762. if (npc_level > MAX_LEVEL) {
  19763. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  19764. return SCRIPT_CMD_FAILURE;
  19765. }
  19766. if (nd == nullptr) { //ain't possible, but I don't trust people.
  19767. return SCRIPT_CMD_FAILURE;
  19768. }
  19769. nd->level = npc_level;
  19770. nd->stat_point = stat_point;
  19771. if (!nd->status.hp)
  19772. status_calc_npc(nd, SCO_FIRST);
  19773. else
  19774. status_calc_npc(nd, SCO_NONE);
  19775. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  19776. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0,true);
  19777. else
  19778. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0,true);
  19779. return SCRIPT_CMD_SUCCESS;
  19780. }
  19781. /* Consumes an item.
  19782. * consumeitem <item id>{,<char_id>};
  19783. * consumeitem "<item name>"{,<char_id>};
  19784. * @param item: Item ID or name
  19785. */
  19786. BUILDIN_FUNC(consumeitem)
  19787. {
  19788. map_session_data *sd;
  19789. std::shared_ptr<item_data> item_data;
  19790. if (!script_charid2sd(3, sd))
  19791. return SCRIPT_CMD_FAILURE;
  19792. if( script_isstring(st, 2) ){
  19793. const char *name = script_getstr(st, 2);
  19794. item_data = item_db.searchname( name );
  19795. if( item_data == nullptr ){
  19796. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  19797. return SCRIPT_CMD_FAILURE;
  19798. }
  19799. } else {
  19800. t_itemid nameid = script_getnum(st, 2);
  19801. item_data = item_db.find( nameid );
  19802. if( item_data == nullptr ){
  19803. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  19804. return SCRIPT_CMD_FAILURE;
  19805. }
  19806. }
  19807. run_script( item_data->script, 0, sd->bl.id, 0 );
  19808. return SCRIPT_CMD_SUCCESS;
  19809. }
  19810. /** Makes a player sit/stand.
  19811. * sit {"<character name>"};
  19812. * stand {"<character name>"};
  19813. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  19814. * @param name: Player name that will be invoked
  19815. */
  19816. BUILDIN_FUNC(sit)
  19817. {
  19818. TBL_PC *sd;
  19819. if( !script_nick2sd(2,sd) )
  19820. return SCRIPT_CMD_FAILURE;
  19821. if( !pc_issit(sd) ) {
  19822. pc_setsit(sd);
  19823. skill_sit(sd, 1);
  19824. clif_sitting(sd->bl);
  19825. }
  19826. return SCRIPT_CMD_SUCCESS;
  19827. }
  19828. /** Makes player to stand
  19829. * @param name: Player name that will be set
  19830. */
  19831. BUILDIN_FUNC(stand)
  19832. {
  19833. TBL_PC *sd;
  19834. if( !script_nick2sd(2,sd) )
  19835. return SCRIPT_CMD_FAILURE;
  19836. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19837. skill_sit(sd, 0);
  19838. clif_standing(sd->bl);
  19839. }
  19840. return SCRIPT_CMD_SUCCESS;
  19841. }
  19842. /** Creates an array of bounded item IDs
  19843. * countbound {<type>{,<char_id>}};
  19844. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19845. * @return amt: Total number of different items type found
  19846. */
  19847. BUILDIN_FUNC(countbound)
  19848. {
  19849. TBL_PC *sd;
  19850. if (!script_charid2sd(3,sd))
  19851. return SCRIPT_CMD_FAILURE;
  19852. int32 i, k = 0;
  19853. int32 type = script_getnum(st,2);
  19854. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19855. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19856. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19857. ))
  19858. {
  19859. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19860. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19861. k++;
  19862. }
  19863. }
  19864. script_pushint(st,k);
  19865. return SCRIPT_CMD_SUCCESS;
  19866. }
  19867. /** Creates new party
  19868. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19869. * @param party_name: String of party name that will be created
  19870. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19871. * @param item_share: 0-Each Take. 1-Party Share
  19872. * @param item_share_type: 0-Each Take. 1-Even Share
  19873. * @return val: Result value
  19874. * -3 - party name is exist
  19875. * -2 - player is in party already
  19876. * -1 - player is not found
  19877. * 0 - unknown error
  19878. * 1 - success, will return party id $@party_create_id
  19879. */
  19880. BUILDIN_FUNC(party_create)
  19881. {
  19882. char party_name[NAME_LENGTH];
  19883. int32 item1 = 0, item2 = 0;
  19884. TBL_PC *sd = nullptr;
  19885. if (!script_charid2sd(3, sd)) {
  19886. script_pushint(st,-1);
  19887. return SCRIPT_CMD_FAILURE;
  19888. }
  19889. if( sd->status.party_id ) {
  19890. script_pushint(st,-2);
  19891. return SCRIPT_CMD_FAILURE;
  19892. }
  19893. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19894. trim(party_name);
  19895. if( party_searchname(party_name) ) {
  19896. script_pushint(st,-3);
  19897. return SCRIPT_CMD_FAILURE;
  19898. }
  19899. if( script_getnum(st,4) )
  19900. item1 = 1;
  19901. if( script_getnum(st,5) )
  19902. item2 = 1;
  19903. party_create_byscript = 1;
  19904. script_pushint( st, party_create( *sd, party_name, item1, item2 ) );
  19905. return SCRIPT_CMD_SUCCESS;
  19906. }
  19907. /** Adds player to specified party
  19908. * party_addmember <party id>,<char id>;
  19909. * @param party_id: The party that will be entered by player
  19910. * @param char_id: Char id of player that will be joined to the party
  19911. * @return val: Result value
  19912. * -5 - another character of the same account is in the party
  19913. * -4 - party is full
  19914. * -3 - party is not found
  19915. * -2 - player is in party already
  19916. * -1 - player is not found
  19917. * 0 - unknown error
  19918. * 1 - success
  19919. */
  19920. BUILDIN_FUNC(party_addmember)
  19921. {
  19922. int32 party_id = script_getnum(st,2);
  19923. TBL_PC *sd;
  19924. struct party_data *party;
  19925. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19926. script_pushint(st,-1);
  19927. return SCRIPT_CMD_FAILURE;
  19928. }
  19929. if( sd->status.party_id ) {
  19930. script_pushint(st,-2);
  19931. return SCRIPT_CMD_FAILURE;
  19932. }
  19933. if( !(party = party_search(party_id)) ) {
  19934. script_pushint(st,-3);
  19935. return SCRIPT_CMD_FAILURE;
  19936. }
  19937. if (battle_config.block_account_in_same_party) {
  19938. int32 i;
  19939. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19940. if (i < MAX_PARTY) {
  19941. script_pushint(st,-5);
  19942. return SCRIPT_CMD_FAILURE;
  19943. }
  19944. }
  19945. if( party->party.count >= MAX_PARTY ) {
  19946. script_pushint(st,-4);
  19947. return SCRIPT_CMD_FAILURE;
  19948. }
  19949. sd->party_invite = party_id;
  19950. script_pushint( st, party_add_member( party_id, *sd ) );
  19951. return SCRIPT_CMD_SUCCESS;
  19952. }
  19953. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19954. * party_delmember {<char id>,<party_id>};
  19955. * @param: char_id
  19956. * @param: party_id
  19957. * @return val: Result value
  19958. * -3 - player is not in party
  19959. * -2 - party is not found
  19960. * -1 - player is not found
  19961. * 0 - unknown error
  19962. * 1 - success
  19963. */
  19964. BUILDIN_FUNC(party_delmember)
  19965. {
  19966. TBL_PC *sd = nullptr;
  19967. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19968. script_pushint(st,-1);
  19969. return SCRIPT_CMD_FAILURE;
  19970. }
  19971. if( sd || script_charid2sd(2,sd) )
  19972. script_pushint(st,party_removemember2(sd,0,0));
  19973. else
  19974. script_pushint(st,party_removemember2(nullptr,script_getnum(st,2),script_getnum(st,3)));
  19975. return SCRIPT_CMD_SUCCESS;
  19976. }
  19977. /** Changes party leader of specified party (even the leader is offline)
  19978. * party_changeleader <party id>,<char id>;
  19979. * @param party_id: ID of party
  19980. * @param char_id: Char ID of new leader
  19981. * @return val: Result value
  19982. * -4 - player is party leader already
  19983. * -3 - player is not in this party
  19984. * -2 - player is not found
  19985. * -1 - party is not found
  19986. * 0 - unknown error
  19987. * 1 - success */
  19988. BUILDIN_FUNC(party_changeleader)
  19989. {
  19990. int32 i, party_id = script_getnum(st,2);
  19991. TBL_PC *sd = nullptr;
  19992. TBL_PC *tsd = nullptr;
  19993. struct party_data *party = nullptr;
  19994. if( !(party = party_search(party_id)) ) {
  19995. script_pushint(st,-1);
  19996. return SCRIPT_CMD_FAILURE;
  19997. }
  19998. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19999. script_pushint(st,-2);
  20000. return SCRIPT_CMD_FAILURE;
  20001. }
  20002. if( tsd->status.party_id != party_id ) {
  20003. script_pushint(st,-3);
  20004. return SCRIPT_CMD_FAILURE;
  20005. }
  20006. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  20007. if( i >= MAX_PARTY ) { //this is should impossible!
  20008. script_pushint(st,0);
  20009. return SCRIPT_CMD_FAILURE;
  20010. }
  20011. if( party->data[i].sd == tsd ) {
  20012. script_pushint(st,-4);
  20013. return SCRIPT_CMD_FAILURE;
  20014. }
  20015. script_pushint(st,party_changeleader(sd,tsd,party));
  20016. return SCRIPT_CMD_SUCCESS;
  20017. }
  20018. /** Changes party option
  20019. * party_changeoption <party id>,<option>,<flag>;
  20020. * @param party_id: ID of party that will be changed
  20021. * @param option: Type of option
  20022. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  20023. */
  20024. BUILDIN_FUNC(party_changeoption)
  20025. {
  20026. struct party_data *party;
  20027. if( !(party = party_search(script_getnum(st,2))) ) {
  20028. script_pushint(st,-1);
  20029. return SCRIPT_CMD_FAILURE;
  20030. }
  20031. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  20032. return SCRIPT_CMD_SUCCESS;
  20033. }
  20034. /** Destroys party with party id.
  20035. * party_destroy <party id>;
  20036. * @param party_id: ID of party that will be destroyed
  20037. */
  20038. BUILDIN_FUNC(party_destroy)
  20039. {
  20040. int32 i;
  20041. struct party_data *party;
  20042. if( !(party = party_search(script_getnum(st,2))) ) {
  20043. script_pushint(st,0);
  20044. return SCRIPT_CMD_FAILURE;
  20045. }
  20046. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  20047. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  20048. int32 j;
  20049. for( j = 0; j < MAX_PARTY; j++ ) {
  20050. TBL_PC *sd = party->data[j].sd;
  20051. if(sd)
  20052. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  20053. else if( party->party.member[j].char_id )
  20054. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id,party->party.member[j].name,PARTY_MEMBER_WITHDRAW_LEAVE);
  20055. }
  20056. party_broken(party->party.party_id);
  20057. script_pushint(st,1);
  20058. }
  20059. else //leader leave = party broken
  20060. script_pushint( st, party_leave( *party->data[i].sd ) );
  20061. return SCRIPT_CMD_SUCCESS;
  20062. }
  20063. /** Returns various information about a player's VIP status. Need to enable VIP system
  20064. * vip_status <type>,{"<character name>"};
  20065. * @param type: Info type, see enum vip_status_type
  20066. * @param name: Character name (Optional)
  20067. */
  20068. BUILDIN_FUNC(vip_status) {
  20069. #ifdef VIP_ENABLE
  20070. TBL_PC *sd;
  20071. int32 type;
  20072. if( !script_nick2sd(3,sd) )
  20073. return SCRIPT_CMD_FAILURE;
  20074. type = script_getnum(st, 2);
  20075. switch(type) {
  20076. case VIP_STATUS_ACTIVE: // Get VIP status.
  20077. script_pushint(st, pc_isvip(sd));
  20078. break;
  20079. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  20080. if (pc_isvip(sd)) {
  20081. script_pushint(st, sd->vip.time);
  20082. } else
  20083. script_pushint(st, 0);
  20084. break;
  20085. case VIP_STATUS_REMAINING: // Get remaining time.
  20086. if (pc_isvip(sd)) {
  20087. script_pushint(st, sd->vip.time - time(nullptr));
  20088. } else
  20089. script_pushint(st, 0);
  20090. break;
  20091. default:
  20092. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  20093. return SCRIPT_CMD_FAILURE;
  20094. }
  20095. #else
  20096. script_pushint(st, 0);
  20097. #endif
  20098. return SCRIPT_CMD_SUCCESS;
  20099. }
  20100. /** Adds or removes VIP time in minutes. Need to enable VIP system
  20101. * vip_time <time in mn>,{"<character name>"};
  20102. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  20103. * @param name: Character name (optional)
  20104. */
  20105. BUILDIN_FUNC(vip_time) {
  20106. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  20107. TBL_PC *sd;
  20108. int32 viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  20109. if( !script_nick2sd(3,sd) )
  20110. return SCRIPT_CMD_FAILURE;
  20111. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  20112. #endif
  20113. return SCRIPT_CMD_SUCCESS;
  20114. }
  20115. /**
  20116. * Turns a player into a monster and optionally can grant a SC attribute effect.
  20117. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  20118. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  20119. * @param monster: Monster ID or name
  20120. * @param duration: Transform duration in millisecond (ms)
  20121. * @param sc_type: Type of SC that will be affected during the transformation
  20122. * @param val1: Value for SC
  20123. * @param val2: Value for SC
  20124. * @param val3: Value for SC
  20125. * @param val4: Value for SC
  20126. * @author: malufett
  20127. */
  20128. BUILDIN_FUNC(montransform) {
  20129. TBL_PC *sd;
  20130. enum sc_type type;
  20131. int32 tick, mob_id, val1, val2, val3, val4;
  20132. val1 = val2 = val3 = val4 = 0;
  20133. if( !script_rid2sd(sd) )
  20134. return SCRIPT_CMD_FAILURE;
  20135. if( script_isstring(st, 2) )
  20136. mob_id = mobdb_searchname(script_getstr(st, 2));
  20137. else
  20138. mob_id = mobdb_checkid(script_getnum(st, 2));
  20139. tick = script_getnum(st, 3);
  20140. if (script_hasdata(st, 4))
  20141. type = (sc_type)script_getnum(st, 4);
  20142. else
  20143. type = SC_NONE;
  20144. if (mob_id == 0) {
  20145. if( script_isstring(st, 2) )
  20146. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  20147. else
  20148. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  20149. return SCRIPT_CMD_FAILURE;
  20150. }
  20151. if (mob_id == MOBID_EMPERIUM) {
  20152. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  20153. return SCRIPT_CMD_FAILURE;
  20154. }
  20155. if (!(type >= SC_NONE && type < SC_MAX)) {
  20156. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  20157. return SCRIPT_CMD_FAILURE;
  20158. }
  20159. if (script_hasdata(st, 5))
  20160. val1 = script_getnum(st, 5);
  20161. if (script_hasdata(st, 6))
  20162. val2 = script_getnum(st, 6);
  20163. if (script_hasdata(st, 7))
  20164. val3 = script_getnum(st, 7);
  20165. if (script_hasdata(st, 8))
  20166. val4 = script_getnum(st, 8);
  20167. if (tick != 0) {
  20168. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  20169. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  20170. return SCRIPT_CMD_FAILURE;
  20171. }
  20172. if (sd->disguise){
  20173. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  20174. return SCRIPT_CMD_FAILURE;
  20175. }
  20176. if (!strcmp(script_getfuncname(st), "active_transform")) {
  20177. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM); // Clear previous
  20178. sc_start2(nullptr, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  20179. } else {
  20180. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM); // Clear previous
  20181. sc_start2(nullptr, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  20182. }
  20183. if (type != SC_NONE)
  20184. sc_start4(nullptr, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  20185. }
  20186. return SCRIPT_CMD_SUCCESS;
  20187. }
  20188. /**
  20189. * Attach script to player for certain duration
  20190. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  20191. * @param "script code"
  20192. * @param duration
  20193. * @param flag
  20194. * @param icon
  20195. * @param char_id
  20196. * @author [Cydh]
  20197. **/
  20198. BUILDIN_FUNC(bonus_script) {
  20199. uint16 flag = 0;
  20200. int16 icon = EFST_BLANK;
  20201. uint32 dur;
  20202. uint8 type = 0;
  20203. TBL_PC* sd;
  20204. const char *script_str = nullptr;
  20205. struct s_bonus_script_entry *entry = nullptr;
  20206. if ( !script_charid2sd(7,sd) )
  20207. return SCRIPT_CMD_FAILURE;
  20208. script_str = script_getstr(st,2);
  20209. dur = 1000 * abs(script_getnum(st,3));
  20210. FETCH(4, flag);
  20211. FETCH(5, type);
  20212. FETCH(6, icon);
  20213. // No Script string, No Duration!
  20214. if (script_str[0] == '\0' || !dur) {
  20215. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  20216. return SCRIPT_CMD_FAILURE;
  20217. }
  20218. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  20219. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  20220. return SCRIPT_CMD_FAILURE;
  20221. }
  20222. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  20223. icon = EFST_BLANK;
  20224. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_type)icon, flag, type))) {
  20225. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  20226. status_calc_pc(sd,SCO_NONE);
  20227. }
  20228. return SCRIPT_CMD_SUCCESS;
  20229. }
  20230. /**
  20231. * Removes all bonus script from player
  20232. * bonus_script_clear {<flag>,{<char_id>}};
  20233. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  20234. * @param char_id Clear script from this character
  20235. * @author [Cydh]
  20236. */
  20237. BUILDIN_FUNC(bonus_script_clear) {
  20238. TBL_PC* sd;
  20239. bool flag = false;
  20240. if (!script_charid2sd(3,sd))
  20241. return SCRIPT_CMD_FAILURE;
  20242. if (script_hasdata(st,2))
  20243. flag = script_getnum(st,2) != 0;
  20244. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  20245. return SCRIPT_CMD_SUCCESS;
  20246. }
  20247. /** Allows player to use atcommand while talking with NPC
  20248. * enable_command;
  20249. * @author [Cydh], [Kichi]
  20250. */
  20251. BUILDIN_FUNC(enable_command) {
  20252. TBL_PC* sd;
  20253. if (!script_rid2sd(sd))
  20254. return SCRIPT_CMD_FAILURE;
  20255. sd->state.disable_atcommand_on_npc = 0;
  20256. return SCRIPT_CMD_SUCCESS;
  20257. }
  20258. /** Prevents player to use atcommand while talking with NPC
  20259. * disable_command;
  20260. * @author [Cydh], [Kichi]
  20261. */
  20262. BUILDIN_FUNC(disable_command) {
  20263. TBL_PC* sd;
  20264. if (!script_rid2sd(sd))
  20265. return SCRIPT_CMD_FAILURE;
  20266. sd->state.disable_atcommand_on_npc = 1;
  20267. return SCRIPT_CMD_SUCCESS;
  20268. }
  20269. /** Get the information of the members of a guild by type.
  20270. * getguildmember <guild_id>{,<type>};
  20271. * @param guild_id: ID of guild
  20272. * @param type: Type of option (optional)
  20273. */
  20274. BUILDIN_FUNC(getguildmember)
  20275. {
  20276. uint8 j = 0;
  20277. auto g = guild_search(script_getnum(st,2));
  20278. if (g) {
  20279. uint8 i, type = 0;
  20280. struct script_data *data = nullptr;
  20281. char *varname = nullptr;
  20282. if (script_hasdata(st,3))
  20283. type = script_getnum(st,3);
  20284. if (script_hasdata(st,4)) {
  20285. data = script_getdata(st, 4);
  20286. if (!data_isreference(data)) {
  20287. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  20288. return SCRIPT_CMD_FAILURE;
  20289. }
  20290. varname = reference_getname(data);
  20291. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  20292. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  20293. return SCRIPT_CMD_FAILURE;
  20294. }
  20295. if (not_server_variable(*varname)) {
  20296. map_session_data *sd;
  20297. if (!script_rid2sd(sd)) {
  20298. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  20299. return SCRIPT_CMD_FAILURE;
  20300. }
  20301. }
  20302. }
  20303. for (i = 0; i < MAX_GUILD; i++) {
  20304. if (g->guild.member[i].account_id) {
  20305. switch (type) {
  20306. case 2:
  20307. if (data)
  20308. setd_sub_num( st, nullptr, varname, j, g->guild.member[i].account_id, data->ref );
  20309. else
  20310. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->guild.member[i].account_id);
  20311. break;
  20312. case 1:
  20313. if (data)
  20314. setd_sub_num( st, nullptr, varname, j, g->guild.member[i].char_id, data->ref );
  20315. else
  20316. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->guild.member[i].char_id);
  20317. break;
  20318. default:
  20319. if (data)
  20320. setd_sub_str( st, nullptr, varname, j, g->guild.member[i].name, data->ref );
  20321. else
  20322. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->guild.member[i].name);
  20323. break;
  20324. }
  20325. j++;
  20326. }
  20327. }
  20328. }
  20329. mapreg_setreg(add_str("$@guildmembercount"), j);
  20330. script_pushint(st, j);
  20331. return SCRIPT_CMD_SUCCESS;
  20332. }
  20333. /** Adds spirit ball to player for 'duration' in milisecond
  20334. * addspiritball <count>,<duration>{,<char_id>};
  20335. * @param count How many spirit ball will be added
  20336. * @param duration How long spiritball is active until it disappears
  20337. * @param char_id Target player (Optional)
  20338. * @author [Cydh]
  20339. */
  20340. BUILDIN_FUNC(addspiritball) {
  20341. uint8 i, count;
  20342. uint16 duration;
  20343. map_session_data *sd = nullptr;
  20344. if (script_hasdata(st,4)) {
  20345. if (!script_isstring(st,4))
  20346. sd = map_charid2sd(script_getnum(st,4));
  20347. else
  20348. sd = map_nick2sd(script_getstr(st,4),false);
  20349. }
  20350. else
  20351. script_rid2sd(sd);
  20352. if (!sd)
  20353. return SCRIPT_CMD_FAILURE;
  20354. count = script_getnum(st,2);
  20355. if (count == 0)
  20356. return SCRIPT_CMD_SUCCESS;
  20357. duration = script_getnum(st,3);
  20358. for (i = 0; i < count; i++)
  20359. pc_addspiritball(sd,duration,10);
  20360. return SCRIPT_CMD_SUCCESS;
  20361. }
  20362. /** Deletes the spirit ball(s) from player
  20363. * delspiritball <count>{,<char_id>};
  20364. * @param count How many spirit ball will be deleted
  20365. * @param char_id Target player (Optional)
  20366. * @author [Cydh]
  20367. */
  20368. BUILDIN_FUNC(delspiritball) {
  20369. uint8 count;
  20370. map_session_data *sd = nullptr;
  20371. if (script_hasdata(st,3)) {
  20372. if (!script_isstring(st,3))
  20373. sd = map_charid2sd(script_getnum(st,3));
  20374. else
  20375. sd = map_nick2sd(script_getstr(st,3),false);
  20376. }
  20377. else
  20378. script_rid2sd(sd);
  20379. if (!sd)
  20380. return SCRIPT_CMD_FAILURE;
  20381. count = script_getnum(st,2);
  20382. if (count == 0)
  20383. count = 1;
  20384. pc_delspiritball(sd,count,0);
  20385. return SCRIPT_CMD_SUCCESS;
  20386. }
  20387. /** Counts the spirit ball that player has
  20388. * countspiritball {<char_id>};
  20389. * @param char_id Target player (Optional)
  20390. * @author [Cydh]
  20391. */
  20392. BUILDIN_FUNC(countspiritball) {
  20393. map_session_data *sd;
  20394. if (script_hasdata(st,2)) {
  20395. if (!script_isstring(st,2))
  20396. sd = map_charid2sd(script_getnum(st,2));
  20397. else
  20398. sd = map_nick2sd(script_getstr(st,2),false);
  20399. }
  20400. else
  20401. script_rid2sd(sd);
  20402. if (!sd)
  20403. return SCRIPT_CMD_FAILURE;
  20404. script_pushint(st,sd->spiritball);
  20405. return SCRIPT_CMD_SUCCESS;
  20406. }
  20407. /** Merges separated stackable items because of guid
  20408. * mergeitem {<char_id>};
  20409. * @param char_id Char ID (Optional)
  20410. * @author [Cydh]
  20411. */
  20412. BUILDIN_FUNC(mergeitem) {
  20413. map_session_data *sd;
  20414. if (!script_charid2sd(2, sd))
  20415. return SCRIPT_CMD_FAILURE;
  20416. clif_merge_item_open( *sd );
  20417. return SCRIPT_CMD_SUCCESS;
  20418. }
  20419. /** Merges separated stackable items because of guid (Unofficial)
  20420. * mergeitem2 {<item_id>,{<char_id>}};
  20421. * mergeitem2 {"<item name>",{<char_id>}};
  20422. * @param item Item ID/Name for merging specific item (Optional)
  20423. * @author [Cydh]
  20424. */
  20425. BUILDIN_FUNC(mergeitem2) {
  20426. map_session_data *sd;
  20427. struct item *items = nullptr;
  20428. uint16 i, count = 0;
  20429. t_itemid nameid = 0;
  20430. if (!script_charid2sd(3, sd))
  20431. return SCRIPT_CMD_FAILURE;
  20432. if (script_hasdata(st, 2)) {
  20433. if (script_isstring(st, 2)) {// "<item name>"
  20434. const char *name = script_getstr(st, 2);
  20435. std::shared_ptr<item_data> id = item_db.searchname( name );
  20436. if( id == nullptr ){
  20437. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  20438. script_pushint(st, count);
  20439. return SCRIPT_CMD_FAILURE;
  20440. }
  20441. nameid = id->nameid;
  20442. } else {// <item id>
  20443. nameid = script_getnum(st, 2);
  20444. if (!item_db.exists(nameid)) {
  20445. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  20446. script_pushint(st, count);
  20447. return SCRIPT_CMD_FAILURE;
  20448. }
  20449. }
  20450. }
  20451. for (i = 0; i < MAX_INVENTORY; i++) {
  20452. struct item *it = &sd->inventory.u.items_inventory[i];
  20453. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  20454. continue;
  20455. if ((!nameid || (nameid == it->nameid))) {
  20456. uint16 k;
  20457. if (!count) {
  20458. CREATE(items, struct item, 1);
  20459. memcpy(&items[count++], it, sizeof(struct item));
  20460. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20461. continue;
  20462. }
  20463. for (k = 0; k < count; k++) {
  20464. // Find Match
  20465. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  20466. items[k].amount += it->amount;
  20467. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20468. break;
  20469. }
  20470. }
  20471. if (k >= count) {
  20472. // New entry
  20473. RECREATE(items, struct item, count+1);
  20474. memcpy(&items[count++], it, sizeof(struct item));
  20475. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20476. }
  20477. }
  20478. }
  20479. if (!items) // Nothing todo here
  20480. return SCRIPT_CMD_SUCCESS;
  20481. // Retrieve the items
  20482. for (i = 0; i < count; i++) {
  20483. uint8 flag = 0;
  20484. if (!&items[i])
  20485. continue;
  20486. items[i].id = 0;
  20487. items[i].unique_id = 0;
  20488. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  20489. clif_additem(sd, i, items[i].amount, flag);
  20490. }
  20491. aFree(items);
  20492. script_pushint(st, count);
  20493. return SCRIPT_CMD_SUCCESS;
  20494. }
  20495. /**
  20496. * Update an entry from NPC shop.
  20497. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  20498. **/
  20499. BUILDIN_FUNC(npcshopupdate) {
  20500. const char* npcname = script_getstr(st, 2);
  20501. struct npc_data* nd = npc_name2id(npcname);
  20502. t_itemid nameid = script_getnum(st, 3);
  20503. int32 price = script_getnum(st, 4);
  20504. #if PACKETVER >= 20131223
  20505. int32 stock = script_hasdata(st,5) ? script_getnum(st,5) : -1;
  20506. if( stock < -1 ){
  20507. ShowError( "buildin_npcshopupdate: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  20508. script_pushint( st, 0 );
  20509. return SCRIPT_CMD_FAILURE;
  20510. }
  20511. #endif
  20512. int32 i;
  20513. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  20514. script_pushint(st,0);
  20515. return SCRIPT_CMD_FAILURE;
  20516. }
  20517. if (!nd->u.shop.count) {
  20518. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  20519. script_pushint(st,0);
  20520. return SCRIPT_CMD_FAILURE;
  20521. }
  20522. for (i = 0; i < nd->u.shop.count; i++) {
  20523. if (nd->u.shop.shop_item[i].nameid == nameid) {
  20524. if (price != 0) {
  20525. if (price < 0) {
  20526. if (nd->subtype == NPCTYPE_CASHSHOP || nd->subtype == NPCTYPE_POINTSHOP || nd->subtype == NPCTYPE_ITEMSHOP) {
  20527. ShowError("builtin_npcshopupdate: Invalid price in shop '%s'.\n", nd->exname);
  20528. script_pushint(st, 0);
  20529. return SCRIPT_CMD_FAILURE;
  20530. }
  20531. std::shared_ptr<item_data> id = item_db.find(nameid);
  20532. if (!id) {
  20533. ShowError("buildin_npcshopupdate: Item ID %u does not exist.\n", nameid);
  20534. script_pushint(st, 0);
  20535. return SCRIPT_CMD_FAILURE;
  20536. }
  20537. price = id->value_buy;
  20538. }
  20539. nd->u.shop.shop_item[i].value = price;
  20540. }
  20541. #if PACKETVER >= 20131223
  20542. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  20543. nd->u.shop.shop_item[i].qty = stock;
  20544. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  20545. }
  20546. #endif
  20547. }
  20548. }
  20549. script_pushint(st,1);
  20550. return SCRIPT_CMD_SUCCESS;
  20551. }
  20552. // Clan System
  20553. BUILDIN_FUNC(clan_join){
  20554. map_session_data *sd;
  20555. int32 clan_id = script_getnum(st,2);
  20556. if( !script_charid2sd( 3, sd ) ){
  20557. script_pushint(st, false);
  20558. return SCRIPT_CMD_FAILURE;
  20559. }
  20560. if( clan_member_join( *sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  20561. script_pushint(st, true);
  20562. else
  20563. script_pushint(st, false);
  20564. return SCRIPT_CMD_SUCCESS;
  20565. }
  20566. BUILDIN_FUNC(clan_leave){
  20567. map_session_data* sd;
  20568. if( !script_charid2sd( 2, sd ) ){
  20569. script_pushint(st, false);
  20570. return SCRIPT_CMD_FAILURE;
  20571. }
  20572. if( clan_member_leave( *sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  20573. script_pushint(st, true);
  20574. else
  20575. script_pushint(st, false);
  20576. return SCRIPT_CMD_SUCCESS;
  20577. }
  20578. /**
  20579. * Get rid from running script.
  20580. * getattachedrid();
  20581. **/
  20582. BUILDIN_FUNC(getattachedrid) {
  20583. script_pushint(st, st->rid);
  20584. return SCRIPT_CMD_SUCCESS;
  20585. }
  20586. /**
  20587. * Get variable from a Player
  20588. * getvar <variable>,<char_id>;
  20589. */
  20590. BUILDIN_FUNC(getvar) {
  20591. int32 char_id = script_getnum(st, 3);
  20592. map_session_data *sd = map_charid2sd(char_id);
  20593. struct script_data *data = nullptr;
  20594. const char *name = nullptr;
  20595. if (!sd) {
  20596. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  20597. return SCRIPT_CMD_FAILURE;
  20598. }
  20599. data = script_getdata(st, 2);
  20600. if (!data_isreference(data)) {
  20601. ShowError("buildin_getvar: Not a variable\n");
  20602. script_reportdata(data);
  20603. script_pushnil(st);
  20604. st->state = END;
  20605. return SCRIPT_CMD_FAILURE;
  20606. }
  20607. name = reference_getname(data);
  20608. if (reference_toparam(data)) {
  20609. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  20610. script_reportdata(data);
  20611. script_pushnil(st);
  20612. st->state = END;
  20613. return SCRIPT_CMD_FAILURE;
  20614. }
  20615. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  20616. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  20617. script_reportdata(data);
  20618. script_pushnil(st);
  20619. st->state = END;
  20620. return SCRIPT_CMD_FAILURE;
  20621. }
  20622. get_val_(st, data, sd);
  20623. if (data_isint(data))
  20624. script_pushint(st, conv_num_(st, data, sd));
  20625. else
  20626. script_pushstrcopy(st, conv_str_(st, data, sd));
  20627. return SCRIPT_CMD_SUCCESS;
  20628. }
  20629. /**
  20630. * Display script message
  20631. * showscript "<message>"{,<GID>,<flag>};
  20632. * @param flag: Specify target
  20633. * AREA - Message is sent to players in the vicinity of the source (default).
  20634. * SELF - Message is sent only to player attached.
  20635. **/
  20636. BUILDIN_FUNC(showscript) {
  20637. struct block_list *bl = nullptr;
  20638. const char *msg = script_getstr(st,2);
  20639. int32 id = 0;
  20640. send_target target = AREA;
  20641. if (script_hasdata(st,3)) {
  20642. id = script_getnum(st,3);
  20643. bl = map_id2bl(id);
  20644. }
  20645. else {
  20646. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  20647. }
  20648. if (!bl) {
  20649. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  20650. script_pushint(st,0);
  20651. return SCRIPT_CMD_FAILURE;
  20652. }
  20653. if (script_hasdata(st, 4)) {
  20654. target = static_cast<send_target>(script_getnum(st, 4));
  20655. if (target == SELF && map_id2sd(bl->id) == nullptr) {
  20656. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  20657. return SCRIPT_CMD_FAILURE;
  20658. }
  20659. }
  20660. clif_showscript(bl, msg, target);
  20661. script_pushint(st,1);
  20662. return SCRIPT_CMD_SUCCESS;
  20663. }
  20664. /**
  20665. * Ignore the SECURE_NPCTIMEOUT function.
  20666. * ignoretimeout <flag>{,<char_id>};
  20667. */
  20668. BUILDIN_FUNC(ignoretimeout)
  20669. {
  20670. #ifdef SECURE_NPCTIMEOUT
  20671. map_session_data *sd = nullptr;
  20672. if (script_hasdata(st,3)) {
  20673. if (!script_isstring(st,3))
  20674. sd = map_charid2sd(script_getnum(st,3));
  20675. else
  20676. sd = map_nick2sd(script_getstr(st,3),false);
  20677. } else
  20678. script_rid2sd(sd);
  20679. if (!sd)
  20680. return SCRIPT_CMD_FAILURE;
  20681. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  20682. #endif
  20683. return SCRIPT_CMD_SUCCESS;
  20684. }
  20685. /**
  20686. * geteleminfo <type>{,<char_id>};
  20687. **/
  20688. BUILDIN_FUNC(geteleminfo) {
  20689. map_session_data *sd = nullptr;
  20690. if (!script_charid2sd(3, sd)) {
  20691. script_pushint(st, 0);
  20692. return SCRIPT_CMD_FAILURE;
  20693. }
  20694. s_elemental_data *ed = nullptr;
  20695. if (!(ed = sd->ed)) {
  20696. script_pushint(st, 0);
  20697. return SCRIPT_CMD_SUCCESS;
  20698. }
  20699. int32 type = script_getnum(st,2);
  20700. switch (type) {
  20701. case ELEMINFO_ID:
  20702. script_pushint(st, ed->elemental.elemental_id);
  20703. break;
  20704. case ELEMINFO_GAMEID:
  20705. script_pushint(st, ed->bl.id);
  20706. break;
  20707. case ELEMINFO_CLASS:
  20708. script_pushint(st, ed->elemental.class_);
  20709. break;
  20710. default:
  20711. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  20712. script_pushint(st, 0);
  20713. return SCRIPT_CMD_FAILURE;
  20714. }
  20715. return SCRIPT_CMD_SUCCESS;
  20716. }
  20717. /**
  20718. * opendressroom(<flag>{,<char_id>});
  20719. */
  20720. BUILDIN_FUNC(opendressroom)
  20721. {
  20722. #if PACKETVER >= 20150513
  20723. int32 flag = 1;
  20724. TBL_PC* sd;
  20725. if( script_hasdata(st,2) )
  20726. flag = script_getnum(st,2);
  20727. if (!script_charid2sd(3, sd))
  20728. return SCRIPT_CMD_FAILURE;
  20729. clif_dressing_room(sd, flag);
  20730. return SCRIPT_CMD_SUCCESS;
  20731. #else
  20732. return SCRIPT_CMD_FAILURE;
  20733. #endif
  20734. }
  20735. /**
  20736. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  20737. */
  20738. BUILDIN_FUNC(navigateto){
  20739. #if PACKETVER >= 20111010
  20740. TBL_PC* sd;
  20741. const char *mapname;
  20742. uint16 x = 0, y = 0, monster_id = 0;
  20743. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  20744. bool hideWindow = true;
  20745. mapname = script_getstr(st,2);
  20746. if( script_hasdata(st,3) )
  20747. x = script_getnum(st,3);
  20748. if( script_hasdata(st,4) )
  20749. y = script_getnum(st,4);
  20750. if( script_hasdata(st,5) )
  20751. flag = (uint8)script_getnum(st,5);
  20752. if( script_hasdata(st,6) )
  20753. hideWindow = script_getnum(st,6) ? true : false;
  20754. if( script_hasdata(st,7) )
  20755. monster_id = script_getnum(st,7);
  20756. if (!script_charid2sd(8, sd))
  20757. return SCRIPT_CMD_FAILURE;
  20758. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  20759. return SCRIPT_CMD_SUCCESS;
  20760. #else
  20761. return SCRIPT_CMD_FAILURE;
  20762. #endif
  20763. }
  20764. /**
  20765. * adopt("<parent_name>","<baby_name>");
  20766. * adopt(<parent_id>,<baby_id>);
  20767. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  20768. */
  20769. BUILDIN_FUNC(adopt)
  20770. {
  20771. TBL_PC *sd, *b_sd;
  20772. enum adopt_responses response;
  20773. if (script_isstring(st, 2)) {
  20774. const char *name = script_getstr(st, 2);
  20775. sd = map_nick2sd(name,false);
  20776. if (sd == nullptr) {
  20777. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  20778. return SCRIPT_CMD_FAILURE;
  20779. }
  20780. } else {
  20781. uint32 char_id = script_getnum(st, 2);
  20782. sd = map_charid2sd(char_id);
  20783. if (sd == nullptr) {
  20784. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  20785. return SCRIPT_CMD_FAILURE;
  20786. }
  20787. }
  20788. if (script_isstring(st, 3)) {
  20789. const char *name = script_getstr(st, 3);
  20790. b_sd = map_nick2sd(name,false);
  20791. if (b_sd == nullptr) {
  20792. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  20793. return SCRIPT_CMD_FAILURE;
  20794. }
  20795. } else {
  20796. uint32 char_id = script_getnum(st, 3);
  20797. b_sd = map_charid2sd(char_id);
  20798. if (b_sd == nullptr) {
  20799. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  20800. return SCRIPT_CMD_FAILURE;
  20801. }
  20802. }
  20803. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  20804. if (response == ADOPT_ALLOWED) {
  20805. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  20806. b_sd->adopt_invite = sd->status.account_id;
  20807. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  20808. script_pushint(st, ADOPT_ALLOWED);
  20809. return SCRIPT_CMD_SUCCESS;
  20810. }
  20811. script_pushint(st, response);
  20812. return SCRIPT_CMD_FAILURE;
  20813. }
  20814. /**
  20815. * Returns the minimum or maximum of all the given parameters for integer variables.
  20816. *
  20817. * min( <value or array>{,value or array 2,...} );
  20818. * minimum( <value or array>{,value or array 2,...} );
  20819. * max( <value or array>{,value or array 2,...} );
  20820. * maximum( <value or array>{,value or array 2,...} );
  20821. */
  20822. BUILDIN_FUNC(minmax){
  20823. char *functionname;
  20824. uint32 i;
  20825. int64 value;
  20826. // Function pointer for our comparison function (either min or max at the moment)
  20827. int64 (*func)(int64, int64);
  20828. // Get the real function name
  20829. functionname = script_getfuncname(st);
  20830. // Our data should start at offset 2
  20831. i = 2;
  20832. if( !script_hasdata( st, i ) ){
  20833. ShowError( "buildin_%s: no arguments given!\n", functionname );
  20834. st->state = END;
  20835. return SCRIPT_CMD_FAILURE;
  20836. }
  20837. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  20838. value = SCRIPT_INT_MAX;
  20839. func = i64min;
  20840. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  20841. value = SCRIPT_INT_MIN;
  20842. func = i64max;
  20843. }else{
  20844. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  20845. st->state = END;
  20846. return SCRIPT_CMD_FAILURE;
  20847. }
  20848. // As long as we have data on our script stack
  20849. while( script_hasdata(st,i) ){
  20850. struct script_data *data;
  20851. // Get the next piece of data from the script stack
  20852. data = script_getdata( st, i );
  20853. // Is the current parameter a single integer?
  20854. if( data_isint( data ) ){
  20855. value = func( value, script_getnum( st, i ) );
  20856. // Is the current parameter an array variable?
  20857. }else if( data_isreference( data ) ){
  20858. const char *name;
  20859. map_session_data* sd;
  20860. uint32 start, end;
  20861. // Get the name of the variable
  20862. name = reference_getname(data);
  20863. // Check if it's a string variable
  20864. if( is_string_variable( name ) ){
  20865. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20866. script_reportdata( data );
  20867. st->state = END;
  20868. return SCRIPT_CMD_FAILURE;
  20869. }
  20870. // Get the session data, if a player is attached
  20871. sd = st->rid ? map_id2sd(st->rid) : nullptr;
  20872. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20873. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20874. return SCRIPT_CMD_FAILURE;
  20875. }
  20876. // Try to find the array's source pointer
  20877. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20878. ShowError( "buildin_%s: not a array!\n", functionname );
  20879. script_reportdata( data );
  20880. st->state = END;
  20881. return SCRIPT_CMD_FAILURE;
  20882. }
  20883. // Get the start and end indices of the array
  20884. start = reference_getindex( data );
  20885. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20886. // Skip empty arrays
  20887. if( start < end ){
  20888. int32 id;
  20889. // For getting the values we need the id of the array
  20890. id = reference_getid( data );
  20891. // Loop through each value stored in the array
  20892. for( ; start < end; start++ ){
  20893. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20894. }
  20895. }
  20896. else {
  20897. value = func( value, 0 );
  20898. }
  20899. }else{
  20900. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20901. script_reportdata( data );
  20902. st->state = END;
  20903. return SCRIPT_CMD_FAILURE;
  20904. }
  20905. // Continue with the next stack entry
  20906. i++;
  20907. }
  20908. script_pushint( st, value );
  20909. return SCRIPT_CMD_SUCCESS;
  20910. }
  20911. BUILDIN_FUNC(cap_value)
  20912. {
  20913. script_pushint64(st, cap_value(script_getnum64(st, 2), script_getnum64(st, 3), script_getnum64(st, 4)));
  20914. return SCRIPT_CMD_SUCCESS;
  20915. }
  20916. /**
  20917. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20918. * Unlike `getexp` that affected by some adjustments.
  20919. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20920. * @author [Cydh]
  20921. **/
  20922. BUILDIN_FUNC(getexp2) {
  20923. TBL_PC *sd = nullptr;
  20924. int64 base_exp = script_getnum64(st, 2);
  20925. int64 job_exp = script_getnum64(st, 3);
  20926. if (!script_charid2sd(4, sd))
  20927. return SCRIPT_CMD_FAILURE;
  20928. if( base_exp == 0 && job_exp == 0 ){
  20929. ShowError( "buildin_getexp2: Called with base and job exp 0.\n" );
  20930. return SCRIPT_CMD_FAILURE;
  20931. }
  20932. if (base_exp > 0)
  20933. pc_gainexp(sd, nullptr, base_exp, 0, 2);
  20934. else if (base_exp < 0)
  20935. pc_lostexp(sd, base_exp * -1, 0);
  20936. if (job_exp > 0)
  20937. pc_gainexp(sd, nullptr, 0, job_exp, 2);
  20938. else if (job_exp < 0)
  20939. pc_lostexp(sd, 0, job_exp * -1);
  20940. return SCRIPT_CMD_SUCCESS;
  20941. }
  20942. /**
  20943. * Force stat recalculation of sd
  20944. * recalculatestat;
  20945. * @author [secretdataz]
  20946. **/
  20947. BUILDIN_FUNC(recalculatestat) {
  20948. TBL_PC* sd;
  20949. if (!script_rid2sd(sd))
  20950. return SCRIPT_CMD_FAILURE;
  20951. status_calc_pc(sd, SCO_FORCE);
  20952. return SCRIPT_CMD_SUCCESS;
  20953. }
  20954. BUILDIN_FUNC(hateffect){
  20955. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20956. map_session_data* sd;
  20957. if( !script_rid2sd(sd) )
  20958. return SCRIPT_CMD_FAILURE;
  20959. int16 effectID = script_getnum(st,2);
  20960. bool enable = script_getnum(st,3) ? true : false;
  20961. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20962. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20963. return SCRIPT_CMD_FAILURE;
  20964. }
  20965. auto it = util::vector_get( sd->hatEffects, effectID );
  20966. if( enable ){
  20967. if( it != sd->hatEffects.end() ){
  20968. return SCRIPT_CMD_SUCCESS;
  20969. }
  20970. sd->hatEffects.push_back( effectID );
  20971. }else{
  20972. if( it == sd->hatEffects.end() ){
  20973. return SCRIPT_CMD_SUCCESS;
  20974. }
  20975. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20976. }
  20977. if( !sd->state.connect_new ){
  20978. clif_hat_effect_single( *sd, effectID, enable );
  20979. }
  20980. #endif
  20981. return SCRIPT_CMD_SUCCESS;
  20982. }
  20983. /**
  20984. * Retrieves param of current random option. Intended for random option script only.
  20985. * getrandomoptinfo(<type>);
  20986. * @author [secretdataz]
  20987. **/
  20988. BUILDIN_FUNC(getrandomoptinfo) {
  20989. map_session_data *sd;
  20990. int32 val;
  20991. if (script_rid2sd(sd) && current_equip_item_index != -1 && current_equip_opt_index != -1 && sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id) {
  20992. int32 param = script_getnum(st, 2);
  20993. switch (param) {
  20994. case ROA_ID:
  20995. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20996. break;
  20997. case ROA_VALUE:
  20998. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20999. break;
  21000. case ROA_PARAM:
  21001. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  21002. break;
  21003. default:
  21004. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  21005. val = 0;
  21006. break;
  21007. }
  21008. script_pushint(st, val);
  21009. }
  21010. else {
  21011. script_pushint(st, 0);
  21012. }
  21013. return SCRIPT_CMD_SUCCESS;
  21014. }
  21015. /**
  21016. * Retrieves a random option on an equipped item.
  21017. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  21018. * @author [secretdataz]
  21019. */
  21020. BUILDIN_FUNC(getequiprandomoption) {
  21021. map_session_data *sd;
  21022. int32 val;
  21023. short i = -1;
  21024. int32 pos = script_getnum(st, 2);
  21025. int32 index = script_getnum(st, 3);
  21026. int32 type = script_getnum(st, 4);
  21027. if (!script_charid2sd(5, sd)) {
  21028. script_pushint(st, -1);
  21029. return SCRIPT_CMD_FAILURE;
  21030. }
  21031. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  21032. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  21033. script_pushint(st, -1);
  21034. return SCRIPT_CMD_FAILURE;
  21035. }
  21036. if (equip_index_check(pos))
  21037. i = pc_checkequip(sd, equip_bitmask[pos]);
  21038. if (i < 0) {
  21039. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  21040. script_pushint(st, -1);
  21041. return SCRIPT_CMD_FAILURE;
  21042. }
  21043. switch (type) {
  21044. case ROA_ID:
  21045. val = sd->inventory.u.items_inventory[i].option[index].id;
  21046. break;
  21047. case ROA_VALUE:
  21048. val = sd->inventory.u.items_inventory[i].option[index].value;
  21049. break;
  21050. case ROA_PARAM:
  21051. val = sd->inventory.u.items_inventory[i].option[index].param;
  21052. break;
  21053. default:
  21054. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  21055. val = 0;
  21056. break;
  21057. }
  21058. script_pushint(st, val);
  21059. return SCRIPT_CMD_SUCCESS;
  21060. }
  21061. /**
  21062. * Adds a random option on a random option slot on an equipped item and overwrites
  21063. * existing random option in the process.
  21064. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  21065. * @author [secretdataz]
  21066. */
  21067. BUILDIN_FUNC(setrandomoption) {
  21068. map_session_data *sd;
  21069. int32 pos, index, id, value, param, ep;
  21070. int32 i = -1;
  21071. if (!script_charid2sd(7, sd))
  21072. return SCRIPT_CMD_FAILURE;
  21073. pos = script_getnum(st, 2);
  21074. index = script_getnum(st, 3);
  21075. id = script_getnum(st, 4);
  21076. value = script_getnum(st, 5);
  21077. param = script_getnum(st, 6);
  21078. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  21079. if (opt == nullptr) {
  21080. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  21081. script_pushint(st, 0);
  21082. return SCRIPT_CMD_FAILURE;
  21083. }
  21084. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  21085. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  21086. script_pushint(st, 0);
  21087. return SCRIPT_CMD_FAILURE;
  21088. }
  21089. if (equip_index_check(pos))
  21090. i = pc_checkequip(sd, equip_bitmask[pos]);
  21091. if (i >= 0) {
  21092. ep = sd->inventory.u.items_inventory[i].equip;
  21093. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  21094. pc_unequipitem(sd, i, 2);
  21095. sd->inventory.u.items_inventory[i].option[index].id = id;
  21096. sd->inventory.u.items_inventory[i].option[index].value = value;
  21097. sd->inventory.u.items_inventory[i].option[index].param = param;
  21098. clif_delitem( *sd, i, 1, 3 );
  21099. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  21100. clif_additem(sd, i, 1, 0);
  21101. pc_equipitem(sd, i, ep);
  21102. script_pushint(st, 1);
  21103. return SCRIPT_CMD_SUCCESS;
  21104. }
  21105. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  21106. script_pushint(st, 0);
  21107. return SCRIPT_CMD_FAILURE;
  21108. }
  21109. /// Returns the number of stat points needed to change the specified stat by val.
  21110. /// If val is negative, returns the number of stat points that would be needed to
  21111. /// raise the specified stat from (current value - val) to current value.
  21112. /// *needed_status_point(<type>,<val>{,<char id>});
  21113. /// @author [secretdataz]
  21114. BUILDIN_FUNC(needed_status_point) {
  21115. map_session_data *sd;
  21116. int32 type, val;
  21117. if (!script_charid2sd(4, sd))
  21118. return SCRIPT_CMD_FAILURE;
  21119. type = script_getnum(st, 2);
  21120. val = script_getnum(st, 3);
  21121. script_pushint(st, pc_need_status_point(sd, type, val));
  21122. return SCRIPT_CMD_SUCCESS;
  21123. }
  21124. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  21125. /// If val is negative, returns the number of trait stat points that would be needed to
  21126. /// raise the specified trait stat from (current value - val) to current value.
  21127. /// *needed_trait_point(<type>,<val>{,<char id>});
  21128. BUILDIN_FUNC(needed_trait_point) {
  21129. map_session_data *sd;
  21130. if (!script_charid2sd(4, sd))
  21131. return SCRIPT_CMD_FAILURE;
  21132. int32 type = script_getnum( st, 2 );
  21133. if( type < SP_POW || type > SP_CRT ){
  21134. ShowError( "buildin_needed_trait_point: Unknown trait type %d\n", type );
  21135. return SCRIPT_CMD_FAILURE;
  21136. }
  21137. script_pushint( st, pc_need_trait_point( sd, type, script_getnum( st, 3 ) ) );
  21138. return SCRIPT_CMD_SUCCESS;
  21139. }
  21140. /**
  21141. * jobcanentermap("<mapname>"{,<JobID>});
  21142. * Check if (player with) JobID can enter the map.
  21143. * @param mapname Map name
  21144. * @param JobID Player's JobID (optional)
  21145. **/
  21146. BUILDIN_FUNC(jobcanentermap) {
  21147. const char *mapname = script_getstr(st, 2);
  21148. int32 mapidx = mapindex_name2id(mapname), m = -1;
  21149. int32 jobid = 0;
  21150. TBL_PC *sd = nullptr;
  21151. if (!mapidx) {// Invalid map
  21152. script_pushint(st, false);
  21153. return SCRIPT_CMD_FAILURE;
  21154. }
  21155. m = map_mapindex2mapid(mapidx);
  21156. if (m == -1) { // Map is on different map server
  21157. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  21158. script_pushint(st, false);
  21159. return SCRIPT_CMD_FAILURE;
  21160. }
  21161. if (script_hasdata(st, 3)) {
  21162. jobid = script_getnum(st, 3);
  21163. } else {
  21164. if (!script_rid2sd(sd)) {
  21165. script_pushint(st, false);
  21166. return SCRIPT_CMD_FAILURE;
  21167. }
  21168. jobid = sd->status.class_;
  21169. }
  21170. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? pc_get_group_level(sd) : 0));
  21171. return SCRIPT_CMD_SUCCESS;
  21172. }
  21173. /**
  21174. * Return alliance information between the two guilds.
  21175. * getguildalliance(<guild id1>,<guild id2>);
  21176. * Return values:
  21177. * -2 - Guild ID1 does not exist
  21178. * -1 - Guild ID2 does not exist
  21179. * 0 - Both guilds have no relation OR guild ID aren't given
  21180. * 1 - Both guilds are allies
  21181. * 2 - Both guilds are antagonists
  21182. */
  21183. BUILDIN_FUNC(getguildalliance)
  21184. {
  21185. int32 guild_id1, guild_id2, i = 0;
  21186. guild_id1 = script_getnum(st,2);
  21187. guild_id2 = script_getnum(st,3);
  21188. if (guild_id1 < 1 || guild_id2 < 1) {
  21189. script_pushint(st, 0);
  21190. return SCRIPT_CMD_SUCCESS;
  21191. }
  21192. if (guild_id1 == guild_id2) {
  21193. script_pushint(st, 1);
  21194. return SCRIPT_CMD_SUCCESS;
  21195. }
  21196. auto guild_data1 = guild_search(guild_id1);
  21197. auto guild_data2 = guild_search(guild_id2);
  21198. if (guild_data1 == nullptr) {
  21199. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  21200. script_pushint(st, -2);
  21201. return SCRIPT_CMD_FAILURE;
  21202. }
  21203. if (guild_data2 == nullptr) {
  21204. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  21205. script_pushint(st, -1);
  21206. return SCRIPT_CMD_FAILURE;
  21207. }
  21208. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->guild.alliance[i].guild_id == guild_id2);
  21209. if (i == MAX_GUILDALLIANCE) {
  21210. script_pushint(st, 0);
  21211. return SCRIPT_CMD_SUCCESS;
  21212. }
  21213. if (guild_data1->guild.alliance[i].opposition)
  21214. script_pushint(st, 2);
  21215. else
  21216. script_pushint(st, 1);
  21217. return SCRIPT_CMD_SUCCESS;
  21218. }
  21219. /*
  21220. * openstorage2 <storage_id>{,<mode>{,<account_id>}}
  21221. * mode @see enum e_storage_mode
  21222. **/
  21223. BUILDIN_FUNC(openstorage2) {
  21224. map_session_data *sd = nullptr;
  21225. if (!script_accid2sd(4, sd)) {
  21226. st->state = END;
  21227. return SCRIPT_CMD_FAILURE;
  21228. }
  21229. int32 stor_id = script_getnum(st, 2);
  21230. if (!storage_exists(stor_id)) {
  21231. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  21232. st->state = END;
  21233. return SCRIPT_CMD_FAILURE;
  21234. }
  21235. int32 mode;
  21236. if (!script_hasdata(st, 3))
  21237. mode = STOR_MODE_ALL;
  21238. else {
  21239. mode = script_getnum(st, 3);
  21240. if (mode < STOR_MODE_NONE || mode > STOR_MODE_ALL) {
  21241. ShowError("buildin_openstorage2: Invalid mode '%d'!\n", mode);
  21242. st->state = END;
  21243. return SCRIPT_CMD_FAILURE;
  21244. }
  21245. }
  21246. script_pushint(st, storage_premiumStorage_load(sd, stor_id, mode));
  21247. return SCRIPT_CMD_SUCCESS;
  21248. }
  21249. /**
  21250. * Create a new channel
  21251. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  21252. * @author [Cydh]
  21253. **/
  21254. BUILDIN_FUNC(channel_create) {
  21255. struct Channel tmp_chan, *ch = nullptr;
  21256. const char *chname = script_getstr(st,2), *pass = nullptr;
  21257. int32 i = channel_chk((char*)chname, nullptr, 3);
  21258. TBL_PC *sd = nullptr;
  21259. if (i != 0) {
  21260. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  21261. script_pushint(st,i);
  21262. return SCRIPT_CMD_FAILURE;
  21263. }
  21264. memset(&tmp_chan, 0, sizeof(struct Channel));
  21265. if (script_hasdata(st,8)) {
  21266. tmp_chan.char_id = script_getnum(st,8);
  21267. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  21268. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  21269. script_pushint(st,-5);
  21270. return SCRIPT_CMD_FAILURE;
  21271. }
  21272. tmp_chan.type = CHAN_TYPE_PRIVATE;
  21273. i = 1;
  21274. }
  21275. else {
  21276. tmp_chan.type = CHAN_TYPE_PUBLIC;
  21277. i = 0;
  21278. }
  21279. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  21280. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  21281. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  21282. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  21283. if (script_hasdata(st,5))
  21284. tmp_chan.opt = script_getnum(st,5);
  21285. else
  21286. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  21287. if (script_hasdata(st,6))
  21288. tmp_chan.msg_delay = script_getnum(st,6);
  21289. else
  21290. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  21291. if (script_hasdata(st,7))
  21292. tmp_chan.color = script_getnum(st,7);
  21293. else
  21294. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  21295. if (!(ch = channel_create(&tmp_chan))) {
  21296. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  21297. script_pushint(st,0);
  21298. return SCRIPT_CMD_FAILURE;
  21299. }
  21300. if (tmp_chan.char_id)
  21301. channel_join(ch, sd);
  21302. script_pushint(st,1);
  21303. return SCRIPT_CMD_SUCCESS;
  21304. }
  21305. // ===================================
  21306. // *channel_join "<channel_name>"{, <char_id>};
  21307. // Join an existing channel.
  21308. // The command returns 0 upon success, and these values upon failure:
  21309. // -1 : Invalid channel or player
  21310. // -2 : Player already in channel
  21311. // -3 : Player banned
  21312. // -4 : Reached max limit
  21313. // ===================================
  21314. BUILDIN_FUNC(channel_join) {
  21315. map_session_data *sd = nullptr;
  21316. if (!script_charid2sd(3, sd)) {
  21317. script_pushint(st, -1);
  21318. return SCRIPT_CMD_FAILURE;
  21319. }
  21320. struct Channel *channel = nullptr;
  21321. const char *chname = script_getstr(st, 2);
  21322. if (!(channel = channel_name2channel((char *)chname, nullptr, 0))) {
  21323. ShowError("buildin_channel_join: Channel name '%s' is invalid.\n", chname);
  21324. script_pushint(st, -1);
  21325. return SCRIPT_CMD_FAILURE;
  21326. }
  21327. script_pushint(st, channel_join(channel, sd));
  21328. return SCRIPT_CMD_SUCCESS;
  21329. }
  21330. /**
  21331. * Set channel option
  21332. * channel_setopt "<chname>",<option>,<value>;
  21333. * @author [Cydh]
  21334. **/
  21335. BUILDIN_FUNC(channel_setopt) {
  21336. struct Channel *ch = nullptr;
  21337. const char *chname = script_getstr(st,2);
  21338. int32 opt = script_getnum(st,3), value = script_getnum(st,4);
  21339. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21340. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  21341. script_pushint(st,0);
  21342. return SCRIPT_CMD_FAILURE;
  21343. }
  21344. switch (opt) {
  21345. case CHAN_OPT_ANNOUNCE_SELF:
  21346. case CHAN_OPT_ANNOUNCE_JOIN:
  21347. case CHAN_OPT_ANNOUNCE_LEAVE:
  21348. case CHAN_OPT_COLOR_OVERRIDE:
  21349. case CHAN_OPT_CAN_CHAT:
  21350. case CHAN_OPT_CAN_LEAVE:
  21351. case CHAN_OPT_AUTOJOIN:
  21352. if (value)
  21353. ch->opt |= opt;
  21354. else
  21355. ch->opt &= ~opt;
  21356. break;
  21357. case CHAN_OPT_MSG_DELAY:
  21358. ch->msg_delay = value;
  21359. break;
  21360. default:
  21361. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  21362. script_pushint(st,0);
  21363. return SCRIPT_CMD_FAILURE;
  21364. }
  21365. script_pushint(st,1);
  21366. return SCRIPT_CMD_SUCCESS;
  21367. }
  21368. /**
  21369. * Get channel options
  21370. * channel_getopt <chname>,<option>;
  21371. */
  21372. BUILDIN_FUNC(channel_getopt) {
  21373. Channel *ch;
  21374. const char *chname = script_getstr(st, 2);
  21375. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21376. ShowError("buildin_channel_getopt: Channel name '%s' is invalid.\n", chname);
  21377. script_pushint(st, false);
  21378. return SCRIPT_CMD_FAILURE;
  21379. }
  21380. int32 opt = script_getnum(st, 3);
  21381. switch (opt) {
  21382. case CHAN_OPT_ANNOUNCE_SELF:
  21383. case CHAN_OPT_ANNOUNCE_JOIN:
  21384. case CHAN_OPT_ANNOUNCE_LEAVE:
  21385. case CHAN_OPT_COLOR_OVERRIDE:
  21386. case CHAN_OPT_CAN_CHAT:
  21387. case CHAN_OPT_CAN_LEAVE:
  21388. case CHAN_OPT_AUTOJOIN:
  21389. script_pushint(st, (ch->opt & opt) != 0);
  21390. break;
  21391. case CHAN_OPT_MSG_DELAY:
  21392. script_pushint(st, ch->msg_delay);
  21393. break;
  21394. default:
  21395. ShowError("buildin_channel_getopt: Invalid option %d!\n", opt);
  21396. script_pushint(st, false);
  21397. return SCRIPT_CMD_FAILURE;
  21398. }
  21399. return SCRIPT_CMD_SUCCESS;
  21400. }
  21401. /**
  21402. * Set channel color
  21403. * channel_setcolor "<chname>",<color>;
  21404. * @author [Cydh]
  21405. **/
  21406. BUILDIN_FUNC(channel_setcolor) {
  21407. struct Channel *ch = nullptr;
  21408. const char *chname = script_getstr(st,2);
  21409. int32 color = script_getnum(st,3);
  21410. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21411. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  21412. script_pushint(st,0);
  21413. return SCRIPT_CMD_FAILURE;
  21414. }
  21415. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  21416. script_pushint(st,1);
  21417. return SCRIPT_CMD_SUCCESS;
  21418. }
  21419. /**
  21420. * Set channel password
  21421. * channel_setpass "<chname>","<password>";
  21422. * @author [Cydh]
  21423. **/
  21424. BUILDIN_FUNC(channel_setpass) {
  21425. struct Channel *ch = nullptr;
  21426. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  21427. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21428. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  21429. script_pushint(st,0);
  21430. return SCRIPT_CMD_FAILURE;
  21431. }
  21432. if (!passwd || !strcmpi(passwd,"null"))
  21433. memset(ch->pass, '\0', sizeof(ch->pass));
  21434. else
  21435. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  21436. script_pushint(st,1);
  21437. return SCRIPT_CMD_SUCCESS;
  21438. }
  21439. /**
  21440. * Set authorized groups
  21441. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  21442. * @author [Cydh]
  21443. **/
  21444. BUILDIN_FUNC(channel_setgroup) {
  21445. struct Channel *ch = nullptr;
  21446. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  21447. int32 i, n = 0, group = 0;
  21448. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21449. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  21450. script_pushint(st,0);
  21451. return SCRIPT_CMD_FAILURE;
  21452. }
  21453. if (funcname[strlen(funcname)-1] == '2') {
  21454. struct script_data *data = script_getdata(st,3);
  21455. const char *varname = reference_getname(data);
  21456. int32 id, idx;
  21457. if (varname[strlen(varname)-1] == '$') {
  21458. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  21459. script_pushint(st,0);
  21460. return SCRIPT_CMD_FAILURE;
  21461. }
  21462. if (not_server_variable(*varname)) {
  21463. map_session_data *sd;
  21464. if (!script_rid2sd(sd)) {
  21465. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  21466. return SCRIPT_CMD_FAILURE;
  21467. }
  21468. }
  21469. n = script_array_highest_key(st, nullptr, reference_getname(data), reference_getref(data));
  21470. if (n < 1) {
  21471. ShowError("buildin_channel_setgroup: No group id listed.\n");
  21472. script_pushint(st,0);
  21473. return SCRIPT_CMD_FAILURE;
  21474. }
  21475. if (ch->groups)
  21476. aFree(ch->groups);
  21477. ch->groups = nullptr;
  21478. ch->group_count = 0;
  21479. id = reference_getid(data);
  21480. idx = reference_getindex(data);
  21481. for (i = 0; i < n; i++) {
  21482. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  21483. if (group == 0) {
  21484. script_pushint(st,1);
  21485. return SCRIPT_CMD_SUCCESS;
  21486. }
  21487. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  21488. ch->groups[ch->group_count-1] = group;
  21489. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21490. }
  21491. }
  21492. else {
  21493. group = script_getnum(st,3);
  21494. n = script_lastdata(st)-1;
  21495. if (n < 1) {
  21496. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  21497. script_pushint(st,0);
  21498. return SCRIPT_CMD_FAILURE;
  21499. }
  21500. if (ch->groups)
  21501. aFree(ch->groups);
  21502. ch->groups = nullptr;
  21503. ch->group_count = 0;
  21504. if (group == 0) { // Removed group list
  21505. script_pushint(st,1);
  21506. return SCRIPT_CMD_SUCCESS;
  21507. }
  21508. CREATE(ch->groups, unsigned short, n);
  21509. for (i = 3; i < n+2; i++) {
  21510. ch->groups[ch->group_count++] = script_getnum(st,i);
  21511. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21512. }
  21513. }
  21514. script_pushint(st,n);
  21515. return SCRIPT_CMD_SUCCESS;
  21516. }
  21517. /**
  21518. * Send message on channel
  21519. * channel_chat "<chname>","<message>"{,<color>};
  21520. * @author [Cydh]
  21521. **/
  21522. BUILDIN_FUNC(channel_chat) {
  21523. struct Channel *ch = nullptr;
  21524. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  21525. char output[CHAT_SIZE_MAX+1];
  21526. unsigned long color = 0;
  21527. // Check also local channels
  21528. if (chname[0] != '#') { // By Map
  21529. int32 m = mapindex_name2id(chname);
  21530. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  21531. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  21532. script_pushint(st,0);
  21533. return SCRIPT_CMD_FAILURE;
  21534. }
  21535. if (!(ch = map_getmapdata(m)->channel)) {
  21536. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21537. script_pushint(st,0);
  21538. return SCRIPT_CMD_SUCCESS;
  21539. }
  21540. }
  21541. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  21542. TBL_NPC *nd = map_id2nd(st->oid);
  21543. if (!nd || nd->bl.m == -1) {
  21544. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  21545. script_pushint(st,0);
  21546. return SCRIPT_CMD_FAILURE;
  21547. }
  21548. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  21549. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21550. script_pushint(st,0);
  21551. return SCRIPT_CMD_SUCCESS;
  21552. }
  21553. }
  21554. else if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21555. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  21556. script_pushint(st,0);
  21557. return SCRIPT_CMD_FAILURE;
  21558. }
  21559. if (!ch) {
  21560. script_pushint(st,0);
  21561. return SCRIPT_CMD_FAILURE;
  21562. }
  21563. color = ch->color;
  21564. FETCH(4, color);
  21565. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  21566. clif_channel_msg(ch,output,color);
  21567. script_pushint(st,1);
  21568. return SCRIPT_CMD_SUCCESS;
  21569. }
  21570. /**
  21571. * Ban player from a channel
  21572. * channel_ban "<chname>",<char_id>;
  21573. * @author [Cydh]
  21574. **/
  21575. BUILDIN_FUNC(channel_ban) {
  21576. struct Channel *ch = nullptr;
  21577. const char *chname = script_getstr(st,2);
  21578. uint32 char_id = script_getnum(st,3);
  21579. TBL_PC *tsd;
  21580. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21581. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  21582. script_pushint(st,0);
  21583. return SCRIPT_CMD_FAILURE;
  21584. }
  21585. if (ch->char_id == char_id) {
  21586. script_pushint(st,0);
  21587. return SCRIPT_CMD_SUCCESS;
  21588. }
  21589. tsd = map_charid2sd(char_id);
  21590. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21591. script_pushint(st,0);
  21592. return SCRIPT_CMD_SUCCESS;
  21593. }
  21594. if (!idb_exists(ch->banned, char_id)) {
  21595. struct chan_banentry *cbe;
  21596. char output[CHAT_SIZE_MAX+1];
  21597. CREATE(cbe, struct chan_banentry, 1);
  21598. cbe->char_id = char_id;
  21599. if (tsd) {
  21600. strcpy(cbe->char_name,tsd->status.name);
  21601. channel_clean(ch,tsd,0);
  21602. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  21603. }
  21604. else
  21605. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  21606. idb_put(ch->banned, char_id, cbe);
  21607. clif_channel_msg(ch,output,ch->color);
  21608. }
  21609. script_pushint(st,1);
  21610. return SCRIPT_CMD_SUCCESS;
  21611. }
  21612. /**
  21613. * Ban player from a channel
  21614. * channel_unban "<chname>",<char_id>;
  21615. * @author [Cydh]
  21616. **/
  21617. BUILDIN_FUNC(channel_unban) {
  21618. struct Channel *ch = nullptr;
  21619. const char *chname = script_getstr(st,2);
  21620. uint32 char_id = script_getnum(st,3);
  21621. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21622. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  21623. script_pushint(st,0);
  21624. return SCRIPT_CMD_FAILURE;
  21625. }
  21626. if (ch->char_id == char_id) {
  21627. script_pushint(st,0);
  21628. return SCRIPT_CMD_SUCCESS;
  21629. }
  21630. if (idb_exists(ch->banned, char_id)) {
  21631. char output[CHAT_SIZE_MAX+1];
  21632. TBL_PC *tsd = map_charid2sd(char_id);
  21633. if (tsd)
  21634. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  21635. else
  21636. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  21637. idb_remove(ch->banned, char_id);
  21638. clif_channel_msg(ch,output,ch->color);
  21639. }
  21640. script_pushint(st,1);
  21641. return SCRIPT_CMD_SUCCESS;
  21642. }
  21643. /**
  21644. * Kick player from a channel
  21645. * channel_kick "<chname>",<char_id>;
  21646. * channel_kick "<chname>","<char_name>";
  21647. * @author [Cydh]
  21648. **/
  21649. BUILDIN_FUNC(channel_kick) {
  21650. struct Channel *ch = nullptr;
  21651. const char *chname = script_getstr(st,2);
  21652. TBL_PC *tsd = nullptr;
  21653. int32 res = 1;
  21654. if (script_isstring(st, 3)) {
  21655. const char *name = script_getstr(st, 3);
  21656. if (!(tsd = map_nick2sd(name,false))) {
  21657. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  21658. script_pushint(st,0);
  21659. return SCRIPT_CMD_FAILURE;
  21660. }
  21661. } else {
  21662. int32 char_id = script_getnum(st, 3);
  21663. if (!(tsd = map_charid2sd(char_id))) {
  21664. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  21665. script_pushint(st,0);
  21666. return SCRIPT_CMD_FAILURE;
  21667. }
  21668. }
  21669. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  21670. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  21671. script_pushint(st,0);
  21672. return SCRIPT_CMD_FAILURE;
  21673. }
  21674. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21675. script_pushint(st,0);
  21676. return SCRIPT_CMD_SUCCESS;
  21677. }
  21678. switch(ch->type){
  21679. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  21680. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  21681. default: res = channel_clean(ch,tsd,0); break;
  21682. }
  21683. if (res == 0) {
  21684. char output[CHAT_SIZE_MAX+1];
  21685. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  21686. clif_channel_msg(ch,output,ch->color);
  21687. }
  21688. script_pushint(st,1);
  21689. return SCRIPT_CMD_SUCCESS;
  21690. }
  21691. /**
  21692. * Delete a channel
  21693. * channel_delete "<chname>";
  21694. * @author [Cydh]
  21695. **/
  21696. BUILDIN_FUNC(channel_delete) {
  21697. struct Channel *ch = nullptr;
  21698. const char *chname = script_getstr(st,2);
  21699. if (!(ch = channel_name2channel((char *)chname, nullptr, 0))) {
  21700. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  21701. script_pushint(st,-1);
  21702. return SCRIPT_CMD_FAILURE;
  21703. }
  21704. script_pushint(st,channel_delete(ch,true));
  21705. return SCRIPT_CMD_SUCCESS;
  21706. }
  21707. BUILDIN_FUNC(unloadnpc) {
  21708. const char *name;
  21709. struct npc_data* nd;
  21710. name = script_getstr(st, 2);
  21711. nd = npc_name2id(name);
  21712. if( nd == nullptr ){
  21713. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  21714. return SCRIPT_CMD_FAILURE;
  21715. } else if ( nd->bl.id == st->oid ) {
  21716. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  21717. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  21718. return SCRIPT_CMD_FAILURE;
  21719. }
  21720. npc_unload_duplicates(nd);
  21721. npc_unload(nd, true);
  21722. npc_read_event_script();
  21723. return SCRIPT_CMD_SUCCESS;
  21724. }
  21725. /**
  21726. * Duplicate a NPC.
  21727. * Return the duplicate Unique name on success or empty string on failure.
  21728. * duplicate "<NPC name>","<map>",<x>,<y>{,"<Duplicate NPC name>"{,<sprite>{,<dir>{,<xs>{,<xy>}}}}};
  21729. */
  21730. BUILDIN_FUNC(duplicate)
  21731. {
  21732. const char* old_npcname = script_getstr( st, 2 );
  21733. npc_data* nd = npc_name2id( old_npcname );
  21734. if( nd == nullptr ){
  21735. ShowError( "buildin_duplicate: No such NPC '%s'.\n", old_npcname );
  21736. script_pushstrcopy( st, "" );
  21737. return SCRIPT_CMD_FAILURE;
  21738. }
  21739. const char* mapname = script_getstr( st, 3 );
  21740. int16 mapid = map_mapname2mapid( mapname );
  21741. if( mapid < 0 ){
  21742. ShowError( "buildin_duplicate: map '%s' in not found!\n", mapname );
  21743. script_pushstrcopy( st, "" );
  21744. return SCRIPT_CMD_FAILURE;
  21745. }
  21746. struct map_data* mapdata = map_getmapdata( mapid );
  21747. if( mapdata == nullptr ){
  21748. // Should not happen, but who knows...
  21749. ShowError( "buildin_duplicate: mapdata for '%s' is unavailable!\n", mapname );
  21750. script_pushstrcopy( st, "" );
  21751. return SCRIPT_CMD_FAILURE;
  21752. }
  21753. int16 x = script_getnum( st, 4 );
  21754. if( x < 0 || x >= mapdata->xs ){
  21755. ShowError( "buildin_duplicate: x coordinate %hd is out of bounds for map %s[0-%hd]!\n", x, mapname, mapdata->xs );
  21756. script_pushstrcopy( st, "" );
  21757. return SCRIPT_CMD_FAILURE;
  21758. }
  21759. int16 y = script_getnum( st, 5 );
  21760. if( y < 0 || y >= mapdata->ys ){
  21761. ShowError( "buildin_duplicate: y coordinate %hd is out of bounds for map %s[0-%hd]!\n", y, mapname, mapdata->ys );
  21762. script_pushstrcopy( st, "" );
  21763. return SCRIPT_CMD_FAILURE;
  21764. }
  21765. char name[NPC_NAME_LENGTH + 1];
  21766. if( script_hasdata( st, 6 ) ){
  21767. const char* new_name = script_getstr( st, 6 );
  21768. if( strlen( new_name ) > NPC_NAME_LENGTH ){
  21769. ShowError( "buildin_duplicate: new NPC name \"%s\" is too long!\n", new_name );
  21770. script_pushstrcopy( st, "" );
  21771. return SCRIPT_CMD_FAILURE;
  21772. }
  21773. safestrncpy( name, new_name, sizeof( name ) );
  21774. }else{
  21775. safestrncpy( name, nd->name, sizeof( name ) );
  21776. }
  21777. int32 class_;
  21778. if( script_hasdata( st, 7 ) ){
  21779. class_ = script_getnum( st, 7 );
  21780. }else{
  21781. class_ = nd->class_;
  21782. }
  21783. uint8 dir;
  21784. if( script_hasdata( st, 8 ) ){
  21785. dir = script_getnum( st, 8 );
  21786. }else{
  21787. dir = nd->ud.dir;
  21788. }
  21789. int16 xs;
  21790. if( script_hasdata( st, 9 ) ){
  21791. xs = script_getnum( st, 9 );
  21792. }else{
  21793. xs = nd->u.scr.xs;
  21794. }
  21795. int16 ys;
  21796. if( script_hasdata( st, 10 ) ){
  21797. ys = script_getnum( st, 10 );
  21798. }else{
  21799. ys = nd->u.scr.ys;
  21800. }
  21801. npc_data* dnd = npc_duplicate_npc( *nd, name, mapid, x, y, class_, dir, xs, ys );
  21802. if( dnd == nullptr ){
  21803. script_pushstrcopy( st, "" );
  21804. return SCRIPT_CMD_FAILURE;
  21805. }else{
  21806. script_pushstrcopy( st, dnd->exname );
  21807. return SCRIPT_CMD_SUCCESS;
  21808. }
  21809. }
  21810. /**
  21811. * Duplicate a NPC for a player.
  21812. * Return the duplicate Unique name on success or empty string on failure.
  21813. * duplicate_dynamic("<NPC name>"{,<character ID>});
  21814. */
  21815. BUILDIN_FUNC(duplicate_dynamic){
  21816. const char* old_npcname = script_getstr( st, 2 );
  21817. struct npc_data* nd = npc_name2id( old_npcname );
  21818. if( nd == nullptr ){
  21819. ShowError( "buildin_duplicate_dynamic: No such NPC '%s'.\n", old_npcname );
  21820. script_pushstrcopy( st, "" );
  21821. return SCRIPT_CMD_FAILURE;
  21822. }
  21823. map_session_data* sd;
  21824. if( !script_charid2sd( 3, sd ) ){
  21825. script_pushstrcopy( st, "" );
  21826. return SCRIPT_CMD_FAILURE;
  21827. }
  21828. struct npc_data* dnd = npc_duplicate_npc_for_player( *nd, *sd );
  21829. if( dnd == nullptr ){
  21830. script_pushstrcopy( st, "" );
  21831. return SCRIPT_CMD_SUCCESS;
  21832. }else{
  21833. script_pushstrcopy( st, dnd->exname );
  21834. return SCRIPT_CMD_SUCCESS;
  21835. }
  21836. }
  21837. /**
  21838. * Add an achievement to the player's log
  21839. * achievementadd(<achievement ID>{,<char ID>});
  21840. */
  21841. BUILDIN_FUNC(achievementadd) {
  21842. map_session_data *sd;
  21843. int32 achievement_id = script_getnum(st, 2);
  21844. if (!script_charid2sd(3, sd)) {
  21845. script_pushint(st, false);
  21846. return SCRIPT_CMD_FAILURE;
  21847. }
  21848. if (achievement_db.exists(achievement_id) == false) {
  21849. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  21850. script_pushint(st, false);
  21851. return SCRIPT_CMD_FAILURE;
  21852. }
  21853. if( !sd->state.pc_loaded ){
  21854. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21855. return SCRIPT_CMD_SUCCESS;
  21856. }
  21857. if (achievement_add(sd, achievement_id))
  21858. script_pushint(st, true);
  21859. else
  21860. script_pushint(st, false);
  21861. return SCRIPT_CMD_SUCCESS;
  21862. }
  21863. /**
  21864. * Removes an achievement on a player.
  21865. * achievementremove(<achievement ID>{,<char ID>});
  21866. * Just for Atemo. ;)
  21867. */
  21868. BUILDIN_FUNC(achievementremove) {
  21869. map_session_data *sd;
  21870. int32 achievement_id = script_getnum(st, 2);
  21871. if (!script_charid2sd(3, sd)) {
  21872. script_pushint(st, false);
  21873. return SCRIPT_CMD_FAILURE;
  21874. }
  21875. if (achievement_db.exists(achievement_id) == false) {
  21876. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  21877. script_pushint(st, false);
  21878. return SCRIPT_CMD_SUCCESS;
  21879. }
  21880. if( !sd->state.pc_loaded ){
  21881. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21882. return SCRIPT_CMD_SUCCESS;
  21883. }
  21884. if (achievement_remove(sd, achievement_id))
  21885. script_pushint(st, true);
  21886. else
  21887. script_pushint(st, false);
  21888. return SCRIPT_CMD_SUCCESS;
  21889. }
  21890. /**
  21891. * Returns achievement progress
  21892. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  21893. */
  21894. BUILDIN_FUNC(achievementinfo) {
  21895. map_session_data *sd;
  21896. int32 achievement_id = script_getnum(st, 2);
  21897. if (!script_charid2sd(4, sd)) {
  21898. script_pushint(st, false);
  21899. return SCRIPT_CMD_FAILURE;
  21900. }
  21901. if (achievement_db.exists(achievement_id) == false) {
  21902. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  21903. script_pushint(st, false);
  21904. return SCRIPT_CMD_FAILURE;
  21905. }
  21906. if( !sd->state.pc_loaded ){
  21907. script_pushint(st, false);
  21908. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21909. return SCRIPT_CMD_SUCCESS;
  21910. }
  21911. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  21912. return SCRIPT_CMD_SUCCESS;
  21913. }
  21914. /**
  21915. * Award an achievement; Ignores requirements
  21916. * achievementcomplete(<achievement ID>{,<char ID>});
  21917. */
  21918. BUILDIN_FUNC(achievementcomplete) {
  21919. map_session_data *sd;
  21920. int32 i, achievement_id = script_getnum(st, 2);
  21921. if (!script_charid2sd(3, sd)) {
  21922. script_pushint(st, false);
  21923. return SCRIPT_CMD_FAILURE;
  21924. }
  21925. if (achievement_db.exists(achievement_id) == false) {
  21926. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  21927. script_pushint(st, false);
  21928. return SCRIPT_CMD_FAILURE;
  21929. }
  21930. if( !sd->state.pc_loaded ){
  21931. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21932. return SCRIPT_CMD_SUCCESS;
  21933. }
  21934. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21935. if (i == sd->achievement_data.count)
  21936. achievement_add(sd, achievement_id);
  21937. achievement_update_achievement(sd, achievement_id, true);
  21938. script_pushint(st, true);
  21939. return SCRIPT_CMD_SUCCESS;
  21940. }
  21941. /**
  21942. * Checks if the achievement exists on player.
  21943. * achievementexists(<achievement ID>{,<char ID>});
  21944. */
  21945. BUILDIN_FUNC(achievementexists) {
  21946. map_session_data *sd;
  21947. int32 i, achievement_id = script_getnum(st, 2);
  21948. if (!script_charid2sd(3, sd)) {
  21949. script_pushint(st, false);
  21950. return SCRIPT_CMD_FAILURE;
  21951. }
  21952. if (achievement_db.exists(achievement_id) == false) {
  21953. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  21954. script_pushint(st, false);
  21955. return SCRIPT_CMD_SUCCESS;
  21956. }
  21957. if( !sd->state.pc_loaded ){
  21958. script_pushint(st, false);
  21959. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21960. return SCRIPT_CMD_SUCCESS;
  21961. }
  21962. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  21963. script_pushint(st, i < sd->achievement_data.count ? true : false);
  21964. return SCRIPT_CMD_SUCCESS;
  21965. }
  21966. /**
  21967. * Updates an achievement's value.
  21968. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  21969. */
  21970. BUILDIN_FUNC(achievementupdate) {
  21971. map_session_data *sd;
  21972. int32 i, achievement_id, type, value;
  21973. achievement_id = script_getnum(st, 2);
  21974. type = script_getnum(st, 3);
  21975. value = script_getnum(st, 4);
  21976. if (!script_charid2sd(5, sd)) {
  21977. script_pushint(st, false);
  21978. return SCRIPT_CMD_FAILURE;
  21979. }
  21980. if (achievement_db.exists(achievement_id) == false) {
  21981. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  21982. script_pushint(st, false);
  21983. return SCRIPT_CMD_FAILURE;
  21984. }
  21985. if( !sd->state.pc_loaded ){
  21986. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21987. return SCRIPT_CMD_SUCCESS;
  21988. }
  21989. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21990. if (i == sd->achievement_data.count)
  21991. achievement_add(sd, achievement_id);
  21992. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21993. if (i == sd->achievement_data.count) {
  21994. script_pushint(st, false);
  21995. return SCRIPT_CMD_SUCCESS;
  21996. }
  21997. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  21998. sd->achievement_data.achievements[i].count[type - 1] = value;
  21999. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  22000. sd->achievement_data.achievements[i].completed = value;
  22001. else if (type == ACHIEVEINFO_GOTREWARD)
  22002. sd->achievement_data.achievements[i].rewarded = value;
  22003. else {
  22004. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  22005. script_pushint(st, false);
  22006. return SCRIPT_CMD_FAILURE;
  22007. }
  22008. achievement_update_achievement(sd, achievement_id, false);
  22009. script_pushint(st, true);
  22010. return SCRIPT_CMD_SUCCESS;
  22011. }
  22012. /**
  22013. * Get an equipment's refine cost
  22014. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  22015. * returns -1 on fail
  22016. */
  22017. BUILDIN_FUNC(getequiprefinecost) {
  22018. int32 i = -1, slot, type, info;
  22019. map_session_data *sd;
  22020. slot = script_getnum(st, 2);
  22021. type = script_getnum(st, 3);
  22022. info = script_getnum(st, 4);
  22023. if (!script_charid2sd(5, sd)) {
  22024. script_pushint(st, 0);
  22025. return SCRIPT_CMD_FAILURE;
  22026. }
  22027. if (type < 0 || type >= REFINE_COST_MAX) {
  22028. script_pushint(st, -1);
  22029. return SCRIPT_CMD_SUCCESS;
  22030. }
  22031. if (equip_index_check(slot))
  22032. i = pc_checkequip(sd, equip_bitmask[slot]);
  22033. if (i < 0) {
  22034. script_pushint(st, -1);
  22035. return SCRIPT_CMD_SUCCESS;
  22036. }
  22037. if( sd->inventory_data[i] == nullptr ){
  22038. script_pushint( st, -1 );
  22039. return SCRIPT_CMD_SUCCESS;
  22040. }
  22041. std::shared_ptr<s_refine_level_info> level_info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  22042. if( level_info == nullptr ){
  22043. script_pushint( st, -1 );
  22044. return SCRIPT_CMD_SUCCESS;
  22045. }
  22046. std::shared_ptr<s_refine_cost> cost = util::umap_find( level_info->costs, (uint16)type );
  22047. if( cost == nullptr ){
  22048. script_pushint( st, -1 );
  22049. return SCRIPT_CMD_SUCCESS;
  22050. }
  22051. switch( info ){
  22052. case REFINE_MATERIAL_ID:
  22053. script_pushint( st, cost->nameid );
  22054. break;
  22055. case REFINE_ZENY_COST:
  22056. script_pushint( st, cost->zeny );
  22057. break;
  22058. default:
  22059. script_pushint( st, -1 );
  22060. return SCRIPT_CMD_FAILURE;
  22061. }
  22062. return SCRIPT_CMD_SUCCESS;
  22063. }
  22064. /**
  22065. * Round, floor, ceiling a number to arbitrary integer precision.
  22066. * round(<number>,<precision>);
  22067. * ceil(<number>,<precision>);
  22068. * floor(<number>,<precision>);
  22069. */
  22070. BUILDIN_FUNC(round) {
  22071. int32 num = script_getnum(st, 2);
  22072. int32 precision = script_getnum(st, 3);
  22073. char* func = script_getfuncname(st);
  22074. if (precision <= 0) {
  22075. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  22076. return SCRIPT_CMD_FAILURE;
  22077. }
  22078. if (strcasecmp(func, "floor") == 0) {
  22079. script_pushint(st, num - (num % precision));
  22080. }
  22081. else if (strcasecmp(func, "ceil") == 0) {
  22082. script_pushint(st, num + precision - (num % precision));
  22083. }
  22084. else {
  22085. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  22086. }
  22087. return SCRIPT_CMD_SUCCESS;
  22088. }
  22089. BUILDIN_FUNC(getequiptradability) {
  22090. int32 i, num;
  22091. TBL_PC *sd;
  22092. num = script_getnum(st, 2);
  22093. if (!script_charid2sd(3, sd)) {
  22094. return SCRIPT_CMD_FAILURE;
  22095. }
  22096. if (equip_index_check(num))
  22097. i = pc_checkequip(sd, equip_bitmask[num]);
  22098. else{
  22099. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  22100. return SCRIPT_CMD_FAILURE;
  22101. }
  22102. if (i >= 0)
  22103. script_pushint(st, pc_can_trade_item(sd, i));
  22104. else
  22105. script_pushint(st, false);
  22106. return SCRIPT_CMD_SUCCESS;
  22107. }
  22108. static inline bool mail_sub( struct script_state *st, struct script_data *data, map_session_data *sd, int32 i, const char **out_name, uint32 *start, uint32 *end, int32 *id ){
  22109. const char *name;
  22110. // Check if it is a variable
  22111. if( !data_isreference(data) ){
  22112. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  22113. return false;
  22114. }
  22115. name = reference_getname(data);
  22116. if( is_string_variable(name) ){
  22117. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  22118. return false;
  22119. }
  22120. // Check if the variable requires a player
  22121. if( not_server_variable(*name) && sd == nullptr ){
  22122. // If no player is attached
  22123. if( !script_rid2sd(sd) ){
  22124. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  22125. return false;
  22126. }
  22127. }
  22128. // Try to find the array's source pointer
  22129. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  22130. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  22131. return false;
  22132. }
  22133. // Store the name for later usage
  22134. *out_name = name;
  22135. // Get the start and end indices of the array
  22136. *start = reference_getindex(data);
  22137. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  22138. // For getting the values we need the id of the array
  22139. *id = reference_getid(data);
  22140. return true;
  22141. }
  22142. // mail <destination id>,"<sender name>","<title>","<body>"{,<zeny>{,<item id array>,<item amount array>{,refine{,bound{,<item card0 array>{,<item card1 array>{,<item card2 array>{,<item card3 array>{,<random option id0 array>, <random option value0 array>, <random option paramter0 array>{,<random option id1 array>, <random option value1 array>, <random option paramter1 array>{,<random option id2 array>, <random option value2 array>, <random option paramter2 array>{,<random option id3 array>, <random option value3 array>, <random option paramter3 array>{,<random option id4 array>, <random option value4 array>, <random option paramter4 array>}}}}}}}}};
  22143. BUILDIN_FUNC(mail){
  22144. const char *sender, *title, *body, *name;
  22145. struct mail_message msg;
  22146. struct script_data *data;
  22147. map_session_data *sd = nullptr;
  22148. uint32 i, j, k, num_items, start, end;
  22149. int32 id;
  22150. memset(&msg, 0, sizeof(struct mail_message));
  22151. msg.dest_id = script_getnum(st,2);
  22152. sender = script_getstr(st, 3);
  22153. if( strlen(sender) > NAME_LENGTH ){
  22154. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  22155. return SCRIPT_CMD_FAILURE;
  22156. }
  22157. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  22158. title = script_getstr(st, 4);
  22159. if( strlen(title) > MAIL_TITLE_LENGTH ){
  22160. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  22161. return SCRIPT_CMD_FAILURE;
  22162. }
  22163. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  22164. body = script_getstr(st, 5);
  22165. if( strlen(body) > MAIL_BODY_LENGTH ){
  22166. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  22167. return SCRIPT_CMD_FAILURE;
  22168. }
  22169. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  22170. if( script_hasdata(st,6) ){
  22171. int32 zeny = script_getnum(st, 6);
  22172. if( zeny < 0 ){
  22173. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  22174. return SCRIPT_CMD_FAILURE;
  22175. }else if( zeny > MAX_ZENY ){
  22176. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  22177. zeny = MAX_ZENY;
  22178. }
  22179. msg.zeny = zeny;
  22180. }
  22181. // Items
  22182. num_items = 0;
  22183. while( script_hasdata(st,7) ){
  22184. data = script_getdata(st,7);
  22185. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  22186. return SCRIPT_CMD_FAILURE;
  22187. }
  22188. num_items = end - start;
  22189. if( num_items == 0 ){
  22190. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  22191. break;
  22192. }
  22193. if( num_items > MAIL_MAX_ITEM ){
  22194. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  22195. num_items = MAIL_MAX_ITEM;
  22196. }
  22197. for( i = 0; i < num_items && start < end; i++, start++ ){
  22198. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22199. msg.item[i].identify = 1;
  22200. if( !item_db.exists(msg.item[i].nameid) ){
  22201. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  22202. return SCRIPT_CMD_FAILURE;
  22203. }
  22204. }
  22205. // Amounts
  22206. if( !script_hasdata(st,8) ){
  22207. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  22208. return SCRIPT_CMD_FAILURE;
  22209. }
  22210. data = script_getdata(st,8);
  22211. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  22212. return SCRIPT_CMD_FAILURE;
  22213. }
  22214. for( i = 0; i < num_items && start < end; i++, start++ ){
  22215. std::shared_ptr<item_data> itm = item_db.find(msg.item[i].nameid);
  22216. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22217. if( msg.item[i].amount <= 0 ){
  22218. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  22219. return SCRIPT_CMD_FAILURE;
  22220. }else if( itemdb_isstackable2(itm.get()) ){
  22221. uint16 max = itm->stack.amount > 0 ? itm->stack.amount : MAX_AMOUNT;
  22222. if( msg.item[i].amount > max ){
  22223. ShowWarning( "buildin_mail: amount %d for item %u is exceeding the maximum of %d. Capping...\n", msg.item[i].amount, msg.item[i].nameid, max );
  22224. msg.item[i].amount = max;
  22225. }
  22226. }else{
  22227. if( msg.item[i].amount > 1 ){
  22228. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  22229. msg.item[i].amount = 1;
  22230. }
  22231. }
  22232. }
  22233. // Refine
  22234. if (!script_hasdata(st, 9)) {
  22235. break;
  22236. }
  22237. data = script_getdata(st, 9);
  22238. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  22239. return SCRIPT_CMD_FAILURE;
  22240. }
  22241. for (i = 0; i < num_items && start < end; i++, start++) {
  22242. std::shared_ptr<item_data> itm = item_db.find(msg.item[i].nameid);
  22243. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22244. if (!itm->flag.no_refine && (itm->type == IT_WEAPON || itm->type == IT_ARMOR || itm->type == IT_SHADOWGEAR)) {
  22245. if (msg.item[i].refine > MAX_REFINE)
  22246. msg.item[i].refine = MAX_REFINE;
  22247. }
  22248. else
  22249. msg.item[i].refine = 0;
  22250. if (msg.item[i].refine < 0)
  22251. msg.item[i].refine = 0;
  22252. }
  22253. // Bound
  22254. if (!script_hasdata(st, 10)) {
  22255. break;
  22256. }
  22257. data = script_getdata(st,10);
  22258. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  22259. return SCRIPT_CMD_FAILURE;
  22260. }
  22261. for( i = 0; i < num_items && start < end; i++, start++ ){
  22262. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22263. if( msg.item[i].bound < BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  22264. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  22265. return SCRIPT_CMD_FAILURE;
  22266. }
  22267. }
  22268. // Cards
  22269. if( !script_hasdata(st,11) ){
  22270. break;
  22271. }
  22272. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  22273. data = script_getdata(st,j);
  22274. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22275. return SCRIPT_CMD_FAILURE;
  22276. }
  22277. for( k = 0; k < num_items && start < end; k++, start++ ){
  22278. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22279. if( msg.item[k].card[i] == 0 ){
  22280. // Continue with the next card, no further checks needed
  22281. continue;
  22282. }
  22283. if( itemdb_isspecial( msg.item[k].card[0] ) ){
  22284. // Continue with the next card, but do not check it against the item database
  22285. continue;
  22286. }
  22287. if( !item_db.exists( msg.item[k].card[i] ) ){
  22288. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  22289. return SCRIPT_CMD_FAILURE;
  22290. }
  22291. }
  22292. }
  22293. // Random Options
  22294. if( !script_hasdata(st,11 + MAX_SLOTS) ){
  22295. break;
  22296. }
  22297. for( i = 0, j = 11 + MAX_SLOTS; i < MAX_ITEM_RDM_OPT && script_hasdata(st,j) && script_hasdata(st,j + 1) && script_hasdata(st,j + 2); i++, j++ ){
  22298. // Option IDs
  22299. data = script_getdata(st, j);
  22300. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22301. return SCRIPT_CMD_FAILURE;
  22302. }
  22303. for( k = 0; k < num_items && start < end; k++, start++ ){
  22304. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22305. }
  22306. j++;
  22307. // Option values
  22308. data = script_getdata(st, j);
  22309. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22310. return SCRIPT_CMD_FAILURE;
  22311. }
  22312. for( k = 0; k < num_items && start < end; k++, start++ ){
  22313. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22314. }
  22315. j++;
  22316. // Option parameters
  22317. data = script_getdata(st, j);
  22318. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22319. return SCRIPT_CMD_FAILURE;
  22320. }
  22321. for( k = 0; k < num_items && start < end; k++, start++ ){
  22322. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22323. }
  22324. }
  22325. // Break the pseudo scope
  22326. break;
  22327. }
  22328. msg.status = MAIL_NEW;
  22329. msg.type = MAIL_INBOX_NORMAL;
  22330. msg.timestamp = time(nullptr);
  22331. intif_Mail_send(0, &msg);
  22332. return SCRIPT_CMD_SUCCESS;
  22333. }
  22334. BUILDIN_FUNC(open_roulette){
  22335. #if PACKETVER < 20141022
  22336. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  22337. return SCRIPT_CMD_FAILURE;
  22338. #else
  22339. map_session_data* sd;
  22340. if( !battle_config.feature_roulette ){
  22341. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  22342. return SCRIPT_CMD_FAILURE;
  22343. }
  22344. if( !script_charid2sd( 2, sd ) ){
  22345. return SCRIPT_CMD_FAILURE;
  22346. }
  22347. clif_roulette_open(sd);
  22348. return SCRIPT_CMD_SUCCESS;
  22349. #endif
  22350. }
  22351. /*==========================================
  22352. * identifyall({<type>{,<account_id>}})
  22353. * <type>:
  22354. * true: identify the items and returns the count of unidentified items (default)
  22355. * false: returns the count of unidentified items only
  22356. *------------------------------------------*/
  22357. BUILDIN_FUNC(identifyall) {
  22358. TBL_PC *sd;
  22359. bool identify_item = true;
  22360. if (script_hasdata(st, 2))
  22361. identify_item = script_getnum(st, 2) != 0;
  22362. if( !script_hasdata(st, 3) )
  22363. script_rid2sd(sd);
  22364. else
  22365. sd = map_id2sd( script_getnum(st, 3) );
  22366. if (sd == nullptr) {
  22367. script_pushint(st, -1);
  22368. return SCRIPT_CMD_SUCCESS;
  22369. }
  22370. script_pushint(st, pc_identifyall(sd, identify_item));
  22371. return SCRIPT_CMD_SUCCESS;
  22372. }
  22373. BUILDIN_FUNC(is_guild_leader)
  22374. {
  22375. map_session_data* sd;
  22376. int32 guild_id;
  22377. if (!script_rid2sd(sd)) {
  22378. script_pushint(st, false);
  22379. return SCRIPT_CMD_FAILURE;
  22380. }
  22381. if (script_hasdata(st, 2))
  22382. guild_id = script_getnum(st, 2);
  22383. else
  22384. guild_id = sd->status.guild_id;
  22385. auto guild_data = guild_search(guild_id);
  22386. if (guild_data)
  22387. script_pushint(st, (guild_data->guild.member[0].char_id == sd->status.char_id));
  22388. else
  22389. script_pushint(st, false);
  22390. return SCRIPT_CMD_SUCCESS;
  22391. }
  22392. BUILDIN_FUNC(is_party_leader)
  22393. {
  22394. map_session_data* sd;
  22395. struct party_data* p_data;
  22396. int32 p_id, i = 0;
  22397. if (!script_rid2sd(sd)) {
  22398. script_pushint(st, false);
  22399. return SCRIPT_CMD_FAILURE;
  22400. }
  22401. if (script_hasdata(st, 2))
  22402. p_id = script_getnum(st, 2);
  22403. else
  22404. p_id = sd->status.party_id;
  22405. p_data = party_search(p_id);
  22406. if (p_data) {
  22407. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  22408. if (i < MAX_PARTY){
  22409. script_pushint(st, p_data->party.member[i].leader);
  22410. return SCRIPT_CMD_SUCCESS;
  22411. }
  22412. }
  22413. script_pushint(st, false);
  22414. return SCRIPT_CMD_SUCCESS;
  22415. }
  22416. BUILDIN_FUNC( camerainfo ){
  22417. #if PACKETVER < 20160525
  22418. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  22419. return SCRIPT_CMD_FAILURE;
  22420. #else
  22421. map_session_data* sd;
  22422. if( !script_charid2sd( 5, sd ) ){
  22423. return SCRIPT_CMD_FAILURE;
  22424. }
  22425. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  22426. return SCRIPT_CMD_SUCCESS;
  22427. #endif
  22428. }
  22429. // This function is only meant to be used inside of achievement conditions
  22430. BUILDIN_FUNC(achievement_condition){
  22431. // Push what we get from the script
  22432. script_pushint( st, 2 );
  22433. // Otherwise the script is freed afterwards
  22434. st->state = RERUNLINE;
  22435. return SCRIPT_CMD_SUCCESS;
  22436. }
  22437. /// Returns a reference to a variable of the specific instance ID.
  22438. /// Returns 0 if an error occurs.
  22439. ///
  22440. /// getinstancevar(<variable>, <instance ID>) -> <reference>
  22441. BUILDIN_FUNC(getinstancevar)
  22442. {
  22443. struct script_data* data = script_getdata(st, 2);
  22444. if (!data_isreference(data)) {
  22445. ShowError("buildin_getinstancevar: %s is not a variable.\n", script_getstr(st, 2));
  22446. script_reportdata(data);
  22447. script_pushnil(st);
  22448. st->state = END;
  22449. return SCRIPT_CMD_FAILURE;
  22450. }
  22451. const char* name = reference_getname(data);
  22452. if (*name != '\'') {
  22453. ShowError("buildin_getinstancevar: Invalid scope. %s is not an instance variable.\n", name);
  22454. script_reportdata(data);
  22455. script_pushnil(st);
  22456. st->state = END;
  22457. return SCRIPT_CMD_FAILURE;
  22458. }
  22459. int32 instance_id = script_getnum(st, 3);
  22460. if (instance_id <= 0) {
  22461. ShowError("buildin_getinstancevar: Invalid instance ID %d.\n", instance_id);
  22462. script_pushnil(st);
  22463. st->state = END;
  22464. return SCRIPT_CMD_FAILURE;
  22465. }
  22466. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  22467. if (im->state != INSTANCE_BUSY) {
  22468. ShowError("buildin_getinstancevar: Unknown instance ID %d.\n", instance_id);
  22469. script_pushnil(st);
  22470. st->state = END;
  22471. return SCRIPT_CMD_FAILURE;
  22472. }
  22473. if (!im->regs.vars)
  22474. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  22475. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  22476. return SCRIPT_CMD_SUCCESS;
  22477. }
  22478. /// Sets the value of an instance variable.
  22479. ///
  22480. /// setinstancevar(<variable>,<value>,<instance ID>)
  22481. BUILDIN_FUNC(setinstancevar)
  22482. {
  22483. const char *command = script_getfuncname(st);
  22484. struct script_data* data = script_getdata(st, 2);
  22485. if (!data_isreference(data)) {
  22486. ShowError("buildin_%s: %s is not a variable.\n", command, script_getstr(st, 2));
  22487. script_reportdata(data);
  22488. script_pushnil(st);
  22489. st->state = END;
  22490. return SCRIPT_CMD_FAILURE;
  22491. }
  22492. const char* name = reference_getname(data);
  22493. if (*name != '\'') {
  22494. ShowError("buildin_%s: Invalid scope. %s is not an instance variable.\n", command, name);
  22495. script_reportdata(data);
  22496. script_pushnil(st);
  22497. st->state = END;
  22498. return SCRIPT_CMD_FAILURE;
  22499. }
  22500. int32 instance_id = script_getnum(st, 4);
  22501. if (instance_id <= 0) {
  22502. ShowError("buildin_%s: Invalid instance ID %d.\n", command, instance_id);
  22503. script_pushnil(st);
  22504. st->state = END;
  22505. return SCRIPT_CMD_FAILURE;
  22506. }
  22507. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  22508. if (im->state != INSTANCE_BUSY) {
  22509. ShowError("buildin_%s: Unknown instance ID %d.\n", command, instance_id);
  22510. script_pushnil(st);
  22511. st->state = END;
  22512. return SCRIPT_CMD_FAILURE;
  22513. }
  22514. script_pushcopy(st, 2);
  22515. map_session_data* sd = nullptr;
  22516. if( is_string_variable(name) )
  22517. set_reg_str( st, sd, reference_getuid(data), name, script_getstr( st, 3 ), &im->regs );
  22518. else
  22519. set_reg_num( st, sd, reference_getuid(data), name, script_getnum64( st, 3 ), &im->regs );
  22520. return SCRIPT_CMD_SUCCESS;
  22521. }
  22522. /*
  22523. convertpcinfo(<char_id>,<type>)
  22524. convertpcinfo(<account_id>,<type>)
  22525. convertpcinfo(<player_name>,<type>)
  22526. */
  22527. BUILDIN_FUNC(convertpcinfo) {
  22528. TBL_PC *sd;
  22529. if (script_isstring(st, 2))
  22530. sd = map_nick2sd(script_getstr(st, 2),false);
  22531. else {
  22532. int32 id = script_getnum(st, 2);
  22533. sd = map_id2sd(id);
  22534. if (!sd)
  22535. sd = map_charid2sd(id);
  22536. }
  22537. int32 type = script_getnum(st, 3);
  22538. switch (type) {
  22539. case CPC_NAME:
  22540. case CPC_CHAR:
  22541. case CPC_ACCOUNT:
  22542. break;
  22543. default:
  22544. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  22545. script_pushnil(st);
  22546. st->state = END;
  22547. return SCRIPT_CMD_FAILURE;
  22548. }
  22549. if (!sd) {
  22550. if (type == CPC_NAME)
  22551. script_pushstrcopy(st, "");
  22552. else
  22553. script_pushint(st, 0);
  22554. return SCRIPT_CMD_SUCCESS;
  22555. }
  22556. switch (type) {
  22557. case CPC_NAME:
  22558. script_pushstrcopy(st, sd->status.name);
  22559. break;
  22560. case CPC_CHAR:
  22561. script_pushint(st, sd->status.char_id);
  22562. break;
  22563. case CPC_ACCOUNT:
  22564. script_pushint(st, sd->status.account_id);
  22565. break;
  22566. }
  22567. return SCRIPT_CMD_SUCCESS;
  22568. }
  22569. BUILDIN_FUNC(isnpccloaked)
  22570. {
  22571. npc_data *nd;
  22572. if (script_hasdata(st, 2))
  22573. nd = npc_name2id(script_getstr(st, 2));
  22574. else
  22575. nd = map_id2nd(st->oid);
  22576. if (!nd) {
  22577. if (script_hasdata(st, 2))
  22578. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  22579. else
  22580. ShowError("buildin_isnpccloaked: non-existing NPC.\n");
  22581. return SCRIPT_CMD_FAILURE;
  22582. }
  22583. map_session_data* sd;
  22584. if (!script_charid2sd(3, sd))
  22585. return SCRIPT_CMD_FAILURE;
  22586. script_pushint(st, npc_is_cloaked(nd, sd));
  22587. return SCRIPT_CMD_SUCCESS;
  22588. }
  22589. BUILDIN_FUNC(refineui){
  22590. #if PACKETVER < 20161012
  22591. ShowError( "buildin_refineui: This command requires packet version 2016-10-12 or newer.\n" );
  22592. return SCRIPT_CMD_FAILURE;
  22593. #else
  22594. map_session_data* sd;
  22595. if( !script_charid2sd(2,sd) ){
  22596. return SCRIPT_CMD_FAILURE;
  22597. }
  22598. if( !battle_config.feature_refineui ){
  22599. ShowError( "buildin_refineui: This command is disabled via configuration.\n" );
  22600. return SCRIPT_CMD_FAILURE;
  22601. }
  22602. if( !sd->state.refineui_open ){
  22603. clif_refineui_open(sd);
  22604. }
  22605. return SCRIPT_CMD_SUCCESS;
  22606. #endif
  22607. }
  22608. BUILDIN_FUNC(getenchantgrade){
  22609. map_session_data *sd;
  22610. if (!script_charid2sd(3, sd)) {
  22611. script_pushint(st,-1);
  22612. return SCRIPT_CMD_FAILURE;
  22613. }
  22614. int32 index, position;
  22615. if (script_hasdata(st, 2))
  22616. position = script_getnum(st, 2);
  22617. else
  22618. position = EQI_COMPOUND_ON;
  22619. if (position == EQI_COMPOUND_ON)
  22620. index = current_equip_item_index;
  22621. else if (equip_index_check(position))
  22622. index = pc_checkequip(sd, equip_bitmask[position]);
  22623. else {
  22624. ShowError( "buildin_getenchantgrade: Unknown equip index '%d'\n", position );
  22625. script_pushint(st,-1);
  22626. return SCRIPT_CMD_FAILURE;
  22627. }
  22628. if (index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0)
  22629. script_pushint(st, -1);
  22630. else
  22631. script_pushint(st, sd->inventory.u.items_inventory[index].enchantgrade);
  22632. return SCRIPT_CMD_SUCCESS;
  22633. }
  22634. BUILDIN_FUNC(naviregisterwarp) {
  22635. #ifdef MAP_GENERATOR
  22636. TBL_NPC* nd;
  22637. int32 x, y, m;
  22638. const char *warpname, *mapname=nullptr;
  22639. struct navi_link link;
  22640. nd = map_id2nd(st->oid);
  22641. if (!nd) {
  22642. return SCRIPT_CMD_SUCCESS;
  22643. }
  22644. warpname = script_getstr(st, 2);
  22645. mapname = script_getstr(st, 3);
  22646. x = script_getnum(st,4);
  22647. y = script_getnum(st,5);
  22648. m = map_mapname2mapid(mapname);
  22649. link.npc = nd;
  22650. link.id = 0;
  22651. link.pos = nd->navi.pos;
  22652. link.warp_dest = {m, x, y};
  22653. link.name = warpname;
  22654. link.hidden = nd->navi.hidden;
  22655. nd->links.push_back(link);
  22656. #endif
  22657. return SCRIPT_CMD_SUCCESS;
  22658. }
  22659. /*==========================================
  22660. * mob_setidleevent( <monster game ID>, "<event label>" )
  22661. *------------------------------------------*/
  22662. BUILDIN_FUNC(mob_setidleevent){
  22663. struct block_list* bl;
  22664. if( !script_rid2bl( 2, bl ) ){
  22665. return SCRIPT_CMD_FAILURE;
  22666. }
  22667. if( bl->type != BL_MOB ){
  22668. ShowError( "buildin_mob_setidleevent: the target GID was not a monster.\n" );
  22669. return SCRIPT_CMD_FAILURE;
  22670. }
  22671. struct mob_data* md = (struct mob_data*)bl;
  22672. if (md == nullptr)
  22673. return SCRIPT_CMD_FAILURE;
  22674. const char* idle_event = script_getstr( st, 3 );
  22675. check_event( st, idle_event );
  22676. safestrncpy( md->idle_event, idle_event, EVENT_NAME_LENGTH );
  22677. return SCRIPT_CMD_SUCCESS;
  22678. }
  22679. BUILDIN_FUNC( openstylist ){
  22680. #if PACKETVER >= 20151104
  22681. if( !battle_config.feature_stylist ){
  22682. ShowError( "buildin_openstylist: stylist is disabled.\n" );
  22683. return SCRIPT_CMD_FAILURE;
  22684. }
  22685. map_session_data* sd;
  22686. if( !script_charid2sd( 2, sd ) ){
  22687. return SCRIPT_CMD_FAILURE;
  22688. }
  22689. clif_ui_open( *sd, OUT_UI_STYLIST, 0 );
  22690. return SCRIPT_CMD_SUCCESS;
  22691. #else
  22692. ShowError( "buildin_openstylist: This command requires packet version 2015-11-04 or newer.\n" );
  22693. return SCRIPT_CMD_FAILURE;
  22694. #endif
  22695. }
  22696. BUILDIN_FUNC(navihide) {
  22697. #ifdef MAP_GENERATOR
  22698. TBL_NPC *nd;
  22699. nd = map_id2nd(st->oid);
  22700. if (!nd) {
  22701. return SCRIPT_CMD_SUCCESS;
  22702. }
  22703. nd->navi.hidden = true;
  22704. for (auto &link : nd->links) {
  22705. link.hidden = true;
  22706. }
  22707. #endif
  22708. return SCRIPT_CMD_SUCCESS;
  22709. }
  22710. BUILDIN_FUNC(getitempos) {
  22711. map_session_data* sd;
  22712. if ( !script_rid2sd(sd) ){
  22713. script_pushint(st, 0);
  22714. return SCRIPT_CMD_FAILURE;
  22715. }
  22716. if( current_equip_item_index == -1 ){
  22717. ShowError( "buildin_getitempos: Invalid usage detected. This command should only be used inside item scripts.\n" );
  22718. script_pushint(st, 0);
  22719. return SCRIPT_CMD_FAILURE;
  22720. }
  22721. script_pushint(st, sd->inventory.u.items_inventory[current_equip_item_index].equip);
  22722. return SCRIPT_CMD_SUCCESS;
  22723. }
  22724. BUILDIN_FUNC( laphine_synthesis ){
  22725. map_session_data* sd;
  22726. if( !script_rid2sd( sd ) ){
  22727. return SCRIPT_CMD_FAILURE;
  22728. }
  22729. t_itemid item_id;
  22730. if( script_hasdata( st, 2 ) ){
  22731. if( script_isstring( st, 2 ) ){
  22732. const char* item_name = script_getstr( st, 2 );
  22733. std::shared_ptr<item_data> item = item_db.searchname( item_name );
  22734. if( item == nullptr ){
  22735. ShowError("buildin_laphine_synthesis: Item \"%s\" does not exist.\n", item_name );
  22736. return SCRIPT_CMD_FAILURE;
  22737. }
  22738. item_id = item->nameid;
  22739. }else{
  22740. item_id = script_getnum( st, 2 );
  22741. if( !item_db.exists( item_id ) ){
  22742. ShowError( "buildin_laphine_synthesis: Item ID %u does not exist.\n", item_id );
  22743. return SCRIPT_CMD_FAILURE;
  22744. }
  22745. }
  22746. }else{
  22747. if( sd->itemid == 0 ){
  22748. ShowError( "buildin_laphine_synthesis: Called outside of an item script without item id.\n" );
  22749. return SCRIPT_CMD_FAILURE;
  22750. }
  22751. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22752. ShowError( "buildin_laphine_synthesis: Called from item %u, which is not a consumed delayed.\n", sd->itemid );
  22753. return SCRIPT_CMD_FAILURE;
  22754. }
  22755. item_id = sd->itemid;
  22756. }
  22757. if( sd->state.laphine_synthesis != 0 ){
  22758. ShowError( "buildin_laphine_synthesis: Laphine Synthesis window was already open. Player %s (AID: %u, CID: %u) with item id %u.\n", sd->status.name, sd->status.account_id, sd->status.char_id, sd->itemid );
  22759. return SCRIPT_CMD_FAILURE;
  22760. }
  22761. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( item_id );
  22762. if( synthesis == nullptr ){
  22763. ShowError( "buildin_laphine_synthesis: %u is not a valid Laphine Synthesis item.\n", item_id );
  22764. return SCRIPT_CMD_FAILURE;
  22765. }
  22766. clif_laphine_synthesis_open( sd, synthesis );
  22767. return SCRIPT_CMD_SUCCESS;
  22768. }
  22769. BUILDIN_FUNC( laphine_upgrade ){
  22770. map_session_data* sd;
  22771. if( !script_rid2sd( sd ) ){
  22772. return SCRIPT_CMD_FAILURE;
  22773. }
  22774. if( sd->itemid == 0 ){
  22775. ShowError( "buildin_laphine_upgrade: Called outside of an item script without item id.\n" );
  22776. return SCRIPT_CMD_FAILURE;
  22777. }
  22778. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22779. ShowError( "buildin_laphine_upgrade: Called from item %u, which is not a consumed delayed.\n", sd->itemid );
  22780. return SCRIPT_CMD_FAILURE;
  22781. }
  22782. if( sd->state.laphine_upgrade != 0 ){
  22783. ShowError( "buildin_laphine_upgrade: Laphine Upgrade window was already open. Player %s (AID: %u, CID: %u) with item id %u.\n", sd->status.name, sd->status.account_id, sd->status.char_id, sd->itemid );
  22784. return SCRIPT_CMD_FAILURE;
  22785. }
  22786. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->itemid );
  22787. if( upgrade == nullptr ){
  22788. ShowError( "buildin_laphine_upgrade: %u is not a valid Laphine Upgrade item.\n", sd->itemid );
  22789. return SCRIPT_CMD_FAILURE;
  22790. }
  22791. clif_laphine_upgrade_open( sd, upgrade );
  22792. return SCRIPT_CMD_SUCCESS;
  22793. }
  22794. BUILDIN_FUNC(randomoptgroup)
  22795. {
  22796. int32 id = script_getnum(st,2);
  22797. auto group = random_option_group.find(id);
  22798. if (group == nullptr) {
  22799. ShowError("buildin_randomoptgroup: Invalid random option group id (%d)!\n", id);
  22800. return SCRIPT_CMD_FAILURE;
  22801. }
  22802. struct item item_tmp = {};
  22803. group->apply( item_tmp );
  22804. for ( int32 i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  22805. setd_sub_num(st, nullptr, ".@opt_id", i, item_tmp.option[i].id, nullptr);
  22806. setd_sub_num(st, nullptr, ".@opt_value", i, item_tmp.option[i].value, nullptr);
  22807. setd_sub_num(st, nullptr, ".@opt_param", i, item_tmp.option[i].param, nullptr);
  22808. }
  22809. return SCRIPT_CMD_SUCCESS;
  22810. }
  22811. BUILDIN_FUNC( open_quest_ui ){
  22812. #if PACKETVER < 20151202
  22813. ShowError( "buildin_open_quest_ui: This command requires PACKETVER 20151202 or newer.\n" );
  22814. return SCRIPT_CMD_FAILURE;
  22815. #else
  22816. map_session_data* sd;
  22817. if (!script_charid2sd(3, sd))
  22818. return SCRIPT_CMD_FAILURE;
  22819. int32 quest_id = script_hasdata(st, 2) ? script_getnum(st, 2) : 0;
  22820. if( quest_id != 0 ){
  22821. int32 i;
  22822. ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
  22823. if (i == sd->avail_quests)
  22824. ShowWarning("buildin_open_quest_ui: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
  22825. }
  22826. clif_ui_open( *sd, OUT_UI_QUEST, quest_id );
  22827. return SCRIPT_CMD_SUCCESS;
  22828. #endif
  22829. }
  22830. BUILDIN_FUNC(openbank){
  22831. #if PACKETVER < 20151202
  22832. ShowError( "buildin_openbank: This command requires PACKETVER 20151202 or newer.\n" );
  22833. return SCRIPT_CMD_FAILURE;
  22834. #else
  22835. map_session_data* sd = nullptr;
  22836. if (!script_charid2sd(2, sd)) {
  22837. return SCRIPT_CMD_FAILURE;
  22838. }
  22839. if( !battle_config.feature_banking ){
  22840. ShowError( "buildin_openbank: banking is disabled.\n" );
  22841. return SCRIPT_CMD_FAILURE;
  22842. }
  22843. clif_ui_open( *sd, OUT_UI_BANK, 0 );
  22844. return SCRIPT_CMD_SUCCESS;
  22845. #endif
  22846. }
  22847. BUILDIN_FUNC(getbaseexp_ratio){
  22848. map_session_data *sd;
  22849. if( !script_charid2sd( 4, sd ) ){
  22850. return SCRIPT_CMD_FAILURE;
  22851. }
  22852. int32 percent = script_getnum( st, 2 );
  22853. if( percent < 0 || percent > 100 ){
  22854. ShowError( "getbaseexp_ratio: percentage is out of range.\n" );
  22855. return SCRIPT_CMD_FAILURE;
  22856. }
  22857. int32 base_level;
  22858. if( script_hasdata( st, 3 ) ){
  22859. base_level = script_getnum( st, 3 );
  22860. if( base_level < 1 ){
  22861. ShowError( "getbaseexp_ratio: level cannot be below 1.\n" );
  22862. return SCRIPT_CMD_FAILURE;
  22863. }
  22864. uint32 max_level = job_db.get_maxBaseLv( sd->status.class_ );
  22865. if( base_level > max_level ){
  22866. ShowError( "getbaseexp_ratio: level is above maximum base level %u for job %s.\n", max_level, job_name( sd->status.class_ ) );
  22867. return SCRIPT_CMD_FAILURE;
  22868. }
  22869. }else{
  22870. base_level = sd->status.base_level;
  22871. }
  22872. t_exp class_exp = job_db.get_baseExp( sd->status.class_, base_level );
  22873. if( class_exp <= 0 ){
  22874. ShowError( "getbaseexp_ratio: No base experience defined for class %s at base level %d.\n", job_name( sd->status.class_ ), base_level );
  22875. return SCRIPT_CMD_FAILURE;
  22876. }
  22877. double result = ( class_exp * percent ) / 100.0;
  22878. script_pushint64( st, static_cast<t_exp>( result ) );
  22879. return SCRIPT_CMD_SUCCESS;
  22880. }
  22881. BUILDIN_FUNC(getjobexp_ratio){
  22882. map_session_data *sd;
  22883. if( !script_charid2sd( 4, sd ) ){
  22884. return SCRIPT_CMD_FAILURE;
  22885. }
  22886. int32 percent = script_getnum( st, 2 );
  22887. if( percent < 0 || percent > 100 ){
  22888. ShowError( "getjobexp_ratio: percentage is out of range.\n" );
  22889. return SCRIPT_CMD_FAILURE;
  22890. }
  22891. int32 job_level;
  22892. if( script_hasdata( st, 3 ) ){
  22893. job_level = script_getnum( st, 3 );
  22894. if( job_level < 1 ){
  22895. ShowError( "getjobexp_ratio: level cannot be below 1.\n" );
  22896. return SCRIPT_CMD_FAILURE;
  22897. }
  22898. uint32 max_level = job_db.get_maxJobLv( sd->status.class_ );
  22899. if( job_level > max_level ){
  22900. ShowError( "getjobexp_ratio: level is above maximum job level %u for job %s.\n", max_level, job_name( sd->status.class_ ) );
  22901. return SCRIPT_CMD_FAILURE;
  22902. }
  22903. }else{
  22904. job_level = sd->status.job_level;
  22905. }
  22906. t_exp class_exp = job_db.get_baseExp( sd->status.class_, job_level );
  22907. if( class_exp <= 0 ){
  22908. ShowError( "getjobexp_ratio: No job experience defined for class %s at job level %d.\n", job_name( sd->status.class_ ), job_level );
  22909. return SCRIPT_CMD_FAILURE;
  22910. }
  22911. double result = ( class_exp * percent ) / 100.0;
  22912. script_pushint64( st, static_cast<t_exp>( result ) );
  22913. return SCRIPT_CMD_SUCCESS;
  22914. }
  22915. BUILDIN_FUNC( enchantgradeui ){
  22916. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  22917. map_session_data* sd;
  22918. if( !script_charid2sd( 2, sd ) ){
  22919. return SCRIPT_CMD_FAILURE;
  22920. }
  22921. clif_ui_open( *sd, OUT_UI_ENCHANTGRADE, 0 );
  22922. return SCRIPT_CMD_SUCCESS;
  22923. #else
  22924. ShowError( "buildin_enchantgradeui: This command requires PACKETVER 2020-07-24 or newer.\n" );
  22925. return SCRIPT_CMD_FAILURE;
  22926. #endif
  22927. }
  22928. BUILDIN_FUNC(set_reputation_points){
  22929. map_session_data* sd;
  22930. if( !script_charid2sd( 4, sd ) ){
  22931. return SCRIPT_CMD_FAILURE;
  22932. }
  22933. int64 type = script_getnum64( st, 2 );
  22934. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22935. if( reputation == nullptr ){
  22936. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22937. return SCRIPT_CMD_FAILURE;
  22938. }
  22939. int64 points = script_getnum64( st, 3 );
  22940. points = cap_value( points, reputation->minimum, reputation->maximum );
  22941. if( !pc_setreg2( sd, reputation->variable.c_str(), points ) ){
  22942. return SCRIPT_CMD_FAILURE;
  22943. }
  22944. clif_reputation_type( *sd, type, points );
  22945. return SCRIPT_CMD_SUCCESS;
  22946. }
  22947. BUILDIN_FUNC(get_reputation_points){
  22948. map_session_data* sd;
  22949. if( !script_charid2sd( 3, sd ) ){
  22950. return SCRIPT_CMD_FAILURE;
  22951. }
  22952. int64 type = script_getnum64( st, 2 );
  22953. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22954. if( reputation == nullptr ){
  22955. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22956. return SCRIPT_CMD_FAILURE;
  22957. }
  22958. int64 points = pc_readreg2( sd, reputation->variable.c_str() );
  22959. points = cap_value( points, reputation->minimum, reputation->maximum );
  22960. script_pushint( st, points );
  22961. return SCRIPT_CMD_SUCCESS;
  22962. }
  22963. BUILDIN_FUNC(add_reputation_points)
  22964. {
  22965. map_session_data* sd;
  22966. if( !script_charid2sd( 4, sd ) ){
  22967. return SCRIPT_CMD_FAILURE;
  22968. }
  22969. int64 type = script_getnum64( st, 2 );
  22970. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22971. if( reputation == nullptr ){
  22972. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22973. return SCRIPT_CMD_FAILURE;
  22974. }
  22975. int64 points = pc_readreg2( sd, reputation->variable.c_str() ) + script_getnum64(st, 3);
  22976. points = cap_value( points, reputation->minimum, reputation->maximum );
  22977. if( !pc_setreg2( sd, reputation->variable.c_str(), points ) ){
  22978. return SCRIPT_CMD_FAILURE;
  22979. }
  22980. clif_reputation_type( *sd, type, points );
  22981. return SCRIPT_CMD_SUCCESS;
  22982. }
  22983. BUILDIN_FUNC(item_reform){
  22984. #if PACKETVER < 20211103
  22985. ShowError( "buildin_item_reform: This command requires packet version 2021-11-03 or newer.\n" );
  22986. return SCRIPT_CMD_FAILURE;
  22987. #else
  22988. map_session_data* sd;
  22989. if( !script_charid2sd( 3, sd ) ){
  22990. return SCRIPT_CMD_FAILURE;
  22991. }
  22992. t_itemid item_id;
  22993. if( script_hasdata( st, 2 ) ){
  22994. if( script_isstring( st, 2 ) ){
  22995. const char* item_name = script_getstr( st, 2 );
  22996. std::shared_ptr<item_data> item = item_db.searchname( item_name );
  22997. if( item == nullptr ){
  22998. ShowError("buildin_item_reform: Item \"%s\" does not exist.\n", item_name );
  22999. return SCRIPT_CMD_FAILURE;
  23000. }
  23001. item_id = item->nameid;
  23002. }else{
  23003. item_id = script_getnum( st, 2 );
  23004. if( !item_db.exists( item_id ) ){
  23005. ShowError( "buildin_item_reform: Item ID %u does not exist.\n", item_id );
  23006. return SCRIPT_CMD_FAILURE;
  23007. }
  23008. }
  23009. }else{
  23010. if( sd->itemid == 0 ){
  23011. ShowError( "buildin_item_reform: Called outside of an item script without item id.\n" );
  23012. return SCRIPT_CMD_FAILURE;
  23013. }
  23014. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  23015. ShowError( "buildin_item_reform: Called from item %u, which is not a DelayConsume type.\n", sd->itemid );
  23016. return SCRIPT_CMD_FAILURE;
  23017. }
  23018. item_id = sd->itemid;
  23019. }
  23020. if( !item_reform_db.exists( item_id ) ){
  23021. ShowError( "buildin_item_reform: Item ID %u is not in the Item Reform database.\n", item_id );
  23022. return SCRIPT_CMD_FAILURE;
  23023. }
  23024. clif_item_reform_open( *sd, item_id );
  23025. return SCRIPT_CMD_SUCCESS;
  23026. #endif
  23027. }
  23028. BUILDIN_FUNC(item_enchant){
  23029. #if !( PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330 )
  23030. ShowError( "buildin_item_enchant: This command requires packet version 2021-11-03 or newer.\n" );
  23031. return SCRIPT_CMD_FAILURE;
  23032. #else
  23033. map_session_data* sd;
  23034. if( !script_charid2sd( 3, sd ) ){
  23035. return SCRIPT_CMD_FAILURE;
  23036. }
  23037. uint64 clientLuaIndex = script_getnum64( st, 2 );
  23038. if( !item_enchant_db.exists( clientLuaIndex ) ){
  23039. ShowError( "buildin_item_enchant: %" PRIu64 " is not a valid item enchant index.\n", clientLuaIndex );
  23040. return SCRIPT_CMD_FAILURE;
  23041. }
  23042. clif_enchantwindow_open( *sd, clientLuaIndex );
  23043. return SCRIPT_CMD_SUCCESS;
  23044. #endif
  23045. }
  23046. /**
  23047. * Generate item link string for client
  23048. * itemlink(<item_id>,<refine>,<card0>,<card1>,<card2>,<card3>,<enchantgrade>{,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>});
  23049. * @author [Cydh]
  23050. **/
  23051. BUILDIN_FUNC(itemlink)
  23052. {
  23053. struct item item = {};
  23054. item.nameid = script_getnum(st, 2);
  23055. if( !item_db.exists( item.nameid ) ){
  23056. ShowError( "buildin_itemlink: Item ID %u does not exists.\n", item.nameid );
  23057. st->state = END;
  23058. return SCRIPT_CMD_FAILURE;
  23059. }
  23060. FETCH(3, item.refine);
  23061. FETCH(4, item.card[0]);
  23062. FETCH(5, item.card[1]);
  23063. FETCH(6, item.card[2]);
  23064. FETCH(7, item.card[3]);
  23065. FETCH(8, item.enchantgrade);
  23066. #if PACKETVER >= 20150225
  23067. if ( script_hasdata(st,9) && script_getitem_randomoption(st, nullptr, &item, "itemlink", 9) == false) {
  23068. st->state = END;
  23069. return SCRIPT_CMD_FAILURE;
  23070. }
  23071. #endif
  23072. std::string itemlstr = item_db.create_item_link(item);
  23073. script_pushstrcopy(st, itemlstr.c_str());
  23074. return SCRIPT_CMD_SUCCESS;
  23075. }
  23076. BUILDIN_FUNC(mesitemlink){
  23077. t_itemid nameid = script_getnum( st, 2 );
  23078. std::shared_ptr<item_data> data = item_db.find( nameid );
  23079. if( data == nullptr ){
  23080. ShowError( "buildin_mesitemlink: Item ID %u does not exists.\n", nameid );
  23081. script_pushconststr( st, "" );
  23082. return SCRIPT_CMD_FAILURE;
  23083. }
  23084. bool use_brackets = true;
  23085. if( script_hasdata( st, 3 ) ){
  23086. use_brackets = script_getnum( st, 3 ) != 0;
  23087. }
  23088. const char* name = nullptr;
  23089. if( script_hasdata( st, 4 ) ){
  23090. name = script_getstr( st, 4 );
  23091. }
  23092. // Create the link, depending on configuration and packet version
  23093. std::string itemlstr = item_db.create_item_link_for_mes( data, use_brackets, name );
  23094. // Push it to the script engine for further usage
  23095. script_pushstrcopy( st, itemlstr.c_str() );
  23096. return SCRIPT_CMD_SUCCESS;
  23097. }
  23098. BUILDIN_FUNC(addfame) {
  23099. map_session_data *sd;
  23100. if (!script_charid2sd(3, sd))
  23101. return SCRIPT_CMD_FAILURE;
  23102. if (!pc_addfame(*sd, script_getnum(st, 2)))
  23103. return SCRIPT_CMD_FAILURE;
  23104. return SCRIPT_CMD_SUCCESS;
  23105. }
  23106. BUILDIN_FUNC(getfame) {
  23107. map_session_data *sd;
  23108. if (!script_charid2sd(2, sd))
  23109. return SCRIPT_CMD_FAILURE;
  23110. script_pushint(st, sd->status.fame);
  23111. return SCRIPT_CMD_SUCCESS;
  23112. }
  23113. BUILDIN_FUNC(getfamerank) {
  23114. map_session_data *sd;
  23115. if (!script_charid2sd(2, sd))
  23116. return SCRIPT_CMD_FAILURE;
  23117. script_pushint(st, pc_famerank(sd->status.char_id, sd->class_ & MAPID_UPPERMASK));
  23118. return SCRIPT_CMD_SUCCESS;
  23119. }
  23120. BUILDIN_FUNC(isdead) {
  23121. map_session_data *sd;
  23122. if (!script_mapid2sd(2, sd))
  23123. return SCRIPT_CMD_FAILURE;
  23124. script_pushint(st, pc_isdead(sd));
  23125. return SCRIPT_CMD_SUCCESS;
  23126. }
  23127. BUILDIN_FUNC(macro_detector) {
  23128. map_session_data *sd;
  23129. if (script_hasdata(st, 2) && script_isstring(st, 2)) { // Character Name
  23130. if (!script_nick2sd(2, sd)) {
  23131. return SCRIPT_CMD_FAILURE;
  23132. }
  23133. } else { // Account ID
  23134. if (!script_accid2sd(2, sd)) {
  23135. return SCRIPT_CMD_FAILURE;
  23136. }
  23137. }
  23138. // Reporter Account ID as -1 for server.
  23139. pc_macro_reporter_process(*sd);
  23140. return SCRIPT_CMD_SUCCESS;
  23141. }
  23142. // ===================================
  23143. // *has_autoloot({<char_id>});
  23144. // This command checks whether a player configured autoloot.
  23145. // Returns current autoloot value on success.
  23146. // ===================================
  23147. BUILDIN_FUNC(has_autoloot) {
  23148. map_session_data *sd;
  23149. if (!script_charid2sd(2, sd)) {
  23150. script_pushint(st, 0);
  23151. return SCRIPT_CMD_FAILURE;
  23152. }
  23153. script_pushint(st, sd->state.autoloot);
  23154. return SCRIPT_CMD_SUCCESS;
  23155. }
  23156. // ===================================
  23157. // *autoloot({<rate>{, <char_id>}});
  23158. // This command sets the rate of autoloot.
  23159. // If no rate is provided and the user has autoloot disabled it will default to 10000 = 100% (enabled) or
  23160. // if the user has autoloot enabled it will default to 0 = 0% (disabled).
  23161. // Returns true on success and false on failure.
  23162. // ===================================
  23163. BUILDIN_FUNC(autoloot) {
  23164. map_session_data *sd = nullptr;
  23165. if (!script_charid2sd(3, sd)) {
  23166. script_pushint(st, false);
  23167. return SCRIPT_CMD_FAILURE;
  23168. }
  23169. int32 rate;
  23170. if (script_hasdata(st, 2)) {
  23171. rate = script_getnum(st, 2);
  23172. if (rate < 0 || rate > 10000) {
  23173. ShowWarning("buildin_autoloot: Invalid rate value %d, should be between 0 ~ 10000.\n", rate);
  23174. script_pushint(st, false);
  23175. return SCRIPT_CMD_FAILURE;
  23176. }
  23177. } else {
  23178. rate = (sd->state.autoloot > 0 ? 0 : 10000);
  23179. }
  23180. sd->state.autoloot = rate;
  23181. script_pushint(st, true);
  23182. return SCRIPT_CMD_SUCCESS;
  23183. }
  23184. BUILDIN_FUNC(opentips){
  23185. #if PACKETVER < 20171122
  23186. ShowError( "buildin_opentips: This command requires PACKETVER 20171122 or newer.\n" );
  23187. return SCRIPT_CMD_FAILURE;
  23188. #else
  23189. map_session_data* sd;
  23190. if (!script_charid2sd(3, sd)) {
  23191. return SCRIPT_CMD_FAILURE;
  23192. }
  23193. clif_ui_open( *sd, OUT_UI_TIP, script_getnum(st, 2) );
  23194. return SCRIPT_CMD_SUCCESS;
  23195. #endif
  23196. }
  23197. /**
  23198. * Displays a special popup.
  23199. * specialpopup(<popup id>);
  23200. */
  23201. BUILDIN_FUNC(specialpopup) {
  23202. map_session_data* sd;
  23203. if( !script_rid2sd(sd) )
  23204. return SCRIPT_CMD_FAILURE;
  23205. int32 id = script_getnum(st,2);
  23206. clif_specialpopup(*sd, id);
  23207. return SCRIPT_CMD_SUCCESS;
  23208. }
  23209. BUILDIN_FUNC(setdialogalign){
  23210. map_session_data *sd;
  23211. if ( !script_rid2sd(sd) ) {
  23212. return SCRIPT_CMD_FAILURE;
  23213. }
  23214. int32 align = script_getnum( st, 2 );
  23215. if( align < DIALOG_ALIGN_LEFT || align > DIALOG_ALIGN_BOTTOM ){
  23216. ShowError( "buildin_setdialogalign: Unknown align value %d\n", align );
  23217. return SCRIPT_CMD_FAILURE;
  23218. }
  23219. clif_set_dialog_align( *sd, st->oid, static_cast<e_say_dialog_align>( align ) );
  23220. return SCRIPT_CMD_SUCCESS;
  23221. }
  23222. BUILDIN_FUNC(setdialogsize){
  23223. map_session_data *sd;
  23224. if ( !script_rid2sd(sd) ) {
  23225. script_pushint(st, 0);
  23226. return SCRIPT_CMD_FAILURE;
  23227. }
  23228. int32 x = script_getnum( st, 2 );
  23229. if( x < 0 || x > INT16_MAX ){
  23230. ShowError( "buildin_setdialogsize: x size %d is out of range [0,%d]\n", x, INT16_MAX );
  23231. return SCRIPT_CMD_FAILURE;
  23232. }
  23233. int32 y = script_getnum( st, 3 );
  23234. if( y < 0 || y > INT16_MAX ){
  23235. ShowError( "buildin_setdialogsize: y size %d is out of range [0,%d]\n", y, INT16_MAX );
  23236. return SCRIPT_CMD_FAILURE;
  23237. }
  23238. clif_set_npc_window_size( *sd, x, y );
  23239. return SCRIPT_CMD_SUCCESS;
  23240. }
  23241. BUILDIN_FUNC(setdialogpos){
  23242. map_session_data *sd;
  23243. if ( !script_rid2sd(sd) ) {
  23244. script_pushint(st, 0);
  23245. return SCRIPT_CMD_FAILURE;
  23246. }
  23247. int32 x = script_getnum( st, 2 );
  23248. if( x < 0 || x > INT16_MAX ){
  23249. ShowError( "buildin_setdialogpos: x position %d is out of range [0,%d]\n", x, INT16_MAX );
  23250. return SCRIPT_CMD_FAILURE;
  23251. }
  23252. int32 y = script_getnum( st, 3 );
  23253. if( y < 0 || y > INT16_MAX ){
  23254. ShowError( "buildin_setdialogpos: y position %d is out of range [0,%d]\n", y, INT16_MAX );
  23255. return SCRIPT_CMD_FAILURE;
  23256. }
  23257. clif_set_npc_window_pos( *sd, x, y );
  23258. return SCRIPT_CMD_SUCCESS;
  23259. }
  23260. BUILDIN_FUNC(setdialogpospercent){
  23261. map_session_data *sd;
  23262. if ( !script_rid2sd(sd) ) {
  23263. script_pushint(st, 0);
  23264. return SCRIPT_CMD_FAILURE;
  23265. }
  23266. int32 x = script_getnum( st, 2 );
  23267. if( x < 0 || x > 100 ){
  23268. ShowError( "buildin_setdialogpospercent: x rate %d is out of range [0,100]\n", x );
  23269. return SCRIPT_CMD_FAILURE;
  23270. }
  23271. int32 y = script_getnum( st, 3 );
  23272. if( y < 0 || y > 100 ){
  23273. ShowError( "buildin_setdialogpospercent: y rate %d is out of range [0,100]\n", y );
  23274. return SCRIPT_CMD_FAILURE;
  23275. }
  23276. clif_set_npc_window_pos_percent( *sd, x, y );
  23277. return SCRIPT_CMD_SUCCESS;
  23278. }
  23279. /**
  23280. * Checks if the character has that permission.
  23281. * permission_check(<permission>{,<char_id>}) -> <bool>
  23282. */
  23283. BUILDIN_FUNC(permission_check)
  23284. {
  23285. map_session_data* sd = nullptr;
  23286. if (!script_charid2sd(3, sd))
  23287. return SCRIPT_CMD_FAILURE;
  23288. int32 permission = script_getnum(st, 2);
  23289. if (permission < PC_PERM_TRADE || permission >= PC_PERM_MAX) {
  23290. ShowError("buildin_permission_check: Invalid permission %d\n", permission);
  23291. return SCRIPT_CMD_FAILURE;
  23292. }
  23293. script_pushint(st, pc_has_permission(sd, static_cast<e_pc_permission>(permission)));
  23294. return SCRIPT_CMD_SUCCESS;
  23295. }
  23296. /**
  23297. * Adds or removes a permission from the character
  23298. * permission_add(<permission>{,<char_id>})
  23299. * permission_remove(<permission>{,<char_id>})
  23300. */
  23301. BUILDIN_FUNC(permission_add)
  23302. {
  23303. map_session_data* sd = nullptr;
  23304. if (!script_charid2sd(3, sd))
  23305. return SCRIPT_CMD_FAILURE;
  23306. int32 permission = script_getnum(st, 2);
  23307. if (permission < PC_PERM_TRADE || permission >= PC_PERM_MAX) {
  23308. ShowError("buildin_permission_check: Invalid permission %d\n", permission);
  23309. return SCRIPT_CMD_FAILURE;
  23310. }
  23311. if (strcmp(script_getfuncname(st), "permission_add") == 0)
  23312. sd->permissions.set(static_cast<e_pc_permission>(permission)); // Adds permission
  23313. else
  23314. sd->permissions.reset(static_cast<e_pc_permission>(permission)); // Removes permission
  23315. return SCRIPT_CMD_SUCCESS;
  23316. }
  23317. #include <custom/script.inc>
  23318. // declarations that were supposed to be exported from npc_chat.cpp
  23319. #ifdef PCRE_SUPPORT
  23320. BUILDIN_FUNC(defpattern);
  23321. BUILDIN_FUNC(activatepset);
  23322. BUILDIN_FUNC(deactivatepset);
  23323. BUILDIN_FUNC(deletepset);
  23324. #endif
  23325. /** Regular expression matching
  23326. * preg_match(<pattern>,<string>{,<offset>})
  23327. */
  23328. BUILDIN_FUNC(preg_match) {
  23329. #ifdef PCRE_SUPPORT
  23330. pcre *re;
  23331. pcre_extra *pcreExtra;
  23332. const char *error;
  23333. int32 erroffset, r, offset = 0;
  23334. int32 subStrVec[30];
  23335. const char* pattern = script_getstr(st,2);
  23336. const char* subject = script_getstr(st,3);
  23337. if (script_hasdata(st,4))
  23338. offset = script_getnum(st,4);
  23339. re = pcre_compile(pattern, 0, &error, &erroffset, nullptr);
  23340. pcreExtra = pcre_study(re, 0, &error);
  23341. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  23342. pcre_free(re);
  23343. if (pcreExtra != nullptr)
  23344. pcre_free(pcreExtra);
  23345. if (r < 0)
  23346. script_pushint(st,0);
  23347. else
  23348. script_pushint(st,(r > 0) ? r : 30 / 3);
  23349. return SCRIPT_CMD_SUCCESS;
  23350. #else
  23351. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  23352. script_pushint(st,0);
  23353. return SCRIPT_CMD_SUCCESS;
  23354. #endif
  23355. }
  23356. /// script command definitions
  23357. /// for an explanation on args, see add_buildin_func
  23358. struct script_function buildin_func[] = {
  23359. // NPC interaction
  23360. BUILDIN_DEF(mes,"s*"),
  23361. BUILDIN_DEF(next,""),
  23362. BUILDIN_DEF(clear,""),
  23363. BUILDIN_DEF(close,""),
  23364. BUILDIN_DEF(close2,""),
  23365. BUILDIN_DEF2(close, "close3", ""),
  23366. BUILDIN_DEF(menu,"sl*"),
  23367. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  23368. BUILDIN_DEF(prompt,"s*"),
  23369. //
  23370. BUILDIN_DEF(goto,"l"),
  23371. BUILDIN_DEF(callsub,"l*"),
  23372. BUILDIN_DEF(callfunc,"s*"),
  23373. BUILDIN_DEF(return,"?"),
  23374. BUILDIN_DEF(getarg,"i?"),
  23375. BUILDIN_DEF(jobchange,"i??"),
  23376. BUILDIN_DEF(jobname,"i"),
  23377. BUILDIN_DEF(input,"r??"),
  23378. BUILDIN_DEF(warp,"sii?"),
  23379. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  23380. BUILDIN_DEF(areawarp,"siiiisii??"),
  23381. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  23382. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  23383. BUILDIN_DEF(setlook,"ii?"),
  23384. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  23385. BUILDIN_DEF2(setr,"set","rv?"),
  23386. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  23387. BUILDIN_DEF(setarray,"rv*"),
  23388. BUILDIN_DEF(cleararray,"rvi"),
  23389. BUILDIN_DEF(copyarray,"rri"),
  23390. BUILDIN_DEF(getarraysize,"r"),
  23391. BUILDIN_DEF(deletearray,"r?"),
  23392. BUILDIN_DEF(getelementofarray,"ri"),
  23393. BUILDIN_DEF(inarray,"rv"),
  23394. BUILDIN_DEF(countinarray,"rr"),
  23395. BUILDIN_DEF(getitem,"vi?"),
  23396. BUILDIN_DEF(rentitem,"vi?"),
  23397. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  23398. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  23399. BUILDIN_DEF(getnameditem,"vv"),
  23400. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  23401. BUILDIN_DEF(makeitem,"visii?"),
  23402. BUILDIN_DEF(makeitem2,"visiiiiiiiii?"),
  23403. BUILDIN_DEF(delitem,"vi?"),
  23404. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  23405. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  23406. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  23407. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  23408. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  23409. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  23410. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  23411. BUILDIN_DEF(delitemidx,"i??"),
  23412. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  23413. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  23414. BUILDIN_DEF(cutin,"si"),
  23415. BUILDIN_DEF(viewpoint,"iiiii?"),
  23416. BUILDIN_DEF(viewpointmap, "siiiii"),
  23417. BUILDIN_DEF(heal,"ii?"),
  23418. BUILDIN_DEF(healap,"i?"),
  23419. BUILDIN_DEF(itemheal,"ii?"),
  23420. BUILDIN_DEF(percentheal,"ii?"),
  23421. BUILDIN_DEF(rand,"i?"),
  23422. BUILDIN_DEF(countitem,"v?"),
  23423. BUILDIN_DEF(storagecountitem,"v?"),
  23424. BUILDIN_DEF(guildstoragecountitem,"v?"),
  23425. BUILDIN_DEF(cartcountitem,"v?"),
  23426. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  23427. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  23428. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  23429. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  23430. BUILDIN_DEF(checkweight,"vi*"),
  23431. BUILDIN_DEF(checkweight2,"rr"),
  23432. BUILDIN_DEF(readparam,"i?"),
  23433. BUILDIN_DEF(getcharid,"i?"),
  23434. BUILDIN_DEF(getnpcid,"i?"),
  23435. BUILDIN_DEF(getpartyname,"i"),
  23436. BUILDIN_DEF(getpartymember,"i??"),
  23437. BUILDIN_DEF(getpartyleader,"i?"),
  23438. BUILDIN_DEF(getguildname,"i"),
  23439. BUILDIN_DEF(getguildmaster,"i"),
  23440. BUILDIN_DEF(getguildmasterid,"i"),
  23441. BUILDIN_DEF(strcharinfo,"i?"),
  23442. BUILDIN_DEF(strnpcinfo,"i"),
  23443. BUILDIN_DEF(getequipid,"??"),
  23444. BUILDIN_DEF(getequipuniqueid,"i?"),
  23445. BUILDIN_DEF(getequipname,"i?"),
  23446. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  23447. BUILDIN_DEF(repair,"i?"), // [Valaris]
  23448. BUILDIN_DEF(repairall,"?"),
  23449. BUILDIN_DEF(getequipisequiped,"i?"),
  23450. BUILDIN_DEF(getequipisenableref,"i?"),
  23451. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  23452. BUILDIN_DEF(getequipweaponlv,"??"),
  23453. BUILDIN_DEF(getequiparmorlv, "??"),
  23454. BUILDIN_DEF(getequippercentrefinery,"i?"),
  23455. BUILDIN_DEF(successrefitem,"i??"),
  23456. BUILDIN_DEF(failedrefitem,"i?"),
  23457. BUILDIN_DEF(downrefitem,"i??"),
  23458. BUILDIN_DEF(statusup,"i?"),
  23459. BUILDIN_DEF(statusup2,"ii?"),
  23460. BUILDIN_DEF(traitstatusup,"i?"),
  23461. BUILDIN_DEF(traitstatusup2,"ii?"),
  23462. BUILDIN_DEF(bonus,"i?"),
  23463. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  23464. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  23465. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  23466. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  23467. BUILDIN_DEF(autobonus,"sii??"),
  23468. BUILDIN_DEF(autobonus2,"sii??"),
  23469. BUILDIN_DEF(autobonus3,"siiv?"),
  23470. BUILDIN_DEF(petautobonus,"sii??"),
  23471. BUILDIN_DEF2(petautobonus,"petautobonus2","sii??"),
  23472. BUILDIN_DEF(petautobonus3,"siiv?"),
  23473. BUILDIN_DEF(plagiarizeskill, "ii"),
  23474. BUILDIN_DEF(plagiarizeskillreset, "i"),
  23475. BUILDIN_DEF(skill,"vi?"),
  23476. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  23477. BUILDIN_DEF(guildskill,"vi"),
  23478. BUILDIN_DEF(getskilllv,"v"),
  23479. BUILDIN_DEF(getgdskilllv,"iv"),
  23480. BUILDIN_DEF(basicskillcheck,""),
  23481. BUILDIN_DEF(getgmlevel,"?"),
  23482. BUILDIN_DEF(getgroupid,"?"),
  23483. BUILDIN_DEF(end,""),
  23484. BUILDIN_DEF(checkoption,"i?"),
  23485. BUILDIN_DEF(setoption,"i??"),
  23486. BUILDIN_DEF(setcart,"??"),
  23487. BUILDIN_DEF(checkcart,"?"),
  23488. BUILDIN_DEF(setfalcon,"??"),
  23489. BUILDIN_DEF(checkfalcon,"?"),
  23490. BUILDIN_DEF(setriding,"??"),
  23491. BUILDIN_DEF(checkriding,"?"),
  23492. BUILDIN_DEF(checkwug,"?"),
  23493. BUILDIN_DEF(checkmadogear,"?"),
  23494. BUILDIN_DEF(setmadogear,"???"),
  23495. BUILDIN_DEF2(savepoint,"save","sii???"),
  23496. BUILDIN_DEF(savepoint,"sii???"),
  23497. BUILDIN_DEF(gettimetick,"i"),
  23498. BUILDIN_DEF(gettime,"i"),
  23499. BUILDIN_DEF(gettimestr,"si?"),
  23500. BUILDIN_DEF(openstorage,""),
  23501. BUILDIN_DEF(guildopenstorage,""),
  23502. BUILDIN_DEF(guildopenstorage_log,"?"),
  23503. BUILDIN_DEF(guild_has_permission,"i?"),
  23504. BUILDIN_DEF(itemskill,"vi?"),
  23505. BUILDIN_DEF(produce,"i"),
  23506. BUILDIN_DEF(cooking,"i"),
  23507. BUILDIN_DEF(monster,"siisvi???"),
  23508. BUILDIN_DEF(getmobdrops,"i"),
  23509. BUILDIN_DEF(areamonster,"siiiisvi???"),
  23510. BUILDIN_DEF(killmonster,"ss?"),
  23511. BUILDIN_DEF(killmonsterall,"s?"),
  23512. BUILDIN_DEF(clone,"siisi????"),
  23513. BUILDIN_DEF(doevent,"s"),
  23514. BUILDIN_DEF(donpcevent,"s"),
  23515. BUILDIN_DEF(cmdothernpc,"ss"),
  23516. BUILDIN_DEF(addtimer,"is"),
  23517. BUILDIN_DEF(deltimer,"s"),
  23518. BUILDIN_DEF(addtimercount,"is"),
  23519. BUILDIN_DEF(initnpctimer,"??"),
  23520. BUILDIN_DEF(stopnpctimer,"??"),
  23521. BUILDIN_DEF(startnpctimer,"??"),
  23522. BUILDIN_DEF(setnpctimer,"i?"),
  23523. BUILDIN_DEF(getnpctimer,"i?"),
  23524. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  23525. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  23526. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  23527. BUILDIN_DEF(announce,"si??????"),
  23528. BUILDIN_DEF(mapannounce,"ssi?????"),
  23529. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  23530. BUILDIN_DEF(getusers,"i"),
  23531. BUILDIN_DEF(getmapguildusers,"si"),
  23532. BUILDIN_DEF(getmapusers,"s"),
  23533. BUILDIN_DEF(getareausers,"siiii"),
  23534. BUILDIN_DEF(getunits, "i?"),
  23535. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  23536. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  23537. BUILDIN_DEF(getareadropitem,"siiiiv"),
  23538. BUILDIN_DEF(enablenpc,"?"),
  23539. BUILDIN_DEF2(enablenpc, "disablenpc", "?"),
  23540. BUILDIN_DEF2(enablenpc, "hideoffnpc", "?"),
  23541. BUILDIN_DEF2(enablenpc, "hideonnpc", "?"),
  23542. BUILDIN_DEF2(enablenpc, "cloakoffnpc", "??"),
  23543. BUILDIN_DEF2(enablenpc, "cloakonnpc", "??"),
  23544. BUILDIN_DEF2(enablenpc, "cloakoffnpcself", "?"),
  23545. BUILDIN_DEF2(enablenpc, "cloakonnpcself", "?"),
  23546. BUILDIN_DEF(sc_start,"iii???"),
  23547. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  23548. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  23549. BUILDIN_DEF(sc_end,"i?"),
  23550. BUILDIN_DEF(sc_end_class,"??"),
  23551. BUILDIN_DEF(getstatus, "i??"),
  23552. BUILDIN_DEF(getscrate,"ii?"),
  23553. BUILDIN_DEF(debugmes,"s"),
  23554. BUILDIN_DEF(errormes,"s"),
  23555. BUILDIN_DEF2(catchpet,"pet","i"),
  23556. BUILDIN_DEF2(birthpet,"bpet",""),
  23557. BUILDIN_DEF(catchpet,"i"),
  23558. BUILDIN_DEF(birthpet,""),
  23559. BUILDIN_DEF(resetlvl,"i?"),
  23560. BUILDIN_DEF(resetstatus,"?"),
  23561. BUILDIN_DEF(resetskill,"?"),
  23562. BUILDIN_DEF(resetfeel,"?"),
  23563. BUILDIN_DEF(resethate,"?"),
  23564. BUILDIN_DEF(skillpointcount,"?"),
  23565. BUILDIN_DEF(changebase,"i?"),
  23566. BUILDIN_DEF(changesex,"?"),
  23567. BUILDIN_DEF(changecharsex,"?"),
  23568. BUILDIN_DEF(waitingroom,"si?????"),
  23569. BUILDIN_DEF(delwaitingroom,"?"),
  23570. BUILDIN_DEF(waitingroomkick,"ss"),
  23571. BUILDIN_DEF(getwaitingroomusers, "?"),
  23572. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  23573. BUILDIN_DEF(enablewaitingroomevent,"?"),
  23574. BUILDIN_DEF(disablewaitingroomevent,"?"),
  23575. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  23576. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  23577. BUILDIN_DEF(getwaitingroomstate,"i?"),
  23578. BUILDIN_DEF(warpwaitingpc,"sii?"),
  23579. BUILDIN_DEF(attachrid,"i?"),
  23580. BUILDIN_DEF(addrid,"i?????"),
  23581. BUILDIN_DEF(detachrid,""),
  23582. BUILDIN_DEF(isloggedin,"i?"),
  23583. BUILDIN_DEF(setmapflagnosave,"ssii"),
  23584. BUILDIN_DEF(getmapflag,"si?"),
  23585. BUILDIN_DEF(setmapflag,"si??"),
  23586. BUILDIN_DEF(removemapflag,"si?"),
  23587. BUILDIN_DEF(pvpon,"s"),
  23588. BUILDIN_DEF(pvpoff,"s"),
  23589. BUILDIN_DEF(gvgon,"s"),
  23590. BUILDIN_DEF(gvgoff,"s"),
  23591. BUILDIN_DEF(emotion,"i?"),
  23592. BUILDIN_DEF(maprespawnguildid,"sii"),
  23593. BUILDIN_DEF(agitstart,""), // <Agit>
  23594. BUILDIN_DEF(agitend,""),
  23595. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  23596. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  23597. BUILDIN_DEF(getcastlename,"s"),
  23598. BUILDIN_DEF(getcastledata,"si"),
  23599. BUILDIN_DEF(setcastledata,"sii"),
  23600. BUILDIN_DEF(requestguildinfo,"i?"),
  23601. BUILDIN_DEF(getequipcardcnt,"i"),
  23602. BUILDIN_DEF(successremovecards,"i"),
  23603. BUILDIN_DEF(failedremovecards,"ii"),
  23604. BUILDIN_DEF(marriage,"s"),
  23605. BUILDIN_DEF2(wedding_effect,"wedding",""),
  23606. BUILDIN_DEF(divorce,"?"),
  23607. BUILDIN_DEF(ispartneron,"?"),
  23608. BUILDIN_DEF(getpartnerid,"?"),
  23609. BUILDIN_DEF(getchildid,"?"),
  23610. BUILDIN_DEF(getmotherid,"?"),
  23611. BUILDIN_DEF(getfatherid,"?"),
  23612. BUILDIN_DEF(warppartner,"sii"),
  23613. BUILDIN_DEF(getitemname,"v"),
  23614. BUILDIN_DEF(getitemslots,"i"),
  23615. BUILDIN_DEF(makepet,"i"),
  23616. BUILDIN_DEF(getexp,"ii?"),
  23617. BUILDIN_DEF(getinventorylist,"?"),
  23618. BUILDIN_DEF(getskilllist,"?"),
  23619. BUILDIN_DEF(clearitem,"?"),
  23620. BUILDIN_DEF(classchange,"i??"),
  23621. BUILDIN_DEF(misceffect,"i"),
  23622. BUILDIN_DEF(playBGM,"s"),
  23623. BUILDIN_DEF(playBGMall,"s?????"),
  23624. BUILDIN_DEF(soundeffect,"si"),
  23625. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  23626. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  23627. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  23628. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  23629. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  23630. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  23631. BUILDIN_DEF(petloot,"i"), // [Valaris]
  23632. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  23633. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  23634. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  23635. BUILDIN_DEF(skilleffect,"vi?"), // skill effect [Celest]
  23636. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  23637. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  23638. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  23639. BUILDIN_DEF(removespecialeffect,"i??"),
  23640. BUILDIN_DEF2(removespecialeffect,"removespecialeffect2","i??"),
  23641. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  23642. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  23643. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  23644. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  23645. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  23646. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  23647. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  23648. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  23649. BUILDIN_DEF(mobcount,"ss"),
  23650. BUILDIN_DEF(getlook,"i?"),
  23651. BUILDIN_DEF(getsavepoint,"i?"),
  23652. BUILDIN_DEF(npcspeed,"i?"),
  23653. BUILDIN_DEF(npcwalkto,"ii?"),
  23654. BUILDIN_DEF(npcstop,"??"),
  23655. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  23656. BUILDIN_DEF(mapid2name,"i"),
  23657. BUILDIN_DEF(checkoption1,"i?"),
  23658. BUILDIN_DEF(checkoption2,"i?"),
  23659. BUILDIN_DEF(guildgetexp,"i"),
  23660. BUILDIN_DEF(guildchangegm,"is"),
  23661. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  23662. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  23663. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  23664. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  23665. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  23666. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  23667. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  23668. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  23669. BUILDIN_DEF(night,""), // sets the server to night time
  23670. BUILDIN_DEF(day,""), // sets the server to day time
  23671. #ifdef PCRE_SUPPORT
  23672. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  23673. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  23674. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  23675. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  23676. #endif
  23677. BUILDIN_DEF(preg_match,"ss?"),
  23678. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  23679. BUILDIN_DEF(recovery,"i???"),
  23680. BUILDIN_DEF(getpetinfo,"i?"),
  23681. BUILDIN_DEF(gethominfo,"i?"),
  23682. BUILDIN_DEF(addhomintimacy,"i?"),
  23683. BUILDIN_DEF(getmercinfo,"i?"),
  23684. BUILDIN_DEF(checkequipedcard,"i"),
  23685. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  23686. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  23687. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  23688. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  23689. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  23690. BUILDIN_DEF(charat,"si"),
  23691. BUILDIN_DEF(setchar,"ssi"),
  23692. BUILDIN_DEF(insertchar,"ssi"),
  23693. BUILDIN_DEF(delchar,"si"),
  23694. BUILDIN_DEF(strtoupper,"s"),
  23695. BUILDIN_DEF(strtolower,"s"),
  23696. BUILDIN_DEF(charisupper, "si"),
  23697. BUILDIN_DEF(charislower, "si"),
  23698. BUILDIN_DEF(substr,"sii"),
  23699. BUILDIN_DEF(explode, "rss"),
  23700. BUILDIN_DEF(implode, "r?"),
  23701. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  23702. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  23703. BUILDIN_DEF(strpos,"ss?"),
  23704. BUILDIN_DEF(replacestr,"sss??"),
  23705. BUILDIN_DEF(countstr,"ss?"),
  23706. BUILDIN_DEF(setnpcdisplay,"sv??"),
  23707. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  23708. BUILDIN_DEF(strcmp,"ss"),
  23709. BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
  23710. BUILDIN_DEF(setiteminfo,"vii"), //[Lupus] set Items Buy / sell Price, etc info
  23711. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  23712. // [zBuffer] List of mathematics commands --->
  23713. BUILDIN_DEF(sqrt,"i"),
  23714. BUILDIN_DEF(pow,"ii"),
  23715. BUILDIN_DEF(distance,"iiii"),
  23716. BUILDIN_DEF2(minmax,"min", "*"),
  23717. BUILDIN_DEF2(minmax,"minimum", "*"),
  23718. BUILDIN_DEF2(minmax,"max", "*"),
  23719. BUILDIN_DEF2(minmax,"maximum", "*"),
  23720. BUILDIN_DEF(cap_value, "iii"),
  23721. // <--- [zBuffer] List of mathematics commands
  23722. BUILDIN_DEF(md5,"s"),
  23723. // [zBuffer] List of dynamic var commands --->
  23724. BUILDIN_DEF(getd,"s"),
  23725. BUILDIN_DEF(setd,"sv?"),
  23726. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  23727. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  23728. BUILDIN_DEF(npcshopadditem,"sii*"),
  23729. BUILDIN_DEF(npcshopdelitem,"si*"),
  23730. BUILDIN_DEF(npcshopattach,"s?"),
  23731. BUILDIN_DEF(equip,"i?"),
  23732. BUILDIN_DEF(autoequip,"ii"),
  23733. BUILDIN_DEF(setbattleflag,"si?"),
  23734. BUILDIN_DEF(getbattleflag,"s"),
  23735. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  23736. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  23737. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  23738. BUILDIN_DEF(getrandmobid, "i??"),
  23739. BUILDIN_DEF(getmonsterinfo,"vi"), //Lupus
  23740. BUILDIN_DEF(addmonsterdrop,"vii??"), //Akinari [Lupus]
  23741. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  23742. BUILDIN_DEF(axtoi,"s"),
  23743. BUILDIN_DEF(query_sql,"s*"),
  23744. BUILDIN_DEF(query_logsql,"s*"),
  23745. BUILDIN_DEF(escape_sql,"v"),
  23746. BUILDIN_DEF(atoi,"s"),
  23747. BUILDIN_DEF(strtol,"si"),
  23748. // [zBuffer] List of player cont commands --->
  23749. BUILDIN_DEF(rid2name,"i"),
  23750. BUILDIN_DEF(pcfollow,"ii"),
  23751. BUILDIN_DEF(pcstopfollow,"i"),
  23752. BUILDIN_DEF(pcblockmove,"ii"),
  23753. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  23754. BUILDIN_DEF(pcblockskill,"ii"),
  23755. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  23756. BUILDIN_DEF(setpcblock, "ii?"),
  23757. BUILDIN_DEF(getpcblock, "?"),
  23758. // <--- [zBuffer] List of player cont commands
  23759. // [zBuffer] List of unit control commands --->
  23760. BUILDIN_DEF(unitexists,"i"),
  23761. BUILDIN_DEF(getunittype,"i"),
  23762. BUILDIN_DEF(getunitname,"i"),
  23763. BUILDIN_DEF(setunitname,"is"),
  23764. BUILDIN_DEF(setunittitle,"is"),
  23765. BUILDIN_DEF(getunittitle,"i"),
  23766. BUILDIN_DEF(getunitdata,"i*"),
  23767. BUILDIN_DEF(setunitdata,"iii"),
  23768. BUILDIN_DEF(unitwalk,"iii?"),
  23769. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  23770. BUILDIN_DEF(unitkill,"i"),
  23771. BUILDIN_DEF(unitwarp,"isii"),
  23772. BUILDIN_DEF(unitattack,"iv?"),
  23773. BUILDIN_DEF(unitstopattack,"i"),
  23774. BUILDIN_DEF(unitstopwalk,"i?"),
  23775. BUILDIN_DEF(unittalk,"is?"),
  23776. BUILDIN_DEF(unitskilluseid,"ivi?????"), // originally by Qamera [Celest]
  23777. BUILDIN_DEF(unitskillusepos,"iviii????"), // [Celest]
  23778. // <--- [zBuffer] List of unit control commands
  23779. BUILDIN_DEF(sleep,"i"),
  23780. BUILDIN_DEF(sleep2,"i"),
  23781. BUILDIN_DEF(awake,"s"),
  23782. BUILDIN_DEF(getvariableofnpc,"rs"),
  23783. BUILDIN_DEF(warpportal,"iisii"),
  23784. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  23785. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  23786. BUILDIN_DEF(morphembryo,""),
  23787. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  23788. BUILDIN_DEF(checkhomcall,""),
  23789. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  23790. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  23791. BUILDIN_DEF(checkvending,"?"),
  23792. BUILDIN_DEF(checkchatting,"?"),
  23793. BUILDIN_DEF(checkidle,"?"),
  23794. BUILDIN_DEF(checkidlehom,"?"),
  23795. BUILDIN_DEF(checkidlemer,"?"),
  23796. BUILDIN_DEF(openmail,"?"),
  23797. BUILDIN_DEF(openauction,"?"),
  23798. BUILDIN_DEF(checkcell,"siii"),
  23799. BUILDIN_DEF(setcell,"siiiiii"),
  23800. BUILDIN_DEF(getfreecell,"srr?????"),
  23801. BUILDIN_DEF(setwall,"siiiiis"),
  23802. BUILDIN_DEF(delwall,"s"),
  23803. BUILDIN_DEF(checkwall,"s"),
  23804. BUILDIN_DEF(searchitem,"rs"),
  23805. BUILDIN_DEF(mercenary_create,"ii"),
  23806. BUILDIN_DEF(mercenary_delete,"??"),
  23807. BUILDIN_DEF(mercenary_heal,"ii"),
  23808. BUILDIN_DEF(mercenary_sc_start,"iii"),
  23809. BUILDIN_DEF(mercenary_get_calls,"i"),
  23810. BUILDIN_DEF(mercenary_get_faith,"i"),
  23811. BUILDIN_DEF(mercenary_set_calls,"ii"),
  23812. BUILDIN_DEF(mercenary_set_faith,"ii"),
  23813. BUILDIN_DEF(readbook,"ii"),
  23814. BUILDIN_DEF(setfont,"i"),
  23815. BUILDIN_DEF(areamobuseskill,"siiivviiiii"),
  23816. BUILDIN_DEF(progressbar,"si"),
  23817. BUILDIN_DEF(progressbar_npc, "si?"),
  23818. BUILDIN_DEF(pushpc,"ii"),
  23819. BUILDIN_DEF(buyingstore,"i"),
  23820. BUILDIN_DEF(searchstores,"ii?"),
  23821. BUILDIN_DEF(showdigit,"i?"),
  23822. // WoE SE
  23823. BUILDIN_DEF(agitstart2,""),
  23824. BUILDIN_DEF(agitend2,""),
  23825. BUILDIN_DEF(agitcheck2,""),
  23826. // BattleGround
  23827. BUILDIN_DEF(waitingroom2bg,"sii???"),
  23828. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  23829. BUILDIN_DEF(bg_team_setxy,"iii"),
  23830. BUILDIN_DEF(bg_warp,"isii"),
  23831. BUILDIN_DEF(bg_monster,"isiisi?"),
  23832. BUILDIN_DEF(bg_monster_set_team,"ii"),
  23833. BUILDIN_DEF(bg_leave,"?"),
  23834. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  23835. BUILDIN_DEF(bg_destroy,"i"),
  23836. BUILDIN_DEF(areapercentheal,"siiiiii"),
  23837. BUILDIN_DEF(bg_get_data,"ii"),
  23838. BUILDIN_DEF(bg_getareausers,"isiiii"),
  23839. BUILDIN_DEF(bg_updatescore,"sii"),
  23840. BUILDIN_DEF(bg_join,"i????"),
  23841. BUILDIN_DEF(bg_create,"sii??"),
  23842. BUILDIN_DEF(bg_reserve,"s?"),
  23843. BUILDIN_DEF(bg_unbook,"s"),
  23844. BUILDIN_DEF(bg_info,"si"),
  23845. // Instancing
  23846. BUILDIN_DEF(instance_create,"s??"),
  23847. BUILDIN_DEF(instance_destroy,"?"),
  23848. BUILDIN_DEF(instance_id,"?"),
  23849. BUILDIN_DEF(instance_enter,"s????"),
  23850. BUILDIN_DEF(instance_npcname,"s?"),
  23851. BUILDIN_DEF(instance_mapname,"s?"),
  23852. BUILDIN_DEF(instance_warpall,"sii??"),
  23853. BUILDIN_DEF(instance_announce,"isi?????"),
  23854. BUILDIN_DEF(instance_check_party,"i???"),
  23855. BUILDIN_DEF(instance_check_guild,"i???"),
  23856. BUILDIN_DEF(instance_check_clan,"i???"),
  23857. BUILDIN_DEF(instance_info,"si?"),
  23858. BUILDIN_DEF(instance_live_info,"i?"),
  23859. BUILDIN_DEF(instance_list, "s?"),
  23860. /**
  23861. * 3rd-related
  23862. **/
  23863. BUILDIN_DEF(makerune,"i?"),
  23864. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  23865. BUILDIN_DEF(setdragon,"??"),//[Ind]
  23866. BUILDIN_DEF(ismounting,"?"),//[Ind]
  23867. BUILDIN_DEF(setmounting,"?"),//[Ind]
  23868. BUILDIN_DEF(checkre,"i"),
  23869. /**
  23870. * rAthena and beyond!
  23871. **/
  23872. BUILDIN_DEF(getargcount,""),
  23873. BUILDIN_DEF(getcharip,"?"),
  23874. BUILDIN_DEF(is_function,"s"),
  23875. BUILDIN_DEF(get_revision,""),
  23876. BUILDIN_DEF(get_githash,""),
  23877. BUILDIN_DEF(freeloop,"?"),
  23878. BUILDIN_DEF(getrandgroupitem,"i????"),
  23879. BUILDIN_DEF(cleanmap,"s"),
  23880. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  23881. BUILDIN_DEF(npcskill,"viii"),
  23882. BUILDIN_DEF(consumeitem,"v?"),
  23883. BUILDIN_DEF(delequip,"i?"),
  23884. BUILDIN_DEF(breakequip,"i?"),
  23885. BUILDIN_DEF(sit,"?"),
  23886. BUILDIN_DEF(stand,"?"),
  23887. //@commands (script based)
  23888. BUILDIN_DEF(bindatcmd, "ss??"),
  23889. BUILDIN_DEF(unbindatcmd, "s"),
  23890. BUILDIN_DEF(useatcmd, "s"),
  23891. //Quest Log System [Inkfish]
  23892. BUILDIN_DEF(questinfo, "i??"),
  23893. BUILDIN_DEF(setquest, "i?"),
  23894. BUILDIN_DEF(erasequest, "i?"),
  23895. BUILDIN_DEF(completequest, "i?"),
  23896. BUILDIN_DEF(checkquest, "i??"),
  23897. BUILDIN_DEF(isbegin_quest,"i?"),
  23898. BUILDIN_DEF(changequest, "ii?"),
  23899. BUILDIN_DEF(showevent, "i??"),
  23900. BUILDIN_DEF(questinfo_refresh, "?"),
  23901. //Bound items [Xantara] & [Akinari]
  23902. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  23903. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  23904. BUILDIN_DEF(countbound, "??"),
  23905. // Party related
  23906. BUILDIN_DEF(party_create,"s???"),
  23907. BUILDIN_DEF(party_addmember,"ii"),
  23908. BUILDIN_DEF(party_delmember,"??"),
  23909. BUILDIN_DEF(party_changeleader,"ii"),
  23910. BUILDIN_DEF(party_changeoption,"iii"),
  23911. BUILDIN_DEF(party_destroy,"i"),
  23912. // Clan system
  23913. BUILDIN_DEF(clan_join,"i?"),
  23914. BUILDIN_DEF(clan_leave,"?"),
  23915. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  23916. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  23917. BUILDIN_DEF(vip_status,"i?"),
  23918. BUILDIN_DEF(vip_time,"i?"),
  23919. BUILDIN_DEF(bonus_script,"si????"),
  23920. BUILDIN_DEF(bonus_script_clear,"??"),
  23921. BUILDIN_DEF(getgroupitem,"i??"),
  23922. BUILDIN_DEF(enable_command,""),
  23923. BUILDIN_DEF(disable_command,""),
  23924. BUILDIN_DEF(getguildmember,"i??"),
  23925. BUILDIN_DEF(addspiritball,"ii?"),
  23926. BUILDIN_DEF(delspiritball,"i?"),
  23927. BUILDIN_DEF(countspiritball,"?"),
  23928. BUILDIN_DEF(mergeitem,"?"),
  23929. BUILDIN_DEF(mergeitem2,"??"),
  23930. BUILDIN_DEF(npcshopupdate,"sii?"),
  23931. BUILDIN_DEF(getattachedrid,""),
  23932. BUILDIN_DEF(getvar,"vi"),
  23933. BUILDIN_DEF(showscript,"s??"),
  23934. BUILDIN_DEF(ignoretimeout,"i?"),
  23935. BUILDIN_DEF(geteleminfo,"i?"),
  23936. BUILDIN_DEF(opendressroom,"i?"),
  23937. BUILDIN_DEF(navigateto,"s???????"),
  23938. BUILDIN_DEF(getguildalliance,"ii"),
  23939. BUILDIN_DEF(adopt,"vv"),
  23940. BUILDIN_DEF(getexp2,"ii?"),
  23941. BUILDIN_DEF(recalculatestat,""),
  23942. BUILDIN_DEF(hateffect,"ii"),
  23943. BUILDIN_DEF(getrandomoptinfo, "i"),
  23944. BUILDIN_DEF(getequiprandomoption, "iii?"),
  23945. BUILDIN_DEF(setrandomoption,"iiiii?"),
  23946. BUILDIN_DEF(needed_status_point,"ii?"),
  23947. BUILDIN_DEF(needed_trait_point, "ii?"),
  23948. BUILDIN_DEF(jobcanentermap,"s?"),
  23949. BUILDIN_DEF(openstorage2,"i??"),
  23950. BUILDIN_DEF(unloadnpc, "s"),
  23951. BUILDIN_DEF(duplicate, "ssii?????"),
  23952. BUILDIN_DEF(duplicate_dynamic, "s?"),
  23953. // WoE TE
  23954. BUILDIN_DEF(agitstart3,""),
  23955. BUILDIN_DEF(agitend3,""),
  23956. BUILDIN_DEF(agitcheck3,""),
  23957. BUILDIN_DEF(gvgon3,"s"),
  23958. BUILDIN_DEF(gvgoff3,"s"),
  23959. // Channel System
  23960. BUILDIN_DEF(channel_create,"ss?????"),
  23961. BUILDIN_DEF(channel_join, "s?"),
  23962. BUILDIN_DEF(channel_setopt,"sii"),
  23963. BUILDIN_DEF(channel_getopt,"si"),
  23964. BUILDIN_DEF(channel_setcolor,"si"),
  23965. BUILDIN_DEF(channel_setpass,"ss"),
  23966. BUILDIN_DEF(channel_setgroup,"si*"),
  23967. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  23968. BUILDIN_DEF(channel_chat,"ss?"),
  23969. BUILDIN_DEF(channel_ban,"si"),
  23970. BUILDIN_DEF(channel_unban,"si"),
  23971. BUILDIN_DEF(channel_kick,"sv"),
  23972. BUILDIN_DEF(channel_delete,"s"),
  23973. // Item Random Option Extension [Cydh]
  23974. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  23975. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  23976. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  23977. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr?"),
  23978. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  23979. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  23980. // Grade Extension
  23981. BUILDIN_DEF2(getitem2,"getitem4","viiiiiiiiirrr?"),
  23982. BUILDIN_DEF2(getitem2,"getitembound4","viiiiiiiiiirrr?"),
  23983. BUILDIN_DEF2(rentitem2,"rentitem4","viiiiiiiiirrr?"),
  23984. BUILDIN_DEF2(makeitem2,"makeitem4","visiiiiiiiiiirrr?"),
  23985. BUILDIN_DEF2(delitem2,"delitem4","viiiiiiiiirrr?"),
  23986. BUILDIN_DEF2(countitem,"countitem4","viiiiiiiirrr?"),
  23987. // Achievement System
  23988. BUILDIN_DEF(achievementinfo,"ii?"),
  23989. BUILDIN_DEF(achievementadd,"i?"),
  23990. BUILDIN_DEF(achievementremove,"i?"),
  23991. BUILDIN_DEF(achievementcomplete,"i?"),
  23992. BUILDIN_DEF(achievementexists,"i?"),
  23993. BUILDIN_DEF(achievementupdate,"iii?"),
  23994. BUILDIN_DEF(getequiprefinecost,"iii?"),
  23995. BUILDIN_DEF(refineui,"?"),
  23996. BUILDIN_DEF2(round, "round", "ii"),
  23997. BUILDIN_DEF2(round, "ceil", "ii"),
  23998. BUILDIN_DEF2(round, "floor", "ii"),
  23999. BUILDIN_DEF(getequiptradability, "i?"),
  24000. BUILDIN_DEF(mail, "isss*"),
  24001. BUILDIN_DEF(open_roulette,"?"),
  24002. BUILDIN_DEF(identifyall,"??"),
  24003. BUILDIN_DEF(is_guild_leader,"?"),
  24004. BUILDIN_DEF(is_party_leader,"?"),
  24005. BUILDIN_DEF(camerainfo,"iii?"),
  24006. BUILDIN_DEF(achievement_condition,"i"),
  24007. BUILDIN_DEF(getinstancevar,"ri"),
  24008. BUILDIN_DEF2_DEPRECATED(getinstancevar, "getvariableofinstance","ri", "2021-12-13"),
  24009. BUILDIN_DEF(convertpcinfo,"vi"),
  24010. BUILDIN_DEF(isnpccloaked, "??"),
  24011. BUILDIN_DEF(rentalcountitem, "v?"),
  24012. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  24013. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  24014. BUILDIN_DEF2(rentalcountitem, "rentalcountitem4", "viiiiiiiirrr?"),
  24015. BUILDIN_DEF(getenchantgrade, "??"),
  24016. BUILDIN_DEF(mob_setidleevent, "is"),
  24017. BUILDIN_DEF(setinstancevar,"rvi"),
  24018. BUILDIN_DEF(openstylist, "?"),
  24019. // Navigation Generation System
  24020. BUILDIN_DEF(naviregisterwarp, "ssii"),
  24021. BUILDIN_DEF(navihide, ""),
  24022. BUILDIN_DEF(getitempos,""),
  24023. BUILDIN_DEF(laphine_synthesis, "?"),
  24024. BUILDIN_DEF(laphine_upgrade, ""),
  24025. BUILDIN_DEF(randomoptgroup,"i"),
  24026. BUILDIN_DEF(open_quest_ui, "??"),
  24027. BUILDIN_DEF(openbank,"?"),
  24028. BUILDIN_DEF(getbaseexp_ratio, "i??"),
  24029. BUILDIN_DEF(getjobexp_ratio, "i??"),
  24030. BUILDIN_DEF(enchantgradeui, "?" ),
  24031. BUILDIN_DEF(set_reputation_points, "ii?"),
  24032. BUILDIN_DEF(get_reputation_points, "i?"),
  24033. BUILDIN_DEF(add_reputation_points, "ii?"),
  24034. BUILDIN_DEF(item_reform, "??"),
  24035. BUILDIN_DEF(item_enchant, "i?"),
  24036. BUILDIN_DEF(itemlink, "i?????????"),
  24037. BUILDIN_DEF(mesitemlink, "i??"),
  24038. BUILDIN_DEF(addfame, "i?"),
  24039. BUILDIN_DEF(getfame, "?"),
  24040. BUILDIN_DEF(getfamerank, "?"),
  24041. BUILDIN_DEF(isdead, "?"),
  24042. BUILDIN_DEF(macro_detector, "?"),
  24043. BUILDIN_DEF(has_autoloot,"?"),
  24044. BUILDIN_DEF(autoloot,"??"),
  24045. BUILDIN_DEF(opentips, "i?"),
  24046. BUILDIN_DEF(specialpopup,"i"),
  24047. BUILDIN_DEF(setdialogalign, "i"),
  24048. BUILDIN_DEF(setdialogsize, "ii"),
  24049. BUILDIN_DEF(setdialogpos, "ii"),
  24050. BUILDIN_DEF(setdialogpospercent, "ii"),
  24051. BUILDIN_DEF(permission_check, "i?"),
  24052. BUILDIN_DEF(permission_add, "i?"),
  24053. BUILDIN_DEF2(permission_add, "permission_remove", "i?"),
  24054. #include <custom/script_def.inc>
  24055. {nullptr,nullptr,nullptr},
  24056. };