script.cpp 709 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297
  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. };
  281. static jmp_buf error_jump;
  282. static char* error_msg;
  283. static const char* error_pos;
  284. static int error_report; // if the error should produce output
  285. // Used by disp_warning_message
  286. static const char* parser_current_src;
  287. static const char* parser_current_file;
  288. static int parser_current_line;
  289. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  290. // [Eoe / jA 1080, 1081, 1094, 1164]
  291. enum curly_type {
  292. TYPE_NULL = 0,
  293. TYPE_IF,
  294. TYPE_SWITCH,
  295. TYPE_WHILE,
  296. TYPE_FOR,
  297. TYPE_DO,
  298. TYPE_USERFUNC,
  299. TYPE_ARGLIST // function argument list
  300. };
  301. enum e_arglist
  302. {
  303. ARGLIST_UNDEFINED = 0,
  304. ARGLIST_NO_PAREN = 1,
  305. ARGLIST_PAREN = 2,
  306. };
  307. static struct {
  308. struct {
  309. enum curly_type type;
  310. int index;
  311. int count;
  312. int flag;
  313. struct linkdb_node *case_label;
  314. } curly[256]; // Information right parenthesis
  315. int curly_count; // The number of right brackets
  316. int index; // Number of the syntax used in the script
  317. } syntax;
  318. const char* parse_curly_close(const char* p);
  319. const char* parse_syntax_close(const char* p);
  320. const char* parse_syntax_close_sub(const char* p,int* flag);
  321. const char* parse_syntax(const char* p);
  322. static int parse_syntax_for_flag = 0;
  323. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  324. extern unsigned int current_equip_combo_pos;
  325. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  326. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  327. int potion_target = 0;
  328. unsigned int *generic_ui_array = NULL;
  329. unsigned int generic_ui_array_size = 0;
  330. c_op get_com(unsigned char *script,int *pos);
  331. int64 get_num(unsigned char *script,int *pos);
  332. typedef struct script_function {
  333. int (*func)(struct script_state *st);
  334. const char *name;
  335. const char *arg;
  336. const char *deprecated;
  337. } script_function;
  338. extern script_function buildin_func[];
  339. static struct linkdb_node *sleep_db; // int oid -> struct script_state *
  340. /*==========================================
  341. * (Only those needed) local declaration prototype
  342. *------------------------------------------*/
  343. const char* parse_subexpr(const char* p,int limit);
  344. int run_func(struct script_state *st);
  345. int script_instancegetid(struct script_state *st, e_instance_mode mode = IM_NONE);
  346. const char* script_op2name(int op)
  347. {
  348. #define RETURN_OP_NAME(type) case type: return #type
  349. switch( op )
  350. {
  351. RETURN_OP_NAME(C_NOP);
  352. RETURN_OP_NAME(C_POS);
  353. RETURN_OP_NAME(C_INT);
  354. RETURN_OP_NAME(C_PARAM);
  355. RETURN_OP_NAME(C_FUNC);
  356. RETURN_OP_NAME(C_STR);
  357. RETURN_OP_NAME(C_CONSTSTR);
  358. RETURN_OP_NAME(C_ARG);
  359. RETURN_OP_NAME(C_NAME);
  360. RETURN_OP_NAME(C_EOL);
  361. RETURN_OP_NAME(C_RETINFO);
  362. RETURN_OP_NAME(C_USERFUNC);
  363. RETURN_OP_NAME(C_USERFUNC_POS);
  364. RETURN_OP_NAME(C_REF);
  365. // operators
  366. RETURN_OP_NAME(C_OP3);
  367. RETURN_OP_NAME(C_LOR);
  368. RETURN_OP_NAME(C_LAND);
  369. RETURN_OP_NAME(C_LE);
  370. RETURN_OP_NAME(C_LT);
  371. RETURN_OP_NAME(C_GE);
  372. RETURN_OP_NAME(C_GT);
  373. RETURN_OP_NAME(C_EQ);
  374. RETURN_OP_NAME(C_NE);
  375. RETURN_OP_NAME(C_XOR);
  376. RETURN_OP_NAME(C_OR);
  377. RETURN_OP_NAME(C_AND);
  378. RETURN_OP_NAME(C_ADD);
  379. RETURN_OP_NAME(C_SUB);
  380. RETURN_OP_NAME(C_MUL);
  381. RETURN_OP_NAME(C_DIV);
  382. RETURN_OP_NAME(C_MOD);
  383. RETURN_OP_NAME(C_NEG);
  384. RETURN_OP_NAME(C_LNOT);
  385. RETURN_OP_NAME(C_NOT);
  386. RETURN_OP_NAME(C_R_SHIFT);
  387. RETURN_OP_NAME(C_L_SHIFT);
  388. RETURN_OP_NAME(C_ADD_POST);
  389. RETURN_OP_NAME(C_SUB_POST);
  390. RETURN_OP_NAME(C_ADD_PRE);
  391. RETURN_OP_NAME(C_SUB_PRE);
  392. default:
  393. ShowDebug("script_op2name: unexpected op=%d\n", op);
  394. return "???";
  395. }
  396. #undef RETURN_OP_NAME
  397. }
  398. #ifdef DEBUG_DUMP_STACK
  399. static void script_dump_stack(struct script_state* st)
  400. {
  401. int i;
  402. ShowMessage("\tstart = %d\n", st->start);
  403. ShowMessage("\tend = %d\n", st->end);
  404. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  405. ShowMessage("\tsp = %d\n", st->stack->sp);
  406. for( i = 0; i < st->stack->sp; ++i )
  407. {
  408. struct script_data* data = &st->stack->stack_data[i];
  409. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  410. switch( data->type )
  411. {
  412. case C_INT:
  413. case C_POS:
  414. ShowMessage(" %d\n", data->u.num);
  415. break;
  416. case C_STR:
  417. case C_CONSTSTR:
  418. ShowMessage(" \"%s\"\n", data->u.str);
  419. break;
  420. case C_NAME:
  421. 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));
  422. break;
  423. case C_RETINFO:
  424. {
  425. struct script_retinfo* ri = data->u.ri;
  426. 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);
  427. }
  428. break;
  429. default:
  430. ShowMessage("\n");
  431. break;
  432. }
  433. }
  434. }
  435. #endif
  436. /// Reports on the console the src of a script error.
  437. static void script_reportsrc(struct script_state *st)
  438. {
  439. if( st->oid == 0 )
  440. return; //Can't report source.
  441. struct block_list* bl = map_id2bl(st->oid);
  442. if (!bl)
  443. return;
  444. switch( bl->type ) {
  445. case BL_NPC: {
  446. struct npc_data* nd = (struct npc_data*)bl;
  447. if( bl->m >= 0 )
  448. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", nd->name, map_mapid2mapname(bl->m), bl->x, bl->y);
  449. else
  450. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", nd->name);
  451. ShowDebug( "Source (NPC): %s is located in: %s\n", nd->name, nd->path );
  452. } break;
  453. default:
  454. if( bl->m >= 0 )
  455. 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);
  456. else
  457. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  458. break;
  459. }
  460. }
  461. /// Reports on the console information about the script data.
  462. static void script_reportdata(struct script_data* data)
  463. {
  464. if( data == NULL )
  465. return;
  466. switch( data->type ) {
  467. case C_NOP:// no value
  468. ShowDebug("Data: nothing (nil)\n");
  469. break;
  470. case C_INT:// number
  471. ShowDebug("Data: number value=%" PRId64 "\n", data->u.num);
  472. break;
  473. case C_STR:
  474. case C_CONSTSTR:// string
  475. if( data->u.str ) {
  476. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  477. } else {
  478. ShowDebug("Data: string value=NULL\n");
  479. }
  480. break;
  481. case C_NAME:// reference
  482. if( reference_tovariable(data) ) {// variable
  483. const char* name = reference_getname(data);
  484. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  485. } else if( reference_toconstant(data) ) {// constant
  486. ShowDebug("Data: constant name='%s' value=%" PRId64 "\n", reference_getname(data), reference_getconstant(data));
  487. } else if( reference_toparam(data) ) {// param
  488. ShowDebug("Data: param name='%s' type=%" PRId64 "\n", reference_getname(data), reference_getparamtype(data));
  489. } else {// ???
  490. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  491. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  492. }
  493. break;
  494. case C_POS:// label
  495. ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num);
  496. break;
  497. default:
  498. ShowDebug("Data: %s\n", script_op2name(data->type));
  499. break;
  500. }
  501. }
  502. /// Reports on the console information about the current built-in function.
  503. static void script_reportfunc(struct script_state* st)
  504. {
  505. int params, id;
  506. struct script_data* data;
  507. if( !script_hasdata(st,0) )
  508. {// no stack
  509. return;
  510. }
  511. data = script_getdata(st,0);
  512. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  513. {// script currently not executing a built-in function or corrupt stack
  514. return;
  515. }
  516. id = reference_getid(data);
  517. params = script_lastdata(st)-1;
  518. if( params > 0 )
  519. {
  520. int i;
  521. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  522. for( i = 2; i <= script_lastdata(st); i++ )
  523. {
  524. script_reportdata(script_getdata(st,i));
  525. }
  526. }
  527. else
  528. {
  529. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  530. }
  531. }
  532. /*==========================================
  533. * Output error message
  534. *------------------------------------------*/
  535. static void disp_error_message2(const char *mes,const char *pos,int report)
  536. {
  537. error_msg = aStrdup(mes);
  538. error_pos = pos;
  539. error_report = report;
  540. longjmp( error_jump, 1 );
  541. }
  542. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  543. static void disp_warning_message(const char *mes, const char *pos) {
  544. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  545. }
  546. /// Checks event parameter validity
  547. static void check_event(struct script_state *st, const char *evt)
  548. {
  549. if( evt && evt[0] && !stristr(evt, "::On") )
  550. {
  551. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  552. script_reportsrc(st);
  553. }
  554. }
  555. /*==========================================
  556. * Hashes the input string
  557. *------------------------------------------*/
  558. static unsigned int calc_hash(const char* p)
  559. {
  560. unsigned int h;
  561. #if defined(SCRIPT_HASH_DJB2)
  562. h = 5381;
  563. while( *p ) // hash*33 + c
  564. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  565. #elif defined(SCRIPT_HASH_SDBM)
  566. h = 0;
  567. while( *p ) // hash*65599 + c
  568. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  569. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  570. h = 0;
  571. while( *p ){
  572. unsigned int g;
  573. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  574. g = h & 0xF0000000;
  575. if( g )
  576. {
  577. h ^= g >> 24;
  578. h &= ~g;
  579. }
  580. }
  581. #else // athena hash
  582. h = 0;
  583. while( *p )
  584. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  585. #endif
  586. return h % SCRIPT_HASH_SIZE;
  587. }
  588. bool script_check_RegistryVariableLength(int pType, const char *val, size_t* vlen)
  589. {
  590. size_t len = strlen(val);
  591. if (vlen)
  592. *vlen = len;
  593. switch (pType) {
  594. case 0:
  595. return (len < 33); // key check
  596. case 1:
  597. return (len < 255); // value check
  598. default:
  599. return false;
  600. }
  601. }
  602. /*==========================================
  603. * str_data manipulation functions
  604. *------------------------------------------*/
  605. /// Looks up string using the provided id.
  606. const char* get_str(int id)
  607. {
  608. Assert( id >= LABEL_START && id < str_size );
  609. return str_buf+str_data[id].str;
  610. }
  611. /// Returns the uid of the string, or -1.
  612. static int search_str(const char* p)
  613. {
  614. int i;
  615. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  616. if( strcasecmp(get_str(i),p) == 0 )
  617. return i;
  618. return -1;
  619. }
  620. /// Stores a copy of the string and returns its id.
  621. /// If an identical string is already present, returns its id instead.
  622. int add_str(const char* p)
  623. {
  624. int h;
  625. int len;
  626. h = calc_hash(p);
  627. if( str_hash[h] == 0 )
  628. {// empty bucket, add new node here
  629. str_hash[h] = str_num;
  630. }
  631. else
  632. {// scan for end of list, or occurence of identical string
  633. int i;
  634. for( i = str_hash[h]; ; i = str_data[i].next )
  635. {
  636. if( strcasecmp(get_str(i),p) == 0 )
  637. return i; // string already in list
  638. if( str_data[i].next == 0 )
  639. break; // reached the end
  640. }
  641. // append node to end of list
  642. str_data[i].next = str_num;
  643. }
  644. // grow list if neccessary
  645. if( str_num >= str_data_size )
  646. {
  647. str_data_size += 128;
  648. RECREATE(str_data,struct str_data_struct,str_data_size);
  649. memset(str_data + (str_data_size - 128), '\0', 128);
  650. }
  651. len=(int)strlen(p);
  652. // grow string buffer if neccessary
  653. while( str_pos+len+1 >= str_size )
  654. {
  655. str_size += 256;
  656. RECREATE(str_buf,char,str_size);
  657. memset(str_buf + (str_size - 256), '\0', 256);
  658. }
  659. safestrncpy(str_buf+str_pos, p, len+1);
  660. str_data[str_num].type = C_NOP;
  661. str_data[str_num].str = str_pos;
  662. str_data[str_num].next = 0;
  663. str_data[str_num].func = NULL;
  664. str_data[str_num].backpatch = -1;
  665. str_data[str_num].label = -1;
  666. str_pos += len+1;
  667. return str_num++;
  668. }
  669. /// Appends 1 byte to the script buffer.
  670. static void add_scriptb(uint8 a)
  671. {
  672. if( script_pos+1 >= script_size )
  673. {
  674. script_size += SCRIPT_BLOCK_SIZE;
  675. RECREATE(script_buf,unsigned char,script_size);
  676. }
  677. script_buf[script_pos++] = a;
  678. }
  679. /// Appends a c_op value to the script buffer.
  680. /// The value is variable-length encoded into 8-bit blocks.
  681. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  682. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  683. static void add_scriptc(int a)
  684. {
  685. while( a >= 0x40 )
  686. {
  687. add_scriptb((a&0x3f)|0x40);
  688. a = (a - 0x40) >> 6;
  689. }
  690. add_scriptb(a);
  691. }
  692. /// Appends an integer value to the script buffer.
  693. /// The value is variable-length encoded into 8-bit blocks.
  694. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  695. /// All blocks hold 6 bits of data.
  696. static void add_scripti(int64 a){
  697. while( a > 0x3f ){
  698. add_scriptb((a & (int64)0x3f)|(int64)0xc0);
  699. a >>= 6;
  700. }
  701. add_scriptb(((a & (int64)0x3f)|(int64)0x80));
  702. }
  703. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  704. ///
  705. /// @param l The id of the str_data entry
  706. // Maximum up to 16M
  707. static void add_scriptl(int l)
  708. {
  709. int backpatch = str_data[l].backpatch;
  710. switch(str_data[l].type){
  711. case C_POS:
  712. case C_USERFUNC_POS:
  713. add_scriptc(C_POS);
  714. add_scriptb(str_data[l].label);
  715. add_scriptb(str_data[l].label>>8);
  716. add_scriptb(str_data[l].label>>16);
  717. break;
  718. case C_NOP:
  719. case C_USERFUNC:
  720. // Embedded data backpatch there is a possibility of label
  721. add_scriptc(C_NAME);
  722. str_data[l].backpatch = script_pos;
  723. add_scriptb(backpatch);
  724. add_scriptb(backpatch>>8);
  725. add_scriptb(backpatch>>16);
  726. break;
  727. case C_INT:
  728. add_scripti(std::abs(str_data[l].val));
  729. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  730. add_scriptc(C_NEG);
  731. break;
  732. default: // assume C_NAME
  733. add_scriptc(C_NAME);
  734. add_scriptb(l);
  735. add_scriptb(l>>8);
  736. add_scriptb(l>>16);
  737. break;
  738. }
  739. }
  740. /*==========================================
  741. * Resolve the label
  742. *------------------------------------------*/
  743. void set_label(int l,int pos, const char* script_pos_cur)
  744. {
  745. int i;
  746. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  747. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  748. disp_error_message("set_label: invalid label name",script_pos_cur);
  749. return;
  750. }
  751. if(str_data[l].label!=-1){
  752. disp_error_message("set_label: dup label ",script_pos_cur);
  753. return;
  754. }
  755. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  756. str_data[l].label=pos;
  757. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  758. int next=GETVALUE(script_buf,i);
  759. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  760. SETVALUE(script_buf,i,pos);
  761. i=next;
  762. }
  763. }
  764. /// Skips spaces and/or comments.
  765. const char* skip_space(const char* p)
  766. {
  767. if( p == NULL )
  768. return NULL;
  769. for(;;)
  770. {
  771. while( ISSPACE(*p) )
  772. ++p;
  773. if( *p == '/' && p[1] == '/' )
  774. {// line comment
  775. while(*p && *p!='\n')
  776. ++p;
  777. }
  778. else if( *p == '/' && p[1] == '*' )
  779. {// block comment
  780. p += 2;
  781. for(;;)
  782. {
  783. if( *p == '\0' ) {
  784. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p);
  785. return p;
  786. }
  787. if( *p == '*' && p[1] == '/' )
  788. {// end of block comment
  789. p += 2;
  790. break;
  791. }
  792. ++p;
  793. }
  794. }
  795. else
  796. break;
  797. }
  798. return p;
  799. }
  800. /// Skips a word.
  801. /// A word consists of undercores and/or alphanumeric characters,
  802. /// and valid variable prefixes/postfixes.
  803. static const char* skip_word(const char* p)
  804. {
  805. // prefix
  806. switch( *p ) {
  807. case '@':// temporary char variable
  808. ++p; break;
  809. case '#':// account variable
  810. p += ( p[1] == '#' ? 2 : 1 ); break;
  811. case '\'':// instance variable
  812. ++p; break;
  813. case '.':// npc variable
  814. p += ( p[1] == '@' ? 2 : 1 ); break;
  815. case '$':// global variable
  816. p += ( p[1] == '@' ? 2 : 1 ); break;
  817. }
  818. while( ISALNUM(*p) || *p == '_' )
  819. ++p;
  820. // postfix
  821. if( *p == '$' )// string
  822. p++;
  823. return p;
  824. }
  825. /// Adds a word to str_data.
  826. /// @see skip_word
  827. /// @see add_str
  828. static int add_word(const char* p)
  829. {
  830. char* word;
  831. int len;
  832. int i;
  833. // Check for a word
  834. len = skip_word(p) - p;
  835. if( len == 0 )
  836. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  837. // Duplicate the word
  838. word = (char*)aMalloc(len+1);
  839. memcpy(word, p, len);
  840. word[len] = 0;
  841. // add the word
  842. i = add_str(word);
  843. aFree(word);
  844. return i;
  845. }
  846. /// Parses a function call.
  847. /// The argument list can have parenthesis or not.
  848. /// The number of arguments is checked.
  849. static
  850. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  851. {
  852. const char* p2;
  853. const char* arg=NULL;
  854. int func;
  855. func = add_word(p);
  856. if( str_data[func].type == C_FUNC ){
  857. // buildin function
  858. add_scriptl(func);
  859. add_scriptc(C_ARG);
  860. arg = buildin_func[str_data[func].val].arg;
  861. #if defined(SCRIPT_COMMAND_DEPRECATION)
  862. if( str_data[func].deprecated ){
  863. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  864. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  865. }
  866. #endif
  867. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  868. // script defined function
  869. add_scriptl(buildin_callsub_ref);
  870. add_scriptc(C_ARG);
  871. add_scriptl(func);
  872. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  873. if( *arg == 0 )
  874. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  875. if( *arg != '*' )
  876. ++arg; // count func as argument
  877. } else {
  878. const char* name = get_str(func);
  879. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  880. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  881. } else {;
  882. add_scriptl(buildin_callfunc_ref);
  883. add_scriptc(C_ARG);
  884. add_scriptc(C_STR);
  885. while( *name ) add_scriptb(*name ++);
  886. add_scriptb(0);
  887. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  888. if( *arg != '*' ) ++ arg;
  889. }
  890. }
  891. p = skip_word(p);
  892. p = skip_space(p);
  893. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  894. syntax.curly[syntax.curly_count].count = 0;
  895. if( *p == ';' )
  896. {// <func name> ';'
  897. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  898. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  899. {// <func name> '(' ')'
  900. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  901. p = p2;
  902. /*
  903. } else if( 0 && require_paren && *p != '(' )
  904. {// <func name>
  905. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  906. */
  907. } else
  908. {// <func name> <arg list>
  909. if( require_paren ){
  910. if( *p != '(' )
  911. disp_error_message("need '('",p);
  912. ++p; // skip '('
  913. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  914. } else if( *p == '(' ){
  915. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  916. } else {
  917. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  918. }
  919. ++syntax.curly_count;
  920. while( *arg ) {
  921. p2=parse_subexpr(p,-1);
  922. if( p == p2 )
  923. break; // not an argument
  924. if( *arg != '*' )
  925. ++arg; // next argument
  926. p=skip_space(p2);
  927. if( *arg == 0 || *p != ',' )
  928. break; // no more arguments
  929. ++p; // skip comma
  930. }
  931. --syntax.curly_count;
  932. }
  933. if( *arg && *arg != '?' && *arg != '*' )
  934. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  935. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  936. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  937. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  938. if( *p != ')' )
  939. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  940. ++p;
  941. }
  942. add_scriptc(C_FUNC);
  943. return p;
  944. }
  945. /// Processes end of logical script line.
  946. /// @param first When true, only fix up scheduling data is initialized
  947. /// @param p Script position for error reporting in set_label
  948. static void parse_nextline(bool first, const char* p)
  949. {
  950. if( !first )
  951. {
  952. add_scriptc(C_EOL); // mark end of line for stack cleanup
  953. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  954. }
  955. // initialize data for new '-' label fix up scheduling
  956. str_data[LABEL_NEXTLINE].type = C_NOP;
  957. str_data[LABEL_NEXTLINE].backpatch = -1;
  958. str_data[LABEL_NEXTLINE].label = -1;
  959. }
  960. /**
  961. * Pushes a variable into stack, processing its array index if needed.
  962. * @see parse_variable
  963. */
  964. void parse_variable_sub_push(int word, const char *p2)
  965. {
  966. if( p2 ) { // Process the variable index
  967. const char *p3 = NULL;
  968. // Push the getelementofarray method into the stack
  969. add_scriptl(buildin_getelementofarray_ref);
  970. add_scriptc(C_ARG);
  971. add_scriptl(word);
  972. // Process the sub-expression for this assignment
  973. p3 = parse_subexpr(p2 + 1, 1);
  974. p3 = skip_space(p3);
  975. if( *p3 != ']' ) // Closing parenthesis is required for this script
  976. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  977. // Push the closing function stack operator onto the stack
  978. add_scriptc(C_FUNC);
  979. p3++;
  980. } else // No array index, simply push the variable or value onto the stack
  981. add_scriptl(word);
  982. }
  983. /// Parse a variable assignment using the direct equals operator
  984. /// @param p script position where the function should run from
  985. /// @return NULL if not a variable assignment, the new position otherwise
  986. const char* parse_variable(const char* p) {
  987. int word;
  988. c_op type = C_NOP;
  989. const char *p2 = NULL;
  990. const char *var = p;
  991. if( p[0] == '+' && p[1] == '+' ){
  992. type = C_ADD_PRE; // pre ++
  993. }else if( p[0] == '-' && p[1] == '-' ){
  994. type = C_SUB_PRE; // pre --
  995. }
  996. if( type != C_NOP ){
  997. var = p = skip_space(&p[2]);
  998. }
  999. // skip the variable where applicable
  1000. p = skip_word(p);
  1001. p = skip_space(p);
  1002. if( p == NULL ) {// end of the line or invalid buffer
  1003. return NULL;
  1004. }
  1005. if( *p == '[' ) {// array variable so process the array as appropriate
  1006. int i,j;
  1007. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1008. if( *p ++ == ']' && --(j) == 0 ) break;
  1009. if( *p == '[' ) ++ j;
  1010. }
  1011. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1012. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1013. }
  1014. }
  1015. if( type == C_NOP ){
  1016. if( p[0] == '=' && p[1] != '=' ){
  1017. type = C_EQ; // =
  1018. }else if( p[0] == '+' && p[1] == '=' ){
  1019. type = C_ADD; // +=
  1020. }else if( p[0] == '-' && p[1] == '=' ){
  1021. type = C_SUB; // -=
  1022. }else if( p[0] == '^' && p[1] == '=' ){
  1023. type = C_XOR; // ^=
  1024. }else if( p[0] == '|' && p[1] == '=' ){
  1025. type = C_OR; // |=
  1026. }else if( p[0] == '&' && p[1] == '=' ){
  1027. type = C_AND; // &=
  1028. }else if( p[0] == '*' && p[1] == '=' ){
  1029. type = C_MUL; // *=
  1030. }else if( p[0] == '/' && p[1] == '=' ){
  1031. type = C_DIV; // /=
  1032. }else if( p[0] == '%' && p[1] == '=' ){
  1033. type = C_MOD; // %=
  1034. }else if( p[0] == '~' && p[1] == '=' ){
  1035. type = C_NOT; // ~=
  1036. }else if( p[0] == '+' && p[1] == '+' ){
  1037. type = C_ADD_POST; // post ++
  1038. }else if( p[0] == '-' && p[1] == '-' ){
  1039. type = C_SUB_POST; // post --
  1040. }else if( p[0] == '<' && p[1] == '<' && p[2] == '=' ){
  1041. type = C_L_SHIFT; // <<=
  1042. }else if( p[0] == '>' && p[1] == '>' && p[2] == '=' ){
  1043. type = C_R_SHIFT; // >>=
  1044. }else{
  1045. // failed to find a matching operator combination so invalid
  1046. return nullptr;
  1047. }
  1048. }
  1049. switch( type ) {
  1050. case C_ADD_PRE: // pre ++
  1051. case C_SUB_PRE: // pre --
  1052. // Nothing more to skip
  1053. break;
  1054. case C_EQ: {// incremental modifier
  1055. p = skip_space( &p[1] );
  1056. }
  1057. break;
  1058. case C_L_SHIFT:
  1059. case C_R_SHIFT: {// left or right shift modifier
  1060. p = skip_space( &p[3] );
  1061. }
  1062. break;
  1063. default: {// normal incremental command
  1064. p = skip_space( &p[2] );
  1065. }
  1066. }
  1067. if( p == NULL ) {// end of line or invalid buffer
  1068. return NULL;
  1069. }
  1070. // push the set function onto the stack
  1071. add_scriptl(buildin_set_ref);
  1072. add_scriptc(C_ARG);
  1073. // always append parenthesis to avoid errors
  1074. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1075. syntax.curly[syntax.curly_count].count = 0;
  1076. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1077. // increment the total curly count for the position in the script
  1078. ++ syntax.curly_count;
  1079. // parse the variable currently being modified
  1080. word = add_word(var);
  1081. 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
  1082. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1083. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1084. if( type != C_EQ )
  1085. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1086. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1087. add_scripti(1);
  1088. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1089. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1090. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1091. add_scripti(1);
  1092. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1093. } else { // Process the value as an expression
  1094. p = parse_subexpr(p, -1);
  1095. if( type != C_EQ )
  1096. {// push the type of modifier onto the stack
  1097. add_scriptc(type);
  1098. }
  1099. }
  1100. // decrement the curly count for the position within the script
  1101. -- syntax.curly_count;
  1102. // close the script by appending the function operator
  1103. add_scriptc(C_FUNC);
  1104. // push the buffer from the method
  1105. return p;
  1106. }
  1107. /*
  1108. * Checks whether the gives string is a number literal
  1109. *
  1110. * Mainly necessary to differentiate between number literals and NPC name
  1111. * constants, since several of those start with a digit.
  1112. *
  1113. * All this does is to check if the string begins with an optional + or - sign,
  1114. * followed by a hexadecimal or decimal number literal literal and is NOT
  1115. * followed by a underscore or letter.
  1116. *
  1117. * @author : Hercules.ws
  1118. * @param p Pointer to the string to check
  1119. * @return Whether the string is a number literal
  1120. */
  1121. bool is_number(const char *p) {
  1122. const char *np;
  1123. if (!p)
  1124. return false;
  1125. if (*p == '-' || *p == '+')
  1126. p++;
  1127. np = p;
  1128. if (*p == '0' && p[1] == 'x') {
  1129. p+=2;
  1130. np = p;
  1131. // Hexadecimal
  1132. while (ISXDIGIT(*np))
  1133. np++;
  1134. } else {
  1135. // Decimal
  1136. while (ISDIGIT(*np))
  1137. np++;
  1138. }
  1139. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1140. return true;
  1141. return false;
  1142. }
  1143. /*==========================================
  1144. * Analysis section
  1145. *------------------------------------------*/
  1146. const char* parse_simpleexpr(const char *p)
  1147. {
  1148. int64 i;
  1149. p=skip_space(p);
  1150. if(*p==';' || *p==',')
  1151. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1152. if(*p=='('){
  1153. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1154. ++syntax.curly[i].count;
  1155. p=parse_subexpr(p+1,-1);
  1156. p=skip_space(p);
  1157. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1158. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1159. ){
  1160. if( *p == ',' ){
  1161. syntax.curly[i].flag = ARGLIST_PAREN;
  1162. return p;
  1163. } else
  1164. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1165. }
  1166. if( *p != ')' )
  1167. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1168. ++p;
  1169. } else if(is_number(p)) {
  1170. char *np;
  1171. bool error;
  1172. // Skip leading zeroes
  1173. while(*p == '0' && ISDIGIT(p[1])) p++;
  1174. errno = 0;
  1175. i=strtoll(p,&np,0);
  1176. error = (errno == ERANGE);
  1177. if( i < SCRIPT_INT_MIN || ( error && i == INT64_MIN ) ){
  1178. i = SCRIPT_INT_MIN;
  1179. disp_warning_message( "parse_simpleexpr: underflow detected, capping value to SCRIPT_INT_MIN", p );
  1180. }else if( i > SCRIPT_INT_MAX || ( error && i == INT64_MAX ) ){
  1181. i = SCRIPT_INT_MAX;
  1182. disp_warning_message( "parse_simpleexpr: overflow detected, capping value to SCRIPT_INT_MAX", p );
  1183. }
  1184. add_scripti(i);
  1185. p=np;
  1186. } else if(*p=='"'){
  1187. add_scriptc(C_STR);
  1188. p++;
  1189. while( *p && *p != '"' ){
  1190. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1191. {
  1192. char buf[8];
  1193. size_t len = skip_escaped_c(p) - p;
  1194. size_t n = sv_unescape_c(buf, p, len);
  1195. if( n != 1 )
  1196. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1197. p += len;
  1198. add_scriptb(*buf);
  1199. continue;
  1200. }
  1201. else if( *p == '\n' )
  1202. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1203. add_scriptb(*p++);
  1204. }
  1205. if(!*p)
  1206. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1207. add_scriptb(0);
  1208. p++; //'"'
  1209. } else {
  1210. int l;
  1211. const char* pv;
  1212. // label , register , function etc
  1213. if(skip_word(p)==p)
  1214. disp_error_message("parse_simpleexpr: unexpected character",p);
  1215. l=add_word(p);
  1216. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1217. return parse_callfunc(p,1,0);
  1218. else {
  1219. const char* name = get_str(l);
  1220. if( strdb_get(userfunc_db,name) != NULL ) {
  1221. return parse_callfunc(p,1,1);
  1222. }
  1223. }
  1224. if( (pv = parse_variable(p)) )
  1225. {// successfully processed a variable assignment
  1226. return pv;
  1227. }
  1228. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1229. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1230. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1231. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1232. if (str_data[l].name)
  1233. ShowWarning( "Please use '%s' instead!\n", str_data[l].name );
  1234. disp_warning_message("parse_simpleexpr: deprecated constant", p);
  1235. }
  1236. #endif
  1237. p=skip_word(p);
  1238. if( *p == '[' ){
  1239. // array(name[i] => getelementofarray(name,i) )
  1240. add_scriptl(buildin_getelementofarray_ref);
  1241. add_scriptc(C_ARG);
  1242. add_scriptl(l);
  1243. p=parse_subexpr(p+1,-1);
  1244. p=skip_space(p);
  1245. if( *p != ']' )
  1246. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1247. ++p;
  1248. add_scriptc(C_FUNC);
  1249. }else
  1250. add_scriptl(l);
  1251. }
  1252. return p;
  1253. }
  1254. /*==========================================
  1255. * Analysis of the expression
  1256. *------------------------------------------*/
  1257. const char* parse_subexpr(const char* p,int limit)
  1258. {
  1259. int op,opl,len;
  1260. p=skip_space(p);
  1261. if( *p == '-' ){
  1262. const char* tmpp = skip_space(p+1);
  1263. if( *tmpp == ';' || *tmpp == ',' ){
  1264. add_scriptl(LABEL_NEXTLINE);
  1265. p++;
  1266. return p;
  1267. }
  1268. }
  1269. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1270. p = parse_variable(p);
  1271. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1272. p = parse_subexpr(p + 1, 11);
  1273. add_scriptc(op);
  1274. } else
  1275. p = parse_simpleexpr(p);
  1276. p = skip_space(p);
  1277. while((
  1278. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1279. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1280. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1281. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1282. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1283. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1284. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1285. (op=C_AND,opl=5,len=1,*p=='&') ||
  1286. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1287. (op=C_OR,opl=3,len=1,*p=='|') ||
  1288. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1289. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1290. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1291. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1292. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1293. (op=C_GT,opl=7,len=1,*p=='>') ||
  1294. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1295. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1296. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1297. p+=len;
  1298. if(op == C_OP3) {
  1299. p=parse_subexpr(p,-1);
  1300. p=skip_space(p);
  1301. if( *(p++) != ':')
  1302. disp_error_message("parse_subexpr: expected ':'", p-1);
  1303. p=parse_subexpr(p,-1);
  1304. } else {
  1305. p=parse_subexpr(p,opl);
  1306. }
  1307. add_scriptc(op);
  1308. p=skip_space(p);
  1309. }
  1310. return p; /* return first untreated operator */
  1311. }
  1312. /*==========================================
  1313. * Evaluation of the expression
  1314. *------------------------------------------*/
  1315. const char* parse_expr(const char *p)
  1316. {
  1317. switch(*p){
  1318. case ')': case ';': case ':': case '[': case ']':
  1319. case '}':
  1320. disp_error_message("parse_expr: unexpected character",p);
  1321. }
  1322. p=parse_subexpr(p,-1);
  1323. return p;
  1324. }
  1325. /*==========================================
  1326. * Analysis of the line
  1327. *------------------------------------------*/
  1328. const char* parse_line(const char* p)
  1329. {
  1330. const char* p2;
  1331. p=skip_space(p);
  1332. if(*p==';') {
  1333. //Close decision for if(); for(); while();
  1334. p = parse_syntax_close(p + 1);
  1335. return p;
  1336. }
  1337. if(*p==')' && parse_syntax_for_flag)
  1338. return p+1;
  1339. p = skip_space(p);
  1340. if(p[0] == '{') {
  1341. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1342. syntax.curly[syntax.curly_count].count = -1;
  1343. syntax.curly[syntax.curly_count].index = -1;
  1344. syntax.curly_count++;
  1345. return p + 1;
  1346. } else if(p[0] == '}') {
  1347. return parse_curly_close(p);
  1348. }
  1349. // Syntax-related processing
  1350. p2 = parse_syntax(p);
  1351. if(p2 != NULL)
  1352. return p2;
  1353. // attempt to process a variable assignment
  1354. p2 = parse_variable(p);
  1355. if( p2 != NULL )
  1356. {// variable assignment processed so leave the method
  1357. return parse_syntax_close(p2 + 1);
  1358. }
  1359. p = parse_callfunc(p,0,0);
  1360. p = skip_space(p);
  1361. if(parse_syntax_for_flag) {
  1362. if( *p != ')' )
  1363. disp_error_message("parse_line: expected ')'",p);
  1364. } else {
  1365. if( *p != ';' )
  1366. disp_error_message("parse_line: expected ';'",p);
  1367. }
  1368. //Binding decision for if(), for(), while()
  1369. p = parse_syntax_close(p+1);
  1370. return p;
  1371. }
  1372. // { ... } Closing process
  1373. const char* parse_curly_close(const char* p)
  1374. {
  1375. if(syntax.curly_count <= 0) {
  1376. disp_error_message("parse_curly_close: unexpected string",p);
  1377. return p + 1;
  1378. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1379. syntax.curly_count--;
  1380. //Close decision if, for , while
  1381. p = parse_syntax_close(p + 1);
  1382. return p;
  1383. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1384. //Closing switch()
  1385. int pos = syntax.curly_count-1;
  1386. char label[256];
  1387. int l;
  1388. // Remove temporary variables
  1389. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1390. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1391. parse_line(label);
  1392. syntax.curly_count--;
  1393. // Go to the end pointer unconditionally
  1394. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1395. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1396. parse_line(label);
  1397. syntax.curly_count--;
  1398. // You are here labeled
  1399. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1400. l=add_str(label);
  1401. set_label(l,script_pos, p);
  1402. if(syntax.curly[pos].flag) {
  1403. //Exists default
  1404. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1405. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1406. parse_line(label);
  1407. syntax.curly_count--;
  1408. }
  1409. // Label end
  1410. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1411. l=add_str(label);
  1412. set_label(l,script_pos, p);
  1413. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1414. syntax.curly_count--;
  1415. //Closing decision if, for , while
  1416. p = parse_syntax_close(p + 1);
  1417. return p;
  1418. } else {
  1419. disp_error_message("parse_curly_close: unexpected string",p);
  1420. return p + 1;
  1421. }
  1422. }
  1423. // Syntax-related processing
  1424. // break, case, continue, default, do, for, function,
  1425. // if, switch, while ? will handle this internally.
  1426. const char* parse_syntax(const char* p)
  1427. {
  1428. const char *p2 = skip_word(p);
  1429. switch(*p) {
  1430. case 'B':
  1431. case 'b':
  1432. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1433. // break Processing
  1434. char label[256];
  1435. int pos = syntax.curly_count - 1;
  1436. while(pos >= 0) {
  1437. if(syntax.curly[pos].type == TYPE_DO) {
  1438. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1439. break;
  1440. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1441. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1442. break;
  1443. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1444. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1445. break;
  1446. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1447. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1448. break;
  1449. }
  1450. pos--;
  1451. }
  1452. if(pos < 0) {
  1453. disp_error_message("parse_syntax: unexpected 'break'",p);
  1454. } else {
  1455. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1456. parse_line(label);
  1457. syntax.curly_count--;
  1458. }
  1459. p = skip_space(p2);
  1460. if(*p != ';')
  1461. disp_error_message("parse_syntax: expected ';'",p);
  1462. // Closing decision if, for , while
  1463. p = parse_syntax_close(p + 1);
  1464. return p;
  1465. }
  1466. break;
  1467. case 'c':
  1468. case 'C':
  1469. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1470. //Processing case
  1471. int pos = syntax.curly_count-1;
  1472. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1473. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1474. return p+1;
  1475. } else {
  1476. char label[256];
  1477. int l;
  1478. int64 v;
  1479. char *np;
  1480. if(syntax.curly[pos].count != 1) {
  1481. //Jump for FALLTHRU
  1482. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1483. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1484. parse_line(label);
  1485. syntax.curly_count--;
  1486. // You are here labeled
  1487. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1488. l=add_str(label);
  1489. set_label(l,script_pos, p);
  1490. }
  1491. //Decision statement switch
  1492. p = skip_space(p2);
  1493. if(p == p2) {
  1494. disp_error_message("parse_syntax: expected a space ' '",p);
  1495. }
  1496. // check whether case label is integer or not
  1497. if(is_number(p)) {
  1498. //Numeric value
  1499. v = (int)strtol(p,&np,0);
  1500. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1501. p++;
  1502. p = skip_word(p);
  1503. if(np != p)
  1504. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1505. } else {
  1506. //Check for constants
  1507. p2 = skip_word(p);
  1508. v = (int)(size_t) (p2-p); // length of word at p2
  1509. memcpy(label,p,static_cast<int>(v));
  1510. label[v]='\0';
  1511. if( !script_get_constant(label, &v) )
  1512. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1513. p = skip_word(p);
  1514. }
  1515. p = skip_space(p);
  1516. if(*p != ':')
  1517. disp_error_message("parse_syntax: expect ':'",p);
  1518. sprintf(label,"if(%" PRId64 " != $@__SW%x_VAL) goto __SW%x_%x;",
  1519. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1520. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1521. // Bad I do not parse twice
  1522. p2 = parse_line(label);
  1523. parse_line(p2);
  1524. syntax.curly_count--;
  1525. if(syntax.curly[pos].count != 1) {
  1526. // Label after the completion of FALLTHRU
  1527. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1528. l=add_str(label);
  1529. set_label(l,script_pos,p);
  1530. }
  1531. // check duplication of case label [Rayce]
  1532. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1533. disp_error_message("parse_syntax: dup 'case'",p);
  1534. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1535. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1536. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1537. parse_line(label);
  1538. syntax.curly_count--;
  1539. syntax.curly[pos].count++;
  1540. }
  1541. return p + 1;
  1542. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1543. // Processing continue
  1544. char label[256];
  1545. int pos = syntax.curly_count - 1;
  1546. while(pos >= 0) {
  1547. if(syntax.curly[pos].type == TYPE_DO) {
  1548. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1549. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1550. break;
  1551. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1552. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1553. break;
  1554. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1555. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1556. break;
  1557. }
  1558. pos--;
  1559. }
  1560. if(pos < 0) {
  1561. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1562. } else {
  1563. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1564. parse_line(label);
  1565. syntax.curly_count--;
  1566. }
  1567. p = skip_space(p2);
  1568. if(*p != ';')
  1569. disp_error_message("parse_syntax: expected ';'",p);
  1570. //Closing decision if, for , while
  1571. p = parse_syntax_close(p + 1);
  1572. return p;
  1573. }
  1574. break;
  1575. case 'd':
  1576. case 'D':
  1577. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1578. // Switch - default processing
  1579. int pos = syntax.curly_count-1;
  1580. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1581. disp_error_message("parse_syntax: unexpected 'default'",p);
  1582. } else if(syntax.curly[pos].flag) {
  1583. disp_error_message("parse_syntax: dup 'default'",p);
  1584. } else {
  1585. char label[256];
  1586. int l;
  1587. // Put the label location
  1588. p = skip_space(p2);
  1589. if(*p != ':') {
  1590. disp_error_message("parse_syntax: expected ':'",p);
  1591. }
  1592. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1593. l=add_str(label);
  1594. set_label(l,script_pos,p);
  1595. // Skip to the next link w/o condition
  1596. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1597. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1598. parse_line(label);
  1599. syntax.curly_count--;
  1600. // The default label
  1601. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1602. l=add_str(label);
  1603. set_label(l,script_pos,p);
  1604. syntax.curly[syntax.curly_count - 1].flag = 1;
  1605. syntax.curly[pos].count++;
  1606. }
  1607. return p + 1;
  1608. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1609. int l;
  1610. char label[256];
  1611. p=skip_space(p2);
  1612. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1613. syntax.curly[syntax.curly_count].count = 1;
  1614. syntax.curly[syntax.curly_count].index = syntax.index++;
  1615. syntax.curly[syntax.curly_count].flag = 0;
  1616. // Label of the (do) form here
  1617. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1618. l=add_str(label);
  1619. set_label(l,script_pos,p);
  1620. syntax.curly_count++;
  1621. return p;
  1622. }
  1623. break;
  1624. case 'f':
  1625. case 'F':
  1626. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1627. int l;
  1628. char label[256];
  1629. int pos = syntax.curly_count;
  1630. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1631. syntax.curly[syntax.curly_count].count = 1;
  1632. syntax.curly[syntax.curly_count].index = syntax.index++;
  1633. syntax.curly[syntax.curly_count].flag = 0;
  1634. syntax.curly_count++;
  1635. p=skip_space(p2);
  1636. if(*p != '(')
  1637. disp_error_message("parse_syntax: expected '('",p);
  1638. p++;
  1639. // Execute the initialization statement
  1640. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1641. p=parse_line(p);
  1642. syntax.curly_count--;
  1643. // Form the start of label decision
  1644. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1645. l=add_str(label);
  1646. set_label(l,script_pos,p);
  1647. p=skip_space(p);
  1648. if(*p == ';') {
  1649. // For (; Because the pattern of always true ;)
  1650. ;
  1651. } else {
  1652. // Skip to the end point if the condition is false
  1653. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1654. add_scriptl(add_str("jump_zero"));
  1655. add_scriptc(C_ARG);
  1656. p=parse_expr(p);
  1657. p=skip_space(p);
  1658. add_scriptl(add_str(label));
  1659. add_scriptc(C_FUNC);
  1660. }
  1661. if(*p != ';')
  1662. disp_error_message("parse_syntax: expected ';'",p);
  1663. p++;
  1664. // Skip to the beginning of the loop
  1665. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1666. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1667. parse_line(label);
  1668. syntax.curly_count--;
  1669. // Labels to form the next loop
  1670. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1671. l=add_str(label);
  1672. set_label(l,script_pos,p);
  1673. // Process the next time you enter the loop
  1674. // A ')' last for; flag to be treated as'
  1675. parse_syntax_for_flag = 1;
  1676. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1677. p=parse_line(p);
  1678. syntax.curly_count--;
  1679. parse_syntax_for_flag = 0;
  1680. // Skip to the determination process conditions
  1681. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1682. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1683. parse_line(label);
  1684. syntax.curly_count--;
  1685. // Loop start labeling
  1686. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1687. l=add_str(label);
  1688. set_label(l,script_pos,p);
  1689. return p;
  1690. }
  1691. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1692. {// internal script function
  1693. const char *func_name;
  1694. func_name = skip_space(p2);
  1695. p = skip_word(func_name);
  1696. if( p == func_name )
  1697. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1698. p2 = skip_space(p);
  1699. if( *p2 == ';' )
  1700. {// function <name> ;
  1701. // function declaration - just register the name
  1702. int l;
  1703. l = add_word(func_name);
  1704. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1705. str_data[l].type = C_USERFUNC;
  1706. else if( str_data[l].type == C_USERFUNC )
  1707. ; // already registered
  1708. else
  1709. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1710. // Close condition of if, for, while
  1711. p = parse_syntax_close(p2 + 1);
  1712. return p;
  1713. }
  1714. else if(*p2 == '{')
  1715. {// function <name> <line/block of code>
  1716. char label[256];
  1717. int l;
  1718. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1719. syntax.curly[syntax.curly_count].count = 1;
  1720. syntax.curly[syntax.curly_count].index = syntax.index++;
  1721. syntax.curly[syntax.curly_count].flag = 0;
  1722. ++syntax.curly_count;
  1723. // Jump over the function code
  1724. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1725. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1726. ++syntax.curly_count;
  1727. parse_line(label);
  1728. --syntax.curly_count;
  1729. // Set the position of the function (label)
  1730. l=add_word(func_name);
  1731. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1732. {
  1733. str_data[l].type = C_USERFUNC;
  1734. set_label(l, script_pos, p);
  1735. if( parse_options&SCRIPT_USE_LABEL_DB )
  1736. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1737. }
  1738. else
  1739. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1740. return skip_space(p);
  1741. }
  1742. else
  1743. {
  1744. disp_error_message("expect ';' or '{' at function syntax",p);
  1745. }
  1746. }
  1747. break;
  1748. case 'i':
  1749. case 'I':
  1750. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1751. // If process
  1752. char label[256];
  1753. p=skip_space(p2);
  1754. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1755. disp_error_message("need '('",p);
  1756. }
  1757. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1758. syntax.curly[syntax.curly_count].count = 1;
  1759. syntax.curly[syntax.curly_count].index = syntax.index++;
  1760. syntax.curly[syntax.curly_count].flag = 0;
  1761. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1762. syntax.curly_count++;
  1763. add_scriptl(add_str("jump_zero"));
  1764. add_scriptc(C_ARG);
  1765. p=parse_expr(p);
  1766. p=skip_space(p);
  1767. add_scriptl(add_str(label));
  1768. add_scriptc(C_FUNC);
  1769. return p;
  1770. }
  1771. break;
  1772. case 's':
  1773. case 'S':
  1774. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1775. // Processing of switch ()
  1776. char label[256];
  1777. p=skip_space(p2);
  1778. if(*p != '(') {
  1779. disp_error_message("need '('",p);
  1780. }
  1781. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1782. syntax.curly[syntax.curly_count].count = 1;
  1783. syntax.curly[syntax.curly_count].index = syntax.index++;
  1784. syntax.curly[syntax.curly_count].flag = 0;
  1785. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1786. syntax.curly_count++;
  1787. add_scriptl(add_str("set"));
  1788. add_scriptc(C_ARG);
  1789. add_scriptl(add_str(label));
  1790. p=parse_expr(p);
  1791. p=skip_space(p);
  1792. if(*p != '{') {
  1793. disp_error_message("parse_syntax: expected '{'",p);
  1794. }
  1795. add_scriptc(C_FUNC);
  1796. return p + 1;
  1797. }
  1798. break;
  1799. case 'w':
  1800. case 'W':
  1801. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1802. int l;
  1803. char label[256];
  1804. p=skip_space(p2);
  1805. if(*p != '(') {
  1806. disp_error_message("need '('",p);
  1807. }
  1808. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1809. syntax.curly[syntax.curly_count].count = 1;
  1810. syntax.curly[syntax.curly_count].index = syntax.index++;
  1811. syntax.curly[syntax.curly_count].flag = 0;
  1812. // Form the start of label decision
  1813. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1814. l=add_str(label);
  1815. set_label(l,script_pos,p);
  1816. // Skip to the end point if the condition is false
  1817. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1818. syntax.curly_count++;
  1819. add_scriptl(add_str("jump_zero"));
  1820. add_scriptc(C_ARG);
  1821. p=parse_expr(p);
  1822. p=skip_space(p);
  1823. add_scriptl(add_str(label));
  1824. add_scriptc(C_FUNC);
  1825. return p;
  1826. }
  1827. break;
  1828. }
  1829. return NULL;
  1830. }
  1831. const char* parse_syntax_close(const char *p) {
  1832. // If (...) for (...) hoge (); as to make sure closed closed once again
  1833. int flag;
  1834. do {
  1835. p = parse_syntax_close_sub(p,&flag);
  1836. } while(flag);
  1837. return p;
  1838. }
  1839. // Close judgment if, for, while, of do
  1840. // flag == 1 : closed
  1841. // flag == 0 : not closed
  1842. const char* parse_syntax_close_sub(const char* p,int* flag)
  1843. {
  1844. char label[256];
  1845. int pos = syntax.curly_count - 1;
  1846. int l;
  1847. *flag = 1;
  1848. if(syntax.curly_count <= 0) {
  1849. *flag = 0;
  1850. return p;
  1851. } else if(syntax.curly[pos].type == TYPE_IF) {
  1852. const char *bp = p;
  1853. const char *p2;
  1854. // if-block and else-block end is a new line
  1855. parse_nextline(false, p);
  1856. // Skip to the last location if
  1857. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1858. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1859. parse_line(label);
  1860. syntax.curly_count--;
  1861. // Put the label of the location
  1862. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1863. l=add_str(label);
  1864. set_label(l,script_pos,p);
  1865. syntax.curly[pos].count++;
  1866. p = skip_space(p);
  1867. p2 = skip_word(p);
  1868. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1869. // else or else - if
  1870. p = skip_space(p2);
  1871. p2 = skip_word(p);
  1872. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1873. // else - if
  1874. p=skip_space(p2);
  1875. if(*p != '(') {
  1876. disp_error_message("need '('",p);
  1877. }
  1878. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1879. add_scriptl(add_str("jump_zero"));
  1880. add_scriptc(C_ARG);
  1881. p=parse_expr(p);
  1882. p=skip_space(p);
  1883. add_scriptl(add_str(label));
  1884. add_scriptc(C_FUNC);
  1885. *flag = 0;
  1886. return p;
  1887. } else {
  1888. // else
  1889. if(!syntax.curly[pos].flag) {
  1890. syntax.curly[pos].flag = 1;
  1891. *flag = 0;
  1892. return p;
  1893. }
  1894. }
  1895. }
  1896. // Close if
  1897. syntax.curly_count--;
  1898. // Put the label of the final location
  1899. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1900. l=add_str(label);
  1901. set_label(l,script_pos,p);
  1902. if(syntax.curly[pos].flag == 1) {
  1903. // Because the position of the pointer is the same if not else for this
  1904. return bp;
  1905. }
  1906. return p;
  1907. } else if(syntax.curly[pos].type == TYPE_DO) {
  1908. int l2;
  1909. char label2[256];
  1910. const char *p2;
  1911. if(syntax.curly[pos].flag) {
  1912. // (Come here continue) to form the label here
  1913. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1914. l2=add_str(label2);
  1915. set_label(l2,script_pos,p);
  1916. }
  1917. // Skip to the end point if the condition is false
  1918. p = skip_space(p);
  1919. p2 = skip_word(p);
  1920. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1921. disp_error_message("parse_syntax: expected 'while'",p);
  1922. p = skip_space(p2);
  1923. if(*p != '(') {
  1924. disp_error_message("need '('",p);
  1925. }
  1926. // do-block end is a new line
  1927. parse_nextline(false, p);
  1928. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1929. add_scriptl(add_str("jump_zero"));
  1930. add_scriptc(C_ARG);
  1931. p=parse_expr(p);
  1932. p=skip_space(p);
  1933. add_scriptl(add_str(label2));
  1934. add_scriptc(C_FUNC);
  1935. // Skip to the starting point
  1936. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1937. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1938. parse_line(label2);
  1939. syntax.curly_count--;
  1940. // Form label of the end point conditions
  1941. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1942. l2=add_str(label2);
  1943. set_label(l2,script_pos,p);
  1944. p = skip_space(p);
  1945. if(*p != ';') {
  1946. disp_error_message("parse_syntax: expected ';'",p);
  1947. return p+1;
  1948. }
  1949. p++;
  1950. syntax.curly_count--;
  1951. return p;
  1952. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1953. // for-block end is a new line
  1954. parse_nextline(false, p);
  1955. // Skip to the next loop
  1956. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1957. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1958. parse_line(label);
  1959. syntax.curly_count--;
  1960. // End for labeling
  1961. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1962. l=add_str(label);
  1963. set_label(l,script_pos,p);
  1964. syntax.curly_count--;
  1965. return p;
  1966. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1967. // while-block end is a new line
  1968. parse_nextline(false, p);
  1969. // Skip to the decision while
  1970. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1971. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1972. parse_line(label);
  1973. syntax.curly_count--;
  1974. // End while labeling
  1975. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1976. l=add_str(label);
  1977. set_label(l,script_pos,p);
  1978. syntax.curly_count--;
  1979. return p;
  1980. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1981. int pos2 = syntax.curly_count-1;
  1982. char label2[256];
  1983. int l2;
  1984. // Back
  1985. sprintf(label2,"return;");
  1986. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1987. parse_line(label2);
  1988. syntax.curly_count--;
  1989. // Put the label of the location
  1990. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1991. l2=add_str(label2);
  1992. set_label(l2,script_pos,p);
  1993. syntax.curly_count--;
  1994. return p;
  1995. } else {
  1996. *flag = 0;
  1997. return p;
  1998. }
  1999. }
  2000. /*==========================================
  2001. * Added built-in functions
  2002. *------------------------------------------*/
  2003. static void add_buildin_func(void)
  2004. {
  2005. int i;
  2006. for( i = 0; buildin_func[i].func; i++ )
  2007. {
  2008. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2009. // 'v' - value (either string or int or reference)
  2010. // 's' - string
  2011. // 'i' - int
  2012. // 'r' - reference (of a variable)
  2013. // 'l' - label
  2014. // '?' - one optional parameter
  2015. // '*' - unknown number of optional parameters
  2016. const char* p = buildin_func[i].arg;
  2017. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2018. while( *p == '?' ) ++p;
  2019. if( *p == '*' ) ++p;
  2020. if( *p != 0){
  2021. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2022. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2023. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2024. } else {
  2025. int n = add_str(buildin_func[i].name);
  2026. str_data[n].type = C_FUNC;
  2027. str_data[n].val = i;
  2028. str_data[n].func = buildin_func[i].func;
  2029. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  2030. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2031. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2032. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2033. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2034. }
  2035. }
  2036. }
  2037. /**
  2038. * String comparison with a char array to a script constant
  2039. * @param prefix: Char array to compare
  2040. * @param value: Script constant
  2041. * @return Constant name as char array or NULL otherwise
  2042. */
  2043. const char* script_get_constant_str( const char* prefix, int64 value ){
  2044. const char* name;
  2045. for(int i = 0; i < str_data_size; i++ ){
  2046. // Check if it is a constant
  2047. if( str_data[i].type != C_INT ){
  2048. continue;
  2049. }
  2050. // Check if the value matches
  2051. if( str_data[i].val != value ){
  2052. continue;
  2053. }
  2054. // Look up the actual string
  2055. name = get_str(i);
  2056. // Compare the prefix
  2057. if( !strncasecmp( name, prefix, strlen(prefix) ) ){
  2058. // We found a match
  2059. return name;
  2060. }
  2061. }
  2062. return NULL;
  2063. }
  2064. /// Retrieves the value of a constant parameter.
  2065. bool script_get_parameter(const char* name, int64* value)
  2066. {
  2067. int n = search_str(name);
  2068. if (n == -1 || str_data[n].type != C_PARAM)
  2069. {// not found or not a parameter
  2070. return false;
  2071. }
  2072. value[0] = str_data[n].val;
  2073. return true;
  2074. }
  2075. /// Retrieves the value of a constant.
  2076. bool script_get_constant(const char* name, int64* value)
  2077. {
  2078. int n = search_str(name);
  2079. if( n == -1 || str_data[n].type != C_INT )
  2080. {// not found or not a constant
  2081. return false;
  2082. }
  2083. value[0] = str_data[n].val;
  2084. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2085. if( str_data[n].deprecated ){
  2086. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2087. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2088. if (str_data[n].name)
  2089. ShowWarning( "Please use '%s' instead!\n", str_data[n].name );
  2090. }
  2091. #endif
  2092. return true;
  2093. }
  2094. /// Creates new constant or parameter with given value.
  2095. void script_set_constant_(const char* name, int64 value, const char* constant_name, bool isparameter, bool deprecated)
  2096. {
  2097. int n = add_str(name);
  2098. if( str_data[n].type == C_NOP )
  2099. {// new
  2100. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2101. str_data[n].val = value;
  2102. str_data[n].deprecated = deprecated;
  2103. str_data[n].name = constant_name;
  2104. }
  2105. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2106. {// existing parameter or constant
  2107. 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);
  2108. }
  2109. else
  2110. {// existing name
  2111. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2112. }
  2113. }
  2114. const std::string ConstantDatabase::getDefaultLocation(){
  2115. return std::string(db_path) + "/const.yml";
  2116. }
  2117. uint64 ConstantDatabase::parseBodyNode( const YAML::Node& node ) {
  2118. std::string constant_name;
  2119. if (!this->asString( node, "Name", constant_name ))
  2120. return 0;
  2121. char name[1024];
  2122. if (sscanf(constant_name.c_str(), "%1023[A-Za-z0-9/_]", name) != 1) {
  2123. this->invalidWarning( node["Name"], "Invalid constant definition \"%s\", skipping.\n", constant_name.c_str() );
  2124. return 0;
  2125. }
  2126. int64 val;
  2127. if (!this->asInt64( node, "Value", val ))
  2128. return 0;
  2129. bool type = false;
  2130. if (this->nodeExists(node, "Parameter") && !this->asBool( node, "Parameter", type ))
  2131. return 0;
  2132. script_set_constant(name, val, type, false);
  2133. return 1;
  2134. }
  2135. ConstantDatabase constant_db;
  2136. /**
  2137. * Sets source-end constants for NPC scripts to access.
  2138. **/
  2139. void script_hardcoded_constants(void) {
  2140. #include "script_constants.hpp"
  2141. }
  2142. /*==========================================
  2143. * Display emplacement line of script
  2144. *------------------------------------------*/
  2145. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2146. {
  2147. int i;
  2148. if( p == NULL || !p[0] ) return NULL;
  2149. if( line < 0 )
  2150. StringBuf_Printf(buf, "*% 5d : ", -line);
  2151. else
  2152. StringBuf_Printf(buf, " % 5d : ", line);
  2153. for(i=0;p[i] && p[i] != '\n';i++){
  2154. if(p + i != mark)
  2155. StringBuf_Printf(buf, "%c", p[i]);
  2156. else
  2157. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2158. }
  2159. StringBuf_AppendStr(buf, "\n");
  2160. return p+i+(p[i] == '\n' ? 1 : 0);
  2161. }
  2162. 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) {
  2163. // Find the line where the error occurred
  2164. int j;
  2165. int line = start_line;
  2166. const char *p;
  2167. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2168. for(p=src;p && *p;line++){
  2169. const char *lineend=strchr(p,'\n');
  2170. if(lineend==NULL || error_pos_cur<lineend){
  2171. break;
  2172. }
  2173. for( j = 0; j < 4; j++ ) {
  2174. linestart[j] = linestart[j+1];
  2175. }
  2176. linestart[4] = p;
  2177. p=lineend+1;
  2178. }
  2179. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2180. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2181. for(j = 0; j < 5; j++ ) {
  2182. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2183. }
  2184. p = script_print_line(buf, p, error_pos_cur, -line);
  2185. for(j = 0; j < 5; j++) {
  2186. p = script_print_line(buf, p, NULL, line + j + 1 );
  2187. }
  2188. }
  2189. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2190. StringBuf buf;
  2191. StringBuf_Init(&buf);
  2192. StringBuf_AppendStr(&buf, "\a\n");
  2193. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2194. ShowError("%s", StringBuf_Value(&buf));
  2195. StringBuf_Destroy(&buf);
  2196. }
  2197. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2198. StringBuf buf;
  2199. StringBuf_Init(&buf);
  2200. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2201. ShowWarning("%s", StringBuf_Value(&buf));
  2202. StringBuf_Destroy(&buf);
  2203. }
  2204. /*==========================================
  2205. * Analysis of the script
  2206. *------------------------------------------*/
  2207. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2208. {
  2209. const char *p,*tmpp;
  2210. int i;
  2211. struct script_code* code = NULL;
  2212. char end;
  2213. bool unresolved_names = false;
  2214. parser_current_src = src;
  2215. parser_current_file = file;
  2216. parser_current_line = line;
  2217. if( src == NULL )
  2218. return NULL;// empty script
  2219. memset(&syntax,0,sizeof(syntax));
  2220. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2221. script_pos=0;
  2222. script_size=SCRIPT_BLOCK_SIZE;
  2223. parse_nextline(true, NULL);
  2224. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2225. if( options&SCRIPT_USE_LABEL_DB )
  2226. db_clear(scriptlabel_db);
  2227. parse_options = options;
  2228. if( setjmp( error_jump ) != 0 ) {
  2229. //Restore program state when script has problems. [from jA]
  2230. int j;
  2231. const int size = ARRAYLENGTH(syntax.curly);
  2232. if( error_report )
  2233. script_error(src,file,line,error_msg,error_pos);
  2234. aFree( error_msg );
  2235. aFree( script_buf );
  2236. script_pos = 0;
  2237. script_size = 0;
  2238. script_buf = NULL;
  2239. for(j=LABEL_START;j<str_num;j++)
  2240. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2241. for(j=0; j<size; j++)
  2242. linkdb_final(&syntax.curly[j].case_label);
  2243. return NULL;
  2244. }
  2245. parse_syntax_for_flag=0;
  2246. p=src;
  2247. p=skip_space(p);
  2248. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2249. {// does not require brackets around the script
  2250. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2251. {// empty script and can return NULL
  2252. aFree( script_buf );
  2253. script_pos = 0;
  2254. script_size = 0;
  2255. script_buf = NULL;
  2256. return NULL;
  2257. }
  2258. end = '\0';
  2259. }
  2260. else
  2261. {// requires brackets around the script
  2262. if( *p != '{' )
  2263. disp_error_message("not found '{'",p);
  2264. p = skip_space(p+1);
  2265. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2266. {// empty script and can return NULL
  2267. aFree( script_buf );
  2268. script_pos = 0;
  2269. script_size = 0;
  2270. script_buf = NULL;
  2271. return NULL;
  2272. }
  2273. end = '}';
  2274. }
  2275. // clear references of labels, variables and internal functions
  2276. for(i=LABEL_START;i<str_num;i++){
  2277. if(
  2278. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2279. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2280. ){
  2281. str_data[i].type=C_NOP;
  2282. str_data[i].backpatch=-1;
  2283. str_data[i].label=-1;
  2284. }
  2285. }
  2286. while( syntax.curly_count != 0 || *p != end )
  2287. {
  2288. if( *p == '\0' )
  2289. disp_error_message("unexpected end of script",p);
  2290. // Special handling only label
  2291. tmpp=skip_space(skip_word(p));
  2292. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2293. i=add_word(p);
  2294. set_label(i,script_pos,p);
  2295. if( parse_options&SCRIPT_USE_LABEL_DB )
  2296. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2297. p=tmpp+1;
  2298. p=skip_space(p);
  2299. continue;
  2300. }
  2301. // All other lumped
  2302. p=parse_line(p);
  2303. p=skip_space(p);
  2304. parse_nextline(false, p);
  2305. }
  2306. add_scriptc(C_NOP);
  2307. // trim code to size
  2308. script_size = script_pos;
  2309. RECREATE(script_buf,unsigned char,script_pos);
  2310. // default unknown references to variables
  2311. for(i=LABEL_START;i<str_num;i++){
  2312. if(str_data[i].type==C_NOP){
  2313. int j;
  2314. str_data[i].type=C_NAME;
  2315. str_data[i].label=i;
  2316. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2317. int next=GETVALUE(script_buf,j);
  2318. SETVALUE(script_buf,j,i);
  2319. j=next;
  2320. }
  2321. }
  2322. else if( str_data[i].type == C_USERFUNC )
  2323. {// 'function name;' without follow-up code
  2324. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2325. unresolved_names = true;
  2326. }
  2327. }
  2328. if( unresolved_names )
  2329. {
  2330. disp_error_message("parse_script: unresolved function references", p);
  2331. }
  2332. #ifdef DEBUG_DISP
  2333. for(i=0;i<script_pos;i++){
  2334. if((i&15)==0) ShowMessage("%04x : ",i);
  2335. ShowMessage("%02x ",script_buf[i]);
  2336. if((i&15)==15) ShowMessage("\n");
  2337. }
  2338. ShowMessage("\n");
  2339. #endif
  2340. #ifdef DEBUG_DISASM
  2341. {
  2342. int i = 0,j;
  2343. while(i < script_pos) {
  2344. c_op op = get_com(script_buf,&i);
  2345. ShowMessage("%06x %s", i, script_op2name(op));
  2346. j = i;
  2347. switch(op) {
  2348. case C_INT:
  2349. ShowMessage(" %d", get_num(script_buf,&i));
  2350. break;
  2351. case C_POS:
  2352. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2353. i += 3;
  2354. break;
  2355. case C_NAME:
  2356. j = (*(int*)(script_buf+i)&0xffffff);
  2357. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2358. i += 3;
  2359. break;
  2360. case C_STR:
  2361. j = strlen(script_buf + i);
  2362. ShowMessage(" %s", script_buf + i);
  2363. i += j+1;
  2364. break;
  2365. }
  2366. ShowMessage(CL_CLL"\n");
  2367. }
  2368. }
  2369. #endif
  2370. CREATE(code,struct script_code,1);
  2371. code->script_buf = script_buf;
  2372. code->script_size = script_size;
  2373. code->local.vars = NULL;
  2374. code->local.arrays = NULL;
  2375. return code;
  2376. }
  2377. /// Returns the player attached to this script, identified by the rid.
  2378. /// If there is no player attached, the script is terminated.
  2379. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){
  2380. *sd = map_id2sd( st->rid );
  2381. if( *sd ){
  2382. return true;
  2383. }else{
  2384. ShowError("%s: fatal error ! player not attached!\n",func);
  2385. script_reportfunc(st);
  2386. script_reportsrc(st);
  2387. st->state = END;
  2388. return false;
  2389. }
  2390. }
  2391. /**
  2392. * Dereferences a variable/constant, replacing it with a copy of the value.
  2393. * @param st Script state
  2394. * @param data Variable/constant
  2395. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2396. */
  2397. struct script_data *get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2398. {
  2399. const char* name;
  2400. char prefix;
  2401. char postfix;
  2402. if( !data_isreference(data) )
  2403. return data;// not a variable/constant
  2404. name = reference_getname(data);
  2405. prefix = name[0];
  2406. postfix = name[strlen(name) - 1];
  2407. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2408. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2409. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2410. if( postfix == '$' ) {// string variable
  2411. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2412. data->type = C_CONSTSTR;
  2413. data->u.str = const_cast<char *>("");
  2414. } else {// integer variable
  2415. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2416. data->type = C_INT;
  2417. data->u.num = 0;
  2418. }
  2419. return data;
  2420. }
  2421. }
  2422. if( postfix == '$' ) {// string variable
  2423. switch( prefix ) {
  2424. case '@':
  2425. data->u.str = pc_readregstr(sd, data->u.num);
  2426. break;
  2427. case '$':
  2428. data->u.str = mapreg_readregstr(data->u.num);
  2429. break;
  2430. case '#':
  2431. if( name[1] == '#' )
  2432. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2433. else
  2434. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2435. break;
  2436. case '.':
  2437. {
  2438. struct DBMap* n = data->ref ?
  2439. data->ref->vars : name[1] == '@' ?
  2440. st->stack->scope.vars : // instance/scope variable
  2441. st->script->local.vars; // npc variable
  2442. if( n )
  2443. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2444. else
  2445. data->u.str = NULL;
  2446. }
  2447. break;
  2448. case '\'':
  2449. {
  2450. struct DBMap* n = nullptr;
  2451. if (data->ref)
  2452. n = data->ref->vars;
  2453. else {
  2454. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2455. if (idata)
  2456. n = idata->regs.vars;
  2457. }
  2458. if (n)
  2459. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2460. else {
  2461. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2462. data->u.str = NULL;
  2463. }
  2464. break;
  2465. }
  2466. default:
  2467. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2468. break;
  2469. }
  2470. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2471. data->type = C_CONSTSTR;
  2472. data->u.str = const_cast<char *>("");
  2473. } else {// duplicate string
  2474. data->type = C_STR;
  2475. data->u.str = aStrdup(data->u.str);
  2476. }
  2477. } else {// integer variable
  2478. data->type = C_INT;
  2479. if( reference_toconstant(data) ) {
  2480. data->u.num = reference_getconstant(data);
  2481. } else if( reference_toparam(data) ) {
  2482. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2483. } else
  2484. switch( prefix ) {
  2485. case '@':
  2486. data->u.num = pc_readreg(sd, data->u.num);
  2487. break;
  2488. case '$':
  2489. data->u.num = mapreg_readreg(data->u.num);
  2490. break;
  2491. case '#':
  2492. if( name[1] == '#' )
  2493. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2494. else
  2495. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2496. break;
  2497. case '.':
  2498. {
  2499. struct DBMap* n = data->ref ?
  2500. data->ref->vars : name[1] == '@' ?
  2501. st->stack->scope.vars : // instance/scope variable
  2502. st->script->local.vars; // npc variable
  2503. if( n )
  2504. data->u.num = i64db_i64get(n,reference_getuid(data));
  2505. else
  2506. data->u.num = 0;
  2507. }
  2508. break;
  2509. case '\'':
  2510. {
  2511. struct DBMap* n = nullptr;
  2512. if (data->ref)
  2513. n = data->ref->vars;
  2514. else {
  2515. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2516. if (idata)
  2517. n = idata->regs.vars;
  2518. }
  2519. if (n)
  2520. data->u.num = i64db_i64get(n,reference_getuid(data));
  2521. else {
  2522. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2523. data->u.num = 0;
  2524. }
  2525. break;
  2526. }
  2527. default:
  2528. data->u.num = pc_readglobalreg(sd, data->u.num);
  2529. break;
  2530. }
  2531. }
  2532. data->ref = NULL;
  2533. return data;
  2534. }
  2535. struct script_data *get_val(struct script_state* st, struct script_data* data)
  2536. {
  2537. return get_val_(st,data,NULL);
  2538. }
  2539. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2540. const char* get_val2_str( struct script_state* st, int64 uid, struct reg_db* ref ){
  2541. push_val2( st->stack, C_NAME, uid, ref );
  2542. struct script_data* data = script_getdatatop( st, -1 );
  2543. get_val( st, data );
  2544. const char* value = "";
  2545. if( data->type == C_INT ){
  2546. ShowError( "get_val2_num: Invalid call. Variable %s is a numeric type.\n", reference_getname( data ) );
  2547. }else{
  2548. value = data->u.str;
  2549. }
  2550. // Do NOT remove the value from stack here, the pointer is returned here and will be used by the caller [Lemongrass]
  2551. // script_removetop( st, -1, 0 );
  2552. return value;
  2553. }
  2554. int64 get_val2_num( struct script_state* st, int64 uid, struct reg_db* ref ){
  2555. push_val2( st->stack, C_NAME, uid, ref );
  2556. struct script_data* data = script_getdatatop( st, -1 );
  2557. get_val( st, data );
  2558. int64 value = 0;
  2559. if( data->type == C_INT ){
  2560. value = data->u.num;
  2561. }else{
  2562. ShowError( "get_val2_num: Invalid call. Variable %s is not a numeric type.\n", reference_getname( data ) );
  2563. }
  2564. script_removetop( st, -1, 0 );
  2565. return value;
  2566. }
  2567. /**
  2568. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2569. * Will be gone as soon as undefined var feature is implemented
  2570. **/
  2571. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2572. {
  2573. const char *name = get_str(script_getvarid(uid));
  2574. // is here st can be null pointer and st->rid is wrong?
  2575. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2576. bool insert = false;
  2577. if (sd && !st) {
  2578. // when sd comes, st isn't available
  2579. insert = true;
  2580. } else {
  2581. if( is_string_variable(name) ) {
  2582. const char* str = get_val2_str( st, uid, ref );
  2583. if( str && *str )
  2584. insert = true;
  2585. // Remove stack entry from get_val2_str
  2586. script_removetop( st, -1, 0 );
  2587. } else {
  2588. int64 num = get_val2_num( st, uid, ref );
  2589. if( num )
  2590. insert = true;
  2591. }
  2592. }
  2593. if (src && src->arrays) {
  2594. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2595. if (sa) {
  2596. unsigned int i;
  2597. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2598. if( i != sa->size ) {
  2599. if( !insert )
  2600. script_array_remove_member(src,sa,i);
  2601. return;
  2602. }
  2603. script_array_add_member(sa,0);
  2604. } else if (insert) {
  2605. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2606. }
  2607. }
  2608. }
  2609. /**
  2610. * Returns array size by ID
  2611. **/
  2612. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2613. {
  2614. struct script_array *sa = NULL;
  2615. struct reg_db *src = script_array_src(st, sd, name, ref);
  2616. if (src && src->arrays)
  2617. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2618. return sa ? sa->size : 0;
  2619. }
  2620. /**
  2621. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2622. **/
  2623. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2624. {
  2625. struct script_array *sa = NULL;
  2626. struct reg_db *src = script_array_src(st, sd, name, ref);
  2627. if (src && src->arrays) {
  2628. int key = add_word(name);
  2629. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2630. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2631. unsigned int i, highest_key = 0;
  2632. for(i = 0; i < sa->size; i++) {
  2633. if( sa->members[i] > highest_key )
  2634. highest_key = sa->members[i];
  2635. }
  2636. return sa->size ? highest_key + 1 : 0;
  2637. }
  2638. }
  2639. return SCRIPT_CMD_SUCCESS;
  2640. }
  2641. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2642. {
  2643. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2644. aFree(sa->members);
  2645. ers_free(array_ers, sa);
  2646. return SCRIPT_CMD_SUCCESS;
  2647. }
  2648. /**
  2649. * Clears script_array and removes it from script->array_db
  2650. **/
  2651. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2652. {
  2653. aFree(sa->members);
  2654. idb_remove(src->arrays, sa->id);
  2655. ers_free(array_ers, sa);
  2656. }
  2657. /**
  2658. * Removes a member from a script_array list
  2659. *
  2660. * @param idx the index of the member in script_array struct list, not of the actual array member
  2661. **/
  2662. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2663. {
  2664. unsigned int i, cursor;
  2665. // it's the only member left, no need to do anything other than delete the array data
  2666. if( sa->size == 1 ) {
  2667. script_array_delete(src,sa);
  2668. return;
  2669. }
  2670. sa->members[idx] = UINT_MAX;
  2671. for(i = 0, cursor = 0; i < sa->size; i++) {
  2672. if( sa->members[i] == UINT_MAX )
  2673. continue;
  2674. if( i != cursor )
  2675. sa->members[cursor] = sa->members[i];
  2676. cursor++;
  2677. }
  2678. sa->size = cursor;
  2679. }
  2680. /**
  2681. * Appends a new array index to the list in script_array
  2682. *
  2683. * @param idx the index of the array member being inserted
  2684. **/
  2685. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2686. {
  2687. RECREATE(sa->members, unsigned int, ++sa->size);
  2688. sa->members[sa->size - 1] = idx;
  2689. }
  2690. /**
  2691. * Obtains the source of the array database for this type and scenario
  2692. * Initializes such database when not yet initialized.
  2693. **/
  2694. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2695. {
  2696. struct reg_db *src = NULL;
  2697. switch( name[0] ) {
  2698. // from player
  2699. default: // char reg
  2700. case '@': // temp char reg
  2701. case '#': // account reg
  2702. src = &sd->regs;
  2703. break;
  2704. case '$': // map reg
  2705. src = &regs;
  2706. break;
  2707. case '.': // npc/script
  2708. if (ref)
  2709. src = ref;
  2710. else
  2711. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2712. break;
  2713. case '\'': // instance
  2714. {
  2715. if (ref)
  2716. src = ref;
  2717. else {
  2718. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2719. if (idata)
  2720. src = &idata->regs;
  2721. }
  2722. break;
  2723. }
  2724. }
  2725. if( src ) {
  2726. if( !src->arrays )
  2727. src->arrays = idb_alloc(DB_OPT_BASE);
  2728. return src;
  2729. }
  2730. return NULL;
  2731. }
  2732. /**
  2733. * Processes a array member modification, and update data accordingly
  2734. *
  2735. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2736. * @param num[in] Variable ID
  2737. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2738. **/
  2739. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2740. {
  2741. struct script_array *sa = NULL;
  2742. int id = script_getvarid(num);
  2743. unsigned int index = script_getvaridx(num);
  2744. if (!src->arrays) {
  2745. src->arrays = idb_alloc(DB_OPT_BASE);
  2746. } else {
  2747. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2748. }
  2749. if( sa ) {
  2750. unsigned int i;
  2751. // search
  2752. for(i = 0; i < sa->size; i++) {
  2753. if( sa->members[i] == index )
  2754. break;
  2755. }
  2756. // if existent
  2757. if( i != sa->size ) {
  2758. // if empty, we gotta remove it
  2759. if( empty ) {
  2760. script_array_remove_member(src, sa, i);
  2761. }
  2762. } else if( !empty ) { /* new entry */
  2763. script_array_add_member(sa,index);
  2764. // we do nothing if its empty, no point in modifying array data for a new empty member
  2765. }
  2766. } else if ( !empty ) { // we only move to create if not empty
  2767. sa = ers_alloc(array_ers, struct script_array);
  2768. sa->id = id;
  2769. sa->members = NULL;
  2770. sa->size = 0;
  2771. script_array_add_member(sa,index);
  2772. idb_put(src->arrays, id, sa);
  2773. }
  2774. }
  2775. /**
  2776. * Stores the value of a script variable
  2777. *
  2778. * @param st: current script state.
  2779. * @param sd: current character data.
  2780. * @param num: variable identifier.
  2781. * @param name: variable name.
  2782. * @param value: new value.
  2783. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2784. * @return: 0 failure, 1 success.
  2785. *
  2786. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2787. *------------------------------------------*/
  2788. bool set_reg_str( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, const char* value, struct reg_db *ref ){
  2789. char prefix = name[0];
  2790. size_t vlen = 0;
  2791. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2792. ShowError( "set_reg: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%" PRIuPTR "\n", sd ? sd->status.account_id : -1, sd ? sd->status.char_id : -1, name, vlen );
  2793. return false;
  2794. }
  2795. if( !is_string_variable( name ) ){
  2796. // integer variable
  2797. return false;
  2798. }
  2799. switch( prefix ){
  2800. case '@':
  2801. pc_setregstr( sd, num, value );
  2802. return true;
  2803. case '$':
  2804. return mapreg_setregstr( num, value );
  2805. case '#':
  2806. return ( name[1] == '#' ) ? pc_setaccountreg2str( sd, num, value ) : pc_setaccountregstr( sd, num, value );
  2807. case '.': {
  2808. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2809. if( n ){
  2810. if( value[0] ){
  2811. i64db_put( n->vars, num, aStrdup( value ) );
  2812. if( script_getvaridx( num ) ){
  2813. script_array_update( n, num, false );
  2814. }
  2815. }else{
  2816. i64db_remove( n->vars, num );
  2817. if( script_getvaridx( num ) ){
  2818. script_array_update( n, num, true );
  2819. }
  2820. }
  2821. }
  2822. }
  2823. return true;
  2824. case '\'': {
  2825. struct reg_db *src = nullptr;
  2826. if( ref ){
  2827. src = ref;
  2828. }else{
  2829. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2830. if (idata)
  2831. src = &idata->regs;
  2832. }
  2833. if( src ){
  2834. bool empty;
  2835. if( value[0] ){
  2836. i64db_put( src->vars, num, aStrdup( value ) );
  2837. empty = false;
  2838. }else{
  2839. i64db_remove( src->vars, num );
  2840. empty = true;
  2841. }
  2842. if( script_getvaridx( num ) != 0 ){
  2843. script_array_update( src, num, empty );
  2844. }
  2845. }else{
  2846. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2847. script_reportsrc( st );
  2848. }
  2849. return true;
  2850. }
  2851. default:
  2852. return pc_setglobalreg_str( sd, num, value );
  2853. }
  2854. }
  2855. bool set_reg_num( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, int64 value, struct reg_db *ref ){
  2856. char prefix = name[0];
  2857. size_t vlen = 0;
  2858. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2859. ShowError( "set_reg: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%" PRIuPTR "\n", sd ? sd->status.account_id : -1, sd ? sd->status.char_id : -1, name, vlen );
  2860. return false;
  2861. }
  2862. if( is_string_variable( name ) ){
  2863. // string variable
  2864. return false;
  2865. }
  2866. if( str_data[script_getvarid(num)].type == C_PARAM ){
  2867. if( pc_setparam( sd, str_data[script_getvarid(num)].val, value ) == 0 ){
  2868. if( st != NULL ) {
  2869. ShowError( "script_set_reg: failed to set param '%s' to %" PRId64 ".\n", name, value );
  2870. script_reportsrc( st );
  2871. st->state = END;
  2872. }
  2873. return false;
  2874. }
  2875. return true;
  2876. }
  2877. switch( prefix ){
  2878. case '@':
  2879. pc_setreg( sd, num, value );
  2880. return true;
  2881. case '$':
  2882. return mapreg_setreg( num, value );
  2883. case '#':
  2884. return ( name[1] == '#' ) ? pc_setaccountreg2(sd, num, value) : pc_setaccountreg( sd, num, value );
  2885. case '.': {
  2886. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2887. if( n ){
  2888. if( value != 0 ){
  2889. i64db_i64put( n->vars, num, value );
  2890. if( script_getvaridx( num ) ){
  2891. script_array_update( n, num, false );
  2892. }
  2893. }else{
  2894. i64db_remove( n->vars, num );
  2895. if( script_getvaridx( num ) ){
  2896. script_array_update( n, num, true );
  2897. }
  2898. }
  2899. }
  2900. }
  2901. return true;
  2902. case '\'': {
  2903. struct reg_db *src = nullptr;
  2904. if( ref ){
  2905. src = ref;
  2906. }else{
  2907. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2908. if (idata)
  2909. src = &idata->regs;
  2910. }
  2911. if( src ){
  2912. bool empty;
  2913. if( value != 0 ){
  2914. i64db_i64put( src->vars, num, value );
  2915. empty = false;
  2916. }else{
  2917. i64db_remove( src->vars, num );
  2918. empty = true;
  2919. }
  2920. if( script_getvaridx( num ) != 0 ){
  2921. script_array_update( src, num, empty );
  2922. }
  2923. }else{
  2924. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2925. script_reportsrc( st );
  2926. }
  2927. return true;
  2928. }
  2929. default:
  2930. return pc_setglobalreg( sd, num, value );
  2931. }
  2932. }
  2933. bool set_var_str( struct map_session_data* sd, const char* name, const char* val ){
  2934. return set_reg_str( NULL, sd, reference_uid( add_str( name ), 0 ), name, val, NULL );
  2935. }
  2936. bool clear_reg( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, struct reg_db *ref ){
  2937. if( is_string_variable( name ) ){
  2938. return set_reg_str( st, sd, num, name, "", ref );
  2939. }else{
  2940. return set_reg_num( st, sd, num, name, 0, ref );
  2941. }
  2942. }
  2943. void setd_sub_num( struct script_state* st, struct map_session_data* sd, const char* varname, int elem, int64 value, struct reg_db* ref ){
  2944. set_reg_num( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2945. }
  2946. void setd_sub_str( struct script_state* st, struct map_session_data* sd, const char* varname, int elem, const char* value, struct reg_db* ref ){
  2947. set_reg_str( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2948. }
  2949. /**
  2950. * Converts the data to a string
  2951. * @param st
  2952. * @param data
  2953. * @param sd
  2954. */
  2955. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2956. {
  2957. char* p;
  2958. get_val_(st, data, sd);
  2959. if( data_isstring(data) )
  2960. {// nothing to convert
  2961. }
  2962. else if( data_isint(data) )
  2963. {// int -> string
  2964. CREATE(p, char, ITEM_NAME_LENGTH);
  2965. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2966. p[ITEM_NAME_LENGTH-1] = '\0';
  2967. data->type = C_STR;
  2968. data->u.str = p;
  2969. }
  2970. else if( data_isreference(data) )
  2971. {// reference -> string
  2972. //##TODO when does this happen (check get_val) [FlavioJS] -- at getd!!
  2973. data->type = C_CONSTSTR;
  2974. data->u.str = reference_getname(data);
  2975. }
  2976. else
  2977. {// unsupported data type
  2978. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2979. script_reportdata(data);
  2980. script_reportsrc(st);
  2981. data->type = C_CONSTSTR;
  2982. data->u.str = const_cast<char *>("");
  2983. }
  2984. return data->u.str;
  2985. }
  2986. const char* conv_str(struct script_state* st, struct script_data* data)
  2987. {
  2988. return conv_str_(st, data, NULL);
  2989. }
  2990. /**
  2991. * Converts the data to an int
  2992. * @param st
  2993. * @param data
  2994. * @param sd
  2995. */
  2996. int64 conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2997. {
  2998. get_val_(st, data, sd);
  2999. if( data_isint(data) )
  3000. {// nothing to convert
  3001. }
  3002. else if( data_isstring(data) )
  3003. {// string -> int
  3004. // the result does not overflow or underflow, it is capped instead
  3005. // ex: 999999999999 is capped to INT_MAX (2147483647)
  3006. char* p = data->u.str;
  3007. int64 num;
  3008. bool error;
  3009. errno = 0;
  3010. num = strtoll(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  3011. error = (errno == ERANGE);
  3012. if( num < SCRIPT_INT_MIN || ( error && num == INT64_MIN ) ){
  3013. num = SCRIPT_INT_MIN;
  3014. ShowError( "script:conv_num: underflow detected, capping value to %" PRId64 "", SCRIPT_INT_MIN );
  3015. script_reportdata(data);
  3016. script_reportsrc(st);
  3017. }else if( num > SCRIPT_INT_MAX || ( error && num == INT64_MAX ) ){
  3018. num = SCRIPT_INT_MAX;
  3019. ShowError( "script:conv_num: overflow detected, capping value to %" PRId64 "", SCRIPT_INT_MAX );
  3020. script_reportdata(data);
  3021. script_reportsrc(st);
  3022. }
  3023. if( data->type == C_STR )
  3024. aFree(p);
  3025. data->type = C_INT;
  3026. data->u.num = num;
  3027. }
  3028. #if 0
  3029. // FIXME this function is being used to retrieve the position of labels and
  3030. // probably other stuff [FlavioJS]
  3031. else
  3032. {// unsupported data type
  3033. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3034. script_reportdata(data);
  3035. script_reportsrc(st);
  3036. data->type = C_INT;
  3037. data->u.num = 0;
  3038. }
  3039. #endif
  3040. return data->u.num;
  3041. }
  3042. int64 conv_num64(struct script_state* st, struct script_data* data)
  3043. {
  3044. return conv_num_(st, data, NULL);
  3045. }
  3046. int conv_num(struct script_state* st, struct script_data* data)
  3047. {
  3048. return static_cast<int>(conv_num_(st, data, NULL));
  3049. }
  3050. //
  3051. // Stack operations
  3052. //
  3053. /// Increases the size of the stack
  3054. void stack_expand(struct script_stack* stack)
  3055. {
  3056. stack->sp_max += 64;
  3057. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3058. stack->sp_max * sizeof(stack->stack_data[0]) );
  3059. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3060. 64 * sizeof(stack->stack_data[0]) );
  3061. }
  3062. /// Pushes a value into the stack
  3063. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  3064. /// Pushes a value into the stack (with reference)
  3065. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3066. {
  3067. if( stack->sp >= stack->sp_max )
  3068. stack_expand(stack);
  3069. stack->stack_data[stack->sp].type = type;
  3070. stack->stack_data[stack->sp].u.num = val;
  3071. stack->stack_data[stack->sp].ref = ref;
  3072. stack->sp++;
  3073. return &stack->stack_data[stack->sp-1];
  3074. }
  3075. /// Pushes a string into the stack
  3076. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3077. {
  3078. if( stack->sp >= stack->sp_max )
  3079. stack_expand(stack);
  3080. stack->stack_data[stack->sp].type = type;
  3081. stack->stack_data[stack->sp].u.str = str;
  3082. stack->stack_data[stack->sp].ref = NULL;
  3083. stack->sp++;
  3084. return &stack->stack_data[stack->sp-1];
  3085. }
  3086. /// Pushes a retinfo into the stack
  3087. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3088. {
  3089. if( stack->sp >= stack->sp_max )
  3090. stack_expand(stack);
  3091. stack->stack_data[stack->sp].type = C_RETINFO;
  3092. stack->stack_data[stack->sp].u.ri = ri;
  3093. stack->stack_data[stack->sp].ref = ref;
  3094. stack->sp++;
  3095. return &stack->stack_data[stack->sp-1];
  3096. }
  3097. /// Pushes a copy of the target position into the stack
  3098. struct script_data* push_copy(struct script_stack* stack, int pos)
  3099. {
  3100. switch( stack->stack_data[pos].type ) {
  3101. case C_CONSTSTR:
  3102. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3103. break;
  3104. case C_STR:
  3105. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3106. break;
  3107. case C_RETINFO:
  3108. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3109. exit(1);
  3110. break;
  3111. default:
  3112. return push_val2(
  3113. stack,stack->stack_data[pos].type,
  3114. stack->stack_data[pos].u.num,
  3115. stack->stack_data[pos].ref
  3116. );
  3117. break;
  3118. }
  3119. }
  3120. /// Removes the values in indexes [start,end] from the stack.
  3121. /// Adjusts all stack pointers.
  3122. void pop_stack(struct script_state* st, int start, int end)
  3123. {
  3124. struct script_stack* stack = st->stack;
  3125. struct script_data* data;
  3126. int i;
  3127. if( start < 0 )
  3128. start = 0;
  3129. if( end > stack->sp )
  3130. end = stack->sp;
  3131. if( start >= end )
  3132. return;// nothing to pop
  3133. // free stack elements
  3134. for( i = start; i < end; i++ )
  3135. {
  3136. data = &stack->stack_data[i];
  3137. if( data->type == C_STR )
  3138. aFree(data->u.str);
  3139. if( data->type == C_RETINFO ) {
  3140. struct script_retinfo* ri = data->u.ri;
  3141. if (ri->scope.vars) {
  3142. script_free_vars(ri->scope.vars);
  3143. ri->scope.vars = NULL;
  3144. }
  3145. if (ri->scope.arrays) {
  3146. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3147. ri->scope.arrays = NULL;
  3148. }
  3149. if( data->ref )
  3150. aFree(data->ref);
  3151. aFree(ri);
  3152. }
  3153. data->type = C_NOP;
  3154. }
  3155. // move the rest of the elements
  3156. if( stack->sp > end )
  3157. {
  3158. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3159. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3160. stack->stack_data[i].type = C_NOP;
  3161. }
  3162. // adjust stack pointers
  3163. if( st->start > end ){
  3164. st->start -= end - start;
  3165. }else if( st->start > start ){
  3166. st->start = start;
  3167. }
  3168. if( st->end > end ){
  3169. st->end -= end - start;
  3170. }else if( st->end > start ){
  3171. st->end = start;
  3172. }
  3173. if( stack->defsp > end ){
  3174. stack->defsp -= end - start;
  3175. }else if( stack->defsp > start ){
  3176. stack->defsp = start;
  3177. }
  3178. stack->sp -= end - start;
  3179. }
  3180. ///
  3181. ///
  3182. ///
  3183. /*==========================================
  3184. * Release script dependent variable, dependent variable of function
  3185. *------------------------------------------*/
  3186. void script_free_vars(struct DBMap* storage)
  3187. {
  3188. if( storage ) {
  3189. // destroy the storage construct containing the variables
  3190. db_destroy(storage);
  3191. }
  3192. }
  3193. void script_free_code(struct script_code* code)
  3194. {
  3195. nullpo_retv(code);
  3196. if (code->instances)
  3197. script_stop_scriptinstances(code);
  3198. script_free_vars(code->local.vars);
  3199. if (code->local.arrays)
  3200. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3201. aFree(code->script_buf);
  3202. aFree(code);
  3203. }
  3204. /// Creates a new script state.
  3205. ///
  3206. /// @param script Script code
  3207. /// @param pos Position in the code
  3208. /// @param rid Who is running the script (attached player)
  3209. /// @param oid Where the code is being run (npc 'object')
  3210. /// @return Script state
  3211. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3212. {
  3213. struct script_state* st;
  3214. st = ers_alloc(st_ers, struct script_state);
  3215. st->stack = ers_alloc(stack_ers, struct script_stack);
  3216. st->stack->sp = 0;
  3217. st->stack->sp_max = 64;
  3218. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3219. st->stack->defsp = st->stack->sp;
  3220. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3221. st->stack->scope.arrays = NULL;
  3222. st->state = RUN;
  3223. st->script = rootscript;
  3224. st->pos = pos;
  3225. st->rid = rid;
  3226. st->oid = oid;
  3227. st->sleep.timer = INVALID_TIMER;
  3228. st->npc_item_flag = battle_config.item_enabled_npc;
  3229. if( st->script->instances != USHRT_MAX )
  3230. st->script->instances++;
  3231. else {
  3232. struct npc_data *nd = map_id2nd(oid);
  3233. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3234. }
  3235. if (!st->script->local.vars)
  3236. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3237. st->id = next_id++;
  3238. active_scripts++;
  3239. idb_put(st_db, st->id, st);
  3240. return st;
  3241. }
  3242. /// Frees a script state.
  3243. ///
  3244. /// @param st Script state
  3245. void script_free_state(struct script_state* st)
  3246. {
  3247. if (idb_exists(st_db, st->id)) {
  3248. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3249. if (st->bk_st) // backup was not restored
  3250. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3251. if (sd && sd->st == st) { // Current script is aborted.
  3252. if(sd->state.using_fake_npc) {
  3253. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3254. sd->state.using_fake_npc = 0;
  3255. }
  3256. sd->st = NULL;
  3257. sd->npc_id = 0;
  3258. }
  3259. if (st->sleep.timer != INVALID_TIMER)
  3260. delete_timer(st->sleep.timer, run_script_timer);
  3261. if (st->stack) {
  3262. script_free_vars(st->stack->scope.vars);
  3263. if (st->stack->scope.arrays)
  3264. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3265. pop_stack(st, 0, st->stack->sp);
  3266. aFree(st->stack->stack_data);
  3267. ers_free(stack_ers, st->stack);
  3268. st->stack = NULL;
  3269. }
  3270. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3271. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3272. script_free_vars(st->script->local.vars);
  3273. st->script->local.vars = NULL;
  3274. }
  3275. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3276. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3277. st->script->local.arrays = NULL;
  3278. }
  3279. }
  3280. st->pos = -1;
  3281. idb_remove(st_db, st->id);
  3282. ers_free(st_ers, st);
  3283. if (--active_scripts == 0)
  3284. next_id = 0;
  3285. }
  3286. }
  3287. //
  3288. // Main execution unit
  3289. //
  3290. /*==========================================
  3291. * Read command
  3292. *------------------------------------------*/
  3293. c_op get_com(unsigned char *script,int *pos)
  3294. {
  3295. int i = 0, j = 0;
  3296. if(script[*pos]>=0x80){
  3297. return C_INT;
  3298. }
  3299. while(script[*pos]>=0x40){
  3300. i=script[(*pos)++]<<j;
  3301. j+=6;
  3302. }
  3303. return (c_op)(i+(script[(*pos)++]<<j));
  3304. }
  3305. /*==========================================
  3306. * Income figures
  3307. *------------------------------------------*/
  3308. int64 get_num(unsigned char *script,int *pos)
  3309. {
  3310. int i, end;
  3311. int64 value;
  3312. // Find the end of the data
  3313. for( i = 0; script[*pos+i] >= 0xc0; i++ ) ; // do nothing just find it
  3314. end = i; // store the end for later
  3315. value = 0; // initialize empty value
  3316. // If more than one byte of data exists
  3317. for( ; i > 0; i-- ){
  3318. value |= (int64)( script[*pos+i] & (int64)0x3f );
  3319. value <<= 6;
  3320. }
  3321. // Put our last piece of data into the return value
  3322. value |= (script[*pos+i] & (int64)0x3f);
  3323. *pos += end + 1; // one byte after the last byte of data
  3324. return value;
  3325. }
  3326. /// Ternary operators
  3327. /// test ? if_true : if_false
  3328. void op_3(struct script_state* st, int op)
  3329. {
  3330. struct script_data* data;
  3331. int flag = 0;
  3332. data = script_getdatatop(st, -3);
  3333. get_val(st, data);
  3334. if( data_isstring(data) )
  3335. flag = data->u.str[0];// "" -> false
  3336. else if( data_isint(data) )
  3337. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3338. else
  3339. {
  3340. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3341. script_reportdata(data);
  3342. script_reportsrc(st);
  3343. script_removetop(st, -3, 0);
  3344. script_pushnil(st);
  3345. return;
  3346. }
  3347. if( flag )
  3348. script_pushcopytop(st, -2);
  3349. else
  3350. script_pushcopytop(st, -1);
  3351. script_removetop(st, -4, -1);
  3352. }
  3353. /// Binary string operators
  3354. /// s1 EQ s2 -> i
  3355. /// s1 NE s2 -> i
  3356. /// s1 GT s2 -> i
  3357. /// s1 GE s2 -> i
  3358. /// s1 LT s2 -> i
  3359. /// s1 LE s2 -> i
  3360. /// s1 ADD s2 -> s
  3361. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3362. {
  3363. int a = 0;
  3364. switch(op){
  3365. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3366. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3367. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3368. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3369. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3370. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3371. case C_ADD:
  3372. {
  3373. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3374. strcpy(buf, s1);
  3375. strcat(buf, s2);
  3376. script_pushstr(st, buf);
  3377. return;
  3378. }
  3379. default:
  3380. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3381. script_reportsrc(st);
  3382. script_pushnil(st);
  3383. st->state = END;
  3384. return;
  3385. }
  3386. script_pushint(st,a);
  3387. }
  3388. /// Binary number operators
  3389. /// i OP i -> i
  3390. void op_2num( struct script_state* st, int op, int64 i1, int64 i2 ){
  3391. int64 ret;
  3392. switch( op ) {
  3393. case C_AND: ret = i1 & i2; break;
  3394. case C_OR: ret = i1 | i2; break;
  3395. case C_XOR: ret = i1 ^ i2; break;
  3396. case C_LAND: ret = (i1 && i2); break;
  3397. case C_LOR: ret = (i1 || i2); break;
  3398. case C_EQ: ret = (i1 == i2); break;
  3399. case C_NE: ret = (i1 != i2); break;
  3400. case C_GT: ret = (i1 > i2); break;
  3401. case C_GE: ret = (i1 >= i2); break;
  3402. case C_LT: ret = (i1 < i2); break;
  3403. case C_LE: ret = (i1 <= i2); break;
  3404. case C_R_SHIFT: ret = i1>>i2; break;
  3405. case C_L_SHIFT: ret = i1<<i2; break;
  3406. case C_DIV:
  3407. case C_MOD:
  3408. if( i2 == 0 )
  3409. {
  3410. ShowError("script:op_2num: division by zero detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3411. script_reportsrc(st);
  3412. script_pushnil(st);
  3413. st->state = END;
  3414. return;
  3415. }
  3416. else if( op == C_DIV )
  3417. ret = i1 / i2;
  3418. else//if( op == C_MOD )
  3419. ret = i1 % i2;
  3420. break;
  3421. default:
  3422. bool overflow;
  3423. // operators that can overflow/underflow
  3424. switch( op ) {
  3425. case C_ADD: overflow = util::safe_addition( i1, i2, ret ); break;
  3426. case C_SUB: overflow = util::safe_substraction( i1, i2, ret ); break;
  3427. case C_MUL: overflow = util::safe_multiplication( i1, i2, ret ); break;
  3428. default:
  3429. ShowError("script:op_2num: unexpected number operator %s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3430. script_reportsrc(st);
  3431. script_pushnil(st);
  3432. return;
  3433. }
  3434. if( overflow ){
  3435. ShowWarning("script:op_2num: overflow detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3436. script_reportsrc(st);
  3437. script_pushnil(st);
  3438. st->state = END;
  3439. return;
  3440. }
  3441. }
  3442. script_pushint(st, ret);
  3443. }
  3444. /// Binary operators
  3445. void op_2(struct script_state *st, int op)
  3446. {
  3447. struct script_data* left, leftref;
  3448. struct script_data* right;
  3449. leftref.type = C_NOP;
  3450. left = script_getdatatop(st, -2);
  3451. right = script_getdatatop(st, -1);
  3452. if (st->op2ref) {
  3453. if (data_isreference(left)) {
  3454. leftref = *left;
  3455. }
  3456. st->op2ref = 0;
  3457. }
  3458. get_val(st, left);
  3459. get_val(st, right);
  3460. // automatic conversions
  3461. switch( op )
  3462. {
  3463. case C_ADD:
  3464. if( data_isint(left) && data_isstring(right) )
  3465. {// convert int-string to string-string
  3466. conv_str(st, left);
  3467. }
  3468. else if( data_isstring(left) && data_isint(right) )
  3469. {// convert string-int to string-string
  3470. conv_str(st, right);
  3471. }
  3472. break;
  3473. }
  3474. if( data_isstring(left) && data_isstring(right) )
  3475. {// ss => op_2str
  3476. op_2str(st, op, left->u.str, right->u.str);
  3477. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3478. if (leftref.type != C_NOP)
  3479. {
  3480. if (left->type == C_STR) // don't free C_CONSTSTR
  3481. aFree(left->u.str);
  3482. *left = leftref;
  3483. }
  3484. }
  3485. else if( data_isint(left) && data_isint(right) )
  3486. {// ii => op_2num
  3487. int64 i1 = left->u.num;
  3488. int64 i2 = right->u.num;
  3489. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3490. op_2num(st, op, i1, i2);
  3491. if (leftref.type != C_NOP)
  3492. *left = leftref;
  3493. }
  3494. else
  3495. {// invalid argument
  3496. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3497. script_reportdata(left);
  3498. script_reportdata(right);
  3499. script_reportsrc(st);
  3500. script_removetop(st, -2, 0);
  3501. script_pushnil(st);
  3502. st->state = END;
  3503. }
  3504. }
  3505. /// Unary operators
  3506. /// NEG i -> i
  3507. /// NOT i -> i
  3508. /// LNOT i -> i
  3509. void op_1(struct script_state* st, int op)
  3510. {
  3511. struct script_data* data;
  3512. data = script_getdatatop(st, -1);
  3513. get_val(st, data);
  3514. if( !data_isint(data) )
  3515. {// not a number
  3516. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3517. script_reportdata(data);
  3518. script_reportsrc(st);
  3519. script_pushnil(st);
  3520. st->state = END;
  3521. return;
  3522. }
  3523. int64 i1 = data->u.num;
  3524. script_removetop(st, -1, 0);
  3525. switch( op )
  3526. {
  3527. case C_NEG: i1 = -i1; break;
  3528. case C_NOT: i1 = ~i1; break;
  3529. case C_LNOT: i1 = !i1; break;
  3530. default:
  3531. ShowError("script:op_1: unexpected operator %s i1=%" PRId64 "\n", script_op2name(op), i1);
  3532. script_reportsrc(st);
  3533. script_pushnil(st);
  3534. st->state = END;
  3535. return;
  3536. }
  3537. script_pushint(st, i1);
  3538. }
  3539. /// Checks the type of all arguments passed to a built-in function.
  3540. ///
  3541. /// @param st Script state whose stack arguments should be inspected.
  3542. /// @param func Built-in function for which the arguments are intended.
  3543. static void script_check_buildin_argtype(struct script_state* st, int func)
  3544. {
  3545. int idx, invalid = 0;
  3546. for( idx = 2; script_hasdata(st, idx); idx++ )
  3547. {
  3548. struct script_data* data = script_getdata(st, idx);
  3549. script_function* sf = &buildin_func[str_data[func].val];
  3550. char type = sf->arg[idx-2];
  3551. if( type == '?' || type == '*' )
  3552. {// optional argument or unknown number of optional parameters ( no types are after this )
  3553. break;
  3554. }
  3555. else if( type == 0 )
  3556. {// more arguments than necessary ( should not happen, as it is checked before )
  3557. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3558. invalid++;
  3559. break;
  3560. }
  3561. else
  3562. {
  3563. const char* name = NULL;
  3564. if( data_isreference(data) )
  3565. {// get name for variables to determine the type they refer to
  3566. name = reference_getname(data);
  3567. }
  3568. switch( type )
  3569. {
  3570. case 'v':
  3571. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3572. {// variant
  3573. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3574. script_reportdata(data);
  3575. invalid++;
  3576. }
  3577. break;
  3578. case 's':
  3579. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3580. {// string
  3581. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3582. script_reportdata(data);
  3583. invalid++;
  3584. }
  3585. break;
  3586. case 'i':
  3587. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3588. {// int ( params and constants are always int )
  3589. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3590. script_reportdata(data);
  3591. invalid++;
  3592. }
  3593. break;
  3594. case 'r':
  3595. if( !data_isreference(data) )
  3596. {// variables
  3597. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3598. script_reportdata(data);
  3599. invalid++;
  3600. }
  3601. break;
  3602. case 'l':
  3603. if( !data_islabel(data) && !data_isfunclabel(data) )
  3604. {// label
  3605. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3606. script_reportdata(data);
  3607. invalid++;
  3608. }
  3609. break;
  3610. }
  3611. }
  3612. }
  3613. if(invalid)
  3614. {
  3615. ShowDebug("Function: %s\n", get_str(func));
  3616. script_reportsrc(st);
  3617. }
  3618. }
  3619. /// Executes a buildin command.
  3620. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3621. int run_func(struct script_state *st)
  3622. {
  3623. struct script_data* data;
  3624. int i,start_sp,end_sp,func;
  3625. end_sp = st->stack->sp;// position after the last argument
  3626. for( i = end_sp-1; i > 0 ; --i )
  3627. if( st->stack->stack_data[i].type == C_ARG )
  3628. break;
  3629. if( i == 0 ) {
  3630. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3631. st->state = END;
  3632. script_reportsrc(st);
  3633. return 1;
  3634. }
  3635. start_sp = i-1;// C_NAME of the command
  3636. st->start = start_sp;
  3637. st->end = end_sp;
  3638. data = &st->stack->stack_data[st->start];
  3639. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3640. func = (int)data->u.num;
  3641. st->funcname = reference_getname(data);
  3642. } else {
  3643. ShowError("script:run_func: not a buildin command.\n");
  3644. script_reportdata(data);
  3645. script_reportsrc(st);
  3646. st->state = END;
  3647. return 1;
  3648. }
  3649. if( script_config.warn_func_mismatch_argtypes ) {
  3650. script_check_buildin_argtype(st, func);
  3651. }
  3652. if(str_data[func].func) {
  3653. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3654. if( buildin_func[str_data[func].val].deprecated ){
  3655. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3656. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3657. script_reportsrc(st);
  3658. }
  3659. #endif
  3660. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3661. script_reportsrc(st);
  3662. } else {
  3663. 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));
  3664. script_reportsrc(st);
  3665. st->state = END;
  3666. }
  3667. // Stack's datum are used when re-running functions [Eoe]
  3668. if( st->state == RERUNLINE )
  3669. return 0;
  3670. pop_stack(st, st->start, st->end);
  3671. if( st->state == RETFUNC ) {// return from a user-defined function
  3672. struct script_retinfo* ri;
  3673. int olddefsp = st->stack->defsp;
  3674. int nargs;
  3675. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3676. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3677. {
  3678. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3679. script_reportsrc(st);
  3680. st->state = END;
  3681. return 1;
  3682. }
  3683. script_free_vars(st->stack->scope.vars);
  3684. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3685. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3686. nargs = ri->nargs;
  3687. st->pos = ri->pos;
  3688. st->script = ri->script;
  3689. st->stack->scope.vars = ri->scope.vars;
  3690. st->stack->scope.arrays = ri->scope.arrays;
  3691. st->stack->defsp = ri->defsp;
  3692. memset(ri, 0, sizeof(struct script_retinfo));
  3693. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3694. st->state = GOTO;
  3695. }
  3696. return 0;
  3697. }
  3698. /*==========================================
  3699. * script execution
  3700. *------------------------------------------*/
  3701. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3702. {
  3703. struct script_state *st;
  3704. if( rootscript == NULL || pos < 0 )
  3705. return;
  3706. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3707. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3708. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3709. st = script_alloc_state(rootscript, pos, rid, oid);
  3710. run_script_main(st);
  3711. }
  3712. /**
  3713. * Free all related script code
  3714. * @param code: Script code to free
  3715. */
  3716. void script_stop_scriptinstances(struct script_code *code) {
  3717. DBIterator *iter;
  3718. struct script_state* st;
  3719. if( !active_scripts )
  3720. return; // Don't even bother.
  3721. iter = db_iterator(st_db);
  3722. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3723. if( st->script == code )
  3724. script_free_state(st);
  3725. }
  3726. dbi_destroy(iter);
  3727. }
  3728. /*==========================================
  3729. * Timer function for sleep
  3730. *------------------------------------------*/
  3731. TIMER_FUNC(run_script_timer){
  3732. struct script_state *st = (struct script_state *)data;
  3733. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3734. // If it was a player before going to sleep and there is still a unit attached to the script
  3735. if( id != 0 && st->rid ){
  3736. struct map_session_data *sd = map_id2sd(st->rid);
  3737. // Attached player is offline(logout) or another unit type(should not happen)
  3738. if( !sd ){
  3739. st->rid = 0;
  3740. st->state = END;
  3741. // Character mismatch. Cancel execution.
  3742. }else if( sd->status.char_id != id ){
  3743. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3744. script_reportsrc(st);
  3745. st->rid = 0;
  3746. st->state = END;
  3747. }
  3748. }
  3749. while (node && st->sleep.timer != INVALID_TIMER) {
  3750. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3751. script_erase_sleepdb(node);
  3752. st->sleep.timer = INVALID_TIMER;
  3753. break;
  3754. }
  3755. node = node->next;
  3756. }
  3757. if(st->state != RERUNLINE)
  3758. st->sleep.tick = 0;
  3759. run_script_main(st);
  3760. return 0;
  3761. }
  3762. /**
  3763. * Remove sleep timers from the NPC
  3764. * @param id: NPC ID
  3765. */
  3766. void script_stop_sleeptimers(int id) {
  3767. for (;;) {
  3768. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3769. if (!st)
  3770. break; // No more sleep timers
  3771. if (st->oid == id)
  3772. script_free_state(st);
  3773. }
  3774. }
  3775. /**
  3776. * Delete the specified node from sleep_db
  3777. * @param n: Linked list of sleep timers
  3778. */
  3779. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3780. struct linkdb_node *retnode;
  3781. if (!n)
  3782. return NULL;
  3783. if (!n->prev)
  3784. sleep_db = n->next;
  3785. else
  3786. n->prev->next = n->next;
  3787. if (n->next)
  3788. n->next->prev = n->prev;
  3789. retnode = n->next;
  3790. aFree(n);
  3791. return retnode;
  3792. }
  3793. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3794. ///
  3795. /// @param st Script state to detach.
  3796. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3797. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3798. {
  3799. struct map_session_data* sd;
  3800. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3801. if( sd->state.using_fake_npc ){
  3802. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, sd->fd );
  3803. sd->state.using_fake_npc = 0;
  3804. }
  3805. sd->st = st->bk_st;
  3806. sd->npc_id = st->bk_npcid;
  3807. sd->state.disable_atcommand_on_npc = 0;
  3808. if(st->bk_st) {
  3809. //Remove tag for removal.
  3810. st->bk_st = NULL;
  3811. st->bk_npcid = 0;
  3812. } else if(dequeue_event) {
  3813. #ifdef SECURE_NPCTIMEOUT
  3814. /**
  3815. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3816. **/
  3817. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3818. delete_timer(sd->npc_idle_timer,npc_secure_timeout_timer);
  3819. sd->npc_idle_timer = INVALID_TIMER;
  3820. }
  3821. #endif
  3822. npc_event_dequeue(sd);
  3823. }
  3824. }
  3825. else if(st->bk_st)
  3826. {// rid was set to 0, before detaching the script state
  3827. 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);
  3828. script_reportsrc(st->bk_st);
  3829. script_free_state(st->bk_st);
  3830. st->bk_st = NULL;
  3831. }
  3832. }
  3833. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3834. ///
  3835. /// @param st Script state to attach.
  3836. void script_attach_state(struct script_state* st){
  3837. struct map_session_data* sd;
  3838. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3839. {
  3840. if(st!=sd->st)
  3841. {
  3842. if(st->bk_st)
  3843. {// there is already a backup
  3844. 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);
  3845. }
  3846. st->bk_st = sd->st;
  3847. st->bk_npcid = sd->npc_id;
  3848. }
  3849. sd->st = st;
  3850. sd->npc_id = st->oid;
  3851. sd->npc_item_flag = st->npc_item_flag; // load default.
  3852. sd->state.disable_atcommand_on_npc = battle_config.atcommand_disable_npc && (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3853. #ifdef SECURE_NPCTIMEOUT
  3854. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  3855. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_secure_timeout_timer,sd->bl.id,0);
  3856. sd->npc_idle_tick = gettick();
  3857. #endif
  3858. }
  3859. }
  3860. /*==========================================
  3861. * The main part of the script execution
  3862. *------------------------------------------*/
  3863. void run_script_main(struct script_state *st)
  3864. {
  3865. int cmdcount = script_config.check_cmdcount;
  3866. int gotocount = script_config.check_gotocount;
  3867. TBL_PC *sd;
  3868. struct script_stack *stack = st->stack;
  3869. script_attach_state(st);
  3870. if(st->state == RERUNLINE) {
  3871. run_func(st);
  3872. if(st->state == GOTO)
  3873. st->state = RUN;
  3874. } else if(st->state != END)
  3875. st->state = RUN;
  3876. while(st->state == RUN) {
  3877. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3878. switch(c){
  3879. case C_EOL:
  3880. if( stack->defsp > stack->sp )
  3881. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3882. else
  3883. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3884. break;
  3885. case C_INT:
  3886. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3887. break;
  3888. case C_POS:
  3889. case C_NAME:
  3890. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3891. st->pos+=3;
  3892. break;
  3893. case C_ARG:
  3894. push_val(stack,c,0);
  3895. break;
  3896. case C_STR:
  3897. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3898. while(st->script->script_buf[st->pos++]);
  3899. break;
  3900. case C_FUNC:
  3901. run_func(st);
  3902. if(st->state==GOTO){
  3903. st->state = RUN;
  3904. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3905. ShowError("script:run_script_main: infinity loop !\n");
  3906. script_reportsrc(st);
  3907. st->state=END;
  3908. }
  3909. }
  3910. break;
  3911. case C_REF:
  3912. st->op2ref = 1;
  3913. break;
  3914. case C_NEG:
  3915. case C_NOT:
  3916. case C_LNOT:
  3917. op_1(st ,c);
  3918. break;
  3919. case C_ADD:
  3920. case C_SUB:
  3921. case C_MUL:
  3922. case C_DIV:
  3923. case C_MOD:
  3924. case C_EQ:
  3925. case C_NE:
  3926. case C_GT:
  3927. case C_GE:
  3928. case C_LT:
  3929. case C_LE:
  3930. case C_AND:
  3931. case C_OR:
  3932. case C_XOR:
  3933. case C_LAND:
  3934. case C_LOR:
  3935. case C_R_SHIFT:
  3936. case C_L_SHIFT:
  3937. op_2(st, c);
  3938. break;
  3939. case C_OP3:
  3940. op_3(st, c);
  3941. break;
  3942. case C_NOP:
  3943. st->state=END;
  3944. break;
  3945. default:
  3946. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3947. st->state=END;
  3948. break;
  3949. }
  3950. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3951. ShowError("script:run_script_main: infinity loop !\n");
  3952. script_reportsrc(st);
  3953. st->state=END;
  3954. }
  3955. }
  3956. if(st->sleep.tick > 0) {
  3957. //Restore previous script
  3958. script_detach_state(st, false);
  3959. //Delay execution
  3960. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3961. st->sleep.charid = sd?sd->status.char_id:0;
  3962. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3963. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3964. } else if(st->state != END && st->rid) {
  3965. //Resume later (st is already attached to player).
  3966. if(st->bk_st) {
  3967. ShowWarning("Unable to restore stack! Double continuation!\n");
  3968. //Report BOTH scripts to see if that can help somehow.
  3969. ShowDebug("Previous script (lost):\n");
  3970. script_reportsrc(st->bk_st);
  3971. ShowDebug("Current script:\n");
  3972. script_reportsrc(st);
  3973. script_free_state(st->bk_st);
  3974. st->bk_st = NULL;
  3975. }
  3976. } else {
  3977. if (st->stack && st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp - 1].type == C_RETINFO) {
  3978. for (int i = 0; i < st->stack->sp; i++) {
  3979. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  3980. st->script = st->stack->stack_data[i].u.ri->script;
  3981. break;
  3982. }
  3983. }
  3984. }
  3985. //Dispose of script.
  3986. if ((sd = map_id2sd(st->rid))!=NULL)
  3987. { //Restore previous stack and save char.
  3988. if(sd->state.using_fake_npc){
  3989. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3990. sd->state.using_fake_npc = 0;
  3991. }
  3992. //Restore previous script if any.
  3993. script_detach_state(st, true);
  3994. if (sd->vars_dirty)
  3995. intif_saveregistry(sd);
  3996. }
  3997. script_free_state(st);
  3998. }
  3999. }
  4000. int script_config_read(const char *cfgName)
  4001. {
  4002. int i;
  4003. char line[1024],w1[1024],w2[1024];
  4004. FILE *fp;
  4005. fp=fopen(cfgName,"r");
  4006. if(fp==NULL){
  4007. ShowError("File not found: %s\n", cfgName);
  4008. return 1;
  4009. }
  4010. while(fgets(line, sizeof(line), fp))
  4011. {
  4012. if(line[0] == '/' && line[1] == '/')
  4013. continue;
  4014. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  4015. if(i!=2)
  4016. continue;
  4017. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  4018. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  4019. }
  4020. else if(strcmpi(w1,"check_cmdcount")==0) {
  4021. script_config.check_cmdcount = config_switch(w2);
  4022. }
  4023. else if(strcmpi(w1,"check_gotocount")==0) {
  4024. script_config.check_gotocount = config_switch(w2);
  4025. }
  4026. else if(strcmpi(w1,"input_min_value")==0) {
  4027. script_config.input_min_value = config_switch(w2);
  4028. }
  4029. else if(strcmpi(w1,"input_max_value")==0) {
  4030. script_config.input_max_value = config_switch(w2);
  4031. }
  4032. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  4033. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  4034. }
  4035. else if(strcmpi(w1,"import")==0){
  4036. script_config_read(w2);
  4037. }
  4038. else {
  4039. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  4040. }
  4041. }
  4042. fclose(fp);
  4043. return 0;
  4044. }
  4045. /**
  4046. * @see DBApply
  4047. */
  4048. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  4049. {
  4050. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  4051. if (code)
  4052. script_free_code(code);
  4053. return 0;
  4054. }
  4055. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  4056. {
  4057. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4058. if( script )
  4059. {
  4060. int j;
  4061. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4062. if( j < EQI_MAX ) {
  4063. //Single item autobonus
  4064. current_equip_item_index = sd->equip_index[j];
  4065. current_equip_combo_pos = 0;
  4066. } else {
  4067. //Combo autobonus
  4068. current_equip_item_index = -1;
  4069. current_equip_combo_pos = pos;
  4070. }
  4071. run_script(script,0,sd->bl.id,0);
  4072. }
  4073. }
  4074. void script_add_autobonus(const char *autobonus)
  4075. {
  4076. if( strdb_get(autobonus_db, autobonus) == NULL )
  4077. {
  4078. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4079. if( script )
  4080. strdb_put(autobonus_db, autobonus, script);
  4081. }
  4082. }
  4083. /// resets a temporary character array variable to given value
  4084. void script_cleararray_pc( struct map_session_data* sd, const char* varname ){
  4085. struct script_array *sa = NULL;
  4086. struct reg_db *src = NULL;
  4087. unsigned int i, *list = NULL, size = 0;
  4088. int key;
  4089. key = add_str(varname);
  4090. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4091. return;
  4092. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4093. return;
  4094. size = sa->size;
  4095. list = script_array_cpy_list(sa);
  4096. for( i = 0; i < size; i++ ){
  4097. clear_reg( NULL, sd, reference_uid( key, list[i] ), varname, NULL );
  4098. }
  4099. }
  4100. /// sets a temporary character array variable element idx to given value
  4101. /// @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.
  4102. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, int64 value, int* refcache)
  4103. {
  4104. int key;
  4105. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4106. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4107. return;
  4108. }
  4109. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4110. set_reg_num( NULL, sd, reference_uid( key, idx ), varname, value, NULL );
  4111. if( refcache ) {
  4112. // save to avoid repeated script->add_str calls
  4113. refcache[0] = key;
  4114. }
  4115. }
  4116. /**
  4117. * Clears persistent variables from memory
  4118. **/
  4119. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4120. {
  4121. struct script_reg_state *src;
  4122. if( data->type != DB_DATA_PTR ) // got no need for those!
  4123. return 0;
  4124. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4125. if( src->type ) {
  4126. struct script_reg_str *p = (struct script_reg_str *)src;
  4127. if( p->value )
  4128. aFree(p->value);
  4129. ers_free(str_reg_ers,p);
  4130. } else {
  4131. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4132. }
  4133. return 0;
  4134. }
  4135. /**
  4136. * Clears a single persistent variable
  4137. **/
  4138. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4139. {
  4140. i64db_remove(sd->regs.vars, reg);
  4141. if( data->type ) {
  4142. struct script_reg_str *p = (struct script_reg_str*)data;
  4143. if( p->value )
  4144. aFree(p->value);
  4145. ers_free(str_reg_ers,p);
  4146. } else {
  4147. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4148. }
  4149. }
  4150. unsigned int *script_array_cpy_list(struct script_array *sa)
  4151. {
  4152. if( sa->size > generic_ui_array_size )
  4153. script_generic_ui_array_expand(sa->size);
  4154. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4155. return generic_ui_array;
  4156. }
  4157. void script_generic_ui_array_expand (unsigned int plus)
  4158. {
  4159. generic_ui_array_size += plus + 100;
  4160. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4161. }
  4162. /*==========================================
  4163. * Destructor
  4164. *------------------------------------------*/
  4165. void do_final_script() {
  4166. int i;
  4167. DBIterator *iter;
  4168. struct script_state *st;
  4169. #ifdef DEBUG_HASH
  4170. if (battle_config.etc_log)
  4171. {
  4172. FILE *fp = fopen("hash_dump.txt","wt");
  4173. if(fp) {
  4174. int count[SCRIPT_HASH_SIZE];
  4175. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4176. int n=0;
  4177. int min=INT_MAX,max=0,zero=0;
  4178. double mean=0.0f;
  4179. double median=0.0f;
  4180. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4181. memset(count, 0, sizeof(count));
  4182. fprintf(fp,"num : hash : data_name\n");
  4183. fprintf(fp,"---------------------------------------------------------------\n");
  4184. for(i=LABEL_START; i<str_num; i++) {
  4185. unsigned int h = calc_hash(get_str(i));
  4186. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4187. ++count[h];
  4188. }
  4189. fprintf(fp,"--------------------\n\n");
  4190. memset(count2, 0, sizeof(count2));
  4191. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4192. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4193. if(min > count[i])
  4194. min = count[i]; // minimun count of collision
  4195. if(max < count[i])
  4196. max = count[i]; // maximun count of collision
  4197. if(count[i] == 0)
  4198. zero++;
  4199. ++count2[count[i]];
  4200. }
  4201. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4202. for( i=min; i <= max; ++i ){
  4203. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4204. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4205. }
  4206. for( i=min; i <= max; ++i ){
  4207. n += count2[i];
  4208. if( n*2 >= SCRIPT_HASH_SIZE )
  4209. {
  4210. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4211. median = (i+i+1)/2.0f;
  4212. else
  4213. median = i;
  4214. break;
  4215. }
  4216. }
  4217. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4218. fclose(fp);
  4219. }
  4220. }
  4221. #endif
  4222. mapreg_final();
  4223. db_destroy(scriptlabel_db);
  4224. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4225. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4226. ers_destroy(array_ers);
  4227. if (generic_ui_array)
  4228. aFree(generic_ui_array);
  4229. iter = db_iterator(st_db);
  4230. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4231. script_free_state(st);
  4232. dbi_destroy(iter);
  4233. if (str_data)
  4234. aFree(str_data);
  4235. if (str_buf)
  4236. aFree(str_buf);
  4237. for( i = 0; i < atcmd_binding_count; i++ ) {
  4238. aFree(atcmd_binding[i]);
  4239. }
  4240. if( atcmd_binding_count != 0 )
  4241. aFree(atcmd_binding);
  4242. ers_destroy(st_ers);
  4243. ers_destroy(stack_ers);
  4244. db_destroy(st_db);
  4245. }
  4246. /*==========================================
  4247. * Initialization
  4248. *------------------------------------------*/
  4249. void do_init_script(void) {
  4250. st_db = idb_alloc(DB_OPT_BASE);
  4251. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4252. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4253. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4254. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4255. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4256. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4257. ers_chunk_size(st_ers, 10);
  4258. ers_chunk_size(stack_ers, 10);
  4259. active_scripts = 0;
  4260. next_id = 0;
  4261. mapreg_init();
  4262. add_buildin_func();
  4263. constant_db.load();
  4264. script_hardcoded_constants();
  4265. }
  4266. void script_reload(void) {
  4267. int i;
  4268. DBIterator *iter;
  4269. struct script_state *st;
  4270. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4271. db_clear(scriptlabel_db);
  4272. // @commands (script based)
  4273. // Clear bindings
  4274. for( i = 0; i < atcmd_binding_count; i++ ) {
  4275. aFree(atcmd_binding[i]);
  4276. }
  4277. if( atcmd_binding_count != 0 )
  4278. aFree(atcmd_binding);
  4279. atcmd_binding_count = 0;
  4280. iter = db_iterator(st_db);
  4281. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4282. script_free_state(st);
  4283. dbi_destroy(iter);
  4284. db_clear(st_db);
  4285. mapreg_reload();
  4286. }
  4287. //-----------------------------------------------------------------------------
  4288. // buildin functions
  4289. //
  4290. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4291. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4292. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4293. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4294. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4295. /////////////////////////////////////////////////////////////////////
  4296. // NPC interaction
  4297. //
  4298. /// Appends a message to the npc dialog.
  4299. /// If a dialog doesn't exist yet, one is created.
  4300. ///
  4301. /// mes "<message>";
  4302. BUILDIN_FUNC(mes)
  4303. {
  4304. TBL_PC* sd;
  4305. if( !script_rid2sd(sd) )
  4306. return SCRIPT_CMD_SUCCESS;
  4307. if( !script_hasdata(st, 3) )
  4308. {// only a single line detected in the script
  4309. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4310. }
  4311. else
  4312. {// parse multiple lines as they exist
  4313. int i;
  4314. for( i = 2; script_hasdata(st, i); i++ )
  4315. {
  4316. // send the message to the client
  4317. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4318. }
  4319. }
  4320. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4321. return SCRIPT_CMD_SUCCESS;
  4322. }
  4323. /// Displays the button 'next' in the npc dialog.
  4324. /// The dialog text is cleared and the script continues when the button is pressed.
  4325. ///
  4326. /// next;
  4327. BUILDIN_FUNC(next)
  4328. {
  4329. TBL_PC* sd;
  4330. if (!st->mes_active) {
  4331. ShowWarning("buildin_next: There is no mes active.\n");
  4332. return SCRIPT_CMD_FAILURE;
  4333. }
  4334. if( !script_rid2sd(sd) )
  4335. return SCRIPT_CMD_SUCCESS;
  4336. #ifdef SECURE_NPCTIMEOUT
  4337. sd->npc_idle_type = NPCT_WAIT;
  4338. #endif
  4339. st->state = STOP;
  4340. clif_scriptnext(sd, st->oid);
  4341. return SCRIPT_CMD_SUCCESS;
  4342. }
  4343. /// Clears the dialog and continues the script without a next button.
  4344. ///
  4345. /// clear;
  4346. BUILDIN_FUNC(clear)
  4347. {
  4348. TBL_PC* sd;
  4349. if (!st->mes_active) {
  4350. ShowWarning("buildin_clear: There is no mes active.\n");
  4351. return SCRIPT_CMD_FAILURE;
  4352. }
  4353. if (!script_rid2sd(sd))
  4354. return SCRIPT_CMD_FAILURE;
  4355. clif_scriptclear(sd, st->oid);
  4356. return SCRIPT_CMD_SUCCESS;
  4357. }
  4358. /// Ends the script and displays the button 'close' on the npc dialog.
  4359. /// The dialog is closed when the button is pressed.
  4360. ///
  4361. /// close;
  4362. BUILDIN_FUNC(close)
  4363. {
  4364. TBL_PC* sd;
  4365. if( !script_rid2sd(sd) )
  4366. return SCRIPT_CMD_SUCCESS;
  4367. if( !st->mes_active ) {
  4368. st->state = END; // Keep backwards compatibility.
  4369. ShowWarning("Incorrect use of 'close' command!\n");
  4370. script_reportsrc(st);
  4371. } else {
  4372. st->state = CLOSE;
  4373. st->mes_active = 0;
  4374. }
  4375. clif_scriptclose(sd, st->oid);
  4376. return SCRIPT_CMD_SUCCESS;
  4377. }
  4378. /// Displays the button 'close' on the npc dialog.
  4379. /// The dialog is closed and the script continues when the button is pressed.
  4380. ///
  4381. /// close2;
  4382. BUILDIN_FUNC(close2)
  4383. {
  4384. TBL_PC* sd;
  4385. if( !script_rid2sd(sd) )
  4386. return SCRIPT_CMD_SUCCESS;
  4387. st->state = STOP;
  4388. if( st->mes_active )
  4389. st->mes_active = 0;
  4390. clif_scriptclose(sd, st->oid);
  4391. return SCRIPT_CMD_SUCCESS;
  4392. }
  4393. /// Counts the number of valid and total number of options in 'str'
  4394. /// If max_count > 0 the counting stops when that valid option is reached
  4395. /// total is incremented for each option (NULL is supported)
  4396. static int menu_countoptions(const char* str, int max_count, int* total)
  4397. {
  4398. int count = 0;
  4399. int bogus_total;
  4400. if( total == NULL )
  4401. total = &bogus_total;
  4402. ++(*total);
  4403. // initial empty options
  4404. while( *str == ':' )
  4405. {
  4406. ++str;
  4407. ++(*total);
  4408. }
  4409. // count menu options
  4410. while( *str != '\0' )
  4411. {
  4412. ++count;
  4413. --max_count;
  4414. if( max_count == 0 )
  4415. break;
  4416. while( *str != ':' && *str != '\0' )
  4417. ++str;
  4418. while( *str == ':' )
  4419. {
  4420. ++str;
  4421. ++(*total);
  4422. }
  4423. }
  4424. return count;
  4425. }
  4426. /// Displays a menu with options and goes to the target label.
  4427. /// The script is stopped if cancel is pressed.
  4428. /// Options with no text are not displayed in the client.
  4429. ///
  4430. /// Options can be grouped together, separated by the character ':' in the text:
  4431. /// ex: menu "A:B:C",L_target;
  4432. /// All these options go to the specified target label.
  4433. ///
  4434. /// The index of the selected option is put in the variable @menu.
  4435. /// Indexes start with 1 and are consistent with grouped and empty options.
  4436. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4437. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4438. ///
  4439. /// NOTE: the client closes the npc dialog when cancel is pressed
  4440. ///
  4441. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4442. BUILDIN_FUNC(menu)
  4443. {
  4444. int i;
  4445. const char* text;
  4446. TBL_PC* sd;
  4447. if( !script_rid2sd(sd) )
  4448. return SCRIPT_CMD_SUCCESS;
  4449. #ifdef SECURE_NPCTIMEOUT
  4450. sd->npc_idle_type = NPCT_MENU;
  4451. #endif
  4452. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4453. if( sd->state.menu_or_input == 0 )
  4454. {
  4455. struct StringBuf buf;
  4456. if( script_lastdata(st) % 2 == 0 )
  4457. {// argument count is not even (1st argument is at index 2)
  4458. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4459. st->state = END;
  4460. return SCRIPT_CMD_FAILURE;
  4461. }
  4462. StringBuf_Init(&buf);
  4463. sd->npc_menu = 0;
  4464. for( i = 2; i < script_lastdata(st); i += 2 )
  4465. {
  4466. struct script_data* data;
  4467. // menu options
  4468. text = script_getstr(st, i);
  4469. // target label
  4470. data = script_getdata(st, i+1);
  4471. if( !data_islabel(data) )
  4472. {// not a label
  4473. StringBuf_Destroy(&buf);
  4474. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4475. script_reportdata(data);
  4476. st->state = END;
  4477. return SCRIPT_CMD_FAILURE;
  4478. }
  4479. // append option(s)
  4480. if( text[0] == '\0' )
  4481. continue;// empty string, ignore
  4482. if( sd->npc_menu > 0 )
  4483. StringBuf_AppendStr(&buf, ":");
  4484. StringBuf_AppendStr(&buf, text);
  4485. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4486. }
  4487. st->state = RERUNLINE;
  4488. sd->state.menu_or_input = 1;
  4489. /**
  4490. * menus beyond this length crash the client (see bugreport:6402)
  4491. **/
  4492. if( StringBuf_Length(&buf) >= 2047 ) {
  4493. struct npc_data * nd = map_id2nd(st->oid);
  4494. char* menu;
  4495. CREATE(menu, char, 2048);
  4496. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4497. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4498. clif_scriptmenu(sd, st->oid, menu);
  4499. aFree(menu);
  4500. } else
  4501. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4502. StringBuf_Destroy(&buf);
  4503. if( sd->npc_menu >= 0xff )
  4504. {// 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
  4505. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4506. script_reportsrc(st);
  4507. }
  4508. }
  4509. else if( sd->npc_menu == 0xff )
  4510. {// Cancel was pressed
  4511. sd->state.menu_or_input = 0;
  4512. st->state = END;
  4513. }
  4514. else
  4515. {// goto target label
  4516. int menu = 0;
  4517. sd->state.menu_or_input = 0;
  4518. if( sd->npc_menu <= 0 )
  4519. {
  4520. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4521. st->state = END;
  4522. return SCRIPT_CMD_FAILURE;
  4523. }
  4524. // get target label
  4525. for( i = 2; i < script_lastdata(st); i += 2 )
  4526. {
  4527. text = script_getstr(st, i);
  4528. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4529. if( sd->npc_menu <= 0 )
  4530. break;// entry found
  4531. }
  4532. if( sd->npc_menu > 0 )
  4533. {// Invalid selection
  4534. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4535. st->state = END;
  4536. return SCRIPT_CMD_FAILURE;
  4537. }
  4538. if( !data_islabel(script_getdata(st, i + 1)) )
  4539. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4540. ShowError("buildin_menu: Unexpected data in label argument\n");
  4541. script_reportdata(script_getdata(st, i + 1));
  4542. st->state = END;
  4543. return SCRIPT_CMD_FAILURE;
  4544. }
  4545. pc_setreg(sd, add_str("@menu"), menu);
  4546. st->pos = script_getnum(st, i + 1);
  4547. st->state = GOTO;
  4548. }
  4549. return SCRIPT_CMD_SUCCESS;
  4550. }
  4551. /// Displays a menu with options and returns the selected option.
  4552. /// Behaves like 'menu' without the target labels.
  4553. ///
  4554. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4555. ///
  4556. /// @see menu
  4557. BUILDIN_FUNC(select)
  4558. {
  4559. int i;
  4560. const char* text;
  4561. TBL_PC* sd;
  4562. if( !script_rid2sd(sd) )
  4563. return SCRIPT_CMD_SUCCESS;
  4564. #ifdef SECURE_NPCTIMEOUT
  4565. sd->npc_idle_type = NPCT_MENU;
  4566. #endif
  4567. if( sd->state.menu_or_input == 0 ) {
  4568. struct StringBuf buf;
  4569. StringBuf_Init(&buf);
  4570. sd->npc_menu = 0;
  4571. for( i = 2; i <= script_lastdata(st); ++i ) {
  4572. text = script_getstr(st, i);
  4573. if( sd->npc_menu > 0 )
  4574. StringBuf_AppendStr(&buf, ":");
  4575. StringBuf_AppendStr(&buf, text);
  4576. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4577. }
  4578. st->state = RERUNLINE;
  4579. sd->state.menu_or_input = 1;
  4580. /**
  4581. * menus beyond this length crash the client (see bugreport:6402)
  4582. **/
  4583. if( StringBuf_Length(&buf) >= 2047 ) {
  4584. struct npc_data * nd = map_id2nd(st->oid);
  4585. char* menu;
  4586. CREATE(menu, char, 2048);
  4587. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4588. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4589. clif_scriptmenu(sd, st->oid, menu);
  4590. aFree(menu);
  4591. } else
  4592. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4593. StringBuf_Destroy(&buf);
  4594. if( sd->npc_menu >= 0xff ) {
  4595. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4596. script_reportsrc(st);
  4597. }
  4598. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4599. sd->state.menu_or_input = 0;
  4600. st->state = END;
  4601. } else {// return selected option
  4602. int menu = 0;
  4603. sd->state.menu_or_input = 0;
  4604. for( i = 2; i <= script_lastdata(st); ++i ) {
  4605. text = script_getstr(st, i);
  4606. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4607. if( sd->npc_menu <= 0 )
  4608. break;// entry found
  4609. }
  4610. pc_setreg(sd, add_str("@menu"), menu);
  4611. script_pushint(st, menu);
  4612. st->state = RUN;
  4613. }
  4614. return SCRIPT_CMD_SUCCESS;
  4615. }
  4616. /// Displays a menu with options and returns the selected option.
  4617. /// Behaves like 'menu' without the target labels, except when cancel is
  4618. /// pressed.
  4619. /// When cancel is pressed, the script continues and 255 is returned.
  4620. ///
  4621. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4622. ///
  4623. /// @see menu
  4624. BUILDIN_FUNC(prompt)
  4625. {
  4626. int i;
  4627. const char *text;
  4628. TBL_PC* sd;
  4629. if( !script_rid2sd(sd) )
  4630. return SCRIPT_CMD_SUCCESS;
  4631. #ifdef SECURE_NPCTIMEOUT
  4632. sd->npc_idle_type = NPCT_MENU;
  4633. #endif
  4634. if( sd->state.menu_or_input == 0 )
  4635. {
  4636. struct StringBuf buf;
  4637. StringBuf_Init(&buf);
  4638. sd->npc_menu = 0;
  4639. for( i = 2; i <= script_lastdata(st); ++i )
  4640. {
  4641. text = script_getstr(st, i);
  4642. if( sd->npc_menu > 0 )
  4643. StringBuf_AppendStr(&buf, ":");
  4644. StringBuf_AppendStr(&buf, text);
  4645. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4646. }
  4647. st->state = RERUNLINE;
  4648. sd->state.menu_or_input = 1;
  4649. /**
  4650. * menus beyond this length crash the client (see bugreport:6402)
  4651. **/
  4652. if( StringBuf_Length(&buf) >= 2047 ) {
  4653. struct npc_data * nd = map_id2nd(st->oid);
  4654. char* menu;
  4655. CREATE(menu, char, 2048);
  4656. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4657. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4658. clif_scriptmenu(sd, st->oid, menu);
  4659. aFree(menu);
  4660. } else
  4661. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4662. StringBuf_Destroy(&buf);
  4663. if( sd->npc_menu >= 0xff )
  4664. {
  4665. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4666. script_reportsrc(st);
  4667. }
  4668. }
  4669. else if( sd->npc_menu == 0xff )
  4670. {// Cancel was pressed
  4671. sd->state.menu_or_input = 0;
  4672. pc_setreg(sd, add_str("@menu"), 0xff);
  4673. script_pushint(st, 0xff);
  4674. st->state = RUN;
  4675. }
  4676. else
  4677. {// return selected option
  4678. int menu = 0;
  4679. sd->state.menu_or_input = 0;
  4680. for( i = 2; i <= script_lastdata(st); ++i )
  4681. {
  4682. text = script_getstr(st, i);
  4683. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4684. if( sd->npc_menu <= 0 )
  4685. break;// entry found
  4686. }
  4687. pc_setreg(sd, add_str("@menu"), menu);
  4688. script_pushint(st, menu);
  4689. st->state = RUN;
  4690. }
  4691. return SCRIPT_CMD_SUCCESS;
  4692. }
  4693. /////////////////////////////////////////////////////////////////////
  4694. // ...
  4695. //
  4696. /// Jumps to the target script label.
  4697. ///
  4698. /// goto <label>;
  4699. BUILDIN_FUNC(goto)
  4700. {
  4701. if( !data_islabel(script_getdata(st,2)) )
  4702. {
  4703. ShowError("buildin_goto: Not a label\n");
  4704. script_reportdata(script_getdata(st,2));
  4705. st->state = END;
  4706. return SCRIPT_CMD_FAILURE;
  4707. }
  4708. st->pos = script_getnum(st,2);
  4709. st->state = GOTO;
  4710. return SCRIPT_CMD_SUCCESS;
  4711. }
  4712. /*==========================================
  4713. * user-defined function call
  4714. *------------------------------------------*/
  4715. BUILDIN_FUNC(callfunc)
  4716. {
  4717. int i, j;
  4718. struct script_retinfo* ri;
  4719. struct script_code* scr;
  4720. const char* str = script_getstr(st,2);
  4721. struct reg_db *ref = NULL;
  4722. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4723. if(!scr) {
  4724. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4725. st->state = END;
  4726. return SCRIPT_CMD_FAILURE;
  4727. }
  4728. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4729. ref[0].vars = st->stack->scope.vars;
  4730. if (!st->stack->scope.arrays)
  4731. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4732. ref[0].arrays = st->stack->scope.arrays;
  4733. ref[1].vars = st->script->local.vars;
  4734. if (!st->script->local.arrays)
  4735. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4736. ref[1].arrays = st->script->local.arrays;
  4737. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4738. struct script_data* data = push_copy(st->stack,i);
  4739. if (data_isreference(data) && !data->ref) {
  4740. const char* name = reference_getname(data);
  4741. if (name[0] == '.')
  4742. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4743. }
  4744. }
  4745. CREATE(ri, struct script_retinfo, 1);
  4746. ri->script = st->script; // script code
  4747. ri->scope.vars = st->stack->scope.vars; // scope variables
  4748. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4749. ri->pos = st->pos; // script location
  4750. ri->nargs = j; // argument count
  4751. ri->defsp = st->stack->defsp; // default stack pointer
  4752. push_retinfo(st->stack, ri, ref);
  4753. st->pos = 0;
  4754. st->script = scr;
  4755. st->stack->defsp = st->stack->sp;
  4756. st->state = GOTO;
  4757. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4758. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4759. if (!st->script->local.vars)
  4760. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4761. return SCRIPT_CMD_SUCCESS;
  4762. }
  4763. /*==========================================
  4764. * subroutine call
  4765. *------------------------------------------*/
  4766. BUILDIN_FUNC(callsub)
  4767. {
  4768. int i,j;
  4769. struct script_retinfo* ri;
  4770. int pos = script_getnum(st,2);
  4771. struct reg_db *ref = NULL;
  4772. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4773. ShowError("buildin_callsub: Argument is not a label\n");
  4774. script_reportdata(script_getdata(st,2));
  4775. st->state = END;
  4776. return SCRIPT_CMD_FAILURE;
  4777. }
  4778. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4779. ref[0].vars = st->stack->scope.vars;
  4780. if (!st->stack->scope.arrays)
  4781. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4782. ref[0].arrays = st->stack->scope.arrays;
  4783. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4784. struct script_data* data = push_copy(st->stack,i);
  4785. if (data_isreference(data) && !data->ref) {
  4786. const char* name = reference_getname(data);
  4787. if (name[0] == '.' && name[1] == '@')
  4788. data->ref = &ref[0];
  4789. }
  4790. }
  4791. CREATE(ri, struct script_retinfo, 1);
  4792. ri->script = st->script; // script code
  4793. ri->scope.vars = st->stack->scope.vars; // scope variables
  4794. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4795. ri->pos = st->pos; // script location
  4796. ri->nargs = j; // argument count
  4797. ri->defsp = st->stack->defsp; // default stack pointer
  4798. push_retinfo(st->stack, ri, ref);
  4799. st->pos = pos;
  4800. st->stack->defsp = st->stack->sp;
  4801. st->state = GOTO;
  4802. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4803. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4804. return SCRIPT_CMD_SUCCESS;
  4805. }
  4806. /// Retrieves an argument provided to callfunc/callsub.
  4807. /// If the argument doesn't exist
  4808. ///
  4809. /// getarg(<index>{,<default_value>}) -> <value>
  4810. BUILDIN_FUNC(getarg)
  4811. {
  4812. struct script_retinfo* ri;
  4813. int idx;
  4814. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4815. {
  4816. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4817. st->state = END;
  4818. return SCRIPT_CMD_FAILURE;
  4819. }
  4820. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4821. idx = script_getnum(st,2);
  4822. if( idx >= 0 && idx < ri->nargs )
  4823. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4824. else if( script_hasdata(st,3) )
  4825. script_pushcopy(st, 3);
  4826. else
  4827. {
  4828. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4829. st->state = END;
  4830. return SCRIPT_CMD_FAILURE;
  4831. }
  4832. return SCRIPT_CMD_SUCCESS;
  4833. }
  4834. /// Returns from the current function, optionaly returning a value from the functions.
  4835. /// Don't use outside script functions.
  4836. ///
  4837. /// return;
  4838. /// return <value>;
  4839. BUILDIN_FUNC(return)
  4840. {
  4841. if( script_hasdata(st,2) )
  4842. {// return value
  4843. struct script_data* data;
  4844. script_pushcopy(st, 2);
  4845. data = script_getdatatop(st, -1);
  4846. if( data_isreference(data) ) {
  4847. const char* name = reference_getname(data);
  4848. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4849. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4850. get_val(st, data); // current scope, convert to value
  4851. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4852. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4853. }
  4854. }
  4855. }
  4856. else
  4857. {// no return value
  4858. script_pushnil(st);
  4859. }
  4860. st->state = RETFUNC;
  4861. return SCRIPT_CMD_SUCCESS;
  4862. }
  4863. /// Returns a random number from 0 to <range>-1.
  4864. /// Or returns a random number from <min> to <max>.
  4865. /// If <min> is greater than <max>, their numbers are switched.
  4866. /// rand(<range>) -> <int>
  4867. /// rand(<min>,<max>) -> <int>
  4868. BUILDIN_FUNC(rand)
  4869. {
  4870. int range;
  4871. int min;
  4872. if( script_hasdata(st,3) )
  4873. {// min,max
  4874. int max = script_getnum(st,3);
  4875. min = script_getnum(st,2);
  4876. if( max < min )
  4877. SWAP(min, max);
  4878. range = max - min + 1;
  4879. }
  4880. else
  4881. {// range
  4882. min = 0;
  4883. range = script_getnum(st,2);
  4884. }
  4885. if( range <= 1 )
  4886. script_pushint(st, min);
  4887. else
  4888. script_pushint(st, rnd()%range + min);
  4889. return SCRIPT_CMD_SUCCESS;
  4890. }
  4891. /*==========================================
  4892. * Warp sd to str,x,y or Random or SavePoint/Save
  4893. *------------------------------------------*/
  4894. BUILDIN_FUNC(warp)
  4895. {
  4896. int ret;
  4897. int x,y;
  4898. const char* str;
  4899. struct map_session_data* sd;
  4900. if(!script_charid2sd(5, sd))
  4901. return SCRIPT_CMD_SUCCESS;
  4902. str = script_getstr(st,2);
  4903. x = script_getnum(st,3);
  4904. y = script_getnum(st,4);
  4905. if(strcmp(str,"Random")==0)
  4906. ret = pc_randomwarp(sd,CLR_TELEPORT,true);
  4907. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4908. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4909. else
  4910. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4911. if( ret ) {
  4912. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4913. return SCRIPT_CMD_FAILURE;
  4914. }
  4915. return SCRIPT_CMD_SUCCESS;
  4916. }
  4917. /*==========================================
  4918. * Warp a specified area
  4919. *------------------------------------------*/
  4920. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4921. {
  4922. int x2,y2,x3,y3;
  4923. unsigned int index;
  4924. index = va_arg(ap,unsigned int);
  4925. x2 = va_arg(ap,int);
  4926. y2 = va_arg(ap,int);
  4927. x3 = va_arg(ap,int);
  4928. y3 = va_arg(ap,int);
  4929. if(index == 0)
  4930. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT,true);
  4931. else if(x3 && y3) {
  4932. int max, tx, ty, j = 0;
  4933. int16 m;
  4934. m = map_mapindex2mapid(index);
  4935. // choose a suitable max number of attempts
  4936. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4937. max = 1000;
  4938. // find a suitable map cell
  4939. do {
  4940. tx = rnd()%(x3-x2+1)+x2;
  4941. ty = rnd()%(y3-y2+1)+y2;
  4942. j++;
  4943. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4944. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4945. }
  4946. else
  4947. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4948. return 0;
  4949. }
  4950. BUILDIN_FUNC(areawarp)
  4951. {
  4952. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4953. unsigned int index;
  4954. const char *str;
  4955. const char *mapname;
  4956. mapname = script_getstr(st,2);
  4957. x0 = script_getnum(st,3);
  4958. y0 = script_getnum(st,4);
  4959. x1 = script_getnum(st,5);
  4960. y1 = script_getnum(st,6);
  4961. str = script_getstr(st,7);
  4962. x2 = script_getnum(st,8);
  4963. y2 = script_getnum(st,9);
  4964. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4965. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4966. x3 = 0;
  4967. y3 = 0;
  4968. } else if( x3 && y3 ) {
  4969. // normalize x3/y3 coordinates
  4970. if( x3 < x2 ) SWAP(x3,x2);
  4971. if( y3 < y2 ) SWAP(y3,y2);
  4972. }
  4973. }
  4974. if( (m = map_mapname2mapid(mapname)) < 0 )
  4975. return SCRIPT_CMD_FAILURE;
  4976. if( strcmp(str,"Random") == 0 )
  4977. index = 0;
  4978. else if( !(index=mapindex_name2id(str)) )
  4979. return SCRIPT_CMD_FAILURE;
  4980. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4981. return SCRIPT_CMD_SUCCESS;
  4982. }
  4983. /*==========================================
  4984. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4985. *------------------------------------------*/
  4986. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4987. {
  4988. int hp, sp;
  4989. hp = va_arg(ap, int);
  4990. sp = va_arg(ap, int);
  4991. pc_percentheal((TBL_PC *)bl,hp,sp);
  4992. return 0;
  4993. }
  4994. BUILDIN_FUNC(areapercentheal)
  4995. {
  4996. int hp,sp,m;
  4997. const char *mapname;
  4998. int x0,y0,x1,y1;
  4999. mapname=script_getstr(st,2);
  5000. x0=script_getnum(st,3);
  5001. y0=script_getnum(st,4);
  5002. x1=script_getnum(st,5);
  5003. y1=script_getnum(st,6);
  5004. hp=script_getnum(st,7);
  5005. sp=script_getnum(st,8);
  5006. if( (m=map_mapname2mapid(mapname))< 0)
  5007. return SCRIPT_CMD_FAILURE;
  5008. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5009. return SCRIPT_CMD_SUCCESS;
  5010. }
  5011. /*==========================================
  5012. * Warpparty - [Fredzilla] [Paradox924X]
  5013. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5014. * If 'from_mapname' is specified, only the party members on that map will be warped
  5015. *------------------------------------------*/
  5016. BUILDIN_FUNC(warpparty)
  5017. {
  5018. TBL_PC *sd = NULL;
  5019. TBL_PC *pl_sd;
  5020. struct party_data* p;
  5021. int type, mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5022. const char* str = script_getstr(st,2);
  5023. int x = script_getnum(st,3);
  5024. int y = script_getnum(st,4);
  5025. int p_id = script_getnum(st,5);
  5026. const char* str2 = NULL;
  5027. if ( script_hasdata(st,6) )
  5028. str2 = script_getstr(st,6);
  5029. if (script_hasdata(st, 7))
  5030. rx = script_getnum(st, 7);
  5031. if (script_hasdata(st, 8))
  5032. ry = script_getnum(st, 8);
  5033. p = party_search(p_id);
  5034. if(!p)
  5035. return SCRIPT_CMD_SUCCESS;
  5036. type = ( strcmp(str,"Random")==0 ) ? 0
  5037. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5038. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5039. : ( strcmp(str,"Leader")==0 ) ? 3
  5040. : 4;
  5041. switch (type)
  5042. {
  5043. case 3:
  5044. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5045. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5046. return SCRIPT_CMD_FAILURE;
  5047. pl_sd = p->data[i].sd;
  5048. mapindex = pl_sd->mapindex;
  5049. m = map_mapindex2mapid(mapindex);
  5050. x = pl_sd->bl.x;
  5051. y = pl_sd->bl.y;
  5052. break;
  5053. case 4:
  5054. mapindex = mapindex_name2id(str);
  5055. if (!mapindex) {// Invalid map
  5056. return SCRIPT_CMD_FAILURE;
  5057. }
  5058. m = map_mapindex2mapid(mapindex);
  5059. break;
  5060. case 2:
  5061. //"SavePoint" uses save point of the currently attached player
  5062. if ( !script_rid2sd(sd) )
  5063. return SCRIPT_CMD_SUCCESS;
  5064. break;
  5065. }
  5066. for (i = 0; i < MAX_PARTY; i++)
  5067. {
  5068. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5069. continue;
  5070. if( str2 && strcmp(str2, map_getmapdata(pl_sd->bl.m)->name) != 0 )
  5071. continue;
  5072. if( pc_isdead(pl_sd) )
  5073. continue;
  5074. switch( type )
  5075. {
  5076. case 0: // Random
  5077. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5078. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5079. break;
  5080. case 1: // SavePointAll
  5081. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5082. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5083. break;
  5084. case 2: // SavePoint
  5085. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5086. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5087. break;
  5088. case 3: // Leader
  5089. if (p->party.member[i].leader)
  5090. continue;
  5091. case 4: // m,x,y
  5092. if (rx || ry) {
  5093. int x1 = x + rx, y1 = y + ry,
  5094. x0 = x - rx, y0 = y - ry;
  5095. uint8 attempts = 10;
  5096. do {
  5097. x = x0 + rnd()%(x1 - x0 + 1);
  5098. y = y0 + rnd()%(y1 - y0 + 1);
  5099. } while ((--attempts) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  5100. }
  5101. 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, pl_sd->group_level))
  5102. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5103. break;
  5104. }
  5105. }
  5106. return SCRIPT_CMD_SUCCESS;
  5107. }
  5108. /*==========================================
  5109. * Warpguild - [Fredzilla]
  5110. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5111. *------------------------------------------*/
  5112. BUILDIN_FUNC(warpguild)
  5113. {
  5114. TBL_PC *sd = NULL;
  5115. TBL_PC *pl_sd;
  5116. struct guild* g;
  5117. struct s_mapiterator* iter;
  5118. int type, mapindex = 0, m = -1;
  5119. const char* str = script_getstr(st,2);
  5120. int x = script_getnum(st,3);
  5121. int y = script_getnum(st,4);
  5122. int gid = script_getnum(st,5);
  5123. g = guild_search(gid);
  5124. if( g == NULL )
  5125. return SCRIPT_CMD_SUCCESS;
  5126. type = ( strcmp(str,"Random")==0 ) ? 0
  5127. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5128. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5129. : 3;
  5130. if( type == 2 && !script_rid2sd(sd) )
  5131. {// "SavePoint" uses save point of the currently attached player
  5132. return SCRIPT_CMD_SUCCESS;
  5133. }
  5134. switch (type) {
  5135. case 3:
  5136. mapindex = mapindex_name2id(str);
  5137. if (!mapindex)
  5138. return SCRIPT_CMD_FAILURE;
  5139. m = map_mapindex2mapid(mapindex);
  5140. break;
  5141. }
  5142. iter = mapit_getallusers();
  5143. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5144. {
  5145. if( pl_sd->status.guild_id != gid )
  5146. continue;
  5147. switch( type )
  5148. {
  5149. case 0: // Random
  5150. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5151. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5152. break;
  5153. case 1: // SavePointAll
  5154. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5155. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5156. break;
  5157. case 2: // SavePoint
  5158. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5159. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5160. break;
  5161. case 3: // m,x,y
  5162. 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, pl_sd->group_level))
  5163. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5164. break;
  5165. }
  5166. }
  5167. mapit_free(iter);
  5168. return SCRIPT_CMD_SUCCESS;
  5169. }
  5170. /*==========================================
  5171. * Force Heal a player (hp and sp)
  5172. *------------------------------------------*/
  5173. BUILDIN_FUNC(heal)
  5174. {
  5175. TBL_PC *sd;
  5176. int hp,sp;
  5177. if (!script_charid2sd(4,sd))
  5178. return SCRIPT_CMD_SUCCESS;
  5179. hp=script_getnum(st,2);
  5180. sp=script_getnum(st,3);
  5181. status_heal(&sd->bl, hp, sp, 1);
  5182. return SCRIPT_CMD_SUCCESS;
  5183. }
  5184. /*==========================================
  5185. * Heal a player by item (get vit bonus etc)
  5186. *------------------------------------------*/
  5187. BUILDIN_FUNC(itemheal)
  5188. {
  5189. TBL_PC *sd;
  5190. int hp,sp;
  5191. hp=script_getnum(st,2);
  5192. sp=script_getnum(st,3);
  5193. if(potion_flag==1) {
  5194. potion_hp = hp;
  5195. potion_sp = sp;
  5196. return SCRIPT_CMD_SUCCESS;
  5197. }
  5198. if (!script_charid2sd(4,sd))
  5199. return SCRIPT_CMD_SUCCESS;
  5200. pc_itemheal(sd,sd->itemid,hp,sp);
  5201. return SCRIPT_CMD_SUCCESS;
  5202. }
  5203. /*==========================================
  5204. *
  5205. *------------------------------------------*/
  5206. BUILDIN_FUNC(percentheal)
  5207. {
  5208. int hp,sp;
  5209. TBL_PC* sd;
  5210. hp=script_getnum(st,2);
  5211. sp=script_getnum(st,3);
  5212. if(potion_flag==1) {
  5213. potion_per_hp = hp;
  5214. potion_per_sp = sp;
  5215. return SCRIPT_CMD_SUCCESS;
  5216. }
  5217. if (!script_charid2sd(4,sd))
  5218. return SCRIPT_CMD_SUCCESS;
  5219. #ifdef RENEWAL
  5220. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5221. sp = 0;
  5222. #endif
  5223. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5224. hp = 0;
  5225. sp = 0;
  5226. }
  5227. if (sd->sc.data[SC_BITESCAR])
  5228. hp = 0;
  5229. pc_percentheal(sd,hp,sp);
  5230. return SCRIPT_CMD_SUCCESS;
  5231. }
  5232. /*==========================================
  5233. *
  5234. *------------------------------------------*/
  5235. BUILDIN_FUNC(jobchange)
  5236. {
  5237. int job, upper=-1;
  5238. job=script_getnum(st,2);
  5239. if( script_hasdata(st,3) )
  5240. upper=script_getnum(st,3);
  5241. if (pcdb_checkid(job))
  5242. {
  5243. TBL_PC* sd;
  5244. if (!script_charid2sd(4,sd))
  5245. return SCRIPT_CMD_SUCCESS;
  5246. pc_jobchange(sd, job, upper);
  5247. }
  5248. return SCRIPT_CMD_SUCCESS;
  5249. }
  5250. /*==========================================
  5251. *
  5252. *------------------------------------------*/
  5253. BUILDIN_FUNC(jobname)
  5254. {
  5255. int class_=script_getnum(st,2);
  5256. script_pushconststr(st, (char*)job_name(class_));
  5257. return SCRIPT_CMD_SUCCESS;
  5258. }
  5259. /// Get input from the player.
  5260. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5261. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5262. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5263. /// shorter than 'min' and 0 otherwise.
  5264. ///
  5265. /// input(<var>{,<min>{,<max>}}) -> <int>
  5266. BUILDIN_FUNC(input)
  5267. {
  5268. TBL_PC* sd;
  5269. struct script_data* data;
  5270. int64 uid;
  5271. const char* name;
  5272. int min;
  5273. int max;
  5274. if( !script_rid2sd(sd) )
  5275. return SCRIPT_CMD_SUCCESS;
  5276. data = script_getdata(st,2);
  5277. if( !data_isreference(data) ){
  5278. ShowError("script:input: not a variable\n");
  5279. script_reportdata(data);
  5280. st->state = END;
  5281. return SCRIPT_CMD_FAILURE;
  5282. }
  5283. uid = reference_getuid(data);
  5284. name = reference_getname(data);
  5285. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5286. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5287. #ifdef SECURE_NPCTIMEOUT
  5288. sd->npc_idle_type = NPCT_WAIT;
  5289. #endif
  5290. if( !sd->state.menu_or_input )
  5291. { // first invocation, display npc input box
  5292. sd->state.menu_or_input = 1;
  5293. st->state = RERUNLINE;
  5294. if( is_string_variable(name) )
  5295. clif_scriptinputstr(sd,st->oid);
  5296. else
  5297. clif_scriptinput(sd,st->oid);
  5298. }
  5299. else
  5300. { // take received text/value and store it in the designated variable
  5301. sd->state.menu_or_input = 0;
  5302. if( is_string_variable(name) )
  5303. {
  5304. int len = (int)strlen(sd->npc_str);
  5305. set_reg_str(st, sd, uid, name, sd->npc_str, script_getref(st,2));
  5306. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5307. }
  5308. else
  5309. {
  5310. int amount = sd->npc_amount;
  5311. set_reg_num(st, sd, uid, name, cap_value(amount,min,max), script_getref(st,2));
  5312. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5313. }
  5314. st->state = RUN;
  5315. }
  5316. return SCRIPT_CMD_SUCCESS;
  5317. }
  5318. // declare the copyarray method here for future reference
  5319. BUILDIN_FUNC(copyarray);
  5320. /// Sets the value of a variable.
  5321. /// The value is converted to the type of the variable.
  5322. ///
  5323. /// set(<variable>,<value>{,<charid>})
  5324. BUILDIN_FUNC(setr)
  5325. {
  5326. TBL_PC* sd = NULL;
  5327. struct script_data* data;
  5328. //struct script_data* datavalue;
  5329. int64 num;
  5330. uint8 pos = 4;
  5331. const char* name, *command = script_getfuncname(st);
  5332. char prefix;
  5333. data = script_getdata(st,2);
  5334. //datavalue = script_getdata(st,3);
  5335. if( !data_isreference(data) )
  5336. {
  5337. ShowError("script:set: not a variable\n");
  5338. script_reportdata(script_getdata(st,2));
  5339. st->state = END;
  5340. return SCRIPT_CMD_FAILURE;
  5341. }
  5342. num = reference_getuid(data);
  5343. name = reference_getname(data);
  5344. prefix = *name;
  5345. if (!strcmp(command, "setr"))
  5346. pos = 5;
  5347. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5348. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5349. return SCRIPT_CMD_FAILURE;
  5350. }
  5351. #if 0
  5352. if( data_isreference(datavalue) )
  5353. {// the value being referenced is a variable
  5354. const char* namevalue = reference_getname(datavalue);
  5355. if( !not_array_variable(*namevalue) )
  5356. {// array variable being copied into another array variable
  5357. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5358. {// player must be attached in order to copy a player variable
  5359. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5360. return SCRIPT_CMD_SUCCESS;
  5361. }
  5362. if( is_string_variable(namevalue) != is_string_variable(name) )
  5363. {// non-matching array value types
  5364. ShowWarning("script:set: two array variables do not match in type.\n");
  5365. return SCRIPT_CMD_SUCCESS;
  5366. }
  5367. // push the maximum number of array values to the stack
  5368. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5369. // call the copy array method directly
  5370. return buildin_copyarray(st);
  5371. }
  5372. }
  5373. #endif
  5374. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5375. if( is_string_variable(name) )
  5376. script_pushstrcopy(st,script_getstr(st, 4));
  5377. else
  5378. script_pushint(st,script_getnum64(st, 4));
  5379. } else // Return a copy of the variable reference
  5380. script_pushcopy(st, 2);
  5381. if( is_string_variable(name) )
  5382. set_reg_str( st, sd, num, name, script_getstr( st, 3 ), script_getref( st, 2 ) );
  5383. else
  5384. set_reg_num( st, sd, num, name, script_getnum64( st, 3 ), script_getref( st, 2 ) );
  5385. return SCRIPT_CMD_SUCCESS;
  5386. }
  5387. /////////////////////////////////////////////////////////////////////
  5388. /// Array variables
  5389. ///
  5390. /// Sets values of an array, from the starting index.
  5391. /// ex: setarray arr[1],1,2,3;
  5392. ///
  5393. /// setarray <array variable>,<value1>{,<value2>...};
  5394. BUILDIN_FUNC(setarray)
  5395. {
  5396. struct script_data* data;
  5397. const char* name;
  5398. uint32 start;
  5399. uint32 end;
  5400. int32 id;
  5401. int32 i;
  5402. TBL_PC* sd = NULL;
  5403. data = script_getdata(st, 2);
  5404. if( !data_isreference(data) )
  5405. {
  5406. ShowError("script:setarray: not a variable\n");
  5407. script_reportdata(data);
  5408. st->state = END;
  5409. return SCRIPT_CMD_FAILURE;// not a variable
  5410. }
  5411. id = reference_getid(data);
  5412. start = reference_getindex(data);
  5413. name = reference_getname(data);
  5414. if( not_server_variable(*name) )
  5415. {
  5416. if( !script_rid2sd(sd) )
  5417. return SCRIPT_CMD_SUCCESS;// no player attached
  5418. }
  5419. end = start + script_lastdata(st) - 2;
  5420. if( end > SCRIPT_MAX_ARRAYSIZE )
  5421. end = SCRIPT_MAX_ARRAYSIZE;
  5422. if( is_string_variable(name) )
  5423. {// string array
  5424. for( i = 3; start < end; ++start, ++i )
  5425. set_reg_str( st, sd, reference_uid( id, start ), name, script_getstr( st, i ), reference_getref( data ) );
  5426. }
  5427. else
  5428. {// int array
  5429. for( i = 3; start < end; ++start, ++i )
  5430. set_reg_num( st, sd, reference_uid( id, start ), name, script_getnum64( st, i ), reference_getref( data ) );
  5431. }
  5432. return SCRIPT_CMD_SUCCESS;
  5433. }
  5434. /// Sets count values of an array, from the starting index.
  5435. /// ex: cleararray arr[0],0,1;
  5436. ///
  5437. /// cleararray <array variable>,<value>,<count>;
  5438. BUILDIN_FUNC(cleararray)
  5439. {
  5440. struct script_data* data;
  5441. const char* name;
  5442. uint32 start;
  5443. uint32 end;
  5444. int32 id;
  5445. TBL_PC* sd = NULL;
  5446. data = script_getdata(st, 2);
  5447. if( !data_isreference(data) )
  5448. {
  5449. ShowError("script:cleararray: not a variable\n");
  5450. script_reportdata(data);
  5451. st->state = END;
  5452. return SCRIPT_CMD_FAILURE;// not a variable
  5453. }
  5454. id = reference_getid(data);
  5455. start = reference_getindex(data);
  5456. name = reference_getname(data);
  5457. if( not_server_variable(*name) )
  5458. {
  5459. if( !script_rid2sd(sd) )
  5460. return SCRIPT_CMD_SUCCESS;// no player attached
  5461. }
  5462. end = start + script_getnum(st, 4);
  5463. if( end > SCRIPT_MAX_ARRAYSIZE )
  5464. end = SCRIPT_MAX_ARRAYSIZE;
  5465. if( is_string_variable( name ) ){
  5466. const char* value = script_getstr( st, 3 );
  5467. for( ; start < end; ++start ){
  5468. set_reg_str( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5469. }
  5470. }else{
  5471. int64 value = script_getnum64( st, 3 );
  5472. for( ; start < end; ++start ){
  5473. set_reg_num( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5474. }
  5475. }
  5476. return SCRIPT_CMD_SUCCESS;
  5477. }
  5478. /// Copies data from one array to another.
  5479. /// ex: copyarray arr[0],arr[2],2;
  5480. ///
  5481. /// copyarray <destination array variable>,<source array variable>,<count>;
  5482. BUILDIN_FUNC(copyarray)
  5483. {
  5484. struct script_data* data1;
  5485. struct script_data* data2;
  5486. const char* name1;
  5487. const char* name2;
  5488. int32 idx1;
  5489. int32 idx2;
  5490. int32 id1;
  5491. int32 id2;
  5492. int32 i;
  5493. uint32 count;
  5494. TBL_PC* sd = NULL;
  5495. data1 = script_getdata(st, 2);
  5496. data2 = script_getdata(st, 3);
  5497. if( !data_isreference(data1) || !data_isreference(data2) )
  5498. {
  5499. ShowError("script:copyarray: not a variable\n");
  5500. script_reportdata(data1);
  5501. script_reportdata(data2);
  5502. st->state = END;
  5503. return SCRIPT_CMD_FAILURE;// not a variable
  5504. }
  5505. id1 = reference_getid(data1);
  5506. id2 = reference_getid(data2);
  5507. idx1 = reference_getindex(data1);
  5508. idx2 = reference_getindex(data2);
  5509. name1 = reference_getname(data1);
  5510. name2 = reference_getname(data2);
  5511. bool is_string = is_string_variable( name1 );
  5512. if( is_string != is_string_variable( name2 ) ){
  5513. ShowError("script:copyarray: type mismatch\n");
  5514. script_reportdata(data1);
  5515. script_reportdata(data2);
  5516. st->state = END;
  5517. return SCRIPT_CMD_FAILURE;// data type mismatch
  5518. }
  5519. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5520. {
  5521. if( !script_rid2sd(sd) )
  5522. return SCRIPT_CMD_SUCCESS;// no player attached
  5523. }
  5524. count = script_getnum(st, 4);
  5525. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5526. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5527. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5528. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5529. if( id1 == id2 && idx1 > idx2 ){
  5530. // destination might be overlapping the source - copy in reverse order
  5531. for( i = count - 1; i >= 0; --i ){
  5532. if( is_string ){
  5533. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5534. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5535. // Remove stack entry from get_val2_str
  5536. script_removetop( st, -1, 0 );
  5537. }else{
  5538. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5539. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5540. }
  5541. }
  5542. }else{
  5543. // normal copy
  5544. for( i = 0; i < count; ++i ){
  5545. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ){
  5546. if( is_string ){
  5547. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5548. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5549. // Remove stack entry from get_val2_str
  5550. script_removetop( st, -1, 0 );
  5551. }else{
  5552. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5553. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5554. }
  5555. }else{
  5556. // out of range - assume ""/0
  5557. clear_reg( st, sd, reference_uid( id1, idx1 + i ), name1, reference_getref( data1 ) );
  5558. }
  5559. }
  5560. }
  5561. return SCRIPT_CMD_SUCCESS;
  5562. }
  5563. /// Returns the size of the array.
  5564. /// Assumes that everything before the starting index exists.
  5565. /// ex: getarraysize(arr[3])
  5566. ///
  5567. /// getarraysize(<array variable>) -> <int>
  5568. BUILDIN_FUNC(getarraysize)
  5569. {
  5570. struct script_data* data;
  5571. const char* name;
  5572. struct map_session_data* sd = NULL;
  5573. data = script_getdata(st, 2);
  5574. if( !data_isreference(data) )
  5575. {
  5576. ShowError("script:getarraysize: not a variable\n");
  5577. script_reportdata(data);
  5578. script_pushnil(st);
  5579. st->state = END;
  5580. return SCRIPT_CMD_FAILURE;// not a variable
  5581. }
  5582. name = reference_getname(data);
  5583. if( not_server_variable(*name) ){
  5584. if (!script_rid2sd(sd))
  5585. return SCRIPT_CMD_SUCCESS;// no player attached
  5586. }
  5587. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5588. return SCRIPT_CMD_SUCCESS;
  5589. }
  5590. int script_array_index_cmp(const void *a, const void *b) {
  5591. return ( *(unsigned int*)a - *(unsigned int*)b );
  5592. }
  5593. /// Deletes count or all the elements in an array, from the starting index.
  5594. /// ex: deletearray arr[4],2;
  5595. ///
  5596. /// deletearray <array variable>;
  5597. /// deletearray <array variable>,<count>;
  5598. BUILDIN_FUNC(deletearray)
  5599. {
  5600. struct script_data* data;
  5601. const char* name;
  5602. unsigned int start, end, i;
  5603. int id;
  5604. TBL_PC *sd = NULL;
  5605. struct script_array *sa = NULL;
  5606. struct reg_db *src = NULL;
  5607. data = script_getdata(st, 2);
  5608. if( !data_isreference(data) ) {
  5609. ShowError("script:deletearray: not a variable\n");
  5610. script_reportdata(data);
  5611. st->state = END;
  5612. return SCRIPT_CMD_FAILURE;// not a variable
  5613. }
  5614. id = reference_getid(data);
  5615. start = reference_getindex(data);
  5616. name = reference_getname(data);
  5617. if( not_server_variable(*name) ) {
  5618. if( !script_rid2sd(sd) )
  5619. return SCRIPT_CMD_SUCCESS;// no player attached
  5620. }
  5621. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5622. ShowError("script:deletearray: not a array\n");
  5623. script_reportdata(data);
  5624. st->state = END;
  5625. return SCRIPT_CMD_FAILURE;// not a variable
  5626. }
  5627. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5628. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5629. return SCRIPT_CMD_SUCCESS;// not a variable
  5630. }
  5631. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5632. if( start >= end )
  5633. return SCRIPT_CMD_SUCCESS;// nothing to free
  5634. if( script_hasdata(st,3) ) {
  5635. unsigned int count = script_getnum(st, 3);
  5636. if( count > end - start )
  5637. count = end - start;
  5638. if( count <= 0 )
  5639. return SCRIPT_CMD_SUCCESS;// nothing to free
  5640. bool is_string = is_string_variable( name );
  5641. if( end - start < sa->size ) {
  5642. // Better to iterate directly on the array, no speed-up from using sa
  5643. for( ; start + count < end; ++start ) {
  5644. // Compact and overwrite
  5645. if( is_string ){
  5646. const char* value = get_val2_str( st, reference_uid( id, start + count ), reference_getref( data ) );
  5647. set_reg_str( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5648. // Remove stack entry from get_val2_str
  5649. script_removetop( st, -1, 0 );
  5650. }else{
  5651. int64 value = get_val2_num( st, reference_uid( id, start + count ), reference_getref( data ) );
  5652. set_reg_num( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5653. }
  5654. }
  5655. for( ; start < end; start++ ) {
  5656. // Clean up any leftovers that weren't overwritten
  5657. clear_reg( st, sd, reference_uid( id, start ), name, reference_getref( data ) );
  5658. }
  5659. } else {
  5660. // using sa to speed up
  5661. unsigned int *list = NULL, size = 0;
  5662. list = script_array_cpy_list(sa);
  5663. size = sa->size;
  5664. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5665. ARR_FIND(0, size, i, list[i] >= start);
  5666. for( ; i < size && list[i] < start + count; i++ ) {
  5667. // Clear any entries between start and start+count, if they exist
  5668. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5669. }
  5670. for( ; i < size && list[i] < end; i++ ) {
  5671. // Move back count positions any entries between start+count to fill the gaps
  5672. if( is_string ){
  5673. const char* value = get_val2_str( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5674. set_reg_str( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5675. // Remove stack entry from get_val2_str
  5676. script_removetop( st, -1, 0 );
  5677. }else{
  5678. int64 value = get_val2_num( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5679. set_reg_num( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5680. }
  5681. // Clear their originals
  5682. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5683. }
  5684. }
  5685. } else {
  5686. unsigned int *list = NULL, size = 0;
  5687. list = script_array_cpy_list(sa);
  5688. size = sa->size;
  5689. for(i = 0; i < size; i++) {
  5690. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5691. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5692. }
  5693. }
  5694. return SCRIPT_CMD_SUCCESS;
  5695. }
  5696. /// Returns a reference to the target index of the array variable.
  5697. /// Equivalent to var[index].
  5698. ///
  5699. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5700. BUILDIN_FUNC(getelementofarray)
  5701. {
  5702. struct script_data* data;
  5703. int32 id;
  5704. int64 i;
  5705. data = script_getdata(st, 2);
  5706. if( !data_isreference(data) )
  5707. {
  5708. ShowError("script:getelementofarray: not a variable\n");
  5709. script_reportdata(data);
  5710. script_pushnil(st);
  5711. st->state = END;
  5712. return SCRIPT_CMD_SUCCESS;// not a variable
  5713. }
  5714. id = reference_getid(data);
  5715. i = script_getnum(st, 3);
  5716. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5717. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5718. script_reportdata(data);
  5719. script_pushnil(st);
  5720. st->state = END;
  5721. return SCRIPT_CMD_FAILURE;// out of range
  5722. }
  5723. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5724. return SCRIPT_CMD_SUCCESS;
  5725. }
  5726. /// Return the index number of the first matching value in an array.
  5727. /// ex: inarray arr,1;
  5728. ///
  5729. /// inarray <array variable>,<value>;
  5730. BUILDIN_FUNC(inarray)
  5731. {
  5732. struct script_data *data;
  5733. const char* name;
  5734. int id, i, array_size;
  5735. struct map_session_data* sd = NULL;
  5736. struct reg_db *ref = NULL;
  5737. data = script_getdata(st, 2);
  5738. if (!data_isreference(data))
  5739. {
  5740. ShowError("buildin_inarray: not a variable\n");
  5741. script_reportdata(data);
  5742. st->state = END;
  5743. return SCRIPT_CMD_FAILURE;
  5744. }
  5745. name = reference_getname(data);
  5746. ref = reference_getref(data);
  5747. if (not_server_variable(*name) && !script_rid2sd(sd))
  5748. return SCRIPT_CMD_FAILURE;
  5749. array_size = script_array_highest_key(st, sd, name, ref) - 1;
  5750. if (array_size < 0)
  5751. {
  5752. script_pushint(st, -1);
  5753. return SCRIPT_CMD_SUCCESS;
  5754. }
  5755. if (array_size > SCRIPT_MAX_ARRAYSIZE)
  5756. {
  5757. ShowError("buildin_inarray: The array is too large.\n");
  5758. script_reportdata(data);
  5759. st->state = END;
  5760. return SCRIPT_CMD_FAILURE;
  5761. }
  5762. id = reference_getid(data);
  5763. if( is_string_variable( name ) ){
  5764. const char* value = script_getstr( st, 3 );
  5765. for( i = 0; i <= array_size; ++i ){
  5766. const char* temp = get_val2_str( st, reference_uid( id, i ), ref );
  5767. if( !strcmp( temp, value ) ){
  5768. // Remove stack entry from get_val2_str
  5769. script_removetop( st, -1, 0 );
  5770. script_pushint( st, i );
  5771. return SCRIPT_CMD_SUCCESS;
  5772. }
  5773. // Remove stack entry from get_val2_str
  5774. script_removetop( st, -1, 0 );
  5775. }
  5776. }else{
  5777. int64 value = script_getnum64( st, 3 );
  5778. for( i = 0; i <= array_size; ++i ){
  5779. int64 temp = get_val2_num( st, reference_uid( id, i ), ref );
  5780. if( temp == value ){
  5781. script_pushint( st, i );
  5782. return SCRIPT_CMD_SUCCESS;
  5783. }
  5784. }
  5785. }
  5786. script_pushint(st, -1);
  5787. return SCRIPT_CMD_SUCCESS;
  5788. }
  5789. /// Return the number of matches in two arrays.
  5790. /// ex: countinarray arr[0],arr1[0];
  5791. ///
  5792. /// countinarray <array variable>,<array variable>;
  5793. BUILDIN_FUNC(countinarray)
  5794. {
  5795. struct script_data *data1 , *data2;
  5796. const char* name1;
  5797. const char* name2;
  5798. int id1, id2, i, j, array_size1, array_size2, case_count = 0;
  5799. struct map_session_data* sd = NULL;
  5800. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5801. data1 = script_getdata(st, 2);
  5802. data2 = script_getdata(st, 3);
  5803. if (!data_isreference(data1) || !data_isreference(data2))
  5804. {
  5805. ShowError("buildin_countinarray: not a variable\n");
  5806. script_reportdata(data1);
  5807. script_reportdata(data2);
  5808. st->state = END;
  5809. return SCRIPT_CMD_FAILURE;
  5810. }
  5811. name1 = reference_getname(data1);
  5812. name2 = reference_getname(data2);
  5813. ref1 = reference_getref(data1);
  5814. ref2 = reference_getref(data2);
  5815. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5816. return SCRIPT_CMD_FAILURE;
  5817. array_size1 = script_array_highest_key(st, sd, name1, ref1) - 1;
  5818. array_size2 = script_array_highest_key(st, sd, name2, ref2) - 1;
  5819. if (array_size1 < 0 || array_size2 < 0)
  5820. {
  5821. script_pushint(st, 0);
  5822. return SCRIPT_CMD_SUCCESS;
  5823. }
  5824. if (array_size1 > SCRIPT_MAX_ARRAYSIZE || array_size2 > SCRIPT_MAX_ARRAYSIZE)
  5825. {
  5826. ShowError("buildin_countinarray: The array is too large.\n");
  5827. script_reportdata(data1);
  5828. script_reportdata(data2);
  5829. st->state = END;
  5830. return SCRIPT_CMD_FAILURE;
  5831. }
  5832. i = reference_getindex(data1);
  5833. j = reference_getindex(data2);
  5834. if (array_size1 < i || array_size2 < j)
  5835. { //To prevent unintended behavior
  5836. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5837. script_reportdata(data1);
  5838. script_reportdata(data2);
  5839. st->state = END;
  5840. return SCRIPT_CMD_FAILURE;
  5841. }
  5842. id1 = reference_getid(data1);
  5843. id2 = reference_getid(data2);
  5844. if( is_string_variable( name1 ) && is_string_variable( name2 ) ){
  5845. for( ; i <= array_size1; ++i ){
  5846. const char* temp1 = get_val2_str( st, reference_uid( id1, i ), ref1 );
  5847. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5848. const char* temp2 = get_val2_str( st, reference_uid( id2, j ), ref2 );
  5849. if( !strcmp( temp1, temp2 ) ){
  5850. case_count++;
  5851. }
  5852. // Remove stack entry from get_val2_str
  5853. script_removetop( st, -1, 0 );
  5854. }
  5855. // Remove stack entry from get_val2_str
  5856. script_removetop( st, -1, 0 );
  5857. }
  5858. }else if( !is_string_variable( name1 ) && !is_string_variable( name2 ) ){
  5859. for( ; i <= array_size1; ++i ){
  5860. int64 temp1 = get_val2_num( st, reference_uid( id1, i ), ref1 );
  5861. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5862. int64 temp2 = get_val2_num( st, reference_uid( id2, j ), ref2 );
  5863. if( temp1 == temp2 ){
  5864. case_count++;
  5865. }
  5866. }
  5867. }
  5868. }else{
  5869. ShowError("buildin_countinarray: Arrays do not match , You can't compare an int array to a string array.\n");
  5870. script_reportdata(data1);
  5871. script_reportdata(data2);
  5872. st->state = END;
  5873. return SCRIPT_CMD_FAILURE;
  5874. }
  5875. script_pushint(st, case_count);
  5876. return SCRIPT_CMD_SUCCESS;
  5877. }
  5878. /////////////////////////////////////////////////////////////////////
  5879. /// ...
  5880. ///
  5881. /*==========================================
  5882. *
  5883. *------------------------------------------*/
  5884. BUILDIN_FUNC(setlook)
  5885. {
  5886. int type,val;
  5887. TBL_PC* sd;
  5888. type = script_getnum(st,2);
  5889. val = script_getnum(st,3);
  5890. if (!script_charid2sd(4,sd))
  5891. return SCRIPT_CMD_SUCCESS;
  5892. pc_changelook(sd,type,val);
  5893. return SCRIPT_CMD_SUCCESS;
  5894. }
  5895. BUILDIN_FUNC(changelook)
  5896. { // As setlook but only client side
  5897. int type,val;
  5898. TBL_PC* sd;
  5899. type = script_getnum(st,2);
  5900. val = script_getnum(st,3);
  5901. if (!script_charid2sd(4,sd))
  5902. return SCRIPT_CMD_SUCCESS;
  5903. switch(type) {
  5904. case LOOK_HAIR:
  5905. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  5906. break;
  5907. case LOOK_HAIR_COLOR:
  5908. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  5909. break;
  5910. case LOOK_CLOTHES_COLOR:
  5911. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  5912. break;
  5913. case LOOK_BODY2:
  5914. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  5915. break;
  5916. default:
  5917. break;
  5918. }
  5919. clif_changelook(&sd->bl, type, val);
  5920. return SCRIPT_CMD_SUCCESS;
  5921. }
  5922. /*==========================================
  5923. *
  5924. *------------------------------------------*/
  5925. BUILDIN_FUNC(cutin)
  5926. {
  5927. TBL_PC* sd;
  5928. if( !script_rid2sd(sd) )
  5929. return SCRIPT_CMD_SUCCESS;
  5930. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5931. return SCRIPT_CMD_SUCCESS;
  5932. }
  5933. /*==========================================
  5934. *
  5935. *------------------------------------------*/
  5936. BUILDIN_FUNC(viewpoint)
  5937. {
  5938. int type,x,y,id,color;
  5939. TBL_PC* sd;
  5940. if (!script_charid2sd(7, sd)) {
  5941. st->state = END;
  5942. return SCRIPT_CMD_FAILURE;
  5943. }
  5944. type=script_getnum(st,2);
  5945. x=script_getnum(st,3);
  5946. y=script_getnum(st,4);
  5947. id=script_getnum(st,5);
  5948. color=script_getnum(st,6);
  5949. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5950. return SCRIPT_CMD_SUCCESS;
  5951. }
  5952. /**
  5953. * Set random options for new item
  5954. * @param st Script state
  5955. * @param it Temporary item data
  5956. * @param funcname Function name
  5957. * @param x First position of random option id array from the script
  5958. **/
  5959. static int script_getitem_randomoption(struct script_state *st, struct map_session_data* sd, struct item *it, const char *funcname, int x) {
  5960. int i, opt_id_n;
  5961. struct script_data *opt_id = script_getdata(st,x);
  5962. struct script_data *opt_val = script_getdata(st,x+1);
  5963. struct script_data *opt_param = script_getdata(st,x+2);
  5964. const char *opt_id_var = reference_getname(opt_id);
  5965. const char *opt_val_var = reference_getname(opt_val);
  5966. const char *opt_param_var = reference_getname(opt_param);
  5967. int32 opt_id_id, opt_id_idx;
  5968. int32 opt_val_id, opt_val_idx;
  5969. int32 opt_param_id, opt_param_idx;
  5970. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  5971. // Check if the variable requires a player
  5972. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  5973. // If no player is attached
  5974. if( !script_rid2sd(sd) ){
  5975. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  5976. return SCRIPT_CMD_FAILURE;
  5977. }
  5978. }
  5979. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  5980. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  5981. return SCRIPT_CMD_FAILURE;
  5982. }
  5983. if (is_string_variable(opt_id_var)) {
  5984. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  5985. return SCRIPT_CMD_FAILURE;
  5986. }
  5987. // Check if the variable requires a player
  5988. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  5989. // If no player is attached
  5990. if( !script_rid2sd(sd) ){
  5991. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  5992. return SCRIPT_CMD_FAILURE;
  5993. }
  5994. }
  5995. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  5996. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  5997. return SCRIPT_CMD_FAILURE;
  5998. }
  5999. if (is_string_variable(opt_val_var)) {
  6000. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6001. return SCRIPT_CMD_FAILURE;
  6002. }
  6003. // Check if the variable requires a player
  6004. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  6005. // If no player is attached
  6006. if( !script_rid2sd(sd) ){
  6007. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  6008. return SCRIPT_CMD_FAILURE;
  6009. }
  6010. }
  6011. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  6012. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  6013. return SCRIPT_CMD_FAILURE;
  6014. }
  6015. if (is_string_variable(opt_param_var)) {
  6016. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6017. return SCRIPT_CMD_FAILURE;
  6018. }
  6019. opt_id_ref = reference_getref(opt_id);
  6020. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  6021. opt_val_ref = reference_getref(opt_val);
  6022. opt_param_ref = reference_getref(opt_param);
  6023. opt_id_id = reference_getid(opt_id);
  6024. opt_val_id = reference_getid(opt_val);
  6025. opt_param_id = reference_getid(opt_param);
  6026. opt_id_idx = reference_getindex(opt_id);
  6027. opt_val_idx = reference_getindex(opt_val);
  6028. opt_param_idx = reference_getindex(opt_param);
  6029. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6030. it->option[i].id = (short)get_val2_num( st, reference_uid( opt_id_id, opt_id_idx + i ), opt_id_ref );
  6031. it->option[i].value = (short)get_val2_num( st, reference_uid( opt_val_id, opt_val_idx + i ), opt_val_ref );
  6032. it->option[i].param = (char)get_val2_num( st, reference_uid( opt_param_id, opt_param_idx + i ), opt_param_ref );
  6033. }
  6034. return SCRIPT_CMD_SUCCESS;
  6035. }
  6036. /**
  6037. * Sub function for counting items
  6038. * @param items: Item array to search
  6039. * @param id: Item data to search for
  6040. * @param size: Maximum size of array
  6041. * @param expanded: If the script command has extra arguments
  6042. * @param random_option: If the struct command has random option arguments
  6043. * @param st: Script state (required for random options)
  6044. * @param sd: Player data (required for random options)
  6045. * @param rental: Whether or not to count rental items
  6046. * @return Total count of item being searched
  6047. */
  6048. static int script_countitem_sub(struct item *items, std::shared_ptr<item_data> id, int size, bool expanded, bool random_option, struct script_state *st, struct map_session_data *sd = nullptr, bool rental = false) {
  6049. nullpo_retr(-1, items);
  6050. nullpo_retr(-1, st);
  6051. int count = 0;
  6052. if (!expanded) { // For non-expanded functions
  6053. t_itemid nameid = id->nameid;
  6054. for (int i = 0; i < size; i++) {
  6055. item *itm = &items[i];
  6056. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1 || (!rental && itm->expire_time > 0))
  6057. continue;
  6058. if (itm->nameid == nameid)
  6059. count += itm->amount;
  6060. }
  6061. } else { // For expanded functions
  6062. item it;
  6063. memset(&it, 0, sizeof(it));
  6064. it.nameid = id->nameid;
  6065. it.identify = script_getnum(st,3);
  6066. it.refine = script_getnum(st,4);
  6067. it.attribute = script_getnum(st,5);
  6068. it.card[0] = script_getnum(st,6);
  6069. it.card[1] = script_getnum(st,7);
  6070. it.card[2] = script_getnum(st,8);
  6071. it.card[3] = script_getnum(st,9);
  6072. if (random_option) {
  6073. if (!sd) {
  6074. ShowError("buildin_countitem3: Player not attached.\n");
  6075. return -1;
  6076. }
  6077. int res = script_getitem_randomoption(st, sd, &it, "countitem3", 10);
  6078. if (res != SCRIPT_CMD_SUCCESS)
  6079. return -1;
  6080. }
  6081. for (int i = 0; i < size; i++) {
  6082. item *itm = &items[i];
  6083. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1 || (!rental && items[i].expire_time > 0))
  6084. continue;
  6085. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute)
  6086. continue;
  6087. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6088. continue;
  6089. if (random_option) {
  6090. uint8 j;
  6091. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6092. 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)
  6093. break;
  6094. }
  6095. if (j != MAX_ITEM_RDM_OPT)
  6096. continue;
  6097. }
  6098. count += items[i].amount;
  6099. }
  6100. }
  6101. return count;
  6102. }
  6103. /**
  6104. * Returns number of items in inventory
  6105. * countitem(<nameID>{,<accountID>})
  6106. * countitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6107. * countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6108. * countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6109. */
  6110. BUILDIN_FUNC(countitem)
  6111. {
  6112. TBL_PC *sd;
  6113. char *command = (char *)script_getfuncname(st);
  6114. int aid = 3;
  6115. bool random_option = false;
  6116. if (command[strlen(command)-1] == '2')
  6117. aid = 10;
  6118. else if (command[strlen(command)-1] == '3') {
  6119. aid = 13;
  6120. random_option = true;
  6121. }
  6122. if (!script_accid2sd(aid, sd))
  6123. return SCRIPT_CMD_FAILURE;
  6124. std::shared_ptr<item_data> id;
  6125. if (script_isstring(st, 2)) // item name
  6126. id = item_db.searchname( script_getstr( st, 2 ) );
  6127. else // item id
  6128. id = item_db.find( script_getnum( st, 2 ) );
  6129. if (!id) {
  6130. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6131. script_pushint(st, 0);
  6132. return SCRIPT_CMD_FAILURE;
  6133. }
  6134. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, (aid > 3) ? true : false, random_option, st, sd);
  6135. if (count < 0) {
  6136. st->state = END;
  6137. return SCRIPT_CMD_FAILURE;
  6138. }
  6139. script_pushint(st, count);
  6140. return SCRIPT_CMD_SUCCESS;
  6141. }
  6142. /**
  6143. * Returns number of items in cart
  6144. * cartcountitem(<nameID>{,<accountID>})
  6145. * cartcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6146. */
  6147. BUILDIN_FUNC(cartcountitem)
  6148. {
  6149. TBL_PC *sd;
  6150. char *command = (char *)script_getfuncname(st);
  6151. int aid = 3;
  6152. if (command[strlen(command) - 1] == '2')
  6153. aid = 10;
  6154. if (!script_accid2sd(aid, sd))
  6155. return SCRIPT_CMD_FAILURE;
  6156. if (!pc_iscarton(sd)) {
  6157. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6158. script_pushint(st, -1);
  6159. return SCRIPT_CMD_FAILURE;
  6160. }
  6161. std::shared_ptr<item_data> id;
  6162. if (script_isstring(st, 2)) // item name
  6163. id = item_db.searchname( script_getstr( st, 2 ) );
  6164. else // item id
  6165. id = item_db.find( script_getnum( st, 2 ) );
  6166. if (!id) {
  6167. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6168. script_pushint(st, 0);
  6169. return SCRIPT_CMD_FAILURE;
  6170. }
  6171. int count = script_countitem_sub(sd->cart.u.items_cart, id, MAX_CART, (aid > 3) ? true : false, false, st);
  6172. if (count < 0) {
  6173. st->state = END;
  6174. return SCRIPT_CMD_FAILURE;
  6175. }
  6176. script_pushint(st, count);
  6177. return SCRIPT_CMD_SUCCESS;
  6178. }
  6179. /**
  6180. * Returns number of items in storage
  6181. * storagecountitem(<nameID>{,<accountID>})
  6182. * storagecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6183. */
  6184. BUILDIN_FUNC(storagecountitem)
  6185. {
  6186. TBL_PC *sd;
  6187. char *command = (char *)script_getfuncname(st);
  6188. int aid = 3;
  6189. if (command[strlen(command) - 1] == '2')
  6190. aid = 10;
  6191. if (!script_accid2sd(aid, sd))
  6192. return SCRIPT_CMD_FAILURE;
  6193. std::shared_ptr<item_data> id;
  6194. if (script_isstring(st, 2)) // item name
  6195. id = item_db.searchname( script_getstr( st, 2 ) );
  6196. else // item id
  6197. id = item_db.find( script_getnum( st, 2 ) );
  6198. if (!id) {
  6199. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6200. script_pushint(st, 0);
  6201. return SCRIPT_CMD_FAILURE;
  6202. }
  6203. if (sd->state.storage_flag != 0) {
  6204. script_pushint(st, -1);
  6205. return SCRIPT_CMD_SUCCESS;
  6206. }
  6207. int count = script_countitem_sub(sd->storage.u.items_storage, id, MAX_STORAGE, (aid > 3) ? true : false, false, st);
  6208. if (count < 0) {
  6209. st->state = END;
  6210. return SCRIPT_CMD_FAILURE;
  6211. }
  6212. script_pushint(st, count);
  6213. return SCRIPT_CMD_SUCCESS;
  6214. }
  6215. /**
  6216. * Returns number of items in guild storage
  6217. * guildstoragecountitem(<nameID>{,<accountID>})
  6218. * guildstoragecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6219. */
  6220. BUILDIN_FUNC(guildstoragecountitem)
  6221. {
  6222. TBL_PC *sd;
  6223. char *command = (char *)script_getfuncname(st);
  6224. int aid = 3;
  6225. if (command[strlen(command) - 1] == '2')
  6226. aid = 10;
  6227. if (!script_accid2sd(aid, sd))
  6228. return SCRIPT_CMD_FAILURE;
  6229. std::shared_ptr<item_data> id;
  6230. if (script_isstring(st, 2)) // item name
  6231. id = item_db.searchname( script_getstr( st, 2 ) );
  6232. else // item id
  6233. id = item_db.find( script_getnum( st, 2 ) );
  6234. if (!id) {
  6235. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6236. script_pushint(st, 0);
  6237. return SCRIPT_CMD_FAILURE;
  6238. }
  6239. struct s_storage *gstor = guild2storage(sd->status.guild_id);
  6240. if (!gstor || (gstor && sd->state.storage_flag != 0)) {
  6241. script_pushint(st, -1);
  6242. return SCRIPT_CMD_SUCCESS;
  6243. }
  6244. gstor->lock = true;
  6245. int count = script_countitem_sub(gstor->u.items_guild, id, MAX_GUILD_STORAGE, (aid > 3) ? true : false, false, st);
  6246. storage_guild_storageclose(sd);
  6247. gstor->lock = false;
  6248. if (count < 0) {
  6249. st->state = END;
  6250. return SCRIPT_CMD_FAILURE;
  6251. }
  6252. script_pushint(st, count);
  6253. return SCRIPT_CMD_SUCCESS;
  6254. }
  6255. /**
  6256. * Returns number of rental items in inventory
  6257. * rentalcountitem(<nameID>{,<accountID>})
  6258. * rentalcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6259. * rentalcountitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6260. * rentalcountitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6261. */
  6262. BUILDIN_FUNC(rentalcountitem)
  6263. {
  6264. char *command = (char *)script_getfuncname(st);
  6265. int aid = 3;
  6266. bool random_option = false;
  6267. if (command[strlen(command) - 1] == '2')
  6268. aid = 10;
  6269. else if (command[strlen(command) - 1] == '3') {
  6270. aid = 13;
  6271. random_option = true;
  6272. }
  6273. map_session_data *sd;
  6274. if (!script_accid2sd(aid, sd))
  6275. return SCRIPT_CMD_FAILURE;
  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->inventory.u.items_inventory, id, MAX_INVENTORY, (aid > 3) ? true : false, random_option, st, sd, true);
  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. * Check if item with this amount can fit in inventory
  6296. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6297. * Return
  6298. * 0 : fail
  6299. * 1 : success (npc side only)
  6300. *------------------------------------------*/
  6301. BUILDIN_FUNC(checkweight)
  6302. {
  6303. int slots = 0;
  6304. unsigned short amount2 = 0;
  6305. unsigned int weight = 0, i, nbargs;
  6306. std::shared_ptr<item_data> id;
  6307. struct map_session_data* sd;
  6308. if( !script_rid2sd(sd) )
  6309. return SCRIPT_CMD_SUCCESS;
  6310. nbargs = script_lastdata(st)+1;
  6311. if(nbargs%2) {
  6312. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6313. script_pushint(st,0);
  6314. return SCRIPT_CMD_FAILURE;
  6315. }
  6316. slots = pc_inventoryblank(sd); //nb of empty slot
  6317. for (i = 2; i < nbargs; i += 2) {
  6318. t_itemid nameid;
  6319. unsigned short amount;
  6320. if( script_isstring(st, i) ) // item name
  6321. id = item_db.searchname( script_getstr( st, i ) );
  6322. else // item id
  6323. id = item_db.find( script_getnum( st, i ) );
  6324. if( id == nullptr ){
  6325. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6326. script_pushint(st,0);
  6327. return SCRIPT_CMD_FAILURE;
  6328. }
  6329. nameid = id->nameid;
  6330. amount = script_getnum(st,i+1);
  6331. if( amount < 1 ) {
  6332. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6333. script_pushint(st,0);
  6334. return SCRIPT_CMD_FAILURE;
  6335. }
  6336. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6337. if( weight + sd->weight > sd->max_weight )
  6338. {// too heavy
  6339. script_pushint(st,0);
  6340. return SCRIPT_CMD_SUCCESS;
  6341. }
  6342. switch( pc_checkadditem(sd, nameid, amount) ) {
  6343. case CHKADDITEM_EXIST:
  6344. // item is already in inventory, but there is still space for the requested amount
  6345. break;
  6346. case CHKADDITEM_NEW:
  6347. if( itemdb_isstackable(nameid) ) {
  6348. // stackable
  6349. amount2++;
  6350. if( slots < amount2 ) {
  6351. script_pushint(st,0);
  6352. return SCRIPT_CMD_SUCCESS;
  6353. }
  6354. } else {
  6355. // non-stackable
  6356. amount2 += amount;
  6357. if( slots < amount2) {
  6358. script_pushint(st,0);
  6359. return SCRIPT_CMD_SUCCESS;
  6360. }
  6361. }
  6362. break;
  6363. case CHKADDITEM_OVERAMOUNT:
  6364. script_pushint(st,0);
  6365. return SCRIPT_CMD_SUCCESS;
  6366. }
  6367. }
  6368. script_pushint(st,1);
  6369. return SCRIPT_CMD_SUCCESS;
  6370. }
  6371. BUILDIN_FUNC(checkweight2)
  6372. {
  6373. //variable sub checkweight
  6374. int i = 0, slots = 0, weight = 0;
  6375. short fail = 0;
  6376. unsigned short amount2 = 0;
  6377. //variable for array parsing
  6378. struct script_data* data_it;
  6379. struct script_data* data_nb;
  6380. const char* name_it;
  6381. const char* name_nb;
  6382. int32 id_it, id_nb;
  6383. int32 idx_it, idx_nb;
  6384. int nb_it, nb_nb; //array size
  6385. TBL_PC *sd;
  6386. if( !script_rid2sd(sd) )
  6387. return SCRIPT_CMD_SUCCESS;
  6388. data_it = script_getdata(st, 2);
  6389. data_nb = script_getdata(st, 3);
  6390. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6391. ShowError("buildin_checkweight2: parameter not a variable\n");
  6392. script_pushint(st,0);
  6393. return SCRIPT_CMD_FAILURE;// not a variable
  6394. }
  6395. id_it = reference_getid(data_it);
  6396. id_nb = reference_getid(data_nb);
  6397. idx_it = reference_getindex(data_it);
  6398. idx_nb = reference_getindex(data_nb);
  6399. name_it = reference_getname(data_it);
  6400. name_nb = reference_getname(data_nb);
  6401. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6402. ShowError("buildin_checkweight2: illegal type, need int\n");
  6403. script_pushint(st,0);
  6404. return SCRIPT_CMD_FAILURE;// not supported
  6405. }
  6406. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6407. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6408. if(nb_it != nb_nb) {
  6409. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6410. fail = 1;
  6411. }
  6412. slots = pc_inventoryblank(sd);
  6413. for(i=0; i<nb_it; i++) {
  6414. t_itemid nameid = (t_itemid)get_val2_num( st, reference_uid( id_it, idx_it + i ), reference_getref( data_it ) );
  6415. unsigned short amount = (unsigned short)get_val2_num( st, reference_uid( id_nb, idx_nb + i ), reference_getref( data_nb ) );
  6416. if(fail)
  6417. continue; //cpntonie to depop rest
  6418. if(itemdb_exists(nameid) == NULL ) {
  6419. ShowError("buildin_checkweight2: Invalid item '%u'.\n", nameid);
  6420. fail=1;
  6421. continue;
  6422. }
  6423. if(amount < 0 ) {
  6424. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6425. fail = 1;
  6426. continue;
  6427. }
  6428. weight += itemdb_weight(nameid)*amount;
  6429. if( weight + sd->weight > sd->max_weight ) {
  6430. fail = 1;
  6431. continue;
  6432. }
  6433. switch( pc_checkadditem(sd, nameid, amount) ) {
  6434. case CHKADDITEM_EXIST:
  6435. // item is already in inventory, but there is still space for the requested amount
  6436. break;
  6437. case CHKADDITEM_NEW:
  6438. if( itemdb_isstackable(nameid) ) {// stackable
  6439. amount2++;
  6440. if( slots < amount2 )
  6441. fail = 1;
  6442. } else {// non-stackable
  6443. amount2 += amount;
  6444. if( slots < amount2 ) {
  6445. fail = 1;
  6446. }
  6447. }
  6448. break;
  6449. case CHKADDITEM_OVERAMOUNT:
  6450. fail = 1;
  6451. } //end switch
  6452. } //end loop DO NOT break it prematurly we need to depop all stack
  6453. fail ? script_pushint(st,0) : script_pushint(st,1);
  6454. return SCRIPT_CMD_SUCCESS;
  6455. }
  6456. /*==========================================
  6457. * getitem <item id>,<amount>{,<account ID>};
  6458. * getitem "<item name>",<amount>{,<account ID>};
  6459. *
  6460. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6461. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6462. * Type:
  6463. * 0 - No bound
  6464. * 1 - Account Bound
  6465. * 2 - Guild Bound
  6466. * 3 - Party Bound
  6467. * 4 - Character Bound
  6468. *------------------------------------------*/
  6469. BUILDIN_FUNC(getitem)
  6470. {
  6471. int get_count, i;
  6472. t_itemid nameid;
  6473. unsigned short amount;
  6474. struct item it;
  6475. struct map_session_data *sd;
  6476. unsigned char flag = 0;
  6477. const char* command = script_getfuncname(st);
  6478. std::shared_ptr<item_data> id;
  6479. if( script_isstring(st, 2) ) {// "<item name>"
  6480. const char *name = script_getstr(st, 2);
  6481. id = item_db.searchname( name );
  6482. if( id == nullptr ){
  6483. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6484. return SCRIPT_CMD_FAILURE; //No item created.
  6485. }
  6486. nameid = id->nameid;
  6487. } else {// <item id>
  6488. nameid = script_getnum(st, 2);
  6489. id = item_db.find( nameid );
  6490. if( id == nullptr ){
  6491. ShowError("buildin_getitem: Nonexistant item %u requested.\n", nameid);
  6492. return SCRIPT_CMD_FAILURE; //No item created.
  6493. }
  6494. }
  6495. // <amount>
  6496. if( (amount = script_getnum(st,3)) <= 0)
  6497. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6498. memset(&it,0,sizeof(it));
  6499. it.nameid = nameid;
  6500. it.identify = 1;
  6501. it.bound = BOUND_NONE;
  6502. if( !strcmp(command,"getitembound") ) {
  6503. char bound = script_getnum(st,4);
  6504. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6505. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6506. return SCRIPT_CMD_FAILURE;
  6507. }
  6508. script_mapid2sd(5,sd);
  6509. it.bound = bound;
  6510. } else {
  6511. script_mapid2sd(4,sd);
  6512. }
  6513. if( sd == NULL ) // no target
  6514. return SCRIPT_CMD_SUCCESS;
  6515. //Check if it's stackable.
  6516. if( !itemdb_isstackable2( id.get() ) ){
  6517. get_count = 1;
  6518. }else{
  6519. get_count = amount;
  6520. }
  6521. for (i = 0; i < amount; i += get_count)
  6522. {
  6523. // if not pet egg
  6524. if (!pet_create_egg(sd, nameid))
  6525. {
  6526. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6527. {
  6528. clif_additem(sd, 0, 0, flag);
  6529. if( pc_candrop(sd,&it) )
  6530. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6531. }
  6532. }
  6533. }
  6534. return SCRIPT_CMD_SUCCESS;
  6535. }
  6536. /*==========================================
  6537. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6538. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6539. *
  6540. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6541. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6542. *
  6543. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6544. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6545. *
  6546. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6547. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6548. * Type:
  6549. * 0 - No bound
  6550. * 1 - Account Bound
  6551. * 2 - Guild Bound
  6552. * 3 - Party Bound
  6553. * 4 - Character Bound
  6554. *------------------------------------------*/
  6555. BUILDIN_FUNC(getitem2)
  6556. {
  6557. t_itemid nameid;
  6558. unsigned short amount;
  6559. int iden, ref, attr;
  6560. t_itemid c1, c2, c3, c4;
  6561. char bound = BOUND_NONE;
  6562. std::shared_ptr<item_data> item_data;
  6563. struct item item_tmp;
  6564. TBL_PC *sd;
  6565. const char* command = script_getfuncname(st);
  6566. int offset = 0;
  6567. if( !strncmp(command,"getitembound",12) ) {
  6568. int aid_pos = 12;
  6569. bound = script_getnum(st,11);
  6570. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6571. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6572. return SCRIPT_CMD_FAILURE;
  6573. }
  6574. if (command[strlen(command)-1] == '3') {
  6575. offset = 12;
  6576. aid_pos = 15;
  6577. }
  6578. script_mapid2sd(aid_pos,sd);
  6579. } else {
  6580. int aid_pos = 11;
  6581. if (strcmpi(command,"getitem3") == 0) {
  6582. offset = 11;
  6583. aid_pos = 14;
  6584. }
  6585. script_mapid2sd(aid_pos,sd);
  6586. }
  6587. if( sd == NULL ) // no target
  6588. return SCRIPT_CMD_SUCCESS;
  6589. if( script_isstring(st, 2) ) {
  6590. const char *name = script_getstr(st, 2);
  6591. item_data = item_db.searchname( name );
  6592. if( item_data == nullptr ){
  6593. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6594. return SCRIPT_CMD_FAILURE; //No item created.
  6595. }
  6596. nameid = item_data->nameid;
  6597. } else {
  6598. nameid = script_getnum(st, 2);
  6599. item_data = item_db.find( nameid );
  6600. if( item_data == nullptr ){
  6601. ShowError("buildin_getitem2: Nonexistant item %u requested (by conv_num).\n", nameid);
  6602. return SCRIPT_CMD_FAILURE; //No item created.
  6603. }
  6604. }
  6605. amount = script_getnum(st,3);
  6606. iden = script_getnum(st,4);
  6607. ref = script_getnum(st,5);
  6608. attr = script_getnum(st,6);
  6609. c1 = script_getnum(st,7);
  6610. c2 = script_getnum(st,8);
  6611. c3 = script_getnum(st,9);
  6612. c4 = script_getnum(st,10);
  6613. if( item_data ) {
  6614. int get_count = 0, i;
  6615. memset(&item_tmp,0,sizeof(item_tmp));
  6616. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6617. if(ref > MAX_REFINE)
  6618. ref = MAX_REFINE;
  6619. }
  6620. else if( item_data->type == IT_PETEGG ) {
  6621. iden = 1;
  6622. ref = 0;
  6623. }
  6624. else {
  6625. iden = 1;
  6626. ref = attr = 0;
  6627. }
  6628. item_tmp.nameid = nameid;
  6629. item_tmp.identify = iden;
  6630. item_tmp.refine = ref;
  6631. item_tmp.attribute = attr;
  6632. item_tmp.card[0] = c1;
  6633. item_tmp.card[1] = c2;
  6634. item_tmp.card[2] = c3;
  6635. item_tmp.card[3] = c4;
  6636. item_tmp.bound = bound;
  6637. if (offset != 0) {
  6638. int res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6639. if (res == SCRIPT_CMD_FAILURE)
  6640. return SCRIPT_CMD_FAILURE;
  6641. }
  6642. //Check if it's stackable.
  6643. if( !itemdb_isstackable2( item_data.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. unsigned char flag = 0;
  6654. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6655. {
  6656. clif_additem(sd, 0, 0, flag);
  6657. if( pc_candrop(sd,&item_tmp) )
  6658. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6659. }
  6660. }
  6661. }
  6662. }
  6663. return SCRIPT_CMD_SUCCESS;
  6664. }
  6665. /** Gives rental item to player
  6666. * rentitem <item id>,<seconds>{,<account_id>}
  6667. * rentitem "<item name>",<seconds>{,<account_id>}
  6668. */
  6669. BUILDIN_FUNC(rentitem) {
  6670. struct map_session_data *sd;
  6671. struct item it;
  6672. int seconds;
  6673. t_itemid nameid = 0;
  6674. unsigned char flag = 0;
  6675. if (!script_accid2sd(4,sd))
  6676. return SCRIPT_CMD_FAILURE;
  6677. if( script_isstring(st, 2) )
  6678. {
  6679. const char *name = script_getstr(st, 2);
  6680. std::shared_ptr<item_data> itd = item_db.searchname( name );
  6681. if( itd == nullptr ){
  6682. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6683. return SCRIPT_CMD_FAILURE;
  6684. }
  6685. nameid = itd->nameid;
  6686. }
  6687. else
  6688. {
  6689. nameid = script_getnum(st, 2);
  6690. if( nameid == 0 || !itemdb_exists(nameid) )
  6691. {
  6692. ShowError("buildin_rentitem: Nonexistant item %u requested.\n", nameid);
  6693. return SCRIPT_CMD_FAILURE;
  6694. }
  6695. }
  6696. seconds = script_getnum(st,3);
  6697. memset(&it, 0, sizeof(it));
  6698. it.nameid = nameid;
  6699. it.identify = 1;
  6700. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6701. it.bound = BOUND_NONE;
  6702. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6703. {
  6704. clif_additem(sd, 0, 0, flag);
  6705. return SCRIPT_CMD_FAILURE;
  6706. }
  6707. return SCRIPT_CMD_SUCCESS;
  6708. }
  6709. /**
  6710. * Gives rental item to player with advanced option
  6711. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6712. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6713. *
  6714. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6715. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6716. */
  6717. BUILDIN_FUNC(rentitem2) {
  6718. struct map_session_data *sd;
  6719. struct item it;
  6720. std::shared_ptr<item_data> id;
  6721. int seconds;
  6722. t_itemid nameid = 0;
  6723. unsigned char flag = 0;
  6724. int iden,ref,attr;
  6725. t_itemid c1,c2,c3,c4;
  6726. const char *funcname = script_getfuncname(st);
  6727. if (funcname[strlen(funcname)-1] == '3') {
  6728. if (!script_accid2sd(14,sd))
  6729. return SCRIPT_CMD_FAILURE;
  6730. } else if (!script_accid2sd(11,sd))
  6731. return SCRIPT_CMD_FAILURE;
  6732. if( script_isstring(st, 2) ) {
  6733. const char *name = script_getstr(st, 2);
  6734. id = item_db.searchname( name );
  6735. if( id == nullptr ) {
  6736. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6737. return SCRIPT_CMD_FAILURE;
  6738. }
  6739. nameid = id->nameid;
  6740. } else {
  6741. nameid = script_getnum(st, 2);
  6742. id = item_db.find( nameid );
  6743. if( id == nullptr ){
  6744. ShowError("buildin_rentitem2: Nonexistant item %u requested.\n", nameid);
  6745. return SCRIPT_CMD_FAILURE;
  6746. }
  6747. }
  6748. seconds = script_getnum(st,3);
  6749. iden = script_getnum(st,4);
  6750. ref = script_getnum(st,5);
  6751. attr = script_getnum(st,6);
  6752. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6753. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6754. }
  6755. else if (id->type==IT_PETEGG) {
  6756. iden = 1;
  6757. ref = 0;
  6758. }
  6759. else {
  6760. iden = 1;
  6761. ref = attr = 0;
  6762. }
  6763. c1 = script_getnum(st,7);
  6764. c2 = script_getnum(st,8);
  6765. c3 = script_getnum(st,9);
  6766. c4 = script_getnum(st,10);
  6767. memset(&it, 0, sizeof(it));
  6768. it.nameid = nameid;
  6769. it.identify = iden;
  6770. it.refine = ref;
  6771. it.attribute = attr;
  6772. it.card[0] = c1;
  6773. it.card[1] = c2;
  6774. it.card[2] = c3;
  6775. it.card[3] = c4;
  6776. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6777. it.bound = BOUND_NONE;
  6778. if (funcname[strlen(funcname)-1] == '3') {
  6779. int res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6780. if (res != SCRIPT_CMD_SUCCESS)
  6781. return res;
  6782. }
  6783. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6784. clif_additem(sd, 0, 0, flag);
  6785. return SCRIPT_CMD_FAILURE;
  6786. }
  6787. return SCRIPT_CMD_SUCCESS;
  6788. }
  6789. /*==========================================
  6790. * gets an item with someone's name inscribed [Skotlex]
  6791. * getinscribeditem item_num, character_name
  6792. * Returned Qty is always 1, only works on equip-able
  6793. * equipment
  6794. *------------------------------------------*/
  6795. BUILDIN_FUNC(getnameditem)
  6796. {
  6797. t_itemid nameid;
  6798. struct item item_tmp;
  6799. TBL_PC *sd, *tsd;
  6800. if (!script_rid2sd(sd))
  6801. { //Player not attached!
  6802. script_pushint(st,0);
  6803. return SCRIPT_CMD_SUCCESS;
  6804. }
  6805. if( script_isstring(st, 2) ){
  6806. const char *name = script_getstr(st, 2);
  6807. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6808. // Failed
  6809. if( item_data == nullptr){
  6810. script_pushint(st,0);
  6811. return SCRIPT_CMD_SUCCESS;
  6812. }
  6813. nameid = item_data->nameid;
  6814. }else
  6815. nameid = script_getnum(st, 2);
  6816. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6817. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6818. script_pushint(st,0);
  6819. return SCRIPT_CMD_SUCCESS;
  6820. }
  6821. if( script_isstring(st, 3) ) //Char Name
  6822. tsd = map_nick2sd(script_getstr(st, 3),false);
  6823. else //Char Id was given
  6824. tsd = map_charid2sd(script_getnum(st, 3));
  6825. if( tsd == NULL )
  6826. { //Failed
  6827. script_pushint(st,0);
  6828. return SCRIPT_CMD_SUCCESS;
  6829. }
  6830. memset(&item_tmp,0,sizeof(item_tmp));
  6831. item_tmp.nameid=nameid;
  6832. item_tmp.amount=1;
  6833. item_tmp.identify=1;
  6834. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6835. item_tmp.card[2]=GetWord(tsd->status.char_id,0);
  6836. item_tmp.card[3]=GetWord(tsd->status.char_id,1);
  6837. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6838. script_pushint(st,0);
  6839. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6840. }
  6841. script_pushint(st,1);
  6842. return SCRIPT_CMD_SUCCESS;
  6843. }
  6844. /*==========================================
  6845. * gets a random item ID from an item group [Skotlex]
  6846. * groupranditem <group_num>{,<sub_group>};
  6847. *------------------------------------------*/
  6848. BUILDIN_FUNC(grouprandomitem) {
  6849. int sub_group = 1;
  6850. FETCH(3, sub_group);
  6851. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(script_getnum(st,2),sub_group);
  6852. if (!entry) {
  6853. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6854. script_pushint(st,UNKNOWN_ITEM_ID);
  6855. return SCRIPT_CMD_FAILURE;
  6856. }
  6857. script_pushint(st,entry->nameid);
  6858. return SCRIPT_CMD_SUCCESS;
  6859. }
  6860. /**
  6861. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  6862. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  6863. */
  6864. BUILDIN_FUNC(makeitem) {
  6865. t_itemid nameid;
  6866. uint16 amount, flag = 0, x, y;
  6867. const char *mapname;
  6868. int m;
  6869. struct item item_tmp;
  6870. bool canShowEffect = false;
  6871. if( script_isstring(st, 2) ){
  6872. const char *name = script_getstr(st, 2);
  6873. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6874. if( item_data )
  6875. nameid = item_data->nameid;
  6876. else{
  6877. ShowError( "buildin_makeitem: Unknown item %s\n", name );
  6878. return SCRIPT_CMD_FAILURE;
  6879. }
  6880. }
  6881. else {
  6882. int32 val = script_getnum( st, 2 );
  6883. if( val < 0 ){
  6884. flag = 1;
  6885. nameid = (t_itemid)( -1 * val );
  6886. }else{
  6887. nameid = (t_itemid)val;
  6888. }
  6889. if( !itemdb_exists( nameid ) ){
  6890. ShowError( "buildin_makeitem: Unknown item id %u\n", nameid );
  6891. return SCRIPT_CMD_FAILURE;
  6892. }
  6893. }
  6894. amount = script_getnum(st,3);
  6895. mapname = script_getstr(st,4);
  6896. x = script_getnum(st,5);
  6897. y = script_getnum(st,6);
  6898. if (script_hasdata(st, 7))
  6899. canShowEffect = script_getnum(st, 7) != 0;
  6900. if (strcmp(mapname, "this")==0) {
  6901. TBL_PC *sd;
  6902. if (!script_rid2sd(sd))
  6903. return SCRIPT_CMD_SUCCESS; //Failed...
  6904. m = sd->bl.m;
  6905. } else
  6906. m = map_mapname2mapid(mapname);
  6907. memset(&item_tmp,0,sizeof(item_tmp));
  6908. item_tmp.nameid = nameid;
  6909. if (!flag)
  6910. item_tmp.identify = 1;
  6911. else
  6912. item_tmp.identify = itemdb_isidentified(nameid);
  6913. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  6914. return SCRIPT_CMD_SUCCESS;
  6915. }
  6916. /**
  6917. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  6918. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  6919. *
  6920. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  6921. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  6922. */
  6923. BUILDIN_FUNC(makeitem2) {
  6924. t_itemid nameid;
  6925. uint16 amount, x, y;
  6926. const char *mapname;
  6927. int m;
  6928. struct item item_tmp;
  6929. struct item_data *id;
  6930. const char *funcname = script_getfuncname(st);
  6931. bool canShowEffect = false;
  6932. if( script_isstring( st, 2 ) ){
  6933. const char *name = script_getstr( st, 2 );
  6934. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6935. if( item_data ){
  6936. nameid = item_data->nameid;
  6937. }else{
  6938. ShowError( "buildin_%s: Unknown item %s\n", funcname, name );
  6939. return SCRIPT_CMD_FAILURE;
  6940. }
  6941. }else{
  6942. nameid = (t_itemid)script_getnum( st, 2 );
  6943. if( !itemdb_exists( nameid ) ){
  6944. ShowError( "buildin_%s: Unknown item id %u\n", funcname, nameid );
  6945. return SCRIPT_CMD_FAILURE;
  6946. }
  6947. }
  6948. amount = script_getnum(st,3);
  6949. mapname = script_getstr(st,4);
  6950. x = script_getnum(st,5);
  6951. y = script_getnum(st,6);
  6952. if (strcmp(mapname,"this")==0) {
  6953. TBL_PC *sd;
  6954. if (!script_rid2sd(sd))
  6955. return SCRIPT_CMD_SUCCESS; //Failed...
  6956. m = sd->bl.m;
  6957. }
  6958. else
  6959. m = map_mapname2mapid(mapname);
  6960. if ((id = itemdb_search(nameid))) {
  6961. char iden, ref, attr;
  6962. memset(&item_tmp,0,sizeof(item_tmp));
  6963. item_tmp.nameid = nameid;
  6964. iden = (char)script_getnum(st,7);
  6965. ref = (char)script_getnum(st,8);
  6966. attr = (char)script_getnum(st,9);
  6967. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6968. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6969. }
  6970. else if (id->type==IT_PETEGG) {
  6971. iden = 1;
  6972. ref = 0;
  6973. }
  6974. else {
  6975. iden = 1;
  6976. ref = attr = 0;
  6977. }
  6978. item_tmp.identify = iden;
  6979. item_tmp.refine = ref;
  6980. item_tmp.attribute = attr;
  6981. item_tmp.card[0] = script_getnum(st,10);
  6982. item_tmp.card[1] = script_getnum(st,11);
  6983. item_tmp.card[2] = script_getnum(st,12);
  6984. item_tmp.card[3] = script_getnum(st,13);
  6985. if (funcname[strlen(funcname)-1] == '3') {
  6986. int res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, 14);
  6987. if (res != SCRIPT_CMD_SUCCESS)
  6988. return res;
  6989. if (script_hasdata(st, 17)) {
  6990. if (script_getnum(st, 17) != 0)
  6991. canShowEffect = script_getnum(st, 17) != 0;
  6992. }
  6993. }
  6994. else {
  6995. if (script_hasdata(st, 14)) {
  6996. if (script_getnum(st, 14) != 0)
  6997. canShowEffect = script_getnum(st, 14) != 0;
  6998. }
  6999. }
  7000. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7001. }
  7002. else
  7003. return SCRIPT_CMD_FAILURE;
  7004. return SCRIPT_CMD_SUCCESS;
  7005. }
  7006. /// Counts / deletes the current item given by idx.
  7007. /// Used by buildin_delitem_search
  7008. /// Relies on all input data being already fully valid.
  7009. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  7010. {
  7011. int delamount;
  7012. struct item *itm = NULL;
  7013. struct s_storage *gstor = NULL;
  7014. switch(loc) {
  7015. case TABLE_CART:
  7016. itm = &sd->cart.u.items_cart[idx];
  7017. break;
  7018. case TABLE_STORAGE:
  7019. itm = &sd->storage.u.items_storage[idx];
  7020. break;
  7021. case TABLE_GUILD_STORAGE:
  7022. {
  7023. gstor = guild2storage2(sd->status.guild_id);
  7024. itm = &gstor->u.items_guild[idx];
  7025. }
  7026. break;
  7027. default: // TABLE_INVENTORY
  7028. itm = &sd->inventory.u.items_inventory[idx];
  7029. break;
  7030. }
  7031. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7032. if( delete_items )
  7033. {
  7034. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7035. {// delete associated pet
  7036. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7037. }
  7038. switch(loc) {
  7039. case TABLE_CART:
  7040. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7041. break;
  7042. case TABLE_STORAGE:
  7043. storage_delitem(sd,&sd->storage,idx,delamount);
  7044. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7045. break;
  7046. case TABLE_GUILD_STORAGE:
  7047. gstor->lock = true;
  7048. storage_guild_delitem(sd, gstor, idx, delamount);
  7049. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7050. storage_guild_storageclose(sd);
  7051. gstor->lock = false;
  7052. break;
  7053. default: // TABLE_INVENTORY
  7054. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7055. break;
  7056. }
  7057. }
  7058. amount[0]-= delamount;
  7059. }
  7060. /// Searches for item(s) and checks, if there is enough of them.
  7061. /// Used by delitem and delitem2
  7062. /// Relies on all input data being already fully valid.
  7063. /// @param exact_match will also match item by specified attributes
  7064. /// 0x0: Only item id
  7065. /// 0x1: identify, attributes, cards
  7066. /// 0x2: random option
  7067. /// @return true when all items could be deleted, false when there were not enough items to delete
  7068. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7069. {
  7070. bool delete_items = false;
  7071. int i, amount, size;
  7072. struct item *items;
  7073. // prefer always non-equipped items
  7074. it->equip = 0;
  7075. // when searching for nameid only, prefer additionally
  7076. if( !exact_match )
  7077. {
  7078. // non-refined items
  7079. it->refine = 0;
  7080. // card-less items
  7081. memset(it->card, 0, sizeof(it->card));
  7082. }
  7083. switch(loc) {
  7084. case TABLE_CART:
  7085. size = MAX_CART;
  7086. items = sd->cart.u.items_cart;
  7087. break;
  7088. case TABLE_STORAGE:
  7089. size = MAX_STORAGE;
  7090. items = sd->storage.u.items_storage;
  7091. break;
  7092. case TABLE_GUILD_STORAGE:
  7093. {
  7094. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7095. size = MAX_GUILD_STORAGE;
  7096. items = gstor->u.items_guild;
  7097. }
  7098. break;
  7099. default: // TABLE_INVENTORY
  7100. size = MAX_INVENTORY;
  7101. items = sd->inventory.u.items_inventory;
  7102. break;
  7103. }
  7104. for(;;)
  7105. {
  7106. unsigned short important = 0;
  7107. amount = it->amount;
  7108. // 1st pass -- less important items / exact match
  7109. for( i = 0; amount && i < size; i++ )
  7110. {
  7111. struct item *itm = NULL;
  7112. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7113. {// wrong/invalid item
  7114. continue;
  7115. }
  7116. if( itm->equip != it->equip || itm->refine != it->refine )
  7117. {// not matching attributes
  7118. important++;
  7119. continue;
  7120. }
  7121. if( exact_match )
  7122. {
  7123. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7124. {// not matching exact attributes
  7125. continue;
  7126. }
  7127. if (exact_match&0x2) {
  7128. uint8 j;
  7129. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7130. 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)
  7131. break;
  7132. }
  7133. if (j != MAX_ITEM_RDM_OPT)
  7134. continue;
  7135. }
  7136. }
  7137. else
  7138. {
  7139. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7140. {
  7141. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7142. {// pet which cannot be deleted
  7143. continue;
  7144. }
  7145. }
  7146. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7147. {// named/carded item
  7148. important++;
  7149. continue;
  7150. }
  7151. }
  7152. // count / delete item
  7153. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7154. }
  7155. // 2nd pass -- any matching item
  7156. if( amount == 0 || important == 0 )
  7157. {// either everything was already consumed or no items were skipped
  7158. ;
  7159. }
  7160. else for( i = 0; amount && i < size; i++ )
  7161. {
  7162. struct item *itm = NULL;
  7163. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7164. {// wrong/invalid item
  7165. continue;
  7166. }
  7167. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7168. {// pet which cannot be deleted
  7169. continue;
  7170. }
  7171. if( exact_match )
  7172. {
  7173. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7174. {// not matching attributes
  7175. continue;
  7176. }
  7177. if (exact_match&0x2) {
  7178. uint8 j;
  7179. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7180. 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)
  7181. break;
  7182. }
  7183. if (j != MAX_ITEM_RDM_OPT)
  7184. continue;
  7185. }
  7186. }
  7187. // count / delete item
  7188. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7189. }
  7190. if( amount )
  7191. {// not enough items
  7192. return false;
  7193. }
  7194. else if( delete_items )
  7195. {// we are done with the work
  7196. return true;
  7197. }
  7198. else
  7199. {// get rid of the items now
  7200. delete_items = true;
  7201. }
  7202. }
  7203. }
  7204. /// Deletes items from the target/attached player.
  7205. /// Prioritizes ordinary items.
  7206. ///
  7207. /// delitem <item id>,<amount>{,<account id>}
  7208. /// delitem "<item name>",<amount>{,<account id>}
  7209. /// cartdelitem <item id>,<amount>{,<account id>}
  7210. /// cartdelitem "<item name>",<amount>{,<account id>}
  7211. /// storagedelitem <item id>,<amount>{,<account id>}
  7212. /// storagedelitem "<item name>",<amount>{,<account id>}
  7213. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7214. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7215. BUILDIN_FUNC(delitem)
  7216. {
  7217. TBL_PC *sd;
  7218. struct item it;
  7219. uint8 loc = 0;
  7220. char* command = (char*)script_getfuncname(st);
  7221. if(!strncmp(command, "cart", 4))
  7222. loc = TABLE_CART;
  7223. else if(!strncmp(command, "storage", 7))
  7224. loc = TABLE_STORAGE;
  7225. else if(!strncmp(command, "guildstorage", 12))
  7226. loc = TABLE_GUILD_STORAGE;
  7227. if( !script_accid2sd(4,sd) ){
  7228. // In any case cancel script execution
  7229. st->state = END;
  7230. return SCRIPT_CMD_SUCCESS;
  7231. }
  7232. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7233. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7234. script_pushint(st, -1);
  7235. return SCRIPT_CMD_FAILURE;
  7236. }
  7237. if (loc == TABLE_GUILD_STORAGE) {
  7238. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7239. if (gstor == NULL || sd->state.storage_flag) {
  7240. script_pushint(st, -1);
  7241. return SCRIPT_CMD_FAILURE;
  7242. }
  7243. }
  7244. if( script_isstring(st, 2) )
  7245. {
  7246. const char* item_name = script_getstr(st, 2);
  7247. std::shared_ptr<item_data> id = item_db.searchname(item_name);
  7248. if( id == nullptr ){
  7249. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7250. st->state = END;
  7251. return SCRIPT_CMD_FAILURE;
  7252. }
  7253. it.nameid = id->nameid;// "<item name>"
  7254. }
  7255. else
  7256. {
  7257. it.nameid = script_getnum(st, 2);// <item id>
  7258. if( !itemdb_exists( it.nameid ) )
  7259. {
  7260. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7261. st->state = END;
  7262. return SCRIPT_CMD_FAILURE;
  7263. }
  7264. }
  7265. it.amount=script_getnum(st,3);
  7266. if( it.amount <= 0 )
  7267. return SCRIPT_CMD_SUCCESS;// nothing to do
  7268. if( buildin_delitem_search(sd, &it, 0, loc) )
  7269. {// success
  7270. return SCRIPT_CMD_SUCCESS;
  7271. }
  7272. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7273. st->state = END;
  7274. st->mes_active = 0;
  7275. clif_scriptclose(sd, st->oid);
  7276. return SCRIPT_CMD_FAILURE;
  7277. }
  7278. /// Deletes items from the target/attached player.
  7279. ///
  7280. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7281. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7282. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7283. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7284. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7285. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7286. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7287. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7288. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7289. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7290. BUILDIN_FUNC(delitem2)
  7291. {
  7292. TBL_PC *sd;
  7293. struct item it;
  7294. uint8 loc = 0;
  7295. char* command = (char*)script_getfuncname(st);
  7296. int aid_pos = 11;
  7297. uint8 flag = 0x1;
  7298. if(!strncmp(command, "cart", 4))
  7299. loc = TABLE_CART;
  7300. else if(!strncmp(command, "storage", 7))
  7301. loc = TABLE_STORAGE;
  7302. else if(!strncmp(command, "guildstorage", 12))
  7303. loc = TABLE_GUILD_STORAGE;
  7304. if (command[strlen(command)-1] == '3')
  7305. aid_pos = 14;
  7306. if( !script_accid2sd(aid_pos,sd) ){
  7307. // In any case cancel script execution
  7308. st->state = END;
  7309. return SCRIPT_CMD_SUCCESS;
  7310. }
  7311. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7312. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7313. script_pushint(st,-1);
  7314. return SCRIPT_CMD_FAILURE;
  7315. }
  7316. if (loc == TABLE_GUILD_STORAGE) {
  7317. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7318. if (gstor == NULL || sd->state.storage_flag) {
  7319. script_pushint(st, -1);
  7320. return SCRIPT_CMD_FAILURE;
  7321. }
  7322. }
  7323. memset(&it, 0, sizeof(it));
  7324. if( script_isstring(st, 2) )
  7325. {
  7326. const char* item_name = script_getstr(st, 2);
  7327. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  7328. if( id == nullptr ){
  7329. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7330. st->state = END;
  7331. return SCRIPT_CMD_FAILURE;
  7332. }
  7333. it.nameid = id->nameid;// "<item name>"
  7334. }
  7335. else
  7336. {
  7337. it.nameid = script_getnum(st, 2);// <item id>
  7338. if( !itemdb_exists( it.nameid ) )
  7339. {
  7340. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7341. st->state = END;
  7342. return SCRIPT_CMD_FAILURE;
  7343. }
  7344. }
  7345. it.amount=script_getnum(st,3);
  7346. it.identify=script_getnum(st,4);
  7347. it.refine=script_getnum(st,5);
  7348. it.attribute=script_getnum(st,6);
  7349. it.card[0]=script_getnum(st,7);
  7350. it.card[1]=script_getnum(st,8);
  7351. it.card[2]=script_getnum(st,9);
  7352. it.card[3]=script_getnum(st,10);
  7353. if (command[strlen(command)-1] == '3') {
  7354. int res = script_getitem_randomoption(st, sd, &it, command, 11);
  7355. if (res != SCRIPT_CMD_SUCCESS)
  7356. return SCRIPT_CMD_FAILURE;
  7357. flag |= 0x2;
  7358. }
  7359. if( it.amount <= 0 )
  7360. return SCRIPT_CMD_SUCCESS;// nothing to do
  7361. if( buildin_delitem_search(sd, &it, flag, loc) )
  7362. {// success
  7363. return SCRIPT_CMD_SUCCESS;
  7364. }
  7365. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7366. st->state = END;
  7367. st->mes_active = 0;
  7368. clif_scriptclose(sd, st->oid);
  7369. return SCRIPT_CMD_FAILURE;
  7370. }
  7371. /// Deletes items from the target/attached player at given index.
  7372. /// delitemidx <index>{,<amount>{,<char id>}};
  7373. BUILDIN_FUNC(delitemidx) {
  7374. struct map_session_data* sd;
  7375. if (!script_charid2sd(4, sd)) {
  7376. script_pushint(st, false);
  7377. return SCRIPT_CMD_FAILURE;
  7378. }
  7379. int idx = script_getnum(st, 2);
  7380. if (idx < 0 || idx >= MAX_INVENTORY) {
  7381. ShowWarning("buildin_delitemidx: Index %d is out of the range 0-%d.\n", idx, MAX_INVENTORY - 1);
  7382. script_pushint(st, false);
  7383. return SCRIPT_CMD_FAILURE;
  7384. }
  7385. if (sd->inventory_data[idx] == nullptr) {
  7386. 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);
  7387. script_pushint(st, false);
  7388. return SCRIPT_CMD_FAILURE;
  7389. }
  7390. int amount;
  7391. if (script_hasdata(st, 3))
  7392. amount = script_getnum(st, 3);
  7393. else
  7394. amount = sd->inventory.u.items_inventory[idx].amount;
  7395. if (amount > 0)
  7396. script_pushint(st, pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_SCRIPT) == 0);
  7397. else
  7398. script_pushint(st, false);
  7399. return SCRIPT_CMD_SUCCESS;
  7400. }
  7401. /*==========================================
  7402. * Enables/Disables use of items while in an NPC [Skotlex]
  7403. *------------------------------------------*/
  7404. BUILDIN_FUNC(enableitemuse)
  7405. {
  7406. TBL_PC *sd;
  7407. if (script_rid2sd(sd))
  7408. st->npc_item_flag = sd->npc_item_flag = 1;
  7409. return SCRIPT_CMD_SUCCESS;
  7410. }
  7411. BUILDIN_FUNC(disableitemuse)
  7412. {
  7413. TBL_PC *sd;
  7414. if (script_rid2sd(sd))
  7415. st->npc_item_flag = sd->npc_item_flag = 0;
  7416. return SCRIPT_CMD_SUCCESS;
  7417. }
  7418. /*==========================================
  7419. * Returns a character's specified stat.
  7420. * Check pc_readparam for available options.
  7421. * readparam <param>{,"<nick>"}
  7422. * readparam <param>{,<char_id>}
  7423. *------------------------------------------*/
  7424. BUILDIN_FUNC(readparam)
  7425. {
  7426. int value;
  7427. struct script_data *data = script_getdata(st, 2);
  7428. TBL_PC *sd = NULL;
  7429. if( script_hasdata(st, 3) ){
  7430. if (script_isint(st, 3))
  7431. script_charid2sd(3, sd);
  7432. else
  7433. script_nick2sd(3, sd);
  7434. }else{
  7435. script_rid2sd(sd);
  7436. }
  7437. if( !sd ){
  7438. script_pushint(st, -1);
  7439. return SCRIPT_CMD_FAILURE;
  7440. }
  7441. // If you use a parameter, return the value behind it
  7442. if( reference_toparam(data) ){
  7443. get_val_(st, data, sd);
  7444. value = (int)data->u.num;
  7445. }else{
  7446. value = static_cast<int>(pc_readparam(sd,script_getnum(st, 2)));
  7447. }
  7448. script_pushint(st,value);
  7449. return SCRIPT_CMD_SUCCESS;
  7450. }
  7451. /*==========================================
  7452. * Return charid identification
  7453. * return by @num :
  7454. * 0 : char_id
  7455. * 1 : party_id
  7456. * 2 : guild_id
  7457. * 3 : account_id
  7458. * 4 : bg_id
  7459. * 5 : clan_id
  7460. *------------------------------------------*/
  7461. BUILDIN_FUNC(getcharid)
  7462. {
  7463. int num;
  7464. TBL_PC *sd;
  7465. num = script_getnum(st,2);
  7466. if( !script_nick2sd(3,sd) ){
  7467. script_pushint(st,0); //return 0, according docs
  7468. return SCRIPT_CMD_SUCCESS;
  7469. }
  7470. switch( num ) {
  7471. case 0: script_pushint(st,sd->status.char_id); break;
  7472. case 1: script_pushint(st,sd->status.party_id); break;
  7473. case 2: script_pushint(st,sd->status.guild_id); break;
  7474. case 3: script_pushint(st,sd->status.account_id); break;
  7475. case 4: script_pushint(st,sd->bg_id); break;
  7476. case 5: script_pushint(st,sd->status.clan_id); break;
  7477. default:
  7478. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7479. script_pushint(st,0);
  7480. break;
  7481. }
  7482. return SCRIPT_CMD_SUCCESS;
  7483. }
  7484. /*==========================================
  7485. * returns the GID of an NPC
  7486. *------------------------------------------*/
  7487. BUILDIN_FUNC(getnpcid)
  7488. {
  7489. int num = script_getnum(st,2);
  7490. struct npc_data* nd = NULL;
  7491. if( script_hasdata(st,3) )
  7492. {// unique npc name
  7493. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7494. {
  7495. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7496. script_pushint(st,0);
  7497. return SCRIPT_CMD_FAILURE;
  7498. }
  7499. }
  7500. switch (num) {
  7501. case 0:
  7502. script_pushint(st,nd ? nd->bl.id : st->oid);
  7503. break;
  7504. default:
  7505. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7506. script_pushint(st,0);
  7507. return SCRIPT_CMD_FAILURE;
  7508. }
  7509. return SCRIPT_CMD_SUCCESS;
  7510. }
  7511. /*==========================================
  7512. * Return the name of the party_id
  7513. * null if not found
  7514. *------------------------------------------*/
  7515. BUILDIN_FUNC(getpartyname)
  7516. {
  7517. int party_id;
  7518. struct party_data* p;
  7519. party_id = script_getnum(st,2);
  7520. if( ( p = party_search(party_id) ) != NULL )
  7521. {
  7522. script_pushstrcopy(st,p->party.name);
  7523. }
  7524. else
  7525. {
  7526. script_pushconststr(st,"null");
  7527. }
  7528. return SCRIPT_CMD_SUCCESS;
  7529. }
  7530. /*==========================================
  7531. * Get the information of the members of a party by type
  7532. * @party_id, @type
  7533. * return by @type :
  7534. * - : nom des membres
  7535. * 1 : char_id des membres
  7536. * 2 : account_id des membres
  7537. *------------------------------------------*/
  7538. BUILDIN_FUNC(getpartymember)
  7539. {
  7540. struct party_data *p;
  7541. uint8 j = 0;
  7542. p = party_search(script_getnum(st,2));
  7543. if (p != NULL) {
  7544. uint8 i, type = 0;
  7545. struct script_data *data = NULL;
  7546. char *varname = NULL;
  7547. if (script_hasdata(st,3))
  7548. type = script_getnum(st,3);
  7549. if (script_hasdata(st,4)) {
  7550. data = script_getdata(st, 4);
  7551. if (!data_isreference(data)) {
  7552. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7553. return SCRIPT_CMD_FAILURE;
  7554. }
  7555. varname = reference_getname(data);
  7556. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7557. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7558. return SCRIPT_CMD_FAILURE;
  7559. }
  7560. if (not_server_variable(*varname)) {
  7561. struct map_session_data *sd;
  7562. if (!script_rid2sd(sd)) {
  7563. ShowError("buildin_getpartymember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  7564. return SCRIPT_CMD_FAILURE;
  7565. }
  7566. }
  7567. }
  7568. for (i = 0; i < MAX_PARTY; i++) {
  7569. if (p->party.member[i].account_id) {
  7570. switch (type) {
  7571. case 2:
  7572. if (data)
  7573. setd_sub_num( st, NULL, varname, j, p->party.member[i].account_id, data->ref );
  7574. else
  7575. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7576. break;
  7577. case 1:
  7578. if (data)
  7579. setd_sub_num( st, NULL, varname, j, p->party.member[i].char_id, data->ref );
  7580. else
  7581. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7582. break;
  7583. default:
  7584. if (data)
  7585. setd_sub_str( st, NULL, varname, j, p->party.member[i].name, data->ref );
  7586. else
  7587. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7588. break;
  7589. }
  7590. j++;
  7591. }
  7592. }
  7593. }
  7594. mapreg_setreg(add_str("$@partymembercount"),j);
  7595. script_pushint(st, j);
  7596. return SCRIPT_CMD_SUCCESS;
  7597. }
  7598. /*==========================================
  7599. * Retrieves party leader. if flag is specified,
  7600. * return some of the leader data. Otherwise, return name.
  7601. *------------------------------------------*/
  7602. BUILDIN_FUNC(getpartyleader)
  7603. {
  7604. int party_id, type = 0, i=0;
  7605. struct party_data *p;
  7606. party_id=script_getnum(st,2);
  7607. if( script_hasdata(st,3) )
  7608. type=script_getnum(st,3);
  7609. p=party_search(party_id);
  7610. if (p) //Search leader
  7611. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7612. if (!p || i == MAX_PARTY) { //leader not found
  7613. if (type)
  7614. script_pushint(st,-1);
  7615. else
  7616. script_pushconststr(st,"null");
  7617. return SCRIPT_CMD_SUCCESS;
  7618. }
  7619. switch (type) {
  7620. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7621. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7622. case 3: script_pushint(st,p->party.member[i].class_); break;
  7623. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7624. case 5: script_pushint(st,p->party.member[i].lv); break;
  7625. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7626. }
  7627. return SCRIPT_CMD_SUCCESS;
  7628. }
  7629. /*==========================================
  7630. * Return the name of the @guild_id
  7631. * null if not found
  7632. *------------------------------------------*/
  7633. BUILDIN_FUNC(getguildname)
  7634. {
  7635. int guild_id;
  7636. struct guild* g;
  7637. guild_id = script_getnum(st,2);
  7638. if( ( g = guild_search(guild_id) ) != NULL )
  7639. script_pushstrcopy(st,g->name);
  7640. else
  7641. script_pushconststr(st,"null");
  7642. return SCRIPT_CMD_SUCCESS;
  7643. }
  7644. /*==========================================
  7645. * Return the name of the guild master of @guild_id
  7646. * null if not found
  7647. *------------------------------------------*/
  7648. BUILDIN_FUNC(getguildmaster)
  7649. {
  7650. int guild_id;
  7651. struct guild* g;
  7652. guild_id = script_getnum(st,2);
  7653. if( ( g = guild_search(guild_id) ) != NULL )
  7654. script_pushstrcopy(st,g->member[0].name);
  7655. else
  7656. script_pushconststr(st,"null");
  7657. return SCRIPT_CMD_SUCCESS;
  7658. }
  7659. BUILDIN_FUNC(getguildmasterid)
  7660. {
  7661. int guild_id;
  7662. struct guild* g;
  7663. guild_id = script_getnum(st,2);
  7664. if( ( g = guild_search(guild_id) ) != NULL )
  7665. script_pushint(st,g->member[0].char_id);
  7666. else
  7667. script_pushint(st,0);
  7668. return SCRIPT_CMD_SUCCESS;
  7669. }
  7670. /*==========================================
  7671. * Get char string information by type :
  7672. * Return by @type :
  7673. * 0 : char_name
  7674. * 1 : party_name or ""
  7675. * 2 : guild_name or ""
  7676. * 3 : map_name
  7677. * - : ""
  7678. * strcharinfo(<type>{,<char_id>})
  7679. *------------------------------------------*/
  7680. BUILDIN_FUNC(strcharinfo)
  7681. {
  7682. TBL_PC *sd;
  7683. int num;
  7684. struct guild* g;
  7685. struct party_data* p;
  7686. if (!script_charid2sd(3,sd)) {
  7687. script_pushconststr(st,"");
  7688. return SCRIPT_CMD_FAILURE;
  7689. }
  7690. num=script_getnum(st,2);
  7691. switch(num){
  7692. case 0:
  7693. script_pushstrcopy(st,sd->status.name);
  7694. break;
  7695. case 1:
  7696. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7697. script_pushstrcopy(st,p->party.name);
  7698. } else {
  7699. script_pushconststr(st,"");
  7700. }
  7701. break;
  7702. case 2:
  7703. if( ( g = sd->guild ) != NULL ) {
  7704. script_pushstrcopy(st,g->name);
  7705. } else {
  7706. script_pushconststr(st,"");
  7707. }
  7708. break;
  7709. case 3:
  7710. script_pushconststr(st,map_getmapdata(sd->bl.m)->name);
  7711. break;
  7712. default:
  7713. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7714. script_pushconststr(st,"");
  7715. break;
  7716. }
  7717. return SCRIPT_CMD_SUCCESS;
  7718. }
  7719. /*==========================================
  7720. * Get npc string information by type
  7721. * return by @type:
  7722. * 0 : name
  7723. * 1 : str#
  7724. * 2 : #str
  7725. * 3 : ::str
  7726. * 4 : map name
  7727. *------------------------------------------*/
  7728. BUILDIN_FUNC(strnpcinfo)
  7729. {
  7730. TBL_NPC* nd;
  7731. int num;
  7732. char *buf,*name=NULL;
  7733. nd = map_id2nd(st->oid);
  7734. if (!nd) {
  7735. script_pushconststr(st, "");
  7736. return SCRIPT_CMD_SUCCESS;
  7737. }
  7738. num = script_getnum(st,2);
  7739. switch(num){
  7740. case 0: // display name
  7741. name = aStrdup(nd->name);
  7742. break;
  7743. case 1: // visible part of display name
  7744. if((buf = strchr(nd->name,'#')) != NULL)
  7745. {
  7746. name = aStrdup(nd->name);
  7747. name[buf - nd->name] = 0;
  7748. } else // Return the name, there is no '#' present
  7749. name = aStrdup(nd->name);
  7750. break;
  7751. case 2: // # fragment
  7752. if((buf = strchr(nd->name,'#')) != NULL)
  7753. name = aStrdup(buf+1);
  7754. break;
  7755. case 3: // unique name
  7756. name = aStrdup(nd->exname);
  7757. break;
  7758. case 4: // map name
  7759. if (nd->bl.m >= 0)
  7760. name = aStrdup(map_getmapdata(nd->bl.m)->name);
  7761. break;
  7762. }
  7763. if(name)
  7764. script_pushstr(st, name);
  7765. else
  7766. script_pushconststr(st, "");
  7767. return SCRIPT_CMD_SUCCESS;
  7768. }
  7769. /**
  7770. * getequipid({<equipment slot>,<char_id>})
  7771. **/
  7772. BUILDIN_FUNC(getequipid)
  7773. {
  7774. int i, num;
  7775. TBL_PC* sd;
  7776. if (!script_charid2sd(3, sd)) {
  7777. script_pushint(st,-1);
  7778. return SCRIPT_CMD_FAILURE;
  7779. }
  7780. if (script_hasdata(st, 2))
  7781. num = script_getnum(st, 2);
  7782. else
  7783. num = EQI_COMPOUND_ON;
  7784. if (num == EQI_COMPOUND_ON)
  7785. i = current_equip_item_index;
  7786. else if (equip_index_check(num)) // get inventory position of item
  7787. i = pc_checkequip(sd, equip_bitmask[num]);
  7788. else {
  7789. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7790. script_pushint(st,-1);
  7791. return SCRIPT_CMD_FAILURE;
  7792. }
  7793. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7794. script_pushint(st, sd->inventory_data[i]->nameid);
  7795. else
  7796. script_pushint(st, -1);
  7797. return SCRIPT_CMD_SUCCESS;
  7798. }
  7799. /*==========================================
  7800. * getequipuniqueid(<equipment slot>{,<char_id>})
  7801. *------------------------------------------*/
  7802. BUILDIN_FUNC(getequipuniqueid)
  7803. {
  7804. int i, num;
  7805. TBL_PC* sd;
  7806. struct item* item;
  7807. if (!script_charid2sd(3, sd)) {
  7808. script_pushconststr(st, "");
  7809. return SCRIPT_CMD_FAILURE;
  7810. }
  7811. num = script_getnum(st,2);
  7812. if ( !equip_index_check(num) ) {
  7813. script_pushconststr(st, "");
  7814. return SCRIPT_CMD_FAILURE;
  7815. }
  7816. // get inventory position of item
  7817. i = pc_checkequip(sd,equip_bitmask[num]);
  7818. if (i < 0) {
  7819. script_pushconststr(st, "");
  7820. return SCRIPT_CMD_FAILURE;
  7821. }
  7822. item = &sd->inventory.u.items_inventory[i];
  7823. if (item != 0) {
  7824. int maxlen = 256;
  7825. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7826. memset(buf, 0, maxlen);
  7827. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7828. script_pushstr(st, buf);
  7829. } else
  7830. script_pushconststr(st, "");
  7831. return SCRIPT_CMD_SUCCESS;
  7832. }
  7833. /*==========================================
  7834. * Get the equipement name at pos
  7835. * return item jname or ""
  7836. * getequipname(<equipment slot>{,<char_id>})
  7837. *------------------------------------------*/
  7838. BUILDIN_FUNC(getequipname)
  7839. {
  7840. int i, num;
  7841. TBL_PC* sd;
  7842. struct item_data* item;
  7843. if (!script_charid2sd(3, sd)) {
  7844. script_pushconststr(st, "");
  7845. return SCRIPT_CMD_FAILURE;
  7846. }
  7847. num = script_getnum(st,2);
  7848. if( !equip_index_check(num) )
  7849. {
  7850. script_pushconststr(st,"");
  7851. return SCRIPT_CMD_SUCCESS;
  7852. }
  7853. // get inventory position of item
  7854. i = pc_checkequip(sd,equip_bitmask[num]);
  7855. if( i < 0 )
  7856. {
  7857. script_pushconststr(st,"");
  7858. return SCRIPT_CMD_SUCCESS;
  7859. }
  7860. item = sd->inventory_data[i];
  7861. if( item != 0 )
  7862. script_pushstrcopy(st,item->ename.c_str());
  7863. else
  7864. script_pushconststr(st,"");
  7865. return SCRIPT_CMD_SUCCESS;
  7866. }
  7867. /*==========================================
  7868. * getbrokenid [Valaris]
  7869. * getbrokenid(<number>{,<char_id>})
  7870. *------------------------------------------*/
  7871. BUILDIN_FUNC(getbrokenid)
  7872. {
  7873. int i,num,id = 0,brokencounter = 0;
  7874. TBL_PC *sd;
  7875. if (!script_charid2sd(3, sd)) {
  7876. script_pushint(st,0);
  7877. return SCRIPT_CMD_FAILURE;
  7878. }
  7879. num = script_getnum(st,2);
  7880. for(i = 0; i < MAX_INVENTORY; i++) {
  7881. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  7882. brokencounter++;
  7883. if(num == brokencounter){
  7884. id = sd->inventory.u.items_inventory[i].nameid;
  7885. break;
  7886. }
  7887. }
  7888. }
  7889. script_pushint(st,id);
  7890. return SCRIPT_CMD_SUCCESS;
  7891. }
  7892. /*==========================================
  7893. * repair [Valaris]
  7894. * repair <num>{,<char_id>};
  7895. *------------------------------------------*/
  7896. BUILDIN_FUNC(repair)
  7897. {
  7898. int i,num;
  7899. int repaircounter = 0;
  7900. TBL_PC *sd;
  7901. if (!script_charid2sd(3,sd))
  7902. return SCRIPT_CMD_FAILURE;
  7903. num = script_getnum(st,2);
  7904. for(i = 0; i < MAX_INVENTORY; i++) {
  7905. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  7906. repaircounter++;
  7907. if(num == repaircounter) {
  7908. sd->inventory.u.items_inventory[i].attribute = 0;
  7909. clif_equiplist(sd);
  7910. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7911. clif_misceffect(&sd->bl, 3);
  7912. break;
  7913. }
  7914. }
  7915. }
  7916. return SCRIPT_CMD_SUCCESS;
  7917. }
  7918. /*==========================================
  7919. * repairall {<char_id>}
  7920. *------------------------------------------*/
  7921. BUILDIN_FUNC(repairall)
  7922. {
  7923. int i, repaircounter = 0;
  7924. TBL_PC *sd;
  7925. if (!script_charid2sd(2,sd))
  7926. return SCRIPT_CMD_FAILURE;
  7927. for(i = 0; i < MAX_INVENTORY; i++)
  7928. {
  7929. 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] ) ){
  7930. sd->inventory.u.items_inventory[i].attribute = 0;
  7931. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7932. repaircounter++;
  7933. }
  7934. }
  7935. if(repaircounter)
  7936. {
  7937. clif_misceffect(&sd->bl, 3);
  7938. clif_equiplist(sd);
  7939. }
  7940. return SCRIPT_CMD_SUCCESS;
  7941. }
  7942. /*==========================================
  7943. * Chk if player have something equiped at pos
  7944. * getequipisequiped <pos>{,<char_id>}
  7945. *------------------------------------------*/
  7946. BUILDIN_FUNC(getequipisequiped)
  7947. {
  7948. int i = -1,num;
  7949. TBL_PC *sd;
  7950. num = script_getnum(st,2);
  7951. if (!script_charid2sd(3,sd))
  7952. return SCRIPT_CMD_FAILURE;
  7953. if ( equip_index_check(num) )
  7954. i=pc_checkequip(sd,equip_bitmask[num]);
  7955. if(i >= 0)
  7956. script_pushint(st,1);
  7957. else
  7958. script_pushint(st,0);
  7959. return SCRIPT_CMD_SUCCESS;
  7960. }
  7961. /*==========================================
  7962. * Chk if the player have something equiped at pos
  7963. * if so chk if this item ain't marked not refinable or rental
  7964. * return (npc)
  7965. * 1 : true
  7966. * 0 : false
  7967. * getequipisenableref(<equipment slot>{,<char_id>})
  7968. *------------------------------------------*/
  7969. BUILDIN_FUNC(getequipisenableref)
  7970. {
  7971. int i = -1,num;
  7972. TBL_PC *sd;
  7973. num = script_getnum(st,2);
  7974. if (!script_charid2sd(3, sd)) {
  7975. script_pushint(st,0);
  7976. return SCRIPT_CMD_FAILURE;
  7977. }
  7978. if( equip_index_check(num) )
  7979. i = pc_checkequip(sd,equip_bitmask[num]);
  7980. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7981. script_pushint(st,1);
  7982. else
  7983. script_pushint(st,0);
  7984. return SCRIPT_CMD_SUCCESS;
  7985. }
  7986. /*==========================================
  7987. * Get the item refined value at pos
  7988. * return (npc)
  7989. * x : refine amount
  7990. * 0 : false (not refined)
  7991. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7992. *------------------------------------------*/
  7993. BUILDIN_FUNC(getequiprefinerycnt)
  7994. {
  7995. int i = -1,num;
  7996. TBL_PC *sd;
  7997. num = script_getnum(st,2);
  7998. if (!script_charid2sd(3, sd)) {
  7999. script_pushint(st,0);
  8000. return SCRIPT_CMD_FAILURE;
  8001. }
  8002. if (equip_index_check(num))
  8003. i=pc_checkequip(sd,equip_bitmask[num]);
  8004. if(i >= 0)
  8005. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  8006. else
  8007. script_pushint(st,0);
  8008. return SCRIPT_CMD_SUCCESS;
  8009. }
  8010. /*==========================================
  8011. * Get the weapon level value at pos
  8012. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  8013. * return (npc)
  8014. * x : weapon level
  8015. * 0 : false
  8016. * getequipweaponlv({<equipment slot>{,<char_id>}})
  8017. *------------------------------------------*/
  8018. BUILDIN_FUNC(getequipweaponlv)
  8019. {
  8020. int num;
  8021. if( script_hasdata( st, 2 ) ){
  8022. num = script_getnum(st, 2);
  8023. }else{
  8024. num = EQI_COMPOUND_ON;
  8025. }
  8026. struct map_session_data* sd;
  8027. if (!script_charid2sd(3, sd)) {
  8028. script_pushint(st, 0);
  8029. return SCRIPT_CMD_FAILURE;
  8030. }
  8031. int i = -1;
  8032. if( num == EQI_COMPOUND_ON ){
  8033. if( current_equip_item_index == -1 ){
  8034. script_pushint(st, 0);
  8035. return SCRIPT_CMD_FAILURE;
  8036. }
  8037. i = current_equip_item_index;
  8038. }else if (equip_index_check(num))
  8039. i = pc_checkequip(sd, equip_bitmask[num]);
  8040. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON ){
  8041. script_pushint( st, sd->inventory_data[i]->weapon_level );
  8042. }else{
  8043. script_pushint( st, 0 );
  8044. }
  8045. return SCRIPT_CMD_SUCCESS;
  8046. }
  8047. BUILDIN_FUNC(getequiparmorlv){
  8048. int num;
  8049. if( script_hasdata( st, 2 ) ){
  8050. num = script_getnum(st, 2);
  8051. }else{
  8052. num = EQI_COMPOUND_ON;
  8053. }
  8054. struct map_session_data* sd;
  8055. if( !script_charid2sd( 3, sd ) ){
  8056. script_pushint( st, 0 );
  8057. return SCRIPT_CMD_FAILURE;
  8058. }
  8059. int i = -1;
  8060. if( num == EQI_COMPOUND_ON ){
  8061. if( current_equip_item_index == -1 ){
  8062. script_pushint( st, 0 );
  8063. return SCRIPT_CMD_FAILURE;
  8064. }
  8065. i = current_equip_item_index;
  8066. }else if( equip_index_check( num ) ){
  8067. i = pc_checkequip( sd, equip_bitmask[num] );
  8068. }else{
  8069. ShowError( "buildin_getequiparmorlv: unsupported equip index %d.\n", num );
  8070. script_pushint( st, 0 );
  8071. return SCRIPT_CMD_FAILURE;
  8072. }
  8073. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_ARMOR ){
  8074. script_pushint( st, sd->inventory_data[i]->armor_level );
  8075. }else{
  8076. script_pushint( st, 0 );
  8077. }
  8078. return SCRIPT_CMD_SUCCESS;
  8079. }
  8080. /*==========================================
  8081. * Get the item refine chance (from refine.txt) for item at pos
  8082. * return (npc)
  8083. * x : refine chance
  8084. * 0 : false (max refine level or unequip..)
  8085. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8086. *------------------------------------------*/
  8087. BUILDIN_FUNC(getequippercentrefinery)
  8088. {
  8089. int i = -1,num;
  8090. bool enriched = false;
  8091. TBL_PC *sd;
  8092. num = script_getnum(st,2);
  8093. if (script_hasdata(st, 3))
  8094. enriched = script_getnum(st, 3) != 0;
  8095. if (!script_charid2sd(4, sd)) {
  8096. script_pushint(st,0);
  8097. return SCRIPT_CMD_FAILURE;
  8098. }
  8099. if (equip_index_check(num))
  8100. i = pc_checkequip(sd,equip_bitmask[num]);
  8101. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid) {
  8102. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  8103. if( info == nullptr ){
  8104. script_pushint( st, 0 );
  8105. return SCRIPT_CMD_SUCCESS;
  8106. }
  8107. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, (uint16)( enriched ? REFINE_COST_ENRICHED : REFINE_COST_NORMAL ) );
  8108. if( cost == nullptr ){
  8109. script_pushint( st, 0 );
  8110. return SCRIPT_CMD_SUCCESS;
  8111. }
  8112. script_pushint( st, cost->chance / 100 );
  8113. }
  8114. else
  8115. script_pushint(st,0);
  8116. return SCRIPT_CMD_SUCCESS;
  8117. }
  8118. /*==========================================
  8119. * Refine +1 item at pos and log and display refine
  8120. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8121. *------------------------------------------*/
  8122. BUILDIN_FUNC(successrefitem) {
  8123. short i = -1, up = 1;
  8124. int pos;
  8125. TBL_PC *sd;
  8126. pos = script_getnum(st,2);
  8127. if (!script_charid2sd(4, sd)) {
  8128. script_pushint(st, -1);
  8129. return SCRIPT_CMD_FAILURE;
  8130. }
  8131. if (script_hasdata(st, 3))
  8132. up = script_getnum(st, 3);
  8133. if (equip_index_check(pos))
  8134. i = pc_checkequip(sd,equip_bitmask[pos]);
  8135. if (i >= 0) {
  8136. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8137. //Logs items, got from (N)PC scripts [Lupus]
  8138. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8139. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8140. script_pushint(st, MAX_REFINE);
  8141. return SCRIPT_CMD_SUCCESS;
  8142. }
  8143. sd->inventory.u.items_inventory[i].refine += up;
  8144. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8145. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8146. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8147. clif_delitem(sd,i,1,3);
  8148. //Logs items, got from (N)PC scripts [Lupus]
  8149. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8150. clif_additem(sd,i,1,0);
  8151. pc_equipitem(sd,i,ep);
  8152. clif_misceffect(&sd->bl,3);
  8153. if( sd->inventory_data[i]->type == IT_WEAPON ){
  8154. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->weapon_level, sd->inventory.u.items_inventory[i].refine);
  8155. }
  8156. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8157. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8158. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]) &&
  8159. sd->inventory_data[i]->type == IT_WEAPON )
  8160. { // Fame point system [DracoRPG]
  8161. switch( sd->inventory_data[i]->weapon_level ){
  8162. case 1:
  8163. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8164. break;
  8165. case 2:
  8166. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8167. break;
  8168. case 3:
  8169. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8170. break;
  8171. }
  8172. }
  8173. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8174. return SCRIPT_CMD_SUCCESS;
  8175. }
  8176. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8177. script_pushint(st, -1);
  8178. return SCRIPT_CMD_FAILURE;
  8179. }
  8180. /*==========================================
  8181. * Show a failed Refine +1 attempt
  8182. * failedrefitem <equipment slot>{,<char_id>}
  8183. *------------------------------------------*/
  8184. BUILDIN_FUNC(failedrefitem) {
  8185. short i = -1;
  8186. int pos;
  8187. TBL_PC *sd;
  8188. pos = script_getnum(st,2);
  8189. if (!script_charid2sd(3, sd)) {
  8190. script_pushint(st, 0);
  8191. return SCRIPT_CMD_FAILURE;
  8192. }
  8193. if (equip_index_check(pos))
  8194. i = pc_checkequip(sd,equip_bitmask[pos]);
  8195. if (i >= 0) {
  8196. sd->inventory.u.items_inventory[i].refine = 0;
  8197. pc_unequipitem(sd,i,3); //recalculate bonus
  8198. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8199. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8200. clif_misceffect(&sd->bl,2); // display failure effect
  8201. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8202. script_pushint(st, 1);
  8203. return SCRIPT_CMD_SUCCESS;
  8204. }
  8205. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8206. script_pushint(st, 0);
  8207. return SCRIPT_CMD_FAILURE;
  8208. }
  8209. /*==========================================
  8210. * Downgrades an Equipment Part by -1 . [Masao]
  8211. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8212. *------------------------------------------*/
  8213. BUILDIN_FUNC(downrefitem) {
  8214. short i = -1, down = 1;
  8215. int pos;
  8216. TBL_PC *sd;
  8217. if (!script_charid2sd(4, sd)) {
  8218. script_pushint(st, -1);
  8219. return SCRIPT_CMD_FAILURE;
  8220. }
  8221. pos = script_getnum(st,2);
  8222. if (script_hasdata(st, 3))
  8223. down = script_getnum(st, 3);
  8224. if (equip_index_check(pos))
  8225. i = pc_checkequip(sd,equip_bitmask[pos]);
  8226. if (i >= 0) {
  8227. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8228. //Logs items, got from (N)PC scripts [Lupus]
  8229. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8230. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8231. sd->inventory.u.items_inventory[i].refine -= down;
  8232. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8233. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8234. clif_delitem(sd,i,1,3);
  8235. //Logs items, got from (N)PC scripts [Lupus]
  8236. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8237. clif_additem(sd,i,1,0);
  8238. pc_equipitem(sd,i,ep);
  8239. clif_misceffect(&sd->bl,2);
  8240. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8241. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8242. return SCRIPT_CMD_SUCCESS;
  8243. }
  8244. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8245. script_pushint(st, -1);
  8246. return SCRIPT_CMD_FAILURE;
  8247. }
  8248. /**
  8249. * Delete the item equipped at pos.
  8250. * delequip <equipment slot>{,<char_id>};
  8251. **/
  8252. BUILDIN_FUNC(delequip) {
  8253. short i = -1;
  8254. int pos;
  8255. int8 ret;
  8256. TBL_PC *sd;
  8257. pos = script_getnum(st,2);
  8258. if (!script_charid2sd(3,sd)) {
  8259. st->state = END;
  8260. return SCRIPT_CMD_FAILURE;
  8261. }
  8262. if (equip_index_check(pos))
  8263. i = pc_checkequip(sd,equip_bitmask[pos]);
  8264. if (i >= 0) {
  8265. pc_unequipitem(sd,i,3); //recalculate bonus
  8266. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8267. }
  8268. else {
  8269. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8270. st->state = END;
  8271. return SCRIPT_CMD_FAILURE;
  8272. }
  8273. script_pushint(st,ret);
  8274. return SCRIPT_CMD_SUCCESS;
  8275. }
  8276. /**
  8277. * Break the item equipped at pos.
  8278. * breakequip <equipment slot>{,<char_id>};
  8279. **/
  8280. BUILDIN_FUNC(breakequip) {
  8281. short i = -1;
  8282. int pos;
  8283. TBL_PC *sd;
  8284. pos = script_getnum(st,2);
  8285. if (!script_charid2sd(3,sd))
  8286. return SCRIPT_CMD_FAILURE;
  8287. if (equip_index_check(pos))
  8288. i = pc_checkequip(sd,equip_bitmask[pos]);
  8289. if (i >= 0) {
  8290. sd->inventory.u.items_inventory[i].attribute = 1;
  8291. pc_unequipitem(sd,i,3);
  8292. clif_equiplist(sd);
  8293. script_pushint(st,1);
  8294. return SCRIPT_CMD_SUCCESS;
  8295. }
  8296. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8297. script_pushint(st,0);
  8298. return SCRIPT_CMD_FAILURE;
  8299. }
  8300. /**
  8301. * statusup <stat>{,<char_id>};
  8302. **/
  8303. BUILDIN_FUNC(statusup)
  8304. {
  8305. int type;
  8306. TBL_PC *sd;
  8307. type = script_getnum(st,2);
  8308. if (!script_charid2sd(3, sd))
  8309. return SCRIPT_CMD_FAILURE;
  8310. pc_statusup(sd, type, 1);
  8311. return SCRIPT_CMD_SUCCESS;
  8312. }
  8313. /**
  8314. * statusup2 <stat>,<amount>{,<char_id>};
  8315. **/
  8316. BUILDIN_FUNC(statusup2)
  8317. {
  8318. int type,val;
  8319. TBL_PC *sd;
  8320. type=script_getnum(st,2);
  8321. val=script_getnum(st,3);
  8322. if (!script_charid2sd(4, sd))
  8323. return SCRIPT_CMD_FAILURE;
  8324. pc_statusup2(sd,type,val);
  8325. return SCRIPT_CMD_SUCCESS;
  8326. }
  8327. /**
  8328. * traitstatusup <stat>{,<char_id>};
  8329. **/
  8330. BUILDIN_FUNC(traitstatusup)
  8331. {
  8332. struct map_session_data *sd;
  8333. if (!script_charid2sd(3, sd))
  8334. return SCRIPT_CMD_FAILURE;
  8335. int type = script_getnum( st, 2 );
  8336. if( type < SP_POW || type > SP_CRT ){
  8337. ShowError( "buildin_traitstatusup: Unknown trait type %d\n", type );
  8338. return SCRIPT_CMD_FAILURE;
  8339. }
  8340. pc_traitstatusup( sd, type, 1 );
  8341. return SCRIPT_CMD_SUCCESS;
  8342. }
  8343. /**
  8344. * traitstatusup2 <stat>,<amount>{,<char_id>};
  8345. **/
  8346. BUILDIN_FUNC(traitstatusup2)
  8347. {
  8348. struct map_session_data *sd;
  8349. if (!script_charid2sd(4, sd))
  8350. return SCRIPT_CMD_FAILURE;
  8351. int type = script_getnum( st, 2 );
  8352. if( type < SP_POW || type > SP_CRT ){
  8353. ShowError( "buildin_traitstatusup2: Unknown trait type %d\n", type );
  8354. return SCRIPT_CMD_FAILURE;
  8355. }
  8356. pc_traitstatusup2( sd, type, script_getnum( st, 3 ) );
  8357. return SCRIPT_CMD_SUCCESS;
  8358. }
  8359. /// See 'doc/item_bonus.txt'
  8360. ///
  8361. /// bonus <bonus type>,<val1>;
  8362. /// bonus2 <bonus type>,<val1>,<val2>;
  8363. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8364. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8365. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8366. BUILDIN_FUNC(bonus)
  8367. {
  8368. int type;
  8369. int val1 = 0;
  8370. int val2 = 0;
  8371. int val3 = 0;
  8372. int val4 = 0;
  8373. int val5 = 0;
  8374. TBL_PC* sd;
  8375. if( !script_rid2sd(sd) )
  8376. return SCRIPT_CMD_SUCCESS; // no player attached
  8377. type = script_getnum(st,2);
  8378. switch( type ) {
  8379. case SP_AUTOSPELL:
  8380. case SP_AUTOSPELL_WHENHIT:
  8381. case SP_AUTOSPELL_ONSKILL:
  8382. case SP_SKILL_ATK:
  8383. case SP_SKILL_HEAL:
  8384. case SP_SKILL_HEAL2:
  8385. case SP_ADD_SKILL_BLOW:
  8386. case SP_CASTRATE:
  8387. case SP_ADDEFF_ONSKILL:
  8388. case SP_SKILL_USE_SP_RATE:
  8389. case SP_SKILL_COOLDOWN:
  8390. case SP_SKILL_FIXEDCAST:
  8391. case SP_SKILL_VARIABLECAST:
  8392. case SP_VARCASTRATE:
  8393. case SP_FIXCASTRATE:
  8394. case SP_SKILL_DELAY:
  8395. case SP_SKILL_USE_SP:
  8396. case SP_SUB_SKILL:
  8397. // these bonuses support skill names
  8398. if (script_isstring(st, 3)) {
  8399. const char *name = script_getstr(st, 3);
  8400. if (!(val1 = skill_name2id(name))) {
  8401. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8402. return SCRIPT_CMD_FAILURE;
  8403. }
  8404. } else {
  8405. val1 = script_getnum(st, 3);
  8406. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8407. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8408. return SCRIPT_CMD_FAILURE;
  8409. }
  8410. }
  8411. break;
  8412. default:
  8413. if (script_hasdata(st, 3))
  8414. val1 = script_getnum(st, 3);
  8415. break;
  8416. }
  8417. switch( script_lastdata(st)-2 ) {
  8418. case 0:
  8419. case 1:
  8420. pc_bonus(sd, type, val1);
  8421. break;
  8422. case 2:
  8423. val2 = script_getnum(st,4);
  8424. pc_bonus2(sd, type, val1, val2);
  8425. break;
  8426. case 3:
  8427. val2 = script_getnum(st,4);
  8428. val3 = script_getnum(st,5);
  8429. pc_bonus3(sd, type, val1, val2, val3);
  8430. break;
  8431. case 4:
  8432. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8433. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8434. else
  8435. val2 = script_getnum(st,4);
  8436. val3 = script_getnum(st,5);
  8437. val4 = script_getnum(st,6);
  8438. pc_bonus4(sd, type, val1, val2, val3, val4);
  8439. break;
  8440. case 5:
  8441. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8442. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8443. else
  8444. val2 = script_getnum(st,4);
  8445. val3 = script_getnum(st,5);
  8446. val4 = script_getnum(st,6);
  8447. val5 = script_getnum(st,7);
  8448. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8449. break;
  8450. default:
  8451. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8452. break;
  8453. }
  8454. return SCRIPT_CMD_SUCCESS;
  8455. }
  8456. BUILDIN_FUNC(autobonus)
  8457. {
  8458. unsigned int dur, pos;
  8459. short rate;
  8460. uint16 atk_type = 0;
  8461. TBL_PC* sd;
  8462. const char *bonus_script, *other_script = NULL;
  8463. if( !script_rid2sd(sd) )
  8464. return SCRIPT_CMD_SUCCESS; // no player attached
  8465. if (current_equip_combo_pos)
  8466. pos = current_equip_combo_pos;
  8467. else
  8468. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8469. rate = script_getnum(st,3);
  8470. dur = script_getnum(st,4);
  8471. bonus_script = script_getstr(st,2);
  8472. if( !rate || !dur || !pos || !bonus_script )
  8473. return SCRIPT_CMD_SUCCESS;
  8474. if( script_hasdata(st,5) )
  8475. atk_type = script_getnum(st,5);
  8476. if( script_hasdata(st,6) )
  8477. other_script = script_getstr(st,6);
  8478. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8479. {
  8480. script_add_autobonus(bonus_script);
  8481. if( other_script )
  8482. script_add_autobonus(other_script);
  8483. }
  8484. return SCRIPT_CMD_SUCCESS;
  8485. }
  8486. BUILDIN_FUNC(autobonus2)
  8487. {
  8488. unsigned int dur, pos;
  8489. short rate;
  8490. uint16 atk_type = 0;
  8491. TBL_PC* sd;
  8492. const char *bonus_script, *other_script = NULL;
  8493. if( !script_rid2sd(sd) )
  8494. return SCRIPT_CMD_SUCCESS; // no player attached
  8495. if (current_equip_combo_pos)
  8496. pos = current_equip_combo_pos;
  8497. else
  8498. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8499. rate = script_getnum(st,3);
  8500. dur = script_getnum(st,4);
  8501. bonus_script = script_getstr(st,2);
  8502. if( !rate || !dur || !pos || !bonus_script )
  8503. return SCRIPT_CMD_SUCCESS;
  8504. if( script_hasdata(st,5) )
  8505. atk_type = script_getnum(st,5);
  8506. if( script_hasdata(st,6) )
  8507. other_script = script_getstr(st,6);
  8508. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8509. {
  8510. script_add_autobonus(bonus_script);
  8511. if( other_script )
  8512. script_add_autobonus(other_script);
  8513. }
  8514. return SCRIPT_CMD_SUCCESS;
  8515. }
  8516. BUILDIN_FUNC(autobonus3)
  8517. {
  8518. unsigned int dur, pos;
  8519. short rate;
  8520. uint16 skill_id = 0;
  8521. TBL_PC* sd;
  8522. const char *bonus_script, *other_script = NULL;
  8523. if( !script_rid2sd(sd) )
  8524. return SCRIPT_CMD_SUCCESS; // no player attached
  8525. if (current_equip_combo_pos)
  8526. pos = current_equip_combo_pos;
  8527. else
  8528. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8529. rate = script_getnum(st,3);
  8530. dur = script_getnum(st,4);
  8531. if (script_isstring(st, 5)) {
  8532. const char *name = script_getstr(st, 5);
  8533. if (!(skill_id = skill_name2id(name))) {
  8534. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8535. return SCRIPT_CMD_FAILURE;
  8536. }
  8537. } else {
  8538. skill_id = script_getnum(st, 5);
  8539. if (!skill_get_index(skill_id)) {
  8540. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8541. return SCRIPT_CMD_FAILURE;
  8542. }
  8543. }
  8544. bonus_script = script_getstr(st,2);
  8545. if( !rate || !dur || !pos || !bonus_script )
  8546. return SCRIPT_CMD_SUCCESS;
  8547. if( script_hasdata(st,6) )
  8548. other_script = script_getstr(st,6);
  8549. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8550. {
  8551. script_add_autobonus(bonus_script);
  8552. if( other_script )
  8553. script_add_autobonus(other_script);
  8554. }
  8555. return SCRIPT_CMD_SUCCESS;
  8556. }
  8557. /// Changes the level of a player skill.
  8558. /// <flag> defaults to 1
  8559. /// <flag>=0 : set the level of the skill
  8560. /// <flag>=1 : set the temporary level of the skill
  8561. /// <flag>=2 : add to the level of the skill
  8562. ///
  8563. /// skill <skill id>,<level>,<flag>
  8564. /// skill <skill id>,<level>
  8565. /// skill "<skill name>",<level>,<flag>
  8566. /// skill "<skill name>",<level>
  8567. BUILDIN_FUNC(skill)
  8568. {
  8569. int id;
  8570. int level;
  8571. int flag = ADDSKILL_TEMP;
  8572. TBL_PC* sd;
  8573. const char* command = script_getfuncname(st);
  8574. if( !script_rid2sd(sd) )
  8575. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8576. if (strcmpi(command, "addtoskill") == 0)
  8577. flag = ADDSKILL_TEMP_ADDLEVEL;
  8578. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8579. level = script_getnum(st,3);
  8580. if( script_hasdata(st,4) )
  8581. flag = script_getnum(st,4);
  8582. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8583. return SCRIPT_CMD_SUCCESS;
  8584. }
  8585. /// Increases the level of a guild skill.
  8586. ///
  8587. /// guildskill <skill id>,<amount>;
  8588. /// guildskill "<skill name>",<amount>;
  8589. BUILDIN_FUNC(guildskill)
  8590. {
  8591. int id;
  8592. int level;
  8593. TBL_PC* sd;
  8594. int i;
  8595. if( !script_rid2sd(sd) )
  8596. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8597. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8598. level = script_getnum(st,3);
  8599. for( i=0; i < level; i++ )
  8600. guild_skillup(sd, id);
  8601. return SCRIPT_CMD_SUCCESS;
  8602. }
  8603. /// Returns the level of the player skill.
  8604. ///
  8605. /// getskilllv(<skill id>) -> <level>
  8606. /// getskilllv("<skill name>") -> <level>
  8607. BUILDIN_FUNC(getskilllv)
  8608. {
  8609. int id;
  8610. TBL_PC* sd;
  8611. if( !script_rid2sd(sd) )
  8612. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8613. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8614. script_pushint(st, pc_checkskill(sd,id));
  8615. return SCRIPT_CMD_SUCCESS;
  8616. }
  8617. /// Returns the level of the guild skill.
  8618. ///
  8619. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8620. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8621. BUILDIN_FUNC(getgdskilllv)
  8622. {
  8623. int guild_id;
  8624. uint16 skill_id;
  8625. struct guild* g;
  8626. guild_id = script_getnum(st,2);
  8627. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8628. g = guild_search(guild_id);
  8629. if( g == NULL )
  8630. script_pushint(st, -1);
  8631. else
  8632. script_pushint(st, guild_checkskill(g,skill_id));
  8633. return SCRIPT_CMD_SUCCESS;
  8634. }
  8635. /// Returns the 'basic_skill_check' setting.
  8636. /// This config determines if the server checks the skill level of NV_BASIC
  8637. /// before allowing the basic actions.
  8638. ///
  8639. /// basicskillcheck() -> <bool>
  8640. BUILDIN_FUNC(basicskillcheck)
  8641. {
  8642. script_pushint(st, battle_config.basic_skill_check);
  8643. return SCRIPT_CMD_SUCCESS;
  8644. }
  8645. /// Returns the GM level of the player.
  8646. ///
  8647. /// getgmlevel({<char_id>}) -> <level>
  8648. BUILDIN_FUNC(getgmlevel)
  8649. {
  8650. TBL_PC* sd;
  8651. if (!script_charid2sd(2,sd))
  8652. return SCRIPT_CMD_FAILURE;
  8653. script_pushint(st, pc_get_group_level(sd));
  8654. return SCRIPT_CMD_SUCCESS;
  8655. }
  8656. /// Returns the group ID of the player.
  8657. ///
  8658. /// getgroupid({<char_id>}) -> <int>
  8659. BUILDIN_FUNC(getgroupid)
  8660. {
  8661. TBL_PC* sd;
  8662. if (!script_charid2sd(2,sd))
  8663. return SCRIPT_CMD_FAILURE;
  8664. script_pushint(st, pc_get_group_id(sd));
  8665. return SCRIPT_CMD_SUCCESS;
  8666. }
  8667. /// Terminates the execution of this script instance.
  8668. ///
  8669. /// end
  8670. BUILDIN_FUNC(end)
  8671. {
  8672. TBL_PC* sd;
  8673. sd = map_id2sd(st->rid);
  8674. st->state = END;
  8675. if( st->mes_active )
  8676. st->mes_active = 0;
  8677. if (sd){
  8678. if (sd->state.callshop == 0)
  8679. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8680. else
  8681. sd->state.callshop = 0;
  8682. }
  8683. return SCRIPT_CMD_SUCCESS;
  8684. }
  8685. /// Checks if the player has that effect state (option).
  8686. ///
  8687. /// checkoption(<option>{,<char_id>}) -> <bool>
  8688. BUILDIN_FUNC(checkoption)
  8689. {
  8690. int option;
  8691. TBL_PC* sd;
  8692. if (!script_charid2sd(3,sd))
  8693. return SCRIPT_CMD_FAILURE;
  8694. option = script_getnum(st,2);
  8695. if( sd->sc.option&option )
  8696. script_pushint(st, 1);
  8697. else
  8698. script_pushint(st, 0);
  8699. return SCRIPT_CMD_SUCCESS;
  8700. }
  8701. /// Checks if the player is in that body state (opt1).
  8702. ///
  8703. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8704. BUILDIN_FUNC(checkoption1)
  8705. {
  8706. int opt1;
  8707. TBL_PC* sd;
  8708. if (!script_charid2sd(3,sd))
  8709. return SCRIPT_CMD_FAILURE;
  8710. opt1 = script_getnum(st,2);
  8711. if( sd->sc.opt1 == opt1 )
  8712. script_pushint(st, 1);
  8713. else
  8714. script_pushint(st, 0);
  8715. return SCRIPT_CMD_SUCCESS;
  8716. }
  8717. /// Checks if the player has that health state (opt2).
  8718. ///
  8719. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8720. BUILDIN_FUNC(checkoption2)
  8721. {
  8722. int opt2;
  8723. TBL_PC* sd;
  8724. if (!script_charid2sd(3,sd))
  8725. return SCRIPT_CMD_FAILURE;
  8726. opt2 = script_getnum(st,2);
  8727. if( sd->sc.opt2&opt2 )
  8728. script_pushint(st, 1);
  8729. else
  8730. script_pushint(st, 0);
  8731. return SCRIPT_CMD_SUCCESS;
  8732. }
  8733. /// Changes the effect state (option) of the player.
  8734. /// <flag> defaults to 1
  8735. /// <flag>=0 : removes the option
  8736. /// <flag>=other : adds the option
  8737. ///
  8738. /// setoption <option>{,<flag>{,<char_id>}};
  8739. BUILDIN_FUNC(setoption)
  8740. {
  8741. int option;
  8742. int flag = 1;
  8743. TBL_PC* sd;
  8744. if (!script_charid2sd(4,sd))
  8745. return SCRIPT_CMD_FAILURE;
  8746. option = script_getnum(st,2);
  8747. if( script_hasdata(st,3) )
  8748. flag = script_getnum(st,3);
  8749. else if( !option ){// Request to remove everything.
  8750. flag = 0;
  8751. option = OPTION_FALCON|OPTION_RIDING;
  8752. #ifndef NEW_CARTS
  8753. option |= OPTION_CART;
  8754. #endif
  8755. }
  8756. if( flag ){// Add option
  8757. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8758. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8759. pc_setoption(sd, sd->sc.option|option);
  8760. } else// Remove option
  8761. pc_setoption(sd, sd->sc.option&~option);
  8762. return SCRIPT_CMD_SUCCESS;
  8763. }
  8764. /// Returns if the player has a cart.
  8765. ///
  8766. /// checkcart({char_id}) -> <bool>
  8767. ///
  8768. /// @author Valaris
  8769. BUILDIN_FUNC(checkcart)
  8770. {
  8771. TBL_PC* sd;
  8772. if (!script_charid2sd(2,sd))
  8773. return SCRIPT_CMD_FAILURE;
  8774. if( pc_iscarton(sd) )
  8775. script_pushint(st, 1);
  8776. else
  8777. script_pushint(st, 0);
  8778. return SCRIPT_CMD_SUCCESS;
  8779. }
  8780. /// Sets the cart of the player.
  8781. /// <type> defaults to 1
  8782. /// <type>=0 : removes the cart
  8783. /// <type>=1 : Normal cart
  8784. /// <type>=2 : Wooden cart
  8785. /// <type>=3 : Covered cart with flowers and ferns
  8786. /// <type>=4 : Wooden cart with a Panda doll on the back
  8787. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8788. ///
  8789. /// setcart {<type>{,<char_id>}};
  8790. BUILDIN_FUNC(setcart)
  8791. {
  8792. int type = 1;
  8793. TBL_PC* sd;
  8794. if (!script_charid2sd(3,sd))
  8795. return SCRIPT_CMD_FAILURE;
  8796. if( script_hasdata(st,2) )
  8797. type = script_getnum(st,2);
  8798. pc_setcart(sd, type);
  8799. return SCRIPT_CMD_SUCCESS;
  8800. }
  8801. /// Returns if the player has a falcon.
  8802. ///
  8803. /// checkfalcon({char_id}) -> <bool>
  8804. ///
  8805. /// @author Valaris
  8806. BUILDIN_FUNC(checkfalcon)
  8807. {
  8808. TBL_PC* sd;
  8809. if (!script_charid2sd(2,sd))
  8810. return SCRIPT_CMD_FAILURE;
  8811. if( pc_isfalcon(sd) )
  8812. script_pushint(st, 1);
  8813. else
  8814. script_pushint(st, 0);
  8815. return SCRIPT_CMD_SUCCESS;
  8816. }
  8817. /// Sets if the player has a falcon or not.
  8818. /// <flag> defaults to 1
  8819. ///
  8820. /// setfalcon {<flag>{,<char_id>}};
  8821. BUILDIN_FUNC(setfalcon)
  8822. {
  8823. int flag = 1;
  8824. TBL_PC* sd;
  8825. if (!script_charid2sd(3,sd))
  8826. return SCRIPT_CMD_FAILURE;
  8827. if( script_hasdata(st,2) )
  8828. flag = script_getnum(st,2);
  8829. pc_setfalcon(sd, flag);
  8830. return SCRIPT_CMD_SUCCESS;
  8831. }
  8832. /// Returns if the player is riding.
  8833. ///
  8834. /// checkriding({char_id}) -> <bool>
  8835. ///
  8836. /// @author Valaris
  8837. BUILDIN_FUNC(checkriding)
  8838. {
  8839. TBL_PC* sd;
  8840. if (!script_charid2sd(2,sd))
  8841. return SCRIPT_CMD_FAILURE;
  8842. if( pc_isriding(sd) )
  8843. script_pushint(st, 1);
  8844. else
  8845. script_pushint(st, 0);
  8846. return SCRIPT_CMD_SUCCESS;
  8847. }
  8848. /// Sets if the player is riding.
  8849. /// <flag> defaults to 1
  8850. ///
  8851. /// setriding {<flag>{,<char_id>}};
  8852. BUILDIN_FUNC(setriding)
  8853. {
  8854. int flag = 1;
  8855. TBL_PC* sd;
  8856. if (!script_charid2sd(3,sd))
  8857. return SCRIPT_CMD_FAILURE;
  8858. if( script_hasdata(st,2) )
  8859. flag = script_getnum(st,2);
  8860. pc_setriding(sd, flag);
  8861. return SCRIPT_CMD_SUCCESS;
  8862. }
  8863. /// Returns if the player has a warg.
  8864. ///
  8865. /// checkwug({char_id}) -> <bool>
  8866. ///
  8867. BUILDIN_FUNC(checkwug)
  8868. {
  8869. TBL_PC* sd;
  8870. if (!script_charid2sd(2,sd))
  8871. return SCRIPT_CMD_FAILURE;
  8872. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8873. script_pushint(st, 1);
  8874. else
  8875. script_pushint(st, 0);
  8876. return SCRIPT_CMD_SUCCESS;
  8877. }
  8878. /// Returns if the player is wearing MADO Gear.
  8879. ///
  8880. /// checkmadogear({<char_id>}) -> <bool>
  8881. ///
  8882. BUILDIN_FUNC(checkmadogear)
  8883. {
  8884. TBL_PC* sd;
  8885. if (!script_charid2sd(2,sd))
  8886. return SCRIPT_CMD_FAILURE;
  8887. if( pc_ismadogear(sd) )
  8888. script_pushint(st, 1);
  8889. else
  8890. script_pushint(st, 0);
  8891. return SCRIPT_CMD_SUCCESS;
  8892. }
  8893. /// Sets if the player is riding MADO Gear.
  8894. /// <flag> defaults to true
  8895. /// <type> defaults to MADO_ROBOT
  8896. ///
  8897. /// setmadogear {<flag>{,type{,<char_id>}}};
  8898. BUILDIN_FUNC(setmadogear)
  8899. {
  8900. bool flag = true;
  8901. TBL_PC* sd;
  8902. uint16 type = MADO_ROBOT;
  8903. if (!script_charid2sd(4,sd))
  8904. return SCRIPT_CMD_FAILURE;
  8905. if( script_hasdata(st,2) )
  8906. flag = script_getnum(st,2) != 0;
  8907. if (script_hasdata(st, 3)) {
  8908. type = script_getnum(st, 3);
  8909. if (type == MADO_UNUSED || type >= MADO_MAX) {
  8910. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  8911. type = MADO_ROBOT;
  8912. }
  8913. }
  8914. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  8915. return SCRIPT_CMD_SUCCESS;
  8916. }
  8917. /// Sets the save point of the player.
  8918. ///
  8919. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8920. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8921. BUILDIN_FUNC(savepoint)
  8922. {
  8923. int x, y, m, cid_pos = 5;
  8924. unsigned short map_idx;
  8925. const char* str;
  8926. TBL_PC* sd;
  8927. if (script_lastdata(st) > 5)
  8928. cid_pos = 7;
  8929. if (!script_charid2sd(cid_pos,sd))
  8930. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8931. str = script_getstr(st, 2);
  8932. map_idx = mapindex_name2id(str);
  8933. if( !map_idx )
  8934. return SCRIPT_CMD_FAILURE;
  8935. x = script_getnum(st,3);
  8936. y = script_getnum(st,4);
  8937. m = map_mapindex2mapid(map_idx);
  8938. if (cid_pos == 7) {
  8939. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8940. x1 = x + dx, y1 = y + dy,
  8941. x0 = x - dx, y0 = y - dy;
  8942. uint8 n = 10;
  8943. do {
  8944. x = x0 + rnd()%(x1-x0+1);
  8945. y = y0 + rnd()%(y1-y0+1);
  8946. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8947. }
  8948. // Check for valid coordinates if map in local map-server
  8949. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8950. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8951. return SCRIPT_CMD_FAILURE;
  8952. }
  8953. pc_setsavepoint(sd, map_idx, x, y);
  8954. return SCRIPT_CMD_SUCCESS;
  8955. }
  8956. /*==========================================
  8957. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8958. *------------------------------------------*/
  8959. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8960. {
  8961. int type;
  8962. time_t timer;
  8963. struct tm *t;
  8964. type=script_getnum(st,2);
  8965. switch(type){
  8966. case 2:
  8967. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8968. // from the system clock.)
  8969. script_pushint(st,(int)time(NULL));
  8970. break;
  8971. case 1:
  8972. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8973. time(&timer);
  8974. t=localtime(&timer);
  8975. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8976. break;
  8977. case 0:
  8978. default:
  8979. //type 0:(System Ticks)
  8980. script_pushint(st,gettick());
  8981. break;
  8982. }
  8983. return SCRIPT_CMD_SUCCESS;
  8984. }
  8985. /*==========================================
  8986. * GetTime(Type)
  8987. *
  8988. * Returns the current value of a certain date type.
  8989. * Possible types are:
  8990. * - DT_SECOND Current seconds
  8991. * - DT_MINUTE Current minute
  8992. * - DT_HOUR Current hour
  8993. * - DT_DAYOFWEEK Day of current week
  8994. * - DT_DAYOFMONTH Day of current month
  8995. * - DT_MONTH Current month
  8996. * - DT_YEAR Current year
  8997. * - DT_DAYOFYEAR Day of current year
  8998. *
  8999. * If none of the above types is supplied -1 will be returned to the script
  9000. * and the script source will be reported into the mapserver console.
  9001. *------------------------------------------*/
  9002. BUILDIN_FUNC(gettime)
  9003. {
  9004. int type;
  9005. type = script_getnum(st,2);
  9006. if( type <= DT_MIN || type >= DT_MAX ){
  9007. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  9008. script_reportsrc(st);
  9009. script_pushint(st,-1);
  9010. }else{
  9011. script_pushint(st,date_get((enum e_date_type)type));
  9012. }
  9013. return SCRIPT_CMD_SUCCESS;
  9014. }
  9015. /**
  9016. * Returns the current server time or the provided time in a readable format.
  9017. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  9018. */
  9019. BUILDIN_FUNC(gettimestr)
  9020. {
  9021. char *tmpstr;
  9022. const char *fmtstr;
  9023. int maxlen;
  9024. time_t now;
  9025. fmtstr = script_getstr(st,2);
  9026. maxlen = script_getnum(st,3);
  9027. if (script_hasdata(st, 4)) {
  9028. if (script_getnum(st, 4) < 0) {
  9029. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  9030. return SCRIPT_CMD_FAILURE;
  9031. } else
  9032. now = (time_t)script_getnum(st, 4);
  9033. } else
  9034. now = time(NULL);
  9035. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  9036. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  9037. tmpstr[maxlen] = '\0';
  9038. script_pushstr(st,tmpstr);
  9039. return SCRIPT_CMD_SUCCESS;
  9040. }
  9041. /*==========================================
  9042. * Open player storage
  9043. *------------------------------------------*/
  9044. BUILDIN_FUNC(openstorage)
  9045. {
  9046. TBL_PC* sd;
  9047. if( !script_rid2sd(sd) )
  9048. return SCRIPT_CMD_SUCCESS;
  9049. storage_storageopen(sd);
  9050. return SCRIPT_CMD_SUCCESS;
  9051. }
  9052. BUILDIN_FUNC(guildopenstorage)
  9053. {
  9054. TBL_PC* sd;
  9055. int ret;
  9056. if( !script_rid2sd(sd) )
  9057. return SCRIPT_CMD_SUCCESS;
  9058. ret = storage_guild_storageopen(sd);
  9059. script_pushint(st,ret);
  9060. return SCRIPT_CMD_SUCCESS;
  9061. }
  9062. BUILDIN_FUNC(guildopenstorage_log){
  9063. #if PACKETVER < 20140205
  9064. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  9065. return SCRIPT_CMD_FAILURE;
  9066. #else
  9067. struct map_session_data* sd;
  9068. if( !script_charid2sd( 2, sd ) ){
  9069. return SCRIPT_CMD_FAILURE;
  9070. }
  9071. script_pushint( st, storage_guild_log_read( sd ) );
  9072. return SCRIPT_CMD_SUCCESS;
  9073. #endif
  9074. }
  9075. BUILDIN_FUNC(guild_has_permission){
  9076. struct map_session_data* sd;
  9077. if( !script_charid2sd( 3, sd ) ){
  9078. return SCRIPT_CMD_FAILURE;
  9079. }
  9080. int permission = script_getnum(st,2);
  9081. if( permission == 0 ){
  9082. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  9083. return SCRIPT_CMD_FAILURE;
  9084. }
  9085. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  9086. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  9087. return SCRIPT_CMD_FAILURE;
  9088. }
  9089. if( !sd->guild ){
  9090. script_pushint( st, false );
  9091. return SCRIPT_CMD_SUCCESS;
  9092. }
  9093. int position = guild_getposition(sd);
  9094. if( position < 0 || ( sd->guild->position[position].mode&permission ) != permission ){
  9095. script_pushint( st, false );
  9096. return SCRIPT_CMD_SUCCESS;
  9097. }
  9098. script_pushint( st, true );
  9099. return SCRIPT_CMD_SUCCESS;
  9100. }
  9101. /*==========================================
  9102. * Make player use a skill trought item usage
  9103. *------------------------------------------*/
  9104. /// itemskill <skill id>,<level>
  9105. /// itemskill "<skill name>",<level>
  9106. BUILDIN_FUNC(itemskill)
  9107. {
  9108. int id;
  9109. int lv;
  9110. bool keep_requirement;
  9111. TBL_PC* sd;
  9112. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9113. return SCRIPT_CMD_SUCCESS;
  9114. if (script_isstring(st, 2)) {
  9115. const char *name = script_getstr(st, 2);
  9116. if (!(id = skill_name2id(name))) {
  9117. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9118. return SCRIPT_CMD_FAILURE;
  9119. }
  9120. } else {
  9121. id = script_getnum(st, 2);
  9122. if (!skill_get_index(id)) {
  9123. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9124. return SCRIPT_CMD_FAILURE;
  9125. }
  9126. }
  9127. lv = script_getnum(st,3);
  9128. if (script_hasdata(st, 4)) {
  9129. keep_requirement = (script_getnum(st, 4) != 0);
  9130. } else {
  9131. keep_requirement = false;
  9132. }
  9133. sd->skillitem=id;
  9134. sd->skillitemlv=lv;
  9135. sd->skillitem_keep_requirement = keep_requirement;
  9136. clif_item_skill(sd,id,lv);
  9137. return SCRIPT_CMD_SUCCESS;
  9138. }
  9139. /*==========================================
  9140. * Attempt to create an item
  9141. *------------------------------------------*/
  9142. BUILDIN_FUNC(produce)
  9143. {
  9144. int trigger;
  9145. TBL_PC* sd;
  9146. if( !script_rid2sd(sd) )
  9147. return SCRIPT_CMD_SUCCESS;
  9148. trigger=script_getnum(st,2);
  9149. clif_skill_produce_mix_list(sd, -1, trigger);
  9150. return SCRIPT_CMD_SUCCESS;
  9151. }
  9152. /*==========================================
  9153. *
  9154. *------------------------------------------*/
  9155. BUILDIN_FUNC(cooking)
  9156. {
  9157. int trigger;
  9158. TBL_PC* sd;
  9159. if( !script_rid2sd(sd) )
  9160. return SCRIPT_CMD_SUCCESS;
  9161. trigger=script_getnum(st,2);
  9162. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  9163. return SCRIPT_CMD_SUCCESS;
  9164. }
  9165. /*==========================================
  9166. * Create a pet
  9167. *------------------------------------------*/
  9168. BUILDIN_FUNC(makepet)
  9169. {
  9170. struct map_session_data* sd;
  9171. uint16 mob_id;
  9172. if( !script_rid2sd(sd) )
  9173. return SCRIPT_CMD_FAILURE;
  9174. mob_id = script_getnum(st,2);
  9175. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9176. if( !pet ){
  9177. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9178. return SCRIPT_CMD_FAILURE;
  9179. }
  9180. sd->catch_target_class = mob_id;
  9181. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9182. 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() );
  9183. return SCRIPT_CMD_SUCCESS;
  9184. }
  9185. /**
  9186. * Give player exp base,job * quest_exp_rate/100
  9187. * getexp <base xp>,<job xp>{,<char_id>};
  9188. **/
  9189. BUILDIN_FUNC(getexp){
  9190. struct map_session_data* sd;
  9191. if( !script_charid2sd( 4, sd ) ){
  9192. return SCRIPT_CMD_FAILURE;
  9193. }
  9194. int64 base = script_getnum64( st, 2 );
  9195. if( base < 0 ){
  9196. ShowError( "buildin_getexp: Called with negative base exp.\n" );
  9197. return SCRIPT_CMD_FAILURE;
  9198. }
  9199. int64 job = script_getnum64( st, 3 );
  9200. if( job < 0 ){
  9201. ShowError( "buildin_getexp: Called with negative job exp.\n" );
  9202. return SCRIPT_CMD_FAILURE;
  9203. }
  9204. if( base == 0 && job == 0 ){
  9205. ShowError( "buildin_getexp: Called with base and job exp 0.\n" );
  9206. return SCRIPT_CMD_FAILURE;
  9207. }
  9208. // bonus for npc-given exp
  9209. double bonus = battle_config.quest_exp_rate / 100.;
  9210. if (base)
  9211. base = (int64) cap_value(base * bonus, 0, MAX_EXP);
  9212. if (job)
  9213. job = (int64) cap_value(job * bonus, 0, MAX_EXP);
  9214. pc_gainexp(sd, NULL, base, job, 1);
  9215. #ifdef RENEWAL
  9216. if (base && sd->hd)
  9217. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9218. #endif
  9219. return SCRIPT_CMD_SUCCESS;
  9220. }
  9221. /*==========================================
  9222. * Gain guild exp [Celest]
  9223. *------------------------------------------*/
  9224. BUILDIN_FUNC(guildgetexp){
  9225. struct map_session_data* sd;
  9226. if( !script_rid2sd( sd ) ){
  9227. return SCRIPT_CMD_FAILURE;
  9228. }
  9229. int64 exp = script_getnum64( st, 2 );
  9230. if( exp <= 0 ){
  9231. ShowError( "buildin_guildgetexp: Called with exp <= 0.\n" );
  9232. return SCRIPT_CMD_FAILURE;
  9233. }
  9234. if( sd->status.guild_id <= 0 ){
  9235. 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 );
  9236. return SCRIPT_CMD_FAILURE;
  9237. }
  9238. guild_getexp( sd, exp );
  9239. return SCRIPT_CMD_SUCCESS;
  9240. }
  9241. /*==========================================
  9242. * Changes the guild master of a guild [Skotlex]
  9243. *------------------------------------------*/
  9244. BUILDIN_FUNC(guildchangegm)
  9245. {
  9246. TBL_PC *sd;
  9247. int guild_id;
  9248. const char *name;
  9249. guild_id = script_getnum(st,2);
  9250. name = script_getstr(st,3);
  9251. sd=map_nick2sd(name,false);
  9252. if (!sd)
  9253. script_pushint(st,0);
  9254. else
  9255. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9256. return SCRIPT_CMD_SUCCESS;
  9257. }
  9258. /*==========================================
  9259. * Spawn a monster:
  9260. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9261. *------------------------------------------*/
  9262. BUILDIN_FUNC(monster)
  9263. {
  9264. const char* mapn = script_getstr(st,2);
  9265. int x = script_getnum(st,3);
  9266. int y = script_getnum(st,4);
  9267. const char* str = script_getstr(st,5);
  9268. int class_ = script_getnum(st,6);
  9269. int amount = script_getnum(st,7);
  9270. const char* event = "";
  9271. unsigned int size = SZ_SMALL;
  9272. enum mob_ai ai = AI_NONE;
  9273. struct map_session_data* sd;
  9274. int16 m;
  9275. int i;
  9276. if (script_hasdata(st, 8)) {
  9277. event = script_getstr(st, 8);
  9278. check_event(st, event);
  9279. }
  9280. if (script_hasdata(st, 9)) {
  9281. size = script_getnum(st, 9);
  9282. if (size > SZ_BIG) {
  9283. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9284. return SCRIPT_CMD_FAILURE;
  9285. }
  9286. }
  9287. if (script_hasdata(st, 10)) {
  9288. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9289. if (ai >= AI_MAX) {
  9290. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9291. return SCRIPT_CMD_FAILURE;
  9292. }
  9293. }
  9294. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9295. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9296. return SCRIPT_CMD_FAILURE;
  9297. }
  9298. sd = map_id2sd(st->rid);
  9299. if (sd && strcmp(mapn, "this") == 0)
  9300. m = sd->bl.m;
  9301. else
  9302. m = map_mapname2mapid(mapn);
  9303. for(i = 0; i < amount; i++) { //not optimised
  9304. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9305. if (mobid)
  9306. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9307. }
  9308. return SCRIPT_CMD_SUCCESS;
  9309. }
  9310. /*==========================================
  9311. * Request List of Monster Drops
  9312. *------------------------------------------*/
  9313. BUILDIN_FUNC(getmobdrops)
  9314. {
  9315. int class_ = script_getnum(st,2);
  9316. int i, j = 0;
  9317. if( !mobdb_checkid(class_) )
  9318. {
  9319. script_pushint(st, 0);
  9320. return SCRIPT_CMD_SUCCESS;
  9321. }
  9322. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9323. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9324. {
  9325. if( mob->dropitem[i].nameid == 0 )
  9326. continue;
  9327. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  9328. continue;
  9329. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9330. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9331. mapreg_setreg(reference_uid(add_str("$@MobDrop_nosteal"), j), mob->dropitem[i].steal_protected);
  9332. mapreg_setreg(reference_uid(add_str("$@MobDrop_randomopt"), j), mob->dropitem[i].randomopt_group);
  9333. j++;
  9334. }
  9335. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9336. script_pushint(st, 1);
  9337. return SCRIPT_CMD_SUCCESS;
  9338. }
  9339. /*==========================================
  9340. * Spawn a monster in a random location
  9341. * in x0,x1,y0,y1 area.
  9342. *------------------------------------------*/
  9343. BUILDIN_FUNC(areamonster)
  9344. {
  9345. const char* mapn = script_getstr(st,2);
  9346. int x0 = script_getnum(st,3);
  9347. int y0 = script_getnum(st,4);
  9348. int x1 = script_getnum(st,5);
  9349. int y1 = script_getnum(st,6);
  9350. const char* str = script_getstr(st,7);
  9351. int class_ = script_getnum(st,8);
  9352. int amount = script_getnum(st,9);
  9353. const char* event = "";
  9354. unsigned int size = SZ_SMALL;
  9355. enum mob_ai ai = AI_NONE;
  9356. struct map_session_data* sd;
  9357. int16 m;
  9358. int i;
  9359. if (script_hasdata(st,10)) {
  9360. event = script_getstr(st, 10);
  9361. check_event(st, event);
  9362. }
  9363. if (script_hasdata(st, 11)) {
  9364. size = script_getnum(st, 11);
  9365. if (size > 3) {
  9366. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9367. return SCRIPT_CMD_FAILURE;
  9368. }
  9369. }
  9370. if (script_hasdata(st, 12)) {
  9371. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9372. if (ai >= AI_MAX) {
  9373. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9374. return SCRIPT_CMD_FAILURE;
  9375. }
  9376. }
  9377. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9378. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9379. return SCRIPT_CMD_FAILURE;
  9380. }
  9381. sd = map_id2sd(st->rid);
  9382. if (sd && strcmp(mapn, "this") == 0)
  9383. m = sd->bl.m;
  9384. else
  9385. m = map_mapname2mapid(mapn);
  9386. for(i = 0; i < amount; i++) { //not optimised
  9387. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9388. if (mobid)
  9389. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9390. }
  9391. return SCRIPT_CMD_SUCCESS;
  9392. }
  9393. /*==========================================
  9394. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9395. *------------------------------------------*/
  9396. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9397. { //same fix but with killmonster instead - stripping events from mobs.
  9398. TBL_MOB* md = (TBL_MOB*)bl;
  9399. char *event=va_arg(ap,char *);
  9400. int allflag=va_arg(ap,int);
  9401. md->state.npc_killmonster = 1;
  9402. if(!allflag){
  9403. if(strcmp(event,md->npc_event)==0)
  9404. status_kill(bl);
  9405. }else{
  9406. if(!md->spawn)
  9407. status_kill(bl);
  9408. }
  9409. md->state.npc_killmonster = 0;
  9410. return SCRIPT_CMD_SUCCESS;
  9411. }
  9412. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9413. {
  9414. TBL_MOB* md = (TBL_MOB*)bl;
  9415. char *event=va_arg(ap,char *);
  9416. int allflag=va_arg(ap,int);
  9417. if(!allflag){
  9418. if(strcmp(event,md->npc_event)==0)
  9419. status_kill(bl);
  9420. }else{
  9421. if(!md->spawn)
  9422. status_kill(bl);
  9423. }
  9424. return SCRIPT_CMD_SUCCESS;
  9425. }
  9426. BUILDIN_FUNC(killmonster)
  9427. {
  9428. const char *mapname,*event;
  9429. int16 m,allflag=0;
  9430. mapname=script_getstr(st,2);
  9431. event=script_getstr(st,3);
  9432. if(strcmp(event,"All")==0)
  9433. allflag = 1;
  9434. else
  9435. check_event(st, event);
  9436. if( (m=map_mapname2mapid(mapname))<0 )
  9437. return SCRIPT_CMD_SUCCESS;
  9438. if( script_hasdata(st,4) ) {
  9439. if ( script_getnum(st,4) == 1 ) {
  9440. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9441. return SCRIPT_CMD_SUCCESS;
  9442. }
  9443. }
  9444. map_freeblock_lock();
  9445. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9446. map_freeblock_unlock();
  9447. return SCRIPT_CMD_SUCCESS;
  9448. }
  9449. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9450. { //Strips the event from the mob if it's killed the old method.
  9451. struct mob_data *md;
  9452. md = BL_CAST(BL_MOB, bl);
  9453. if (md->npc_event[0])
  9454. md->npc_event[0] = 0;
  9455. status_kill(bl);
  9456. return 0;
  9457. }
  9458. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9459. {
  9460. status_kill(bl);
  9461. return 0;
  9462. }
  9463. BUILDIN_FUNC(killmonsterall)
  9464. {
  9465. const char *mapname;
  9466. int16 m;
  9467. mapname=script_getstr(st,2);
  9468. if( (m = map_mapname2mapid(mapname))<0 )
  9469. return SCRIPT_CMD_SUCCESS;
  9470. if( script_hasdata(st,3) ) {
  9471. if ( script_getnum(st,3) == 1 ) {
  9472. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9473. return SCRIPT_CMD_SUCCESS;
  9474. }
  9475. }
  9476. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9477. return SCRIPT_CMD_SUCCESS;
  9478. }
  9479. /*==========================================
  9480. * Creates a clone of a player.
  9481. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9482. *------------------------------------------*/
  9483. BUILDIN_FUNC(clone)
  9484. {
  9485. TBL_PC *sd, *msd=NULL;
  9486. uint32 char_id, master_id = 0, x, y, flag = 0;
  9487. int16 m;
  9488. enum e_mode mode = MD_NONE;
  9489. unsigned int duration = 0;
  9490. const char *mapname,*event;
  9491. mapname=script_getstr(st,2);
  9492. x=script_getnum(st,3);
  9493. y=script_getnum(st,4);
  9494. event=script_getstr(st,5);
  9495. char_id=script_getnum(st,6);
  9496. if( script_hasdata(st,7) )
  9497. master_id=script_getnum(st,7);
  9498. if( script_hasdata(st,8) )
  9499. mode=static_cast<e_mode>(script_getnum(st,8));
  9500. if( script_hasdata(st,9) )
  9501. flag=script_getnum(st,9);
  9502. if( script_hasdata(st,10) )
  9503. duration=script_getnum(st,10);
  9504. check_event(st, event);
  9505. m = map_mapname2mapid(mapname);
  9506. if (m < 0)
  9507. return SCRIPT_CMD_SUCCESS;
  9508. sd = map_charid2sd(char_id);
  9509. if (master_id) {
  9510. msd = map_charid2sd(master_id);
  9511. if (msd)
  9512. master_id = msd->bl.id;
  9513. else
  9514. master_id = 0;
  9515. }
  9516. if (sd) //Return ID of newly crafted clone.
  9517. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9518. else //Failed to create clone.
  9519. script_pushint(st,0);
  9520. return SCRIPT_CMD_SUCCESS;
  9521. }
  9522. /*==========================================
  9523. *------------------------------------------*/
  9524. BUILDIN_FUNC(doevent)
  9525. {
  9526. const char* event;
  9527. struct map_session_data* sd;
  9528. if( !script_rid2sd(sd) )
  9529. return SCRIPT_CMD_SUCCESS;
  9530. event = script_getstr(st,2);
  9531. check_event(st, event);
  9532. npc_event(sd, event, 0);
  9533. return SCRIPT_CMD_SUCCESS;
  9534. }
  9535. /*==========================================
  9536. *------------------------------------------*/
  9537. BUILDIN_FUNC(donpcevent)
  9538. {
  9539. const char* event = script_getstr(st,2);
  9540. check_event(st, event);
  9541. if( !npc_event_do(event) ) {
  9542. struct npc_data * nd = map_id2nd(st->oid);
  9543. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9544. script_pushint(st, 0);
  9545. } else
  9546. script_pushint(st, 1);
  9547. return SCRIPT_CMD_SUCCESS;
  9548. }
  9549. /// for Aegis compatibility
  9550. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9551. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9552. {
  9553. const char* npc = script_getstr(st,2);
  9554. const char* command = script_getstr(st,3);
  9555. char event[EVENT_NAME_LENGTH];
  9556. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9557. check_event(st, event);
  9558. if( npc_event_do(event) ){
  9559. script_pushint(st, true);
  9560. }else{
  9561. struct npc_data * nd = map_id2nd(st->oid);
  9562. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9563. script_pushint(st, false);
  9564. }
  9565. return SCRIPT_CMD_SUCCESS;
  9566. }
  9567. /*==========================================
  9568. *------------------------------------------*/
  9569. BUILDIN_FUNC(addtimer)
  9570. {
  9571. int tick;
  9572. const char* event;
  9573. TBL_PC* sd;
  9574. if( !script_rid2sd(sd) )
  9575. return SCRIPT_CMD_SUCCESS;
  9576. tick = script_getnum(st,2);
  9577. event = script_getstr(st, 3);
  9578. check_event(st, event);
  9579. if (!pc_addeventtimer(sd,tick,event)) {
  9580. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9581. return SCRIPT_CMD_FAILURE;
  9582. }
  9583. return SCRIPT_CMD_SUCCESS;
  9584. }
  9585. /*==========================================
  9586. *------------------------------------------*/
  9587. BUILDIN_FUNC(deltimer)
  9588. {
  9589. const char *event;
  9590. TBL_PC* sd;
  9591. if( !script_rid2sd(sd) )
  9592. return SCRIPT_CMD_SUCCESS;
  9593. event=script_getstr(st, 2);
  9594. check_event(st, event);
  9595. pc_deleventtimer(sd,event);
  9596. return SCRIPT_CMD_SUCCESS;
  9597. }
  9598. /*==========================================
  9599. *------------------------------------------*/
  9600. BUILDIN_FUNC(addtimercount)
  9601. {
  9602. const char *event;
  9603. int tick;
  9604. TBL_PC* sd;
  9605. if( !script_rid2sd(sd) )
  9606. return SCRIPT_CMD_SUCCESS;
  9607. tick=script_getnum(st,2);
  9608. event=script_getstr(st,3);
  9609. check_event(st, event);
  9610. pc_addeventtimercount(sd,event,tick);
  9611. return SCRIPT_CMD_SUCCESS;
  9612. }
  9613. /*==========================================
  9614. *------------------------------------------*/
  9615. BUILDIN_FUNC(initnpctimer)
  9616. {
  9617. struct npc_data *nd;
  9618. int flag = 0;
  9619. if( script_hasdata(st,3) )
  9620. { //Two arguments: NPC name and attach flag.
  9621. nd = npc_name2id(script_getstr(st, 2));
  9622. flag = script_getnum(st,3);
  9623. }
  9624. else if( script_hasdata(st,2) )
  9625. { //Check if argument is numeric (flag) or string (npc name)
  9626. if( script_isstring(st, 2) ) //NPC name
  9627. nd = npc_name2id(script_getstr(st, 2));
  9628. else //Flag
  9629. {
  9630. nd = (struct npc_data *)map_id2bl(st->oid);
  9631. flag = script_getnum(st, 2);
  9632. }
  9633. }
  9634. else
  9635. nd = (struct npc_data *)map_id2bl(st->oid);
  9636. if( !nd )
  9637. return SCRIPT_CMD_SUCCESS;
  9638. if( flag ) //Attach
  9639. {
  9640. TBL_PC* sd;
  9641. if( !script_rid2sd(sd) )
  9642. return SCRIPT_CMD_SUCCESS;
  9643. nd->u.scr.rid = sd->bl.id;
  9644. }
  9645. nd->u.scr.timertick = 0;
  9646. npc_settimerevent_tick(nd,0);
  9647. npc_timerevent_start(nd, st->rid);
  9648. return SCRIPT_CMD_SUCCESS;
  9649. }
  9650. /*==========================================
  9651. *------------------------------------------*/
  9652. BUILDIN_FUNC(startnpctimer)
  9653. {
  9654. struct npc_data *nd;
  9655. int flag = 0;
  9656. if( script_hasdata(st,3) )
  9657. { //Two arguments: NPC name and attach flag.
  9658. nd = npc_name2id(script_getstr(st, 2));
  9659. flag = script_getnum(st,3);
  9660. }
  9661. else if( script_hasdata(st,2) )
  9662. { //Check if argument is numeric (flag) or string (npc name)
  9663. if( script_isstring(st, 2) ) //NPC name
  9664. nd = npc_name2id(script_getstr(st, 2));
  9665. else //Flag
  9666. {
  9667. nd = (struct npc_data *)map_id2bl(st->oid);
  9668. flag = script_getnum(st, 2);
  9669. }
  9670. }
  9671. else
  9672. nd=(struct npc_data *)map_id2bl(st->oid);
  9673. if( !nd )
  9674. return SCRIPT_CMD_SUCCESS;
  9675. if( flag ) //Attach
  9676. {
  9677. TBL_PC* sd;
  9678. if( !script_rid2sd(sd) )
  9679. return SCRIPT_CMD_SUCCESS;
  9680. nd->u.scr.rid = sd->bl.id;
  9681. }
  9682. npc_timerevent_start(nd, st->rid);
  9683. return SCRIPT_CMD_SUCCESS;
  9684. }
  9685. /*==========================================
  9686. *------------------------------------------*/
  9687. BUILDIN_FUNC(stopnpctimer)
  9688. {
  9689. struct npc_data *nd;
  9690. int flag = 0;
  9691. if( script_hasdata(st,3) )
  9692. { //Two arguments: NPC name and attach flag.
  9693. nd = npc_name2id(script_getstr(st, 2));
  9694. flag = script_getnum(st,3);
  9695. }
  9696. else if( script_hasdata(st,2) )
  9697. { //Check if argument is numeric (flag) or string (npc name)
  9698. if( script_isstring(st, 2) ) //NPC name
  9699. nd = npc_name2id(script_getstr(st, 2));
  9700. else //Flag
  9701. {
  9702. nd = (struct npc_data *)map_id2bl(st->oid);
  9703. flag = script_getnum(st, 2);
  9704. }
  9705. }
  9706. else
  9707. nd=(struct npc_data *)map_id2bl(st->oid);
  9708. if( !nd )
  9709. return SCRIPT_CMD_SUCCESS;
  9710. if( flag ) //Detach
  9711. nd->u.scr.rid = 0;
  9712. npc_timerevent_stop(nd);
  9713. return SCRIPT_CMD_SUCCESS;
  9714. }
  9715. /*==========================================
  9716. *------------------------------------------*/
  9717. BUILDIN_FUNC(getnpctimer)
  9718. {
  9719. struct npc_data *nd;
  9720. TBL_PC *sd;
  9721. int type = script_getnum(st,2);
  9722. t_tick val = 0;
  9723. if( script_hasdata(st,3) )
  9724. nd = npc_name2id(script_getstr(st,3));
  9725. else
  9726. nd = (struct npc_data *)map_id2bl(st->oid);
  9727. if( !nd || nd->bl.type != BL_NPC )
  9728. {
  9729. script_pushint(st,0);
  9730. ShowError("getnpctimer: Invalid NPC.\n");
  9731. return SCRIPT_CMD_FAILURE;
  9732. }
  9733. switch( type )
  9734. {
  9735. case 0: val = npc_gettimerevent_tick(nd); break;
  9736. case 1:
  9737. if( nd->u.scr.rid )
  9738. {
  9739. sd = map_id2sd(nd->u.scr.rid);
  9740. if( !sd )
  9741. {
  9742. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9743. break;
  9744. }
  9745. val = (sd->npc_timer_id != INVALID_TIMER);
  9746. }
  9747. else
  9748. val = (nd->u.scr.timerid != INVALID_TIMER);
  9749. break;
  9750. case 2: val = nd->u.scr.timeramount; break;
  9751. }
  9752. script_pushint(st,val);
  9753. return SCRIPT_CMD_SUCCESS;
  9754. }
  9755. /*==========================================
  9756. *------------------------------------------*/
  9757. BUILDIN_FUNC(setnpctimer)
  9758. {
  9759. int tick;
  9760. struct npc_data *nd;
  9761. tick = script_getnum(st,2);
  9762. if( script_hasdata(st,3) )
  9763. nd = npc_name2id(script_getstr(st,3));
  9764. else
  9765. nd = (struct npc_data *)map_id2bl(st->oid);
  9766. if( !nd || nd->bl.type != BL_NPC )
  9767. {
  9768. script_pushint(st,1);
  9769. ShowError("setnpctimer: Invalid NPC.\n");
  9770. return SCRIPT_CMD_FAILURE;
  9771. }
  9772. npc_settimerevent_tick(nd,tick);
  9773. script_pushint(st,0);
  9774. return SCRIPT_CMD_SUCCESS;
  9775. }
  9776. /*==========================================
  9777. * attaches the player rid to the timer [Celest]
  9778. *------------------------------------------*/
  9779. BUILDIN_FUNC(attachnpctimer)
  9780. {
  9781. TBL_PC *sd;
  9782. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9783. if( !nd || nd->bl.type != BL_NPC )
  9784. {
  9785. script_pushint(st,1);
  9786. ShowError("setnpctimer: Invalid NPC.\n");
  9787. return SCRIPT_CMD_FAILURE;
  9788. }
  9789. if( !script_nick2sd(2,sd) ){
  9790. script_pushint(st,1);
  9791. ShowWarning("attachnpctimer: Invalid player.\n");
  9792. return SCRIPT_CMD_FAILURE;
  9793. }
  9794. nd->u.scr.rid = sd->bl.id;
  9795. script_pushint(st,0);
  9796. return SCRIPT_CMD_SUCCESS;
  9797. }
  9798. /*==========================================
  9799. * detaches a player rid from the timer [Celest]
  9800. *------------------------------------------*/
  9801. BUILDIN_FUNC(detachnpctimer)
  9802. {
  9803. struct npc_data *nd;
  9804. if( script_hasdata(st,2) )
  9805. nd = npc_name2id(script_getstr(st,2));
  9806. else
  9807. nd = (struct npc_data *)map_id2bl(st->oid);
  9808. if( !nd || nd->bl.type != BL_NPC )
  9809. {
  9810. script_pushint(st,1);
  9811. ShowError("detachnpctimer: Invalid NPC.\n");
  9812. return SCRIPT_CMD_FAILURE;
  9813. }
  9814. nd->u.scr.rid = 0;
  9815. script_pushint(st,0);
  9816. return SCRIPT_CMD_SUCCESS;
  9817. }
  9818. /*==========================================
  9819. * To avoid "player not attached" script errors, this function is provided,
  9820. * it checks if there is a player attached to the current script. [Skotlex]
  9821. * If no, returns 0, if yes, returns the account_id of the attached player.
  9822. *------------------------------------------*/
  9823. BUILDIN_FUNC(playerattached)
  9824. {
  9825. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9826. script_pushint(st,0);
  9827. else
  9828. script_pushint(st,st->rid);
  9829. return SCRIPT_CMD_SUCCESS;
  9830. }
  9831. /*==========================================
  9832. *------------------------------------------*/
  9833. BUILDIN_FUNC(announce)
  9834. {
  9835. const char *mes = script_getstr(st,2);
  9836. int flag = script_getnum(st,3);
  9837. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9838. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9839. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9840. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9841. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9842. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9843. {
  9844. send_target target;
  9845. struct block_list *bl;
  9846. // If bc_npc flag is set, use NPC as broadcast source
  9847. if(flag&BC_NPC){
  9848. bl = map_id2bl(st->oid);
  9849. }else{
  9850. struct map_session_data* sd;
  9851. if(script_charid2sd(9, sd))
  9852. bl = &sd->bl;
  9853. else
  9854. bl = NULL;
  9855. }
  9856. if (bl == NULL)
  9857. return SCRIPT_CMD_SUCCESS;
  9858. switch (flag&BC_TARGET_MASK) {
  9859. case BC_MAP: target = ALL_SAMEMAP; break;
  9860. case BC_AREA: target = AREA; break;
  9861. case BC_SELF: target = SELF; break;
  9862. default: target = ALL_CLIENT; break; // BC_ALL
  9863. }
  9864. if (fontColor)
  9865. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9866. else
  9867. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9868. }
  9869. else
  9870. {
  9871. if (fontColor)
  9872. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9873. else
  9874. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9875. }
  9876. return SCRIPT_CMD_SUCCESS;
  9877. }
  9878. /*==========================================
  9879. *------------------------------------------*/
  9880. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9881. {
  9882. char *mes = va_arg(ap, char *);
  9883. int len = va_arg(ap, int);
  9884. int type = va_arg(ap, int);
  9885. char *fontColor = va_arg(ap, char *);
  9886. short fontType = (short)va_arg(ap, int);
  9887. short fontSize = (short)va_arg(ap, int);
  9888. short fontAlign = (short)va_arg(ap, int);
  9889. short fontY = (short)va_arg(ap, int);
  9890. if (fontColor)
  9891. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9892. else
  9893. clif_broadcast(bl, mes, len, type, SELF);
  9894. return SCRIPT_CMD_SUCCESS;
  9895. }
  9896. BUILDIN_FUNC(mapannounce)
  9897. {
  9898. const char *mapname = script_getstr(st,2);
  9899. const char *mes = script_getstr(st,3);
  9900. int flag = script_getnum(st,4);
  9901. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9902. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9903. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9904. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9905. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9906. int16 m;
  9907. if ((m = map_mapname2mapid(mapname)) < 0)
  9908. return SCRIPT_CMD_SUCCESS;
  9909. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9910. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9911. return SCRIPT_CMD_SUCCESS;
  9912. }
  9913. /*==========================================
  9914. *------------------------------------------*/
  9915. BUILDIN_FUNC(areaannounce)
  9916. {
  9917. const char *mapname = script_getstr(st,2);
  9918. int x0 = script_getnum(st,3);
  9919. int y0 = script_getnum(st,4);
  9920. int x1 = script_getnum(st,5);
  9921. int y1 = script_getnum(st,6);
  9922. const char *mes = script_getstr(st,7);
  9923. int flag = script_getnum(st,8);
  9924. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9925. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9926. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9927. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9928. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9929. int16 m;
  9930. if ((m = map_mapname2mapid(mapname)) < 0)
  9931. return SCRIPT_CMD_SUCCESS;
  9932. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9933. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9934. return SCRIPT_CMD_SUCCESS;
  9935. }
  9936. /*==========================================
  9937. *------------------------------------------*/
  9938. BUILDIN_FUNC(getusers)
  9939. {
  9940. int flag, val = 0;
  9941. struct map_session_data* sd;
  9942. struct block_list* bl = NULL;
  9943. flag = script_getnum(st,2);
  9944. switch(flag&0x07)
  9945. {
  9946. case 0:
  9947. if(flag&0x8)
  9948. {// npc
  9949. bl = map_id2bl(st->oid);
  9950. }
  9951. else if(script_rid2sd(sd))
  9952. {// pc
  9953. bl = &sd->bl;
  9954. }
  9955. if(bl)
  9956. {
  9957. val = map_getmapdata(bl->m)->users;
  9958. }
  9959. break;
  9960. case 1:
  9961. val = map_getusers();
  9962. break;
  9963. default:
  9964. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9965. script_pushint(st,0);
  9966. return SCRIPT_CMD_FAILURE;
  9967. }
  9968. script_pushint(st,val);
  9969. return SCRIPT_CMD_SUCCESS;
  9970. }
  9971. /*==========================================
  9972. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9973. *------------------------------------------*/
  9974. BUILDIN_FUNC(getmapguildusers)
  9975. {
  9976. const char *str;
  9977. int16 m;
  9978. int gid;
  9979. int c=0;
  9980. struct guild *g = NULL;
  9981. str=script_getstr(st,2);
  9982. gid=script_getnum(st,3);
  9983. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9984. script_pushint(st,-1);
  9985. return SCRIPT_CMD_SUCCESS;
  9986. }
  9987. g = guild_search(gid);
  9988. if (g){
  9989. unsigned short i;
  9990. for(i = 0; i < g->max_member; i++)
  9991. {
  9992. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9993. c++;
  9994. }
  9995. }
  9996. script_pushint(st,c);
  9997. return SCRIPT_CMD_SUCCESS;
  9998. }
  9999. /*==========================================
  10000. *------------------------------------------*/
  10001. BUILDIN_FUNC(getmapusers)
  10002. {
  10003. const char *str;
  10004. int16 m;
  10005. str=script_getstr(st,2);
  10006. if( (m=map_mapname2mapid(str))< 0){
  10007. script_pushint(st,-1);
  10008. return SCRIPT_CMD_SUCCESS;
  10009. }
  10010. script_pushint(st,map_getmapdata(m)->users);
  10011. return SCRIPT_CMD_SUCCESS;
  10012. }
  10013. /*==========================================
  10014. *------------------------------------------*/
  10015. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  10016. {
  10017. int *users=va_arg(ap,int *);
  10018. (*users)++;
  10019. return SCRIPT_CMD_SUCCESS;
  10020. }
  10021. BUILDIN_FUNC(getareausers)
  10022. {
  10023. const char *str;
  10024. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  10025. str=script_getstr(st,2);
  10026. x0=script_getnum(st,3);
  10027. y0=script_getnum(st,4);
  10028. x1=script_getnum(st,5);
  10029. y1=script_getnum(st,6);
  10030. if( (m=map_mapname2mapid(str))< 0){
  10031. script_pushint(st,-1);
  10032. return SCRIPT_CMD_SUCCESS;
  10033. }
  10034. map_foreachinallarea(buildin_getareausers_sub,
  10035. m,x0,y0,x1,y1,BL_PC,&users);
  10036. script_pushint(st,users);
  10037. return SCRIPT_CMD_SUCCESS;
  10038. }
  10039. /*==========================================
  10040. * getunits(<type>{,<array_variable>[<first value>]})
  10041. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  10042. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  10043. *------------------------------------------*/
  10044. BUILDIN_FUNC(getunits)
  10045. {
  10046. struct block_list *bl = NULL;
  10047. struct map_session_data *sd = NULL;
  10048. struct script_data *data = NULL;
  10049. char *command = (char *)script_getfuncname(st);
  10050. const char *str;
  10051. const char *name;
  10052. int type = script_getnum(st, 2);
  10053. int size = 0;
  10054. int32 idx, id;
  10055. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  10056. if (!strcmp(command, "getmapunits"))
  10057. {
  10058. str = script_getstr(st, 3);
  10059. if ((m = map_mapname2mapid(str)) < 0) {
  10060. script_pushint(st, -1);
  10061. st->state = END;
  10062. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10063. return SCRIPT_CMD_FAILURE;
  10064. }
  10065. if (script_hasdata(st, 4))
  10066. data = script_getdata(st, 4);
  10067. }
  10068. else if (!strcmp(command, "getareaunits"))
  10069. {
  10070. str = script_getstr(st, 3);
  10071. if ((m = map_mapname2mapid(str)) < 0) {
  10072. script_pushint(st, -1);
  10073. st->state = END;
  10074. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10075. return SCRIPT_CMD_FAILURE;
  10076. }
  10077. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  10078. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  10079. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  10080. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  10081. if (script_hasdata(st, 8))
  10082. data = script_getdata(st, 8);
  10083. }
  10084. else
  10085. {
  10086. if (script_hasdata(st, 3))
  10087. data = script_getdata(st, 3);
  10088. }
  10089. if (data)
  10090. {
  10091. if (!data_isreference(data))
  10092. {
  10093. ShowError("buildin_%s: not a variable\n", command);
  10094. script_reportdata(data);
  10095. st->state = END;
  10096. return SCRIPT_CMD_FAILURE;
  10097. }
  10098. id = reference_getid(data);
  10099. idx = reference_getindex(data);
  10100. name = reference_getname(data);
  10101. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  10102. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  10103. return SCRIPT_CMD_FAILURE;
  10104. }
  10105. }
  10106. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  10107. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  10108. {
  10109. 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)))
  10110. {
  10111. if( data ){
  10112. if( is_string_variable( name ) ){
  10113. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  10114. }else{
  10115. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  10116. }
  10117. }
  10118. size++;
  10119. }
  10120. }
  10121. mapit_free(iter);
  10122. script_pushint(st, size);
  10123. return SCRIPT_CMD_SUCCESS;
  10124. }
  10125. /*==========================================
  10126. *------------------------------------------*/
  10127. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10128. {
  10129. t_itemid nameid = va_arg(ap, t_itemid);
  10130. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  10131. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10132. if(drop->item.nameid==nameid)
  10133. (*amount)+=drop->item.amount;
  10134. return SCRIPT_CMD_SUCCESS;
  10135. }
  10136. BUILDIN_FUNC(getareadropitem)
  10137. {
  10138. const char *str;
  10139. int16 m,x0,y0,x1,y1;
  10140. t_itemid nameid;
  10141. unsigned short amount = 0;
  10142. str=script_getstr(st,2);
  10143. x0=script_getnum(st,3);
  10144. y0=script_getnum(st,4);
  10145. x1=script_getnum(st,5);
  10146. y1=script_getnum(st,6);
  10147. if( script_isstring(st, 7) ){
  10148. const char *name = script_getstr(st, 7);
  10149. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  10150. if( item_data )
  10151. nameid=item_data->nameid;
  10152. else{
  10153. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10154. return SCRIPT_CMD_FAILURE;
  10155. }
  10156. }else{
  10157. nameid = script_getnum(st, 7);
  10158. if( !itemdb_exists( nameid ) ){
  10159. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10160. return SCRIPT_CMD_FAILURE;
  10161. }
  10162. }
  10163. if( (m=map_mapname2mapid(str))< 0){
  10164. script_pushint(st,-1);
  10165. return SCRIPT_CMD_SUCCESS;
  10166. }
  10167. map_foreachinallarea(buildin_getareadropitem_sub,
  10168. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10169. script_pushint(st,amount);
  10170. return SCRIPT_CMD_SUCCESS;
  10171. }
  10172. /*==========================================
  10173. *------------------------------------------*/
  10174. BUILDIN_FUNC(enablenpc)
  10175. {
  10176. npc_data* nd;
  10177. e_npcv_status flag = NPCVIEW_DISABLE;
  10178. int char_id = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10179. const char* command = script_getfuncname(st);
  10180. if (script_hasdata(st, 2))
  10181. nd = npc_name2id(script_getstr(st,2));
  10182. else
  10183. nd = map_id2nd(st->oid);
  10184. if (!strcmp(command,"enablenpc"))
  10185. flag = NPCVIEW_ENABLE;
  10186. else if (!strcmp(command,"disablenpc"))
  10187. flag = NPCVIEW_DISABLE;
  10188. else if (!strcmp(command,"hideoffnpc"))
  10189. flag = NPCVIEW_HIDEOFF;
  10190. else if (!strcmp(command,"hideonnpc"))
  10191. flag = NPCVIEW_HIDEON;
  10192. else if (!strcmp(command,"cloakoffnpc"))
  10193. flag = NPCVIEW_CLOAKOFF;
  10194. else if (!strcmp(command,"cloakonnpc"))
  10195. flag = NPCVIEW_CLOAKON;
  10196. else{
  10197. ShowError( "buildin_enablenpc: Undefined command \"%s\".\n", command );
  10198. return SCRIPT_CMD_FAILURE;
  10199. }
  10200. if (!nd) {
  10201. if (script_hasdata(st, 2))
  10202. 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);
  10203. else
  10204. ShowError("buildin_%s: Attempted to %s a non-existing NPC (flag=%d).\n", (flag & NPCVIEW_VISIBLE) ? "show" : "hide", command, flag);
  10205. return SCRIPT_CMD_FAILURE;
  10206. }
  10207. if (npc_enable_target(*nd, char_id, flag))
  10208. return SCRIPT_CMD_SUCCESS;
  10209. return SCRIPT_CMD_FAILURE;
  10210. }
  10211. /* Starts a status effect on the target unit or on the attached player.
  10212. *
  10213. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10214. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10215. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10216. * <flag>: enum e_status_change_start_flags
  10217. */
  10218. BUILDIN_FUNC(sc_start)
  10219. {
  10220. TBL_NPC * nd = map_id2nd(st->oid);
  10221. struct block_list* bl;
  10222. enum sc_type type;
  10223. int tick, val1, val2, val3, val4=0, rate, flag;
  10224. char start_type;
  10225. const char* command = script_getfuncname(st);
  10226. if(strstr(command, "4"))
  10227. start_type = 4;
  10228. else if(strstr(command, "2"))
  10229. start_type = 2;
  10230. else
  10231. start_type = 1;
  10232. type = (sc_type)script_getnum(st,2);
  10233. tick = script_getnum(st,3);
  10234. val1 = script_getnum(st,4);
  10235. //If from NPC we make default flag 1 to be unavoidable
  10236. if(nd && nd->bl.id == fake_nd->bl.id)
  10237. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10238. else
  10239. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10240. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10241. if(script_hasdata(st,(6+start_type)))
  10242. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10243. else
  10244. bl = map_id2bl(st->rid);
  10245. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  10246. {// When there isn't a duration specified, try to get it from the skill_db
  10247. tick = skill_get_time(status_sc2skill(type), val1);
  10248. }
  10249. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10250. bl = map_id2bl(potion_target);
  10251. tick /= 2;// Thrown potions only last half.
  10252. val4 = 1;// Mark that this was a thrown sc_effect
  10253. }
  10254. if(!bl)
  10255. return SCRIPT_CMD_SUCCESS;
  10256. switch(start_type) {
  10257. case 1:
  10258. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10259. break;
  10260. case 2:
  10261. val2 = script_getnum(st,5);
  10262. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10263. break;
  10264. case 4:
  10265. val2 = script_getnum(st,5);
  10266. val3 = script_getnum(st,6);
  10267. val4 = script_getnum(st,7);
  10268. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10269. break;
  10270. }
  10271. return SCRIPT_CMD_SUCCESS;
  10272. }
  10273. /// Ends one or all status effects on the target unit or on the attached player.
  10274. ///
  10275. /// sc_end <effect_id>{,<unit_id>};
  10276. BUILDIN_FUNC(sc_end)
  10277. {
  10278. struct block_list* bl;
  10279. int type;
  10280. type = script_getnum(st, 2);
  10281. if (script_hasdata(st, 3))
  10282. bl = map_id2bl(script_getnum(st, 3));
  10283. else
  10284. bl = map_id2bl(st->rid);
  10285. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10286. bl = map_id2bl(potion_target);
  10287. if (!bl)
  10288. return SCRIPT_CMD_SUCCESS;
  10289. if (type >= 0 && type < SC_MAX) {
  10290. struct status_change *sc = status_get_sc(bl);
  10291. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  10292. if (!sce)
  10293. return SCRIPT_CMD_SUCCESS;
  10294. switch (type) {
  10295. case SC_WEIGHT50:
  10296. case SC_WEIGHT90:
  10297. case SC_NOCHAT:
  10298. case SC_PUSH_CART:
  10299. case SC_ALL_RIDING:
  10300. case SC_STYLE_CHANGE:
  10301. case SC_MONSTER_TRANSFORM:
  10302. case SC_ACTIVE_MONSTER_TRANSFORM:
  10303. case SC_MTF_ASPD:
  10304. case SC_MTF_RANGEATK:
  10305. case SC_MTF_MATK:
  10306. case SC_MTF_MLEATKED:
  10307. case SC_MTF_CRIDAMAGE:
  10308. case SC_MTF_ASPD2:
  10309. case SC_MTF_RANGEATK2:
  10310. case SC_MTF_MATK2:
  10311. case SC_MTF_MHP:
  10312. case SC_MTF_MSP:
  10313. case SC_MTF_PUMPKIN:
  10314. case SC_MTF_HITFLEE:
  10315. case SC_ATTHASTE_CASH:
  10316. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  10317. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  10318. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  10319. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  10320. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  10321. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  10322. return SCRIPT_CMD_SUCCESS;
  10323. default:
  10324. break;
  10325. }
  10326. //This should help status_change_end force disabling the SC in case it has no limit.
  10327. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10328. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  10329. } else
  10330. status_change_clear(bl, 3); // remove all effects
  10331. return SCRIPT_CMD_SUCCESS;
  10332. }
  10333. /**
  10334. * Ends all status effects from any learned skill on the attached player.
  10335. * if <job_id> was given it will end the effect of that class for the attached player
  10336. * sc_end_class {<char_id>{,<job_id>}};
  10337. */
  10338. BUILDIN_FUNC(sc_end_class)
  10339. {
  10340. struct map_session_data *sd;
  10341. uint16 skill_id;
  10342. int class_;
  10343. if (!script_charid2sd(2, sd))
  10344. return SCRIPT_CMD_FAILURE;
  10345. if (script_hasdata(st, 3))
  10346. class_ = script_getnum(st, 3);
  10347. else
  10348. class_ = sd->status.class_;
  10349. if (!pcdb_checkid(class_)) {
  10350. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10351. return SCRIPT_CMD_FAILURE;
  10352. }
  10353. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(class_);
  10354. if( tree != nullptr ){
  10355. for (const auto &it : tree->skills) {
  10356. skill_id = it.first;
  10357. enum sc_type sc = status_skill2sc(skill_id);
  10358. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  10359. status_change_end(&sd->bl, sc, INVALID_TIMER);
  10360. }
  10361. }
  10362. return SCRIPT_CMD_SUCCESS;
  10363. }
  10364. /*==========================================
  10365. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10366. *------------------------------------------*/
  10367. BUILDIN_FUNC(getscrate)
  10368. {
  10369. struct block_list *bl;
  10370. int type;
  10371. t_tick rate;
  10372. type=script_getnum(st,2);
  10373. rate=script_getnum(st,3);
  10374. if( script_hasdata(st,4) ) //get for the bl assigned
  10375. bl = map_id2bl(script_getnum(st,4));
  10376. else
  10377. bl = map_id2bl(st->rid);
  10378. if (bl)
  10379. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10380. script_pushint(st,rate);
  10381. return SCRIPT_CMD_SUCCESS;
  10382. }
  10383. /**
  10384. * getstatus(<effect type>{,<type>{,<char_id>}});
  10385. **/
  10386. BUILDIN_FUNC(getstatus)
  10387. {
  10388. int id, type;
  10389. struct map_session_data* sd;
  10390. if (!script_charid2sd(4,sd))
  10391. return SCRIPT_CMD_FAILURE;
  10392. id = script_getnum(st, 2);
  10393. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10394. if( id <= SC_NONE || id >= SC_MAX )
  10395. {// invalid status type given
  10396. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10397. return SCRIPT_CMD_SUCCESS;
  10398. }
  10399. if( sd->sc.count == 0 || !sd->sc.data[id] )
  10400. {// no status is active
  10401. script_pushint(st, 0);
  10402. return SCRIPT_CMD_SUCCESS;
  10403. }
  10404. switch( type )
  10405. {
  10406. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  10407. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  10408. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  10409. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  10410. case 5:
  10411. {
  10412. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  10413. if( timer )
  10414. {// return the amount of time remaining
  10415. script_pushint(st, timer->tick - gettick());
  10416. } else {
  10417. script_pushint(st, -1);
  10418. }
  10419. }
  10420. break;
  10421. default: script_pushint(st, 1); break;
  10422. }
  10423. return SCRIPT_CMD_SUCCESS;
  10424. }
  10425. /*==========================================
  10426. *
  10427. *------------------------------------------*/
  10428. BUILDIN_FUNC(debugmes)
  10429. {
  10430. const char *str;
  10431. str=script_getstr(st,2);
  10432. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10433. return SCRIPT_CMD_SUCCESS;
  10434. }
  10435. BUILDIN_FUNC( errormes ){
  10436. ShowError( "%s\n", script_getstr( st, 2 ) );
  10437. script_reportsrc( st );
  10438. return SCRIPT_CMD_SUCCESS;
  10439. }
  10440. /*==========================================
  10441. *------------------------------------------*/
  10442. BUILDIN_FUNC(catchpet)
  10443. {
  10444. int pet_id;
  10445. TBL_PC *sd;
  10446. if( !script_rid2sd(sd) )
  10447. return SCRIPT_CMD_SUCCESS;
  10448. pet_id= script_getnum(st,2);
  10449. pet_catch_process1(sd,pet_id);
  10450. return SCRIPT_CMD_SUCCESS;
  10451. }
  10452. /*==========================================
  10453. * [orn]
  10454. *------------------------------------------*/
  10455. BUILDIN_FUNC(homunculus_evolution)
  10456. {
  10457. TBL_PC *sd;
  10458. if( !script_rid2sd(sd) )
  10459. return SCRIPT_CMD_SUCCESS;
  10460. if(hom_is_active(sd->hd))
  10461. {
  10462. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10463. hom_evolution(sd->hd);
  10464. else
  10465. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10466. }
  10467. return SCRIPT_CMD_SUCCESS;
  10468. }
  10469. /*==========================================
  10470. * Checks for vaporized morph state
  10471. * and deletes ITEMID_STRANGE_EMBRYO.
  10472. *------------------------------------------*/
  10473. BUILDIN_FUNC(homunculus_mutate)
  10474. {
  10475. int homun_id;
  10476. TBL_PC *sd;
  10477. if( !script_rid2sd(sd) || sd->hd == NULL )
  10478. return SCRIPT_CMD_SUCCESS;
  10479. if(script_hasdata(st,2))
  10480. homun_id = script_getnum(st,2);
  10481. else
  10482. homun_id = 6048 + (rnd() % 4);
  10483. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10484. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10485. int m_id = hom_class2mapid(homun_id);
  10486. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10487. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10488. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10489. hom_call(sd); // Respawn homunculus.
  10490. hom_mutate(sd->hd, homun_id);
  10491. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10492. script_pushint(st, 1);
  10493. return SCRIPT_CMD_SUCCESS;
  10494. } else
  10495. clif_emotion(&sd->bl, ET_SWEAT);
  10496. } else
  10497. clif_emotion(&sd->bl, ET_SWEAT);
  10498. script_pushint(st, 0);
  10499. return SCRIPT_CMD_SUCCESS;
  10500. }
  10501. /*==========================================
  10502. * Puts homunculus into morph state
  10503. * and gives ITEMID_STRANGE_EMBRYO.
  10504. *------------------------------------------*/
  10505. BUILDIN_FUNC(morphembryo)
  10506. {
  10507. struct item item_tmp;
  10508. TBL_PC *sd;
  10509. if( !script_rid2sd(sd) || sd->hd == NULL )
  10510. return SCRIPT_CMD_SUCCESS;
  10511. if( hom_is_active(sd->hd) ) {
  10512. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10513. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10514. char i;
  10515. memset(&item_tmp, 0, sizeof(item_tmp));
  10516. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10517. item_tmp.identify = 1;
  10518. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10519. clif_additem(sd, 0, 0, i);
  10520. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10521. } else {
  10522. hom_vaporize(sd, HOM_ST_MORPH);
  10523. script_pushint(st, 1);
  10524. return SCRIPT_CMD_SUCCESS;
  10525. }
  10526. } else
  10527. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10528. } else
  10529. clif_emotion(&sd->bl, ET_SWEAT);
  10530. script_pushint(st, 0);
  10531. return SCRIPT_CMD_SUCCESS;
  10532. }
  10533. // [Zephyrus]
  10534. BUILDIN_FUNC(homunculus_shuffle)
  10535. {
  10536. TBL_PC *sd;
  10537. if( !script_rid2sd(sd) )
  10538. return SCRIPT_CMD_SUCCESS;
  10539. if(hom_is_active(sd->hd))
  10540. hom_shuffle(sd->hd);
  10541. return SCRIPT_CMD_SUCCESS;
  10542. }
  10543. /*==========================================
  10544. * Check for homunculus state.
  10545. * Return: -1 = No homunculus
  10546. * 0 = Homunculus is active
  10547. * 1 = Homunculus is vaporized (rest)
  10548. * 2 = Homunculus is in morph state
  10549. *------------------------------------------*/
  10550. BUILDIN_FUNC(checkhomcall)
  10551. {
  10552. TBL_PC *sd;
  10553. TBL_HOM *hd;
  10554. if( !script_rid2sd(sd) )
  10555. return SCRIPT_CMD_SUCCESS;
  10556. hd = sd->hd;
  10557. if( !hd )
  10558. script_pushint(st, -1);
  10559. else
  10560. script_pushint(st, hd->homunculus.vaporize);
  10561. return SCRIPT_CMD_SUCCESS;
  10562. }
  10563. //These two functions bring the eA MAPID_* class functionality to scripts.
  10564. BUILDIN_FUNC(eaclass)
  10565. {
  10566. int class_;
  10567. if( script_hasdata(st,2) )
  10568. class_ = script_getnum(st,2);
  10569. else {
  10570. TBL_PC *sd;
  10571. if (!script_charid2sd(3,sd)) {
  10572. script_pushint(st,-1);
  10573. return SCRIPT_CMD_SUCCESS;
  10574. }
  10575. class_ = sd->status.class_;
  10576. }
  10577. script_pushint(st,pc_jobid2mapid(class_));
  10578. return SCRIPT_CMD_SUCCESS;
  10579. }
  10580. BUILDIN_FUNC(roclass)
  10581. {
  10582. int class_ =script_getnum(st,2);
  10583. int sex;
  10584. if( script_hasdata(st,3) )
  10585. sex = script_getnum(st,3);
  10586. else {
  10587. TBL_PC *sd;
  10588. if (st->rid && script_rid2sd(sd))
  10589. sex = sd->status.sex;
  10590. else
  10591. sex = SEX_MALE; //Just use male when not found.
  10592. }
  10593. script_pushint(st,pc_mapid2jobid(class_, sex));
  10594. return SCRIPT_CMD_SUCCESS;
  10595. }
  10596. /*==========================================
  10597. * Tells client to open a hatching window, used for pet incubator
  10598. *------------------------------------------*/
  10599. BUILDIN_FUNC(birthpet)
  10600. {
  10601. TBL_PC *sd;
  10602. if( !script_rid2sd(sd) )
  10603. return SCRIPT_CMD_SUCCESS;
  10604. if( sd->status.pet_id )
  10605. {// do not send egg list, when you already have a pet
  10606. return SCRIPT_CMD_SUCCESS;
  10607. }
  10608. clif_sendegg(sd);
  10609. return SCRIPT_CMD_SUCCESS;
  10610. }
  10611. /**
  10612. * resetlvl <action type>{,<char_id>};
  10613. * @param action_type:
  10614. * 1 : make like after rebirth
  10615. * 2 : blvl,jlvl=1, skillpoint=0
  10616. * 3 : don't reset skill, blvl=1
  10617. * 4 : jlvl=0
  10618. * @author AppleGirl
  10619. **/
  10620. BUILDIN_FUNC(resetlvl)
  10621. {
  10622. TBL_PC *sd;
  10623. int type=script_getnum(st,2);
  10624. if (!script_charid2sd(3,sd))
  10625. return SCRIPT_CMD_FAILURE;
  10626. pc_resetlvl(sd,type);
  10627. return SCRIPT_CMD_SUCCESS;
  10628. }
  10629. /**
  10630. * Reset a player status point
  10631. * resetstatus({<char_id>});
  10632. **/
  10633. BUILDIN_FUNC(resetstatus)
  10634. {
  10635. TBL_PC *sd;
  10636. if (!script_charid2sd(2,sd))
  10637. return SCRIPT_CMD_FAILURE;
  10638. pc_resetstate(sd);
  10639. return SCRIPT_CMD_SUCCESS;
  10640. }
  10641. /**
  10642. * Reset player's skill
  10643. * resetskill({<char_id>});
  10644. **/
  10645. BUILDIN_FUNC(resetskill)
  10646. {
  10647. TBL_PC *sd;
  10648. if (!script_charid2sd(2,sd))
  10649. return SCRIPT_CMD_FAILURE;
  10650. pc_resetskill(sd,1);
  10651. return SCRIPT_CMD_SUCCESS;
  10652. }
  10653. /**
  10654. * Reset SG designated maps
  10655. * resetfeel({<char_id>});
  10656. **/
  10657. BUILDIN_FUNC(resetfeel)
  10658. {
  10659. TBL_PC *sd;
  10660. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10661. return SCRIPT_CMD_FAILURE;
  10662. pc_resetfeel(sd);
  10663. return SCRIPT_CMD_SUCCESS;
  10664. }
  10665. /**
  10666. * Reset SG designated monsters
  10667. * resethate({<char_id>});
  10668. **/
  10669. BUILDIN_FUNC(resethate)
  10670. {
  10671. TBL_PC *sd;
  10672. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10673. return SCRIPT_CMD_FAILURE;
  10674. pc_resethate(sd);
  10675. return SCRIPT_CMD_SUCCESS;
  10676. }
  10677. /**
  10678. * Counts total amount of skill points.
  10679. * skillpointcount({<char_id>})
  10680. **/
  10681. BUILDIN_FUNC(skillpointcount)
  10682. {
  10683. TBL_PC *sd;
  10684. if (!script_charid2sd(2,sd))
  10685. return SCRIPT_CMD_FAILURE;
  10686. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10687. return SCRIPT_CMD_SUCCESS;
  10688. }
  10689. /*==========================================
  10690. *
  10691. *------------------------------------------*/
  10692. BUILDIN_FUNC(changebase)
  10693. {
  10694. TBL_PC *sd=NULL;
  10695. int vclass;
  10696. if( !script_mapid2sd(3,sd) )
  10697. return SCRIPT_CMD_SUCCESS;
  10698. vclass = script_getnum(st,2);
  10699. if(vclass == JOB_WEDDING)
  10700. {
  10701. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10702. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10703. )
  10704. return SCRIPT_CMD_SUCCESS;
  10705. }
  10706. if(!sd->disguise && vclass != sd->vd.class_) {
  10707. status_set_viewdata(&sd->bl, vclass);
  10708. //Updated client view. Base, Weapon and Cloth Colors.
  10709. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10710. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10711. if (sd->vd.cloth_color)
  10712. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10713. if (sd->vd.body_style)
  10714. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10715. clif_skillinfoblock(sd);
  10716. }
  10717. return SCRIPT_CMD_SUCCESS;
  10718. }
  10719. /**
  10720. * Change account sex and unequip all item and request for a changesex to char-serv
  10721. * changesex({<char_id>});
  10722. */
  10723. BUILDIN_FUNC(changesex)
  10724. {
  10725. int i;
  10726. TBL_PC *sd = NULL;
  10727. if (!script_charid2sd(2,sd))
  10728. return SCRIPT_CMD_FAILURE;
  10729. pc_resetskill(sd,4);
  10730. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10731. for(i = 0; i < EQI_MAX; i++) {
  10732. if (sd->equip_index[i] >= 0)
  10733. pc_unequipitem(sd, sd->equip_index[i], 3);
  10734. }
  10735. chrif_changesex(sd, true);
  10736. return SCRIPT_CMD_SUCCESS;
  10737. }
  10738. /**
  10739. * Change character's sex and unequip all item and request for a changesex to char-serv
  10740. * changecharsex({<char_id>});
  10741. */
  10742. BUILDIN_FUNC(changecharsex)
  10743. {
  10744. #if PACKETVER >= 20141016
  10745. int i;
  10746. TBL_PC *sd = NULL;
  10747. if (!script_charid2sd(2,sd))
  10748. return SCRIPT_CMD_FAILURE;
  10749. pc_resetskill(sd,4);
  10750. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10751. for (i = 0; i < EQI_MAX; i++) {
  10752. if (sd->equip_index[i] >= 0)
  10753. pc_unequipitem(sd, sd->equip_index[i], 3);
  10754. }
  10755. chrif_changesex(sd, false);
  10756. return SCRIPT_CMD_SUCCESS;
  10757. #else
  10758. return SCRIPT_CMD_FAILURE;
  10759. #endif
  10760. }
  10761. /*==========================================
  10762. * Works like 'announce' but outputs in the common chat window
  10763. *------------------------------------------*/
  10764. BUILDIN_FUNC(globalmes)
  10765. {
  10766. struct block_list *bl = map_id2bl(st->oid);
  10767. struct npc_data *nd = (struct npc_data *)bl;
  10768. const char *name=NULL,*mes;
  10769. mes=script_getstr(st,2);
  10770. if(mes==NULL)
  10771. return SCRIPT_CMD_SUCCESS;
  10772. if(script_hasdata(st,3)){ // npc name to display
  10773. name=script_getstr(st,3);
  10774. } else {
  10775. name=nd->name; //use current npc name
  10776. }
  10777. npc_globalmessage(name,mes); // broadcast to all players connected
  10778. return SCRIPT_CMD_SUCCESS;
  10779. }
  10780. /////////////////////////////////////////////////////////////////////
  10781. // NPC waiting room (chat room)
  10782. //
  10783. /// Creates a waiting room (chat room) for this npc.
  10784. ///
  10785. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10786. BUILDIN_FUNC(waitingroom)
  10787. {
  10788. struct npc_data* nd;
  10789. const char* title = script_getstr(st, 2);
  10790. int limit = script_getnum(st, 3);
  10791. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10792. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10793. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10794. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10795. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10796. nd = (struct npc_data *)map_id2bl(st->oid);
  10797. if( nd != NULL )
  10798. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10799. return SCRIPT_CMD_SUCCESS;
  10800. }
  10801. /// Removes the waiting room of the current or target npc.
  10802. ///
  10803. /// delwaitingroom "<npc_name>";
  10804. /// delwaitingroom;
  10805. BUILDIN_FUNC(delwaitingroom)
  10806. {
  10807. struct npc_data* nd;
  10808. if( script_hasdata(st,2) )
  10809. nd = npc_name2id(script_getstr(st, 2));
  10810. else
  10811. nd = (struct npc_data *)map_id2bl(st->oid);
  10812. if( nd != NULL )
  10813. chat_deletenpcchat(nd);
  10814. return SCRIPT_CMD_SUCCESS;
  10815. }
  10816. /// Kick the specified player from the waiting room of the target npc.
  10817. ///
  10818. /// waitingroomkick "<npc_name>", <kickusername>;
  10819. BUILDIN_FUNC(waitingroomkick)
  10820. {
  10821. struct npc_data* nd;
  10822. struct chat_data* cd;
  10823. const char* kickusername;
  10824. nd = npc_name2id(script_getstr(st,2));
  10825. kickusername = script_getstr(st,3);
  10826. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10827. chat_npckickchat(cd, kickusername);
  10828. return SCRIPT_CMD_SUCCESS;
  10829. }
  10830. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10831. /// Num users stored in .@waitingroom_usercount
  10832. ///
  10833. /// getwaitingroomusers "<npc_name>";
  10834. BUILDIN_FUNC(getwaitingroomusers)
  10835. {
  10836. struct npc_data* nd;
  10837. struct chat_data* cd;
  10838. int i, j=0;
  10839. if( script_hasdata(st,2) )
  10840. nd = npc_name2id(script_getstr(st, 2));
  10841. else
  10842. nd = (struct npc_data *)map_id2bl(st->oid);
  10843. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10844. for(i = 0; i < cd->users; ++i) {
  10845. setd_sub_num( st, NULL, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, NULL );
  10846. j++;
  10847. }
  10848. setd_sub_num( st, NULL, ".@waitingroom_usercount", 0, j, NULL );
  10849. }
  10850. return SCRIPT_CMD_SUCCESS;
  10851. }
  10852. /// Kicks all the players from the waiting room of the current or target npc.
  10853. ///
  10854. /// kickwaitingroomall "<npc_name>";
  10855. /// kickwaitingroomall;
  10856. BUILDIN_FUNC(waitingroomkickall)
  10857. {
  10858. struct npc_data* nd;
  10859. struct chat_data* cd;
  10860. if( script_hasdata(st,2) )
  10861. nd = npc_name2id(script_getstr(st,2));
  10862. else
  10863. nd = (struct npc_data *)map_id2bl(st->oid);
  10864. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10865. chat_npckickall(cd);
  10866. return SCRIPT_CMD_SUCCESS;
  10867. }
  10868. /// Enables the waiting room event of the current or target npc.
  10869. ///
  10870. /// enablewaitingroomevent "<npc_name>";
  10871. /// enablewaitingroomevent;
  10872. BUILDIN_FUNC(enablewaitingroomevent)
  10873. {
  10874. struct npc_data* nd;
  10875. struct chat_data* cd;
  10876. if( script_hasdata(st,2) )
  10877. nd = npc_name2id(script_getstr(st, 2));
  10878. else
  10879. nd = (struct npc_data *)map_id2bl(st->oid);
  10880. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10881. chat_enableevent(cd);
  10882. return SCRIPT_CMD_SUCCESS;
  10883. }
  10884. /// Disables the waiting room event of the current or target npc.
  10885. ///
  10886. /// disablewaitingroomevent "<npc_name>";
  10887. /// disablewaitingroomevent;
  10888. BUILDIN_FUNC(disablewaitingroomevent)
  10889. {
  10890. struct npc_data *nd;
  10891. struct chat_data *cd;
  10892. if( script_hasdata(st,2) )
  10893. nd = npc_name2id(script_getstr(st, 2));
  10894. else
  10895. nd = (struct npc_data *)map_id2bl(st->oid);
  10896. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10897. chat_disableevent(cd);
  10898. return SCRIPT_CMD_SUCCESS;
  10899. }
  10900. /// Returns info on the waiting room of the current or target npc.
  10901. /// Returns -1 if the type unknown
  10902. /// <type>=0 : current number of users
  10903. /// <type>=1 : maximum number of users allowed
  10904. /// <type>=2 : the number of users that trigger the event
  10905. /// <type>=3 : if the trigger is disabled
  10906. /// <type>=4 : the title of the waiting room
  10907. /// <type>=5 : the password of the waiting room
  10908. /// <type>=16 : the name of the waiting room event
  10909. /// <type>=32 : if the waiting room is full
  10910. /// <type>=33 : if there are enough users to trigger the event
  10911. ///
  10912. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10913. /// getwaitingroomstate(<type>) -> <info>
  10914. BUILDIN_FUNC(getwaitingroomstate)
  10915. {
  10916. struct npc_data *nd;
  10917. struct chat_data *cd;
  10918. int type;
  10919. type = script_getnum(st,2);
  10920. if( script_hasdata(st,3) )
  10921. nd = npc_name2id(script_getstr(st, 3));
  10922. else
  10923. nd = (struct npc_data *)map_id2bl(st->oid);
  10924. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10925. {
  10926. script_pushint(st, -1);
  10927. return SCRIPT_CMD_SUCCESS;
  10928. }
  10929. switch(type)
  10930. {
  10931. case 0: script_pushint(st, cd->users); break;
  10932. case 1: script_pushint(st, cd->limit); break;
  10933. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10934. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10935. case 4: script_pushstrcopy(st, cd->title); break;
  10936. case 5: script_pushstrcopy(st, cd->pass); break;
  10937. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10938. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10939. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10940. default: script_pushint(st, -1); break;
  10941. }
  10942. return SCRIPT_CMD_SUCCESS;
  10943. }
  10944. /// Warps the trigger or target amount of players to the target map and position.
  10945. /// Players are automatically removed from the waiting room.
  10946. /// Those waiting the longest will get warped first.
  10947. /// The target map can be "Random" for a random position in the current map,
  10948. /// and "SavePoint" for the savepoint map+position.
  10949. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10950. ///
  10951. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10952. /// The total number of teleported players is put into $@warpwaitingpcnum
  10953. ///
  10954. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10955. /// warpwaitingpc "<map name>",<x>,<y>;
  10956. BUILDIN_FUNC(warpwaitingpc)
  10957. {
  10958. int x;
  10959. int y;
  10960. int i;
  10961. int n;
  10962. const char* map_name;
  10963. struct npc_data* nd;
  10964. struct chat_data* cd;
  10965. nd = (struct npc_data *)map_id2bl(st->oid);
  10966. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10967. return SCRIPT_CMD_SUCCESS;
  10968. map_name = script_getstr(st,2);
  10969. x = script_getnum(st,3);
  10970. y = script_getnum(st,4);
  10971. n = cd->trigger&0x7f;
  10972. if( script_hasdata(st,5) )
  10973. n = script_getnum(st,5);
  10974. for( i = 0; i < n && cd->users > 0; i++ )
  10975. {
  10976. TBL_PC* sd = cd->usersd[0];
  10977. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  10978. {// can't teleport on this map
  10979. break;
  10980. }
  10981. if( cd->zeny )
  10982. {// fee set
  10983. if( (uint32)sd->status.zeny < cd->zeny )
  10984. {// no zeny to cover set fee
  10985. break;
  10986. }
  10987. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10988. }
  10989. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10990. if( strcmp(map_name,"Random") == 0 )
  10991. pc_randomwarp(sd,CLR_TELEPORT,true);
  10992. else if( strcmp(map_name,"SavePoint") == 0 )
  10993. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10994. else
  10995. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10996. }
  10997. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10998. return SCRIPT_CMD_SUCCESS;
  10999. }
  11000. /////////////////////////////////////////////////////////////////////
  11001. // ...
  11002. //
  11003. /// Detaches a character from a script.
  11004. ///
  11005. /// @param st Script state to detach the character from.
  11006. void script_detach_rid(struct script_state* st)
  11007. {
  11008. if(st->rid)
  11009. {
  11010. script_detach_state(st, false);
  11011. st->rid = 0;
  11012. }
  11013. }
  11014. /*=========================================================================
  11015. * Attaches a set of RIDs to the current script. [digitalhamster]
  11016. * addrid(<type>{,<flag>{,<parameters>}});
  11017. * <type>:
  11018. * 0 : All players in the server.
  11019. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  11020. * 2 : Party members of a specified party ID.
  11021. * [ Parameters: <party id> ]
  11022. * 3 : Guild members of a specified guild ID.
  11023. * [ Parameters: <guild id> ]
  11024. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  11025. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  11026. * 5 : All players in the map.
  11027. * [ Parameters: "<map name>" ]
  11028. * Account ID: The specified account ID.
  11029. * <flag>:
  11030. * 0 : Players are always attached. (default)
  11031. * 1 : Players currently running another script will not be attached.
  11032. *-------------------------------------------------------------------------*/
  11033. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  11034. {
  11035. int forceflag;
  11036. struct map_session_data *sd = (TBL_PC *)bl;
  11037. struct script_state* st;
  11038. st = va_arg(ap,struct script_state*);
  11039. forceflag = va_arg(ap,int);
  11040. if(!forceflag || !sd->st)
  11041. if(sd->status.account_id != st->rid)
  11042. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11043. return 0;
  11044. }
  11045. BUILDIN_FUNC(addrid)
  11046. {
  11047. struct s_mapiterator* iter;
  11048. struct block_list *bl;
  11049. TBL_PC *sd;
  11050. if(st->rid < 1) {
  11051. st->state = END;
  11052. bl = map_id2bl(st->oid);
  11053. } else
  11054. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  11055. iter = mapit_getallusers();
  11056. switch(script_getnum(st,2)) {
  11057. case 0:
  11058. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11059. if(!script_getnum(st,3) || !sd->st)
  11060. if(sd->status.account_id != st->rid) //attached player already runs.
  11061. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11062. }
  11063. break;
  11064. case 1:
  11065. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11066. if(!script_getnum(st,3) || !sd->st)
  11067. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  11068. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11069. }
  11070. break;
  11071. case 2:
  11072. if(script_getnum(st,4) == 0) {
  11073. script_pushint(st,0);
  11074. mapit_free(iter);
  11075. return SCRIPT_CMD_SUCCESS;
  11076. }
  11077. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11078. if(!script_getnum(st,3) || !sd->st)
  11079. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  11080. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11081. }
  11082. break;
  11083. case 3:
  11084. if(script_getnum(st,4) == 0) {
  11085. script_pushint(st,0);
  11086. mapit_free(iter);
  11087. return SCRIPT_CMD_SUCCESS;
  11088. }
  11089. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11090. if(!script_getnum(st,3) || !sd->st)
  11091. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  11092. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11093. }
  11094. break;
  11095. case 4:
  11096. map_foreachinallarea(buildin_addrid_sub,
  11097. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  11098. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  11099. break;
  11100. case 5:
  11101. if (script_getstr(st, 4) == NULL) {
  11102. script_pushint(st, 0);
  11103. mapit_free(iter);
  11104. return SCRIPT_CMD_FAILURE;
  11105. }
  11106. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  11107. script_pushint(st, 0);
  11108. mapit_free(iter);
  11109. return SCRIPT_CMD_FAILURE;
  11110. }
  11111. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  11112. break;
  11113. default:
  11114. mapit_free(iter);
  11115. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  11116. script_pushint(st,0);
  11117. return SCRIPT_CMD_SUCCESS;
  11118. }
  11119. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  11120. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  11121. script_pushint(st,1);
  11122. }
  11123. return SCRIPT_CMD_SUCCESS;
  11124. }
  11125. mapit_free(iter);
  11126. script_pushint(st,1);
  11127. return SCRIPT_CMD_SUCCESS;
  11128. }
  11129. /*==========================================
  11130. * Attach sd char id to script and detach current one if any
  11131. *------------------------------------------*/
  11132. BUILDIN_FUNC(attachrid)
  11133. {
  11134. int rid = script_getnum(st,2);
  11135. bool force;
  11136. if( script_hasdata(st,3) ){
  11137. force = script_getnum(st,3) != 0;
  11138. }else{
  11139. force = true;
  11140. }
  11141. struct map_session_data* sd = map_id2sd(rid);
  11142. if( sd != NULL && ( !sd->npc_id || force ) ){
  11143. script_detach_rid(st);
  11144. st->rid = rid;
  11145. script_attach_state(st);
  11146. script_pushint(st,true);
  11147. }else{
  11148. script_pushint(st,false);
  11149. }
  11150. return SCRIPT_CMD_SUCCESS;
  11151. }
  11152. /*==========================================
  11153. * Detach script to rid
  11154. *------------------------------------------*/
  11155. BUILDIN_FUNC(detachrid)
  11156. {
  11157. script_detach_rid(st);
  11158. return SCRIPT_CMD_SUCCESS;
  11159. }
  11160. /*==========================================
  11161. * Chk if account connected, (and charid from account if specified)
  11162. *------------------------------------------*/
  11163. BUILDIN_FUNC(isloggedin)
  11164. {
  11165. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11166. if (script_hasdata(st,3) && sd &&
  11167. sd->status.char_id != script_getnum(st,3))
  11168. sd = NULL;
  11169. push_val(st->stack,C_INT,sd!=NULL);
  11170. return SCRIPT_CMD_SUCCESS;
  11171. }
  11172. /*==========================================
  11173. *
  11174. *------------------------------------------*/
  11175. BUILDIN_FUNC(setmapflagnosave)
  11176. {
  11177. int16 m,x,y;
  11178. unsigned short mapindex;
  11179. const char *str,*str2;
  11180. union u_mapflag_args args = {};
  11181. str=script_getstr(st,2);
  11182. str2=script_getstr(st,3);
  11183. x=script_getnum(st,4);
  11184. y=script_getnum(st,5);
  11185. m = map_mapname2mapid(str);
  11186. mapindex = mapindex_name2id(str2);
  11187. if(m < 0) {
  11188. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11189. return SCRIPT_CMD_FAILURE;
  11190. }
  11191. args.nosave.map = mapindex;
  11192. args.nosave.x = x;
  11193. args.nosave.y = y;
  11194. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11195. return SCRIPT_CMD_SUCCESS;
  11196. }
  11197. BUILDIN_FUNC(getmapflag)
  11198. {
  11199. int16 m;
  11200. int mf;
  11201. const char *str;
  11202. str=script_getstr(st,2);
  11203. m = map_mapname2mapid(str);
  11204. if (m < 0) {
  11205. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11206. return SCRIPT_CMD_FAILURE;
  11207. }
  11208. mf = script_getnum(st, 3);
  11209. if( mf < MF_MIN || mf > MF_MAX ){
  11210. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11211. return SCRIPT_CMD_FAILURE;
  11212. }
  11213. union u_mapflag_args args = {};
  11214. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11215. args.flag_val = SKILLDMG_MAX;
  11216. else
  11217. FETCH(4, args.flag_val);
  11218. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11219. return SCRIPT_CMD_SUCCESS;
  11220. }
  11221. BUILDIN_FUNC(setmapflag)
  11222. {
  11223. int16 m;
  11224. int mf;
  11225. const char *str;
  11226. str = script_getstr(st,2);
  11227. m = map_mapname2mapid(str);
  11228. if (m < 0) {
  11229. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11230. return SCRIPT_CMD_FAILURE;
  11231. }
  11232. mf = script_getnum(st, 3);
  11233. if( mf < MF_MIN || mf > MF_MAX ){
  11234. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11235. return SCRIPT_CMD_FAILURE;
  11236. }
  11237. union u_mapflag_args args = {};
  11238. switch(mf) {
  11239. case MF_SKILL_DAMAGE:
  11240. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11241. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11242. else {
  11243. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11244. return SCRIPT_CMD_FAILURE;
  11245. }
  11246. break;
  11247. case MF_SKILL_DURATION:
  11248. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11249. args.skill_duration.skill_id = script_getnum(st, 4);
  11250. if (!skill_get_index(args.skill_duration.skill_id)) {
  11251. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11252. return SCRIPT_CMD_FAILURE;
  11253. }
  11254. args.skill_duration.per = script_getnum(st, 5);
  11255. } else {
  11256. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11257. return SCRIPT_CMD_FAILURE;
  11258. }
  11259. break;
  11260. case MF_NOSAVE: // Assume setting "SavePoint"
  11261. args.nosave.map = 0;
  11262. args.nosave.x = -1;
  11263. args.nosave.y = -1;
  11264. break;
  11265. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11266. args.nightmaredrop.drop_id = -1;
  11267. args.nightmaredrop.drop_per = 300;
  11268. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11269. break;
  11270. default:
  11271. FETCH(4, args.flag_val);
  11272. break;
  11273. }
  11274. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11275. return SCRIPT_CMD_SUCCESS;
  11276. }
  11277. BUILDIN_FUNC(removemapflag)
  11278. {
  11279. int16 m;
  11280. int mf;
  11281. const char *str;
  11282. union u_mapflag_args args = {};
  11283. str = script_getstr(st, 2);
  11284. m = map_mapname2mapid(str);
  11285. if (m < 0) {
  11286. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11287. return SCRIPT_CMD_FAILURE;
  11288. }
  11289. mf = script_getnum(st, 3);
  11290. if( mf < MF_MIN || mf > MF_MAX ){
  11291. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11292. return SCRIPT_CMD_FAILURE;
  11293. }
  11294. FETCH(4, args.flag_val);
  11295. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11296. return SCRIPT_CMD_SUCCESS;
  11297. }
  11298. BUILDIN_FUNC(pvpon)
  11299. {
  11300. int16 m;
  11301. const char *str = script_getstr(st,2);
  11302. m = map_mapname2mapid(str);
  11303. if (m < 0) {
  11304. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11305. return SCRIPT_CMD_FAILURE;
  11306. }
  11307. if (!map_getmapflag(m, MF_PVP))
  11308. map_setmapflag(m, MF_PVP, true);
  11309. return SCRIPT_CMD_SUCCESS;
  11310. }
  11311. BUILDIN_FUNC(pvpoff)
  11312. {
  11313. int16 m;
  11314. const char *str = script_getstr(st,2);
  11315. m = map_mapname2mapid(str);
  11316. if (m < 0) {
  11317. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11318. return SCRIPT_CMD_FAILURE;
  11319. }
  11320. if (map_getmapflag(m, MF_PVP))
  11321. map_setmapflag(m, MF_PVP, false);
  11322. return SCRIPT_CMD_SUCCESS;
  11323. }
  11324. BUILDIN_FUNC(gvgon)
  11325. {
  11326. int16 m;
  11327. const char *str = script_getstr(st,2);
  11328. m = map_mapname2mapid(str);
  11329. if (m < 0) {
  11330. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11331. return SCRIPT_CMD_FAILURE;
  11332. }
  11333. if (!map_getmapflag(m, MF_GVG))
  11334. map_setmapflag(m, MF_GVG, true);
  11335. return SCRIPT_CMD_SUCCESS;
  11336. }
  11337. BUILDIN_FUNC(gvgoff)
  11338. {
  11339. int16 m;
  11340. const char *str = script_getstr(st,2);
  11341. m = map_mapname2mapid(str);
  11342. if (m < 0) {
  11343. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11344. return SCRIPT_CMD_FAILURE;
  11345. }
  11346. if (map_getmapflag(m, MF_GVG))
  11347. map_setmapflag(m, MF_GVG, false);
  11348. return SCRIPT_CMD_SUCCESS;
  11349. }
  11350. BUILDIN_FUNC(gvgon3)
  11351. {
  11352. int16 m;
  11353. const char *str = script_getstr(st,2);
  11354. m = map_mapname2mapid(str);
  11355. if (m < 0) {
  11356. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11357. return SCRIPT_CMD_FAILURE;
  11358. }
  11359. if (!map_getmapflag(m, MF_GVG_TE))
  11360. map_setmapflag(m, MF_GVG_TE, true);
  11361. return SCRIPT_CMD_SUCCESS;
  11362. }
  11363. BUILDIN_FUNC(gvgoff3)
  11364. {
  11365. int16 m;
  11366. const char *str = script_getstr(st,2);
  11367. m = map_mapname2mapid(str);
  11368. if (m < 0) {
  11369. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11370. return SCRIPT_CMD_FAILURE;
  11371. }
  11372. if (map_getmapflag(m, MF_GVG_TE))
  11373. map_setmapflag(m, MF_GVG_TE, false);
  11374. return SCRIPT_CMD_SUCCESS;
  11375. }
  11376. /**
  11377. * Shows an emotion on top of a NPC by default or the given GID
  11378. * emotion <emotion ID>{,<target ID>};
  11379. */
  11380. BUILDIN_FUNC(emotion)
  11381. {
  11382. struct block_list *bl = NULL;
  11383. int type = script_getnum(st,2);
  11384. if (type < ET_SURPRISE || type >= ET_MAX) {
  11385. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11386. return SCRIPT_CMD_FAILURE;
  11387. }
  11388. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11389. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11390. return SCRIPT_CMD_FAILURE;
  11391. }
  11392. if (!bl)
  11393. bl = map_id2bl(st->oid);
  11394. clif_emotion(bl, type);
  11395. return SCRIPT_CMD_SUCCESS;
  11396. }
  11397. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11398. {
  11399. int16 m=va_arg(ap,int);
  11400. int g_id=va_arg(ap,int);
  11401. int flag=va_arg(ap,int);
  11402. if(!sd || sd->bl.m != m)
  11403. return 0;
  11404. if(
  11405. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11406. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11407. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11408. )
  11409. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11410. return 1;
  11411. }
  11412. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11413. {
  11414. struct mob_data *md=(struct mob_data *)bl;
  11415. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11416. status_kill(bl);
  11417. return 1;
  11418. }
  11419. /*
  11420. * Function to kick guild members out of a map and to their save points.
  11421. * m : mapid
  11422. * g_id : owner guild id
  11423. * flag & 1 : Warp out owners
  11424. * flag & 2 : Warp out outsiders
  11425. * flag & 4 : reserved for mobs
  11426. * */
  11427. BUILDIN_FUNC(maprespawnguildid)
  11428. {
  11429. const char *mapname=script_getstr(st,2);
  11430. int g_id=script_getnum(st,3);
  11431. int flag=script_getnum(st,4);
  11432. int16 m=map_mapname2mapid(mapname);
  11433. if(m == -1)
  11434. return SCRIPT_CMD_SUCCESS;
  11435. //Catch ALL players (in case some are 'between maps' on execution time)
  11436. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11437. if (flag&4) //Remove script mobs.
  11438. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11439. return SCRIPT_CMD_SUCCESS;
  11440. }
  11441. /// Siege commands
  11442. /**
  11443. * Start WoE:FE
  11444. * agitstart();
  11445. */
  11446. BUILDIN_FUNC(agitstart)
  11447. {
  11448. guild_agit_start();
  11449. return SCRIPT_CMD_SUCCESS;
  11450. }
  11451. /**
  11452. * End WoE:FE
  11453. * agitend();
  11454. */
  11455. BUILDIN_FUNC(agitend)
  11456. {
  11457. guild_agit_end();
  11458. return SCRIPT_CMD_SUCCESS;
  11459. }
  11460. /**
  11461. * Start WoE:SE
  11462. * agitstart2();
  11463. */
  11464. BUILDIN_FUNC(agitstart2)
  11465. {
  11466. guild_agit2_start();
  11467. return SCRIPT_CMD_SUCCESS;
  11468. }
  11469. /**
  11470. * End WoE:SE
  11471. * agitend();
  11472. */
  11473. BUILDIN_FUNC(agitend2)
  11474. {
  11475. guild_agit2_end();
  11476. return SCRIPT_CMD_SUCCESS;
  11477. }
  11478. /**
  11479. * Start WoE:TE
  11480. * agitstart3();
  11481. */
  11482. BUILDIN_FUNC(agitstart3)
  11483. {
  11484. guild_agit3_start();
  11485. return SCRIPT_CMD_SUCCESS;
  11486. }
  11487. /**
  11488. * End WoE:TE
  11489. * agitend3();
  11490. */
  11491. BUILDIN_FUNC(agitend3)
  11492. {
  11493. guild_agit3_end();
  11494. return SCRIPT_CMD_SUCCESS;
  11495. }
  11496. /**
  11497. * Returns whether WoE:FE is on or off.
  11498. * agitcheck();
  11499. */
  11500. BUILDIN_FUNC(agitcheck)
  11501. {
  11502. script_pushint(st, agit_flag);
  11503. return SCRIPT_CMD_SUCCESS;
  11504. }
  11505. /**
  11506. * Returns whether WoE:SE is on or off.
  11507. * agitcheck2();
  11508. */
  11509. BUILDIN_FUNC(agitcheck2)
  11510. {
  11511. script_pushint(st, agit2_flag);
  11512. return SCRIPT_CMD_SUCCESS;
  11513. }
  11514. /**
  11515. * Returns whether WoE:TE is on or off.
  11516. * agitcheck3();
  11517. */
  11518. BUILDIN_FUNC(agitcheck3)
  11519. {
  11520. script_pushint(st, agit3_flag);
  11521. return SCRIPT_CMD_SUCCESS;
  11522. }
  11523. /// Sets the guild_id of this npc.
  11524. ///
  11525. /// flagemblem <guild_id>;
  11526. BUILDIN_FUNC(flagemblem)
  11527. {
  11528. TBL_NPC* nd;
  11529. int g_id = script_getnum(st,2);
  11530. if(g_id < 0)
  11531. return SCRIPT_CMD_SUCCESS;
  11532. nd = (TBL_NPC*)map_id2nd(st->oid);
  11533. if( nd == NULL ) {
  11534. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11535. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11536. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11537. } else {
  11538. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11539. nd->u.scr.guild_id = g_id;
  11540. clif_guild_emblem_area(&nd->bl);
  11541. /* guild flag caching */
  11542. if( g_id ) /* adding a id */
  11543. guild_flag_add(nd);
  11544. else if( changed ) /* removing a flag */
  11545. guild_flag_remove(nd);
  11546. }
  11547. return SCRIPT_CMD_SUCCESS;
  11548. }
  11549. BUILDIN_FUNC(getcastlename)
  11550. {
  11551. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11552. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11553. const char* name = (gc) ? gc->castle_name : "";
  11554. script_pushstrcopy(st,name);
  11555. return SCRIPT_CMD_SUCCESS;
  11556. }
  11557. BUILDIN_FUNC(getcastledata)
  11558. {
  11559. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11560. int index = script_getnum(st,3);
  11561. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11562. if (gc == NULL) {
  11563. script_pushint(st,0);
  11564. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11565. return SCRIPT_CMD_FAILURE;
  11566. }
  11567. switch (index) {
  11568. case CD_GUILD_ID:
  11569. script_pushint(st,gc->guild_id); break;
  11570. case CD_CURRENT_ECONOMY:
  11571. script_pushint(st,gc->economy); break;
  11572. case CD_CURRENT_DEFENSE:
  11573. script_pushint(st,gc->defense); break;
  11574. case CD_INVESTED_ECONOMY:
  11575. script_pushint(st,gc->triggerE); break;
  11576. case CD_INVESTED_DEFENSE:
  11577. script_pushint(st,gc->triggerD); break;
  11578. case CD_NEXT_TIME:
  11579. script_pushint(st,gc->nextTime); break;
  11580. case CD_PAY_TIME:
  11581. script_pushint(st,gc->payTime); break;
  11582. case CD_CREATE_TIME:
  11583. script_pushint(st,gc->createTime); break;
  11584. case CD_ENABLED_KAFRA:
  11585. script_pushint(st,gc->visibleC); break;
  11586. default:
  11587. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11588. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11589. break;
  11590. }
  11591. script_pushint(st,0);
  11592. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11593. return SCRIPT_CMD_FAILURE;
  11594. }
  11595. return SCRIPT_CMD_SUCCESS;
  11596. }
  11597. BUILDIN_FUNC(setcastledata)
  11598. {
  11599. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11600. int index = script_getnum(st,3);
  11601. int value = script_getnum(st,4);
  11602. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11603. if (gc == NULL) {
  11604. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11605. return SCRIPT_CMD_FAILURE;
  11606. }
  11607. if (index <= CD_NONE || index >= CD_MAX) {
  11608. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11609. return SCRIPT_CMD_FAILURE;
  11610. }
  11611. guild_castledatasave(gc->castle_id, index, value);
  11612. return SCRIPT_CMD_SUCCESS;
  11613. }
  11614. /* =====================================================================
  11615. * ---------------------------------------------------------------------*/
  11616. BUILDIN_FUNC(requestguildinfo)
  11617. {
  11618. int guild_id=script_getnum(st,2);
  11619. const char *event=NULL;
  11620. if( script_hasdata(st,3) ){
  11621. event=script_getstr(st,3);
  11622. check_event(st, event);
  11623. }
  11624. if(guild_id>0)
  11625. guild_npc_request_info(guild_id,event);
  11626. return SCRIPT_CMD_SUCCESS;
  11627. }
  11628. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11629. /// getequipcardcnt(<equipment slot>);
  11630. BUILDIN_FUNC(getequipcardcnt)
  11631. {
  11632. int i=-1,j,num;
  11633. TBL_PC *sd;
  11634. int count;
  11635. if( !script_rid2sd(sd) )
  11636. return SCRIPT_CMD_SUCCESS;
  11637. num=script_getnum(st,2);
  11638. if (equip_index_check(num))
  11639. i=pc_checkequip(sd,equip_bitmask[num]);
  11640. if (i < 0 || !sd->inventory_data[i]) {
  11641. script_pushint(st,0);
  11642. return SCRIPT_CMD_SUCCESS;
  11643. }
  11644. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11645. {
  11646. script_pushint(st,0);
  11647. return SCRIPT_CMD_SUCCESS;
  11648. }
  11649. count = 0;
  11650. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11651. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11652. count++;
  11653. script_pushint(st,count);
  11654. return SCRIPT_CMD_SUCCESS;
  11655. }
  11656. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11657. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11658. /// successremovecards <slot>;
  11659. BUILDIN_FUNC(successremovecards) {
  11660. int i=-1,c,cardflag=0;
  11661. TBL_PC* sd;
  11662. int num;
  11663. if( !script_rid2sd(sd) )
  11664. return SCRIPT_CMD_SUCCESS;
  11665. num = script_getnum(st,2);
  11666. if (equip_index_check(num))
  11667. i=pc_checkequip(sd,equip_bitmask[num]);
  11668. if (i < 0 || !sd->inventory_data[i]) {
  11669. return SCRIPT_CMD_SUCCESS;
  11670. }
  11671. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11672. return SCRIPT_CMD_SUCCESS;
  11673. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11674. 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
  11675. unsigned char flag = 0;
  11676. struct item item_tmp;
  11677. memset(&item_tmp,0,sizeof(item_tmp));
  11678. cardflag = 1;
  11679. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11680. item_tmp.identify = 1;
  11681. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11682. clif_additem(sd,0,0,flag);
  11683. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11684. }
  11685. }
  11686. }
  11687. if(cardflag == 1) {//if card was remove remplace item with no card
  11688. unsigned char flag = 0;
  11689. struct item item_tmp;
  11690. memset(&item_tmp,0,sizeof(item_tmp));
  11691. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11692. item_tmp.identify = 1;
  11693. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11694. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11695. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11696. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11697. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  11698. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11699. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11700. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11701. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11702. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11703. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11704. }
  11705. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11706. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11707. clif_additem(sd,0,0,flag);
  11708. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11709. }
  11710. clif_misceffect(&sd->bl,3);
  11711. }
  11712. return SCRIPT_CMD_SUCCESS;
  11713. }
  11714. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11715. /// failedremovecards <slot>, <type>;
  11716. /// <type>=0 : will destroy both the item and the cards.
  11717. /// <type>=1 : will keep the item, but destroy the cards.
  11718. /// <type>=2 : will keep the cards, but destroy the item.
  11719. /// <type>=? : will just display the failure effect.
  11720. BUILDIN_FUNC(failedremovecards) {
  11721. int i=-1,c,cardflag=0;
  11722. TBL_PC* sd;
  11723. int num;
  11724. int typefail;
  11725. if( !script_rid2sd(sd) )
  11726. return SCRIPT_CMD_SUCCESS;
  11727. num = script_getnum(st,2);
  11728. typefail = script_getnum(st,3);
  11729. if (equip_index_check(num))
  11730. i=pc_checkequip(sd,equip_bitmask[num]);
  11731. if (i < 0 || !sd->inventory_data[i])
  11732. return SCRIPT_CMD_SUCCESS;
  11733. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11734. return SCRIPT_CMD_SUCCESS;
  11735. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11736. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11737. cardflag = 1;
  11738. if(typefail == 2) {// add cards to inventory, clear
  11739. unsigned char flag = 0;
  11740. struct item item_tmp;
  11741. memset(&item_tmp,0,sizeof(item_tmp));
  11742. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11743. item_tmp.identify = 1;
  11744. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11745. clif_additem(sd,0,0,flag);
  11746. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11747. }
  11748. }
  11749. }
  11750. }
  11751. if(cardflag == 1) {
  11752. if(typefail == 0 || typefail == 2){ // destroy the item
  11753. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11754. }else if(typefail == 1){ // destroy the card
  11755. unsigned char flag = 0;
  11756. struct item item_tmp;
  11757. memset(&item_tmp,0,sizeof(item_tmp));
  11758. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11759. item_tmp.identify = 1;
  11760. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11761. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11762. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11763. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11764. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  11765. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11766. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11767. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11768. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11769. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11770. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11771. }
  11772. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11773. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11774. clif_additem(sd,0,0,flag);
  11775. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11776. }
  11777. }
  11778. clif_misceffect(&sd->bl,2);
  11779. }
  11780. return SCRIPT_CMD_SUCCESS;
  11781. }
  11782. /* ================================================================
  11783. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11784. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11785. * improved by [Lance]
  11786. * ================================================================*/
  11787. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11788. {
  11789. int x,y,m,check_val=0,check_ID=0,i=0;
  11790. struct guild *g = NULL;
  11791. struct party_data *p = NULL;
  11792. const char *str;
  11793. const char *mapname;
  11794. unsigned int index;
  11795. mapname=script_getstr(st,2);
  11796. str=script_getstr(st,3);
  11797. x=script_getnum(st,4);
  11798. y=script_getnum(st,5);
  11799. if(script_hasdata(st,7)){
  11800. check_val=script_getnum(st,6);
  11801. check_ID=script_getnum(st,7);
  11802. }
  11803. if((m=map_mapname2mapid(mapname))< 0)
  11804. return SCRIPT_CMD_SUCCESS;
  11805. if(!(index=mapindex_name2id(str)))
  11806. return SCRIPT_CMD_SUCCESS;
  11807. switch(check_val){
  11808. case 1:
  11809. g = guild_search(check_ID);
  11810. if (g){
  11811. for( i=0; i < g->max_member; i++)
  11812. {
  11813. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11814. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11815. }
  11816. }
  11817. }
  11818. break;
  11819. case 2:
  11820. p = party_search(check_ID);
  11821. if(p){
  11822. for(i=0;i<MAX_PARTY; i++){
  11823. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11824. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11825. }
  11826. }
  11827. }
  11828. break;
  11829. default:
  11830. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11831. break;
  11832. }
  11833. return SCRIPT_CMD_SUCCESS;
  11834. }
  11835. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11836. {
  11837. char *event=va_arg(ap,char *);
  11838. struct mob_data *md = ((struct mob_data *)bl);
  11839. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11840. return 1;
  11841. return SCRIPT_CMD_SUCCESS;
  11842. }
  11843. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11844. {
  11845. const char *mapname,*event;
  11846. int16 m;
  11847. mapname=script_getstr(st,2);
  11848. event=script_getstr(st,3);
  11849. if( strcmp(event, "all") == 0 )
  11850. event = NULL;
  11851. else
  11852. check_event(st, event);
  11853. if( strcmp(mapname, "this") == 0 ) {
  11854. struct map_session_data *sd;
  11855. if( script_rid2sd(sd) )
  11856. m = sd->bl.m;
  11857. else {
  11858. script_pushint(st,-1);
  11859. return SCRIPT_CMD_SUCCESS;
  11860. }
  11861. }
  11862. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11863. script_pushint(st,-1);
  11864. return SCRIPT_CMD_SUCCESS;
  11865. }
  11866. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11867. return SCRIPT_CMD_SUCCESS;
  11868. }
  11869. BUILDIN_FUNC(marriage)
  11870. {
  11871. TBL_PC *sd;
  11872. TBL_PC *p_sd;
  11873. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11874. script_pushint(st,0);
  11875. return SCRIPT_CMD_SUCCESS;
  11876. }
  11877. script_pushint(st,1);
  11878. return SCRIPT_CMD_SUCCESS;
  11879. }
  11880. BUILDIN_FUNC(wedding_effect)
  11881. {
  11882. TBL_PC *sd;
  11883. struct block_list *bl;
  11884. if(!script_rid2sd(sd)) {
  11885. bl=map_id2bl(st->oid);
  11886. } else
  11887. bl=&sd->bl;
  11888. clif_wedding_effect(bl);
  11889. return SCRIPT_CMD_SUCCESS;
  11890. }
  11891. /**
  11892. * divorce({<char_id>})
  11893. **/
  11894. BUILDIN_FUNC(divorce)
  11895. {
  11896. TBL_PC *sd;
  11897. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11898. script_pushint(st,0);
  11899. return SCRIPT_CMD_FAILURE;
  11900. }
  11901. script_pushint(st,1);
  11902. return SCRIPT_CMD_SUCCESS;
  11903. }
  11904. /**
  11905. * ispartneron({<char_id>})
  11906. **/
  11907. BUILDIN_FUNC(ispartneron)
  11908. {
  11909. TBL_PC *sd;
  11910. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11911. map_charid2sd(sd->status.partner_id) == NULL)
  11912. {
  11913. script_pushint(st,0);
  11914. return SCRIPT_CMD_FAILURE;
  11915. }
  11916. script_pushint(st,1);
  11917. return SCRIPT_CMD_SUCCESS;
  11918. }
  11919. /**
  11920. * getpartnerid({<char_id>})
  11921. **/
  11922. BUILDIN_FUNC(getpartnerid)
  11923. {
  11924. TBL_PC *sd;
  11925. if (!script_charid2sd(2,sd)) {
  11926. script_pushint(st,0);
  11927. return SCRIPT_CMD_FAILURE;
  11928. }
  11929. script_pushint(st,sd->status.partner_id);
  11930. return SCRIPT_CMD_SUCCESS;
  11931. }
  11932. /**
  11933. * getchildid({<char_id>})
  11934. **/
  11935. BUILDIN_FUNC(getchildid)
  11936. {
  11937. TBL_PC *sd;
  11938. if (!script_charid2sd(2,sd)) {
  11939. script_pushint(st,0);
  11940. return SCRIPT_CMD_FAILURE;
  11941. }
  11942. script_pushint(st,sd->status.child);
  11943. return SCRIPT_CMD_SUCCESS;
  11944. }
  11945. /**
  11946. * getmotherid({<char_id>})
  11947. **/
  11948. BUILDIN_FUNC(getmotherid)
  11949. {
  11950. TBL_PC *sd;
  11951. if (!script_charid2sd(2,sd)) {
  11952. script_pushint(st,0);
  11953. return SCRIPT_CMD_FAILURE;
  11954. }
  11955. script_pushint(st,sd->status.mother);
  11956. return SCRIPT_CMD_SUCCESS;
  11957. }
  11958. /**
  11959. * getfatherid({<char_id>})
  11960. **/
  11961. BUILDIN_FUNC(getfatherid)
  11962. {
  11963. TBL_PC *sd;
  11964. if (!script_charid2sd(2,sd)) {
  11965. script_pushint(st,0);
  11966. return SCRIPT_CMD_FAILURE;
  11967. }
  11968. script_pushint(st,sd->status.father);
  11969. return SCRIPT_CMD_SUCCESS;
  11970. }
  11971. BUILDIN_FUNC(warppartner)
  11972. {
  11973. int x,y;
  11974. unsigned short mapindex;
  11975. const char *str;
  11976. TBL_PC *sd;
  11977. TBL_PC *p_sd;
  11978. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  11979. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11980. script_pushint(st,0);
  11981. return SCRIPT_CMD_SUCCESS;
  11982. }
  11983. str=script_getstr(st,2);
  11984. x=script_getnum(st,3);
  11985. y=script_getnum(st,4);
  11986. mapindex = mapindex_name2id(str);
  11987. if (mapindex) {
  11988. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11989. script_pushint(st,1);
  11990. } else
  11991. script_pushint(st,0);
  11992. return SCRIPT_CMD_SUCCESS;
  11993. }
  11994. /*================================================
  11995. * Script for Displaying MOB Information [Valaris]
  11996. *------------------------------------------------*/
  11997. BUILDIN_FUNC(strmobinfo)
  11998. {
  11999. int num=script_getnum(st,2);
  12000. int class_=script_getnum(st,3);
  12001. if(!mobdb_checkid(class_))
  12002. {
  12003. if (num < 3) //requested a string
  12004. script_pushconststr(st,"");
  12005. else
  12006. script_pushint(st,0);
  12007. return SCRIPT_CMD_SUCCESS;
  12008. }
  12009. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  12010. switch (num) {
  12011. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  12012. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  12013. case 3: script_pushint(st,mob->lv); break;
  12014. case 4: script_pushint(st,mob->status.max_hp); break;
  12015. case 5: script_pushint(st,mob->status.max_sp); break;
  12016. case 6: script_pushint(st,mob->base_exp); break;
  12017. case 7: script_pushint(st,mob->job_exp); break;
  12018. default:
  12019. script_pushint(st,0);
  12020. break;
  12021. }
  12022. return SCRIPT_CMD_SUCCESS;
  12023. }
  12024. /*==========================================
  12025. * Summon guardians [Valaris]
  12026. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  12027. *------------------------------------------*/
  12028. BUILDIN_FUNC(guardian)
  12029. {
  12030. int class_=0,x=0,y=0,guardian=0;
  12031. const char *str,*mapname,*evt="";
  12032. bool has_index = false;
  12033. mapname =script_getstr(st,2);
  12034. x =script_getnum(st,3);
  12035. y =script_getnum(st,4);
  12036. str =script_getstr(st,5);
  12037. class_=script_getnum(st,6);
  12038. if( script_hasdata(st,8) )
  12039. {// "<event label>",<guardian index>
  12040. evt=script_getstr(st,7);
  12041. guardian=script_getnum(st,8);
  12042. has_index = true;
  12043. } else if( script_hasdata(st,7) ){
  12044. if( script_isstring(st, 7) )
  12045. {// "<event label>"
  12046. evt=script_getstr(st,7);
  12047. } else
  12048. {// <guardian index>
  12049. guardian=script_getnum(st,7);
  12050. has_index = true;
  12051. }
  12052. }
  12053. check_event(st, evt);
  12054. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  12055. return SCRIPT_CMD_SUCCESS;
  12056. }
  12057. /*==========================================
  12058. * Invisible Walls [Zephyrus]
  12059. *------------------------------------------*/
  12060. BUILDIN_FUNC(setwall)
  12061. {
  12062. const char *mapname, *name;
  12063. int x, y, m, size, dir;
  12064. bool shootable;
  12065. mapname = script_getstr(st,2);
  12066. x = script_getnum(st,3);
  12067. y = script_getnum(st,4);
  12068. size = script_getnum(st,5);
  12069. dir = script_getnum(st,6);
  12070. shootable = script_getnum(st,7) != 0;
  12071. name = script_getstr(st,8);
  12072. if( (m = map_mapname2mapid(mapname)) < 0 )
  12073. return SCRIPT_CMD_SUCCESS; // Invalid Map
  12074. map_iwall_set(m, x, y, size, dir, shootable, name);
  12075. return SCRIPT_CMD_SUCCESS;
  12076. }
  12077. BUILDIN_FUNC(delwall)
  12078. {
  12079. const char *name = script_getstr(st,2);
  12080. if( !map_iwall_remove(name) ){
  12081. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  12082. return SCRIPT_CMD_FAILURE;
  12083. }
  12084. return SCRIPT_CMD_SUCCESS;
  12085. }
  12086. BUILDIN_FUNC(checkwall)
  12087. {
  12088. const char *wall_name = script_getstr(st, 2);
  12089. script_pushint(st, map_iwall_exist(wall_name));
  12090. return SCRIPT_CMD_SUCCESS;
  12091. }
  12092. /// Retrieves various information about the specified guardian.
  12093. ///
  12094. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  12095. /// type: 0 - whether it is deployed or not
  12096. /// 1 - maximum hp
  12097. /// 2 - current hp
  12098. ///
  12099. BUILDIN_FUNC(guardianinfo)
  12100. {
  12101. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  12102. int id = script_getnum(st,3);
  12103. int type = script_getnum(st,4);
  12104. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  12105. struct mob_data* gd;
  12106. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  12107. {
  12108. script_pushint(st,-1);
  12109. return SCRIPT_CMD_SUCCESS;
  12110. }
  12111. if( type == 0 )
  12112. script_pushint(st, gc->guardian[id].visible);
  12113. else
  12114. if( !gc->guardian[id].visible )
  12115. script_pushint(st,-1);
  12116. else
  12117. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  12118. script_pushint(st,-1);
  12119. else
  12120. {
  12121. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  12122. else if( type == 2 ) script_pushint(st,gd->status.hp);
  12123. else
  12124. script_pushint(st,-1);
  12125. }
  12126. return SCRIPT_CMD_SUCCESS;
  12127. }
  12128. /*==========================================
  12129. * Get the item name by item_id or null
  12130. *------------------------------------------*/
  12131. BUILDIN_FUNC(getitemname)
  12132. {
  12133. std::shared_ptr<item_data> i_data;
  12134. if( script_isstring(st, 2) ){
  12135. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12136. }else{
  12137. i_data = item_db.find( script_getnum( st, 2 ) );
  12138. }
  12139. if( i_data == nullptr ){
  12140. script_pushconststr(st,"null");
  12141. return SCRIPT_CMD_SUCCESS;
  12142. }
  12143. char* item_name = (char *)aMalloc( ITEM_NAME_LENGTH * sizeof( char ) );
  12144. memcpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12145. script_pushstr(st,item_name);
  12146. return SCRIPT_CMD_SUCCESS;
  12147. }
  12148. /*==========================================
  12149. * Returns number of slots an item has. [Skotlex]
  12150. *------------------------------------------*/
  12151. BUILDIN_FUNC(getitemslots)
  12152. {
  12153. struct item_data *i_data;
  12154. t_itemid item_id=script_getnum(st,2);
  12155. i_data = itemdb_exists(item_id);
  12156. if (i_data)
  12157. script_pushint(st,i_data->slots);
  12158. else
  12159. script_pushint(st,-1);
  12160. return SCRIPT_CMD_SUCCESS;
  12161. }
  12162. // TODO: add matk here if needed/once we get rid of RENEWAL
  12163. /*==========================================
  12164. * Returns some values of an item [Lupus]
  12165. * Price, Weight, etc...
  12166. *------------------------------------------*/
  12167. BUILDIN_FUNC(getiteminfo)
  12168. {
  12169. std::shared_ptr<item_data> i_data;
  12170. int type = script_getnum(st, 3);
  12171. if (script_isstring(st, 2))
  12172. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12173. else
  12174. i_data = item_db.find( script_getnum( st, 2 ) );
  12175. if (i_data == nullptr) {
  12176. if (type != ITEMINFO_AEGISNAME)
  12177. script_pushint(st, -1);
  12178. else
  12179. script_pushstrcopy(st, "");
  12180. return SCRIPT_CMD_SUCCESS;
  12181. }
  12182. switch( type ) {
  12183. case ITEMINFO_BUY: script_pushint(st, i_data->value_buy); break;
  12184. case ITEMINFO_SELL: script_pushint(st, i_data->value_sell); break;
  12185. case ITEMINFO_TYPE: script_pushint(st, i_data->type); break;
  12186. case ITEMINFO_MAXCHANCE: script_pushint(st, i_data->maxchance); break;
  12187. case ITEMINFO_GENDER: script_pushint(st, i_data->sex); break;
  12188. case ITEMINFO_LOCATIONS: script_pushint(st, i_data->equip); break;
  12189. case ITEMINFO_WEIGHT: script_pushint(st, i_data->weight); break;
  12190. case ITEMINFO_ATTACK: script_pushint(st, i_data->atk); break;
  12191. case ITEMINFO_DEFENSE: script_pushint(st, i_data->def); break;
  12192. case ITEMINFO_RANGE: script_pushint(st, i_data->range); break;
  12193. case ITEMINFO_SLOT: script_pushint(st, i_data->slots); break;
  12194. case ITEMINFO_VIEW:
  12195. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12196. script_pushint(st, i_data->subtype);
  12197. } else {
  12198. script_pushint(st, i_data->look);
  12199. }
  12200. break;
  12201. case ITEMINFO_EQUIPLEVELMIN: script_pushint(st, i_data->elv); break;
  12202. case ITEMINFO_WEAPONLEVEL:
  12203. if( i_data->type == IT_WEAPON ){
  12204. script_pushint( st, i_data->weapon_level );
  12205. }else{
  12206. script_pushint( st, 0 );
  12207. }
  12208. break;
  12209. case ITEMINFO_ARMORLEVEL:
  12210. if( i_data->type == IT_ARMOR ){
  12211. script_pushint( st, i_data->armor_level );
  12212. }else{
  12213. script_pushint( st, 0 );
  12214. }
  12215. break;
  12216. case ITEMINFO_ALIASNAME: script_pushint(st, i_data->view_id); break;
  12217. case ITEMINFO_EQUIPLEVELMAX: script_pushint(st, i_data->elvmax); break;
  12218. case ITEMINFO_MAGICATTACK: {
  12219. #ifdef RENEWAL
  12220. script_pushint(st, i_data->matk);
  12221. #else
  12222. script_pushint(st, 0);
  12223. #endif
  12224. break;
  12225. }
  12226. case ITEMINFO_ID: script_pushint(st, i_data->nameid); break;
  12227. case ITEMINFO_AEGISNAME: script_pushstrcopy(st, i_data->name.c_str()); break;
  12228. case ITEMINFO_SUBTYPE: script_pushint(st, i_data->subtype); break;
  12229. default:
  12230. script_pushint(st, -1);
  12231. break;
  12232. }
  12233. return SCRIPT_CMD_SUCCESS;
  12234. }
  12235. /*==========================================
  12236. * Set some values of an item [Lupus]
  12237. * Price, Weight, etc...
  12238. *------------------------------------------*/
  12239. BUILDIN_FUNC(setiteminfo)
  12240. {
  12241. std::shared_ptr<item_data> i_data;
  12242. if (script_isstring(st, 2))
  12243. i_data = item_db.search_aegisname( script_getstr( st, 2 ) );
  12244. else
  12245. i_data = item_db.find( script_getnum( st, 2 ) );
  12246. if (i_data == nullptr) {
  12247. script_pushint(st, -1);
  12248. return SCRIPT_CMD_SUCCESS;
  12249. }
  12250. int value = script_getnum(st,4);
  12251. switch( script_getnum(st, 3) ) {
  12252. case ITEMINFO_BUY: i_data->value_buy = static_cast<uint32>(value); break;
  12253. case ITEMINFO_SELL: i_data->value_sell = static_cast<uint32>(value); break;
  12254. case ITEMINFO_TYPE: i_data->type = static_cast<item_types>(value); break;
  12255. case ITEMINFO_MAXCHANCE: i_data->maxchance = static_cast<int>(value); break;
  12256. case ITEMINFO_GENDER: i_data->sex = static_cast<uint8>(value); break;
  12257. case ITEMINFO_LOCATIONS: i_data->equip = static_cast<uint32>(value); break;
  12258. case ITEMINFO_WEIGHT: i_data->weight = static_cast<uint32>(value); break;
  12259. case ITEMINFO_ATTACK: i_data->atk = static_cast<uint32>(value); break;
  12260. case ITEMINFO_DEFENSE: i_data->def = static_cast<uint32>(value); break;
  12261. case ITEMINFO_RANGE: i_data->range = static_cast<uint16>(value); break;
  12262. case ITEMINFO_SLOT: i_data->slots = static_cast<uint16>(value); break;
  12263. case ITEMINFO_VIEW:
  12264. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12265. i_data->subtype = static_cast<uint8>(value);
  12266. } else {
  12267. i_data->look = static_cast<uint32>(value);
  12268. }
  12269. break;
  12270. case ITEMINFO_EQUIPLEVELMIN: i_data->elv = static_cast<uint16>(value); break;
  12271. case ITEMINFO_WEAPONLEVEL:
  12272. if( i_data->type == IT_WEAPON ){
  12273. if( value > MAX_WEAPON_LEVEL ){
  12274. ShowError( "buildin_setiteminfo: weapon level %d is above maximum %d.\n", value, MAX_WEAPON_LEVEL );
  12275. script_pushint( st, -1 );
  12276. return SCRIPT_CMD_FAILURE;
  12277. }
  12278. i_data->weapon_level = static_cast<uint16>(value);
  12279. break;
  12280. }else{
  12281. ShowError( "buildin_setiteminfo: Can not set a weapon level for %d.\n", i_data->nameid );
  12282. script_pushint( st, -1 );
  12283. return SCRIPT_CMD_FAILURE;
  12284. }
  12285. case ITEMINFO_ARMORLEVEL:
  12286. if( i_data->type == IT_ARMOR ){
  12287. if( value > MAX_ARMOR_LEVEL ){
  12288. ShowError( "buildin_setiteminfo: armor level %d is above maximum %d.\n", value, MAX_ARMOR_LEVEL );
  12289. script_pushint( st, -1 );
  12290. return SCRIPT_CMD_FAILURE;
  12291. }
  12292. i_data->armor_level = static_cast<uint16>(value);
  12293. break;
  12294. }else{
  12295. ShowError( "buildin_setiteminfo: Can not set an armor level for %d.\n", i_data->nameid );
  12296. script_pushint( st, -1 );
  12297. return SCRIPT_CMD_FAILURE;
  12298. }
  12299. case ITEMINFO_ALIASNAME: i_data->view_id = static_cast<t_itemid>(value); break;
  12300. case ITEMINFO_EQUIPLEVELMAX: i_data->elvmax = static_cast<uint16>(value); break;
  12301. case ITEMINFO_MAGICATTACK: {
  12302. #ifdef RENEWAL
  12303. i_data->matk = static_cast<uint32>(value);
  12304. #else
  12305. value = 0;
  12306. #endif
  12307. break;
  12308. }
  12309. case ITEMINFO_SUBTYPE: i_data->subtype = static_cast<uint8>(value); break;
  12310. default:
  12311. script_pushint(st, -1);
  12312. break;
  12313. }
  12314. script_pushint(st, value);
  12315. return SCRIPT_CMD_SUCCESS;
  12316. }
  12317. /*==========================================
  12318. * Returns value from equipped item slot n [Lupus]
  12319. getequipcardid(num,slot)
  12320. where
  12321. num = eqip position slot
  12322. slot = 0,1,2,3 (Card Slot N)
  12323. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12324. it's useful when you want to check item cards or if it's signed
  12325. Useful for such quests as "Sign this refined item with players name" etc
  12326. Hat[0] +4 -> Player's Hat[0] +4
  12327. *------------------------------------------*/
  12328. BUILDIN_FUNC(getequipcardid)
  12329. {
  12330. int i=-1,num,slot;
  12331. TBL_PC *sd;
  12332. if( !script_rid2sd(sd) )
  12333. return SCRIPT_CMD_SUCCESS;
  12334. num=script_getnum(st,2);
  12335. slot=script_getnum(st,3);
  12336. if (equip_index_check(num))
  12337. i=pc_checkequip(sd,equip_bitmask[num]);
  12338. if(i >= 0 && slot>=0 && slot<4)
  12339. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12340. else
  12341. script_pushint(st,0);
  12342. return SCRIPT_CMD_SUCCESS;
  12343. }
  12344. /*==========================================
  12345. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12346. *------------------------------------------*/
  12347. BUILDIN_FUNC(petskillbonus)
  12348. {
  12349. struct pet_data *pd;
  12350. TBL_PC *sd;
  12351. if(!script_rid2sd(sd) || sd->pd == NULL)
  12352. return SCRIPT_CMD_FAILURE;
  12353. pd = sd->pd;
  12354. if (pd->bonus)
  12355. { //Clear previous bonus
  12356. if (pd->bonus->timer != INVALID_TIMER)
  12357. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12358. } else //init
  12359. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12360. pd->bonus->type = script_getnum(st,2);
  12361. pd->bonus->val = script_getnum(st,3);
  12362. pd->bonus->duration = script_getnum(st,4);
  12363. pd->bonus->delay = script_getnum(st,5);
  12364. if (pd->state.skillbonus == 1)
  12365. pd->state.skillbonus = 0; // waiting state
  12366. // wait for timer to start
  12367. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12368. pd->bonus->timer = INVALID_TIMER;
  12369. else
  12370. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12371. return SCRIPT_CMD_SUCCESS;
  12372. }
  12373. /*==========================================
  12374. * pet looting [Valaris] //Rewritten by [Skotlex]
  12375. *------------------------------------------*/
  12376. BUILDIN_FUNC(petloot)
  12377. {
  12378. int max;
  12379. struct pet_data *pd;
  12380. TBL_PC *sd;
  12381. if(!script_rid2sd(sd) || sd->pd==NULL)
  12382. return SCRIPT_CMD_SUCCESS;
  12383. max=script_getnum(st,2);
  12384. if(max < 1)
  12385. max = 1; //Let'em loot at least 1 item.
  12386. else if (max > MAX_PETLOOT_SIZE)
  12387. max = MAX_PETLOOT_SIZE;
  12388. pd = sd->pd;
  12389. if (pd->loot != NULL)
  12390. { //Release whatever was there already and reallocate memory
  12391. pet_lootitem_drop(pd, pd->master);
  12392. aFree(pd->loot->item);
  12393. }
  12394. else
  12395. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12396. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12397. pd->loot->max=max;
  12398. pd->loot->count = 0;
  12399. pd->loot->weight = 0;
  12400. return SCRIPT_CMD_SUCCESS;
  12401. }
  12402. /*==========================================
  12403. * Set arrays with info of all sd inventory :
  12404. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12405. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12406. * @inventorylist_card(0..3), @inventorylist_expire
  12407. * @inventorylist_count = scalar
  12408. *------------------------------------------*/
  12409. BUILDIN_FUNC(getinventorylist)
  12410. {
  12411. TBL_PC *sd;
  12412. char card_var[NAME_LENGTH], randopt_var[50];
  12413. int i,j=0,k;
  12414. if (!script_charid2sd(2,sd))
  12415. return SCRIPT_CMD_FAILURE;
  12416. for(i=0;i<MAX_INVENTORY;i++){
  12417. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12418. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12419. pc_setreg(sd,reference_uid(add_str("@inventorylist_idx"), j),i);
  12420. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12421. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12422. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12423. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12424. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12425. for (k = 0; k < MAX_SLOTS; k++)
  12426. {
  12427. sprintf(card_var, "@inventorylist_card%d",k+1);
  12428. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12429. }
  12430. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12431. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12432. pc_setreg(sd,reference_uid(add_str("@inventorylist_enchantgrade"), j),sd->inventory.u.items_inventory[i].enchantgrade);
  12433. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12434. {
  12435. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12436. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12437. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12438. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12439. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12440. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12441. }
  12442. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12443. pc_setreg(sd,reference_uid(add_str("@inventorylist_favorite"), j),sd->inventory.u.items_inventory[i].favorite);
  12444. j++;
  12445. }
  12446. }
  12447. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12448. return SCRIPT_CMD_SUCCESS;
  12449. }
  12450. /**
  12451. * getskilllist ({<char_id>});
  12452. **/
  12453. BUILDIN_FUNC(getskilllist)
  12454. {
  12455. TBL_PC *sd;
  12456. int i,j=0;
  12457. if (!script_charid2sd(2,sd))
  12458. return SCRIPT_CMD_FAILURE;
  12459. for(i=0;i<MAX_SKILL;i++){
  12460. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12461. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12462. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12463. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12464. j++;
  12465. }
  12466. }
  12467. pc_setreg(sd,add_str("@skilllist_count"),j);
  12468. return SCRIPT_CMD_SUCCESS;
  12469. }
  12470. /**
  12471. * clearitem ({<char_id>});
  12472. **/
  12473. BUILDIN_FUNC(clearitem)
  12474. {
  12475. TBL_PC *sd;
  12476. int i;
  12477. if (!script_charid2sd(2,sd))
  12478. return SCRIPT_CMD_FAILURE;
  12479. for (i=0; i<MAX_INVENTORY; i++) {
  12480. if (sd->inventory.u.items_inventory[i].amount) {
  12481. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12482. }
  12483. }
  12484. return SCRIPT_CMD_SUCCESS;
  12485. }
  12486. /**
  12487. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12488. * disguise <Monster ID>{,<char_id>};
  12489. **/
  12490. BUILDIN_FUNC(disguise)
  12491. {
  12492. int id;
  12493. TBL_PC* sd;
  12494. if (!script_charid2sd(3,sd))
  12495. return SCRIPT_CMD_FAILURE;
  12496. id = script_getnum(st,2);
  12497. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12498. pc_disguise(sd, id);
  12499. script_pushint(st,id);
  12500. } else
  12501. script_pushint(st,0);
  12502. return SCRIPT_CMD_SUCCESS;
  12503. }
  12504. /**
  12505. * Undisguise Player (returns 1 if success, 0 on fail)
  12506. * undisguise {<char_id>};
  12507. **/
  12508. BUILDIN_FUNC(undisguise)
  12509. {
  12510. TBL_PC* sd;
  12511. if (!script_charid2sd(2,sd))
  12512. return SCRIPT_CMD_FAILURE;
  12513. if (sd->disguise) {
  12514. pc_disguise(sd, 0);
  12515. script_pushint(st,0);
  12516. } else {
  12517. script_pushint(st,1);
  12518. }
  12519. return SCRIPT_CMD_SUCCESS;
  12520. }
  12521. /**
  12522. * Transform an NPC to another _class
  12523. *
  12524. * classchange(<view id>{,"<NPC name>","<flag>"});
  12525. * @param flag: Specify target
  12526. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12527. * BC_SELF - Sprite is sent only to player attached.
  12528. */
  12529. BUILDIN_FUNC(classchange)
  12530. {
  12531. int _class, type = 1;
  12532. struct npc_data* nd = NULL;
  12533. TBL_PC *sd = map_id2sd(st->rid);
  12534. send_target target = AREA;
  12535. _class = script_getnum(st,2);
  12536. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12537. nd = npc_name2id(script_getstr(st, 3));
  12538. else
  12539. nd = (struct npc_data *)map_id2bl(st->oid);
  12540. if (nd == NULL)
  12541. return SCRIPT_CMD_FAILURE;
  12542. if (script_hasdata(st, 4)) {
  12543. switch(script_getnum(st, 4)) {
  12544. case BC_SELF: target = SELF; break;
  12545. case BC_AREA:
  12546. default: target = AREA; break;
  12547. }
  12548. }
  12549. if (target != SELF)
  12550. clif_class_change(&nd->bl,_class,type);
  12551. else if (sd == NULL)
  12552. return SCRIPT_CMD_FAILURE;
  12553. else
  12554. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12555. return SCRIPT_CMD_SUCCESS;
  12556. }
  12557. /*==========================================
  12558. * Display an effect
  12559. *------------------------------------------*/
  12560. BUILDIN_FUNC(misceffect)
  12561. {
  12562. int type;
  12563. type=script_getnum(st,2);
  12564. if( type <= EF_NONE || type >= EF_MAX ){
  12565. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12566. return SCRIPT_CMD_FAILURE;
  12567. }
  12568. if(st->oid && st->oid != fake_nd->bl.id) {
  12569. struct block_list *bl = map_id2bl(st->oid);
  12570. if (bl)
  12571. clif_specialeffect(bl,type,AREA);
  12572. } else{
  12573. TBL_PC *sd;
  12574. if(script_rid2sd(sd))
  12575. clif_specialeffect(&sd->bl,type,AREA);
  12576. }
  12577. return SCRIPT_CMD_SUCCESS;
  12578. }
  12579. /*==========================================
  12580. * Play a BGM on a single client [Rikter/Yommy]
  12581. *------------------------------------------*/
  12582. BUILDIN_FUNC(playBGM)
  12583. {
  12584. struct map_session_data* sd;
  12585. if( script_rid2sd(sd) ) {
  12586. clif_playBGM(sd, script_getstr(st,2));
  12587. }
  12588. return SCRIPT_CMD_SUCCESS;
  12589. }
  12590. static int playBGM_sub(struct block_list* bl,va_list ap)
  12591. {
  12592. const char* name = va_arg(ap,const char*);
  12593. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12594. return 0;
  12595. }
  12596. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12597. {
  12598. const char* name = va_arg(args, const char*);
  12599. clif_playBGM(sd, name);
  12600. return 0;
  12601. }
  12602. /*==========================================
  12603. * Play a BGM on multiple client [Rikter/Yommy]
  12604. *------------------------------------------*/
  12605. BUILDIN_FUNC(playBGMall)
  12606. {
  12607. const char* name;
  12608. name = script_getstr(st,2);
  12609. if( script_hasdata(st,7) ) {// specified part of map
  12610. const char* mapname = script_getstr(st,3);
  12611. int x0 = script_getnum(st,4);
  12612. int y0 = script_getnum(st,5);
  12613. int x1 = script_getnum(st,6);
  12614. int y1 = script_getnum(st,7);
  12615. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12616. }
  12617. else if( script_hasdata(st,3) ) {// entire map
  12618. const char* mapname = script_getstr(st,3);
  12619. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12620. }
  12621. else {// entire server
  12622. map_foreachpc(&playBGM_foreachpc_sub, name);
  12623. }
  12624. return SCRIPT_CMD_SUCCESS;
  12625. }
  12626. /*==========================================
  12627. * Play a .wav sound for sd
  12628. *------------------------------------------*/
  12629. BUILDIN_FUNC(soundeffect)
  12630. {
  12631. TBL_PC* sd;
  12632. if(script_rid2sd(sd)){
  12633. const char* name = script_getstr(st,2);
  12634. int type = script_getnum(st,3);
  12635. clif_soundeffect(sd,&sd->bl,name,type);
  12636. }
  12637. return SCRIPT_CMD_SUCCESS;
  12638. }
  12639. int soundeffect_sub(struct block_list* bl,va_list ap)
  12640. {
  12641. char* name = va_arg(ap,char*);
  12642. int type = va_arg(ap,int);
  12643. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12644. return 0;
  12645. }
  12646. /*==========================================
  12647. * Play a sound effect (.wav) on multiple clients
  12648. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12649. *------------------------------------------*/
  12650. BUILDIN_FUNC(soundeffectall)
  12651. {
  12652. struct block_list* bl;
  12653. struct map_session_data* sd;
  12654. const char* name;
  12655. int type;
  12656. if( st->rid && script_rid2sd(sd) )
  12657. bl = &sd->bl;
  12658. else
  12659. bl = map_id2bl(st->oid);
  12660. if (!bl)
  12661. return SCRIPT_CMD_SUCCESS;
  12662. name = script_getstr(st,2);
  12663. type = script_getnum(st,3);
  12664. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12665. if(!script_hasdata(st,4))
  12666. { // area around
  12667. clif_soundeffectall(bl, name, type, AREA);
  12668. }
  12669. else
  12670. if(!script_hasdata(st,5))
  12671. { // entire map
  12672. const char* mapname = script_getstr(st,4);
  12673. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12674. }
  12675. else
  12676. if(script_hasdata(st,8))
  12677. { // specified part of map
  12678. const char* mapname = script_getstr(st,4);
  12679. int x0 = script_getnum(st,5);
  12680. int y0 = script_getnum(st,6);
  12681. int x1 = script_getnum(st,7);
  12682. int y1 = script_getnum(st,8);
  12683. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12684. }
  12685. else
  12686. {
  12687. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12688. }
  12689. return SCRIPT_CMD_SUCCESS;
  12690. }
  12691. /*==========================================
  12692. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12693. *------------------------------------------*/
  12694. BUILDIN_FUNC(petrecovery)
  12695. {
  12696. struct pet_data *pd;
  12697. TBL_PC *sd;
  12698. int sc;
  12699. if(!script_rid2sd(sd) || sd->pd == NULL)
  12700. return SCRIPT_CMD_FAILURE;
  12701. sc = script_getnum(st,2);
  12702. if (sc <= SC_NONE || sc >= SC_MAX) {
  12703. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12704. return SCRIPT_CMD_FAILURE;
  12705. }
  12706. pd = sd->pd;
  12707. if (pd->recovery)
  12708. { //Halt previous bonus
  12709. if (pd->recovery->timer != INVALID_TIMER)
  12710. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12711. } else //Init
  12712. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12713. pd->recovery->type = (sc_type)sc;
  12714. pd->recovery->delay = script_getnum(st,3);
  12715. pd->recovery->timer = INVALID_TIMER;
  12716. return SCRIPT_CMD_SUCCESS;
  12717. }
  12718. /*==========================================
  12719. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12720. *------------------------------------------*/
  12721. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12722. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12723. BUILDIN_FUNC(petskillattack)
  12724. {
  12725. struct pet_data *pd;
  12726. TBL_PC *sd;
  12727. int id = 0;
  12728. if(!script_rid2sd(sd) || sd->pd == NULL)
  12729. return SCRIPT_CMD_FAILURE;
  12730. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12731. if (!id) {
  12732. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12733. return SCRIPT_CMD_FAILURE;
  12734. }
  12735. pd = sd->pd;
  12736. if (pd->a_skill == NULL)
  12737. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12738. pd->a_skill->id = id;
  12739. pd->a_skill->damage = 0;
  12740. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12741. pd->a_skill->div_ = 0;
  12742. pd->a_skill->rate = script_getnum(st,4);
  12743. pd->a_skill->bonusrate = script_getnum(st,5);
  12744. return SCRIPT_CMD_SUCCESS;
  12745. }
  12746. /*==========================================
  12747. * pet attack skills [Valaris]
  12748. *------------------------------------------*/
  12749. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12750. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12751. BUILDIN_FUNC(petskillattack2)
  12752. {
  12753. struct pet_data *pd;
  12754. TBL_PC *sd;
  12755. int id = 0;
  12756. if(!script_rid2sd(sd) || sd->pd == NULL)
  12757. return SCRIPT_CMD_FAILURE;
  12758. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12759. if (!id) {
  12760. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12761. return SCRIPT_CMD_FAILURE;
  12762. }
  12763. pd = sd->pd;
  12764. if (pd->a_skill == NULL)
  12765. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12766. pd->a_skill->id = id;
  12767. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12768. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12769. pd->a_skill->div_ = script_getnum(st,4);
  12770. pd->a_skill->rate = script_getnum(st,5);
  12771. pd->a_skill->bonusrate = script_getnum(st,6);
  12772. return SCRIPT_CMD_SUCCESS;
  12773. }
  12774. /*==========================================
  12775. * pet support skills [Skotlex]
  12776. *------------------------------------------*/
  12777. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12778. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12779. BUILDIN_FUNC(petskillsupport)
  12780. {
  12781. struct pet_data *pd;
  12782. TBL_PC *sd;
  12783. int id = 0;
  12784. if(!script_rid2sd(sd) || sd->pd == NULL)
  12785. return SCRIPT_CMD_FAILURE;
  12786. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12787. if (!id) {
  12788. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12789. return SCRIPT_CMD_FAILURE;
  12790. }
  12791. pd = sd->pd;
  12792. if (pd->s_skill)
  12793. { //Clear previous skill
  12794. if (pd->s_skill->timer != INVALID_TIMER)
  12795. {
  12796. if (pd->s_skill->id)
  12797. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12798. else
  12799. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12800. }
  12801. } else //init memory
  12802. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12803. pd->s_skill->id = id;
  12804. pd->s_skill->lv = script_getnum(st,3);
  12805. pd->s_skill->delay = script_getnum(st,4);
  12806. pd->s_skill->hp = script_getnum(st,5);
  12807. pd->s_skill->sp = script_getnum(st,6);
  12808. //Use delay as initial offset to avoid skill/heal exploits
  12809. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12810. pd->s_skill->timer = INVALID_TIMER;
  12811. else
  12812. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12813. return SCRIPT_CMD_SUCCESS;
  12814. }
  12815. static inline void script_skill_effect(block_list *bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y) {
  12816. nullpo_retv(bl);
  12817. switch (skill_get_casttype(skill_id)) {
  12818. case CAST_GROUND:
  12819. clif_skill_poseffect(bl, skill_id, skill_lv, x, y, gettick());
  12820. break;
  12821. case CAST_NODAMAGE:
  12822. clif_skill_nodamage(bl, bl, skill_id, skill_lv, 1);
  12823. break;
  12824. case CAST_DAMAGE:
  12825. 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));
  12826. break;
  12827. }
  12828. }
  12829. /*==========================================
  12830. * Scripted skill effects [Celest]
  12831. *------------------------------------------*/
  12832. /// skilleffect <skill id>,<level>
  12833. /// skilleffect "<skill name>",<level>
  12834. BUILDIN_FUNC(skilleffect)
  12835. {
  12836. TBL_PC *sd;
  12837. uint16 skill_id, skill_lv;
  12838. if( !script_rid2sd(sd) )
  12839. return SCRIPT_CMD_FAILURE;
  12840. skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12841. skill_lv = script_getnum(st,3);
  12842. if (skill_db.find(skill_id) == nullptr) {
  12843. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12844. return SCRIPT_CMD_FAILURE;
  12845. }
  12846. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12847. * which leaves the server thinking it still is sitting. */
  12848. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12849. skill_sit(sd, 0);
  12850. clif_standing(&sd->bl);
  12851. }
  12852. script_skill_effect(&sd->bl, skill_id, skill_lv, sd->bl.x, sd->bl.y);
  12853. return SCRIPT_CMD_SUCCESS;
  12854. }
  12855. /*==========================================
  12856. * NPC skill effects [Valaris]
  12857. *------------------------------------------*/
  12858. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12859. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12860. BUILDIN_FUNC(npcskilleffect)
  12861. {
  12862. struct block_list *bl= map_id2bl(st->oid);
  12863. if (bl == nullptr) {
  12864. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  12865. return SCRIPT_CMD_FAILURE;
  12866. }
  12867. uint16 skill_id, skill_lv;
  12868. int16 x, y;
  12869. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12870. skill_lv=script_getnum(st,3);
  12871. x=script_getnum(st,4);
  12872. y=script_getnum(st,5);
  12873. if (skill_db.find(skill_id) == nullptr) {
  12874. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12875. return SCRIPT_CMD_FAILURE;
  12876. }
  12877. script_skill_effect(bl, skill_id, skill_lv, bl->x, bl->y);
  12878. return SCRIPT_CMD_SUCCESS;
  12879. }
  12880. /*==========================================
  12881. * Special effects [Valaris]
  12882. *------------------------------------------*/
  12883. BUILDIN_FUNC(specialeffect)
  12884. {
  12885. struct block_list *bl=map_id2bl(st->oid);
  12886. int type = script_getnum(st,2);
  12887. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12888. if(bl==NULL)
  12889. return SCRIPT_CMD_SUCCESS;
  12890. if( type <= EF_NONE || type >= EF_MAX ){
  12891. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  12892. return SCRIPT_CMD_FAILURE;
  12893. }
  12894. if( script_hasdata(st,4) )
  12895. {
  12896. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12897. if(nd)
  12898. clif_specialeffect(&nd->bl, type, target);
  12899. }
  12900. else
  12901. {
  12902. if (target == SELF) {
  12903. TBL_PC *sd;
  12904. if (script_rid2sd(sd))
  12905. clif_specialeffect_single(bl,type,sd->fd);
  12906. } else {
  12907. clif_specialeffect(bl, type, target);
  12908. }
  12909. }
  12910. return SCRIPT_CMD_SUCCESS;
  12911. }
  12912. BUILDIN_FUNC(specialeffect2)
  12913. {
  12914. TBL_PC *sd;
  12915. if( script_nick2sd(4,sd) ){
  12916. int type = script_getnum(st,2);
  12917. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12918. if( type <= EF_NONE || type >= EF_MAX ){
  12919. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  12920. return SCRIPT_CMD_FAILURE;
  12921. }
  12922. clif_specialeffect(&sd->bl, type, target);
  12923. }
  12924. return SCRIPT_CMD_SUCCESS;
  12925. }
  12926. BUILDIN_FUNC(removespecialeffect)
  12927. {
  12928. const char* command = script_getfuncname(st);
  12929. #if PACKETVER < 20181002
  12930. ShowError("buildin_%s: This command is not supported for PACKETVER older than 2018-10-02.\n", command);
  12931. return SCRIPT_CMD_FAILURE;
  12932. #endif
  12933. int effect = script_getnum(st, 2);
  12934. if (effect <= EF_NONE || effect >= EF_MAX) {
  12935. ShowError("buildin_%s: unsupported effect id %d.\n", command, effect);
  12936. return SCRIPT_CMD_FAILURE;
  12937. }
  12938. send_target e_target = script_hasdata(st, 3) ? static_cast<send_target>(script_getnum(st, 3)) : AREA;
  12939. struct block_list *bl_src;
  12940. struct block_list *bl_target;
  12941. struct map_session_data *sd;
  12942. if( strcmp(command, "removespecialeffect") == 0 ) {
  12943. if (!script_hasdata(st, 4)) {
  12944. bl_src = map_id2bl(st->oid);
  12945. if (bl_src == nullptr) {
  12946. ShowError("buildin_%s: npc not attached.\n", command);
  12947. return SCRIPT_CMD_FAILURE;
  12948. }
  12949. }
  12950. else {
  12951. struct npc_data *nd = npc_name2id(script_getstr(st, 4));
  12952. if (nd == nullptr) {
  12953. ShowError("buildin_%s: unknown npc %s.\n", command, script_getstr(st, 4));
  12954. return SCRIPT_CMD_FAILURE;
  12955. }
  12956. bl_src = &nd->bl;
  12957. }
  12958. if (e_target != SELF)
  12959. bl_target = bl_src;
  12960. else {
  12961. if (!script_rid2sd(sd))
  12962. return SCRIPT_CMD_FAILURE;
  12963. bl_target = &sd->bl;
  12964. }
  12965. }else{
  12966. if (!script_nick2sd(4, sd))
  12967. return SCRIPT_CMD_FAILURE;
  12968. bl_src = bl_target = &sd->bl;
  12969. }
  12970. clif_specialeffect_remove(bl_src, effect, e_target, bl_target);
  12971. return SCRIPT_CMD_SUCCESS;
  12972. }
  12973. /**
  12974. * nude({<char_id>});
  12975. * @author [Valaris]
  12976. **/
  12977. BUILDIN_FUNC(nude)
  12978. {
  12979. TBL_PC *sd;
  12980. int i, calcflag = 0;
  12981. if (!script_charid2sd(2,sd))
  12982. return SCRIPT_CMD_FAILURE;
  12983. for( i = 0 ; i < EQI_MAX; i++ ) {
  12984. if( sd->equip_index[ i ] >= 0 ) {
  12985. if( !calcflag )
  12986. calcflag = 1;
  12987. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12988. }
  12989. }
  12990. if( calcflag )
  12991. status_calc_pc(sd,SCO_NONE);
  12992. return SCRIPT_CMD_SUCCESS;
  12993. }
  12994. int atcommand_sub(struct script_state* st,int type) {
  12995. TBL_PC *sd, dummy_sd;
  12996. int fd;
  12997. const char *cmd;
  12998. cmd = script_getstr(st,2);
  12999. if (st->rid) {
  13000. if( !script_rid2sd(sd) )
  13001. return SCRIPT_CMD_SUCCESS;
  13002. fd = sd->fd;
  13003. } else { //Use a dummy character.
  13004. sd = &dummy_sd;
  13005. fd = 0;
  13006. memset(&dummy_sd, 0, sizeof(TBL_PC));
  13007. if (st->oid) {
  13008. struct block_list* bl = map_id2bl(st->oid);
  13009. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  13010. if (bl->type == BL_NPC)
  13011. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  13012. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  13013. }
  13014. // Init Group ID, Level, & permissions
  13015. sd->group_id = sd->group_level = 99;
  13016. sd->permissions |= PC_PERM_ALLPERMISSION;
  13017. }
  13018. if (!is_atcommand(fd, sd, cmd, type)) {
  13019. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  13020. script_reportsrc(st);
  13021. return SCRIPT_CMD_FAILURE;
  13022. }
  13023. return SCRIPT_CMD_SUCCESS;
  13024. }
  13025. /*==========================================
  13026. * gmcommand [MouseJstr]
  13027. *------------------------------------------*/
  13028. BUILDIN_FUNC(atcommand) {
  13029. return atcommand_sub(st,0);
  13030. }
  13031. /** Displays a message for the player only (like system messages like "you got an apple" )
  13032. * dispbottom("<message>"{,<color>{,<char_id>}})
  13033. * @param message
  13034. * @param color Hex color default (Green)
  13035. */
  13036. BUILDIN_FUNC(dispbottom)
  13037. {
  13038. TBL_PC *sd;
  13039. int color = 0;
  13040. const char *message;
  13041. if (!script_charid2sd(4,sd))
  13042. return SCRIPT_CMD_FAILURE;
  13043. message = script_getstr(st,2);
  13044. if (script_hasdata(st,3))
  13045. color = script_getnum(st,3); // <color>
  13046. if(sd) {
  13047. if (script_hasdata(st,3))
  13048. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  13049. else
  13050. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  13051. }
  13052. return SCRIPT_CMD_SUCCESS;
  13053. }
  13054. /*===================================
  13055. * Heal portion of recovery command
  13056. *-----------------------------------*/
  13057. int recovery_sub(struct map_session_data* sd, int revive)
  13058. {
  13059. if(revive&(1|4) && pc_isdead(sd)) {
  13060. status_revive(&sd->bl, 100, 100);
  13061. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  13062. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  13063. } else if(revive&(1|2) && !pc_isdead(sd)) {
  13064. status_percent_heal(&sd->bl, 100, 100);
  13065. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  13066. }
  13067. return SCRIPT_CMD_SUCCESS;
  13068. }
  13069. /*=========================================================================
  13070. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  13071. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  13072. * <type> determines <option>:
  13073. * 0 : char_id
  13074. * 1 : party_id
  13075. * 2 : guild_id
  13076. * 3 : map_name
  13077. * 4 : all characters
  13078. * <revive_flag>:
  13079. * 1 : Revive and heal all players (default)
  13080. * 2 : Heal living players only
  13081. * 4 : Revive dead players only
  13082. * <map name>:
  13083. * for types 1-2 : map_name (null = all maps)
  13084. *-------------------------------------------------------------------------*/
  13085. BUILDIN_FUNC(recovery)
  13086. {
  13087. TBL_PC *sd;
  13088. int map_idx = 0, type = 0, revive = 1;
  13089. type = script_getnum(st,2);
  13090. if(script_hasdata(st,4))
  13091. revive = script_getnum(st,4);
  13092. switch(type) {
  13093. case 0:
  13094. if(!script_charid2sd(3,sd))
  13095. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  13096. recovery_sub(sd, revive);
  13097. break;
  13098. case 1:
  13099. {
  13100. struct party_data* p;
  13101. //When no party given, we use invoker party
  13102. int p_id = 0, i;
  13103. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13104. map_idx = map_mapname2mapid(script_getstr(st,5));
  13105. if(map_idx < 1) { //But we'll check anyways
  13106. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13107. return SCRIPT_CMD_FAILURE;
  13108. }
  13109. }
  13110. if(script_hasdata(st,3))
  13111. p_id = script_getnum(st,3);
  13112. else if(script_rid2sd(sd))
  13113. p_id = sd->status.party_id;
  13114. p = party_search(p_id);
  13115. if(p == NULL)
  13116. return SCRIPT_CMD_SUCCESS;
  13117. for (i = 0; i < MAX_PARTY; i++) {
  13118. struct map_session_data* pl_sd;
  13119. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  13120. || (map_idx && pl_sd->bl.m != map_idx))
  13121. continue;
  13122. recovery_sub(pl_sd, revive);
  13123. }
  13124. break;
  13125. }
  13126. case 2:
  13127. {
  13128. struct guild* g;
  13129. //When no guild given, we use invoker guild
  13130. int g_id = 0, i;
  13131. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13132. map_idx = map_mapname2mapid(script_getstr(st,5));
  13133. if(map_idx < 1) { //But we'll check anyways
  13134. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13135. return SCRIPT_CMD_FAILURE;
  13136. }
  13137. }
  13138. if(script_hasdata(st,3))
  13139. g_id = script_getnum(st,3);
  13140. else if(script_rid2sd(sd))
  13141. g_id = sd->status.guild_id;
  13142. g = guild_search(g_id);
  13143. if(g == NULL)
  13144. return SCRIPT_CMD_SUCCESS;
  13145. for (i = 0; i < MAX_GUILD; i++) {
  13146. struct map_session_data* pl_sd;
  13147. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  13148. || (map_idx && pl_sd->bl.m != map_idx))
  13149. continue;
  13150. recovery_sub(pl_sd, revive);
  13151. }
  13152. break;
  13153. }
  13154. case 3:
  13155. if(script_hasdata(st,3))
  13156. map_idx = map_mapname2mapid(script_getstr(st,3));
  13157. else if(script_rid2sd(sd))
  13158. map_idx = sd->bl.m;
  13159. if(map_idx < 1)
  13160. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  13161. case 4:
  13162. {
  13163. struct s_mapiterator *iter;
  13164. if(script_hasdata(st,3) && script_isint(st, 3))
  13165. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  13166. iter = mapit_getallusers();
  13167. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  13168. if(type == 3 && sd->bl.m != map_idx)
  13169. continue;
  13170. recovery_sub(sd, revive);
  13171. }
  13172. mapit_free(iter);
  13173. break;
  13174. }
  13175. default:
  13176. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  13177. script_pushint(st,-1);
  13178. return SCRIPT_CMD_FAILURE;
  13179. }
  13180. script_pushint(st,1); //Successfully executed without errors
  13181. return SCRIPT_CMD_SUCCESS;
  13182. }
  13183. /*==========================================
  13184. * Get your pet info: getpetinfo <type>{,<char_id>}
  13185. * n -> 0:pet_id 1:pet_class 2:pet_name
  13186. * 3:friendly 4:hungry, 5: rename flag.6:level,
  13187. * 7:game id
  13188. *------------------------------------------*/
  13189. BUILDIN_FUNC(getpetinfo)
  13190. {
  13191. TBL_PC *sd;
  13192. TBL_PET *pd;
  13193. int type = script_getnum(st,2);
  13194. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  13195. if (type == 2)
  13196. script_pushconststr(st,"null");
  13197. else
  13198. script_pushint(st,0);
  13199. return SCRIPT_CMD_SUCCESS;
  13200. }
  13201. switch(type){
  13202. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13203. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13204. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13205. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13206. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13207. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13208. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13209. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13210. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13211. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13212. default:
  13213. script_pushint(st,0);
  13214. break;
  13215. }
  13216. return SCRIPT_CMD_SUCCESS;
  13217. }
  13218. /*==========================================
  13219. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13220. * type -> 0:hom_id 1:class 2:name
  13221. * 3:friendly 4:hungry, 5: rename flag.
  13222. * 6: level, 7: game id
  13223. *------------------------------------------*/
  13224. BUILDIN_FUNC(gethominfo)
  13225. {
  13226. TBL_PC *sd;
  13227. TBL_HOM *hd;
  13228. int type=script_getnum(st,2);
  13229. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13230. if (type == 2)
  13231. script_pushconststr(st,"null");
  13232. else
  13233. script_pushint(st,0);
  13234. return SCRIPT_CMD_SUCCESS;
  13235. }
  13236. switch(type){
  13237. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13238. case 1: script_pushint(st,hd->homunculus.class_); break;
  13239. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13240. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13241. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13242. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13243. case 6: script_pushint(st,hd->homunculus.level); break;
  13244. case 7: script_pushint(st,hd->bl.id); break;
  13245. default:
  13246. script_pushint(st,0);
  13247. break;
  13248. }
  13249. return SCRIPT_CMD_SUCCESS;
  13250. }
  13251. BUILDIN_FUNC(addhomintimacy)
  13252. {
  13253. map_session_data *sd;
  13254. homun_data *hd;
  13255. if (!script_charid2sd(3, sd) || !(hd = sd->hd))
  13256. return SCRIPT_CMD_FAILURE;
  13257. int32 value = script_getnum(st, 2);
  13258. if (value == 0) // Nothing to change
  13259. return SCRIPT_CMD_SUCCESS;
  13260. if (value > 0)
  13261. hom_increase_intimacy(hd, (uint32)value);
  13262. else
  13263. hom_decrease_intimacy(hd, (uint32)abs(value));
  13264. clif_send_homdata(sd, SP_INTIMATE, hd->homunculus.intimacy / 100);
  13265. return SCRIPT_CMD_SUCCESS;
  13266. }
  13267. /// Retrieves information about character's mercenary
  13268. /// getmercinfo <type>[,<char id>];
  13269. BUILDIN_FUNC(getmercinfo)
  13270. {
  13271. int type;
  13272. struct map_session_data* sd;
  13273. s_mercenary_data* md;
  13274. if( !script_charid2sd(3,sd) ){
  13275. script_pushnil(st);
  13276. return SCRIPT_CMD_FAILURE;
  13277. }
  13278. type = script_getnum(st,2);
  13279. md = sd->md;
  13280. if( md == NULL ){
  13281. if( type == 2 )
  13282. script_pushconststr(st,"");
  13283. else
  13284. script_pushint(st,0);
  13285. return SCRIPT_CMD_SUCCESS;
  13286. }
  13287. switch( type )
  13288. {
  13289. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13290. case 1: script_pushint(st,md->mercenary.class_); break;
  13291. case 2: script_pushstrcopy(st,md->db->name.c_str()); break;
  13292. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13293. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13294. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13295. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13296. case 7: script_pushint(st,md->db->lv); break;
  13297. case 8: script_pushint(st,md->bl.id); break;
  13298. default:
  13299. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13300. script_pushnil(st);
  13301. return SCRIPT_CMD_FAILURE;
  13302. }
  13303. return SCRIPT_CMD_SUCCESS;
  13304. }
  13305. /*==========================================
  13306. * Shows wether your inventory(and equips) contain
  13307. selected card or not.
  13308. checkequipedcard(4001);
  13309. *------------------------------------------*/
  13310. BUILDIN_FUNC(checkequipedcard)
  13311. {
  13312. TBL_PC *sd;
  13313. if(script_rid2sd(sd)){
  13314. int n,i,c=0;
  13315. c=script_getnum(st,2);
  13316. for(i=0;i<MAX_INVENTORY;i++){
  13317. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13318. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13319. continue;
  13320. for(n=0;n<sd->inventory_data[i]->slots;n++){
  13321. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13322. script_pushint(st,1);
  13323. return SCRIPT_CMD_SUCCESS;
  13324. }
  13325. }
  13326. }
  13327. }
  13328. }
  13329. script_pushint(st,0);
  13330. return SCRIPT_CMD_SUCCESS;
  13331. }
  13332. BUILDIN_FUNC(jump_zero)
  13333. {
  13334. int64 sel=script_getnum64(st,2);
  13335. if(!sel) {
  13336. int pos;
  13337. if( !data_islabel(script_getdata(st,3)) ){
  13338. ShowError("script: jump_zero: not label !\n");
  13339. st->state=END;
  13340. return SCRIPT_CMD_FAILURE;
  13341. }
  13342. pos=script_getnum(st,3);
  13343. st->pos=pos;
  13344. st->state=GOTO;
  13345. }
  13346. return SCRIPT_CMD_SUCCESS;
  13347. }
  13348. /*==========================================
  13349. * movenpc [MouseJstr]
  13350. *------------------------------------------*/
  13351. BUILDIN_FUNC(movenpc)
  13352. {
  13353. TBL_NPC *nd = NULL;
  13354. const char *npc;
  13355. int x,y;
  13356. npc = script_getstr(st,2);
  13357. x = script_getnum(st,3);
  13358. y = script_getnum(st,4);
  13359. if ((nd = npc_name2id(npc)) == NULL){
  13360. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13361. return -1;
  13362. }
  13363. if (script_hasdata(st,5))
  13364. nd->ud.dir = script_getnum(st,5) % 8;
  13365. npc_movenpc(nd, x, y);
  13366. return SCRIPT_CMD_SUCCESS;
  13367. }
  13368. /*==========================================
  13369. * message [MouseJstr]
  13370. *------------------------------------------*/
  13371. BUILDIN_FUNC(message)
  13372. {
  13373. const char *msg,*player;
  13374. TBL_PC *pl_sd = NULL;
  13375. player = script_getstr(st,2);
  13376. msg = script_getstr(st,3);
  13377. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13378. return SCRIPT_CMD_SUCCESS;
  13379. clif_displaymessage(pl_sd->fd, msg);
  13380. return SCRIPT_CMD_SUCCESS;
  13381. }
  13382. /**
  13383. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13384. * @param flag: Specify target
  13385. * BC_ALL - Broadcast message is sent server-wide.
  13386. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13387. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13388. * BC_SELF - Message is sent only to player attached.
  13389. */
  13390. BUILDIN_FUNC(npctalk)
  13391. {
  13392. struct npc_data* nd = NULL;
  13393. const char* str = script_getstr(st,2);
  13394. int color = 0xFFFFFF;
  13395. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13396. nd = npc_name2id(script_getstr(st, 3));
  13397. else
  13398. nd = (struct npc_data *)map_id2bl(st->oid);
  13399. if (script_hasdata(st, 5))
  13400. color = script_getnum(st, 5);
  13401. if (nd != NULL) {
  13402. send_target target = AREA;
  13403. char message[CHAT_SIZE_MAX];
  13404. if (script_hasdata(st, 4)) {
  13405. switch(script_getnum(st, 4)) {
  13406. case BC_ALL: target = ALL_CLIENT; break;
  13407. case BC_MAP: target = ALL_SAMEMAP; break;
  13408. case BC_SELF: target = SELF; break;
  13409. case BC_AREA:
  13410. default: target = AREA; break;
  13411. }
  13412. }
  13413. safesnprintf(message, sizeof(message), "%s", str);
  13414. if (target != SELF)
  13415. clif_messagecolor(&nd->bl, color, message, true, target);
  13416. else {
  13417. TBL_PC *sd = map_id2sd(st->rid);
  13418. if (sd == NULL)
  13419. return SCRIPT_CMD_FAILURE;
  13420. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13421. }
  13422. }
  13423. return SCRIPT_CMD_SUCCESS;
  13424. }
  13425. /**
  13426. * Sends a message to the waitingroom of the invoking NPC.
  13427. * chatmes "<message>"{,"<NPC name>"};
  13428. * @author Jey
  13429. */
  13430. BUILDIN_FUNC(chatmes)
  13431. {
  13432. struct npc_data* nd = NULL;
  13433. const char* str = script_getstr(st,2);
  13434. if (script_hasdata(st, 3))
  13435. nd = npc_name2id(script_getstr(st, 3));
  13436. else
  13437. nd = (struct npc_data *)map_id2bl(st->oid);
  13438. if (nd != NULL && nd->chat_id) {
  13439. char message[256];
  13440. safesnprintf(message, sizeof(message), "%s", str);
  13441. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13442. }
  13443. return SCRIPT_CMD_SUCCESS;
  13444. }
  13445. // change npc walkspeed [Valaris]
  13446. BUILDIN_FUNC(npcspeed)
  13447. {
  13448. struct npc_data* nd;
  13449. int speed;
  13450. speed = script_getnum(st,2);
  13451. nd =(struct npc_data *)map_id2bl(st->oid);
  13452. if( nd ) {
  13453. nd->speed = speed;
  13454. nd->ud.state.speed_changed = 1;
  13455. }
  13456. return SCRIPT_CMD_SUCCESS;
  13457. }
  13458. // make an npc walk to a position [Valaris]
  13459. BUILDIN_FUNC(npcwalkto)
  13460. {
  13461. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13462. int x=0,y=0;
  13463. x=script_getnum(st,2);
  13464. y=script_getnum(st,3);
  13465. if(nd) {
  13466. if (!nd->status.hp)
  13467. status_calc_npc(nd, SCO_FIRST);
  13468. else
  13469. status_calc_npc(nd, SCO_NONE);
  13470. unit_walktoxy(&nd->bl,x,y,0);
  13471. }
  13472. return SCRIPT_CMD_SUCCESS;
  13473. }
  13474. // stop an npc's movement [Valaris]
  13475. BUILDIN_FUNC(npcstop)
  13476. {
  13477. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13478. if(nd) {
  13479. unit_stop_walking(&nd->bl,1|4);
  13480. }
  13481. return SCRIPT_CMD_SUCCESS;
  13482. }
  13483. /**
  13484. * getlook(<type>{,<char_id>})
  13485. **/
  13486. BUILDIN_FUNC(getlook)
  13487. {
  13488. int type,val;
  13489. TBL_PC *sd;
  13490. if (!script_charid2sd(3,sd)) {
  13491. script_pushint(st,-1);
  13492. return SCRIPT_CMD_FAILURE;
  13493. }
  13494. type=script_getnum(st,2);
  13495. val=-1;
  13496. switch(type) {
  13497. // TODO: implement LOOK_BASE as stated in script doc
  13498. case LOOK_HAIR: val=sd->status.hair; break; //1
  13499. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13500. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13501. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13502. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13503. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13504. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13505. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13506. case LOOK_SHOES: break; //9
  13507. case LOOK_ROBE: val=sd->status.robe; break; //12
  13508. case LOOK_BODY2: val=sd->status.body; break; //13
  13509. }
  13510. script_pushint(st,val);
  13511. return SCRIPT_CMD_SUCCESS;
  13512. }
  13513. /**
  13514. * getsavepoint(<information type>{,<char_id>})
  13515. * @param type 0- map name, 1- x, 2- y
  13516. **/
  13517. BUILDIN_FUNC(getsavepoint)
  13518. {
  13519. TBL_PC* sd;
  13520. int type;
  13521. if (!script_charid2sd(3,sd)) {
  13522. script_pushint(st,0);
  13523. return SCRIPT_CMD_SUCCESS;
  13524. }
  13525. type = script_getnum(st,2);
  13526. switch(type) {
  13527. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13528. case 1: script_pushint(st,sd->status.save_point.x); break;
  13529. case 2: script_pushint(st,sd->status.save_point.y); break;
  13530. default:
  13531. script_pushint(st,0);
  13532. break;
  13533. }
  13534. return SCRIPT_CMD_SUCCESS;
  13535. }
  13536. /**
  13537. * Get position for BL objects.
  13538. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13539. * @param mapname: String variable for output map name
  13540. * @param x: Integer variable for output coord X
  13541. * @param y: Integer variable for output coord Y
  13542. * @param type: Type of object
  13543. * BL_PC - Character coord
  13544. * BL_NPC - NPC coord
  13545. * BL_PET - Pet coord
  13546. * BL_HOM - Homun coord
  13547. * BL_MER - Mercenary coord
  13548. * BL_ELEM - Elemental coord
  13549. * @param charname: Name object. If empty or "this" use the current object
  13550. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13551. */
  13552. BUILDIN_FUNC(getmapxy)
  13553. {
  13554. struct block_list *bl = NULL;
  13555. TBL_PC *sd=NULL;
  13556. int64 num;
  13557. const char *name;
  13558. char prefix;
  13559. int x,y,type = BL_PC;
  13560. char mapname[MAP_NAME_LENGTH];
  13561. if( !data_isreference(script_getdata(st,2)) ) {
  13562. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13563. script_pushint(st,-1);
  13564. return SCRIPT_CMD_FAILURE;
  13565. }
  13566. if( !data_isreference(script_getdata(st,3)) ) {
  13567. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13568. script_pushint(st,-1);
  13569. return SCRIPT_CMD_FAILURE;
  13570. }
  13571. if( !data_isreference(script_getdata(st,4)) ) {
  13572. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13573. script_pushint(st,-1);
  13574. return SCRIPT_CMD_FAILURE;
  13575. }
  13576. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13577. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13578. script_pushint(st,-1);
  13579. return SCRIPT_CMD_FAILURE;
  13580. }
  13581. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13582. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13583. script_pushint(st,-1);
  13584. return SCRIPT_CMD_FAILURE;
  13585. }
  13586. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13587. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13588. script_pushint(st,-1);
  13589. return SCRIPT_CMD_FAILURE;
  13590. }
  13591. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13592. if (script_hasdata(st, 5))
  13593. type = script_getnum(st, 5);
  13594. switch (type) {
  13595. case BL_PC: //Get Character Position
  13596. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  13597. bl = &sd->bl;
  13598. break;
  13599. case BL_NPC: //Get NPC Position
  13600. if (script_hasdata(st, 6)) {
  13601. struct npc_data *nd;
  13602. if (script_isstring(st, 6))
  13603. nd = npc_name2id(script_getstr(st, 6));
  13604. else
  13605. nd = map_id2nd(script_getnum(st, 6));
  13606. if (nd)
  13607. bl = &nd->bl;
  13608. } else //In case the origin is not an NPC?
  13609. bl = map_id2bl(st->oid);
  13610. break;
  13611. case BL_PET: //Get Pet Position
  13612. if (sd->pd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13613. bl = &sd->pd->bl;
  13614. break;
  13615. case BL_HOM: //Get Homun Position
  13616. if (sd->hd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13617. bl = &sd->hd->bl;
  13618. break;
  13619. case BL_MER: //Get Mercenary Position
  13620. if (sd->md && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13621. bl = &sd->md->bl;
  13622. break;
  13623. case BL_ELEM: //Get Elemental Position
  13624. if (sd->ed && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13625. bl = &sd->ed->bl;
  13626. break;
  13627. default:
  13628. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13629. script_pushint(st,-1);
  13630. return SCRIPT_CMD_FAILURE;
  13631. }
  13632. if (!bl) { //No object found.
  13633. script_pushint(st,-1);
  13634. return SCRIPT_CMD_SUCCESS;
  13635. }
  13636. x= bl->x;
  13637. y= bl->y;
  13638. if (bl->m >= 0)
  13639. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  13640. else
  13641. memset(mapname, '\0', sizeof(mapname));
  13642. //Set MapName$
  13643. num=st->stack->stack_data[st->start+2].u.num;
  13644. name=get_str(script_getvarid(num));
  13645. prefix=*name;
  13646. if(not_server_variable(prefix)){
  13647. if( !script_rid2sd(sd) ){
  13648. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13649. return SCRIPT_CMD_FAILURE;
  13650. }
  13651. }else
  13652. sd=NULL;
  13653. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  13654. //Set MapX
  13655. num=st->stack->stack_data[st->start+3].u.num;
  13656. name=get_str(script_getvarid(num));
  13657. prefix=*name;
  13658. if(not_server_variable(prefix)){
  13659. if( !script_rid2sd(sd) ){
  13660. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13661. return SCRIPT_CMD_FAILURE;
  13662. }
  13663. }else
  13664. sd=NULL;
  13665. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  13666. //Set MapY
  13667. num=st->stack->stack_data[st->start+4].u.num;
  13668. name=get_str(script_getvarid(num));
  13669. prefix=*name;
  13670. if(not_server_variable(prefix)){
  13671. if( !script_rid2sd(sd) ){
  13672. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13673. return SCRIPT_CMD_FAILURE;
  13674. }
  13675. }else
  13676. sd=NULL;
  13677. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  13678. //Return Success value
  13679. script_pushint(st,0);
  13680. return SCRIPT_CMD_SUCCESS;
  13681. }
  13682. /// Returns the map name of given map ID.
  13683. ///
  13684. /// mapid2name <map ID>;
  13685. BUILDIN_FUNC(mapid2name)
  13686. {
  13687. uint16 m = script_getnum(st, 2);
  13688. if (m < 0) {
  13689. script_pushconststr(st, "");
  13690. return SCRIPT_CMD_FAILURE;
  13691. }
  13692. script_pushstrcopy(st, map_mapid2mapname(m));
  13693. return SCRIPT_CMD_SUCCESS;
  13694. }
  13695. /*==========================================
  13696. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13697. *------------------------------------------*/
  13698. BUILDIN_FUNC(logmes)
  13699. {
  13700. const char *str;
  13701. struct map_session_data* sd = map_id2sd(st->rid);
  13702. str = script_getstr(st,2);
  13703. if( sd ){
  13704. log_npc(sd,str);
  13705. }else{
  13706. struct npc_data* nd = map_id2nd(st->oid);
  13707. if( !nd ){
  13708. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  13709. return SCRIPT_CMD_FAILURE;
  13710. }
  13711. log_npc(nd,str);
  13712. }
  13713. return SCRIPT_CMD_SUCCESS;
  13714. }
  13715. BUILDIN_FUNC(summon)
  13716. {
  13717. int _class, timeout=0;
  13718. const char *str,*event="";
  13719. TBL_PC *sd;
  13720. struct mob_data *md;
  13721. t_tick tick = gettick();
  13722. if (!script_rid2sd(sd))
  13723. return SCRIPT_CMD_SUCCESS;
  13724. str =script_getstr(st,2);
  13725. _class=script_getnum(st,3);
  13726. if( script_hasdata(st,4) )
  13727. timeout=script_getnum(st,4);
  13728. if( script_hasdata(st,5) ){
  13729. event=script_getstr(st,5);
  13730. check_event(st, event);
  13731. }
  13732. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13733. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13734. if (md) {
  13735. md->master_id=sd->bl.id;
  13736. md->special_state.ai = AI_ATTACK;
  13737. if( md->deletetimer != INVALID_TIMER )
  13738. delete_timer(md->deletetimer, mob_timer_delete);
  13739. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13740. mob_spawn (md); //Now it is ready for spawning.
  13741. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  13742. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13743. }
  13744. script_pushint(st, md->bl.id);
  13745. return SCRIPT_CMD_SUCCESS;
  13746. }
  13747. /*==========================================
  13748. * Checks whether it is daytime/nighttime
  13749. *------------------------------------------*/
  13750. BUILDIN_FUNC(isnight)
  13751. {
  13752. script_pushint(st,(night_flag == 1));
  13753. return SCRIPT_CMD_SUCCESS;
  13754. }
  13755. BUILDIN_FUNC(isday)
  13756. {
  13757. script_pushint(st,(night_flag == 0));
  13758. return SCRIPT_CMD_SUCCESS;
  13759. }
  13760. /*================================================
  13761. * Check how many items/cards in the list are
  13762. * equipped - used for 2/15's cards patch [celest]
  13763. *------------------------------------------------*/
  13764. BUILDIN_FUNC(isequippedcnt)
  13765. {
  13766. TBL_PC *sd;
  13767. if (!script_rid2sd(sd)) {
  13768. script_pushint(st,0);
  13769. return SCRIPT_CMD_SUCCESS;
  13770. }
  13771. int ret = 0;
  13772. int total = script_lastdata(st);
  13773. std::vector<int32> list(total);
  13774. for (int i = 2; i <= total; ++i) {
  13775. int id = script_getnum(st,i);
  13776. if (id <= 0)
  13777. continue;
  13778. if (std::find(list.begin(), list.end(), id) != list.end())
  13779. continue;
  13780. list.push_back(id);
  13781. for (short j = 0; j < EQI_MAX; j++) {
  13782. short index = sd->equip_index[j];
  13783. if (index < 0)
  13784. continue;
  13785. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13786. continue;
  13787. if (!sd->inventory_data[index])
  13788. continue;
  13789. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13790. if (sd->inventory_data[index]->nameid == id)
  13791. ret += sd->inventory.u.items_inventory[index].amount;
  13792. } else { //Count cards.
  13793. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13794. continue; //No cards
  13795. for (short k = 0; k < sd->inventory_data[index]->slots; k++) {
  13796. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13797. ret++; //[Lupus]
  13798. }
  13799. }
  13800. }
  13801. }
  13802. script_pushint(st,ret);
  13803. return SCRIPT_CMD_SUCCESS;
  13804. }
  13805. /*================================================
  13806. * Check whether another card has been
  13807. * equipped - used for 2/15's cards patch [celest]
  13808. * -- Items checked cannot be reused in another
  13809. * card set to prevent exploits
  13810. *------------------------------------------------*/
  13811. BUILDIN_FUNC(isequipped)
  13812. {
  13813. TBL_PC *sd;
  13814. int i, id = 1;
  13815. int ret = -1;
  13816. //Original hash to reverse it when full check fails.
  13817. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13818. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13819. script_pushint(st,0);
  13820. return SCRIPT_CMD_SUCCESS;
  13821. }
  13822. setitem_hash = sd->bonus.setitem_hash;
  13823. setitem_hash2 = sd->bonus.setitem_hash2;
  13824. for (i=0; id!=0; i++) {
  13825. int flag = 0;
  13826. short j;
  13827. FETCH (i+2, id) else id = 0;
  13828. if (id <= 0)
  13829. continue;
  13830. for (j=0; j<EQI_MAX; j++) {
  13831. short index = sd->equip_index[j];
  13832. if(index < 0)
  13833. continue;
  13834. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13835. continue;
  13836. if(!sd->inventory_data[index])
  13837. continue;
  13838. if (itemdb_type(id) != IT_CARD) {
  13839. if (sd->inventory_data[index]->nameid != id)
  13840. continue;
  13841. flag = 1;
  13842. break;
  13843. } else { //Cards
  13844. short k;
  13845. if (sd->inventory_data[index]->slots == 0 ||
  13846. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13847. continue;
  13848. for (k = 0; k < sd->inventory_data[index]->slots; k++)
  13849. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13850. unsigned int hash = 0;
  13851. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13852. continue;
  13853. hash = 1<<((j<5?j:j-5)*4 + k);
  13854. // check if card is already used by another set
  13855. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13856. continue;
  13857. // We have found a match
  13858. flag = 1;
  13859. // Set hash so this card cannot be used by another
  13860. if (j<5)
  13861. sd->bonus.setitem_hash |= hash;
  13862. else
  13863. sd->bonus.setitem_hash2 |= hash;
  13864. break;
  13865. }
  13866. }
  13867. if (flag) break; //Card found
  13868. }
  13869. if (ret == -1)
  13870. ret = flag;
  13871. else
  13872. ret &= flag;
  13873. if (!ret) break;
  13874. }
  13875. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13876. sd->bonus.setitem_hash = setitem_hash;
  13877. sd->bonus.setitem_hash2 = setitem_hash2;
  13878. }
  13879. script_pushint(st,ret);
  13880. return SCRIPT_CMD_SUCCESS;
  13881. }
  13882. /*================================================
  13883. * Check how many given inserted cards in the CURRENT
  13884. * weapon - used for 2/15's cards patch [Lupus]
  13885. *------------------------------------------------*/
  13886. BUILDIN_FUNC(cardscnt)
  13887. {
  13888. TBL_PC *sd;
  13889. int i, k, id = 1;
  13890. int ret = 0;
  13891. int index;
  13892. if( !script_rid2sd(sd) ){
  13893. script_pushint(st,0);
  13894. return SCRIPT_CMD_SUCCESS;
  13895. }
  13896. for (i=0; id!=0; i++) {
  13897. FETCH (i+2, id) else id = 0;
  13898. if (id <= 0)
  13899. continue;
  13900. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  13901. if(index < 0) continue;
  13902. if(!sd->inventory_data[index])
  13903. continue;
  13904. if(itemdb_type(id) != IT_CARD) {
  13905. if (sd->inventory_data[index]->nameid == id)
  13906. ret+= sd->inventory.u.items_inventory[index].amount;
  13907. } else {
  13908. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13909. continue;
  13910. for(k=0; k<sd->inventory_data[index]->slots; k++) {
  13911. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13912. ret++;
  13913. }
  13914. }
  13915. }
  13916. script_pushint(st,ret);
  13917. // script_pushint(st,current_equip_item_index);
  13918. return SCRIPT_CMD_SUCCESS;
  13919. }
  13920. /*=======================================================
  13921. * Returns the refined number of the current item, or an
  13922. * item with inventory index specified
  13923. *-------------------------------------------------------*/
  13924. BUILDIN_FUNC(getrefine)
  13925. {
  13926. TBL_PC *sd;
  13927. if (script_rid2sd(sd)){
  13928. if( current_equip_item_index == -1 ){
  13929. script_pushint(st, 0);
  13930. return SCRIPT_CMD_FAILURE;
  13931. }
  13932. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13933. }else
  13934. script_pushint(st,0);
  13935. return SCRIPT_CMD_SUCCESS;
  13936. }
  13937. /*=======================================================
  13938. * Day/Night controls
  13939. *-------------------------------------------------------*/
  13940. BUILDIN_FUNC(night)
  13941. {
  13942. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13943. return SCRIPT_CMD_SUCCESS;
  13944. }
  13945. BUILDIN_FUNC(day)
  13946. {
  13947. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13948. return SCRIPT_CMD_SUCCESS;
  13949. }
  13950. /**
  13951. * unequip <equipment slot>{,<char_id>};
  13952. * @author [Spectre]
  13953. **/
  13954. BUILDIN_FUNC(unequip) {
  13955. int pos;
  13956. TBL_PC *sd;
  13957. if (!script_charid2sd(3,sd))
  13958. return SCRIPT_CMD_FAILURE;
  13959. pos = script_getnum(st,2);
  13960. if (equip_index_check(pos)) {
  13961. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13962. if (i >= 0) {
  13963. pc_unequipitem(sd,i,1|2);
  13964. script_pushint(st, 1);
  13965. return SCRIPT_CMD_SUCCESS;
  13966. }
  13967. }
  13968. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13969. script_pushint(st, 0);
  13970. return SCRIPT_CMD_FAILURE;
  13971. }
  13972. /**
  13973. * equip <item id>{,<char_id>};
  13974. **/
  13975. BUILDIN_FUNC(equip) {
  13976. TBL_PC *sd;
  13977. struct item_data *item_data;
  13978. if (!script_charid2sd(3,sd))
  13979. return SCRIPT_CMD_FAILURE;
  13980. t_itemid nameid = script_getnum(st,2);
  13981. if ((item_data = itemdb_exists(nameid))) {
  13982. int i;
  13983. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13984. if (i < MAX_INVENTORY) {
  13985. pc_equipitem(sd,i,item_data->equip);
  13986. script_pushint(st,1);
  13987. return SCRIPT_CMD_SUCCESS;
  13988. }
  13989. }
  13990. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  13991. script_pushint(st,0);
  13992. return SCRIPT_CMD_FAILURE;
  13993. }
  13994. BUILDIN_FUNC(autoequip)
  13995. {
  13996. int flag;
  13997. struct item_data *item_data;
  13998. t_itemid nameid=script_getnum(st,2);
  13999. flag=script_getnum(st,3);
  14000. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  14001. {
  14002. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  14003. return SCRIPT_CMD_FAILURE;
  14004. }
  14005. if( !itemdb_isequip2(item_data) )
  14006. {
  14007. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  14008. return SCRIPT_CMD_FAILURE;
  14009. }
  14010. item_data->flag.autoequip = flag>0?1:0;
  14011. return SCRIPT_CMD_SUCCESS;
  14012. }
  14013. /**
  14014. * Set the value of a given battle config
  14015. * setbattleflag "<battle flag>",<value>{,<reload>};
  14016. */
  14017. BUILDIN_FUNC(setbattleflag)
  14018. {
  14019. const char *flag, *value;
  14020. flag = script_getstr(st,2);
  14021. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  14022. if (battle_set_value(flag, value) == 0)
  14023. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  14024. else {
  14025. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  14026. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  14027. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  14028. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  14029. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  14030. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  14031. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  14032. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  14033. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  14034. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  14035. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  14036. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  14037. };
  14038. uint8 i;
  14039. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  14040. if (!strcmpi(flag, expdrop_flags[i])) {
  14041. mob_reload();
  14042. break;
  14043. }
  14044. }
  14045. }
  14046. }
  14047. return SCRIPT_CMD_SUCCESS;
  14048. }
  14049. /**
  14050. * Get the value of a given battle config
  14051. * getbattleflag("<battle flag>")
  14052. */
  14053. BUILDIN_FUNC(getbattleflag)
  14054. {
  14055. const char *flag = script_getstr(st,2);
  14056. script_pushint(st,battle_get_value(flag));
  14057. return SCRIPT_CMD_SUCCESS;
  14058. }
  14059. //=======================================================
  14060. // strlen [Valaris]
  14061. //-------------------------------------------------------
  14062. BUILDIN_FUNC(getstrlen)
  14063. {
  14064. const char *str = script_getstr(st,2);
  14065. int len = (str) ? (int)strlen(str) : 0;
  14066. script_pushint(st,len);
  14067. return SCRIPT_CMD_SUCCESS;
  14068. }
  14069. //=======================================================
  14070. // isalpha [Valaris]
  14071. //-------------------------------------------------------
  14072. BUILDIN_FUNC(charisalpha)
  14073. {
  14074. const char *str=script_getstr(st,2);
  14075. int pos=script_getnum(st,3);
  14076. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  14077. script_pushint(st,val);
  14078. return SCRIPT_CMD_SUCCESS;
  14079. }
  14080. //=======================================================
  14081. // charisupper <str>, <index>
  14082. //-------------------------------------------------------
  14083. BUILDIN_FUNC(charisupper)
  14084. {
  14085. const char *str = script_getstr(st,2);
  14086. int pos = script_getnum(st,3);
  14087. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  14088. script_pushint(st,val);
  14089. return SCRIPT_CMD_SUCCESS;
  14090. }
  14091. //=======================================================
  14092. // charislower <str>, <index>
  14093. //-------------------------------------------------------
  14094. BUILDIN_FUNC(charislower)
  14095. {
  14096. const char *str = script_getstr(st,2);
  14097. int pos = script_getnum(st,3);
  14098. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  14099. script_pushint(st,val);
  14100. return SCRIPT_CMD_SUCCESS;
  14101. }
  14102. //=======================================================
  14103. // charat <str>, <index>
  14104. //-------------------------------------------------------
  14105. BUILDIN_FUNC(charat) {
  14106. const char *str = script_getstr(st,2);
  14107. int pos = script_getnum(st,3);
  14108. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  14109. char output[2];
  14110. output[0] = str[pos];
  14111. output[1] = '\0';
  14112. script_pushstrcopy(st, output);
  14113. } else
  14114. script_pushconststr(st, "");
  14115. return SCRIPT_CMD_SUCCESS;
  14116. }
  14117. //=======================================================
  14118. // setchar <string>, <char>, <index>
  14119. //-------------------------------------------------------
  14120. BUILDIN_FUNC(setchar)
  14121. {
  14122. const char *str = script_getstr(st,2);
  14123. const char *c = script_getstr(st,3);
  14124. int index = script_getnum(st,4);
  14125. char *output = aStrdup(str);
  14126. if(index >= 0 && index < strlen(output))
  14127. output[index] = *c;
  14128. script_pushstr(st, output);
  14129. return SCRIPT_CMD_SUCCESS;
  14130. }
  14131. //=======================================================
  14132. // insertchar <string>, <char>, <index>
  14133. //-------------------------------------------------------
  14134. BUILDIN_FUNC(insertchar)
  14135. {
  14136. const char *str = script_getstr(st,2);
  14137. const char *c = script_getstr(st,3);
  14138. int index = script_getnum(st,4);
  14139. char *output;
  14140. size_t len = strlen(str);
  14141. if(index < 0)
  14142. index = 0;
  14143. else if(index > len)
  14144. index = len;
  14145. output = (char*)aMalloc(len + 2);
  14146. memcpy(output, str, index);
  14147. output[index] = c[0];
  14148. memcpy(&output[index+1], &str[index], len - index);
  14149. output[len+1] = '\0';
  14150. script_pushstr(st, output);
  14151. return SCRIPT_CMD_SUCCESS;
  14152. }
  14153. //=======================================================
  14154. // delchar <string>, <index>
  14155. //-------------------------------------------------------
  14156. BUILDIN_FUNC(delchar)
  14157. {
  14158. const char *str = script_getstr(st,2);
  14159. int index = script_getnum(st,3);
  14160. char *output;
  14161. size_t len = strlen(str);
  14162. if(index < 0 || index > len) {
  14163. //return original
  14164. output = aStrdup(str);
  14165. script_pushstr(st, output);
  14166. return SCRIPT_CMD_SUCCESS;
  14167. }
  14168. output = (char*)aMalloc(len);
  14169. memcpy(output, str, index);
  14170. memcpy(&output[index], &str[index+1], len - index);
  14171. script_pushstr(st, output);
  14172. return SCRIPT_CMD_SUCCESS;
  14173. }
  14174. //=======================================================
  14175. // strtoupper <str>
  14176. //-------------------------------------------------------
  14177. BUILDIN_FUNC(strtoupper)
  14178. {
  14179. const char *str = script_getstr(st,2);
  14180. char *output = aStrdup(str);
  14181. char *cursor = output;
  14182. while (*cursor != '\0') {
  14183. *cursor = TOUPPER(*cursor);
  14184. cursor++;
  14185. }
  14186. script_pushstr(st, output);
  14187. return SCRIPT_CMD_SUCCESS;
  14188. }
  14189. //=======================================================
  14190. // strtolower <str>
  14191. //-------------------------------------------------------
  14192. BUILDIN_FUNC(strtolower)
  14193. {
  14194. const char *str = script_getstr(st,2);
  14195. char *output = aStrdup(str);
  14196. char *cursor = output;
  14197. while (*cursor != '\0') {
  14198. *cursor = TOLOWER(*cursor);
  14199. cursor++;
  14200. }
  14201. script_pushstr(st, output);
  14202. return SCRIPT_CMD_SUCCESS;
  14203. }
  14204. //=======================================================
  14205. // substr <str>, <start>, <end>
  14206. //-------------------------------------------------------
  14207. BUILDIN_FUNC(substr)
  14208. {
  14209. const char *str = script_getstr(st,2);
  14210. char *output;
  14211. int start = script_getnum(st,3);
  14212. int end = script_getnum(st,4);
  14213. int len = 0;
  14214. if(start >= 0 && end < strlen(str) && start <= end) {
  14215. len = end - start + 1;
  14216. output = (char*)aMalloc(len + 1);
  14217. memcpy(output, &str[start], len);
  14218. } else
  14219. output = (char*)aMalloc(1);
  14220. output[len] = '\0';
  14221. script_pushstr(st, output);
  14222. return SCRIPT_CMD_SUCCESS;
  14223. }
  14224. //=======================================================
  14225. // explode <dest_string_array>, <str>, <delimiter>
  14226. // Note: delimiter is limited to 1 char
  14227. //-------------------------------------------------------
  14228. BUILDIN_FUNC(explode)
  14229. {
  14230. struct script_data* data = script_getdata(st, 2);
  14231. const char *str = script_getstr(st,3);
  14232. const char delimiter = script_getstr(st, 4)[0];
  14233. int32 id;
  14234. size_t len = strlen(str);
  14235. int i = 0, j = 0;
  14236. int start;
  14237. char *temp;
  14238. const char* name;
  14239. TBL_PC* sd = NULL;
  14240. temp = (char*)aMalloc(len + 1);
  14241. if( !data_isreference(data) ) {
  14242. ShowError("script:explode: not a variable\n");
  14243. script_reportdata(data);
  14244. st->state = END;
  14245. return SCRIPT_CMD_FAILURE;// not a variable
  14246. }
  14247. id = reference_getid(data);
  14248. start = reference_getindex(data);
  14249. name = reference_getname(data);
  14250. if( !is_string_variable(name) ) {
  14251. ShowError("script:explode: not string array\n");
  14252. script_reportdata(data);
  14253. st->state = END;
  14254. return SCRIPT_CMD_FAILURE;// data type mismatch
  14255. }
  14256. if( not_server_variable(*name) ) {
  14257. if( !script_rid2sd(sd) )
  14258. return SCRIPT_CMD_SUCCESS;// no player attached
  14259. }
  14260. while(str[i] != '\0') {
  14261. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14262. temp[j] = '\0';
  14263. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14264. j = 0;
  14265. ++i;
  14266. } else {
  14267. temp[j++] = str[i++];
  14268. }
  14269. }
  14270. //set last string
  14271. temp[j] = '\0';
  14272. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14273. aFree(temp);
  14274. return SCRIPT_CMD_SUCCESS;
  14275. }
  14276. //=======================================================
  14277. // implode <string_array>
  14278. // implode <string_array>, <glue>
  14279. //-------------------------------------------------------
  14280. BUILDIN_FUNC(implode)
  14281. {
  14282. struct script_data* data = script_getdata(st, 2);
  14283. const char *name;
  14284. uint32 glue_len = 0, array_size, id;
  14285. char *output;
  14286. TBL_PC* sd = NULL;
  14287. if( !data_isreference(data) ) {
  14288. ShowError("script:implode: not a variable\n");
  14289. script_reportdata(data);
  14290. st->state = END;
  14291. return SCRIPT_CMD_FAILURE;// not a variable
  14292. }
  14293. id = reference_getid(data);
  14294. name = reference_getname(data);
  14295. if( !is_string_variable(name) ) {
  14296. ShowError("script:implode: not string array\n");
  14297. script_reportdata(data);
  14298. st->state = END;
  14299. return SCRIPT_CMD_FAILURE;// data type mismatch
  14300. }
  14301. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14302. return SCRIPT_CMD_SUCCESS;// no player attached
  14303. }
  14304. //count chars
  14305. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  14306. if(array_size == -1) { //empty array check (AmsTaff)
  14307. ShowWarning("script:implode: array length = 0\n");
  14308. output = (char*)aMalloc(sizeof(char)*5);
  14309. sprintf(output,"%s","NULL");
  14310. } else {
  14311. const char *glue = NULL, *temp;
  14312. size_t len = 0;
  14313. int i, k = 0;
  14314. for(i = 0; i <= array_size; ++i) {
  14315. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14316. len += strlen(temp);
  14317. // Remove stack entry from get_val2_str
  14318. script_removetop( st, -1, 0 );
  14319. }
  14320. //allocate mem
  14321. if( script_hasdata(st,3) ) {
  14322. glue = script_getstr(st,3);
  14323. glue_len = strlen(glue);
  14324. len += glue_len * (array_size);
  14325. }
  14326. output = (char*)aMalloc(len + 1);
  14327. //build output
  14328. for(i = 0; i < array_size; ++i) {
  14329. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14330. len = strlen(temp);
  14331. memcpy(&output[k], temp, len);
  14332. k += len;
  14333. // Remove stack entry from get_val2_str
  14334. script_removetop( st, -1, 0 );
  14335. if(glue_len != 0) {
  14336. memcpy(&output[k], glue, glue_len);
  14337. k += glue_len;
  14338. }
  14339. }
  14340. temp = get_val2_str( st, reference_uid( id, array_size ), reference_getref( data ) );
  14341. len = strlen(temp);
  14342. memcpy(&output[k], temp, len);
  14343. k += len;
  14344. output[k] = '\0';
  14345. // Remove stack entry from get_val2_str
  14346. script_removetop( st, -1, 0 );
  14347. }
  14348. script_pushstr(st, output);
  14349. return SCRIPT_CMD_SUCCESS;
  14350. }
  14351. //=======================================================
  14352. // sprintf(<format>, ...);
  14353. // Implements C sprintf, except format %n. The resulting string is
  14354. // returned, instead of being saved in variable by reference.
  14355. //-------------------------------------------------------
  14356. BUILDIN_FUNC(sprintf)
  14357. {
  14358. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14359. const char* format;
  14360. char* p;
  14361. char* q;
  14362. char* buf = NULL;
  14363. char* buf2 = NULL;
  14364. struct script_data* data;
  14365. StringBuf final_buf;
  14366. // Fetch init data
  14367. format = script_getstr(st, 2);
  14368. argc = script_lastdata(st)-2;
  14369. len = strlen(format);
  14370. // Skip parsing, where no parsing is required.
  14371. if(len == 0) {
  14372. script_pushconststr(st,"");
  14373. return SCRIPT_CMD_SUCCESS;
  14374. }
  14375. // Pessimistic alloc
  14376. CREATE(buf, char, len+1);
  14377. // Need not be parsed, just solve stuff like %%.
  14378. if(argc == 0) {
  14379. memcpy(buf,format,len+1);
  14380. script_pushstrcopy(st, buf);
  14381. aFree(buf);
  14382. return SCRIPT_CMD_SUCCESS;
  14383. }
  14384. safestrncpy(buf, format, len+1);
  14385. // Issue sprintf for each parameter
  14386. StringBuf_Init(&final_buf);
  14387. q = buf;
  14388. while((p = strchr(q, '%')) != NULL) {
  14389. if(p != q) {
  14390. len = p - q + 1;
  14391. if(buf2_len < len) {
  14392. RECREATE(buf2, char, len);
  14393. buf2_len = len;
  14394. }
  14395. safestrncpy(buf2, q, len);
  14396. StringBuf_AppendStr(&final_buf, buf2);
  14397. q = p;
  14398. }
  14399. p = q + 1;
  14400. if(*p == '%') { // %%
  14401. StringBuf_AppendStr(&final_buf, "%");
  14402. q+=2;
  14403. continue;
  14404. }
  14405. if(*p == 'n') { // %n
  14406. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14407. script_reportsrc(st);
  14408. q+=2;
  14409. continue;
  14410. }
  14411. if(arg >= argc) {
  14412. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14413. if(buf) aFree(buf);
  14414. if(buf2) aFree(buf2);
  14415. StringBuf_Destroy(&final_buf);
  14416. script_pushconststr(st,"");
  14417. return SCRIPT_CMD_FAILURE;
  14418. }
  14419. if((p = strchr(q+1, '%')) == NULL)
  14420. p = strchr(q, 0); // EOS
  14421. len = p - q + 1;
  14422. if(buf2_len < len) {
  14423. RECREATE(buf2, char, len);
  14424. buf2_len = len;
  14425. }
  14426. safestrncpy(buf2, q, len);
  14427. q = p;
  14428. // Note: This assumes the passed value being the correct
  14429. // type to the current format specifier. If not, the server
  14430. // probably crashes or returns anything else, than expected,
  14431. // but it would behave in normal code the same way so it's
  14432. // the scripter's responsibility.
  14433. data = script_getdata(st, arg+3);
  14434. if(data_isstring(data)) // String
  14435. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14436. else if(data_isint(data)) // Number
  14437. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14438. else if(data_isreference(data)) { // Variable
  14439. char* name = reference_getname(data);
  14440. if(name[strlen(name)-1]=='$') // var Str
  14441. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14442. else // var Int
  14443. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14444. } else { // Unsupported type
  14445. ShowError("buildin_sprintf: Unknown argument type!\n");
  14446. if(buf) aFree(buf);
  14447. if(buf2) aFree(buf2);
  14448. StringBuf_Destroy(&final_buf);
  14449. script_pushconststr(st,"");
  14450. return SCRIPT_CMD_FAILURE;
  14451. }
  14452. arg++;
  14453. }
  14454. // Append anything left
  14455. if(*q)
  14456. StringBuf_AppendStr(&final_buf, q);
  14457. // Passed more, than needed
  14458. if(arg < argc) {
  14459. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14460. script_reportsrc(st);
  14461. }
  14462. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14463. if(buf) aFree(buf);
  14464. if(buf2) aFree(buf2);
  14465. StringBuf_Destroy(&final_buf);
  14466. return SCRIPT_CMD_SUCCESS;
  14467. }
  14468. //=======================================================
  14469. // sscanf(<str>, <format>, ...);
  14470. // Implements C sscanf.
  14471. //-------------------------------------------------------
  14472. BUILDIN_FUNC(sscanf){
  14473. unsigned int argc, arg = 0, len;
  14474. struct script_data* data;
  14475. struct map_session_data* sd = NULL;
  14476. const char* str;
  14477. const char* format;
  14478. const char* p;
  14479. const char* q;
  14480. char* buf = NULL;
  14481. char* buf_p;
  14482. char* ref_str = NULL;
  14483. int ref_int;
  14484. // Get data
  14485. str = script_getstr(st, 2);
  14486. format = script_getstr(st, 3);
  14487. argc = script_lastdata(st)-3;
  14488. len = strlen(format);
  14489. if (len != 0 && strlen(str) == 0) {
  14490. // If the source string is empty but the format string is not, we return -1
  14491. // according to the C specs. (if the format string is also empty, we shall
  14492. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14493. script_pushint(st, -1);
  14494. return SCRIPT_CMD_SUCCESS;
  14495. }
  14496. CREATE(buf, char, len*2+1);
  14497. // Issue sscanf for each parameter
  14498. *buf = 0;
  14499. q = format;
  14500. while((p = strchr(q, '%'))){
  14501. if(p!=q){
  14502. strncat(buf, q, (size_t)(p-q));
  14503. q = p;
  14504. }
  14505. p = q+1;
  14506. if(*p=='*' || *p=='%'){ // Skip
  14507. strncat(buf, q, 2);
  14508. q+=2;
  14509. continue;
  14510. }
  14511. if(arg>=argc){
  14512. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14513. script_pushint(st, -1);
  14514. if(buf) aFree(buf);
  14515. if(ref_str) aFree(ref_str);
  14516. return SCRIPT_CMD_FAILURE;
  14517. }
  14518. if((p = strchr(q+1, '%'))==NULL){
  14519. p = strchr(q, 0); // EOS
  14520. }
  14521. len = p-q;
  14522. strncat(buf, q, len);
  14523. q = p;
  14524. // Validate output
  14525. data = script_getdata(st, arg+4);
  14526. if(!data_isreference(data) || !reference_tovariable(data)){
  14527. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14528. script_pushint(st, -1);
  14529. if(buf) aFree(buf);
  14530. if(ref_str) aFree(ref_str);
  14531. return SCRIPT_CMD_FAILURE;
  14532. }
  14533. buf_p = reference_getname(data);
  14534. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14535. script_pushint(st, -1);
  14536. if(buf) aFree(buf);
  14537. if(ref_str) aFree(ref_str);
  14538. return SCRIPT_CMD_SUCCESS;
  14539. }
  14540. // Save value if any
  14541. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14542. if(ref_str==NULL){
  14543. CREATE(ref_str, char, strlen(str)+1);
  14544. }
  14545. if(sscanf(str, buf, ref_str)==0){
  14546. break;
  14547. }
  14548. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14549. } else { // Number
  14550. if(sscanf(str, buf, &ref_int)==0){
  14551. break;
  14552. }
  14553. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14554. }
  14555. arg++;
  14556. // Disable used format (%... -> %*...)
  14557. buf_p = strchr(buf, 0);
  14558. memmove(buf_p-len+2, buf_p-len+1, len);
  14559. *(buf_p-len+1) = '*';
  14560. }
  14561. script_pushint(st, arg);
  14562. if(buf) aFree(buf);
  14563. if(ref_str) aFree(ref_str);
  14564. return SCRIPT_CMD_SUCCESS;
  14565. }
  14566. //=======================================================
  14567. // strpos(<haystack>, <needle>)
  14568. // strpos(<haystack>, <needle>, <offset>)
  14569. //
  14570. // Implements PHP style strpos. Adapted from code from
  14571. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14572. //-------------------------------------------------------
  14573. BUILDIN_FUNC(strpos) {
  14574. const char *haystack = script_getstr(st,2);
  14575. const char *needle = script_getstr(st,3);
  14576. int i;
  14577. size_t len;
  14578. if( script_hasdata(st,4) )
  14579. i = script_getnum(st,4);
  14580. else
  14581. i = 0;
  14582. if (needle[0] == '\0') {
  14583. script_pushint(st, -1);
  14584. return SCRIPT_CMD_SUCCESS;
  14585. }
  14586. len = strlen(haystack);
  14587. for ( ; i < len; ++i ) {
  14588. if ( haystack[i] == *needle ) {
  14589. // matched starting char -- loop through remaining chars
  14590. const char *h, *n;
  14591. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14592. if ( *h != *n ) {
  14593. break;
  14594. }
  14595. }
  14596. if ( !*n ) { // matched all of 'needle' to null termination
  14597. script_pushint(st, i);
  14598. return SCRIPT_CMD_SUCCESS;
  14599. }
  14600. }
  14601. }
  14602. script_pushint(st, -1);
  14603. return SCRIPT_CMD_SUCCESS;
  14604. }
  14605. //===============================================================
  14606. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14607. //
  14608. // Note: Finds all instances of <search> in <input> and replaces
  14609. // with <replace>. If specified will only replace as many
  14610. // instances as specified in <count>. By default will be case
  14611. // sensitive.
  14612. //---------------------------------------------------------------
  14613. BUILDIN_FUNC(replacestr)
  14614. {
  14615. const char *input = script_getstr(st, 2);
  14616. const char *find = script_getstr(st, 3);
  14617. const char *replace = script_getstr(st, 4);
  14618. size_t inputlen = strlen(input);
  14619. size_t findlen = strlen(find);
  14620. struct StringBuf output;
  14621. bool usecase = true;
  14622. int count = 0;
  14623. int numFinds = 0;
  14624. int i = 0, f = 0;
  14625. if(findlen == 0) {
  14626. ShowError("script:replacestr: Invalid search length.\n");
  14627. st->state = END;
  14628. return SCRIPT_CMD_FAILURE;
  14629. }
  14630. if(script_hasdata(st, 5)) {
  14631. if( script_isint(st, 5) )
  14632. usecase = script_getnum(st, 5) != 0;
  14633. else {
  14634. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14635. st->state = END;
  14636. return SCRIPT_CMD_FAILURE;
  14637. }
  14638. }
  14639. if(script_hasdata(st, 6)) {
  14640. count = script_getnum(st, 6);
  14641. if(count == 0) {
  14642. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14643. st->state = END;
  14644. return SCRIPT_CMD_FAILURE;
  14645. }
  14646. }
  14647. StringBuf_Init(&output);
  14648. for(; i < inputlen; i++) {
  14649. if(count && count == numFinds) { //found enough, stop looking
  14650. break;
  14651. }
  14652. for(f = 0; f <= findlen; f++) {
  14653. if(f == findlen) { //complete match
  14654. numFinds++;
  14655. StringBuf_AppendStr(&output, replace);
  14656. i += findlen - 1;
  14657. break;
  14658. } else {
  14659. if(usecase) {
  14660. if((i + f) > inputlen || input[i + f] != find[f]) {
  14661. StringBuf_Printf(&output, "%c", input[i]);
  14662. break;
  14663. }
  14664. } else {
  14665. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14666. StringBuf_Printf(&output, "%c", input[i]);
  14667. break;
  14668. }
  14669. }
  14670. }
  14671. }
  14672. }
  14673. //append excess after enough found
  14674. if(i < inputlen)
  14675. StringBuf_AppendStr(&output, &(input[i]));
  14676. script_pushstrcopy(st, StringBuf_Value(&output));
  14677. StringBuf_Destroy(&output);
  14678. return SCRIPT_CMD_SUCCESS;
  14679. }
  14680. //========================================================
  14681. // countstr <input>, <search>{, <usecase>}
  14682. //
  14683. // Note: Counts the number of times <search> occurs in
  14684. // <input>. By default will be case sensitive.
  14685. //--------------------------------------------------------
  14686. BUILDIN_FUNC(countstr)
  14687. {
  14688. const char *input = script_getstr(st, 2);
  14689. const char *find = script_getstr(st, 3);
  14690. size_t inputlen = strlen(input);
  14691. size_t findlen = strlen(find);
  14692. bool usecase = true;
  14693. int numFinds = 0;
  14694. int i = 0, f = 0;
  14695. if(findlen == 0) {
  14696. ShowError("script:countstr: Invalid search length.\n");
  14697. st->state = END;
  14698. return SCRIPT_CMD_FAILURE;
  14699. }
  14700. if(script_hasdata(st, 4)) {
  14701. if( script_isint(st, 4) )
  14702. usecase = script_getnum(st, 4) != 0;
  14703. else {
  14704. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14705. st->state = END;
  14706. return SCRIPT_CMD_FAILURE;
  14707. }
  14708. }
  14709. for(; i < inputlen; i++) {
  14710. for(f = 0; f <= findlen; f++) {
  14711. if(f == findlen) { //complete match
  14712. numFinds++;
  14713. i += findlen - 1;
  14714. break;
  14715. } else {
  14716. if(usecase) {
  14717. if((i + f) > inputlen || input[i + f] != find[f]) {
  14718. break;
  14719. }
  14720. } else {
  14721. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14722. break;
  14723. }
  14724. }
  14725. }
  14726. }
  14727. }
  14728. script_pushint(st, numFinds);
  14729. return SCRIPT_CMD_SUCCESS;
  14730. }
  14731. /// Changes the display name and/or display class of the npc.
  14732. /// Returns 0 is successful, 1 if the npc does not exist.
  14733. ///
  14734. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14735. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14736. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14737. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14738. BUILDIN_FUNC(setnpcdisplay)
  14739. {
  14740. const char* name;
  14741. const char* newname = NULL;
  14742. int class_ = JT_FAKENPC, size = -1;
  14743. struct npc_data* nd;
  14744. name = script_getstr(st,2);
  14745. if( script_hasdata(st,4) )
  14746. class_ = script_getnum(st,4);
  14747. if( script_hasdata(st,5) )
  14748. size = script_getnum(st,5);
  14749. if( script_isstring(st, 3) )
  14750. newname = script_getstr(st, 3);
  14751. else
  14752. class_ = script_getnum(st, 3);
  14753. nd = npc_name2id(name);
  14754. if( nd == NULL )
  14755. {// not found
  14756. script_pushint(st,1);
  14757. return SCRIPT_CMD_SUCCESS;
  14758. }
  14759. // update npc
  14760. if( newname )
  14761. npc_setdisplayname(nd, newname);
  14762. if( size != -1 && size != (int)nd->size )
  14763. nd->size = size;
  14764. else
  14765. size = -1;
  14766. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  14767. npc_setclass(nd, class_);
  14768. else if( size != -1 )
  14769. unit_refresh(&nd->bl); // Required to update the visual size
  14770. script_pushint(st,0);
  14771. return SCRIPT_CMD_SUCCESS;
  14772. }
  14773. BUILDIN_FUNC(atoi)
  14774. {
  14775. const char *value;
  14776. value = script_getstr(st,2);
  14777. script_pushint(st,atoi(value));
  14778. return SCRIPT_CMD_SUCCESS;
  14779. }
  14780. BUILDIN_FUNC(axtoi)
  14781. {
  14782. const char *hex = script_getstr(st,2);
  14783. long value = strtol(hex, NULL, 16);
  14784. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14785. value = cap_value(value, INT_MIN, INT_MAX);
  14786. #endif
  14787. script_pushint(st, (int)value);
  14788. return SCRIPT_CMD_SUCCESS;
  14789. }
  14790. BUILDIN_FUNC(strtol)
  14791. {
  14792. const char *string = script_getstr(st, 2);
  14793. int base = script_getnum(st, 3);
  14794. long value = strtol(string, NULL, base);
  14795. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14796. value = cap_value(value, INT_MIN, INT_MAX);
  14797. #endif
  14798. script_pushint(st, (int)value);
  14799. return SCRIPT_CMD_SUCCESS;
  14800. }
  14801. // case-insensitive substring search [lordalfa]
  14802. BUILDIN_FUNC(compare)
  14803. {
  14804. const char *message;
  14805. const char *cmpstring;
  14806. message = script_getstr(st,2);
  14807. cmpstring = script_getstr(st,3);
  14808. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14809. return SCRIPT_CMD_SUCCESS;
  14810. }
  14811. BUILDIN_FUNC(strcmp)
  14812. {
  14813. const char *str1;
  14814. const char *str2;
  14815. str1 = script_getstr(st,2);
  14816. str2 = script_getstr(st,3);
  14817. script_pushint(st,strcmp(str1, str2));
  14818. return SCRIPT_CMD_SUCCESS;
  14819. }
  14820. // [zBuffer] List of mathematics commands --->
  14821. BUILDIN_FUNC(sqrt)
  14822. {
  14823. double i, a;
  14824. i = script_getnum(st,2);
  14825. a = sqrt(i);
  14826. script_pushint(st,(int)a);
  14827. return SCRIPT_CMD_SUCCESS;
  14828. }
  14829. BUILDIN_FUNC(pow)
  14830. {
  14831. double i, a, b;
  14832. a = script_getnum(st,2);
  14833. b = script_getnum(st,3);
  14834. i = pow(a,b);
  14835. script_pushint(st,(int)i);
  14836. return SCRIPT_CMD_SUCCESS;
  14837. }
  14838. BUILDIN_FUNC(distance)
  14839. {
  14840. int x0, y0, x1, y1;
  14841. x0 = script_getnum(st,2);
  14842. y0 = script_getnum(st,3);
  14843. x1 = script_getnum(st,4);
  14844. y1 = script_getnum(st,5);
  14845. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14846. return SCRIPT_CMD_SUCCESS;
  14847. }
  14848. // <--- [zBuffer] List of mathematics commands
  14849. BUILDIN_FUNC(md5)
  14850. {
  14851. const char *tmpstr;
  14852. char *md5str;
  14853. tmpstr = script_getstr(st,2);
  14854. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14855. MD5_String(tmpstr, md5str);
  14856. script_pushstr(st, md5str);
  14857. return SCRIPT_CMD_SUCCESS;
  14858. }
  14859. // [zBuffer] List of dynamic var commands --->
  14860. /**
  14861. * setd "<variable name>",<value>{,<char_id>};
  14862. **/
  14863. BUILDIN_FUNC(setd)
  14864. {
  14865. TBL_PC *sd = NULL;
  14866. char varname[100];
  14867. const char *buffer;
  14868. int elem;
  14869. buffer = script_getstr(st, 2);
  14870. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14871. elem = 0;
  14872. if( not_server_variable(*varname) ) {
  14873. if (!script_charid2sd(4,sd))
  14874. return SCRIPT_CMD_FAILURE;
  14875. }
  14876. if( is_string_variable(varname) ) {
  14877. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), NULL );
  14878. } else {
  14879. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), NULL );
  14880. }
  14881. return SCRIPT_CMD_SUCCESS;
  14882. }
  14883. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14884. {
  14885. int i, j;
  14886. TBL_PC* sd = NULL;
  14887. const char* query;
  14888. struct script_data* data;
  14889. const char* name;
  14890. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14891. int num_vars;
  14892. int num_cols;
  14893. // check target variables
  14894. for( i = 3; script_hasdata(st,i); ++i ) {
  14895. data = script_getdata(st, i);
  14896. if( data_isreference(data) ) { // it's a variable
  14897. name = reference_getname(data);
  14898. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14899. if( !script_rid2sd(sd) ) { // no player attached
  14900. script_reportdata(data);
  14901. st->state = END;
  14902. return SCRIPT_CMD_FAILURE;
  14903. }
  14904. }
  14905. } else {
  14906. ShowError("script:query_sql: not a variable\n");
  14907. script_reportdata(data);
  14908. st->state = END;
  14909. return SCRIPT_CMD_FAILURE;
  14910. }
  14911. }
  14912. num_vars = i - 3;
  14913. // Execute the query
  14914. query = script_getstr(st,2);
  14915. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14916. Sql_ShowDebug(handle);
  14917. script_pushint(st, -1);
  14918. return SCRIPT_CMD_FAILURE;
  14919. }
  14920. if( Sql_NumRows(handle) == 0 ) { // No data received
  14921. Sql_FreeResult(handle);
  14922. script_pushint(st, 0);
  14923. return SCRIPT_CMD_SUCCESS;
  14924. }
  14925. // Count the number of columns to store
  14926. num_cols = Sql_NumColumns(handle);
  14927. if( num_vars < num_cols ) {
  14928. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14929. script_reportsrc(st);
  14930. } else if( num_vars > num_cols ) {
  14931. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14932. script_reportsrc(st);
  14933. }
  14934. // Store data
  14935. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14936. for( j = 0; j < num_vars; ++j ) {
  14937. char* str = NULL;
  14938. if( j < num_cols )
  14939. Sql_GetData(handle, j, &str, NULL);
  14940. data = script_getdata(st, j+3);
  14941. name = reference_getname(data);
  14942. if( is_string_variable(name) )
  14943. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  14944. else
  14945. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  14946. }
  14947. }
  14948. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14949. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14950. script_reportsrc(st);
  14951. }
  14952. // Free data
  14953. Sql_FreeResult(handle);
  14954. script_pushint(st, i);
  14955. return SCRIPT_CMD_SUCCESS;
  14956. }
  14957. BUILDIN_FUNC(query_sql) {
  14958. return buildin_query_sql_sub(st, qsmysql_handle);
  14959. }
  14960. BUILDIN_FUNC(query_logsql) {
  14961. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14962. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14963. script_pushint(st,-1);
  14964. return SCRIPT_CMD_FAILURE;
  14965. }
  14966. return buildin_query_sql_sub(st, logmysql_handle);
  14967. }
  14968. //Allows escaping of a given string.
  14969. BUILDIN_FUNC(escape_sql)
  14970. {
  14971. const char *str;
  14972. char *esc_str;
  14973. size_t len;
  14974. str = script_getstr(st,2);
  14975. len = strlen(str);
  14976. esc_str = (char*)aMalloc(len*2+1);
  14977. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14978. script_pushstr(st, esc_str);
  14979. return SCRIPT_CMD_SUCCESS;
  14980. }
  14981. BUILDIN_FUNC(getd)
  14982. {
  14983. char varname[100];
  14984. const char *buffer;
  14985. int elem;
  14986. buffer = script_getstr(st, 2);
  14987. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14988. elem = 0;
  14989. // Push the 'pointer' so it's more flexible [Lance]
  14990. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14991. return SCRIPT_CMD_SUCCESS;
  14992. }
  14993. BUILDIN_FUNC(callshop)
  14994. {
  14995. TBL_PC *sd = NULL;
  14996. struct npc_data *nd;
  14997. const char *shopname;
  14998. int flag = 0;
  14999. if (!script_rid2sd(sd)) {
  15000. script_pushint(st,0);
  15001. return SCRIPT_CMD_SUCCESS;
  15002. }
  15003. shopname = script_getstr(st, 2);
  15004. if (script_hasdata(st,3))
  15005. flag = script_getnum(st,3);
  15006. nd = npc_name2id(shopname);
  15007. 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) ) {
  15008. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  15009. script_pushint(st,0);
  15010. return SCRIPT_CMD_FAILURE;
  15011. }
  15012. if (nd->subtype == NPCTYPE_SHOP) {
  15013. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15014. sd->state.callshop = 1;
  15015. switch (flag) {
  15016. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  15017. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  15018. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  15019. }
  15020. }
  15021. #if PACKETVER >= 20131223
  15022. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15023. unsigned short i;
  15024. for (i = 0; i < nd->u.shop.count; i++) {
  15025. if (nd->u.shop.shop_item[i].qty)
  15026. break;
  15027. }
  15028. if (i == nd->u.shop.count) {
  15029. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  15030. return SCRIPT_CMD_FAILURE;
  15031. }
  15032. sd->npc_shopid = nd->bl.id;
  15033. clif_npc_market_open(sd, nd);
  15034. script_pushint(st,1);
  15035. return SCRIPT_CMD_SUCCESS;
  15036. }
  15037. #endif
  15038. else
  15039. clif_cashshop_show(sd, nd);
  15040. sd->npc_shopid = nd->bl.id;
  15041. script_pushint(st,1);
  15042. return SCRIPT_CMD_SUCCESS;
  15043. }
  15044. BUILDIN_FUNC(npcshopitem)
  15045. {
  15046. const char* npcname = script_getstr(st, 2);
  15047. struct npc_data* nd = npc_name2id(npcname);
  15048. int n, i;
  15049. int amount;
  15050. uint16 offs = 2;
  15051. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  15052. script_pushint(st,0);
  15053. return SCRIPT_CMD_SUCCESS;
  15054. }
  15055. #if PACKETVER >= 20131223
  15056. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15057. offs = 3;
  15058. npc_market_delfromsql_(nd->exname, 0, true);
  15059. }
  15060. #endif
  15061. // get the count of new entries
  15062. amount = (script_lastdata(st)-2)/offs;
  15063. // generate new shop item list
  15064. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  15065. nd->u.shop.count = 0;
  15066. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  15067. t_itemid nameid = script_getnum( st, i );
  15068. if( itemdb_exists( nameid ) == nullptr ){
  15069. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  15070. script_pushint( st, 0 );
  15071. return SCRIPT_CMD_FAILURE;
  15072. }
  15073. nd->u.shop.shop_item[n].nameid = nameid;
  15074. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  15075. #if PACKETVER >= 20131223
  15076. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15077. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  15078. nd->u.shop.shop_item[n].flag = 1;
  15079. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  15080. }
  15081. #endif
  15082. nd->u.shop.count++;
  15083. }
  15084. script_pushint(st,1);
  15085. return SCRIPT_CMD_SUCCESS;
  15086. }
  15087. BUILDIN_FUNC(npcshopadditem)
  15088. {
  15089. const char* npcname = script_getstr(st,2);
  15090. struct npc_data* nd = npc_name2id(npcname);
  15091. uint16 offs = 2, amount;
  15092. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15093. script_pushint(st,0);
  15094. return SCRIPT_CMD_SUCCESS;
  15095. }
  15096. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15097. offs = 3;
  15098. // get the count of new entries
  15099. amount = (script_lastdata(st)-2)/offs;
  15100. #if PACKETVER >= 20131223
  15101. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15102. for (int n = 0, i = 3; n < amount; n++, i += offs) {
  15103. t_itemid nameid = script_getnum(st,i);
  15104. uint16 j;
  15105. if( itemdb_exists( nameid ) == nullptr ){
  15106. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15107. script_pushint( st, 0 );
  15108. return SCRIPT_CMD_FAILURE;
  15109. }
  15110. // Check existing entries
  15111. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  15112. if (j == nd->u.shop.count) {
  15113. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  15114. j = nd->u.shop.count;
  15115. nd->u.shop.shop_item[j].flag = 1;
  15116. nd->u.shop.count++;
  15117. }
  15118. nd->u.shop.shop_item[j].nameid = nameid;
  15119. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  15120. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  15121. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  15122. }
  15123. script_pushint(st,1);
  15124. return SCRIPT_CMD_SUCCESS;
  15125. }
  15126. #endif
  15127. // append new items to existing shop item list
  15128. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  15129. for (int n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  15130. {
  15131. t_itemid nameid = script_getnum( st, i );
  15132. if( itemdb_exists( nameid ) == nullptr ){
  15133. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15134. script_pushint( st, 0 );
  15135. return SCRIPT_CMD_FAILURE;
  15136. }
  15137. nd->u.shop.shop_item[n].nameid = nameid;
  15138. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  15139. nd->u.shop.count++;
  15140. }
  15141. script_pushint(st,1);
  15142. return SCRIPT_CMD_SUCCESS;
  15143. }
  15144. BUILDIN_FUNC(npcshopdelitem)
  15145. {
  15146. const char* npcname = script_getstr(st,2);
  15147. struct npc_data* nd = npc_name2id(npcname);
  15148. int n, i, size;
  15149. unsigned short amount;
  15150. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15151. script_pushint(st,0);
  15152. return SCRIPT_CMD_SUCCESS;
  15153. }
  15154. amount = script_lastdata(st)-2;
  15155. size = nd->u.shop.count;
  15156. // remove specified items from the shop item list
  15157. for( i = 3; i < 3 + amount; i++ ) {
  15158. t_itemid nameid = script_getnum(st,i);
  15159. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  15160. if( n < size ) {
  15161. if (n+1 != size)
  15162. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  15163. #if PACKETVER >= 20131223
  15164. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15165. npc_market_delfromsql_(nd->exname, nameid, false);
  15166. #endif
  15167. size--;
  15168. }
  15169. }
  15170. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  15171. nd->u.shop.count = size;
  15172. script_pushint(st,1);
  15173. return SCRIPT_CMD_SUCCESS;
  15174. }
  15175. /**
  15176. * Sets a script to attach to a shop npc.
  15177. * npcshopattach "<npc_name>";
  15178. **/
  15179. BUILDIN_FUNC(npcshopattach)
  15180. {
  15181. const char* npcname = script_getstr(st,2);
  15182. struct npc_data* nd = npc_name2id(npcname);
  15183. int flag = 1;
  15184. if( script_hasdata(st,3) )
  15185. flag = script_getnum(st,3);
  15186. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15187. script_pushint(st,0);
  15188. return SCRIPT_CMD_SUCCESS;
  15189. }
  15190. if (flag)
  15191. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  15192. else
  15193. nd->master_nd = NULL;
  15194. script_pushint(st,1);
  15195. return SCRIPT_CMD_SUCCESS;
  15196. }
  15197. /*==========================================
  15198. * Returns some values of an item [Lupus]
  15199. * Price, Weight, etc...
  15200. setitemscript(itemID,"{new item bonus script}",[n]);
  15201. Where n:
  15202. 0 - script
  15203. 1 - Equip script
  15204. 2 - Unequip script
  15205. *------------------------------------------*/
  15206. BUILDIN_FUNC(setitemscript)
  15207. {
  15208. int n = 0;
  15209. const char *script;
  15210. struct item_data *i_data;
  15211. struct script_code **dstscript;
  15212. t_itemid item_id = script_getnum(st,2);
  15213. script = script_getstr(st,3);
  15214. if( script_hasdata(st,4) )
  15215. n=script_getnum(st,4);
  15216. i_data = itemdb_exists(item_id);
  15217. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15218. script_pushint(st,0);
  15219. return SCRIPT_CMD_SUCCESS;
  15220. }
  15221. switch (n) {
  15222. case 2:
  15223. dstscript = &i_data->unequip_script;
  15224. break;
  15225. case 1:
  15226. dstscript = &i_data->equip_script;
  15227. break;
  15228. default:
  15229. dstscript = &i_data->script;
  15230. break;
  15231. }
  15232. if(*dstscript)
  15233. script_free_code(*dstscript);
  15234. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15235. script_pushint(st,1);
  15236. return SCRIPT_CMD_SUCCESS;
  15237. }
  15238. /*=======================================================
  15239. * Add or Update a mob drop temporarily [Akinari]
  15240. * Original Idea By: [Lupus]
  15241. *
  15242. * addmonsterdrop <mob_id or name>,<item_id>,<rate>,{<steal protected>,{<random option group id>}};
  15243. *
  15244. * If given an item the mob already drops, the rate
  15245. * is updated to the new rate. Rate range is 1-10000.
  15246. * Returns true if succeeded (added/updated a mob drop) false otherwise.
  15247. *-------------------------------------------------------*/
  15248. BUILDIN_FUNC(addmonsterdrop)
  15249. {
  15250. std::shared_ptr<s_mob_db> mob;
  15251. if (script_isstring(st, 2))
  15252. mob = mob_db.find(mobdb_searchname(script_getstr(st, 2)));
  15253. else
  15254. mob = mob_db.find(script_getnum(st, 2));
  15255. if (mob == nullptr) {
  15256. if (script_isstring(st, 2))
  15257. ShowError("addmonsterdrop: bad mob name given %s\n", script_getstr(st, 2));
  15258. else
  15259. ShowError("addmonsterdrop: bad mob id given %d\n", script_getnum(st, 2));
  15260. script_pushint(st, false);
  15261. return SCRIPT_CMD_FAILURE;
  15262. }
  15263. t_itemid item_id = script_getnum(st, 3);
  15264. item_data *itm = itemdb_exists(item_id);
  15265. if (itm == nullptr) {
  15266. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id);
  15267. script_pushint(st, false);
  15268. return SCRIPT_CMD_FAILURE;
  15269. }
  15270. int rate = script_getnum(st, 4);
  15271. if (rate < 1 || rate > 10000) {
  15272. ShowError("addmonsterdrop: Invalid rate %d (min 1, max 10000).\n", rate);
  15273. script_pushint(st, false);
  15274. return SCRIPT_CMD_FAILURE;
  15275. }
  15276. uint16 c = 0;
  15277. for (uint16 i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15278. if (mob->dropitem[i].nameid > 0) {
  15279. if (mob->dropitem[i].nameid == item_id) { // If it equals item_id we update that drop
  15280. c = i;
  15281. break;
  15282. }
  15283. continue;
  15284. }
  15285. if (c == 0) // Accept first available slot only
  15286. c = i;
  15287. }
  15288. if (c == 0) { // No place to put the new drop
  15289. script_pushint(st, false);
  15290. return SCRIPT_CMD_SUCCESS;
  15291. }
  15292. int steal_protected = 0;
  15293. int group = 0;
  15294. if (script_hasdata(st,5))
  15295. steal_protected = script_getnum(st, 5);
  15296. if (script_hasdata(st,6)) {
  15297. group = script_getnum(st, 6);
  15298. if (!random_option_group.exists(group)) {
  15299. ShowError("buildin_addmonsterdrop: Unknown random option group %d.\n", group);
  15300. script_pushint(st, false);
  15301. return SCRIPT_CMD_FAILURE;
  15302. }
  15303. if (itm->type != IT_WEAPON && itm->type != IT_ARMOR && itm->type != IT_SHADOWGEAR) {
  15304. ShowError("buildin_addmonsterdrop: Random option group can't be used with this type of item (item Id: %d).\n", item_id);
  15305. script_pushint(st, false);
  15306. return SCRIPT_CMD_FAILURE;
  15307. }
  15308. }
  15309. // Fill in the slot with the item and rate
  15310. mob->dropitem[c].nameid = item_id;
  15311. mob->dropitem[c].rate = rate;
  15312. mob->dropitem[c].steal_protected = steal_protected > 0;
  15313. mob->dropitem[c].randomopt_group = group;
  15314. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15315. script_pushint(st, true);
  15316. return SCRIPT_CMD_SUCCESS;
  15317. }
  15318. /*=======================================================
  15319. * Delete a mob drop temporarily [Akinari]
  15320. * Original Idea By: [Lupus]
  15321. *
  15322. * delmonsterdrop <mob_id or name>,<item_id>;
  15323. *
  15324. * Returns 1 if succeeded (deleted a mob drop)
  15325. *-------------------------------------------------------*/
  15326. BUILDIN_FUNC(delmonsterdrop)
  15327. {
  15328. std::shared_ptr<s_mob_db> mob;
  15329. if(script_isstring(st, 2))
  15330. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15331. else
  15332. mob = mob_db.find(script_getnum(st,2));
  15333. t_itemid item_id=script_getnum(st,3);
  15334. if(!item_db.exists(item_id)){
  15335. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15336. return SCRIPT_CMD_FAILURE;
  15337. }
  15338. if(mob) { //We got a valid monster, check for item drop on monster
  15339. unsigned char i;
  15340. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15341. if(mob->dropitem[i].nameid == item_id) {
  15342. mob->dropitem[i].nameid = 0;
  15343. mob->dropitem[i].rate = 0;
  15344. mob->dropitem[i].steal_protected = false;
  15345. mob->dropitem[i].randomopt_group = 0;
  15346. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15347. script_pushint(st,1);
  15348. return SCRIPT_CMD_SUCCESS;
  15349. }
  15350. }
  15351. //No drop on that monster
  15352. script_pushint(st,0);
  15353. } else {
  15354. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15355. return SCRIPT_CMD_FAILURE;
  15356. }
  15357. return SCRIPT_CMD_SUCCESS;
  15358. }
  15359. /*==========================================
  15360. * Returns some values of a monster [Lupus]
  15361. * Name, Level, race, size, etc...
  15362. getmonsterinfo(monsterID,queryIndex);
  15363. *------------------------------------------*/
  15364. BUILDIN_FUNC(getmonsterinfo)
  15365. {
  15366. int mob_id;
  15367. mob_id = script_getnum(st,2);
  15368. if (!mobdb_checkid(mob_id)) {
  15369. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15370. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  15371. script_pushconststr(st,"null");
  15372. else
  15373. script_pushint(st,-1);
  15374. return SCRIPT_CMD_SUCCESS;
  15375. }
  15376. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  15377. switch ( script_getnum(st,3) ) {
  15378. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15379. case MOB_LV: script_pushint(st,mob->lv); break;
  15380. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15381. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15382. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15383. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15384. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15385. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15386. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15387. case MOB_RES: script_pushint(st, mob->status.res); break;
  15388. case MOB_MRES: script_pushint(st, mob->status.mres); break;
  15389. case MOB_STR: script_pushint(st,mob->status.str); break;
  15390. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15391. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15392. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15393. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15394. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15395. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15396. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15397. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15398. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15399. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15400. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15401. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15402. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15403. default: script_pushint(st,-1); //wrong Index
  15404. }
  15405. return SCRIPT_CMD_SUCCESS;
  15406. }
  15407. /**
  15408. * Check player's vending/buyingstore/autotrade state
  15409. * checkvending({"<Player Name>"})
  15410. * @author [Nab4]
  15411. */
  15412. BUILDIN_FUNC(checkvending) {
  15413. TBL_PC *sd = NULL;
  15414. if (!script_nick2sd(2,sd) ) {
  15415. script_pushint(st,0);
  15416. return SCRIPT_CMD_SUCCESS;
  15417. }
  15418. else {
  15419. int8 ret = 0;
  15420. if (sd->state.vending)
  15421. ret = 1;
  15422. else if (sd->state.buyingstore)
  15423. ret = 4;
  15424. if (sd->state.autotrade)
  15425. ret |= 2;
  15426. script_pushint(st, ret);
  15427. }
  15428. return SCRIPT_CMD_SUCCESS;
  15429. }
  15430. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15431. {
  15432. TBL_PC *sd = NULL;
  15433. if( script_nick2sd(2,sd) )
  15434. script_pushint(st,(sd->chatID != 0));
  15435. else
  15436. script_pushint(st,0);
  15437. return SCRIPT_CMD_SUCCESS;
  15438. }
  15439. BUILDIN_FUNC(checkidle)
  15440. {
  15441. TBL_PC *sd = NULL;
  15442. if( script_nick2sd(2,sd) )
  15443. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15444. else
  15445. script_pushint(st, 0);
  15446. return SCRIPT_CMD_SUCCESS;
  15447. }
  15448. BUILDIN_FUNC(checkidlehom)
  15449. {
  15450. TBL_PC *sd = NULL;
  15451. if( script_nick2sd(2,sd) )
  15452. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15453. else
  15454. script_pushint(st, 0);
  15455. return SCRIPT_CMD_SUCCESS;
  15456. }
  15457. BUILDIN_FUNC(checkidlemer)
  15458. {
  15459. TBL_PC *sd;
  15460. if( script_nick2sd(2,sd) )
  15461. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_mer));
  15462. else
  15463. script_pushint(st, 0);
  15464. return SCRIPT_CMD_SUCCESS;
  15465. }
  15466. BUILDIN_FUNC(searchitem)
  15467. {
  15468. struct script_data* data = script_getdata(st, 2);
  15469. const char *itemname = script_getstr(st,3);
  15470. struct item_data *items[MAX_SEARCH];
  15471. int count;
  15472. char* name;
  15473. int32 start;
  15474. int32 id;
  15475. int32 i;
  15476. TBL_PC* sd = NULL;
  15477. if ((items[0] = itemdb_exists(atoi(itemname))))
  15478. count = 1;
  15479. else
  15480. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  15481. if (!count) {
  15482. script_pushint(st, 0);
  15483. return SCRIPT_CMD_SUCCESS;
  15484. }
  15485. if( !data_isreference(data) )
  15486. {
  15487. ShowError("script:searchitem: not a variable\n");
  15488. script_reportdata(data);
  15489. st->state = END;
  15490. return SCRIPT_CMD_FAILURE;// not a variable
  15491. }
  15492. id = reference_getid(data);
  15493. start = reference_getindex(data);
  15494. name = reference_getname(data);
  15495. if( not_server_variable(*name) && !script_rid2sd(sd) )
  15496. {
  15497. return SCRIPT_CMD_SUCCESS;// no player attached
  15498. }
  15499. if( is_string_variable(name) )
  15500. {// string array
  15501. ShowError("script:searchitem: not an integer array reference\n");
  15502. script_reportdata(data);
  15503. st->state = END;
  15504. return SCRIPT_CMD_FAILURE;// not supported
  15505. }
  15506. for( i = 0; i < count; ++start, ++i )
  15507. {// Set array
  15508. set_reg_num( st, sd, reference_uid( id, start ), name, items[i]->nameid, reference_getref( data ) );
  15509. }
  15510. script_pushint(st, count);
  15511. return SCRIPT_CMD_SUCCESS;
  15512. }
  15513. // [zBuffer] List of player cont commands --->
  15514. BUILDIN_FUNC(rid2name)
  15515. {
  15516. struct block_list *bl = NULL;
  15517. int rid = script_getnum(st,2);
  15518. if((bl = map_id2bl(rid)))
  15519. {
  15520. switch(bl->type) {
  15521. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15522. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15523. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15524. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15525. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15526. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name.c_str()); break;
  15527. default:
  15528. ShowError("buildin_rid2name: BL type unknown.\n");
  15529. script_pushconststr(st,"");
  15530. break;
  15531. }
  15532. } else {
  15533. ShowError("buildin_rid2name: invalid RID\n");
  15534. script_pushconststr(st,"(null)");
  15535. }
  15536. return SCRIPT_CMD_SUCCESS;
  15537. }
  15538. /**
  15539. * Toggle a unit from moving.
  15540. * pcblockmove(<unit_id>,<option>);
  15541. */
  15542. BUILDIN_FUNC(pcblockmove)
  15543. {
  15544. struct block_list *bl = NULL;
  15545. if (script_getnum(st, 2))
  15546. bl = map_id2bl(script_getnum(st,2));
  15547. else
  15548. bl = map_id2bl(st->rid);
  15549. if (bl) {
  15550. struct unit_data *ud = unit_bl2ud(bl);
  15551. if (ud)
  15552. ud->state.blockedmove = script_getnum(st,3) > 0;
  15553. }
  15554. return SCRIPT_CMD_SUCCESS;
  15555. }
  15556. /**
  15557. * Toggle a unit from casting skills.
  15558. * pcblockskill(<unit_id>,<option>);
  15559. */
  15560. BUILDIN_FUNC(pcblockskill)
  15561. {
  15562. struct block_list *bl = NULL;
  15563. if (script_getnum(st, 2))
  15564. bl = map_id2bl(script_getnum(st,2));
  15565. else
  15566. bl = map_id2bl(st->rid);
  15567. if (bl) {
  15568. struct unit_data *ud = unit_bl2ud(bl);
  15569. if (ud)
  15570. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15571. }
  15572. return SCRIPT_CMD_SUCCESS;
  15573. }
  15574. BUILDIN_FUNC(setpcblock)
  15575. {
  15576. TBL_PC *sd;
  15577. if (script_mapid2sd(4, sd)) {
  15578. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  15579. if (script_getnum(st, 3) > 0)
  15580. sd->state.block_action |= type;
  15581. else
  15582. sd->state.block_action &= ~type;
  15583. }
  15584. return SCRIPT_CMD_SUCCESS;
  15585. }
  15586. BUILDIN_FUNC(getpcblock)
  15587. {
  15588. TBL_PC *sd;
  15589. if (script_mapid2sd(2, sd))
  15590. script_pushint(st, sd->state.block_action);
  15591. else
  15592. script_pushint(st, 0);
  15593. return SCRIPT_CMD_SUCCESS;
  15594. }
  15595. BUILDIN_FUNC(pcfollow)
  15596. {
  15597. TBL_PC *sd;
  15598. if( script_mapid2sd(2,sd) )
  15599. pc_follow(sd, script_getnum(st,3));
  15600. return SCRIPT_CMD_SUCCESS;
  15601. }
  15602. BUILDIN_FUNC(pcstopfollow)
  15603. {
  15604. TBL_PC *sd;
  15605. if(script_mapid2sd(2,sd))
  15606. pc_stop_following(sd);
  15607. return SCRIPT_CMD_SUCCESS;
  15608. }
  15609. // <--- [zBuffer] List of player cont commands
  15610. // [zBuffer] List of unit control commands --->
  15611. /// Checks to see if the unit exists.
  15612. ///
  15613. /// unitexists <unit id>;
  15614. BUILDIN_FUNC(unitexists)
  15615. {
  15616. struct block_list* bl;
  15617. bl = map_id2bl(script_getnum(st, 2));
  15618. if (!bl)
  15619. script_pushint(st, false);
  15620. else
  15621. script_pushint(st, true);
  15622. return SCRIPT_CMD_SUCCESS;
  15623. }
  15624. /// Gets the type of the given Game ID.
  15625. ///
  15626. /// getunittype <unit id>;
  15627. BUILDIN_FUNC(getunittype)
  15628. {
  15629. struct block_list* bl;
  15630. int value = 0;
  15631. if(!script_rid2bl(2,bl))
  15632. {
  15633. script_pushint(st, -1);
  15634. return SCRIPT_CMD_FAILURE;
  15635. }
  15636. switch (bl->type) {
  15637. case BL_PC:
  15638. case BL_NPC:
  15639. case BL_PET:
  15640. case BL_MOB:
  15641. case BL_HOM:
  15642. case BL_MER:
  15643. case BL_ELEM:
  15644. value = bl->type;
  15645. break;
  15646. default:
  15647. value = -1;
  15648. break;
  15649. }
  15650. script_pushint(st, value);
  15651. return SCRIPT_CMD_SUCCESS;
  15652. }
  15653. /// Gets specific live information of a bl.
  15654. ///
  15655. /// getunitdata <unit id>,<arrayname>;
  15656. BUILDIN_FUNC(getunitdata)
  15657. {
  15658. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15659. struct block_list* bl;
  15660. TBL_MOB* md = NULL;
  15661. TBL_HOM* hd = NULL;
  15662. TBL_MER* mc = NULL;
  15663. TBL_PET* pd = NULL;
  15664. TBL_ELEM* ed = NULL;
  15665. TBL_NPC* nd = NULL;
  15666. char* name;
  15667. struct script_data *data = script_getdata(st, 3);
  15668. if (!data_isreference(data)) {
  15669. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15670. return SCRIPT_CMD_FAILURE;
  15671. }
  15672. if(!script_rid2bl(2,bl))
  15673. {
  15674. script_pushint(st, -1);
  15675. return SCRIPT_CMD_FAILURE;
  15676. }
  15677. switch (bl->type) {
  15678. case BL_MOB: md = map_id2md(bl->id); break;
  15679. case BL_HOM: hd = map_id2hd(bl->id); break;
  15680. case BL_PET: pd = map_id2pd(bl->id); break;
  15681. case BL_MER: mc = map_id2mc(bl->id); break;
  15682. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15683. case BL_NPC: nd = map_id2nd(bl->id); break;
  15684. default:
  15685. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  15686. return SCRIPT_CMD_FAILURE;
  15687. }
  15688. name = reference_getname(data);
  15689. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  15690. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  15691. return SCRIPT_CMD_FAILURE;
  15692. }
  15693. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  15694. switch(bl->type) {
  15695. case BL_MOB:
  15696. if (!md) {
  15697. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  15698. return SCRIPT_CMD_FAILURE;
  15699. }
  15700. getunitdata_sub(UMOB_SIZE, md->status.size);
  15701. getunitdata_sub(UMOB_LEVEL, md->level);
  15702. getunitdata_sub(UMOB_HP, md->status.hp);
  15703. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  15704. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  15705. getunitdata_sub(UMOB_MAPID, md->bl.m);
  15706. getunitdata_sub(UMOB_X, md->bl.x);
  15707. getunitdata_sub(UMOB_Y, md->bl.y);
  15708. getunitdata_sub(UMOB_SPEED, md->status.speed);
  15709. getunitdata_sub(UMOB_MODE, md->status.mode);
  15710. getunitdata_sub(UMOB_AI, md->special_state.ai);
  15711. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  15712. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15713. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15714. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15715. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15716. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15717. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15718. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15719. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15720. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15721. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15722. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15723. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15724. getunitdata_sub(UMOB_STR, md->status.str);
  15725. getunitdata_sub(UMOB_AGI, md->status.agi);
  15726. getunitdata_sub(UMOB_VIT, md->status.vit);
  15727. getunitdata_sub(UMOB_INT, md->status.int_);
  15728. getunitdata_sub(UMOB_DEX, md->status.dex);
  15729. getunitdata_sub(UMOB_LUK, md->status.luk);
  15730. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15731. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15732. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15733. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15734. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15735. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15736. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15737. getunitdata_sub(UMOB_DEF, md->status.def);
  15738. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15739. getunitdata_sub(UMOB_HIT, md->status.hit);
  15740. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15741. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15742. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15743. getunitdata_sub(UMOB_RACE, md->status.race);
  15744. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15745. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15746. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15747. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15748. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15749. getunitdata_sub(UMOB_TARGETID, md->target_id);
  15750. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  15751. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  15752. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  15753. getunitdata_sub(UMOB_IGNORE_CELL_STACK_LIMIT, md->ud.state.ignore_cell_stack_limit);
  15754. getunitdata_sub(UMOB_RES, md->status.res);
  15755. getunitdata_sub(UMOB_MRES, md->status.mres);
  15756. break;
  15757. case BL_HOM:
  15758. if (!hd) {
  15759. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15760. return SCRIPT_CMD_FAILURE;
  15761. }
  15762. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15763. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15764. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15765. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15766. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15767. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15768. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15769. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15770. getunitdata_sub(UHOM_X, hd->bl.x);
  15771. getunitdata_sub(UHOM_Y, hd->bl.y);
  15772. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15773. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15774. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15775. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15776. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15777. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15778. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15779. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15780. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15781. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15782. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15783. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15784. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15785. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15786. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15787. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15788. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15789. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15790. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15791. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15792. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15793. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15794. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15795. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15796. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15797. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15798. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15799. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15800. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15801. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  15802. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  15803. break;
  15804. case BL_PET:
  15805. if (!pd) {
  15806. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15807. return SCRIPT_CMD_FAILURE;
  15808. }
  15809. getunitdata_sub(UPET_SIZE, pd->status.size);
  15810. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15811. getunitdata_sub(UPET_HP, pd->status.hp);
  15812. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15813. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15814. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15815. getunitdata_sub(UPET_X, pd->bl.x);
  15816. getunitdata_sub(UPET_Y, pd->bl.y);
  15817. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15818. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15819. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15820. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15821. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15822. getunitdata_sub(UPET_STR, pd->status.str);
  15823. getunitdata_sub(UPET_AGI, pd->status.agi);
  15824. getunitdata_sub(UPET_VIT, pd->status.vit);
  15825. getunitdata_sub(UPET_INT, pd->status.int_);
  15826. getunitdata_sub(UPET_DEX, pd->status.dex);
  15827. getunitdata_sub(UPET_LUK, pd->status.luk);
  15828. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15829. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15830. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15831. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15832. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15833. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15834. getunitdata_sub(UPET_DEF, pd->status.def);
  15835. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15836. getunitdata_sub(UPET_HIT, pd->status.hit);
  15837. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15838. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15839. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15840. getunitdata_sub(UPET_RACE, pd->status.race);
  15841. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15842. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15843. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15844. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15845. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15846. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  15847. break;
  15848. case BL_MER:
  15849. if (!mc) {
  15850. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15851. return SCRIPT_CMD_FAILURE;
  15852. }
  15853. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15854. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15855. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15856. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15857. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15858. getunitdata_sub(UMER_X, mc->bl.x);
  15859. getunitdata_sub(UMER_Y, mc->bl.y);
  15860. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15861. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15862. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15863. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15864. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15865. getunitdata_sub(UMER_STR, mc->base_status.str);
  15866. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15867. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15868. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15869. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15870. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15871. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15872. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15873. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15874. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15875. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15876. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15877. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15878. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15879. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15880. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15881. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15882. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15883. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15884. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15885. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15886. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15887. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15888. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15889. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  15890. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  15891. break;
  15892. case BL_ELEM:
  15893. if (!ed) {
  15894. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15895. return SCRIPT_CMD_FAILURE;
  15896. }
  15897. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15898. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15899. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15900. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15901. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15902. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15903. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15904. getunitdata_sub(UELE_X, ed->bl.x);
  15905. getunitdata_sub(UELE_Y, ed->bl.y);
  15906. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15907. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15908. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15909. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15910. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15911. getunitdata_sub(UELE_STR, ed->base_status.str);
  15912. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15913. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15914. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15915. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15916. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15917. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15918. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15919. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15920. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15921. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15922. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15923. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15924. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15925. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15926. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15927. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15928. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15929. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15930. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15931. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15932. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15933. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15934. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15935. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  15936. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  15937. break;
  15938. case BL_NPC:
  15939. if (!nd) {
  15940. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15941. return SCRIPT_CMD_FAILURE;
  15942. }
  15943. getunitdata_sub(UNPC_LEVEL, nd->level);
  15944. getunitdata_sub(UNPC_HP, nd->status.hp);
  15945. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15946. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15947. getunitdata_sub(UNPC_X, nd->bl.x);
  15948. getunitdata_sub(UNPC_Y, nd->bl.y);
  15949. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15950. getunitdata_sub(UNPC_STR, nd->status.str);
  15951. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15952. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15953. getunitdata_sub(UNPC_INT, nd->status.int_);
  15954. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15955. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15956. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15957. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15958. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15959. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15960. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15961. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15962. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15963. getunitdata_sub(UNPC_DEF, nd->status.def);
  15964. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15965. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15966. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15967. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15968. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15969. getunitdata_sub(UNPC_RACE, nd->status.race);
  15970. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15971. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15972. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15973. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15974. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15975. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  15976. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  15977. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  15978. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  15979. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  15980. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  15981. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  15982. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  15983. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  15984. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  15985. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  15986. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  15987. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  15988. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  15989. break;
  15990. default:
  15991. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15992. return SCRIPT_CMD_FAILURE;
  15993. }
  15994. #undef getunitdata_sub
  15995. return SCRIPT_CMD_SUCCESS;
  15996. }
  15997. /// Changes the live data of a bl.
  15998. ///
  15999. /// setunitdata <unit id>,<type>,<value>;
  16000. BUILDIN_FUNC(setunitdata)
  16001. {
  16002. struct block_list* bl;
  16003. if(!script_rid2bl(2,bl))
  16004. {
  16005. script_pushint(st, -1);
  16006. return SCRIPT_CMD_FAILURE;
  16007. }
  16008. TBL_MOB* md;
  16009. TBL_HOM* hd;
  16010. TBL_MER* mc;
  16011. TBL_PET* pd;
  16012. TBL_ELEM* ed;
  16013. TBL_NPC* nd;
  16014. switch (bl->type) {
  16015. case BL_MOB: md = map_id2md(bl->id); break;
  16016. case BL_HOM: hd = map_id2hd(bl->id); break;
  16017. case BL_PET: pd = map_id2pd(bl->id); break;
  16018. case BL_MER: mc = map_id2mc(bl->id); break;
  16019. case BL_ELEM: ed = map_id2ed(bl->id); break;
  16020. case BL_NPC:
  16021. nd = map_id2nd(bl->id);
  16022. if (!nd->status.hp)
  16023. status_calc_npc(nd, SCO_FIRST);
  16024. else
  16025. status_calc_npc(nd, SCO_NONE);
  16026. break;
  16027. default:
  16028. ShowError("buildin_setunitdata: Invalid object!\n");
  16029. return SCRIPT_CMD_FAILURE;
  16030. }
  16031. int type = script_getnum(st, 3), value = 0;
  16032. const char *mapname = NULL;
  16033. bool calc_status = false;
  16034. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  16035. mapname = script_getstr(st, 4);
  16036. else
  16037. value = script_getnum(st, 4);
  16038. switch (bl->type) {
  16039. case BL_MOB:
  16040. if (!md) {
  16041. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  16042. return SCRIPT_CMD_FAILURE;
  16043. }
  16044. if (!md->base_status) {
  16045. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  16046. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  16047. }
  16048. // Check if the view data will be modified
  16049. switch( type ){
  16050. case UMOB_SEX:
  16051. //case UMOB_CLASS: // Called by status_set_viewdata
  16052. case UMOB_HAIRSTYLE:
  16053. case UMOB_HAIRCOLOR:
  16054. case UMOB_HEADBOTTOM:
  16055. case UMOB_HEADMIDDLE:
  16056. case UMOB_HEADTOP:
  16057. case UMOB_CLOTHCOLOR:
  16058. case UMOB_SHIELD:
  16059. case UMOB_WEAPON:
  16060. case UMOB_ROBE:
  16061. case UMOB_BODY2:
  16062. mob_set_dynamic_viewdata( md );
  16063. break;
  16064. }
  16065. switch (type) {
  16066. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  16067. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  16068. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  16069. 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;
  16070. case UMOB_MASTERAID: md->master_id = value; break;
  16071. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16072. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16073. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  16074. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16075. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  16076. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  16077. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  16078. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  16079. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  16080. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16081. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16082. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16083. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16084. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16085. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16086. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16087. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16088. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16089. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16090. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16091. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16092. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16093. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16094. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16095. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16096. case UMOB_SLAVECPYMSTRMD:
  16097. if (value > 0) {
  16098. TBL_MOB *md2 = NULL;
  16099. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  16100. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  16101. break;
  16102. }
  16103. md->base_status->mode = md2->status.mode;
  16104. md->state.copy_master_mode = 1;
  16105. } else
  16106. md->state.copy_master_mode = 0;
  16107. calc_status = true;
  16108. break;
  16109. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  16110. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  16111. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  16112. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16113. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  16114. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  16115. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  16116. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  16117. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  16118. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  16119. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  16120. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  16121. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  16122. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  16123. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  16124. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  16125. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  16126. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  16127. case UMOB_TARGETID: {
  16128. if (value==0) {
  16129. mob_unlocktarget(md,gettick());
  16130. break;
  16131. }
  16132. struct block_list* target = map_id2bl(value);
  16133. if (!target) {
  16134. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  16135. return SCRIPT_CMD_FAILURE;
  16136. }
  16137. mob_target(md,target,0);
  16138. break;
  16139. }
  16140. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16141. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16142. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  16143. case UMOB_IGNORE_CELL_STACK_LIMIT: md->ud.state.ignore_cell_stack_limit = value > 0; break;
  16144. case UMOB_RES: md->base_status->res = (short)value; calc_status = true; break;
  16145. case UMOB_MRES: md->base_status->mres = (short)value; calc_status = true; break;
  16146. default:
  16147. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  16148. return SCRIPT_CMD_FAILURE;
  16149. }
  16150. if (calc_status)
  16151. status_calc_bl(&md->bl, SCB_BATTLE);
  16152. break;
  16153. case BL_HOM:
  16154. if (!hd) {
  16155. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  16156. return SCRIPT_CMD_FAILURE;
  16157. }
  16158. switch (type) {
  16159. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  16160. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  16161. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16162. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16163. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16164. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16165. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  16166. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16167. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  16168. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  16169. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  16170. 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;
  16171. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16172. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16173. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16174. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16175. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16176. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16177. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16178. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16179. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16180. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  16181. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16182. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16183. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16184. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16185. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16186. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  16187. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  16188. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  16189. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  16190. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  16191. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  16192. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  16193. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16194. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16195. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  16196. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  16197. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  16198. case UHOM_TARGETID: {
  16199. if (value==0) {
  16200. unit_stop_attack(&hd->bl);
  16201. break;
  16202. }
  16203. struct block_list* target = map_id2bl(value);
  16204. if (!target) {
  16205. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  16206. return SCRIPT_CMD_FAILURE;
  16207. }
  16208. unit_attack(&hd->bl, target->id, 1);
  16209. break;
  16210. }
  16211. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  16212. default:
  16213. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  16214. return SCRIPT_CMD_FAILURE;
  16215. }
  16216. if (calc_status)
  16217. status_calc_bl(&hd->bl, SCB_BATTLE);
  16218. break;
  16219. case BL_PET:
  16220. if (!pd) {
  16221. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  16222. return SCRIPT_CMD_FAILURE;
  16223. }
  16224. switch (type) {
  16225. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  16226. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  16227. case UPET_HP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16228. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16229. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  16230. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16231. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16232. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  16233. 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;
  16234. 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;
  16235. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16236. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16237. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16238. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16239. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16240. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16241. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16242. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16243. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16244. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  16245. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  16246. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  16247. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  16248. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  16249. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  16250. case UPET_DEF: pd->status.def = (defType)value; break;
  16251. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  16252. case UPET_HIT: pd->status.hit = (short)value; break;
  16253. case UPET_FLEE: pd->status.flee = (short)value; break;
  16254. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16255. case UPET_CRIT: pd->status.cri = (short)value; break;
  16256. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16257. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16258. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16259. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16260. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16261. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16262. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16263. default:
  16264. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16265. return SCRIPT_CMD_FAILURE;
  16266. }
  16267. break;
  16268. case BL_MER:
  16269. if (!mc) {
  16270. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  16271. return SCRIPT_CMD_FAILURE;
  16272. }
  16273. switch (type) {
  16274. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16275. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16276. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16277. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16278. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16279. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16280. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16281. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  16282. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  16283. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16284. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16285. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16286. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16287. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16288. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16289. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16290. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16291. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16292. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16293. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16294. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16295. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16296. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16297. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16298. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16299. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16300. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16301. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16302. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16303. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16304. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16305. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16306. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16307. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16308. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16309. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16310. case UMER_TARGETID: {
  16311. if (value==0) {
  16312. unit_stop_attack(&mc->bl);
  16313. break;
  16314. }
  16315. struct block_list* target = map_id2bl(value);
  16316. if (!target) {
  16317. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16318. return SCRIPT_CMD_FAILURE;
  16319. }
  16320. unit_attack(&mc->bl, target->id, 1);
  16321. break;
  16322. }
  16323. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16324. default:
  16325. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16326. return SCRIPT_CMD_FAILURE;
  16327. }
  16328. if (calc_status)
  16329. status_calc_bl(&mc->bl, SCB_BATTLE);
  16330. break;
  16331. case BL_ELEM:
  16332. if (!ed) {
  16333. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16334. return SCRIPT_CMD_FAILURE;
  16335. }
  16336. switch (type) {
  16337. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16338. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16339. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16340. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16341. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16342. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16343. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16344. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16345. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16346. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16347. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16348. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16349. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16350. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16351. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16352. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16353. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16354. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16355. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16356. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16357. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16358. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16359. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16360. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16361. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16362. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16363. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16364. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16365. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16366. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16367. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16368. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16369. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16370. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16371. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16372. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16373. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16374. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16375. case UELE_TARGETID: {
  16376. if (value==0) {
  16377. unit_stop_attack(&ed->bl);
  16378. break;
  16379. }
  16380. struct block_list* target = map_id2bl(value);
  16381. if (!target) {
  16382. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16383. return SCRIPT_CMD_FAILURE;
  16384. }
  16385. elemental_change_mode(ed, EL_MODE_AGGRESSIVE);
  16386. unit_attack(&ed->bl, target->id, 1);
  16387. break;
  16388. }
  16389. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16390. default:
  16391. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16392. return SCRIPT_CMD_FAILURE;
  16393. }
  16394. if (calc_status)
  16395. status_calc_bl(&ed->bl, SCB_BATTLE);
  16396. break;
  16397. case BL_NPC:
  16398. if (!nd) {
  16399. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16400. return SCRIPT_CMD_FAILURE;
  16401. }
  16402. switch (type) {
  16403. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16404. case UNPC_HP: nd->status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16405. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16406. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16407. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16408. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16409. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16410. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16411. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16412. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16413. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16414. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16415. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16416. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16417. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16418. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16419. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16420. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16421. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16422. case UNPC_DEF: nd->status.def = (defType)value; break;
  16423. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16424. case UNPC_HIT: nd->status.hit = (short)value; break;
  16425. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16426. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16427. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16428. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16429. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16430. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16431. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16432. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16433. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16434. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16435. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16436. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16437. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16438. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16439. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16440. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16441. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16442. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16443. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16444. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16445. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16446. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16447. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16448. default:
  16449. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16450. return SCRIPT_CMD_FAILURE;
  16451. }
  16452. break;
  16453. default:
  16454. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16455. return SCRIPT_CMD_FAILURE;
  16456. }
  16457. // Client information updates
  16458. switch (bl->type) {
  16459. case BL_HOM:
  16460. clif_send_homdata(hd->master, SP_ACK, 0);
  16461. break;
  16462. case BL_PET:
  16463. clif_send_petstatus(pd->master);
  16464. break;
  16465. case BL_MER:
  16466. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16467. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16468. break;
  16469. case BL_ELEM:
  16470. clif_elemental_info(ed->master);
  16471. break;
  16472. }
  16473. return SCRIPT_CMD_SUCCESS;
  16474. }
  16475. /// Gets the name of a bl.
  16476. /// Supported types are [MOB|HOM|PET|NPC].
  16477. /// MER and ELEM don't support custom names.
  16478. ///
  16479. /// getunitname <unit id>;
  16480. BUILDIN_FUNC(getunitname)
  16481. {
  16482. struct block_list* bl = NULL;
  16483. if(!script_rid2bl(2,bl)){
  16484. script_pushconststr(st, "Unknown");
  16485. return SCRIPT_CMD_FAILURE;
  16486. }
  16487. script_pushstrcopy(st, status_get_name(bl));
  16488. return SCRIPT_CMD_SUCCESS;
  16489. }
  16490. /// Changes the name of a bl.
  16491. /// Supported types are [MOB|HOM|PET].
  16492. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16493. ///
  16494. /// setunitname <unit id>,<name>;
  16495. BUILDIN_FUNC(setunitname)
  16496. {
  16497. struct block_list* bl = NULL;
  16498. TBL_MOB* md = NULL;
  16499. TBL_HOM* hd = NULL;
  16500. TBL_PET* pd = NULL;
  16501. if(!script_rid2bl(2,bl))
  16502. {
  16503. script_pushconststr(st, "Unknown");
  16504. return SCRIPT_CMD_FAILURE;
  16505. }
  16506. switch (bl->type) {
  16507. case BL_MOB: md = map_id2md(bl->id); break;
  16508. case BL_HOM: hd = map_id2hd(bl->id); break;
  16509. case BL_PET: pd = map_id2pd(bl->id); break;
  16510. default:
  16511. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16512. return SCRIPT_CMD_FAILURE;
  16513. }
  16514. switch (bl->type) {
  16515. case BL_MOB:
  16516. if (!md) {
  16517. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16518. return SCRIPT_CMD_FAILURE;
  16519. }
  16520. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16521. break;
  16522. case BL_HOM:
  16523. if (!hd) {
  16524. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16525. return SCRIPT_CMD_FAILURE;
  16526. }
  16527. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16528. break;
  16529. case BL_PET:
  16530. if (!pd) {
  16531. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16532. return SCRIPT_CMD_FAILURE;
  16533. }
  16534. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16535. break;
  16536. default:
  16537. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16538. return SCRIPT_CMD_FAILURE;
  16539. }
  16540. clif_name_area(bl); // Send update to client.
  16541. return SCRIPT_CMD_SUCCESS;
  16542. }
  16543. /**
  16544. * Sets a unit's title.
  16545. * setunittitle <GID>,<title>;
  16546. */
  16547. BUILDIN_FUNC(setunittitle)
  16548. {
  16549. int gid = script_getnum(st, 2);
  16550. block_list *bl = map_id2bl(gid);
  16551. if (bl == nullptr) {
  16552. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16553. return SCRIPT_CMD_FAILURE;
  16554. }
  16555. unit_data *ud = unit_bl2ud(bl);
  16556. if (ud == nullptr) {
  16557. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16558. return SCRIPT_CMD_FAILURE;
  16559. }
  16560. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16561. clif_name_area(bl);
  16562. return SCRIPT_CMD_SUCCESS;
  16563. }
  16564. /**
  16565. * Gets a unit's title.
  16566. * getunittitle <GID>;
  16567. */
  16568. BUILDIN_FUNC(getunittitle)
  16569. {
  16570. int gid = script_getnum(st, 2);
  16571. block_list *bl = map_id2bl(gid);
  16572. if (bl == nullptr) {
  16573. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16574. return SCRIPT_CMD_FAILURE;
  16575. }
  16576. unit_data *ud = unit_bl2ud(bl);
  16577. if (ud == nullptr) {
  16578. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16579. return SCRIPT_CMD_FAILURE;
  16580. }
  16581. script_pushstrcopy(st, ud->title);
  16582. return SCRIPT_CMD_SUCCESS;
  16583. }
  16584. /// Makes the unit walk to target position or map.
  16585. /// Returns if it was successful.
  16586. ///
  16587. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16588. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16589. BUILDIN_FUNC(unitwalk)
  16590. {
  16591. struct block_list* bl;
  16592. struct unit_data *ud = NULL;
  16593. const char *cmd = script_getfuncname(st), *done_label = "";
  16594. uint8 off = 5;
  16595. if(!script_rid2bl(2,bl))
  16596. {
  16597. script_pushint(st, 0);
  16598. return SCRIPT_CMD_FAILURE;
  16599. }
  16600. ud = unit_bl2ud(bl);
  16601. if (bl->type == BL_NPC) {
  16602. if (!((TBL_NPC*)bl)->status.hp)
  16603. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16604. else
  16605. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16606. }
  16607. if (!strcmp(cmd,"unitwalk")) {
  16608. int x = script_getnum(st,3);
  16609. int y = script_getnum(st,4);
  16610. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  16611. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  16612. } else {
  16613. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  16614. if (!tbl) {
  16615. ShowError("buildin_unitwalk: Bad target destination.\n");
  16616. script_pushint(st, 0);
  16617. return SCRIPT_CMD_FAILURE;
  16618. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  16619. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  16620. off = 4;
  16621. }
  16622. if (ud && script_hasdata(st, off)) {
  16623. done_label = script_getstr(st, off);
  16624. check_event(st, done_label);
  16625. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  16626. }
  16627. return SCRIPT_CMD_SUCCESS;
  16628. }
  16629. /// Kills the unit.
  16630. ///
  16631. /// unitkill <unit_id>;
  16632. BUILDIN_FUNC(unitkill)
  16633. {
  16634. struct block_list* bl;
  16635. if(script_rid2bl(2,bl))
  16636. status_kill(bl);
  16637. return SCRIPT_CMD_SUCCESS;
  16638. }
  16639. /// Warps the unit to the target position in the target map.
  16640. /// Returns if it was successful.
  16641. ///
  16642. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  16643. BUILDIN_FUNC(unitwarp)
  16644. {
  16645. int map_idx;
  16646. short x;
  16647. short y;
  16648. struct block_list* bl;
  16649. const char *mapname;
  16650. mapname = script_getstr(st, 3);
  16651. x = (short)script_getnum(st,4);
  16652. y = (short)script_getnum(st,5);
  16653. if(!script_rid2bl(2,bl))
  16654. {
  16655. script_pushint(st, 0);
  16656. return SCRIPT_CMD_SUCCESS;
  16657. }
  16658. if (!strcmp(mapname,"this"))
  16659. map_idx = bl?bl->m:-1;
  16660. else
  16661. map_idx = map_mapname2mapid(mapname);
  16662. if (map_idx >= 0 && bl != NULL)
  16663. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  16664. else
  16665. script_pushint(st, 0);
  16666. return SCRIPT_CMD_SUCCESS;
  16667. }
  16668. /// Makes the unit attack the target.
  16669. /// If the unit is a player and <action type> is not 0, it does a continuous
  16670. /// attack instead of a single attack.
  16671. /// Returns if the request was successful.
  16672. ///
  16673. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  16674. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  16675. BUILDIN_FUNC(unitattack)
  16676. {
  16677. struct block_list* unit_bl;
  16678. struct block_list* target_bl = NULL;
  16679. int actiontype = 0;
  16680. if (!script_rid2bl(2,unit_bl)) {
  16681. script_pushint(st, false);
  16682. return SCRIPT_CMD_FAILURE;
  16683. }
  16684. if (script_isstring(st, 3)) {
  16685. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  16686. if( sd != NULL )
  16687. target_bl = &sd->bl;
  16688. } else
  16689. target_bl = map_id2bl(script_getnum(st, 3));
  16690. if (!target_bl) {
  16691. script_pushint(st, false);
  16692. return SCRIPT_CMD_FAILURE;
  16693. }
  16694. if (script_hasdata(st,4))
  16695. actiontype = script_getnum(st,4);
  16696. switch(unit_bl->type) {
  16697. case BL_PC: {
  16698. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  16699. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  16700. script_pushint(st, sd->ud.target == target_bl->id);
  16701. return SCRIPT_CMD_SUCCESS;
  16702. }
  16703. case BL_MOB:
  16704. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  16705. break;
  16706. case BL_PET:
  16707. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  16708. break;
  16709. default:
  16710. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  16711. script_pushint(st, false);
  16712. return SCRIPT_CMD_FAILURE;
  16713. }
  16714. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  16715. return SCRIPT_CMD_SUCCESS;
  16716. }
  16717. /// Makes the unit stop attacking.
  16718. ///
  16719. /// unitstopattack <unit_id>;
  16720. BUILDIN_FUNC(unitstopattack)
  16721. {
  16722. struct block_list* bl;
  16723. if(script_rid2bl(2,bl))
  16724. {
  16725. unit_stop_attack(bl);
  16726. if (bl->type == BL_MOB)
  16727. ((TBL_MOB*)bl)->target_id = 0;
  16728. }
  16729. return SCRIPT_CMD_SUCCESS;
  16730. }
  16731. /// Makes the unit stop walking.
  16732. ///
  16733. /// unitstopwalk <unit_id>{,<flag>};
  16734. BUILDIN_FUNC(unitstopwalk)
  16735. {
  16736. struct block_list* bl;
  16737. int flag = USW_NONE;
  16738. if (script_hasdata(st, 3))
  16739. flag = script_getnum(st, 3);
  16740. if(script_rid2bl(2,bl))
  16741. unit_stop_walking(bl, flag);
  16742. return SCRIPT_CMD_SUCCESS;
  16743. }
  16744. /**
  16745. * Makes the unit say the given message.
  16746. *
  16747. * unittalk <unit_id>,"<message>"{,"<flag>"};
  16748. * @param flag: Specify target
  16749. * bc_area - Message is sent to players in the vicinity of the source (default).
  16750. * bc_self - Message is sent only to player attached.
  16751. */
  16752. BUILDIN_FUNC(unittalk)
  16753. {
  16754. const char* message;
  16755. struct block_list* bl;
  16756. message = script_getstr(st, 3);
  16757. if(script_rid2bl(2,bl))
  16758. {
  16759. send_target target = AREA;
  16760. struct StringBuf sbuf;
  16761. if (script_hasdata(st, 4)) {
  16762. if (script_getnum(st, 4) == BC_SELF) {
  16763. if (map_id2sd(bl->id) == NULL) {
  16764. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  16765. return SCRIPT_CMD_FAILURE;
  16766. }
  16767. target = SELF;
  16768. }
  16769. }
  16770. StringBuf_Init(&sbuf);
  16771. StringBuf_Printf(&sbuf, "%s", message);
  16772. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  16773. StringBuf_Destroy(&sbuf);
  16774. }
  16775. return SCRIPT_CMD_SUCCESS;
  16776. }
  16777. /// Makes the unit do an emotion.
  16778. ///
  16779. /// unitemote <unit_id>,<emotion>;
  16780. ///
  16781. /// @see ET_* in script_constants.hpp
  16782. BUILDIN_FUNC(unitemote)
  16783. {
  16784. int emotion;
  16785. struct block_list* bl;
  16786. emotion = script_getnum(st,3);
  16787. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  16788. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  16789. return SCRIPT_CMD_FAILURE;
  16790. }
  16791. if (script_rid2bl(2,bl))
  16792. clif_emotion(bl, emotion);
  16793. return SCRIPT_CMD_SUCCESS;
  16794. }
  16795. /// Makes the unit cast the skill on the target or self if no target is specified.
  16796. ///
  16797. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  16798. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  16799. BUILDIN_FUNC(unitskilluseid)
  16800. {
  16801. int unit_id, target_id, casttime;
  16802. uint16 skill_id, skill_lv;
  16803. struct block_list* bl;
  16804. unit_id = script_getnum(st,2);
  16805. if (script_isstring(st, 3)) {
  16806. const char *name = script_getstr(st, 3);
  16807. if (!(skill_id = skill_name2id(name))) {
  16808. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  16809. return SCRIPT_CMD_FAILURE;
  16810. }
  16811. } else {
  16812. skill_id = script_getnum(st, 3);
  16813. if (!skill_get_index(skill_id)) {
  16814. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  16815. return SCRIPT_CMD_FAILURE;
  16816. }
  16817. }
  16818. skill_lv = script_getnum(st,4);
  16819. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  16820. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  16821. bool cancel = ( script_hasdata(st,7) ? script_getnum(st,7) > 0 : skill_get_castcancel(skill_id) );
  16822. int msg_id = (script_hasdata(st, 8) ? script_getnum(st, 8) : 0);
  16823. if(script_rid2bl(2,bl)){
  16824. if (msg_id > 0) {
  16825. if (bl->type != BL_MOB) {
  16826. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  16827. return SCRIPT_CMD_FAILURE;
  16828. }
  16829. TBL_MOB* md = map_id2md(bl->id);
  16830. if (md)
  16831. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  16832. }
  16833. if (bl->type == BL_NPC) {
  16834. if (!((TBL_NPC*)bl)->status.hp)
  16835. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16836. else
  16837. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16838. }
  16839. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel);
  16840. }
  16841. return SCRIPT_CMD_SUCCESS;
  16842. }
  16843. /// Makes the unit cast the skill on the target position.
  16844. ///
  16845. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  16846. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  16847. BUILDIN_FUNC(unitskillusepos)
  16848. {
  16849. int skill_x, skill_y, casttime;
  16850. uint16 skill_id, skill_lv;
  16851. struct block_list* bl;
  16852. if (script_isstring(st, 3)) {
  16853. const char *name = script_getstr(st, 3);
  16854. if (!(skill_id = skill_name2id(name))) {
  16855. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  16856. return SCRIPT_CMD_FAILURE;
  16857. }
  16858. } else {
  16859. skill_id = script_getnum(st, 3);
  16860. if (!skill_get_index(skill_id)) {
  16861. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  16862. return SCRIPT_CMD_FAILURE;
  16863. }
  16864. }
  16865. skill_lv = script_getnum(st,4);
  16866. skill_x = script_getnum(st,5);
  16867. skill_y = script_getnum(st,6);
  16868. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16869. bool cancel = ( script_hasdata(st,8) ? script_getnum(st,8) > 0 : skill_get_castcancel(skill_id) );
  16870. int msg_id = (script_hasdata(st, 9) ? script_getnum(st, 9) : 0);
  16871. if(script_rid2bl(2,bl)){
  16872. if (msg_id > 0) {
  16873. if (bl->type != BL_MOB) {
  16874. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  16875. return SCRIPT_CMD_FAILURE;
  16876. }
  16877. TBL_MOB* md = map_id2md(bl->id);
  16878. if (md)
  16879. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  16880. }
  16881. if (bl->type == BL_NPC) {
  16882. if (!((TBL_NPC*)bl)->status.hp)
  16883. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16884. else
  16885. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16886. }
  16887. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel);
  16888. }
  16889. return SCRIPT_CMD_SUCCESS;
  16890. }
  16891. // <--- [zBuffer] List of unit control commands
  16892. /// Pauses the execution of the script, detaching the player
  16893. ///
  16894. /// sleep <milli seconds>;
  16895. BUILDIN_FUNC(sleep)
  16896. {
  16897. // First call(by function call)
  16898. if (st->sleep.tick == 0) {
  16899. int ticks;
  16900. ticks = script_getnum(st, 2);
  16901. if (ticks <= 0) {
  16902. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  16903. return SCRIPT_CMD_FAILURE;
  16904. }
  16905. // detach the player
  16906. script_detach_rid(st);
  16907. // sleep for the target amount of time
  16908. st->state = RERUNLINE;
  16909. st->sleep.tick = ticks;
  16910. // Second call(by timer after sleeping time is over)
  16911. } else {
  16912. // Continue the script
  16913. st->state = RUN;
  16914. st->sleep.tick = 0;
  16915. }
  16916. return SCRIPT_CMD_SUCCESS;
  16917. }
  16918. /// Pauses the execution of the script, keeping the unit attached
  16919. /// Stops the script if no unit is attached
  16920. ///
  16921. /// sleep2(<milli seconds>)
  16922. BUILDIN_FUNC(sleep2)
  16923. {
  16924. // First call(by function call)
  16925. if (st->sleep.tick == 0) {
  16926. int ticks;
  16927. ticks = script_getnum(st, 2);
  16928. if (ticks <= 0) {
  16929. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  16930. return SCRIPT_CMD_FAILURE;
  16931. }
  16932. if (map_id2bl(st->rid) == NULL) {
  16933. ShowError( "buildin_sleep2: no unit is attached\n" );
  16934. return SCRIPT_CMD_FAILURE;
  16935. }
  16936. // sleep for the target amount of time
  16937. st->state = RERUNLINE;
  16938. st->sleep.tick = ticks;
  16939. // Second call(by timer after sleeping time is over)
  16940. } else {
  16941. // Check if the unit is still attached
  16942. // NOTE: This should never happen, since run_script_timer already checks this
  16943. if (map_id2bl(st->rid) == NULL) {
  16944. // The unit is not attached anymore - terminate the script
  16945. st->rid = 0;
  16946. st->state = END;
  16947. } else {
  16948. // The unit is still attached - continue the script
  16949. st->state = RUN;
  16950. st->sleep.tick = 0;
  16951. }
  16952. }
  16953. return SCRIPT_CMD_SUCCESS;
  16954. }
  16955. /// Awakes all the sleep timers of the target npc
  16956. ///
  16957. /// awake "<npc name>";
  16958. BUILDIN_FUNC(awake)
  16959. {
  16960. DBIterator *iter;
  16961. struct script_state *tst;
  16962. struct npc_data* nd;
  16963. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  16964. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  16965. return SCRIPT_CMD_FAILURE;
  16966. }
  16967. int rid = st->rid;
  16968. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  16969. if( rid ){
  16970. script_detach_rid(st);
  16971. }
  16972. iter = db_iterator(st_db);
  16973. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  16974. if (tst->oid == nd->bl.id) {
  16975. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  16976. continue;
  16977. }
  16978. delete_timer(tst->sleep.timer, run_script_timer);
  16979. // Trigger the timer function
  16980. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  16981. }
  16982. }
  16983. dbi_destroy(iter);
  16984. // If a player had been attached, now is the time to restore it
  16985. if( rid ){
  16986. st->rid = rid;
  16987. script_attach_state(st);
  16988. }
  16989. return SCRIPT_CMD_SUCCESS;
  16990. }
  16991. /// Returns a reference to a variable of the target NPC.
  16992. /// Returns 0 if an error occurs.
  16993. ///
  16994. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  16995. BUILDIN_FUNC(getvariableofnpc)
  16996. {
  16997. struct script_data* data;
  16998. const char* name;
  16999. struct npc_data* nd;
  17000. data = script_getdata(st,2);
  17001. if( !data_isreference(data) )
  17002. {// Not a reference (aka varaible name)
  17003. ShowError("buildin_getvariableofnpc: not a variable\n");
  17004. script_reportdata(data);
  17005. script_pushnil(st);
  17006. st->state = END;
  17007. return SCRIPT_CMD_FAILURE;
  17008. }
  17009. name = reference_getname(data);
  17010. if( *name != '.' || name[1] == '@' )
  17011. {// not a npc variable
  17012. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  17013. script_reportdata(data);
  17014. script_pushnil(st);
  17015. st->state = END;
  17016. return SCRIPT_CMD_FAILURE;
  17017. }
  17018. nd = npc_name2id(script_getstr(st,3));
  17019. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  17020. {// NPC not found or has no script
  17021. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  17022. script_pushnil(st);
  17023. st->state = END;
  17024. return SCRIPT_CMD_FAILURE;
  17025. }
  17026. if (!nd->u.scr.script->local.vars)
  17027. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  17028. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  17029. return SCRIPT_CMD_SUCCESS;
  17030. }
  17031. /// Opens a warp portal.
  17032. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  17033. ///
  17034. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  17035. ///
  17036. /// @author blackhole89
  17037. BUILDIN_FUNC(warpportal)
  17038. {
  17039. int spx;
  17040. int spy;
  17041. unsigned short mapindex;
  17042. int tpx;
  17043. int tpy;
  17044. std::shared_ptr<s_skill_unit_group> group;
  17045. struct block_list* bl;
  17046. bl = map_id2bl(st->oid);
  17047. if( bl == NULL ) {
  17048. ShowError("buildin_warpportal: NPC is needed\n");
  17049. return SCRIPT_CMD_FAILURE;
  17050. }
  17051. spx = script_getnum(st,2);
  17052. spy = script_getnum(st,3);
  17053. mapindex = mapindex_name2id(script_getstr(st, 4));
  17054. tpx = script_getnum(st,5);
  17055. tpy = script_getnum(st,6);
  17056. if( mapindex == 0 ) {
  17057. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  17058. return SCRIPT_CMD_FAILURE;
  17059. }
  17060. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  17061. if( group == NULL )
  17062. return SCRIPT_CMD_FAILURE;// failed
  17063. group->val1 = (group->val1<<16)|(short)0;
  17064. group->val2 = (tpx<<16) | tpy;
  17065. group->val3 = mapindex;
  17066. return SCRIPT_CMD_SUCCESS;
  17067. }
  17068. /**
  17069. * openmail({<char_id>});
  17070. **/
  17071. BUILDIN_FUNC(openmail)
  17072. {
  17073. struct map_session_data* sd;
  17074. if (!script_charid2sd(2,sd))
  17075. return SCRIPT_CMD_FAILURE;
  17076. #if PACKETVER < 20150513
  17077. mail_openmail(sd);
  17078. return SCRIPT_CMD_SUCCESS;
  17079. #else
  17080. ShowError( "buildin_openmail: This command is not supported for PACKETVER 2015-05-13 or newer.\n" );
  17081. return SCRIPT_CMD_FAILURE;
  17082. #endif
  17083. }
  17084. /**
  17085. * openauction({<char_id>});
  17086. **/
  17087. BUILDIN_FUNC(openauction)
  17088. {
  17089. TBL_PC* sd;
  17090. if (!script_charid2sd(2,sd))
  17091. return SCRIPT_CMD_FAILURE;
  17092. if( !battle_config.feature_auction ) {
  17093. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  17094. return SCRIPT_CMD_SUCCESS;
  17095. }
  17096. clif_Auction_openwindow(sd);
  17097. return SCRIPT_CMD_SUCCESS;
  17098. }
  17099. /// Retrieves the value of the specified flag of the specified cell.
  17100. ///
  17101. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  17102. ///
  17103. /// @see cell_chk* constants in src/map/script_constants.hpp for the types
  17104. BUILDIN_FUNC(checkcell)
  17105. {
  17106. int16 m = map_mapname2mapid(script_getstr(st,2));
  17107. int16 x = script_getnum(st,3);
  17108. int16 y = script_getnum(st,4);
  17109. cell_chk type = (cell_chk)script_getnum(st,5);
  17110. script_pushint(st, map_getcell(m, x, y, type));
  17111. return SCRIPT_CMD_SUCCESS;
  17112. }
  17113. /// Modifies flags of cells in the specified area.
  17114. ///
  17115. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  17116. ///
  17117. /// @see cell_* constants in src/map/script_constants.hpp for the types
  17118. BUILDIN_FUNC(setcell)
  17119. {
  17120. int16 m = map_mapname2mapid(script_getstr(st,2));
  17121. int16 x1 = script_getnum(st,3);
  17122. int16 y1 = script_getnum(st,4);
  17123. int16 x2 = script_getnum(st,5);
  17124. int16 y2 = script_getnum(st,6);
  17125. cell_t type = (cell_t)script_getnum(st,7);
  17126. bool flag = script_getnum(st,8) != 0;
  17127. int x,y;
  17128. if( x1 > x2 ) SWAP(x1,x2);
  17129. if( y1 > y2 ) SWAP(y1,y2);
  17130. for( y = y1; y <= y2; ++y )
  17131. for( x = x1; x <= x2; ++x )
  17132. map_setcell(m, x, y, type, flag);
  17133. return SCRIPT_CMD_SUCCESS;
  17134. }
  17135. /**
  17136. * Gets a free cell in the specified area.
  17137. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  17138. */
  17139. BUILDIN_FUNC(getfreecell)
  17140. {
  17141. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  17142. struct script_data
  17143. *data_x = script_getdata(st, 3),
  17144. *data_y = script_getdata(st, 4);
  17145. struct block_list* bl = map_id2bl(st->rid);
  17146. struct map_session_data *sd = nullptr;
  17147. int16 m, x = 0, y = 0;
  17148. int rx = -1, ry = -1, flag = 1;
  17149. if (!data_isreference(data_x)) {
  17150. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  17151. return SCRIPT_CMD_FAILURE;
  17152. }
  17153. if (!data_isreference(data_y)) {
  17154. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  17155. return SCRIPT_CMD_FAILURE;
  17156. }
  17157. name_x = reference_getname(data_x),
  17158. name_y = reference_getname(data_y);
  17159. if (is_string_variable(name_x)) {
  17160. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  17161. return SCRIPT_CMD_FAILURE;
  17162. }
  17163. if (is_string_variable(name_y)) {
  17164. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  17165. return SCRIPT_CMD_FAILURE;
  17166. }
  17167. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  17168. if (!script_rid2sd(sd)) {
  17169. if (not_server_variable(*name_x))
  17170. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  17171. else
  17172. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  17173. return SCRIPT_CMD_FAILURE;
  17174. }
  17175. }
  17176. if (script_hasdata(st, 5))
  17177. x = script_getnum(st, 5);
  17178. if (script_hasdata(st, 6))
  17179. y = script_getnum(st, 6);
  17180. if (script_hasdata(st, 7))
  17181. rx = script_getnum(st, 7);
  17182. if (script_hasdata(st, 8))
  17183. ry = script_getnum(st, 8);
  17184. if (script_hasdata(st, 9))
  17185. flag = script_getnum(st, 9);
  17186. if (bl && strcmp(mapn, "this") == 0)
  17187. m = bl->m;
  17188. else
  17189. m = map_mapname2mapid(mapn);
  17190. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  17191. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  17192. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  17193. return SCRIPT_CMD_SUCCESS;
  17194. }
  17195. /*==========================================
  17196. * Mercenary Commands
  17197. *------------------------------------------*/
  17198. BUILDIN_FUNC(mercenary_create)
  17199. {
  17200. struct map_session_data *sd;
  17201. int class_, contract_time;
  17202. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  17203. return SCRIPT_CMD_SUCCESS;
  17204. class_ = script_getnum(st,2);
  17205. if( !mercenary_db.exists(class_) )
  17206. return SCRIPT_CMD_SUCCESS;
  17207. contract_time = script_getnum(st,3);
  17208. mercenary_create(sd, class_, contract_time);
  17209. return SCRIPT_CMD_SUCCESS;
  17210. }
  17211. BUILDIN_FUNC(mercenary_delete)
  17212. {
  17213. struct map_session_data *sd;
  17214. int type = 0;
  17215. if( !script_charid2sd(2, sd) )
  17216. return SCRIPT_CMD_FAILURE;
  17217. if( sd->md == nullptr ) {
  17218. 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);
  17219. return SCRIPT_CMD_FAILURE;
  17220. }
  17221. if( script_hasdata(st, 3) ) {
  17222. type = script_getnum(st, 3);
  17223. if( type < 0 || type > 3 ) {
  17224. ShowWarning("buildin_mercenary_delete: invalid type value of %d.\n", type);
  17225. return SCRIPT_CMD_FAILURE;
  17226. }
  17227. }
  17228. mercenary_delete(sd->md, type);
  17229. return SCRIPT_CMD_SUCCESS;
  17230. }
  17231. BUILDIN_FUNC(mercenary_heal)
  17232. {
  17233. struct map_session_data *sd;
  17234. int hp, sp;
  17235. if( !script_rid2sd(sd) || sd->md == NULL )
  17236. return SCRIPT_CMD_SUCCESS;
  17237. hp = script_getnum(st,2);
  17238. sp = script_getnum(st,3);
  17239. status_heal(&sd->md->bl, hp, sp, 0);
  17240. return SCRIPT_CMD_SUCCESS;
  17241. }
  17242. BUILDIN_FUNC(mercenary_sc_start)
  17243. {
  17244. struct map_session_data *sd;
  17245. enum sc_type type;
  17246. int tick, val1;
  17247. if( !script_rid2sd(sd) || sd->md == NULL )
  17248. return SCRIPT_CMD_SUCCESS;
  17249. type = (sc_type)script_getnum(st,2);
  17250. tick = script_getnum(st,3);
  17251. val1 = script_getnum(st,4);
  17252. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  17253. return SCRIPT_CMD_SUCCESS;
  17254. }
  17255. BUILDIN_FUNC(mercenary_get_calls)
  17256. {
  17257. struct map_session_data *sd;
  17258. int guild;
  17259. if( !script_rid2sd(sd) )
  17260. return SCRIPT_CMD_SUCCESS;
  17261. guild = script_getnum(st,2);
  17262. switch( guild )
  17263. {
  17264. case ARCH_MERC_GUILD:
  17265. script_pushint(st,sd->status.arch_calls);
  17266. break;
  17267. case SPEAR_MERC_GUILD:
  17268. script_pushint(st,sd->status.spear_calls);
  17269. break;
  17270. case SWORD_MERC_GUILD:
  17271. script_pushint(st,sd->status.sword_calls);
  17272. break;
  17273. default:
  17274. script_pushint(st,0);
  17275. break;
  17276. }
  17277. return SCRIPT_CMD_SUCCESS;
  17278. }
  17279. BUILDIN_FUNC(mercenary_set_calls)
  17280. {
  17281. struct map_session_data *sd;
  17282. int guild, value, *calls;
  17283. if( !script_rid2sd(sd) )
  17284. return SCRIPT_CMD_SUCCESS;
  17285. guild = script_getnum(st,2);
  17286. value = script_getnum(st,3);
  17287. switch( guild )
  17288. {
  17289. case ARCH_MERC_GUILD:
  17290. calls = &sd->status.arch_calls;
  17291. break;
  17292. case SPEAR_MERC_GUILD:
  17293. calls = &sd->status.spear_calls;
  17294. break;
  17295. case SWORD_MERC_GUILD:
  17296. calls = &sd->status.sword_calls;
  17297. break;
  17298. default:
  17299. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  17300. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17301. }
  17302. *calls += value;
  17303. *calls = cap_value(*calls, 0, INT_MAX);
  17304. return SCRIPT_CMD_SUCCESS;
  17305. }
  17306. BUILDIN_FUNC(mercenary_get_faith)
  17307. {
  17308. struct map_session_data *sd;
  17309. int guild;
  17310. if( !script_rid2sd(sd) )
  17311. return SCRIPT_CMD_SUCCESS;
  17312. guild = script_getnum(st,2);
  17313. switch( guild )
  17314. {
  17315. case ARCH_MERC_GUILD:
  17316. script_pushint(st,sd->status.arch_faith);
  17317. break;
  17318. case SPEAR_MERC_GUILD:
  17319. script_pushint(st,sd->status.spear_faith);
  17320. break;
  17321. case SWORD_MERC_GUILD:
  17322. script_pushint(st,sd->status.sword_faith);
  17323. break;
  17324. default:
  17325. script_pushint(st,0);
  17326. break;
  17327. }
  17328. return SCRIPT_CMD_SUCCESS;
  17329. }
  17330. BUILDIN_FUNC(mercenary_set_faith)
  17331. {
  17332. struct map_session_data *sd;
  17333. int guild, value, *calls;
  17334. if( !script_rid2sd(sd) )
  17335. return SCRIPT_CMD_SUCCESS;
  17336. guild = script_getnum(st,2);
  17337. value = script_getnum(st,3);
  17338. switch( guild )
  17339. {
  17340. case ARCH_MERC_GUILD:
  17341. calls = &sd->status.arch_faith;
  17342. break;
  17343. case SPEAR_MERC_GUILD:
  17344. calls = &sd->status.spear_faith;
  17345. break;
  17346. case SWORD_MERC_GUILD:
  17347. calls = &sd->status.sword_faith;
  17348. break;
  17349. default:
  17350. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17351. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17352. }
  17353. *calls += value;
  17354. *calls = cap_value(*calls, 0, INT_MAX);
  17355. if( mercenary_get_guild(sd->md) == guild )
  17356. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17357. return SCRIPT_CMD_SUCCESS;
  17358. }
  17359. /*------------------------------------------
  17360. * Book Reading
  17361. *------------------------------------------*/
  17362. BUILDIN_FUNC(readbook)
  17363. {
  17364. struct map_session_data *sd;
  17365. int book_id, page;
  17366. if( !script_rid2sd(sd) )
  17367. return SCRIPT_CMD_SUCCESS;
  17368. book_id = script_getnum(st,2);
  17369. page = script_getnum(st,3);
  17370. clif_readbook(sd->fd, book_id, page);
  17371. return SCRIPT_CMD_SUCCESS;
  17372. }
  17373. /******************
  17374. Questlog script commands
  17375. *******************/
  17376. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17377. BUILDIN_FUNC(questinfo)
  17378. {
  17379. TBL_NPC* nd = map_id2nd(st->oid);
  17380. if (!nd || nd->bl.m == -1) {
  17381. ShowError("buildin_questinfo: No NPC attached.\n");
  17382. return SCRIPT_CMD_FAILURE;
  17383. }
  17384. int icon = script_getnum(st, 2);
  17385. #if PACKETVER >= 20120410
  17386. switch(icon){
  17387. case QTYPE_QUEST:
  17388. case QTYPE_QUEST2:
  17389. case QTYPE_JOB:
  17390. case QTYPE_JOB2:
  17391. case QTYPE_EVENT:
  17392. case QTYPE_EVENT2:
  17393. // Warg icons were replaced in this client
  17394. #if PACKETVER < 20170315
  17395. case QTYPE_WARG:
  17396. case QTYPE_WARG2:
  17397. #else
  17398. case QTYPE_CLICKME:
  17399. case QTYPE_DAILYQUEST:
  17400. case QTYPE_EVENT3:
  17401. case QTYPE_JOBQUEST:
  17402. case QTYPE_JUMPING_PORING:
  17403. #endif
  17404. // Leave everything as it is
  17405. break;
  17406. case QTYPE_NONE:
  17407. default:
  17408. // Default to nothing if icon id is invalid.
  17409. icon = QTYPE_NONE;
  17410. break;
  17411. }
  17412. #else
  17413. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17414. icon = QTYPE_QUEST;
  17415. else
  17416. icon = icon + 1;
  17417. #endif
  17418. int color = QMARK_NONE;
  17419. if (script_hasdata(st, 3)) {
  17420. color = script_getnum(st, 3);
  17421. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17422. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17423. script_reportfunc(st);
  17424. color = QMARK_NONE;
  17425. }
  17426. }
  17427. struct script_code *script = nullptr;
  17428. if (script_hasdata(st, 4)) {
  17429. const char *str = script_getstr(st, 4);
  17430. if (str) {
  17431. std::string condition(str);
  17432. if (condition.find( "achievement_condition" ) == std::string::npos)
  17433. condition = "achievement_condition( " + condition + " );";
  17434. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17435. if (!script) {
  17436. st->state = END;
  17437. return SCRIPT_CMD_FAILURE;
  17438. }
  17439. }
  17440. }
  17441. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17442. qi->icon = static_cast<e_questinfo_types>(icon);
  17443. qi->color = static_cast<e_questinfo_markcolor>(color);
  17444. qi->condition = script;
  17445. nd->qi_data.push_back(qi);
  17446. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17447. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17448. mapdata->qi_npc.push_back(nd->bl.id);
  17449. return SCRIPT_CMD_SUCCESS;
  17450. }
  17451. /**
  17452. * questinfo_refresh {<char_id>};
  17453. **/
  17454. BUILDIN_FUNC(questinfo_refresh)
  17455. {
  17456. struct map_session_data *sd;
  17457. if (!script_charid2sd(2, sd))
  17458. return SCRIPT_CMD_FAILURE;
  17459. pc_show_questinfo(sd);
  17460. return SCRIPT_CMD_SUCCESS;
  17461. }
  17462. /**
  17463. * setquest <ID>{,<char_id>};
  17464. **/
  17465. BUILDIN_FUNC(setquest)
  17466. {
  17467. struct map_session_data *sd;
  17468. int quest_id;
  17469. quest_id = script_getnum(st, 2);
  17470. if (!script_charid2sd(3,sd))
  17471. return SCRIPT_CMD_FAILURE;
  17472. if( quest_add(sd, quest_id) == -1 ){
  17473. script_reportsrc(st);
  17474. script_reportfunc(st);
  17475. }
  17476. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17477. pc_show_questinfo(sd);
  17478. return SCRIPT_CMD_SUCCESS;
  17479. }
  17480. /**
  17481. * erasequest <ID>{,<char_id>};
  17482. **/
  17483. BUILDIN_FUNC(erasequest)
  17484. {
  17485. struct map_session_data *sd;
  17486. if (!script_charid2sd(3,sd))
  17487. return SCRIPT_CMD_FAILURE;
  17488. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17489. script_reportsrc(st);
  17490. script_reportfunc(st);
  17491. }
  17492. return SCRIPT_CMD_SUCCESS;
  17493. }
  17494. /**
  17495. * completequest <ID>{,<char_id>};
  17496. **/
  17497. BUILDIN_FUNC(completequest)
  17498. {
  17499. struct map_session_data *sd;
  17500. if (!script_charid2sd(3,sd))
  17501. return SCRIPT_CMD_FAILURE;
  17502. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17503. //20120410 or 20090218
  17504. pc_show_questinfo(sd);
  17505. return SCRIPT_CMD_SUCCESS;
  17506. }
  17507. /**
  17508. * changequest <ID>,<ID2>{,<char_id>};
  17509. **/
  17510. BUILDIN_FUNC(changequest)
  17511. {
  17512. struct map_session_data *sd;
  17513. if (!script_charid2sd(4,sd))
  17514. return SCRIPT_CMD_FAILURE;
  17515. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17516. script_reportsrc(st);
  17517. script_reportfunc(st);
  17518. }
  17519. //20120410 or 20090218
  17520. pc_show_questinfo(sd);
  17521. return SCRIPT_CMD_SUCCESS;
  17522. }
  17523. /**
  17524. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17525. **/
  17526. BUILDIN_FUNC(checkquest)
  17527. {
  17528. struct map_session_data *sd;
  17529. e_quest_check_type type = HAVEQUEST;
  17530. if( script_hasdata(st, 3) )
  17531. type = (e_quest_check_type)script_getnum(st, 3);
  17532. if (!script_charid2sd(4,sd))
  17533. return SCRIPT_CMD_FAILURE;
  17534. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17535. return SCRIPT_CMD_SUCCESS;
  17536. }
  17537. /**
  17538. * isbegin_quest(<ID>{,<char_id>})
  17539. **/
  17540. BUILDIN_FUNC(isbegin_quest)
  17541. {
  17542. struct map_session_data *sd;
  17543. if (!script_charid2sd(3,sd))
  17544. return SCRIPT_CMD_FAILURE;
  17545. int i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17546. script_pushint(st, i + (i < 1));
  17547. return SCRIPT_CMD_SUCCESS;
  17548. }
  17549. /**
  17550. * showevent <icon>{,<mark color>{,<char_id>}}
  17551. **/
  17552. BUILDIN_FUNC(showevent)
  17553. {
  17554. TBL_PC *sd;
  17555. if (!script_charid2sd(4,sd))
  17556. return SCRIPT_CMD_FAILURE;
  17557. struct npc_data *nd = map_id2nd(st->oid);
  17558. if (!nd)
  17559. return SCRIPT_CMD_SUCCESS;
  17560. int color = QMARK_NONE;
  17561. int icon = script_getnum(st, 2);
  17562. if (script_hasdata(st, 3)) {
  17563. color = script_getnum(st, 3);
  17564. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17565. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17566. script_reportfunc(st);
  17567. color = QMARK_NONE;
  17568. }
  17569. }
  17570. #if PACKETVER >= 20120410
  17571. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17572. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17573. #else
  17574. if (icon < 0 || icon > 7)
  17575. icon = 0;
  17576. else
  17577. icon = icon + 1;
  17578. #endif
  17579. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17580. return SCRIPT_CMD_SUCCESS;
  17581. }
  17582. /*==========================================
  17583. * BattleGround System
  17584. *------------------------------------------*/
  17585. BUILDIN_FUNC(waitingroom2bg)
  17586. {
  17587. struct npc_data *nd;
  17588. struct chat_data *cd;
  17589. const char *map_name;
  17590. int mapindex = 0, bg_id;
  17591. unsigned char i,c=0;
  17592. struct s_battleground_team team;
  17593. if( script_hasdata(st,7) )
  17594. nd = npc_name2id(script_getstr(st,7));
  17595. else
  17596. nd = (struct npc_data *)map_id2bl(st->oid);
  17597. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  17598. {
  17599. script_pushint(st,0);
  17600. return SCRIPT_CMD_SUCCESS;
  17601. }
  17602. map_name = script_getstr(st,2);
  17603. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17604. { // Invalid Map
  17605. script_pushint(st, 0);
  17606. return SCRIPT_CMD_SUCCESS;
  17607. }
  17608. team.warp_x = script_getnum(st,3);
  17609. team.warp_y = script_getnum(st,4);
  17610. if (script_hasdata(st,5)) {
  17611. team.quit_event = script_getstr(st,5); // Logout Event
  17612. check_event(st, team.quit_event.c_str());
  17613. } else
  17614. team.quit_event = "";
  17615. if (script_hasdata(st,6)) {
  17616. team.death_event = script_getstr(st,6); // Die Event
  17617. check_event(st, team.death_event.c_str());
  17618. } else
  17619. team.death_event = "";
  17620. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  17621. { // Creation failed
  17622. script_pushint(st,0);
  17623. return SCRIPT_CMD_SUCCESS;
  17624. }
  17625. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  17626. struct map_session_data *sd;
  17627. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd, false) ){
  17628. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  17629. ++c;
  17630. }
  17631. }
  17632. mapreg_setreg(add_str("$@arenamembersnum"), c);
  17633. script_pushint(st,bg_id);
  17634. return SCRIPT_CMD_SUCCESS;
  17635. }
  17636. BUILDIN_FUNC(waitingroom2bg_single)
  17637. {
  17638. const char* map_name;
  17639. struct npc_data *nd;
  17640. struct chat_data *cd;
  17641. struct map_session_data *sd;
  17642. int x, y, mapindex, bg_id = script_getnum(st,2);
  17643. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17644. if (!bg) {
  17645. script_pushint(st, false);
  17646. return SCRIPT_CMD_SUCCESS;
  17647. }
  17648. if (script_hasdata(st, 3)) {
  17649. map_name = script_getstr(st, 3);
  17650. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17651. script_pushint(st, false);
  17652. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17653. }
  17654. x = script_getnum(st, 4);
  17655. y = script_getnum(st, 5);
  17656. }
  17657. else {
  17658. mapindex = bg->cemetery.map;
  17659. x = bg->cemetery.x;
  17660. y = bg->cemetery.y;
  17661. }
  17662. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17663. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17664. script_pushint(st, false);
  17665. return SCRIPT_CMD_FAILURE;
  17666. }
  17667. nd = npc_name2id(script_getstr(st,6));
  17668. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  17669. return SCRIPT_CMD_SUCCESS;
  17670. if( (sd = cd->usersd[0]) == NULL )
  17671. return SCRIPT_CMD_SUCCESS;
  17672. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17673. {
  17674. script_pushint(st, true);
  17675. }
  17676. else
  17677. script_pushint(st, false);
  17678. return SCRIPT_CMD_SUCCESS;
  17679. }
  17680. /// Creates an instance of battleground battle group.
  17681. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  17682. /// @author [secretdataz]
  17683. BUILDIN_FUNC(bg_create) {
  17684. const char *map_name;
  17685. int mapindex = 0;
  17686. struct s_battleground_team team;
  17687. map_name = script_getstr(st, 2);
  17688. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17689. { // Invalid Map
  17690. script_pushint(st, 0);
  17691. return SCRIPT_CMD_SUCCESS;
  17692. }
  17693. team.warp_x = script_getnum(st,3);
  17694. team.warp_y = script_getnum(st,4);
  17695. if (script_hasdata(st,5)) {
  17696. team.quit_event = script_getstr(st,5); // Logout Event
  17697. check_event(st, team.quit_event.c_str());
  17698. } else
  17699. team.quit_event = "";
  17700. if (script_hasdata(st,6)) {
  17701. team.death_event = script_getstr(st,6); // Die Event
  17702. check_event(st, team.death_event.c_str());
  17703. } else
  17704. team.death_event = "";
  17705. script_pushint(st, bg_create(mapindex, &team));
  17706. return SCRIPT_CMD_SUCCESS;
  17707. }
  17708. /// Adds attached player or <char id> (if specified) to an existing
  17709. /// battleground group and warps it to the specified coordinates on
  17710. /// the given map.
  17711. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  17712. /// @author [secretdataz]
  17713. BUILDIN_FUNC(bg_join) {
  17714. const char* map_name;
  17715. struct map_session_data *sd;
  17716. int x, y, mapindex, bg_id = script_getnum(st, 2);
  17717. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17718. if (!bg) {
  17719. script_pushint(st, false);
  17720. return SCRIPT_CMD_SUCCESS;
  17721. }
  17722. if (script_hasdata(st, 3)) {
  17723. map_name = script_getstr(st, 3);
  17724. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17725. script_pushint(st, false);
  17726. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17727. }
  17728. x = script_getnum(st, 4);
  17729. y = script_getnum(st, 5);
  17730. } else {
  17731. mapindex = bg->cemetery.map;
  17732. x = bg->cemetery.x;
  17733. y = bg->cemetery.y;
  17734. }
  17735. if (!script_charid2sd(6, sd)) {
  17736. script_pushint(st, false);
  17737. return SCRIPT_CMD_FAILURE;
  17738. }
  17739. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17740. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17741. script_pushint(st, false);
  17742. return SCRIPT_CMD_FAILURE;
  17743. }
  17744. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17745. {
  17746. script_pushint(st, true);
  17747. }
  17748. else
  17749. script_pushint(st, false);
  17750. return SCRIPT_CMD_SUCCESS;
  17751. }
  17752. BUILDIN_FUNC(bg_team_setxy)
  17753. {
  17754. int bg_id = script_getnum(st,2);
  17755. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17756. if (bg) {
  17757. bg->cemetery.x = script_getnum(st, 3);
  17758. bg->cemetery.y = script_getnum(st, 4);
  17759. }
  17760. return SCRIPT_CMD_SUCCESS;
  17761. }
  17762. BUILDIN_FUNC(bg_warp)
  17763. {
  17764. int x, y, mapindex, bg_id;
  17765. const char* map_name;
  17766. bg_id = script_getnum(st,2);
  17767. map_name = script_getstr(st,3);
  17768. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  17769. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17770. x = script_getnum(st,4);
  17771. y = script_getnum(st,5);
  17772. bg_team_warp(bg_id, mapindex, x, y);
  17773. return SCRIPT_CMD_SUCCESS;
  17774. }
  17775. BUILDIN_FUNC(bg_monster)
  17776. {
  17777. int class_ = 0, x = 0, y = 0, bg_id = 0;
  17778. const char *str,*mapname, *evt="";
  17779. bg_id = script_getnum(st,2);
  17780. mapname = script_getstr(st,3);
  17781. x = script_getnum(st,4);
  17782. y = script_getnum(st,5);
  17783. str = script_getstr(st,6);
  17784. class_ = script_getnum(st,7);
  17785. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  17786. check_event(st, evt);
  17787. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  17788. return SCRIPT_CMD_SUCCESS;
  17789. }
  17790. BUILDIN_FUNC(bg_monster_set_team)
  17791. {
  17792. struct mob_data *md;
  17793. struct block_list *mbl;
  17794. int id = script_getnum(st,2),
  17795. bg_id = script_getnum(st,3);
  17796. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  17797. return SCRIPT_CMD_SUCCESS;
  17798. md = (TBL_MOB *)mbl;
  17799. md->bg_id = bg_id;
  17800. mob_stop_attack(md);
  17801. mob_stop_walking(md, 0);
  17802. md->target_id = md->attacked_id = 0;
  17803. clif_name_area(&md->bl);
  17804. return SCRIPT_CMD_SUCCESS;
  17805. }
  17806. BUILDIN_FUNC(bg_leave)
  17807. {
  17808. struct map_session_data *sd = NULL;
  17809. bool deserter = false;
  17810. if( !script_charid2sd(2,sd) || !sd->bg_id )
  17811. return SCRIPT_CMD_SUCCESS;
  17812. if (!strcmp(script_getfuncname(st), "bg_desert"))
  17813. deserter = true;
  17814. bg_team_leave(sd, false, deserter);
  17815. return SCRIPT_CMD_SUCCESS;
  17816. }
  17817. BUILDIN_FUNC(bg_destroy)
  17818. {
  17819. int bg_id = script_getnum(st,2);
  17820. bg_team_delete(bg_id);
  17821. return SCRIPT_CMD_SUCCESS;
  17822. }
  17823. BUILDIN_FUNC(bg_getareausers)
  17824. {
  17825. const char *str = script_getstr(st, 3);
  17826. int16 m, x0, y0, x1, y1;
  17827. int bg_id = script_getnum(st, 2), c = 0;
  17828. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17829. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  17830. script_pushint(st,0);
  17831. return SCRIPT_CMD_SUCCESS;
  17832. }
  17833. x0 = script_getnum(st,4);
  17834. y0 = script_getnum(st,5);
  17835. x1 = script_getnum(st,6);
  17836. y1 = script_getnum(st,7);
  17837. for (const auto &member : bg->members) {
  17838. 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 )
  17839. continue;
  17840. c++;
  17841. }
  17842. script_pushint(st,c);
  17843. return SCRIPT_CMD_SUCCESS;
  17844. }
  17845. BUILDIN_FUNC(bg_updatescore)
  17846. {
  17847. const char *str;
  17848. int16 m;
  17849. str = script_getstr(st,2);
  17850. if( (m = map_mapname2mapid(str)) < 0 )
  17851. return SCRIPT_CMD_SUCCESS;
  17852. struct map_data *mapdata = map_getmapdata(m);
  17853. mapdata->bgscore_lion = script_getnum(st,3);
  17854. mapdata->bgscore_eagle = script_getnum(st,4);
  17855. clif_bg_updatescore(m);
  17856. return SCRIPT_CMD_SUCCESS;
  17857. }
  17858. BUILDIN_FUNC(bg_get_data)
  17859. {
  17860. int bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  17861. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17862. if (bg) {
  17863. switch (type) {
  17864. case 0:
  17865. script_pushint(st, bg->members.size());
  17866. break;
  17867. case 1:
  17868. for (const auto &member : bg->members)
  17869. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  17870. mapreg_setreg(add_str("$@arenamemberscount"), i);
  17871. script_pushint(st, i);
  17872. break;
  17873. default:
  17874. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  17875. break;
  17876. }
  17877. } else
  17878. script_pushint(st, 0);
  17879. return SCRIPT_CMD_SUCCESS;
  17880. }
  17881. /**
  17882. * Reserves a slot for the given Battleground.
  17883. * bg_reserve("<battleground_map_name>"{,<ended>});
  17884. */
  17885. BUILDIN_FUNC(bg_reserve)
  17886. {
  17887. const char *str = script_getstr(st, 2);
  17888. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  17889. if (!bg_queue_reserve(str, ended))
  17890. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  17891. return SCRIPT_CMD_SUCCESS;
  17892. }
  17893. /**
  17894. * Removes a spot for the given Battleground.
  17895. * bg_unbook("<battleground_map_name>");
  17896. */
  17897. BUILDIN_FUNC(bg_unbook)
  17898. {
  17899. const char *str = script_getstr(st, 2);
  17900. if (!bg_queue_unbook(str))
  17901. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  17902. return SCRIPT_CMD_SUCCESS;
  17903. }
  17904. /**
  17905. * Gets battleground database information.
  17906. * bg_info("<battleground name>", <type>);
  17907. */
  17908. BUILDIN_FUNC(bg_info)
  17909. {
  17910. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  17911. if (!bg) {
  17912. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  17913. return SCRIPT_CMD_FAILURE;
  17914. }
  17915. int type = script_getnum(st, 3);
  17916. switch (type) {
  17917. case BG_INFO_ID:
  17918. script_pushint(st, bg->id);
  17919. break;
  17920. case BG_INFO_REQUIRED_PLAYERS:
  17921. script_pushint(st, bg->required_players);
  17922. break;
  17923. case BG_INFO_MAX_PLAYERS:
  17924. script_pushint(st, bg->max_players);
  17925. break;
  17926. case BG_INFO_MIN_LEVEL:
  17927. script_pushint(st, bg->min_lvl);
  17928. break;
  17929. case BG_INFO_MAX_LEVEL:
  17930. script_pushint(st, bg->max_lvl);
  17931. break;
  17932. case BG_INFO_MAPS: {
  17933. size_t i;
  17934. for (i = 0; i < bg->maps.size(); i++)
  17935. setd_sub_str(st, nullptr, ".@bgmaps$", i, mapindex_id2name(bg->maps[i].mapindex), nullptr);
  17936. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  17937. script_pushint(st, i);
  17938. break;
  17939. }
  17940. case BG_INFO_DESERTER_TIME:
  17941. script_pushint(st, bg->deserter_time);
  17942. break;
  17943. default:
  17944. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  17945. return SCRIPT_CMD_FAILURE;
  17946. }
  17947. return SCRIPT_CMD_SUCCESS;
  17948. }
  17949. /*==========================================
  17950. * Instancing System
  17951. *------------------------------------------*/
  17952. /**
  17953. * Returns an Instance ID.
  17954. * @param st: Script state
  17955. * @param mode: Instance mode
  17956. * @return instance ID on success or 0 otherwise
  17957. */
  17958. int script_instancegetid(struct script_state* st, e_instance_mode mode)
  17959. {
  17960. int instance_id = 0;
  17961. if (mode == IM_NONE) {
  17962. struct npc_data *nd = map_id2nd(st->oid);
  17963. if (nd->instance_id > 0)
  17964. instance_id = nd->instance_id;
  17965. } else {
  17966. struct map_session_data *sd = map_id2sd(st->rid);
  17967. if (sd) {
  17968. switch (mode) {
  17969. case IM_CHAR:
  17970. if (sd->instance_id > 0)
  17971. instance_id = sd->instance_id;
  17972. break;
  17973. case IM_PARTY: {
  17974. struct party_data *pd = party_search(sd->status.party_id);
  17975. if (pd && pd->instance_id > 0)
  17976. instance_id = pd->instance_id;
  17977. }
  17978. break;
  17979. case IM_GUILD: {
  17980. struct guild *gd = guild_search(sd->status.guild_id);
  17981. if (gd && gd->instance_id > 0)
  17982. instance_id = gd->instance_id;
  17983. }
  17984. break;
  17985. case IM_CLAN: {
  17986. struct clan *cd = clan_search(sd->status.clan_id);
  17987. if (cd && cd->instance_id > 0)
  17988. instance_id = cd->instance_id;
  17989. }
  17990. break;
  17991. default: // Unsupported type
  17992. break;
  17993. }
  17994. }
  17995. }
  17996. return instance_id;
  17997. }
  17998. /*==========================================
  17999. * Creates the instance
  18000. * Returns Instance ID if created successfully
  18001. *------------------------------------------*/
  18002. BUILDIN_FUNC(instance_create)
  18003. {
  18004. e_instance_mode mode = IM_PARTY;
  18005. int owner_id = 0;
  18006. if (script_hasdata(st, 3)) {
  18007. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18008. if (mode < IM_NONE || mode >= IM_MAX) {
  18009. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  18010. return SCRIPT_CMD_FAILURE;
  18011. }
  18012. }
  18013. if (script_hasdata(st, 4))
  18014. owner_id = script_getnum(st, 4);
  18015. else {
  18016. // If sd is NULL, instance_create will return -2.
  18017. struct map_session_data *sd = NULL;
  18018. switch(mode) {
  18019. case IM_NONE:
  18020. owner_id = st->oid;
  18021. break;
  18022. case IM_CHAR:
  18023. if (script_rid2sd(sd))
  18024. owner_id = sd->status.char_id;
  18025. break;
  18026. case IM_PARTY:
  18027. if (script_rid2sd(sd))
  18028. owner_id = sd->status.party_id;
  18029. break;
  18030. case IM_GUILD:
  18031. if (script_rid2sd(sd))
  18032. owner_id = sd->status.guild_id;
  18033. break;
  18034. case IM_CLAN:
  18035. if (script_rid2sd(sd))
  18036. owner_id = sd->status.clan_id;
  18037. break;
  18038. default:
  18039. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  18040. return SCRIPT_CMD_FAILURE;
  18041. }
  18042. }
  18043. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  18044. return SCRIPT_CMD_SUCCESS;
  18045. }
  18046. /*==========================================
  18047. * Destroys an instance (unofficial)
  18048. * Officially instances are only destroyed by timeout
  18049. *
  18050. * instance_destroy {<instance_id>};
  18051. *------------------------------------------*/
  18052. BUILDIN_FUNC(instance_destroy)
  18053. {
  18054. int instance_id;
  18055. if( script_hasdata(st,2) )
  18056. instance_id = script_getnum(st,2);
  18057. else
  18058. instance_id = script_instancegetid(st);
  18059. if( instance_id == 0 ) {
  18060. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  18061. return SCRIPT_CMD_FAILURE;
  18062. }
  18063. instance_destroy(instance_id);
  18064. return SCRIPT_CMD_SUCCESS;
  18065. }
  18066. /*==========================================
  18067. * Warps player to instance
  18068. * Results:
  18069. * IE_OK: Success
  18070. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  18071. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  18072. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  18073. *------------------------------------------*/
  18074. BUILDIN_FUNC(instance_enter)
  18075. {
  18076. struct map_session_data *sd = NULL;
  18077. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  18078. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  18079. int instance_id;
  18080. if (script_hasdata(st, 6))
  18081. instance_id = script_getnum(st, 6);
  18082. else
  18083. instance_id = script_instancegetid(st, IM_PARTY);
  18084. if (!script_charid2sd(5,sd))
  18085. return SCRIPT_CMD_FAILURE;
  18086. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  18087. return SCRIPT_CMD_SUCCESS;
  18088. }
  18089. /*==========================================
  18090. * Returns the name of a duplicated NPC
  18091. *
  18092. * instance_npcname <npc_name>{,<instance_id>};
  18093. * <npc_name> is the full name of an NPC.
  18094. *------------------------------------------*/
  18095. BUILDIN_FUNC(instance_npcname)
  18096. {
  18097. const char *str;
  18098. int instance_id;
  18099. struct npc_data *nd;
  18100. str = script_getstr(st,2);
  18101. if( script_hasdata(st,3) )
  18102. instance_id = script_getnum(st,3);
  18103. else
  18104. instance_id = script_instancegetid(st);
  18105. if( instance_id > 0 && (nd = npc_name2id(str)) != NULL ) {
  18106. static char npcname[NAME_LENGTH];
  18107. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  18108. script_pushconststr(st,npcname);
  18109. } else {
  18110. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  18111. st->state = END;
  18112. return SCRIPT_CMD_FAILURE;
  18113. }
  18114. return SCRIPT_CMD_SUCCESS;
  18115. }
  18116. /*==========================================
  18117. * Returns the name of a duplicated map
  18118. *
  18119. * instance_mapname <map_name>{,<instance_id>};
  18120. *------------------------------------------*/
  18121. BUILDIN_FUNC(instance_mapname)
  18122. {
  18123. const char *str;
  18124. int16 m;
  18125. int instance_id;
  18126. str = script_getstr(st,2);
  18127. if( script_hasdata(st,3) )
  18128. instance_id = script_getnum(st,3);
  18129. else
  18130. instance_id = script_instancegetid(st);
  18131. // Check that instance mapname is a valid map
  18132. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  18133. script_pushconststr(st, "");
  18134. else
  18135. script_pushconststr(st, map_getmapdata(m)->name);
  18136. return SCRIPT_CMD_SUCCESS;
  18137. }
  18138. /*==========================================
  18139. * Returns an Instance ID
  18140. *------------------------------------------*/
  18141. BUILDIN_FUNC(instance_id)
  18142. {
  18143. int mode = IM_NONE; // Default to the attached NPC
  18144. if (script_hasdata(st, 2)) {
  18145. mode = script_getnum(st, 2);
  18146. if (mode <= IM_NONE || mode >= IM_MAX) {
  18147. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  18148. script_pushint(st, 0);
  18149. return SCRIPT_CMD_SUCCESS;
  18150. }
  18151. }
  18152. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  18153. return SCRIPT_CMD_SUCCESS;
  18154. }
  18155. /*==========================================
  18156. * Warps all players inside an instance
  18157. *
  18158. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  18159. *------------------------------------------*/
  18160. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  18161. {
  18162. unsigned int m = va_arg(ap,unsigned int);
  18163. int x = va_arg(ap,int);
  18164. int y = va_arg(ap,int);
  18165. int instance_id = va_arg(ap, int);
  18166. struct map_session_data *sd;
  18167. nullpo_retr(0, bl);
  18168. if (bl->type != BL_PC)
  18169. return 0;
  18170. sd = (TBL_PC *)bl;
  18171. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18172. if (!idata)
  18173. return 0;
  18174. int owner_id = idata->owner_id;
  18175. switch(idata->mode) {
  18176. case IM_NONE:
  18177. break;
  18178. case IM_CHAR:
  18179. if (sd->status.char_id != owner_id)
  18180. return 0;
  18181. break;
  18182. case IM_PARTY:
  18183. if (sd->status.party_id != owner_id)
  18184. return 0;
  18185. break;
  18186. case IM_GUILD:
  18187. if (sd->status.guild_id != owner_id)
  18188. return 0;
  18189. case IM_CLAN:
  18190. if (sd->status.clan_id != owner_id)
  18191. return 0;
  18192. }
  18193. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  18194. return 1;
  18195. }
  18196. BUILDIN_FUNC(instance_warpall)
  18197. {
  18198. int16 m;
  18199. int instance_id;
  18200. const char *mapn;
  18201. int x, y;
  18202. mapn = script_getstr(st,2);
  18203. x = script_getnum(st,3);
  18204. y = script_getnum(st,4);
  18205. if( script_hasdata(st,5) )
  18206. instance_id = script_getnum(st,5);
  18207. else
  18208. instance_id = script_instancegetid(st, IM_PARTY);
  18209. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0)
  18210. return SCRIPT_CMD_FAILURE;
  18211. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18212. if (!idata) {
  18213. ShowError("buildin_instance_warpall: Instance is not found.\n");
  18214. return SCRIPT_CMD_FAILURE;
  18215. }
  18216. for(const auto &it : idata->map)
  18217. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id);
  18218. return SCRIPT_CMD_SUCCESS;
  18219. }
  18220. /*==========================================
  18221. * Broadcasts to all maps inside an instance
  18222. *
  18223. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  18224. * Using 0 for <instance id> will auto-detect the id.
  18225. *------------------------------------------*/
  18226. BUILDIN_FUNC(instance_announce) {
  18227. int instance_id = script_getnum(st,2);
  18228. const char *mes = script_getstr(st,3);
  18229. int flag = script_getnum(st,4);
  18230. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  18231. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  18232. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  18233. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  18234. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  18235. if (instance_id <= 0)
  18236. instance_id = script_instancegetid(st);
  18237. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18238. if (instance_id <= 0 || !idata) {
  18239. ShowError("buildin_instance_announce: Instance not found.\n");
  18240. return SCRIPT_CMD_FAILURE;
  18241. }
  18242. for (const auto &it : idata->map)
  18243. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  18244. return SCRIPT_CMD_SUCCESS;
  18245. }
  18246. /*==========================================
  18247. * instance_check_party [malufett]
  18248. * Values:
  18249. * party_id : Party ID of the invoking character. [Required Parameter]
  18250. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  18251. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18252. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18253. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  18254. * Example 2: instance_check_party (getcharid(1),1,1,99);
  18255. *------------------------------------------*/
  18256. BUILDIN_FUNC(instance_check_party)
  18257. {
  18258. int amount, min, max, i, party_id, c = 0;
  18259. struct party_data *p;
  18260. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  18261. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18262. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18263. if( min < 1 || min > MAX_LEVEL) {
  18264. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  18265. return SCRIPT_CMD_FAILURE;
  18266. } else if( max < 1 || max > MAX_LEVEL) {
  18267. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  18268. return SCRIPT_CMD_FAILURE;
  18269. }
  18270. if( script_hasdata(st,2) )
  18271. party_id = script_getnum(st,2);
  18272. else return SCRIPT_CMD_FAILURE;
  18273. if( !(p = party_search(party_id)) ) {
  18274. script_pushint(st, 0); // Returns false if party does not exist.
  18275. return SCRIPT_CMD_FAILURE;
  18276. }
  18277. for( i = 0; i < MAX_PARTY; i++ ) {
  18278. struct map_session_data *pl_sd;
  18279. if( (pl_sd = p->data[i].sd) )
  18280. if(map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18281. if(pl_sd->status.base_level < min) {
  18282. script_pushint(st, 0);
  18283. return SCRIPT_CMD_SUCCESS;
  18284. } else if(pl_sd->status.base_level > max) {
  18285. script_pushint(st, 0);
  18286. return SCRIPT_CMD_SUCCESS;
  18287. }
  18288. c++;
  18289. }
  18290. }
  18291. if(c < amount)
  18292. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  18293. else
  18294. script_pushint(st, 1);
  18295. return SCRIPT_CMD_SUCCESS;
  18296. }
  18297. /*==========================================
  18298. * instance_check_guild
  18299. * Values:
  18300. * guild_id : Guild ID of the invoking character. [Required Parameter]
  18301. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  18302. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18303. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18304. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  18305. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  18306. *------------------------------------------*/
  18307. BUILDIN_FUNC(instance_check_guild)
  18308. {
  18309. int amount, min, max, i, guild_id = 0, c = 0;
  18310. struct guild *g = NULL;
  18311. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18312. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18313. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18314. if (min < 1 || min > MAX_LEVEL) {
  18315. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18316. return SCRIPT_CMD_FAILURE;
  18317. } else if (max < 1 || max > MAX_LEVEL) {
  18318. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18319. return SCRIPT_CMD_FAILURE;
  18320. }
  18321. if (script_hasdata(st,2))
  18322. guild_id = script_getnum(st,2);
  18323. else
  18324. return SCRIPT_CMD_FAILURE;
  18325. if (!(g = guild_search(guild_id))) {
  18326. script_pushint(st, 0); // Returns false if guild does not exist.
  18327. return SCRIPT_CMD_FAILURE;
  18328. }
  18329. for(i = 0; i < MAX_GUILD; i++) {
  18330. struct map_session_data *pl_sd;
  18331. if ((pl_sd = g->member[i].sd)) {
  18332. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18333. if (pl_sd->status.base_level < min) {
  18334. script_pushint(st, 0);
  18335. return SCRIPT_CMD_SUCCESS;
  18336. } else if (pl_sd->status.base_level > max) {
  18337. script_pushint(st, 0);
  18338. return SCRIPT_CMD_SUCCESS;
  18339. }
  18340. c++;
  18341. }
  18342. }
  18343. }
  18344. if (c < amount)
  18345. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18346. else
  18347. script_pushint(st, 1);
  18348. return SCRIPT_CMD_SUCCESS;
  18349. }
  18350. /*==========================================
  18351. * instance_check_clan
  18352. * Values:
  18353. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18354. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18355. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18356. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18357. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18358. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18359. *------------------------------------------*/
  18360. BUILDIN_FUNC(instance_check_clan)
  18361. {
  18362. int amount, min, max, i, clan_id = 0, c = 0;
  18363. struct clan *cd = NULL;
  18364. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18365. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18366. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18367. if (min < 1 || min > MAX_LEVEL) {
  18368. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18369. return SCRIPT_CMD_FAILURE;
  18370. } else if (max < 1 || max > MAX_LEVEL) {
  18371. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18372. return SCRIPT_CMD_FAILURE;
  18373. }
  18374. if (script_hasdata(st,2))
  18375. clan_id = script_getnum(st,2);
  18376. else
  18377. return SCRIPT_CMD_FAILURE;
  18378. if (!(cd = clan_search(clan_id))) {
  18379. script_pushint(st, 0); // Returns false if clan does not exist.
  18380. return SCRIPT_CMD_FAILURE;
  18381. }
  18382. for(i = 0; i < MAX_CLAN; i++) {
  18383. struct map_session_data *pl_sd;
  18384. if ((pl_sd = cd->members[i])) {
  18385. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18386. if (pl_sd->status.base_level < min) {
  18387. script_pushint(st, 0);
  18388. return SCRIPT_CMD_SUCCESS;
  18389. } else if (pl_sd->status.base_level > max) {
  18390. script_pushint(st, 0);
  18391. return SCRIPT_CMD_SUCCESS;
  18392. }
  18393. c++;
  18394. }
  18395. }
  18396. }
  18397. if (c < amount)
  18398. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18399. else
  18400. script_pushint(st, 1);
  18401. return SCRIPT_CMD_SUCCESS;
  18402. }
  18403. /*==========================================
  18404. * instance_info
  18405. * Values:
  18406. * name : name of the instance you want to look up. [Required Parameter]
  18407. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18408. * index : index of the map in the instance. [Optional Parameter]
  18409. *------------------------------------------*/
  18410. BUILDIN_FUNC(instance_info)
  18411. {
  18412. const char* name = script_getstr(st, 2);
  18413. int type = script_getnum(st, 3);
  18414. int index = 0;
  18415. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18416. if (!db) {
  18417. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18418. script_pushint(st, -1);
  18419. return SCRIPT_CMD_FAILURE;
  18420. }
  18421. switch( type ){
  18422. case IIT_ID:
  18423. script_pushint(st, db->id);
  18424. break;
  18425. case IIT_TIME_LIMIT:
  18426. script_pushint(st, db->limit);
  18427. break;
  18428. case IIT_IDLE_TIMEOUT:
  18429. script_pushint(st, db->timeout);
  18430. break;
  18431. case IIT_ENTER_MAP:
  18432. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18433. break;
  18434. case IIT_ENTER_X:
  18435. script_pushint(st, db->enter.x);
  18436. break;
  18437. case IIT_ENTER_Y:
  18438. script_pushint(st, db->enter.y);
  18439. break;
  18440. case IIT_MAPCOUNT:
  18441. script_pushint(st, db->maplist.size());
  18442. break;
  18443. case IIT_MAP:
  18444. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18445. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18446. script_pushconststr(st, "");
  18447. return SCRIPT_CMD_FAILURE;
  18448. }
  18449. index = script_getnum(st, 4);
  18450. if( index < 0 ){
  18451. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18452. script_pushconststr(st, "");
  18453. return SCRIPT_CMD_FAILURE;
  18454. }
  18455. if( index > UINT8_MAX ){
  18456. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18457. script_pushconststr(st, "");
  18458. return SCRIPT_CMD_FAILURE;
  18459. }
  18460. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18461. break;
  18462. default:
  18463. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18464. script_pushint(st, -1);
  18465. return SCRIPT_CMD_FAILURE;
  18466. }
  18467. return SCRIPT_CMD_SUCCESS;
  18468. }
  18469. /*------------------------------------------
  18470. *instance_live_info( <info type>{, <instance id>} );
  18471. - ILI_NAME : Instance Name
  18472. - ILI_MODE : Instance Mode
  18473. - ILI_OWNER : owner id
  18474. *------------------------------------------*/
  18475. BUILDIN_FUNC(instance_live_info)
  18476. {
  18477. int type = script_getnum(st, 2);
  18478. int id = 0;
  18479. if (type < ILI_NAME || type > ILI_OWNER) {
  18480. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18481. script_pushint(st, -1);
  18482. return SCRIPT_CMD_FAILURE;
  18483. }
  18484. if (!script_hasdata(st, 3))
  18485. id = script_instancegetid(st);
  18486. else
  18487. id = script_getnum(st, 3);
  18488. std::shared_ptr<s_instance_db> db = nullptr;
  18489. std::shared_ptr<s_instance_data> im = nullptr;
  18490. if (id > 0 && id < INT_MAX) {
  18491. im = util::umap_find(instances, id);
  18492. if (im)
  18493. db = instance_db.find(im->id);
  18494. }
  18495. if (!im || !db) {
  18496. if (type == ILI_NAME)
  18497. script_pushconststr(st, "");
  18498. else
  18499. script_pushint(st, -1);
  18500. return SCRIPT_CMD_SUCCESS;
  18501. }
  18502. switch( type ) {
  18503. case ILI_NAME:
  18504. script_pushstrcopy(st, db->name.c_str());
  18505. break;
  18506. case ILI_MODE:
  18507. script_pushint(st, im->mode);
  18508. break;
  18509. case ILI_OWNER:
  18510. script_pushint(st, im->owner_id);
  18511. break;
  18512. }
  18513. return SCRIPT_CMD_SUCCESS;
  18514. }
  18515. /*==========================================
  18516. * Custom Fonts
  18517. *------------------------------------------*/
  18518. BUILDIN_FUNC(setfont)
  18519. {
  18520. struct map_session_data *sd;
  18521. int font;
  18522. if( !script_rid2sd(sd) )
  18523. return SCRIPT_CMD_SUCCESS;
  18524. font = script_getnum(st,2);
  18525. if( sd->status.font != font )
  18526. sd->status.font = font;
  18527. else
  18528. sd->status.font = 0;
  18529. clif_font(sd);
  18530. return SCRIPT_CMD_SUCCESS;
  18531. }
  18532. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  18533. {
  18534. TBL_MOB* md = (TBL_MOB*)bl;
  18535. struct block_list *tbl;
  18536. int mobid = va_arg(ap,int);
  18537. uint16 skill_id = va_arg(ap,int);
  18538. uint16 skill_lv = va_arg(ap,int);
  18539. int casttime = va_arg(ap,int);
  18540. int cancel = va_arg(ap,int);
  18541. int emotion = va_arg(ap,int);
  18542. int target = va_arg(ap,int);
  18543. if( md->mob_id != mobid )
  18544. return 0;
  18545. // 0:self, 1:target, 2:master, default:random
  18546. switch( target ) {
  18547. case 0: tbl = map_id2bl(md->bl.id); break;
  18548. case 1: tbl = map_id2bl(md->target_id); break;
  18549. case 2: tbl = map_id2bl(md->master_id); break;
  18550. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  18551. }
  18552. if( !tbl )
  18553. return 0;
  18554. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  18555. unit_skillcastcancel(bl,0);
  18556. if( skill_get_casttype(skill_id) == CAST_GROUND )
  18557. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  18558. else
  18559. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  18560. clif_emotion(&md->bl, emotion);
  18561. return 1;
  18562. }
  18563. /*==========================================
  18564. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  18565. *------------------------------------------*/
  18566. BUILDIN_FUNC(areamobuseskill)
  18567. {
  18568. struct block_list center;
  18569. int16 m;
  18570. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  18571. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  18572. ShowError("areamobuseskill: invalid map name.\n");
  18573. return SCRIPT_CMD_SUCCESS;
  18574. }
  18575. center.m = m;
  18576. center.x = script_getnum(st,3);
  18577. center.y = script_getnum(st,4);
  18578. range = script_getnum(st,5);
  18579. mobid = script_getnum(st,6);
  18580. if (script_isstring(st, 7)) {
  18581. const char *name = script_getstr(st, 7);
  18582. if (!(skill_id = skill_name2id(name))) {
  18583. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  18584. return SCRIPT_CMD_FAILURE;
  18585. }
  18586. } else {
  18587. skill_id = script_getnum(st, 7);
  18588. if (!skill_get_index(skill_id)) {
  18589. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  18590. return SCRIPT_CMD_FAILURE;
  18591. }
  18592. }
  18593. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  18594. skill_lv = battle_config.mob_max_skilllvl;
  18595. casttime = script_getnum(st,9);
  18596. cancel = script_getnum(st,10);
  18597. emotion = script_getnum(st,11);
  18598. target = script_getnum(st,12);
  18599. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  18600. return SCRIPT_CMD_SUCCESS;
  18601. }
  18602. /**
  18603. * Display a progress bar above a character
  18604. * progressbar "<color>",<seconds>;
  18605. */
  18606. BUILDIN_FUNC(progressbar)
  18607. {
  18608. struct map_session_data * sd;
  18609. const char * color;
  18610. unsigned int second;
  18611. if( !script_rid2sd(sd) )
  18612. return SCRIPT_CMD_SUCCESS;
  18613. st->state = STOP;
  18614. color = script_getstr(st,2);
  18615. second = script_getnum(st,3);
  18616. sd->progressbar.npc_id = st->oid;
  18617. sd->progressbar.timeout = gettick() + second*1000;
  18618. sd->state.workinprogress = WIP_DISABLE_ALL;
  18619. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  18620. return SCRIPT_CMD_SUCCESS;
  18621. }
  18622. /**
  18623. * Display a progress bar above an NPC
  18624. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  18625. */
  18626. BUILDIN_FUNC(progressbar_npc){
  18627. map_session_data *sd = map_id2sd(st->rid);
  18628. struct npc_data* nd = NULL;
  18629. if( script_hasdata(st, 4) ){
  18630. const char* name = script_getstr(st, 4);
  18631. nd = npc_name2id(name);
  18632. if( !nd ){
  18633. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  18634. return SCRIPT_CMD_FAILURE;
  18635. }
  18636. }else{
  18637. nd = map_id2nd(st->oid);
  18638. }
  18639. // First call(by function call)
  18640. if( !nd->progressbar.timeout ){
  18641. const char *color;
  18642. int second;
  18643. color = script_getstr(st, 2);
  18644. second = script_getnum(st, 3);
  18645. if( second < 0 ){
  18646. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  18647. return SCRIPT_CMD_FAILURE;
  18648. }
  18649. if (sd) { // Player attached - keep them from doing other things
  18650. sd->state.workinprogress = WIP_DISABLE_ALL;
  18651. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18652. }
  18653. // sleep for the target amount of time
  18654. st->state = RERUNLINE;
  18655. st->sleep.tick = second * 1000;
  18656. nd->progressbar.timeout = gettick() + second * 1000;
  18657. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  18658. clif_progressbar_npc_area(nd);
  18659. // Second call(by timer after sleeping time is over)
  18660. } else {
  18661. // Continue the script
  18662. if (sd) { // Player attached - remove restrictions
  18663. sd->state.workinprogress = WIP_DISABLE_NONE;
  18664. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18665. }
  18666. st->state = RUN;
  18667. st->sleep.tick = 0;
  18668. nd->progressbar.timeout = nd->progressbar.color = 0;
  18669. }
  18670. return SCRIPT_CMD_SUCCESS;
  18671. }
  18672. BUILDIN_FUNC(pushpc)
  18673. {
  18674. uint8 dir;
  18675. int cells, dx, dy;
  18676. struct map_session_data* sd;
  18677. if(!script_rid2sd(sd))
  18678. {
  18679. return SCRIPT_CMD_SUCCESS;
  18680. }
  18681. dir = script_getnum(st,2);
  18682. cells = script_getnum(st,3);
  18683. if(dir >= DIR_MAX)
  18684. {
  18685. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  18686. script_reportsrc(st);
  18687. dir%= DIR_MAX; // trim spin-over
  18688. }
  18689. if(!cells)
  18690. {// zero distance
  18691. return SCRIPT_CMD_SUCCESS;
  18692. }
  18693. else if(cells<0)
  18694. {// pushing backwards
  18695. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  18696. cells = -cells;
  18697. }
  18698. dx = dirx[dir];
  18699. dy = diry[dir];
  18700. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  18701. return SCRIPT_CMD_SUCCESS;
  18702. }
  18703. /// Invokes buying store preparation window
  18704. /// buyingstore <slots>;
  18705. BUILDIN_FUNC(buyingstore)
  18706. {
  18707. struct map_session_data* sd;
  18708. if( !script_rid2sd(sd) )
  18709. {
  18710. return SCRIPT_CMD_SUCCESS;
  18711. }
  18712. if( npc_isnear(&sd->bl) ) {
  18713. char output[150];
  18714. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  18715. clif_displaymessage(sd->fd, output);
  18716. return SCRIPT_CMD_SUCCESS;
  18717. }
  18718. buyingstore_setup(sd, script_getnum(st,2));
  18719. return SCRIPT_CMD_SUCCESS;
  18720. }
  18721. /// Invokes search store info window
  18722. /// searchstores <uses>,<effect>;
  18723. BUILDIN_FUNC(searchstores)
  18724. {
  18725. unsigned short effect;
  18726. unsigned int uses;
  18727. struct map_session_data* sd;
  18728. if( !script_rid2sd(sd) )
  18729. {
  18730. return SCRIPT_CMD_SUCCESS;
  18731. }
  18732. uses = script_getnum(st,2);
  18733. effect = script_getnum(st,3);
  18734. if( !uses )
  18735. {
  18736. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  18737. return SCRIPT_CMD_FAILURE;
  18738. }
  18739. if( effect > 1 )
  18740. {
  18741. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  18742. return SCRIPT_CMD_FAILURE;
  18743. }
  18744. searchstore_open(sd, uses, effect);
  18745. return SCRIPT_CMD_SUCCESS;
  18746. }
  18747. /// Displays a number as large digital clock.
  18748. /// showdigit <value>[,<type>];
  18749. BUILDIN_FUNC(showdigit)
  18750. {
  18751. unsigned int type = 0;
  18752. int value;
  18753. struct map_session_data* sd;
  18754. if( !script_rid2sd(sd) )
  18755. {
  18756. return SCRIPT_CMD_SUCCESS;
  18757. }
  18758. value = script_getnum(st,2);
  18759. if( script_hasdata(st,3) ){
  18760. type = script_getnum(st,3);
  18761. }
  18762. switch( type ){
  18763. case 0:
  18764. break;
  18765. case 1:
  18766. case 2:
  18767. // Use absolute value and then the negative value of it as starting value
  18768. // This is what gravity's client does for these counters
  18769. value = -abs(value);
  18770. break;
  18771. case 3:
  18772. value = abs(value);
  18773. if( value > 99 ){
  18774. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  18775. script_reportsrc(st);
  18776. value = 99;
  18777. }
  18778. break;
  18779. default:
  18780. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  18781. return SCRIPT_CMD_FAILURE;
  18782. }
  18783. clif_showdigit(sd, (unsigned char)type, value);
  18784. return SCRIPT_CMD_SUCCESS;
  18785. }
  18786. /**
  18787. * Rune Knight
  18788. * makerune <% success bonus>{,<char_id>};
  18789. **/
  18790. BUILDIN_FUNC(makerune) {
  18791. TBL_PC* sd;
  18792. if (!script_charid2sd(3,sd))
  18793. return SCRIPT_CMD_FAILURE;
  18794. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  18795. sd->itemid = script_getnum(st,2);
  18796. return SCRIPT_CMD_SUCCESS;
  18797. }
  18798. /**
  18799. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  18800. **/
  18801. BUILDIN_FUNC(checkdragon) {
  18802. TBL_PC* sd;
  18803. if (!script_charid2sd(2,sd))
  18804. return SCRIPT_CMD_FAILURE;
  18805. if( pc_isridingdragon(sd) )
  18806. script_pushint(st,1);
  18807. else
  18808. script_pushint(st,0);
  18809. return SCRIPT_CMD_SUCCESS;
  18810. }
  18811. /**
  18812. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  18813. * - Toggles the dragon on a RK if he can mount;
  18814. * @param Color - when not provided uses the green dragon;
  18815. * - 1 : Green Dragon
  18816. * - 2 : Brown Dragon
  18817. * - 3 : Gray Dragon
  18818. * - 4 : Blue Dragon
  18819. * - 5 : Red Dragon
  18820. **/
  18821. BUILDIN_FUNC(setdragon) {
  18822. TBL_PC* sd;
  18823. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  18824. if (!script_charid2sd(3,sd))
  18825. return SCRIPT_CMD_FAILURE;
  18826. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  18827. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  18828. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  18829. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  18830. script_pushint(st,1);
  18831. } else {//Not mounted; Mount now.
  18832. unsigned int option = OPTION_DRAGON1;
  18833. if( color ) {
  18834. option = ( color == 1 ? OPTION_DRAGON1 :
  18835. color == 2 ? OPTION_DRAGON2 :
  18836. color == 3 ? OPTION_DRAGON3 :
  18837. color == 4 ? OPTION_DRAGON4 :
  18838. color == 5 ? OPTION_DRAGON5 : 0);
  18839. if( !option ) {
  18840. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  18841. option = OPTION_DRAGON1;
  18842. }
  18843. }
  18844. pc_setoption(sd, sd->sc.option|option);
  18845. script_pushint(st,1);
  18846. }
  18847. return SCRIPT_CMD_SUCCESS;
  18848. }
  18849. /**
  18850. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  18851. **/
  18852. BUILDIN_FUNC(ismounting) {
  18853. TBL_PC* sd;
  18854. if (!script_charid2sd(2,sd))
  18855. return SCRIPT_CMD_FAILURE;
  18856. if( sd->sc.data[SC_ALL_RIDING] )
  18857. script_pushint(st,1);
  18858. else
  18859. script_pushint(st,0);
  18860. return SCRIPT_CMD_SUCCESS;
  18861. }
  18862. /**
  18863. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  18864. * - Toggles new mounts on a player when he can mount
  18865. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  18866. * - Will unmount the player is he is already mounting
  18867. **/
  18868. BUILDIN_FUNC(setmounting) {
  18869. TBL_PC* sd;
  18870. if (!script_charid2sd(2,sd))
  18871. return SCRIPT_CMD_FAILURE;
  18872. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  18873. clif_msg(sd, NEED_REINS_OF_MOUNT);
  18874. script_pushint(st,0); //can't mount with one of these
  18875. } 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]) {
  18876. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  18877. script_pushint(st, 0); // Silent failure
  18878. } else {
  18879. if( sd->sc.data[SC_ALL_RIDING] )
  18880. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  18881. else
  18882. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  18883. script_pushint(st,1);//in both cases, return 1.
  18884. }
  18885. return SCRIPT_CMD_SUCCESS;
  18886. }
  18887. /**
  18888. * Retrieves quantity of arguments provided to callfunc/callsub.
  18889. * getargcount() -> amount of arguments received in a function
  18890. **/
  18891. BUILDIN_FUNC(getargcount) {
  18892. struct script_retinfo* ri;
  18893. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  18894. ShowError("script:getargcount: used out of function or callsub label!\n");
  18895. st->state = END;
  18896. return SCRIPT_CMD_FAILURE;
  18897. }
  18898. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  18899. script_pushint(st, ri->nargs);
  18900. return SCRIPT_CMD_SUCCESS;
  18901. }
  18902. /**
  18903. * getcharip(<account ID>/<character ID>/<character name>)
  18904. **/
  18905. BUILDIN_FUNC(getcharip)
  18906. {
  18907. struct map_session_data* sd = NULL;
  18908. /* check if a character name is specified */
  18909. if( script_hasdata(st, 2) )
  18910. {
  18911. if (script_isstring(st, 2))
  18912. sd = map_nick2sd(script_getstr(st, 2),false);
  18913. else
  18914. {
  18915. int id = script_getnum(st, 2);
  18916. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  18917. }
  18918. }
  18919. else
  18920. script_rid2sd(sd);
  18921. /* check for sd and IP */
  18922. if (!sd || !session[sd->fd]->client_addr)
  18923. {
  18924. script_pushconststr(st, "");
  18925. return SCRIPT_CMD_SUCCESS;
  18926. }
  18927. /* return the client ip_addr converted for output */
  18928. if (sd && sd->fd && session[sd->fd])
  18929. {
  18930. /* initiliaze */
  18931. const char *ip_addr = NULL;
  18932. uint32 ip;
  18933. /* set ip, ip_addr and convert to ip and push str */
  18934. ip = session[sd->fd]->client_addr;
  18935. ip_addr = ip2str(ip, NULL);
  18936. script_pushstrcopy(st, ip_addr);
  18937. }
  18938. return SCRIPT_CMD_SUCCESS;
  18939. }
  18940. /**
  18941. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  18942. **/
  18943. BUILDIN_FUNC(is_function) {
  18944. const char* str = script_getstr(st,2);
  18945. if( strdb_exists(userfunc_db, str) )
  18946. script_pushint(st,1);
  18947. else
  18948. script_pushint(st,0);
  18949. return SCRIPT_CMD_SUCCESS;
  18950. }
  18951. /**
  18952. * get_revision() -> retrieves the current svn revision (if available)
  18953. **/
  18954. BUILDIN_FUNC(get_revision) {
  18955. const char *svn = get_svn_revision();
  18956. if ( svn[0] != UNKNOWN_VERSION )
  18957. script_pushint(st,atoi(svn));
  18958. else
  18959. script_pushint(st,-1); //unknown
  18960. return SCRIPT_CMD_SUCCESS;
  18961. }
  18962. /* get_hash() -> retrieves the current git hash (if available)*/
  18963. BUILDIN_FUNC(get_githash) {
  18964. const char* git = get_git_hash();
  18965. char buf[CHAT_SIZE_MAX];
  18966. safestrncpy(buf,git,strlen(git)+1);
  18967. if ( git[0] != UNKNOWN_VERSION )
  18968. script_pushstrcopy(st,buf);
  18969. else
  18970. script_pushconststr(st,"Unknown"); //unknown
  18971. return SCRIPT_CMD_SUCCESS;
  18972. }
  18973. /**
  18974. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  18975. **/
  18976. BUILDIN_FUNC(freeloop) {
  18977. if( script_hasdata(st,2) ) {
  18978. if( script_getnum(st,2) )
  18979. st->freeloop = 1;
  18980. else
  18981. st->freeloop = 0;
  18982. }
  18983. script_pushint(st, st->freeloop);
  18984. return SCRIPT_CMD_SUCCESS;
  18985. }
  18986. /**
  18987. * @commands (script based)
  18988. **/
  18989. BUILDIN_FUNC(bindatcmd) {
  18990. const char* atcmd;
  18991. const char* eventName;
  18992. int i, level = 0, level2 = 100;
  18993. bool create = false;
  18994. atcmd = script_getstr(st,2);
  18995. eventName = script_getstr(st,3);
  18996. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18997. atcmd++;
  18998. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  18999. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  19000. if( atcmd_binding_count == 0 ) {
  19001. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  19002. create = true;
  19003. } else {
  19004. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  19005. if( i < atcmd_binding_count ) {/* update existent entry */
  19006. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19007. atcmd_binding[i]->level = level;
  19008. atcmd_binding[i]->level2 = level2;
  19009. } else
  19010. create = true;
  19011. }
  19012. if( create ) {
  19013. i = atcmd_binding_count;
  19014. if( atcmd_binding_count++ != 0 )
  19015. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  19016. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  19017. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  19018. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19019. atcmd_binding[i]->level = level;
  19020. atcmd_binding[i]->level2 = level2;
  19021. }
  19022. return SCRIPT_CMD_SUCCESS;
  19023. }
  19024. BUILDIN_FUNC(unbindatcmd) {
  19025. const char* atcmd;
  19026. int i = 0;
  19027. atcmd = script_getstr(st, 2);
  19028. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19029. atcmd++;
  19030. if( atcmd_binding_count == 0 ) {
  19031. script_pushint(st, 0);
  19032. return SCRIPT_CMD_SUCCESS;
  19033. }
  19034. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  19035. if( i < atcmd_binding_count ) {
  19036. int cursor = 0;
  19037. aFree(atcmd_binding[i]);
  19038. atcmd_binding[i] = NULL;
  19039. /* compact the list now that we freed a slot somewhere */
  19040. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  19041. if( atcmd_binding[i] == NULL )
  19042. continue;
  19043. if( cursor != i ) {
  19044. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  19045. }
  19046. cursor++;
  19047. }
  19048. if( (atcmd_binding_count = cursor) == 0 )
  19049. aFree(atcmd_binding);
  19050. script_pushint(st, 1);
  19051. } else
  19052. script_pushint(st, 0);/* not found */
  19053. return SCRIPT_CMD_SUCCESS;
  19054. }
  19055. BUILDIN_FUNC(useatcmd) {
  19056. return atcommand_sub(st,3);
  19057. }
  19058. BUILDIN_FUNC(checkre)
  19059. {
  19060. int num;
  19061. num=script_getnum(st,2);
  19062. switch(num){
  19063. case 0:
  19064. #ifdef RENEWAL
  19065. script_pushint(st, 1);
  19066. #else
  19067. script_pushint(st, 0);
  19068. #endif
  19069. break;
  19070. case 1:
  19071. #ifdef RENEWAL_CAST
  19072. script_pushint(st, 1);
  19073. #else
  19074. script_pushint(st, 0);
  19075. #endif
  19076. break;
  19077. case 2:
  19078. #ifdef RENEWAL_DROP
  19079. script_pushint(st, 1);
  19080. #else
  19081. script_pushint(st, 0);
  19082. #endif
  19083. break;
  19084. case 3:
  19085. #ifdef RENEWAL_EXP
  19086. script_pushint(st, 1);
  19087. #else
  19088. script_pushint(st, 0);
  19089. #endif
  19090. break;
  19091. case 4:
  19092. #ifdef RENEWAL_LVDMG
  19093. script_pushint(st, 1);
  19094. #else
  19095. script_pushint(st, 0);
  19096. #endif
  19097. break;
  19098. case 5:
  19099. #ifdef RENEWAL_ASPD
  19100. script_pushint(st, 1);
  19101. #else
  19102. script_pushint(st, 0);
  19103. #endif
  19104. break;
  19105. default:
  19106. ShowWarning("buildin_checkre: unknown parameter.\n");
  19107. break;
  19108. }
  19109. return SCRIPT_CMD_SUCCESS;
  19110. }
  19111. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  19112. BUILDIN_FUNC(getrandgroupitem) {
  19113. TBL_PC* sd;
  19114. int i, get_count = 0, identify = 0;
  19115. uint16 group, qty = 0;
  19116. uint8 sub_group = 1;
  19117. struct item item_tmp;
  19118. if (!script_charid2sd(6, sd))
  19119. return SCRIPT_CMD_SUCCESS;
  19120. group = script_getnum(st,2);
  19121. if (!group) {
  19122. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  19123. return SCRIPT_CMD_FAILURE;
  19124. }
  19125. FETCH(3, qty);
  19126. FETCH(4, sub_group);
  19127. FETCH(5, identify);
  19128. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(group,sub_group);
  19129. if (!entry)
  19130. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  19131. memset(&item_tmp,0,sizeof(item_tmp));
  19132. item_tmp.nameid = entry->nameid;
  19133. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  19134. if (!qty)
  19135. qty = entry->amount;
  19136. //Check if it's stackable.
  19137. if (!itemdb_isstackable(entry->nameid)) {
  19138. item_tmp.amount = 1;
  19139. get_count = qty;
  19140. }
  19141. else {
  19142. item_tmp.amount = qty;
  19143. get_count = 1;
  19144. }
  19145. for (i = 0; i < get_count; i++) {
  19146. // if not pet egg
  19147. if (!pet_create_egg(sd, entry->nameid)) {
  19148. unsigned char flag = 0;
  19149. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  19150. clif_additem(sd,0,0,flag);
  19151. if (pc_candrop(sd,&item_tmp))
  19152. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  19153. }
  19154. }
  19155. }
  19156. return SCRIPT_CMD_SUCCESS;
  19157. }
  19158. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  19159. * Gives item(s) to the attached player based on item group contents
  19160. */
  19161. BUILDIN_FUNC(getgroupitem) {
  19162. TBL_PC *sd;
  19163. int group_id = script_getnum(st,2);
  19164. if (!script_charid2sd(4,sd))
  19165. return SCRIPT_CMD_SUCCESS;
  19166. if (itemdb_group.pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  19167. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  19168. return SCRIPT_CMD_FAILURE;
  19169. }
  19170. return SCRIPT_CMD_SUCCESS;
  19171. }
  19172. /* cleanmap <map_name>;
  19173. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  19174. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  19175. {
  19176. nullpo_ret(bl);
  19177. map_clearflooritem(bl);
  19178. return SCRIPT_CMD_SUCCESS;
  19179. }
  19180. BUILDIN_FUNC(cleanmap)
  19181. {
  19182. const char *mapname;
  19183. int16 m;
  19184. mapname = script_getstr(st, 2);
  19185. m = map_mapname2mapid(mapname);
  19186. if (!m)
  19187. return SCRIPT_CMD_FAILURE;
  19188. if ((script_lastdata(st) - 2) < 4) {
  19189. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  19190. } else {
  19191. int16 x0 = script_getnum(st, 3);
  19192. int16 y0 = script_getnum(st, 4);
  19193. int16 x1 = script_getnum(st, 5);
  19194. int16 y1 = script_getnum(st, 6);
  19195. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  19196. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  19197. } else {
  19198. ShowError("cleanarea: invalid coordinate defined!\n");
  19199. return SCRIPT_CMD_FAILURE;
  19200. }
  19201. }
  19202. return SCRIPT_CMD_SUCCESS;
  19203. }
  19204. /* Cast a skill on the attached player.
  19205. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  19206. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  19207. BUILDIN_FUNC(npcskill)
  19208. {
  19209. uint16 skill_id;
  19210. unsigned short skill_level;
  19211. unsigned int stat_point;
  19212. unsigned int npc_level;
  19213. struct npc_data *nd;
  19214. struct map_session_data *sd;
  19215. if( !script_rid2sd(sd) )
  19216. return SCRIPT_CMD_SUCCESS;
  19217. if (script_isstring(st, 2)) {
  19218. const char *name = script_getstr(st, 2);
  19219. if (!(skill_id = skill_name2id(name))) {
  19220. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  19221. return SCRIPT_CMD_FAILURE;
  19222. }
  19223. } else {
  19224. skill_id = script_getnum(st, 2);
  19225. if (!skill_get_index(skill_id)) {
  19226. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  19227. return SCRIPT_CMD_FAILURE;
  19228. }
  19229. }
  19230. skill_level = script_getnum(st, 3);
  19231. stat_point = script_getnum(st, 4);
  19232. npc_level = script_getnum(st, 5);
  19233. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  19234. if (stat_point > battle_config.max_third_parameter) {
  19235. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  19236. return SCRIPT_CMD_FAILURE;
  19237. }
  19238. if (npc_level > MAX_LEVEL) {
  19239. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  19240. return SCRIPT_CMD_FAILURE;
  19241. }
  19242. if (nd == NULL) { //ain't possible, but I don't trust people.
  19243. return SCRIPT_CMD_FAILURE;
  19244. }
  19245. nd->level = npc_level;
  19246. nd->stat_point = stat_point;
  19247. if (!nd->status.hp)
  19248. status_calc_npc(nd, SCO_FIRST);
  19249. else
  19250. status_calc_npc(nd, SCO_NONE);
  19251. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  19252. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  19253. else
  19254. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  19255. return SCRIPT_CMD_SUCCESS;
  19256. }
  19257. /* Consumes an item.
  19258. * consumeitem <item id>{,<char_id>};
  19259. * consumeitem "<item name>"{,<char_id>};
  19260. * @param item: Item ID or name
  19261. */
  19262. BUILDIN_FUNC(consumeitem)
  19263. {
  19264. struct map_session_data *sd;
  19265. std::shared_ptr<item_data> item_data;
  19266. if (!script_charid2sd(3, sd))
  19267. return SCRIPT_CMD_FAILURE;
  19268. if( script_isstring(st, 2) ){
  19269. const char *name = script_getstr(st, 2);
  19270. item_data = item_db.searchname( name );
  19271. if( item_data == nullptr ){
  19272. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  19273. return SCRIPT_CMD_FAILURE;
  19274. }
  19275. } else {
  19276. t_itemid nameid = script_getnum(st, 2);
  19277. item_data = item_db.find( nameid );
  19278. if( item_data == nullptr ){
  19279. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  19280. return SCRIPT_CMD_FAILURE;
  19281. }
  19282. }
  19283. run_script( item_data->script, 0, sd->bl.id, 0 );
  19284. return SCRIPT_CMD_SUCCESS;
  19285. }
  19286. /** Makes a player sit/stand.
  19287. * sit {"<character name>"};
  19288. * stand {"<character name>"};
  19289. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  19290. * @param name: Player name that will be invoked
  19291. */
  19292. BUILDIN_FUNC(sit)
  19293. {
  19294. TBL_PC *sd;
  19295. if( !script_nick2sd(2,sd) )
  19296. return SCRIPT_CMD_FAILURE;
  19297. if( !pc_issit(sd) ) {
  19298. pc_setsit(sd);
  19299. skill_sit(sd, 1);
  19300. clif_sitting(&sd->bl);
  19301. }
  19302. return SCRIPT_CMD_SUCCESS;
  19303. }
  19304. /** Makes player to stand
  19305. * @param name: Player name that will be set
  19306. */
  19307. BUILDIN_FUNC(stand)
  19308. {
  19309. TBL_PC *sd;
  19310. if( !script_nick2sd(2,sd) )
  19311. return SCRIPT_CMD_FAILURE;
  19312. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19313. skill_sit(sd, 0);
  19314. clif_standing(&sd->bl);
  19315. }
  19316. return SCRIPT_CMD_SUCCESS;
  19317. }
  19318. /** Creates an array of bounded item IDs
  19319. * countbound {<type>{,<char_id>}};
  19320. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19321. * @return amt: Total number of different items type found
  19322. */
  19323. BUILDIN_FUNC(countbound)
  19324. {
  19325. TBL_PC *sd;
  19326. if (!script_charid2sd(3,sd))
  19327. return SCRIPT_CMD_FAILURE;
  19328. int i, k = 0;
  19329. int type = script_getnum(st,2);
  19330. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19331. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19332. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19333. ))
  19334. {
  19335. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19336. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19337. k++;
  19338. }
  19339. }
  19340. script_pushint(st,k);
  19341. return SCRIPT_CMD_SUCCESS;
  19342. }
  19343. /** Creates new party
  19344. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19345. * @param party_name: String of party name that will be created
  19346. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19347. * @param item_share: 0-Each Take. 1-Party Share
  19348. * @param item_share_type: 0-Each Take. 1-Even Share
  19349. * @return val: Result value
  19350. * -3 - party name is exist
  19351. * -2 - player is in party already
  19352. * -1 - player is not found
  19353. * 0 - unknown error
  19354. * 1 - success, will return party id $@party_create_id
  19355. */
  19356. BUILDIN_FUNC(party_create)
  19357. {
  19358. char party_name[NAME_LENGTH];
  19359. int item1 = 0, item2 = 0;
  19360. TBL_PC *sd = NULL;
  19361. if (!script_charid2sd(3, sd)) {
  19362. script_pushint(st,-1);
  19363. return SCRIPT_CMD_FAILURE;
  19364. }
  19365. if( sd->status.party_id ) {
  19366. script_pushint(st,-2);
  19367. return SCRIPT_CMD_FAILURE;
  19368. }
  19369. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19370. trim(party_name);
  19371. if( party_searchname(party_name) ) {
  19372. script_pushint(st,-3);
  19373. return SCRIPT_CMD_FAILURE;
  19374. }
  19375. if( script_getnum(st,4) )
  19376. item1 = 1;
  19377. if( script_getnum(st,5) )
  19378. item2 = 1;
  19379. party_create_byscript = 1;
  19380. script_pushint(st,party_create(sd,party_name,item1,item2));
  19381. return SCRIPT_CMD_SUCCESS;
  19382. }
  19383. /** Adds player to specified party
  19384. * party_addmember <party id>,<char id>;
  19385. * @param party_id: The party that will be entered by player
  19386. * @param char_id: Char id of player that will be joined to the party
  19387. * @return val: Result value
  19388. * -5 - another character of the same account is in the party
  19389. * -4 - party is full
  19390. * -3 - party is not found
  19391. * -2 - player is in party already
  19392. * -1 - player is not found
  19393. * 0 - unknown error
  19394. * 1 - success
  19395. */
  19396. BUILDIN_FUNC(party_addmember)
  19397. {
  19398. int party_id = script_getnum(st,2);
  19399. TBL_PC *sd;
  19400. struct party_data *party;
  19401. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19402. script_pushint(st,-1);
  19403. return SCRIPT_CMD_FAILURE;
  19404. }
  19405. if( sd->status.party_id ) {
  19406. script_pushint(st,-2);
  19407. return SCRIPT_CMD_FAILURE;
  19408. }
  19409. if( !(party = party_search(party_id)) ) {
  19410. script_pushint(st,-3);
  19411. return SCRIPT_CMD_FAILURE;
  19412. }
  19413. if (battle_config.block_account_in_same_party) {
  19414. int i;
  19415. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19416. if (i < MAX_PARTY) {
  19417. script_pushint(st,-5);
  19418. return SCRIPT_CMD_FAILURE;
  19419. }
  19420. }
  19421. if( party->party.count >= MAX_PARTY ) {
  19422. script_pushint(st,-4);
  19423. return SCRIPT_CMD_FAILURE;
  19424. }
  19425. sd->party_invite = party_id;
  19426. script_pushint(st,party_add_member(party_id,sd));
  19427. return SCRIPT_CMD_SUCCESS;
  19428. }
  19429. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19430. * party_delmember {<char id>,<party_id>};
  19431. * @param: char_id
  19432. * @param: party_id
  19433. * @return val: Result value
  19434. * -3 - player is not in party
  19435. * -2 - party is not found
  19436. * -1 - player is not found
  19437. * 0 - unknown error
  19438. * 1 - success
  19439. */
  19440. BUILDIN_FUNC(party_delmember)
  19441. {
  19442. TBL_PC *sd = NULL;
  19443. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19444. script_pushint(st,-1);
  19445. return SCRIPT_CMD_FAILURE;
  19446. }
  19447. if( sd || script_charid2sd(2,sd) )
  19448. script_pushint(st,party_removemember2(sd,0,0));
  19449. else
  19450. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  19451. return SCRIPT_CMD_SUCCESS;
  19452. }
  19453. /** Changes party leader of specified party (even the leader is offline)
  19454. * party_changeleader <party id>,<char id>;
  19455. * @param party_id: ID of party
  19456. * @param char_id: Char ID of new leader
  19457. * @return val: Result value
  19458. * -4 - player is party leader already
  19459. * -3 - player is not in this party
  19460. * -2 - player is not found
  19461. * -1 - party is not found
  19462. * 0 - unknown error
  19463. * 1 - success */
  19464. BUILDIN_FUNC(party_changeleader)
  19465. {
  19466. int i, party_id = script_getnum(st,2);
  19467. TBL_PC *sd = NULL;
  19468. TBL_PC *tsd = NULL;
  19469. struct party_data *party = NULL;
  19470. if( !(party = party_search(party_id)) ) {
  19471. script_pushint(st,-1);
  19472. return SCRIPT_CMD_FAILURE;
  19473. }
  19474. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19475. script_pushint(st,-2);
  19476. return SCRIPT_CMD_FAILURE;
  19477. }
  19478. if( tsd->status.party_id != party_id ) {
  19479. script_pushint(st,-3);
  19480. return SCRIPT_CMD_FAILURE;
  19481. }
  19482. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19483. if( i >= MAX_PARTY ) { //this is should impossible!
  19484. script_pushint(st,0);
  19485. return SCRIPT_CMD_FAILURE;
  19486. }
  19487. if( party->data[i].sd == tsd ) {
  19488. script_pushint(st,-4);
  19489. return SCRIPT_CMD_FAILURE;
  19490. }
  19491. script_pushint(st,party_changeleader(sd,tsd,party));
  19492. return SCRIPT_CMD_SUCCESS;
  19493. }
  19494. /** Changes party option
  19495. * party_changeoption <party id>,<option>,<flag>;
  19496. * @param party_id: ID of party that will be changed
  19497. * @param option: Type of option
  19498. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  19499. */
  19500. BUILDIN_FUNC(party_changeoption)
  19501. {
  19502. struct party_data *party;
  19503. if( !(party = party_search(script_getnum(st,2))) ) {
  19504. script_pushint(st,-1);
  19505. return SCRIPT_CMD_FAILURE;
  19506. }
  19507. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  19508. return SCRIPT_CMD_SUCCESS;
  19509. }
  19510. /** Destroys party with party id.
  19511. * party_destroy <party id>;
  19512. * @param party_id: ID of party that will be destroyed
  19513. */
  19514. BUILDIN_FUNC(party_destroy)
  19515. {
  19516. int i;
  19517. struct party_data *party;
  19518. if( !(party = party_search(script_getnum(st,2))) ) {
  19519. script_pushint(st,0);
  19520. return SCRIPT_CMD_FAILURE;
  19521. }
  19522. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19523. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  19524. int j;
  19525. for( j = 0; j < MAX_PARTY; j++ ) {
  19526. TBL_PC *sd = party->data[j].sd;
  19527. if(sd)
  19528. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  19529. else if( party->party.member[j].char_id )
  19530. 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);
  19531. }
  19532. party_broken(party->party.party_id);
  19533. script_pushint(st,1);
  19534. }
  19535. else //leader leave = party broken
  19536. script_pushint(st,party_leave(party->data[i].sd));
  19537. return SCRIPT_CMD_SUCCESS;
  19538. }
  19539. /** Returns various information about a player's VIP status. Need to enable VIP system
  19540. * vip_status <type>,{"<character name>"};
  19541. * @param type: Info type, see enum vip_status_type
  19542. * @param name: Character name (Optional)
  19543. */
  19544. BUILDIN_FUNC(vip_status) {
  19545. #ifdef VIP_ENABLE
  19546. TBL_PC *sd;
  19547. int type;
  19548. if( !script_nick2sd(3,sd) )
  19549. return SCRIPT_CMD_FAILURE;
  19550. type = script_getnum(st, 2);
  19551. switch(type) {
  19552. case VIP_STATUS_ACTIVE: // Get VIP status.
  19553. script_pushint(st, pc_isvip(sd));
  19554. break;
  19555. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  19556. if (pc_isvip(sd)) {
  19557. script_pushint(st, sd->vip.time);
  19558. } else
  19559. script_pushint(st, 0);
  19560. break;
  19561. case VIP_STATUS_REMAINING: // Get remaining time.
  19562. if (pc_isvip(sd)) {
  19563. script_pushint(st, sd->vip.time - time(NULL));
  19564. } else
  19565. script_pushint(st, 0);
  19566. break;
  19567. default:
  19568. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  19569. return SCRIPT_CMD_FAILURE;
  19570. }
  19571. #else
  19572. script_pushint(st, 0);
  19573. #endif
  19574. return SCRIPT_CMD_SUCCESS;
  19575. }
  19576. /** Adds or removes VIP time in minutes. Need to enable VIP system
  19577. * vip_time <time in mn>,{"<character name>"};
  19578. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  19579. * @param name: Character name (optional)
  19580. */
  19581. BUILDIN_FUNC(vip_time) {
  19582. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  19583. TBL_PC *sd;
  19584. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  19585. if( !script_nick2sd(3,sd) )
  19586. return SCRIPT_CMD_FAILURE;
  19587. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  19588. #endif
  19589. return SCRIPT_CMD_SUCCESS;
  19590. }
  19591. /**
  19592. * Turns a player into a monster and optionally can grant a SC attribute effect.
  19593. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19594. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19595. * @param monster: Monster ID or name
  19596. * @param duration: Transform duration in millisecond (ms)
  19597. * @param sc_type: Type of SC that will be affected during the transformation
  19598. * @param val1: Value for SC
  19599. * @param val2: Value for SC
  19600. * @param val3: Value for SC
  19601. * @param val4: Value for SC
  19602. * @author: malufett
  19603. */
  19604. BUILDIN_FUNC(montransform) {
  19605. TBL_PC *sd;
  19606. enum sc_type type;
  19607. int tick, mob_id, val1, val2, val3, val4;
  19608. val1 = val2 = val3 = val4 = 0;
  19609. if( !script_rid2sd(sd) )
  19610. return SCRIPT_CMD_FAILURE;
  19611. if( script_isstring(st, 2) )
  19612. mob_id = mobdb_searchname(script_getstr(st, 2));
  19613. else
  19614. mob_id = mobdb_checkid(script_getnum(st, 2));
  19615. tick = script_getnum(st, 3);
  19616. if (script_hasdata(st, 4))
  19617. type = (sc_type)script_getnum(st, 4);
  19618. else
  19619. type = SC_NONE;
  19620. if (mob_id == 0) {
  19621. if( script_isstring(st, 2) )
  19622. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  19623. else
  19624. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  19625. return SCRIPT_CMD_FAILURE;
  19626. }
  19627. if (mob_id == MOBID_EMPERIUM) {
  19628. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  19629. return SCRIPT_CMD_FAILURE;
  19630. }
  19631. if (!(type >= SC_NONE && type < SC_MAX)) {
  19632. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  19633. return SCRIPT_CMD_FAILURE;
  19634. }
  19635. if (script_hasdata(st, 5))
  19636. val1 = script_getnum(st, 5);
  19637. if (script_hasdata(st, 6))
  19638. val2 = script_getnum(st, 6);
  19639. if (script_hasdata(st, 7))
  19640. val3 = script_getnum(st, 7);
  19641. if (script_hasdata(st, 8))
  19642. val4 = script_getnum(st, 8);
  19643. if (tick != 0) {
  19644. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  19645. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  19646. return SCRIPT_CMD_FAILURE;
  19647. }
  19648. if (sd->disguise){
  19649. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  19650. return SCRIPT_CMD_FAILURE;
  19651. }
  19652. if (!strcmp(script_getfuncname(st), "active_transform")) {
  19653. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19654. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19655. } else {
  19656. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19657. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19658. }
  19659. if (type != SC_NONE)
  19660. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  19661. }
  19662. return SCRIPT_CMD_SUCCESS;
  19663. }
  19664. /**
  19665. * Attach script to player for certain duration
  19666. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  19667. * @param "script code"
  19668. * @param duration
  19669. * @param flag
  19670. * @param icon
  19671. * @param char_id
  19672. * @author [Cydh]
  19673. **/
  19674. BUILDIN_FUNC(bonus_script) {
  19675. uint16 flag = 0;
  19676. int16 icon = EFST_BLANK;
  19677. uint32 dur;
  19678. uint8 type = 0;
  19679. TBL_PC* sd;
  19680. const char *script_str = NULL;
  19681. struct s_bonus_script_entry *entry = NULL;
  19682. if ( !script_charid2sd(7,sd) )
  19683. return SCRIPT_CMD_FAILURE;
  19684. script_str = script_getstr(st,2);
  19685. dur = 1000 * abs(script_getnum(st,3));
  19686. FETCH(4, flag);
  19687. FETCH(5, type);
  19688. FETCH(6, icon);
  19689. // No Script string, No Duration!
  19690. if (script_str[0] == '\0' || !dur) {
  19691. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  19692. return SCRIPT_CMD_FAILURE;
  19693. }
  19694. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  19695. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  19696. return SCRIPT_CMD_FAILURE;
  19697. }
  19698. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  19699. icon = EFST_BLANK;
  19700. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_types)icon, flag, type))) {
  19701. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  19702. status_calc_pc(sd,SCO_NONE);
  19703. }
  19704. return SCRIPT_CMD_SUCCESS;
  19705. }
  19706. /**
  19707. * Removes all bonus script from player
  19708. * bonus_script_clear {<flag>,{<char_id>}};
  19709. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  19710. * @param char_id Clear script from this character
  19711. * @author [Cydh]
  19712. */
  19713. BUILDIN_FUNC(bonus_script_clear) {
  19714. TBL_PC* sd;
  19715. bool flag = false;
  19716. if (!script_charid2sd(3,sd))
  19717. return SCRIPT_CMD_FAILURE;
  19718. if (script_hasdata(st,2))
  19719. flag = script_getnum(st,2) != 0;
  19720. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  19721. return SCRIPT_CMD_SUCCESS;
  19722. }
  19723. /** Allows player to use atcommand while talking with NPC
  19724. * enable_command;
  19725. * @author [Cydh], [Kichi]
  19726. */
  19727. BUILDIN_FUNC(enable_command) {
  19728. TBL_PC* sd;
  19729. if (!script_rid2sd(sd))
  19730. return SCRIPT_CMD_FAILURE;
  19731. sd->state.disable_atcommand_on_npc = 0;
  19732. return SCRIPT_CMD_SUCCESS;
  19733. }
  19734. /** Prevents player to use atcommand while talking with NPC
  19735. * disable_command;
  19736. * @author [Cydh], [Kichi]
  19737. */
  19738. BUILDIN_FUNC(disable_command) {
  19739. TBL_PC* sd;
  19740. if (!script_rid2sd(sd))
  19741. return SCRIPT_CMD_FAILURE;
  19742. sd->state.disable_atcommand_on_npc = 1;
  19743. return SCRIPT_CMD_SUCCESS;
  19744. }
  19745. /** Get the information of the members of a guild by type.
  19746. * getguildmember <guild_id>{,<type>};
  19747. * @param guild_id: ID of guild
  19748. * @param type: Type of option (optional)
  19749. */
  19750. BUILDIN_FUNC(getguildmember)
  19751. {
  19752. struct guild *g = NULL;
  19753. uint8 j = 0;
  19754. g = guild_search(script_getnum(st,2));
  19755. if (g) {
  19756. uint8 i, type = 0;
  19757. struct script_data *data = NULL;
  19758. char *varname = NULL;
  19759. if (script_hasdata(st,3))
  19760. type = script_getnum(st,3);
  19761. if (script_hasdata(st,4)) {
  19762. data = script_getdata(st, 4);
  19763. if (!data_isreference(data)) {
  19764. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  19765. return SCRIPT_CMD_FAILURE;
  19766. }
  19767. varname = reference_getname(data);
  19768. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  19769. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  19770. return SCRIPT_CMD_FAILURE;
  19771. }
  19772. if (not_server_variable(*varname)) {
  19773. struct map_session_data *sd;
  19774. if (!script_rid2sd(sd)) {
  19775. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  19776. return SCRIPT_CMD_FAILURE;
  19777. }
  19778. }
  19779. }
  19780. for (i = 0; i < MAX_GUILD; i++) {
  19781. if (g->member[i].account_id) {
  19782. switch (type) {
  19783. case 2:
  19784. if (data)
  19785. setd_sub_num( st, NULL, varname, j, g->member[i].account_id, data->ref );
  19786. else
  19787. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  19788. break;
  19789. case 1:
  19790. if (data)
  19791. setd_sub_num( st, NULL, varname, j, g->member[i].char_id, data->ref );
  19792. else
  19793. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  19794. break;
  19795. default:
  19796. if (data)
  19797. setd_sub_str( st, NULL, varname, j, g->member[i].name, data->ref );
  19798. else
  19799. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  19800. break;
  19801. }
  19802. j++;
  19803. }
  19804. }
  19805. }
  19806. mapreg_setreg(add_str("$@guildmembercount"), j);
  19807. script_pushint(st, j);
  19808. return SCRIPT_CMD_SUCCESS;
  19809. }
  19810. /** Adds spirit ball to player for 'duration' in milisecond
  19811. * addspiritball <count>,<duration>{,<char_id>};
  19812. * @param count How many spirit ball will be added
  19813. * @param duration How long spiritball is active until it disappears
  19814. * @param char_id Target player (Optional)
  19815. * @author [Cydh]
  19816. */
  19817. BUILDIN_FUNC(addspiritball) {
  19818. uint8 i, count;
  19819. uint16 duration;
  19820. struct map_session_data *sd = NULL;
  19821. if (script_hasdata(st,4)) {
  19822. if (!script_isstring(st,4))
  19823. sd = map_charid2sd(script_getnum(st,4));
  19824. else
  19825. sd = map_nick2sd(script_getstr(st,4),false);
  19826. }
  19827. else
  19828. script_rid2sd(sd);
  19829. if (!sd)
  19830. return SCRIPT_CMD_FAILURE;
  19831. count = script_getnum(st,2);
  19832. if (count == 0)
  19833. return SCRIPT_CMD_SUCCESS;
  19834. duration = script_getnum(st,3);
  19835. for (i = 0; i < count; i++)
  19836. pc_addspiritball(sd,duration,10);
  19837. return SCRIPT_CMD_SUCCESS;
  19838. }
  19839. /** Deletes the spirit ball(s) from player
  19840. * delspiritball <count>{,<char_id>};
  19841. * @param count How many spirit ball will be deleted
  19842. * @param char_id Target player (Optional)
  19843. * @author [Cydh]
  19844. */
  19845. BUILDIN_FUNC(delspiritball) {
  19846. uint8 count;
  19847. struct map_session_data *sd = NULL;
  19848. if (script_hasdata(st,3)) {
  19849. if (!script_isstring(st,3))
  19850. sd = map_charid2sd(script_getnum(st,3));
  19851. else
  19852. sd = map_nick2sd(script_getstr(st,3),false);
  19853. }
  19854. else
  19855. script_rid2sd(sd);
  19856. if (!sd)
  19857. return SCRIPT_CMD_FAILURE;
  19858. count = script_getnum(st,2);
  19859. if (count == 0)
  19860. count = 1;
  19861. pc_delspiritball(sd,count,0);
  19862. return SCRIPT_CMD_SUCCESS;
  19863. }
  19864. /** Counts the spirit ball that player has
  19865. * countspiritball {<char_id>};
  19866. * @param char_id Target player (Optional)
  19867. * @author [Cydh]
  19868. */
  19869. BUILDIN_FUNC(countspiritball) {
  19870. struct map_session_data *sd;
  19871. if (script_hasdata(st,2)) {
  19872. if (!script_isstring(st,2))
  19873. sd = map_charid2sd(script_getnum(st,2));
  19874. else
  19875. sd = map_nick2sd(script_getstr(st,2),false);
  19876. }
  19877. else
  19878. script_rid2sd(sd);
  19879. if (!sd)
  19880. return SCRIPT_CMD_FAILURE;
  19881. script_pushint(st,sd->spiritball);
  19882. return SCRIPT_CMD_SUCCESS;
  19883. }
  19884. /** Merges separated stackable items because of guid
  19885. * mergeitem {<char_id>};
  19886. * @param char_id Char ID (Optional)
  19887. * @author [Cydh]
  19888. */
  19889. BUILDIN_FUNC(mergeitem) {
  19890. struct map_session_data *sd;
  19891. if (!script_charid2sd(2, sd))
  19892. return SCRIPT_CMD_FAILURE;
  19893. clif_merge_item_open(sd);
  19894. return SCRIPT_CMD_SUCCESS;
  19895. }
  19896. /** Merges separated stackable items because of guid (Unofficial)
  19897. * mergeitem2 {<item_id>,{<char_id>}};
  19898. * mergeitem2 {"<item name>",{<char_id>}};
  19899. * @param item Item ID/Name for merging specific item (Optional)
  19900. * @author [Cydh]
  19901. */
  19902. BUILDIN_FUNC(mergeitem2) {
  19903. struct map_session_data *sd;
  19904. struct item *items = NULL;
  19905. uint16 i, count = 0;
  19906. t_itemid nameid = 0;
  19907. if (!script_charid2sd(3, sd))
  19908. return SCRIPT_CMD_FAILURE;
  19909. if (script_hasdata(st, 2)) {
  19910. if (script_isstring(st, 2)) {// "<item name>"
  19911. const char *name = script_getstr(st, 2);
  19912. std::shared_ptr<item_data> id = item_db.searchname( name );
  19913. if( id == nullptr ){
  19914. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  19915. script_pushint(st, count);
  19916. return SCRIPT_CMD_FAILURE;
  19917. }
  19918. nameid = id->nameid;
  19919. } else {// <item id>
  19920. nameid = script_getnum(st, 2);
  19921. if (!itemdb_exists(nameid)) {
  19922. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  19923. script_pushint(st, count);
  19924. return SCRIPT_CMD_FAILURE;
  19925. }
  19926. }
  19927. }
  19928. for (i = 0; i < MAX_INVENTORY; i++) {
  19929. struct item *it = &sd->inventory.u.items_inventory[i];
  19930. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  19931. continue;
  19932. if ((!nameid || (nameid == it->nameid))) {
  19933. uint8 k;
  19934. if (!count) {
  19935. CREATE(items, struct item, 1);
  19936. memcpy(&items[count++], it, sizeof(struct item));
  19937. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19938. continue;
  19939. }
  19940. for (k = 0; k < count; k++) {
  19941. // Find Match
  19942. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  19943. items[k].amount += it->amount;
  19944. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19945. break;
  19946. }
  19947. }
  19948. if (k >= count) {
  19949. // New entry
  19950. RECREATE(items, struct item, count+1);
  19951. memcpy(&items[count++], it, sizeof(struct item));
  19952. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19953. }
  19954. }
  19955. }
  19956. if (!items) // Nothing todo here
  19957. return SCRIPT_CMD_SUCCESS;
  19958. // Retrieve the items
  19959. for (i = 0; i < count; i++) {
  19960. uint8 flag = 0;
  19961. if (!&items[i])
  19962. continue;
  19963. items[i].id = 0;
  19964. items[i].unique_id = 0;
  19965. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  19966. clif_additem(sd, i, items[i].amount, flag);
  19967. }
  19968. aFree(items);
  19969. script_pushint(st, count);
  19970. return SCRIPT_CMD_SUCCESS;
  19971. }
  19972. /**
  19973. * Update an entry from NPC shop.
  19974. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  19975. **/
  19976. BUILDIN_FUNC(npcshopupdate) {
  19977. const char* npcname = script_getstr(st, 2);
  19978. struct npc_data* nd = npc_name2id(npcname);
  19979. t_itemid nameid = script_getnum(st, 3);
  19980. int price = script_getnum(st, 4);
  19981. #if PACKETVER >= 20131223
  19982. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  19983. #endif
  19984. int i;
  19985. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  19986. script_pushint(st,0);
  19987. return SCRIPT_CMD_FAILURE;
  19988. }
  19989. if (!nd->u.shop.count) {
  19990. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  19991. script_pushint(st,0);
  19992. return SCRIPT_CMD_FAILURE;
  19993. }
  19994. for (i = 0; i < nd->u.shop.count; i++) {
  19995. if (nd->u.shop.shop_item[i].nameid == nameid) {
  19996. if (price != 0)
  19997. nd->u.shop.shop_item[i].value = price;
  19998. #if PACKETVER >= 20131223
  19999. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  20000. nd->u.shop.shop_item[i].qty = stock;
  20001. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  20002. }
  20003. #endif
  20004. }
  20005. }
  20006. script_pushint(st,1);
  20007. return SCRIPT_CMD_SUCCESS;
  20008. }
  20009. // Clan System
  20010. BUILDIN_FUNC(clan_join){
  20011. struct map_session_data *sd;
  20012. int clan_id = script_getnum(st,2);
  20013. if( !script_charid2sd( 3, sd ) ){
  20014. script_pushint(st, false);
  20015. return SCRIPT_CMD_FAILURE;
  20016. }
  20017. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  20018. script_pushint(st, true);
  20019. else
  20020. script_pushint(st, false);
  20021. return SCRIPT_CMD_SUCCESS;
  20022. }
  20023. BUILDIN_FUNC(clan_leave){
  20024. struct map_session_data *sd;
  20025. if( !script_charid2sd( 2, sd ) ){
  20026. script_pushint(st, false);
  20027. return SCRIPT_CMD_FAILURE;
  20028. }
  20029. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  20030. script_pushint(st, true);
  20031. else
  20032. script_pushint(st, false);
  20033. return SCRIPT_CMD_SUCCESS;
  20034. }
  20035. /**
  20036. * Get rid from running script.
  20037. * getattachedrid();
  20038. **/
  20039. BUILDIN_FUNC(getattachedrid) {
  20040. script_pushint(st, st->rid);
  20041. return SCRIPT_CMD_SUCCESS;
  20042. }
  20043. /**
  20044. * Get variable from a Player
  20045. * getvar <variable>,<char_id>;
  20046. */
  20047. BUILDIN_FUNC(getvar) {
  20048. int char_id = script_getnum(st, 3);
  20049. struct map_session_data *sd = map_charid2sd(char_id);
  20050. struct script_data *data = NULL;
  20051. const char *name = NULL;
  20052. if (!sd) {
  20053. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  20054. return SCRIPT_CMD_FAILURE;
  20055. }
  20056. data = script_getdata(st, 2);
  20057. if (!data_isreference(data)) {
  20058. ShowError("buildin_getvar: Not a variable\n");
  20059. script_reportdata(data);
  20060. script_pushnil(st);
  20061. st->state = END;
  20062. return SCRIPT_CMD_FAILURE;
  20063. }
  20064. name = reference_getname(data);
  20065. if (reference_toparam(data)) {
  20066. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  20067. script_reportdata(data);
  20068. script_pushnil(st);
  20069. st->state = END;
  20070. return SCRIPT_CMD_FAILURE;
  20071. }
  20072. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  20073. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  20074. script_reportdata(data);
  20075. script_pushnil(st);
  20076. st->state = END;
  20077. return SCRIPT_CMD_FAILURE;
  20078. }
  20079. get_val_(st, data, sd);
  20080. if (data_isint(data))
  20081. script_pushint(st, conv_num_(st, data, sd));
  20082. else
  20083. script_pushstrcopy(st, conv_str_(st, data, sd));
  20084. return SCRIPT_CMD_SUCCESS;
  20085. }
  20086. /**
  20087. * Display script message
  20088. * showscript "<message>"{,<GID>,<flag>};
  20089. * @param flag: Specify target
  20090. * AREA - Message is sent to players in the vicinity of the source (default).
  20091. * SELF - Message is sent only to player attached.
  20092. **/
  20093. BUILDIN_FUNC(showscript) {
  20094. struct block_list *bl = NULL;
  20095. const char *msg = script_getstr(st,2);
  20096. int id = 0;
  20097. send_target target = AREA;
  20098. if (script_hasdata(st,3)) {
  20099. id = script_getnum(st,3);
  20100. bl = map_id2bl(id);
  20101. }
  20102. else {
  20103. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  20104. }
  20105. if (!bl) {
  20106. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  20107. script_pushint(st,0);
  20108. return SCRIPT_CMD_FAILURE;
  20109. }
  20110. if (script_hasdata(st, 4)) {
  20111. target = static_cast<send_target>(script_getnum(st, 4));
  20112. if (target == SELF && map_id2sd(bl->id) == NULL) {
  20113. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  20114. return SCRIPT_CMD_FAILURE;
  20115. }
  20116. }
  20117. clif_showscript(bl, msg, target);
  20118. script_pushint(st,1);
  20119. return SCRIPT_CMD_SUCCESS;
  20120. }
  20121. /**
  20122. * Ignore the SECURE_NPCTIMEOUT function.
  20123. * ignoretimeout <flag>{,<char_id>};
  20124. */
  20125. BUILDIN_FUNC(ignoretimeout)
  20126. {
  20127. #ifdef SECURE_NPCTIMEOUT
  20128. struct map_session_data *sd = NULL;
  20129. if (script_hasdata(st,3)) {
  20130. if (!script_isstring(st,3))
  20131. sd = map_charid2sd(script_getnum(st,3));
  20132. else
  20133. sd = map_nick2sd(script_getstr(st,3),false);
  20134. } else
  20135. script_rid2sd(sd);
  20136. if (!sd)
  20137. return SCRIPT_CMD_FAILURE;
  20138. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  20139. #endif
  20140. return SCRIPT_CMD_SUCCESS;
  20141. }
  20142. /**
  20143. * geteleminfo <type>{,<char_id>};
  20144. **/
  20145. BUILDIN_FUNC(geteleminfo) {
  20146. TBL_ELEM *ed = NULL;
  20147. TBL_PC *sd = NULL;
  20148. int type = script_getnum(st,2);
  20149. if (!script_charid2sd(3, sd)) {
  20150. script_pushint(st, 0);
  20151. return SCRIPT_CMD_SUCCESS;
  20152. }
  20153. if (!(ed = sd->ed)) {
  20154. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  20155. script_pushint(st, 0);
  20156. return SCRIPT_CMD_SUCCESS;
  20157. }
  20158. switch (type) {
  20159. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  20160. case 1: script_pushint(st, ed->bl.id); break;
  20161. default:
  20162. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  20163. script_pushint(st, 0);
  20164. return SCRIPT_CMD_FAILURE;
  20165. }
  20166. return SCRIPT_CMD_SUCCESS;
  20167. }
  20168. /**
  20169. * opendressroom(<flag>{,<char_id>});
  20170. */
  20171. BUILDIN_FUNC(opendressroom)
  20172. {
  20173. #if PACKETVER >= 20150513
  20174. int flag = 1;
  20175. TBL_PC* sd;
  20176. if( script_hasdata(st,2) )
  20177. flag = script_getnum(st,2);
  20178. if (!script_charid2sd(3, sd))
  20179. return SCRIPT_CMD_FAILURE;
  20180. clif_dressing_room(sd, flag);
  20181. return SCRIPT_CMD_SUCCESS;
  20182. #else
  20183. return SCRIPT_CMD_FAILURE;
  20184. #endif
  20185. }
  20186. /**
  20187. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  20188. */
  20189. BUILDIN_FUNC(navigateto){
  20190. #if PACKETVER >= 20111010
  20191. TBL_PC* sd;
  20192. const char *mapname;
  20193. uint16 x = 0, y = 0, monster_id = 0;
  20194. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  20195. bool hideWindow = true;
  20196. mapname = script_getstr(st,2);
  20197. if( script_hasdata(st,3) )
  20198. x = script_getnum(st,3);
  20199. if( script_hasdata(st,4) )
  20200. y = script_getnum(st,4);
  20201. if( script_hasdata(st,5) )
  20202. flag = (uint8)script_getnum(st,5);
  20203. if( script_hasdata(st,6) )
  20204. hideWindow = script_getnum(st,6) ? true : false;
  20205. if( script_hasdata(st,7) )
  20206. monster_id = script_getnum(st,7);
  20207. if (!script_charid2sd(8, sd))
  20208. return SCRIPT_CMD_FAILURE;
  20209. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  20210. return SCRIPT_CMD_SUCCESS;
  20211. #else
  20212. return SCRIPT_CMD_FAILURE;
  20213. #endif
  20214. }
  20215. /**
  20216. * adopt("<parent_name>","<baby_name>");
  20217. * adopt(<parent_id>,<baby_id>);
  20218. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  20219. */
  20220. BUILDIN_FUNC(adopt)
  20221. {
  20222. TBL_PC *sd, *b_sd;
  20223. enum adopt_responses response;
  20224. if (script_isstring(st, 2)) {
  20225. const char *name = script_getstr(st, 2);
  20226. sd = map_nick2sd(name,false);
  20227. if (sd == NULL) {
  20228. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  20229. return SCRIPT_CMD_FAILURE;
  20230. }
  20231. } else {
  20232. uint32 char_id = script_getnum(st, 2);
  20233. sd = map_charid2sd(char_id);
  20234. if (sd == NULL) {
  20235. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  20236. return SCRIPT_CMD_FAILURE;
  20237. }
  20238. }
  20239. if (script_isstring(st, 3)) {
  20240. const char *name = script_getstr(st, 3);
  20241. b_sd = map_nick2sd(name,false);
  20242. if (b_sd == NULL) {
  20243. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  20244. return SCRIPT_CMD_FAILURE;
  20245. }
  20246. } else {
  20247. uint32 char_id = script_getnum(st, 3);
  20248. b_sd = map_charid2sd(char_id);
  20249. if (b_sd == NULL) {
  20250. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  20251. return SCRIPT_CMD_FAILURE;
  20252. }
  20253. }
  20254. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  20255. if (response == ADOPT_ALLOWED) {
  20256. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  20257. b_sd->adopt_invite = sd->status.account_id;
  20258. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  20259. script_pushint(st, ADOPT_ALLOWED);
  20260. return SCRIPT_CMD_SUCCESS;
  20261. }
  20262. script_pushint(st, response);
  20263. return SCRIPT_CMD_FAILURE;
  20264. }
  20265. /**
  20266. * Returns the minimum or maximum of all the given parameters for integer variables.
  20267. *
  20268. * min( <value or array>{,value or array 2,...} );
  20269. * minimum( <value or array>{,value or array 2,...} );
  20270. * max( <value or array>{,value or array 2,...} );
  20271. * maximum( <value or array>{,value or array 2,...} );
  20272. */
  20273. BUILDIN_FUNC(minmax){
  20274. char *functionname;
  20275. unsigned int i;
  20276. int64 value;
  20277. // Function pointer for our comparison function (either min or max at the moment)
  20278. int64 (*func)(int64, int64);
  20279. // Get the real function name
  20280. functionname = script_getfuncname(st);
  20281. // Our data should start at offset 2
  20282. i = 2;
  20283. if( !script_hasdata( st, i ) ){
  20284. ShowError( "buildin_%s: no arguments given!\n", functionname );
  20285. st->state = END;
  20286. return SCRIPT_CMD_FAILURE;
  20287. }
  20288. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  20289. value = SCRIPT_INT_MAX;
  20290. func = i64min;
  20291. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  20292. value = SCRIPT_INT_MIN;
  20293. func = i64max;
  20294. }else{
  20295. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  20296. st->state = END;
  20297. return SCRIPT_CMD_FAILURE;
  20298. }
  20299. // As long as we have data on our script stack
  20300. while( script_hasdata(st,i) ){
  20301. struct script_data *data;
  20302. // Get the next piece of data from the script stack
  20303. data = script_getdata( st, i );
  20304. // Is the current parameter a single integer?
  20305. if( data_isint( data ) ){
  20306. value = func( value, script_getnum( st, i ) );
  20307. // Is the current parameter an array variable?
  20308. }else if( data_isreference( data ) ){
  20309. const char *name;
  20310. struct map_session_data* sd;
  20311. unsigned int start, end;
  20312. // Get the name of the variable
  20313. name = reference_getname(data);
  20314. // Check if it's a string variable
  20315. if( is_string_variable( name ) ){
  20316. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20317. script_reportdata( data );
  20318. st->state = END;
  20319. return SCRIPT_CMD_FAILURE;
  20320. }
  20321. // Get the session data, if a player is attached
  20322. sd = st->rid ? map_id2sd(st->rid) : NULL;
  20323. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20324. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20325. return SCRIPT_CMD_FAILURE;
  20326. }
  20327. // Try to find the array's source pointer
  20328. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20329. ShowError( "buildin_%s: not a array!\n", functionname );
  20330. script_reportdata( data );
  20331. st->state = END;
  20332. return SCRIPT_CMD_FAILURE;
  20333. }
  20334. // Get the start and end indices of the array
  20335. start = reference_getindex( data );
  20336. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20337. // Skip empty arrays
  20338. if( start < end ){
  20339. int id;
  20340. // For getting the values we need the id of the array
  20341. id = reference_getid( data );
  20342. // Loop through each value stored in the array
  20343. for( ; start < end; start++ ){
  20344. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20345. }
  20346. }
  20347. else {
  20348. value = func( value, 0 );
  20349. }
  20350. }else{
  20351. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20352. script_reportdata( data );
  20353. st->state = END;
  20354. return SCRIPT_CMD_FAILURE;
  20355. }
  20356. // Continue with the next stack entry
  20357. i++;
  20358. }
  20359. script_pushint( st, value );
  20360. return SCRIPT_CMD_SUCCESS;
  20361. }
  20362. /**
  20363. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20364. * Unlike `getexp` that affected by some adjustments.
  20365. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20366. * @author [Cydh]
  20367. **/
  20368. BUILDIN_FUNC(getexp2) {
  20369. TBL_PC *sd = NULL;
  20370. int64 base_exp = script_getnum64(st, 2);
  20371. int64 job_exp = script_getnum64(st, 3);
  20372. if (!script_charid2sd(4, sd))
  20373. return SCRIPT_CMD_FAILURE;
  20374. if( base_exp == 0 && job_exp == 0 ){
  20375. ShowError( "buildin_getexp2: Called with base and job exp 0.\n" );
  20376. return SCRIPT_CMD_FAILURE;
  20377. }
  20378. if (base_exp > 0)
  20379. pc_gainexp(sd, NULL, base_exp, 0, 2);
  20380. else if (base_exp < 0)
  20381. pc_lostexp(sd, base_exp * -1, 0);
  20382. if (job_exp > 0)
  20383. pc_gainexp(sd, NULL, 0, job_exp, 2);
  20384. else if (job_exp < 0)
  20385. pc_lostexp(sd, 0, job_exp * -1);
  20386. return SCRIPT_CMD_SUCCESS;
  20387. }
  20388. /**
  20389. * Force stat recalculation of sd
  20390. * recalculatestat;
  20391. * @author [secretdataz]
  20392. **/
  20393. BUILDIN_FUNC(recalculatestat) {
  20394. TBL_PC* sd;
  20395. if (!script_rid2sd(sd))
  20396. return SCRIPT_CMD_FAILURE;
  20397. status_calc_pc(sd, SCO_FORCE);
  20398. return SCRIPT_CMD_SUCCESS;
  20399. }
  20400. BUILDIN_FUNC(hateffect){
  20401. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20402. struct map_session_data* sd;
  20403. if( !script_rid2sd(sd) )
  20404. return SCRIPT_CMD_FAILURE;
  20405. int16 effectID = script_getnum(st,2);
  20406. bool enable = script_getnum(st,3) ? true : false;
  20407. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20408. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20409. return SCRIPT_CMD_FAILURE;
  20410. }
  20411. auto it = util::vector_get( sd->hatEffects, effectID );
  20412. if( enable ){
  20413. if( it != sd->hatEffects.end() ){
  20414. return SCRIPT_CMD_SUCCESS;
  20415. }
  20416. sd->hatEffects.push_back( effectID );
  20417. }else{
  20418. if( it == sd->hatEffects.end() ){
  20419. return SCRIPT_CMD_SUCCESS;
  20420. }
  20421. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20422. }
  20423. if( !sd->state.connect_new ){
  20424. clif_hat_effect_single( sd, effectID, enable );
  20425. }
  20426. #endif
  20427. return SCRIPT_CMD_SUCCESS;
  20428. }
  20429. /**
  20430. * Retrieves param of current random option. Intended for random option script only.
  20431. * getrandomoptinfo(<type>);
  20432. * @author [secretdataz]
  20433. **/
  20434. BUILDIN_FUNC(getrandomoptinfo) {
  20435. struct map_session_data *sd;
  20436. int val;
  20437. 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) {
  20438. int param = script_getnum(st, 2);
  20439. switch (param) {
  20440. case ROA_ID:
  20441. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20442. break;
  20443. case ROA_VALUE:
  20444. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20445. break;
  20446. case ROA_PARAM:
  20447. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  20448. break;
  20449. default:
  20450. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  20451. val = 0;
  20452. break;
  20453. }
  20454. script_pushint(st, val);
  20455. }
  20456. else {
  20457. script_pushint(st, 0);
  20458. }
  20459. return SCRIPT_CMD_SUCCESS;
  20460. }
  20461. /**
  20462. * Retrieves a random option on an equipped item.
  20463. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  20464. * @author [secretdataz]
  20465. */
  20466. BUILDIN_FUNC(getequiprandomoption) {
  20467. struct map_session_data *sd;
  20468. int val;
  20469. short i = -1;
  20470. int pos = script_getnum(st, 2);
  20471. int index = script_getnum(st, 3);
  20472. int type = script_getnum(st, 4);
  20473. if (!script_charid2sd(5, sd)) {
  20474. script_pushint(st, -1);
  20475. return SCRIPT_CMD_FAILURE;
  20476. }
  20477. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20478. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  20479. script_pushint(st, -1);
  20480. return SCRIPT_CMD_FAILURE;
  20481. }
  20482. if (equip_index_check(pos))
  20483. i = pc_checkequip(sd, equip_bitmask[pos]);
  20484. if (i < 0) {
  20485. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20486. script_pushint(st, -1);
  20487. return SCRIPT_CMD_FAILURE;
  20488. }
  20489. switch (type) {
  20490. case ROA_ID:
  20491. val = sd->inventory.u.items_inventory[i].option[index].id;
  20492. break;
  20493. case ROA_VALUE:
  20494. val = sd->inventory.u.items_inventory[i].option[index].value;
  20495. break;
  20496. case ROA_PARAM:
  20497. val = sd->inventory.u.items_inventory[i].option[index].param;
  20498. break;
  20499. default:
  20500. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  20501. val = 0;
  20502. break;
  20503. }
  20504. script_pushint(st, val);
  20505. return SCRIPT_CMD_SUCCESS;
  20506. }
  20507. /**
  20508. * Adds a random option on a random option slot on an equipped item and overwrites
  20509. * existing random option in the process.
  20510. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  20511. * @author [secretdataz]
  20512. */
  20513. BUILDIN_FUNC(setrandomoption) {
  20514. struct map_session_data *sd;
  20515. int pos, index, id, value, param, ep;
  20516. int i = -1;
  20517. if (!script_charid2sd(7, sd))
  20518. return SCRIPT_CMD_FAILURE;
  20519. pos = script_getnum(st, 2);
  20520. index = script_getnum(st, 3);
  20521. id = script_getnum(st, 4);
  20522. value = script_getnum(st, 5);
  20523. param = script_getnum(st, 6);
  20524. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  20525. if (opt == nullptr) {
  20526. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  20527. script_pushint(st, 0);
  20528. return SCRIPT_CMD_FAILURE;
  20529. }
  20530. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20531. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  20532. script_pushint(st, 0);
  20533. return SCRIPT_CMD_FAILURE;
  20534. }
  20535. if (equip_index_check(pos))
  20536. i = pc_checkequip(sd, equip_bitmask[pos]);
  20537. if (i >= 0) {
  20538. ep = sd->inventory.u.items_inventory[i].equip;
  20539. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20540. pc_unequipitem(sd, i, 2);
  20541. sd->inventory.u.items_inventory[i].option[index].id = id;
  20542. sd->inventory.u.items_inventory[i].option[index].value = value;
  20543. sd->inventory.u.items_inventory[i].option[index].param = param;
  20544. clif_delitem(sd, i, 1, 3);
  20545. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20546. clif_additem(sd, i, 1, 0);
  20547. pc_equipitem(sd, i, ep);
  20548. script_pushint(st, 1);
  20549. return SCRIPT_CMD_SUCCESS;
  20550. }
  20551. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20552. script_pushint(st, 0);
  20553. return SCRIPT_CMD_FAILURE;
  20554. }
  20555. /// Returns the number of stat points needed to change the specified stat by val.
  20556. /// If val is negative, returns the number of stat points that would be needed to
  20557. /// raise the specified stat from (current value - val) to current value.
  20558. /// *needed_status_point(<type>,<val>{,<char id>});
  20559. /// @author [secretdataz]
  20560. BUILDIN_FUNC(needed_status_point) {
  20561. struct map_session_data *sd;
  20562. int type, val;
  20563. if (!script_charid2sd(4, sd))
  20564. return SCRIPT_CMD_FAILURE;
  20565. type = script_getnum(st, 2);
  20566. val = script_getnum(st, 3);
  20567. script_pushint(st, pc_need_status_point(sd, type, val));
  20568. return SCRIPT_CMD_SUCCESS;
  20569. }
  20570. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  20571. /// If val is negative, returns the number of trait stat points that would be needed to
  20572. /// raise the specified trait stat from (current value - val) to current value.
  20573. /// *needed_trait_point(<type>,<val>{,<char id>});
  20574. BUILDIN_FUNC(needed_trait_point) {
  20575. struct map_session_data *sd;
  20576. if (!script_charid2sd(4, sd))
  20577. return SCRIPT_CMD_FAILURE;
  20578. int type = script_getnum( st, 2 );
  20579. if( type < SP_POW || type > SP_CRT ){
  20580. ShowError( "buildin_needed_trait_point: Unknown trait type %d\n", type );
  20581. return SCRIPT_CMD_FAILURE;
  20582. }
  20583. script_pushint( st, pc_need_trait_point( sd, type, script_getnum( st, 3 ) ) );
  20584. return SCRIPT_CMD_SUCCESS;
  20585. }
  20586. /**
  20587. * jobcanentermap("<mapname>"{,<JobID>});
  20588. * Check if (player with) JobID can enter the map.
  20589. * @param mapname Map name
  20590. * @param JobID Player's JobID (optional)
  20591. **/
  20592. BUILDIN_FUNC(jobcanentermap) {
  20593. const char *mapname = script_getstr(st, 2);
  20594. int mapidx = mapindex_name2id(mapname), m = -1;
  20595. int jobid = 0;
  20596. TBL_PC *sd = NULL;
  20597. if (!mapidx) {// Invalid map
  20598. script_pushint(st, false);
  20599. return SCRIPT_CMD_FAILURE;
  20600. }
  20601. m = map_mapindex2mapid(mapidx);
  20602. if (m == -1) { // Map is on different map server
  20603. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  20604. script_pushint(st, false);
  20605. return SCRIPT_CMD_FAILURE;
  20606. }
  20607. if (script_hasdata(st, 3)) {
  20608. jobid = script_getnum(st, 3);
  20609. } else {
  20610. if (!script_rid2sd(sd)) {
  20611. script_pushint(st, false);
  20612. return SCRIPT_CMD_FAILURE;
  20613. }
  20614. jobid = sd->status.class_;
  20615. }
  20616. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  20617. return SCRIPT_CMD_SUCCESS;
  20618. }
  20619. /**
  20620. * Return alliance information between the two guilds.
  20621. * getguildalliance(<guild id1>,<guild id2>);
  20622. * Return values:
  20623. * -2 - Guild ID1 does not exist
  20624. * -1 - Guild ID2 does not exist
  20625. * 0 - Both guilds have no relation OR guild ID aren't given
  20626. * 1 - Both guilds are allies
  20627. * 2 - Both guilds are antagonists
  20628. */
  20629. BUILDIN_FUNC(getguildalliance)
  20630. {
  20631. struct guild *guild_data1, *guild_data2;
  20632. int guild_id1, guild_id2, i = 0;
  20633. guild_id1 = script_getnum(st,2);
  20634. guild_id2 = script_getnum(st,3);
  20635. if (guild_id1 < 1 || guild_id2 < 1) {
  20636. script_pushint(st, 0);
  20637. return SCRIPT_CMD_SUCCESS;
  20638. }
  20639. if (guild_id1 == guild_id2) {
  20640. script_pushint(st, 1);
  20641. return SCRIPT_CMD_SUCCESS;
  20642. }
  20643. guild_data1 = guild_search(guild_id1);
  20644. guild_data2 = guild_search(guild_id2);
  20645. if (guild_data1 == NULL) {
  20646. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  20647. script_pushint(st, -2);
  20648. return SCRIPT_CMD_FAILURE;
  20649. }
  20650. if (guild_data2 == NULL) {
  20651. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  20652. script_pushint(st, -1);
  20653. return SCRIPT_CMD_FAILURE;
  20654. }
  20655. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  20656. if (i == MAX_GUILDALLIANCE) {
  20657. script_pushint(st, 0);
  20658. return SCRIPT_CMD_SUCCESS;
  20659. }
  20660. if (guild_data1->alliance[i].opposition)
  20661. script_pushint(st, 2);
  20662. else
  20663. script_pushint(st, 1);
  20664. return SCRIPT_CMD_SUCCESS;
  20665. }
  20666. /*
  20667. * openstorage2 <storage_id>,<mode>{,<account_id>}
  20668. * mode @see enum e_storage_mode
  20669. **/
  20670. BUILDIN_FUNC(openstorage2) {
  20671. int stor_id = script_getnum(st, 2);
  20672. TBL_PC *sd = NULL;
  20673. if (!script_accid2sd(4, sd)) {
  20674. script_pushint(st, 0);
  20675. return SCRIPT_CMD_FAILURE;
  20676. }
  20677. if (!storage_exists(stor_id)) {
  20678. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  20679. return SCRIPT_CMD_FAILURE;
  20680. }
  20681. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  20682. return SCRIPT_CMD_SUCCESS;
  20683. }
  20684. /**
  20685. * Create a new channel
  20686. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  20687. * @author [Cydh]
  20688. **/
  20689. BUILDIN_FUNC(channel_create) {
  20690. struct Channel tmp_chan, *ch = NULL;
  20691. const char *chname = script_getstr(st,2), *pass = NULL;
  20692. int i = channel_chk((char*)chname, NULL, 3);
  20693. TBL_PC *sd = NULL;
  20694. if (i != 0) {
  20695. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  20696. script_pushint(st,i);
  20697. return SCRIPT_CMD_FAILURE;
  20698. }
  20699. memset(&tmp_chan, 0, sizeof(struct Channel));
  20700. if (script_hasdata(st,8)) {
  20701. tmp_chan.char_id = script_getnum(st,8);
  20702. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  20703. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  20704. script_pushint(st,-5);
  20705. return SCRIPT_CMD_FAILURE;
  20706. }
  20707. tmp_chan.type = CHAN_TYPE_PRIVATE;
  20708. i = 1;
  20709. }
  20710. else {
  20711. tmp_chan.type = CHAN_TYPE_PUBLIC;
  20712. i = 0;
  20713. }
  20714. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  20715. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  20716. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  20717. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  20718. if (script_hasdata(st,5))
  20719. tmp_chan.opt = script_getnum(st,5);
  20720. else
  20721. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  20722. if (script_hasdata(st,6))
  20723. tmp_chan.msg_delay = script_getnum(st,6);
  20724. else
  20725. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  20726. if (script_hasdata(st,7))
  20727. tmp_chan.color = script_getnum(st,7);
  20728. else
  20729. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  20730. if (!(ch = channel_create(&tmp_chan))) {
  20731. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  20732. script_pushint(st,0);
  20733. return SCRIPT_CMD_FAILURE;
  20734. }
  20735. if (tmp_chan.char_id)
  20736. channel_join(ch, sd);
  20737. script_pushint(st,1);
  20738. return SCRIPT_CMD_SUCCESS;
  20739. }
  20740. /**
  20741. * Set channel option
  20742. * channel_setopt "<chname>",<option>,<value>;
  20743. * @author [Cydh]
  20744. **/
  20745. BUILDIN_FUNC(channel_setopt) {
  20746. struct Channel *ch = NULL;
  20747. const char *chname = script_getstr(st,2);
  20748. int opt = script_getnum(st,3), value = script_getnum(st,4);
  20749. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20750. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  20751. script_pushint(st,0);
  20752. return SCRIPT_CMD_FAILURE;
  20753. }
  20754. switch (opt) {
  20755. case CHAN_OPT_ANNOUNCE_SELF:
  20756. case CHAN_OPT_ANNOUNCE_JOIN:
  20757. case CHAN_OPT_ANNOUNCE_LEAVE:
  20758. case CHAN_OPT_COLOR_OVERRIDE:
  20759. case CHAN_OPT_CAN_CHAT:
  20760. case CHAN_OPT_CAN_LEAVE:
  20761. case CHAN_OPT_AUTOJOIN:
  20762. if (value)
  20763. ch->opt |= opt;
  20764. else
  20765. ch->opt &= ~opt;
  20766. break;
  20767. case CHAN_OPT_MSG_DELAY:
  20768. ch->msg_delay = value;
  20769. break;
  20770. default:
  20771. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  20772. script_pushint(st,0);
  20773. return SCRIPT_CMD_FAILURE;
  20774. }
  20775. script_pushint(st,1);
  20776. return SCRIPT_CMD_SUCCESS;
  20777. }
  20778. /**
  20779. * Get channel options
  20780. * channel_getopt <chname>,<option>;
  20781. */
  20782. BUILDIN_FUNC(channel_getopt) {
  20783. Channel *ch;
  20784. const char *chname = script_getstr(st, 2);
  20785. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20786. ShowError("buildin_channel_getopt: Channel name '%s' is invalid.\n", chname);
  20787. script_pushint(st, false);
  20788. return SCRIPT_CMD_FAILURE;
  20789. }
  20790. int opt = script_getnum(st, 3);
  20791. switch (opt) {
  20792. case CHAN_OPT_ANNOUNCE_SELF:
  20793. case CHAN_OPT_ANNOUNCE_JOIN:
  20794. case CHAN_OPT_ANNOUNCE_LEAVE:
  20795. case CHAN_OPT_COLOR_OVERRIDE:
  20796. case CHAN_OPT_CAN_CHAT:
  20797. case CHAN_OPT_CAN_LEAVE:
  20798. case CHAN_OPT_AUTOJOIN:
  20799. script_pushint(st, (ch->opt & opt) != 0);
  20800. break;
  20801. case CHAN_OPT_MSG_DELAY:
  20802. script_pushint(st, ch->msg_delay);
  20803. break;
  20804. default:
  20805. ShowError("buildin_channel_getopt: Invalid option %d!\n", opt);
  20806. script_pushint(st, false);
  20807. return SCRIPT_CMD_FAILURE;
  20808. }
  20809. return SCRIPT_CMD_SUCCESS;
  20810. }
  20811. /**
  20812. * Set channel color
  20813. * channel_setcolor "<chname>",<color>;
  20814. * @author [Cydh]
  20815. **/
  20816. BUILDIN_FUNC(channel_setcolor) {
  20817. struct Channel *ch = NULL;
  20818. const char *chname = script_getstr(st,2);
  20819. int color = script_getnum(st,3);
  20820. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20821. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  20822. script_pushint(st,0);
  20823. return SCRIPT_CMD_FAILURE;
  20824. }
  20825. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  20826. script_pushint(st,1);
  20827. return SCRIPT_CMD_SUCCESS;
  20828. }
  20829. /**
  20830. * Set channel password
  20831. * channel_setpass "<chname>","<password>";
  20832. * @author [Cydh]
  20833. **/
  20834. BUILDIN_FUNC(channel_setpass) {
  20835. struct Channel *ch = NULL;
  20836. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  20837. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20838. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  20839. script_pushint(st,0);
  20840. return SCRIPT_CMD_FAILURE;
  20841. }
  20842. if (!passwd || !strcmpi(passwd,"null"))
  20843. memset(ch->pass, '\0', sizeof(ch->pass));
  20844. else
  20845. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  20846. script_pushint(st,1);
  20847. return SCRIPT_CMD_SUCCESS;
  20848. }
  20849. /**
  20850. * Set authorized groups
  20851. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  20852. * @author [Cydh]
  20853. **/
  20854. BUILDIN_FUNC(channel_setgroup) {
  20855. struct Channel *ch = NULL;
  20856. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  20857. int i, n = 0, group = 0;
  20858. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20859. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  20860. script_pushint(st,0);
  20861. return SCRIPT_CMD_FAILURE;
  20862. }
  20863. if (funcname[strlen(funcname)-1] == '2') {
  20864. struct script_data *data = script_getdata(st,3);
  20865. const char *varname = reference_getname(data);
  20866. int32 id, idx;
  20867. if (varname[strlen(varname)-1] == '$') {
  20868. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  20869. script_pushint(st,0);
  20870. return SCRIPT_CMD_FAILURE;
  20871. }
  20872. if (not_server_variable(*varname)) {
  20873. struct map_session_data *sd;
  20874. if (!script_rid2sd(sd)) {
  20875. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  20876. return SCRIPT_CMD_FAILURE;
  20877. }
  20878. }
  20879. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  20880. if (n < 1) {
  20881. ShowError("buildin_channel_setgroup: No group id listed.\n");
  20882. script_pushint(st,0);
  20883. return SCRIPT_CMD_FAILURE;
  20884. }
  20885. if (ch->groups)
  20886. aFree(ch->groups);
  20887. ch->groups = NULL;
  20888. ch->group_count = 0;
  20889. id = reference_getid(data);
  20890. idx = reference_getindex(data);
  20891. for (i = 0; i < n; i++) {
  20892. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  20893. if (group == 0) {
  20894. script_pushint(st,1);
  20895. return SCRIPT_CMD_SUCCESS;
  20896. }
  20897. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  20898. ch->groups[ch->group_count-1] = group;
  20899. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20900. }
  20901. }
  20902. else {
  20903. group = script_getnum(st,3);
  20904. n = script_lastdata(st)-1;
  20905. if (n < 1) {
  20906. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  20907. script_pushint(st,0);
  20908. return SCRIPT_CMD_FAILURE;
  20909. }
  20910. if (ch->groups)
  20911. aFree(ch->groups);
  20912. ch->groups = NULL;
  20913. ch->group_count = 0;
  20914. if (group == 0) { // Removed group list
  20915. script_pushint(st,1);
  20916. return SCRIPT_CMD_SUCCESS;
  20917. }
  20918. CREATE(ch->groups, unsigned short, n);
  20919. for (i = 3; i < n+2; i++) {
  20920. ch->groups[ch->group_count++] = script_getnum(st,i);
  20921. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20922. }
  20923. }
  20924. script_pushint(st,n);
  20925. return SCRIPT_CMD_SUCCESS;
  20926. }
  20927. /**
  20928. * Send message on channel
  20929. * channel_chat "<chname>","<message>"{,<color>};
  20930. * @author [Cydh]
  20931. **/
  20932. BUILDIN_FUNC(channel_chat) {
  20933. struct Channel *ch = NULL;
  20934. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  20935. char output[CHAT_SIZE_MAX+1];
  20936. unsigned long color = 0;
  20937. // Check also local channels
  20938. if (chname[0] != '#') { // By Map
  20939. int m = mapindex_name2id(chname);
  20940. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  20941. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  20942. script_pushint(st,0);
  20943. return SCRIPT_CMD_FAILURE;
  20944. }
  20945. if (!(ch = map_getmapdata(m)->channel)) {
  20946. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20947. script_pushint(st,0);
  20948. return SCRIPT_CMD_SUCCESS;
  20949. }
  20950. }
  20951. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  20952. TBL_NPC *nd = map_id2nd(st->oid);
  20953. if (!nd || nd->bl.m == -1) {
  20954. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  20955. script_pushint(st,0);
  20956. return SCRIPT_CMD_FAILURE;
  20957. }
  20958. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  20959. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20960. script_pushint(st,0);
  20961. return SCRIPT_CMD_SUCCESS;
  20962. }
  20963. }
  20964. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20965. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  20966. script_pushint(st,0);
  20967. return SCRIPT_CMD_FAILURE;
  20968. }
  20969. if (!ch) {
  20970. script_pushint(st,0);
  20971. return SCRIPT_CMD_FAILURE;
  20972. }
  20973. color = ch->color;
  20974. FETCH(4, color);
  20975. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  20976. clif_channel_msg(ch,output,color);
  20977. script_pushint(st,1);
  20978. return SCRIPT_CMD_SUCCESS;
  20979. }
  20980. /**
  20981. * Ban player from a channel
  20982. * channel_ban "<chname>",<char_id>;
  20983. * @author [Cydh]
  20984. **/
  20985. BUILDIN_FUNC(channel_ban) {
  20986. struct Channel *ch = NULL;
  20987. const char *chname = script_getstr(st,2);
  20988. unsigned int char_id = script_getnum(st,3);
  20989. TBL_PC *tsd;
  20990. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20991. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  20992. script_pushint(st,0);
  20993. return SCRIPT_CMD_FAILURE;
  20994. }
  20995. if (ch->char_id == char_id) {
  20996. script_pushint(st,0);
  20997. return SCRIPT_CMD_SUCCESS;
  20998. }
  20999. tsd = map_charid2sd(char_id);
  21000. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21001. script_pushint(st,0);
  21002. return SCRIPT_CMD_SUCCESS;
  21003. }
  21004. if (!idb_exists(ch->banned, char_id)) {
  21005. struct chan_banentry *cbe;
  21006. char output[CHAT_SIZE_MAX+1];
  21007. CREATE(cbe, struct chan_banentry, 1);
  21008. cbe->char_id = char_id;
  21009. if (tsd) {
  21010. strcpy(cbe->char_name,tsd->status.name);
  21011. channel_clean(ch,tsd,0);
  21012. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  21013. }
  21014. else
  21015. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  21016. idb_put(ch->banned, char_id, cbe);
  21017. clif_channel_msg(ch,output,ch->color);
  21018. }
  21019. script_pushint(st,1);
  21020. return SCRIPT_CMD_SUCCESS;
  21021. }
  21022. /**
  21023. * Ban player from a channel
  21024. * channel_unban "<chname>",<char_id>;
  21025. * @author [Cydh]
  21026. **/
  21027. BUILDIN_FUNC(channel_unban) {
  21028. struct Channel *ch = NULL;
  21029. const char *chname = script_getstr(st,2);
  21030. unsigned int char_id = script_getnum(st,3);
  21031. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21032. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  21033. script_pushint(st,0);
  21034. return SCRIPT_CMD_FAILURE;
  21035. }
  21036. if (ch->char_id == char_id) {
  21037. script_pushint(st,0);
  21038. return SCRIPT_CMD_SUCCESS;
  21039. }
  21040. if (idb_exists(ch->banned, char_id)) {
  21041. char output[CHAT_SIZE_MAX+1];
  21042. TBL_PC *tsd = map_charid2sd(char_id);
  21043. if (tsd)
  21044. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  21045. else
  21046. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  21047. idb_remove(ch->banned, char_id);
  21048. clif_channel_msg(ch,output,ch->color);
  21049. }
  21050. script_pushint(st,1);
  21051. return SCRIPT_CMD_SUCCESS;
  21052. }
  21053. /**
  21054. * Kick player from a channel
  21055. * channel_kick "<chname>",<char_id>;
  21056. * channel_kick "<chname>","<char_name>";
  21057. * @author [Cydh]
  21058. **/
  21059. BUILDIN_FUNC(channel_kick) {
  21060. struct Channel *ch = NULL;
  21061. const char *chname = script_getstr(st,2);
  21062. TBL_PC *tsd = NULL;
  21063. int res = 1;
  21064. if (script_isstring(st, 3)) {
  21065. const char *name = script_getstr(st, 3);
  21066. if (!(tsd = map_nick2sd(name,false))) {
  21067. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  21068. script_pushint(st,0);
  21069. return SCRIPT_CMD_FAILURE;
  21070. }
  21071. } else {
  21072. int char_id = script_getnum(st, 3);
  21073. if (!(tsd = map_charid2sd(char_id))) {
  21074. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  21075. script_pushint(st,0);
  21076. return SCRIPT_CMD_FAILURE;
  21077. }
  21078. }
  21079. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  21080. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  21081. script_pushint(st,0);
  21082. return SCRIPT_CMD_FAILURE;
  21083. }
  21084. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21085. script_pushint(st,0);
  21086. return SCRIPT_CMD_SUCCESS;
  21087. }
  21088. switch(ch->type){
  21089. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  21090. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  21091. default: res = channel_clean(ch,tsd,0); break;
  21092. }
  21093. if (res == 0) {
  21094. char output[CHAT_SIZE_MAX+1];
  21095. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  21096. clif_channel_msg(ch,output,ch->color);
  21097. }
  21098. script_pushint(st,1);
  21099. return SCRIPT_CMD_SUCCESS;
  21100. }
  21101. /**
  21102. * Delete a channel
  21103. * channel_delete "<chname>";
  21104. * @author [Cydh]
  21105. **/
  21106. BUILDIN_FUNC(channel_delete) {
  21107. struct Channel *ch = NULL;
  21108. const char *chname = script_getstr(st,2);
  21109. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21110. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  21111. script_pushint(st,-1);
  21112. return SCRIPT_CMD_FAILURE;
  21113. }
  21114. script_pushint(st,channel_delete(ch,true));
  21115. return SCRIPT_CMD_SUCCESS;
  21116. }
  21117. BUILDIN_FUNC(unloadnpc) {
  21118. const char *name;
  21119. struct npc_data* nd;
  21120. name = script_getstr(st, 2);
  21121. nd = npc_name2id(name);
  21122. if( nd == NULL ){
  21123. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  21124. return SCRIPT_CMD_FAILURE;
  21125. } else if ( nd->bl.id == st->oid ) {
  21126. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  21127. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  21128. return SCRIPT_CMD_FAILURE;
  21129. }
  21130. npc_unload_duplicates(nd);
  21131. npc_unload(nd, true);
  21132. npc_read_event_script();
  21133. return SCRIPT_CMD_SUCCESS;
  21134. }
  21135. /**
  21136. * Add an achievement to the player's log
  21137. * achievementadd(<achievement ID>{,<char ID>});
  21138. */
  21139. BUILDIN_FUNC(achievementadd) {
  21140. struct map_session_data *sd;
  21141. int achievement_id = script_getnum(st, 2);
  21142. if (!script_charid2sd(3, sd)) {
  21143. script_pushint(st, false);
  21144. return SCRIPT_CMD_FAILURE;
  21145. }
  21146. if (achievement_db.exists(achievement_id) == false) {
  21147. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  21148. script_pushint(st, false);
  21149. return SCRIPT_CMD_FAILURE;
  21150. }
  21151. if( !sd->state.pc_loaded ){
  21152. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21153. return SCRIPT_CMD_SUCCESS;
  21154. }
  21155. if (achievement_add(sd, achievement_id))
  21156. script_pushint(st, true);
  21157. else
  21158. script_pushint(st, false);
  21159. return SCRIPT_CMD_SUCCESS;
  21160. }
  21161. /**
  21162. * Removes an achievement on a player.
  21163. * achievementremove(<achievement ID>{,<char ID>});
  21164. * Just for Atemo. ;)
  21165. */
  21166. BUILDIN_FUNC(achievementremove) {
  21167. struct map_session_data *sd;
  21168. int achievement_id = script_getnum(st, 2);
  21169. if (!script_charid2sd(3, sd)) {
  21170. script_pushint(st, false);
  21171. return SCRIPT_CMD_FAILURE;
  21172. }
  21173. if (achievement_db.exists(achievement_id) == false) {
  21174. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  21175. script_pushint(st, false);
  21176. return SCRIPT_CMD_SUCCESS;
  21177. }
  21178. if( !sd->state.pc_loaded ){
  21179. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21180. return SCRIPT_CMD_SUCCESS;
  21181. }
  21182. if (achievement_remove(sd, achievement_id))
  21183. script_pushint(st, true);
  21184. else
  21185. script_pushint(st, false);
  21186. return SCRIPT_CMD_SUCCESS;
  21187. }
  21188. /**
  21189. * Returns achievement progress
  21190. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  21191. */
  21192. BUILDIN_FUNC(achievementinfo) {
  21193. struct map_session_data *sd;
  21194. int achievement_id = script_getnum(st, 2);
  21195. if (!script_charid2sd(4, sd)) {
  21196. script_pushint(st, false);
  21197. return SCRIPT_CMD_FAILURE;
  21198. }
  21199. if (achievement_db.exists(achievement_id) == false) {
  21200. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  21201. script_pushint(st, false);
  21202. return SCRIPT_CMD_FAILURE;
  21203. }
  21204. if( !sd->state.pc_loaded ){
  21205. script_pushint(st, false);
  21206. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21207. return SCRIPT_CMD_SUCCESS;
  21208. }
  21209. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  21210. return SCRIPT_CMD_SUCCESS;
  21211. }
  21212. /**
  21213. * Award an achievement; Ignores requirements
  21214. * achievementcomplete(<achievement ID>{,<char ID>});
  21215. */
  21216. BUILDIN_FUNC(achievementcomplete) {
  21217. struct map_session_data *sd;
  21218. int i, achievement_id = script_getnum(st, 2);
  21219. if (!script_charid2sd(3, sd)) {
  21220. script_pushint(st, false);
  21221. return SCRIPT_CMD_FAILURE;
  21222. }
  21223. if (achievement_db.exists(achievement_id) == false) {
  21224. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  21225. script_pushint(st, false);
  21226. return SCRIPT_CMD_FAILURE;
  21227. }
  21228. if( !sd->state.pc_loaded ){
  21229. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21230. return SCRIPT_CMD_SUCCESS;
  21231. }
  21232. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21233. if (i == sd->achievement_data.count)
  21234. achievement_add(sd, achievement_id);
  21235. achievement_update_achievement(sd, achievement_id, true);
  21236. script_pushint(st, true);
  21237. return SCRIPT_CMD_SUCCESS;
  21238. }
  21239. /**
  21240. * Checks if the achievement exists on player.
  21241. * achievementexists(<achievement ID>{,<char ID>});
  21242. */
  21243. BUILDIN_FUNC(achievementexists) {
  21244. struct map_session_data *sd;
  21245. int i, achievement_id = script_getnum(st, 2);
  21246. if (!script_charid2sd(3, sd)) {
  21247. script_pushint(st, false);
  21248. return SCRIPT_CMD_FAILURE;
  21249. }
  21250. if (achievement_db.exists(achievement_id) == false) {
  21251. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  21252. script_pushint(st, false);
  21253. return SCRIPT_CMD_SUCCESS;
  21254. }
  21255. if( !sd->state.pc_loaded ){
  21256. script_pushint(st, false);
  21257. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21258. return SCRIPT_CMD_SUCCESS;
  21259. }
  21260. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  21261. script_pushint(st, i < sd->achievement_data.count ? true : false);
  21262. return SCRIPT_CMD_SUCCESS;
  21263. }
  21264. /**
  21265. * Updates an achievement's value.
  21266. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  21267. */
  21268. BUILDIN_FUNC(achievementupdate) {
  21269. struct map_session_data *sd;
  21270. int i, achievement_id, type, value;
  21271. achievement_id = script_getnum(st, 2);
  21272. type = script_getnum(st, 3);
  21273. value = script_getnum(st, 4);
  21274. if (!script_charid2sd(5, sd)) {
  21275. script_pushint(st, false);
  21276. return SCRIPT_CMD_FAILURE;
  21277. }
  21278. if (achievement_db.exists(achievement_id) == false) {
  21279. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  21280. script_pushint(st, false);
  21281. return SCRIPT_CMD_FAILURE;
  21282. }
  21283. if( !sd->state.pc_loaded ){
  21284. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21285. return SCRIPT_CMD_SUCCESS;
  21286. }
  21287. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21288. if (i == sd->achievement_data.count)
  21289. achievement_add(sd, achievement_id);
  21290. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21291. if (i == sd->achievement_data.count) {
  21292. script_pushint(st, false);
  21293. return SCRIPT_CMD_SUCCESS;
  21294. }
  21295. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  21296. sd->achievement_data.achievements[i].count[type - 1] = value;
  21297. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  21298. sd->achievement_data.achievements[i].completed = value;
  21299. else if (type == ACHIEVEINFO_GOTREWARD)
  21300. sd->achievement_data.achievements[i].rewarded = value;
  21301. else {
  21302. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  21303. script_pushint(st, false);
  21304. return SCRIPT_CMD_FAILURE;
  21305. }
  21306. achievement_update_achievement(sd, achievement_id, false);
  21307. script_pushint(st, true);
  21308. return SCRIPT_CMD_SUCCESS;
  21309. }
  21310. /**
  21311. * Get an equipment's refine cost
  21312. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  21313. * returns -1 on fail
  21314. */
  21315. BUILDIN_FUNC(getequiprefinecost) {
  21316. int i = -1, slot, type, info;
  21317. map_session_data *sd;
  21318. slot = script_getnum(st, 2);
  21319. type = script_getnum(st, 3);
  21320. info = script_getnum(st, 4);
  21321. if (!script_charid2sd(5, sd)) {
  21322. script_pushint(st, 0);
  21323. return SCRIPT_CMD_FAILURE;
  21324. }
  21325. if (type < 0 || type >= REFINE_COST_MAX) {
  21326. script_pushint(st, -1);
  21327. return SCRIPT_CMD_SUCCESS;
  21328. }
  21329. if (equip_index_check(slot))
  21330. i = pc_checkequip(sd, equip_bitmask[slot]);
  21331. if (i < 0) {
  21332. script_pushint(st, -1);
  21333. return SCRIPT_CMD_SUCCESS;
  21334. }
  21335. if( sd->inventory_data[i] == nullptr ){
  21336. script_pushint( st, -1 );
  21337. return SCRIPT_CMD_SUCCESS;
  21338. }
  21339. std::shared_ptr<s_refine_level_info> level_info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  21340. if( level_info == nullptr ){
  21341. script_pushint( st, -1 );
  21342. return SCRIPT_CMD_SUCCESS;
  21343. }
  21344. std::shared_ptr<s_refine_cost> cost = util::umap_find( level_info->costs, (uint16)type );
  21345. if( cost == nullptr ){
  21346. script_pushint( st, -1 );
  21347. return SCRIPT_CMD_SUCCESS;
  21348. }
  21349. switch( info ){
  21350. case REFINE_MATERIAL_ID:
  21351. script_pushint( st, cost->nameid );
  21352. break;
  21353. case REFINE_ZENY_COST:
  21354. script_pushint( st, cost->zeny );
  21355. break;
  21356. default:
  21357. script_pushint( st, -1 );
  21358. return SCRIPT_CMD_FAILURE;
  21359. }
  21360. return SCRIPT_CMD_SUCCESS;
  21361. }
  21362. /**
  21363. * Round, floor, ceiling a number to arbitrary integer precision.
  21364. * round(<number>,<precision>);
  21365. * ceil(<number>,<precision>);
  21366. * floor(<number>,<precision>);
  21367. */
  21368. BUILDIN_FUNC(round) {
  21369. int num = script_getnum(st, 2);
  21370. int precision = script_getnum(st, 3);
  21371. char* func = script_getfuncname(st);
  21372. if (precision <= 0) {
  21373. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  21374. return SCRIPT_CMD_FAILURE;
  21375. }
  21376. if (strcasecmp(func, "floor") == 0) {
  21377. script_pushint(st, num - (num % precision));
  21378. }
  21379. else if (strcasecmp(func, "ceil") == 0) {
  21380. script_pushint(st, num + precision - (num % precision));
  21381. }
  21382. else {
  21383. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  21384. }
  21385. return SCRIPT_CMD_SUCCESS;
  21386. }
  21387. BUILDIN_FUNC(getequiptradability) {
  21388. int i, num;
  21389. TBL_PC *sd;
  21390. num = script_getnum(st, 2);
  21391. if (!script_charid2sd(3, sd)) {
  21392. return SCRIPT_CMD_FAILURE;
  21393. }
  21394. if (equip_index_check(num))
  21395. i = pc_checkequip(sd, equip_bitmask[num]);
  21396. else{
  21397. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  21398. return SCRIPT_CMD_FAILURE;
  21399. }
  21400. if (i >= 0)
  21401. script_pushint(st, pc_can_trade_item(sd, i));
  21402. else
  21403. script_pushint(st, false);
  21404. return SCRIPT_CMD_SUCCESS;
  21405. }
  21406. 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 ){
  21407. const char *name;
  21408. // Check if it is a variable
  21409. if( !data_isreference(data) ){
  21410. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  21411. return false;
  21412. }
  21413. name = reference_getname(data);
  21414. if( is_string_variable(name) ){
  21415. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  21416. return false;
  21417. }
  21418. // Check if the variable requires a player
  21419. if( not_server_variable(*name) && sd == NULL ){
  21420. // If no player is attached
  21421. if( !script_rid2sd(sd) ){
  21422. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  21423. return false;
  21424. }
  21425. }
  21426. // Try to find the array's source pointer
  21427. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  21428. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  21429. return false;
  21430. }
  21431. // Store the name for later usage
  21432. *out_name = name;
  21433. // Get the start and end indices of the array
  21434. *start = reference_getindex(data);
  21435. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  21436. // For getting the values we need the id of the array
  21437. *id = reference_getid(data);
  21438. return true;
  21439. }
  21440. // 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>}}}}}}}}};
  21441. BUILDIN_FUNC(mail){
  21442. const char *sender, *title, *body, *name;
  21443. struct mail_message msg;
  21444. struct script_data *data;
  21445. struct map_session_data *sd = NULL;
  21446. unsigned int i, j, k, num_items, start, end;
  21447. int32 id;
  21448. memset(&msg, 0, sizeof(struct mail_message));
  21449. msg.dest_id = script_getnum(st,2);
  21450. sender = script_getstr(st, 3);
  21451. if( strlen(sender) > NAME_LENGTH ){
  21452. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  21453. return SCRIPT_CMD_FAILURE;
  21454. }
  21455. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  21456. title = script_getstr(st, 4);
  21457. if( strlen(title) > MAIL_TITLE_LENGTH ){
  21458. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  21459. return SCRIPT_CMD_FAILURE;
  21460. }
  21461. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  21462. body = script_getstr(st, 5);
  21463. if( strlen(body) > MAIL_BODY_LENGTH ){
  21464. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  21465. return SCRIPT_CMD_FAILURE;
  21466. }
  21467. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  21468. if( script_hasdata(st,6) ){
  21469. int zeny = script_getnum(st, 6);
  21470. if( zeny < 0 ){
  21471. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  21472. return SCRIPT_CMD_FAILURE;
  21473. }else if( zeny > MAX_ZENY ){
  21474. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  21475. zeny = MAX_ZENY;
  21476. }
  21477. msg.zeny = zeny;
  21478. }
  21479. // Items
  21480. num_items = 0;
  21481. while( script_hasdata(st,7) ){
  21482. data = script_getdata(st,7);
  21483. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  21484. return SCRIPT_CMD_FAILURE;
  21485. }
  21486. num_items = end - start;
  21487. if( num_items == 0 ){
  21488. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  21489. break;
  21490. }
  21491. if( num_items > MAIL_MAX_ITEM ){
  21492. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  21493. num_items = MAIL_MAX_ITEM;
  21494. }
  21495. for( i = 0; i < num_items && start < end; i++, start++ ){
  21496. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21497. msg.item[i].identify = 1;
  21498. if( !itemdb_exists(msg.item[i].nameid) ){
  21499. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  21500. return SCRIPT_CMD_FAILURE;
  21501. }
  21502. }
  21503. // Amounts
  21504. if( !script_hasdata(st,8) ){
  21505. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  21506. return SCRIPT_CMD_FAILURE;
  21507. }
  21508. data = script_getdata(st,8);
  21509. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  21510. return SCRIPT_CMD_FAILURE;
  21511. }
  21512. for( i = 0; i < num_items && start < end; i++, start++ ){
  21513. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21514. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21515. if( msg.item[i].amount <= 0 ){
  21516. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  21517. return SCRIPT_CMD_FAILURE;
  21518. }else if( itemdb_isstackable2(item) ){
  21519. uint16 max = item->stack.amount > 0 ? item->stack.amount : MAX_AMOUNT;
  21520. if( msg.item[i].amount > max ){
  21521. 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 );
  21522. msg.item[i].amount = max;
  21523. }
  21524. }else{
  21525. if( msg.item[i].amount > 1 ){
  21526. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  21527. msg.item[i].amount = 1;
  21528. }
  21529. }
  21530. }
  21531. // Refine
  21532. if (!script_hasdata(st, 9)) {
  21533. break;
  21534. }
  21535. data = script_getdata(st, 9);
  21536. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  21537. return SCRIPT_CMD_FAILURE;
  21538. }
  21539. for (i = 0; i < num_items && start < end; i++, start++) {
  21540. struct item_data* item = itemdb_exists(msg.item[i].nameid);
  21541. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21542. if (!item->flag.no_refine && (item->type == IT_WEAPON || item->type == IT_ARMOR || item->type == IT_SHADOWGEAR)) {
  21543. if (msg.item[i].refine > MAX_REFINE)
  21544. msg.item[i].refine = MAX_REFINE;
  21545. }
  21546. else
  21547. msg.item[i].refine = 0;
  21548. if (msg.item[i].refine < 0)
  21549. msg.item[i].refine = 0;
  21550. }
  21551. // Bound
  21552. if (!script_hasdata(st, 10)) {
  21553. break;
  21554. }
  21555. data = script_getdata(st,10);
  21556. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  21557. return SCRIPT_CMD_FAILURE;
  21558. }
  21559. for( i = 0; i < num_items && start < end; i++, start++ ){
  21560. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21561. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21562. if( msg.item[i].bound < BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  21563. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  21564. return SCRIPT_CMD_FAILURE;
  21565. }
  21566. }
  21567. // Cards
  21568. if( !script_hasdata(st,11) ){
  21569. break;
  21570. }
  21571. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  21572. data = script_getdata(st,j);
  21573. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21574. return SCRIPT_CMD_FAILURE;
  21575. }
  21576. for( k = 0; k < num_items && start < end; k++, start++ ){
  21577. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21578. if( msg.item[k].card[i] != 0 && !itemdb_exists(msg.item[k].card[i]) ){
  21579. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  21580. return SCRIPT_CMD_FAILURE;
  21581. }
  21582. }
  21583. }
  21584. // Random Options
  21585. if( !script_hasdata(st,11 + MAX_SLOTS) ){
  21586. break;
  21587. }
  21588. 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++ ){
  21589. // Option IDs
  21590. data = script_getdata(st, j);
  21591. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21592. return SCRIPT_CMD_FAILURE;
  21593. }
  21594. for( k = 0; k < num_items && start < end; k++, start++ ){
  21595. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21596. }
  21597. j++;
  21598. // Option values
  21599. data = script_getdata(st, j);
  21600. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21601. return SCRIPT_CMD_FAILURE;
  21602. }
  21603. for( k = 0; k < num_items && start < end; k++, start++ ){
  21604. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21605. }
  21606. j++;
  21607. // Option parameters
  21608. data = script_getdata(st, j);
  21609. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21610. return SCRIPT_CMD_FAILURE;
  21611. }
  21612. for( k = 0; k < num_items && start < end; k++, start++ ){
  21613. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21614. }
  21615. }
  21616. // Break the pseudo scope
  21617. break;
  21618. }
  21619. msg.status = MAIL_NEW;
  21620. msg.type = MAIL_INBOX_NORMAL;
  21621. msg.timestamp = time(NULL);
  21622. intif_Mail_send(0, &msg);
  21623. return SCRIPT_CMD_SUCCESS;
  21624. }
  21625. BUILDIN_FUNC(open_roulette){
  21626. #if PACKETVER < 20141022
  21627. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  21628. return SCRIPT_CMD_FAILURE;
  21629. #else
  21630. struct map_session_data* sd;
  21631. if( !battle_config.feature_roulette ){
  21632. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  21633. return SCRIPT_CMD_FAILURE;
  21634. }
  21635. if( !script_charid2sd( 2, sd ) ){
  21636. return SCRIPT_CMD_FAILURE;
  21637. }
  21638. clif_roulette_open(sd);
  21639. return SCRIPT_CMD_SUCCESS;
  21640. #endif
  21641. }
  21642. /*==========================================
  21643. * identifyall({<type>{,<account_id>}})
  21644. * <type>:
  21645. * true: identify the items and returns the count of unidentified items (default)
  21646. * false: returns the count of unidentified items only
  21647. *------------------------------------------*/
  21648. BUILDIN_FUNC(identifyall) {
  21649. TBL_PC *sd;
  21650. bool identify_item = true;
  21651. if (script_hasdata(st, 2))
  21652. identify_item = script_getnum(st, 2) != 0;
  21653. if( !script_hasdata(st, 3) )
  21654. script_rid2sd(sd);
  21655. else
  21656. sd = map_id2sd( script_getnum(st, 3) );
  21657. if (sd == NULL) {
  21658. script_pushint(st, -1);
  21659. return SCRIPT_CMD_SUCCESS;
  21660. }
  21661. script_pushint(st, pc_identifyall(sd, identify_item));
  21662. return SCRIPT_CMD_SUCCESS;
  21663. }
  21664. BUILDIN_FUNC(is_guild_leader)
  21665. {
  21666. struct map_session_data* sd;
  21667. struct guild* guild_data;
  21668. int guild_id;
  21669. if (!script_rid2sd(sd)) {
  21670. script_pushint(st, false);
  21671. return SCRIPT_CMD_FAILURE;
  21672. }
  21673. if (script_hasdata(st, 2))
  21674. guild_id = script_getnum(st, 2);
  21675. else
  21676. guild_id = sd->status.guild_id;
  21677. guild_data = guild_search(guild_id);
  21678. if (guild_data)
  21679. script_pushint(st, (guild_data->member[0].char_id == sd->status.char_id));
  21680. else
  21681. script_pushint(st, false);
  21682. return SCRIPT_CMD_SUCCESS;
  21683. }
  21684. BUILDIN_FUNC(is_party_leader)
  21685. {
  21686. struct map_session_data* sd;
  21687. struct party_data* p_data;
  21688. int p_id, i = 0;
  21689. if (!script_rid2sd(sd)) {
  21690. script_pushint(st, false);
  21691. return SCRIPT_CMD_FAILURE;
  21692. }
  21693. if (script_hasdata(st, 2))
  21694. p_id = script_getnum(st, 2);
  21695. else
  21696. p_id = sd->status.party_id;
  21697. p_data = party_search(p_id);
  21698. if (p_data) {
  21699. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  21700. if (i < MAX_PARTY){
  21701. script_pushint(st, p_data->party.member[i].leader);
  21702. return SCRIPT_CMD_SUCCESS;
  21703. }
  21704. }
  21705. script_pushint(st, false);
  21706. return SCRIPT_CMD_SUCCESS;
  21707. }
  21708. BUILDIN_FUNC( camerainfo ){
  21709. #if PACKETVER < 20160525
  21710. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  21711. return SCRIPT_CMD_FAILURE;
  21712. #else
  21713. struct map_session_data* sd;
  21714. if( !script_charid2sd( 5, sd ) ){
  21715. return SCRIPT_CMD_FAILURE;
  21716. }
  21717. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  21718. return SCRIPT_CMD_SUCCESS;
  21719. #endif
  21720. }
  21721. // This function is only meant to be used inside of achievement conditions
  21722. BUILDIN_FUNC(achievement_condition){
  21723. // Push what we get from the script
  21724. script_pushint( st, 2 );
  21725. // Otherwise the script is freed afterwards
  21726. st->state = RERUNLINE;
  21727. return SCRIPT_CMD_SUCCESS;
  21728. }
  21729. /// Returns a reference to a variable of the specific instance ID.
  21730. /// Returns 0 if an error occurs.
  21731. ///
  21732. /// getinstancevar(<variable>, <instance ID>) -> <reference>
  21733. BUILDIN_FUNC(getinstancevar)
  21734. {
  21735. struct script_data* data = script_getdata(st, 2);
  21736. if (!data_isreference(data)) {
  21737. ShowError("buildin_getinstancevar: %s is not a variable.\n", script_getstr(st, 2));
  21738. script_reportdata(data);
  21739. script_pushnil(st);
  21740. st->state = END;
  21741. return SCRIPT_CMD_FAILURE;
  21742. }
  21743. const char* name = reference_getname(data);
  21744. if (*name != '\'') {
  21745. ShowError("buildin_getinstancevar: Invalid scope. %s is not an instance variable.\n", name);
  21746. script_reportdata(data);
  21747. script_pushnil(st);
  21748. st->state = END;
  21749. return SCRIPT_CMD_FAILURE;
  21750. }
  21751. int instance_id = script_getnum(st, 3);
  21752. if (instance_id <= 0) {
  21753. ShowError("buildin_getinstancevar: Invalid instance ID %d.\n", instance_id);
  21754. script_pushnil(st);
  21755. st->state = END;
  21756. return SCRIPT_CMD_FAILURE;
  21757. }
  21758. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  21759. if (im->state != INSTANCE_BUSY) {
  21760. ShowError("buildin_getinstancevar: Unknown instance ID %d.\n", instance_id);
  21761. script_pushnil(st);
  21762. st->state = END;
  21763. return SCRIPT_CMD_FAILURE;
  21764. }
  21765. if (!im->regs.vars)
  21766. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  21767. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  21768. return SCRIPT_CMD_SUCCESS;
  21769. }
  21770. /// Sets the value of an instance variable.
  21771. ///
  21772. /// setinstancevar(<variable>,<value>,<instance ID>)
  21773. BUILDIN_FUNC(setinstancevar)
  21774. {
  21775. const char *command = script_getfuncname(st);
  21776. struct script_data* data = script_getdata(st, 2);
  21777. if (!data_isreference(data)) {
  21778. ShowError("buildin_%s: %s is not a variable.\n", command, script_getstr(st, 2));
  21779. script_reportdata(data);
  21780. script_pushnil(st);
  21781. st->state = END;
  21782. return SCRIPT_CMD_FAILURE;
  21783. }
  21784. const char* name = reference_getname(data);
  21785. if (*name != '\'') {
  21786. ShowError("buildin_%s: Invalid scope. %s is not an instance variable.\n", command, name);
  21787. script_reportdata(data);
  21788. script_pushnil(st);
  21789. st->state = END;
  21790. return SCRIPT_CMD_FAILURE;
  21791. }
  21792. int instance_id = script_getnum(st, 4);
  21793. if (instance_id <= 0) {
  21794. ShowError("buildin_%s: Invalid instance ID %d.\n", command, instance_id);
  21795. script_pushnil(st);
  21796. st->state = END;
  21797. return SCRIPT_CMD_FAILURE;
  21798. }
  21799. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  21800. if (im->state != INSTANCE_BUSY) {
  21801. ShowError("buildin_%s: Unknown instance ID %d.\n", command, instance_id);
  21802. script_pushnil(st);
  21803. st->state = END;
  21804. return SCRIPT_CMD_FAILURE;
  21805. }
  21806. script_pushcopy(st, 2);
  21807. struct map_session_data* sd = nullptr;
  21808. if( is_string_variable(name) )
  21809. set_reg_str( st, sd, reference_getuid(data), name, script_getstr( st, 3 ), &im->regs );
  21810. else
  21811. set_reg_num( st, sd, reference_getuid(data), name, script_getnum64( st, 3 ), &im->regs );
  21812. return SCRIPT_CMD_SUCCESS;
  21813. }
  21814. /*
  21815. convertpcinfo(<char_id>,<type>)
  21816. convertpcinfo(<account_id>,<type>)
  21817. convertpcinfo(<player_name>,<type>)
  21818. */
  21819. BUILDIN_FUNC(convertpcinfo) {
  21820. TBL_PC *sd;
  21821. if (script_isstring(st, 2))
  21822. sd = map_nick2sd(script_getstr(st, 2),false);
  21823. else {
  21824. int id = script_getnum(st, 2);
  21825. sd = map_id2sd(id);
  21826. if (!sd)
  21827. sd = map_charid2sd(id);
  21828. }
  21829. int type = script_getnum(st, 3);
  21830. switch (type) {
  21831. case CPC_NAME:
  21832. case CPC_CHAR:
  21833. case CPC_ACCOUNT:
  21834. break;
  21835. default:
  21836. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  21837. script_pushnil(st);
  21838. st->state = END;
  21839. return SCRIPT_CMD_FAILURE;
  21840. }
  21841. if (!sd) {
  21842. if (type == CPC_NAME)
  21843. script_pushstrcopy(st, "");
  21844. else
  21845. script_pushint(st, 0);
  21846. return SCRIPT_CMD_SUCCESS;
  21847. }
  21848. switch (type) {
  21849. case CPC_NAME:
  21850. script_pushstrcopy(st, sd->status.name);
  21851. break;
  21852. case CPC_CHAR:
  21853. script_pushint(st, sd->status.char_id);
  21854. break;
  21855. case CPC_ACCOUNT:
  21856. script_pushint(st, sd->status.account_id);
  21857. break;
  21858. }
  21859. return SCRIPT_CMD_SUCCESS;
  21860. }
  21861. BUILDIN_FUNC(isnpccloaked)
  21862. {
  21863. npc_data *nd;
  21864. if (script_hasdata(st, 2))
  21865. nd = npc_name2id(script_getstr(st, 2));
  21866. else
  21867. nd = map_id2nd(st->oid);
  21868. if (!nd) {
  21869. if (script_hasdata(st, 2))
  21870. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  21871. else
  21872. ShowError("buildin_isnpccloaked: non-existing NPC.\n");
  21873. return SCRIPT_CMD_FAILURE;
  21874. }
  21875. map_session_data* sd;
  21876. if (!script_charid2sd(3, sd))
  21877. return SCRIPT_CMD_FAILURE;
  21878. script_pushint(st, npc_is_cloaked(nd, sd));
  21879. return SCRIPT_CMD_SUCCESS;
  21880. }
  21881. BUILDIN_FUNC(refineui){
  21882. #if PACKETVER < 20161012
  21883. ShowError( "buildin_refineui: This command requires packet version 2016-10-12 or newer.\n" );
  21884. return SCRIPT_CMD_FAILURE;
  21885. #else
  21886. struct map_session_data* sd;
  21887. if( !script_charid2sd(2,sd) ){
  21888. return SCRIPT_CMD_FAILURE;
  21889. }
  21890. if( !battle_config.feature_refineui ){
  21891. ShowError( "buildin_refineui: This command is disabled via configuration.\n" );
  21892. return SCRIPT_CMD_FAILURE;
  21893. }
  21894. if( !sd->state.refineui_open ){
  21895. clif_refineui_open(sd);
  21896. }
  21897. return SCRIPT_CMD_SUCCESS;
  21898. #endif
  21899. }
  21900. BUILDIN_FUNC(getenchantgrade){
  21901. struct map_session_data *sd;
  21902. if (!script_charid2sd(3, sd)) {
  21903. script_pushint(st,-1);
  21904. return SCRIPT_CMD_FAILURE;
  21905. }
  21906. int index, position;
  21907. if (script_hasdata(st, 2))
  21908. position = script_getnum(st, 2);
  21909. else
  21910. position = EQI_COMPOUND_ON;
  21911. if (position == EQI_COMPOUND_ON)
  21912. index = current_equip_item_index;
  21913. else if (equip_index_check(position))
  21914. index = pc_checkequip(sd, equip_bitmask[position]);
  21915. else {
  21916. ShowError( "buildin_getenchantgrade: Unknown equip index '%d'\n", position );
  21917. script_pushint(st,-1);
  21918. return SCRIPT_CMD_FAILURE;
  21919. }
  21920. if (index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0)
  21921. script_pushint(st, -1);
  21922. else
  21923. script_pushint(st, sd->inventory.u.items_inventory[index].enchantgrade);
  21924. return SCRIPT_CMD_SUCCESS;
  21925. }
  21926. /*==========================================
  21927. * mob_setidleevent( <monster game ID>, "<event label>" )
  21928. *------------------------------------------*/
  21929. BUILDIN_FUNC(mob_setidleevent){
  21930. struct block_list* bl;
  21931. if( !script_rid2bl( 2, bl ) ){
  21932. return SCRIPT_CMD_FAILURE;
  21933. }
  21934. if( bl->type != BL_MOB ){
  21935. ShowError( "buildin_mob_setidleevent: the target GID was not a monster.\n" );
  21936. return SCRIPT_CMD_FAILURE;
  21937. }
  21938. struct mob_data* md = (struct mob_data*)bl;
  21939. if (md == nullptr)
  21940. return SCRIPT_CMD_FAILURE;
  21941. const char* idle_event = script_getstr( st, 3 );
  21942. check_event( st, idle_event );
  21943. safestrncpy( md->idle_event, idle_event, EVENT_NAME_LENGTH );
  21944. return SCRIPT_CMD_SUCCESS;
  21945. }
  21946. #include "../custom/script.inc"
  21947. // declarations that were supposed to be exported from npc_chat.cpp
  21948. #ifdef PCRE_SUPPORT
  21949. BUILDIN_FUNC(defpattern);
  21950. BUILDIN_FUNC(activatepset);
  21951. BUILDIN_FUNC(deactivatepset);
  21952. BUILDIN_FUNC(deletepset);
  21953. #endif
  21954. /** Regular expression matching
  21955. * preg_match(<pattern>,<string>{,<offset>})
  21956. */
  21957. BUILDIN_FUNC(preg_match) {
  21958. #ifdef PCRE_SUPPORT
  21959. pcre *re;
  21960. pcre_extra *pcreExtra;
  21961. const char *error;
  21962. int erroffset, r, offset = 0;
  21963. int subStrVec[30];
  21964. const char* pattern = script_getstr(st,2);
  21965. const char* subject = script_getstr(st,3);
  21966. if (script_hasdata(st,4))
  21967. offset = script_getnum(st,4);
  21968. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  21969. pcreExtra = pcre_study(re, 0, &error);
  21970. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  21971. pcre_free(re);
  21972. if (pcreExtra != NULL)
  21973. pcre_free(pcreExtra);
  21974. if (r < 0)
  21975. script_pushint(st,0);
  21976. else
  21977. script_pushint(st,(r > 0) ? r : 30 / 3);
  21978. return SCRIPT_CMD_SUCCESS;
  21979. #else
  21980. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  21981. script_pushint(st,0);
  21982. return SCRIPT_CMD_SUCCESS;
  21983. #endif
  21984. }
  21985. /// script command definitions
  21986. /// for an explanation on args, see add_buildin_func
  21987. struct script_function buildin_func[] = {
  21988. // NPC interaction
  21989. BUILDIN_DEF(mes,"s*"),
  21990. BUILDIN_DEF(next,""),
  21991. BUILDIN_DEF(clear,""),
  21992. BUILDIN_DEF(close,""),
  21993. BUILDIN_DEF(close2,""),
  21994. BUILDIN_DEF(menu,"sl*"),
  21995. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  21996. BUILDIN_DEF(prompt,"s*"),
  21997. //
  21998. BUILDIN_DEF(goto,"l"),
  21999. BUILDIN_DEF(callsub,"l*"),
  22000. BUILDIN_DEF(callfunc,"s*"),
  22001. BUILDIN_DEF(return,"?"),
  22002. BUILDIN_DEF(getarg,"i?"),
  22003. BUILDIN_DEF(jobchange,"i??"),
  22004. BUILDIN_DEF(jobname,"i"),
  22005. BUILDIN_DEF(input,"r??"),
  22006. BUILDIN_DEF(warp,"sii?"),
  22007. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  22008. BUILDIN_DEF(areawarp,"siiiisii??"),
  22009. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  22010. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  22011. BUILDIN_DEF(setlook,"ii?"),
  22012. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  22013. BUILDIN_DEF2(setr,"set","rv?"),
  22014. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  22015. BUILDIN_DEF(setarray,"rv*"),
  22016. BUILDIN_DEF(cleararray,"rvi"),
  22017. BUILDIN_DEF(copyarray,"rri"),
  22018. BUILDIN_DEF(getarraysize,"r"),
  22019. BUILDIN_DEF(deletearray,"r?"),
  22020. BUILDIN_DEF(getelementofarray,"ri"),
  22021. BUILDIN_DEF(inarray,"rv"),
  22022. BUILDIN_DEF(countinarray,"rr"),
  22023. BUILDIN_DEF(getitem,"vi?"),
  22024. BUILDIN_DEF(rentitem,"vi?"),
  22025. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  22026. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  22027. BUILDIN_DEF(getnameditem,"vv"),
  22028. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  22029. BUILDIN_DEF(makeitem,"visii?"),
  22030. BUILDIN_DEF(makeitem2,"visiiiiiiiii?"),
  22031. BUILDIN_DEF(delitem,"vi?"),
  22032. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  22033. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  22034. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  22035. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  22036. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  22037. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  22038. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  22039. BUILDIN_DEF(delitemidx,"i??"),
  22040. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  22041. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  22042. BUILDIN_DEF(cutin,"si"),
  22043. BUILDIN_DEF(viewpoint,"iiiii?"),
  22044. BUILDIN_DEF(heal,"ii?"),
  22045. BUILDIN_DEF(itemheal,"ii?"),
  22046. BUILDIN_DEF(percentheal,"ii?"),
  22047. BUILDIN_DEF(rand,"i?"),
  22048. BUILDIN_DEF(countitem,"v?"),
  22049. BUILDIN_DEF(storagecountitem,"v?"),
  22050. BUILDIN_DEF(guildstoragecountitem,"v?"),
  22051. BUILDIN_DEF(cartcountitem,"v?"),
  22052. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  22053. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  22054. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  22055. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  22056. BUILDIN_DEF(checkweight,"vi*"),
  22057. BUILDIN_DEF(checkweight2,"rr"),
  22058. BUILDIN_DEF(readparam,"i?"),
  22059. BUILDIN_DEF(getcharid,"i?"),
  22060. BUILDIN_DEF(getnpcid,"i?"),
  22061. BUILDIN_DEF(getpartyname,"i"),
  22062. BUILDIN_DEF(getpartymember,"i??"),
  22063. BUILDIN_DEF(getpartyleader,"i?"),
  22064. BUILDIN_DEF(getguildname,"i"),
  22065. BUILDIN_DEF(getguildmaster,"i"),
  22066. BUILDIN_DEF(getguildmasterid,"i"),
  22067. BUILDIN_DEF(strcharinfo,"i?"),
  22068. BUILDIN_DEF(strnpcinfo,"i"),
  22069. BUILDIN_DEF(getequipid,"??"),
  22070. BUILDIN_DEF(getequipuniqueid,"i?"),
  22071. BUILDIN_DEF(getequipname,"i?"),
  22072. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  22073. BUILDIN_DEF(repair,"i?"), // [Valaris]
  22074. BUILDIN_DEF(repairall,"?"),
  22075. BUILDIN_DEF(getequipisequiped,"i?"),
  22076. BUILDIN_DEF(getequipisenableref,"i?"),
  22077. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  22078. BUILDIN_DEF(getequipweaponlv,"??"),
  22079. BUILDIN_DEF(getequiparmorlv, "??"),
  22080. BUILDIN_DEF(getequippercentrefinery,"i?"),
  22081. BUILDIN_DEF(successrefitem,"i??"),
  22082. BUILDIN_DEF(failedrefitem,"i?"),
  22083. BUILDIN_DEF(downrefitem,"i??"),
  22084. BUILDIN_DEF(statusup,"i?"),
  22085. BUILDIN_DEF(statusup2,"ii?"),
  22086. BUILDIN_DEF(traitstatusup,"i?"),
  22087. BUILDIN_DEF(traitstatusup2,"ii?"),
  22088. BUILDIN_DEF(bonus,"i?"),
  22089. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  22090. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  22091. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  22092. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  22093. BUILDIN_DEF(autobonus,"sii??"),
  22094. BUILDIN_DEF(autobonus2,"sii??"),
  22095. BUILDIN_DEF(autobonus3,"siiv?"),
  22096. BUILDIN_DEF(skill,"vi?"),
  22097. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  22098. BUILDIN_DEF(guildskill,"vi"),
  22099. BUILDIN_DEF(getskilllv,"v"),
  22100. BUILDIN_DEF(getgdskilllv,"iv"),
  22101. BUILDIN_DEF(basicskillcheck,""),
  22102. BUILDIN_DEF(getgmlevel,"?"),
  22103. BUILDIN_DEF(getgroupid,"?"),
  22104. BUILDIN_DEF(end,""),
  22105. BUILDIN_DEF(checkoption,"i?"),
  22106. BUILDIN_DEF(setoption,"i??"),
  22107. BUILDIN_DEF(setcart,"??"),
  22108. BUILDIN_DEF(checkcart,"?"),
  22109. BUILDIN_DEF(setfalcon,"??"),
  22110. BUILDIN_DEF(checkfalcon,"?"),
  22111. BUILDIN_DEF(setriding,"??"),
  22112. BUILDIN_DEF(checkriding,"?"),
  22113. BUILDIN_DEF(checkwug,"?"),
  22114. BUILDIN_DEF(checkmadogear,"?"),
  22115. BUILDIN_DEF(setmadogear,"???"),
  22116. BUILDIN_DEF2(savepoint,"save","sii???"),
  22117. BUILDIN_DEF(savepoint,"sii???"),
  22118. BUILDIN_DEF(gettimetick,"i"),
  22119. BUILDIN_DEF(gettime,"i"),
  22120. BUILDIN_DEF(gettimestr,"si?"),
  22121. BUILDIN_DEF(openstorage,""),
  22122. BUILDIN_DEF(guildopenstorage,""),
  22123. BUILDIN_DEF(guildopenstorage_log,"?"),
  22124. BUILDIN_DEF(guild_has_permission,"i?"),
  22125. BUILDIN_DEF(itemskill,"vi?"),
  22126. BUILDIN_DEF(produce,"i"),
  22127. BUILDIN_DEF(cooking,"i"),
  22128. BUILDIN_DEF(monster,"siisii???"),
  22129. BUILDIN_DEF(getmobdrops,"i"),
  22130. BUILDIN_DEF(areamonster,"siiiisii???"),
  22131. BUILDIN_DEF(killmonster,"ss?"),
  22132. BUILDIN_DEF(killmonsterall,"s?"),
  22133. BUILDIN_DEF(clone,"siisi????"),
  22134. BUILDIN_DEF(doevent,"s"),
  22135. BUILDIN_DEF(donpcevent,"s"),
  22136. BUILDIN_DEF(cmdothernpc,"ss"),
  22137. BUILDIN_DEF(addtimer,"is"),
  22138. BUILDIN_DEF(deltimer,"s"),
  22139. BUILDIN_DEF(addtimercount,"is"),
  22140. BUILDIN_DEF(initnpctimer,"??"),
  22141. BUILDIN_DEF(stopnpctimer,"??"),
  22142. BUILDIN_DEF(startnpctimer,"??"),
  22143. BUILDIN_DEF(setnpctimer,"i?"),
  22144. BUILDIN_DEF(getnpctimer,"i?"),
  22145. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  22146. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  22147. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  22148. BUILDIN_DEF(announce,"si??????"),
  22149. BUILDIN_DEF(mapannounce,"ssi?????"),
  22150. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  22151. BUILDIN_DEF(getusers,"i"),
  22152. BUILDIN_DEF(getmapguildusers,"si"),
  22153. BUILDIN_DEF(getmapusers,"s"),
  22154. BUILDIN_DEF(getareausers,"siiii"),
  22155. BUILDIN_DEF(getunits, "i?"),
  22156. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  22157. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  22158. BUILDIN_DEF(getareadropitem,"siiiiv"),
  22159. BUILDIN_DEF(enablenpc,"?"),
  22160. BUILDIN_DEF2(enablenpc, "disablenpc", "?"),
  22161. BUILDIN_DEF2(enablenpc, "hideoffnpc", "?"),
  22162. BUILDIN_DEF2(enablenpc, "hideonnpc", "?"),
  22163. BUILDIN_DEF2(enablenpc, "cloakoffnpc", "??"),
  22164. BUILDIN_DEF2(enablenpc, "cloakonnpc", "??"),
  22165. BUILDIN_DEF(sc_start,"iii???"),
  22166. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  22167. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  22168. BUILDIN_DEF(sc_end,"i?"),
  22169. BUILDIN_DEF(sc_end_class,"??"),
  22170. BUILDIN_DEF(getstatus, "i??"),
  22171. BUILDIN_DEF(getscrate,"ii?"),
  22172. BUILDIN_DEF(debugmes,"s"),
  22173. BUILDIN_DEF(errormes,"s"),
  22174. BUILDIN_DEF2(catchpet,"pet","i"),
  22175. BUILDIN_DEF2(birthpet,"bpet",""),
  22176. BUILDIN_DEF(catchpet,"i"),
  22177. BUILDIN_DEF(birthpet,""),
  22178. BUILDIN_DEF(resetlvl,"i?"),
  22179. BUILDIN_DEF(resetstatus,"?"),
  22180. BUILDIN_DEF(resetskill,"?"),
  22181. BUILDIN_DEF(resetfeel,"?"),
  22182. BUILDIN_DEF(resethate,"?"),
  22183. BUILDIN_DEF(skillpointcount,"?"),
  22184. BUILDIN_DEF(changebase,"i?"),
  22185. BUILDIN_DEF(changesex,"?"),
  22186. BUILDIN_DEF(changecharsex,"?"),
  22187. BUILDIN_DEF(waitingroom,"si?????"),
  22188. BUILDIN_DEF(delwaitingroom,"?"),
  22189. BUILDIN_DEF(waitingroomkick,"ss"),
  22190. BUILDIN_DEF(getwaitingroomusers, "?"),
  22191. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  22192. BUILDIN_DEF(enablewaitingroomevent,"?"),
  22193. BUILDIN_DEF(disablewaitingroomevent,"?"),
  22194. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  22195. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  22196. BUILDIN_DEF(getwaitingroomstate,"i?"),
  22197. BUILDIN_DEF(warpwaitingpc,"sii?"),
  22198. BUILDIN_DEF(attachrid,"i?"),
  22199. BUILDIN_DEF(addrid,"i?????"),
  22200. BUILDIN_DEF(detachrid,""),
  22201. BUILDIN_DEF(isloggedin,"i?"),
  22202. BUILDIN_DEF(setmapflagnosave,"ssii"),
  22203. BUILDIN_DEF(getmapflag,"si?"),
  22204. BUILDIN_DEF(setmapflag,"si??"),
  22205. BUILDIN_DEF(removemapflag,"si?"),
  22206. BUILDIN_DEF(pvpon,"s"),
  22207. BUILDIN_DEF(pvpoff,"s"),
  22208. BUILDIN_DEF(gvgon,"s"),
  22209. BUILDIN_DEF(gvgoff,"s"),
  22210. BUILDIN_DEF(emotion,"i?"),
  22211. BUILDIN_DEF(maprespawnguildid,"sii"),
  22212. BUILDIN_DEF(agitstart,""), // <Agit>
  22213. BUILDIN_DEF(agitend,""),
  22214. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  22215. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  22216. BUILDIN_DEF(getcastlename,"s"),
  22217. BUILDIN_DEF(getcastledata,"si"),
  22218. BUILDIN_DEF(setcastledata,"sii"),
  22219. BUILDIN_DEF(requestguildinfo,"i?"),
  22220. BUILDIN_DEF(getequipcardcnt,"i"),
  22221. BUILDIN_DEF(successremovecards,"i"),
  22222. BUILDIN_DEF(failedremovecards,"ii"),
  22223. BUILDIN_DEF(marriage,"s"),
  22224. BUILDIN_DEF2(wedding_effect,"wedding",""),
  22225. BUILDIN_DEF(divorce,"?"),
  22226. BUILDIN_DEF(ispartneron,"?"),
  22227. BUILDIN_DEF(getpartnerid,"?"),
  22228. BUILDIN_DEF(getchildid,"?"),
  22229. BUILDIN_DEF(getmotherid,"?"),
  22230. BUILDIN_DEF(getfatherid,"?"),
  22231. BUILDIN_DEF(warppartner,"sii"),
  22232. BUILDIN_DEF(getitemname,"v"),
  22233. BUILDIN_DEF(getitemslots,"i"),
  22234. BUILDIN_DEF(makepet,"i"),
  22235. BUILDIN_DEF(getexp,"ii?"),
  22236. BUILDIN_DEF(getinventorylist,"?"),
  22237. BUILDIN_DEF(getskilllist,"?"),
  22238. BUILDIN_DEF(clearitem,"?"),
  22239. BUILDIN_DEF(classchange,"i??"),
  22240. BUILDIN_DEF(misceffect,"i"),
  22241. BUILDIN_DEF(playBGM,"s"),
  22242. BUILDIN_DEF(playBGMall,"s?????"),
  22243. BUILDIN_DEF(soundeffect,"si"),
  22244. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  22245. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  22246. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  22247. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  22248. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  22249. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  22250. BUILDIN_DEF(petloot,"i"), // [Valaris]
  22251. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  22252. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  22253. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  22254. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  22255. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  22256. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  22257. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  22258. BUILDIN_DEF(removespecialeffect,"i??"),
  22259. BUILDIN_DEF2(removespecialeffect,"removespecialeffect2","i??"),
  22260. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  22261. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  22262. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  22263. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  22264. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  22265. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  22266. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  22267. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  22268. BUILDIN_DEF(mobcount,"ss"),
  22269. BUILDIN_DEF(getlook,"i?"),
  22270. BUILDIN_DEF(getsavepoint,"i?"),
  22271. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  22272. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  22273. BUILDIN_DEF(npcstop,""), // [Valaris]
  22274. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  22275. BUILDIN_DEF(mapid2name,"i"),
  22276. BUILDIN_DEF(checkoption1,"i?"),
  22277. BUILDIN_DEF(checkoption2,"i?"),
  22278. BUILDIN_DEF(guildgetexp,"i"),
  22279. BUILDIN_DEF(guildchangegm,"is"),
  22280. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  22281. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  22282. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  22283. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  22284. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  22285. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  22286. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  22287. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  22288. BUILDIN_DEF(night,""), // sets the server to night time
  22289. BUILDIN_DEF(day,""), // sets the server to day time
  22290. #ifdef PCRE_SUPPORT
  22291. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  22292. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  22293. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  22294. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  22295. #endif
  22296. BUILDIN_DEF(preg_match,"ss?"),
  22297. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  22298. BUILDIN_DEF(recovery,"i???"),
  22299. BUILDIN_DEF(getpetinfo,"i?"),
  22300. BUILDIN_DEF(gethominfo,"i?"),
  22301. BUILDIN_DEF(addhomintimacy,"i?"),
  22302. BUILDIN_DEF(getmercinfo,"i?"),
  22303. BUILDIN_DEF(checkequipedcard,"i"),
  22304. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  22305. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  22306. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  22307. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  22308. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  22309. BUILDIN_DEF(charat,"si"),
  22310. BUILDIN_DEF(setchar,"ssi"),
  22311. BUILDIN_DEF(insertchar,"ssi"),
  22312. BUILDIN_DEF(delchar,"si"),
  22313. BUILDIN_DEF(strtoupper,"s"),
  22314. BUILDIN_DEF(strtolower,"s"),
  22315. BUILDIN_DEF(charisupper, "si"),
  22316. BUILDIN_DEF(charislower, "si"),
  22317. BUILDIN_DEF(substr,"sii"),
  22318. BUILDIN_DEF(explode, "rss"),
  22319. BUILDIN_DEF(implode, "r?"),
  22320. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  22321. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  22322. BUILDIN_DEF(strpos,"ss?"),
  22323. BUILDIN_DEF(replacestr,"sss??"),
  22324. BUILDIN_DEF(countstr,"ss?"),
  22325. BUILDIN_DEF(setnpcdisplay,"sv??"),
  22326. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  22327. BUILDIN_DEF(strcmp,"ss"),
  22328. BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
  22329. BUILDIN_DEF(setiteminfo,"vii"), //[Lupus] set Items Buy / sell Price, etc info
  22330. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  22331. // [zBuffer] List of mathematics commands --->
  22332. BUILDIN_DEF(sqrt,"i"),
  22333. BUILDIN_DEF(pow,"ii"),
  22334. BUILDIN_DEF(distance,"iiii"),
  22335. BUILDIN_DEF2(minmax,"min", "*"),
  22336. BUILDIN_DEF2(minmax,"minimum", "*"),
  22337. BUILDIN_DEF2(minmax,"max", "*"),
  22338. BUILDIN_DEF2(minmax,"maximum", "*"),
  22339. // <--- [zBuffer] List of mathematics commands
  22340. BUILDIN_DEF(md5,"s"),
  22341. // [zBuffer] List of dynamic var commands --->
  22342. BUILDIN_DEF(getd,"s"),
  22343. BUILDIN_DEF(setd,"sv?"),
  22344. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  22345. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  22346. BUILDIN_DEF(npcshopadditem,"sii*"),
  22347. BUILDIN_DEF(npcshopdelitem,"si*"),
  22348. BUILDIN_DEF(npcshopattach,"s?"),
  22349. BUILDIN_DEF(equip,"i?"),
  22350. BUILDIN_DEF(autoequip,"ii"),
  22351. BUILDIN_DEF(setbattleflag,"si?"),
  22352. BUILDIN_DEF(getbattleflag,"s"),
  22353. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  22354. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  22355. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  22356. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  22357. BUILDIN_DEF(addmonsterdrop,"vii??"), //Akinari [Lupus]
  22358. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  22359. BUILDIN_DEF(axtoi,"s"),
  22360. BUILDIN_DEF(query_sql,"s*"),
  22361. BUILDIN_DEF(query_logsql,"s*"),
  22362. BUILDIN_DEF(escape_sql,"v"),
  22363. BUILDIN_DEF(atoi,"s"),
  22364. BUILDIN_DEF(strtol,"si"),
  22365. // [zBuffer] List of player cont commands --->
  22366. BUILDIN_DEF(rid2name,"i"),
  22367. BUILDIN_DEF(pcfollow,"ii"),
  22368. BUILDIN_DEF(pcstopfollow,"i"),
  22369. BUILDIN_DEF(pcblockmove,"ii"),
  22370. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  22371. BUILDIN_DEF(pcblockskill,"ii"),
  22372. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  22373. BUILDIN_DEF(setpcblock, "ii?"),
  22374. BUILDIN_DEF(getpcblock, "?"),
  22375. // <--- [zBuffer] List of player cont commands
  22376. // [zBuffer] List of unit control commands --->
  22377. BUILDIN_DEF(unitexists,"i"),
  22378. BUILDIN_DEF(getunittype,"i"),
  22379. BUILDIN_DEF(getunitname,"i"),
  22380. BUILDIN_DEF(setunitname,"is"),
  22381. BUILDIN_DEF(setunittitle,"is"),
  22382. BUILDIN_DEF(getunittitle,"i"),
  22383. BUILDIN_DEF(getunitdata,"i*"),
  22384. BUILDIN_DEF(setunitdata,"iii"),
  22385. BUILDIN_DEF(unitwalk,"iii?"),
  22386. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  22387. BUILDIN_DEF(unitkill,"i"),
  22388. BUILDIN_DEF(unitwarp,"isii"),
  22389. BUILDIN_DEF(unitattack,"iv?"),
  22390. BUILDIN_DEF(unitstopattack,"i"),
  22391. BUILDIN_DEF(unitstopwalk,"i?"),
  22392. BUILDIN_DEF(unittalk,"is?"),
  22393. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  22394. BUILDIN_DEF(unitskilluseid,"ivi????"), // originally by Qamera [Celest]
  22395. BUILDIN_DEF(unitskillusepos,"iviii???"), // [Celest]
  22396. // <--- [zBuffer] List of unit control commands
  22397. BUILDIN_DEF(sleep,"i"),
  22398. BUILDIN_DEF(sleep2,"i"),
  22399. BUILDIN_DEF(awake,"s"),
  22400. BUILDIN_DEF(getvariableofnpc,"rs"),
  22401. BUILDIN_DEF(warpportal,"iisii"),
  22402. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  22403. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  22404. BUILDIN_DEF(morphembryo,""),
  22405. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  22406. BUILDIN_DEF(checkhomcall,""),
  22407. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  22408. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  22409. BUILDIN_DEF(checkvending,"?"),
  22410. BUILDIN_DEF(checkchatting,"?"),
  22411. BUILDIN_DEF(checkidle,"?"),
  22412. BUILDIN_DEF(checkidlehom,"?"),
  22413. BUILDIN_DEF(checkidlemer,"?"),
  22414. BUILDIN_DEF(openmail,"?"),
  22415. BUILDIN_DEF(openauction,"?"),
  22416. BUILDIN_DEF(checkcell,"siii"),
  22417. BUILDIN_DEF(setcell,"siiiiii"),
  22418. BUILDIN_DEF(getfreecell,"srr?????"),
  22419. BUILDIN_DEF(setwall,"siiiiis"),
  22420. BUILDIN_DEF(delwall,"s"),
  22421. BUILDIN_DEF(checkwall,"s"),
  22422. BUILDIN_DEF(searchitem,"rs"),
  22423. BUILDIN_DEF(mercenary_create,"ii"),
  22424. BUILDIN_DEF(mercenary_delete,"??"),
  22425. BUILDIN_DEF(mercenary_heal,"ii"),
  22426. BUILDIN_DEF(mercenary_sc_start,"iii"),
  22427. BUILDIN_DEF(mercenary_get_calls,"i"),
  22428. BUILDIN_DEF(mercenary_get_faith,"i"),
  22429. BUILDIN_DEF(mercenary_set_calls,"ii"),
  22430. BUILDIN_DEF(mercenary_set_faith,"ii"),
  22431. BUILDIN_DEF(readbook,"ii"),
  22432. BUILDIN_DEF(setfont,"i"),
  22433. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  22434. BUILDIN_DEF(progressbar,"si"),
  22435. BUILDIN_DEF(progressbar_npc, "si?"),
  22436. BUILDIN_DEF(pushpc,"ii"),
  22437. BUILDIN_DEF(buyingstore,"i"),
  22438. BUILDIN_DEF(searchstores,"ii"),
  22439. BUILDIN_DEF(showdigit,"i?"),
  22440. // WoE SE
  22441. BUILDIN_DEF(agitstart2,""),
  22442. BUILDIN_DEF(agitend2,""),
  22443. BUILDIN_DEF(agitcheck2,""),
  22444. // BattleGround
  22445. BUILDIN_DEF(waitingroom2bg,"sii???"),
  22446. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  22447. BUILDIN_DEF(bg_team_setxy,"iii"),
  22448. BUILDIN_DEF(bg_warp,"isii"),
  22449. BUILDIN_DEF(bg_monster,"isiisi?"),
  22450. BUILDIN_DEF(bg_monster_set_team,"ii"),
  22451. BUILDIN_DEF(bg_leave,"?"),
  22452. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  22453. BUILDIN_DEF(bg_destroy,"i"),
  22454. BUILDIN_DEF(areapercentheal,"siiiiii"),
  22455. BUILDIN_DEF(bg_get_data,"ii"),
  22456. BUILDIN_DEF(bg_getareausers,"isiiii"),
  22457. BUILDIN_DEF(bg_updatescore,"sii"),
  22458. BUILDIN_DEF(bg_join,"i????"),
  22459. BUILDIN_DEF(bg_create,"sii??"),
  22460. BUILDIN_DEF(bg_reserve,"s?"),
  22461. BUILDIN_DEF(bg_unbook,"s"),
  22462. BUILDIN_DEF(bg_info,"si"),
  22463. // Instancing
  22464. BUILDIN_DEF(instance_create,"s??"),
  22465. BUILDIN_DEF(instance_destroy,"?"),
  22466. BUILDIN_DEF(instance_id,"?"),
  22467. BUILDIN_DEF(instance_enter,"s????"),
  22468. BUILDIN_DEF(instance_npcname,"s?"),
  22469. BUILDIN_DEF(instance_mapname,"s?"),
  22470. BUILDIN_DEF(instance_warpall,"sii?"),
  22471. BUILDIN_DEF(instance_announce,"isi?????"),
  22472. BUILDIN_DEF(instance_check_party,"i???"),
  22473. BUILDIN_DEF(instance_check_guild,"i???"),
  22474. BUILDIN_DEF(instance_check_clan,"i???"),
  22475. BUILDIN_DEF(instance_info,"si?"),
  22476. BUILDIN_DEF(instance_live_info,"i?"),
  22477. /**
  22478. * 3rd-related
  22479. **/
  22480. BUILDIN_DEF(makerune,"i?"),
  22481. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  22482. BUILDIN_DEF(setdragon,"??"),//[Ind]
  22483. BUILDIN_DEF(ismounting,"?"),//[Ind]
  22484. BUILDIN_DEF(setmounting,"?"),//[Ind]
  22485. BUILDIN_DEF(checkre,"i"),
  22486. /**
  22487. * rAthena and beyond!
  22488. **/
  22489. BUILDIN_DEF(getargcount,""),
  22490. BUILDIN_DEF(getcharip,"?"),
  22491. BUILDIN_DEF(is_function,"s"),
  22492. BUILDIN_DEF(get_revision,""),
  22493. BUILDIN_DEF(get_githash,""),
  22494. BUILDIN_DEF(freeloop,"?"),
  22495. BUILDIN_DEF(getrandgroupitem,"i????"),
  22496. BUILDIN_DEF(cleanmap,"s"),
  22497. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  22498. BUILDIN_DEF(npcskill,"viii"),
  22499. BUILDIN_DEF(consumeitem,"v?"),
  22500. BUILDIN_DEF(delequip,"i?"),
  22501. BUILDIN_DEF(breakequip,"i?"),
  22502. BUILDIN_DEF(sit,"?"),
  22503. BUILDIN_DEF(stand,"?"),
  22504. //@commands (script based)
  22505. BUILDIN_DEF(bindatcmd, "ss??"),
  22506. BUILDIN_DEF(unbindatcmd, "s"),
  22507. BUILDIN_DEF(useatcmd, "s"),
  22508. //Quest Log System [Inkfish]
  22509. BUILDIN_DEF(questinfo, "i??"),
  22510. BUILDIN_DEF(setquest, "i?"),
  22511. BUILDIN_DEF(erasequest, "i?"),
  22512. BUILDIN_DEF(completequest, "i?"),
  22513. BUILDIN_DEF(checkquest, "i??"),
  22514. BUILDIN_DEF(isbegin_quest,"i?"),
  22515. BUILDIN_DEF(changequest, "ii?"),
  22516. BUILDIN_DEF(showevent, "i??"),
  22517. BUILDIN_DEF(questinfo_refresh, "?"),
  22518. //Bound items [Xantara] & [Akinari]
  22519. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  22520. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  22521. BUILDIN_DEF(countbound, "??"),
  22522. // Party related
  22523. BUILDIN_DEF(party_create,"s???"),
  22524. BUILDIN_DEF(party_addmember,"ii"),
  22525. BUILDIN_DEF(party_delmember,"??"),
  22526. BUILDIN_DEF(party_changeleader,"ii"),
  22527. BUILDIN_DEF(party_changeoption,"iii"),
  22528. BUILDIN_DEF(party_destroy,"i"),
  22529. // Clan system
  22530. BUILDIN_DEF(clan_join,"i?"),
  22531. BUILDIN_DEF(clan_leave,"?"),
  22532. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  22533. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  22534. BUILDIN_DEF(vip_status,"i?"),
  22535. BUILDIN_DEF(vip_time,"i?"),
  22536. BUILDIN_DEF(bonus_script,"si????"),
  22537. BUILDIN_DEF(bonus_script_clear,"??"),
  22538. BUILDIN_DEF(getgroupitem,"i??"),
  22539. BUILDIN_DEF(enable_command,""),
  22540. BUILDIN_DEF(disable_command,""),
  22541. BUILDIN_DEF(getguildmember,"i??"),
  22542. BUILDIN_DEF(addspiritball,"ii?"),
  22543. BUILDIN_DEF(delspiritball,"i?"),
  22544. BUILDIN_DEF(countspiritball,"?"),
  22545. BUILDIN_DEF(mergeitem,"?"),
  22546. BUILDIN_DEF(mergeitem2,"??"),
  22547. BUILDIN_DEF(npcshopupdate,"sii?"),
  22548. BUILDIN_DEF(getattachedrid,""),
  22549. BUILDIN_DEF(getvar,"vi"),
  22550. BUILDIN_DEF(showscript,"s??"),
  22551. BUILDIN_DEF(ignoretimeout,"i?"),
  22552. BUILDIN_DEF(geteleminfo,"i?"),
  22553. BUILDIN_DEF(opendressroom,"i?"),
  22554. BUILDIN_DEF(navigateto,"s???????"),
  22555. BUILDIN_DEF(getguildalliance,"ii"),
  22556. BUILDIN_DEF(adopt,"vv"),
  22557. BUILDIN_DEF(getexp2,"ii?"),
  22558. BUILDIN_DEF(recalculatestat,""),
  22559. BUILDIN_DEF(hateffect,"ii"),
  22560. BUILDIN_DEF(getrandomoptinfo, "i"),
  22561. BUILDIN_DEF(getequiprandomoption, "iii?"),
  22562. BUILDIN_DEF(setrandomoption,"iiiii?"),
  22563. BUILDIN_DEF(needed_status_point,"ii?"),
  22564. BUILDIN_DEF(needed_trait_point, "ii?"),
  22565. BUILDIN_DEF(jobcanentermap,"s?"),
  22566. BUILDIN_DEF(openstorage2,"ii?"),
  22567. BUILDIN_DEF(unloadnpc, "s"),
  22568. // WoE TE
  22569. BUILDIN_DEF(agitstart3,""),
  22570. BUILDIN_DEF(agitend3,""),
  22571. BUILDIN_DEF(agitcheck3,""),
  22572. BUILDIN_DEF(gvgon3,"s"),
  22573. BUILDIN_DEF(gvgoff3,"s"),
  22574. // Channel System
  22575. BUILDIN_DEF(channel_create,"ss?????"),
  22576. BUILDIN_DEF(channel_setopt,"sii"),
  22577. BUILDIN_DEF(channel_getopt,"si"),
  22578. BUILDIN_DEF(channel_setcolor,"si"),
  22579. BUILDIN_DEF(channel_setpass,"ss"),
  22580. BUILDIN_DEF(channel_setgroup,"si*"),
  22581. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  22582. BUILDIN_DEF(channel_chat,"ss?"),
  22583. BUILDIN_DEF(channel_ban,"si"),
  22584. BUILDIN_DEF(channel_unban,"si"),
  22585. BUILDIN_DEF(channel_kick,"sv"),
  22586. BUILDIN_DEF(channel_delete,"s"),
  22587. // Item Random Option Extension [Cydh]
  22588. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  22589. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  22590. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  22591. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr?"),
  22592. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  22593. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  22594. // Achievement System
  22595. BUILDIN_DEF(achievementinfo,"ii?"),
  22596. BUILDIN_DEF(achievementadd,"i?"),
  22597. BUILDIN_DEF(achievementremove,"i?"),
  22598. BUILDIN_DEF(achievementcomplete,"i?"),
  22599. BUILDIN_DEF(achievementexists,"i?"),
  22600. BUILDIN_DEF(achievementupdate,"iii?"),
  22601. BUILDIN_DEF(getequiprefinecost,"iii?"),
  22602. BUILDIN_DEF(refineui,"?"),
  22603. BUILDIN_DEF2(round, "round", "ii"),
  22604. BUILDIN_DEF2(round, "ceil", "ii"),
  22605. BUILDIN_DEF2(round, "floor", "ii"),
  22606. BUILDIN_DEF(getequiptradability, "i?"),
  22607. BUILDIN_DEF(mail, "isss*"),
  22608. BUILDIN_DEF(open_roulette,"?"),
  22609. BUILDIN_DEF(identifyall,"??"),
  22610. BUILDIN_DEF(is_guild_leader,"?"),
  22611. BUILDIN_DEF(is_party_leader,"?"),
  22612. BUILDIN_DEF(camerainfo,"iii?"),
  22613. BUILDIN_DEF(achievement_condition,"i"),
  22614. BUILDIN_DEF(getinstancevar,"ri"),
  22615. BUILDIN_DEF2_DEPRECATED(getinstancevar, "getvariableofinstance","ri", "2021-12-13"),
  22616. BUILDIN_DEF(convertpcinfo,"vi"),
  22617. BUILDIN_DEF(isnpccloaked, "??"),
  22618. BUILDIN_DEF(rentalcountitem, "v?"),
  22619. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  22620. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  22621. BUILDIN_DEF(getenchantgrade, "??"),
  22622. BUILDIN_DEF(mob_setidleevent, "is"),
  22623. BUILDIN_DEF(setinstancevar,"rvi"),
  22624. #include "../custom/script_def.inc"
  22625. {NULL,NULL,NULL},
  22626. };