script.cpp 742 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503
  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 "../../3rdparty/pcre/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 "channel.hpp"
  33. #include "chat.hpp"
  34. #include "chrif.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "date.hpp" // date type enum, date_get()
  38. #include "elemental.hpp"
  39. #include "guild.hpp"
  40. #include "homunculus.hpp"
  41. #include "instance.hpp"
  42. #include "intif.hpp"
  43. #include "itemdb.hpp"
  44. #include "log.hpp"
  45. #include "mail.hpp"
  46. #include "map.hpp"
  47. #include "mapreg.hpp"
  48. #include "mercenary.hpp"
  49. #include "mob.hpp"
  50. #include "npc.hpp"
  51. #include "party.hpp"
  52. #include "path.hpp"
  53. #include "pc.hpp"
  54. #include "pc_groups.hpp"
  55. #include "pet.hpp"
  56. #include "quest.hpp"
  57. #include "storage.hpp"
  58. using namespace rathena;
  59. const int64 SCRIPT_INT_MIN = INT64_MIN;
  60. const int64 SCRIPT_INT_MAX = INT64_MAX;
  61. struct eri *array_ers;
  62. DBMap *st_db;
  63. unsigned int active_scripts;
  64. unsigned int next_id;
  65. struct eri *st_ers;
  66. struct eri *stack_ers;
  67. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func );
  68. /**
  69. * Get `sd` from a account id in `loc` param instead of attached rid
  70. * @param st Script
  71. * @param loc Location to look account id in script parameter
  72. * @param sd Variable that will be assigned
  73. * @return True if `sd` is assigned, false otherwise
  74. **/
  75. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  76. if (script_hasdata(st, loc)) {
  77. int id_ = script_getnum(st, loc);
  78. if (!(*sd = map_id2sd(id_))){
  79. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  80. return false;
  81. }else{
  82. return true;
  83. }
  84. }
  85. else
  86. return script_rid2sd_(st,sd,func);
  87. }
  88. /**
  89. * Get `sd` from a char id in `loc` param instead of attached rid
  90. * @param st Script
  91. * @param loc Location to look char id in script parameter
  92. * @param sd Variable that will be assigned
  93. * @return True if `sd` is assigned, false otherwise
  94. **/
  95. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  96. if (script_hasdata(st, loc)) {
  97. int id_ = script_getnum(st, loc);
  98. if (!(*sd = map_charid2sd(id_))){
  99. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  100. return false;
  101. }else{
  102. return true;
  103. }
  104. }
  105. else
  106. return script_rid2sd_(st,sd,func);
  107. }
  108. /**
  109. * Get `sd` from a nick in `loc` param instead of attached rid
  110. * @param st Script
  111. * @param loc Location to look nick in script parameter
  112. * @param sd Variable that will be assigned
  113. * @return True if `sd` is assigned, false otherwise
  114. **/
  115. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  116. if (script_hasdata(st, loc)) {
  117. const char *name_ = script_getstr(st, loc);
  118. if (!(*sd = map_nick2sd(name_,false))){
  119. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  120. return false;
  121. }else{
  122. return true;
  123. }
  124. }
  125. else
  126. return script_rid2sd_(st,sd,func);
  127. }
  128. /**
  129. * Get `sd` from a mapid in `loc` param instead of attached rid
  130. * @param st Script
  131. * @param loc Location to look mapid in script parameter
  132. * @param sd Variable that will be assigned
  133. * @return True if `sd` is assigned, false otherwise
  134. **/
  135. static bool script_mapid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  136. if (script_hasdata(st, loc)) {
  137. int id_ = script_getnum(st, loc);
  138. if (!(*sd = map_id2sd(id_))){
  139. ShowError("%s: Player with map id '%d' is not found.\n", func, id_);
  140. return false;
  141. }else{
  142. return true;
  143. }
  144. }
  145. else
  146. return script_rid2sd_(st,sd,func);
  147. }
  148. /**
  149. * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator.
  150. * @param st Script
  151. * @param loc Location to look for ID in script parameter
  152. * @param bl Variable that will be assigned
  153. * @return True if `bl` is assigned, false otherwise
  154. **/
  155. static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) {
  156. int unit_id;
  157. if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0)
  158. unit_id = st->rid;
  159. *bl = map_id2bl(unit_id);
  160. if ( *bl )
  161. return true;
  162. else {
  163. ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id);
  164. return false;
  165. }
  166. }
  167. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  168. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  169. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  170. #define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__)
  171. #define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__)
  172. #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__)
  173. /// temporary buffer for passing around compiled bytecode
  174. /// @see add_scriptb, set_label, parse_script
  175. static unsigned char* script_buf = NULL;
  176. static int script_pos = 0, script_size = 0;
  177. static inline int GETVALUE(const unsigned char* buf, int i)
  178. {
  179. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  180. }
  181. static inline void SETVALUE(unsigned char* buf, int i, int n)
  182. {
  183. buf[i] = GetByte(n, 0);
  184. buf[i+1] = GetByte(n, 1);
  185. buf[i+2] = GetByte(n, 2);
  186. }
  187. // String buffer structures.
  188. // str_data stores string information
  189. static struct str_data_struct {
  190. enum c_op type;
  191. int str;
  192. int backpatch;
  193. int label;
  194. int (*func)(struct script_state *st);
  195. int64 val;
  196. int next;
  197. const char *name;
  198. bool deprecated;
  199. } *str_data = nullptr;
  200. static int str_data_size = 0; // size of the data
  201. static int str_num = LABEL_START; // next id to be assigned
  202. // str_buf holds the strings themselves
  203. static char *str_buf;
  204. static int str_size = 0; // size of the buffer
  205. static int str_pos = 0; // next position to be assigned
  206. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  207. #define SCRIPT_HASH_SIZE 1021
  208. int str_hash[SCRIPT_HASH_SIZE];
  209. // Specifies which string hashing method to use
  210. //#define SCRIPT_HASH_DJB2
  211. //#define SCRIPT_HASH_SDBM
  212. #define SCRIPT_HASH_ELF
  213. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  214. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  215. static int parse_options = 0;
  216. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  217. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  218. // important buildin function references for usage in scripts
  219. static int buildin_set_ref = 0;
  220. static int buildin_callsub_ref = 0;
  221. static int buildin_callfunc_ref = 0;
  222. static int buildin_getelementofarray_ref = 0;
  223. // Caches compiled autoscript item code.
  224. // Note: This is not cleared when reloading itemdb.
  225. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  226. struct Script_Config script_config = {
  227. 1, // warn_func_mismatch_argtypes
  228. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  229. 0, INT_MAX, // input_min_value/input_max_value
  230. // NOTE: None of these event labels should be longer than <EVENT_NAME_LENGTH> characters
  231. // PC related
  232. "OnPCDieEvent", //die_event_name
  233. "OnPCKillEvent", //kill_pc_event_name
  234. "OnNPCKillEvent", //kill_mob_event_name
  235. "OnPCLoginEvent", //login_event_name
  236. "OnPCLogoutEvent", //logout_event_name
  237. "OnPCLoadMapEvent", //loadmap_event_name
  238. "OnPCBaseLvUpEvent", //baselvup_event_name
  239. "OnPCJobLvUpEvent", //joblvup_event_name
  240. // NPC related
  241. "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves)
  242. "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area)
  243. "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area)
  244. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  245. "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc)
  246. "OnBuyItem", //onbuy_event_name (is executed when items are bought)
  247. "OnSellItem", //onsell_event_name (is executed when items are sold)
  248. // Init related
  249. "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded)
  250. "OnInterIfInit", //inter_init_event_name (is executed on inter server connection)
  251. "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection)
  252. // Guild related
  253. "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken)
  254. "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started)
  255. "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded)
  256. "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended)
  257. "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started)
  258. "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded)
  259. "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended)
  260. "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started)
  261. "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded)
  262. "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended)
  263. // Timer related
  264. "OnTimer", //timer_event_name (is executed by a timer at the specific second)
  265. "OnTimerQuit", //timer_quit_event_name (is executed when a timer is aborted)
  266. "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute)
  267. "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour)
  268. "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute)
  269. "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day)
  270. "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute)
  271. "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute)
  272. "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute)
  273. "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute)
  274. "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute)
  275. "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute)
  276. "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute)
  277. // Instance related
  278. "OnInstanceInit", //instance_init_event_name (is executed right after instance creation)
  279. "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction)
  280. "OnNaviGenerate", //navi_generate_name (is executed right before navi generation)
  281. };
  282. static jmp_buf error_jump;
  283. static char* error_msg;
  284. static const char* error_pos;
  285. static int error_report; // if the error should produce output
  286. // Used by disp_warning_message
  287. static const char* parser_current_src;
  288. static const char* parser_current_file;
  289. static int parser_current_line;
  290. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  291. // [Eoe / jA 1080, 1081, 1094, 1164]
  292. enum curly_type {
  293. TYPE_NULL = 0,
  294. TYPE_IF,
  295. TYPE_SWITCH,
  296. TYPE_WHILE,
  297. TYPE_FOR,
  298. TYPE_DO,
  299. TYPE_USERFUNC,
  300. TYPE_ARGLIST // function argument list
  301. };
  302. enum e_arglist
  303. {
  304. ARGLIST_UNDEFINED = 0,
  305. ARGLIST_NO_PAREN = 1,
  306. ARGLIST_PAREN = 2,
  307. };
  308. static struct {
  309. struct {
  310. enum curly_type type;
  311. int index;
  312. int count;
  313. int flag;
  314. struct linkdb_node *case_label;
  315. } curly[256]; // Information right parenthesis
  316. int curly_count; // The number of right brackets
  317. int index; // Number of the syntax used in the script
  318. } syntax;
  319. const char* parse_curly_close(const char* p);
  320. const char* parse_syntax_close(const char* p);
  321. const char* parse_syntax_close_sub(const char* p,int* flag);
  322. const char* parse_syntax(const char* p);
  323. static int parse_syntax_for_flag = 0;
  324. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  325. extern unsigned int current_equip_combo_pos;
  326. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  327. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  328. int potion_target = 0;
  329. unsigned int *generic_ui_array = NULL;
  330. unsigned int generic_ui_array_size = 0;
  331. c_op get_com(unsigned char *script,int *pos);
  332. int64 get_num(unsigned char *script,int *pos);
  333. typedef struct script_function {
  334. int (*func)(struct script_state *st);
  335. const char *name;
  336. const char *arg;
  337. const char *deprecated;
  338. } script_function;
  339. extern script_function buildin_func[];
  340. static struct linkdb_node *sleep_db; // int oid -> struct script_state *
  341. /*==========================================
  342. * (Only those needed) local declaration prototype
  343. *------------------------------------------*/
  344. const char* parse_subexpr(const char* p,int limit);
  345. int run_func(struct script_state *st);
  346. int script_instancegetid(struct script_state *st, e_instance_mode mode = IM_NONE);
  347. const char* script_op2name(int op)
  348. {
  349. #define RETURN_OP_NAME(type) case type: return #type
  350. switch( op )
  351. {
  352. RETURN_OP_NAME(C_NOP);
  353. RETURN_OP_NAME(C_POS);
  354. RETURN_OP_NAME(C_INT);
  355. RETURN_OP_NAME(C_PARAM);
  356. RETURN_OP_NAME(C_FUNC);
  357. RETURN_OP_NAME(C_STR);
  358. RETURN_OP_NAME(C_CONSTSTR);
  359. RETURN_OP_NAME(C_ARG);
  360. RETURN_OP_NAME(C_NAME);
  361. RETURN_OP_NAME(C_EOL);
  362. RETURN_OP_NAME(C_RETINFO);
  363. RETURN_OP_NAME(C_USERFUNC);
  364. RETURN_OP_NAME(C_USERFUNC_POS);
  365. RETURN_OP_NAME(C_REF);
  366. // operators
  367. RETURN_OP_NAME(C_OP3);
  368. RETURN_OP_NAME(C_LOR);
  369. RETURN_OP_NAME(C_LAND);
  370. RETURN_OP_NAME(C_LE);
  371. RETURN_OP_NAME(C_LT);
  372. RETURN_OP_NAME(C_GE);
  373. RETURN_OP_NAME(C_GT);
  374. RETURN_OP_NAME(C_EQ);
  375. RETURN_OP_NAME(C_NE);
  376. RETURN_OP_NAME(C_XOR);
  377. RETURN_OP_NAME(C_OR);
  378. RETURN_OP_NAME(C_AND);
  379. RETURN_OP_NAME(C_ADD);
  380. RETURN_OP_NAME(C_SUB);
  381. RETURN_OP_NAME(C_MUL);
  382. RETURN_OP_NAME(C_DIV);
  383. RETURN_OP_NAME(C_MOD);
  384. RETURN_OP_NAME(C_NEG);
  385. RETURN_OP_NAME(C_LNOT);
  386. RETURN_OP_NAME(C_NOT);
  387. RETURN_OP_NAME(C_R_SHIFT);
  388. RETURN_OP_NAME(C_L_SHIFT);
  389. RETURN_OP_NAME(C_ADD_POST);
  390. RETURN_OP_NAME(C_SUB_POST);
  391. RETURN_OP_NAME(C_ADD_PRE);
  392. RETURN_OP_NAME(C_SUB_PRE);
  393. default:
  394. ShowDebug("script_op2name: unexpected op=%d\n", op);
  395. return "???";
  396. }
  397. #undef RETURN_OP_NAME
  398. }
  399. #ifdef DEBUG_DUMP_STACK
  400. static void script_dump_stack(struct script_state* st)
  401. {
  402. int i;
  403. ShowMessage("\tstart = %d\n", st->start);
  404. ShowMessage("\tend = %d\n", st->end);
  405. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  406. ShowMessage("\tsp = %d\n", st->stack->sp);
  407. for( i = 0; i < st->stack->sp; ++i )
  408. {
  409. struct script_data* data = &st->stack->stack_data[i];
  410. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  411. switch( data->type )
  412. {
  413. case C_INT:
  414. case C_POS:
  415. ShowMessage(" %d\n", data->u.num);
  416. break;
  417. case C_STR:
  418. case C_CONSTSTR:
  419. ShowMessage(" \"%s\"\n", data->u.str);
  420. break;
  421. case C_NAME:
  422. 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));
  423. break;
  424. case C_RETINFO:
  425. {
  426. struct script_retinfo* ri = data->u.ri;
  427. 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);
  428. }
  429. break;
  430. default:
  431. ShowMessage("\n");
  432. break;
  433. }
  434. }
  435. }
  436. #endif
  437. /// Reports on the console the src of a script error.
  438. static void script_reportsrc(struct script_state *st)
  439. {
  440. if( st->oid == 0 )
  441. return; //Can't report source.
  442. struct block_list* bl = map_id2bl(st->oid);
  443. if (!bl)
  444. return;
  445. switch( bl->type ) {
  446. case BL_NPC: {
  447. struct npc_data* nd = (struct npc_data*)bl;
  448. if( bl->m >= 0 )
  449. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", nd->name, map_mapid2mapname(bl->m), bl->x, bl->y);
  450. else
  451. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", nd->name);
  452. ShowDebug( "Source (NPC): %s is located in: %s\n", nd->name, nd->path );
  453. } break;
  454. default:
  455. if( bl->m >= 0 )
  456. 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);
  457. else
  458. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  459. break;
  460. }
  461. }
  462. /// Reports on the console information about the script data.
  463. static void script_reportdata(struct script_data* data)
  464. {
  465. if( data == NULL )
  466. return;
  467. switch( data->type ) {
  468. case C_NOP:// no value
  469. ShowDebug("Data: nothing (nil)\n");
  470. break;
  471. case C_INT:// number
  472. ShowDebug("Data: number value=%" PRId64 "\n", data->u.num);
  473. break;
  474. case C_STR:
  475. case C_CONSTSTR:// string
  476. if( data->u.str ) {
  477. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  478. } else {
  479. ShowDebug("Data: string value=NULL\n");
  480. }
  481. break;
  482. case C_NAME:// reference
  483. if( reference_tovariable(data) ) {// variable
  484. const char* name = reference_getname(data);
  485. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  486. } else if( reference_toconstant(data) ) {// constant
  487. ShowDebug("Data: constant name='%s' value=%" PRId64 "\n", reference_getname(data), reference_getconstant(data));
  488. } else if( reference_toparam(data) ) {// param
  489. ShowDebug("Data: param name='%s' type=%" PRId64 "\n", reference_getname(data), reference_getparamtype(data));
  490. } else {// ???
  491. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  492. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  493. }
  494. break;
  495. case C_POS:// label
  496. ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num);
  497. break;
  498. default:
  499. ShowDebug("Data: %s\n", script_op2name(data->type));
  500. break;
  501. }
  502. }
  503. /// Reports on the console information about the current built-in function.
  504. static void script_reportfunc(struct script_state* st)
  505. {
  506. int params, id;
  507. struct script_data* data;
  508. if( !script_hasdata(st,0) )
  509. {// no stack
  510. return;
  511. }
  512. data = script_getdata(st,0);
  513. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  514. {// script currently not executing a built-in function or corrupt stack
  515. return;
  516. }
  517. id = reference_getid(data);
  518. params = script_lastdata(st)-1;
  519. if( params > 0 )
  520. {
  521. int i;
  522. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  523. for( i = 2; i <= script_lastdata(st); i++ )
  524. {
  525. script_reportdata(script_getdata(st,i));
  526. }
  527. }
  528. else
  529. {
  530. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  531. }
  532. }
  533. /*==========================================
  534. * Output error message
  535. *------------------------------------------*/
  536. static void disp_error_message2(const char *mes,const char *pos,int report)
  537. {
  538. error_msg = aStrdup(mes);
  539. error_pos = pos;
  540. error_report = report;
  541. longjmp( error_jump, 1 );
  542. }
  543. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  544. static void disp_warning_message(const char *mes, const char *pos) {
  545. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  546. }
  547. /// Checks event parameter validity
  548. static void check_event(struct script_state *st, const char *evt)
  549. {
  550. if( evt && evt[0] && !stristr(evt, "::On") )
  551. {
  552. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  553. script_reportsrc(st);
  554. }
  555. }
  556. /*==========================================
  557. * Hashes the input string
  558. *------------------------------------------*/
  559. static unsigned int calc_hash(const char* p)
  560. {
  561. unsigned int h;
  562. #if defined(SCRIPT_HASH_DJB2)
  563. h = 5381;
  564. while( *p ) // hash*33 + c
  565. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  566. #elif defined(SCRIPT_HASH_SDBM)
  567. h = 0;
  568. while( *p ) // hash*65599 + c
  569. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  570. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  571. h = 0;
  572. while( *p ){
  573. unsigned int g;
  574. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  575. g = h & 0xF0000000;
  576. if( g )
  577. {
  578. h ^= g >> 24;
  579. h &= ~g;
  580. }
  581. }
  582. #else // athena hash
  583. h = 0;
  584. while( *p )
  585. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  586. #endif
  587. return h % SCRIPT_HASH_SIZE;
  588. }
  589. bool script_check_RegistryVariableLength(int pType, const char *val, size_t* vlen)
  590. {
  591. size_t len = strlen(val);
  592. if (vlen)
  593. *vlen = len;
  594. switch (pType) {
  595. case 0:
  596. return (len < 33); // key check
  597. case 1:
  598. return (len < 255); // value check
  599. default:
  600. return false;
  601. }
  602. }
  603. /*==========================================
  604. * str_data manipulation functions
  605. *------------------------------------------*/
  606. /// Looks up string using the provided id.
  607. const char* get_str(int id)
  608. {
  609. Assert( id >= LABEL_START && id < str_size );
  610. return str_buf+str_data[id].str;
  611. }
  612. /// Returns the uid of the string, or -1.
  613. static int search_str(const char* p)
  614. {
  615. int i;
  616. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  617. if( strcasecmp(get_str(i),p) == 0 )
  618. return i;
  619. return -1;
  620. }
  621. /// Stores a copy of the string and returns its id.
  622. /// If an identical string is already present, returns its id instead.
  623. int add_str(const char* p)
  624. {
  625. int h;
  626. int len;
  627. h = calc_hash(p);
  628. if( str_hash[h] == 0 )
  629. {// empty bucket, add new node here
  630. str_hash[h] = str_num;
  631. }
  632. else
  633. {// scan for end of list, or occurence of identical string
  634. int i;
  635. for( i = str_hash[h]; ; i = str_data[i].next )
  636. {
  637. if( strcasecmp(get_str(i),p) == 0 )
  638. return i; // string already in list
  639. if( str_data[i].next == 0 )
  640. break; // reached the end
  641. }
  642. // append node to end of list
  643. str_data[i].next = str_num;
  644. }
  645. // grow list if neccessary
  646. if( str_num >= str_data_size )
  647. {
  648. str_data_size += 128;
  649. RECREATE(str_data,struct str_data_struct,str_data_size);
  650. memset(str_data + (str_data_size - 128), '\0', 128);
  651. }
  652. len=(int)strlen(p);
  653. // grow string buffer if neccessary
  654. while( str_pos+len+1 >= str_size )
  655. {
  656. str_size += 256;
  657. RECREATE(str_buf,char,str_size);
  658. memset(str_buf + (str_size - 256), '\0', 256);
  659. }
  660. safestrncpy(str_buf+str_pos, p, len+1);
  661. str_data[str_num].type = C_NOP;
  662. str_data[str_num].str = str_pos;
  663. str_data[str_num].next = 0;
  664. str_data[str_num].func = NULL;
  665. str_data[str_num].backpatch = -1;
  666. str_data[str_num].label = -1;
  667. str_pos += len+1;
  668. return str_num++;
  669. }
  670. /// Appends 1 byte to the script buffer.
  671. static void add_scriptb(uint8 a)
  672. {
  673. if( script_pos+1 >= script_size )
  674. {
  675. script_size += SCRIPT_BLOCK_SIZE;
  676. RECREATE(script_buf,unsigned char,script_size);
  677. }
  678. script_buf[script_pos++] = a;
  679. }
  680. /// Appends a c_op value to the script buffer.
  681. /// The value is variable-length encoded into 8-bit blocks.
  682. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  683. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  684. static void add_scriptc(int a)
  685. {
  686. while( a >= 0x40 )
  687. {
  688. add_scriptb((a&0x3f)|0x40);
  689. a = (a - 0x40) >> 6;
  690. }
  691. add_scriptb(a);
  692. }
  693. /// Appends an integer value to the script buffer.
  694. /// The value is variable-length encoded into 8-bit blocks.
  695. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  696. /// All blocks hold 6 bits of data.
  697. static void add_scripti(int64 a){
  698. while( a > 0x3f ){
  699. add_scriptb((a & (int64)0x3f)|(int64)0xc0);
  700. a >>= 6;
  701. }
  702. add_scriptb(((a & (int64)0x3f)|(int64)0x80));
  703. }
  704. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  705. ///
  706. /// @param l The id of the str_data entry
  707. // Maximum up to 16M
  708. static void add_scriptl(int l)
  709. {
  710. int backpatch = str_data[l].backpatch;
  711. switch(str_data[l].type){
  712. case C_POS:
  713. case C_USERFUNC_POS:
  714. add_scriptc(C_POS);
  715. add_scriptb(str_data[l].label);
  716. add_scriptb(str_data[l].label>>8);
  717. add_scriptb(str_data[l].label>>16);
  718. break;
  719. case C_NOP:
  720. case C_USERFUNC:
  721. // Embedded data backpatch there is a possibility of label
  722. add_scriptc(C_NAME);
  723. str_data[l].backpatch = script_pos;
  724. add_scriptb(backpatch);
  725. add_scriptb(backpatch>>8);
  726. add_scriptb(backpatch>>16);
  727. break;
  728. case C_INT:
  729. add_scripti(std::abs(str_data[l].val));
  730. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  731. add_scriptc(C_NEG);
  732. break;
  733. default: // assume C_NAME
  734. add_scriptc(C_NAME);
  735. add_scriptb(l);
  736. add_scriptb(l>>8);
  737. add_scriptb(l>>16);
  738. break;
  739. }
  740. }
  741. /*==========================================
  742. * Resolve the label
  743. *------------------------------------------*/
  744. void set_label(int l,int pos, const char* script_pos_cur)
  745. {
  746. int i;
  747. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  748. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  749. disp_error_message("set_label: invalid label name",script_pos_cur);
  750. return;
  751. }
  752. if(str_data[l].label!=-1){
  753. disp_error_message("set_label: dup label ",script_pos_cur);
  754. return;
  755. }
  756. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  757. str_data[l].label=pos;
  758. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  759. int next=GETVALUE(script_buf,i);
  760. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  761. SETVALUE(script_buf,i,pos);
  762. i=next;
  763. }
  764. }
  765. /// Skips spaces and/or comments.
  766. const char* skip_space(const char* p)
  767. {
  768. if( p == NULL )
  769. return NULL;
  770. for(;;)
  771. {
  772. while( ISSPACE(*p) )
  773. ++p;
  774. if( *p == '/' && p[1] == '/' )
  775. {// line comment
  776. while(*p && *p!='\n')
  777. ++p;
  778. }
  779. else if( *p == '/' && p[1] == '*' )
  780. {// block comment
  781. p += 2;
  782. for(;;)
  783. {
  784. if( *p == '\0' ) {
  785. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p);
  786. return p;
  787. }
  788. if( *p == '*' && p[1] == '/' )
  789. {// end of block comment
  790. p += 2;
  791. break;
  792. }
  793. ++p;
  794. }
  795. }
  796. else
  797. break;
  798. }
  799. return p;
  800. }
  801. /// Skips a word.
  802. /// A word consists of undercores and/or alphanumeric characters,
  803. /// and valid variable prefixes/postfixes.
  804. static const char* skip_word(const char* p)
  805. {
  806. // prefix
  807. switch( *p ) {
  808. case '@':// temporary char variable
  809. ++p; break;
  810. case '#':// account variable
  811. p += ( p[1] == '#' ? 2 : 1 ); break;
  812. case '\'':// instance variable
  813. ++p; break;
  814. case '.':// npc variable
  815. p += ( p[1] == '@' ? 2 : 1 ); break;
  816. case '$':// global variable
  817. p += ( p[1] == '@' ? 2 : 1 ); break;
  818. }
  819. while( ISALNUM(*p) || *p == '_' )
  820. ++p;
  821. // postfix
  822. if( *p == '$' )// string
  823. p++;
  824. return p;
  825. }
  826. /// Adds a word to str_data.
  827. /// @see skip_word
  828. /// @see add_str
  829. static int add_word(const char* p)
  830. {
  831. char* word;
  832. int len;
  833. int i;
  834. // Check for a word
  835. len = skip_word(p) - p;
  836. if( len == 0 )
  837. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  838. // Duplicate the word
  839. word = (char*)aMalloc(len+1);
  840. memcpy(word, p, len);
  841. word[len] = 0;
  842. // add the word
  843. i = add_str(word);
  844. aFree(word);
  845. return i;
  846. }
  847. /// Parses a function call.
  848. /// The argument list can have parenthesis or not.
  849. /// The number of arguments is checked.
  850. static
  851. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  852. {
  853. const char* p2;
  854. const char* arg=NULL;
  855. int func;
  856. func = add_word(p);
  857. if( str_data[func].type == C_FUNC ){
  858. // buildin function
  859. add_scriptl(func);
  860. add_scriptc(C_ARG);
  861. arg = buildin_func[str_data[func].val].arg;
  862. #if defined(SCRIPT_COMMAND_DEPRECATION)
  863. if( str_data[func].deprecated ){
  864. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  865. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  866. }
  867. #endif
  868. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  869. // script defined function
  870. add_scriptl(buildin_callsub_ref);
  871. add_scriptc(C_ARG);
  872. add_scriptl(func);
  873. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  874. if( *arg == 0 )
  875. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  876. if( *arg != '*' )
  877. ++arg; // count func as argument
  878. } else {
  879. const char* name = get_str(func);
  880. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  881. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  882. } else {;
  883. add_scriptl(buildin_callfunc_ref);
  884. add_scriptc(C_ARG);
  885. add_scriptc(C_STR);
  886. while( *name ) add_scriptb(*name ++);
  887. add_scriptb(0);
  888. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  889. if( *arg != '*' ) ++ arg;
  890. }
  891. }
  892. p = skip_word(p);
  893. p = skip_space(p);
  894. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  895. syntax.curly[syntax.curly_count].count = 0;
  896. if( *p == ';' )
  897. {// <func name> ';'
  898. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  899. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  900. {// <func name> '(' ')'
  901. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  902. p = p2;
  903. /*
  904. } else if( 0 && require_paren && *p != '(' )
  905. {// <func name>
  906. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  907. */
  908. } else
  909. {// <func name> <arg list>
  910. if( require_paren ){
  911. if( *p != '(' )
  912. disp_error_message("need '('",p);
  913. ++p; // skip '('
  914. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  915. } else if( *p == '(' ){
  916. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  917. } else {
  918. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  919. }
  920. ++syntax.curly_count;
  921. while( *arg ) {
  922. p2=parse_subexpr(p,-1);
  923. if( p == p2 )
  924. break; // not an argument
  925. if( *arg != '*' )
  926. ++arg; // next argument
  927. p=skip_space(p2);
  928. if( *arg == 0 || *p != ',' )
  929. break; // no more arguments
  930. ++p; // skip comma
  931. }
  932. --syntax.curly_count;
  933. }
  934. if( *arg && *arg != '?' && *arg != '*' )
  935. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  936. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  937. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  938. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  939. if( *p != ')' )
  940. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  941. ++p;
  942. }
  943. add_scriptc(C_FUNC);
  944. return p;
  945. }
  946. /// Processes end of logical script line.
  947. /// @param first When true, only fix up scheduling data is initialized
  948. /// @param p Script position for error reporting in set_label
  949. static void parse_nextline(bool first, const char* p)
  950. {
  951. if( !first )
  952. {
  953. add_scriptc(C_EOL); // mark end of line for stack cleanup
  954. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  955. }
  956. // initialize data for new '-' label fix up scheduling
  957. str_data[LABEL_NEXTLINE].type = C_NOP;
  958. str_data[LABEL_NEXTLINE].backpatch = -1;
  959. str_data[LABEL_NEXTLINE].label = -1;
  960. }
  961. /**
  962. * Pushes a variable into stack, processing its array index if needed.
  963. * @see parse_variable
  964. */
  965. void parse_variable_sub_push(int word, const char *p2)
  966. {
  967. if( p2 ) { // Process the variable index
  968. const char *p3 = NULL;
  969. // Push the getelementofarray method into the stack
  970. add_scriptl(buildin_getelementofarray_ref);
  971. add_scriptc(C_ARG);
  972. add_scriptl(word);
  973. // Process the sub-expression for this assignment
  974. p3 = parse_subexpr(p2 + 1, 1);
  975. p3 = skip_space(p3);
  976. if( *p3 != ']' ) // Closing parenthesis is required for this script
  977. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  978. // Push the closing function stack operator onto the stack
  979. add_scriptc(C_FUNC);
  980. p3++;
  981. } else // No array index, simply push the variable or value onto the stack
  982. add_scriptl(word);
  983. }
  984. /// Parse a variable assignment using the direct equals operator
  985. /// @param p script position where the function should run from
  986. /// @return NULL if not a variable assignment, the new position otherwise
  987. const char* parse_variable(const char* p) {
  988. int word;
  989. c_op type = C_NOP;
  990. const char *p2 = NULL;
  991. const char *var = p;
  992. if( p[0] == '+' && p[1] == '+' ){
  993. type = C_ADD_PRE; // pre ++
  994. }else if( p[0] == '-' && p[1] == '-' ){
  995. type = C_SUB_PRE; // pre --
  996. }
  997. if( type != C_NOP ){
  998. var = p = skip_space(&p[2]);
  999. }
  1000. // skip the variable where applicable
  1001. p = skip_word(p);
  1002. p = skip_space(p);
  1003. if( p == NULL ) {// end of the line or invalid buffer
  1004. return NULL;
  1005. }
  1006. if( *p == '[' ) {// array variable so process the array as appropriate
  1007. int i,j;
  1008. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1009. if( *p ++ == ']' && --(j) == 0 ) break;
  1010. if( *p == '[' ) ++ j;
  1011. }
  1012. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1013. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1014. }
  1015. }
  1016. if( type == C_NOP ){
  1017. if( p[0] == '=' && p[1] != '=' ){
  1018. type = C_EQ; // =
  1019. }else if( p[0] == '+' && p[1] == '=' ){
  1020. type = C_ADD; // +=
  1021. }else if( p[0] == '-' && p[1] == '=' ){
  1022. type = C_SUB; // -=
  1023. }else if( p[0] == '^' && p[1] == '=' ){
  1024. type = C_XOR; // ^=
  1025. }else if( p[0] == '|' && p[1] == '=' ){
  1026. type = C_OR; // |=
  1027. }else if( p[0] == '&' && p[1] == '=' ){
  1028. type = C_AND; // &=
  1029. }else if( p[0] == '*' && p[1] == '=' ){
  1030. type = C_MUL; // *=
  1031. }else if( p[0] == '/' && p[1] == '=' ){
  1032. type = C_DIV; // /=
  1033. }else if( p[0] == '%' && p[1] == '=' ){
  1034. type = C_MOD; // %=
  1035. }else if( p[0] == '~' && p[1] == '=' ){
  1036. type = C_NOT; // ~=
  1037. }else if( p[0] == '+' && p[1] == '+' ){
  1038. type = C_ADD_POST; // post ++
  1039. }else if( p[0] == '-' && p[1] == '-' ){
  1040. type = C_SUB_POST; // post --
  1041. }else if( p[0] == '<' && p[1] == '<' && p[2] == '=' ){
  1042. type = C_L_SHIFT; // <<=
  1043. }else if( p[0] == '>' && p[1] == '>' && p[2] == '=' ){
  1044. type = C_R_SHIFT; // >>=
  1045. }else{
  1046. // failed to find a matching operator combination so invalid
  1047. return nullptr;
  1048. }
  1049. }
  1050. switch( type ) {
  1051. case C_ADD_PRE: // pre ++
  1052. case C_SUB_PRE: // pre --
  1053. // Nothing more to skip
  1054. break;
  1055. case C_EQ: {// incremental modifier
  1056. p = skip_space( &p[1] );
  1057. }
  1058. break;
  1059. case C_L_SHIFT:
  1060. case C_R_SHIFT: {// left or right shift modifier
  1061. p = skip_space( &p[3] );
  1062. }
  1063. break;
  1064. default: {// normal incremental command
  1065. p = skip_space( &p[2] );
  1066. }
  1067. }
  1068. if( p == NULL ) {// end of line or invalid buffer
  1069. return NULL;
  1070. }
  1071. // push the set function onto the stack
  1072. add_scriptl(buildin_set_ref);
  1073. add_scriptc(C_ARG);
  1074. // always append parenthesis to avoid errors
  1075. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1076. syntax.curly[syntax.curly_count].count = 0;
  1077. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1078. // increment the total curly count for the position in the script
  1079. ++ syntax.curly_count;
  1080. // parse the variable currently being modified
  1081. word = add_word(var);
  1082. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS ) // cannot assign a variable which exists as a function or label
  1083. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1084. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1085. if( type != C_EQ )
  1086. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1087. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1088. add_scripti(1);
  1089. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1090. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1091. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1092. add_scripti(1);
  1093. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1094. } else { // Process the value as an expression
  1095. p = parse_subexpr(p, -1);
  1096. if( type != C_EQ )
  1097. {// push the type of modifier onto the stack
  1098. add_scriptc(type);
  1099. }
  1100. }
  1101. // decrement the curly count for the position within the script
  1102. -- syntax.curly_count;
  1103. // close the script by appending the function operator
  1104. add_scriptc(C_FUNC);
  1105. // push the buffer from the method
  1106. return p;
  1107. }
  1108. /*
  1109. * Checks whether the gives string is a number literal
  1110. *
  1111. * Mainly necessary to differentiate between number literals and NPC name
  1112. * constants, since several of those start with a digit.
  1113. *
  1114. * All this does is to check if the string begins with an optional + or - sign,
  1115. * followed by a hexadecimal or decimal number literal literal and is NOT
  1116. * followed by a underscore or letter.
  1117. *
  1118. * @author : Hercules.ws
  1119. * @param p Pointer to the string to check
  1120. * @return Whether the string is a number literal
  1121. */
  1122. bool is_number(const char *p) {
  1123. const char *np;
  1124. if (!p)
  1125. return false;
  1126. if (*p == '-' || *p == '+')
  1127. p++;
  1128. np = p;
  1129. if (*p == '0' && p[1] == 'x') {
  1130. p+=2;
  1131. np = p;
  1132. // Hexadecimal
  1133. while (ISXDIGIT(*np))
  1134. np++;
  1135. } else {
  1136. // Decimal
  1137. while (ISDIGIT(*np))
  1138. np++;
  1139. }
  1140. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1141. return true;
  1142. return false;
  1143. }
  1144. /*==========================================
  1145. * Analysis section
  1146. *------------------------------------------*/
  1147. const char* parse_simpleexpr(const char *p)
  1148. {
  1149. int64 i;
  1150. p=skip_space(p);
  1151. if(*p==';' || *p==',')
  1152. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1153. if(*p=='('){
  1154. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1155. ++syntax.curly[i].count;
  1156. p=parse_subexpr(p+1,-1);
  1157. p=skip_space(p);
  1158. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1159. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1160. ){
  1161. if( *p == ',' ){
  1162. syntax.curly[i].flag = ARGLIST_PAREN;
  1163. return p;
  1164. } else
  1165. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1166. }
  1167. if( *p != ')' )
  1168. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1169. ++p;
  1170. } else if(is_number(p)) {
  1171. char *np;
  1172. bool error;
  1173. // Skip leading zeroes
  1174. while(*p == '0' && ISDIGIT(p[1])) p++;
  1175. errno = 0;
  1176. i=strtoll(p,&np,0);
  1177. error = (errno == ERANGE);
  1178. if( i < SCRIPT_INT_MIN || ( error && i == INT64_MIN ) ){
  1179. i = SCRIPT_INT_MIN;
  1180. disp_warning_message( "parse_simpleexpr: underflow detected, capping value to SCRIPT_INT_MIN", p );
  1181. }else if( i > SCRIPT_INT_MAX || ( error && i == INT64_MAX ) ){
  1182. i = SCRIPT_INT_MAX;
  1183. disp_warning_message( "parse_simpleexpr: overflow detected, capping value to SCRIPT_INT_MAX", p );
  1184. }
  1185. add_scripti(i);
  1186. p=np;
  1187. } else if(*p=='"'){
  1188. add_scriptc(C_STR);
  1189. p++;
  1190. while( *p && *p != '"' ){
  1191. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1192. {
  1193. char buf[8];
  1194. size_t len = skip_escaped_c(p) - p;
  1195. size_t n = sv_unescape_c(buf, p, len);
  1196. if( n != 1 )
  1197. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1198. p += len;
  1199. add_scriptb(*buf);
  1200. continue;
  1201. }
  1202. else if( *p == '\n' )
  1203. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1204. add_scriptb(*p++);
  1205. }
  1206. if(!*p)
  1207. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1208. add_scriptb(0);
  1209. p++; //'"'
  1210. } else {
  1211. int l;
  1212. const char* pv;
  1213. // label , register , function etc
  1214. if(skip_word(p)==p)
  1215. disp_error_message("parse_simpleexpr: unexpected character",p);
  1216. l=add_word(p);
  1217. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1218. return parse_callfunc(p,1,0);
  1219. else {
  1220. const char* name = get_str(l);
  1221. if( strdb_get(userfunc_db,name) != NULL ) {
  1222. return parse_callfunc(p,1,1);
  1223. }
  1224. }
  1225. if( (pv = parse_variable(p)) )
  1226. {// successfully processed a variable assignment
  1227. return pv;
  1228. }
  1229. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1230. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1231. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1232. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1233. if (str_data[l].name)
  1234. ShowWarning( "Please use '%s' instead!\n", str_data[l].name );
  1235. disp_warning_message("parse_simpleexpr: deprecated constant", p);
  1236. }
  1237. #endif
  1238. p=skip_word(p);
  1239. if( *p == '[' ){
  1240. // array(name[i] => getelementofarray(name,i) )
  1241. add_scriptl(buildin_getelementofarray_ref);
  1242. add_scriptc(C_ARG);
  1243. add_scriptl(l);
  1244. p=parse_subexpr(p+1,-1);
  1245. p=skip_space(p);
  1246. if( *p != ']' )
  1247. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1248. ++p;
  1249. add_scriptc(C_FUNC);
  1250. }else
  1251. add_scriptl(l);
  1252. }
  1253. return p;
  1254. }
  1255. /*==========================================
  1256. * Analysis of the expression
  1257. *------------------------------------------*/
  1258. const char* parse_subexpr(const char* p,int limit)
  1259. {
  1260. int op,opl,len;
  1261. p=skip_space(p);
  1262. if( *p == '-' ){
  1263. const char* tmpp = skip_space(p+1);
  1264. if( *tmpp == ';' || *tmpp == ',' ){
  1265. add_scriptl(LABEL_NEXTLINE);
  1266. p++;
  1267. return p;
  1268. }
  1269. }
  1270. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1271. p = parse_variable(p);
  1272. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1273. p = parse_subexpr(p + 1, 11);
  1274. add_scriptc(op);
  1275. } else
  1276. p = parse_simpleexpr(p);
  1277. p = skip_space(p);
  1278. while((
  1279. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1280. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1281. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1282. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1283. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1284. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1285. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1286. (op=C_AND,opl=5,len=1,*p=='&') ||
  1287. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1288. (op=C_OR,opl=3,len=1,*p=='|') ||
  1289. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1290. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1291. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1292. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1293. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1294. (op=C_GT,opl=7,len=1,*p=='>') ||
  1295. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1296. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1297. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1298. p+=len;
  1299. if(op == C_OP3) {
  1300. p=parse_subexpr(p,-1);
  1301. p=skip_space(p);
  1302. if( *(p++) != ':')
  1303. disp_error_message("parse_subexpr: expected ':'", p-1);
  1304. p=parse_subexpr(p,-1);
  1305. } else {
  1306. p=parse_subexpr(p,opl);
  1307. }
  1308. add_scriptc(op);
  1309. p=skip_space(p);
  1310. }
  1311. return p; /* return first untreated operator */
  1312. }
  1313. /*==========================================
  1314. * Evaluation of the expression
  1315. *------------------------------------------*/
  1316. const char* parse_expr(const char *p)
  1317. {
  1318. switch(*p){
  1319. case ')': case ';': case ':': case '[': case ']':
  1320. case '}':
  1321. disp_error_message("parse_expr: unexpected character",p);
  1322. }
  1323. p=parse_subexpr(p,-1);
  1324. return p;
  1325. }
  1326. /*==========================================
  1327. * Analysis of the line
  1328. *------------------------------------------*/
  1329. const char* parse_line(const char* p)
  1330. {
  1331. const char* p2;
  1332. p=skip_space(p);
  1333. if(*p==';') {
  1334. //Close decision for if(); for(); while();
  1335. p = parse_syntax_close(p + 1);
  1336. return p;
  1337. }
  1338. if(*p==')' && parse_syntax_for_flag)
  1339. return p+1;
  1340. p = skip_space(p);
  1341. if(p[0] == '{') {
  1342. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1343. syntax.curly[syntax.curly_count].count = -1;
  1344. syntax.curly[syntax.curly_count].index = -1;
  1345. syntax.curly_count++;
  1346. return p + 1;
  1347. } else if(p[0] == '}') {
  1348. return parse_curly_close(p);
  1349. }
  1350. // Syntax-related processing
  1351. p2 = parse_syntax(p);
  1352. if(p2 != NULL)
  1353. return p2;
  1354. // attempt to process a variable assignment
  1355. p2 = parse_variable(p);
  1356. if( p2 != NULL )
  1357. {// variable assignment processed so leave the method
  1358. return parse_syntax_close(p2 + 1);
  1359. }
  1360. p = parse_callfunc(p,0,0);
  1361. p = skip_space(p);
  1362. if(parse_syntax_for_flag) {
  1363. if( *p != ')' )
  1364. disp_error_message("parse_line: expected ')'",p);
  1365. } else {
  1366. if( *p != ';' )
  1367. disp_error_message("parse_line: expected ';'",p);
  1368. }
  1369. //Binding decision for if(), for(), while()
  1370. p = parse_syntax_close(p+1);
  1371. return p;
  1372. }
  1373. // { ... } Closing process
  1374. const char* parse_curly_close(const char* p)
  1375. {
  1376. if(syntax.curly_count <= 0) {
  1377. disp_error_message("parse_curly_close: unexpected string",p);
  1378. return p + 1;
  1379. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1380. syntax.curly_count--;
  1381. //Close decision if, for , while
  1382. p = parse_syntax_close(p + 1);
  1383. return p;
  1384. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1385. //Closing switch()
  1386. int pos = syntax.curly_count-1;
  1387. char label[256];
  1388. int l;
  1389. // Remove temporary variables
  1390. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1391. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1392. parse_line(label);
  1393. syntax.curly_count--;
  1394. // Go to the end pointer unconditionally
  1395. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1396. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1397. parse_line(label);
  1398. syntax.curly_count--;
  1399. // You are here labeled
  1400. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1401. l=add_str(label);
  1402. set_label(l,script_pos, p);
  1403. if(syntax.curly[pos].flag) {
  1404. //Exists default
  1405. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1406. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1407. parse_line(label);
  1408. syntax.curly_count--;
  1409. }
  1410. // Label end
  1411. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1412. l=add_str(label);
  1413. set_label(l,script_pos, p);
  1414. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1415. syntax.curly_count--;
  1416. //Closing decision if, for , while
  1417. p = parse_syntax_close(p + 1);
  1418. return p;
  1419. } else {
  1420. disp_error_message("parse_curly_close: unexpected string",p);
  1421. return p + 1;
  1422. }
  1423. }
  1424. // Syntax-related processing
  1425. // break, case, continue, default, do, for, function,
  1426. // if, switch, while ? will handle this internally.
  1427. const char* parse_syntax(const char* p)
  1428. {
  1429. const char *p2 = skip_word(p);
  1430. switch(*p) {
  1431. case 'B':
  1432. case 'b':
  1433. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1434. // break Processing
  1435. char label[256];
  1436. int pos = syntax.curly_count - 1;
  1437. while(pos >= 0) {
  1438. if(syntax.curly[pos].type == TYPE_DO) {
  1439. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1440. break;
  1441. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1442. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1443. break;
  1444. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1445. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1446. break;
  1447. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1448. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1449. break;
  1450. }
  1451. pos--;
  1452. }
  1453. if(pos < 0) {
  1454. disp_error_message("parse_syntax: unexpected 'break'",p);
  1455. } else {
  1456. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1457. parse_line(label);
  1458. syntax.curly_count--;
  1459. }
  1460. p = skip_space(p2);
  1461. if(*p != ';')
  1462. disp_error_message("parse_syntax: expected ';'",p);
  1463. // Closing decision if, for , while
  1464. p = parse_syntax_close(p + 1);
  1465. return p;
  1466. }
  1467. break;
  1468. case 'c':
  1469. case 'C':
  1470. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1471. //Processing case
  1472. int pos = syntax.curly_count-1;
  1473. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1474. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1475. return p+1;
  1476. } else {
  1477. char label[256];
  1478. int l;
  1479. int64 v;
  1480. char *np;
  1481. if(syntax.curly[pos].count != 1) {
  1482. //Jump for FALLTHRU
  1483. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1484. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1485. parse_line(label);
  1486. syntax.curly_count--;
  1487. // You are here labeled
  1488. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1489. l=add_str(label);
  1490. set_label(l,script_pos, p);
  1491. }
  1492. //Decision statement switch
  1493. p = skip_space(p2);
  1494. if(p == p2) {
  1495. disp_error_message("parse_syntax: expected a space ' '",p);
  1496. }
  1497. // check whether case label is integer or not
  1498. if(is_number(p)) {
  1499. //Numeric value
  1500. v = (int)strtol(p,&np,0);
  1501. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1502. p++;
  1503. p = skip_word(p);
  1504. if(np != p)
  1505. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1506. } else {
  1507. //Check for constants
  1508. p2 = skip_word(p);
  1509. v = (int)(size_t) (p2-p); // length of word at p2
  1510. memcpy(label,p,static_cast<int>(v));
  1511. label[v]='\0';
  1512. if( !script_get_constant(label, &v) )
  1513. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1514. p = skip_word(p);
  1515. }
  1516. p = skip_space(p);
  1517. if(*p != ':')
  1518. disp_error_message("parse_syntax: expect ':'",p);
  1519. sprintf(label,"if(%" PRId64 " != $@__SW%x_VAL) goto __SW%x_%x;",
  1520. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1521. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1522. // Bad I do not parse twice
  1523. p2 = parse_line(label);
  1524. parse_line(p2);
  1525. syntax.curly_count--;
  1526. if(syntax.curly[pos].count != 1) {
  1527. // Label after the completion of FALLTHRU
  1528. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1529. l=add_str(label);
  1530. set_label(l,script_pos,p);
  1531. }
  1532. // check duplication of case label [Rayce]
  1533. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1534. disp_error_message("parse_syntax: dup 'case'",p);
  1535. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1536. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1537. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1538. parse_line(label);
  1539. syntax.curly_count--;
  1540. syntax.curly[pos].count++;
  1541. }
  1542. return p + 1;
  1543. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1544. // Processing continue
  1545. char label[256];
  1546. int pos = syntax.curly_count - 1;
  1547. while(pos >= 0) {
  1548. if(syntax.curly[pos].type == TYPE_DO) {
  1549. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1550. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1551. break;
  1552. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1553. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1554. break;
  1555. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1556. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1557. break;
  1558. }
  1559. pos--;
  1560. }
  1561. if(pos < 0) {
  1562. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1563. } else {
  1564. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1565. parse_line(label);
  1566. syntax.curly_count--;
  1567. }
  1568. p = skip_space(p2);
  1569. if(*p != ';')
  1570. disp_error_message("parse_syntax: expected ';'",p);
  1571. //Closing decision if, for , while
  1572. p = parse_syntax_close(p + 1);
  1573. return p;
  1574. }
  1575. break;
  1576. case 'd':
  1577. case 'D':
  1578. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1579. // Switch - default processing
  1580. int pos = syntax.curly_count-1;
  1581. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1582. disp_error_message("parse_syntax: unexpected 'default'",p);
  1583. } else if(syntax.curly[pos].flag) {
  1584. disp_error_message("parse_syntax: dup 'default'",p);
  1585. } else {
  1586. char label[256];
  1587. int l;
  1588. // Put the label location
  1589. p = skip_space(p2);
  1590. if(*p != ':') {
  1591. disp_error_message("parse_syntax: expected ':'",p);
  1592. }
  1593. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1594. l=add_str(label);
  1595. set_label(l,script_pos,p);
  1596. // Skip to the next link w/o condition
  1597. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1598. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1599. parse_line(label);
  1600. syntax.curly_count--;
  1601. // The default label
  1602. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1603. l=add_str(label);
  1604. set_label(l,script_pos,p);
  1605. syntax.curly[syntax.curly_count - 1].flag = 1;
  1606. syntax.curly[pos].count++;
  1607. }
  1608. return p + 1;
  1609. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1610. int l;
  1611. char label[256];
  1612. p=skip_space(p2);
  1613. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1614. syntax.curly[syntax.curly_count].count = 1;
  1615. syntax.curly[syntax.curly_count].index = syntax.index++;
  1616. syntax.curly[syntax.curly_count].flag = 0;
  1617. // Label of the (do) form here
  1618. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1619. l=add_str(label);
  1620. set_label(l,script_pos,p);
  1621. syntax.curly_count++;
  1622. return p;
  1623. }
  1624. break;
  1625. case 'f':
  1626. case 'F':
  1627. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1628. int l;
  1629. char label[256];
  1630. int pos = syntax.curly_count;
  1631. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1632. syntax.curly[syntax.curly_count].count = 1;
  1633. syntax.curly[syntax.curly_count].index = syntax.index++;
  1634. syntax.curly[syntax.curly_count].flag = 0;
  1635. syntax.curly_count++;
  1636. p=skip_space(p2);
  1637. if(*p != '(')
  1638. disp_error_message("parse_syntax: expected '('",p);
  1639. p++;
  1640. // Execute the initialization statement
  1641. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1642. p=parse_line(p);
  1643. syntax.curly_count--;
  1644. // Form the start of label decision
  1645. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1646. l=add_str(label);
  1647. set_label(l,script_pos,p);
  1648. p=skip_space(p);
  1649. if(*p == ';') {
  1650. // For (; Because the pattern of always true ;)
  1651. ;
  1652. } else {
  1653. // Skip to the end point if the condition is false
  1654. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1655. add_scriptl(add_str("jump_zero"));
  1656. add_scriptc(C_ARG);
  1657. p=parse_expr(p);
  1658. p=skip_space(p);
  1659. add_scriptl(add_str(label));
  1660. add_scriptc(C_FUNC);
  1661. }
  1662. if(*p != ';')
  1663. disp_error_message("parse_syntax: expected ';'",p);
  1664. p++;
  1665. // Skip to the beginning of the loop
  1666. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1667. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1668. parse_line(label);
  1669. syntax.curly_count--;
  1670. // Labels to form the next loop
  1671. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1672. l=add_str(label);
  1673. set_label(l,script_pos,p);
  1674. // Process the next time you enter the loop
  1675. // A ')' last for; flag to be treated as'
  1676. parse_syntax_for_flag = 1;
  1677. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1678. p=parse_line(p);
  1679. syntax.curly_count--;
  1680. parse_syntax_for_flag = 0;
  1681. // Skip to the determination process conditions
  1682. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1683. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1684. parse_line(label);
  1685. syntax.curly_count--;
  1686. // Loop start labeling
  1687. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1688. l=add_str(label);
  1689. set_label(l,script_pos,p);
  1690. return p;
  1691. }
  1692. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1693. {// internal script function
  1694. const char *func_name;
  1695. func_name = skip_space(p2);
  1696. p = skip_word(func_name);
  1697. if( p == func_name )
  1698. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1699. p2 = skip_space(p);
  1700. if( *p2 == ';' )
  1701. {// function <name> ;
  1702. // function declaration - just register the name
  1703. int l;
  1704. l = add_word(func_name);
  1705. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1706. str_data[l].type = C_USERFUNC;
  1707. else if( str_data[l].type == C_USERFUNC )
  1708. ; // already registered
  1709. else
  1710. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1711. // Close condition of if, for, while
  1712. p = parse_syntax_close(p2 + 1);
  1713. return p;
  1714. }
  1715. else if(*p2 == '{')
  1716. {// function <name> <line/block of code>
  1717. char label[256];
  1718. int l;
  1719. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1720. syntax.curly[syntax.curly_count].count = 1;
  1721. syntax.curly[syntax.curly_count].index = syntax.index++;
  1722. syntax.curly[syntax.curly_count].flag = 0;
  1723. ++syntax.curly_count;
  1724. // Jump over the function code
  1725. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1726. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1727. ++syntax.curly_count;
  1728. parse_line(label);
  1729. --syntax.curly_count;
  1730. // Set the position of the function (label)
  1731. l=add_word(func_name);
  1732. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1733. {
  1734. str_data[l].type = C_USERFUNC;
  1735. set_label(l, script_pos, p);
  1736. if( parse_options&SCRIPT_USE_LABEL_DB )
  1737. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1738. }
  1739. else
  1740. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1741. return skip_space(p);
  1742. }
  1743. else
  1744. {
  1745. disp_error_message("expect ';' or '{' at function syntax",p);
  1746. }
  1747. }
  1748. break;
  1749. case 'i':
  1750. case 'I':
  1751. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1752. // If process
  1753. char label[256];
  1754. p=skip_space(p2);
  1755. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1756. disp_error_message("need '('",p);
  1757. }
  1758. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1759. syntax.curly[syntax.curly_count].count = 1;
  1760. syntax.curly[syntax.curly_count].index = syntax.index++;
  1761. syntax.curly[syntax.curly_count].flag = 0;
  1762. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1763. syntax.curly_count++;
  1764. add_scriptl(add_str("jump_zero"));
  1765. add_scriptc(C_ARG);
  1766. p=parse_expr(p);
  1767. p=skip_space(p);
  1768. add_scriptl(add_str(label));
  1769. add_scriptc(C_FUNC);
  1770. return p;
  1771. }
  1772. break;
  1773. case 's':
  1774. case 'S':
  1775. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1776. // Processing of switch ()
  1777. char label[256];
  1778. p=skip_space(p2);
  1779. if(*p != '(') {
  1780. disp_error_message("need '('",p);
  1781. }
  1782. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1783. syntax.curly[syntax.curly_count].count = 1;
  1784. syntax.curly[syntax.curly_count].index = syntax.index++;
  1785. syntax.curly[syntax.curly_count].flag = 0;
  1786. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1787. syntax.curly_count++;
  1788. add_scriptl(add_str("set"));
  1789. add_scriptc(C_ARG);
  1790. add_scriptl(add_str(label));
  1791. p=parse_expr(p);
  1792. p=skip_space(p);
  1793. if(*p != '{') {
  1794. disp_error_message("parse_syntax: expected '{'",p);
  1795. }
  1796. add_scriptc(C_FUNC);
  1797. return p + 1;
  1798. }
  1799. break;
  1800. case 'w':
  1801. case 'W':
  1802. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1803. int l;
  1804. char label[256];
  1805. p=skip_space(p2);
  1806. if(*p != '(') {
  1807. disp_error_message("need '('",p);
  1808. }
  1809. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1810. syntax.curly[syntax.curly_count].count = 1;
  1811. syntax.curly[syntax.curly_count].index = syntax.index++;
  1812. syntax.curly[syntax.curly_count].flag = 0;
  1813. // Form the start of label decision
  1814. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1815. l=add_str(label);
  1816. set_label(l,script_pos,p);
  1817. // Skip to the end point if the condition is false
  1818. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1819. syntax.curly_count++;
  1820. add_scriptl(add_str("jump_zero"));
  1821. add_scriptc(C_ARG);
  1822. p=parse_expr(p);
  1823. p=skip_space(p);
  1824. add_scriptl(add_str(label));
  1825. add_scriptc(C_FUNC);
  1826. return p;
  1827. }
  1828. break;
  1829. }
  1830. return NULL;
  1831. }
  1832. const char* parse_syntax_close(const char *p) {
  1833. // If (...) for (...) hoge (); as to make sure closed closed once again
  1834. int flag;
  1835. do {
  1836. p = parse_syntax_close_sub(p,&flag);
  1837. } while(flag);
  1838. return p;
  1839. }
  1840. // Close judgment if, for, while, of do
  1841. // flag == 1 : closed
  1842. // flag == 0 : not closed
  1843. const char* parse_syntax_close_sub(const char* p,int* flag)
  1844. {
  1845. char label[256];
  1846. int pos = syntax.curly_count - 1;
  1847. int l;
  1848. *flag = 1;
  1849. if(syntax.curly_count <= 0) {
  1850. *flag = 0;
  1851. return p;
  1852. } else if(syntax.curly[pos].type == TYPE_IF) {
  1853. const char *bp = p;
  1854. const char *p2;
  1855. // if-block and else-block end is a new line
  1856. parse_nextline(false, p);
  1857. // Skip to the last location if
  1858. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1859. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1860. parse_line(label);
  1861. syntax.curly_count--;
  1862. // Put the label of the location
  1863. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1864. l=add_str(label);
  1865. set_label(l,script_pos,p);
  1866. syntax.curly[pos].count++;
  1867. p = skip_space(p);
  1868. p2 = skip_word(p);
  1869. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1870. // else or else - if
  1871. p = skip_space(p2);
  1872. p2 = skip_word(p);
  1873. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1874. // else - if
  1875. p=skip_space(p2);
  1876. if(*p != '(') {
  1877. disp_error_message("need '('",p);
  1878. }
  1879. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1880. add_scriptl(add_str("jump_zero"));
  1881. add_scriptc(C_ARG);
  1882. p=parse_expr(p);
  1883. p=skip_space(p);
  1884. add_scriptl(add_str(label));
  1885. add_scriptc(C_FUNC);
  1886. *flag = 0;
  1887. return p;
  1888. } else {
  1889. // else
  1890. if(!syntax.curly[pos].flag) {
  1891. syntax.curly[pos].flag = 1;
  1892. *flag = 0;
  1893. return p;
  1894. }
  1895. }
  1896. }
  1897. // Close if
  1898. syntax.curly_count--;
  1899. // Put the label of the final location
  1900. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1901. l=add_str(label);
  1902. set_label(l,script_pos,p);
  1903. if(syntax.curly[pos].flag == 1) {
  1904. // Because the position of the pointer is the same if not else for this
  1905. return bp;
  1906. }
  1907. return p;
  1908. } else if(syntax.curly[pos].type == TYPE_DO) {
  1909. int l2;
  1910. char label2[256];
  1911. const char *p2;
  1912. if(syntax.curly[pos].flag) {
  1913. // (Come here continue) to form the label here
  1914. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1915. l2=add_str(label2);
  1916. set_label(l2,script_pos,p);
  1917. }
  1918. // Skip to the end point if the condition is false
  1919. p = skip_space(p);
  1920. p2 = skip_word(p);
  1921. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1922. disp_error_message("parse_syntax: expected 'while'",p);
  1923. p = skip_space(p2);
  1924. if(*p != '(') {
  1925. disp_error_message("need '('",p);
  1926. }
  1927. // do-block end is a new line
  1928. parse_nextline(false, p);
  1929. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1930. add_scriptl(add_str("jump_zero"));
  1931. add_scriptc(C_ARG);
  1932. p=parse_expr(p);
  1933. p=skip_space(p);
  1934. add_scriptl(add_str(label2));
  1935. add_scriptc(C_FUNC);
  1936. // Skip to the starting point
  1937. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1938. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1939. parse_line(label2);
  1940. syntax.curly_count--;
  1941. // Form label of the end point conditions
  1942. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1943. l2=add_str(label2);
  1944. set_label(l2,script_pos,p);
  1945. p = skip_space(p);
  1946. if(*p != ';') {
  1947. disp_error_message("parse_syntax: expected ';'",p);
  1948. return p+1;
  1949. }
  1950. p++;
  1951. syntax.curly_count--;
  1952. return p;
  1953. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1954. // for-block end is a new line
  1955. parse_nextline(false, p);
  1956. // Skip to the next loop
  1957. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1958. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1959. parse_line(label);
  1960. syntax.curly_count--;
  1961. // End for labeling
  1962. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1963. l=add_str(label);
  1964. set_label(l,script_pos,p);
  1965. syntax.curly_count--;
  1966. return p;
  1967. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1968. // while-block end is a new line
  1969. parse_nextline(false, p);
  1970. // Skip to the decision while
  1971. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1972. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1973. parse_line(label);
  1974. syntax.curly_count--;
  1975. // End while labeling
  1976. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1977. l=add_str(label);
  1978. set_label(l,script_pos,p);
  1979. syntax.curly_count--;
  1980. return p;
  1981. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1982. int pos2 = syntax.curly_count-1;
  1983. char label2[256];
  1984. int l2;
  1985. // Back
  1986. sprintf(label2,"return;");
  1987. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1988. parse_line(label2);
  1989. syntax.curly_count--;
  1990. // Put the label of the location
  1991. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1992. l2=add_str(label2);
  1993. set_label(l2,script_pos,p);
  1994. syntax.curly_count--;
  1995. return p;
  1996. } else {
  1997. *flag = 0;
  1998. return p;
  1999. }
  2000. }
  2001. /*==========================================
  2002. * Added built-in functions
  2003. *------------------------------------------*/
  2004. static void add_buildin_func(void)
  2005. {
  2006. int i;
  2007. for( i = 0; buildin_func[i].func; i++ )
  2008. {
  2009. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2010. // 'v' - value (either string or int or reference)
  2011. // 's' - string
  2012. // 'i' - int
  2013. // 'r' - reference (of a variable)
  2014. // 'l' - label
  2015. // '?' - one optional parameter
  2016. // '*' - unknown number of optional parameters
  2017. const char* p = buildin_func[i].arg;
  2018. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2019. while( *p == '?' ) ++p;
  2020. if( *p == '*' ) ++p;
  2021. if( *p != 0){
  2022. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2023. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2024. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2025. } else {
  2026. int n = add_str(buildin_func[i].name);
  2027. str_data[n].type = C_FUNC;
  2028. str_data[n].val = i;
  2029. str_data[n].func = buildin_func[i].func;
  2030. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  2031. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2032. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2033. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2034. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2035. }
  2036. }
  2037. }
  2038. /**
  2039. * String comparison with a char array to a script constant
  2040. * @param prefix: Char array to compare
  2041. * @param value: Script constant
  2042. * @return Constant name as char array or NULL otherwise
  2043. */
  2044. const char* script_get_constant_str( const char* prefix, int64 value ){
  2045. const char* name;
  2046. for(int i = 0; i < str_data_size; i++ ){
  2047. // Check if it is a constant
  2048. if( str_data[i].type != C_INT ){
  2049. continue;
  2050. }
  2051. // Check if the value matches
  2052. if( str_data[i].val != value ){
  2053. continue;
  2054. }
  2055. // Look up the actual string
  2056. name = get_str(i);
  2057. // Compare the prefix
  2058. if( !strncasecmp( name, prefix, strlen(prefix) ) ){
  2059. // We found a match
  2060. return name;
  2061. }
  2062. }
  2063. return NULL;
  2064. }
  2065. /// Retrieves the value of a constant parameter.
  2066. bool script_get_parameter(const char* name, int64* value)
  2067. {
  2068. int n = search_str(name);
  2069. if (n == -1 || str_data[n].type != C_PARAM)
  2070. {// not found or not a parameter
  2071. return false;
  2072. }
  2073. value[0] = str_data[n].val;
  2074. return true;
  2075. }
  2076. /// Retrieves the value of a constant.
  2077. bool script_get_constant(const char* name, int64* value)
  2078. {
  2079. int n = search_str(name);
  2080. if( n == -1 || str_data[n].type != C_INT )
  2081. {// not found or not a constant
  2082. return false;
  2083. }
  2084. value[0] = str_data[n].val;
  2085. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2086. if( str_data[n].deprecated ){
  2087. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2088. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2089. if (str_data[n].name)
  2090. ShowWarning( "Please use '%s' instead!\n", str_data[n].name );
  2091. }
  2092. #endif
  2093. return true;
  2094. }
  2095. /// Creates new constant or parameter with given value.
  2096. void script_set_constant_(const char* name, int64 value, const char* constant_name, bool isparameter, bool deprecated)
  2097. {
  2098. int n = add_str(name);
  2099. if( str_data[n].type == C_NOP )
  2100. {// new
  2101. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2102. str_data[n].val = value;
  2103. str_data[n].deprecated = deprecated;
  2104. str_data[n].name = constant_name;
  2105. }
  2106. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2107. {// existing parameter or constant
  2108. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%" PRId64 ", new value=%" PRId64 ").\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  2109. }
  2110. else
  2111. {// existing name
  2112. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2113. }
  2114. }
  2115. const std::string ConstantDatabase::getDefaultLocation(){
  2116. return std::string(db_path) + "/const.yml";
  2117. }
  2118. uint64 ConstantDatabase::parseBodyNode( const ryml::NodeRef& node ) {
  2119. std::string constant_name;
  2120. if (!this->asString( node, "Name", constant_name ))
  2121. return 0;
  2122. char name[1024];
  2123. if (sscanf(constant_name.c_str(), "%1023[A-Za-z0-9/_]", name) != 1) {
  2124. this->invalidWarning( node["Name"], "Invalid constant definition \"%s\", skipping.\n", constant_name.c_str() );
  2125. return 0;
  2126. }
  2127. int64 val;
  2128. if (!this->asInt64( node, "Value", val ))
  2129. return 0;
  2130. bool type = false;
  2131. if (this->nodeExists(node, "Parameter") && !this->asBool( node, "Parameter", type ))
  2132. return 0;
  2133. script_set_constant(name, val, type, false);
  2134. return 1;
  2135. }
  2136. ConstantDatabase constant_db;
  2137. /**
  2138. * Sets source-end constants for NPC scripts to access.
  2139. **/
  2140. void script_hardcoded_constants(void) {
  2141. #include "script_constants.hpp"
  2142. }
  2143. /*==========================================
  2144. * Display emplacement line of script
  2145. *------------------------------------------*/
  2146. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2147. {
  2148. int i;
  2149. if( p == NULL || !p[0] ) return NULL;
  2150. if( line < 0 )
  2151. StringBuf_Printf(buf, "*% 5d : ", -line);
  2152. else
  2153. StringBuf_Printf(buf, " % 5d : ", line);
  2154. for(i=0;p[i] && p[i] != '\n';i++){
  2155. if(p + i != mark)
  2156. StringBuf_Printf(buf, "%c", p[i]);
  2157. else
  2158. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2159. }
  2160. StringBuf_AppendStr(buf, "\n");
  2161. return p+i+(p[i] == '\n' ? 1 : 0);
  2162. }
  2163. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2164. // Find the line where the error occurred
  2165. int j;
  2166. int line = start_line;
  2167. const char *p;
  2168. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2169. for(p=src;p && *p;line++){
  2170. const char *lineend=strchr(p,'\n');
  2171. if(lineend==NULL || error_pos_cur<lineend){
  2172. break;
  2173. }
  2174. for( j = 0; j < 4; j++ ) {
  2175. linestart[j] = linestart[j+1];
  2176. }
  2177. linestart[4] = p;
  2178. p=lineend+1;
  2179. }
  2180. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2181. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2182. for(j = 0; j < 5; j++ ) {
  2183. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2184. }
  2185. p = script_print_line(buf, p, error_pos_cur, -line);
  2186. for(j = 0; j < 5; j++) {
  2187. p = script_print_line(buf, p, NULL, line + j + 1 );
  2188. }
  2189. }
  2190. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2191. StringBuf buf;
  2192. StringBuf_Init(&buf);
  2193. StringBuf_AppendStr(&buf, "\a\n");
  2194. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2195. ShowError("%s", StringBuf_Value(&buf));
  2196. StringBuf_Destroy(&buf);
  2197. }
  2198. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2199. StringBuf buf;
  2200. StringBuf_Init(&buf);
  2201. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2202. ShowWarning("%s", StringBuf_Value(&buf));
  2203. StringBuf_Destroy(&buf);
  2204. }
  2205. /*==========================================
  2206. * Analysis of the script
  2207. *------------------------------------------*/
  2208. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2209. {
  2210. const char *p,*tmpp;
  2211. int i;
  2212. struct script_code* code = NULL;
  2213. char end;
  2214. bool unresolved_names = false;
  2215. parser_current_src = src;
  2216. parser_current_file = file;
  2217. parser_current_line = line;
  2218. if( src == NULL )
  2219. return NULL;// empty script
  2220. memset(&syntax,0,sizeof(syntax));
  2221. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2222. script_pos=0;
  2223. script_size=SCRIPT_BLOCK_SIZE;
  2224. parse_nextline(true, NULL);
  2225. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2226. if( options&SCRIPT_USE_LABEL_DB )
  2227. db_clear(scriptlabel_db);
  2228. parse_options = options;
  2229. if( setjmp( error_jump ) != 0 ) {
  2230. //Restore program state when script has problems. [from jA]
  2231. int j;
  2232. const int size = ARRAYLENGTH(syntax.curly);
  2233. if( error_report )
  2234. script_error(src,file,line,error_msg,error_pos);
  2235. aFree( error_msg );
  2236. aFree( script_buf );
  2237. script_pos = 0;
  2238. script_size = 0;
  2239. script_buf = NULL;
  2240. for(j=LABEL_START;j<str_num;j++)
  2241. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2242. for(j=0; j<size; j++)
  2243. linkdb_final(&syntax.curly[j].case_label);
  2244. return NULL;
  2245. }
  2246. parse_syntax_for_flag=0;
  2247. p=src;
  2248. p=skip_space(p);
  2249. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2250. {// does not require brackets around the script
  2251. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2252. {// empty script and can return NULL
  2253. aFree( script_buf );
  2254. script_pos = 0;
  2255. script_size = 0;
  2256. script_buf = NULL;
  2257. return NULL;
  2258. }
  2259. end = '\0';
  2260. }
  2261. else
  2262. {// requires brackets around the script
  2263. if( *p != '{' )
  2264. disp_error_message("not found '{'",p);
  2265. p = skip_space(p+1);
  2266. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2267. {// empty script and can return NULL
  2268. aFree( script_buf );
  2269. script_pos = 0;
  2270. script_size = 0;
  2271. script_buf = NULL;
  2272. return NULL;
  2273. }
  2274. end = '}';
  2275. }
  2276. // clear references of labels, variables and internal functions
  2277. for(i=LABEL_START;i<str_num;i++){
  2278. if(
  2279. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2280. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2281. ){
  2282. str_data[i].type=C_NOP;
  2283. str_data[i].backpatch=-1;
  2284. str_data[i].label=-1;
  2285. }
  2286. }
  2287. while( syntax.curly_count != 0 || *p != end )
  2288. {
  2289. if( *p == '\0' )
  2290. disp_error_message("unexpected end of script",p);
  2291. // Special handling only label
  2292. tmpp=skip_space(skip_word(p));
  2293. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2294. i=add_word(p);
  2295. set_label(i,script_pos,p);
  2296. if( parse_options&SCRIPT_USE_LABEL_DB )
  2297. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2298. p=tmpp+1;
  2299. p=skip_space(p);
  2300. continue;
  2301. }
  2302. // All other lumped
  2303. p=parse_line(p);
  2304. p=skip_space(p);
  2305. parse_nextline(false, p);
  2306. }
  2307. add_scriptc(C_NOP);
  2308. // trim code to size
  2309. script_size = script_pos;
  2310. RECREATE(script_buf,unsigned char,script_pos);
  2311. // default unknown references to variables
  2312. for(i=LABEL_START;i<str_num;i++){
  2313. if(str_data[i].type==C_NOP){
  2314. int j;
  2315. str_data[i].type=C_NAME;
  2316. str_data[i].label=i;
  2317. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2318. int next=GETVALUE(script_buf,j);
  2319. SETVALUE(script_buf,j,i);
  2320. j=next;
  2321. }
  2322. }
  2323. else if( str_data[i].type == C_USERFUNC )
  2324. {// 'function name;' without follow-up code
  2325. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2326. unresolved_names = true;
  2327. }
  2328. }
  2329. if( unresolved_names )
  2330. {
  2331. disp_error_message("parse_script: unresolved function references", p);
  2332. }
  2333. #ifdef DEBUG_DISP
  2334. for(i=0;i<script_pos;i++){
  2335. if((i&15)==0) ShowMessage("%04x : ",i);
  2336. ShowMessage("%02x ",script_buf[i]);
  2337. if((i&15)==15) ShowMessage("\n");
  2338. }
  2339. ShowMessage("\n");
  2340. #endif
  2341. #ifdef DEBUG_DISASM
  2342. {
  2343. int i = 0,j;
  2344. while(i < script_pos) {
  2345. c_op op = get_com(script_buf,&i);
  2346. ShowMessage("%06x %s", i, script_op2name(op));
  2347. j = i;
  2348. switch(op) {
  2349. case C_INT:
  2350. ShowMessage(" %d", get_num(script_buf,&i));
  2351. break;
  2352. case C_POS:
  2353. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2354. i += 3;
  2355. break;
  2356. case C_NAME:
  2357. j = (*(int*)(script_buf+i)&0xffffff);
  2358. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2359. i += 3;
  2360. break;
  2361. case C_STR:
  2362. j = strlen(script_buf + i);
  2363. ShowMessage(" %s", script_buf + i);
  2364. i += j+1;
  2365. break;
  2366. }
  2367. ShowMessage(CL_CLL"\n");
  2368. }
  2369. }
  2370. #endif
  2371. CREATE(code,struct script_code,1);
  2372. code->script_buf = script_buf;
  2373. code->script_size = script_size;
  2374. code->local.vars = NULL;
  2375. code->local.arrays = NULL;
  2376. return code;
  2377. }
  2378. /// Returns the player attached to this script, identified by the rid.
  2379. /// If there is no player attached, the script is terminated.
  2380. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){
  2381. *sd = map_id2sd( st->rid );
  2382. if( *sd ){
  2383. return true;
  2384. }else{
  2385. ShowError("%s: fatal error ! player not attached!\n",func);
  2386. script_reportfunc(st);
  2387. script_reportsrc(st);
  2388. st->state = END;
  2389. return false;
  2390. }
  2391. }
  2392. /**
  2393. * Dereferences a variable/constant, replacing it with a copy of the value.
  2394. * @param st Script state
  2395. * @param data Variable/constant
  2396. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2397. */
  2398. struct script_data *get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2399. {
  2400. const char* name;
  2401. char prefix;
  2402. char postfix;
  2403. if( !data_isreference(data) )
  2404. return data;// not a variable/constant
  2405. name = reference_getname(data);
  2406. prefix = name[0];
  2407. postfix = name[strlen(name) - 1];
  2408. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2409. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2410. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2411. if( postfix == '$' ) {// string variable
  2412. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2413. data->type = C_CONSTSTR;
  2414. data->u.str = const_cast<char *>("");
  2415. } else {// integer variable
  2416. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2417. data->type = C_INT;
  2418. data->u.num = 0;
  2419. }
  2420. return data;
  2421. }
  2422. }
  2423. if( postfix == '$' ) {// string variable
  2424. switch( prefix ) {
  2425. case '@':
  2426. data->u.str = pc_readregstr(sd, data->u.num);
  2427. break;
  2428. case '$':
  2429. data->u.str = mapreg_readregstr(data->u.num);
  2430. break;
  2431. case '#':
  2432. if( name[1] == '#' )
  2433. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2434. else
  2435. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2436. break;
  2437. case '.':
  2438. {
  2439. struct DBMap* n = data->ref ?
  2440. data->ref->vars : name[1] == '@' ?
  2441. st->stack->scope.vars : // instance/scope variable
  2442. st->script->local.vars; // npc variable
  2443. if( n )
  2444. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2445. else
  2446. data->u.str = NULL;
  2447. }
  2448. break;
  2449. case '\'':
  2450. {
  2451. struct DBMap* n = nullptr;
  2452. if (data->ref)
  2453. n = data->ref->vars;
  2454. else {
  2455. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2456. if (idata)
  2457. n = idata->regs.vars;
  2458. }
  2459. if (n)
  2460. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2461. else {
  2462. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2463. data->u.str = NULL;
  2464. }
  2465. break;
  2466. }
  2467. default:
  2468. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2469. break;
  2470. }
  2471. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2472. data->type = C_CONSTSTR;
  2473. data->u.str = const_cast<char *>("");
  2474. } else {// duplicate string
  2475. data->type = C_STR;
  2476. data->u.str = aStrdup(data->u.str);
  2477. }
  2478. } else {// integer variable
  2479. data->type = C_INT;
  2480. if( reference_toconstant(data) ) {
  2481. data->u.num = reference_getconstant(data);
  2482. } else if( reference_toparam(data) ) {
  2483. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2484. } else
  2485. switch( prefix ) {
  2486. case '@':
  2487. data->u.num = pc_readreg(sd, data->u.num);
  2488. break;
  2489. case '$':
  2490. data->u.num = mapreg_readreg(data->u.num);
  2491. break;
  2492. case '#':
  2493. if( name[1] == '#' )
  2494. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2495. else
  2496. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2497. break;
  2498. case '.':
  2499. {
  2500. struct DBMap* n = data->ref ?
  2501. data->ref->vars : name[1] == '@' ?
  2502. st->stack->scope.vars : // instance/scope variable
  2503. st->script->local.vars; // npc variable
  2504. if( n )
  2505. data->u.num = i64db_i64get(n,reference_getuid(data));
  2506. else
  2507. data->u.num = 0;
  2508. }
  2509. break;
  2510. case '\'':
  2511. {
  2512. struct DBMap* n = nullptr;
  2513. if (data->ref)
  2514. n = data->ref->vars;
  2515. else {
  2516. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2517. if (idata)
  2518. n = idata->regs.vars;
  2519. }
  2520. if (n)
  2521. data->u.num = i64db_i64get(n,reference_getuid(data));
  2522. else {
  2523. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2524. data->u.num = 0;
  2525. }
  2526. break;
  2527. }
  2528. default:
  2529. data->u.num = pc_readglobalreg(sd, data->u.num);
  2530. break;
  2531. }
  2532. }
  2533. data->ref = NULL;
  2534. return data;
  2535. }
  2536. struct script_data *get_val(struct script_state* st, struct script_data* data)
  2537. {
  2538. return get_val_(st,data,NULL);
  2539. }
  2540. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2541. const char* get_val2_str( struct script_state* st, int64 uid, struct reg_db* ref ){
  2542. push_val2( st->stack, C_NAME, uid, ref );
  2543. struct script_data* data = script_getdatatop( st, -1 );
  2544. get_val( st, data );
  2545. const char* value = "";
  2546. if( data->type == C_INT ){
  2547. ShowError( "get_val2_num: Invalid call. Variable %s is a numeric type.\n", reference_getname( data ) );
  2548. }else{
  2549. value = data->u.str;
  2550. }
  2551. // Do NOT remove the value from stack here, the pointer is returned here and will be used by the caller [Lemongrass]
  2552. // script_removetop( st, -1, 0 );
  2553. return value;
  2554. }
  2555. int64 get_val2_num( struct script_state* st, int64 uid, struct reg_db* ref ){
  2556. push_val2( st->stack, C_NAME, uid, ref );
  2557. struct script_data* data = script_getdatatop( st, -1 );
  2558. get_val( st, data );
  2559. int64 value = 0;
  2560. if( data->type == C_INT ){
  2561. value = data->u.num;
  2562. }else{
  2563. ShowError( "get_val2_num: Invalid call. Variable %s is not a numeric type.\n", reference_getname( data ) );
  2564. }
  2565. script_removetop( st, -1, 0 );
  2566. return value;
  2567. }
  2568. /**
  2569. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2570. * Will be gone as soon as undefined var feature is implemented
  2571. **/
  2572. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2573. {
  2574. const char *name = get_str(script_getvarid(uid));
  2575. // is here st can be null pointer and st->rid is wrong?
  2576. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2577. bool insert = false;
  2578. if (sd && !st) {
  2579. // when sd comes, st isn't available
  2580. insert = true;
  2581. } else {
  2582. if( is_string_variable(name) ) {
  2583. const char* str = get_val2_str( st, uid, ref );
  2584. if( str && *str )
  2585. insert = true;
  2586. // Remove stack entry from get_val2_str
  2587. script_removetop( st, -1, 0 );
  2588. } else {
  2589. int64 num = get_val2_num( st, uid, ref );
  2590. if( num )
  2591. insert = true;
  2592. }
  2593. }
  2594. if (src && src->arrays) {
  2595. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2596. if (sa) {
  2597. unsigned int i;
  2598. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2599. if( i != sa->size ) {
  2600. if( !insert )
  2601. script_array_remove_member(src,sa,i);
  2602. return;
  2603. }
  2604. script_array_add_member(sa,0);
  2605. } else if (insert) {
  2606. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2607. }
  2608. }
  2609. }
  2610. /**
  2611. * Returns array size by ID
  2612. **/
  2613. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2614. {
  2615. struct script_array *sa = NULL;
  2616. struct reg_db *src = script_array_src(st, sd, name, ref);
  2617. if (src && src->arrays)
  2618. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2619. return sa ? sa->size : 0;
  2620. }
  2621. /**
  2622. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2623. **/
  2624. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2625. {
  2626. struct script_array *sa = NULL;
  2627. struct reg_db *src = script_array_src(st, sd, name, ref);
  2628. if (src && src->arrays) {
  2629. int key = add_word(name);
  2630. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2631. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2632. unsigned int i, highest_key = 0;
  2633. for(i = 0; i < sa->size; i++) {
  2634. if( sa->members[i] > highest_key )
  2635. highest_key = sa->members[i];
  2636. }
  2637. return sa->size ? highest_key + 1 : 0;
  2638. }
  2639. }
  2640. return SCRIPT_CMD_SUCCESS;
  2641. }
  2642. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2643. {
  2644. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2645. aFree(sa->members);
  2646. ers_free(array_ers, sa);
  2647. return SCRIPT_CMD_SUCCESS;
  2648. }
  2649. /**
  2650. * Clears script_array and removes it from script->array_db
  2651. **/
  2652. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2653. {
  2654. aFree(sa->members);
  2655. idb_remove(src->arrays, sa->id);
  2656. ers_free(array_ers, sa);
  2657. }
  2658. /**
  2659. * Removes a member from a script_array list
  2660. *
  2661. * @param idx the index of the member in script_array struct list, not of the actual array member
  2662. **/
  2663. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2664. {
  2665. unsigned int i, cursor;
  2666. // it's the only member left, no need to do anything other than delete the array data
  2667. if( sa->size == 1 ) {
  2668. script_array_delete(src,sa);
  2669. return;
  2670. }
  2671. sa->members[idx] = UINT_MAX;
  2672. for(i = 0, cursor = 0; i < sa->size; i++) {
  2673. if( sa->members[i] == UINT_MAX )
  2674. continue;
  2675. if( i != cursor )
  2676. sa->members[cursor] = sa->members[i];
  2677. cursor++;
  2678. }
  2679. sa->size = cursor;
  2680. }
  2681. /**
  2682. * Appends a new array index to the list in script_array
  2683. *
  2684. * @param idx the index of the array member being inserted
  2685. **/
  2686. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2687. {
  2688. RECREATE(sa->members, unsigned int, ++sa->size);
  2689. sa->members[sa->size - 1] = idx;
  2690. }
  2691. /**
  2692. * Obtains the source of the array database for this type and scenario
  2693. * Initializes such database when not yet initialized.
  2694. **/
  2695. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2696. {
  2697. struct reg_db *src = NULL;
  2698. switch( name[0] ) {
  2699. // from player
  2700. default: // char reg
  2701. case '@': // temp char reg
  2702. case '#': // account reg
  2703. src = &sd->regs;
  2704. break;
  2705. case '$': // map reg
  2706. src = &regs;
  2707. break;
  2708. case '.': // npc/script
  2709. if (ref)
  2710. src = ref;
  2711. else
  2712. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2713. break;
  2714. case '\'': // instance
  2715. {
  2716. if (ref)
  2717. src = ref;
  2718. else {
  2719. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2720. if (idata)
  2721. src = &idata->regs;
  2722. }
  2723. break;
  2724. }
  2725. }
  2726. if( src ) {
  2727. if( !src->arrays )
  2728. src->arrays = idb_alloc(DB_OPT_BASE);
  2729. return src;
  2730. }
  2731. return NULL;
  2732. }
  2733. /**
  2734. * Processes a array member modification, and update data accordingly
  2735. *
  2736. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2737. * @param num[in] Variable ID
  2738. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2739. **/
  2740. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2741. {
  2742. struct script_array *sa = NULL;
  2743. int id = script_getvarid(num);
  2744. unsigned int index = script_getvaridx(num);
  2745. if (!src->arrays) {
  2746. src->arrays = idb_alloc(DB_OPT_BASE);
  2747. } else {
  2748. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2749. }
  2750. if( sa ) {
  2751. unsigned int i;
  2752. // search
  2753. for(i = 0; i < sa->size; i++) {
  2754. if( sa->members[i] == index )
  2755. break;
  2756. }
  2757. // if existent
  2758. if( i != sa->size ) {
  2759. // if empty, we gotta remove it
  2760. if( empty ) {
  2761. script_array_remove_member(src, sa, i);
  2762. }
  2763. } else if( !empty ) { /* new entry */
  2764. script_array_add_member(sa,index);
  2765. // we do nothing if its empty, no point in modifying array data for a new empty member
  2766. }
  2767. } else if ( !empty ) { // we only move to create if not empty
  2768. sa = ers_alloc(array_ers, struct script_array);
  2769. sa->id = id;
  2770. sa->members = NULL;
  2771. sa->size = 0;
  2772. script_array_add_member(sa,index);
  2773. idb_put(src->arrays, id, sa);
  2774. }
  2775. }
  2776. /**
  2777. * Stores the value of a script variable
  2778. *
  2779. * @param st: current script state.
  2780. * @param sd: current character data.
  2781. * @param num: variable identifier.
  2782. * @param name: variable name.
  2783. * @param value: new value.
  2784. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2785. * @return: 0 failure, 1 success.
  2786. *
  2787. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2788. *------------------------------------------*/
  2789. bool set_reg_str( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, const char* value, struct reg_db *ref ){
  2790. char prefix = name[0];
  2791. size_t vlen = 0;
  2792. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2793. 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 );
  2794. return false;
  2795. }
  2796. if( !is_string_variable( name ) ){
  2797. // integer variable
  2798. return false;
  2799. }
  2800. switch( prefix ){
  2801. case '@':
  2802. pc_setregstr( sd, num, value );
  2803. return true;
  2804. case '$':
  2805. return mapreg_setregstr( num, value );
  2806. case '#':
  2807. return ( name[1] == '#' ) ? pc_setaccountreg2str( sd, num, value ) : pc_setaccountregstr( sd, num, value );
  2808. case '.': {
  2809. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2810. if( n ){
  2811. if( value[0] ){
  2812. i64db_put( n->vars, num, aStrdup( value ) );
  2813. if( script_getvaridx( num ) ){
  2814. script_array_update( n, num, false );
  2815. }
  2816. }else{
  2817. i64db_remove( n->vars, num );
  2818. if( script_getvaridx( num ) ){
  2819. script_array_update( n, num, true );
  2820. }
  2821. }
  2822. }
  2823. }
  2824. return true;
  2825. case '\'': {
  2826. struct reg_db *src = nullptr;
  2827. if( ref ){
  2828. src = ref;
  2829. }else{
  2830. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2831. if (idata)
  2832. src = &idata->regs;
  2833. }
  2834. if( src ){
  2835. bool empty;
  2836. if( value[0] ){
  2837. i64db_put( src->vars, num, aStrdup( value ) );
  2838. empty = false;
  2839. }else{
  2840. i64db_remove( src->vars, num );
  2841. empty = true;
  2842. }
  2843. if( script_getvaridx( num ) != 0 ){
  2844. script_array_update( src, num, empty );
  2845. }
  2846. }else{
  2847. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2848. script_reportsrc( st );
  2849. }
  2850. return true;
  2851. }
  2852. default:
  2853. return pc_setglobalreg_str( sd, num, value );
  2854. }
  2855. }
  2856. bool set_reg_num( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, int64 value, struct reg_db *ref ){
  2857. char prefix = name[0];
  2858. size_t vlen = 0;
  2859. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2860. 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 );
  2861. return false;
  2862. }
  2863. if( is_string_variable( name ) ){
  2864. // string variable
  2865. return false;
  2866. }
  2867. if( str_data[script_getvarid(num)].type == C_PARAM ){
  2868. if( pc_setparam( sd, str_data[script_getvarid(num)].val, value ) == 0 ){
  2869. if( st != NULL ) {
  2870. ShowError( "script_set_reg: failed to set param '%s' to %" PRId64 ".\n", name, value );
  2871. script_reportsrc( st );
  2872. st->state = END;
  2873. }
  2874. return false;
  2875. }
  2876. return true;
  2877. }
  2878. switch( prefix ){
  2879. case '@':
  2880. pc_setreg( sd, num, value );
  2881. return true;
  2882. case '$':
  2883. return mapreg_setreg( num, value );
  2884. case '#':
  2885. return ( name[1] == '#' ) ? pc_setaccountreg2(sd, num, value) : pc_setaccountreg( sd, num, value );
  2886. case '.': {
  2887. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2888. if( n ){
  2889. if( value != 0 ){
  2890. i64db_i64put( n->vars, num, value );
  2891. if( script_getvaridx( num ) ){
  2892. script_array_update( n, num, false );
  2893. }
  2894. }else{
  2895. i64db_remove( n->vars, num );
  2896. if( script_getvaridx( num ) ){
  2897. script_array_update( n, num, true );
  2898. }
  2899. }
  2900. }
  2901. }
  2902. return true;
  2903. case '\'': {
  2904. struct reg_db *src = nullptr;
  2905. if( ref ){
  2906. src = ref;
  2907. }else{
  2908. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2909. if (idata)
  2910. src = &idata->regs;
  2911. }
  2912. if( src ){
  2913. bool empty;
  2914. if( value != 0 ){
  2915. i64db_i64put( src->vars, num, value );
  2916. empty = false;
  2917. }else{
  2918. i64db_remove( src->vars, num );
  2919. empty = true;
  2920. }
  2921. if( script_getvaridx( num ) != 0 ){
  2922. script_array_update( src, num, empty );
  2923. }
  2924. }else{
  2925. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2926. script_reportsrc( st );
  2927. }
  2928. return true;
  2929. }
  2930. default:
  2931. return pc_setglobalreg( sd, num, value );
  2932. }
  2933. }
  2934. bool set_var_str( struct map_session_data* sd, const char* name, const char* val ){
  2935. return set_reg_str( NULL, sd, reference_uid( add_str( name ), 0 ), name, val, NULL );
  2936. }
  2937. bool clear_reg( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, struct reg_db *ref ){
  2938. if( is_string_variable( name ) ){
  2939. return set_reg_str( st, sd, num, name, "", ref );
  2940. }else{
  2941. return set_reg_num( st, sd, num, name, 0, ref );
  2942. }
  2943. }
  2944. void setd_sub_num( struct script_state* st, struct map_session_data* sd, const char* varname, int elem, int64 value, struct reg_db* ref ){
  2945. set_reg_num( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2946. }
  2947. void setd_sub_str( struct script_state* st, struct map_session_data* sd, const char* varname, int elem, const char* value, struct reg_db* ref ){
  2948. set_reg_str( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2949. }
  2950. /**
  2951. * Converts the data to a string
  2952. * @param st
  2953. * @param data
  2954. * @param sd
  2955. */
  2956. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2957. {
  2958. char* p;
  2959. get_val_(st, data, sd);
  2960. if( data_isstring(data) )
  2961. {// nothing to convert
  2962. }
  2963. else if( data_isint(data) )
  2964. {// int -> string
  2965. CREATE(p, char, ITEM_NAME_LENGTH);
  2966. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2967. p[ITEM_NAME_LENGTH-1] = '\0';
  2968. data->type = C_STR;
  2969. data->u.str = p;
  2970. }
  2971. else if( data_isreference(data) )
  2972. {// reference -> string
  2973. //##TODO when does this happen (check get_val) [FlavioJS] -- at getd!!
  2974. data->type = C_CONSTSTR;
  2975. data->u.str = reference_getname(data);
  2976. }
  2977. else
  2978. {// unsupported data type
  2979. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2980. script_reportdata(data);
  2981. script_reportsrc(st);
  2982. data->type = C_CONSTSTR;
  2983. data->u.str = const_cast<char *>("");
  2984. }
  2985. return data->u.str;
  2986. }
  2987. const char* conv_str(struct script_state* st, struct script_data* data)
  2988. {
  2989. return conv_str_(st, data, NULL);
  2990. }
  2991. /**
  2992. * Converts the data to an int
  2993. * @param st
  2994. * @param data
  2995. * @param sd
  2996. */
  2997. int64 conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2998. {
  2999. get_val_(st, data, sd);
  3000. if( data_isint(data) )
  3001. {// nothing to convert
  3002. }
  3003. else if( data_isstring(data) )
  3004. {// string -> int
  3005. // the result does not overflow or underflow, it is capped instead
  3006. // ex: 999999999999 is capped to INT_MAX (2147483647)
  3007. char* p = data->u.str;
  3008. int64 num;
  3009. bool error;
  3010. errno = 0;
  3011. num = strtoll(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  3012. error = (errno == ERANGE);
  3013. if( num < SCRIPT_INT_MIN || ( error && num == INT64_MIN ) ){
  3014. num = SCRIPT_INT_MIN;
  3015. ShowError( "script:conv_num: underflow detected, capping value to %" PRId64 "", SCRIPT_INT_MIN );
  3016. script_reportdata(data);
  3017. script_reportsrc(st);
  3018. }else if( num > SCRIPT_INT_MAX || ( error && num == INT64_MAX ) ){
  3019. num = SCRIPT_INT_MAX;
  3020. ShowError( "script:conv_num: overflow detected, capping value to %" PRId64 "", SCRIPT_INT_MAX );
  3021. script_reportdata(data);
  3022. script_reportsrc(st);
  3023. }
  3024. if( data->type == C_STR )
  3025. aFree(p);
  3026. data->type = C_INT;
  3027. data->u.num = num;
  3028. }
  3029. #if 0
  3030. // FIXME this function is being used to retrieve the position of labels and
  3031. // probably other stuff [FlavioJS]
  3032. else
  3033. {// unsupported data type
  3034. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3035. script_reportdata(data);
  3036. script_reportsrc(st);
  3037. data->type = C_INT;
  3038. data->u.num = 0;
  3039. }
  3040. #endif
  3041. return data->u.num;
  3042. }
  3043. int64 conv_num64(struct script_state* st, struct script_data* data)
  3044. {
  3045. return conv_num_(st, data, NULL);
  3046. }
  3047. int conv_num(struct script_state* st, struct script_data* data)
  3048. {
  3049. return static_cast<int>(conv_num_(st, data, NULL));
  3050. }
  3051. //
  3052. // Stack operations
  3053. //
  3054. /// Increases the size of the stack
  3055. void stack_expand(struct script_stack* stack)
  3056. {
  3057. stack->sp_max += 64;
  3058. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3059. stack->sp_max * sizeof(stack->stack_data[0]) );
  3060. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3061. 64 * sizeof(stack->stack_data[0]) );
  3062. }
  3063. /// Pushes a value into the stack
  3064. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  3065. /// Pushes a value into the stack (with reference)
  3066. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3067. {
  3068. if( stack->sp >= stack->sp_max )
  3069. stack_expand(stack);
  3070. stack->stack_data[stack->sp].type = type;
  3071. stack->stack_data[stack->sp].u.num = val;
  3072. stack->stack_data[stack->sp].ref = ref;
  3073. stack->sp++;
  3074. return &stack->stack_data[stack->sp-1];
  3075. }
  3076. /// Pushes a string into the stack
  3077. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3078. {
  3079. if( stack->sp >= stack->sp_max )
  3080. stack_expand(stack);
  3081. stack->stack_data[stack->sp].type = type;
  3082. stack->stack_data[stack->sp].u.str = str;
  3083. stack->stack_data[stack->sp].ref = NULL;
  3084. stack->sp++;
  3085. return &stack->stack_data[stack->sp-1];
  3086. }
  3087. /// Pushes a retinfo into the stack
  3088. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3089. {
  3090. if( stack->sp >= stack->sp_max )
  3091. stack_expand(stack);
  3092. stack->stack_data[stack->sp].type = C_RETINFO;
  3093. stack->stack_data[stack->sp].u.ri = ri;
  3094. stack->stack_data[stack->sp].ref = ref;
  3095. stack->sp++;
  3096. return &stack->stack_data[stack->sp-1];
  3097. }
  3098. /// Pushes a copy of the target position into the stack
  3099. struct script_data* push_copy(struct script_stack* stack, int pos)
  3100. {
  3101. switch( stack->stack_data[pos].type ) {
  3102. case C_CONSTSTR:
  3103. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3104. break;
  3105. case C_STR:
  3106. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3107. break;
  3108. case C_RETINFO:
  3109. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3110. exit(1);
  3111. break;
  3112. default:
  3113. return push_val2(
  3114. stack,stack->stack_data[pos].type,
  3115. stack->stack_data[pos].u.num,
  3116. stack->stack_data[pos].ref
  3117. );
  3118. break;
  3119. }
  3120. }
  3121. /// Removes the values in indexes [start,end] from the stack.
  3122. /// Adjusts all stack pointers.
  3123. void pop_stack(struct script_state* st, int start, int end)
  3124. {
  3125. struct script_stack* stack = st->stack;
  3126. struct script_data* data;
  3127. int i;
  3128. if( start < 0 )
  3129. start = 0;
  3130. if( end > stack->sp )
  3131. end = stack->sp;
  3132. if( start >= end )
  3133. return;// nothing to pop
  3134. // free stack elements
  3135. for( i = start; i < end; i++ )
  3136. {
  3137. data = &stack->stack_data[i];
  3138. if( data->type == C_STR )
  3139. aFree(data->u.str);
  3140. if( data->type == C_RETINFO ) {
  3141. struct script_retinfo* ri = data->u.ri;
  3142. if (ri->scope.vars) {
  3143. script_free_vars(ri->scope.vars);
  3144. ri->scope.vars = NULL;
  3145. }
  3146. if (ri->scope.arrays) {
  3147. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3148. ri->scope.arrays = NULL;
  3149. }
  3150. if( data->ref )
  3151. aFree(data->ref);
  3152. aFree(ri);
  3153. }
  3154. data->type = C_NOP;
  3155. }
  3156. // move the rest of the elements
  3157. if( stack->sp > end )
  3158. {
  3159. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3160. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3161. stack->stack_data[i].type = C_NOP;
  3162. }
  3163. // adjust stack pointers
  3164. if( st->start > end ){
  3165. st->start -= end - start;
  3166. }else if( st->start > start ){
  3167. st->start = start;
  3168. }
  3169. if( st->end > end ){
  3170. st->end -= end - start;
  3171. }else if( st->end > start ){
  3172. st->end = start;
  3173. }
  3174. if( stack->defsp > end ){
  3175. stack->defsp -= end - start;
  3176. }else if( stack->defsp > start ){
  3177. stack->defsp = start;
  3178. }
  3179. stack->sp -= end - start;
  3180. }
  3181. ///
  3182. ///
  3183. ///
  3184. /*==========================================
  3185. * Release script dependent variable, dependent variable of function
  3186. *------------------------------------------*/
  3187. void script_free_vars(struct DBMap* storage)
  3188. {
  3189. if( storage ) {
  3190. // destroy the storage construct containing the variables
  3191. db_destroy(storage);
  3192. }
  3193. }
  3194. void script_free_code(struct script_code* code)
  3195. {
  3196. nullpo_retv(code);
  3197. if (code->instances)
  3198. script_stop_scriptinstances(code);
  3199. script_free_vars(code->local.vars);
  3200. if (code->local.arrays)
  3201. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3202. aFree(code->script_buf);
  3203. aFree(code);
  3204. }
  3205. /// Creates a new script state.
  3206. ///
  3207. /// @param script Script code
  3208. /// @param pos Position in the code
  3209. /// @param rid Who is running the script (attached player)
  3210. /// @param oid Where the code is being run (npc 'object')
  3211. /// @return Script state
  3212. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3213. {
  3214. struct script_state* st;
  3215. st = ers_alloc(st_ers, struct script_state);
  3216. st->stack = ers_alloc(stack_ers, struct script_stack);
  3217. st->stack->sp = 0;
  3218. st->stack->sp_max = 64;
  3219. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3220. st->stack->defsp = st->stack->sp;
  3221. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3222. st->stack->scope.arrays = NULL;
  3223. st->state = RUN;
  3224. st->script = rootscript;
  3225. st->pos = pos;
  3226. st->rid = rid;
  3227. st->oid = oid;
  3228. st->sleep.timer = INVALID_TIMER;
  3229. st->npc_item_flag = battle_config.item_enabled_npc;
  3230. if( st->script->instances != USHRT_MAX )
  3231. st->script->instances++;
  3232. else {
  3233. struct npc_data *nd = map_id2nd(oid);
  3234. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3235. }
  3236. if (!st->script->local.vars)
  3237. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3238. st->id = next_id++;
  3239. active_scripts++;
  3240. idb_put(st_db, st->id, st);
  3241. return st;
  3242. }
  3243. /// Frees a script state.
  3244. ///
  3245. /// @param st Script state
  3246. void script_free_state(struct script_state* st)
  3247. {
  3248. if (idb_exists(st_db, st->id)) {
  3249. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3250. if (st->bk_st) // backup was not restored
  3251. 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);
  3252. if (sd && sd->st == st) { // Current script is aborted.
  3253. if(sd->state.using_fake_npc) {
  3254. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3255. sd->state.using_fake_npc = 0;
  3256. }
  3257. sd->st = NULL;
  3258. sd->npc_id = 0;
  3259. }
  3260. if (st->sleep.timer != INVALID_TIMER)
  3261. delete_timer(st->sleep.timer, run_script_timer);
  3262. if (st->stack) {
  3263. script_free_vars(st->stack->scope.vars);
  3264. if (st->stack->scope.arrays)
  3265. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3266. pop_stack(st, 0, st->stack->sp);
  3267. aFree(st->stack->stack_data);
  3268. ers_free(stack_ers, st->stack);
  3269. st->stack = NULL;
  3270. }
  3271. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3272. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3273. script_free_vars(st->script->local.vars);
  3274. st->script->local.vars = NULL;
  3275. }
  3276. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3277. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3278. st->script->local.arrays = NULL;
  3279. }
  3280. }
  3281. st->pos = -1;
  3282. idb_remove(st_db, st->id);
  3283. ers_free(st_ers, st);
  3284. if (--active_scripts == 0)
  3285. next_id = 0;
  3286. }
  3287. }
  3288. //
  3289. // Main execution unit
  3290. //
  3291. /*==========================================
  3292. * Read command
  3293. *------------------------------------------*/
  3294. c_op get_com(unsigned char *script,int *pos)
  3295. {
  3296. int i = 0, j = 0;
  3297. if(script[*pos]>=0x80){
  3298. return C_INT;
  3299. }
  3300. while(script[*pos]>=0x40){
  3301. i=script[(*pos)++]<<j;
  3302. j+=6;
  3303. }
  3304. return (c_op)(i+(script[(*pos)++]<<j));
  3305. }
  3306. /*==========================================
  3307. * Income figures
  3308. *------------------------------------------*/
  3309. int64 get_num(unsigned char *script,int *pos)
  3310. {
  3311. int i, end;
  3312. int64 value;
  3313. // Find the end of the data
  3314. for( i = 0; script[*pos+i] >= 0xc0; i++ ) ; // do nothing just find it
  3315. end = i; // store the end for later
  3316. value = 0; // initialize empty value
  3317. // If more than one byte of data exists
  3318. for( ; i > 0; i-- ){
  3319. value |= (int64)( script[*pos+i] & (int64)0x3f );
  3320. value <<= 6;
  3321. }
  3322. // Put our last piece of data into the return value
  3323. value |= (script[*pos+i] & (int64)0x3f);
  3324. *pos += end + 1; // one byte after the last byte of data
  3325. return value;
  3326. }
  3327. /// Ternary operators
  3328. /// test ? if_true : if_false
  3329. void op_3(struct script_state* st, int op)
  3330. {
  3331. struct script_data* data;
  3332. int flag = 0;
  3333. data = script_getdatatop(st, -3);
  3334. get_val(st, data);
  3335. if( data_isstring(data) )
  3336. flag = data->u.str[0];// "" -> false
  3337. else if( data_isint(data) )
  3338. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3339. else
  3340. {
  3341. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3342. script_reportdata(data);
  3343. script_reportsrc(st);
  3344. script_removetop(st, -3, 0);
  3345. script_pushnil(st);
  3346. return;
  3347. }
  3348. if( flag )
  3349. script_pushcopytop(st, -2);
  3350. else
  3351. script_pushcopytop(st, -1);
  3352. script_removetop(st, -4, -1);
  3353. }
  3354. /// Binary string operators
  3355. /// s1 EQ s2 -> i
  3356. /// s1 NE s2 -> i
  3357. /// s1 GT s2 -> i
  3358. /// s1 GE s2 -> i
  3359. /// s1 LT s2 -> i
  3360. /// s1 LE s2 -> i
  3361. /// s1 ADD s2 -> s
  3362. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3363. {
  3364. int a = 0;
  3365. switch(op){
  3366. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3367. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3368. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3369. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3370. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3371. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3372. case C_ADD:
  3373. {
  3374. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3375. strcpy(buf, s1);
  3376. strcat(buf, s2);
  3377. script_pushstr(st, buf);
  3378. return;
  3379. }
  3380. default:
  3381. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3382. script_reportsrc(st);
  3383. script_pushnil(st);
  3384. st->state = END;
  3385. return;
  3386. }
  3387. script_pushint(st,a);
  3388. }
  3389. /// Binary number operators
  3390. /// i OP i -> i
  3391. void op_2num( struct script_state* st, int op, int64 i1, int64 i2 ){
  3392. int64 ret;
  3393. switch( op ) {
  3394. case C_AND: ret = i1 & i2; break;
  3395. case C_OR: ret = i1 | i2; break;
  3396. case C_XOR: ret = i1 ^ i2; break;
  3397. case C_LAND: ret = (i1 && i2); break;
  3398. case C_LOR: ret = (i1 || i2); break;
  3399. case C_EQ: ret = (i1 == i2); break;
  3400. case C_NE: ret = (i1 != i2); break;
  3401. case C_GT: ret = (i1 > i2); break;
  3402. case C_GE: ret = (i1 >= i2); break;
  3403. case C_LT: ret = (i1 < i2); break;
  3404. case C_LE: ret = (i1 <= i2); break;
  3405. case C_R_SHIFT: ret = i1>>i2; break;
  3406. case C_L_SHIFT: ret = i1<<i2; break;
  3407. case C_DIV:
  3408. case C_MOD:
  3409. if( i2 == 0 )
  3410. {
  3411. ShowError("script:op_2num: division by zero detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3412. script_reportsrc(st);
  3413. script_pushnil(st);
  3414. st->state = END;
  3415. return;
  3416. }
  3417. else if( op == C_DIV )
  3418. ret = i1 / i2;
  3419. else//if( op == C_MOD )
  3420. ret = i1 % i2;
  3421. break;
  3422. default:
  3423. bool overflow;
  3424. // operators that can overflow/underflow
  3425. switch( op ) {
  3426. case C_ADD: overflow = util::safe_addition( i1, i2, ret ); break;
  3427. case C_SUB: overflow = util::safe_substraction( i1, i2, ret ); break;
  3428. case C_MUL: overflow = util::safe_multiplication( i1, i2, ret ); break;
  3429. default:
  3430. ShowError("script:op_2num: unexpected number operator %s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3431. script_reportsrc(st);
  3432. script_pushnil(st);
  3433. return;
  3434. }
  3435. if( overflow ){
  3436. ShowWarning("script:op_2num: overflow detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3437. script_reportsrc(st);
  3438. script_pushnil(st);
  3439. st->state = END;
  3440. return;
  3441. }
  3442. }
  3443. script_pushint(st, ret);
  3444. }
  3445. /// Binary operators
  3446. void op_2(struct script_state *st, int op)
  3447. {
  3448. struct script_data* left, leftref;
  3449. struct script_data* right;
  3450. leftref.type = C_NOP;
  3451. left = script_getdatatop(st, -2);
  3452. right = script_getdatatop(st, -1);
  3453. if (st->op2ref) {
  3454. if (data_isreference(left)) {
  3455. leftref = *left;
  3456. }
  3457. st->op2ref = 0;
  3458. }
  3459. get_val(st, left);
  3460. get_val(st, right);
  3461. // automatic conversions
  3462. switch( op )
  3463. {
  3464. case C_ADD:
  3465. if( data_isint(left) && data_isstring(right) )
  3466. {// convert int-string to string-string
  3467. conv_str(st, left);
  3468. }
  3469. else if( data_isstring(left) && data_isint(right) )
  3470. {// convert string-int to string-string
  3471. conv_str(st, right);
  3472. }
  3473. break;
  3474. }
  3475. if( data_isstring(left) && data_isstring(right) )
  3476. {// ss => op_2str
  3477. op_2str(st, op, left->u.str, right->u.str);
  3478. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3479. if (leftref.type != C_NOP)
  3480. {
  3481. if (left->type == C_STR) // don't free C_CONSTSTR
  3482. aFree(left->u.str);
  3483. *left = leftref;
  3484. }
  3485. }
  3486. else if( data_isint(left) && data_isint(right) )
  3487. {// ii => op_2num
  3488. int64 i1 = left->u.num;
  3489. int64 i2 = right->u.num;
  3490. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3491. op_2num(st, op, i1, i2);
  3492. if (leftref.type != C_NOP)
  3493. *left = leftref;
  3494. }
  3495. else
  3496. {// invalid argument
  3497. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3498. script_reportdata(left);
  3499. script_reportdata(right);
  3500. script_reportsrc(st);
  3501. script_removetop(st, -2, 0);
  3502. script_pushnil(st);
  3503. st->state = END;
  3504. }
  3505. }
  3506. /// Unary operators
  3507. /// NEG i -> i
  3508. /// NOT i -> i
  3509. /// LNOT i -> i
  3510. void op_1(struct script_state* st, int op)
  3511. {
  3512. struct script_data* data;
  3513. data = script_getdatatop(st, -1);
  3514. get_val(st, data);
  3515. if( !data_isint(data) )
  3516. {// not a number
  3517. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3518. script_reportdata(data);
  3519. script_reportsrc(st);
  3520. script_pushnil(st);
  3521. st->state = END;
  3522. return;
  3523. }
  3524. int64 i1 = data->u.num;
  3525. script_removetop(st, -1, 0);
  3526. switch( op )
  3527. {
  3528. case C_NEG: i1 = -i1; break;
  3529. case C_NOT: i1 = ~i1; break;
  3530. case C_LNOT: i1 = !i1; break;
  3531. default:
  3532. ShowError("script:op_1: unexpected operator %s i1=%" PRId64 "\n", script_op2name(op), i1);
  3533. script_reportsrc(st);
  3534. script_pushnil(st);
  3535. st->state = END;
  3536. return;
  3537. }
  3538. script_pushint(st, i1);
  3539. }
  3540. /// Checks the type of all arguments passed to a built-in function.
  3541. ///
  3542. /// @param st Script state whose stack arguments should be inspected.
  3543. /// @param func Built-in function for which the arguments are intended.
  3544. static void script_check_buildin_argtype(struct script_state* st, int func)
  3545. {
  3546. int idx, invalid = 0;
  3547. for( idx = 2; script_hasdata(st, idx); idx++ )
  3548. {
  3549. struct script_data* data = script_getdata(st, idx);
  3550. script_function* sf = &buildin_func[str_data[func].val];
  3551. char type = sf->arg[idx-2];
  3552. if( type == '?' || type == '*' )
  3553. {// optional argument or unknown number of optional parameters ( no types are after this )
  3554. break;
  3555. }
  3556. else if( type == 0 )
  3557. {// more arguments than necessary ( should not happen, as it is checked before )
  3558. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3559. invalid++;
  3560. break;
  3561. }
  3562. else
  3563. {
  3564. const char* name = NULL;
  3565. if( data_isreference(data) )
  3566. {// get name for variables to determine the type they refer to
  3567. name = reference_getname(data);
  3568. }
  3569. switch( type )
  3570. {
  3571. case 'v':
  3572. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3573. {// variant
  3574. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3575. script_reportdata(data);
  3576. invalid++;
  3577. }
  3578. break;
  3579. case 's':
  3580. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3581. {// string
  3582. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3583. script_reportdata(data);
  3584. invalid++;
  3585. }
  3586. break;
  3587. case 'i':
  3588. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3589. {// int ( params and constants are always int )
  3590. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3591. script_reportdata(data);
  3592. invalid++;
  3593. }
  3594. break;
  3595. case 'r':
  3596. if( !data_isreference(data) )
  3597. {// variables
  3598. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3599. script_reportdata(data);
  3600. invalid++;
  3601. }
  3602. break;
  3603. case 'l':
  3604. if( !data_islabel(data) && !data_isfunclabel(data) )
  3605. {// label
  3606. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3607. script_reportdata(data);
  3608. invalid++;
  3609. }
  3610. break;
  3611. }
  3612. }
  3613. }
  3614. if(invalid)
  3615. {
  3616. ShowDebug("Function: %s\n", get_str(func));
  3617. script_reportsrc(st);
  3618. }
  3619. }
  3620. /// Executes a buildin command.
  3621. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3622. int run_func(struct script_state *st)
  3623. {
  3624. struct script_data* data;
  3625. int i,start_sp,end_sp,func;
  3626. end_sp = st->stack->sp;// position after the last argument
  3627. for( i = end_sp-1; i > 0 ; --i )
  3628. if( st->stack->stack_data[i].type == C_ARG )
  3629. break;
  3630. if( i == 0 ) {
  3631. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3632. st->state = END;
  3633. script_reportsrc(st);
  3634. return 1;
  3635. }
  3636. start_sp = i-1;// C_NAME of the command
  3637. st->start = start_sp;
  3638. st->end = end_sp;
  3639. data = &st->stack->stack_data[st->start];
  3640. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3641. func = (int)data->u.num;
  3642. st->funcname = reference_getname(data);
  3643. } else {
  3644. ShowError("script:run_func: not a buildin command.\n");
  3645. script_reportdata(data);
  3646. script_reportsrc(st);
  3647. st->state = END;
  3648. return 1;
  3649. }
  3650. if( script_config.warn_func_mismatch_argtypes ) {
  3651. script_check_buildin_argtype(st, func);
  3652. }
  3653. if(str_data[func].func) {
  3654. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3655. if( buildin_func[str_data[func].val].deprecated ){
  3656. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3657. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3658. script_reportsrc(st);
  3659. }
  3660. #endif
  3661. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3662. script_reportsrc(st);
  3663. } else {
  3664. 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));
  3665. script_reportsrc(st);
  3666. st->state = END;
  3667. }
  3668. // Stack's datum are used when re-running functions [Eoe]
  3669. if( st->state == RERUNLINE )
  3670. return 0;
  3671. pop_stack(st, st->start, st->end);
  3672. if( st->state == RETFUNC ) {// return from a user-defined function
  3673. struct script_retinfo* ri;
  3674. int olddefsp = st->stack->defsp;
  3675. int nargs;
  3676. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3677. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3678. {
  3679. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3680. script_reportsrc(st);
  3681. st->state = END;
  3682. return 1;
  3683. }
  3684. script_free_vars(st->stack->scope.vars);
  3685. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3686. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3687. nargs = ri->nargs;
  3688. st->pos = ri->pos;
  3689. st->script = ri->script;
  3690. st->stack->scope.vars = ri->scope.vars;
  3691. st->stack->scope.arrays = ri->scope.arrays;
  3692. st->stack->defsp = ri->defsp;
  3693. memset(ri, 0, sizeof(struct script_retinfo));
  3694. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3695. st->state = GOTO;
  3696. }
  3697. return 0;
  3698. }
  3699. /*==========================================
  3700. * script execution
  3701. *------------------------------------------*/
  3702. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3703. {
  3704. struct script_state *st;
  3705. if( rootscript == NULL || pos < 0 )
  3706. return;
  3707. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3708. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3709. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3710. st = script_alloc_state(rootscript, pos, rid, oid);
  3711. run_script_main(st);
  3712. }
  3713. /**
  3714. * Free all related script code
  3715. * @param code: Script code to free
  3716. */
  3717. void script_stop_scriptinstances(struct script_code *code) {
  3718. DBIterator *iter;
  3719. struct script_state* st;
  3720. if( !active_scripts )
  3721. return; // Don't even bother.
  3722. iter = db_iterator(st_db);
  3723. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3724. if( st->script == code )
  3725. script_free_state(st);
  3726. }
  3727. dbi_destroy(iter);
  3728. }
  3729. /*==========================================
  3730. * Timer function for sleep
  3731. *------------------------------------------*/
  3732. TIMER_FUNC(run_script_timer){
  3733. struct script_state *st = (struct script_state *)data;
  3734. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3735. // If it was a player before going to sleep and there is still a unit attached to the script
  3736. if( id != 0 && st->rid ){
  3737. struct map_session_data *sd = map_id2sd(st->rid);
  3738. // Attached player is offline(logout) or another unit type(should not happen)
  3739. if( !sd ){
  3740. st->rid = 0;
  3741. st->state = END;
  3742. // Character mismatch. Cancel execution.
  3743. }else if( sd->status.char_id != id ){
  3744. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3745. script_reportsrc(st);
  3746. st->rid = 0;
  3747. st->state = END;
  3748. }
  3749. }
  3750. while (node && st->sleep.timer != INVALID_TIMER) {
  3751. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3752. script_erase_sleepdb(node);
  3753. st->sleep.timer = INVALID_TIMER;
  3754. break;
  3755. }
  3756. node = node->next;
  3757. }
  3758. if(st->state != RERUNLINE)
  3759. st->sleep.tick = 0;
  3760. run_script_main(st);
  3761. return 0;
  3762. }
  3763. /**
  3764. * Remove sleep timers from the NPC
  3765. * @param id: NPC ID
  3766. */
  3767. void script_stop_sleeptimers(int id) {
  3768. for (;;) {
  3769. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3770. if (!st)
  3771. break; // No more sleep timers
  3772. if (st->oid == id)
  3773. script_free_state(st);
  3774. }
  3775. }
  3776. /**
  3777. * Delete the specified node from sleep_db
  3778. * @param n: Linked list of sleep timers
  3779. */
  3780. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3781. struct linkdb_node *retnode;
  3782. if (!n)
  3783. return NULL;
  3784. if (!n->prev)
  3785. sleep_db = n->next;
  3786. else
  3787. n->prev->next = n->next;
  3788. if (n->next)
  3789. n->next->prev = n->prev;
  3790. retnode = n->next;
  3791. aFree(n);
  3792. return retnode;
  3793. }
  3794. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3795. ///
  3796. /// @param st Script state to detach.
  3797. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3798. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3799. {
  3800. struct map_session_data* sd;
  3801. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3802. if( sd->state.using_fake_npc ){
  3803. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, sd->fd );
  3804. sd->state.using_fake_npc = 0;
  3805. }
  3806. sd->st = st->bk_st;
  3807. sd->npc_id = st->bk_npcid;
  3808. sd->state.disable_atcommand_on_npc = 0;
  3809. if(st->bk_st) {
  3810. //Remove tag for removal.
  3811. st->bk_st = NULL;
  3812. st->bk_npcid = 0;
  3813. } else if(dequeue_event) {
  3814. #ifdef SECURE_NPCTIMEOUT
  3815. /**
  3816. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3817. **/
  3818. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3819. delete_timer(sd->npc_idle_timer,npc_secure_timeout_timer);
  3820. sd->npc_idle_timer = INVALID_TIMER;
  3821. }
  3822. #endif
  3823. npc_event_dequeue(sd);
  3824. }
  3825. }
  3826. else if(st->bk_st)
  3827. {// rid was set to 0, before detaching the script state
  3828. 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);
  3829. script_reportsrc(st->bk_st);
  3830. script_free_state(st->bk_st);
  3831. st->bk_st = NULL;
  3832. }
  3833. }
  3834. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3835. ///
  3836. /// @param st Script state to attach.
  3837. void script_attach_state(struct script_state* st){
  3838. struct map_session_data* sd;
  3839. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3840. {
  3841. if(st!=sd->st)
  3842. {
  3843. if(st->bk_st)
  3844. {// there is already a backup
  3845. 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);
  3846. }
  3847. st->bk_st = sd->st;
  3848. st->bk_npcid = sd->npc_id;
  3849. }
  3850. sd->st = st;
  3851. sd->npc_id = st->oid;
  3852. sd->npc_item_flag = st->npc_item_flag; // load default.
  3853. sd->state.disable_atcommand_on_npc = battle_config.atcommand_disable_npc && (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3854. #ifdef SECURE_NPCTIMEOUT
  3855. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  3856. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_secure_timeout_timer,sd->bl.id,0);
  3857. sd->npc_idle_tick = gettick();
  3858. #endif
  3859. }
  3860. }
  3861. /*==========================================
  3862. * The main part of the script execution
  3863. *------------------------------------------*/
  3864. void run_script_main(struct script_state *st)
  3865. {
  3866. int cmdcount = script_config.check_cmdcount;
  3867. int gotocount = script_config.check_gotocount;
  3868. TBL_PC *sd;
  3869. struct script_stack *stack = st->stack;
  3870. script_attach_state(st);
  3871. if(st->state == RERUNLINE) {
  3872. run_func(st);
  3873. if(st->state == GOTO)
  3874. st->state = RUN;
  3875. } else if(st->state != END)
  3876. st->state = RUN;
  3877. while(st->state == RUN) {
  3878. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3879. switch(c){
  3880. case C_EOL:
  3881. if( stack->defsp > stack->sp )
  3882. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3883. else
  3884. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3885. break;
  3886. case C_INT:
  3887. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3888. break;
  3889. case C_POS:
  3890. case C_NAME:
  3891. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3892. st->pos+=3;
  3893. break;
  3894. case C_ARG:
  3895. push_val(stack,c,0);
  3896. break;
  3897. case C_STR:
  3898. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3899. while(st->script->script_buf[st->pos++]);
  3900. break;
  3901. case C_FUNC:
  3902. run_func(st);
  3903. if(st->state==GOTO){
  3904. st->state = RUN;
  3905. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3906. ShowError("script:run_script_main: infinity loop !\n");
  3907. script_reportsrc(st);
  3908. st->state=END;
  3909. }
  3910. }
  3911. break;
  3912. case C_REF:
  3913. st->op2ref = 1;
  3914. break;
  3915. case C_NEG:
  3916. case C_NOT:
  3917. case C_LNOT:
  3918. op_1(st ,c);
  3919. break;
  3920. case C_ADD:
  3921. case C_SUB:
  3922. case C_MUL:
  3923. case C_DIV:
  3924. case C_MOD:
  3925. case C_EQ:
  3926. case C_NE:
  3927. case C_GT:
  3928. case C_GE:
  3929. case C_LT:
  3930. case C_LE:
  3931. case C_AND:
  3932. case C_OR:
  3933. case C_XOR:
  3934. case C_LAND:
  3935. case C_LOR:
  3936. case C_R_SHIFT:
  3937. case C_L_SHIFT:
  3938. op_2(st, c);
  3939. break;
  3940. case C_OP3:
  3941. op_3(st, c);
  3942. break;
  3943. case C_NOP:
  3944. st->state=END;
  3945. break;
  3946. default:
  3947. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3948. st->state=END;
  3949. break;
  3950. }
  3951. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3952. ShowError("script:run_script_main: infinity loop !\n");
  3953. script_reportsrc(st);
  3954. st->state=END;
  3955. }
  3956. }
  3957. if(st->sleep.tick > 0) {
  3958. //Restore previous script
  3959. script_detach_state(st, false);
  3960. //Delay execution
  3961. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3962. st->sleep.charid = sd?sd->status.char_id:0;
  3963. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3964. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3965. } else if(st->state != END && st->rid) {
  3966. //Resume later (st is already attached to player).
  3967. if(st->bk_st) {
  3968. ShowWarning("Unable to restore stack! Double continuation!\n");
  3969. //Report BOTH scripts to see if that can help somehow.
  3970. ShowDebug("Previous script (lost):\n");
  3971. script_reportsrc(st->bk_st);
  3972. ShowDebug("Current script:\n");
  3973. script_reportsrc(st);
  3974. script_free_state(st->bk_st);
  3975. st->bk_st = NULL;
  3976. }
  3977. } else {
  3978. if (st->stack && st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp - 1].type == C_RETINFO) {
  3979. for (int i = 0; i < st->stack->sp; i++) {
  3980. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  3981. st->script = st->stack->stack_data[i].u.ri->script;
  3982. break;
  3983. }
  3984. }
  3985. }
  3986. //Dispose of script.
  3987. if ((sd = map_id2sd(st->rid))!=NULL)
  3988. { //Restore previous stack and save char.
  3989. if(sd->state.using_fake_npc){
  3990. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3991. sd->state.using_fake_npc = 0;
  3992. }
  3993. //Restore previous script if any.
  3994. script_detach_state(st, true);
  3995. if (sd->vars_dirty)
  3996. intif_saveregistry(sd);
  3997. }
  3998. script_free_state(st);
  3999. }
  4000. }
  4001. int script_config_read(const char *cfgName)
  4002. {
  4003. int i;
  4004. char line[1024],w1[1024],w2[1024];
  4005. FILE *fp;
  4006. fp=fopen(cfgName,"r");
  4007. if(fp==NULL){
  4008. ShowError("File not found: %s\n", cfgName);
  4009. return 1;
  4010. }
  4011. while(fgets(line, sizeof(line), fp))
  4012. {
  4013. if(line[0] == '/' && line[1] == '/')
  4014. continue;
  4015. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  4016. if(i!=2)
  4017. continue;
  4018. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  4019. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  4020. }
  4021. else if(strcmpi(w1,"check_cmdcount")==0) {
  4022. script_config.check_cmdcount = config_switch(w2);
  4023. }
  4024. else if(strcmpi(w1,"check_gotocount")==0) {
  4025. script_config.check_gotocount = config_switch(w2);
  4026. }
  4027. else if(strcmpi(w1,"input_min_value")==0) {
  4028. script_config.input_min_value = config_switch(w2);
  4029. }
  4030. else if(strcmpi(w1,"input_max_value")==0) {
  4031. script_config.input_max_value = config_switch(w2);
  4032. }
  4033. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  4034. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  4035. }
  4036. else if(strcmpi(w1,"import")==0){
  4037. script_config_read(w2);
  4038. }
  4039. else {
  4040. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  4041. }
  4042. }
  4043. fclose(fp);
  4044. return 0;
  4045. }
  4046. /**
  4047. * @see DBApply
  4048. */
  4049. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  4050. {
  4051. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  4052. if (code)
  4053. script_free_code(code);
  4054. return 0;
  4055. }
  4056. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  4057. {
  4058. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4059. if( script )
  4060. {
  4061. int j;
  4062. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4063. if( j < EQI_MAX ) {
  4064. //Single item autobonus
  4065. current_equip_item_index = sd->equip_index[j];
  4066. current_equip_combo_pos = 0;
  4067. } else {
  4068. //Combo autobonus
  4069. current_equip_item_index = -1;
  4070. current_equip_combo_pos = pos;
  4071. }
  4072. run_script(script,0,sd->bl.id,0);
  4073. }
  4074. }
  4075. void script_add_autobonus(const char *autobonus)
  4076. {
  4077. if( strdb_get(autobonus_db, autobonus) == NULL )
  4078. {
  4079. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4080. if( script )
  4081. strdb_put(autobonus_db, autobonus, script);
  4082. }
  4083. }
  4084. void script_run_petautobonus(const std::string &autobonus, map_session_data &sd) {
  4085. std::shared_ptr<s_pet_autobonus_wrapper> script = util::umap_find(pet_autobonuses, autobonus);
  4086. if (script != nullptr) {
  4087. run_script(script->script, 0, sd.bl.id, 0);
  4088. }
  4089. }
  4090. void script_add_petautobonus(const std::string &autobonus) {
  4091. if (util::umap_find(pet_autobonuses, autobonus) == nullptr) {
  4092. script_code *script = parse_script(autobonus.c_str(), "petautobonus", 0, 0);
  4093. if (script != nullptr) {
  4094. std::shared_ptr<s_pet_autobonus_wrapper> bonus = std::make_shared<s_pet_autobonus_wrapper>();
  4095. bonus->script = script;
  4096. pet_autobonuses.emplace(autobonus, bonus);
  4097. }
  4098. }
  4099. }
  4100. /// resets a temporary character array variable to given value
  4101. void script_cleararray_pc( struct map_session_data* sd, const char* varname ){
  4102. struct script_array *sa = NULL;
  4103. struct reg_db *src = NULL;
  4104. unsigned int i, *list = NULL, size = 0;
  4105. int key;
  4106. key = add_str(varname);
  4107. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4108. return;
  4109. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4110. return;
  4111. size = sa->size;
  4112. list = script_array_cpy_list(sa);
  4113. for( i = 0; i < size; i++ ){
  4114. clear_reg( NULL, sd, reference_uid( key, list[i] ), varname, NULL );
  4115. }
  4116. }
  4117. /// sets a temporary character array variable element idx to given value
  4118. /// @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.
  4119. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, int64 value, int* refcache)
  4120. {
  4121. int key;
  4122. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4123. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4124. return;
  4125. }
  4126. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4127. set_reg_num( NULL, sd, reference_uid( key, idx ), varname, value, NULL );
  4128. if( refcache ) {
  4129. // save to avoid repeated script->add_str calls
  4130. refcache[0] = key;
  4131. }
  4132. }
  4133. /**
  4134. * Clears persistent variables from memory
  4135. **/
  4136. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4137. {
  4138. struct script_reg_state *src;
  4139. if( data->type != DB_DATA_PTR ) // got no need for those!
  4140. return 0;
  4141. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4142. if( src->type ) {
  4143. struct script_reg_str *p = (struct script_reg_str *)src;
  4144. if( p->value )
  4145. aFree(p->value);
  4146. ers_free(str_reg_ers,p);
  4147. } else {
  4148. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4149. }
  4150. return 0;
  4151. }
  4152. /**
  4153. * Clears a single persistent variable
  4154. **/
  4155. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4156. {
  4157. i64db_remove(sd->regs.vars, reg);
  4158. if( data->type ) {
  4159. struct script_reg_str *p = (struct script_reg_str*)data;
  4160. if( p->value )
  4161. aFree(p->value);
  4162. ers_free(str_reg_ers,p);
  4163. } else {
  4164. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4165. }
  4166. }
  4167. unsigned int *script_array_cpy_list(struct script_array *sa)
  4168. {
  4169. if( sa->size > generic_ui_array_size )
  4170. script_generic_ui_array_expand(sa->size);
  4171. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4172. return generic_ui_array;
  4173. }
  4174. void script_generic_ui_array_expand (unsigned int plus)
  4175. {
  4176. generic_ui_array_size += plus + 100;
  4177. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4178. }
  4179. /*==========================================
  4180. * Destructor
  4181. *------------------------------------------*/
  4182. void do_final_script() {
  4183. int i;
  4184. DBIterator *iter;
  4185. struct script_state *st;
  4186. #ifdef DEBUG_HASH
  4187. if (battle_config.etc_log)
  4188. {
  4189. FILE *fp = fopen("hash_dump.txt","wt");
  4190. if(fp) {
  4191. int count[SCRIPT_HASH_SIZE];
  4192. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4193. int n=0;
  4194. int min=INT_MAX,max=0,zero=0;
  4195. double mean=0.0f;
  4196. double median=0.0f;
  4197. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4198. memset(count, 0, sizeof(count));
  4199. fprintf(fp,"num : hash : data_name\n");
  4200. fprintf(fp,"---------------------------------------------------------------\n");
  4201. for(i=LABEL_START; i<str_num; i++) {
  4202. unsigned int h = calc_hash(get_str(i));
  4203. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4204. ++count[h];
  4205. }
  4206. fprintf(fp,"--------------------\n\n");
  4207. memset(count2, 0, sizeof(count2));
  4208. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4209. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4210. if(min > count[i])
  4211. min = count[i]; // minimun count of collision
  4212. if(max < count[i])
  4213. max = count[i]; // maximun count of collision
  4214. if(count[i] == 0)
  4215. zero++;
  4216. ++count2[count[i]];
  4217. }
  4218. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4219. for( i=min; i <= max; ++i ){
  4220. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4221. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4222. }
  4223. for( i=min; i <= max; ++i ){
  4224. n += count2[i];
  4225. if( n*2 >= SCRIPT_HASH_SIZE )
  4226. {
  4227. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4228. median = (i+i+1)/2.0f;
  4229. else
  4230. median = i;
  4231. break;
  4232. }
  4233. }
  4234. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4235. fclose(fp);
  4236. }
  4237. }
  4238. #endif
  4239. mapreg_final();
  4240. db_destroy(scriptlabel_db);
  4241. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4242. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4243. ers_destroy(array_ers);
  4244. if (generic_ui_array)
  4245. aFree(generic_ui_array);
  4246. iter = db_iterator(st_db);
  4247. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4248. script_free_state(st);
  4249. dbi_destroy(iter);
  4250. if (str_data)
  4251. aFree(str_data);
  4252. if (str_buf)
  4253. aFree(str_buf);
  4254. for( i = 0; i < atcmd_binding_count; i++ ) {
  4255. aFree(atcmd_binding[i]);
  4256. }
  4257. if( atcmd_binding_count != 0 )
  4258. aFree(atcmd_binding);
  4259. ers_destroy(st_ers);
  4260. ers_destroy(stack_ers);
  4261. db_destroy(st_db);
  4262. }
  4263. /*==========================================
  4264. * Initialization
  4265. *------------------------------------------*/
  4266. void do_init_script(void) {
  4267. st_db = idb_alloc(DB_OPT_BASE);
  4268. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4269. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4270. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4271. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4272. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4273. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4274. ers_chunk_size(st_ers, 10);
  4275. ers_chunk_size(stack_ers, 10);
  4276. active_scripts = 0;
  4277. next_id = 0;
  4278. mapreg_init();
  4279. add_buildin_func();
  4280. constant_db.load();
  4281. script_hardcoded_constants();
  4282. }
  4283. void script_reload(void) {
  4284. int i;
  4285. DBIterator *iter;
  4286. struct script_state *st;
  4287. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4288. db_clear(scriptlabel_db);
  4289. // @commands (script based)
  4290. // Clear bindings
  4291. for( i = 0; i < atcmd_binding_count; i++ ) {
  4292. aFree(atcmd_binding[i]);
  4293. }
  4294. if( atcmd_binding_count != 0 )
  4295. aFree(atcmd_binding);
  4296. atcmd_binding_count = 0;
  4297. iter = db_iterator(st_db);
  4298. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4299. script_free_state(st);
  4300. dbi_destroy(iter);
  4301. db_clear(st_db);
  4302. mapreg_reload();
  4303. }
  4304. //-----------------------------------------------------------------------------
  4305. // buildin functions
  4306. //
  4307. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4308. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4309. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4310. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4311. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4312. /////////////////////////////////////////////////////////////////////
  4313. // NPC interaction
  4314. //
  4315. /// Appends a message to the npc dialog.
  4316. /// If a dialog doesn't exist yet, one is created.
  4317. ///
  4318. /// mes "<message>";
  4319. BUILDIN_FUNC(mes)
  4320. {
  4321. struct map_session_data* sd;
  4322. if( !script_rid2sd(sd) )
  4323. return SCRIPT_CMD_SUCCESS;
  4324. if( !script_hasdata(st, 3) )
  4325. {// only a single line detected in the script
  4326. clif_scriptmes( *sd, st->oid, script_getstr( st, 2 ) );
  4327. }
  4328. else
  4329. {// parse multiple lines as they exist
  4330. int i;
  4331. for( i = 2; script_hasdata(st, i); i++ )
  4332. {
  4333. // send the message to the client
  4334. clif_scriptmes( *sd, st->oid, script_getstr( st, i ) );
  4335. }
  4336. }
  4337. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4338. return SCRIPT_CMD_SUCCESS;
  4339. }
  4340. /// Displays the button 'next' in the npc dialog.
  4341. /// The dialog text is cleared and the script continues when the button is pressed.
  4342. ///
  4343. /// next;
  4344. BUILDIN_FUNC(next)
  4345. {
  4346. struct map_session_data* sd;
  4347. if (!st->mes_active) {
  4348. ShowWarning("buildin_next: There is no mes active.\n");
  4349. return SCRIPT_CMD_FAILURE;
  4350. }
  4351. if( !script_rid2sd(sd) )
  4352. return SCRIPT_CMD_SUCCESS;
  4353. #ifdef SECURE_NPCTIMEOUT
  4354. sd->npc_idle_type = NPCT_WAIT;
  4355. #endif
  4356. st->state = STOP;
  4357. clif_scriptnext( *sd, st->oid );
  4358. return SCRIPT_CMD_SUCCESS;
  4359. }
  4360. /// Clears the dialog and continues the script without a next button.
  4361. ///
  4362. /// clear;
  4363. BUILDIN_FUNC(clear)
  4364. {
  4365. TBL_PC* sd;
  4366. if (!st->mes_active) {
  4367. ShowWarning("buildin_clear: There is no mes active.\n");
  4368. return SCRIPT_CMD_FAILURE;
  4369. }
  4370. if (!script_rid2sd(sd))
  4371. return SCRIPT_CMD_FAILURE;
  4372. clif_scriptclear(sd, st->oid);
  4373. return SCRIPT_CMD_SUCCESS;
  4374. }
  4375. /// Ends the script and displays the button 'close' on the npc dialog.
  4376. /// The dialog is closed when the button is pressed.
  4377. ///
  4378. /// close;
  4379. BUILDIN_FUNC(close)
  4380. {
  4381. TBL_PC* sd;
  4382. if( !script_rid2sd(sd) )
  4383. return SCRIPT_CMD_SUCCESS;
  4384. if( !st->mes_active ) {
  4385. st->state = END; // Keep backwards compatibility.
  4386. ShowWarning("Incorrect use of 'close' command!\n");
  4387. script_reportsrc(st);
  4388. } else {
  4389. st->state = CLOSE;
  4390. st->mes_active = 0;
  4391. }
  4392. clif_scriptclose(sd, st->oid);
  4393. return SCRIPT_CMD_SUCCESS;
  4394. }
  4395. /// Displays the button 'close' on the npc dialog.
  4396. /// The dialog is closed and the script continues when the button is pressed.
  4397. ///
  4398. /// close2;
  4399. BUILDIN_FUNC(close2)
  4400. {
  4401. TBL_PC* sd;
  4402. if( !script_rid2sd(sd) )
  4403. return SCRIPT_CMD_SUCCESS;
  4404. st->state = STOP;
  4405. if( st->mes_active )
  4406. st->mes_active = 0;
  4407. clif_scriptclose(sd, st->oid);
  4408. return SCRIPT_CMD_SUCCESS;
  4409. }
  4410. /// Counts the number of valid and total number of options in 'str'
  4411. /// If max_count > 0 the counting stops when that valid option is reached
  4412. /// total is incremented for each option (NULL is supported)
  4413. static int menu_countoptions(const char* str, int max_count, int* total)
  4414. {
  4415. int count = 0;
  4416. int bogus_total;
  4417. if( total == NULL )
  4418. total = &bogus_total;
  4419. ++(*total);
  4420. // initial empty options
  4421. while( *str == ':' )
  4422. {
  4423. ++str;
  4424. ++(*total);
  4425. }
  4426. // count menu options
  4427. while( *str != '\0' )
  4428. {
  4429. ++count;
  4430. --max_count;
  4431. if( max_count == 0 )
  4432. break;
  4433. while( *str != ':' && *str != '\0' )
  4434. ++str;
  4435. while( *str == ':' )
  4436. {
  4437. ++str;
  4438. ++(*total);
  4439. }
  4440. }
  4441. return count;
  4442. }
  4443. /// Displays a menu with options and goes to the target label.
  4444. /// The script is stopped if cancel is pressed.
  4445. /// Options with no text are not displayed in the client.
  4446. ///
  4447. /// Options can be grouped together, separated by the character ':' in the text:
  4448. /// ex: menu "A:B:C",L_target;
  4449. /// All these options go to the specified target label.
  4450. ///
  4451. /// The index of the selected option is put in the variable @menu.
  4452. /// Indexes start with 1 and are consistent with grouped and empty options.
  4453. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4454. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4455. ///
  4456. /// NOTE: the client closes the npc dialog when cancel is pressed
  4457. ///
  4458. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4459. BUILDIN_FUNC(menu)
  4460. {
  4461. int i;
  4462. const char* text;
  4463. TBL_PC* sd;
  4464. if( !script_rid2sd(sd) )
  4465. return SCRIPT_CMD_SUCCESS;
  4466. #ifdef SECURE_NPCTIMEOUT
  4467. sd->npc_idle_type = NPCT_MENU;
  4468. #endif
  4469. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4470. if( sd->state.menu_or_input == 0 )
  4471. {
  4472. struct StringBuf buf;
  4473. if( script_lastdata(st) % 2 == 0 )
  4474. {// argument count is not even (1st argument is at index 2)
  4475. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4476. st->state = END;
  4477. return SCRIPT_CMD_FAILURE;
  4478. }
  4479. StringBuf_Init(&buf);
  4480. sd->npc_menu = 0;
  4481. for( i = 2; i < script_lastdata(st); i += 2 )
  4482. {
  4483. struct script_data* data;
  4484. // menu options
  4485. text = script_getstr(st, i);
  4486. // target label
  4487. data = script_getdata(st, i+1);
  4488. if( !data_islabel(data) )
  4489. {// not a label
  4490. StringBuf_Destroy(&buf);
  4491. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4492. script_reportdata(data);
  4493. st->state = END;
  4494. return SCRIPT_CMD_FAILURE;
  4495. }
  4496. // append option(s)
  4497. if( text[0] == '\0' )
  4498. continue;// empty string, ignore
  4499. if( sd->npc_menu > 0 )
  4500. StringBuf_AppendStr(&buf, ":");
  4501. StringBuf_AppendStr(&buf, text);
  4502. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4503. }
  4504. st->state = RERUNLINE;
  4505. sd->state.menu_or_input = 1;
  4506. /**
  4507. * menus beyond this length crash the client (see bugreport:6402)
  4508. **/
  4509. if( StringBuf_Length(&buf) >= 2047 ) {
  4510. struct npc_data * nd = map_id2nd(st->oid);
  4511. char* menu;
  4512. CREATE(menu, char, 2048);
  4513. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4514. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4515. clif_scriptmenu(sd, st->oid, menu);
  4516. aFree(menu);
  4517. } else
  4518. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4519. StringBuf_Destroy(&buf);
  4520. if( sd->npc_menu >= 0xff )
  4521. {// 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
  4522. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4523. script_reportsrc(st);
  4524. }
  4525. }
  4526. else if( sd->npc_menu == 0xff )
  4527. {// Cancel was pressed
  4528. sd->state.menu_or_input = 0;
  4529. st->state = END;
  4530. }
  4531. else
  4532. {// goto target label
  4533. int menu = 0;
  4534. sd->state.menu_or_input = 0;
  4535. if( sd->npc_menu <= 0 )
  4536. {
  4537. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4538. st->state = END;
  4539. return SCRIPT_CMD_FAILURE;
  4540. }
  4541. // get target label
  4542. for( i = 2; i < script_lastdata(st); i += 2 )
  4543. {
  4544. text = script_getstr(st, i);
  4545. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4546. if( sd->npc_menu <= 0 )
  4547. break;// entry found
  4548. }
  4549. if( sd->npc_menu > 0 )
  4550. {// Invalid selection
  4551. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4552. st->state = END;
  4553. return SCRIPT_CMD_FAILURE;
  4554. }
  4555. if( !data_islabel(script_getdata(st, i + 1)) )
  4556. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4557. ShowError("buildin_menu: Unexpected data in label argument\n");
  4558. script_reportdata(script_getdata(st, i + 1));
  4559. st->state = END;
  4560. return SCRIPT_CMD_FAILURE;
  4561. }
  4562. pc_setreg(sd, add_str("@menu"), menu);
  4563. st->pos = script_getnum(st, i + 1);
  4564. st->state = GOTO;
  4565. }
  4566. return SCRIPT_CMD_SUCCESS;
  4567. }
  4568. /// Displays a menu with options and returns the selected option.
  4569. /// Behaves like 'menu' without the target labels.
  4570. ///
  4571. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4572. ///
  4573. /// @see menu
  4574. BUILDIN_FUNC(select)
  4575. {
  4576. int i;
  4577. const char* text;
  4578. TBL_PC* sd;
  4579. if( !script_rid2sd(sd) )
  4580. return SCRIPT_CMD_SUCCESS;
  4581. #ifdef SECURE_NPCTIMEOUT
  4582. sd->npc_idle_type = NPCT_MENU;
  4583. #endif
  4584. if( sd->state.menu_or_input == 0 ) {
  4585. struct StringBuf buf;
  4586. StringBuf_Init(&buf);
  4587. sd->npc_menu = 0;
  4588. for( i = 2; i <= script_lastdata(st); ++i ) {
  4589. text = script_getstr(st, i);
  4590. if( sd->npc_menu > 0 )
  4591. StringBuf_AppendStr(&buf, ":");
  4592. StringBuf_AppendStr(&buf, text);
  4593. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4594. }
  4595. st->state = RERUNLINE;
  4596. sd->state.menu_or_input = 1;
  4597. /**
  4598. * menus beyond this length crash the client (see bugreport:6402)
  4599. **/
  4600. if( StringBuf_Length(&buf) >= 2047 ) {
  4601. struct npc_data * nd = map_id2nd(st->oid);
  4602. char* menu;
  4603. CREATE(menu, char, 2048);
  4604. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4605. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4606. clif_scriptmenu(sd, st->oid, menu);
  4607. aFree(menu);
  4608. } else
  4609. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4610. StringBuf_Destroy(&buf);
  4611. if( sd->npc_menu >= 0xff ) {
  4612. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4613. script_reportsrc(st);
  4614. }
  4615. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4616. sd->state.menu_or_input = 0;
  4617. st->state = END;
  4618. } else {// return selected option
  4619. int menu = 0;
  4620. sd->state.menu_or_input = 0;
  4621. for( i = 2; i <= script_lastdata(st); ++i ) {
  4622. text = script_getstr(st, i);
  4623. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4624. if( sd->npc_menu <= 0 )
  4625. break;// entry found
  4626. }
  4627. pc_setreg(sd, add_str("@menu"), menu);
  4628. script_pushint(st, menu);
  4629. st->state = RUN;
  4630. }
  4631. return SCRIPT_CMD_SUCCESS;
  4632. }
  4633. /// Displays a menu with options and returns the selected option.
  4634. /// Behaves like 'menu' without the target labels, except when cancel is
  4635. /// pressed.
  4636. /// When cancel is pressed, the script continues and 255 is returned.
  4637. ///
  4638. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4639. ///
  4640. /// @see menu
  4641. BUILDIN_FUNC(prompt)
  4642. {
  4643. int i;
  4644. const char *text;
  4645. TBL_PC* sd;
  4646. if( !script_rid2sd(sd) )
  4647. return SCRIPT_CMD_SUCCESS;
  4648. #ifdef SECURE_NPCTIMEOUT
  4649. sd->npc_idle_type = NPCT_MENU;
  4650. #endif
  4651. if( sd->state.menu_or_input == 0 )
  4652. {
  4653. struct StringBuf buf;
  4654. StringBuf_Init(&buf);
  4655. sd->npc_menu = 0;
  4656. for( i = 2; i <= script_lastdata(st); ++i )
  4657. {
  4658. text = script_getstr(st, i);
  4659. if( sd->npc_menu > 0 )
  4660. StringBuf_AppendStr(&buf, ":");
  4661. StringBuf_AppendStr(&buf, text);
  4662. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4663. }
  4664. st->state = RERUNLINE;
  4665. sd->state.menu_or_input = 1;
  4666. /**
  4667. * menus beyond this length crash the client (see bugreport:6402)
  4668. **/
  4669. if( StringBuf_Length(&buf) >= 2047 ) {
  4670. struct npc_data * nd = map_id2nd(st->oid);
  4671. char* menu;
  4672. CREATE(menu, char, 2048);
  4673. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4674. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4675. clif_scriptmenu(sd, st->oid, menu);
  4676. aFree(menu);
  4677. } else
  4678. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4679. StringBuf_Destroy(&buf);
  4680. if( sd->npc_menu >= 0xff )
  4681. {
  4682. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4683. script_reportsrc(st);
  4684. }
  4685. }
  4686. else if( sd->npc_menu == 0xff )
  4687. {// Cancel was pressed
  4688. sd->state.menu_or_input = 0;
  4689. pc_setreg(sd, add_str("@menu"), 0xff);
  4690. script_pushint(st, 0xff);
  4691. st->state = RUN;
  4692. }
  4693. else
  4694. {// return selected option
  4695. int menu = 0;
  4696. sd->state.menu_or_input = 0;
  4697. for( i = 2; i <= script_lastdata(st); ++i )
  4698. {
  4699. text = script_getstr(st, i);
  4700. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4701. if( sd->npc_menu <= 0 )
  4702. break;// entry found
  4703. }
  4704. pc_setreg(sd, add_str("@menu"), menu);
  4705. script_pushint(st, menu);
  4706. st->state = RUN;
  4707. }
  4708. return SCRIPT_CMD_SUCCESS;
  4709. }
  4710. /////////////////////////////////////////////////////////////////////
  4711. // ...
  4712. //
  4713. /// Jumps to the target script label.
  4714. ///
  4715. /// goto <label>;
  4716. BUILDIN_FUNC(goto)
  4717. {
  4718. if( !data_islabel(script_getdata(st,2)) )
  4719. {
  4720. ShowError("buildin_goto: Not a label\n");
  4721. script_reportdata(script_getdata(st,2));
  4722. st->state = END;
  4723. return SCRIPT_CMD_FAILURE;
  4724. }
  4725. st->pos = script_getnum(st,2);
  4726. st->state = GOTO;
  4727. return SCRIPT_CMD_SUCCESS;
  4728. }
  4729. /*==========================================
  4730. * user-defined function call
  4731. *------------------------------------------*/
  4732. BUILDIN_FUNC(callfunc)
  4733. {
  4734. int i, j;
  4735. struct script_retinfo* ri;
  4736. struct script_code* scr;
  4737. const char* str = script_getstr(st,2);
  4738. struct reg_db *ref = NULL;
  4739. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4740. if(!scr) {
  4741. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4742. st->state = END;
  4743. return SCRIPT_CMD_FAILURE;
  4744. }
  4745. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4746. ref[0].vars = st->stack->scope.vars;
  4747. if (!st->stack->scope.arrays)
  4748. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4749. ref[0].arrays = st->stack->scope.arrays;
  4750. ref[1].vars = st->script->local.vars;
  4751. if (!st->script->local.arrays)
  4752. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4753. ref[1].arrays = st->script->local.arrays;
  4754. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4755. struct script_data* data = push_copy(st->stack,i);
  4756. if (data_isreference(data) && !data->ref) {
  4757. const char* name = reference_getname(data);
  4758. if (name[0] == '.')
  4759. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4760. }
  4761. }
  4762. CREATE(ri, struct script_retinfo, 1);
  4763. ri->script = st->script; // script code
  4764. ri->scope.vars = st->stack->scope.vars; // scope variables
  4765. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4766. ri->pos = st->pos; // script location
  4767. ri->nargs = j; // argument count
  4768. ri->defsp = st->stack->defsp; // default stack pointer
  4769. push_retinfo(st->stack, ri, ref);
  4770. st->pos = 0;
  4771. st->script = scr;
  4772. st->stack->defsp = st->stack->sp;
  4773. st->state = GOTO;
  4774. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4775. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4776. if (!st->script->local.vars)
  4777. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4778. return SCRIPT_CMD_SUCCESS;
  4779. }
  4780. /*==========================================
  4781. * subroutine call
  4782. *------------------------------------------*/
  4783. BUILDIN_FUNC(callsub)
  4784. {
  4785. int i,j;
  4786. struct script_retinfo* ri;
  4787. int pos = script_getnum(st,2);
  4788. struct reg_db *ref = NULL;
  4789. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4790. ShowError("buildin_callsub: Argument is not a label\n");
  4791. script_reportdata(script_getdata(st,2));
  4792. st->state = END;
  4793. return SCRIPT_CMD_FAILURE;
  4794. }
  4795. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4796. ref[0].vars = st->stack->scope.vars;
  4797. if (!st->stack->scope.arrays)
  4798. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4799. ref[0].arrays = st->stack->scope.arrays;
  4800. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4801. struct script_data* data = push_copy(st->stack,i);
  4802. if (data_isreference(data) && !data->ref) {
  4803. const char* name = reference_getname(data);
  4804. if (name[0] == '.' && name[1] == '@')
  4805. data->ref = &ref[0];
  4806. }
  4807. }
  4808. CREATE(ri, struct script_retinfo, 1);
  4809. ri->script = st->script; // script code
  4810. ri->scope.vars = st->stack->scope.vars; // scope variables
  4811. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4812. ri->pos = st->pos; // script location
  4813. ri->nargs = j; // argument count
  4814. ri->defsp = st->stack->defsp; // default stack pointer
  4815. push_retinfo(st->stack, ri, ref);
  4816. st->pos = pos;
  4817. st->stack->defsp = st->stack->sp;
  4818. st->state = GOTO;
  4819. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4820. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4821. return SCRIPT_CMD_SUCCESS;
  4822. }
  4823. /// Retrieves an argument provided to callfunc/callsub.
  4824. /// If the argument doesn't exist
  4825. ///
  4826. /// getarg(<index>{,<default_value>}) -> <value>
  4827. BUILDIN_FUNC(getarg)
  4828. {
  4829. struct script_retinfo* ri;
  4830. int idx;
  4831. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4832. {
  4833. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4834. st->state = END;
  4835. return SCRIPT_CMD_FAILURE;
  4836. }
  4837. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4838. idx = script_getnum(st,2);
  4839. if( idx >= 0 && idx < ri->nargs )
  4840. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4841. else if( script_hasdata(st,3) )
  4842. script_pushcopy(st, 3);
  4843. else
  4844. {
  4845. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4846. st->state = END;
  4847. return SCRIPT_CMD_FAILURE;
  4848. }
  4849. return SCRIPT_CMD_SUCCESS;
  4850. }
  4851. /// Returns from the current function, optionaly returning a value from the functions.
  4852. /// Don't use outside script functions.
  4853. ///
  4854. /// return;
  4855. /// return <value>;
  4856. BUILDIN_FUNC(return)
  4857. {
  4858. if( script_hasdata(st,2) )
  4859. {// return value
  4860. struct script_data* data;
  4861. script_pushcopy(st, 2);
  4862. data = script_getdatatop(st, -1);
  4863. if( data_isreference(data) ) {
  4864. const char* name = reference_getname(data);
  4865. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4866. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4867. get_val(st, data); // current scope, convert to value
  4868. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4869. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4870. }
  4871. }
  4872. }
  4873. else
  4874. {// no return value
  4875. script_pushnil(st);
  4876. }
  4877. st->state = RETFUNC;
  4878. return SCRIPT_CMD_SUCCESS;
  4879. }
  4880. /// Returns a random number from 0 to <range>-1.
  4881. /// Or returns a random number from <min> to <max>.
  4882. /// If <min> is greater than <max>, their numbers are switched.
  4883. /// rand(<range>) -> <int>
  4884. /// rand(<min>,<max>) -> <int>
  4885. BUILDIN_FUNC(rand)
  4886. {
  4887. int range;
  4888. int min;
  4889. if( script_hasdata(st,3) )
  4890. {// min,max
  4891. int max = script_getnum(st,3);
  4892. min = script_getnum(st,2);
  4893. if( max < min )
  4894. SWAP(min, max);
  4895. range = max - min + 1;
  4896. }
  4897. else
  4898. {// range
  4899. min = 0;
  4900. range = script_getnum(st,2);
  4901. }
  4902. if( range <= 1 )
  4903. script_pushint(st, min);
  4904. else
  4905. script_pushint(st, rnd()%range + min);
  4906. return SCRIPT_CMD_SUCCESS;
  4907. }
  4908. /*==========================================
  4909. * Warp sd to str,x,y or Random or SavePoint/Save
  4910. *------------------------------------------*/
  4911. BUILDIN_FUNC(warp)
  4912. {
  4913. int ret;
  4914. int x,y;
  4915. const char* str;
  4916. struct map_session_data* sd;
  4917. if(!script_charid2sd(5, sd))
  4918. return SCRIPT_CMD_SUCCESS;
  4919. str = script_getstr(st,2);
  4920. x = script_getnum(st,3);
  4921. y = script_getnum(st,4);
  4922. if(strcmp(str,"Random")==0)
  4923. ret = pc_randomwarp(sd,CLR_TELEPORT,true);
  4924. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4925. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4926. else
  4927. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4928. if( ret ) {
  4929. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4930. return SCRIPT_CMD_FAILURE;
  4931. }
  4932. return SCRIPT_CMD_SUCCESS;
  4933. }
  4934. /*==========================================
  4935. * Warp a specified area
  4936. *------------------------------------------*/
  4937. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4938. {
  4939. int x2,y2,x3,y3;
  4940. unsigned int index;
  4941. index = va_arg(ap,unsigned int);
  4942. x2 = va_arg(ap,int);
  4943. y2 = va_arg(ap,int);
  4944. x3 = va_arg(ap,int);
  4945. y3 = va_arg(ap,int);
  4946. if(index == 0)
  4947. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT,true);
  4948. else if(x3 && y3) {
  4949. int max, tx, ty, j = 0;
  4950. int16 m;
  4951. m = map_mapindex2mapid(index);
  4952. // choose a suitable max number of attempts
  4953. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4954. max = 1000;
  4955. // find a suitable map cell
  4956. do {
  4957. tx = rnd()%(x3-x2+1)+x2;
  4958. ty = rnd()%(y3-y2+1)+y2;
  4959. j++;
  4960. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4961. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4962. }
  4963. else
  4964. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4965. return 0;
  4966. }
  4967. BUILDIN_FUNC(areawarp)
  4968. {
  4969. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4970. unsigned int index;
  4971. const char *str;
  4972. const char *mapname;
  4973. mapname = script_getstr(st,2);
  4974. x0 = script_getnum(st,3);
  4975. y0 = script_getnum(st,4);
  4976. x1 = script_getnum(st,5);
  4977. y1 = script_getnum(st,6);
  4978. str = script_getstr(st,7);
  4979. x2 = script_getnum(st,8);
  4980. y2 = script_getnum(st,9);
  4981. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4982. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4983. x3 = 0;
  4984. y3 = 0;
  4985. } else if( x3 && y3 ) {
  4986. // normalize x3/y3 coordinates
  4987. if( x3 < x2 ) SWAP(x3,x2);
  4988. if( y3 < y2 ) SWAP(y3,y2);
  4989. }
  4990. }
  4991. if( (m = map_mapname2mapid(mapname)) < 0 )
  4992. return SCRIPT_CMD_FAILURE;
  4993. if( strcmp(str,"Random") == 0 )
  4994. index = 0;
  4995. else if( !(index=mapindex_name2id(str)) )
  4996. return SCRIPT_CMD_FAILURE;
  4997. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4998. return SCRIPT_CMD_SUCCESS;
  4999. }
  5000. /*==========================================
  5001. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  5002. *------------------------------------------*/
  5003. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  5004. {
  5005. int hp, sp;
  5006. hp = va_arg(ap, int);
  5007. sp = va_arg(ap, int);
  5008. pc_percentheal((TBL_PC *)bl,hp,sp);
  5009. return 0;
  5010. }
  5011. BUILDIN_FUNC(areapercentheal)
  5012. {
  5013. int hp,sp,m;
  5014. const char *mapname;
  5015. int x0,y0,x1,y1;
  5016. mapname=script_getstr(st,2);
  5017. x0=script_getnum(st,3);
  5018. y0=script_getnum(st,4);
  5019. x1=script_getnum(st,5);
  5020. y1=script_getnum(st,6);
  5021. hp=script_getnum(st,7);
  5022. sp=script_getnum(st,8);
  5023. if( (m=map_mapname2mapid(mapname))< 0)
  5024. return SCRIPT_CMD_FAILURE;
  5025. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5026. return SCRIPT_CMD_SUCCESS;
  5027. }
  5028. enum e_warpparty_target{
  5029. WARPPARTY_RANDOM = 0,
  5030. WARPPARTY_SAVEPOINTALL,
  5031. WARPPARTY_SAVEPOINT,
  5032. WARPPARTY_LEADER,
  5033. WARPPARTY_RANDOMALL,
  5034. WARPPARTY_RANDOMALLAREA
  5035. };
  5036. /*==========================================
  5037. * Warpparty - [Fredzilla] [Paradox924X]
  5038. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5039. * If 'from_mapname' is specified, only the party members on that map will be warped
  5040. *------------------------------------------*/
  5041. BUILDIN_FUNC(warpparty)
  5042. {
  5043. TBL_PC *sd = NULL;
  5044. TBL_PC *pl_sd;
  5045. struct party_data* p;
  5046. int mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5047. const char* str = script_getstr(st,2);
  5048. int x = script_getnum(st,3);
  5049. int y = script_getnum(st,4);
  5050. int p_id = script_getnum(st,5);
  5051. const char* str2 = NULL;
  5052. if ( script_hasdata(st,6) )
  5053. str2 = script_getstr(st,6);
  5054. if (script_hasdata(st, 7))
  5055. rx = script_getnum(st, 7);
  5056. if (script_hasdata(st, 8))
  5057. ry = script_getnum(st, 8);
  5058. p = party_search(p_id);
  5059. if(!p)
  5060. return SCRIPT_CMD_SUCCESS;
  5061. enum e_warpparty_target type = ( strcmp(str,"Random")==0 ) ? WARPPARTY_RANDOM
  5062. : ( strcmp(str,"SavePointAll")==0 ) ? WARPPARTY_SAVEPOINTALL
  5063. : ( strcmp(str,"SavePoint")==0 ) ? WARPPARTY_SAVEPOINT
  5064. : ( strcmp(str,"Leader")==0 ) ? WARPPARTY_LEADER
  5065. : ( strcmp(str,"RandomAll")==0 ) ? WARPPARTY_RANDOMALL
  5066. : WARPPARTY_RANDOMALLAREA;
  5067. switch (type)
  5068. {
  5069. case WARPPARTY_SAVEPOINT:
  5070. //"SavePoint" uses save point of the currently attached player
  5071. if ( !script_rid2sd(sd) )
  5072. return SCRIPT_CMD_FAILURE;
  5073. break;
  5074. case WARPPARTY_LEADER:
  5075. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5076. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5077. return SCRIPT_CMD_FAILURE;
  5078. pl_sd = p->data[i].sd;
  5079. mapindex = pl_sd->mapindex;
  5080. m = map_mapindex2mapid(mapindex);
  5081. x = pl_sd->bl.x;
  5082. y = pl_sd->bl.y;
  5083. break;
  5084. case WARPPARTY_RANDOMALL: {
  5085. if ( !script_rid2sd(sd) )
  5086. return SCRIPT_CMD_FAILURE;
  5087. mapindex = sd->mapindex;
  5088. m = map_mapindex2mapid(mapindex);
  5089. struct map_data *mapdata = map_getmapdata(m);
  5090. if ( mapdata == nullptr || mapdata->flag[MF_NOWARP] || mapdata->flag[MF_NOTELEPORT] )
  5091. return SCRIPT_CMD_FAILURE;
  5092. i = 0;
  5093. do {
  5094. x = rnd()%(mapdata->xs - 2) + 1;
  5095. y = rnd()%(mapdata->ys - 2) + 1;
  5096. } while ((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  5097. if (i >= 1000) {
  5098. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  5099. return SCRIPT_CMD_FAILURE;
  5100. }
  5101. } break;
  5102. case WARPPARTY_RANDOMALLAREA:
  5103. mapindex = mapindex_name2id(str);
  5104. if (!mapindex) {// Invalid map
  5105. return SCRIPT_CMD_FAILURE;
  5106. }
  5107. m = map_mapindex2mapid(mapindex);
  5108. break;
  5109. }
  5110. for (i = 0; i < MAX_PARTY; i++)
  5111. {
  5112. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5113. continue;
  5114. map_data* mapdata = map_getmapdata(pl_sd->bl.m);
  5115. if( str2 && strcmp(str2, mapdata->name) != 0 )
  5116. continue;
  5117. if( pc_isdead(pl_sd) )
  5118. continue;
  5119. e_setpos ret = SETPOS_OK;
  5120. switch( type )
  5121. {
  5122. case WARPPARTY_RANDOM:
  5123. if (!mapdata->flag[MF_NOWARP])
  5124. ret = (e_setpos)pc_randomwarp(pl_sd,CLR_TELEPORT);
  5125. break;
  5126. case WARPPARTY_SAVEPOINTALL:
  5127. if (!mapdata->flag[MF_NORETURN])
  5128. ret = pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5129. break;
  5130. case WARPPARTY_SAVEPOINT:
  5131. if (!mapdata->flag[MF_NORETURN])
  5132. ret = pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5133. break;
  5134. case WARPPARTY_LEADER:
  5135. if (p->party.member[i].leader)
  5136. continue;
  5137. // Fall through
  5138. case WARPPARTY_RANDOMALL:
  5139. if (pl_sd == sd) {
  5140. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5141. break;
  5142. }
  5143. // Fall through
  5144. case WARPPARTY_RANDOMALLAREA:
  5145. if(!mapdata->flag[MF_NORETURN] && !mapdata->flag[MF_NOWARP] && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pc_get_group_level(pl_sd))){
  5146. if (rx || ry) {
  5147. int x1 = x + rx, y1 = y + ry,
  5148. x0 = x - rx, y0 = y - ry,
  5149. nx, ny;
  5150. uint8 attempts = 10;
  5151. do {
  5152. nx = x0 + rnd()%(x1 - x0 + 1);
  5153. ny = y0 + rnd()%(y1 - y0 + 1);
  5154. } while ((--attempts) > 0 && !map_getcell(m, nx, ny, CELL_CHKPASS));
  5155. if (attempts != 0) { //Keep the original coordinates if fails to find a valid cell within the range
  5156. x = nx;
  5157. y = ny;
  5158. }
  5159. }
  5160. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5161. }
  5162. break;
  5163. }
  5164. if( ret != SETPOS_OK ) {
  5165. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", pl_sd->status.name, str, x, y);
  5166. if ( ( type == WARPPARTY_RANDOMALL || type == WARPPARTY_RANDOMALLAREA ) && (rx || ry) )
  5167. continue;
  5168. else
  5169. return SCRIPT_CMD_FAILURE;
  5170. }
  5171. }
  5172. return SCRIPT_CMD_SUCCESS;
  5173. }
  5174. /*==========================================
  5175. * Warpguild - [Fredzilla]
  5176. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5177. *------------------------------------------*/
  5178. BUILDIN_FUNC(warpguild)
  5179. {
  5180. TBL_PC *sd = NULL;
  5181. TBL_PC *pl_sd;
  5182. struct guild* g;
  5183. struct s_mapiterator* iter;
  5184. int type, mapindex = 0, m = -1;
  5185. const char* str = script_getstr(st,2);
  5186. int x = script_getnum(st,3);
  5187. int y = script_getnum(st,4);
  5188. int gid = script_getnum(st,5);
  5189. g = guild_search(gid);
  5190. if( g == NULL )
  5191. return SCRIPT_CMD_SUCCESS;
  5192. type = ( strcmp(str,"Random")==0 ) ? 0
  5193. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5194. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5195. : 3;
  5196. if( type == 2 && !script_rid2sd(sd) )
  5197. {// "SavePoint" uses save point of the currently attached player
  5198. return SCRIPT_CMD_SUCCESS;
  5199. }
  5200. switch (type) {
  5201. case 3:
  5202. mapindex = mapindex_name2id(str);
  5203. if (!mapindex)
  5204. return SCRIPT_CMD_FAILURE;
  5205. m = map_mapindex2mapid(mapindex);
  5206. break;
  5207. }
  5208. iter = mapit_getallusers();
  5209. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5210. {
  5211. if( pl_sd->status.guild_id != gid )
  5212. continue;
  5213. switch( type )
  5214. {
  5215. case 0: // Random
  5216. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5217. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5218. break;
  5219. case 1: // SavePointAll
  5220. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5221. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5222. break;
  5223. case 2: // SavePoint
  5224. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5225. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5226. break;
  5227. case 3: // m,x,y
  5228. 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)))
  5229. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5230. break;
  5231. }
  5232. }
  5233. mapit_free(iter);
  5234. return SCRIPT_CMD_SUCCESS;
  5235. }
  5236. /*==========================================
  5237. * Force Heal a player (hp and sp)
  5238. *------------------------------------------*/
  5239. BUILDIN_FUNC(heal)
  5240. {
  5241. TBL_PC *sd;
  5242. int hp,sp;
  5243. if (!script_charid2sd(4,sd))
  5244. return SCRIPT_CMD_SUCCESS;
  5245. hp=script_getnum(st,2);
  5246. sp=script_getnum(st,3);
  5247. status_heal(&sd->bl, hp, sp, 1);
  5248. return SCRIPT_CMD_SUCCESS;
  5249. }
  5250. /*==========================================
  5251. * Force Heal a player (ap)
  5252. *------------------------------------------*/
  5253. BUILDIN_FUNC(healap)
  5254. {
  5255. map_session_data* sd;
  5256. if (!script_charid2sd(3, sd))
  5257. return SCRIPT_CMD_FAILURE;
  5258. status_heal(&sd->bl, 0, 0, script_getnum(st, 2), 1);
  5259. return SCRIPT_CMD_SUCCESS;
  5260. }
  5261. /*==========================================
  5262. * Heal a player by item (get vit bonus etc)
  5263. *------------------------------------------*/
  5264. BUILDIN_FUNC(itemheal)
  5265. {
  5266. TBL_PC *sd;
  5267. int hp,sp;
  5268. hp=script_getnum(st,2);
  5269. sp=script_getnum(st,3);
  5270. if(potion_flag==1) {
  5271. potion_hp = hp;
  5272. potion_sp = sp;
  5273. return SCRIPT_CMD_SUCCESS;
  5274. }
  5275. if (!script_charid2sd(4,sd))
  5276. return SCRIPT_CMD_SUCCESS;
  5277. pc_itemheal(sd,sd->itemid,hp,sp);
  5278. return SCRIPT_CMD_SUCCESS;
  5279. }
  5280. /*==========================================
  5281. *
  5282. *------------------------------------------*/
  5283. BUILDIN_FUNC(percentheal)
  5284. {
  5285. int hp,sp;
  5286. TBL_PC* sd;
  5287. hp=script_getnum(st,2);
  5288. sp=script_getnum(st,3);
  5289. if(potion_flag==1) {
  5290. potion_per_hp = hp;
  5291. potion_per_sp = sp;
  5292. return SCRIPT_CMD_SUCCESS;
  5293. }
  5294. if (!script_charid2sd(4,sd))
  5295. return SCRIPT_CMD_SUCCESS;
  5296. #ifdef RENEWAL
  5297. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5298. sp = 0;
  5299. #endif
  5300. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5301. hp = 0;
  5302. sp = 0;
  5303. }
  5304. if (sd->sc.data[SC_BITESCAR])
  5305. hp = 0;
  5306. pc_percentheal(sd,hp,sp);
  5307. return SCRIPT_CMD_SUCCESS;
  5308. }
  5309. /*==========================================
  5310. *
  5311. *------------------------------------------*/
  5312. BUILDIN_FUNC(jobchange)
  5313. {
  5314. int job, upper=-1;
  5315. job=script_getnum(st,2);
  5316. if( script_hasdata(st,3) )
  5317. upper=script_getnum(st,3);
  5318. if (pcdb_checkid(job))
  5319. {
  5320. TBL_PC* sd;
  5321. if (!script_charid2sd(4,sd))
  5322. return SCRIPT_CMD_SUCCESS;
  5323. pc_jobchange(sd, job, upper);
  5324. }
  5325. return SCRIPT_CMD_SUCCESS;
  5326. }
  5327. /*==========================================
  5328. *
  5329. *------------------------------------------*/
  5330. BUILDIN_FUNC(jobname)
  5331. {
  5332. int class_=script_getnum(st,2);
  5333. script_pushconststr(st, (char*)job_name(class_));
  5334. return SCRIPT_CMD_SUCCESS;
  5335. }
  5336. /// Get input from the player.
  5337. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5338. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5339. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5340. /// shorter than 'min' and 0 otherwise.
  5341. ///
  5342. /// input(<var>{,<min>{,<max>}}) -> <int>
  5343. BUILDIN_FUNC(input)
  5344. {
  5345. TBL_PC* sd;
  5346. struct script_data* data;
  5347. int64 uid;
  5348. const char* name;
  5349. int min;
  5350. int max;
  5351. if( !script_rid2sd(sd) )
  5352. return SCRIPT_CMD_SUCCESS;
  5353. data = script_getdata(st,2);
  5354. if( !data_isreference(data) ){
  5355. ShowError("script:input: not a variable\n");
  5356. script_reportdata(data);
  5357. st->state = END;
  5358. return SCRIPT_CMD_FAILURE;
  5359. }
  5360. uid = reference_getuid(data);
  5361. name = reference_getname(data);
  5362. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5363. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5364. #ifdef SECURE_NPCTIMEOUT
  5365. sd->npc_idle_type = NPCT_WAIT;
  5366. #endif
  5367. if( !sd->state.menu_or_input )
  5368. { // first invocation, display npc input box
  5369. sd->state.menu_or_input = 1;
  5370. st->state = RERUNLINE;
  5371. if( is_string_variable(name) )
  5372. clif_scriptinputstr(sd,st->oid);
  5373. else
  5374. clif_scriptinput(sd,st->oid);
  5375. }
  5376. else
  5377. { // take received text/value and store it in the designated variable
  5378. sd->state.menu_or_input = 0;
  5379. if( is_string_variable(name) )
  5380. {
  5381. int len = (int)strlen(sd->npc_str);
  5382. set_reg_str(st, sd, uid, name, sd->npc_str, script_getref(st,2));
  5383. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5384. }
  5385. else
  5386. {
  5387. int amount = sd->npc_amount;
  5388. set_reg_num(st, sd, uid, name, cap_value(amount,min,max), script_getref(st,2));
  5389. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5390. }
  5391. st->state = RUN;
  5392. }
  5393. return SCRIPT_CMD_SUCCESS;
  5394. }
  5395. // declare the copyarray method here for future reference
  5396. BUILDIN_FUNC(copyarray);
  5397. /// Sets the value of a variable.
  5398. /// The value is converted to the type of the variable.
  5399. ///
  5400. /// set(<variable>,<value>{,<charid>})
  5401. BUILDIN_FUNC(setr)
  5402. {
  5403. TBL_PC* sd = NULL;
  5404. struct script_data* data;
  5405. //struct script_data* datavalue;
  5406. int64 num;
  5407. uint8 pos = 4;
  5408. const char* name, *command = script_getfuncname(st);
  5409. char prefix;
  5410. data = script_getdata(st,2);
  5411. //datavalue = script_getdata(st,3);
  5412. if( !data_isreference(data) )
  5413. {
  5414. ShowError("script:set: not a variable\n");
  5415. script_reportdata(script_getdata(st,2));
  5416. st->state = END;
  5417. return SCRIPT_CMD_FAILURE;
  5418. }
  5419. num = reference_getuid(data);
  5420. name = reference_getname(data);
  5421. prefix = *name;
  5422. if (!strcmp(command, "setr"))
  5423. pos = 5;
  5424. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5425. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5426. return SCRIPT_CMD_FAILURE;
  5427. }
  5428. #if 0
  5429. if( data_isreference(datavalue) )
  5430. {// the value being referenced is a variable
  5431. const char* namevalue = reference_getname(datavalue);
  5432. if( !not_array_variable(*namevalue) )
  5433. {// array variable being copied into another array variable
  5434. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5435. {// player must be attached in order to copy a player variable
  5436. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5437. return SCRIPT_CMD_SUCCESS;
  5438. }
  5439. if( is_string_variable(namevalue) != is_string_variable(name) )
  5440. {// non-matching array value types
  5441. ShowWarning("script:set: two array variables do not match in type.\n");
  5442. return SCRIPT_CMD_SUCCESS;
  5443. }
  5444. // push the maximum number of array values to the stack
  5445. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5446. // call the copy array method directly
  5447. return buildin_copyarray(st);
  5448. }
  5449. }
  5450. #endif
  5451. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5452. if( is_string_variable(name) )
  5453. script_pushstrcopy(st,script_getstr(st, 4));
  5454. else
  5455. script_pushint(st,script_getnum64(st, 4));
  5456. } else // Return a copy of the variable reference
  5457. script_pushcopy(st, 2);
  5458. if( is_string_variable(name) )
  5459. set_reg_str( st, sd, num, name, script_getstr( st, 3 ), script_getref( st, 2 ) );
  5460. else
  5461. set_reg_num( st, sd, num, name, script_getnum64( st, 3 ), script_getref( st, 2 ) );
  5462. return SCRIPT_CMD_SUCCESS;
  5463. }
  5464. /////////////////////////////////////////////////////////////////////
  5465. /// Array variables
  5466. ///
  5467. /// Sets values of an array, from the starting index.
  5468. /// ex: setarray arr[1],1,2,3;
  5469. ///
  5470. /// setarray <array variable>,<value1>{,<value2>...};
  5471. BUILDIN_FUNC(setarray)
  5472. {
  5473. struct script_data* data;
  5474. const char* name;
  5475. uint32 start;
  5476. uint32 end;
  5477. int32 id;
  5478. int32 i;
  5479. TBL_PC* sd = NULL;
  5480. data = script_getdata(st, 2);
  5481. if( !data_isreference(data) )
  5482. {
  5483. ShowError("script:setarray: not a variable\n");
  5484. script_reportdata(data);
  5485. st->state = END;
  5486. return SCRIPT_CMD_FAILURE;// not a variable
  5487. }
  5488. id = reference_getid(data);
  5489. start = reference_getindex(data);
  5490. name = reference_getname(data);
  5491. if( not_server_variable(*name) )
  5492. {
  5493. if( !script_rid2sd(sd) )
  5494. return SCRIPT_CMD_SUCCESS;// no player attached
  5495. }
  5496. end = start + script_lastdata(st) - 2;
  5497. if( end > SCRIPT_MAX_ARRAYSIZE )
  5498. end = SCRIPT_MAX_ARRAYSIZE;
  5499. if( is_string_variable(name) )
  5500. {// string array
  5501. for( i = 3; start < end; ++start, ++i )
  5502. set_reg_str( st, sd, reference_uid( id, start ), name, script_getstr( st, i ), reference_getref( data ) );
  5503. }
  5504. else
  5505. {// int array
  5506. for( i = 3; start < end; ++start, ++i )
  5507. set_reg_num( st, sd, reference_uid( id, start ), name, script_getnum64( st, i ), reference_getref( data ) );
  5508. }
  5509. return SCRIPT_CMD_SUCCESS;
  5510. }
  5511. /// Sets count values of an array, from the starting index.
  5512. /// ex: cleararray arr[0],0,1;
  5513. ///
  5514. /// cleararray <array variable>,<value>,<count>;
  5515. BUILDIN_FUNC(cleararray)
  5516. {
  5517. struct script_data* data;
  5518. const char* name;
  5519. uint32 start;
  5520. uint32 end;
  5521. int32 id;
  5522. TBL_PC* sd = NULL;
  5523. data = script_getdata(st, 2);
  5524. if( !data_isreference(data) )
  5525. {
  5526. ShowError("script:cleararray: not a variable\n");
  5527. script_reportdata(data);
  5528. st->state = END;
  5529. return SCRIPT_CMD_FAILURE;// not a variable
  5530. }
  5531. id = reference_getid(data);
  5532. start = reference_getindex(data);
  5533. name = reference_getname(data);
  5534. if( not_server_variable(*name) )
  5535. {
  5536. if( !script_rid2sd(sd) )
  5537. return SCRIPT_CMD_SUCCESS;// no player attached
  5538. }
  5539. end = start + script_getnum(st, 4);
  5540. if( end > SCRIPT_MAX_ARRAYSIZE )
  5541. end = SCRIPT_MAX_ARRAYSIZE;
  5542. if( is_string_variable( name ) ){
  5543. const char* value = script_getstr( st, 3 );
  5544. for( ; start < end; ++start ){
  5545. set_reg_str( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5546. }
  5547. }else{
  5548. int64 value = script_getnum64( st, 3 );
  5549. for( ; start < end; ++start ){
  5550. set_reg_num( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5551. }
  5552. }
  5553. return SCRIPT_CMD_SUCCESS;
  5554. }
  5555. /// Copies data from one array to another.
  5556. /// ex: copyarray arr[0],arr[2],2;
  5557. ///
  5558. /// copyarray <destination array variable>,<source array variable>,<count>;
  5559. BUILDIN_FUNC(copyarray)
  5560. {
  5561. struct script_data* data1;
  5562. struct script_data* data2;
  5563. const char* name1;
  5564. const char* name2;
  5565. int32 idx1;
  5566. int32 idx2;
  5567. int32 id1;
  5568. int32 id2;
  5569. int32 i;
  5570. uint32 count;
  5571. TBL_PC* sd = NULL;
  5572. data1 = script_getdata(st, 2);
  5573. data2 = script_getdata(st, 3);
  5574. if( !data_isreference(data1) || !data_isreference(data2) )
  5575. {
  5576. ShowError("script:copyarray: not a variable\n");
  5577. script_reportdata(data1);
  5578. script_reportdata(data2);
  5579. st->state = END;
  5580. return SCRIPT_CMD_FAILURE;// not a variable
  5581. }
  5582. id1 = reference_getid(data1);
  5583. id2 = reference_getid(data2);
  5584. idx1 = reference_getindex(data1);
  5585. idx2 = reference_getindex(data2);
  5586. name1 = reference_getname(data1);
  5587. name2 = reference_getname(data2);
  5588. bool is_string = is_string_variable( name1 );
  5589. if( is_string != is_string_variable( name2 ) ){
  5590. ShowError("script:copyarray: type mismatch\n");
  5591. script_reportdata(data1);
  5592. script_reportdata(data2);
  5593. st->state = END;
  5594. return SCRIPT_CMD_FAILURE;// data type mismatch
  5595. }
  5596. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5597. {
  5598. if( !script_rid2sd(sd) )
  5599. return SCRIPT_CMD_SUCCESS;// no player attached
  5600. }
  5601. count = script_getnum(st, 4);
  5602. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5603. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5604. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5605. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5606. if( id1 == id2 && idx1 > idx2 ){
  5607. // destination might be overlapping the source - copy in reverse order
  5608. for( i = count - 1; i >= 0; --i ){
  5609. if( is_string ){
  5610. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5611. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5612. // Remove stack entry from get_val2_str
  5613. script_removetop( st, -1, 0 );
  5614. }else{
  5615. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5616. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5617. }
  5618. }
  5619. }else{
  5620. // normal copy
  5621. for( i = 0; i < count; ++i ){
  5622. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ){
  5623. if( is_string ){
  5624. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5625. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5626. // Remove stack entry from get_val2_str
  5627. script_removetop( st, -1, 0 );
  5628. }else{
  5629. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5630. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5631. }
  5632. }else{
  5633. // out of range - assume ""/0
  5634. clear_reg( st, sd, reference_uid( id1, idx1 + i ), name1, reference_getref( data1 ) );
  5635. }
  5636. }
  5637. }
  5638. return SCRIPT_CMD_SUCCESS;
  5639. }
  5640. /// Returns the size of the array.
  5641. /// Assumes that everything before the starting index exists.
  5642. /// ex: getarraysize(arr[3])
  5643. ///
  5644. /// getarraysize(<array variable>) -> <int>
  5645. BUILDIN_FUNC(getarraysize)
  5646. {
  5647. struct script_data* data;
  5648. const char* name;
  5649. struct map_session_data* sd = NULL;
  5650. data = script_getdata(st, 2);
  5651. if( !data_isreference(data) )
  5652. {
  5653. ShowError("script:getarraysize: not a variable\n");
  5654. script_reportdata(data);
  5655. script_pushnil(st);
  5656. st->state = END;
  5657. return SCRIPT_CMD_FAILURE;// not a variable
  5658. }
  5659. name = reference_getname(data);
  5660. if( not_server_variable(*name) ){
  5661. if (!script_rid2sd(sd))
  5662. return SCRIPT_CMD_SUCCESS;// no player attached
  5663. }
  5664. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5665. return SCRIPT_CMD_SUCCESS;
  5666. }
  5667. int script_array_index_cmp(const void *a, const void *b) {
  5668. return ( *(unsigned int*)a - *(unsigned int*)b );
  5669. }
  5670. /// Deletes count or all the elements in an array, from the starting index.
  5671. /// ex: deletearray arr[4],2;
  5672. ///
  5673. /// deletearray <array variable>;
  5674. /// deletearray <array variable>,<count>;
  5675. BUILDIN_FUNC(deletearray)
  5676. {
  5677. struct script_data* data;
  5678. const char* name;
  5679. unsigned int start, end, i;
  5680. int id;
  5681. TBL_PC *sd = NULL;
  5682. struct script_array *sa = NULL;
  5683. struct reg_db *src = NULL;
  5684. data = script_getdata(st, 2);
  5685. if( !data_isreference(data) ) {
  5686. ShowError("script:deletearray: not a variable\n");
  5687. script_reportdata(data);
  5688. st->state = END;
  5689. return SCRIPT_CMD_FAILURE;// not a variable
  5690. }
  5691. id = reference_getid(data);
  5692. start = reference_getindex(data);
  5693. name = reference_getname(data);
  5694. if( not_server_variable(*name) ) {
  5695. if( !script_rid2sd(sd) )
  5696. return SCRIPT_CMD_SUCCESS;// no player attached
  5697. }
  5698. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5699. ShowError("script:deletearray: not a array\n");
  5700. script_reportdata(data);
  5701. st->state = END;
  5702. return SCRIPT_CMD_FAILURE;// not a variable
  5703. }
  5704. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5705. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5706. return SCRIPT_CMD_SUCCESS;// not a variable
  5707. }
  5708. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5709. if( start >= end )
  5710. return SCRIPT_CMD_SUCCESS;// nothing to free
  5711. if( script_hasdata(st,3) ) {
  5712. unsigned int count = script_getnum(st, 3);
  5713. if( count > end - start )
  5714. count = end - start;
  5715. if( count <= 0 )
  5716. return SCRIPT_CMD_SUCCESS;// nothing to free
  5717. bool is_string = is_string_variable( name );
  5718. if( end - start < sa->size ) {
  5719. // Better to iterate directly on the array, no speed-up from using sa
  5720. for( ; start + count < end; ++start ) {
  5721. // Compact and overwrite
  5722. if( is_string ){
  5723. const char* value = get_val2_str( st, reference_uid( id, start + count ), reference_getref( data ) );
  5724. set_reg_str( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5725. // Remove stack entry from get_val2_str
  5726. script_removetop( st, -1, 0 );
  5727. }else{
  5728. int64 value = get_val2_num( st, reference_uid( id, start + count ), reference_getref( data ) );
  5729. set_reg_num( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5730. }
  5731. }
  5732. for( ; start < end; start++ ) {
  5733. // Clean up any leftovers that weren't overwritten
  5734. clear_reg( st, sd, reference_uid( id, start ), name, reference_getref( data ) );
  5735. }
  5736. } else {
  5737. // using sa to speed up
  5738. unsigned int *list = NULL, size = 0;
  5739. list = script_array_cpy_list(sa);
  5740. size = sa->size;
  5741. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5742. ARR_FIND(0, size, i, list[i] >= start);
  5743. for( ; i < size && list[i] < start + count; i++ ) {
  5744. // Clear any entries between start and start+count, if they exist
  5745. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5746. }
  5747. for( ; i < size && list[i] < end; i++ ) {
  5748. // Move back count positions any entries between start+count to fill the gaps
  5749. if( is_string ){
  5750. const char* value = get_val2_str( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5751. set_reg_str( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5752. // Remove stack entry from get_val2_str
  5753. script_removetop( st, -1, 0 );
  5754. }else{
  5755. int64 value = get_val2_num( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5756. set_reg_num( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5757. }
  5758. // Clear their originals
  5759. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5760. }
  5761. }
  5762. } else {
  5763. unsigned int *list = NULL, size = 0;
  5764. list = script_array_cpy_list(sa);
  5765. size = sa->size;
  5766. for(i = 0; i < size; i++) {
  5767. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5768. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5769. }
  5770. }
  5771. return SCRIPT_CMD_SUCCESS;
  5772. }
  5773. /// Returns a reference to the target index of the array variable.
  5774. /// Equivalent to var[index].
  5775. ///
  5776. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5777. BUILDIN_FUNC(getelementofarray)
  5778. {
  5779. struct script_data* data;
  5780. int32 id;
  5781. int64 i;
  5782. data = script_getdata(st, 2);
  5783. if( !data_isreference(data) )
  5784. {
  5785. ShowError("script:getelementofarray: not a variable\n");
  5786. script_reportdata(data);
  5787. script_pushnil(st);
  5788. st->state = END;
  5789. return SCRIPT_CMD_SUCCESS;// not a variable
  5790. }
  5791. id = reference_getid(data);
  5792. i = script_getnum(st, 3);
  5793. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5794. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5795. script_reportdata(data);
  5796. script_pushnil(st);
  5797. st->state = END;
  5798. return SCRIPT_CMD_FAILURE;// out of range
  5799. }
  5800. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5801. return SCRIPT_CMD_SUCCESS;
  5802. }
  5803. /// Return the index number of the first matching value in an array.
  5804. /// ex: inarray arr,1;
  5805. ///
  5806. /// inarray <array variable>,<value>;
  5807. BUILDIN_FUNC(inarray)
  5808. {
  5809. struct script_data *data;
  5810. const char* name;
  5811. int id, i, array_size;
  5812. struct map_session_data* sd = NULL;
  5813. struct reg_db *ref = NULL;
  5814. data = script_getdata(st, 2);
  5815. if (!data_isreference(data))
  5816. {
  5817. ShowError("buildin_inarray: not a variable\n");
  5818. script_reportdata(data);
  5819. st->state = END;
  5820. return SCRIPT_CMD_FAILURE;
  5821. }
  5822. name = reference_getname(data);
  5823. ref = reference_getref(data);
  5824. if (not_server_variable(*name) && !script_rid2sd(sd))
  5825. return SCRIPT_CMD_FAILURE;
  5826. array_size = script_array_highest_key(st, sd, name, ref) - 1;
  5827. if (array_size < 0)
  5828. {
  5829. script_pushint(st, -1);
  5830. return SCRIPT_CMD_SUCCESS;
  5831. }
  5832. if (array_size > SCRIPT_MAX_ARRAYSIZE)
  5833. {
  5834. ShowError("buildin_inarray: The array is too large.\n");
  5835. script_reportdata(data);
  5836. st->state = END;
  5837. return SCRIPT_CMD_FAILURE;
  5838. }
  5839. id = reference_getid(data);
  5840. if( is_string_variable( name ) ){
  5841. const char* value = script_getstr( st, 3 );
  5842. for( i = 0; i <= array_size; ++i ){
  5843. const char* temp = get_val2_str( st, reference_uid( id, i ), ref );
  5844. if( !strcmp( temp, value ) ){
  5845. // Remove stack entry from get_val2_str
  5846. script_removetop( st, -1, 0 );
  5847. script_pushint( st, i );
  5848. return SCRIPT_CMD_SUCCESS;
  5849. }
  5850. // Remove stack entry from get_val2_str
  5851. script_removetop( st, -1, 0 );
  5852. }
  5853. }else{
  5854. int64 value = script_getnum64( st, 3 );
  5855. for( i = 0; i <= array_size; ++i ){
  5856. int64 temp = get_val2_num( st, reference_uid( id, i ), ref );
  5857. if( temp == value ){
  5858. script_pushint( st, i );
  5859. return SCRIPT_CMD_SUCCESS;
  5860. }
  5861. }
  5862. }
  5863. script_pushint(st, -1);
  5864. return SCRIPT_CMD_SUCCESS;
  5865. }
  5866. /// Return the number of matches in two arrays.
  5867. /// ex: countinarray arr[0],arr1[0];
  5868. ///
  5869. /// countinarray <array variable>,<array variable>;
  5870. BUILDIN_FUNC(countinarray)
  5871. {
  5872. struct script_data *data1 , *data2;
  5873. const char* name1;
  5874. const char* name2;
  5875. int id1, id2, i, j, array_size1, array_size2, case_count = 0;
  5876. struct map_session_data* sd = NULL;
  5877. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5878. data1 = script_getdata(st, 2);
  5879. data2 = script_getdata(st, 3);
  5880. if (!data_isreference(data1) || !data_isreference(data2))
  5881. {
  5882. ShowError("buildin_countinarray: not a variable\n");
  5883. script_reportdata(data1);
  5884. script_reportdata(data2);
  5885. st->state = END;
  5886. return SCRIPT_CMD_FAILURE;
  5887. }
  5888. name1 = reference_getname(data1);
  5889. name2 = reference_getname(data2);
  5890. ref1 = reference_getref(data1);
  5891. ref2 = reference_getref(data2);
  5892. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5893. return SCRIPT_CMD_FAILURE;
  5894. array_size1 = script_array_highest_key(st, sd, name1, ref1) - 1;
  5895. array_size2 = script_array_highest_key(st, sd, name2, ref2) - 1;
  5896. if (array_size1 < 0 || array_size2 < 0)
  5897. {
  5898. script_pushint(st, 0);
  5899. return SCRIPT_CMD_SUCCESS;
  5900. }
  5901. if (array_size1 > SCRIPT_MAX_ARRAYSIZE || array_size2 > SCRIPT_MAX_ARRAYSIZE)
  5902. {
  5903. ShowError("buildin_countinarray: The array is too large.\n");
  5904. script_reportdata(data1);
  5905. script_reportdata(data2);
  5906. st->state = END;
  5907. return SCRIPT_CMD_FAILURE;
  5908. }
  5909. i = reference_getindex(data1);
  5910. j = reference_getindex(data2);
  5911. if (array_size1 < i || array_size2 < j)
  5912. { //To prevent unintended behavior
  5913. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5914. script_reportdata(data1);
  5915. script_reportdata(data2);
  5916. st->state = END;
  5917. return SCRIPT_CMD_FAILURE;
  5918. }
  5919. id1 = reference_getid(data1);
  5920. id2 = reference_getid(data2);
  5921. if( is_string_variable( name1 ) && is_string_variable( name2 ) ){
  5922. for( ; i <= array_size1; ++i ){
  5923. const char* temp1 = get_val2_str( st, reference_uid( id1, i ), ref1 );
  5924. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5925. const char* temp2 = get_val2_str( st, reference_uid( id2, j ), ref2 );
  5926. if( !strcmp( temp1, temp2 ) ){
  5927. case_count++;
  5928. }
  5929. // Remove stack entry from get_val2_str
  5930. script_removetop( st, -1, 0 );
  5931. }
  5932. // Remove stack entry from get_val2_str
  5933. script_removetop( st, -1, 0 );
  5934. }
  5935. }else if( !is_string_variable( name1 ) && !is_string_variable( name2 ) ){
  5936. for( ; i <= array_size1; ++i ){
  5937. int64 temp1 = get_val2_num( st, reference_uid( id1, i ), ref1 );
  5938. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5939. int64 temp2 = get_val2_num( st, reference_uid( id2, j ), ref2 );
  5940. if( temp1 == temp2 ){
  5941. case_count++;
  5942. }
  5943. }
  5944. }
  5945. }else{
  5946. ShowError("buildin_countinarray: Arrays do not match , You can't compare an int array to a string array.\n");
  5947. script_reportdata(data1);
  5948. script_reportdata(data2);
  5949. st->state = END;
  5950. return SCRIPT_CMD_FAILURE;
  5951. }
  5952. script_pushint(st, case_count);
  5953. return SCRIPT_CMD_SUCCESS;
  5954. }
  5955. /////////////////////////////////////////////////////////////////////
  5956. /// ...
  5957. ///
  5958. /*==========================================
  5959. *
  5960. *------------------------------------------*/
  5961. BUILDIN_FUNC(setlook)
  5962. {
  5963. int type,val;
  5964. TBL_PC* sd;
  5965. type = script_getnum(st,2);
  5966. val = script_getnum(st,3);
  5967. if (!script_charid2sd(4,sd))
  5968. return SCRIPT_CMD_SUCCESS;
  5969. pc_changelook(sd,type,val);
  5970. return SCRIPT_CMD_SUCCESS;
  5971. }
  5972. BUILDIN_FUNC(changelook)
  5973. { // As setlook but only client side
  5974. int type,val;
  5975. TBL_PC* sd;
  5976. type = script_getnum(st,2);
  5977. val = script_getnum(st,3);
  5978. if (!script_charid2sd(4,sd))
  5979. return SCRIPT_CMD_SUCCESS;
  5980. switch(type) {
  5981. case LOOK_HAIR:
  5982. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  5983. break;
  5984. case LOOK_HAIR_COLOR:
  5985. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  5986. break;
  5987. case LOOK_CLOTHES_COLOR:
  5988. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  5989. break;
  5990. case LOOK_BODY2:
  5991. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  5992. break;
  5993. default:
  5994. break;
  5995. }
  5996. clif_changelook(&sd->bl, type, val);
  5997. return SCRIPT_CMD_SUCCESS;
  5998. }
  5999. /*==========================================
  6000. *
  6001. *------------------------------------------*/
  6002. BUILDIN_FUNC(cutin)
  6003. {
  6004. TBL_PC* sd;
  6005. if( !script_rid2sd(sd) )
  6006. return SCRIPT_CMD_SUCCESS;
  6007. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  6008. return SCRIPT_CMD_SUCCESS;
  6009. }
  6010. /*==========================================
  6011. *
  6012. *------------------------------------------*/
  6013. BUILDIN_FUNC(viewpoint)
  6014. {
  6015. int type,x,y,id,color;
  6016. TBL_PC* sd;
  6017. if (!script_charid2sd(7, sd)) {
  6018. st->state = END;
  6019. return SCRIPT_CMD_FAILURE;
  6020. }
  6021. type=script_getnum(st,2);
  6022. x=script_getnum(st,3);
  6023. y=script_getnum(st,4);
  6024. id=script_getnum(st,5);
  6025. color=script_getnum(st,6);
  6026. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  6027. return SCRIPT_CMD_SUCCESS;
  6028. }
  6029. static int buildin_viewpointmap_sub(block_list *bl, va_list ap) {
  6030. int oid, type, x, y, id, color;
  6031. oid = va_arg(ap, int);
  6032. type = va_arg(ap, int);
  6033. x = va_arg(ap, int);
  6034. y = va_arg(ap, int);
  6035. id = va_arg(ap, int);
  6036. color = va_arg(ap, int);
  6037. clif_viewpoint((map_session_data *)bl, oid, type, x, y, id, color);
  6038. return 0;
  6039. }
  6040. BUILDIN_FUNC(viewpointmap) {
  6041. const char* map = script_getstr(st, 2);
  6042. int16 mapid = map_mapname2mapid(map);
  6043. if (mapid < 0) {
  6044. ShowError("buildin_viewpointmap: Unknown map name %s.\n", map);
  6045. return SCRIPT_CMD_FAILURE;
  6046. }
  6047. 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));
  6048. return SCRIPT_CMD_SUCCESS;
  6049. }
  6050. /**
  6051. * Set random options for new item
  6052. * @param st Script state
  6053. * @param it Temporary item data
  6054. * @param funcname Function name
  6055. * @param x First position of random option id array from the script
  6056. **/
  6057. static bool script_getitem_randomoption(struct script_state *st, struct map_session_data* sd, struct item *it, const char *funcname, int x) {
  6058. int i, opt_id_n;
  6059. struct script_data *opt_id = script_getdata(st,x);
  6060. struct script_data *opt_val = script_getdata(st,x+1);
  6061. struct script_data *opt_param = script_getdata(st,x+2);
  6062. const char *opt_id_var = reference_getname(opt_id);
  6063. const char *opt_val_var = reference_getname(opt_val);
  6064. const char *opt_param_var = reference_getname(opt_param);
  6065. int32 opt_id_id, opt_id_idx;
  6066. int32 opt_val_id, opt_val_idx;
  6067. int32 opt_param_id, opt_param_idx;
  6068. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  6069. // Check if the variable requires a player
  6070. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  6071. // If no player is attached
  6072. if( !script_rid2sd(sd) ){
  6073. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  6074. return false;
  6075. }
  6076. }
  6077. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  6078. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  6079. return false;
  6080. }
  6081. if (is_string_variable(opt_id_var)) {
  6082. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  6083. return false;
  6084. }
  6085. // Check if the variable requires a player
  6086. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  6087. // If no player is attached
  6088. if( !script_rid2sd(sd) ){
  6089. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  6090. return false;
  6091. }
  6092. }
  6093. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  6094. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  6095. return false;
  6096. }
  6097. if (is_string_variable(opt_val_var)) {
  6098. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6099. return false;
  6100. }
  6101. // Check if the variable requires a player
  6102. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  6103. // If no player is attached
  6104. if( !script_rid2sd(sd) ){
  6105. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  6106. return false;
  6107. }
  6108. }
  6109. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  6110. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  6111. return false;
  6112. }
  6113. if (is_string_variable(opt_param_var)) {
  6114. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6115. return false;
  6116. }
  6117. opt_id_ref = reference_getref(opt_id);
  6118. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  6119. opt_val_ref = reference_getref(opt_val);
  6120. opt_param_ref = reference_getref(opt_param);
  6121. opt_id_id = reference_getid(opt_id);
  6122. opt_val_id = reference_getid(opt_val);
  6123. opt_param_id = reference_getid(opt_param);
  6124. opt_id_idx = reference_getindex(opt_id);
  6125. opt_val_idx = reference_getindex(opt_val);
  6126. opt_param_idx = reference_getindex(opt_param);
  6127. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6128. it->option[i].id = (short)get_val2_num( st, reference_uid( opt_id_id, opt_id_idx + i ), opt_id_ref );
  6129. it->option[i].value = (short)get_val2_num( st, reference_uid( opt_val_id, opt_val_idx + i ), opt_val_ref );
  6130. it->option[i].param = (char)get_val2_num( st, reference_uid( opt_param_id, opt_param_idx + i ), opt_param_ref );
  6131. }
  6132. return true;
  6133. }
  6134. /**
  6135. * Sub function for counting items
  6136. * @param items: Item array to search
  6137. * @param id: Item data to search for
  6138. * @param size: Maximum size of array
  6139. * @param expanded: If the script command has extra arguments
  6140. * @param random_option: If the struct command has random option arguments
  6141. * @param st: Script state (required for random options)
  6142. * @param sd: Player data (required for random options)
  6143. * @param rental: Whether or not to count rental items
  6144. * @return Total count of item being searched
  6145. */
  6146. static int script_countitem_sub(struct item *items, std::shared_ptr<item_data> id, int size, int expanded, struct script_state *st, struct map_session_data *sd = nullptr, bool rental = false) {
  6147. nullpo_retr(-1, items);
  6148. nullpo_retr(-1, st);
  6149. int count = 0;
  6150. if (!expanded) { // For non-expanded functions
  6151. t_itemid nameid = id->nameid;
  6152. for (int i = 0; i < size; i++) {
  6153. item *itm = &items[i];
  6154. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1)
  6155. continue;
  6156. if (itm->nameid == nameid && ((rental && itm->expire_time > 0) || (!rental && itm->expire_time == 0)))
  6157. count += itm->amount;
  6158. }
  6159. } else { // For expanded functions
  6160. item it = {};
  6161. int offset = 10;
  6162. it.nameid = id->nameid;
  6163. it.identify = script_getnum(st,3);
  6164. it.refine = script_getnum(st,4);
  6165. it.attribute = script_getnum(st,5);
  6166. it.card[0] = script_getnum(st,6);
  6167. it.card[1] = script_getnum(st,7);
  6168. it.card[2] = script_getnum(st,8);
  6169. it.card[3] = script_getnum(st,9);
  6170. if (expanded&4) {
  6171. it.enchantgrade = script_getnum(st,10);
  6172. offset = 11;
  6173. }
  6174. if (expanded&2) {
  6175. if (!sd) {
  6176. ShowError("buildin_countitem3: Player not attached.\n");
  6177. return -1;
  6178. }
  6179. bool res = script_getitem_randomoption(st, sd, &it, "countitem3", offset);
  6180. if (!res)
  6181. return -1;
  6182. }
  6183. for (int i = 0; i < size; i++) {
  6184. item *itm = &items[i];
  6185. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1)
  6186. continue;
  6187. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute || itm->enchantgrade != it.enchantgrade)
  6188. continue;
  6189. if ((!rental && itm->expire_time > 0) || (rental && itm->expire_time == 0))
  6190. continue;
  6191. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6192. continue;
  6193. if (expanded&2) {
  6194. uint8 j;
  6195. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6196. 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)
  6197. break;
  6198. }
  6199. if (j != MAX_ITEM_RDM_OPT)
  6200. continue;
  6201. }
  6202. count += items[i].amount;
  6203. }
  6204. }
  6205. return count;
  6206. }
  6207. /**
  6208. * Returns number of items in inventory
  6209. * countitem(<nameID>{,<accountID>})
  6210. * countitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6211. * countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6212. * countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6213. * countitem4(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6214. * countitem4("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6215. */
  6216. BUILDIN_FUNC(countitem)
  6217. {
  6218. TBL_PC *sd;
  6219. char *command = (char *)script_getfuncname(st);
  6220. int aid = 3;
  6221. int expanded = 0;
  6222. if (command[strlen(command)-1] == '2') {
  6223. expanded = 1;
  6224. aid = 10;
  6225. }
  6226. else if (command[strlen(command)-1] == '3') {
  6227. expanded = 3;
  6228. aid = 13;
  6229. }
  6230. else if (command[strlen(command)-1] == '4') {
  6231. expanded = 7;
  6232. aid = 14;
  6233. }
  6234. if (!script_accid2sd(aid, sd))
  6235. return SCRIPT_CMD_FAILURE;
  6236. std::shared_ptr<item_data> id;
  6237. if (script_isstring(st, 2)) // item name
  6238. id = item_db.searchname( script_getstr( st, 2 ) );
  6239. else // item id
  6240. id = item_db.find( script_getnum( st, 2 ) );
  6241. if (!id) {
  6242. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6243. script_pushint(st, 0);
  6244. return SCRIPT_CMD_FAILURE;
  6245. }
  6246. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, expanded, st, sd);
  6247. if (count < 0) {
  6248. st->state = END;
  6249. return SCRIPT_CMD_FAILURE;
  6250. }
  6251. script_pushint(st, count);
  6252. return SCRIPT_CMD_SUCCESS;
  6253. }
  6254. /**
  6255. * Returns number of items in cart
  6256. * cartcountitem(<nameID>{,<accountID>})
  6257. * cartcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6258. */
  6259. BUILDIN_FUNC(cartcountitem)
  6260. {
  6261. TBL_PC *sd;
  6262. char *command = (char *)script_getfuncname(st);
  6263. int aid = 3;
  6264. int expanded = 0;
  6265. if (command[strlen(command) - 1] == '2') {
  6266. expanded = 1;
  6267. aid = 10;
  6268. }
  6269. if (!script_accid2sd(aid, sd))
  6270. return SCRIPT_CMD_FAILURE;
  6271. if (!pc_iscarton(sd)) {
  6272. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6273. script_pushint(st, -1);
  6274. return SCRIPT_CMD_FAILURE;
  6275. }
  6276. std::shared_ptr<item_data> id;
  6277. if (script_isstring(st, 2)) // item name
  6278. id = item_db.searchname( script_getstr( st, 2 ) );
  6279. else // item id
  6280. id = item_db.find( script_getnum( st, 2 ) );
  6281. if (!id) {
  6282. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6283. script_pushint(st, 0);
  6284. return SCRIPT_CMD_FAILURE;
  6285. }
  6286. int count = script_countitem_sub(sd->cart.u.items_cart, id, MAX_CART, expanded, st);
  6287. if (count < 0) {
  6288. st->state = END;
  6289. return SCRIPT_CMD_FAILURE;
  6290. }
  6291. script_pushint(st, count);
  6292. return SCRIPT_CMD_SUCCESS;
  6293. }
  6294. /**
  6295. * Returns number of items in storage
  6296. * storagecountitem(<nameID>{,<accountID>})
  6297. * storagecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6298. */
  6299. BUILDIN_FUNC(storagecountitem)
  6300. {
  6301. TBL_PC *sd;
  6302. char *command = (char *)script_getfuncname(st);
  6303. int aid = 3;
  6304. int expanded = 0;
  6305. if (command[strlen(command) - 1] == '2') {
  6306. expanded = 1;
  6307. aid = 10;
  6308. }
  6309. if (!script_accid2sd(aid, sd))
  6310. return SCRIPT_CMD_FAILURE;
  6311. std::shared_ptr<item_data> id;
  6312. if (script_isstring(st, 2)) // item name
  6313. id = item_db.searchname( script_getstr( st, 2 ) );
  6314. else // item id
  6315. id = item_db.find( script_getnum( st, 2 ) );
  6316. if (!id) {
  6317. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6318. script_pushint(st, 0);
  6319. return SCRIPT_CMD_FAILURE;
  6320. }
  6321. if (sd->state.storage_flag != 0) {
  6322. script_pushint(st, -1);
  6323. return SCRIPT_CMD_SUCCESS;
  6324. }
  6325. int count = script_countitem_sub(sd->storage.u.items_storage, id, MAX_STORAGE, expanded, st);
  6326. if (count < 0) {
  6327. st->state = END;
  6328. return SCRIPT_CMD_FAILURE;
  6329. }
  6330. script_pushint(st, count);
  6331. return SCRIPT_CMD_SUCCESS;
  6332. }
  6333. /**
  6334. * Returns number of items in guild storage
  6335. * guildstoragecountitem(<nameID>{,<accountID>})
  6336. * guildstoragecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6337. */
  6338. BUILDIN_FUNC(guildstoragecountitem)
  6339. {
  6340. TBL_PC *sd;
  6341. char *command = (char *)script_getfuncname(st);
  6342. int aid = 3;
  6343. int expanded = 0;
  6344. if (command[strlen(command) - 1] == '2') {
  6345. expanded = 1;
  6346. aid = 10;
  6347. }
  6348. if (!script_accid2sd(aid, sd))
  6349. return SCRIPT_CMD_FAILURE;
  6350. std::shared_ptr<item_data> id;
  6351. if (script_isstring(st, 2)) // item name
  6352. id = item_db.searchname( script_getstr( st, 2 ) );
  6353. else // item id
  6354. id = item_db.find( script_getnum( st, 2 ) );
  6355. if (!id) {
  6356. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6357. script_pushint(st, 0);
  6358. return SCRIPT_CMD_FAILURE;
  6359. }
  6360. struct s_storage *gstor = guild2storage(sd->status.guild_id);
  6361. if (!gstor || (gstor && sd->state.storage_flag != 0)) {
  6362. script_pushint(st, -1);
  6363. return SCRIPT_CMD_SUCCESS;
  6364. }
  6365. gstor->lock = true;
  6366. int count = script_countitem_sub(gstor->u.items_guild, id, MAX_GUILD_STORAGE, expanded, st);
  6367. storage_guild_storageclose(sd);
  6368. gstor->lock = false;
  6369. if (count < 0) {
  6370. st->state = END;
  6371. return SCRIPT_CMD_FAILURE;
  6372. }
  6373. script_pushint(st, count);
  6374. return SCRIPT_CMD_SUCCESS;
  6375. }
  6376. /**
  6377. * Returns number of rental items in inventory
  6378. * rentalcountitem(<nameID>{,<accountID>})
  6379. * rentalcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6380. * rentalcountitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6381. * rentalcountitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6382. */
  6383. BUILDIN_FUNC(rentalcountitem)
  6384. {
  6385. char *command = (char *)script_getfuncname(st);
  6386. int aid = 3;
  6387. int expanded = 0;
  6388. if (command[strlen(command) - 1] == '2') {
  6389. expanded = 1;
  6390. aid = 10;
  6391. }
  6392. else if (command[strlen(command) - 1] == '3') {
  6393. expanded = 3;
  6394. aid = 13;
  6395. }
  6396. else if (command[strlen(command) - 1] == '4') {
  6397. expanded = 7;
  6398. aid = 14;
  6399. }
  6400. map_session_data *sd;
  6401. if (!script_accid2sd(aid, sd))
  6402. return SCRIPT_CMD_FAILURE;
  6403. std::shared_ptr<item_data> id;
  6404. if (script_isstring(st, 2)) // item name
  6405. id = item_db.searchname( script_getstr( st, 2 ) );
  6406. else // item id
  6407. id = item_db.find( script_getnum( st, 2 ) );
  6408. if (!id) {
  6409. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6410. script_pushint(st, 0);
  6411. return SCRIPT_CMD_FAILURE;
  6412. }
  6413. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, expanded, st, sd, true);
  6414. if (count < 0) {
  6415. st->state = END;
  6416. return SCRIPT_CMD_FAILURE;
  6417. }
  6418. script_pushint(st, count);
  6419. return SCRIPT_CMD_SUCCESS;
  6420. }
  6421. /*==========================================
  6422. * Check if item with this amount can fit in inventory
  6423. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6424. * Return
  6425. * 0 : fail
  6426. * 1 : success (npc side only)
  6427. *------------------------------------------*/
  6428. BUILDIN_FUNC(checkweight)
  6429. {
  6430. int slots = 0;
  6431. unsigned short amount2 = 0;
  6432. unsigned int weight = 0, i, nbargs;
  6433. std::shared_ptr<item_data> id;
  6434. struct map_session_data* sd;
  6435. if( !script_rid2sd(sd) )
  6436. return SCRIPT_CMD_SUCCESS;
  6437. nbargs = script_lastdata(st)+1;
  6438. if(nbargs%2) {
  6439. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6440. script_pushint(st,0);
  6441. return SCRIPT_CMD_FAILURE;
  6442. }
  6443. slots = pc_inventoryblank(sd); //nb of empty slot
  6444. for (i = 2; i < nbargs; i += 2) {
  6445. t_itemid nameid;
  6446. unsigned short amount;
  6447. if( script_isstring(st, i) ) // item name
  6448. id = item_db.searchname( script_getstr( st, i ) );
  6449. else // item id
  6450. id = item_db.find( script_getnum( st, i ) );
  6451. if( id == nullptr ){
  6452. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6453. script_pushint(st,0);
  6454. return SCRIPT_CMD_FAILURE;
  6455. }
  6456. nameid = id->nameid;
  6457. amount = script_getnum(st,i+1);
  6458. if( amount < 1 ) {
  6459. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6460. script_pushint(st,0);
  6461. return SCRIPT_CMD_FAILURE;
  6462. }
  6463. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6464. if( weight + sd->weight > sd->max_weight )
  6465. {// too heavy
  6466. script_pushint(st,0);
  6467. return SCRIPT_CMD_SUCCESS;
  6468. }
  6469. switch( pc_checkadditem(sd, nameid, amount) ) {
  6470. case CHKADDITEM_EXIST:
  6471. // item is already in inventory, but there is still space for the requested amount
  6472. break;
  6473. case CHKADDITEM_NEW:
  6474. if( itemdb_isstackable(nameid) ) {
  6475. // stackable
  6476. amount2++;
  6477. if( slots < amount2 ) {
  6478. script_pushint(st,0);
  6479. return SCRIPT_CMD_SUCCESS;
  6480. }
  6481. } else {
  6482. // non-stackable
  6483. amount2 += amount;
  6484. if( slots < amount2) {
  6485. script_pushint(st,0);
  6486. return SCRIPT_CMD_SUCCESS;
  6487. }
  6488. }
  6489. break;
  6490. case CHKADDITEM_OVERAMOUNT:
  6491. script_pushint(st,0);
  6492. return SCRIPT_CMD_SUCCESS;
  6493. }
  6494. }
  6495. script_pushint(st,1);
  6496. return SCRIPT_CMD_SUCCESS;
  6497. }
  6498. BUILDIN_FUNC(checkweight2)
  6499. {
  6500. //variable sub checkweight
  6501. int i = 0, slots = 0, weight = 0;
  6502. short fail = 0;
  6503. unsigned short amount2 = 0;
  6504. //variable for array parsing
  6505. struct script_data* data_it;
  6506. struct script_data* data_nb;
  6507. const char* name_it;
  6508. const char* name_nb;
  6509. int32 id_it, id_nb;
  6510. int32 idx_it, idx_nb;
  6511. int nb_it, nb_nb; //array size
  6512. TBL_PC *sd;
  6513. if( !script_rid2sd(sd) )
  6514. return SCRIPT_CMD_SUCCESS;
  6515. data_it = script_getdata(st, 2);
  6516. data_nb = script_getdata(st, 3);
  6517. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6518. ShowError("buildin_checkweight2: parameter not a variable\n");
  6519. script_pushint(st,0);
  6520. return SCRIPT_CMD_FAILURE;// not a variable
  6521. }
  6522. id_it = reference_getid(data_it);
  6523. id_nb = reference_getid(data_nb);
  6524. idx_it = reference_getindex(data_it);
  6525. idx_nb = reference_getindex(data_nb);
  6526. name_it = reference_getname(data_it);
  6527. name_nb = reference_getname(data_nb);
  6528. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6529. ShowError("buildin_checkweight2: illegal type, need int\n");
  6530. script_pushint(st,0);
  6531. return SCRIPT_CMD_FAILURE;// not supported
  6532. }
  6533. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6534. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6535. if(nb_it != nb_nb) {
  6536. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6537. fail = 1;
  6538. }
  6539. slots = pc_inventoryblank(sd);
  6540. for(i=0; i<nb_it; i++) {
  6541. t_itemid nameid = (t_itemid)get_val2_num( st, reference_uid( id_it, idx_it + i ), reference_getref( data_it ) );
  6542. uint16 amount = (uint16)get_val2_num( st, reference_uid( id_nb, idx_nb + i ), reference_getref( data_nb ) );
  6543. if(fail)
  6544. continue; //cpntonie to depop rest
  6545. if (!item_db.exists(nameid)) {
  6546. ShowError("buildin_checkweight2: Invalid item '%u'.\n", nameid);
  6547. fail=1;
  6548. continue;
  6549. }
  6550. if(amount == 0 ) {
  6551. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6552. fail = 1;
  6553. continue;
  6554. }
  6555. weight += itemdb_weight(nameid)*amount;
  6556. if( weight + sd->weight > sd->max_weight ) {
  6557. fail = 1;
  6558. continue;
  6559. }
  6560. switch( pc_checkadditem(sd, nameid, amount) ) {
  6561. case CHKADDITEM_EXIST:
  6562. // item is already in inventory, but there is still space for the requested amount
  6563. break;
  6564. case CHKADDITEM_NEW:
  6565. if( itemdb_isstackable(nameid) ) {// stackable
  6566. amount2++;
  6567. if( slots < amount2 )
  6568. fail = 1;
  6569. } else {// non-stackable
  6570. amount2 += amount;
  6571. if( slots < amount2 ) {
  6572. fail = 1;
  6573. }
  6574. }
  6575. break;
  6576. case CHKADDITEM_OVERAMOUNT:
  6577. fail = 1;
  6578. } //end switch
  6579. } //end loop DO NOT break it prematurly we need to depop all stack
  6580. fail ? script_pushint(st,0) : script_pushint(st,1);
  6581. return SCRIPT_CMD_SUCCESS;
  6582. }
  6583. /*==========================================
  6584. * getitem <item id>,<amount>{,<account ID>};
  6585. * getitem "<item name>",<amount>{,<account ID>};
  6586. *
  6587. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6588. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6589. * Type:
  6590. * 0 - No bound
  6591. * 1 - Account Bound
  6592. * 2 - Guild Bound
  6593. * 3 - Party Bound
  6594. * 4 - Character Bound
  6595. *------------------------------------------*/
  6596. BUILDIN_FUNC(getitem)
  6597. {
  6598. int get_count, i;
  6599. t_itemid nameid;
  6600. unsigned short amount;
  6601. struct item it;
  6602. struct map_session_data *sd;
  6603. unsigned char flag = 0;
  6604. const char* command = script_getfuncname(st);
  6605. std::shared_ptr<item_data> id;
  6606. if( script_isstring(st, 2) ) {// "<item name>"
  6607. const char *name = script_getstr(st, 2);
  6608. id = item_db.searchname( name );
  6609. if( id == nullptr ){
  6610. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6611. return SCRIPT_CMD_FAILURE; //No item created.
  6612. }
  6613. nameid = id->nameid;
  6614. } else {// <item id>
  6615. nameid = script_getnum(st, 2);
  6616. id = item_db.find( nameid );
  6617. if( id == nullptr ){
  6618. ShowError("buildin_getitem: Nonexistant item %u requested.\n", nameid);
  6619. return SCRIPT_CMD_FAILURE; //No item created.
  6620. }
  6621. }
  6622. // <amount>
  6623. if( (amount = script_getnum(st,3)) <= 0)
  6624. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6625. memset(&it,0,sizeof(it));
  6626. it.nameid = nameid;
  6627. it.identify = 1;
  6628. it.bound = BOUND_NONE;
  6629. if( !strcmp(command,"getitembound") ) {
  6630. char bound = script_getnum(st,4);
  6631. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6632. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6633. return SCRIPT_CMD_FAILURE;
  6634. }
  6635. script_mapid2sd(5,sd);
  6636. it.bound = bound;
  6637. } else {
  6638. script_mapid2sd(4,sd);
  6639. }
  6640. if( sd == NULL ) // no target
  6641. return SCRIPT_CMD_SUCCESS;
  6642. //Check if it's stackable.
  6643. if( !itemdb_isstackable2( id.get() ) ){
  6644. get_count = 1;
  6645. }else{
  6646. get_count = amount;
  6647. }
  6648. for (i = 0; i < amount; i += get_count)
  6649. {
  6650. // if not pet egg
  6651. if (!pet_create_egg(sd, nameid))
  6652. {
  6653. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6654. {
  6655. clif_additem(sd, 0, 0, flag);
  6656. if( pc_candrop(sd,&it) )
  6657. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6658. }
  6659. }
  6660. }
  6661. return SCRIPT_CMD_SUCCESS;
  6662. }
  6663. /*==========================================
  6664. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6665. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6666. *
  6667. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6668. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6669. *
  6670. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6671. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6672. *
  6673. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6674. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6675. *
  6676. * getitem4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6677. * getitem4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6678. *
  6679. * getitembound4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6680. * getitembound4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6681. Type:
  6682. * 0 - No bound
  6683. * 1 - Account Bound
  6684. * 2 - Guild Bound
  6685. * 3 - Party Bound
  6686. * 4 - Character Bound
  6687. *------------------------------------------*/
  6688. BUILDIN_FUNC(getitem2)
  6689. {
  6690. TBL_PC *sd;
  6691. char bound = BOUND_NONE;
  6692. const char* command = script_getfuncname(st);
  6693. int offset = 0;
  6694. int grade_offset = 0;
  6695. if( !strncmp(command,"getitembound",12) ) {
  6696. int aid_pos = 12;
  6697. bound = script_getnum(st,11);
  6698. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6699. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6700. return SCRIPT_CMD_FAILURE;
  6701. }
  6702. if (command[strlen(command)-1] == '3') {
  6703. offset = 12;
  6704. aid_pos = 15;
  6705. }
  6706. else if (command[strlen(command)-1] == '4') {
  6707. grade_offset = 12;
  6708. offset = 13;
  6709. aid_pos = 16;
  6710. }
  6711. script_mapid2sd(aid_pos,sd);
  6712. } else {
  6713. int aid_pos = 11;
  6714. if (strcmpi(command,"getitem3") == 0) {
  6715. offset = 11;
  6716. aid_pos = 14;
  6717. }
  6718. if (strcmpi(command,"getitem4") == 0) {
  6719. grade_offset = 11;
  6720. offset = 12;
  6721. aid_pos = 15;
  6722. }
  6723. script_mapid2sd(aid_pos,sd);
  6724. }
  6725. if( sd == nullptr ) // no target
  6726. return SCRIPT_CMD_SUCCESS;
  6727. t_itemid nameid;
  6728. std::shared_ptr<item_data> item_data;
  6729. if( script_isstring(st, 2) ) {
  6730. const char *name = script_getstr(st, 2);
  6731. item_data = item_db.searchname( name );
  6732. if( item_data == nullptr ){
  6733. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6734. return SCRIPT_CMD_FAILURE; //No item created.
  6735. }
  6736. nameid = item_data->nameid;
  6737. } else {
  6738. nameid = script_getnum(st, 2);
  6739. item_data = item_db.find( nameid );
  6740. if( item_data == nullptr ){
  6741. ShowError("buildin_getitem2: Nonexistant item %u requested (by conv_num).\n", nameid);
  6742. return SCRIPT_CMD_FAILURE; //No item created.
  6743. }
  6744. }
  6745. int amount = script_getnum(st,3);
  6746. int iden = script_getnum(st,4);
  6747. int ref = script_getnum(st,5);
  6748. int attr = script_getnum(st,6);
  6749. t_itemid c1 = script_getnum(st,7);
  6750. t_itemid c2 = script_getnum(st,8);
  6751. t_itemid c3 = script_getnum(st,9);
  6752. t_itemid c4 = script_getnum(st,10);
  6753. struct item item_tmp = {};
  6754. if( item_data ) {
  6755. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6756. if(ref > MAX_REFINE)
  6757. ref = MAX_REFINE;
  6758. }
  6759. else if( item_data->type == IT_PETEGG ) {
  6760. iden = 1;
  6761. ref = 0;
  6762. }
  6763. else {
  6764. iden = 1;
  6765. ref = attr = 0;
  6766. }
  6767. item_tmp.nameid = nameid;
  6768. item_tmp.identify = iden;
  6769. item_tmp.refine = ref;
  6770. item_tmp.attribute = attr;
  6771. item_tmp.card[0] = c1;
  6772. item_tmp.card[1] = c2;
  6773. item_tmp.card[2] = c3;
  6774. item_tmp.card[3] = c4;
  6775. item_tmp.bound = bound;
  6776. if (offset != 0) {
  6777. bool res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6778. if (!res)
  6779. return SCRIPT_CMD_FAILURE;
  6780. if (grade_offset > 0) {
  6781. int grade = script_getnum(st, grade_offset);
  6782. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  6783. ShowError("buildin_getitem2: Not a correct grade! Grade=%d\n", grade);
  6784. return SCRIPT_CMD_FAILURE;
  6785. }
  6786. item_tmp.enchantgrade = static_cast<e_enchantgrade>(grade);
  6787. }
  6788. }
  6789. int get_count = 0;
  6790. //Check if it's stackable.
  6791. if( !itemdb_isstackable2( item_data.get() ) ){
  6792. get_count = 1;
  6793. }else{
  6794. get_count = amount;
  6795. }
  6796. for (int i = 0; i < amount; i += get_count)
  6797. {
  6798. // if not pet egg
  6799. if (!pet_create_egg(sd, nameid))
  6800. {
  6801. unsigned char flag = 0;
  6802. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6803. {
  6804. clif_additem(sd, 0, 0, flag);
  6805. if( pc_candrop(sd,&item_tmp) )
  6806. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6807. }
  6808. }
  6809. }
  6810. }
  6811. return SCRIPT_CMD_SUCCESS;
  6812. }
  6813. /** Gives rental item to player
  6814. * rentitem <item id>,<seconds>{,<account_id>}
  6815. * rentitem "<item name>",<seconds>{,<account_id>}
  6816. */
  6817. BUILDIN_FUNC(rentitem) {
  6818. struct map_session_data *sd;
  6819. t_itemid nameid = 0;
  6820. unsigned char flag = 0;
  6821. if (!script_accid2sd(4,sd))
  6822. return SCRIPT_CMD_FAILURE;
  6823. if( script_isstring(st, 2) )
  6824. {
  6825. const char *name = script_getstr(st, 2);
  6826. std::shared_ptr<item_data> itd = item_db.searchname( name );
  6827. if( itd == nullptr ){
  6828. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6829. return SCRIPT_CMD_FAILURE;
  6830. }
  6831. nameid = itd->nameid;
  6832. }
  6833. else
  6834. {
  6835. nameid = script_getnum(st, 2);
  6836. if( !item_db.exists(nameid) )
  6837. {
  6838. ShowError("buildin_rentitem: Nonexistant item %u requested.\n", nameid);
  6839. return SCRIPT_CMD_FAILURE;
  6840. }
  6841. }
  6842. int seconds = script_getnum(st,3);
  6843. struct item it = {};
  6844. it.nameid = nameid;
  6845. it.identify = 1;
  6846. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6847. it.bound = BOUND_NONE;
  6848. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6849. {
  6850. clif_additem(sd, 0, 0, flag);
  6851. return SCRIPT_CMD_FAILURE;
  6852. }
  6853. return SCRIPT_CMD_SUCCESS;
  6854. }
  6855. /**
  6856. * Gives rental item to player with advanced option
  6857. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6858. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6859. *
  6860. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6861. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6862. *
  6863. * rentitem4 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6864. * rentitem4 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6865. */
  6866. BUILDIN_FUNC(rentitem2) {
  6867. struct map_session_data *sd;
  6868. const char *funcname = script_getfuncname(st);
  6869. if (funcname[strlen(funcname)-1] == '3') {
  6870. if (!script_accid2sd(14,sd))
  6871. return SCRIPT_CMD_FAILURE;
  6872. } else if (funcname[strlen(funcname)-1] == '4') {
  6873. if (!script_accid2sd(15,sd))
  6874. return SCRIPT_CMD_FAILURE;
  6875. } else if (!script_accid2sd(11,sd))
  6876. return SCRIPT_CMD_FAILURE;
  6877. std::shared_ptr<item_data> id;
  6878. t_itemid nameid = 0;
  6879. if( script_isstring(st, 2) ) {
  6880. const char *name = script_getstr(st, 2);
  6881. id = item_db.searchname( name );
  6882. if( id == nullptr ) {
  6883. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6884. return SCRIPT_CMD_FAILURE;
  6885. }
  6886. nameid = id->nameid;
  6887. } else {
  6888. nameid = script_getnum(st, 2);
  6889. id = item_db.find( nameid );
  6890. if( id == nullptr ){
  6891. ShowError("buildin_rentitem2: Nonexistant item %u requested.\n", nameid);
  6892. return SCRIPT_CMD_FAILURE;
  6893. }
  6894. }
  6895. int seconds = script_getnum(st,3);
  6896. int iden = script_getnum(st,4);
  6897. int ref = script_getnum(st,5);
  6898. int attr = script_getnum(st,6);
  6899. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6900. if(ref > MAX_REFINE)
  6901. ref = MAX_REFINE;
  6902. }
  6903. else if (id->type==IT_PETEGG) {
  6904. iden = 1;
  6905. ref = 0;
  6906. }
  6907. else {
  6908. iden = 1;
  6909. ref = attr = 0;
  6910. }
  6911. t_itemid c1 = script_getnum(st,7);
  6912. t_itemid c2 = script_getnum(st,8);
  6913. t_itemid c3 = script_getnum(st,9);
  6914. t_itemid c4 = script_getnum(st,10);
  6915. struct item it = {};
  6916. it.nameid = nameid;
  6917. it.identify = iden;
  6918. it.refine = ref;
  6919. it.attribute = attr;
  6920. it.card[0] = c1;
  6921. it.card[1] = c2;
  6922. it.card[2] = c3;
  6923. it.card[3] = c4;
  6924. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6925. it.bound = BOUND_NONE;
  6926. if (funcname[strlen(funcname)-1] == '3') {
  6927. bool res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6928. if (!res)
  6929. return SCRIPT_CMD_FAILURE;
  6930. }
  6931. else if (funcname[strlen(funcname)-1] == '4') {
  6932. bool res = script_getitem_randomoption(st, sd, &it, funcname, 12);
  6933. if (!res)
  6934. return SCRIPT_CMD_FAILURE;
  6935. int grade = script_getnum(st, 11);
  6936. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  6937. ShowError("buildin_rentitem2: Not a correct grade! Grade=%d\n", grade);
  6938. return SCRIPT_CMD_FAILURE;
  6939. }
  6940. it.enchantgrade = static_cast<e_enchantgrade>(grade);
  6941. }
  6942. unsigned char flag = 0;
  6943. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6944. clif_additem(sd, 0, 0, flag);
  6945. return SCRIPT_CMD_FAILURE;
  6946. }
  6947. return SCRIPT_CMD_SUCCESS;
  6948. }
  6949. /*==========================================
  6950. * gets an item with someone's name inscribed [Skotlex]
  6951. * getinscribeditem item_num, character_name
  6952. * Returned Qty is always 1, only works on equip-able
  6953. * equipment
  6954. *------------------------------------------*/
  6955. BUILDIN_FUNC(getnameditem)
  6956. {
  6957. t_itemid nameid;
  6958. struct item item_tmp;
  6959. TBL_PC *sd, *tsd;
  6960. if (!script_rid2sd(sd))
  6961. { //Player not attached!
  6962. script_pushint(st,0);
  6963. return SCRIPT_CMD_SUCCESS;
  6964. }
  6965. if( script_isstring(st, 2) ){
  6966. const char *name = script_getstr(st, 2);
  6967. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6968. // Failed
  6969. if( item_data == nullptr){
  6970. script_pushint(st,0);
  6971. return SCRIPT_CMD_SUCCESS;
  6972. }
  6973. nameid = item_data->nameid;
  6974. }else
  6975. nameid = script_getnum(st, 2);
  6976. if(!item_db.exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6977. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6978. script_pushint(st,0);
  6979. return SCRIPT_CMD_SUCCESS;
  6980. }
  6981. if( script_isstring(st, 3) ) //Char Name
  6982. tsd = map_nick2sd(script_getstr(st, 3),false);
  6983. else //Char Id was given
  6984. tsd = map_charid2sd(script_getnum(st, 3));
  6985. if( tsd == NULL )
  6986. { //Failed
  6987. script_pushint(st,0);
  6988. return SCRIPT_CMD_SUCCESS;
  6989. }
  6990. memset(&item_tmp,0,sizeof(item_tmp));
  6991. item_tmp.nameid=nameid;
  6992. item_tmp.amount=1;
  6993. item_tmp.identify=1;
  6994. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6995. item_tmp.card[2]=GetWord(tsd->status.char_id,0);
  6996. item_tmp.card[3]=GetWord(tsd->status.char_id,1);
  6997. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6998. script_pushint(st,0);
  6999. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  7000. }
  7001. script_pushint(st,1);
  7002. return SCRIPT_CMD_SUCCESS;
  7003. }
  7004. /*==========================================
  7005. * gets a random item ID from an item group [Skotlex]
  7006. * groupranditem <group_num>{,<sub_group>};
  7007. *------------------------------------------*/
  7008. BUILDIN_FUNC(grouprandomitem) {
  7009. int sub_group = 1;
  7010. FETCH(3, sub_group);
  7011. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(script_getnum(st,2),sub_group);
  7012. if (!entry) {
  7013. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  7014. script_pushint(st,UNKNOWN_ITEM_ID);
  7015. return SCRIPT_CMD_FAILURE;
  7016. }
  7017. script_pushint(st,entry->nameid);
  7018. return SCRIPT_CMD_SUCCESS;
  7019. }
  7020. /**
  7021. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  7022. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  7023. */
  7024. BUILDIN_FUNC(makeitem) {
  7025. t_itemid nameid;
  7026. uint16 amount, flag = 0, x, y;
  7027. const char *mapname;
  7028. int m;
  7029. struct item item_tmp;
  7030. bool canShowEffect = false;
  7031. if( script_isstring(st, 2) ){
  7032. const char *name = script_getstr(st, 2);
  7033. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  7034. if( item_data )
  7035. nameid = item_data->nameid;
  7036. else{
  7037. ShowError( "buildin_makeitem: Unknown item %s\n", name );
  7038. return SCRIPT_CMD_FAILURE;
  7039. }
  7040. }
  7041. else {
  7042. int32 val = script_getnum( st, 2 );
  7043. if( val < 0 ){
  7044. flag = 1;
  7045. nameid = (t_itemid)( -1 * val );
  7046. }else{
  7047. nameid = (t_itemid)val;
  7048. }
  7049. if( !item_db.exists( nameid ) ){
  7050. ShowError( "buildin_makeitem: Unknown item id %u\n", nameid );
  7051. return SCRIPT_CMD_FAILURE;
  7052. }
  7053. }
  7054. amount = script_getnum(st,3);
  7055. mapname = script_getstr(st,4);
  7056. x = script_getnum(st,5);
  7057. y = script_getnum(st,6);
  7058. if (script_hasdata(st, 7))
  7059. canShowEffect = script_getnum(st, 7) != 0;
  7060. if (strcmp(mapname, "this")==0) {
  7061. TBL_PC *sd;
  7062. if (!script_rid2sd(sd))
  7063. return SCRIPT_CMD_SUCCESS; //Failed...
  7064. m = sd->bl.m;
  7065. } else
  7066. m = map_mapname2mapid(mapname);
  7067. memset(&item_tmp,0,sizeof(item_tmp));
  7068. item_tmp.nameid = nameid;
  7069. if (!flag)
  7070. item_tmp.identify = 1;
  7071. else
  7072. item_tmp.identify = itemdb_isidentified(nameid);
  7073. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7074. return SCRIPT_CMD_SUCCESS;
  7075. }
  7076. /**
  7077. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  7078. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  7079. *
  7080. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7081. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7082. *
  7083. * makeitem4 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7084. * makeitem4 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  7085. */
  7086. BUILDIN_FUNC(makeitem2) {
  7087. t_itemid nameid;
  7088. const char *funcname = script_getfuncname(st);
  7089. if( script_isstring( st, 2 ) ){
  7090. const char *name = script_getstr( st, 2 );
  7091. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  7092. if( item_data ){
  7093. nameid = item_data->nameid;
  7094. }else{
  7095. ShowError( "buildin_%s: Unknown item %s\n", funcname, name );
  7096. return SCRIPT_CMD_FAILURE;
  7097. }
  7098. }else{
  7099. nameid = (t_itemid)script_getnum( st, 2 );
  7100. if( !item_db.exists( nameid ) ){
  7101. ShowError( "buildin_%s: Unknown item id %u\n", funcname, nameid );
  7102. return SCRIPT_CMD_FAILURE;
  7103. }
  7104. }
  7105. int amount = script_getnum(st,3);
  7106. const char *mapname = script_getstr(st,4);
  7107. int x = script_getnum(st,5);
  7108. int y = script_getnum(st,6);
  7109. int m;
  7110. if (strcmp(mapname,"this")==0) {
  7111. TBL_PC *sd;
  7112. if (!script_rid2sd(sd))
  7113. return SCRIPT_CMD_SUCCESS; //Failed...
  7114. m = sd->bl.m;
  7115. }
  7116. else
  7117. m = map_mapname2mapid(mapname);
  7118. struct item_data *id;
  7119. if ((id = itemdb_search(nameid))) {
  7120. struct item item_tmp = {};
  7121. item_tmp.nameid = nameid;
  7122. char iden = (char)script_getnum(st,7);
  7123. char ref = (char)script_getnum(st,8);
  7124. char attr = (char)script_getnum(st,9);
  7125. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  7126. if(ref > MAX_REFINE)
  7127. ref = MAX_REFINE;
  7128. }
  7129. else if (id->type==IT_PETEGG) {
  7130. iden = 1;
  7131. ref = 0;
  7132. }
  7133. else {
  7134. iden = 1;
  7135. ref = attr = 0;
  7136. }
  7137. item_tmp.identify = iden;
  7138. item_tmp.refine = ref;
  7139. item_tmp.attribute = attr;
  7140. item_tmp.card[0] = script_getnum(st,10);
  7141. item_tmp.card[1] = script_getnum(st,11);
  7142. item_tmp.card[2] = script_getnum(st,12);
  7143. item_tmp.card[3] = script_getnum(st,13);
  7144. bool canShowEffect = false;
  7145. if (funcname[strlen(funcname)-1] == '3' || funcname[strlen(funcname)-1] == '4') {
  7146. int offset = 14;
  7147. if (funcname[strlen(funcname)-1] == '4') {
  7148. offset = 15;
  7149. int grade = script_getnum(st, 14);
  7150. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  7151. ShowError( "buildin_%s: Not a correct grade! Grade=%d\n", funcname, grade );
  7152. return SCRIPT_CMD_FAILURE;
  7153. }
  7154. item_tmp.enchantgrade = static_cast<e_enchantgrade>(grade);
  7155. }
  7156. bool res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, offset);
  7157. if (!res)
  7158. return SCRIPT_CMD_FAILURE;
  7159. if (script_hasdata(st, offset+3)) {
  7160. if (script_getnum(st, offset+3) != 0)
  7161. canShowEffect = script_getnum(st, offset+3) != 0;
  7162. }
  7163. }
  7164. else {
  7165. if (script_hasdata(st, 14)) {
  7166. if (script_getnum(st, 14) != 0)
  7167. canShowEffect = script_getnum(st, 14) != 0;
  7168. }
  7169. }
  7170. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7171. }
  7172. else
  7173. return SCRIPT_CMD_FAILURE;
  7174. return SCRIPT_CMD_SUCCESS;
  7175. }
  7176. /// Counts / deletes the current item given by idx.
  7177. /// Used by buildin_delitem_search
  7178. /// Relies on all input data being already fully valid.
  7179. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  7180. {
  7181. int delamount;
  7182. struct item *itm = NULL;
  7183. struct s_storage *gstor = NULL;
  7184. switch(loc) {
  7185. case TABLE_CART:
  7186. itm = &sd->cart.u.items_cart[idx];
  7187. break;
  7188. case TABLE_STORAGE:
  7189. itm = &sd->storage.u.items_storage[idx];
  7190. break;
  7191. case TABLE_GUILD_STORAGE:
  7192. {
  7193. gstor = guild2storage2(sd->status.guild_id);
  7194. itm = &gstor->u.items_guild[idx];
  7195. }
  7196. break;
  7197. default: // TABLE_INVENTORY
  7198. itm = &sd->inventory.u.items_inventory[idx];
  7199. break;
  7200. }
  7201. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7202. if( delete_items )
  7203. {
  7204. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7205. {// delete associated pet
  7206. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7207. }
  7208. switch(loc) {
  7209. case TABLE_CART:
  7210. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7211. break;
  7212. case TABLE_STORAGE:
  7213. storage_delitem(sd,&sd->storage,idx,delamount);
  7214. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7215. break;
  7216. case TABLE_GUILD_STORAGE:
  7217. gstor->lock = true;
  7218. storage_guild_delitem(sd, gstor, idx, delamount);
  7219. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7220. storage_guild_storageclose(sd);
  7221. gstor->lock = false;
  7222. break;
  7223. default: // TABLE_INVENTORY
  7224. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7225. break;
  7226. }
  7227. }
  7228. amount[0]-= delamount;
  7229. }
  7230. /// Searches for item(s) and checks, if there is enough of them.
  7231. /// Used by delitem and delitem2
  7232. /// Relies on all input data being already fully valid.
  7233. /// @param exact_match will also match item by specified attributes
  7234. /// 0x0: Only item id
  7235. /// 0x1: identify, attributes, cards
  7236. /// 0x2: random option
  7237. /// @return true when all items could be deleted, false when there were not enough items to delete
  7238. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7239. {
  7240. bool delete_items = false;
  7241. int i, amount, size;
  7242. struct item *items;
  7243. // prefer always non-equipped items
  7244. it->equip = 0;
  7245. // when searching for nameid only, prefer additionally
  7246. if( !exact_match )
  7247. {
  7248. // non-refined items
  7249. it->refine = 0;
  7250. // card-less items
  7251. memset(it->card, 0, sizeof(it->card));
  7252. }
  7253. switch(loc) {
  7254. case TABLE_CART:
  7255. size = MAX_CART;
  7256. items = sd->cart.u.items_cart;
  7257. break;
  7258. case TABLE_STORAGE:
  7259. size = MAX_STORAGE;
  7260. items = sd->storage.u.items_storage;
  7261. break;
  7262. case TABLE_GUILD_STORAGE:
  7263. {
  7264. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7265. size = MAX_GUILD_STORAGE;
  7266. items = gstor->u.items_guild;
  7267. }
  7268. break;
  7269. default: // TABLE_INVENTORY
  7270. size = MAX_INVENTORY;
  7271. items = sd->inventory.u.items_inventory;
  7272. break;
  7273. }
  7274. for(;;)
  7275. {
  7276. unsigned short important = 0;
  7277. amount = it->amount;
  7278. // 1st pass -- less important items / exact match
  7279. for( i = 0; amount && i < size; i++ )
  7280. {
  7281. struct item *itm = NULL;
  7282. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7283. {// wrong/invalid item
  7284. continue;
  7285. }
  7286. if( itm->equip != it->equip || itm->refine != it->refine )
  7287. {// not matching attributes
  7288. important++;
  7289. continue;
  7290. }
  7291. if( exact_match )
  7292. {
  7293. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || itm->enchantgrade != it->enchantgrade || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7294. {// not matching exact attributes
  7295. continue;
  7296. }
  7297. if (exact_match&0x2) {
  7298. uint8 j;
  7299. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7300. 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)
  7301. break;
  7302. }
  7303. if (j != MAX_ITEM_RDM_OPT)
  7304. continue;
  7305. }
  7306. }
  7307. else
  7308. {
  7309. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7310. {
  7311. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7312. {// pet which cannot be deleted
  7313. continue;
  7314. }
  7315. }
  7316. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7317. {// named/carded item
  7318. important++;
  7319. continue;
  7320. }
  7321. }
  7322. // count / delete item
  7323. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7324. }
  7325. // 2nd pass -- any matching item
  7326. if( amount == 0 || important == 0 )
  7327. {// either everything was already consumed or no items were skipped
  7328. ;
  7329. }
  7330. else for( i = 0; amount && i < size; i++ )
  7331. {
  7332. struct item *itm = NULL;
  7333. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7334. {// wrong/invalid item
  7335. continue;
  7336. }
  7337. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7338. {// pet which cannot be deleted
  7339. continue;
  7340. }
  7341. if( exact_match )
  7342. {
  7343. 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)) ) )
  7344. {// not matching attributes
  7345. continue;
  7346. }
  7347. if (exact_match&0x2) {
  7348. uint8 j;
  7349. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7350. 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)
  7351. break;
  7352. }
  7353. if (j != MAX_ITEM_RDM_OPT)
  7354. continue;
  7355. }
  7356. }
  7357. // count / delete item
  7358. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7359. }
  7360. if( amount )
  7361. {// not enough items
  7362. return false;
  7363. }
  7364. else if( delete_items )
  7365. {// we are done with the work
  7366. return true;
  7367. }
  7368. else
  7369. {// get rid of the items now
  7370. delete_items = true;
  7371. }
  7372. }
  7373. }
  7374. /// Deletes items from the target/attached player.
  7375. /// Prioritizes ordinary items.
  7376. ///
  7377. /// delitem <item id>,<amount>{,<account id>}
  7378. /// delitem "<item name>",<amount>{,<account id>}
  7379. /// cartdelitem <item id>,<amount>{,<account id>}
  7380. /// cartdelitem "<item name>",<amount>{,<account id>}
  7381. /// storagedelitem <item id>,<amount>{,<account id>}
  7382. /// storagedelitem "<item name>",<amount>{,<account id>}
  7383. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7384. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7385. BUILDIN_FUNC(delitem)
  7386. {
  7387. TBL_PC *sd;
  7388. struct item it;
  7389. uint8 loc = 0;
  7390. char* command = (char*)script_getfuncname(st);
  7391. if(!strncmp(command, "cart", 4))
  7392. loc = TABLE_CART;
  7393. else if(!strncmp(command, "storage", 7))
  7394. loc = TABLE_STORAGE;
  7395. else if(!strncmp(command, "guildstorage", 12))
  7396. loc = TABLE_GUILD_STORAGE;
  7397. if( !script_accid2sd(4,sd) ){
  7398. // In any case cancel script execution
  7399. st->state = END;
  7400. return SCRIPT_CMD_SUCCESS;
  7401. }
  7402. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7403. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7404. script_pushint(st, -1);
  7405. return SCRIPT_CMD_FAILURE;
  7406. }
  7407. if (loc == TABLE_GUILD_STORAGE) {
  7408. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7409. if (gstor == NULL || sd->state.storage_flag) {
  7410. script_pushint(st, -1);
  7411. return SCRIPT_CMD_FAILURE;
  7412. }
  7413. }
  7414. if( script_isstring(st, 2) )
  7415. {
  7416. const char* item_name = script_getstr(st, 2);
  7417. std::shared_ptr<item_data> id = item_db.searchname(item_name);
  7418. if( id == nullptr ){
  7419. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7420. st->state = END;
  7421. return SCRIPT_CMD_FAILURE;
  7422. }
  7423. it.nameid = id->nameid;// "<item name>"
  7424. }
  7425. else
  7426. {
  7427. it.nameid = script_getnum(st, 2);// <item id>
  7428. if( !item_db.exists( it.nameid ) )
  7429. {
  7430. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7431. st->state = END;
  7432. return SCRIPT_CMD_FAILURE;
  7433. }
  7434. }
  7435. it.amount=script_getnum(st,3);
  7436. if( it.amount <= 0 )
  7437. return SCRIPT_CMD_SUCCESS;// nothing to do
  7438. if( buildin_delitem_search(sd, &it, 0, loc) )
  7439. {// success
  7440. return SCRIPT_CMD_SUCCESS;
  7441. }
  7442. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7443. st->state = END;
  7444. st->mes_active = 0;
  7445. clif_scriptclose(sd, st->oid);
  7446. return SCRIPT_CMD_FAILURE;
  7447. }
  7448. /// Deletes items from the target/attached player.
  7449. ///
  7450. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7451. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7452. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7453. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7454. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7455. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7456. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7457. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7458. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7459. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7460. /// delitem4 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7461. /// delitem4 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<grade>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7462. BUILDIN_FUNC(delitem2)
  7463. {
  7464. TBL_PC *sd;
  7465. uint8 loc = 0;
  7466. char* command = (char*)script_getfuncname(st);
  7467. int aid_pos = 11;
  7468. if(!strncmp(command, "cart", 4))
  7469. loc = TABLE_CART;
  7470. else if(!strncmp(command, "storage", 7))
  7471. loc = TABLE_STORAGE;
  7472. else if(!strncmp(command, "guildstorage", 12))
  7473. loc = TABLE_GUILD_STORAGE;
  7474. if (command[strlen(command)-1] == '3')
  7475. aid_pos = 14;
  7476. else if (command[strlen(command)-1] == '4')
  7477. aid_pos = 15;
  7478. if( !script_accid2sd(aid_pos,sd) ){
  7479. // In any case cancel script execution
  7480. st->state = END;
  7481. return SCRIPT_CMD_SUCCESS;
  7482. }
  7483. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7484. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7485. script_pushint(st,-1);
  7486. return SCRIPT_CMD_FAILURE;
  7487. }
  7488. if (loc == TABLE_GUILD_STORAGE) {
  7489. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7490. if (gstor == NULL || sd->state.storage_flag) {
  7491. script_pushint(st, -1);
  7492. return SCRIPT_CMD_FAILURE;
  7493. }
  7494. }
  7495. struct item it = {};
  7496. if( script_isstring(st, 2) )
  7497. {
  7498. const char* item_name = script_getstr(st, 2);
  7499. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  7500. if( id == nullptr ){
  7501. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7502. st->state = END;
  7503. return SCRIPT_CMD_FAILURE;
  7504. }
  7505. it.nameid = id->nameid;// "<item name>"
  7506. }
  7507. else
  7508. {
  7509. it.nameid = script_getnum(st, 2);// <item id>
  7510. if( !item_db.exists( it.nameid ) )
  7511. {
  7512. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7513. st->state = END;
  7514. return SCRIPT_CMD_FAILURE;
  7515. }
  7516. }
  7517. it.amount=script_getnum(st,3);
  7518. it.identify=script_getnum(st,4);
  7519. it.refine=script_getnum(st,5);
  7520. it.attribute=script_getnum(st,6);
  7521. it.card[0]=script_getnum(st,7);
  7522. it.card[1]=script_getnum(st,8);
  7523. it.card[2]=script_getnum(st,9);
  7524. it.card[3]=script_getnum(st,10);
  7525. uint8 flag = 0x1;
  7526. if (command[strlen(command)-1] == '3' || command[strlen(command)-1] == '4') {
  7527. int offset = 11;
  7528. if (command[strlen(command)-1] == '4') {
  7529. offset = 12;
  7530. int grade = script_getnum(st, 11);
  7531. if (grade < ENCHANTGRADE_NONE || grade > MAX_ENCHANTGRADE) {
  7532. ShowError( "buildin_%s: Not a correct grade! Grade=%d\n", command, grade );
  7533. return SCRIPT_CMD_FAILURE;
  7534. }
  7535. it.enchantgrade = static_cast<e_enchantgrade>(grade);
  7536. }
  7537. bool res = script_getitem_randomoption(st, sd, &it, command, offset);
  7538. if (!res)
  7539. return SCRIPT_CMD_FAILURE;
  7540. flag |= 0x2;
  7541. }
  7542. if( it.amount <= 0 )
  7543. return SCRIPT_CMD_SUCCESS;// nothing to do
  7544. if( buildin_delitem_search(sd, &it, flag, loc) )
  7545. {// success
  7546. return SCRIPT_CMD_SUCCESS;
  7547. }
  7548. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7549. st->state = END;
  7550. st->mes_active = 0;
  7551. clif_scriptclose(sd, st->oid);
  7552. return SCRIPT_CMD_FAILURE;
  7553. }
  7554. /// Deletes items from the target/attached player at given index.
  7555. /// delitemidx <index>{,<amount>{,<char id>}};
  7556. BUILDIN_FUNC(delitemidx) {
  7557. struct map_session_data* sd;
  7558. if (!script_charid2sd(4, sd)) {
  7559. script_pushint(st, false);
  7560. return SCRIPT_CMD_FAILURE;
  7561. }
  7562. int idx = script_getnum(st, 2);
  7563. if (idx < 0 || idx >= MAX_INVENTORY) {
  7564. ShowWarning("buildin_delitemidx: Index %d is out of the range 0-%d.\n", idx, MAX_INVENTORY - 1);
  7565. script_pushint(st, false);
  7566. return SCRIPT_CMD_FAILURE;
  7567. }
  7568. if (sd->inventory_data[idx] == nullptr) {
  7569. 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);
  7570. script_pushint(st, false);
  7571. return SCRIPT_CMD_FAILURE;
  7572. }
  7573. int amount;
  7574. if (script_hasdata(st, 3))
  7575. amount = script_getnum(st, 3);
  7576. else
  7577. amount = sd->inventory.u.items_inventory[idx].amount;
  7578. if (amount > 0)
  7579. script_pushint(st, pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_SCRIPT) == 0);
  7580. else
  7581. script_pushint(st, false);
  7582. return SCRIPT_CMD_SUCCESS;
  7583. }
  7584. /*==========================================
  7585. * Enables/Disables use of items while in an NPC [Skotlex]
  7586. *------------------------------------------*/
  7587. BUILDIN_FUNC(enableitemuse)
  7588. {
  7589. TBL_PC *sd;
  7590. if (script_rid2sd(sd))
  7591. st->npc_item_flag = sd->npc_item_flag = 1;
  7592. return SCRIPT_CMD_SUCCESS;
  7593. }
  7594. BUILDIN_FUNC(disableitemuse)
  7595. {
  7596. TBL_PC *sd;
  7597. if (script_rid2sd(sd))
  7598. st->npc_item_flag = sd->npc_item_flag = 0;
  7599. return SCRIPT_CMD_SUCCESS;
  7600. }
  7601. /*==========================================
  7602. * Returns a character's specified stat.
  7603. * Check pc_readparam for available options.
  7604. * readparam <param>{,"<nick>"}
  7605. * readparam <param>{,<char_id>}
  7606. *------------------------------------------*/
  7607. BUILDIN_FUNC(readparam)
  7608. {
  7609. int value;
  7610. struct script_data *data = script_getdata(st, 2);
  7611. TBL_PC *sd = NULL;
  7612. if( script_hasdata(st, 3) ){
  7613. if (script_isint(st, 3))
  7614. script_charid2sd(3, sd);
  7615. else
  7616. script_nick2sd(3, sd);
  7617. }else{
  7618. script_rid2sd(sd);
  7619. }
  7620. if( !sd ){
  7621. script_pushint(st, -1);
  7622. return SCRIPT_CMD_FAILURE;
  7623. }
  7624. // If you use a parameter, return the value behind it
  7625. if( reference_toparam(data) ){
  7626. get_val_(st, data, sd);
  7627. value = (int)data->u.num;
  7628. }else{
  7629. value = static_cast<int>(pc_readparam(sd,script_getnum(st, 2)));
  7630. }
  7631. script_pushint(st,value);
  7632. return SCRIPT_CMD_SUCCESS;
  7633. }
  7634. /*==========================================
  7635. * Return charid identification
  7636. * return by @num :
  7637. * 0 : char_id
  7638. * 1 : party_id
  7639. * 2 : guild_id
  7640. * 3 : account_id
  7641. * 4 : bg_id
  7642. * 5 : clan_id
  7643. *------------------------------------------*/
  7644. BUILDIN_FUNC(getcharid)
  7645. {
  7646. int num;
  7647. TBL_PC *sd;
  7648. num = script_getnum(st,2);
  7649. if( !script_nick2sd(3,sd) ){
  7650. script_pushint(st,0); //return 0, according docs
  7651. return SCRIPT_CMD_SUCCESS;
  7652. }
  7653. switch( num ) {
  7654. case 0: script_pushint(st,sd->status.char_id); break;
  7655. case 1: script_pushint(st,sd->status.party_id); break;
  7656. case 2: script_pushint(st,sd->status.guild_id); break;
  7657. case 3: script_pushint(st,sd->status.account_id); break;
  7658. case 4: script_pushint(st,sd->bg_id); break;
  7659. case 5: script_pushint(st,sd->status.clan_id); break;
  7660. default:
  7661. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7662. script_pushint(st,0);
  7663. break;
  7664. }
  7665. return SCRIPT_CMD_SUCCESS;
  7666. }
  7667. /*==========================================
  7668. * returns the GID of an NPC
  7669. * Returns 0 if the NPC name provided is not found.
  7670. *------------------------------------------*/
  7671. BUILDIN_FUNC(getnpcid)
  7672. {
  7673. int num = script_getnum(st,2);
  7674. struct npc_data* nd = NULL;
  7675. if( script_hasdata(st,3) )
  7676. {// unique npc name
  7677. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7678. {
  7679. //Npc not found.
  7680. script_pushint(st,0);
  7681. return SCRIPT_CMD_SUCCESS;
  7682. }
  7683. }
  7684. switch (num) {
  7685. case 0:
  7686. script_pushint(st,nd ? nd->bl.id : st->oid);
  7687. break;
  7688. default:
  7689. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7690. script_pushint(st,0);
  7691. return SCRIPT_CMD_FAILURE;
  7692. }
  7693. return SCRIPT_CMD_SUCCESS;
  7694. }
  7695. /*==========================================
  7696. * Return the name of the party_id
  7697. * null if not found
  7698. *------------------------------------------*/
  7699. BUILDIN_FUNC(getpartyname)
  7700. {
  7701. int party_id;
  7702. struct party_data* p;
  7703. party_id = script_getnum(st,2);
  7704. if( ( p = party_search(party_id) ) != NULL )
  7705. {
  7706. script_pushstrcopy(st,p->party.name);
  7707. }
  7708. else
  7709. {
  7710. script_pushconststr(st,"null");
  7711. }
  7712. return SCRIPT_CMD_SUCCESS;
  7713. }
  7714. /*==========================================
  7715. * Get the information of the members of a party by type
  7716. * @party_id, @type
  7717. * return by @type :
  7718. * - : nom des membres
  7719. * 1 : char_id des membres
  7720. * 2 : account_id des membres
  7721. *------------------------------------------*/
  7722. BUILDIN_FUNC(getpartymember)
  7723. {
  7724. struct party_data *p;
  7725. uint8 j = 0;
  7726. p = party_search(script_getnum(st,2));
  7727. if (p != NULL) {
  7728. uint8 i, type = 0;
  7729. struct script_data *data = NULL;
  7730. char *varname = NULL;
  7731. if (script_hasdata(st,3))
  7732. type = script_getnum(st,3);
  7733. if (script_hasdata(st,4)) {
  7734. data = script_getdata(st, 4);
  7735. if (!data_isreference(data)) {
  7736. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7737. return SCRIPT_CMD_FAILURE;
  7738. }
  7739. varname = reference_getname(data);
  7740. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7741. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7742. return SCRIPT_CMD_FAILURE;
  7743. }
  7744. if (not_server_variable(*varname)) {
  7745. struct map_session_data *sd;
  7746. if (!script_rid2sd(sd)) {
  7747. ShowError("buildin_getpartymember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  7748. return SCRIPT_CMD_FAILURE;
  7749. }
  7750. }
  7751. }
  7752. for (i = 0; i < MAX_PARTY; i++) {
  7753. if (p->party.member[i].account_id) {
  7754. switch (type) {
  7755. case 2:
  7756. if (data)
  7757. setd_sub_num( st, NULL, varname, j, p->party.member[i].account_id, data->ref );
  7758. else
  7759. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7760. break;
  7761. case 1:
  7762. if (data)
  7763. setd_sub_num( st, NULL, varname, j, p->party.member[i].char_id, data->ref );
  7764. else
  7765. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7766. break;
  7767. default:
  7768. if (data)
  7769. setd_sub_str( st, NULL, varname, j, p->party.member[i].name, data->ref );
  7770. else
  7771. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7772. break;
  7773. }
  7774. j++;
  7775. }
  7776. }
  7777. }
  7778. mapreg_setreg(add_str("$@partymembercount"),j);
  7779. script_pushint(st, j);
  7780. return SCRIPT_CMD_SUCCESS;
  7781. }
  7782. /*==========================================
  7783. * Retrieves party leader. if flag is specified,
  7784. * return some of the leader data. Otherwise, return name.
  7785. *------------------------------------------*/
  7786. BUILDIN_FUNC(getpartyleader)
  7787. {
  7788. int party_id, type = 0, i=0;
  7789. struct party_data *p;
  7790. party_id=script_getnum(st,2);
  7791. if( script_hasdata(st,3) )
  7792. type=script_getnum(st,3);
  7793. p=party_search(party_id);
  7794. if (p) //Search leader
  7795. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7796. if (!p || i == MAX_PARTY) { //leader not found
  7797. if (type)
  7798. script_pushint(st,-1);
  7799. else
  7800. script_pushconststr(st,"null");
  7801. return SCRIPT_CMD_SUCCESS;
  7802. }
  7803. switch (type) {
  7804. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7805. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7806. case 3: script_pushint(st,p->party.member[i].class_); break;
  7807. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7808. case 5: script_pushint(st,p->party.member[i].lv); break;
  7809. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7810. }
  7811. return SCRIPT_CMD_SUCCESS;
  7812. }
  7813. /*==========================================
  7814. * Return the name of the @guild_id
  7815. * null if not found
  7816. *------------------------------------------*/
  7817. BUILDIN_FUNC(getguildname)
  7818. {
  7819. int guild_id;
  7820. struct guild* g;
  7821. guild_id = script_getnum(st,2);
  7822. if( ( g = guild_search(guild_id) ) != NULL )
  7823. script_pushstrcopy(st,g->name);
  7824. else
  7825. script_pushconststr(st,"null");
  7826. return SCRIPT_CMD_SUCCESS;
  7827. }
  7828. /*==========================================
  7829. * Return the name of the guild master of @guild_id
  7830. * null if not found
  7831. *------------------------------------------*/
  7832. BUILDIN_FUNC(getguildmaster)
  7833. {
  7834. int guild_id;
  7835. struct guild* g;
  7836. guild_id = script_getnum(st,2);
  7837. if( ( g = guild_search(guild_id) ) != NULL )
  7838. script_pushstrcopy(st,g->member[0].name);
  7839. else
  7840. script_pushconststr(st,"null");
  7841. return SCRIPT_CMD_SUCCESS;
  7842. }
  7843. BUILDIN_FUNC(getguildmasterid)
  7844. {
  7845. int guild_id;
  7846. struct guild* g;
  7847. guild_id = script_getnum(st,2);
  7848. if( ( g = guild_search(guild_id) ) != NULL )
  7849. script_pushint(st,g->member[0].char_id);
  7850. else
  7851. script_pushint(st,0);
  7852. return SCRIPT_CMD_SUCCESS;
  7853. }
  7854. /*==========================================
  7855. * Get char string information by type :
  7856. * Return by @type :
  7857. * 0 : char_name
  7858. * 1 : party_name or ""
  7859. * 2 : guild_name or ""
  7860. * 3 : map_name
  7861. * - : ""
  7862. * strcharinfo(<type>{,<char_id>})
  7863. *------------------------------------------*/
  7864. BUILDIN_FUNC(strcharinfo)
  7865. {
  7866. TBL_PC *sd;
  7867. int num;
  7868. struct guild* g;
  7869. struct party_data* p;
  7870. if (!script_charid2sd(3,sd)) {
  7871. script_pushconststr(st,"");
  7872. return SCRIPT_CMD_FAILURE;
  7873. }
  7874. num=script_getnum(st,2);
  7875. switch(num){
  7876. case 0:
  7877. script_pushstrcopy(st,sd->status.name);
  7878. break;
  7879. case 1:
  7880. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7881. script_pushstrcopy(st,p->party.name);
  7882. } else {
  7883. script_pushconststr(st,"");
  7884. }
  7885. break;
  7886. case 2:
  7887. if( ( g = sd->guild ) != NULL ) {
  7888. script_pushstrcopy(st,g->name);
  7889. } else {
  7890. script_pushconststr(st,"");
  7891. }
  7892. break;
  7893. case 3:
  7894. script_pushconststr(st,map_getmapdata(sd->bl.m)->name);
  7895. break;
  7896. default:
  7897. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7898. script_pushconststr(st,"");
  7899. break;
  7900. }
  7901. return SCRIPT_CMD_SUCCESS;
  7902. }
  7903. /*==========================================
  7904. * Get npc string information by type
  7905. * return by @type:
  7906. * 0 : name
  7907. * 1 : str#
  7908. * 2 : #str
  7909. * 3 : ::str
  7910. * 4 : map name
  7911. *------------------------------------------*/
  7912. BUILDIN_FUNC(strnpcinfo)
  7913. {
  7914. TBL_NPC* nd;
  7915. int num;
  7916. char *buf,*name=NULL;
  7917. nd = map_id2nd(st->oid);
  7918. if (!nd) {
  7919. script_pushconststr(st, "");
  7920. return SCRIPT_CMD_SUCCESS;
  7921. }
  7922. num = script_getnum(st,2);
  7923. switch(num){
  7924. case 0: // display name
  7925. name = aStrdup(nd->name);
  7926. break;
  7927. case 1: // visible part of display name
  7928. if((buf = strchr(nd->name,'#')) != NULL)
  7929. {
  7930. name = aStrdup(nd->name);
  7931. name[buf - nd->name] = 0;
  7932. } else // Return the name, there is no '#' present
  7933. name = aStrdup(nd->name);
  7934. break;
  7935. case 2: // # fragment
  7936. if((buf = strchr(nd->name,'#')) != NULL)
  7937. name = aStrdup(buf+1);
  7938. break;
  7939. case 3: // unique name
  7940. name = aStrdup(nd->exname);
  7941. break;
  7942. case 4: // map name
  7943. if (nd->bl.m >= 0)
  7944. name = aStrdup(map_getmapdata(nd->bl.m)->name);
  7945. break;
  7946. }
  7947. if(name)
  7948. script_pushstr(st, name);
  7949. else
  7950. script_pushconststr(st, "");
  7951. return SCRIPT_CMD_SUCCESS;
  7952. }
  7953. /**
  7954. * getequipid({<equipment slot>,<char_id>})
  7955. **/
  7956. BUILDIN_FUNC(getequipid)
  7957. {
  7958. int i, num;
  7959. TBL_PC* sd;
  7960. if (!script_charid2sd(3, sd)) {
  7961. script_pushint(st,-1);
  7962. return SCRIPT_CMD_FAILURE;
  7963. }
  7964. if (script_hasdata(st, 2))
  7965. num = script_getnum(st, 2);
  7966. else
  7967. num = EQI_COMPOUND_ON;
  7968. if (num == EQI_COMPOUND_ON)
  7969. i = current_equip_item_index;
  7970. else if (equip_index_check(num)) // get inventory position of item
  7971. i = pc_checkequip(sd, equip_bitmask[num]);
  7972. else {
  7973. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7974. script_pushint(st,-1);
  7975. return SCRIPT_CMD_FAILURE;
  7976. }
  7977. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7978. script_pushint(st, sd->inventory_data[i]->nameid);
  7979. else
  7980. script_pushint(st, -1);
  7981. return SCRIPT_CMD_SUCCESS;
  7982. }
  7983. /*==========================================
  7984. * getequipuniqueid(<equipment slot>{,<char_id>})
  7985. *------------------------------------------*/
  7986. BUILDIN_FUNC(getequipuniqueid)
  7987. {
  7988. int i, num;
  7989. TBL_PC* sd;
  7990. struct item* item;
  7991. if (!script_charid2sd(3, sd)) {
  7992. script_pushconststr(st, "");
  7993. return SCRIPT_CMD_FAILURE;
  7994. }
  7995. num = script_getnum(st,2);
  7996. if ( !equip_index_check(num) ) {
  7997. script_pushconststr(st, "");
  7998. return SCRIPT_CMD_FAILURE;
  7999. }
  8000. // get inventory position of item
  8001. i = pc_checkequip(sd,equip_bitmask[num]);
  8002. if (i < 0) {
  8003. script_pushconststr(st, "");
  8004. return SCRIPT_CMD_FAILURE;
  8005. }
  8006. item = &sd->inventory.u.items_inventory[i];
  8007. if (item != 0) {
  8008. int maxlen = 256;
  8009. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  8010. memset(buf, 0, maxlen);
  8011. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  8012. script_pushstr(st, buf);
  8013. } else
  8014. script_pushconststr(st, "");
  8015. return SCRIPT_CMD_SUCCESS;
  8016. }
  8017. /*==========================================
  8018. * Get the equipement name at pos
  8019. * return item jname or ""
  8020. * getequipname(<equipment slot>{,<char_id>})
  8021. *------------------------------------------*/
  8022. BUILDIN_FUNC(getequipname)
  8023. {
  8024. int i, num;
  8025. TBL_PC* sd;
  8026. struct item_data* item;
  8027. if (!script_charid2sd(3, sd)) {
  8028. script_pushconststr(st, "");
  8029. return SCRIPT_CMD_FAILURE;
  8030. }
  8031. num = script_getnum(st,2);
  8032. if( !equip_index_check(num) )
  8033. {
  8034. script_pushconststr(st,"");
  8035. return SCRIPT_CMD_SUCCESS;
  8036. }
  8037. // get inventory position of item
  8038. i = pc_checkequip(sd,equip_bitmask[num]);
  8039. if( i < 0 )
  8040. {
  8041. script_pushconststr(st,"");
  8042. return SCRIPT_CMD_SUCCESS;
  8043. }
  8044. item = sd->inventory_data[i];
  8045. if( item != 0 )
  8046. script_pushstrcopy(st,item->ename.c_str());
  8047. else
  8048. script_pushconststr(st,"");
  8049. return SCRIPT_CMD_SUCCESS;
  8050. }
  8051. /*==========================================
  8052. * getbrokenid [Valaris]
  8053. * getbrokenid(<number>{,<char_id>})
  8054. *------------------------------------------*/
  8055. BUILDIN_FUNC(getbrokenid)
  8056. {
  8057. int i,num,id = 0,brokencounter = 0;
  8058. TBL_PC *sd;
  8059. if (!script_charid2sd(3, sd)) {
  8060. script_pushint(st,0);
  8061. return SCRIPT_CMD_FAILURE;
  8062. }
  8063. num = script_getnum(st,2);
  8064. for(i = 0; i < MAX_INVENTORY; i++) {
  8065. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8066. brokencounter++;
  8067. if(num == brokencounter){
  8068. id = sd->inventory.u.items_inventory[i].nameid;
  8069. break;
  8070. }
  8071. }
  8072. }
  8073. script_pushint(st,id);
  8074. return SCRIPT_CMD_SUCCESS;
  8075. }
  8076. /*==========================================
  8077. * repair [Valaris]
  8078. * repair <num>{,<char_id>};
  8079. *------------------------------------------*/
  8080. BUILDIN_FUNC(repair)
  8081. {
  8082. int i,num;
  8083. int repaircounter = 0;
  8084. TBL_PC *sd;
  8085. if (!script_charid2sd(3,sd))
  8086. return SCRIPT_CMD_FAILURE;
  8087. num = script_getnum(st,2);
  8088. for(i = 0; i < MAX_INVENTORY; i++) {
  8089. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  8090. repaircounter++;
  8091. if(num == repaircounter) {
  8092. sd->inventory.u.items_inventory[i].attribute = 0;
  8093. clif_equiplist(sd);
  8094. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  8095. clif_misceffect(&sd->bl, 3);
  8096. break;
  8097. }
  8098. }
  8099. }
  8100. return SCRIPT_CMD_SUCCESS;
  8101. }
  8102. /*==========================================
  8103. * repairall {<char_id>}
  8104. *------------------------------------------*/
  8105. BUILDIN_FUNC(repairall)
  8106. {
  8107. int i, repaircounter = 0;
  8108. TBL_PC *sd;
  8109. if (!script_charid2sd(2,sd))
  8110. return SCRIPT_CMD_FAILURE;
  8111. for(i = 0; i < MAX_INVENTORY; i++)
  8112. {
  8113. 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] ) ){
  8114. sd->inventory.u.items_inventory[i].attribute = 0;
  8115. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  8116. repaircounter++;
  8117. }
  8118. }
  8119. if(repaircounter)
  8120. {
  8121. clif_misceffect(&sd->bl, 3);
  8122. clif_equiplist(sd);
  8123. }
  8124. return SCRIPT_CMD_SUCCESS;
  8125. }
  8126. /*==========================================
  8127. * Chk if player have something equiped at pos
  8128. * getequipisequiped <pos>{,<char_id>}
  8129. *------------------------------------------*/
  8130. BUILDIN_FUNC(getequipisequiped)
  8131. {
  8132. int i = -1,num;
  8133. TBL_PC *sd;
  8134. num = script_getnum(st,2);
  8135. if (!script_charid2sd(3,sd))
  8136. return SCRIPT_CMD_FAILURE;
  8137. if ( equip_index_check(num) )
  8138. i=pc_checkequip(sd,equip_bitmask[num]);
  8139. if(i >= 0)
  8140. script_pushint(st,1);
  8141. else
  8142. script_pushint(st,0);
  8143. return SCRIPT_CMD_SUCCESS;
  8144. }
  8145. /*==========================================
  8146. * Chk if the player have something equiped at pos
  8147. * if so chk if this item ain't marked not refinable or rental
  8148. * return (npc)
  8149. * 1 : true
  8150. * 0 : false
  8151. * getequipisenableref(<equipment slot>{,<char_id>})
  8152. *------------------------------------------*/
  8153. BUILDIN_FUNC(getequipisenableref)
  8154. {
  8155. int i = -1,num;
  8156. TBL_PC *sd;
  8157. num = script_getnum(st,2);
  8158. if (!script_charid2sd(3, sd)) {
  8159. script_pushint(st,0);
  8160. return SCRIPT_CMD_FAILURE;
  8161. }
  8162. if( equip_index_check(num) )
  8163. i = pc_checkequip(sd,equip_bitmask[num]);
  8164. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  8165. script_pushint(st,1);
  8166. else
  8167. script_pushint(st,0);
  8168. return SCRIPT_CMD_SUCCESS;
  8169. }
  8170. /*==========================================
  8171. * Get the item refined value at pos
  8172. * return (npc)
  8173. * x : refine amount
  8174. * 0 : false (not refined)
  8175. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  8176. *------------------------------------------*/
  8177. BUILDIN_FUNC(getequiprefinerycnt)
  8178. {
  8179. int i = -1,num;
  8180. TBL_PC *sd;
  8181. num = script_getnum(st,2);
  8182. if (!script_charid2sd(3, sd)) {
  8183. script_pushint(st,0);
  8184. return SCRIPT_CMD_FAILURE;
  8185. }
  8186. if (equip_index_check(num))
  8187. i=pc_checkequip(sd,equip_bitmask[num]);
  8188. if(i >= 0)
  8189. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  8190. else
  8191. script_pushint(st,0);
  8192. return SCRIPT_CMD_SUCCESS;
  8193. }
  8194. /*==========================================
  8195. * Get the weapon level value at pos
  8196. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  8197. * return (npc)
  8198. * x : weapon level
  8199. * 0 : false
  8200. * getequipweaponlv({<equipment slot>{,<char_id>}})
  8201. *------------------------------------------*/
  8202. BUILDIN_FUNC(getequipweaponlv)
  8203. {
  8204. int num;
  8205. if( script_hasdata( st, 2 ) ){
  8206. num = script_getnum(st, 2);
  8207. }else{
  8208. num = EQI_COMPOUND_ON;
  8209. }
  8210. struct map_session_data* sd;
  8211. if (!script_charid2sd(3, sd)) {
  8212. script_pushint(st, 0);
  8213. return SCRIPT_CMD_FAILURE;
  8214. }
  8215. int i = -1;
  8216. if( num == EQI_COMPOUND_ON ){
  8217. if( current_equip_item_index == -1 ){
  8218. script_pushint(st, 0);
  8219. return SCRIPT_CMD_FAILURE;
  8220. }
  8221. i = current_equip_item_index;
  8222. }else if (equip_index_check(num))
  8223. i = pc_checkequip(sd, equip_bitmask[num]);
  8224. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON ){
  8225. script_pushint( st, sd->inventory_data[i]->weapon_level );
  8226. }else{
  8227. script_pushint( st, 0 );
  8228. }
  8229. return SCRIPT_CMD_SUCCESS;
  8230. }
  8231. BUILDIN_FUNC(getequiparmorlv){
  8232. int num;
  8233. if( script_hasdata( st, 2 ) ){
  8234. num = script_getnum(st, 2);
  8235. }else{
  8236. num = EQI_COMPOUND_ON;
  8237. }
  8238. struct map_session_data* sd;
  8239. if( !script_charid2sd( 3, sd ) ){
  8240. script_pushint( st, 0 );
  8241. return SCRIPT_CMD_FAILURE;
  8242. }
  8243. int i = -1;
  8244. if( num == EQI_COMPOUND_ON ){
  8245. if( current_equip_item_index == -1 ){
  8246. script_pushint( st, 0 );
  8247. return SCRIPT_CMD_FAILURE;
  8248. }
  8249. i = current_equip_item_index;
  8250. }else if( equip_index_check( num ) ){
  8251. i = pc_checkequip( sd, equip_bitmask[num] );
  8252. }else{
  8253. ShowError( "buildin_getequiparmorlv: unsupported equip index %d.\n", num );
  8254. script_pushint( st, 0 );
  8255. return SCRIPT_CMD_FAILURE;
  8256. }
  8257. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_ARMOR ){
  8258. script_pushint( st, sd->inventory_data[i]->armor_level );
  8259. }else{
  8260. script_pushint( st, 0 );
  8261. }
  8262. return SCRIPT_CMD_SUCCESS;
  8263. }
  8264. /*==========================================
  8265. * Get the item refine chance (from refine.txt) for item at pos
  8266. * return (npc)
  8267. * x : refine chance
  8268. * 0 : false (max refine level or unequip..)
  8269. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8270. *------------------------------------------*/
  8271. BUILDIN_FUNC(getequippercentrefinery)
  8272. {
  8273. int i = -1,num;
  8274. bool enriched = false;
  8275. TBL_PC *sd;
  8276. num = script_getnum(st,2);
  8277. if (script_hasdata(st, 3))
  8278. enriched = script_getnum(st, 3) != 0;
  8279. if (!script_charid2sd(4, sd)) {
  8280. script_pushint(st,0);
  8281. return SCRIPT_CMD_FAILURE;
  8282. }
  8283. if (equip_index_check(num))
  8284. i = pc_checkequip(sd,equip_bitmask[num]);
  8285. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid) {
  8286. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  8287. if( info == nullptr ){
  8288. script_pushint( st, 0 );
  8289. return SCRIPT_CMD_SUCCESS;
  8290. }
  8291. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, (uint16)( enriched ? REFINE_COST_ENRICHED : REFINE_COST_NORMAL ) );
  8292. if( cost == nullptr ){
  8293. script_pushint( st, 0 );
  8294. return SCRIPT_CMD_SUCCESS;
  8295. }
  8296. script_pushint( st, cost->chance / 100 );
  8297. }
  8298. else
  8299. script_pushint(st,0);
  8300. return SCRIPT_CMD_SUCCESS;
  8301. }
  8302. /*==========================================
  8303. * Refine +1 item at pos and log and display refine
  8304. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8305. *------------------------------------------*/
  8306. BUILDIN_FUNC(successrefitem) {
  8307. short i = -1, up = 1;
  8308. int pos;
  8309. TBL_PC *sd;
  8310. pos = script_getnum(st,2);
  8311. if (!script_charid2sd(4, sd)) {
  8312. script_pushint(st, -1);
  8313. return SCRIPT_CMD_FAILURE;
  8314. }
  8315. if (script_hasdata(st, 3))
  8316. up = script_getnum(st, 3);
  8317. if (equip_index_check(pos))
  8318. i = pc_checkequip(sd,equip_bitmask[pos]);
  8319. if (i >= 0) {
  8320. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8321. //Logs items, got from (N)PC scripts [Lupus]
  8322. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8323. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8324. script_pushint(st, MAX_REFINE);
  8325. return SCRIPT_CMD_SUCCESS;
  8326. }
  8327. sd->inventory.u.items_inventory[i].refine += up;
  8328. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8329. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8330. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8331. clif_delitem(sd,i,1,3);
  8332. //Logs items, got from (N)PC scripts [Lupus]
  8333. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8334. clif_additem(sd,i,1,0);
  8335. pc_equipitem(sd,i,ep);
  8336. clif_misceffect(&sd->bl,3);
  8337. if( sd->inventory_data[i]->type == IT_WEAPON ){
  8338. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->weapon_level, sd->inventory.u.items_inventory[i].refine);
  8339. }
  8340. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8341. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8342. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]) &&
  8343. sd->inventory_data[i]->type == IT_WEAPON )
  8344. { // Fame point system [DracoRPG]
  8345. switch( sd->inventory_data[i]->weapon_level ){
  8346. case 1:
  8347. pc_addfame(*sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8348. break;
  8349. case 2:
  8350. pc_addfame(*sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8351. break;
  8352. case 3:
  8353. pc_addfame(*sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8354. break;
  8355. }
  8356. }
  8357. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8358. return SCRIPT_CMD_SUCCESS;
  8359. }
  8360. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8361. script_pushint(st, -1);
  8362. return SCRIPT_CMD_FAILURE;
  8363. }
  8364. /*==========================================
  8365. * Show a failed Refine +1 attempt
  8366. * failedrefitem <equipment slot>{,<char_id>}
  8367. *------------------------------------------*/
  8368. BUILDIN_FUNC(failedrefitem) {
  8369. short i = -1;
  8370. int pos;
  8371. TBL_PC *sd;
  8372. pos = script_getnum(st,2);
  8373. if (!script_charid2sd(3, sd)) {
  8374. script_pushint(st, 0);
  8375. return SCRIPT_CMD_FAILURE;
  8376. }
  8377. if (equip_index_check(pos))
  8378. i = pc_checkequip(sd,equip_bitmask[pos]);
  8379. if (i >= 0) {
  8380. sd->inventory.u.items_inventory[i].refine = 0;
  8381. pc_unequipitem(sd,i,3); //recalculate bonus
  8382. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8383. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8384. clif_misceffect(&sd->bl,2); // display failure effect
  8385. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8386. script_pushint(st, 1);
  8387. return SCRIPT_CMD_SUCCESS;
  8388. }
  8389. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8390. script_pushint(st, 0);
  8391. return SCRIPT_CMD_FAILURE;
  8392. }
  8393. /*==========================================
  8394. * Downgrades an Equipment Part by -1 . [Masao]
  8395. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8396. *------------------------------------------*/
  8397. BUILDIN_FUNC(downrefitem) {
  8398. short i = -1, down = 1;
  8399. int pos;
  8400. TBL_PC *sd;
  8401. if (!script_charid2sd(4, sd)) {
  8402. script_pushint(st, -1);
  8403. return SCRIPT_CMD_FAILURE;
  8404. }
  8405. pos = script_getnum(st,2);
  8406. if (script_hasdata(st, 3))
  8407. down = script_getnum(st, 3);
  8408. if (equip_index_check(pos))
  8409. i = pc_checkequip(sd,equip_bitmask[pos]);
  8410. if (i >= 0) {
  8411. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8412. //Logs items, got from (N)PC scripts [Lupus]
  8413. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8414. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8415. sd->inventory.u.items_inventory[i].refine -= down;
  8416. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8417. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8418. clif_delitem(sd,i,1,3);
  8419. //Logs items, got from (N)PC scripts [Lupus]
  8420. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8421. clif_additem(sd,i,1,0);
  8422. pc_equipitem(sd,i,ep);
  8423. clif_misceffect(&sd->bl,2);
  8424. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8425. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8426. return SCRIPT_CMD_SUCCESS;
  8427. }
  8428. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8429. script_pushint(st, -1);
  8430. return SCRIPT_CMD_FAILURE;
  8431. }
  8432. /**
  8433. * Delete the item equipped at pos.
  8434. * delequip <equipment slot>{,<char_id>};
  8435. **/
  8436. BUILDIN_FUNC(delequip) {
  8437. short i = -1;
  8438. int pos;
  8439. int8 ret;
  8440. TBL_PC *sd;
  8441. pos = script_getnum(st,2);
  8442. if (!script_charid2sd(3,sd)) {
  8443. st->state = END;
  8444. return SCRIPT_CMD_FAILURE;
  8445. }
  8446. if (equip_index_check(pos))
  8447. i = pc_checkequip(sd,equip_bitmask[pos]);
  8448. if (i >= 0) {
  8449. pc_unequipitem(sd,i,3); //recalculate bonus
  8450. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8451. }
  8452. else {
  8453. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8454. st->state = END;
  8455. return SCRIPT_CMD_FAILURE;
  8456. }
  8457. script_pushint(st,ret);
  8458. return SCRIPT_CMD_SUCCESS;
  8459. }
  8460. /**
  8461. * Break the item equipped at pos.
  8462. * breakequip <equipment slot>{,<char_id>};
  8463. **/
  8464. BUILDIN_FUNC(breakequip) {
  8465. short i = -1;
  8466. int pos;
  8467. TBL_PC *sd;
  8468. pos = script_getnum(st,2);
  8469. if (!script_charid2sd(3,sd))
  8470. return SCRIPT_CMD_FAILURE;
  8471. if (equip_index_check(pos))
  8472. i = pc_checkequip(sd,equip_bitmask[pos]);
  8473. if (i >= 0) {
  8474. sd->inventory.u.items_inventory[i].attribute = 1;
  8475. pc_unequipitem(sd,i,3);
  8476. clif_equiplist(sd);
  8477. script_pushint(st,1);
  8478. return SCRIPT_CMD_SUCCESS;
  8479. }
  8480. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8481. script_pushint(st,0);
  8482. return SCRIPT_CMD_FAILURE;
  8483. }
  8484. /**
  8485. * statusup <stat>{,<char_id>};
  8486. **/
  8487. BUILDIN_FUNC(statusup)
  8488. {
  8489. int type;
  8490. TBL_PC *sd;
  8491. type = script_getnum(st,2);
  8492. if (!script_charid2sd(3, sd))
  8493. return SCRIPT_CMD_FAILURE;
  8494. pc_statusup(sd, type, 1);
  8495. return SCRIPT_CMD_SUCCESS;
  8496. }
  8497. /**
  8498. * statusup2 <stat>,<amount>{,<char_id>};
  8499. **/
  8500. BUILDIN_FUNC(statusup2)
  8501. {
  8502. int type,val;
  8503. TBL_PC *sd;
  8504. type=script_getnum(st,2);
  8505. val=script_getnum(st,3);
  8506. if (!script_charid2sd(4, sd))
  8507. return SCRIPT_CMD_FAILURE;
  8508. pc_statusup2(sd,type,val);
  8509. return SCRIPT_CMD_SUCCESS;
  8510. }
  8511. /**
  8512. * traitstatusup <stat>{,<char_id>};
  8513. **/
  8514. BUILDIN_FUNC(traitstatusup)
  8515. {
  8516. struct map_session_data *sd;
  8517. if (!script_charid2sd(3, sd))
  8518. return SCRIPT_CMD_FAILURE;
  8519. int type = script_getnum( st, 2 );
  8520. if( type < SP_POW || type > SP_CRT ){
  8521. ShowError( "buildin_traitstatusup: Unknown trait type %d\n", type );
  8522. return SCRIPT_CMD_FAILURE;
  8523. }
  8524. pc_traitstatusup( sd, type, 1 );
  8525. return SCRIPT_CMD_SUCCESS;
  8526. }
  8527. /**
  8528. * traitstatusup2 <stat>,<amount>{,<char_id>};
  8529. **/
  8530. BUILDIN_FUNC(traitstatusup2)
  8531. {
  8532. struct map_session_data *sd;
  8533. if (!script_charid2sd(4, sd))
  8534. return SCRIPT_CMD_FAILURE;
  8535. int type = script_getnum( st, 2 );
  8536. if( type < SP_POW || type > SP_CRT ){
  8537. ShowError( "buildin_traitstatusup2: Unknown trait type %d\n", type );
  8538. return SCRIPT_CMD_FAILURE;
  8539. }
  8540. pc_traitstatusup2( sd, type, script_getnum( st, 3 ) );
  8541. return SCRIPT_CMD_SUCCESS;
  8542. }
  8543. /// See 'doc/item_bonus.txt'
  8544. ///
  8545. /// bonus <bonus type>,<val1>;
  8546. /// bonus2 <bonus type>,<val1>,<val2>;
  8547. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8548. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8549. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8550. BUILDIN_FUNC(bonus)
  8551. {
  8552. int type;
  8553. int val1 = 0;
  8554. int val2 = 0;
  8555. int val3 = 0;
  8556. int val4 = 0;
  8557. int val5 = 0;
  8558. TBL_PC* sd;
  8559. if( !script_rid2sd(sd) )
  8560. return SCRIPT_CMD_SUCCESS; // no player attached
  8561. type = script_getnum(st,2);
  8562. switch( type ) {
  8563. case SP_AUTOSPELL:
  8564. case SP_AUTOSPELL_WHENHIT:
  8565. case SP_AUTOSPELL_ONSKILL:
  8566. case SP_SKILL_ATK:
  8567. case SP_SKILL_HEAL:
  8568. case SP_SKILL_HEAL2:
  8569. case SP_ADD_SKILL_BLOW:
  8570. case SP_CASTRATE:
  8571. case SP_ADDEFF_ONSKILL:
  8572. case SP_SKILL_USE_SP_RATE:
  8573. case SP_SKILL_COOLDOWN:
  8574. case SP_SKILL_FIXEDCAST:
  8575. case SP_SKILL_VARIABLECAST:
  8576. case SP_VARCASTRATE:
  8577. case SP_FIXCASTRATE:
  8578. case SP_SKILL_DELAY:
  8579. case SP_SKILL_USE_SP:
  8580. case SP_SUB_SKILL:
  8581. // these bonuses support skill names
  8582. if (script_isstring(st, 3)) {
  8583. const char *name = script_getstr(st, 3);
  8584. if (!(val1 = skill_name2id(name))) {
  8585. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8586. return SCRIPT_CMD_FAILURE;
  8587. }
  8588. } else {
  8589. val1 = script_getnum(st, 3);
  8590. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8591. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8592. return SCRIPT_CMD_FAILURE;
  8593. }
  8594. }
  8595. break;
  8596. default:
  8597. if (script_hasdata(st, 3))
  8598. val1 = script_getnum(st, 3);
  8599. break;
  8600. }
  8601. switch( script_lastdata(st)-2 ) {
  8602. case 0:
  8603. case 1:
  8604. pc_bonus(sd, type, val1);
  8605. break;
  8606. case 2:
  8607. val2 = script_getnum(st,4);
  8608. pc_bonus2(sd, type, val1, val2);
  8609. break;
  8610. case 3:
  8611. val2 = script_getnum(st,4);
  8612. val3 = script_getnum(st,5);
  8613. pc_bonus3(sd, type, val1, val2, val3);
  8614. break;
  8615. case 4:
  8616. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8617. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8618. else
  8619. val2 = script_getnum(st,4);
  8620. val3 = script_getnum(st,5);
  8621. val4 = script_getnum(st,6);
  8622. pc_bonus4(sd, type, val1, val2, val3, val4);
  8623. break;
  8624. case 5:
  8625. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8626. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8627. else
  8628. val2 = script_getnum(st,4);
  8629. val3 = script_getnum(st,5);
  8630. val4 = script_getnum(st,6);
  8631. val5 = script_getnum(st,7);
  8632. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8633. break;
  8634. default:
  8635. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8636. break;
  8637. }
  8638. return SCRIPT_CMD_SUCCESS;
  8639. }
  8640. BUILDIN_FUNC(autobonus)
  8641. {
  8642. unsigned int dur, pos;
  8643. short rate;
  8644. uint16 atk_type = 0;
  8645. TBL_PC* sd;
  8646. const char *bonus_script, *other_script = NULL;
  8647. if( !script_rid2sd(sd) )
  8648. return SCRIPT_CMD_SUCCESS; // no player attached
  8649. if (current_equip_combo_pos)
  8650. pos = current_equip_combo_pos;
  8651. else
  8652. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8653. rate = script_getnum(st,3);
  8654. dur = script_getnum(st,4);
  8655. bonus_script = script_getstr(st,2);
  8656. if( !rate || !dur || !pos || !bonus_script )
  8657. return SCRIPT_CMD_SUCCESS;
  8658. if( script_hasdata(st,5) )
  8659. atk_type = script_getnum(st,5);
  8660. if( script_hasdata(st,6) )
  8661. other_script = script_getstr(st,6);
  8662. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8663. {
  8664. script_add_autobonus(bonus_script);
  8665. if( other_script )
  8666. script_add_autobonus(other_script);
  8667. }
  8668. return SCRIPT_CMD_SUCCESS;
  8669. }
  8670. BUILDIN_FUNC(autobonus2)
  8671. {
  8672. unsigned int dur, pos;
  8673. short rate;
  8674. uint16 atk_type = 0;
  8675. TBL_PC* sd;
  8676. const char *bonus_script, *other_script = NULL;
  8677. if( !script_rid2sd(sd) )
  8678. return SCRIPT_CMD_SUCCESS; // no player attached
  8679. if (current_equip_combo_pos)
  8680. pos = current_equip_combo_pos;
  8681. else
  8682. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8683. rate = script_getnum(st,3);
  8684. dur = script_getnum(st,4);
  8685. bonus_script = script_getstr(st,2);
  8686. if( !rate || !dur || !pos || !bonus_script )
  8687. return SCRIPT_CMD_SUCCESS;
  8688. if( script_hasdata(st,5) )
  8689. atk_type = script_getnum(st,5);
  8690. if( script_hasdata(st,6) )
  8691. other_script = script_getstr(st,6);
  8692. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8693. {
  8694. script_add_autobonus(bonus_script);
  8695. if( other_script )
  8696. script_add_autobonus(other_script);
  8697. }
  8698. return SCRIPT_CMD_SUCCESS;
  8699. }
  8700. BUILDIN_FUNC(autobonus3)
  8701. {
  8702. unsigned int dur, pos;
  8703. short rate;
  8704. uint16 skill_id = 0;
  8705. TBL_PC* sd;
  8706. const char *bonus_script, *other_script = NULL;
  8707. if( !script_rid2sd(sd) )
  8708. return SCRIPT_CMD_SUCCESS; // no player attached
  8709. if (current_equip_combo_pos)
  8710. pos = current_equip_combo_pos;
  8711. else
  8712. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8713. rate = script_getnum(st,3);
  8714. dur = script_getnum(st,4);
  8715. if (script_isstring(st, 5)) {
  8716. const char *name = script_getstr(st, 5);
  8717. if (!(skill_id = skill_name2id(name))) {
  8718. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8719. return SCRIPT_CMD_FAILURE;
  8720. }
  8721. } else {
  8722. skill_id = script_getnum(st, 5);
  8723. if (!skill_get_index(skill_id)) {
  8724. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8725. return SCRIPT_CMD_FAILURE;
  8726. }
  8727. }
  8728. bonus_script = script_getstr(st,2);
  8729. if( !rate || !dur || !pos || !bonus_script )
  8730. return SCRIPT_CMD_SUCCESS;
  8731. if( script_hasdata(st,6) )
  8732. other_script = script_getstr(st,6);
  8733. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8734. {
  8735. script_add_autobonus(bonus_script);
  8736. if( other_script )
  8737. script_add_autobonus(other_script);
  8738. }
  8739. return SCRIPT_CMD_SUCCESS;
  8740. }
  8741. BUILDIN_FUNC(petautobonus) {
  8742. map_session_data *sd;
  8743. if (!script_rid2sd(sd))
  8744. return SCRIPT_CMD_FAILURE; // No player attached
  8745. const char *command = script_getfuncname(st);
  8746. if (sd->pd == nullptr) {
  8747. ShowError("buildin_%s: Requires an active pet.\n", command);
  8748. return SCRIPT_CMD_FAILURE; // No pet attached to player
  8749. }
  8750. std::string bonus_script = script_getstr(st, 2);
  8751. int16 rate = script_getnum(st, 3);
  8752. uint32 dur = script_getnum(st, 4);
  8753. if (!rate || !dur || bonus_script.empty())
  8754. return SCRIPT_CMD_FAILURE;
  8755. uint16 atk_type = 0;
  8756. std::string other_script = {};
  8757. bool bonus2 = false;
  8758. if (strcmpi(command, "petautobonus2") == 0)
  8759. bonus2 = true;
  8760. if (script_hasdata(st, 5))
  8761. atk_type = script_getnum(st, 5);
  8762. if (script_hasdata(st, 6))
  8763. other_script = script_getstr(st, 6);
  8764. if (pet_addautobonus(bonus2 ? sd->pd->autobonus2 : sd->pd->autobonus, bonus_script, rate, dur, atk_type, other_script, false)) {
  8765. script_add_petautobonus(bonus_script);
  8766. if (!other_script.empty())
  8767. script_add_petautobonus(other_script);
  8768. }
  8769. return SCRIPT_CMD_SUCCESS;
  8770. }
  8771. BUILDIN_FUNC(petautobonus3) {
  8772. map_session_data *sd;
  8773. if (!script_rid2sd(sd))
  8774. return SCRIPT_CMD_SUCCESS; // No player attached
  8775. if (sd->pd == nullptr) {
  8776. ShowError("buildin_petautobonus3: Requires an active pet.\n");
  8777. return SCRIPT_CMD_FAILURE; // No pet attached to player
  8778. }
  8779. std::string bonus_script = script_getstr(st, 2);
  8780. int16 rate = script_getnum(st, 3);
  8781. uint32 dur = script_getnum(st, 4);
  8782. if (!rate || !dur || bonus_script.empty())
  8783. return SCRIPT_CMD_SUCCESS;
  8784. uint16 skill_id = 0;
  8785. std::string other_script = {};
  8786. if (script_isstring(st, 5)) {
  8787. const char *name = script_getstr(st, 5);
  8788. if (!(skill_id = skill_name2id(name))) {
  8789. ShowError("buildin_petautobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8790. return SCRIPT_CMD_FAILURE;
  8791. }
  8792. } else {
  8793. skill_id = script_getnum(st, 5);
  8794. if (!skill_get_index(skill_id)) {
  8795. ShowError("buildin_petautobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8796. return SCRIPT_CMD_FAILURE;
  8797. }
  8798. }
  8799. if (script_hasdata(st, 6))
  8800. other_script = script_getstr(st, 6);
  8801. if (pet_addautobonus(sd->pd->autobonus3, bonus_script, rate, dur, skill_id, other_script, true)) {
  8802. script_add_petautobonus(bonus_script);
  8803. if (!other_script.empty())
  8804. script_add_petautobonus(other_script);
  8805. }
  8806. return SCRIPT_CMD_SUCCESS;
  8807. }
  8808. BUILDIN_FUNC(plagiarizeskill)
  8809. {
  8810. TBL_PC *sd;
  8811. if (script_rid2sd(sd))
  8812. script_pushint(st, pc_skill_plagiarism(*sd, script_getnum(st, 2), script_getnum(st, 3)));
  8813. return SCRIPT_CMD_SUCCESS;
  8814. }
  8815. BUILDIN_FUNC(plagiarizeskillreset)
  8816. {
  8817. TBL_PC *sd;
  8818. if (script_rid2sd(sd))
  8819. script_pushint(st, pc_skill_plagiarism_reset(*sd, script_getnum(st, 2)));
  8820. return SCRIPT_CMD_SUCCESS;
  8821. }
  8822. /// Changes the level of a player skill.
  8823. /// <flag> defaults to 1
  8824. /// <flag>=0 : set the level of the skill
  8825. /// <flag>=1 : set the temporary level of the skill
  8826. /// <flag>=2 : add to the level of the skill
  8827. ///
  8828. /// skill <skill id>,<level>,<flag>
  8829. /// skill <skill id>,<level>
  8830. /// skill "<skill name>",<level>,<flag>
  8831. /// skill "<skill name>",<level>
  8832. BUILDIN_FUNC(skill)
  8833. {
  8834. int id;
  8835. int level;
  8836. int flag = ADDSKILL_TEMP;
  8837. TBL_PC* sd;
  8838. const char* command = script_getfuncname(st);
  8839. if( !script_rid2sd(sd) )
  8840. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8841. if (strcmpi(command, "addtoskill") == 0)
  8842. flag = ADDSKILL_TEMP_ADDLEVEL;
  8843. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8844. level = script_getnum(st,3);
  8845. if( script_hasdata(st,4) )
  8846. flag = script_getnum(st,4);
  8847. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8848. return SCRIPT_CMD_SUCCESS;
  8849. }
  8850. /// Increases the level of a guild skill.
  8851. ///
  8852. /// guildskill <skill id>,<amount>;
  8853. /// guildskill "<skill name>",<amount>;
  8854. BUILDIN_FUNC(guildskill)
  8855. {
  8856. int id;
  8857. int level;
  8858. TBL_PC* sd;
  8859. int i;
  8860. if( !script_rid2sd(sd) )
  8861. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8862. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8863. level = script_getnum(st,3);
  8864. for( i=0; i < level; i++ )
  8865. guild_skillup(sd, id);
  8866. return SCRIPT_CMD_SUCCESS;
  8867. }
  8868. /// Returns the level of the player skill.
  8869. ///
  8870. /// getskilllv(<skill id>) -> <level>
  8871. /// getskilllv("<skill name>") -> <level>
  8872. BUILDIN_FUNC(getskilllv)
  8873. {
  8874. int id;
  8875. TBL_PC* sd;
  8876. if( !script_rid2sd(sd) )
  8877. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8878. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8879. script_pushint(st, pc_checkskill(sd,id));
  8880. return SCRIPT_CMD_SUCCESS;
  8881. }
  8882. /// Returns the level of the guild skill.
  8883. ///
  8884. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8885. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8886. BUILDIN_FUNC(getgdskilllv)
  8887. {
  8888. int guild_id;
  8889. uint16 skill_id;
  8890. struct guild* g;
  8891. guild_id = script_getnum(st,2);
  8892. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8893. g = guild_search(guild_id);
  8894. if( g == NULL )
  8895. script_pushint(st, -1);
  8896. else
  8897. script_pushint(st, guild_checkskill(g,skill_id));
  8898. return SCRIPT_CMD_SUCCESS;
  8899. }
  8900. /// Returns the 'basic_skill_check' setting.
  8901. /// This config determines if the server checks the skill level of NV_BASIC
  8902. /// before allowing the basic actions.
  8903. ///
  8904. /// basicskillcheck() -> <bool>
  8905. BUILDIN_FUNC(basicskillcheck)
  8906. {
  8907. script_pushint(st, battle_config.basic_skill_check);
  8908. return SCRIPT_CMD_SUCCESS;
  8909. }
  8910. /// Returns the GM level of the player.
  8911. ///
  8912. /// getgmlevel({<char_id>}) -> <level>
  8913. BUILDIN_FUNC(getgmlevel)
  8914. {
  8915. TBL_PC* sd;
  8916. if (!script_charid2sd(2,sd))
  8917. return SCRIPT_CMD_FAILURE;
  8918. script_pushint(st, pc_get_group_level(sd));
  8919. return SCRIPT_CMD_SUCCESS;
  8920. }
  8921. /// Returns the group ID of the player.
  8922. ///
  8923. /// getgroupid({<char_id>}) -> <int>
  8924. BUILDIN_FUNC(getgroupid)
  8925. {
  8926. TBL_PC* sd;
  8927. if (!script_charid2sd(2,sd))
  8928. return SCRIPT_CMD_FAILURE;
  8929. script_pushint(st, pc_get_group_id(sd));
  8930. return SCRIPT_CMD_SUCCESS;
  8931. }
  8932. /// Terminates the execution of this script instance.
  8933. ///
  8934. /// end
  8935. BUILDIN_FUNC(end)
  8936. {
  8937. TBL_PC* sd;
  8938. sd = map_id2sd(st->rid);
  8939. st->state = END;
  8940. if( st->mes_active )
  8941. st->mes_active = 0;
  8942. if (sd){
  8943. if (sd->state.callshop == 0)
  8944. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8945. else
  8946. sd->state.callshop = 0;
  8947. }
  8948. return SCRIPT_CMD_SUCCESS;
  8949. }
  8950. /// Checks if the player has that effect state (option).
  8951. ///
  8952. /// checkoption(<option>{,<char_id>}) -> <bool>
  8953. BUILDIN_FUNC(checkoption)
  8954. {
  8955. int option;
  8956. TBL_PC* sd;
  8957. if (!script_charid2sd(3,sd))
  8958. return SCRIPT_CMD_FAILURE;
  8959. option = script_getnum(st,2);
  8960. if( sd->sc.option&option )
  8961. script_pushint(st, 1);
  8962. else
  8963. script_pushint(st, 0);
  8964. return SCRIPT_CMD_SUCCESS;
  8965. }
  8966. /// Checks if the player is in that body state (opt1).
  8967. ///
  8968. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8969. BUILDIN_FUNC(checkoption1)
  8970. {
  8971. int opt1;
  8972. TBL_PC* sd;
  8973. if (!script_charid2sd(3,sd))
  8974. return SCRIPT_CMD_FAILURE;
  8975. opt1 = script_getnum(st,2);
  8976. if( sd->sc.opt1 == opt1 )
  8977. script_pushint(st, 1);
  8978. else
  8979. script_pushint(st, 0);
  8980. return SCRIPT_CMD_SUCCESS;
  8981. }
  8982. /// Checks if the player has that health state (opt2).
  8983. ///
  8984. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8985. BUILDIN_FUNC(checkoption2)
  8986. {
  8987. int opt2;
  8988. TBL_PC* sd;
  8989. if (!script_charid2sd(3,sd))
  8990. return SCRIPT_CMD_FAILURE;
  8991. opt2 = script_getnum(st,2);
  8992. if( sd->sc.opt2&opt2 )
  8993. script_pushint(st, 1);
  8994. else
  8995. script_pushint(st, 0);
  8996. return SCRIPT_CMD_SUCCESS;
  8997. }
  8998. /// Changes the effect state (option) of the player.
  8999. /// <flag> defaults to 1
  9000. /// <flag>=0 : removes the option
  9001. /// <flag>=other : adds the option
  9002. ///
  9003. /// setoption <option>{,<flag>{,<char_id>}};
  9004. BUILDIN_FUNC(setoption)
  9005. {
  9006. int option;
  9007. int flag = 1;
  9008. TBL_PC* sd;
  9009. if (!script_charid2sd(4,sd))
  9010. return SCRIPT_CMD_FAILURE;
  9011. option = script_getnum(st,2);
  9012. if( script_hasdata(st,3) )
  9013. flag = script_getnum(st,3);
  9014. else if( !option ){// Request to remove everything.
  9015. flag = 0;
  9016. option = OPTION_FALCON|OPTION_RIDING;
  9017. #ifndef NEW_CARTS
  9018. option |= OPTION_CART;
  9019. #endif
  9020. }
  9021. if( flag ){// Add option
  9022. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  9023. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  9024. pc_setoption(sd, sd->sc.option|option);
  9025. } else// Remove option
  9026. pc_setoption(sd, sd->sc.option&~option);
  9027. return SCRIPT_CMD_SUCCESS;
  9028. }
  9029. /// Returns if the player has a cart.
  9030. ///
  9031. /// checkcart({char_id}) -> <bool>
  9032. ///
  9033. /// @author Valaris
  9034. BUILDIN_FUNC(checkcart)
  9035. {
  9036. TBL_PC* sd;
  9037. if (!script_charid2sd(2,sd))
  9038. return SCRIPT_CMD_FAILURE;
  9039. if( pc_iscarton(sd) )
  9040. script_pushint(st, 1);
  9041. else
  9042. script_pushint(st, 0);
  9043. return SCRIPT_CMD_SUCCESS;
  9044. }
  9045. /// Sets the cart of the player.
  9046. /// <type> defaults to 1
  9047. /// <type>=0 : removes the cart
  9048. /// <type>=1 : Normal cart
  9049. /// <type>=2 : Wooden cart
  9050. /// <type>=3 : Covered cart with flowers and ferns
  9051. /// <type>=4 : Wooden cart with a Panda doll on the back
  9052. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  9053. ///
  9054. /// setcart {<type>{,<char_id>}};
  9055. BUILDIN_FUNC(setcart)
  9056. {
  9057. int type = 1;
  9058. TBL_PC* sd;
  9059. if (!script_charid2sd(3,sd))
  9060. return SCRIPT_CMD_FAILURE;
  9061. if( script_hasdata(st,2) )
  9062. type = script_getnum(st,2);
  9063. pc_setcart(sd, type);
  9064. return SCRIPT_CMD_SUCCESS;
  9065. }
  9066. /// Returns if the player has a falcon.
  9067. ///
  9068. /// checkfalcon({char_id}) -> <bool>
  9069. ///
  9070. /// @author Valaris
  9071. BUILDIN_FUNC(checkfalcon)
  9072. {
  9073. TBL_PC* sd;
  9074. if (!script_charid2sd(2,sd))
  9075. return SCRIPT_CMD_FAILURE;
  9076. if( pc_isfalcon(sd) )
  9077. script_pushint(st, 1);
  9078. else
  9079. script_pushint(st, 0);
  9080. return SCRIPT_CMD_SUCCESS;
  9081. }
  9082. /// Sets if the player has a falcon or not.
  9083. /// <flag> defaults to 1
  9084. ///
  9085. /// setfalcon {<flag>{,<char_id>}};
  9086. BUILDIN_FUNC(setfalcon)
  9087. {
  9088. int flag = 1;
  9089. TBL_PC* sd;
  9090. if (!script_charid2sd(3,sd))
  9091. return SCRIPT_CMD_FAILURE;
  9092. if( script_hasdata(st,2) )
  9093. flag = script_getnum(st,2);
  9094. pc_setfalcon(sd, flag);
  9095. return SCRIPT_CMD_SUCCESS;
  9096. }
  9097. /// Returns if the player is riding.
  9098. ///
  9099. /// checkriding({char_id}) -> <bool>
  9100. ///
  9101. /// @author Valaris
  9102. BUILDIN_FUNC(checkriding)
  9103. {
  9104. TBL_PC* sd;
  9105. if (!script_charid2sd(2,sd))
  9106. return SCRIPT_CMD_FAILURE;
  9107. if( pc_isriding(sd) )
  9108. script_pushint(st, 1);
  9109. else
  9110. script_pushint(st, 0);
  9111. return SCRIPT_CMD_SUCCESS;
  9112. }
  9113. /// Sets if the player is riding.
  9114. /// <flag> defaults to 1
  9115. ///
  9116. /// setriding {<flag>{,<char_id>}};
  9117. BUILDIN_FUNC(setriding)
  9118. {
  9119. int flag = 1;
  9120. TBL_PC* sd;
  9121. if (!script_charid2sd(3,sd))
  9122. return SCRIPT_CMD_FAILURE;
  9123. if( script_hasdata(st,2) )
  9124. flag = script_getnum(st,2);
  9125. pc_setriding(sd, flag);
  9126. return SCRIPT_CMD_SUCCESS;
  9127. }
  9128. /// Returns if the player has a warg.
  9129. ///
  9130. /// checkwug({char_id}) -> <bool>
  9131. ///
  9132. BUILDIN_FUNC(checkwug)
  9133. {
  9134. TBL_PC* sd;
  9135. if (!script_charid2sd(2,sd))
  9136. return SCRIPT_CMD_FAILURE;
  9137. if( pc_iswug(sd) || pc_isridingwug(sd) )
  9138. script_pushint(st, 1);
  9139. else
  9140. script_pushint(st, 0);
  9141. return SCRIPT_CMD_SUCCESS;
  9142. }
  9143. /// Returns if the player is wearing MADO Gear.
  9144. ///
  9145. /// checkmadogear({<char_id>}) -> <bool>
  9146. ///
  9147. BUILDIN_FUNC(checkmadogear)
  9148. {
  9149. TBL_PC* sd;
  9150. if (!script_charid2sd(2,sd))
  9151. return SCRIPT_CMD_FAILURE;
  9152. if( pc_ismadogear(sd) )
  9153. script_pushint(st, 1);
  9154. else
  9155. script_pushint(st, 0);
  9156. return SCRIPT_CMD_SUCCESS;
  9157. }
  9158. /// Sets if the player is riding MADO Gear.
  9159. /// <flag> defaults to true
  9160. /// <type> defaults to MADO_ROBOT
  9161. ///
  9162. /// setmadogear {<flag>{,type{,<char_id>}}};
  9163. BUILDIN_FUNC(setmadogear)
  9164. {
  9165. bool flag = true;
  9166. TBL_PC* sd;
  9167. uint16 type = MADO_ROBOT;
  9168. if (!script_charid2sd(4,sd))
  9169. return SCRIPT_CMD_FAILURE;
  9170. if( script_hasdata(st,2) )
  9171. flag = script_getnum(st,2) != 0;
  9172. if (script_hasdata(st, 3)) {
  9173. type = script_getnum(st, 3);
  9174. if (type == MADO_UNUSED || type >= MADO_MAX) {
  9175. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  9176. type = MADO_ROBOT;
  9177. }
  9178. }
  9179. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  9180. return SCRIPT_CMD_SUCCESS;
  9181. }
  9182. /// Sets the save point of the player.
  9183. ///
  9184. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  9185. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  9186. BUILDIN_FUNC(savepoint)
  9187. {
  9188. int x, y, m, cid_pos = 5;
  9189. unsigned short map_idx;
  9190. const char* str;
  9191. TBL_PC* sd;
  9192. if (script_lastdata(st) > 5)
  9193. cid_pos = 7;
  9194. if (!script_charid2sd(cid_pos,sd))
  9195. return SCRIPT_CMD_FAILURE;// no player attached, report source
  9196. str = script_getstr(st, 2);
  9197. map_idx = mapindex_name2id(str);
  9198. if( !map_idx )
  9199. return SCRIPT_CMD_FAILURE;
  9200. x = script_getnum(st,3);
  9201. y = script_getnum(st,4);
  9202. m = map_mapindex2mapid(map_idx);
  9203. if (cid_pos == 7) {
  9204. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  9205. x1 = x + dx, y1 = y + dy,
  9206. x0 = x - dx, y0 = y - dy;
  9207. uint8 n = 10;
  9208. do {
  9209. x = x0 + rnd()%(x1-x0+1);
  9210. y = y0 + rnd()%(y1-y0+1);
  9211. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  9212. }
  9213. // Check for valid coordinates if map in local map-server
  9214. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  9215. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  9216. return SCRIPT_CMD_FAILURE;
  9217. }
  9218. pc_setsavepoint(sd, map_idx, x, y);
  9219. return SCRIPT_CMD_SUCCESS;
  9220. }
  9221. /*==========================================
  9222. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  9223. *------------------------------------------*/
  9224. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  9225. {
  9226. int type;
  9227. time_t timer;
  9228. struct tm *t;
  9229. type=script_getnum(st,2);
  9230. switch(type){
  9231. case 2:
  9232. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  9233. // from the system clock.)
  9234. script_pushint(st,(int)time(NULL));
  9235. break;
  9236. case 1:
  9237. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  9238. time(&timer);
  9239. t=localtime(&timer);
  9240. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  9241. break;
  9242. case 0:
  9243. default:
  9244. //type 0:(System Ticks)
  9245. script_pushint(st,gettick());
  9246. break;
  9247. }
  9248. return SCRIPT_CMD_SUCCESS;
  9249. }
  9250. /*==========================================
  9251. * GetTime(Type)
  9252. *
  9253. * Returns the current value of a certain date type.
  9254. * Possible types are:
  9255. * - DT_SECOND Current seconds
  9256. * - DT_MINUTE Current minute
  9257. * - DT_HOUR Current hour
  9258. * - DT_DAYOFWEEK Day of current week
  9259. * - DT_DAYOFMONTH Day of current month
  9260. * - DT_MONTH Current month
  9261. * - DT_YEAR Current year
  9262. * - DT_DAYOFYEAR Day of current year
  9263. *
  9264. * If none of the above types is supplied -1 will be returned to the script
  9265. * and the script source will be reported into the mapserver console.
  9266. *------------------------------------------*/
  9267. BUILDIN_FUNC(gettime)
  9268. {
  9269. int type;
  9270. type = script_getnum(st,2);
  9271. if( type <= DT_MIN || type >= DT_MAX ){
  9272. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  9273. script_reportsrc(st);
  9274. script_pushint(st,-1);
  9275. }else{
  9276. script_pushint(st,date_get((enum e_date_type)type));
  9277. }
  9278. return SCRIPT_CMD_SUCCESS;
  9279. }
  9280. /**
  9281. * Returns the current server time or the provided time in a readable format.
  9282. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  9283. */
  9284. BUILDIN_FUNC(gettimestr)
  9285. {
  9286. char *tmpstr;
  9287. const char *fmtstr;
  9288. int maxlen;
  9289. time_t now;
  9290. fmtstr = script_getstr(st,2);
  9291. maxlen = script_getnum(st,3);
  9292. if (script_hasdata(st, 4)) {
  9293. if (script_getnum(st, 4) < 0) {
  9294. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  9295. return SCRIPT_CMD_FAILURE;
  9296. } else
  9297. now = (time_t)script_getnum(st, 4);
  9298. } else
  9299. now = time(NULL);
  9300. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  9301. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  9302. tmpstr[maxlen] = '\0';
  9303. script_pushstr(st,tmpstr);
  9304. return SCRIPT_CMD_SUCCESS;
  9305. }
  9306. /*==========================================
  9307. * Open player storage
  9308. *------------------------------------------*/
  9309. BUILDIN_FUNC(openstorage)
  9310. {
  9311. TBL_PC* sd;
  9312. if( !script_rid2sd(sd) )
  9313. return SCRIPT_CMD_SUCCESS;
  9314. storage_storageopen(sd);
  9315. return SCRIPT_CMD_SUCCESS;
  9316. }
  9317. BUILDIN_FUNC(guildopenstorage)
  9318. {
  9319. TBL_PC* sd;
  9320. int ret;
  9321. if( !script_rid2sd(sd) )
  9322. return SCRIPT_CMD_SUCCESS;
  9323. ret = storage_guild_storageopen(sd);
  9324. script_pushint(st,ret);
  9325. return SCRIPT_CMD_SUCCESS;
  9326. }
  9327. BUILDIN_FUNC(guildopenstorage_log){
  9328. #if PACKETVER < 20140205
  9329. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  9330. return SCRIPT_CMD_FAILURE;
  9331. #else
  9332. struct map_session_data* sd;
  9333. if( !script_charid2sd( 2, sd ) ){
  9334. return SCRIPT_CMD_FAILURE;
  9335. }
  9336. script_pushint( st, storage_guild_log_read( sd ) );
  9337. return SCRIPT_CMD_SUCCESS;
  9338. #endif
  9339. }
  9340. BUILDIN_FUNC(guild_has_permission){
  9341. struct map_session_data* sd;
  9342. if( !script_charid2sd( 3, sd ) ){
  9343. return SCRIPT_CMD_FAILURE;
  9344. }
  9345. int permission = script_getnum(st,2);
  9346. if( permission == 0 ){
  9347. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  9348. return SCRIPT_CMD_FAILURE;
  9349. }
  9350. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  9351. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  9352. return SCRIPT_CMD_FAILURE;
  9353. }
  9354. if( !sd->guild ){
  9355. script_pushint( st, false );
  9356. return SCRIPT_CMD_SUCCESS;
  9357. }
  9358. int position = guild_getposition(sd);
  9359. if( position < 0 || ( sd->guild->position[position].mode&permission ) != permission ){
  9360. script_pushint( st, false );
  9361. return SCRIPT_CMD_SUCCESS;
  9362. }
  9363. script_pushint( st, true );
  9364. return SCRIPT_CMD_SUCCESS;
  9365. }
  9366. /*==========================================
  9367. * Make player use a skill trought item usage
  9368. *------------------------------------------*/
  9369. /// itemskill <skill id>,<level>
  9370. /// itemskill "<skill name>",<level>
  9371. BUILDIN_FUNC(itemskill)
  9372. {
  9373. int id;
  9374. int lv;
  9375. bool keep_requirement;
  9376. TBL_PC* sd;
  9377. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9378. return SCRIPT_CMD_SUCCESS;
  9379. if (script_isstring(st, 2)) {
  9380. const char *name = script_getstr(st, 2);
  9381. if (!(id = skill_name2id(name))) {
  9382. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9383. return SCRIPT_CMD_FAILURE;
  9384. }
  9385. } else {
  9386. id = script_getnum(st, 2);
  9387. if (!skill_get_index(id)) {
  9388. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9389. return SCRIPT_CMD_FAILURE;
  9390. }
  9391. }
  9392. lv = script_getnum(st,3);
  9393. if (script_hasdata(st, 4)) {
  9394. keep_requirement = (script_getnum(st, 4) != 0);
  9395. } else {
  9396. keep_requirement = false;
  9397. }
  9398. sd->skillitem=id;
  9399. sd->skillitemlv=lv;
  9400. sd->skillitem_keep_requirement = keep_requirement;
  9401. clif_item_skill(sd,id,lv);
  9402. return SCRIPT_CMD_SUCCESS;
  9403. }
  9404. /*==========================================
  9405. * Attempt to create an item
  9406. *------------------------------------------*/
  9407. BUILDIN_FUNC(produce)
  9408. {
  9409. int trigger;
  9410. TBL_PC* sd;
  9411. if( !script_rid2sd(sd) )
  9412. return SCRIPT_CMD_SUCCESS;
  9413. trigger=script_getnum(st,2);
  9414. clif_skill_produce_mix_list(sd, -1, trigger);
  9415. return SCRIPT_CMD_SUCCESS;
  9416. }
  9417. /*==========================================
  9418. *
  9419. *------------------------------------------*/
  9420. BUILDIN_FUNC(cooking)
  9421. {
  9422. int trigger;
  9423. TBL_PC* sd;
  9424. if( !script_rid2sd(sd) )
  9425. return SCRIPT_CMD_SUCCESS;
  9426. trigger=script_getnum(st,2);
  9427. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  9428. return SCRIPT_CMD_SUCCESS;
  9429. }
  9430. /*==========================================
  9431. * Create a pet
  9432. *------------------------------------------*/
  9433. BUILDIN_FUNC(makepet)
  9434. {
  9435. struct map_session_data* sd;
  9436. uint16 mob_id;
  9437. if( !script_rid2sd(sd) )
  9438. return SCRIPT_CMD_FAILURE;
  9439. mob_id = script_getnum(st,2);
  9440. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9441. if( !pet ){
  9442. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9443. return SCRIPT_CMD_FAILURE;
  9444. }
  9445. sd->catch_target_class = mob_id;
  9446. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9447. 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() );
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. /**
  9451. * Give player exp base,job * quest_exp_rate/100
  9452. * getexp <base xp>,<job xp>{,<char_id>};
  9453. **/
  9454. BUILDIN_FUNC(getexp){
  9455. struct map_session_data* sd;
  9456. if( !script_charid2sd( 4, sd ) ){
  9457. return SCRIPT_CMD_FAILURE;
  9458. }
  9459. int64 base = script_getnum64( st, 2 );
  9460. if( base < 0 ){
  9461. ShowError( "buildin_getexp: Called with negative base exp.\n" );
  9462. return SCRIPT_CMD_FAILURE;
  9463. }
  9464. int64 job = script_getnum64( st, 3 );
  9465. if( job < 0 ){
  9466. ShowError( "buildin_getexp: Called with negative job exp.\n" );
  9467. return SCRIPT_CMD_FAILURE;
  9468. }
  9469. if( base == 0 && job == 0 ){
  9470. ShowError( "buildin_getexp: Called with base and job exp 0.\n" );
  9471. return SCRIPT_CMD_FAILURE;
  9472. }
  9473. // bonus for npc-given exp
  9474. double bonus = battle_config.quest_exp_rate / 100.;
  9475. if (base)
  9476. base = (int64) cap_value(base * bonus, 0, MAX_EXP);
  9477. if (job)
  9478. job = (int64) cap_value(job * bonus, 0, MAX_EXP);
  9479. pc_gainexp(sd, NULL, base, job, 1);
  9480. #ifdef RENEWAL
  9481. if (base && sd->hd)
  9482. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9483. #endif
  9484. return SCRIPT_CMD_SUCCESS;
  9485. }
  9486. /*==========================================
  9487. * Gain guild exp [Celest]
  9488. *------------------------------------------*/
  9489. BUILDIN_FUNC(guildgetexp){
  9490. struct map_session_data* sd;
  9491. if( !script_rid2sd( sd ) ){
  9492. return SCRIPT_CMD_FAILURE;
  9493. }
  9494. int64 exp = script_getnum64( st, 2 );
  9495. if( exp <= 0 ){
  9496. ShowError( "buildin_guildgetexp: Called with exp <= 0.\n" );
  9497. return SCRIPT_CMD_FAILURE;
  9498. }
  9499. if( sd->status.guild_id <= 0 ){
  9500. 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 );
  9501. return SCRIPT_CMD_FAILURE;
  9502. }
  9503. guild_getexp( sd, exp );
  9504. return SCRIPT_CMD_SUCCESS;
  9505. }
  9506. /*==========================================
  9507. * Changes the guild master of a guild [Skotlex]
  9508. *------------------------------------------*/
  9509. BUILDIN_FUNC(guildchangegm)
  9510. {
  9511. TBL_PC *sd;
  9512. int guild_id;
  9513. const char *name;
  9514. guild_id = script_getnum(st,2);
  9515. name = script_getstr(st,3);
  9516. sd=map_nick2sd(name,false);
  9517. if (!sd)
  9518. script_pushint(st,0);
  9519. else
  9520. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9521. return SCRIPT_CMD_SUCCESS;
  9522. }
  9523. /*==========================================
  9524. * Spawn a monster:
  9525. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9526. * *monster "<map name>",<x>,<y>,"<name to show>","<mob name>",<amount>{,"<event label>",<size>,<ai>};
  9527. *------------------------------------------*/
  9528. BUILDIN_FUNC(monster)
  9529. {
  9530. const char* mapn = script_getstr(st,2);
  9531. int x = script_getnum(st,3);
  9532. int y = script_getnum(st,4);
  9533. const char* str = script_getstr(st,5);
  9534. int class_;
  9535. int amount = script_getnum(st,7);
  9536. const char* event = "";
  9537. unsigned int size = SZ_SMALL;
  9538. enum mob_ai ai = AI_NONE;
  9539. struct map_session_data* sd;
  9540. int16 m;
  9541. int i;
  9542. if( script_isstring( st, 6 ) ){
  9543. const char* name = script_getstr( st, 6 );
  9544. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  9545. if( mob == nullptr ){
  9546. ShowWarning( "buildin_monster: Attempted to spawn non-existing monster \"%s\"\n", name );
  9547. return SCRIPT_CMD_FAILURE;
  9548. }
  9549. class_ = mob->id;
  9550. }else{
  9551. class_ = script_getnum( st, 6 );
  9552. if( class_ >= 0 && !mobdb_checkid( class_ ) ){
  9553. ShowWarning( "buildin_monster: Attempted to spawn non-existing monster class %d\n", class_ );
  9554. return SCRIPT_CMD_FAILURE;
  9555. }
  9556. }
  9557. if (script_hasdata(st, 8)) {
  9558. event = script_getstr(st, 8);
  9559. check_event(st, event);
  9560. }
  9561. if (script_hasdata(st, 9)) {
  9562. size = script_getnum(st, 9);
  9563. if (size > SZ_BIG) {
  9564. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9565. return SCRIPT_CMD_FAILURE;
  9566. }
  9567. }
  9568. if (script_hasdata(st, 10)) {
  9569. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9570. if (ai >= AI_MAX) {
  9571. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9572. return SCRIPT_CMD_FAILURE;
  9573. }
  9574. }
  9575. sd = map_id2sd(st->rid);
  9576. if (sd && strcmp(mapn, "this") == 0)
  9577. m = sd->bl.m;
  9578. else
  9579. m = map_mapname2mapid(mapn);
  9580. for(i = 0; i < amount; i++) { //not optimised
  9581. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9582. if (mobid)
  9583. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9584. }
  9585. return SCRIPT_CMD_SUCCESS;
  9586. }
  9587. /*==========================================
  9588. * Request List of Monster Drops
  9589. *------------------------------------------*/
  9590. BUILDIN_FUNC(getmobdrops)
  9591. {
  9592. int class_ = script_getnum(st,2);
  9593. int i, j = 0;
  9594. if( !mobdb_checkid(class_) )
  9595. {
  9596. script_pushint(st, 0);
  9597. return SCRIPT_CMD_SUCCESS;
  9598. }
  9599. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9600. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9601. {
  9602. if( mob->dropitem[i].nameid == 0 )
  9603. continue;
  9604. if( !item_db.exists(mob->dropitem[i].nameid) )
  9605. continue;
  9606. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9607. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9608. mapreg_setreg(reference_uid(add_str("$@MobDrop_nosteal"), j), mob->dropitem[i].steal_protected);
  9609. mapreg_setreg(reference_uid(add_str("$@MobDrop_randomopt"), j), mob->dropitem[i].randomopt_group);
  9610. j++;
  9611. }
  9612. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9613. script_pushint(st, 1);
  9614. return SCRIPT_CMD_SUCCESS;
  9615. }
  9616. /*==========================================
  9617. * Spawn a monster in a random location
  9618. * in x0,x1,y0,y1 area.
  9619. *------------------------------------------*/
  9620. BUILDIN_FUNC(areamonster)
  9621. {
  9622. const char* mapn = script_getstr(st,2);
  9623. int x0 = script_getnum(st,3);
  9624. int y0 = script_getnum(st,4);
  9625. int x1 = script_getnum(st,5);
  9626. int y1 = script_getnum(st,6);
  9627. const char* str = script_getstr(st,7);
  9628. int class_;
  9629. int amount = script_getnum(st,9);
  9630. const char* event = "";
  9631. unsigned int size = SZ_SMALL;
  9632. enum mob_ai ai = AI_NONE;
  9633. struct map_session_data* sd;
  9634. int16 m;
  9635. int i;
  9636. if( script_isstring( st, 8 ) ){
  9637. const char* name = script_getstr( st, 8 );
  9638. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  9639. if( mob == nullptr ){
  9640. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing monster \"%s\"\n", name );
  9641. return SCRIPT_CMD_FAILURE;
  9642. }
  9643. class_ = mob->id;
  9644. }else{
  9645. class_ = script_getnum( st, 8 );
  9646. if( class_ >= 0 && !mobdb_checkid( class_ ) ){
  9647. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing monster class %d\n", class_ );
  9648. return SCRIPT_CMD_FAILURE;
  9649. }
  9650. }
  9651. if (script_hasdata(st,10)) {
  9652. event = script_getstr(st, 10);
  9653. check_event(st, event);
  9654. }
  9655. if (script_hasdata(st, 11)) {
  9656. size = script_getnum(st, 11);
  9657. if (size > 3) {
  9658. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_ );
  9659. return SCRIPT_CMD_FAILURE;
  9660. }
  9661. }
  9662. if (script_hasdata(st, 12)) {
  9663. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9664. if (ai >= AI_MAX) {
  9665. ShowWarning( "buildin_areamonster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_ );
  9666. return SCRIPT_CMD_FAILURE;
  9667. }
  9668. }
  9669. sd = map_id2sd(st->rid);
  9670. if (sd && strcmp(mapn, "this") == 0)
  9671. m = sd->bl.m;
  9672. else
  9673. m = map_mapname2mapid(mapn);
  9674. for(i = 0; i < amount; i++) { //not optimised
  9675. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9676. if (mobid)
  9677. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9678. }
  9679. return SCRIPT_CMD_SUCCESS;
  9680. }
  9681. /*==========================================
  9682. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9683. *------------------------------------------*/
  9684. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9685. { //same fix but with killmonster instead - stripping events from mobs.
  9686. TBL_MOB* md = (TBL_MOB*)bl;
  9687. char *event=va_arg(ap,char *);
  9688. int allflag=va_arg(ap,int);
  9689. md->state.npc_killmonster = 1;
  9690. if(!allflag){
  9691. if(strcmp(event,md->npc_event)==0)
  9692. status_kill(bl);
  9693. }else{
  9694. if(!md->spawn)
  9695. status_kill(bl);
  9696. }
  9697. md->state.npc_killmonster = 0;
  9698. return SCRIPT_CMD_SUCCESS;
  9699. }
  9700. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9701. {
  9702. TBL_MOB* md = (TBL_MOB*)bl;
  9703. char *event=va_arg(ap,char *);
  9704. int allflag=va_arg(ap,int);
  9705. if(!allflag){
  9706. if(strcmp(event,md->npc_event)==0)
  9707. status_kill(bl);
  9708. }else{
  9709. if(!md->spawn)
  9710. status_kill(bl);
  9711. }
  9712. return SCRIPT_CMD_SUCCESS;
  9713. }
  9714. BUILDIN_FUNC(killmonster)
  9715. {
  9716. const char *mapname,*event;
  9717. int16 m,allflag=0;
  9718. mapname=script_getstr(st,2);
  9719. event=script_getstr(st,3);
  9720. if(strcmp(event,"All")==0)
  9721. allflag = 1;
  9722. else
  9723. check_event(st, event);
  9724. if( (m=map_mapname2mapid(mapname))<0 )
  9725. return SCRIPT_CMD_SUCCESS;
  9726. if( script_hasdata(st,4) ) {
  9727. if ( script_getnum(st,4) == 1 ) {
  9728. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9729. return SCRIPT_CMD_SUCCESS;
  9730. }
  9731. }
  9732. map_freeblock_lock();
  9733. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9734. map_freeblock_unlock();
  9735. return SCRIPT_CMD_SUCCESS;
  9736. }
  9737. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9738. { //Strips the event from the mob if it's killed the old method.
  9739. struct mob_data *md;
  9740. md = BL_CAST(BL_MOB, bl);
  9741. if (md->npc_event[0])
  9742. md->npc_event[0] = 0;
  9743. status_kill(bl);
  9744. return 0;
  9745. }
  9746. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9747. {
  9748. status_kill(bl);
  9749. return 0;
  9750. }
  9751. BUILDIN_FUNC(killmonsterall)
  9752. {
  9753. const char *mapname;
  9754. int16 m;
  9755. mapname=script_getstr(st,2);
  9756. if( (m = map_mapname2mapid(mapname))<0 )
  9757. return SCRIPT_CMD_SUCCESS;
  9758. if( script_hasdata(st,3) ) {
  9759. if ( script_getnum(st,3) == 1 ) {
  9760. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9761. return SCRIPT_CMD_SUCCESS;
  9762. }
  9763. }
  9764. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9765. return SCRIPT_CMD_SUCCESS;
  9766. }
  9767. /*==========================================
  9768. * Creates a clone of a player.
  9769. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9770. *------------------------------------------*/
  9771. BUILDIN_FUNC(clone)
  9772. {
  9773. TBL_PC *sd, *msd=NULL;
  9774. uint32 char_id, master_id = 0, x, y, flag = 0;
  9775. int16 m;
  9776. enum e_mode mode = MD_NONE;
  9777. unsigned int duration = 0;
  9778. const char *mapname,*event;
  9779. mapname=script_getstr(st,2);
  9780. x=script_getnum(st,3);
  9781. y=script_getnum(st,4);
  9782. event=script_getstr(st,5);
  9783. char_id=script_getnum(st,6);
  9784. if( script_hasdata(st,7) )
  9785. master_id=script_getnum(st,7);
  9786. if( script_hasdata(st,8) )
  9787. mode=static_cast<e_mode>(script_getnum(st,8));
  9788. if( script_hasdata(st,9) )
  9789. flag=script_getnum(st,9);
  9790. if( script_hasdata(st,10) )
  9791. duration=script_getnum(st,10);
  9792. check_event(st, event);
  9793. m = map_mapname2mapid(mapname);
  9794. if (m < 0)
  9795. return SCRIPT_CMD_SUCCESS;
  9796. sd = map_charid2sd(char_id);
  9797. if (master_id) {
  9798. msd = map_charid2sd(master_id);
  9799. if (msd)
  9800. master_id = msd->bl.id;
  9801. else
  9802. master_id = 0;
  9803. }
  9804. if (sd) //Return ID of newly crafted clone.
  9805. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9806. else //Failed to create clone.
  9807. script_pushint(st,0);
  9808. return SCRIPT_CMD_SUCCESS;
  9809. }
  9810. /*==========================================
  9811. *------------------------------------------*/
  9812. BUILDIN_FUNC(doevent)
  9813. {
  9814. const char* event;
  9815. struct map_session_data* sd;
  9816. if( !script_rid2sd(sd) )
  9817. return SCRIPT_CMD_SUCCESS;
  9818. event = script_getstr(st,2);
  9819. check_event(st, event);
  9820. npc_event(sd, event, 0);
  9821. return SCRIPT_CMD_SUCCESS;
  9822. }
  9823. /*==========================================
  9824. *------------------------------------------*/
  9825. BUILDIN_FUNC(donpcevent)
  9826. {
  9827. const char* event = script_getstr(st,2);
  9828. check_event(st, event);
  9829. if( !npc_event_do(event) ) {
  9830. struct npc_data * nd = map_id2nd(st->oid);
  9831. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9832. script_pushint(st, 0);
  9833. } else
  9834. script_pushint(st, 1);
  9835. return SCRIPT_CMD_SUCCESS;
  9836. }
  9837. /// for Aegis compatibility
  9838. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9839. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9840. {
  9841. const char* npc = script_getstr(st,2);
  9842. const char* command = script_getstr(st,3);
  9843. char event[EVENT_NAME_LENGTH];
  9844. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9845. check_event(st, event);
  9846. if( npc_event_do(event) ){
  9847. script_pushint(st, true);
  9848. }else{
  9849. struct npc_data * nd = map_id2nd(st->oid);
  9850. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9851. script_pushint(st, false);
  9852. }
  9853. return SCRIPT_CMD_SUCCESS;
  9854. }
  9855. /*==========================================
  9856. *------------------------------------------*/
  9857. BUILDIN_FUNC(addtimer)
  9858. {
  9859. int tick;
  9860. const char* event;
  9861. TBL_PC* sd;
  9862. if( !script_rid2sd(sd) )
  9863. return SCRIPT_CMD_SUCCESS;
  9864. tick = script_getnum(st,2);
  9865. event = script_getstr(st, 3);
  9866. check_event(st, event);
  9867. if (!pc_addeventtimer(sd,tick,event)) {
  9868. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9869. return SCRIPT_CMD_FAILURE;
  9870. }
  9871. return SCRIPT_CMD_SUCCESS;
  9872. }
  9873. /*==========================================
  9874. *------------------------------------------*/
  9875. BUILDIN_FUNC(deltimer)
  9876. {
  9877. const char *event;
  9878. TBL_PC* sd;
  9879. if( !script_rid2sd(sd) )
  9880. return SCRIPT_CMD_SUCCESS;
  9881. event=script_getstr(st, 2);
  9882. check_event(st, event);
  9883. pc_deleventtimer(sd,event);
  9884. return SCRIPT_CMD_SUCCESS;
  9885. }
  9886. /*==========================================
  9887. *------------------------------------------*/
  9888. BUILDIN_FUNC(addtimercount)
  9889. {
  9890. const char *event;
  9891. int tick;
  9892. TBL_PC* sd;
  9893. if( !script_rid2sd(sd) )
  9894. return SCRIPT_CMD_SUCCESS;
  9895. tick=script_getnum(st,2);
  9896. event=script_getstr(st,3);
  9897. check_event(st, event);
  9898. pc_addeventtimercount(sd,event,tick);
  9899. return SCRIPT_CMD_SUCCESS;
  9900. }
  9901. /*==========================================
  9902. *------------------------------------------*/
  9903. BUILDIN_FUNC(initnpctimer)
  9904. {
  9905. struct npc_data *nd;
  9906. int flag = 0;
  9907. if( script_hasdata(st,3) )
  9908. { //Two arguments: NPC name and attach flag.
  9909. nd = npc_name2id(script_getstr(st, 2));
  9910. flag = script_getnum(st,3);
  9911. }
  9912. else if( script_hasdata(st,2) )
  9913. { //Check if argument is numeric (flag) or string (npc name)
  9914. if( script_isstring(st, 2) ) //NPC name
  9915. nd = npc_name2id(script_getstr(st, 2));
  9916. else //Flag
  9917. {
  9918. nd = (struct npc_data *)map_id2bl(st->oid);
  9919. flag = script_getnum(st, 2);
  9920. }
  9921. }
  9922. else
  9923. nd = (struct npc_data *)map_id2bl(st->oid);
  9924. if( !nd )
  9925. return SCRIPT_CMD_SUCCESS;
  9926. if( flag ) //Attach
  9927. {
  9928. TBL_PC* sd;
  9929. if( !script_rid2sd(sd) )
  9930. return SCRIPT_CMD_SUCCESS;
  9931. nd->u.scr.rid = sd->bl.id;
  9932. }
  9933. nd->u.scr.timertick = 0;
  9934. npc_settimerevent_tick(nd,0);
  9935. npc_timerevent_start(nd, st->rid);
  9936. return SCRIPT_CMD_SUCCESS;
  9937. }
  9938. /*==========================================
  9939. *------------------------------------------*/
  9940. BUILDIN_FUNC(startnpctimer)
  9941. {
  9942. struct npc_data *nd;
  9943. int flag = 0;
  9944. if( script_hasdata(st,3) )
  9945. { //Two arguments: NPC name and attach flag.
  9946. nd = npc_name2id(script_getstr(st, 2));
  9947. flag = script_getnum(st,3);
  9948. }
  9949. else if( script_hasdata(st,2) )
  9950. { //Check if argument is numeric (flag) or string (npc name)
  9951. if( script_isstring(st, 2) ) //NPC name
  9952. nd = npc_name2id(script_getstr(st, 2));
  9953. else //Flag
  9954. {
  9955. nd = (struct npc_data *)map_id2bl(st->oid);
  9956. flag = script_getnum(st, 2);
  9957. }
  9958. }
  9959. else
  9960. nd=(struct npc_data *)map_id2bl(st->oid);
  9961. if( !nd )
  9962. return SCRIPT_CMD_SUCCESS;
  9963. if( flag ) //Attach
  9964. {
  9965. TBL_PC* sd;
  9966. if( !script_rid2sd(sd) )
  9967. return SCRIPT_CMD_SUCCESS;
  9968. nd->u.scr.rid = sd->bl.id;
  9969. }
  9970. npc_timerevent_start(nd, st->rid);
  9971. return SCRIPT_CMD_SUCCESS;
  9972. }
  9973. /*==========================================
  9974. *------------------------------------------*/
  9975. BUILDIN_FUNC(stopnpctimer)
  9976. {
  9977. struct npc_data *nd;
  9978. int flag = 0;
  9979. if( script_hasdata(st,3) )
  9980. { //Two arguments: NPC name and attach flag.
  9981. nd = npc_name2id(script_getstr(st, 2));
  9982. flag = script_getnum(st,3);
  9983. }
  9984. else if( script_hasdata(st,2) )
  9985. { //Check if argument is numeric (flag) or string (npc name)
  9986. if( script_isstring(st, 2) ) //NPC name
  9987. nd = npc_name2id(script_getstr(st, 2));
  9988. else //Flag
  9989. {
  9990. nd = (struct npc_data *)map_id2bl(st->oid);
  9991. flag = script_getnum(st, 2);
  9992. }
  9993. }
  9994. else
  9995. nd=(struct npc_data *)map_id2bl(st->oid);
  9996. if( !nd )
  9997. return SCRIPT_CMD_SUCCESS;
  9998. if( flag ) //Detach
  9999. nd->u.scr.rid = 0;
  10000. npc_timerevent_stop(nd);
  10001. return SCRIPT_CMD_SUCCESS;
  10002. }
  10003. /*==========================================
  10004. *------------------------------------------*/
  10005. BUILDIN_FUNC(getnpctimer)
  10006. {
  10007. struct npc_data *nd;
  10008. TBL_PC *sd;
  10009. int type = script_getnum(st,2);
  10010. t_tick val = 0;
  10011. if( script_hasdata(st,3) )
  10012. nd = npc_name2id(script_getstr(st,3));
  10013. else
  10014. nd = (struct npc_data *)map_id2bl(st->oid);
  10015. if( !nd || nd->bl.type != BL_NPC )
  10016. {
  10017. script_pushint(st,0);
  10018. ShowError("getnpctimer: Invalid NPC.\n");
  10019. return SCRIPT_CMD_FAILURE;
  10020. }
  10021. switch( type )
  10022. {
  10023. case 0: val = npc_gettimerevent_tick(nd); break;
  10024. case 1:
  10025. if( nd->u.scr.rid )
  10026. {
  10027. sd = map_id2sd(nd->u.scr.rid);
  10028. if( !sd )
  10029. {
  10030. ShowError("buildin_getnpctimer: Attached player not found!\n");
  10031. break;
  10032. }
  10033. val = (sd->npc_timer_id != INVALID_TIMER);
  10034. }
  10035. else
  10036. val = (nd->u.scr.timerid != INVALID_TIMER);
  10037. break;
  10038. case 2: val = nd->u.scr.timeramount; break;
  10039. }
  10040. script_pushint(st,val);
  10041. return SCRIPT_CMD_SUCCESS;
  10042. }
  10043. /*==========================================
  10044. *------------------------------------------*/
  10045. BUILDIN_FUNC(setnpctimer)
  10046. {
  10047. int tick;
  10048. struct npc_data *nd;
  10049. tick = script_getnum(st,2);
  10050. if( script_hasdata(st,3) )
  10051. nd = npc_name2id(script_getstr(st,3));
  10052. else
  10053. nd = (struct npc_data *)map_id2bl(st->oid);
  10054. if( !nd || nd->bl.type != BL_NPC )
  10055. {
  10056. script_pushint(st,1);
  10057. ShowError("setnpctimer: Invalid NPC.\n");
  10058. return SCRIPT_CMD_FAILURE;
  10059. }
  10060. npc_settimerevent_tick(nd,tick);
  10061. script_pushint(st,0);
  10062. return SCRIPT_CMD_SUCCESS;
  10063. }
  10064. /*==========================================
  10065. * attaches the player rid to the timer [Celest]
  10066. *------------------------------------------*/
  10067. BUILDIN_FUNC(attachnpctimer)
  10068. {
  10069. TBL_PC *sd;
  10070. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  10071. if( !nd || nd->bl.type != BL_NPC )
  10072. {
  10073. script_pushint(st,1);
  10074. ShowError("setnpctimer: Invalid NPC.\n");
  10075. return SCRIPT_CMD_FAILURE;
  10076. }
  10077. if( !script_nick2sd(2,sd) ){
  10078. script_pushint(st,1);
  10079. ShowWarning("attachnpctimer: Invalid player.\n");
  10080. return SCRIPT_CMD_FAILURE;
  10081. }
  10082. nd->u.scr.rid = sd->bl.id;
  10083. script_pushint(st,0);
  10084. return SCRIPT_CMD_SUCCESS;
  10085. }
  10086. /*==========================================
  10087. * detaches a player rid from the timer [Celest]
  10088. *------------------------------------------*/
  10089. BUILDIN_FUNC(detachnpctimer)
  10090. {
  10091. struct npc_data *nd;
  10092. if( script_hasdata(st,2) )
  10093. nd = npc_name2id(script_getstr(st,2));
  10094. else
  10095. nd = (struct npc_data *)map_id2bl(st->oid);
  10096. if( !nd || nd->bl.type != BL_NPC )
  10097. {
  10098. script_pushint(st,1);
  10099. ShowError("detachnpctimer: Invalid NPC.\n");
  10100. return SCRIPT_CMD_FAILURE;
  10101. }
  10102. nd->u.scr.rid = 0;
  10103. script_pushint(st,0);
  10104. return SCRIPT_CMD_SUCCESS;
  10105. }
  10106. /*==========================================
  10107. * To avoid "player not attached" script errors, this function is provided,
  10108. * it checks if there is a player attached to the current script. [Skotlex]
  10109. * If no, returns 0, if yes, returns the account_id of the attached player.
  10110. *------------------------------------------*/
  10111. BUILDIN_FUNC(playerattached)
  10112. {
  10113. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  10114. script_pushint(st,0);
  10115. else
  10116. script_pushint(st,st->rid);
  10117. return SCRIPT_CMD_SUCCESS;
  10118. }
  10119. /*==========================================
  10120. *------------------------------------------*/
  10121. BUILDIN_FUNC(announce)
  10122. {
  10123. const char *mes = script_getstr(st,2);
  10124. int flag = script_getnum(st,3);
  10125. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  10126. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  10127. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  10128. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  10129. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  10130. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  10131. {
  10132. send_target target;
  10133. struct block_list *bl;
  10134. // If bc_npc flag is set, use NPC as broadcast source
  10135. if(flag&BC_NPC){
  10136. bl = map_id2bl(st->oid);
  10137. }else{
  10138. struct map_session_data* sd;
  10139. if(script_charid2sd(9, sd))
  10140. bl = &sd->bl;
  10141. else
  10142. bl = NULL;
  10143. }
  10144. if (bl == NULL)
  10145. return SCRIPT_CMD_SUCCESS;
  10146. switch (flag&BC_TARGET_MASK) {
  10147. case BC_MAP: target = ALL_SAMEMAP; break;
  10148. case BC_AREA: target = AREA; break;
  10149. case BC_SELF: target = SELF; break;
  10150. default: target = ALL_CLIENT; break; // BC_ALL
  10151. }
  10152. if (fontColor)
  10153. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  10154. else
  10155. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  10156. }
  10157. else
  10158. {
  10159. if (fontColor)
  10160. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  10161. else
  10162. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  10163. }
  10164. return SCRIPT_CMD_SUCCESS;
  10165. }
  10166. /*==========================================
  10167. *------------------------------------------*/
  10168. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  10169. {
  10170. char *mes = va_arg(ap, char *);
  10171. int len = va_arg(ap, int);
  10172. int type = va_arg(ap, int);
  10173. char *fontColor = va_arg(ap, char *);
  10174. short fontType = (short)va_arg(ap, int);
  10175. short fontSize = (short)va_arg(ap, int);
  10176. short fontAlign = (short)va_arg(ap, int);
  10177. short fontY = (short)va_arg(ap, int);
  10178. if (fontColor)
  10179. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  10180. else
  10181. clif_broadcast(bl, mes, len, type, SELF);
  10182. return SCRIPT_CMD_SUCCESS;
  10183. }
  10184. BUILDIN_FUNC(mapannounce)
  10185. {
  10186. const char *mapname = script_getstr(st,2);
  10187. const char *mes = script_getstr(st,3);
  10188. int flag = script_getnum(st,4);
  10189. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  10190. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  10191. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  10192. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  10193. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  10194. int16 m;
  10195. if ((m = map_mapname2mapid(mapname)) < 0)
  10196. return SCRIPT_CMD_SUCCESS;
  10197. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  10198. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  10199. return SCRIPT_CMD_SUCCESS;
  10200. }
  10201. /*==========================================
  10202. *------------------------------------------*/
  10203. BUILDIN_FUNC(areaannounce)
  10204. {
  10205. const char *mapname = script_getstr(st,2);
  10206. int x0 = script_getnum(st,3);
  10207. int y0 = script_getnum(st,4);
  10208. int x1 = script_getnum(st,5);
  10209. int y1 = script_getnum(st,6);
  10210. const char *mes = script_getstr(st,7);
  10211. int flag = script_getnum(st,8);
  10212. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  10213. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  10214. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  10215. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  10216. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  10217. int16 m;
  10218. if ((m = map_mapname2mapid(mapname)) < 0)
  10219. return SCRIPT_CMD_SUCCESS;
  10220. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  10221. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  10222. return SCRIPT_CMD_SUCCESS;
  10223. }
  10224. /*==========================================
  10225. *------------------------------------------*/
  10226. BUILDIN_FUNC(getusers)
  10227. {
  10228. int flag, val = 0;
  10229. struct map_session_data* sd;
  10230. struct block_list* bl = NULL;
  10231. flag = script_getnum(st,2);
  10232. switch(flag&0x07)
  10233. {
  10234. case 0:
  10235. if(flag&0x8)
  10236. {// npc
  10237. bl = map_id2bl(st->oid);
  10238. }
  10239. else if(script_rid2sd(sd))
  10240. {// pc
  10241. bl = &sd->bl;
  10242. }
  10243. if(bl)
  10244. {
  10245. val = map_getmapdata(bl->m)->users;
  10246. }
  10247. break;
  10248. case 1:
  10249. val = map_getusers();
  10250. break;
  10251. default:
  10252. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  10253. script_pushint(st,0);
  10254. return SCRIPT_CMD_FAILURE;
  10255. }
  10256. script_pushint(st,val);
  10257. return SCRIPT_CMD_SUCCESS;
  10258. }
  10259. /*==========================================
  10260. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  10261. *------------------------------------------*/
  10262. BUILDIN_FUNC(getmapguildusers)
  10263. {
  10264. const char *str;
  10265. int16 m;
  10266. int gid;
  10267. int c=0;
  10268. struct guild *g = NULL;
  10269. str=script_getstr(st,2);
  10270. gid=script_getnum(st,3);
  10271. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  10272. script_pushint(st,-1);
  10273. return SCRIPT_CMD_SUCCESS;
  10274. }
  10275. g = guild_search(gid);
  10276. if (g){
  10277. unsigned short i;
  10278. for(i = 0; i < g->max_member; i++)
  10279. {
  10280. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  10281. c++;
  10282. }
  10283. }
  10284. script_pushint(st,c);
  10285. return SCRIPT_CMD_SUCCESS;
  10286. }
  10287. /*==========================================
  10288. *------------------------------------------*/
  10289. BUILDIN_FUNC(getmapusers)
  10290. {
  10291. const char *str;
  10292. int16 m;
  10293. str=script_getstr(st,2);
  10294. if( (m=map_mapname2mapid(str))< 0){
  10295. script_pushint(st,-1);
  10296. return SCRIPT_CMD_SUCCESS;
  10297. }
  10298. script_pushint(st,map_getmapdata(m)->users);
  10299. return SCRIPT_CMD_SUCCESS;
  10300. }
  10301. /*==========================================
  10302. *------------------------------------------*/
  10303. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  10304. {
  10305. int *users = va_arg(ap, int *);
  10306. (*users)++;
  10307. return SCRIPT_CMD_SUCCESS;
  10308. }
  10309. BUILDIN_FUNC(getareausers)
  10310. {
  10311. const char *str;
  10312. int16 m,x0,y0,x1,y1;
  10313. int users = 0;
  10314. str=script_getstr(st,2);
  10315. x0=script_getnum(st,3);
  10316. y0=script_getnum(st,4);
  10317. x1=script_getnum(st,5);
  10318. y1=script_getnum(st,6);
  10319. if( (m=map_mapname2mapid(str))< 0){
  10320. script_pushint(st,-1);
  10321. return SCRIPT_CMD_SUCCESS;
  10322. }
  10323. map_foreachinallarea(buildin_getareausers_sub,
  10324. m,x0,y0,x1,y1,BL_PC,&users);
  10325. script_pushint(st,users);
  10326. return SCRIPT_CMD_SUCCESS;
  10327. }
  10328. /*==========================================
  10329. * getunits(<type>{,<array_variable>[<first value>]})
  10330. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  10331. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  10332. *------------------------------------------*/
  10333. BUILDIN_FUNC(getunits)
  10334. {
  10335. struct block_list *bl = NULL;
  10336. struct map_session_data *sd = NULL;
  10337. struct script_data *data = NULL;
  10338. char *command = (char *)script_getfuncname(st);
  10339. const char *str;
  10340. const char *name;
  10341. int type = script_getnum(st, 2);
  10342. int size = 0;
  10343. int32 idx, id;
  10344. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  10345. if (!strcmp(command, "getmapunits"))
  10346. {
  10347. str = script_getstr(st, 3);
  10348. if ((m = map_mapname2mapid(str)) < 0) {
  10349. script_pushint(st, -1);
  10350. st->state = END;
  10351. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10352. return SCRIPT_CMD_FAILURE;
  10353. }
  10354. if (script_hasdata(st, 4))
  10355. data = script_getdata(st, 4);
  10356. }
  10357. else if (!strcmp(command, "getareaunits"))
  10358. {
  10359. str = script_getstr(st, 3);
  10360. if ((m = map_mapname2mapid(str)) < 0) {
  10361. script_pushint(st, -1);
  10362. st->state = END;
  10363. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10364. return SCRIPT_CMD_FAILURE;
  10365. }
  10366. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  10367. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  10368. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  10369. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  10370. if (script_hasdata(st, 8))
  10371. data = script_getdata(st, 8);
  10372. }
  10373. else
  10374. {
  10375. if (script_hasdata(st, 3))
  10376. data = script_getdata(st, 3);
  10377. }
  10378. if (data)
  10379. {
  10380. if (!data_isreference(data))
  10381. {
  10382. ShowError("buildin_%s: not a variable\n", command);
  10383. script_reportdata(data);
  10384. st->state = END;
  10385. return SCRIPT_CMD_FAILURE;
  10386. }
  10387. id = reference_getid(data);
  10388. idx = reference_getindex(data);
  10389. name = reference_getname(data);
  10390. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  10391. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  10392. return SCRIPT_CMD_FAILURE;
  10393. }
  10394. }
  10395. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  10396. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  10397. {
  10398. 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)))
  10399. {
  10400. if( data ){
  10401. if( is_string_variable( name ) ){
  10402. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  10403. }else{
  10404. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  10405. }
  10406. }
  10407. size++;
  10408. }
  10409. }
  10410. mapit_free(iter);
  10411. script_pushint(st, size);
  10412. return SCRIPT_CMD_SUCCESS;
  10413. }
  10414. /*==========================================
  10415. *------------------------------------------*/
  10416. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10417. {
  10418. t_itemid nameid = va_arg(ap, t_itemid);
  10419. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  10420. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10421. if(drop->item.nameid==nameid)
  10422. (*amount)+=drop->item.amount;
  10423. return SCRIPT_CMD_SUCCESS;
  10424. }
  10425. BUILDIN_FUNC(getareadropitem)
  10426. {
  10427. const char *str;
  10428. int16 m,x0,y0,x1,y1;
  10429. t_itemid nameid;
  10430. unsigned short amount = 0;
  10431. str=script_getstr(st,2);
  10432. x0=script_getnum(st,3);
  10433. y0=script_getnum(st,4);
  10434. x1=script_getnum(st,5);
  10435. y1=script_getnum(st,6);
  10436. if( script_isstring(st, 7) ){
  10437. const char *name = script_getstr(st, 7);
  10438. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  10439. if( item_data )
  10440. nameid=item_data->nameid;
  10441. else{
  10442. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10443. return SCRIPT_CMD_FAILURE;
  10444. }
  10445. }else{
  10446. nameid = script_getnum(st, 7);
  10447. if( !item_db.exists( nameid ) ){
  10448. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10449. return SCRIPT_CMD_FAILURE;
  10450. }
  10451. }
  10452. if( (m=map_mapname2mapid(str))< 0){
  10453. script_pushint(st,-1);
  10454. return SCRIPT_CMD_SUCCESS;
  10455. }
  10456. map_foreachinallarea(buildin_getareadropitem_sub,
  10457. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10458. script_pushint(st,amount);
  10459. return SCRIPT_CMD_SUCCESS;
  10460. }
  10461. /*==========================================
  10462. *------------------------------------------*/
  10463. BUILDIN_FUNC(enablenpc)
  10464. {
  10465. npc_data* nd;
  10466. e_npcv_status flag = NPCVIEW_DISABLE;
  10467. const char* command = script_getfuncname(st);
  10468. if (script_hasdata(st, 2))
  10469. nd = npc_name2id(script_getstr(st,2));
  10470. else
  10471. nd = map_id2nd(st->oid);
  10472. if (!strcmp(command,"enablenpc"))
  10473. flag = NPCVIEW_ENABLE;
  10474. else if (!strcmp(command,"disablenpc"))
  10475. flag = NPCVIEW_DISABLE;
  10476. else if (!strcmp(command,"hideoffnpc"))
  10477. flag = NPCVIEW_HIDEOFF;
  10478. else if (!strcmp(command,"hideonnpc"))
  10479. flag = NPCVIEW_HIDEON;
  10480. else if (!strcmp(command,"cloakoffnpc") || !strcmp(command,"cloakoffnpcself"))
  10481. flag = NPCVIEW_CLOAKOFF;
  10482. else if (!strcmp(command,"cloakonnpc") || !strcmp(command,"cloakonnpcself"))
  10483. flag = NPCVIEW_CLOAKON;
  10484. else{
  10485. ShowError( "buildin_enablenpc: Undefined command \"%s\".\n", command );
  10486. return SCRIPT_CMD_FAILURE;
  10487. }
  10488. if (!nd) {
  10489. if (script_hasdata(st, 2))
  10490. 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);
  10491. else
  10492. ShowError("buildin_%s: Attempted to %s a non-existing NPC (flag=%d).\n", (flag & NPCVIEW_VISIBLE) ? "show" : "hide", command, flag);
  10493. return SCRIPT_CMD_FAILURE;
  10494. }
  10495. int char_id = 0;
  10496. if (script_hasdata(st, 3))
  10497. char_id = script_getnum(st, 3);
  10498. else if (!strcmp(command,"cloakoffnpcself") || !strcmp(command,"cloakonnpcself")) {
  10499. map_session_data* sd;
  10500. if (!script_rid2sd(sd))
  10501. return SCRIPT_CMD_SUCCESS;
  10502. char_id = sd->status.char_id;
  10503. }
  10504. if (npc_enable_target(*nd, char_id, flag))
  10505. return SCRIPT_CMD_SUCCESS;
  10506. return SCRIPT_CMD_FAILURE;
  10507. }
  10508. /* Starts a status effect on the target unit or on the attached player.
  10509. *
  10510. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10511. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10512. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10513. * <flag>: enum e_status_change_start_flags
  10514. */
  10515. BUILDIN_FUNC(sc_start)
  10516. {
  10517. TBL_NPC * nd = map_id2nd(st->oid);
  10518. struct block_list* bl;
  10519. enum sc_type type;
  10520. int tick, val1, val2, val3, val4=0, rate, flag;
  10521. char start_type;
  10522. const char* command = script_getfuncname(st);
  10523. if(strstr(command, "4"))
  10524. start_type = 4;
  10525. else if(strstr(command, "2"))
  10526. start_type = 2;
  10527. else
  10528. start_type = 1;
  10529. type = (sc_type)script_getnum(st,2);
  10530. tick = script_getnum(st,3);
  10531. val1 = script_getnum(st,4);
  10532. //If from NPC we make default flag 1 to be unavoidable
  10533. if(nd && nd->bl.id == fake_nd->bl.id)
  10534. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10535. else
  10536. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10537. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10538. if(script_hasdata(st,(6+start_type)))
  10539. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10540. else
  10541. bl = map_id2bl(st->rid);
  10542. uint16 skill_id;
  10543. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && (skill_id = status_db.getSkill(type)) > 0)
  10544. {// When there isn't a duration specified, try to get it from the skill_db
  10545. tick = skill_get_time(skill_id, val1);
  10546. }
  10547. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10548. bl = map_id2bl(potion_target);
  10549. tick /= 2;// Thrown potions only last half.
  10550. val4 = 1;// Mark that this was a thrown sc_effect
  10551. }
  10552. if(!bl)
  10553. return SCRIPT_CMD_SUCCESS;
  10554. switch(start_type) {
  10555. case 1:
  10556. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10557. break;
  10558. case 2:
  10559. val2 = script_getnum(st,5);
  10560. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10561. break;
  10562. case 4:
  10563. val2 = script_getnum(st,5);
  10564. val3 = script_getnum(st,6);
  10565. val4 = script_getnum(st,7);
  10566. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10567. break;
  10568. }
  10569. return SCRIPT_CMD_SUCCESS;
  10570. }
  10571. /// Ends one or all status effects on the target unit or on the attached player.
  10572. ///
  10573. /// sc_end <effect_id>{,<unit_id>};
  10574. BUILDIN_FUNC(sc_end)
  10575. {
  10576. struct block_list* bl;
  10577. int type;
  10578. type = script_getnum(st, 2);
  10579. if (script_hasdata(st, 3))
  10580. bl = map_id2bl(script_getnum(st, 3));
  10581. else
  10582. bl = map_id2bl(st->rid);
  10583. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10584. bl = map_id2bl(potion_target);
  10585. if (!bl)
  10586. return SCRIPT_CMD_SUCCESS;
  10587. if (type > SC_NONE && type < SC_MAX) {
  10588. struct status_change *sc = status_get_sc(bl);
  10589. if (sc == nullptr)
  10590. return SCRIPT_CMD_SUCCESS;
  10591. struct status_change_entry *sce = sc->data[type];
  10592. if (sce == nullptr)
  10593. return SCRIPT_CMD_SUCCESS;
  10594. std::shared_ptr<s_status_change_db> sc_db = status_db.find( type );
  10595. if( sc_db == nullptr ){
  10596. ShowError( "buildin_sc_end: Unknown status change %d.\n", type );
  10597. return SCRIPT_CMD_FAILURE;
  10598. }
  10599. if( sc_db->flag[SCF_NOCLEARBUFF] && sc_db->flag[SCF_NOFORCEDEND] ){
  10600. ShowError( "buildin_sc_end: Status %d cannot be cleared.\n", type );
  10601. return SCRIPT_CMD_FAILURE;
  10602. }
  10603. //This should help status_change_end force disabling the SC in case it has no limit.
  10604. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10605. status_change_end(bl, (sc_type)type);
  10606. } else
  10607. status_change_clear(bl, 3); // remove all effects
  10608. return SCRIPT_CMD_SUCCESS;
  10609. }
  10610. /**
  10611. * Ends all status effects from any learned skill on the attached player.
  10612. * if <job_id> was given it will end the effect of that class for the attached player
  10613. * sc_end_class {<char_id>{,<job_id>}};
  10614. */
  10615. BUILDIN_FUNC(sc_end_class)
  10616. {
  10617. struct map_session_data *sd;
  10618. int class_;
  10619. if (!script_charid2sd(2, sd))
  10620. return SCRIPT_CMD_FAILURE;
  10621. if (script_hasdata(st, 3))
  10622. class_ = script_getnum(st, 3);
  10623. else
  10624. class_ = sd->status.class_;
  10625. if (!pcdb_checkid(class_)) {
  10626. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10627. return SCRIPT_CMD_FAILURE;
  10628. }
  10629. status_db.changeSkillTree(sd, class_);
  10630. return SCRIPT_CMD_SUCCESS;
  10631. }
  10632. /*==========================================
  10633. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10634. *------------------------------------------*/
  10635. BUILDIN_FUNC(getscrate)
  10636. {
  10637. struct block_list *bl;
  10638. int type;
  10639. t_tick rate;
  10640. type=script_getnum(st,2);
  10641. rate=script_getnum(st,3);
  10642. if( script_hasdata(st,4) ) //get for the bl assigned
  10643. bl = map_id2bl(script_getnum(st,4));
  10644. else
  10645. bl = map_id2bl(st->rid);
  10646. if (bl)
  10647. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10648. script_pushint(st,rate);
  10649. return SCRIPT_CMD_SUCCESS;
  10650. }
  10651. /**
  10652. * getstatus(<effect type>{,<type>{,<char_id>}});
  10653. **/
  10654. BUILDIN_FUNC(getstatus)
  10655. {
  10656. int id, type;
  10657. struct map_session_data* sd;
  10658. if (!script_charid2sd(4,sd))
  10659. return SCRIPT_CMD_FAILURE;
  10660. id = script_getnum(st, 2);
  10661. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10662. if( id <= SC_NONE || id >= SC_MAX )
  10663. {// invalid status type given
  10664. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10665. return SCRIPT_CMD_SUCCESS;
  10666. }
  10667. if( sd->sc.count == 0 || !sd->sc.data[id] )
  10668. {// no status is active
  10669. script_pushint(st, 0);
  10670. return SCRIPT_CMD_SUCCESS;
  10671. }
  10672. switch( type )
  10673. {
  10674. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  10675. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  10676. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  10677. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  10678. case 5:
  10679. {
  10680. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  10681. if( timer )
  10682. {// return the amount of time remaining
  10683. script_pushint(st, timer->tick - gettick());
  10684. } else {
  10685. script_pushint(st, -1);
  10686. }
  10687. }
  10688. break;
  10689. default: script_pushint(st, 1); break;
  10690. }
  10691. return SCRIPT_CMD_SUCCESS;
  10692. }
  10693. /*==========================================
  10694. *
  10695. *------------------------------------------*/
  10696. BUILDIN_FUNC(debugmes)
  10697. {
  10698. const char *str;
  10699. str=script_getstr(st,2);
  10700. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10701. return SCRIPT_CMD_SUCCESS;
  10702. }
  10703. BUILDIN_FUNC( errormes ){
  10704. ShowError( "%s\n", script_getstr( st, 2 ) );
  10705. script_reportsrc( st );
  10706. return SCRIPT_CMD_SUCCESS;
  10707. }
  10708. /*==========================================
  10709. *------------------------------------------*/
  10710. BUILDIN_FUNC(catchpet)
  10711. {
  10712. int pet_id;
  10713. TBL_PC *sd;
  10714. if( !script_rid2sd(sd) )
  10715. return SCRIPT_CMD_SUCCESS;
  10716. pet_id= script_getnum(st,2);
  10717. pet_catch_process1(sd,pet_id);
  10718. return SCRIPT_CMD_SUCCESS;
  10719. }
  10720. /*==========================================
  10721. * [orn]
  10722. *------------------------------------------*/
  10723. BUILDIN_FUNC(homunculus_evolution)
  10724. {
  10725. TBL_PC *sd;
  10726. if( !script_rid2sd(sd) )
  10727. return SCRIPT_CMD_SUCCESS;
  10728. if(hom_is_active(sd->hd))
  10729. {
  10730. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10731. hom_evolution(sd->hd);
  10732. else
  10733. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10734. }
  10735. return SCRIPT_CMD_SUCCESS;
  10736. }
  10737. /*==========================================
  10738. * Checks for vaporized morph state
  10739. * and deletes ITEMID_STRANGE_EMBRYO.
  10740. *------------------------------------------*/
  10741. BUILDIN_FUNC(homunculus_mutate)
  10742. {
  10743. int homun_id;
  10744. TBL_PC *sd;
  10745. if( !script_rid2sd(sd) || sd->hd == NULL )
  10746. return SCRIPT_CMD_SUCCESS;
  10747. if(script_hasdata(st,2))
  10748. homun_id = script_getnum(st,2);
  10749. else
  10750. homun_id = 6048 + (rnd() % 4);
  10751. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10752. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10753. int m_id = hom_class2mapid(homun_id);
  10754. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10755. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10756. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10757. hom_call(sd); // Respawn homunculus.
  10758. hom_mutate(sd->hd, homun_id);
  10759. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10760. script_pushint(st, 1);
  10761. return SCRIPT_CMD_SUCCESS;
  10762. } else
  10763. clif_emotion(&sd->bl, ET_SWEAT);
  10764. } else
  10765. clif_emotion(&sd->bl, ET_SWEAT);
  10766. script_pushint(st, 0);
  10767. return SCRIPT_CMD_SUCCESS;
  10768. }
  10769. /*==========================================
  10770. * Puts homunculus into morph state
  10771. * and gives ITEMID_STRANGE_EMBRYO.
  10772. *------------------------------------------*/
  10773. BUILDIN_FUNC(morphembryo)
  10774. {
  10775. struct item item_tmp;
  10776. TBL_PC *sd;
  10777. if( !script_rid2sd(sd) || sd->hd == NULL )
  10778. return SCRIPT_CMD_SUCCESS;
  10779. if( hom_is_active(sd->hd) ) {
  10780. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10781. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10782. char i;
  10783. memset(&item_tmp, 0, sizeof(item_tmp));
  10784. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10785. item_tmp.identify = 1;
  10786. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10787. clif_additem(sd, 0, 0, i);
  10788. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10789. } else {
  10790. hom_vaporize(sd, HOM_ST_MORPH);
  10791. script_pushint(st, 1);
  10792. return SCRIPT_CMD_SUCCESS;
  10793. }
  10794. } else
  10795. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10796. } else
  10797. clif_emotion(&sd->bl, ET_SWEAT);
  10798. script_pushint(st, 0);
  10799. return SCRIPT_CMD_SUCCESS;
  10800. }
  10801. // [Zephyrus]
  10802. BUILDIN_FUNC(homunculus_shuffle)
  10803. {
  10804. TBL_PC *sd;
  10805. if( !script_rid2sd(sd) )
  10806. return SCRIPT_CMD_SUCCESS;
  10807. if(hom_is_active(sd->hd))
  10808. hom_shuffle(sd->hd);
  10809. return SCRIPT_CMD_SUCCESS;
  10810. }
  10811. /*==========================================
  10812. * Check for homunculus state.
  10813. * Return: -1 = No homunculus
  10814. * 0 = Homunculus is active
  10815. * 1 = Homunculus is vaporized (rest)
  10816. * 2 = Homunculus is in morph state
  10817. *------------------------------------------*/
  10818. BUILDIN_FUNC(checkhomcall)
  10819. {
  10820. TBL_PC *sd;
  10821. TBL_HOM *hd;
  10822. if( !script_rid2sd(sd) )
  10823. return SCRIPT_CMD_SUCCESS;
  10824. hd = sd->hd;
  10825. if( !hd )
  10826. script_pushint(st, -1);
  10827. else
  10828. script_pushint(st, hd->homunculus.vaporize);
  10829. return SCRIPT_CMD_SUCCESS;
  10830. }
  10831. //These two functions bring the eA MAPID_* class functionality to scripts.
  10832. BUILDIN_FUNC(eaclass)
  10833. {
  10834. int class_;
  10835. if( script_hasdata(st,2) )
  10836. class_ = script_getnum(st,2);
  10837. else {
  10838. TBL_PC *sd;
  10839. if (!script_charid2sd(3,sd)) {
  10840. script_pushint(st,-1);
  10841. return SCRIPT_CMD_SUCCESS;
  10842. }
  10843. class_ = sd->status.class_;
  10844. }
  10845. script_pushint(st,pc_jobid2mapid(class_));
  10846. return SCRIPT_CMD_SUCCESS;
  10847. }
  10848. BUILDIN_FUNC(roclass)
  10849. {
  10850. int class_ =script_getnum(st,2);
  10851. int sex;
  10852. if( script_hasdata(st,3) )
  10853. sex = script_getnum(st,3);
  10854. else {
  10855. TBL_PC *sd;
  10856. if (st->rid && script_rid2sd(sd))
  10857. sex = sd->status.sex;
  10858. else
  10859. sex = SEX_MALE; //Just use male when not found.
  10860. }
  10861. script_pushint(st,pc_mapid2jobid(class_, sex));
  10862. return SCRIPT_CMD_SUCCESS;
  10863. }
  10864. /*==========================================
  10865. * Tells client to open a hatching window, used for pet incubator
  10866. *------------------------------------------*/
  10867. BUILDIN_FUNC(birthpet)
  10868. {
  10869. TBL_PC *sd;
  10870. if( !script_rid2sd(sd) )
  10871. return SCRIPT_CMD_SUCCESS;
  10872. if( sd->status.pet_id )
  10873. {// do not send egg list, when you already have a pet
  10874. return SCRIPT_CMD_SUCCESS;
  10875. }
  10876. clif_sendegg(sd);
  10877. return SCRIPT_CMD_SUCCESS;
  10878. }
  10879. /**
  10880. * resetlvl <action type>{,<char_id>};
  10881. * @param action_type:
  10882. * 1 : make like after rebirth
  10883. * 2 : blvl,jlvl=1, skillpoint=0
  10884. * 3 : don't reset skill, blvl=1
  10885. * 4 : jlvl=0
  10886. * @author AppleGirl
  10887. **/
  10888. BUILDIN_FUNC(resetlvl)
  10889. {
  10890. TBL_PC *sd;
  10891. int type=script_getnum(st,2);
  10892. if (!script_charid2sd(3,sd))
  10893. return SCRIPT_CMD_FAILURE;
  10894. pc_resetlvl(sd,type);
  10895. return SCRIPT_CMD_SUCCESS;
  10896. }
  10897. /**
  10898. * Reset a player status point
  10899. * resetstatus({<char_id>});
  10900. **/
  10901. BUILDIN_FUNC(resetstatus)
  10902. {
  10903. TBL_PC *sd;
  10904. if (!script_charid2sd(2,sd))
  10905. return SCRIPT_CMD_FAILURE;
  10906. pc_resetstate(sd);
  10907. return SCRIPT_CMD_SUCCESS;
  10908. }
  10909. /**
  10910. * Reset player's skill
  10911. * resetskill({<char_id>});
  10912. **/
  10913. BUILDIN_FUNC(resetskill)
  10914. {
  10915. TBL_PC *sd;
  10916. if (!script_charid2sd(2,sd))
  10917. return SCRIPT_CMD_FAILURE;
  10918. pc_resetskill(sd,1);
  10919. return SCRIPT_CMD_SUCCESS;
  10920. }
  10921. /**
  10922. * Reset SG designated maps
  10923. * resetfeel({<char_id>});
  10924. **/
  10925. BUILDIN_FUNC(resetfeel)
  10926. {
  10927. TBL_PC *sd;
  10928. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10929. return SCRIPT_CMD_FAILURE;
  10930. pc_resetfeel(sd);
  10931. return SCRIPT_CMD_SUCCESS;
  10932. }
  10933. /**
  10934. * Reset SG designated monsters
  10935. * resethate({<char_id>});
  10936. **/
  10937. BUILDIN_FUNC(resethate)
  10938. {
  10939. TBL_PC *sd;
  10940. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10941. return SCRIPT_CMD_FAILURE;
  10942. pc_resethate(sd);
  10943. return SCRIPT_CMD_SUCCESS;
  10944. }
  10945. /**
  10946. * Counts total amount of skill points.
  10947. * skillpointcount({<char_id>})
  10948. **/
  10949. BUILDIN_FUNC(skillpointcount)
  10950. {
  10951. TBL_PC *sd;
  10952. if (!script_charid2sd(2,sd))
  10953. return SCRIPT_CMD_FAILURE;
  10954. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10955. return SCRIPT_CMD_SUCCESS;
  10956. }
  10957. /*==========================================
  10958. *
  10959. *------------------------------------------*/
  10960. BUILDIN_FUNC(changebase)
  10961. {
  10962. TBL_PC *sd=NULL;
  10963. int vclass;
  10964. if( !script_mapid2sd(3,sd) )
  10965. return SCRIPT_CMD_SUCCESS;
  10966. vclass = script_getnum(st,2);
  10967. if(vclass == JOB_WEDDING)
  10968. {
  10969. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10970. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10971. )
  10972. return SCRIPT_CMD_SUCCESS;
  10973. }
  10974. if(!sd->disguise && vclass != sd->vd.class_) {
  10975. status_set_viewdata(&sd->bl, vclass);
  10976. //Updated client view. Base, Weapon and Cloth Colors.
  10977. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10978. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10979. if (sd->vd.cloth_color)
  10980. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10981. if (sd->vd.body_style)
  10982. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10983. clif_skillinfoblock(sd);
  10984. }
  10985. return SCRIPT_CMD_SUCCESS;
  10986. }
  10987. /**
  10988. * Change account sex and unequip all item and request for a changesex to char-serv
  10989. * changesex({<char_id>});
  10990. */
  10991. BUILDIN_FUNC(changesex)
  10992. {
  10993. int i;
  10994. TBL_PC *sd = NULL;
  10995. if (!script_charid2sd(2,sd))
  10996. return SCRIPT_CMD_FAILURE;
  10997. pc_resetskill(sd,4);
  10998. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10999. for(i = 0; i < EQI_MAX; i++) {
  11000. if (sd->equip_index[i] >= 0)
  11001. pc_unequipitem(sd, sd->equip_index[i], 3);
  11002. }
  11003. chrif_changesex(sd, true);
  11004. return SCRIPT_CMD_SUCCESS;
  11005. }
  11006. /**
  11007. * Change character's sex and unequip all item and request for a changesex to char-serv
  11008. * changecharsex({<char_id>});
  11009. */
  11010. BUILDIN_FUNC(changecharsex)
  11011. {
  11012. #if PACKETVER >= 20141016
  11013. int i;
  11014. TBL_PC *sd = NULL;
  11015. if (!script_charid2sd(2,sd))
  11016. return SCRIPT_CMD_FAILURE;
  11017. pc_resetskill(sd,4);
  11018. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  11019. for (i = 0; i < EQI_MAX; i++) {
  11020. if (sd->equip_index[i] >= 0)
  11021. pc_unequipitem(sd, sd->equip_index[i], 3);
  11022. }
  11023. chrif_changesex(sd, false);
  11024. return SCRIPT_CMD_SUCCESS;
  11025. #else
  11026. return SCRIPT_CMD_FAILURE;
  11027. #endif
  11028. }
  11029. /*==========================================
  11030. * Works like 'announce' but outputs in the common chat window
  11031. *------------------------------------------*/
  11032. BUILDIN_FUNC(globalmes)
  11033. {
  11034. struct block_list *bl = map_id2bl(st->oid);
  11035. struct npc_data *nd = (struct npc_data *)bl;
  11036. const char *name=NULL,*mes;
  11037. mes=script_getstr(st,2);
  11038. if(mes==NULL)
  11039. return SCRIPT_CMD_SUCCESS;
  11040. if(script_hasdata(st,3)){ // npc name to display
  11041. name=script_getstr(st,3);
  11042. } else {
  11043. name=nd->name; //use current npc name
  11044. }
  11045. npc_globalmessage(name,mes); // broadcast to all players connected
  11046. return SCRIPT_CMD_SUCCESS;
  11047. }
  11048. /////////////////////////////////////////////////////////////////////
  11049. // NPC waiting room (chat room)
  11050. //
  11051. /// Creates a waiting room (chat room) for this npc.
  11052. ///
  11053. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  11054. BUILDIN_FUNC(waitingroom)
  11055. {
  11056. struct npc_data* nd;
  11057. const char* title = script_getstr(st, 2);
  11058. int limit = script_getnum(st, 3);
  11059. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  11060. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  11061. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  11062. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  11063. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  11064. nd = (struct npc_data *)map_id2bl(st->oid);
  11065. if( nd != NULL )
  11066. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  11067. return SCRIPT_CMD_SUCCESS;
  11068. }
  11069. /// Removes the waiting room of the current or target npc.
  11070. ///
  11071. /// delwaitingroom "<npc_name>";
  11072. /// delwaitingroom;
  11073. BUILDIN_FUNC(delwaitingroom)
  11074. {
  11075. struct npc_data* nd;
  11076. if( script_hasdata(st,2) )
  11077. nd = npc_name2id(script_getstr(st, 2));
  11078. else
  11079. nd = (struct npc_data *)map_id2bl(st->oid);
  11080. if( nd != NULL )
  11081. chat_deletenpcchat(nd);
  11082. return SCRIPT_CMD_SUCCESS;
  11083. }
  11084. /// Kick the specified player from the waiting room of the target npc.
  11085. ///
  11086. /// waitingroomkick "<npc_name>", <kickusername>;
  11087. BUILDIN_FUNC(waitingroomkick)
  11088. {
  11089. struct npc_data* nd;
  11090. struct chat_data* cd;
  11091. const char* kickusername;
  11092. nd = npc_name2id(script_getstr(st,2));
  11093. kickusername = script_getstr(st,3);
  11094. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11095. chat_npckickchat(cd, kickusername);
  11096. return SCRIPT_CMD_SUCCESS;
  11097. }
  11098. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  11099. /// Num users stored in .@waitingroom_usercount
  11100. ///
  11101. /// getwaitingroomusers "<npc_name>";
  11102. BUILDIN_FUNC(getwaitingroomusers)
  11103. {
  11104. struct npc_data* nd;
  11105. struct chat_data* cd;
  11106. int i, j=0;
  11107. if( script_hasdata(st,2) )
  11108. nd = npc_name2id(script_getstr(st, 2));
  11109. else
  11110. nd = (struct npc_data *)map_id2bl(st->oid);
  11111. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  11112. for(i = 0; i < cd->users; ++i) {
  11113. setd_sub_num( st, NULL, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, NULL );
  11114. j++;
  11115. }
  11116. setd_sub_num( st, NULL, ".@waitingroom_usercount", 0, j, NULL );
  11117. }
  11118. return SCRIPT_CMD_SUCCESS;
  11119. }
  11120. /// Kicks all the players from the waiting room of the current or target npc.
  11121. ///
  11122. /// kickwaitingroomall "<npc_name>";
  11123. /// kickwaitingroomall;
  11124. BUILDIN_FUNC(waitingroomkickall)
  11125. {
  11126. struct npc_data* nd;
  11127. struct chat_data* cd;
  11128. if( script_hasdata(st,2) )
  11129. nd = npc_name2id(script_getstr(st,2));
  11130. else
  11131. nd = (struct npc_data *)map_id2bl(st->oid);
  11132. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11133. chat_npckickall(cd);
  11134. return SCRIPT_CMD_SUCCESS;
  11135. }
  11136. /// Enables the waiting room event of the current or target npc.
  11137. ///
  11138. /// enablewaitingroomevent "<npc_name>";
  11139. /// enablewaitingroomevent;
  11140. BUILDIN_FUNC(enablewaitingroomevent)
  11141. {
  11142. struct npc_data* nd;
  11143. struct chat_data* cd;
  11144. if( script_hasdata(st,2) )
  11145. nd = npc_name2id(script_getstr(st, 2));
  11146. else
  11147. nd = (struct npc_data *)map_id2bl(st->oid);
  11148. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11149. chat_enableevent(cd);
  11150. return SCRIPT_CMD_SUCCESS;
  11151. }
  11152. /// Disables the waiting room event of the current or target npc.
  11153. ///
  11154. /// disablewaitingroomevent "<npc_name>";
  11155. /// disablewaitingroomevent;
  11156. BUILDIN_FUNC(disablewaitingroomevent)
  11157. {
  11158. struct npc_data *nd;
  11159. struct chat_data *cd;
  11160. if( script_hasdata(st,2) )
  11161. nd = npc_name2id(script_getstr(st, 2));
  11162. else
  11163. nd = (struct npc_data *)map_id2bl(st->oid);
  11164. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  11165. chat_disableevent(cd);
  11166. return SCRIPT_CMD_SUCCESS;
  11167. }
  11168. /// Returns info on the waiting room of the current or target npc.
  11169. /// Returns -1 if the type unknown
  11170. /// <type>=0 : current number of users
  11171. /// <type>=1 : maximum number of users allowed
  11172. /// <type>=2 : the number of users that trigger the event
  11173. /// <type>=3 : if the trigger is disabled
  11174. /// <type>=4 : the title of the waiting room
  11175. /// <type>=5 : the password of the waiting room
  11176. /// <type>=16 : the name of the waiting room event
  11177. /// <type>=32 : if the waiting room is full
  11178. /// <type>=33 : if there are enough users to trigger the event
  11179. ///
  11180. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  11181. /// getwaitingroomstate(<type>) -> <info>
  11182. BUILDIN_FUNC(getwaitingroomstate)
  11183. {
  11184. struct npc_data *nd;
  11185. struct chat_data *cd;
  11186. int type;
  11187. type = script_getnum(st,2);
  11188. if( script_hasdata(st,3) )
  11189. nd = npc_name2id(script_getstr(st, 3));
  11190. else
  11191. nd = (struct npc_data *)map_id2bl(st->oid);
  11192. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11193. {
  11194. script_pushint(st, -1);
  11195. return SCRIPT_CMD_SUCCESS;
  11196. }
  11197. switch(type)
  11198. {
  11199. case 0: script_pushint(st, cd->users); break;
  11200. case 1: script_pushint(st, cd->limit); break;
  11201. case 2: script_pushint(st, cd->trigger&0x7f); break;
  11202. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  11203. case 4: script_pushstrcopy(st, cd->title); break;
  11204. case 5: script_pushstrcopy(st, cd->pass); break;
  11205. case 16: script_pushstrcopy(st, cd->npc_event);break;
  11206. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  11207. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  11208. default: script_pushint(st, -1); break;
  11209. }
  11210. return SCRIPT_CMD_SUCCESS;
  11211. }
  11212. /// Warps the trigger or target amount of players to the target map and position.
  11213. /// Players are automatically removed from the waiting room.
  11214. /// Those waiting the longest will get warped first.
  11215. /// The target map can be "Random" for a random position in the current map,
  11216. /// and "SavePoint" for the savepoint map+position.
  11217. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  11218. ///
  11219. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  11220. /// The total number of teleported players is put into $@warpwaitingpcnum
  11221. ///
  11222. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  11223. /// warpwaitingpc "<map name>",<x>,<y>;
  11224. BUILDIN_FUNC(warpwaitingpc)
  11225. {
  11226. int x;
  11227. int y;
  11228. int i;
  11229. int n;
  11230. const char* map_name;
  11231. struct npc_data* nd;
  11232. struct chat_data* cd;
  11233. nd = (struct npc_data *)map_id2bl(st->oid);
  11234. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11235. return SCRIPT_CMD_SUCCESS;
  11236. map_name = script_getstr(st,2);
  11237. x = script_getnum(st,3);
  11238. y = script_getnum(st,4);
  11239. n = cd->trigger&0x7f;
  11240. if( script_hasdata(st,5) )
  11241. n = script_getnum(st,5);
  11242. for( i = 0; i < n && cd->users > 0; i++ )
  11243. {
  11244. TBL_PC* sd = cd->usersd[0];
  11245. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  11246. {// can't teleport on this map
  11247. break;
  11248. }
  11249. if( cd->zeny )
  11250. {// fee set
  11251. if( (uint32)sd->status.zeny < cd->zeny )
  11252. {// no zeny to cover set fee
  11253. break;
  11254. }
  11255. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  11256. }
  11257. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  11258. if( strcmp(map_name,"Random") == 0 )
  11259. pc_randomwarp(sd,CLR_TELEPORT,true);
  11260. else if( strcmp(map_name,"SavePoint") == 0 )
  11261. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  11262. else
  11263. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  11264. }
  11265. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  11266. return SCRIPT_CMD_SUCCESS;
  11267. }
  11268. /////////////////////////////////////////////////////////////////////
  11269. // ...
  11270. //
  11271. /// Detaches a character from a script.
  11272. ///
  11273. /// @param st Script state to detach the character from.
  11274. void script_detach_rid(struct script_state* st)
  11275. {
  11276. if(st->rid)
  11277. {
  11278. script_detach_state(st, false);
  11279. st->rid = 0;
  11280. }
  11281. }
  11282. /*=========================================================================
  11283. * Attaches a set of RIDs to the current script. [digitalhamster]
  11284. * addrid(<type>{,<flag>{,<parameters>}});
  11285. * <type>:
  11286. * 0 : All players in the server.
  11287. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  11288. * 2 : Party members of a specified party ID.
  11289. * [ Parameters: <party id> ]
  11290. * 3 : Guild members of a specified guild ID.
  11291. * [ Parameters: <guild id> ]
  11292. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  11293. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  11294. * 5 : All players in the map.
  11295. * [ Parameters: "<map name>" ]
  11296. * Account ID: The specified account ID.
  11297. * <flag>:
  11298. * 0 : Players are always attached. (default)
  11299. * 1 : Players currently running another script will not be attached.
  11300. *-------------------------------------------------------------------------*/
  11301. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  11302. {
  11303. int forceflag;
  11304. struct map_session_data *sd = (TBL_PC *)bl;
  11305. struct script_state* st;
  11306. st = va_arg(ap,struct script_state*);
  11307. forceflag = va_arg(ap,int);
  11308. if(!forceflag || !sd->st)
  11309. if(sd->status.account_id != st->rid)
  11310. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11311. return 0;
  11312. }
  11313. BUILDIN_FUNC(addrid)
  11314. {
  11315. struct s_mapiterator* iter;
  11316. struct block_list *bl;
  11317. TBL_PC *sd;
  11318. if(st->rid < 1) {
  11319. st->state = END;
  11320. bl = map_id2bl(st->oid);
  11321. } else
  11322. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  11323. iter = mapit_getallusers();
  11324. switch(script_getnum(st,2)) {
  11325. case 0:
  11326. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11327. if(!script_getnum(st,3) || !sd->st)
  11328. if(sd->status.account_id != st->rid) //attached player already runs.
  11329. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11330. }
  11331. break;
  11332. case 1:
  11333. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11334. if(!script_getnum(st,3) || !sd->st)
  11335. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  11336. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11337. }
  11338. break;
  11339. case 2:
  11340. if(script_getnum(st,4) == 0) {
  11341. script_pushint(st,0);
  11342. mapit_free(iter);
  11343. return SCRIPT_CMD_SUCCESS;
  11344. }
  11345. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11346. if(!script_getnum(st,3) || !sd->st)
  11347. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  11348. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11349. }
  11350. break;
  11351. case 3:
  11352. if(script_getnum(st,4) == 0) {
  11353. script_pushint(st,0);
  11354. mapit_free(iter);
  11355. return SCRIPT_CMD_SUCCESS;
  11356. }
  11357. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11358. if(!script_getnum(st,3) || !sd->st)
  11359. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  11360. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11361. }
  11362. break;
  11363. case 4:
  11364. map_foreachinallarea(buildin_addrid_sub,
  11365. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  11366. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  11367. break;
  11368. case 5:
  11369. if (script_getstr(st, 4) == NULL) {
  11370. script_pushint(st, 0);
  11371. mapit_free(iter);
  11372. return SCRIPT_CMD_FAILURE;
  11373. }
  11374. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  11375. script_pushint(st, 0);
  11376. mapit_free(iter);
  11377. return SCRIPT_CMD_FAILURE;
  11378. }
  11379. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  11380. break;
  11381. default:
  11382. mapit_free(iter);
  11383. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  11384. script_pushint(st,0);
  11385. return SCRIPT_CMD_SUCCESS;
  11386. }
  11387. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  11388. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  11389. script_pushint(st,1);
  11390. }
  11391. return SCRIPT_CMD_SUCCESS;
  11392. }
  11393. mapit_free(iter);
  11394. script_pushint(st,1);
  11395. return SCRIPT_CMD_SUCCESS;
  11396. }
  11397. /*==========================================
  11398. * Attach sd char id to script and detach current one if any
  11399. *------------------------------------------*/
  11400. BUILDIN_FUNC(attachrid)
  11401. {
  11402. int rid = script_getnum(st,2);
  11403. bool force;
  11404. if( script_hasdata(st,3) ){
  11405. force = script_getnum(st,3) != 0;
  11406. }else{
  11407. force = true;
  11408. }
  11409. struct map_session_data* sd = map_id2sd(rid);
  11410. if( sd != NULL && ( !sd->npc_id || force ) ){
  11411. script_detach_rid(st);
  11412. st->rid = rid;
  11413. script_attach_state(st);
  11414. script_pushint(st,true);
  11415. }else{
  11416. script_pushint(st,false);
  11417. }
  11418. return SCRIPT_CMD_SUCCESS;
  11419. }
  11420. /*==========================================
  11421. * Detach script to rid
  11422. *------------------------------------------*/
  11423. BUILDIN_FUNC(detachrid)
  11424. {
  11425. script_detach_rid(st);
  11426. return SCRIPT_CMD_SUCCESS;
  11427. }
  11428. /*==========================================
  11429. * Chk if account connected, (and charid from account if specified)
  11430. *------------------------------------------*/
  11431. BUILDIN_FUNC(isloggedin)
  11432. {
  11433. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11434. if (script_hasdata(st,3) && sd &&
  11435. sd->status.char_id != script_getnum(st,3))
  11436. sd = NULL;
  11437. push_val(st->stack,C_INT,sd!=NULL);
  11438. return SCRIPT_CMD_SUCCESS;
  11439. }
  11440. /*==========================================
  11441. *
  11442. *------------------------------------------*/
  11443. BUILDIN_FUNC(setmapflagnosave)
  11444. {
  11445. int16 m,x,y;
  11446. unsigned short mapindex;
  11447. const char *str,*str2;
  11448. union u_mapflag_args args = {};
  11449. str=script_getstr(st,2);
  11450. str2=script_getstr(st,3);
  11451. x=script_getnum(st,4);
  11452. y=script_getnum(st,5);
  11453. m = map_mapname2mapid(str);
  11454. mapindex = mapindex_name2id(str2);
  11455. if(m < 0) {
  11456. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11457. return SCRIPT_CMD_FAILURE;
  11458. }
  11459. args.nosave.map = mapindex;
  11460. args.nosave.x = x;
  11461. args.nosave.y = y;
  11462. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11463. return SCRIPT_CMD_SUCCESS;
  11464. }
  11465. BUILDIN_FUNC(getmapflag)
  11466. {
  11467. int16 m;
  11468. int mf;
  11469. const char *str;
  11470. str=script_getstr(st,2);
  11471. m = map_mapname2mapid(str);
  11472. if (m < 0) {
  11473. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11474. return SCRIPT_CMD_FAILURE;
  11475. }
  11476. mf = script_getnum(st, 3);
  11477. if( mf < MF_MIN || mf > MF_MAX ){
  11478. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11479. return SCRIPT_CMD_FAILURE;
  11480. }
  11481. union u_mapflag_args args = {};
  11482. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11483. args.flag_val = SKILLDMG_MAX;
  11484. else
  11485. FETCH(4, args.flag_val);
  11486. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11487. return SCRIPT_CMD_SUCCESS;
  11488. }
  11489. BUILDIN_FUNC(setmapflag)
  11490. {
  11491. int16 m;
  11492. int mf;
  11493. const char *str;
  11494. str = script_getstr(st,2);
  11495. m = map_mapname2mapid(str);
  11496. if (m < 0) {
  11497. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11498. return SCRIPT_CMD_FAILURE;
  11499. }
  11500. mf = script_getnum(st, 3);
  11501. if( mf < MF_MIN || mf > MF_MAX ){
  11502. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11503. return SCRIPT_CMD_FAILURE;
  11504. }
  11505. union u_mapflag_args args = {};
  11506. switch(mf) {
  11507. case MF_SKILL_DAMAGE:
  11508. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11509. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11510. else {
  11511. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11512. return SCRIPT_CMD_FAILURE;
  11513. }
  11514. break;
  11515. case MF_SKILL_DURATION:
  11516. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11517. args.skill_duration.skill_id = script_getnum(st, 4);
  11518. if (!skill_get_index(args.skill_duration.skill_id)) {
  11519. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11520. return SCRIPT_CMD_FAILURE;
  11521. }
  11522. args.skill_duration.per = script_getnum(st, 5);
  11523. } else {
  11524. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11525. return SCRIPT_CMD_FAILURE;
  11526. }
  11527. break;
  11528. case MF_NOSAVE: // Assume setting "SavePoint"
  11529. args.nosave.map = 0;
  11530. args.nosave.x = -1;
  11531. args.nosave.y = -1;
  11532. break;
  11533. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11534. args.nightmaredrop.drop_id = -1;
  11535. args.nightmaredrop.drop_per = 300;
  11536. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11537. break;
  11538. default:
  11539. FETCH(4, args.flag_val);
  11540. break;
  11541. }
  11542. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11543. return SCRIPT_CMD_SUCCESS;
  11544. }
  11545. BUILDIN_FUNC(removemapflag)
  11546. {
  11547. int16 m;
  11548. int mf;
  11549. const char *str;
  11550. union u_mapflag_args args = {};
  11551. str = script_getstr(st, 2);
  11552. m = map_mapname2mapid(str);
  11553. if (m < 0) {
  11554. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11555. return SCRIPT_CMD_FAILURE;
  11556. }
  11557. mf = script_getnum(st, 3);
  11558. if( mf < MF_MIN || mf > MF_MAX ){
  11559. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11560. return SCRIPT_CMD_FAILURE;
  11561. }
  11562. FETCH(4, args.flag_val);
  11563. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11564. return SCRIPT_CMD_SUCCESS;
  11565. }
  11566. BUILDIN_FUNC(pvpon)
  11567. {
  11568. int16 m;
  11569. const char *str = script_getstr(st,2);
  11570. m = map_mapname2mapid(str);
  11571. if (m < 0) {
  11572. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11573. return SCRIPT_CMD_FAILURE;
  11574. }
  11575. if (!map_getmapflag(m, MF_PVP))
  11576. map_setmapflag(m, MF_PVP, true);
  11577. return SCRIPT_CMD_SUCCESS;
  11578. }
  11579. BUILDIN_FUNC(pvpoff)
  11580. {
  11581. int16 m;
  11582. const char *str = script_getstr(st,2);
  11583. m = map_mapname2mapid(str);
  11584. if (m < 0) {
  11585. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11586. return SCRIPT_CMD_FAILURE;
  11587. }
  11588. if (map_getmapflag(m, MF_PVP))
  11589. map_setmapflag(m, MF_PVP, false);
  11590. return SCRIPT_CMD_SUCCESS;
  11591. }
  11592. BUILDIN_FUNC(gvgon)
  11593. {
  11594. int16 m;
  11595. const char *str = script_getstr(st,2);
  11596. m = map_mapname2mapid(str);
  11597. if (m < 0) {
  11598. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11599. return SCRIPT_CMD_FAILURE;
  11600. }
  11601. if (!map_getmapflag(m, MF_GVG))
  11602. map_setmapflag(m, MF_GVG, true);
  11603. return SCRIPT_CMD_SUCCESS;
  11604. }
  11605. BUILDIN_FUNC(gvgoff)
  11606. {
  11607. int16 m;
  11608. const char *str = script_getstr(st,2);
  11609. m = map_mapname2mapid(str);
  11610. if (m < 0) {
  11611. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11612. return SCRIPT_CMD_FAILURE;
  11613. }
  11614. if (map_getmapflag(m, MF_GVG))
  11615. map_setmapflag(m, MF_GVG, false);
  11616. return SCRIPT_CMD_SUCCESS;
  11617. }
  11618. BUILDIN_FUNC(gvgon3)
  11619. {
  11620. int16 m;
  11621. const char *str = script_getstr(st,2);
  11622. m = map_mapname2mapid(str);
  11623. if (m < 0) {
  11624. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11625. return SCRIPT_CMD_FAILURE;
  11626. }
  11627. if (!map_getmapflag(m, MF_GVG_TE))
  11628. map_setmapflag(m, MF_GVG_TE, true);
  11629. return SCRIPT_CMD_SUCCESS;
  11630. }
  11631. BUILDIN_FUNC(gvgoff3)
  11632. {
  11633. int16 m;
  11634. const char *str = script_getstr(st,2);
  11635. m = map_mapname2mapid(str);
  11636. if (m < 0) {
  11637. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11638. return SCRIPT_CMD_FAILURE;
  11639. }
  11640. if (map_getmapflag(m, MF_GVG_TE))
  11641. map_setmapflag(m, MF_GVG_TE, false);
  11642. return SCRIPT_CMD_SUCCESS;
  11643. }
  11644. /**
  11645. * Shows an emotion on top of a NPC by default or the given GID
  11646. * emotion <emotion ID>{,<target ID>};
  11647. */
  11648. BUILDIN_FUNC(emotion)
  11649. {
  11650. struct block_list *bl = NULL;
  11651. int type = script_getnum(st,2);
  11652. if (type < ET_SURPRISE || type >= ET_MAX) {
  11653. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11654. return SCRIPT_CMD_FAILURE;
  11655. }
  11656. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11657. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11658. return SCRIPT_CMD_FAILURE;
  11659. }
  11660. if (!bl)
  11661. bl = map_id2bl(st->oid);
  11662. clif_emotion(bl, type);
  11663. return SCRIPT_CMD_SUCCESS;
  11664. }
  11665. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11666. {
  11667. int16 m=va_arg(ap,int);
  11668. int g_id=va_arg(ap,int);
  11669. int flag=va_arg(ap,int);
  11670. if(!sd || sd->bl.m != m)
  11671. return 0;
  11672. if(
  11673. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11674. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11675. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11676. )
  11677. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11678. return 1;
  11679. }
  11680. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11681. {
  11682. struct mob_data *md=(struct mob_data *)bl;
  11683. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11684. status_kill(bl);
  11685. return 1;
  11686. }
  11687. /*
  11688. * Function to kick guild members out of a map and to their save points.
  11689. * m : mapid
  11690. * g_id : owner guild id
  11691. * flag & 1 : Warp out owners
  11692. * flag & 2 : Warp out outsiders
  11693. * flag & 4 : reserved for mobs
  11694. * */
  11695. BUILDIN_FUNC(maprespawnguildid)
  11696. {
  11697. const char *mapname=script_getstr(st,2);
  11698. int g_id=script_getnum(st,3);
  11699. int flag=script_getnum(st,4);
  11700. int16 m=map_mapname2mapid(mapname);
  11701. if(m == -1)
  11702. return SCRIPT_CMD_SUCCESS;
  11703. //Catch ALL players (in case some are 'between maps' on execution time)
  11704. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11705. if (flag&4) //Remove script mobs.
  11706. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11707. return SCRIPT_CMD_SUCCESS;
  11708. }
  11709. /// Siege commands
  11710. /**
  11711. * Start WoE:FE
  11712. * agitstart();
  11713. */
  11714. BUILDIN_FUNC(agitstart)
  11715. {
  11716. guild_agit_start();
  11717. return SCRIPT_CMD_SUCCESS;
  11718. }
  11719. /**
  11720. * End WoE:FE
  11721. * agitend();
  11722. */
  11723. BUILDIN_FUNC(agitend)
  11724. {
  11725. guild_agit_end();
  11726. return SCRIPT_CMD_SUCCESS;
  11727. }
  11728. /**
  11729. * Start WoE:SE
  11730. * agitstart2();
  11731. */
  11732. BUILDIN_FUNC(agitstart2)
  11733. {
  11734. guild_agit2_start();
  11735. return SCRIPT_CMD_SUCCESS;
  11736. }
  11737. /**
  11738. * End WoE:SE
  11739. * agitend();
  11740. */
  11741. BUILDIN_FUNC(agitend2)
  11742. {
  11743. guild_agit2_end();
  11744. return SCRIPT_CMD_SUCCESS;
  11745. }
  11746. /**
  11747. * Start WoE:TE
  11748. * agitstart3();
  11749. */
  11750. BUILDIN_FUNC(agitstart3)
  11751. {
  11752. guild_agit3_start();
  11753. return SCRIPT_CMD_SUCCESS;
  11754. }
  11755. /**
  11756. * End WoE:TE
  11757. * agitend3();
  11758. */
  11759. BUILDIN_FUNC(agitend3)
  11760. {
  11761. guild_agit3_end();
  11762. return SCRIPT_CMD_SUCCESS;
  11763. }
  11764. /**
  11765. * Returns whether WoE:FE is on or off.
  11766. * agitcheck();
  11767. */
  11768. BUILDIN_FUNC(agitcheck)
  11769. {
  11770. script_pushint(st, agit_flag);
  11771. return SCRIPT_CMD_SUCCESS;
  11772. }
  11773. /**
  11774. * Returns whether WoE:SE is on or off.
  11775. * agitcheck2();
  11776. */
  11777. BUILDIN_FUNC(agitcheck2)
  11778. {
  11779. script_pushint(st, agit2_flag);
  11780. return SCRIPT_CMD_SUCCESS;
  11781. }
  11782. /**
  11783. * Returns whether WoE:TE is on or off.
  11784. * agitcheck3();
  11785. */
  11786. BUILDIN_FUNC(agitcheck3)
  11787. {
  11788. script_pushint(st, agit3_flag);
  11789. return SCRIPT_CMD_SUCCESS;
  11790. }
  11791. /// Sets the guild_id of this npc.
  11792. ///
  11793. /// flagemblem <guild_id>;
  11794. BUILDIN_FUNC(flagemblem)
  11795. {
  11796. TBL_NPC* nd;
  11797. int g_id = script_getnum(st,2);
  11798. if(g_id < 0)
  11799. return SCRIPT_CMD_SUCCESS;
  11800. nd = (TBL_NPC*)map_id2nd(st->oid);
  11801. if( nd == NULL ) {
  11802. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11803. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11804. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11805. } else {
  11806. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11807. nd->u.scr.guild_id = g_id;
  11808. clif_guild_emblem_area(&nd->bl);
  11809. /* guild flag caching */
  11810. if( g_id ) /* adding a id */
  11811. guild_flag_add(nd);
  11812. else if( changed ) /* removing a flag */
  11813. guild_flag_remove(nd);
  11814. }
  11815. return SCRIPT_CMD_SUCCESS;
  11816. }
  11817. BUILDIN_FUNC(getcastlename)
  11818. {
  11819. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11820. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11821. const char* name = (gc) ? gc->castle_name : "";
  11822. script_pushstrcopy(st,name);
  11823. return SCRIPT_CMD_SUCCESS;
  11824. }
  11825. BUILDIN_FUNC(getcastledata)
  11826. {
  11827. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11828. int index = script_getnum(st,3);
  11829. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11830. if (gc == NULL) {
  11831. script_pushint(st,0);
  11832. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11833. return SCRIPT_CMD_FAILURE;
  11834. }
  11835. switch (index) {
  11836. case CD_GUILD_ID:
  11837. script_pushint(st,gc->guild_id); break;
  11838. case CD_CURRENT_ECONOMY:
  11839. script_pushint(st,gc->economy); break;
  11840. case CD_CURRENT_DEFENSE:
  11841. script_pushint(st,gc->defense); break;
  11842. case CD_INVESTED_ECONOMY:
  11843. script_pushint(st,gc->triggerE); break;
  11844. case CD_INVESTED_DEFENSE:
  11845. script_pushint(st,gc->triggerD); break;
  11846. case CD_NEXT_TIME:
  11847. script_pushint(st,gc->nextTime); break;
  11848. case CD_PAY_TIME:
  11849. script_pushint(st,gc->payTime); break;
  11850. case CD_CREATE_TIME:
  11851. script_pushint(st,gc->createTime); break;
  11852. case CD_ENABLED_KAFRA:
  11853. script_pushint(st,gc->visibleC); break;
  11854. default:
  11855. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11856. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11857. break;
  11858. }
  11859. script_pushint(st,0);
  11860. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11861. return SCRIPT_CMD_FAILURE;
  11862. }
  11863. return SCRIPT_CMD_SUCCESS;
  11864. }
  11865. BUILDIN_FUNC(setcastledata)
  11866. {
  11867. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11868. int index = script_getnum(st,3);
  11869. int value = script_getnum(st,4);
  11870. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11871. if (gc == NULL) {
  11872. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11873. return SCRIPT_CMD_FAILURE;
  11874. }
  11875. if (index <= CD_NONE || index >= CD_MAX) {
  11876. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11877. return SCRIPT_CMD_FAILURE;
  11878. }
  11879. guild_castledatasave(gc->castle_id, index, value);
  11880. return SCRIPT_CMD_SUCCESS;
  11881. }
  11882. /* =====================================================================
  11883. * ---------------------------------------------------------------------*/
  11884. BUILDIN_FUNC(requestguildinfo)
  11885. {
  11886. int guild_id=script_getnum(st,2);
  11887. const char *event=NULL;
  11888. if( script_hasdata(st,3) ){
  11889. event=script_getstr(st,3);
  11890. check_event(st, event);
  11891. }
  11892. if(guild_id>0)
  11893. guild_npc_request_info(guild_id,event);
  11894. return SCRIPT_CMD_SUCCESS;
  11895. }
  11896. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11897. /// getequipcardcnt(<equipment slot>);
  11898. BUILDIN_FUNC(getequipcardcnt)
  11899. {
  11900. int i=-1,j,num;
  11901. TBL_PC *sd;
  11902. int count;
  11903. if( !script_rid2sd(sd) )
  11904. return SCRIPT_CMD_SUCCESS;
  11905. num=script_getnum(st,2);
  11906. if (equip_index_check(num))
  11907. i=pc_checkequip(sd,equip_bitmask[num]);
  11908. if (i < 0 || !sd->inventory_data[i]) {
  11909. script_pushint(st,0);
  11910. return SCRIPT_CMD_SUCCESS;
  11911. }
  11912. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11913. {
  11914. script_pushint(st,0);
  11915. return SCRIPT_CMD_SUCCESS;
  11916. }
  11917. count = 0;
  11918. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11919. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11920. count++;
  11921. script_pushint(st,count);
  11922. return SCRIPT_CMD_SUCCESS;
  11923. }
  11924. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11925. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11926. /// successremovecards <slot>;
  11927. BUILDIN_FUNC(successremovecards) {
  11928. int i=-1,c,cardflag=0;
  11929. TBL_PC* sd;
  11930. int num;
  11931. if( !script_rid2sd(sd) )
  11932. return SCRIPT_CMD_SUCCESS;
  11933. num = script_getnum(st,2);
  11934. if (equip_index_check(num))
  11935. i=pc_checkequip(sd,equip_bitmask[num]);
  11936. if (i < 0 || !sd->inventory_data[i]) {
  11937. return SCRIPT_CMD_SUCCESS;
  11938. }
  11939. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11940. return SCRIPT_CMD_SUCCESS;
  11941. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11942. 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
  11943. unsigned char flag = 0;
  11944. struct item item_tmp;
  11945. memset(&item_tmp,0,sizeof(item_tmp));
  11946. cardflag = 1;
  11947. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11948. item_tmp.identify = 1;
  11949. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11950. clif_additem(sd,0,0,flag);
  11951. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11952. }
  11953. }
  11954. }
  11955. if(cardflag == 1) {//if card was remove remplace item with no card
  11956. unsigned char flag = 0;
  11957. struct item item_tmp;
  11958. memset(&item_tmp,0,sizeof(item_tmp));
  11959. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11960. item_tmp.identify = 1;
  11961. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11962. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11963. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11964. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11965. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  11966. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11967. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11968. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11969. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11970. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11971. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11972. }
  11973. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11974. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11975. clif_additem(sd,0,0,flag);
  11976. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11977. }
  11978. clif_misceffect(&sd->bl,3);
  11979. }
  11980. return SCRIPT_CMD_SUCCESS;
  11981. }
  11982. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11983. /// failedremovecards <slot>, <type>;
  11984. /// <type>=0 : will destroy both the item and the cards.
  11985. /// <type>=1 : will keep the item, but destroy the cards.
  11986. /// <type>=2 : will keep the cards, but destroy the item.
  11987. /// <type>=? : will just display the failure effect.
  11988. BUILDIN_FUNC(failedremovecards) {
  11989. int i=-1,c,cardflag=0;
  11990. TBL_PC* sd;
  11991. int num;
  11992. int typefail;
  11993. if( !script_rid2sd(sd) )
  11994. return SCRIPT_CMD_SUCCESS;
  11995. num = script_getnum(st,2);
  11996. typefail = script_getnum(st,3);
  11997. if (equip_index_check(num))
  11998. i=pc_checkequip(sd,equip_bitmask[num]);
  11999. if (i < 0 || !sd->inventory_data[i])
  12000. return SCRIPT_CMD_SUCCESS;
  12001. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12002. return SCRIPT_CMD_SUCCESS;
  12003. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  12004. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  12005. cardflag = 1;
  12006. if(typefail == 2) {// add cards to inventory, clear
  12007. unsigned char flag = 0;
  12008. struct item item_tmp;
  12009. memset(&item_tmp,0,sizeof(item_tmp));
  12010. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  12011. item_tmp.identify = 1;
  12012. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  12013. clif_additem(sd,0,0,flag);
  12014. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  12015. }
  12016. }
  12017. }
  12018. }
  12019. if(cardflag == 1) {
  12020. if(typefail == 0 || typefail == 2){ // destroy the item
  12021. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  12022. }else if(typefail == 1){ // destroy the card
  12023. unsigned char flag = 0;
  12024. struct item item_tmp;
  12025. memset(&item_tmp,0,sizeof(item_tmp));
  12026. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  12027. item_tmp.identify = 1;
  12028. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  12029. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  12030. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  12031. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  12032. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  12033. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  12034. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  12035. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  12036. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  12037. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  12038. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  12039. }
  12040. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  12041. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  12042. clif_additem(sd,0,0,flag);
  12043. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  12044. }
  12045. }
  12046. clif_misceffect(&sd->bl,2);
  12047. }
  12048. return SCRIPT_CMD_SUCCESS;
  12049. }
  12050. /* ================================================================
  12051. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  12052. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  12053. * improved by [Lance]
  12054. * ================================================================*/
  12055. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  12056. {
  12057. int x,y,m,check_val=0,check_ID=0,i=0;
  12058. struct guild *g = NULL;
  12059. struct party_data *p = NULL;
  12060. const char *str;
  12061. const char *mapname;
  12062. unsigned int index;
  12063. mapname=script_getstr(st,2);
  12064. str=script_getstr(st,3);
  12065. x=script_getnum(st,4);
  12066. y=script_getnum(st,5);
  12067. if(script_hasdata(st,7)){
  12068. check_val=script_getnum(st,6);
  12069. check_ID=script_getnum(st,7);
  12070. }
  12071. if((m=map_mapname2mapid(mapname))< 0)
  12072. return SCRIPT_CMD_SUCCESS;
  12073. if(!(index=mapindex_name2id(str)))
  12074. return SCRIPT_CMD_SUCCESS;
  12075. switch(check_val){
  12076. case 1:
  12077. g = guild_search(check_ID);
  12078. if (g){
  12079. for( i=0; i < g->max_member; i++)
  12080. {
  12081. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  12082. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  12083. }
  12084. }
  12085. }
  12086. break;
  12087. case 2:
  12088. p = party_search(check_ID);
  12089. if(p){
  12090. for(i=0;i<MAX_PARTY; i++){
  12091. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  12092. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  12093. }
  12094. }
  12095. }
  12096. break;
  12097. default:
  12098. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  12099. break;
  12100. }
  12101. return SCRIPT_CMD_SUCCESS;
  12102. }
  12103. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  12104. {
  12105. char *event=va_arg(ap,char *);
  12106. struct mob_data *md = ((struct mob_data *)bl);
  12107. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  12108. return 1;
  12109. return SCRIPT_CMD_SUCCESS;
  12110. }
  12111. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  12112. {
  12113. const char *mapname,*event;
  12114. int16 m;
  12115. mapname=script_getstr(st,2);
  12116. event=script_getstr(st,3);
  12117. if( strcmp(event, "all") == 0 )
  12118. event = NULL;
  12119. else
  12120. check_event(st, event);
  12121. if( strcmp(mapname, "this") == 0 ) {
  12122. struct map_session_data *sd;
  12123. if( script_rid2sd(sd) )
  12124. m = sd->bl.m;
  12125. else {
  12126. script_pushint(st,-1);
  12127. return SCRIPT_CMD_SUCCESS;
  12128. }
  12129. }
  12130. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  12131. script_pushint(st,-1);
  12132. return SCRIPT_CMD_SUCCESS;
  12133. }
  12134. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  12135. return SCRIPT_CMD_SUCCESS;
  12136. }
  12137. BUILDIN_FUNC(marriage)
  12138. {
  12139. TBL_PC *sd;
  12140. TBL_PC *p_sd;
  12141. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  12142. script_pushint(st,0);
  12143. return SCRIPT_CMD_SUCCESS;
  12144. }
  12145. script_pushint(st,1);
  12146. return SCRIPT_CMD_SUCCESS;
  12147. }
  12148. BUILDIN_FUNC(wedding_effect)
  12149. {
  12150. TBL_PC *sd;
  12151. struct block_list *bl;
  12152. if(!script_rid2sd(sd)) {
  12153. bl=map_id2bl(st->oid);
  12154. } else
  12155. bl=&sd->bl;
  12156. clif_wedding_effect(bl);
  12157. return SCRIPT_CMD_SUCCESS;
  12158. }
  12159. /**
  12160. * divorce({<char_id>})
  12161. **/
  12162. BUILDIN_FUNC(divorce)
  12163. {
  12164. TBL_PC *sd;
  12165. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  12166. script_pushint(st,0);
  12167. return SCRIPT_CMD_FAILURE;
  12168. }
  12169. script_pushint(st,1);
  12170. return SCRIPT_CMD_SUCCESS;
  12171. }
  12172. /**
  12173. * ispartneron({<char_id>})
  12174. **/
  12175. BUILDIN_FUNC(ispartneron)
  12176. {
  12177. TBL_PC *sd;
  12178. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  12179. map_charid2sd(sd->status.partner_id) == NULL)
  12180. {
  12181. script_pushint(st,0);
  12182. return SCRIPT_CMD_FAILURE;
  12183. }
  12184. script_pushint(st,1);
  12185. return SCRIPT_CMD_SUCCESS;
  12186. }
  12187. /**
  12188. * getpartnerid({<char_id>})
  12189. **/
  12190. BUILDIN_FUNC(getpartnerid)
  12191. {
  12192. TBL_PC *sd;
  12193. if (!script_charid2sd(2,sd)) {
  12194. script_pushint(st,0);
  12195. return SCRIPT_CMD_FAILURE;
  12196. }
  12197. script_pushint(st,sd->status.partner_id);
  12198. return SCRIPT_CMD_SUCCESS;
  12199. }
  12200. /**
  12201. * getchildid({<char_id>})
  12202. **/
  12203. BUILDIN_FUNC(getchildid)
  12204. {
  12205. TBL_PC *sd;
  12206. if (!script_charid2sd(2,sd)) {
  12207. script_pushint(st,0);
  12208. return SCRIPT_CMD_FAILURE;
  12209. }
  12210. script_pushint(st,sd->status.child);
  12211. return SCRIPT_CMD_SUCCESS;
  12212. }
  12213. /**
  12214. * getmotherid({<char_id>})
  12215. **/
  12216. BUILDIN_FUNC(getmotherid)
  12217. {
  12218. TBL_PC *sd;
  12219. if (!script_charid2sd(2,sd)) {
  12220. script_pushint(st,0);
  12221. return SCRIPT_CMD_FAILURE;
  12222. }
  12223. script_pushint(st,sd->status.mother);
  12224. return SCRIPT_CMD_SUCCESS;
  12225. }
  12226. /**
  12227. * getfatherid({<char_id>})
  12228. **/
  12229. BUILDIN_FUNC(getfatherid)
  12230. {
  12231. TBL_PC *sd;
  12232. if (!script_charid2sd(2,sd)) {
  12233. script_pushint(st,0);
  12234. return SCRIPT_CMD_FAILURE;
  12235. }
  12236. script_pushint(st,sd->status.father);
  12237. return SCRIPT_CMD_SUCCESS;
  12238. }
  12239. BUILDIN_FUNC(warppartner)
  12240. {
  12241. int x,y;
  12242. unsigned short mapindex;
  12243. const char *str;
  12244. TBL_PC *sd;
  12245. TBL_PC *p_sd;
  12246. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  12247. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  12248. script_pushint(st,0);
  12249. return SCRIPT_CMD_SUCCESS;
  12250. }
  12251. str=script_getstr(st,2);
  12252. x=script_getnum(st,3);
  12253. y=script_getnum(st,4);
  12254. mapindex = mapindex_name2id(str);
  12255. if (mapindex) {
  12256. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  12257. script_pushint(st,1);
  12258. } else
  12259. script_pushint(st,0);
  12260. return SCRIPT_CMD_SUCCESS;
  12261. }
  12262. /*================================================
  12263. * Script for Displaying MOB Information [Valaris]
  12264. *------------------------------------------------*/
  12265. BUILDIN_FUNC(strmobinfo)
  12266. {
  12267. int num=script_getnum(st,2);
  12268. int class_=script_getnum(st,3);
  12269. if(!mobdb_checkid(class_))
  12270. {
  12271. if (num < 3) //requested a string
  12272. script_pushconststr(st,"");
  12273. else
  12274. script_pushint(st,0);
  12275. return SCRIPT_CMD_SUCCESS;
  12276. }
  12277. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  12278. switch (num) {
  12279. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  12280. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  12281. case 3: script_pushint(st,mob->lv); break;
  12282. case 4: script_pushint(st,mob->status.max_hp); break;
  12283. case 5: script_pushint(st,mob->status.max_sp); break;
  12284. case 6: script_pushint(st,mob->base_exp); break;
  12285. case 7: script_pushint(st,mob->job_exp); break;
  12286. default:
  12287. script_pushint(st,0);
  12288. break;
  12289. }
  12290. return SCRIPT_CMD_SUCCESS;
  12291. }
  12292. /*==========================================
  12293. * Summon guardians [Valaris]
  12294. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  12295. *------------------------------------------*/
  12296. BUILDIN_FUNC(guardian)
  12297. {
  12298. int class_=0,x=0,y=0,guardian=0;
  12299. const char *str,*mapname,*evt="";
  12300. bool has_index = false;
  12301. mapname =script_getstr(st,2);
  12302. x =script_getnum(st,3);
  12303. y =script_getnum(st,4);
  12304. str =script_getstr(st,5);
  12305. class_=script_getnum(st,6);
  12306. if( script_hasdata(st,8) )
  12307. {// "<event label>",<guardian index>
  12308. evt=script_getstr(st,7);
  12309. guardian=script_getnum(st,8);
  12310. has_index = true;
  12311. } else if( script_hasdata(st,7) ){
  12312. if( script_isstring(st, 7) )
  12313. {// "<event label>"
  12314. evt=script_getstr(st,7);
  12315. } else
  12316. {// <guardian index>
  12317. guardian=script_getnum(st,7);
  12318. has_index = true;
  12319. }
  12320. }
  12321. check_event(st, evt);
  12322. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  12323. return SCRIPT_CMD_SUCCESS;
  12324. }
  12325. /*==========================================
  12326. * Invisible Walls [Zephyrus]
  12327. *------------------------------------------*/
  12328. BUILDIN_FUNC(setwall)
  12329. {
  12330. const char *mapname, *name;
  12331. int x, y, m, size, dir;
  12332. bool shootable;
  12333. mapname = script_getstr(st,2);
  12334. x = script_getnum(st,3);
  12335. y = script_getnum(st,4);
  12336. size = script_getnum(st,5);
  12337. dir = script_getnum(st,6);
  12338. shootable = script_getnum(st,7) != 0;
  12339. name = script_getstr(st,8);
  12340. if( (m = map_mapname2mapid(mapname)) < 0 )
  12341. return SCRIPT_CMD_SUCCESS; // Invalid Map
  12342. map_iwall_set(m, x, y, size, dir, shootable, name);
  12343. return SCRIPT_CMD_SUCCESS;
  12344. }
  12345. BUILDIN_FUNC(delwall)
  12346. {
  12347. const char *name = script_getstr(st,2);
  12348. if( !map_iwall_remove(name) ){
  12349. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  12350. return SCRIPT_CMD_FAILURE;
  12351. }
  12352. return SCRIPT_CMD_SUCCESS;
  12353. }
  12354. BUILDIN_FUNC(checkwall)
  12355. {
  12356. const char *wall_name = script_getstr(st, 2);
  12357. script_pushint(st, map_iwall_exist(wall_name));
  12358. return SCRIPT_CMD_SUCCESS;
  12359. }
  12360. /// Retrieves various information about the specified guardian.
  12361. ///
  12362. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  12363. /// type: 0 - whether it is deployed or not
  12364. /// 1 - maximum hp
  12365. /// 2 - current hp
  12366. ///
  12367. BUILDIN_FUNC(guardianinfo)
  12368. {
  12369. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  12370. int id = script_getnum(st,3);
  12371. int type = script_getnum(st,4);
  12372. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  12373. struct mob_data* gd;
  12374. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  12375. {
  12376. script_pushint(st,-1);
  12377. return SCRIPT_CMD_SUCCESS;
  12378. }
  12379. if( type == 0 )
  12380. script_pushint(st, gc->guardian[id].visible);
  12381. else
  12382. if( !gc->guardian[id].visible )
  12383. script_pushint(st,-1);
  12384. else
  12385. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  12386. script_pushint(st,-1);
  12387. else
  12388. {
  12389. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  12390. else if( type == 2 ) script_pushint(st,gd->status.hp);
  12391. else
  12392. script_pushint(st,-1);
  12393. }
  12394. return SCRIPT_CMD_SUCCESS;
  12395. }
  12396. /*==========================================
  12397. * Get the item name by item_id or null
  12398. *------------------------------------------*/
  12399. BUILDIN_FUNC(getitemname)
  12400. {
  12401. std::shared_ptr<item_data> i_data;
  12402. if( script_isstring(st, 2) ){
  12403. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12404. }else{
  12405. i_data = item_db.find( script_getnum( st, 2 ) );
  12406. }
  12407. if( i_data == nullptr ){
  12408. script_pushconststr(st,"null");
  12409. return SCRIPT_CMD_SUCCESS;
  12410. }
  12411. char* item_name = (char *)aMalloc( ITEM_NAME_LENGTH * sizeof( char ) );
  12412. memcpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12413. script_pushstr(st,item_name);
  12414. return SCRIPT_CMD_SUCCESS;
  12415. }
  12416. /*==========================================
  12417. * Returns number of slots an item has. [Skotlex]
  12418. *------------------------------------------*/
  12419. BUILDIN_FUNC(getitemslots)
  12420. {
  12421. t_itemid item_id=script_getnum(st,2);
  12422. std::shared_ptr<item_data> i_data = item_db.find(item_id);
  12423. if (i_data != nullptr)
  12424. script_pushint(st,i_data->slots);
  12425. else
  12426. script_pushint(st,-1);
  12427. return SCRIPT_CMD_SUCCESS;
  12428. }
  12429. // TODO: add matk here if needed/once we get rid of RENEWAL
  12430. /*==========================================
  12431. * Returns some values of an item [Lupus]
  12432. * Price, Weight, etc...
  12433. *------------------------------------------*/
  12434. BUILDIN_FUNC(getiteminfo)
  12435. {
  12436. std::shared_ptr<item_data> i_data;
  12437. int type = script_getnum(st, 3);
  12438. if (script_isstring(st, 2))
  12439. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12440. else
  12441. i_data = item_db.find( script_getnum( st, 2 ) );
  12442. if (i_data == nullptr) {
  12443. if (type != ITEMINFO_AEGISNAME)
  12444. script_pushint(st, -1);
  12445. else
  12446. script_pushstrcopy(st, "");
  12447. return SCRIPT_CMD_SUCCESS;
  12448. }
  12449. switch( type ) {
  12450. case ITEMINFO_BUY: script_pushint(st, i_data->value_buy); break;
  12451. case ITEMINFO_SELL: script_pushint(st, i_data->value_sell); break;
  12452. case ITEMINFO_TYPE: script_pushint(st, i_data->type); break;
  12453. case ITEMINFO_MAXCHANCE: script_pushint(st, i_data->maxchance); break;
  12454. case ITEMINFO_GENDER: script_pushint(st, i_data->sex); break;
  12455. case ITEMINFO_LOCATIONS: script_pushint(st, i_data->equip); break;
  12456. case ITEMINFO_WEIGHT: script_pushint(st, i_data->weight); break;
  12457. case ITEMINFO_ATTACK: script_pushint(st, i_data->atk); break;
  12458. case ITEMINFO_DEFENSE: script_pushint(st, i_data->def); break;
  12459. case ITEMINFO_RANGE: script_pushint(st, i_data->range); break;
  12460. case ITEMINFO_SLOT: script_pushint(st, i_data->slots); break;
  12461. case ITEMINFO_VIEW:
  12462. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12463. script_pushint(st, i_data->subtype);
  12464. } else {
  12465. script_pushint(st, i_data->look);
  12466. }
  12467. break;
  12468. case ITEMINFO_EQUIPLEVELMIN: script_pushint(st, i_data->elv); break;
  12469. case ITEMINFO_WEAPONLEVEL:
  12470. if( i_data->type == IT_WEAPON ){
  12471. script_pushint( st, i_data->weapon_level );
  12472. }else{
  12473. script_pushint( st, 0 );
  12474. }
  12475. break;
  12476. case ITEMINFO_ARMORLEVEL:
  12477. if( i_data->type == IT_ARMOR ){
  12478. script_pushint( st, i_data->armor_level );
  12479. }else{
  12480. script_pushint( st, 0 );
  12481. }
  12482. break;
  12483. case ITEMINFO_ALIASNAME: script_pushint(st, i_data->view_id); break;
  12484. case ITEMINFO_EQUIPLEVELMAX: script_pushint(st, i_data->elvmax); break;
  12485. case ITEMINFO_MAGICATTACK: {
  12486. #ifdef RENEWAL
  12487. script_pushint(st, i_data->matk);
  12488. #else
  12489. script_pushint(st, 0);
  12490. #endif
  12491. break;
  12492. }
  12493. case ITEMINFO_ID: script_pushint(st, i_data->nameid); break;
  12494. case ITEMINFO_AEGISNAME: script_pushstrcopy(st, i_data->name.c_str()); break;
  12495. case ITEMINFO_SUBTYPE: script_pushint(st, i_data->subtype); break;
  12496. default:
  12497. script_pushint(st, -1);
  12498. break;
  12499. }
  12500. return SCRIPT_CMD_SUCCESS;
  12501. }
  12502. /*==========================================
  12503. * Set some values of an item [Lupus]
  12504. * Price, Weight, etc...
  12505. *------------------------------------------*/
  12506. BUILDIN_FUNC(setiteminfo)
  12507. {
  12508. std::shared_ptr<item_data> i_data;
  12509. if (script_isstring(st, 2))
  12510. i_data = item_db.search_aegisname( script_getstr( st, 2 ) );
  12511. else
  12512. i_data = item_db.find( script_getnum( st, 2 ) );
  12513. if (i_data == nullptr) {
  12514. script_pushint(st, -1);
  12515. return SCRIPT_CMD_SUCCESS;
  12516. }
  12517. int value = script_getnum(st,4);
  12518. switch( script_getnum(st, 3) ) {
  12519. case ITEMINFO_BUY: i_data->value_buy = static_cast<uint32>(value); break;
  12520. case ITEMINFO_SELL: i_data->value_sell = static_cast<uint32>(value); break;
  12521. case ITEMINFO_TYPE: i_data->type = static_cast<item_types>(value); break;
  12522. case ITEMINFO_MAXCHANCE: i_data->maxchance = static_cast<int>(value); break;
  12523. case ITEMINFO_GENDER: i_data->sex = static_cast<uint8>(value); break;
  12524. case ITEMINFO_LOCATIONS: i_data->equip = static_cast<uint32>(value); break;
  12525. case ITEMINFO_WEIGHT: i_data->weight = static_cast<uint32>(value); break;
  12526. case ITEMINFO_ATTACK: i_data->atk = static_cast<uint32>(value); break;
  12527. case ITEMINFO_DEFENSE: i_data->def = static_cast<uint32>(value); break;
  12528. case ITEMINFO_RANGE: i_data->range = static_cast<uint16>(value); break;
  12529. case ITEMINFO_SLOT: i_data->slots = static_cast<uint16>(value); break;
  12530. case ITEMINFO_VIEW:
  12531. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12532. i_data->subtype = static_cast<uint8>(value);
  12533. } else {
  12534. i_data->look = static_cast<uint32>(value);
  12535. }
  12536. break;
  12537. case ITEMINFO_EQUIPLEVELMIN: i_data->elv = static_cast<uint16>(value); break;
  12538. case ITEMINFO_WEAPONLEVEL:
  12539. if( i_data->type == IT_WEAPON ){
  12540. if( value > MAX_WEAPON_LEVEL ){
  12541. ShowError( "buildin_setiteminfo: weapon level %d is above maximum %d.\n", value, MAX_WEAPON_LEVEL );
  12542. script_pushint( st, -1 );
  12543. return SCRIPT_CMD_FAILURE;
  12544. }
  12545. i_data->weapon_level = static_cast<uint16>(value);
  12546. break;
  12547. }else{
  12548. ShowError( "buildin_setiteminfo: Can not set a weapon level for %d.\n", i_data->nameid );
  12549. script_pushint( st, -1 );
  12550. return SCRIPT_CMD_FAILURE;
  12551. }
  12552. case ITEMINFO_ARMORLEVEL:
  12553. if( i_data->type == IT_ARMOR ){
  12554. if( value > MAX_ARMOR_LEVEL ){
  12555. ShowError( "buildin_setiteminfo: armor level %d is above maximum %d.\n", value, MAX_ARMOR_LEVEL );
  12556. script_pushint( st, -1 );
  12557. return SCRIPT_CMD_FAILURE;
  12558. }
  12559. i_data->armor_level = static_cast<uint16>(value);
  12560. break;
  12561. }else{
  12562. ShowError( "buildin_setiteminfo: Can not set an armor level for %d.\n", i_data->nameid );
  12563. script_pushint( st, -1 );
  12564. return SCRIPT_CMD_FAILURE;
  12565. }
  12566. case ITEMINFO_ALIASNAME: i_data->view_id = static_cast<t_itemid>(value); break;
  12567. case ITEMINFO_EQUIPLEVELMAX: i_data->elvmax = static_cast<uint16>(value); break;
  12568. case ITEMINFO_MAGICATTACK: {
  12569. #ifdef RENEWAL
  12570. i_data->matk = static_cast<uint32>(value);
  12571. #else
  12572. value = 0;
  12573. #endif
  12574. break;
  12575. }
  12576. case ITEMINFO_SUBTYPE: i_data->subtype = static_cast<uint8>(value); break;
  12577. default:
  12578. script_pushint(st, -1);
  12579. break;
  12580. }
  12581. script_pushint(st, value);
  12582. return SCRIPT_CMD_SUCCESS;
  12583. }
  12584. /*==========================================
  12585. * Returns value from equipped item slot n [Lupus]
  12586. getequipcardid(num,slot)
  12587. where
  12588. num = eqip position slot
  12589. slot = 0,1,2,3 (Card Slot N)
  12590. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12591. it's useful when you want to check item cards or if it's signed
  12592. Useful for such quests as "Sign this refined item with players name" etc
  12593. Hat[0] +4 -> Player's Hat[0] +4
  12594. *------------------------------------------*/
  12595. BUILDIN_FUNC(getequipcardid)
  12596. {
  12597. int i=-1,num,slot;
  12598. TBL_PC *sd;
  12599. if( !script_rid2sd(sd) )
  12600. return SCRIPT_CMD_SUCCESS;
  12601. num=script_getnum(st,2);
  12602. slot=script_getnum(st,3);
  12603. if (equip_index_check(num))
  12604. i=pc_checkequip(sd,equip_bitmask[num]);
  12605. if(i >= 0 && slot>=0 && slot<4)
  12606. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12607. else
  12608. script_pushint(st,0);
  12609. return SCRIPT_CMD_SUCCESS;
  12610. }
  12611. /*==========================================
  12612. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12613. *------------------------------------------*/
  12614. BUILDIN_FUNC(petskillbonus)
  12615. {
  12616. struct pet_data *pd;
  12617. TBL_PC *sd;
  12618. if(!script_rid2sd(sd) || sd->pd == NULL)
  12619. return SCRIPT_CMD_FAILURE;
  12620. pd = sd->pd;
  12621. if (pd->bonus)
  12622. { //Clear previous bonus
  12623. if (pd->bonus->timer != INVALID_TIMER)
  12624. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12625. } else //init
  12626. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12627. pd->bonus->type = script_getnum(st,2);
  12628. pd->bonus->val = script_getnum(st,3);
  12629. pd->bonus->duration = script_getnum(st,4);
  12630. pd->bonus->delay = script_getnum(st,5);
  12631. if (pd->state.skillbonus == 1)
  12632. pd->state.skillbonus = 0; // waiting state
  12633. // wait for timer to start
  12634. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12635. pd->bonus->timer = INVALID_TIMER;
  12636. else
  12637. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12638. return SCRIPT_CMD_SUCCESS;
  12639. }
  12640. /*==========================================
  12641. * pet looting [Valaris] //Rewritten by [Skotlex]
  12642. *------------------------------------------*/
  12643. BUILDIN_FUNC(petloot)
  12644. {
  12645. int max;
  12646. struct pet_data *pd;
  12647. TBL_PC *sd;
  12648. if(!script_rid2sd(sd) || sd->pd==NULL)
  12649. return SCRIPT_CMD_SUCCESS;
  12650. max=script_getnum(st,2);
  12651. if(max < 1)
  12652. max = 1; //Let'em loot at least 1 item.
  12653. else if (max > MAX_PETLOOT_SIZE)
  12654. max = MAX_PETLOOT_SIZE;
  12655. pd = sd->pd;
  12656. if (pd->loot != NULL)
  12657. { //Release whatever was there already and reallocate memory
  12658. pet_lootitem_drop(pd, pd->master);
  12659. aFree(pd->loot->item);
  12660. }
  12661. else
  12662. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12663. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12664. pd->loot->max=max;
  12665. pd->loot->count = 0;
  12666. pd->loot->weight = 0;
  12667. return SCRIPT_CMD_SUCCESS;
  12668. }
  12669. /*==========================================
  12670. * Set arrays with info of all sd inventory :
  12671. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12672. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12673. * @inventorylist_card(0..3), @inventorylist_expire
  12674. * @inventorylist_count = scalar
  12675. *------------------------------------------*/
  12676. BUILDIN_FUNC(getinventorylist)
  12677. {
  12678. TBL_PC *sd;
  12679. char card_var[NAME_LENGTH], randopt_var[50];
  12680. int i,j=0,k;
  12681. if (!script_charid2sd(2,sd))
  12682. return SCRIPT_CMD_FAILURE;
  12683. for(i=0;i<MAX_INVENTORY;i++){
  12684. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12685. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12686. pc_setreg(sd,reference_uid(add_str("@inventorylist_idx"), j),i);
  12687. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12688. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12689. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12690. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12691. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12692. for (k = 0; k < MAX_SLOTS; k++)
  12693. {
  12694. sprintf(card_var, "@inventorylist_card%d",k+1);
  12695. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12696. }
  12697. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12698. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12699. pc_setreg(sd,reference_uid(add_str("@inventorylist_enchantgrade"), j),sd->inventory.u.items_inventory[i].enchantgrade);
  12700. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12701. {
  12702. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12703. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12704. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12705. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12706. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12707. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12708. }
  12709. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12710. pc_setreg(sd,reference_uid(add_str("@inventorylist_favorite"), j),sd->inventory.u.items_inventory[i].favorite);
  12711. j++;
  12712. }
  12713. }
  12714. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12715. return SCRIPT_CMD_SUCCESS;
  12716. }
  12717. /**
  12718. * getskilllist ({<char_id>});
  12719. **/
  12720. BUILDIN_FUNC(getskilllist)
  12721. {
  12722. TBL_PC *sd;
  12723. int i,j=0;
  12724. if (!script_charid2sd(2,sd))
  12725. return SCRIPT_CMD_FAILURE;
  12726. for(i=0;i<MAX_SKILL;i++){
  12727. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12728. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12729. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12730. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12731. j++;
  12732. }
  12733. }
  12734. pc_setreg(sd,add_str("@skilllist_count"),j);
  12735. return SCRIPT_CMD_SUCCESS;
  12736. }
  12737. /**
  12738. * clearitem ({<char_id>});
  12739. **/
  12740. BUILDIN_FUNC(clearitem)
  12741. {
  12742. TBL_PC *sd;
  12743. int i;
  12744. if (!script_charid2sd(2,sd))
  12745. return SCRIPT_CMD_FAILURE;
  12746. for (i=0; i<MAX_INVENTORY; i++) {
  12747. if (sd->inventory.u.items_inventory[i].amount) {
  12748. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12749. }
  12750. }
  12751. return SCRIPT_CMD_SUCCESS;
  12752. }
  12753. /**
  12754. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12755. * disguise <Monster ID>{,<char_id>};
  12756. **/
  12757. BUILDIN_FUNC(disguise)
  12758. {
  12759. int id;
  12760. TBL_PC* sd;
  12761. if (!script_charid2sd(3,sd))
  12762. return SCRIPT_CMD_FAILURE;
  12763. id = script_getnum(st,2);
  12764. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12765. pc_disguise(sd, id);
  12766. script_pushint(st,id);
  12767. } else
  12768. script_pushint(st,0);
  12769. return SCRIPT_CMD_SUCCESS;
  12770. }
  12771. /**
  12772. * Undisguise Player (returns 1 if success, 0 on fail)
  12773. * undisguise {<char_id>};
  12774. **/
  12775. BUILDIN_FUNC(undisguise)
  12776. {
  12777. TBL_PC* sd;
  12778. if (!script_charid2sd(2,sd))
  12779. return SCRIPT_CMD_FAILURE;
  12780. if (sd->disguise) {
  12781. pc_disguise(sd, 0);
  12782. script_pushint(st,0);
  12783. } else {
  12784. script_pushint(st,1);
  12785. }
  12786. return SCRIPT_CMD_SUCCESS;
  12787. }
  12788. /**
  12789. * Transform an NPC to another _class
  12790. *
  12791. * classchange(<view id>{,"<NPC name>","<flag>"});
  12792. * @param flag: Specify target
  12793. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12794. * BC_SELF - Sprite is sent only to player attached.
  12795. */
  12796. BUILDIN_FUNC(classchange)
  12797. {
  12798. int _class, type = 1;
  12799. struct npc_data* nd = NULL;
  12800. TBL_PC *sd = map_id2sd(st->rid);
  12801. send_target target = AREA;
  12802. _class = script_getnum(st,2);
  12803. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12804. nd = npc_name2id(script_getstr(st, 3));
  12805. else
  12806. nd = (struct npc_data *)map_id2bl(st->oid);
  12807. if (nd == NULL)
  12808. return SCRIPT_CMD_FAILURE;
  12809. if (script_hasdata(st, 4)) {
  12810. switch(script_getnum(st, 4)) {
  12811. case BC_SELF: target = SELF; break;
  12812. case BC_AREA:
  12813. default: target = AREA; break;
  12814. }
  12815. }
  12816. if (target != SELF)
  12817. clif_class_change(&nd->bl,_class,type);
  12818. else if (sd == NULL)
  12819. return SCRIPT_CMD_FAILURE;
  12820. else
  12821. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12822. return SCRIPT_CMD_SUCCESS;
  12823. }
  12824. /*==========================================
  12825. * Display an effect
  12826. *------------------------------------------*/
  12827. BUILDIN_FUNC(misceffect)
  12828. {
  12829. int type;
  12830. type=script_getnum(st,2);
  12831. if( type <= EF_NONE || type >= EF_MAX ){
  12832. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12833. return SCRIPT_CMD_FAILURE;
  12834. }
  12835. if(st->oid && st->oid != fake_nd->bl.id) {
  12836. struct block_list *bl = map_id2bl(st->oid);
  12837. if (bl)
  12838. clif_specialeffect(bl,type,AREA);
  12839. } else{
  12840. TBL_PC *sd;
  12841. if(script_rid2sd(sd))
  12842. clif_specialeffect(&sd->bl,type,AREA);
  12843. }
  12844. return SCRIPT_CMD_SUCCESS;
  12845. }
  12846. /*==========================================
  12847. * Play a BGM on a single client [Rikter/Yommy]
  12848. *------------------------------------------*/
  12849. BUILDIN_FUNC(playBGM)
  12850. {
  12851. struct map_session_data* sd;
  12852. if( script_rid2sd(sd) ) {
  12853. clif_playBGM( *sd, script_getstr( st, 2 ) );
  12854. }
  12855. return SCRIPT_CMD_SUCCESS;
  12856. }
  12857. static int playBGM_sub(struct block_list* bl,va_list ap)
  12858. {
  12859. const char* name = va_arg(ap,const char*);
  12860. clif_playBGM( *BL_CAST( BL_PC, bl ), name );
  12861. return 0;
  12862. }
  12863. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12864. {
  12865. const char* name = va_arg(args, const char*);
  12866. clif_playBGM( *sd, name );
  12867. return 0;
  12868. }
  12869. /*==========================================
  12870. * Play a BGM on multiple client [Rikter/Yommy]
  12871. *------------------------------------------*/
  12872. BUILDIN_FUNC(playBGMall)
  12873. {
  12874. const char* name;
  12875. name = script_getstr(st,2);
  12876. if( script_hasdata(st,7) ) {// specified part of map
  12877. const char* mapname = script_getstr(st,3);
  12878. int x0 = script_getnum(st,4);
  12879. int y0 = script_getnum(st,5);
  12880. int x1 = script_getnum(st,6);
  12881. int y1 = script_getnum(st,7);
  12882. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12883. }
  12884. else if( script_hasdata(st,3) ) {// entire map
  12885. const char* mapname = script_getstr(st,3);
  12886. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12887. }
  12888. else {// entire server
  12889. map_foreachpc(&playBGM_foreachpc_sub, name);
  12890. }
  12891. return SCRIPT_CMD_SUCCESS;
  12892. }
  12893. /*==========================================
  12894. * Play a .wav sound for sd
  12895. *------------------------------------------*/
  12896. BUILDIN_FUNC(soundeffect)
  12897. {
  12898. struct map_session_data* sd;
  12899. if(script_rid2sd(sd)){
  12900. const char* name = script_getstr(st,2);
  12901. int type = script_getnum(st,3);
  12902. clif_soundeffect( sd->bl, name, type, SELF );
  12903. }
  12904. return SCRIPT_CMD_SUCCESS;
  12905. }
  12906. int soundeffect_sub(struct block_list* bl,va_list ap)
  12907. {
  12908. char* name = va_arg(ap,char*);
  12909. int type = va_arg(ap,int);
  12910. clif_soundeffect( *bl, name, type, SELF );
  12911. return 0;
  12912. }
  12913. /*==========================================
  12914. * Play a sound effect (.wav) on multiple clients
  12915. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12916. *------------------------------------------*/
  12917. BUILDIN_FUNC(soundeffectall)
  12918. {
  12919. struct block_list* bl;
  12920. struct map_session_data* sd;
  12921. const char* name;
  12922. int type;
  12923. if( st->rid && script_rid2sd(sd) )
  12924. bl = &sd->bl;
  12925. else
  12926. bl = map_id2bl(st->oid);
  12927. if (!bl)
  12928. return SCRIPT_CMD_SUCCESS;
  12929. name = script_getstr(st,2);
  12930. type = script_getnum(st,3);
  12931. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12932. if(!script_hasdata(st,4))
  12933. { // area around
  12934. clif_soundeffect( *bl, name, type, AREA );
  12935. }
  12936. else
  12937. if(!script_hasdata(st,5))
  12938. { // entire map
  12939. const char* mapname = script_getstr(st,4);
  12940. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12941. }
  12942. else
  12943. if(script_hasdata(st,8))
  12944. { // specified part of map
  12945. const char* mapname = script_getstr(st,4);
  12946. int x0 = script_getnum(st,5);
  12947. int y0 = script_getnum(st,6);
  12948. int x1 = script_getnum(st,7);
  12949. int y1 = script_getnum(st,8);
  12950. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12951. }
  12952. else
  12953. {
  12954. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12955. }
  12956. return SCRIPT_CMD_SUCCESS;
  12957. }
  12958. /*==========================================
  12959. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12960. *------------------------------------------*/
  12961. BUILDIN_FUNC(petrecovery)
  12962. {
  12963. struct pet_data *pd;
  12964. TBL_PC *sd;
  12965. int sc;
  12966. if(!script_rid2sd(sd) || sd->pd == NULL)
  12967. return SCRIPT_CMD_FAILURE;
  12968. sc = script_getnum(st,2);
  12969. if (sc <= SC_NONE || sc >= SC_MAX) {
  12970. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12971. return SCRIPT_CMD_FAILURE;
  12972. }
  12973. pd = sd->pd;
  12974. if (pd->recovery)
  12975. { //Halt previous bonus
  12976. if (pd->recovery->timer != INVALID_TIMER)
  12977. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12978. } else //Init
  12979. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12980. pd->recovery->type = (sc_type)sc;
  12981. pd->recovery->delay = script_getnum(st,3);
  12982. pd->recovery->timer = INVALID_TIMER;
  12983. return SCRIPT_CMD_SUCCESS;
  12984. }
  12985. /*==========================================
  12986. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12987. *------------------------------------------*/
  12988. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12989. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12990. BUILDIN_FUNC(petskillattack)
  12991. {
  12992. struct pet_data *pd;
  12993. TBL_PC *sd;
  12994. int id = 0;
  12995. if(!script_rid2sd(sd) || sd->pd == NULL)
  12996. return SCRIPT_CMD_FAILURE;
  12997. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12998. if (!id) {
  12999. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  13000. return SCRIPT_CMD_FAILURE;
  13001. }
  13002. pd = sd->pd;
  13003. if (pd->a_skill == NULL)
  13004. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  13005. pd->a_skill->id = id;
  13006. pd->a_skill->damage = 0;
  13007. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  13008. pd->a_skill->div_ = 0;
  13009. pd->a_skill->rate = script_getnum(st,4);
  13010. pd->a_skill->bonusrate = script_getnum(st,5);
  13011. return SCRIPT_CMD_SUCCESS;
  13012. }
  13013. /*==========================================
  13014. * pet attack skills [Valaris]
  13015. *------------------------------------------*/
  13016. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  13017. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  13018. BUILDIN_FUNC(petskillattack2)
  13019. {
  13020. struct pet_data *pd;
  13021. TBL_PC *sd;
  13022. int id = 0;
  13023. if(!script_rid2sd(sd) || sd->pd == NULL)
  13024. return SCRIPT_CMD_FAILURE;
  13025. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13026. if (!id) {
  13027. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  13028. return SCRIPT_CMD_FAILURE;
  13029. }
  13030. pd = sd->pd;
  13031. if (pd->a_skill == NULL)
  13032. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  13033. pd->a_skill->id = id;
  13034. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  13035. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  13036. pd->a_skill->div_ = script_getnum(st,4);
  13037. pd->a_skill->rate = script_getnum(st,5);
  13038. pd->a_skill->bonusrate = script_getnum(st,6);
  13039. return SCRIPT_CMD_SUCCESS;
  13040. }
  13041. /*==========================================
  13042. * pet support skills [Skotlex]
  13043. *------------------------------------------*/
  13044. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  13045. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  13046. BUILDIN_FUNC(petskillsupport)
  13047. {
  13048. struct pet_data *pd;
  13049. TBL_PC *sd;
  13050. int id = 0;
  13051. if(!script_rid2sd(sd) || sd->pd == NULL)
  13052. return SCRIPT_CMD_FAILURE;
  13053. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  13054. if (!id) {
  13055. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  13056. return SCRIPT_CMD_FAILURE;
  13057. }
  13058. pd = sd->pd;
  13059. if (pd->s_skill)
  13060. { //Clear previous skill
  13061. if (pd->s_skill->timer != INVALID_TIMER)
  13062. {
  13063. if (pd->s_skill->id)
  13064. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  13065. else
  13066. delete_timer(pd->s_skill->timer, pet_heal_timer);
  13067. }
  13068. } else //init memory
  13069. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  13070. pd->s_skill->id = id;
  13071. pd->s_skill->lv = script_getnum(st,3);
  13072. pd->s_skill->delay = script_getnum(st,4);
  13073. pd->s_skill->hp = script_getnum(st,5);
  13074. pd->s_skill->sp = script_getnum(st,6);
  13075. //Use delay as initial offset to avoid skill/heal exploits
  13076. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  13077. pd->s_skill->timer = INVALID_TIMER;
  13078. else
  13079. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  13080. return SCRIPT_CMD_SUCCESS;
  13081. }
  13082. static inline void script_skill_effect(block_list *bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y) {
  13083. nullpo_retv(bl);
  13084. switch (skill_get_casttype(skill_id)) {
  13085. case CAST_GROUND:
  13086. clif_skill_poseffect(bl, skill_id, skill_lv, x, y, gettick());
  13087. break;
  13088. case CAST_NODAMAGE:
  13089. clif_skill_nodamage(bl, bl, skill_id, skill_lv, 1);
  13090. break;
  13091. case CAST_DAMAGE:
  13092. 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));
  13093. break;
  13094. }
  13095. }
  13096. /*==========================================
  13097. * Scripted skill effects [Celest]
  13098. *------------------------------------------*/
  13099. /// skilleffect <skill id>,<level>
  13100. /// skilleffect "<skill name>",<level>
  13101. BUILDIN_FUNC(skilleffect)
  13102. {
  13103. TBL_PC *sd;
  13104. uint16 skill_id, skill_lv;
  13105. if( !script_rid2sd(sd) )
  13106. return SCRIPT_CMD_FAILURE;
  13107. skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  13108. skill_lv = script_getnum(st,3);
  13109. if (skill_db.find(skill_id) == nullptr) {
  13110. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  13111. return SCRIPT_CMD_FAILURE;
  13112. }
  13113. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  13114. * which leaves the server thinking it still is sitting. */
  13115. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  13116. skill_sit(sd, 0);
  13117. clif_standing(&sd->bl);
  13118. }
  13119. script_skill_effect(&sd->bl, skill_id, skill_lv, sd->bl.x, sd->bl.y);
  13120. return SCRIPT_CMD_SUCCESS;
  13121. }
  13122. /*==========================================
  13123. * NPC skill effects [Valaris]
  13124. *------------------------------------------*/
  13125. /// npcskilleffect <skill id>,<level>,<x>,<y>
  13126. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  13127. BUILDIN_FUNC(npcskilleffect)
  13128. {
  13129. struct block_list *bl= map_id2bl(st->oid);
  13130. if (bl == nullptr) {
  13131. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  13132. return SCRIPT_CMD_FAILURE;
  13133. }
  13134. uint16 skill_id, skill_lv;
  13135. int16 x, y;
  13136. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  13137. skill_lv=script_getnum(st,3);
  13138. x=script_getnum(st,4);
  13139. y=script_getnum(st,5);
  13140. if (skill_db.find(skill_id) == nullptr) {
  13141. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  13142. return SCRIPT_CMD_FAILURE;
  13143. }
  13144. script_skill_effect(bl, skill_id, skill_lv, bl->x, bl->y);
  13145. return SCRIPT_CMD_SUCCESS;
  13146. }
  13147. /*==========================================
  13148. * Special effects [Valaris]
  13149. *------------------------------------------*/
  13150. BUILDIN_FUNC(specialeffect)
  13151. {
  13152. struct block_list *bl=map_id2bl(st->oid);
  13153. int type = script_getnum(st,2);
  13154. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  13155. if(bl==NULL)
  13156. return SCRIPT_CMD_SUCCESS;
  13157. if( type <= EF_NONE || type >= EF_MAX ){
  13158. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  13159. return SCRIPT_CMD_FAILURE;
  13160. }
  13161. if( script_hasdata(st,4) )
  13162. {
  13163. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  13164. if(nd)
  13165. clif_specialeffect(&nd->bl, type, target);
  13166. }
  13167. else
  13168. {
  13169. if (target == SELF) {
  13170. TBL_PC *sd;
  13171. if (script_rid2sd(sd))
  13172. clif_specialeffect_single(bl,type,sd->fd);
  13173. } else {
  13174. clif_specialeffect(bl, type, target);
  13175. }
  13176. }
  13177. return SCRIPT_CMD_SUCCESS;
  13178. }
  13179. BUILDIN_FUNC(specialeffect2)
  13180. {
  13181. TBL_PC *sd;
  13182. if( script_nick2sd(4,sd) ){
  13183. int type = script_getnum(st,2);
  13184. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  13185. if( type <= EF_NONE || type >= EF_MAX ){
  13186. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  13187. return SCRIPT_CMD_FAILURE;
  13188. }
  13189. clif_specialeffect(&sd->bl, type, target);
  13190. }
  13191. return SCRIPT_CMD_SUCCESS;
  13192. }
  13193. BUILDIN_FUNC(removespecialeffect)
  13194. {
  13195. const char* command = script_getfuncname(st);
  13196. #if PACKETVER < 20181002
  13197. ShowError("buildin_%s: This command is not supported for PACKETVER older than 2018-10-02.\n", command);
  13198. return SCRIPT_CMD_FAILURE;
  13199. #endif
  13200. int effect = script_getnum(st, 2);
  13201. if (effect <= EF_NONE || effect >= EF_MAX) {
  13202. ShowError("buildin_%s: unsupported effect id %d.\n", command, effect);
  13203. return SCRIPT_CMD_FAILURE;
  13204. }
  13205. send_target e_target = script_hasdata(st, 3) ? static_cast<send_target>(script_getnum(st, 3)) : AREA;
  13206. struct block_list *bl_src;
  13207. struct block_list *bl_target;
  13208. struct map_session_data *sd;
  13209. if( strcmp(command, "removespecialeffect") == 0 ) {
  13210. if (!script_hasdata(st, 4)) {
  13211. bl_src = map_id2bl(st->oid);
  13212. if (bl_src == nullptr) {
  13213. ShowError("buildin_%s: npc not attached.\n", command);
  13214. return SCRIPT_CMD_FAILURE;
  13215. }
  13216. }
  13217. else {
  13218. struct npc_data *nd = npc_name2id(script_getstr(st, 4));
  13219. if (nd == nullptr) {
  13220. ShowError("buildin_%s: unknown npc %s.\n", command, script_getstr(st, 4));
  13221. return SCRIPT_CMD_FAILURE;
  13222. }
  13223. bl_src = &nd->bl;
  13224. }
  13225. if (e_target != SELF)
  13226. bl_target = bl_src;
  13227. else {
  13228. if (!script_rid2sd(sd))
  13229. return SCRIPT_CMD_FAILURE;
  13230. bl_target = &sd->bl;
  13231. }
  13232. }else{
  13233. if (!script_nick2sd(4, sd))
  13234. return SCRIPT_CMD_FAILURE;
  13235. bl_src = bl_target = &sd->bl;
  13236. }
  13237. clif_specialeffect_remove(bl_src, effect, e_target, bl_target);
  13238. return SCRIPT_CMD_SUCCESS;
  13239. }
  13240. /**
  13241. * nude({<char_id>});
  13242. * @author [Valaris]
  13243. **/
  13244. BUILDIN_FUNC(nude)
  13245. {
  13246. TBL_PC *sd;
  13247. int i, calcflag = 0;
  13248. if (!script_charid2sd(2,sd))
  13249. return SCRIPT_CMD_FAILURE;
  13250. for( i = 0 ; i < EQI_MAX; i++ ) {
  13251. if( sd->equip_index[ i ] >= 0 ) {
  13252. if( !calcflag )
  13253. calcflag = 1;
  13254. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  13255. }
  13256. }
  13257. if( calcflag )
  13258. status_calc_pc(sd,SCO_NONE);
  13259. return SCRIPT_CMD_SUCCESS;
  13260. }
  13261. int atcommand_sub(struct script_state* st,int type) {
  13262. TBL_PC *sd, dummy_sd;
  13263. int fd;
  13264. const char *cmd;
  13265. cmd = script_getstr(st,2);
  13266. if (st->rid) {
  13267. if( !script_rid2sd(sd) )
  13268. return SCRIPT_CMD_SUCCESS;
  13269. fd = sd->fd;
  13270. } else { //Use a dummy character.
  13271. sd = &dummy_sd;
  13272. fd = 0;
  13273. memset(&dummy_sd, 0, sizeof(TBL_PC));
  13274. if (st->oid) {
  13275. struct block_list* bl = map_id2bl(st->oid);
  13276. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  13277. if (bl->type == BL_NPC)
  13278. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  13279. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  13280. }
  13281. // Init Group ID, Level, & permissions
  13282. sd->group_id = 99;
  13283. pc_group_pc_load( sd );
  13284. }
  13285. if (!is_atcommand(fd, sd, cmd, type)) {
  13286. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  13287. script_reportsrc(st);
  13288. return SCRIPT_CMD_FAILURE;
  13289. }
  13290. return SCRIPT_CMD_SUCCESS;
  13291. }
  13292. /*==========================================
  13293. * gmcommand [MouseJstr]
  13294. *------------------------------------------*/
  13295. BUILDIN_FUNC(atcommand) {
  13296. return atcommand_sub(st,0);
  13297. }
  13298. /** Displays a message for the player only (like system messages like "you got an apple" )
  13299. * dispbottom("<message>"{,<color>{,<char_id>}})
  13300. * @param message
  13301. * @param color Hex color default (Green)
  13302. */
  13303. BUILDIN_FUNC(dispbottom)
  13304. {
  13305. TBL_PC *sd;
  13306. int color = 0;
  13307. const char *message;
  13308. if (!script_charid2sd(4,sd))
  13309. return SCRIPT_CMD_FAILURE;
  13310. message = script_getstr(st,2);
  13311. if (script_hasdata(st,3))
  13312. color = script_getnum(st,3); // <color>
  13313. if(sd) {
  13314. if (script_hasdata(st,3))
  13315. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  13316. else
  13317. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  13318. }
  13319. return SCRIPT_CMD_SUCCESS;
  13320. }
  13321. /*===================================
  13322. * Heal portion of recovery command
  13323. *-----------------------------------*/
  13324. int recovery_sub(struct map_session_data* sd, int revive)
  13325. {
  13326. if(revive&(1|4) && pc_isdead(sd)) {
  13327. status_revive(&sd->bl, 100, 100);
  13328. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  13329. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  13330. } else if(revive&(1|2) && !pc_isdead(sd)) {
  13331. status_percent_heal(&sd->bl, 100, 100);
  13332. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  13333. }
  13334. return SCRIPT_CMD_SUCCESS;
  13335. }
  13336. /*=========================================================================
  13337. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  13338. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  13339. * <type> determines <option>:
  13340. * 0 : char_id
  13341. * 1 : party_id
  13342. * 2 : guild_id
  13343. * 3 : map_name
  13344. * 4 : all characters
  13345. * <revive_flag>:
  13346. * 1 : Revive and heal all players (default)
  13347. * 2 : Heal living players only
  13348. * 4 : Revive dead players only
  13349. * <map name>:
  13350. * for types 1-2 : map_name (null = all maps)
  13351. *-------------------------------------------------------------------------*/
  13352. BUILDIN_FUNC(recovery)
  13353. {
  13354. TBL_PC *sd;
  13355. int map_idx = 0, type = 0, revive = 1;
  13356. type = script_getnum(st,2);
  13357. if(script_hasdata(st,4))
  13358. revive = script_getnum(st,4);
  13359. switch(type) {
  13360. case 0:
  13361. if(!script_charid2sd(3,sd))
  13362. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  13363. recovery_sub(sd, revive);
  13364. break;
  13365. case 1:
  13366. {
  13367. struct party_data* p;
  13368. //When no party given, we use invoker party
  13369. int p_id = 0, i;
  13370. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13371. map_idx = map_mapname2mapid(script_getstr(st,5));
  13372. if(map_idx < 1) { //But we'll check anyways
  13373. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13374. return SCRIPT_CMD_FAILURE;
  13375. }
  13376. }
  13377. if(script_hasdata(st,3))
  13378. p_id = script_getnum(st,3);
  13379. else if(script_rid2sd(sd))
  13380. p_id = sd->status.party_id;
  13381. p = party_search(p_id);
  13382. if(p == NULL)
  13383. return SCRIPT_CMD_SUCCESS;
  13384. for (i = 0; i < MAX_PARTY; i++) {
  13385. struct map_session_data* pl_sd;
  13386. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  13387. || (map_idx && pl_sd->bl.m != map_idx))
  13388. continue;
  13389. recovery_sub(pl_sd, revive);
  13390. }
  13391. break;
  13392. }
  13393. case 2:
  13394. {
  13395. struct guild* g;
  13396. //When no guild given, we use invoker guild
  13397. int g_id = 0, i;
  13398. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13399. map_idx = map_mapname2mapid(script_getstr(st,5));
  13400. if(map_idx < 1) { //But we'll check anyways
  13401. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13402. return SCRIPT_CMD_FAILURE;
  13403. }
  13404. }
  13405. if(script_hasdata(st,3))
  13406. g_id = script_getnum(st,3);
  13407. else if(script_rid2sd(sd))
  13408. g_id = sd->status.guild_id;
  13409. g = guild_search(g_id);
  13410. if(g == NULL)
  13411. return SCRIPT_CMD_SUCCESS;
  13412. for (i = 0; i < MAX_GUILD; i++) {
  13413. struct map_session_data* pl_sd;
  13414. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  13415. || (map_idx && pl_sd->bl.m != map_idx))
  13416. continue;
  13417. recovery_sub(pl_sd, revive);
  13418. }
  13419. break;
  13420. }
  13421. case 3:
  13422. if(script_hasdata(st,3))
  13423. map_idx = map_mapname2mapid(script_getstr(st,3));
  13424. else if(script_rid2sd(sd))
  13425. map_idx = sd->bl.m;
  13426. if(map_idx < 1)
  13427. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  13428. case 4:
  13429. {
  13430. struct s_mapiterator *iter;
  13431. if(script_hasdata(st,3) && script_isint(st, 3))
  13432. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  13433. iter = mapit_getallusers();
  13434. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  13435. if(type == 3 && sd->bl.m != map_idx)
  13436. continue;
  13437. recovery_sub(sd, revive);
  13438. }
  13439. mapit_free(iter);
  13440. break;
  13441. }
  13442. default:
  13443. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  13444. script_pushint(st,-1);
  13445. return SCRIPT_CMD_FAILURE;
  13446. }
  13447. script_pushint(st,1); //Successfully executed without errors
  13448. return SCRIPT_CMD_SUCCESS;
  13449. }
  13450. /*==========================================
  13451. * Get your pet info: getpetinfo <type>{,<char_id>}
  13452. * n -> 0:pet_id 1:pet_class 2:pet_name
  13453. * 3:friendly 4:hungry, 5: rename flag.6:level,
  13454. * 7:game id
  13455. *------------------------------------------*/
  13456. BUILDIN_FUNC(getpetinfo)
  13457. {
  13458. TBL_PC *sd;
  13459. TBL_PET *pd;
  13460. int type = script_getnum(st,2);
  13461. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  13462. if (type == 2)
  13463. script_pushconststr(st,"null");
  13464. else
  13465. script_pushint(st,0);
  13466. return SCRIPT_CMD_SUCCESS;
  13467. }
  13468. switch(type){
  13469. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13470. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13471. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13472. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13473. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13474. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13475. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13476. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13477. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13478. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13479. default:
  13480. script_pushint(st,0);
  13481. break;
  13482. }
  13483. return SCRIPT_CMD_SUCCESS;
  13484. }
  13485. /*==========================================
  13486. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13487. * type -> 0:hom_id 1:class 2:name
  13488. * 3:friendly 4:hungry, 5: rename flag.
  13489. * 6: level, 7: game id
  13490. *------------------------------------------*/
  13491. BUILDIN_FUNC(gethominfo)
  13492. {
  13493. TBL_PC *sd;
  13494. TBL_HOM *hd;
  13495. int type=script_getnum(st,2);
  13496. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13497. if (type == 2)
  13498. script_pushconststr(st,"null");
  13499. else
  13500. script_pushint(st,0);
  13501. return SCRIPT_CMD_SUCCESS;
  13502. }
  13503. switch(type){
  13504. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13505. case 1: script_pushint(st,hd->homunculus.class_); break;
  13506. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13507. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13508. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13509. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13510. case 6: script_pushint(st,hd->homunculus.level); break;
  13511. case 7: script_pushint(st,hd->bl.id); break;
  13512. default:
  13513. script_pushint(st,0);
  13514. break;
  13515. }
  13516. return SCRIPT_CMD_SUCCESS;
  13517. }
  13518. BUILDIN_FUNC(addhomintimacy)
  13519. {
  13520. map_session_data *sd;
  13521. homun_data *hd;
  13522. if (!script_charid2sd(3, sd) || !(hd = sd->hd))
  13523. return SCRIPT_CMD_FAILURE;
  13524. int32 value = script_getnum(st, 2);
  13525. if (value == 0) // Nothing to change
  13526. return SCRIPT_CMD_SUCCESS;
  13527. if (value > 0)
  13528. hom_increase_intimacy(hd, (uint32)value);
  13529. else
  13530. hom_decrease_intimacy(hd, (uint32)abs(value));
  13531. clif_send_homdata(sd, SP_INTIMATE, hd->homunculus.intimacy / 100);
  13532. return SCRIPT_CMD_SUCCESS;
  13533. }
  13534. /// Retrieves information about character's mercenary
  13535. /// getmercinfo <type>[,<char id>];
  13536. BUILDIN_FUNC(getmercinfo)
  13537. {
  13538. int type;
  13539. struct map_session_data* sd;
  13540. s_mercenary_data* md;
  13541. if( !script_charid2sd(3,sd) ){
  13542. script_pushnil(st);
  13543. return SCRIPT_CMD_FAILURE;
  13544. }
  13545. type = script_getnum(st,2);
  13546. md = sd->md;
  13547. if( md == NULL ){
  13548. if( type == 2 )
  13549. script_pushconststr(st,"");
  13550. else
  13551. script_pushint(st,0);
  13552. return SCRIPT_CMD_SUCCESS;
  13553. }
  13554. switch( type )
  13555. {
  13556. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13557. case 1: script_pushint(st,md->mercenary.class_); break;
  13558. case 2: script_pushstrcopy(st,md->db->name.c_str()); break;
  13559. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13560. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13561. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13562. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13563. case 7: script_pushint(st,md->db->lv); break;
  13564. case 8: script_pushint(st,md->bl.id); break;
  13565. default:
  13566. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13567. script_pushnil(st);
  13568. return SCRIPT_CMD_FAILURE;
  13569. }
  13570. return SCRIPT_CMD_SUCCESS;
  13571. }
  13572. /*==========================================
  13573. * Shows wether your inventory(and equips) contain
  13574. selected card or not.
  13575. checkequipedcard(4001);
  13576. *------------------------------------------*/
  13577. BUILDIN_FUNC(checkequipedcard)
  13578. {
  13579. TBL_PC *sd;
  13580. if(script_rid2sd(sd)){
  13581. int n,i,c=0;
  13582. c=script_getnum(st,2);
  13583. for(i=0;i<MAX_INVENTORY;i++){
  13584. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13585. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13586. continue;
  13587. for (n=0; n < MAX_SLOTS; n++) {
  13588. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13589. script_pushint(st,1);
  13590. return SCRIPT_CMD_SUCCESS;
  13591. }
  13592. }
  13593. }
  13594. }
  13595. }
  13596. script_pushint(st,0);
  13597. return SCRIPT_CMD_SUCCESS;
  13598. }
  13599. BUILDIN_FUNC(jump_zero)
  13600. {
  13601. int64 sel=script_getnum64(st,2);
  13602. if(!sel) {
  13603. int pos;
  13604. if( !data_islabel(script_getdata(st,3)) ){
  13605. ShowError("script: jump_zero: not label !\n");
  13606. st->state=END;
  13607. return SCRIPT_CMD_FAILURE;
  13608. }
  13609. pos=script_getnum(st,3);
  13610. st->pos=pos;
  13611. st->state=GOTO;
  13612. }
  13613. return SCRIPT_CMD_SUCCESS;
  13614. }
  13615. /*==========================================
  13616. * movenpc [MouseJstr]
  13617. *------------------------------------------*/
  13618. BUILDIN_FUNC(movenpc)
  13619. {
  13620. TBL_NPC *nd = NULL;
  13621. const char *npc;
  13622. int x,y;
  13623. npc = script_getstr(st,2);
  13624. x = script_getnum(st,3);
  13625. y = script_getnum(st,4);
  13626. if ((nd = npc_name2id(npc)) == NULL){
  13627. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13628. return -1;
  13629. }
  13630. if (script_hasdata(st,5))
  13631. nd->ud.dir = script_getnum(st,5) % 8;
  13632. npc_movenpc(nd, x, y);
  13633. return SCRIPT_CMD_SUCCESS;
  13634. }
  13635. /*==========================================
  13636. * message [MouseJstr]
  13637. *------------------------------------------*/
  13638. BUILDIN_FUNC(message)
  13639. {
  13640. const char *msg,*player;
  13641. TBL_PC *pl_sd = NULL;
  13642. player = script_getstr(st,2);
  13643. msg = script_getstr(st,3);
  13644. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13645. return SCRIPT_CMD_SUCCESS;
  13646. clif_displaymessage(pl_sd->fd, msg);
  13647. return SCRIPT_CMD_SUCCESS;
  13648. }
  13649. /**
  13650. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13651. * @param flag: Specify target
  13652. * BC_ALL - Broadcast message is sent server-wide.
  13653. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13654. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13655. * BC_SELF - Message is sent only to player attached.
  13656. */
  13657. BUILDIN_FUNC(npctalk)
  13658. {
  13659. struct npc_data* nd = NULL;
  13660. const char* str = script_getstr(st,2);
  13661. int color = 0xFFFFFF;
  13662. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13663. nd = npc_name2id(script_getstr(st, 3));
  13664. else
  13665. nd = (struct npc_data *)map_id2bl(st->oid);
  13666. if (script_hasdata(st, 5))
  13667. color = script_getnum(st, 5);
  13668. if (nd != NULL) {
  13669. send_target target = AREA;
  13670. char message[CHAT_SIZE_MAX];
  13671. if (script_hasdata(st, 4)) {
  13672. switch(script_getnum(st, 4)) {
  13673. case BC_ALL: target = ALL_CLIENT; break;
  13674. case BC_MAP: target = ALL_SAMEMAP; break;
  13675. case BC_SELF: target = SELF; break;
  13676. case BC_AREA:
  13677. default: target = AREA; break;
  13678. }
  13679. }
  13680. safesnprintf(message, sizeof(message), "%s", str);
  13681. if (target != SELF)
  13682. clif_messagecolor(&nd->bl, color, message, true, target);
  13683. else {
  13684. TBL_PC *sd = map_id2sd(st->rid);
  13685. if (sd == NULL)
  13686. return SCRIPT_CMD_FAILURE;
  13687. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13688. }
  13689. }
  13690. return SCRIPT_CMD_SUCCESS;
  13691. }
  13692. /**
  13693. * Sends a message to the waitingroom of the invoking NPC.
  13694. * chatmes "<message>"{,"<NPC name>"};
  13695. * @author Jey
  13696. */
  13697. BUILDIN_FUNC(chatmes)
  13698. {
  13699. struct npc_data* nd = NULL;
  13700. const char* str = script_getstr(st,2);
  13701. if (script_hasdata(st, 3))
  13702. nd = npc_name2id(script_getstr(st, 3));
  13703. else
  13704. nd = (struct npc_data *)map_id2bl(st->oid);
  13705. if (nd != NULL && nd->chat_id) {
  13706. char message[256];
  13707. safesnprintf(message, sizeof(message), "%s", str);
  13708. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13709. }
  13710. return SCRIPT_CMD_SUCCESS;
  13711. }
  13712. // change npc walkspeed [Valaris]
  13713. BUILDIN_FUNC(npcspeed)
  13714. {
  13715. struct npc_data* nd;
  13716. int speed;
  13717. speed = script_getnum(st,2);
  13718. nd =(struct npc_data *)map_id2bl(st->oid);
  13719. if( nd ) {
  13720. nd->speed = speed;
  13721. nd->ud.state.speed_changed = 1;
  13722. }
  13723. return SCRIPT_CMD_SUCCESS;
  13724. }
  13725. // make an npc walk to a position [Valaris]
  13726. BUILDIN_FUNC(npcwalkto)
  13727. {
  13728. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13729. int x=0,y=0;
  13730. x=script_getnum(st,2);
  13731. y=script_getnum(st,3);
  13732. if(nd) {
  13733. if (!nd->status.hp)
  13734. status_calc_npc(nd, SCO_FIRST);
  13735. else
  13736. status_calc_npc(nd, SCO_NONE);
  13737. unit_walktoxy(&nd->bl,x,y,0);
  13738. }
  13739. return SCRIPT_CMD_SUCCESS;
  13740. }
  13741. // stop an npc's movement [Valaris]
  13742. BUILDIN_FUNC(npcstop)
  13743. {
  13744. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13745. if(nd) {
  13746. unit_stop_walking(&nd->bl,1|4);
  13747. }
  13748. return SCRIPT_CMD_SUCCESS;
  13749. }
  13750. /**
  13751. * getlook(<type>{,<char_id>})
  13752. **/
  13753. BUILDIN_FUNC(getlook)
  13754. {
  13755. int type,val;
  13756. TBL_PC *sd;
  13757. if (!script_charid2sd(3,sd)) {
  13758. script_pushint(st,-1);
  13759. return SCRIPT_CMD_FAILURE;
  13760. }
  13761. type=script_getnum(st,2);
  13762. val=-1;
  13763. switch(type) {
  13764. // TODO: implement LOOK_BASE as stated in script doc
  13765. case LOOK_HAIR: val=sd->status.hair; break; //1
  13766. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13767. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13768. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13769. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13770. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13771. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13772. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13773. case LOOK_SHOES: break; //9
  13774. case LOOK_ROBE: val=sd->status.robe; break; //12
  13775. case LOOK_BODY2: val=sd->status.body; break; //13
  13776. }
  13777. script_pushint(st,val);
  13778. return SCRIPT_CMD_SUCCESS;
  13779. }
  13780. /**
  13781. * getsavepoint(<information type>{,<char_id>})
  13782. * @param type 0- map name, 1- x, 2- y
  13783. **/
  13784. BUILDIN_FUNC(getsavepoint)
  13785. {
  13786. TBL_PC* sd;
  13787. int type;
  13788. if (!script_charid2sd(3,sd)) {
  13789. script_pushint(st,0);
  13790. return SCRIPT_CMD_SUCCESS;
  13791. }
  13792. type = script_getnum(st,2);
  13793. switch(type) {
  13794. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13795. case 1: script_pushint(st,sd->status.save_point.x); break;
  13796. case 2: script_pushint(st,sd->status.save_point.y); break;
  13797. default:
  13798. script_pushint(st,0);
  13799. break;
  13800. }
  13801. return SCRIPT_CMD_SUCCESS;
  13802. }
  13803. /**
  13804. * Get position for BL objects.
  13805. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13806. * @param mapname: String variable for output map name
  13807. * @param x: Integer variable for output coord X
  13808. * @param y: Integer variable for output coord Y
  13809. * @param type: Type of object
  13810. * BL_PC - Character coord
  13811. * BL_NPC - NPC coord
  13812. * BL_PET - Pet coord
  13813. * BL_HOM - Homun coord
  13814. * BL_MER - Mercenary coord
  13815. * BL_ELEM - Elemental coord
  13816. * @param charname: Name object. If empty or "this" use the current object
  13817. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13818. */
  13819. BUILDIN_FUNC(getmapxy)
  13820. {
  13821. struct block_list *bl = NULL;
  13822. TBL_PC *sd=NULL;
  13823. int64 num;
  13824. const char *name;
  13825. char prefix;
  13826. int x,y,type = BL_PC;
  13827. char mapname[MAP_NAME_LENGTH];
  13828. if( !data_isreference(script_getdata(st,2)) ) {
  13829. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13830. script_pushint(st,-1);
  13831. return SCRIPT_CMD_FAILURE;
  13832. }
  13833. if( !data_isreference(script_getdata(st,3)) ) {
  13834. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13835. script_pushint(st,-1);
  13836. return SCRIPT_CMD_FAILURE;
  13837. }
  13838. if( !data_isreference(script_getdata(st,4)) ) {
  13839. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13840. script_pushint(st,-1);
  13841. return SCRIPT_CMD_FAILURE;
  13842. }
  13843. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13844. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13845. script_pushint(st,-1);
  13846. return SCRIPT_CMD_FAILURE;
  13847. }
  13848. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13849. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13850. script_pushint(st,-1);
  13851. return SCRIPT_CMD_FAILURE;
  13852. }
  13853. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13854. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13855. script_pushint(st,-1);
  13856. return SCRIPT_CMD_FAILURE;
  13857. }
  13858. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13859. if (script_hasdata(st, 5))
  13860. type = script_getnum(st, 5);
  13861. switch (type) {
  13862. case BL_PC: //Get Character Position
  13863. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  13864. bl = &sd->bl;
  13865. break;
  13866. case BL_NPC: //Get NPC Position
  13867. if (script_hasdata(st, 6)) {
  13868. struct npc_data *nd;
  13869. if (script_isstring(st, 6))
  13870. nd = npc_name2id(script_getstr(st, 6));
  13871. else
  13872. nd = map_id2nd(script_getnum(st, 6));
  13873. if (nd)
  13874. bl = &nd->bl;
  13875. } else //In case the origin is not an NPC?
  13876. bl = map_id2bl(st->oid);
  13877. break;
  13878. case BL_PET: //Get Pet Position
  13879. if (sd->pd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13880. bl = &sd->pd->bl;
  13881. break;
  13882. case BL_HOM: //Get Homun Position
  13883. if (sd->hd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13884. bl = &sd->hd->bl;
  13885. break;
  13886. case BL_MER: //Get Mercenary Position
  13887. if (sd->md && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13888. bl = &sd->md->bl;
  13889. break;
  13890. case BL_ELEM: //Get Elemental Position
  13891. if (sd->ed && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13892. bl = &sd->ed->bl;
  13893. break;
  13894. default:
  13895. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13896. script_pushint(st,-1);
  13897. return SCRIPT_CMD_FAILURE;
  13898. }
  13899. if (!bl) { //No object found.
  13900. script_pushint(st,-1);
  13901. return SCRIPT_CMD_SUCCESS;
  13902. }
  13903. x= bl->x;
  13904. y= bl->y;
  13905. if (bl->m >= 0)
  13906. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  13907. else
  13908. memset(mapname, '\0', sizeof(mapname));
  13909. //Set MapName$
  13910. num=st->stack->stack_data[st->start+2].u.num;
  13911. name=get_str(script_getvarid(num));
  13912. prefix=*name;
  13913. if(not_server_variable(prefix)){
  13914. if( !script_rid2sd(sd) ){
  13915. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13916. return SCRIPT_CMD_FAILURE;
  13917. }
  13918. }else
  13919. sd=NULL;
  13920. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  13921. //Set MapX
  13922. num=st->stack->stack_data[st->start+3].u.num;
  13923. name=get_str(script_getvarid(num));
  13924. prefix=*name;
  13925. if(not_server_variable(prefix)){
  13926. if( !script_rid2sd(sd) ){
  13927. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13928. return SCRIPT_CMD_FAILURE;
  13929. }
  13930. }else
  13931. sd=NULL;
  13932. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  13933. //Set MapY
  13934. num=st->stack->stack_data[st->start+4].u.num;
  13935. name=get_str(script_getvarid(num));
  13936. prefix=*name;
  13937. if(not_server_variable(prefix)){
  13938. if( !script_rid2sd(sd) ){
  13939. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13940. return SCRIPT_CMD_FAILURE;
  13941. }
  13942. }else
  13943. sd=NULL;
  13944. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  13945. //Return Success value
  13946. script_pushint(st,0);
  13947. return SCRIPT_CMD_SUCCESS;
  13948. }
  13949. /// Returns the map name of given map ID.
  13950. ///
  13951. /// mapid2name <map ID>;
  13952. BUILDIN_FUNC(mapid2name)
  13953. {
  13954. uint16 m = script_getnum(st, 2);
  13955. if (m >= MAX_MAP_PER_SERVER) {
  13956. script_pushconststr(st, "");
  13957. return SCRIPT_CMD_FAILURE;
  13958. }
  13959. script_pushstrcopy(st, map_mapid2mapname(m));
  13960. return SCRIPT_CMD_SUCCESS;
  13961. }
  13962. /*==========================================
  13963. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13964. *------------------------------------------*/
  13965. BUILDIN_FUNC(logmes)
  13966. {
  13967. const char *str;
  13968. struct map_session_data* sd = map_id2sd(st->rid);
  13969. str = script_getstr(st,2);
  13970. if( sd ){
  13971. log_npc(sd,str);
  13972. }else{
  13973. struct npc_data* nd = map_id2nd(st->oid);
  13974. if( !nd ){
  13975. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  13976. return SCRIPT_CMD_FAILURE;
  13977. }
  13978. log_npc(nd,str);
  13979. }
  13980. return SCRIPT_CMD_SUCCESS;
  13981. }
  13982. BUILDIN_FUNC(summon)
  13983. {
  13984. int _class, timeout=0;
  13985. const char *str,*event="";
  13986. TBL_PC *sd;
  13987. struct mob_data *md;
  13988. t_tick tick = gettick();
  13989. if (!script_rid2sd(sd))
  13990. return SCRIPT_CMD_SUCCESS;
  13991. str =script_getstr(st,2);
  13992. _class=script_getnum(st,3);
  13993. if( script_hasdata(st,4) )
  13994. timeout=script_getnum(st,4);
  13995. if( script_hasdata(st,5) ){
  13996. event=script_getstr(st,5);
  13997. check_event(st, event);
  13998. }
  13999. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  14000. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  14001. if (md) {
  14002. md->master_id=sd->bl.id;
  14003. md->special_state.ai = AI_ATTACK;
  14004. if( md->deletetimer != INVALID_TIMER )
  14005. delete_timer(md->deletetimer, mob_timer_delete);
  14006. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  14007. mob_spawn (md); //Now it is ready for spawning.
  14008. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  14009. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  14010. }
  14011. script_pushint(st, md->bl.id);
  14012. return SCRIPT_CMD_SUCCESS;
  14013. }
  14014. /*==========================================
  14015. * Checks whether it is daytime/nighttime
  14016. *------------------------------------------*/
  14017. BUILDIN_FUNC(isnight)
  14018. {
  14019. script_pushint(st,(night_flag == 1));
  14020. return SCRIPT_CMD_SUCCESS;
  14021. }
  14022. BUILDIN_FUNC(isday)
  14023. {
  14024. script_pushint(st,(night_flag == 0));
  14025. return SCRIPT_CMD_SUCCESS;
  14026. }
  14027. /*================================================
  14028. * Check how many items/cards in the list are
  14029. * equipped - used for 2/15's cards patch [celest]
  14030. *------------------------------------------------*/
  14031. BUILDIN_FUNC(isequippedcnt)
  14032. {
  14033. TBL_PC *sd;
  14034. if (!script_rid2sd(sd)) {
  14035. script_pushint(st,0);
  14036. return SCRIPT_CMD_SUCCESS;
  14037. }
  14038. int ret = 0;
  14039. int total = script_lastdata(st);
  14040. std::vector<int32> list(total);
  14041. for (int i = 2; i <= total; ++i) {
  14042. int id = script_getnum(st,i);
  14043. if (id <= 0)
  14044. continue;
  14045. if (std::find(list.begin(), list.end(), id) != list.end())
  14046. continue;
  14047. list.push_back(id);
  14048. for (short j = 0; j < EQI_MAX; j++) {
  14049. short index = sd->equip_index[j];
  14050. if (index < 0)
  14051. continue;
  14052. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  14053. continue;
  14054. if (!sd->inventory_data[index])
  14055. continue;
  14056. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  14057. if (sd->inventory_data[index]->nameid == id)
  14058. ret += sd->inventory.u.items_inventory[index].amount;
  14059. } else { //Count cards.
  14060. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14061. continue; //No cards
  14062. for (short k = 0; k < MAX_SLOTS; k++) {
  14063. if (sd->inventory.u.items_inventory[index].card[k] == id)
  14064. ret++; //[Lupus]
  14065. }
  14066. }
  14067. }
  14068. }
  14069. script_pushint(st,ret);
  14070. return SCRIPT_CMD_SUCCESS;
  14071. }
  14072. /*================================================
  14073. * Check whether another card has been
  14074. * equipped - used for 2/15's cards patch [celest]
  14075. * -- Items checked cannot be reused in another
  14076. * card set to prevent exploits
  14077. *------------------------------------------------*/
  14078. BUILDIN_FUNC(isequipped)
  14079. {
  14080. TBL_PC *sd;
  14081. int i, id = 1;
  14082. int ret = -1;
  14083. //Original hash to reverse it when full check fails.
  14084. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  14085. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  14086. script_pushint(st,0);
  14087. return SCRIPT_CMD_SUCCESS;
  14088. }
  14089. setitem_hash = sd->bonus.setitem_hash;
  14090. setitem_hash2 = sd->bonus.setitem_hash2;
  14091. for (i=0; id!=0; i++) {
  14092. int flag = 0;
  14093. short j;
  14094. FETCH (i+2, id) else id = 0;
  14095. if (id <= 0)
  14096. continue;
  14097. for (j=0; j<EQI_MAX; j++) {
  14098. short index = sd->equip_index[j];
  14099. if(index < 0)
  14100. continue;
  14101. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  14102. continue;
  14103. if(!sd->inventory_data[index])
  14104. continue;
  14105. if (itemdb_type(id) != IT_CARD) {
  14106. if (sd->inventory_data[index]->nameid != id)
  14107. continue;
  14108. flag = 1;
  14109. break;
  14110. } else { //Cards
  14111. short k;
  14112. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14113. continue;
  14114. for (k = 0; k < MAX_SLOTS; k++)
  14115. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  14116. unsigned int hash = 0;
  14117. if (sd->inventory.u.items_inventory[index].card[k] != id)
  14118. continue;
  14119. hash = 1<<((j<5?j:j-5)*4 + k);
  14120. // check if card is already used by another set
  14121. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  14122. continue;
  14123. // We have found a match
  14124. flag = 1;
  14125. // Set hash so this card cannot be used by another
  14126. if (j<5)
  14127. sd->bonus.setitem_hash |= hash;
  14128. else
  14129. sd->bonus.setitem_hash2 |= hash;
  14130. break;
  14131. }
  14132. }
  14133. if (flag) break; //Card found
  14134. }
  14135. if (ret == -1)
  14136. ret = flag;
  14137. else
  14138. ret &= flag;
  14139. if (!ret) break;
  14140. }
  14141. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  14142. sd->bonus.setitem_hash = setitem_hash;
  14143. sd->bonus.setitem_hash2 = setitem_hash2;
  14144. }
  14145. script_pushint(st,ret);
  14146. return SCRIPT_CMD_SUCCESS;
  14147. }
  14148. /*================================================
  14149. * Check how many given inserted cards in the CURRENT
  14150. * weapon - used for 2/15's cards patch [Lupus]
  14151. *------------------------------------------------*/
  14152. BUILDIN_FUNC(cardscnt)
  14153. {
  14154. TBL_PC *sd;
  14155. int i, k, id = 1;
  14156. int ret = 0;
  14157. int index;
  14158. if( !script_rid2sd(sd) ){
  14159. script_pushint(st,0);
  14160. return SCRIPT_CMD_SUCCESS;
  14161. }
  14162. for (i=0; id!=0; i++) {
  14163. FETCH (i+2, id) else id = 0;
  14164. if (id <= 0)
  14165. continue;
  14166. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  14167. if(index < 0) continue;
  14168. if(!sd->inventory_data[index])
  14169. continue;
  14170. if(itemdb_type(id) != IT_CARD) {
  14171. if (sd->inventory_data[index]->nameid == id)
  14172. ret+= sd->inventory.u.items_inventory[index].amount;
  14173. } else {
  14174. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  14175. continue;
  14176. for (k=0; k < MAX_SLOTS; k++) {
  14177. if (sd->inventory.u.items_inventory[index].card[k] == id)
  14178. ret++;
  14179. }
  14180. }
  14181. }
  14182. script_pushint(st,ret);
  14183. // script_pushint(st,current_equip_item_index);
  14184. return SCRIPT_CMD_SUCCESS;
  14185. }
  14186. /*=======================================================
  14187. * Returns the refined number of the current item, or an
  14188. * item with inventory index specified
  14189. *-------------------------------------------------------*/
  14190. BUILDIN_FUNC(getrefine)
  14191. {
  14192. TBL_PC *sd;
  14193. if (script_rid2sd(sd)){
  14194. if( current_equip_item_index == -1 ){
  14195. script_pushint(st, 0);
  14196. return SCRIPT_CMD_FAILURE;
  14197. }
  14198. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  14199. }else
  14200. script_pushint(st,0);
  14201. return SCRIPT_CMD_SUCCESS;
  14202. }
  14203. /*=======================================================
  14204. * Day/Night controls
  14205. *-------------------------------------------------------*/
  14206. BUILDIN_FUNC(night)
  14207. {
  14208. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  14209. return SCRIPT_CMD_SUCCESS;
  14210. }
  14211. BUILDIN_FUNC(day)
  14212. {
  14213. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  14214. return SCRIPT_CMD_SUCCESS;
  14215. }
  14216. /**
  14217. * unequip <equipment slot>{,<char_id>};
  14218. * @author [Spectre]
  14219. **/
  14220. BUILDIN_FUNC(unequip) {
  14221. int pos;
  14222. TBL_PC *sd;
  14223. if (!script_charid2sd(3,sd))
  14224. return SCRIPT_CMD_FAILURE;
  14225. pos = script_getnum(st,2);
  14226. if (equip_index_check(pos)) {
  14227. short i = pc_checkequip(sd,equip_bitmask[pos]);
  14228. if (i >= 0) {
  14229. pc_unequipitem(sd,i,1|2);
  14230. script_pushint(st, 1);
  14231. return SCRIPT_CMD_SUCCESS;
  14232. }
  14233. }
  14234. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  14235. script_pushint(st, 0);
  14236. return SCRIPT_CMD_FAILURE;
  14237. }
  14238. /**
  14239. * equip <item id>{,<char_id>};
  14240. **/
  14241. BUILDIN_FUNC(equip) {
  14242. TBL_PC *sd;
  14243. if (!script_charid2sd(3,sd))
  14244. return SCRIPT_CMD_FAILURE;
  14245. t_itemid nameid = script_getnum(st,2);
  14246. std::shared_ptr<item_data> id = item_db.find(nameid);
  14247. if (id != nullptr) {
  14248. int i;
  14249. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  14250. if (i < MAX_INVENTORY) {
  14251. pc_equipitem(sd,i,id->equip);
  14252. script_pushint(st,1);
  14253. return SCRIPT_CMD_SUCCESS;
  14254. }
  14255. }
  14256. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  14257. script_pushint(st,0);
  14258. return SCRIPT_CMD_FAILURE;
  14259. }
  14260. BUILDIN_FUNC(autoequip)
  14261. {
  14262. t_itemid nameid=script_getnum(st,2);
  14263. int flag=script_getnum(st,3);
  14264. std::shared_ptr<item_data> id = item_db.find(nameid);
  14265. if( id == nullptr )
  14266. {
  14267. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  14268. return SCRIPT_CMD_FAILURE;
  14269. }
  14270. if( !itemdb_isequip2(id.get()) )
  14271. {
  14272. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  14273. return SCRIPT_CMD_FAILURE;
  14274. }
  14275. id->flag.autoequip = flag>0?1:0;
  14276. return SCRIPT_CMD_SUCCESS;
  14277. }
  14278. /**
  14279. * Set the value of a given battle config
  14280. * setbattleflag "<battle flag>",<value>{,<reload>};
  14281. */
  14282. BUILDIN_FUNC(setbattleflag)
  14283. {
  14284. const char *flag, *value;
  14285. flag = script_getstr(st,2);
  14286. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  14287. if (battle_set_value(flag, value) == 0)
  14288. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  14289. else {
  14290. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  14291. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  14292. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  14293. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  14294. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  14295. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  14296. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  14297. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  14298. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  14299. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  14300. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  14301. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  14302. };
  14303. uint8 i;
  14304. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  14305. if (!strcmpi(flag, expdrop_flags[i])) {
  14306. mob_reload();
  14307. break;
  14308. }
  14309. }
  14310. }
  14311. }
  14312. return SCRIPT_CMD_SUCCESS;
  14313. }
  14314. /**
  14315. * Get the value of a given battle config
  14316. * getbattleflag("<battle flag>")
  14317. */
  14318. BUILDIN_FUNC(getbattleflag)
  14319. {
  14320. const char *flag = script_getstr(st,2);
  14321. script_pushint(st,battle_get_value(flag));
  14322. return SCRIPT_CMD_SUCCESS;
  14323. }
  14324. //=======================================================
  14325. // strlen [Valaris]
  14326. //-------------------------------------------------------
  14327. BUILDIN_FUNC(getstrlen)
  14328. {
  14329. const char *str = script_getstr(st,2);
  14330. int len = (str) ? (int)strlen(str) : 0;
  14331. script_pushint(st,len);
  14332. return SCRIPT_CMD_SUCCESS;
  14333. }
  14334. //=======================================================
  14335. // isalpha [Valaris]
  14336. //-------------------------------------------------------
  14337. BUILDIN_FUNC(charisalpha)
  14338. {
  14339. const char *str=script_getstr(st,2);
  14340. int pos=script_getnum(st,3);
  14341. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  14342. script_pushint(st,val);
  14343. return SCRIPT_CMD_SUCCESS;
  14344. }
  14345. //=======================================================
  14346. // charisupper <str>, <index>
  14347. //-------------------------------------------------------
  14348. BUILDIN_FUNC(charisupper)
  14349. {
  14350. const char *str = script_getstr(st,2);
  14351. int pos = script_getnum(st,3);
  14352. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  14353. script_pushint(st,val);
  14354. return SCRIPT_CMD_SUCCESS;
  14355. }
  14356. //=======================================================
  14357. // charislower <str>, <index>
  14358. //-------------------------------------------------------
  14359. BUILDIN_FUNC(charislower)
  14360. {
  14361. const char *str = script_getstr(st,2);
  14362. int pos = script_getnum(st,3);
  14363. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  14364. script_pushint(st,val);
  14365. return SCRIPT_CMD_SUCCESS;
  14366. }
  14367. //=======================================================
  14368. // charat <str>, <index>
  14369. //-------------------------------------------------------
  14370. BUILDIN_FUNC(charat) {
  14371. const char *str = script_getstr(st,2);
  14372. int pos = script_getnum(st,3);
  14373. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  14374. char output[2];
  14375. output[0] = str[pos];
  14376. output[1] = '\0';
  14377. script_pushstrcopy(st, output);
  14378. } else
  14379. script_pushconststr(st, "");
  14380. return SCRIPT_CMD_SUCCESS;
  14381. }
  14382. //=======================================================
  14383. // setchar <string>, <char>, <index>
  14384. //-------------------------------------------------------
  14385. BUILDIN_FUNC(setchar)
  14386. {
  14387. const char *str = script_getstr(st,2);
  14388. const char *c = script_getstr(st,3);
  14389. int index = script_getnum(st,4);
  14390. char *output = aStrdup(str);
  14391. if(index >= 0 && index < strlen(output))
  14392. output[index] = *c;
  14393. script_pushstr(st, output);
  14394. return SCRIPT_CMD_SUCCESS;
  14395. }
  14396. //=======================================================
  14397. // insertchar <string>, <char>, <index>
  14398. //-------------------------------------------------------
  14399. BUILDIN_FUNC(insertchar)
  14400. {
  14401. const char *str = script_getstr(st,2);
  14402. const char *c = script_getstr(st,3);
  14403. int index = script_getnum(st,4);
  14404. char *output;
  14405. size_t len = strlen(str);
  14406. if(index < 0)
  14407. index = 0;
  14408. else if(index > len)
  14409. index = len;
  14410. output = (char*)aMalloc(len + 2);
  14411. memcpy(output, str, index);
  14412. output[index] = c[0];
  14413. memcpy(&output[index+1], &str[index], len - index);
  14414. output[len+1] = '\0';
  14415. script_pushstr(st, output);
  14416. return SCRIPT_CMD_SUCCESS;
  14417. }
  14418. //=======================================================
  14419. // delchar <string>, <index>
  14420. //-------------------------------------------------------
  14421. BUILDIN_FUNC(delchar)
  14422. {
  14423. const char *str = script_getstr(st,2);
  14424. int index = script_getnum(st,3);
  14425. char *output;
  14426. size_t len = strlen(str);
  14427. if(index < 0 || index > len) {
  14428. //return original
  14429. output = aStrdup(str);
  14430. script_pushstr(st, output);
  14431. return SCRIPT_CMD_SUCCESS;
  14432. }
  14433. output = (char*)aMalloc(len);
  14434. memcpy(output, str, index);
  14435. memcpy(&output[index], &str[index+1], len - index);
  14436. script_pushstr(st, output);
  14437. return SCRIPT_CMD_SUCCESS;
  14438. }
  14439. //=======================================================
  14440. // strtoupper <str>
  14441. //-------------------------------------------------------
  14442. BUILDIN_FUNC(strtoupper)
  14443. {
  14444. const char *str = script_getstr(st,2);
  14445. char *output = aStrdup(str);
  14446. char *cursor = output;
  14447. while (*cursor != '\0') {
  14448. *cursor = TOUPPER(*cursor);
  14449. cursor++;
  14450. }
  14451. script_pushstr(st, output);
  14452. return SCRIPT_CMD_SUCCESS;
  14453. }
  14454. //=======================================================
  14455. // strtolower <str>
  14456. //-------------------------------------------------------
  14457. BUILDIN_FUNC(strtolower)
  14458. {
  14459. const char *str = script_getstr(st,2);
  14460. char *output = aStrdup(str);
  14461. char *cursor = output;
  14462. while (*cursor != '\0') {
  14463. *cursor = TOLOWER(*cursor);
  14464. cursor++;
  14465. }
  14466. script_pushstr(st, output);
  14467. return SCRIPT_CMD_SUCCESS;
  14468. }
  14469. //=======================================================
  14470. // substr <str>, <start>, <end>
  14471. //-------------------------------------------------------
  14472. BUILDIN_FUNC(substr)
  14473. {
  14474. const char *str = script_getstr(st,2);
  14475. char *output;
  14476. int start = script_getnum(st,3);
  14477. int end = script_getnum(st,4);
  14478. int len = 0;
  14479. if(start >= 0 && end < strlen(str) && start <= end) {
  14480. len = end - start + 1;
  14481. output = (char*)aMalloc(len + 1);
  14482. memcpy(output, &str[start], len);
  14483. } else
  14484. output = (char*)aMalloc(1);
  14485. output[len] = '\0';
  14486. script_pushstr(st, output);
  14487. return SCRIPT_CMD_SUCCESS;
  14488. }
  14489. //=======================================================
  14490. // explode <dest_string_array>, <str>, <delimiter>
  14491. // Note: delimiter is limited to 1 char
  14492. //-------------------------------------------------------
  14493. BUILDIN_FUNC(explode)
  14494. {
  14495. struct script_data* data = script_getdata(st, 2);
  14496. const char *str = script_getstr(st,3);
  14497. const char delimiter = script_getstr(st, 4)[0];
  14498. int32 id;
  14499. size_t len = strlen(str);
  14500. int i = 0, j = 0;
  14501. int start;
  14502. char *temp;
  14503. const char* name;
  14504. TBL_PC* sd = NULL;
  14505. temp = (char*)aMalloc(len + 1);
  14506. if( !data_isreference(data) ) {
  14507. ShowError("script:explode: not a variable\n");
  14508. script_reportdata(data);
  14509. st->state = END;
  14510. return SCRIPT_CMD_FAILURE;// not a variable
  14511. }
  14512. id = reference_getid(data);
  14513. start = reference_getindex(data);
  14514. name = reference_getname(data);
  14515. if( !is_string_variable(name) ) {
  14516. ShowError("script:explode: not string array\n");
  14517. script_reportdata(data);
  14518. st->state = END;
  14519. return SCRIPT_CMD_FAILURE;// data type mismatch
  14520. }
  14521. if( not_server_variable(*name) ) {
  14522. if( !script_rid2sd(sd) )
  14523. return SCRIPT_CMD_SUCCESS;// no player attached
  14524. }
  14525. while(str[i] != '\0') {
  14526. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14527. temp[j] = '\0';
  14528. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14529. j = 0;
  14530. ++i;
  14531. } else {
  14532. temp[j++] = str[i++];
  14533. }
  14534. }
  14535. //set last string
  14536. temp[j] = '\0';
  14537. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14538. aFree(temp);
  14539. return SCRIPT_CMD_SUCCESS;
  14540. }
  14541. //=======================================================
  14542. // implode <string_array>
  14543. // implode <string_array>, <glue>
  14544. //-------------------------------------------------------
  14545. BUILDIN_FUNC(implode)
  14546. {
  14547. struct script_data* data = script_getdata(st, 2);
  14548. const char *name;
  14549. uint32 glue_len = 0, array_size, id;
  14550. char *output;
  14551. TBL_PC* sd = NULL;
  14552. if( !data_isreference(data) ) {
  14553. ShowError("script:implode: not a variable\n");
  14554. script_reportdata(data);
  14555. st->state = END;
  14556. return SCRIPT_CMD_FAILURE;// not a variable
  14557. }
  14558. id = reference_getid(data);
  14559. name = reference_getname(data);
  14560. if( !is_string_variable(name) ) {
  14561. ShowError("script:implode: not string array\n");
  14562. script_reportdata(data);
  14563. st->state = END;
  14564. return SCRIPT_CMD_FAILURE;// data type mismatch
  14565. }
  14566. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14567. return SCRIPT_CMD_SUCCESS;// no player attached
  14568. }
  14569. //count chars
  14570. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  14571. if(array_size == -1) { //empty array check (AmsTaff)
  14572. ShowWarning("script:implode: array length = 0\n");
  14573. output = (char*)aMalloc(sizeof(char)*5);
  14574. sprintf(output,"%s","NULL");
  14575. } else {
  14576. const char *glue = NULL, *temp;
  14577. size_t len = 0;
  14578. int i, k = 0;
  14579. for(i = 0; i <= array_size; ++i) {
  14580. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14581. len += strlen(temp);
  14582. // Remove stack entry from get_val2_str
  14583. script_removetop( st, -1, 0 );
  14584. }
  14585. //allocate mem
  14586. if( script_hasdata(st,3) ) {
  14587. glue = script_getstr(st,3);
  14588. glue_len = strlen(glue);
  14589. len += glue_len * (array_size);
  14590. }
  14591. output = (char*)aMalloc(len + 1);
  14592. //build output
  14593. for(i = 0; i < array_size; ++i) {
  14594. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14595. len = strlen(temp);
  14596. memcpy(&output[k], temp, len);
  14597. k += len;
  14598. // Remove stack entry from get_val2_str
  14599. script_removetop( st, -1, 0 );
  14600. if(glue_len != 0) {
  14601. memcpy(&output[k], glue, glue_len);
  14602. k += glue_len;
  14603. }
  14604. }
  14605. temp = get_val2_str( st, reference_uid( id, array_size ), reference_getref( data ) );
  14606. len = strlen(temp);
  14607. memcpy(&output[k], temp, len);
  14608. k += len;
  14609. output[k] = '\0';
  14610. // Remove stack entry from get_val2_str
  14611. script_removetop( st, -1, 0 );
  14612. }
  14613. script_pushstr(st, output);
  14614. return SCRIPT_CMD_SUCCESS;
  14615. }
  14616. //=======================================================
  14617. // sprintf(<format>, ...);
  14618. // Implements C sprintf, except format %n. The resulting string is
  14619. // returned, instead of being saved in variable by reference.
  14620. //-------------------------------------------------------
  14621. BUILDIN_FUNC(sprintf)
  14622. {
  14623. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14624. const char* format;
  14625. char* p;
  14626. char* q;
  14627. char* buf = NULL;
  14628. char* buf2 = NULL;
  14629. struct script_data* data;
  14630. StringBuf final_buf;
  14631. // Fetch init data
  14632. format = script_getstr(st, 2);
  14633. argc = script_lastdata(st)-2;
  14634. len = strlen(format);
  14635. // Skip parsing, where no parsing is required.
  14636. if(len == 0) {
  14637. script_pushconststr(st,"");
  14638. return SCRIPT_CMD_SUCCESS;
  14639. }
  14640. // Pessimistic alloc
  14641. CREATE(buf, char, len+1);
  14642. // Need not be parsed, just solve stuff like %%.
  14643. if(argc == 0) {
  14644. memcpy(buf,format,len+1);
  14645. script_pushstrcopy(st, buf);
  14646. aFree(buf);
  14647. return SCRIPT_CMD_SUCCESS;
  14648. }
  14649. safestrncpy(buf, format, len+1);
  14650. // Issue sprintf for each parameter
  14651. StringBuf_Init(&final_buf);
  14652. q = buf;
  14653. while((p = strchr(q, '%')) != NULL) {
  14654. if(p != q) {
  14655. len = p - q + 1;
  14656. if(buf2_len < len) {
  14657. RECREATE(buf2, char, len);
  14658. buf2_len = len;
  14659. }
  14660. safestrncpy(buf2, q, len);
  14661. StringBuf_AppendStr(&final_buf, buf2);
  14662. q = p;
  14663. }
  14664. p = q + 1;
  14665. if(*p == '%') { // %%
  14666. StringBuf_AppendStr(&final_buf, "%");
  14667. q+=2;
  14668. continue;
  14669. }
  14670. if(*p == 'n') { // %n
  14671. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14672. script_reportsrc(st);
  14673. q+=2;
  14674. continue;
  14675. }
  14676. if(arg >= argc) {
  14677. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14678. if(buf) aFree(buf);
  14679. if(buf2) aFree(buf2);
  14680. StringBuf_Destroy(&final_buf);
  14681. script_pushconststr(st,"");
  14682. return SCRIPT_CMD_FAILURE;
  14683. }
  14684. if((p = strchr(q+1, '%')) == NULL)
  14685. p = strchr(q, 0); // EOS
  14686. len = p - q + 1;
  14687. if(buf2_len < len) {
  14688. RECREATE(buf2, char, len);
  14689. buf2_len = len;
  14690. }
  14691. safestrncpy(buf2, q, len);
  14692. q = p;
  14693. // Note: This assumes the passed value being the correct
  14694. // type to the current format specifier. If not, the server
  14695. // probably crashes or returns anything else, than expected,
  14696. // but it would behave in normal code the same way so it's
  14697. // the scripter's responsibility.
  14698. data = script_getdata(st, arg+3);
  14699. if(data_isstring(data)) // String
  14700. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14701. else if(data_isint(data)) // Number
  14702. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14703. else if(data_isreference(data)) { // Variable
  14704. char* name = reference_getname(data);
  14705. if(name[strlen(name)-1]=='$') // var Str
  14706. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14707. else // var Int
  14708. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14709. } else { // Unsupported type
  14710. ShowError("buildin_sprintf: Unknown argument type!\n");
  14711. if(buf) aFree(buf);
  14712. if(buf2) aFree(buf2);
  14713. StringBuf_Destroy(&final_buf);
  14714. script_pushconststr(st,"");
  14715. return SCRIPT_CMD_FAILURE;
  14716. }
  14717. arg++;
  14718. }
  14719. // Append anything left
  14720. if(*q)
  14721. StringBuf_AppendStr(&final_buf, q);
  14722. // Passed more, than needed
  14723. if(arg < argc) {
  14724. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14725. script_reportsrc(st);
  14726. }
  14727. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14728. if(buf) aFree(buf);
  14729. if(buf2) aFree(buf2);
  14730. StringBuf_Destroy(&final_buf);
  14731. return SCRIPT_CMD_SUCCESS;
  14732. }
  14733. //=======================================================
  14734. // sscanf(<str>, <format>, ...);
  14735. // Implements C sscanf.
  14736. //-------------------------------------------------------
  14737. BUILDIN_FUNC(sscanf){
  14738. unsigned int argc, arg = 0, len;
  14739. struct script_data* data;
  14740. struct map_session_data* sd = NULL;
  14741. const char* str;
  14742. const char* format;
  14743. const char* p;
  14744. const char* q;
  14745. char* buf = NULL;
  14746. char* buf_p;
  14747. char* ref_str = NULL;
  14748. int ref_int;
  14749. // Get data
  14750. str = script_getstr(st, 2);
  14751. format = script_getstr(st, 3);
  14752. argc = script_lastdata(st)-3;
  14753. len = strlen(format);
  14754. if (len != 0 && strlen(str) == 0) {
  14755. // If the source string is empty but the format string is not, we return -1
  14756. // according to the C specs. (if the format string is also empty, we shall
  14757. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14758. script_pushint(st, -1);
  14759. return SCRIPT_CMD_SUCCESS;
  14760. }
  14761. CREATE(buf, char, len*2+1);
  14762. // Issue sscanf for each parameter
  14763. *buf = 0;
  14764. q = format;
  14765. while((p = strchr(q, '%'))){
  14766. if(p!=q){
  14767. strncat(buf, q, (size_t)(p-q));
  14768. q = p;
  14769. }
  14770. p = q+1;
  14771. if(*p=='*' || *p=='%'){ // Skip
  14772. strncat(buf, q, 2);
  14773. q+=2;
  14774. continue;
  14775. }
  14776. if(arg>=argc){
  14777. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14778. script_pushint(st, -1);
  14779. if(buf) aFree(buf);
  14780. if(ref_str) aFree(ref_str);
  14781. return SCRIPT_CMD_FAILURE;
  14782. }
  14783. if((p = strchr(q+1, '%'))==NULL){
  14784. p = strchr(q, 0); // EOS
  14785. }
  14786. len = p-q;
  14787. strncat(buf, q, len);
  14788. q = p;
  14789. // Validate output
  14790. data = script_getdata(st, arg+4);
  14791. if(!data_isreference(data) || !reference_tovariable(data)){
  14792. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14793. script_pushint(st, -1);
  14794. if(buf) aFree(buf);
  14795. if(ref_str) aFree(ref_str);
  14796. return SCRIPT_CMD_FAILURE;
  14797. }
  14798. buf_p = reference_getname(data);
  14799. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14800. script_pushint(st, -1);
  14801. if(buf) aFree(buf);
  14802. if(ref_str) aFree(ref_str);
  14803. return SCRIPT_CMD_SUCCESS;
  14804. }
  14805. // Save value if any
  14806. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14807. if(ref_str==NULL){
  14808. CREATE(ref_str, char, strlen(str)+1);
  14809. }
  14810. if(sscanf(str, buf, ref_str)==0){
  14811. break;
  14812. }
  14813. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14814. } else { // Number
  14815. if(sscanf(str, buf, &ref_int)==0){
  14816. break;
  14817. }
  14818. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14819. }
  14820. arg++;
  14821. // Disable used format (%... -> %*...)
  14822. buf_p = strchr(buf, 0);
  14823. memmove(buf_p-len+2, buf_p-len+1, len);
  14824. *(buf_p-len+1) = '*';
  14825. }
  14826. script_pushint(st, arg);
  14827. if(buf) aFree(buf);
  14828. if(ref_str) aFree(ref_str);
  14829. return SCRIPT_CMD_SUCCESS;
  14830. }
  14831. //=======================================================
  14832. // strpos(<haystack>, <needle>)
  14833. // strpos(<haystack>, <needle>, <offset>)
  14834. //
  14835. // Implements PHP style strpos. Adapted from code from
  14836. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14837. //-------------------------------------------------------
  14838. BUILDIN_FUNC(strpos) {
  14839. const char *haystack = script_getstr(st,2);
  14840. const char *needle = script_getstr(st,3);
  14841. int i;
  14842. size_t len;
  14843. if( script_hasdata(st,4) )
  14844. i = script_getnum(st,4);
  14845. else
  14846. i = 0;
  14847. if (needle[0] == '\0') {
  14848. script_pushint(st, -1);
  14849. return SCRIPT_CMD_SUCCESS;
  14850. }
  14851. len = strlen(haystack);
  14852. for ( ; i < len; ++i ) {
  14853. if ( haystack[i] == *needle ) {
  14854. // matched starting char -- loop through remaining chars
  14855. const char *h, *n;
  14856. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14857. if ( *h != *n ) {
  14858. break;
  14859. }
  14860. }
  14861. if ( !*n ) { // matched all of 'needle' to null termination
  14862. script_pushint(st, i);
  14863. return SCRIPT_CMD_SUCCESS;
  14864. }
  14865. }
  14866. }
  14867. script_pushint(st, -1);
  14868. return SCRIPT_CMD_SUCCESS;
  14869. }
  14870. //===============================================================
  14871. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14872. //
  14873. // Note: Finds all instances of <search> in <input> and replaces
  14874. // with <replace>. If specified will only replace as many
  14875. // instances as specified in <count>. By default will be case
  14876. // sensitive.
  14877. //---------------------------------------------------------------
  14878. BUILDIN_FUNC(replacestr)
  14879. {
  14880. const char *input = script_getstr(st, 2);
  14881. const char *find = script_getstr(st, 3);
  14882. const char *replace = script_getstr(st, 4);
  14883. size_t inputlen = strlen(input);
  14884. size_t findlen = strlen(find);
  14885. struct StringBuf output;
  14886. bool usecase = true;
  14887. int count = 0;
  14888. int numFinds = 0;
  14889. int i = 0, f = 0;
  14890. if(findlen == 0) {
  14891. ShowError("script:replacestr: Invalid search length.\n");
  14892. st->state = END;
  14893. return SCRIPT_CMD_FAILURE;
  14894. }
  14895. if(script_hasdata(st, 5)) {
  14896. if( script_isint(st, 5) )
  14897. usecase = script_getnum(st, 5) != 0;
  14898. else {
  14899. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14900. st->state = END;
  14901. return SCRIPT_CMD_FAILURE;
  14902. }
  14903. }
  14904. if(script_hasdata(st, 6)) {
  14905. count = script_getnum(st, 6);
  14906. if(count == 0) {
  14907. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14908. st->state = END;
  14909. return SCRIPT_CMD_FAILURE;
  14910. }
  14911. }
  14912. StringBuf_Init(&output);
  14913. for(; i < inputlen; i++) {
  14914. if(count && count == numFinds) { //found enough, stop looking
  14915. break;
  14916. }
  14917. for(f = 0; f <= findlen; f++) {
  14918. if(f == findlen) { //complete match
  14919. numFinds++;
  14920. StringBuf_AppendStr(&output, replace);
  14921. i += findlen - 1;
  14922. break;
  14923. } else {
  14924. if(usecase) {
  14925. if((i + f) > inputlen || input[i + f] != find[f]) {
  14926. StringBuf_Printf(&output, "%c", input[i]);
  14927. break;
  14928. }
  14929. } else {
  14930. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14931. StringBuf_Printf(&output, "%c", input[i]);
  14932. break;
  14933. }
  14934. }
  14935. }
  14936. }
  14937. }
  14938. //append excess after enough found
  14939. if(i < inputlen)
  14940. StringBuf_AppendStr(&output, &(input[i]));
  14941. script_pushstrcopy(st, StringBuf_Value(&output));
  14942. StringBuf_Destroy(&output);
  14943. return SCRIPT_CMD_SUCCESS;
  14944. }
  14945. //========================================================
  14946. // countstr <input>, <search>{, <usecase>}
  14947. //
  14948. // Note: Counts the number of times <search> occurs in
  14949. // <input>. By default will be case sensitive.
  14950. //--------------------------------------------------------
  14951. BUILDIN_FUNC(countstr)
  14952. {
  14953. const char *input = script_getstr(st, 2);
  14954. const char *find = script_getstr(st, 3);
  14955. size_t inputlen = strlen(input);
  14956. size_t findlen = strlen(find);
  14957. bool usecase = true;
  14958. int numFinds = 0;
  14959. int i = 0, f = 0;
  14960. if(findlen == 0) {
  14961. ShowError("script:countstr: Invalid search length.\n");
  14962. st->state = END;
  14963. return SCRIPT_CMD_FAILURE;
  14964. }
  14965. if(script_hasdata(st, 4)) {
  14966. if( script_isint(st, 4) )
  14967. usecase = script_getnum(st, 4) != 0;
  14968. else {
  14969. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14970. st->state = END;
  14971. return SCRIPT_CMD_FAILURE;
  14972. }
  14973. }
  14974. for(; i < inputlen; i++) {
  14975. for(f = 0; f <= findlen; f++) {
  14976. if(f == findlen) { //complete match
  14977. numFinds++;
  14978. i += findlen - 1;
  14979. break;
  14980. } else {
  14981. if(usecase) {
  14982. if((i + f) > inputlen || input[i + f] != find[f]) {
  14983. break;
  14984. }
  14985. } else {
  14986. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14987. break;
  14988. }
  14989. }
  14990. }
  14991. }
  14992. }
  14993. script_pushint(st, numFinds);
  14994. return SCRIPT_CMD_SUCCESS;
  14995. }
  14996. /// Changes the display name and/or display class of the npc.
  14997. /// Returns 0 is successful, 1 if the npc does not exist.
  14998. ///
  14999. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  15000. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  15001. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  15002. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  15003. BUILDIN_FUNC(setnpcdisplay)
  15004. {
  15005. const char* name;
  15006. const char* newname = NULL;
  15007. int class_ = JT_FAKENPC, size = -1;
  15008. struct npc_data* nd;
  15009. name = script_getstr(st,2);
  15010. if( script_hasdata(st,4) )
  15011. class_ = script_getnum(st,4);
  15012. if( script_hasdata(st,5) )
  15013. size = script_getnum(st,5);
  15014. if( script_isstring(st, 3) )
  15015. newname = script_getstr(st, 3);
  15016. else
  15017. class_ = script_getnum(st, 3);
  15018. nd = npc_name2id(name);
  15019. if( nd == NULL )
  15020. {// not found
  15021. script_pushint(st,1);
  15022. return SCRIPT_CMD_SUCCESS;
  15023. }
  15024. // update npc
  15025. if( newname )
  15026. npc_setdisplayname(nd, newname);
  15027. if( size != -1 && size != (int)nd->size )
  15028. nd->size = size;
  15029. else
  15030. size = -1;
  15031. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  15032. npc_setclass(nd, class_);
  15033. else if( size != -1 )
  15034. unit_refresh(&nd->bl); // Required to update the visual size
  15035. script_pushint(st,0);
  15036. return SCRIPT_CMD_SUCCESS;
  15037. }
  15038. BUILDIN_FUNC(atoi)
  15039. {
  15040. const char *value;
  15041. value = script_getstr(st,2);
  15042. script_pushint(st,atoi(value));
  15043. return SCRIPT_CMD_SUCCESS;
  15044. }
  15045. BUILDIN_FUNC(axtoi)
  15046. {
  15047. const char *hex = script_getstr(st,2);
  15048. long value = strtol(hex, NULL, 16);
  15049. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  15050. value = cap_value(value, INT_MIN, INT_MAX);
  15051. #endif
  15052. script_pushint(st, (int)value);
  15053. return SCRIPT_CMD_SUCCESS;
  15054. }
  15055. BUILDIN_FUNC(strtol)
  15056. {
  15057. const char *string = script_getstr(st, 2);
  15058. int base = script_getnum(st, 3);
  15059. long value = strtol(string, NULL, base);
  15060. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  15061. value = cap_value(value, INT_MIN, INT_MAX);
  15062. #endif
  15063. script_pushint(st, (int)value);
  15064. return SCRIPT_CMD_SUCCESS;
  15065. }
  15066. // case-insensitive substring search [lordalfa]
  15067. BUILDIN_FUNC(compare)
  15068. {
  15069. const char *message;
  15070. const char *cmpstring;
  15071. message = script_getstr(st,2);
  15072. cmpstring = script_getstr(st,3);
  15073. script_pushint(st,(stristr(message,cmpstring) != NULL));
  15074. return SCRIPT_CMD_SUCCESS;
  15075. }
  15076. BUILDIN_FUNC(strcmp)
  15077. {
  15078. const char *str1;
  15079. const char *str2;
  15080. str1 = script_getstr(st,2);
  15081. str2 = script_getstr(st,3);
  15082. script_pushint(st,strcmp(str1, str2));
  15083. return SCRIPT_CMD_SUCCESS;
  15084. }
  15085. // [zBuffer] List of mathematics commands --->
  15086. BUILDIN_FUNC(sqrt)
  15087. {
  15088. double i, a;
  15089. i = script_getnum(st,2);
  15090. a = sqrt(i);
  15091. script_pushint(st,(int)a);
  15092. return SCRIPT_CMD_SUCCESS;
  15093. }
  15094. BUILDIN_FUNC(pow)
  15095. {
  15096. double i, a, b;
  15097. a = script_getnum(st,2);
  15098. b = script_getnum(st,3);
  15099. i = pow(a,b);
  15100. script_pushint(st,(int)i);
  15101. return SCRIPT_CMD_SUCCESS;
  15102. }
  15103. BUILDIN_FUNC(distance)
  15104. {
  15105. int x0, y0, x1, y1;
  15106. x0 = script_getnum(st,2);
  15107. y0 = script_getnum(st,3);
  15108. x1 = script_getnum(st,4);
  15109. y1 = script_getnum(st,5);
  15110. script_pushint(st,distance_xy(x0,y0,x1,y1));
  15111. return SCRIPT_CMD_SUCCESS;
  15112. }
  15113. // <--- [zBuffer] List of mathematics commands
  15114. BUILDIN_FUNC(md5)
  15115. {
  15116. const char *tmpstr;
  15117. char *md5str;
  15118. tmpstr = script_getstr(st,2);
  15119. md5str = (char *)aMalloc((32+1)*sizeof(char));
  15120. MD5_String(tmpstr, md5str);
  15121. script_pushstr(st, md5str);
  15122. return SCRIPT_CMD_SUCCESS;
  15123. }
  15124. // [zBuffer] List of dynamic var commands --->
  15125. /**
  15126. * setd "<variable name>",<value>{,<char_id>};
  15127. **/
  15128. BUILDIN_FUNC(setd)
  15129. {
  15130. TBL_PC *sd = NULL;
  15131. char varname[100];
  15132. const char *buffer;
  15133. int elem;
  15134. buffer = script_getstr(st, 2);
  15135. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15136. elem = 0;
  15137. if( not_server_variable(*varname) ) {
  15138. if (!script_charid2sd(4,sd))
  15139. return SCRIPT_CMD_FAILURE;
  15140. }
  15141. if( is_string_variable(varname) ) {
  15142. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), NULL );
  15143. } else {
  15144. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), NULL );
  15145. }
  15146. return SCRIPT_CMD_SUCCESS;
  15147. }
  15148. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  15149. {
  15150. int i, j;
  15151. TBL_PC* sd = NULL;
  15152. const char* query;
  15153. struct script_data* data;
  15154. const char* name;
  15155. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  15156. int num_vars;
  15157. int num_cols;
  15158. // check target variables
  15159. for( i = 3; script_hasdata(st,i); ++i ) {
  15160. data = script_getdata(st, i);
  15161. if( data_isreference(data) ) { // it's a variable
  15162. name = reference_getname(data);
  15163. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  15164. if( !script_rid2sd(sd) ) { // no player attached
  15165. script_reportdata(data);
  15166. st->state = END;
  15167. return SCRIPT_CMD_FAILURE;
  15168. }
  15169. }
  15170. } else {
  15171. ShowError("script:query_sql: not a variable\n");
  15172. script_reportdata(data);
  15173. st->state = END;
  15174. return SCRIPT_CMD_FAILURE;
  15175. }
  15176. }
  15177. num_vars = i - 3;
  15178. // Execute the query
  15179. query = script_getstr(st,2);
  15180. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  15181. Sql_ShowDebug(handle);
  15182. script_pushint(st, -1);
  15183. return SCRIPT_CMD_FAILURE;
  15184. }
  15185. if( Sql_NumRows(handle) == 0 ) { // No data received
  15186. Sql_FreeResult(handle);
  15187. script_pushint(st, 0);
  15188. return SCRIPT_CMD_SUCCESS;
  15189. }
  15190. // Count the number of columns to store
  15191. num_cols = Sql_NumColumns(handle);
  15192. if( num_vars < num_cols ) {
  15193. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  15194. script_reportsrc(st);
  15195. } else if( num_vars > num_cols ) {
  15196. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  15197. script_reportsrc(st);
  15198. }
  15199. // Store data
  15200. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  15201. for( j = 0; j < num_vars; ++j ) {
  15202. char* str = NULL;
  15203. if( j < num_cols )
  15204. Sql_GetData(handle, j, &str, NULL);
  15205. data = script_getdata(st, j+3);
  15206. name = reference_getname(data);
  15207. if( is_string_variable(name) )
  15208. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  15209. else
  15210. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  15211. }
  15212. }
  15213. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  15214. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  15215. script_reportsrc(st);
  15216. }
  15217. // Free data
  15218. Sql_FreeResult(handle);
  15219. script_pushint(st, i);
  15220. return SCRIPT_CMD_SUCCESS;
  15221. }
  15222. BUILDIN_FUNC(query_sql) {
  15223. return buildin_query_sql_sub(st, qsmysql_handle);
  15224. }
  15225. BUILDIN_FUNC(query_logsql) {
  15226. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  15227. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  15228. script_pushint(st,-1);
  15229. return SCRIPT_CMD_FAILURE;
  15230. }
  15231. return buildin_query_sql_sub(st, logmysql_handle);
  15232. }
  15233. //Allows escaping of a given string.
  15234. BUILDIN_FUNC(escape_sql)
  15235. {
  15236. const char *str;
  15237. char *esc_str;
  15238. size_t len;
  15239. str = script_getstr(st,2);
  15240. len = strlen(str);
  15241. esc_str = (char*)aMalloc(len*2+1);
  15242. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  15243. script_pushstr(st, esc_str);
  15244. return SCRIPT_CMD_SUCCESS;
  15245. }
  15246. BUILDIN_FUNC(getd)
  15247. {
  15248. char varname[100];
  15249. const char *buffer;
  15250. int elem;
  15251. buffer = script_getstr(st, 2);
  15252. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15253. elem = 0;
  15254. // Push the 'pointer' so it's more flexible [Lance]
  15255. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  15256. return SCRIPT_CMD_SUCCESS;
  15257. }
  15258. BUILDIN_FUNC(callshop)
  15259. {
  15260. TBL_PC *sd = NULL;
  15261. struct npc_data *nd;
  15262. const char *shopname;
  15263. int flag = 0;
  15264. if (!script_rid2sd(sd)) {
  15265. script_pushint(st,0);
  15266. return SCRIPT_CMD_SUCCESS;
  15267. }
  15268. shopname = script_getstr(st, 2);
  15269. if (script_hasdata(st,3))
  15270. flag = script_getnum(st,3);
  15271. nd = npc_name2id(shopname);
  15272. 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) ) {
  15273. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  15274. script_pushint(st,0);
  15275. return SCRIPT_CMD_FAILURE;
  15276. }
  15277. if (nd->subtype == NPCTYPE_SHOP) {
  15278. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15279. sd->state.callshop = 1;
  15280. switch (flag) {
  15281. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  15282. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  15283. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  15284. }
  15285. }
  15286. #if PACKETVER >= 20131223
  15287. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15288. unsigned short i;
  15289. for (i = 0; i < nd->u.shop.count; i++) {
  15290. if (nd->u.shop.shop_item[i].qty)
  15291. break;
  15292. }
  15293. if (i == nd->u.shop.count) {
  15294. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  15295. return SCRIPT_CMD_FAILURE;
  15296. }
  15297. sd->npc_shopid = nd->bl.id;
  15298. clif_npc_market_open(sd, nd);
  15299. script_pushint(st,1);
  15300. return SCRIPT_CMD_SUCCESS;
  15301. }
  15302. #endif
  15303. else if( nd->subtype == NPCTYPE_BARTER ){
  15304. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15305. sd->state.callshop = 1;
  15306. if( nd->u.barter.extended ){
  15307. clif_barter_extended_open( *sd, *nd );
  15308. }else{
  15309. clif_barter_open( *sd, *nd );
  15310. }
  15311. }else
  15312. clif_cashshop_show(sd, nd);
  15313. sd->npc_shopid = nd->bl.id;
  15314. script_pushint(st,1);
  15315. return SCRIPT_CMD_SUCCESS;
  15316. }
  15317. BUILDIN_FUNC(npcshopitem)
  15318. {
  15319. const char* npcname = script_getstr(st, 2);
  15320. struct npc_data* nd = npc_name2id(npcname);
  15321. int n, i;
  15322. int amount;
  15323. uint16 offs = 2;
  15324. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  15325. script_pushint(st,0);
  15326. return SCRIPT_CMD_SUCCESS;
  15327. }
  15328. #if PACKETVER >= 20131223
  15329. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15330. offs = 3;
  15331. npc_market_delfromsql_(nd->exname, 0, true);
  15332. }
  15333. #endif
  15334. // get the count of new entries
  15335. amount = (script_lastdata(st)-2)/offs;
  15336. // generate new shop item list
  15337. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  15338. nd->u.shop.count = 0;
  15339. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  15340. t_itemid nameid = script_getnum( st, i );
  15341. if( !item_db.exists( nameid ) ){
  15342. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  15343. script_pushint( st, 0 );
  15344. return SCRIPT_CMD_FAILURE;
  15345. }
  15346. nd->u.shop.shop_item[n].nameid = nameid;
  15347. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  15348. #if PACKETVER >= 20131223
  15349. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15350. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  15351. nd->u.shop.shop_item[n].flag = 1;
  15352. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  15353. }
  15354. #endif
  15355. nd->u.shop.count++;
  15356. }
  15357. script_pushint(st,1);
  15358. return SCRIPT_CMD_SUCCESS;
  15359. }
  15360. BUILDIN_FUNC(npcshopadditem)
  15361. {
  15362. const char* npcname = script_getstr(st,2);
  15363. struct npc_data* nd = npc_name2id(npcname);
  15364. uint16 offs = 2, amount;
  15365. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15366. script_pushint(st,0);
  15367. return SCRIPT_CMD_SUCCESS;
  15368. }
  15369. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15370. offs = 3;
  15371. // get the count of new entries
  15372. amount = (script_lastdata(st)-2)/offs;
  15373. #if PACKETVER >= 20131223
  15374. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15375. for (int n = 0, i = 3; n < amount; n++, i += offs) {
  15376. t_itemid nameid = script_getnum(st,i);
  15377. uint16 j;
  15378. if( !item_db.exists( nameid ) ){
  15379. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15380. script_pushint( st, 0 );
  15381. return SCRIPT_CMD_FAILURE;
  15382. }
  15383. // Check existing entries
  15384. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  15385. if (j == nd->u.shop.count) {
  15386. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  15387. j = nd->u.shop.count;
  15388. nd->u.shop.shop_item[j].flag = 1;
  15389. nd->u.shop.count++;
  15390. }
  15391. int32 stock = script_getnum( st, i + 2 );
  15392. if( stock < -1 ){
  15393. ShowError( "builtin_npcshopadditem: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  15394. script_pushint( st, 0 );
  15395. return SCRIPT_CMD_FAILURE;
  15396. }
  15397. nd->u.shop.shop_item[j].nameid = nameid;
  15398. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  15399. nd->u.shop.shop_item[j].qty = stock;
  15400. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  15401. }
  15402. script_pushint(st,1);
  15403. return SCRIPT_CMD_SUCCESS;
  15404. }
  15405. #endif
  15406. // append new items to existing shop item list
  15407. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  15408. for (int n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  15409. {
  15410. t_itemid nameid = script_getnum( st, i );
  15411. if( !item_db.exists( nameid ) ){
  15412. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15413. script_pushint( st, 0 );
  15414. return SCRIPT_CMD_FAILURE;
  15415. }
  15416. nd->u.shop.shop_item[n].nameid = nameid;
  15417. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  15418. nd->u.shop.count++;
  15419. }
  15420. script_pushint(st,1);
  15421. return SCRIPT_CMD_SUCCESS;
  15422. }
  15423. BUILDIN_FUNC(npcshopdelitem)
  15424. {
  15425. const char* npcname = script_getstr(st,2);
  15426. struct npc_data* nd = npc_name2id(npcname);
  15427. int n, i, size;
  15428. unsigned short amount;
  15429. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15430. script_pushint(st,0);
  15431. return SCRIPT_CMD_SUCCESS;
  15432. }
  15433. amount = script_lastdata(st)-2;
  15434. size = nd->u.shop.count;
  15435. // remove specified items from the shop item list
  15436. for( i = 3; i < 3 + amount; i++ ) {
  15437. t_itemid nameid = script_getnum(st,i);
  15438. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  15439. if( n < size ) {
  15440. if (n+1 != size)
  15441. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  15442. #if PACKETVER >= 20131223
  15443. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15444. npc_market_delfromsql_(nd->exname, nameid, false);
  15445. #endif
  15446. size--;
  15447. }
  15448. }
  15449. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  15450. nd->u.shop.count = size;
  15451. script_pushint(st,1);
  15452. return SCRIPT_CMD_SUCCESS;
  15453. }
  15454. /**
  15455. * Sets a script to attach to a shop npc.
  15456. * npcshopattach "<npc_name>";
  15457. **/
  15458. BUILDIN_FUNC(npcshopattach)
  15459. {
  15460. const char* npcname = script_getstr(st,2);
  15461. struct npc_data* nd = npc_name2id(npcname);
  15462. int flag = 1;
  15463. if( script_hasdata(st,3) )
  15464. flag = script_getnum(st,3);
  15465. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15466. script_pushint(st,0);
  15467. return SCRIPT_CMD_SUCCESS;
  15468. }
  15469. if (flag)
  15470. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  15471. else
  15472. nd->master_nd = NULL;
  15473. script_pushint(st,1);
  15474. return SCRIPT_CMD_SUCCESS;
  15475. }
  15476. /*==========================================
  15477. * Returns some values of an item [Lupus]
  15478. * Price, Weight, etc...
  15479. setitemscript(itemID,"{new item bonus script}",[n]);
  15480. Where n:
  15481. 0 - script
  15482. 1 - Equip script
  15483. 2 - Unequip script
  15484. *------------------------------------------*/
  15485. BUILDIN_FUNC(setitemscript)
  15486. {
  15487. int n = 0;
  15488. const char *script;
  15489. struct script_code **dstscript;
  15490. t_itemid item_id = script_getnum(st,2);
  15491. script = script_getstr(st,3);
  15492. if( script_hasdata(st,4) )
  15493. n=script_getnum(st,4);
  15494. std::shared_ptr<item_data> i_data = item_db.find(item_id);
  15495. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15496. script_pushint(st,0);
  15497. return SCRIPT_CMD_SUCCESS;
  15498. }
  15499. switch (n) {
  15500. case 2:
  15501. dstscript = &i_data->unequip_script;
  15502. break;
  15503. case 1:
  15504. dstscript = &i_data->equip_script;
  15505. break;
  15506. default:
  15507. dstscript = &i_data->script;
  15508. break;
  15509. }
  15510. if(*dstscript)
  15511. script_free_code(*dstscript);
  15512. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15513. script_pushint(st,1);
  15514. return SCRIPT_CMD_SUCCESS;
  15515. }
  15516. /*=======================================================
  15517. * Add or Update a mob drop temporarily [Akinari]
  15518. * Original Idea By: [Lupus]
  15519. *
  15520. * addmonsterdrop <mob_id or name>,<item_id>,<rate>,{<steal protected>,{<random option group id>}};
  15521. *
  15522. * If given an item the mob already drops, the rate
  15523. * is updated to the new rate. Rate range is 1-10000.
  15524. * Returns true if succeeded (added/updated a mob drop) false otherwise.
  15525. *-------------------------------------------------------*/
  15526. BUILDIN_FUNC(addmonsterdrop)
  15527. {
  15528. std::shared_ptr<s_mob_db> mob;
  15529. if (script_isstring(st, 2))
  15530. mob = mob_db.find(mobdb_searchname(script_getstr(st, 2)));
  15531. else
  15532. mob = mob_db.find(script_getnum(st, 2));
  15533. if (mob == nullptr) {
  15534. if (script_isstring(st, 2))
  15535. ShowError("addmonsterdrop: bad mob name given %s\n", script_getstr(st, 2));
  15536. else
  15537. ShowError("addmonsterdrop: bad mob id given %d\n", script_getnum(st, 2));
  15538. script_pushint(st, false);
  15539. return SCRIPT_CMD_FAILURE;
  15540. }
  15541. t_itemid item_id = script_getnum(st, 3);
  15542. std::shared_ptr<item_data> itm = item_db.find(item_id);
  15543. if (itm == nullptr) {
  15544. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id);
  15545. script_pushint(st, false);
  15546. return SCRIPT_CMD_FAILURE;
  15547. }
  15548. int rate = script_getnum(st, 4);
  15549. if (rate < 1 || rate > 10000) {
  15550. ShowError("addmonsterdrop: Invalid rate %d (min 1, max 10000).\n", rate);
  15551. script_pushint(st, false);
  15552. return SCRIPT_CMD_FAILURE;
  15553. }
  15554. uint16 c = 0;
  15555. for (uint16 i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15556. if (mob->dropitem[i].nameid > 0) {
  15557. if (mob->dropitem[i].nameid == item_id) { // If it equals item_id we update that drop
  15558. c = i;
  15559. break;
  15560. }
  15561. continue;
  15562. }
  15563. if (c == 0) // Accept first available slot only
  15564. c = i;
  15565. }
  15566. if (c == 0) { // No place to put the new drop
  15567. script_pushint(st, false);
  15568. return SCRIPT_CMD_SUCCESS;
  15569. }
  15570. int steal_protected = 0;
  15571. int group = 0;
  15572. if (script_hasdata(st,5))
  15573. steal_protected = script_getnum(st, 5);
  15574. if (script_hasdata(st,6)) {
  15575. group = script_getnum(st, 6);
  15576. if (!random_option_group.exists(group)) {
  15577. ShowError("buildin_addmonsterdrop: Unknown random option group %d.\n", group);
  15578. script_pushint(st, false);
  15579. return SCRIPT_CMD_FAILURE;
  15580. }
  15581. if (itm->type != IT_WEAPON && itm->type != IT_ARMOR && itm->type != IT_SHADOWGEAR) {
  15582. ShowError("buildin_addmonsterdrop: Random option group can't be used with this type of item (item Id: %d).\n", item_id);
  15583. script_pushint(st, false);
  15584. return SCRIPT_CMD_FAILURE;
  15585. }
  15586. }
  15587. // Fill in the slot with the item and rate
  15588. mob->dropitem[c].nameid = item_id;
  15589. mob->dropitem[c].rate = rate;
  15590. mob->dropitem[c].steal_protected = steal_protected > 0;
  15591. mob->dropitem[c].randomopt_group = group;
  15592. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15593. script_pushint(st, true);
  15594. return SCRIPT_CMD_SUCCESS;
  15595. }
  15596. /*=======================================================
  15597. * Delete a mob drop temporarily [Akinari]
  15598. * Original Idea By: [Lupus]
  15599. *
  15600. * delmonsterdrop <mob_id or name>,<item_id>;
  15601. *
  15602. * Returns 1 if succeeded (deleted a mob drop)
  15603. *-------------------------------------------------------*/
  15604. BUILDIN_FUNC(delmonsterdrop)
  15605. {
  15606. std::shared_ptr<s_mob_db> mob;
  15607. if(script_isstring(st, 2))
  15608. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15609. else
  15610. mob = mob_db.find(script_getnum(st,2));
  15611. t_itemid item_id=script_getnum(st,3);
  15612. if(!item_db.exists(item_id)){
  15613. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15614. return SCRIPT_CMD_FAILURE;
  15615. }
  15616. if(mob) { //We got a valid monster, check for item drop on monster
  15617. unsigned char i;
  15618. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15619. if(mob->dropitem[i].nameid == item_id) {
  15620. mob->dropitem[i].nameid = 0;
  15621. mob->dropitem[i].rate = 0;
  15622. mob->dropitem[i].steal_protected = false;
  15623. mob->dropitem[i].randomopt_group = 0;
  15624. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15625. script_pushint(st,1);
  15626. return SCRIPT_CMD_SUCCESS;
  15627. }
  15628. }
  15629. //No drop on that monster
  15630. script_pushint(st,0);
  15631. } else {
  15632. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15633. return SCRIPT_CMD_FAILURE;
  15634. }
  15635. return SCRIPT_CMD_SUCCESS;
  15636. }
  15637. /*==========================================
  15638. * Returns a random mob_id
  15639. * type: Where to fetch from (see enum e_random_monster)
  15640. * flag: Type of checks to apply (see enum e_random_monster_flags)
  15641. * lv: Mob level to check against
  15642. *------------------------------------------*/
  15643. BUILDIN_FUNC(getrandmobid)
  15644. {
  15645. int type = script_getnum(st, 2);
  15646. if (type < MOBG_BRANCH_OF_DEAD_TREE || type >= MOBG_MAX) {
  15647. ShowWarning("buildin_getrandmobid: Invalid type %d.\n", type);
  15648. script_pushint(st, 0);
  15649. return SCRIPT_CMD_FAILURE;
  15650. }
  15651. int flag = script_hasdata(st, 3) ? script_getnum(st, 3) : RMF_MOB_NOT_BOSS;
  15652. if (flag < RMF_NONE || flag > RMF_ALL) {
  15653. ShowWarning("buildin_getrandmobid: Invalid flag %d.\n", flag);
  15654. script_pushint(st, 0);
  15655. return SCRIPT_CMD_FAILURE;
  15656. }
  15657. int lv;
  15658. if ( script_hasdata(st, 4) ) {
  15659. lv = script_getnum(st, 4);
  15660. if (lv <= 0) {
  15661. ShowWarning("buildin_getrandmobid: Invalid level %d.\n", lv);
  15662. script_pushint(st, 0);
  15663. return SCRIPT_CMD_FAILURE;
  15664. }
  15665. // If a level is provided, make sure it is respected
  15666. flag |= RMF_CHECK_MOB_LV;
  15667. } else {
  15668. lv = MAX_LEVEL;
  15669. }
  15670. script_pushint(st, mob_get_random_id(type, (enum e_random_monster_flags)flag, lv));
  15671. return SCRIPT_CMD_SUCCESS;
  15672. }
  15673. /*==========================================
  15674. * Returns some values of a monster [Lupus]
  15675. * Name, Level, race, size, etc...
  15676. getmonsterinfo(monsterID,queryIndex);
  15677. getmonsterinfo(monsterName,queryIndex);
  15678. *------------------------------------------*/
  15679. BUILDIN_FUNC(getmonsterinfo)
  15680. {
  15681. std::shared_ptr<s_mob_db> mob = nullptr;
  15682. if (script_isstring(st, 2))
  15683. mob = mobdb_search_aegisname(script_getstr(st, 2));
  15684. else {
  15685. uint16 mob_id = script_getnum(st, 2);
  15686. if (!mob_is_clone(mob_id)) {
  15687. mob = mob_db.find(mob_id);
  15688. }
  15689. }
  15690. if (mob == nullptr) {
  15691. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15692. if (script_getnum(st, 3) == MOB_NAME) // requested the name
  15693. script_pushconststr(st, "null");
  15694. else
  15695. script_pushint(st, -1);
  15696. return SCRIPT_CMD_SUCCESS;
  15697. }
  15698. switch ( script_getnum(st, 3) ) {
  15699. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15700. case MOB_LV: script_pushint(st,mob->lv); break;
  15701. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15702. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15703. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15704. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15705. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15706. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15707. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15708. case MOB_RES: script_pushint(st, mob->status.res); break;
  15709. case MOB_MRES: script_pushint(st, mob->status.mres); break;
  15710. case MOB_STR: script_pushint(st,mob->status.str); break;
  15711. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15712. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15713. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15714. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15715. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15716. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15717. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15718. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15719. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15720. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15721. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15722. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15723. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15724. case MOB_ID: script_pushint(st,mob->id); break;
  15725. default: script_pushint(st,-1); //wrong Index
  15726. }
  15727. return SCRIPT_CMD_SUCCESS;
  15728. }
  15729. /**
  15730. * Check player's vending/buyingstore/autotrade state
  15731. * checkvending({"<Player Name>"})
  15732. * @author [Nab4]
  15733. */
  15734. BUILDIN_FUNC(checkvending) {
  15735. TBL_PC *sd = NULL;
  15736. if (!script_nick2sd(2,sd) ) {
  15737. script_pushint(st,0);
  15738. return SCRIPT_CMD_SUCCESS;
  15739. }
  15740. else {
  15741. int8 ret = 0;
  15742. if (sd->state.vending)
  15743. ret = 1;
  15744. else if (sd->state.buyingstore)
  15745. ret = 4;
  15746. if (sd->state.autotrade)
  15747. ret |= 2;
  15748. script_pushint(st, ret);
  15749. }
  15750. return SCRIPT_CMD_SUCCESS;
  15751. }
  15752. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15753. {
  15754. TBL_PC *sd = NULL;
  15755. if( script_nick2sd(2,sd) )
  15756. script_pushint(st,(sd->chatID != 0));
  15757. else
  15758. script_pushint(st,0);
  15759. return SCRIPT_CMD_SUCCESS;
  15760. }
  15761. BUILDIN_FUNC(checkidle)
  15762. {
  15763. TBL_PC *sd = NULL;
  15764. if( script_nick2sd(2,sd) )
  15765. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15766. else
  15767. script_pushint(st, 0);
  15768. return SCRIPT_CMD_SUCCESS;
  15769. }
  15770. BUILDIN_FUNC(checkidlehom)
  15771. {
  15772. TBL_PC *sd = NULL;
  15773. if( script_nick2sd(2,sd) )
  15774. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15775. else
  15776. script_pushint(st, 0);
  15777. return SCRIPT_CMD_SUCCESS;
  15778. }
  15779. BUILDIN_FUNC(checkidlemer)
  15780. {
  15781. TBL_PC *sd;
  15782. if( script_nick2sd(2,sd) )
  15783. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_mer));
  15784. else
  15785. script_pushint(st, 0);
  15786. return SCRIPT_CMD_SUCCESS;
  15787. }
  15788. BUILDIN_FUNC(searchitem)
  15789. {
  15790. struct script_data* data = script_getdata(st, 2);
  15791. const char *itemname = script_getstr(st,3);
  15792. std::map<t_itemid, std::shared_ptr<item_data>> items;
  15793. int count;
  15794. char* name;
  15795. int32 start;
  15796. int32 id;
  15797. int32 i;
  15798. TBL_PC* sd = NULL;
  15799. if ((items[0] = item_db.find(strtoul(itemname, nullptr, 10))))
  15800. count = 1;
  15801. else
  15802. count = itemdb_searchname_array(items, MAX_SEARCH, itemname);
  15803. if (!count) {
  15804. script_pushint(st, 0);
  15805. return SCRIPT_CMD_SUCCESS;
  15806. }
  15807. if( !data_isreference(data) )
  15808. {
  15809. ShowError("script:searchitem: not a variable\n");
  15810. script_reportdata(data);
  15811. st->state = END;
  15812. return SCRIPT_CMD_FAILURE;// not a variable
  15813. }
  15814. id = reference_getid(data);
  15815. start = reference_getindex(data);
  15816. name = reference_getname(data);
  15817. if( not_server_variable(*name) && !script_rid2sd(sd) )
  15818. {
  15819. return SCRIPT_CMD_SUCCESS;// no player attached
  15820. }
  15821. if( is_string_variable(name) )
  15822. {// string array
  15823. ShowError("script:searchitem: not an integer array reference\n");
  15824. script_reportdata(data);
  15825. st->state = END;
  15826. return SCRIPT_CMD_FAILURE;// not supported
  15827. }
  15828. for( i = 0; i < count; ++start, ++i )
  15829. {// Set array
  15830. set_reg_num( st, sd, reference_uid( id, start ), name, items[i]->nameid, reference_getref( data ) );
  15831. }
  15832. script_pushint(st, count);
  15833. return SCRIPT_CMD_SUCCESS;
  15834. }
  15835. // [zBuffer] List of player cont commands --->
  15836. BUILDIN_FUNC(rid2name)
  15837. {
  15838. struct block_list *bl = NULL;
  15839. int rid = script_getnum(st,2);
  15840. if((bl = map_id2bl(rid)))
  15841. {
  15842. switch(bl->type) {
  15843. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15844. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15845. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15846. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15847. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15848. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name.c_str()); break;
  15849. default:
  15850. ShowError("buildin_rid2name: BL type unknown.\n");
  15851. script_pushconststr(st,"");
  15852. break;
  15853. }
  15854. } else {
  15855. ShowError("buildin_rid2name: invalid RID\n");
  15856. script_pushconststr(st,"(null)");
  15857. }
  15858. return SCRIPT_CMD_SUCCESS;
  15859. }
  15860. /**
  15861. * Toggle a unit from moving.
  15862. * pcblockmove(<unit_id>,<option>);
  15863. */
  15864. BUILDIN_FUNC(pcblockmove)
  15865. {
  15866. struct block_list *bl = NULL;
  15867. if (script_getnum(st, 2))
  15868. bl = map_id2bl(script_getnum(st,2));
  15869. else
  15870. bl = map_id2bl(st->rid);
  15871. if (bl) {
  15872. struct unit_data *ud = unit_bl2ud(bl);
  15873. if (ud)
  15874. ud->state.blockedmove = script_getnum(st,3) > 0;
  15875. }
  15876. return SCRIPT_CMD_SUCCESS;
  15877. }
  15878. /**
  15879. * Toggle a unit from casting skills.
  15880. * pcblockskill(<unit_id>,<option>);
  15881. */
  15882. BUILDIN_FUNC(pcblockskill)
  15883. {
  15884. struct block_list *bl = NULL;
  15885. if (script_getnum(st, 2))
  15886. bl = map_id2bl(script_getnum(st,2));
  15887. else
  15888. bl = map_id2bl(st->rid);
  15889. if (bl) {
  15890. struct unit_data *ud = unit_bl2ud(bl);
  15891. if (ud)
  15892. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15893. }
  15894. return SCRIPT_CMD_SUCCESS;
  15895. }
  15896. BUILDIN_FUNC(setpcblock)
  15897. {
  15898. TBL_PC *sd;
  15899. if (script_mapid2sd(4, sd)) {
  15900. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  15901. if (script_getnum(st, 3) > 0)
  15902. sd->state.block_action |= type;
  15903. else
  15904. sd->state.block_action &= ~type;
  15905. }
  15906. return SCRIPT_CMD_SUCCESS;
  15907. }
  15908. BUILDIN_FUNC(getpcblock)
  15909. {
  15910. TBL_PC *sd;
  15911. if (script_mapid2sd(2, sd))
  15912. script_pushint(st, sd->state.block_action);
  15913. else
  15914. script_pushint(st, 0);
  15915. return SCRIPT_CMD_SUCCESS;
  15916. }
  15917. BUILDIN_FUNC(pcfollow)
  15918. {
  15919. TBL_PC *sd;
  15920. if( script_mapid2sd(2,sd) )
  15921. pc_follow(sd, script_getnum(st,3));
  15922. return SCRIPT_CMD_SUCCESS;
  15923. }
  15924. BUILDIN_FUNC(pcstopfollow)
  15925. {
  15926. TBL_PC *sd;
  15927. if(script_mapid2sd(2,sd))
  15928. pc_stop_following(sd);
  15929. return SCRIPT_CMD_SUCCESS;
  15930. }
  15931. // <--- [zBuffer] List of player cont commands
  15932. // [zBuffer] List of unit control commands --->
  15933. /// Checks to see if the unit exists.
  15934. ///
  15935. /// unitexists <unit id>;
  15936. BUILDIN_FUNC(unitexists)
  15937. {
  15938. struct block_list* bl;
  15939. bl = map_id2bl(script_getnum(st, 2));
  15940. if (!bl)
  15941. script_pushint(st, false);
  15942. else
  15943. script_pushint(st, true);
  15944. return SCRIPT_CMD_SUCCESS;
  15945. }
  15946. /// Gets the type of the given Game ID.
  15947. ///
  15948. /// getunittype <unit id>;
  15949. BUILDIN_FUNC(getunittype)
  15950. {
  15951. struct block_list* bl;
  15952. int value = 0;
  15953. if(!script_rid2bl(2,bl))
  15954. {
  15955. script_pushint(st, -1);
  15956. return SCRIPT_CMD_FAILURE;
  15957. }
  15958. switch (bl->type) {
  15959. case BL_PC:
  15960. case BL_NPC:
  15961. case BL_PET:
  15962. case BL_MOB:
  15963. case BL_HOM:
  15964. case BL_MER:
  15965. case BL_ELEM:
  15966. value = bl->type;
  15967. break;
  15968. default:
  15969. value = -1;
  15970. break;
  15971. }
  15972. script_pushint(st, value);
  15973. return SCRIPT_CMD_SUCCESS;
  15974. }
  15975. /// Gets specific live information of a bl.
  15976. ///
  15977. /// getunitdata <unit id>,<arrayname>;
  15978. BUILDIN_FUNC(getunitdata)
  15979. {
  15980. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15981. struct block_list* bl;
  15982. TBL_MOB* md = NULL;
  15983. TBL_HOM* hd = NULL;
  15984. TBL_MER* mc = NULL;
  15985. TBL_PET* pd = NULL;
  15986. TBL_ELEM* ed = NULL;
  15987. TBL_NPC* nd = NULL;
  15988. char* name;
  15989. struct script_data *data = script_getdata(st, 3);
  15990. if (!data_isreference(data)) {
  15991. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15992. return SCRIPT_CMD_FAILURE;
  15993. }
  15994. if(!script_rid2bl(2,bl))
  15995. {
  15996. script_pushint(st, -1);
  15997. return SCRIPT_CMD_FAILURE;
  15998. }
  15999. switch (bl->type) {
  16000. case BL_MOB: md = map_id2md(bl->id); break;
  16001. case BL_HOM: hd = map_id2hd(bl->id); break;
  16002. case BL_PET: pd = map_id2pd(bl->id); break;
  16003. case BL_MER: mc = map_id2mc(bl->id); break;
  16004. case BL_ELEM: ed = map_id2ed(bl->id); break;
  16005. case BL_NPC: nd = map_id2nd(bl->id); break;
  16006. default:
  16007. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  16008. return SCRIPT_CMD_FAILURE;
  16009. }
  16010. name = reference_getname(data);
  16011. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  16012. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  16013. return SCRIPT_CMD_FAILURE;
  16014. }
  16015. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  16016. switch(bl->type) {
  16017. case BL_MOB:
  16018. if (!md) {
  16019. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  16020. return SCRIPT_CMD_FAILURE;
  16021. }
  16022. getunitdata_sub(UMOB_SIZE, md->status.size);
  16023. getunitdata_sub(UMOB_LEVEL, md->level);
  16024. getunitdata_sub(UMOB_HP, md->status.hp);
  16025. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  16026. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  16027. getunitdata_sub(UMOB_MAPID, md->bl.m);
  16028. getunitdata_sub(UMOB_X, md->bl.x);
  16029. getunitdata_sub(UMOB_Y, md->bl.y);
  16030. getunitdata_sub(UMOB_SPEED, md->status.speed);
  16031. getunitdata_sub(UMOB_MODE, md->status.mode);
  16032. getunitdata_sub(UMOB_AI, md->special_state.ai);
  16033. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  16034. getunitdata_sub(UMOB_SEX, md->vd->sex);
  16035. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  16036. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  16037. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  16038. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  16039. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  16040. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  16041. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  16042. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  16043. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  16044. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  16045. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  16046. getunitdata_sub(UMOB_STR, md->status.str);
  16047. getunitdata_sub(UMOB_AGI, md->status.agi);
  16048. getunitdata_sub(UMOB_VIT, md->status.vit);
  16049. getunitdata_sub(UMOB_INT, md->status.int_);
  16050. getunitdata_sub(UMOB_DEX, md->status.dex);
  16051. getunitdata_sub(UMOB_LUK, md->status.luk);
  16052. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  16053. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  16054. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  16055. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  16056. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  16057. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  16058. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  16059. getunitdata_sub(UMOB_DEF, md->status.def);
  16060. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  16061. getunitdata_sub(UMOB_HIT, md->status.hit);
  16062. getunitdata_sub(UMOB_FLEE, md->status.flee);
  16063. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  16064. getunitdata_sub(UMOB_CRIT, md->status.cri);
  16065. getunitdata_sub(UMOB_RACE, md->status.race);
  16066. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  16067. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  16068. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  16069. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  16070. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  16071. getunitdata_sub(UMOB_TARGETID, md->target_id);
  16072. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  16073. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  16074. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  16075. getunitdata_sub(UMOB_IGNORE_CELL_STACK_LIMIT, md->ud.state.ignore_cell_stack_limit);
  16076. getunitdata_sub(UMOB_RES, md->status.res);
  16077. getunitdata_sub(UMOB_MRES, md->status.mres);
  16078. getunitdata_sub(UMOB_DAMAGETAKEN, md->damagetaken);
  16079. break;
  16080. case BL_HOM:
  16081. if (!hd) {
  16082. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  16083. return SCRIPT_CMD_FAILURE;
  16084. }
  16085. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  16086. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  16087. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  16088. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  16089. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  16090. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  16091. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  16092. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  16093. getunitdata_sub(UHOM_X, hd->bl.x);
  16094. getunitdata_sub(UHOM_Y, hd->bl.y);
  16095. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  16096. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  16097. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  16098. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  16099. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  16100. getunitdata_sub(UHOM_STR, hd->base_status.str);
  16101. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  16102. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  16103. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  16104. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  16105. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  16106. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  16107. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  16108. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  16109. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  16110. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  16111. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  16112. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  16113. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  16114. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  16115. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  16116. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  16117. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  16118. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  16119. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  16120. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  16121. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  16122. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  16123. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  16124. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  16125. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  16126. break;
  16127. case BL_PET:
  16128. if (!pd) {
  16129. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  16130. return SCRIPT_CMD_FAILURE;
  16131. }
  16132. getunitdata_sub(UPET_SIZE, pd->status.size);
  16133. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  16134. getunitdata_sub(UPET_HP, pd->status.hp);
  16135. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  16136. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  16137. getunitdata_sub(UPET_MAPID, pd->bl.m);
  16138. getunitdata_sub(UPET_X, pd->bl.x);
  16139. getunitdata_sub(UPET_Y, pd->bl.y);
  16140. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  16141. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  16142. getunitdata_sub(UPET_SPEED, pd->status.speed);
  16143. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  16144. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  16145. getunitdata_sub(UPET_STR, pd->status.str);
  16146. getunitdata_sub(UPET_AGI, pd->status.agi);
  16147. getunitdata_sub(UPET_VIT, pd->status.vit);
  16148. getunitdata_sub(UPET_INT, pd->status.int_);
  16149. getunitdata_sub(UPET_DEX, pd->status.dex);
  16150. getunitdata_sub(UPET_LUK, pd->status.luk);
  16151. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  16152. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  16153. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  16154. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  16155. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  16156. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  16157. getunitdata_sub(UPET_DEF, pd->status.def);
  16158. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  16159. getunitdata_sub(UPET_HIT, pd->status.hit);
  16160. getunitdata_sub(UPET_FLEE, pd->status.flee);
  16161. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  16162. getunitdata_sub(UPET_CRIT, pd->status.cri);
  16163. getunitdata_sub(UPET_RACE, pd->status.race);
  16164. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  16165. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  16166. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  16167. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  16168. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  16169. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  16170. break;
  16171. case BL_MER:
  16172. if (!mc) {
  16173. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  16174. return SCRIPT_CMD_FAILURE;
  16175. }
  16176. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  16177. getunitdata_sub(UMER_HP, mc->base_status.hp);
  16178. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  16179. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  16180. getunitdata_sub(UMER_MAPID, mc->bl.m);
  16181. getunitdata_sub(UMER_X, mc->bl.x);
  16182. getunitdata_sub(UMER_Y, mc->bl.y);
  16183. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  16184. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  16185. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  16186. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  16187. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  16188. getunitdata_sub(UMER_STR, mc->base_status.str);
  16189. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  16190. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  16191. getunitdata_sub(UMER_INT, mc->base_status.int_);
  16192. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  16193. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  16194. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  16195. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  16196. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  16197. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  16198. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  16199. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  16200. getunitdata_sub(UMER_DEF, mc->base_status.def);
  16201. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  16202. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  16203. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  16204. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  16205. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  16206. getunitdata_sub(UMER_RACE, mc->base_status.race);
  16207. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  16208. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  16209. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  16210. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  16211. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  16212. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  16213. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  16214. break;
  16215. case BL_ELEM:
  16216. if (!ed) {
  16217. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  16218. return SCRIPT_CMD_FAILURE;
  16219. }
  16220. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  16221. getunitdata_sub(UELE_HP, ed->elemental.hp);
  16222. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  16223. getunitdata_sub(UELE_SP, ed->elemental.sp);
  16224. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  16225. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  16226. getunitdata_sub(UELE_MAPID, ed->bl.m);
  16227. getunitdata_sub(UELE_X, ed->bl.x);
  16228. getunitdata_sub(UELE_Y, ed->bl.y);
  16229. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  16230. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  16231. getunitdata_sub(UELE_SP, ed->base_status.speed);
  16232. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  16233. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  16234. getunitdata_sub(UELE_STR, ed->base_status.str);
  16235. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  16236. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  16237. getunitdata_sub(UELE_INT, ed->base_status.int_);
  16238. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  16239. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  16240. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  16241. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  16242. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  16243. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  16244. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  16245. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  16246. getunitdata_sub(UELE_DEF, ed->base_status.def);
  16247. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  16248. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  16249. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  16250. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  16251. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  16252. getunitdata_sub(UELE_RACE, ed->base_status.race);
  16253. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  16254. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  16255. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  16256. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  16257. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  16258. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  16259. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  16260. break;
  16261. case BL_NPC:
  16262. if (!nd) {
  16263. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  16264. return SCRIPT_CMD_FAILURE;
  16265. }
  16266. getunitdata_sub(UNPC_LEVEL, nd->level);
  16267. getunitdata_sub(UNPC_HP, nd->status.hp);
  16268. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  16269. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  16270. getunitdata_sub(UNPC_X, nd->bl.x);
  16271. getunitdata_sub(UNPC_Y, nd->bl.y);
  16272. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  16273. getunitdata_sub(UNPC_STR, nd->status.str);
  16274. getunitdata_sub(UNPC_AGI, nd->status.agi);
  16275. getunitdata_sub(UNPC_VIT, nd->status.vit);
  16276. getunitdata_sub(UNPC_INT, nd->status.int_);
  16277. getunitdata_sub(UNPC_DEX, nd->status.dex);
  16278. getunitdata_sub(UNPC_LUK, nd->status.luk);
  16279. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  16280. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  16281. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  16282. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  16283. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  16284. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  16285. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  16286. getunitdata_sub(UNPC_DEF, nd->status.def);
  16287. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  16288. getunitdata_sub(UNPC_HIT, nd->status.hit);
  16289. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  16290. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  16291. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  16292. getunitdata_sub(UNPC_RACE, nd->status.race);
  16293. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  16294. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  16295. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  16296. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  16297. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  16298. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  16299. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  16300. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  16301. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  16302. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  16303. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  16304. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  16305. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  16306. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  16307. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  16308. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  16309. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  16310. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  16311. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  16312. break;
  16313. default:
  16314. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  16315. return SCRIPT_CMD_FAILURE;
  16316. }
  16317. #undef getunitdata_sub
  16318. return SCRIPT_CMD_SUCCESS;
  16319. }
  16320. /// Changes the live data of a bl.
  16321. ///
  16322. /// setunitdata <unit id>,<type>,<value>;
  16323. BUILDIN_FUNC(setunitdata)
  16324. {
  16325. struct block_list* bl;
  16326. if(!script_rid2bl(2,bl))
  16327. {
  16328. script_pushint(st, -1);
  16329. return SCRIPT_CMD_FAILURE;
  16330. }
  16331. TBL_MOB* md;
  16332. TBL_HOM* hd;
  16333. TBL_MER* mc;
  16334. TBL_PET* pd;
  16335. TBL_ELEM* ed;
  16336. TBL_NPC* nd;
  16337. switch (bl->type) {
  16338. case BL_MOB: md = map_id2md(bl->id); break;
  16339. case BL_HOM: hd = map_id2hd(bl->id); break;
  16340. case BL_PET: pd = map_id2pd(bl->id); break;
  16341. case BL_MER: mc = map_id2mc(bl->id); break;
  16342. case BL_ELEM: ed = map_id2ed(bl->id); break;
  16343. case BL_NPC:
  16344. nd = map_id2nd(bl->id);
  16345. if (!nd->status.hp)
  16346. status_calc_npc(nd, SCO_FIRST);
  16347. else
  16348. status_calc_npc(nd, SCO_NONE);
  16349. break;
  16350. default:
  16351. ShowError("buildin_setunitdata: Invalid object!\n");
  16352. return SCRIPT_CMD_FAILURE;
  16353. }
  16354. int type = script_getnum(st, 3), value = 0;
  16355. const char *mapname = NULL;
  16356. bool calc_status = false;
  16357. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  16358. mapname = script_getstr(st, 4);
  16359. else
  16360. value = script_getnum(st, 4);
  16361. switch (bl->type) {
  16362. case BL_MOB:
  16363. if (!md) {
  16364. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  16365. return SCRIPT_CMD_FAILURE;
  16366. }
  16367. if (!md->base_status) {
  16368. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  16369. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  16370. }
  16371. // Check if the view data will be modified
  16372. switch( type ){
  16373. case UMOB_SEX:
  16374. //case UMOB_CLASS: // Called by status_set_viewdata
  16375. case UMOB_HAIRSTYLE:
  16376. case UMOB_HAIRCOLOR:
  16377. case UMOB_HEADBOTTOM:
  16378. case UMOB_HEADMIDDLE:
  16379. case UMOB_HEADTOP:
  16380. case UMOB_CLOTHCOLOR:
  16381. case UMOB_SHIELD:
  16382. case UMOB_WEAPON:
  16383. case UMOB_ROBE:
  16384. case UMOB_BODY2:
  16385. mob_set_dynamic_viewdata( md );
  16386. break;
  16387. }
  16388. switch (type) {
  16389. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  16390. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  16391. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  16392. 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;
  16393. case UMOB_MASTERAID: md->master_id = value; break;
  16394. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16395. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16396. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  16397. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16398. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  16399. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  16400. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  16401. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  16402. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  16403. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16404. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16405. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16406. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16407. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16408. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16409. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16410. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16411. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16412. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16413. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16414. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16415. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16416. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16417. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16418. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16419. case UMOB_SLAVECPYMSTRMD:
  16420. if (value > 0) {
  16421. TBL_MOB *md2 = NULL;
  16422. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  16423. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  16424. break;
  16425. }
  16426. md->base_status->mode = md2->status.mode;
  16427. md->state.copy_master_mode = 1;
  16428. } else
  16429. md->state.copy_master_mode = 0;
  16430. calc_status = true;
  16431. break;
  16432. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  16433. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  16434. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  16435. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16436. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  16437. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  16438. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  16439. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  16440. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  16441. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  16442. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  16443. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  16444. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  16445. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  16446. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  16447. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  16448. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  16449. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  16450. case UMOB_TARGETID: {
  16451. if (value==0) {
  16452. mob_unlocktarget(md,gettick());
  16453. break;
  16454. }
  16455. struct block_list* target = map_id2bl(value);
  16456. if (!target) {
  16457. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  16458. return SCRIPT_CMD_FAILURE;
  16459. }
  16460. mob_target(md,target,0);
  16461. break;
  16462. }
  16463. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16464. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16465. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  16466. case UMOB_IGNORE_CELL_STACK_LIMIT: md->ud.state.ignore_cell_stack_limit = value > 0; break;
  16467. case UMOB_RES: md->base_status->res = (short)value; calc_status = true; break;
  16468. case UMOB_MRES: md->base_status->mres = (short)value; calc_status = true; break;
  16469. case UMOB_DAMAGETAKEN: md->damagetaken = (unsigned short)value; break;
  16470. default:
  16471. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  16472. return SCRIPT_CMD_FAILURE;
  16473. }
  16474. if (calc_status)
  16475. status_calc_bl_(&md->bl, status_db.getSCB_BATTLE());
  16476. break;
  16477. case BL_HOM:
  16478. if (!hd) {
  16479. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  16480. return SCRIPT_CMD_FAILURE;
  16481. }
  16482. switch (type) {
  16483. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  16484. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  16485. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16486. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16487. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16488. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16489. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  16490. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16491. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  16492. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  16493. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  16494. 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;
  16495. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16496. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16497. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16498. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16499. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16500. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16501. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16502. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16503. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16504. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  16505. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16506. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16507. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16508. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16509. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16510. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  16511. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  16512. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  16513. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  16514. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  16515. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  16516. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  16517. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16518. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16519. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  16520. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  16521. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  16522. case UHOM_TARGETID: {
  16523. if (value==0) {
  16524. unit_stop_attack(&hd->bl);
  16525. break;
  16526. }
  16527. struct block_list* target = map_id2bl(value);
  16528. if (!target) {
  16529. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  16530. return SCRIPT_CMD_FAILURE;
  16531. }
  16532. unit_attack(&hd->bl, target->id, 1);
  16533. break;
  16534. }
  16535. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  16536. default:
  16537. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  16538. return SCRIPT_CMD_FAILURE;
  16539. }
  16540. if (calc_status)
  16541. status_calc_bl_(&hd->bl, status_db.getSCB_BATTLE());
  16542. break;
  16543. case BL_PET:
  16544. if (!pd) {
  16545. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  16546. return SCRIPT_CMD_FAILURE;
  16547. }
  16548. switch (type) {
  16549. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  16550. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  16551. case UPET_HP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16552. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16553. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  16554. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16555. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16556. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  16557. 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;
  16558. 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;
  16559. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16560. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16561. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16562. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16563. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16564. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16565. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16566. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16567. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16568. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  16569. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  16570. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  16571. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  16572. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  16573. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  16574. case UPET_DEF: pd->status.def = (defType)value; break;
  16575. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  16576. case UPET_HIT: pd->status.hit = (short)value; break;
  16577. case UPET_FLEE: pd->status.flee = (short)value; break;
  16578. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16579. case UPET_CRIT: pd->status.cri = (short)value; break;
  16580. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16581. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16582. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16583. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16584. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16585. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16586. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16587. default:
  16588. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16589. return SCRIPT_CMD_FAILURE;
  16590. }
  16591. break;
  16592. case BL_MER:
  16593. if (!mc) {
  16594. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  16595. return SCRIPT_CMD_FAILURE;
  16596. }
  16597. switch (type) {
  16598. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16599. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16600. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16601. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16602. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16603. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16604. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16605. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  16606. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  16607. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16608. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16609. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16610. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16611. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16612. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16613. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16614. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16615. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16616. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16617. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16618. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16619. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16620. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16621. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16622. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16623. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16624. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16625. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16626. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16627. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16628. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16629. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16630. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16631. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16632. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16633. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16634. case UMER_TARGETID: {
  16635. if (value==0) {
  16636. unit_stop_attack(&mc->bl);
  16637. break;
  16638. }
  16639. struct block_list* target = map_id2bl(value);
  16640. if (!target) {
  16641. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16642. return SCRIPT_CMD_FAILURE;
  16643. }
  16644. unit_attack(&mc->bl, target->id, 1);
  16645. break;
  16646. }
  16647. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16648. default:
  16649. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16650. return SCRIPT_CMD_FAILURE;
  16651. }
  16652. if (calc_status)
  16653. status_calc_bl_(&mc->bl, status_db.getSCB_BATTLE());
  16654. break;
  16655. case BL_ELEM:
  16656. if (!ed) {
  16657. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16658. return SCRIPT_CMD_FAILURE;
  16659. }
  16660. switch (type) {
  16661. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16662. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16663. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16664. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16665. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16666. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16667. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16668. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16669. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16670. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16671. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16672. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16673. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16674. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16675. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16676. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16677. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16678. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16679. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16680. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16681. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16682. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16683. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16684. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16685. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16686. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16687. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16688. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16689. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16690. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16691. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16692. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16693. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16694. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16695. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16696. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16697. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16698. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16699. case UELE_TARGETID: {
  16700. if (value==0) {
  16701. unit_stop_attack(&ed->bl);
  16702. break;
  16703. }
  16704. struct block_list* target = map_id2bl(value);
  16705. if (!target) {
  16706. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16707. return SCRIPT_CMD_FAILURE;
  16708. }
  16709. elemental_change_mode(ed, EL_MODE_AGGRESSIVE);
  16710. unit_attack(&ed->bl, target->id, 1);
  16711. break;
  16712. }
  16713. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16714. default:
  16715. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16716. return SCRIPT_CMD_FAILURE;
  16717. }
  16718. if (calc_status)
  16719. status_calc_bl_(&ed->bl, status_db.getSCB_BATTLE());
  16720. break;
  16721. case BL_NPC:
  16722. if (!nd) {
  16723. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16724. return SCRIPT_CMD_FAILURE;
  16725. }
  16726. switch (type) {
  16727. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16728. case UNPC_HP: nd->status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16729. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16730. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16731. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16732. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16733. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16734. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16735. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16736. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16737. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16738. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16739. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16740. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16741. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16742. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16743. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16744. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16745. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16746. case UNPC_DEF: nd->status.def = (defType)value; break;
  16747. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16748. case UNPC_HIT: nd->status.hit = (short)value; break;
  16749. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16750. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16751. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16752. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16753. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16754. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16755. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16756. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16757. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16758. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16759. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16760. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16761. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16762. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16763. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16764. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16765. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16766. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16767. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16768. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16769. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16770. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16771. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16772. default:
  16773. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16774. return SCRIPT_CMD_FAILURE;
  16775. }
  16776. break;
  16777. default:
  16778. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16779. return SCRIPT_CMD_FAILURE;
  16780. }
  16781. // Client information updates
  16782. switch (bl->type) {
  16783. case BL_HOM:
  16784. clif_send_homdata(hd->master, SP_ACK, 0);
  16785. break;
  16786. case BL_PET:
  16787. clif_send_petstatus(pd->master);
  16788. break;
  16789. case BL_MER:
  16790. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16791. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16792. break;
  16793. case BL_ELEM:
  16794. clif_elemental_info(ed->master);
  16795. break;
  16796. }
  16797. return SCRIPT_CMD_SUCCESS;
  16798. }
  16799. /// Gets the name of a bl.
  16800. /// Supported types are [MOB|HOM|PET|NPC].
  16801. /// MER and ELEM don't support custom names.
  16802. ///
  16803. /// getunitname <unit id>;
  16804. BUILDIN_FUNC(getunitname)
  16805. {
  16806. struct block_list* bl = NULL;
  16807. if(!script_rid2bl(2,bl)){
  16808. script_pushconststr(st, "Unknown");
  16809. return SCRIPT_CMD_FAILURE;
  16810. }
  16811. script_pushstrcopy(st, status_get_name(bl));
  16812. return SCRIPT_CMD_SUCCESS;
  16813. }
  16814. /// Changes the name of a bl.
  16815. /// Supported types are [MOB|HOM|PET].
  16816. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16817. ///
  16818. /// setunitname <unit id>,<name>;
  16819. BUILDIN_FUNC(setunitname)
  16820. {
  16821. struct block_list* bl = NULL;
  16822. TBL_MOB* md = NULL;
  16823. TBL_HOM* hd = NULL;
  16824. TBL_PET* pd = NULL;
  16825. if(!script_rid2bl(2,bl))
  16826. {
  16827. script_pushconststr(st, "Unknown");
  16828. return SCRIPT_CMD_FAILURE;
  16829. }
  16830. switch (bl->type) {
  16831. case BL_MOB: md = map_id2md(bl->id); break;
  16832. case BL_HOM: hd = map_id2hd(bl->id); break;
  16833. case BL_PET: pd = map_id2pd(bl->id); break;
  16834. default:
  16835. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16836. return SCRIPT_CMD_FAILURE;
  16837. }
  16838. switch (bl->type) {
  16839. case BL_MOB:
  16840. if (!md) {
  16841. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16842. return SCRIPT_CMD_FAILURE;
  16843. }
  16844. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16845. break;
  16846. case BL_HOM:
  16847. if (!hd) {
  16848. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16849. return SCRIPT_CMD_FAILURE;
  16850. }
  16851. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16852. break;
  16853. case BL_PET:
  16854. if (!pd) {
  16855. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16856. return SCRIPT_CMD_FAILURE;
  16857. }
  16858. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16859. break;
  16860. default:
  16861. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16862. return SCRIPT_CMD_FAILURE;
  16863. }
  16864. clif_name_area(bl); // Send update to client.
  16865. return SCRIPT_CMD_SUCCESS;
  16866. }
  16867. /**
  16868. * Sets a unit's title.
  16869. * setunittitle <GID>,<title>;
  16870. */
  16871. BUILDIN_FUNC(setunittitle)
  16872. {
  16873. int gid = script_getnum(st, 2);
  16874. block_list *bl = map_id2bl(gid);
  16875. if (bl == nullptr) {
  16876. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16877. return SCRIPT_CMD_FAILURE;
  16878. }
  16879. unit_data *ud = unit_bl2ud(bl);
  16880. if (ud == nullptr) {
  16881. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16882. return SCRIPT_CMD_FAILURE;
  16883. }
  16884. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16885. clif_name_area(bl);
  16886. return SCRIPT_CMD_SUCCESS;
  16887. }
  16888. /**
  16889. * Gets a unit's title.
  16890. * getunittitle <GID>;
  16891. */
  16892. BUILDIN_FUNC(getunittitle)
  16893. {
  16894. int gid = script_getnum(st, 2);
  16895. block_list *bl = map_id2bl(gid);
  16896. if (bl == nullptr) {
  16897. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16898. return SCRIPT_CMD_FAILURE;
  16899. }
  16900. unit_data *ud = unit_bl2ud(bl);
  16901. if (ud == nullptr) {
  16902. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16903. return SCRIPT_CMD_FAILURE;
  16904. }
  16905. script_pushstrcopy(st, ud->title);
  16906. return SCRIPT_CMD_SUCCESS;
  16907. }
  16908. /// Makes the unit walk to target position or map.
  16909. /// Returns if it was successful.
  16910. ///
  16911. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16912. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16913. BUILDIN_FUNC(unitwalk)
  16914. {
  16915. struct block_list* bl;
  16916. struct unit_data *ud = NULL;
  16917. const char *cmd = script_getfuncname(st), *done_label = "";
  16918. uint8 off = 5;
  16919. if(!script_rid2bl(2,bl))
  16920. {
  16921. script_pushint(st, 0);
  16922. return SCRIPT_CMD_FAILURE;
  16923. }
  16924. ud = unit_bl2ud(bl);
  16925. // Unit was already forced to walk.
  16926. if (ud != nullptr && ud->state.force_walk) {
  16927. script_pushint(st, 0);
  16928. ShowWarning("buildin_%s: Unit has already been forced to walk and not reached it's destination yet.\n", cmd);
  16929. return SCRIPT_CMD_FAILURE;
  16930. }
  16931. if (bl->type == BL_NPC) {
  16932. if (!((TBL_NPC*)bl)->status.hp)
  16933. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16934. else
  16935. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16936. }
  16937. if (!strcmp(cmd,"unitwalk")) {
  16938. int x = script_getnum(st,3);
  16939. int y = script_getnum(st,4);
  16940. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0))) {
  16941. if (ud != nullptr)
  16942. ud->state.force_walk = true;
  16943. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  16944. }
  16945. } else {
  16946. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  16947. if (!tbl) {
  16948. ShowError("buildin_unitwalk: Bad target destination.\n");
  16949. script_pushint(st, 0);
  16950. return SCRIPT_CMD_FAILURE;
  16951. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL))) {
  16952. if (ud != nullptr)
  16953. ud->state.force_walk = true;
  16954. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  16955. }
  16956. off = 4;
  16957. }
  16958. if (ud && script_hasdata(st, off)) {
  16959. done_label = script_getstr(st, off);
  16960. check_event(st, done_label);
  16961. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  16962. }
  16963. return SCRIPT_CMD_SUCCESS;
  16964. }
  16965. /// Kills the unit.
  16966. ///
  16967. /// unitkill <unit_id>;
  16968. BUILDIN_FUNC(unitkill)
  16969. {
  16970. struct block_list* bl;
  16971. if(script_rid2bl(2,bl))
  16972. status_kill(bl);
  16973. return SCRIPT_CMD_SUCCESS;
  16974. }
  16975. /// Warps the unit to the target position in the target map.
  16976. /// Returns if it was successful.
  16977. ///
  16978. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  16979. BUILDIN_FUNC(unitwarp)
  16980. {
  16981. int map_idx;
  16982. short x;
  16983. short y;
  16984. struct block_list* bl;
  16985. const char *mapname;
  16986. mapname = script_getstr(st, 3);
  16987. x = (short)script_getnum(st,4);
  16988. y = (short)script_getnum(st,5);
  16989. if(!script_rid2bl(2,bl))
  16990. {
  16991. script_pushint(st, 0);
  16992. return SCRIPT_CMD_SUCCESS;
  16993. }
  16994. if (!strcmp(mapname,"this"))
  16995. map_idx = bl?bl->m:-1;
  16996. else
  16997. map_idx = map_mapname2mapid(mapname);
  16998. if (map_idx >= 0 && bl != NULL)
  16999. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  17000. else
  17001. script_pushint(st, 0);
  17002. return SCRIPT_CMD_SUCCESS;
  17003. }
  17004. /// Makes the unit attack the target.
  17005. /// If the unit is a player and <action type> is not 0, it does a continuous
  17006. /// attack instead of a single attack.
  17007. /// Returns if the request was successful.
  17008. ///
  17009. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  17010. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  17011. BUILDIN_FUNC(unitattack)
  17012. {
  17013. struct block_list* unit_bl;
  17014. struct block_list* target_bl = NULL;
  17015. int actiontype = 0;
  17016. if (!script_rid2bl(2,unit_bl)) {
  17017. script_pushint(st, false);
  17018. return SCRIPT_CMD_FAILURE;
  17019. }
  17020. if (script_isstring(st, 3)) {
  17021. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  17022. if( sd != NULL )
  17023. target_bl = &sd->bl;
  17024. } else
  17025. target_bl = map_id2bl(script_getnum(st, 3));
  17026. if (!target_bl) {
  17027. script_pushint(st, false);
  17028. return SCRIPT_CMD_FAILURE;
  17029. }
  17030. if (script_hasdata(st,4))
  17031. actiontype = script_getnum(st,4);
  17032. switch(unit_bl->type) {
  17033. case BL_PC: {
  17034. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  17035. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  17036. script_pushint(st, sd->ud.target == target_bl->id);
  17037. return SCRIPT_CMD_SUCCESS;
  17038. }
  17039. case BL_MOB:
  17040. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  17041. break;
  17042. case BL_PET:
  17043. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  17044. break;
  17045. default:
  17046. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  17047. script_pushint(st, false);
  17048. return SCRIPT_CMD_FAILURE;
  17049. }
  17050. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  17051. return SCRIPT_CMD_SUCCESS;
  17052. }
  17053. /// Makes the unit stop attacking.
  17054. ///
  17055. /// unitstopattack <unit_id>;
  17056. BUILDIN_FUNC(unitstopattack)
  17057. {
  17058. struct block_list* bl;
  17059. if(script_rid2bl(2,bl))
  17060. {
  17061. unit_stop_attack(bl);
  17062. if (bl->type == BL_MOB)
  17063. ((TBL_MOB*)bl)->target_id = 0;
  17064. }
  17065. return SCRIPT_CMD_SUCCESS;
  17066. }
  17067. /// Makes the unit stop walking.
  17068. ///
  17069. /// unitstopwalk <unit_id>{,<flag>};
  17070. BUILDIN_FUNC(unitstopwalk)
  17071. {
  17072. struct block_list* bl;
  17073. int flag = USW_NONE;
  17074. if (script_hasdata(st, 3))
  17075. flag = script_getnum(st, 3);
  17076. if(script_rid2bl(2,bl)) {
  17077. unit_data *ud = unit_bl2ud(bl);
  17078. if (ud != nullptr)
  17079. ud->state.force_walk = false;
  17080. if (unit_stop_walking(bl, flag) == 0 && flag != USW_FORCE_STOP) {
  17081. ShowWarning("buildin_unitstopwalk: Unable to find unit or unit is not walking.\n");
  17082. return SCRIPT_CMD_FAILURE;
  17083. }
  17084. return SCRIPT_CMD_SUCCESS;
  17085. } else {
  17086. return SCRIPT_CMD_FAILURE;
  17087. }
  17088. }
  17089. /**
  17090. * Makes the unit say the given message.
  17091. *
  17092. * unittalk <unit_id>,"<message>"{,"<flag>"};
  17093. * @param flag: Specify target
  17094. * bc_area - Message is sent to players in the vicinity of the source (default).
  17095. * bc_self - Message is sent only to player attached.
  17096. */
  17097. BUILDIN_FUNC(unittalk)
  17098. {
  17099. const char* message;
  17100. struct block_list* bl;
  17101. message = script_getstr(st, 3);
  17102. if(script_rid2bl(2,bl))
  17103. {
  17104. send_target target = AREA;
  17105. struct StringBuf sbuf;
  17106. if (script_hasdata(st, 4)) {
  17107. if (script_getnum(st, 4) == BC_SELF) {
  17108. if (map_id2sd(bl->id) == NULL) {
  17109. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  17110. return SCRIPT_CMD_FAILURE;
  17111. }
  17112. target = SELF;
  17113. }
  17114. }
  17115. StringBuf_Init(&sbuf);
  17116. StringBuf_Printf(&sbuf, "%s", message);
  17117. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  17118. StringBuf_Destroy(&sbuf);
  17119. }
  17120. return SCRIPT_CMD_SUCCESS;
  17121. }
  17122. /// Makes the unit do an emotion.
  17123. ///
  17124. /// unitemote <unit_id>,<emotion>;
  17125. ///
  17126. /// @see ET_* in script_constants.hpp
  17127. BUILDIN_FUNC(unitemote)
  17128. {
  17129. int emotion;
  17130. struct block_list* bl;
  17131. emotion = script_getnum(st,3);
  17132. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  17133. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  17134. return SCRIPT_CMD_FAILURE;
  17135. }
  17136. if (script_rid2bl(2,bl))
  17137. clif_emotion(bl, emotion);
  17138. return SCRIPT_CMD_SUCCESS;
  17139. }
  17140. /// Makes the unit cast the skill on the target or self if no target is specified.
  17141. ///
  17142. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  17143. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  17144. BUILDIN_FUNC(unitskilluseid)
  17145. {
  17146. int unit_id, target_id, casttime;
  17147. uint16 skill_id, skill_lv;
  17148. struct block_list* bl;
  17149. unit_id = script_getnum(st,2);
  17150. if (script_isstring(st, 3)) {
  17151. const char *name = script_getstr(st, 3);
  17152. if (!(skill_id = skill_name2id(name))) {
  17153. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  17154. return SCRIPT_CMD_FAILURE;
  17155. }
  17156. } else {
  17157. skill_id = script_getnum(st, 3);
  17158. if (!skill_get_index(skill_id)) {
  17159. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  17160. return SCRIPT_CMD_FAILURE;
  17161. }
  17162. }
  17163. skill_lv = script_getnum(st,4);
  17164. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  17165. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  17166. bool cancel = ( script_hasdata(st,7) ? script_getnum(st,7) > 0 : skill_get_castcancel(skill_id) );
  17167. int msg_id = (script_hasdata(st, 8) ? script_getnum(st, 8) : 0);
  17168. if(script_rid2bl(2,bl)){
  17169. if (msg_id > 0) {
  17170. if (bl->type != BL_MOB) {
  17171. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  17172. return SCRIPT_CMD_FAILURE;
  17173. }
  17174. TBL_MOB* md = map_id2md(bl->id);
  17175. if (md)
  17176. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  17177. }
  17178. if (bl->type == BL_NPC) {
  17179. if (!((TBL_NPC*)bl)->status.hp)
  17180. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17181. else
  17182. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17183. }
  17184. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel);
  17185. }
  17186. return SCRIPT_CMD_SUCCESS;
  17187. }
  17188. /// Makes the unit cast the skill on the target position.
  17189. ///
  17190. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  17191. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  17192. BUILDIN_FUNC(unitskillusepos)
  17193. {
  17194. int skill_x, skill_y, casttime;
  17195. uint16 skill_id, skill_lv;
  17196. struct block_list* bl;
  17197. if (script_isstring(st, 3)) {
  17198. const char *name = script_getstr(st, 3);
  17199. if (!(skill_id = skill_name2id(name))) {
  17200. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  17201. return SCRIPT_CMD_FAILURE;
  17202. }
  17203. } else {
  17204. skill_id = script_getnum(st, 3);
  17205. if (!skill_get_index(skill_id)) {
  17206. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  17207. return SCRIPT_CMD_FAILURE;
  17208. }
  17209. }
  17210. skill_lv = script_getnum(st,4);
  17211. skill_x = script_getnum(st,5);
  17212. skill_y = script_getnum(st,6);
  17213. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  17214. bool cancel = ( script_hasdata(st,8) ? script_getnum(st,8) > 0 : skill_get_castcancel(skill_id) );
  17215. int msg_id = (script_hasdata(st, 9) ? script_getnum(st, 9) : 0);
  17216. if(script_rid2bl(2,bl)){
  17217. if (msg_id > 0) {
  17218. if (bl->type != BL_MOB) {
  17219. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  17220. return SCRIPT_CMD_FAILURE;
  17221. }
  17222. TBL_MOB* md = map_id2md(bl->id);
  17223. if (md)
  17224. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  17225. }
  17226. if (bl->type == BL_NPC) {
  17227. if (!((TBL_NPC*)bl)->status.hp)
  17228. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  17229. else
  17230. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  17231. }
  17232. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel);
  17233. }
  17234. return SCRIPT_CMD_SUCCESS;
  17235. }
  17236. // <--- [zBuffer] List of unit control commands
  17237. /// Pauses the execution of the script, detaching the player
  17238. ///
  17239. /// sleep <milli seconds>;
  17240. BUILDIN_FUNC(sleep)
  17241. {
  17242. // First call(by function call)
  17243. if (st->sleep.tick == 0) {
  17244. int ticks;
  17245. ticks = script_getnum(st, 2);
  17246. if (ticks <= 0) {
  17247. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  17248. return SCRIPT_CMD_FAILURE;
  17249. }
  17250. // detach the player
  17251. script_detach_rid(st);
  17252. // sleep for the target amount of time
  17253. st->state = RERUNLINE;
  17254. st->sleep.tick = ticks;
  17255. // Second call(by timer after sleeping time is over)
  17256. } else {
  17257. // Continue the script
  17258. st->state = RUN;
  17259. st->sleep.tick = 0;
  17260. }
  17261. return SCRIPT_CMD_SUCCESS;
  17262. }
  17263. /// Pauses the execution of the script, keeping the unit attached
  17264. /// Stops the script if no unit is attached
  17265. ///
  17266. /// sleep2(<milli seconds>)
  17267. BUILDIN_FUNC(sleep2)
  17268. {
  17269. // First call(by function call)
  17270. if (st->sleep.tick == 0) {
  17271. int ticks;
  17272. ticks = script_getnum(st, 2);
  17273. if (ticks <= 0) {
  17274. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  17275. return SCRIPT_CMD_FAILURE;
  17276. }
  17277. if (map_id2bl(st->rid) == NULL) {
  17278. ShowError( "buildin_sleep2: no unit is attached\n" );
  17279. return SCRIPT_CMD_FAILURE;
  17280. }
  17281. // sleep for the target amount of time
  17282. st->state = RERUNLINE;
  17283. st->sleep.tick = ticks;
  17284. // Second call(by timer after sleeping time is over)
  17285. } else {
  17286. // Check if the unit is still attached
  17287. // NOTE: This should never happen, since run_script_timer already checks this
  17288. if (map_id2bl(st->rid) == NULL) {
  17289. // The unit is not attached anymore - terminate the script
  17290. st->rid = 0;
  17291. st->state = END;
  17292. } else {
  17293. // The unit is still attached - continue the script
  17294. st->state = RUN;
  17295. st->sleep.tick = 0;
  17296. }
  17297. }
  17298. return SCRIPT_CMD_SUCCESS;
  17299. }
  17300. /// Awakes all the sleep timers of the target npc
  17301. ///
  17302. /// awake "<npc name>";
  17303. BUILDIN_FUNC(awake)
  17304. {
  17305. DBIterator *iter;
  17306. struct script_state *tst;
  17307. struct npc_data* nd;
  17308. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  17309. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  17310. return SCRIPT_CMD_FAILURE;
  17311. }
  17312. int rid = st->rid;
  17313. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  17314. if( rid ){
  17315. script_detach_rid(st);
  17316. }
  17317. iter = db_iterator(st_db);
  17318. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  17319. if (tst->oid == nd->bl.id) {
  17320. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  17321. continue;
  17322. }
  17323. delete_timer(tst->sleep.timer, run_script_timer);
  17324. // Trigger the timer function
  17325. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  17326. }
  17327. }
  17328. dbi_destroy(iter);
  17329. // If a player had been attached, now is the time to restore it
  17330. if( rid ){
  17331. st->rid = rid;
  17332. script_attach_state(st);
  17333. }
  17334. return SCRIPT_CMD_SUCCESS;
  17335. }
  17336. /// Returns a reference to a variable of the target NPC.
  17337. /// Returns 0 if an error occurs.
  17338. ///
  17339. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  17340. BUILDIN_FUNC(getvariableofnpc)
  17341. {
  17342. struct script_data* data;
  17343. const char* name;
  17344. struct npc_data* nd;
  17345. data = script_getdata(st,2);
  17346. if( !data_isreference(data) )
  17347. {// Not a reference (aka varaible name)
  17348. ShowError("buildin_getvariableofnpc: not a variable\n");
  17349. script_reportdata(data);
  17350. script_pushnil(st);
  17351. st->state = END;
  17352. return SCRIPT_CMD_FAILURE;
  17353. }
  17354. name = reference_getname(data);
  17355. if( *name != '.' || name[1] == '@' )
  17356. {// not a npc variable
  17357. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  17358. script_reportdata(data);
  17359. script_pushnil(st);
  17360. st->state = END;
  17361. return SCRIPT_CMD_FAILURE;
  17362. }
  17363. nd = npc_name2id(script_getstr(st,3));
  17364. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  17365. {// NPC not found or has no script
  17366. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  17367. script_pushnil(st);
  17368. st->state = END;
  17369. return SCRIPT_CMD_FAILURE;
  17370. }
  17371. if (!nd->u.scr.script->local.vars)
  17372. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  17373. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  17374. return SCRIPT_CMD_SUCCESS;
  17375. }
  17376. /// Opens a warp portal.
  17377. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  17378. ///
  17379. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  17380. ///
  17381. /// @author blackhole89
  17382. BUILDIN_FUNC(warpportal)
  17383. {
  17384. int spx;
  17385. int spy;
  17386. unsigned short mapindex;
  17387. int tpx;
  17388. int tpy;
  17389. std::shared_ptr<s_skill_unit_group> group;
  17390. struct block_list* bl;
  17391. bl = map_id2bl(st->oid);
  17392. if( bl == NULL ) {
  17393. ShowError("buildin_warpportal: NPC is needed\n");
  17394. return SCRIPT_CMD_FAILURE;
  17395. }
  17396. spx = script_getnum(st,2);
  17397. spy = script_getnum(st,3);
  17398. mapindex = mapindex_name2id(script_getstr(st, 4));
  17399. tpx = script_getnum(st,5);
  17400. tpy = script_getnum(st,6);
  17401. if( mapindex == 0 ) {
  17402. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  17403. return SCRIPT_CMD_FAILURE;
  17404. }
  17405. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  17406. if( group == NULL )
  17407. return SCRIPT_CMD_FAILURE;// failed
  17408. group->val1 = (group->val1<<16)|(short)0;
  17409. group->val2 = (tpx<<16) | tpy;
  17410. group->val3 = mapindex;
  17411. return SCRIPT_CMD_SUCCESS;
  17412. }
  17413. /**
  17414. * openmail({<char_id>});
  17415. **/
  17416. BUILDIN_FUNC(openmail)
  17417. {
  17418. struct map_session_data* sd;
  17419. if (!script_charid2sd(2,sd))
  17420. return SCRIPT_CMD_FAILURE;
  17421. #if PACKETVER < 20150513
  17422. mail_openmail(sd);
  17423. return SCRIPT_CMD_SUCCESS;
  17424. #else
  17425. ShowError( "buildin_openmail: This command is not supported for PACKETVER 2015-05-13 or newer.\n" );
  17426. return SCRIPT_CMD_FAILURE;
  17427. #endif
  17428. }
  17429. /**
  17430. * openauction({<char_id>});
  17431. **/
  17432. BUILDIN_FUNC(openauction)
  17433. {
  17434. TBL_PC* sd;
  17435. if (!script_charid2sd(2,sd))
  17436. return SCRIPT_CMD_FAILURE;
  17437. if( !battle_config.feature_auction ) {
  17438. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  17439. return SCRIPT_CMD_SUCCESS;
  17440. }
  17441. clif_Auction_openwindow(sd);
  17442. return SCRIPT_CMD_SUCCESS;
  17443. }
  17444. /// Retrieves the value of the specified flag of the specified cell.
  17445. ///
  17446. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  17447. ///
  17448. /// @see cell_chk* constants in src/map/script_constants.hpp for the types
  17449. BUILDIN_FUNC(checkcell)
  17450. {
  17451. int16 m = map_mapname2mapid(script_getstr(st,2));
  17452. int16 x = script_getnum(st,3);
  17453. int16 y = script_getnum(st,4);
  17454. cell_chk type = (cell_chk)script_getnum(st,5);
  17455. script_pushint(st, map_getcell(m, x, y, type));
  17456. return SCRIPT_CMD_SUCCESS;
  17457. }
  17458. /// Modifies flags of cells in the specified area.
  17459. ///
  17460. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  17461. ///
  17462. /// @see cell_* constants in src/map/script_constants.hpp for the types
  17463. BUILDIN_FUNC(setcell)
  17464. {
  17465. int16 m = map_mapname2mapid(script_getstr(st,2));
  17466. int16 x1 = script_getnum(st,3);
  17467. int16 y1 = script_getnum(st,4);
  17468. int16 x2 = script_getnum(st,5);
  17469. int16 y2 = script_getnum(st,6);
  17470. cell_t type = (cell_t)script_getnum(st,7);
  17471. bool flag = script_getnum(st,8) != 0;
  17472. int x,y;
  17473. if( x1 > x2 ) SWAP(x1,x2);
  17474. if( y1 > y2 ) SWAP(y1,y2);
  17475. for( y = y1; y <= y2; ++y )
  17476. for( x = x1; x <= x2; ++x )
  17477. map_setcell(m, x, y, type, flag);
  17478. return SCRIPT_CMD_SUCCESS;
  17479. }
  17480. /**
  17481. * Gets a free cell in the specified area.
  17482. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  17483. */
  17484. BUILDIN_FUNC(getfreecell)
  17485. {
  17486. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  17487. struct script_data
  17488. *data_x = script_getdata(st, 3),
  17489. *data_y = script_getdata(st, 4);
  17490. struct block_list* bl = map_id2bl(st->rid);
  17491. struct map_session_data *sd = nullptr;
  17492. int16 m, x = 0, y = 0;
  17493. int rx = -1, ry = -1, flag = 1;
  17494. if (!data_isreference(data_x)) {
  17495. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  17496. return SCRIPT_CMD_FAILURE;
  17497. }
  17498. if (!data_isreference(data_y)) {
  17499. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  17500. return SCRIPT_CMD_FAILURE;
  17501. }
  17502. name_x = reference_getname(data_x),
  17503. name_y = reference_getname(data_y);
  17504. if (is_string_variable(name_x)) {
  17505. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  17506. return SCRIPT_CMD_FAILURE;
  17507. }
  17508. if (is_string_variable(name_y)) {
  17509. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  17510. return SCRIPT_CMD_FAILURE;
  17511. }
  17512. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  17513. if (!script_rid2sd(sd)) {
  17514. if (not_server_variable(*name_x))
  17515. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  17516. else
  17517. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  17518. return SCRIPT_CMD_FAILURE;
  17519. }
  17520. }
  17521. if (script_hasdata(st, 5))
  17522. x = script_getnum(st, 5);
  17523. if (script_hasdata(st, 6))
  17524. y = script_getnum(st, 6);
  17525. if (script_hasdata(st, 7))
  17526. rx = script_getnum(st, 7);
  17527. if (script_hasdata(st, 8))
  17528. ry = script_getnum(st, 8);
  17529. if (script_hasdata(st, 9))
  17530. flag = script_getnum(st, 9);
  17531. if (bl && strcmp(mapn, "this") == 0)
  17532. m = bl->m;
  17533. else
  17534. m = map_mapname2mapid(mapn);
  17535. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  17536. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  17537. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  17538. return SCRIPT_CMD_SUCCESS;
  17539. }
  17540. /*==========================================
  17541. * Mercenary Commands
  17542. *------------------------------------------*/
  17543. BUILDIN_FUNC(mercenary_create)
  17544. {
  17545. struct map_session_data *sd;
  17546. int class_, contract_time;
  17547. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  17548. return SCRIPT_CMD_SUCCESS;
  17549. class_ = script_getnum(st,2);
  17550. if( !mercenary_db.exists(class_) )
  17551. return SCRIPT_CMD_SUCCESS;
  17552. contract_time = script_getnum(st,3);
  17553. mercenary_create(sd, class_, contract_time);
  17554. return SCRIPT_CMD_SUCCESS;
  17555. }
  17556. BUILDIN_FUNC(mercenary_delete)
  17557. {
  17558. struct map_session_data *sd;
  17559. int type = 0;
  17560. if( !script_charid2sd(2, sd) )
  17561. return SCRIPT_CMD_FAILURE;
  17562. if( sd->md == nullptr ) {
  17563. 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);
  17564. return SCRIPT_CMD_FAILURE;
  17565. }
  17566. if( script_hasdata(st, 3) ) {
  17567. type = script_getnum(st, 3);
  17568. if( type < 0 || type > 3 ) {
  17569. ShowWarning("buildin_mercenary_delete: invalid type value of %d.\n", type);
  17570. return SCRIPT_CMD_FAILURE;
  17571. }
  17572. }
  17573. mercenary_delete(sd->md, type);
  17574. return SCRIPT_CMD_SUCCESS;
  17575. }
  17576. BUILDIN_FUNC(mercenary_heal)
  17577. {
  17578. struct map_session_data *sd;
  17579. int hp, sp;
  17580. if( !script_rid2sd(sd) || sd->md == NULL )
  17581. return SCRIPT_CMD_SUCCESS;
  17582. hp = script_getnum(st,2);
  17583. sp = script_getnum(st,3);
  17584. status_heal(&sd->md->bl, hp, sp, 0);
  17585. return SCRIPT_CMD_SUCCESS;
  17586. }
  17587. BUILDIN_FUNC(mercenary_sc_start)
  17588. {
  17589. struct map_session_data *sd;
  17590. enum sc_type type;
  17591. int tick, val1;
  17592. if( !script_rid2sd(sd) || sd->md == NULL )
  17593. return SCRIPT_CMD_SUCCESS;
  17594. type = (sc_type)script_getnum(st,2);
  17595. tick = script_getnum(st,3);
  17596. val1 = script_getnum(st,4);
  17597. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  17598. return SCRIPT_CMD_SUCCESS;
  17599. }
  17600. BUILDIN_FUNC(mercenary_get_calls)
  17601. {
  17602. struct map_session_data *sd;
  17603. int guild;
  17604. if( !script_rid2sd(sd) )
  17605. return SCRIPT_CMD_SUCCESS;
  17606. guild = script_getnum(st,2);
  17607. switch( guild )
  17608. {
  17609. case ARCH_MERC_GUILD:
  17610. script_pushint(st,sd->status.arch_calls);
  17611. break;
  17612. case SPEAR_MERC_GUILD:
  17613. script_pushint(st,sd->status.spear_calls);
  17614. break;
  17615. case SWORD_MERC_GUILD:
  17616. script_pushint(st,sd->status.sword_calls);
  17617. break;
  17618. default:
  17619. script_pushint(st,0);
  17620. break;
  17621. }
  17622. return SCRIPT_CMD_SUCCESS;
  17623. }
  17624. BUILDIN_FUNC(mercenary_set_calls)
  17625. {
  17626. struct map_session_data *sd;
  17627. int guild, value, *calls;
  17628. if( !script_rid2sd(sd) )
  17629. return SCRIPT_CMD_SUCCESS;
  17630. guild = script_getnum(st,2);
  17631. value = script_getnum(st,3);
  17632. switch( guild )
  17633. {
  17634. case ARCH_MERC_GUILD:
  17635. calls = &sd->status.arch_calls;
  17636. break;
  17637. case SPEAR_MERC_GUILD:
  17638. calls = &sd->status.spear_calls;
  17639. break;
  17640. case SWORD_MERC_GUILD:
  17641. calls = &sd->status.sword_calls;
  17642. break;
  17643. default:
  17644. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  17645. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17646. }
  17647. *calls += value;
  17648. *calls = cap_value(*calls, 0, INT_MAX);
  17649. return SCRIPT_CMD_SUCCESS;
  17650. }
  17651. BUILDIN_FUNC(mercenary_get_faith)
  17652. {
  17653. struct map_session_data *sd;
  17654. int guild;
  17655. if( !script_rid2sd(sd) )
  17656. return SCRIPT_CMD_SUCCESS;
  17657. guild = script_getnum(st,2);
  17658. switch( guild )
  17659. {
  17660. case ARCH_MERC_GUILD:
  17661. script_pushint(st,sd->status.arch_faith);
  17662. break;
  17663. case SPEAR_MERC_GUILD:
  17664. script_pushint(st,sd->status.spear_faith);
  17665. break;
  17666. case SWORD_MERC_GUILD:
  17667. script_pushint(st,sd->status.sword_faith);
  17668. break;
  17669. default:
  17670. script_pushint(st,0);
  17671. break;
  17672. }
  17673. return SCRIPT_CMD_SUCCESS;
  17674. }
  17675. BUILDIN_FUNC(mercenary_set_faith)
  17676. {
  17677. struct map_session_data *sd;
  17678. int guild, value, *calls;
  17679. if( !script_rid2sd(sd) )
  17680. return SCRIPT_CMD_SUCCESS;
  17681. guild = script_getnum(st,2);
  17682. value = script_getnum(st,3);
  17683. switch( guild )
  17684. {
  17685. case ARCH_MERC_GUILD:
  17686. calls = &sd->status.arch_faith;
  17687. break;
  17688. case SPEAR_MERC_GUILD:
  17689. calls = &sd->status.spear_faith;
  17690. break;
  17691. case SWORD_MERC_GUILD:
  17692. calls = &sd->status.sword_faith;
  17693. break;
  17694. default:
  17695. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17696. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17697. }
  17698. *calls += value;
  17699. *calls = cap_value(*calls, 0, INT_MAX);
  17700. if( mercenary_get_guild(sd->md) == guild )
  17701. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17702. return SCRIPT_CMD_SUCCESS;
  17703. }
  17704. /*------------------------------------------
  17705. * Book Reading
  17706. *------------------------------------------*/
  17707. BUILDIN_FUNC(readbook)
  17708. {
  17709. struct map_session_data *sd;
  17710. int book_id, page;
  17711. if( !script_rid2sd(sd) )
  17712. return SCRIPT_CMD_SUCCESS;
  17713. book_id = script_getnum(st,2);
  17714. page = script_getnum(st,3);
  17715. clif_readbook(sd->fd, book_id, page);
  17716. return SCRIPT_CMD_SUCCESS;
  17717. }
  17718. /******************
  17719. Questlog script commands
  17720. *******************/
  17721. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17722. BUILDIN_FUNC(questinfo)
  17723. {
  17724. TBL_NPC* nd = map_id2nd(st->oid);
  17725. if (!nd || nd->bl.m == -1) {
  17726. ShowError("buildin_questinfo: No NPC attached.\n");
  17727. return SCRIPT_CMD_FAILURE;
  17728. }
  17729. int icon = script_getnum(st, 2);
  17730. #if PACKETVER >= 20120410
  17731. switch(icon){
  17732. case QTYPE_QUEST:
  17733. case QTYPE_QUEST2:
  17734. case QTYPE_JOB:
  17735. case QTYPE_JOB2:
  17736. case QTYPE_EVENT:
  17737. case QTYPE_EVENT2:
  17738. // Warg icons were replaced in this client
  17739. #if PACKETVER < 20170315
  17740. case QTYPE_WARG:
  17741. case QTYPE_WARG2:
  17742. #else
  17743. case QTYPE_CLICKME:
  17744. case QTYPE_DAILYQUEST:
  17745. case QTYPE_EVENT3:
  17746. case QTYPE_JOBQUEST:
  17747. case QTYPE_JUMPING_PORING:
  17748. #endif
  17749. // Leave everything as it is
  17750. break;
  17751. case QTYPE_NONE:
  17752. default:
  17753. // Default to nothing if icon id is invalid.
  17754. icon = QTYPE_NONE;
  17755. break;
  17756. }
  17757. #else
  17758. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17759. icon = QTYPE_QUEST;
  17760. else
  17761. icon = icon + 1;
  17762. #endif
  17763. int color = QMARK_NONE;
  17764. if (script_hasdata(st, 3)) {
  17765. color = script_getnum(st, 3);
  17766. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17767. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17768. script_reportfunc(st);
  17769. color = QMARK_NONE;
  17770. }
  17771. }
  17772. struct script_code *script = nullptr;
  17773. if (script_hasdata(st, 4)) {
  17774. const char *str = script_getstr(st, 4);
  17775. if (str) {
  17776. std::string condition(str);
  17777. if (condition.find( "achievement_condition" ) == std::string::npos)
  17778. condition = "achievement_condition( " + condition + " );";
  17779. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17780. if (!script) {
  17781. st->state = END;
  17782. return SCRIPT_CMD_FAILURE;
  17783. }
  17784. }
  17785. }
  17786. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17787. qi->icon = static_cast<e_questinfo_types>(icon);
  17788. qi->color = static_cast<e_questinfo_markcolor>(color);
  17789. qi->condition = script;
  17790. nd->qi_data.push_back(qi);
  17791. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17792. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17793. mapdata->qi_npc.push_back(nd->bl.id);
  17794. return SCRIPT_CMD_SUCCESS;
  17795. }
  17796. /**
  17797. * questinfo_refresh {<char_id>};
  17798. **/
  17799. BUILDIN_FUNC(questinfo_refresh)
  17800. {
  17801. struct map_session_data *sd;
  17802. if (!script_charid2sd(2, sd))
  17803. return SCRIPT_CMD_FAILURE;
  17804. pc_show_questinfo(sd);
  17805. return SCRIPT_CMD_SUCCESS;
  17806. }
  17807. /**
  17808. * setquest <ID>{,<char_id>};
  17809. **/
  17810. BUILDIN_FUNC(setquest)
  17811. {
  17812. struct map_session_data *sd;
  17813. int quest_id;
  17814. quest_id = script_getnum(st, 2);
  17815. if (!script_charid2sd(3,sd))
  17816. return SCRIPT_CMD_FAILURE;
  17817. if( quest_add(sd, quest_id) == -1 ){
  17818. script_reportsrc(st);
  17819. script_reportfunc(st);
  17820. }
  17821. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17822. pc_show_questinfo(sd);
  17823. return SCRIPT_CMD_SUCCESS;
  17824. }
  17825. /**
  17826. * erasequest <ID>{,<char_id>};
  17827. **/
  17828. BUILDIN_FUNC(erasequest)
  17829. {
  17830. struct map_session_data *sd;
  17831. if (!script_charid2sd(3,sd))
  17832. return SCRIPT_CMD_FAILURE;
  17833. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17834. script_reportsrc(st);
  17835. script_reportfunc(st);
  17836. }
  17837. return SCRIPT_CMD_SUCCESS;
  17838. }
  17839. /**
  17840. * completequest <ID>{,<char_id>};
  17841. **/
  17842. BUILDIN_FUNC(completequest)
  17843. {
  17844. struct map_session_data *sd;
  17845. if (!script_charid2sd(3,sd))
  17846. return SCRIPT_CMD_FAILURE;
  17847. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17848. //20120410 or 20090218
  17849. pc_show_questinfo(sd);
  17850. return SCRIPT_CMD_SUCCESS;
  17851. }
  17852. /**
  17853. * changequest <ID>,<ID2>{,<char_id>};
  17854. **/
  17855. BUILDIN_FUNC(changequest)
  17856. {
  17857. struct map_session_data *sd;
  17858. if (!script_charid2sd(4,sd))
  17859. return SCRIPT_CMD_FAILURE;
  17860. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17861. script_reportsrc(st);
  17862. script_reportfunc(st);
  17863. }
  17864. //20120410 or 20090218
  17865. pc_show_questinfo(sd);
  17866. return SCRIPT_CMD_SUCCESS;
  17867. }
  17868. /**
  17869. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17870. **/
  17871. BUILDIN_FUNC(checkquest)
  17872. {
  17873. struct map_session_data *sd;
  17874. e_quest_check_type type = HAVEQUEST;
  17875. if( script_hasdata(st, 3) )
  17876. type = (e_quest_check_type)script_getnum(st, 3);
  17877. if (!script_charid2sd(4,sd))
  17878. return SCRIPT_CMD_FAILURE;
  17879. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17880. return SCRIPT_CMD_SUCCESS;
  17881. }
  17882. /**
  17883. * isbegin_quest(<ID>{,<char_id>})
  17884. **/
  17885. BUILDIN_FUNC(isbegin_quest)
  17886. {
  17887. struct map_session_data *sd;
  17888. if (!script_charid2sd(3,sd))
  17889. return SCRIPT_CMD_FAILURE;
  17890. int i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17891. script_pushint(st, i + (i < 1));
  17892. return SCRIPT_CMD_SUCCESS;
  17893. }
  17894. /**
  17895. * showevent <icon>{,<mark color>{,<char_id>}}
  17896. **/
  17897. BUILDIN_FUNC(showevent)
  17898. {
  17899. TBL_PC *sd;
  17900. if (!script_charid2sd(4,sd))
  17901. return SCRIPT_CMD_FAILURE;
  17902. struct npc_data *nd = map_id2nd(st->oid);
  17903. if (!nd)
  17904. return SCRIPT_CMD_SUCCESS;
  17905. int color = QMARK_NONE;
  17906. int icon = script_getnum(st, 2);
  17907. if (script_hasdata(st, 3)) {
  17908. color = script_getnum(st, 3);
  17909. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17910. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17911. script_reportfunc(st);
  17912. color = QMARK_NONE;
  17913. }
  17914. }
  17915. #if PACKETVER >= 20120410
  17916. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17917. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17918. #else
  17919. if (icon < 0 || icon > 7)
  17920. icon = 0;
  17921. else
  17922. icon = icon + 1;
  17923. #endif
  17924. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17925. return SCRIPT_CMD_SUCCESS;
  17926. }
  17927. /*==========================================
  17928. * BattleGround System
  17929. *------------------------------------------*/
  17930. BUILDIN_FUNC(waitingroom2bg)
  17931. {
  17932. struct npc_data *nd;
  17933. struct chat_data *cd;
  17934. const char *map_name;
  17935. int mapindex = 0, bg_id;
  17936. unsigned char i,c=0;
  17937. struct s_battleground_team team;
  17938. if( script_hasdata(st,7) )
  17939. nd = npc_name2id(script_getstr(st,7));
  17940. else
  17941. nd = (struct npc_data *)map_id2bl(st->oid);
  17942. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  17943. {
  17944. script_pushint(st,0);
  17945. return SCRIPT_CMD_SUCCESS;
  17946. }
  17947. map_name = script_getstr(st,2);
  17948. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17949. { // Invalid Map
  17950. script_pushint(st, 0);
  17951. return SCRIPT_CMD_SUCCESS;
  17952. }
  17953. team.warp_x = script_getnum(st,3);
  17954. team.warp_y = script_getnum(st,4);
  17955. if (script_hasdata(st,5)) {
  17956. team.quit_event = script_getstr(st,5); // Logout Event
  17957. check_event(st, team.quit_event.c_str());
  17958. } else
  17959. team.quit_event = "";
  17960. if (script_hasdata(st,6)) {
  17961. team.death_event = script_getstr(st,6); // Die Event
  17962. check_event(st, team.death_event.c_str());
  17963. } else
  17964. team.death_event = "";
  17965. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  17966. { // Creation failed
  17967. script_pushint(st,0);
  17968. return SCRIPT_CMD_SUCCESS;
  17969. }
  17970. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  17971. struct map_session_data *sd;
  17972. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd, false) ){
  17973. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  17974. ++c;
  17975. }
  17976. }
  17977. mapreg_setreg(add_str("$@arenamembersnum"), c);
  17978. script_pushint(st,bg_id);
  17979. return SCRIPT_CMD_SUCCESS;
  17980. }
  17981. BUILDIN_FUNC(waitingroom2bg_single)
  17982. {
  17983. const char* map_name;
  17984. struct npc_data *nd;
  17985. struct chat_data *cd;
  17986. struct map_session_data *sd;
  17987. int x, y, mapindex, bg_id = script_getnum(st,2);
  17988. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17989. if (!bg) {
  17990. script_pushint(st, false);
  17991. return SCRIPT_CMD_SUCCESS;
  17992. }
  17993. if (script_hasdata(st, 3)) {
  17994. map_name = script_getstr(st, 3);
  17995. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17996. script_pushint(st, false);
  17997. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17998. }
  17999. x = script_getnum(st, 4);
  18000. y = script_getnum(st, 5);
  18001. }
  18002. else {
  18003. mapindex = bg->cemetery.map;
  18004. x = bg->cemetery.x;
  18005. y = bg->cemetery.y;
  18006. }
  18007. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  18008. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  18009. script_pushint(st, false);
  18010. return SCRIPT_CMD_FAILURE;
  18011. }
  18012. nd = npc_name2id(script_getstr(st,6));
  18013. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  18014. return SCRIPT_CMD_SUCCESS;
  18015. if( (sd = cd->usersd[0]) == NULL )
  18016. return SCRIPT_CMD_SUCCESS;
  18017. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  18018. {
  18019. script_pushint(st, true);
  18020. }
  18021. else
  18022. script_pushint(st, false);
  18023. return SCRIPT_CMD_SUCCESS;
  18024. }
  18025. /// Creates an instance of battleground battle group.
  18026. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  18027. /// @author [secretdataz]
  18028. BUILDIN_FUNC(bg_create) {
  18029. const char *map_name;
  18030. int mapindex = 0;
  18031. struct s_battleground_team team;
  18032. map_name = script_getstr(st, 2);
  18033. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  18034. { // Invalid Map
  18035. script_pushint(st, 0);
  18036. return SCRIPT_CMD_SUCCESS;
  18037. }
  18038. team.warp_x = script_getnum(st,3);
  18039. team.warp_y = script_getnum(st,4);
  18040. if (script_hasdata(st,5)) {
  18041. team.quit_event = script_getstr(st,5); // Logout Event
  18042. check_event(st, team.quit_event.c_str());
  18043. } else
  18044. team.quit_event = "";
  18045. if (script_hasdata(st,6)) {
  18046. team.death_event = script_getstr(st,6); // Die Event
  18047. check_event(st, team.death_event.c_str());
  18048. } else
  18049. team.death_event = "";
  18050. script_pushint(st, bg_create(mapindex, &team));
  18051. return SCRIPT_CMD_SUCCESS;
  18052. }
  18053. /// Adds attached player or <char id> (if specified) to an existing
  18054. /// battleground group and warps it to the specified coordinates on
  18055. /// the given map.
  18056. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  18057. /// @author [secretdataz]
  18058. BUILDIN_FUNC(bg_join) {
  18059. const char* map_name;
  18060. struct map_session_data *sd;
  18061. int x, y, mapindex, bg_id = script_getnum(st, 2);
  18062. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18063. if (!bg) {
  18064. script_pushint(st, false);
  18065. return SCRIPT_CMD_SUCCESS;
  18066. }
  18067. if (script_hasdata(st, 3)) {
  18068. map_name = script_getstr(st, 3);
  18069. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  18070. script_pushint(st, false);
  18071. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18072. }
  18073. x = script_getnum(st, 4);
  18074. y = script_getnum(st, 5);
  18075. } else {
  18076. mapindex = bg->cemetery.map;
  18077. x = bg->cemetery.x;
  18078. y = bg->cemetery.y;
  18079. }
  18080. if (!script_charid2sd(6, sd)) {
  18081. script_pushint(st, false);
  18082. return SCRIPT_CMD_FAILURE;
  18083. }
  18084. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  18085. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  18086. script_pushint(st, false);
  18087. return SCRIPT_CMD_FAILURE;
  18088. }
  18089. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  18090. {
  18091. script_pushint(st, true);
  18092. }
  18093. else
  18094. script_pushint(st, false);
  18095. return SCRIPT_CMD_SUCCESS;
  18096. }
  18097. BUILDIN_FUNC(bg_team_setxy)
  18098. {
  18099. int bg_id = script_getnum(st,2);
  18100. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18101. if (bg) {
  18102. bg->cemetery.x = script_getnum(st, 3);
  18103. bg->cemetery.y = script_getnum(st, 4);
  18104. }
  18105. return SCRIPT_CMD_SUCCESS;
  18106. }
  18107. BUILDIN_FUNC(bg_warp)
  18108. {
  18109. int x, y, mapindex, bg_id;
  18110. const char* map_name;
  18111. bg_id = script_getnum(st,2);
  18112. map_name = script_getstr(st,3);
  18113. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  18114. return SCRIPT_CMD_SUCCESS; // Invalid Map
  18115. x = script_getnum(st,4);
  18116. y = script_getnum(st,5);
  18117. bg_team_warp(bg_id, mapindex, x, y);
  18118. return SCRIPT_CMD_SUCCESS;
  18119. }
  18120. BUILDIN_FUNC(bg_monster)
  18121. {
  18122. int class_ = 0, x = 0, y = 0, bg_id = 0;
  18123. const char *str,*mapname, *evt="";
  18124. bg_id = script_getnum(st,2);
  18125. mapname = script_getstr(st,3);
  18126. x = script_getnum(st,4);
  18127. y = script_getnum(st,5);
  18128. str = script_getstr(st,6);
  18129. class_ = script_getnum(st,7);
  18130. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  18131. check_event(st, evt);
  18132. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  18133. return SCRIPT_CMD_SUCCESS;
  18134. }
  18135. BUILDIN_FUNC(bg_monster_set_team)
  18136. {
  18137. struct mob_data *md;
  18138. struct block_list *mbl;
  18139. int id = script_getnum(st,2),
  18140. bg_id = script_getnum(st,3);
  18141. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  18142. return SCRIPT_CMD_SUCCESS;
  18143. md = (TBL_MOB *)mbl;
  18144. md->bg_id = bg_id;
  18145. mob_stop_attack(md);
  18146. mob_stop_walking(md, 0);
  18147. md->target_id = md->attacked_id = 0;
  18148. clif_name_area(&md->bl);
  18149. return SCRIPT_CMD_SUCCESS;
  18150. }
  18151. BUILDIN_FUNC(bg_leave)
  18152. {
  18153. struct map_session_data *sd = NULL;
  18154. bool deserter = false;
  18155. if( !script_charid2sd(2,sd) || !sd->bg_id )
  18156. return SCRIPT_CMD_SUCCESS;
  18157. if (!strcmp(script_getfuncname(st), "bg_desert"))
  18158. deserter = true;
  18159. bg_team_leave(sd, false, deserter);
  18160. return SCRIPT_CMD_SUCCESS;
  18161. }
  18162. BUILDIN_FUNC(bg_destroy)
  18163. {
  18164. int bg_id = script_getnum(st,2);
  18165. bg_team_delete(bg_id);
  18166. return SCRIPT_CMD_SUCCESS;
  18167. }
  18168. BUILDIN_FUNC(bg_getareausers)
  18169. {
  18170. const char *str = script_getstr(st, 3);
  18171. int16 m, x0, y0, x1, y1;
  18172. int bg_id = script_getnum(st, 2), c = 0;
  18173. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18174. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  18175. script_pushint(st,0);
  18176. return SCRIPT_CMD_SUCCESS;
  18177. }
  18178. x0 = script_getnum(st,4);
  18179. y0 = script_getnum(st,5);
  18180. x1 = script_getnum(st,6);
  18181. y1 = script_getnum(st,7);
  18182. for (const auto &member : bg->members) {
  18183. 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 )
  18184. continue;
  18185. c++;
  18186. }
  18187. script_pushint(st,c);
  18188. return SCRIPT_CMD_SUCCESS;
  18189. }
  18190. BUILDIN_FUNC(bg_updatescore)
  18191. {
  18192. const char *str;
  18193. int16 m;
  18194. str = script_getstr(st,2);
  18195. if( (m = map_mapname2mapid(str)) < 0 )
  18196. return SCRIPT_CMD_SUCCESS;
  18197. struct map_data *mapdata = map_getmapdata(m);
  18198. mapdata->bgscore_lion = script_getnum(st,3);
  18199. mapdata->bgscore_eagle = script_getnum(st,4);
  18200. clif_bg_updatescore(m);
  18201. return SCRIPT_CMD_SUCCESS;
  18202. }
  18203. BUILDIN_FUNC(bg_get_data)
  18204. {
  18205. int bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  18206. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  18207. if (bg) {
  18208. switch (type) {
  18209. case 0:
  18210. script_pushint(st, bg->members.size());
  18211. break;
  18212. case 1:
  18213. for (const auto &member : bg->members)
  18214. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  18215. mapreg_setreg(add_str("$@arenamemberscount"), i);
  18216. script_pushint(st, i);
  18217. break;
  18218. default:
  18219. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  18220. break;
  18221. }
  18222. } else
  18223. script_pushint(st, 0);
  18224. return SCRIPT_CMD_SUCCESS;
  18225. }
  18226. /**
  18227. * Reserves a slot for the given Battleground.
  18228. * bg_reserve("<battleground_map_name>"{,<ended>});
  18229. */
  18230. BUILDIN_FUNC(bg_reserve)
  18231. {
  18232. const char *str = script_getstr(st, 2);
  18233. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  18234. if (!bg_queue_reserve(str, ended))
  18235. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  18236. return SCRIPT_CMD_SUCCESS;
  18237. }
  18238. /**
  18239. * Removes a spot for the given Battleground.
  18240. * bg_unbook("<battleground_map_name>");
  18241. */
  18242. BUILDIN_FUNC(bg_unbook)
  18243. {
  18244. const char *str = script_getstr(st, 2);
  18245. if (!bg_queue_unbook(str))
  18246. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  18247. return SCRIPT_CMD_SUCCESS;
  18248. }
  18249. /**
  18250. * Gets battleground database information.
  18251. * bg_info("<battleground name>", <type>);
  18252. */
  18253. BUILDIN_FUNC(bg_info)
  18254. {
  18255. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  18256. if (!bg) {
  18257. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  18258. return SCRIPT_CMD_FAILURE;
  18259. }
  18260. int type = script_getnum(st, 3);
  18261. switch (type) {
  18262. case BG_INFO_ID:
  18263. script_pushint(st, bg->id);
  18264. break;
  18265. case BG_INFO_REQUIRED_PLAYERS:
  18266. script_pushint(st, bg->required_players);
  18267. break;
  18268. case BG_INFO_MAX_PLAYERS:
  18269. script_pushint(st, bg->max_players);
  18270. break;
  18271. case BG_INFO_MIN_LEVEL:
  18272. script_pushint(st, bg->min_lvl);
  18273. break;
  18274. case BG_INFO_MAX_LEVEL:
  18275. script_pushint(st, bg->max_lvl);
  18276. break;
  18277. case BG_INFO_MAPS: {
  18278. size_t i;
  18279. for (i = 0; i < bg->maps.size(); i++)
  18280. setd_sub_str(st, nullptr, ".@bgmaps$", i, mapindex_id2name(bg->maps[i].mapindex), nullptr);
  18281. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  18282. script_pushint(st, i);
  18283. break;
  18284. }
  18285. case BG_INFO_DESERTER_TIME:
  18286. script_pushint(st, bg->deserter_time);
  18287. break;
  18288. default:
  18289. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  18290. return SCRIPT_CMD_FAILURE;
  18291. }
  18292. return SCRIPT_CMD_SUCCESS;
  18293. }
  18294. /*==========================================
  18295. * Instancing System
  18296. *------------------------------------------*/
  18297. /**
  18298. * Returns an Instance ID.
  18299. * @param st: Script state
  18300. * @param mode: Instance mode
  18301. * @return instance ID on success or 0 otherwise
  18302. */
  18303. int script_instancegetid(struct script_state* st, e_instance_mode mode)
  18304. {
  18305. int instance_id = 0;
  18306. if (mode == IM_NONE) {
  18307. struct npc_data *nd = map_id2nd(st->oid);
  18308. if (nd->instance_id > 0)
  18309. instance_id = nd->instance_id;
  18310. } else {
  18311. struct map_session_data *sd = map_id2sd(st->rid);
  18312. if (sd) {
  18313. switch (mode) {
  18314. case IM_CHAR:
  18315. if (sd->instance_id > 0)
  18316. instance_id = sd->instance_id;
  18317. break;
  18318. case IM_PARTY: {
  18319. struct party_data *pd = party_search(sd->status.party_id);
  18320. if (pd && pd->instance_id > 0)
  18321. instance_id = pd->instance_id;
  18322. }
  18323. break;
  18324. case IM_GUILD: {
  18325. struct guild *gd = guild_search(sd->status.guild_id);
  18326. if (gd && gd->instance_id > 0)
  18327. instance_id = gd->instance_id;
  18328. }
  18329. break;
  18330. case IM_CLAN: {
  18331. struct clan *cd = clan_search(sd->status.clan_id);
  18332. if (cd && cd->instance_id > 0)
  18333. instance_id = cd->instance_id;
  18334. }
  18335. break;
  18336. default: // Unsupported type
  18337. break;
  18338. }
  18339. }
  18340. }
  18341. return instance_id;
  18342. }
  18343. /*==========================================
  18344. * Creates the instance
  18345. * Returns Instance ID if created successfully
  18346. *------------------------------------------*/
  18347. BUILDIN_FUNC(instance_create)
  18348. {
  18349. e_instance_mode mode = IM_PARTY;
  18350. int owner_id = 0;
  18351. if (script_hasdata(st, 3)) {
  18352. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18353. if (mode < IM_NONE || mode >= IM_MAX) {
  18354. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  18355. return SCRIPT_CMD_FAILURE;
  18356. }
  18357. }
  18358. if (script_hasdata(st, 4))
  18359. owner_id = script_getnum(st, 4);
  18360. else {
  18361. // If sd is NULL, instance_create will return -2.
  18362. struct map_session_data *sd = NULL;
  18363. switch(mode) {
  18364. case IM_NONE:
  18365. owner_id = st->oid;
  18366. break;
  18367. case IM_CHAR:
  18368. if (script_rid2sd(sd))
  18369. owner_id = sd->status.char_id;
  18370. break;
  18371. case IM_PARTY:
  18372. if (script_rid2sd(sd))
  18373. owner_id = sd->status.party_id;
  18374. break;
  18375. case IM_GUILD:
  18376. if (script_rid2sd(sd))
  18377. owner_id = sd->status.guild_id;
  18378. break;
  18379. case IM_CLAN:
  18380. if (script_rid2sd(sd))
  18381. owner_id = sd->status.clan_id;
  18382. break;
  18383. default:
  18384. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  18385. return SCRIPT_CMD_FAILURE;
  18386. }
  18387. }
  18388. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  18389. return SCRIPT_CMD_SUCCESS;
  18390. }
  18391. /*==========================================
  18392. * Destroys an instance (unofficial)
  18393. * Officially instances are only destroyed by timeout
  18394. *
  18395. * instance_destroy {<instance_id>};
  18396. *------------------------------------------*/
  18397. BUILDIN_FUNC(instance_destroy)
  18398. {
  18399. int instance_id;
  18400. if( script_hasdata(st,2) )
  18401. instance_id = script_getnum(st,2);
  18402. else
  18403. instance_id = script_instancegetid(st);
  18404. if( instance_id == 0 ) {
  18405. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  18406. return SCRIPT_CMD_FAILURE;
  18407. }
  18408. instance_destroy(instance_id);
  18409. return SCRIPT_CMD_SUCCESS;
  18410. }
  18411. /*==========================================
  18412. * Warps player to instance
  18413. * Results:
  18414. * IE_OK: Success
  18415. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  18416. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  18417. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  18418. *------------------------------------------*/
  18419. BUILDIN_FUNC(instance_enter)
  18420. {
  18421. struct map_session_data *sd = NULL;
  18422. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  18423. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  18424. int instance_id;
  18425. if (script_hasdata(st, 6))
  18426. instance_id = script_getnum(st, 6);
  18427. else
  18428. instance_id = script_instancegetid(st, IM_PARTY);
  18429. if (!script_charid2sd(5,sd))
  18430. return SCRIPT_CMD_FAILURE;
  18431. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  18432. return SCRIPT_CMD_SUCCESS;
  18433. }
  18434. /*==========================================
  18435. * Returns the name of a duplicated NPC
  18436. *
  18437. * instance_npcname <npc_name>{,<instance_id>};
  18438. * <npc_name> is the full name of an NPC.
  18439. *------------------------------------------*/
  18440. BUILDIN_FUNC(instance_npcname)
  18441. {
  18442. const char *str;
  18443. int instance_id;
  18444. struct npc_data *nd;
  18445. str = script_getstr(st,2);
  18446. if( script_hasdata(st,3) )
  18447. instance_id = script_getnum(st,3);
  18448. else
  18449. instance_id = script_instancegetid(st);
  18450. if( instance_id > 0 && (nd = npc_name2id(str)) != NULL ) {
  18451. static char npcname[NAME_LENGTH];
  18452. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  18453. script_pushconststr(st,npcname);
  18454. } else {
  18455. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  18456. st->state = END;
  18457. return SCRIPT_CMD_FAILURE;
  18458. }
  18459. return SCRIPT_CMD_SUCCESS;
  18460. }
  18461. /*==========================================
  18462. * Returns the name of a duplicated map
  18463. *
  18464. * instance_mapname <map_name>{,<instance_id>};
  18465. *------------------------------------------*/
  18466. BUILDIN_FUNC(instance_mapname)
  18467. {
  18468. const char *str;
  18469. int16 m;
  18470. int instance_id;
  18471. str = script_getstr(st,2);
  18472. if( script_hasdata(st,3) )
  18473. instance_id = script_getnum(st,3);
  18474. else
  18475. instance_id = script_instancegetid(st);
  18476. // Check that instance mapname is a valid map
  18477. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  18478. script_pushconststr(st, "");
  18479. else
  18480. script_pushconststr(st, map_getmapdata(m)->name);
  18481. return SCRIPT_CMD_SUCCESS;
  18482. }
  18483. /*==========================================
  18484. * Returns an Instance ID
  18485. *------------------------------------------*/
  18486. BUILDIN_FUNC(instance_id)
  18487. {
  18488. int mode = IM_NONE; // Default to the attached NPC
  18489. if (script_hasdata(st, 2)) {
  18490. mode = script_getnum(st, 2);
  18491. if (mode <= IM_NONE || mode >= IM_MAX) {
  18492. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  18493. script_pushint(st, 0);
  18494. return SCRIPT_CMD_SUCCESS;
  18495. }
  18496. }
  18497. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  18498. return SCRIPT_CMD_SUCCESS;
  18499. }
  18500. /*==========================================
  18501. * Warps all players inside an instance
  18502. *
  18503. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  18504. *------------------------------------------*/
  18505. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  18506. {
  18507. unsigned int m = va_arg(ap,unsigned int);
  18508. int x = va_arg(ap,int);
  18509. int y = va_arg(ap,int);
  18510. int instance_id = va_arg(ap, int);
  18511. struct map_session_data *sd;
  18512. nullpo_retr(0, bl);
  18513. if (bl->type != BL_PC)
  18514. return 0;
  18515. sd = (TBL_PC *)bl;
  18516. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18517. if (!idata)
  18518. return 0;
  18519. int owner_id = idata->owner_id;
  18520. switch(idata->mode) {
  18521. case IM_NONE:
  18522. break;
  18523. case IM_CHAR:
  18524. if (sd->status.char_id != owner_id)
  18525. return 0;
  18526. break;
  18527. case IM_PARTY:
  18528. if (sd->status.party_id != owner_id)
  18529. return 0;
  18530. break;
  18531. case IM_GUILD:
  18532. if (sd->status.guild_id != owner_id)
  18533. return 0;
  18534. case IM_CLAN:
  18535. if (sd->status.clan_id != owner_id)
  18536. return 0;
  18537. }
  18538. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  18539. return 1;
  18540. }
  18541. BUILDIN_FUNC(instance_warpall)
  18542. {
  18543. int16 m;
  18544. int instance_id;
  18545. const char *mapn;
  18546. int x, y;
  18547. mapn = script_getstr(st,2);
  18548. x = script_getnum(st,3);
  18549. y = script_getnum(st,4);
  18550. if( script_hasdata(st,5) )
  18551. instance_id = script_getnum(st,5);
  18552. else
  18553. instance_id = script_instancegetid(st, IM_PARTY);
  18554. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0)
  18555. return SCRIPT_CMD_FAILURE;
  18556. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18557. if (!idata) {
  18558. ShowError("buildin_instance_warpall: Instance is not found.\n");
  18559. return SCRIPT_CMD_FAILURE;
  18560. }
  18561. for(const auto &it : idata->map)
  18562. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id);
  18563. return SCRIPT_CMD_SUCCESS;
  18564. }
  18565. /*==========================================
  18566. * Broadcasts to all maps inside an instance
  18567. *
  18568. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  18569. * Using 0 for <instance id> will auto-detect the id.
  18570. *------------------------------------------*/
  18571. BUILDIN_FUNC(instance_announce) {
  18572. int instance_id = script_getnum(st,2);
  18573. const char *mes = script_getstr(st,3);
  18574. int flag = script_getnum(st,4);
  18575. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  18576. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  18577. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  18578. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  18579. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  18580. if (instance_id <= 0)
  18581. instance_id = script_instancegetid(st);
  18582. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18583. if (instance_id <= 0 || !idata) {
  18584. ShowError("buildin_instance_announce: Instance not found.\n");
  18585. return SCRIPT_CMD_FAILURE;
  18586. }
  18587. for (const auto &it : idata->map)
  18588. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  18589. return SCRIPT_CMD_SUCCESS;
  18590. }
  18591. /*==========================================
  18592. * instance_check_party [malufett]
  18593. * Values:
  18594. * party_id : Party ID of the invoking character. [Required Parameter]
  18595. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  18596. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18597. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18598. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  18599. * Example 2: instance_check_party (getcharid(1),1,1,99);
  18600. *------------------------------------------*/
  18601. BUILDIN_FUNC(instance_check_party)
  18602. {
  18603. int amount, min, max, i, party_id, c = 0;
  18604. struct party_data *p;
  18605. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  18606. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18607. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18608. if( min < 1 || min > MAX_LEVEL) {
  18609. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  18610. return SCRIPT_CMD_FAILURE;
  18611. } else if( max < 1 || max > MAX_LEVEL) {
  18612. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  18613. return SCRIPT_CMD_FAILURE;
  18614. }
  18615. if( script_hasdata(st,2) )
  18616. party_id = script_getnum(st,2);
  18617. else return SCRIPT_CMD_FAILURE;
  18618. if( !(p = party_search(party_id)) ) {
  18619. script_pushint(st, 0); // Returns false if party does not exist.
  18620. return SCRIPT_CMD_FAILURE;
  18621. }
  18622. for( i = 0; i < MAX_PARTY; i++ ) {
  18623. struct map_session_data *pl_sd;
  18624. if( (pl_sd = p->data[i].sd) )
  18625. if(map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18626. if(pl_sd->status.base_level < min) {
  18627. script_pushint(st, 0);
  18628. return SCRIPT_CMD_SUCCESS;
  18629. } else if(pl_sd->status.base_level > max) {
  18630. script_pushint(st, 0);
  18631. return SCRIPT_CMD_SUCCESS;
  18632. }
  18633. c++;
  18634. }
  18635. }
  18636. if(c < amount)
  18637. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  18638. else
  18639. script_pushint(st, 1);
  18640. return SCRIPT_CMD_SUCCESS;
  18641. }
  18642. /*==========================================
  18643. * instance_check_guild
  18644. * Values:
  18645. * guild_id : Guild ID of the invoking character. [Required Parameter]
  18646. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  18647. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18648. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18649. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  18650. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  18651. *------------------------------------------*/
  18652. BUILDIN_FUNC(instance_check_guild)
  18653. {
  18654. int amount, min, max, i, guild_id = 0, c = 0;
  18655. struct guild *g = NULL;
  18656. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18657. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18658. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18659. if (min < 1 || min > MAX_LEVEL) {
  18660. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18661. return SCRIPT_CMD_FAILURE;
  18662. } else if (max < 1 || max > MAX_LEVEL) {
  18663. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18664. return SCRIPT_CMD_FAILURE;
  18665. }
  18666. if (script_hasdata(st,2))
  18667. guild_id = script_getnum(st,2);
  18668. else
  18669. return SCRIPT_CMD_FAILURE;
  18670. if (!(g = guild_search(guild_id))) {
  18671. script_pushint(st, 0); // Returns false if guild does not exist.
  18672. return SCRIPT_CMD_FAILURE;
  18673. }
  18674. for(i = 0; i < MAX_GUILD; i++) {
  18675. struct map_session_data *pl_sd;
  18676. if ((pl_sd = g->member[i].sd)) {
  18677. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18678. if (pl_sd->status.base_level < min) {
  18679. script_pushint(st, 0);
  18680. return SCRIPT_CMD_SUCCESS;
  18681. } else if (pl_sd->status.base_level > max) {
  18682. script_pushint(st, 0);
  18683. return SCRIPT_CMD_SUCCESS;
  18684. }
  18685. c++;
  18686. }
  18687. }
  18688. }
  18689. if (c < amount)
  18690. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18691. else
  18692. script_pushint(st, 1);
  18693. return SCRIPT_CMD_SUCCESS;
  18694. }
  18695. /*==========================================
  18696. * instance_check_clan
  18697. * Values:
  18698. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18699. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18700. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18701. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18702. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18703. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18704. *------------------------------------------*/
  18705. BUILDIN_FUNC(instance_check_clan)
  18706. {
  18707. int amount, min, max, i, clan_id = 0, c = 0;
  18708. struct clan *cd = NULL;
  18709. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18710. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18711. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18712. if (min < 1 || min > MAX_LEVEL) {
  18713. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18714. return SCRIPT_CMD_FAILURE;
  18715. } else if (max < 1 || max > MAX_LEVEL) {
  18716. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18717. return SCRIPT_CMD_FAILURE;
  18718. }
  18719. if (script_hasdata(st,2))
  18720. clan_id = script_getnum(st,2);
  18721. else
  18722. return SCRIPT_CMD_FAILURE;
  18723. if (!(cd = clan_search(clan_id))) {
  18724. script_pushint(st, 0); // Returns false if clan does not exist.
  18725. return SCRIPT_CMD_FAILURE;
  18726. }
  18727. for(i = 0; i < MAX_CLAN; i++) {
  18728. struct map_session_data *pl_sd;
  18729. if ((pl_sd = cd->members[i])) {
  18730. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18731. if (pl_sd->status.base_level < min) {
  18732. script_pushint(st, 0);
  18733. return SCRIPT_CMD_SUCCESS;
  18734. } else if (pl_sd->status.base_level > max) {
  18735. script_pushint(st, 0);
  18736. return SCRIPT_CMD_SUCCESS;
  18737. }
  18738. c++;
  18739. }
  18740. }
  18741. }
  18742. if (c < amount)
  18743. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18744. else
  18745. script_pushint(st, 1);
  18746. return SCRIPT_CMD_SUCCESS;
  18747. }
  18748. /*==========================================
  18749. * instance_info
  18750. * Values:
  18751. * name : name of the instance you want to look up. [Required Parameter]
  18752. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18753. * index : index of the map in the instance. [Optional Parameter]
  18754. *------------------------------------------*/
  18755. BUILDIN_FUNC(instance_info)
  18756. {
  18757. const char* name = script_getstr(st, 2);
  18758. int type = script_getnum(st, 3);
  18759. int index = 0;
  18760. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18761. if (!db) {
  18762. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18763. script_pushint(st, -1);
  18764. return SCRIPT_CMD_FAILURE;
  18765. }
  18766. switch( type ){
  18767. case IIT_ID:
  18768. script_pushint(st, db->id);
  18769. break;
  18770. case IIT_TIME_LIMIT:
  18771. script_pushint(st, db->limit);
  18772. break;
  18773. case IIT_IDLE_TIMEOUT:
  18774. script_pushint(st, db->timeout);
  18775. break;
  18776. case IIT_ENTER_MAP:
  18777. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18778. break;
  18779. case IIT_ENTER_X:
  18780. script_pushint(st, db->enter.x);
  18781. break;
  18782. case IIT_ENTER_Y:
  18783. script_pushint(st, db->enter.y);
  18784. break;
  18785. case IIT_MAPCOUNT:
  18786. script_pushint(st, db->maplist.size());
  18787. break;
  18788. case IIT_MAP:
  18789. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18790. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18791. script_pushconststr(st, "");
  18792. return SCRIPT_CMD_FAILURE;
  18793. }
  18794. index = script_getnum(st, 4);
  18795. if( index < 0 ){
  18796. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18797. script_pushconststr(st, "");
  18798. return SCRIPT_CMD_FAILURE;
  18799. }
  18800. if( index > UINT8_MAX ){
  18801. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18802. script_pushconststr(st, "");
  18803. return SCRIPT_CMD_FAILURE;
  18804. }
  18805. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18806. break;
  18807. default:
  18808. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18809. script_pushint(st, -1);
  18810. return SCRIPT_CMD_FAILURE;
  18811. }
  18812. return SCRIPT_CMD_SUCCESS;
  18813. }
  18814. /*------------------------------------------
  18815. *instance_live_info( <info type>{, <instance id>} );
  18816. - ILI_NAME : Instance Name
  18817. - ILI_MODE : Instance Mode
  18818. - ILI_OWNER : owner id
  18819. *------------------------------------------*/
  18820. BUILDIN_FUNC(instance_live_info)
  18821. {
  18822. int type = script_getnum(st, 2);
  18823. int id = 0;
  18824. if (type < ILI_NAME || type > ILI_OWNER) {
  18825. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18826. script_pushint(st, -1);
  18827. return SCRIPT_CMD_FAILURE;
  18828. }
  18829. if (!script_hasdata(st, 3))
  18830. id = script_instancegetid(st);
  18831. else
  18832. id = script_getnum(st, 3);
  18833. std::shared_ptr<s_instance_db> db = nullptr;
  18834. std::shared_ptr<s_instance_data> im = nullptr;
  18835. if (id > 0 && id < INT_MAX) {
  18836. im = util::umap_find(instances, id);
  18837. if (im)
  18838. db = instance_db.find(im->id);
  18839. }
  18840. if (!im || !db) {
  18841. if (type == ILI_NAME)
  18842. script_pushconststr(st, "");
  18843. else
  18844. script_pushint(st, -1);
  18845. return SCRIPT_CMD_SUCCESS;
  18846. }
  18847. switch( type ) {
  18848. case ILI_NAME:
  18849. script_pushstrcopy(st, db->name.c_str());
  18850. break;
  18851. case ILI_MODE:
  18852. script_pushint(st, im->mode);
  18853. break;
  18854. case ILI_OWNER:
  18855. script_pushint(st, im->owner_id);
  18856. break;
  18857. }
  18858. return SCRIPT_CMD_SUCCESS;
  18859. }
  18860. /*==========================================
  18861. * instance_list(<"map name">{,<instance mode>});
  18862. * set '.@instance_list' to a list of the live instance ids for the map with the mode.
  18863. * return the array size of '.@instance_list'
  18864. *------------------------------------------*/
  18865. BUILDIN_FUNC(instance_list)
  18866. {
  18867. int src_id = map_mapname2mapid(script_getstr(st, 2));
  18868. if (src_id == 0) {
  18869. ShowError("buildin_instance_list: map '%s' doesn't exist\n", script_getstr(st, 2));
  18870. return SCRIPT_CMD_FAILURE;
  18871. }
  18872. e_instance_mode mode = IM_MAX;
  18873. if (script_hasdata(st, 3)) {
  18874. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18875. if (mode < IM_NONE || mode >= IM_MAX) {
  18876. ShowError("buildin_instance_list: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 3));
  18877. return SCRIPT_CMD_FAILURE;
  18878. }
  18879. }
  18880. int j = 0;
  18881. for (int i = instance_start; i < map_num; i++) {
  18882. struct map_data* mapdata = &map[i];
  18883. if (mapdata->instance_src_map == src_id) {
  18884. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, mapdata->instance_id);
  18885. if (idata && (mode == IM_MAX || idata->mode == mode)) {
  18886. setd_sub_num(st, nullptr, ".@instance_list", j, mapdata->instance_id, nullptr);
  18887. j++;
  18888. }
  18889. }
  18890. }
  18891. script_pushint(st, j);
  18892. return SCRIPT_CMD_SUCCESS;
  18893. }
  18894. /*==========================================
  18895. * Custom Fonts
  18896. *------------------------------------------*/
  18897. BUILDIN_FUNC(setfont)
  18898. {
  18899. struct map_session_data *sd;
  18900. int font;
  18901. if( !script_rid2sd(sd) )
  18902. return SCRIPT_CMD_SUCCESS;
  18903. font = script_getnum(st,2);
  18904. if( sd->status.font != font )
  18905. sd->status.font = font;
  18906. else
  18907. sd->status.font = 0;
  18908. clif_font(sd);
  18909. return SCRIPT_CMD_SUCCESS;
  18910. }
  18911. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  18912. {
  18913. TBL_MOB* md = (TBL_MOB*)bl;
  18914. struct block_list *tbl;
  18915. int mobid = va_arg(ap,int);
  18916. uint16 skill_id = va_arg(ap,int);
  18917. uint16 skill_lv = va_arg(ap,int);
  18918. int casttime = va_arg(ap,int);
  18919. int cancel = va_arg(ap,int);
  18920. int emotion = va_arg(ap,int);
  18921. int target = va_arg(ap,int);
  18922. if( md->mob_id != mobid )
  18923. return 0;
  18924. // 0:self, 1:target, 2:master, default:random
  18925. switch( target ) {
  18926. case 0: tbl = map_id2bl(md->bl.id); break;
  18927. case 1: tbl = map_id2bl(md->target_id); break;
  18928. case 2: tbl = map_id2bl(md->master_id); break;
  18929. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  18930. }
  18931. if( !tbl )
  18932. return 0;
  18933. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  18934. unit_skillcastcancel(bl,0);
  18935. if( skill_get_casttype(skill_id) == CAST_GROUND )
  18936. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  18937. else
  18938. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  18939. clif_emotion(&md->bl, emotion);
  18940. return 1;
  18941. }
  18942. /*==========================================
  18943. * areamobuseskill "<Map Name>",<x>,<y>,<range>,"<Mob name>"/<Mob ID>,"<Skill Name>"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  18944. *------------------------------------------*/
  18945. BUILDIN_FUNC(areamobuseskill)
  18946. {
  18947. struct block_list center;
  18948. int16 m;
  18949. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  18950. ShowError("areamobuseskill: invalid map name.\n");
  18951. return SCRIPT_CMD_SUCCESS;
  18952. }
  18953. center.m = m;
  18954. center.x = script_getnum(st,3);
  18955. center.y = script_getnum(st,4);
  18956. int range = script_getnum( st,5 );
  18957. uint16 mobid;
  18958. if( script_isstring( st, 6 ) ){
  18959. const char* name = script_getstr( st, 6 );
  18960. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname( name );
  18961. if( mob == nullptr ){
  18962. ShowWarning( "buildin_areamobuseskill: Attempted to use skill of non-existing monster \"%s\"\n", name );
  18963. return SCRIPT_CMD_FAILURE;
  18964. }
  18965. mobid = mob->id;
  18966. }else{
  18967. mobid = script_getnum( st, 6 );
  18968. if( !mob_db.exists( mobid ) ){
  18969. ShowWarning( "buildin_areamobuseskill: Attempted to use skill of non-existing monster class %d\n", mobid );
  18970. return SCRIPT_CMD_FAILURE;
  18971. }
  18972. }
  18973. int skill_id;
  18974. if (script_isstring(st, 7)) {
  18975. const char *name = script_getstr(st, 7);
  18976. if (!(skill_id = skill_name2id(name))) {
  18977. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  18978. return SCRIPT_CMD_FAILURE;
  18979. }
  18980. } else {
  18981. skill_id = script_getnum(st, 7);
  18982. if (!skill_get_index(skill_id)) {
  18983. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  18984. return SCRIPT_CMD_FAILURE;
  18985. }
  18986. }
  18987. int skill_lv = script_getnum( st, 8 );
  18988. if( skill_lv > battle_config.mob_max_skilllvl ){
  18989. skill_lv = battle_config.mob_max_skilllvl;
  18990. }
  18991. int casttime = script_getnum( st, 9 );
  18992. int cancel = script_getnum( st, 10 );
  18993. int emotion = script_getnum( st, 11 );
  18994. int target = script_getnum( st, 12 );
  18995. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  18996. return SCRIPT_CMD_SUCCESS;
  18997. }
  18998. /**
  18999. * Display a progress bar above a character
  19000. * progressbar "<color>",<seconds>;
  19001. */
  19002. BUILDIN_FUNC(progressbar)
  19003. {
  19004. struct map_session_data * sd;
  19005. const char * color;
  19006. unsigned int second;
  19007. if( !script_rid2sd(sd) )
  19008. return SCRIPT_CMD_SUCCESS;
  19009. st->state = STOP;
  19010. color = script_getstr(st,2);
  19011. second = script_getnum(st,3);
  19012. sd->progressbar.npc_id = st->oid;
  19013. sd->progressbar.timeout = gettick() + second*1000;
  19014. sd->state.workinprogress = WIP_DISABLE_ALL;
  19015. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  19016. return SCRIPT_CMD_SUCCESS;
  19017. }
  19018. /**
  19019. * Display a progress bar above an NPC
  19020. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  19021. */
  19022. BUILDIN_FUNC(progressbar_npc){
  19023. map_session_data *sd = map_id2sd(st->rid);
  19024. struct npc_data* nd = NULL;
  19025. if( script_hasdata(st, 4) ){
  19026. const char* name = script_getstr(st, 4);
  19027. nd = npc_name2id(name);
  19028. if( !nd ){
  19029. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  19030. return SCRIPT_CMD_FAILURE;
  19031. }
  19032. }else{
  19033. nd = map_id2nd(st->oid);
  19034. }
  19035. // First call(by function call)
  19036. if( !nd->progressbar.timeout ){
  19037. const char *color;
  19038. int second;
  19039. color = script_getstr(st, 2);
  19040. second = script_getnum(st, 3);
  19041. if( second < 0 ){
  19042. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  19043. return SCRIPT_CMD_FAILURE;
  19044. }
  19045. if (sd) { // Player attached - keep them from doing other things
  19046. sd->state.workinprogress = WIP_DISABLE_ALL;
  19047. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  19048. }
  19049. // sleep for the target amount of time
  19050. st->state = RERUNLINE;
  19051. st->sleep.tick = second * 1000;
  19052. nd->progressbar.timeout = gettick() + second * 1000;
  19053. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  19054. clif_progressbar_npc_area(nd);
  19055. // Second call(by timer after sleeping time is over)
  19056. } else {
  19057. // Continue the script
  19058. if (sd) { // Player attached - remove restrictions
  19059. sd->state.workinprogress = WIP_DISABLE_NONE;
  19060. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  19061. }
  19062. st->state = RUN;
  19063. st->sleep.tick = 0;
  19064. nd->progressbar.timeout = nd->progressbar.color = 0;
  19065. }
  19066. return SCRIPT_CMD_SUCCESS;
  19067. }
  19068. BUILDIN_FUNC(pushpc)
  19069. {
  19070. uint8 dir;
  19071. int cells, dx, dy;
  19072. struct map_session_data* sd;
  19073. if(!script_rid2sd(sd))
  19074. {
  19075. return SCRIPT_CMD_SUCCESS;
  19076. }
  19077. dir = script_getnum(st,2);
  19078. cells = script_getnum(st,3);
  19079. if(dir >= DIR_MAX)
  19080. {
  19081. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  19082. script_reportsrc(st);
  19083. dir%= DIR_MAX; // trim spin-over
  19084. }
  19085. if(!cells)
  19086. {// zero distance
  19087. return SCRIPT_CMD_SUCCESS;
  19088. }
  19089. else if(cells<0)
  19090. {// pushing backwards
  19091. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  19092. cells = -cells;
  19093. }
  19094. dx = dirx[dir];
  19095. dy = diry[dir];
  19096. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  19097. return SCRIPT_CMD_SUCCESS;
  19098. }
  19099. /// Invokes buying store preparation window
  19100. /// buyingstore <slots>;
  19101. BUILDIN_FUNC(buyingstore)
  19102. {
  19103. struct map_session_data* sd;
  19104. if( !script_rid2sd(sd) )
  19105. {
  19106. return SCRIPT_CMD_SUCCESS;
  19107. }
  19108. if( npc_isnear(&sd->bl) ) {
  19109. char output[150];
  19110. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  19111. clif_displaymessage(sd->fd, output);
  19112. return SCRIPT_CMD_SUCCESS;
  19113. }
  19114. buyingstore_setup(sd, script_getnum(st,2));
  19115. return SCRIPT_CMD_SUCCESS;
  19116. }
  19117. /// Invokes search store info window
  19118. /// searchstores <uses>,<effect>;
  19119. BUILDIN_FUNC(searchstores)
  19120. {
  19121. unsigned short effect;
  19122. unsigned int uses;
  19123. struct map_session_data* sd;
  19124. if( !script_rid2sd(sd) )
  19125. {
  19126. return SCRIPT_CMD_SUCCESS;
  19127. }
  19128. uses = script_getnum(st,2);
  19129. effect = script_getnum(st,3);
  19130. if( !uses )
  19131. {
  19132. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  19133. return SCRIPT_CMD_FAILURE;
  19134. }
  19135. if( effect > 1 )
  19136. {
  19137. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  19138. return SCRIPT_CMD_FAILURE;
  19139. }
  19140. searchstore_open(sd, uses, effect);
  19141. return SCRIPT_CMD_SUCCESS;
  19142. }
  19143. /// Displays a number as large digital clock.
  19144. /// showdigit <value>[,<type>];
  19145. BUILDIN_FUNC(showdigit)
  19146. {
  19147. unsigned int type = 0;
  19148. int value;
  19149. struct map_session_data* sd;
  19150. if( !script_rid2sd(sd) )
  19151. {
  19152. return SCRIPT_CMD_SUCCESS;
  19153. }
  19154. value = script_getnum(st,2);
  19155. if( script_hasdata(st,3) ){
  19156. type = script_getnum(st,3);
  19157. }
  19158. switch( type ){
  19159. case 0:
  19160. break;
  19161. case 1:
  19162. case 2:
  19163. // Use absolute value and then the negative value of it as starting value
  19164. // This is what gravity's client does for these counters
  19165. value = -abs(value);
  19166. break;
  19167. case 3:
  19168. value = abs(value);
  19169. if( value > 99 ){
  19170. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  19171. script_reportsrc(st);
  19172. value = 99;
  19173. }
  19174. break;
  19175. default:
  19176. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  19177. return SCRIPT_CMD_FAILURE;
  19178. }
  19179. clif_showdigit(sd, (unsigned char)type, value);
  19180. return SCRIPT_CMD_SUCCESS;
  19181. }
  19182. /**
  19183. * Rune Knight
  19184. * makerune <% success bonus>{,<char_id>};
  19185. **/
  19186. BUILDIN_FUNC(makerune) {
  19187. TBL_PC* sd;
  19188. if (!script_charid2sd(3,sd))
  19189. return SCRIPT_CMD_FAILURE;
  19190. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  19191. sd->itemid = script_getnum(st,2);
  19192. return SCRIPT_CMD_SUCCESS;
  19193. }
  19194. /**
  19195. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  19196. **/
  19197. BUILDIN_FUNC(checkdragon) {
  19198. TBL_PC* sd;
  19199. if (!script_charid2sd(2,sd))
  19200. return SCRIPT_CMD_FAILURE;
  19201. if( pc_isridingdragon(sd) )
  19202. script_pushint(st,1);
  19203. else
  19204. script_pushint(st,0);
  19205. return SCRIPT_CMD_SUCCESS;
  19206. }
  19207. /**
  19208. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  19209. * - Toggles the dragon on a RK if he can mount;
  19210. * @param Color - when not provided uses the green dragon;
  19211. * - 1 : Green Dragon
  19212. * - 2 : Brown Dragon
  19213. * - 3 : Gray Dragon
  19214. * - 4 : Blue Dragon
  19215. * - 5 : Red Dragon
  19216. **/
  19217. BUILDIN_FUNC(setdragon) {
  19218. TBL_PC* sd;
  19219. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  19220. if (!script_charid2sd(3,sd))
  19221. return SCRIPT_CMD_FAILURE;
  19222. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  19223. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  19224. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  19225. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  19226. script_pushint(st,1);
  19227. } else {//Not mounted; Mount now.
  19228. unsigned int option = OPTION_DRAGON1;
  19229. if( color ) {
  19230. option = ( color == 1 ? OPTION_DRAGON1 :
  19231. color == 2 ? OPTION_DRAGON2 :
  19232. color == 3 ? OPTION_DRAGON3 :
  19233. color == 4 ? OPTION_DRAGON4 :
  19234. color == 5 ? OPTION_DRAGON5 : 0);
  19235. if( !option ) {
  19236. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  19237. option = OPTION_DRAGON1;
  19238. }
  19239. }
  19240. pc_setoption(sd, sd->sc.option|option);
  19241. script_pushint(st,1);
  19242. }
  19243. return SCRIPT_CMD_SUCCESS;
  19244. }
  19245. /**
  19246. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  19247. **/
  19248. BUILDIN_FUNC(ismounting) {
  19249. TBL_PC* sd;
  19250. if (!script_charid2sd(2,sd))
  19251. return SCRIPT_CMD_FAILURE;
  19252. if( sd->sc.data[SC_ALL_RIDING] )
  19253. script_pushint(st,1);
  19254. else
  19255. script_pushint(st,0);
  19256. return SCRIPT_CMD_SUCCESS;
  19257. }
  19258. /**
  19259. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  19260. * - Toggles new mounts on a player when he can mount
  19261. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  19262. * - Will unmount the player is he is already mounting
  19263. **/
  19264. BUILDIN_FUNC(setmounting) {
  19265. TBL_PC* sd;
  19266. if (!script_charid2sd(2,sd))
  19267. return SCRIPT_CMD_FAILURE;
  19268. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  19269. clif_msg(sd, NEED_REINS_OF_MOUNT);
  19270. script_pushint(st,0); //can't mount with one of these
  19271. } else if (sd->sc.data[SC_CLOAKING] || sd->sc.data[SC_CHASEWALK] || sd->sc.data[SC_CLOAKINGEXCEED] || sd->sc.data[SC_CAMOUFLAGE] || sd->sc.data[SC_STEALTHFIELD] || sd->sc.data[SC__FEINTBOMB]) {
  19272. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  19273. script_pushint(st, 0); // Silent failure
  19274. } else {
  19275. if( sd->sc.data[SC_ALL_RIDING] )
  19276. status_change_end(&sd->bl, SC_ALL_RIDING); //release mount
  19277. else
  19278. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  19279. script_pushint(st,1);//in both cases, return 1.
  19280. }
  19281. return SCRIPT_CMD_SUCCESS;
  19282. }
  19283. /**
  19284. * Retrieves quantity of arguments provided to callfunc/callsub.
  19285. * getargcount() -> amount of arguments received in a function
  19286. **/
  19287. BUILDIN_FUNC(getargcount) {
  19288. struct script_retinfo* ri;
  19289. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  19290. ShowError("script:getargcount: used out of function or callsub label!\n");
  19291. st->state = END;
  19292. return SCRIPT_CMD_FAILURE;
  19293. }
  19294. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  19295. script_pushint(st, ri->nargs);
  19296. return SCRIPT_CMD_SUCCESS;
  19297. }
  19298. /**
  19299. * getcharip(<account ID>/<character ID>/<character name>)
  19300. **/
  19301. BUILDIN_FUNC(getcharip)
  19302. {
  19303. struct map_session_data* sd = NULL;
  19304. /* check if a character name is specified */
  19305. if( script_hasdata(st, 2) )
  19306. {
  19307. if (script_isstring(st, 2))
  19308. sd = map_nick2sd(script_getstr(st, 2),false);
  19309. else
  19310. {
  19311. int id = script_getnum(st, 2);
  19312. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  19313. }
  19314. }
  19315. else
  19316. script_rid2sd(sd);
  19317. /* check for sd and IP */
  19318. if (!sd || !session[sd->fd]->client_addr)
  19319. {
  19320. script_pushconststr(st, "");
  19321. return SCRIPT_CMD_SUCCESS;
  19322. }
  19323. /* return the client ip_addr converted for output */
  19324. if (sd && sd->fd && session[sd->fd])
  19325. {
  19326. /* initiliaze */
  19327. const char *ip_addr = NULL;
  19328. uint32 ip;
  19329. /* set ip, ip_addr and convert to ip and push str */
  19330. ip = session[sd->fd]->client_addr;
  19331. ip_addr = ip2str(ip, NULL);
  19332. script_pushstrcopy(st, ip_addr);
  19333. }
  19334. return SCRIPT_CMD_SUCCESS;
  19335. }
  19336. /**
  19337. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  19338. **/
  19339. BUILDIN_FUNC(is_function) {
  19340. const char* str = script_getstr(st,2);
  19341. if( strdb_exists(userfunc_db, str) )
  19342. script_pushint(st,1);
  19343. else
  19344. script_pushint(st,0);
  19345. return SCRIPT_CMD_SUCCESS;
  19346. }
  19347. /**
  19348. * get_revision() -> retrieves the current svn revision (if available)
  19349. **/
  19350. BUILDIN_FUNC(get_revision) {
  19351. const char *svn = get_svn_revision();
  19352. if ( svn[0] != UNKNOWN_VERSION )
  19353. script_pushint(st,atoi(svn));
  19354. else
  19355. script_pushint(st,-1); //unknown
  19356. return SCRIPT_CMD_SUCCESS;
  19357. }
  19358. /* get_hash() -> retrieves the current git hash (if available)*/
  19359. BUILDIN_FUNC(get_githash) {
  19360. const char* git = get_git_hash();
  19361. char buf[CHAT_SIZE_MAX];
  19362. safestrncpy(buf,git,strlen(git)+1);
  19363. if ( git[0] != UNKNOWN_VERSION )
  19364. script_pushstrcopy(st,buf);
  19365. else
  19366. script_pushconststr(st,"Unknown"); //unknown
  19367. return SCRIPT_CMD_SUCCESS;
  19368. }
  19369. /**
  19370. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  19371. **/
  19372. BUILDIN_FUNC(freeloop) {
  19373. if( script_hasdata(st,2) ) {
  19374. if( script_getnum(st,2) )
  19375. st->freeloop = 1;
  19376. else
  19377. st->freeloop = 0;
  19378. }
  19379. script_pushint(st, st->freeloop);
  19380. return SCRIPT_CMD_SUCCESS;
  19381. }
  19382. /**
  19383. * @commands (script based)
  19384. **/
  19385. BUILDIN_FUNC(bindatcmd) {
  19386. const char* atcmd;
  19387. const char* eventName;
  19388. int i, level = 0, level2 = 100;
  19389. bool create = false;
  19390. atcmd = script_getstr(st,2);
  19391. eventName = script_getstr(st,3);
  19392. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19393. atcmd++;
  19394. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  19395. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  19396. if( atcmd_binding_count == 0 ) {
  19397. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  19398. create = true;
  19399. } else {
  19400. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  19401. if( i < atcmd_binding_count ) {/* update existent entry */
  19402. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19403. atcmd_binding[i]->level = level;
  19404. atcmd_binding[i]->level2 = level2;
  19405. } else
  19406. create = true;
  19407. }
  19408. if( create ) {
  19409. i = atcmd_binding_count;
  19410. if( atcmd_binding_count++ != 0 )
  19411. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  19412. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  19413. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  19414. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19415. atcmd_binding[i]->level = level;
  19416. atcmd_binding[i]->level2 = level2;
  19417. }
  19418. return SCRIPT_CMD_SUCCESS;
  19419. }
  19420. BUILDIN_FUNC(unbindatcmd) {
  19421. const char* atcmd;
  19422. int i = 0;
  19423. atcmd = script_getstr(st, 2);
  19424. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19425. atcmd++;
  19426. if( atcmd_binding_count == 0 ) {
  19427. script_pushint(st, 0);
  19428. return SCRIPT_CMD_SUCCESS;
  19429. }
  19430. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  19431. if( i < atcmd_binding_count ) {
  19432. int cursor = 0;
  19433. aFree(atcmd_binding[i]);
  19434. atcmd_binding[i] = NULL;
  19435. /* compact the list now that we freed a slot somewhere */
  19436. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  19437. if( atcmd_binding[i] == NULL )
  19438. continue;
  19439. if( cursor != i ) {
  19440. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  19441. }
  19442. cursor++;
  19443. }
  19444. if( (atcmd_binding_count = cursor) == 0 )
  19445. aFree(atcmd_binding);
  19446. script_pushint(st, 1);
  19447. } else
  19448. script_pushint(st, 0);/* not found */
  19449. return SCRIPT_CMD_SUCCESS;
  19450. }
  19451. BUILDIN_FUNC(useatcmd) {
  19452. return atcommand_sub(st,3);
  19453. }
  19454. BUILDIN_FUNC(checkre)
  19455. {
  19456. int num;
  19457. num=script_getnum(st,2);
  19458. switch(num){
  19459. case 0:
  19460. #ifdef RENEWAL
  19461. script_pushint(st, 1);
  19462. #else
  19463. script_pushint(st, 0);
  19464. #endif
  19465. break;
  19466. case 1:
  19467. #ifdef RENEWAL_CAST
  19468. script_pushint(st, 1);
  19469. #else
  19470. script_pushint(st, 0);
  19471. #endif
  19472. break;
  19473. case 2:
  19474. #ifdef RENEWAL_DROP
  19475. script_pushint(st, 1);
  19476. #else
  19477. script_pushint(st, 0);
  19478. #endif
  19479. break;
  19480. case 3:
  19481. #ifdef RENEWAL_EXP
  19482. script_pushint(st, 1);
  19483. #else
  19484. script_pushint(st, 0);
  19485. #endif
  19486. break;
  19487. case 4:
  19488. #ifdef RENEWAL_LVDMG
  19489. script_pushint(st, 1);
  19490. #else
  19491. script_pushint(st, 0);
  19492. #endif
  19493. break;
  19494. case 5:
  19495. #ifdef RENEWAL_ASPD
  19496. script_pushint(st, 1);
  19497. #else
  19498. script_pushint(st, 0);
  19499. #endif
  19500. break;
  19501. default:
  19502. ShowWarning("buildin_checkre: unknown parameter.\n");
  19503. break;
  19504. }
  19505. return SCRIPT_CMD_SUCCESS;
  19506. }
  19507. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  19508. BUILDIN_FUNC(getrandgroupitem) {
  19509. TBL_PC* sd;
  19510. int i, get_count = 0, identify = 0;
  19511. uint16 group, qty = 0;
  19512. uint8 sub_group = 1;
  19513. struct item item_tmp;
  19514. if (!script_charid2sd(6, sd))
  19515. return SCRIPT_CMD_SUCCESS;
  19516. group = script_getnum(st,2);
  19517. if (!group) {
  19518. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  19519. return SCRIPT_CMD_FAILURE;
  19520. }
  19521. FETCH(3, qty);
  19522. FETCH(4, sub_group);
  19523. FETCH(5, identify);
  19524. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(group,sub_group);
  19525. if (!entry)
  19526. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  19527. memset(&item_tmp,0,sizeof(item_tmp));
  19528. item_tmp.nameid = entry->nameid;
  19529. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  19530. if (!qty)
  19531. qty = entry->amount;
  19532. //Check if it's stackable.
  19533. if (!itemdb_isstackable(entry->nameid)) {
  19534. item_tmp.amount = 1;
  19535. get_count = qty;
  19536. }
  19537. else {
  19538. item_tmp.amount = qty;
  19539. get_count = 1;
  19540. }
  19541. for (i = 0; i < get_count; i++) {
  19542. // if not pet egg
  19543. if (!pet_create_egg(sd, entry->nameid)) {
  19544. unsigned char flag = 0;
  19545. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  19546. clif_additem(sd,0,0,flag);
  19547. if (pc_candrop(sd,&item_tmp))
  19548. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  19549. }
  19550. }
  19551. }
  19552. return SCRIPT_CMD_SUCCESS;
  19553. }
  19554. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  19555. * Gives item(s) to the attached player based on item group contents
  19556. */
  19557. BUILDIN_FUNC(getgroupitem) {
  19558. TBL_PC *sd;
  19559. int group_id = script_getnum(st,2);
  19560. if (!script_charid2sd(4,sd))
  19561. return SCRIPT_CMD_SUCCESS;
  19562. if (itemdb_group.pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  19563. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  19564. return SCRIPT_CMD_FAILURE;
  19565. }
  19566. return SCRIPT_CMD_SUCCESS;
  19567. }
  19568. /* cleanmap <map_name>;
  19569. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  19570. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  19571. {
  19572. nullpo_ret(bl);
  19573. map_clearflooritem(bl);
  19574. return SCRIPT_CMD_SUCCESS;
  19575. }
  19576. BUILDIN_FUNC(cleanmap)
  19577. {
  19578. const char *mapname;
  19579. int16 m;
  19580. mapname = script_getstr(st, 2);
  19581. m = map_mapname2mapid(mapname);
  19582. if (!m)
  19583. return SCRIPT_CMD_FAILURE;
  19584. if ((script_lastdata(st) - 2) < 4) {
  19585. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  19586. } else {
  19587. int16 x0 = script_getnum(st, 3);
  19588. int16 y0 = script_getnum(st, 4);
  19589. int16 x1 = script_getnum(st, 5);
  19590. int16 y1 = script_getnum(st, 6);
  19591. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  19592. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  19593. } else {
  19594. ShowError("cleanarea: invalid coordinate defined!\n");
  19595. return SCRIPT_CMD_FAILURE;
  19596. }
  19597. }
  19598. return SCRIPT_CMD_SUCCESS;
  19599. }
  19600. /* Cast a skill on the attached player.
  19601. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  19602. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  19603. BUILDIN_FUNC(npcskill)
  19604. {
  19605. uint16 skill_id;
  19606. unsigned short skill_level;
  19607. unsigned int stat_point;
  19608. unsigned int npc_level;
  19609. struct npc_data *nd;
  19610. struct map_session_data *sd;
  19611. if( !script_rid2sd(sd) )
  19612. return SCRIPT_CMD_SUCCESS;
  19613. if (script_isstring(st, 2)) {
  19614. const char *name = script_getstr(st, 2);
  19615. if (!(skill_id = skill_name2id(name))) {
  19616. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  19617. return SCRIPT_CMD_FAILURE;
  19618. }
  19619. } else {
  19620. skill_id = script_getnum(st, 2);
  19621. if (!skill_get_index(skill_id)) {
  19622. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  19623. return SCRIPT_CMD_FAILURE;
  19624. }
  19625. }
  19626. skill_level = script_getnum(st, 3);
  19627. stat_point = script_getnum(st, 4);
  19628. npc_level = script_getnum(st, 5);
  19629. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  19630. if (stat_point > battle_config.max_third_parameter) {
  19631. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  19632. return SCRIPT_CMD_FAILURE;
  19633. }
  19634. if (npc_level > MAX_LEVEL) {
  19635. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  19636. return SCRIPT_CMD_FAILURE;
  19637. }
  19638. if (nd == NULL) { //ain't possible, but I don't trust people.
  19639. return SCRIPT_CMD_FAILURE;
  19640. }
  19641. nd->level = npc_level;
  19642. nd->stat_point = stat_point;
  19643. if (!nd->status.hp)
  19644. status_calc_npc(nd, SCO_FIRST);
  19645. else
  19646. status_calc_npc(nd, SCO_NONE);
  19647. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  19648. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  19649. else
  19650. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  19651. return SCRIPT_CMD_SUCCESS;
  19652. }
  19653. /* Consumes an item.
  19654. * consumeitem <item id>{,<char_id>};
  19655. * consumeitem "<item name>"{,<char_id>};
  19656. * @param item: Item ID or name
  19657. */
  19658. BUILDIN_FUNC(consumeitem)
  19659. {
  19660. struct map_session_data *sd;
  19661. std::shared_ptr<item_data> item_data;
  19662. if (!script_charid2sd(3, sd))
  19663. return SCRIPT_CMD_FAILURE;
  19664. if( script_isstring(st, 2) ){
  19665. const char *name = script_getstr(st, 2);
  19666. item_data = item_db.searchname( name );
  19667. if( item_data == nullptr ){
  19668. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  19669. return SCRIPT_CMD_FAILURE;
  19670. }
  19671. } else {
  19672. t_itemid nameid = script_getnum(st, 2);
  19673. item_data = item_db.find( nameid );
  19674. if( item_data == nullptr ){
  19675. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  19676. return SCRIPT_CMD_FAILURE;
  19677. }
  19678. }
  19679. run_script( item_data->script, 0, sd->bl.id, 0 );
  19680. return SCRIPT_CMD_SUCCESS;
  19681. }
  19682. /** Makes a player sit/stand.
  19683. * sit {"<character name>"};
  19684. * stand {"<character name>"};
  19685. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  19686. * @param name: Player name that will be invoked
  19687. */
  19688. BUILDIN_FUNC(sit)
  19689. {
  19690. TBL_PC *sd;
  19691. if( !script_nick2sd(2,sd) )
  19692. return SCRIPT_CMD_FAILURE;
  19693. if( !pc_issit(sd) ) {
  19694. pc_setsit(sd);
  19695. skill_sit(sd, 1);
  19696. clif_sitting(&sd->bl);
  19697. }
  19698. return SCRIPT_CMD_SUCCESS;
  19699. }
  19700. /** Makes player to stand
  19701. * @param name: Player name that will be set
  19702. */
  19703. BUILDIN_FUNC(stand)
  19704. {
  19705. TBL_PC *sd;
  19706. if( !script_nick2sd(2,sd) )
  19707. return SCRIPT_CMD_FAILURE;
  19708. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19709. skill_sit(sd, 0);
  19710. clif_standing(&sd->bl);
  19711. }
  19712. return SCRIPT_CMD_SUCCESS;
  19713. }
  19714. /** Creates an array of bounded item IDs
  19715. * countbound {<type>{,<char_id>}};
  19716. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19717. * @return amt: Total number of different items type found
  19718. */
  19719. BUILDIN_FUNC(countbound)
  19720. {
  19721. TBL_PC *sd;
  19722. if (!script_charid2sd(3,sd))
  19723. return SCRIPT_CMD_FAILURE;
  19724. int i, k = 0;
  19725. int type = script_getnum(st,2);
  19726. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19727. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19728. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19729. ))
  19730. {
  19731. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19732. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19733. k++;
  19734. }
  19735. }
  19736. script_pushint(st,k);
  19737. return SCRIPT_CMD_SUCCESS;
  19738. }
  19739. /** Creates new party
  19740. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19741. * @param party_name: String of party name that will be created
  19742. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19743. * @param item_share: 0-Each Take. 1-Party Share
  19744. * @param item_share_type: 0-Each Take. 1-Even Share
  19745. * @return val: Result value
  19746. * -3 - party name is exist
  19747. * -2 - player is in party already
  19748. * -1 - player is not found
  19749. * 0 - unknown error
  19750. * 1 - success, will return party id $@party_create_id
  19751. */
  19752. BUILDIN_FUNC(party_create)
  19753. {
  19754. char party_name[NAME_LENGTH];
  19755. int item1 = 0, item2 = 0;
  19756. TBL_PC *sd = NULL;
  19757. if (!script_charid2sd(3, sd)) {
  19758. script_pushint(st,-1);
  19759. return SCRIPT_CMD_FAILURE;
  19760. }
  19761. if( sd->status.party_id ) {
  19762. script_pushint(st,-2);
  19763. return SCRIPT_CMD_FAILURE;
  19764. }
  19765. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19766. trim(party_name);
  19767. if( party_searchname(party_name) ) {
  19768. script_pushint(st,-3);
  19769. return SCRIPT_CMD_FAILURE;
  19770. }
  19771. if( script_getnum(st,4) )
  19772. item1 = 1;
  19773. if( script_getnum(st,5) )
  19774. item2 = 1;
  19775. party_create_byscript = 1;
  19776. script_pushint(st,party_create(sd,party_name,item1,item2));
  19777. return SCRIPT_CMD_SUCCESS;
  19778. }
  19779. /** Adds player to specified party
  19780. * party_addmember <party id>,<char id>;
  19781. * @param party_id: The party that will be entered by player
  19782. * @param char_id: Char id of player that will be joined to the party
  19783. * @return val: Result value
  19784. * -5 - another character of the same account is in the party
  19785. * -4 - party is full
  19786. * -3 - party is not found
  19787. * -2 - player is in party already
  19788. * -1 - player is not found
  19789. * 0 - unknown error
  19790. * 1 - success
  19791. */
  19792. BUILDIN_FUNC(party_addmember)
  19793. {
  19794. int party_id = script_getnum(st,2);
  19795. TBL_PC *sd;
  19796. struct party_data *party;
  19797. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19798. script_pushint(st,-1);
  19799. return SCRIPT_CMD_FAILURE;
  19800. }
  19801. if( sd->status.party_id ) {
  19802. script_pushint(st,-2);
  19803. return SCRIPT_CMD_FAILURE;
  19804. }
  19805. if( !(party = party_search(party_id)) ) {
  19806. script_pushint(st,-3);
  19807. return SCRIPT_CMD_FAILURE;
  19808. }
  19809. if (battle_config.block_account_in_same_party) {
  19810. int i;
  19811. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19812. if (i < MAX_PARTY) {
  19813. script_pushint(st,-5);
  19814. return SCRIPT_CMD_FAILURE;
  19815. }
  19816. }
  19817. if( party->party.count >= MAX_PARTY ) {
  19818. script_pushint(st,-4);
  19819. return SCRIPT_CMD_FAILURE;
  19820. }
  19821. sd->party_invite = party_id;
  19822. script_pushint(st,party_add_member(party_id,sd));
  19823. return SCRIPT_CMD_SUCCESS;
  19824. }
  19825. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19826. * party_delmember {<char id>,<party_id>};
  19827. * @param: char_id
  19828. * @param: party_id
  19829. * @return val: Result value
  19830. * -3 - player is not in party
  19831. * -2 - party is not found
  19832. * -1 - player is not found
  19833. * 0 - unknown error
  19834. * 1 - success
  19835. */
  19836. BUILDIN_FUNC(party_delmember)
  19837. {
  19838. TBL_PC *sd = NULL;
  19839. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19840. script_pushint(st,-1);
  19841. return SCRIPT_CMD_FAILURE;
  19842. }
  19843. if( sd || script_charid2sd(2,sd) )
  19844. script_pushint(st,party_removemember2(sd,0,0));
  19845. else
  19846. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  19847. return SCRIPT_CMD_SUCCESS;
  19848. }
  19849. /** Changes party leader of specified party (even the leader is offline)
  19850. * party_changeleader <party id>,<char id>;
  19851. * @param party_id: ID of party
  19852. * @param char_id: Char ID of new leader
  19853. * @return val: Result value
  19854. * -4 - player is party leader already
  19855. * -3 - player is not in this party
  19856. * -2 - player is not found
  19857. * -1 - party is not found
  19858. * 0 - unknown error
  19859. * 1 - success */
  19860. BUILDIN_FUNC(party_changeleader)
  19861. {
  19862. int i, party_id = script_getnum(st,2);
  19863. TBL_PC *sd = NULL;
  19864. TBL_PC *tsd = NULL;
  19865. struct party_data *party = NULL;
  19866. if( !(party = party_search(party_id)) ) {
  19867. script_pushint(st,-1);
  19868. return SCRIPT_CMD_FAILURE;
  19869. }
  19870. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19871. script_pushint(st,-2);
  19872. return SCRIPT_CMD_FAILURE;
  19873. }
  19874. if( tsd->status.party_id != party_id ) {
  19875. script_pushint(st,-3);
  19876. return SCRIPT_CMD_FAILURE;
  19877. }
  19878. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19879. if( i >= MAX_PARTY ) { //this is should impossible!
  19880. script_pushint(st,0);
  19881. return SCRIPT_CMD_FAILURE;
  19882. }
  19883. if( party->data[i].sd == tsd ) {
  19884. script_pushint(st,-4);
  19885. return SCRIPT_CMD_FAILURE;
  19886. }
  19887. script_pushint(st,party_changeleader(sd,tsd,party));
  19888. return SCRIPT_CMD_SUCCESS;
  19889. }
  19890. /** Changes party option
  19891. * party_changeoption <party id>,<option>,<flag>;
  19892. * @param party_id: ID of party that will be changed
  19893. * @param option: Type of option
  19894. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  19895. */
  19896. BUILDIN_FUNC(party_changeoption)
  19897. {
  19898. struct party_data *party;
  19899. if( !(party = party_search(script_getnum(st,2))) ) {
  19900. script_pushint(st,-1);
  19901. return SCRIPT_CMD_FAILURE;
  19902. }
  19903. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  19904. return SCRIPT_CMD_SUCCESS;
  19905. }
  19906. /** Destroys party with party id.
  19907. * party_destroy <party id>;
  19908. * @param party_id: ID of party that will be destroyed
  19909. */
  19910. BUILDIN_FUNC(party_destroy)
  19911. {
  19912. int i;
  19913. struct party_data *party;
  19914. if( !(party = party_search(script_getnum(st,2))) ) {
  19915. script_pushint(st,0);
  19916. return SCRIPT_CMD_FAILURE;
  19917. }
  19918. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19919. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  19920. int j;
  19921. for( j = 0; j < MAX_PARTY; j++ ) {
  19922. TBL_PC *sd = party->data[j].sd;
  19923. if(sd)
  19924. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  19925. else if( party->party.member[j].char_id )
  19926. 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);
  19927. }
  19928. party_broken(party->party.party_id);
  19929. script_pushint(st,1);
  19930. }
  19931. else //leader leave = party broken
  19932. script_pushint(st,party_leave(party->data[i].sd));
  19933. return SCRIPT_CMD_SUCCESS;
  19934. }
  19935. /** Returns various information about a player's VIP status. Need to enable VIP system
  19936. * vip_status <type>,{"<character name>"};
  19937. * @param type: Info type, see enum vip_status_type
  19938. * @param name: Character name (Optional)
  19939. */
  19940. BUILDIN_FUNC(vip_status) {
  19941. #ifdef VIP_ENABLE
  19942. TBL_PC *sd;
  19943. int type;
  19944. if( !script_nick2sd(3,sd) )
  19945. return SCRIPT_CMD_FAILURE;
  19946. type = script_getnum(st, 2);
  19947. switch(type) {
  19948. case VIP_STATUS_ACTIVE: // Get VIP status.
  19949. script_pushint(st, pc_isvip(sd));
  19950. break;
  19951. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  19952. if (pc_isvip(sd)) {
  19953. script_pushint(st, sd->vip.time);
  19954. } else
  19955. script_pushint(st, 0);
  19956. break;
  19957. case VIP_STATUS_REMAINING: // Get remaining time.
  19958. if (pc_isvip(sd)) {
  19959. script_pushint(st, sd->vip.time - time(NULL));
  19960. } else
  19961. script_pushint(st, 0);
  19962. break;
  19963. default:
  19964. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  19965. return SCRIPT_CMD_FAILURE;
  19966. }
  19967. #else
  19968. script_pushint(st, 0);
  19969. #endif
  19970. return SCRIPT_CMD_SUCCESS;
  19971. }
  19972. /** Adds or removes VIP time in minutes. Need to enable VIP system
  19973. * vip_time <time in mn>,{"<character name>"};
  19974. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  19975. * @param name: Character name (optional)
  19976. */
  19977. BUILDIN_FUNC(vip_time) {
  19978. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  19979. TBL_PC *sd;
  19980. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  19981. if( !script_nick2sd(3,sd) )
  19982. return SCRIPT_CMD_FAILURE;
  19983. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  19984. #endif
  19985. return SCRIPT_CMD_SUCCESS;
  19986. }
  19987. /**
  19988. * Turns a player into a monster and optionally can grant a SC attribute effect.
  19989. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19990. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19991. * @param monster: Monster ID or name
  19992. * @param duration: Transform duration in millisecond (ms)
  19993. * @param sc_type: Type of SC that will be affected during the transformation
  19994. * @param val1: Value for SC
  19995. * @param val2: Value for SC
  19996. * @param val3: Value for SC
  19997. * @param val4: Value for SC
  19998. * @author: malufett
  19999. */
  20000. BUILDIN_FUNC(montransform) {
  20001. TBL_PC *sd;
  20002. enum sc_type type;
  20003. int tick, mob_id, val1, val2, val3, val4;
  20004. val1 = val2 = val3 = val4 = 0;
  20005. if( !script_rid2sd(sd) )
  20006. return SCRIPT_CMD_FAILURE;
  20007. if( script_isstring(st, 2) )
  20008. mob_id = mobdb_searchname(script_getstr(st, 2));
  20009. else
  20010. mob_id = mobdb_checkid(script_getnum(st, 2));
  20011. tick = script_getnum(st, 3);
  20012. if (script_hasdata(st, 4))
  20013. type = (sc_type)script_getnum(st, 4);
  20014. else
  20015. type = SC_NONE;
  20016. if (mob_id == 0) {
  20017. if( script_isstring(st, 2) )
  20018. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  20019. else
  20020. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  20021. return SCRIPT_CMD_FAILURE;
  20022. }
  20023. if (mob_id == MOBID_EMPERIUM) {
  20024. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  20025. return SCRIPT_CMD_FAILURE;
  20026. }
  20027. if (!(type >= SC_NONE && type < SC_MAX)) {
  20028. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  20029. return SCRIPT_CMD_FAILURE;
  20030. }
  20031. if (script_hasdata(st, 5))
  20032. val1 = script_getnum(st, 5);
  20033. if (script_hasdata(st, 6))
  20034. val2 = script_getnum(st, 6);
  20035. if (script_hasdata(st, 7))
  20036. val3 = script_getnum(st, 7);
  20037. if (script_hasdata(st, 8))
  20038. val4 = script_getnum(st, 8);
  20039. if (tick != 0) {
  20040. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  20041. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  20042. return SCRIPT_CMD_FAILURE;
  20043. }
  20044. if (sd->disguise){
  20045. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  20046. return SCRIPT_CMD_FAILURE;
  20047. }
  20048. if (!strcmp(script_getfuncname(st), "active_transform")) {
  20049. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM); // Clear previous
  20050. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  20051. } else {
  20052. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM); // Clear previous
  20053. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  20054. }
  20055. if (type != SC_NONE)
  20056. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  20057. }
  20058. return SCRIPT_CMD_SUCCESS;
  20059. }
  20060. /**
  20061. * Attach script to player for certain duration
  20062. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  20063. * @param "script code"
  20064. * @param duration
  20065. * @param flag
  20066. * @param icon
  20067. * @param char_id
  20068. * @author [Cydh]
  20069. **/
  20070. BUILDIN_FUNC(bonus_script) {
  20071. uint16 flag = 0;
  20072. int16 icon = EFST_BLANK;
  20073. uint32 dur;
  20074. uint8 type = 0;
  20075. TBL_PC* sd;
  20076. const char *script_str = NULL;
  20077. struct s_bonus_script_entry *entry = NULL;
  20078. if ( !script_charid2sd(7,sd) )
  20079. return SCRIPT_CMD_FAILURE;
  20080. script_str = script_getstr(st,2);
  20081. dur = 1000 * abs(script_getnum(st,3));
  20082. FETCH(4, flag);
  20083. FETCH(5, type);
  20084. FETCH(6, icon);
  20085. // No Script string, No Duration!
  20086. if (script_str[0] == '\0' || !dur) {
  20087. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  20088. return SCRIPT_CMD_FAILURE;
  20089. }
  20090. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  20091. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  20092. return SCRIPT_CMD_FAILURE;
  20093. }
  20094. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  20095. icon = EFST_BLANK;
  20096. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_type)icon, flag, type))) {
  20097. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  20098. status_calc_pc(sd,SCO_NONE);
  20099. }
  20100. return SCRIPT_CMD_SUCCESS;
  20101. }
  20102. /**
  20103. * Removes all bonus script from player
  20104. * bonus_script_clear {<flag>,{<char_id>}};
  20105. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  20106. * @param char_id Clear script from this character
  20107. * @author [Cydh]
  20108. */
  20109. BUILDIN_FUNC(bonus_script_clear) {
  20110. TBL_PC* sd;
  20111. bool flag = false;
  20112. if (!script_charid2sd(3,sd))
  20113. return SCRIPT_CMD_FAILURE;
  20114. if (script_hasdata(st,2))
  20115. flag = script_getnum(st,2) != 0;
  20116. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  20117. return SCRIPT_CMD_SUCCESS;
  20118. }
  20119. /** Allows player to use atcommand while talking with NPC
  20120. * enable_command;
  20121. * @author [Cydh], [Kichi]
  20122. */
  20123. BUILDIN_FUNC(enable_command) {
  20124. TBL_PC* sd;
  20125. if (!script_rid2sd(sd))
  20126. return SCRIPT_CMD_FAILURE;
  20127. sd->state.disable_atcommand_on_npc = 0;
  20128. return SCRIPT_CMD_SUCCESS;
  20129. }
  20130. /** Prevents player to use atcommand while talking with NPC
  20131. * disable_command;
  20132. * @author [Cydh], [Kichi]
  20133. */
  20134. BUILDIN_FUNC(disable_command) {
  20135. TBL_PC* sd;
  20136. if (!script_rid2sd(sd))
  20137. return SCRIPT_CMD_FAILURE;
  20138. sd->state.disable_atcommand_on_npc = 1;
  20139. return SCRIPT_CMD_SUCCESS;
  20140. }
  20141. /** Get the information of the members of a guild by type.
  20142. * getguildmember <guild_id>{,<type>};
  20143. * @param guild_id: ID of guild
  20144. * @param type: Type of option (optional)
  20145. */
  20146. BUILDIN_FUNC(getguildmember)
  20147. {
  20148. struct guild *g = NULL;
  20149. uint8 j = 0;
  20150. g = guild_search(script_getnum(st,2));
  20151. if (g) {
  20152. uint8 i, type = 0;
  20153. struct script_data *data = NULL;
  20154. char *varname = NULL;
  20155. if (script_hasdata(st,3))
  20156. type = script_getnum(st,3);
  20157. if (script_hasdata(st,4)) {
  20158. data = script_getdata(st, 4);
  20159. if (!data_isreference(data)) {
  20160. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  20161. return SCRIPT_CMD_FAILURE;
  20162. }
  20163. varname = reference_getname(data);
  20164. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  20165. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  20166. return SCRIPT_CMD_FAILURE;
  20167. }
  20168. if (not_server_variable(*varname)) {
  20169. struct map_session_data *sd;
  20170. if (!script_rid2sd(sd)) {
  20171. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  20172. return SCRIPT_CMD_FAILURE;
  20173. }
  20174. }
  20175. }
  20176. for (i = 0; i < MAX_GUILD; i++) {
  20177. if (g->member[i].account_id) {
  20178. switch (type) {
  20179. case 2:
  20180. if (data)
  20181. setd_sub_num( st, NULL, varname, j, g->member[i].account_id, data->ref );
  20182. else
  20183. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  20184. break;
  20185. case 1:
  20186. if (data)
  20187. setd_sub_num( st, NULL, varname, j, g->member[i].char_id, data->ref );
  20188. else
  20189. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  20190. break;
  20191. default:
  20192. if (data)
  20193. setd_sub_str( st, NULL, varname, j, g->member[i].name, data->ref );
  20194. else
  20195. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  20196. break;
  20197. }
  20198. j++;
  20199. }
  20200. }
  20201. }
  20202. mapreg_setreg(add_str("$@guildmembercount"), j);
  20203. script_pushint(st, j);
  20204. return SCRIPT_CMD_SUCCESS;
  20205. }
  20206. /** Adds spirit ball to player for 'duration' in milisecond
  20207. * addspiritball <count>,<duration>{,<char_id>};
  20208. * @param count How many spirit ball will be added
  20209. * @param duration How long spiritball is active until it disappears
  20210. * @param char_id Target player (Optional)
  20211. * @author [Cydh]
  20212. */
  20213. BUILDIN_FUNC(addspiritball) {
  20214. uint8 i, count;
  20215. uint16 duration;
  20216. struct map_session_data *sd = NULL;
  20217. if (script_hasdata(st,4)) {
  20218. if (!script_isstring(st,4))
  20219. sd = map_charid2sd(script_getnum(st,4));
  20220. else
  20221. sd = map_nick2sd(script_getstr(st,4),false);
  20222. }
  20223. else
  20224. script_rid2sd(sd);
  20225. if (!sd)
  20226. return SCRIPT_CMD_FAILURE;
  20227. count = script_getnum(st,2);
  20228. if (count == 0)
  20229. return SCRIPT_CMD_SUCCESS;
  20230. duration = script_getnum(st,3);
  20231. for (i = 0; i < count; i++)
  20232. pc_addspiritball(sd,duration,10);
  20233. return SCRIPT_CMD_SUCCESS;
  20234. }
  20235. /** Deletes the spirit ball(s) from player
  20236. * delspiritball <count>{,<char_id>};
  20237. * @param count How many spirit ball will be deleted
  20238. * @param char_id Target player (Optional)
  20239. * @author [Cydh]
  20240. */
  20241. BUILDIN_FUNC(delspiritball) {
  20242. uint8 count;
  20243. struct map_session_data *sd = NULL;
  20244. if (script_hasdata(st,3)) {
  20245. if (!script_isstring(st,3))
  20246. sd = map_charid2sd(script_getnum(st,3));
  20247. else
  20248. sd = map_nick2sd(script_getstr(st,3),false);
  20249. }
  20250. else
  20251. script_rid2sd(sd);
  20252. if (!sd)
  20253. return SCRIPT_CMD_FAILURE;
  20254. count = script_getnum(st,2);
  20255. if (count == 0)
  20256. count = 1;
  20257. pc_delspiritball(sd,count,0);
  20258. return SCRIPT_CMD_SUCCESS;
  20259. }
  20260. /** Counts the spirit ball that player has
  20261. * countspiritball {<char_id>};
  20262. * @param char_id Target player (Optional)
  20263. * @author [Cydh]
  20264. */
  20265. BUILDIN_FUNC(countspiritball) {
  20266. struct map_session_data *sd;
  20267. if (script_hasdata(st,2)) {
  20268. if (!script_isstring(st,2))
  20269. sd = map_charid2sd(script_getnum(st,2));
  20270. else
  20271. sd = map_nick2sd(script_getstr(st,2),false);
  20272. }
  20273. else
  20274. script_rid2sd(sd);
  20275. if (!sd)
  20276. return SCRIPT_CMD_FAILURE;
  20277. script_pushint(st,sd->spiritball);
  20278. return SCRIPT_CMD_SUCCESS;
  20279. }
  20280. /** Merges separated stackable items because of guid
  20281. * mergeitem {<char_id>};
  20282. * @param char_id Char ID (Optional)
  20283. * @author [Cydh]
  20284. */
  20285. BUILDIN_FUNC(mergeitem) {
  20286. struct map_session_data *sd;
  20287. if (!script_charid2sd(2, sd))
  20288. return SCRIPT_CMD_FAILURE;
  20289. clif_merge_item_open(sd);
  20290. return SCRIPT_CMD_SUCCESS;
  20291. }
  20292. /** Merges separated stackable items because of guid (Unofficial)
  20293. * mergeitem2 {<item_id>,{<char_id>}};
  20294. * mergeitem2 {"<item name>",{<char_id>}};
  20295. * @param item Item ID/Name for merging specific item (Optional)
  20296. * @author [Cydh]
  20297. */
  20298. BUILDIN_FUNC(mergeitem2) {
  20299. struct map_session_data *sd;
  20300. struct item *items = NULL;
  20301. uint16 i, count = 0;
  20302. t_itemid nameid = 0;
  20303. if (!script_charid2sd(3, sd))
  20304. return SCRIPT_CMD_FAILURE;
  20305. if (script_hasdata(st, 2)) {
  20306. if (script_isstring(st, 2)) {// "<item name>"
  20307. const char *name = script_getstr(st, 2);
  20308. std::shared_ptr<item_data> id = item_db.searchname( name );
  20309. if( id == nullptr ){
  20310. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  20311. script_pushint(st, count);
  20312. return SCRIPT_CMD_FAILURE;
  20313. }
  20314. nameid = id->nameid;
  20315. } else {// <item id>
  20316. nameid = script_getnum(st, 2);
  20317. if (!item_db.exists(nameid)) {
  20318. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  20319. script_pushint(st, count);
  20320. return SCRIPT_CMD_FAILURE;
  20321. }
  20322. }
  20323. }
  20324. for (i = 0; i < MAX_INVENTORY; i++) {
  20325. struct item *it = &sd->inventory.u.items_inventory[i];
  20326. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  20327. continue;
  20328. if ((!nameid || (nameid == it->nameid))) {
  20329. uint16 k;
  20330. if (!count) {
  20331. CREATE(items, struct item, 1);
  20332. memcpy(&items[count++], it, sizeof(struct item));
  20333. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20334. continue;
  20335. }
  20336. for (k = 0; k < count; k++) {
  20337. // Find Match
  20338. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  20339. items[k].amount += it->amount;
  20340. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20341. break;
  20342. }
  20343. }
  20344. if (k >= count) {
  20345. // New entry
  20346. RECREATE(items, struct item, count+1);
  20347. memcpy(&items[count++], it, sizeof(struct item));
  20348. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20349. }
  20350. }
  20351. }
  20352. if (!items) // Nothing todo here
  20353. return SCRIPT_CMD_SUCCESS;
  20354. // Retrieve the items
  20355. for (i = 0; i < count; i++) {
  20356. uint8 flag = 0;
  20357. if (!&items[i])
  20358. continue;
  20359. items[i].id = 0;
  20360. items[i].unique_id = 0;
  20361. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  20362. clif_additem(sd, i, items[i].amount, flag);
  20363. }
  20364. aFree(items);
  20365. script_pushint(st, count);
  20366. return SCRIPT_CMD_SUCCESS;
  20367. }
  20368. /**
  20369. * Update an entry from NPC shop.
  20370. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  20371. **/
  20372. BUILDIN_FUNC(npcshopupdate) {
  20373. const char* npcname = script_getstr(st, 2);
  20374. struct npc_data* nd = npc_name2id(npcname);
  20375. t_itemid nameid = script_getnum(st, 3);
  20376. int price = script_getnum(st, 4);
  20377. #if PACKETVER >= 20131223
  20378. int32 stock = script_hasdata(st,5) ? script_getnum(st,5) : -1;
  20379. if( stock < -1 ){
  20380. ShowError( "buildin_npcshopupdate: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  20381. script_pushint( st, 0 );
  20382. return SCRIPT_CMD_FAILURE;
  20383. }
  20384. #endif
  20385. int i;
  20386. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  20387. script_pushint(st,0);
  20388. return SCRIPT_CMD_FAILURE;
  20389. }
  20390. if (!nd->u.shop.count) {
  20391. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  20392. script_pushint(st,0);
  20393. return SCRIPT_CMD_FAILURE;
  20394. }
  20395. for (i = 0; i < nd->u.shop.count; i++) {
  20396. if (nd->u.shop.shop_item[i].nameid == nameid) {
  20397. if (price != 0)
  20398. nd->u.shop.shop_item[i].value = price;
  20399. #if PACKETVER >= 20131223
  20400. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  20401. nd->u.shop.shop_item[i].qty = stock;
  20402. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  20403. }
  20404. #endif
  20405. }
  20406. }
  20407. script_pushint(st,1);
  20408. return SCRIPT_CMD_SUCCESS;
  20409. }
  20410. // Clan System
  20411. BUILDIN_FUNC(clan_join){
  20412. struct map_session_data *sd;
  20413. int clan_id = script_getnum(st,2);
  20414. if( !script_charid2sd( 3, sd ) ){
  20415. script_pushint(st, false);
  20416. return SCRIPT_CMD_FAILURE;
  20417. }
  20418. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  20419. script_pushint(st, true);
  20420. else
  20421. script_pushint(st, false);
  20422. return SCRIPT_CMD_SUCCESS;
  20423. }
  20424. BUILDIN_FUNC(clan_leave){
  20425. struct map_session_data *sd;
  20426. if( !script_charid2sd( 2, sd ) ){
  20427. script_pushint(st, false);
  20428. return SCRIPT_CMD_FAILURE;
  20429. }
  20430. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  20431. script_pushint(st, true);
  20432. else
  20433. script_pushint(st, false);
  20434. return SCRIPT_CMD_SUCCESS;
  20435. }
  20436. /**
  20437. * Get rid from running script.
  20438. * getattachedrid();
  20439. **/
  20440. BUILDIN_FUNC(getattachedrid) {
  20441. script_pushint(st, st->rid);
  20442. return SCRIPT_CMD_SUCCESS;
  20443. }
  20444. /**
  20445. * Get variable from a Player
  20446. * getvar <variable>,<char_id>;
  20447. */
  20448. BUILDIN_FUNC(getvar) {
  20449. int char_id = script_getnum(st, 3);
  20450. struct map_session_data *sd = map_charid2sd(char_id);
  20451. struct script_data *data = NULL;
  20452. const char *name = NULL;
  20453. if (!sd) {
  20454. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  20455. return SCRIPT_CMD_FAILURE;
  20456. }
  20457. data = script_getdata(st, 2);
  20458. if (!data_isreference(data)) {
  20459. ShowError("buildin_getvar: Not a variable\n");
  20460. script_reportdata(data);
  20461. script_pushnil(st);
  20462. st->state = END;
  20463. return SCRIPT_CMD_FAILURE;
  20464. }
  20465. name = reference_getname(data);
  20466. if (reference_toparam(data)) {
  20467. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  20468. script_reportdata(data);
  20469. script_pushnil(st);
  20470. st->state = END;
  20471. return SCRIPT_CMD_FAILURE;
  20472. }
  20473. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  20474. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  20475. script_reportdata(data);
  20476. script_pushnil(st);
  20477. st->state = END;
  20478. return SCRIPT_CMD_FAILURE;
  20479. }
  20480. get_val_(st, data, sd);
  20481. if (data_isint(data))
  20482. script_pushint(st, conv_num_(st, data, sd));
  20483. else
  20484. script_pushstrcopy(st, conv_str_(st, data, sd));
  20485. return SCRIPT_CMD_SUCCESS;
  20486. }
  20487. /**
  20488. * Display script message
  20489. * showscript "<message>"{,<GID>,<flag>};
  20490. * @param flag: Specify target
  20491. * AREA - Message is sent to players in the vicinity of the source (default).
  20492. * SELF - Message is sent only to player attached.
  20493. **/
  20494. BUILDIN_FUNC(showscript) {
  20495. struct block_list *bl = NULL;
  20496. const char *msg = script_getstr(st,2);
  20497. int id = 0;
  20498. send_target target = AREA;
  20499. if (script_hasdata(st,3)) {
  20500. id = script_getnum(st,3);
  20501. bl = map_id2bl(id);
  20502. }
  20503. else {
  20504. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  20505. }
  20506. if (!bl) {
  20507. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  20508. script_pushint(st,0);
  20509. return SCRIPT_CMD_FAILURE;
  20510. }
  20511. if (script_hasdata(st, 4)) {
  20512. target = static_cast<send_target>(script_getnum(st, 4));
  20513. if (target == SELF && map_id2sd(bl->id) == NULL) {
  20514. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  20515. return SCRIPT_CMD_FAILURE;
  20516. }
  20517. }
  20518. clif_showscript(bl, msg, target);
  20519. script_pushint(st,1);
  20520. return SCRIPT_CMD_SUCCESS;
  20521. }
  20522. /**
  20523. * Ignore the SECURE_NPCTIMEOUT function.
  20524. * ignoretimeout <flag>{,<char_id>};
  20525. */
  20526. BUILDIN_FUNC(ignoretimeout)
  20527. {
  20528. #ifdef SECURE_NPCTIMEOUT
  20529. struct map_session_data *sd = NULL;
  20530. if (script_hasdata(st,3)) {
  20531. if (!script_isstring(st,3))
  20532. sd = map_charid2sd(script_getnum(st,3));
  20533. else
  20534. sd = map_nick2sd(script_getstr(st,3),false);
  20535. } else
  20536. script_rid2sd(sd);
  20537. if (!sd)
  20538. return SCRIPT_CMD_FAILURE;
  20539. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  20540. #endif
  20541. return SCRIPT_CMD_SUCCESS;
  20542. }
  20543. /**
  20544. * geteleminfo <type>{,<char_id>};
  20545. **/
  20546. BUILDIN_FUNC(geteleminfo) {
  20547. TBL_ELEM *ed = NULL;
  20548. TBL_PC *sd = NULL;
  20549. int type = script_getnum(st,2);
  20550. if (!script_charid2sd(3, sd)) {
  20551. script_pushint(st, 0);
  20552. return SCRIPT_CMD_SUCCESS;
  20553. }
  20554. if (!(ed = sd->ed)) {
  20555. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  20556. script_pushint(st, 0);
  20557. return SCRIPT_CMD_SUCCESS;
  20558. }
  20559. switch (type) {
  20560. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  20561. case 1: script_pushint(st, ed->bl.id); break;
  20562. default:
  20563. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  20564. script_pushint(st, 0);
  20565. return SCRIPT_CMD_FAILURE;
  20566. }
  20567. return SCRIPT_CMD_SUCCESS;
  20568. }
  20569. /**
  20570. * opendressroom(<flag>{,<char_id>});
  20571. */
  20572. BUILDIN_FUNC(opendressroom)
  20573. {
  20574. #if PACKETVER >= 20150513
  20575. int flag = 1;
  20576. TBL_PC* sd;
  20577. if( script_hasdata(st,2) )
  20578. flag = script_getnum(st,2);
  20579. if (!script_charid2sd(3, sd))
  20580. return SCRIPT_CMD_FAILURE;
  20581. clif_dressing_room(sd, flag);
  20582. return SCRIPT_CMD_SUCCESS;
  20583. #else
  20584. return SCRIPT_CMD_FAILURE;
  20585. #endif
  20586. }
  20587. /**
  20588. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  20589. */
  20590. BUILDIN_FUNC(navigateto){
  20591. #if PACKETVER >= 20111010
  20592. TBL_PC* sd;
  20593. const char *mapname;
  20594. uint16 x = 0, y = 0, monster_id = 0;
  20595. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  20596. bool hideWindow = true;
  20597. mapname = script_getstr(st,2);
  20598. if( script_hasdata(st,3) )
  20599. x = script_getnum(st,3);
  20600. if( script_hasdata(st,4) )
  20601. y = script_getnum(st,4);
  20602. if( script_hasdata(st,5) )
  20603. flag = (uint8)script_getnum(st,5);
  20604. if( script_hasdata(st,6) )
  20605. hideWindow = script_getnum(st,6) ? true : false;
  20606. if( script_hasdata(st,7) )
  20607. monster_id = script_getnum(st,7);
  20608. if (!script_charid2sd(8, sd))
  20609. return SCRIPT_CMD_FAILURE;
  20610. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  20611. return SCRIPT_CMD_SUCCESS;
  20612. #else
  20613. return SCRIPT_CMD_FAILURE;
  20614. #endif
  20615. }
  20616. /**
  20617. * adopt("<parent_name>","<baby_name>");
  20618. * adopt(<parent_id>,<baby_id>);
  20619. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  20620. */
  20621. BUILDIN_FUNC(adopt)
  20622. {
  20623. TBL_PC *sd, *b_sd;
  20624. enum adopt_responses response;
  20625. if (script_isstring(st, 2)) {
  20626. const char *name = script_getstr(st, 2);
  20627. sd = map_nick2sd(name,false);
  20628. if (sd == NULL) {
  20629. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  20630. return SCRIPT_CMD_FAILURE;
  20631. }
  20632. } else {
  20633. uint32 char_id = script_getnum(st, 2);
  20634. sd = map_charid2sd(char_id);
  20635. if (sd == NULL) {
  20636. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  20637. return SCRIPT_CMD_FAILURE;
  20638. }
  20639. }
  20640. if (script_isstring(st, 3)) {
  20641. const char *name = script_getstr(st, 3);
  20642. b_sd = map_nick2sd(name,false);
  20643. if (b_sd == NULL) {
  20644. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  20645. return SCRIPT_CMD_FAILURE;
  20646. }
  20647. } else {
  20648. uint32 char_id = script_getnum(st, 3);
  20649. b_sd = map_charid2sd(char_id);
  20650. if (b_sd == NULL) {
  20651. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  20652. return SCRIPT_CMD_FAILURE;
  20653. }
  20654. }
  20655. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  20656. if (response == ADOPT_ALLOWED) {
  20657. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  20658. b_sd->adopt_invite = sd->status.account_id;
  20659. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  20660. script_pushint(st, ADOPT_ALLOWED);
  20661. return SCRIPT_CMD_SUCCESS;
  20662. }
  20663. script_pushint(st, response);
  20664. return SCRIPT_CMD_FAILURE;
  20665. }
  20666. /**
  20667. * Returns the minimum or maximum of all the given parameters for integer variables.
  20668. *
  20669. * min( <value or array>{,value or array 2,...} );
  20670. * minimum( <value or array>{,value or array 2,...} );
  20671. * max( <value or array>{,value or array 2,...} );
  20672. * maximum( <value or array>{,value or array 2,...} );
  20673. */
  20674. BUILDIN_FUNC(minmax){
  20675. char *functionname;
  20676. unsigned int i;
  20677. int64 value;
  20678. // Function pointer for our comparison function (either min or max at the moment)
  20679. int64 (*func)(int64, int64);
  20680. // Get the real function name
  20681. functionname = script_getfuncname(st);
  20682. // Our data should start at offset 2
  20683. i = 2;
  20684. if( !script_hasdata( st, i ) ){
  20685. ShowError( "buildin_%s: no arguments given!\n", functionname );
  20686. st->state = END;
  20687. return SCRIPT_CMD_FAILURE;
  20688. }
  20689. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  20690. value = SCRIPT_INT_MAX;
  20691. func = i64min;
  20692. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  20693. value = SCRIPT_INT_MIN;
  20694. func = i64max;
  20695. }else{
  20696. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  20697. st->state = END;
  20698. return SCRIPT_CMD_FAILURE;
  20699. }
  20700. // As long as we have data on our script stack
  20701. while( script_hasdata(st,i) ){
  20702. struct script_data *data;
  20703. // Get the next piece of data from the script stack
  20704. data = script_getdata( st, i );
  20705. // Is the current parameter a single integer?
  20706. if( data_isint( data ) ){
  20707. value = func( value, script_getnum( st, i ) );
  20708. // Is the current parameter an array variable?
  20709. }else if( data_isreference( data ) ){
  20710. const char *name;
  20711. struct map_session_data* sd;
  20712. unsigned int start, end;
  20713. // Get the name of the variable
  20714. name = reference_getname(data);
  20715. // Check if it's a string variable
  20716. if( is_string_variable( name ) ){
  20717. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20718. script_reportdata( data );
  20719. st->state = END;
  20720. return SCRIPT_CMD_FAILURE;
  20721. }
  20722. // Get the session data, if a player is attached
  20723. sd = st->rid ? map_id2sd(st->rid) : NULL;
  20724. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20725. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20726. return SCRIPT_CMD_FAILURE;
  20727. }
  20728. // Try to find the array's source pointer
  20729. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20730. ShowError( "buildin_%s: not a array!\n", functionname );
  20731. script_reportdata( data );
  20732. st->state = END;
  20733. return SCRIPT_CMD_FAILURE;
  20734. }
  20735. // Get the start and end indices of the array
  20736. start = reference_getindex( data );
  20737. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20738. // Skip empty arrays
  20739. if( start < end ){
  20740. int id;
  20741. // For getting the values we need the id of the array
  20742. id = reference_getid( data );
  20743. // Loop through each value stored in the array
  20744. for( ; start < end; start++ ){
  20745. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20746. }
  20747. }
  20748. else {
  20749. value = func( value, 0 );
  20750. }
  20751. }else{
  20752. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20753. script_reportdata( data );
  20754. st->state = END;
  20755. return SCRIPT_CMD_FAILURE;
  20756. }
  20757. // Continue with the next stack entry
  20758. i++;
  20759. }
  20760. script_pushint( st, value );
  20761. return SCRIPT_CMD_SUCCESS;
  20762. }
  20763. BUILDIN_FUNC(cap_value)
  20764. {
  20765. script_pushint64(st, cap_value(script_getnum64(st, 2), script_getnum64(st, 3), script_getnum64(st, 4)));
  20766. return SCRIPT_CMD_SUCCESS;
  20767. }
  20768. /**
  20769. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20770. * Unlike `getexp` that affected by some adjustments.
  20771. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20772. * @author [Cydh]
  20773. **/
  20774. BUILDIN_FUNC(getexp2) {
  20775. TBL_PC *sd = NULL;
  20776. int64 base_exp = script_getnum64(st, 2);
  20777. int64 job_exp = script_getnum64(st, 3);
  20778. if (!script_charid2sd(4, sd))
  20779. return SCRIPT_CMD_FAILURE;
  20780. if( base_exp == 0 && job_exp == 0 ){
  20781. ShowError( "buildin_getexp2: Called with base and job exp 0.\n" );
  20782. return SCRIPT_CMD_FAILURE;
  20783. }
  20784. if (base_exp > 0)
  20785. pc_gainexp(sd, NULL, base_exp, 0, 2);
  20786. else if (base_exp < 0)
  20787. pc_lostexp(sd, base_exp * -1, 0);
  20788. if (job_exp > 0)
  20789. pc_gainexp(sd, NULL, 0, job_exp, 2);
  20790. else if (job_exp < 0)
  20791. pc_lostexp(sd, 0, job_exp * -1);
  20792. return SCRIPT_CMD_SUCCESS;
  20793. }
  20794. /**
  20795. * Force stat recalculation of sd
  20796. * recalculatestat;
  20797. * @author [secretdataz]
  20798. **/
  20799. BUILDIN_FUNC(recalculatestat) {
  20800. TBL_PC* sd;
  20801. if (!script_rid2sd(sd))
  20802. return SCRIPT_CMD_FAILURE;
  20803. status_calc_pc(sd, SCO_FORCE);
  20804. return SCRIPT_CMD_SUCCESS;
  20805. }
  20806. BUILDIN_FUNC(hateffect){
  20807. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20808. struct map_session_data* sd;
  20809. if( !script_rid2sd(sd) )
  20810. return SCRIPT_CMD_FAILURE;
  20811. int16 effectID = script_getnum(st,2);
  20812. bool enable = script_getnum(st,3) ? true : false;
  20813. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20814. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20815. return SCRIPT_CMD_FAILURE;
  20816. }
  20817. auto it = util::vector_get( sd->hatEffects, effectID );
  20818. if( enable ){
  20819. if( it != sd->hatEffects.end() ){
  20820. return SCRIPT_CMD_SUCCESS;
  20821. }
  20822. sd->hatEffects.push_back( effectID );
  20823. }else{
  20824. if( it == sd->hatEffects.end() ){
  20825. return SCRIPT_CMD_SUCCESS;
  20826. }
  20827. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20828. }
  20829. if( !sd->state.connect_new ){
  20830. clif_hat_effect_single( sd, effectID, enable );
  20831. }
  20832. #endif
  20833. return SCRIPT_CMD_SUCCESS;
  20834. }
  20835. /**
  20836. * Retrieves param of current random option. Intended for random option script only.
  20837. * getrandomoptinfo(<type>);
  20838. * @author [secretdataz]
  20839. **/
  20840. BUILDIN_FUNC(getrandomoptinfo) {
  20841. struct map_session_data *sd;
  20842. int val;
  20843. 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) {
  20844. int param = script_getnum(st, 2);
  20845. switch (param) {
  20846. case ROA_ID:
  20847. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20848. break;
  20849. case ROA_VALUE:
  20850. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20851. break;
  20852. case ROA_PARAM:
  20853. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  20854. break;
  20855. default:
  20856. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  20857. val = 0;
  20858. break;
  20859. }
  20860. script_pushint(st, val);
  20861. }
  20862. else {
  20863. script_pushint(st, 0);
  20864. }
  20865. return SCRIPT_CMD_SUCCESS;
  20866. }
  20867. /**
  20868. * Retrieves a random option on an equipped item.
  20869. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  20870. * @author [secretdataz]
  20871. */
  20872. BUILDIN_FUNC(getequiprandomoption) {
  20873. struct map_session_data *sd;
  20874. int val;
  20875. short i = -1;
  20876. int pos = script_getnum(st, 2);
  20877. int index = script_getnum(st, 3);
  20878. int type = script_getnum(st, 4);
  20879. if (!script_charid2sd(5, sd)) {
  20880. script_pushint(st, -1);
  20881. return SCRIPT_CMD_FAILURE;
  20882. }
  20883. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20884. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  20885. script_pushint(st, -1);
  20886. return SCRIPT_CMD_FAILURE;
  20887. }
  20888. if (equip_index_check(pos))
  20889. i = pc_checkequip(sd, equip_bitmask[pos]);
  20890. if (i < 0) {
  20891. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20892. script_pushint(st, -1);
  20893. return SCRIPT_CMD_FAILURE;
  20894. }
  20895. switch (type) {
  20896. case ROA_ID:
  20897. val = sd->inventory.u.items_inventory[i].option[index].id;
  20898. break;
  20899. case ROA_VALUE:
  20900. val = sd->inventory.u.items_inventory[i].option[index].value;
  20901. break;
  20902. case ROA_PARAM:
  20903. val = sd->inventory.u.items_inventory[i].option[index].param;
  20904. break;
  20905. default:
  20906. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  20907. val = 0;
  20908. break;
  20909. }
  20910. script_pushint(st, val);
  20911. return SCRIPT_CMD_SUCCESS;
  20912. }
  20913. /**
  20914. * Adds a random option on a random option slot on an equipped item and overwrites
  20915. * existing random option in the process.
  20916. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  20917. * @author [secretdataz]
  20918. */
  20919. BUILDIN_FUNC(setrandomoption) {
  20920. struct map_session_data *sd;
  20921. int pos, index, id, value, param, ep;
  20922. int i = -1;
  20923. if (!script_charid2sd(7, sd))
  20924. return SCRIPT_CMD_FAILURE;
  20925. pos = script_getnum(st, 2);
  20926. index = script_getnum(st, 3);
  20927. id = script_getnum(st, 4);
  20928. value = script_getnum(st, 5);
  20929. param = script_getnum(st, 6);
  20930. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  20931. if (opt == nullptr) {
  20932. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  20933. script_pushint(st, 0);
  20934. return SCRIPT_CMD_FAILURE;
  20935. }
  20936. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20937. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  20938. script_pushint(st, 0);
  20939. return SCRIPT_CMD_FAILURE;
  20940. }
  20941. if (equip_index_check(pos))
  20942. i = pc_checkequip(sd, equip_bitmask[pos]);
  20943. if (i >= 0) {
  20944. ep = sd->inventory.u.items_inventory[i].equip;
  20945. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20946. pc_unequipitem(sd, i, 2);
  20947. sd->inventory.u.items_inventory[i].option[index].id = id;
  20948. sd->inventory.u.items_inventory[i].option[index].value = value;
  20949. sd->inventory.u.items_inventory[i].option[index].param = param;
  20950. clif_delitem(sd, i, 1, 3);
  20951. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20952. clif_additem(sd, i, 1, 0);
  20953. pc_equipitem(sd, i, ep);
  20954. script_pushint(st, 1);
  20955. return SCRIPT_CMD_SUCCESS;
  20956. }
  20957. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20958. script_pushint(st, 0);
  20959. return SCRIPT_CMD_FAILURE;
  20960. }
  20961. /// Returns the number of stat points needed to change the specified stat by val.
  20962. /// If val is negative, returns the number of stat points that would be needed to
  20963. /// raise the specified stat from (current value - val) to current value.
  20964. /// *needed_status_point(<type>,<val>{,<char id>});
  20965. /// @author [secretdataz]
  20966. BUILDIN_FUNC(needed_status_point) {
  20967. struct map_session_data *sd;
  20968. int type, val;
  20969. if (!script_charid2sd(4, sd))
  20970. return SCRIPT_CMD_FAILURE;
  20971. type = script_getnum(st, 2);
  20972. val = script_getnum(st, 3);
  20973. script_pushint(st, pc_need_status_point(sd, type, val));
  20974. return SCRIPT_CMD_SUCCESS;
  20975. }
  20976. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  20977. /// If val is negative, returns the number of trait stat points that would be needed to
  20978. /// raise the specified trait stat from (current value - val) to current value.
  20979. /// *needed_trait_point(<type>,<val>{,<char id>});
  20980. BUILDIN_FUNC(needed_trait_point) {
  20981. struct map_session_data *sd;
  20982. if (!script_charid2sd(4, sd))
  20983. return SCRIPT_CMD_FAILURE;
  20984. int type = script_getnum( st, 2 );
  20985. if( type < SP_POW || type > SP_CRT ){
  20986. ShowError( "buildin_needed_trait_point: Unknown trait type %d\n", type );
  20987. return SCRIPT_CMD_FAILURE;
  20988. }
  20989. script_pushint( st, pc_need_trait_point( sd, type, script_getnum( st, 3 ) ) );
  20990. return SCRIPT_CMD_SUCCESS;
  20991. }
  20992. /**
  20993. * jobcanentermap("<mapname>"{,<JobID>});
  20994. * Check if (player with) JobID can enter the map.
  20995. * @param mapname Map name
  20996. * @param JobID Player's JobID (optional)
  20997. **/
  20998. BUILDIN_FUNC(jobcanentermap) {
  20999. const char *mapname = script_getstr(st, 2);
  21000. int mapidx = mapindex_name2id(mapname), m = -1;
  21001. int jobid = 0;
  21002. TBL_PC *sd = NULL;
  21003. if (!mapidx) {// Invalid map
  21004. script_pushint(st, false);
  21005. return SCRIPT_CMD_FAILURE;
  21006. }
  21007. m = map_mapindex2mapid(mapidx);
  21008. if (m == -1) { // Map is on different map server
  21009. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  21010. script_pushint(st, false);
  21011. return SCRIPT_CMD_FAILURE;
  21012. }
  21013. if (script_hasdata(st, 3)) {
  21014. jobid = script_getnum(st, 3);
  21015. } else {
  21016. if (!script_rid2sd(sd)) {
  21017. script_pushint(st, false);
  21018. return SCRIPT_CMD_FAILURE;
  21019. }
  21020. jobid = sd->status.class_;
  21021. }
  21022. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? pc_get_group_level(sd) : 0));
  21023. return SCRIPT_CMD_SUCCESS;
  21024. }
  21025. /**
  21026. * Return alliance information between the two guilds.
  21027. * getguildalliance(<guild id1>,<guild id2>);
  21028. * Return values:
  21029. * -2 - Guild ID1 does not exist
  21030. * -1 - Guild ID2 does not exist
  21031. * 0 - Both guilds have no relation OR guild ID aren't given
  21032. * 1 - Both guilds are allies
  21033. * 2 - Both guilds are antagonists
  21034. */
  21035. BUILDIN_FUNC(getguildalliance)
  21036. {
  21037. struct guild *guild_data1, *guild_data2;
  21038. int guild_id1, guild_id2, i = 0;
  21039. guild_id1 = script_getnum(st,2);
  21040. guild_id2 = script_getnum(st,3);
  21041. if (guild_id1 < 1 || guild_id2 < 1) {
  21042. script_pushint(st, 0);
  21043. return SCRIPT_CMD_SUCCESS;
  21044. }
  21045. if (guild_id1 == guild_id2) {
  21046. script_pushint(st, 1);
  21047. return SCRIPT_CMD_SUCCESS;
  21048. }
  21049. guild_data1 = guild_search(guild_id1);
  21050. guild_data2 = guild_search(guild_id2);
  21051. if (guild_data1 == NULL) {
  21052. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  21053. script_pushint(st, -2);
  21054. return SCRIPT_CMD_FAILURE;
  21055. }
  21056. if (guild_data2 == NULL) {
  21057. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  21058. script_pushint(st, -1);
  21059. return SCRIPT_CMD_FAILURE;
  21060. }
  21061. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  21062. if (i == MAX_GUILDALLIANCE) {
  21063. script_pushint(st, 0);
  21064. return SCRIPT_CMD_SUCCESS;
  21065. }
  21066. if (guild_data1->alliance[i].opposition)
  21067. script_pushint(st, 2);
  21068. else
  21069. script_pushint(st, 1);
  21070. return SCRIPT_CMD_SUCCESS;
  21071. }
  21072. /*
  21073. * openstorage2 <storage_id>,<mode>{,<account_id>}
  21074. * mode @see enum e_storage_mode
  21075. **/
  21076. BUILDIN_FUNC(openstorage2) {
  21077. int stor_id = script_getnum(st, 2);
  21078. TBL_PC *sd = NULL;
  21079. if (!script_accid2sd(4, sd)) {
  21080. script_pushint(st, 0);
  21081. return SCRIPT_CMD_FAILURE;
  21082. }
  21083. if (!storage_exists(stor_id)) {
  21084. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  21085. return SCRIPT_CMD_FAILURE;
  21086. }
  21087. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  21088. return SCRIPT_CMD_SUCCESS;
  21089. }
  21090. /**
  21091. * Create a new channel
  21092. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  21093. * @author [Cydh]
  21094. **/
  21095. BUILDIN_FUNC(channel_create) {
  21096. struct Channel tmp_chan, *ch = NULL;
  21097. const char *chname = script_getstr(st,2), *pass = NULL;
  21098. int i = channel_chk((char*)chname, NULL, 3);
  21099. TBL_PC *sd = NULL;
  21100. if (i != 0) {
  21101. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  21102. script_pushint(st,i);
  21103. return SCRIPT_CMD_FAILURE;
  21104. }
  21105. memset(&tmp_chan, 0, sizeof(struct Channel));
  21106. if (script_hasdata(st,8)) {
  21107. tmp_chan.char_id = script_getnum(st,8);
  21108. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  21109. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  21110. script_pushint(st,-5);
  21111. return SCRIPT_CMD_FAILURE;
  21112. }
  21113. tmp_chan.type = CHAN_TYPE_PRIVATE;
  21114. i = 1;
  21115. }
  21116. else {
  21117. tmp_chan.type = CHAN_TYPE_PUBLIC;
  21118. i = 0;
  21119. }
  21120. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  21121. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  21122. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  21123. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  21124. if (script_hasdata(st,5))
  21125. tmp_chan.opt = script_getnum(st,5);
  21126. else
  21127. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  21128. if (script_hasdata(st,6))
  21129. tmp_chan.msg_delay = script_getnum(st,6);
  21130. else
  21131. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  21132. if (script_hasdata(st,7))
  21133. tmp_chan.color = script_getnum(st,7);
  21134. else
  21135. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  21136. if (!(ch = channel_create(&tmp_chan))) {
  21137. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  21138. script_pushint(st,0);
  21139. return SCRIPT_CMD_FAILURE;
  21140. }
  21141. if (tmp_chan.char_id)
  21142. channel_join(ch, sd);
  21143. script_pushint(st,1);
  21144. return SCRIPT_CMD_SUCCESS;
  21145. }
  21146. /**
  21147. * Set channel option
  21148. * channel_setopt "<chname>",<option>,<value>;
  21149. * @author [Cydh]
  21150. **/
  21151. BUILDIN_FUNC(channel_setopt) {
  21152. struct Channel *ch = NULL;
  21153. const char *chname = script_getstr(st,2);
  21154. int opt = script_getnum(st,3), value = script_getnum(st,4);
  21155. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21156. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  21157. script_pushint(st,0);
  21158. return SCRIPT_CMD_FAILURE;
  21159. }
  21160. switch (opt) {
  21161. case CHAN_OPT_ANNOUNCE_SELF:
  21162. case CHAN_OPT_ANNOUNCE_JOIN:
  21163. case CHAN_OPT_ANNOUNCE_LEAVE:
  21164. case CHAN_OPT_COLOR_OVERRIDE:
  21165. case CHAN_OPT_CAN_CHAT:
  21166. case CHAN_OPT_CAN_LEAVE:
  21167. case CHAN_OPT_AUTOJOIN:
  21168. if (value)
  21169. ch->opt |= opt;
  21170. else
  21171. ch->opt &= ~opt;
  21172. break;
  21173. case CHAN_OPT_MSG_DELAY:
  21174. ch->msg_delay = value;
  21175. break;
  21176. default:
  21177. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  21178. script_pushint(st,0);
  21179. return SCRIPT_CMD_FAILURE;
  21180. }
  21181. script_pushint(st,1);
  21182. return SCRIPT_CMD_SUCCESS;
  21183. }
  21184. /**
  21185. * Get channel options
  21186. * channel_getopt <chname>,<option>;
  21187. */
  21188. BUILDIN_FUNC(channel_getopt) {
  21189. Channel *ch;
  21190. const char *chname = script_getstr(st, 2);
  21191. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21192. ShowError("buildin_channel_getopt: Channel name '%s' is invalid.\n", chname);
  21193. script_pushint(st, false);
  21194. return SCRIPT_CMD_FAILURE;
  21195. }
  21196. int opt = script_getnum(st, 3);
  21197. switch (opt) {
  21198. case CHAN_OPT_ANNOUNCE_SELF:
  21199. case CHAN_OPT_ANNOUNCE_JOIN:
  21200. case CHAN_OPT_ANNOUNCE_LEAVE:
  21201. case CHAN_OPT_COLOR_OVERRIDE:
  21202. case CHAN_OPT_CAN_CHAT:
  21203. case CHAN_OPT_CAN_LEAVE:
  21204. case CHAN_OPT_AUTOJOIN:
  21205. script_pushint(st, (ch->opt & opt) != 0);
  21206. break;
  21207. case CHAN_OPT_MSG_DELAY:
  21208. script_pushint(st, ch->msg_delay);
  21209. break;
  21210. default:
  21211. ShowError("buildin_channel_getopt: Invalid option %d!\n", opt);
  21212. script_pushint(st, false);
  21213. return SCRIPT_CMD_FAILURE;
  21214. }
  21215. return SCRIPT_CMD_SUCCESS;
  21216. }
  21217. /**
  21218. * Set channel color
  21219. * channel_setcolor "<chname>",<color>;
  21220. * @author [Cydh]
  21221. **/
  21222. BUILDIN_FUNC(channel_setcolor) {
  21223. struct Channel *ch = NULL;
  21224. const char *chname = script_getstr(st,2);
  21225. int color = script_getnum(st,3);
  21226. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21227. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  21228. script_pushint(st,0);
  21229. return SCRIPT_CMD_FAILURE;
  21230. }
  21231. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  21232. script_pushint(st,1);
  21233. return SCRIPT_CMD_SUCCESS;
  21234. }
  21235. /**
  21236. * Set channel password
  21237. * channel_setpass "<chname>","<password>";
  21238. * @author [Cydh]
  21239. **/
  21240. BUILDIN_FUNC(channel_setpass) {
  21241. struct Channel *ch = NULL;
  21242. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  21243. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21244. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  21245. script_pushint(st,0);
  21246. return SCRIPT_CMD_FAILURE;
  21247. }
  21248. if (!passwd || !strcmpi(passwd,"null"))
  21249. memset(ch->pass, '\0', sizeof(ch->pass));
  21250. else
  21251. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  21252. script_pushint(st,1);
  21253. return SCRIPT_CMD_SUCCESS;
  21254. }
  21255. /**
  21256. * Set authorized groups
  21257. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  21258. * @author [Cydh]
  21259. **/
  21260. BUILDIN_FUNC(channel_setgroup) {
  21261. struct Channel *ch = NULL;
  21262. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  21263. int i, n = 0, group = 0;
  21264. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21265. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  21266. script_pushint(st,0);
  21267. return SCRIPT_CMD_FAILURE;
  21268. }
  21269. if (funcname[strlen(funcname)-1] == '2') {
  21270. struct script_data *data = script_getdata(st,3);
  21271. const char *varname = reference_getname(data);
  21272. int32 id, idx;
  21273. if (varname[strlen(varname)-1] == '$') {
  21274. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  21275. script_pushint(st,0);
  21276. return SCRIPT_CMD_FAILURE;
  21277. }
  21278. if (not_server_variable(*varname)) {
  21279. struct map_session_data *sd;
  21280. if (!script_rid2sd(sd)) {
  21281. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  21282. return SCRIPT_CMD_FAILURE;
  21283. }
  21284. }
  21285. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  21286. if (n < 1) {
  21287. ShowError("buildin_channel_setgroup: No group id listed.\n");
  21288. script_pushint(st,0);
  21289. return SCRIPT_CMD_FAILURE;
  21290. }
  21291. if (ch->groups)
  21292. aFree(ch->groups);
  21293. ch->groups = NULL;
  21294. ch->group_count = 0;
  21295. id = reference_getid(data);
  21296. idx = reference_getindex(data);
  21297. for (i = 0; i < n; i++) {
  21298. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  21299. if (group == 0) {
  21300. script_pushint(st,1);
  21301. return SCRIPT_CMD_SUCCESS;
  21302. }
  21303. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  21304. ch->groups[ch->group_count-1] = group;
  21305. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21306. }
  21307. }
  21308. else {
  21309. group = script_getnum(st,3);
  21310. n = script_lastdata(st)-1;
  21311. if (n < 1) {
  21312. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  21313. script_pushint(st,0);
  21314. return SCRIPT_CMD_FAILURE;
  21315. }
  21316. if (ch->groups)
  21317. aFree(ch->groups);
  21318. ch->groups = NULL;
  21319. ch->group_count = 0;
  21320. if (group == 0) { // Removed group list
  21321. script_pushint(st,1);
  21322. return SCRIPT_CMD_SUCCESS;
  21323. }
  21324. CREATE(ch->groups, unsigned short, n);
  21325. for (i = 3; i < n+2; i++) {
  21326. ch->groups[ch->group_count++] = script_getnum(st,i);
  21327. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21328. }
  21329. }
  21330. script_pushint(st,n);
  21331. return SCRIPT_CMD_SUCCESS;
  21332. }
  21333. /**
  21334. * Send message on channel
  21335. * channel_chat "<chname>","<message>"{,<color>};
  21336. * @author [Cydh]
  21337. **/
  21338. BUILDIN_FUNC(channel_chat) {
  21339. struct Channel *ch = NULL;
  21340. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  21341. char output[CHAT_SIZE_MAX+1];
  21342. unsigned long color = 0;
  21343. // Check also local channels
  21344. if (chname[0] != '#') { // By Map
  21345. int m = mapindex_name2id(chname);
  21346. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  21347. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  21348. script_pushint(st,0);
  21349. return SCRIPT_CMD_FAILURE;
  21350. }
  21351. if (!(ch = map_getmapdata(m)->channel)) {
  21352. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21353. script_pushint(st,0);
  21354. return SCRIPT_CMD_SUCCESS;
  21355. }
  21356. }
  21357. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  21358. TBL_NPC *nd = map_id2nd(st->oid);
  21359. if (!nd || nd->bl.m == -1) {
  21360. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  21361. script_pushint(st,0);
  21362. return SCRIPT_CMD_FAILURE;
  21363. }
  21364. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  21365. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21366. script_pushint(st,0);
  21367. return SCRIPT_CMD_SUCCESS;
  21368. }
  21369. }
  21370. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21371. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  21372. script_pushint(st,0);
  21373. return SCRIPT_CMD_FAILURE;
  21374. }
  21375. if (!ch) {
  21376. script_pushint(st,0);
  21377. return SCRIPT_CMD_FAILURE;
  21378. }
  21379. color = ch->color;
  21380. FETCH(4, color);
  21381. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  21382. clif_channel_msg(ch,output,color);
  21383. script_pushint(st,1);
  21384. return SCRIPT_CMD_SUCCESS;
  21385. }
  21386. /**
  21387. * Ban player from a channel
  21388. * channel_ban "<chname>",<char_id>;
  21389. * @author [Cydh]
  21390. **/
  21391. BUILDIN_FUNC(channel_ban) {
  21392. struct Channel *ch = NULL;
  21393. const char *chname = script_getstr(st,2);
  21394. unsigned int char_id = script_getnum(st,3);
  21395. TBL_PC *tsd;
  21396. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21397. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  21398. script_pushint(st,0);
  21399. return SCRIPT_CMD_FAILURE;
  21400. }
  21401. if (ch->char_id == char_id) {
  21402. script_pushint(st,0);
  21403. return SCRIPT_CMD_SUCCESS;
  21404. }
  21405. tsd = map_charid2sd(char_id);
  21406. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21407. script_pushint(st,0);
  21408. return SCRIPT_CMD_SUCCESS;
  21409. }
  21410. if (!idb_exists(ch->banned, char_id)) {
  21411. struct chan_banentry *cbe;
  21412. char output[CHAT_SIZE_MAX+1];
  21413. CREATE(cbe, struct chan_banentry, 1);
  21414. cbe->char_id = char_id;
  21415. if (tsd) {
  21416. strcpy(cbe->char_name,tsd->status.name);
  21417. channel_clean(ch,tsd,0);
  21418. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  21419. }
  21420. else
  21421. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  21422. idb_put(ch->banned, char_id, cbe);
  21423. clif_channel_msg(ch,output,ch->color);
  21424. }
  21425. script_pushint(st,1);
  21426. return SCRIPT_CMD_SUCCESS;
  21427. }
  21428. /**
  21429. * Ban player from a channel
  21430. * channel_unban "<chname>",<char_id>;
  21431. * @author [Cydh]
  21432. **/
  21433. BUILDIN_FUNC(channel_unban) {
  21434. struct Channel *ch = NULL;
  21435. const char *chname = script_getstr(st,2);
  21436. unsigned int char_id = script_getnum(st,3);
  21437. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21438. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  21439. script_pushint(st,0);
  21440. return SCRIPT_CMD_FAILURE;
  21441. }
  21442. if (ch->char_id == char_id) {
  21443. script_pushint(st,0);
  21444. return SCRIPT_CMD_SUCCESS;
  21445. }
  21446. if (idb_exists(ch->banned, char_id)) {
  21447. char output[CHAT_SIZE_MAX+1];
  21448. TBL_PC *tsd = map_charid2sd(char_id);
  21449. if (tsd)
  21450. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  21451. else
  21452. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  21453. idb_remove(ch->banned, char_id);
  21454. clif_channel_msg(ch,output,ch->color);
  21455. }
  21456. script_pushint(st,1);
  21457. return SCRIPT_CMD_SUCCESS;
  21458. }
  21459. /**
  21460. * Kick player from a channel
  21461. * channel_kick "<chname>",<char_id>;
  21462. * channel_kick "<chname>","<char_name>";
  21463. * @author [Cydh]
  21464. **/
  21465. BUILDIN_FUNC(channel_kick) {
  21466. struct Channel *ch = NULL;
  21467. const char *chname = script_getstr(st,2);
  21468. TBL_PC *tsd = NULL;
  21469. int res = 1;
  21470. if (script_isstring(st, 3)) {
  21471. const char *name = script_getstr(st, 3);
  21472. if (!(tsd = map_nick2sd(name,false))) {
  21473. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  21474. script_pushint(st,0);
  21475. return SCRIPT_CMD_FAILURE;
  21476. }
  21477. } else {
  21478. int char_id = script_getnum(st, 3);
  21479. if (!(tsd = map_charid2sd(char_id))) {
  21480. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  21481. script_pushint(st,0);
  21482. return SCRIPT_CMD_FAILURE;
  21483. }
  21484. }
  21485. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  21486. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  21487. script_pushint(st,0);
  21488. return SCRIPT_CMD_FAILURE;
  21489. }
  21490. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21491. script_pushint(st,0);
  21492. return SCRIPT_CMD_SUCCESS;
  21493. }
  21494. switch(ch->type){
  21495. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  21496. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  21497. default: res = channel_clean(ch,tsd,0); break;
  21498. }
  21499. if (res == 0) {
  21500. char output[CHAT_SIZE_MAX+1];
  21501. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  21502. clif_channel_msg(ch,output,ch->color);
  21503. }
  21504. script_pushint(st,1);
  21505. return SCRIPT_CMD_SUCCESS;
  21506. }
  21507. /**
  21508. * Delete a channel
  21509. * channel_delete "<chname>";
  21510. * @author [Cydh]
  21511. **/
  21512. BUILDIN_FUNC(channel_delete) {
  21513. struct Channel *ch = NULL;
  21514. const char *chname = script_getstr(st,2);
  21515. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21516. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  21517. script_pushint(st,-1);
  21518. return SCRIPT_CMD_FAILURE;
  21519. }
  21520. script_pushint(st,channel_delete(ch,true));
  21521. return SCRIPT_CMD_SUCCESS;
  21522. }
  21523. BUILDIN_FUNC(unloadnpc) {
  21524. const char *name;
  21525. struct npc_data* nd;
  21526. name = script_getstr(st, 2);
  21527. nd = npc_name2id(name);
  21528. if( nd == NULL ){
  21529. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  21530. return SCRIPT_CMD_FAILURE;
  21531. } else if ( nd->bl.id == st->oid ) {
  21532. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  21533. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  21534. return SCRIPT_CMD_FAILURE;
  21535. }
  21536. npc_unload_duplicates(nd);
  21537. npc_unload(nd, true);
  21538. npc_read_event_script();
  21539. return SCRIPT_CMD_SUCCESS;
  21540. }
  21541. /**
  21542. * Duplicate a NPC.
  21543. * Return the duplicate Unique name on success or empty string on failure.
  21544. * duplicate "<NPC name>","<map>",<x>,<y>{,"<Duplicate NPC name>"{,<sprite>{,<dir>{,<xs>{,<xy>}}}}};
  21545. */
  21546. BUILDIN_FUNC(duplicate)
  21547. {
  21548. const char* old_npcname = script_getstr( st, 2 );
  21549. npc_data* nd = npc_name2id( old_npcname );
  21550. if( nd == nullptr ){
  21551. ShowError( "buildin_duplicate: No such NPC '%s'.\n", old_npcname );
  21552. script_pushstrcopy( st, "" );
  21553. return SCRIPT_CMD_FAILURE;
  21554. }
  21555. const char* mapname = script_getstr( st, 3 );
  21556. int16 mapid = map_mapname2mapid( mapname );
  21557. if( mapid < 0 ){
  21558. ShowError( "buildin_duplicate: map '%s' in not found!\n", mapname );
  21559. script_pushstrcopy( st, "" );
  21560. return SCRIPT_CMD_FAILURE;
  21561. }
  21562. struct map_data* mapdata = map_getmapdata( mapid );
  21563. if( mapdata == nullptr ){
  21564. // Should not happen, but who knows...
  21565. ShowError( "buildin_duplicate: mapdata for '%s' is unavailable!\n", mapname );
  21566. script_pushstrcopy( st, "" );
  21567. return SCRIPT_CMD_FAILURE;
  21568. }
  21569. int16 x = script_getnum( st, 4 );
  21570. if( x < 0 || x >= mapdata->xs ){
  21571. ShowError( "buildin_duplicate: x coordinate %hd is out of bounds for map %s[0-%hd]!\n", x, mapname, mapdata->xs );
  21572. script_pushstrcopy( st, "" );
  21573. return SCRIPT_CMD_FAILURE;
  21574. }
  21575. int16 y = script_getnum( st, 5 );
  21576. if( y < 0 || y >= mapdata->ys ){
  21577. ShowError( "buildin_duplicate: y coordinate %hd is out of bounds for map %s[0-%hd]!\n", y, mapname, mapdata->ys );
  21578. script_pushstrcopy( st, "" );
  21579. return SCRIPT_CMD_FAILURE;
  21580. }
  21581. char name[NPC_NAME_LENGTH + 1];
  21582. if( script_hasdata( st, 6 ) ){
  21583. const char* new_name = script_getstr( st, 6 );
  21584. if( strlen( new_name ) > NPC_NAME_LENGTH ){
  21585. ShowError( "buildin_duplicate: new NPC name \"%s\" is too long!\n", new_name );
  21586. script_pushstrcopy( st, "" );
  21587. return SCRIPT_CMD_FAILURE;
  21588. }
  21589. safestrncpy( name, new_name, sizeof( name ) );
  21590. }else{
  21591. safestrncpy( name, nd->name, sizeof( name ) );
  21592. }
  21593. int class_;
  21594. if( script_hasdata( st, 7 ) ){
  21595. class_ = script_getnum( st, 7 );
  21596. }else{
  21597. class_ = nd->class_;
  21598. }
  21599. uint8 dir;
  21600. if( script_hasdata( st, 8 ) ){
  21601. dir = script_getnum( st, 8 );
  21602. }else{
  21603. dir = nd->ud.dir;
  21604. }
  21605. int16 xs;
  21606. if( script_hasdata( st, 9 ) ){
  21607. xs = script_getnum( st, 9 );
  21608. }else{
  21609. xs = nd->u.scr.xs;
  21610. }
  21611. int16 ys;
  21612. if( script_hasdata( st, 10 ) ){
  21613. ys = script_getnum( st, 10 );
  21614. }else{
  21615. ys = nd->u.scr.ys;
  21616. }
  21617. npc_data* dnd = npc_duplicate_npc( nd, name, mapid, x, y, class_, dir, xs, ys );
  21618. if( dnd == nullptr ){
  21619. script_pushstrcopy( st, "" );
  21620. return SCRIPT_CMD_FAILURE;
  21621. }else{
  21622. script_pushstrcopy( st, dnd->exname );
  21623. return SCRIPT_CMD_SUCCESS;
  21624. }
  21625. }
  21626. /**
  21627. * Add an achievement to the player's log
  21628. * achievementadd(<achievement ID>{,<char ID>});
  21629. */
  21630. BUILDIN_FUNC(achievementadd) {
  21631. struct map_session_data *sd;
  21632. int achievement_id = script_getnum(st, 2);
  21633. if (!script_charid2sd(3, sd)) {
  21634. script_pushint(st, false);
  21635. return SCRIPT_CMD_FAILURE;
  21636. }
  21637. if (achievement_db.exists(achievement_id) == false) {
  21638. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  21639. script_pushint(st, false);
  21640. return SCRIPT_CMD_FAILURE;
  21641. }
  21642. if( !sd->state.pc_loaded ){
  21643. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21644. return SCRIPT_CMD_SUCCESS;
  21645. }
  21646. if (achievement_add(sd, achievement_id))
  21647. script_pushint(st, true);
  21648. else
  21649. script_pushint(st, false);
  21650. return SCRIPT_CMD_SUCCESS;
  21651. }
  21652. /**
  21653. * Removes an achievement on a player.
  21654. * achievementremove(<achievement ID>{,<char ID>});
  21655. * Just for Atemo. ;)
  21656. */
  21657. BUILDIN_FUNC(achievementremove) {
  21658. struct map_session_data *sd;
  21659. int achievement_id = script_getnum(st, 2);
  21660. if (!script_charid2sd(3, sd)) {
  21661. script_pushint(st, false);
  21662. return SCRIPT_CMD_FAILURE;
  21663. }
  21664. if (achievement_db.exists(achievement_id) == false) {
  21665. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  21666. script_pushint(st, false);
  21667. return SCRIPT_CMD_SUCCESS;
  21668. }
  21669. if( !sd->state.pc_loaded ){
  21670. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21671. return SCRIPT_CMD_SUCCESS;
  21672. }
  21673. if (achievement_remove(sd, achievement_id))
  21674. script_pushint(st, true);
  21675. else
  21676. script_pushint(st, false);
  21677. return SCRIPT_CMD_SUCCESS;
  21678. }
  21679. /**
  21680. * Returns achievement progress
  21681. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  21682. */
  21683. BUILDIN_FUNC(achievementinfo) {
  21684. struct map_session_data *sd;
  21685. int achievement_id = script_getnum(st, 2);
  21686. if (!script_charid2sd(4, sd)) {
  21687. script_pushint(st, false);
  21688. return SCRIPT_CMD_FAILURE;
  21689. }
  21690. if (achievement_db.exists(achievement_id) == false) {
  21691. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  21692. script_pushint(st, false);
  21693. return SCRIPT_CMD_FAILURE;
  21694. }
  21695. if( !sd->state.pc_loaded ){
  21696. script_pushint(st, false);
  21697. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21698. return SCRIPT_CMD_SUCCESS;
  21699. }
  21700. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  21701. return SCRIPT_CMD_SUCCESS;
  21702. }
  21703. /**
  21704. * Award an achievement; Ignores requirements
  21705. * achievementcomplete(<achievement ID>{,<char ID>});
  21706. */
  21707. BUILDIN_FUNC(achievementcomplete) {
  21708. struct map_session_data *sd;
  21709. int i, achievement_id = script_getnum(st, 2);
  21710. if (!script_charid2sd(3, sd)) {
  21711. script_pushint(st, false);
  21712. return SCRIPT_CMD_FAILURE;
  21713. }
  21714. if (achievement_db.exists(achievement_id) == false) {
  21715. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  21716. script_pushint(st, false);
  21717. return SCRIPT_CMD_FAILURE;
  21718. }
  21719. if( !sd->state.pc_loaded ){
  21720. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21721. return SCRIPT_CMD_SUCCESS;
  21722. }
  21723. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21724. if (i == sd->achievement_data.count)
  21725. achievement_add(sd, achievement_id);
  21726. achievement_update_achievement(sd, achievement_id, true);
  21727. script_pushint(st, true);
  21728. return SCRIPT_CMD_SUCCESS;
  21729. }
  21730. /**
  21731. * Checks if the achievement exists on player.
  21732. * achievementexists(<achievement ID>{,<char ID>});
  21733. */
  21734. BUILDIN_FUNC(achievementexists) {
  21735. struct map_session_data *sd;
  21736. int i, achievement_id = script_getnum(st, 2);
  21737. if (!script_charid2sd(3, sd)) {
  21738. script_pushint(st, false);
  21739. return SCRIPT_CMD_FAILURE;
  21740. }
  21741. if (achievement_db.exists(achievement_id) == false) {
  21742. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  21743. script_pushint(st, false);
  21744. return SCRIPT_CMD_SUCCESS;
  21745. }
  21746. if( !sd->state.pc_loaded ){
  21747. script_pushint(st, false);
  21748. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21749. return SCRIPT_CMD_SUCCESS;
  21750. }
  21751. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  21752. script_pushint(st, i < sd->achievement_data.count ? true : false);
  21753. return SCRIPT_CMD_SUCCESS;
  21754. }
  21755. /**
  21756. * Updates an achievement's value.
  21757. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  21758. */
  21759. BUILDIN_FUNC(achievementupdate) {
  21760. struct map_session_data *sd;
  21761. int i, achievement_id, type, value;
  21762. achievement_id = script_getnum(st, 2);
  21763. type = script_getnum(st, 3);
  21764. value = script_getnum(st, 4);
  21765. if (!script_charid2sd(5, sd)) {
  21766. script_pushint(st, false);
  21767. return SCRIPT_CMD_FAILURE;
  21768. }
  21769. if (achievement_db.exists(achievement_id) == false) {
  21770. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  21771. script_pushint(st, false);
  21772. return SCRIPT_CMD_FAILURE;
  21773. }
  21774. if( !sd->state.pc_loaded ){
  21775. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21776. return SCRIPT_CMD_SUCCESS;
  21777. }
  21778. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21779. if (i == sd->achievement_data.count)
  21780. achievement_add(sd, achievement_id);
  21781. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21782. if (i == sd->achievement_data.count) {
  21783. script_pushint(st, false);
  21784. return SCRIPT_CMD_SUCCESS;
  21785. }
  21786. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  21787. sd->achievement_data.achievements[i].count[type - 1] = value;
  21788. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  21789. sd->achievement_data.achievements[i].completed = value;
  21790. else if (type == ACHIEVEINFO_GOTREWARD)
  21791. sd->achievement_data.achievements[i].rewarded = value;
  21792. else {
  21793. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  21794. script_pushint(st, false);
  21795. return SCRIPT_CMD_FAILURE;
  21796. }
  21797. achievement_update_achievement(sd, achievement_id, false);
  21798. script_pushint(st, true);
  21799. return SCRIPT_CMD_SUCCESS;
  21800. }
  21801. /**
  21802. * Get an equipment's refine cost
  21803. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  21804. * returns -1 on fail
  21805. */
  21806. BUILDIN_FUNC(getequiprefinecost) {
  21807. int i = -1, slot, type, info;
  21808. map_session_data *sd;
  21809. slot = script_getnum(st, 2);
  21810. type = script_getnum(st, 3);
  21811. info = script_getnum(st, 4);
  21812. if (!script_charid2sd(5, sd)) {
  21813. script_pushint(st, 0);
  21814. return SCRIPT_CMD_FAILURE;
  21815. }
  21816. if (type < 0 || type >= REFINE_COST_MAX) {
  21817. script_pushint(st, -1);
  21818. return SCRIPT_CMD_SUCCESS;
  21819. }
  21820. if (equip_index_check(slot))
  21821. i = pc_checkequip(sd, equip_bitmask[slot]);
  21822. if (i < 0) {
  21823. script_pushint(st, -1);
  21824. return SCRIPT_CMD_SUCCESS;
  21825. }
  21826. if( sd->inventory_data[i] == nullptr ){
  21827. script_pushint( st, -1 );
  21828. return SCRIPT_CMD_SUCCESS;
  21829. }
  21830. std::shared_ptr<s_refine_level_info> level_info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  21831. if( level_info == nullptr ){
  21832. script_pushint( st, -1 );
  21833. return SCRIPT_CMD_SUCCESS;
  21834. }
  21835. std::shared_ptr<s_refine_cost> cost = util::umap_find( level_info->costs, (uint16)type );
  21836. if( cost == nullptr ){
  21837. script_pushint( st, -1 );
  21838. return SCRIPT_CMD_SUCCESS;
  21839. }
  21840. switch( info ){
  21841. case REFINE_MATERIAL_ID:
  21842. script_pushint( st, cost->nameid );
  21843. break;
  21844. case REFINE_ZENY_COST:
  21845. script_pushint( st, cost->zeny );
  21846. break;
  21847. default:
  21848. script_pushint( st, -1 );
  21849. return SCRIPT_CMD_FAILURE;
  21850. }
  21851. return SCRIPT_CMD_SUCCESS;
  21852. }
  21853. /**
  21854. * Round, floor, ceiling a number to arbitrary integer precision.
  21855. * round(<number>,<precision>);
  21856. * ceil(<number>,<precision>);
  21857. * floor(<number>,<precision>);
  21858. */
  21859. BUILDIN_FUNC(round) {
  21860. int num = script_getnum(st, 2);
  21861. int precision = script_getnum(st, 3);
  21862. char* func = script_getfuncname(st);
  21863. if (precision <= 0) {
  21864. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  21865. return SCRIPT_CMD_FAILURE;
  21866. }
  21867. if (strcasecmp(func, "floor") == 0) {
  21868. script_pushint(st, num - (num % precision));
  21869. }
  21870. else if (strcasecmp(func, "ceil") == 0) {
  21871. script_pushint(st, num + precision - (num % precision));
  21872. }
  21873. else {
  21874. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  21875. }
  21876. return SCRIPT_CMD_SUCCESS;
  21877. }
  21878. BUILDIN_FUNC(getequiptradability) {
  21879. int i, num;
  21880. TBL_PC *sd;
  21881. num = script_getnum(st, 2);
  21882. if (!script_charid2sd(3, sd)) {
  21883. return SCRIPT_CMD_FAILURE;
  21884. }
  21885. if (equip_index_check(num))
  21886. i = pc_checkequip(sd, equip_bitmask[num]);
  21887. else{
  21888. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  21889. return SCRIPT_CMD_FAILURE;
  21890. }
  21891. if (i >= 0)
  21892. script_pushint(st, pc_can_trade_item(sd, i));
  21893. else
  21894. script_pushint(st, false);
  21895. return SCRIPT_CMD_SUCCESS;
  21896. }
  21897. static inline bool mail_sub( struct script_state *st, struct script_data *data, struct map_session_data *sd, int i, const char **out_name, unsigned int *start, unsigned int *end, int32 *id ){
  21898. const char *name;
  21899. // Check if it is a variable
  21900. if( !data_isreference(data) ){
  21901. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  21902. return false;
  21903. }
  21904. name = reference_getname(data);
  21905. if( is_string_variable(name) ){
  21906. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  21907. return false;
  21908. }
  21909. // Check if the variable requires a player
  21910. if( not_server_variable(*name) && sd == NULL ){
  21911. // If no player is attached
  21912. if( !script_rid2sd(sd) ){
  21913. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  21914. return false;
  21915. }
  21916. }
  21917. // Try to find the array's source pointer
  21918. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  21919. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  21920. return false;
  21921. }
  21922. // Store the name for later usage
  21923. *out_name = name;
  21924. // Get the start and end indices of the array
  21925. *start = reference_getindex(data);
  21926. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  21927. // For getting the values we need the id of the array
  21928. *id = reference_getid(data);
  21929. return true;
  21930. }
  21931. // 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>}}}}}}}}};
  21932. BUILDIN_FUNC(mail){
  21933. const char *sender, *title, *body, *name;
  21934. struct mail_message msg;
  21935. struct script_data *data;
  21936. struct map_session_data *sd = NULL;
  21937. unsigned int i, j, k, num_items, start, end;
  21938. int32 id;
  21939. memset(&msg, 0, sizeof(struct mail_message));
  21940. msg.dest_id = script_getnum(st,2);
  21941. sender = script_getstr(st, 3);
  21942. if( strlen(sender) > NAME_LENGTH ){
  21943. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  21944. return SCRIPT_CMD_FAILURE;
  21945. }
  21946. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  21947. title = script_getstr(st, 4);
  21948. if( strlen(title) > MAIL_TITLE_LENGTH ){
  21949. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  21950. return SCRIPT_CMD_FAILURE;
  21951. }
  21952. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  21953. body = script_getstr(st, 5);
  21954. if( strlen(body) > MAIL_BODY_LENGTH ){
  21955. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  21956. return SCRIPT_CMD_FAILURE;
  21957. }
  21958. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  21959. if( script_hasdata(st,6) ){
  21960. int zeny = script_getnum(st, 6);
  21961. if( zeny < 0 ){
  21962. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  21963. return SCRIPT_CMD_FAILURE;
  21964. }else if( zeny > MAX_ZENY ){
  21965. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  21966. zeny = MAX_ZENY;
  21967. }
  21968. msg.zeny = zeny;
  21969. }
  21970. // Items
  21971. num_items = 0;
  21972. while( script_hasdata(st,7) ){
  21973. data = script_getdata(st,7);
  21974. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  21975. return SCRIPT_CMD_FAILURE;
  21976. }
  21977. num_items = end - start;
  21978. if( num_items == 0 ){
  21979. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  21980. break;
  21981. }
  21982. if( num_items > MAIL_MAX_ITEM ){
  21983. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  21984. num_items = MAIL_MAX_ITEM;
  21985. }
  21986. for( i = 0; i < num_items && start < end; i++, start++ ){
  21987. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21988. msg.item[i].identify = 1;
  21989. if( !item_db.exists(msg.item[i].nameid) ){
  21990. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  21991. return SCRIPT_CMD_FAILURE;
  21992. }
  21993. }
  21994. // Amounts
  21995. if( !script_hasdata(st,8) ){
  21996. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  21997. return SCRIPT_CMD_FAILURE;
  21998. }
  21999. data = script_getdata(st,8);
  22000. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  22001. return SCRIPT_CMD_FAILURE;
  22002. }
  22003. for( i = 0; i < num_items && start < end; i++, start++ ){
  22004. std::shared_ptr<item_data> itm = item_db.find(msg.item[i].nameid);
  22005. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22006. if( msg.item[i].amount <= 0 ){
  22007. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  22008. return SCRIPT_CMD_FAILURE;
  22009. }else if( itemdb_isstackable2(itm.get()) ){
  22010. uint16 max = itm->stack.amount > 0 ? itm->stack.amount : MAX_AMOUNT;
  22011. if( msg.item[i].amount > max ){
  22012. 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 );
  22013. msg.item[i].amount = max;
  22014. }
  22015. }else{
  22016. if( msg.item[i].amount > 1 ){
  22017. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  22018. msg.item[i].amount = 1;
  22019. }
  22020. }
  22021. }
  22022. // Refine
  22023. if (!script_hasdata(st, 9)) {
  22024. break;
  22025. }
  22026. data = script_getdata(st, 9);
  22027. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  22028. return SCRIPT_CMD_FAILURE;
  22029. }
  22030. for (i = 0; i < num_items && start < end; i++, start++) {
  22031. std::shared_ptr<item_data> itm = item_db.find(msg.item[i].nameid);
  22032. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22033. if (!itm->flag.no_refine && (itm->type == IT_WEAPON || itm->type == IT_ARMOR || itm->type == IT_SHADOWGEAR)) {
  22034. if (msg.item[i].refine > MAX_REFINE)
  22035. msg.item[i].refine = MAX_REFINE;
  22036. }
  22037. else
  22038. msg.item[i].refine = 0;
  22039. if (msg.item[i].refine < 0)
  22040. msg.item[i].refine = 0;
  22041. }
  22042. // Bound
  22043. if (!script_hasdata(st, 10)) {
  22044. break;
  22045. }
  22046. data = script_getdata(st,10);
  22047. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  22048. return SCRIPT_CMD_FAILURE;
  22049. }
  22050. for( i = 0; i < num_items && start < end; i++, start++ ){
  22051. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22052. if( msg.item[i].bound < BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  22053. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  22054. return SCRIPT_CMD_FAILURE;
  22055. }
  22056. }
  22057. // Cards
  22058. if( !script_hasdata(st,11) ){
  22059. break;
  22060. }
  22061. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  22062. data = script_getdata(st,j);
  22063. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22064. return SCRIPT_CMD_FAILURE;
  22065. }
  22066. for( k = 0; k < num_items && start < end; k++, start++ ){
  22067. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22068. if( !item_db.exists(msg.item[k].card[i]) ){
  22069. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  22070. return SCRIPT_CMD_FAILURE;
  22071. }
  22072. }
  22073. }
  22074. // Random Options
  22075. if( !script_hasdata(st,11 + MAX_SLOTS) ){
  22076. break;
  22077. }
  22078. 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++ ){
  22079. // Option IDs
  22080. data = script_getdata(st, j);
  22081. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22082. return SCRIPT_CMD_FAILURE;
  22083. }
  22084. for( k = 0; k < num_items && start < end; k++, start++ ){
  22085. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22086. }
  22087. j++;
  22088. // Option values
  22089. data = script_getdata(st, j);
  22090. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22091. return SCRIPT_CMD_FAILURE;
  22092. }
  22093. for( k = 0; k < num_items && start < end; k++, start++ ){
  22094. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22095. }
  22096. j++;
  22097. // Option parameters
  22098. data = script_getdata(st, j);
  22099. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  22100. return SCRIPT_CMD_FAILURE;
  22101. }
  22102. for( k = 0; k < num_items && start < end; k++, start++ ){
  22103. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  22104. }
  22105. }
  22106. // Break the pseudo scope
  22107. break;
  22108. }
  22109. msg.status = MAIL_NEW;
  22110. msg.type = MAIL_INBOX_NORMAL;
  22111. msg.timestamp = time(NULL);
  22112. intif_Mail_send(0, &msg);
  22113. return SCRIPT_CMD_SUCCESS;
  22114. }
  22115. BUILDIN_FUNC(open_roulette){
  22116. #if PACKETVER < 20141022
  22117. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  22118. return SCRIPT_CMD_FAILURE;
  22119. #else
  22120. struct map_session_data* sd;
  22121. if( !battle_config.feature_roulette ){
  22122. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  22123. return SCRIPT_CMD_FAILURE;
  22124. }
  22125. if( !script_charid2sd( 2, sd ) ){
  22126. return SCRIPT_CMD_FAILURE;
  22127. }
  22128. clif_roulette_open(sd);
  22129. return SCRIPT_CMD_SUCCESS;
  22130. #endif
  22131. }
  22132. /*==========================================
  22133. * identifyall({<type>{,<account_id>}})
  22134. * <type>:
  22135. * true: identify the items and returns the count of unidentified items (default)
  22136. * false: returns the count of unidentified items only
  22137. *------------------------------------------*/
  22138. BUILDIN_FUNC(identifyall) {
  22139. TBL_PC *sd;
  22140. bool identify_item = true;
  22141. if (script_hasdata(st, 2))
  22142. identify_item = script_getnum(st, 2) != 0;
  22143. if( !script_hasdata(st, 3) )
  22144. script_rid2sd(sd);
  22145. else
  22146. sd = map_id2sd( script_getnum(st, 3) );
  22147. if (sd == NULL) {
  22148. script_pushint(st, -1);
  22149. return SCRIPT_CMD_SUCCESS;
  22150. }
  22151. script_pushint(st, pc_identifyall(sd, identify_item));
  22152. return SCRIPT_CMD_SUCCESS;
  22153. }
  22154. BUILDIN_FUNC(is_guild_leader)
  22155. {
  22156. struct map_session_data* sd;
  22157. struct guild* guild_data;
  22158. int guild_id;
  22159. if (!script_rid2sd(sd)) {
  22160. script_pushint(st, false);
  22161. return SCRIPT_CMD_FAILURE;
  22162. }
  22163. if (script_hasdata(st, 2))
  22164. guild_id = script_getnum(st, 2);
  22165. else
  22166. guild_id = sd->status.guild_id;
  22167. guild_data = guild_search(guild_id);
  22168. if (guild_data)
  22169. script_pushint(st, (guild_data->member[0].char_id == sd->status.char_id));
  22170. else
  22171. script_pushint(st, false);
  22172. return SCRIPT_CMD_SUCCESS;
  22173. }
  22174. BUILDIN_FUNC(is_party_leader)
  22175. {
  22176. struct map_session_data* sd;
  22177. struct party_data* p_data;
  22178. int p_id, i = 0;
  22179. if (!script_rid2sd(sd)) {
  22180. script_pushint(st, false);
  22181. return SCRIPT_CMD_FAILURE;
  22182. }
  22183. if (script_hasdata(st, 2))
  22184. p_id = script_getnum(st, 2);
  22185. else
  22186. p_id = sd->status.party_id;
  22187. p_data = party_search(p_id);
  22188. if (p_data) {
  22189. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  22190. if (i < MAX_PARTY){
  22191. script_pushint(st, p_data->party.member[i].leader);
  22192. return SCRIPT_CMD_SUCCESS;
  22193. }
  22194. }
  22195. script_pushint(st, false);
  22196. return SCRIPT_CMD_SUCCESS;
  22197. }
  22198. BUILDIN_FUNC( camerainfo ){
  22199. #if PACKETVER < 20160525
  22200. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  22201. return SCRIPT_CMD_FAILURE;
  22202. #else
  22203. struct map_session_data* sd;
  22204. if( !script_charid2sd( 5, sd ) ){
  22205. return SCRIPT_CMD_FAILURE;
  22206. }
  22207. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  22208. return SCRIPT_CMD_SUCCESS;
  22209. #endif
  22210. }
  22211. // This function is only meant to be used inside of achievement conditions
  22212. BUILDIN_FUNC(achievement_condition){
  22213. // Push what we get from the script
  22214. script_pushint( st, 2 );
  22215. // Otherwise the script is freed afterwards
  22216. st->state = RERUNLINE;
  22217. return SCRIPT_CMD_SUCCESS;
  22218. }
  22219. /// Returns a reference to a variable of the specific instance ID.
  22220. /// Returns 0 if an error occurs.
  22221. ///
  22222. /// getinstancevar(<variable>, <instance ID>) -> <reference>
  22223. BUILDIN_FUNC(getinstancevar)
  22224. {
  22225. struct script_data* data = script_getdata(st, 2);
  22226. if (!data_isreference(data)) {
  22227. ShowError("buildin_getinstancevar: %s is not a variable.\n", script_getstr(st, 2));
  22228. script_reportdata(data);
  22229. script_pushnil(st);
  22230. st->state = END;
  22231. return SCRIPT_CMD_FAILURE;
  22232. }
  22233. const char* name = reference_getname(data);
  22234. if (*name != '\'') {
  22235. ShowError("buildin_getinstancevar: Invalid scope. %s is not an instance variable.\n", name);
  22236. script_reportdata(data);
  22237. script_pushnil(st);
  22238. st->state = END;
  22239. return SCRIPT_CMD_FAILURE;
  22240. }
  22241. int instance_id = script_getnum(st, 3);
  22242. if (instance_id <= 0) {
  22243. ShowError("buildin_getinstancevar: Invalid instance ID %d.\n", instance_id);
  22244. script_pushnil(st);
  22245. st->state = END;
  22246. return SCRIPT_CMD_FAILURE;
  22247. }
  22248. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  22249. if (im->state != INSTANCE_BUSY) {
  22250. ShowError("buildin_getinstancevar: Unknown instance ID %d.\n", instance_id);
  22251. script_pushnil(st);
  22252. st->state = END;
  22253. return SCRIPT_CMD_FAILURE;
  22254. }
  22255. if (!im->regs.vars)
  22256. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  22257. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  22258. return SCRIPT_CMD_SUCCESS;
  22259. }
  22260. /// Sets the value of an instance variable.
  22261. ///
  22262. /// setinstancevar(<variable>,<value>,<instance ID>)
  22263. BUILDIN_FUNC(setinstancevar)
  22264. {
  22265. const char *command = script_getfuncname(st);
  22266. struct script_data* data = script_getdata(st, 2);
  22267. if (!data_isreference(data)) {
  22268. ShowError("buildin_%s: %s is not a variable.\n", command, script_getstr(st, 2));
  22269. script_reportdata(data);
  22270. script_pushnil(st);
  22271. st->state = END;
  22272. return SCRIPT_CMD_FAILURE;
  22273. }
  22274. const char* name = reference_getname(data);
  22275. if (*name != '\'') {
  22276. ShowError("buildin_%s: Invalid scope. %s is not an instance variable.\n", command, name);
  22277. script_reportdata(data);
  22278. script_pushnil(st);
  22279. st->state = END;
  22280. return SCRIPT_CMD_FAILURE;
  22281. }
  22282. int instance_id = script_getnum(st, 4);
  22283. if (instance_id <= 0) {
  22284. ShowError("buildin_%s: Invalid instance ID %d.\n", command, instance_id);
  22285. script_pushnil(st);
  22286. st->state = END;
  22287. return SCRIPT_CMD_FAILURE;
  22288. }
  22289. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  22290. if (im->state != INSTANCE_BUSY) {
  22291. ShowError("buildin_%s: Unknown instance ID %d.\n", command, instance_id);
  22292. script_pushnil(st);
  22293. st->state = END;
  22294. return SCRIPT_CMD_FAILURE;
  22295. }
  22296. script_pushcopy(st, 2);
  22297. struct map_session_data* sd = nullptr;
  22298. if( is_string_variable(name) )
  22299. set_reg_str( st, sd, reference_getuid(data), name, script_getstr( st, 3 ), &im->regs );
  22300. else
  22301. set_reg_num( st, sd, reference_getuid(data), name, script_getnum64( st, 3 ), &im->regs );
  22302. return SCRIPT_CMD_SUCCESS;
  22303. }
  22304. /*
  22305. convertpcinfo(<char_id>,<type>)
  22306. convertpcinfo(<account_id>,<type>)
  22307. convertpcinfo(<player_name>,<type>)
  22308. */
  22309. BUILDIN_FUNC(convertpcinfo) {
  22310. TBL_PC *sd;
  22311. if (script_isstring(st, 2))
  22312. sd = map_nick2sd(script_getstr(st, 2),false);
  22313. else {
  22314. int id = script_getnum(st, 2);
  22315. sd = map_id2sd(id);
  22316. if (!sd)
  22317. sd = map_charid2sd(id);
  22318. }
  22319. int type = script_getnum(st, 3);
  22320. switch (type) {
  22321. case CPC_NAME:
  22322. case CPC_CHAR:
  22323. case CPC_ACCOUNT:
  22324. break;
  22325. default:
  22326. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  22327. script_pushnil(st);
  22328. st->state = END;
  22329. return SCRIPT_CMD_FAILURE;
  22330. }
  22331. if (!sd) {
  22332. if (type == CPC_NAME)
  22333. script_pushstrcopy(st, "");
  22334. else
  22335. script_pushint(st, 0);
  22336. return SCRIPT_CMD_SUCCESS;
  22337. }
  22338. switch (type) {
  22339. case CPC_NAME:
  22340. script_pushstrcopy(st, sd->status.name);
  22341. break;
  22342. case CPC_CHAR:
  22343. script_pushint(st, sd->status.char_id);
  22344. break;
  22345. case CPC_ACCOUNT:
  22346. script_pushint(st, sd->status.account_id);
  22347. break;
  22348. }
  22349. return SCRIPT_CMD_SUCCESS;
  22350. }
  22351. BUILDIN_FUNC(isnpccloaked)
  22352. {
  22353. npc_data *nd;
  22354. if (script_hasdata(st, 2))
  22355. nd = npc_name2id(script_getstr(st, 2));
  22356. else
  22357. nd = map_id2nd(st->oid);
  22358. if (!nd) {
  22359. if (script_hasdata(st, 2))
  22360. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  22361. else
  22362. ShowError("buildin_isnpccloaked: non-existing NPC.\n");
  22363. return SCRIPT_CMD_FAILURE;
  22364. }
  22365. map_session_data* sd;
  22366. if (!script_charid2sd(3, sd))
  22367. return SCRIPT_CMD_FAILURE;
  22368. script_pushint(st, npc_is_cloaked(nd, sd));
  22369. return SCRIPT_CMD_SUCCESS;
  22370. }
  22371. BUILDIN_FUNC(refineui){
  22372. #if PACKETVER < 20161012
  22373. ShowError( "buildin_refineui: This command requires packet version 2016-10-12 or newer.\n" );
  22374. return SCRIPT_CMD_FAILURE;
  22375. #else
  22376. struct map_session_data* sd;
  22377. if( !script_charid2sd(2,sd) ){
  22378. return SCRIPT_CMD_FAILURE;
  22379. }
  22380. if( !battle_config.feature_refineui ){
  22381. ShowError( "buildin_refineui: This command is disabled via configuration.\n" );
  22382. return SCRIPT_CMD_FAILURE;
  22383. }
  22384. if( !sd->state.refineui_open ){
  22385. clif_refineui_open(sd);
  22386. }
  22387. return SCRIPT_CMD_SUCCESS;
  22388. #endif
  22389. }
  22390. BUILDIN_FUNC(getenchantgrade){
  22391. struct map_session_data *sd;
  22392. if (!script_charid2sd(3, sd)) {
  22393. script_pushint(st,-1);
  22394. return SCRIPT_CMD_FAILURE;
  22395. }
  22396. int index, position;
  22397. if (script_hasdata(st, 2))
  22398. position = script_getnum(st, 2);
  22399. else
  22400. position = EQI_COMPOUND_ON;
  22401. if (position == EQI_COMPOUND_ON)
  22402. index = current_equip_item_index;
  22403. else if (equip_index_check(position))
  22404. index = pc_checkequip(sd, equip_bitmask[position]);
  22405. else {
  22406. ShowError( "buildin_getenchantgrade: Unknown equip index '%d'\n", position );
  22407. script_pushint(st,-1);
  22408. return SCRIPT_CMD_FAILURE;
  22409. }
  22410. if (index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0)
  22411. script_pushint(st, -1);
  22412. else
  22413. script_pushint(st, sd->inventory.u.items_inventory[index].enchantgrade);
  22414. return SCRIPT_CMD_SUCCESS;
  22415. }
  22416. BUILDIN_FUNC(naviregisterwarp) {
  22417. #ifdef MAP_GENERATOR
  22418. TBL_NPC* nd;
  22419. int x, y, m;
  22420. const char *warpname, *mapname=NULL;
  22421. struct navi_link link;
  22422. nd = map_id2nd(st->oid);
  22423. if (!nd) {
  22424. return SCRIPT_CMD_SUCCESS;
  22425. }
  22426. warpname = script_getstr(st, 2);
  22427. mapname = script_getstr(st, 3);
  22428. x = script_getnum(st,4);
  22429. y = script_getnum(st,5);
  22430. m = map_mapname2mapid(mapname);
  22431. link.npc = nd;
  22432. link.id = 0;
  22433. link.pos = nd->navi.pos;
  22434. link.warp_dest = {m, x, y};
  22435. link.name = warpname;
  22436. link.hidden = nd->navi.hidden;
  22437. nd->links.push_back(link);
  22438. #endif
  22439. return SCRIPT_CMD_SUCCESS;
  22440. }
  22441. /*==========================================
  22442. * mob_setidleevent( <monster game ID>, "<event label>" )
  22443. *------------------------------------------*/
  22444. BUILDIN_FUNC(mob_setidleevent){
  22445. struct block_list* bl;
  22446. if( !script_rid2bl( 2, bl ) ){
  22447. return SCRIPT_CMD_FAILURE;
  22448. }
  22449. if( bl->type != BL_MOB ){
  22450. ShowError( "buildin_mob_setidleevent: the target GID was not a monster.\n" );
  22451. return SCRIPT_CMD_FAILURE;
  22452. }
  22453. struct mob_data* md = (struct mob_data*)bl;
  22454. if (md == nullptr)
  22455. return SCRIPT_CMD_FAILURE;
  22456. const char* idle_event = script_getstr( st, 3 );
  22457. check_event( st, idle_event );
  22458. safestrncpy( md->idle_event, idle_event, EVENT_NAME_LENGTH );
  22459. return SCRIPT_CMD_SUCCESS;
  22460. }
  22461. BUILDIN_FUNC( openstylist ){
  22462. #if PACKETVER >= 20151104
  22463. struct map_session_data* sd;
  22464. if( !script_charid2sd( 2, sd ) ){
  22465. return SCRIPT_CMD_FAILURE;
  22466. }
  22467. clif_ui_open( *sd, OUT_UI_STYLIST, 0 );
  22468. return SCRIPT_CMD_SUCCESS;
  22469. #else
  22470. ShowError( "buildin_openstylist: This command requires packet version 2015-11-04 or newer.\n" );
  22471. return SCRIPT_CMD_FAILURE;
  22472. #endif
  22473. }
  22474. BUILDIN_FUNC(navihide) {
  22475. #ifdef MAP_GENERATOR
  22476. TBL_NPC *nd;
  22477. nd = map_id2nd(st->oid);
  22478. if (!nd) {
  22479. return SCRIPT_CMD_SUCCESS;
  22480. }
  22481. nd->navi.hidden = true;
  22482. for (auto &link : nd->links) {
  22483. link.hidden = true;
  22484. }
  22485. #endif
  22486. return SCRIPT_CMD_SUCCESS;
  22487. }
  22488. BUILDIN_FUNC(getitempos) {
  22489. struct map_session_data* sd;
  22490. if ( !script_rid2sd(sd) ){
  22491. script_pushint(st, 0);
  22492. return SCRIPT_CMD_FAILURE;
  22493. }
  22494. if( current_equip_item_index == -1 ){
  22495. ShowError( "buildin_getitempos: Invalid usage detected. This command should only be used inside item scripts.\n" );
  22496. script_pushint(st, 0);
  22497. return SCRIPT_CMD_FAILURE;
  22498. }
  22499. script_pushint(st, sd->inventory.u.items_inventory[current_equip_item_index].equip);
  22500. return SCRIPT_CMD_SUCCESS;
  22501. }
  22502. BUILDIN_FUNC( laphine_synthesis ){
  22503. struct map_session_data* sd;
  22504. if( !script_rid2sd( sd ) ){
  22505. return SCRIPT_CMD_FAILURE;
  22506. }
  22507. t_itemid item_id;
  22508. if( script_hasdata( st, 2 ) ){
  22509. if( script_isstring( st, 2 ) ){
  22510. const char* item_name = script_getstr( st, 2 );
  22511. std::shared_ptr<item_data> item = item_db.searchname( item_name );
  22512. if( item == nullptr ){
  22513. ShowError("buildin_laphine_synthesis: Item \"%s\" does not exist.\n", item_name );
  22514. return SCRIPT_CMD_FAILURE;
  22515. }
  22516. item_id = item->nameid;
  22517. }else{
  22518. item_id = script_getnum( st, 2 );
  22519. if( !item_db.exists( item_id ) ){
  22520. ShowError( "buildin_laphine_synthesis: Item ID %u does not exist.\n", item_id );
  22521. return SCRIPT_CMD_FAILURE;
  22522. }
  22523. }
  22524. }else{
  22525. if( sd->itemid == 0 ){
  22526. ShowError( "buildin_laphine_synthesis: Called outside of an item script without item id.\n" );
  22527. return SCRIPT_CMD_FAILURE;
  22528. }
  22529. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22530. ShowError( "buildin_laphine_synthesis: Called from item %u, which is not a consumed delayed.\n", sd->itemid );
  22531. return SCRIPT_CMD_FAILURE;
  22532. }
  22533. item_id = sd->itemid;
  22534. }
  22535. if( sd->state.laphine_synthesis != 0 ){
  22536. 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 );
  22537. return SCRIPT_CMD_FAILURE;
  22538. }
  22539. std::shared_ptr<s_laphine_synthesis> synthesis = laphine_synthesis_db.find( item_id );
  22540. if( synthesis == nullptr ){
  22541. ShowError( "buildin_laphine_synthesis: %u is not a valid Laphine Synthesis item.\n", item_id );
  22542. return SCRIPT_CMD_FAILURE;
  22543. }
  22544. clif_laphine_synthesis_open( sd, synthesis );
  22545. return SCRIPT_CMD_SUCCESS;
  22546. }
  22547. BUILDIN_FUNC( laphine_upgrade ){
  22548. struct map_session_data* sd;
  22549. if( !script_rid2sd( sd ) ){
  22550. return SCRIPT_CMD_FAILURE;
  22551. }
  22552. if( sd->itemid == 0 ){
  22553. ShowError( "buildin_laphine_upgrade: Called outside of an item script without item id.\n" );
  22554. return SCRIPT_CMD_FAILURE;
  22555. }
  22556. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22557. ShowError( "buildin_laphine_upgrade: Called from item %u, which is not a consumed delayed.\n", sd->itemid );
  22558. return SCRIPT_CMD_FAILURE;
  22559. }
  22560. if( sd->state.laphine_upgrade != 0 ){
  22561. 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 );
  22562. return SCRIPT_CMD_FAILURE;
  22563. }
  22564. std::shared_ptr<s_laphine_upgrade> upgrade = laphine_upgrade_db.find( sd->itemid );
  22565. if( upgrade == nullptr ){
  22566. ShowError( "buildin_laphine_upgrade: %u is not a valid Laphine Upgrade item.\n", sd->itemid );
  22567. return SCRIPT_CMD_FAILURE;
  22568. }
  22569. clif_laphine_upgrade_open( sd, upgrade );
  22570. return SCRIPT_CMD_SUCCESS;
  22571. }
  22572. BUILDIN_FUNC(randomoptgroup)
  22573. {
  22574. int id = script_getnum(st,2);
  22575. auto group = random_option_group.find(id);
  22576. if (group == nullptr) {
  22577. ShowError("buildin_randomoptgroup: Invalid random option group id (%d)!\n", id);
  22578. return SCRIPT_CMD_FAILURE;
  22579. }
  22580. struct item item_tmp = {};
  22581. group->apply( item_tmp );
  22582. for ( int i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  22583. setd_sub_num(st, nullptr, ".@opt_id", i, item_tmp.option[i].id, nullptr);
  22584. setd_sub_num(st, nullptr, ".@opt_value", i, item_tmp.option[i].value, nullptr);
  22585. setd_sub_num(st, nullptr, ".@opt_param", i, item_tmp.option[i].param, nullptr);
  22586. }
  22587. return SCRIPT_CMD_SUCCESS;
  22588. }
  22589. BUILDIN_FUNC( open_quest_ui ){
  22590. #if PACKETVER < 20151202
  22591. ShowError( "buildin_open_quest_ui: This command requires PACKETVER 20151202 or newer.\n" );
  22592. return SCRIPT_CMD_FAILURE;
  22593. #else
  22594. struct map_session_data* sd;
  22595. if (!script_charid2sd(3, sd))
  22596. return SCRIPT_CMD_FAILURE;
  22597. int quest_id = script_hasdata(st, 2) ? script_getnum(st, 2) : 0;
  22598. if( quest_id != 0 ){
  22599. int i;
  22600. ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
  22601. if (i == sd->avail_quests)
  22602. ShowWarning("buildin_open_quest_ui: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
  22603. }
  22604. clif_ui_open( *sd, OUT_UI_QUEST, quest_id );
  22605. return SCRIPT_CMD_SUCCESS;
  22606. #endif
  22607. }
  22608. BUILDIN_FUNC(openbank){
  22609. #if PACKETVER < 20151202
  22610. ShowError( "buildin_openbank: This command requires PACKETVER 20151202 or newer.\n" );
  22611. return SCRIPT_CMD_FAILURE;
  22612. #else
  22613. struct map_session_data* sd = nullptr;
  22614. if (!script_charid2sd(2, sd)) {
  22615. return SCRIPT_CMD_FAILURE;
  22616. }
  22617. if( !battle_config.feature_banking ){
  22618. ShowError( "buildin_openbank: banking is disabled.\n" );
  22619. return SCRIPT_CMD_FAILURE;
  22620. }
  22621. clif_ui_open( *sd, OUT_UI_BANK, 0 );
  22622. return SCRIPT_CMD_SUCCESS;
  22623. #endif
  22624. }
  22625. BUILDIN_FUNC(getbaseexp_ratio){
  22626. struct map_session_data *sd;
  22627. if( !script_charid2sd( 4, sd ) ){
  22628. return SCRIPT_CMD_FAILURE;
  22629. }
  22630. int32 percent = script_getnum( st, 2 );
  22631. if( percent < 0 || percent > 100 ){
  22632. ShowError( "getbaseexp_ratio: percentage is out of range.\n" );
  22633. return SCRIPT_CMD_FAILURE;
  22634. }
  22635. int32 base_level;
  22636. if( script_hasdata( st, 3 ) ){
  22637. base_level = script_getnum( st, 3 );
  22638. if( base_level < 1 ){
  22639. ShowError( "getbaseexp_ratio: level cannot be below 1.\n" );
  22640. return SCRIPT_CMD_FAILURE;
  22641. }
  22642. uint32 max_level = job_db.get_maxBaseLv( sd->status.class_ );
  22643. if( base_level > max_level ){
  22644. ShowError( "getbaseexp_ratio: level is above maximum base level %u for job %s.\n", max_level, job_name( sd->status.class_ ) );
  22645. return SCRIPT_CMD_FAILURE;
  22646. }
  22647. }else{
  22648. base_level = sd->status.base_level;
  22649. }
  22650. t_exp class_exp = job_db.get_baseExp( sd->status.class_, base_level );
  22651. if( class_exp <= 0 ){
  22652. ShowError( "getbaseexp_ratio: No base experience defined for class %s at base level %d.\n", job_name( sd->status.class_ ), base_level );
  22653. return SCRIPT_CMD_FAILURE;
  22654. }
  22655. double result = ( class_exp * percent ) / 100.0;
  22656. script_pushint64( st, static_cast<t_exp>( result ) );
  22657. return SCRIPT_CMD_SUCCESS;
  22658. }
  22659. BUILDIN_FUNC(getjobexp_ratio){
  22660. struct map_session_data *sd;
  22661. if( !script_charid2sd( 4, sd ) ){
  22662. return SCRIPT_CMD_FAILURE;
  22663. }
  22664. int32 percent = script_getnum( st, 2 );
  22665. if( percent < 0 || percent > 100 ){
  22666. ShowError( "getjobexp_ratio: percentage is out of range.\n" );
  22667. return SCRIPT_CMD_FAILURE;
  22668. }
  22669. int32 job_level;
  22670. if( script_hasdata( st, 3 ) ){
  22671. job_level = script_getnum( st, 3 );
  22672. if( job_level < 1 ){
  22673. ShowError( "getjobexp_ratio: level cannot be below 1.\n" );
  22674. return SCRIPT_CMD_FAILURE;
  22675. }
  22676. uint32 max_level = job_db.get_maxJobLv( sd->status.class_ );
  22677. if( job_level > max_level ){
  22678. ShowError( "getjobexp_ratio: level is above maximum job level %u for job %s.\n", max_level, job_name( sd->status.class_ ) );
  22679. return SCRIPT_CMD_FAILURE;
  22680. }
  22681. }else{
  22682. job_level = sd->status.job_level;
  22683. }
  22684. t_exp class_exp = job_db.get_baseExp( sd->status.class_, job_level );
  22685. if( class_exp <= 0 ){
  22686. ShowError( "getjobexp_ratio: No job experience defined for class %s at job level %d.\n", job_name( sd->status.class_ ), job_level );
  22687. return SCRIPT_CMD_FAILURE;
  22688. }
  22689. double result = ( class_exp * percent ) / 100.0;
  22690. script_pushint64( st, static_cast<t_exp>( result ) );
  22691. return SCRIPT_CMD_SUCCESS;
  22692. }
  22693. BUILDIN_FUNC( enchantgradeui ){
  22694. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  22695. struct map_session_data* sd;
  22696. if( !script_charid2sd( 2, sd ) ){
  22697. return SCRIPT_CMD_FAILURE;
  22698. }
  22699. clif_ui_open( *sd, OUT_UI_ENCHANTGRADE, 0 );
  22700. return SCRIPT_CMD_SUCCESS;
  22701. #else
  22702. ShowError( "buildin_enchantgradeui: This command requires PACKETVER 2020-07-24 or newer.\n" );
  22703. return SCRIPT_CMD_FAILURE;
  22704. #endif
  22705. }
  22706. BUILDIN_FUNC(set_reputation_points){
  22707. struct map_session_data* sd;
  22708. if( !script_charid2sd( 4, sd ) ){
  22709. return SCRIPT_CMD_FAILURE;
  22710. }
  22711. int64 type = script_getnum64( st, 2 );
  22712. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22713. if( reputation == nullptr ){
  22714. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22715. return SCRIPT_CMD_FAILURE;
  22716. }
  22717. int64 points = script_getnum64( st, 3 );
  22718. points = cap_value( points, reputation->minimum, reputation->maximum );
  22719. if( !pc_setreg2( sd, reputation->variable.c_str(), points ) ){
  22720. return SCRIPT_CMD_FAILURE;
  22721. }
  22722. clif_reputation_type( *sd, type, points );
  22723. return SCRIPT_CMD_SUCCESS;
  22724. }
  22725. BUILDIN_FUNC(get_reputation_points){
  22726. struct map_session_data* sd;
  22727. if( !script_charid2sd( 3, sd ) ){
  22728. return SCRIPT_CMD_FAILURE;
  22729. }
  22730. int64 type = script_getnum64( st, 2 );
  22731. std::shared_ptr<s_reputation> reputation = reputation_db.find( type );
  22732. if( reputation == nullptr ){
  22733. ShowError( "buildin_set_reputation_points: Unknown reputation type %" PRIi64 ".\n", type );
  22734. return SCRIPT_CMD_FAILURE;
  22735. }
  22736. int64 points = pc_readreg2( sd, reputation->variable.c_str() );
  22737. points = cap_value( points, reputation->minimum, reputation->maximum );
  22738. script_pushint( st, points );
  22739. return SCRIPT_CMD_SUCCESS;
  22740. }
  22741. BUILDIN_FUNC(item_reform){
  22742. #if PACKETVER < 20211103
  22743. ShowError( "buildin_item_reform: This command requires packet version 2021-11-03 or newer.\n" );
  22744. return SCRIPT_CMD_FAILURE;
  22745. #else
  22746. struct map_session_data* sd;
  22747. if( !script_charid2sd( 3, sd ) ){
  22748. return SCRIPT_CMD_FAILURE;
  22749. }
  22750. t_itemid item_id;
  22751. if( script_hasdata( st, 2 ) ){
  22752. if( script_isstring( st, 2 ) ){
  22753. const char* item_name = script_getstr( st, 2 );
  22754. std::shared_ptr<item_data> item = item_db.searchname( item_name );
  22755. if( item == nullptr ){
  22756. ShowError("buildin_item_reform: Item \"%s\" does not exist.\n", item_name );
  22757. return SCRIPT_CMD_FAILURE;
  22758. }
  22759. item_id = item->nameid;
  22760. }else{
  22761. item_id = script_getnum( st, 2 );
  22762. if( !item_db.exists( item_id ) ){
  22763. ShowError( "buildin_item_reform: Item ID %u does not exist.\n", item_id );
  22764. return SCRIPT_CMD_FAILURE;
  22765. }
  22766. }
  22767. }else{
  22768. if( sd->itemid == 0 ){
  22769. ShowError( "buildin_item_reform: Called outside of an item script without item id.\n" );
  22770. return SCRIPT_CMD_FAILURE;
  22771. }
  22772. if( sd->inventory_data[sd->itemindex]->flag.delay_consume == 0 ){
  22773. ShowError( "buildin_item_reform: Called from item %u, which is not a DelayConsume type.\n", sd->itemid );
  22774. return SCRIPT_CMD_FAILURE;
  22775. }
  22776. item_id = sd->itemid;
  22777. }
  22778. if( !item_reform_db.exists( item_id ) ){
  22779. ShowError( "buildin_item_reform: Item ID %u is not in the Item Reform database.\n", item_id );
  22780. return SCRIPT_CMD_FAILURE;
  22781. }
  22782. clif_item_reform_open( *sd, item_id );
  22783. return SCRIPT_CMD_SUCCESS;
  22784. #endif
  22785. }
  22786. BUILDIN_FUNC(item_enchant){
  22787. #if !( PACKETVER_RE_NUM >= 20211103 || PACKETVER_MAIN_NUM >= 20220330 )
  22788. ShowError( "buildin_item_enchant: This command requires packet version 2021-11-03 or newer.\n" );
  22789. return SCRIPT_CMD_FAILURE;
  22790. #else
  22791. struct map_session_data* sd;
  22792. if( !script_charid2sd( 3, sd ) ){
  22793. return SCRIPT_CMD_FAILURE;
  22794. }
  22795. uint64 clientLuaIndex = script_getnum64( st, 2 );
  22796. if( !item_enchant_db.exists( clientLuaIndex ) ){
  22797. ShowError( "buildin_item_enchant: %" PRIu64 " is not a valid item enchant index.\n", clientLuaIndex );
  22798. return SCRIPT_CMD_FAILURE;
  22799. }
  22800. clif_enchantwindow_open( *sd, clientLuaIndex );
  22801. return SCRIPT_CMD_SUCCESS;
  22802. #endif
  22803. }
  22804. BUILDIN_FUNC(addfame) {
  22805. struct map_session_data *sd;
  22806. if (!script_charid2sd(3, sd))
  22807. return SCRIPT_CMD_FAILURE;
  22808. if (!pc_addfame(*sd, script_getnum(st, 2)))
  22809. return SCRIPT_CMD_FAILURE;
  22810. return SCRIPT_CMD_SUCCESS;
  22811. }
  22812. BUILDIN_FUNC(getfame) {
  22813. struct map_session_data *sd;
  22814. if (!script_charid2sd(2, sd))
  22815. return SCRIPT_CMD_FAILURE;
  22816. script_pushint(st, sd->status.fame);
  22817. return SCRIPT_CMD_SUCCESS;
  22818. }
  22819. BUILDIN_FUNC(getfamerank) {
  22820. struct map_session_data *sd;
  22821. if (!script_charid2sd(2, sd))
  22822. return SCRIPT_CMD_FAILURE;
  22823. script_pushint(st, pc_famerank(sd->status.char_id, sd->class_ & MAPID_UPPERMASK));
  22824. return SCRIPT_CMD_SUCCESS;
  22825. }
  22826. #include "../custom/script.inc"
  22827. // declarations that were supposed to be exported from npc_chat.cpp
  22828. #ifdef PCRE_SUPPORT
  22829. BUILDIN_FUNC(defpattern);
  22830. BUILDIN_FUNC(activatepset);
  22831. BUILDIN_FUNC(deactivatepset);
  22832. BUILDIN_FUNC(deletepset);
  22833. #endif
  22834. /** Regular expression matching
  22835. * preg_match(<pattern>,<string>{,<offset>})
  22836. */
  22837. BUILDIN_FUNC(preg_match) {
  22838. #ifdef PCRE_SUPPORT
  22839. pcre *re;
  22840. pcre_extra *pcreExtra;
  22841. const char *error;
  22842. int erroffset, r, offset = 0;
  22843. int subStrVec[30];
  22844. const char* pattern = script_getstr(st,2);
  22845. const char* subject = script_getstr(st,3);
  22846. if (script_hasdata(st,4))
  22847. offset = script_getnum(st,4);
  22848. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  22849. pcreExtra = pcre_study(re, 0, &error);
  22850. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  22851. pcre_free(re);
  22852. if (pcreExtra != NULL)
  22853. pcre_free(pcreExtra);
  22854. if (r < 0)
  22855. script_pushint(st,0);
  22856. else
  22857. script_pushint(st,(r > 0) ? r : 30 / 3);
  22858. return SCRIPT_CMD_SUCCESS;
  22859. #else
  22860. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  22861. script_pushint(st,0);
  22862. return SCRIPT_CMD_SUCCESS;
  22863. #endif
  22864. }
  22865. /// script command definitions
  22866. /// for an explanation on args, see add_buildin_func
  22867. struct script_function buildin_func[] = {
  22868. // NPC interaction
  22869. BUILDIN_DEF(mes,"s*"),
  22870. BUILDIN_DEF(next,""),
  22871. BUILDIN_DEF(clear,""),
  22872. BUILDIN_DEF(close,""),
  22873. BUILDIN_DEF(close2,""),
  22874. BUILDIN_DEF(menu,"sl*"),
  22875. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  22876. BUILDIN_DEF(prompt,"s*"),
  22877. //
  22878. BUILDIN_DEF(goto,"l"),
  22879. BUILDIN_DEF(callsub,"l*"),
  22880. BUILDIN_DEF(callfunc,"s*"),
  22881. BUILDIN_DEF(return,"?"),
  22882. BUILDIN_DEF(getarg,"i?"),
  22883. BUILDIN_DEF(jobchange,"i??"),
  22884. BUILDIN_DEF(jobname,"i"),
  22885. BUILDIN_DEF(input,"r??"),
  22886. BUILDIN_DEF(warp,"sii?"),
  22887. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  22888. BUILDIN_DEF(areawarp,"siiiisii??"),
  22889. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  22890. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  22891. BUILDIN_DEF(setlook,"ii?"),
  22892. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  22893. BUILDIN_DEF2(setr,"set","rv?"),
  22894. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  22895. BUILDIN_DEF(setarray,"rv*"),
  22896. BUILDIN_DEF(cleararray,"rvi"),
  22897. BUILDIN_DEF(copyarray,"rri"),
  22898. BUILDIN_DEF(getarraysize,"r"),
  22899. BUILDIN_DEF(deletearray,"r?"),
  22900. BUILDIN_DEF(getelementofarray,"ri"),
  22901. BUILDIN_DEF(inarray,"rv"),
  22902. BUILDIN_DEF(countinarray,"rr"),
  22903. BUILDIN_DEF(getitem,"vi?"),
  22904. BUILDIN_DEF(rentitem,"vi?"),
  22905. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  22906. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  22907. BUILDIN_DEF(getnameditem,"vv"),
  22908. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  22909. BUILDIN_DEF(makeitem,"visii?"),
  22910. BUILDIN_DEF(makeitem2,"visiiiiiiiii?"),
  22911. BUILDIN_DEF(delitem,"vi?"),
  22912. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  22913. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  22914. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  22915. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  22916. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  22917. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  22918. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  22919. BUILDIN_DEF(delitemidx,"i??"),
  22920. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  22921. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  22922. BUILDIN_DEF(cutin,"si"),
  22923. BUILDIN_DEF(viewpoint,"iiiii?"),
  22924. BUILDIN_DEF(viewpointmap, "siiiii"),
  22925. BUILDIN_DEF(heal,"ii?"),
  22926. BUILDIN_DEF(healap,"i?"),
  22927. BUILDIN_DEF(itemheal,"ii?"),
  22928. BUILDIN_DEF(percentheal,"ii?"),
  22929. BUILDIN_DEF(rand,"i?"),
  22930. BUILDIN_DEF(countitem,"v?"),
  22931. BUILDIN_DEF(storagecountitem,"v?"),
  22932. BUILDIN_DEF(guildstoragecountitem,"v?"),
  22933. BUILDIN_DEF(cartcountitem,"v?"),
  22934. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  22935. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  22936. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  22937. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  22938. BUILDIN_DEF(checkweight,"vi*"),
  22939. BUILDIN_DEF(checkweight2,"rr"),
  22940. BUILDIN_DEF(readparam,"i?"),
  22941. BUILDIN_DEF(getcharid,"i?"),
  22942. BUILDIN_DEF(getnpcid,"i?"),
  22943. BUILDIN_DEF(getpartyname,"i"),
  22944. BUILDIN_DEF(getpartymember,"i??"),
  22945. BUILDIN_DEF(getpartyleader,"i?"),
  22946. BUILDIN_DEF(getguildname,"i"),
  22947. BUILDIN_DEF(getguildmaster,"i"),
  22948. BUILDIN_DEF(getguildmasterid,"i"),
  22949. BUILDIN_DEF(strcharinfo,"i?"),
  22950. BUILDIN_DEF(strnpcinfo,"i"),
  22951. BUILDIN_DEF(getequipid,"??"),
  22952. BUILDIN_DEF(getequipuniqueid,"i?"),
  22953. BUILDIN_DEF(getequipname,"i?"),
  22954. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  22955. BUILDIN_DEF(repair,"i?"), // [Valaris]
  22956. BUILDIN_DEF(repairall,"?"),
  22957. BUILDIN_DEF(getequipisequiped,"i?"),
  22958. BUILDIN_DEF(getequipisenableref,"i?"),
  22959. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  22960. BUILDIN_DEF(getequipweaponlv,"??"),
  22961. BUILDIN_DEF(getequiparmorlv, "??"),
  22962. BUILDIN_DEF(getequippercentrefinery,"i?"),
  22963. BUILDIN_DEF(successrefitem,"i??"),
  22964. BUILDIN_DEF(failedrefitem,"i?"),
  22965. BUILDIN_DEF(downrefitem,"i??"),
  22966. BUILDIN_DEF(statusup,"i?"),
  22967. BUILDIN_DEF(statusup2,"ii?"),
  22968. BUILDIN_DEF(traitstatusup,"i?"),
  22969. BUILDIN_DEF(traitstatusup2,"ii?"),
  22970. BUILDIN_DEF(bonus,"i?"),
  22971. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  22972. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  22973. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  22974. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  22975. BUILDIN_DEF(autobonus,"sii??"),
  22976. BUILDIN_DEF(autobonus2,"sii??"),
  22977. BUILDIN_DEF(autobonus3,"siiv?"),
  22978. BUILDIN_DEF(petautobonus,"sii??"),
  22979. BUILDIN_DEF2(petautobonus,"petautobonus2","sii??"),
  22980. BUILDIN_DEF(petautobonus3,"siiv?"),
  22981. BUILDIN_DEF(plagiarizeskill, "ii"),
  22982. BUILDIN_DEF(plagiarizeskillreset, "i"),
  22983. BUILDIN_DEF(skill,"vi?"),
  22984. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  22985. BUILDIN_DEF(guildskill,"vi"),
  22986. BUILDIN_DEF(getskilllv,"v"),
  22987. BUILDIN_DEF(getgdskilllv,"iv"),
  22988. BUILDIN_DEF(basicskillcheck,""),
  22989. BUILDIN_DEF(getgmlevel,"?"),
  22990. BUILDIN_DEF(getgroupid,"?"),
  22991. BUILDIN_DEF(end,""),
  22992. BUILDIN_DEF(checkoption,"i?"),
  22993. BUILDIN_DEF(setoption,"i??"),
  22994. BUILDIN_DEF(setcart,"??"),
  22995. BUILDIN_DEF(checkcart,"?"),
  22996. BUILDIN_DEF(setfalcon,"??"),
  22997. BUILDIN_DEF(checkfalcon,"?"),
  22998. BUILDIN_DEF(setriding,"??"),
  22999. BUILDIN_DEF(checkriding,"?"),
  23000. BUILDIN_DEF(checkwug,"?"),
  23001. BUILDIN_DEF(checkmadogear,"?"),
  23002. BUILDIN_DEF(setmadogear,"???"),
  23003. BUILDIN_DEF2(savepoint,"save","sii???"),
  23004. BUILDIN_DEF(savepoint,"sii???"),
  23005. BUILDIN_DEF(gettimetick,"i"),
  23006. BUILDIN_DEF(gettime,"i"),
  23007. BUILDIN_DEF(gettimestr,"si?"),
  23008. BUILDIN_DEF(openstorage,""),
  23009. BUILDIN_DEF(guildopenstorage,""),
  23010. BUILDIN_DEF(guildopenstorage_log,"?"),
  23011. BUILDIN_DEF(guild_has_permission,"i?"),
  23012. BUILDIN_DEF(itemskill,"vi?"),
  23013. BUILDIN_DEF(produce,"i"),
  23014. BUILDIN_DEF(cooking,"i"),
  23015. BUILDIN_DEF(monster,"siisvi???"),
  23016. BUILDIN_DEF(getmobdrops,"i"),
  23017. BUILDIN_DEF(areamonster,"siiiisvi???"),
  23018. BUILDIN_DEF(killmonster,"ss?"),
  23019. BUILDIN_DEF(killmonsterall,"s?"),
  23020. BUILDIN_DEF(clone,"siisi????"),
  23021. BUILDIN_DEF(doevent,"s"),
  23022. BUILDIN_DEF(donpcevent,"s"),
  23023. BUILDIN_DEF(cmdothernpc,"ss"),
  23024. BUILDIN_DEF(addtimer,"is"),
  23025. BUILDIN_DEF(deltimer,"s"),
  23026. BUILDIN_DEF(addtimercount,"is"),
  23027. BUILDIN_DEF(initnpctimer,"??"),
  23028. BUILDIN_DEF(stopnpctimer,"??"),
  23029. BUILDIN_DEF(startnpctimer,"??"),
  23030. BUILDIN_DEF(setnpctimer,"i?"),
  23031. BUILDIN_DEF(getnpctimer,"i?"),
  23032. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  23033. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  23034. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  23035. BUILDIN_DEF(announce,"si??????"),
  23036. BUILDIN_DEF(mapannounce,"ssi?????"),
  23037. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  23038. BUILDIN_DEF(getusers,"i"),
  23039. BUILDIN_DEF(getmapguildusers,"si"),
  23040. BUILDIN_DEF(getmapusers,"s"),
  23041. BUILDIN_DEF(getareausers,"siiii"),
  23042. BUILDIN_DEF(getunits, "i?"),
  23043. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  23044. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  23045. BUILDIN_DEF(getareadropitem,"siiiiv"),
  23046. BUILDIN_DEF(enablenpc,"?"),
  23047. BUILDIN_DEF2(enablenpc, "disablenpc", "?"),
  23048. BUILDIN_DEF2(enablenpc, "hideoffnpc", "?"),
  23049. BUILDIN_DEF2(enablenpc, "hideonnpc", "?"),
  23050. BUILDIN_DEF2(enablenpc, "cloakoffnpc", "??"),
  23051. BUILDIN_DEF2(enablenpc, "cloakonnpc", "??"),
  23052. BUILDIN_DEF2(enablenpc, "cloakoffnpcself", "?"),
  23053. BUILDIN_DEF2(enablenpc, "cloakonnpcself", "?"),
  23054. BUILDIN_DEF(sc_start,"iii???"),
  23055. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  23056. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  23057. BUILDIN_DEF(sc_end,"i?"),
  23058. BUILDIN_DEF(sc_end_class,"??"),
  23059. BUILDIN_DEF(getstatus, "i??"),
  23060. BUILDIN_DEF(getscrate,"ii?"),
  23061. BUILDIN_DEF(debugmes,"s"),
  23062. BUILDIN_DEF(errormes,"s"),
  23063. BUILDIN_DEF2(catchpet,"pet","i"),
  23064. BUILDIN_DEF2(birthpet,"bpet",""),
  23065. BUILDIN_DEF(catchpet,"i"),
  23066. BUILDIN_DEF(birthpet,""),
  23067. BUILDIN_DEF(resetlvl,"i?"),
  23068. BUILDIN_DEF(resetstatus,"?"),
  23069. BUILDIN_DEF(resetskill,"?"),
  23070. BUILDIN_DEF(resetfeel,"?"),
  23071. BUILDIN_DEF(resethate,"?"),
  23072. BUILDIN_DEF(skillpointcount,"?"),
  23073. BUILDIN_DEF(changebase,"i?"),
  23074. BUILDIN_DEF(changesex,"?"),
  23075. BUILDIN_DEF(changecharsex,"?"),
  23076. BUILDIN_DEF(waitingroom,"si?????"),
  23077. BUILDIN_DEF(delwaitingroom,"?"),
  23078. BUILDIN_DEF(waitingroomkick,"ss"),
  23079. BUILDIN_DEF(getwaitingroomusers, "?"),
  23080. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  23081. BUILDIN_DEF(enablewaitingroomevent,"?"),
  23082. BUILDIN_DEF(disablewaitingroomevent,"?"),
  23083. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  23084. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  23085. BUILDIN_DEF(getwaitingroomstate,"i?"),
  23086. BUILDIN_DEF(warpwaitingpc,"sii?"),
  23087. BUILDIN_DEF(attachrid,"i?"),
  23088. BUILDIN_DEF(addrid,"i?????"),
  23089. BUILDIN_DEF(detachrid,""),
  23090. BUILDIN_DEF(isloggedin,"i?"),
  23091. BUILDIN_DEF(setmapflagnosave,"ssii"),
  23092. BUILDIN_DEF(getmapflag,"si?"),
  23093. BUILDIN_DEF(setmapflag,"si??"),
  23094. BUILDIN_DEF(removemapflag,"si?"),
  23095. BUILDIN_DEF(pvpon,"s"),
  23096. BUILDIN_DEF(pvpoff,"s"),
  23097. BUILDIN_DEF(gvgon,"s"),
  23098. BUILDIN_DEF(gvgoff,"s"),
  23099. BUILDIN_DEF(emotion,"i?"),
  23100. BUILDIN_DEF(maprespawnguildid,"sii"),
  23101. BUILDIN_DEF(agitstart,""), // <Agit>
  23102. BUILDIN_DEF(agitend,""),
  23103. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  23104. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  23105. BUILDIN_DEF(getcastlename,"s"),
  23106. BUILDIN_DEF(getcastledata,"si"),
  23107. BUILDIN_DEF(setcastledata,"sii"),
  23108. BUILDIN_DEF(requestguildinfo,"i?"),
  23109. BUILDIN_DEF(getequipcardcnt,"i"),
  23110. BUILDIN_DEF(successremovecards,"i"),
  23111. BUILDIN_DEF(failedremovecards,"ii"),
  23112. BUILDIN_DEF(marriage,"s"),
  23113. BUILDIN_DEF2(wedding_effect,"wedding",""),
  23114. BUILDIN_DEF(divorce,"?"),
  23115. BUILDIN_DEF(ispartneron,"?"),
  23116. BUILDIN_DEF(getpartnerid,"?"),
  23117. BUILDIN_DEF(getchildid,"?"),
  23118. BUILDIN_DEF(getmotherid,"?"),
  23119. BUILDIN_DEF(getfatherid,"?"),
  23120. BUILDIN_DEF(warppartner,"sii"),
  23121. BUILDIN_DEF(getitemname,"v"),
  23122. BUILDIN_DEF(getitemslots,"i"),
  23123. BUILDIN_DEF(makepet,"i"),
  23124. BUILDIN_DEF(getexp,"ii?"),
  23125. BUILDIN_DEF(getinventorylist,"?"),
  23126. BUILDIN_DEF(getskilllist,"?"),
  23127. BUILDIN_DEF(clearitem,"?"),
  23128. BUILDIN_DEF(classchange,"i??"),
  23129. BUILDIN_DEF(misceffect,"i"),
  23130. BUILDIN_DEF(playBGM,"s"),
  23131. BUILDIN_DEF(playBGMall,"s?????"),
  23132. BUILDIN_DEF(soundeffect,"si"),
  23133. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  23134. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  23135. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  23136. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  23137. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  23138. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  23139. BUILDIN_DEF(petloot,"i"), // [Valaris]
  23140. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  23141. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  23142. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  23143. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  23144. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  23145. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  23146. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  23147. BUILDIN_DEF(removespecialeffect,"i??"),
  23148. BUILDIN_DEF2(removespecialeffect,"removespecialeffect2","i??"),
  23149. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  23150. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  23151. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  23152. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  23153. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  23154. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  23155. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  23156. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  23157. BUILDIN_DEF(mobcount,"ss"),
  23158. BUILDIN_DEF(getlook,"i?"),
  23159. BUILDIN_DEF(getsavepoint,"i?"),
  23160. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  23161. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  23162. BUILDIN_DEF(npcstop,""), // [Valaris]
  23163. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  23164. BUILDIN_DEF(mapid2name,"i"),
  23165. BUILDIN_DEF(checkoption1,"i?"),
  23166. BUILDIN_DEF(checkoption2,"i?"),
  23167. BUILDIN_DEF(guildgetexp,"i"),
  23168. BUILDIN_DEF(guildchangegm,"is"),
  23169. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  23170. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  23171. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  23172. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  23173. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  23174. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  23175. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  23176. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  23177. BUILDIN_DEF(night,""), // sets the server to night time
  23178. BUILDIN_DEF(day,""), // sets the server to day time
  23179. #ifdef PCRE_SUPPORT
  23180. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  23181. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  23182. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  23183. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  23184. #endif
  23185. BUILDIN_DEF(preg_match,"ss?"),
  23186. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  23187. BUILDIN_DEF(recovery,"i???"),
  23188. BUILDIN_DEF(getpetinfo,"i?"),
  23189. BUILDIN_DEF(gethominfo,"i?"),
  23190. BUILDIN_DEF(addhomintimacy,"i?"),
  23191. BUILDIN_DEF(getmercinfo,"i?"),
  23192. BUILDIN_DEF(checkequipedcard,"i"),
  23193. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  23194. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  23195. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  23196. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  23197. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  23198. BUILDIN_DEF(charat,"si"),
  23199. BUILDIN_DEF(setchar,"ssi"),
  23200. BUILDIN_DEF(insertchar,"ssi"),
  23201. BUILDIN_DEF(delchar,"si"),
  23202. BUILDIN_DEF(strtoupper,"s"),
  23203. BUILDIN_DEF(strtolower,"s"),
  23204. BUILDIN_DEF(charisupper, "si"),
  23205. BUILDIN_DEF(charislower, "si"),
  23206. BUILDIN_DEF(substr,"sii"),
  23207. BUILDIN_DEF(explode, "rss"),
  23208. BUILDIN_DEF(implode, "r?"),
  23209. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  23210. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  23211. BUILDIN_DEF(strpos,"ss?"),
  23212. BUILDIN_DEF(replacestr,"sss??"),
  23213. BUILDIN_DEF(countstr,"ss?"),
  23214. BUILDIN_DEF(setnpcdisplay,"sv??"),
  23215. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  23216. BUILDIN_DEF(strcmp,"ss"),
  23217. BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
  23218. BUILDIN_DEF(setiteminfo,"vii"), //[Lupus] set Items Buy / sell Price, etc info
  23219. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  23220. // [zBuffer] List of mathematics commands --->
  23221. BUILDIN_DEF(sqrt,"i"),
  23222. BUILDIN_DEF(pow,"ii"),
  23223. BUILDIN_DEF(distance,"iiii"),
  23224. BUILDIN_DEF2(minmax,"min", "*"),
  23225. BUILDIN_DEF2(minmax,"minimum", "*"),
  23226. BUILDIN_DEF2(minmax,"max", "*"),
  23227. BUILDIN_DEF2(minmax,"maximum", "*"),
  23228. BUILDIN_DEF(cap_value, "iii"),
  23229. // <--- [zBuffer] List of mathematics commands
  23230. BUILDIN_DEF(md5,"s"),
  23231. // [zBuffer] List of dynamic var commands --->
  23232. BUILDIN_DEF(getd,"s"),
  23233. BUILDIN_DEF(setd,"sv?"),
  23234. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  23235. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  23236. BUILDIN_DEF(npcshopadditem,"sii*"),
  23237. BUILDIN_DEF(npcshopdelitem,"si*"),
  23238. BUILDIN_DEF(npcshopattach,"s?"),
  23239. BUILDIN_DEF(equip,"i?"),
  23240. BUILDIN_DEF(autoequip,"ii"),
  23241. BUILDIN_DEF(setbattleflag,"si?"),
  23242. BUILDIN_DEF(getbattleflag,"s"),
  23243. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  23244. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  23245. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  23246. BUILDIN_DEF(getrandmobid, "i??"),
  23247. BUILDIN_DEF(getmonsterinfo,"vi"), //Lupus
  23248. BUILDIN_DEF(addmonsterdrop,"vii??"), //Akinari [Lupus]
  23249. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  23250. BUILDIN_DEF(axtoi,"s"),
  23251. BUILDIN_DEF(query_sql,"s*"),
  23252. BUILDIN_DEF(query_logsql,"s*"),
  23253. BUILDIN_DEF(escape_sql,"v"),
  23254. BUILDIN_DEF(atoi,"s"),
  23255. BUILDIN_DEF(strtol,"si"),
  23256. // [zBuffer] List of player cont commands --->
  23257. BUILDIN_DEF(rid2name,"i"),
  23258. BUILDIN_DEF(pcfollow,"ii"),
  23259. BUILDIN_DEF(pcstopfollow,"i"),
  23260. BUILDIN_DEF(pcblockmove,"ii"),
  23261. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  23262. BUILDIN_DEF(pcblockskill,"ii"),
  23263. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  23264. BUILDIN_DEF(setpcblock, "ii?"),
  23265. BUILDIN_DEF(getpcblock, "?"),
  23266. // <--- [zBuffer] List of player cont commands
  23267. // [zBuffer] List of unit control commands --->
  23268. BUILDIN_DEF(unitexists,"i"),
  23269. BUILDIN_DEF(getunittype,"i"),
  23270. BUILDIN_DEF(getunitname,"i"),
  23271. BUILDIN_DEF(setunitname,"is"),
  23272. BUILDIN_DEF(setunittitle,"is"),
  23273. BUILDIN_DEF(getunittitle,"i"),
  23274. BUILDIN_DEF(getunitdata,"i*"),
  23275. BUILDIN_DEF(setunitdata,"iii"),
  23276. BUILDIN_DEF(unitwalk,"iii?"),
  23277. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  23278. BUILDIN_DEF(unitkill,"i"),
  23279. BUILDIN_DEF(unitwarp,"isii"),
  23280. BUILDIN_DEF(unitattack,"iv?"),
  23281. BUILDIN_DEF(unitstopattack,"i"),
  23282. BUILDIN_DEF(unitstopwalk,"i?"),
  23283. BUILDIN_DEF(unittalk,"is?"),
  23284. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  23285. BUILDIN_DEF(unitskilluseid,"ivi????"), // originally by Qamera [Celest]
  23286. BUILDIN_DEF(unitskillusepos,"iviii???"), // [Celest]
  23287. // <--- [zBuffer] List of unit control commands
  23288. BUILDIN_DEF(sleep,"i"),
  23289. BUILDIN_DEF(sleep2,"i"),
  23290. BUILDIN_DEF(awake,"s"),
  23291. BUILDIN_DEF(getvariableofnpc,"rs"),
  23292. BUILDIN_DEF(warpportal,"iisii"),
  23293. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  23294. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  23295. BUILDIN_DEF(morphembryo,""),
  23296. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  23297. BUILDIN_DEF(checkhomcall,""),
  23298. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  23299. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  23300. BUILDIN_DEF(checkvending,"?"),
  23301. BUILDIN_DEF(checkchatting,"?"),
  23302. BUILDIN_DEF(checkidle,"?"),
  23303. BUILDIN_DEF(checkidlehom,"?"),
  23304. BUILDIN_DEF(checkidlemer,"?"),
  23305. BUILDIN_DEF(openmail,"?"),
  23306. BUILDIN_DEF(openauction,"?"),
  23307. BUILDIN_DEF(checkcell,"siii"),
  23308. BUILDIN_DEF(setcell,"siiiiii"),
  23309. BUILDIN_DEF(getfreecell,"srr?????"),
  23310. BUILDIN_DEF(setwall,"siiiiis"),
  23311. BUILDIN_DEF(delwall,"s"),
  23312. BUILDIN_DEF(checkwall,"s"),
  23313. BUILDIN_DEF(searchitem,"rs"),
  23314. BUILDIN_DEF(mercenary_create,"ii"),
  23315. BUILDIN_DEF(mercenary_delete,"??"),
  23316. BUILDIN_DEF(mercenary_heal,"ii"),
  23317. BUILDIN_DEF(mercenary_sc_start,"iii"),
  23318. BUILDIN_DEF(mercenary_get_calls,"i"),
  23319. BUILDIN_DEF(mercenary_get_faith,"i"),
  23320. BUILDIN_DEF(mercenary_set_calls,"ii"),
  23321. BUILDIN_DEF(mercenary_set_faith,"ii"),
  23322. BUILDIN_DEF(readbook,"ii"),
  23323. BUILDIN_DEF(setfont,"i"),
  23324. BUILDIN_DEF(areamobuseskill,"siiivviiiii"),
  23325. BUILDIN_DEF(progressbar,"si"),
  23326. BUILDIN_DEF(progressbar_npc, "si?"),
  23327. BUILDIN_DEF(pushpc,"ii"),
  23328. BUILDIN_DEF(buyingstore,"i"),
  23329. BUILDIN_DEF(searchstores,"ii"),
  23330. BUILDIN_DEF(showdigit,"i?"),
  23331. // WoE SE
  23332. BUILDIN_DEF(agitstart2,""),
  23333. BUILDIN_DEF(agitend2,""),
  23334. BUILDIN_DEF(agitcheck2,""),
  23335. // BattleGround
  23336. BUILDIN_DEF(waitingroom2bg,"sii???"),
  23337. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  23338. BUILDIN_DEF(bg_team_setxy,"iii"),
  23339. BUILDIN_DEF(bg_warp,"isii"),
  23340. BUILDIN_DEF(bg_monster,"isiisi?"),
  23341. BUILDIN_DEF(bg_monster_set_team,"ii"),
  23342. BUILDIN_DEF(bg_leave,"?"),
  23343. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  23344. BUILDIN_DEF(bg_destroy,"i"),
  23345. BUILDIN_DEF(areapercentheal,"siiiiii"),
  23346. BUILDIN_DEF(bg_get_data,"ii"),
  23347. BUILDIN_DEF(bg_getareausers,"isiiii"),
  23348. BUILDIN_DEF(bg_updatescore,"sii"),
  23349. BUILDIN_DEF(bg_join,"i????"),
  23350. BUILDIN_DEF(bg_create,"sii??"),
  23351. BUILDIN_DEF(bg_reserve,"s?"),
  23352. BUILDIN_DEF(bg_unbook,"s"),
  23353. BUILDIN_DEF(bg_info,"si"),
  23354. // Instancing
  23355. BUILDIN_DEF(instance_create,"s??"),
  23356. BUILDIN_DEF(instance_destroy,"?"),
  23357. BUILDIN_DEF(instance_id,"?"),
  23358. BUILDIN_DEF(instance_enter,"s????"),
  23359. BUILDIN_DEF(instance_npcname,"s?"),
  23360. BUILDIN_DEF(instance_mapname,"s?"),
  23361. BUILDIN_DEF(instance_warpall,"sii?"),
  23362. BUILDIN_DEF(instance_announce,"isi?????"),
  23363. BUILDIN_DEF(instance_check_party,"i???"),
  23364. BUILDIN_DEF(instance_check_guild,"i???"),
  23365. BUILDIN_DEF(instance_check_clan,"i???"),
  23366. BUILDIN_DEF(instance_info,"si?"),
  23367. BUILDIN_DEF(instance_live_info,"i?"),
  23368. BUILDIN_DEF(instance_list, "s?"),
  23369. /**
  23370. * 3rd-related
  23371. **/
  23372. BUILDIN_DEF(makerune,"i?"),
  23373. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  23374. BUILDIN_DEF(setdragon,"??"),//[Ind]
  23375. BUILDIN_DEF(ismounting,"?"),//[Ind]
  23376. BUILDIN_DEF(setmounting,"?"),//[Ind]
  23377. BUILDIN_DEF(checkre,"i"),
  23378. /**
  23379. * rAthena and beyond!
  23380. **/
  23381. BUILDIN_DEF(getargcount,""),
  23382. BUILDIN_DEF(getcharip,"?"),
  23383. BUILDIN_DEF(is_function,"s"),
  23384. BUILDIN_DEF(get_revision,""),
  23385. BUILDIN_DEF(get_githash,""),
  23386. BUILDIN_DEF(freeloop,"?"),
  23387. BUILDIN_DEF(getrandgroupitem,"i????"),
  23388. BUILDIN_DEF(cleanmap,"s"),
  23389. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  23390. BUILDIN_DEF(npcskill,"viii"),
  23391. BUILDIN_DEF(consumeitem,"v?"),
  23392. BUILDIN_DEF(delequip,"i?"),
  23393. BUILDIN_DEF(breakequip,"i?"),
  23394. BUILDIN_DEF(sit,"?"),
  23395. BUILDIN_DEF(stand,"?"),
  23396. //@commands (script based)
  23397. BUILDIN_DEF(bindatcmd, "ss??"),
  23398. BUILDIN_DEF(unbindatcmd, "s"),
  23399. BUILDIN_DEF(useatcmd, "s"),
  23400. //Quest Log System [Inkfish]
  23401. BUILDIN_DEF(questinfo, "i??"),
  23402. BUILDIN_DEF(setquest, "i?"),
  23403. BUILDIN_DEF(erasequest, "i?"),
  23404. BUILDIN_DEF(completequest, "i?"),
  23405. BUILDIN_DEF(checkquest, "i??"),
  23406. BUILDIN_DEF(isbegin_quest,"i?"),
  23407. BUILDIN_DEF(changequest, "ii?"),
  23408. BUILDIN_DEF(showevent, "i??"),
  23409. BUILDIN_DEF(questinfo_refresh, "?"),
  23410. //Bound items [Xantara] & [Akinari]
  23411. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  23412. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  23413. BUILDIN_DEF(countbound, "??"),
  23414. // Party related
  23415. BUILDIN_DEF(party_create,"s???"),
  23416. BUILDIN_DEF(party_addmember,"ii"),
  23417. BUILDIN_DEF(party_delmember,"??"),
  23418. BUILDIN_DEF(party_changeleader,"ii"),
  23419. BUILDIN_DEF(party_changeoption,"iii"),
  23420. BUILDIN_DEF(party_destroy,"i"),
  23421. // Clan system
  23422. BUILDIN_DEF(clan_join,"i?"),
  23423. BUILDIN_DEF(clan_leave,"?"),
  23424. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  23425. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  23426. BUILDIN_DEF(vip_status,"i?"),
  23427. BUILDIN_DEF(vip_time,"i?"),
  23428. BUILDIN_DEF(bonus_script,"si????"),
  23429. BUILDIN_DEF(bonus_script_clear,"??"),
  23430. BUILDIN_DEF(getgroupitem,"i??"),
  23431. BUILDIN_DEF(enable_command,""),
  23432. BUILDIN_DEF(disable_command,""),
  23433. BUILDIN_DEF(getguildmember,"i??"),
  23434. BUILDIN_DEF(addspiritball,"ii?"),
  23435. BUILDIN_DEF(delspiritball,"i?"),
  23436. BUILDIN_DEF(countspiritball,"?"),
  23437. BUILDIN_DEF(mergeitem,"?"),
  23438. BUILDIN_DEF(mergeitem2,"??"),
  23439. BUILDIN_DEF(npcshopupdate,"sii?"),
  23440. BUILDIN_DEF(getattachedrid,""),
  23441. BUILDIN_DEF(getvar,"vi"),
  23442. BUILDIN_DEF(showscript,"s??"),
  23443. BUILDIN_DEF(ignoretimeout,"i?"),
  23444. BUILDIN_DEF(geteleminfo,"i?"),
  23445. BUILDIN_DEF(opendressroom,"i?"),
  23446. BUILDIN_DEF(navigateto,"s???????"),
  23447. BUILDIN_DEF(getguildalliance,"ii"),
  23448. BUILDIN_DEF(adopt,"vv"),
  23449. BUILDIN_DEF(getexp2,"ii?"),
  23450. BUILDIN_DEF(recalculatestat,""),
  23451. BUILDIN_DEF(hateffect,"ii"),
  23452. BUILDIN_DEF(getrandomoptinfo, "i"),
  23453. BUILDIN_DEF(getequiprandomoption, "iii?"),
  23454. BUILDIN_DEF(setrandomoption,"iiiii?"),
  23455. BUILDIN_DEF(needed_status_point,"ii?"),
  23456. BUILDIN_DEF(needed_trait_point, "ii?"),
  23457. BUILDIN_DEF(jobcanentermap,"s?"),
  23458. BUILDIN_DEF(openstorage2,"ii?"),
  23459. BUILDIN_DEF(unloadnpc, "s"),
  23460. BUILDIN_DEF(duplicate, "ssii?????"),
  23461. // WoE TE
  23462. BUILDIN_DEF(agitstart3,""),
  23463. BUILDIN_DEF(agitend3,""),
  23464. BUILDIN_DEF(agitcheck3,""),
  23465. BUILDIN_DEF(gvgon3,"s"),
  23466. BUILDIN_DEF(gvgoff3,"s"),
  23467. // Channel System
  23468. BUILDIN_DEF(channel_create,"ss?????"),
  23469. BUILDIN_DEF(channel_setopt,"sii"),
  23470. BUILDIN_DEF(channel_getopt,"si"),
  23471. BUILDIN_DEF(channel_setcolor,"si"),
  23472. BUILDIN_DEF(channel_setpass,"ss"),
  23473. BUILDIN_DEF(channel_setgroup,"si*"),
  23474. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  23475. BUILDIN_DEF(channel_chat,"ss?"),
  23476. BUILDIN_DEF(channel_ban,"si"),
  23477. BUILDIN_DEF(channel_unban,"si"),
  23478. BUILDIN_DEF(channel_kick,"sv"),
  23479. BUILDIN_DEF(channel_delete,"s"),
  23480. // Item Random Option Extension [Cydh]
  23481. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  23482. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  23483. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  23484. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr?"),
  23485. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  23486. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  23487. // Grade Extension
  23488. BUILDIN_DEF2(getitem2,"getitem4","viiiiiiiiirrr?"),
  23489. BUILDIN_DEF2(getitem2,"getitembound4","viiiiiiiiiirrr?"),
  23490. BUILDIN_DEF2(rentitem2,"rentitem4","viiiiiiiiirrr?"),
  23491. BUILDIN_DEF2(makeitem2,"makeitem4","visiiiiiiiiiirrr?"),
  23492. BUILDIN_DEF2(delitem2,"delitem4","viiiiiiiiirrr?"),
  23493. BUILDIN_DEF2(countitem,"countitem4","viiiiiiiirrr?"),
  23494. // Achievement System
  23495. BUILDIN_DEF(achievementinfo,"ii?"),
  23496. BUILDIN_DEF(achievementadd,"i?"),
  23497. BUILDIN_DEF(achievementremove,"i?"),
  23498. BUILDIN_DEF(achievementcomplete,"i?"),
  23499. BUILDIN_DEF(achievementexists,"i?"),
  23500. BUILDIN_DEF(achievementupdate,"iii?"),
  23501. BUILDIN_DEF(getequiprefinecost,"iii?"),
  23502. BUILDIN_DEF(refineui,"?"),
  23503. BUILDIN_DEF2(round, "round", "ii"),
  23504. BUILDIN_DEF2(round, "ceil", "ii"),
  23505. BUILDIN_DEF2(round, "floor", "ii"),
  23506. BUILDIN_DEF(getequiptradability, "i?"),
  23507. BUILDIN_DEF(mail, "isss*"),
  23508. BUILDIN_DEF(open_roulette,"?"),
  23509. BUILDIN_DEF(identifyall,"??"),
  23510. BUILDIN_DEF(is_guild_leader,"?"),
  23511. BUILDIN_DEF(is_party_leader,"?"),
  23512. BUILDIN_DEF(camerainfo,"iii?"),
  23513. BUILDIN_DEF(achievement_condition,"i"),
  23514. BUILDIN_DEF(getinstancevar,"ri"),
  23515. BUILDIN_DEF2_DEPRECATED(getinstancevar, "getvariableofinstance","ri", "2021-12-13"),
  23516. BUILDIN_DEF(convertpcinfo,"vi"),
  23517. BUILDIN_DEF(isnpccloaked, "??"),
  23518. BUILDIN_DEF(rentalcountitem, "v?"),
  23519. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  23520. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  23521. BUILDIN_DEF2(rentalcountitem, "rentalcountitem4", "viiiiiiiirrr?"),
  23522. BUILDIN_DEF(getenchantgrade, "??"),
  23523. BUILDIN_DEF(mob_setidleevent, "is"),
  23524. BUILDIN_DEF(setinstancevar,"rvi"),
  23525. BUILDIN_DEF(openstylist, "?"),
  23526. // Navigation Generation System
  23527. BUILDIN_DEF(naviregisterwarp, "ssii"),
  23528. BUILDIN_DEF(navihide, ""),
  23529. BUILDIN_DEF(getitempos,""),
  23530. BUILDIN_DEF(laphine_synthesis, "?"),
  23531. BUILDIN_DEF(laphine_upgrade, ""),
  23532. BUILDIN_DEF(randomoptgroup,"i"),
  23533. BUILDIN_DEF(open_quest_ui, "??"),
  23534. BUILDIN_DEF(openbank,"?"),
  23535. BUILDIN_DEF(getbaseexp_ratio, "i??"),
  23536. BUILDIN_DEF(getjobexp_ratio, "i??"),
  23537. BUILDIN_DEF(enchantgradeui, "?" ),
  23538. BUILDIN_DEF(set_reputation_points, "ii?"),
  23539. BUILDIN_DEF(get_reputation_points, "i?"),
  23540. BUILDIN_DEF(item_reform, "??"),
  23541. BUILDIN_DEF(item_enchant, "i?"),
  23542. BUILDIN_DEF(addfame, "i?"),
  23543. BUILDIN_DEF(getfame, "?"),
  23544. BUILDIN_DEF(getfamerank, "?"),
  23545. #include "../custom/script_def.inc"
  23546. {NULL,NULL,NULL},
  23547. };