script.cpp 752 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889
  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 <errno.h>
  10. #include <math.h>
  11. #include <setjmp.h>
  12. #include <stdlib.h> // 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. unsigned int active_scripts;
  65. unsigned int 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. int 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. int 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. int 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. int 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 = NULL;
  177. static int script_pos = 0, script_size = 0;
  178. static inline int GETVALUE(const unsigned char* buf, int 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, int i, int 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. int str;
  193. int backpatch;
  194. int label;
  195. int (*func)(struct script_state *st);
  196. int64 val;
  197. int next;
  198. const char *name;
  199. bool deprecated;
  200. } *str_data = nullptr;
  201. static int str_data_size = 0; // size of the data
  202. static int str_num = LABEL_START; // next id to be assigned
  203. // str_buf holds the strings themselves
  204. static char *str_buf;
  205. static int str_size = 0; // size of the buffer
  206. static int 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. int 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 = NULL; // const char* label_name -> int script_pos
  215. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  216. static int 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 int buildin_set_ref = 0;
  221. static int buildin_callsub_ref = 0;
  222. static int buildin_callfunc_ref = 0;
  223. static int buildin_getelementofarray_ref = 0;
  224. // Caches compiled autoscript item code.
  225. // Note: This is not cleared when reloading itemdb.
  226. static DBMap* autobonus_db = NULL; // 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 int 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 int 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. int index;
  313. int count;
  314. int flag;
  315. struct linkdb_node *case_label;
  316. } curly[256]; // Information right parenthesis
  317. int curly_count; // The number of right brackets
  318. int 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 int 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 unsigned int current_equip_combo_pos;
  327. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  328. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  329. int potion_target = 0;
  330. unsigned int *generic_ui_array = NULL;
  331. unsigned int generic_ui_array_size = 0;
  332. c_op get_com(unsigned char *script,int *pos);
  333. int64 get_num(unsigned char *script,int *pos);
  334. typedef struct script_function {
  335. int (*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; // int oid -> struct script_state *
  342. /*==========================================
  343. * (Only those needed) local declaration prototype
  344. *------------------------------------------*/
  345. const char* parse_subexpr(const char* p,int limit);
  346. int run_func(struct script_state *st);
  347. int script_instancegetid(struct script_state *st, e_instance_mode mode = IM_NONE);
  348. const char* script_op2name(int 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. int 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 == NULL )
  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=NULL\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. int 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. int 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,int 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 unsigned int calc_hash(const char* p)
  561. {
  562. unsigned int 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. unsigned int 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(int 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(int 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 int search_str(const char* p)
  615. {
  616. int 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. int add_str(const char* p)
  625. {
  626. int h;
  627. int 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. int 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 = NULL;
  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(int 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(int l)
  710. {
  711. int 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(int l,int pos, const char* script_pos_cur)
  746. {
  747. int 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. int 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 == NULL )
  770. return NULL;
  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 int add_word(const char* p)
  831. {
  832. char* word;
  833. int len;
  834. int i;
  835. // Check for a word
  836. len = skip_word(p) - p;
  837. if( len == 0 )
  838. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  839. // Duplicate the word
  840. word = (char*)aMalloc(len+1);
  841. memcpy(word, p, len);
  842. word[len] = 0;
  843. // add the word
  844. i = add_str(word);
  845. aFree(word);
  846. return i;
  847. }
  848. /// Parses a function call.
  849. /// The argument list can have parenthesis or not.
  850. /// The number of arguments is checked.
  851. static
  852. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  853. {
  854. const char* p2;
  855. const char* arg=NULL;
  856. int func;
  857. func = add_word(p);
  858. if( str_data[func].type == C_FUNC ){
  859. // buildin function
  860. add_scriptl(func);
  861. add_scriptc(C_ARG);
  862. arg = buildin_func[str_data[func].val].arg;
  863. #if defined(SCRIPT_COMMAND_DEPRECATION)
  864. if( str_data[func].deprecated ){
  865. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  866. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  867. }
  868. #endif
  869. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  870. // script defined function
  871. add_scriptl(buildin_callsub_ref);
  872. add_scriptc(C_ARG);
  873. add_scriptl(func);
  874. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  875. if( *arg == 0 )
  876. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  877. if( *arg != '*' )
  878. ++arg; // count func as argument
  879. } else {
  880. const char* name = get_str(func);
  881. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  882. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  883. } else {;
  884. add_scriptl(buildin_callfunc_ref);
  885. add_scriptc(C_ARG);
  886. add_scriptc(C_STR);
  887. while( *name ) add_scriptb(*name ++);
  888. add_scriptb(0);
  889. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  890. if( *arg != '*' ) ++ arg;
  891. }
  892. }
  893. p = skip_word(p);
  894. p = skip_space(p);
  895. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  896. syntax.curly[syntax.curly_count].count = 0;
  897. if( *p == ';' )
  898. {// <func name> ';'
  899. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  900. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  901. {// <func name> '(' ')'
  902. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  903. p = p2;
  904. /*
  905. } else if( 0 && require_paren && *p != '(' )
  906. {// <func name>
  907. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  908. */
  909. } else
  910. {// <func name> <arg list>
  911. if( require_paren ){
  912. if( *p != '(' )
  913. disp_error_message("need '('",p);
  914. ++p; // skip '('
  915. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  916. } else if( *p == '(' ){
  917. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  918. } else {
  919. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  920. }
  921. ++syntax.curly_count;
  922. while( *arg ) {
  923. p2=parse_subexpr(p,-1);
  924. if( p == p2 )
  925. break; // not an argument
  926. if( *arg != '*' )
  927. ++arg; // next argument
  928. p=skip_space(p2);
  929. if( *arg == 0 || *p != ',' )
  930. break; // no more arguments
  931. ++p; // skip comma
  932. }
  933. --syntax.curly_count;
  934. }
  935. if( *arg && *arg != '?' && *arg != '*' )
  936. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  937. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  938. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  939. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  940. if( *p != ')' )
  941. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  942. ++p;
  943. }
  944. add_scriptc(C_FUNC);
  945. return p;
  946. }
  947. /// Processes end of logical script line.
  948. /// @param first When true, only fix up scheduling data is initialized
  949. /// @param p Script position for error reporting in set_label
  950. static void parse_nextline(bool first, const char* p)
  951. {
  952. if( !first )
  953. {
  954. add_scriptc(C_EOL); // mark end of line for stack cleanup
  955. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  956. }
  957. // initialize data for new '-' label fix up scheduling
  958. str_data[LABEL_NEXTLINE].type = C_NOP;
  959. str_data[LABEL_NEXTLINE].backpatch = -1;
  960. str_data[LABEL_NEXTLINE].label = -1;
  961. }
  962. /**
  963. * Pushes a variable into stack, processing its array index if needed.
  964. * @see parse_variable
  965. */
  966. void parse_variable_sub_push(int word, const char *p2)
  967. {
  968. if( p2 ) { // Process the variable index
  969. const char *p3 = NULL;
  970. // Push the getelementofarray method into the stack
  971. add_scriptl(buildin_getelementofarray_ref);
  972. add_scriptc(C_ARG);
  973. add_scriptl(word);
  974. // Process the sub-expression for this assignment
  975. p3 = parse_subexpr(p2 + 1, 1);
  976. p3 = skip_space(p3);
  977. if( *p3 != ']' ) // Closing parenthesis is required for this script
  978. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  979. // Push the closing function stack operator onto the stack
  980. add_scriptc(C_FUNC);
  981. p3++;
  982. } else // No array index, simply push the variable or value onto the stack
  983. add_scriptl(word);
  984. }
  985. /// Parse a variable assignment using the direct equals operator
  986. /// @param p script position where the function should run from
  987. /// @return NULL if not a variable assignment, the new position otherwise
  988. const char* parse_variable(const char* p) {
  989. int word;
  990. c_op type = C_NOP;
  991. const char *p2 = NULL;
  992. const char *var = p;
  993. if( p[0] == '+' && p[1] == '+' ){
  994. type = C_ADD_PRE; // pre ++
  995. }else if( p[0] == '-' && p[1] == '-' ){
  996. type = C_SUB_PRE; // pre --
  997. }
  998. if( type != C_NOP ){
  999. var = p = skip_space(&p[2]);
  1000. }
  1001. // skip the variable where applicable
  1002. p = skip_word(p);
  1003. p = skip_space(p);
  1004. if( p == NULL ) {// end of the line or invalid buffer
  1005. return NULL;
  1006. }
  1007. if( *p == '[' ) {// array variable so process the array as appropriate
  1008. int i,j;
  1009. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1010. if( *p ++ == ']' && --(j) == 0 ) break;
  1011. if( *p == '[' ) ++ j;
  1012. }
  1013. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1014. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1015. }
  1016. }
  1017. if( type == C_NOP ){
  1018. if( p[0] == '=' && p[1] != '=' ){
  1019. type = C_EQ; // =
  1020. }else if( p[0] == '+' && p[1] == '=' ){
  1021. type = C_ADD; // +=
  1022. }else if( p[0] == '-' && p[1] == '=' ){
  1023. type = C_SUB; // -=
  1024. }else if( p[0] == '^' && p[1] == '=' ){
  1025. type = C_XOR; // ^=
  1026. }else if( p[0] == '|' && p[1] == '=' ){
  1027. type = C_OR; // |=
  1028. }else if( p[0] == '&' && p[1] == '=' ){
  1029. type = C_AND; // &=
  1030. }else if( p[0] == '*' && p[1] == '=' ){
  1031. type = C_MUL; // *=
  1032. }else if( p[0] == '/' && p[1] == '=' ){
  1033. type = C_DIV; // /=
  1034. }else if( p[0] == '%' && p[1] == '=' ){
  1035. type = C_MOD; // %=
  1036. }else if( p[0] == '~' && p[1] == '=' ){
  1037. type = C_NOT; // ~=
  1038. }else if( p[0] == '+' && p[1] == '+' ){
  1039. type = C_ADD_POST; // post ++
  1040. }else if( p[0] == '-' && p[1] == '-' ){
  1041. type = C_SUB_POST; // post --
  1042. }else if( p[0] == '<' && p[1] == '<' && p[2] == '=' ){
  1043. type = C_L_SHIFT; // <<=
  1044. }else if( p[0] == '>' && p[1] == '>' && p[2] == '=' ){
  1045. type = C_R_SHIFT; // >>=
  1046. }else{
  1047. // failed to find a matching operator combination so invalid
  1048. return nullptr;
  1049. }
  1050. }
  1051. switch( type ) {
  1052. case C_ADD_PRE: // pre ++
  1053. case C_SUB_PRE: // pre --
  1054. // Nothing more to skip
  1055. break;
  1056. case C_EQ: {// incremental modifier
  1057. p = skip_space( &p[1] );
  1058. }
  1059. break;
  1060. case C_L_SHIFT:
  1061. case C_R_SHIFT: {// left or right shift modifier
  1062. p = skip_space( &p[3] );
  1063. }
  1064. break;
  1065. default: {// normal incremental command
  1066. p = skip_space( &p[2] );
  1067. }
  1068. }
  1069. if( p == NULL ) {// end of line or invalid buffer
  1070. return NULL;
  1071. }
  1072. // push the set function onto the stack
  1073. add_scriptl(buildin_set_ref);
  1074. add_scriptc(C_ARG);
  1075. // always append parenthesis to avoid errors
  1076. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1077. syntax.curly[syntax.curly_count].count = 0;
  1078. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1079. // increment the total curly count for the position in the script
  1080. ++ syntax.curly_count;
  1081. // parse the variable currently being modified
  1082. word = add_word(var);
  1083. 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
  1084. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1085. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1086. if( type != C_EQ )
  1087. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1088. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1089. add_scripti(1);
  1090. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1091. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1092. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1093. add_scripti(1);
  1094. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1095. } else { // Process the value as an expression
  1096. p = parse_subexpr(p, -1);
  1097. if( type != C_EQ )
  1098. {// push the type of modifier onto the stack
  1099. add_scriptc(type);
  1100. }
  1101. }
  1102. // decrement the curly count for the position within the script
  1103. -- syntax.curly_count;
  1104. // close the script by appending the function operator
  1105. add_scriptc(C_FUNC);
  1106. // push the buffer from the method
  1107. return p;
  1108. }
  1109. /*
  1110. * Checks whether the gives string is a number literal
  1111. *
  1112. * Mainly necessary to differentiate between number literals and NPC name
  1113. * constants, since several of those start with a digit.
  1114. *
  1115. * All this does is to check if the string begins with an optional + or - sign,
  1116. * followed by a hexadecimal or decimal number literal literal and is NOT
  1117. * followed by a underscore or letter.
  1118. *
  1119. * @author : Hercules.ws
  1120. * @param p Pointer to the string to check
  1121. * @return Whether the string is a number literal
  1122. */
  1123. bool is_number(const char *p) {
  1124. const char *np;
  1125. if (!p)
  1126. return false;
  1127. if (*p == '-' || *p == '+')
  1128. p++;
  1129. np = p;
  1130. if (*p == '0' && p[1] == 'x') {
  1131. p+=2;
  1132. np = p;
  1133. // Hexadecimal
  1134. while (ISXDIGIT(*np))
  1135. np++;
  1136. } else {
  1137. // Decimal
  1138. while (ISDIGIT(*np))
  1139. np++;
  1140. }
  1141. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1142. return true;
  1143. return false;
  1144. }
  1145. /*==========================================
  1146. * Analysis section
  1147. *------------------------------------------*/
  1148. const char* parse_simpleexpr(const char *p)
  1149. {
  1150. int64 i;
  1151. p=skip_space(p);
  1152. if(*p==';' || *p==',')
  1153. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1154. if(*p=='('){
  1155. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1156. ++syntax.curly[i].count;
  1157. p=parse_subexpr(p+1,-1);
  1158. p=skip_space(p);
  1159. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1160. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1161. ){
  1162. if( *p == ',' ){
  1163. syntax.curly[i].flag = ARGLIST_PAREN;
  1164. return p;
  1165. } else
  1166. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1167. }
  1168. if( *p != ')' )
  1169. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1170. ++p;
  1171. } else if(is_number(p)) {
  1172. char *np;
  1173. bool error;
  1174. // Skip leading zeroes
  1175. while(*p == '0' && ISDIGIT(p[1])) p++;
  1176. errno = 0;
  1177. i=strtoll(p,&np,0);
  1178. error = (errno == ERANGE);
  1179. if( i < SCRIPT_INT_MIN || ( error && i == INT64_MIN ) ){
  1180. i = SCRIPT_INT_MIN;
  1181. disp_warning_message( "parse_simpleexpr: underflow detected, capping value to SCRIPT_INT_MIN", p );
  1182. }else if( i > SCRIPT_INT_MAX || ( error && i == INT64_MAX ) ){
  1183. i = SCRIPT_INT_MAX;
  1184. disp_warning_message( "parse_simpleexpr: overflow detected, capping value to SCRIPT_INT_MAX", p );
  1185. }
  1186. add_scripti(i);
  1187. p=np;
  1188. } else if(*p=='"'){
  1189. add_scriptc(C_STR);
  1190. p++;
  1191. while( *p && *p != '"' ){
  1192. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1193. {
  1194. char buf[8];
  1195. size_t len = skip_escaped_c(p) - p;
  1196. size_t n = sv_unescape_c(buf, p, len);
  1197. if( n != 1 )
  1198. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1199. p += len;
  1200. add_scriptb(*buf);
  1201. continue;
  1202. }
  1203. else if( *p == '\n' )
  1204. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1205. add_scriptb(*p++);
  1206. }
  1207. if(!*p)
  1208. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1209. add_scriptb(0);
  1210. p++; //'"'
  1211. } else {
  1212. int l;
  1213. const char* pv;
  1214. // label , register , function etc
  1215. if(skip_word(p)==p)
  1216. disp_error_message("parse_simpleexpr: unexpected character",p);
  1217. l=add_word(p);
  1218. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1219. return parse_callfunc(p,1,0);
  1220. else {
  1221. const char* name = get_str(l);
  1222. if( strdb_get(userfunc_db,name) != NULL ) {
  1223. return parse_callfunc(p,1,1);
  1224. }
  1225. }
  1226. if( (pv = parse_variable(p)) )
  1227. {// successfully processed a variable assignment
  1228. return pv;
  1229. }
  1230. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1231. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1232. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1233. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1234. if (str_data[l].name)
  1235. ShowWarning( "Please use '%s' instead!\n", str_data[l].name );
  1236. disp_warning_message("parse_simpleexpr: deprecated constant", p);
  1237. }
  1238. #endif
  1239. p=skip_word(p);
  1240. if( *p == '[' ){
  1241. // array(name[i] => getelementofarray(name,i) )
  1242. add_scriptl(buildin_getelementofarray_ref);
  1243. add_scriptc(C_ARG);
  1244. add_scriptl(l);
  1245. p=parse_subexpr(p+1,-1);
  1246. p=skip_space(p);
  1247. if( *p != ']' )
  1248. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1249. ++p;
  1250. add_scriptc(C_FUNC);
  1251. }else
  1252. add_scriptl(l);
  1253. }
  1254. return p;
  1255. }
  1256. /*==========================================
  1257. * Analysis of the expression
  1258. *------------------------------------------*/
  1259. const char* parse_subexpr(const char* p,int limit)
  1260. {
  1261. int op,opl,len;
  1262. p=skip_space(p);
  1263. if( *p == '-' ){
  1264. const char* tmpp = skip_space(p+1);
  1265. if( *tmpp == ';' || *tmpp == ',' ){
  1266. add_scriptl(LABEL_NEXTLINE);
  1267. p++;
  1268. return p;
  1269. }
  1270. }
  1271. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1272. p = parse_variable(p);
  1273. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1274. p = parse_subexpr(p + 1, 11);
  1275. add_scriptc(op);
  1276. } else
  1277. p = parse_simpleexpr(p);
  1278. p = skip_space(p);
  1279. while((
  1280. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1281. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1282. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1283. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1284. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1285. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1286. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1287. (op=C_AND,opl=5,len=1,*p=='&') ||
  1288. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1289. (op=C_OR,opl=3,len=1,*p=='|') ||
  1290. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1291. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1292. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1293. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1294. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1295. (op=C_GT,opl=7,len=1,*p=='>') ||
  1296. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1297. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1298. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1299. p+=len;
  1300. if(op == C_OP3) {
  1301. p=parse_subexpr(p,-1);
  1302. p=skip_space(p);
  1303. if( *(p++) != ':')
  1304. disp_error_message("parse_subexpr: expected ':'", p-1);
  1305. p=parse_subexpr(p,-1);
  1306. } else {
  1307. p=parse_subexpr(p,opl);
  1308. }
  1309. add_scriptc(op);
  1310. p=skip_space(p);
  1311. }
  1312. return p; /* return first untreated operator */
  1313. }
  1314. /*==========================================
  1315. * Evaluation of the expression
  1316. *------------------------------------------*/
  1317. const char* parse_expr(const char *p)
  1318. {
  1319. switch(*p){
  1320. case ')': case ';': case ':': case '[': case ']':
  1321. case '}':
  1322. disp_error_message("parse_expr: unexpected character",p);
  1323. }
  1324. p=parse_subexpr(p,-1);
  1325. return p;
  1326. }
  1327. /*==========================================
  1328. * Analysis of the line
  1329. *------------------------------------------*/
  1330. const char* parse_line(const char* p)
  1331. {
  1332. const char* p2;
  1333. p=skip_space(p);
  1334. if(*p==';') {
  1335. //Close decision for if(); for(); while();
  1336. p = parse_syntax_close(p + 1);
  1337. return p;
  1338. }
  1339. if(*p==')' && parse_syntax_for_flag)
  1340. return p+1;
  1341. p = skip_space(p);
  1342. if(p[0] == '{') {
  1343. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1344. syntax.curly[syntax.curly_count].count = -1;
  1345. syntax.curly[syntax.curly_count].index = -1;
  1346. syntax.curly_count++;
  1347. return p + 1;
  1348. } else if(p[0] == '}') {
  1349. return parse_curly_close(p);
  1350. }
  1351. // Syntax-related processing
  1352. p2 = parse_syntax(p);
  1353. if(p2 != NULL)
  1354. return p2;
  1355. // attempt to process a variable assignment
  1356. p2 = parse_variable(p);
  1357. if( p2 != NULL )
  1358. {// variable assignment processed so leave the method
  1359. return parse_syntax_close(p2 + 1);
  1360. }
  1361. p = parse_callfunc(p,0,0);
  1362. p = skip_space(p);
  1363. if(parse_syntax_for_flag) {
  1364. if( *p != ')' )
  1365. disp_error_message("parse_line: expected ')'",p);
  1366. } else {
  1367. if( *p != ';' )
  1368. disp_error_message("parse_line: expected ';'",p);
  1369. }
  1370. //Binding decision for if(), for(), while()
  1371. p = parse_syntax_close(p+1);
  1372. return p;
  1373. }
  1374. // { ... } Closing process
  1375. const char* parse_curly_close(const char* p)
  1376. {
  1377. if(syntax.curly_count <= 0) {
  1378. disp_error_message("parse_curly_close: unexpected string",p);
  1379. return p + 1;
  1380. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1381. syntax.curly_count--;
  1382. //Close decision if, for , while
  1383. p = parse_syntax_close(p + 1);
  1384. return p;
  1385. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1386. //Closing switch()
  1387. int pos = syntax.curly_count-1;
  1388. char label[256];
  1389. int l;
  1390. // Remove temporary variables
  1391. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1392. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1393. parse_line(label);
  1394. syntax.curly_count--;
  1395. // Go to the end pointer unconditionally
  1396. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1397. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1398. parse_line(label);
  1399. syntax.curly_count--;
  1400. // You are here labeled
  1401. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1402. l=add_str(label);
  1403. set_label(l,script_pos, p);
  1404. if(syntax.curly[pos].flag) {
  1405. //Exists default
  1406. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1407. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1408. parse_line(label);
  1409. syntax.curly_count--;
  1410. }
  1411. // Label end
  1412. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1413. l=add_str(label);
  1414. set_label(l,script_pos, p);
  1415. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1416. syntax.curly_count--;
  1417. //Closing decision if, for , while
  1418. p = parse_syntax_close(p + 1);
  1419. return p;
  1420. } else {
  1421. disp_error_message("parse_curly_close: unexpected string",p);
  1422. return p + 1;
  1423. }
  1424. }
  1425. // Syntax-related processing
  1426. // break, case, continue, default, do, for, function,
  1427. // if, switch, while ? will handle this internally.
  1428. const char* parse_syntax(const char* p)
  1429. {
  1430. const char *p2 = skip_word(p);
  1431. switch(*p) {
  1432. case 'B':
  1433. case 'b':
  1434. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1435. // break Processing
  1436. char label[256];
  1437. int pos = syntax.curly_count - 1;
  1438. while(pos >= 0) {
  1439. if(syntax.curly[pos].type == TYPE_DO) {
  1440. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1441. break;
  1442. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1443. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1444. break;
  1445. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1446. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1447. break;
  1448. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1449. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1450. break;
  1451. }
  1452. pos--;
  1453. }
  1454. if(pos < 0) {
  1455. disp_error_message("parse_syntax: unexpected 'break'",p);
  1456. } else {
  1457. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1458. parse_line(label);
  1459. syntax.curly_count--;
  1460. }
  1461. p = skip_space(p2);
  1462. if(*p != ';')
  1463. disp_error_message("parse_syntax: expected ';'",p);
  1464. // Closing decision if, for , while
  1465. p = parse_syntax_close(p + 1);
  1466. return p;
  1467. }
  1468. break;
  1469. case 'c':
  1470. case 'C':
  1471. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1472. //Processing case
  1473. int pos = syntax.curly_count-1;
  1474. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1475. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1476. return p+1;
  1477. } else {
  1478. char label[256];
  1479. int l;
  1480. int64 v;
  1481. char *np;
  1482. if(syntax.curly[pos].count != 1) {
  1483. //Jump for FALLTHRU
  1484. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1485. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1486. parse_line(label);
  1487. syntax.curly_count--;
  1488. // You are here labeled
  1489. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1490. l=add_str(label);
  1491. set_label(l,script_pos, p);
  1492. }
  1493. //Decision statement switch
  1494. p = skip_space(p2);
  1495. if(p == p2) {
  1496. disp_error_message("parse_syntax: expected a space ' '",p);
  1497. }
  1498. // check whether case label is integer or not
  1499. if(is_number(p)) {
  1500. //Numeric value
  1501. v = (int)strtol(p,&np,0);
  1502. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1503. p++;
  1504. p = skip_word(p);
  1505. if(np != p)
  1506. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1507. } else {
  1508. //Check for constants
  1509. p2 = skip_word(p);
  1510. v = (int)(size_t) (p2-p); // length of word at p2
  1511. memcpy(label,p,static_cast<int>(v));
  1512. label[v]='\0';
  1513. if( !script_get_constant(label, &v) )
  1514. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1515. p = skip_word(p);
  1516. }
  1517. p = skip_space(p);
  1518. if(*p != ':')
  1519. disp_error_message("parse_syntax: expect ':'",p);
  1520. sprintf(label,"if(%" PRId64 " != $@__SW%x_VAL) goto __SW%x_%x;",
  1521. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1522. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1523. // Bad I do not parse twice
  1524. p2 = parse_line(label);
  1525. parse_line(p2);
  1526. syntax.curly_count--;
  1527. if(syntax.curly[pos].count != 1) {
  1528. // Label after the completion of FALLTHRU
  1529. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1530. l=add_str(label);
  1531. set_label(l,script_pos,p);
  1532. }
  1533. // check duplication of case label [Rayce]
  1534. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1535. disp_error_message("parse_syntax: dup 'case'",p);
  1536. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1537. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1538. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1539. parse_line(label);
  1540. syntax.curly_count--;
  1541. syntax.curly[pos].count++;
  1542. }
  1543. return p + 1;
  1544. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1545. // Processing continue
  1546. char label[256];
  1547. int pos = syntax.curly_count - 1;
  1548. while(pos >= 0) {
  1549. if(syntax.curly[pos].type == TYPE_DO) {
  1550. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1551. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1552. break;
  1553. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1554. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1555. break;
  1556. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1557. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1558. break;
  1559. }
  1560. pos--;
  1561. }
  1562. if(pos < 0) {
  1563. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1564. } else {
  1565. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1566. parse_line(label);
  1567. syntax.curly_count--;
  1568. }
  1569. p = skip_space(p2);
  1570. if(*p != ';')
  1571. disp_error_message("parse_syntax: expected ';'",p);
  1572. //Closing decision if, for , while
  1573. p = parse_syntax_close(p + 1);
  1574. return p;
  1575. }
  1576. break;
  1577. case 'd':
  1578. case 'D':
  1579. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1580. // Switch - default processing
  1581. int pos = syntax.curly_count-1;
  1582. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1583. disp_error_message("parse_syntax: unexpected 'default'",p);
  1584. } else if(syntax.curly[pos].flag) {
  1585. disp_error_message("parse_syntax: dup 'default'",p);
  1586. } else {
  1587. char label[256];
  1588. int l;
  1589. // Put the label location
  1590. p = skip_space(p2);
  1591. if(*p != ':') {
  1592. disp_error_message("parse_syntax: expected ':'",p);
  1593. }
  1594. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1595. l=add_str(label);
  1596. set_label(l,script_pos,p);
  1597. // Skip to the next link w/o condition
  1598. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1599. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1600. parse_line(label);
  1601. syntax.curly_count--;
  1602. // The default label
  1603. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1604. l=add_str(label);
  1605. set_label(l,script_pos,p);
  1606. syntax.curly[syntax.curly_count - 1].flag = 1;
  1607. syntax.curly[pos].count++;
  1608. }
  1609. return p + 1;
  1610. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1611. int l;
  1612. char label[256];
  1613. p=skip_space(p2);
  1614. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1615. syntax.curly[syntax.curly_count].count = 1;
  1616. syntax.curly[syntax.curly_count].index = syntax.index++;
  1617. syntax.curly[syntax.curly_count].flag = 0;
  1618. // Label of the (do) form here
  1619. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1620. l=add_str(label);
  1621. set_label(l,script_pos,p);
  1622. syntax.curly_count++;
  1623. return p;
  1624. }
  1625. break;
  1626. case 'f':
  1627. case 'F':
  1628. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1629. int l;
  1630. char label[256];
  1631. int pos = syntax.curly_count;
  1632. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1633. syntax.curly[syntax.curly_count].count = 1;
  1634. syntax.curly[syntax.curly_count].index = syntax.index++;
  1635. syntax.curly[syntax.curly_count].flag = 0;
  1636. syntax.curly_count++;
  1637. p=skip_space(p2);
  1638. if(*p != '(')
  1639. disp_error_message("parse_syntax: expected '('",p);
  1640. p++;
  1641. // Execute the initialization statement
  1642. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1643. p=parse_line(p);
  1644. syntax.curly_count--;
  1645. // Form the start of label decision
  1646. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1647. l=add_str(label);
  1648. set_label(l,script_pos,p);
  1649. p=skip_space(p);
  1650. if(*p == ';') {
  1651. // For (; Because the pattern of always true ;)
  1652. ;
  1653. } else {
  1654. // Skip to the end point if the condition is false
  1655. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1656. add_scriptl(add_str("jump_zero"));
  1657. add_scriptc(C_ARG);
  1658. p=parse_expr(p);
  1659. p=skip_space(p);
  1660. add_scriptl(add_str(label));
  1661. add_scriptc(C_FUNC);
  1662. }
  1663. if(*p != ';')
  1664. disp_error_message("parse_syntax: expected ';'",p);
  1665. p++;
  1666. // Skip to the beginning of the loop
  1667. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1668. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1669. parse_line(label);
  1670. syntax.curly_count--;
  1671. // Labels to form the next loop
  1672. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1673. l=add_str(label);
  1674. set_label(l,script_pos,p);
  1675. // Process the next time you enter the loop
  1676. // A ')' last for; flag to be treated as'
  1677. parse_syntax_for_flag = 1;
  1678. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1679. p=parse_line(p);
  1680. syntax.curly_count--;
  1681. parse_syntax_for_flag = 0;
  1682. // Skip to the determination process conditions
  1683. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1684. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1685. parse_line(label);
  1686. syntax.curly_count--;
  1687. // Loop start labeling
  1688. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1689. l=add_str(label);
  1690. set_label(l,script_pos,p);
  1691. return p;
  1692. }
  1693. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1694. {// internal script function
  1695. const char *func_name;
  1696. func_name = skip_space(p2);
  1697. p = skip_word(func_name);
  1698. if( p == func_name )
  1699. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1700. p2 = skip_space(p);
  1701. if( *p2 == ';' )
  1702. {// function <name> ;
  1703. // function declaration - just register the name
  1704. int l;
  1705. l = add_word(func_name);
  1706. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1707. str_data[l].type = C_USERFUNC;
  1708. else if( str_data[l].type == C_USERFUNC )
  1709. ; // already registered
  1710. else
  1711. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1712. // Close condition of if, for, while
  1713. p = parse_syntax_close(p2 + 1);
  1714. return p;
  1715. }
  1716. else if(*p2 == '{')
  1717. {// function <name> <line/block of code>
  1718. char label[256];
  1719. int l;
  1720. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1721. syntax.curly[syntax.curly_count].count = 1;
  1722. syntax.curly[syntax.curly_count].index = syntax.index++;
  1723. syntax.curly[syntax.curly_count].flag = 0;
  1724. ++syntax.curly_count;
  1725. // Jump over the function code
  1726. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1727. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1728. ++syntax.curly_count;
  1729. parse_line(label);
  1730. --syntax.curly_count;
  1731. // Set the position of the function (label)
  1732. l=add_word(func_name);
  1733. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1734. {
  1735. str_data[l].type = C_USERFUNC;
  1736. set_label(l, script_pos, p);
  1737. if( parse_options&SCRIPT_USE_LABEL_DB )
  1738. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1739. }
  1740. else
  1741. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1742. return skip_space(p);
  1743. }
  1744. else
  1745. {
  1746. disp_error_message("expect ';' or '{' at function syntax",p);
  1747. }
  1748. }
  1749. break;
  1750. case 'i':
  1751. case 'I':
  1752. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1753. // If process
  1754. char label[256];
  1755. p=skip_space(p2);
  1756. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1757. disp_error_message("need '('",p);
  1758. }
  1759. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1760. syntax.curly[syntax.curly_count].count = 1;
  1761. syntax.curly[syntax.curly_count].index = syntax.index++;
  1762. syntax.curly[syntax.curly_count].flag = 0;
  1763. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1764. syntax.curly_count++;
  1765. add_scriptl(add_str("jump_zero"));
  1766. add_scriptc(C_ARG);
  1767. p=parse_expr(p);
  1768. p=skip_space(p);
  1769. add_scriptl(add_str(label));
  1770. add_scriptc(C_FUNC);
  1771. return p;
  1772. }
  1773. break;
  1774. case 's':
  1775. case 'S':
  1776. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1777. // Processing of switch ()
  1778. char label[256];
  1779. p=skip_space(p2);
  1780. if(*p != '(') {
  1781. disp_error_message("need '('",p);
  1782. }
  1783. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1784. syntax.curly[syntax.curly_count].count = 1;
  1785. syntax.curly[syntax.curly_count].index = syntax.index++;
  1786. syntax.curly[syntax.curly_count].flag = 0;
  1787. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1788. syntax.curly_count++;
  1789. add_scriptl(add_str("set"));
  1790. add_scriptc(C_ARG);
  1791. add_scriptl(add_str(label));
  1792. p=parse_expr(p);
  1793. p=skip_space(p);
  1794. if(*p != '{') {
  1795. disp_error_message("parse_syntax: expected '{'",p);
  1796. }
  1797. add_scriptc(C_FUNC);
  1798. return p + 1;
  1799. }
  1800. break;
  1801. case 'w':
  1802. case 'W':
  1803. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1804. int l;
  1805. char label[256];
  1806. p=skip_space(p2);
  1807. if(*p != '(') {
  1808. disp_error_message("need '('",p);
  1809. }
  1810. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1811. syntax.curly[syntax.curly_count].count = 1;
  1812. syntax.curly[syntax.curly_count].index = syntax.index++;
  1813. syntax.curly[syntax.curly_count].flag = 0;
  1814. // Form the start of label decision
  1815. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1816. l=add_str(label);
  1817. set_label(l,script_pos,p);
  1818. // Skip to the end point if the condition is false
  1819. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1820. syntax.curly_count++;
  1821. add_scriptl(add_str("jump_zero"));
  1822. add_scriptc(C_ARG);
  1823. p=parse_expr(p);
  1824. p=skip_space(p);
  1825. add_scriptl(add_str(label));
  1826. add_scriptc(C_FUNC);
  1827. return p;
  1828. }
  1829. break;
  1830. }
  1831. return NULL;
  1832. }
  1833. const char* parse_syntax_close(const char *p) {
  1834. // If (...) for (...) hoge (); as to make sure closed closed once again
  1835. int flag;
  1836. do {
  1837. p = parse_syntax_close_sub(p,&flag);
  1838. } while(flag);
  1839. return p;
  1840. }
  1841. // Close judgment if, for, while, of do
  1842. // flag == 1 : closed
  1843. // flag == 0 : not closed
  1844. const char* parse_syntax_close_sub(const char* p,int* flag)
  1845. {
  1846. char label[256];
  1847. int pos = syntax.curly_count - 1;
  1848. int l;
  1849. *flag = 1;
  1850. if(syntax.curly_count <= 0) {
  1851. *flag = 0;
  1852. return p;
  1853. } else if(syntax.curly[pos].type == TYPE_IF) {
  1854. const char *bp = p;
  1855. const char *p2;
  1856. // if-block and else-block end is a new line
  1857. parse_nextline(false, p);
  1858. // Skip to the last location if
  1859. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1860. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1861. parse_line(label);
  1862. syntax.curly_count--;
  1863. // Put the label of the location
  1864. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1865. l=add_str(label);
  1866. set_label(l,script_pos,p);
  1867. syntax.curly[pos].count++;
  1868. p = skip_space(p);
  1869. p2 = skip_word(p);
  1870. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1871. // else or else - if
  1872. p = skip_space(p2);
  1873. p2 = skip_word(p);
  1874. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1875. // else - if
  1876. p=skip_space(p2);
  1877. if(*p != '(') {
  1878. disp_error_message("need '('",p);
  1879. }
  1880. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1881. add_scriptl(add_str("jump_zero"));
  1882. add_scriptc(C_ARG);
  1883. p=parse_expr(p);
  1884. p=skip_space(p);
  1885. add_scriptl(add_str(label));
  1886. add_scriptc(C_FUNC);
  1887. *flag = 0;
  1888. return p;
  1889. } else {
  1890. // else
  1891. if(!syntax.curly[pos].flag) {
  1892. syntax.curly[pos].flag = 1;
  1893. *flag = 0;
  1894. return p;
  1895. }
  1896. }
  1897. }
  1898. // Close if
  1899. syntax.curly_count--;
  1900. // Put the label of the final location
  1901. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1902. l=add_str(label);
  1903. set_label(l,script_pos,p);
  1904. if(syntax.curly[pos].flag == 1) {
  1905. // Because the position of the pointer is the same if not else for this
  1906. return bp;
  1907. }
  1908. return p;
  1909. } else if(syntax.curly[pos].type == TYPE_DO) {
  1910. int l2;
  1911. char label2[256];
  1912. const char *p2;
  1913. if(syntax.curly[pos].flag) {
  1914. // (Come here continue) to form the label here
  1915. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1916. l2=add_str(label2);
  1917. set_label(l2,script_pos,p);
  1918. }
  1919. // Skip to the end point if the condition is false
  1920. p = skip_space(p);
  1921. p2 = skip_word(p);
  1922. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1923. disp_error_message("parse_syntax: expected 'while'",p);
  1924. p = skip_space(p2);
  1925. if(*p != '(') {
  1926. disp_error_message("need '('",p);
  1927. }
  1928. // do-block end is a new line
  1929. parse_nextline(false, p);
  1930. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1931. add_scriptl(add_str("jump_zero"));
  1932. add_scriptc(C_ARG);
  1933. p=parse_expr(p);
  1934. p=skip_space(p);
  1935. add_scriptl(add_str(label2));
  1936. add_scriptc(C_FUNC);
  1937. // Skip to the starting point
  1938. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1939. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1940. parse_line(label2);
  1941. syntax.curly_count--;
  1942. // Form label of the end point conditions
  1943. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1944. l2=add_str(label2);
  1945. set_label(l2,script_pos,p);
  1946. p = skip_space(p);
  1947. if(*p != ';') {
  1948. disp_error_message("parse_syntax: expected ';'",p);
  1949. return p+1;
  1950. }
  1951. p++;
  1952. syntax.curly_count--;
  1953. return p;
  1954. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1955. // for-block end is a new line
  1956. parse_nextline(false, p);
  1957. // Skip to the next loop
  1958. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1959. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1960. parse_line(label);
  1961. syntax.curly_count--;
  1962. // End for labeling
  1963. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1964. l=add_str(label);
  1965. set_label(l,script_pos,p);
  1966. syntax.curly_count--;
  1967. return p;
  1968. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1969. // while-block end is a new line
  1970. parse_nextline(false, p);
  1971. // Skip to the decision while
  1972. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1973. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1974. parse_line(label);
  1975. syntax.curly_count--;
  1976. // End while labeling
  1977. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1978. l=add_str(label);
  1979. set_label(l,script_pos,p);
  1980. syntax.curly_count--;
  1981. return p;
  1982. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1983. int pos2 = syntax.curly_count-1;
  1984. char label2[256];
  1985. int l2;
  1986. // Back
  1987. sprintf(label2,"return;");
  1988. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1989. parse_line(label2);
  1990. syntax.curly_count--;
  1991. // Put the label of the location
  1992. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1993. l2=add_str(label2);
  1994. set_label(l2,script_pos,p);
  1995. syntax.curly_count--;
  1996. return p;
  1997. } else {
  1998. *flag = 0;
  1999. return p;
  2000. }
  2001. }
  2002. /*==========================================
  2003. * Added built-in functions
  2004. *------------------------------------------*/
  2005. static void add_buildin_func(void)
  2006. {
  2007. int i;
  2008. for( i = 0; buildin_func[i].func; i++ )
  2009. {
  2010. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2011. // 'v' - value (either string or int or reference)
  2012. // 's' - string
  2013. // 'i' - int
  2014. // 'r' - reference (of a variable)
  2015. // 'l' - label
  2016. // '?' - one optional parameter
  2017. // '*' - unknown number of optional parameters
  2018. const char* p = buildin_func[i].arg;
  2019. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2020. while( *p == '?' ) ++p;
  2021. if( *p == '*' ) ++p;
  2022. if( *p != 0){
  2023. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2024. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2025. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2026. } else {
  2027. int n = add_str(buildin_func[i].name);
  2028. str_data[n].type = C_FUNC;
  2029. str_data[n].val = i;
  2030. str_data[n].func = buildin_func[i].func;
  2031. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  2032. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2033. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2034. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2035. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2036. }
  2037. }
  2038. }
  2039. /**
  2040. * String comparison with a char array to a script constant
  2041. * @param prefix: Char array to compare
  2042. * @param value: Script constant
  2043. * @return Constant name as char array or NULL otherwise
  2044. */
  2045. const char* script_get_constant_str( const char* prefix, int64 value ){
  2046. const char* name;
  2047. for(int i = 0; i < str_data_size; i++ ){
  2048. // Check if it is a constant
  2049. if( str_data[i].type != C_INT ){
  2050. continue;
  2051. }
  2052. // Check if the value matches
  2053. if( str_data[i].val != value ){
  2054. continue;
  2055. }
  2056. // Look up the actual string
  2057. name = get_str(i);
  2058. // Compare the prefix
  2059. if( !strncasecmp( name, prefix, strlen(prefix) ) ){
  2060. // We found a match
  2061. return name;
  2062. }
  2063. }
  2064. return NULL;
  2065. }
  2066. /// Retrieves the value of a constant parameter.
  2067. bool script_get_parameter(const char* name, int64* value)
  2068. {
  2069. int n = search_str(name);
  2070. if (n == -1 || str_data[n].type != C_PARAM)
  2071. {// not found or not a parameter
  2072. return false;
  2073. }
  2074. value[0] = str_data[n].val;
  2075. return true;
  2076. }
  2077. /// Retrieves the value of a constant.
  2078. bool script_get_constant(const char* name, int64* value)
  2079. {
  2080. int n = search_str(name);
  2081. if( n == -1 || str_data[n].type != C_INT )
  2082. {// not found or not a constant
  2083. return false;
  2084. }
  2085. value[0] = str_data[n].val;
  2086. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2087. if( str_data[n].deprecated ){
  2088. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2089. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2090. if (str_data[n].name)
  2091. ShowWarning( "Please use '%s' instead!\n", str_data[n].name );
  2092. }
  2093. #endif
  2094. return true;
  2095. }
  2096. /// Creates new constant or parameter with given value.
  2097. void script_set_constant_(const char* name, int64 value, const char* constant_name, bool isparameter, bool deprecated)
  2098. {
  2099. int n = add_str(name);
  2100. if( str_data[n].type == C_NOP )
  2101. {// new
  2102. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2103. str_data[n].val = value;
  2104. str_data[n].deprecated = deprecated;
  2105. str_data[n].name = constant_name;
  2106. }
  2107. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2108. {// existing parameter or constant
  2109. 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);
  2110. }
  2111. else
  2112. {// existing name
  2113. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2114. }
  2115. }
  2116. const std::string ConstantDatabase::getDefaultLocation(){
  2117. return std::string(db_path) + "/const.yml";
  2118. }
  2119. uint64 ConstantDatabase::parseBodyNode( const ryml::NodeRef& node ) {
  2120. std::string constant_name;
  2121. if (!this->asString( node, "Name", constant_name ))
  2122. return 0;
  2123. char name[1024];
  2124. if (sscanf(constant_name.c_str(), "%1023[A-Za-z0-9/_]", name) != 1) {
  2125. this->invalidWarning( node["Name"], "Invalid constant definition \"%s\", skipping.\n", constant_name.c_str() );
  2126. return 0;
  2127. }
  2128. int64 val;
  2129. if (!this->asInt64( node, "Value", val ))
  2130. return 0;
  2131. bool type = false;
  2132. if (this->nodeExists(node, "Parameter") && !this->asBool( node, "Parameter", type ))
  2133. return 0;
  2134. script_set_constant(name, val, type, false);
  2135. return 1;
  2136. }
  2137. ConstantDatabase constant_db;
  2138. /**
  2139. * Sets source-end constants for NPC scripts to access.
  2140. **/
  2141. void script_hardcoded_constants(void) {
  2142. #include "script_constants.hpp"
  2143. }
  2144. /*==========================================
  2145. * Display emplacement line of script
  2146. *------------------------------------------*/
  2147. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2148. {
  2149. int i;
  2150. if( p == NULL || !p[0] ) return NULL;
  2151. if( line < 0 )
  2152. StringBuf_Printf(buf, "*% 5d : ", -line);
  2153. else
  2154. StringBuf_Printf(buf, " % 5d : ", line);
  2155. for(i=0;p[i] && p[i] != '\n';i++){
  2156. if(p + i != mark)
  2157. StringBuf_Printf(buf, "%c", p[i]);
  2158. else
  2159. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2160. }
  2161. StringBuf_AppendStr(buf, "\n");
  2162. return p+i+(p[i] == '\n' ? 1 : 0);
  2163. }
  2164. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2165. // Find the line where the error occurred
  2166. int j;
  2167. int line = start_line;
  2168. const char *p;
  2169. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2170. for(p=src;p && *p;line++){
  2171. const char *lineend=strchr(p,'\n');
  2172. if(lineend==NULL || error_pos_cur<lineend){
  2173. break;
  2174. }
  2175. for( j = 0; j < 4; j++ ) {
  2176. linestart[j] = linestart[j+1];
  2177. }
  2178. linestart[4] = p;
  2179. p=lineend+1;
  2180. }
  2181. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2182. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2183. for(j = 0; j < 5; j++ ) {
  2184. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2185. }
  2186. p = script_print_line(buf, p, error_pos_cur, -line);
  2187. for(j = 0; j < 5; j++) {
  2188. p = script_print_line(buf, p, NULL, line + j + 1 );
  2189. }
  2190. }
  2191. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2192. StringBuf buf;
  2193. StringBuf_Init(&buf);
  2194. StringBuf_AppendStr(&buf, "\a\n");
  2195. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2196. ShowError("%s", StringBuf_Value(&buf));
  2197. StringBuf_Destroy(&buf);
  2198. }
  2199. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2200. StringBuf buf;
  2201. StringBuf_Init(&buf);
  2202. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2203. ShowWarning("%s", StringBuf_Value(&buf));
  2204. StringBuf_Destroy(&buf);
  2205. }
  2206. /*==========================================
  2207. * Analysis of the script
  2208. *------------------------------------------*/
  2209. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2210. {
  2211. const char *p,*tmpp;
  2212. int i;
  2213. struct script_code* code = NULL;
  2214. char end;
  2215. bool unresolved_names = false;
  2216. parser_current_src = src;
  2217. parser_current_file = file;
  2218. parser_current_line = line;
  2219. if( src == NULL )
  2220. return NULL;// empty script
  2221. memset(&syntax,0,sizeof(syntax));
  2222. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2223. script_pos=0;
  2224. script_size=SCRIPT_BLOCK_SIZE;
  2225. parse_nextline(true, NULL);
  2226. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2227. if( options&SCRIPT_USE_LABEL_DB )
  2228. db_clear(scriptlabel_db);
  2229. parse_options = options;
  2230. if( setjmp( error_jump ) != 0 ) {
  2231. //Restore program state when script has problems. [from jA]
  2232. int j;
  2233. const int size = ARRAYLENGTH(syntax.curly);
  2234. if( error_report )
  2235. script_error(src,file,line,error_msg,error_pos);
  2236. aFree( error_msg );
  2237. aFree( script_buf );
  2238. script_pos = 0;
  2239. script_size = 0;
  2240. script_buf = NULL;
  2241. for(j=LABEL_START;j<str_num;j++)
  2242. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2243. for(j=0; j<size; j++)
  2244. linkdb_final(&syntax.curly[j].case_label);
  2245. return NULL;
  2246. }
  2247. parse_syntax_for_flag=0;
  2248. p=src;
  2249. p=skip_space(p);
  2250. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2251. {// does not require brackets around the script
  2252. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2253. {// empty script and can return NULL
  2254. aFree( script_buf );
  2255. script_pos = 0;
  2256. script_size = 0;
  2257. script_buf = NULL;
  2258. return NULL;
  2259. }
  2260. end = '\0';
  2261. }
  2262. else
  2263. {// requires brackets around the script
  2264. if( *p != '{' )
  2265. disp_error_message("not found '{'",p);
  2266. p = skip_space(p+1);
  2267. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2268. {// empty script and can return NULL
  2269. aFree( script_buf );
  2270. script_pos = 0;
  2271. script_size = 0;
  2272. script_buf = NULL;
  2273. return NULL;
  2274. }
  2275. end = '}';
  2276. }
  2277. // clear references of labels, variables and internal functions
  2278. for(i=LABEL_START;i<str_num;i++){
  2279. if(
  2280. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2281. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2282. ){
  2283. str_data[i].type=C_NOP;
  2284. str_data[i].backpatch=-1;
  2285. str_data[i].label=-1;
  2286. }
  2287. }
  2288. while( syntax.curly_count != 0 || *p != end )
  2289. {
  2290. if( *p == '\0' )
  2291. disp_error_message("unexpected end of script",p);
  2292. // Special handling only label
  2293. tmpp=skip_space(skip_word(p));
  2294. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2295. i=add_word(p);
  2296. set_label(i,script_pos,p);
  2297. if( parse_options&SCRIPT_USE_LABEL_DB )
  2298. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2299. p=tmpp+1;
  2300. p=skip_space(p);
  2301. continue;
  2302. }
  2303. // All other lumped
  2304. p=parse_line(p);
  2305. p=skip_space(p);
  2306. parse_nextline(false, p);
  2307. }
  2308. add_scriptc(C_NOP);
  2309. // trim code to size
  2310. script_size = script_pos;
  2311. RECREATE(script_buf,unsigned char,script_pos);
  2312. // default unknown references to variables
  2313. for(i=LABEL_START;i<str_num;i++){
  2314. if(str_data[i].type==C_NOP){
  2315. int j;
  2316. str_data[i].type=C_NAME;
  2317. str_data[i].label=i;
  2318. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2319. int next=GETVALUE(script_buf,j);
  2320. SETVALUE(script_buf,j,i);
  2321. j=next;
  2322. }
  2323. }
  2324. else if( str_data[i].type == C_USERFUNC )
  2325. {// 'function name;' without follow-up code
  2326. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2327. unresolved_names = true;
  2328. }
  2329. }
  2330. if( unresolved_names )
  2331. {
  2332. disp_error_message("parse_script: unresolved function references", p);
  2333. }
  2334. #ifdef DEBUG_DISP
  2335. for(i=0;i<script_pos;i++){
  2336. if((i&15)==0) ShowMessage("%04x : ",i);
  2337. ShowMessage("%02x ",script_buf[i]);
  2338. if((i&15)==15) ShowMessage("\n");
  2339. }
  2340. ShowMessage("\n");
  2341. #endif
  2342. #ifdef DEBUG_DISASM
  2343. {
  2344. int i = 0,j;
  2345. while(i < script_pos) {
  2346. c_op op = get_com(script_buf,&i);
  2347. ShowMessage("%06x %s", i, script_op2name(op));
  2348. j = i;
  2349. switch(op) {
  2350. case C_INT:
  2351. ShowMessage(" %d", get_num(script_buf,&i));
  2352. break;
  2353. case C_POS:
  2354. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2355. i += 3;
  2356. break;
  2357. case C_NAME:
  2358. j = (*(int*)(script_buf+i)&0xffffff);
  2359. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2360. i += 3;
  2361. break;
  2362. case C_STR:
  2363. j = strlen(script_buf + i);
  2364. ShowMessage(" %s", script_buf + i);
  2365. i += j+1;
  2366. break;
  2367. }
  2368. ShowMessage(CL_CLL"\n");
  2369. }
  2370. }
  2371. #endif
  2372. CREATE(code,struct script_code,1);
  2373. code->script_buf = script_buf;
  2374. code->script_size = script_size;
  2375. code->local.vars = NULL;
  2376. code->local.arrays = NULL;
  2377. return code;
  2378. }
  2379. /// Returns the player attached to this script, identified by the rid.
  2380. /// If there is no player attached, the script is terminated.
  2381. static bool script_rid2sd_( struct script_state *st, map_session_data** sd, const char *func ){
  2382. *sd = map_id2sd( st->rid );
  2383. if( *sd ){
  2384. return true;
  2385. }else{
  2386. ShowError("%s: fatal error ! player not attached!\n",func);
  2387. script_reportfunc(st);
  2388. script_reportsrc(st);
  2389. st->state = END;
  2390. return false;
  2391. }
  2392. }
  2393. /**
  2394. * Dereferences a variable/constant, replacing it with a copy of the value.
  2395. * @param st Script state
  2396. * @param data Variable/constant
  2397. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2398. */
  2399. struct script_data *get_val_(struct script_state* st, struct script_data* data, map_session_data *sd)
  2400. {
  2401. const char* name;
  2402. char prefix;
  2403. char postfix;
  2404. if( !data_isreference(data) )
  2405. return data;// not a variable/constant
  2406. name = reference_getname(data);
  2407. prefix = name[0];
  2408. postfix = name[strlen(name) - 1];
  2409. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2410. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2411. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2412. if( postfix == '$' ) {// string variable
  2413. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2414. data->type = C_CONSTSTR;
  2415. data->u.str = const_cast<char *>("");
  2416. } else {// integer variable
  2417. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2418. data->type = C_INT;
  2419. data->u.num = 0;
  2420. }
  2421. return data;
  2422. }
  2423. }
  2424. if( postfix == '$' ) {// string variable
  2425. switch( prefix ) {
  2426. case '@':
  2427. data->u.str = pc_readregstr(sd, data->u.num);
  2428. break;
  2429. case '$':
  2430. data->u.str = mapreg_readregstr(data->u.num);
  2431. break;
  2432. case '#':
  2433. if( name[1] == '#' )
  2434. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2435. else
  2436. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2437. break;
  2438. case '.':
  2439. {
  2440. struct DBMap* n = data->ref ?
  2441. data->ref->vars : name[1] == '@' ?
  2442. st->stack->scope.vars : // instance/scope variable
  2443. st->script->local.vars; // npc variable
  2444. if( n )
  2445. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2446. else
  2447. data->u.str = NULL;
  2448. }
  2449. break;
  2450. case '\'':
  2451. {
  2452. struct DBMap* n = nullptr;
  2453. if (data->ref)
  2454. n = data->ref->vars;
  2455. else {
  2456. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2457. if (idata)
  2458. n = idata->regs.vars;
  2459. }
  2460. if (n)
  2461. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2462. else {
  2463. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2464. data->u.str = NULL;
  2465. }
  2466. break;
  2467. }
  2468. default:
  2469. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2470. break;
  2471. }
  2472. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2473. data->type = C_CONSTSTR;
  2474. data->u.str = const_cast<char *>("");
  2475. } else {// duplicate string
  2476. data->type = C_STR;
  2477. data->u.str = aStrdup(data->u.str);
  2478. }
  2479. } else {// integer variable
  2480. data->type = C_INT;
  2481. if( reference_toconstant(data) ) {
  2482. data->u.num = reference_getconstant(data);
  2483. } else if( reference_toparam(data) ) {
  2484. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2485. } else
  2486. switch( prefix ) {
  2487. case '@':
  2488. data->u.num = pc_readreg(sd, data->u.num);
  2489. break;
  2490. case '$':
  2491. data->u.num = mapreg_readreg(data->u.num);
  2492. break;
  2493. case '#':
  2494. if( name[1] == '#' )
  2495. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2496. else
  2497. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2498. break;
  2499. case '.':
  2500. {
  2501. struct DBMap* n = data->ref ?
  2502. data->ref->vars : name[1] == '@' ?
  2503. st->stack->scope.vars : // instance/scope variable
  2504. st->script->local.vars; // npc variable
  2505. if( n )
  2506. data->u.num = i64db_i64get(n,reference_getuid(data));
  2507. else
  2508. data->u.num = 0;
  2509. }
  2510. break;
  2511. case '\'':
  2512. {
  2513. struct DBMap* n = nullptr;
  2514. if (data->ref)
  2515. n = data->ref->vars;
  2516. else {
  2517. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2518. if (idata)
  2519. n = idata->regs.vars;
  2520. }
  2521. if (n)
  2522. data->u.num = i64db_i64get(n,reference_getuid(data));
  2523. else {
  2524. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2525. data->u.num = 0;
  2526. }
  2527. break;
  2528. }
  2529. default:
  2530. data->u.num = pc_readglobalreg(sd, data->u.num);
  2531. break;
  2532. }
  2533. }
  2534. data->ref = NULL;
  2535. return data;
  2536. }
  2537. struct script_data *get_val(struct script_state* st, struct script_data* data)
  2538. {
  2539. return get_val_(st,data,NULL);
  2540. }
  2541. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2542. const char* get_val2_str( struct script_state* st, int64 uid, struct reg_db* ref ){
  2543. push_val2( st->stack, C_NAME, uid, ref );
  2544. struct script_data* data = script_getdatatop( st, -1 );
  2545. get_val( st, data );
  2546. const char* value = "";
  2547. if( data->type == C_INT ){
  2548. ShowError( "get_val2_num: Invalid call. Variable %s is a numeric type.\n", reference_getname( data ) );
  2549. }else{
  2550. value = data->u.str;
  2551. }
  2552. // Do NOT remove the value from stack here, the pointer is returned here and will be used by the caller [Lemongrass]
  2553. // script_removetop( st, -1, 0 );
  2554. return value;
  2555. }
  2556. int64 get_val2_num( struct script_state* st, int64 uid, struct reg_db* ref ){
  2557. push_val2( st->stack, C_NAME, uid, ref );
  2558. struct script_data* data = script_getdatatop( st, -1 );
  2559. get_val( st, data );
  2560. int64 value = 0;
  2561. if( data->type == C_INT ){
  2562. value = data->u.num;
  2563. }else{
  2564. ShowError( "get_val2_num: Invalid call. Variable %s is not a numeric type.\n", reference_getname( data ) );
  2565. }
  2566. script_removetop( st, -1, 0 );
  2567. return value;
  2568. }
  2569. /**
  2570. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2571. * Will be gone as soon as undefined var feature is implemented
  2572. **/
  2573. void script_array_ensure_zero(struct script_state *st, map_session_data *sd, int64 uid, struct reg_db *ref)
  2574. {
  2575. const char *name = get_str(script_getvarid(uid));
  2576. // is here st can be null pointer and st->rid is wrong?
  2577. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2578. bool insert = false;
  2579. if (sd && !st) {
  2580. // when sd comes, st isn't available
  2581. insert = true;
  2582. } else {
  2583. if( is_string_variable(name) ) {
  2584. const char* str = get_val2_str( st, uid, ref );
  2585. if( str && *str )
  2586. insert = true;
  2587. // Remove stack entry from get_val2_str
  2588. script_removetop( st, -1, 0 );
  2589. } else {
  2590. int64 num = get_val2_num( st, uid, ref );
  2591. if( num )
  2592. insert = true;
  2593. }
  2594. }
  2595. if (src && src->arrays) {
  2596. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2597. if (sa) {
  2598. unsigned int i;
  2599. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2600. if( i != sa->size ) {
  2601. if( !insert )
  2602. script_array_remove_member(src,sa,i);
  2603. return;
  2604. }
  2605. script_array_add_member(sa,0);
  2606. } else if (insert) {
  2607. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2608. }
  2609. }
  2610. }
  2611. /**
  2612. * Returns array size by ID
  2613. **/
  2614. unsigned int script_array_size(struct script_state *st, map_session_data *sd, const char *name, struct reg_db *ref)
  2615. {
  2616. struct script_array *sa = NULL;
  2617. struct reg_db *src = script_array_src(st, sd, name, ref);
  2618. if (src && src->arrays)
  2619. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2620. return sa ? sa->size : 0;
  2621. }
  2622. /**
  2623. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2624. **/
  2625. unsigned int script_array_highest_key(struct script_state *st, map_session_data *sd, const char *name, struct reg_db *ref)
  2626. {
  2627. struct script_array *sa = NULL;
  2628. struct reg_db *src = script_array_src(st, sd, name, ref);
  2629. if (src && src->arrays) {
  2630. int key = add_word(name);
  2631. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2632. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2633. unsigned int i, highest_key = 0;
  2634. for(i = 0; i < sa->size; i++) {
  2635. if( sa->members[i] > highest_key )
  2636. highest_key = sa->members[i];
  2637. }
  2638. return sa->size ? highest_key + 1 : 0;
  2639. }
  2640. }
  2641. return SCRIPT_CMD_SUCCESS;
  2642. }
  2643. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2644. {
  2645. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2646. aFree(sa->members);
  2647. ers_free(array_ers, sa);
  2648. return SCRIPT_CMD_SUCCESS;
  2649. }
  2650. /**
  2651. * Clears script_array and removes it from script->array_db
  2652. **/
  2653. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2654. {
  2655. aFree(sa->members);
  2656. idb_remove(src->arrays, sa->id);
  2657. ers_free(array_ers, sa);
  2658. }
  2659. /**
  2660. * Removes a member from a script_array list
  2661. *
  2662. * @param idx the index of the member in script_array struct list, not of the actual array member
  2663. **/
  2664. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2665. {
  2666. unsigned int i, cursor;
  2667. // it's the only member left, no need to do anything other than delete the array data
  2668. if( sa->size == 1 ) {
  2669. script_array_delete(src,sa);
  2670. return;
  2671. }
  2672. sa->members[idx] = UINT_MAX;
  2673. for(i = 0, cursor = 0; i < sa->size; i++) {
  2674. if( sa->members[i] == UINT_MAX )
  2675. continue;
  2676. if( i != cursor )
  2677. sa->members[cursor] = sa->members[i];
  2678. cursor++;
  2679. }
  2680. sa->size = cursor;
  2681. }
  2682. /**
  2683. * Appends a new array index to the list in script_array
  2684. *
  2685. * @param idx the index of the array member being inserted
  2686. **/
  2687. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2688. {
  2689. RECREATE(sa->members, unsigned int, ++sa->size);
  2690. sa->members[sa->size - 1] = idx;
  2691. }
  2692. /**
  2693. * Obtains the source of the array database for this type and scenario
  2694. * Initializes such database when not yet initialized.
  2695. **/
  2696. struct reg_db *script_array_src(struct script_state *st, map_session_data *sd, const char *name, struct reg_db *ref)
  2697. {
  2698. struct reg_db *src = NULL;
  2699. switch( name[0] ) {
  2700. // from player
  2701. default: // char reg
  2702. case '@': // temp char reg
  2703. case '#': // account reg
  2704. src = &sd->regs;
  2705. break;
  2706. case '$': // map reg
  2707. src = &regs;
  2708. break;
  2709. case '.': // npc/script
  2710. if (ref)
  2711. src = ref;
  2712. else
  2713. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2714. break;
  2715. case '\'': // instance
  2716. {
  2717. if (ref)
  2718. src = ref;
  2719. else {
  2720. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2721. if (idata)
  2722. src = &idata->regs;
  2723. }
  2724. break;
  2725. }
  2726. }
  2727. if( src ) {
  2728. if( !src->arrays )
  2729. src->arrays = idb_alloc(DB_OPT_BASE);
  2730. return src;
  2731. }
  2732. return NULL;
  2733. }
  2734. /**
  2735. * Processes a array member modification, and update data accordingly
  2736. *
  2737. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2738. * @param num[in] Variable ID
  2739. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2740. **/
  2741. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2742. {
  2743. struct script_array *sa = NULL;
  2744. int id = script_getvarid(num);
  2745. unsigned int index = script_getvaridx(num);
  2746. if (!src->arrays) {
  2747. src->arrays = idb_alloc(DB_OPT_BASE);
  2748. } else {
  2749. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2750. }
  2751. if( sa ) {
  2752. unsigned int i;
  2753. // search
  2754. for(i = 0; i < sa->size; i++) {
  2755. if( sa->members[i] == index )
  2756. break;
  2757. }
  2758. // if existent
  2759. if( i != sa->size ) {
  2760. // if empty, we gotta remove it
  2761. if( empty ) {
  2762. script_array_remove_member(src, sa, i);
  2763. }
  2764. } else if( !empty ) { /* new entry */
  2765. script_array_add_member(sa,index);
  2766. // we do nothing if its empty, no point in modifying array data for a new empty member
  2767. }
  2768. } else if ( !empty ) { // we only move to create if not empty
  2769. sa = ers_alloc(array_ers, struct script_array);
  2770. sa->id = id;
  2771. sa->members = NULL;
  2772. sa->size = 0;
  2773. script_array_add_member(sa,index);
  2774. idb_put(src->arrays, id, sa);
  2775. }
  2776. }
  2777. /**
  2778. * Stores the value of a script variable
  2779. *
  2780. * @param st: current script state.
  2781. * @param sd: current character data.
  2782. * @param num: variable identifier.
  2783. * @param name: variable name.
  2784. * @param value: new value.
  2785. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2786. * @return: 0 failure, 1 success.
  2787. *
  2788. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2789. *------------------------------------------*/
  2790. bool set_reg_str( struct script_state* st, map_session_data* sd, int64 num, const char* name, const char* value, struct reg_db *ref ){
  2791. char prefix = name[0];
  2792. size_t vlen = 0;
  2793. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2794. 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 );
  2795. return false;
  2796. }
  2797. if( !is_string_variable( name ) ){
  2798. // integer variable
  2799. return false;
  2800. }
  2801. switch( prefix ){
  2802. case '@':
  2803. pc_setregstr( sd, num, value );
  2804. return true;
  2805. case '$':
  2806. return mapreg_setregstr( num, value );
  2807. case '#':
  2808. return ( name[1] == '#' ) ? pc_setaccountreg2str( sd, num, value ) : pc_setaccountregstr( sd, num, value );
  2809. case '.': {
  2810. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2811. if( n ){
  2812. if( value[0] ){
  2813. i64db_put( n->vars, num, aStrdup( value ) );
  2814. if( script_getvaridx( num ) ){
  2815. script_array_update( n, num, false );
  2816. }
  2817. }else{
  2818. i64db_remove( n->vars, num );
  2819. if( script_getvaridx( num ) ){
  2820. script_array_update( n, num, true );
  2821. }
  2822. }
  2823. }
  2824. }
  2825. return true;
  2826. case '\'': {
  2827. struct reg_db *src = nullptr;
  2828. if( ref ){
  2829. src = ref;
  2830. }else{
  2831. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2832. if (idata)
  2833. src = &idata->regs;
  2834. }
  2835. if( src ){
  2836. bool empty;
  2837. if( value[0] ){
  2838. i64db_put( src->vars, num, aStrdup( value ) );
  2839. empty = false;
  2840. }else{
  2841. i64db_remove( src->vars, num );
  2842. empty = true;
  2843. }
  2844. if( script_getvaridx( num ) != 0 ){
  2845. script_array_update( src, num, empty );
  2846. }
  2847. }else{
  2848. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2849. script_reportsrc( st );
  2850. }
  2851. return true;
  2852. }
  2853. default:
  2854. return pc_setglobalreg_str( sd, num, value );
  2855. }
  2856. }
  2857. bool set_reg_num( struct script_state* st, map_session_data* sd, int64 num, const char* name, int64 value, struct reg_db *ref ){
  2858. char prefix = name[0];
  2859. size_t vlen = 0;
  2860. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2861. 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 );
  2862. return false;
  2863. }
  2864. if( is_string_variable( name ) ){
  2865. // string variable
  2866. return false;
  2867. }
  2868. if( str_data[script_getvarid(num)].type == C_PARAM ){
  2869. if( pc_setparam( sd, str_data[script_getvarid(num)].val, value ) == 0 ){
  2870. if( st != NULL ) {
  2871. ShowError( "script_set_reg: failed to set param '%s' to %" PRId64 ".\n", name, value );
  2872. script_reportsrc( st );
  2873. st->state = END;
  2874. }
  2875. return false;
  2876. }
  2877. return true;
  2878. }
  2879. switch( prefix ){
  2880. case '@':
  2881. pc_setreg( sd, num, value );
  2882. return true;
  2883. case '$':
  2884. return mapreg_setreg( num, value );
  2885. case '#':
  2886. return ( name[1] == '#' ) ? pc_setaccountreg2(sd, num, value) : pc_setaccountreg( sd, num, value );
  2887. case '.': {
  2888. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2889. if( n ){
  2890. if( value != 0 ){
  2891. i64db_i64put( n->vars, num, value );
  2892. if( script_getvaridx( num ) ){
  2893. script_array_update( n, num, false );
  2894. }
  2895. }else{
  2896. i64db_remove( n->vars, num );
  2897. if( script_getvaridx( num ) ){
  2898. script_array_update( n, num, true );
  2899. }
  2900. }
  2901. }
  2902. }
  2903. return true;
  2904. case '\'': {
  2905. struct reg_db *src = nullptr;
  2906. if( ref ){
  2907. src = ref;
  2908. }else{
  2909. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2910. if (idata)
  2911. src = &idata->regs;
  2912. }
  2913. if( src ){
  2914. bool empty;
  2915. if( value != 0 ){
  2916. i64db_i64put( src->vars, num, value );
  2917. empty = false;
  2918. }else{
  2919. i64db_remove( src->vars, num );
  2920. empty = true;
  2921. }
  2922. if( script_getvaridx( num ) != 0 ){
  2923. script_array_update( src, num, empty );
  2924. }
  2925. }else{
  2926. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2927. script_reportsrc( st );
  2928. }
  2929. return true;
  2930. }
  2931. default:
  2932. return pc_setglobalreg( sd, num, value );
  2933. }
  2934. }
  2935. bool set_var_str( map_session_data* sd, const char* name, const char* val ){
  2936. return set_reg_str( NULL, sd, reference_uid( add_str( name ), 0 ), name, val, NULL );
  2937. }
  2938. bool clear_reg( struct script_state* st, map_session_data* sd, int64 num, const char* name, struct reg_db *ref ){
  2939. if( is_string_variable( name ) ){
  2940. return set_reg_str( st, sd, num, name, "", ref );
  2941. }else{
  2942. return set_reg_num( st, sd, num, name, 0, ref );
  2943. }
  2944. }
  2945. void setd_sub_num( struct script_state* st, map_session_data* sd, const char* varname, int elem, int64 value, struct reg_db* ref ){
  2946. set_reg_num( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2947. }
  2948. void setd_sub_str( struct script_state* st, map_session_data* sd, const char* varname, int elem, const char* value, struct reg_db* ref ){
  2949. set_reg_str( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2950. }
  2951. /**
  2952. * Converts the data to a string
  2953. * @param st
  2954. * @param data
  2955. * @param sd
  2956. */
  2957. const char* conv_str_(struct script_state* st, struct script_data* data, map_session_data *sd)
  2958. {
  2959. char* p;
  2960. get_val_(st, data, sd);
  2961. if( data_isstring(data) )
  2962. {// nothing to convert
  2963. }
  2964. else if( data_isint(data) )
  2965. {// int -> string
  2966. CREATE(p, char, ITEM_NAME_LENGTH);
  2967. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2968. p[ITEM_NAME_LENGTH-1] = '\0';
  2969. data->type = C_STR;
  2970. data->u.str = p;
  2971. }
  2972. else if( data_isreference(data) )
  2973. {// reference -> string
  2974. //##TODO when does this happen (check get_val) [FlavioJS] -- at getd!!
  2975. data->type = C_CONSTSTR;
  2976. data->u.str = reference_getname(data);
  2977. }
  2978. else
  2979. {// unsupported data type
  2980. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2981. script_reportdata(data);
  2982. script_reportsrc(st);
  2983. data->type = C_CONSTSTR;
  2984. data->u.str = const_cast<char *>("");
  2985. }
  2986. return data->u.str;
  2987. }
  2988. const char* conv_str(struct script_state* st, struct script_data* data)
  2989. {
  2990. return conv_str_(st, data, NULL);
  2991. }
  2992. /**
  2993. * Converts the data to an int
  2994. * @param st
  2995. * @param data
  2996. * @param sd
  2997. */
  2998. int64 conv_num_(struct script_state* st, struct script_data* data, map_session_data *sd)
  2999. {
  3000. get_val_(st, data, sd);
  3001. if( data_isint(data) )
  3002. {// nothing to convert
  3003. }
  3004. else if( data_isstring(data) )
  3005. {// string -> int
  3006. // the result does not overflow or underflow, it is capped instead
  3007. // ex: 999999999999 is capped to INT_MAX (2147483647)
  3008. char* p = data->u.str;
  3009. int64 num;
  3010. bool error;
  3011. errno = 0;
  3012. num = strtoll(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  3013. error = (errno == ERANGE);
  3014. if( num < SCRIPT_INT_MIN || ( error && num == INT64_MIN ) ){
  3015. num = SCRIPT_INT_MIN;
  3016. ShowError( "script:conv_num: underflow detected, capping value to %" PRId64 "", SCRIPT_INT_MIN );
  3017. script_reportdata(data);
  3018. script_reportsrc(st);
  3019. }else if( num > SCRIPT_INT_MAX || ( error && num == INT64_MAX ) ){
  3020. num = SCRIPT_INT_MAX;
  3021. ShowError( "script:conv_num: overflow detected, capping value to %" PRId64 "", SCRIPT_INT_MAX );
  3022. script_reportdata(data);
  3023. script_reportsrc(st);
  3024. }
  3025. if( data->type == C_STR )
  3026. aFree(p);
  3027. data->type = C_INT;
  3028. data->u.num = num;
  3029. }
  3030. #if 0
  3031. // FIXME this function is being used to retrieve the position of labels and
  3032. // probably other stuff [FlavioJS]
  3033. else
  3034. {// unsupported data type
  3035. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3036. script_reportdata(data);
  3037. script_reportsrc(st);
  3038. data->type = C_INT;
  3039. data->u.num = 0;
  3040. }
  3041. #endif
  3042. return data->u.num;
  3043. }
  3044. int64 conv_num64(struct script_state* st, struct script_data* data)
  3045. {
  3046. return conv_num_(st, data, NULL);
  3047. }
  3048. int conv_num(struct script_state* st, struct script_data* data)
  3049. {
  3050. return static_cast<int>(conv_num_(st, data, NULL));
  3051. }
  3052. //
  3053. // Stack operations
  3054. //
  3055. /// Increases the size of the stack
  3056. void stack_expand(struct script_stack* stack)
  3057. {
  3058. stack->sp_max += 64;
  3059. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3060. stack->sp_max * sizeof(stack->stack_data[0]) );
  3061. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3062. 64 * sizeof(stack->stack_data[0]) );
  3063. }
  3064. /// Pushes a value into the stack
  3065. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  3066. /// Pushes a value into the stack (with reference)
  3067. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3068. {
  3069. if( stack->sp >= stack->sp_max )
  3070. stack_expand(stack);
  3071. stack->stack_data[stack->sp].type = type;
  3072. stack->stack_data[stack->sp].u.num = val;
  3073. stack->stack_data[stack->sp].ref = ref;
  3074. stack->sp++;
  3075. return &stack->stack_data[stack->sp-1];
  3076. }
  3077. /// Pushes a string into the stack
  3078. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3079. {
  3080. if( stack->sp >= stack->sp_max )
  3081. stack_expand(stack);
  3082. stack->stack_data[stack->sp].type = type;
  3083. stack->stack_data[stack->sp].u.str = str;
  3084. stack->stack_data[stack->sp].ref = NULL;
  3085. stack->sp++;
  3086. return &stack->stack_data[stack->sp-1];
  3087. }
  3088. /// Pushes a retinfo into the stack
  3089. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3090. {
  3091. if( stack->sp >= stack->sp_max )
  3092. stack_expand(stack);
  3093. stack->stack_data[stack->sp].type = C_RETINFO;
  3094. stack->stack_data[stack->sp].u.ri = ri;
  3095. stack->stack_data[stack->sp].ref = ref;
  3096. stack->sp++;
  3097. return &stack->stack_data[stack->sp-1];
  3098. }
  3099. /// Pushes a copy of the target position into the stack
  3100. struct script_data* push_copy(struct script_stack* stack, int pos)
  3101. {
  3102. switch( stack->stack_data[pos].type ) {
  3103. case C_CONSTSTR:
  3104. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3105. break;
  3106. case C_STR:
  3107. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3108. break;
  3109. case C_RETINFO:
  3110. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3111. exit(1);
  3112. break;
  3113. default:
  3114. return push_val2(
  3115. stack,stack->stack_data[pos].type,
  3116. stack->stack_data[pos].u.num,
  3117. stack->stack_data[pos].ref
  3118. );
  3119. break;
  3120. }
  3121. }
  3122. /// Removes the values in indexes [start,end] from the stack.
  3123. /// Adjusts all stack pointers.
  3124. void pop_stack(struct script_state* st, int start, int end)
  3125. {
  3126. struct script_stack* stack = st->stack;
  3127. struct script_data* data;
  3128. int i;
  3129. if( start < 0 )
  3130. start = 0;
  3131. if( end > stack->sp )
  3132. end = stack->sp;
  3133. if( start >= end )
  3134. return;// nothing to pop
  3135. // free stack elements
  3136. for( i = start; i < end; i++ )
  3137. {
  3138. data = &stack->stack_data[i];
  3139. if( data->type == C_STR )
  3140. aFree(data->u.str);
  3141. if( data->type == C_RETINFO ) {
  3142. struct script_retinfo* ri = data->u.ri;
  3143. if (ri->scope.vars) {
  3144. script_free_vars(ri->scope.vars);
  3145. ri->scope.vars = NULL;
  3146. }
  3147. if (ri->scope.arrays) {
  3148. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3149. ri->scope.arrays = NULL;
  3150. }
  3151. if( data->ref )
  3152. aFree(data->ref);
  3153. aFree(ri);
  3154. }
  3155. data->type = C_NOP;
  3156. }
  3157. // move the rest of the elements
  3158. if( stack->sp > end )
  3159. {
  3160. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3161. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3162. stack->stack_data[i].type = C_NOP;
  3163. }
  3164. // adjust stack pointers
  3165. if( st->start > end ){
  3166. st->start -= end - start;
  3167. }else if( st->start > start ){
  3168. st->start = start;
  3169. }
  3170. if( st->end > end ){
  3171. st->end -= end - start;
  3172. }else if( st->end > start ){
  3173. st->end = start;
  3174. }
  3175. if( stack->defsp > end ){
  3176. stack->defsp -= end - start;
  3177. }else if( stack->defsp > start ){
  3178. stack->defsp = start;
  3179. }
  3180. stack->sp -= end - start;
  3181. }
  3182. ///
  3183. ///
  3184. ///
  3185. /*==========================================
  3186. * Release script dependent variable, dependent variable of function
  3187. *------------------------------------------*/
  3188. void script_free_vars(struct DBMap* storage)
  3189. {
  3190. if( storage ) {
  3191. // destroy the storage construct containing the variables
  3192. db_destroy(storage);
  3193. }
  3194. }
  3195. void script_free_code(struct script_code* code)
  3196. {
  3197. nullpo_retv(code);
  3198. if (code->instances)
  3199. script_stop_scriptinstances(code);
  3200. script_free_vars(code->local.vars);
  3201. if (code->local.arrays)
  3202. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3203. aFree(code->script_buf);
  3204. aFree(code);
  3205. }
  3206. /// Creates a new script state.
  3207. ///
  3208. /// @param script Script code
  3209. /// @param pos Position in the code
  3210. /// @param rid Who is running the script (attached player)
  3211. /// @param oid Where the code is being run (npc 'object')
  3212. /// @return Script state
  3213. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3214. {
  3215. struct script_state* st;
  3216. st = ers_alloc(st_ers, struct script_state);
  3217. st->stack = ers_alloc(stack_ers, struct script_stack);
  3218. st->stack->sp = 0;
  3219. st->stack->sp_max = 64;
  3220. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3221. st->stack->defsp = st->stack->sp;
  3222. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3223. st->stack->scope.arrays = NULL;
  3224. st->state = RUN;
  3225. st->script = rootscript;
  3226. st->pos = pos;
  3227. st->rid = rid;
  3228. st->oid = oid;
  3229. st->sleep.timer = INVALID_TIMER;
  3230. st->npc_item_flag = battle_config.item_enabled_npc;
  3231. if( st->script->instances != USHRT_MAX )
  3232. st->script->instances++;
  3233. else {
  3234. struct npc_data *nd = map_id2nd(oid);
  3235. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3236. }
  3237. if (!st->script->local.vars)
  3238. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3239. st->id = next_id++;
  3240. active_scripts++;
  3241. idb_put(st_db, st->id, st);
  3242. return st;
  3243. }
  3244. /// Frees a script state.
  3245. ///
  3246. /// @param st Script state
  3247. void script_free_state(struct script_state* st)
  3248. {
  3249. if (idb_exists(st_db, st->id)) {
  3250. map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3251. if (st->bk_st) // backup was not restored
  3252. 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);
  3253. if (sd && sd->st == st) { // Current script is aborted.
  3254. if(sd->state.using_fake_npc) {
  3255. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3256. sd->state.using_fake_npc = 0;
  3257. }
  3258. sd->st = NULL;
  3259. sd->npc_id = 0;
  3260. }
  3261. if (st->sleep.timer != INVALID_TIMER)
  3262. delete_timer(st->sleep.timer, run_script_timer);
  3263. if (st->stack) {
  3264. script_free_vars(st->stack->scope.vars);
  3265. if (st->stack->scope.arrays)
  3266. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3267. pop_stack(st, 0, st->stack->sp);
  3268. aFree(st->stack->stack_data);
  3269. ers_free(stack_ers, st->stack);
  3270. st->stack = NULL;
  3271. }
  3272. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3273. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3274. script_free_vars(st->script->local.vars);
  3275. st->script->local.vars = NULL;
  3276. }
  3277. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3278. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3279. st->script->local.arrays = NULL;
  3280. }
  3281. }
  3282. st->pos = -1;
  3283. idb_remove(st_db, st->id);
  3284. ers_free(st_ers, st);
  3285. if (--active_scripts == 0)
  3286. next_id = 0;
  3287. }
  3288. }
  3289. //
  3290. // Main execution unit
  3291. //
  3292. /*==========================================
  3293. * Read command
  3294. *------------------------------------------*/
  3295. c_op get_com(unsigned char *script,int *pos)
  3296. {
  3297. int i = 0, j = 0;
  3298. if(script[*pos]>=0x80){
  3299. return C_INT;
  3300. }
  3301. while(script[*pos]>=0x40){
  3302. i=script[(*pos)++]<<j;
  3303. j+=6;
  3304. }
  3305. return (c_op)(i+(script[(*pos)++]<<j));
  3306. }
  3307. /*==========================================
  3308. * Income figures
  3309. *------------------------------------------*/
  3310. int64 get_num(unsigned char *script,int *pos)
  3311. {
  3312. int i, end;
  3313. int64 value;
  3314. // Find the end of the data
  3315. for( i = 0; script[*pos+i] >= 0xc0; i++ ) ; // do nothing just find it
  3316. end = i; // store the end for later
  3317. value = 0; // initialize empty value
  3318. // If more than one byte of data exists
  3319. for( ; i > 0; i-- ){
  3320. value |= (int64)( script[*pos+i] & (int64)0x3f );
  3321. value <<= 6;
  3322. }
  3323. // Put our last piece of data into the return value
  3324. value |= (script[*pos+i] & (int64)0x3f);
  3325. *pos += end + 1; // one byte after the last byte of data
  3326. return value;
  3327. }
  3328. /// Ternary operators
  3329. /// test ? if_true : if_false
  3330. void op_3(struct script_state* st, int op)
  3331. {
  3332. struct script_data* data;
  3333. int flag = 0;
  3334. data = script_getdatatop(st, -3);
  3335. get_val(st, data);
  3336. if( data_isstring(data) )
  3337. flag = data->u.str[0];// "" -> false
  3338. else if( data_isint(data) )
  3339. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3340. else
  3341. {
  3342. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3343. script_reportdata(data);
  3344. script_reportsrc(st);
  3345. script_removetop(st, -3, 0);
  3346. script_pushnil(st);
  3347. return;
  3348. }
  3349. if( flag )
  3350. script_pushcopytop(st, -2);
  3351. else
  3352. script_pushcopytop(st, -1);
  3353. script_removetop(st, -4, -1);
  3354. }
  3355. /// Binary string operators
  3356. /// s1 EQ s2 -> i
  3357. /// s1 NE s2 -> i
  3358. /// s1 GT s2 -> i
  3359. /// s1 GE s2 -> i
  3360. /// s1 LT s2 -> i
  3361. /// s1 LE s2 -> i
  3362. /// s1 ADD s2 -> s
  3363. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3364. {
  3365. int a = 0;
  3366. switch(op){
  3367. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3368. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3369. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3370. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3371. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3372. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3373. case C_ADD:
  3374. {
  3375. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3376. strcpy(buf, s1);
  3377. strcat(buf, s2);
  3378. script_pushstr(st, buf);
  3379. return;
  3380. }
  3381. default:
  3382. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3383. script_reportsrc(st);
  3384. script_pushnil(st);
  3385. st->state = END;
  3386. return;
  3387. }
  3388. script_pushint(st,a);
  3389. }
  3390. /// Binary number operators
  3391. /// i OP i -> i
  3392. void op_2num( struct script_state* st, int op, int64 i1, int64 i2 ){
  3393. int64 ret;
  3394. switch( op ) {
  3395. case C_AND: ret = i1 & i2; break;
  3396. case C_OR: ret = i1 | i2; break;
  3397. case C_XOR: ret = i1 ^ i2; break;
  3398. case C_LAND: ret = (i1 && i2); break;
  3399. case C_LOR: ret = (i1 || i2); break;
  3400. case C_EQ: ret = (i1 == i2); break;
  3401. case C_NE: ret = (i1 != i2); break;
  3402. case C_GT: ret = (i1 > i2); break;
  3403. case C_GE: ret = (i1 >= i2); break;
  3404. case C_LT: ret = (i1 < i2); break;
  3405. case C_LE: ret = (i1 <= i2); break;
  3406. case C_R_SHIFT: ret = i1>>i2; break;
  3407. case C_L_SHIFT: ret = i1<<i2; break;
  3408. case C_DIV:
  3409. case C_MOD:
  3410. if( i2 == 0 )
  3411. {
  3412. ShowError("script:op_2num: division by zero detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3413. script_reportsrc(st);
  3414. script_pushnil(st);
  3415. st->state = END;
  3416. return;
  3417. }
  3418. else if( op == C_DIV )
  3419. ret = i1 / i2;
  3420. else//if( op == C_MOD )
  3421. ret = i1 % i2;
  3422. break;
  3423. default:
  3424. bool overflow;
  3425. // operators that can overflow/underflow
  3426. switch( op ) {
  3427. case C_ADD: overflow = util::safe_addition( i1, i2, ret ); break;
  3428. case C_SUB: overflow = util::safe_substraction( i1, i2, ret ); break;
  3429. case C_MUL: overflow = util::safe_multiplication( i1, i2, ret ); break;
  3430. default:
  3431. ShowError("script:op_2num: unexpected number operator %s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3432. script_reportsrc(st);
  3433. script_pushnil(st);
  3434. return;
  3435. }
  3436. if( overflow ){
  3437. ShowWarning("script:op_2num: overflow detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3438. script_reportsrc(st);
  3439. script_pushnil(st);
  3440. st->state = END;
  3441. return;
  3442. }
  3443. }
  3444. script_pushint(st, ret);
  3445. }
  3446. /// Binary operators
  3447. void op_2(struct script_state *st, int op)
  3448. {
  3449. struct script_data* left, leftref;
  3450. struct script_data* right;
  3451. leftref.type = C_NOP;
  3452. left = script_getdatatop(st, -2);
  3453. right = script_getdatatop(st, -1);
  3454. if (st->op2ref) {
  3455. if (data_isreference(left)) {
  3456. leftref = *left;
  3457. }
  3458. st->op2ref = 0;
  3459. }
  3460. get_val(st, left);
  3461. get_val(st, right);
  3462. // automatic conversions
  3463. switch( op )
  3464. {
  3465. case C_ADD:
  3466. if( data_isint(left) && data_isstring(right) )
  3467. {// convert int-string to string-string
  3468. conv_str(st, left);
  3469. }
  3470. else if( data_isstring(left) && data_isint(right) )
  3471. {// convert string-int to string-string
  3472. conv_str(st, right);
  3473. }
  3474. break;
  3475. }
  3476. if( data_isstring(left) && data_isstring(right) )
  3477. {// ss => op_2str
  3478. op_2str(st, op, left->u.str, right->u.str);
  3479. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3480. if (leftref.type != C_NOP)
  3481. {
  3482. if (left->type == C_STR) // don't free C_CONSTSTR
  3483. aFree(left->u.str);
  3484. *left = leftref;
  3485. }
  3486. }
  3487. else if( data_isint(left) && data_isint(right) )
  3488. {// ii => op_2num
  3489. int64 i1 = left->u.num;
  3490. int64 i2 = right->u.num;
  3491. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3492. op_2num(st, op, i1, i2);
  3493. if (leftref.type != C_NOP)
  3494. *left = leftref;
  3495. }
  3496. else
  3497. {// invalid argument
  3498. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3499. script_reportdata(left);
  3500. script_reportdata(right);
  3501. script_reportsrc(st);
  3502. script_removetop(st, -2, 0);
  3503. script_pushnil(st);
  3504. st->state = END;
  3505. }
  3506. }
  3507. /// Unary operators
  3508. /// NEG i -> i
  3509. /// NOT i -> i
  3510. /// LNOT i -> i
  3511. void op_1(struct script_state* st, int op)
  3512. {
  3513. struct script_data* data;
  3514. data = script_getdatatop(st, -1);
  3515. get_val(st, data);
  3516. if( !data_isint(data) )
  3517. {// not a number
  3518. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3519. script_reportdata(data);
  3520. script_reportsrc(st);
  3521. script_pushnil(st);
  3522. st->state = END;
  3523. return;
  3524. }
  3525. int64 i1 = data->u.num;
  3526. script_removetop(st, -1, 0);
  3527. switch( op )
  3528. {
  3529. case C_NEG: i1 = -i1; break;
  3530. case C_NOT: i1 = ~i1; break;
  3531. case C_LNOT: i1 = !i1; break;
  3532. default:
  3533. ShowError("script:op_1: unexpected operator %s i1=%" PRId64 "\n", script_op2name(op), i1);
  3534. script_reportsrc(st);
  3535. script_pushnil(st);
  3536. st->state = END;
  3537. return;
  3538. }
  3539. script_pushint(st, i1);
  3540. }
  3541. /// Checks the type of all arguments passed to a built-in function.
  3542. ///
  3543. /// @param st Script state whose stack arguments should be inspected.
  3544. /// @param func Built-in function for which the arguments are intended.
  3545. static void script_check_buildin_argtype(struct script_state* st, int func)
  3546. {
  3547. int idx, invalid = 0;
  3548. for( idx = 2; script_hasdata(st, idx); idx++ )
  3549. {
  3550. struct script_data* data = script_getdata(st, idx);
  3551. script_function* sf = &buildin_func[str_data[func].val];
  3552. char type = sf->arg[idx-2];
  3553. if( type == '?' || type == '*' )
  3554. {// optional argument or unknown number of optional parameters ( no types are after this )
  3555. break;
  3556. }
  3557. else if( type == 0 )
  3558. {// more arguments than necessary ( should not happen, as it is checked before )
  3559. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3560. invalid++;
  3561. break;
  3562. }
  3563. else
  3564. {
  3565. const char* name = NULL;
  3566. if( data_isreference(data) )
  3567. {// get name for variables to determine the type they refer to
  3568. name = reference_getname(data);
  3569. }
  3570. switch( type )
  3571. {
  3572. case 'v':
  3573. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3574. {// variant
  3575. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3576. script_reportdata(data);
  3577. invalid++;
  3578. }
  3579. break;
  3580. case 's':
  3581. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3582. {// string
  3583. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3584. script_reportdata(data);
  3585. invalid++;
  3586. }
  3587. break;
  3588. case 'i':
  3589. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3590. {// int ( params and constants are always int )
  3591. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3592. script_reportdata(data);
  3593. invalid++;
  3594. }
  3595. break;
  3596. case 'r':
  3597. if( !data_isreference(data) )
  3598. {// variables
  3599. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3600. script_reportdata(data);
  3601. invalid++;
  3602. }
  3603. break;
  3604. case 'l':
  3605. if( !data_islabel(data) && !data_isfunclabel(data) )
  3606. {// label
  3607. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3608. script_reportdata(data);
  3609. invalid++;
  3610. }
  3611. break;
  3612. }
  3613. }
  3614. }
  3615. if(invalid)
  3616. {
  3617. ShowDebug("Function: %s\n", get_str(func));
  3618. script_reportsrc(st);
  3619. }
  3620. }
  3621. /// Executes a buildin command.
  3622. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3623. int run_func(struct script_state *st)
  3624. {
  3625. struct script_data* data;
  3626. int i,start_sp,end_sp,func;
  3627. end_sp = st->stack->sp;// position after the last argument
  3628. for( i = end_sp-1; i > 0 ; --i )
  3629. if( st->stack->stack_data[i].type == C_ARG )
  3630. break;
  3631. if( i == 0 ) {
  3632. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3633. st->state = END;
  3634. script_reportsrc(st);
  3635. return 1;
  3636. }
  3637. start_sp = i-1;// C_NAME of the command
  3638. st->start = start_sp;
  3639. st->end = end_sp;
  3640. data = &st->stack->stack_data[st->start];
  3641. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3642. func = (int)data->u.num;
  3643. st->funcname = reference_getname(data);
  3644. } else {
  3645. ShowError("script:run_func: not a buildin command.\n");
  3646. script_reportdata(data);
  3647. script_reportsrc(st);
  3648. st->state = END;
  3649. return 1;
  3650. }
  3651. if( script_config.warn_func_mismatch_argtypes ) {
  3652. script_check_buildin_argtype(st, func);
  3653. }
  3654. if(str_data[func].func) {
  3655. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3656. if( buildin_func[str_data[func].val].deprecated ){
  3657. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3658. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3659. script_reportsrc(st);
  3660. }
  3661. #endif
  3662. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) {
  3663. //Report error
  3664. ShowWarning("Script command '%s' returned failure.\n", get_str(func));
  3665. script_reportsrc(st);
  3666. }
  3667. } else {
  3668. 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));
  3669. script_reportsrc(st);
  3670. st->state = END;
  3671. }
  3672. // Stack's datum are used when re-running functions [Eoe]
  3673. if( st->state == RERUNLINE )
  3674. return 0;
  3675. pop_stack(st, st->start, st->end);
  3676. if( st->state == RETFUNC ) {// return from a user-defined function
  3677. struct script_retinfo* ri;
  3678. int olddefsp = st->stack->defsp;
  3679. int nargs;
  3680. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3681. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3682. {
  3683. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3684. script_reportsrc(st);
  3685. st->state = END;
  3686. return 1;
  3687. }
  3688. script_free_vars(st->stack->scope.vars);
  3689. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3690. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3691. nargs = ri->nargs;
  3692. st->pos = ri->pos;
  3693. st->script = ri->script;
  3694. st->stack->scope.vars = ri->scope.vars;
  3695. st->stack->scope.arrays = ri->scope.arrays;
  3696. st->stack->defsp = ri->defsp;
  3697. memset(ri, 0, sizeof(struct script_retinfo));
  3698. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3699. st->state = GOTO;
  3700. }
  3701. return 0;
  3702. }
  3703. /*==========================================
  3704. * script execution
  3705. *------------------------------------------*/
  3706. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3707. {
  3708. struct script_state *st;
  3709. if( rootscript == NULL || pos < 0 )
  3710. return;
  3711. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3712. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3713. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3714. st = script_alloc_state(rootscript, pos, rid, oid);
  3715. run_script_main(st);
  3716. }
  3717. /**
  3718. * Free all related script code
  3719. * @param code: Script code to free
  3720. */
  3721. void script_stop_scriptinstances(struct script_code *code) {
  3722. DBIterator *iter;
  3723. struct script_state* st;
  3724. if( !active_scripts )
  3725. return; // Don't even bother.
  3726. iter = db_iterator(st_db);
  3727. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3728. if( st->script == code )
  3729. script_free_state(st);
  3730. }
  3731. dbi_destroy(iter);
  3732. }
  3733. /*==========================================
  3734. * Timer function for sleep
  3735. *------------------------------------------*/
  3736. TIMER_FUNC(run_script_timer){
  3737. struct script_state *st = (struct script_state *)data;
  3738. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3739. // If it was a player before going to sleep and there is still a unit attached to the script
  3740. if( id != 0 && st->rid ){
  3741. map_session_data *sd = map_id2sd(st->rid);
  3742. // Attached player is offline(logout) or another unit type(should not happen)
  3743. if( !sd ){
  3744. st->rid = 0;
  3745. st->state = END;
  3746. // Character mismatch. Cancel execution.
  3747. }else if( sd->status.char_id != id ){
  3748. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3749. script_reportsrc(st);
  3750. st->rid = 0;
  3751. st->state = END;
  3752. }
  3753. }
  3754. while (node && st->sleep.timer != INVALID_TIMER) {
  3755. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3756. script_erase_sleepdb(node);
  3757. st->sleep.timer = INVALID_TIMER;
  3758. break;
  3759. }
  3760. node = node->next;
  3761. }
  3762. if(st->state != RERUNLINE)
  3763. st->sleep.tick = 0;
  3764. run_script_main(st);
  3765. return 0;
  3766. }
  3767. /**
  3768. * Remove sleep timers from the NPC
  3769. * @param id: NPC ID
  3770. */
  3771. void script_stop_sleeptimers(int id) {
  3772. for (;;) {
  3773. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3774. if (!st)
  3775. break; // No more sleep timers
  3776. if (st->oid == id)
  3777. script_free_state(st);
  3778. }
  3779. }
  3780. /**
  3781. * Delete the specified node from sleep_db
  3782. * @param n: Linked list of sleep timers
  3783. */
  3784. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3785. struct linkdb_node *retnode;
  3786. if (!n)
  3787. return NULL;
  3788. if (!n->prev)
  3789. sleep_db = n->next;
  3790. else
  3791. n->prev->next = n->next;
  3792. if (n->next)
  3793. n->next->prev = n->prev;
  3794. retnode = n->next;
  3795. aFree(n);
  3796. return retnode;
  3797. }
  3798. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3799. ///
  3800. /// @param st Script state to detach.
  3801. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3802. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3803. {
  3804. map_session_data* sd;
  3805. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3806. if( sd->state.using_fake_npc ){
  3807. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, sd->fd );
  3808. sd->state.using_fake_npc = 0;
  3809. }
  3810. sd->st = st->bk_st;
  3811. sd->npc_id = st->bk_npcid;
  3812. sd->state.disable_atcommand_on_npc = 0;
  3813. if(st->bk_st) {
  3814. //Remove tag for removal.
  3815. st->bk_st = NULL;
  3816. st->bk_npcid = 0;
  3817. } else if(dequeue_event) {
  3818. #ifdef SECURE_NPCTIMEOUT
  3819. /**
  3820. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3821. **/
  3822. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3823. delete_timer(sd->npc_idle_timer,npc_secure_timeout_timer);
  3824. sd->npc_idle_timer = INVALID_TIMER;
  3825. }
  3826. #endif
  3827. npc_event_dequeue(sd);
  3828. }
  3829. }
  3830. else if(st->bk_st)
  3831. {// rid was set to 0, before detaching the script state
  3832. 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);
  3833. script_reportsrc(st->bk_st);
  3834. script_free_state(st->bk_st);
  3835. st->bk_st = NULL;
  3836. }
  3837. }
  3838. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3839. ///
  3840. /// @param st Script state to attach.
  3841. void script_attach_state(struct script_state* st){
  3842. map_session_data* sd;
  3843. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3844. {
  3845. if(st!=sd->st)
  3846. {
  3847. if(st->bk_st)
  3848. {// there is already a backup
  3849. 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);
  3850. }
  3851. st->bk_st = sd->st;
  3852. st->bk_npcid = sd->npc_id;
  3853. }
  3854. sd->st = st;
  3855. sd->npc_id = st->oid;
  3856. sd->npc_item_flag = st->npc_item_flag; // load default.
  3857. sd->state.disable_atcommand_on_npc = battle_config.atcommand_disable_npc && (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3858. #ifdef SECURE_NPCTIMEOUT
  3859. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  3860. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_secure_timeout_timer,sd->bl.id,0);
  3861. sd->npc_idle_tick = gettick();
  3862. #endif
  3863. }
  3864. }
  3865. /*==========================================
  3866. * The main part of the script execution
  3867. *------------------------------------------*/
  3868. void run_script_main(struct script_state *st)
  3869. {
  3870. int cmdcount = script_config.check_cmdcount;
  3871. int gotocount = script_config.check_gotocount;
  3872. TBL_PC *sd;
  3873. struct script_stack *stack = st->stack;
  3874. script_attach_state(st);
  3875. if(st->state == RERUNLINE) {
  3876. run_func(st);
  3877. if(st->state == GOTO)
  3878. st->state = RUN;
  3879. } else if(st->state != END)
  3880. st->state = RUN;
  3881. while(st->state == RUN) {
  3882. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3883. switch(c){
  3884. case C_EOL:
  3885. if( stack->defsp > stack->sp )
  3886. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3887. else
  3888. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3889. break;
  3890. case C_INT:
  3891. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3892. break;
  3893. case C_POS:
  3894. case C_NAME:
  3895. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3896. st->pos+=3;
  3897. break;
  3898. case C_ARG:
  3899. push_val(stack,c,0);
  3900. break;
  3901. case C_STR:
  3902. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3903. while(st->script->script_buf[st->pos++]);
  3904. break;
  3905. case C_FUNC:
  3906. run_func(st);
  3907. if(st->state==GOTO){
  3908. st->state = RUN;
  3909. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3910. ShowError("script:run_script_main: infinity loop !\n");
  3911. script_reportsrc(st);
  3912. st->state=END;
  3913. }
  3914. }
  3915. break;
  3916. case C_REF:
  3917. st->op2ref = 1;
  3918. break;
  3919. case C_NEG:
  3920. case C_NOT:
  3921. case C_LNOT:
  3922. op_1(st ,c);
  3923. break;
  3924. case C_ADD:
  3925. case C_SUB:
  3926. case C_MUL:
  3927. case C_DIV:
  3928. case C_MOD:
  3929. case C_EQ:
  3930. case C_NE:
  3931. case C_GT:
  3932. case C_GE:
  3933. case C_LT:
  3934. case C_LE:
  3935. case C_AND:
  3936. case C_OR:
  3937. case C_XOR:
  3938. case C_LAND:
  3939. case C_LOR:
  3940. case C_R_SHIFT:
  3941. case C_L_SHIFT:
  3942. op_2(st, c);
  3943. break;
  3944. case C_OP3:
  3945. op_3(st, c);
  3946. break;
  3947. case C_NOP:
  3948. st->state=END;
  3949. break;
  3950. default:
  3951. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3952. st->state=END;
  3953. break;
  3954. }
  3955. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3956. ShowError("script:run_script_main: infinity loop !\n");
  3957. script_reportsrc(st);
  3958. st->state=END;
  3959. }
  3960. }
  3961. if(st->sleep.tick > 0) {
  3962. //Restore previous script
  3963. script_detach_state(st, false);
  3964. //Delay execution
  3965. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3966. st->sleep.charid = sd?sd->status.char_id:0;
  3967. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3968. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3969. } else if(st->state != END && st->rid) {
  3970. //Resume later (st is already attached to player).
  3971. if(st->bk_st) {
  3972. ShowWarning("Unable to restore stack! Double continuation!\n");
  3973. //Report BOTH scripts to see if that can help somehow.
  3974. ShowDebug("Previous script (lost):\n");
  3975. script_reportsrc(st->bk_st);
  3976. ShowDebug("Current script:\n");
  3977. script_reportsrc(st);
  3978. script_free_state(st->bk_st);
  3979. st->bk_st = NULL;
  3980. }
  3981. } else {
  3982. if (st->stack && st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp - 1].type == C_RETINFO) {
  3983. for (int i = 0; i < st->stack->sp; i++) {
  3984. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  3985. st->script = st->stack->stack_data[i].u.ri->script;
  3986. break;
  3987. }
  3988. }
  3989. }
  3990. //Dispose of script.
  3991. if ((sd = map_id2sd(st->rid))!=NULL)
  3992. { //Restore previous stack and save char.
  3993. if(sd->state.using_fake_npc){
  3994. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3995. sd->state.using_fake_npc = 0;
  3996. }
  3997. //Restore previous script if any.
  3998. script_detach_state(st, true);
  3999. if (sd->vars_dirty)
  4000. intif_saveregistry(sd);
  4001. }
  4002. script_free_state(st);
  4003. }
  4004. }
  4005. int script_config_read(const char *cfgName)
  4006. {
  4007. int i;
  4008. char line[1024],w1[1024],w2[1024];
  4009. FILE *fp;
  4010. fp=fopen(cfgName,"r");
  4011. if(fp==NULL){
  4012. ShowError("File not found: %s\n", cfgName);
  4013. return 1;
  4014. }
  4015. while(fgets(line, sizeof(line), fp))
  4016. {
  4017. if(line[0] == '/' && line[1] == '/')
  4018. continue;
  4019. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  4020. if(i!=2)
  4021. continue;
  4022. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  4023. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  4024. }
  4025. else if(strcmpi(w1,"check_cmdcount")==0) {
  4026. script_config.check_cmdcount = config_switch(w2);
  4027. }
  4028. else if(strcmpi(w1,"check_gotocount")==0) {
  4029. script_config.check_gotocount = config_switch(w2);
  4030. }
  4031. else if(strcmpi(w1,"input_min_value")==0) {
  4032. script_config.input_min_value = config_switch(w2);
  4033. }
  4034. else if(strcmpi(w1,"input_max_value")==0) {
  4035. script_config.input_max_value = config_switch(w2);
  4036. }
  4037. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  4038. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  4039. }
  4040. else if(strcmpi(w1,"import")==0){
  4041. script_config_read(w2);
  4042. }
  4043. else {
  4044. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  4045. }
  4046. }
  4047. fclose(fp);
  4048. return 0;
  4049. }
  4050. /**
  4051. * @see DBApply
  4052. */
  4053. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  4054. {
  4055. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  4056. if (code)
  4057. script_free_code(code);
  4058. return 0;
  4059. }
  4060. void script_run_autobonus(const char *autobonus, map_session_data *sd, unsigned int pos)
  4061. {
  4062. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4063. if( script )
  4064. {
  4065. int j;
  4066. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4067. if( j < EQI_MAX ) {
  4068. //Single item autobonus
  4069. current_equip_item_index = sd->equip_index[j];
  4070. current_equip_combo_pos = 0;
  4071. } else {
  4072. //Combo autobonus
  4073. current_equip_item_index = -1;
  4074. current_equip_combo_pos = pos;
  4075. }
  4076. run_script(script,0,sd->bl.id,0);
  4077. }
  4078. }
  4079. void script_add_autobonus(const char *autobonus)
  4080. {
  4081. if( strdb_get(autobonus_db, autobonus) == NULL )
  4082. {
  4083. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4084. if( script )
  4085. strdb_put(autobonus_db, autobonus, script);
  4086. }
  4087. }
  4088. void script_run_petautobonus(const std::string &autobonus, map_session_data &sd) {
  4089. std::shared_ptr<s_pet_autobonus_wrapper> script = util::umap_find(pet_autobonuses, autobonus);
  4090. if (script != nullptr) {
  4091. run_script(script->script, 0, sd.bl.id, 0);
  4092. }
  4093. }
  4094. void script_add_petautobonus(const std::string &autobonus) {
  4095. if (util::umap_find(pet_autobonuses, autobonus) == nullptr) {
  4096. script_code *script = parse_script(autobonus.c_str(), "petautobonus", 0, 0);
  4097. if (script != nullptr) {
  4098. std::shared_ptr<s_pet_autobonus_wrapper> bonus = std::make_shared<s_pet_autobonus_wrapper>();
  4099. bonus->script = script;
  4100. pet_autobonuses.emplace(autobonus, bonus);
  4101. }
  4102. }
  4103. }
  4104. /// resets a temporary character array variable to given value
  4105. void script_cleararray_pc( map_session_data* sd, const char* varname ){
  4106. struct script_array *sa = NULL;
  4107. struct reg_db *src = NULL;
  4108. unsigned int i, *list = NULL, size = 0;
  4109. int key;
  4110. key = add_str(varname);
  4111. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4112. return;
  4113. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4114. return;
  4115. size = sa->size;
  4116. list = script_array_cpy_list(sa);
  4117. for( i = 0; i < size; i++ ){
  4118. clear_reg( NULL, sd, reference_uid( key, list[i] ), varname, NULL );
  4119. }
  4120. }
  4121. /// sets a temporary character array variable element idx to given value
  4122. /// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set.
  4123. void script_setarray_pc(map_session_data* sd, const char* varname, uint32 idx, int64 value, int* refcache)
  4124. {
  4125. int key;
  4126. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4127. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4128. return;
  4129. }
  4130. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4131. set_reg_num( NULL, sd, reference_uid( key, idx ), varname, value, NULL );
  4132. if( refcache ) {
  4133. // save to avoid repeated script->add_str calls
  4134. refcache[0] = key;
  4135. }
  4136. }
  4137. /**
  4138. * Clears persistent variables from memory
  4139. **/
  4140. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4141. {
  4142. struct script_reg_state *src;
  4143. if( data->type != DB_DATA_PTR ) // got no need for those!
  4144. return 0;
  4145. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4146. if( src->type ) {
  4147. struct script_reg_str *p = (struct script_reg_str *)src;
  4148. if( p->value )
  4149. aFree(p->value);
  4150. ers_free(str_reg_ers,p);
  4151. } else {
  4152. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4153. }
  4154. return 0;
  4155. }
  4156. /**
  4157. * Clears a single persistent variable
  4158. **/
  4159. void script_reg_destroy_single(map_session_data *sd, int64 reg, struct script_reg_state *data)
  4160. {
  4161. i64db_remove(sd->regs.vars, reg);
  4162. if( data->type ) {
  4163. struct script_reg_str *p = (struct script_reg_str*)data;
  4164. if( p->value )
  4165. aFree(p->value);
  4166. ers_free(str_reg_ers,p);
  4167. } else {
  4168. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4169. }
  4170. }
  4171. unsigned int *script_array_cpy_list(struct script_array *sa)
  4172. {
  4173. if( sa->size > generic_ui_array_size )
  4174. script_generic_ui_array_expand(sa->size);
  4175. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4176. return generic_ui_array;
  4177. }
  4178. void script_generic_ui_array_expand (unsigned int plus)
  4179. {
  4180. generic_ui_array_size += plus + 100;
  4181. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4182. }
  4183. /*==========================================
  4184. * Destructor
  4185. *------------------------------------------*/
  4186. void do_final_script() {
  4187. int i;
  4188. DBIterator *iter;
  4189. struct script_state *st;
  4190. #ifdef DEBUG_HASH
  4191. if (battle_config.etc_log)
  4192. {
  4193. FILE *fp = fopen("hash_dump.txt","wt");
  4194. if(fp) {
  4195. int count[SCRIPT_HASH_SIZE];
  4196. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4197. int n=0;
  4198. int min=INT_MAX,max=0,zero=0;
  4199. double mean=0.0f;
  4200. double median=0.0f;
  4201. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4202. memset(count, 0, sizeof(count));
  4203. fprintf(fp,"num : hash : data_name\n");
  4204. fprintf(fp,"---------------------------------------------------------------\n");
  4205. for(i=LABEL_START; i<str_num; i++) {
  4206. unsigned int h = calc_hash(get_str(i));
  4207. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4208. ++count[h];
  4209. }
  4210. fprintf(fp,"--------------------\n\n");
  4211. memset(count2, 0, sizeof(count2));
  4212. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4213. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4214. if(min > count[i])
  4215. min = count[i]; // minimun count of collision
  4216. if(max < count[i])
  4217. max = count[i]; // maximun count of collision
  4218. if(count[i] == 0)
  4219. zero++;
  4220. ++count2[count[i]];
  4221. }
  4222. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4223. for( i=min; i <= max; ++i ){
  4224. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4225. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4226. }
  4227. for( i=min; i <= max; ++i ){
  4228. n += count2[i];
  4229. if( n*2 >= SCRIPT_HASH_SIZE )
  4230. {
  4231. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4232. median = (i+i+1)/2.0f;
  4233. else
  4234. median = i;
  4235. break;
  4236. }
  4237. }
  4238. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4239. fclose(fp);
  4240. }
  4241. }
  4242. #endif
  4243. mapreg_final();
  4244. db_destroy(scriptlabel_db);
  4245. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4246. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4247. ers_destroy(array_ers);
  4248. if (generic_ui_array)
  4249. aFree(generic_ui_array);
  4250. iter = db_iterator(st_db);
  4251. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4252. script_free_state(st);
  4253. dbi_destroy(iter);
  4254. if (str_data)
  4255. aFree(str_data);
  4256. if (str_buf)
  4257. aFree(str_buf);
  4258. for( i = 0; i < atcmd_binding_count; i++ ) {
  4259. aFree(atcmd_binding[i]);
  4260. }
  4261. if( atcmd_binding_count != 0 )
  4262. aFree(atcmd_binding);
  4263. ers_destroy(st_ers);
  4264. ers_destroy(stack_ers);
  4265. db_destroy(st_db);
  4266. }
  4267. /*==========================================
  4268. * Initialization
  4269. *------------------------------------------*/
  4270. void do_init_script(void) {
  4271. st_db = idb_alloc(DB_OPT_BASE);
  4272. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4273. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4274. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4275. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4276. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4277. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4278. add_timer_func_list( run_script_timer, "run_script_timer" );
  4279. ers_chunk_size(st_ers, 10);
  4280. ers_chunk_size(stack_ers, 10);
  4281. active_scripts = 0;
  4282. next_id = 0;
  4283. mapreg_init();
  4284. add_buildin_func();
  4285. constant_db.load();
  4286. script_hardcoded_constants();
  4287. }
  4288. void script_reload(void) {
  4289. int i;
  4290. DBIterator *iter;
  4291. struct script_state *st;
  4292. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4293. db_clear(scriptlabel_db);
  4294. // @commands (script based)
  4295. // Clear bindings
  4296. for( i = 0; i < atcmd_binding_count; i++ ) {
  4297. aFree(atcmd_binding[i]);
  4298. }
  4299. if( atcmd_binding_count != 0 )
  4300. aFree(atcmd_binding);
  4301. atcmd_binding_count = 0;
  4302. iter = db_iterator(st_db);
  4303. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4304. script_free_state(st);
  4305. dbi_destroy(iter);
  4306. db_clear(st_db);
  4307. mapreg_reload();
  4308. }
  4309. //-----------------------------------------------------------------------------
  4310. // buildin functions
  4311. //
  4312. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4313. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4314. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4315. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4316. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4317. /////////////////////////////////////////////////////////////////////
  4318. // NPC interaction
  4319. //
  4320. /// Appends a message to the npc dialog.
  4321. /// If a dialog doesn't exist yet, one is created.
  4322. ///
  4323. /// mes "<message>";
  4324. BUILDIN_FUNC(mes)
  4325. {
  4326. map_session_data* sd;
  4327. if( !script_rid2sd(sd) )
  4328. return SCRIPT_CMD_SUCCESS;
  4329. if( !script_hasdata(st, 3) )
  4330. {// only a single line detected in the script
  4331. clif_scriptmes( *sd, st->oid, script_getstr( st, 2 ) );
  4332. }
  4333. else
  4334. {// parse multiple lines as they exist
  4335. int i;
  4336. for( i = 2; script_hasdata(st, i); i++ )
  4337. {
  4338. // send the message to the client
  4339. clif_scriptmes( *sd, st->oid, script_getstr( st, i ) );
  4340. }
  4341. }
  4342. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4343. return SCRIPT_CMD_SUCCESS;
  4344. }
  4345. /// Displays the button 'next' in the npc dialog.
  4346. /// The dialog text is cleared and the script continues when the button is pressed.
  4347. ///
  4348. /// next;
  4349. BUILDIN_FUNC(next)
  4350. {
  4351. map_session_data* sd;
  4352. if (!st->mes_active) {
  4353. ShowWarning("buildin_next: There is no mes active.\n");
  4354. return SCRIPT_CMD_FAILURE;
  4355. }
  4356. if( !script_rid2sd(sd) )
  4357. return SCRIPT_CMD_SUCCESS;
  4358. #ifdef SECURE_NPCTIMEOUT
  4359. sd->npc_idle_type = NPCT_WAIT;
  4360. #endif
  4361. st->state = STOP;
  4362. clif_scriptnext( *sd, st->oid );
  4363. return SCRIPT_CMD_SUCCESS;
  4364. }
  4365. /// Clears the dialog and continues the script without a next button.
  4366. ///
  4367. /// clear;
  4368. BUILDIN_FUNC(clear)
  4369. {
  4370. TBL_PC* sd;
  4371. if (!st->mes_active) {
  4372. ShowWarning("buildin_clear: There is no mes active.\n");
  4373. return SCRIPT_CMD_FAILURE;
  4374. }
  4375. if (!script_rid2sd(sd))
  4376. return SCRIPT_CMD_FAILURE;
  4377. clif_scriptclear( *sd, st->oid );
  4378. return SCRIPT_CMD_SUCCESS;
  4379. }
  4380. /// Ends the script and displays the button 'close' on the npc dialog.
  4381. /// The dialog is closed when the button is pressed.
  4382. ///
  4383. /// close;
  4384. BUILDIN_FUNC(close)
  4385. {
  4386. map_session_data* sd;
  4387. if( !script_rid2sd(sd) )
  4388. return SCRIPT_CMD_SUCCESS;
  4389. npc_data* nd = map_id2nd( st->oid );
  4390. if( nd != nullptr && nd->dynamicnpc.owner_char_id != 0 ){
  4391. nd->dynamicnpc.last_interaction = gettick();
  4392. }
  4393. const char* command = script_getfuncname( st );
  4394. if( !st->mes_active ) {
  4395. st->state = END; // Keep backwards compatibility.
  4396. ShowWarning("buildin_close: Incorrect use of '%s' command!\n", command);
  4397. script_reportsrc(st);
  4398. } else {
  4399. st->state = CLOSE;
  4400. st->mes_active = 0;
  4401. }
  4402. if( !strcmp(command, "close3") ){
  4403. st->clear_cutin = true;
  4404. }
  4405. clif_scriptclose(sd, st->oid);
  4406. return SCRIPT_CMD_SUCCESS;
  4407. }
  4408. /// Displays the button 'close' on the npc dialog.
  4409. /// The dialog is closed and the script continues when the button is pressed.
  4410. ///
  4411. /// close2;
  4412. BUILDIN_FUNC(close2)
  4413. {
  4414. TBL_PC* sd;
  4415. if( !script_rid2sd(sd) )
  4416. return SCRIPT_CMD_SUCCESS;
  4417. st->state = STOP;
  4418. if( st->mes_active )
  4419. st->mes_active = 0;
  4420. clif_scriptclose(sd, st->oid);
  4421. return SCRIPT_CMD_SUCCESS;
  4422. }
  4423. /// Counts the number of valid and total number of options in 'str'
  4424. /// If max_count > 0 the counting stops when that valid option is reached
  4425. /// total is incremented for each option (NULL is supported)
  4426. static int menu_countoptions(const char* str, int max_count, int* total)
  4427. {
  4428. int count = 0;
  4429. int bogus_total;
  4430. if( total == NULL )
  4431. total = &bogus_total;
  4432. ++(*total);
  4433. // initial empty options
  4434. while( *str == ':' )
  4435. {
  4436. ++str;
  4437. ++(*total);
  4438. }
  4439. // count menu options
  4440. while( *str != '\0' )
  4441. {
  4442. ++count;
  4443. --max_count;
  4444. if( max_count == 0 )
  4445. break;
  4446. while( *str != ':' && *str != '\0' )
  4447. ++str;
  4448. while( *str == ':' )
  4449. {
  4450. ++str;
  4451. ++(*total);
  4452. }
  4453. }
  4454. return count;
  4455. }
  4456. /// Displays a menu with options and goes to the target label.
  4457. /// The script is stopped if cancel is pressed.
  4458. /// Options with no text are not displayed in the client.
  4459. ///
  4460. /// Options can be grouped together, separated by the character ':' in the text:
  4461. /// ex: menu "A:B:C",L_target;
  4462. /// All these options go to the specified target label.
  4463. ///
  4464. /// The index of the selected option is put in the variable @menu.
  4465. /// Indexes start with 1 and are consistent with grouped and empty options.
  4466. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4467. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4468. ///
  4469. /// NOTE: the client closes the npc dialog when cancel is pressed
  4470. ///
  4471. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4472. BUILDIN_FUNC(menu)
  4473. {
  4474. int i;
  4475. const char* text;
  4476. TBL_PC* sd;
  4477. if( !script_rid2sd(sd) )
  4478. return SCRIPT_CMD_SUCCESS;
  4479. #ifdef SECURE_NPCTIMEOUT
  4480. sd->npc_idle_type = NPCT_MENU;
  4481. #endif
  4482. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4483. if( sd->state.menu_or_input == 0 )
  4484. {
  4485. struct StringBuf buf;
  4486. if( script_lastdata(st) % 2 == 0 )
  4487. {// argument count is not even (1st argument is at index 2)
  4488. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4489. st->state = END;
  4490. return SCRIPT_CMD_FAILURE;
  4491. }
  4492. StringBuf_Init(&buf);
  4493. sd->npc_menu = 0;
  4494. for( i = 2; i < script_lastdata(st); i += 2 )
  4495. {
  4496. struct script_data* data;
  4497. // menu options
  4498. text = script_getstr(st, i);
  4499. // target label
  4500. data = script_getdata(st, i+1);
  4501. if( !data_islabel(data) )
  4502. {// not a label
  4503. StringBuf_Destroy(&buf);
  4504. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4505. script_reportdata(data);
  4506. st->state = END;
  4507. return SCRIPT_CMD_FAILURE;
  4508. }
  4509. // append option(s)
  4510. if( text[0] == '\0' )
  4511. continue;// empty string, ignore
  4512. if( sd->npc_menu > 0 )
  4513. StringBuf_AppendStr(&buf, ":");
  4514. StringBuf_AppendStr(&buf, text);
  4515. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4516. }
  4517. st->state = RERUNLINE;
  4518. sd->state.menu_or_input = 1;
  4519. /**
  4520. * menus beyond this length crash the client (see bugreport:6402)
  4521. **/
  4522. if( StringBuf_Length(&buf) >= 2047 ) {
  4523. struct npc_data * nd = map_id2nd(st->oid);
  4524. char* menu;
  4525. CREATE(menu, char, 2048);
  4526. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4527. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4528. clif_scriptmenu(sd, st->oid, menu);
  4529. aFree(menu);
  4530. } else
  4531. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4532. StringBuf_Destroy(&buf);
  4533. if( sd->npc_menu >= 0xff )
  4534. {// 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
  4535. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4536. script_reportsrc(st);
  4537. }
  4538. }
  4539. else if( sd->npc_menu == 0xff )
  4540. {// Cancel was pressed
  4541. sd->state.menu_or_input = 0;
  4542. st->state = END;
  4543. }
  4544. else
  4545. {// goto target label
  4546. int menu = 0;
  4547. sd->state.menu_or_input = 0;
  4548. if( sd->npc_menu <= 0 )
  4549. {
  4550. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4551. st->state = END;
  4552. return SCRIPT_CMD_FAILURE;
  4553. }
  4554. // get target label
  4555. for( i = 2; i < script_lastdata(st); i += 2 )
  4556. {
  4557. text = script_getstr(st, i);
  4558. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4559. if( sd->npc_menu <= 0 )
  4560. break;// entry found
  4561. }
  4562. if( sd->npc_menu > 0 )
  4563. {// Invalid selection
  4564. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4565. st->state = END;
  4566. return SCRIPT_CMD_FAILURE;
  4567. }
  4568. if( !data_islabel(script_getdata(st, i + 1)) )
  4569. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4570. ShowError("buildin_menu: Unexpected data in label argument\n");
  4571. script_reportdata(script_getdata(st, i + 1));
  4572. st->state = END;
  4573. return SCRIPT_CMD_FAILURE;
  4574. }
  4575. pc_setreg(sd, add_str("@menu"), menu);
  4576. st->pos = script_getnum(st, i + 1);
  4577. st->state = GOTO;
  4578. }
  4579. return SCRIPT_CMD_SUCCESS;
  4580. }
  4581. /// Displays a menu with options and returns the selected option.
  4582. /// Behaves like 'menu' without the target labels.
  4583. ///
  4584. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4585. ///
  4586. /// @see menu
  4587. BUILDIN_FUNC(select)
  4588. {
  4589. int i;
  4590. const char* text;
  4591. TBL_PC* sd;
  4592. if( !script_rid2sd(sd) )
  4593. return SCRIPT_CMD_SUCCESS;
  4594. #ifdef SECURE_NPCTIMEOUT
  4595. sd->npc_idle_type = NPCT_MENU;
  4596. #endif
  4597. if( sd->state.menu_or_input == 0 ) {
  4598. struct StringBuf buf;
  4599. StringBuf_Init(&buf);
  4600. sd->npc_menu = 0;
  4601. for( i = 2; i <= script_lastdata(st); ++i ) {
  4602. text = script_getstr(st, i);
  4603. if( sd->npc_menu > 0 )
  4604. StringBuf_AppendStr(&buf, ":");
  4605. StringBuf_AppendStr(&buf, text);
  4606. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4607. }
  4608. st->state = RERUNLINE;
  4609. sd->state.menu_or_input = 1;
  4610. /**
  4611. * menus beyond this length crash the client (see bugreport:6402)
  4612. **/
  4613. if( StringBuf_Length(&buf) >= 2047 ) {
  4614. struct npc_data * nd = map_id2nd(st->oid);
  4615. char* menu;
  4616. CREATE(menu, char, 2048);
  4617. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4618. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4619. clif_scriptmenu(sd, st->oid, menu);
  4620. aFree(menu);
  4621. } else
  4622. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4623. StringBuf_Destroy(&buf);
  4624. if( sd->npc_menu >= 0xff ) {
  4625. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4626. script_reportsrc(st);
  4627. }
  4628. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4629. sd->state.menu_or_input = 0;
  4630. st->state = END;
  4631. } else {// return selected option
  4632. int menu = 0;
  4633. sd->state.menu_or_input = 0;
  4634. for( i = 2; i <= script_lastdata(st); ++i ) {
  4635. text = script_getstr(st, i);
  4636. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4637. if( sd->npc_menu <= 0 )
  4638. break;// entry found
  4639. }
  4640. pc_setreg(sd, add_str("@menu"), menu);
  4641. script_pushint(st, menu);
  4642. st->state = RUN;
  4643. }
  4644. return SCRIPT_CMD_SUCCESS;
  4645. }
  4646. /// Displays a menu with options and returns the selected option.
  4647. /// Behaves like 'menu' without the target labels, except when cancel is
  4648. /// pressed.
  4649. /// When cancel is pressed, the script continues and 255 is returned.
  4650. ///
  4651. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4652. ///
  4653. /// @see menu
  4654. BUILDIN_FUNC(prompt)
  4655. {
  4656. int i;
  4657. const char *text;
  4658. TBL_PC* sd;
  4659. if( !script_rid2sd(sd) )
  4660. return SCRIPT_CMD_SUCCESS;
  4661. #ifdef SECURE_NPCTIMEOUT
  4662. sd->npc_idle_type = NPCT_MENU;
  4663. #endif
  4664. if( sd->state.menu_or_input == 0 )
  4665. {
  4666. struct StringBuf buf;
  4667. StringBuf_Init(&buf);
  4668. sd->npc_menu = 0;
  4669. for( i = 2; i <= script_lastdata(st); ++i )
  4670. {
  4671. text = script_getstr(st, i);
  4672. if( sd->npc_menu > 0 )
  4673. StringBuf_AppendStr(&buf, ":");
  4674. StringBuf_AppendStr(&buf, text);
  4675. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4676. }
  4677. st->state = RERUNLINE;
  4678. sd->state.menu_or_input = 1;
  4679. /**
  4680. * menus beyond this length crash the client (see bugreport:6402)
  4681. **/
  4682. if( StringBuf_Length(&buf) >= 2047 ) {
  4683. struct npc_data * nd = map_id2nd(st->oid);
  4684. char* menu;
  4685. CREATE(menu, char, 2048);
  4686. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4687. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4688. clif_scriptmenu(sd, st->oid, menu);
  4689. aFree(menu);
  4690. } else
  4691. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4692. StringBuf_Destroy(&buf);
  4693. if( sd->npc_menu >= 0xff )
  4694. {
  4695. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4696. script_reportsrc(st);
  4697. }
  4698. }
  4699. else if( sd->npc_menu == 0xff )
  4700. {// Cancel was pressed
  4701. sd->state.menu_or_input = 0;
  4702. pc_setreg(sd, add_str("@menu"), 0xff);
  4703. script_pushint(st, 0xff);
  4704. st->state = RUN;
  4705. }
  4706. else
  4707. {// return selected option
  4708. int menu = 0;
  4709. sd->state.menu_or_input = 0;
  4710. for( i = 2; i <= script_lastdata(st); ++i )
  4711. {
  4712. text = script_getstr(st, i);
  4713. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4714. if( sd->npc_menu <= 0 )
  4715. break;// entry found
  4716. }
  4717. pc_setreg(sd, add_str("@menu"), menu);
  4718. script_pushint(st, menu);
  4719. st->state = RUN;
  4720. }
  4721. return SCRIPT_CMD_SUCCESS;
  4722. }
  4723. /////////////////////////////////////////////////////////////////////
  4724. // ...
  4725. //
  4726. /// Jumps to the target script label.
  4727. ///
  4728. /// goto <label>;
  4729. BUILDIN_FUNC(goto)
  4730. {
  4731. if( !data_islabel(script_getdata(st,2)) )
  4732. {
  4733. ShowError("buildin_goto: Not a label\n");
  4734. script_reportdata(script_getdata(st,2));
  4735. st->state = END;
  4736. return SCRIPT_CMD_FAILURE;
  4737. }
  4738. st->pos = script_getnum(st,2);
  4739. st->state = GOTO;
  4740. return SCRIPT_CMD_SUCCESS;
  4741. }
  4742. /*==========================================
  4743. * user-defined function call
  4744. *------------------------------------------*/
  4745. BUILDIN_FUNC(callfunc)
  4746. {
  4747. int i, j;
  4748. struct script_retinfo* ri;
  4749. struct script_code* scr;
  4750. const char* str = script_getstr(st,2);
  4751. struct reg_db *ref = NULL;
  4752. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4753. if(!scr) {
  4754. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4755. st->state = END;
  4756. return SCRIPT_CMD_FAILURE;
  4757. }
  4758. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4759. ref[0].vars = st->stack->scope.vars;
  4760. if (!st->stack->scope.arrays)
  4761. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4762. ref[0].arrays = st->stack->scope.arrays;
  4763. ref[1].vars = st->script->local.vars;
  4764. if (!st->script->local.arrays)
  4765. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4766. ref[1].arrays = st->script->local.arrays;
  4767. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4768. struct script_data* data = push_copy(st->stack,i);
  4769. if (data_isreference(data) && !data->ref) {
  4770. const char* name = reference_getname(data);
  4771. if (name[0] == '.')
  4772. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4773. }
  4774. }
  4775. CREATE(ri, struct script_retinfo, 1);
  4776. ri->script = st->script; // script code
  4777. ri->scope.vars = st->stack->scope.vars; // scope variables
  4778. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4779. ri->pos = st->pos; // script location
  4780. ri->nargs = j; // argument count
  4781. ri->defsp = st->stack->defsp; // default stack pointer
  4782. push_retinfo(st->stack, ri, ref);
  4783. st->pos = 0;
  4784. st->script = scr;
  4785. st->stack->defsp = st->stack->sp;
  4786. st->state = GOTO;
  4787. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4788. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4789. if (!st->script->local.vars)
  4790. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4791. return SCRIPT_CMD_SUCCESS;
  4792. }
  4793. /*==========================================
  4794. * subroutine call
  4795. *------------------------------------------*/
  4796. BUILDIN_FUNC(callsub)
  4797. {
  4798. int i,j;
  4799. struct script_retinfo* ri;
  4800. int pos = script_getnum(st,2);
  4801. struct reg_db *ref = NULL;
  4802. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4803. ShowError("buildin_callsub: Argument is not a label\n");
  4804. script_reportdata(script_getdata(st,2));
  4805. st->state = END;
  4806. return SCRIPT_CMD_FAILURE;
  4807. }
  4808. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4809. ref[0].vars = st->stack->scope.vars;
  4810. if (!st->stack->scope.arrays)
  4811. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4812. ref[0].arrays = st->stack->scope.arrays;
  4813. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4814. struct script_data* data = push_copy(st->stack,i);
  4815. if (data_isreference(data) && !data->ref) {
  4816. const char* name = reference_getname(data);
  4817. if (name[0] == '.' && name[1] == '@')
  4818. data->ref = &ref[0];
  4819. }
  4820. }
  4821. CREATE(ri, struct script_retinfo, 1);
  4822. ri->script = st->script; // script code
  4823. ri->scope.vars = st->stack->scope.vars; // scope variables
  4824. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4825. ri->pos = st->pos; // script location
  4826. ri->nargs = j; // argument count
  4827. ri->defsp = st->stack->defsp; // default stack pointer
  4828. push_retinfo(st->stack, ri, ref);
  4829. st->pos = pos;
  4830. st->stack->defsp = st->stack->sp;
  4831. st->state = GOTO;
  4832. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4833. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4834. return SCRIPT_CMD_SUCCESS;
  4835. }
  4836. /// Retrieves an argument provided to callfunc/callsub.
  4837. /// If the argument doesn't exist
  4838. ///
  4839. /// getarg(<index>{,<default_value>}) -> <value>
  4840. BUILDIN_FUNC(getarg)
  4841. {
  4842. struct script_retinfo* ri;
  4843. int idx;
  4844. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4845. {
  4846. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4847. st->state = END;
  4848. return SCRIPT_CMD_FAILURE;
  4849. }
  4850. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4851. idx = script_getnum(st,2);
  4852. if( idx >= 0 && idx < ri->nargs )
  4853. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4854. else if( script_hasdata(st,3) )
  4855. script_pushcopy(st, 3);
  4856. else
  4857. {
  4858. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4859. st->state = END;
  4860. return SCRIPT_CMD_FAILURE;
  4861. }
  4862. return SCRIPT_CMD_SUCCESS;
  4863. }
  4864. /// Returns from the current function, optionaly returning a value from the functions.
  4865. /// Don't use outside script functions.
  4866. ///
  4867. /// return;
  4868. /// return <value>;
  4869. BUILDIN_FUNC(return)
  4870. {
  4871. if( script_hasdata(st,2) )
  4872. {// return value
  4873. struct script_data* data;
  4874. script_pushcopy(st, 2);
  4875. data = script_getdatatop(st, -1);
  4876. if( data_isreference(data) ) {
  4877. const char* name = reference_getname(data);
  4878. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4879. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4880. get_val(st, data); // current scope, convert to value
  4881. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4882. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4883. }
  4884. }
  4885. }
  4886. else
  4887. {// no return value
  4888. script_pushnil(st);
  4889. }
  4890. st->state = RETFUNC;
  4891. return SCRIPT_CMD_SUCCESS;
  4892. }
  4893. /// Returns a random number.
  4894. /// rand(<range>) -> <int64> in the mathematical range [0, <range - 1>]
  4895. /// rand(<min>,<max>) -> <int64> in the mathematical range [<min>, <max>]
  4896. BUILDIN_FUNC(rand)
  4897. {
  4898. int64 minimum;
  4899. int64 maximum;
  4900. // min,max
  4901. if( script_hasdata( st, 3 ) ){
  4902. minimum = script_getnum64( st, 2 );
  4903. maximum = script_getnum64( st, 3 );
  4904. if( minimum > maximum ){
  4905. ShowWarning( "buildin_rand: minimum (%" PRId64 ") is bigger than maximum (%" PRId64 ").\n", minimum, maximum );
  4906. // rnd_value already fixes this by swapping minimum and maximum automatically
  4907. }
  4908. // range
  4909. }else{
  4910. minimum = 0;
  4911. maximum = script_getnum64( st, 2 );
  4912. if( maximum < 0 ){
  4913. ShowError( "buildin_rand: range (%" PRId64 ") is negative.\n", maximum );
  4914. st->state = END;
  4915. return SCRIPT_CMD_FAILURE;
  4916. }
  4917. // The range version is exclusive maximum
  4918. maximum -= 1;
  4919. if( maximum < 1 ){
  4920. ShowError( "buildin_rand: range (%" PRId64 ") is too small. No randomness possible.\n", maximum );
  4921. st->state = END;
  4922. return SCRIPT_CMD_FAILURE;
  4923. }
  4924. }
  4925. if( minimum == maximum ){
  4926. ShowError( "buildin_rand: minimum (%" PRId64 ") and maximum (%" PRId64 ") are equal. No randomness possible.\n", minimum, maximum );
  4927. st->state = END;
  4928. return SCRIPT_CMD_FAILURE;
  4929. }
  4930. script_pushint64( st, rnd_value( minimum, maximum ) );
  4931. return SCRIPT_CMD_SUCCESS;
  4932. }
  4933. /*==========================================
  4934. * Warp sd to str,x,y or Random or SavePoint/Save
  4935. *------------------------------------------*/
  4936. BUILDIN_FUNC(warp)
  4937. {
  4938. int ret;
  4939. int x,y;
  4940. const char* str;
  4941. map_session_data* sd;
  4942. if(!script_charid2sd(5, sd))
  4943. return SCRIPT_CMD_SUCCESS;
  4944. str = script_getstr(st,2);
  4945. x = script_getnum(st,3);
  4946. y = script_getnum(st,4);
  4947. if(strcmp(str,"Random")==0)
  4948. ret = pc_randomwarp(sd,CLR_TELEPORT,true);
  4949. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4950. ret = pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  4951. else
  4952. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4953. if( ret ) {
  4954. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4955. return SCRIPT_CMD_FAILURE;
  4956. }
  4957. return SCRIPT_CMD_SUCCESS;
  4958. }
  4959. /*==========================================
  4960. * Warp a specified area
  4961. *------------------------------------------*/
  4962. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4963. {
  4964. int x2,y2,x3,y3;
  4965. unsigned int index;
  4966. index = va_arg(ap,unsigned int);
  4967. x2 = va_arg(ap,int);
  4968. y2 = va_arg(ap,int);
  4969. x3 = va_arg(ap,int);
  4970. y3 = va_arg(ap,int);
  4971. if(index == 0)
  4972. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT,true);
  4973. else if(x3 && y3) {
  4974. int max, tx, ty, j = 0;
  4975. int16 m;
  4976. m = map_mapindex2mapid(index);
  4977. // choose a suitable max number of attempts
  4978. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4979. max = 1000;
  4980. // find a suitable map cell
  4981. do {
  4982. tx = rnd_value(x2, x3);
  4983. ty = rnd_value(y2, y3);
  4984. j++;
  4985. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4986. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4987. }
  4988. else
  4989. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4990. return 0;
  4991. }
  4992. BUILDIN_FUNC(areawarp)
  4993. {
  4994. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4995. unsigned int index;
  4996. const char *str;
  4997. const char *mapname;
  4998. mapname = script_getstr(st,2);
  4999. x0 = script_getnum(st,3);
  5000. y0 = script_getnum(st,4);
  5001. x1 = script_getnum(st,5);
  5002. y1 = script_getnum(st,6);
  5003. str = script_getstr(st,7);
  5004. x2 = script_getnum(st,8);
  5005. y2 = script_getnum(st,9);
  5006. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  5007. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  5008. x3 = 0;
  5009. y3 = 0;
  5010. } else if( x3 && y3 ) {
  5011. // normalize x3/y3 coordinates
  5012. if( x3 < x2 ) std::swap(x3,x2);
  5013. if( y3 < y2 ) std::swap(y3,y2);
  5014. }
  5015. }
  5016. if( (m = map_mapname2mapid(mapname)) < 0 )
  5017. return SCRIPT_CMD_FAILURE;
  5018. if( strcmp(str,"Random") == 0 )
  5019. index = 0;
  5020. else if( !(index=mapindex_name2id(str)) )
  5021. return SCRIPT_CMD_FAILURE;
  5022. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  5023. return SCRIPT_CMD_SUCCESS;
  5024. }
  5025. /*==========================================
  5026. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  5027. *------------------------------------------*/
  5028. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  5029. {
  5030. int hp, sp;
  5031. hp = va_arg(ap, int);
  5032. sp = va_arg(ap, int);
  5033. pc_percentheal((TBL_PC *)bl,hp,sp);
  5034. return 0;
  5035. }
  5036. BUILDIN_FUNC(areapercentheal)
  5037. {
  5038. int hp,sp,m;
  5039. const char *mapname;
  5040. int x0,y0,x1,y1;
  5041. mapname=script_getstr(st,2);
  5042. x0=script_getnum(st,3);
  5043. y0=script_getnum(st,4);
  5044. x1=script_getnum(st,5);
  5045. y1=script_getnum(st,6);
  5046. hp=script_getnum(st,7);
  5047. sp=script_getnum(st,8);
  5048. if( (m=map_mapname2mapid(mapname))< 0)
  5049. return SCRIPT_CMD_FAILURE;
  5050. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5051. return SCRIPT_CMD_SUCCESS;
  5052. }
  5053. enum e_warpparty_target{
  5054. WARPPARTY_RANDOM = 0,
  5055. WARPPARTY_SAVEPOINTALL,
  5056. WARPPARTY_SAVEPOINT,
  5057. WARPPARTY_LEADER,
  5058. WARPPARTY_RANDOMALL,
  5059. WARPPARTY_RANDOMALLAREA
  5060. };
  5061. /*==========================================
  5062. * Warpparty - [Fredzilla] [Paradox924X]
  5063. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5064. * If 'from_mapname' is specified, only the party members on that map will be warped
  5065. *------------------------------------------*/
  5066. BUILDIN_FUNC(warpparty)
  5067. {
  5068. TBL_PC *sd = NULL;
  5069. TBL_PC *pl_sd;
  5070. struct party_data* p;
  5071. int mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5072. const char* str = script_getstr(st,2);
  5073. int x = script_getnum(st,3);
  5074. int y = script_getnum(st,4);
  5075. int p_id = script_getnum(st,5);
  5076. const char* str2 = NULL;
  5077. if ( script_hasdata(st,6) )
  5078. str2 = script_getstr(st,6);
  5079. if (script_hasdata(st, 7))
  5080. rx = script_getnum(st, 7);
  5081. if (script_hasdata(st, 8))
  5082. ry = script_getnum(st, 8);
  5083. p = party_search(p_id);
  5084. if(!p)
  5085. return SCRIPT_CMD_SUCCESS;
  5086. enum e_warpparty_target type = ( strcmp(str,"Random")==0 ) ? WARPPARTY_RANDOM
  5087. : ( strcmp(str,"SavePointAll")==0 ) ? WARPPARTY_SAVEPOINTALL
  5088. : ( strcmp(str,"SavePoint")==0 ) ? WARPPARTY_SAVEPOINT
  5089. : ( strcmp(str,"Leader")==0 ) ? WARPPARTY_LEADER
  5090. : ( strcmp(str,"RandomAll")==0 ) ? WARPPARTY_RANDOMALL
  5091. : WARPPARTY_RANDOMALLAREA;
  5092. switch (type)
  5093. {
  5094. case WARPPARTY_SAVEPOINT:
  5095. //"SavePoint" uses save point of the currently attached player
  5096. if ( !script_rid2sd(sd) )
  5097. return SCRIPT_CMD_FAILURE;
  5098. break;
  5099. case WARPPARTY_LEADER:
  5100. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5101. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5102. return SCRIPT_CMD_FAILURE;
  5103. pl_sd = p->data[i].sd;
  5104. mapindex = pl_sd->mapindex;
  5105. m = map_mapindex2mapid(mapindex);
  5106. x = pl_sd->bl.x;
  5107. y = pl_sd->bl.y;
  5108. break;
  5109. case WARPPARTY_RANDOMALL: {
  5110. if ( !script_rid2sd(sd) )
  5111. return SCRIPT_CMD_FAILURE;
  5112. mapindex = sd->mapindex;
  5113. m = map_mapindex2mapid(mapindex);
  5114. struct map_data *mapdata = map_getmapdata(m);
  5115. if ( mapdata == nullptr || mapdata->getMapFlag(MF_NOWARP) || mapdata->getMapFlag(MF_NOTELEPORT) )
  5116. return SCRIPT_CMD_FAILURE;
  5117. i = 0;
  5118. do {
  5119. x = rnd_value(1, mapdata->xs - 1);
  5120. y = rnd_value(1, mapdata->ys - 1);
  5121. } while ((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  5122. if (i >= 1000) {
  5123. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  5124. return SCRIPT_CMD_FAILURE;
  5125. }
  5126. } break;
  5127. case WARPPARTY_RANDOMALLAREA:
  5128. mapindex = mapindex_name2id(str);
  5129. if (!mapindex) {// Invalid map
  5130. return SCRIPT_CMD_FAILURE;
  5131. }
  5132. m = map_mapindex2mapid(mapindex);
  5133. break;
  5134. }
  5135. for (i = 0; i < MAX_PARTY; i++)
  5136. {
  5137. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5138. continue;
  5139. map_data* mapdata = map_getmapdata(pl_sd->bl.m);
  5140. if( str2 && strcmp(str2, mapdata->name) != 0 )
  5141. continue;
  5142. if( pc_isdead(pl_sd) )
  5143. continue;
  5144. e_setpos ret = SETPOS_OK;
  5145. switch( type )
  5146. {
  5147. case WARPPARTY_RANDOM:
  5148. if (!mapdata->getMapFlag(MF_NOWARP))
  5149. ret = (e_setpos)pc_randomwarp(pl_sd,CLR_TELEPORT);
  5150. break;
  5151. case WARPPARTY_SAVEPOINTALL:
  5152. if (!mapdata->getMapFlag(MF_NORETURN))
  5153. 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 );
  5154. break;
  5155. case WARPPARTY_SAVEPOINT:
  5156. if (!mapdata->getMapFlag(MF_NORETURN))
  5157. ret = pc_setpos( pl_sd, mapindex_name2id( sd->status.save_point.map ),sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  5158. break;
  5159. case WARPPARTY_LEADER:
  5160. if (p->party.member[i].leader)
  5161. continue;
  5162. // Fall through
  5163. case WARPPARTY_RANDOMALL:
  5164. if (pl_sd == sd) {
  5165. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5166. break;
  5167. }
  5168. // Fall through
  5169. case WARPPARTY_RANDOMALLAREA:
  5170. 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))){
  5171. if (rx || ry) {
  5172. int x1 = x + rx, y1 = y + ry,
  5173. x0 = x - rx, y0 = y - ry,
  5174. nx, ny;
  5175. uint8 attempts = 10;
  5176. do {
  5177. nx = x0 + rnd_value(x0, x1);
  5178. ny = y0 + rnd_value(y0, y1);
  5179. } while ((--attempts) > 0 && !map_getcell(m, nx, ny, CELL_CHKPASS));
  5180. if (attempts != 0) { //Keep the original coordinates if fails to find a valid cell within the range
  5181. x = nx;
  5182. y = ny;
  5183. }
  5184. }
  5185. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5186. }
  5187. break;
  5188. }
  5189. if( ret != SETPOS_OK ) {
  5190. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", pl_sd->status.name, str, x, y);
  5191. if ( ( type == WARPPARTY_RANDOMALL || type == WARPPARTY_RANDOMALLAREA ) && (rx || ry) )
  5192. continue;
  5193. else
  5194. return SCRIPT_CMD_FAILURE;
  5195. }
  5196. }
  5197. return SCRIPT_CMD_SUCCESS;
  5198. }
  5199. /*==========================================
  5200. * Warpguild - [Fredzilla]
  5201. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5202. *------------------------------------------*/
  5203. BUILDIN_FUNC(warpguild)
  5204. {
  5205. TBL_PC *sd = NULL;
  5206. TBL_PC *pl_sd;
  5207. struct s_mapiterator* iter;
  5208. int type, mapindex = 0, m = -1;
  5209. const char* str = script_getstr(st,2);
  5210. int x = script_getnum(st,3);
  5211. int y = script_getnum(st,4);
  5212. int gid = script_getnum(st,5);
  5213. auto g = guild_search(gid);
  5214. if( g == NULL )
  5215. return SCRIPT_CMD_SUCCESS;
  5216. type = ( strcmp(str,"Random")==0 ) ? 0
  5217. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5218. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5219. : 3;
  5220. if( type == 2 && !script_rid2sd(sd) )
  5221. {// "SavePoint" uses save point of the currently attached player
  5222. return SCRIPT_CMD_SUCCESS;
  5223. }
  5224. switch (type) {
  5225. case 3:
  5226. mapindex = mapindex_name2id(str);
  5227. if (!mapindex)
  5228. return SCRIPT_CMD_FAILURE;
  5229. m = map_mapindex2mapid(mapindex);
  5230. break;
  5231. }
  5232. iter = mapit_getallusers();
  5233. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5234. {
  5235. if( pl_sd->status.guild_id != gid )
  5236. continue;
  5237. switch( type )
  5238. {
  5239. case 0: // Random
  5240. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5241. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5242. break;
  5243. case 1: // SavePointAll
  5244. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5245. 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 );
  5246. break;
  5247. case 2: // SavePoint
  5248. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5249. pc_setpos( pl_sd, mapindex_name2id( sd->status.save_point.map ),sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  5250. break;
  5251. case 3: // m,x,y
  5252. 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)))
  5253. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5254. break;
  5255. }
  5256. }
  5257. mapit_free(iter);
  5258. return SCRIPT_CMD_SUCCESS;
  5259. }
  5260. /*==========================================
  5261. * Force Heal a player (hp and sp)
  5262. *------------------------------------------*/
  5263. BUILDIN_FUNC(heal)
  5264. {
  5265. TBL_PC *sd;
  5266. int hp,sp;
  5267. if (!script_charid2sd(4,sd))
  5268. return SCRIPT_CMD_SUCCESS;
  5269. hp=script_getnum(st,2);
  5270. sp=script_getnum(st,3);
  5271. status_heal(&sd->bl, hp, sp, 1);
  5272. return SCRIPT_CMD_SUCCESS;
  5273. }
  5274. /*==========================================
  5275. * Force Heal a player (ap)
  5276. *------------------------------------------*/
  5277. BUILDIN_FUNC(healap)
  5278. {
  5279. map_session_data* sd;
  5280. if (!script_charid2sd(3, sd))
  5281. return SCRIPT_CMD_FAILURE;
  5282. status_heal(&sd->bl, 0, 0, script_getnum(st, 2), 1);
  5283. return SCRIPT_CMD_SUCCESS;
  5284. }
  5285. /*==========================================
  5286. * Heal a player by item (get vit bonus etc)
  5287. *------------------------------------------*/
  5288. BUILDIN_FUNC(itemheal)
  5289. {
  5290. TBL_PC *sd;
  5291. int hp,sp;
  5292. hp=script_getnum(st,2);
  5293. sp=script_getnum(st,3);
  5294. if(potion_flag==1) {
  5295. potion_hp = hp;
  5296. potion_sp = sp;
  5297. return SCRIPT_CMD_SUCCESS;
  5298. }
  5299. if (!script_charid2sd(4,sd))
  5300. return SCRIPT_CMD_SUCCESS;
  5301. pc_itemheal(sd,sd->itemid,hp,sp);
  5302. return SCRIPT_CMD_SUCCESS;
  5303. }
  5304. /*==========================================
  5305. *
  5306. *------------------------------------------*/
  5307. BUILDIN_FUNC(percentheal)
  5308. {
  5309. int hp,sp;
  5310. TBL_PC* sd;
  5311. hp=script_getnum(st,2);
  5312. sp=script_getnum(st,3);
  5313. if(potion_flag==1) {
  5314. potion_per_hp = hp;
  5315. potion_per_sp = sp;
  5316. return SCRIPT_CMD_SUCCESS;
  5317. }
  5318. if (!script_charid2sd(4,sd))
  5319. return SCRIPT_CMD_SUCCESS;
  5320. #ifdef RENEWAL
  5321. if( sd->sc.getSCE(SC_EXTREMITYFIST2) )
  5322. sp = 0;
  5323. #endif
  5324. if (sd->sc.getSCE(SC_NORECOVER_STATE)) {
  5325. hp = 0;
  5326. sp = 0;
  5327. }
  5328. if (sd->sc.getSCE(SC_BITESCAR))
  5329. hp = 0;
  5330. pc_percentheal(sd,hp,sp);
  5331. return SCRIPT_CMD_SUCCESS;
  5332. }
  5333. /*==========================================
  5334. *
  5335. *------------------------------------------*/
  5336. BUILDIN_FUNC(jobchange)
  5337. {
  5338. int job, upper=-1;
  5339. job=script_getnum(st,2);
  5340. if( script_hasdata(st,3) )
  5341. upper=script_getnum(st,3);
  5342. if (pcdb_checkid(job))
  5343. {
  5344. TBL_PC* sd;
  5345. if (!script_charid2sd(4,sd))
  5346. return SCRIPT_CMD_SUCCESS;
  5347. pc_jobchange(sd, job, upper);
  5348. }
  5349. return SCRIPT_CMD_SUCCESS;
  5350. }
  5351. /*==========================================
  5352. *
  5353. *------------------------------------------*/
  5354. BUILDIN_FUNC(jobname)
  5355. {
  5356. int class_=script_getnum(st,2);
  5357. script_pushconststr(st, (char*)job_name(class_));
  5358. return SCRIPT_CMD_SUCCESS;
  5359. }
  5360. /// Get input from the player.
  5361. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5362. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5363. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5364. /// shorter than 'min' and 0 otherwise.
  5365. ///
  5366. /// input(<var>{,<min>{,<max>}}) -> <int>
  5367. BUILDIN_FUNC(input)
  5368. {
  5369. TBL_PC* sd;
  5370. struct script_data* data;
  5371. int64 uid;
  5372. const char* name;
  5373. int min;
  5374. int max;
  5375. if( !script_rid2sd(sd) )
  5376. return SCRIPT_CMD_SUCCESS;
  5377. data = script_getdata(st,2);
  5378. if( !data_isreference(data) ){
  5379. ShowError("script:input: not a variable\n");
  5380. script_reportdata(data);
  5381. st->state = END;
  5382. return SCRIPT_CMD_FAILURE;
  5383. }
  5384. uid = reference_getuid(data);
  5385. name = reference_getname(data);
  5386. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5387. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5388. #ifdef SECURE_NPCTIMEOUT
  5389. sd->npc_idle_type = NPCT_WAIT;
  5390. #endif
  5391. if( !sd->state.menu_or_input )
  5392. { // first invocation, display npc input box
  5393. sd->state.menu_or_input = 1;
  5394. st->state = RERUNLINE;
  5395. if( is_string_variable(name) )
  5396. clif_scriptinputstr(sd,st->oid);
  5397. else
  5398. clif_scriptinput(sd,st->oid);
  5399. }
  5400. else
  5401. { // take received text/value and store it in the designated variable
  5402. sd->state.menu_or_input = 0;
  5403. if( is_string_variable(name) )
  5404. {
  5405. int len = (int)strlen(sd->npc_str);
  5406. set_reg_str(st, sd, uid, name, sd->npc_str, script_getref(st,2));
  5407. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5408. }
  5409. else
  5410. {
  5411. int amount = sd->npc_amount;
  5412. set_reg_num(st, sd, uid, name, cap_value(amount,min,max), script_getref(st,2));
  5413. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5414. }
  5415. st->state = RUN;
  5416. }
  5417. return SCRIPT_CMD_SUCCESS;
  5418. }
  5419. // declare the copyarray method here for future reference
  5420. BUILDIN_FUNC(copyarray);
  5421. /// Sets the value of a variable.
  5422. /// The value is converted to the type of the variable.
  5423. ///
  5424. /// set(<variable>,<value>{,<charid>})
  5425. BUILDIN_FUNC(setr)
  5426. {
  5427. TBL_PC* sd = NULL;
  5428. struct script_data* data;
  5429. //struct script_data* datavalue;
  5430. int64 num;
  5431. uint8 pos = 4;
  5432. const char* name, *command = script_getfuncname(st);
  5433. char prefix;
  5434. data = script_getdata(st,2);
  5435. //datavalue = script_getdata(st,3);
  5436. if( !data_isreference(data) )
  5437. {
  5438. ShowError("script:set: not a variable\n");
  5439. script_reportdata(script_getdata(st,2));
  5440. st->state = END;
  5441. return SCRIPT_CMD_FAILURE;
  5442. }
  5443. num = reference_getuid(data);
  5444. name = reference_getname(data);
  5445. prefix = *name;
  5446. if (!strcmp(command, "setr"))
  5447. pos = 5;
  5448. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5449. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5450. return SCRIPT_CMD_FAILURE;
  5451. }
  5452. #if 0
  5453. if( data_isreference(datavalue) )
  5454. {// the value being referenced is a variable
  5455. const char* namevalue = reference_getname(datavalue);
  5456. if( !not_array_variable(*namevalue) )
  5457. {// array variable being copied into another array variable
  5458. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5459. {// player must be attached in order to copy a player variable
  5460. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5461. return SCRIPT_CMD_SUCCESS;
  5462. }
  5463. if( is_string_variable(namevalue) != is_string_variable(name) )
  5464. {// non-matching array value types
  5465. ShowWarning("script:set: two array variables do not match in type.\n");
  5466. return SCRIPT_CMD_SUCCESS;
  5467. }
  5468. // push the maximum number of array values to the stack
  5469. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5470. // call the copy array method directly
  5471. return buildin_copyarray(st);
  5472. }
  5473. }
  5474. #endif
  5475. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5476. if( is_string_variable(name) )
  5477. script_pushstrcopy(st,script_getstr(st, 4));
  5478. else
  5479. script_pushint(st,script_getnum64(st, 4));
  5480. } else // Return a copy of the variable reference
  5481. script_pushcopy(st, 2);
  5482. if( is_string_variable(name) )
  5483. set_reg_str( st, sd, num, name, script_getstr( st, 3 ), script_getref( st, 2 ) );
  5484. else
  5485. set_reg_num( st, sd, num, name, script_getnum64( st, 3 ), script_getref( st, 2 ) );
  5486. return SCRIPT_CMD_SUCCESS;
  5487. }
  5488. /////////////////////////////////////////////////////////////////////
  5489. /// Array variables
  5490. ///
  5491. /// Sets values of an array, from the starting index.
  5492. /// ex: setarray arr[1],1,2,3;
  5493. ///
  5494. /// setarray <array variable>,<value1>{,<value2>...};
  5495. BUILDIN_FUNC(setarray)
  5496. {
  5497. struct script_data* data;
  5498. const char* name;
  5499. uint32 start;
  5500. uint32 end;
  5501. int32 id;
  5502. int32 i;
  5503. TBL_PC* sd = NULL;
  5504. data = script_getdata(st, 2);
  5505. if( !data_isreference(data) )
  5506. {
  5507. ShowError("script:setarray: not a variable\n");
  5508. script_reportdata(data);
  5509. st->state = END;
  5510. return SCRIPT_CMD_FAILURE;// not a variable
  5511. }
  5512. id = reference_getid(data);
  5513. start = reference_getindex(data);
  5514. name = reference_getname(data);
  5515. if( not_server_variable(*name) )
  5516. {
  5517. if( !script_rid2sd(sd) )
  5518. return SCRIPT_CMD_SUCCESS;// no player attached
  5519. }
  5520. end = start + script_lastdata(st) - 2;
  5521. if( end > SCRIPT_MAX_ARRAYSIZE )
  5522. end = SCRIPT_MAX_ARRAYSIZE;
  5523. if( is_string_variable(name) )
  5524. {// string array
  5525. for( i = 3; start < end; ++start, ++i )
  5526. set_reg_str( st, sd, reference_uid( id, start ), name, script_getstr( st, i ), reference_getref( data ) );
  5527. }
  5528. else
  5529. {// int array
  5530. for( i = 3; start < end; ++start, ++i )
  5531. set_reg_num( st, sd, reference_uid( id, start ), name, script_getnum64( st, i ), reference_getref( data ) );
  5532. }
  5533. return SCRIPT_CMD_SUCCESS;
  5534. }
  5535. /// Sets count values of an array, from the starting index.
  5536. /// ex: cleararray arr[0],0,1;
  5537. ///
  5538. /// cleararray <array variable>,<value>,<count>;
  5539. BUILDIN_FUNC(cleararray)
  5540. {
  5541. struct script_data* data;
  5542. const char* name;
  5543. uint32 start;
  5544. uint32 end;
  5545. int32 id;
  5546. TBL_PC* sd = NULL;
  5547. data = script_getdata(st, 2);
  5548. if( !data_isreference(data) )
  5549. {
  5550. ShowError("script:cleararray: not a variable\n");
  5551. script_reportdata(data);
  5552. st->state = END;
  5553. return SCRIPT_CMD_FAILURE;// not a variable
  5554. }
  5555. id = reference_getid(data);
  5556. start = reference_getindex(data);
  5557. name = reference_getname(data);
  5558. if( not_server_variable(*name) )
  5559. {
  5560. if( !script_rid2sd(sd) )
  5561. return SCRIPT_CMD_SUCCESS;// no player attached
  5562. }
  5563. end = start + script_getnum(st, 4);
  5564. if( end > SCRIPT_MAX_ARRAYSIZE )
  5565. end = SCRIPT_MAX_ARRAYSIZE;
  5566. if( is_string_variable( name ) ){
  5567. const char* value = script_getstr( st, 3 );
  5568. for( ; start < end; ++start ){
  5569. set_reg_str( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5570. }
  5571. }else{
  5572. int64 value = script_getnum64( st, 3 );
  5573. for( ; start < end; ++start ){
  5574. set_reg_num( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5575. }
  5576. }
  5577. return SCRIPT_CMD_SUCCESS;
  5578. }
  5579. /// Copies data from one array to another.
  5580. /// ex: copyarray arr[0],arr[2],2;
  5581. ///
  5582. /// copyarray <destination array variable>,<source array variable>,<count>;
  5583. BUILDIN_FUNC(copyarray)
  5584. {
  5585. struct script_data* data1;
  5586. struct script_data* data2;
  5587. const char* name1;
  5588. const char* name2;
  5589. int32 idx1;
  5590. int32 idx2;
  5591. int32 id1;
  5592. int32 id2;
  5593. int32 i;
  5594. uint32 count;
  5595. TBL_PC* sd = NULL;
  5596. data1 = script_getdata(st, 2);
  5597. data2 = script_getdata(st, 3);
  5598. if( !data_isreference(data1) || !data_isreference(data2) )
  5599. {
  5600. ShowError("script:copyarray: not a variable\n");
  5601. script_reportdata(data1);
  5602. script_reportdata(data2);
  5603. st->state = END;
  5604. return SCRIPT_CMD_FAILURE;// not a variable
  5605. }
  5606. id1 = reference_getid(data1);
  5607. id2 = reference_getid(data2);
  5608. idx1 = reference_getindex(data1);
  5609. idx2 = reference_getindex(data2);
  5610. name1 = reference_getname(data1);
  5611. name2 = reference_getname(data2);
  5612. bool is_string = is_string_variable( name1 );
  5613. if( is_string != is_string_variable( name2 ) ){
  5614. ShowError("script:copyarray: type mismatch\n");
  5615. script_reportdata(data1);
  5616. script_reportdata(data2);
  5617. st->state = END;
  5618. return SCRIPT_CMD_FAILURE;// data type mismatch
  5619. }
  5620. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5621. {
  5622. if( !script_rid2sd(sd) )
  5623. return SCRIPT_CMD_SUCCESS;// no player attached
  5624. }
  5625. count = script_getnum(st, 4);
  5626. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5627. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5628. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5629. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5630. if( id1 == id2 && idx1 > idx2 ){
  5631. // destination might be overlapping the source - copy in reverse order
  5632. for( i = count - 1; i >= 0; --i ){
  5633. if( is_string ){
  5634. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5635. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5636. // Remove stack entry from get_val2_str
  5637. script_removetop( st, -1, 0 );
  5638. }else{
  5639. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5640. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5641. }
  5642. }
  5643. }else{
  5644. // normal copy
  5645. for( i = 0; i < count; ++i ){
  5646. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ){
  5647. if( is_string ){
  5648. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5649. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5650. // Remove stack entry from get_val2_str
  5651. script_removetop( st, -1, 0 );
  5652. }else{
  5653. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5654. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5655. }
  5656. }else{
  5657. // out of range - assume ""/0
  5658. clear_reg( st, sd, reference_uid( id1, idx1 + i ), name1, reference_getref( data1 ) );
  5659. }
  5660. }
  5661. }
  5662. return SCRIPT_CMD_SUCCESS;
  5663. }
  5664. /// Returns the size of the array.
  5665. /// Assumes that everything before the starting index exists.
  5666. /// ex: getarraysize(arr[3])
  5667. ///
  5668. /// getarraysize(<array variable>) -> <int>
  5669. BUILDIN_FUNC(getarraysize)
  5670. {
  5671. struct script_data* data;
  5672. const char* name;
  5673. map_session_data* sd = NULL;
  5674. data = script_getdata(st, 2);
  5675. if( !data_isreference(data) )
  5676. {
  5677. ShowError("script:getarraysize: not a variable\n");
  5678. script_reportdata(data);
  5679. script_pushnil(st);
  5680. st->state = END;
  5681. return SCRIPT_CMD_FAILURE;// not a variable
  5682. }
  5683. name = reference_getname(data);
  5684. if( not_server_variable(*name) ){
  5685. if (!script_rid2sd(sd))
  5686. return SCRIPT_CMD_SUCCESS;// no player attached
  5687. }
  5688. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5689. return SCRIPT_CMD_SUCCESS;
  5690. }
  5691. int script_array_index_cmp(const void *a, const void *b) {
  5692. return ( *(unsigned int*)a - *(unsigned int*)b );
  5693. }
  5694. /// Deletes count or all the elements in an array, from the starting index.
  5695. /// ex: deletearray arr[4],2;
  5696. ///
  5697. /// deletearray <array variable>;
  5698. /// deletearray <array variable>,<count>;
  5699. BUILDIN_FUNC(deletearray)
  5700. {
  5701. struct script_data* data;
  5702. const char* name;
  5703. unsigned int start, end, i;
  5704. int id;
  5705. TBL_PC *sd = NULL;
  5706. struct script_array *sa = NULL;
  5707. struct reg_db *src = NULL;
  5708. data = script_getdata(st, 2);
  5709. if( !data_isreference(data) ) {
  5710. ShowError("script:deletearray: not a variable\n");
  5711. script_reportdata(data);
  5712. st->state = END;
  5713. return SCRIPT_CMD_FAILURE;// not a variable
  5714. }
  5715. id = reference_getid(data);
  5716. start = reference_getindex(data);
  5717. name = reference_getname(data);
  5718. if( not_server_variable(*name) ) {
  5719. if( !script_rid2sd(sd) )
  5720. return SCRIPT_CMD_SUCCESS;// no player attached
  5721. }
  5722. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5723. ShowError("script:deletearray: not a array\n");
  5724. script_reportdata(data);
  5725. st->state = END;
  5726. return SCRIPT_CMD_FAILURE;// not a variable
  5727. }
  5728. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5729. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5730. return SCRIPT_CMD_SUCCESS;// not a variable
  5731. }
  5732. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5733. if( start >= end )
  5734. return SCRIPT_CMD_SUCCESS;// nothing to free
  5735. if( script_hasdata(st,3) ) {
  5736. unsigned int count = script_getnum(st, 3);
  5737. if( count > end - start )
  5738. count = end - start;
  5739. if( count <= 0 )
  5740. return SCRIPT_CMD_SUCCESS;// nothing to free
  5741. bool is_string = is_string_variable( name );
  5742. if( end - start < sa->size ) {
  5743. // Better to iterate directly on the array, no speed-up from using sa
  5744. for( ; start + count < end; ++start ) {
  5745. // Compact and overwrite
  5746. if( is_string ){
  5747. const char* value = get_val2_str( st, reference_uid( id, start + count ), reference_getref( data ) );
  5748. set_reg_str( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5749. // Remove stack entry from get_val2_str
  5750. script_removetop( st, -1, 0 );
  5751. }else{
  5752. int64 value = get_val2_num( st, reference_uid( id, start + count ), reference_getref( data ) );
  5753. set_reg_num( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5754. }
  5755. }
  5756. for( ; start < end; start++ ) {
  5757. // Clean up any leftovers that weren't overwritten
  5758. clear_reg( st, sd, reference_uid( id, start ), name, reference_getref( data ) );
  5759. }
  5760. } else {
  5761. // using sa to speed up
  5762. unsigned int *list = NULL, size = 0;
  5763. list = script_array_cpy_list(sa);
  5764. size = sa->size;
  5765. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5766. ARR_FIND(0, size, i, list[i] >= start);
  5767. for( ; i < size && list[i] < start + count; i++ ) {
  5768. // Clear any entries between start and start+count, if they exist
  5769. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5770. }
  5771. for( ; i < size && list[i] < end; i++ ) {
  5772. // Move back count positions any entries between start+count to fill the gaps
  5773. if( is_string ){
  5774. const char* value = get_val2_str( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5775. set_reg_str( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5776. // Remove stack entry from get_val2_str
  5777. script_removetop( st, -1, 0 );
  5778. }else{
  5779. int64 value = get_val2_num( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5780. set_reg_num( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5781. }
  5782. // Clear their originals
  5783. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5784. }
  5785. }
  5786. } else {
  5787. unsigned int *list = NULL, size = 0;
  5788. list = script_array_cpy_list(sa);
  5789. size = sa->size;
  5790. for(i = 0; i < size; i++) {
  5791. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5792. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5793. }
  5794. }
  5795. return SCRIPT_CMD_SUCCESS;
  5796. }
  5797. /// Returns a reference to the target index of the array variable.
  5798. /// Equivalent to var[index].
  5799. ///
  5800. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5801. BUILDIN_FUNC(getelementofarray)
  5802. {
  5803. struct script_data* data;
  5804. int32 id;
  5805. int64 i;
  5806. data = script_getdata(st, 2);
  5807. if( !data_isreference(data) )
  5808. {
  5809. ShowError("script:getelementofarray: not a variable\n");
  5810. script_reportdata(data);
  5811. script_pushnil(st);
  5812. st->state = END;
  5813. return SCRIPT_CMD_SUCCESS;// not a variable
  5814. }
  5815. id = reference_getid(data);
  5816. i = script_getnum(st, 3);
  5817. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5818. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5819. script_reportdata(data);
  5820. script_pushnil(st);
  5821. st->state = END;
  5822. return SCRIPT_CMD_FAILURE;// out of range
  5823. }
  5824. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5825. return SCRIPT_CMD_SUCCESS;
  5826. }
  5827. /// Return the index number of the first matching value in an array.
  5828. /// ex: inarray arr,1;
  5829. ///
  5830. /// inarray <array variable>,<value>;
  5831. BUILDIN_FUNC(inarray)
  5832. {
  5833. struct script_data *data;
  5834. const char* name;
  5835. int id;
  5836. map_session_data* sd = NULL;
  5837. struct reg_db *ref = NULL;
  5838. data = script_getdata(st, 2);
  5839. if (!data_isreference(data))
  5840. {
  5841. ShowError("buildin_inarray: not a variable\n");
  5842. script_reportdata(data);
  5843. st->state = END;
  5844. return SCRIPT_CMD_FAILURE;
  5845. }
  5846. name = reference_getname(data);
  5847. ref = reference_getref(data);
  5848. if (not_server_variable(*name) && !script_rid2sd(sd))
  5849. return SCRIPT_CMD_FAILURE;
  5850. const uint32 array_size = script_array_highest_key(st, sd, name, ref);
  5851. if (array_size == 0)
  5852. {
  5853. script_pushint(st, -1);
  5854. return SCRIPT_CMD_SUCCESS;
  5855. }
  5856. if (array_size >= SCRIPT_MAX_ARRAYSIZE)
  5857. {
  5858. ShowError("buildin_inarray: The array is too large.\n");
  5859. script_reportdata(data);
  5860. st->state = END;
  5861. return SCRIPT_CMD_FAILURE;
  5862. }
  5863. id = reference_getid(data);
  5864. if( is_string_variable( name ) ){
  5865. const char* value = script_getstr( st, 3 );
  5866. for( uint32 i = 0; i < array_size; ++i ){
  5867. const char* temp = get_val2_str( st, reference_uid( id, i ), ref );
  5868. if( !strcmp( temp, value ) ){
  5869. // Remove stack entry from get_val2_str
  5870. script_removetop( st, -1, 0 );
  5871. script_pushint( st, i );
  5872. return SCRIPT_CMD_SUCCESS;
  5873. }
  5874. // Remove stack entry from get_val2_str
  5875. script_removetop( st, -1, 0 );
  5876. }
  5877. }else{
  5878. int64 value = script_getnum64( st, 3 );
  5879. for( uint32 i = 0; i < array_size; ++i ){
  5880. int64 temp = get_val2_num( st, reference_uid( id, i ), ref );
  5881. if( temp == value ){
  5882. script_pushint( st, i );
  5883. return SCRIPT_CMD_SUCCESS;
  5884. }
  5885. }
  5886. }
  5887. script_pushint(st, -1);
  5888. return SCRIPT_CMD_SUCCESS;
  5889. }
  5890. /// Return the number of matches in two arrays.
  5891. /// ex: countinarray arr[0],arr1[0];
  5892. ///
  5893. /// countinarray <array variable>,<array variable>;
  5894. BUILDIN_FUNC(countinarray)
  5895. {
  5896. struct script_data *data1 , *data2;
  5897. const char* name1;
  5898. const char* name2;
  5899. int id1, id2, case_count = 0;
  5900. map_session_data* sd = NULL;
  5901. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5902. data1 = script_getdata(st, 2);
  5903. data2 = script_getdata(st, 3);
  5904. if (!data_isreference(data1) || !data_isreference(data2))
  5905. {
  5906. ShowError("buildin_countinarray: not a variable\n");
  5907. script_reportdata(data1);
  5908. script_reportdata(data2);
  5909. st->state = END;
  5910. return SCRIPT_CMD_FAILURE;
  5911. }
  5912. name1 = reference_getname(data1);
  5913. name2 = reference_getname(data2);
  5914. ref1 = reference_getref(data1);
  5915. ref2 = reference_getref(data2);
  5916. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5917. return SCRIPT_CMD_FAILURE;
  5918. const uint32 array_size1 = script_array_highest_key(st, sd, name1, ref1);
  5919. const uint32 array_size2 = script_array_highest_key(st, sd, name2, ref2);
  5920. if (array_size1 == 0 || array_size2 == 0)
  5921. {
  5922. script_pushint(st, 0);
  5923. return SCRIPT_CMD_SUCCESS;
  5924. }
  5925. if (array_size1 >= SCRIPT_MAX_ARRAYSIZE || array_size2 >= SCRIPT_MAX_ARRAYSIZE)
  5926. {
  5927. ShowError("buildin_countinarray: The array is too large.\n");
  5928. script_reportdata(data1);
  5929. script_reportdata(data2);
  5930. st->state = END;
  5931. return SCRIPT_CMD_FAILURE;
  5932. }
  5933. uint32 i = reference_getindex(data1);
  5934. uint32 j = reference_getindex(data2);
  5935. if (i > array_size1 - 1 || j > array_size2 - 1)
  5936. { //To prevent unintended behavior
  5937. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5938. script_reportdata(data1);
  5939. script_reportdata(data2);
  5940. st->state = END;
  5941. return SCRIPT_CMD_FAILURE;
  5942. }
  5943. id1 = reference_getid(data1);
  5944. id2 = reference_getid(data2);
  5945. if( is_string_variable( name1 ) && is_string_variable( name2 ) ){
  5946. for( ; i < array_size1; ++i ){
  5947. const char* temp1 = get_val2_str( st, reference_uid( id1, i ), ref1 );
  5948. for( j = reference_getindex( data2 ); j < array_size2; j++ ){
  5949. const char* temp2 = get_val2_str( st, reference_uid( id2, j ), ref2 );
  5950. if( !strcmp( temp1, temp2 ) ){
  5951. case_count++;
  5952. }
  5953. // Remove stack entry from get_val2_str
  5954. script_removetop( st, -1, 0 );
  5955. }
  5956. // Remove stack entry from get_val2_str
  5957. script_removetop( st, -1, 0 );
  5958. }
  5959. }else if( !is_string_variable( name1 ) && !is_string_variable( name2 ) ){
  5960. for( ; i < array_size1; ++i ){
  5961. int64 temp1 = get_val2_num( st, reference_uid( id1, i ), ref1 );
  5962. for( j = reference_getindex( data2 ); j < array_size2; j++ ){
  5963. int64 temp2 = get_val2_num( st, reference_uid( id2, j ), ref2 );
  5964. if( temp1 == temp2 ){
  5965. case_count++;
  5966. }
  5967. }
  5968. }
  5969. }else{
  5970. ShowError("buildin_countinarray: Arrays do not match , You can't compare an int array to a string array.\n");
  5971. script_reportdata(data1);
  5972. script_reportdata(data2);
  5973. st->state = END;
  5974. return SCRIPT_CMD_FAILURE;
  5975. }
  5976. script_pushint(st, case_count);
  5977. return SCRIPT_CMD_SUCCESS;
  5978. }
  5979. /////////////////////////////////////////////////////////////////////
  5980. /// ...
  5981. ///
  5982. /*==========================================
  5983. *
  5984. *------------------------------------------*/
  5985. BUILDIN_FUNC(setlook)
  5986. {
  5987. int type,val;
  5988. TBL_PC* sd;
  5989. type = script_getnum(st,2);
  5990. val = script_getnum(st,3);
  5991. if (!script_charid2sd(4,sd))
  5992. return SCRIPT_CMD_SUCCESS;
  5993. pc_changelook(sd,type,val);
  5994. return SCRIPT_CMD_SUCCESS;
  5995. }
  5996. BUILDIN_FUNC(changelook)
  5997. { // As setlook but only client side
  5998. int type,val;
  5999. TBL_PC* sd;
  6000. type = script_getnum(st,2);
  6001. val = script_getnum(st,3);
  6002. if (!script_charid2sd(4,sd))
  6003. return SCRIPT_CMD_SUCCESS;
  6004. switch(type) {
  6005. case LOOK_HAIR:
  6006. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  6007. break;
  6008. case LOOK_HAIR_COLOR:
  6009. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  6010. break;
  6011. case LOOK_CLOTHES_COLOR:
  6012. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  6013. break;
  6014. case LOOK_BODY2:
  6015. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  6016. break;
  6017. default:
  6018. break;
  6019. }
  6020. clif_changelook(&sd->bl, type, val);
  6021. return SCRIPT_CMD_SUCCESS;
  6022. }
  6023. /*==========================================
  6024. *
  6025. *------------------------------------------*/
  6026. BUILDIN_FUNC(cutin)
  6027. {
  6028. TBL_PC* sd;
  6029. if( !script_rid2sd(sd) )
  6030. return SCRIPT_CMD_SUCCESS;
  6031. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  6032. return SCRIPT_CMD_SUCCESS;
  6033. }
  6034. /*==========================================
  6035. *
  6036. *------------------------------------------*/
  6037. BUILDIN_FUNC(viewpoint)
  6038. {
  6039. int type,x,y,id,color;
  6040. TBL_PC* sd;
  6041. if (!script_charid2sd(7, sd)) {
  6042. st->state = END;
  6043. return SCRIPT_CMD_FAILURE;
  6044. }
  6045. type=script_getnum(st,2);
  6046. x=script_getnum(st,3);
  6047. y=script_getnum(st,4);
  6048. id=script_getnum(st,5);
  6049. color=script_getnum(st,6);
  6050. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  6051. return SCRIPT_CMD_SUCCESS;
  6052. }
  6053. static int buildin_viewpointmap_sub(block_list *bl, va_list ap) {
  6054. int oid, type, x, y, id, color;
  6055. oid = va_arg(ap, int);
  6056. type = va_arg(ap, int);
  6057. x = va_arg(ap, int);
  6058. y = va_arg(ap, int);
  6059. id = va_arg(ap, int);
  6060. color = va_arg(ap, int);
  6061. clif_viewpoint((map_session_data *)bl, oid, type, x, y, id, color);
  6062. return 0;
  6063. }
  6064. BUILDIN_FUNC(viewpointmap) {
  6065. const char* map = script_getstr(st, 2);
  6066. int16 mapid = map_mapname2mapid(map);
  6067. if (mapid < 0) {
  6068. ShowError("buildin_viewpointmap: Unknown map name %s.\n", map);
  6069. return SCRIPT_CMD_FAILURE;
  6070. }
  6071. 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));
  6072. return SCRIPT_CMD_SUCCESS;
  6073. }
  6074. /**
  6075. * Set random options for new item
  6076. * @param st Script state
  6077. * @param it Temporary item data
  6078. * @param funcname Function name
  6079. * @param x First position of random option id array from the script
  6080. **/
  6081. static bool script_getitem_randomoption(struct script_state *st, map_session_data* sd, struct item *it, const char *funcname, int x) {
  6082. int i, opt_id_n;
  6083. struct script_data *opt_id = script_getdata(st,x);
  6084. struct script_data *opt_val = script_getdata(st,x+1);
  6085. struct script_data *opt_param = script_getdata(st,x+2);
  6086. const char *opt_id_var = reference_getname(opt_id);
  6087. const char *opt_val_var = reference_getname(opt_val);
  6088. const char *opt_param_var = reference_getname(opt_param);
  6089. int32 opt_id_id, opt_id_idx;
  6090. int32 opt_val_id, opt_val_idx;
  6091. int32 opt_param_id, opt_param_idx;
  6092. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  6093. // Check if the variable requires a player
  6094. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  6095. // If no player is attached
  6096. if( !script_rid2sd(sd) ){
  6097. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  6098. return false;
  6099. }
  6100. }
  6101. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  6102. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  6103. return false;
  6104. }
  6105. if (is_string_variable(opt_id_var)) {
  6106. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  6107. return false;
  6108. }
  6109. // Check if the variable requires a player
  6110. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  6111. // If no player is attached
  6112. if( !script_rid2sd(sd) ){
  6113. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  6114. return false;
  6115. }
  6116. }
  6117. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  6118. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  6119. return false;
  6120. }
  6121. if (is_string_variable(opt_val_var)) {
  6122. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6123. return false;
  6124. }
  6125. // Check if the variable requires a player
  6126. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  6127. // If no player is attached
  6128. if( !script_rid2sd(sd) ){
  6129. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  6130. return false;
  6131. }
  6132. }
  6133. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  6134. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  6135. return false;
  6136. }
  6137. if (is_string_variable(opt_param_var)) {
  6138. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6139. return false;
  6140. }
  6141. opt_id_ref = reference_getref(opt_id);
  6142. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  6143. opt_val_ref = reference_getref(opt_val);
  6144. opt_param_ref = reference_getref(opt_param);
  6145. opt_id_id = reference_getid(opt_id);
  6146. opt_val_id = reference_getid(opt_val);
  6147. opt_param_id = reference_getid(opt_param);
  6148. opt_id_idx = reference_getindex(opt_id);
  6149. opt_val_idx = reference_getindex(opt_val);
  6150. opt_param_idx = reference_getindex(opt_param);
  6151. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6152. it->option[i].id = (short)get_val2_num( st, reference_uid( opt_id_id, opt_id_idx + i ), opt_id_ref );
  6153. it->option[i].value = (short)get_val2_num( st, reference_uid( opt_val_id, opt_val_idx + i ), opt_val_ref );
  6154. it->option[i].param = (char)get_val2_num( st, reference_uid( opt_param_id, opt_param_idx + i ), opt_param_ref );
  6155. }
  6156. return true;
  6157. }
  6158. /**
  6159. * Sub function for counting items
  6160. * @param items: Item array to search
  6161. * @param id: Item data to search for
  6162. * @param size: Maximum size of array
  6163. * @param expanded: If the script command has extra arguments
  6164. * @param random_option: If the struct command has random option arguments
  6165. * @param st: Script state (required for random options)
  6166. * @param sd: Player data (required for random options)
  6167. * @param rental: Whether or not to count rental items
  6168. * @return Total count of item being searched
  6169. */
  6170. static int script_countitem_sub(struct item *items, std::shared_ptr<item_data> id, int size, int expanded, struct script_state *st, map_session_data *sd = nullptr, bool rental = false) {
  6171. nullpo_retr(-1, items);
  6172. nullpo_retr(-1, st);
  6173. int count = 0;
  6174. if (!expanded) { // For non-expanded functions
  6175. t_itemid nameid = id->nameid;
  6176. for (int i = 0; i < size; i++) {
  6177. item *itm = &items[i];
  6178. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1)
  6179. continue;
  6180. if (itm->nameid == nameid && ((rental && itm->expire_time > 0) || (!rental && itm->expire_time == 0)))
  6181. count += itm->amount;
  6182. }
  6183. } else { // For expanded functions
  6184. item it = {};
  6185. int offset = 10;
  6186. it.nameid = id->nameid;
  6187. it.identify = script_getnum(st,3);
  6188. it.refine = script_getnum(st,4);
  6189. it.attribute = script_getnum(st,5);
  6190. it.card[0] = script_getnum(st,6);
  6191. it.card[1] = script_getnum(st,7);
  6192. it.card[2] = script_getnum(st,8);
  6193. it.card[3] = script_getnum(st,9);
  6194. if (expanded&4) {
  6195. it.enchantgrade = script_getnum(st,10);
  6196. offset = 11;
  6197. }
  6198. if (expanded&2) {
  6199. if (!sd) {
  6200. ShowError("buildin_countitem3: Player not attached.\n");
  6201. return -1;
  6202. }
  6203. bool res = script_getitem_randomoption(st, sd, &it, "countitem3", offset);
  6204. if (!res)
  6205. return -1;
  6206. }
  6207. for (int i = 0; i < size; i++) {
  6208. item *itm = &items[i];
  6209. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1)
  6210. continue;
  6211. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute || itm->enchantgrade != it.enchantgrade)
  6212. continue;
  6213. if ((!rental && itm->expire_time > 0) || (rental && itm->expire_time == 0))
  6214. continue;
  6215. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6216. continue;
  6217. if (expanded&2) {
  6218. uint8 j;
  6219. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6220. 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)
  6221. break;
  6222. }
  6223. if (j != MAX_ITEM_RDM_OPT)
  6224. continue;
  6225. }
  6226. count += items[i].amount;
  6227. }
  6228. }
  6229. return count;
  6230. }
  6231. /**
  6232. * Returns number of items in inventory
  6233. * countitem(<nameID>{,<accountID>})
  6234. * countitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6235. * countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6236. * countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6237. * countitem4(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6238. * countitem4("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6239. */
  6240. BUILDIN_FUNC(countitem)
  6241. {
  6242. TBL_PC *sd;
  6243. char *command = (char *)script_getfuncname(st);
  6244. int aid = 3;
  6245. int expanded = 0;
  6246. if (command[strlen(command)-1] == '2') {
  6247. expanded = 1;
  6248. aid = 10;
  6249. }
  6250. else if (command[strlen(command)-1] == '3') {
  6251. expanded = 3;
  6252. aid = 13;
  6253. }
  6254. else if (command[strlen(command)-1] == '4') {
  6255. expanded = 7;
  6256. aid = 14;
  6257. }
  6258. if (!script_accid2sd(aid, sd))
  6259. return SCRIPT_CMD_FAILURE;
  6260. std::shared_ptr<item_data> id;
  6261. if (script_isstring(st, 2)) // item name
  6262. id = item_db.searchname( script_getstr( st, 2 ) );
  6263. else // item id
  6264. id = item_db.find( script_getnum( st, 2 ) );
  6265. if (!id) {
  6266. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6267. script_pushint(st, 0);
  6268. return SCRIPT_CMD_FAILURE;
  6269. }
  6270. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, expanded, st, sd);
  6271. if (count < 0) {
  6272. st->state = END;
  6273. return SCRIPT_CMD_FAILURE;
  6274. }
  6275. script_pushint(st, count);
  6276. return SCRIPT_CMD_SUCCESS;
  6277. }
  6278. /**
  6279. * Returns number of items in cart
  6280. * cartcountitem(<nameID>{,<accountID>})
  6281. * cartcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6282. */
  6283. BUILDIN_FUNC(cartcountitem)
  6284. {
  6285. TBL_PC *sd;
  6286. char *command = (char *)script_getfuncname(st);
  6287. int aid = 3;
  6288. int expanded = 0;
  6289. if (command[strlen(command) - 1] == '2') {
  6290. expanded = 1;
  6291. aid = 10;
  6292. }
  6293. if (!script_accid2sd(aid, sd))
  6294. return SCRIPT_CMD_FAILURE;
  6295. if (!pc_iscarton(sd)) {
  6296. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6297. script_pushint(st, -1);
  6298. return SCRIPT_CMD_FAILURE;
  6299. }
  6300. std::shared_ptr<item_data> id;
  6301. if (script_isstring(st, 2)) // item name
  6302. id = item_db.searchname( script_getstr( st, 2 ) );
  6303. else // item id
  6304. id = item_db.find( script_getnum( st, 2 ) );
  6305. if (!id) {
  6306. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6307. script_pushint(st, 0);
  6308. return SCRIPT_CMD_FAILURE;
  6309. }
  6310. int count = script_countitem_sub(sd->cart.u.items_cart, id, MAX_CART, expanded, st);
  6311. if (count < 0) {
  6312. st->state = END;
  6313. return SCRIPT_CMD_FAILURE;
  6314. }
  6315. script_pushint(st, count);
  6316. return SCRIPT_CMD_SUCCESS;
  6317. }
  6318. /**
  6319. * Returns number of items in storage
  6320. * storagecountitem(<nameID>{,<accountID>})
  6321. * storagecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6322. */
  6323. BUILDIN_FUNC(storagecountitem)
  6324. {
  6325. TBL_PC *sd;
  6326. char *command = (char *)script_getfuncname(st);
  6327. int aid = 3;
  6328. int expanded = 0;
  6329. if (command[strlen(command) - 1] == '2') {
  6330. expanded = 1;
  6331. aid = 10;
  6332. }
  6333. if (!script_accid2sd(aid, sd))
  6334. return SCRIPT_CMD_FAILURE;
  6335. std::shared_ptr<item_data> id;
  6336. if (script_isstring(st, 2)) // item name
  6337. id = item_db.searchname( script_getstr( st, 2 ) );
  6338. else // item id
  6339. id = item_db.find( script_getnum( st, 2 ) );
  6340. if (!id) {
  6341. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6342. script_pushint(st, 0);
  6343. return SCRIPT_CMD_FAILURE;
  6344. }
  6345. if (sd->state.storage_flag != 0) {
  6346. script_pushint(st, -1);
  6347. return SCRIPT_CMD_SUCCESS;
  6348. }
  6349. int count = script_countitem_sub(sd->storage.u.items_storage, id, MAX_STORAGE, expanded, st);
  6350. if (count < 0) {
  6351. st->state = END;
  6352. return SCRIPT_CMD_FAILURE;
  6353. }
  6354. script_pushint(st, count);
  6355. return SCRIPT_CMD_SUCCESS;
  6356. }
  6357. /**
  6358. * Returns number of items in guild storage
  6359. * guildstoragecountitem(<nameID>{,<accountID>})
  6360. * guildstoragecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6361. */
  6362. BUILDIN_FUNC(guildstoragecountitem)
  6363. {
  6364. TBL_PC *sd;
  6365. char *command = (char *)script_getfuncname(st);
  6366. int aid = 3;
  6367. int expanded = 0;
  6368. if (command[strlen(command) - 1] == '2') {
  6369. expanded = 1;
  6370. aid = 10;
  6371. }
  6372. if (!script_accid2sd(aid, sd))
  6373. return SCRIPT_CMD_FAILURE;
  6374. std::shared_ptr<item_data> id;
  6375. if (script_isstring(st, 2)) // item name
  6376. id = item_db.searchname( script_getstr( st, 2 ) );
  6377. else // item id
  6378. id = item_db.find( script_getnum( st, 2 ) );
  6379. if (!id) {
  6380. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6381. script_pushint(st, 0);
  6382. return SCRIPT_CMD_FAILURE;
  6383. }
  6384. struct s_storage *gstor = guild2storage(sd->status.guild_id);
  6385. if (!gstor || (gstor && sd->state.storage_flag != 0)) {
  6386. script_pushint(st, -1);
  6387. return SCRIPT_CMD_SUCCESS;
  6388. }
  6389. gstor->lock = true;
  6390. int count = script_countitem_sub(gstor->u.items_guild, id, MAX_GUILD_STORAGE, expanded, st);
  6391. storage_guild_storageclose(sd);
  6392. gstor->lock = false;
  6393. if (count < 0) {
  6394. st->state = END;
  6395. return SCRIPT_CMD_FAILURE;
  6396. }
  6397. script_pushint(st, count);
  6398. return SCRIPT_CMD_SUCCESS;
  6399. }
  6400. /**
  6401. * Returns number of rental items in inventory
  6402. * rentalcountitem(<nameID>{,<accountID>})
  6403. * rentalcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6404. * rentalcountitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6405. * rentalcountitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6406. */
  6407. BUILDIN_FUNC(rentalcountitem)
  6408. {
  6409. char *command = (char *)script_getfuncname(st);
  6410. int aid = 3;
  6411. int expanded = 0;
  6412. if (command[strlen(command) - 1] == '2') {
  6413. expanded = 1;
  6414. aid = 10;
  6415. }
  6416. else if (command[strlen(command) - 1] == '3') {
  6417. expanded = 3;
  6418. aid = 13;
  6419. }
  6420. else if (command[strlen(command) - 1] == '4') {
  6421. expanded = 7;
  6422. aid = 14;
  6423. }
  6424. map_session_data *sd;
  6425. if (!script_accid2sd(aid, sd))
  6426. return SCRIPT_CMD_FAILURE;
  6427. std::shared_ptr<item_data> id;
  6428. if (script_isstring(st, 2)) // item name
  6429. id = item_db.searchname( script_getstr( st, 2 ) );
  6430. else // item id
  6431. id = item_db.find( script_getnum( st, 2 ) );
  6432. if (!id) {
  6433. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6434. script_pushint(st, 0);
  6435. return SCRIPT_CMD_FAILURE;
  6436. }
  6437. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, expanded, st, sd, true);
  6438. if (count < 0) {
  6439. st->state = END;
  6440. return SCRIPT_CMD_FAILURE;
  6441. }
  6442. script_pushint(st, count);
  6443. return SCRIPT_CMD_SUCCESS;
  6444. }
  6445. /*==========================================
  6446. * Check if item with this amount can fit in inventory
  6447. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6448. * Return
  6449. * 0 : fail
  6450. * 1 : success (npc side only)
  6451. *------------------------------------------*/
  6452. BUILDIN_FUNC(checkweight)
  6453. {
  6454. int slots = 0;
  6455. unsigned short amount2 = 0;
  6456. unsigned int weight = 0, i, nbargs;
  6457. std::shared_ptr<item_data> id;
  6458. map_session_data* sd;
  6459. if( !script_rid2sd(sd) )
  6460. return SCRIPT_CMD_SUCCESS;
  6461. nbargs = script_lastdata(st)+1;
  6462. if(nbargs%2) {
  6463. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6464. script_pushint(st,0);
  6465. return SCRIPT_CMD_FAILURE;
  6466. }
  6467. slots = pc_inventoryblank(sd); //nb of empty slot
  6468. for (i = 2; i < nbargs; i += 2) {
  6469. t_itemid nameid;
  6470. unsigned short amount;
  6471. if( script_isstring(st, i) ) // item name
  6472. id = item_db.searchname( script_getstr( st, i ) );
  6473. else // item id
  6474. id = item_db.find( script_getnum( st, i ) );
  6475. if( id == nullptr ){
  6476. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6477. script_pushint(st,0);
  6478. return SCRIPT_CMD_FAILURE;
  6479. }
  6480. nameid = id->nameid;
  6481. amount = script_getnum(st,i+1);
  6482. if( amount < 1 ) {
  6483. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6484. script_pushint(st,0);
  6485. return SCRIPT_CMD_FAILURE;
  6486. }
  6487. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6488. if( weight + sd->weight > sd->max_weight )
  6489. {// too heavy
  6490. script_pushint(st,0);
  6491. return SCRIPT_CMD_SUCCESS;
  6492. }
  6493. switch( pc_checkadditem(sd, nameid, amount) ) {
  6494. case CHKADDITEM_EXIST:
  6495. // item is already in inventory, but there is still space for the requested amount
  6496. break;
  6497. case CHKADDITEM_NEW:
  6498. if( itemdb_isstackable(nameid) ) {
  6499. // stackable
  6500. amount2++;
  6501. if( slots < amount2 ) {
  6502. script_pushint(st,0);
  6503. return SCRIPT_CMD_SUCCESS;
  6504. }
  6505. } else {
  6506. // non-stackable
  6507. amount2 += amount;
  6508. if( slots < amount2) {
  6509. script_pushint(st,0);
  6510. return SCRIPT_CMD_SUCCESS;
  6511. }
  6512. }
  6513. break;
  6514. case CHKADDITEM_OVERAMOUNT:
  6515. script_pushint(st,0);
  6516. return SCRIPT_CMD_SUCCESS;
  6517. }
  6518. }
  6519. script_pushint(st,1);
  6520. return SCRIPT_CMD_SUCCESS;
  6521. }
  6522. BUILDIN_FUNC(checkweight2)
  6523. {
  6524. //variable sub checkweight
  6525. int i = 0, slots = 0, weight = 0;
  6526. short fail = 0;
  6527. unsigned short amount2 = 0;
  6528. //variable for array parsing
  6529. struct script_data* data_it;
  6530. struct script_data* data_nb;
  6531. const char* name_it;
  6532. const char* name_nb;
  6533. int32 id_it, id_nb;
  6534. int32 idx_it, idx_nb;
  6535. int nb_it, nb_nb; //array size
  6536. TBL_PC *sd;
  6537. if( !script_rid2sd(sd) )
  6538. return SCRIPT_CMD_SUCCESS;
  6539. data_it = script_getdata(st, 2);
  6540. data_nb = script_getdata(st, 3);
  6541. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6542. ShowError("buildin_checkweight2: parameter not a variable\n");
  6543. script_pushint(st,0);
  6544. return SCRIPT_CMD_FAILURE;// not a variable
  6545. }
  6546. id_it = reference_getid(data_it);
  6547. id_nb = reference_getid(data_nb);
  6548. idx_it = reference_getindex(data_it);
  6549. idx_nb = reference_getindex(data_nb);
  6550. name_it = reference_getname(data_it);
  6551. name_nb = reference_getname(data_nb);
  6552. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6553. ShowError("buildin_checkweight2: illegal type, need int\n");
  6554. script_pushint(st,0);
  6555. return SCRIPT_CMD_FAILURE;// not supported
  6556. }
  6557. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6558. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6559. if(nb_it != nb_nb) {
  6560. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6561. fail = 1;
  6562. }
  6563. slots = pc_inventoryblank(sd);
  6564. for(i=0; i<nb_it; i++) {
  6565. t_itemid nameid = (t_itemid)get_val2_num( st, reference_uid( id_it, idx_it + i ), reference_getref( data_it ) );
  6566. uint16 amount = (uint16)get_val2_num( st, reference_uid( id_nb, idx_nb + i ), reference_getref( data_nb ) );
  6567. if(fail)
  6568. continue; //cpntonie to depop rest
  6569. if (!item_db.exists(nameid)) {
  6570. ShowError("buildin_checkweight2: Invalid item '%u'.\n", nameid);
  6571. fail=1;
  6572. continue;
  6573. }
  6574. if(amount == 0 ) {
  6575. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6576. fail = 1;
  6577. continue;
  6578. }
  6579. weight += itemdb_weight(nameid)*amount;
  6580. if( weight + sd->weight > sd->max_weight ) {
  6581. fail = 1;
  6582. continue;
  6583. }
  6584. switch( pc_checkadditem(sd, nameid, amount) ) {
  6585. case CHKADDITEM_EXIST:
  6586. // item is already in inventory, but there is still space for the requested amount
  6587. break;
  6588. case CHKADDITEM_NEW:
  6589. if( itemdb_isstackable(nameid) ) {// stackable
  6590. amount2++;
  6591. if( slots < amount2 )
  6592. fail = 1;
  6593. } else {// non-stackable
  6594. amount2 += amount;
  6595. if( slots < amount2 ) {
  6596. fail = 1;
  6597. }
  6598. }
  6599. break;
  6600. case CHKADDITEM_OVERAMOUNT:
  6601. fail = 1;
  6602. } //end switch
  6603. } //end loop DO NOT break it prematurly we need to depop all stack
  6604. fail ? script_pushint(st,0) : script_pushint(st,1);
  6605. return SCRIPT_CMD_SUCCESS;
  6606. }
  6607. /*==========================================
  6608. * getitem <item id>,<amount>{,<account ID>};
  6609. * getitem "<item name>",<amount>{,<account ID>};
  6610. *
  6611. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6612. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6613. * Type:
  6614. * 0 - No bound
  6615. * 1 - Account Bound
  6616. * 2 - Guild Bound
  6617. * 3 - Party Bound
  6618. * 4 - Character Bound
  6619. *------------------------------------------*/
  6620. BUILDIN_FUNC(getitem)
  6621. {
  6622. int get_count, i;
  6623. t_itemid nameid;
  6624. unsigned short amount;
  6625. struct item it;
  6626. map_session_data *sd;
  6627. unsigned char flag = 0;
  6628. const char* command = script_getfuncname(st);
  6629. std::shared_ptr<item_data> id;
  6630. if( script_isstring(st, 2) ) {// "<item name>"
  6631. const char *name = script_getstr(st, 2);
  6632. id = item_db.searchname( name );
  6633. if( id == nullptr ){
  6634. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6635. return SCRIPT_CMD_FAILURE; //No item created.
  6636. }
  6637. nameid = id->nameid;
  6638. } else {// <item id>
  6639. nameid = script_getnum(st, 2);
  6640. id = item_db.find( nameid );
  6641. if( id == nullptr ){
  6642. ShowError("buildin_getitem: Nonexistant item %u requested.\n", nameid);
  6643. return SCRIPT_CMD_FAILURE; //No item created.
  6644. }
  6645. }
  6646. // <amount>
  6647. if( (amount = script_getnum(st,3)) <= 0)
  6648. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6649. memset(&it,0,sizeof(it));
  6650. it.nameid = nameid;
  6651. it.identify = 1;
  6652. it.bound = BOUND_NONE;
  6653. if( !strcmp(command,"getitembound") ) {
  6654. char bound = script_getnum(st,4);
  6655. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6656. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6657. return SCRIPT_CMD_FAILURE;
  6658. }
  6659. script_mapid2sd(5,sd);
  6660. it.bound = bound;
  6661. } else {
  6662. script_mapid2sd(4,sd);
  6663. }
  6664. if( sd == NULL ) // no target
  6665. return SCRIPT_CMD_SUCCESS;
  6666. //Check if it's stackable.
  6667. if( !itemdb_isstackable2( id.get() ) ){
  6668. get_count = 1;
  6669. }else{
  6670. get_count = amount;
  6671. }
  6672. for (i = 0; i < amount; i += get_count)
  6673. {
  6674. // if not pet egg
  6675. if (!pet_create_egg(sd, nameid))
  6676. {
  6677. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6678. {
  6679. clif_additem(sd, 0, 0, flag);
  6680. if( pc_candrop(sd,&it) )
  6681. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6682. }
  6683. }
  6684. }
  6685. return SCRIPT_CMD_SUCCESS;
  6686. }
  6687. /*==========================================
  6688. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6689. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6690. *
  6691. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6692. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6693. *
  6694. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6695. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6696. *
  6697. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6698. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6699. *
  6700. * getitem4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6701. * getitem4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6702. *
  6703. * getitembound4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6704. * getitembound4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6705. Type:
  6706. * 0 - No bound
  6707. * 1 - Account Bound
  6708. * 2 - Guild Bound
  6709. * 3 - Party Bound
  6710. * 4 - Character Bound
  6711. *------------------------------------------*/
  6712. BUILDIN_FUNC(getitem2)
  6713. {
  6714. TBL_PC *sd;
  6715. char bound = BOUND_NONE;
  6716. const char* command = script_getfuncname(st);
  6717. int offset = 0;
  6718. int grade_offset = 0;
  6719. if( !strncmp(command,"getitembound",12) ) {
  6720. int aid_pos = 12;
  6721. bound = script_getnum(st,11);
  6722. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6723. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6724. return SCRIPT_CMD_FAILURE;
  6725. }
  6726. if (command[strlen(command)-1] == '3') {
  6727. offset = 12;
  6728. aid_pos = 15;
  6729. }
  6730. else if (command[strlen(command)-1] == '4') {
  6731. grade_offset = 12;
  6732. offset = 13;
  6733. aid_pos = 16;
  6734. }
  6735. script_mapid2sd(aid_pos,sd);
  6736. } else {
  6737. int aid_pos = 11;
  6738. if (strcmpi(command,"getitem3") == 0) {
  6739. offset = 11;
  6740. aid_pos = 14;
  6741. }
  6742. if (strcmpi(command,"getitem4") == 0) {
  6743. grade_offset = 11;
  6744. offset = 12;
  6745. aid_pos = 15;
  6746. }
  6747. script_mapid2sd(aid_pos,sd);
  6748. }
  6749. if( sd == nullptr ) // no target
  6750. return SCRIPT_CMD_SUCCESS;
  6751. t_itemid nameid;
  6752. std::shared_ptr<item_data> item_data;
  6753. if( script_isstring(st, 2) ) {
  6754. const char *name = script_getstr(st, 2);
  6755. item_data = item_db.searchname( name );
  6756. if( item_data == nullptr ){
  6757. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6758. return SCRIPT_CMD_FAILURE; //No item created.
  6759. }
  6760. nameid = item_data->nameid;
  6761. } else {
  6762. nameid = script_getnum(st, 2);
  6763. item_data = item_db.find( nameid );
  6764. if( item_data == nullptr ){
  6765. ShowError("buildin_getitem2: Nonexistant item %u requested (by conv_num).\n", nameid);
  6766. return SCRIPT_CMD_FAILURE; //No item created.
  6767. }
  6768. }
  6769. int amount = script_getnum(st,3);
  6770. int iden = script_getnum(st,4);
  6771. int ref = script_getnum(st,5);
  6772. int attr = script_getnum(st,6);
  6773. t_itemid c1 = script_getnum(st,7);
  6774. t_itemid c2 = script_getnum(st,8);
  6775. t_itemid c3 = script_getnum(st,9);
  6776. t_itemid c4 = script_getnum(st,10);
  6777. struct item item_tmp = {};
  6778. if( item_data ) {
  6779. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6780. if(ref > MAX_REFINE)
  6781. ref = MAX_REFINE;
  6782. }
  6783. else if( item_data->type == IT_PETEGG ) {
  6784. iden = 1;
  6785. ref = 0;
  6786. }
  6787. else {
  6788. iden = 1;
  6789. ref = attr = 0;
  6790. }
  6791. item_tmp.nameid = nameid;
  6792. item_tmp.identify = iden;
  6793. item_tmp.refine = ref;
  6794. item_tmp.attribute = attr;
  6795. item_tmp.card[0] = c1;
  6796. item_tmp.card[1] = c2;
  6797. item_tmp.card[2] = c3;
  6798. item_tmp.card[3] = c4;
  6799. item_tmp.bound = bound;
  6800. if (offset != 0) {
  6801. bool res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6802. if (!res)
  6803. return SCRIPT_CMD_FAILURE;
  6804. if (grade_offset > 0) {
  6805. int grade = script_getnum(st, grade_offset);
  6806. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  6807. ShowError("buildin_getitem2: Not a correct grade! Grade=%d\n", grade);
  6808. return SCRIPT_CMD_FAILURE;
  6809. }
  6810. item_tmp.enchantgrade = static_cast<e_enchantgrade>(grade);
  6811. }
  6812. }
  6813. int get_count = 0;
  6814. //Check if it's stackable.
  6815. if( !itemdb_isstackable2( item_data.get() ) ){
  6816. get_count = 1;
  6817. }else{
  6818. get_count = amount;
  6819. }
  6820. for (int i = 0; i < amount; i += get_count)
  6821. {
  6822. // if not pet egg
  6823. if (!pet_create_egg(sd, nameid))
  6824. {
  6825. unsigned char flag = 0;
  6826. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6827. {
  6828. clif_additem(sd, 0, 0, flag);
  6829. if( pc_candrop(sd,&item_tmp) )
  6830. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6831. }
  6832. }
  6833. }
  6834. }
  6835. return SCRIPT_CMD_SUCCESS;
  6836. }
  6837. /** Gives rental item to player
  6838. * rentitem <item id>,<seconds>{,<account_id>}
  6839. * rentitem "<item name>",<seconds>{,<account_id>}
  6840. */
  6841. BUILDIN_FUNC(rentitem) {
  6842. map_session_data *sd;
  6843. t_itemid nameid = 0;
  6844. unsigned char flag = 0;
  6845. if (!script_accid2sd(4,sd))
  6846. return SCRIPT_CMD_FAILURE;
  6847. if( script_isstring(st, 2) )
  6848. {
  6849. const char *name = script_getstr(st, 2);
  6850. std::shared_ptr<item_data> itd = item_db.searchname( name );
  6851. if( itd == nullptr ){
  6852. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6853. return SCRIPT_CMD_FAILURE;
  6854. }
  6855. nameid = itd->nameid;
  6856. }
  6857. else
  6858. {
  6859. nameid = script_getnum(st, 2);
  6860. if( !item_db.exists(nameid) )
  6861. {
  6862. ShowError("buildin_rentitem: Nonexistant item %u requested.\n", nameid);
  6863. return SCRIPT_CMD_FAILURE;
  6864. }
  6865. }
  6866. int seconds = script_getnum(st,3);
  6867. struct item it = {};
  6868. it.nameid = nameid;
  6869. it.identify = 1;
  6870. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6871. it.bound = BOUND_NONE;
  6872. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6873. {
  6874. clif_additem(sd, 0, 0, flag);
  6875. return SCRIPT_CMD_FAILURE;
  6876. }
  6877. return SCRIPT_CMD_SUCCESS;
  6878. }
  6879. /**
  6880. * Gives rental item to player with advanced option
  6881. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6882. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6883. *
  6884. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6885. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6886. *
  6887. * rentitem4 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6888. * rentitem4 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6889. */
  6890. BUILDIN_FUNC(rentitem2) {
  6891. map_session_data *sd;
  6892. const char *funcname = script_getfuncname(st);
  6893. if (funcname[strlen(funcname)-1] == '3') {
  6894. if (!script_accid2sd(14,sd))
  6895. return SCRIPT_CMD_FAILURE;
  6896. } else if (funcname[strlen(funcname)-1] == '4') {
  6897. if (!script_accid2sd(15,sd))
  6898. return SCRIPT_CMD_FAILURE;
  6899. } else if (!script_accid2sd(11,sd))
  6900. return SCRIPT_CMD_FAILURE;
  6901. std::shared_ptr<item_data> id;
  6902. t_itemid nameid = 0;
  6903. if( script_isstring(st, 2) ) {
  6904. const char *name = script_getstr(st, 2);
  6905. id = item_db.searchname( name );
  6906. if( id == nullptr ) {
  6907. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6908. return SCRIPT_CMD_FAILURE;
  6909. }
  6910. nameid = id->nameid;
  6911. } else {
  6912. nameid = script_getnum(st, 2);
  6913. id = item_db.find( nameid );
  6914. if( id == nullptr ){
  6915. ShowError("buildin_rentitem2: Nonexistant item %u requested.\n", nameid);
  6916. return SCRIPT_CMD_FAILURE;
  6917. }
  6918. }
  6919. int seconds = script_getnum(st,3);
  6920. int iden = script_getnum(st,4);
  6921. int ref = script_getnum(st,5);
  6922. int attr = script_getnum(st,6);
  6923. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6924. if(ref > MAX_REFINE)
  6925. ref = MAX_REFINE;
  6926. }
  6927. else if (id->type==IT_PETEGG) {
  6928. iden = 1;
  6929. ref = 0;
  6930. }
  6931. else {
  6932. iden = 1;
  6933. ref = attr = 0;
  6934. }
  6935. t_itemid c1 = script_getnum(st,7);
  6936. t_itemid c2 = script_getnum(st,8);
  6937. t_itemid c3 = script_getnum(st,9);
  6938. t_itemid c4 = script_getnum(st,10);
  6939. struct item it = {};
  6940. it.nameid = nameid;
  6941. it.identify = iden;
  6942. it.refine = ref;
  6943. it.attribute = attr;
  6944. it.card[0] = c1;
  6945. it.card[1] = c2;
  6946. it.card[2] = c3;
  6947. it.card[3] = c4;
  6948. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6949. it.bound = BOUND_NONE;
  6950. if (funcname[strlen(funcname)-1] == '3') {
  6951. bool res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6952. if (!res)
  6953. return SCRIPT_CMD_FAILURE;
  6954. }
  6955. else if (funcname[strlen(funcname)-1] == '4') {
  6956. bool res = script_getitem_randomoption(st, sd, &it, funcname, 12);
  6957. if (!res)
  6958. return SCRIPT_CMD_FAILURE;
  6959. int grade = script_getnum(st, 11);
  6960. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  6961. ShowError("buildin_rentitem2: Not a correct grade! Grade=%d\n", grade);
  6962. return SCRIPT_CMD_FAILURE;
  6963. }
  6964. it.enchantgrade = static_cast<e_enchantgrade>(grade);
  6965. }
  6966. unsigned char flag = 0;
  6967. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6968. clif_additem(sd, 0, 0, flag);
  6969. return SCRIPT_CMD_FAILURE;
  6970. }
  6971. return SCRIPT_CMD_SUCCESS;
  6972. }
  6973. /*==========================================
  6974. * gets an item with someone's name inscribed [Skotlex]
  6975. * getinscribeditem item_num, character_name
  6976. * Returned Qty is always 1, only works on equip-able
  6977. * equipment
  6978. *------------------------------------------*/
  6979. BUILDIN_FUNC(getnameditem)
  6980. {
  6981. t_itemid nameid;
  6982. struct item item_tmp;
  6983. TBL_PC *sd, *tsd;
  6984. if (!script_rid2sd(sd))
  6985. { //Player not attached!
  6986. script_pushint(st,0);
  6987. return SCRIPT_CMD_SUCCESS;
  6988. }
  6989. if( script_isstring(st, 2) ){
  6990. const char *name = script_getstr(st, 2);
  6991. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6992. // Failed
  6993. if( item_data == nullptr){
  6994. script_pushint(st,0);
  6995. return SCRIPT_CMD_SUCCESS;
  6996. }
  6997. nameid = item_data->nameid;
  6998. }else
  6999. nameid = script_getnum(st, 2);
  7000. if(!item_db.exists(nameid)/* || itemdb_isstackable(nameid)*/)
  7001. { //Even though named stackable items "could" be risky, they are required for certain quests.
  7002. script_pushint(st,0);
  7003. return SCRIPT_CMD_SUCCESS;
  7004. }
  7005. if( script_isstring(st, 3) ) //Char Name
  7006. tsd = map_nick2sd(script_getstr(st, 3),false);
  7007. else //Char Id was given
  7008. tsd = map_charid2sd(script_getnum(st, 3));
  7009. if( tsd == NULL )
  7010. { //Failed
  7011. script_pushint(st,0);
  7012. return SCRIPT_CMD_SUCCESS;
  7013. }
  7014. memset(&item_tmp,0,sizeof(item_tmp));
  7015. item_tmp.nameid=nameid;
  7016. item_tmp.amount=1;
  7017. item_tmp.identify=1;
  7018. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  7019. item_tmp.card[2]=GetWord(tsd->status.char_id,0);
  7020. item_tmp.card[3]=GetWord(tsd->status.char_id,1);
  7021. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  7022. script_pushint(st,0);
  7023. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  7024. }
  7025. script_pushint(st,1);
  7026. return SCRIPT_CMD_SUCCESS;
  7027. }
  7028. /*==========================================
  7029. * gets a random item ID from an item group [Skotlex]
  7030. * groupranditem <group_num>{,<sub_group>};
  7031. *------------------------------------------*/
  7032. BUILDIN_FUNC(grouprandomitem) {
  7033. int sub_group = 1;
  7034. FETCH(3, sub_group);
  7035. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(script_getnum(st,2),sub_group);
  7036. if (!entry) {
  7037. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  7038. script_pushint(st,UNKNOWN_ITEM_ID);
  7039. return SCRIPT_CMD_FAILURE;
  7040. }
  7041. script_pushint(st,entry->nameid);
  7042. return SCRIPT_CMD_SUCCESS;
  7043. }
  7044. /**
  7045. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  7046. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  7047. */
  7048. BUILDIN_FUNC(makeitem) {
  7049. t_itemid nameid;
  7050. uint16 amount, flag = 0, x, y;
  7051. const char *mapname;
  7052. int m;
  7053. struct item item_tmp;
  7054. bool canShowEffect = false;
  7055. if( script_isstring(st, 2) ){
  7056. const char *name = script_getstr(st, 2);
  7057. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  7058. if( item_data )
  7059. nameid = item_data->nameid;
  7060. else{
  7061. ShowError( "buildin_makeitem: Unknown item %s\n", name );
  7062. return SCRIPT_CMD_FAILURE;
  7063. }
  7064. }
  7065. else {
  7066. int32 val = script_getnum( st, 2 );
  7067. if( val < 0 ){
  7068. flag = 1;
  7069. nameid = (t_itemid)( -1 * val );
  7070. }else{
  7071. nameid = (t_itemid)val;
  7072. }
  7073. if( !item_db.exists( nameid ) ){
  7074. ShowError( "buildin_makeitem: Unknown item id %u\n", nameid );
  7075. return SCRIPT_CMD_FAILURE;
  7076. }
  7077. }
  7078. amount = script_getnum(st,3);
  7079. mapname = script_getstr(st,4);
  7080. x = script_getnum(st,5);
  7081. y = script_getnum(st,6);
  7082. if (script_hasdata(st, 7))
  7083. canShowEffect = script_getnum(st, 7) != 0;
  7084. if (strcmp(mapname, "this")==0) {
  7085. TBL_PC *sd;
  7086. if (!script_rid2sd(sd))
  7087. return SCRIPT_CMD_SUCCESS; //Failed...
  7088. m = sd->bl.m;
  7089. } else
  7090. m = map_mapname2mapid(mapname);
  7091. memset(&item_tmp,0,sizeof(item_tmp));
  7092. item_tmp.nameid = nameid;
  7093. if (!flag)
  7094. item_tmp.identify = 1;
  7095. else
  7096. item_tmp.identify = itemdb_isidentified(nameid);
  7097. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7098. return SCRIPT_CMD_SUCCESS;
  7099. }
  7100. /**
  7101. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  7102. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  7103. *
  7104. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7105. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7106. *
  7107. * makeitem4 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7108. * makeitem4 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7109. */
  7110. BUILDIN_FUNC(makeitem2) {
  7111. t_itemid nameid;
  7112. const char *funcname = script_getfuncname(st);
  7113. if( script_isstring( st, 2 ) ){
  7114. const char *name = script_getstr( st, 2 );
  7115. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  7116. if( item_data ){
  7117. nameid = item_data->nameid;
  7118. }else{
  7119. ShowError( "buildin_%s: Unknown item %s\n", funcname, name );
  7120. return SCRIPT_CMD_FAILURE;
  7121. }
  7122. }else{
  7123. nameid = (t_itemid)script_getnum( st, 2 );
  7124. if( !item_db.exists( nameid ) ){
  7125. ShowError( "buildin_%s: Unknown item id %u\n", funcname, nameid );
  7126. return SCRIPT_CMD_FAILURE;
  7127. }
  7128. }
  7129. int amount = script_getnum(st,3);
  7130. const char *mapname = script_getstr(st,4);
  7131. int x = script_getnum(st,5);
  7132. int y = script_getnum(st,6);
  7133. int m;
  7134. if (strcmp(mapname,"this")==0) {
  7135. TBL_PC *sd;
  7136. if (!script_rid2sd(sd))
  7137. return SCRIPT_CMD_SUCCESS; //Failed...
  7138. m = sd->bl.m;
  7139. }
  7140. else
  7141. m = map_mapname2mapid(mapname);
  7142. struct item_data *id;
  7143. if ((id = itemdb_search(nameid))) {
  7144. struct item item_tmp = {};
  7145. item_tmp.nameid = nameid;
  7146. char iden = (char)script_getnum(st,7);
  7147. char ref = (char)script_getnum(st,8);
  7148. char attr = (char)script_getnum(st,9);
  7149. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  7150. if(ref > MAX_REFINE)
  7151. ref = MAX_REFINE;
  7152. }
  7153. else if (id->type==IT_PETEGG) {
  7154. iden = 1;
  7155. ref = 0;
  7156. }
  7157. else {
  7158. iden = 1;
  7159. ref = attr = 0;
  7160. }
  7161. item_tmp.identify = iden;
  7162. item_tmp.refine = ref;
  7163. item_tmp.attribute = attr;
  7164. item_tmp.card[0] = script_getnum(st,10);
  7165. item_tmp.card[1] = script_getnum(st,11);
  7166. item_tmp.card[2] = script_getnum(st,12);
  7167. item_tmp.card[3] = script_getnum(st,13);
  7168. bool canShowEffect = false;
  7169. if (funcname[strlen(funcname)-1] == '3' || funcname[strlen(funcname)-1] == '4') {
  7170. int offset = 14;
  7171. if (funcname[strlen(funcname)-1] == '4') {
  7172. offset = 15;
  7173. int grade = script_getnum(st, 14);
  7174. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  7175. ShowError( "buildin_%s: Not a correct grade! Grade=%d\n", funcname, grade );
  7176. return SCRIPT_CMD_FAILURE;
  7177. }
  7178. item_tmp.enchantgrade = static_cast<e_enchantgrade>(grade);
  7179. }
  7180. bool res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, offset);
  7181. if (!res)
  7182. return SCRIPT_CMD_FAILURE;
  7183. if (script_hasdata(st, offset+3)) {
  7184. if (script_getnum(st, offset+3) != 0)
  7185. canShowEffect = script_getnum(st, offset+3) != 0;
  7186. }
  7187. }
  7188. else {
  7189. if (script_hasdata(st, 14)) {
  7190. if (script_getnum(st, 14) != 0)
  7191. canShowEffect = script_getnum(st, 14) != 0;
  7192. }
  7193. }
  7194. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7195. }
  7196. else
  7197. return SCRIPT_CMD_FAILURE;
  7198. return SCRIPT_CMD_SUCCESS;
  7199. }
  7200. /// Counts / deletes the current item given by idx.
  7201. /// Used by buildin_delitem_search
  7202. /// Relies on all input data being already fully valid.
  7203. static void buildin_delitem_delete(map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  7204. {
  7205. int delamount;
  7206. struct item *itm = NULL;
  7207. struct s_storage *gstor = NULL;
  7208. switch(loc) {
  7209. case TABLE_CART:
  7210. itm = &sd->cart.u.items_cart[idx];
  7211. break;
  7212. case TABLE_STORAGE:
  7213. itm = &sd->storage.u.items_storage[idx];
  7214. break;
  7215. case TABLE_GUILD_STORAGE:
  7216. {
  7217. gstor = guild2storage2(sd->status.guild_id);
  7218. itm = &gstor->u.items_guild[idx];
  7219. }
  7220. break;
  7221. default: // TABLE_INVENTORY
  7222. itm = &sd->inventory.u.items_inventory[idx];
  7223. break;
  7224. }
  7225. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7226. if( delete_items )
  7227. {
  7228. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7229. {// delete associated pet
  7230. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7231. }
  7232. switch(loc) {
  7233. case TABLE_CART:
  7234. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7235. break;
  7236. case TABLE_STORAGE:
  7237. storage_delitem(sd,&sd->storage,idx,delamount);
  7238. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7239. break;
  7240. case TABLE_GUILD_STORAGE:
  7241. gstor->lock = true;
  7242. storage_guild_delitem(sd, gstor, idx, delamount);
  7243. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7244. storage_guild_storageclose(sd);
  7245. gstor->lock = false;
  7246. break;
  7247. default: // TABLE_INVENTORY
  7248. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7249. break;
  7250. }
  7251. }
  7252. amount[0]-= delamount;
  7253. }
  7254. /// Searches for item(s) and checks, if there is enough of them.
  7255. /// Used by delitem and delitem2
  7256. /// Relies on all input data being already fully valid.
  7257. /// @param exact_match will also match item by specified attributes
  7258. /// 0x0: Only item id
  7259. /// 0x1: identify, attributes, cards
  7260. /// 0x2: random option
  7261. /// @return true when all items could be deleted, false when there were not enough items to delete
  7262. static bool buildin_delitem_search(map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7263. {
  7264. bool delete_items = false;
  7265. int i, amount, size;
  7266. struct item *items;
  7267. // prefer always non-equipped items
  7268. it->equip = 0;
  7269. // when searching for nameid only, prefer additionally
  7270. if( !exact_match )
  7271. {
  7272. // non-refined items
  7273. it->refine = 0;
  7274. // card-less items
  7275. memset(it->card, 0, sizeof(it->card));
  7276. }
  7277. switch(loc) {
  7278. case TABLE_CART:
  7279. size = MAX_CART;
  7280. items = sd->cart.u.items_cart;
  7281. break;
  7282. case TABLE_STORAGE:
  7283. size = MAX_STORAGE;
  7284. items = sd->storage.u.items_storage;
  7285. break;
  7286. case TABLE_GUILD_STORAGE:
  7287. {
  7288. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7289. size = MAX_GUILD_STORAGE;
  7290. items = gstor->u.items_guild;
  7291. }
  7292. break;
  7293. default: // TABLE_INVENTORY
  7294. size = MAX_INVENTORY;
  7295. items = sd->inventory.u.items_inventory;
  7296. break;
  7297. }
  7298. for(;;)
  7299. {
  7300. unsigned short important = 0;
  7301. amount = it->amount;
  7302. // 1st pass -- less important items / exact match
  7303. for( i = 0; amount && i < size; i++ )
  7304. {
  7305. struct item *itm = NULL;
  7306. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7307. {// wrong/invalid item
  7308. continue;
  7309. }
  7310. if( itm->equip != it->equip || itm->refine != it->refine )
  7311. {// not matching attributes
  7312. important++;
  7313. continue;
  7314. }
  7315. if( exact_match )
  7316. {
  7317. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || itm->enchantgrade != it->enchantgrade || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7318. {// not matching exact attributes
  7319. continue;
  7320. }
  7321. if (exact_match&0x2) {
  7322. uint8 j;
  7323. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7324. 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)
  7325. break;
  7326. }
  7327. if (j != MAX_ITEM_RDM_OPT)
  7328. continue;
  7329. }
  7330. }
  7331. else
  7332. {
  7333. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7334. {
  7335. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7336. {// pet which cannot be deleted
  7337. continue;
  7338. }
  7339. }
  7340. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7341. {// named/carded item
  7342. important++;
  7343. continue;
  7344. }
  7345. }
  7346. // count / delete item
  7347. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7348. }
  7349. // 2nd pass -- any matching item
  7350. if( amount == 0 || important == 0 )
  7351. {// either everything was already consumed or no items were skipped
  7352. ;
  7353. }
  7354. else for( i = 0; amount && i < size; i++ )
  7355. {
  7356. struct item *itm = NULL;
  7357. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7358. {// wrong/invalid item
  7359. continue;
  7360. }
  7361. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7362. {// pet which cannot be deleted
  7363. continue;
  7364. }
  7365. if( exact_match )
  7366. {
  7367. 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)) ) )
  7368. {// not matching attributes
  7369. continue;
  7370. }
  7371. if (exact_match&0x2) {
  7372. uint8 j;
  7373. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7374. 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)
  7375. break;
  7376. }
  7377. if (j != MAX_ITEM_RDM_OPT)
  7378. continue;
  7379. }
  7380. }
  7381. // count / delete item
  7382. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7383. }
  7384. if( amount )
  7385. {// not enough items
  7386. return false;
  7387. }
  7388. else if( delete_items )
  7389. {// we are done with the work
  7390. return true;
  7391. }
  7392. else
  7393. {// get rid of the items now
  7394. delete_items = true;
  7395. }
  7396. }
  7397. }
  7398. /// Deletes items from the target/attached player.
  7399. /// Prioritizes ordinary items.
  7400. ///
  7401. /// delitem <item id>,<amount>{,<account id>}
  7402. /// delitem "<item name>",<amount>{,<account id>}
  7403. /// cartdelitem <item id>,<amount>{,<account id>}
  7404. /// cartdelitem "<item name>",<amount>{,<account id>}
  7405. /// storagedelitem <item id>,<amount>{,<account id>}
  7406. /// storagedelitem "<item name>",<amount>{,<account id>}
  7407. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7408. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7409. BUILDIN_FUNC(delitem)
  7410. {
  7411. TBL_PC *sd;
  7412. struct item it;
  7413. uint8 loc = 0;
  7414. char* command = (char*)script_getfuncname(st);
  7415. if(!strncmp(command, "cart", 4))
  7416. loc = TABLE_CART;
  7417. else if(!strncmp(command, "storage", 7))
  7418. loc = TABLE_STORAGE;
  7419. else if(!strncmp(command, "guildstorage", 12))
  7420. loc = TABLE_GUILD_STORAGE;
  7421. if( !script_accid2sd(4,sd) ){
  7422. // In any case cancel script execution
  7423. st->state = END;
  7424. return SCRIPT_CMD_SUCCESS;
  7425. }
  7426. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7427. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7428. script_pushint(st, -1);
  7429. return SCRIPT_CMD_FAILURE;
  7430. }
  7431. if (loc == TABLE_GUILD_STORAGE) {
  7432. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7433. if (gstor == NULL || sd->state.storage_flag) {
  7434. script_pushint(st, -1);
  7435. return SCRIPT_CMD_FAILURE;
  7436. }
  7437. }
  7438. if( script_isstring(st, 2) )
  7439. {
  7440. const char* item_name = script_getstr(st, 2);
  7441. std::shared_ptr<item_data> id = item_db.searchname(item_name);
  7442. if( id == nullptr ){
  7443. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7444. st->state = END;
  7445. return SCRIPT_CMD_FAILURE;
  7446. }
  7447. it.nameid = id->nameid;// "<item name>"
  7448. }
  7449. else
  7450. {
  7451. it.nameid = script_getnum(st, 2);// <item id>
  7452. if( !item_db.exists( it.nameid ) )
  7453. {
  7454. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7455. st->state = END;
  7456. return SCRIPT_CMD_FAILURE;
  7457. }
  7458. }
  7459. it.amount=script_getnum(st,3);
  7460. if( it.amount <= 0 )
  7461. return SCRIPT_CMD_SUCCESS;// nothing to do
  7462. if( buildin_delitem_search(sd, &it, 0, loc) )
  7463. {// success
  7464. return SCRIPT_CMD_SUCCESS;
  7465. }
  7466. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7467. st->state = END;
  7468. st->mes_active = 0;
  7469. clif_scriptclose(sd, st->oid);
  7470. return SCRIPT_CMD_FAILURE;
  7471. }
  7472. /// Deletes items from the target/attached player.
  7473. ///
  7474. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7475. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7476. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7477. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7478. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7479. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7480. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7481. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7482. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7483. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7484. /// delitem4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7485. /// delitem4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7486. BUILDIN_FUNC(delitem2)
  7487. {
  7488. TBL_PC *sd;
  7489. uint8 loc = 0;
  7490. char* command = (char*)script_getfuncname(st);
  7491. int aid_pos = 11;
  7492. if(!strncmp(command, "cart", 4))
  7493. loc = TABLE_CART;
  7494. else if(!strncmp(command, "storage", 7))
  7495. loc = TABLE_STORAGE;
  7496. else if(!strncmp(command, "guildstorage", 12))
  7497. loc = TABLE_GUILD_STORAGE;
  7498. if (command[strlen(command)-1] == '3')
  7499. aid_pos = 14;
  7500. else if (command[strlen(command)-1] == '4')
  7501. aid_pos = 15;
  7502. if( !script_accid2sd(aid_pos,sd) ){
  7503. // In any case cancel script execution
  7504. st->state = END;
  7505. return SCRIPT_CMD_SUCCESS;
  7506. }
  7507. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7508. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7509. script_pushint(st,-1);
  7510. return SCRIPT_CMD_FAILURE;
  7511. }
  7512. if (loc == TABLE_GUILD_STORAGE) {
  7513. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7514. if (gstor == NULL || sd->state.storage_flag) {
  7515. script_pushint(st, -1);
  7516. return SCRIPT_CMD_FAILURE;
  7517. }
  7518. }
  7519. struct item it = {};
  7520. if( script_isstring(st, 2) )
  7521. {
  7522. const char* item_name = script_getstr(st, 2);
  7523. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  7524. if( id == nullptr ){
  7525. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7526. st->state = END;
  7527. return SCRIPT_CMD_FAILURE;
  7528. }
  7529. it.nameid = id->nameid;// "<item name>"
  7530. }
  7531. else
  7532. {
  7533. it.nameid = script_getnum(st, 2);// <item id>
  7534. if( !item_db.exists( it.nameid ) )
  7535. {
  7536. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7537. st->state = END;
  7538. return SCRIPT_CMD_FAILURE;
  7539. }
  7540. }
  7541. it.amount=script_getnum(st,3);
  7542. it.identify=script_getnum(st,4);
  7543. it.refine=script_getnum(st,5);
  7544. it.attribute=script_getnum(st,6);
  7545. it.card[0]=script_getnum(st,7);
  7546. it.card[1]=script_getnum(st,8);
  7547. it.card[2]=script_getnum(st,9);
  7548. it.card[3]=script_getnum(st,10);
  7549. uint8 flag = 0x1;
  7550. if (command[strlen(command)-1] == '3' || command[strlen(command)-1] == '4') {
  7551. int offset = 11;
  7552. if (command[strlen(command)-1] == '4') {
  7553. offset = 12;
  7554. int grade = script_getnum(st, 11);
  7555. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  7556. ShowError( "buildin_%s: Not a correct grade! Grade=%d\n", command, grade );
  7557. return SCRIPT_CMD_FAILURE;
  7558. }
  7559. it.enchantgrade = static_cast<e_enchantgrade>(grade);
  7560. }
  7561. bool res = script_getitem_randomoption(st, sd, &it, command, offset);
  7562. if (!res)
  7563. return SCRIPT_CMD_FAILURE;
  7564. flag |= 0x2;
  7565. }
  7566. if( it.amount <= 0 )
  7567. return SCRIPT_CMD_SUCCESS;// nothing to do
  7568. if( buildin_delitem_search(sd, &it, flag, loc) )
  7569. {// success
  7570. return SCRIPT_CMD_SUCCESS;
  7571. }
  7572. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7573. st->state = END;
  7574. st->mes_active = 0;
  7575. clif_scriptclose(sd, st->oid);
  7576. return SCRIPT_CMD_FAILURE;
  7577. }
  7578. /// Deletes items from the target/attached player at given index.
  7579. /// delitemidx <index>{,<amount>{,<char id>}};
  7580. BUILDIN_FUNC(delitemidx) {
  7581. map_session_data* sd;
  7582. if (!script_charid2sd(4, sd)) {
  7583. script_pushint(st, false);
  7584. return SCRIPT_CMD_FAILURE;
  7585. }
  7586. int idx = script_getnum(st, 2);
  7587. if (idx < 0 || idx >= MAX_INVENTORY) {
  7588. ShowWarning("buildin_delitemidx: Index %d is out of the range 0-%d.\n", idx, MAX_INVENTORY - 1);
  7589. script_pushint(st, false);
  7590. return SCRIPT_CMD_FAILURE;
  7591. }
  7592. if (sd->inventory_data[idx] == nullptr) {
  7593. 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);
  7594. script_pushint(st, false);
  7595. return SCRIPT_CMD_FAILURE;
  7596. }
  7597. int amount;
  7598. if (script_hasdata(st, 3))
  7599. amount = script_getnum(st, 3);
  7600. else
  7601. amount = sd->inventory.u.items_inventory[idx].amount;
  7602. if (amount > 0)
  7603. script_pushint(st, pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_SCRIPT) == 0);
  7604. else
  7605. script_pushint(st, false);
  7606. return SCRIPT_CMD_SUCCESS;
  7607. }
  7608. /*==========================================
  7609. * Enables/Disables use of items while in an NPC [Skotlex]
  7610. *------------------------------------------*/
  7611. BUILDIN_FUNC(enableitemuse)
  7612. {
  7613. TBL_PC *sd;
  7614. if (script_rid2sd(sd))
  7615. st->npc_item_flag = sd->npc_item_flag = 1;
  7616. return SCRIPT_CMD_SUCCESS;
  7617. }
  7618. BUILDIN_FUNC(disableitemuse)
  7619. {
  7620. TBL_PC *sd;
  7621. if (script_rid2sd(sd))
  7622. st->npc_item_flag = sd->npc_item_flag = 0;
  7623. return SCRIPT_CMD_SUCCESS;
  7624. }
  7625. /*==========================================
  7626. * Returns a character's specified stat.
  7627. * Check pc_readparam for available options.
  7628. * readparam <param>{,"<nick>"}
  7629. * readparam <param>{,<char_id>}
  7630. *------------------------------------------*/
  7631. BUILDIN_FUNC(readparam)
  7632. {
  7633. int value;
  7634. struct script_data *data = script_getdata(st, 2);
  7635. TBL_PC *sd = NULL;
  7636. if( script_hasdata(st, 3) ){
  7637. if (script_isint(st, 3))
  7638. script_charid2sd(3, sd);
  7639. else
  7640. script_nick2sd(3, sd);
  7641. }else{
  7642. script_rid2sd(sd);
  7643. }
  7644. if( !sd ){
  7645. script_pushint(st, -1);
  7646. return SCRIPT_CMD_FAILURE;
  7647. }
  7648. // If you use a parameter, return the value behind it
  7649. if( reference_toparam(data) ){
  7650. get_val_(st, data, sd);
  7651. value = (int)data->u.num;
  7652. }else{
  7653. value = static_cast<int>(pc_readparam(sd,script_getnum(st, 2)));
  7654. }
  7655. script_pushint(st,value);
  7656. return SCRIPT_CMD_SUCCESS;
  7657. }
  7658. /*==========================================
  7659. * Return charid identification
  7660. * return by @num :
  7661. * 0 : char_id
  7662. * 1 : party_id
  7663. * 2 : guild_id
  7664. * 3 : account_id
  7665. * 4 : bg_id
  7666. * 5 : clan_id
  7667. *------------------------------------------*/
  7668. BUILDIN_FUNC(getcharid)
  7669. {
  7670. int num;
  7671. TBL_PC *sd;
  7672. num = script_getnum(st,2);
  7673. if( !script_nick2sd(3,sd) ){
  7674. script_pushint(st,0); //return 0, according docs
  7675. return SCRIPT_CMD_SUCCESS;
  7676. }
  7677. switch( num ) {
  7678. case 0: script_pushint(st,sd->status.char_id); break;
  7679. case 1: script_pushint(st,sd->status.party_id); break;
  7680. case 2: script_pushint(st,sd->status.guild_id); break;
  7681. case 3: script_pushint(st,sd->status.account_id); break;
  7682. case 4: script_pushint(st,sd->bg_id); break;
  7683. case 5: script_pushint(st,sd->status.clan_id); break;
  7684. default:
  7685. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7686. script_pushint(st,0);
  7687. break;
  7688. }
  7689. return SCRIPT_CMD_SUCCESS;
  7690. }
  7691. /*==========================================
  7692. * returns the GID of an NPC
  7693. * Returns 0 if the NPC name provided is not found.
  7694. *------------------------------------------*/
  7695. BUILDIN_FUNC(getnpcid)
  7696. {
  7697. int num = script_getnum(st,2);
  7698. struct npc_data* nd = NULL;
  7699. if( script_hasdata(st,3) )
  7700. {// unique npc name
  7701. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7702. {
  7703. //Npc not found.
  7704. script_pushint(st,0);
  7705. return SCRIPT_CMD_SUCCESS;
  7706. }
  7707. }
  7708. switch (num) {
  7709. case 0:
  7710. script_pushint(st,nd ? nd->bl.id : st->oid);
  7711. break;
  7712. default:
  7713. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7714. script_pushint(st,0);
  7715. return SCRIPT_CMD_FAILURE;
  7716. }
  7717. return SCRIPT_CMD_SUCCESS;
  7718. }
  7719. /*==========================================
  7720. * Return the name of the party_id
  7721. * null if not found
  7722. *------------------------------------------*/
  7723. BUILDIN_FUNC(getpartyname)
  7724. {
  7725. int party_id;
  7726. struct party_data* p;
  7727. party_id = script_getnum(st,2);
  7728. if( ( p = party_search(party_id) ) != NULL )
  7729. {
  7730. script_pushstrcopy(st,p->party.name);
  7731. }
  7732. else
  7733. {
  7734. script_pushconststr(st,"null");
  7735. }
  7736. return SCRIPT_CMD_SUCCESS;
  7737. }
  7738. /*==========================================
  7739. * Get the information of the members of a party by type
  7740. * @party_id, @type
  7741. * return by @type :
  7742. * - : nom des membres
  7743. * 1 : char_id des membres
  7744. * 2 : account_id des membres
  7745. *------------------------------------------*/
  7746. BUILDIN_FUNC(getpartymember)
  7747. {
  7748. struct party_data *p;
  7749. uint8 j = 0;
  7750. p = party_search(script_getnum(st,2));
  7751. if (p != NULL) {
  7752. uint8 i, type = 0;
  7753. struct script_data *data = NULL;
  7754. char *varname = NULL;
  7755. if (script_hasdata(st,3))
  7756. type = script_getnum(st,3);
  7757. if (script_hasdata(st,4)) {
  7758. data = script_getdata(st, 4);
  7759. if (!data_isreference(data)) {
  7760. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7761. return SCRIPT_CMD_FAILURE;
  7762. }
  7763. varname = reference_getname(data);
  7764. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7765. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7766. return SCRIPT_CMD_FAILURE;
  7767. }
  7768. if (not_server_variable(*varname)) {
  7769. map_session_data *sd;
  7770. if (!script_rid2sd(sd)) {
  7771. ShowError("buildin_getpartymember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  7772. return SCRIPT_CMD_FAILURE;
  7773. }
  7774. }
  7775. }
  7776. for (i = 0; i < MAX_PARTY; i++) {
  7777. if (p->party.member[i].account_id) {
  7778. switch (type) {
  7779. case 2:
  7780. if (data)
  7781. setd_sub_num( st, NULL, varname, j, p->party.member[i].account_id, data->ref );
  7782. else
  7783. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7784. break;
  7785. case 1:
  7786. if (data)
  7787. setd_sub_num( st, NULL, varname, j, p->party.member[i].char_id, data->ref );
  7788. else
  7789. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7790. break;
  7791. default:
  7792. if (data)
  7793. setd_sub_str( st, NULL, varname, j, p->party.member[i].name, data->ref );
  7794. else
  7795. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7796. break;
  7797. }
  7798. j++;
  7799. }
  7800. }
  7801. }
  7802. mapreg_setreg(add_str("$@partymembercount"),j);
  7803. script_pushint(st, j);
  7804. return SCRIPT_CMD_SUCCESS;
  7805. }
  7806. /*==========================================
  7807. * Retrieves party leader. if flag is specified,
  7808. * return some of the leader data. Otherwise, return name.
  7809. *------------------------------------------*/
  7810. BUILDIN_FUNC(getpartyleader)
  7811. {
  7812. int party_id, type = 0, i=0;
  7813. struct party_data *p;
  7814. party_id=script_getnum(st,2);
  7815. if( script_hasdata(st,3) )
  7816. type=script_getnum(st,3);
  7817. p=party_search(party_id);
  7818. if (p) //Search leader
  7819. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7820. if (!p || i == MAX_PARTY) { //leader not found
  7821. if (type)
  7822. script_pushint(st,-1);
  7823. else
  7824. script_pushconststr(st,"null");
  7825. return SCRIPT_CMD_SUCCESS;
  7826. }
  7827. switch (type) {
  7828. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7829. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7830. case 3: script_pushint(st,p->party.member[i].class_); break;
  7831. case 4: script_pushstrcopy( st, p->party.member[i].map ); break;
  7832. case 5: script_pushint(st,p->party.member[i].lv); break;
  7833. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7834. }
  7835. return SCRIPT_CMD_SUCCESS;
  7836. }
  7837. /*==========================================
  7838. * Return the name of the @guild_id
  7839. * null if not found
  7840. *------------------------------------------*/
  7841. BUILDIN_FUNC(getguildname)
  7842. {
  7843. int guild_id;
  7844. guild_id = script_getnum(st,2);
  7845. auto g = guild_search(guild_id);
  7846. if (g)
  7847. script_pushstrcopy(st,g->guild.name);
  7848. else
  7849. script_pushconststr(st,"null");
  7850. return SCRIPT_CMD_SUCCESS;
  7851. }
  7852. /*==========================================
  7853. * Return the name of the guild master of @guild_id
  7854. * null if not found
  7855. *------------------------------------------*/
  7856. BUILDIN_FUNC(getguildmaster)
  7857. {
  7858. int guild_id;
  7859. guild_id = script_getnum(st,2);
  7860. auto g = guild_search(guild_id);
  7861. if (g)
  7862. script_pushstrcopy(st,g->guild.member[0].name);
  7863. else
  7864. script_pushconststr(st,"null");
  7865. return SCRIPT_CMD_SUCCESS;
  7866. }
  7867. BUILDIN_FUNC(getguildmasterid)
  7868. {
  7869. int guild_id;
  7870. guild_id = script_getnum(st,2);
  7871. auto g = guild_search(guild_id);
  7872. if (g)
  7873. script_pushint(st,g->guild.member[0].char_id);
  7874. else
  7875. script_pushint(st,0);
  7876. return SCRIPT_CMD_SUCCESS;
  7877. }
  7878. /*==========================================
  7879. * Get char string information by type :
  7880. * Return by @type :
  7881. * 0 : char_name
  7882. * 1 : party_name or ""
  7883. * 2 : guild_name or ""
  7884. * 3 : map_name
  7885. * - : ""
  7886. * strcharinfo(<type>{,<char_id>})
  7887. *------------------------------------------*/
  7888. BUILDIN_FUNC(strcharinfo)
  7889. {
  7890. TBL_PC *sd;
  7891. int num;
  7892. struct party_data* p;
  7893. if (!script_charid2sd(3,sd)) {
  7894. script_pushconststr(st,"");
  7895. return SCRIPT_CMD_FAILURE;
  7896. }
  7897. num=script_getnum(st,2);
  7898. switch(num){
  7899. case 0:
  7900. script_pushstrcopy(st,sd->status.name);
  7901. break;
  7902. case 1:
  7903. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7904. script_pushstrcopy(st,p->party.name);
  7905. } else {
  7906. script_pushconststr(st,"");
  7907. }
  7908. break;
  7909. case 2:
  7910. if (sd->guild) {
  7911. script_pushstrcopy(st,sd->guild->guild.name);
  7912. } else {
  7913. script_pushconststr(st,"");
  7914. }
  7915. break;
  7916. case 3:
  7917. script_pushconststr(st,map_getmapdata(sd->bl.m)->name);
  7918. break;
  7919. default:
  7920. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7921. script_pushconststr(st,"");
  7922. break;
  7923. }
  7924. return SCRIPT_CMD_SUCCESS;
  7925. }
  7926. /*==========================================
  7927. * Get npc string information by type
  7928. * return by @type:
  7929. * 0 : name
  7930. * 1 : str#
  7931. * 2 : #str
  7932. * 3 : ::str
  7933. * 4 : map name
  7934. *------------------------------------------*/
  7935. BUILDIN_FUNC(strnpcinfo)
  7936. {
  7937. TBL_NPC* nd;
  7938. int num;
  7939. char *buf,*name=NULL;
  7940. nd = map_id2nd(st->oid);
  7941. if (!nd) {
  7942. script_pushconststr(st, "");
  7943. return SCRIPT_CMD_SUCCESS;
  7944. }
  7945. num = script_getnum(st,2);
  7946. switch(num){
  7947. case 0: // display name
  7948. name = aStrdup(nd->name);
  7949. break;
  7950. case 1: // visible part of display name
  7951. if((buf = strchr(nd->name,'#')) != NULL)
  7952. {
  7953. name = aStrdup(nd->name);
  7954. name[buf - nd->name] = 0;
  7955. } else // Return the name, there is no '#' present
  7956. name = aStrdup(nd->name);
  7957. break;
  7958. case 2: // # fragment
  7959. if((buf = strchr(nd->name,'#')) != NULL)
  7960. name = aStrdup(buf+1);
  7961. break;
  7962. case 3: // unique name
  7963. name = aStrdup(nd->exname);
  7964. break;
  7965. case 4: // map name
  7966. if (nd->bl.m >= 0)
  7967. name = aStrdup(map_getmapdata(nd->bl.m)->name);
  7968. break;
  7969. }
  7970. if(name)
  7971. script_pushstr(st, name);
  7972. else
  7973. script_pushconststr(st, "");
  7974. return SCRIPT_CMD_SUCCESS;
  7975. }
  7976. /**
  7977. * getequipid({<equipment slot>,<char_id>})
  7978. **/
  7979. BUILDIN_FUNC(getequipid)
  7980. {
  7981. int i, num;
  7982. TBL_PC* sd;
  7983. if (!script_charid2sd(3, sd)) {
  7984. script_pushint(st,-1);
  7985. return SCRIPT_CMD_FAILURE;
  7986. }
  7987. if (script_hasdata(st, 2))
  7988. num = script_getnum(st, 2);
  7989. else
  7990. num = EQI_COMPOUND_ON;
  7991. if (num == EQI_COMPOUND_ON)
  7992. i = current_equip_item_index;
  7993. else if (equip_index_check(num)) // get inventory position of item
  7994. i = pc_checkequip(sd, equip_bitmask[num]);
  7995. else {
  7996. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7997. script_pushint(st,-1);
  7998. return SCRIPT_CMD_FAILURE;
  7999. }
  8000. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  8001. script_pushint(st, sd->inventory_data[i]->nameid);
  8002. else
  8003. script_pushint(st, -1);
  8004. return SCRIPT_CMD_SUCCESS;
  8005. }
  8006. /*==========================================
  8007. * getequipuniqueid(<equipment slot>{,<char_id>})
  8008. *------------------------------------------*/
  8009. BUILDIN_FUNC(getequipuniqueid)
  8010. {
  8011. int i, num;
  8012. TBL_PC* sd;
  8013. struct item* item;
  8014. if (!script_charid2sd(3, sd)) {
  8015. script_pushconststr(st, "");
  8016. return SCRIPT_CMD_FAILURE;
  8017. }
  8018. num = script_getnum(st,2);
  8019. if ( !equip_index_check(num) ) {
  8020. script_pushconststr(st, "");
  8021. return SCRIPT_CMD_FAILURE;
  8022. }
  8023. // get inventory position of item
  8024. i = pc_checkequip(sd,equip_bitmask[num]);
  8025. if (i < 0) {
  8026. script_pushconststr(st, "");
  8027. return SCRIPT_CMD_FAILURE;
  8028. }
  8029. item = &sd->inventory.u.items_inventory[i];
  8030. if (item != 0) {
  8031. int maxlen = 256;
  8032. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  8033. memset(buf, 0, maxlen);
  8034. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  8035. script_pushstr(st, buf);
  8036. } else
  8037. script_pushconststr(st, "");
  8038. return SCRIPT_CMD_SUCCESS;
  8039. }
  8040. /*==========================================
  8041. * Get the equipement name at pos
  8042. * return item jname or ""
  8043. * getequipname(<equipment slot>{,<char_id>})
  8044. *------------------------------------------*/
  8045. BUILDIN_FUNC(getequipname)
  8046. {
  8047. int i, num;
  8048. TBL_PC* sd;
  8049. struct item_data* item;
  8050. if (!script_charid2sd(3, sd)) {
  8051. script_pushconststr(st, "");
  8052. return SCRIPT_CMD_FAILURE;
  8053. }
  8054. num = script_getnum(st,2);
  8055. if( !equip_index_check(num) )
  8056. {
  8057. script_pushconststr(st,"");
  8058. return SCRIPT_CMD_SUCCESS;
  8059. }
  8060. // get inventory position of item
  8061. i = pc_checkequip(sd,equip_bitmask[num]);
  8062. if( i < 0 )
  8063. {
  8064. script_pushconststr(st,"");
  8065. return SCRIPT_CMD_SUCCESS;
  8066. }
  8067. item = sd->inventory_data[i];
  8068. if( item != 0 )
  8069. script_pushstrcopy(st,item->ename.c_str());
  8070. else
  8071. script_pushconststr(st,"");
  8072. return SCRIPT_CMD_SUCCESS;
  8073. }
  8074. /*==========================================
  8075. * getbrokenid [Valaris]
  8076. * getbrokenid(<number>{,<char_id>})
  8077. *------------------------------------------*/
  8078. BUILDIN_FUNC(getbrokenid)
  8079. {
  8080. int i,num,id = 0,brokencounter = 0;
  8081. TBL_PC *sd;
  8082. if (!script_charid2sd(3, sd)) {
  8083. script_pushint(st,0);
  8084. return SCRIPT_CMD_FAILURE;
  8085. }
  8086. num = script_getnum(st,2);
  8087. for(i = 0; i < MAX_INVENTORY; i++) {
  8088. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8089. brokencounter++;
  8090. if(num == brokencounter){
  8091. id = sd->inventory.u.items_inventory[i].nameid;
  8092. break;
  8093. }
  8094. }
  8095. }
  8096. script_pushint(st,id);
  8097. return SCRIPT_CMD_SUCCESS;
  8098. }
  8099. /*==========================================
  8100. * repair [Valaris]
  8101. * repair <num>{,<char_id>};
  8102. *------------------------------------------*/
  8103. BUILDIN_FUNC(repair)
  8104. {
  8105. int i,num;
  8106. int repaircounter = 0;
  8107. TBL_PC *sd;
  8108. if (!script_charid2sd(3,sd))
  8109. return SCRIPT_CMD_FAILURE;
  8110. num = script_getnum(st,2);
  8111. for(i = 0; i < MAX_INVENTORY; i++) {
  8112. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8113. repaircounter++;
  8114. if(num == repaircounter) {
  8115. sd->inventory.u.items_inventory[i].attribute = 0;
  8116. clif_equiplist(sd);
  8117. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  8118. clif_misceffect(&sd->bl, 3);
  8119. break;
  8120. }
  8121. }
  8122. }
  8123. return SCRIPT_CMD_SUCCESS;
  8124. }
  8125. /*==========================================
  8126. * repairall {<char_id>}
  8127. *------------------------------------------*/
  8128. BUILDIN_FUNC(repairall)
  8129. {
  8130. int i, repaircounter = 0;
  8131. TBL_PC *sd;
  8132. if (!script_charid2sd(2,sd))
  8133. return SCRIPT_CMD_FAILURE;
  8134. for(i = 0; i < MAX_INVENTORY; i++)
  8135. {
  8136. 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] ) ){
  8137. sd->inventory.u.items_inventory[i].attribute = 0;
  8138. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  8139. repaircounter++;
  8140. }
  8141. }
  8142. if(repaircounter)
  8143. {
  8144. clif_misceffect(&sd->bl, 3);
  8145. clif_equiplist(sd);
  8146. }
  8147. return SCRIPT_CMD_SUCCESS;
  8148. }
  8149. /*==========================================
  8150. * Chk if player have something equiped at pos
  8151. * getequipisequiped <pos>{,<char_id>}
  8152. *------------------------------------------*/
  8153. BUILDIN_FUNC(getequipisequiped)
  8154. {
  8155. int i = -1,num;
  8156. TBL_PC *sd;
  8157. num = script_getnum(st,2);
  8158. if (!script_charid2sd(3,sd))
  8159. return SCRIPT_CMD_FAILURE;
  8160. if ( equip_index_check(num) )
  8161. i=pc_checkequip(sd,equip_bitmask[num]);
  8162. if(i >= 0)
  8163. script_pushint(st,1);
  8164. else
  8165. script_pushint(st,0);
  8166. return SCRIPT_CMD_SUCCESS;
  8167. }
  8168. /*==========================================
  8169. * Chk if the player have something equiped at pos
  8170. * if so chk if this item ain't marked not refinable or rental
  8171. * return (npc)
  8172. * 1 : true
  8173. * 0 : false
  8174. * getequipisenableref(<equipment slot>{,<char_id>})
  8175. *------------------------------------------*/
  8176. BUILDIN_FUNC(getequipisenableref)
  8177. {
  8178. int i = -1,num;
  8179. TBL_PC *sd;
  8180. num = script_getnum(st,2);
  8181. if (!script_charid2sd(3, sd)) {
  8182. script_pushint(st,0);
  8183. return SCRIPT_CMD_FAILURE;
  8184. }
  8185. if( equip_index_check(num) )
  8186. i = pc_checkequip(sd,equip_bitmask[num]);
  8187. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  8188. script_pushint(st,1);
  8189. else
  8190. script_pushint(st,0);
  8191. return SCRIPT_CMD_SUCCESS;
  8192. }
  8193. /*==========================================
  8194. * Get the item refined value at pos
  8195. * return (npc)
  8196. * x : refine amount
  8197. * 0 : false (not refined)
  8198. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  8199. *------------------------------------------*/
  8200. BUILDIN_FUNC(getequiprefinerycnt)
  8201. {
  8202. int i = -1,num;
  8203. TBL_PC *sd;
  8204. num = script_getnum(st,2);
  8205. if (!script_charid2sd(3, sd)) {
  8206. script_pushint(st,0);
  8207. return SCRIPT_CMD_FAILURE;
  8208. }
  8209. if (equip_index_check(num))
  8210. i=pc_checkequip(sd,equip_bitmask[num]);
  8211. if(i >= 0)
  8212. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  8213. else
  8214. script_pushint(st,0);
  8215. return SCRIPT_CMD_SUCCESS;
  8216. }
  8217. /*==========================================
  8218. * Get the weapon level value at pos
  8219. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  8220. * return (npc)
  8221. * x : weapon level
  8222. * 0 : false
  8223. * getequipweaponlv({<equipment slot>{,<char_id>}})
  8224. *------------------------------------------*/
  8225. BUILDIN_FUNC(getequipweaponlv)
  8226. {
  8227. int num;
  8228. if( script_hasdata( st, 2 ) ){
  8229. num = script_getnum(st, 2);
  8230. }else{
  8231. num = EQI_COMPOUND_ON;
  8232. }
  8233. map_session_data* sd;
  8234. if (!script_charid2sd(3, sd)) {
  8235. script_pushint(st, 0);
  8236. return SCRIPT_CMD_FAILURE;
  8237. }
  8238. int i = -1;
  8239. if( num == EQI_COMPOUND_ON ){
  8240. if( current_equip_item_index == -1 ){
  8241. script_pushint(st, 0);
  8242. return SCRIPT_CMD_FAILURE;
  8243. }
  8244. i = current_equip_item_index;
  8245. }else if (equip_index_check(num))
  8246. i = pc_checkequip(sd, equip_bitmask[num]);
  8247. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON ){
  8248. script_pushint( st, sd->inventory_data[i]->weapon_level );
  8249. }else{
  8250. script_pushint( st, 0 );
  8251. }
  8252. return SCRIPT_CMD_SUCCESS;
  8253. }
  8254. BUILDIN_FUNC(getequiparmorlv){
  8255. int num;
  8256. if( script_hasdata( st, 2 ) ){
  8257. num = script_getnum(st, 2);
  8258. }else{
  8259. num = EQI_COMPOUND_ON;
  8260. }
  8261. map_session_data* sd;
  8262. if( !script_charid2sd( 3, sd ) ){
  8263. script_pushint( st, 0 );
  8264. return SCRIPT_CMD_FAILURE;
  8265. }
  8266. int i = -1;
  8267. if( num == EQI_COMPOUND_ON ){
  8268. if( current_equip_item_index == -1 ){
  8269. script_pushint( st, 0 );
  8270. return SCRIPT_CMD_FAILURE;
  8271. }
  8272. i = current_equip_item_index;
  8273. }else if( equip_index_check( num ) ){
  8274. i = pc_checkequip( sd, equip_bitmask[num] );
  8275. }else{
  8276. ShowError( "buildin_getequiparmorlv: unsupported equip index %d.\n", num );
  8277. script_pushint( st, 0 );
  8278. return SCRIPT_CMD_FAILURE;
  8279. }
  8280. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_ARMOR ){
  8281. script_pushint( st, sd->inventory_data[i]->armor_level );
  8282. }else{
  8283. script_pushint( st, 0 );
  8284. }
  8285. return SCRIPT_CMD_SUCCESS;
  8286. }
  8287. /*==========================================
  8288. * Get the item refine chance (from refine.txt) for item at pos
  8289. * return (npc)
  8290. * x : refine chance
  8291. * 0 : false (max refine level or unequip..)
  8292. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8293. *------------------------------------------*/
  8294. BUILDIN_FUNC(getequippercentrefinery)
  8295. {
  8296. int i = -1,num;
  8297. bool enriched = false;
  8298. TBL_PC *sd;
  8299. num = script_getnum(st,2);
  8300. if (script_hasdata(st, 3))
  8301. enriched = script_getnum(st, 3) != 0;
  8302. if (!script_charid2sd(4, sd)) {
  8303. script_pushint(st,0);
  8304. return SCRIPT_CMD_FAILURE;
  8305. }
  8306. if (equip_index_check(num))
  8307. i = pc_checkequip(sd,equip_bitmask[num]);
  8308. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid) {
  8309. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  8310. if( info == nullptr ){
  8311. script_pushint( st, 0 );
  8312. return SCRIPT_CMD_SUCCESS;
  8313. }
  8314. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, (uint16)( enriched ? REFINE_COST_ENRICHED : REFINE_COST_NORMAL ) );
  8315. if( cost == nullptr ){
  8316. script_pushint( st, 0 );
  8317. return SCRIPT_CMD_SUCCESS;
  8318. }
  8319. script_pushint( st, cost->chance / 100 );
  8320. }
  8321. else
  8322. script_pushint(st,0);
  8323. return SCRIPT_CMD_SUCCESS;
  8324. }
  8325. /*==========================================
  8326. * Refine +1 item at pos and log and display refine
  8327. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8328. *------------------------------------------*/
  8329. BUILDIN_FUNC(successrefitem) {
  8330. short i = -1, up = 1;
  8331. int pos;
  8332. TBL_PC *sd;
  8333. pos = script_getnum(st,2);
  8334. if (!script_charid2sd(4, sd)) {
  8335. script_pushint(st, -1);
  8336. return SCRIPT_CMD_FAILURE;
  8337. }
  8338. if (script_hasdata(st, 3))
  8339. up = script_getnum(st, 3);
  8340. if (equip_index_check(pos))
  8341. i = pc_checkequip(sd,equip_bitmask[pos]);
  8342. if (i >= 0) {
  8343. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8344. //Logs items, got from (N)PC scripts [Lupus]
  8345. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8346. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8347. script_pushint(st, MAX_REFINE);
  8348. return SCRIPT_CMD_SUCCESS;
  8349. }
  8350. sd->inventory.u.items_inventory[i].refine += up;
  8351. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8352. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8353. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8354. clif_delitem(sd,i,1,3);
  8355. //Logs items, got from (N)PC scripts [Lupus]
  8356. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8357. clif_additem(sd,i,1,0);
  8358. pc_equipitem(sd,i,ep);
  8359. clif_misceffect(&sd->bl,3);
  8360. if( sd->inventory_data[i]->type == IT_WEAPON ){
  8361. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->weapon_level, sd->inventory.u.items_inventory[i].refine);
  8362. }
  8363. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8364. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8365. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]) &&
  8366. sd->inventory_data[i]->type == IT_WEAPON )
  8367. { // Fame point system [DracoRPG]
  8368. switch( sd->inventory_data[i]->weapon_level ){
  8369. case 1:
  8370. pc_addfame(*sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8371. break;
  8372. case 2:
  8373. pc_addfame(*sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8374. break;
  8375. case 3:
  8376. pc_addfame(*sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8377. break;
  8378. }
  8379. }
  8380. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8381. return SCRIPT_CMD_SUCCESS;
  8382. }
  8383. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8384. script_pushint(st, -1);
  8385. return SCRIPT_CMD_FAILURE;
  8386. }
  8387. /*==========================================
  8388. * Show a failed Refine +1 attempt
  8389. * failedrefitem <equipment slot>{,<char_id>}
  8390. *------------------------------------------*/
  8391. BUILDIN_FUNC(failedrefitem) {
  8392. short i = -1;
  8393. int pos;
  8394. TBL_PC *sd;
  8395. pos = script_getnum(st,2);
  8396. if (!script_charid2sd(3, sd)) {
  8397. script_pushint(st, 0);
  8398. return SCRIPT_CMD_FAILURE;
  8399. }
  8400. if (equip_index_check(pos))
  8401. i = pc_checkequip(sd,equip_bitmask[pos]);
  8402. if (i >= 0) {
  8403. sd->inventory.u.items_inventory[i].refine = 0;
  8404. pc_unequipitem(sd,i,3); //recalculate bonus
  8405. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8406. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8407. clif_misceffect(&sd->bl,2); // display failure effect
  8408. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8409. script_pushint(st, 1);
  8410. return SCRIPT_CMD_SUCCESS;
  8411. }
  8412. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8413. script_pushint(st, 0);
  8414. return SCRIPT_CMD_FAILURE;
  8415. }
  8416. /*==========================================
  8417. * Downgrades an Equipment Part by -1 . [Masao]
  8418. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8419. *------------------------------------------*/
  8420. BUILDIN_FUNC(downrefitem) {
  8421. short i = -1, down = 1;
  8422. int pos;
  8423. TBL_PC *sd;
  8424. if (!script_charid2sd(4, sd)) {
  8425. script_pushint(st, -1);
  8426. return SCRIPT_CMD_FAILURE;
  8427. }
  8428. pos = script_getnum(st,2);
  8429. if (script_hasdata(st, 3))
  8430. down = script_getnum(st, 3);
  8431. if (equip_index_check(pos))
  8432. i = pc_checkequip(sd,equip_bitmask[pos]);
  8433. if (i >= 0) {
  8434. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8435. //Logs items, got from (N)PC scripts [Lupus]
  8436. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8437. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8438. sd->inventory.u.items_inventory[i].refine -= down;
  8439. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8440. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8441. clif_delitem(sd,i,1,3);
  8442. //Logs items, got from (N)PC scripts [Lupus]
  8443. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8444. clif_additem(sd,i,1,0);
  8445. pc_equipitem(sd,i,ep);
  8446. clif_misceffect(&sd->bl,2);
  8447. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8448. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8449. return SCRIPT_CMD_SUCCESS;
  8450. }
  8451. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8452. script_pushint(st, -1);
  8453. return SCRIPT_CMD_FAILURE;
  8454. }
  8455. /**
  8456. * Delete the item equipped at pos.
  8457. * delequip <equipment slot>{,<char_id>};
  8458. **/
  8459. BUILDIN_FUNC(delequip) {
  8460. short i = -1;
  8461. int pos;
  8462. int8 ret;
  8463. TBL_PC *sd;
  8464. pos = script_getnum(st,2);
  8465. if (!script_charid2sd(3,sd)) {
  8466. st->state = END;
  8467. return SCRIPT_CMD_FAILURE;
  8468. }
  8469. if (equip_index_check(pos))
  8470. i = pc_checkequip(sd,equip_bitmask[pos]);
  8471. if (i >= 0) {
  8472. pc_unequipitem(sd,i,3); //recalculate bonus
  8473. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8474. }
  8475. else {
  8476. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8477. st->state = END;
  8478. return SCRIPT_CMD_FAILURE;
  8479. }
  8480. script_pushint(st,ret);
  8481. return SCRIPT_CMD_SUCCESS;
  8482. }
  8483. /**
  8484. * Break the item equipped at pos.
  8485. * breakequip <equipment slot>{,<char_id>};
  8486. **/
  8487. BUILDIN_FUNC(breakequip) {
  8488. short i = -1;
  8489. int pos;
  8490. TBL_PC *sd;
  8491. pos = script_getnum(st,2);
  8492. if (!script_charid2sd(3,sd))
  8493. return SCRIPT_CMD_FAILURE;
  8494. if (equip_index_check(pos))
  8495. i = pc_checkequip(sd,equip_bitmask[pos]);
  8496. if (i >= 0) {
  8497. sd->inventory.u.items_inventory[i].attribute = 1;
  8498. pc_unequipitem(sd,i,3);
  8499. clif_equiplist(sd);
  8500. script_pushint(st,1);
  8501. return SCRIPT_CMD_SUCCESS;
  8502. }
  8503. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8504. script_pushint(st,0);
  8505. return SCRIPT_CMD_FAILURE;
  8506. }
  8507. /**
  8508. * statusup <stat>{,<char_id>};
  8509. **/
  8510. BUILDIN_FUNC(statusup)
  8511. {
  8512. int type;
  8513. TBL_PC *sd;
  8514. type = script_getnum(st,2);
  8515. if (!script_charid2sd(3, sd))
  8516. return SCRIPT_CMD_FAILURE;
  8517. pc_statusup(sd, type, 1);
  8518. return SCRIPT_CMD_SUCCESS;
  8519. }
  8520. /**
  8521. * statusup2 <stat>,<amount>{,<char_id>};
  8522. **/
  8523. BUILDIN_FUNC(statusup2)
  8524. {
  8525. int type,val;
  8526. TBL_PC *sd;
  8527. type=script_getnum(st,2);
  8528. val=script_getnum(st,3);
  8529. if (!script_charid2sd(4, sd))
  8530. return SCRIPT_CMD_FAILURE;
  8531. pc_statusup2(sd,type,val);
  8532. return SCRIPT_CMD_SUCCESS;
  8533. }
  8534. /**
  8535. * traitstatusup <stat>{,<char_id>};
  8536. **/
  8537. BUILDIN_FUNC(traitstatusup)
  8538. {
  8539. map_session_data *sd;
  8540. if (!script_charid2sd(3, sd))
  8541. return SCRIPT_CMD_FAILURE;
  8542. int type = script_getnum( st, 2 );
  8543. if( type < SP_POW || type > SP_CRT ){
  8544. ShowError( "buildin_traitstatusup: Unknown trait type %d\n", type );
  8545. return SCRIPT_CMD_FAILURE;
  8546. }
  8547. pc_traitstatusup( sd, type, 1 );
  8548. return SCRIPT_CMD_SUCCESS;
  8549. }
  8550. /**
  8551. * traitstatusup2 <stat>,<amount>{,<char_id>};
  8552. **/
  8553. BUILDIN_FUNC(traitstatusup2)
  8554. {
  8555. map_session_data *sd;
  8556. if (!script_charid2sd(4, sd))
  8557. return SCRIPT_CMD_FAILURE;
  8558. int type = script_getnum( st, 2 );
  8559. if( type < SP_POW || type > SP_CRT ){
  8560. ShowError( "buildin_traitstatusup2: Unknown trait type %d\n", type );
  8561. return SCRIPT_CMD_FAILURE;
  8562. }
  8563. pc_traitstatusup2( sd, type, script_getnum( st, 3 ) );
  8564. return SCRIPT_CMD_SUCCESS;
  8565. }
  8566. /// See 'doc/item_bonus.txt'
  8567. ///
  8568. /// bonus <bonus type>,<val1>;
  8569. /// bonus2 <bonus type>,<val1>,<val2>;
  8570. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8571. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8572. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8573. BUILDIN_FUNC(bonus)
  8574. {
  8575. int type;
  8576. int val1 = 0;
  8577. int val2 = 0;
  8578. int val3 = 0;
  8579. int val4 = 0;
  8580. int val5 = 0;
  8581. TBL_PC* sd;
  8582. if( !script_rid2sd(sd) )
  8583. return SCRIPT_CMD_SUCCESS; // no player attached
  8584. type = script_getnum(st,2);
  8585. switch( type ) {
  8586. case SP_AUTOSPELL:
  8587. case SP_AUTOSPELL_WHENHIT:
  8588. case SP_AUTOSPELL_ONSKILL:
  8589. case SP_SKILL_ATK:
  8590. case SP_SKILL_HEAL:
  8591. case SP_SKILL_HEAL2:
  8592. case SP_ADD_SKILL_BLOW:
  8593. case SP_CASTRATE:
  8594. case SP_ADDEFF_ONSKILL:
  8595. case SP_SKILL_USE_SP_RATE:
  8596. case SP_SKILL_COOLDOWN:
  8597. case SP_SKILL_FIXEDCAST:
  8598. case SP_SKILL_VARIABLECAST:
  8599. case SP_VARCASTRATE:
  8600. case SP_FIXCASTRATE:
  8601. case SP_SKILL_DELAY:
  8602. case SP_SKILL_USE_SP:
  8603. case SP_SUB_SKILL:
  8604. // these bonuses support skill names
  8605. if (script_isstring(st, 3)) {
  8606. const char *name = script_getstr(st, 3);
  8607. if (!(val1 = skill_name2id(name))) {
  8608. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8609. return SCRIPT_CMD_FAILURE;
  8610. }
  8611. } else {
  8612. val1 = script_getnum(st, 3);
  8613. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8614. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8615. return SCRIPT_CMD_FAILURE;
  8616. }
  8617. }
  8618. break;
  8619. default:
  8620. if (script_hasdata(st, 3))
  8621. val1 = script_getnum(st, 3);
  8622. break;
  8623. }
  8624. switch( script_lastdata(st)-2 ) {
  8625. case 0:
  8626. case 1:
  8627. pc_bonus(sd, type, val1);
  8628. break;
  8629. case 2:
  8630. val2 = script_getnum(st,4);
  8631. pc_bonus2(sd, type, val1, val2);
  8632. break;
  8633. case 3:
  8634. val2 = script_getnum(st,4);
  8635. val3 = script_getnum(st,5);
  8636. pc_bonus3(sd, type, val1, val2, val3);
  8637. break;
  8638. case 4:
  8639. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8640. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8641. else
  8642. val2 = script_getnum(st,4);
  8643. val3 = script_getnum(st,5);
  8644. val4 = script_getnum(st,6);
  8645. pc_bonus4(sd, type, val1, val2, val3, val4);
  8646. break;
  8647. case 5:
  8648. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8649. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8650. else
  8651. val2 = script_getnum(st,4);
  8652. val3 = script_getnum(st,5);
  8653. val4 = script_getnum(st,6);
  8654. val5 = script_getnum(st,7);
  8655. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8656. break;
  8657. default:
  8658. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8659. break;
  8660. }
  8661. return SCRIPT_CMD_SUCCESS;
  8662. }
  8663. BUILDIN_FUNC(autobonus)
  8664. {
  8665. unsigned int dur, pos;
  8666. short rate;
  8667. uint16 atk_type = 0;
  8668. TBL_PC* sd;
  8669. const char *bonus_script, *other_script = NULL;
  8670. if( !script_rid2sd(sd) )
  8671. return SCRIPT_CMD_SUCCESS; // no player attached
  8672. if (current_equip_combo_pos)
  8673. pos = current_equip_combo_pos;
  8674. else
  8675. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8676. rate = script_getnum(st,3);
  8677. dur = script_getnum(st,4);
  8678. bonus_script = script_getstr(st,2);
  8679. if( !rate || !dur || !pos || !bonus_script )
  8680. return SCRIPT_CMD_SUCCESS;
  8681. if( script_hasdata(st,5) )
  8682. atk_type = script_getnum(st,5);
  8683. if( script_hasdata(st,6) )
  8684. other_script = script_getstr(st,6);
  8685. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8686. {
  8687. script_add_autobonus(bonus_script);
  8688. if( other_script )
  8689. script_add_autobonus(other_script);
  8690. }
  8691. return SCRIPT_CMD_SUCCESS;
  8692. }
  8693. BUILDIN_FUNC(autobonus2)
  8694. {
  8695. unsigned int dur, pos;
  8696. short rate;
  8697. uint16 atk_type = 0;
  8698. TBL_PC* sd;
  8699. const char *bonus_script, *other_script = NULL;
  8700. if( !script_rid2sd(sd) )
  8701. return SCRIPT_CMD_SUCCESS; // no player attached
  8702. if (current_equip_combo_pos)
  8703. pos = current_equip_combo_pos;
  8704. else
  8705. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8706. rate = script_getnum(st,3);
  8707. dur = script_getnum(st,4);
  8708. bonus_script = script_getstr(st,2);
  8709. if( !rate || !dur || !pos || !bonus_script )
  8710. return SCRIPT_CMD_SUCCESS;
  8711. if( script_hasdata(st,5) )
  8712. atk_type = script_getnum(st,5);
  8713. if( script_hasdata(st,6) )
  8714. other_script = script_getstr(st,6);
  8715. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8716. {
  8717. script_add_autobonus(bonus_script);
  8718. if( other_script )
  8719. script_add_autobonus(other_script);
  8720. }
  8721. return SCRIPT_CMD_SUCCESS;
  8722. }
  8723. BUILDIN_FUNC(autobonus3)
  8724. {
  8725. unsigned int dur, pos;
  8726. short rate;
  8727. uint16 skill_id = 0;
  8728. TBL_PC* sd;
  8729. const char *bonus_script, *other_script = NULL;
  8730. if( !script_rid2sd(sd) )
  8731. return SCRIPT_CMD_SUCCESS; // no player attached
  8732. if (current_equip_combo_pos)
  8733. pos = current_equip_combo_pos;
  8734. else
  8735. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8736. rate = script_getnum(st,3);
  8737. dur = script_getnum(st,4);
  8738. if (script_isstring(st, 5)) {
  8739. const char *name = script_getstr(st, 5);
  8740. if (!(skill_id = skill_name2id(name))) {
  8741. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8742. return SCRIPT_CMD_FAILURE;
  8743. }
  8744. } else {
  8745. skill_id = script_getnum(st, 5);
  8746. if (!skill_get_index(skill_id)) {
  8747. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8748. return SCRIPT_CMD_FAILURE;
  8749. }
  8750. }
  8751. bonus_script = script_getstr(st,2);
  8752. if( !rate || !dur || !pos || !bonus_script )
  8753. return SCRIPT_CMD_SUCCESS;
  8754. if( script_hasdata(st,6) )
  8755. other_script = script_getstr(st,6);
  8756. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8757. {
  8758. script_add_autobonus(bonus_script);
  8759. if( other_script )
  8760. script_add_autobonus(other_script);
  8761. }
  8762. return SCRIPT_CMD_SUCCESS;
  8763. }
  8764. BUILDIN_FUNC(petautobonus) {
  8765. map_session_data *sd;
  8766. if (!script_rid2sd(sd))
  8767. return SCRIPT_CMD_FAILURE; // No player attached
  8768. const char *command = script_getfuncname(st);
  8769. if (sd->pd == nullptr) {
  8770. ShowError("buildin_%s: Requires an active pet.\n", command);
  8771. return SCRIPT_CMD_FAILURE; // No pet attached to player
  8772. }
  8773. std::string bonus_script = script_getstr(st, 2);
  8774. int16 rate = script_getnum(st, 3);
  8775. uint32 dur = script_getnum(st, 4);
  8776. if (!rate || !dur || bonus_script.empty())
  8777. return SCRIPT_CMD_FAILURE;
  8778. uint16 atk_type = 0;
  8779. std::string other_script = {};
  8780. bool bonus2 = false;
  8781. if (strcmpi(command, "petautobonus2") == 0)
  8782. bonus2 = true;
  8783. if (script_hasdata(st, 5))
  8784. atk_type = script_getnum(st, 5);
  8785. if (script_hasdata(st, 6))
  8786. other_script = script_getstr(st, 6);
  8787. if (pet_addautobonus(bonus2 ? sd->pd->autobonus2 : sd->pd->autobonus, bonus_script, rate, dur, atk_type, other_script, false)) {
  8788. script_add_petautobonus(bonus_script);
  8789. if (!other_script.empty())
  8790. script_add_petautobonus(other_script);
  8791. }
  8792. return SCRIPT_CMD_SUCCESS;
  8793. }
  8794. BUILDIN_FUNC(petautobonus3) {
  8795. map_session_data *sd;
  8796. if (!script_rid2sd(sd))
  8797. return SCRIPT_CMD_SUCCESS; // No player attached
  8798. if (sd->pd == nullptr) {
  8799. ShowError("buildin_petautobonus3: Requires an active pet.\n");
  8800. return SCRIPT_CMD_FAILURE; // No pet attached to player
  8801. }
  8802. std::string bonus_script = script_getstr(st, 2);
  8803. int16 rate = script_getnum(st, 3);
  8804. uint32 dur = script_getnum(st, 4);
  8805. if (!rate || !dur || bonus_script.empty())
  8806. return SCRIPT_CMD_SUCCESS;
  8807. uint16 skill_id = 0;
  8808. std::string other_script = {};
  8809. if (script_isstring(st, 5)) {
  8810. const char *name = script_getstr(st, 5);
  8811. if (!(skill_id = skill_name2id(name))) {
  8812. ShowError("buildin_petautobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8813. return SCRIPT_CMD_FAILURE;
  8814. }
  8815. } else {
  8816. skill_id = script_getnum(st, 5);
  8817. if (!skill_get_index(skill_id)) {
  8818. ShowError("buildin_petautobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8819. return SCRIPT_CMD_FAILURE;
  8820. }
  8821. }
  8822. if (script_hasdata(st, 6))
  8823. other_script = script_getstr(st, 6);
  8824. if (pet_addautobonus(sd->pd->autobonus3, bonus_script, rate, dur, skill_id, other_script, true)) {
  8825. script_add_petautobonus(bonus_script);
  8826. if (!other_script.empty())
  8827. script_add_petautobonus(other_script);
  8828. }
  8829. return SCRIPT_CMD_SUCCESS;
  8830. }
  8831. BUILDIN_FUNC(plagiarizeskill)
  8832. {
  8833. TBL_PC *sd;
  8834. if (script_rid2sd(sd))
  8835. script_pushint(st, pc_skill_plagiarism(*sd, script_getnum(st, 2), script_getnum(st, 3)));
  8836. return SCRIPT_CMD_SUCCESS;
  8837. }
  8838. BUILDIN_FUNC(plagiarizeskillreset)
  8839. {
  8840. TBL_PC *sd;
  8841. if (script_rid2sd(sd))
  8842. script_pushint(st, pc_skill_plagiarism_reset(*sd, script_getnum(st, 2)));
  8843. return SCRIPT_CMD_SUCCESS;
  8844. }
  8845. /// Changes the level of a player skill.
  8846. /// <flag> defaults to 1
  8847. /// <flag>=0 : set the level of the skill
  8848. /// <flag>=1 : set the temporary level of the skill
  8849. /// <flag>=2 : add to the level of the skill
  8850. ///
  8851. /// skill <skill id>,<level>,<flag>
  8852. /// skill <skill id>,<level>
  8853. /// skill "<skill name>",<level>,<flag>
  8854. /// skill "<skill name>",<level>
  8855. BUILDIN_FUNC(skill)
  8856. {
  8857. int id;
  8858. int level;
  8859. int flag = ADDSKILL_TEMP;
  8860. TBL_PC* sd;
  8861. const char* command = script_getfuncname(st);
  8862. if( !script_rid2sd(sd) )
  8863. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8864. if (strcmpi(command, "addtoskill") == 0)
  8865. flag = ADDSKILL_TEMP_ADDLEVEL;
  8866. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8867. level = script_getnum(st,3);
  8868. if( script_hasdata(st,4) )
  8869. flag = script_getnum(st,4);
  8870. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8871. return SCRIPT_CMD_SUCCESS;
  8872. }
  8873. /// Increases the level of a guild skill.
  8874. ///
  8875. /// guildskill <skill id>,<amount>;
  8876. /// guildskill "<skill name>",<amount>;
  8877. BUILDIN_FUNC(guildskill)
  8878. {
  8879. int id;
  8880. int level;
  8881. TBL_PC* sd;
  8882. int i;
  8883. if( !script_rid2sd(sd) )
  8884. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8885. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8886. level = script_getnum(st,3);
  8887. for( i=0; i < level; i++ )
  8888. guild_skillup(sd, id);
  8889. return SCRIPT_CMD_SUCCESS;
  8890. }
  8891. /// Returns the level of the player skill.
  8892. ///
  8893. /// getskilllv(<skill id>) -> <level>
  8894. /// getskilllv("<skill name>") -> <level>
  8895. BUILDIN_FUNC(getskilllv)
  8896. {
  8897. int id;
  8898. TBL_PC* sd;
  8899. if( !script_rid2sd(sd) )
  8900. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8901. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8902. script_pushint(st, pc_checkskill(sd,id));
  8903. return SCRIPT_CMD_SUCCESS;
  8904. }
  8905. /// Returns the level of the guild skill.
  8906. ///
  8907. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8908. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8909. BUILDIN_FUNC(getgdskilllv)
  8910. {
  8911. int guild_id;
  8912. uint16 skill_id;
  8913. guild_id = script_getnum(st,2);
  8914. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8915. auto g = guild_search(guild_id);
  8916. if (!g)
  8917. script_pushint(st, -1);
  8918. else
  8919. script_pushint(st, guild_checkskill(g->guild,skill_id));
  8920. return SCRIPT_CMD_SUCCESS;
  8921. }
  8922. /// Returns the 'basic_skill_check' setting.
  8923. /// This config determines if the server checks the skill level of NV_BASIC
  8924. /// before allowing the basic actions.
  8925. ///
  8926. /// basicskillcheck() -> <bool>
  8927. BUILDIN_FUNC(basicskillcheck)
  8928. {
  8929. script_pushint(st, battle_config.basic_skill_check);
  8930. return SCRIPT_CMD_SUCCESS;
  8931. }
  8932. /// Returns the GM level of the player.
  8933. ///
  8934. /// getgmlevel({<char_id>}) -> <level>
  8935. BUILDIN_FUNC(getgmlevel)
  8936. {
  8937. TBL_PC* sd;
  8938. if (!script_charid2sd(2,sd))
  8939. return SCRIPT_CMD_FAILURE;
  8940. script_pushint(st, pc_get_group_level(sd));
  8941. return SCRIPT_CMD_SUCCESS;
  8942. }
  8943. /// Returns the group ID of the player.
  8944. ///
  8945. /// getgroupid({<char_id>}) -> <int>
  8946. BUILDIN_FUNC(getgroupid)
  8947. {
  8948. TBL_PC* sd;
  8949. if (!script_charid2sd(2,sd))
  8950. return SCRIPT_CMD_FAILURE;
  8951. script_pushint(st, pc_get_group_id(sd));
  8952. return SCRIPT_CMD_SUCCESS;
  8953. }
  8954. /// Terminates the execution of this script instance.
  8955. ///
  8956. /// end
  8957. BUILDIN_FUNC(end)
  8958. {
  8959. TBL_PC* sd;
  8960. sd = map_id2sd(st->rid);
  8961. st->state = END;
  8962. npc_data* nd = map_id2nd( st->oid );
  8963. if( nd != nullptr && nd->dynamicnpc.owner_char_id != 0 ){
  8964. nd->dynamicnpc.last_interaction = gettick();
  8965. }
  8966. if( st->mes_active )
  8967. st->mes_active = 0;
  8968. if (sd){
  8969. if (sd->state.callshop == 0)
  8970. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8971. else
  8972. sd->state.callshop = 0;
  8973. }
  8974. return SCRIPT_CMD_SUCCESS;
  8975. }
  8976. /// Checks if the player has that effect state (option).
  8977. ///
  8978. /// checkoption(<option>{,<char_id>}) -> <bool>
  8979. BUILDIN_FUNC(checkoption)
  8980. {
  8981. int option;
  8982. TBL_PC* sd;
  8983. if (!script_charid2sd(3,sd))
  8984. return SCRIPT_CMD_FAILURE;
  8985. option = script_getnum(st,2);
  8986. if( sd->sc.option&option )
  8987. script_pushint(st, 1);
  8988. else
  8989. script_pushint(st, 0);
  8990. return SCRIPT_CMD_SUCCESS;
  8991. }
  8992. /// Checks if the player is in that body state (opt1).
  8993. ///
  8994. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8995. BUILDIN_FUNC(checkoption1)
  8996. {
  8997. int opt1;
  8998. TBL_PC* sd;
  8999. if (!script_charid2sd(3,sd))
  9000. return SCRIPT_CMD_FAILURE;
  9001. opt1 = script_getnum(st,2);
  9002. if( sd->sc.opt1 == opt1 )
  9003. script_pushint(st, 1);
  9004. else
  9005. script_pushint(st, 0);
  9006. return SCRIPT_CMD_SUCCESS;
  9007. }
  9008. /// Checks if the player has that health state (opt2).
  9009. ///
  9010. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  9011. BUILDIN_FUNC(checkoption2)
  9012. {
  9013. int opt2;
  9014. TBL_PC* sd;
  9015. if (!script_charid2sd(3,sd))
  9016. return SCRIPT_CMD_FAILURE;
  9017. opt2 = script_getnum(st,2);
  9018. if( sd->sc.opt2&opt2 )
  9019. script_pushint(st, 1);
  9020. else
  9021. script_pushint(st, 0);
  9022. return SCRIPT_CMD_SUCCESS;
  9023. }
  9024. /// Changes the effect state (option) of the player.
  9025. /// <flag> defaults to 1
  9026. /// <flag>=0 : removes the option
  9027. /// <flag>=other : adds the option
  9028. ///
  9029. /// setoption <option>{,<flag>{,<char_id>}};
  9030. BUILDIN_FUNC(setoption)
  9031. {
  9032. int option;
  9033. int flag = 1;
  9034. TBL_PC* sd;
  9035. if (!script_charid2sd(4,sd))
  9036. return SCRIPT_CMD_FAILURE;
  9037. option = script_getnum(st,2);
  9038. if( script_hasdata(st,3) )
  9039. flag = script_getnum(st,3);
  9040. else if( !option ){// Request to remove everything.
  9041. flag = 0;
  9042. option = OPTION_FALCON|OPTION_RIDING;
  9043. #ifndef NEW_CARTS
  9044. option |= OPTION_CART;
  9045. #endif
  9046. }
  9047. if( flag ){// Add option
  9048. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  9049. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  9050. pc_setoption(sd, sd->sc.option|option);
  9051. } else// Remove option
  9052. pc_setoption(sd, sd->sc.option&~option);
  9053. return SCRIPT_CMD_SUCCESS;
  9054. }
  9055. /// Returns if the player has a cart.
  9056. ///
  9057. /// checkcart({char_id}) -> <bool>
  9058. ///
  9059. /// @author Valaris
  9060. BUILDIN_FUNC(checkcart)
  9061. {
  9062. TBL_PC* sd;
  9063. if (!script_charid2sd(2,sd))
  9064. return SCRIPT_CMD_FAILURE;
  9065. if( pc_iscarton(sd) )
  9066. script_pushint(st, 1);
  9067. else
  9068. script_pushint(st, 0);
  9069. return SCRIPT_CMD_SUCCESS;
  9070. }
  9071. /// Sets the cart of the player.
  9072. /// <type> defaults to 1
  9073. /// <type>=0 : removes the cart
  9074. /// <type>=1 : Normal cart
  9075. /// <type>=2 : Wooden cart
  9076. /// <type>=3 : Covered cart with flowers and ferns
  9077. /// <type>=4 : Wooden cart with a Panda doll on the back
  9078. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  9079. ///
  9080. /// setcart {<type>{,<char_id>}};
  9081. BUILDIN_FUNC(setcart)
  9082. {
  9083. int type = 1;
  9084. TBL_PC* sd;
  9085. if (!script_charid2sd(3,sd))
  9086. return SCRIPT_CMD_FAILURE;
  9087. if( script_hasdata(st,2) )
  9088. type = script_getnum(st,2);
  9089. pc_setcart(sd, type);
  9090. return SCRIPT_CMD_SUCCESS;
  9091. }
  9092. /// Returns if the player has a falcon.
  9093. ///
  9094. /// checkfalcon({char_id}) -> <bool>
  9095. ///
  9096. /// @author Valaris
  9097. BUILDIN_FUNC(checkfalcon)
  9098. {
  9099. TBL_PC* sd;
  9100. if (!script_charid2sd(2,sd))
  9101. return SCRIPT_CMD_FAILURE;
  9102. if( pc_isfalcon(sd) )
  9103. script_pushint(st, 1);
  9104. else
  9105. script_pushint(st, 0);
  9106. return SCRIPT_CMD_SUCCESS;
  9107. }
  9108. /// Sets if the player has a falcon or not.
  9109. /// <flag> defaults to 1
  9110. ///
  9111. /// setfalcon {<flag>{,<char_id>}};
  9112. BUILDIN_FUNC(setfalcon)
  9113. {
  9114. int flag = 1;
  9115. TBL_PC* sd;
  9116. if (!script_charid2sd(3,sd))
  9117. return SCRIPT_CMD_FAILURE;
  9118. if( script_hasdata(st,2) )
  9119. flag = script_getnum(st,2);
  9120. pc_setfalcon(sd, flag);
  9121. return SCRIPT_CMD_SUCCESS;
  9122. }
  9123. /// Returns if the player is riding.
  9124. ///
  9125. /// checkriding({char_id}) -> <bool>
  9126. ///
  9127. /// @author Valaris
  9128. BUILDIN_FUNC(checkriding)
  9129. {
  9130. TBL_PC* sd;
  9131. if (!script_charid2sd(2,sd))
  9132. return SCRIPT_CMD_FAILURE;
  9133. if( pc_isriding(sd) )
  9134. script_pushint(st, 1);
  9135. else
  9136. script_pushint(st, 0);
  9137. return SCRIPT_CMD_SUCCESS;
  9138. }
  9139. /// Sets if the player is riding.
  9140. /// <flag> defaults to 1
  9141. ///
  9142. /// setriding {<flag>{,<char_id>}};
  9143. BUILDIN_FUNC(setriding)
  9144. {
  9145. int flag = 1;
  9146. TBL_PC* sd;
  9147. if (!script_charid2sd(3,sd))
  9148. return SCRIPT_CMD_FAILURE;
  9149. if( script_hasdata(st,2) )
  9150. flag = script_getnum(st,2);
  9151. pc_setriding(sd, flag);
  9152. return SCRIPT_CMD_SUCCESS;
  9153. }
  9154. /// Returns if the player has a warg.
  9155. ///
  9156. /// checkwug({char_id}) -> <bool>
  9157. ///
  9158. BUILDIN_FUNC(checkwug)
  9159. {
  9160. TBL_PC* sd;
  9161. if (!script_charid2sd(2,sd))
  9162. return SCRIPT_CMD_FAILURE;
  9163. if( pc_iswug(sd) || pc_isridingwug(sd) )
  9164. script_pushint(st, 1);
  9165. else
  9166. script_pushint(st, 0);
  9167. return SCRIPT_CMD_SUCCESS;
  9168. }
  9169. /// Returns if the player is wearing MADO Gear.
  9170. ///
  9171. /// checkmadogear({<char_id>}) -> <bool>
  9172. ///
  9173. BUILDIN_FUNC(checkmadogear)
  9174. {
  9175. TBL_PC* sd;
  9176. if (!script_charid2sd(2,sd))
  9177. return SCRIPT_CMD_FAILURE;
  9178. if( pc_ismadogear(sd) )
  9179. script_pushint(st, 1);
  9180. else
  9181. script_pushint(st, 0);
  9182. return SCRIPT_CMD_SUCCESS;
  9183. }
  9184. /// Sets if the player is riding MADO Gear.
  9185. /// <flag> defaults to true
  9186. /// <type> defaults to MADO_ROBOT
  9187. ///
  9188. /// setmadogear {<flag>{,type{,<char_id>}}};
  9189. BUILDIN_FUNC(setmadogear)
  9190. {
  9191. bool flag = true;
  9192. TBL_PC* sd;
  9193. uint16 type = MADO_ROBOT;
  9194. if (!script_charid2sd(4,sd))
  9195. return SCRIPT_CMD_FAILURE;
  9196. if( script_hasdata(st,2) )
  9197. flag = script_getnum(st,2) != 0;
  9198. if (script_hasdata(st, 3)) {
  9199. type = script_getnum(st, 3);
  9200. if (type == MADO_UNUSED || type >= MADO_MAX) {
  9201. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  9202. type = MADO_ROBOT;
  9203. }
  9204. }
  9205. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  9206. return SCRIPT_CMD_SUCCESS;
  9207. }
  9208. /// Sets the save point of the player.
  9209. ///
  9210. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  9211. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  9212. BUILDIN_FUNC(savepoint)
  9213. {
  9214. int x, y, m, cid_pos = 5;
  9215. unsigned short map_idx;
  9216. const char* str;
  9217. TBL_PC* sd;
  9218. if (script_lastdata(st) > 5)
  9219. cid_pos = 7;
  9220. if (!script_charid2sd(cid_pos,sd))
  9221. return SCRIPT_CMD_FAILURE;// no player attached, report source
  9222. str = script_getstr(st, 2);
  9223. map_idx = mapindex_name2id(str);
  9224. if( !map_idx )
  9225. return SCRIPT_CMD_FAILURE;
  9226. x = script_getnum(st,3);
  9227. y = script_getnum(st,4);
  9228. m = map_mapindex2mapid(map_idx);
  9229. if (cid_pos == 7) {
  9230. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  9231. x1 = x + dx, y1 = y + dy,
  9232. x0 = x - dx, y0 = y - dy;
  9233. uint8 n = 10;
  9234. do {
  9235. x = rnd_value(x0, x1);
  9236. y = rnd_value(y0, y1);
  9237. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  9238. }
  9239. // Check for valid coordinates if map in local map-server
  9240. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  9241. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  9242. return SCRIPT_CMD_FAILURE;
  9243. }
  9244. pc_setsavepoint(sd, map_idx, x, y);
  9245. return SCRIPT_CMD_SUCCESS;
  9246. }
  9247. /*==========================================
  9248. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  9249. *------------------------------------------*/
  9250. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  9251. {
  9252. int type;
  9253. time_t timer;
  9254. struct tm *t;
  9255. type=script_getnum(st,2);
  9256. switch(type){
  9257. case 2:
  9258. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  9259. // from the system clock.)
  9260. script_pushint(st,(int)time(NULL));
  9261. break;
  9262. case 1:
  9263. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  9264. time(&timer);
  9265. t=localtime(&timer);
  9266. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  9267. break;
  9268. case 0:
  9269. default:
  9270. //type 0:(System Ticks)
  9271. script_pushint(st,gettick());
  9272. break;
  9273. }
  9274. return SCRIPT_CMD_SUCCESS;
  9275. }
  9276. /*==========================================
  9277. * GetTime(Type)
  9278. *
  9279. * Returns the current value of a certain date type.
  9280. * Possible types are:
  9281. * - DT_SECOND Current seconds
  9282. * - DT_MINUTE Current minute
  9283. * - DT_HOUR Current hour
  9284. * - DT_DAYOFWEEK Day of current week
  9285. * - DT_DAYOFMONTH Day of current month
  9286. * - DT_MONTH Current month
  9287. * - DT_YEAR Current year
  9288. * - DT_DAYOFYEAR Day of current year
  9289. *
  9290. * If none of the above types is supplied -1 will be returned to the script
  9291. * and the script source will be reported into the mapserver console.
  9292. *------------------------------------------*/
  9293. BUILDIN_FUNC(gettime)
  9294. {
  9295. int type;
  9296. type = script_getnum(st,2);
  9297. if( type <= DT_MIN || type >= DT_MAX ){
  9298. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  9299. script_reportsrc(st);
  9300. script_pushint(st,-1);
  9301. }else{
  9302. script_pushint(st,date_get((enum e_date_type)type));
  9303. }
  9304. return SCRIPT_CMD_SUCCESS;
  9305. }
  9306. /**
  9307. * Returns the current server time or the provided time in a readable format.
  9308. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  9309. */
  9310. BUILDIN_FUNC(gettimestr)
  9311. {
  9312. char *tmpstr;
  9313. const char *fmtstr;
  9314. int maxlen;
  9315. time_t now;
  9316. fmtstr = script_getstr(st,2);
  9317. maxlen = script_getnum(st,3);
  9318. if (script_hasdata(st, 4)) {
  9319. if (script_getnum(st, 4) < 0) {
  9320. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  9321. return SCRIPT_CMD_FAILURE;
  9322. } else
  9323. now = (time_t)script_getnum(st, 4);
  9324. } else
  9325. now = time(NULL);
  9326. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  9327. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  9328. tmpstr[maxlen] = '\0';
  9329. script_pushstr(st,tmpstr);
  9330. return SCRIPT_CMD_SUCCESS;
  9331. }
  9332. /*==========================================
  9333. * Open player storage
  9334. *------------------------------------------*/
  9335. BUILDIN_FUNC(openstorage)
  9336. {
  9337. TBL_PC* sd;
  9338. if( !script_rid2sd(sd) )
  9339. return SCRIPT_CMD_SUCCESS;
  9340. storage_storageopen(sd);
  9341. return SCRIPT_CMD_SUCCESS;
  9342. }
  9343. BUILDIN_FUNC(guildopenstorage)
  9344. {
  9345. TBL_PC* sd;
  9346. int ret;
  9347. if( !script_rid2sd(sd) )
  9348. return SCRIPT_CMD_SUCCESS;
  9349. ret = storage_guild_storageopen(sd);
  9350. script_pushint(st,ret);
  9351. return SCRIPT_CMD_SUCCESS;
  9352. }
  9353. BUILDIN_FUNC(guildopenstorage_log){
  9354. #if PACKETVER < 20140205
  9355. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  9356. return SCRIPT_CMD_FAILURE;
  9357. #else
  9358. map_session_data* sd;
  9359. if( !script_charid2sd( 2, sd ) ){
  9360. return SCRIPT_CMD_FAILURE;
  9361. }
  9362. script_pushint( st, storage_guild_log_read( sd ) );
  9363. return SCRIPT_CMD_SUCCESS;
  9364. #endif
  9365. }
  9366. BUILDIN_FUNC(guild_has_permission){
  9367. map_session_data* sd;
  9368. if( !script_charid2sd( 3, sd ) ){
  9369. return SCRIPT_CMD_FAILURE;
  9370. }
  9371. int permission = script_getnum(st,2);
  9372. if( permission == 0 ){
  9373. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  9374. return SCRIPT_CMD_FAILURE;
  9375. }
  9376. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  9377. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  9378. return SCRIPT_CMD_FAILURE;
  9379. }
  9380. if( !sd->guild ){
  9381. script_pushint( st, false );
  9382. return SCRIPT_CMD_SUCCESS;
  9383. }
  9384. int position = guild_getposition(*sd);
  9385. if( position < 0 || ( sd->guild->guild.position[position].mode&permission ) != permission ){
  9386. script_pushint( st, false );
  9387. return SCRIPT_CMD_SUCCESS;
  9388. }
  9389. script_pushint( st, true );
  9390. return SCRIPT_CMD_SUCCESS;
  9391. }
  9392. /*==========================================
  9393. * Make player use a skill trought item usage
  9394. *------------------------------------------*/
  9395. /// itemskill <skill id>,<level>
  9396. /// itemskill "<skill name>",<level>
  9397. BUILDIN_FUNC(itemskill)
  9398. {
  9399. int id;
  9400. int lv;
  9401. bool keep_requirement;
  9402. TBL_PC* sd;
  9403. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9404. return SCRIPT_CMD_SUCCESS;
  9405. if (script_isstring(st, 2)) {
  9406. const char *name = script_getstr(st, 2);
  9407. if (!(id = skill_name2id(name))) {
  9408. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9409. return SCRIPT_CMD_FAILURE;
  9410. }
  9411. } else {
  9412. id = script_getnum(st, 2);
  9413. if (!skill_get_index(id)) {
  9414. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9415. return SCRIPT_CMD_FAILURE;
  9416. }
  9417. }
  9418. lv = script_getnum(st,3);
  9419. if (script_hasdata(st, 4)) {
  9420. keep_requirement = (script_getnum(st, 4) != 0);
  9421. } else {
  9422. keep_requirement = false;
  9423. }
  9424. sd->skillitem=id;
  9425. sd->skillitemlv=lv;
  9426. sd->skillitem_keep_requirement = keep_requirement;
  9427. clif_item_skill(sd,id,lv);
  9428. return SCRIPT_CMD_SUCCESS;
  9429. }
  9430. /*==========================================
  9431. * Attempt to create an item
  9432. *------------------------------------------*/
  9433. BUILDIN_FUNC(produce)
  9434. {
  9435. int trigger;
  9436. TBL_PC* sd;
  9437. if( !script_rid2sd(sd) )
  9438. return SCRIPT_CMD_SUCCESS;
  9439. trigger=script_getnum(st,2);
  9440. clif_skill_produce_mix_list(sd, -1, trigger);
  9441. return SCRIPT_CMD_SUCCESS;
  9442. }
  9443. /*==========================================
  9444. *
  9445. *------------------------------------------*/
  9446. BUILDIN_FUNC(cooking)
  9447. {
  9448. int trigger;
  9449. TBL_PC* sd;
  9450. if( !script_rid2sd(sd) )
  9451. return SCRIPT_CMD_SUCCESS;
  9452. trigger=script_getnum(st,2);
  9453. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  9454. return SCRIPT_CMD_SUCCESS;
  9455. }
  9456. /*==========================================
  9457. * Create a pet
  9458. *------------------------------------------*/
  9459. BUILDIN_FUNC(makepet)
  9460. {
  9461. map_session_data* sd;
  9462. uint16 mob_id;
  9463. if( !script_rid2sd(sd) )
  9464. return SCRIPT_CMD_FAILURE;
  9465. mob_id = script_getnum(st,2);
  9466. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9467. if( !pet ){
  9468. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9469. return SCRIPT_CMD_FAILURE;
  9470. }
  9471. sd->catch_target_class = mob_id;
  9472. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9473. 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() );
  9474. return SCRIPT_CMD_SUCCESS;
  9475. }
  9476. /**
  9477. * Give player exp base,job * quest_exp_rate/100
  9478. * getexp <base xp>,<job xp>{,<char_id>};
  9479. **/
  9480. BUILDIN_FUNC(getexp){
  9481. map_session_data* sd;
  9482. if( !script_charid2sd( 4, sd ) ){
  9483. return SCRIPT_CMD_FAILURE;
  9484. }
  9485. int64 base = script_getnum64( st, 2 );
  9486. if( base < 0 ){
  9487. ShowError( "buildin_getexp: Called with negative base exp.\n" );
  9488. return SCRIPT_CMD_FAILURE;
  9489. }
  9490. int64 job = script_getnum64( st, 3 );
  9491. if( job < 0 ){
  9492. ShowError( "buildin_getexp: Called with negative job exp.\n" );
  9493. return SCRIPT_CMD_FAILURE;
  9494. }
  9495. if( base == 0 && job == 0 ){
  9496. ShowError( "buildin_getexp: Called with base and job exp 0.\n" );
  9497. return SCRIPT_CMD_FAILURE;
  9498. }
  9499. // bonus for npc-given exp
  9500. double bonus = battle_config.quest_exp_rate / 100.;
  9501. if (base)
  9502. base = (int64) cap_value(base * bonus, 0, MAX_EXP);
  9503. if (job)
  9504. job = (int64) cap_value(job * bonus, 0, MAX_EXP);
  9505. pc_gainexp(sd, NULL, base, job, 1);
  9506. #ifdef RENEWAL
  9507. if (base && sd->hd)
  9508. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9509. #endif
  9510. return SCRIPT_CMD_SUCCESS;
  9511. }
  9512. /*==========================================
  9513. * Gain guild exp [Celest]
  9514. *------------------------------------------*/
  9515. BUILDIN_FUNC(guildgetexp){
  9516. map_session_data* sd;
  9517. if( !script_rid2sd( sd ) ){
  9518. return SCRIPT_CMD_FAILURE;
  9519. }
  9520. int64 exp = script_getnum64( st, 2 );
  9521. if( exp <= 0 ){
  9522. ShowError( "buildin_guildgetexp: Called with exp <= 0.\n" );
  9523. return SCRIPT_CMD_FAILURE;
  9524. }
  9525. if( sd->status.guild_id <= 0 ){
  9526. 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 );
  9527. return SCRIPT_CMD_FAILURE;
  9528. }
  9529. guild_getexp( sd, exp );
  9530. return SCRIPT_CMD_SUCCESS;
  9531. }
  9532. /*==========================================
  9533. * Changes the guild master of a guild [Skotlex]
  9534. *------------------------------------------*/
  9535. BUILDIN_FUNC(guildchangegm)
  9536. {
  9537. TBL_PC *sd;
  9538. int guild_id;
  9539. const char *name;
  9540. guild_id = script_getnum(st,2);
  9541. name = script_getstr(st,3);
  9542. sd=map_nick2sd(name,false);
  9543. if (!sd)
  9544. script_pushint(st,0);
  9545. else
  9546. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9547. return SCRIPT_CMD_SUCCESS;
  9548. }
  9549. /*==========================================
  9550. * Spawn a monster:
  9551. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9552. * *monster "<map name>",<x>,<y>,"<name to show>","<mob name>",<amount>{,"<event label>",<size>,<ai>};
  9553. *------------------------------------------*/
  9554. BUILDIN_FUNC(monster)
  9555. {
  9556. const char* mapn = script_getstr(st,2);
  9557. int x = script_getnum(st,3);
  9558. int y = script_getnum(st,4);
  9559. const char* str = script_getstr(st,5);
  9560. int class_;
  9561. int amount = script_getnum(st,7);
  9562. const char* event = "";
  9563. unsigned int size = SZ_SMALL;
  9564. enum mob_ai ai = AI_NONE;
  9565. map_session_data* sd;
  9566. int16 m;
  9567. int i;
  9568. if( script_isstring( st, 6 ) ){
  9569. const char* name = script_getstr( st, 6 );
  9570. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  9571. if( mob == nullptr ){
  9572. ShowWarning( "buildin_monster: Attempted to spawn non-existing monster \"%s\"\n", name );
  9573. return SCRIPT_CMD_FAILURE;
  9574. }
  9575. class_ = mob->id;
  9576. }else{
  9577. class_ = script_getnum( st, 6 );
  9578. if( class_ >= 0 && !mobdb_checkid( class_ ) ){
  9579. ShowWarning( "buildin_monster: Attempted to spawn non-existing monster class %d\n", class_ );
  9580. return SCRIPT_CMD_FAILURE;
  9581. }
  9582. }
  9583. if (script_hasdata(st, 8)) {
  9584. event = script_getstr(st, 8);
  9585. check_event(st, event);
  9586. }
  9587. if (script_hasdata(st, 9)) {
  9588. size = script_getnum(st, 9);
  9589. if (size > SZ_BIG) {
  9590. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9591. return SCRIPT_CMD_FAILURE;
  9592. }
  9593. }
  9594. if (script_hasdata(st, 10)) {
  9595. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9596. if (ai >= AI_MAX) {
  9597. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9598. return SCRIPT_CMD_FAILURE;
  9599. }
  9600. }
  9601. sd = map_id2sd(st->rid);
  9602. if (sd && strcmp(mapn, "this") == 0)
  9603. m = sd->bl.m;
  9604. else
  9605. m = map_mapname2mapid(mapn);
  9606. TBL_MOB* md;
  9607. for(i = 0; i < amount; i++) { //not optimised
  9608. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9609. if (mobid > 0) {
  9610. md = map_id2md(mobid);
  9611. if (md)
  9612. md->next_walktime = INVALID_TIMER;
  9613. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9614. }
  9615. }
  9616. return SCRIPT_CMD_SUCCESS;
  9617. }
  9618. /*==========================================
  9619. * Request List of Monster Drops
  9620. *------------------------------------------*/
  9621. BUILDIN_FUNC(getmobdrops)
  9622. {
  9623. int class_ = script_getnum(st,2);
  9624. int i, j = 0;
  9625. if( !mobdb_checkid(class_) )
  9626. {
  9627. script_pushint(st, 0);
  9628. return SCRIPT_CMD_SUCCESS;
  9629. }
  9630. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9631. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9632. {
  9633. if( mob->dropitem[i].nameid == 0 )
  9634. continue;
  9635. if( !item_db.exists(mob->dropitem[i].nameid) )
  9636. continue;
  9637. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9638. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9639. mapreg_setreg(reference_uid(add_str("$@MobDrop_nosteal"), j), mob->dropitem[i].steal_protected);
  9640. mapreg_setreg(reference_uid(add_str("$@MobDrop_randomopt"), j), mob->dropitem[i].randomopt_group);
  9641. j++;
  9642. }
  9643. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9644. script_pushint(st, 1);
  9645. return SCRIPT_CMD_SUCCESS;
  9646. }
  9647. /*==========================================
  9648. * Spawn a monster in a random location
  9649. * in x0,x1,y0,y1 area.
  9650. *------------------------------------------*/
  9651. BUILDIN_FUNC(areamonster)
  9652. {
  9653. const char* mapn = script_getstr(st,2);
  9654. int x0 = script_getnum(st,3);
  9655. int y0 = script_getnum(st,4);
  9656. int x1 = script_getnum(st,5);
  9657. int y1 = script_getnum(st,6);
  9658. const char* str = script_getstr(st,7);
  9659. int class_;
  9660. int amount = script_getnum(st,9);
  9661. const char* event = "";
  9662. unsigned int size = SZ_SMALL;
  9663. enum mob_ai ai = AI_NONE;
  9664. map_session_data* sd;
  9665. int16 m;
  9666. int i;
  9667. if( script_isstring( st, 8 ) ){
  9668. const char* name = script_getstr( st, 8 );
  9669. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  9670. if( mob == nullptr ){
  9671. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing monster \"%s\"\n", name );
  9672. return SCRIPT_CMD_FAILURE;
  9673. }
  9674. class_ = mob->id;
  9675. }else{
  9676. class_ = script_getnum( st, 8 );
  9677. if( class_ >= 0 && !mobdb_checkid( class_ ) ){
  9678. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing monster class %d\n", class_ );
  9679. return SCRIPT_CMD_FAILURE;
  9680. }
  9681. }
  9682. if (script_hasdata(st,10)) {
  9683. event = script_getstr(st, 10);
  9684. check_event(st, event);
  9685. }
  9686. if (script_hasdata(st, 11)) {
  9687. size = script_getnum(st, 11);
  9688. if (size > 3) {
  9689. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_ );
  9690. return SCRIPT_CMD_FAILURE;
  9691. }
  9692. }
  9693. if (script_hasdata(st, 12)) {
  9694. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9695. if (ai >= AI_MAX) {
  9696. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_ );
  9697. return SCRIPT_CMD_FAILURE;
  9698. }
  9699. }
  9700. sd = map_id2sd(st->rid);
  9701. if (sd && strcmp(mapn, "this") == 0)
  9702. m = sd->bl.m;
  9703. else
  9704. m = map_mapname2mapid(mapn);
  9705. TBL_MOB* md;
  9706. for(i = 0; i < amount; i++) { //not optimised
  9707. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9708. if (mobid > 0) {
  9709. md = map_id2md(mobid);
  9710. if (md)
  9711. md->next_walktime = INVALID_TIMER;
  9712. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9713. }
  9714. }
  9715. return SCRIPT_CMD_SUCCESS;
  9716. }
  9717. /*==========================================
  9718. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9719. *------------------------------------------*/
  9720. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9721. { //same fix but with killmonster instead - stripping events from mobs.
  9722. TBL_MOB* md = (TBL_MOB*)bl;
  9723. char *event=va_arg(ap,char *);
  9724. int allflag=va_arg(ap,int);
  9725. md->state.npc_killmonster = 1;
  9726. if(!allflag){
  9727. if(strcmp(event,md->npc_event)==0)
  9728. status_kill(bl);
  9729. }else{
  9730. if(!md->spawn)
  9731. status_kill(bl);
  9732. }
  9733. md->state.npc_killmonster = 0;
  9734. return SCRIPT_CMD_SUCCESS;
  9735. }
  9736. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9737. {
  9738. TBL_MOB* md = (TBL_MOB*)bl;
  9739. char *event=va_arg(ap,char *);
  9740. int allflag=va_arg(ap,int);
  9741. if(!allflag){
  9742. if(strcmp(event,md->npc_event)==0)
  9743. status_kill(bl);
  9744. }else{
  9745. if(!md->spawn)
  9746. status_kill(bl);
  9747. }
  9748. return SCRIPT_CMD_SUCCESS;
  9749. }
  9750. BUILDIN_FUNC(killmonster)
  9751. {
  9752. const char *mapname,*event;
  9753. int16 m,allflag=0;
  9754. mapname=script_getstr(st,2);
  9755. event=script_getstr(st,3);
  9756. if(strcmp(event,"All")==0)
  9757. allflag = 1;
  9758. else
  9759. check_event(st, event);
  9760. if( (m=map_mapname2mapid(mapname))<0 )
  9761. return SCRIPT_CMD_SUCCESS;
  9762. if( script_hasdata(st,4) ) {
  9763. if ( script_getnum(st,4) == 1 ) {
  9764. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9765. return SCRIPT_CMD_SUCCESS;
  9766. }
  9767. }
  9768. map_freeblock_lock();
  9769. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9770. map_freeblock_unlock();
  9771. return SCRIPT_CMD_SUCCESS;
  9772. }
  9773. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9774. { //Strips the event from the mob if it's killed the old method.
  9775. struct mob_data *md;
  9776. md = BL_CAST(BL_MOB, bl);
  9777. if (md->npc_event[0])
  9778. md->npc_event[0] = 0;
  9779. status_kill(bl);
  9780. return 0;
  9781. }
  9782. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9783. {
  9784. status_kill(bl);
  9785. return 0;
  9786. }
  9787. BUILDIN_FUNC(killmonsterall)
  9788. {
  9789. const char *mapname;
  9790. int16 m;
  9791. mapname=script_getstr(st,2);
  9792. if( (m = map_mapname2mapid(mapname))<0 )
  9793. return SCRIPT_CMD_SUCCESS;
  9794. if( script_hasdata(st,3) ) {
  9795. if ( script_getnum(st,3) == 1 ) {
  9796. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9797. return SCRIPT_CMD_SUCCESS;
  9798. }
  9799. }
  9800. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9801. return SCRIPT_CMD_SUCCESS;
  9802. }
  9803. /*==========================================
  9804. * Creates a clone of a player.
  9805. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9806. *------------------------------------------*/
  9807. BUILDIN_FUNC(clone)
  9808. {
  9809. TBL_PC *sd, *msd=NULL;
  9810. uint32 char_id, master_id = 0, x, y, flag = 0;
  9811. int16 m;
  9812. enum e_mode mode = MD_NONE;
  9813. unsigned int duration = 0;
  9814. const char *mapname,*event;
  9815. mapname=script_getstr(st,2);
  9816. x=script_getnum(st,3);
  9817. y=script_getnum(st,4);
  9818. event=script_getstr(st,5);
  9819. char_id=script_getnum(st,6);
  9820. if( script_hasdata(st,7) )
  9821. master_id=script_getnum(st,7);
  9822. if( script_hasdata(st,8) )
  9823. mode=static_cast<e_mode>(script_getnum(st,8));
  9824. if( script_hasdata(st,9) )
  9825. flag=script_getnum(st,9);
  9826. if( script_hasdata(st,10) )
  9827. duration=script_getnum(st,10);
  9828. check_event(st, event);
  9829. m = map_mapname2mapid(mapname);
  9830. if (m < 0)
  9831. return SCRIPT_CMD_SUCCESS;
  9832. sd = map_charid2sd(char_id);
  9833. if (master_id) {
  9834. msd = map_charid2sd(master_id);
  9835. if (msd)
  9836. master_id = msd->bl.id;
  9837. else
  9838. master_id = 0;
  9839. }
  9840. if (sd) //Return ID of newly crafted clone.
  9841. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9842. else //Failed to create clone.
  9843. script_pushint(st,0);
  9844. return SCRIPT_CMD_SUCCESS;
  9845. }
  9846. /*==========================================
  9847. *------------------------------------------*/
  9848. BUILDIN_FUNC(doevent)
  9849. {
  9850. const char* event;
  9851. map_session_data* sd;
  9852. if( !script_rid2sd(sd) )
  9853. return SCRIPT_CMD_SUCCESS;
  9854. event = script_getstr(st,2);
  9855. check_event(st, event);
  9856. npc_event(sd, event, 0);
  9857. return SCRIPT_CMD_SUCCESS;
  9858. }
  9859. /*==========================================
  9860. *------------------------------------------*/
  9861. BUILDIN_FUNC(donpcevent)
  9862. {
  9863. const char* event = script_getstr(st,2);
  9864. check_event(st, event);
  9865. if( !npc_event_do(event) ) {
  9866. struct npc_data * nd = map_id2nd(st->oid);
  9867. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9868. script_pushint(st, 0);
  9869. } else
  9870. script_pushint(st, 1);
  9871. return SCRIPT_CMD_SUCCESS;
  9872. }
  9873. /// for Aegis compatibility
  9874. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9875. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9876. {
  9877. const char* npc = script_getstr(st,2);
  9878. const char* command = script_getstr(st,3);
  9879. char event[EVENT_NAME_LENGTH];
  9880. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9881. check_event(st, event);
  9882. if( npc_event_do(event) ){
  9883. script_pushint(st, true);
  9884. }else{
  9885. struct npc_data * nd = map_id2nd(st->oid);
  9886. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9887. script_pushint(st, false);
  9888. }
  9889. return SCRIPT_CMD_SUCCESS;
  9890. }
  9891. /*==========================================
  9892. *------------------------------------------*/
  9893. BUILDIN_FUNC(addtimer)
  9894. {
  9895. int tick;
  9896. const char* event;
  9897. TBL_PC* sd;
  9898. if( !script_rid2sd(sd) )
  9899. return SCRIPT_CMD_SUCCESS;
  9900. tick = script_getnum(st,2);
  9901. event = script_getstr(st, 3);
  9902. check_event(st, event);
  9903. if (!pc_addeventtimer(sd,tick,event)) {
  9904. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9905. return SCRIPT_CMD_FAILURE;
  9906. }
  9907. return SCRIPT_CMD_SUCCESS;
  9908. }
  9909. /*==========================================
  9910. *------------------------------------------*/
  9911. BUILDIN_FUNC(deltimer)
  9912. {
  9913. const char *event;
  9914. TBL_PC* sd;
  9915. if( !script_rid2sd(sd) )
  9916. return SCRIPT_CMD_SUCCESS;
  9917. event=script_getstr(st, 2);
  9918. check_event(st, event);
  9919. pc_deleventtimer(sd,event);
  9920. return SCRIPT_CMD_SUCCESS;
  9921. }
  9922. /*==========================================
  9923. *------------------------------------------*/
  9924. BUILDIN_FUNC(addtimercount)
  9925. {
  9926. const char *event;
  9927. int tick;
  9928. TBL_PC* sd;
  9929. if( !script_rid2sd(sd) )
  9930. return SCRIPT_CMD_SUCCESS;
  9931. tick=script_getnum(st,2);
  9932. event=script_getstr(st,3);
  9933. check_event(st, event);
  9934. pc_addeventtimercount(sd,event,tick);
  9935. return SCRIPT_CMD_SUCCESS;
  9936. }
  9937. /*==========================================
  9938. *------------------------------------------*/
  9939. BUILDIN_FUNC(initnpctimer)
  9940. {
  9941. struct npc_data *nd;
  9942. int flag = 0;
  9943. if( script_hasdata(st,3) )
  9944. { //Two arguments: NPC name and attach flag.
  9945. nd = npc_name2id(script_getstr(st, 2));
  9946. flag = script_getnum(st,3);
  9947. }
  9948. else if( script_hasdata(st,2) )
  9949. { //Check if argument is numeric (flag) or string (npc name)
  9950. if( script_isstring(st, 2) ) //NPC name
  9951. nd = npc_name2id(script_getstr(st, 2));
  9952. else //Flag
  9953. {
  9954. nd = (struct npc_data *)map_id2bl(st->oid);
  9955. flag = script_getnum(st, 2);
  9956. }
  9957. }
  9958. else
  9959. nd = (struct npc_data *)map_id2bl(st->oid);
  9960. if( !nd )
  9961. return SCRIPT_CMD_SUCCESS;
  9962. if( flag ) //Attach
  9963. {
  9964. TBL_PC* sd;
  9965. if( !script_rid2sd(sd) )
  9966. return SCRIPT_CMD_SUCCESS;
  9967. nd->u.scr.rid = sd->bl.id;
  9968. }
  9969. nd->u.scr.timertick = 0;
  9970. npc_settimerevent_tick(nd,0);
  9971. npc_timerevent_start(nd, st->rid);
  9972. return SCRIPT_CMD_SUCCESS;
  9973. }
  9974. /*==========================================
  9975. *------------------------------------------*/
  9976. BUILDIN_FUNC(startnpctimer)
  9977. {
  9978. struct npc_data *nd;
  9979. int flag = 0;
  9980. if( script_hasdata(st,3) )
  9981. { //Two arguments: NPC name and attach flag.
  9982. nd = npc_name2id(script_getstr(st, 2));
  9983. flag = script_getnum(st,3);
  9984. }
  9985. else if( script_hasdata(st,2) )
  9986. { //Check if argument is numeric (flag) or string (npc name)
  9987. if( script_isstring(st, 2) ) //NPC name
  9988. nd = npc_name2id(script_getstr(st, 2));
  9989. else //Flag
  9990. {
  9991. nd = (struct npc_data *)map_id2bl(st->oid);
  9992. flag = script_getnum(st, 2);
  9993. }
  9994. }
  9995. else
  9996. nd=(struct npc_data *)map_id2bl(st->oid);
  9997. if( !nd )
  9998. return SCRIPT_CMD_SUCCESS;
  9999. if( flag ) //Attach
  10000. {
  10001. TBL_PC* sd;
  10002. if( !script_rid2sd(sd) )
  10003. return SCRIPT_CMD_SUCCESS;
  10004. nd->u.scr.rid = sd->bl.id;
  10005. }
  10006. npc_timerevent_start(nd, st->rid);
  10007. return SCRIPT_CMD_SUCCESS;
  10008. }
  10009. /*==========================================
  10010. *------------------------------------------*/
  10011. BUILDIN_FUNC(stopnpctimer)
  10012. {
  10013. struct npc_data *nd;
  10014. int flag = 0;
  10015. if( script_hasdata(st,3) )
  10016. { //Two arguments: NPC name and attach flag.
  10017. nd = npc_name2id(script_getstr(st, 2));
  10018. flag = script_getnum(st,3);
  10019. }
  10020. else if( script_hasdata(st,2) )
  10021. { //Check if argument is numeric (flag) or string (npc name)
  10022. if( script_isstring(st, 2) ) //NPC name
  10023. nd = npc_name2id(script_getstr(st, 2));
  10024. else //Flag
  10025. {
  10026. nd = (struct npc_data *)map_id2bl(st->oid);
  10027. flag = script_getnum(st, 2);
  10028. }
  10029. }
  10030. else
  10031. nd=(struct npc_data *)map_id2bl(st->oid);
  10032. if( !nd )
  10033. return SCRIPT_CMD_SUCCESS;
  10034. if( flag ) //Detach
  10035. nd->u.scr.rid = 0;
  10036. npc_timerevent_stop(nd);
  10037. return SCRIPT_CMD_SUCCESS;
  10038. }
  10039. /*==========================================
  10040. *------------------------------------------*/
  10041. BUILDIN_FUNC(getnpctimer)
  10042. {
  10043. struct npc_data *nd;
  10044. TBL_PC *sd;
  10045. int type = script_getnum(st,2);
  10046. t_tick val = 0;
  10047. if( script_hasdata(st,3) )
  10048. nd = npc_name2id(script_getstr(st,3));
  10049. else
  10050. nd = (struct npc_data *)map_id2bl(st->oid);
  10051. if( !nd || nd->bl.type != BL_NPC )
  10052. {
  10053. script_pushint(st,0);
  10054. ShowError("getnpctimer: Invalid NPC.\n");
  10055. return SCRIPT_CMD_FAILURE;
  10056. }
  10057. switch( type )
  10058. {
  10059. case 0: val = npc_gettimerevent_tick(nd); break;
  10060. case 1:
  10061. if( nd->u.scr.rid )
  10062. {
  10063. sd = map_id2sd(nd->u.scr.rid);
  10064. if( !sd )
  10065. {
  10066. ShowError("buildin_getnpctimer: Attached player not found!\n");
  10067. break;
  10068. }
  10069. val = (sd->npc_timer_id != INVALID_TIMER);
  10070. }
  10071. else
  10072. val = (nd->u.scr.timerid != INVALID_TIMER);
  10073. break;
  10074. case 2: val = nd->u.scr.timeramount; break;
  10075. }
  10076. script_pushint(st,val);
  10077. return SCRIPT_CMD_SUCCESS;
  10078. }
  10079. /*==========================================
  10080. *------------------------------------------*/
  10081. BUILDIN_FUNC(setnpctimer)
  10082. {
  10083. int tick;
  10084. struct npc_data *nd;
  10085. tick = script_getnum(st,2);
  10086. if( script_hasdata(st,3) )
  10087. nd = npc_name2id(script_getstr(st,3));
  10088. else
  10089. nd = (struct npc_data *)map_id2bl(st->oid);
  10090. if( !nd || nd->bl.type != BL_NPC )
  10091. {
  10092. script_pushint(st,1);
  10093. ShowError("setnpctimer: Invalid NPC.\n");
  10094. return SCRIPT_CMD_FAILURE;
  10095. }
  10096. npc_settimerevent_tick(nd,tick);
  10097. script_pushint(st,0);
  10098. return SCRIPT_CMD_SUCCESS;
  10099. }
  10100. /*==========================================
  10101. * attaches the player rid to the timer [Celest]
  10102. *------------------------------------------*/
  10103. BUILDIN_FUNC(attachnpctimer)
  10104. {
  10105. TBL_PC *sd;
  10106. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  10107. if( !nd || nd->bl.type != BL_NPC )
  10108. {
  10109. script_pushint(st,1);
  10110. ShowError("setnpctimer: Invalid NPC.\n");
  10111. return SCRIPT_CMD_FAILURE;
  10112. }
  10113. if( !script_nick2sd(2,sd) ){
  10114. script_pushint(st,1);
  10115. ShowWarning("attachnpctimer: Invalid player.\n");
  10116. return SCRIPT_CMD_FAILURE;
  10117. }
  10118. nd->u.scr.rid = sd->bl.id;
  10119. script_pushint(st,0);
  10120. return SCRIPT_CMD_SUCCESS;
  10121. }
  10122. /*==========================================
  10123. * detaches a player rid from the timer [Celest]
  10124. *------------------------------------------*/
  10125. BUILDIN_FUNC(detachnpctimer)
  10126. {
  10127. struct npc_data *nd;
  10128. if( script_hasdata(st,2) )
  10129. nd = npc_name2id(script_getstr(st,2));
  10130. else
  10131. nd = (struct npc_data *)map_id2bl(st->oid);
  10132. if( !nd || nd->bl.type != BL_NPC )
  10133. {
  10134. script_pushint(st,1);
  10135. ShowError("detachnpctimer: Invalid NPC.\n");
  10136. return SCRIPT_CMD_FAILURE;
  10137. }
  10138. nd->u.scr.rid = 0;
  10139. script_pushint(st,0);
  10140. return SCRIPT_CMD_SUCCESS;
  10141. }
  10142. /*==========================================
  10143. * To avoid "player not attached" script errors, this function is provided,
  10144. * it checks if there is a player attached to the current script. [Skotlex]
  10145. * If no, returns 0, if yes, returns the account_id of the attached player.
  10146. *------------------------------------------*/
  10147. BUILDIN_FUNC(playerattached)
  10148. {
  10149. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  10150. script_pushint(st,0);
  10151. else
  10152. script_pushint(st,st->rid);
  10153. return SCRIPT_CMD_SUCCESS;
  10154. }
  10155. /*==========================================
  10156. *------------------------------------------*/
  10157. BUILDIN_FUNC(announce)
  10158. {
  10159. const char *mes = script_getstr(st,2);
  10160. int flag = script_getnum(st,3);
  10161. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  10162. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  10163. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  10164. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  10165. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  10166. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  10167. {
  10168. send_target target;
  10169. struct block_list *bl;
  10170. // If bc_npc flag is set, use NPC as broadcast source
  10171. if(flag&BC_NPC){
  10172. bl = map_id2bl(st->oid);
  10173. }else{
  10174. map_session_data* sd;
  10175. if(script_charid2sd(9, sd))
  10176. bl = &sd->bl;
  10177. else
  10178. bl = NULL;
  10179. }
  10180. if (bl == NULL)
  10181. return SCRIPT_CMD_SUCCESS;
  10182. switch (flag&BC_TARGET_MASK) {
  10183. case BC_MAP: target = ALL_SAMEMAP; break;
  10184. case BC_AREA: target = AREA; break;
  10185. case BC_SELF: target = SELF; break;
  10186. default: target = ALL_CLIENT; break; // BC_ALL
  10187. }
  10188. if (fontColor)
  10189. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  10190. else
  10191. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  10192. }
  10193. else
  10194. {
  10195. if (fontColor)
  10196. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  10197. else
  10198. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  10199. }
  10200. return SCRIPT_CMD_SUCCESS;
  10201. }
  10202. /*==========================================
  10203. *------------------------------------------*/
  10204. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  10205. {
  10206. char *mes = va_arg(ap, char *);
  10207. int len = va_arg(ap, int);
  10208. int type = va_arg(ap, int);
  10209. char *fontColor = va_arg(ap, char *);
  10210. short fontType = (short)va_arg(ap, int);
  10211. short fontSize = (short)va_arg(ap, int);
  10212. short fontAlign = (short)va_arg(ap, int);
  10213. short fontY = (short)va_arg(ap, int);
  10214. if (fontColor)
  10215. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  10216. else
  10217. clif_broadcast(bl, mes, len, type, SELF);
  10218. return SCRIPT_CMD_SUCCESS;
  10219. }
  10220. BUILDIN_FUNC(mapannounce)
  10221. {
  10222. const char *mapname = script_getstr(st,2);
  10223. const char *mes = script_getstr(st,3);
  10224. int flag = script_getnum(st,4);
  10225. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  10226. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  10227. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  10228. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  10229. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  10230. int16 m;
  10231. if ((m = map_mapname2mapid(mapname)) < 0)
  10232. return SCRIPT_CMD_SUCCESS;
  10233. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  10234. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  10235. return SCRIPT_CMD_SUCCESS;
  10236. }
  10237. /*==========================================
  10238. *------------------------------------------*/
  10239. BUILDIN_FUNC(areaannounce)
  10240. {
  10241. const char *mapname = script_getstr(st,2);
  10242. int x0 = script_getnum(st,3);
  10243. int y0 = script_getnum(st,4);
  10244. int x1 = script_getnum(st,5);
  10245. int y1 = script_getnum(st,6);
  10246. const char *mes = script_getstr(st,7);
  10247. int flag = script_getnum(st,8);
  10248. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  10249. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  10250. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  10251. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  10252. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  10253. int16 m;
  10254. if ((m = map_mapname2mapid(mapname)) < 0)
  10255. return SCRIPT_CMD_SUCCESS;
  10256. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  10257. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  10258. return SCRIPT_CMD_SUCCESS;
  10259. }
  10260. /*==========================================
  10261. *------------------------------------------*/
  10262. BUILDIN_FUNC(getusers)
  10263. {
  10264. int flag, val = 0;
  10265. map_session_data* sd;
  10266. struct block_list* bl = NULL;
  10267. flag = script_getnum(st,2);
  10268. switch(flag&0x07)
  10269. {
  10270. case 0:
  10271. if(flag&0x8)
  10272. {// npc
  10273. bl = map_id2bl(st->oid);
  10274. }
  10275. else if(script_rid2sd(sd))
  10276. {// pc
  10277. bl = &sd->bl;
  10278. }
  10279. if(bl)
  10280. {
  10281. val = map_getmapdata(bl->m)->users;
  10282. }
  10283. break;
  10284. case 1:
  10285. val = map_getusers();
  10286. break;
  10287. default:
  10288. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  10289. script_pushint(st,0);
  10290. return SCRIPT_CMD_FAILURE;
  10291. }
  10292. script_pushint(st,val);
  10293. return SCRIPT_CMD_SUCCESS;
  10294. }
  10295. /*==========================================
  10296. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  10297. *------------------------------------------*/
  10298. BUILDIN_FUNC(getmapguildusers)
  10299. {
  10300. const char *str;
  10301. int16 m;
  10302. int gid;
  10303. int c=0;
  10304. str=script_getstr(st,2);
  10305. gid=script_getnum(st,3);
  10306. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  10307. script_pushint(st,-1);
  10308. return SCRIPT_CMD_SUCCESS;
  10309. }
  10310. auto g = guild_search(gid);
  10311. if (g){
  10312. unsigned short i;
  10313. for(i = 0; i < g->guild.max_member; i++)
  10314. {
  10315. if (g->guild.member[i].sd && g->guild.member[i].sd->bl.m == m)
  10316. c++;
  10317. }
  10318. }
  10319. script_pushint(st,c);
  10320. return SCRIPT_CMD_SUCCESS;
  10321. }
  10322. /*==========================================
  10323. *------------------------------------------*/
  10324. BUILDIN_FUNC(getmapusers)
  10325. {
  10326. const char *str;
  10327. int16 m;
  10328. str=script_getstr(st,2);
  10329. if( (m=map_mapname2mapid(str))< 0){
  10330. script_pushint(st,-1);
  10331. return SCRIPT_CMD_SUCCESS;
  10332. }
  10333. script_pushint(st,map_getmapdata(m)->users);
  10334. return SCRIPT_CMD_SUCCESS;
  10335. }
  10336. /*==========================================
  10337. *------------------------------------------*/
  10338. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  10339. {
  10340. int *users = va_arg(ap, int *);
  10341. (*users)++;
  10342. return SCRIPT_CMD_SUCCESS;
  10343. }
  10344. BUILDIN_FUNC(getareausers)
  10345. {
  10346. const char *str;
  10347. int16 m,x0,y0,x1,y1;
  10348. int users = 0;
  10349. str=script_getstr(st,2);
  10350. x0=script_getnum(st,3);
  10351. y0=script_getnum(st,4);
  10352. x1=script_getnum(st,5);
  10353. y1=script_getnum(st,6);
  10354. if( (m=map_mapname2mapid(str))< 0){
  10355. script_pushint(st,-1);
  10356. return SCRIPT_CMD_SUCCESS;
  10357. }
  10358. map_foreachinallarea(buildin_getareausers_sub,
  10359. m,x0,y0,x1,y1,BL_PC,&users);
  10360. script_pushint(st,users);
  10361. return SCRIPT_CMD_SUCCESS;
  10362. }
  10363. /*==========================================
  10364. * getunits(<type>{,<array_variable>[<first value>]})
  10365. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  10366. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  10367. *------------------------------------------*/
  10368. BUILDIN_FUNC(getunits)
  10369. {
  10370. struct block_list *bl = NULL;
  10371. map_session_data *sd = NULL;
  10372. struct script_data *data = NULL;
  10373. char *command = (char *)script_getfuncname(st);
  10374. const char *str;
  10375. const char *name;
  10376. int type = script_getnum(st, 2);
  10377. int size = 0;
  10378. int32 idx, id;
  10379. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  10380. if (!strcmp(command, "getmapunits"))
  10381. {
  10382. str = script_getstr(st, 3);
  10383. if ((m = map_mapname2mapid(str)) < 0) {
  10384. script_pushint(st, -1);
  10385. st->state = END;
  10386. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10387. return SCRIPT_CMD_FAILURE;
  10388. }
  10389. if (script_hasdata(st, 4))
  10390. data = script_getdata(st, 4);
  10391. }
  10392. else if (!strcmp(command, "getareaunits"))
  10393. {
  10394. str = script_getstr(st, 3);
  10395. if ((m = map_mapname2mapid(str)) < 0) {
  10396. script_pushint(st, -1);
  10397. st->state = END;
  10398. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10399. return SCRIPT_CMD_FAILURE;
  10400. }
  10401. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  10402. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  10403. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  10404. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  10405. if (script_hasdata(st, 8))
  10406. data = script_getdata(st, 8);
  10407. }
  10408. else
  10409. {
  10410. if (script_hasdata(st, 3))
  10411. data = script_getdata(st, 3);
  10412. }
  10413. if (data)
  10414. {
  10415. if (!data_isreference(data))
  10416. {
  10417. ShowError("buildin_%s: not a variable\n", command);
  10418. script_reportdata(data);
  10419. st->state = END;
  10420. return SCRIPT_CMD_FAILURE;
  10421. }
  10422. id = reference_getid(data);
  10423. idx = reference_getindex(data);
  10424. name = reference_getname(data);
  10425. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  10426. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  10427. return SCRIPT_CMD_FAILURE;
  10428. }
  10429. }
  10430. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  10431. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  10432. {
  10433. 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)))
  10434. {
  10435. if( data ){
  10436. if( is_string_variable( name ) ){
  10437. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  10438. }else{
  10439. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  10440. }
  10441. }
  10442. size++;
  10443. }
  10444. }
  10445. mapit_free(iter);
  10446. script_pushint(st, size);
  10447. return SCRIPT_CMD_SUCCESS;
  10448. }
  10449. /*==========================================
  10450. *------------------------------------------*/
  10451. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10452. {
  10453. t_itemid nameid = va_arg(ap, t_itemid);
  10454. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  10455. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10456. if(drop->item.nameid==nameid)
  10457. (*amount)+=drop->item.amount;
  10458. return SCRIPT_CMD_SUCCESS;
  10459. }
  10460. BUILDIN_FUNC(getareadropitem)
  10461. {
  10462. const char *str;
  10463. int16 m,x0,y0,x1,y1;
  10464. t_itemid nameid;
  10465. unsigned short amount = 0;
  10466. str=script_getstr(st,2);
  10467. x0=script_getnum(st,3);
  10468. y0=script_getnum(st,4);
  10469. x1=script_getnum(st,5);
  10470. y1=script_getnum(st,6);
  10471. if( script_isstring(st, 7) ){
  10472. const char *name = script_getstr(st, 7);
  10473. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  10474. if( item_data )
  10475. nameid=item_data->nameid;
  10476. else{
  10477. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10478. return SCRIPT_CMD_FAILURE;
  10479. }
  10480. }else{
  10481. nameid = script_getnum(st, 7);
  10482. if( !item_db.exists( nameid ) ){
  10483. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10484. return SCRIPT_CMD_FAILURE;
  10485. }
  10486. }
  10487. if( (m=map_mapname2mapid(str))< 0){
  10488. script_pushint(st,-1);
  10489. return SCRIPT_CMD_SUCCESS;
  10490. }
  10491. map_foreachinallarea(buildin_getareadropitem_sub,
  10492. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10493. script_pushint(st,amount);
  10494. return SCRIPT_CMD_SUCCESS;
  10495. }
  10496. /*==========================================
  10497. *------------------------------------------*/
  10498. BUILDIN_FUNC(enablenpc)
  10499. {
  10500. npc_data* nd;
  10501. e_npcv_status flag = NPCVIEW_DISABLE;
  10502. const char* command = script_getfuncname(st);
  10503. if (script_hasdata(st, 2))
  10504. nd = npc_name2id(script_getstr(st,2));
  10505. else
  10506. nd = map_id2nd(st->oid);
  10507. if (!strcmp(command,"enablenpc"))
  10508. flag = NPCVIEW_ENABLE;
  10509. else if (!strcmp(command,"disablenpc"))
  10510. flag = NPCVIEW_DISABLE;
  10511. else if (!strcmp(command,"hideoffnpc"))
  10512. flag = NPCVIEW_HIDEOFF;
  10513. else if (!strcmp(command,"hideonnpc"))
  10514. flag = NPCVIEW_HIDEON;
  10515. else if (!strcmp(command,"cloakoffnpc") || !strcmp(command,"cloakoffnpcself"))
  10516. flag = NPCVIEW_CLOAKOFF;
  10517. else if (!strcmp(command,"cloakonnpc") || !strcmp(command,"cloakonnpcself"))
  10518. flag = NPCVIEW_CLOAKON;
  10519. else{
  10520. ShowError( "buildin_enablenpc: Undefined command \"%s\".\n", command );
  10521. return SCRIPT_CMD_FAILURE;
  10522. }
  10523. if (!nd) {
  10524. if (script_hasdata(st, 2))
  10525. 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);
  10526. else
  10527. ShowError("buildin_%s: Attempted to %s a non-existing NPC (flag=%d).\n", (flag & NPCVIEW_VISIBLE) ? "show" : "hide", command, flag);
  10528. return SCRIPT_CMD_FAILURE;
  10529. }
  10530. int char_id = 0;
  10531. if (script_hasdata(st, 3))
  10532. char_id = script_getnum(st, 3);
  10533. else if (!strcmp(command,"cloakoffnpcself") || !strcmp(command,"cloakonnpcself")) {
  10534. map_session_data* sd;
  10535. if (!script_rid2sd(sd))
  10536. return SCRIPT_CMD_SUCCESS;
  10537. char_id = sd->status.char_id;
  10538. }
  10539. if (npc_enable_target(*nd, char_id, flag))
  10540. return SCRIPT_CMD_SUCCESS;
  10541. return SCRIPT_CMD_FAILURE;
  10542. }
  10543. /* Starts a status effect on the target unit or on the attached player.
  10544. *
  10545. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10546. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10547. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10548. * <flag>: enum e_status_change_start_flags
  10549. */
  10550. BUILDIN_FUNC(sc_start)
  10551. {
  10552. TBL_NPC * nd = map_id2nd(st->oid);
  10553. struct block_list* bl;
  10554. enum sc_type type;
  10555. int tick, val1, val2, val3, val4=0, rate, flag;
  10556. char start_type;
  10557. const char* command = script_getfuncname(st);
  10558. if(strstr(command, "4"))
  10559. start_type = 4;
  10560. else if(strstr(command, "2"))
  10561. start_type = 2;
  10562. else
  10563. start_type = 1;
  10564. type = (sc_type)script_getnum(st,2);
  10565. tick = script_getnum(st,3);
  10566. val1 = script_getnum(st,4);
  10567. //If from NPC we make default flag 1 to be unavoidable
  10568. if(nd && nd->bl.id == fake_nd->bl.id)
  10569. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10570. else
  10571. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10572. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10573. if(script_hasdata(st,(6+start_type)))
  10574. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10575. else
  10576. bl = map_id2bl(st->rid);
  10577. uint16 skill_id;
  10578. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && (skill_id = status_db.getSkill(type)) > 0)
  10579. {// When there isn't a duration specified, try to get it from the skill_db
  10580. tick = skill_get_time(skill_id, val1);
  10581. }
  10582. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10583. bl = map_id2bl(potion_target);
  10584. tick /= 2;// Thrown potions only last half.
  10585. val4 = 1;// Mark that this was a thrown sc_effect
  10586. }
  10587. if(!bl)
  10588. return SCRIPT_CMD_SUCCESS;
  10589. switch(start_type) {
  10590. case 1:
  10591. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10592. break;
  10593. case 2:
  10594. val2 = script_getnum(st,5);
  10595. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10596. break;
  10597. case 4:
  10598. val2 = script_getnum(st,5);
  10599. val3 = script_getnum(st,6);
  10600. val4 = script_getnum(st,7);
  10601. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10602. break;
  10603. }
  10604. return SCRIPT_CMD_SUCCESS;
  10605. }
  10606. /// Ends one or all status effects on the target unit or on the attached player.
  10607. ///
  10608. /// sc_end <effect_id>{,<unit_id>};
  10609. BUILDIN_FUNC(sc_end)
  10610. {
  10611. struct block_list* bl;
  10612. int type;
  10613. type = script_getnum(st, 2);
  10614. if (script_hasdata(st, 3))
  10615. bl = map_id2bl(script_getnum(st, 3));
  10616. else
  10617. bl = map_id2bl(st->rid);
  10618. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10619. bl = map_id2bl(potion_target);
  10620. if (!bl)
  10621. return SCRIPT_CMD_SUCCESS;
  10622. if (type > SC_NONE && type < SC_MAX) {
  10623. status_change *sc = status_get_sc(bl);
  10624. if (sc == nullptr)
  10625. return SCRIPT_CMD_SUCCESS;
  10626. struct status_change_entry *sce = sc->getSCE(type);
  10627. if (sce == nullptr)
  10628. return SCRIPT_CMD_SUCCESS;
  10629. std::shared_ptr<s_status_change_db> sc_db = status_db.find( type );
  10630. if( sc_db == nullptr ){
  10631. ShowError( "buildin_sc_end: Unknown status change %d.\n", type );
  10632. return SCRIPT_CMD_FAILURE;
  10633. }
  10634. if( sc_db->flag[SCF_NOCLEARBUFF] && sc_db->flag[SCF_NOFORCEDEND] ){
  10635. ShowError( "buildin_sc_end: Status %d cannot be cleared.\n", type );
  10636. return SCRIPT_CMD_FAILURE;
  10637. }
  10638. //This should help status_change_end force disabling the SC in case it has no limit.
  10639. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10640. status_change_end(bl, (sc_type)type);
  10641. } else
  10642. status_change_clear(bl, 3); // remove all effects
  10643. return SCRIPT_CMD_SUCCESS;
  10644. }
  10645. /**
  10646. * Ends all status effects from any learned skill on the attached player.
  10647. * if <job_id> was given it will end the effect of that class for the attached player
  10648. * sc_end_class {<char_id>{,<job_id>}};
  10649. */
  10650. BUILDIN_FUNC(sc_end_class)
  10651. {
  10652. map_session_data *sd;
  10653. int class_;
  10654. if (!script_charid2sd(2, sd))
  10655. return SCRIPT_CMD_FAILURE;
  10656. if (script_hasdata(st, 3))
  10657. class_ = script_getnum(st, 3);
  10658. else
  10659. class_ = sd->status.class_;
  10660. if (!pcdb_checkid(class_)) {
  10661. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10662. return SCRIPT_CMD_FAILURE;
  10663. }
  10664. status_db.changeSkillTree(sd, class_);
  10665. return SCRIPT_CMD_SUCCESS;
  10666. }
  10667. /*==========================================
  10668. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10669. *------------------------------------------*/
  10670. BUILDIN_FUNC(getscrate)
  10671. {
  10672. struct block_list *bl;
  10673. int type;
  10674. t_tick rate;
  10675. type=script_getnum(st,2);
  10676. rate=script_getnum(st,3);
  10677. if( script_hasdata(st,4) ) //get for the bl assigned
  10678. bl = map_id2bl(script_getnum(st,4));
  10679. else
  10680. bl = map_id2bl(st->rid);
  10681. if (bl)
  10682. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10683. script_pushint(st,rate);
  10684. return SCRIPT_CMD_SUCCESS;
  10685. }
  10686. /**
  10687. * getstatus(<effect type>{,<type>{,<char_id>}});
  10688. **/
  10689. BUILDIN_FUNC(getstatus)
  10690. {
  10691. int id, type;
  10692. map_session_data* sd;
  10693. if (!script_charid2sd(4,sd))
  10694. return SCRIPT_CMD_FAILURE;
  10695. id = script_getnum(st, 2);
  10696. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10697. if( id <= SC_NONE || id >= SC_MAX )
  10698. {// invalid status type given
  10699. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10700. return SCRIPT_CMD_SUCCESS;
  10701. }
  10702. if( sd->sc.count == 0 || !sd->sc.getSCE(id) )
  10703. {// no status is active
  10704. script_pushint(st, 0);
  10705. return SCRIPT_CMD_SUCCESS;
  10706. }
  10707. switch( type )
  10708. {
  10709. case 1: script_pushint(st, sd->sc.getSCE(id)->val1); break;
  10710. case 2: script_pushint(st, sd->sc.getSCE(id)->val2); break;
  10711. case 3: script_pushint(st, sd->sc.getSCE(id)->val3); break;
  10712. case 4: script_pushint(st, sd->sc.getSCE(id)->val4); break;
  10713. case 5:
  10714. {
  10715. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.getSCE(id)->timer);
  10716. if( timer )
  10717. {// return the amount of time remaining
  10718. script_pushint(st, timer->tick - gettick());
  10719. } else {
  10720. script_pushint(st, -1);
  10721. }
  10722. }
  10723. break;
  10724. default: script_pushint(st, 1); break;
  10725. }
  10726. return SCRIPT_CMD_SUCCESS;
  10727. }
  10728. /*==========================================
  10729. *
  10730. *------------------------------------------*/
  10731. BUILDIN_FUNC(debugmes)
  10732. {
  10733. const char *str;
  10734. str=script_getstr(st,2);
  10735. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10736. return SCRIPT_CMD_SUCCESS;
  10737. }
  10738. BUILDIN_FUNC( errormes ){
  10739. ShowError( "%s\n", script_getstr( st, 2 ) );
  10740. script_reportsrc( st );
  10741. return SCRIPT_CMD_SUCCESS;
  10742. }
  10743. /*==========================================
  10744. *------------------------------------------*/
  10745. BUILDIN_FUNC(catchpet)
  10746. {
  10747. int pet_id;
  10748. TBL_PC *sd;
  10749. if( !script_rid2sd(sd) )
  10750. return SCRIPT_CMD_SUCCESS;
  10751. pet_id= script_getnum(st,2);
  10752. pet_catch_process1(sd,pet_id);
  10753. return SCRIPT_CMD_SUCCESS;
  10754. }
  10755. /*==========================================
  10756. * [orn]
  10757. *------------------------------------------*/
  10758. BUILDIN_FUNC(homunculus_evolution)
  10759. {
  10760. TBL_PC *sd;
  10761. if( !script_rid2sd(sd) )
  10762. return SCRIPT_CMD_SUCCESS;
  10763. if(hom_is_active(sd->hd))
  10764. {
  10765. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10766. hom_evolution(sd->hd);
  10767. else
  10768. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10769. }
  10770. return SCRIPT_CMD_SUCCESS;
  10771. }
  10772. /*==========================================
  10773. * Checks for vaporized morph state
  10774. * and deletes ITEMID_STRANGE_EMBRYO.
  10775. *------------------------------------------*/
  10776. BUILDIN_FUNC(homunculus_mutate)
  10777. {
  10778. uint16 homun_id;
  10779. TBL_PC *sd;
  10780. if( !script_rid2sd(sd) || sd->hd == NULL )
  10781. return SCRIPT_CMD_SUCCESS;
  10782. if(script_hasdata(st,2))
  10783. homun_id = script_getnum(st,2);
  10784. else
  10785. homun_id = rnd_value<uint16>(MER_EIRA, MER_ELEANOR);
  10786. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10787. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10788. int m_id = hom_class2mapid(homun_id);
  10789. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10790. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10791. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10792. hom_call(sd); // Respawn homunculus.
  10793. hom_mutate(sd->hd, homun_id);
  10794. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10795. script_pushint(st, 1);
  10796. return SCRIPT_CMD_SUCCESS;
  10797. } else
  10798. clif_emotion(&sd->bl, ET_SWEAT);
  10799. } else
  10800. clif_emotion(&sd->bl, ET_SWEAT);
  10801. script_pushint(st, 0);
  10802. return SCRIPT_CMD_SUCCESS;
  10803. }
  10804. /*==========================================
  10805. * Puts homunculus into morph state
  10806. * and gives ITEMID_STRANGE_EMBRYO.
  10807. *------------------------------------------*/
  10808. BUILDIN_FUNC(morphembryo)
  10809. {
  10810. struct item item_tmp;
  10811. TBL_PC *sd;
  10812. if( !script_rid2sd(sd) || sd->hd == NULL )
  10813. return SCRIPT_CMD_SUCCESS;
  10814. if( hom_is_active(sd->hd) ) {
  10815. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10816. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10817. char i;
  10818. memset(&item_tmp, 0, sizeof(item_tmp));
  10819. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10820. item_tmp.identify = 1;
  10821. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10822. clif_additem(sd, 0, 0, i);
  10823. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10824. } else {
  10825. hom_vaporize(sd, HOM_ST_MORPH);
  10826. script_pushint(st, 1);
  10827. return SCRIPT_CMD_SUCCESS;
  10828. }
  10829. } else
  10830. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10831. } else
  10832. clif_emotion(&sd->bl, ET_SWEAT);
  10833. script_pushint(st, 0);
  10834. return SCRIPT_CMD_SUCCESS;
  10835. }
  10836. // [Zephyrus]
  10837. BUILDIN_FUNC(homunculus_shuffle)
  10838. {
  10839. TBL_PC *sd;
  10840. if( !script_rid2sd(sd) )
  10841. return SCRIPT_CMD_SUCCESS;
  10842. if(hom_is_active(sd->hd))
  10843. hom_shuffle(sd->hd);
  10844. return SCRIPT_CMD_SUCCESS;
  10845. }
  10846. /*==========================================
  10847. * Check for homunculus state.
  10848. * Return: -1 = No homunculus
  10849. * 0 = Homunculus is active
  10850. * 1 = Homunculus is vaporized (rest)
  10851. * 2 = Homunculus is in morph state
  10852. *------------------------------------------*/
  10853. BUILDIN_FUNC(checkhomcall)
  10854. {
  10855. TBL_PC *sd;
  10856. TBL_HOM *hd;
  10857. if( !script_rid2sd(sd) )
  10858. return SCRIPT_CMD_SUCCESS;
  10859. hd = sd->hd;
  10860. if( !hd )
  10861. script_pushint(st, -1);
  10862. else
  10863. script_pushint(st, hd->homunculus.vaporize);
  10864. return SCRIPT_CMD_SUCCESS;
  10865. }
  10866. //These two functions bring the eA MAPID_* class functionality to scripts.
  10867. BUILDIN_FUNC(eaclass)
  10868. {
  10869. int class_;
  10870. if( script_hasdata(st,2) )
  10871. class_ = script_getnum(st,2);
  10872. else {
  10873. TBL_PC *sd;
  10874. if (!script_charid2sd(3,sd)) {
  10875. script_pushint(st,-1);
  10876. return SCRIPT_CMD_SUCCESS;
  10877. }
  10878. class_ = sd->status.class_;
  10879. }
  10880. script_pushint(st,pc_jobid2mapid(class_));
  10881. return SCRIPT_CMD_SUCCESS;
  10882. }
  10883. BUILDIN_FUNC(roclass)
  10884. {
  10885. int class_ =script_getnum(st,2);
  10886. int sex;
  10887. if( script_hasdata(st,3) )
  10888. sex = script_getnum(st,3);
  10889. else {
  10890. TBL_PC *sd;
  10891. if (st->rid && script_rid2sd(sd))
  10892. sex = sd->status.sex;
  10893. else
  10894. sex = SEX_MALE; //Just use male when not found.
  10895. }
  10896. script_pushint(st,pc_mapid2jobid(class_, sex));
  10897. return SCRIPT_CMD_SUCCESS;
  10898. }
  10899. /*==========================================
  10900. * Tells client to open a hatching window, used for pet incubator
  10901. *------------------------------------------*/
  10902. BUILDIN_FUNC(birthpet)
  10903. {
  10904. TBL_PC *sd;
  10905. if( !script_rid2sd(sd) )
  10906. return SCRIPT_CMD_SUCCESS;
  10907. if( sd->status.pet_id )
  10908. {// do not send egg list, when you already have a pet
  10909. return SCRIPT_CMD_SUCCESS;
  10910. }
  10911. clif_sendegg(sd);
  10912. return SCRIPT_CMD_SUCCESS;
  10913. }
  10914. /**
  10915. * resetlvl <action type>{,<char_id>};
  10916. * @param action_type:
  10917. * 1 : make like after rebirth
  10918. * 2 : blvl,jlvl=1, skillpoint=0
  10919. * 3 : don't reset skill, blvl=1
  10920. * 4 : jlvl=0
  10921. * @author AppleGirl
  10922. **/
  10923. BUILDIN_FUNC(resetlvl)
  10924. {
  10925. TBL_PC *sd;
  10926. int type=script_getnum(st,2);
  10927. if (!script_charid2sd(3,sd))
  10928. return SCRIPT_CMD_FAILURE;
  10929. pc_resetlvl(sd,type);
  10930. return SCRIPT_CMD_SUCCESS;
  10931. }
  10932. /**
  10933. * Reset a player status point
  10934. * resetstatus({<char_id>});
  10935. **/
  10936. BUILDIN_FUNC(resetstatus)
  10937. {
  10938. TBL_PC *sd;
  10939. if (!script_charid2sd(2,sd))
  10940. return SCRIPT_CMD_FAILURE;
  10941. pc_resetstate(sd);
  10942. return SCRIPT_CMD_SUCCESS;
  10943. }
  10944. /**
  10945. * Reset player's skill
  10946. * resetskill({<char_id>});
  10947. **/
  10948. BUILDIN_FUNC(resetskill)
  10949. {
  10950. TBL_PC *sd;
  10951. if (!script_charid2sd(2,sd))
  10952. return SCRIPT_CMD_FAILURE;
  10953. pc_resetskill(sd,1);
  10954. return SCRIPT_CMD_SUCCESS;
  10955. }
  10956. /**
  10957. * Reset SG designated maps
  10958. * resetfeel({<char_id>});
  10959. **/
  10960. BUILDIN_FUNC(resetfeel)
  10961. {
  10962. TBL_PC *sd;
  10963. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10964. return SCRIPT_CMD_FAILURE;
  10965. pc_resetfeel(sd);
  10966. return SCRIPT_CMD_SUCCESS;
  10967. }
  10968. /**
  10969. * Reset SG designated monsters
  10970. * resethate({<char_id>});
  10971. **/
  10972. BUILDIN_FUNC(resethate)
  10973. {
  10974. TBL_PC *sd;
  10975. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10976. return SCRIPT_CMD_FAILURE;
  10977. pc_resethate(sd);
  10978. return SCRIPT_CMD_SUCCESS;
  10979. }
  10980. /**
  10981. * Counts total amount of skill points.
  10982. * skillpointcount({<char_id>})
  10983. **/
  10984. BUILDIN_FUNC(skillpointcount)
  10985. {
  10986. TBL_PC *sd;
  10987. if (!script_charid2sd(2,sd))
  10988. return SCRIPT_CMD_FAILURE;
  10989. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10990. return SCRIPT_CMD_SUCCESS;
  10991. }
  10992. /*==========================================
  10993. *
  10994. *------------------------------------------*/
  10995. BUILDIN_FUNC(changebase)
  10996. {
  10997. TBL_PC *sd=NULL;
  10998. int vclass;
  10999. if( !script_mapid2sd(3,sd) )
  11000. return SCRIPT_CMD_SUCCESS;
  11001. vclass = script_getnum(st,2);
  11002. if(vclass == JOB_WEDDING)
  11003. {
  11004. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  11005. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  11006. )
  11007. return SCRIPT_CMD_SUCCESS;
  11008. }
  11009. if(!sd->disguise && vclass != sd->vd.class_) {
  11010. status_set_viewdata(&sd->bl, vclass);
  11011. //Updated client view. Base, Weapon and Cloth Colors.
  11012. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  11013. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  11014. if (sd->vd.cloth_color)
  11015. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  11016. if (sd->vd.body_style)
  11017. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  11018. clif_skillinfoblock(sd);
  11019. }
  11020. return SCRIPT_CMD_SUCCESS;
  11021. }
  11022. /**
  11023. * Change account sex and unequip all item and request for a changesex to char-serv
  11024. * changesex({<char_id>});
  11025. */
  11026. BUILDIN_FUNC(changesex)
  11027. {
  11028. int i;
  11029. TBL_PC *sd = NULL;
  11030. if (!script_charid2sd(2,sd))
  11031. return SCRIPT_CMD_FAILURE;
  11032. pc_resetskill(sd,4);
  11033. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  11034. for(i = 0; i < EQI_MAX; i++) {
  11035. if (sd->equip_index[i] >= 0)
  11036. pc_unequipitem(sd, sd->equip_index[i], 3);
  11037. }
  11038. chrif_changesex(sd, true);
  11039. return SCRIPT_CMD_SUCCESS;
  11040. }
  11041. /**
  11042. * Change character's sex and unequip all item and request for a changesex to char-serv
  11043. * changecharsex({<char_id>});
  11044. */
  11045. BUILDIN_FUNC(changecharsex)
  11046. {
  11047. #if PACKETVER >= 20141016
  11048. int i;
  11049. TBL_PC *sd = NULL;
  11050. if (!script_charid2sd(2,sd))
  11051. return SCRIPT_CMD_FAILURE;
  11052. pc_resetskill(sd,4);
  11053. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  11054. for (i = 0; i < EQI_MAX; i++) {
  11055. if (sd->equip_index[i] >= 0)
  11056. pc_unequipitem(sd, sd->equip_index[i], 3);
  11057. }
  11058. chrif_changesex(sd, false);
  11059. return SCRIPT_CMD_SUCCESS;
  11060. #else
  11061. return SCRIPT_CMD_FAILURE;
  11062. #endif
  11063. }
  11064. /*==========================================
  11065. * Works like 'announce' but outputs in the common chat window
  11066. *------------------------------------------*/
  11067. BUILDIN_FUNC(globalmes)
  11068. {
  11069. struct block_list *bl = map_id2bl(st->oid);
  11070. struct npc_data *nd = (struct npc_data *)bl;
  11071. const char *name=NULL,*mes;
  11072. mes=script_getstr(st,2);
  11073. if(mes==NULL)
  11074. return SCRIPT_CMD_SUCCESS;
  11075. if(script_hasdata(st,3)){ // npc name to display
  11076. name=script_getstr(st,3);
  11077. } else {
  11078. name=nd->name; //use current npc name
  11079. }
  11080. npc_globalmessage(name,mes); // broadcast to all players connected
  11081. return SCRIPT_CMD_SUCCESS;
  11082. }
  11083. /////////////////////////////////////////////////////////////////////
  11084. // NPC waiting room (chat room)
  11085. //
  11086. /// Creates a waiting room (chat room) for this npc.
  11087. ///
  11088. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  11089. BUILDIN_FUNC(waitingroom)
  11090. {
  11091. struct npc_data* nd;
  11092. const char* title = script_getstr(st, 2);
  11093. int limit = script_getnum(st, 3);
  11094. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  11095. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  11096. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  11097. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  11098. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  11099. nd = (struct npc_data *)map_id2bl(st->oid);
  11100. if( nd != NULL )
  11101. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  11102. return SCRIPT_CMD_SUCCESS;
  11103. }
  11104. /// Removes the waiting room of the current or target npc.
  11105. ///
  11106. /// delwaitingroom "<npc_name>";
  11107. /// delwaitingroom;
  11108. BUILDIN_FUNC(delwaitingroom)
  11109. {
  11110. struct npc_data* nd;
  11111. if( script_hasdata(st,2) )
  11112. nd = npc_name2id(script_getstr(st, 2));
  11113. else
  11114. nd = (struct npc_data *)map_id2bl(st->oid);
  11115. if( nd != NULL )
  11116. chat_deletenpcchat(nd);
  11117. return SCRIPT_CMD_SUCCESS;
  11118. }
  11119. /// Kick the specified player from the waiting room of the target npc.
  11120. ///
  11121. /// waitingroomkick "<npc_name>", <kickusername>;
  11122. BUILDIN_FUNC(waitingroomkick)
  11123. {
  11124. struct npc_data* nd;
  11125. struct chat_data* cd;
  11126. const char* kickusername;
  11127. nd = npc_name2id(script_getstr(st,2));
  11128. kickusername = script_getstr(st,3);
  11129. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11130. chat_npckickchat(cd, kickusername);
  11131. return SCRIPT_CMD_SUCCESS;
  11132. }
  11133. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  11134. /// Num users stored in .@waitingroom_usercount
  11135. ///
  11136. /// getwaitingroomusers "<npc_name>";
  11137. BUILDIN_FUNC(getwaitingroomusers)
  11138. {
  11139. struct npc_data* nd;
  11140. struct chat_data* cd;
  11141. int i, j=0;
  11142. if( script_hasdata(st,2) )
  11143. nd = npc_name2id(script_getstr(st, 2));
  11144. else
  11145. nd = (struct npc_data *)map_id2bl(st->oid);
  11146. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  11147. for(i = 0; i < cd->users; ++i) {
  11148. setd_sub_num( st, NULL, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, NULL );
  11149. j++;
  11150. }
  11151. setd_sub_num( st, NULL, ".@waitingroom_usercount", 0, j, NULL );
  11152. }
  11153. return SCRIPT_CMD_SUCCESS;
  11154. }
  11155. /// Kicks all the players from the waiting room of the current or target npc.
  11156. ///
  11157. /// kickwaitingroomall "<npc_name>";
  11158. /// kickwaitingroomall;
  11159. BUILDIN_FUNC(waitingroomkickall)
  11160. {
  11161. struct npc_data* nd;
  11162. struct chat_data* cd;
  11163. if( script_hasdata(st,2) )
  11164. nd = npc_name2id(script_getstr(st,2));
  11165. else
  11166. nd = (struct npc_data *)map_id2bl(st->oid);
  11167. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11168. chat_npckickall(cd);
  11169. return SCRIPT_CMD_SUCCESS;
  11170. }
  11171. /// Enables the waiting room event of the current or target npc.
  11172. ///
  11173. /// enablewaitingroomevent "<npc_name>";
  11174. /// enablewaitingroomevent;
  11175. BUILDIN_FUNC(enablewaitingroomevent)
  11176. {
  11177. struct npc_data* nd;
  11178. struct chat_data* cd;
  11179. if( script_hasdata(st,2) )
  11180. nd = npc_name2id(script_getstr(st, 2));
  11181. else
  11182. nd = (struct npc_data *)map_id2bl(st->oid);
  11183. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11184. chat_enableevent(cd);
  11185. return SCRIPT_CMD_SUCCESS;
  11186. }
  11187. /// Disables the waiting room event of the current or target npc.
  11188. ///
  11189. /// disablewaitingroomevent "<npc_name>";
  11190. /// disablewaitingroomevent;
  11191. BUILDIN_FUNC(disablewaitingroomevent)
  11192. {
  11193. struct npc_data *nd;
  11194. struct chat_data *cd;
  11195. if( script_hasdata(st,2) )
  11196. nd = npc_name2id(script_getstr(st, 2));
  11197. else
  11198. nd = (struct npc_data *)map_id2bl(st->oid);
  11199. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11200. chat_disableevent(cd);
  11201. return SCRIPT_CMD_SUCCESS;
  11202. }
  11203. /// Returns info on the waiting room of the current or target npc.
  11204. /// Returns -1 if the type unknown
  11205. /// <type>=0 : current number of users
  11206. /// <type>=1 : maximum number of users allowed
  11207. /// <type>=2 : the number of users that trigger the event
  11208. /// <type>=3 : if the trigger is disabled
  11209. /// <type>=4 : the title of the waiting room
  11210. /// <type>=5 : the password of the waiting room
  11211. /// <type>=16 : the name of the waiting room event
  11212. /// <type>=32 : if the waiting room is full
  11213. /// <type>=33 : if there are enough users to trigger the event
  11214. ///
  11215. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  11216. /// getwaitingroomstate(<type>) -> <info>
  11217. BUILDIN_FUNC(getwaitingroomstate)
  11218. {
  11219. struct npc_data *nd;
  11220. struct chat_data *cd;
  11221. int type;
  11222. type = script_getnum(st,2);
  11223. if( script_hasdata(st,3) )
  11224. nd = npc_name2id(script_getstr(st, 3));
  11225. else
  11226. nd = (struct npc_data *)map_id2bl(st->oid);
  11227. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11228. {
  11229. script_pushint(st, -1);
  11230. return SCRIPT_CMD_SUCCESS;
  11231. }
  11232. switch(type)
  11233. {
  11234. case 0: script_pushint(st, cd->users); break;
  11235. case 1: script_pushint(st, cd->limit); break;
  11236. case 2: script_pushint(st, cd->trigger&0x7f); break;
  11237. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  11238. case 4: script_pushstrcopy(st, cd->title); break;
  11239. case 5: script_pushstrcopy(st, cd->pass); break;
  11240. case 16: script_pushstrcopy(st, cd->npc_event);break;
  11241. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  11242. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  11243. default: script_pushint(st, -1); break;
  11244. }
  11245. return SCRIPT_CMD_SUCCESS;
  11246. }
  11247. /// Warps the trigger or target amount of players to the target map and position.
  11248. /// Players are automatically removed from the waiting room.
  11249. /// Those waiting the longest will get warped first.
  11250. /// The target map can be "Random" for a random position in the current map,
  11251. /// and "SavePoint" for the savepoint map+position.
  11252. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  11253. ///
  11254. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  11255. /// The total number of teleported players is put into $@warpwaitingpcnum
  11256. ///
  11257. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  11258. /// warpwaitingpc "<map name>",<x>,<y>;
  11259. BUILDIN_FUNC(warpwaitingpc)
  11260. {
  11261. int x;
  11262. int y;
  11263. int i;
  11264. int n;
  11265. const char* map_name;
  11266. struct npc_data* nd;
  11267. struct chat_data* cd;
  11268. nd = (struct npc_data *)map_id2bl(st->oid);
  11269. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11270. return SCRIPT_CMD_SUCCESS;
  11271. map_name = script_getstr(st,2);
  11272. x = script_getnum(st,3);
  11273. y = script_getnum(st,4);
  11274. n = cd->trigger&0x7f;
  11275. if( script_hasdata(st,5) )
  11276. n = script_getnum(st,5);
  11277. for( i = 0; i < n && cd->users > 0; i++ )
  11278. {
  11279. TBL_PC* sd = cd->usersd[0];
  11280. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  11281. {// can't teleport on this map
  11282. break;
  11283. }
  11284. if( cd->zeny )
  11285. {// fee set
  11286. if( (uint32)sd->status.zeny < cd->zeny )
  11287. {// no zeny to cover set fee
  11288. break;
  11289. }
  11290. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC);
  11291. }
  11292. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  11293. if( strcmp(map_name,"Random") == 0 )
  11294. pc_randomwarp(sd,CLR_TELEPORT,true);
  11295. else if( strcmp(map_name,"SavePoint") == 0 )
  11296. pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  11297. else
  11298. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  11299. }
  11300. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  11301. return SCRIPT_CMD_SUCCESS;
  11302. }
  11303. /////////////////////////////////////////////////////////////////////
  11304. // ...
  11305. //
  11306. /// Detaches a character from a script.
  11307. ///
  11308. /// @param st Script state to detach the character from.
  11309. void script_detach_rid(struct script_state* st)
  11310. {
  11311. if(st->rid)
  11312. {
  11313. script_detach_state(st, false);
  11314. st->rid = 0;
  11315. }
  11316. }
  11317. /*=========================================================================
  11318. * Attaches a set of RIDs to the current script. [digitalhamster]
  11319. * addrid(<type>{,<flag>{,<parameters>}});
  11320. * <type>:
  11321. * 0 : All players in the server.
  11322. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  11323. * 2 : Party members of a specified party ID.
  11324. * [ Parameters: <party id> ]
  11325. * 3 : Guild members of a specified guild ID.
  11326. * [ Parameters: <guild id> ]
  11327. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  11328. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  11329. * 5 : All players in the map.
  11330. * [ Parameters: "<map name>" ]
  11331. * 6 : Battleground members of a specified battleground ID.
  11332. * [ Parameters: <battleground id> ]
  11333. * 7 : Clan members of a specified clan ID.
  11334. * [ Parameters: <clan id> ]
  11335. * Account ID: The specified account ID.
  11336. * <flag>:
  11337. * 0 : Players are always attached. (default)
  11338. * 1 : Players currently running another script will not be attached.
  11339. *-------------------------------------------------------------------------*/
  11340. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  11341. {
  11342. int forceflag;
  11343. map_session_data *sd = (TBL_PC *)bl;
  11344. struct script_state* st;
  11345. st = va_arg(ap,struct script_state*);
  11346. forceflag = va_arg(ap,int);
  11347. if(!forceflag || !sd->st)
  11348. if(sd->status.account_id != st->rid)
  11349. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11350. return 0;
  11351. }
  11352. BUILDIN_FUNC(addrid)
  11353. {
  11354. struct s_mapiterator* iter;
  11355. struct block_list *bl;
  11356. TBL_PC *sd;
  11357. if(st->rid < 1) {
  11358. st->state = END;
  11359. bl = map_id2bl(st->oid);
  11360. } else
  11361. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  11362. iter = mapit_getallusers();
  11363. int type = script_getnum(st,2);
  11364. bool forceflag = (script_hasdata(st,3) ? script_getnum(st,3) != 0 : 0);
  11365. switch(type) {
  11366. case 0:
  11367. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11368. if(!forceflag || !sd->st)
  11369. if(sd->status.account_id != st->rid) //attached player already runs.
  11370. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11371. }
  11372. break;
  11373. case 1:
  11374. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11375. if(!forceflag || !sd->st)
  11376. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  11377. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11378. }
  11379. break;
  11380. case 2:
  11381. if(script_getnum(st,4) == 0) {
  11382. script_pushint(st,0);
  11383. mapit_free(iter);
  11384. return SCRIPT_CMD_SUCCESS;
  11385. }
  11386. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11387. if(!forceflag || !sd->st)
  11388. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  11389. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11390. }
  11391. break;
  11392. case 3:
  11393. if(script_getnum(st,4) == 0) {
  11394. script_pushint(st,0);
  11395. mapit_free(iter);
  11396. return SCRIPT_CMD_SUCCESS;
  11397. }
  11398. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11399. if(!forceflag || !sd->st)
  11400. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  11401. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11402. }
  11403. break;
  11404. case 4:
  11405. map_foreachinallarea(buildin_addrid_sub,
  11406. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  11407. st,forceflag);//4-x0 , 5-y0 , 6-x1, 7-y1
  11408. break;
  11409. case 5:
  11410. if (script_getstr(st, 4) == NULL) {
  11411. script_pushint(st, 0);
  11412. mapit_free(iter);
  11413. return SCRIPT_CMD_FAILURE;
  11414. }
  11415. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  11416. script_pushint(st, 0);
  11417. mapit_free(iter);
  11418. return SCRIPT_CMD_FAILURE;
  11419. }
  11420. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  11421. break;
  11422. case 6:
  11423. if (script_getnum(st, 4) == 0) {
  11424. script_pushint(st, 0);
  11425. mapit_free(iter);
  11426. return SCRIPT_CMD_FAILURE;
  11427. }
  11428. for (sd = (TBL_PC *)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC *)mapit_next(iter)) {
  11429. if (!forceflag || !sd->st)
  11430. if ((sd->status.account_id != st->rid) && (sd->bg_id == script_getnum(st, 4))) //attached player already runs.
  11431. run_script(st->script, st->pos, sd->status.account_id, st->oid);
  11432. }
  11433. break;
  11434. case 7:
  11435. if (script_getnum(st, 4) == 0) {
  11436. script_pushint(st, 0);
  11437. mapit_free(iter);
  11438. return SCRIPT_CMD_FAILURE;
  11439. }
  11440. for (sd = (TBL_PC *)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC *)mapit_next(iter)) {
  11441. if (!forceflag || !sd->st)
  11442. if ((sd->status.account_id != st->rid) && (sd->status.clan_id == script_getnum(st, 4))) //attached player already runs.
  11443. run_script(st->script, st->pos, sd->status.account_id, st->oid);
  11444. }
  11445. break;
  11446. default:
  11447. mapit_free(iter);
  11448. if((map_id2sd(type)) == NULL) { // Player not found.
  11449. script_pushint(st,0);
  11450. return SCRIPT_CMD_SUCCESS;
  11451. }
  11452. if(!forceflag || !map_id2sd(type)->st) {
  11453. run_script(st->script,st->pos,type,st->oid);
  11454. script_pushint(st,1);
  11455. }
  11456. return SCRIPT_CMD_SUCCESS;
  11457. }
  11458. mapit_free(iter);
  11459. script_pushint(st,1);
  11460. return SCRIPT_CMD_SUCCESS;
  11461. }
  11462. /*==========================================
  11463. * Attach sd char id to script and detach current one if any
  11464. *------------------------------------------*/
  11465. BUILDIN_FUNC(attachrid)
  11466. {
  11467. int rid = script_getnum(st,2);
  11468. bool force;
  11469. if( script_hasdata(st,3) ){
  11470. force = script_getnum(st,3) != 0;
  11471. }else{
  11472. force = true;
  11473. }
  11474. map_session_data* sd = map_id2sd(rid);
  11475. if( sd != NULL && ( !sd->npc_id || force ) ){
  11476. script_detach_rid(st);
  11477. st->rid = rid;
  11478. script_attach_state(st);
  11479. script_pushint(st,true);
  11480. }else{
  11481. script_pushint(st,false);
  11482. }
  11483. return SCRIPT_CMD_SUCCESS;
  11484. }
  11485. /*==========================================
  11486. * Detach script to rid
  11487. *------------------------------------------*/
  11488. BUILDIN_FUNC(detachrid)
  11489. {
  11490. script_detach_rid(st);
  11491. return SCRIPT_CMD_SUCCESS;
  11492. }
  11493. /*==========================================
  11494. * Chk if account connected, (and charid from account if specified)
  11495. *------------------------------------------*/
  11496. BUILDIN_FUNC(isloggedin)
  11497. {
  11498. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11499. if (script_hasdata(st,3) && sd &&
  11500. sd->status.char_id != script_getnum(st,3))
  11501. sd = NULL;
  11502. push_val(st->stack,C_INT,sd!=NULL);
  11503. return SCRIPT_CMD_SUCCESS;
  11504. }
  11505. /*==========================================
  11506. *
  11507. *------------------------------------------*/
  11508. BUILDIN_FUNC(setmapflagnosave)
  11509. {
  11510. int16 m,x,y;
  11511. unsigned short mapindex;
  11512. const char *str,*str2;
  11513. union u_mapflag_args args = {};
  11514. str=script_getstr(st,2);
  11515. str2=script_getstr(st,3);
  11516. x=script_getnum(st,4);
  11517. y=script_getnum(st,5);
  11518. m = map_mapname2mapid(str);
  11519. mapindex = mapindex_name2id(str2);
  11520. if(m < 0) {
  11521. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11522. return SCRIPT_CMD_FAILURE;
  11523. }
  11524. args.nosave.map = mapindex;
  11525. args.nosave.x = x;
  11526. args.nosave.y = y;
  11527. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11528. return SCRIPT_CMD_SUCCESS;
  11529. }
  11530. BUILDIN_FUNC(getmapflag)
  11531. {
  11532. int16 m;
  11533. int mf;
  11534. const char *str;
  11535. str=script_getstr(st,2);
  11536. m = map_mapname2mapid(str);
  11537. if (m < 0) {
  11538. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11539. return SCRIPT_CMD_FAILURE;
  11540. }
  11541. mf = script_getnum(st, 3);
  11542. if( mf < MF_MIN || mf > MF_MAX ){
  11543. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11544. return SCRIPT_CMD_FAILURE;
  11545. }
  11546. union u_mapflag_args args = {};
  11547. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11548. args.flag_val = SKILLDMG_MAX;
  11549. else
  11550. FETCH(4, args.flag_val);
  11551. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11552. return SCRIPT_CMD_SUCCESS;
  11553. }
  11554. BUILDIN_FUNC(setmapflag)
  11555. {
  11556. int16 m;
  11557. int mf;
  11558. const char *str;
  11559. str = script_getstr(st,2);
  11560. m = map_mapname2mapid(str);
  11561. if (m < 0) {
  11562. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11563. return SCRIPT_CMD_FAILURE;
  11564. }
  11565. mf = script_getnum(st, 3);
  11566. if( mf < MF_MIN || mf > MF_MAX ){
  11567. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11568. return SCRIPT_CMD_FAILURE;
  11569. }
  11570. union u_mapflag_args args = {};
  11571. switch(mf) {
  11572. case MF_SKILL_DAMAGE:
  11573. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11574. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11575. else {
  11576. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11577. return SCRIPT_CMD_FAILURE;
  11578. }
  11579. break;
  11580. case MF_SKILL_DURATION:
  11581. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11582. args.skill_duration.skill_id = script_getnum(st, 4);
  11583. if (!skill_get_index(args.skill_duration.skill_id)) {
  11584. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11585. return SCRIPT_CMD_FAILURE;
  11586. }
  11587. args.skill_duration.per = script_getnum(st, 5);
  11588. } else {
  11589. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11590. return SCRIPT_CMD_FAILURE;
  11591. }
  11592. break;
  11593. case MF_NOSAVE: // Assume setting "SavePoint"
  11594. args.nosave.map = 0;
  11595. args.nosave.x = -1;
  11596. args.nosave.y = -1;
  11597. break;
  11598. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11599. args.nightmaredrop.drop_id = -1;
  11600. args.nightmaredrop.drop_per = 300;
  11601. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11602. break;
  11603. default:
  11604. FETCH(4, args.flag_val);
  11605. break;
  11606. }
  11607. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11608. return SCRIPT_CMD_SUCCESS;
  11609. }
  11610. BUILDIN_FUNC(removemapflag)
  11611. {
  11612. int16 m;
  11613. int mf;
  11614. const char *str;
  11615. union u_mapflag_args args = {};
  11616. str = script_getstr(st, 2);
  11617. m = map_mapname2mapid(str);
  11618. if (m < 0) {
  11619. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11620. return SCRIPT_CMD_FAILURE;
  11621. }
  11622. mf = script_getnum(st, 3);
  11623. if( mf < MF_MIN || mf > MF_MAX ){
  11624. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11625. return SCRIPT_CMD_FAILURE;
  11626. }
  11627. FETCH(4, args.flag_val);
  11628. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11629. return SCRIPT_CMD_SUCCESS;
  11630. }
  11631. BUILDIN_FUNC(pvpon)
  11632. {
  11633. int16 m;
  11634. const char *str = script_getstr(st,2);
  11635. m = map_mapname2mapid(str);
  11636. if (m < 0) {
  11637. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11638. return SCRIPT_CMD_FAILURE;
  11639. }
  11640. if (!map_getmapflag(m, MF_PVP))
  11641. map_setmapflag(m, MF_PVP, true);
  11642. return SCRIPT_CMD_SUCCESS;
  11643. }
  11644. BUILDIN_FUNC(pvpoff)
  11645. {
  11646. int16 m;
  11647. const char *str = script_getstr(st,2);
  11648. m = map_mapname2mapid(str);
  11649. if (m < 0) {
  11650. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11651. return SCRIPT_CMD_FAILURE;
  11652. }
  11653. if (map_getmapflag(m, MF_PVP))
  11654. map_setmapflag(m, MF_PVP, false);
  11655. return SCRIPT_CMD_SUCCESS;
  11656. }
  11657. BUILDIN_FUNC(gvgon)
  11658. {
  11659. int16 m;
  11660. const char *str = script_getstr(st,2);
  11661. m = map_mapname2mapid(str);
  11662. if (m < 0) {
  11663. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11664. return SCRIPT_CMD_FAILURE;
  11665. }
  11666. if (!map_getmapflag(m, MF_GVG))
  11667. map_setmapflag(m, MF_GVG, true);
  11668. return SCRIPT_CMD_SUCCESS;
  11669. }
  11670. BUILDIN_FUNC(gvgoff)
  11671. {
  11672. int16 m;
  11673. const char *str = script_getstr(st,2);
  11674. m = map_mapname2mapid(str);
  11675. if (m < 0) {
  11676. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11677. return SCRIPT_CMD_FAILURE;
  11678. }
  11679. if (map_getmapflag(m, MF_GVG))
  11680. map_setmapflag(m, MF_GVG, false);
  11681. return SCRIPT_CMD_SUCCESS;
  11682. }
  11683. BUILDIN_FUNC(gvgon3)
  11684. {
  11685. int16 m;
  11686. const char *str = script_getstr(st,2);
  11687. m = map_mapname2mapid(str);
  11688. if (m < 0) {
  11689. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11690. return SCRIPT_CMD_FAILURE;
  11691. }
  11692. if (!map_getmapflag(m, MF_GVG_TE))
  11693. map_setmapflag(m, MF_GVG_TE, true);
  11694. return SCRIPT_CMD_SUCCESS;
  11695. }
  11696. BUILDIN_FUNC(gvgoff3)
  11697. {
  11698. int16 m;
  11699. const char *str = script_getstr(st,2);
  11700. m = map_mapname2mapid(str);
  11701. if (m < 0) {
  11702. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11703. return SCRIPT_CMD_FAILURE;
  11704. }
  11705. if (map_getmapflag(m, MF_GVG_TE))
  11706. map_setmapflag(m, MF_GVG_TE, false);
  11707. return SCRIPT_CMD_SUCCESS;
  11708. }
  11709. /**
  11710. * Shows an emotion on top of a NPC by default or the given GID
  11711. * emotion <emotion ID>{,<target ID>};
  11712. */
  11713. BUILDIN_FUNC(emotion)
  11714. {
  11715. struct block_list *bl = NULL;
  11716. int type = script_getnum(st,2);
  11717. if (type < ET_SURPRISE || type >= ET_MAX) {
  11718. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11719. return SCRIPT_CMD_FAILURE;
  11720. }
  11721. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11722. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11723. return SCRIPT_CMD_FAILURE;
  11724. }
  11725. if (!bl)
  11726. bl = map_id2bl(st->oid);
  11727. clif_emotion(bl, type);
  11728. return SCRIPT_CMD_SUCCESS;
  11729. }
  11730. static int buildin_maprespawnguildid_sub_pc(map_session_data* sd, va_list ap)
  11731. {
  11732. int16 m=va_arg(ap,int);
  11733. int g_id=va_arg(ap,int);
  11734. int flag=va_arg(ap,int);
  11735. if(!sd || sd->bl.m != m)
  11736. return 0;
  11737. if(
  11738. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11739. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11740. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11741. )
  11742. pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT );
  11743. return 1;
  11744. }
  11745. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11746. {
  11747. struct mob_data *md=(struct mob_data *)bl;
  11748. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11749. status_kill(bl);
  11750. return 1;
  11751. }
  11752. /*
  11753. * Function to kick guild members out of a map and to their save points.
  11754. * m : mapid
  11755. * g_id : owner guild id
  11756. * flag & 1 : Warp out owners
  11757. * flag & 2 : Warp out outsiders
  11758. * flag & 4 : reserved for mobs
  11759. * */
  11760. BUILDIN_FUNC(maprespawnguildid)
  11761. {
  11762. const char *mapname=script_getstr(st,2);
  11763. int g_id=script_getnum(st,3);
  11764. int flag=script_getnum(st,4);
  11765. int16 m=map_mapname2mapid(mapname);
  11766. if(m == -1)
  11767. return SCRIPT_CMD_SUCCESS;
  11768. //Catch ALL players (in case some are 'between maps' on execution time)
  11769. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11770. if (flag&4) //Remove script mobs.
  11771. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11772. return SCRIPT_CMD_SUCCESS;
  11773. }
  11774. /// Siege commands
  11775. /**
  11776. * Start WoE:FE
  11777. * agitstart();
  11778. */
  11779. BUILDIN_FUNC(agitstart)
  11780. {
  11781. guild_agit_start();
  11782. return SCRIPT_CMD_SUCCESS;
  11783. }
  11784. /**
  11785. * End WoE:FE
  11786. * agitend();
  11787. */
  11788. BUILDIN_FUNC(agitend)
  11789. {
  11790. guild_agit_end();
  11791. return SCRIPT_CMD_SUCCESS;
  11792. }
  11793. /**
  11794. * Start WoE:SE
  11795. * agitstart2();
  11796. */
  11797. BUILDIN_FUNC(agitstart2)
  11798. {
  11799. guild_agit2_start();
  11800. return SCRIPT_CMD_SUCCESS;
  11801. }
  11802. /**
  11803. * End WoE:SE
  11804. * agitend();
  11805. */
  11806. BUILDIN_FUNC(agitend2)
  11807. {
  11808. guild_agit2_end();
  11809. return SCRIPT_CMD_SUCCESS;
  11810. }
  11811. /**
  11812. * Start WoE:TE
  11813. * agitstart3();
  11814. */
  11815. BUILDIN_FUNC(agitstart3)
  11816. {
  11817. guild_agit3_start();
  11818. return SCRIPT_CMD_SUCCESS;
  11819. }
  11820. /**
  11821. * End WoE:TE
  11822. * agitend3();
  11823. */
  11824. BUILDIN_FUNC(agitend3)
  11825. {
  11826. guild_agit3_end();
  11827. return SCRIPT_CMD_SUCCESS;
  11828. }
  11829. /**
  11830. * Returns whether WoE:FE is on or off.
  11831. * agitcheck();
  11832. */
  11833. BUILDIN_FUNC(agitcheck)
  11834. {
  11835. script_pushint(st, agit_flag);
  11836. return SCRIPT_CMD_SUCCESS;
  11837. }
  11838. /**
  11839. * Returns whether WoE:SE is on or off.
  11840. * agitcheck2();
  11841. */
  11842. BUILDIN_FUNC(agitcheck2)
  11843. {
  11844. script_pushint(st, agit2_flag);
  11845. return SCRIPT_CMD_SUCCESS;
  11846. }
  11847. /**
  11848. * Returns whether WoE:TE is on or off.
  11849. * agitcheck3();
  11850. */
  11851. BUILDIN_FUNC(agitcheck3)
  11852. {
  11853. script_pushint(st, agit3_flag);
  11854. return SCRIPT_CMD_SUCCESS;
  11855. }
  11856. /// Sets the guild_id of this npc.
  11857. ///
  11858. /// flagemblem <guild_id>;
  11859. BUILDIN_FUNC(flagemblem)
  11860. {
  11861. TBL_NPC* nd;
  11862. int g_id = script_getnum(st,2);
  11863. if(g_id < 0)
  11864. return SCRIPT_CMD_SUCCESS;
  11865. nd = (TBL_NPC*)map_id2nd(st->oid);
  11866. if( nd == NULL ) {
  11867. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11868. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11869. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11870. } else {
  11871. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11872. nd->u.scr.guild_id = g_id;
  11873. clif_guild_emblem_area(&nd->bl);
  11874. /* guild flag caching */
  11875. if( g_id ) /* adding a id */
  11876. guild_flag_add(nd);
  11877. else if( changed ) /* removing a flag */
  11878. guild_flag_remove(nd);
  11879. }
  11880. return SCRIPT_CMD_SUCCESS;
  11881. }
  11882. BUILDIN_FUNC(getcastlename)
  11883. {
  11884. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11885. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11886. const char* name = (gc) ? gc->castle_name : "";
  11887. script_pushstrcopy(st,name);
  11888. return SCRIPT_CMD_SUCCESS;
  11889. }
  11890. BUILDIN_FUNC(getcastledata)
  11891. {
  11892. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11893. int index = script_getnum(st,3);
  11894. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11895. if (gc == NULL) {
  11896. script_pushint(st,0);
  11897. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11898. return SCRIPT_CMD_FAILURE;
  11899. }
  11900. switch (index) {
  11901. case CD_GUILD_ID:
  11902. script_pushint(st,gc->guild_id); break;
  11903. case CD_CURRENT_ECONOMY:
  11904. script_pushint(st,gc->economy); break;
  11905. case CD_CURRENT_DEFENSE:
  11906. script_pushint(st,gc->defense); break;
  11907. case CD_INVESTED_ECONOMY:
  11908. script_pushint(st,gc->triggerE); break;
  11909. case CD_INVESTED_DEFENSE:
  11910. script_pushint(st,gc->triggerD); break;
  11911. case CD_NEXT_TIME:
  11912. script_pushint(st,gc->nextTime); break;
  11913. case CD_PAY_TIME:
  11914. script_pushint(st,gc->payTime); break;
  11915. case CD_CREATE_TIME:
  11916. script_pushint(st,gc->createTime); break;
  11917. case CD_ENABLED_KAFRA:
  11918. script_pushint(st,gc->visibleC); break;
  11919. default:
  11920. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11921. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11922. break;
  11923. }
  11924. script_pushint(st,0);
  11925. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11926. return SCRIPT_CMD_FAILURE;
  11927. }
  11928. return SCRIPT_CMD_SUCCESS;
  11929. }
  11930. BUILDIN_FUNC(setcastledata)
  11931. {
  11932. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11933. int index = script_getnum(st,3);
  11934. int value = script_getnum(st,4);
  11935. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11936. if (gc == NULL) {
  11937. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11938. return SCRIPT_CMD_FAILURE;
  11939. }
  11940. if (index <= CD_NONE || index >= CD_MAX) {
  11941. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11942. return SCRIPT_CMD_FAILURE;
  11943. }
  11944. guild_castledatasave(gc->castle_id, index, value);
  11945. return SCRIPT_CMD_SUCCESS;
  11946. }
  11947. /* =====================================================================
  11948. * ---------------------------------------------------------------------*/
  11949. BUILDIN_FUNC(requestguildinfo)
  11950. {
  11951. int guild_id=script_getnum(st,2);
  11952. const char *event=NULL;
  11953. if( script_hasdata(st,3) ){
  11954. event=script_getstr(st,3);
  11955. check_event(st, event);
  11956. }
  11957. if(guild_id>0)
  11958. guild_npc_request_info(guild_id,event);
  11959. return SCRIPT_CMD_SUCCESS;
  11960. }
  11961. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11962. /// getequipcardcnt(<equipment slot>);
  11963. BUILDIN_FUNC(getequipcardcnt)
  11964. {
  11965. int i=-1,j,num;
  11966. TBL_PC *sd;
  11967. int count;
  11968. if( !script_rid2sd(sd) )
  11969. return SCRIPT_CMD_SUCCESS;
  11970. num=script_getnum(st,2);
  11971. if (equip_index_check(num))
  11972. i=pc_checkequip(sd,equip_bitmask[num]);
  11973. if (i < 0 || !sd->inventory_data[i]) {
  11974. script_pushint(st,0);
  11975. return SCRIPT_CMD_SUCCESS;
  11976. }
  11977. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11978. {
  11979. script_pushint(st,0);
  11980. return SCRIPT_CMD_SUCCESS;
  11981. }
  11982. count = 0;
  11983. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11984. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11985. count++;
  11986. script_pushint(st,count);
  11987. return SCRIPT_CMD_SUCCESS;
  11988. }
  11989. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11990. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11991. /// successremovecards <slot>;
  11992. BUILDIN_FUNC(successremovecards) {
  11993. int i=-1,c,cardflag=0;
  11994. TBL_PC* sd;
  11995. int num;
  11996. if( !script_rid2sd(sd) )
  11997. return SCRIPT_CMD_SUCCESS;
  11998. num = script_getnum(st,2);
  11999. if (equip_index_check(num))
  12000. i=pc_checkequip(sd,equip_bitmask[num]);
  12001. if (i < 0 || !sd->inventory_data[i]) {
  12002. return SCRIPT_CMD_SUCCESS;
  12003. }
  12004. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12005. return SCRIPT_CMD_SUCCESS;
  12006. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  12007. 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
  12008. unsigned char flag = 0;
  12009. struct item item_tmp;
  12010. memset(&item_tmp,0,sizeof(item_tmp));
  12011. cardflag = 1;
  12012. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  12013. item_tmp.identify = 1;
  12014. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  12015. clif_additem(sd,0,0,flag);
  12016. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  12017. }
  12018. }
  12019. }
  12020. if(cardflag == 1) {//if card was remove remplace item with no card
  12021. unsigned char flag = 0;
  12022. struct item item_tmp;
  12023. memset(&item_tmp,0,sizeof(item_tmp));
  12024. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  12025. item_tmp.identify = 1;
  12026. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  12027. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  12028. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  12029. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  12030. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  12031. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  12032. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  12033. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  12034. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  12035. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  12036. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  12037. }
  12038. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  12039. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  12040. clif_additem(sd,0,0,flag);
  12041. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  12042. }
  12043. clif_misceffect(&sd->bl,3);
  12044. }
  12045. return SCRIPT_CMD_SUCCESS;
  12046. }
  12047. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  12048. /// failedremovecards <slot>, <type>;
  12049. /// <type>=0 : will destroy both the item and the cards.
  12050. /// <type>=1 : will keep the item, but destroy the cards.
  12051. /// <type>=2 : will keep the cards, but destroy the item.
  12052. /// <type>=? : will just display the failure effect.
  12053. BUILDIN_FUNC(failedremovecards) {
  12054. int i=-1,c,cardflag=0;
  12055. TBL_PC* sd;
  12056. int num;
  12057. int typefail;
  12058. if( !script_rid2sd(sd) )
  12059. return SCRIPT_CMD_SUCCESS;
  12060. num = script_getnum(st,2);
  12061. typefail = script_getnum(st,3);
  12062. if (equip_index_check(num))
  12063. i=pc_checkequip(sd,equip_bitmask[num]);
  12064. if (i < 0 || !sd->inventory_data[i])
  12065. return SCRIPT_CMD_SUCCESS;
  12066. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12067. return SCRIPT_CMD_SUCCESS;
  12068. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  12069. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  12070. cardflag = 1;
  12071. if(typefail == 2) {// add cards to inventory, clear
  12072. unsigned char flag = 0;
  12073. struct item item_tmp;
  12074. memset(&item_tmp,0,sizeof(item_tmp));
  12075. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  12076. item_tmp.identify = 1;
  12077. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  12078. clif_additem(sd,0,0,flag);
  12079. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  12080. }
  12081. }
  12082. }
  12083. }
  12084. if(cardflag == 1) {
  12085. if(typefail == 0 || typefail == 2){ // destroy the item
  12086. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  12087. }else if(typefail == 1){ // destroy the card
  12088. unsigned char flag = 0;
  12089. struct item item_tmp;
  12090. memset(&item_tmp,0,sizeof(item_tmp));
  12091. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  12092. item_tmp.identify = 1;
  12093. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  12094. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  12095. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  12096. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  12097. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  12098. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  12099. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  12100. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  12101. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  12102. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  12103. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  12104. }
  12105. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  12106. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  12107. clif_additem(sd,0,0,flag);
  12108. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  12109. }
  12110. }
  12111. clif_misceffect(&sd->bl,2);
  12112. }
  12113. return SCRIPT_CMD_SUCCESS;
  12114. }
  12115. /* ================================================================
  12116. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  12117. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  12118. * improved by [Lance]
  12119. * ================================================================*/
  12120. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  12121. {
  12122. int x,y,m,check_val=0,check_ID=0,i=0;
  12123. std::shared_ptr<MapGuild> g;
  12124. struct party_data *p = NULL;
  12125. const char *str;
  12126. const char *mapname;
  12127. unsigned int index;
  12128. mapname=script_getstr(st,2);
  12129. str=script_getstr(st,3);
  12130. x=script_getnum(st,4);
  12131. y=script_getnum(st,5);
  12132. if(script_hasdata(st,7)){
  12133. check_val=script_getnum(st,6);
  12134. check_ID=script_getnum(st,7);
  12135. }
  12136. if((m=map_mapname2mapid(mapname))< 0)
  12137. return SCRIPT_CMD_SUCCESS;
  12138. if(!(index=mapindex_name2id(str)))
  12139. return SCRIPT_CMD_SUCCESS;
  12140. switch(check_val){
  12141. case 1:
  12142. g = guild_search(check_ID);
  12143. if (g){
  12144. for( i=0; i < g->guild.max_member; i++)
  12145. {
  12146. if(g->guild.member[i].sd && g->guild.member[i].sd->bl.m==m){
  12147. pc_setpos(g->guild.member[i].sd,index,x,y,CLR_TELEPORT);
  12148. }
  12149. }
  12150. }
  12151. break;
  12152. case 2:
  12153. p = party_search(check_ID);
  12154. if(p){
  12155. for(i=0;i<MAX_PARTY; i++){
  12156. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  12157. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  12158. }
  12159. }
  12160. }
  12161. break;
  12162. default:
  12163. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  12164. break;
  12165. }
  12166. return SCRIPT_CMD_SUCCESS;
  12167. }
  12168. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  12169. {
  12170. char *event=va_arg(ap,char *);
  12171. struct mob_data *md = ((struct mob_data *)bl);
  12172. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  12173. return 1;
  12174. return SCRIPT_CMD_SUCCESS;
  12175. }
  12176. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  12177. {
  12178. const char *mapname,*event;
  12179. int16 m;
  12180. mapname=script_getstr(st,2);
  12181. event=script_getstr(st,3);
  12182. if( strcmp(event, "all") == 0 )
  12183. event = NULL;
  12184. else
  12185. check_event(st, event);
  12186. if( strcmp(mapname, "this") == 0 ) {
  12187. map_session_data *sd;
  12188. if( script_rid2sd(sd) )
  12189. m = sd->bl.m;
  12190. else {
  12191. script_pushint(st,-1);
  12192. return SCRIPT_CMD_SUCCESS;
  12193. }
  12194. }
  12195. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  12196. script_pushint(st,-1);
  12197. return SCRIPT_CMD_SUCCESS;
  12198. }
  12199. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  12200. return SCRIPT_CMD_SUCCESS;
  12201. }
  12202. BUILDIN_FUNC(marriage)
  12203. {
  12204. TBL_PC *sd;
  12205. TBL_PC *p_sd;
  12206. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  12207. script_pushint(st,0);
  12208. return SCRIPT_CMD_SUCCESS;
  12209. }
  12210. script_pushint(st,1);
  12211. return SCRIPT_CMD_SUCCESS;
  12212. }
  12213. BUILDIN_FUNC(wedding_effect)
  12214. {
  12215. TBL_PC *sd;
  12216. struct block_list *bl;
  12217. if(!script_rid2sd(sd)) {
  12218. bl=map_id2bl(st->oid);
  12219. } else
  12220. bl=&sd->bl;
  12221. clif_wedding_effect(bl);
  12222. return SCRIPT_CMD_SUCCESS;
  12223. }
  12224. /**
  12225. * divorce({<char_id>})
  12226. **/
  12227. BUILDIN_FUNC(divorce)
  12228. {
  12229. TBL_PC *sd;
  12230. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  12231. script_pushint(st,0);
  12232. return SCRIPT_CMD_FAILURE;
  12233. }
  12234. script_pushint(st,1);
  12235. return SCRIPT_CMD_SUCCESS;
  12236. }
  12237. /**
  12238. * ispartneron({<char_id>})
  12239. **/
  12240. BUILDIN_FUNC(ispartneron)
  12241. {
  12242. TBL_PC *sd;
  12243. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  12244. map_charid2sd(sd->status.partner_id) == NULL)
  12245. {
  12246. script_pushint(st,0);
  12247. return SCRIPT_CMD_FAILURE;
  12248. }
  12249. script_pushint(st,1);
  12250. return SCRIPT_CMD_SUCCESS;
  12251. }
  12252. /**
  12253. * getpartnerid({<char_id>})
  12254. **/
  12255. BUILDIN_FUNC(getpartnerid)
  12256. {
  12257. TBL_PC *sd;
  12258. if (!script_charid2sd(2,sd)) {
  12259. script_pushint(st,0);
  12260. return SCRIPT_CMD_FAILURE;
  12261. }
  12262. script_pushint(st,sd->status.partner_id);
  12263. return SCRIPT_CMD_SUCCESS;
  12264. }
  12265. /**
  12266. * getchildid({<char_id>})
  12267. **/
  12268. BUILDIN_FUNC(getchildid)
  12269. {
  12270. TBL_PC *sd;
  12271. if (!script_charid2sd(2,sd)) {
  12272. script_pushint(st,0);
  12273. return SCRIPT_CMD_FAILURE;
  12274. }
  12275. script_pushint(st,sd->status.child);
  12276. return SCRIPT_CMD_SUCCESS;
  12277. }
  12278. /**
  12279. * getmotherid({<char_id>})
  12280. **/
  12281. BUILDIN_FUNC(getmotherid)
  12282. {
  12283. TBL_PC *sd;
  12284. if (!script_charid2sd(2,sd)) {
  12285. script_pushint(st,0);
  12286. return SCRIPT_CMD_FAILURE;
  12287. }
  12288. script_pushint(st,sd->status.mother);
  12289. return SCRIPT_CMD_SUCCESS;
  12290. }
  12291. /**
  12292. * getfatherid({<char_id>})
  12293. **/
  12294. BUILDIN_FUNC(getfatherid)
  12295. {
  12296. TBL_PC *sd;
  12297. if (!script_charid2sd(2,sd)) {
  12298. script_pushint(st,0);
  12299. return SCRIPT_CMD_FAILURE;
  12300. }
  12301. script_pushint(st,sd->status.father);
  12302. return SCRIPT_CMD_SUCCESS;
  12303. }
  12304. BUILDIN_FUNC(warppartner)
  12305. {
  12306. int x,y;
  12307. unsigned short mapindex;
  12308. const char *str;
  12309. TBL_PC *sd;
  12310. TBL_PC *p_sd;
  12311. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  12312. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  12313. script_pushint(st,0);
  12314. return SCRIPT_CMD_SUCCESS;
  12315. }
  12316. str=script_getstr(st,2);
  12317. x=script_getnum(st,3);
  12318. y=script_getnum(st,4);
  12319. mapindex = mapindex_name2id(str);
  12320. if (mapindex) {
  12321. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  12322. script_pushint(st,1);
  12323. } else
  12324. script_pushint(st,0);
  12325. return SCRIPT_CMD_SUCCESS;
  12326. }
  12327. /*================================================
  12328. * Script for Displaying MOB Information [Valaris]
  12329. *------------------------------------------------*/
  12330. BUILDIN_FUNC(strmobinfo)
  12331. {
  12332. int num=script_getnum(st,2);
  12333. int class_=script_getnum(st,3);
  12334. if(!mobdb_checkid(class_))
  12335. {
  12336. if (num < 3) //requested a string
  12337. script_pushconststr(st,"");
  12338. else
  12339. script_pushint(st,0);
  12340. return SCRIPT_CMD_SUCCESS;
  12341. }
  12342. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  12343. switch (num) {
  12344. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  12345. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  12346. case 3: script_pushint(st,mob->lv); break;
  12347. case 4: script_pushint(st,mob->status.max_hp); break;
  12348. case 5: script_pushint(st,mob->status.max_sp); break;
  12349. case 6: script_pushint(st,mob->base_exp); break;
  12350. case 7: script_pushint(st,mob->job_exp); break;
  12351. default:
  12352. script_pushint(st,0);
  12353. break;
  12354. }
  12355. return SCRIPT_CMD_SUCCESS;
  12356. }
  12357. /*==========================================
  12358. * Summon guardians [Valaris]
  12359. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  12360. *------------------------------------------*/
  12361. BUILDIN_FUNC(guardian)
  12362. {
  12363. int class_=0,x=0,y=0,guardian=0;
  12364. const char *str,*mapname,*evt="";
  12365. bool has_index = false;
  12366. mapname =script_getstr(st,2);
  12367. x =script_getnum(st,3);
  12368. y =script_getnum(st,4);
  12369. str =script_getstr(st,5);
  12370. class_=script_getnum(st,6);
  12371. if( script_hasdata(st,8) )
  12372. {// "<event label>",<guardian index>
  12373. evt=script_getstr(st,7);
  12374. guardian=script_getnum(st,8);
  12375. has_index = true;
  12376. } else if( script_hasdata(st,7) ){
  12377. if( script_isstring(st, 7) )
  12378. {// "<event label>"
  12379. evt=script_getstr(st,7);
  12380. } else
  12381. {// <guardian index>
  12382. guardian=script_getnum(st,7);
  12383. has_index = true;
  12384. }
  12385. }
  12386. check_event(st, evt);
  12387. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  12388. return SCRIPT_CMD_SUCCESS;
  12389. }
  12390. /*==========================================
  12391. * Invisible Walls [Zephyrus]
  12392. *------------------------------------------*/
  12393. BUILDIN_FUNC(setwall)
  12394. {
  12395. const char *mapname, *name;
  12396. int x, y, m, size, dir;
  12397. bool shootable;
  12398. mapname = script_getstr(st,2);
  12399. x = script_getnum(st,3);
  12400. y = script_getnum(st,4);
  12401. size = script_getnum(st,5);
  12402. dir = script_getnum(st,6);
  12403. shootable = script_getnum(st,7) != 0;
  12404. name = script_getstr(st,8);
  12405. if( (m = map_mapname2mapid(mapname)) < 0 )
  12406. return SCRIPT_CMD_SUCCESS; // Invalid Map
  12407. map_iwall_set(m, x, y, size, dir, shootable, name);
  12408. return SCRIPT_CMD_SUCCESS;
  12409. }
  12410. BUILDIN_FUNC(delwall)
  12411. {
  12412. const char *name = script_getstr(st,2);
  12413. if( !map_iwall_remove(name) ){
  12414. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  12415. return SCRIPT_CMD_FAILURE;
  12416. }
  12417. return SCRIPT_CMD_SUCCESS;
  12418. }
  12419. BUILDIN_FUNC(checkwall)
  12420. {
  12421. const char *wall_name = script_getstr(st, 2);
  12422. script_pushint(st, map_iwall_exist(wall_name));
  12423. return SCRIPT_CMD_SUCCESS;
  12424. }
  12425. /// Retrieves various information about the specified guardian.
  12426. ///
  12427. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  12428. /// type: 0 - whether it is deployed or not
  12429. /// 1 - maximum hp
  12430. /// 2 - current hp
  12431. ///
  12432. BUILDIN_FUNC(guardianinfo)
  12433. {
  12434. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  12435. int id = script_getnum(st,3);
  12436. int type = script_getnum(st,4);
  12437. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  12438. struct mob_data* gd;
  12439. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  12440. {
  12441. script_pushint(st,-1);
  12442. return SCRIPT_CMD_SUCCESS;
  12443. }
  12444. if( type == 0 )
  12445. script_pushint(st, gc->guardian[id].visible);
  12446. else
  12447. if( !gc->guardian[id].visible )
  12448. script_pushint(st,-1);
  12449. else
  12450. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  12451. script_pushint(st,-1);
  12452. else
  12453. {
  12454. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  12455. else if( type == 2 ) script_pushint(st,gd->status.hp);
  12456. else
  12457. script_pushint(st,-1);
  12458. }
  12459. return SCRIPT_CMD_SUCCESS;
  12460. }
  12461. /*==========================================
  12462. * Get the item name by item_id or null
  12463. *------------------------------------------*/
  12464. BUILDIN_FUNC(getitemname)
  12465. {
  12466. std::shared_ptr<item_data> i_data;
  12467. if( script_isstring(st, 2) ){
  12468. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12469. }else{
  12470. i_data = item_db.find( script_getnum( st, 2 ) );
  12471. }
  12472. if( i_data == nullptr ){
  12473. script_pushconststr(st,"null");
  12474. return SCRIPT_CMD_SUCCESS;
  12475. }
  12476. char* item_name = (char *)aMalloc( ITEM_NAME_LENGTH * sizeof( char ) );
  12477. safestrncpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12478. script_pushstr(st,item_name);
  12479. return SCRIPT_CMD_SUCCESS;
  12480. }
  12481. /*==========================================
  12482. * Returns number of slots an item has. [Skotlex]
  12483. *------------------------------------------*/
  12484. BUILDIN_FUNC(getitemslots)
  12485. {
  12486. t_itemid item_id=script_getnum(st,2);
  12487. std::shared_ptr<item_data> i_data = item_db.find(item_id);
  12488. if (i_data != nullptr)
  12489. script_pushint(st,i_data->slots);
  12490. else
  12491. script_pushint(st,-1);
  12492. return SCRIPT_CMD_SUCCESS;
  12493. }
  12494. // TODO: add matk here if needed/once we get rid of RENEWAL
  12495. /*==========================================
  12496. * Returns some values of an item [Lupus]
  12497. * Price, Weight, etc...
  12498. *------------------------------------------*/
  12499. BUILDIN_FUNC(getiteminfo)
  12500. {
  12501. std::shared_ptr<item_data> i_data;
  12502. int type = script_getnum(st, 3);
  12503. if (script_isstring(st, 2))
  12504. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12505. else
  12506. i_data = item_db.find( script_getnum( st, 2 ) );
  12507. if (i_data == nullptr) {
  12508. if (type != ITEMINFO_AEGISNAME)
  12509. script_pushint(st, -1);
  12510. else
  12511. script_pushstrcopy(st, "");
  12512. return SCRIPT_CMD_SUCCESS;
  12513. }
  12514. switch( type ) {
  12515. case ITEMINFO_BUY: script_pushint(st, i_data->value_buy); break;
  12516. case ITEMINFO_SELL: script_pushint(st, i_data->value_sell); break;
  12517. case ITEMINFO_TYPE: script_pushint(st, i_data->type); break;
  12518. case ITEMINFO_MAXCHANCE: script_pushint(st, i_data->maxchance); break;
  12519. case ITEMINFO_GENDER: script_pushint(st, i_data->sex); break;
  12520. case ITEMINFO_LOCATIONS: script_pushint(st, i_data->equip); break;
  12521. case ITEMINFO_WEIGHT: script_pushint(st, i_data->weight); break;
  12522. case ITEMINFO_ATTACK: script_pushint(st, i_data->atk); break;
  12523. case ITEMINFO_DEFENSE: script_pushint(st, i_data->def); break;
  12524. case ITEMINFO_RANGE: script_pushint(st, i_data->range); break;
  12525. case ITEMINFO_SLOT: script_pushint(st, i_data->slots); break;
  12526. case ITEMINFO_VIEW:
  12527. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12528. script_pushint(st, i_data->subtype);
  12529. } else {
  12530. script_pushint(st, i_data->look);
  12531. }
  12532. break;
  12533. case ITEMINFO_EQUIPLEVELMIN: script_pushint(st, i_data->elv); break;
  12534. case ITEMINFO_WEAPONLEVEL:
  12535. if( i_data->type == IT_WEAPON ){
  12536. script_pushint( st, i_data->weapon_level );
  12537. }else{
  12538. script_pushint( st, 0 );
  12539. }
  12540. break;
  12541. case ITEMINFO_ARMORLEVEL:
  12542. if( i_data->type == IT_ARMOR ){
  12543. script_pushint( st, i_data->armor_level );
  12544. }else{
  12545. script_pushint( st, 0 );
  12546. }
  12547. break;
  12548. case ITEMINFO_ALIASNAME: script_pushint(st, i_data->view_id); break;
  12549. case ITEMINFO_EQUIPLEVELMAX: script_pushint(st, i_data->elvmax); break;
  12550. case ITEMINFO_MAGICATTACK: {
  12551. #ifdef RENEWAL
  12552. script_pushint(st, i_data->matk);
  12553. #else
  12554. script_pushint(st, 0);
  12555. #endif
  12556. break;
  12557. }
  12558. case ITEMINFO_ID: script_pushint(st, i_data->nameid); break;
  12559. case ITEMINFO_AEGISNAME: script_pushstrcopy(st, i_data->name.c_str()); break;
  12560. case ITEMINFO_SUBTYPE: script_pushint(st, i_data->subtype); break;
  12561. default:
  12562. script_pushint(st, -1);
  12563. break;
  12564. }
  12565. return SCRIPT_CMD_SUCCESS;
  12566. }
  12567. /*==========================================
  12568. * Set some values of an item [Lupus]
  12569. * Price, Weight, etc...
  12570. *------------------------------------------*/
  12571. BUILDIN_FUNC(setiteminfo)
  12572. {
  12573. std::shared_ptr<item_data> i_data;
  12574. if (script_isstring(st, 2))
  12575. i_data = item_db.search_aegisname( script_getstr( st, 2 ) );
  12576. else
  12577. i_data = item_db.find( script_getnum( st, 2 ) );
  12578. if (i_data == nullptr) {
  12579. script_pushint(st, -1);
  12580. return SCRIPT_CMD_SUCCESS;
  12581. }
  12582. int value = script_getnum(st,4);
  12583. switch( script_getnum(st, 3) ) {
  12584. case ITEMINFO_BUY: i_data->value_buy = static_cast<uint32>(value); break;
  12585. case ITEMINFO_SELL: i_data->value_sell = static_cast<uint32>(value); break;
  12586. case ITEMINFO_TYPE: i_data->type = static_cast<item_types>(value); break;
  12587. case ITEMINFO_MAXCHANCE: i_data->maxchance = static_cast<int>(value); break;
  12588. case ITEMINFO_GENDER: i_data->sex = static_cast<uint8>(value); break;
  12589. case ITEMINFO_LOCATIONS: i_data->equip = static_cast<uint32>(value); break;
  12590. case ITEMINFO_WEIGHT: i_data->weight = static_cast<uint32>(value); break;
  12591. case ITEMINFO_ATTACK: i_data->atk = static_cast<uint32>(value); break;
  12592. case ITEMINFO_DEFENSE: i_data->def = static_cast<uint32>(value); break;
  12593. case ITEMINFO_RANGE: i_data->range = static_cast<uint16>(value); break;
  12594. case ITEMINFO_SLOT: i_data->slots = static_cast<uint16>(value); break;
  12595. case ITEMINFO_VIEW:
  12596. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12597. i_data->subtype = static_cast<uint8>(value);
  12598. } else {
  12599. i_data->look = static_cast<uint32>(value);
  12600. }
  12601. break;
  12602. case ITEMINFO_EQUIPLEVELMIN: i_data->elv = static_cast<uint16>(value); break;
  12603. case ITEMINFO_WEAPONLEVEL:
  12604. if( i_data->type == IT_WEAPON ){
  12605. if( value > MAX_WEAPON_LEVEL ){
  12606. ShowError( "buildin_setiteminfo: weapon level %d is above maximum %d.\n", value, MAX_WEAPON_LEVEL );
  12607. script_pushint( st, -1 );
  12608. return SCRIPT_CMD_FAILURE;
  12609. }
  12610. i_data->weapon_level = static_cast<uint16>(value);
  12611. break;
  12612. }else{
  12613. ShowError( "buildin_setiteminfo: Can not set a weapon level for %d.\n", i_data->nameid );
  12614. script_pushint( st, -1 );
  12615. return SCRIPT_CMD_FAILURE;
  12616. }
  12617. case ITEMINFO_ARMORLEVEL:
  12618. if( i_data->type == IT_ARMOR ){
  12619. if( value > MAX_ARMOR_LEVEL ){
  12620. ShowError( "buildin_setiteminfo: armor level %d is above maximum %d.\n", value, MAX_ARMOR_LEVEL );
  12621. script_pushint( st, -1 );
  12622. return SCRIPT_CMD_FAILURE;
  12623. }
  12624. i_data->armor_level = static_cast<uint16>(value);
  12625. break;
  12626. }else{
  12627. ShowError( "buildin_setiteminfo: Can not set an armor level for %d.\n", i_data->nameid );
  12628. script_pushint( st, -1 );
  12629. return SCRIPT_CMD_FAILURE;
  12630. }
  12631. case ITEMINFO_ALIASNAME: i_data->view_id = static_cast<t_itemid>(value); break;
  12632. case ITEMINFO_EQUIPLEVELMAX: i_data->elvmax = static_cast<uint16>(value); break;
  12633. case ITEMINFO_MAGICATTACK: {
  12634. #ifdef RENEWAL
  12635. i_data->matk = static_cast<uint32>(value);
  12636. #else
  12637. value = 0;
  12638. #endif
  12639. break;
  12640. }
  12641. case ITEMINFO_SUBTYPE: i_data->subtype = static_cast<uint8>(value); break;
  12642. default:
  12643. script_pushint(st, -1);
  12644. break;
  12645. }
  12646. script_pushint(st, value);
  12647. return SCRIPT_CMD_SUCCESS;
  12648. }
  12649. /*==========================================
  12650. * Returns value from equipped item slot n [Lupus]
  12651. getequipcardid(num,slot)
  12652. where
  12653. num = eqip position slot
  12654. slot = 0,1,2,3 (Card Slot N)
  12655. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12656. it's useful when you want to check item cards or if it's signed
  12657. Useful for such quests as "Sign this refined item with players name" etc
  12658. Hat[0] +4 -> Player's Hat[0] +4
  12659. *------------------------------------------*/
  12660. BUILDIN_FUNC(getequipcardid)
  12661. {
  12662. int i=-1,num,slot;
  12663. TBL_PC *sd;
  12664. if( !script_rid2sd(sd) )
  12665. return SCRIPT_CMD_SUCCESS;
  12666. num=script_getnum(st,2);
  12667. slot=script_getnum(st,3);
  12668. if (equip_index_check(num))
  12669. i=pc_checkequip(sd,equip_bitmask[num]);
  12670. if(i >= 0 && slot>=0 && slot<4)
  12671. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12672. else
  12673. script_pushint(st,0);
  12674. return SCRIPT_CMD_SUCCESS;
  12675. }
  12676. /*==========================================
  12677. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12678. *------------------------------------------*/
  12679. BUILDIN_FUNC(petskillbonus)
  12680. {
  12681. struct pet_data *pd;
  12682. TBL_PC *sd;
  12683. if(!script_rid2sd(sd) || sd->pd == NULL)
  12684. return SCRIPT_CMD_FAILURE;
  12685. pd = sd->pd;
  12686. if (pd->bonus)
  12687. { //Clear previous bonus
  12688. if (pd->bonus->timer != INVALID_TIMER)
  12689. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12690. } else //init
  12691. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12692. pd->bonus->type = script_getnum(st,2);
  12693. pd->bonus->val = script_getnum(st,3);
  12694. pd->bonus->duration = script_getnum(st,4);
  12695. pd->bonus->delay = script_getnum(st,5);
  12696. if (pd->state.skillbonus == 1)
  12697. pd->state.skillbonus = 0; // waiting state
  12698. // wait for timer to start
  12699. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12700. pd->bonus->timer = INVALID_TIMER;
  12701. else
  12702. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12703. return SCRIPT_CMD_SUCCESS;
  12704. }
  12705. /*==========================================
  12706. * pet looting [Valaris] //Rewritten by [Skotlex]
  12707. *------------------------------------------*/
  12708. BUILDIN_FUNC(petloot)
  12709. {
  12710. int max;
  12711. struct pet_data *pd;
  12712. TBL_PC *sd;
  12713. if(!script_rid2sd(sd) || sd->pd==NULL)
  12714. return SCRIPT_CMD_SUCCESS;
  12715. max=script_getnum(st,2);
  12716. if(max < 1)
  12717. max = 1; //Let'em loot at least 1 item.
  12718. else if (max > MAX_PETLOOT_SIZE)
  12719. max = MAX_PETLOOT_SIZE;
  12720. pd = sd->pd;
  12721. if (pd->loot != NULL)
  12722. { //Release whatever was there already and reallocate memory
  12723. pet_lootitem_drop(pd, pd->master);
  12724. aFree(pd->loot->item);
  12725. }
  12726. else
  12727. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12728. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12729. pd->loot->max=max;
  12730. pd->loot->count = 0;
  12731. pd->loot->weight = 0;
  12732. return SCRIPT_CMD_SUCCESS;
  12733. }
  12734. /*==========================================
  12735. * Set arrays with info of all sd inventory :
  12736. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12737. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12738. * @inventorylist_card(0..3), @inventorylist_expire
  12739. * @inventorylist_count = scalar
  12740. *------------------------------------------*/
  12741. BUILDIN_FUNC(getinventorylist)
  12742. {
  12743. TBL_PC *sd;
  12744. char card_var[NAME_LENGTH], randopt_var[50];
  12745. int i,j=0,k;
  12746. if (!script_charid2sd(2,sd))
  12747. return SCRIPT_CMD_FAILURE;
  12748. for(i=0;i<MAX_INVENTORY;i++){
  12749. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12750. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12751. pc_setreg(sd,reference_uid(add_str("@inventorylist_idx"), j),i);
  12752. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12753. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12754. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12755. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12756. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12757. for (k = 0; k < MAX_SLOTS; k++)
  12758. {
  12759. sprintf(card_var, "@inventorylist_card%d",k+1);
  12760. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12761. }
  12762. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12763. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12764. pc_setreg(sd,reference_uid(add_str("@inventorylist_enchantgrade"), j),sd->inventory.u.items_inventory[i].enchantgrade);
  12765. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12766. {
  12767. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12768. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12769. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12770. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12771. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12772. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12773. }
  12774. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12775. pc_setreg(sd,reference_uid(add_str("@inventorylist_favorite"), j),sd->inventory.u.items_inventory[i].favorite);
  12776. j++;
  12777. }
  12778. }
  12779. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12780. return SCRIPT_CMD_SUCCESS;
  12781. }
  12782. /**
  12783. * getskilllist ({<char_id>});
  12784. **/
  12785. BUILDIN_FUNC(getskilllist)
  12786. {
  12787. TBL_PC *sd;
  12788. int i,j=0;
  12789. if (!script_charid2sd(2,sd))
  12790. return SCRIPT_CMD_FAILURE;
  12791. for(i=0;i<MAX_SKILL;i++){
  12792. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12793. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12794. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12795. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12796. j++;
  12797. }
  12798. }
  12799. pc_setreg(sd,add_str("@skilllist_count"),j);
  12800. return SCRIPT_CMD_SUCCESS;
  12801. }
  12802. /**
  12803. * clearitem ({<char_id>});
  12804. **/
  12805. BUILDIN_FUNC(clearitem)
  12806. {
  12807. TBL_PC *sd;
  12808. int i;
  12809. if (!script_charid2sd(2,sd))
  12810. return SCRIPT_CMD_FAILURE;
  12811. for (i=0; i<MAX_INVENTORY; i++) {
  12812. if (sd->inventory.u.items_inventory[i].amount) {
  12813. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12814. }
  12815. }
  12816. return SCRIPT_CMD_SUCCESS;
  12817. }
  12818. /**
  12819. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12820. * disguise <Monster ID>{,<char_id>};
  12821. **/
  12822. BUILDIN_FUNC(disguise)
  12823. {
  12824. int id;
  12825. TBL_PC* sd;
  12826. if (!script_charid2sd(3,sd))
  12827. return SCRIPT_CMD_FAILURE;
  12828. id = script_getnum(st,2);
  12829. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12830. pc_disguise(sd, id);
  12831. script_pushint(st,id);
  12832. } else
  12833. script_pushint(st,0);
  12834. return SCRIPT_CMD_SUCCESS;
  12835. }
  12836. /**
  12837. * Undisguise Player (returns 1 if success, 0 on fail)
  12838. * undisguise {<char_id>};
  12839. **/
  12840. BUILDIN_FUNC(undisguise)
  12841. {
  12842. TBL_PC* sd;
  12843. if (!script_charid2sd(2,sd))
  12844. return SCRIPT_CMD_FAILURE;
  12845. if (sd->disguise) {
  12846. pc_disguise(sd, 0);
  12847. script_pushint(st,0);
  12848. } else {
  12849. script_pushint(st,1);
  12850. }
  12851. return SCRIPT_CMD_SUCCESS;
  12852. }
  12853. /**
  12854. * Transform an NPC to another _class
  12855. *
  12856. * classchange(<view id>{,"<NPC name>","<flag>"});
  12857. * @param flag: Specify target
  12858. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12859. * BC_SELF - Sprite is sent only to player attached.
  12860. */
  12861. BUILDIN_FUNC(classchange)
  12862. {
  12863. int _class, type = 1;
  12864. struct npc_data* nd = NULL;
  12865. TBL_PC *sd = map_id2sd(st->rid);
  12866. send_target target = AREA;
  12867. _class = script_getnum(st,2);
  12868. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12869. nd = npc_name2id(script_getstr(st, 3));
  12870. else
  12871. nd = (struct npc_data *)map_id2bl(st->oid);
  12872. if (nd == NULL)
  12873. return SCRIPT_CMD_FAILURE;
  12874. if (script_hasdata(st, 4)) {
  12875. switch(script_getnum(st, 4)) {
  12876. case BC_SELF: target = SELF; break;
  12877. case BC_AREA:
  12878. default: target = AREA; break;
  12879. }
  12880. }
  12881. if (target != SELF)
  12882. clif_class_change(&nd->bl,_class,type);
  12883. else if (sd == NULL)
  12884. return SCRIPT_CMD_FAILURE;
  12885. else
  12886. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12887. return SCRIPT_CMD_SUCCESS;
  12888. }
  12889. /*==========================================
  12890. * Display an effect
  12891. *------------------------------------------*/
  12892. BUILDIN_FUNC(misceffect)
  12893. {
  12894. int type;
  12895. type=script_getnum(st,2);
  12896. if( type <= EF_NONE || type >= EF_MAX ){
  12897. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12898. return SCRIPT_CMD_FAILURE;
  12899. }
  12900. if(st->oid && st->oid != fake_nd->bl.id) {
  12901. struct block_list *bl = map_id2bl(st->oid);
  12902. if (bl)
  12903. clif_specialeffect(bl,type,AREA);
  12904. } else{
  12905. TBL_PC *sd;
  12906. if(script_rid2sd(sd))
  12907. clif_specialeffect(&sd->bl,type,AREA);
  12908. }
  12909. return SCRIPT_CMD_SUCCESS;
  12910. }
  12911. /*==========================================
  12912. * Play a BGM on a single client [Rikter/Yommy]
  12913. *------------------------------------------*/
  12914. BUILDIN_FUNC(playBGM)
  12915. {
  12916. map_session_data* sd;
  12917. if( script_rid2sd(sd) ) {
  12918. clif_playBGM( *sd, script_getstr( st, 2 ) );
  12919. }
  12920. return SCRIPT_CMD_SUCCESS;
  12921. }
  12922. static int playBGM_sub(struct block_list* bl,va_list ap)
  12923. {
  12924. const char* name = va_arg(ap,const char*);
  12925. clif_playBGM( *BL_CAST( BL_PC, bl ), name );
  12926. return 0;
  12927. }
  12928. static int playBGM_foreachpc_sub(map_session_data* sd, va_list args)
  12929. {
  12930. const char* name = va_arg(args, const char*);
  12931. clif_playBGM( *sd, name );
  12932. return 0;
  12933. }
  12934. /*==========================================
  12935. * Play a BGM on multiple client [Rikter/Yommy]
  12936. *------------------------------------------*/
  12937. BUILDIN_FUNC(playBGMall)
  12938. {
  12939. const char* name;
  12940. name = script_getstr(st,2);
  12941. if( script_hasdata(st,7) ) {// specified part of map
  12942. const char* mapname = script_getstr(st,3);
  12943. int x0 = script_getnum(st,4);
  12944. int y0 = script_getnum(st,5);
  12945. int x1 = script_getnum(st,6);
  12946. int y1 = script_getnum(st,7);
  12947. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12948. }
  12949. else if( script_hasdata(st,3) ) {// entire map
  12950. const char* mapname = script_getstr(st,3);
  12951. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12952. }
  12953. else {// entire server
  12954. map_foreachpc(&playBGM_foreachpc_sub, name);
  12955. }
  12956. return SCRIPT_CMD_SUCCESS;
  12957. }
  12958. /*==========================================
  12959. * Play a .wav sound for sd
  12960. *------------------------------------------*/
  12961. BUILDIN_FUNC(soundeffect)
  12962. {
  12963. map_session_data* sd;
  12964. if(script_rid2sd(sd)){
  12965. const char* name = script_getstr(st,2);
  12966. int type = script_getnum(st,3);
  12967. clif_soundeffect( sd->bl, name, type, SELF );
  12968. }
  12969. return SCRIPT_CMD_SUCCESS;
  12970. }
  12971. int soundeffect_sub(struct block_list* bl,va_list ap)
  12972. {
  12973. char* name = va_arg(ap,char*);
  12974. int type = va_arg(ap,int);
  12975. clif_soundeffect( *bl, name, type, SELF );
  12976. return 0;
  12977. }
  12978. /*==========================================
  12979. * Play a sound effect (.wav) on multiple clients
  12980. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12981. *------------------------------------------*/
  12982. BUILDIN_FUNC(soundeffectall)
  12983. {
  12984. struct block_list* bl;
  12985. map_session_data* sd;
  12986. const char* name;
  12987. int type;
  12988. if( st->rid && script_rid2sd(sd) )
  12989. bl = &sd->bl;
  12990. else
  12991. bl = map_id2bl(st->oid);
  12992. if (!bl)
  12993. return SCRIPT_CMD_SUCCESS;
  12994. name = script_getstr(st,2);
  12995. type = script_getnum(st,3);
  12996. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12997. if(!script_hasdata(st,4))
  12998. { // area around
  12999. clif_soundeffect( *bl, name, type, AREA );
  13000. }
  13001. else
  13002. if(!script_hasdata(st,5))
  13003. { // entire map
  13004. const char* mapname = script_getstr(st,4);
  13005. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  13006. }
  13007. else
  13008. if(script_hasdata(st,8))
  13009. { // specified part of map
  13010. const char* mapname = script_getstr(st,4);
  13011. int x0 = script_getnum(st,5);
  13012. int y0 = script_getnum(st,6);
  13013. int x1 = script_getnum(st,7);
  13014. int y1 = script_getnum(st,8);
  13015. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  13016. }
  13017. else
  13018. {
  13019. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  13020. }
  13021. return SCRIPT_CMD_SUCCESS;
  13022. }
  13023. /*==========================================
  13024. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  13025. *------------------------------------------*/
  13026. BUILDIN_FUNC(petrecovery)
  13027. {
  13028. struct pet_data *pd;
  13029. TBL_PC *sd;
  13030. int sc;
  13031. if(!script_rid2sd(sd) || sd->pd == NULL)
  13032. return SCRIPT_CMD_FAILURE;
  13033. sc = script_getnum(st,2);
  13034. if (sc <= SC_NONE || sc >= SC_MAX) {
  13035. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  13036. return SCRIPT_CMD_FAILURE;
  13037. }
  13038. pd = sd->pd;
  13039. if (pd->recovery)
  13040. { //Halt previous bonus
  13041. if (pd->recovery->timer != INVALID_TIMER)
  13042. delete_timer(pd->recovery->timer, pet_recovery_timer);
  13043. } else //Init
  13044. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  13045. pd->recovery->type = (sc_type)sc;
  13046. pd->recovery->delay = script_getnum(st,3);
  13047. pd->recovery->timer = INVALID_TIMER;
  13048. return SCRIPT_CMD_SUCCESS;
  13049. }
  13050. /*==========================================
  13051. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  13052. *------------------------------------------*/
  13053. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  13054. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  13055. BUILDIN_FUNC(petskillattack)
  13056. {
  13057. struct pet_data *pd;
  13058. TBL_PC *sd;
  13059. int id = 0;
  13060. if(!script_rid2sd(sd) || sd->pd == NULL)
  13061. return SCRIPT_CMD_FAILURE;
  13062. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13063. if (!id) {
  13064. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  13065. return SCRIPT_CMD_FAILURE;
  13066. }
  13067. pd = sd->pd;
  13068. if (pd->a_skill == NULL)
  13069. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  13070. pd->a_skill->id = id;
  13071. pd->a_skill->damage = 0;
  13072. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  13073. pd->a_skill->div_ = 0;
  13074. pd->a_skill->rate = script_getnum(st,4);
  13075. pd->a_skill->bonusrate = script_getnum(st,5);
  13076. return SCRIPT_CMD_SUCCESS;
  13077. }
  13078. /*==========================================
  13079. * pet attack skills [Valaris]
  13080. *------------------------------------------*/
  13081. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  13082. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  13083. BUILDIN_FUNC(petskillattack2)
  13084. {
  13085. struct pet_data *pd;
  13086. TBL_PC *sd;
  13087. int id = 0;
  13088. if(!script_rid2sd(sd) || sd->pd == NULL)
  13089. return SCRIPT_CMD_FAILURE;
  13090. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13091. if (!id) {
  13092. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  13093. return SCRIPT_CMD_FAILURE;
  13094. }
  13095. pd = sd->pd;
  13096. if (pd->a_skill == NULL)
  13097. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  13098. pd->a_skill->id = id;
  13099. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  13100. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  13101. pd->a_skill->div_ = script_getnum(st,4);
  13102. pd->a_skill->rate = script_getnum(st,5);
  13103. pd->a_skill->bonusrate = script_getnum(st,6);
  13104. return SCRIPT_CMD_SUCCESS;
  13105. }
  13106. /*==========================================
  13107. * pet support skills [Skotlex]
  13108. *------------------------------------------*/
  13109. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  13110. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  13111. BUILDIN_FUNC(petskillsupport)
  13112. {
  13113. struct pet_data *pd;
  13114. TBL_PC *sd;
  13115. int id = 0;
  13116. if(!script_rid2sd(sd) || sd->pd == NULL)
  13117. return SCRIPT_CMD_FAILURE;
  13118. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13119. if (!id) {
  13120. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  13121. return SCRIPT_CMD_FAILURE;
  13122. }
  13123. pd = sd->pd;
  13124. if (pd->s_skill)
  13125. { //Clear previous skill
  13126. if (pd->s_skill->timer != INVALID_TIMER)
  13127. {
  13128. if (pd->s_skill->id)
  13129. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  13130. else
  13131. delete_timer(pd->s_skill->timer, pet_heal_timer);
  13132. }
  13133. } else //init memory
  13134. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  13135. pd->s_skill->id = id;
  13136. pd->s_skill->lv = script_getnum(st,3);
  13137. pd->s_skill->delay = script_getnum(st,4);
  13138. pd->s_skill->hp = script_getnum(st,5);
  13139. pd->s_skill->sp = script_getnum(st,6);
  13140. //Use delay as initial offset to avoid skill/heal exploits
  13141. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  13142. pd->s_skill->timer = INVALID_TIMER;
  13143. else
  13144. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  13145. return SCRIPT_CMD_SUCCESS;
  13146. }
  13147. static inline void script_skill_effect(block_list *bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y) {
  13148. nullpo_retv(bl);
  13149. switch (skill_get_casttype(skill_id)) {
  13150. case CAST_GROUND:
  13151. clif_skill_poseffect(bl, skill_id, skill_lv, x, y, gettick());
  13152. break;
  13153. case CAST_NODAMAGE:
  13154. clif_skill_nodamage(bl, bl, skill_id, skill_lv, 1);
  13155. break;
  13156. case CAST_DAMAGE:
  13157. 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));
  13158. break;
  13159. }
  13160. }
  13161. /*==========================================
  13162. * Scripted skill effects [Celest]
  13163. *------------------------------------------*/
  13164. /// skilleffect <skill id>,<level>
  13165. /// skilleffect "<skill name>",<level>
  13166. BUILDIN_FUNC(skilleffect)
  13167. {
  13168. TBL_PC *sd;
  13169. uint16 skill_id, skill_lv;
  13170. if( !script_rid2sd(sd) )
  13171. return SCRIPT_CMD_FAILURE;
  13172. skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  13173. skill_lv = script_getnum(st,3);
  13174. if (skill_db.find(skill_id) == nullptr) {
  13175. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  13176. return SCRIPT_CMD_FAILURE;
  13177. }
  13178. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  13179. * which leaves the server thinking it still is sitting. */
  13180. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  13181. skill_sit(sd, 0);
  13182. clif_standing(&sd->bl);
  13183. }
  13184. script_skill_effect(&sd->bl, skill_id, skill_lv, sd->bl.x, sd->bl.y);
  13185. return SCRIPT_CMD_SUCCESS;
  13186. }
  13187. /*==========================================
  13188. * NPC skill effects [Valaris]
  13189. *------------------------------------------*/
  13190. /// npcskilleffect <skill id>,<level>,<x>,<y>
  13191. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  13192. BUILDIN_FUNC(npcskilleffect)
  13193. {
  13194. struct block_list *bl= map_id2bl(st->oid);
  13195. if (bl == nullptr) {
  13196. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  13197. return SCRIPT_CMD_FAILURE;
  13198. }
  13199. uint16 skill_id, skill_lv;
  13200. int16 x, y;
  13201. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  13202. skill_lv=script_getnum(st,3);
  13203. x=script_getnum(st,4);
  13204. y=script_getnum(st,5);
  13205. if (skill_db.find(skill_id) == nullptr) {
  13206. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  13207. return SCRIPT_CMD_FAILURE;
  13208. }
  13209. script_skill_effect(bl, skill_id, skill_lv, bl->x, bl->y);
  13210. return SCRIPT_CMD_SUCCESS;
  13211. }
  13212. /*==========================================
  13213. * Special effects [Valaris]
  13214. *------------------------------------------*/
  13215. BUILDIN_FUNC(specialeffect)
  13216. {
  13217. struct block_list *bl=map_id2bl(st->oid);
  13218. int type = script_getnum(st,2);
  13219. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  13220. if(bl==NULL)
  13221. return SCRIPT_CMD_SUCCESS;
  13222. if( type <= EF_NONE || type >= EF_MAX ){
  13223. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  13224. return SCRIPT_CMD_FAILURE;
  13225. }
  13226. if( script_hasdata(st,4) )
  13227. {
  13228. const char* name = script_getstr( st, 4 );
  13229. npc_data* nd = npc_name2id( name );
  13230. if(nd)
  13231. bl = &nd->bl;
  13232. else{
  13233. ShowError( "buildin_specialeffect: Unknown NPC \"%s\"\n", name );
  13234. return SCRIPT_CMD_FAILURE;
  13235. }
  13236. }
  13237. if (target == SELF) {
  13238. map_session_data* sd;
  13239. if (!script_rid2sd(sd)){
  13240. return SCRIPT_CMD_FAILURE;
  13241. }
  13242. clif_specialeffect_single(bl,type,sd->fd);
  13243. } else {
  13244. clif_specialeffect(bl, type, target);
  13245. }
  13246. return SCRIPT_CMD_SUCCESS;
  13247. }
  13248. BUILDIN_FUNC(specialeffect2)
  13249. {
  13250. TBL_PC *sd;
  13251. if( script_nick2sd(4,sd) ){
  13252. int type = script_getnum(st,2);
  13253. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  13254. if( type <= EF_NONE || type >= EF_MAX ){
  13255. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  13256. return SCRIPT_CMD_FAILURE;
  13257. }
  13258. clif_specialeffect(&sd->bl, type, target);
  13259. }
  13260. return SCRIPT_CMD_SUCCESS;
  13261. }
  13262. BUILDIN_FUNC(removespecialeffect)
  13263. {
  13264. const char* command = script_getfuncname(st);
  13265. #if PACKETVER < 20181002
  13266. ShowError("buildin_%s: This command is not supported for PACKETVER older than 2018-10-02.\n", command);
  13267. return SCRIPT_CMD_FAILURE;
  13268. #endif
  13269. int effect = script_getnum(st, 2);
  13270. if (effect <= EF_NONE || effect >= EF_MAX) {
  13271. ShowError("buildin_%s: unsupported effect id %d.\n", command, effect);
  13272. return SCRIPT_CMD_FAILURE;
  13273. }
  13274. send_target e_target = script_hasdata(st, 3) ? static_cast<send_target>(script_getnum(st, 3)) : AREA;
  13275. struct block_list *bl_src;
  13276. struct block_list *bl_target;
  13277. map_session_data *sd;
  13278. if( strcmp(command, "removespecialeffect") == 0 ) {
  13279. if (!script_hasdata(st, 4)) {
  13280. bl_src = map_id2bl(st->oid);
  13281. if (bl_src == nullptr) {
  13282. ShowError("buildin_%s: npc not attached.\n", command);
  13283. return SCRIPT_CMD_FAILURE;
  13284. }
  13285. }
  13286. else {
  13287. struct npc_data *nd = npc_name2id(script_getstr(st, 4));
  13288. if (nd == nullptr) {
  13289. ShowError("buildin_%s: unknown npc %s.\n", command, script_getstr(st, 4));
  13290. return SCRIPT_CMD_FAILURE;
  13291. }
  13292. bl_src = &nd->bl;
  13293. }
  13294. if (e_target != SELF)
  13295. bl_target = bl_src;
  13296. else {
  13297. if (!script_rid2sd(sd))
  13298. return SCRIPT_CMD_FAILURE;
  13299. bl_target = &sd->bl;
  13300. }
  13301. }else{
  13302. if (!script_nick2sd(4, sd))
  13303. return SCRIPT_CMD_FAILURE;
  13304. bl_src = bl_target = &sd->bl;
  13305. }
  13306. clif_specialeffect_remove(bl_src, effect, e_target, bl_target);
  13307. return SCRIPT_CMD_SUCCESS;
  13308. }
  13309. /**
  13310. * nude({<char_id>});
  13311. * @author [Valaris]
  13312. **/
  13313. BUILDIN_FUNC(nude)
  13314. {
  13315. TBL_PC *sd;
  13316. int i, calcflag = 0;
  13317. if (!script_charid2sd(2,sd))
  13318. return SCRIPT_CMD_FAILURE;
  13319. for( i = 0 ; i < EQI_MAX; i++ ) {
  13320. if( sd->equip_index[ i ] >= 0 ) {
  13321. if( !calcflag )
  13322. calcflag = 1;
  13323. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  13324. }
  13325. }
  13326. if( calcflag )
  13327. status_calc_pc(sd,SCO_NONE);
  13328. return SCRIPT_CMD_SUCCESS;
  13329. }
  13330. int atcommand_sub(struct script_state* st,int type) {
  13331. TBL_PC *sd, dummy_sd;
  13332. int fd;
  13333. const char *cmd;
  13334. cmd = script_getstr(st,2);
  13335. if (st->rid) {
  13336. if( !script_rid2sd(sd) )
  13337. return SCRIPT_CMD_SUCCESS;
  13338. fd = sd->fd;
  13339. } else { //Use a dummy character.
  13340. sd = &dummy_sd;
  13341. fd = 0;
  13342. memset(&dummy_sd, 0, sizeof(TBL_PC));
  13343. if (st->oid) {
  13344. struct block_list* bl = map_id2bl(st->oid);
  13345. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  13346. if (bl->type == BL_NPC)
  13347. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  13348. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : 0;
  13349. }
  13350. // Init Group ID, Level, & permissions
  13351. sd->group_id = 99;
  13352. pc_group_pc_load( sd );
  13353. }
  13354. if (!is_atcommand(fd, sd, cmd, type)) {
  13355. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  13356. script_reportsrc(st);
  13357. return SCRIPT_CMD_FAILURE;
  13358. }
  13359. return SCRIPT_CMD_SUCCESS;
  13360. }
  13361. /*==========================================
  13362. * gmcommand [MouseJstr]
  13363. *------------------------------------------*/
  13364. BUILDIN_FUNC(atcommand) {
  13365. return atcommand_sub(st,0);
  13366. }
  13367. /** Displays a message for the player only (like system messages like "you got an apple" )
  13368. * dispbottom("<message>"{,<color>{,<char_id>}})
  13369. * @param message
  13370. * @param color Hex color default (Green)
  13371. */
  13372. BUILDIN_FUNC(dispbottom)
  13373. {
  13374. TBL_PC *sd;
  13375. int color = 0;
  13376. const char *message;
  13377. if (!script_charid2sd(4,sd))
  13378. return SCRIPT_CMD_FAILURE;
  13379. message = script_getstr(st,2);
  13380. if (script_hasdata(st,3))
  13381. color = script_getnum(st,3); // <color>
  13382. if(sd) {
  13383. if (script_hasdata(st,3))
  13384. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  13385. else
  13386. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  13387. }
  13388. return SCRIPT_CMD_SUCCESS;
  13389. }
  13390. /*===================================
  13391. * Heal portion of recovery command
  13392. *-----------------------------------*/
  13393. int recovery_sub(map_session_data* sd, int revive)
  13394. {
  13395. if(revive&(1|4) && pc_isdead(sd)) {
  13396. status_revive(&sd->bl, 100, 100);
  13397. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  13398. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  13399. } else if(revive&(1|2) && !pc_isdead(sd)) {
  13400. status_percent_heal(&sd->bl, 100, 100);
  13401. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  13402. }
  13403. return SCRIPT_CMD_SUCCESS;
  13404. }
  13405. /*=========================================================================
  13406. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  13407. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  13408. * <type> determines <option>:
  13409. * 0 : char_id
  13410. * 1 : party_id
  13411. * 2 : guild_id
  13412. * 3 : map_name
  13413. * 4 : all characters
  13414. * <revive_flag>:
  13415. * 1 : Revive and heal all players (default)
  13416. * 2 : Heal living players only
  13417. * 4 : Revive dead players only
  13418. * <map name>:
  13419. * for types 1-2 : map_name (null = all maps)
  13420. *-------------------------------------------------------------------------*/
  13421. BUILDIN_FUNC(recovery)
  13422. {
  13423. TBL_PC *sd;
  13424. int map_idx = 0, type = 0, revive = 1;
  13425. type = script_getnum(st,2);
  13426. if(script_hasdata(st,4))
  13427. revive = script_getnum(st,4);
  13428. switch(type) {
  13429. case 0:
  13430. if(!script_charid2sd(3,sd))
  13431. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  13432. recovery_sub(sd, revive);
  13433. break;
  13434. case 1:
  13435. {
  13436. struct party_data* p;
  13437. //When no party given, we use invoker party
  13438. int p_id = 0, i;
  13439. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13440. map_idx = map_mapname2mapid(script_getstr(st,5));
  13441. if(map_idx < 1) { //But we'll check anyways
  13442. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13443. return SCRIPT_CMD_FAILURE;
  13444. }
  13445. }
  13446. if(script_hasdata(st,3))
  13447. p_id = script_getnum(st,3);
  13448. else if(script_rid2sd(sd))
  13449. p_id = sd->status.party_id;
  13450. p = party_search(p_id);
  13451. if(p == NULL)
  13452. return SCRIPT_CMD_SUCCESS;
  13453. for (i = 0; i < MAX_PARTY; i++) {
  13454. map_session_data* pl_sd;
  13455. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  13456. || (map_idx && pl_sd->bl.m != map_idx))
  13457. continue;
  13458. recovery_sub(pl_sd, revive);
  13459. }
  13460. break;
  13461. }
  13462. case 2:
  13463. {
  13464. //When no guild given, we use invoker guild
  13465. int g_id = 0, i;
  13466. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13467. map_idx = map_mapname2mapid(script_getstr(st,5));
  13468. if(map_idx < 1) { //But we'll check anyways
  13469. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13470. return SCRIPT_CMD_FAILURE;
  13471. }
  13472. }
  13473. if(script_hasdata(st,3))
  13474. g_id = script_getnum(st,3);
  13475. else if(script_rid2sd(sd))
  13476. g_id = sd->status.guild_id;
  13477. auto g = guild_search(g_id);
  13478. if(g == NULL)
  13479. return SCRIPT_CMD_SUCCESS;
  13480. for (i = 0; i < MAX_GUILD; i++) {
  13481. map_session_data* pl_sd;
  13482. if((!(pl_sd = g->guild.member[i].sd) || pl_sd->status.guild_id != g_id)
  13483. || (map_idx && pl_sd->bl.m != map_idx))
  13484. continue;
  13485. recovery_sub(pl_sd, revive);
  13486. }
  13487. break;
  13488. }
  13489. case 3:
  13490. if(script_hasdata(st,3))
  13491. map_idx = map_mapname2mapid(script_getstr(st,3));
  13492. else if(script_rid2sd(sd))
  13493. map_idx = sd->bl.m;
  13494. if(map_idx < 1)
  13495. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  13496. case 4:
  13497. {
  13498. struct s_mapiterator *iter;
  13499. if(script_hasdata(st,3) && script_isint(st, 3))
  13500. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  13501. iter = mapit_getallusers();
  13502. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  13503. if(type == 3 && sd->bl.m != map_idx)
  13504. continue;
  13505. recovery_sub(sd, revive);
  13506. }
  13507. mapit_free(iter);
  13508. break;
  13509. }
  13510. default:
  13511. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  13512. script_pushint(st,-1);
  13513. return SCRIPT_CMD_FAILURE;
  13514. }
  13515. script_pushint(st,1); //Successfully executed without errors
  13516. return SCRIPT_CMD_SUCCESS;
  13517. }
  13518. /*==========================================
  13519. * Get your pet info: getpetinfo <type>{,<char_id>}
  13520. * n -> 0:pet_id 1:pet_class 2:pet_name
  13521. * 3:friendly 4:hungry, 5: rename flag.6:level,
  13522. * 7:game id
  13523. *------------------------------------------*/
  13524. BUILDIN_FUNC(getpetinfo)
  13525. {
  13526. TBL_PC *sd;
  13527. TBL_PET *pd;
  13528. int type = script_getnum(st,2);
  13529. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  13530. if (type == 2)
  13531. script_pushconststr(st,"null");
  13532. else
  13533. script_pushint(st,0);
  13534. return SCRIPT_CMD_SUCCESS;
  13535. }
  13536. switch(type){
  13537. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13538. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13539. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13540. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13541. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13542. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13543. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13544. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13545. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13546. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13547. default:
  13548. script_pushint(st,0);
  13549. break;
  13550. }
  13551. return SCRIPT_CMD_SUCCESS;
  13552. }
  13553. /*==========================================
  13554. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13555. * type -> 0:hom_id 1:class 2:name
  13556. * 3:friendly 4:hungry, 5: rename flag.
  13557. * 6: level, 7: game id
  13558. *------------------------------------------*/
  13559. BUILDIN_FUNC(gethominfo)
  13560. {
  13561. TBL_PC *sd;
  13562. TBL_HOM *hd;
  13563. int type=script_getnum(st,2);
  13564. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13565. if (type == 2)
  13566. script_pushconststr(st,"null");
  13567. else
  13568. script_pushint(st,0);
  13569. return SCRIPT_CMD_SUCCESS;
  13570. }
  13571. switch(type){
  13572. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13573. case 1: script_pushint(st,hd->homunculus.class_); break;
  13574. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13575. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13576. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13577. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13578. case 6: script_pushint(st,hd->homunculus.level); break;
  13579. case 7: script_pushint(st,hd->bl.id); break;
  13580. default:
  13581. script_pushint(st,0);
  13582. break;
  13583. }
  13584. return SCRIPT_CMD_SUCCESS;
  13585. }
  13586. BUILDIN_FUNC(addhomintimacy)
  13587. {
  13588. map_session_data *sd;
  13589. homun_data *hd;
  13590. if (!script_charid2sd(3, sd) || !(hd = sd->hd))
  13591. return SCRIPT_CMD_FAILURE;
  13592. int32 value = script_getnum(st, 2);
  13593. if (value == 0) // Nothing to change
  13594. return SCRIPT_CMD_SUCCESS;
  13595. if (value > 0)
  13596. hom_increase_intimacy(hd, (uint32)value);
  13597. else
  13598. hom_decrease_intimacy(hd, (uint32)abs(value));
  13599. clif_send_homdata(sd, SP_INTIMATE, hd->homunculus.intimacy / 100);
  13600. return SCRIPT_CMD_SUCCESS;
  13601. }
  13602. /// Retrieves information about character's mercenary
  13603. /// getmercinfo <type>[,<char id>];
  13604. BUILDIN_FUNC(getmercinfo)
  13605. {
  13606. int type;
  13607. map_session_data* sd;
  13608. s_mercenary_data* md;
  13609. if( !script_charid2sd(3,sd) ){
  13610. script_pushnil(st);
  13611. return SCRIPT_CMD_FAILURE;
  13612. }
  13613. type = script_getnum(st,2);
  13614. md = sd->md;
  13615. if( md == NULL ){
  13616. if( type == 2 )
  13617. script_pushconststr(st,"");
  13618. else
  13619. script_pushint(st,0);
  13620. return SCRIPT_CMD_SUCCESS;
  13621. }
  13622. switch( type )
  13623. {
  13624. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13625. case 1: script_pushint(st,md->mercenary.class_); break;
  13626. case 2: script_pushstrcopy(st,md->db->name.c_str()); break;
  13627. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13628. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13629. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13630. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13631. case 7: script_pushint(st,md->db->lv); break;
  13632. case 8: script_pushint(st,md->bl.id); break;
  13633. default:
  13634. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13635. script_pushnil(st);
  13636. return SCRIPT_CMD_FAILURE;
  13637. }
  13638. return SCRIPT_CMD_SUCCESS;
  13639. }
  13640. /*==========================================
  13641. * Shows wether your inventory(and equips) contain
  13642. selected card or not.
  13643. checkequipedcard(4001);
  13644. *------------------------------------------*/
  13645. BUILDIN_FUNC(checkequipedcard)
  13646. {
  13647. TBL_PC *sd;
  13648. if(script_rid2sd(sd)){
  13649. int n,i,c=0;
  13650. c=script_getnum(st,2);
  13651. for(i=0;i<MAX_INVENTORY;i++){
  13652. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13653. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13654. continue;
  13655. for (n=0; n < MAX_SLOTS; n++) {
  13656. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13657. script_pushint(st,1);
  13658. return SCRIPT_CMD_SUCCESS;
  13659. }
  13660. }
  13661. }
  13662. }
  13663. }
  13664. script_pushint(st,0);
  13665. return SCRIPT_CMD_SUCCESS;
  13666. }
  13667. BUILDIN_FUNC(jump_zero)
  13668. {
  13669. int64 sel=script_getnum64(st,2);
  13670. if(!sel) {
  13671. int pos;
  13672. if( !data_islabel(script_getdata(st,3)) ){
  13673. ShowError("script: jump_zero: not label !\n");
  13674. st->state=END;
  13675. return SCRIPT_CMD_FAILURE;
  13676. }
  13677. pos=script_getnum(st,3);
  13678. st->pos=pos;
  13679. st->state=GOTO;
  13680. }
  13681. return SCRIPT_CMD_SUCCESS;
  13682. }
  13683. /*==========================================
  13684. * movenpc [MouseJstr]
  13685. *------------------------------------------*/
  13686. BUILDIN_FUNC(movenpc)
  13687. {
  13688. TBL_NPC *nd = NULL;
  13689. const char *npc;
  13690. int x,y;
  13691. npc = script_getstr(st,2);
  13692. x = script_getnum(st,3);
  13693. y = script_getnum(st,4);
  13694. if ((nd = npc_name2id(npc)) == NULL){
  13695. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13696. return -1;
  13697. }
  13698. if (script_hasdata(st,5))
  13699. nd->ud.dir = script_getnum(st,5) % 8;
  13700. npc_movenpc(nd, x, y);
  13701. return SCRIPT_CMD_SUCCESS;
  13702. }
  13703. /*==========================================
  13704. * message [MouseJstr]
  13705. *------------------------------------------*/
  13706. BUILDIN_FUNC(message)
  13707. {
  13708. const char *msg,*player;
  13709. TBL_PC *pl_sd = NULL;
  13710. player = script_getstr(st,2);
  13711. msg = script_getstr(st,3);
  13712. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13713. return SCRIPT_CMD_SUCCESS;
  13714. clif_displaymessage(pl_sd->fd, msg);
  13715. return SCRIPT_CMD_SUCCESS;
  13716. }
  13717. /**
  13718. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13719. * @param flag: Specify target
  13720. * BC_ALL - Broadcast message is sent server-wide.
  13721. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13722. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13723. * BC_SELF - Message is sent only to player attached.
  13724. */
  13725. BUILDIN_FUNC(npctalk)
  13726. {
  13727. struct npc_data* nd = NULL;
  13728. const char* str = script_getstr(st,2);
  13729. int color = 0xFFFFFF;
  13730. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13731. nd = npc_name2id(script_getstr(st, 3));
  13732. else
  13733. nd = (struct npc_data *)map_id2bl(st->oid);
  13734. if (script_hasdata(st, 5))
  13735. color = script_getnum(st, 5);
  13736. if (nd != NULL) {
  13737. send_target target = AREA;
  13738. char message[CHAT_SIZE_MAX];
  13739. if (script_hasdata(st, 4)) {
  13740. switch(script_getnum(st, 4)) {
  13741. case BC_ALL: target = ALL_CLIENT; break;
  13742. case BC_MAP: target = ALL_SAMEMAP; break;
  13743. case BC_SELF: target = SELF; break;
  13744. case BC_AREA:
  13745. default: target = AREA; break;
  13746. }
  13747. }
  13748. safesnprintf(message, sizeof(message), "%s", str);
  13749. if (target != SELF)
  13750. clif_messagecolor(&nd->bl, color, message, true, target);
  13751. else {
  13752. TBL_PC *sd = map_id2sd(st->rid);
  13753. if (sd == NULL)
  13754. return SCRIPT_CMD_FAILURE;
  13755. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13756. }
  13757. }
  13758. return SCRIPT_CMD_SUCCESS;
  13759. }
  13760. /**
  13761. * Sends a message to the waitingroom of the invoking NPC.
  13762. * chatmes "<message>"{,"<NPC name>"};
  13763. * @author Jey
  13764. */
  13765. BUILDIN_FUNC(chatmes)
  13766. {
  13767. struct npc_data* nd = NULL;
  13768. const char* str = script_getstr(st,2);
  13769. if (script_hasdata(st, 3))
  13770. nd = npc_name2id(script_getstr(st, 3));
  13771. else
  13772. nd = (struct npc_data *)map_id2bl(st->oid);
  13773. if (nd != NULL && nd->chat_id) {
  13774. char message[256];
  13775. safesnprintf(message, sizeof(message), "%s", str);
  13776. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13777. }
  13778. return SCRIPT_CMD_SUCCESS;
  13779. }
  13780. // change npc walkspeed [Valaris]
  13781. BUILDIN_FUNC(npcspeed)
  13782. {
  13783. struct npc_data* nd;
  13784. int speed;
  13785. speed = script_getnum(st,2);
  13786. nd =(struct npc_data *)map_id2bl(st->oid);
  13787. if( nd ) {
  13788. nd->speed = speed;
  13789. nd->ud.state.speed_changed = 1;
  13790. }
  13791. return SCRIPT_CMD_SUCCESS;
  13792. }
  13793. // make an npc walk to a position [Valaris]
  13794. BUILDIN_FUNC(npcwalkto)
  13795. {
  13796. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13797. int x=0,y=0;
  13798. x=script_getnum(st,2);
  13799. y=script_getnum(st,3);
  13800. if(nd) {
  13801. if (!nd->status.hp)
  13802. status_calc_npc(nd, SCO_FIRST);
  13803. else
  13804. status_calc_npc(nd, SCO_NONE);
  13805. unit_walktoxy(&nd->bl,x,y,0);
  13806. }
  13807. return SCRIPT_CMD_SUCCESS;
  13808. }
  13809. // stop an npc's movement [Valaris]
  13810. BUILDIN_FUNC(npcstop)
  13811. {
  13812. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13813. if(nd) {
  13814. unit_stop_walking(&nd->bl,1|4);
  13815. }
  13816. return SCRIPT_CMD_SUCCESS;
  13817. }
  13818. /**
  13819. * getlook(<type>{,<char_id>})
  13820. **/
  13821. BUILDIN_FUNC(getlook)
  13822. {
  13823. int type,val;
  13824. TBL_PC *sd;
  13825. if (!script_charid2sd(3,sd)) {
  13826. script_pushint(st,-1);
  13827. return SCRIPT_CMD_FAILURE;
  13828. }
  13829. type=script_getnum(st,2);
  13830. val=-1;
  13831. switch(type) {
  13832. // TODO: implement LOOK_BASE as stated in script doc
  13833. case LOOK_HAIR: val=sd->status.hair; break; //1
  13834. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13835. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13836. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13837. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13838. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13839. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13840. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13841. case LOOK_SHOES: break; //9
  13842. case LOOK_ROBE: val=sd->status.robe; break; //12
  13843. case LOOK_BODY2: val=sd->status.body; break; //13
  13844. }
  13845. script_pushint(st,val);
  13846. return SCRIPT_CMD_SUCCESS;
  13847. }
  13848. /**
  13849. * getsavepoint(<information type>{,<char_id>})
  13850. * @param type 0- map name, 1- x, 2- y
  13851. **/
  13852. BUILDIN_FUNC(getsavepoint)
  13853. {
  13854. TBL_PC* sd;
  13855. int type;
  13856. if (!script_charid2sd(3,sd)) {
  13857. script_pushint(st,0);
  13858. return SCRIPT_CMD_SUCCESS;
  13859. }
  13860. type = script_getnum(st,2);
  13861. switch(type) {
  13862. case 0: script_pushstrcopy( st, sd->status.save_point.map ); break;
  13863. case 1: script_pushint(st,sd->status.save_point.x); break;
  13864. case 2: script_pushint(st,sd->status.save_point.y); break;
  13865. default:
  13866. script_pushint(st,0);
  13867. break;
  13868. }
  13869. return SCRIPT_CMD_SUCCESS;
  13870. }
  13871. /**
  13872. * Get position for BL objects.
  13873. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13874. * @param mapname: String variable for output map name
  13875. * @param x: Integer variable for output coord X
  13876. * @param y: Integer variable for output coord Y
  13877. * @param type: Type of object
  13878. * BL_PC - Character coord
  13879. * BL_NPC - NPC coord
  13880. * BL_PET - Pet coord
  13881. * BL_HOM - Homun coord
  13882. * BL_MER - Mercenary coord
  13883. * BL_ELEM - Elemental coord
  13884. * @param charname: Name object. If empty or "this" use the current object
  13885. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13886. */
  13887. BUILDIN_FUNC(getmapxy)
  13888. {
  13889. struct block_list *bl = NULL;
  13890. TBL_PC *sd=NULL;
  13891. int64 num;
  13892. const char *name;
  13893. char prefix;
  13894. int x,y,type = BL_PC;
  13895. char mapname[MAP_NAME_LENGTH];
  13896. if( !data_isreference(script_getdata(st,2)) ) {
  13897. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13898. script_pushint(st,-1);
  13899. return SCRIPT_CMD_FAILURE;
  13900. }
  13901. if( !data_isreference(script_getdata(st,3)) ) {
  13902. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13903. script_pushint(st,-1);
  13904. return SCRIPT_CMD_FAILURE;
  13905. }
  13906. if( !data_isreference(script_getdata(st,4)) ) {
  13907. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13908. script_pushint(st,-1);
  13909. return SCRIPT_CMD_FAILURE;
  13910. }
  13911. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13912. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13913. script_pushint(st,-1);
  13914. return SCRIPT_CMD_FAILURE;
  13915. }
  13916. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13917. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13918. script_pushint(st,-1);
  13919. return SCRIPT_CMD_FAILURE;
  13920. }
  13921. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13922. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13923. script_pushint(st,-1);
  13924. return SCRIPT_CMD_FAILURE;
  13925. }
  13926. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13927. if (script_hasdata(st, 5))
  13928. type = script_getnum(st, 5);
  13929. switch (type) {
  13930. case BL_PC: //Get Character Position
  13931. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  13932. bl = &sd->bl;
  13933. break;
  13934. case BL_NPC: //Get NPC Position
  13935. if (script_hasdata(st, 6)) {
  13936. struct npc_data *nd;
  13937. if (script_isstring(st, 6))
  13938. nd = npc_name2id(script_getstr(st, 6));
  13939. else
  13940. nd = map_id2nd(script_getnum(st, 6));
  13941. if (nd)
  13942. bl = &nd->bl;
  13943. } else //In case the origin is not an NPC?
  13944. bl = map_id2bl(st->oid);
  13945. break;
  13946. case BL_PET: //Get Pet Position
  13947. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->pd)
  13948. bl = &sd->pd->bl;
  13949. break;
  13950. case BL_HOM: //Get Homun Position
  13951. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->hd)
  13952. bl = &sd->hd->bl;
  13953. break;
  13954. case BL_MER: //Get Mercenary Position
  13955. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->md)
  13956. bl = &sd->md->bl;
  13957. break;
  13958. case BL_ELEM: //Get Elemental Position
  13959. if (((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)) && sd->ed)
  13960. bl = &sd->ed->bl;
  13961. break;
  13962. default:
  13963. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13964. script_pushint(st,-1);
  13965. return SCRIPT_CMD_FAILURE;
  13966. }
  13967. if (!bl) { //No object found.
  13968. script_pushint(st,-1);
  13969. return SCRIPT_CMD_SUCCESS;
  13970. }
  13971. x= bl->x;
  13972. y= bl->y;
  13973. if (bl->m >= 0)
  13974. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  13975. else
  13976. memset(mapname, '\0', sizeof(mapname));
  13977. //Set MapName$
  13978. num=st->stack->stack_data[st->start+2].u.num;
  13979. name=get_str(script_getvarid(num));
  13980. prefix=*name;
  13981. if(not_server_variable(prefix)){
  13982. if( !script_rid2sd(sd) ){
  13983. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13984. return SCRIPT_CMD_FAILURE;
  13985. }
  13986. }else
  13987. sd=NULL;
  13988. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  13989. //Set MapX
  13990. num=st->stack->stack_data[st->start+3].u.num;
  13991. name=get_str(script_getvarid(num));
  13992. prefix=*name;
  13993. if(not_server_variable(prefix)){
  13994. if( !script_rid2sd(sd) ){
  13995. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13996. return SCRIPT_CMD_FAILURE;
  13997. }
  13998. }else
  13999. sd=NULL;
  14000. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  14001. //Set MapY
  14002. num=st->stack->stack_data[st->start+4].u.num;
  14003. name=get_str(script_getvarid(num));
  14004. prefix=*name;
  14005. if(not_server_variable(prefix)){
  14006. if( !script_rid2sd(sd) ){
  14007. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  14008. return SCRIPT_CMD_FAILURE;
  14009. }
  14010. }else
  14011. sd=NULL;
  14012. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  14013. //Return Success value
  14014. script_pushint(st,0);
  14015. return SCRIPT_CMD_SUCCESS;
  14016. }
  14017. /// Returns the map name of given map ID.
  14018. ///
  14019. /// mapid2name <map ID>;
  14020. BUILDIN_FUNC(mapid2name)
  14021. {
  14022. uint16 m = script_getnum(st, 2);
  14023. if (m >= MAX_MAP_PER_SERVER) {
  14024. script_pushconststr(st, "");
  14025. return SCRIPT_CMD_FAILURE;
  14026. }
  14027. script_pushstrcopy(st, map_mapid2mapname(m));
  14028. return SCRIPT_CMD_SUCCESS;
  14029. }
  14030. /*==========================================
  14031. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  14032. *------------------------------------------*/
  14033. BUILDIN_FUNC(logmes)
  14034. {
  14035. const char *str;
  14036. map_session_data* sd = map_id2sd(st->rid);
  14037. str = script_getstr(st,2);
  14038. if( sd ){
  14039. log_npc(sd,str);
  14040. }else{
  14041. struct npc_data* nd = map_id2nd(st->oid);
  14042. if( !nd ){
  14043. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  14044. return SCRIPT_CMD_FAILURE;
  14045. }
  14046. log_npc(nd,str);
  14047. }
  14048. return SCRIPT_CMD_SUCCESS;
  14049. }
  14050. BUILDIN_FUNC(summon)
  14051. {
  14052. int _class, timeout=0;
  14053. const char *str,*event="";
  14054. TBL_PC *sd;
  14055. struct mob_data *md;
  14056. t_tick tick = gettick();
  14057. if (!script_rid2sd(sd))
  14058. return SCRIPT_CMD_SUCCESS;
  14059. str =script_getstr(st,2);
  14060. _class=script_getnum(st,3);
  14061. if( script_hasdata(st,4) )
  14062. timeout=script_getnum(st,4);
  14063. if( script_hasdata(st,5) ){
  14064. event=script_getstr(st,5);
  14065. check_event(st, event);
  14066. }
  14067. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  14068. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  14069. if (md) {
  14070. md->master_id=sd->bl.id;
  14071. md->special_state.ai = AI_ATTACK;
  14072. if( md->deletetimer != INVALID_TIMER )
  14073. delete_timer(md->deletetimer, mob_timer_delete);
  14074. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  14075. mob_spawn (md); //Now it is ready for spawning.
  14076. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  14077. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  14078. }
  14079. script_pushint(st, md->bl.id);
  14080. return SCRIPT_CMD_SUCCESS;
  14081. }
  14082. /*==========================================
  14083. * Checks whether it is daytime/nighttime
  14084. *------------------------------------------*/
  14085. BUILDIN_FUNC(isnight)
  14086. {
  14087. script_pushint(st,(night_flag == 1));
  14088. return SCRIPT_CMD_SUCCESS;
  14089. }
  14090. BUILDIN_FUNC(isday)
  14091. {
  14092. script_pushint(st,(night_flag == 0));
  14093. return SCRIPT_CMD_SUCCESS;
  14094. }
  14095. /*================================================
  14096. * Check how many items/cards in the list are
  14097. * equipped - used for 2/15's cards patch [celest]
  14098. *------------------------------------------------*/
  14099. BUILDIN_FUNC(isequippedcnt)
  14100. {
  14101. TBL_PC *sd;
  14102. if (!script_rid2sd(sd)) {
  14103. script_pushint(st,0);
  14104. return SCRIPT_CMD_SUCCESS;
  14105. }
  14106. int ret = 0;
  14107. int total = script_lastdata(st);
  14108. std::vector<int32> list(total);
  14109. for (int i = 2; i <= total; ++i) {
  14110. int id = script_getnum(st,i);
  14111. if (id <= 0)
  14112. continue;
  14113. if (std::find(list.begin(), list.end(), id) != list.end())
  14114. continue;
  14115. list.push_back(id);
  14116. for (short j = 0; j < EQI_MAX; j++) {
  14117. short index = sd->equip_index[j];
  14118. if (index < 0)
  14119. continue;
  14120. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  14121. continue;
  14122. if (!sd->inventory_data[index])
  14123. continue;
  14124. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  14125. if (sd->inventory_data[index]->nameid == id)
  14126. ret += sd->inventory.u.items_inventory[index].amount;
  14127. } else { //Count cards.
  14128. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14129. continue; //No cards
  14130. for (short k = 0; k < MAX_SLOTS; k++) {
  14131. if (sd->inventory.u.items_inventory[index].card[k] == id)
  14132. ret++; //[Lupus]
  14133. }
  14134. }
  14135. }
  14136. }
  14137. script_pushint(st,ret);
  14138. return SCRIPT_CMD_SUCCESS;
  14139. }
  14140. /*================================================
  14141. * Check whether another card has been
  14142. * equipped - used for 2/15's cards patch [celest]
  14143. * -- Items checked cannot be reused in another
  14144. * card set to prevent exploits
  14145. *------------------------------------------------*/
  14146. BUILDIN_FUNC(isequipped)
  14147. {
  14148. TBL_PC *sd;
  14149. int i, id = 1;
  14150. int ret = -1;
  14151. //Original hash to reverse it when full check fails.
  14152. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  14153. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  14154. script_pushint(st,0);
  14155. return SCRIPT_CMD_SUCCESS;
  14156. }
  14157. setitem_hash = sd->bonus.setitem_hash;
  14158. setitem_hash2 = sd->bonus.setitem_hash2;
  14159. for (i=0; id!=0; i++) {
  14160. int flag = 0;
  14161. short j;
  14162. FETCH (i+2, id) else id = 0;
  14163. if (id <= 0)
  14164. continue;
  14165. for (j=0; j<EQI_MAX; j++) {
  14166. short index = sd->equip_index[j];
  14167. if(index < 0)
  14168. continue;
  14169. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  14170. continue;
  14171. if(!sd->inventory_data[index])
  14172. continue;
  14173. if (itemdb_type(id) != IT_CARD) {
  14174. if (sd->inventory_data[index]->nameid != id)
  14175. continue;
  14176. flag = 1;
  14177. break;
  14178. } else { //Cards
  14179. short k;
  14180. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14181. continue;
  14182. for (k = 0; k < MAX_SLOTS; k++)
  14183. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  14184. unsigned int hash = 0;
  14185. if (sd->inventory.u.items_inventory[index].card[k] != id)
  14186. continue;
  14187. hash = 1<<((j<5?j:j-5)*4 + k);
  14188. // check if card is already used by another set
  14189. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  14190. continue;
  14191. // We have found a match
  14192. flag = 1;
  14193. // Set hash so this card cannot be used by another
  14194. if (j<5)
  14195. sd->bonus.setitem_hash |= hash;
  14196. else
  14197. sd->bonus.setitem_hash2 |= hash;
  14198. break;
  14199. }
  14200. }
  14201. if (flag) break; //Card found
  14202. }
  14203. if (ret == -1)
  14204. ret = flag;
  14205. else
  14206. ret &= flag;
  14207. if (!ret) break;
  14208. }
  14209. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  14210. sd->bonus.setitem_hash = setitem_hash;
  14211. sd->bonus.setitem_hash2 = setitem_hash2;
  14212. }
  14213. script_pushint(st,ret);
  14214. return SCRIPT_CMD_SUCCESS;
  14215. }
  14216. /*================================================
  14217. * Check how many given inserted cards in the CURRENT
  14218. * weapon - used for 2/15's cards patch [Lupus]
  14219. *------------------------------------------------*/
  14220. BUILDIN_FUNC(cardscnt)
  14221. {
  14222. TBL_PC *sd;
  14223. int i, k, id = 1;
  14224. int ret = 0;
  14225. int index;
  14226. if( !script_rid2sd(sd) ){
  14227. script_pushint(st,0);
  14228. return SCRIPT_CMD_SUCCESS;
  14229. }
  14230. for (i=0; id!=0; i++) {
  14231. FETCH (i+2, id) else id = 0;
  14232. if (id <= 0)
  14233. continue;
  14234. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  14235. if(index < 0) continue;
  14236. if(!sd->inventory_data[index])
  14237. continue;
  14238. if(itemdb_type(id) != IT_CARD) {
  14239. if (sd->inventory_data[index]->nameid == id)
  14240. ret+= sd->inventory.u.items_inventory[index].amount;
  14241. } else {
  14242. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14243. continue;
  14244. for (k=0; k < MAX_SLOTS; k++) {
  14245. if (sd->inventory.u.items_inventory[index].card[k] == id)
  14246. ret++;
  14247. }
  14248. }
  14249. }
  14250. script_pushint(st,ret);
  14251. // script_pushint(st,current_equip_item_index);
  14252. return SCRIPT_CMD_SUCCESS;
  14253. }
  14254. /*=======================================================
  14255. * Returns the refined number of the current item, or an
  14256. * item with inventory index specified
  14257. *-------------------------------------------------------*/
  14258. BUILDIN_FUNC(getrefine)
  14259. {
  14260. TBL_PC *sd;
  14261. if (script_rid2sd(sd)){
  14262. if( current_equip_item_index == -1 ){
  14263. script_pushint(st, 0);
  14264. return SCRIPT_CMD_FAILURE;
  14265. }
  14266. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  14267. }else
  14268. script_pushint(st,0);
  14269. return SCRIPT_CMD_SUCCESS;
  14270. }
  14271. /*=======================================================
  14272. * Day/Night controls
  14273. *-------------------------------------------------------*/
  14274. BUILDIN_FUNC(night)
  14275. {
  14276. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  14277. return SCRIPT_CMD_SUCCESS;
  14278. }
  14279. BUILDIN_FUNC(day)
  14280. {
  14281. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  14282. return SCRIPT_CMD_SUCCESS;
  14283. }
  14284. /**
  14285. * unequip <equipment slot>{,<char_id>};
  14286. * @author [Spectre]
  14287. **/
  14288. BUILDIN_FUNC(unequip) {
  14289. int pos;
  14290. TBL_PC *sd;
  14291. if (!script_charid2sd(3,sd))
  14292. return SCRIPT_CMD_FAILURE;
  14293. pos = script_getnum(st,2);
  14294. if (equip_index_check(pos)) {
  14295. short i = pc_checkequip(sd,equip_bitmask[pos]);
  14296. if (i >= 0) {
  14297. pc_unequipitem(sd,i,1|2);
  14298. script_pushint(st, 1);
  14299. return SCRIPT_CMD_SUCCESS;
  14300. }
  14301. }
  14302. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  14303. script_pushint(st, 0);
  14304. return SCRIPT_CMD_FAILURE;
  14305. }
  14306. /**
  14307. * equip <item id>{,<char_id>};
  14308. **/
  14309. BUILDIN_FUNC(equip) {
  14310. TBL_PC *sd;
  14311. if (!script_charid2sd(3,sd))
  14312. return SCRIPT_CMD_FAILURE;
  14313. t_itemid nameid = script_getnum(st,2);
  14314. std::shared_ptr<item_data> id = item_db.find(nameid);
  14315. if (id != nullptr) {
  14316. int i;
  14317. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  14318. if (i < MAX_INVENTORY) {
  14319. pc_equipitem(sd,i,id->equip);
  14320. script_pushint(st,1);
  14321. return SCRIPT_CMD_SUCCESS;
  14322. }
  14323. }
  14324. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  14325. script_pushint(st,0);
  14326. return SCRIPT_CMD_FAILURE;
  14327. }
  14328. BUILDIN_FUNC(autoequip)
  14329. {
  14330. t_itemid nameid=script_getnum(st,2);
  14331. int flag=script_getnum(st,3);
  14332. std::shared_ptr<item_data> id = item_db.find(nameid);
  14333. if( id == nullptr )
  14334. {
  14335. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  14336. return SCRIPT_CMD_FAILURE;
  14337. }
  14338. if( !itemdb_isequip2(id.get()) )
  14339. {
  14340. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  14341. return SCRIPT_CMD_FAILURE;
  14342. }
  14343. id->flag.autoequip = flag>0?1:0;
  14344. return SCRIPT_CMD_SUCCESS;
  14345. }
  14346. /**
  14347. * Set the value of a given battle config
  14348. * setbattleflag "<battle flag>",<value>{,<reload>};
  14349. */
  14350. BUILDIN_FUNC(setbattleflag)
  14351. {
  14352. const char *flag, *value;
  14353. flag = script_getstr(st,2);
  14354. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  14355. if (battle_set_value(flag, value) == 0)
  14356. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  14357. else {
  14358. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  14359. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  14360. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  14361. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  14362. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  14363. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  14364. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  14365. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  14366. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  14367. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  14368. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  14369. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  14370. };
  14371. uint8 i;
  14372. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  14373. if (!strcmpi(flag, expdrop_flags[i])) {
  14374. mob_reload();
  14375. break;
  14376. }
  14377. }
  14378. }
  14379. }
  14380. return SCRIPT_CMD_SUCCESS;
  14381. }
  14382. /**
  14383. * Get the value of a given battle config
  14384. * getbattleflag("<battle flag>")
  14385. */
  14386. BUILDIN_FUNC(getbattleflag)
  14387. {
  14388. const char *flag = script_getstr(st,2);
  14389. script_pushint(st,battle_get_value(flag));
  14390. return SCRIPT_CMD_SUCCESS;
  14391. }
  14392. //=======================================================
  14393. // strlen [Valaris]
  14394. //-------------------------------------------------------
  14395. BUILDIN_FUNC(getstrlen)
  14396. {
  14397. const char *str = script_getstr(st,2);
  14398. int len = (str) ? (int)strlen(str) : 0;
  14399. script_pushint(st,len);
  14400. return SCRIPT_CMD_SUCCESS;
  14401. }
  14402. //=======================================================
  14403. // isalpha [Valaris]
  14404. //-------------------------------------------------------
  14405. BUILDIN_FUNC(charisalpha)
  14406. {
  14407. const char *str=script_getstr(st,2);
  14408. int pos=script_getnum(st,3);
  14409. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  14410. script_pushint(st,val);
  14411. return SCRIPT_CMD_SUCCESS;
  14412. }
  14413. //=======================================================
  14414. // charisupper <str>, <index>
  14415. //-------------------------------------------------------
  14416. BUILDIN_FUNC(charisupper)
  14417. {
  14418. const char *str = script_getstr(st,2);
  14419. int pos = script_getnum(st,3);
  14420. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  14421. script_pushint(st,val);
  14422. return SCRIPT_CMD_SUCCESS;
  14423. }
  14424. //=======================================================
  14425. // charislower <str>, <index>
  14426. //-------------------------------------------------------
  14427. BUILDIN_FUNC(charislower)
  14428. {
  14429. const char *str = script_getstr(st,2);
  14430. int pos = script_getnum(st,3);
  14431. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  14432. script_pushint(st,val);
  14433. return SCRIPT_CMD_SUCCESS;
  14434. }
  14435. //=======================================================
  14436. // charat <str>, <index>
  14437. //-------------------------------------------------------
  14438. BUILDIN_FUNC(charat) {
  14439. const char *str = script_getstr(st,2);
  14440. int pos = script_getnum(st,3);
  14441. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  14442. char output[2];
  14443. output[0] = str[pos];
  14444. output[1] = '\0';
  14445. script_pushstrcopy(st, output);
  14446. } else
  14447. script_pushconststr(st, "");
  14448. return SCRIPT_CMD_SUCCESS;
  14449. }
  14450. //=======================================================
  14451. // setchar <string>, <char>, <index>
  14452. //-------------------------------------------------------
  14453. BUILDIN_FUNC(setchar)
  14454. {
  14455. const char *str = script_getstr(st,2);
  14456. const char *c = script_getstr(st,3);
  14457. int index = script_getnum(st,4);
  14458. char *output = aStrdup(str);
  14459. if(index >= 0 && index < strlen(output))
  14460. output[index] = *c;
  14461. script_pushstr(st, output);
  14462. return SCRIPT_CMD_SUCCESS;
  14463. }
  14464. //=======================================================
  14465. // insertchar <string>, <char>, <index>
  14466. //-------------------------------------------------------
  14467. BUILDIN_FUNC(insertchar)
  14468. {
  14469. const char *str = script_getstr(st,2);
  14470. const char *c = script_getstr(st,3);
  14471. int index = script_getnum(st,4);
  14472. char *output;
  14473. size_t len = strlen(str);
  14474. if(index < 0)
  14475. index = 0;
  14476. else if(index > len)
  14477. index = len;
  14478. output = (char*)aMalloc(len + 2);
  14479. memcpy(output, str, index);
  14480. output[index] = c[0];
  14481. memcpy(&output[index+1], &str[index], len - index);
  14482. output[len+1] = '\0';
  14483. script_pushstr(st, output);
  14484. return SCRIPT_CMD_SUCCESS;
  14485. }
  14486. //=======================================================
  14487. // delchar <string>, <index>
  14488. //-------------------------------------------------------
  14489. BUILDIN_FUNC(delchar)
  14490. {
  14491. const char *str = script_getstr(st,2);
  14492. int index = script_getnum(st,3);
  14493. char *output;
  14494. size_t len = strlen(str);
  14495. if(index < 0 || index > len) {
  14496. //return original
  14497. output = aStrdup(str);
  14498. script_pushstr(st, output);
  14499. return SCRIPT_CMD_SUCCESS;
  14500. }
  14501. output = (char*)aMalloc(len);
  14502. memcpy(output, str, index);
  14503. memcpy(&output[index], &str[index+1], len - index);
  14504. script_pushstr(st, output);
  14505. return SCRIPT_CMD_SUCCESS;
  14506. }
  14507. //=======================================================
  14508. // strtoupper <str>
  14509. //-------------------------------------------------------
  14510. BUILDIN_FUNC(strtoupper)
  14511. {
  14512. const char *str = script_getstr(st,2);
  14513. char *output = aStrdup(str);
  14514. char *cursor = output;
  14515. while (*cursor != '\0') {
  14516. *cursor = TOUPPER(*cursor);
  14517. cursor++;
  14518. }
  14519. script_pushstr(st, output);
  14520. return SCRIPT_CMD_SUCCESS;
  14521. }
  14522. //=======================================================
  14523. // strtolower <str>
  14524. //-------------------------------------------------------
  14525. BUILDIN_FUNC(strtolower)
  14526. {
  14527. const char *str = script_getstr(st,2);
  14528. char *output = aStrdup(str);
  14529. char *cursor = output;
  14530. while (*cursor != '\0') {
  14531. *cursor = TOLOWER(*cursor);
  14532. cursor++;
  14533. }
  14534. script_pushstr(st, output);
  14535. return SCRIPT_CMD_SUCCESS;
  14536. }
  14537. //=======================================================
  14538. // substr <str>, <start>, <end>
  14539. //-------------------------------------------------------
  14540. BUILDIN_FUNC(substr)
  14541. {
  14542. const char *str = script_getstr(st,2);
  14543. char *output;
  14544. int start = script_getnum(st,3);
  14545. int end = script_getnum(st,4);
  14546. int len = 0;
  14547. if(start >= 0 && end < strlen(str) && start <= end) {
  14548. len = end - start + 1;
  14549. output = (char*)aMalloc(len + 1);
  14550. memcpy(output, &str[start], len);
  14551. } else
  14552. output = (char*)aMalloc(1);
  14553. output[len] = '\0';
  14554. script_pushstr(st, output);
  14555. return SCRIPT_CMD_SUCCESS;
  14556. }
  14557. //=======================================================
  14558. // explode <dest_string_array>, <str>, <delimiter>
  14559. // Note: delimiter is limited to 1 char
  14560. //-------------------------------------------------------
  14561. BUILDIN_FUNC(explode)
  14562. {
  14563. struct script_data* data = script_getdata(st, 2);
  14564. const char *str = script_getstr(st,3);
  14565. const char delimiter = script_getstr(st, 4)[0];
  14566. int32 id;
  14567. size_t len = strlen(str);
  14568. int i = 0, j = 0;
  14569. int start;
  14570. char *temp;
  14571. const char* name;
  14572. TBL_PC* sd = NULL;
  14573. temp = (char*)aMalloc(len + 1);
  14574. if( !data_isreference(data) ) {
  14575. ShowError("script:explode: not a variable\n");
  14576. script_reportdata(data);
  14577. st->state = END;
  14578. return SCRIPT_CMD_FAILURE;// not a variable
  14579. }
  14580. id = reference_getid(data);
  14581. start = reference_getindex(data);
  14582. name = reference_getname(data);
  14583. if( !is_string_variable(name) ) {
  14584. ShowError("script:explode: not string array\n");
  14585. script_reportdata(data);
  14586. st->state = END;
  14587. return SCRIPT_CMD_FAILURE;// data type mismatch
  14588. }
  14589. if( not_server_variable(*name) ) {
  14590. if( !script_rid2sd(sd) )
  14591. return SCRIPT_CMD_SUCCESS;// no player attached
  14592. }
  14593. while(str[i] != '\0') {
  14594. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14595. temp[j] = '\0';
  14596. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14597. j = 0;
  14598. ++i;
  14599. } else {
  14600. temp[j++] = str[i++];
  14601. }
  14602. }
  14603. //set last string
  14604. temp[j] = '\0';
  14605. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14606. aFree(temp);
  14607. return SCRIPT_CMD_SUCCESS;
  14608. }
  14609. //=======================================================
  14610. // implode <string_array>
  14611. // implode <string_array>, <glue>
  14612. //-------------------------------------------------------
  14613. BUILDIN_FUNC(implode)
  14614. {
  14615. struct script_data* data = script_getdata(st, 2);
  14616. if( !data_isreference(data) ) {
  14617. ShowError("script:implode: not a variable\n");
  14618. script_reportdata(data);
  14619. st->state = END;
  14620. return SCRIPT_CMD_FAILURE;// not a variable
  14621. }
  14622. uint32 id = reference_getid( data );
  14623. const char* name = reference_getname( data );
  14624. if( !is_string_variable(name) ) {
  14625. ShowError("script:implode: not string array\n");
  14626. script_reportdata(data);
  14627. st->state = END;
  14628. return SCRIPT_CMD_FAILURE;// data type mismatch
  14629. }
  14630. map_session_data* sd = nullptr;
  14631. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14632. return SCRIPT_CMD_SUCCESS;// no player attached
  14633. }
  14634. //count chars
  14635. const uint32 array_size = script_array_highest_key( st, sd, name, reference_getref( data ) );
  14636. if(array_size == 0) {
  14637. ShowWarning("script:implode: array length = 0\n");
  14638. script_pushstrcopy( st, "NULL" );
  14639. } else {
  14640. const char *glue = nullptr, *temp;
  14641. size_t len = 0, glue_len = 0, k = 0;
  14642. for( uint32 i = 0; i < array_size; ++i ){
  14643. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14644. len += strlen(temp);
  14645. // Remove stack entry from get_val2_str
  14646. script_removetop( st, -1, 0 );
  14647. }
  14648. //allocate mem
  14649. if( script_hasdata(st,3) ) {
  14650. glue = script_getstr(st,3);
  14651. glue_len = strlen(glue);
  14652. len += glue_len * (size_t)( array_size - 1 );
  14653. }
  14654. char* output = (char*)aMalloc( len + 1 );
  14655. //build output
  14656. for( uint32 i = 0; i < array_size - 1; ++i ){
  14657. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14658. len = strlen(temp);
  14659. memcpy(&output[k], temp, len);
  14660. k += len;
  14661. // Remove stack entry from get_val2_str
  14662. script_removetop( st, -1, 0 );
  14663. if(glue_len != 0) {
  14664. memcpy(&output[k], glue, glue_len);
  14665. k += glue_len;
  14666. }
  14667. }
  14668. temp = get_val2_str( st, reference_uid( id, array_size - 1), reference_getref( data ) );
  14669. len = strlen(temp);
  14670. memcpy(&output[k], temp, len);
  14671. k += len;
  14672. output[k] = '\0';
  14673. // Remove stack entry from get_val2_str
  14674. script_removetop( st, -1, 0 );
  14675. script_pushstr( st, output );
  14676. }
  14677. return SCRIPT_CMD_SUCCESS;
  14678. }
  14679. //=======================================================
  14680. // sprintf(<format>, ...);
  14681. // Implements C sprintf, except format %n. The resulting string is
  14682. // returned, instead of being saved in variable by reference.
  14683. //-------------------------------------------------------
  14684. BUILDIN_FUNC(sprintf)
  14685. {
  14686. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14687. const char* format;
  14688. char* p;
  14689. char* q;
  14690. char* buf = NULL;
  14691. char* buf2 = NULL;
  14692. struct script_data* data;
  14693. StringBuf final_buf;
  14694. // Fetch init data
  14695. format = script_getstr(st, 2);
  14696. argc = script_lastdata(st)-2;
  14697. len = strlen(format);
  14698. // Skip parsing, where no parsing is required.
  14699. if(len == 0) {
  14700. script_pushconststr(st,"");
  14701. return SCRIPT_CMD_SUCCESS;
  14702. }
  14703. // Pessimistic alloc
  14704. CREATE(buf, char, len+1);
  14705. // Need not be parsed, just solve stuff like %%.
  14706. if(argc == 0) {
  14707. memcpy(buf,format,len+1);
  14708. script_pushstrcopy(st, buf);
  14709. aFree(buf);
  14710. return SCRIPT_CMD_SUCCESS;
  14711. }
  14712. safestrncpy(buf, format, len+1);
  14713. // Issue sprintf for each parameter
  14714. StringBuf_Init(&final_buf);
  14715. q = buf;
  14716. while((p = strchr(q, '%')) != NULL) {
  14717. if(p != q) {
  14718. len = p - q + 1;
  14719. if(buf2_len < len) {
  14720. RECREATE(buf2, char, len);
  14721. buf2_len = len;
  14722. }
  14723. safestrncpy(buf2, q, len);
  14724. StringBuf_AppendStr(&final_buf, buf2);
  14725. q = p;
  14726. }
  14727. p = q + 1;
  14728. if(*p == '%') { // %%
  14729. StringBuf_AppendStr(&final_buf, "%");
  14730. q+=2;
  14731. continue;
  14732. }
  14733. if(*p == 'n') { // %n
  14734. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14735. script_reportsrc(st);
  14736. q+=2;
  14737. continue;
  14738. }
  14739. if(arg >= argc) {
  14740. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14741. if(buf) aFree(buf);
  14742. if(buf2) aFree(buf2);
  14743. StringBuf_Destroy(&final_buf);
  14744. script_pushconststr(st,"");
  14745. return SCRIPT_CMD_FAILURE;
  14746. }
  14747. if((p = strchr(q+1, '%')) == NULL)
  14748. p = strchr(q, 0); // EOS
  14749. len = p - q + 1;
  14750. if(buf2_len < len) {
  14751. RECREATE(buf2, char, len);
  14752. buf2_len = len;
  14753. }
  14754. safestrncpy(buf2, q, len);
  14755. q = p;
  14756. // Note: This assumes the passed value being the correct
  14757. // type to the current format specifier. If not, the server
  14758. // probably crashes or returns anything else, than expected,
  14759. // but it would behave in normal code the same way so it's
  14760. // the scripter's responsibility.
  14761. data = script_getdata(st, arg+3);
  14762. if(data_isstring(data)) // String
  14763. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14764. else if(data_isint(data)) // Number
  14765. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14766. else if(data_isreference(data)) { // Variable
  14767. char* name = reference_getname(data);
  14768. if(name[strlen(name)-1]=='$') // var Str
  14769. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14770. else // var Int
  14771. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14772. } else { // Unsupported type
  14773. ShowError("buildin_sprintf: Unknown argument type!\n");
  14774. if(buf) aFree(buf);
  14775. if(buf2) aFree(buf2);
  14776. StringBuf_Destroy(&final_buf);
  14777. script_pushconststr(st,"");
  14778. return SCRIPT_CMD_FAILURE;
  14779. }
  14780. arg++;
  14781. }
  14782. // Append anything left
  14783. if(*q)
  14784. StringBuf_AppendStr(&final_buf, q);
  14785. // Passed more, than needed
  14786. if(arg < argc) {
  14787. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14788. script_reportsrc(st);
  14789. }
  14790. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14791. if(buf) aFree(buf);
  14792. if(buf2) aFree(buf2);
  14793. StringBuf_Destroy(&final_buf);
  14794. return SCRIPT_CMD_SUCCESS;
  14795. }
  14796. //=======================================================
  14797. // sscanf(<str>, <format>, ...);
  14798. // Implements C sscanf.
  14799. //-------------------------------------------------------
  14800. BUILDIN_FUNC(sscanf){
  14801. unsigned int argc, arg = 0, len;
  14802. struct script_data* data;
  14803. map_session_data* sd = NULL;
  14804. const char* str;
  14805. const char* format;
  14806. const char* p;
  14807. const char* q;
  14808. char* buf = NULL;
  14809. char* buf_p;
  14810. char* ref_str = NULL;
  14811. int ref_int;
  14812. // Get data
  14813. str = script_getstr(st, 2);
  14814. format = script_getstr(st, 3);
  14815. argc = script_lastdata(st)-3;
  14816. len = strlen(format);
  14817. if (len != 0 && strlen(str) == 0) {
  14818. // If the source string is empty but the format string is not, we return -1
  14819. // according to the C specs. (if the format string is also empty, we shall
  14820. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14821. script_pushint(st, -1);
  14822. return SCRIPT_CMD_SUCCESS;
  14823. }
  14824. CREATE(buf, char, len*2+1);
  14825. // Issue sscanf for each parameter
  14826. *buf = 0;
  14827. q = format;
  14828. while((p = strchr(q, '%'))){
  14829. if(p!=q){
  14830. strncat(buf, q, (size_t)(p-q));
  14831. q = p;
  14832. }
  14833. p = q+1;
  14834. if(*p=='*' || *p=='%'){ // Skip
  14835. strncat(buf, q, 2);
  14836. q+=2;
  14837. continue;
  14838. }
  14839. if(arg>=argc){
  14840. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14841. script_pushint(st, -1);
  14842. if(buf) aFree(buf);
  14843. if(ref_str) aFree(ref_str);
  14844. return SCRIPT_CMD_FAILURE;
  14845. }
  14846. if((p = strchr(q+1, '%'))==NULL){
  14847. p = strchr(q, 0); // EOS
  14848. }
  14849. len = p-q;
  14850. strncat(buf, q, len);
  14851. q = p;
  14852. // Validate output
  14853. data = script_getdata(st, arg+4);
  14854. if(!data_isreference(data) || !reference_tovariable(data)){
  14855. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14856. script_pushint(st, -1);
  14857. if(buf) aFree(buf);
  14858. if(ref_str) aFree(ref_str);
  14859. return SCRIPT_CMD_FAILURE;
  14860. }
  14861. buf_p = reference_getname(data);
  14862. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14863. script_pushint(st, -1);
  14864. if(buf) aFree(buf);
  14865. if(ref_str) aFree(ref_str);
  14866. return SCRIPT_CMD_SUCCESS;
  14867. }
  14868. // Save value if any
  14869. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14870. if(ref_str==NULL){
  14871. CREATE(ref_str, char, strlen(str)+1);
  14872. }
  14873. if(sscanf(str, buf, ref_str)==0){
  14874. break;
  14875. }
  14876. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14877. } else { // Number
  14878. if(sscanf(str, buf, &ref_int)==0){
  14879. break;
  14880. }
  14881. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14882. }
  14883. arg++;
  14884. // Disable used format (%... -> %*...)
  14885. buf_p = strchr(buf, 0);
  14886. memmove(buf_p-len+2, buf_p-len+1, len);
  14887. *(buf_p-len+1) = '*';
  14888. }
  14889. script_pushint(st, arg);
  14890. if(buf) aFree(buf);
  14891. if(ref_str) aFree(ref_str);
  14892. return SCRIPT_CMD_SUCCESS;
  14893. }
  14894. //=======================================================
  14895. // strpos(<haystack>, <needle>)
  14896. // strpos(<haystack>, <needle>, <offset>)
  14897. //
  14898. // Implements PHP style strpos. Adapted from code from
  14899. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14900. //-------------------------------------------------------
  14901. BUILDIN_FUNC(strpos) {
  14902. const char *haystack = script_getstr(st,2);
  14903. const char *needle = script_getstr(st,3);
  14904. int i;
  14905. size_t len;
  14906. if( script_hasdata(st,4) )
  14907. i = script_getnum(st,4);
  14908. else
  14909. i = 0;
  14910. if (needle[0] == '\0') {
  14911. script_pushint(st, -1);
  14912. return SCRIPT_CMD_SUCCESS;
  14913. }
  14914. len = strlen(haystack);
  14915. for ( ; i < len; ++i ) {
  14916. if ( haystack[i] == *needle ) {
  14917. // matched starting char -- loop through remaining chars
  14918. const char *h, *n;
  14919. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14920. if ( *h != *n ) {
  14921. break;
  14922. }
  14923. }
  14924. if ( !*n ) { // matched all of 'needle' to null termination
  14925. script_pushint(st, i);
  14926. return SCRIPT_CMD_SUCCESS;
  14927. }
  14928. }
  14929. }
  14930. script_pushint(st, -1);
  14931. return SCRIPT_CMD_SUCCESS;
  14932. }
  14933. //===============================================================
  14934. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14935. //
  14936. // Note: Finds all instances of <search> in <input> and replaces
  14937. // with <replace>. If specified will only replace as many
  14938. // instances as specified in <count>. By default will be case
  14939. // sensitive.
  14940. //---------------------------------------------------------------
  14941. BUILDIN_FUNC(replacestr)
  14942. {
  14943. const char *input = script_getstr(st, 2);
  14944. const char *find = script_getstr(st, 3);
  14945. const char *replace = script_getstr(st, 4);
  14946. size_t inputlen = strlen(input);
  14947. size_t findlen = strlen(find);
  14948. struct StringBuf output;
  14949. bool usecase = true;
  14950. int count = 0;
  14951. int numFinds = 0;
  14952. int i = 0, f = 0;
  14953. if(findlen == 0) {
  14954. ShowError("script:replacestr: Invalid search length.\n");
  14955. st->state = END;
  14956. return SCRIPT_CMD_FAILURE;
  14957. }
  14958. if(script_hasdata(st, 5)) {
  14959. if( script_isint(st, 5) )
  14960. usecase = script_getnum(st, 5) != 0;
  14961. else {
  14962. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14963. st->state = END;
  14964. return SCRIPT_CMD_FAILURE;
  14965. }
  14966. }
  14967. if(script_hasdata(st, 6)) {
  14968. count = script_getnum(st, 6);
  14969. if(count == 0) {
  14970. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14971. st->state = END;
  14972. return SCRIPT_CMD_FAILURE;
  14973. }
  14974. }
  14975. StringBuf_Init(&output);
  14976. for(; i < inputlen; i++) {
  14977. if(count && count == numFinds) { //found enough, stop looking
  14978. break;
  14979. }
  14980. for(f = 0; f <= findlen; f++) {
  14981. if(f == findlen) { //complete match
  14982. numFinds++;
  14983. StringBuf_AppendStr(&output, replace);
  14984. i += findlen - 1;
  14985. break;
  14986. } else {
  14987. if(usecase) {
  14988. if((i + f) > inputlen || input[i + f] != find[f]) {
  14989. StringBuf_Printf(&output, "%c", input[i]);
  14990. break;
  14991. }
  14992. } else {
  14993. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14994. StringBuf_Printf(&output, "%c", input[i]);
  14995. break;
  14996. }
  14997. }
  14998. }
  14999. }
  15000. }
  15001. //append excess after enough found
  15002. if(i < inputlen)
  15003. StringBuf_AppendStr(&output, &(input[i]));
  15004. script_pushstrcopy(st, StringBuf_Value(&output));
  15005. StringBuf_Destroy(&output);
  15006. return SCRIPT_CMD_SUCCESS;
  15007. }
  15008. //========================================================
  15009. // countstr <input>, <search>{, <usecase>}
  15010. //
  15011. // Note: Counts the number of times <search> occurs in
  15012. // <input>. By default will be case sensitive.
  15013. //--------------------------------------------------------
  15014. BUILDIN_FUNC(countstr)
  15015. {
  15016. const char *input = script_getstr(st, 2);
  15017. const char *find = script_getstr(st, 3);
  15018. size_t inputlen = strlen(input);
  15019. size_t findlen = strlen(find);
  15020. bool usecase = true;
  15021. int numFinds = 0;
  15022. int i = 0, f = 0;
  15023. if(findlen == 0) {
  15024. ShowError("script:countstr: Invalid search length.\n");
  15025. st->state = END;
  15026. return SCRIPT_CMD_FAILURE;
  15027. }
  15028. if(script_hasdata(st, 4)) {
  15029. if( script_isint(st, 4) )
  15030. usecase = script_getnum(st, 4) != 0;
  15031. else {
  15032. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  15033. st->state = END;
  15034. return SCRIPT_CMD_FAILURE;
  15035. }
  15036. }
  15037. for(; i < inputlen; i++) {
  15038. for(f = 0; f <= findlen; f++) {
  15039. if(f == findlen) { //complete match
  15040. numFinds++;
  15041. i += findlen - 1;
  15042. break;
  15043. } else {
  15044. if(usecase) {
  15045. if((i + f) > inputlen || input[i + f] != find[f]) {
  15046. break;
  15047. }
  15048. } else {
  15049. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  15050. break;
  15051. }
  15052. }
  15053. }
  15054. }
  15055. }
  15056. script_pushint(st, numFinds);
  15057. return SCRIPT_CMD_SUCCESS;
  15058. }
  15059. /// Changes the display name and/or display class of the npc.
  15060. /// Returns 0 is successful, 1 if the npc does not exist.
  15061. ///
  15062. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  15063. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  15064. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  15065. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  15066. BUILDIN_FUNC(setnpcdisplay)
  15067. {
  15068. const char* name;
  15069. const char* newname = NULL;
  15070. int class_ = JT_FAKENPC, size = -1;
  15071. struct npc_data* nd;
  15072. name = script_getstr(st,2);
  15073. if( script_hasdata(st,4) )
  15074. class_ = script_getnum(st,4);
  15075. if( script_hasdata(st,5) )
  15076. size = script_getnum(st,5);
  15077. if( script_isstring(st, 3) )
  15078. newname = script_getstr(st, 3);
  15079. else
  15080. class_ = script_getnum(st, 3);
  15081. nd = npc_name2id(name);
  15082. if( nd == NULL )
  15083. {// not found
  15084. script_pushint(st,1);
  15085. return SCRIPT_CMD_SUCCESS;
  15086. }
  15087. // update npc
  15088. if( newname )
  15089. npc_setdisplayname(nd, newname);
  15090. if( size != -1 && size != (int)nd->size )
  15091. nd->size = size;
  15092. else
  15093. size = -1;
  15094. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  15095. npc_setclass(nd, class_);
  15096. else if( size != -1 )
  15097. unit_refresh(&nd->bl); // Required to update the visual size
  15098. script_pushint(st,0);
  15099. return SCRIPT_CMD_SUCCESS;
  15100. }
  15101. BUILDIN_FUNC(atoi)
  15102. {
  15103. const char *value;
  15104. value = script_getstr(st,2);
  15105. script_pushint(st,atoi(value));
  15106. return SCRIPT_CMD_SUCCESS;
  15107. }
  15108. BUILDIN_FUNC(axtoi)
  15109. {
  15110. const char *hex = script_getstr(st,2);
  15111. long value = strtol(hex, NULL, 16);
  15112. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  15113. value = cap_value(value, INT_MIN, INT_MAX);
  15114. #endif
  15115. script_pushint(st, (int)value);
  15116. return SCRIPT_CMD_SUCCESS;
  15117. }
  15118. BUILDIN_FUNC(strtol)
  15119. {
  15120. const char *string = script_getstr(st, 2);
  15121. int base = script_getnum(st, 3);
  15122. long value = strtol(string, NULL, base);
  15123. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  15124. value = cap_value(value, INT_MIN, INT_MAX);
  15125. #endif
  15126. script_pushint(st, (int)value);
  15127. return SCRIPT_CMD_SUCCESS;
  15128. }
  15129. // case-insensitive substring search [lordalfa]
  15130. BUILDIN_FUNC(compare)
  15131. {
  15132. const char *message;
  15133. const char *cmpstring;
  15134. message = script_getstr(st,2);
  15135. cmpstring = script_getstr(st,3);
  15136. script_pushint(st,(stristr(message,cmpstring) != NULL));
  15137. return SCRIPT_CMD_SUCCESS;
  15138. }
  15139. BUILDIN_FUNC(strcmp)
  15140. {
  15141. const char *str1;
  15142. const char *str2;
  15143. str1 = script_getstr(st,2);
  15144. str2 = script_getstr(st,3);
  15145. script_pushint(st,strcmp(str1, str2));
  15146. return SCRIPT_CMD_SUCCESS;
  15147. }
  15148. // [zBuffer] List of mathematics commands --->
  15149. BUILDIN_FUNC(sqrt)
  15150. {
  15151. double i, a;
  15152. i = script_getnum(st,2);
  15153. a = sqrt(i);
  15154. script_pushint(st,(int)a);
  15155. return SCRIPT_CMD_SUCCESS;
  15156. }
  15157. BUILDIN_FUNC(pow)
  15158. {
  15159. double i, a, b;
  15160. a = script_getnum(st,2);
  15161. b = script_getnum(st,3);
  15162. i = pow(a,b);
  15163. script_pushint(st,(int)i);
  15164. return SCRIPT_CMD_SUCCESS;
  15165. }
  15166. BUILDIN_FUNC(distance)
  15167. {
  15168. int x0, y0, x1, y1;
  15169. x0 = script_getnum(st,2);
  15170. y0 = script_getnum(st,3);
  15171. x1 = script_getnum(st,4);
  15172. y1 = script_getnum(st,5);
  15173. script_pushint(st,distance_xy(x0,y0,x1,y1));
  15174. return SCRIPT_CMD_SUCCESS;
  15175. }
  15176. // <--- [zBuffer] List of mathematics commands
  15177. BUILDIN_FUNC(md5)
  15178. {
  15179. const char *tmpstr;
  15180. char *md5str;
  15181. tmpstr = script_getstr(st,2);
  15182. md5str = (char *)aMalloc((32+1)*sizeof(char));
  15183. MD5_String(tmpstr, md5str);
  15184. script_pushstr(st, md5str);
  15185. return SCRIPT_CMD_SUCCESS;
  15186. }
  15187. // [zBuffer] List of dynamic var commands --->
  15188. /**
  15189. * setd "<variable name>",<value>{,<char_id>};
  15190. **/
  15191. BUILDIN_FUNC(setd)
  15192. {
  15193. TBL_PC *sd = NULL;
  15194. char varname[100];
  15195. const char *buffer;
  15196. int elem;
  15197. buffer = script_getstr(st, 2);
  15198. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15199. elem = 0;
  15200. if( not_server_variable(*varname) ) {
  15201. if (!script_charid2sd(4,sd))
  15202. return SCRIPT_CMD_FAILURE;
  15203. }
  15204. if( is_string_variable(varname) ) {
  15205. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), NULL );
  15206. } else {
  15207. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), NULL );
  15208. }
  15209. return SCRIPT_CMD_SUCCESS;
  15210. }
  15211. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  15212. {
  15213. int i, j;
  15214. TBL_PC* sd = NULL;
  15215. const char* query;
  15216. struct script_data* data;
  15217. const char* name;
  15218. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  15219. int num_vars;
  15220. int num_cols;
  15221. // check target variables
  15222. for( i = 3; script_hasdata(st,i); ++i ) {
  15223. data = script_getdata(st, i);
  15224. if( data_isreference(data) ) { // it's a variable
  15225. name = reference_getname(data);
  15226. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  15227. if( !script_rid2sd(sd) ) { // no player attached
  15228. script_reportdata(data);
  15229. st->state = END;
  15230. return SCRIPT_CMD_FAILURE;
  15231. }
  15232. }
  15233. } else {
  15234. ShowError("script:query_sql: not a variable\n");
  15235. script_reportdata(data);
  15236. st->state = END;
  15237. return SCRIPT_CMD_FAILURE;
  15238. }
  15239. }
  15240. num_vars = i - 3;
  15241. // Execute the query
  15242. query = script_getstr(st,2);
  15243. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  15244. Sql_ShowDebug(handle);
  15245. script_pushint(st, -1);
  15246. return SCRIPT_CMD_FAILURE;
  15247. }
  15248. if( Sql_NumRows(handle) == 0 ) { // No data received
  15249. Sql_FreeResult(handle);
  15250. script_pushint(st, 0);
  15251. return SCRIPT_CMD_SUCCESS;
  15252. }
  15253. // Count the number of columns to store
  15254. num_cols = Sql_NumColumns(handle);
  15255. if( num_vars < num_cols ) {
  15256. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  15257. script_reportsrc(st);
  15258. } else if( num_vars > num_cols ) {
  15259. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  15260. script_reportsrc(st);
  15261. }
  15262. // Store data
  15263. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  15264. for( j = 0; j < num_vars; ++j ) {
  15265. char* str = NULL;
  15266. if( j < num_cols )
  15267. Sql_GetData(handle, j, &str, NULL);
  15268. data = script_getdata(st, j+3);
  15269. name = reference_getname(data);
  15270. if( is_string_variable(name) )
  15271. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  15272. else
  15273. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  15274. }
  15275. }
  15276. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  15277. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  15278. script_reportsrc(st);
  15279. }
  15280. // Free data
  15281. Sql_FreeResult(handle);
  15282. script_pushint(st, i);
  15283. return SCRIPT_CMD_SUCCESS;
  15284. }
  15285. BUILDIN_FUNC(query_sql) {
  15286. return buildin_query_sql_sub(st, qsmysql_handle);
  15287. }
  15288. BUILDIN_FUNC(query_logsql) {
  15289. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  15290. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  15291. script_pushint(st,-1);
  15292. return SCRIPT_CMD_FAILURE;
  15293. }
  15294. return buildin_query_sql_sub(st, logmysql_handle);
  15295. }
  15296. //Allows escaping of a given string.
  15297. BUILDIN_FUNC(escape_sql)
  15298. {
  15299. const char *str;
  15300. char *esc_str;
  15301. size_t len;
  15302. str = script_getstr(st,2);
  15303. len = strlen(str);
  15304. esc_str = (char*)aMalloc(len*2+1);
  15305. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  15306. script_pushstr(st, esc_str);
  15307. return SCRIPT_CMD_SUCCESS;
  15308. }
  15309. BUILDIN_FUNC(getd)
  15310. {
  15311. char varname[100];
  15312. const char *buffer;
  15313. int elem;
  15314. buffer = script_getstr(st, 2);
  15315. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15316. elem = 0;
  15317. // Push the 'pointer' so it's more flexible [Lance]
  15318. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  15319. return SCRIPT_CMD_SUCCESS;
  15320. }
  15321. BUILDIN_FUNC(callshop)
  15322. {
  15323. TBL_PC *sd = NULL;
  15324. struct npc_data *nd;
  15325. const char *shopname;
  15326. int flag = 0;
  15327. if (!script_rid2sd(sd)) {
  15328. script_pushint(st,0);
  15329. return SCRIPT_CMD_SUCCESS;
  15330. }
  15331. shopname = script_getstr(st, 2);
  15332. if (script_hasdata(st,3))
  15333. flag = script_getnum(st,3);
  15334. nd = npc_name2id(shopname);
  15335. 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) ) {
  15336. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  15337. script_pushint(st,0);
  15338. return SCRIPT_CMD_FAILURE;
  15339. }
  15340. if (nd->subtype == NPCTYPE_SHOP) {
  15341. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15342. sd->state.callshop = 1;
  15343. switch (flag) {
  15344. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  15345. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  15346. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  15347. }
  15348. }
  15349. #if PACKETVER >= 20131223
  15350. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15351. unsigned short i;
  15352. for (i = 0; i < nd->u.shop.count; i++) {
  15353. if (nd->u.shop.shop_item[i].qty)
  15354. break;
  15355. }
  15356. if (i == nd->u.shop.count) {
  15357. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  15358. return SCRIPT_CMD_FAILURE;
  15359. }
  15360. sd->npc_shopid = nd->bl.id;
  15361. clif_npc_market_open(sd, nd);
  15362. script_pushint(st,1);
  15363. return SCRIPT_CMD_SUCCESS;
  15364. }
  15365. #endif
  15366. else if( nd->subtype == NPCTYPE_BARTER ){
  15367. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15368. sd->state.callshop = 1;
  15369. if( nd->u.barter.extended ){
  15370. clif_barter_extended_open( *sd, *nd );
  15371. }else{
  15372. clif_barter_open( *sd, *nd );
  15373. }
  15374. }else
  15375. clif_cashshop_show(sd, nd);
  15376. sd->npc_shopid = nd->bl.id;
  15377. script_pushint(st,1);
  15378. return SCRIPT_CMD_SUCCESS;
  15379. }
  15380. BUILDIN_FUNC(npcshopitem)
  15381. {
  15382. const char* npcname = script_getstr(st, 2);
  15383. struct npc_data* nd = npc_name2id(npcname);
  15384. int n, i;
  15385. int amount;
  15386. uint16 offs = 2;
  15387. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  15388. script_pushint(st,0);
  15389. return SCRIPT_CMD_SUCCESS;
  15390. }
  15391. #if PACKETVER >= 20131223
  15392. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15393. offs = 3;
  15394. npc_market_delfromsql_(nd->exname, 0, true);
  15395. }
  15396. #endif
  15397. // get the count of new entries
  15398. amount = (script_lastdata(st)-2)/offs;
  15399. // generate new shop item list
  15400. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  15401. nd->u.shop.count = 0;
  15402. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  15403. t_itemid nameid = script_getnum( st, i );
  15404. std::shared_ptr<item_data> id = item_db.find(nameid);
  15405. if( !id ){
  15406. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  15407. script_pushint( st, 0 );
  15408. return SCRIPT_CMD_FAILURE;
  15409. }
  15410. int32 price = script_getnum(st, i + 1);
  15411. if (price < 0) {
  15412. if (nd->subtype == NPCTYPE_CASHSHOP || nd->subtype == NPCTYPE_POINTSHOP || nd->subtype == NPCTYPE_ITEMSHOP) {
  15413. ShowError("builtin_npcshopitem: Invalid price in shop '%s'.\n", nd->exname);
  15414. script_pushint(st, 0);
  15415. return SCRIPT_CMD_FAILURE;
  15416. }
  15417. price = id->value_buy;
  15418. }
  15419. nd->u.shop.shop_item[n].nameid = nameid;
  15420. nd->u.shop.shop_item[n].value = price;
  15421. #if PACKETVER >= 20131223
  15422. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15423. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  15424. nd->u.shop.shop_item[n].flag = 1;
  15425. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  15426. }
  15427. #endif
  15428. nd->u.shop.count++;
  15429. }
  15430. script_pushint(st,1);
  15431. return SCRIPT_CMD_SUCCESS;
  15432. }
  15433. BUILDIN_FUNC(npcshopadditem)
  15434. {
  15435. const char* npcname = script_getstr(st,2);
  15436. struct npc_data* nd = npc_name2id(npcname);
  15437. uint16 offs = 2, amount;
  15438. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15439. script_pushint(st,0);
  15440. return SCRIPT_CMD_SUCCESS;
  15441. }
  15442. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15443. offs = 3;
  15444. // get the count of new entries
  15445. amount = (script_lastdata(st)-2)/offs;
  15446. #if PACKETVER >= 20131223
  15447. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15448. for (int n = 0, i = 3; n < amount; n++, i += offs) {
  15449. t_itemid nameid = script_getnum(st,i);
  15450. uint16 j;
  15451. std::shared_ptr<item_data> id = item_db.find(nameid);
  15452. if( !id ){
  15453. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15454. script_pushint( st, 0 );
  15455. return SCRIPT_CMD_FAILURE;
  15456. }
  15457. // Check existing entries
  15458. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  15459. if (j == nd->u.shop.count) {
  15460. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  15461. j = nd->u.shop.count;
  15462. nd->u.shop.shop_item[j].flag = 1;
  15463. nd->u.shop.count++;
  15464. }
  15465. int32 price = script_getnum(st, i + 1);
  15466. if (price < 0) {
  15467. price = id->value_buy;
  15468. }
  15469. int32 stock = script_getnum(st, i + 2);
  15470. if( stock < -1 ){
  15471. ShowError( "builtin_npcshopadditem: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  15472. script_pushint( st, 0 );
  15473. return SCRIPT_CMD_FAILURE;
  15474. }
  15475. nd->u.shop.shop_item[j].nameid = nameid;
  15476. nd->u.shop.shop_item[j].value = price;
  15477. nd->u.shop.shop_item[j].qty = stock;
  15478. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  15479. }
  15480. script_pushint(st,1);
  15481. return SCRIPT_CMD_SUCCESS;
  15482. }
  15483. #endif
  15484. // append new items to existing shop item list
  15485. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  15486. for (int n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  15487. {
  15488. t_itemid nameid = script_getnum( st, i );
  15489. std::shared_ptr<item_data> id = item_db.find(nameid);
  15490. if( !id ){
  15491. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15492. script_pushint( st, 0 );
  15493. return SCRIPT_CMD_FAILURE;
  15494. }
  15495. int32 price = script_getnum(st, i + 1);
  15496. if (price < 0) {
  15497. if (nd->subtype == NPCTYPE_CASHSHOP || nd->subtype == NPCTYPE_POINTSHOP || nd->subtype == NPCTYPE_ITEMSHOP) {
  15498. ShowError("builtin_npcshopadditem: Invalid price in shop '%s'.\n", nd->exname);
  15499. script_pushint(st, 0);
  15500. return SCRIPT_CMD_FAILURE;
  15501. }
  15502. price = id->value_buy;
  15503. }
  15504. nd->u.shop.shop_item[n].nameid = nameid;
  15505. nd->u.shop.shop_item[n].value = price;
  15506. nd->u.shop.count++;
  15507. }
  15508. script_pushint(st,1);
  15509. return SCRIPT_CMD_SUCCESS;
  15510. }
  15511. BUILDIN_FUNC(npcshopdelitem)
  15512. {
  15513. const char* npcname = script_getstr(st,2);
  15514. struct npc_data* nd = npc_name2id(npcname);
  15515. int n, i, size;
  15516. unsigned short amount;
  15517. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15518. script_pushint(st,0);
  15519. return SCRIPT_CMD_SUCCESS;
  15520. }
  15521. amount = script_lastdata(st)-2;
  15522. size = nd->u.shop.count;
  15523. // remove specified items from the shop item list
  15524. for( i = 3; i < 3 + amount; i++ ) {
  15525. t_itemid nameid = script_getnum(st,i);
  15526. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  15527. if( n < size ) {
  15528. if (n+1 != size)
  15529. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-(n + 1)));
  15530. #if PACKETVER >= 20131223
  15531. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15532. npc_market_delfromsql_(nd->exname, nameid, false);
  15533. #endif
  15534. size--;
  15535. }
  15536. }
  15537. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  15538. nd->u.shop.count = size;
  15539. script_pushint(st,1);
  15540. return SCRIPT_CMD_SUCCESS;
  15541. }
  15542. /**
  15543. * Sets a script to attach to a shop npc.
  15544. * npcshopattach "<npc_name>";
  15545. **/
  15546. BUILDIN_FUNC(npcshopattach)
  15547. {
  15548. const char* npcname = script_getstr(st,2);
  15549. struct npc_data* nd = npc_name2id(npcname);
  15550. int flag = 1;
  15551. if( script_hasdata(st,3) )
  15552. flag = script_getnum(st,3);
  15553. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15554. script_pushint(st,0);
  15555. return SCRIPT_CMD_SUCCESS;
  15556. }
  15557. if (flag)
  15558. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  15559. else
  15560. nd->master_nd = NULL;
  15561. script_pushint(st,1);
  15562. return SCRIPT_CMD_SUCCESS;
  15563. }
  15564. /*==========================================
  15565. * Returns some values of an item [Lupus]
  15566. * Price, Weight, etc...
  15567. setitemscript(itemID,"{new item bonus script}",[n]);
  15568. Where n:
  15569. 0 - script
  15570. 1 - Equip script
  15571. 2 - Unequip script
  15572. *------------------------------------------*/
  15573. BUILDIN_FUNC(setitemscript)
  15574. {
  15575. int n = 0;
  15576. const char *script;
  15577. struct script_code **dstscript;
  15578. t_itemid item_id = script_getnum(st,2);
  15579. script = script_getstr(st,3);
  15580. if( script_hasdata(st,4) )
  15581. n=script_getnum(st,4);
  15582. std::shared_ptr<item_data> i_data = item_db.find(item_id);
  15583. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15584. script_pushint(st,0);
  15585. return SCRIPT_CMD_SUCCESS;
  15586. }
  15587. switch (n) {
  15588. case 2:
  15589. dstscript = &i_data->unequip_script;
  15590. break;
  15591. case 1:
  15592. dstscript = &i_data->equip_script;
  15593. break;
  15594. default:
  15595. dstscript = &i_data->script;
  15596. break;
  15597. }
  15598. if(*dstscript)
  15599. script_free_code(*dstscript);
  15600. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15601. script_pushint(st,1);
  15602. return SCRIPT_CMD_SUCCESS;
  15603. }
  15604. /*=======================================================
  15605. * Add or Update a mob drop temporarily [Akinari]
  15606. * Original Idea By: [Lupus]
  15607. *
  15608. * addmonsterdrop <mob_id or name>,<item_id>,<rate>,{<steal protected>,{<random option group id>}};
  15609. *
  15610. * If given an item the mob already drops, the rate
  15611. * is updated to the new rate. Rate range is 1-10000.
  15612. * Returns true if succeeded (added/updated a mob drop) false otherwise.
  15613. *-------------------------------------------------------*/
  15614. BUILDIN_FUNC(addmonsterdrop)
  15615. {
  15616. std::shared_ptr<s_mob_db> mob;
  15617. if (script_isstring(st, 2))
  15618. mob = mob_db.find(mobdb_searchname(script_getstr(st, 2)));
  15619. else
  15620. mob = mob_db.find(script_getnum(st, 2));
  15621. if (mob == nullptr) {
  15622. if (script_isstring(st, 2))
  15623. ShowError("addmonsterdrop: bad mob name given %s\n", script_getstr(st, 2));
  15624. else
  15625. ShowError("addmonsterdrop: bad mob id given %d\n", script_getnum(st, 2));
  15626. script_pushint(st, false);
  15627. return SCRIPT_CMD_FAILURE;
  15628. }
  15629. t_itemid item_id = script_getnum(st, 3);
  15630. std::shared_ptr<item_data> itm = item_db.find(item_id);
  15631. if (itm == nullptr) {
  15632. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id);
  15633. script_pushint(st, false);
  15634. return SCRIPT_CMD_FAILURE;
  15635. }
  15636. int rate = script_getnum(st, 4);
  15637. if (rate < 1 || rate > 10000) {
  15638. ShowError("addmonsterdrop: Invalid rate %d (min 1, max 10000).\n", rate);
  15639. script_pushint(st, false);
  15640. return SCRIPT_CMD_FAILURE;
  15641. }
  15642. uint16 c = 0;
  15643. for (uint16 i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15644. if (mob->dropitem[i].nameid > 0) {
  15645. if (mob->dropitem[i].nameid == item_id) { // If it equals item_id we update that drop
  15646. c = i;
  15647. break;
  15648. }
  15649. continue;
  15650. }
  15651. if (c == 0) // Accept first available slot only
  15652. c = i;
  15653. }
  15654. if (c == 0) { // No place to put the new drop
  15655. script_pushint(st, false);
  15656. return SCRIPT_CMD_SUCCESS;
  15657. }
  15658. int steal_protected = 0;
  15659. int group = 0;
  15660. if (script_hasdata(st,5))
  15661. steal_protected = script_getnum(st, 5);
  15662. if (script_hasdata(st,6)) {
  15663. group = script_getnum(st, 6);
  15664. if (!random_option_group.exists(group)) {
  15665. ShowError("buildin_addmonsterdrop: Unknown random option group %d.\n", group);
  15666. script_pushint(st, false);
  15667. return SCRIPT_CMD_FAILURE;
  15668. }
  15669. if (itm->type != IT_WEAPON && itm->type != IT_ARMOR && itm->type != IT_SHADOWGEAR) {
  15670. ShowError("buildin_addmonsterdrop: Random option group can't be used with this type of item (item Id: %d).\n", item_id);
  15671. script_pushint(st, false);
  15672. return SCRIPT_CMD_FAILURE;
  15673. }
  15674. }
  15675. // Fill in the slot with the item and rate
  15676. mob->dropitem[c].nameid = item_id;
  15677. mob->dropitem[c].rate = rate;
  15678. mob->dropitem[c].steal_protected = steal_protected > 0;
  15679. mob->dropitem[c].randomopt_group = group;
  15680. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15681. script_pushint(st, true);
  15682. return SCRIPT_CMD_SUCCESS;
  15683. }
  15684. /*=======================================================
  15685. * Delete a mob drop temporarily [Akinari]
  15686. * Original Idea By: [Lupus]
  15687. *
  15688. * delmonsterdrop <mob_id or name>,<item_id>;
  15689. *
  15690. * Returns 1 if succeeded (deleted a mob drop)
  15691. *-------------------------------------------------------*/
  15692. BUILDIN_FUNC(delmonsterdrop)
  15693. {
  15694. std::shared_ptr<s_mob_db> mob;
  15695. if(script_isstring(st, 2))
  15696. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15697. else
  15698. mob = mob_db.find(script_getnum(st,2));
  15699. t_itemid item_id=script_getnum(st,3);
  15700. if(!item_db.exists(item_id)){
  15701. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15702. return SCRIPT_CMD_FAILURE;
  15703. }
  15704. if(mob) { //We got a valid monster, check for item drop on monster
  15705. unsigned char i;
  15706. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15707. if(mob->dropitem[i].nameid == item_id) {
  15708. mob->dropitem[i].nameid = 0;
  15709. mob->dropitem[i].rate = 0;
  15710. mob->dropitem[i].steal_protected = false;
  15711. mob->dropitem[i].randomopt_group = 0;
  15712. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15713. script_pushint(st,1);
  15714. return SCRIPT_CMD_SUCCESS;
  15715. }
  15716. }
  15717. //No drop on that monster
  15718. script_pushint(st,0);
  15719. } else {
  15720. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15721. return SCRIPT_CMD_FAILURE;
  15722. }
  15723. return SCRIPT_CMD_SUCCESS;
  15724. }
  15725. /*==========================================
  15726. * Returns a random mob_id
  15727. * type: Where to fetch from (see enum e_random_monster)
  15728. * flag: Type of checks to apply (see enum e_random_monster_flags)
  15729. * lv: Mob level to check against
  15730. *------------------------------------------*/
  15731. BUILDIN_FUNC(getrandmobid)
  15732. {
  15733. int type = script_getnum(st, 2);
  15734. if (type < MOBG_BRANCH_OF_DEAD_TREE || type >= MOBG_MAX) {
  15735. ShowWarning("buildin_getrandmobid: Invalid type %d.\n", type);
  15736. script_pushint(st, 0);
  15737. return SCRIPT_CMD_FAILURE;
  15738. }
  15739. int flag = script_hasdata(st, 3) ? script_getnum(st, 3) : RMF_MOB_NOT_BOSS;
  15740. if (flag < RMF_NONE || flag > RMF_ALL) {
  15741. ShowWarning("buildin_getrandmobid: Invalid flag %d.\n", flag);
  15742. script_pushint(st, 0);
  15743. return SCRIPT_CMD_FAILURE;
  15744. }
  15745. int lv;
  15746. if ( script_hasdata(st, 4) ) {
  15747. lv = script_getnum(st, 4);
  15748. if (lv <= 0) {
  15749. ShowWarning("buildin_getrandmobid: Invalid level %d.\n", lv);
  15750. script_pushint(st, 0);
  15751. return SCRIPT_CMD_FAILURE;
  15752. }
  15753. // If a level is provided, make sure it is respected
  15754. flag |= RMF_CHECK_MOB_LV;
  15755. } else {
  15756. lv = MAX_LEVEL;
  15757. }
  15758. script_pushint(st, mob_get_random_id(type, (enum e_random_monster_flags)flag, lv));
  15759. return SCRIPT_CMD_SUCCESS;
  15760. }
  15761. /*==========================================
  15762. * Returns some values of a monster [Lupus]
  15763. * Name, Level, race, size, etc...
  15764. getmonsterinfo(monsterID,queryIndex);
  15765. getmonsterinfo(monsterName,queryIndex);
  15766. *------------------------------------------*/
  15767. BUILDIN_FUNC(getmonsterinfo)
  15768. {
  15769. std::shared_ptr<s_mob_db> mob = nullptr;
  15770. if (script_isstring(st, 2))
  15771. mob = mobdb_search_aegisname(script_getstr(st, 2));
  15772. else {
  15773. uint16 mob_id = script_getnum(st, 2);
  15774. if (!mob_is_clone(mob_id)) {
  15775. mob = mob_db.find(mob_id);
  15776. }
  15777. }
  15778. if (mob == nullptr) {
  15779. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15780. if (script_getnum(st, 3) == MOB_NAME) // requested the name
  15781. script_pushconststr(st, "null");
  15782. else
  15783. script_pushint(st, -1);
  15784. return SCRIPT_CMD_SUCCESS;
  15785. }
  15786. switch ( script_getnum(st, 3) ) {
  15787. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15788. case MOB_LV: script_pushint(st,mob->lv); break;
  15789. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15790. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15791. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15792. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15793. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15794. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15795. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15796. case MOB_RES: script_pushint(st, mob->status.res); break;
  15797. case MOB_MRES: script_pushint(st, mob->status.mres); break;
  15798. case MOB_STR: script_pushint(st,mob->status.str); break;
  15799. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15800. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15801. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15802. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15803. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15804. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15805. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15806. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15807. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15808. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15809. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15810. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15811. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15812. case MOB_ID: script_pushint(st,mob->id); break;
  15813. default: script_pushint(st,-1); //wrong Index
  15814. }
  15815. return SCRIPT_CMD_SUCCESS;
  15816. }
  15817. /**
  15818. * Check player's vending/buyingstore/autotrade state
  15819. * checkvending({"<Player Name>"})
  15820. * @author [Nab4]
  15821. */
  15822. BUILDIN_FUNC(checkvending) {
  15823. TBL_PC *sd = NULL;
  15824. if (!script_nick2sd(2,sd) ) {
  15825. script_pushint(st,0);
  15826. return SCRIPT_CMD_SUCCESS;
  15827. }
  15828. else {
  15829. int8 ret = 0;
  15830. if (sd->state.vending)
  15831. ret = 1;
  15832. else if (sd->state.buyingstore)
  15833. ret = 4;
  15834. if (sd->state.autotrade)
  15835. ret |= 2;
  15836. script_pushint(st, ret);
  15837. }
  15838. return SCRIPT_CMD_SUCCESS;
  15839. }
  15840. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15841. {
  15842. TBL_PC *sd = NULL;
  15843. if( script_nick2sd(2,sd) )
  15844. script_pushint(st,(sd->chatID != 0));
  15845. else
  15846. script_pushint(st,0);
  15847. return SCRIPT_CMD_SUCCESS;
  15848. }
  15849. BUILDIN_FUNC(checkidle)
  15850. {
  15851. TBL_PC *sd = NULL;
  15852. if( script_nick2sd(2,sd) )
  15853. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15854. else
  15855. script_pushint(st, 0);
  15856. return SCRIPT_CMD_SUCCESS;
  15857. }
  15858. BUILDIN_FUNC(checkidlehom)
  15859. {
  15860. TBL_PC *sd = NULL;
  15861. if( script_nick2sd(2,sd) )
  15862. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15863. else
  15864. script_pushint(st, 0);
  15865. return SCRIPT_CMD_SUCCESS;
  15866. }
  15867. BUILDIN_FUNC(checkidlemer)
  15868. {
  15869. TBL_PC *sd;
  15870. if( script_nick2sd(2,sd) )
  15871. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_mer));
  15872. else
  15873. script_pushint(st, 0);
  15874. return SCRIPT_CMD_SUCCESS;
  15875. }
  15876. BUILDIN_FUNC(searchitem)
  15877. {
  15878. script_data* data = script_getdata(st, 2);
  15879. const char *name = reference_getname(data);
  15880. if( !data_isreference(data) )
  15881. {
  15882. ShowError("buildin_searchitem: Argument %s is not a variable.\n", name);
  15883. script_reportdata(data);
  15884. st->state = END;
  15885. return SCRIPT_CMD_FAILURE;// not a variable
  15886. }
  15887. if( is_string_variable(name) )
  15888. {// string array
  15889. ShowError("buildin_searchitem: Argument %s is not an integer array.\n", name);
  15890. script_reportdata(data);
  15891. st->state = END;
  15892. return SCRIPT_CMD_FAILURE;// not supported
  15893. }
  15894. map_session_data *sd = nullptr;
  15895. if (not_server_variable(*name) && !script_rid2sd(sd))
  15896. {
  15897. return SCRIPT_CMD_SUCCESS;// no player attached
  15898. }
  15899. const char *itemname = script_getstr(st, 3);
  15900. std::map<t_itemid, std::shared_ptr<item_data>> items;
  15901. itemdb_searchname_array(items, MAX_SEARCH, itemname);
  15902. if (!items.empty()) {
  15903. int32 id = reference_getid(data);
  15904. int32 start = reference_getindex(data);
  15905. for (const auto &it : items) { // Set array
  15906. set_reg_num(st, sd, reference_uid(id, start), name, it.first, reference_getref(data));
  15907. start++;
  15908. }
  15909. }
  15910. script_pushint64(st, items.size());
  15911. return SCRIPT_CMD_SUCCESS;
  15912. }
  15913. // [zBuffer] List of player cont commands --->
  15914. BUILDIN_FUNC(rid2name)
  15915. {
  15916. struct block_list *bl = NULL;
  15917. int rid = script_getnum(st,2);
  15918. if((bl = map_id2bl(rid)))
  15919. {
  15920. switch(bl->type) {
  15921. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15922. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15923. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15924. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15925. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15926. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name.c_str()); break;
  15927. default:
  15928. ShowError("buildin_rid2name: BL type unknown.\n");
  15929. script_pushconststr(st,"");
  15930. break;
  15931. }
  15932. } else {
  15933. ShowError("buildin_rid2name: invalid RID\n");
  15934. script_pushconststr(st,"(null)");
  15935. }
  15936. return SCRIPT_CMD_SUCCESS;
  15937. }
  15938. /**
  15939. * Toggle a unit from moving.
  15940. * pcblockmove(<unit_id>,<option>);
  15941. */
  15942. BUILDIN_FUNC(pcblockmove)
  15943. {
  15944. struct block_list *bl = NULL;
  15945. if (script_getnum(st, 2))
  15946. bl = map_id2bl(script_getnum(st,2));
  15947. else
  15948. bl = map_id2bl(st->rid);
  15949. if (bl) {
  15950. struct unit_data *ud = unit_bl2ud(bl);
  15951. if (ud)
  15952. ud->state.blockedmove = script_getnum(st,3) > 0;
  15953. }
  15954. return SCRIPT_CMD_SUCCESS;
  15955. }
  15956. /**
  15957. * Toggle a unit from casting skills.
  15958. * pcblockskill(<unit_id>,<option>);
  15959. */
  15960. BUILDIN_FUNC(pcblockskill)
  15961. {
  15962. struct block_list *bl = NULL;
  15963. if (script_getnum(st, 2))
  15964. bl = map_id2bl(script_getnum(st,2));
  15965. else
  15966. bl = map_id2bl(st->rid);
  15967. if (bl) {
  15968. struct unit_data *ud = unit_bl2ud(bl);
  15969. if (ud)
  15970. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15971. }
  15972. return SCRIPT_CMD_SUCCESS;
  15973. }
  15974. BUILDIN_FUNC(setpcblock)
  15975. {
  15976. TBL_PC *sd;
  15977. if (script_mapid2sd(4, sd)) {
  15978. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  15979. if (script_getnum(st, 3) > 0)
  15980. sd->state.block_action |= type;
  15981. else
  15982. sd->state.block_action &= ~type;
  15983. }
  15984. return SCRIPT_CMD_SUCCESS;
  15985. }
  15986. BUILDIN_FUNC(getpcblock)
  15987. {
  15988. TBL_PC *sd;
  15989. if (script_mapid2sd(2, sd))
  15990. script_pushint(st, sd->state.block_action);
  15991. else
  15992. script_pushint(st, 0);
  15993. return SCRIPT_CMD_SUCCESS;
  15994. }
  15995. BUILDIN_FUNC(pcfollow)
  15996. {
  15997. TBL_PC *sd;
  15998. if( script_mapid2sd(2,sd) )
  15999. pc_follow(sd, script_getnum(st,3));
  16000. return SCRIPT_CMD_SUCCESS;
  16001. }
  16002. BUILDIN_FUNC(pcstopfollow)
  16003. {
  16004. TBL_PC *sd;
  16005. if(script_mapid2sd(2,sd))
  16006. pc_stop_following(sd);
  16007. return SCRIPT_CMD_SUCCESS;
  16008. }
  16009. // <--- [zBuffer] List of player cont commands
  16010. // [zBuffer] List of unit control commands --->
  16011. /// Checks to see if the unit exists.
  16012. ///
  16013. /// unitexists <unit id>;
  16014. BUILDIN_FUNC(unitexists)
  16015. {
  16016. struct block_list* bl;
  16017. bl = map_id2bl(script_getnum(st, 2));
  16018. if (!bl)
  16019. script_pushint(st, false);
  16020. else
  16021. script_pushint(st, true);
  16022. return SCRIPT_CMD_SUCCESS;
  16023. }
  16024. /// Gets the type of the given Game ID.
  16025. ///
  16026. /// getunittype <unit id>;
  16027. BUILDIN_FUNC(getunittype)
  16028. {
  16029. struct block_list* bl;
  16030. int value = 0;
  16031. if(!script_rid2bl(2,bl))
  16032. {
  16033. script_pushint(st, -1);
  16034. return SCRIPT_CMD_FAILURE;
  16035. }
  16036. switch (bl->type) {
  16037. case BL_PC:
  16038. case BL_NPC:
  16039. case BL_PET:
  16040. case BL_MOB:
  16041. case BL_HOM:
  16042. case BL_MER:
  16043. case BL_ELEM:
  16044. value = bl->type;
  16045. break;
  16046. default:
  16047. value = -1;
  16048. break;
  16049. }
  16050. script_pushint(st, value);
  16051. return SCRIPT_CMD_SUCCESS;
  16052. }
  16053. /// Gets specific live information of a bl.
  16054. ///
  16055. /// getunitdata <unit id>,<arrayname>;
  16056. BUILDIN_FUNC(getunitdata)
  16057. {
  16058. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  16059. struct block_list* bl;
  16060. TBL_MOB* md = NULL;
  16061. TBL_HOM* hd = NULL;
  16062. TBL_MER* mc = NULL;
  16063. TBL_PET* pd = NULL;
  16064. TBL_ELEM* ed = NULL;
  16065. TBL_NPC* nd = NULL;
  16066. char* name;
  16067. struct script_data *data = script_getdata(st, 3);
  16068. if (!data_isreference(data)) {
  16069. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  16070. return SCRIPT_CMD_FAILURE;
  16071. }
  16072. if(!script_rid2bl(2,bl))
  16073. {
  16074. script_pushint(st, -1);
  16075. return SCRIPT_CMD_FAILURE;
  16076. }
  16077. switch (bl->type) {
  16078. case BL_MOB: md = map_id2md(bl->id); break;
  16079. case BL_HOM: hd = map_id2hd(bl->id); break;
  16080. case BL_PET: pd = map_id2pd(bl->id); break;
  16081. case BL_MER: mc = map_id2mc(bl->id); break;
  16082. case BL_ELEM: ed = map_id2ed(bl->id); break;
  16083. case BL_NPC: nd = map_id2nd(bl->id); break;
  16084. default:
  16085. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  16086. return SCRIPT_CMD_FAILURE;
  16087. }
  16088. name = reference_getname(data);
  16089. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  16090. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  16091. return SCRIPT_CMD_FAILURE;
  16092. }
  16093. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  16094. switch(bl->type) {
  16095. case BL_MOB:
  16096. if (!md) {
  16097. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  16098. return SCRIPT_CMD_FAILURE;
  16099. }
  16100. getunitdata_sub(UMOB_SIZE, md->status.size);
  16101. getunitdata_sub(UMOB_LEVEL, md->level);
  16102. getunitdata_sub(UMOB_HP, md->status.hp);
  16103. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  16104. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  16105. getunitdata_sub(UMOB_MAPID, md->bl.m);
  16106. getunitdata_sub(UMOB_X, md->bl.x);
  16107. getunitdata_sub(UMOB_Y, md->bl.y);
  16108. getunitdata_sub(UMOB_SPEED, md->status.speed);
  16109. getunitdata_sub(UMOB_MODE, md->status.mode);
  16110. getunitdata_sub(UMOB_AI, md->special_state.ai);
  16111. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  16112. getunitdata_sub(UMOB_SEX, md->vd->sex);
  16113. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  16114. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  16115. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  16116. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  16117. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  16118. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  16119. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  16120. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  16121. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  16122. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  16123. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  16124. getunitdata_sub(UMOB_STR, md->status.str);
  16125. getunitdata_sub(UMOB_AGI, md->status.agi);
  16126. getunitdata_sub(UMOB_VIT, md->status.vit);
  16127. getunitdata_sub(UMOB_INT, md->status.int_);
  16128. getunitdata_sub(UMOB_DEX, md->status.dex);
  16129. getunitdata_sub(UMOB_LUK, md->status.luk);
  16130. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  16131. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  16132. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  16133. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  16134. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  16135. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  16136. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  16137. getunitdata_sub(UMOB_DEF, md->status.def);
  16138. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  16139. getunitdata_sub(UMOB_HIT, md->status.hit);
  16140. getunitdata_sub(UMOB_FLEE, md->status.flee);
  16141. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  16142. getunitdata_sub(UMOB_CRIT, md->status.cri);
  16143. getunitdata_sub(UMOB_RACE, md->status.race);
  16144. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  16145. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  16146. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  16147. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  16148. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  16149. getunitdata_sub(UMOB_TARGETID, md->target_id);
  16150. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  16151. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  16152. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  16153. getunitdata_sub(UMOB_IGNORE_CELL_STACK_LIMIT, md->ud.state.ignore_cell_stack_limit);
  16154. getunitdata_sub(UMOB_RES, md->status.res);
  16155. getunitdata_sub(UMOB_MRES, md->status.mres);
  16156. getunitdata_sub(UMOB_DAMAGETAKEN, md->damagetaken);
  16157. break;
  16158. case BL_HOM:
  16159. if (!hd) {
  16160. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  16161. return SCRIPT_CMD_FAILURE;
  16162. }
  16163. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  16164. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  16165. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  16166. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  16167. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  16168. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  16169. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  16170. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  16171. getunitdata_sub(UHOM_X, hd->bl.x);
  16172. getunitdata_sub(UHOM_Y, hd->bl.y);
  16173. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  16174. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  16175. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  16176. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  16177. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  16178. getunitdata_sub(UHOM_STR, hd->base_status.str);
  16179. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  16180. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  16181. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  16182. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  16183. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  16184. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  16185. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  16186. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  16187. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  16188. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  16189. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  16190. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  16191. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  16192. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  16193. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  16194. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  16195. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  16196. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  16197. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  16198. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  16199. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  16200. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  16201. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  16202. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  16203. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  16204. break;
  16205. case BL_PET:
  16206. if (!pd) {
  16207. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  16208. return SCRIPT_CMD_FAILURE;
  16209. }
  16210. getunitdata_sub(UPET_SIZE, pd->status.size);
  16211. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  16212. getunitdata_sub(UPET_HP, pd->status.hp);
  16213. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  16214. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  16215. getunitdata_sub(UPET_MAPID, pd->bl.m);
  16216. getunitdata_sub(UPET_X, pd->bl.x);
  16217. getunitdata_sub(UPET_Y, pd->bl.y);
  16218. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  16219. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  16220. getunitdata_sub(UPET_SPEED, pd->status.speed);
  16221. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  16222. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  16223. getunitdata_sub(UPET_STR, pd->status.str);
  16224. getunitdata_sub(UPET_AGI, pd->status.agi);
  16225. getunitdata_sub(UPET_VIT, pd->status.vit);
  16226. getunitdata_sub(UPET_INT, pd->status.int_);
  16227. getunitdata_sub(UPET_DEX, pd->status.dex);
  16228. getunitdata_sub(UPET_LUK, pd->status.luk);
  16229. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  16230. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  16231. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  16232. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  16233. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  16234. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  16235. getunitdata_sub(UPET_DEF, pd->status.def);
  16236. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  16237. getunitdata_sub(UPET_HIT, pd->status.hit);
  16238. getunitdata_sub(UPET_FLEE, pd->status.flee);
  16239. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  16240. getunitdata_sub(UPET_CRIT, pd->status.cri);
  16241. getunitdata_sub(UPET_RACE, pd->status.race);
  16242. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  16243. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  16244. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  16245. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  16246. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  16247. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  16248. break;
  16249. case BL_MER:
  16250. if (!mc) {
  16251. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  16252. return SCRIPT_CMD_FAILURE;
  16253. }
  16254. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  16255. getunitdata_sub(UMER_HP, mc->base_status.hp);
  16256. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  16257. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  16258. getunitdata_sub(UMER_MAPID, mc->bl.m);
  16259. getunitdata_sub(UMER_X, mc->bl.x);
  16260. getunitdata_sub(UMER_Y, mc->bl.y);
  16261. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  16262. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  16263. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  16264. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  16265. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  16266. getunitdata_sub(UMER_STR, mc->base_status.str);
  16267. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  16268. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  16269. getunitdata_sub(UMER_INT, mc->base_status.int_);
  16270. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  16271. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  16272. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  16273. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  16274. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  16275. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  16276. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  16277. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  16278. getunitdata_sub(UMER_DEF, mc->base_status.def);
  16279. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  16280. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  16281. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  16282. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  16283. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  16284. getunitdata_sub(UMER_RACE, mc->base_status.race);
  16285. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  16286. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  16287. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  16288. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  16289. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  16290. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  16291. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  16292. break;
  16293. case BL_ELEM:
  16294. if (!ed) {
  16295. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  16296. return SCRIPT_CMD_FAILURE;
  16297. }
  16298. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  16299. getunitdata_sub(UELE_HP, ed->elemental.hp);
  16300. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  16301. getunitdata_sub(UELE_SP, ed->elemental.sp);
  16302. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  16303. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  16304. getunitdata_sub(UELE_MAPID, ed->bl.m);
  16305. getunitdata_sub(UELE_X, ed->bl.x);
  16306. getunitdata_sub(UELE_Y, ed->bl.y);
  16307. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  16308. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  16309. getunitdata_sub(UELE_SP, ed->base_status.speed);
  16310. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  16311. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  16312. getunitdata_sub(UELE_STR, ed->base_status.str);
  16313. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  16314. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  16315. getunitdata_sub(UELE_INT, ed->base_status.int_);
  16316. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  16317. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  16318. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  16319. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  16320. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  16321. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  16322. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  16323. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  16324. getunitdata_sub(UELE_DEF, ed->base_status.def);
  16325. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  16326. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  16327. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  16328. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  16329. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  16330. getunitdata_sub(UELE_RACE, ed->base_status.race);
  16331. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  16332. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  16333. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  16334. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  16335. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  16336. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  16337. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  16338. break;
  16339. case BL_NPC:
  16340. if (!nd) {
  16341. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  16342. return SCRIPT_CMD_FAILURE;
  16343. }
  16344. getunitdata_sub(UNPC_LEVEL, nd->level);
  16345. getunitdata_sub(UNPC_HP, nd->status.hp);
  16346. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  16347. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  16348. getunitdata_sub(UNPC_X, nd->bl.x);
  16349. getunitdata_sub(UNPC_Y, nd->bl.y);
  16350. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  16351. getunitdata_sub(UNPC_STR, nd->status.str);
  16352. getunitdata_sub(UNPC_AGI, nd->status.agi);
  16353. getunitdata_sub(UNPC_VIT, nd->status.vit);
  16354. getunitdata_sub(UNPC_INT, nd->status.int_);
  16355. getunitdata_sub(UNPC_DEX, nd->status.dex);
  16356. getunitdata_sub(UNPC_LUK, nd->status.luk);
  16357. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  16358. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  16359. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  16360. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  16361. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  16362. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  16363. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  16364. getunitdata_sub(UNPC_DEF, nd->status.def);
  16365. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  16366. getunitdata_sub(UNPC_HIT, nd->status.hit);
  16367. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  16368. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  16369. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  16370. getunitdata_sub(UNPC_RACE, nd->status.race);
  16371. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  16372. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  16373. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  16374. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  16375. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  16376. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  16377. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  16378. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  16379. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  16380. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  16381. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  16382. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  16383. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  16384. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  16385. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  16386. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  16387. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  16388. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  16389. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  16390. break;
  16391. default:
  16392. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  16393. return SCRIPT_CMD_FAILURE;
  16394. }
  16395. #undef getunitdata_sub
  16396. return SCRIPT_CMD_SUCCESS;
  16397. }
  16398. /// Changes the live data of a bl.
  16399. ///
  16400. /// setunitdata <unit id>,<type>,<value>;
  16401. BUILDIN_FUNC(setunitdata)
  16402. {
  16403. struct block_list* bl;
  16404. if(!script_rid2bl(2,bl))
  16405. {
  16406. script_pushint(st, -1);
  16407. return SCRIPT_CMD_FAILURE;
  16408. }
  16409. TBL_MOB* md;
  16410. TBL_HOM* hd;
  16411. TBL_MER* mc;
  16412. TBL_PET* pd;
  16413. TBL_ELEM* ed;
  16414. TBL_NPC* nd;
  16415. switch (bl->type) {
  16416. case BL_MOB: md = map_id2md(bl->id); break;
  16417. case BL_HOM: hd = map_id2hd(bl->id); break;
  16418. case BL_PET: pd = map_id2pd(bl->id); break;
  16419. case BL_MER: mc = map_id2mc(bl->id); break;
  16420. case BL_ELEM: ed = map_id2ed(bl->id); break;
  16421. case BL_NPC:
  16422. nd = map_id2nd(bl->id);
  16423. if (!nd->status.hp)
  16424. status_calc_npc(nd, SCO_FIRST);
  16425. else
  16426. status_calc_npc(nd, SCO_NONE);
  16427. break;
  16428. default:
  16429. ShowError("buildin_setunitdata: Invalid object!\n");
  16430. return SCRIPT_CMD_FAILURE;
  16431. }
  16432. int type = script_getnum(st, 3), value = 0;
  16433. const char *mapname = NULL;
  16434. bool calc_status = false;
  16435. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  16436. mapname = script_getstr(st, 4);
  16437. else
  16438. value = script_getnum(st, 4);
  16439. switch (bl->type) {
  16440. case BL_MOB:
  16441. if (!md) {
  16442. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  16443. return SCRIPT_CMD_FAILURE;
  16444. }
  16445. if (!md->base_status) {
  16446. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  16447. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  16448. }
  16449. // Check if the view data will be modified
  16450. switch( type ){
  16451. case UMOB_SEX:
  16452. //case UMOB_CLASS: // Called by status_set_viewdata
  16453. case UMOB_HAIRSTYLE:
  16454. case UMOB_HAIRCOLOR:
  16455. case UMOB_HEADBOTTOM:
  16456. case UMOB_HEADMIDDLE:
  16457. case UMOB_HEADTOP:
  16458. case UMOB_CLOTHCOLOR:
  16459. case UMOB_SHIELD:
  16460. case UMOB_WEAPON:
  16461. case UMOB_ROBE:
  16462. case UMOB_BODY2:
  16463. mob_set_dynamic_viewdata( md );
  16464. break;
  16465. }
  16466. switch (type) {
  16467. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  16468. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  16469. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  16470. case UMOB_MAXHP: md->base_status->hp = md->base_status->max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  16471. case UMOB_MASTERAID: md->master_id = value; break;
  16472. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16473. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16474. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  16475. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16476. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  16477. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  16478. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  16479. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  16480. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  16481. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16482. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16483. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16484. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16485. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16486. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16487. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16488. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16489. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16490. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16491. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16492. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16493. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16494. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16495. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16496. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16497. case UMOB_SLAVECPYMSTRMD:
  16498. if (value > 0) {
  16499. TBL_MOB *md2 = NULL;
  16500. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  16501. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  16502. break;
  16503. }
  16504. md->base_status->mode = md2->status.mode;
  16505. md->state.copy_master_mode = 1;
  16506. } else
  16507. md->state.copy_master_mode = 0;
  16508. calc_status = true;
  16509. break;
  16510. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  16511. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  16512. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  16513. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16514. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  16515. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  16516. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  16517. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  16518. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  16519. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  16520. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  16521. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  16522. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  16523. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  16524. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  16525. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  16526. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  16527. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  16528. case UMOB_TARGETID: {
  16529. if (value==0) {
  16530. mob_unlocktarget(md,gettick());
  16531. break;
  16532. }
  16533. struct block_list* target = map_id2bl(value);
  16534. if (!target) {
  16535. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  16536. return SCRIPT_CMD_FAILURE;
  16537. }
  16538. mob_target(md,target,0);
  16539. break;
  16540. }
  16541. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16542. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16543. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  16544. case UMOB_IGNORE_CELL_STACK_LIMIT: md->ud.state.ignore_cell_stack_limit = value > 0; break;
  16545. case UMOB_RES: md->base_status->res = (short)value; calc_status = true; break;
  16546. case UMOB_MRES: md->base_status->mres = (short)value; calc_status = true; break;
  16547. case UMOB_DAMAGETAKEN: md->damagetaken = (unsigned short)value; break;
  16548. default:
  16549. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  16550. return SCRIPT_CMD_FAILURE;
  16551. }
  16552. if (calc_status)
  16553. status_calc_bl_(&md->bl, status_db.getSCB_BATTLE());
  16554. break;
  16555. case BL_HOM:
  16556. if (!hd) {
  16557. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  16558. return SCRIPT_CMD_FAILURE;
  16559. }
  16560. switch (type) {
  16561. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  16562. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  16563. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16564. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16565. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16566. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16567. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  16568. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16569. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  16570. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  16571. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  16572. case UHOM_INTIMACY: hom_increase_intimacy(hd, (unsigned int)value); clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_INTIMATE, hd->homunculus.intimacy / 100); break;
  16573. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16574. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16575. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16576. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16577. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16578. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16579. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16580. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16581. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16582. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  16583. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16584. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16585. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16586. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16587. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16588. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  16589. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  16590. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  16591. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  16592. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  16593. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  16594. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  16595. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16596. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16597. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  16598. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  16599. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  16600. case UHOM_TARGETID: {
  16601. if (value==0) {
  16602. unit_stop_attack(&hd->bl);
  16603. break;
  16604. }
  16605. struct block_list* target = map_id2bl(value);
  16606. if (!target) {
  16607. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  16608. return SCRIPT_CMD_FAILURE;
  16609. }
  16610. unit_attack(&hd->bl, target->id, 1);
  16611. break;
  16612. }
  16613. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  16614. default:
  16615. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  16616. return SCRIPT_CMD_FAILURE;
  16617. }
  16618. if (calc_status)
  16619. status_calc_bl_(&hd->bl, status_db.getSCB_BATTLE());
  16620. break;
  16621. case BL_PET:
  16622. if (!pd) {
  16623. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  16624. return SCRIPT_CMD_FAILURE;
  16625. }
  16626. switch (type) {
  16627. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  16628. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  16629. case UPET_HP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16630. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16631. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  16632. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16633. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16634. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  16635. 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;
  16636. case UPET_INTIMACY: pet_set_intimate(pd, (unsigned int)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  16637. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16638. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16639. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16640. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16641. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16642. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16643. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16644. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16645. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16646. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  16647. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  16648. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  16649. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  16650. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  16651. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  16652. case UPET_DEF: pd->status.def = (defType)value; break;
  16653. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  16654. case UPET_HIT: pd->status.hit = (short)value; break;
  16655. case UPET_FLEE: pd->status.flee = (short)value; break;
  16656. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16657. case UPET_CRIT: pd->status.cri = (short)value; break;
  16658. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16659. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16660. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16661. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16662. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16663. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16664. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16665. default:
  16666. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16667. return SCRIPT_CMD_FAILURE;
  16668. }
  16669. break;
  16670. case BL_MER:
  16671. if (!mc) {
  16672. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  16673. return SCRIPT_CMD_FAILURE;
  16674. }
  16675. switch (type) {
  16676. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16677. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16678. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16679. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16680. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16681. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16682. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16683. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  16684. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  16685. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16686. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16687. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16688. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16689. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16690. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16691. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16692. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16693. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16694. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16695. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16696. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16697. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16698. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16699. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16700. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16701. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16702. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16703. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16704. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16705. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16706. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16707. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16708. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16709. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16710. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16711. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16712. case UMER_TARGETID: {
  16713. if (value==0) {
  16714. unit_stop_attack(&mc->bl);
  16715. break;
  16716. }
  16717. struct block_list* target = map_id2bl(value);
  16718. if (!target) {
  16719. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16720. return SCRIPT_CMD_FAILURE;
  16721. }
  16722. unit_attack(&mc->bl, target->id, 1);
  16723. break;
  16724. }
  16725. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16726. default:
  16727. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16728. return SCRIPT_CMD_FAILURE;
  16729. }
  16730. if (calc_status)
  16731. status_calc_bl_(&mc->bl, status_db.getSCB_BATTLE());
  16732. break;
  16733. case BL_ELEM:
  16734. if (!ed) {
  16735. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16736. return SCRIPT_CMD_FAILURE;
  16737. }
  16738. switch (type) {
  16739. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16740. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16741. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16742. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16743. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16744. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16745. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16746. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16747. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16748. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16749. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16750. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16751. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16752. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16753. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16754. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16755. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16756. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16757. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16758. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16759. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16760. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16761. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16762. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16763. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16764. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16765. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16766. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16767. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16768. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16769. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16770. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16771. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16772. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16773. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16774. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16775. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16776. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16777. case UELE_TARGETID: {
  16778. if (value==0) {
  16779. unit_stop_attack(&ed->bl);
  16780. break;
  16781. }
  16782. struct block_list* target = map_id2bl(value);
  16783. if (!target) {
  16784. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16785. return SCRIPT_CMD_FAILURE;
  16786. }
  16787. elemental_change_mode(ed, EL_MODE_AGGRESSIVE);
  16788. unit_attack(&ed->bl, target->id, 1);
  16789. break;
  16790. }
  16791. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16792. default:
  16793. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16794. return SCRIPT_CMD_FAILURE;
  16795. }
  16796. if (calc_status)
  16797. status_calc_bl_(&ed->bl, status_db.getSCB_BATTLE());
  16798. break;
  16799. case BL_NPC:
  16800. if (!nd) {
  16801. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16802. return SCRIPT_CMD_FAILURE;
  16803. }
  16804. switch (type) {
  16805. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16806. case UNPC_HP: nd->status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16807. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16808. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16809. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16810. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16811. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16812. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16813. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16814. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16815. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16816. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16817. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16818. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16819. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16820. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16821. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16822. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16823. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16824. case UNPC_DEF: nd->status.def = (defType)value; break;
  16825. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16826. case UNPC_HIT: nd->status.hit = (short)value; break;
  16827. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16828. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16829. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16830. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16831. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16832. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16833. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16834. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16835. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16836. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16837. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16838. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16839. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16840. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16841. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16842. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16843. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16844. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16845. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16846. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16847. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16848. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16849. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16850. default:
  16851. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16852. return SCRIPT_CMD_FAILURE;
  16853. }
  16854. break;
  16855. default:
  16856. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16857. return SCRIPT_CMD_FAILURE;
  16858. }
  16859. // Client information updates
  16860. switch (bl->type) {
  16861. case BL_HOM:
  16862. clif_send_homdata(hd->master, SP_ACK, 0);
  16863. break;
  16864. case BL_PET:
  16865. clif_send_petstatus(pd->master);
  16866. break;
  16867. case BL_MER:
  16868. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16869. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16870. break;
  16871. case BL_ELEM:
  16872. clif_elemental_info(ed->master);
  16873. break;
  16874. }
  16875. return SCRIPT_CMD_SUCCESS;
  16876. }
  16877. /// Gets the name of a bl.
  16878. /// Supported types are [MOB|HOM|PET|NPC].
  16879. /// MER and ELEM don't support custom names.
  16880. ///
  16881. /// getunitname <unit id>;
  16882. BUILDIN_FUNC(getunitname)
  16883. {
  16884. struct block_list* bl = NULL;
  16885. if(!script_rid2bl(2,bl)){
  16886. script_pushconststr(st, "Unknown");
  16887. return SCRIPT_CMD_FAILURE;
  16888. }
  16889. script_pushstrcopy(st, status_get_name(bl));
  16890. return SCRIPT_CMD_SUCCESS;
  16891. }
  16892. /// Changes the name of a bl.
  16893. /// Supported types are [MOB|HOM|PET].
  16894. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16895. ///
  16896. /// setunitname <unit id>,<name>;
  16897. BUILDIN_FUNC(setunitname)
  16898. {
  16899. struct block_list* bl = NULL;
  16900. TBL_MOB* md = NULL;
  16901. TBL_HOM* hd = NULL;
  16902. TBL_PET* pd = NULL;
  16903. if(!script_rid2bl(2,bl))
  16904. {
  16905. script_pushconststr(st, "Unknown");
  16906. return SCRIPT_CMD_FAILURE;
  16907. }
  16908. switch (bl->type) {
  16909. case BL_MOB: md = map_id2md(bl->id); break;
  16910. case BL_HOM: hd = map_id2hd(bl->id); break;
  16911. case BL_PET: pd = map_id2pd(bl->id); break;
  16912. default:
  16913. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16914. return SCRIPT_CMD_FAILURE;
  16915. }
  16916. switch (bl->type) {
  16917. case BL_MOB:
  16918. if (!md) {
  16919. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16920. return SCRIPT_CMD_FAILURE;
  16921. }
  16922. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16923. break;
  16924. case BL_HOM:
  16925. if (!hd) {
  16926. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16927. return SCRIPT_CMD_FAILURE;
  16928. }
  16929. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16930. break;
  16931. case BL_PET:
  16932. if (!pd) {
  16933. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16934. return SCRIPT_CMD_FAILURE;
  16935. }
  16936. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16937. break;
  16938. default:
  16939. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16940. return SCRIPT_CMD_FAILURE;
  16941. }
  16942. clif_name_area(bl); // Send update to client.
  16943. return SCRIPT_CMD_SUCCESS;
  16944. }
  16945. /**
  16946. * Sets a unit's title.
  16947. * setunittitle <GID>,<title>;
  16948. */
  16949. BUILDIN_FUNC(setunittitle)
  16950. {
  16951. int gid = script_getnum(st, 2);
  16952. block_list *bl = map_id2bl(gid);
  16953. if (bl == nullptr) {
  16954. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16955. return SCRIPT_CMD_FAILURE;
  16956. }
  16957. unit_data *ud = unit_bl2ud(bl);
  16958. if (ud == nullptr) {
  16959. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16960. return SCRIPT_CMD_FAILURE;
  16961. }
  16962. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16963. clif_name_area(bl);
  16964. return SCRIPT_CMD_SUCCESS;
  16965. }
  16966. /**
  16967. * Gets a unit's title.
  16968. * getunittitle <GID>;
  16969. */
  16970. BUILDIN_FUNC(getunittitle)
  16971. {
  16972. int gid = script_getnum(st, 2);
  16973. block_list *bl = map_id2bl(gid);
  16974. if (bl == nullptr) {
  16975. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16976. return SCRIPT_CMD_FAILURE;
  16977. }
  16978. unit_data *ud = unit_bl2ud(bl);
  16979. if (ud == nullptr) {
  16980. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16981. return SCRIPT_CMD_FAILURE;
  16982. }
  16983. script_pushstrcopy(st, ud->title);
  16984. return SCRIPT_CMD_SUCCESS;
  16985. }
  16986. /// Makes the unit walk to target position or map.
  16987. /// Returns if it was successful.
  16988. ///
  16989. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16990. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16991. BUILDIN_FUNC(unitwalk)
  16992. {
  16993. struct block_list* bl;
  16994. struct unit_data *ud = NULL;
  16995. const char *cmd = script_getfuncname(st), *done_label = "";
  16996. uint8 off = 5;
  16997. if(!script_rid2bl(2,bl))
  16998. {
  16999. script_pushint(st, 0);
  17000. return SCRIPT_CMD_FAILURE;
  17001. }
  17002. ud = unit_bl2ud(bl);
  17003. // Unit was already forced to walk.
  17004. if (ud != nullptr && ud->state.force_walk) {
  17005. script_pushint(st, 0);
  17006. ShowWarning("buildin_%s: Unit has already been forced to walk and not reached it's destination yet.\n", cmd);
  17007. return SCRIPT_CMD_FAILURE;
  17008. }
  17009. if (bl->type == BL_NPC) {
  17010. if (!((TBL_NPC*)bl)->status.hp)
  17011. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17012. else
  17013. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17014. }
  17015. if (!strcmp(cmd,"unitwalk")) {
  17016. int x = script_getnum(st,3);
  17017. int y = script_getnum(st,4);
  17018. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0))) {
  17019. if (ud != nullptr)
  17020. ud->state.force_walk = true;
  17021. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  17022. }
  17023. } else {
  17024. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  17025. if (!tbl) {
  17026. ShowError("buildin_unitwalk: Bad target destination.\n");
  17027. script_pushint(st, 0);
  17028. return SCRIPT_CMD_FAILURE;
  17029. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL))) {
  17030. if (ud != nullptr)
  17031. ud->state.force_walk = true;
  17032. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  17033. }
  17034. off = 4;
  17035. }
  17036. if (ud && script_hasdata(st, off)) {
  17037. done_label = script_getstr(st, off);
  17038. check_event(st, done_label);
  17039. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  17040. }
  17041. return SCRIPT_CMD_SUCCESS;
  17042. }
  17043. /// Kills the unit.
  17044. ///
  17045. /// unitkill <unit_id>;
  17046. BUILDIN_FUNC(unitkill)
  17047. {
  17048. struct block_list* bl;
  17049. if(script_rid2bl(2,bl))
  17050. status_kill(bl);
  17051. return SCRIPT_CMD_SUCCESS;
  17052. }
  17053. /// Warps the unit to the target position in the target map.
  17054. /// Returns if it was successful.
  17055. ///
  17056. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  17057. BUILDIN_FUNC(unitwarp)
  17058. {
  17059. int map_idx;
  17060. short x;
  17061. short y;
  17062. struct block_list* bl;
  17063. const char *mapname;
  17064. mapname = script_getstr(st, 3);
  17065. x = (short)script_getnum(st,4);
  17066. y = (short)script_getnum(st,5);
  17067. if(!script_rid2bl(2,bl))
  17068. {
  17069. script_pushint(st, 0);
  17070. return SCRIPT_CMD_SUCCESS;
  17071. }
  17072. if (!strcmp(mapname,"this"))
  17073. map_idx = bl?bl->m:-1;
  17074. else
  17075. map_idx = map_mapname2mapid(mapname);
  17076. if (map_idx >= 0 && bl != NULL)
  17077. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  17078. else
  17079. script_pushint(st, 0);
  17080. return SCRIPT_CMD_SUCCESS;
  17081. }
  17082. /// Makes the unit attack the target.
  17083. /// If the unit is a player and <action type> is not 0, it does a continuous
  17084. /// attack instead of a single attack.
  17085. /// Returns if the request was successful.
  17086. ///
  17087. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  17088. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  17089. BUILDIN_FUNC(unitattack)
  17090. {
  17091. struct block_list* unit_bl;
  17092. struct block_list* target_bl = NULL;
  17093. int actiontype = 0;
  17094. if (!script_rid2bl(2,unit_bl)) {
  17095. script_pushint(st, false);
  17096. return SCRIPT_CMD_FAILURE;
  17097. }
  17098. if (script_isstring(st, 3)) {
  17099. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  17100. if( sd != NULL )
  17101. target_bl = &sd->bl;
  17102. } else
  17103. target_bl = map_id2bl(script_getnum(st, 3));
  17104. if (!target_bl) {
  17105. script_pushint(st, false);
  17106. return SCRIPT_CMD_FAILURE;
  17107. }
  17108. if (script_hasdata(st,4))
  17109. actiontype = script_getnum(st,4);
  17110. switch(unit_bl->type) {
  17111. case BL_PC: {
  17112. map_session_data* sd = (map_session_data*)unit_bl;
  17113. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  17114. script_pushint(st, sd->ud.target == target_bl->id);
  17115. return SCRIPT_CMD_SUCCESS;
  17116. }
  17117. case BL_MOB:
  17118. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  17119. break;
  17120. case BL_PET:
  17121. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  17122. break;
  17123. default:
  17124. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  17125. script_pushint(st, false);
  17126. return SCRIPT_CMD_FAILURE;
  17127. }
  17128. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  17129. return SCRIPT_CMD_SUCCESS;
  17130. }
  17131. /// Makes the unit stop attacking.
  17132. ///
  17133. /// unitstopattack <unit_id>;
  17134. BUILDIN_FUNC(unitstopattack)
  17135. {
  17136. struct block_list* bl;
  17137. if(script_rid2bl(2,bl))
  17138. {
  17139. unit_stop_attack(bl);
  17140. if (bl->type == BL_MOB)
  17141. ((TBL_MOB*)bl)->target_id = 0;
  17142. }
  17143. return SCRIPT_CMD_SUCCESS;
  17144. }
  17145. /// Makes the unit stop walking.
  17146. ///
  17147. /// unitstopwalk <unit_id>{,<flag>};
  17148. BUILDIN_FUNC(unitstopwalk)
  17149. {
  17150. struct block_list* bl;
  17151. int flag = USW_NONE;
  17152. if (script_hasdata(st, 3))
  17153. flag = script_getnum(st, 3);
  17154. if(script_rid2bl(2,bl)) {
  17155. unit_data *ud = unit_bl2ud(bl);
  17156. if (ud != nullptr)
  17157. ud->state.force_walk = false;
  17158. if (unit_stop_walking(bl, flag) == 0 && flag != USW_FORCE_STOP) {
  17159. ShowWarning("buildin_unitstopwalk: Unable to find unit or unit is not walking.\n");
  17160. return SCRIPT_CMD_FAILURE;
  17161. }
  17162. return SCRIPT_CMD_SUCCESS;
  17163. } else {
  17164. return SCRIPT_CMD_FAILURE;
  17165. }
  17166. }
  17167. /**
  17168. * Makes the unit say the given message.
  17169. *
  17170. * unittalk <unit_id>,"<message>"{,"<flag>"};
  17171. * @param flag: Specify target
  17172. * bc_area - Message is sent to players in the vicinity of the source (default).
  17173. * bc_self - Message is sent only to player attached.
  17174. */
  17175. BUILDIN_FUNC(unittalk)
  17176. {
  17177. const char* message;
  17178. struct block_list* bl;
  17179. message = script_getstr(st, 3);
  17180. if(script_rid2bl(2,bl))
  17181. {
  17182. send_target target = AREA;
  17183. struct StringBuf sbuf;
  17184. if (script_hasdata(st, 4)) {
  17185. if (script_getnum(st, 4) == BC_SELF) {
  17186. if (map_id2sd(bl->id) == NULL) {
  17187. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  17188. return SCRIPT_CMD_FAILURE;
  17189. }
  17190. target = SELF;
  17191. }
  17192. }
  17193. StringBuf_Init(&sbuf);
  17194. StringBuf_Printf(&sbuf, "%s", message);
  17195. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  17196. StringBuf_Destroy(&sbuf);
  17197. }
  17198. return SCRIPT_CMD_SUCCESS;
  17199. }
  17200. /// Makes the unit cast the skill on the target or self if no target is specified.
  17201. ///
  17202. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  17203. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  17204. BUILDIN_FUNC(unitskilluseid)
  17205. {
  17206. int unit_id, target_id, casttime;
  17207. uint16 skill_id, skill_lv;
  17208. struct block_list* bl;
  17209. unit_id = script_getnum(st,2);
  17210. if (script_isstring(st, 3)) {
  17211. const char *name = script_getstr(st, 3);
  17212. if (!(skill_id = skill_name2id(name))) {
  17213. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  17214. return SCRIPT_CMD_FAILURE;
  17215. }
  17216. } else {
  17217. skill_id = script_getnum(st, 3);
  17218. if (!skill_get_index(skill_id)) {
  17219. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  17220. return SCRIPT_CMD_FAILURE;
  17221. }
  17222. }
  17223. skill_lv = script_getnum(st,4);
  17224. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  17225. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  17226. bool cancel = ( script_hasdata(st,7) ? script_getnum(st,7) > 0 : skill_get_castcancel(skill_id) );
  17227. int msg_id = (script_hasdata(st, 8) ? script_getnum(st, 8) : 0);
  17228. bool ignore_range = (script_hasdata(st, 9) ? script_getnum(st, 9) > 0 : false);
  17229. if(script_rid2bl(2,bl)){
  17230. if (msg_id > 0) {
  17231. if (bl->type != BL_MOB) {
  17232. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  17233. return SCRIPT_CMD_FAILURE;
  17234. }
  17235. TBL_MOB* md = map_id2md(bl->id);
  17236. if (md)
  17237. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  17238. }
  17239. if (bl->type == BL_NPC) {
  17240. if (!((TBL_NPC*)bl)->status.hp)
  17241. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17242. else
  17243. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17244. }
  17245. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel, ignore_range);
  17246. }
  17247. return SCRIPT_CMD_SUCCESS;
  17248. }
  17249. /// Makes the unit cast the skill on the target position.
  17250. ///
  17251. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  17252. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  17253. BUILDIN_FUNC(unitskillusepos)
  17254. {
  17255. int skill_x, skill_y, casttime;
  17256. uint16 skill_id, skill_lv;
  17257. struct block_list* bl;
  17258. if (script_isstring(st, 3)) {
  17259. const char *name = script_getstr(st, 3);
  17260. if (!(skill_id = skill_name2id(name))) {
  17261. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  17262. return SCRIPT_CMD_FAILURE;
  17263. }
  17264. } else {
  17265. skill_id = script_getnum(st, 3);
  17266. if (!skill_get_index(skill_id)) {
  17267. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  17268. return SCRIPT_CMD_FAILURE;
  17269. }
  17270. }
  17271. skill_lv = script_getnum(st,4);
  17272. skill_x = script_getnum(st,5);
  17273. skill_y = script_getnum(st,6);
  17274. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  17275. bool cancel = ( script_hasdata(st,8) ? script_getnum(st,8) > 0 : skill_get_castcancel(skill_id) );
  17276. int msg_id = (script_hasdata(st, 9) ? script_getnum(st, 9) : 0);
  17277. bool ignore_range = (script_hasdata(st, 10) ? script_getnum(st, 10) > 0 : false);
  17278. if(script_rid2bl(2,bl)){
  17279. if (msg_id > 0) {
  17280. if (bl->type != BL_MOB) {
  17281. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  17282. return SCRIPT_CMD_FAILURE;
  17283. }
  17284. TBL_MOB* md = map_id2md(bl->id);
  17285. if (md)
  17286. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  17287. }
  17288. if (bl->type == BL_NPC) {
  17289. if (!((TBL_NPC*)bl)->status.hp)
  17290. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17291. else
  17292. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17293. }
  17294. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel, ignore_range);
  17295. }
  17296. return SCRIPT_CMD_SUCCESS;
  17297. }
  17298. // <--- [zBuffer] List of unit control commands
  17299. /// Pauses the execution of the script, detaching the player
  17300. ///
  17301. /// sleep <milli seconds>;
  17302. BUILDIN_FUNC(sleep)
  17303. {
  17304. // First call(by function call)
  17305. if (st->sleep.tick == 0) {
  17306. int ticks;
  17307. ticks = script_getnum(st, 2);
  17308. if (ticks <= 0) {
  17309. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  17310. return SCRIPT_CMD_FAILURE;
  17311. }
  17312. // detach the player
  17313. script_detach_rid(st);
  17314. // sleep for the target amount of time
  17315. st->state = RERUNLINE;
  17316. st->sleep.tick = ticks;
  17317. // Second call(by timer after sleeping time is over)
  17318. } else {
  17319. // Continue the script
  17320. st->state = RUN;
  17321. st->sleep.tick = 0;
  17322. }
  17323. return SCRIPT_CMD_SUCCESS;
  17324. }
  17325. /// Pauses the execution of the script, keeping the unit attached
  17326. /// Stops the script if no unit is attached
  17327. ///
  17328. /// sleep2(<milli seconds>)
  17329. BUILDIN_FUNC(sleep2)
  17330. {
  17331. // First call(by function call)
  17332. if (st->sleep.tick == 0) {
  17333. int ticks;
  17334. ticks = script_getnum(st, 2);
  17335. if (ticks <= 0) {
  17336. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  17337. return SCRIPT_CMD_FAILURE;
  17338. }
  17339. if (map_id2bl(st->rid) == NULL) {
  17340. ShowError( "buildin_sleep2: no unit is attached\n" );
  17341. return SCRIPT_CMD_FAILURE;
  17342. }
  17343. // sleep for the target amount of time
  17344. st->state = RERUNLINE;
  17345. st->sleep.tick = ticks;
  17346. // Second call(by timer after sleeping time is over)
  17347. } else {
  17348. // Check if the unit is still attached
  17349. // NOTE: This should never happen, since run_script_timer already checks this
  17350. if (map_id2bl(st->rid) == NULL) {
  17351. // The unit is not attached anymore - terminate the script
  17352. st->rid = 0;
  17353. st->state = END;
  17354. } else {
  17355. // The unit is still attached - continue the script
  17356. st->state = RUN;
  17357. st->sleep.tick = 0;
  17358. }
  17359. }
  17360. return SCRIPT_CMD_SUCCESS;
  17361. }
  17362. /// Awakes all the sleep timers of the target npc
  17363. ///
  17364. /// awake "<npc name>";
  17365. BUILDIN_FUNC(awake)
  17366. {
  17367. DBIterator *iter;
  17368. struct script_state *tst;
  17369. struct npc_data* nd;
  17370. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  17371. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  17372. return SCRIPT_CMD_FAILURE;
  17373. }
  17374. int rid = st->rid;
  17375. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  17376. if( rid ){
  17377. script_detach_rid(st);
  17378. }
  17379. iter = db_iterator(st_db);
  17380. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  17381. if (tst->oid == nd->bl.id) {
  17382. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  17383. continue;
  17384. }
  17385. delete_timer(tst->sleep.timer, run_script_timer);
  17386. // Trigger the timer function
  17387. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  17388. }
  17389. }
  17390. dbi_destroy(iter);
  17391. // If a player had been attached, now is the time to restore it
  17392. if( rid ){
  17393. st->rid = rid;
  17394. script_attach_state(st);
  17395. }
  17396. return SCRIPT_CMD_SUCCESS;
  17397. }
  17398. /// Returns a reference to a variable of the target NPC.
  17399. /// Returns 0 if an error occurs.
  17400. ///
  17401. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  17402. BUILDIN_FUNC(getvariableofnpc)
  17403. {
  17404. struct script_data* data;
  17405. const char* name;
  17406. struct npc_data* nd;
  17407. data = script_getdata(st,2);
  17408. if( !data_isreference(data) )
  17409. {// Not a reference (aka varaible name)
  17410. ShowError("buildin_getvariableofnpc: not a variable\n");
  17411. script_reportdata(data);
  17412. script_pushnil(st);
  17413. st->state = END;
  17414. return SCRIPT_CMD_FAILURE;
  17415. }
  17416. name = reference_getname(data);
  17417. if( *name != '.' || name[1] == '@' )
  17418. {// not a npc variable
  17419. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  17420. script_reportdata(data);
  17421. script_pushnil(st);
  17422. st->state = END;
  17423. return SCRIPT_CMD_FAILURE;
  17424. }
  17425. nd = npc_name2id(script_getstr(st,3));
  17426. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  17427. {// NPC not found or has no script
  17428. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  17429. script_pushnil(st);
  17430. st->state = END;
  17431. return SCRIPT_CMD_FAILURE;
  17432. }
  17433. if (!nd->u.scr.script->local.vars)
  17434. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  17435. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  17436. return SCRIPT_CMD_SUCCESS;
  17437. }
  17438. /// Opens a warp portal.
  17439. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  17440. ///
  17441. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  17442. ///
  17443. /// @author blackhole89
  17444. BUILDIN_FUNC(warpportal)
  17445. {
  17446. int spx;
  17447. int spy;
  17448. unsigned short mapindex;
  17449. int tpx;
  17450. int tpy;
  17451. std::shared_ptr<s_skill_unit_group> group;
  17452. struct block_list* bl;
  17453. bl = map_id2bl(st->oid);
  17454. if( bl == NULL ) {
  17455. ShowError("buildin_warpportal: NPC is needed\n");
  17456. return SCRIPT_CMD_FAILURE;
  17457. }
  17458. spx = script_getnum(st,2);
  17459. spy = script_getnum(st,3);
  17460. mapindex = mapindex_name2id(script_getstr(st, 4));
  17461. tpx = script_getnum(st,5);
  17462. tpy = script_getnum(st,6);
  17463. if( mapindex == 0 ) {
  17464. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  17465. return SCRIPT_CMD_FAILURE;
  17466. }
  17467. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  17468. if( group == NULL )
  17469. return SCRIPT_CMD_FAILURE;// failed
  17470. group->val1 = (group->val1<<16)|(short)0;
  17471. group->val2 = (tpx<<16) | tpy;
  17472. group->val3 = mapindex;
  17473. return SCRIPT_CMD_SUCCESS;
  17474. }
  17475. /**
  17476. * openmail({<char_id>});
  17477. **/
  17478. BUILDIN_FUNC(openmail)
  17479. {
  17480. map_session_data* sd;
  17481. if (!script_charid2sd(2,sd))
  17482. return SCRIPT_CMD_FAILURE;
  17483. #if PACKETVER < 20150513
  17484. mail_openmail(sd);
  17485. return SCRIPT_CMD_SUCCESS;
  17486. #else
  17487. ShowError( "buildin_openmail: This command is not supported for PACKETVER 2015-05-13 or newer.\n" );
  17488. return SCRIPT_CMD_FAILURE;
  17489. #endif
  17490. }
  17491. /**
  17492. * openauction({<char_id>});
  17493. **/
  17494. BUILDIN_FUNC(openauction)
  17495. {
  17496. TBL_PC* sd;
  17497. if (!script_charid2sd(2,sd))
  17498. return SCRIPT_CMD_FAILURE;
  17499. if( !battle_config.feature_auction ) {
  17500. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  17501. return SCRIPT_CMD_SUCCESS;
  17502. }
  17503. clif_Auction_openwindow(sd);
  17504. return SCRIPT_CMD_SUCCESS;
  17505. }
  17506. /// Retrieves the value of the specified flag of the specified cell.
  17507. ///
  17508. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  17509. ///
  17510. /// @see cell_chk* constants in src/map/script_constants.hpp for the types
  17511. BUILDIN_FUNC(checkcell)
  17512. {
  17513. int16 m = map_mapname2mapid(script_getstr(st,2));
  17514. int16 x = script_getnum(st,3);
  17515. int16 y = script_getnum(st,4);
  17516. cell_chk type = (cell_chk)script_getnum(st,5);
  17517. script_pushint(st, map_getcell(m, x, y, type));
  17518. return SCRIPT_CMD_SUCCESS;
  17519. }
  17520. /// Modifies flags of cells in the specified area.
  17521. ///
  17522. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  17523. ///
  17524. /// @see cell_* constants in src/map/script_constants.hpp for the types
  17525. BUILDIN_FUNC(setcell)
  17526. {
  17527. int16 m = map_mapname2mapid(script_getstr(st,2));
  17528. int16 x1 = script_getnum(st,3);
  17529. int16 y1 = script_getnum(st,4);
  17530. int16 x2 = script_getnum(st,5);
  17531. int16 y2 = script_getnum(st,6);
  17532. cell_t type = (cell_t)script_getnum(st,7);
  17533. bool flag = script_getnum(st,8) != 0;
  17534. int x,y;
  17535. if( x1 > x2 ) std::swap(x1,x2);
  17536. if( y1 > y2 ) std::swap(y1,y2);
  17537. for( y = y1; y <= y2; ++y )
  17538. for( x = x1; x <= x2; ++x )
  17539. map_setcell(m, x, y, type, flag);
  17540. return SCRIPT_CMD_SUCCESS;
  17541. }
  17542. /**
  17543. * Gets a free cell in the specified area.
  17544. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  17545. */
  17546. BUILDIN_FUNC(getfreecell)
  17547. {
  17548. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  17549. struct script_data
  17550. *data_x = script_getdata(st, 3),
  17551. *data_y = script_getdata(st, 4);
  17552. struct block_list* bl = map_id2bl(st->rid);
  17553. map_session_data *sd = nullptr;
  17554. int16 m, x = 0, y = 0;
  17555. int rx = -1, ry = -1, flag = 1;
  17556. if (!data_isreference(data_x)) {
  17557. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  17558. return SCRIPT_CMD_FAILURE;
  17559. }
  17560. if (!data_isreference(data_y)) {
  17561. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  17562. return SCRIPT_CMD_FAILURE;
  17563. }
  17564. name_x = reference_getname(data_x),
  17565. name_y = reference_getname(data_y);
  17566. if (is_string_variable(name_x)) {
  17567. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  17568. return SCRIPT_CMD_FAILURE;
  17569. }
  17570. if (is_string_variable(name_y)) {
  17571. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  17572. return SCRIPT_CMD_FAILURE;
  17573. }
  17574. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  17575. if (!script_rid2sd(sd)) {
  17576. if (not_server_variable(*name_x))
  17577. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  17578. else
  17579. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  17580. return SCRIPT_CMD_FAILURE;
  17581. }
  17582. }
  17583. if (script_hasdata(st, 5))
  17584. x = script_getnum(st, 5);
  17585. if (script_hasdata(st, 6))
  17586. y = script_getnum(st, 6);
  17587. if (script_hasdata(st, 7))
  17588. rx = script_getnum(st, 7);
  17589. if (script_hasdata(st, 8))
  17590. ry = script_getnum(st, 8);
  17591. if (script_hasdata(st, 9))
  17592. flag = script_getnum(st, 9);
  17593. if (bl && strcmp(mapn, "this") == 0)
  17594. m = bl->m;
  17595. else
  17596. m = map_mapname2mapid(mapn);
  17597. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  17598. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  17599. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  17600. return SCRIPT_CMD_SUCCESS;
  17601. }
  17602. /*==========================================
  17603. * Mercenary Commands
  17604. *------------------------------------------*/
  17605. BUILDIN_FUNC(mercenary_create)
  17606. {
  17607. map_session_data *sd;
  17608. int class_, contract_time;
  17609. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  17610. return SCRIPT_CMD_SUCCESS;
  17611. class_ = script_getnum(st,2);
  17612. if( !mercenary_db.exists(class_) )
  17613. return SCRIPT_CMD_SUCCESS;
  17614. contract_time = script_getnum(st,3);
  17615. mercenary_create(sd, class_, contract_time);
  17616. return SCRIPT_CMD_SUCCESS;
  17617. }
  17618. BUILDIN_FUNC(mercenary_delete)
  17619. {
  17620. map_session_data *sd;
  17621. int type = 0;
  17622. if( !script_charid2sd(2, sd) )
  17623. return SCRIPT_CMD_FAILURE;
  17624. if( sd->md == nullptr ) {
  17625. 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);
  17626. return SCRIPT_CMD_FAILURE;
  17627. }
  17628. if( script_hasdata(st, 3) ) {
  17629. type = script_getnum(st, 3);
  17630. if( type < 0 || type > 3 ) {
  17631. ShowWarning("buildin_mercenary_delete: invalid type value of %d.\n", type);
  17632. return SCRIPT_CMD_FAILURE;
  17633. }
  17634. }
  17635. mercenary_delete(sd->md, type);
  17636. return SCRIPT_CMD_SUCCESS;
  17637. }
  17638. BUILDIN_FUNC(mercenary_heal)
  17639. {
  17640. map_session_data *sd;
  17641. int hp, sp;
  17642. if( !script_rid2sd(sd) || sd->md == NULL )
  17643. return SCRIPT_CMD_SUCCESS;
  17644. hp = script_getnum(st,2);
  17645. sp = script_getnum(st,3);
  17646. status_heal(&sd->md->bl, hp, sp, 0);
  17647. return SCRIPT_CMD_SUCCESS;
  17648. }
  17649. BUILDIN_FUNC(mercenary_sc_start)
  17650. {
  17651. map_session_data *sd;
  17652. enum sc_type type;
  17653. int tick, val1;
  17654. if( !script_rid2sd(sd) || sd->md == NULL )
  17655. return SCRIPT_CMD_SUCCESS;
  17656. type = (sc_type)script_getnum(st,2);
  17657. tick = script_getnum(st,3);
  17658. val1 = script_getnum(st,4);
  17659. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  17660. return SCRIPT_CMD_SUCCESS;
  17661. }
  17662. BUILDIN_FUNC(mercenary_get_calls)
  17663. {
  17664. map_session_data *sd;
  17665. int guild;
  17666. if( !script_rid2sd(sd) )
  17667. return SCRIPT_CMD_SUCCESS;
  17668. guild = script_getnum(st,2);
  17669. switch( guild )
  17670. {
  17671. case ARCH_MERC_GUILD:
  17672. script_pushint(st,sd->status.arch_calls);
  17673. break;
  17674. case SPEAR_MERC_GUILD:
  17675. script_pushint(st,sd->status.spear_calls);
  17676. break;
  17677. case SWORD_MERC_GUILD:
  17678. script_pushint(st,sd->status.sword_calls);
  17679. break;
  17680. default:
  17681. script_pushint(st,0);
  17682. break;
  17683. }
  17684. return SCRIPT_CMD_SUCCESS;
  17685. }
  17686. BUILDIN_FUNC(mercenary_set_calls)
  17687. {
  17688. map_session_data *sd;
  17689. int guild, value, *calls;
  17690. if( !script_rid2sd(sd) )
  17691. return SCRIPT_CMD_SUCCESS;
  17692. guild = script_getnum(st,2);
  17693. value = script_getnum(st,3);
  17694. switch( guild )
  17695. {
  17696. case ARCH_MERC_GUILD:
  17697. calls = &sd->status.arch_calls;
  17698. break;
  17699. case SPEAR_MERC_GUILD:
  17700. calls = &sd->status.spear_calls;
  17701. break;
  17702. case SWORD_MERC_GUILD:
  17703. calls = &sd->status.sword_calls;
  17704. break;
  17705. default:
  17706. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  17707. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17708. }
  17709. *calls += value;
  17710. *calls = cap_value(*calls, 0, INT_MAX);
  17711. return SCRIPT_CMD_SUCCESS;
  17712. }
  17713. BUILDIN_FUNC(mercenary_get_faith)
  17714. {
  17715. map_session_data *sd;
  17716. int guild;
  17717. if( !script_rid2sd(sd) )
  17718. return SCRIPT_CMD_SUCCESS;
  17719. guild = script_getnum(st,2);
  17720. switch( guild )
  17721. {
  17722. case ARCH_MERC_GUILD:
  17723. script_pushint(st,sd->status.arch_faith);
  17724. break;
  17725. case SPEAR_MERC_GUILD:
  17726. script_pushint(st,sd->status.spear_faith);
  17727. break;
  17728. case SWORD_MERC_GUILD:
  17729. script_pushint(st,sd->status.sword_faith);
  17730. break;
  17731. default:
  17732. script_pushint(st,0);
  17733. break;
  17734. }
  17735. return SCRIPT_CMD_SUCCESS;
  17736. }
  17737. BUILDIN_FUNC(mercenary_set_faith)
  17738. {
  17739. map_session_data *sd;
  17740. int guild, value, *calls;
  17741. if( !script_rid2sd(sd) )
  17742. return SCRIPT_CMD_SUCCESS;
  17743. guild = script_getnum(st,2);
  17744. value = script_getnum(st,3);
  17745. switch( guild )
  17746. {
  17747. case ARCH_MERC_GUILD:
  17748. calls = &sd->status.arch_faith;
  17749. break;
  17750. case SPEAR_MERC_GUILD:
  17751. calls = &sd->status.spear_faith;
  17752. break;
  17753. case SWORD_MERC_GUILD:
  17754. calls = &sd->status.sword_faith;
  17755. break;
  17756. default:
  17757. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17758. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17759. }
  17760. *calls += value;
  17761. *calls = cap_value(*calls, 0, INT_MAX);
  17762. if( mercenary_get_guild(sd->md) == guild )
  17763. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17764. return SCRIPT_CMD_SUCCESS;
  17765. }
  17766. /*------------------------------------------
  17767. * Book Reading
  17768. *------------------------------------------*/
  17769. BUILDIN_FUNC(readbook)
  17770. {
  17771. map_session_data *sd;
  17772. int book_id, page;
  17773. if( !script_rid2sd(sd) )
  17774. return SCRIPT_CMD_SUCCESS;
  17775. book_id = script_getnum(st,2);
  17776. page = script_getnum(st,3);
  17777. clif_readbook(sd->fd, book_id, page);
  17778. return SCRIPT_CMD_SUCCESS;
  17779. }
  17780. /******************
  17781. Questlog script commands
  17782. *******************/
  17783. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17784. BUILDIN_FUNC(questinfo)
  17785. {
  17786. TBL_NPC* nd = map_id2nd(st->oid);
  17787. if (!nd || nd->bl.m == -1) {
  17788. ShowError("buildin_questinfo: No NPC attached.\n");
  17789. return SCRIPT_CMD_FAILURE;
  17790. }
  17791. int icon = script_getnum(st, 2);
  17792. #if PACKETVER >= 20120410
  17793. switch(icon){
  17794. case QTYPE_QUEST:
  17795. case QTYPE_QUEST2:
  17796. case QTYPE_JOB:
  17797. case QTYPE_JOB2:
  17798. case QTYPE_EVENT:
  17799. case QTYPE_EVENT2:
  17800. // Warg icons were replaced in this client
  17801. #if PACKETVER < 20170315
  17802. case QTYPE_WARG:
  17803. case QTYPE_WARG2:
  17804. #else
  17805. case QTYPE_CLICKME:
  17806. case QTYPE_DAILYQUEST:
  17807. case QTYPE_EVENT3:
  17808. case QTYPE_JOBQUEST:
  17809. case QTYPE_JUMPING_PORING:
  17810. #endif
  17811. // Leave everything as it is
  17812. break;
  17813. case QTYPE_NONE:
  17814. default:
  17815. // Default to nothing if icon id is invalid.
  17816. icon = QTYPE_NONE;
  17817. break;
  17818. }
  17819. #else
  17820. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17821. icon = QTYPE_QUEST;
  17822. else
  17823. icon = icon + 1;
  17824. #endif
  17825. int color = QMARK_NONE;
  17826. if (script_hasdata(st, 3)) {
  17827. color = script_getnum(st, 3);
  17828. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17829. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17830. script_reportfunc(st);
  17831. color = QMARK_NONE;
  17832. }
  17833. }
  17834. struct script_code *script = nullptr;
  17835. if (script_hasdata(st, 4)) {
  17836. const char *str = script_getstr(st, 4);
  17837. if (str) {
  17838. std::string condition(str);
  17839. if (condition.find( "achievement_condition" ) == std::string::npos)
  17840. condition = "achievement_condition( " + condition + " );";
  17841. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17842. if (!script) {
  17843. st->state = END;
  17844. return SCRIPT_CMD_FAILURE;
  17845. }
  17846. }
  17847. }
  17848. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17849. qi->icon = static_cast<e_questinfo_types>(icon);
  17850. qi->color = static_cast<e_questinfo_markcolor>(color);
  17851. qi->condition = script;
  17852. nd->qi_data.push_back(qi);
  17853. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17854. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17855. mapdata->qi_npc.push_back(nd->bl.id);
  17856. return SCRIPT_CMD_SUCCESS;
  17857. }
  17858. /**
  17859. * questinfo_refresh {<char_id>};
  17860. **/
  17861. BUILDIN_FUNC(questinfo_refresh)
  17862. {
  17863. map_session_data *sd;
  17864. if (!script_charid2sd(2, sd))
  17865. return SCRIPT_CMD_FAILURE;
  17866. pc_show_questinfo(sd);
  17867. return SCRIPT_CMD_SUCCESS;
  17868. }
  17869. /**
  17870. * setquest <ID>{,<char_id>};
  17871. **/
  17872. BUILDIN_FUNC(setquest)
  17873. {
  17874. map_session_data *sd;
  17875. int quest_id;
  17876. quest_id = script_getnum(st, 2);
  17877. if (!script_charid2sd(3,sd))
  17878. return SCRIPT_CMD_FAILURE;
  17879. if( quest_add(sd, quest_id) == -1 ){
  17880. script_reportsrc(st);
  17881. script_reportfunc(st);
  17882. }
  17883. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17884. pc_show_questinfo(sd);
  17885. return SCRIPT_CMD_SUCCESS;
  17886. }
  17887. /**
  17888. * erasequest <ID>{,<char_id>};
  17889. **/
  17890. BUILDIN_FUNC(erasequest)
  17891. {
  17892. map_session_data *sd;
  17893. if (!script_charid2sd(3,sd))
  17894. return SCRIPT_CMD_FAILURE;
  17895. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17896. script_reportsrc(st);
  17897. script_reportfunc(st);
  17898. }
  17899. pc_show_questinfo(sd);
  17900. return SCRIPT_CMD_SUCCESS;
  17901. }
  17902. /**
  17903. * completequest <ID>{,<char_id>};
  17904. **/
  17905. BUILDIN_FUNC(completequest)
  17906. {
  17907. map_session_data *sd;
  17908. if (!script_charid2sd(3,sd))
  17909. return SCRIPT_CMD_FAILURE;
  17910. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17911. //20120410 or 20090218
  17912. pc_show_questinfo(sd);
  17913. return SCRIPT_CMD_SUCCESS;
  17914. }
  17915. /**
  17916. * changequest <ID>,<ID2>{,<char_id>};
  17917. **/
  17918. BUILDIN_FUNC(changequest)
  17919. {
  17920. map_session_data *sd;
  17921. if (!script_charid2sd(4,sd))
  17922. return SCRIPT_CMD_FAILURE;
  17923. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17924. script_reportsrc(st);
  17925. script_reportfunc(st);
  17926. }
  17927. //20120410 or 20090218
  17928. pc_show_questinfo(sd);
  17929. return SCRIPT_CMD_SUCCESS;
  17930. }
  17931. /**
  17932. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17933. **/
  17934. BUILDIN_FUNC(checkquest)
  17935. {
  17936. map_session_data *sd;
  17937. e_quest_check_type type = HAVEQUEST;
  17938. if( script_hasdata(st, 3) )
  17939. type = (e_quest_check_type)script_getnum(st, 3);
  17940. if (!script_charid2sd(4,sd))
  17941. return SCRIPT_CMD_FAILURE;
  17942. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17943. return SCRIPT_CMD_SUCCESS;
  17944. }
  17945. /**
  17946. * isbegin_quest(<ID>{,<char_id>})
  17947. **/
  17948. BUILDIN_FUNC(isbegin_quest)
  17949. {
  17950. map_session_data *sd;
  17951. if (!script_charid2sd(3,sd))
  17952. return SCRIPT_CMD_FAILURE;
  17953. int i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17954. script_pushint(st, i + (i < 1));
  17955. return SCRIPT_CMD_SUCCESS;
  17956. }
  17957. /**
  17958. * showevent <icon>{,<mark color>{,<char_id>}}
  17959. **/
  17960. BUILDIN_FUNC(showevent)
  17961. {
  17962. TBL_PC *sd;
  17963. if (!script_charid2sd(4,sd))
  17964. return SCRIPT_CMD_FAILURE;
  17965. struct npc_data *nd = map_id2nd(st->oid);
  17966. if (!nd)
  17967. return SCRIPT_CMD_SUCCESS;
  17968. int color = QMARK_NONE;
  17969. int icon = script_getnum(st, 2);
  17970. if (script_hasdata(st, 3)) {
  17971. color = script_getnum(st, 3);
  17972. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17973. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17974. script_reportfunc(st);
  17975. color = QMARK_NONE;
  17976. }
  17977. }
  17978. #if PACKETVER >= 20120410
  17979. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17980. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17981. #else
  17982. if (icon < 0 || icon > 7)
  17983. icon = 0;
  17984. else
  17985. icon = icon + 1;
  17986. #endif
  17987. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17988. return SCRIPT_CMD_SUCCESS;
  17989. }
  17990. /*==========================================
  17991. * BattleGround System
  17992. *------------------------------------------*/
  17993. BUILDIN_FUNC(waitingroom2bg)
  17994. {
  17995. struct npc_data *nd;
  17996. struct chat_data *cd;
  17997. const char *map_name;
  17998. int mapindex = 0, bg_id;
  17999. unsigned char i,c=0;
  18000. struct s_battleground_team team;
  18001. if( script_hasdata(st,7) )
  18002. nd = npc_name2id(script_getstr(st,7));
  18003. else
  18004. nd = (struct npc_data *)map_id2bl(st->oid);
  18005. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  18006. {
  18007. script_pushint(st,0);
  18008. return SCRIPT_CMD_SUCCESS;
  18009. }
  18010. map_name = script_getstr(st,2);
  18011. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  18012. { // Invalid Map
  18013. script_pushint(st, 0);
  18014. return SCRIPT_CMD_SUCCESS;
  18015. }
  18016. team.warp_x = script_getnum(st,3);
  18017. team.warp_y = script_getnum(st,4);
  18018. if (script_hasdata(st,5)) {
  18019. team.quit_event = script_getstr(st,5); // Logout Event
  18020. check_event(st, team.quit_event.c_str());
  18021. } else
  18022. team.quit_event = "";
  18023. if (script_hasdata(st,6)) {
  18024. team.death_event = script_getstr(st,6); // Die Event
  18025. check_event(st, team.death_event.c_str());
  18026. } else
  18027. team.death_event = "";
  18028. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  18029. { // Creation failed
  18030. script_pushint(st,0);
  18031. return SCRIPT_CMD_SUCCESS;
  18032. }
  18033. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  18034. map_session_data *sd;
  18035. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd, false) ){
  18036. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  18037. ++c;
  18038. }
  18039. }
  18040. mapreg_setreg(add_str("$@arenamembersnum"), c);
  18041. script_pushint(st,bg_id);
  18042. return SCRIPT_CMD_SUCCESS;
  18043. }
  18044. BUILDIN_FUNC(waitingroom2bg_single)
  18045. {
  18046. const char* map_name;
  18047. struct npc_data *nd;
  18048. struct chat_data *cd;
  18049. map_session_data *sd;
  18050. int x, y, mapindex, bg_id = script_getnum(st,2);
  18051. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18052. if (!bg) {
  18053. script_pushint(st, false);
  18054. return SCRIPT_CMD_SUCCESS;
  18055. }
  18056. if (script_hasdata(st, 3)) {
  18057. map_name = script_getstr(st, 3);
  18058. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  18059. script_pushint(st, false);
  18060. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18061. }
  18062. x = script_getnum(st, 4);
  18063. y = script_getnum(st, 5);
  18064. }
  18065. else {
  18066. mapindex = bg->cemetery.map;
  18067. x = bg->cemetery.x;
  18068. y = bg->cemetery.y;
  18069. }
  18070. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  18071. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  18072. script_pushint(st, false);
  18073. return SCRIPT_CMD_FAILURE;
  18074. }
  18075. nd = npc_name2id(script_getstr(st,6));
  18076. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  18077. return SCRIPT_CMD_SUCCESS;
  18078. if( (sd = cd->usersd[0]) == NULL )
  18079. return SCRIPT_CMD_SUCCESS;
  18080. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  18081. {
  18082. script_pushint(st, true);
  18083. }
  18084. else
  18085. script_pushint(st, false);
  18086. return SCRIPT_CMD_SUCCESS;
  18087. }
  18088. /// Creates an instance of battleground battle group.
  18089. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  18090. /// @author [secretdataz]
  18091. BUILDIN_FUNC(bg_create) {
  18092. const char *map_name;
  18093. int mapindex = 0;
  18094. struct s_battleground_team team;
  18095. map_name = script_getstr(st, 2);
  18096. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  18097. { // Invalid Map
  18098. script_pushint(st, 0);
  18099. return SCRIPT_CMD_SUCCESS;
  18100. }
  18101. team.warp_x = script_getnum(st,3);
  18102. team.warp_y = script_getnum(st,4);
  18103. if (script_hasdata(st,5)) {
  18104. team.quit_event = script_getstr(st,5); // Logout Event
  18105. check_event(st, team.quit_event.c_str());
  18106. } else
  18107. team.quit_event = "";
  18108. if (script_hasdata(st,6)) {
  18109. team.death_event = script_getstr(st,6); // Die Event
  18110. check_event(st, team.death_event.c_str());
  18111. } else
  18112. team.death_event = "";
  18113. script_pushint(st, bg_create(mapindex, &team));
  18114. return SCRIPT_CMD_SUCCESS;
  18115. }
  18116. /// Adds attached player or <char id> (if specified) to an existing
  18117. /// battleground group and warps it to the specified coordinates on
  18118. /// the given map.
  18119. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  18120. /// @author [secretdataz]
  18121. BUILDIN_FUNC(bg_join) {
  18122. const char* map_name;
  18123. map_session_data *sd;
  18124. int x, y, mapindex, bg_id = script_getnum(st, 2);
  18125. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18126. if (!bg) {
  18127. script_pushint(st, false);
  18128. return SCRIPT_CMD_SUCCESS;
  18129. }
  18130. if (script_hasdata(st, 3)) {
  18131. map_name = script_getstr(st, 3);
  18132. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  18133. script_pushint(st, false);
  18134. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18135. }
  18136. x = script_getnum(st, 4);
  18137. y = script_getnum(st, 5);
  18138. } else {
  18139. mapindex = bg->cemetery.map;
  18140. x = bg->cemetery.x;
  18141. y = bg->cemetery.y;
  18142. }
  18143. if (!script_charid2sd(6, sd)) {
  18144. script_pushint(st, false);
  18145. return SCRIPT_CMD_FAILURE;
  18146. }
  18147. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  18148. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  18149. script_pushint(st, false);
  18150. return SCRIPT_CMD_FAILURE;
  18151. }
  18152. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  18153. {
  18154. script_pushint(st, true);
  18155. }
  18156. else
  18157. script_pushint(st, false);
  18158. return SCRIPT_CMD_SUCCESS;
  18159. }
  18160. BUILDIN_FUNC(bg_team_setxy)
  18161. {
  18162. int bg_id = script_getnum(st,2);
  18163. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18164. if (bg) {
  18165. bg->cemetery.x = script_getnum(st, 3);
  18166. bg->cemetery.y = script_getnum(st, 4);
  18167. }
  18168. return SCRIPT_CMD_SUCCESS;
  18169. }
  18170. BUILDIN_FUNC(bg_warp)
  18171. {
  18172. int x, y, mapindex, bg_id;
  18173. const char* map_name;
  18174. bg_id = script_getnum(st,2);
  18175. map_name = script_getstr(st,3);
  18176. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  18177. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18178. x = script_getnum(st,4);
  18179. y = script_getnum(st,5);
  18180. bg_team_warp(bg_id, mapindex, x, y);
  18181. return SCRIPT_CMD_SUCCESS;
  18182. }
  18183. BUILDIN_FUNC(bg_monster)
  18184. {
  18185. int class_ = 0, x = 0, y = 0, bg_id = 0;
  18186. const char *str,*mapname, *evt="";
  18187. bg_id = script_getnum(st,2);
  18188. mapname = script_getstr(st,3);
  18189. x = script_getnum(st,4);
  18190. y = script_getnum(st,5);
  18191. str = script_getstr(st,6);
  18192. class_ = script_getnum(st,7);
  18193. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  18194. check_event(st, evt);
  18195. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  18196. return SCRIPT_CMD_SUCCESS;
  18197. }
  18198. BUILDIN_FUNC(bg_monster_set_team)
  18199. {
  18200. struct mob_data *md;
  18201. struct block_list *mbl;
  18202. int id = script_getnum(st,2),
  18203. bg_id = script_getnum(st,3);
  18204. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  18205. return SCRIPT_CMD_SUCCESS;
  18206. md = (TBL_MOB *)mbl;
  18207. md->bg_id = bg_id;
  18208. mob_stop_attack(md);
  18209. mob_stop_walking(md, 0);
  18210. md->target_id = md->attacked_id = 0;
  18211. clif_name_area(&md->bl);
  18212. return SCRIPT_CMD_SUCCESS;
  18213. }
  18214. BUILDIN_FUNC(bg_leave)
  18215. {
  18216. map_session_data *sd = NULL;
  18217. bool deserter = false;
  18218. if( !script_charid2sd(2,sd) || !sd->bg_id )
  18219. return SCRIPT_CMD_SUCCESS;
  18220. if (!strcmp(script_getfuncname(st), "bg_desert"))
  18221. deserter = true;
  18222. bg_team_leave(sd, false, deserter);
  18223. return SCRIPT_CMD_SUCCESS;
  18224. }
  18225. BUILDIN_FUNC(bg_destroy)
  18226. {
  18227. int bg_id = script_getnum(st,2);
  18228. bg_team_delete(bg_id);
  18229. return SCRIPT_CMD_SUCCESS;
  18230. }
  18231. BUILDIN_FUNC(bg_getareausers)
  18232. {
  18233. const char *str = script_getstr(st, 3);
  18234. int16 m, x0, y0, x1, y1;
  18235. int bg_id = script_getnum(st, 2), c = 0;
  18236. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18237. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  18238. script_pushint(st,0);
  18239. return SCRIPT_CMD_SUCCESS;
  18240. }
  18241. x0 = script_getnum(st,4);
  18242. y0 = script_getnum(st,5);
  18243. x1 = script_getnum(st,6);
  18244. y1 = script_getnum(st,7);
  18245. for (const auto &member : bg->members) {
  18246. 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 )
  18247. continue;
  18248. c++;
  18249. }
  18250. script_pushint(st,c);
  18251. return SCRIPT_CMD_SUCCESS;
  18252. }
  18253. BUILDIN_FUNC(bg_updatescore)
  18254. {
  18255. const char *str;
  18256. int16 m;
  18257. str = script_getstr(st,2);
  18258. if( (m = map_mapname2mapid(str)) < 0 )
  18259. return SCRIPT_CMD_SUCCESS;
  18260. struct map_data *mapdata = map_getmapdata(m);
  18261. mapdata->bgscore_lion = script_getnum(st,3);
  18262. mapdata->bgscore_eagle = script_getnum(st,4);
  18263. clif_bg_updatescore(m);
  18264. return SCRIPT_CMD_SUCCESS;
  18265. }
  18266. BUILDIN_FUNC(bg_get_data)
  18267. {
  18268. int bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  18269. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18270. if (bg) {
  18271. switch (type) {
  18272. case 0:
  18273. script_pushint(st, bg->members.size());
  18274. break;
  18275. case 1:
  18276. for (const auto &member : bg->members)
  18277. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  18278. mapreg_setreg(add_str("$@arenamemberscount"), i);
  18279. script_pushint(st, i);
  18280. break;
  18281. default:
  18282. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  18283. break;
  18284. }
  18285. } else
  18286. script_pushint(st, 0);
  18287. return SCRIPT_CMD_SUCCESS;
  18288. }
  18289. /**
  18290. * Reserves a slot for the given Battleground.
  18291. * bg_reserve("<battleground_map_name>"{,<ended>});
  18292. */
  18293. BUILDIN_FUNC(bg_reserve)
  18294. {
  18295. const char *str = script_getstr(st, 2);
  18296. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  18297. if (!bg_queue_reserve(str, ended))
  18298. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  18299. return SCRIPT_CMD_SUCCESS;
  18300. }
  18301. /**
  18302. * Removes a spot for the given Battleground.
  18303. * bg_unbook("<battleground_map_name>");
  18304. */
  18305. BUILDIN_FUNC(bg_unbook)
  18306. {
  18307. const char *str = script_getstr(st, 2);
  18308. if (!bg_queue_unbook(str))
  18309. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  18310. return SCRIPT_CMD_SUCCESS;
  18311. }
  18312. /**
  18313. * Gets battleground database information.
  18314. * bg_info("<battleground name>", <type>);
  18315. */
  18316. BUILDIN_FUNC(bg_info)
  18317. {
  18318. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  18319. if (!bg) {
  18320. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  18321. return SCRIPT_CMD_FAILURE;
  18322. }
  18323. int type = script_getnum(st, 3);
  18324. switch (type) {
  18325. case BG_INFO_ID:
  18326. script_pushint(st, bg->id);
  18327. break;
  18328. case BG_INFO_REQUIRED_PLAYERS:
  18329. script_pushint(st, bg->required_players);
  18330. break;
  18331. case BG_INFO_MAX_PLAYERS:
  18332. script_pushint(st, bg->max_players);
  18333. break;
  18334. case BG_INFO_MIN_LEVEL:
  18335. script_pushint(st, bg->min_lvl);
  18336. break;
  18337. case BG_INFO_MAX_LEVEL:
  18338. script_pushint(st, bg->max_lvl);
  18339. break;
  18340. case BG_INFO_MAPS: {
  18341. size_t i;
  18342. for (i = 0; i < bg->maps.size(); i++)
  18343. setd_sub_str(st, nullptr, ".@bgmaps$", i, mapindex_id2name(bg->maps[i].mapindex), nullptr);
  18344. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  18345. script_pushint(st, i);
  18346. break;
  18347. }
  18348. case BG_INFO_DESERTER_TIME:
  18349. script_pushint(st, bg->deserter_time);
  18350. break;
  18351. default:
  18352. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  18353. return SCRIPT_CMD_FAILURE;
  18354. }
  18355. return SCRIPT_CMD_SUCCESS;
  18356. }
  18357. /*==========================================
  18358. * Instancing System
  18359. *------------------------------------------*/
  18360. /**
  18361. * Returns an Instance ID.
  18362. * @param st: Script state
  18363. * @param mode: Instance mode
  18364. * @return instance ID on success or 0 otherwise
  18365. */
  18366. int script_instancegetid(struct script_state* st, e_instance_mode mode)
  18367. {
  18368. int instance_id = 0;
  18369. if (mode == IM_NONE) {
  18370. struct npc_data *nd = map_id2nd(st->oid);
  18371. if (nd && nd->instance_id > 0)
  18372. instance_id = nd->instance_id;
  18373. } else {
  18374. map_session_data *sd = map_id2sd(st->rid);
  18375. if (sd) {
  18376. switch (mode) {
  18377. case IM_CHAR:
  18378. if (sd->instance_id > 0)
  18379. instance_id = sd->instance_id;
  18380. break;
  18381. case IM_PARTY: {
  18382. struct party_data *pd = party_search(sd->status.party_id);
  18383. if (pd && pd->instance_id > 0)
  18384. instance_id = pd->instance_id;
  18385. }
  18386. break;
  18387. case IM_GUILD: {
  18388. auto gd = guild_search(sd->status.guild_id);
  18389. if (gd && gd->instance_id > 0)
  18390. instance_id = gd->instance_id;
  18391. }
  18392. break;
  18393. case IM_CLAN: {
  18394. struct clan *cd = clan_search(sd->status.clan_id);
  18395. if (cd && cd->instance_id > 0)
  18396. instance_id = cd->instance_id;
  18397. }
  18398. break;
  18399. default: // Unsupported type
  18400. break;
  18401. }
  18402. }
  18403. }
  18404. return instance_id;
  18405. }
  18406. /*==========================================
  18407. * Creates the instance
  18408. * Returns Instance ID if created successfully
  18409. *------------------------------------------*/
  18410. BUILDIN_FUNC(instance_create)
  18411. {
  18412. e_instance_mode mode = IM_PARTY;
  18413. int owner_id = 0;
  18414. if (script_hasdata(st, 3)) {
  18415. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18416. if (mode < IM_NONE || mode >= IM_MAX) {
  18417. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  18418. return SCRIPT_CMD_FAILURE;
  18419. }
  18420. }
  18421. if (script_hasdata(st, 4))
  18422. owner_id = script_getnum(st, 4);
  18423. else {
  18424. // If sd is NULL, instance_create will return -2.
  18425. map_session_data *sd = NULL;
  18426. switch(mode) {
  18427. case IM_NONE:
  18428. owner_id = st->oid;
  18429. break;
  18430. case IM_CHAR:
  18431. if (script_rid2sd(sd))
  18432. owner_id = sd->status.char_id;
  18433. break;
  18434. case IM_PARTY:
  18435. if (script_rid2sd(sd))
  18436. owner_id = sd->status.party_id;
  18437. break;
  18438. case IM_GUILD:
  18439. if (script_rid2sd(sd))
  18440. owner_id = sd->status.guild_id;
  18441. break;
  18442. case IM_CLAN:
  18443. if (script_rid2sd(sd))
  18444. owner_id = sd->status.clan_id;
  18445. break;
  18446. default:
  18447. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  18448. return SCRIPT_CMD_FAILURE;
  18449. }
  18450. }
  18451. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  18452. return SCRIPT_CMD_SUCCESS;
  18453. }
  18454. /*==========================================
  18455. * Destroys an instance (unofficial)
  18456. * Officially instances are only destroyed by timeout
  18457. *
  18458. * instance_destroy {<instance_id>};
  18459. *------------------------------------------*/
  18460. BUILDIN_FUNC(instance_destroy)
  18461. {
  18462. int instance_id;
  18463. if( script_hasdata(st,2) )
  18464. instance_id = script_getnum(st,2);
  18465. else
  18466. instance_id = script_instancegetid(st);
  18467. if( instance_id == 0 ) {
  18468. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  18469. return SCRIPT_CMD_FAILURE;
  18470. }
  18471. instance_destroy(instance_id);
  18472. return SCRIPT_CMD_SUCCESS;
  18473. }
  18474. /*==========================================
  18475. * Warps player to instance
  18476. * Results:
  18477. * IE_OK: Success
  18478. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  18479. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  18480. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  18481. *------------------------------------------*/
  18482. BUILDIN_FUNC(instance_enter)
  18483. {
  18484. map_session_data *sd = NULL;
  18485. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  18486. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  18487. int instance_id;
  18488. if (script_hasdata(st, 6))
  18489. instance_id = script_getnum(st, 6);
  18490. else
  18491. instance_id = script_instancegetid(st, IM_PARTY);
  18492. if (!script_charid2sd(5,sd))
  18493. return SCRIPT_CMD_FAILURE;
  18494. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  18495. return SCRIPT_CMD_SUCCESS;
  18496. }
  18497. /*==========================================
  18498. * Returns the name of a duplicated NPC
  18499. *
  18500. * instance_npcname <npc_name>{,<instance_id>};
  18501. * <npc_name> is the full name of an NPC.
  18502. *------------------------------------------*/
  18503. BUILDIN_FUNC(instance_npcname)
  18504. {
  18505. const char *str;
  18506. int instance_id;
  18507. struct npc_data *nd;
  18508. str = script_getstr(st,2);
  18509. if( script_hasdata(st,3) )
  18510. instance_id = script_getnum(st,3);
  18511. else
  18512. instance_id = script_instancegetid(st);
  18513. if( instance_id > 0 && (nd = npc_name2id(str)) != NULL ) {
  18514. static char npcname[NAME_LENGTH];
  18515. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  18516. script_pushconststr(st,npcname);
  18517. } else {
  18518. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  18519. st->state = END;
  18520. return SCRIPT_CMD_FAILURE;
  18521. }
  18522. return SCRIPT_CMD_SUCCESS;
  18523. }
  18524. /*==========================================
  18525. * Returns the name of a duplicated map
  18526. *
  18527. * instance_mapname <map_name>{,<instance_id>};
  18528. *------------------------------------------*/
  18529. BUILDIN_FUNC(instance_mapname)
  18530. {
  18531. const char *str;
  18532. int16 m;
  18533. int instance_id;
  18534. str = script_getstr(st,2);
  18535. if( script_hasdata(st,3) )
  18536. instance_id = script_getnum(st,3);
  18537. else
  18538. instance_id = script_instancegetid(st);
  18539. // Check that instance mapname is a valid map
  18540. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  18541. script_pushconststr(st, "");
  18542. else
  18543. script_pushconststr(st, map_getmapdata(m)->name);
  18544. return SCRIPT_CMD_SUCCESS;
  18545. }
  18546. /*==========================================
  18547. * Returns an Instance ID
  18548. *------------------------------------------*/
  18549. BUILDIN_FUNC(instance_id)
  18550. {
  18551. int mode = IM_NONE; // Default to the attached NPC
  18552. if (script_hasdata(st, 2)) {
  18553. mode = script_getnum(st, 2);
  18554. if (mode <= IM_NONE || mode >= IM_MAX) {
  18555. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  18556. script_pushint(st, 0);
  18557. return SCRIPT_CMD_SUCCESS;
  18558. }
  18559. }
  18560. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  18561. return SCRIPT_CMD_SUCCESS;
  18562. }
  18563. /*==========================================
  18564. * Warps all players inside an instance
  18565. *
  18566. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  18567. *------------------------------------------*/
  18568. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  18569. {
  18570. unsigned int m = va_arg(ap,unsigned int);
  18571. int x = va_arg(ap,int);
  18572. int y = va_arg(ap,int);
  18573. int instance_id = va_arg(ap, int);
  18574. int flag = va_arg(ap, int);
  18575. map_session_data *sd;
  18576. nullpo_retr(0, bl);
  18577. if (bl->type != BL_PC)
  18578. return 0;
  18579. sd = (TBL_PC *)bl;
  18580. if ((flag & IWA_NOTDEAD) != 0 && pc_isdead(sd))
  18581. return 0;
  18582. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18583. if (!idata)
  18584. return 0;
  18585. int owner_id = idata->owner_id;
  18586. switch(idata->mode) {
  18587. case IM_NONE:
  18588. break;
  18589. case IM_CHAR:
  18590. if (sd->status.char_id != owner_id)
  18591. return 0;
  18592. break;
  18593. case IM_PARTY:
  18594. if (sd->status.party_id != owner_id)
  18595. return 0;
  18596. break;
  18597. case IM_GUILD:
  18598. if (sd->status.guild_id != owner_id)
  18599. return 0;
  18600. break;
  18601. case IM_CLAN:
  18602. if (sd->status.clan_id != owner_id)
  18603. return 0;
  18604. break;
  18605. }
  18606. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  18607. return 1;
  18608. }
  18609. BUILDIN_FUNC(instance_warpall)
  18610. {
  18611. int16 m;
  18612. int instance_id;
  18613. const char *mapn = script_getstr(st,2);
  18614. if( script_hasdata(st,5) )
  18615. instance_id = script_getnum(st,5);
  18616. else
  18617. instance_id = script_instancegetid(st, IM_PARTY);
  18618. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0) {
  18619. ShowError("buildin_instance_warpall: Instance map for instance ID %d is not found.\n", instance_id);
  18620. return SCRIPT_CMD_FAILURE;
  18621. }
  18622. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18623. if (!idata) {
  18624. ShowError("buildin_instance_warpall: Instance is not found.\n");
  18625. return SCRIPT_CMD_FAILURE;
  18626. }
  18627. int flag = IWA_NONE;
  18628. int x = script_getnum(st,3);
  18629. int y = script_getnum(st,4);
  18630. if( script_hasdata(st, 6) )
  18631. flag = script_getnum(st, 6);
  18632. for(const auto &it : idata->map)
  18633. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id, flag);
  18634. return SCRIPT_CMD_SUCCESS;
  18635. }
  18636. /*==========================================
  18637. * Broadcasts to all maps inside an instance
  18638. *
  18639. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  18640. * Using 0 for <instance id> will auto-detect the id.
  18641. *------------------------------------------*/
  18642. BUILDIN_FUNC(instance_announce) {
  18643. int instance_id = script_getnum(st,2);
  18644. const char *mes = script_getstr(st,3);
  18645. int flag = script_getnum(st,4);
  18646. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  18647. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  18648. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  18649. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  18650. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  18651. if (instance_id <= 0)
  18652. instance_id = script_instancegetid(st);
  18653. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18654. if (instance_id <= 0 || !idata) {
  18655. ShowError("buildin_instance_announce: Instance not found.\n");
  18656. return SCRIPT_CMD_FAILURE;
  18657. }
  18658. for (const auto &it : idata->map)
  18659. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  18660. return SCRIPT_CMD_SUCCESS;
  18661. }
  18662. /*==========================================
  18663. * instance_check_party [malufett]
  18664. * Values:
  18665. * party_id : Party ID of the invoking character. [Required Parameter]
  18666. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  18667. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18668. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18669. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  18670. * Example 2: instance_check_party (getcharid(1),1,1,99);
  18671. *------------------------------------------*/
  18672. BUILDIN_FUNC(instance_check_party)
  18673. {
  18674. int amount, min, max, i, party_id, c = 0;
  18675. struct party_data *p;
  18676. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  18677. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18678. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18679. if( min < 1 || min > MAX_LEVEL) {
  18680. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  18681. return SCRIPT_CMD_FAILURE;
  18682. } else if( max < 1 || max > MAX_LEVEL) {
  18683. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  18684. return SCRIPT_CMD_FAILURE;
  18685. }
  18686. if( script_hasdata(st,2) )
  18687. party_id = script_getnum(st,2);
  18688. else return SCRIPT_CMD_FAILURE;
  18689. if( !(p = party_search(party_id)) ) {
  18690. script_pushint(st, 0); // Returns false if party does not exist.
  18691. return SCRIPT_CMD_FAILURE;
  18692. }
  18693. for( i = 0; i < MAX_PARTY; i++ ) {
  18694. map_session_data *pl_sd;
  18695. if( (pl_sd = p->data[i].sd) )
  18696. if(map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18697. if(pl_sd->status.base_level < min) {
  18698. script_pushint(st, 0);
  18699. return SCRIPT_CMD_SUCCESS;
  18700. } else if(pl_sd->status.base_level > max) {
  18701. script_pushint(st, 0);
  18702. return SCRIPT_CMD_SUCCESS;
  18703. }
  18704. c++;
  18705. }
  18706. }
  18707. if(c < amount)
  18708. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  18709. else
  18710. script_pushint(st, 1);
  18711. return SCRIPT_CMD_SUCCESS;
  18712. }
  18713. /*==========================================
  18714. * instance_check_guild
  18715. * Values:
  18716. * guild_id : Guild ID of the invoking character. [Required Parameter]
  18717. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  18718. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18719. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18720. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  18721. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  18722. *------------------------------------------*/
  18723. BUILDIN_FUNC(instance_check_guild)
  18724. {
  18725. int amount, min, max, i, guild_id = 0, c = 0;
  18726. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18727. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18728. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18729. if (min < 1 || min > MAX_LEVEL) {
  18730. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18731. return SCRIPT_CMD_FAILURE;
  18732. } else if (max < 1 || max > MAX_LEVEL) {
  18733. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18734. return SCRIPT_CMD_FAILURE;
  18735. }
  18736. if (script_hasdata(st,2))
  18737. guild_id = script_getnum(st,2);
  18738. else
  18739. return SCRIPT_CMD_FAILURE;
  18740. auto g = guild_search(guild_id);
  18741. if (!g) {
  18742. script_pushint(st, 0); // Returns false if guild does not exist.
  18743. return SCRIPT_CMD_FAILURE;
  18744. }
  18745. for(i = 0; i < MAX_GUILD; i++) {
  18746. map_session_data *pl_sd;
  18747. if ((pl_sd = g->guild.member[i].sd)) {
  18748. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18749. if (pl_sd->status.base_level < min) {
  18750. script_pushint(st, 0);
  18751. return SCRIPT_CMD_SUCCESS;
  18752. } else if (pl_sd->status.base_level > max) {
  18753. script_pushint(st, 0);
  18754. return SCRIPT_CMD_SUCCESS;
  18755. }
  18756. c++;
  18757. }
  18758. }
  18759. }
  18760. if (c < amount)
  18761. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18762. else
  18763. script_pushint(st, 1);
  18764. return SCRIPT_CMD_SUCCESS;
  18765. }
  18766. /*==========================================
  18767. * instance_check_clan
  18768. * Values:
  18769. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18770. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18771. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18772. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18773. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18774. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18775. *------------------------------------------*/
  18776. BUILDIN_FUNC(instance_check_clan)
  18777. {
  18778. int amount, min, max, i, clan_id = 0, c = 0;
  18779. struct clan *cd = NULL;
  18780. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18781. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18782. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18783. if (min < 1 || min > MAX_LEVEL) {
  18784. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18785. return SCRIPT_CMD_FAILURE;
  18786. } else if (max < 1 || max > MAX_LEVEL) {
  18787. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18788. return SCRIPT_CMD_FAILURE;
  18789. }
  18790. if (script_hasdata(st,2))
  18791. clan_id = script_getnum(st,2);
  18792. else
  18793. return SCRIPT_CMD_FAILURE;
  18794. if (!(cd = clan_search(clan_id))) {
  18795. script_pushint(st, 0); // Returns false if clan does not exist.
  18796. return SCRIPT_CMD_FAILURE;
  18797. }
  18798. for(i = 0; i < MAX_CLAN; i++) {
  18799. map_session_data *pl_sd;
  18800. if ((pl_sd = cd->members[i])) {
  18801. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18802. if (pl_sd->status.base_level < min) {
  18803. script_pushint(st, 0);
  18804. return SCRIPT_CMD_SUCCESS;
  18805. } else if (pl_sd->status.base_level > max) {
  18806. script_pushint(st, 0);
  18807. return SCRIPT_CMD_SUCCESS;
  18808. }
  18809. c++;
  18810. }
  18811. }
  18812. }
  18813. if (c < amount)
  18814. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18815. else
  18816. script_pushint(st, 1);
  18817. return SCRIPT_CMD_SUCCESS;
  18818. }
  18819. /*==========================================
  18820. * instance_info
  18821. * Values:
  18822. * name : name of the instance you want to look up. [Required Parameter]
  18823. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18824. * index : index of the map in the instance. [Optional Parameter]
  18825. *------------------------------------------*/
  18826. BUILDIN_FUNC(instance_info)
  18827. {
  18828. const char* name = script_getstr(st, 2);
  18829. int type = script_getnum(st, 3);
  18830. int index = 0;
  18831. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18832. if (!db) {
  18833. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18834. script_pushint(st, -1);
  18835. return SCRIPT_CMD_FAILURE;
  18836. }
  18837. switch( type ){
  18838. case IIT_ID:
  18839. script_pushint(st, db->id);
  18840. break;
  18841. case IIT_TIME_LIMIT:
  18842. script_pushint(st, db->limit);
  18843. break;
  18844. case IIT_IDLE_TIMEOUT:
  18845. script_pushint(st, db->timeout);
  18846. break;
  18847. case IIT_ENTER_MAP:
  18848. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18849. break;
  18850. case IIT_ENTER_X:
  18851. script_pushint(st, db->enter.x);
  18852. break;
  18853. case IIT_ENTER_Y:
  18854. script_pushint(st, db->enter.y);
  18855. break;
  18856. case IIT_MAPCOUNT:
  18857. script_pushint(st, db->maplist.size());
  18858. break;
  18859. case IIT_MAP:
  18860. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18861. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18862. script_pushconststr(st, "");
  18863. return SCRIPT_CMD_FAILURE;
  18864. }
  18865. index = script_getnum(st, 4);
  18866. if( index < 0 ){
  18867. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18868. script_pushconststr(st, "");
  18869. return SCRIPT_CMD_FAILURE;
  18870. }
  18871. if( index > UINT8_MAX ){
  18872. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18873. script_pushconststr(st, "");
  18874. return SCRIPT_CMD_FAILURE;
  18875. }
  18876. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18877. break;
  18878. default:
  18879. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18880. script_pushint(st, -1);
  18881. return SCRIPT_CMD_FAILURE;
  18882. }
  18883. return SCRIPT_CMD_SUCCESS;
  18884. }
  18885. /*------------------------------------------
  18886. *instance_live_info( <info type>{, <instance id>} );
  18887. - ILI_NAME : Instance Name
  18888. - ILI_MODE : Instance Mode
  18889. - ILI_OWNER : owner id
  18890. *------------------------------------------*/
  18891. BUILDIN_FUNC(instance_live_info)
  18892. {
  18893. int type = script_getnum(st, 2);
  18894. int id = 0;
  18895. if (type < ILI_NAME || type > ILI_OWNER) {
  18896. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18897. script_pushint(st, -1);
  18898. return SCRIPT_CMD_FAILURE;
  18899. }
  18900. if (!script_hasdata(st, 3))
  18901. id = script_instancegetid(st);
  18902. else
  18903. id = script_getnum(st, 3);
  18904. std::shared_ptr<s_instance_db> db = nullptr;
  18905. std::shared_ptr<s_instance_data> im = nullptr;
  18906. if (id > 0 && id < INT_MAX) {
  18907. im = util::umap_find(instances, id);
  18908. if (im)
  18909. db = instance_db.find(im->id);
  18910. }
  18911. if (!im || !db) {
  18912. if (type == ILI_NAME)
  18913. script_pushconststr(st, "");
  18914. else
  18915. script_pushint(st, -1);
  18916. return SCRIPT_CMD_SUCCESS;
  18917. }
  18918. switch( type ) {
  18919. case ILI_NAME:
  18920. script_pushstrcopy(st, db->name.c_str());
  18921. break;
  18922. case ILI_MODE:
  18923. script_pushint(st, im->mode);
  18924. break;
  18925. case ILI_OWNER:
  18926. script_pushint(st, im->owner_id);
  18927. break;
  18928. }
  18929. return SCRIPT_CMD_SUCCESS;
  18930. }
  18931. /*==========================================
  18932. * instance_list(<"map name">{,<instance mode>});
  18933. * set '.@instance_list' to a list of the live instance ids for the map with the mode.
  18934. * return the array size of '.@instance_list'
  18935. *------------------------------------------*/
  18936. BUILDIN_FUNC(instance_list)
  18937. {
  18938. int src_id = map_mapname2mapid(script_getstr(st, 2));
  18939. if (src_id == 0) {
  18940. ShowError("buildin_instance_list: map '%s' doesn't exist\n", script_getstr(st, 2));
  18941. return SCRIPT_CMD_FAILURE;
  18942. }
  18943. e_instance_mode mode = IM_MAX;
  18944. if (script_hasdata(st, 3)) {
  18945. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18946. if (mode < IM_NONE || mode >= IM_MAX) {
  18947. ShowError("buildin_instance_list: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 3));
  18948. return SCRIPT_CMD_FAILURE;
  18949. }
  18950. }
  18951. int j = 0;
  18952. for (int i = instance_start; i < map_num; i++) {
  18953. struct map_data* mapdata = &map[i];
  18954. if (mapdata->instance_src_map == src_id) {
  18955. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, mapdata->instance_id);
  18956. if (idata && (mode == IM_MAX || idata->mode == mode)) {
  18957. setd_sub_num(st, nullptr, ".@instance_list", j, mapdata->instance_id, nullptr);
  18958. j++;
  18959. }
  18960. }
  18961. }
  18962. script_pushint(st, j);
  18963. return SCRIPT_CMD_SUCCESS;
  18964. }
  18965. /*==========================================
  18966. * Custom Fonts
  18967. *------------------------------------------*/
  18968. BUILDIN_FUNC(setfont)
  18969. {
  18970. map_session_data *sd;
  18971. int font;
  18972. if( !script_rid2sd(sd) )
  18973. return SCRIPT_CMD_SUCCESS;
  18974. font = script_getnum(st,2);
  18975. if( sd->status.font != font )
  18976. sd->status.font = font;
  18977. else
  18978. sd->status.font = 0;
  18979. clif_font(sd);
  18980. return SCRIPT_CMD_SUCCESS;
  18981. }
  18982. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  18983. {
  18984. TBL_MOB* md = (TBL_MOB*)bl;
  18985. struct block_list *tbl;
  18986. int mobid = va_arg(ap,int);
  18987. uint16 skill_id = va_arg(ap,int);
  18988. uint16 skill_lv = va_arg(ap,int);
  18989. int casttime = va_arg(ap,int);
  18990. int cancel = va_arg(ap,int);
  18991. int emotion = va_arg(ap,int);
  18992. int target = va_arg(ap,int);
  18993. if( md->mob_id != mobid )
  18994. return 0;
  18995. // 0:self, 1:target, 2:master, default:random
  18996. switch( target ) {
  18997. case 0: tbl = map_id2bl(md->bl.id); break;
  18998. case 1: tbl = map_id2bl(md->target_id); break;
  18999. case 2: tbl = map_id2bl(md->master_id); break;
  19000. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  19001. }
  19002. if( !tbl )
  19003. return 0;
  19004. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  19005. unit_skillcastcancel(bl,0);
  19006. if( skill_get_casttype(skill_id) == CAST_GROUND )
  19007. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  19008. else
  19009. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  19010. clif_emotion(&md->bl, emotion);
  19011. return 1;
  19012. }
  19013. /*==========================================
  19014. * areamobuseskill "<Map Name>",<x>,<y>,<range>,"<Mob name>"/<Mob ID>,"<Skill Name>"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  19015. *------------------------------------------*/
  19016. BUILDIN_FUNC(areamobuseskill)
  19017. {
  19018. struct block_list center;
  19019. int16 m;
  19020. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  19021. ShowError("areamobuseskill: invalid map name.\n");
  19022. return SCRIPT_CMD_SUCCESS;
  19023. }
  19024. center.m = m;
  19025. center.x = script_getnum(st,3);
  19026. center.y = script_getnum(st,4);
  19027. int range = script_getnum( st,5 );
  19028. uint16 mobid;
  19029. if( script_isstring( st, 6 ) ){
  19030. const char* name = script_getstr( st, 6 );
  19031. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  19032. if( mob == nullptr ){
  19033. ShowWarning( "buildin_areamobuseskill: Attempted to use skill of non-existing monster \"%s\"\n", name );
  19034. return SCRIPT_CMD_FAILURE;
  19035. }
  19036. mobid = mob->id;
  19037. }else{
  19038. mobid = script_getnum( st, 6 );
  19039. if( !mob_db.exists( mobid ) ){
  19040. ShowWarning( "buildin_areamobuseskill: Attempted to use skill of non-existing monster class %d\n", mobid );
  19041. return SCRIPT_CMD_FAILURE;
  19042. }
  19043. }
  19044. int skill_id;
  19045. if (script_isstring(st, 7)) {
  19046. const char *name = script_getstr(st, 7);
  19047. if (!(skill_id = skill_name2id(name))) {
  19048. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  19049. return SCRIPT_CMD_FAILURE;
  19050. }
  19051. } else {
  19052. skill_id = script_getnum(st, 7);
  19053. if (!skill_get_index(skill_id)) {
  19054. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  19055. return SCRIPT_CMD_FAILURE;
  19056. }
  19057. }
  19058. int skill_lv = script_getnum( st, 8 );
  19059. if( skill_lv > battle_config.mob_max_skilllvl ){
  19060. skill_lv = battle_config.mob_max_skilllvl;
  19061. }
  19062. int casttime = script_getnum( st, 9 );
  19063. int cancel = script_getnum( st, 10 );
  19064. int emotion = script_getnum( st, 11 );
  19065. int target = script_getnum( st, 12 );
  19066. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  19067. return SCRIPT_CMD_SUCCESS;
  19068. }
  19069. /**
  19070. * Display a progress bar above a character
  19071. * progressbar "<color>",<seconds>;
  19072. */
  19073. BUILDIN_FUNC(progressbar)
  19074. {
  19075. map_session_data * sd;
  19076. const char * color;
  19077. unsigned int second;
  19078. if( !script_rid2sd(sd) )
  19079. return SCRIPT_CMD_SUCCESS;
  19080. st->state = STOP;
  19081. color = script_getstr(st,2);
  19082. second = script_getnum(st,3);
  19083. sd->progressbar.npc_id = st->oid;
  19084. sd->progressbar.timeout = gettick() + second*1000;
  19085. sd->state.workinprogress = WIP_DISABLE_ALL;
  19086. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  19087. return SCRIPT_CMD_SUCCESS;
  19088. }
  19089. /**
  19090. * Display a progress bar above an NPC
  19091. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  19092. */
  19093. BUILDIN_FUNC(progressbar_npc){
  19094. map_session_data *sd = map_id2sd(st->rid);
  19095. struct npc_data* nd = NULL;
  19096. if( script_hasdata(st, 4) ){
  19097. const char* name = script_getstr(st, 4);
  19098. nd = npc_name2id(name);
  19099. if( !nd ){
  19100. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  19101. return SCRIPT_CMD_FAILURE;
  19102. }
  19103. }else{
  19104. nd = map_id2nd(st->oid);
  19105. }
  19106. // First call(by function call)
  19107. if( !nd->progressbar.timeout ){
  19108. const char *color;
  19109. int second;
  19110. color = script_getstr(st, 2);
  19111. second = script_getnum(st, 3);
  19112. if( second < 0 ){
  19113. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  19114. return SCRIPT_CMD_FAILURE;
  19115. }
  19116. if (sd) { // Player attached - keep them from doing other things
  19117. sd->state.workinprogress = WIP_DISABLE_ALL;
  19118. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  19119. }
  19120. // sleep for the target amount of time
  19121. st->state = RERUNLINE;
  19122. st->sleep.tick = second * 1000;
  19123. nd->progressbar.timeout = gettick() + second * 1000;
  19124. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  19125. clif_progressbar_npc_area(nd);
  19126. // Second call(by timer after sleeping time is over)
  19127. } else {
  19128. // Continue the script
  19129. if (sd) { // Player attached - remove restrictions
  19130. sd->state.workinprogress = WIP_DISABLE_NONE;
  19131. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  19132. }
  19133. st->state = RUN;
  19134. st->sleep.tick = 0;
  19135. nd->progressbar.timeout = nd->progressbar.color = 0;
  19136. }
  19137. return SCRIPT_CMD_SUCCESS;
  19138. }
  19139. BUILDIN_FUNC(pushpc)
  19140. {
  19141. uint8 dir;
  19142. int cells, dx, dy;
  19143. map_session_data* sd;
  19144. if(!script_rid2sd(sd))
  19145. {
  19146. return SCRIPT_CMD_SUCCESS;
  19147. }
  19148. dir = script_getnum(st,2);
  19149. cells = script_getnum(st,3);
  19150. if(dir >= DIR_MAX)
  19151. {
  19152. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  19153. script_reportsrc(st);
  19154. dir%= DIR_MAX; // trim spin-over
  19155. }
  19156. if(!cells)
  19157. {// zero distance
  19158. return SCRIPT_CMD_SUCCESS;
  19159. }
  19160. else if(cells<0)
  19161. {// pushing backwards
  19162. dir = direction_opposite( static_cast<enum directions>( dir ) ); // turn around
  19163. cells = -cells;
  19164. }
  19165. dx = dirx[dir];
  19166. dy = diry[dir];
  19167. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  19168. return SCRIPT_CMD_SUCCESS;
  19169. }
  19170. /// Invokes buying store preparation window
  19171. /// buyingstore <slots>;
  19172. BUILDIN_FUNC(buyingstore)
  19173. {
  19174. map_session_data* sd;
  19175. if( !script_rid2sd(sd) )
  19176. {
  19177. return SCRIPT_CMD_SUCCESS;
  19178. }
  19179. if( npc_isnear(&sd->bl) ) {
  19180. char output[150];
  19181. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  19182. clif_displaymessage(sd->fd, output);
  19183. return SCRIPT_CMD_SUCCESS;
  19184. }
  19185. buyingstore_setup(sd, script_getnum(st,2));
  19186. return SCRIPT_CMD_SUCCESS;
  19187. }
  19188. /// Invokes search store info window
  19189. /// searchstores <uses>,<effect>;
  19190. BUILDIN_FUNC(searchstores)
  19191. {
  19192. unsigned short effect;
  19193. unsigned int uses;
  19194. map_session_data* sd;
  19195. if( !script_rid2sd(sd) )
  19196. {
  19197. return SCRIPT_CMD_SUCCESS;
  19198. }
  19199. uses = script_getnum(st,2);
  19200. effect = script_getnum(st,3);
  19201. if( !uses )
  19202. {
  19203. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  19204. return SCRIPT_CMD_FAILURE;
  19205. }
  19206. if( effect > 1 )
  19207. {
  19208. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  19209. return SCRIPT_CMD_FAILURE;
  19210. }
  19211. searchstore_open(sd, uses, effect);
  19212. return SCRIPT_CMD_SUCCESS;
  19213. }
  19214. /// Displays a number as large digital clock.
  19215. /// showdigit <value>[,<type>];
  19216. BUILDIN_FUNC(showdigit)
  19217. {
  19218. unsigned int type = 0;
  19219. int value;
  19220. map_session_data* sd;
  19221. if( !script_rid2sd(sd) )
  19222. {
  19223. return SCRIPT_CMD_SUCCESS;
  19224. }
  19225. value = script_getnum(st,2);
  19226. if( script_hasdata(st,3) ){
  19227. type = script_getnum(st,3);
  19228. }
  19229. switch( type ){
  19230. case 0:
  19231. break;
  19232. case 1:
  19233. case 2:
  19234. // Use absolute value and then the negative value of it as starting value
  19235. // This is what gravity's client does for these counters
  19236. value = -abs(value);
  19237. break;
  19238. case 3:
  19239. value = abs(value);
  19240. if( value > 99 ){
  19241. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  19242. script_reportsrc(st);
  19243. value = 99;
  19244. }
  19245. break;
  19246. default:
  19247. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  19248. return SCRIPT_CMD_FAILURE;
  19249. }
  19250. clif_showdigit(sd, (unsigned char)type, value);
  19251. return SCRIPT_CMD_SUCCESS;
  19252. }
  19253. /**
  19254. * Rune Knight
  19255. * makerune <% success bonus>{,<char_id>};
  19256. **/
  19257. BUILDIN_FUNC(makerune) {
  19258. TBL_PC* sd;
  19259. if (!script_charid2sd(3,sd))
  19260. return SCRIPT_CMD_FAILURE;
  19261. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  19262. sd->itemid = script_getnum(st,2);
  19263. return SCRIPT_CMD_SUCCESS;
  19264. }
  19265. /**
  19266. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  19267. **/
  19268. BUILDIN_FUNC(checkdragon) {
  19269. TBL_PC* sd;
  19270. if (!script_charid2sd(2,sd))
  19271. return SCRIPT_CMD_FAILURE;
  19272. if( pc_isridingdragon(sd) )
  19273. script_pushint(st,1);
  19274. else
  19275. script_pushint(st,0);
  19276. return SCRIPT_CMD_SUCCESS;
  19277. }
  19278. /**
  19279. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  19280. * - Toggles the dragon on a RK if he can mount;
  19281. * @param Color - when not provided uses the green dragon;
  19282. * - 1 : Green Dragon
  19283. * - 2 : Brown Dragon
  19284. * - 3 : Gray Dragon
  19285. * - 4 : Blue Dragon
  19286. * - 5 : Red Dragon
  19287. **/
  19288. BUILDIN_FUNC(setdragon) {
  19289. TBL_PC* sd;
  19290. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  19291. if (!script_charid2sd(3,sd))
  19292. return SCRIPT_CMD_FAILURE;
  19293. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  19294. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  19295. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  19296. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  19297. script_pushint(st,1);
  19298. } else {//Not mounted; Mount now.
  19299. unsigned int option = OPTION_DRAGON1;
  19300. if( color ) {
  19301. option = ( color == 1 ? OPTION_DRAGON1 :
  19302. color == 2 ? OPTION_DRAGON2 :
  19303. color == 3 ? OPTION_DRAGON3 :
  19304. color == 4 ? OPTION_DRAGON4 :
  19305. color == 5 ? OPTION_DRAGON5 : 0);
  19306. if( !option ) {
  19307. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  19308. option = OPTION_DRAGON1;
  19309. }
  19310. }
  19311. pc_setoption(sd, sd->sc.option|option);
  19312. script_pushint(st,1);
  19313. }
  19314. return SCRIPT_CMD_SUCCESS;
  19315. }
  19316. /**
  19317. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  19318. **/
  19319. BUILDIN_FUNC(ismounting) {
  19320. TBL_PC* sd;
  19321. if (!script_charid2sd(2,sd))
  19322. return SCRIPT_CMD_FAILURE;
  19323. if( sd->sc.getSCE(SC_ALL_RIDING) )
  19324. script_pushint(st,1);
  19325. else
  19326. script_pushint(st,0);
  19327. return SCRIPT_CMD_SUCCESS;
  19328. }
  19329. /**
  19330. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  19331. * - Toggles new mounts on a player when he can mount
  19332. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  19333. * - Will unmount the player is he is already mounting
  19334. **/
  19335. BUILDIN_FUNC(setmounting) {
  19336. TBL_PC* sd;
  19337. if (!script_charid2sd(2,sd))
  19338. return SCRIPT_CMD_FAILURE;
  19339. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  19340. clif_msg(sd, NEED_REINS_OF_MOUNT);
  19341. script_pushint(st,0); //can't mount with one of these
  19342. } 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)) {
  19343. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  19344. script_pushint(st, 0); // Silent failure
  19345. } else {
  19346. if( sd->sc.getSCE(SC_ALL_RIDING) )
  19347. status_change_end(&sd->bl, SC_ALL_RIDING); //release mount
  19348. else
  19349. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  19350. script_pushint(st,1);//in both cases, return 1.
  19351. }
  19352. return SCRIPT_CMD_SUCCESS;
  19353. }
  19354. /**
  19355. * Retrieves quantity of arguments provided to callfunc/callsub.
  19356. * getargcount() -> amount of arguments received in a function
  19357. **/
  19358. BUILDIN_FUNC(getargcount) {
  19359. struct script_retinfo* ri;
  19360. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  19361. ShowError("script:getargcount: used out of function or callsub label!\n");
  19362. st->state = END;
  19363. return SCRIPT_CMD_FAILURE;
  19364. }
  19365. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  19366. script_pushint(st, ri->nargs);
  19367. return SCRIPT_CMD_SUCCESS;
  19368. }
  19369. /**
  19370. * getcharip(<account ID>/<character ID>/<character name>)
  19371. **/
  19372. BUILDIN_FUNC(getcharip)
  19373. {
  19374. map_session_data* sd = NULL;
  19375. /* check if a character name is specified */
  19376. if( script_hasdata(st, 2) )
  19377. {
  19378. if (script_isstring(st, 2))
  19379. sd = map_nick2sd(script_getstr(st, 2),false);
  19380. else
  19381. {
  19382. int id = script_getnum(st, 2);
  19383. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  19384. }
  19385. }
  19386. else
  19387. script_rid2sd(sd);
  19388. /* check for sd and IP */
  19389. if (!sd || !session[sd->fd]->client_addr)
  19390. {
  19391. script_pushconststr(st, "");
  19392. return SCRIPT_CMD_SUCCESS;
  19393. }
  19394. /* return the client ip_addr converted for output */
  19395. if (sd && sd->fd && session[sd->fd])
  19396. {
  19397. /* initiliaze */
  19398. const char *ip_addr = NULL;
  19399. uint32 ip;
  19400. /* set ip, ip_addr and convert to ip and push str */
  19401. ip = session[sd->fd]->client_addr;
  19402. ip_addr = ip2str(ip, NULL);
  19403. script_pushstrcopy(st, ip_addr);
  19404. }
  19405. return SCRIPT_CMD_SUCCESS;
  19406. }
  19407. /**
  19408. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  19409. **/
  19410. BUILDIN_FUNC(is_function) {
  19411. const char* str = script_getstr(st,2);
  19412. if( strdb_exists(userfunc_db, str) )
  19413. script_pushint(st,1);
  19414. else
  19415. script_pushint(st,0);
  19416. return SCRIPT_CMD_SUCCESS;
  19417. }
  19418. /**
  19419. * get_revision() -> retrieves the current svn revision (if available)
  19420. **/
  19421. BUILDIN_FUNC(get_revision) {
  19422. const char *svn = get_svn_revision();
  19423. if ( svn[0] != UNKNOWN_VERSION )
  19424. script_pushint(st,atoi(svn));
  19425. else
  19426. script_pushint(st,-1); //unknown
  19427. return SCRIPT_CMD_SUCCESS;
  19428. }
  19429. /* get_hash() -> retrieves the current git hash (if available)*/
  19430. BUILDIN_FUNC(get_githash) {
  19431. const char* git = get_git_hash();
  19432. char buf[CHAT_SIZE_MAX];
  19433. safestrncpy(buf,git,strlen(git)+1);
  19434. if ( git[0] != UNKNOWN_VERSION )
  19435. script_pushstrcopy(st,buf);
  19436. else
  19437. script_pushconststr(st,"Unknown"); //unknown
  19438. return SCRIPT_CMD_SUCCESS;
  19439. }
  19440. /**
  19441. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  19442. **/
  19443. BUILDIN_FUNC(freeloop) {
  19444. if( script_hasdata(st,2) ) {
  19445. if( script_getnum(st,2) )
  19446. st->freeloop = 1;
  19447. else
  19448. st->freeloop = 0;
  19449. }
  19450. script_pushint(st, st->freeloop);
  19451. return SCRIPT_CMD_SUCCESS;
  19452. }
  19453. /**
  19454. * @commands (script based)
  19455. **/
  19456. BUILDIN_FUNC(bindatcmd) {
  19457. const char* atcmd;
  19458. const char* eventName;
  19459. int i, level = 0, level2 = 100;
  19460. bool create = false;
  19461. atcmd = script_getstr(st,2);
  19462. eventName = script_getstr(st,3);
  19463. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19464. atcmd++;
  19465. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  19466. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  19467. if( atcmd_binding_count == 0 ) {
  19468. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  19469. create = true;
  19470. } else {
  19471. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  19472. if( i < atcmd_binding_count ) {/* update existent entry */
  19473. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19474. atcmd_binding[i]->level = level;
  19475. atcmd_binding[i]->level2 = level2;
  19476. } else
  19477. create = true;
  19478. }
  19479. if( create ) {
  19480. i = atcmd_binding_count;
  19481. if( atcmd_binding_count++ != 0 )
  19482. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  19483. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  19484. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  19485. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19486. atcmd_binding[i]->level = level;
  19487. atcmd_binding[i]->level2 = level2;
  19488. }
  19489. return SCRIPT_CMD_SUCCESS;
  19490. }
  19491. BUILDIN_FUNC(unbindatcmd) {
  19492. const char* atcmd;
  19493. int i = 0;
  19494. atcmd = script_getstr(st, 2);
  19495. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19496. atcmd++;
  19497. if( atcmd_binding_count == 0 ) {
  19498. script_pushint(st, 0);
  19499. return SCRIPT_CMD_SUCCESS;
  19500. }
  19501. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  19502. if( i < atcmd_binding_count ) {
  19503. int cursor = 0;
  19504. aFree(atcmd_binding[i]);
  19505. atcmd_binding[i] = NULL;
  19506. /* compact the list now that we freed a slot somewhere */
  19507. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  19508. if( atcmd_binding[i] == NULL )
  19509. continue;
  19510. if( cursor != i ) {
  19511. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  19512. }
  19513. cursor++;
  19514. }
  19515. if( (atcmd_binding_count = cursor) == 0 )
  19516. aFree(atcmd_binding);
  19517. script_pushint(st, 1);
  19518. } else
  19519. script_pushint(st, 0);/* not found */
  19520. return SCRIPT_CMD_SUCCESS;
  19521. }
  19522. BUILDIN_FUNC(useatcmd) {
  19523. return atcommand_sub(st,3);
  19524. }
  19525. BUILDIN_FUNC(checkre)
  19526. {
  19527. int num;
  19528. num=script_getnum(st,2);
  19529. switch(num){
  19530. case 0:
  19531. #ifdef RENEWAL
  19532. script_pushint(st, 1);
  19533. #else
  19534. script_pushint(st, 0);
  19535. #endif
  19536. break;
  19537. case 1:
  19538. #ifdef RENEWAL_CAST
  19539. script_pushint(st, 1);
  19540. #else
  19541. script_pushint(st, 0);
  19542. #endif
  19543. break;
  19544. case 2:
  19545. #ifdef RENEWAL_DROP
  19546. script_pushint(st, 1);
  19547. #else
  19548. script_pushint(st, 0);
  19549. #endif
  19550. break;
  19551. case 3:
  19552. #ifdef RENEWAL_EXP
  19553. script_pushint(st, 1);
  19554. #else
  19555. script_pushint(st, 0);
  19556. #endif
  19557. break;
  19558. case 4:
  19559. #ifdef RENEWAL_LVDMG
  19560. script_pushint(st, 1);
  19561. #else
  19562. script_pushint(st, 0);
  19563. #endif
  19564. break;
  19565. case 5:
  19566. #ifdef RENEWAL_ASPD
  19567. script_pushint(st, 1);
  19568. #else
  19569. script_pushint(st, 0);
  19570. #endif
  19571. break;
  19572. default:
  19573. ShowWarning("buildin_checkre: unknown parameter.\n");
  19574. break;
  19575. }
  19576. return SCRIPT_CMD_SUCCESS;
  19577. }
  19578. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  19579. BUILDIN_FUNC(getrandgroupitem) {
  19580. TBL_PC* sd;
  19581. int i, get_count = 0, identify = 0;
  19582. uint16 group, qty = 0;
  19583. uint8 sub_group = 1;
  19584. struct item item_tmp;
  19585. if (!script_charid2sd(6, sd))
  19586. return SCRIPT_CMD_SUCCESS;
  19587. group = script_getnum(st,2);
  19588. if (!group) {
  19589. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  19590. return SCRIPT_CMD_FAILURE;
  19591. }
  19592. FETCH(3, qty);
  19593. FETCH(4, sub_group);
  19594. FETCH(5, identify);
  19595. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(group,sub_group);
  19596. if (!entry)
  19597. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  19598. memset(&item_tmp,0,sizeof(item_tmp));
  19599. item_tmp.nameid = entry->nameid;
  19600. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  19601. if (!qty)
  19602. qty = entry->amount;
  19603. //Check if it's stackable.
  19604. if (!itemdb_isstackable(entry->nameid)) {
  19605. item_tmp.amount = 1;
  19606. get_count = qty;
  19607. }
  19608. else {
  19609. item_tmp.amount = qty;
  19610. get_count = 1;
  19611. }
  19612. for (i = 0; i < get_count; i++) {
  19613. // if not pet egg
  19614. if (!pet_create_egg(sd, entry->nameid)) {
  19615. unsigned char flag = 0;
  19616. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  19617. clif_additem(sd,0,0,flag);
  19618. if (pc_candrop(sd,&item_tmp))
  19619. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  19620. }
  19621. }
  19622. }
  19623. return SCRIPT_CMD_SUCCESS;
  19624. }
  19625. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  19626. * Gives item(s) to the attached player based on item group contents
  19627. */
  19628. BUILDIN_FUNC(getgroupitem) {
  19629. TBL_PC *sd;
  19630. int group_id = script_getnum(st,2);
  19631. if (!script_charid2sd(4,sd))
  19632. return SCRIPT_CMD_SUCCESS;
  19633. if (itemdb_group.pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  19634. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  19635. return SCRIPT_CMD_FAILURE;
  19636. }
  19637. return SCRIPT_CMD_SUCCESS;
  19638. }
  19639. /* cleanmap <map_name>;
  19640. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  19641. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  19642. {
  19643. nullpo_ret(bl);
  19644. map_clearflooritem(bl);
  19645. return SCRIPT_CMD_SUCCESS;
  19646. }
  19647. BUILDIN_FUNC(cleanmap)
  19648. {
  19649. const char *mapname;
  19650. int16 m;
  19651. mapname = script_getstr(st, 2);
  19652. m = map_mapname2mapid(mapname);
  19653. if (!m)
  19654. return SCRIPT_CMD_FAILURE;
  19655. if ((script_lastdata(st) - 2) < 4) {
  19656. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  19657. } else {
  19658. int16 x0 = script_getnum(st, 3);
  19659. int16 y0 = script_getnum(st, 4);
  19660. int16 x1 = script_getnum(st, 5);
  19661. int16 y1 = script_getnum(st, 6);
  19662. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  19663. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  19664. } else {
  19665. ShowError("cleanarea: invalid coordinate defined!\n");
  19666. return SCRIPT_CMD_FAILURE;
  19667. }
  19668. }
  19669. return SCRIPT_CMD_SUCCESS;
  19670. }
  19671. /* Cast a skill on the attached player.
  19672. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  19673. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  19674. BUILDIN_FUNC(npcskill)
  19675. {
  19676. uint16 skill_id;
  19677. unsigned short skill_level;
  19678. unsigned int stat_point;
  19679. unsigned int npc_level;
  19680. struct npc_data *nd;
  19681. map_session_data *sd;
  19682. if( !script_rid2sd(sd) )
  19683. return SCRIPT_CMD_SUCCESS;
  19684. if (script_isstring(st, 2)) {
  19685. const char *name = script_getstr(st, 2);
  19686. if (!(skill_id = skill_name2id(name))) {
  19687. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  19688. return SCRIPT_CMD_FAILURE;
  19689. }
  19690. } else {
  19691. skill_id = script_getnum(st, 2);
  19692. if (!skill_get_index(skill_id)) {
  19693. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  19694. return SCRIPT_CMD_FAILURE;
  19695. }
  19696. }
  19697. skill_level = script_getnum(st, 3);
  19698. stat_point = script_getnum(st, 4);
  19699. npc_level = script_getnum(st, 5);
  19700. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  19701. if (stat_point > battle_config.max_third_parameter) {
  19702. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  19703. return SCRIPT_CMD_FAILURE;
  19704. }
  19705. if (npc_level > MAX_LEVEL) {
  19706. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  19707. return SCRIPT_CMD_FAILURE;
  19708. }
  19709. if (nd == NULL) { //ain't possible, but I don't trust people.
  19710. return SCRIPT_CMD_FAILURE;
  19711. }
  19712. nd->level = npc_level;
  19713. nd->stat_point = stat_point;
  19714. if (!nd->status.hp)
  19715. status_calc_npc(nd, SCO_FIRST);
  19716. else
  19717. status_calc_npc(nd, SCO_NONE);
  19718. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  19719. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0,true);
  19720. else
  19721. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0,true);
  19722. return SCRIPT_CMD_SUCCESS;
  19723. }
  19724. /* Consumes an item.
  19725. * consumeitem <item id>{,<char_id>};
  19726. * consumeitem "<item name>"{,<char_id>};
  19727. * @param item: Item ID or name
  19728. */
  19729. BUILDIN_FUNC(consumeitem)
  19730. {
  19731. map_session_data *sd;
  19732. std::shared_ptr<item_data> item_data;
  19733. if (!script_charid2sd(3, sd))
  19734. return SCRIPT_CMD_FAILURE;
  19735. if( script_isstring(st, 2) ){
  19736. const char *name = script_getstr(st, 2);
  19737. item_data = item_db.searchname( name );
  19738. if( item_data == nullptr ){
  19739. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  19740. return SCRIPT_CMD_FAILURE;
  19741. }
  19742. } else {
  19743. t_itemid nameid = script_getnum(st, 2);
  19744. item_data = item_db.find( nameid );
  19745. if( item_data == nullptr ){
  19746. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  19747. return SCRIPT_CMD_FAILURE;
  19748. }
  19749. }
  19750. run_script( item_data->script, 0, sd->bl.id, 0 );
  19751. return SCRIPT_CMD_SUCCESS;
  19752. }
  19753. /** Makes a player sit/stand.
  19754. * sit {"<character name>"};
  19755. * stand {"<character name>"};
  19756. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  19757. * @param name: Player name that will be invoked
  19758. */
  19759. BUILDIN_FUNC(sit)
  19760. {
  19761. TBL_PC *sd;
  19762. if( !script_nick2sd(2,sd) )
  19763. return SCRIPT_CMD_FAILURE;
  19764. if( !pc_issit(sd) ) {
  19765. pc_setsit(sd);
  19766. skill_sit(sd, 1);
  19767. clif_sitting(&sd->bl);
  19768. }
  19769. return SCRIPT_CMD_SUCCESS;
  19770. }
  19771. /** Makes player to stand
  19772. * @param name: Player name that will be set
  19773. */
  19774. BUILDIN_FUNC(stand)
  19775. {
  19776. TBL_PC *sd;
  19777. if( !script_nick2sd(2,sd) )
  19778. return SCRIPT_CMD_FAILURE;
  19779. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19780. skill_sit(sd, 0);
  19781. clif_standing(&sd->bl);
  19782. }
  19783. return SCRIPT_CMD_SUCCESS;
  19784. }
  19785. /** Creates an array of bounded item IDs
  19786. * countbound {<type>{,<char_id>}};
  19787. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19788. * @return amt: Total number of different items type found
  19789. */
  19790. BUILDIN_FUNC(countbound)
  19791. {
  19792. TBL_PC *sd;
  19793. if (!script_charid2sd(3,sd))
  19794. return SCRIPT_CMD_FAILURE;
  19795. int i, k = 0;
  19796. int type = script_getnum(st,2);
  19797. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19798. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19799. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19800. ))
  19801. {
  19802. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19803. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19804. k++;
  19805. }
  19806. }
  19807. script_pushint(st,k);
  19808. return SCRIPT_CMD_SUCCESS;
  19809. }
  19810. /** Creates new party
  19811. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19812. * @param party_name: String of party name that will be created
  19813. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19814. * @param item_share: 0-Each Take. 1-Party Share
  19815. * @param item_share_type: 0-Each Take. 1-Even Share
  19816. * @return val: Result value
  19817. * -3 - party name is exist
  19818. * -2 - player is in party already
  19819. * -1 - player is not found
  19820. * 0 - unknown error
  19821. * 1 - success, will return party id $@party_create_id
  19822. */
  19823. BUILDIN_FUNC(party_create)
  19824. {
  19825. char party_name[NAME_LENGTH];
  19826. int item1 = 0, item2 = 0;
  19827. TBL_PC *sd = NULL;
  19828. if (!script_charid2sd(3, sd)) {
  19829. script_pushint(st,-1);
  19830. return SCRIPT_CMD_FAILURE;
  19831. }
  19832. if( sd->status.party_id ) {
  19833. script_pushint(st,-2);
  19834. return SCRIPT_CMD_FAILURE;
  19835. }
  19836. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19837. trim(party_name);
  19838. if( party_searchname(party_name) ) {
  19839. script_pushint(st,-3);
  19840. return SCRIPT_CMD_FAILURE;
  19841. }
  19842. if( script_getnum(st,4) )
  19843. item1 = 1;
  19844. if( script_getnum(st,5) )
  19845. item2 = 1;
  19846. party_create_byscript = 1;
  19847. script_pushint(st,party_create(sd,party_name,item1,item2));
  19848. return SCRIPT_CMD_SUCCESS;
  19849. }
  19850. /** Adds player to specified party
  19851. * party_addmember <party id>,<char id>;
  19852. * @param party_id: The party that will be entered by player
  19853. * @param char_id: Char id of player that will be joined to the party
  19854. * @return val: Result value
  19855. * -5 - another character of the same account is in the party
  19856. * -4 - party is full
  19857. * -3 - party is not found
  19858. * -2 - player is in party already
  19859. * -1 - player is not found
  19860. * 0 - unknown error
  19861. * 1 - success
  19862. */
  19863. BUILDIN_FUNC(party_addmember)
  19864. {
  19865. int party_id = script_getnum(st,2);
  19866. TBL_PC *sd;
  19867. struct party_data *party;
  19868. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19869. script_pushint(st,-1);
  19870. return SCRIPT_CMD_FAILURE;
  19871. }
  19872. if( sd->status.party_id ) {
  19873. script_pushint(st,-2);
  19874. return SCRIPT_CMD_FAILURE;
  19875. }
  19876. if( !(party = party_search(party_id)) ) {
  19877. script_pushint(st,-3);
  19878. return SCRIPT_CMD_FAILURE;
  19879. }
  19880. if (battle_config.block_account_in_same_party) {
  19881. int i;
  19882. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19883. if (i < MAX_PARTY) {
  19884. script_pushint(st,-5);
  19885. return SCRIPT_CMD_FAILURE;
  19886. }
  19887. }
  19888. if( party->party.count >= MAX_PARTY ) {
  19889. script_pushint(st,-4);
  19890. return SCRIPT_CMD_FAILURE;
  19891. }
  19892. sd->party_invite = party_id;
  19893. script_pushint(st,party_add_member(party_id,sd));
  19894. return SCRIPT_CMD_SUCCESS;
  19895. }
  19896. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19897. * party_delmember {<char id>,<party_id>};
  19898. * @param: char_id
  19899. * @param: party_id
  19900. * @return val: Result value
  19901. * -3 - player is not in party
  19902. * -2 - party is not found
  19903. * -1 - player is not found
  19904. * 0 - unknown error
  19905. * 1 - success
  19906. */
  19907. BUILDIN_FUNC(party_delmember)
  19908. {
  19909. TBL_PC *sd = NULL;
  19910. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19911. script_pushint(st,-1);
  19912. return SCRIPT_CMD_FAILURE;
  19913. }
  19914. if( sd || script_charid2sd(2,sd) )
  19915. script_pushint(st,party_removemember2(sd,0,0));
  19916. else
  19917. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  19918. return SCRIPT_CMD_SUCCESS;
  19919. }
  19920. /** Changes party leader of specified party (even the leader is offline)
  19921. * party_changeleader <party id>,<char id>;
  19922. * @param party_id: ID of party
  19923. * @param char_id: Char ID of new leader
  19924. * @return val: Result value
  19925. * -4 - player is party leader already
  19926. * -3 - player is not in this party
  19927. * -2 - player is not found
  19928. * -1 - party is not found
  19929. * 0 - unknown error
  19930. * 1 - success */
  19931. BUILDIN_FUNC(party_changeleader)
  19932. {
  19933. int i, party_id = script_getnum(st,2);
  19934. TBL_PC *sd = NULL;
  19935. TBL_PC *tsd = NULL;
  19936. struct party_data *party = NULL;
  19937. if( !(party = party_search(party_id)) ) {
  19938. script_pushint(st,-1);
  19939. return SCRIPT_CMD_FAILURE;
  19940. }
  19941. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19942. script_pushint(st,-2);
  19943. return SCRIPT_CMD_FAILURE;
  19944. }
  19945. if( tsd->status.party_id != party_id ) {
  19946. script_pushint(st,-3);
  19947. return SCRIPT_CMD_FAILURE;
  19948. }
  19949. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19950. if( i >= MAX_PARTY ) { //this is should impossible!
  19951. script_pushint(st,0);
  19952. return SCRIPT_CMD_FAILURE;
  19953. }
  19954. if( party->data[i].sd == tsd ) {
  19955. script_pushint(st,-4);
  19956. return SCRIPT_CMD_FAILURE;
  19957. }
  19958. script_pushint(st,party_changeleader(sd,tsd,party));
  19959. return SCRIPT_CMD_SUCCESS;
  19960. }
  19961. /** Changes party option
  19962. * party_changeoption <party id>,<option>,<flag>;
  19963. * @param party_id: ID of party that will be changed
  19964. * @param option: Type of option
  19965. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  19966. */
  19967. BUILDIN_FUNC(party_changeoption)
  19968. {
  19969. struct party_data *party;
  19970. if( !(party = party_search(script_getnum(st,2))) ) {
  19971. script_pushint(st,-1);
  19972. return SCRIPT_CMD_FAILURE;
  19973. }
  19974. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  19975. return SCRIPT_CMD_SUCCESS;
  19976. }
  19977. /** Destroys party with party id.
  19978. * party_destroy <party id>;
  19979. * @param party_id: ID of party that will be destroyed
  19980. */
  19981. BUILDIN_FUNC(party_destroy)
  19982. {
  19983. int i;
  19984. struct party_data *party;
  19985. if( !(party = party_search(script_getnum(st,2))) ) {
  19986. script_pushint(st,0);
  19987. return SCRIPT_CMD_FAILURE;
  19988. }
  19989. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19990. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  19991. int j;
  19992. for( j = 0; j < MAX_PARTY; j++ ) {
  19993. TBL_PC *sd = party->data[j].sd;
  19994. if(sd)
  19995. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  19996. else if( party->party.member[j].char_id )
  19997. 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);
  19998. }
  19999. party_broken(party->party.party_id);
  20000. script_pushint(st,1);
  20001. }
  20002. else //leader leave = party broken
  20003. script_pushint(st,party_leave(party->data[i].sd));
  20004. return SCRIPT_CMD_SUCCESS;
  20005. }
  20006. /** Returns various information about a player's VIP status. Need to enable VIP system
  20007. * vip_status <type>,{"<character name>"};
  20008. * @param type: Info type, see enum vip_status_type
  20009. * @param name: Character name (Optional)
  20010. */
  20011. BUILDIN_FUNC(vip_status) {
  20012. #ifdef VIP_ENABLE
  20013. TBL_PC *sd;
  20014. int type;
  20015. if( !script_nick2sd(3,sd) )
  20016. return SCRIPT_CMD_FAILURE;
  20017. type = script_getnum(st, 2);
  20018. switch(type) {
  20019. case VIP_STATUS_ACTIVE: // Get VIP status.
  20020. script_pushint(st, pc_isvip(sd));
  20021. break;
  20022. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  20023. if (pc_isvip(sd)) {
  20024. script_pushint(st, sd->vip.time);
  20025. } else
  20026. script_pushint(st, 0);
  20027. break;
  20028. case VIP_STATUS_REMAINING: // Get remaining time.
  20029. if (pc_isvip(sd)) {
  20030. script_pushint(st, sd->vip.time - time(NULL));
  20031. } else
  20032. script_pushint(st, 0);
  20033. break;
  20034. default:
  20035. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  20036. return SCRIPT_CMD_FAILURE;
  20037. }
  20038. #else
  20039. script_pushint(st, 0);
  20040. #endif
  20041. return SCRIPT_CMD_SUCCESS;
  20042. }
  20043. /** Adds or removes VIP time in minutes. Need to enable VIP system
  20044. * vip_time <time in mn>,{"<character name>"};
  20045. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  20046. * @param name: Character name (optional)
  20047. */
  20048. BUILDIN_FUNC(vip_time) {
  20049. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  20050. TBL_PC *sd;
  20051. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  20052. if( !script_nick2sd(3,sd) )
  20053. return SCRIPT_CMD_FAILURE;
  20054. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  20055. #endif
  20056. return SCRIPT_CMD_SUCCESS;
  20057. }
  20058. /**
  20059. * Turns a player into a monster and optionally can grant a SC attribute effect.
  20060. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  20061. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  20062. * @param monster: Monster ID or name
  20063. * @param duration: Transform duration in millisecond (ms)
  20064. * @param sc_type: Type of SC that will be affected during the transformation
  20065. * @param val1: Value for SC
  20066. * @param val2: Value for SC
  20067. * @param val3: Value for SC
  20068. * @param val4: Value for SC
  20069. * @author: malufett
  20070. */
  20071. BUILDIN_FUNC(montransform) {
  20072. TBL_PC *sd;
  20073. enum sc_type type;
  20074. int tick, mob_id, val1, val2, val3, val4;
  20075. val1 = val2 = val3 = val4 = 0;
  20076. if( !script_rid2sd(sd) )
  20077. return SCRIPT_CMD_FAILURE;
  20078. if( script_isstring(st, 2) )
  20079. mob_id = mobdb_searchname(script_getstr(st, 2));
  20080. else
  20081. mob_id = mobdb_checkid(script_getnum(st, 2));
  20082. tick = script_getnum(st, 3);
  20083. if (script_hasdata(st, 4))
  20084. type = (sc_type)script_getnum(st, 4);
  20085. else
  20086. type = SC_NONE;
  20087. if (mob_id == 0) {
  20088. if( script_isstring(st, 2) )
  20089. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  20090. else
  20091. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  20092. return SCRIPT_CMD_FAILURE;
  20093. }
  20094. if (mob_id == MOBID_EMPERIUM) {
  20095. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  20096. return SCRIPT_CMD_FAILURE;
  20097. }
  20098. if (!(type >= SC_NONE && type < SC_MAX)) {
  20099. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  20100. return SCRIPT_CMD_FAILURE;
  20101. }
  20102. if (script_hasdata(st, 5))
  20103. val1 = script_getnum(st, 5);
  20104. if (script_hasdata(st, 6))
  20105. val2 = script_getnum(st, 6);
  20106. if (script_hasdata(st, 7))
  20107. val3 = script_getnum(st, 7);
  20108. if (script_hasdata(st, 8))
  20109. val4 = script_getnum(st, 8);
  20110. if (tick != 0) {
  20111. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  20112. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  20113. return SCRIPT_CMD_FAILURE;
  20114. }
  20115. if (sd->disguise){
  20116. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  20117. return SCRIPT_CMD_FAILURE;
  20118. }
  20119. if (!strcmp(script_getfuncname(st), "active_transform")) {
  20120. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM); // Clear previous
  20121. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  20122. } else {
  20123. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM); // Clear previous
  20124. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  20125. }
  20126. if (type != SC_NONE)
  20127. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  20128. }
  20129. return SCRIPT_CMD_SUCCESS;
  20130. }
  20131. /**
  20132. * Attach script to player for certain duration
  20133. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  20134. * @param "script code"
  20135. * @param duration
  20136. * @param flag
  20137. * @param icon
  20138. * @param char_id
  20139. * @author [Cydh]
  20140. **/
  20141. BUILDIN_FUNC(bonus_script) {
  20142. uint16 flag = 0;
  20143. int16 icon = EFST_BLANK;
  20144. uint32 dur;
  20145. uint8 type = 0;
  20146. TBL_PC* sd;
  20147. const char *script_str = NULL;
  20148. struct s_bonus_script_entry *entry = NULL;
  20149. if ( !script_charid2sd(7,sd) )
  20150. return SCRIPT_CMD_FAILURE;
  20151. script_str = script_getstr(st,2);
  20152. dur = 1000 * abs(script_getnum(st,3));
  20153. FETCH(4, flag);
  20154. FETCH(5, type);
  20155. FETCH(6, icon);
  20156. // No Script string, No Duration!
  20157. if (script_str[0] == '\0' || !dur) {
  20158. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  20159. return SCRIPT_CMD_FAILURE;
  20160. }
  20161. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  20162. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  20163. return SCRIPT_CMD_FAILURE;
  20164. }
  20165. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  20166. icon = EFST_BLANK;
  20167. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_type)icon, flag, type))) {
  20168. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  20169. status_calc_pc(sd,SCO_NONE);
  20170. }
  20171. return SCRIPT_CMD_SUCCESS;
  20172. }
  20173. /**
  20174. * Removes all bonus script from player
  20175. * bonus_script_clear {<flag>,{<char_id>}};
  20176. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  20177. * @param char_id Clear script from this character
  20178. * @author [Cydh]
  20179. */
  20180. BUILDIN_FUNC(bonus_script_clear) {
  20181. TBL_PC* sd;
  20182. bool flag = false;
  20183. if (!script_charid2sd(3,sd))
  20184. return SCRIPT_CMD_FAILURE;
  20185. if (script_hasdata(st,2))
  20186. flag = script_getnum(st,2) != 0;
  20187. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  20188. return SCRIPT_CMD_SUCCESS;
  20189. }
  20190. /** Allows player to use atcommand while talking with NPC
  20191. * enable_command;
  20192. * @author [Cydh], [Kichi]
  20193. */
  20194. BUILDIN_FUNC(enable_command) {
  20195. TBL_PC* sd;
  20196. if (!script_rid2sd(sd))
  20197. return SCRIPT_CMD_FAILURE;
  20198. sd->state.disable_atcommand_on_npc = 0;
  20199. return SCRIPT_CMD_SUCCESS;
  20200. }
  20201. /** Prevents player to use atcommand while talking with NPC
  20202. * disable_command;
  20203. * @author [Cydh], [Kichi]
  20204. */
  20205. BUILDIN_FUNC(disable_command) {
  20206. TBL_PC* sd;
  20207. if (!script_rid2sd(sd))
  20208. return SCRIPT_CMD_FAILURE;
  20209. sd->state.disable_atcommand_on_npc = 1;
  20210. return SCRIPT_CMD_SUCCESS;
  20211. }
  20212. /** Get the information of the members of a guild by type.
  20213. * getguildmember <guild_id>{,<type>};
  20214. * @param guild_id: ID of guild
  20215. * @param type: Type of option (optional)
  20216. */
  20217. BUILDIN_FUNC(getguildmember)
  20218. {
  20219. uint8 j = 0;
  20220. auto g = guild_search(script_getnum(st,2));
  20221. if (g) {
  20222. uint8 i, type = 0;
  20223. struct script_data *data = NULL;
  20224. char *varname = NULL;
  20225. if (script_hasdata(st,3))
  20226. type = script_getnum(st,3);
  20227. if (script_hasdata(st,4)) {
  20228. data = script_getdata(st, 4);
  20229. if (!data_isreference(data)) {
  20230. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  20231. return SCRIPT_CMD_FAILURE;
  20232. }
  20233. varname = reference_getname(data);
  20234. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  20235. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  20236. return SCRIPT_CMD_FAILURE;
  20237. }
  20238. if (not_server_variable(*varname)) {
  20239. map_session_data *sd;
  20240. if (!script_rid2sd(sd)) {
  20241. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  20242. return SCRIPT_CMD_FAILURE;
  20243. }
  20244. }
  20245. }
  20246. for (i = 0; i < MAX_GUILD; i++) {
  20247. if (g->guild.member[i].account_id) {
  20248. switch (type) {
  20249. case 2:
  20250. if (data)
  20251. setd_sub_num( st, NULL, varname, j, g->guild.member[i].account_id, data->ref );
  20252. else
  20253. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->guild.member[i].account_id);
  20254. break;
  20255. case 1:
  20256. if (data)
  20257. setd_sub_num( st, NULL, varname, j, g->guild.member[i].char_id, data->ref );
  20258. else
  20259. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->guild.member[i].char_id);
  20260. break;
  20261. default:
  20262. if (data)
  20263. setd_sub_str( st, NULL, varname, j, g->guild.member[i].name, data->ref );
  20264. else
  20265. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->guild.member[i].name);
  20266. break;
  20267. }
  20268. j++;
  20269. }
  20270. }
  20271. }
  20272. mapreg_setreg(add_str("$@guildmembercount"), j);
  20273. script_pushint(st, j);
  20274. return SCRIPT_CMD_SUCCESS;
  20275. }
  20276. /** Adds spirit ball to player for 'duration' in milisecond
  20277. * addspiritball <count>,<duration>{,<char_id>};
  20278. * @param count How many spirit ball will be added
  20279. * @param duration How long spiritball is active until it disappears
  20280. * @param char_id Target player (Optional)
  20281. * @author [Cydh]
  20282. */
  20283. BUILDIN_FUNC(addspiritball) {
  20284. uint8 i, count;
  20285. uint16 duration;
  20286. map_session_data *sd = NULL;
  20287. if (script_hasdata(st,4)) {
  20288. if (!script_isstring(st,4))
  20289. sd = map_charid2sd(script_getnum(st,4));
  20290. else
  20291. sd = map_nick2sd(script_getstr(st,4),false);
  20292. }
  20293. else
  20294. script_rid2sd(sd);
  20295. if (!sd)
  20296. return SCRIPT_CMD_FAILURE;
  20297. count = script_getnum(st,2);
  20298. if (count == 0)
  20299. return SCRIPT_CMD_SUCCESS;
  20300. duration = script_getnum(st,3);
  20301. for (i = 0; i < count; i++)
  20302. pc_addspiritball(sd,duration,10);
  20303. return SCRIPT_CMD_SUCCESS;
  20304. }
  20305. /** Deletes the spirit ball(s) from player
  20306. * delspiritball <count>{,<char_id>};
  20307. * @param count How many spirit ball will be deleted
  20308. * @param char_id Target player (Optional)
  20309. * @author [Cydh]
  20310. */
  20311. BUILDIN_FUNC(delspiritball) {
  20312. uint8 count;
  20313. map_session_data *sd = NULL;
  20314. if (script_hasdata(st,3)) {
  20315. if (!script_isstring(st,3))
  20316. sd = map_charid2sd(script_getnum(st,3));
  20317. else
  20318. sd = map_nick2sd(script_getstr(st,3),false);
  20319. }
  20320. else
  20321. script_rid2sd(sd);
  20322. if (!sd)
  20323. return SCRIPT_CMD_FAILURE;
  20324. count = script_getnum(st,2);
  20325. if (count == 0)
  20326. count = 1;
  20327. pc_delspiritball(sd,count,0);
  20328. return SCRIPT_CMD_SUCCESS;
  20329. }
  20330. /** Counts the spirit ball that player has
  20331. * countspiritball {<char_id>};
  20332. * @param char_id Target player (Optional)
  20333. * @author [Cydh]
  20334. */
  20335. BUILDIN_FUNC(countspiritball) {
  20336. map_session_data *sd;
  20337. if (script_hasdata(st,2)) {
  20338. if (!script_isstring(st,2))
  20339. sd = map_charid2sd(script_getnum(st,2));
  20340. else
  20341. sd = map_nick2sd(script_getstr(st,2),false);
  20342. }
  20343. else
  20344. script_rid2sd(sd);
  20345. if (!sd)
  20346. return SCRIPT_CMD_FAILURE;
  20347. script_pushint(st,sd->spiritball);
  20348. return SCRIPT_CMD_SUCCESS;
  20349. }
  20350. /** Merges separated stackable items because of guid
  20351. * mergeitem {<char_id>};
  20352. * @param char_id Char ID (Optional)
  20353. * @author [Cydh]
  20354. */
  20355. BUILDIN_FUNC(mergeitem) {
  20356. map_session_data *sd;
  20357. if (!script_charid2sd(2, sd))
  20358. return SCRIPT_CMD_FAILURE;
  20359. clif_merge_item_open( *sd );
  20360. return SCRIPT_CMD_SUCCESS;
  20361. }
  20362. /** Merges separated stackable items because of guid (Unofficial)
  20363. * mergeitem2 {<item_id>,{<char_id>}};
  20364. * mergeitem2 {"<item name>",{<char_id>}};
  20365. * @param item Item ID/Name for merging specific item (Optional)
  20366. * @author [Cydh]
  20367. */
  20368. BUILDIN_FUNC(mergeitem2) {
  20369. map_session_data *sd;
  20370. struct item *items = NULL;
  20371. uint16 i, count = 0;
  20372. t_itemid nameid = 0;
  20373. if (!script_charid2sd(3, sd))
  20374. return SCRIPT_CMD_FAILURE;
  20375. if (script_hasdata(st, 2)) {
  20376. if (script_isstring(st, 2)) {// "<item name>"
  20377. const char *name = script_getstr(st, 2);
  20378. std::shared_ptr<item_data> id = item_db.searchname( name );
  20379. if( id == nullptr ){
  20380. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  20381. script_pushint(st, count);
  20382. return SCRIPT_CMD_FAILURE;
  20383. }
  20384. nameid = id->nameid;
  20385. } else {// <item id>
  20386. nameid = script_getnum(st, 2);
  20387. if (!item_db.exists(nameid)) {
  20388. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  20389. script_pushint(st, count);
  20390. return SCRIPT_CMD_FAILURE;
  20391. }
  20392. }
  20393. }
  20394. for (i = 0; i < MAX_INVENTORY; i++) {
  20395. struct item *it = &sd->inventory.u.items_inventory[i];
  20396. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  20397. continue;
  20398. if ((!nameid || (nameid == it->nameid))) {
  20399. uint16 k;
  20400. if (!count) {
  20401. CREATE(items, struct item, 1);
  20402. memcpy(&items[count++], it, sizeof(struct item));
  20403. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20404. continue;
  20405. }
  20406. for (k = 0; k < count; k++) {
  20407. // Find Match
  20408. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  20409. items[k].amount += it->amount;
  20410. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20411. break;
  20412. }
  20413. }
  20414. if (k >= count) {
  20415. // New entry
  20416. RECREATE(items, struct item, count+1);
  20417. memcpy(&items[count++], it, sizeof(struct item));
  20418. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20419. }
  20420. }
  20421. }
  20422. if (!items) // Nothing todo here
  20423. return SCRIPT_CMD_SUCCESS;
  20424. // Retrieve the items
  20425. for (i = 0; i < count; i++) {
  20426. uint8 flag = 0;
  20427. if (!&items[i])
  20428. continue;
  20429. items[i].id = 0;
  20430. items[i].unique_id = 0;
  20431. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  20432. clif_additem(sd, i, items[i].amount, flag);
  20433. }
  20434. aFree(items);
  20435. script_pushint(st, count);
  20436. return SCRIPT_CMD_SUCCESS;
  20437. }
  20438. /**
  20439. * Update an entry from NPC shop.
  20440. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  20441. **/
  20442. BUILDIN_FUNC(npcshopupdate) {
  20443. const char* npcname = script_getstr(st, 2);
  20444. struct npc_data* nd = npc_name2id(npcname);
  20445. t_itemid nameid = script_getnum(st, 3);
  20446. int price = script_getnum(st, 4);
  20447. #if PACKETVER >= 20131223
  20448. int32 stock = script_hasdata(st,5) ? script_getnum(st,5) : -1;
  20449. if( stock < -1 ){
  20450. ShowError( "buildin_npcshopupdate: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  20451. script_pushint( st, 0 );
  20452. return SCRIPT_CMD_FAILURE;
  20453. }
  20454. #endif
  20455. int i;
  20456. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  20457. script_pushint(st,0);
  20458. return SCRIPT_CMD_FAILURE;
  20459. }
  20460. if (!nd->u.shop.count) {
  20461. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  20462. script_pushint(st,0);
  20463. return SCRIPT_CMD_FAILURE;
  20464. }
  20465. for (i = 0; i < nd->u.shop.count; i++) {
  20466. if (nd->u.shop.shop_item[i].nameid == nameid) {
  20467. if (price != 0) {
  20468. if (price < 0) {
  20469. if (nd->subtype == NPCTYPE_CASHSHOP || nd->subtype == NPCTYPE_POINTSHOP || nd->subtype == NPCTYPE_ITEMSHOP) {
  20470. ShowError("builtin_npcshopupdate: Invalid price in shop '%s'.\n", nd->exname);
  20471. script_pushint(st, 0);
  20472. return SCRIPT_CMD_FAILURE;
  20473. }
  20474. std::shared_ptr<item_data> id = item_db.find(nameid);
  20475. if (!id) {
  20476. ShowError("buildin_npcshopupdate: Item ID %u does not exist.\n", nameid);
  20477. script_pushint(st, 0);
  20478. return SCRIPT_CMD_FAILURE;
  20479. }
  20480. price = id->value_buy;
  20481. }
  20482. nd->u.shop.shop_item[i].value = price;
  20483. }
  20484. #if PACKETVER >= 20131223
  20485. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  20486. nd->u.shop.shop_item[i].qty = stock;
  20487. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  20488. }
  20489. #endif
  20490. }
  20491. }
  20492. script_pushint(st,1);
  20493. return SCRIPT_CMD_SUCCESS;
  20494. }
  20495. // Clan System
  20496. BUILDIN_FUNC(clan_join){
  20497. map_session_data *sd;
  20498. int clan_id = script_getnum(st,2);
  20499. if( !script_charid2sd( 3, sd ) ){
  20500. script_pushint(st, false);
  20501. return SCRIPT_CMD_FAILURE;
  20502. }
  20503. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  20504. script_pushint(st, true);
  20505. else
  20506. script_pushint(st, false);
  20507. return SCRIPT_CMD_SUCCESS;
  20508. }
  20509. BUILDIN_FUNC(clan_leave){
  20510. map_session_data *sd;
  20511. if( !script_charid2sd( 2, sd ) ){
  20512. script_pushint(st, false);
  20513. return SCRIPT_CMD_FAILURE;
  20514. }
  20515. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  20516. script_pushint(st, true);
  20517. else
  20518. script_pushint(st, false);
  20519. return SCRIPT_CMD_SUCCESS;
  20520. }
  20521. /**
  20522. * Get rid from running script.
  20523. * getattachedrid();
  20524. **/
  20525. BUILDIN_FUNC(getattachedrid) {
  20526. script_pushint(st, st->rid);
  20527. return SCRIPT_CMD_SUCCESS;
  20528. }
  20529. /**
  20530. * Get variable from a Player
  20531. * getvar <variable>,<char_id>;
  20532. */
  20533. BUILDIN_FUNC(getvar) {
  20534. int char_id = script_getnum(st, 3);
  20535. map_session_data *sd = map_charid2sd(char_id);
  20536. struct script_data *data = NULL;
  20537. const char *name = NULL;
  20538. if (!sd) {
  20539. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  20540. return SCRIPT_CMD_FAILURE;
  20541. }
  20542. data = script_getdata(st, 2);
  20543. if (!data_isreference(data)) {
  20544. ShowError("buildin_getvar: Not a variable\n");
  20545. script_reportdata(data);
  20546. script_pushnil(st);
  20547. st->state = END;
  20548. return SCRIPT_CMD_FAILURE;
  20549. }
  20550. name = reference_getname(data);
  20551. if (reference_toparam(data)) {
  20552. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  20553. script_reportdata(data);
  20554. script_pushnil(st);
  20555. st->state = END;
  20556. return SCRIPT_CMD_FAILURE;
  20557. }
  20558. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  20559. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  20560. script_reportdata(data);
  20561. script_pushnil(st);
  20562. st->state = END;
  20563. return SCRIPT_CMD_FAILURE;
  20564. }
  20565. get_val_(st, data, sd);
  20566. if (data_isint(data))
  20567. script_pushint(st, conv_num_(st, data, sd));
  20568. else
  20569. script_pushstrcopy(st, conv_str_(st, data, sd));
  20570. return SCRIPT_CMD_SUCCESS;
  20571. }
  20572. /**
  20573. * Display script message
  20574. * showscript "<message>"{,<GID>,<flag>};
  20575. * @param flag: Specify target
  20576. * AREA - Message is sent to players in the vicinity of the source (default).
  20577. * SELF - Message is sent only to player attached.
  20578. **/
  20579. BUILDIN_FUNC(showscript) {
  20580. struct block_list *bl = NULL;
  20581. const char *msg = script_getstr(st,2);
  20582. int id = 0;
  20583. send_target target = AREA;
  20584. if (script_hasdata(st,3)) {
  20585. id = script_getnum(st,3);
  20586. bl = map_id2bl(id);
  20587. }
  20588. else {
  20589. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  20590. }
  20591. if (!bl) {
  20592. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  20593. script_pushint(st,0);
  20594. return SCRIPT_CMD_FAILURE;
  20595. }
  20596. if (script_hasdata(st, 4)) {
  20597. target = static_cast<send_target>(script_getnum(st, 4));
  20598. if (target == SELF && map_id2sd(bl->id) == NULL) {
  20599. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  20600. return SCRIPT_CMD_FAILURE;
  20601. }
  20602. }
  20603. clif_showscript(bl, msg, target);
  20604. script_pushint(st,1);
  20605. return SCRIPT_CMD_SUCCESS;
  20606. }
  20607. /**
  20608. * Ignore the SECURE_NPCTIMEOUT function.
  20609. * ignoretimeout <flag>{,<char_id>};
  20610. */
  20611. BUILDIN_FUNC(ignoretimeout)
  20612. {
  20613. #ifdef SECURE_NPCTIMEOUT
  20614. map_session_data *sd = NULL;
  20615. if (script_hasdata(st,3)) {
  20616. if (!script_isstring(st,3))
  20617. sd = map_charid2sd(script_getnum(st,3));
  20618. else
  20619. sd = map_nick2sd(script_getstr(st,3),false);
  20620. } else
  20621. script_rid2sd(sd);
  20622. if (!sd)
  20623. return SCRIPT_CMD_FAILURE;
  20624. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  20625. #endif
  20626. return SCRIPT_CMD_SUCCESS;
  20627. }
  20628. /**
  20629. * geteleminfo <type>{,<char_id>};
  20630. **/
  20631. BUILDIN_FUNC(geteleminfo) {
  20632. TBL_ELEM *ed = NULL;
  20633. TBL_PC *sd = NULL;
  20634. int type = script_getnum(st,2);
  20635. if (!script_charid2sd(3, sd)) {
  20636. script_pushint(st, 0);
  20637. return SCRIPT_CMD_SUCCESS;
  20638. }
  20639. if (!(ed = sd->ed)) {
  20640. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  20641. script_pushint(st, 0);
  20642. return SCRIPT_CMD_SUCCESS;
  20643. }
  20644. switch (type) {
  20645. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  20646. case 1: script_pushint(st, ed->bl.id); break;
  20647. default:
  20648. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  20649. script_pushint(st, 0);
  20650. return SCRIPT_CMD_FAILURE;
  20651. }
  20652. return SCRIPT_CMD_SUCCESS;
  20653. }
  20654. /**
  20655. * opendressroom(<flag>{,<char_id>});
  20656. */
  20657. BUILDIN_FUNC(opendressroom)
  20658. {
  20659. #if PACKETVER >= 20150513
  20660. int flag = 1;
  20661. TBL_PC* sd;
  20662. if( script_hasdata(st,2) )
  20663. flag = script_getnum(st,2);
  20664. if (!script_charid2sd(3, sd))
  20665. return SCRIPT_CMD_FAILURE;
  20666. clif_dressing_room(sd, flag);
  20667. return SCRIPT_CMD_SUCCESS;
  20668. #else
  20669. return SCRIPT_CMD_FAILURE;
  20670. #endif
  20671. }
  20672. /**
  20673. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  20674. */
  20675. BUILDIN_FUNC(navigateto){
  20676. #if PACKETVER >= 20111010
  20677. TBL_PC* sd;
  20678. const char *mapname;
  20679. uint16 x = 0, y = 0, monster_id = 0;
  20680. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  20681. bool hideWindow = true;
  20682. mapname = script_getstr(st,2);
  20683. if( script_hasdata(st,3) )
  20684. x = script_getnum(st,3);
  20685. if( script_hasdata(st,4) )
  20686. y = script_getnum(st,4);
  20687. if( script_hasdata(st,5) )
  20688. flag = (uint8)script_getnum(st,5);
  20689. if( script_hasdata(st,6) )
  20690. hideWindow = script_getnum(st,6) ? true : false;
  20691. if( script_hasdata(st,7) )
  20692. monster_id = script_getnum(st,7);
  20693. if (!script_charid2sd(8, sd))
  20694. return SCRIPT_CMD_FAILURE;
  20695. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  20696. return SCRIPT_CMD_SUCCESS;
  20697. #else
  20698. return SCRIPT_CMD_FAILURE;
  20699. #endif
  20700. }
  20701. /**
  20702. * adopt("<parent_name>","<baby_name>");
  20703. * adopt(<parent_id>,<baby_id>);
  20704. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  20705. */
  20706. BUILDIN_FUNC(adopt)
  20707. {
  20708. TBL_PC *sd, *b_sd;
  20709. enum adopt_responses response;
  20710. if (script_isstring(st, 2)) {
  20711. const char *name = script_getstr(st, 2);
  20712. sd = map_nick2sd(name,false);
  20713. if (sd == NULL) {
  20714. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  20715. return SCRIPT_CMD_FAILURE;
  20716. }
  20717. } else {
  20718. uint32 char_id = script_getnum(st, 2);
  20719. sd = map_charid2sd(char_id);
  20720. if (sd == NULL) {
  20721. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  20722. return SCRIPT_CMD_FAILURE;
  20723. }
  20724. }
  20725. if (script_isstring(st, 3)) {
  20726. const char *name = script_getstr(st, 3);
  20727. b_sd = map_nick2sd(name,false);
  20728. if (b_sd == NULL) {
  20729. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  20730. return SCRIPT_CMD_FAILURE;
  20731. }
  20732. } else {
  20733. uint32 char_id = script_getnum(st, 3);
  20734. b_sd = map_charid2sd(char_id);
  20735. if (b_sd == NULL) {
  20736. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  20737. return SCRIPT_CMD_FAILURE;
  20738. }
  20739. }
  20740. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  20741. if (response == ADOPT_ALLOWED) {
  20742. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  20743. b_sd->adopt_invite = sd->status.account_id;
  20744. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  20745. script_pushint(st, ADOPT_ALLOWED);
  20746. return SCRIPT_CMD_SUCCESS;
  20747. }
  20748. script_pushint(st, response);
  20749. return SCRIPT_CMD_FAILURE;
  20750. }
  20751. /**
  20752. * Returns the minimum or maximum of all the given parameters for integer variables.
  20753. *
  20754. * min( <value or array>{,value or array 2,...} );
  20755. * minimum( <value or array>{,value or array 2,...} );
  20756. * max( <value or array>{,value or array 2,...} );
  20757. * maximum( <value or array>{,value or array 2,...} );
  20758. */
  20759. BUILDIN_FUNC(minmax){
  20760. char *functionname;
  20761. unsigned int i;
  20762. int64 value;
  20763. // Function pointer for our comparison function (either min or max at the moment)
  20764. int64 (*func)(int64, int64);
  20765. // Get the real function name
  20766. functionname = script_getfuncname(st);
  20767. // Our data should start at offset 2
  20768. i = 2;
  20769. if( !script_hasdata( st, i ) ){
  20770. ShowError( "buildin_%s: no arguments given!\n", functionname );
  20771. st->state = END;
  20772. return SCRIPT_CMD_FAILURE;
  20773. }
  20774. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  20775. value = SCRIPT_INT_MAX;
  20776. func = i64min;
  20777. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  20778. value = SCRIPT_INT_MIN;
  20779. func = i64max;
  20780. }else{
  20781. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  20782. st->state = END;
  20783. return SCRIPT_CMD_FAILURE;
  20784. }
  20785. // As long as we have data on our script stack
  20786. while( script_hasdata(st,i) ){
  20787. struct script_data *data;
  20788. // Get the next piece of data from the script stack
  20789. data = script_getdata( st, i );
  20790. // Is the current parameter a single integer?
  20791. if( data_isint( data ) ){
  20792. value = func( value, script_getnum( st, i ) );
  20793. // Is the current parameter an array variable?
  20794. }else if( data_isreference( data ) ){
  20795. const char *name;
  20796. map_session_data* sd;
  20797. unsigned int start, end;
  20798. // Get the name of the variable
  20799. name = reference_getname(data);
  20800. // Check if it's a string variable
  20801. if( is_string_variable( name ) ){
  20802. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20803. script_reportdata( data );
  20804. st->state = END;
  20805. return SCRIPT_CMD_FAILURE;
  20806. }
  20807. // Get the session data, if a player is attached
  20808. sd = st->rid ? map_id2sd(st->rid) : NULL;
  20809. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20810. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20811. return SCRIPT_CMD_FAILURE;
  20812. }
  20813. // Try to find the array's source pointer
  20814. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20815. ShowError( "buildin_%s: not a array!\n", functionname );
  20816. script_reportdata( data );
  20817. st->state = END;
  20818. return SCRIPT_CMD_FAILURE;
  20819. }
  20820. // Get the start and end indices of the array
  20821. start = reference_getindex( data );
  20822. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20823. // Skip empty arrays
  20824. if( start < end ){
  20825. int id;
  20826. // For getting the values we need the id of the array
  20827. id = reference_getid( data );
  20828. // Loop through each value stored in the array
  20829. for( ; start < end; start++ ){
  20830. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20831. }
  20832. }
  20833. else {
  20834. value = func( value, 0 );
  20835. }
  20836. }else{
  20837. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20838. script_reportdata( data );
  20839. st->state = END;
  20840. return SCRIPT_CMD_FAILURE;
  20841. }
  20842. // Continue with the next stack entry
  20843. i++;
  20844. }
  20845. script_pushint( st, value );
  20846. return SCRIPT_CMD_SUCCESS;
  20847. }
  20848. BUILDIN_FUNC(cap_value)
  20849. {
  20850. script_pushint64(st, cap_value(script_getnum64(st, 2), script_getnum64(st, 3), script_getnum64(st, 4)));
  20851. return SCRIPT_CMD_SUCCESS;
  20852. }
  20853. /**
  20854. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20855. * Unlike `getexp` that affected by some adjustments.
  20856. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20857. * @author [Cydh]
  20858. **/
  20859. BUILDIN_FUNC(getexp2) {
  20860. TBL_PC *sd = NULL;
  20861. int64 base_exp = script_getnum64(st, 2);
  20862. int64 job_exp = script_getnum64(st, 3);
  20863. if (!script_charid2sd(4, sd))
  20864. return SCRIPT_CMD_FAILURE;
  20865. if( base_exp == 0 && job_exp == 0 ){
  20866. ShowError( "buildin_getexp2: Called with base and job exp 0.\n" );
  20867. return SCRIPT_CMD_FAILURE;
  20868. }
  20869. if (base_exp > 0)
  20870. pc_gainexp(sd, NULL, base_exp, 0, 2);
  20871. else if (base_exp < 0)
  20872. pc_lostexp(sd, base_exp * -1, 0);
  20873. if (job_exp > 0)
  20874. pc_gainexp(sd, NULL, 0, job_exp, 2);
  20875. else if (job_exp < 0)
  20876. pc_lostexp(sd, 0, job_exp * -1);
  20877. return SCRIPT_CMD_SUCCESS;
  20878. }
  20879. /**
  20880. * Force stat recalculation of sd
  20881. * recalculatestat;
  20882. * @author [secretdataz]
  20883. **/
  20884. BUILDIN_FUNC(recalculatestat) {
  20885. TBL_PC* sd;
  20886. if (!script_rid2sd(sd))
  20887. return SCRIPT_CMD_FAILURE;
  20888. status_calc_pc(sd, SCO_FORCE);
  20889. return SCRIPT_CMD_SUCCESS;
  20890. }
  20891. BUILDIN_FUNC(hateffect){
  20892. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20893. map_session_data* sd;
  20894. if( !script_rid2sd(sd) )
  20895. return SCRIPT_CMD_FAILURE;
  20896. int16 effectID = script_getnum(st,2);
  20897. bool enable = script_getnum(st,3) ? true : false;
  20898. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20899. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20900. return SCRIPT_CMD_FAILURE;
  20901. }
  20902. auto it = util::vector_get( sd->hatEffects, effectID );
  20903. if( enable ){
  20904. if( it != sd->hatEffects.end() ){
  20905. return SCRIPT_CMD_SUCCESS;
  20906. }
  20907. sd->hatEffects.push_back( effectID );
  20908. }else{
  20909. if( it == sd->hatEffects.end() ){
  20910. return SCRIPT_CMD_SUCCESS;
  20911. }
  20912. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20913. }
  20914. if( !sd->state.connect_new ){
  20915. clif_hat_effect_single( sd, effectID, enable );
  20916. }
  20917. #endif
  20918. return SCRIPT_CMD_SUCCESS;
  20919. }
  20920. /**
  20921. * Retrieves param of current random option. Intended for random option script only.
  20922. * getrandomoptinfo(<type>);
  20923. * @author [secretdataz]
  20924. **/
  20925. BUILDIN_FUNC(getrandomoptinfo) {
  20926. map_session_data *sd;
  20927. int val;
  20928. 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) {
  20929. int param = script_getnum(st, 2);
  20930. switch (param) {
  20931. case ROA_ID:
  20932. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20933. break;
  20934. case ROA_VALUE:
  20935. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20936. break;
  20937. case ROA_PARAM:
  20938. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  20939. break;
  20940. default:
  20941. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  20942. val = 0;
  20943. break;
  20944. }
  20945. script_pushint(st, val);
  20946. }
  20947. else {
  20948. script_pushint(st, 0);
  20949. }
  20950. return SCRIPT_CMD_SUCCESS;
  20951. }
  20952. /**
  20953. * Retrieves a random option on an equipped item.
  20954. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  20955. * @author [secretdataz]
  20956. */
  20957. BUILDIN_FUNC(getequiprandomoption) {
  20958. map_session_data *sd;
  20959. int val;
  20960. short i = -1;
  20961. int pos = script_getnum(st, 2);
  20962. int index = script_getnum(st, 3);
  20963. int type = script_getnum(st, 4);
  20964. if (!script_charid2sd(5, sd)) {
  20965. script_pushint(st, -1);
  20966. return SCRIPT_CMD_FAILURE;
  20967. }
  20968. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20969. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  20970. script_pushint(st, -1);
  20971. return SCRIPT_CMD_FAILURE;
  20972. }
  20973. if (equip_index_check(pos))
  20974. i = pc_checkequip(sd, equip_bitmask[pos]);
  20975. if (i < 0) {
  20976. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20977. script_pushint(st, -1);
  20978. return SCRIPT_CMD_FAILURE;
  20979. }
  20980. switch (type) {
  20981. case ROA_ID:
  20982. val = sd->inventory.u.items_inventory[i].option[index].id;
  20983. break;
  20984. case ROA_VALUE:
  20985. val = sd->inventory.u.items_inventory[i].option[index].value;
  20986. break;
  20987. case ROA_PARAM:
  20988. val = sd->inventory.u.items_inventory[i].option[index].param;
  20989. break;
  20990. default:
  20991. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  20992. val = 0;
  20993. break;
  20994. }
  20995. script_pushint(st, val);
  20996. return SCRIPT_CMD_SUCCESS;
  20997. }
  20998. /**
  20999. * Adds a random option on a random option slot on an equipped item and overwrites
  21000. * existing random option in the process.
  21001. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  21002. * @author [secretdataz]
  21003. */
  21004. BUILDIN_FUNC(setrandomoption) {
  21005. map_session_data *sd;
  21006. int pos, index, id, value, param, ep;
  21007. int i = -1;
  21008. if (!script_charid2sd(7, sd))
  21009. return SCRIPT_CMD_FAILURE;
  21010. pos = script_getnum(st, 2);
  21011. index = script_getnum(st, 3);
  21012. id = script_getnum(st, 4);
  21013. value = script_getnum(st, 5);
  21014. param = script_getnum(st, 6);
  21015. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  21016. if (opt == nullptr) {
  21017. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  21018. script_pushint(st, 0);
  21019. return SCRIPT_CMD_FAILURE;
  21020. }
  21021. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  21022. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  21023. script_pushint(st, 0);
  21024. return SCRIPT_CMD_FAILURE;
  21025. }
  21026. if (equip_index_check(pos))
  21027. i = pc_checkequip(sd, equip_bitmask[pos]);
  21028. if (i >= 0) {
  21029. ep = sd->inventory.u.items_inventory[i].equip;
  21030. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  21031. pc_unequipitem(sd, i, 2);
  21032. sd->inventory.u.items_inventory[i].option[index].id = id;
  21033. sd->inventory.u.items_inventory[i].option[index].value = value;
  21034. sd->inventory.u.items_inventory[i].option[index].param = param;
  21035. clif_delitem(sd, i, 1, 3);
  21036. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  21037. clif_additem(sd, i, 1, 0);
  21038. pc_equipitem(sd, i, ep);
  21039. script_pushint(st, 1);
  21040. return SCRIPT_CMD_SUCCESS;
  21041. }
  21042. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  21043. script_pushint(st, 0);
  21044. return SCRIPT_CMD_FAILURE;
  21045. }
  21046. /// Returns the number of stat points needed to change the specified stat by val.
  21047. /// If val is negative, returns the number of stat points that would be needed to
  21048. /// raise the specified stat from (current value - val) to current value.
  21049. /// *needed_status_point(<type>,<val>{,<char id>});
  21050. /// @author [secretdataz]
  21051. BUILDIN_FUNC(needed_status_point) {
  21052. map_session_data *sd;
  21053. int type, val;
  21054. if (!script_charid2sd(4, sd))
  21055. return SCRIPT_CMD_FAILURE;
  21056. type = script_getnum(st, 2);
  21057. val = script_getnum(st, 3);
  21058. script_pushint(st, pc_need_status_point(sd, type, val));
  21059. return SCRIPT_CMD_SUCCESS;
  21060. }
  21061. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  21062. /// If val is negative, returns the number of trait stat points that would be needed to
  21063. /// raise the specified trait stat from (current value - val) to current value.
  21064. /// *needed_trait_point(<type>,<val>{,<char id>});
  21065. BUILDIN_FUNC(needed_trait_point) {
  21066. map_session_data *sd;
  21067. if (!script_charid2sd(4, sd))
  21068. return SCRIPT_CMD_FAILURE;
  21069. int type = script_getnum( st, 2 );
  21070. if( type < SP_POW || type > SP_CRT ){
  21071. ShowError( "buildin_needed_trait_point: Unknown trait type %d\n", type );
  21072. return SCRIPT_CMD_FAILURE;
  21073. }
  21074. script_pushint( st, pc_need_trait_point( sd, type, script_getnum( st, 3 ) ) );
  21075. return SCRIPT_CMD_SUCCESS;
  21076. }
  21077. /**
  21078. * jobcanentermap("<mapname>"{,<JobID>});
  21079. * Check if (player with) JobID can enter the map.
  21080. * @param mapname Map name
  21081. * @param JobID Player's JobID (optional)
  21082. **/
  21083. BUILDIN_FUNC(jobcanentermap) {
  21084. const char *mapname = script_getstr(st, 2);
  21085. int mapidx = mapindex_name2id(mapname), m = -1;
  21086. int jobid = 0;
  21087. TBL_PC *sd = NULL;
  21088. if (!mapidx) {// Invalid map
  21089. script_pushint(st, false);
  21090. return SCRIPT_CMD_FAILURE;
  21091. }
  21092. m = map_mapindex2mapid(mapidx);
  21093. if (m == -1) { // Map is on different map server
  21094. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  21095. script_pushint(st, false);
  21096. return SCRIPT_CMD_FAILURE;
  21097. }
  21098. if (script_hasdata(st, 3)) {
  21099. jobid = script_getnum(st, 3);
  21100. } else {
  21101. if (!script_rid2sd(sd)) {
  21102. script_pushint(st, false);
  21103. return SCRIPT_CMD_FAILURE;
  21104. }
  21105. jobid = sd->status.class_;
  21106. }
  21107. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? pc_get_group_level(sd) : 0));
  21108. return SCRIPT_CMD_SUCCESS;
  21109. }
  21110. /**
  21111. * Return alliance information between the two guilds.
  21112. * getguildalliance(<guild id1>,<guild id2>);
  21113. * Return values:
  21114. * -2 - Guild ID1 does not exist
  21115. * -1 - Guild ID2 does not exist
  21116. * 0 - Both guilds have no relation OR guild ID aren't given
  21117. * 1 - Both guilds are allies
  21118. * 2 - Both guilds are antagonists
  21119. */
  21120. BUILDIN_FUNC(getguildalliance)
  21121. {
  21122. int guild_id1, guild_id2, i = 0;
  21123. guild_id1 = script_getnum(st,2);
  21124. guild_id2 = script_getnum(st,3);
  21125. if (guild_id1 < 1 || guild_id2 < 1) {
  21126. script_pushint(st, 0);
  21127. return SCRIPT_CMD_SUCCESS;
  21128. }
  21129. if (guild_id1 == guild_id2) {
  21130. script_pushint(st, 1);
  21131. return SCRIPT_CMD_SUCCESS;
  21132. }
  21133. auto guild_data1 = guild_search(guild_id1);
  21134. auto guild_data2 = guild_search(guild_id2);
  21135. if (guild_data1 == NULL) {
  21136. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  21137. script_pushint(st, -2);
  21138. return SCRIPT_CMD_FAILURE;
  21139. }
  21140. if (guild_data2 == NULL) {
  21141. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  21142. script_pushint(st, -1);
  21143. return SCRIPT_CMD_FAILURE;
  21144. }
  21145. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->guild.alliance[i].guild_id == guild_id2);
  21146. if (i == MAX_GUILDALLIANCE) {
  21147. script_pushint(st, 0);
  21148. return SCRIPT_CMD_SUCCESS;
  21149. }
  21150. if (guild_data1->guild.alliance[i].opposition)
  21151. script_pushint(st, 2);
  21152. else
  21153. script_pushint(st, 1);
  21154. return SCRIPT_CMD_SUCCESS;
  21155. }
  21156. /*
  21157. * openstorage2 <storage_id>,<mode>{,<account_id>}
  21158. * mode @see enum e_storage_mode
  21159. **/
  21160. BUILDIN_FUNC(openstorage2) {
  21161. int stor_id = script_getnum(st, 2);
  21162. TBL_PC *sd = NULL;
  21163. if (!script_accid2sd(4, sd)) {
  21164. script_pushint(st, 0);
  21165. return SCRIPT_CMD_FAILURE;
  21166. }
  21167. if (!storage_exists(stor_id)) {
  21168. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  21169. return SCRIPT_CMD_FAILURE;
  21170. }
  21171. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  21172. return SCRIPT_CMD_SUCCESS;
  21173. }
  21174. /**
  21175. * Create a new channel
  21176. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  21177. * @author [Cydh]
  21178. **/
  21179. BUILDIN_FUNC(channel_create) {
  21180. struct Channel tmp_chan, *ch = NULL;
  21181. const char *chname = script_getstr(st,2), *pass = NULL;
  21182. int i = channel_chk((char*)chname, NULL, 3);
  21183. TBL_PC *sd = NULL;
  21184. if (i != 0) {
  21185. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  21186. script_pushint(st,i);
  21187. return SCRIPT_CMD_FAILURE;
  21188. }
  21189. memset(&tmp_chan, 0, sizeof(struct Channel));
  21190. if (script_hasdata(st,8)) {
  21191. tmp_chan.char_id = script_getnum(st,8);
  21192. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  21193. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  21194. script_pushint(st,-5);
  21195. return SCRIPT_CMD_FAILURE;
  21196. }
  21197. tmp_chan.type = CHAN_TYPE_PRIVATE;
  21198. i = 1;
  21199. }
  21200. else {
  21201. tmp_chan.type = CHAN_TYPE_PUBLIC;
  21202. i = 0;
  21203. }
  21204. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  21205. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  21206. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  21207. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  21208. if (script_hasdata(st,5))
  21209. tmp_chan.opt = script_getnum(st,5);
  21210. else
  21211. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  21212. if (script_hasdata(st,6))
  21213. tmp_chan.msg_delay = script_getnum(st,6);
  21214. else
  21215. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  21216. if (script_hasdata(st,7))
  21217. tmp_chan.color = script_getnum(st,7);
  21218. else
  21219. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  21220. if (!(ch = channel_create(&tmp_chan))) {
  21221. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  21222. script_pushint(st,0);
  21223. return SCRIPT_CMD_FAILURE;
  21224. }
  21225. if (tmp_chan.char_id)
  21226. channel_join(ch, sd);
  21227. script_pushint(st,1);
  21228. return SCRIPT_CMD_SUCCESS;
  21229. }
  21230. // ===================================
  21231. // *channel_join "<channel_name>"{, <char_id>};
  21232. // Join an existing channel.
  21233. // The command returns 0 upon success, and these values upon failure:
  21234. // -1 : Invalid channel or player
  21235. // -2 : Player already in channel
  21236. // -3 : Player banned
  21237. // -4 : Reached max limit
  21238. // ===================================
  21239. BUILDIN_FUNC(channel_join) {
  21240. map_session_data *sd = nullptr;
  21241. if (!script_charid2sd(3, sd)) {
  21242. script_pushint(st, -1);
  21243. return SCRIPT_CMD_FAILURE;
  21244. }
  21245. struct Channel *channel = nullptr;
  21246. const char *chname = script_getstr(st, 2);
  21247. if (!(channel = channel_name2channel((char *)chname, nullptr, 0))) {
  21248. ShowError("buildin_channel_join: Channel name '%s' is invalid.\n", chname);
  21249. script_pushint(st, -1);
  21250. return SCRIPT_CMD_FAILURE;
  21251. }
  21252. script_pushint(st, channel_join(channel, sd));
  21253. return SCRIPT_CMD_SUCCESS;
  21254. }
  21255. /**
  21256. * Set channel option
  21257. * channel_setopt "<chname>",<option>,<value>;
  21258. * @author [Cydh]
  21259. **/
  21260. BUILDIN_FUNC(channel_setopt) {
  21261. struct Channel *ch = NULL;
  21262. const char *chname = script_getstr(st,2);
  21263. int opt = script_getnum(st,3), value = script_getnum(st,4);
  21264. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21265. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  21266. script_pushint(st,0);
  21267. return SCRIPT_CMD_FAILURE;
  21268. }
  21269. switch (opt) {
  21270. case CHAN_OPT_ANNOUNCE_SELF:
  21271. case CHAN_OPT_ANNOUNCE_JOIN:
  21272. case CHAN_OPT_ANNOUNCE_LEAVE:
  21273. case CHAN_OPT_COLOR_OVERRIDE:
  21274. case CHAN_OPT_CAN_CHAT:
  21275. case CHAN_OPT_CAN_LEAVE:
  21276. case CHAN_OPT_AUTOJOIN:
  21277. if (value)
  21278. ch->opt |= opt;
  21279. else
  21280. ch->opt &= ~opt;
  21281. break;
  21282. case CHAN_OPT_MSG_DELAY:
  21283. ch->msg_delay = value;
  21284. break;
  21285. default:
  21286. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  21287. script_pushint(st,0);
  21288. return SCRIPT_CMD_FAILURE;
  21289. }
  21290. script_pushint(st,1);
  21291. return SCRIPT_CMD_SUCCESS;
  21292. }
  21293. /**
  21294. * Get channel options
  21295. * channel_getopt <chname>,<option>;
  21296. */
  21297. BUILDIN_FUNC(channel_getopt) {
  21298. Channel *ch;
  21299. const char *chname = script_getstr(st, 2);
  21300. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21301. ShowError("buildin_channel_getopt: Channel name '%s' is invalid.\n", chname);
  21302. script_pushint(st, false);
  21303. return SCRIPT_CMD_FAILURE;
  21304. }
  21305. int opt = script_getnum(st, 3);
  21306. switch (opt) {
  21307. case CHAN_OPT_ANNOUNCE_SELF:
  21308. case CHAN_OPT_ANNOUNCE_JOIN:
  21309. case CHAN_OPT_ANNOUNCE_LEAVE:
  21310. case CHAN_OPT_COLOR_OVERRIDE:
  21311. case CHAN_OPT_CAN_CHAT:
  21312. case CHAN_OPT_CAN_LEAVE:
  21313. case CHAN_OPT_AUTOJOIN:
  21314. script_pushint(st, (ch->opt & opt) != 0);
  21315. break;
  21316. case CHAN_OPT_MSG_DELAY:
  21317. script_pushint(st, ch->msg_delay);
  21318. break;
  21319. default:
  21320. ShowError("buildin_channel_getopt: Invalid option %d!\n", opt);
  21321. script_pushint(st, false);
  21322. return SCRIPT_CMD_FAILURE;
  21323. }
  21324. return SCRIPT_CMD_SUCCESS;
  21325. }
  21326. /**
  21327. * Set channel color
  21328. * channel_setcolor "<chname>",<color>;
  21329. * @author [Cydh]
  21330. **/
  21331. BUILDIN_FUNC(channel_setcolor) {
  21332. struct Channel *ch = NULL;
  21333. const char *chname = script_getstr(st,2);
  21334. int color = script_getnum(st,3);
  21335. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21336. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  21337. script_pushint(st,0);
  21338. return SCRIPT_CMD_FAILURE;
  21339. }
  21340. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  21341. script_pushint(st,1);
  21342. return SCRIPT_CMD_SUCCESS;
  21343. }
  21344. /**
  21345. * Set channel password
  21346. * channel_setpass "<chname>","<password>";
  21347. * @author [Cydh]
  21348. **/
  21349. BUILDIN_FUNC(channel_setpass) {
  21350. struct Channel *ch = NULL;
  21351. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  21352. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21353. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  21354. script_pushint(st,0);
  21355. return SCRIPT_CMD_FAILURE;
  21356. }
  21357. if (!passwd || !strcmpi(passwd,"null"))
  21358. memset(ch->pass, '\0', sizeof(ch->pass));
  21359. else
  21360. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  21361. script_pushint(st,1);
  21362. return SCRIPT_CMD_SUCCESS;
  21363. }
  21364. /**
  21365. * Set authorized groups
  21366. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  21367. * @author [Cydh]
  21368. **/
  21369. BUILDIN_FUNC(channel_setgroup) {
  21370. struct Channel *ch = NULL;
  21371. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  21372. int i, n = 0, group = 0;
  21373. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21374. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  21375. script_pushint(st,0);
  21376. return SCRIPT_CMD_FAILURE;
  21377. }
  21378. if (funcname[strlen(funcname)-1] == '2') {
  21379. struct script_data *data = script_getdata(st,3);
  21380. const char *varname = reference_getname(data);
  21381. int32 id, idx;
  21382. if (varname[strlen(varname)-1] == '$') {
  21383. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  21384. script_pushint(st,0);
  21385. return SCRIPT_CMD_FAILURE;
  21386. }
  21387. if (not_server_variable(*varname)) {
  21388. map_session_data *sd;
  21389. if (!script_rid2sd(sd)) {
  21390. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  21391. return SCRIPT_CMD_FAILURE;
  21392. }
  21393. }
  21394. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  21395. if (n < 1) {
  21396. ShowError("buildin_channel_setgroup: No group id listed.\n");
  21397. script_pushint(st,0);
  21398. return SCRIPT_CMD_FAILURE;
  21399. }
  21400. if (ch->groups)
  21401. aFree(ch->groups);
  21402. ch->groups = NULL;
  21403. ch->group_count = 0;
  21404. id = reference_getid(data);
  21405. idx = reference_getindex(data);
  21406. for (i = 0; i < n; i++) {
  21407. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  21408. if (group == 0) {
  21409. script_pushint(st,1);
  21410. return SCRIPT_CMD_SUCCESS;
  21411. }
  21412. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  21413. ch->groups[ch->group_count-1] = group;
  21414. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21415. }
  21416. }
  21417. else {
  21418. group = script_getnum(st,3);
  21419. n = script_lastdata(st)-1;
  21420. if (n < 1) {
  21421. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  21422. script_pushint(st,0);
  21423. return SCRIPT_CMD_FAILURE;
  21424. }
  21425. if (ch->groups)
  21426. aFree(ch->groups);
  21427. ch->groups = NULL;
  21428. ch->group_count = 0;
  21429. if (group == 0) { // Removed group list
  21430. script_pushint(st,1);
  21431. return SCRIPT_CMD_SUCCESS;
  21432. }
  21433. CREATE(ch->groups, unsigned short, n);
  21434. for (i = 3; i < n+2; i++) {
  21435. ch->groups[ch->group_count++] = script_getnum(st,i);
  21436. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21437. }
  21438. }
  21439. script_pushint(st,n);
  21440. return SCRIPT_CMD_SUCCESS;
  21441. }
  21442. /**
  21443. * Send message on channel
  21444. * channel_chat "<chname>","<message>"{,<color>};
  21445. * @author [Cydh]
  21446. **/
  21447. BUILDIN_FUNC(channel_chat) {
  21448. struct Channel *ch = NULL;
  21449. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  21450. char output[CHAT_SIZE_MAX+1];
  21451. unsigned long color = 0;
  21452. // Check also local channels
  21453. if (chname[0] != '#') { // By Map
  21454. int m = mapindex_name2id(chname);
  21455. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  21456. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  21457. script_pushint(st,0);
  21458. return SCRIPT_CMD_FAILURE;
  21459. }
  21460. if (!(ch = map_getmapdata(m)->channel)) {
  21461. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21462. script_pushint(st,0);
  21463. return SCRIPT_CMD_SUCCESS;
  21464. }
  21465. }
  21466. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  21467. TBL_NPC *nd = map_id2nd(st->oid);
  21468. if (!nd || nd->bl.m == -1) {
  21469. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  21470. script_pushint(st,0);
  21471. return SCRIPT_CMD_FAILURE;
  21472. }
  21473. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  21474. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21475. script_pushint(st,0);
  21476. return SCRIPT_CMD_SUCCESS;
  21477. }
  21478. }
  21479. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21480. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  21481. script_pushint(st,0);
  21482. return SCRIPT_CMD_FAILURE;
  21483. }
  21484. if (!ch) {
  21485. script_pushint(st,0);
  21486. return SCRIPT_CMD_FAILURE;
  21487. }
  21488. color = ch->color;
  21489. FETCH(4, color);
  21490. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  21491. clif_channel_msg(ch,output,color);
  21492. script_pushint(st,1);
  21493. return SCRIPT_CMD_SUCCESS;
  21494. }
  21495. /**
  21496. * Ban player from a channel
  21497. * channel_ban "<chname>",<char_id>;
  21498. * @author [Cydh]
  21499. **/
  21500. BUILDIN_FUNC(channel_ban) {
  21501. struct Channel *ch = NULL;
  21502. const char *chname = script_getstr(st,2);
  21503. unsigned int char_id = script_getnum(st,3);
  21504. TBL_PC *tsd;
  21505. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21506. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  21507. script_pushint(st,0);
  21508. return SCRIPT_CMD_FAILURE;
  21509. }
  21510. if (ch->char_id == char_id) {
  21511. script_pushint(st,0);
  21512. return SCRIPT_CMD_SUCCESS;
  21513. }
  21514. tsd = map_charid2sd(char_id);
  21515. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21516. script_pushint(st,0);
  21517. return SCRIPT_CMD_SUCCESS;
  21518. }
  21519. if (!idb_exists(ch->banned, char_id)) {
  21520. struct chan_banentry *cbe;
  21521. char output[CHAT_SIZE_MAX+1];
  21522. CREATE(cbe, struct chan_banentry, 1);
  21523. cbe->char_id = char_id;
  21524. if (tsd) {
  21525. strcpy(cbe->char_name,tsd->status.name);
  21526. channel_clean(ch,tsd,0);
  21527. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  21528. }
  21529. else
  21530. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  21531. idb_put(ch->banned, char_id, cbe);
  21532. clif_channel_msg(ch,output,ch->color);
  21533. }
  21534. script_pushint(st,1);
  21535. return SCRIPT_CMD_SUCCESS;
  21536. }
  21537. /**
  21538. * Ban player from a channel
  21539. * channel_unban "<chname>",<char_id>;
  21540. * @author [Cydh]
  21541. **/
  21542. BUILDIN_FUNC(channel_unban) {
  21543. struct Channel *ch = NULL;
  21544. const char *chname = script_getstr(st,2);
  21545. unsigned int char_id = script_getnum(st,3);
  21546. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21547. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  21548. script_pushint(st,0);
  21549. return SCRIPT_CMD_FAILURE;
  21550. }
  21551. if (ch->char_id == char_id) {
  21552. script_pushint(st,0);
  21553. return SCRIPT_CMD_SUCCESS;
  21554. }
  21555. if (idb_exists(ch->banned, char_id)) {
  21556. char output[CHAT_SIZE_MAX+1];
  21557. TBL_PC *tsd = map_charid2sd(char_id);
  21558. if (tsd)
  21559. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  21560. else
  21561. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  21562. idb_remove(ch->banned, char_id);
  21563. clif_channel_msg(ch,output,ch->color);
  21564. }
  21565. script_pushint(st,1);
  21566. return SCRIPT_CMD_SUCCESS;
  21567. }
  21568. /**
  21569. * Kick player from a channel
  21570. * channel_kick "<chname>",<char_id>;
  21571. * channel_kick "<chname>","<char_name>";
  21572. * @author [Cydh]
  21573. **/
  21574. BUILDIN_FUNC(channel_kick) {
  21575. struct Channel *ch = NULL;
  21576. const char *chname = script_getstr(st,2);
  21577. TBL_PC *tsd = NULL;
  21578. int res = 1;
  21579. if (script_isstring(st, 3)) {
  21580. const char *name = script_getstr(st, 3);
  21581. if (!(tsd = map_nick2sd(name,false))) {
  21582. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  21583. script_pushint(st,0);
  21584. return SCRIPT_CMD_FAILURE;
  21585. }
  21586. } else {
  21587. int char_id = script_getnum(st, 3);
  21588. if (!(tsd = map_charid2sd(char_id))) {
  21589. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  21590. script_pushint(st,0);
  21591. return SCRIPT_CMD_FAILURE;
  21592. }
  21593. }
  21594. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  21595. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  21596. script_pushint(st,0);
  21597. return SCRIPT_CMD_FAILURE;
  21598. }
  21599. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21600. script_pushint(st,0);
  21601. return SCRIPT_CMD_SUCCESS;
  21602. }
  21603. switch(ch->type){
  21604. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  21605. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  21606. default: res = channel_clean(ch,tsd,0); break;
  21607. }
  21608. if (res == 0) {
  21609. char output[CHAT_SIZE_MAX+1];
  21610. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  21611. clif_channel_msg(ch,output,ch->color);
  21612. }
  21613. script_pushint(st,1);
  21614. return SCRIPT_CMD_SUCCESS;
  21615. }
  21616. /**
  21617. * Delete a channel
  21618. * channel_delete "<chname>";
  21619. * @author [Cydh]
  21620. **/
  21621. BUILDIN_FUNC(channel_delete) {
  21622. struct Channel *ch = NULL;
  21623. const char *chname = script_getstr(st,2);
  21624. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21625. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  21626. script_pushint(st,-1);
  21627. return SCRIPT_CMD_FAILURE;
  21628. }
  21629. script_pushint(st,channel_delete(ch,true));
  21630. return SCRIPT_CMD_SUCCESS;
  21631. }
  21632. BUILDIN_FUNC(unloadnpc) {
  21633. const char *name;
  21634. struct npc_data* nd;
  21635. name = script_getstr(st, 2);
  21636. nd = npc_name2id(name);
  21637. if( nd == NULL ){
  21638. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  21639. return SCRIPT_CMD_FAILURE;
  21640. } else if ( nd->bl.id == st->oid ) {
  21641. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  21642. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  21643. return SCRIPT_CMD_FAILURE;
  21644. }
  21645. npc_unload_duplicates(nd);
  21646. npc_unload(nd, true);
  21647. npc_read_event_script();
  21648. return SCRIPT_CMD_SUCCESS;
  21649. }
  21650. /**
  21651. * Duplicate a NPC.
  21652. * Return the duplicate Unique name on success or empty string on failure.
  21653. * duplicate "<NPC name>","<map>",<x>,<y>{,"<Duplicate NPC name>"{,<sprite>{,<dir>{,<xs>{,<xy>}}}}};
  21654. */
  21655. BUILDIN_FUNC(duplicate)
  21656. {
  21657. const char* old_npcname = script_getstr( st, 2 );
  21658. npc_data* nd = npc_name2id( old_npcname );
  21659. if( nd == nullptr ){
  21660. ShowError( "buildin_duplicate: No such NPC '%s'.\n", old_npcname );
  21661. script_pushstrcopy( st, "" );
  21662. return SCRIPT_CMD_FAILURE;
  21663. }
  21664. const char* mapname = script_getstr( st, 3 );
  21665. int16 mapid = map_mapname2mapid( mapname );
  21666. if( mapid < 0 ){
  21667. ShowError( "buildin_duplicate: map '%s' in not found!\n", mapname );
  21668. script_pushstrcopy( st, "" );
  21669. return SCRIPT_CMD_FAILURE;
  21670. }
  21671. struct map_data* mapdata = map_getmapdata( mapid );
  21672. if( mapdata == nullptr ){
  21673. // Should not happen, but who knows...
  21674. ShowError( "buildin_duplicate: mapdata for '%s' is unavailable!\n", mapname );
  21675. script_pushstrcopy( st, "" );
  21676. return SCRIPT_CMD_FAILURE;
  21677. }
  21678. int16 x = script_getnum( st, 4 );
  21679. if( x < 0 || x >= mapdata->xs ){
  21680. ShowError( "buildin_duplicate: x coordinate %hd is out of bounds for map %s[0-%hd]!\n", x, mapname, mapdata->xs );
  21681. script_pushstrcopy( st, "" );
  21682. return SCRIPT_CMD_FAILURE;
  21683. }
  21684. int16 y = script_getnum( st, 5 );
  21685. if( y < 0 || y >= mapdata->ys ){
  21686. ShowError( "buildin_duplicate: y coordinate %hd is out of bounds for map %s[0-%hd]!\n", y, mapname, mapdata->ys );
  21687. script_pushstrcopy( st, "" );
  21688. return SCRIPT_CMD_FAILURE;
  21689. }
  21690. char name[NPC_NAME_LENGTH + 1];
  21691. if( script_hasdata( st, 6 ) ){
  21692. const char* new_name = script_getstr( st, 6 );
  21693. if( strlen( new_name ) > NPC_NAME_LENGTH ){
  21694. ShowError( "buildin_duplicate: new NPC name \"%s\" is too long!\n", new_name );
  21695. script_pushstrcopy( st, "" );
  21696. return SCRIPT_CMD_FAILURE;
  21697. }
  21698. safestrncpy( name, new_name, sizeof( name ) );
  21699. }else{
  21700. safestrncpy( name, nd->name, sizeof( name ) );
  21701. }
  21702. int class_;
  21703. if( script_hasdata( st, 7 ) ){
  21704. class_ = script_getnum( st, 7 );
  21705. }else{
  21706. class_ = nd->class_;
  21707. }
  21708. uint8 dir;
  21709. if( script_hasdata( st, 8 ) ){
  21710. dir = script_getnum( st, 8 );
  21711. }else{
  21712. dir = nd->ud.dir;
  21713. }
  21714. int16 xs;
  21715. if( script_hasdata( st, 9 ) ){
  21716. xs = script_getnum( st, 9 );
  21717. }else{
  21718. xs = nd->u.scr.xs;
  21719. }
  21720. int16 ys;
  21721. if( script_hasdata( st, 10 ) ){
  21722. ys = script_getnum( st, 10 );
  21723. }else{
  21724. ys = nd->u.scr.ys;
  21725. }
  21726. npc_data* dnd = npc_duplicate_npc( *nd, name, mapid, x, y, class_, dir, xs, ys );
  21727. if( dnd == nullptr ){
  21728. script_pushstrcopy( st, "" );
  21729. return SCRIPT_CMD_FAILURE;
  21730. }else{
  21731. script_pushstrcopy( st, dnd->exname );
  21732. return SCRIPT_CMD_SUCCESS;
  21733. }
  21734. }
  21735. /**
  21736. * Duplicate a NPC for a player.
  21737. * Return the duplicate Unique name on success or empty string on failure.
  21738. * duplicate_dynamic("<NPC name>"{,<character ID>});
  21739. */
  21740. BUILDIN_FUNC(duplicate_dynamic){
  21741. const char* old_npcname = script_getstr( st, 2 );
  21742. struct npc_data* nd = npc_name2id( old_npcname );
  21743. if( nd == nullptr ){
  21744. ShowError( "buildin_duplicate_dynamic: No such NPC '%s'.\n", old_npcname );
  21745. script_pushstrcopy( st, "" );
  21746. return SCRIPT_CMD_FAILURE;
  21747. }
  21748. map_session_data* sd;
  21749. if( !script_charid2sd( 3, sd ) ){
  21750. script_pushstrcopy( st, "" );
  21751. return SCRIPT_CMD_FAILURE;
  21752. }
  21753. struct npc_data* dnd = npc_duplicate_npc_for_player( *nd, *sd );
  21754. if( dnd == nullptr ){
  21755. script_pushstrcopy( st, "" );
  21756. return SCRIPT_CMD_SUCCESS;
  21757. }else{
  21758. script_pushstrcopy( st, dnd->exname );
  21759. return SCRIPT_CMD_SUCCESS;
  21760. }
  21761. }
  21762. /**
  21763. * Add an achievement to the player's log
  21764. * achievementadd(<achievement ID>{,<char ID>});
  21765. */
  21766. BUILDIN_FUNC(achievementadd) {
  21767. map_session_data *sd;
  21768. int achievement_id = script_getnum(st, 2);
  21769. if (!script_charid2sd(3, sd)) {
  21770. script_pushint(st, false);
  21771. return SCRIPT_CMD_FAILURE;
  21772. }
  21773. if (achievement_db.exists(achievement_id) == false) {
  21774. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  21775. script_pushint(st, false);
  21776. return SCRIPT_CMD_FAILURE;
  21777. }
  21778. if( !sd->state.pc_loaded ){
  21779. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21780. return SCRIPT_CMD_SUCCESS;
  21781. }
  21782. if (achievement_add(sd, achievement_id))
  21783. script_pushint(st, true);
  21784. else
  21785. script_pushint(st, false);
  21786. return SCRIPT_CMD_SUCCESS;
  21787. }
  21788. /**
  21789. * Removes an achievement on a player.
  21790. * achievementremove(<achievement ID>{,<char ID>});
  21791. * Just for Atemo. ;)
  21792. */
  21793. BUILDIN_FUNC(achievementremove) {
  21794. map_session_data *sd;
  21795. int achievement_id = script_getnum(st, 2);
  21796. if (!script_charid2sd(3, sd)) {
  21797. script_pushint(st, false);
  21798. return SCRIPT_CMD_FAILURE;
  21799. }
  21800. if (achievement_db.exists(achievement_id) == false) {
  21801. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  21802. script_pushint(st, false);
  21803. return SCRIPT_CMD_SUCCESS;
  21804. }
  21805. if( !sd->state.pc_loaded ){
  21806. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21807. return SCRIPT_CMD_SUCCESS;
  21808. }
  21809. if (achievement_remove(sd, achievement_id))
  21810. script_pushint(st, true);
  21811. else
  21812. script_pushint(st, false);
  21813. return SCRIPT_CMD_SUCCESS;
  21814. }
  21815. /**
  21816. * Returns achievement progress
  21817. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  21818. */
  21819. BUILDIN_FUNC(achievementinfo) {
  21820. map_session_data *sd;
  21821. int achievement_id = script_getnum(st, 2);
  21822. if (!script_charid2sd(4, sd)) {
  21823. script_pushint(st, false);
  21824. return SCRIPT_CMD_FAILURE;
  21825. }
  21826. if (achievement_db.exists(achievement_id) == false) {
  21827. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  21828. script_pushint(st, false);
  21829. return SCRIPT_CMD_FAILURE;
  21830. }
  21831. if( !sd->state.pc_loaded ){
  21832. script_pushint(st, false);
  21833. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21834. return SCRIPT_CMD_SUCCESS;
  21835. }
  21836. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  21837. return SCRIPT_CMD_SUCCESS;
  21838. }
  21839. /**
  21840. * Award an achievement; Ignores requirements
  21841. * achievementcomplete(<achievement ID>{,<char ID>});
  21842. */
  21843. BUILDIN_FUNC(achievementcomplete) {
  21844. map_session_data *sd;
  21845. int i, achievement_id = script_getnum(st, 2);
  21846. if (!script_charid2sd(3, sd)) {
  21847. script_pushint(st, false);
  21848. return SCRIPT_CMD_FAILURE;
  21849. }
  21850. if (achievement_db.exists(achievement_id) == false) {
  21851. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  21852. script_pushint(st, false);
  21853. return SCRIPT_CMD_FAILURE;
  21854. }
  21855. if( !sd->state.pc_loaded ){
  21856. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21857. return SCRIPT_CMD_SUCCESS;
  21858. }
  21859. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21860. if (i == sd->achievement_data.count)
  21861. achievement_add(sd, achievement_id);
  21862. achievement_update_achievement(sd, achievement_id, true);
  21863. script_pushint(st, true);
  21864. return SCRIPT_CMD_SUCCESS;
  21865. }
  21866. /**
  21867. * Checks if the achievement exists on player.
  21868. * achievementexists(<achievement ID>{,<char ID>});
  21869. */
  21870. BUILDIN_FUNC(achievementexists) {
  21871. map_session_data *sd;
  21872. int i, achievement_id = script_getnum(st, 2);
  21873. if (!script_charid2sd(3, sd)) {
  21874. script_pushint(st, false);
  21875. return SCRIPT_CMD_FAILURE;
  21876. }
  21877. if (achievement_db.exists(achievement_id) == false) {
  21878. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  21879. script_pushint(st, false);
  21880. return SCRIPT_CMD_SUCCESS;
  21881. }
  21882. if( !sd->state.pc_loaded ){
  21883. script_pushint(st, false);
  21884. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21885. return SCRIPT_CMD_SUCCESS;
  21886. }
  21887. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  21888. script_pushint(st, i < sd->achievement_data.count ? true : false);
  21889. return SCRIPT_CMD_SUCCESS;
  21890. }
  21891. /**
  21892. * Updates an achievement's value.
  21893. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  21894. */
  21895. BUILDIN_FUNC(achievementupdate) {
  21896. map_session_data *sd;
  21897. int i, achievement_id, type, value;
  21898. achievement_id = script_getnum(st, 2);
  21899. type = script_getnum(st, 3);
  21900. value = script_getnum(st, 4);
  21901. if (!script_charid2sd(5, sd)) {
  21902. script_pushint(st, false);
  21903. return SCRIPT_CMD_FAILURE;
  21904. }
  21905. if (achievement_db.exists(achievement_id) == false) {
  21906. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  21907. script_pushint(st, false);
  21908. return SCRIPT_CMD_FAILURE;
  21909. }
  21910. if( !sd->state.pc_loaded ){
  21911. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21912. return SCRIPT_CMD_SUCCESS;
  21913. }
  21914. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21915. if (i == sd->achievement_data.count)
  21916. achievement_add(sd, achievement_id);
  21917. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21918. if (i == sd->achievement_data.count) {
  21919. script_pushint(st, false);
  21920. return SCRIPT_CMD_SUCCESS;
  21921. }
  21922. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  21923. sd->achievement_data.achievements[i].count[type - 1] = value;
  21924. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  21925. sd->achievement_data.achievements[i].completed = value;
  21926. else if (type == ACHIEVEINFO_GOTREWARD)
  21927. sd->achievement_data.achievements[i].rewarded = value;
  21928. else {
  21929. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  21930. script_pushint(st, false);
  21931. return SCRIPT_CMD_FAILURE;
  21932. }
  21933. achievement_update_achievement(sd, achievement_id, false);
  21934. script_pushint(st, true);
  21935. return SCRIPT_CMD_SUCCESS;
  21936. }
  21937. /**
  21938. * Get an equipment's refine cost
  21939. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  21940. * returns -1 on fail
  21941. */
  21942. BUILDIN_FUNC(getequiprefinecost) {
  21943. int i = -1, slot, type, info;
  21944. map_session_data *sd;
  21945. slot = script_getnum(st, 2);
  21946. type = script_getnum(st, 3);
  21947. info = script_getnum(st, 4);
  21948. if (!script_charid2sd(5, sd)) {
  21949. script_pushint(st, 0);
  21950. return SCRIPT_CMD_FAILURE;
  21951. }
  21952. if (type < 0 || type >= REFINE_COST_MAX) {
  21953. script_pushint(st, -1);
  21954. return SCRIPT_CMD_SUCCESS;
  21955. }
  21956. if (equip_index_check(slot))
  21957. i = pc_checkequip(sd, equip_bitmask[slot]);
  21958. if (i < 0) {
  21959. script_pushint(st, -1);
  21960. return SCRIPT_CMD_SUCCESS;
  21961. }
  21962. if( sd->inventory_data[i] == nullptr ){
  21963. script_pushint( st, -1 );
  21964. return SCRIPT_CMD_SUCCESS;
  21965. }
  21966. std::shared_ptr<s_refine_level_info> level_info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  21967. if( level_info == nullptr ){
  21968. script_pushint( st, -1 );
  21969. return SCRIPT_CMD_SUCCESS;
  21970. }
  21971. std::shared_ptr<s_refine_cost> cost = util::umap_find( level_info->costs, (uint16)type );
  21972. if( cost == nullptr ){
  21973. script_pushint( st, -1 );
  21974. return SCRIPT_CMD_SUCCESS;
  21975. }
  21976. switch( info ){
  21977. case REFINE_MATERIAL_ID:
  21978. script_pushint( st, cost->nameid );
  21979. break;
  21980. case REFINE_ZENY_COST:
  21981. script_pushint( st, cost->zeny );
  21982. break;
  21983. default:
  21984. script_pushint( st, -1 );
  21985. return SCRIPT_CMD_FAILURE;
  21986. }
  21987. return SCRIPT_CMD_SUCCESS;
  21988. }
  21989. /**
  21990. * Round, floor, ceiling a number to arbitrary integer precision.
  21991. * round(<number>,<precision>);
  21992. * ceil(<number>,<precision>);
  21993. * floor(<number>,<precision>);
  21994. */
  21995. BUILDIN_FUNC(round) {
  21996. int num = script_getnum(st, 2);
  21997. int precision = script_getnum(st, 3);
  21998. char* func = script_getfuncname(st);
  21999. if (precision <= 0) {
  22000. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  22001. return SCRIPT_CMD_FAILURE;
  22002. }
  22003. if (strcasecmp(func, "floor") == 0) {
  22004. script_pushint(st, num - (num % precision));
  22005. }
  22006. else if (strcasecmp(func, "ceil") == 0) {
  22007. script_pushint(st, num + precision - (num % precision));
  22008. }
  22009. else {
  22010. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  22011. }
  22012. return SCRIPT_CMD_SUCCESS;
  22013. }
  22014. BUILDIN_FUNC(getequiptradability) {
  22015. int i, num;
  22016. TBL_PC *sd;
  22017. num = script_getnum(st, 2);
  22018. if (!script_charid2sd(3, sd)) {
  22019. return SCRIPT_CMD_FAILURE;
  22020. }
  22021. if (equip_index_check(num))
  22022. i = pc_checkequip(sd, equip_bitmask[num]);
  22023. else{
  22024. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  22025. return SCRIPT_CMD_FAILURE;
  22026. }
  22027. if (i >= 0)
  22028. script_pushint(st, pc_can_trade_item(sd, i));
  22029. else
  22030. script_pushint(st, false);
  22031. return SCRIPT_CMD_SUCCESS;
  22032. }
  22033. static inline bool mail_sub( struct script_state *st, struct script_data *data, map_session_data *sd, int i, const char **out_name, unsigned int *start, unsigned int *end, int32 *id ){
  22034. const char *name;
  22035. // Check if it is a variable
  22036. if( !data_isreference(data) ){
  22037. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  22038. return false;
  22039. }
  22040. name = reference_getname(data);
  22041. if( is_string_variable(name) ){
  22042. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  22043. return false;
  22044. }
  22045. // Check if the variable requires a player
  22046. if( not_server_variable(*name) && sd == NULL ){
  22047. // If no player is attached
  22048. if( !script_rid2sd(sd) ){
  22049. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  22050. return false;
  22051. }
  22052. }
  22053. // Try to find the array's source pointer
  22054. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  22055. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  22056. return false;
  22057. }
  22058. // Store the name for later usage
  22059. *out_name = name;
  22060. // Get the start and end indices of the array
  22061. *start = reference_getindex(data);
  22062. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  22063. // For getting the values we need the id of the array
  22064. *id = reference_getid(data);
  22065. return true;
  22066. }
  22067. // 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>}}}}}}}}};
  22068. BUILDIN_FUNC(mail){
  22069. const char *sender, *title, *body, *name;
  22070. struct mail_message msg;
  22071. struct script_data *data;
  22072. map_session_data *sd = NULL;
  22073. unsigned int i, j, k, num_items, start, end;
  22074. int32 id;
  22075. memset(&msg, 0, sizeof(struct mail_message));
  22076. msg.dest_id = script_getnum(st,2);
  22077. sender = script_getstr(st, 3);
  22078. if( strlen(sender) > NAME_LENGTH ){
  22079. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  22080. return SCRIPT_CMD_FAILURE;
  22081. }
  22082. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  22083. title = script_getstr(st, 4);
  22084. if( strlen(title) > MAIL_TITLE_LENGTH ){
  22085. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  22086. return SCRIPT_CMD_FAILURE;
  22087. }
  22088. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  22089. body = script_getstr(st, 5);
  22090. if( strlen(body) > MAIL_BODY_LENGTH ){
  22091. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  22092. return SCRIPT_CMD_FAILURE;
  22093. }
  22094. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  22095. if( script_hasdata(st,6) ){
  22096. int zeny = script_getnum(st, 6);
  22097. if( zeny < 0 ){
  22098. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  22099. return SCRIPT_CMD_FAILURE;
  22100. }else if( zeny > MAX_ZENY ){
  22101. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  22102. zeny = MAX_ZENY;
  22103. }
  22104. msg.zeny = zeny;
  22105. }
  22106. // Items
  22107. num_items = 0;
  22108. while( script_hasdata(st,7) ){
  22109. data = script_getdata(st,7);
  22110. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  22111. return SCRIPT_CMD_FAILURE;
  22112. }
  22113. num_items = end - start;
  22114. if( num_items == 0 ){
  22115. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  22116. break;
  22117. }
  22118. if( num_items > MAIL_MAX_ITEM ){
  22119. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  22120. num_items = MAIL_MAX_ITEM;
  22121. }
  22122. for( i = 0; i < num_items && start < end; i++, start++ ){
  22123. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22124. msg.item[i].identify = 1;
  22125. if( !item_db.exists(msg.item[i].nameid) ){
  22126. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  22127. return SCRIPT_CMD_FAILURE;
  22128. }
  22129. }
  22130. // Amounts
  22131. if( !script_hasdata(st,8) ){
  22132. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  22133. return SCRIPT_CMD_FAILURE;
  22134. }
  22135. data = script_getdata(st,8);
  22136. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  22137. return SCRIPT_CMD_FAILURE;
  22138. }
  22139. for( i = 0; i < num_items && start < end; i++, start++ ){
  22140. std::shared_ptr<item_data> itm = item_db.find(msg.item[i].nameid);
  22141. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22142. if( msg.item[i].amount <= 0 ){
  22143. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  22144. return SCRIPT_CMD_FAILURE;
  22145. }else if( itemdb_isstackable2(itm.get()) ){
  22146. uint16 max = itm->stack.amount > 0 ? itm->stack.amount : MAX_AMOUNT;
  22147. if( msg.item[i].amount > max ){
  22148. 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 );
  22149. msg.item[i].amount = max;
  22150. }
  22151. }else{
  22152. if( msg.item[i].amount > 1 ){
  22153. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  22154. msg.item[i].amount = 1;
  22155. }
  22156. }
  22157. }
  22158. // Refine
  22159. if (!script_hasdata(st, 9)) {
  22160. break;
  22161. }
  22162. data = script_getdata(st, 9);
  22163. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  22164. return SCRIPT_CMD_FAILURE;
  22165. }
  22166. for (i = 0; i < num_items && start < end; i++, start++) {
  22167. std::shared_ptr<item_data> itm = item_db.find(msg.item[i].nameid);
  22168. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22169. if (!itm->flag.no_refine && (itm->type == IT_WEAPON || itm->type == IT_ARMOR || itm->type == IT_SHADOWGEAR)) {
  22170. if (msg.item[i].refine > MAX_REFINE)
  22171. msg.item[i].refine = MAX_REFINE;
  22172. }
  22173. else
  22174. msg.item[i].refine = 0;
  22175. if (msg.item[i].refine < 0)
  22176. msg.item[i].refine = 0;
  22177. }
  22178. // Bound
  22179. if (!script_hasdata(st, 10)) {
  22180. break;
  22181. }
  22182. data = script_getdata(st,10);
  22183. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  22184. return SCRIPT_CMD_FAILURE;
  22185. }
  22186. for( i = 0; i < num_items && start < end; i++, start++ ){
  22187. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22188. if( msg.item[i].bound < BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  22189. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  22190. return SCRIPT_CMD_FAILURE;
  22191. }
  22192. }
  22193. // Cards
  22194. if( !script_hasdata(st,11) ){
  22195. break;
  22196. }
  22197. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  22198. data = script_getdata(st,j);
  22199. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22200. return SCRIPT_CMD_FAILURE;
  22201. }
  22202. for( k = 0; k < num_items && start < end; k++, start++ ){
  22203. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22204. if( msg.item[k].card[i] == 0 ){
  22205. // Continue with the next card, no further checks needed
  22206. continue;
  22207. }
  22208. if( itemdb_isspecial( msg.item[k].card[0] ) ){
  22209. // Continue with the next card, but do not check it against the item database
  22210. continue;
  22211. }
  22212. if( !item_db.exists( msg.item[k].card[i] ) ){
  22213. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  22214. return SCRIPT_CMD_FAILURE;
  22215. }
  22216. }
  22217. }
  22218. // Random Options
  22219. if( !script_hasdata(st,11 + MAX_SLOTS) ){
  22220. break;
  22221. }
  22222. 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++ ){
  22223. // Option IDs
  22224. data = script_getdata(st, j);
  22225. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22226. return SCRIPT_CMD_FAILURE;
  22227. }
  22228. for( k = 0; k < num_items && start < end; k++, start++ ){
  22229. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22230. }
  22231. j++;
  22232. // Option values
  22233. data = script_getdata(st, j);
  22234. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22235. return SCRIPT_CMD_FAILURE;
  22236. }
  22237. for( k = 0; k < num_items && start < end; k++, start++ ){
  22238. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22239. }
  22240. j++;
  22241. // Option parameters
  22242. data = script_getdata(st, j);
  22243. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22244. return SCRIPT_CMD_FAILURE;
  22245. }
  22246. for( k = 0; k < num_items && start < end; k++, start++ ){
  22247. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22248. }
  22249. }
  22250. // Break the pseudo scope
  22251. break;
  22252. }
  22253. msg.status = MAIL_NEW;
  22254. msg.type = MAIL_INBOX_NORMAL;
  22255. msg.timestamp = time(NULL);
  22256. intif_Mail_send(0, &msg);
  22257. return SCRIPT_CMD_SUCCESS;
  22258. }
  22259. BUILDIN_FUNC(open_roulette){
  22260. #if PACKETVER < 20141022
  22261. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  22262. return SCRIPT_CMD_FAILURE;
  22263. #else
  22264. map_session_data* sd;
  22265. if( !battle_config.feature_roulette ){
  22266. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  22267. return SCRIPT_CMD_FAILURE;
  22268. }
  22269. if( !script_charid2sd( 2, sd ) ){
  22270. return SCRIPT_CMD_FAILURE;
  22271. }
  22272. clif_roulette_open(sd);
  22273. return SCRIPT_CMD_SUCCESS;
  22274. #endif
  22275. }
  22276. /*==========================================
  22277. * identifyall({<type>{,<account_id>}})
  22278. * <type>:
  22279. * true: identify the items and returns the count of unidentified items (default)
  22280. * false: returns the count of unidentified items only
  22281. *------------------------------------------*/
  22282. BUILDIN_FUNC(identifyall) {
  22283. TBL_PC *sd;
  22284. bool identify_item = true;
  22285. if (script_hasdata(st, 2))
  22286. identify_item = script_getnum(st, 2) != 0;
  22287. if( !script_hasdata(st, 3) )
  22288. script_rid2sd(sd);
  22289. else
  22290. sd = map_id2sd( script_getnum(st, 3) );
  22291. if (sd == NULL) {
  22292. script_pushint(st, -1);
  22293. return SCRIPT_CMD_SUCCESS;
  22294. }
  22295. script_pushint(st, pc_identifyall(sd, identify_item));
  22296. return SCRIPT_CMD_SUCCESS;
  22297. }
  22298. BUILDIN_FUNC(is_guild_leader)
  22299. {
  22300. map_session_data* sd;
  22301. int guild_id;
  22302. if (!script_rid2sd(sd)) {
  22303. script_pushint(st, false);
  22304. return SCRIPT_CMD_FAILURE;
  22305. }
  22306. if (script_hasdata(st, 2))
  22307. guild_id = script_getnum(st, 2);
  22308. else
  22309. guild_id = sd->status.guild_id;
  22310. auto guild_data = guild_search(guild_id);
  22311. if (guild_data)
  22312. script_pushint(st, (guild_data->guild.member[0].char_id == sd->status.char_id));
  22313. else
  22314. script_pushint(st, false);
  22315. return SCRIPT_CMD_SUCCESS;
  22316. }
  22317. BUILDIN_FUNC(is_party_leader)
  22318. {
  22319. map_session_data* sd;
  22320. struct party_data* p_data;
  22321. int p_id, i = 0;
  22322. if (!script_rid2sd(sd)) {
  22323. script_pushint(st, false);
  22324. return SCRIPT_CMD_FAILURE;
  22325. }
  22326. if (script_hasdata(st, 2))
  22327. p_id = script_getnum(st, 2);
  22328. else
  22329. p_id = sd->status.party_id;
  22330. p_data = party_search(p_id);
  22331. if (p_data) {
  22332. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  22333. if (i < MAX_PARTY){
  22334. script_pushint(st, p_data->party.member[i].leader);
  22335. return SCRIPT_CMD_SUCCESS;
  22336. }
  22337. }
  22338. script_pushint(st, false);
  22339. return SCRIPT_CMD_SUCCESS;
  22340. }
  22341. BUILDIN_FUNC( camerainfo ){
  22342. #if PACKETVER < 20160525
  22343. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  22344. return SCRIPT_CMD_FAILURE;
  22345. #else
  22346. map_session_data* sd;
  22347. if( !script_charid2sd( 5, sd ) ){
  22348. return SCRIPT_CMD_FAILURE;
  22349. }
  22350. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  22351. return SCRIPT_CMD_SUCCESS;
  22352. #endif
  22353. }
  22354. // This function is only meant to be used inside of achievement conditions
  22355. BUILDIN_FUNC(achievement_condition){
  22356. // Push what we get from the script
  22357. script_pushint( st, 2 );
  22358. // Otherwise the script is freed afterwards
  22359. st->state = RERUNLINE;
  22360. return SCRIPT_CMD_SUCCESS;
  22361. }
  22362. /// Returns a reference to a variable of the specific instance ID.
  22363. /// Returns 0 if an error occurs.
  22364. ///
  22365. /// getinstancevar(<variable>, <instance ID>) -> <reference>
  22366. BUILDIN_FUNC(getinstancevar)
  22367. {
  22368. struct script_data* data = script_getdata(st, 2);
  22369. if (!data_isreference(data)) {
  22370. ShowError("buildin_getinstancevar: %s is not a variable.\n", script_getstr(st, 2));
  22371. script_reportdata(data);
  22372. script_pushnil(st);
  22373. st->state = END;
  22374. return SCRIPT_CMD_FAILURE;
  22375. }
  22376. const char* name = reference_getname(data);
  22377. if (*name != '\'') {
  22378. ShowError("buildin_getinstancevar: Invalid scope. %s is not an instance variable.\n", name);
  22379. script_reportdata(data);
  22380. script_pushnil(st);
  22381. st->state = END;
  22382. return SCRIPT_CMD_FAILURE;
  22383. }
  22384. int instance_id = script_getnum(st, 3);
  22385. if (instance_id <= 0) {
  22386. ShowError("buildin_getinstancevar: Invalid instance ID %d.\n", instance_id);
  22387. script_pushnil(st);
  22388. st->state = END;
  22389. return SCRIPT_CMD_FAILURE;
  22390. }
  22391. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  22392. if (im->state != INSTANCE_BUSY) {
  22393. ShowError("buildin_getinstancevar: Unknown instance ID %d.\n", instance_id);
  22394. script_pushnil(st);
  22395. st->state = END;
  22396. return SCRIPT_CMD_FAILURE;
  22397. }
  22398. if (!im->regs.vars)
  22399. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  22400. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  22401. return SCRIPT_CMD_SUCCESS;
  22402. }
  22403. /// Sets the value of an instance variable.
  22404. ///
  22405. /// setinstancevar(<variable>,<value>,<instance ID>)
  22406. BUILDIN_FUNC(setinstancevar)
  22407. {
  22408. const char *command = script_getfuncname(st);
  22409. struct script_data* data = script_getdata(st, 2);
  22410. if (!data_isreference(data)) {
  22411. ShowError("buildin_%s: %s is not a variable.\n", command, script_getstr(st, 2));
  22412. script_reportdata(data);
  22413. script_pushnil(st);
  22414. st->state = END;
  22415. return SCRIPT_CMD_FAILURE;
  22416. }
  22417. const char* name = reference_getname(data);
  22418. if (*name != '\'') {
  22419. ShowError("buildin_%s: Invalid scope. %s is not an instance variable.\n", command, name);
  22420. script_reportdata(data);
  22421. script_pushnil(st);
  22422. st->state = END;
  22423. return SCRIPT_CMD_FAILURE;
  22424. }
  22425. int instance_id = script_getnum(st, 4);
  22426. if (instance_id <= 0) {
  22427. ShowError("buildin_%s: Invalid instance ID %d.\n", command, instance_id);
  22428. script_pushnil(st);
  22429. st->state = END;
  22430. return SCRIPT_CMD_FAILURE;
  22431. }
  22432. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  22433. if (im->state != INSTANCE_BUSY) {
  22434. ShowError("buildin_%s: Unknown instance ID %d.\n", command, instance_id);
  22435. script_pushnil(st);
  22436. st->state = END;
  22437. return SCRIPT_CMD_FAILURE;
  22438. }
  22439. script_pushcopy(st, 2);
  22440. map_session_data* sd = nullptr;
  22441. if( is_string_variable(name) )
  22442. set_reg_str( st, sd, reference_getuid(data), name, script_getstr( st, 3 ), &im->regs );
  22443. else
  22444. set_reg_num( st, sd, reference_getuid(data), name, script_getnum64( st, 3 ), &im->regs );
  22445. return SCRIPT_CMD_SUCCESS;
  22446. }
  22447. /*
  22448. convertpcinfo(<char_id>,<type>)
  22449. convertpcinfo(<account_id>,<type>)
  22450. convertpcinfo(<player_name>,<type>)
  22451. */
  22452. BUILDIN_FUNC(convertpcinfo) {
  22453. TBL_PC *sd;
  22454. if (script_isstring(st, 2))
  22455. sd = map_nick2sd(script_getstr(st, 2),false);
  22456. else {
  22457. int id = script_getnum(st, 2);
  22458. sd = map_id2sd(id);
  22459. if (!sd)
  22460. sd = map_charid2sd(id);
  22461. }
  22462. int type = script_getnum(st, 3);
  22463. switch (type) {
  22464. case CPC_NAME:
  22465. case CPC_CHAR:
  22466. case CPC_ACCOUNT:
  22467. break;
  22468. default:
  22469. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  22470. script_pushnil(st);
  22471. st->state = END;
  22472. return SCRIPT_CMD_FAILURE;
  22473. }
  22474. if (!sd) {
  22475. if (type == CPC_NAME)
  22476. script_pushstrcopy(st, "");
  22477. else
  22478. script_pushint(st, 0);
  22479. return SCRIPT_CMD_SUCCESS;
  22480. }
  22481. switch (type) {
  22482. case CPC_NAME:
  22483. script_pushstrcopy(st, sd->status.name);
  22484. break;
  22485. case CPC_CHAR:
  22486. script_pushint(st, sd->status.char_id);
  22487. break;
  22488. case CPC_ACCOUNT:
  22489. script_pushint(st, sd->status.account_id);
  22490. break;
  22491. }
  22492. return SCRIPT_CMD_SUCCESS;
  22493. }
  22494. BUILDIN_FUNC(isnpccloaked)
  22495. {
  22496. npc_data *nd;
  22497. if (script_hasdata(st, 2))
  22498. nd = npc_name2id(script_getstr(st, 2));
  22499. else
  22500. nd = map_id2nd(st->oid);
  22501. if (!nd) {
  22502. if (script_hasdata(st, 2))
  22503. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  22504. else
  22505. ShowError("buildin_isnpccloaked: non-existing NPC.\n");
  22506. return SCRIPT_CMD_FAILURE;
  22507. }
  22508. map_session_data* sd;
  22509. if (!script_charid2sd(3, sd))
  22510. return SCRIPT_CMD_FAILURE;
  22511. script_pushint(st, npc_is_cloaked(nd, sd));
  22512. return SCRIPT_CMD_SUCCESS;
  22513. }
  22514. BUILDIN_FUNC(refineui){
  22515. #if PACKETVER < 20161012
  22516. ShowError( "buildin_refineui: This command requires packet version 2016-10-12 or newer.\n" );
  22517. return SCRIPT_CMD_FAILURE;
  22518. #else
  22519. map_session_data* sd;
  22520. if( !script_charid2sd(2,sd) ){
  22521. return SCRIPT_CMD_FAILURE;
  22522. }
  22523. if( !battle_config.feature_refineui ){
  22524. ShowError( "buildin_refineui: This command is disabled via configuration.\n" );
  22525. return SCRIPT_CMD_FAILURE;
  22526. }
  22527. if( !sd->state.refineui_open ){
  22528. clif_refineui_open(sd);
  22529. }
  22530. return SCRIPT_CMD_SUCCESS;
  22531. #endif
  22532. }
  22533. BUILDIN_FUNC(getenchantgrade){
  22534. map_session_data *sd;
  22535. if (!script_charid2sd(3, sd)) {
  22536. script_pushint(st,-1);
  22537. return SCRIPT_CMD_FAILURE;
  22538. }
  22539. int index, position;
  22540. if (script_hasdata(st, 2))
  22541. position = script_getnum(st, 2);
  22542. else
  22543. position = EQI_COMPOUND_ON;
  22544. if (position == EQI_COMPOUND_ON)
  22545. index = current_equip_item_index;
  22546. else if (equip_index_check(position))
  22547. index = pc_checkequip(sd, equip_bitmask[position]);
  22548. else {
  22549. ShowError( "buildin_getenchantgrade: Unknown equip index '%d'\n", position );
  22550. script_pushint(st,-1);
  22551. return SCRIPT_CMD_FAILURE;
  22552. }
  22553. if (index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0)
  22554. script_pushint(st, -1);
  22555. else
  22556. script_pushint(st, sd->inventory.u.items_inventory[index].enchantgrade);
  22557. return SCRIPT_CMD_SUCCESS;
  22558. }
  22559. BUILDIN_FUNC(naviregisterwarp) {
  22560. #ifdef MAP_GENERATOR
  22561. TBL_NPC* nd;
  22562. int x, y, m;
  22563. const char *warpname, *mapname=NULL;
  22564. struct navi_link link;
  22565. nd = map_id2nd(st->oid);
  22566. if (!nd) {
  22567. return SCRIPT_CMD_SUCCESS;
  22568. }
  22569. warpname = script_getstr(st, 2);
  22570. mapname = script_getstr(st, 3);
  22571. x = script_getnum(st,4);
  22572. y = script_getnum(st,5);
  22573. m = map_mapname2mapid(mapname);
  22574. link.npc = nd;
  22575. link.id = 0;
  22576. link.pos = nd->navi.pos;
  22577. link.warp_dest = {m, x, y};
  22578. link.name = warpname;
  22579. link.hidden = nd->navi.hidden;
  22580. nd->links.push_back(link);
  22581. #endif
  22582. return SCRIPT_CMD_SUCCESS;
  22583. }
  22584. /*==========================================
  22585. * mob_setidleevent( <monster game ID>, "<event label>" )
  22586. *------------------------------------------*/
  22587. BUILDIN_FUNC(mob_setidleevent){
  22588. struct block_list* bl;
  22589. if( !script_rid2bl( 2, bl ) ){
  22590. return SCRIPT_CMD_FAILURE;
  22591. }
  22592. if( bl->type != BL_MOB ){
  22593. ShowError( "buildin_mob_setidleevent: the target GID was not a monster.\n" );
  22594. return SCRIPT_CMD_FAILURE;
  22595. }
  22596. struct mob_data* md = (struct mob_data*)bl;
  22597. if (md == nullptr)
  22598. return SCRIPT_CMD_FAILURE;
  22599. const char* idle_event = script_getstr( st, 3 );
  22600. check_event( st, idle_event );
  22601. safestrncpy( md->idle_event, idle_event, EVENT_NAME_LENGTH );
  22602. return SCRIPT_CMD_SUCCESS;
  22603. }
  22604. BUILDIN_FUNC( openstylist ){
  22605. #if PACKETVER >= 20151104
  22606. if( !battle_config.feature_stylist ){
  22607. ShowError( "buildin_openstylist: stylist is disabled.\n" );
  22608. return SCRIPT_CMD_FAILURE;
  22609. }
  22610. map_session_data* sd;
  22611. if( !script_charid2sd( 2, sd ) ){
  22612. return SCRIPT_CMD_FAILURE;
  22613. }
  22614. clif_ui_open( *sd, OUT_UI_STYLIST, 0 );
  22615. return SCRIPT_CMD_SUCCESS;
  22616. #else
  22617. ShowError( "buildin_openstylist: This command requires packet version 2015-11-04 or newer.\n" );
  22618. return SCRIPT_CMD_FAILURE;
  22619. #endif
  22620. }
  22621. BUILDIN_FUNC(navihide) {
  22622. #ifdef MAP_GENERATOR
  22623. TBL_NPC *nd;
  22624. nd = map_id2nd(st->oid);
  22625. if (!nd) {
  22626. return SCRIPT_CMD_SUCCESS;
  22627. }
  22628. nd->navi.hidden = true;
  22629. for (auto &link : nd->links) {
  22630. link.hidden = true;
  22631. }
  22632. #endif
  22633. return SCRIPT_CMD_SUCCESS;
  22634. }
  22635. BUILDIN_FUNC(getitempos) {
  22636. map_session_data* sd;
  22637. if ( !script_rid2sd(sd) ){
  22638. script_pushint(st, 0);
  22639. return SCRIPT_CMD_FAILURE;
  22640. }
  22641. if( current_equip_item_index == -1 ){
  22642. ShowError( "buildin_getitempos: Invalid usage detected. This command should only be used inside item scripts.\n" );
  22643. script_pushint(st, 0);
  22644. return SCRIPT_CMD_FAILURE;
  22645. }
  22646. script_pushint(st, sd->inventory.u.items_inventory[current_equip_item_index].equip);
  22647. return SCRIPT_CMD_SUCCESS;
  22648. }
  22649. BUILDIN_FUNC( laphine_synthesis ){
  22650. map_session_data* sd;
  22651. if( !script_rid2sd( sd ) ){
  22652. return SCRIPT_CMD_FAILURE;
  22653. }
  22654. t_itemid item_id;
  22655. if( script_hasdata( st, 2 ) ){
  22656. if( script_isstring( st, 2 ) ){
  22657. const char* item_name = script_getstr( st, 2 );
  22658. std::shared_ptr<item_data> item = item_db.searchname( item_name );
  22659. if( item == nullptr ){
  22660. ShowError("buildin_laphine_synthesis: Item \"%s\" does not exist.\n", item_name );
  22661. return SCRIPT_CMD_FAILURE;
  22662. }
  22663. item_id = item->nameid;
  22664. }else{
  22665. item_id = script_getnum( st, 2 );
  22666. if( !item_db.exists( item_id ) ){
  22667. ShowError( "buildin_laphine_synthesis: Item ID %u does not exist.\n", item_id );
  22668. return SCRIPT_CMD_FAILURE;
  22669. }
  22670. }
  22671. }else{
  22672. if( sd->itemid == 0 ){
  22673. ShowError( "buildin_laphine_synthesis: Called outside of an item script without item id.\n" );
  22674. return SCRIPT_CMD_FAILURE;
  22675. }
  22676. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22677. ShowError( "buildin_laphine_synthesis: Called from item %u, which is not a consumed delayed.\n", sd->itemid );
  22678. return SCRIPT_CMD_FAILURE;
  22679. }
  22680. item_id = sd->itemid;
  22681. }
  22682. if( sd->state.laphine_synthesis != 0 ){
  22683. 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 );
  22684. return SCRIPT_CMD_FAILURE;
  22685. }
  22686. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( item_id );
  22687. if( synthesis == nullptr ){
  22688. ShowError( "buildin_laphine_synthesis: %u is not a valid Laphine Synthesis item.\n", item_id );
  22689. return SCRIPT_CMD_FAILURE;
  22690. }
  22691. clif_laphine_synthesis_open( sd, synthesis );
  22692. return SCRIPT_CMD_SUCCESS;
  22693. }
  22694. BUILDIN_FUNC( laphine_upgrade ){
  22695. map_session_data* sd;
  22696. if( !script_rid2sd( sd ) ){
  22697. return SCRIPT_CMD_FAILURE;
  22698. }
  22699. if( sd->itemid == 0 ){
  22700. ShowError( "buildin_laphine_upgrade: Called outside of an item script without item id.\n" );
  22701. return SCRIPT_CMD_FAILURE;
  22702. }
  22703. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22704. ShowError( "buildin_laphine_upgrade: Called from item %u, which is not a consumed delayed.\n", sd->itemid );
  22705. return SCRIPT_CMD_FAILURE;
  22706. }
  22707. if( sd->state.laphine_upgrade != 0 ){
  22708. 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 );
  22709. return SCRIPT_CMD_FAILURE;
  22710. }
  22711. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->itemid );
  22712. if( upgrade == nullptr ){
  22713. ShowError( "buildin_laphine_upgrade: %u is not a valid Laphine Upgrade item.\n", sd->itemid );
  22714. return SCRIPT_CMD_FAILURE;
  22715. }
  22716. clif_laphine_upgrade_open( sd, upgrade );
  22717. return SCRIPT_CMD_SUCCESS;
  22718. }
  22719. BUILDIN_FUNC(randomoptgroup)
  22720. {
  22721. int id = script_getnum(st,2);
  22722. auto group = random_option_group.find(id);
  22723. if (group == nullptr) {
  22724. ShowError("buildin_randomoptgroup: Invalid random option group id (%d)!\n", id);
  22725. return SCRIPT_CMD_FAILURE;
  22726. }
  22727. struct item item_tmp = {};
  22728. group->apply( item_tmp );
  22729. for ( int i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  22730. setd_sub_num(st, nullptr, ".@opt_id", i, item_tmp.option[i].id, nullptr);
  22731. setd_sub_num(st, nullptr, ".@opt_value", i, item_tmp.option[i].value, nullptr);
  22732. setd_sub_num(st, nullptr, ".@opt_param", i, item_tmp.option[i].param, nullptr);
  22733. }
  22734. return SCRIPT_CMD_SUCCESS;
  22735. }
  22736. BUILDIN_FUNC( open_quest_ui ){
  22737. #if PACKETVER < 20151202
  22738. ShowError( "buildin_open_quest_ui: This command requires PACKETVER 20151202 or newer.\n" );
  22739. return SCRIPT_CMD_FAILURE;
  22740. #else
  22741. map_session_data* sd;
  22742. if (!script_charid2sd(3, sd))
  22743. return SCRIPT_CMD_FAILURE;
  22744. int quest_id = script_hasdata(st, 2) ? script_getnum(st, 2) : 0;
  22745. if( quest_id != 0 ){
  22746. int i;
  22747. ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
  22748. if (i == sd->avail_quests)
  22749. ShowWarning("buildin_open_quest_ui: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
  22750. }
  22751. clif_ui_open( *sd, OUT_UI_QUEST, quest_id );
  22752. return SCRIPT_CMD_SUCCESS;
  22753. #endif
  22754. }
  22755. BUILDIN_FUNC(openbank){
  22756. #if PACKETVER < 20151202
  22757. ShowError( "buildin_openbank: This command requires PACKETVER 20151202 or newer.\n" );
  22758. return SCRIPT_CMD_FAILURE;
  22759. #else
  22760. map_session_data* sd = nullptr;
  22761. if (!script_charid2sd(2, sd)) {
  22762. return SCRIPT_CMD_FAILURE;
  22763. }
  22764. if( !battle_config.feature_banking ){
  22765. ShowError( "buildin_openbank: banking is disabled.\n" );
  22766. return SCRIPT_CMD_FAILURE;
  22767. }
  22768. clif_ui_open( *sd, OUT_UI_BANK, 0 );
  22769. return SCRIPT_CMD_SUCCESS;
  22770. #endif
  22771. }
  22772. BUILDIN_FUNC(getbaseexp_ratio){
  22773. map_session_data *sd;
  22774. if( !script_charid2sd( 4, sd ) ){
  22775. return SCRIPT_CMD_FAILURE;
  22776. }
  22777. int32 percent = script_getnum( st, 2 );
  22778. if( percent < 0 || percent > 100 ){
  22779. ShowError( "getbaseexp_ratio: percentage is out of range.\n" );
  22780. return SCRIPT_CMD_FAILURE;
  22781. }
  22782. int32 base_level;
  22783. if( script_hasdata( st, 3 ) ){
  22784. base_level = script_getnum( st, 3 );
  22785. if( base_level < 1 ){
  22786. ShowError( "getbaseexp_ratio: level cannot be below 1.\n" );
  22787. return SCRIPT_CMD_FAILURE;
  22788. }
  22789. uint32 max_level = job_db.get_maxBaseLv( sd->status.class_ );
  22790. if( base_level > max_level ){
  22791. ShowError( "getbaseexp_ratio: level is above maximum base level %u for job %s.\n", max_level, job_name( sd->status.class_ ) );
  22792. return SCRIPT_CMD_FAILURE;
  22793. }
  22794. }else{
  22795. base_level = sd->status.base_level;
  22796. }
  22797. t_exp class_exp = job_db.get_baseExp( sd->status.class_, base_level );
  22798. if( class_exp <= 0 ){
  22799. ShowError( "getbaseexp_ratio: No base experience defined for class %s at base level %d.\n", job_name( sd->status.class_ ), base_level );
  22800. return SCRIPT_CMD_FAILURE;
  22801. }
  22802. double result = ( class_exp * percent ) / 100.0;
  22803. script_pushint64( st, static_cast<t_exp>( result ) );
  22804. return SCRIPT_CMD_SUCCESS;
  22805. }
  22806. BUILDIN_FUNC(getjobexp_ratio){
  22807. map_session_data *sd;
  22808. if( !script_charid2sd( 4, sd ) ){
  22809. return SCRIPT_CMD_FAILURE;
  22810. }
  22811. int32 percent = script_getnum( st, 2 );
  22812. if( percent < 0 || percent > 100 ){
  22813. ShowError( "getjobexp_ratio: percentage is out of range.\n" );
  22814. return SCRIPT_CMD_FAILURE;
  22815. }
  22816. int32 job_level;
  22817. if( script_hasdata( st, 3 ) ){
  22818. job_level = script_getnum( st, 3 );
  22819. if( job_level < 1 ){
  22820. ShowError( "getjobexp_ratio: level cannot be below 1.\n" );
  22821. return SCRIPT_CMD_FAILURE;
  22822. }
  22823. uint32 max_level = job_db.get_maxJobLv( sd->status.class_ );
  22824. if( job_level > max_level ){
  22825. ShowError( "getjobexp_ratio: level is above maximum job level %u for job %s.\n", max_level, job_name( sd->status.class_ ) );
  22826. return SCRIPT_CMD_FAILURE;
  22827. }
  22828. }else{
  22829. job_level = sd->status.job_level;
  22830. }
  22831. t_exp class_exp = job_db.get_baseExp( sd->status.class_, job_level );
  22832. if( class_exp <= 0 ){
  22833. ShowError( "getjobexp_ratio: No job experience defined for class %s at job level %d.\n", job_name( sd->status.class_ ), job_level );
  22834. return SCRIPT_CMD_FAILURE;
  22835. }
  22836. double result = ( class_exp * percent ) / 100.0;
  22837. script_pushint64( st, static_cast<t_exp>( result ) );
  22838. return SCRIPT_CMD_SUCCESS;
  22839. }
  22840. BUILDIN_FUNC( enchantgradeui ){
  22841. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  22842. map_session_data* sd;
  22843. if( !script_charid2sd( 2, sd ) ){
  22844. return SCRIPT_CMD_FAILURE;
  22845. }
  22846. clif_ui_open( *sd, OUT_UI_ENCHANTGRADE, 0 );
  22847. return SCRIPT_CMD_SUCCESS;
  22848. #else
  22849. ShowError( "buildin_enchantgradeui: This command requires PACKETVER 2020-07-24 or newer.\n" );
  22850. return SCRIPT_CMD_FAILURE;
  22851. #endif
  22852. }
  22853. BUILDIN_FUNC(set_reputation_points){
  22854. map_session_data* sd;
  22855. if( !script_charid2sd( 4, sd ) ){
  22856. return SCRIPT_CMD_FAILURE;
  22857. }
  22858. int64 type = script_getnum64( st, 2 );
  22859. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22860. if( reputation == nullptr ){
  22861. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22862. return SCRIPT_CMD_FAILURE;
  22863. }
  22864. int64 points = script_getnum64( st, 3 );
  22865. points = cap_value( points, reputation->minimum, reputation->maximum );
  22866. if( !pc_setreg2( sd, reputation->variable.c_str(), points ) ){
  22867. return SCRIPT_CMD_FAILURE;
  22868. }
  22869. clif_reputation_type( *sd, type, points );
  22870. return SCRIPT_CMD_SUCCESS;
  22871. }
  22872. BUILDIN_FUNC(get_reputation_points){
  22873. map_session_data* sd;
  22874. if( !script_charid2sd( 3, sd ) ){
  22875. return SCRIPT_CMD_FAILURE;
  22876. }
  22877. int64 type = script_getnum64( st, 2 );
  22878. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22879. if( reputation == nullptr ){
  22880. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22881. return SCRIPT_CMD_FAILURE;
  22882. }
  22883. int64 points = pc_readreg2( sd, reputation->variable.c_str() );
  22884. points = cap_value( points, reputation->minimum, reputation->maximum );
  22885. script_pushint( st, points );
  22886. return SCRIPT_CMD_SUCCESS;
  22887. }
  22888. BUILDIN_FUNC(add_reputation_points)
  22889. {
  22890. map_session_data* sd;
  22891. if( !script_charid2sd( 4, sd ) ){
  22892. return SCRIPT_CMD_FAILURE;
  22893. }
  22894. int64 type = script_getnum64( st, 2 );
  22895. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22896. if( reputation == nullptr ){
  22897. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22898. return SCRIPT_CMD_FAILURE;
  22899. }
  22900. int64 points = pc_readreg2( sd, reputation->variable.c_str() ) + script_getnum64(st, 3);
  22901. points = cap_value( points, reputation->minimum, reputation->maximum );
  22902. if( !pc_setreg2( sd, reputation->variable.c_str(), points ) ){
  22903. return SCRIPT_CMD_FAILURE;
  22904. }
  22905. clif_reputation_type( *sd, type, points );
  22906. return SCRIPT_CMD_SUCCESS;
  22907. }
  22908. BUILDIN_FUNC(item_reform){
  22909. #if PACKETVER < 20211103
  22910. ShowError( "buildin_item_reform: This command requires packet version 2021-11-03 or newer.\n" );
  22911. return SCRIPT_CMD_FAILURE;
  22912. #else
  22913. map_session_data* sd;
  22914. if( !script_charid2sd( 3, sd ) ){
  22915. return SCRIPT_CMD_FAILURE;
  22916. }
  22917. t_itemid item_id;
  22918. if( script_hasdata( st, 2 ) ){
  22919. if( script_isstring( st, 2 ) ){
  22920. const char* item_name = script_getstr( st, 2 );
  22921. std::shared_ptr<item_data> item = item_db.searchname( item_name );
  22922. if( item == nullptr ){
  22923. ShowError("buildin_item_reform: Item \"%s\" does not exist.\n", item_name );
  22924. return SCRIPT_CMD_FAILURE;
  22925. }
  22926. item_id = item->nameid;
  22927. }else{
  22928. item_id = script_getnum( st, 2 );
  22929. if( !item_db.exists( item_id ) ){
  22930. ShowError( "buildin_item_reform: Item ID %u does not exist.\n", item_id );
  22931. return SCRIPT_CMD_FAILURE;
  22932. }
  22933. }
  22934. }else{
  22935. if( sd->itemid == 0 ){
  22936. ShowError( "buildin_item_reform: Called outside of an item script without item id.\n" );
  22937. return SCRIPT_CMD_FAILURE;
  22938. }
  22939. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22940. ShowError( "buildin_item_reform: Called from item %u, which is not a DelayConsume type.\n", sd->itemid );
  22941. return SCRIPT_CMD_FAILURE;
  22942. }
  22943. item_id = sd->itemid;
  22944. }
  22945. if( !item_reform_db.exists( item_id ) ){
  22946. ShowError( "buildin_item_reform: Item ID %u is not in the Item Reform database.\n", item_id );
  22947. return SCRIPT_CMD_FAILURE;
  22948. }
  22949. clif_item_reform_open( *sd, item_id );
  22950. return SCRIPT_CMD_SUCCESS;
  22951. #endif
  22952. }
  22953. BUILDIN_FUNC(item_enchant){
  22954. #if !( PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330 )
  22955. ShowError( "buildin_item_enchant: This command requires packet version 2021-11-03 or newer.\n" );
  22956. return SCRIPT_CMD_FAILURE;
  22957. #else
  22958. map_session_data* sd;
  22959. if( !script_charid2sd( 3, sd ) ){
  22960. return SCRIPT_CMD_FAILURE;
  22961. }
  22962. uint64 clientLuaIndex = script_getnum64( st, 2 );
  22963. if( !item_enchant_db.exists( clientLuaIndex ) ){
  22964. ShowError( "buildin_item_enchant: %" PRIu64 " is not a valid item enchant index.\n", clientLuaIndex );
  22965. return SCRIPT_CMD_FAILURE;
  22966. }
  22967. clif_enchantwindow_open( *sd, clientLuaIndex );
  22968. return SCRIPT_CMD_SUCCESS;
  22969. #endif
  22970. }
  22971. /**
  22972. * Generate item link string for client
  22973. * itemlink(<item_id>,<refine>,<card0>,<card1>,<card2>,<card3>,<enchantgrade>{,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>});
  22974. * @author [Cydh]
  22975. **/
  22976. BUILDIN_FUNC(itemlink)
  22977. {
  22978. struct item item = {};
  22979. item.nameid = script_getnum(st, 2);
  22980. if( !item_db.exists( item.nameid ) ){
  22981. ShowError( "buildin_itemlink: Item ID %u does not exists.\n", item.nameid );
  22982. st->state = END;
  22983. return SCRIPT_CMD_FAILURE;
  22984. }
  22985. FETCH(3, item.refine);
  22986. FETCH(4, item.card[0]);
  22987. FETCH(5, item.card[1]);
  22988. FETCH(6, item.card[2]);
  22989. FETCH(7, item.card[3]);
  22990. FETCH(8, item.enchantgrade);
  22991. #if PACKETVER >= 20150225
  22992. if ( script_hasdata(st,9) && script_getitem_randomoption(st, nullptr, &item, "itemlink", 9) == false) {
  22993. st->state = END;
  22994. return SCRIPT_CMD_FAILURE;
  22995. }
  22996. #endif
  22997. std::string itemlstr = item_db.create_item_link(item);
  22998. script_pushstrcopy(st, itemlstr.c_str());
  22999. return SCRIPT_CMD_SUCCESS;
  23000. }
  23001. BUILDIN_FUNC(mesitemlink){
  23002. t_itemid nameid = script_getnum( st, 2 );
  23003. std::shared_ptr<item_data> data = item_db.find( nameid );
  23004. if( data == nullptr ){
  23005. ShowError( "buildin_mesitemlink: Item ID %u does not exists.\n", nameid );
  23006. script_pushconststr( st, "" );
  23007. return SCRIPT_CMD_FAILURE;
  23008. }
  23009. bool use_brackets = true;
  23010. if( script_hasdata( st, 3 ) ){
  23011. use_brackets = script_getnum( st, 3 ) != 0;
  23012. }
  23013. const char* name = nullptr;
  23014. if( script_hasdata( st, 4 ) ){
  23015. name = script_getstr( st, 4 );
  23016. }
  23017. // Create the link, depending on configuration and packet version
  23018. std::string itemlstr = item_db.create_item_link_for_mes( data, use_brackets, name );
  23019. // Push it to the script engine for further usage
  23020. script_pushstrcopy( st, itemlstr.c_str() );
  23021. return SCRIPT_CMD_SUCCESS;
  23022. }
  23023. BUILDIN_FUNC(addfame) {
  23024. map_session_data *sd;
  23025. if (!script_charid2sd(3, sd))
  23026. return SCRIPT_CMD_FAILURE;
  23027. if (!pc_addfame(*sd, script_getnum(st, 2)))
  23028. return SCRIPT_CMD_FAILURE;
  23029. return SCRIPT_CMD_SUCCESS;
  23030. }
  23031. BUILDIN_FUNC(getfame) {
  23032. map_session_data *sd;
  23033. if (!script_charid2sd(2, sd))
  23034. return SCRIPT_CMD_FAILURE;
  23035. script_pushint(st, sd->status.fame);
  23036. return SCRIPT_CMD_SUCCESS;
  23037. }
  23038. BUILDIN_FUNC(getfamerank) {
  23039. map_session_data *sd;
  23040. if (!script_charid2sd(2, sd))
  23041. return SCRIPT_CMD_FAILURE;
  23042. script_pushint(st, pc_famerank(sd->status.char_id, sd->class_ & MAPID_UPPERMASK));
  23043. return SCRIPT_CMD_SUCCESS;
  23044. }
  23045. BUILDIN_FUNC(isdead) {
  23046. map_session_data *sd;
  23047. if (!script_mapid2sd(2, sd))
  23048. return SCRIPT_CMD_FAILURE;
  23049. script_pushint(st, pc_isdead(sd));
  23050. return SCRIPT_CMD_SUCCESS;
  23051. }
  23052. BUILDIN_FUNC(macro_detector) {
  23053. map_session_data *sd;
  23054. if (script_hasdata(st, 2) && script_isstring(st, 2)) { // Character Name
  23055. if (!script_nick2sd(2, sd)) {
  23056. return SCRIPT_CMD_FAILURE;
  23057. }
  23058. } else { // Account ID
  23059. if (!script_accid2sd(2, sd)) {
  23060. return SCRIPT_CMD_FAILURE;
  23061. }
  23062. }
  23063. // Reporter Account ID as -1 for server.
  23064. pc_macro_reporter_process(*sd);
  23065. return SCRIPT_CMD_SUCCESS;
  23066. }
  23067. // ===================================
  23068. // *has_autoloot({<char_id>});
  23069. // This command checks whether a player configured autoloot.
  23070. // Returns current autoloot value on success.
  23071. // ===================================
  23072. BUILDIN_FUNC(has_autoloot) {
  23073. map_session_data *sd;
  23074. if (!script_charid2sd(2, sd)) {
  23075. script_pushint(st, 0);
  23076. return SCRIPT_CMD_FAILURE;
  23077. }
  23078. script_pushint(st, sd->state.autoloot);
  23079. return SCRIPT_CMD_SUCCESS;
  23080. }
  23081. // ===================================
  23082. // *autoloot({<rate>{, <char_id>}});
  23083. // This command sets the rate of autoloot.
  23084. // If no rate is provided and the user has autoloot disabled it will default to 10000 = 100% (enabled) or
  23085. // if the user has autoloot enabled it will default to 0 = 0% (disabled).
  23086. // Returns true on success and false on failure.
  23087. // ===================================
  23088. BUILDIN_FUNC(autoloot) {
  23089. map_session_data *sd = nullptr;
  23090. if (!script_charid2sd(3, sd)) {
  23091. script_pushint(st, false);
  23092. return SCRIPT_CMD_FAILURE;
  23093. }
  23094. int rate;
  23095. if (script_hasdata(st, 2)) {
  23096. rate = script_getnum(st, 2);
  23097. if (rate < 0 || rate > 10000) {
  23098. ShowWarning("buildin_autoloot: Invalid rate value %d, should be between 0 ~ 10000.\n", rate);
  23099. script_pushint(st, false);
  23100. return SCRIPT_CMD_FAILURE;
  23101. }
  23102. } else {
  23103. rate = (sd->state.autoloot > 0 ? 0 : 10000);
  23104. }
  23105. sd->state.autoloot = rate;
  23106. script_pushint(st, true);
  23107. return SCRIPT_CMD_SUCCESS;
  23108. }
  23109. BUILDIN_FUNC(opentips){
  23110. #if PACKETVER < 20171122
  23111. ShowError( "buildin_opentips: This command requires PACKETVER 20171122 or newer.\n" );
  23112. return SCRIPT_CMD_FAILURE;
  23113. #else
  23114. map_session_data* sd;
  23115. if (!script_charid2sd(3, sd)) {
  23116. return SCRIPT_CMD_FAILURE;
  23117. }
  23118. clif_ui_open( *sd, OUT_UI_TIP, script_getnum(st, 2) );
  23119. return SCRIPT_CMD_SUCCESS;
  23120. #endif
  23121. }
  23122. #include <custom/script.inc>
  23123. // declarations that were supposed to be exported from npc_chat.cpp
  23124. #ifdef PCRE_SUPPORT
  23125. BUILDIN_FUNC(defpattern);
  23126. BUILDIN_FUNC(activatepset);
  23127. BUILDIN_FUNC(deactivatepset);
  23128. BUILDIN_FUNC(deletepset);
  23129. #endif
  23130. /** Regular expression matching
  23131. * preg_match(<pattern>,<string>{,<offset>})
  23132. */
  23133. BUILDIN_FUNC(preg_match) {
  23134. #ifdef PCRE_SUPPORT
  23135. pcre *re;
  23136. pcre_extra *pcreExtra;
  23137. const char *error;
  23138. int erroffset, r, offset = 0;
  23139. int subStrVec[30];
  23140. const char* pattern = script_getstr(st,2);
  23141. const char* subject = script_getstr(st,3);
  23142. if (script_hasdata(st,4))
  23143. offset = script_getnum(st,4);
  23144. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  23145. pcreExtra = pcre_study(re, 0, &error);
  23146. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  23147. pcre_free(re);
  23148. if (pcreExtra != NULL)
  23149. pcre_free(pcreExtra);
  23150. if (r < 0)
  23151. script_pushint(st,0);
  23152. else
  23153. script_pushint(st,(r > 0) ? r : 30 / 3);
  23154. return SCRIPT_CMD_SUCCESS;
  23155. #else
  23156. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  23157. script_pushint(st,0);
  23158. return SCRIPT_CMD_SUCCESS;
  23159. #endif
  23160. }
  23161. /// script command definitions
  23162. /// for an explanation on args, see add_buildin_func
  23163. struct script_function buildin_func[] = {
  23164. // NPC interaction
  23165. BUILDIN_DEF(mes,"s*"),
  23166. BUILDIN_DEF(next,""),
  23167. BUILDIN_DEF(clear,""),
  23168. BUILDIN_DEF(close,""),
  23169. BUILDIN_DEF(close2,""),
  23170. BUILDIN_DEF2(close, "close3", ""),
  23171. BUILDIN_DEF(menu,"sl*"),
  23172. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  23173. BUILDIN_DEF(prompt,"s*"),
  23174. //
  23175. BUILDIN_DEF(goto,"l"),
  23176. BUILDIN_DEF(callsub,"l*"),
  23177. BUILDIN_DEF(callfunc,"s*"),
  23178. BUILDIN_DEF(return,"?"),
  23179. BUILDIN_DEF(getarg,"i?"),
  23180. BUILDIN_DEF(jobchange,"i??"),
  23181. BUILDIN_DEF(jobname,"i"),
  23182. BUILDIN_DEF(input,"r??"),
  23183. BUILDIN_DEF(warp,"sii?"),
  23184. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  23185. BUILDIN_DEF(areawarp,"siiiisii??"),
  23186. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  23187. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  23188. BUILDIN_DEF(setlook,"ii?"),
  23189. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  23190. BUILDIN_DEF2(setr,"set","rv?"),
  23191. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  23192. BUILDIN_DEF(setarray,"rv*"),
  23193. BUILDIN_DEF(cleararray,"rvi"),
  23194. BUILDIN_DEF(copyarray,"rri"),
  23195. BUILDIN_DEF(getarraysize,"r"),
  23196. BUILDIN_DEF(deletearray,"r?"),
  23197. BUILDIN_DEF(getelementofarray,"ri"),
  23198. BUILDIN_DEF(inarray,"rv"),
  23199. BUILDIN_DEF(countinarray,"rr"),
  23200. BUILDIN_DEF(getitem,"vi?"),
  23201. BUILDIN_DEF(rentitem,"vi?"),
  23202. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  23203. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  23204. BUILDIN_DEF(getnameditem,"vv"),
  23205. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  23206. BUILDIN_DEF(makeitem,"visii?"),
  23207. BUILDIN_DEF(makeitem2,"visiiiiiiiii?"),
  23208. BUILDIN_DEF(delitem,"vi?"),
  23209. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  23210. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  23211. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  23212. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  23213. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  23214. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  23215. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  23216. BUILDIN_DEF(delitemidx,"i??"),
  23217. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  23218. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  23219. BUILDIN_DEF(cutin,"si"),
  23220. BUILDIN_DEF(viewpoint,"iiiii?"),
  23221. BUILDIN_DEF(viewpointmap, "siiiii"),
  23222. BUILDIN_DEF(heal,"ii?"),
  23223. BUILDIN_DEF(healap,"i?"),
  23224. BUILDIN_DEF(itemheal,"ii?"),
  23225. BUILDIN_DEF(percentheal,"ii?"),
  23226. BUILDIN_DEF(rand,"i?"),
  23227. BUILDIN_DEF(countitem,"v?"),
  23228. BUILDIN_DEF(storagecountitem,"v?"),
  23229. BUILDIN_DEF(guildstoragecountitem,"v?"),
  23230. BUILDIN_DEF(cartcountitem,"v?"),
  23231. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  23232. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  23233. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  23234. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  23235. BUILDIN_DEF(checkweight,"vi*"),
  23236. BUILDIN_DEF(checkweight2,"rr"),
  23237. BUILDIN_DEF(readparam,"i?"),
  23238. BUILDIN_DEF(getcharid,"i?"),
  23239. BUILDIN_DEF(getnpcid,"i?"),
  23240. BUILDIN_DEF(getpartyname,"i"),
  23241. BUILDIN_DEF(getpartymember,"i??"),
  23242. BUILDIN_DEF(getpartyleader,"i?"),
  23243. BUILDIN_DEF(getguildname,"i"),
  23244. BUILDIN_DEF(getguildmaster,"i"),
  23245. BUILDIN_DEF(getguildmasterid,"i"),
  23246. BUILDIN_DEF(strcharinfo,"i?"),
  23247. BUILDIN_DEF(strnpcinfo,"i"),
  23248. BUILDIN_DEF(getequipid,"??"),
  23249. BUILDIN_DEF(getequipuniqueid,"i?"),
  23250. BUILDIN_DEF(getequipname,"i?"),
  23251. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  23252. BUILDIN_DEF(repair,"i?"), // [Valaris]
  23253. BUILDIN_DEF(repairall,"?"),
  23254. BUILDIN_DEF(getequipisequiped,"i?"),
  23255. BUILDIN_DEF(getequipisenableref,"i?"),
  23256. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  23257. BUILDIN_DEF(getequipweaponlv,"??"),
  23258. BUILDIN_DEF(getequiparmorlv, "??"),
  23259. BUILDIN_DEF(getequippercentrefinery,"i?"),
  23260. BUILDIN_DEF(successrefitem,"i??"),
  23261. BUILDIN_DEF(failedrefitem,"i?"),
  23262. BUILDIN_DEF(downrefitem,"i??"),
  23263. BUILDIN_DEF(statusup,"i?"),
  23264. BUILDIN_DEF(statusup2,"ii?"),
  23265. BUILDIN_DEF(traitstatusup,"i?"),
  23266. BUILDIN_DEF(traitstatusup2,"ii?"),
  23267. BUILDIN_DEF(bonus,"i?"),
  23268. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  23269. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  23270. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  23271. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  23272. BUILDIN_DEF(autobonus,"sii??"),
  23273. BUILDIN_DEF(autobonus2,"sii??"),
  23274. BUILDIN_DEF(autobonus3,"siiv?"),
  23275. BUILDIN_DEF(petautobonus,"sii??"),
  23276. BUILDIN_DEF2(petautobonus,"petautobonus2","sii??"),
  23277. BUILDIN_DEF(petautobonus3,"siiv?"),
  23278. BUILDIN_DEF(plagiarizeskill, "ii"),
  23279. BUILDIN_DEF(plagiarizeskillreset, "i"),
  23280. BUILDIN_DEF(skill,"vi?"),
  23281. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  23282. BUILDIN_DEF(guildskill,"vi"),
  23283. BUILDIN_DEF(getskilllv,"v"),
  23284. BUILDIN_DEF(getgdskilllv,"iv"),
  23285. BUILDIN_DEF(basicskillcheck,""),
  23286. BUILDIN_DEF(getgmlevel,"?"),
  23287. BUILDIN_DEF(getgroupid,"?"),
  23288. BUILDIN_DEF(end,""),
  23289. BUILDIN_DEF(checkoption,"i?"),
  23290. BUILDIN_DEF(setoption,"i??"),
  23291. BUILDIN_DEF(setcart,"??"),
  23292. BUILDIN_DEF(checkcart,"?"),
  23293. BUILDIN_DEF(setfalcon,"??"),
  23294. BUILDIN_DEF(checkfalcon,"?"),
  23295. BUILDIN_DEF(setriding,"??"),
  23296. BUILDIN_DEF(checkriding,"?"),
  23297. BUILDIN_DEF(checkwug,"?"),
  23298. BUILDIN_DEF(checkmadogear,"?"),
  23299. BUILDIN_DEF(setmadogear,"???"),
  23300. BUILDIN_DEF2(savepoint,"save","sii???"),
  23301. BUILDIN_DEF(savepoint,"sii???"),
  23302. BUILDIN_DEF(gettimetick,"i"),
  23303. BUILDIN_DEF(gettime,"i"),
  23304. BUILDIN_DEF(gettimestr,"si?"),
  23305. BUILDIN_DEF(openstorage,""),
  23306. BUILDIN_DEF(guildopenstorage,""),
  23307. BUILDIN_DEF(guildopenstorage_log,"?"),
  23308. BUILDIN_DEF(guild_has_permission,"i?"),
  23309. BUILDIN_DEF(itemskill,"vi?"),
  23310. BUILDIN_DEF(produce,"i"),
  23311. BUILDIN_DEF(cooking,"i"),
  23312. BUILDIN_DEF(monster,"siisvi???"),
  23313. BUILDIN_DEF(getmobdrops,"i"),
  23314. BUILDIN_DEF(areamonster,"siiiisvi???"),
  23315. BUILDIN_DEF(killmonster,"ss?"),
  23316. BUILDIN_DEF(killmonsterall,"s?"),
  23317. BUILDIN_DEF(clone,"siisi????"),
  23318. BUILDIN_DEF(doevent,"s"),
  23319. BUILDIN_DEF(donpcevent,"s"),
  23320. BUILDIN_DEF(cmdothernpc,"ss"),
  23321. BUILDIN_DEF(addtimer,"is"),
  23322. BUILDIN_DEF(deltimer,"s"),
  23323. BUILDIN_DEF(addtimercount,"is"),
  23324. BUILDIN_DEF(initnpctimer,"??"),
  23325. BUILDIN_DEF(stopnpctimer,"??"),
  23326. BUILDIN_DEF(startnpctimer,"??"),
  23327. BUILDIN_DEF(setnpctimer,"i?"),
  23328. BUILDIN_DEF(getnpctimer,"i?"),
  23329. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  23330. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  23331. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  23332. BUILDIN_DEF(announce,"si??????"),
  23333. BUILDIN_DEF(mapannounce,"ssi?????"),
  23334. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  23335. BUILDIN_DEF(getusers,"i"),
  23336. BUILDIN_DEF(getmapguildusers,"si"),
  23337. BUILDIN_DEF(getmapusers,"s"),
  23338. BUILDIN_DEF(getareausers,"siiii"),
  23339. BUILDIN_DEF(getunits, "i?"),
  23340. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  23341. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  23342. BUILDIN_DEF(getareadropitem,"siiiiv"),
  23343. BUILDIN_DEF(enablenpc,"?"),
  23344. BUILDIN_DEF2(enablenpc, "disablenpc", "?"),
  23345. BUILDIN_DEF2(enablenpc, "hideoffnpc", "?"),
  23346. BUILDIN_DEF2(enablenpc, "hideonnpc", "?"),
  23347. BUILDIN_DEF2(enablenpc, "cloakoffnpc", "??"),
  23348. BUILDIN_DEF2(enablenpc, "cloakonnpc", "??"),
  23349. BUILDIN_DEF2(enablenpc, "cloakoffnpcself", "?"),
  23350. BUILDIN_DEF2(enablenpc, "cloakonnpcself", "?"),
  23351. BUILDIN_DEF(sc_start,"iii???"),
  23352. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  23353. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  23354. BUILDIN_DEF(sc_end,"i?"),
  23355. BUILDIN_DEF(sc_end_class,"??"),
  23356. BUILDIN_DEF(getstatus, "i??"),
  23357. BUILDIN_DEF(getscrate,"ii?"),
  23358. BUILDIN_DEF(debugmes,"s"),
  23359. BUILDIN_DEF(errormes,"s"),
  23360. BUILDIN_DEF2(catchpet,"pet","i"),
  23361. BUILDIN_DEF2(birthpet,"bpet",""),
  23362. BUILDIN_DEF(catchpet,"i"),
  23363. BUILDIN_DEF(birthpet,""),
  23364. BUILDIN_DEF(resetlvl,"i?"),
  23365. BUILDIN_DEF(resetstatus,"?"),
  23366. BUILDIN_DEF(resetskill,"?"),
  23367. BUILDIN_DEF(resetfeel,"?"),
  23368. BUILDIN_DEF(resethate,"?"),
  23369. BUILDIN_DEF(skillpointcount,"?"),
  23370. BUILDIN_DEF(changebase,"i?"),
  23371. BUILDIN_DEF(changesex,"?"),
  23372. BUILDIN_DEF(changecharsex,"?"),
  23373. BUILDIN_DEF(waitingroom,"si?????"),
  23374. BUILDIN_DEF(delwaitingroom,"?"),
  23375. BUILDIN_DEF(waitingroomkick,"ss"),
  23376. BUILDIN_DEF(getwaitingroomusers, "?"),
  23377. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  23378. BUILDIN_DEF(enablewaitingroomevent,"?"),
  23379. BUILDIN_DEF(disablewaitingroomevent,"?"),
  23380. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  23381. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  23382. BUILDIN_DEF(getwaitingroomstate,"i?"),
  23383. BUILDIN_DEF(warpwaitingpc,"sii?"),
  23384. BUILDIN_DEF(attachrid,"i?"),
  23385. BUILDIN_DEF(addrid,"i?????"),
  23386. BUILDIN_DEF(detachrid,""),
  23387. BUILDIN_DEF(isloggedin,"i?"),
  23388. BUILDIN_DEF(setmapflagnosave,"ssii"),
  23389. BUILDIN_DEF(getmapflag,"si?"),
  23390. BUILDIN_DEF(setmapflag,"si??"),
  23391. BUILDIN_DEF(removemapflag,"si?"),
  23392. BUILDIN_DEF(pvpon,"s"),
  23393. BUILDIN_DEF(pvpoff,"s"),
  23394. BUILDIN_DEF(gvgon,"s"),
  23395. BUILDIN_DEF(gvgoff,"s"),
  23396. BUILDIN_DEF(emotion,"i?"),
  23397. BUILDIN_DEF(maprespawnguildid,"sii"),
  23398. BUILDIN_DEF(agitstart,""), // <Agit>
  23399. BUILDIN_DEF(agitend,""),
  23400. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  23401. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  23402. BUILDIN_DEF(getcastlename,"s"),
  23403. BUILDIN_DEF(getcastledata,"si"),
  23404. BUILDIN_DEF(setcastledata,"sii"),
  23405. BUILDIN_DEF(requestguildinfo,"i?"),
  23406. BUILDIN_DEF(getequipcardcnt,"i"),
  23407. BUILDIN_DEF(successremovecards,"i"),
  23408. BUILDIN_DEF(failedremovecards,"ii"),
  23409. BUILDIN_DEF(marriage,"s"),
  23410. BUILDIN_DEF2(wedding_effect,"wedding",""),
  23411. BUILDIN_DEF(divorce,"?"),
  23412. BUILDIN_DEF(ispartneron,"?"),
  23413. BUILDIN_DEF(getpartnerid,"?"),
  23414. BUILDIN_DEF(getchildid,"?"),
  23415. BUILDIN_DEF(getmotherid,"?"),
  23416. BUILDIN_DEF(getfatherid,"?"),
  23417. BUILDIN_DEF(warppartner,"sii"),
  23418. BUILDIN_DEF(getitemname,"v"),
  23419. BUILDIN_DEF(getitemslots,"i"),
  23420. BUILDIN_DEF(makepet,"i"),
  23421. BUILDIN_DEF(getexp,"ii?"),
  23422. BUILDIN_DEF(getinventorylist,"?"),
  23423. BUILDIN_DEF(getskilllist,"?"),
  23424. BUILDIN_DEF(clearitem,"?"),
  23425. BUILDIN_DEF(classchange,"i??"),
  23426. BUILDIN_DEF(misceffect,"i"),
  23427. BUILDIN_DEF(playBGM,"s"),
  23428. BUILDIN_DEF(playBGMall,"s?????"),
  23429. BUILDIN_DEF(soundeffect,"si"),
  23430. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  23431. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  23432. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  23433. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  23434. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  23435. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  23436. BUILDIN_DEF(petloot,"i"), // [Valaris]
  23437. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  23438. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  23439. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  23440. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  23441. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  23442. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  23443. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  23444. BUILDIN_DEF(removespecialeffect,"i??"),
  23445. BUILDIN_DEF2(removespecialeffect,"removespecialeffect2","i??"),
  23446. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  23447. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  23448. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  23449. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  23450. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  23451. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  23452. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  23453. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  23454. BUILDIN_DEF(mobcount,"ss"),
  23455. BUILDIN_DEF(getlook,"i?"),
  23456. BUILDIN_DEF(getsavepoint,"i?"),
  23457. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  23458. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  23459. BUILDIN_DEF(npcstop,""), // [Valaris]
  23460. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  23461. BUILDIN_DEF(mapid2name,"i"),
  23462. BUILDIN_DEF(checkoption1,"i?"),
  23463. BUILDIN_DEF(checkoption2,"i?"),
  23464. BUILDIN_DEF(guildgetexp,"i"),
  23465. BUILDIN_DEF(guildchangegm,"is"),
  23466. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  23467. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  23468. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  23469. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  23470. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  23471. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  23472. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  23473. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  23474. BUILDIN_DEF(night,""), // sets the server to night time
  23475. BUILDIN_DEF(day,""), // sets the server to day time
  23476. #ifdef PCRE_SUPPORT
  23477. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  23478. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  23479. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  23480. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  23481. #endif
  23482. BUILDIN_DEF(preg_match,"ss?"),
  23483. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  23484. BUILDIN_DEF(recovery,"i???"),
  23485. BUILDIN_DEF(getpetinfo,"i?"),
  23486. BUILDIN_DEF(gethominfo,"i?"),
  23487. BUILDIN_DEF(addhomintimacy,"i?"),
  23488. BUILDIN_DEF(getmercinfo,"i?"),
  23489. BUILDIN_DEF(checkequipedcard,"i"),
  23490. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  23491. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  23492. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  23493. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  23494. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  23495. BUILDIN_DEF(charat,"si"),
  23496. BUILDIN_DEF(setchar,"ssi"),
  23497. BUILDIN_DEF(insertchar,"ssi"),
  23498. BUILDIN_DEF(delchar,"si"),
  23499. BUILDIN_DEF(strtoupper,"s"),
  23500. BUILDIN_DEF(strtolower,"s"),
  23501. BUILDIN_DEF(charisupper, "si"),
  23502. BUILDIN_DEF(charislower, "si"),
  23503. BUILDIN_DEF(substr,"sii"),
  23504. BUILDIN_DEF(explode, "rss"),
  23505. BUILDIN_DEF(implode, "r?"),
  23506. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  23507. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  23508. BUILDIN_DEF(strpos,"ss?"),
  23509. BUILDIN_DEF(replacestr,"sss??"),
  23510. BUILDIN_DEF(countstr,"ss?"),
  23511. BUILDIN_DEF(setnpcdisplay,"sv??"),
  23512. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  23513. BUILDIN_DEF(strcmp,"ss"),
  23514. BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
  23515. BUILDIN_DEF(setiteminfo,"vii"), //[Lupus] set Items Buy / sell Price, etc info
  23516. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  23517. // [zBuffer] List of mathematics commands --->
  23518. BUILDIN_DEF(sqrt,"i"),
  23519. BUILDIN_DEF(pow,"ii"),
  23520. BUILDIN_DEF(distance,"iiii"),
  23521. BUILDIN_DEF2(minmax,"min", "*"),
  23522. BUILDIN_DEF2(minmax,"minimum", "*"),
  23523. BUILDIN_DEF2(minmax,"max", "*"),
  23524. BUILDIN_DEF2(minmax,"maximum", "*"),
  23525. BUILDIN_DEF(cap_value, "iii"),
  23526. // <--- [zBuffer] List of mathematics commands
  23527. BUILDIN_DEF(md5,"s"),
  23528. // [zBuffer] List of dynamic var commands --->
  23529. BUILDIN_DEF(getd,"s"),
  23530. BUILDIN_DEF(setd,"sv?"),
  23531. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  23532. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  23533. BUILDIN_DEF(npcshopadditem,"sii*"),
  23534. BUILDIN_DEF(npcshopdelitem,"si*"),
  23535. BUILDIN_DEF(npcshopattach,"s?"),
  23536. BUILDIN_DEF(equip,"i?"),
  23537. BUILDIN_DEF(autoequip,"ii"),
  23538. BUILDIN_DEF(setbattleflag,"si?"),
  23539. BUILDIN_DEF(getbattleflag,"s"),
  23540. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  23541. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  23542. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  23543. BUILDIN_DEF(getrandmobid, "i??"),
  23544. BUILDIN_DEF(getmonsterinfo,"vi"), //Lupus
  23545. BUILDIN_DEF(addmonsterdrop,"vii??"), //Akinari [Lupus]
  23546. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  23547. BUILDIN_DEF(axtoi,"s"),
  23548. BUILDIN_DEF(query_sql,"s*"),
  23549. BUILDIN_DEF(query_logsql,"s*"),
  23550. BUILDIN_DEF(escape_sql,"v"),
  23551. BUILDIN_DEF(atoi,"s"),
  23552. BUILDIN_DEF(strtol,"si"),
  23553. // [zBuffer] List of player cont commands --->
  23554. BUILDIN_DEF(rid2name,"i"),
  23555. BUILDIN_DEF(pcfollow,"ii"),
  23556. BUILDIN_DEF(pcstopfollow,"i"),
  23557. BUILDIN_DEF(pcblockmove,"ii"),
  23558. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  23559. BUILDIN_DEF(pcblockskill,"ii"),
  23560. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  23561. BUILDIN_DEF(setpcblock, "ii?"),
  23562. BUILDIN_DEF(getpcblock, "?"),
  23563. // <--- [zBuffer] List of player cont commands
  23564. // [zBuffer] List of unit control commands --->
  23565. BUILDIN_DEF(unitexists,"i"),
  23566. BUILDIN_DEF(getunittype,"i"),
  23567. BUILDIN_DEF(getunitname,"i"),
  23568. BUILDIN_DEF(setunitname,"is"),
  23569. BUILDIN_DEF(setunittitle,"is"),
  23570. BUILDIN_DEF(getunittitle,"i"),
  23571. BUILDIN_DEF(getunitdata,"i*"),
  23572. BUILDIN_DEF(setunitdata,"iii"),
  23573. BUILDIN_DEF(unitwalk,"iii?"),
  23574. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  23575. BUILDIN_DEF(unitkill,"i"),
  23576. BUILDIN_DEF(unitwarp,"isii"),
  23577. BUILDIN_DEF(unitattack,"iv?"),
  23578. BUILDIN_DEF(unitstopattack,"i"),
  23579. BUILDIN_DEF(unitstopwalk,"i?"),
  23580. BUILDIN_DEF(unittalk,"is?"),
  23581. BUILDIN_DEF(unitskilluseid,"ivi?????"), // originally by Qamera [Celest]
  23582. BUILDIN_DEF(unitskillusepos,"iviii????"), // [Celest]
  23583. // <--- [zBuffer] List of unit control commands
  23584. BUILDIN_DEF(sleep,"i"),
  23585. BUILDIN_DEF(sleep2,"i"),
  23586. BUILDIN_DEF(awake,"s"),
  23587. BUILDIN_DEF(getvariableofnpc,"rs"),
  23588. BUILDIN_DEF(warpportal,"iisii"),
  23589. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  23590. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  23591. BUILDIN_DEF(morphembryo,""),
  23592. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  23593. BUILDIN_DEF(checkhomcall,""),
  23594. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  23595. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  23596. BUILDIN_DEF(checkvending,"?"),
  23597. BUILDIN_DEF(checkchatting,"?"),
  23598. BUILDIN_DEF(checkidle,"?"),
  23599. BUILDIN_DEF(checkidlehom,"?"),
  23600. BUILDIN_DEF(checkidlemer,"?"),
  23601. BUILDIN_DEF(openmail,"?"),
  23602. BUILDIN_DEF(openauction,"?"),
  23603. BUILDIN_DEF(checkcell,"siii"),
  23604. BUILDIN_DEF(setcell,"siiiiii"),
  23605. BUILDIN_DEF(getfreecell,"srr?????"),
  23606. BUILDIN_DEF(setwall,"siiiiis"),
  23607. BUILDIN_DEF(delwall,"s"),
  23608. BUILDIN_DEF(checkwall,"s"),
  23609. BUILDIN_DEF(searchitem,"rs"),
  23610. BUILDIN_DEF(mercenary_create,"ii"),
  23611. BUILDIN_DEF(mercenary_delete,"??"),
  23612. BUILDIN_DEF(mercenary_heal,"ii"),
  23613. BUILDIN_DEF(mercenary_sc_start,"iii"),
  23614. BUILDIN_DEF(mercenary_get_calls,"i"),
  23615. BUILDIN_DEF(mercenary_get_faith,"i"),
  23616. BUILDIN_DEF(mercenary_set_calls,"ii"),
  23617. BUILDIN_DEF(mercenary_set_faith,"ii"),
  23618. BUILDIN_DEF(readbook,"ii"),
  23619. BUILDIN_DEF(setfont,"i"),
  23620. BUILDIN_DEF(areamobuseskill,"siiivviiiii"),
  23621. BUILDIN_DEF(progressbar,"si"),
  23622. BUILDIN_DEF(progressbar_npc, "si?"),
  23623. BUILDIN_DEF(pushpc,"ii"),
  23624. BUILDIN_DEF(buyingstore,"i"),
  23625. BUILDIN_DEF(searchstores,"ii"),
  23626. BUILDIN_DEF(showdigit,"i?"),
  23627. // WoE SE
  23628. BUILDIN_DEF(agitstart2,""),
  23629. BUILDIN_DEF(agitend2,""),
  23630. BUILDIN_DEF(agitcheck2,""),
  23631. // BattleGround
  23632. BUILDIN_DEF(waitingroom2bg,"sii???"),
  23633. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  23634. BUILDIN_DEF(bg_team_setxy,"iii"),
  23635. BUILDIN_DEF(bg_warp,"isii"),
  23636. BUILDIN_DEF(bg_monster,"isiisi?"),
  23637. BUILDIN_DEF(bg_monster_set_team,"ii"),
  23638. BUILDIN_DEF(bg_leave,"?"),
  23639. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  23640. BUILDIN_DEF(bg_destroy,"i"),
  23641. BUILDIN_DEF(areapercentheal,"siiiiii"),
  23642. BUILDIN_DEF(bg_get_data,"ii"),
  23643. BUILDIN_DEF(bg_getareausers,"isiiii"),
  23644. BUILDIN_DEF(bg_updatescore,"sii"),
  23645. BUILDIN_DEF(bg_join,"i????"),
  23646. BUILDIN_DEF(bg_create,"sii??"),
  23647. BUILDIN_DEF(bg_reserve,"s?"),
  23648. BUILDIN_DEF(bg_unbook,"s"),
  23649. BUILDIN_DEF(bg_info,"si"),
  23650. // Instancing
  23651. BUILDIN_DEF(instance_create,"s??"),
  23652. BUILDIN_DEF(instance_destroy,"?"),
  23653. BUILDIN_DEF(instance_id,"?"),
  23654. BUILDIN_DEF(instance_enter,"s????"),
  23655. BUILDIN_DEF(instance_npcname,"s?"),
  23656. BUILDIN_DEF(instance_mapname,"s?"),
  23657. BUILDIN_DEF(instance_warpall,"sii??"),
  23658. BUILDIN_DEF(instance_announce,"isi?????"),
  23659. BUILDIN_DEF(instance_check_party,"i???"),
  23660. BUILDIN_DEF(instance_check_guild,"i???"),
  23661. BUILDIN_DEF(instance_check_clan,"i???"),
  23662. BUILDIN_DEF(instance_info,"si?"),
  23663. BUILDIN_DEF(instance_live_info,"i?"),
  23664. BUILDIN_DEF(instance_list, "s?"),
  23665. /**
  23666. * 3rd-related
  23667. **/
  23668. BUILDIN_DEF(makerune,"i?"),
  23669. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  23670. BUILDIN_DEF(setdragon,"??"),//[Ind]
  23671. BUILDIN_DEF(ismounting,"?"),//[Ind]
  23672. BUILDIN_DEF(setmounting,"?"),//[Ind]
  23673. BUILDIN_DEF(checkre,"i"),
  23674. /**
  23675. * rAthena and beyond!
  23676. **/
  23677. BUILDIN_DEF(getargcount,""),
  23678. BUILDIN_DEF(getcharip,"?"),
  23679. BUILDIN_DEF(is_function,"s"),
  23680. BUILDIN_DEF(get_revision,""),
  23681. BUILDIN_DEF(get_githash,""),
  23682. BUILDIN_DEF(freeloop,"?"),
  23683. BUILDIN_DEF(getrandgroupitem,"i????"),
  23684. BUILDIN_DEF(cleanmap,"s"),
  23685. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  23686. BUILDIN_DEF(npcskill,"viii"),
  23687. BUILDIN_DEF(consumeitem,"v?"),
  23688. BUILDIN_DEF(delequip,"i?"),
  23689. BUILDIN_DEF(breakequip,"i?"),
  23690. BUILDIN_DEF(sit,"?"),
  23691. BUILDIN_DEF(stand,"?"),
  23692. //@commands (script based)
  23693. BUILDIN_DEF(bindatcmd, "ss??"),
  23694. BUILDIN_DEF(unbindatcmd, "s"),
  23695. BUILDIN_DEF(useatcmd, "s"),
  23696. //Quest Log System [Inkfish]
  23697. BUILDIN_DEF(questinfo, "i??"),
  23698. BUILDIN_DEF(setquest, "i?"),
  23699. BUILDIN_DEF(erasequest, "i?"),
  23700. BUILDIN_DEF(completequest, "i?"),
  23701. BUILDIN_DEF(checkquest, "i??"),
  23702. BUILDIN_DEF(isbegin_quest,"i?"),
  23703. BUILDIN_DEF(changequest, "ii?"),
  23704. BUILDIN_DEF(showevent, "i??"),
  23705. BUILDIN_DEF(questinfo_refresh, "?"),
  23706. //Bound items [Xantara] & [Akinari]
  23707. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  23708. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  23709. BUILDIN_DEF(countbound, "??"),
  23710. // Party related
  23711. BUILDIN_DEF(party_create,"s???"),
  23712. BUILDIN_DEF(party_addmember,"ii"),
  23713. BUILDIN_DEF(party_delmember,"??"),
  23714. BUILDIN_DEF(party_changeleader,"ii"),
  23715. BUILDIN_DEF(party_changeoption,"iii"),
  23716. BUILDIN_DEF(party_destroy,"i"),
  23717. // Clan system
  23718. BUILDIN_DEF(clan_join,"i?"),
  23719. BUILDIN_DEF(clan_leave,"?"),
  23720. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  23721. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  23722. BUILDIN_DEF(vip_status,"i?"),
  23723. BUILDIN_DEF(vip_time,"i?"),
  23724. BUILDIN_DEF(bonus_script,"si????"),
  23725. BUILDIN_DEF(bonus_script_clear,"??"),
  23726. BUILDIN_DEF(getgroupitem,"i??"),
  23727. BUILDIN_DEF(enable_command,""),
  23728. BUILDIN_DEF(disable_command,""),
  23729. BUILDIN_DEF(getguildmember,"i??"),
  23730. BUILDIN_DEF(addspiritball,"ii?"),
  23731. BUILDIN_DEF(delspiritball,"i?"),
  23732. BUILDIN_DEF(countspiritball,"?"),
  23733. BUILDIN_DEF(mergeitem,"?"),
  23734. BUILDIN_DEF(mergeitem2,"??"),
  23735. BUILDIN_DEF(npcshopupdate,"sii?"),
  23736. BUILDIN_DEF(getattachedrid,""),
  23737. BUILDIN_DEF(getvar,"vi"),
  23738. BUILDIN_DEF(showscript,"s??"),
  23739. BUILDIN_DEF(ignoretimeout,"i?"),
  23740. BUILDIN_DEF(geteleminfo,"i?"),
  23741. BUILDIN_DEF(opendressroom,"i?"),
  23742. BUILDIN_DEF(navigateto,"s???????"),
  23743. BUILDIN_DEF(getguildalliance,"ii"),
  23744. BUILDIN_DEF(adopt,"vv"),
  23745. BUILDIN_DEF(getexp2,"ii?"),
  23746. BUILDIN_DEF(recalculatestat,""),
  23747. BUILDIN_DEF(hateffect,"ii"),
  23748. BUILDIN_DEF(getrandomoptinfo, "i"),
  23749. BUILDIN_DEF(getequiprandomoption, "iii?"),
  23750. BUILDIN_DEF(setrandomoption,"iiiii?"),
  23751. BUILDIN_DEF(needed_status_point,"ii?"),
  23752. BUILDIN_DEF(needed_trait_point, "ii?"),
  23753. BUILDIN_DEF(jobcanentermap,"s?"),
  23754. BUILDIN_DEF(openstorage2,"ii?"),
  23755. BUILDIN_DEF(unloadnpc, "s"),
  23756. BUILDIN_DEF(duplicate, "ssii?????"),
  23757. BUILDIN_DEF(duplicate_dynamic, "s?"),
  23758. // WoE TE
  23759. BUILDIN_DEF(agitstart3,""),
  23760. BUILDIN_DEF(agitend3,""),
  23761. BUILDIN_DEF(agitcheck3,""),
  23762. BUILDIN_DEF(gvgon3,"s"),
  23763. BUILDIN_DEF(gvgoff3,"s"),
  23764. // Channel System
  23765. BUILDIN_DEF(channel_create,"ss?????"),
  23766. BUILDIN_DEF(channel_join, "s?"),
  23767. BUILDIN_DEF(channel_setopt,"sii"),
  23768. BUILDIN_DEF(channel_getopt,"si"),
  23769. BUILDIN_DEF(channel_setcolor,"si"),
  23770. BUILDIN_DEF(channel_setpass,"ss"),
  23771. BUILDIN_DEF(channel_setgroup,"si*"),
  23772. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  23773. BUILDIN_DEF(channel_chat,"ss?"),
  23774. BUILDIN_DEF(channel_ban,"si"),
  23775. BUILDIN_DEF(channel_unban,"si"),
  23776. BUILDIN_DEF(channel_kick,"sv"),
  23777. BUILDIN_DEF(channel_delete,"s"),
  23778. // Item Random Option Extension [Cydh]
  23779. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  23780. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  23781. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  23782. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr?"),
  23783. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  23784. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  23785. // Grade Extension
  23786. BUILDIN_DEF2(getitem2,"getitem4","viiiiiiiiirrr?"),
  23787. BUILDIN_DEF2(getitem2,"getitembound4","viiiiiiiiiirrr?"),
  23788. BUILDIN_DEF2(rentitem2,"rentitem4","viiiiiiiiirrr?"),
  23789. BUILDIN_DEF2(makeitem2,"makeitem4","visiiiiiiiiiirrr?"),
  23790. BUILDIN_DEF2(delitem2,"delitem4","viiiiiiiiirrr?"),
  23791. BUILDIN_DEF2(countitem,"countitem4","viiiiiiiirrr?"),
  23792. // Achievement System
  23793. BUILDIN_DEF(achievementinfo,"ii?"),
  23794. BUILDIN_DEF(achievementadd,"i?"),
  23795. BUILDIN_DEF(achievementremove,"i?"),
  23796. BUILDIN_DEF(achievementcomplete,"i?"),
  23797. BUILDIN_DEF(achievementexists,"i?"),
  23798. BUILDIN_DEF(achievementupdate,"iii?"),
  23799. BUILDIN_DEF(getequiprefinecost,"iii?"),
  23800. BUILDIN_DEF(refineui,"?"),
  23801. BUILDIN_DEF2(round, "round", "ii"),
  23802. BUILDIN_DEF2(round, "ceil", "ii"),
  23803. BUILDIN_DEF2(round, "floor", "ii"),
  23804. BUILDIN_DEF(getequiptradability, "i?"),
  23805. BUILDIN_DEF(mail, "isss*"),
  23806. BUILDIN_DEF(open_roulette,"?"),
  23807. BUILDIN_DEF(identifyall,"??"),
  23808. BUILDIN_DEF(is_guild_leader,"?"),
  23809. BUILDIN_DEF(is_party_leader,"?"),
  23810. BUILDIN_DEF(camerainfo,"iii?"),
  23811. BUILDIN_DEF(achievement_condition,"i"),
  23812. BUILDIN_DEF(getinstancevar,"ri"),
  23813. BUILDIN_DEF2_DEPRECATED(getinstancevar, "getvariableofinstance","ri", "2021-12-13"),
  23814. BUILDIN_DEF(convertpcinfo,"vi"),
  23815. BUILDIN_DEF(isnpccloaked, "??"),
  23816. BUILDIN_DEF(rentalcountitem, "v?"),
  23817. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  23818. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  23819. BUILDIN_DEF2(rentalcountitem, "rentalcountitem4", "viiiiiiiirrr?"),
  23820. BUILDIN_DEF(getenchantgrade, "??"),
  23821. BUILDIN_DEF(mob_setidleevent, "is"),
  23822. BUILDIN_DEF(setinstancevar,"rvi"),
  23823. BUILDIN_DEF(openstylist, "?"),
  23824. // Navigation Generation System
  23825. BUILDIN_DEF(naviregisterwarp, "ssii"),
  23826. BUILDIN_DEF(navihide, ""),
  23827. BUILDIN_DEF(getitempos,""),
  23828. BUILDIN_DEF(laphine_synthesis, "?"),
  23829. BUILDIN_DEF(laphine_upgrade, ""),
  23830. BUILDIN_DEF(randomoptgroup,"i"),
  23831. BUILDIN_DEF(open_quest_ui, "??"),
  23832. BUILDIN_DEF(openbank,"?"),
  23833. BUILDIN_DEF(getbaseexp_ratio, "i??"),
  23834. BUILDIN_DEF(getjobexp_ratio, "i??"),
  23835. BUILDIN_DEF(enchantgradeui, "?" ),
  23836. BUILDIN_DEF(set_reputation_points, "ii?"),
  23837. BUILDIN_DEF(get_reputation_points, "i?"),
  23838. BUILDIN_DEF(add_reputation_points, "ii?"),
  23839. BUILDIN_DEF(item_reform, "??"),
  23840. BUILDIN_DEF(item_enchant, "i?"),
  23841. BUILDIN_DEF(itemlink, "i?????????"),
  23842. BUILDIN_DEF(mesitemlink, "i??"),
  23843. BUILDIN_DEF(addfame, "i?"),
  23844. BUILDIN_DEF(getfame, "?"),
  23845. BUILDIN_DEF(getfamerank, "?"),
  23846. BUILDIN_DEF(isdead, "?"),
  23847. BUILDIN_DEF(macro_detector, "?"),
  23848. BUILDIN_DEF(has_autoloot,"?"),
  23849. BUILDIN_DEF(autoloot,"??"),
  23850. BUILDIN_DEF(opentips, "i?"),
  23851. #include <custom/script_def.inc>
  23852. {NULL,NULL,NULL},
  23853. };