script.cpp 714 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449
  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. enum e_warpparty_target{
  5012. WARPPARTY_RANDOM = 0,
  5013. WARPPARTY_SAVEPOINTALL,
  5014. WARPPARTY_SAVEPOINT,
  5015. WARPPARTY_LEADER,
  5016. WARPPARTY_RANDOMALL,
  5017. WARPPARTY_RANDOMALLAREA
  5018. };
  5019. /*==========================================
  5020. * Warpparty - [Fredzilla] [Paradox924X]
  5021. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5022. * If 'from_mapname' is specified, only the party members on that map will be warped
  5023. *------------------------------------------*/
  5024. BUILDIN_FUNC(warpparty)
  5025. {
  5026. TBL_PC *sd = NULL;
  5027. TBL_PC *pl_sd;
  5028. struct party_data* p;
  5029. int mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5030. const char* str = script_getstr(st,2);
  5031. int x = script_getnum(st,3);
  5032. int y = script_getnum(st,4);
  5033. int p_id = script_getnum(st,5);
  5034. const char* str2 = NULL;
  5035. if ( script_hasdata(st,6) )
  5036. str2 = script_getstr(st,6);
  5037. if (script_hasdata(st, 7))
  5038. rx = script_getnum(st, 7);
  5039. if (script_hasdata(st, 8))
  5040. ry = script_getnum(st, 8);
  5041. p = party_search(p_id);
  5042. if(!p)
  5043. return SCRIPT_CMD_SUCCESS;
  5044. enum e_warpparty_target type = ( strcmp(str,"Random")==0 ) ? WARPPARTY_RANDOM
  5045. : ( strcmp(str,"SavePointAll")==0 ) ? WARPPARTY_SAVEPOINTALL
  5046. : ( strcmp(str,"SavePoint")==0 ) ? WARPPARTY_SAVEPOINT
  5047. : ( strcmp(str,"Leader")==0 ) ? WARPPARTY_LEADER
  5048. : ( strcmp(str,"RandomAll")==0 ) ? WARPPARTY_RANDOMALL
  5049. : WARPPARTY_RANDOMALLAREA;
  5050. switch (type)
  5051. {
  5052. case WARPPARTY_SAVEPOINT:
  5053. //"SavePoint" uses save point of the currently attached player
  5054. if ( !script_rid2sd(sd) )
  5055. return SCRIPT_CMD_FAILURE;
  5056. break;
  5057. case WARPPARTY_LEADER:
  5058. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5059. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5060. return SCRIPT_CMD_FAILURE;
  5061. pl_sd = p->data[i].sd;
  5062. mapindex = pl_sd->mapindex;
  5063. m = map_mapindex2mapid(mapindex);
  5064. x = pl_sd->bl.x;
  5065. y = pl_sd->bl.y;
  5066. break;
  5067. case WARPPARTY_RANDOMALL: {
  5068. if ( !script_rid2sd(sd) )
  5069. return SCRIPT_CMD_FAILURE;
  5070. mapindex = sd->mapindex;
  5071. m = map_mapindex2mapid(mapindex);
  5072. struct map_data *mapdata = map_getmapdata(m);
  5073. if ( mapdata == nullptr || mapdata->flag[MF_NOWARP] || mapdata->flag[MF_NOTELEPORT] )
  5074. return SCRIPT_CMD_FAILURE;
  5075. i = 0;
  5076. do {
  5077. x = rnd()%(mapdata->xs - 2) + 1;
  5078. y = rnd()%(mapdata->ys - 2) + 1;
  5079. } while ((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  5080. if (i >= 1000) {
  5081. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  5082. return SCRIPT_CMD_FAILURE;
  5083. }
  5084. } break;
  5085. case WARPPARTY_RANDOMALLAREA:
  5086. mapindex = mapindex_name2id(str);
  5087. if (!mapindex) {// Invalid map
  5088. return SCRIPT_CMD_FAILURE;
  5089. }
  5090. m = map_mapindex2mapid(mapindex);
  5091. break;
  5092. }
  5093. for (i = 0; i < MAX_PARTY; i++)
  5094. {
  5095. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5096. continue;
  5097. map_data* mapdata = map_getmapdata(pl_sd->bl.m);
  5098. if( str2 && strcmp(str2, mapdata->name) != 0 )
  5099. continue;
  5100. if( pc_isdead(pl_sd) )
  5101. continue;
  5102. e_setpos ret = SETPOS_OK;
  5103. switch( type )
  5104. {
  5105. case WARPPARTY_RANDOM:
  5106. if (!mapdata->flag[MF_NOWARP])
  5107. ret = (e_setpos)pc_randomwarp(pl_sd,CLR_TELEPORT);
  5108. break;
  5109. case WARPPARTY_SAVEPOINTALL:
  5110. if (!mapdata->flag[MF_NORETURN])
  5111. ret = pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5112. break;
  5113. case WARPPARTY_SAVEPOINT:
  5114. if (!mapdata->flag[MF_NORETURN])
  5115. ret = pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5116. break;
  5117. case WARPPARTY_LEADER:
  5118. if (p->party.member[i].leader)
  5119. continue;
  5120. // Fall through
  5121. case WARPPARTY_RANDOMALL:
  5122. if (pl_sd == sd) {
  5123. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5124. break;
  5125. }
  5126. // Fall through
  5127. case WARPPARTY_RANDOMALLAREA:
  5128. if(!mapdata->flag[MF_NORETURN] && !mapdata->flag[MF_NOWARP] && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pl_sd->group_level)){
  5129. if (rx || ry) {
  5130. int x1 = x + rx, y1 = y + ry,
  5131. x0 = x - rx, y0 = y - ry,
  5132. nx, ny;
  5133. uint8 attempts = 10;
  5134. do {
  5135. nx = x0 + rnd()%(x1 - x0 + 1);
  5136. ny = y0 + rnd()%(y1 - y0 + 1);
  5137. } while ((--attempts) > 0 && !map_getcell(m, nx, ny, CELL_CHKPASS));
  5138. if (attempts != 0) { //Keep the original coordinates if fails to find a valid cell within the range
  5139. x = nx;
  5140. y = ny;
  5141. }
  5142. }
  5143. ret = pc_setpos(pl_sd, mapindex, x, y, CLR_TELEPORT);
  5144. }
  5145. break;
  5146. }
  5147. if( ret != SETPOS_OK ) {
  5148. ShowError("buildin_warpparty: moving player '%s' to \"%s\",%d,%d failed.\n", pl_sd->status.name, str, x, y);
  5149. if ( ( type == WARPPARTY_RANDOMALL || type == WARPPARTY_RANDOMALLAREA ) && (rx || ry) )
  5150. continue;
  5151. else
  5152. return SCRIPT_CMD_FAILURE;
  5153. }
  5154. }
  5155. return SCRIPT_CMD_SUCCESS;
  5156. }
  5157. /*==========================================
  5158. * Warpguild - [Fredzilla]
  5159. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5160. *------------------------------------------*/
  5161. BUILDIN_FUNC(warpguild)
  5162. {
  5163. TBL_PC *sd = NULL;
  5164. TBL_PC *pl_sd;
  5165. struct guild* g;
  5166. struct s_mapiterator* iter;
  5167. int type, mapindex = 0, m = -1;
  5168. const char* str = script_getstr(st,2);
  5169. int x = script_getnum(st,3);
  5170. int y = script_getnum(st,4);
  5171. int gid = script_getnum(st,5);
  5172. g = guild_search(gid);
  5173. if( g == NULL )
  5174. return SCRIPT_CMD_SUCCESS;
  5175. type = ( strcmp(str,"Random")==0 ) ? 0
  5176. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5177. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5178. : 3;
  5179. if( type == 2 && !script_rid2sd(sd) )
  5180. {// "SavePoint" uses save point of the currently attached player
  5181. return SCRIPT_CMD_SUCCESS;
  5182. }
  5183. switch (type) {
  5184. case 3:
  5185. mapindex = mapindex_name2id(str);
  5186. if (!mapindex)
  5187. return SCRIPT_CMD_FAILURE;
  5188. m = map_mapindex2mapid(mapindex);
  5189. break;
  5190. }
  5191. iter = mapit_getallusers();
  5192. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5193. {
  5194. if( pl_sd->status.guild_id != gid )
  5195. continue;
  5196. switch( type )
  5197. {
  5198. case 0: // Random
  5199. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5200. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5201. break;
  5202. case 1: // SavePointAll
  5203. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5204. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5205. break;
  5206. case 2: // SavePoint
  5207. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5208. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5209. break;
  5210. case 3: // m,x,y
  5211. 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))
  5212. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5213. break;
  5214. }
  5215. }
  5216. mapit_free(iter);
  5217. return SCRIPT_CMD_SUCCESS;
  5218. }
  5219. /*==========================================
  5220. * Force Heal a player (hp and sp)
  5221. *------------------------------------------*/
  5222. BUILDIN_FUNC(heal)
  5223. {
  5224. TBL_PC *sd;
  5225. int hp,sp;
  5226. if (!script_charid2sd(4,sd))
  5227. return SCRIPT_CMD_SUCCESS;
  5228. hp=script_getnum(st,2);
  5229. sp=script_getnum(st,3);
  5230. status_heal(&sd->bl, hp, sp, 1);
  5231. return SCRIPT_CMD_SUCCESS;
  5232. }
  5233. /*==========================================
  5234. * Force Heal a player (ap)
  5235. *------------------------------------------*/
  5236. BUILDIN_FUNC(healap)
  5237. {
  5238. map_session_data* sd;
  5239. if (!script_charid2sd(3, sd))
  5240. return SCRIPT_CMD_FAILURE;
  5241. status_heal(&sd->bl, 0, 0, script_getnum(st, 2), 1);
  5242. return SCRIPT_CMD_SUCCESS;
  5243. }
  5244. /*==========================================
  5245. * Heal a player by item (get vit bonus etc)
  5246. *------------------------------------------*/
  5247. BUILDIN_FUNC(itemheal)
  5248. {
  5249. TBL_PC *sd;
  5250. int hp,sp;
  5251. hp=script_getnum(st,2);
  5252. sp=script_getnum(st,3);
  5253. if(potion_flag==1) {
  5254. potion_hp = hp;
  5255. potion_sp = sp;
  5256. return SCRIPT_CMD_SUCCESS;
  5257. }
  5258. if (!script_charid2sd(4,sd))
  5259. return SCRIPT_CMD_SUCCESS;
  5260. pc_itemheal(sd,sd->itemid,hp,sp);
  5261. return SCRIPT_CMD_SUCCESS;
  5262. }
  5263. /*==========================================
  5264. *
  5265. *------------------------------------------*/
  5266. BUILDIN_FUNC(percentheal)
  5267. {
  5268. int hp,sp;
  5269. TBL_PC* sd;
  5270. hp=script_getnum(st,2);
  5271. sp=script_getnum(st,3);
  5272. if(potion_flag==1) {
  5273. potion_per_hp = hp;
  5274. potion_per_sp = sp;
  5275. return SCRIPT_CMD_SUCCESS;
  5276. }
  5277. if (!script_charid2sd(4,sd))
  5278. return SCRIPT_CMD_SUCCESS;
  5279. #ifdef RENEWAL
  5280. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5281. sp = 0;
  5282. #endif
  5283. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5284. hp = 0;
  5285. sp = 0;
  5286. }
  5287. if (sd->sc.data[SC_BITESCAR])
  5288. hp = 0;
  5289. pc_percentheal(sd,hp,sp);
  5290. return SCRIPT_CMD_SUCCESS;
  5291. }
  5292. /*==========================================
  5293. *
  5294. *------------------------------------------*/
  5295. BUILDIN_FUNC(jobchange)
  5296. {
  5297. int job, upper=-1;
  5298. job=script_getnum(st,2);
  5299. if( script_hasdata(st,3) )
  5300. upper=script_getnum(st,3);
  5301. if (pcdb_checkid(job))
  5302. {
  5303. TBL_PC* sd;
  5304. if (!script_charid2sd(4,sd))
  5305. return SCRIPT_CMD_SUCCESS;
  5306. pc_jobchange(sd, job, upper);
  5307. }
  5308. return SCRIPT_CMD_SUCCESS;
  5309. }
  5310. /*==========================================
  5311. *
  5312. *------------------------------------------*/
  5313. BUILDIN_FUNC(jobname)
  5314. {
  5315. int class_=script_getnum(st,2);
  5316. script_pushconststr(st, (char*)job_name(class_));
  5317. return SCRIPT_CMD_SUCCESS;
  5318. }
  5319. /// Get input from the player.
  5320. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5321. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5322. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5323. /// shorter than 'min' and 0 otherwise.
  5324. ///
  5325. /// input(<var>{,<min>{,<max>}}) -> <int>
  5326. BUILDIN_FUNC(input)
  5327. {
  5328. TBL_PC* sd;
  5329. struct script_data* data;
  5330. int64 uid;
  5331. const char* name;
  5332. int min;
  5333. int max;
  5334. if( !script_rid2sd(sd) )
  5335. return SCRIPT_CMD_SUCCESS;
  5336. data = script_getdata(st,2);
  5337. if( !data_isreference(data) ){
  5338. ShowError("script:input: not a variable\n");
  5339. script_reportdata(data);
  5340. st->state = END;
  5341. return SCRIPT_CMD_FAILURE;
  5342. }
  5343. uid = reference_getuid(data);
  5344. name = reference_getname(data);
  5345. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5346. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5347. #ifdef SECURE_NPCTIMEOUT
  5348. sd->npc_idle_type = NPCT_WAIT;
  5349. #endif
  5350. if( !sd->state.menu_or_input )
  5351. { // first invocation, display npc input box
  5352. sd->state.menu_or_input = 1;
  5353. st->state = RERUNLINE;
  5354. if( is_string_variable(name) )
  5355. clif_scriptinputstr(sd,st->oid);
  5356. else
  5357. clif_scriptinput(sd,st->oid);
  5358. }
  5359. else
  5360. { // take received text/value and store it in the designated variable
  5361. sd->state.menu_or_input = 0;
  5362. if( is_string_variable(name) )
  5363. {
  5364. int len = (int)strlen(sd->npc_str);
  5365. set_reg_str(st, sd, uid, name, sd->npc_str, script_getref(st,2));
  5366. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5367. }
  5368. else
  5369. {
  5370. int amount = sd->npc_amount;
  5371. set_reg_num(st, sd, uid, name, cap_value(amount,min,max), script_getref(st,2));
  5372. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5373. }
  5374. st->state = RUN;
  5375. }
  5376. return SCRIPT_CMD_SUCCESS;
  5377. }
  5378. // declare the copyarray method here for future reference
  5379. BUILDIN_FUNC(copyarray);
  5380. /// Sets the value of a variable.
  5381. /// The value is converted to the type of the variable.
  5382. ///
  5383. /// set(<variable>,<value>{,<charid>})
  5384. BUILDIN_FUNC(setr)
  5385. {
  5386. TBL_PC* sd = NULL;
  5387. struct script_data* data;
  5388. //struct script_data* datavalue;
  5389. int64 num;
  5390. uint8 pos = 4;
  5391. const char* name, *command = script_getfuncname(st);
  5392. char prefix;
  5393. data = script_getdata(st,2);
  5394. //datavalue = script_getdata(st,3);
  5395. if( !data_isreference(data) )
  5396. {
  5397. ShowError("script:set: not a variable\n");
  5398. script_reportdata(script_getdata(st,2));
  5399. st->state = END;
  5400. return SCRIPT_CMD_FAILURE;
  5401. }
  5402. num = reference_getuid(data);
  5403. name = reference_getname(data);
  5404. prefix = *name;
  5405. if (!strcmp(command, "setr"))
  5406. pos = 5;
  5407. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5408. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5409. return SCRIPT_CMD_FAILURE;
  5410. }
  5411. #if 0
  5412. if( data_isreference(datavalue) )
  5413. {// the value being referenced is a variable
  5414. const char* namevalue = reference_getname(datavalue);
  5415. if( !not_array_variable(*namevalue) )
  5416. {// array variable being copied into another array variable
  5417. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5418. {// player must be attached in order to copy a player variable
  5419. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5420. return SCRIPT_CMD_SUCCESS;
  5421. }
  5422. if( is_string_variable(namevalue) != is_string_variable(name) )
  5423. {// non-matching array value types
  5424. ShowWarning("script:set: two array variables do not match in type.\n");
  5425. return SCRIPT_CMD_SUCCESS;
  5426. }
  5427. // push the maximum number of array values to the stack
  5428. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5429. // call the copy array method directly
  5430. return buildin_copyarray(st);
  5431. }
  5432. }
  5433. #endif
  5434. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5435. if( is_string_variable(name) )
  5436. script_pushstrcopy(st,script_getstr(st, 4));
  5437. else
  5438. script_pushint(st,script_getnum64(st, 4));
  5439. } else // Return a copy of the variable reference
  5440. script_pushcopy(st, 2);
  5441. if( is_string_variable(name) )
  5442. set_reg_str( st, sd, num, name, script_getstr( st, 3 ), script_getref( st, 2 ) );
  5443. else
  5444. set_reg_num( st, sd, num, name, script_getnum64( st, 3 ), script_getref( st, 2 ) );
  5445. return SCRIPT_CMD_SUCCESS;
  5446. }
  5447. /////////////////////////////////////////////////////////////////////
  5448. /// Array variables
  5449. ///
  5450. /// Sets values of an array, from the starting index.
  5451. /// ex: setarray arr[1],1,2,3;
  5452. ///
  5453. /// setarray <array variable>,<value1>{,<value2>...};
  5454. BUILDIN_FUNC(setarray)
  5455. {
  5456. struct script_data* data;
  5457. const char* name;
  5458. uint32 start;
  5459. uint32 end;
  5460. int32 id;
  5461. int32 i;
  5462. TBL_PC* sd = NULL;
  5463. data = script_getdata(st, 2);
  5464. if( !data_isreference(data) )
  5465. {
  5466. ShowError("script:setarray: not a variable\n");
  5467. script_reportdata(data);
  5468. st->state = END;
  5469. return SCRIPT_CMD_FAILURE;// not a variable
  5470. }
  5471. id = reference_getid(data);
  5472. start = reference_getindex(data);
  5473. name = reference_getname(data);
  5474. if( not_server_variable(*name) )
  5475. {
  5476. if( !script_rid2sd(sd) )
  5477. return SCRIPT_CMD_SUCCESS;// no player attached
  5478. }
  5479. end = start + script_lastdata(st) - 2;
  5480. if( end > SCRIPT_MAX_ARRAYSIZE )
  5481. end = SCRIPT_MAX_ARRAYSIZE;
  5482. if( is_string_variable(name) )
  5483. {// string array
  5484. for( i = 3; start < end; ++start, ++i )
  5485. set_reg_str( st, sd, reference_uid( id, start ), name, script_getstr( st, i ), reference_getref( data ) );
  5486. }
  5487. else
  5488. {// int array
  5489. for( i = 3; start < end; ++start, ++i )
  5490. set_reg_num( st, sd, reference_uid( id, start ), name, script_getnum64( st, i ), reference_getref( data ) );
  5491. }
  5492. return SCRIPT_CMD_SUCCESS;
  5493. }
  5494. /// Sets count values of an array, from the starting index.
  5495. /// ex: cleararray arr[0],0,1;
  5496. ///
  5497. /// cleararray <array variable>,<value>,<count>;
  5498. BUILDIN_FUNC(cleararray)
  5499. {
  5500. struct script_data* data;
  5501. const char* name;
  5502. uint32 start;
  5503. uint32 end;
  5504. int32 id;
  5505. TBL_PC* sd = NULL;
  5506. data = script_getdata(st, 2);
  5507. if( !data_isreference(data) )
  5508. {
  5509. ShowError("script:cleararray: not a variable\n");
  5510. script_reportdata(data);
  5511. st->state = END;
  5512. return SCRIPT_CMD_FAILURE;// not a variable
  5513. }
  5514. id = reference_getid(data);
  5515. start = reference_getindex(data);
  5516. name = reference_getname(data);
  5517. if( not_server_variable(*name) )
  5518. {
  5519. if( !script_rid2sd(sd) )
  5520. return SCRIPT_CMD_SUCCESS;// no player attached
  5521. }
  5522. end = start + script_getnum(st, 4);
  5523. if( end > SCRIPT_MAX_ARRAYSIZE )
  5524. end = SCRIPT_MAX_ARRAYSIZE;
  5525. if( is_string_variable( name ) ){
  5526. const char* value = script_getstr( st, 3 );
  5527. for( ; start < end; ++start ){
  5528. set_reg_str( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5529. }
  5530. }else{
  5531. int64 value = script_getnum64( st, 3 );
  5532. for( ; start < end; ++start ){
  5533. set_reg_num( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5534. }
  5535. }
  5536. return SCRIPT_CMD_SUCCESS;
  5537. }
  5538. /// Copies data from one array to another.
  5539. /// ex: copyarray arr[0],arr[2],2;
  5540. ///
  5541. /// copyarray <destination array variable>,<source array variable>,<count>;
  5542. BUILDIN_FUNC(copyarray)
  5543. {
  5544. struct script_data* data1;
  5545. struct script_data* data2;
  5546. const char* name1;
  5547. const char* name2;
  5548. int32 idx1;
  5549. int32 idx2;
  5550. int32 id1;
  5551. int32 id2;
  5552. int32 i;
  5553. uint32 count;
  5554. TBL_PC* sd = NULL;
  5555. data1 = script_getdata(st, 2);
  5556. data2 = script_getdata(st, 3);
  5557. if( !data_isreference(data1) || !data_isreference(data2) )
  5558. {
  5559. ShowError("script:copyarray: not a variable\n");
  5560. script_reportdata(data1);
  5561. script_reportdata(data2);
  5562. st->state = END;
  5563. return SCRIPT_CMD_FAILURE;// not a variable
  5564. }
  5565. id1 = reference_getid(data1);
  5566. id2 = reference_getid(data2);
  5567. idx1 = reference_getindex(data1);
  5568. idx2 = reference_getindex(data2);
  5569. name1 = reference_getname(data1);
  5570. name2 = reference_getname(data2);
  5571. bool is_string = is_string_variable( name1 );
  5572. if( is_string != is_string_variable( name2 ) ){
  5573. ShowError("script:copyarray: type mismatch\n");
  5574. script_reportdata(data1);
  5575. script_reportdata(data2);
  5576. st->state = END;
  5577. return SCRIPT_CMD_FAILURE;// data type mismatch
  5578. }
  5579. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5580. {
  5581. if( !script_rid2sd(sd) )
  5582. return SCRIPT_CMD_SUCCESS;// no player attached
  5583. }
  5584. count = script_getnum(st, 4);
  5585. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5586. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5587. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5588. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5589. if( id1 == id2 && idx1 > idx2 ){
  5590. // destination might be overlapping the source - copy in reverse order
  5591. for( i = count - 1; i >= 0; --i ){
  5592. if( is_string ){
  5593. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5594. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5595. // Remove stack entry from get_val2_str
  5596. script_removetop( st, -1, 0 );
  5597. }else{
  5598. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5599. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5600. }
  5601. }
  5602. }else{
  5603. // normal copy
  5604. for( i = 0; i < count; ++i ){
  5605. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ){
  5606. if( is_string ){
  5607. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5608. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5609. // Remove stack entry from get_val2_str
  5610. script_removetop( st, -1, 0 );
  5611. }else{
  5612. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5613. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5614. }
  5615. }else{
  5616. // out of range - assume ""/0
  5617. clear_reg( st, sd, reference_uid( id1, idx1 + i ), name1, reference_getref( data1 ) );
  5618. }
  5619. }
  5620. }
  5621. return SCRIPT_CMD_SUCCESS;
  5622. }
  5623. /// Returns the size of the array.
  5624. /// Assumes that everything before the starting index exists.
  5625. /// ex: getarraysize(arr[3])
  5626. ///
  5627. /// getarraysize(<array variable>) -> <int>
  5628. BUILDIN_FUNC(getarraysize)
  5629. {
  5630. struct script_data* data;
  5631. const char* name;
  5632. struct map_session_data* sd = NULL;
  5633. data = script_getdata(st, 2);
  5634. if( !data_isreference(data) )
  5635. {
  5636. ShowError("script:getarraysize: not a variable\n");
  5637. script_reportdata(data);
  5638. script_pushnil(st);
  5639. st->state = END;
  5640. return SCRIPT_CMD_FAILURE;// not a variable
  5641. }
  5642. name = reference_getname(data);
  5643. if( not_server_variable(*name) ){
  5644. if (!script_rid2sd(sd))
  5645. return SCRIPT_CMD_SUCCESS;// no player attached
  5646. }
  5647. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5648. return SCRIPT_CMD_SUCCESS;
  5649. }
  5650. int script_array_index_cmp(const void *a, const void *b) {
  5651. return ( *(unsigned int*)a - *(unsigned int*)b );
  5652. }
  5653. /// Deletes count or all the elements in an array, from the starting index.
  5654. /// ex: deletearray arr[4],2;
  5655. ///
  5656. /// deletearray <array variable>;
  5657. /// deletearray <array variable>,<count>;
  5658. BUILDIN_FUNC(deletearray)
  5659. {
  5660. struct script_data* data;
  5661. const char* name;
  5662. unsigned int start, end, i;
  5663. int id;
  5664. TBL_PC *sd = NULL;
  5665. struct script_array *sa = NULL;
  5666. struct reg_db *src = NULL;
  5667. data = script_getdata(st, 2);
  5668. if( !data_isreference(data) ) {
  5669. ShowError("script:deletearray: not a variable\n");
  5670. script_reportdata(data);
  5671. st->state = END;
  5672. return SCRIPT_CMD_FAILURE;// not a variable
  5673. }
  5674. id = reference_getid(data);
  5675. start = reference_getindex(data);
  5676. name = reference_getname(data);
  5677. if( not_server_variable(*name) ) {
  5678. if( !script_rid2sd(sd) )
  5679. return SCRIPT_CMD_SUCCESS;// no player attached
  5680. }
  5681. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5682. ShowError("script:deletearray: not a array\n");
  5683. script_reportdata(data);
  5684. st->state = END;
  5685. return SCRIPT_CMD_FAILURE;// not a variable
  5686. }
  5687. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5688. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5689. return SCRIPT_CMD_SUCCESS;// not a variable
  5690. }
  5691. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5692. if( start >= end )
  5693. return SCRIPT_CMD_SUCCESS;// nothing to free
  5694. if( script_hasdata(st,3) ) {
  5695. unsigned int count = script_getnum(st, 3);
  5696. if( count > end - start )
  5697. count = end - start;
  5698. if( count <= 0 )
  5699. return SCRIPT_CMD_SUCCESS;// nothing to free
  5700. bool is_string = is_string_variable( name );
  5701. if( end - start < sa->size ) {
  5702. // Better to iterate directly on the array, no speed-up from using sa
  5703. for( ; start + count < end; ++start ) {
  5704. // Compact and overwrite
  5705. if( is_string ){
  5706. const char* value = get_val2_str( st, reference_uid( id, start + count ), reference_getref( data ) );
  5707. set_reg_str( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5708. // Remove stack entry from get_val2_str
  5709. script_removetop( st, -1, 0 );
  5710. }else{
  5711. int64 value = get_val2_num( st, reference_uid( id, start + count ), reference_getref( data ) );
  5712. set_reg_num( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5713. }
  5714. }
  5715. for( ; start < end; start++ ) {
  5716. // Clean up any leftovers that weren't overwritten
  5717. clear_reg( st, sd, reference_uid( id, start ), name, reference_getref( data ) );
  5718. }
  5719. } else {
  5720. // using sa to speed up
  5721. unsigned int *list = NULL, size = 0;
  5722. list = script_array_cpy_list(sa);
  5723. size = sa->size;
  5724. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5725. ARR_FIND(0, size, i, list[i] >= start);
  5726. for( ; i < size && list[i] < start + count; i++ ) {
  5727. // Clear any entries between start and start+count, if they exist
  5728. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5729. }
  5730. for( ; i < size && list[i] < end; i++ ) {
  5731. // Move back count positions any entries between start+count to fill the gaps
  5732. if( is_string ){
  5733. const char* value = get_val2_str( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5734. set_reg_str( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5735. // Remove stack entry from get_val2_str
  5736. script_removetop( st, -1, 0 );
  5737. }else{
  5738. int64 value = get_val2_num( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5739. set_reg_num( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5740. }
  5741. // Clear their originals
  5742. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5743. }
  5744. }
  5745. } else {
  5746. unsigned int *list = NULL, size = 0;
  5747. list = script_array_cpy_list(sa);
  5748. size = sa->size;
  5749. for(i = 0; i < size; i++) {
  5750. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5751. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5752. }
  5753. }
  5754. return SCRIPT_CMD_SUCCESS;
  5755. }
  5756. /// Returns a reference to the target index of the array variable.
  5757. /// Equivalent to var[index].
  5758. ///
  5759. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5760. BUILDIN_FUNC(getelementofarray)
  5761. {
  5762. struct script_data* data;
  5763. int32 id;
  5764. int64 i;
  5765. data = script_getdata(st, 2);
  5766. if( !data_isreference(data) )
  5767. {
  5768. ShowError("script:getelementofarray: not a variable\n");
  5769. script_reportdata(data);
  5770. script_pushnil(st);
  5771. st->state = END;
  5772. return SCRIPT_CMD_SUCCESS;// not a variable
  5773. }
  5774. id = reference_getid(data);
  5775. i = script_getnum(st, 3);
  5776. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5777. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5778. script_reportdata(data);
  5779. script_pushnil(st);
  5780. st->state = END;
  5781. return SCRIPT_CMD_FAILURE;// out of range
  5782. }
  5783. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5784. return SCRIPT_CMD_SUCCESS;
  5785. }
  5786. /// Return the index number of the first matching value in an array.
  5787. /// ex: inarray arr,1;
  5788. ///
  5789. /// inarray <array variable>,<value>;
  5790. BUILDIN_FUNC(inarray)
  5791. {
  5792. struct script_data *data;
  5793. const char* name;
  5794. int id, i, array_size;
  5795. struct map_session_data* sd = NULL;
  5796. struct reg_db *ref = NULL;
  5797. data = script_getdata(st, 2);
  5798. if (!data_isreference(data))
  5799. {
  5800. ShowError("buildin_inarray: not a variable\n");
  5801. script_reportdata(data);
  5802. st->state = END;
  5803. return SCRIPT_CMD_FAILURE;
  5804. }
  5805. name = reference_getname(data);
  5806. ref = reference_getref(data);
  5807. if (not_server_variable(*name) && !script_rid2sd(sd))
  5808. return SCRIPT_CMD_FAILURE;
  5809. array_size = script_array_highest_key(st, sd, name, ref) - 1;
  5810. if (array_size < 0)
  5811. {
  5812. script_pushint(st, -1);
  5813. return SCRIPT_CMD_SUCCESS;
  5814. }
  5815. if (array_size > SCRIPT_MAX_ARRAYSIZE)
  5816. {
  5817. ShowError("buildin_inarray: The array is too large.\n");
  5818. script_reportdata(data);
  5819. st->state = END;
  5820. return SCRIPT_CMD_FAILURE;
  5821. }
  5822. id = reference_getid(data);
  5823. if( is_string_variable( name ) ){
  5824. const char* value = script_getstr( st, 3 );
  5825. for( i = 0; i <= array_size; ++i ){
  5826. const char* temp = get_val2_str( st, reference_uid( id, i ), ref );
  5827. if( !strcmp( temp, value ) ){
  5828. // Remove stack entry from get_val2_str
  5829. script_removetop( st, -1, 0 );
  5830. script_pushint( st, i );
  5831. return SCRIPT_CMD_SUCCESS;
  5832. }
  5833. // Remove stack entry from get_val2_str
  5834. script_removetop( st, -1, 0 );
  5835. }
  5836. }else{
  5837. int64 value = script_getnum64( st, 3 );
  5838. for( i = 0; i <= array_size; ++i ){
  5839. int64 temp = get_val2_num( st, reference_uid( id, i ), ref );
  5840. if( temp == value ){
  5841. script_pushint( st, i );
  5842. return SCRIPT_CMD_SUCCESS;
  5843. }
  5844. }
  5845. }
  5846. script_pushint(st, -1);
  5847. return SCRIPT_CMD_SUCCESS;
  5848. }
  5849. /// Return the number of matches in two arrays.
  5850. /// ex: countinarray arr[0],arr1[0];
  5851. ///
  5852. /// countinarray <array variable>,<array variable>;
  5853. BUILDIN_FUNC(countinarray)
  5854. {
  5855. struct script_data *data1 , *data2;
  5856. const char* name1;
  5857. const char* name2;
  5858. int id1, id2, i, j, array_size1, array_size2, case_count = 0;
  5859. struct map_session_data* sd = NULL;
  5860. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5861. data1 = script_getdata(st, 2);
  5862. data2 = script_getdata(st, 3);
  5863. if (!data_isreference(data1) || !data_isreference(data2))
  5864. {
  5865. ShowError("buildin_countinarray: not a variable\n");
  5866. script_reportdata(data1);
  5867. script_reportdata(data2);
  5868. st->state = END;
  5869. return SCRIPT_CMD_FAILURE;
  5870. }
  5871. name1 = reference_getname(data1);
  5872. name2 = reference_getname(data2);
  5873. ref1 = reference_getref(data1);
  5874. ref2 = reference_getref(data2);
  5875. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5876. return SCRIPT_CMD_FAILURE;
  5877. array_size1 = script_array_highest_key(st, sd, name1, ref1) - 1;
  5878. array_size2 = script_array_highest_key(st, sd, name2, ref2) - 1;
  5879. if (array_size1 < 0 || array_size2 < 0)
  5880. {
  5881. script_pushint(st, 0);
  5882. return SCRIPT_CMD_SUCCESS;
  5883. }
  5884. if (array_size1 > SCRIPT_MAX_ARRAYSIZE || array_size2 > SCRIPT_MAX_ARRAYSIZE)
  5885. {
  5886. ShowError("buildin_countinarray: The array is too large.\n");
  5887. script_reportdata(data1);
  5888. script_reportdata(data2);
  5889. st->state = END;
  5890. return SCRIPT_CMD_FAILURE;
  5891. }
  5892. i = reference_getindex(data1);
  5893. j = reference_getindex(data2);
  5894. if (array_size1 < i || array_size2 < j)
  5895. { //To prevent unintended behavior
  5896. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5897. script_reportdata(data1);
  5898. script_reportdata(data2);
  5899. st->state = END;
  5900. return SCRIPT_CMD_FAILURE;
  5901. }
  5902. id1 = reference_getid(data1);
  5903. id2 = reference_getid(data2);
  5904. if( is_string_variable( name1 ) && is_string_variable( name2 ) ){
  5905. for( ; i <= array_size1; ++i ){
  5906. const char* temp1 = get_val2_str( st, reference_uid( id1, i ), ref1 );
  5907. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5908. const char* temp2 = get_val2_str( st, reference_uid( id2, j ), ref2 );
  5909. if( !strcmp( temp1, temp2 ) ){
  5910. case_count++;
  5911. }
  5912. // Remove stack entry from get_val2_str
  5913. script_removetop( st, -1, 0 );
  5914. }
  5915. // Remove stack entry from get_val2_str
  5916. script_removetop( st, -1, 0 );
  5917. }
  5918. }else if( !is_string_variable( name1 ) && !is_string_variable( name2 ) ){
  5919. for( ; i <= array_size1; ++i ){
  5920. int64 temp1 = get_val2_num( st, reference_uid( id1, i ), ref1 );
  5921. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5922. int64 temp2 = get_val2_num( st, reference_uid( id2, j ), ref2 );
  5923. if( temp1 == temp2 ){
  5924. case_count++;
  5925. }
  5926. }
  5927. }
  5928. }else{
  5929. ShowError("buildin_countinarray: Arrays do not match , You can't compare an int array to a string array.\n");
  5930. script_reportdata(data1);
  5931. script_reportdata(data2);
  5932. st->state = END;
  5933. return SCRIPT_CMD_FAILURE;
  5934. }
  5935. script_pushint(st, case_count);
  5936. return SCRIPT_CMD_SUCCESS;
  5937. }
  5938. /////////////////////////////////////////////////////////////////////
  5939. /// ...
  5940. ///
  5941. /*==========================================
  5942. *
  5943. *------------------------------------------*/
  5944. BUILDIN_FUNC(setlook)
  5945. {
  5946. int type,val;
  5947. TBL_PC* sd;
  5948. type = script_getnum(st,2);
  5949. val = script_getnum(st,3);
  5950. if (!script_charid2sd(4,sd))
  5951. return SCRIPT_CMD_SUCCESS;
  5952. pc_changelook(sd,type,val);
  5953. return SCRIPT_CMD_SUCCESS;
  5954. }
  5955. BUILDIN_FUNC(changelook)
  5956. { // As setlook but only client side
  5957. int type,val;
  5958. TBL_PC* sd;
  5959. type = script_getnum(st,2);
  5960. val = script_getnum(st,3);
  5961. if (!script_charid2sd(4,sd))
  5962. return SCRIPT_CMD_SUCCESS;
  5963. switch(type) {
  5964. case LOOK_HAIR:
  5965. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  5966. break;
  5967. case LOOK_HAIR_COLOR:
  5968. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  5969. break;
  5970. case LOOK_CLOTHES_COLOR:
  5971. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  5972. break;
  5973. case LOOK_BODY2:
  5974. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  5975. break;
  5976. default:
  5977. break;
  5978. }
  5979. clif_changelook(&sd->bl, type, val);
  5980. return SCRIPT_CMD_SUCCESS;
  5981. }
  5982. /*==========================================
  5983. *
  5984. *------------------------------------------*/
  5985. BUILDIN_FUNC(cutin)
  5986. {
  5987. TBL_PC* sd;
  5988. if( !script_rid2sd(sd) )
  5989. return SCRIPT_CMD_SUCCESS;
  5990. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5991. return SCRIPT_CMD_SUCCESS;
  5992. }
  5993. /*==========================================
  5994. *
  5995. *------------------------------------------*/
  5996. BUILDIN_FUNC(viewpoint)
  5997. {
  5998. int type,x,y,id,color;
  5999. TBL_PC* sd;
  6000. if (!script_charid2sd(7, sd)) {
  6001. st->state = END;
  6002. return SCRIPT_CMD_FAILURE;
  6003. }
  6004. type=script_getnum(st,2);
  6005. x=script_getnum(st,3);
  6006. y=script_getnum(st,4);
  6007. id=script_getnum(st,5);
  6008. color=script_getnum(st,6);
  6009. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  6010. return SCRIPT_CMD_SUCCESS;
  6011. }
  6012. /**
  6013. * Set random options for new item
  6014. * @param st Script state
  6015. * @param it Temporary item data
  6016. * @param funcname Function name
  6017. * @param x First position of random option id array from the script
  6018. **/
  6019. static int script_getitem_randomoption(struct script_state *st, struct map_session_data* sd, struct item *it, const char *funcname, int x) {
  6020. int i, opt_id_n;
  6021. struct script_data *opt_id = script_getdata(st,x);
  6022. struct script_data *opt_val = script_getdata(st,x+1);
  6023. struct script_data *opt_param = script_getdata(st,x+2);
  6024. const char *opt_id_var = reference_getname(opt_id);
  6025. const char *opt_val_var = reference_getname(opt_val);
  6026. const char *opt_param_var = reference_getname(opt_param);
  6027. int32 opt_id_id, opt_id_idx;
  6028. int32 opt_val_id, opt_val_idx;
  6029. int32 opt_param_id, opt_param_idx;
  6030. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  6031. // Check if the variable requires a player
  6032. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  6033. // If no player is attached
  6034. if( !script_rid2sd(sd) ){
  6035. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  6036. return SCRIPT_CMD_FAILURE;
  6037. }
  6038. }
  6039. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  6040. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  6041. return SCRIPT_CMD_FAILURE;
  6042. }
  6043. if (is_string_variable(opt_id_var)) {
  6044. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  6045. return SCRIPT_CMD_FAILURE;
  6046. }
  6047. // Check if the variable requires a player
  6048. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  6049. // If no player is attached
  6050. if( !script_rid2sd(sd) ){
  6051. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  6052. return SCRIPT_CMD_FAILURE;
  6053. }
  6054. }
  6055. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  6056. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  6057. return SCRIPT_CMD_FAILURE;
  6058. }
  6059. if (is_string_variable(opt_val_var)) {
  6060. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6061. return SCRIPT_CMD_FAILURE;
  6062. }
  6063. // Check if the variable requires a player
  6064. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  6065. // If no player is attached
  6066. if( !script_rid2sd(sd) ){
  6067. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  6068. return SCRIPT_CMD_FAILURE;
  6069. }
  6070. }
  6071. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  6072. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  6073. return SCRIPT_CMD_FAILURE;
  6074. }
  6075. if (is_string_variable(opt_param_var)) {
  6076. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6077. return SCRIPT_CMD_FAILURE;
  6078. }
  6079. opt_id_ref = reference_getref(opt_id);
  6080. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  6081. opt_val_ref = reference_getref(opt_val);
  6082. opt_param_ref = reference_getref(opt_param);
  6083. opt_id_id = reference_getid(opt_id);
  6084. opt_val_id = reference_getid(opt_val);
  6085. opt_param_id = reference_getid(opt_param);
  6086. opt_id_idx = reference_getindex(opt_id);
  6087. opt_val_idx = reference_getindex(opt_val);
  6088. opt_param_idx = reference_getindex(opt_param);
  6089. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6090. it->option[i].id = (short)get_val2_num( st, reference_uid( opt_id_id, opt_id_idx + i ), opt_id_ref );
  6091. it->option[i].value = (short)get_val2_num( st, reference_uid( opt_val_id, opt_val_idx + i ), opt_val_ref );
  6092. it->option[i].param = (char)get_val2_num( st, reference_uid( opt_param_id, opt_param_idx + i ), opt_param_ref );
  6093. }
  6094. return SCRIPT_CMD_SUCCESS;
  6095. }
  6096. /**
  6097. * Sub function for counting items
  6098. * @param items: Item array to search
  6099. * @param id: Item data to search for
  6100. * @param size: Maximum size of array
  6101. * @param expanded: If the script command has extra arguments
  6102. * @param random_option: If the struct command has random option arguments
  6103. * @param st: Script state (required for random options)
  6104. * @param sd: Player data (required for random options)
  6105. * @param rental: Whether or not to count rental items
  6106. * @return Total count of item being searched
  6107. */
  6108. 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) {
  6109. nullpo_retr(-1, items);
  6110. nullpo_retr(-1, st);
  6111. int count = 0;
  6112. if (!expanded) { // For non-expanded functions
  6113. t_itemid nameid = id->nameid;
  6114. for (int i = 0; i < size; i++) {
  6115. item *itm = &items[i];
  6116. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1 || (!rental && itm->expire_time > 0))
  6117. continue;
  6118. if (itm->nameid == nameid)
  6119. count += itm->amount;
  6120. }
  6121. } else { // For expanded functions
  6122. item it;
  6123. memset(&it, 0, sizeof(it));
  6124. it.nameid = id->nameid;
  6125. it.identify = script_getnum(st,3);
  6126. it.refine = script_getnum(st,4);
  6127. it.attribute = script_getnum(st,5);
  6128. it.card[0] = script_getnum(st,6);
  6129. it.card[1] = script_getnum(st,7);
  6130. it.card[2] = script_getnum(st,8);
  6131. it.card[3] = script_getnum(st,9);
  6132. if (random_option) {
  6133. if (!sd) {
  6134. ShowError("buildin_countitem3: Player not attached.\n");
  6135. return -1;
  6136. }
  6137. int res = script_getitem_randomoption(st, sd, &it, "countitem3", 10);
  6138. if (res != SCRIPT_CMD_SUCCESS)
  6139. return -1;
  6140. }
  6141. for (int i = 0; i < size; i++) {
  6142. item *itm = &items[i];
  6143. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1 || (!rental && items[i].expire_time > 0))
  6144. continue;
  6145. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute)
  6146. continue;
  6147. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6148. continue;
  6149. if (random_option) {
  6150. uint8 j;
  6151. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6152. 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)
  6153. break;
  6154. }
  6155. if (j != MAX_ITEM_RDM_OPT)
  6156. continue;
  6157. }
  6158. count += items[i].amount;
  6159. }
  6160. }
  6161. return count;
  6162. }
  6163. /**
  6164. * Returns number of items in inventory
  6165. * countitem(<nameID>{,<accountID>})
  6166. * countitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6167. * countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6168. * countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6169. */
  6170. BUILDIN_FUNC(countitem)
  6171. {
  6172. TBL_PC *sd;
  6173. char *command = (char *)script_getfuncname(st);
  6174. int aid = 3;
  6175. bool random_option = false;
  6176. if (command[strlen(command)-1] == '2')
  6177. aid = 10;
  6178. else if (command[strlen(command)-1] == '3') {
  6179. aid = 13;
  6180. random_option = true;
  6181. }
  6182. if (!script_accid2sd(aid, sd))
  6183. return SCRIPT_CMD_FAILURE;
  6184. std::shared_ptr<item_data> id;
  6185. if (script_isstring(st, 2)) // item name
  6186. id = item_db.searchname( script_getstr( st, 2 ) );
  6187. else // item id
  6188. id = item_db.find( script_getnum( st, 2 ) );
  6189. if (!id) {
  6190. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6191. script_pushint(st, 0);
  6192. return SCRIPT_CMD_FAILURE;
  6193. }
  6194. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, (aid > 3) ? true : false, random_option, st, sd);
  6195. if (count < 0) {
  6196. st->state = END;
  6197. return SCRIPT_CMD_FAILURE;
  6198. }
  6199. script_pushint(st, count);
  6200. return SCRIPT_CMD_SUCCESS;
  6201. }
  6202. /**
  6203. * Returns number of items in cart
  6204. * cartcountitem(<nameID>{,<accountID>})
  6205. * cartcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6206. */
  6207. BUILDIN_FUNC(cartcountitem)
  6208. {
  6209. TBL_PC *sd;
  6210. char *command = (char *)script_getfuncname(st);
  6211. int aid = 3;
  6212. if (command[strlen(command) - 1] == '2')
  6213. aid = 10;
  6214. if (!script_accid2sd(aid, sd))
  6215. return SCRIPT_CMD_FAILURE;
  6216. if (!pc_iscarton(sd)) {
  6217. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6218. script_pushint(st, -1);
  6219. return SCRIPT_CMD_FAILURE;
  6220. }
  6221. std::shared_ptr<item_data> id;
  6222. if (script_isstring(st, 2)) // item name
  6223. id = item_db.searchname( script_getstr( st, 2 ) );
  6224. else // item id
  6225. id = item_db.find( script_getnum( st, 2 ) );
  6226. if (!id) {
  6227. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6228. script_pushint(st, 0);
  6229. return SCRIPT_CMD_FAILURE;
  6230. }
  6231. int count = script_countitem_sub(sd->cart.u.items_cart, id, MAX_CART, (aid > 3) ? true : false, false, st);
  6232. if (count < 0) {
  6233. st->state = END;
  6234. return SCRIPT_CMD_FAILURE;
  6235. }
  6236. script_pushint(st, count);
  6237. return SCRIPT_CMD_SUCCESS;
  6238. }
  6239. /**
  6240. * Returns number of items in storage
  6241. * storagecountitem(<nameID>{,<accountID>})
  6242. * storagecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6243. */
  6244. BUILDIN_FUNC(storagecountitem)
  6245. {
  6246. TBL_PC *sd;
  6247. char *command = (char *)script_getfuncname(st);
  6248. int aid = 3;
  6249. if (command[strlen(command) - 1] == '2')
  6250. aid = 10;
  6251. if (!script_accid2sd(aid, sd))
  6252. return SCRIPT_CMD_FAILURE;
  6253. std::shared_ptr<item_data> id;
  6254. if (script_isstring(st, 2)) // item name
  6255. id = item_db.searchname( script_getstr( st, 2 ) );
  6256. else // item id
  6257. id = item_db.find( script_getnum( st, 2 ) );
  6258. if (!id) {
  6259. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6260. script_pushint(st, 0);
  6261. return SCRIPT_CMD_FAILURE;
  6262. }
  6263. if (sd->state.storage_flag != 0) {
  6264. script_pushint(st, -1);
  6265. return SCRIPT_CMD_SUCCESS;
  6266. }
  6267. int count = script_countitem_sub(sd->storage.u.items_storage, id, MAX_STORAGE, (aid > 3) ? true : false, false, st);
  6268. if (count < 0) {
  6269. st->state = END;
  6270. return SCRIPT_CMD_FAILURE;
  6271. }
  6272. script_pushint(st, count);
  6273. return SCRIPT_CMD_SUCCESS;
  6274. }
  6275. /**
  6276. * Returns number of items in guild storage
  6277. * guildstoragecountitem(<nameID>{,<accountID>})
  6278. * guildstoragecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6279. */
  6280. BUILDIN_FUNC(guildstoragecountitem)
  6281. {
  6282. TBL_PC *sd;
  6283. char *command = (char *)script_getfuncname(st);
  6284. int aid = 3;
  6285. if (command[strlen(command) - 1] == '2')
  6286. aid = 10;
  6287. if (!script_accid2sd(aid, sd))
  6288. return SCRIPT_CMD_FAILURE;
  6289. std::shared_ptr<item_data> id;
  6290. if (script_isstring(st, 2)) // item name
  6291. id = item_db.searchname( script_getstr( st, 2 ) );
  6292. else // item id
  6293. id = item_db.find( script_getnum( st, 2 ) );
  6294. if (!id) {
  6295. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6296. script_pushint(st, 0);
  6297. return SCRIPT_CMD_FAILURE;
  6298. }
  6299. struct s_storage *gstor = guild2storage(sd->status.guild_id);
  6300. if (!gstor || (gstor && sd->state.storage_flag != 0)) {
  6301. script_pushint(st, -1);
  6302. return SCRIPT_CMD_SUCCESS;
  6303. }
  6304. gstor->lock = true;
  6305. int count = script_countitem_sub(gstor->u.items_guild, id, MAX_GUILD_STORAGE, (aid > 3) ? true : false, false, st);
  6306. storage_guild_storageclose(sd);
  6307. gstor->lock = false;
  6308. if (count < 0) {
  6309. st->state = END;
  6310. return SCRIPT_CMD_FAILURE;
  6311. }
  6312. script_pushint(st, count);
  6313. return SCRIPT_CMD_SUCCESS;
  6314. }
  6315. /**
  6316. * Returns number of rental items in inventory
  6317. * rentalcountitem(<nameID>{,<accountID>})
  6318. * rentalcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6319. * rentalcountitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6320. * rentalcountitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6321. */
  6322. BUILDIN_FUNC(rentalcountitem)
  6323. {
  6324. char *command = (char *)script_getfuncname(st);
  6325. int aid = 3;
  6326. bool random_option = false;
  6327. if (command[strlen(command) - 1] == '2')
  6328. aid = 10;
  6329. else if (command[strlen(command) - 1] == '3') {
  6330. aid = 13;
  6331. random_option = true;
  6332. }
  6333. map_session_data *sd;
  6334. if (!script_accid2sd(aid, sd))
  6335. return SCRIPT_CMD_FAILURE;
  6336. std::shared_ptr<item_data> id;
  6337. if (script_isstring(st, 2)) // item name
  6338. id = item_db.searchname( script_getstr( st, 2 ) );
  6339. else // item id
  6340. id = item_db.find( script_getnum( st, 2 ) );
  6341. if (!id) {
  6342. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6343. script_pushint(st, 0);
  6344. return SCRIPT_CMD_FAILURE;
  6345. }
  6346. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, (aid > 3) ? true : false, random_option, st, sd, true);
  6347. if (count < 0) {
  6348. st->state = END;
  6349. return SCRIPT_CMD_FAILURE;
  6350. }
  6351. script_pushint(st, count);
  6352. return SCRIPT_CMD_SUCCESS;
  6353. }
  6354. /*==========================================
  6355. * Check if item with this amount can fit in inventory
  6356. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6357. * Return
  6358. * 0 : fail
  6359. * 1 : success (npc side only)
  6360. *------------------------------------------*/
  6361. BUILDIN_FUNC(checkweight)
  6362. {
  6363. int slots = 0;
  6364. unsigned short amount2 = 0;
  6365. unsigned int weight = 0, i, nbargs;
  6366. std::shared_ptr<item_data> id;
  6367. struct map_session_data* sd;
  6368. if( !script_rid2sd(sd) )
  6369. return SCRIPT_CMD_SUCCESS;
  6370. nbargs = script_lastdata(st)+1;
  6371. if(nbargs%2) {
  6372. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6373. script_pushint(st,0);
  6374. return SCRIPT_CMD_FAILURE;
  6375. }
  6376. slots = pc_inventoryblank(sd); //nb of empty slot
  6377. for (i = 2; i < nbargs; i += 2) {
  6378. t_itemid nameid;
  6379. unsigned short amount;
  6380. if( script_isstring(st, i) ) // item name
  6381. id = item_db.searchname( script_getstr( st, i ) );
  6382. else // item id
  6383. id = item_db.find( script_getnum( st, i ) );
  6384. if( id == nullptr ){
  6385. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6386. script_pushint(st,0);
  6387. return SCRIPT_CMD_FAILURE;
  6388. }
  6389. nameid = id->nameid;
  6390. amount = script_getnum(st,i+1);
  6391. if( amount < 1 ) {
  6392. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6393. script_pushint(st,0);
  6394. return SCRIPT_CMD_FAILURE;
  6395. }
  6396. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6397. if( weight + sd->weight > sd->max_weight )
  6398. {// too heavy
  6399. script_pushint(st,0);
  6400. return SCRIPT_CMD_SUCCESS;
  6401. }
  6402. switch( pc_checkadditem(sd, nameid, amount) ) {
  6403. case CHKADDITEM_EXIST:
  6404. // item is already in inventory, but there is still space for the requested amount
  6405. break;
  6406. case CHKADDITEM_NEW:
  6407. if( itemdb_isstackable(nameid) ) {
  6408. // stackable
  6409. amount2++;
  6410. if( slots < amount2 ) {
  6411. script_pushint(st,0);
  6412. return SCRIPT_CMD_SUCCESS;
  6413. }
  6414. } else {
  6415. // non-stackable
  6416. amount2 += amount;
  6417. if( slots < amount2) {
  6418. script_pushint(st,0);
  6419. return SCRIPT_CMD_SUCCESS;
  6420. }
  6421. }
  6422. break;
  6423. case CHKADDITEM_OVERAMOUNT:
  6424. script_pushint(st,0);
  6425. return SCRIPT_CMD_SUCCESS;
  6426. }
  6427. }
  6428. script_pushint(st,1);
  6429. return SCRIPT_CMD_SUCCESS;
  6430. }
  6431. BUILDIN_FUNC(checkweight2)
  6432. {
  6433. //variable sub checkweight
  6434. int i = 0, slots = 0, weight = 0;
  6435. short fail = 0;
  6436. unsigned short amount2 = 0;
  6437. //variable for array parsing
  6438. struct script_data* data_it;
  6439. struct script_data* data_nb;
  6440. const char* name_it;
  6441. const char* name_nb;
  6442. int32 id_it, id_nb;
  6443. int32 idx_it, idx_nb;
  6444. int nb_it, nb_nb; //array size
  6445. TBL_PC *sd;
  6446. if( !script_rid2sd(sd) )
  6447. return SCRIPT_CMD_SUCCESS;
  6448. data_it = script_getdata(st, 2);
  6449. data_nb = script_getdata(st, 3);
  6450. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6451. ShowError("buildin_checkweight2: parameter not a variable\n");
  6452. script_pushint(st,0);
  6453. return SCRIPT_CMD_FAILURE;// not a variable
  6454. }
  6455. id_it = reference_getid(data_it);
  6456. id_nb = reference_getid(data_nb);
  6457. idx_it = reference_getindex(data_it);
  6458. idx_nb = reference_getindex(data_nb);
  6459. name_it = reference_getname(data_it);
  6460. name_nb = reference_getname(data_nb);
  6461. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6462. ShowError("buildin_checkweight2: illegal type, need int\n");
  6463. script_pushint(st,0);
  6464. return SCRIPT_CMD_FAILURE;// not supported
  6465. }
  6466. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6467. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6468. if(nb_it != nb_nb) {
  6469. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6470. fail = 1;
  6471. }
  6472. slots = pc_inventoryblank(sd);
  6473. for(i=0; i<nb_it; i++) {
  6474. t_itemid nameid = (t_itemid)get_val2_num( st, reference_uid( id_it, idx_it + i ), reference_getref( data_it ) );
  6475. unsigned short amount = (unsigned short)get_val2_num( st, reference_uid( id_nb, idx_nb + i ), reference_getref( data_nb ) );
  6476. if(fail)
  6477. continue; //cpntonie to depop rest
  6478. if(itemdb_exists(nameid) == NULL ) {
  6479. ShowError("buildin_checkweight2: Invalid item '%u'.\n", nameid);
  6480. fail=1;
  6481. continue;
  6482. }
  6483. if(amount < 0 ) {
  6484. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6485. fail = 1;
  6486. continue;
  6487. }
  6488. weight += itemdb_weight(nameid)*amount;
  6489. if( weight + sd->weight > sd->max_weight ) {
  6490. fail = 1;
  6491. continue;
  6492. }
  6493. switch( pc_checkadditem(sd, nameid, amount) ) {
  6494. case CHKADDITEM_EXIST:
  6495. // item is already in inventory, but there is still space for the requested amount
  6496. break;
  6497. case CHKADDITEM_NEW:
  6498. if( itemdb_isstackable(nameid) ) {// stackable
  6499. amount2++;
  6500. if( slots < amount2 )
  6501. fail = 1;
  6502. } else {// non-stackable
  6503. amount2 += amount;
  6504. if( slots < amount2 ) {
  6505. fail = 1;
  6506. }
  6507. }
  6508. break;
  6509. case CHKADDITEM_OVERAMOUNT:
  6510. fail = 1;
  6511. } //end switch
  6512. } //end loop DO NOT break it prematurly we need to depop all stack
  6513. fail ? script_pushint(st,0) : script_pushint(st,1);
  6514. return SCRIPT_CMD_SUCCESS;
  6515. }
  6516. /*==========================================
  6517. * getitem <item id>,<amount>{,<account ID>};
  6518. * getitem "<item name>",<amount>{,<account ID>};
  6519. *
  6520. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6521. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6522. * Type:
  6523. * 0 - No bound
  6524. * 1 - Account Bound
  6525. * 2 - Guild Bound
  6526. * 3 - Party Bound
  6527. * 4 - Character Bound
  6528. *------------------------------------------*/
  6529. BUILDIN_FUNC(getitem)
  6530. {
  6531. int get_count, i;
  6532. t_itemid nameid;
  6533. unsigned short amount;
  6534. struct item it;
  6535. struct map_session_data *sd;
  6536. unsigned char flag = 0;
  6537. const char* command = script_getfuncname(st);
  6538. std::shared_ptr<item_data> id;
  6539. if( script_isstring(st, 2) ) {// "<item name>"
  6540. const char *name = script_getstr(st, 2);
  6541. id = item_db.searchname( name );
  6542. if( id == nullptr ){
  6543. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6544. return SCRIPT_CMD_FAILURE; //No item created.
  6545. }
  6546. nameid = id->nameid;
  6547. } else {// <item id>
  6548. nameid = script_getnum(st, 2);
  6549. id = item_db.find( nameid );
  6550. if( id == nullptr ){
  6551. ShowError("buildin_getitem: Nonexistant item %u requested.\n", nameid);
  6552. return SCRIPT_CMD_FAILURE; //No item created.
  6553. }
  6554. }
  6555. // <amount>
  6556. if( (amount = script_getnum(st,3)) <= 0)
  6557. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6558. memset(&it,0,sizeof(it));
  6559. it.nameid = nameid;
  6560. it.identify = 1;
  6561. it.bound = BOUND_NONE;
  6562. if( !strcmp(command,"getitembound") ) {
  6563. char bound = script_getnum(st,4);
  6564. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6565. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6566. return SCRIPT_CMD_FAILURE;
  6567. }
  6568. script_mapid2sd(5,sd);
  6569. it.bound = bound;
  6570. } else {
  6571. script_mapid2sd(4,sd);
  6572. }
  6573. if( sd == NULL ) // no target
  6574. return SCRIPT_CMD_SUCCESS;
  6575. //Check if it's stackable.
  6576. if( !itemdb_isstackable2( id.get() ) ){
  6577. get_count = 1;
  6578. }else{
  6579. get_count = amount;
  6580. }
  6581. for (i = 0; i < amount; i += get_count)
  6582. {
  6583. // if not pet egg
  6584. if (!pet_create_egg(sd, nameid))
  6585. {
  6586. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6587. {
  6588. clif_additem(sd, 0, 0, flag);
  6589. if( pc_candrop(sd,&it) )
  6590. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6591. }
  6592. }
  6593. }
  6594. return SCRIPT_CMD_SUCCESS;
  6595. }
  6596. /*==========================================
  6597. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6598. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6599. *
  6600. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6601. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6602. *
  6603. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6604. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6605. *
  6606. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6607. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6608. * Type:
  6609. * 0 - No bound
  6610. * 1 - Account Bound
  6611. * 2 - Guild Bound
  6612. * 3 - Party Bound
  6613. * 4 - Character Bound
  6614. *------------------------------------------*/
  6615. BUILDIN_FUNC(getitem2)
  6616. {
  6617. t_itemid nameid;
  6618. unsigned short amount;
  6619. int iden, ref, attr;
  6620. t_itemid c1, c2, c3, c4;
  6621. char bound = BOUND_NONE;
  6622. std::shared_ptr<item_data> item_data;
  6623. struct item item_tmp;
  6624. TBL_PC *sd;
  6625. const char* command = script_getfuncname(st);
  6626. int offset = 0;
  6627. if( !strncmp(command,"getitembound",12) ) {
  6628. int aid_pos = 12;
  6629. bound = script_getnum(st,11);
  6630. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6631. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6632. return SCRIPT_CMD_FAILURE;
  6633. }
  6634. if (command[strlen(command)-1] == '3') {
  6635. offset = 12;
  6636. aid_pos = 15;
  6637. }
  6638. script_mapid2sd(aid_pos,sd);
  6639. } else {
  6640. int aid_pos = 11;
  6641. if (strcmpi(command,"getitem3") == 0) {
  6642. offset = 11;
  6643. aid_pos = 14;
  6644. }
  6645. script_mapid2sd(aid_pos,sd);
  6646. }
  6647. if( sd == NULL ) // no target
  6648. return SCRIPT_CMD_SUCCESS;
  6649. if( script_isstring(st, 2) ) {
  6650. const char *name = script_getstr(st, 2);
  6651. item_data = item_db.searchname( name );
  6652. if( item_data == nullptr ){
  6653. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6654. return SCRIPT_CMD_FAILURE; //No item created.
  6655. }
  6656. nameid = item_data->nameid;
  6657. } else {
  6658. nameid = script_getnum(st, 2);
  6659. item_data = item_db.find( nameid );
  6660. if( item_data == nullptr ){
  6661. ShowError("buildin_getitem2: Nonexistant item %u requested (by conv_num).\n", nameid);
  6662. return SCRIPT_CMD_FAILURE; //No item created.
  6663. }
  6664. }
  6665. amount = script_getnum(st,3);
  6666. iden = script_getnum(st,4);
  6667. ref = script_getnum(st,5);
  6668. attr = script_getnum(st,6);
  6669. c1 = script_getnum(st,7);
  6670. c2 = script_getnum(st,8);
  6671. c3 = script_getnum(st,9);
  6672. c4 = script_getnum(st,10);
  6673. if( item_data ) {
  6674. int get_count = 0, i;
  6675. memset(&item_tmp,0,sizeof(item_tmp));
  6676. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6677. if(ref > MAX_REFINE)
  6678. ref = MAX_REFINE;
  6679. }
  6680. else if( item_data->type == IT_PETEGG ) {
  6681. iden = 1;
  6682. ref = 0;
  6683. }
  6684. else {
  6685. iden = 1;
  6686. ref = attr = 0;
  6687. }
  6688. item_tmp.nameid = nameid;
  6689. item_tmp.identify = iden;
  6690. item_tmp.refine = ref;
  6691. item_tmp.attribute = attr;
  6692. item_tmp.card[0] = c1;
  6693. item_tmp.card[1] = c2;
  6694. item_tmp.card[2] = c3;
  6695. item_tmp.card[3] = c4;
  6696. item_tmp.bound = bound;
  6697. if (offset != 0) {
  6698. int res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6699. if (res == SCRIPT_CMD_FAILURE)
  6700. return SCRIPT_CMD_FAILURE;
  6701. }
  6702. //Check if it's stackable.
  6703. if( !itemdb_isstackable2( item_data.get() ) ){
  6704. get_count = 1;
  6705. }else{
  6706. get_count = amount;
  6707. }
  6708. for (i = 0; i < amount; i += get_count)
  6709. {
  6710. // if not pet egg
  6711. if (!pet_create_egg(sd, nameid))
  6712. {
  6713. unsigned char flag = 0;
  6714. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6715. {
  6716. clif_additem(sd, 0, 0, flag);
  6717. if( pc_candrop(sd,&item_tmp) )
  6718. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6719. }
  6720. }
  6721. }
  6722. }
  6723. return SCRIPT_CMD_SUCCESS;
  6724. }
  6725. /** Gives rental item to player
  6726. * rentitem <item id>,<seconds>{,<account_id>}
  6727. * rentitem "<item name>",<seconds>{,<account_id>}
  6728. */
  6729. BUILDIN_FUNC(rentitem) {
  6730. struct map_session_data *sd;
  6731. struct item it;
  6732. int seconds;
  6733. t_itemid nameid = 0;
  6734. unsigned char flag = 0;
  6735. if (!script_accid2sd(4,sd))
  6736. return SCRIPT_CMD_FAILURE;
  6737. if( script_isstring(st, 2) )
  6738. {
  6739. const char *name = script_getstr(st, 2);
  6740. std::shared_ptr<item_data> itd = item_db.searchname( name );
  6741. if( itd == nullptr ){
  6742. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6743. return SCRIPT_CMD_FAILURE;
  6744. }
  6745. nameid = itd->nameid;
  6746. }
  6747. else
  6748. {
  6749. nameid = script_getnum(st, 2);
  6750. if( nameid == 0 || !itemdb_exists(nameid) )
  6751. {
  6752. ShowError("buildin_rentitem: Nonexistant item %u requested.\n", nameid);
  6753. return SCRIPT_CMD_FAILURE;
  6754. }
  6755. }
  6756. seconds = script_getnum(st,3);
  6757. memset(&it, 0, sizeof(it));
  6758. it.nameid = nameid;
  6759. it.identify = 1;
  6760. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6761. it.bound = BOUND_NONE;
  6762. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6763. {
  6764. clif_additem(sd, 0, 0, flag);
  6765. return SCRIPT_CMD_FAILURE;
  6766. }
  6767. return SCRIPT_CMD_SUCCESS;
  6768. }
  6769. /**
  6770. * Gives rental item to player with advanced option
  6771. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6772. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6773. *
  6774. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6775. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6776. */
  6777. BUILDIN_FUNC(rentitem2) {
  6778. struct map_session_data *sd;
  6779. struct item it;
  6780. std::shared_ptr<item_data> id;
  6781. int seconds;
  6782. t_itemid nameid = 0;
  6783. unsigned char flag = 0;
  6784. int iden,ref,attr;
  6785. t_itemid c1,c2,c3,c4;
  6786. const char *funcname = script_getfuncname(st);
  6787. if (funcname[strlen(funcname)-1] == '3') {
  6788. if (!script_accid2sd(14,sd))
  6789. return SCRIPT_CMD_FAILURE;
  6790. } else if (!script_accid2sd(11,sd))
  6791. return SCRIPT_CMD_FAILURE;
  6792. if( script_isstring(st, 2) ) {
  6793. const char *name = script_getstr(st, 2);
  6794. id = item_db.searchname( name );
  6795. if( id == nullptr ) {
  6796. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6797. return SCRIPT_CMD_FAILURE;
  6798. }
  6799. nameid = id->nameid;
  6800. } else {
  6801. nameid = script_getnum(st, 2);
  6802. id = item_db.find( nameid );
  6803. if( id == nullptr ){
  6804. ShowError("buildin_rentitem2: Nonexistant item %u requested.\n", nameid);
  6805. return SCRIPT_CMD_FAILURE;
  6806. }
  6807. }
  6808. seconds = script_getnum(st,3);
  6809. iden = script_getnum(st,4);
  6810. ref = script_getnum(st,5);
  6811. attr = script_getnum(st,6);
  6812. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6813. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6814. }
  6815. else if (id->type==IT_PETEGG) {
  6816. iden = 1;
  6817. ref = 0;
  6818. }
  6819. else {
  6820. iden = 1;
  6821. ref = attr = 0;
  6822. }
  6823. c1 = script_getnum(st,7);
  6824. c2 = script_getnum(st,8);
  6825. c3 = script_getnum(st,9);
  6826. c4 = script_getnum(st,10);
  6827. memset(&it, 0, sizeof(it));
  6828. it.nameid = nameid;
  6829. it.identify = iden;
  6830. it.refine = ref;
  6831. it.attribute = attr;
  6832. it.card[0] = c1;
  6833. it.card[1] = c2;
  6834. it.card[2] = c3;
  6835. it.card[3] = c4;
  6836. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6837. it.bound = BOUND_NONE;
  6838. if (funcname[strlen(funcname)-1] == '3') {
  6839. int res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6840. if (res != SCRIPT_CMD_SUCCESS)
  6841. return res;
  6842. }
  6843. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6844. clif_additem(sd, 0, 0, flag);
  6845. return SCRIPT_CMD_FAILURE;
  6846. }
  6847. return SCRIPT_CMD_SUCCESS;
  6848. }
  6849. /*==========================================
  6850. * gets an item with someone's name inscribed [Skotlex]
  6851. * getinscribeditem item_num, character_name
  6852. * Returned Qty is always 1, only works on equip-able
  6853. * equipment
  6854. *------------------------------------------*/
  6855. BUILDIN_FUNC(getnameditem)
  6856. {
  6857. t_itemid nameid;
  6858. struct item item_tmp;
  6859. TBL_PC *sd, *tsd;
  6860. if (!script_rid2sd(sd))
  6861. { //Player not attached!
  6862. script_pushint(st,0);
  6863. return SCRIPT_CMD_SUCCESS;
  6864. }
  6865. if( script_isstring(st, 2) ){
  6866. const char *name = script_getstr(st, 2);
  6867. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6868. // Failed
  6869. if( item_data == nullptr){
  6870. script_pushint(st,0);
  6871. return SCRIPT_CMD_SUCCESS;
  6872. }
  6873. nameid = item_data->nameid;
  6874. }else
  6875. nameid = script_getnum(st, 2);
  6876. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6877. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6878. script_pushint(st,0);
  6879. return SCRIPT_CMD_SUCCESS;
  6880. }
  6881. if( script_isstring(st, 3) ) //Char Name
  6882. tsd = map_nick2sd(script_getstr(st, 3),false);
  6883. else //Char Id was given
  6884. tsd = map_charid2sd(script_getnum(st, 3));
  6885. if( tsd == NULL )
  6886. { //Failed
  6887. script_pushint(st,0);
  6888. return SCRIPT_CMD_SUCCESS;
  6889. }
  6890. memset(&item_tmp,0,sizeof(item_tmp));
  6891. item_tmp.nameid=nameid;
  6892. item_tmp.amount=1;
  6893. item_tmp.identify=1;
  6894. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6895. item_tmp.card[2]=GetWord(tsd->status.char_id,0);
  6896. item_tmp.card[3]=GetWord(tsd->status.char_id,1);
  6897. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6898. script_pushint(st,0);
  6899. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6900. }
  6901. script_pushint(st,1);
  6902. return SCRIPT_CMD_SUCCESS;
  6903. }
  6904. /*==========================================
  6905. * gets a random item ID from an item group [Skotlex]
  6906. * groupranditem <group_num>{,<sub_group>};
  6907. *------------------------------------------*/
  6908. BUILDIN_FUNC(grouprandomitem) {
  6909. int sub_group = 1;
  6910. FETCH(3, sub_group);
  6911. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(script_getnum(st,2),sub_group);
  6912. if (!entry) {
  6913. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6914. script_pushint(st,UNKNOWN_ITEM_ID);
  6915. return SCRIPT_CMD_FAILURE;
  6916. }
  6917. script_pushint(st,entry->nameid);
  6918. return SCRIPT_CMD_SUCCESS;
  6919. }
  6920. /**
  6921. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  6922. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>{,<canShowEffect>};
  6923. */
  6924. BUILDIN_FUNC(makeitem) {
  6925. t_itemid nameid;
  6926. uint16 amount, flag = 0, x, y;
  6927. const char *mapname;
  6928. int m;
  6929. struct item item_tmp;
  6930. bool canShowEffect = false;
  6931. if( script_isstring(st, 2) ){
  6932. const char *name = script_getstr(st, 2);
  6933. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6934. if( item_data )
  6935. nameid = item_data->nameid;
  6936. else{
  6937. ShowError( "buildin_makeitem: Unknown item %s\n", name );
  6938. return SCRIPT_CMD_FAILURE;
  6939. }
  6940. }
  6941. else {
  6942. int32 val = script_getnum( st, 2 );
  6943. if( val < 0 ){
  6944. flag = 1;
  6945. nameid = (t_itemid)( -1 * val );
  6946. }else{
  6947. nameid = (t_itemid)val;
  6948. }
  6949. if( !itemdb_exists( nameid ) ){
  6950. ShowError( "buildin_makeitem: Unknown item id %u\n", nameid );
  6951. return SCRIPT_CMD_FAILURE;
  6952. }
  6953. }
  6954. amount = script_getnum(st,3);
  6955. mapname = script_getstr(st,4);
  6956. x = script_getnum(st,5);
  6957. y = script_getnum(st,6);
  6958. if (script_hasdata(st, 7))
  6959. canShowEffect = script_getnum(st, 7) != 0;
  6960. if (strcmp(mapname, "this")==0) {
  6961. TBL_PC *sd;
  6962. if (!script_rid2sd(sd))
  6963. return SCRIPT_CMD_SUCCESS; //Failed...
  6964. m = sd->bl.m;
  6965. } else
  6966. m = map_mapname2mapid(mapname);
  6967. memset(&item_tmp,0,sizeof(item_tmp));
  6968. item_tmp.nameid = nameid;
  6969. if (!flag)
  6970. item_tmp.identify = 1;
  6971. else
  6972. item_tmp.identify = itemdb_isidentified(nameid);
  6973. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  6974. return SCRIPT_CMD_SUCCESS;
  6975. }
  6976. /**
  6977. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  6978. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<canShowEffect>};
  6979. *
  6980. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  6981. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<canShowEffect>};
  6982. */
  6983. BUILDIN_FUNC(makeitem2) {
  6984. t_itemid nameid;
  6985. uint16 amount, x, y;
  6986. const char *mapname;
  6987. int m;
  6988. struct item item_tmp;
  6989. struct item_data *id;
  6990. const char *funcname = script_getfuncname(st);
  6991. bool canShowEffect = false;
  6992. if( script_isstring( st, 2 ) ){
  6993. const char *name = script_getstr( st, 2 );
  6994. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  6995. if( item_data ){
  6996. nameid = item_data->nameid;
  6997. }else{
  6998. ShowError( "buildin_%s: Unknown item %s\n", funcname, name );
  6999. return SCRIPT_CMD_FAILURE;
  7000. }
  7001. }else{
  7002. nameid = (t_itemid)script_getnum( st, 2 );
  7003. if( !itemdb_exists( nameid ) ){
  7004. ShowError( "buildin_%s: Unknown item id %u\n", funcname, nameid );
  7005. return SCRIPT_CMD_FAILURE;
  7006. }
  7007. }
  7008. amount = script_getnum(st,3);
  7009. mapname = script_getstr(st,4);
  7010. x = script_getnum(st,5);
  7011. y = script_getnum(st,6);
  7012. if (strcmp(mapname,"this")==0) {
  7013. TBL_PC *sd;
  7014. if (!script_rid2sd(sd))
  7015. return SCRIPT_CMD_SUCCESS; //Failed...
  7016. m = sd->bl.m;
  7017. }
  7018. else
  7019. m = map_mapname2mapid(mapname);
  7020. if ((id = itemdb_search(nameid))) {
  7021. char iden, ref, attr;
  7022. memset(&item_tmp,0,sizeof(item_tmp));
  7023. item_tmp.nameid = nameid;
  7024. iden = (char)script_getnum(st,7);
  7025. ref = (char)script_getnum(st,8);
  7026. attr = (char)script_getnum(st,9);
  7027. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  7028. if(ref > MAX_REFINE) ref = MAX_REFINE;
  7029. }
  7030. else if (id->type==IT_PETEGG) {
  7031. iden = 1;
  7032. ref = 0;
  7033. }
  7034. else {
  7035. iden = 1;
  7036. ref = attr = 0;
  7037. }
  7038. item_tmp.identify = iden;
  7039. item_tmp.refine = ref;
  7040. item_tmp.attribute = attr;
  7041. item_tmp.card[0] = script_getnum(st,10);
  7042. item_tmp.card[1] = script_getnum(st,11);
  7043. item_tmp.card[2] = script_getnum(st,12);
  7044. item_tmp.card[3] = script_getnum(st,13);
  7045. if (funcname[strlen(funcname)-1] == '3') {
  7046. int res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, 14);
  7047. if (res != SCRIPT_CMD_SUCCESS)
  7048. return res;
  7049. if (script_hasdata(st, 17)) {
  7050. if (script_getnum(st, 17) != 0)
  7051. canShowEffect = script_getnum(st, 17) != 0;
  7052. }
  7053. }
  7054. else {
  7055. if (script_hasdata(st, 14)) {
  7056. if (script_getnum(st, 14) != 0)
  7057. canShowEffect = script_getnum(st, 14) != 0;
  7058. }
  7059. }
  7060. map_addflooritem(&item_tmp, amount, m, x, y, 0, 0, 0, 4, 0, canShowEffect);
  7061. }
  7062. else
  7063. return SCRIPT_CMD_FAILURE;
  7064. return SCRIPT_CMD_SUCCESS;
  7065. }
  7066. /// Counts / deletes the current item given by idx.
  7067. /// Used by buildin_delitem_search
  7068. /// Relies on all input data being already fully valid.
  7069. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  7070. {
  7071. int delamount;
  7072. struct item *itm = NULL;
  7073. struct s_storage *gstor = NULL;
  7074. switch(loc) {
  7075. case TABLE_CART:
  7076. itm = &sd->cart.u.items_cart[idx];
  7077. break;
  7078. case TABLE_STORAGE:
  7079. itm = &sd->storage.u.items_storage[idx];
  7080. break;
  7081. case TABLE_GUILD_STORAGE:
  7082. {
  7083. gstor = guild2storage2(sd->status.guild_id);
  7084. itm = &gstor->u.items_guild[idx];
  7085. }
  7086. break;
  7087. default: // TABLE_INVENTORY
  7088. itm = &sd->inventory.u.items_inventory[idx];
  7089. break;
  7090. }
  7091. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7092. if( delete_items )
  7093. {
  7094. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7095. {// delete associated pet
  7096. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7097. }
  7098. switch(loc) {
  7099. case TABLE_CART:
  7100. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7101. break;
  7102. case TABLE_STORAGE:
  7103. storage_delitem(sd,&sd->storage,idx,delamount);
  7104. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7105. break;
  7106. case TABLE_GUILD_STORAGE:
  7107. gstor->lock = true;
  7108. storage_guild_delitem(sd, gstor, idx, delamount);
  7109. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7110. storage_guild_storageclose(sd);
  7111. gstor->lock = false;
  7112. break;
  7113. default: // TABLE_INVENTORY
  7114. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7115. break;
  7116. }
  7117. }
  7118. amount[0]-= delamount;
  7119. }
  7120. /// Searches for item(s) and checks, if there is enough of them.
  7121. /// Used by delitem and delitem2
  7122. /// Relies on all input data being already fully valid.
  7123. /// @param exact_match will also match item by specified attributes
  7124. /// 0x0: Only item id
  7125. /// 0x1: identify, attributes, cards
  7126. /// 0x2: random option
  7127. /// @return true when all items could be deleted, false when there were not enough items to delete
  7128. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7129. {
  7130. bool delete_items = false;
  7131. int i, amount, size;
  7132. struct item *items;
  7133. // prefer always non-equipped items
  7134. it->equip = 0;
  7135. // when searching for nameid only, prefer additionally
  7136. if( !exact_match )
  7137. {
  7138. // non-refined items
  7139. it->refine = 0;
  7140. // card-less items
  7141. memset(it->card, 0, sizeof(it->card));
  7142. }
  7143. switch(loc) {
  7144. case TABLE_CART:
  7145. size = MAX_CART;
  7146. items = sd->cart.u.items_cart;
  7147. break;
  7148. case TABLE_STORAGE:
  7149. size = MAX_STORAGE;
  7150. items = sd->storage.u.items_storage;
  7151. break;
  7152. case TABLE_GUILD_STORAGE:
  7153. {
  7154. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7155. size = MAX_GUILD_STORAGE;
  7156. items = gstor->u.items_guild;
  7157. }
  7158. break;
  7159. default: // TABLE_INVENTORY
  7160. size = MAX_INVENTORY;
  7161. items = sd->inventory.u.items_inventory;
  7162. break;
  7163. }
  7164. for(;;)
  7165. {
  7166. unsigned short important = 0;
  7167. amount = it->amount;
  7168. // 1st pass -- less important items / exact match
  7169. for( i = 0; amount && i < size; i++ )
  7170. {
  7171. struct item *itm = NULL;
  7172. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7173. {// wrong/invalid item
  7174. continue;
  7175. }
  7176. if( itm->equip != it->equip || itm->refine != it->refine )
  7177. {// not matching attributes
  7178. important++;
  7179. continue;
  7180. }
  7181. if( exact_match )
  7182. {
  7183. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7184. {// not matching exact attributes
  7185. continue;
  7186. }
  7187. if (exact_match&0x2) {
  7188. uint8 j;
  7189. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7190. 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)
  7191. break;
  7192. }
  7193. if (j != MAX_ITEM_RDM_OPT)
  7194. continue;
  7195. }
  7196. }
  7197. else
  7198. {
  7199. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7200. {
  7201. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7202. {// pet which cannot be deleted
  7203. continue;
  7204. }
  7205. }
  7206. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7207. {// named/carded item
  7208. important++;
  7209. continue;
  7210. }
  7211. }
  7212. // count / delete item
  7213. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7214. }
  7215. // 2nd pass -- any matching item
  7216. if( amount == 0 || important == 0 )
  7217. {// either everything was already consumed or no items were skipped
  7218. ;
  7219. }
  7220. else for( i = 0; amount && i < size; i++ )
  7221. {
  7222. struct item *itm = NULL;
  7223. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7224. {// wrong/invalid item
  7225. continue;
  7226. }
  7227. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7228. {// pet which cannot be deleted
  7229. continue;
  7230. }
  7231. if( exact_match )
  7232. {
  7233. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7234. {// not matching attributes
  7235. continue;
  7236. }
  7237. if (exact_match&0x2) {
  7238. uint8 j;
  7239. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7240. 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)
  7241. break;
  7242. }
  7243. if (j != MAX_ITEM_RDM_OPT)
  7244. continue;
  7245. }
  7246. }
  7247. // count / delete item
  7248. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7249. }
  7250. if( amount )
  7251. {// not enough items
  7252. return false;
  7253. }
  7254. else if( delete_items )
  7255. {// we are done with the work
  7256. return true;
  7257. }
  7258. else
  7259. {// get rid of the items now
  7260. delete_items = true;
  7261. }
  7262. }
  7263. }
  7264. /// Deletes items from the target/attached player.
  7265. /// Prioritizes ordinary items.
  7266. ///
  7267. /// delitem <item id>,<amount>{,<account id>}
  7268. /// delitem "<item name>",<amount>{,<account id>}
  7269. /// cartdelitem <item id>,<amount>{,<account id>}
  7270. /// cartdelitem "<item name>",<amount>{,<account id>}
  7271. /// storagedelitem <item id>,<amount>{,<account id>}
  7272. /// storagedelitem "<item name>",<amount>{,<account id>}
  7273. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7274. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7275. BUILDIN_FUNC(delitem)
  7276. {
  7277. TBL_PC *sd;
  7278. struct item it;
  7279. uint8 loc = 0;
  7280. char* command = (char*)script_getfuncname(st);
  7281. if(!strncmp(command, "cart", 4))
  7282. loc = TABLE_CART;
  7283. else if(!strncmp(command, "storage", 7))
  7284. loc = TABLE_STORAGE;
  7285. else if(!strncmp(command, "guildstorage", 12))
  7286. loc = TABLE_GUILD_STORAGE;
  7287. if( !script_accid2sd(4,sd) ){
  7288. // In any case cancel script execution
  7289. st->state = END;
  7290. return SCRIPT_CMD_SUCCESS;
  7291. }
  7292. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7293. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7294. script_pushint(st, -1);
  7295. return SCRIPT_CMD_FAILURE;
  7296. }
  7297. if (loc == TABLE_GUILD_STORAGE) {
  7298. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7299. if (gstor == NULL || sd->state.storage_flag) {
  7300. script_pushint(st, -1);
  7301. return SCRIPT_CMD_FAILURE;
  7302. }
  7303. }
  7304. if( script_isstring(st, 2) )
  7305. {
  7306. const char* item_name = script_getstr(st, 2);
  7307. std::shared_ptr<item_data> id = item_db.searchname(item_name);
  7308. if( id == nullptr ){
  7309. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7310. st->state = END;
  7311. return SCRIPT_CMD_FAILURE;
  7312. }
  7313. it.nameid = id->nameid;// "<item name>"
  7314. }
  7315. else
  7316. {
  7317. it.nameid = script_getnum(st, 2);// <item id>
  7318. if( !itemdb_exists( it.nameid ) )
  7319. {
  7320. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7321. st->state = END;
  7322. return SCRIPT_CMD_FAILURE;
  7323. }
  7324. }
  7325. it.amount=script_getnum(st,3);
  7326. if( it.amount <= 0 )
  7327. return SCRIPT_CMD_SUCCESS;// nothing to do
  7328. if( buildin_delitem_search(sd, &it, 0, loc) )
  7329. {// success
  7330. return SCRIPT_CMD_SUCCESS;
  7331. }
  7332. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7333. st->state = END;
  7334. st->mes_active = 0;
  7335. clif_scriptclose(sd, st->oid);
  7336. return SCRIPT_CMD_FAILURE;
  7337. }
  7338. /// Deletes items from the target/attached player.
  7339. ///
  7340. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7341. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7342. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7343. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7344. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7345. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7346. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7347. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7348. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7349. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7350. BUILDIN_FUNC(delitem2)
  7351. {
  7352. TBL_PC *sd;
  7353. struct item it;
  7354. uint8 loc = 0;
  7355. char* command = (char*)script_getfuncname(st);
  7356. int aid_pos = 11;
  7357. uint8 flag = 0x1;
  7358. if(!strncmp(command, "cart", 4))
  7359. loc = TABLE_CART;
  7360. else if(!strncmp(command, "storage", 7))
  7361. loc = TABLE_STORAGE;
  7362. else if(!strncmp(command, "guildstorage", 12))
  7363. loc = TABLE_GUILD_STORAGE;
  7364. if (command[strlen(command)-1] == '3')
  7365. aid_pos = 14;
  7366. if( !script_accid2sd(aid_pos,sd) ){
  7367. // In any case cancel script execution
  7368. st->state = END;
  7369. return SCRIPT_CMD_SUCCESS;
  7370. }
  7371. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7372. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7373. script_pushint(st,-1);
  7374. return SCRIPT_CMD_FAILURE;
  7375. }
  7376. if (loc == TABLE_GUILD_STORAGE) {
  7377. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7378. if (gstor == NULL || sd->state.storage_flag) {
  7379. script_pushint(st, -1);
  7380. return SCRIPT_CMD_FAILURE;
  7381. }
  7382. }
  7383. memset(&it, 0, sizeof(it));
  7384. if( script_isstring(st, 2) )
  7385. {
  7386. const char* item_name = script_getstr(st, 2);
  7387. std::shared_ptr<item_data> id = item_db.searchname( item_name );
  7388. if( id == nullptr ){
  7389. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7390. st->state = END;
  7391. return SCRIPT_CMD_FAILURE;
  7392. }
  7393. it.nameid = id->nameid;// "<item name>"
  7394. }
  7395. else
  7396. {
  7397. it.nameid = script_getnum(st, 2);// <item id>
  7398. if( !itemdb_exists( it.nameid ) )
  7399. {
  7400. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7401. st->state = END;
  7402. return SCRIPT_CMD_FAILURE;
  7403. }
  7404. }
  7405. it.amount=script_getnum(st,3);
  7406. it.identify=script_getnum(st,4);
  7407. it.refine=script_getnum(st,5);
  7408. it.attribute=script_getnum(st,6);
  7409. it.card[0]=script_getnum(st,7);
  7410. it.card[1]=script_getnum(st,8);
  7411. it.card[2]=script_getnum(st,9);
  7412. it.card[3]=script_getnum(st,10);
  7413. if (command[strlen(command)-1] == '3') {
  7414. int res = script_getitem_randomoption(st, sd, &it, command, 11);
  7415. if (res != SCRIPT_CMD_SUCCESS)
  7416. return SCRIPT_CMD_FAILURE;
  7417. flag |= 0x2;
  7418. }
  7419. if( it.amount <= 0 )
  7420. return SCRIPT_CMD_SUCCESS;// nothing to do
  7421. if( buildin_delitem_search(sd, &it, flag, loc) )
  7422. {// success
  7423. return SCRIPT_CMD_SUCCESS;
  7424. }
  7425. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7426. st->state = END;
  7427. st->mes_active = 0;
  7428. clif_scriptclose(sd, st->oid);
  7429. return SCRIPT_CMD_FAILURE;
  7430. }
  7431. /// Deletes items from the target/attached player at given index.
  7432. /// delitemidx <index>{,<amount>{,<char id>}};
  7433. BUILDIN_FUNC(delitemidx) {
  7434. struct map_session_data* sd;
  7435. if (!script_charid2sd(4, sd)) {
  7436. script_pushint(st, false);
  7437. return SCRIPT_CMD_FAILURE;
  7438. }
  7439. int idx = script_getnum(st, 2);
  7440. if (idx < 0 || idx >= MAX_INVENTORY) {
  7441. ShowWarning("buildin_delitemidx: Index %d is out of the range 0-%d.\n", idx, MAX_INVENTORY - 1);
  7442. script_pushint(st, false);
  7443. return SCRIPT_CMD_FAILURE;
  7444. }
  7445. if (sd->inventory_data[idx] == nullptr) {
  7446. 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);
  7447. script_pushint(st, false);
  7448. return SCRIPT_CMD_FAILURE;
  7449. }
  7450. int amount;
  7451. if (script_hasdata(st, 3))
  7452. amount = script_getnum(st, 3);
  7453. else
  7454. amount = sd->inventory.u.items_inventory[idx].amount;
  7455. if (amount > 0)
  7456. script_pushint(st, pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_SCRIPT) == 0);
  7457. else
  7458. script_pushint(st, false);
  7459. return SCRIPT_CMD_SUCCESS;
  7460. }
  7461. /*==========================================
  7462. * Enables/Disables use of items while in an NPC [Skotlex]
  7463. *------------------------------------------*/
  7464. BUILDIN_FUNC(enableitemuse)
  7465. {
  7466. TBL_PC *sd;
  7467. if (script_rid2sd(sd))
  7468. st->npc_item_flag = sd->npc_item_flag = 1;
  7469. return SCRIPT_CMD_SUCCESS;
  7470. }
  7471. BUILDIN_FUNC(disableitemuse)
  7472. {
  7473. TBL_PC *sd;
  7474. if (script_rid2sd(sd))
  7475. st->npc_item_flag = sd->npc_item_flag = 0;
  7476. return SCRIPT_CMD_SUCCESS;
  7477. }
  7478. /*==========================================
  7479. * Returns a character's specified stat.
  7480. * Check pc_readparam for available options.
  7481. * readparam <param>{,"<nick>"}
  7482. * readparam <param>{,<char_id>}
  7483. *------------------------------------------*/
  7484. BUILDIN_FUNC(readparam)
  7485. {
  7486. int value;
  7487. struct script_data *data = script_getdata(st, 2);
  7488. TBL_PC *sd = NULL;
  7489. if( script_hasdata(st, 3) ){
  7490. if (script_isint(st, 3))
  7491. script_charid2sd(3, sd);
  7492. else
  7493. script_nick2sd(3, sd);
  7494. }else{
  7495. script_rid2sd(sd);
  7496. }
  7497. if( !sd ){
  7498. script_pushint(st, -1);
  7499. return SCRIPT_CMD_FAILURE;
  7500. }
  7501. // If you use a parameter, return the value behind it
  7502. if( reference_toparam(data) ){
  7503. get_val_(st, data, sd);
  7504. value = (int)data->u.num;
  7505. }else{
  7506. value = static_cast<int>(pc_readparam(sd,script_getnum(st, 2)));
  7507. }
  7508. script_pushint(st,value);
  7509. return SCRIPT_CMD_SUCCESS;
  7510. }
  7511. /*==========================================
  7512. * Return charid identification
  7513. * return by @num :
  7514. * 0 : char_id
  7515. * 1 : party_id
  7516. * 2 : guild_id
  7517. * 3 : account_id
  7518. * 4 : bg_id
  7519. * 5 : clan_id
  7520. *------------------------------------------*/
  7521. BUILDIN_FUNC(getcharid)
  7522. {
  7523. int num;
  7524. TBL_PC *sd;
  7525. num = script_getnum(st,2);
  7526. if( !script_nick2sd(3,sd) ){
  7527. script_pushint(st,0); //return 0, according docs
  7528. return SCRIPT_CMD_SUCCESS;
  7529. }
  7530. switch( num ) {
  7531. case 0: script_pushint(st,sd->status.char_id); break;
  7532. case 1: script_pushint(st,sd->status.party_id); break;
  7533. case 2: script_pushint(st,sd->status.guild_id); break;
  7534. case 3: script_pushint(st,sd->status.account_id); break;
  7535. case 4: script_pushint(st,sd->bg_id); break;
  7536. case 5: script_pushint(st,sd->status.clan_id); break;
  7537. default:
  7538. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7539. script_pushint(st,0);
  7540. break;
  7541. }
  7542. return SCRIPT_CMD_SUCCESS;
  7543. }
  7544. /*==========================================
  7545. * returns the GID of an NPC
  7546. *------------------------------------------*/
  7547. BUILDIN_FUNC(getnpcid)
  7548. {
  7549. int num = script_getnum(st,2);
  7550. struct npc_data* nd = NULL;
  7551. if( script_hasdata(st,3) )
  7552. {// unique npc name
  7553. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7554. {
  7555. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7556. script_pushint(st,0);
  7557. return SCRIPT_CMD_FAILURE;
  7558. }
  7559. }
  7560. switch (num) {
  7561. case 0:
  7562. script_pushint(st,nd ? nd->bl.id : st->oid);
  7563. break;
  7564. default:
  7565. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7566. script_pushint(st,0);
  7567. return SCRIPT_CMD_FAILURE;
  7568. }
  7569. return SCRIPT_CMD_SUCCESS;
  7570. }
  7571. /*==========================================
  7572. * Return the name of the party_id
  7573. * null if not found
  7574. *------------------------------------------*/
  7575. BUILDIN_FUNC(getpartyname)
  7576. {
  7577. int party_id;
  7578. struct party_data* p;
  7579. party_id = script_getnum(st,2);
  7580. if( ( p = party_search(party_id) ) != NULL )
  7581. {
  7582. script_pushstrcopy(st,p->party.name);
  7583. }
  7584. else
  7585. {
  7586. script_pushconststr(st,"null");
  7587. }
  7588. return SCRIPT_CMD_SUCCESS;
  7589. }
  7590. /*==========================================
  7591. * Get the information of the members of a party by type
  7592. * @party_id, @type
  7593. * return by @type :
  7594. * - : nom des membres
  7595. * 1 : char_id des membres
  7596. * 2 : account_id des membres
  7597. *------------------------------------------*/
  7598. BUILDIN_FUNC(getpartymember)
  7599. {
  7600. struct party_data *p;
  7601. uint8 j = 0;
  7602. p = party_search(script_getnum(st,2));
  7603. if (p != NULL) {
  7604. uint8 i, type = 0;
  7605. struct script_data *data = NULL;
  7606. char *varname = NULL;
  7607. if (script_hasdata(st,3))
  7608. type = script_getnum(st,3);
  7609. if (script_hasdata(st,4)) {
  7610. data = script_getdata(st, 4);
  7611. if (!data_isreference(data)) {
  7612. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7613. return SCRIPT_CMD_FAILURE;
  7614. }
  7615. varname = reference_getname(data);
  7616. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7617. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7618. return SCRIPT_CMD_FAILURE;
  7619. }
  7620. if (not_server_variable(*varname)) {
  7621. struct map_session_data *sd;
  7622. if (!script_rid2sd(sd)) {
  7623. ShowError("buildin_getpartymember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  7624. return SCRIPT_CMD_FAILURE;
  7625. }
  7626. }
  7627. }
  7628. for (i = 0; i < MAX_PARTY; i++) {
  7629. if (p->party.member[i].account_id) {
  7630. switch (type) {
  7631. case 2:
  7632. if (data)
  7633. setd_sub_num( st, NULL, varname, j, p->party.member[i].account_id, data->ref );
  7634. else
  7635. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7636. break;
  7637. case 1:
  7638. if (data)
  7639. setd_sub_num( st, NULL, varname, j, p->party.member[i].char_id, data->ref );
  7640. else
  7641. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7642. break;
  7643. default:
  7644. if (data)
  7645. setd_sub_str( st, NULL, varname, j, p->party.member[i].name, data->ref );
  7646. else
  7647. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7648. break;
  7649. }
  7650. j++;
  7651. }
  7652. }
  7653. }
  7654. mapreg_setreg(add_str("$@partymembercount"),j);
  7655. script_pushint(st, j);
  7656. return SCRIPT_CMD_SUCCESS;
  7657. }
  7658. /*==========================================
  7659. * Retrieves party leader. if flag is specified,
  7660. * return some of the leader data. Otherwise, return name.
  7661. *------------------------------------------*/
  7662. BUILDIN_FUNC(getpartyleader)
  7663. {
  7664. int party_id, type = 0, i=0;
  7665. struct party_data *p;
  7666. party_id=script_getnum(st,2);
  7667. if( script_hasdata(st,3) )
  7668. type=script_getnum(st,3);
  7669. p=party_search(party_id);
  7670. if (p) //Search leader
  7671. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7672. if (!p || i == MAX_PARTY) { //leader not found
  7673. if (type)
  7674. script_pushint(st,-1);
  7675. else
  7676. script_pushconststr(st,"null");
  7677. return SCRIPT_CMD_SUCCESS;
  7678. }
  7679. switch (type) {
  7680. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7681. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7682. case 3: script_pushint(st,p->party.member[i].class_); break;
  7683. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7684. case 5: script_pushint(st,p->party.member[i].lv); break;
  7685. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7686. }
  7687. return SCRIPT_CMD_SUCCESS;
  7688. }
  7689. /*==========================================
  7690. * Return the name of the @guild_id
  7691. * null if not found
  7692. *------------------------------------------*/
  7693. BUILDIN_FUNC(getguildname)
  7694. {
  7695. int guild_id;
  7696. struct guild* g;
  7697. guild_id = script_getnum(st,2);
  7698. if( ( g = guild_search(guild_id) ) != NULL )
  7699. script_pushstrcopy(st,g->name);
  7700. else
  7701. script_pushconststr(st,"null");
  7702. return SCRIPT_CMD_SUCCESS;
  7703. }
  7704. /*==========================================
  7705. * Return the name of the guild master of @guild_id
  7706. * null if not found
  7707. *------------------------------------------*/
  7708. BUILDIN_FUNC(getguildmaster)
  7709. {
  7710. int guild_id;
  7711. struct guild* g;
  7712. guild_id = script_getnum(st,2);
  7713. if( ( g = guild_search(guild_id) ) != NULL )
  7714. script_pushstrcopy(st,g->member[0].name);
  7715. else
  7716. script_pushconststr(st,"null");
  7717. return SCRIPT_CMD_SUCCESS;
  7718. }
  7719. BUILDIN_FUNC(getguildmasterid)
  7720. {
  7721. int guild_id;
  7722. struct guild* g;
  7723. guild_id = script_getnum(st,2);
  7724. if( ( g = guild_search(guild_id) ) != NULL )
  7725. script_pushint(st,g->member[0].char_id);
  7726. else
  7727. script_pushint(st,0);
  7728. return SCRIPT_CMD_SUCCESS;
  7729. }
  7730. /*==========================================
  7731. * Get char string information by type :
  7732. * Return by @type :
  7733. * 0 : char_name
  7734. * 1 : party_name or ""
  7735. * 2 : guild_name or ""
  7736. * 3 : map_name
  7737. * - : ""
  7738. * strcharinfo(<type>{,<char_id>})
  7739. *------------------------------------------*/
  7740. BUILDIN_FUNC(strcharinfo)
  7741. {
  7742. TBL_PC *sd;
  7743. int num;
  7744. struct guild* g;
  7745. struct party_data* p;
  7746. if (!script_charid2sd(3,sd)) {
  7747. script_pushconststr(st,"");
  7748. return SCRIPT_CMD_FAILURE;
  7749. }
  7750. num=script_getnum(st,2);
  7751. switch(num){
  7752. case 0:
  7753. script_pushstrcopy(st,sd->status.name);
  7754. break;
  7755. case 1:
  7756. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7757. script_pushstrcopy(st,p->party.name);
  7758. } else {
  7759. script_pushconststr(st,"");
  7760. }
  7761. break;
  7762. case 2:
  7763. if( ( g = sd->guild ) != NULL ) {
  7764. script_pushstrcopy(st,g->name);
  7765. } else {
  7766. script_pushconststr(st,"");
  7767. }
  7768. break;
  7769. case 3:
  7770. script_pushconststr(st,map_getmapdata(sd->bl.m)->name);
  7771. break;
  7772. default:
  7773. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7774. script_pushconststr(st,"");
  7775. break;
  7776. }
  7777. return SCRIPT_CMD_SUCCESS;
  7778. }
  7779. /*==========================================
  7780. * Get npc string information by type
  7781. * return by @type:
  7782. * 0 : name
  7783. * 1 : str#
  7784. * 2 : #str
  7785. * 3 : ::str
  7786. * 4 : map name
  7787. *------------------------------------------*/
  7788. BUILDIN_FUNC(strnpcinfo)
  7789. {
  7790. TBL_NPC* nd;
  7791. int num;
  7792. char *buf,*name=NULL;
  7793. nd = map_id2nd(st->oid);
  7794. if (!nd) {
  7795. script_pushconststr(st, "");
  7796. return SCRIPT_CMD_SUCCESS;
  7797. }
  7798. num = script_getnum(st,2);
  7799. switch(num){
  7800. case 0: // display name
  7801. name = aStrdup(nd->name);
  7802. break;
  7803. case 1: // visible part of display name
  7804. if((buf = strchr(nd->name,'#')) != NULL)
  7805. {
  7806. name = aStrdup(nd->name);
  7807. name[buf - nd->name] = 0;
  7808. } else // Return the name, there is no '#' present
  7809. name = aStrdup(nd->name);
  7810. break;
  7811. case 2: // # fragment
  7812. if((buf = strchr(nd->name,'#')) != NULL)
  7813. name = aStrdup(buf+1);
  7814. break;
  7815. case 3: // unique name
  7816. name = aStrdup(nd->exname);
  7817. break;
  7818. case 4: // map name
  7819. if (nd->bl.m >= 0)
  7820. name = aStrdup(map_getmapdata(nd->bl.m)->name);
  7821. break;
  7822. }
  7823. if(name)
  7824. script_pushstr(st, name);
  7825. else
  7826. script_pushconststr(st, "");
  7827. return SCRIPT_CMD_SUCCESS;
  7828. }
  7829. /**
  7830. * getequipid({<equipment slot>,<char_id>})
  7831. **/
  7832. BUILDIN_FUNC(getequipid)
  7833. {
  7834. int i, num;
  7835. TBL_PC* sd;
  7836. if (!script_charid2sd(3, sd)) {
  7837. script_pushint(st,-1);
  7838. return SCRIPT_CMD_FAILURE;
  7839. }
  7840. if (script_hasdata(st, 2))
  7841. num = script_getnum(st, 2);
  7842. else
  7843. num = EQI_COMPOUND_ON;
  7844. if (num == EQI_COMPOUND_ON)
  7845. i = current_equip_item_index;
  7846. else if (equip_index_check(num)) // get inventory position of item
  7847. i = pc_checkequip(sd, equip_bitmask[num]);
  7848. else {
  7849. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7850. script_pushint(st,-1);
  7851. return SCRIPT_CMD_FAILURE;
  7852. }
  7853. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7854. script_pushint(st, sd->inventory_data[i]->nameid);
  7855. else
  7856. script_pushint(st, -1);
  7857. return SCRIPT_CMD_SUCCESS;
  7858. }
  7859. /*==========================================
  7860. * getequipuniqueid(<equipment slot>{,<char_id>})
  7861. *------------------------------------------*/
  7862. BUILDIN_FUNC(getequipuniqueid)
  7863. {
  7864. int i, num;
  7865. TBL_PC* sd;
  7866. struct item* item;
  7867. if (!script_charid2sd(3, sd)) {
  7868. script_pushconststr(st, "");
  7869. return SCRIPT_CMD_FAILURE;
  7870. }
  7871. num = script_getnum(st,2);
  7872. if ( !equip_index_check(num) ) {
  7873. script_pushconststr(st, "");
  7874. return SCRIPT_CMD_FAILURE;
  7875. }
  7876. // get inventory position of item
  7877. i = pc_checkequip(sd,equip_bitmask[num]);
  7878. if (i < 0) {
  7879. script_pushconststr(st, "");
  7880. return SCRIPT_CMD_FAILURE;
  7881. }
  7882. item = &sd->inventory.u.items_inventory[i];
  7883. if (item != 0) {
  7884. int maxlen = 256;
  7885. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7886. memset(buf, 0, maxlen);
  7887. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7888. script_pushstr(st, buf);
  7889. } else
  7890. script_pushconststr(st, "");
  7891. return SCRIPT_CMD_SUCCESS;
  7892. }
  7893. /*==========================================
  7894. * Get the equipement name at pos
  7895. * return item jname or ""
  7896. * getequipname(<equipment slot>{,<char_id>})
  7897. *------------------------------------------*/
  7898. BUILDIN_FUNC(getequipname)
  7899. {
  7900. int i, num;
  7901. TBL_PC* sd;
  7902. struct item_data* item;
  7903. if (!script_charid2sd(3, sd)) {
  7904. script_pushconststr(st, "");
  7905. return SCRIPT_CMD_FAILURE;
  7906. }
  7907. num = script_getnum(st,2);
  7908. if( !equip_index_check(num) )
  7909. {
  7910. script_pushconststr(st,"");
  7911. return SCRIPT_CMD_SUCCESS;
  7912. }
  7913. // get inventory position of item
  7914. i = pc_checkequip(sd,equip_bitmask[num]);
  7915. if( i < 0 )
  7916. {
  7917. script_pushconststr(st,"");
  7918. return SCRIPT_CMD_SUCCESS;
  7919. }
  7920. item = sd->inventory_data[i];
  7921. if( item != 0 )
  7922. script_pushstrcopy(st,item->ename.c_str());
  7923. else
  7924. script_pushconststr(st,"");
  7925. return SCRIPT_CMD_SUCCESS;
  7926. }
  7927. /*==========================================
  7928. * getbrokenid [Valaris]
  7929. * getbrokenid(<number>{,<char_id>})
  7930. *------------------------------------------*/
  7931. BUILDIN_FUNC(getbrokenid)
  7932. {
  7933. int i,num,id = 0,brokencounter = 0;
  7934. TBL_PC *sd;
  7935. if (!script_charid2sd(3, sd)) {
  7936. script_pushint(st,0);
  7937. return SCRIPT_CMD_FAILURE;
  7938. }
  7939. num = script_getnum(st,2);
  7940. for(i = 0; i < MAX_INVENTORY; i++) {
  7941. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  7942. brokencounter++;
  7943. if(num == brokencounter){
  7944. id = sd->inventory.u.items_inventory[i].nameid;
  7945. break;
  7946. }
  7947. }
  7948. }
  7949. script_pushint(st,id);
  7950. return SCRIPT_CMD_SUCCESS;
  7951. }
  7952. /*==========================================
  7953. * repair [Valaris]
  7954. * repair <num>{,<char_id>};
  7955. *------------------------------------------*/
  7956. BUILDIN_FUNC(repair)
  7957. {
  7958. int i,num;
  7959. int repaircounter = 0;
  7960. TBL_PC *sd;
  7961. if (!script_charid2sd(3,sd))
  7962. return SCRIPT_CMD_FAILURE;
  7963. num = script_getnum(st,2);
  7964. for(i = 0; i < MAX_INVENTORY; i++) {
  7965. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  7966. repaircounter++;
  7967. if(num == repaircounter) {
  7968. sd->inventory.u.items_inventory[i].attribute = 0;
  7969. clif_equiplist(sd);
  7970. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7971. clif_misceffect(&sd->bl, 3);
  7972. break;
  7973. }
  7974. }
  7975. }
  7976. return SCRIPT_CMD_SUCCESS;
  7977. }
  7978. /*==========================================
  7979. * repairall {<char_id>}
  7980. *------------------------------------------*/
  7981. BUILDIN_FUNC(repairall)
  7982. {
  7983. int i, repaircounter = 0;
  7984. TBL_PC *sd;
  7985. if (!script_charid2sd(2,sd))
  7986. return SCRIPT_CMD_FAILURE;
  7987. for(i = 0; i < MAX_INVENTORY; i++)
  7988. {
  7989. 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] ) ){
  7990. sd->inventory.u.items_inventory[i].attribute = 0;
  7991. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7992. repaircounter++;
  7993. }
  7994. }
  7995. if(repaircounter)
  7996. {
  7997. clif_misceffect(&sd->bl, 3);
  7998. clif_equiplist(sd);
  7999. }
  8000. return SCRIPT_CMD_SUCCESS;
  8001. }
  8002. /*==========================================
  8003. * Chk if player have something equiped at pos
  8004. * getequipisequiped <pos>{,<char_id>}
  8005. *------------------------------------------*/
  8006. BUILDIN_FUNC(getequipisequiped)
  8007. {
  8008. int i = -1,num;
  8009. TBL_PC *sd;
  8010. num = script_getnum(st,2);
  8011. if (!script_charid2sd(3,sd))
  8012. return SCRIPT_CMD_FAILURE;
  8013. if ( equip_index_check(num) )
  8014. i=pc_checkequip(sd,equip_bitmask[num]);
  8015. if(i >= 0)
  8016. script_pushint(st,1);
  8017. else
  8018. script_pushint(st,0);
  8019. return SCRIPT_CMD_SUCCESS;
  8020. }
  8021. /*==========================================
  8022. * Chk if the player have something equiped at pos
  8023. * if so chk if this item ain't marked not refinable or rental
  8024. * return (npc)
  8025. * 1 : true
  8026. * 0 : false
  8027. * getequipisenableref(<equipment slot>{,<char_id>})
  8028. *------------------------------------------*/
  8029. BUILDIN_FUNC(getequipisenableref)
  8030. {
  8031. int i = -1,num;
  8032. TBL_PC *sd;
  8033. num = script_getnum(st,2);
  8034. if (!script_charid2sd(3, sd)) {
  8035. script_pushint(st,0);
  8036. return SCRIPT_CMD_FAILURE;
  8037. }
  8038. 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]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  8041. script_pushint(st,1);
  8042. else
  8043. script_pushint(st,0);
  8044. return SCRIPT_CMD_SUCCESS;
  8045. }
  8046. /*==========================================
  8047. * Get the item refined value at pos
  8048. * return (npc)
  8049. * x : refine amount
  8050. * 0 : false (not refined)
  8051. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  8052. *------------------------------------------*/
  8053. BUILDIN_FUNC(getequiprefinerycnt)
  8054. {
  8055. int i = -1,num;
  8056. TBL_PC *sd;
  8057. num = script_getnum(st,2);
  8058. if (!script_charid2sd(3, sd)) {
  8059. script_pushint(st,0);
  8060. return SCRIPT_CMD_FAILURE;
  8061. }
  8062. if (equip_index_check(num))
  8063. i=pc_checkequip(sd,equip_bitmask[num]);
  8064. if(i >= 0)
  8065. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  8066. else
  8067. script_pushint(st,0);
  8068. return SCRIPT_CMD_SUCCESS;
  8069. }
  8070. /*==========================================
  8071. * Get the weapon level value at pos
  8072. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  8073. * return (npc)
  8074. * x : weapon level
  8075. * 0 : false
  8076. * getequipweaponlv({<equipment slot>{,<char_id>}})
  8077. *------------------------------------------*/
  8078. BUILDIN_FUNC(getequipweaponlv)
  8079. {
  8080. int num;
  8081. if( script_hasdata( st, 2 ) ){
  8082. num = script_getnum(st, 2);
  8083. }else{
  8084. num = EQI_COMPOUND_ON;
  8085. }
  8086. struct map_session_data* sd;
  8087. if (!script_charid2sd(3, sd)) {
  8088. script_pushint(st, 0);
  8089. return SCRIPT_CMD_FAILURE;
  8090. }
  8091. int i = -1;
  8092. if( num == EQI_COMPOUND_ON ){
  8093. if( current_equip_item_index == -1 ){
  8094. script_pushint(st, 0);
  8095. return SCRIPT_CMD_FAILURE;
  8096. }
  8097. i = current_equip_item_index;
  8098. }else if (equip_index_check(num))
  8099. i = pc_checkequip(sd, equip_bitmask[num]);
  8100. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON ){
  8101. script_pushint( st, sd->inventory_data[i]->weapon_level );
  8102. }else{
  8103. script_pushint( st, 0 );
  8104. }
  8105. return SCRIPT_CMD_SUCCESS;
  8106. }
  8107. BUILDIN_FUNC(getequiparmorlv){
  8108. int num;
  8109. if( script_hasdata( st, 2 ) ){
  8110. num = script_getnum(st, 2);
  8111. }else{
  8112. num = EQI_COMPOUND_ON;
  8113. }
  8114. struct map_session_data* sd;
  8115. if( !script_charid2sd( 3, sd ) ){
  8116. script_pushint( st, 0 );
  8117. return SCRIPT_CMD_FAILURE;
  8118. }
  8119. int i = -1;
  8120. if( num == EQI_COMPOUND_ON ){
  8121. if( current_equip_item_index == -1 ){
  8122. script_pushint( st, 0 );
  8123. return SCRIPT_CMD_FAILURE;
  8124. }
  8125. i = current_equip_item_index;
  8126. }else if( equip_index_check( num ) ){
  8127. i = pc_checkequip( sd, equip_bitmask[num] );
  8128. }else{
  8129. ShowError( "buildin_getequiparmorlv: unsupported equip index %d.\n", num );
  8130. script_pushint( st, 0 );
  8131. return SCRIPT_CMD_FAILURE;
  8132. }
  8133. if( i >= 0 && sd->inventory_data[i] && sd->inventory_data[i]->type == IT_ARMOR ){
  8134. script_pushint( st, sd->inventory_data[i]->armor_level );
  8135. }else{
  8136. script_pushint( st, 0 );
  8137. }
  8138. return SCRIPT_CMD_SUCCESS;
  8139. }
  8140. /*==========================================
  8141. * Get the item refine chance (from refine.txt) for item at pos
  8142. * return (npc)
  8143. * x : refine chance
  8144. * 0 : false (max refine level or unequip..)
  8145. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8146. *------------------------------------------*/
  8147. BUILDIN_FUNC(getequippercentrefinery)
  8148. {
  8149. int i = -1,num;
  8150. bool enriched = false;
  8151. TBL_PC *sd;
  8152. num = script_getnum(st,2);
  8153. if (script_hasdata(st, 3))
  8154. enriched = script_getnum(st, 3) != 0;
  8155. if (!script_charid2sd(4, sd)) {
  8156. script_pushint(st,0);
  8157. return SCRIPT_CMD_FAILURE;
  8158. }
  8159. if (equip_index_check(num))
  8160. i = pc_checkequip(sd,equip_bitmask[num]);
  8161. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid) {
  8162. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  8163. if( info == nullptr ){
  8164. script_pushint( st, 0 );
  8165. return SCRIPT_CMD_SUCCESS;
  8166. }
  8167. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, (uint16)( enriched ? REFINE_COST_ENRICHED : REFINE_COST_NORMAL ) );
  8168. if( cost == nullptr ){
  8169. script_pushint( st, 0 );
  8170. return SCRIPT_CMD_SUCCESS;
  8171. }
  8172. script_pushint( st, cost->chance / 100 );
  8173. }
  8174. else
  8175. script_pushint(st,0);
  8176. return SCRIPT_CMD_SUCCESS;
  8177. }
  8178. /*==========================================
  8179. * Refine +1 item at pos and log and display refine
  8180. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8181. *------------------------------------------*/
  8182. BUILDIN_FUNC(successrefitem) {
  8183. short i = -1, up = 1;
  8184. int pos;
  8185. TBL_PC *sd;
  8186. pos = script_getnum(st,2);
  8187. if (!script_charid2sd(4, sd)) {
  8188. script_pushint(st, -1);
  8189. return SCRIPT_CMD_FAILURE;
  8190. }
  8191. if (script_hasdata(st, 3))
  8192. up = script_getnum(st, 3);
  8193. if (equip_index_check(pos))
  8194. i = pc_checkequip(sd,equip_bitmask[pos]);
  8195. if (i >= 0) {
  8196. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8197. //Logs items, got from (N)PC scripts [Lupus]
  8198. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8199. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8200. script_pushint(st, MAX_REFINE);
  8201. return SCRIPT_CMD_SUCCESS;
  8202. }
  8203. sd->inventory.u.items_inventory[i].refine += up;
  8204. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8205. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8206. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8207. clif_delitem(sd,i,1,3);
  8208. //Logs items, got from (N)PC scripts [Lupus]
  8209. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8210. clif_additem(sd,i,1,0);
  8211. pc_equipitem(sd,i,ep);
  8212. clif_misceffect(&sd->bl,3);
  8213. if( sd->inventory_data[i]->type == IT_WEAPON ){
  8214. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->weapon_level, sd->inventory.u.items_inventory[i].refine);
  8215. }
  8216. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8217. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8218. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]) &&
  8219. sd->inventory_data[i]->type == IT_WEAPON )
  8220. { // Fame point system [DracoRPG]
  8221. switch( sd->inventory_data[i]->weapon_level ){
  8222. case 1:
  8223. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8224. break;
  8225. case 2:
  8226. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8227. break;
  8228. case 3:
  8229. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8230. break;
  8231. }
  8232. }
  8233. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8234. return SCRIPT_CMD_SUCCESS;
  8235. }
  8236. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8237. script_pushint(st, -1);
  8238. return SCRIPT_CMD_FAILURE;
  8239. }
  8240. /*==========================================
  8241. * Show a failed Refine +1 attempt
  8242. * failedrefitem <equipment slot>{,<char_id>}
  8243. *------------------------------------------*/
  8244. BUILDIN_FUNC(failedrefitem) {
  8245. short i = -1;
  8246. int pos;
  8247. TBL_PC *sd;
  8248. pos = script_getnum(st,2);
  8249. if (!script_charid2sd(3, sd)) {
  8250. script_pushint(st, 0);
  8251. return SCRIPT_CMD_FAILURE;
  8252. }
  8253. if (equip_index_check(pos))
  8254. i = pc_checkequip(sd,equip_bitmask[pos]);
  8255. if (i >= 0) {
  8256. sd->inventory.u.items_inventory[i].refine = 0;
  8257. pc_unequipitem(sd,i,3); //recalculate bonus
  8258. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8259. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8260. clif_misceffect(&sd->bl,2); // display failure effect
  8261. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8262. script_pushint(st, 1);
  8263. return SCRIPT_CMD_SUCCESS;
  8264. }
  8265. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8266. script_pushint(st, 0);
  8267. return SCRIPT_CMD_FAILURE;
  8268. }
  8269. /*==========================================
  8270. * Downgrades an Equipment Part by -1 . [Masao]
  8271. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8272. *------------------------------------------*/
  8273. BUILDIN_FUNC(downrefitem) {
  8274. short i = -1, down = 1;
  8275. int pos;
  8276. TBL_PC *sd;
  8277. if (!script_charid2sd(4, sd)) {
  8278. script_pushint(st, -1);
  8279. return SCRIPT_CMD_FAILURE;
  8280. }
  8281. pos = script_getnum(st,2);
  8282. if (script_hasdata(st, 3))
  8283. down = script_getnum(st, 3);
  8284. if (equip_index_check(pos))
  8285. i = pc_checkequip(sd,equip_bitmask[pos]);
  8286. if (i >= 0) {
  8287. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8288. //Logs items, got from (N)PC scripts [Lupus]
  8289. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8290. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8291. sd->inventory.u.items_inventory[i].refine -= down;
  8292. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8293. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8294. clif_delitem(sd,i,1,3);
  8295. //Logs items, got from (N)PC scripts [Lupus]
  8296. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8297. clif_additem(sd,i,1,0);
  8298. pc_equipitem(sd,i,ep);
  8299. clif_misceffect(&sd->bl,2);
  8300. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8301. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8302. return SCRIPT_CMD_SUCCESS;
  8303. }
  8304. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8305. script_pushint(st, -1);
  8306. return SCRIPT_CMD_FAILURE;
  8307. }
  8308. /**
  8309. * Delete the item equipped at pos.
  8310. * delequip <equipment slot>{,<char_id>};
  8311. **/
  8312. BUILDIN_FUNC(delequip) {
  8313. short i = -1;
  8314. int pos;
  8315. int8 ret;
  8316. TBL_PC *sd;
  8317. pos = script_getnum(st,2);
  8318. if (!script_charid2sd(3,sd)) {
  8319. st->state = END;
  8320. return SCRIPT_CMD_FAILURE;
  8321. }
  8322. if (equip_index_check(pos))
  8323. i = pc_checkequip(sd,equip_bitmask[pos]);
  8324. if (i >= 0) {
  8325. pc_unequipitem(sd,i,3); //recalculate bonus
  8326. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8327. }
  8328. else {
  8329. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8330. st->state = END;
  8331. return SCRIPT_CMD_FAILURE;
  8332. }
  8333. script_pushint(st,ret);
  8334. return SCRIPT_CMD_SUCCESS;
  8335. }
  8336. /**
  8337. * Break the item equipped at pos.
  8338. * breakequip <equipment slot>{,<char_id>};
  8339. **/
  8340. BUILDIN_FUNC(breakequip) {
  8341. short i = -1;
  8342. int pos;
  8343. TBL_PC *sd;
  8344. pos = script_getnum(st,2);
  8345. if (!script_charid2sd(3,sd))
  8346. return SCRIPT_CMD_FAILURE;
  8347. if (equip_index_check(pos))
  8348. i = pc_checkequip(sd,equip_bitmask[pos]);
  8349. if (i >= 0) {
  8350. sd->inventory.u.items_inventory[i].attribute = 1;
  8351. pc_unequipitem(sd,i,3);
  8352. clif_equiplist(sd);
  8353. script_pushint(st,1);
  8354. return SCRIPT_CMD_SUCCESS;
  8355. }
  8356. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8357. script_pushint(st,0);
  8358. return SCRIPT_CMD_FAILURE;
  8359. }
  8360. /**
  8361. * statusup <stat>{,<char_id>};
  8362. **/
  8363. BUILDIN_FUNC(statusup)
  8364. {
  8365. int type;
  8366. TBL_PC *sd;
  8367. type = script_getnum(st,2);
  8368. if (!script_charid2sd(3, sd))
  8369. return SCRIPT_CMD_FAILURE;
  8370. pc_statusup(sd, type, 1);
  8371. return SCRIPT_CMD_SUCCESS;
  8372. }
  8373. /**
  8374. * statusup2 <stat>,<amount>{,<char_id>};
  8375. **/
  8376. BUILDIN_FUNC(statusup2)
  8377. {
  8378. int type,val;
  8379. TBL_PC *sd;
  8380. type=script_getnum(st,2);
  8381. val=script_getnum(st,3);
  8382. if (!script_charid2sd(4, sd))
  8383. return SCRIPT_CMD_FAILURE;
  8384. pc_statusup2(sd,type,val);
  8385. return SCRIPT_CMD_SUCCESS;
  8386. }
  8387. /**
  8388. * traitstatusup <stat>{,<char_id>};
  8389. **/
  8390. BUILDIN_FUNC(traitstatusup)
  8391. {
  8392. struct map_session_data *sd;
  8393. if (!script_charid2sd(3, sd))
  8394. return SCRIPT_CMD_FAILURE;
  8395. int type = script_getnum( st, 2 );
  8396. if( type < SP_POW || type > SP_CRT ){
  8397. ShowError( "buildin_traitstatusup: Unknown trait type %d\n", type );
  8398. return SCRIPT_CMD_FAILURE;
  8399. }
  8400. pc_traitstatusup( sd, type, 1 );
  8401. return SCRIPT_CMD_SUCCESS;
  8402. }
  8403. /**
  8404. * traitstatusup2 <stat>,<amount>{,<char_id>};
  8405. **/
  8406. BUILDIN_FUNC(traitstatusup2)
  8407. {
  8408. struct map_session_data *sd;
  8409. if (!script_charid2sd(4, sd))
  8410. return SCRIPT_CMD_FAILURE;
  8411. int type = script_getnum( st, 2 );
  8412. if( type < SP_POW || type > SP_CRT ){
  8413. ShowError( "buildin_traitstatusup2: Unknown trait type %d\n", type );
  8414. return SCRIPT_CMD_FAILURE;
  8415. }
  8416. pc_traitstatusup2( sd, type, script_getnum( st, 3 ) );
  8417. return SCRIPT_CMD_SUCCESS;
  8418. }
  8419. /// See 'doc/item_bonus.txt'
  8420. ///
  8421. /// bonus <bonus type>,<val1>;
  8422. /// bonus2 <bonus type>,<val1>,<val2>;
  8423. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8424. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8425. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8426. BUILDIN_FUNC(bonus)
  8427. {
  8428. int type;
  8429. int val1 = 0;
  8430. int val2 = 0;
  8431. int val3 = 0;
  8432. int val4 = 0;
  8433. int val5 = 0;
  8434. TBL_PC* sd;
  8435. if( !script_rid2sd(sd) )
  8436. return SCRIPT_CMD_SUCCESS; // no player attached
  8437. type = script_getnum(st,2);
  8438. switch( type ) {
  8439. case SP_AUTOSPELL:
  8440. case SP_AUTOSPELL_WHENHIT:
  8441. case SP_AUTOSPELL_ONSKILL:
  8442. case SP_SKILL_ATK:
  8443. case SP_SKILL_HEAL:
  8444. case SP_SKILL_HEAL2:
  8445. case SP_ADD_SKILL_BLOW:
  8446. case SP_CASTRATE:
  8447. case SP_ADDEFF_ONSKILL:
  8448. case SP_SKILL_USE_SP_RATE:
  8449. case SP_SKILL_COOLDOWN:
  8450. case SP_SKILL_FIXEDCAST:
  8451. case SP_SKILL_VARIABLECAST:
  8452. case SP_VARCASTRATE:
  8453. case SP_FIXCASTRATE:
  8454. case SP_SKILL_DELAY:
  8455. case SP_SKILL_USE_SP:
  8456. case SP_SUB_SKILL:
  8457. // these bonuses support skill names
  8458. if (script_isstring(st, 3)) {
  8459. const char *name = script_getstr(st, 3);
  8460. if (!(val1 = skill_name2id(name))) {
  8461. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8462. return SCRIPT_CMD_FAILURE;
  8463. }
  8464. } else {
  8465. val1 = script_getnum(st, 3);
  8466. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8467. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8468. return SCRIPT_CMD_FAILURE;
  8469. }
  8470. }
  8471. break;
  8472. default:
  8473. if (script_hasdata(st, 3))
  8474. val1 = script_getnum(st, 3);
  8475. break;
  8476. }
  8477. switch( script_lastdata(st)-2 ) {
  8478. case 0:
  8479. case 1:
  8480. pc_bonus(sd, type, val1);
  8481. break;
  8482. case 2:
  8483. val2 = script_getnum(st,4);
  8484. pc_bonus2(sd, type, val1, val2);
  8485. break;
  8486. case 3:
  8487. val2 = script_getnum(st,4);
  8488. val3 = script_getnum(st,5);
  8489. pc_bonus3(sd, type, val1, val2, val3);
  8490. break;
  8491. case 4:
  8492. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8493. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8494. else
  8495. val2 = script_getnum(st,4);
  8496. val3 = script_getnum(st,5);
  8497. val4 = script_getnum(st,6);
  8498. pc_bonus4(sd, type, val1, val2, val3, val4);
  8499. break;
  8500. case 5:
  8501. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8502. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8503. else
  8504. val2 = script_getnum(st,4);
  8505. val3 = script_getnum(st,5);
  8506. val4 = script_getnum(st,6);
  8507. val5 = script_getnum(st,7);
  8508. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8509. break;
  8510. default:
  8511. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8512. break;
  8513. }
  8514. return SCRIPT_CMD_SUCCESS;
  8515. }
  8516. BUILDIN_FUNC(autobonus)
  8517. {
  8518. unsigned int dur, pos;
  8519. short rate;
  8520. uint16 atk_type = 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. bonus_script = script_getstr(st,2);
  8532. if( !rate || !dur || !pos || !bonus_script )
  8533. return SCRIPT_CMD_SUCCESS;
  8534. if( script_hasdata(st,5) )
  8535. atk_type = script_getnum(st,5);
  8536. if( script_hasdata(st,6) )
  8537. other_script = script_getstr(st,6);
  8538. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8539. {
  8540. script_add_autobonus(bonus_script);
  8541. if( other_script )
  8542. script_add_autobonus(other_script);
  8543. }
  8544. return SCRIPT_CMD_SUCCESS;
  8545. }
  8546. BUILDIN_FUNC(autobonus2)
  8547. {
  8548. unsigned int dur, pos;
  8549. short rate;
  8550. uint16 atk_type = 0;
  8551. TBL_PC* sd;
  8552. const char *bonus_script, *other_script = NULL;
  8553. if( !script_rid2sd(sd) )
  8554. return SCRIPT_CMD_SUCCESS; // no player attached
  8555. if (current_equip_combo_pos)
  8556. pos = current_equip_combo_pos;
  8557. else
  8558. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8559. rate = script_getnum(st,3);
  8560. dur = script_getnum(st,4);
  8561. bonus_script = script_getstr(st,2);
  8562. if( !rate || !dur || !pos || !bonus_script )
  8563. return SCRIPT_CMD_SUCCESS;
  8564. if( script_hasdata(st,5) )
  8565. atk_type = script_getnum(st,5);
  8566. if( script_hasdata(st,6) )
  8567. other_script = script_getstr(st,6);
  8568. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8569. {
  8570. script_add_autobonus(bonus_script);
  8571. if( other_script )
  8572. script_add_autobonus(other_script);
  8573. }
  8574. return SCRIPT_CMD_SUCCESS;
  8575. }
  8576. BUILDIN_FUNC(autobonus3)
  8577. {
  8578. unsigned int dur, pos;
  8579. short rate;
  8580. uint16 skill_id = 0;
  8581. TBL_PC* sd;
  8582. const char *bonus_script, *other_script = NULL;
  8583. if( !script_rid2sd(sd) )
  8584. return SCRIPT_CMD_SUCCESS; // no player attached
  8585. if (current_equip_combo_pos)
  8586. pos = current_equip_combo_pos;
  8587. else
  8588. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8589. rate = script_getnum(st,3);
  8590. dur = script_getnum(st,4);
  8591. if (script_isstring(st, 5)) {
  8592. const char *name = script_getstr(st, 5);
  8593. if (!(skill_id = skill_name2id(name))) {
  8594. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8595. return SCRIPT_CMD_FAILURE;
  8596. }
  8597. } else {
  8598. skill_id = script_getnum(st, 5);
  8599. if (!skill_get_index(skill_id)) {
  8600. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8601. return SCRIPT_CMD_FAILURE;
  8602. }
  8603. }
  8604. bonus_script = script_getstr(st,2);
  8605. if( !rate || !dur || !pos || !bonus_script )
  8606. return SCRIPT_CMD_SUCCESS;
  8607. if( script_hasdata(st,6) )
  8608. other_script = script_getstr(st,6);
  8609. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8610. {
  8611. script_add_autobonus(bonus_script);
  8612. if( other_script )
  8613. script_add_autobonus(other_script);
  8614. }
  8615. return SCRIPT_CMD_SUCCESS;
  8616. }
  8617. /// Changes the level of a player skill.
  8618. /// <flag> defaults to 1
  8619. /// <flag>=0 : set the level of the skill
  8620. /// <flag>=1 : set the temporary level of the skill
  8621. /// <flag>=2 : add to the level of the skill
  8622. ///
  8623. /// skill <skill id>,<level>,<flag>
  8624. /// skill <skill id>,<level>
  8625. /// skill "<skill name>",<level>,<flag>
  8626. /// skill "<skill name>",<level>
  8627. BUILDIN_FUNC(skill)
  8628. {
  8629. int id;
  8630. int level;
  8631. int flag = ADDSKILL_TEMP;
  8632. TBL_PC* sd;
  8633. const char* command = script_getfuncname(st);
  8634. if( !script_rid2sd(sd) )
  8635. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8636. if (strcmpi(command, "addtoskill") == 0)
  8637. flag = ADDSKILL_TEMP_ADDLEVEL;
  8638. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8639. level = script_getnum(st,3);
  8640. if( script_hasdata(st,4) )
  8641. flag = script_getnum(st,4);
  8642. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8643. return SCRIPT_CMD_SUCCESS;
  8644. }
  8645. /// Increases the level of a guild skill.
  8646. ///
  8647. /// guildskill <skill id>,<amount>;
  8648. /// guildskill "<skill name>",<amount>;
  8649. BUILDIN_FUNC(guildskill)
  8650. {
  8651. int id;
  8652. int level;
  8653. TBL_PC* sd;
  8654. int i;
  8655. if( !script_rid2sd(sd) )
  8656. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8657. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8658. level = script_getnum(st,3);
  8659. for( i=0; i < level; i++ )
  8660. guild_skillup(sd, id);
  8661. return SCRIPT_CMD_SUCCESS;
  8662. }
  8663. /// Returns the level of the player skill.
  8664. ///
  8665. /// getskilllv(<skill id>) -> <level>
  8666. /// getskilllv("<skill name>") -> <level>
  8667. BUILDIN_FUNC(getskilllv)
  8668. {
  8669. int id;
  8670. TBL_PC* sd;
  8671. if( !script_rid2sd(sd) )
  8672. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8673. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8674. script_pushint(st, pc_checkskill(sd,id));
  8675. return SCRIPT_CMD_SUCCESS;
  8676. }
  8677. /// Returns the level of the guild skill.
  8678. ///
  8679. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8680. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8681. BUILDIN_FUNC(getgdskilllv)
  8682. {
  8683. int guild_id;
  8684. uint16 skill_id;
  8685. struct guild* g;
  8686. guild_id = script_getnum(st,2);
  8687. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8688. g = guild_search(guild_id);
  8689. if( g == NULL )
  8690. script_pushint(st, -1);
  8691. else
  8692. script_pushint(st, guild_checkskill(g,skill_id));
  8693. return SCRIPT_CMD_SUCCESS;
  8694. }
  8695. /// Returns the 'basic_skill_check' setting.
  8696. /// This config determines if the server checks the skill level of NV_BASIC
  8697. /// before allowing the basic actions.
  8698. ///
  8699. /// basicskillcheck() -> <bool>
  8700. BUILDIN_FUNC(basicskillcheck)
  8701. {
  8702. script_pushint(st, battle_config.basic_skill_check);
  8703. return SCRIPT_CMD_SUCCESS;
  8704. }
  8705. /// Returns the GM level of the player.
  8706. ///
  8707. /// getgmlevel({<char_id>}) -> <level>
  8708. BUILDIN_FUNC(getgmlevel)
  8709. {
  8710. TBL_PC* sd;
  8711. if (!script_charid2sd(2,sd))
  8712. return SCRIPT_CMD_FAILURE;
  8713. script_pushint(st, pc_get_group_level(sd));
  8714. return SCRIPT_CMD_SUCCESS;
  8715. }
  8716. /// Returns the group ID of the player.
  8717. ///
  8718. /// getgroupid({<char_id>}) -> <int>
  8719. BUILDIN_FUNC(getgroupid)
  8720. {
  8721. TBL_PC* sd;
  8722. if (!script_charid2sd(2,sd))
  8723. return SCRIPT_CMD_FAILURE;
  8724. script_pushint(st, pc_get_group_id(sd));
  8725. return SCRIPT_CMD_SUCCESS;
  8726. }
  8727. /// Terminates the execution of this script instance.
  8728. ///
  8729. /// end
  8730. BUILDIN_FUNC(end)
  8731. {
  8732. TBL_PC* sd;
  8733. sd = map_id2sd(st->rid);
  8734. st->state = END;
  8735. if( st->mes_active )
  8736. st->mes_active = 0;
  8737. if (sd){
  8738. if (sd->state.callshop == 0)
  8739. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8740. else
  8741. sd->state.callshop = 0;
  8742. }
  8743. return SCRIPT_CMD_SUCCESS;
  8744. }
  8745. /// Checks if the player has that effect state (option).
  8746. ///
  8747. /// checkoption(<option>{,<char_id>}) -> <bool>
  8748. BUILDIN_FUNC(checkoption)
  8749. {
  8750. int option;
  8751. TBL_PC* sd;
  8752. if (!script_charid2sd(3,sd))
  8753. return SCRIPT_CMD_FAILURE;
  8754. option = script_getnum(st,2);
  8755. if( sd->sc.option&option )
  8756. script_pushint(st, 1);
  8757. else
  8758. script_pushint(st, 0);
  8759. return SCRIPT_CMD_SUCCESS;
  8760. }
  8761. /// Checks if the player is in that body state (opt1).
  8762. ///
  8763. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8764. BUILDIN_FUNC(checkoption1)
  8765. {
  8766. int opt1;
  8767. TBL_PC* sd;
  8768. if (!script_charid2sd(3,sd))
  8769. return SCRIPT_CMD_FAILURE;
  8770. opt1 = script_getnum(st,2);
  8771. if( sd->sc.opt1 == opt1 )
  8772. script_pushint(st, 1);
  8773. else
  8774. script_pushint(st, 0);
  8775. return SCRIPT_CMD_SUCCESS;
  8776. }
  8777. /// Checks if the player has that health state (opt2).
  8778. ///
  8779. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8780. BUILDIN_FUNC(checkoption2)
  8781. {
  8782. int opt2;
  8783. TBL_PC* sd;
  8784. if (!script_charid2sd(3,sd))
  8785. return SCRIPT_CMD_FAILURE;
  8786. opt2 = script_getnum(st,2);
  8787. if( sd->sc.opt2&opt2 )
  8788. script_pushint(st, 1);
  8789. else
  8790. script_pushint(st, 0);
  8791. return SCRIPT_CMD_SUCCESS;
  8792. }
  8793. /// Changes the effect state (option) of the player.
  8794. /// <flag> defaults to 1
  8795. /// <flag>=0 : removes the option
  8796. /// <flag>=other : adds the option
  8797. ///
  8798. /// setoption <option>{,<flag>{,<char_id>}};
  8799. BUILDIN_FUNC(setoption)
  8800. {
  8801. int option;
  8802. int flag = 1;
  8803. TBL_PC* sd;
  8804. if (!script_charid2sd(4,sd))
  8805. return SCRIPT_CMD_FAILURE;
  8806. option = script_getnum(st,2);
  8807. if( script_hasdata(st,3) )
  8808. flag = script_getnum(st,3);
  8809. else if( !option ){// Request to remove everything.
  8810. flag = 0;
  8811. option = OPTION_FALCON|OPTION_RIDING;
  8812. #ifndef NEW_CARTS
  8813. option |= OPTION_CART;
  8814. #endif
  8815. }
  8816. if( flag ){// Add option
  8817. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8818. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8819. pc_setoption(sd, sd->sc.option|option);
  8820. } else// Remove option
  8821. pc_setoption(sd, sd->sc.option&~option);
  8822. return SCRIPT_CMD_SUCCESS;
  8823. }
  8824. /// Returns if the player has a cart.
  8825. ///
  8826. /// checkcart({char_id}) -> <bool>
  8827. ///
  8828. /// @author Valaris
  8829. BUILDIN_FUNC(checkcart)
  8830. {
  8831. TBL_PC* sd;
  8832. if (!script_charid2sd(2,sd))
  8833. return SCRIPT_CMD_FAILURE;
  8834. if( pc_iscarton(sd) )
  8835. script_pushint(st, 1);
  8836. else
  8837. script_pushint(st, 0);
  8838. return SCRIPT_CMD_SUCCESS;
  8839. }
  8840. /// Sets the cart of the player.
  8841. /// <type> defaults to 1
  8842. /// <type>=0 : removes the cart
  8843. /// <type>=1 : Normal cart
  8844. /// <type>=2 : Wooden cart
  8845. /// <type>=3 : Covered cart with flowers and ferns
  8846. /// <type>=4 : Wooden cart with a Panda doll on the back
  8847. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8848. ///
  8849. /// setcart {<type>{,<char_id>}};
  8850. BUILDIN_FUNC(setcart)
  8851. {
  8852. int type = 1;
  8853. TBL_PC* sd;
  8854. if (!script_charid2sd(3,sd))
  8855. return SCRIPT_CMD_FAILURE;
  8856. if( script_hasdata(st,2) )
  8857. type = script_getnum(st,2);
  8858. pc_setcart(sd, type);
  8859. return SCRIPT_CMD_SUCCESS;
  8860. }
  8861. /// Returns if the player has a falcon.
  8862. ///
  8863. /// checkfalcon({char_id}) -> <bool>
  8864. ///
  8865. /// @author Valaris
  8866. BUILDIN_FUNC(checkfalcon)
  8867. {
  8868. TBL_PC* sd;
  8869. if (!script_charid2sd(2,sd))
  8870. return SCRIPT_CMD_FAILURE;
  8871. if( pc_isfalcon(sd) )
  8872. script_pushint(st, 1);
  8873. else
  8874. script_pushint(st, 0);
  8875. return SCRIPT_CMD_SUCCESS;
  8876. }
  8877. /// Sets if the player has a falcon or not.
  8878. /// <flag> defaults to 1
  8879. ///
  8880. /// setfalcon {<flag>{,<char_id>}};
  8881. BUILDIN_FUNC(setfalcon)
  8882. {
  8883. int flag = 1;
  8884. TBL_PC* sd;
  8885. if (!script_charid2sd(3,sd))
  8886. return SCRIPT_CMD_FAILURE;
  8887. if( script_hasdata(st,2) )
  8888. flag = script_getnum(st,2);
  8889. pc_setfalcon(sd, flag);
  8890. return SCRIPT_CMD_SUCCESS;
  8891. }
  8892. /// Returns if the player is riding.
  8893. ///
  8894. /// checkriding({char_id}) -> <bool>
  8895. ///
  8896. /// @author Valaris
  8897. BUILDIN_FUNC(checkriding)
  8898. {
  8899. TBL_PC* sd;
  8900. if (!script_charid2sd(2,sd))
  8901. return SCRIPT_CMD_FAILURE;
  8902. if( pc_isriding(sd) )
  8903. script_pushint(st, 1);
  8904. else
  8905. script_pushint(st, 0);
  8906. return SCRIPT_CMD_SUCCESS;
  8907. }
  8908. /// Sets if the player is riding.
  8909. /// <flag> defaults to 1
  8910. ///
  8911. /// setriding {<flag>{,<char_id>}};
  8912. BUILDIN_FUNC(setriding)
  8913. {
  8914. int flag = 1;
  8915. TBL_PC* sd;
  8916. if (!script_charid2sd(3,sd))
  8917. return SCRIPT_CMD_FAILURE;
  8918. if( script_hasdata(st,2) )
  8919. flag = script_getnum(st,2);
  8920. pc_setriding(sd, flag);
  8921. return SCRIPT_CMD_SUCCESS;
  8922. }
  8923. /// Returns if the player has a warg.
  8924. ///
  8925. /// checkwug({char_id}) -> <bool>
  8926. ///
  8927. BUILDIN_FUNC(checkwug)
  8928. {
  8929. TBL_PC* sd;
  8930. if (!script_charid2sd(2,sd))
  8931. return SCRIPT_CMD_FAILURE;
  8932. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8933. script_pushint(st, 1);
  8934. else
  8935. script_pushint(st, 0);
  8936. return SCRIPT_CMD_SUCCESS;
  8937. }
  8938. /// Returns if the player is wearing MADO Gear.
  8939. ///
  8940. /// checkmadogear({<char_id>}) -> <bool>
  8941. ///
  8942. BUILDIN_FUNC(checkmadogear)
  8943. {
  8944. TBL_PC* sd;
  8945. if (!script_charid2sd(2,sd))
  8946. return SCRIPT_CMD_FAILURE;
  8947. if( pc_ismadogear(sd) )
  8948. script_pushint(st, 1);
  8949. else
  8950. script_pushint(st, 0);
  8951. return SCRIPT_CMD_SUCCESS;
  8952. }
  8953. /// Sets if the player is riding MADO Gear.
  8954. /// <flag> defaults to true
  8955. /// <type> defaults to MADO_ROBOT
  8956. ///
  8957. /// setmadogear {<flag>{,type{,<char_id>}}};
  8958. BUILDIN_FUNC(setmadogear)
  8959. {
  8960. bool flag = true;
  8961. TBL_PC* sd;
  8962. uint16 type = MADO_ROBOT;
  8963. if (!script_charid2sd(4,sd))
  8964. return SCRIPT_CMD_FAILURE;
  8965. if( script_hasdata(st,2) )
  8966. flag = script_getnum(st,2) != 0;
  8967. if (script_hasdata(st, 3)) {
  8968. type = script_getnum(st, 3);
  8969. if (type == MADO_UNUSED || type >= MADO_MAX) {
  8970. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  8971. type = MADO_ROBOT;
  8972. }
  8973. }
  8974. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  8975. return SCRIPT_CMD_SUCCESS;
  8976. }
  8977. /// Sets the save point of the player.
  8978. ///
  8979. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8980. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8981. BUILDIN_FUNC(savepoint)
  8982. {
  8983. int x, y, m, cid_pos = 5;
  8984. unsigned short map_idx;
  8985. const char* str;
  8986. TBL_PC* sd;
  8987. if (script_lastdata(st) > 5)
  8988. cid_pos = 7;
  8989. if (!script_charid2sd(cid_pos,sd))
  8990. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8991. str = script_getstr(st, 2);
  8992. map_idx = mapindex_name2id(str);
  8993. if( !map_idx )
  8994. return SCRIPT_CMD_FAILURE;
  8995. x = script_getnum(st,3);
  8996. y = script_getnum(st,4);
  8997. m = map_mapindex2mapid(map_idx);
  8998. if (cid_pos == 7) {
  8999. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  9000. x1 = x + dx, y1 = y + dy,
  9001. x0 = x - dx, y0 = y - dy;
  9002. uint8 n = 10;
  9003. do {
  9004. x = x0 + rnd()%(x1-x0+1);
  9005. y = y0 + rnd()%(y1-y0+1);
  9006. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  9007. }
  9008. // Check for valid coordinates if map in local map-server
  9009. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  9010. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  9011. return SCRIPT_CMD_FAILURE;
  9012. }
  9013. pc_setsavepoint(sd, map_idx, x, y);
  9014. return SCRIPT_CMD_SUCCESS;
  9015. }
  9016. /*==========================================
  9017. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  9018. *------------------------------------------*/
  9019. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  9020. {
  9021. int type;
  9022. time_t timer;
  9023. struct tm *t;
  9024. type=script_getnum(st,2);
  9025. switch(type){
  9026. case 2:
  9027. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  9028. // from the system clock.)
  9029. script_pushint(st,(int)time(NULL));
  9030. break;
  9031. case 1:
  9032. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  9033. time(&timer);
  9034. t=localtime(&timer);
  9035. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  9036. break;
  9037. case 0:
  9038. default:
  9039. //type 0:(System Ticks)
  9040. script_pushint(st,gettick());
  9041. break;
  9042. }
  9043. return SCRIPT_CMD_SUCCESS;
  9044. }
  9045. /*==========================================
  9046. * GetTime(Type)
  9047. *
  9048. * Returns the current value of a certain date type.
  9049. * Possible types are:
  9050. * - DT_SECOND Current seconds
  9051. * - DT_MINUTE Current minute
  9052. * - DT_HOUR Current hour
  9053. * - DT_DAYOFWEEK Day of current week
  9054. * - DT_DAYOFMONTH Day of current month
  9055. * - DT_MONTH Current month
  9056. * - DT_YEAR Current year
  9057. * - DT_DAYOFYEAR Day of current year
  9058. *
  9059. * If none of the above types is supplied -1 will be returned to the script
  9060. * and the script source will be reported into the mapserver console.
  9061. *------------------------------------------*/
  9062. BUILDIN_FUNC(gettime)
  9063. {
  9064. int type;
  9065. type = script_getnum(st,2);
  9066. if( type <= DT_MIN || type >= DT_MAX ){
  9067. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  9068. script_reportsrc(st);
  9069. script_pushint(st,-1);
  9070. }else{
  9071. script_pushint(st,date_get((enum e_date_type)type));
  9072. }
  9073. return SCRIPT_CMD_SUCCESS;
  9074. }
  9075. /**
  9076. * Returns the current server time or the provided time in a readable format.
  9077. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  9078. */
  9079. BUILDIN_FUNC(gettimestr)
  9080. {
  9081. char *tmpstr;
  9082. const char *fmtstr;
  9083. int maxlen;
  9084. time_t now;
  9085. fmtstr = script_getstr(st,2);
  9086. maxlen = script_getnum(st,3);
  9087. if (script_hasdata(st, 4)) {
  9088. if (script_getnum(st, 4) < 0) {
  9089. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  9090. return SCRIPT_CMD_FAILURE;
  9091. } else
  9092. now = (time_t)script_getnum(st, 4);
  9093. } else
  9094. now = time(NULL);
  9095. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  9096. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  9097. tmpstr[maxlen] = '\0';
  9098. script_pushstr(st,tmpstr);
  9099. return SCRIPT_CMD_SUCCESS;
  9100. }
  9101. /*==========================================
  9102. * Open player storage
  9103. *------------------------------------------*/
  9104. BUILDIN_FUNC(openstorage)
  9105. {
  9106. TBL_PC* sd;
  9107. if( !script_rid2sd(sd) )
  9108. return SCRIPT_CMD_SUCCESS;
  9109. storage_storageopen(sd);
  9110. return SCRIPT_CMD_SUCCESS;
  9111. }
  9112. BUILDIN_FUNC(guildopenstorage)
  9113. {
  9114. TBL_PC* sd;
  9115. int ret;
  9116. if( !script_rid2sd(sd) )
  9117. return SCRIPT_CMD_SUCCESS;
  9118. ret = storage_guild_storageopen(sd);
  9119. script_pushint(st,ret);
  9120. return SCRIPT_CMD_SUCCESS;
  9121. }
  9122. BUILDIN_FUNC(guildopenstorage_log){
  9123. #if PACKETVER < 20140205
  9124. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  9125. return SCRIPT_CMD_FAILURE;
  9126. #else
  9127. struct map_session_data* sd;
  9128. if( !script_charid2sd( 2, sd ) ){
  9129. return SCRIPT_CMD_FAILURE;
  9130. }
  9131. script_pushint( st, storage_guild_log_read( sd ) );
  9132. return SCRIPT_CMD_SUCCESS;
  9133. #endif
  9134. }
  9135. BUILDIN_FUNC(guild_has_permission){
  9136. struct map_session_data* sd;
  9137. if( !script_charid2sd( 3, sd ) ){
  9138. return SCRIPT_CMD_FAILURE;
  9139. }
  9140. int permission = script_getnum(st,2);
  9141. if( permission == 0 ){
  9142. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  9143. return SCRIPT_CMD_FAILURE;
  9144. }
  9145. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  9146. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  9147. return SCRIPT_CMD_FAILURE;
  9148. }
  9149. if( !sd->guild ){
  9150. script_pushint( st, false );
  9151. return SCRIPT_CMD_SUCCESS;
  9152. }
  9153. int position = guild_getposition(sd);
  9154. if( position < 0 || ( sd->guild->position[position].mode&permission ) != permission ){
  9155. script_pushint( st, false );
  9156. return SCRIPT_CMD_SUCCESS;
  9157. }
  9158. script_pushint( st, true );
  9159. return SCRIPT_CMD_SUCCESS;
  9160. }
  9161. /*==========================================
  9162. * Make player use a skill trought item usage
  9163. *------------------------------------------*/
  9164. /// itemskill <skill id>,<level>
  9165. /// itemskill "<skill name>",<level>
  9166. BUILDIN_FUNC(itemskill)
  9167. {
  9168. int id;
  9169. int lv;
  9170. bool keep_requirement;
  9171. TBL_PC* sd;
  9172. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9173. return SCRIPT_CMD_SUCCESS;
  9174. if (script_isstring(st, 2)) {
  9175. const char *name = script_getstr(st, 2);
  9176. if (!(id = skill_name2id(name))) {
  9177. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9178. return SCRIPT_CMD_FAILURE;
  9179. }
  9180. } else {
  9181. id = script_getnum(st, 2);
  9182. if (!skill_get_index(id)) {
  9183. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9184. return SCRIPT_CMD_FAILURE;
  9185. }
  9186. }
  9187. lv = script_getnum(st,3);
  9188. if (script_hasdata(st, 4)) {
  9189. keep_requirement = (script_getnum(st, 4) != 0);
  9190. } else {
  9191. keep_requirement = false;
  9192. }
  9193. sd->skillitem=id;
  9194. sd->skillitemlv=lv;
  9195. sd->skillitem_keep_requirement = keep_requirement;
  9196. clif_item_skill(sd,id,lv);
  9197. return SCRIPT_CMD_SUCCESS;
  9198. }
  9199. /*==========================================
  9200. * Attempt to create an item
  9201. *------------------------------------------*/
  9202. BUILDIN_FUNC(produce)
  9203. {
  9204. int trigger;
  9205. TBL_PC* sd;
  9206. if( !script_rid2sd(sd) )
  9207. return SCRIPT_CMD_SUCCESS;
  9208. trigger=script_getnum(st,2);
  9209. clif_skill_produce_mix_list(sd, -1, trigger);
  9210. return SCRIPT_CMD_SUCCESS;
  9211. }
  9212. /*==========================================
  9213. *
  9214. *------------------------------------------*/
  9215. BUILDIN_FUNC(cooking)
  9216. {
  9217. int trigger;
  9218. TBL_PC* sd;
  9219. if( !script_rid2sd(sd) )
  9220. return SCRIPT_CMD_SUCCESS;
  9221. trigger=script_getnum(st,2);
  9222. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  9223. return SCRIPT_CMD_SUCCESS;
  9224. }
  9225. /*==========================================
  9226. * Create a pet
  9227. *------------------------------------------*/
  9228. BUILDIN_FUNC(makepet)
  9229. {
  9230. struct map_session_data* sd;
  9231. uint16 mob_id;
  9232. if( !script_rid2sd(sd) )
  9233. return SCRIPT_CMD_FAILURE;
  9234. mob_id = script_getnum(st,2);
  9235. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9236. if( !pet ){
  9237. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9238. return SCRIPT_CMD_FAILURE;
  9239. }
  9240. sd->catch_target_class = mob_id;
  9241. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9242. 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() );
  9243. return SCRIPT_CMD_SUCCESS;
  9244. }
  9245. /**
  9246. * Give player exp base,job * quest_exp_rate/100
  9247. * getexp <base xp>,<job xp>{,<char_id>};
  9248. **/
  9249. BUILDIN_FUNC(getexp){
  9250. struct map_session_data* sd;
  9251. if( !script_charid2sd( 4, sd ) ){
  9252. return SCRIPT_CMD_FAILURE;
  9253. }
  9254. int64 base = script_getnum64( st, 2 );
  9255. if( base < 0 ){
  9256. ShowError( "buildin_getexp: Called with negative base exp.\n" );
  9257. return SCRIPT_CMD_FAILURE;
  9258. }
  9259. int64 job = script_getnum64( st, 3 );
  9260. if( job < 0 ){
  9261. ShowError( "buildin_getexp: Called with negative job exp.\n" );
  9262. return SCRIPT_CMD_FAILURE;
  9263. }
  9264. if( base == 0 && job == 0 ){
  9265. ShowError( "buildin_getexp: Called with base and job exp 0.\n" );
  9266. return SCRIPT_CMD_FAILURE;
  9267. }
  9268. // bonus for npc-given exp
  9269. double bonus = battle_config.quest_exp_rate / 100.;
  9270. if (base)
  9271. base = (int64) cap_value(base * bonus, 0, MAX_EXP);
  9272. if (job)
  9273. job = (int64) cap_value(job * bonus, 0, MAX_EXP);
  9274. pc_gainexp(sd, NULL, base, job, 1);
  9275. #ifdef RENEWAL
  9276. if (base && sd->hd)
  9277. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9278. #endif
  9279. return SCRIPT_CMD_SUCCESS;
  9280. }
  9281. /*==========================================
  9282. * Gain guild exp [Celest]
  9283. *------------------------------------------*/
  9284. BUILDIN_FUNC(guildgetexp){
  9285. struct map_session_data* sd;
  9286. if( !script_rid2sd( sd ) ){
  9287. return SCRIPT_CMD_FAILURE;
  9288. }
  9289. int64 exp = script_getnum64( st, 2 );
  9290. if( exp <= 0 ){
  9291. ShowError( "buildin_guildgetexp: Called with exp <= 0.\n" );
  9292. return SCRIPT_CMD_FAILURE;
  9293. }
  9294. if( sd->status.guild_id <= 0 ){
  9295. 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 );
  9296. return SCRIPT_CMD_FAILURE;
  9297. }
  9298. guild_getexp( sd, exp );
  9299. return SCRIPT_CMD_SUCCESS;
  9300. }
  9301. /*==========================================
  9302. * Changes the guild master of a guild [Skotlex]
  9303. *------------------------------------------*/
  9304. BUILDIN_FUNC(guildchangegm)
  9305. {
  9306. TBL_PC *sd;
  9307. int guild_id;
  9308. const char *name;
  9309. guild_id = script_getnum(st,2);
  9310. name = script_getstr(st,3);
  9311. sd=map_nick2sd(name,false);
  9312. if (!sd)
  9313. script_pushint(st,0);
  9314. else
  9315. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9316. return SCRIPT_CMD_SUCCESS;
  9317. }
  9318. /*==========================================
  9319. * Spawn a monster:
  9320. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9321. *------------------------------------------*/
  9322. BUILDIN_FUNC(monster)
  9323. {
  9324. const char* mapn = script_getstr(st,2);
  9325. int x = script_getnum(st,3);
  9326. int y = script_getnum(st,4);
  9327. const char* str = script_getstr(st,5);
  9328. int class_ = script_getnum(st,6);
  9329. int amount = script_getnum(st,7);
  9330. const char* event = "";
  9331. unsigned int size = SZ_SMALL;
  9332. enum mob_ai ai = AI_NONE;
  9333. struct map_session_data* sd;
  9334. int16 m;
  9335. int i;
  9336. if (script_hasdata(st, 8)) {
  9337. event = script_getstr(st, 8);
  9338. check_event(st, event);
  9339. }
  9340. if (script_hasdata(st, 9)) {
  9341. size = script_getnum(st, 9);
  9342. if (size > SZ_BIG) {
  9343. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9344. return SCRIPT_CMD_FAILURE;
  9345. }
  9346. }
  9347. if (script_hasdata(st, 10)) {
  9348. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9349. if (ai >= AI_MAX) {
  9350. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9351. return SCRIPT_CMD_FAILURE;
  9352. }
  9353. }
  9354. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9355. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9356. return SCRIPT_CMD_FAILURE;
  9357. }
  9358. sd = map_id2sd(st->rid);
  9359. if (sd && strcmp(mapn, "this") == 0)
  9360. m = sd->bl.m;
  9361. else
  9362. m = map_mapname2mapid(mapn);
  9363. for(i = 0; i < amount; i++) { //not optimised
  9364. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9365. if (mobid)
  9366. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9367. }
  9368. return SCRIPT_CMD_SUCCESS;
  9369. }
  9370. /*==========================================
  9371. * Request List of Monster Drops
  9372. *------------------------------------------*/
  9373. BUILDIN_FUNC(getmobdrops)
  9374. {
  9375. int class_ = script_getnum(st,2);
  9376. int i, j = 0;
  9377. if( !mobdb_checkid(class_) )
  9378. {
  9379. script_pushint(st, 0);
  9380. return SCRIPT_CMD_SUCCESS;
  9381. }
  9382. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9383. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9384. {
  9385. if( mob->dropitem[i].nameid == 0 )
  9386. continue;
  9387. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  9388. continue;
  9389. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9390. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9391. mapreg_setreg(reference_uid(add_str("$@MobDrop_nosteal"), j), mob->dropitem[i].steal_protected);
  9392. mapreg_setreg(reference_uid(add_str("$@MobDrop_randomopt"), j), mob->dropitem[i].randomopt_group);
  9393. j++;
  9394. }
  9395. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9396. script_pushint(st, 1);
  9397. return SCRIPT_CMD_SUCCESS;
  9398. }
  9399. /*==========================================
  9400. * Spawn a monster in a random location
  9401. * in x0,x1,y0,y1 area.
  9402. *------------------------------------------*/
  9403. BUILDIN_FUNC(areamonster)
  9404. {
  9405. const char* mapn = script_getstr(st,2);
  9406. int x0 = script_getnum(st,3);
  9407. int y0 = script_getnum(st,4);
  9408. int x1 = script_getnum(st,5);
  9409. int y1 = script_getnum(st,6);
  9410. const char* str = script_getstr(st,7);
  9411. int class_ = script_getnum(st,8);
  9412. int amount = script_getnum(st,9);
  9413. const char* event = "";
  9414. unsigned int size = SZ_SMALL;
  9415. enum mob_ai ai = AI_NONE;
  9416. struct map_session_data* sd;
  9417. int16 m;
  9418. int i;
  9419. if (script_hasdata(st,10)) {
  9420. event = script_getstr(st, 10);
  9421. check_event(st, event);
  9422. }
  9423. if (script_hasdata(st, 11)) {
  9424. size = script_getnum(st, 11);
  9425. if (size > 3) {
  9426. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9427. return SCRIPT_CMD_FAILURE;
  9428. }
  9429. }
  9430. if (script_hasdata(st, 12)) {
  9431. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9432. if (ai >= AI_MAX) {
  9433. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9434. return SCRIPT_CMD_FAILURE;
  9435. }
  9436. }
  9437. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9438. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9439. return SCRIPT_CMD_FAILURE;
  9440. }
  9441. sd = map_id2sd(st->rid);
  9442. if (sd && strcmp(mapn, "this") == 0)
  9443. m = sd->bl.m;
  9444. else
  9445. m = map_mapname2mapid(mapn);
  9446. for(i = 0; i < amount; i++) { //not optimised
  9447. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9448. if (mobid)
  9449. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9450. }
  9451. return SCRIPT_CMD_SUCCESS;
  9452. }
  9453. /*==========================================
  9454. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9455. *------------------------------------------*/
  9456. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9457. { //same fix but with killmonster instead - stripping events from mobs.
  9458. TBL_MOB* md = (TBL_MOB*)bl;
  9459. char *event=va_arg(ap,char *);
  9460. int allflag=va_arg(ap,int);
  9461. md->state.npc_killmonster = 1;
  9462. if(!allflag){
  9463. if(strcmp(event,md->npc_event)==0)
  9464. status_kill(bl);
  9465. }else{
  9466. if(!md->spawn)
  9467. status_kill(bl);
  9468. }
  9469. md->state.npc_killmonster = 0;
  9470. return SCRIPT_CMD_SUCCESS;
  9471. }
  9472. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9473. {
  9474. TBL_MOB* md = (TBL_MOB*)bl;
  9475. char *event=va_arg(ap,char *);
  9476. int allflag=va_arg(ap,int);
  9477. if(!allflag){
  9478. if(strcmp(event,md->npc_event)==0)
  9479. status_kill(bl);
  9480. }else{
  9481. if(!md->spawn)
  9482. status_kill(bl);
  9483. }
  9484. return SCRIPT_CMD_SUCCESS;
  9485. }
  9486. BUILDIN_FUNC(killmonster)
  9487. {
  9488. const char *mapname,*event;
  9489. int16 m,allflag=0;
  9490. mapname=script_getstr(st,2);
  9491. event=script_getstr(st,3);
  9492. if(strcmp(event,"All")==0)
  9493. allflag = 1;
  9494. else
  9495. check_event(st, event);
  9496. if( (m=map_mapname2mapid(mapname))<0 )
  9497. return SCRIPT_CMD_SUCCESS;
  9498. if( script_hasdata(st,4) ) {
  9499. if ( script_getnum(st,4) == 1 ) {
  9500. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9501. return SCRIPT_CMD_SUCCESS;
  9502. }
  9503. }
  9504. map_freeblock_lock();
  9505. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9506. map_freeblock_unlock();
  9507. return SCRIPT_CMD_SUCCESS;
  9508. }
  9509. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9510. { //Strips the event from the mob if it's killed the old method.
  9511. struct mob_data *md;
  9512. md = BL_CAST(BL_MOB, bl);
  9513. if (md->npc_event[0])
  9514. md->npc_event[0] = 0;
  9515. status_kill(bl);
  9516. return 0;
  9517. }
  9518. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9519. {
  9520. status_kill(bl);
  9521. return 0;
  9522. }
  9523. BUILDIN_FUNC(killmonsterall)
  9524. {
  9525. const char *mapname;
  9526. int16 m;
  9527. mapname=script_getstr(st,2);
  9528. if( (m = map_mapname2mapid(mapname))<0 )
  9529. return SCRIPT_CMD_SUCCESS;
  9530. if( script_hasdata(st,3) ) {
  9531. if ( script_getnum(st,3) == 1 ) {
  9532. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9533. return SCRIPT_CMD_SUCCESS;
  9534. }
  9535. }
  9536. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9537. return SCRIPT_CMD_SUCCESS;
  9538. }
  9539. /*==========================================
  9540. * Creates a clone of a player.
  9541. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9542. *------------------------------------------*/
  9543. BUILDIN_FUNC(clone)
  9544. {
  9545. TBL_PC *sd, *msd=NULL;
  9546. uint32 char_id, master_id = 0, x, y, flag = 0;
  9547. int16 m;
  9548. enum e_mode mode = MD_NONE;
  9549. unsigned int duration = 0;
  9550. const char *mapname,*event;
  9551. mapname=script_getstr(st,2);
  9552. x=script_getnum(st,3);
  9553. y=script_getnum(st,4);
  9554. event=script_getstr(st,5);
  9555. char_id=script_getnum(st,6);
  9556. if( script_hasdata(st,7) )
  9557. master_id=script_getnum(st,7);
  9558. if( script_hasdata(st,8) )
  9559. mode=static_cast<e_mode>(script_getnum(st,8));
  9560. if( script_hasdata(st,9) )
  9561. flag=script_getnum(st,9);
  9562. if( script_hasdata(st,10) )
  9563. duration=script_getnum(st,10);
  9564. check_event(st, event);
  9565. m = map_mapname2mapid(mapname);
  9566. if (m < 0)
  9567. return SCRIPT_CMD_SUCCESS;
  9568. sd = map_charid2sd(char_id);
  9569. if (master_id) {
  9570. msd = map_charid2sd(master_id);
  9571. if (msd)
  9572. master_id = msd->bl.id;
  9573. else
  9574. master_id = 0;
  9575. }
  9576. if (sd) //Return ID of newly crafted clone.
  9577. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9578. else //Failed to create clone.
  9579. script_pushint(st,0);
  9580. return SCRIPT_CMD_SUCCESS;
  9581. }
  9582. /*==========================================
  9583. *------------------------------------------*/
  9584. BUILDIN_FUNC(doevent)
  9585. {
  9586. const char* event;
  9587. struct map_session_data* sd;
  9588. if( !script_rid2sd(sd) )
  9589. return SCRIPT_CMD_SUCCESS;
  9590. event = script_getstr(st,2);
  9591. check_event(st, event);
  9592. npc_event(sd, event, 0);
  9593. return SCRIPT_CMD_SUCCESS;
  9594. }
  9595. /*==========================================
  9596. *------------------------------------------*/
  9597. BUILDIN_FUNC(donpcevent)
  9598. {
  9599. const char* event = script_getstr(st,2);
  9600. check_event(st, event);
  9601. if( !npc_event_do(event) ) {
  9602. struct npc_data * nd = map_id2nd(st->oid);
  9603. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9604. script_pushint(st, 0);
  9605. } else
  9606. script_pushint(st, 1);
  9607. return SCRIPT_CMD_SUCCESS;
  9608. }
  9609. /// for Aegis compatibility
  9610. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9611. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9612. {
  9613. const char* npc = script_getstr(st,2);
  9614. const char* command = script_getstr(st,3);
  9615. char event[EVENT_NAME_LENGTH];
  9616. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9617. check_event(st, event);
  9618. if( npc_event_do(event) ){
  9619. script_pushint(st, true);
  9620. }else{
  9621. struct npc_data * nd = map_id2nd(st->oid);
  9622. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9623. script_pushint(st, false);
  9624. }
  9625. return SCRIPT_CMD_SUCCESS;
  9626. }
  9627. /*==========================================
  9628. *------------------------------------------*/
  9629. BUILDIN_FUNC(addtimer)
  9630. {
  9631. int tick;
  9632. const char* event;
  9633. TBL_PC* sd;
  9634. if( !script_rid2sd(sd) )
  9635. return SCRIPT_CMD_SUCCESS;
  9636. tick = script_getnum(st,2);
  9637. event = script_getstr(st, 3);
  9638. check_event(st, event);
  9639. if (!pc_addeventtimer(sd,tick,event)) {
  9640. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9641. return SCRIPT_CMD_FAILURE;
  9642. }
  9643. return SCRIPT_CMD_SUCCESS;
  9644. }
  9645. /*==========================================
  9646. *------------------------------------------*/
  9647. BUILDIN_FUNC(deltimer)
  9648. {
  9649. const char *event;
  9650. TBL_PC* sd;
  9651. if( !script_rid2sd(sd) )
  9652. return SCRIPT_CMD_SUCCESS;
  9653. event=script_getstr(st, 2);
  9654. check_event(st, event);
  9655. pc_deleventtimer(sd,event);
  9656. return SCRIPT_CMD_SUCCESS;
  9657. }
  9658. /*==========================================
  9659. *------------------------------------------*/
  9660. BUILDIN_FUNC(addtimercount)
  9661. {
  9662. const char *event;
  9663. int tick;
  9664. TBL_PC* sd;
  9665. if( !script_rid2sd(sd) )
  9666. return SCRIPT_CMD_SUCCESS;
  9667. tick=script_getnum(st,2);
  9668. event=script_getstr(st,3);
  9669. check_event(st, event);
  9670. pc_addeventtimercount(sd,event,tick);
  9671. return SCRIPT_CMD_SUCCESS;
  9672. }
  9673. /*==========================================
  9674. *------------------------------------------*/
  9675. BUILDIN_FUNC(initnpctimer)
  9676. {
  9677. struct npc_data *nd;
  9678. int flag = 0;
  9679. if( script_hasdata(st,3) )
  9680. { //Two arguments: NPC name and attach flag.
  9681. nd = npc_name2id(script_getstr(st, 2));
  9682. flag = script_getnum(st,3);
  9683. }
  9684. else if( script_hasdata(st,2) )
  9685. { //Check if argument is numeric (flag) or string (npc name)
  9686. if( script_isstring(st, 2) ) //NPC name
  9687. nd = npc_name2id(script_getstr(st, 2));
  9688. else //Flag
  9689. {
  9690. nd = (struct npc_data *)map_id2bl(st->oid);
  9691. flag = script_getnum(st, 2);
  9692. }
  9693. }
  9694. else
  9695. nd = (struct npc_data *)map_id2bl(st->oid);
  9696. if( !nd )
  9697. return SCRIPT_CMD_SUCCESS;
  9698. if( flag ) //Attach
  9699. {
  9700. TBL_PC* sd;
  9701. if( !script_rid2sd(sd) )
  9702. return SCRIPT_CMD_SUCCESS;
  9703. nd->u.scr.rid = sd->bl.id;
  9704. }
  9705. nd->u.scr.timertick = 0;
  9706. npc_settimerevent_tick(nd,0);
  9707. npc_timerevent_start(nd, st->rid);
  9708. return SCRIPT_CMD_SUCCESS;
  9709. }
  9710. /*==========================================
  9711. *------------------------------------------*/
  9712. BUILDIN_FUNC(startnpctimer)
  9713. {
  9714. struct npc_data *nd;
  9715. int flag = 0;
  9716. if( script_hasdata(st,3) )
  9717. { //Two arguments: NPC name and attach flag.
  9718. nd = npc_name2id(script_getstr(st, 2));
  9719. flag = script_getnum(st,3);
  9720. }
  9721. else if( script_hasdata(st,2) )
  9722. { //Check if argument is numeric (flag) or string (npc name)
  9723. if( script_isstring(st, 2) ) //NPC name
  9724. nd = npc_name2id(script_getstr(st, 2));
  9725. else //Flag
  9726. {
  9727. nd = (struct npc_data *)map_id2bl(st->oid);
  9728. flag = script_getnum(st, 2);
  9729. }
  9730. }
  9731. else
  9732. nd=(struct npc_data *)map_id2bl(st->oid);
  9733. if( !nd )
  9734. return SCRIPT_CMD_SUCCESS;
  9735. if( flag ) //Attach
  9736. {
  9737. TBL_PC* sd;
  9738. if( !script_rid2sd(sd) )
  9739. return SCRIPT_CMD_SUCCESS;
  9740. nd->u.scr.rid = sd->bl.id;
  9741. }
  9742. npc_timerevent_start(nd, st->rid);
  9743. return SCRIPT_CMD_SUCCESS;
  9744. }
  9745. /*==========================================
  9746. *------------------------------------------*/
  9747. BUILDIN_FUNC(stopnpctimer)
  9748. {
  9749. struct npc_data *nd;
  9750. int flag = 0;
  9751. if( script_hasdata(st,3) )
  9752. { //Two arguments: NPC name and attach flag.
  9753. nd = npc_name2id(script_getstr(st, 2));
  9754. flag = script_getnum(st,3);
  9755. }
  9756. else if( script_hasdata(st,2) )
  9757. { //Check if argument is numeric (flag) or string (npc name)
  9758. if( script_isstring(st, 2) ) //NPC name
  9759. nd = npc_name2id(script_getstr(st, 2));
  9760. else //Flag
  9761. {
  9762. nd = (struct npc_data *)map_id2bl(st->oid);
  9763. flag = script_getnum(st, 2);
  9764. }
  9765. }
  9766. else
  9767. nd=(struct npc_data *)map_id2bl(st->oid);
  9768. if( !nd )
  9769. return SCRIPT_CMD_SUCCESS;
  9770. if( flag ) //Detach
  9771. nd->u.scr.rid = 0;
  9772. npc_timerevent_stop(nd);
  9773. return SCRIPT_CMD_SUCCESS;
  9774. }
  9775. /*==========================================
  9776. *------------------------------------------*/
  9777. BUILDIN_FUNC(getnpctimer)
  9778. {
  9779. struct npc_data *nd;
  9780. TBL_PC *sd;
  9781. int type = script_getnum(st,2);
  9782. t_tick val = 0;
  9783. if( script_hasdata(st,3) )
  9784. nd = npc_name2id(script_getstr(st,3));
  9785. else
  9786. nd = (struct npc_data *)map_id2bl(st->oid);
  9787. if( !nd || nd->bl.type != BL_NPC )
  9788. {
  9789. script_pushint(st,0);
  9790. ShowError("getnpctimer: Invalid NPC.\n");
  9791. return SCRIPT_CMD_FAILURE;
  9792. }
  9793. switch( type )
  9794. {
  9795. case 0: val = npc_gettimerevent_tick(nd); break;
  9796. case 1:
  9797. if( nd->u.scr.rid )
  9798. {
  9799. sd = map_id2sd(nd->u.scr.rid);
  9800. if( !sd )
  9801. {
  9802. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9803. break;
  9804. }
  9805. val = (sd->npc_timer_id != INVALID_TIMER);
  9806. }
  9807. else
  9808. val = (nd->u.scr.timerid != INVALID_TIMER);
  9809. break;
  9810. case 2: val = nd->u.scr.timeramount; break;
  9811. }
  9812. script_pushint(st,val);
  9813. return SCRIPT_CMD_SUCCESS;
  9814. }
  9815. /*==========================================
  9816. *------------------------------------------*/
  9817. BUILDIN_FUNC(setnpctimer)
  9818. {
  9819. int tick;
  9820. struct npc_data *nd;
  9821. tick = script_getnum(st,2);
  9822. if( script_hasdata(st,3) )
  9823. nd = npc_name2id(script_getstr(st,3));
  9824. else
  9825. nd = (struct npc_data *)map_id2bl(st->oid);
  9826. if( !nd || nd->bl.type != BL_NPC )
  9827. {
  9828. script_pushint(st,1);
  9829. ShowError("setnpctimer: Invalid NPC.\n");
  9830. return SCRIPT_CMD_FAILURE;
  9831. }
  9832. npc_settimerevent_tick(nd,tick);
  9833. script_pushint(st,0);
  9834. return SCRIPT_CMD_SUCCESS;
  9835. }
  9836. /*==========================================
  9837. * attaches the player rid to the timer [Celest]
  9838. *------------------------------------------*/
  9839. BUILDIN_FUNC(attachnpctimer)
  9840. {
  9841. TBL_PC *sd;
  9842. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9843. if( !nd || nd->bl.type != BL_NPC )
  9844. {
  9845. script_pushint(st,1);
  9846. ShowError("setnpctimer: Invalid NPC.\n");
  9847. return SCRIPT_CMD_FAILURE;
  9848. }
  9849. if( !script_nick2sd(2,sd) ){
  9850. script_pushint(st,1);
  9851. ShowWarning("attachnpctimer: Invalid player.\n");
  9852. return SCRIPT_CMD_FAILURE;
  9853. }
  9854. nd->u.scr.rid = sd->bl.id;
  9855. script_pushint(st,0);
  9856. return SCRIPT_CMD_SUCCESS;
  9857. }
  9858. /*==========================================
  9859. * detaches a player rid from the timer [Celest]
  9860. *------------------------------------------*/
  9861. BUILDIN_FUNC(detachnpctimer)
  9862. {
  9863. struct npc_data *nd;
  9864. if( script_hasdata(st,2) )
  9865. nd = npc_name2id(script_getstr(st,2));
  9866. else
  9867. nd = (struct npc_data *)map_id2bl(st->oid);
  9868. if( !nd || nd->bl.type != BL_NPC )
  9869. {
  9870. script_pushint(st,1);
  9871. ShowError("detachnpctimer: Invalid NPC.\n");
  9872. return SCRIPT_CMD_FAILURE;
  9873. }
  9874. nd->u.scr.rid = 0;
  9875. script_pushint(st,0);
  9876. return SCRIPT_CMD_SUCCESS;
  9877. }
  9878. /*==========================================
  9879. * To avoid "player not attached" script errors, this function is provided,
  9880. * it checks if there is a player attached to the current script. [Skotlex]
  9881. * If no, returns 0, if yes, returns the account_id of the attached player.
  9882. *------------------------------------------*/
  9883. BUILDIN_FUNC(playerattached)
  9884. {
  9885. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9886. script_pushint(st,0);
  9887. else
  9888. script_pushint(st,st->rid);
  9889. return SCRIPT_CMD_SUCCESS;
  9890. }
  9891. /*==========================================
  9892. *------------------------------------------*/
  9893. BUILDIN_FUNC(announce)
  9894. {
  9895. const char *mes = script_getstr(st,2);
  9896. int flag = script_getnum(st,3);
  9897. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9898. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9899. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9900. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9901. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9902. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9903. {
  9904. send_target target;
  9905. struct block_list *bl;
  9906. // If bc_npc flag is set, use NPC as broadcast source
  9907. if(flag&BC_NPC){
  9908. bl = map_id2bl(st->oid);
  9909. }else{
  9910. struct map_session_data* sd;
  9911. if(script_charid2sd(9, sd))
  9912. bl = &sd->bl;
  9913. else
  9914. bl = NULL;
  9915. }
  9916. if (bl == NULL)
  9917. return SCRIPT_CMD_SUCCESS;
  9918. switch (flag&BC_TARGET_MASK) {
  9919. case BC_MAP: target = ALL_SAMEMAP; break;
  9920. case BC_AREA: target = AREA; break;
  9921. case BC_SELF: target = SELF; break;
  9922. default: target = ALL_CLIENT; break; // BC_ALL
  9923. }
  9924. if (fontColor)
  9925. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9926. else
  9927. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9928. }
  9929. else
  9930. {
  9931. if (fontColor)
  9932. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9933. else
  9934. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9935. }
  9936. return SCRIPT_CMD_SUCCESS;
  9937. }
  9938. /*==========================================
  9939. *------------------------------------------*/
  9940. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9941. {
  9942. char *mes = va_arg(ap, char *);
  9943. int len = va_arg(ap, int);
  9944. int type = va_arg(ap, int);
  9945. char *fontColor = va_arg(ap, char *);
  9946. short fontType = (short)va_arg(ap, int);
  9947. short fontSize = (short)va_arg(ap, int);
  9948. short fontAlign = (short)va_arg(ap, int);
  9949. short fontY = (short)va_arg(ap, int);
  9950. if (fontColor)
  9951. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9952. else
  9953. clif_broadcast(bl, mes, len, type, SELF);
  9954. return SCRIPT_CMD_SUCCESS;
  9955. }
  9956. BUILDIN_FUNC(mapannounce)
  9957. {
  9958. const char *mapname = script_getstr(st,2);
  9959. const char *mes = script_getstr(st,3);
  9960. int flag = script_getnum(st,4);
  9961. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9962. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9963. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9964. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9965. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9966. int16 m;
  9967. if ((m = map_mapname2mapid(mapname)) < 0)
  9968. return SCRIPT_CMD_SUCCESS;
  9969. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9970. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9971. return SCRIPT_CMD_SUCCESS;
  9972. }
  9973. /*==========================================
  9974. *------------------------------------------*/
  9975. BUILDIN_FUNC(areaannounce)
  9976. {
  9977. const char *mapname = script_getstr(st,2);
  9978. int x0 = script_getnum(st,3);
  9979. int y0 = script_getnum(st,4);
  9980. int x1 = script_getnum(st,5);
  9981. int y1 = script_getnum(st,6);
  9982. const char *mes = script_getstr(st,7);
  9983. int flag = script_getnum(st,8);
  9984. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9985. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9986. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9987. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9988. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9989. int16 m;
  9990. if ((m = map_mapname2mapid(mapname)) < 0)
  9991. return SCRIPT_CMD_SUCCESS;
  9992. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9993. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9994. return SCRIPT_CMD_SUCCESS;
  9995. }
  9996. /*==========================================
  9997. *------------------------------------------*/
  9998. BUILDIN_FUNC(getusers)
  9999. {
  10000. int flag, val = 0;
  10001. struct map_session_data* sd;
  10002. struct block_list* bl = NULL;
  10003. flag = script_getnum(st,2);
  10004. switch(flag&0x07)
  10005. {
  10006. case 0:
  10007. if(flag&0x8)
  10008. {// npc
  10009. bl = map_id2bl(st->oid);
  10010. }
  10011. else if(script_rid2sd(sd))
  10012. {// pc
  10013. bl = &sd->bl;
  10014. }
  10015. if(bl)
  10016. {
  10017. val = map_getmapdata(bl->m)->users;
  10018. }
  10019. break;
  10020. case 1:
  10021. val = map_getusers();
  10022. break;
  10023. default:
  10024. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  10025. script_pushint(st,0);
  10026. return SCRIPT_CMD_FAILURE;
  10027. }
  10028. script_pushint(st,val);
  10029. return SCRIPT_CMD_SUCCESS;
  10030. }
  10031. /*==========================================
  10032. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  10033. *------------------------------------------*/
  10034. BUILDIN_FUNC(getmapguildusers)
  10035. {
  10036. const char *str;
  10037. int16 m;
  10038. int gid;
  10039. int c=0;
  10040. struct guild *g = NULL;
  10041. str=script_getstr(st,2);
  10042. gid=script_getnum(st,3);
  10043. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  10044. script_pushint(st,-1);
  10045. return SCRIPT_CMD_SUCCESS;
  10046. }
  10047. g = guild_search(gid);
  10048. if (g){
  10049. unsigned short i;
  10050. for(i = 0; i < g->max_member; i++)
  10051. {
  10052. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  10053. c++;
  10054. }
  10055. }
  10056. script_pushint(st,c);
  10057. return SCRIPT_CMD_SUCCESS;
  10058. }
  10059. /*==========================================
  10060. *------------------------------------------*/
  10061. BUILDIN_FUNC(getmapusers)
  10062. {
  10063. const char *str;
  10064. int16 m;
  10065. str=script_getstr(st,2);
  10066. if( (m=map_mapname2mapid(str))< 0){
  10067. script_pushint(st,-1);
  10068. return SCRIPT_CMD_SUCCESS;
  10069. }
  10070. script_pushint(st,map_getmapdata(m)->users);
  10071. return SCRIPT_CMD_SUCCESS;
  10072. }
  10073. /*==========================================
  10074. *------------------------------------------*/
  10075. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  10076. {
  10077. int *users=va_arg(ap,int *);
  10078. (*users)++;
  10079. return SCRIPT_CMD_SUCCESS;
  10080. }
  10081. BUILDIN_FUNC(getareausers)
  10082. {
  10083. const char *str;
  10084. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  10085. str=script_getstr(st,2);
  10086. x0=script_getnum(st,3);
  10087. y0=script_getnum(st,4);
  10088. x1=script_getnum(st,5);
  10089. y1=script_getnum(st,6);
  10090. if( (m=map_mapname2mapid(str))< 0){
  10091. script_pushint(st,-1);
  10092. return SCRIPT_CMD_SUCCESS;
  10093. }
  10094. map_foreachinallarea(buildin_getareausers_sub,
  10095. m,x0,y0,x1,y1,BL_PC,&users);
  10096. script_pushint(st,users);
  10097. return SCRIPT_CMD_SUCCESS;
  10098. }
  10099. /*==========================================
  10100. * getunits(<type>{,<array_variable>[<first value>]})
  10101. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  10102. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  10103. *------------------------------------------*/
  10104. BUILDIN_FUNC(getunits)
  10105. {
  10106. struct block_list *bl = NULL;
  10107. struct map_session_data *sd = NULL;
  10108. struct script_data *data = NULL;
  10109. char *command = (char *)script_getfuncname(st);
  10110. const char *str;
  10111. const char *name;
  10112. int type = script_getnum(st, 2);
  10113. int size = 0;
  10114. int32 idx, id;
  10115. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  10116. if (!strcmp(command, "getmapunits"))
  10117. {
  10118. str = script_getstr(st, 3);
  10119. if ((m = map_mapname2mapid(str)) < 0) {
  10120. script_pushint(st, -1);
  10121. st->state = END;
  10122. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10123. return SCRIPT_CMD_FAILURE;
  10124. }
  10125. if (script_hasdata(st, 4))
  10126. data = script_getdata(st, 4);
  10127. }
  10128. else if (!strcmp(command, "getareaunits"))
  10129. {
  10130. str = script_getstr(st, 3);
  10131. if ((m = map_mapname2mapid(str)) < 0) {
  10132. script_pushint(st, -1);
  10133. st->state = END;
  10134. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  10135. return SCRIPT_CMD_FAILURE;
  10136. }
  10137. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  10138. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  10139. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  10140. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  10141. if (script_hasdata(st, 8))
  10142. data = script_getdata(st, 8);
  10143. }
  10144. else
  10145. {
  10146. if (script_hasdata(st, 3))
  10147. data = script_getdata(st, 3);
  10148. }
  10149. if (data)
  10150. {
  10151. if (!data_isreference(data))
  10152. {
  10153. ShowError("buildin_%s: not a variable\n", command);
  10154. script_reportdata(data);
  10155. st->state = END;
  10156. return SCRIPT_CMD_FAILURE;
  10157. }
  10158. id = reference_getid(data);
  10159. idx = reference_getindex(data);
  10160. name = reference_getname(data);
  10161. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  10162. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  10163. return SCRIPT_CMD_FAILURE;
  10164. }
  10165. }
  10166. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  10167. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  10168. {
  10169. 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)))
  10170. {
  10171. if( data ){
  10172. if( is_string_variable( name ) ){
  10173. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  10174. }else{
  10175. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  10176. }
  10177. }
  10178. size++;
  10179. }
  10180. }
  10181. mapit_free(iter);
  10182. script_pushint(st, size);
  10183. return SCRIPT_CMD_SUCCESS;
  10184. }
  10185. /*==========================================
  10186. *------------------------------------------*/
  10187. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10188. {
  10189. t_itemid nameid = va_arg(ap, t_itemid);
  10190. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  10191. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10192. if(drop->item.nameid==nameid)
  10193. (*amount)+=drop->item.amount;
  10194. return SCRIPT_CMD_SUCCESS;
  10195. }
  10196. BUILDIN_FUNC(getareadropitem)
  10197. {
  10198. const char *str;
  10199. int16 m,x0,y0,x1,y1;
  10200. t_itemid nameid;
  10201. unsigned short amount = 0;
  10202. str=script_getstr(st,2);
  10203. x0=script_getnum(st,3);
  10204. y0=script_getnum(st,4);
  10205. x1=script_getnum(st,5);
  10206. y1=script_getnum(st,6);
  10207. if( script_isstring(st, 7) ){
  10208. const char *name = script_getstr(st, 7);
  10209. std::shared_ptr<item_data> item_data = item_db.searchname( name );
  10210. if( item_data )
  10211. nameid=item_data->nameid;
  10212. else{
  10213. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10214. return SCRIPT_CMD_FAILURE;
  10215. }
  10216. }else{
  10217. nameid = script_getnum(st, 7);
  10218. if( !itemdb_exists( nameid ) ){
  10219. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10220. return SCRIPT_CMD_FAILURE;
  10221. }
  10222. }
  10223. if( (m=map_mapname2mapid(str))< 0){
  10224. script_pushint(st,-1);
  10225. return SCRIPT_CMD_SUCCESS;
  10226. }
  10227. map_foreachinallarea(buildin_getareadropitem_sub,
  10228. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10229. script_pushint(st,amount);
  10230. return SCRIPT_CMD_SUCCESS;
  10231. }
  10232. /*==========================================
  10233. *------------------------------------------*/
  10234. BUILDIN_FUNC(enablenpc)
  10235. {
  10236. npc_data* nd;
  10237. e_npcv_status flag = NPCVIEW_DISABLE;
  10238. int char_id = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10239. const char* command = script_getfuncname(st);
  10240. if (script_hasdata(st, 2))
  10241. nd = npc_name2id(script_getstr(st,2));
  10242. else
  10243. nd = map_id2nd(st->oid);
  10244. if (!strcmp(command,"enablenpc"))
  10245. flag = NPCVIEW_ENABLE;
  10246. else if (!strcmp(command,"disablenpc"))
  10247. flag = NPCVIEW_DISABLE;
  10248. else if (!strcmp(command,"hideoffnpc"))
  10249. flag = NPCVIEW_HIDEOFF;
  10250. else if (!strcmp(command,"hideonnpc"))
  10251. flag = NPCVIEW_HIDEON;
  10252. else if (!strcmp(command,"cloakoffnpc"))
  10253. flag = NPCVIEW_CLOAKOFF;
  10254. else if (!strcmp(command,"cloakonnpc"))
  10255. flag = NPCVIEW_CLOAKON;
  10256. else{
  10257. ShowError( "buildin_enablenpc: Undefined command \"%s\".\n", command );
  10258. return SCRIPT_CMD_FAILURE;
  10259. }
  10260. if (!nd) {
  10261. if (script_hasdata(st, 2))
  10262. 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);
  10263. else
  10264. ShowError("buildin_%s: Attempted to %s a non-existing NPC (flag=%d).\n", (flag & NPCVIEW_VISIBLE) ? "show" : "hide", command, flag);
  10265. return SCRIPT_CMD_FAILURE;
  10266. }
  10267. if (npc_enable_target(*nd, char_id, flag))
  10268. return SCRIPT_CMD_SUCCESS;
  10269. return SCRIPT_CMD_FAILURE;
  10270. }
  10271. /* Starts a status effect on the target unit or on the attached player.
  10272. *
  10273. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10274. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10275. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10276. * <flag>: enum e_status_change_start_flags
  10277. */
  10278. BUILDIN_FUNC(sc_start)
  10279. {
  10280. TBL_NPC * nd = map_id2nd(st->oid);
  10281. struct block_list* bl;
  10282. enum sc_type type;
  10283. int tick, val1, val2, val3, val4=0, rate, flag;
  10284. char start_type;
  10285. const char* command = script_getfuncname(st);
  10286. if(strstr(command, "4"))
  10287. start_type = 4;
  10288. else if(strstr(command, "2"))
  10289. start_type = 2;
  10290. else
  10291. start_type = 1;
  10292. type = (sc_type)script_getnum(st,2);
  10293. tick = script_getnum(st,3);
  10294. val1 = script_getnum(st,4);
  10295. //If from NPC we make default flag 1 to be unavoidable
  10296. if(nd && nd->bl.id == fake_nd->bl.id)
  10297. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10298. else
  10299. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10300. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10301. if(script_hasdata(st,(6+start_type)))
  10302. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10303. else
  10304. bl = map_id2bl(st->rid);
  10305. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  10306. {// When there isn't a duration specified, try to get it from the skill_db
  10307. tick = skill_get_time(status_sc2skill(type), val1);
  10308. }
  10309. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10310. bl = map_id2bl(potion_target);
  10311. tick /= 2;// Thrown potions only last half.
  10312. val4 = 1;// Mark that this was a thrown sc_effect
  10313. }
  10314. if(!bl)
  10315. return SCRIPT_CMD_SUCCESS;
  10316. switch(start_type) {
  10317. case 1:
  10318. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10319. break;
  10320. case 2:
  10321. val2 = script_getnum(st,5);
  10322. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10323. break;
  10324. case 4:
  10325. val2 = script_getnum(st,5);
  10326. val3 = script_getnum(st,6);
  10327. val4 = script_getnum(st,7);
  10328. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10329. break;
  10330. }
  10331. return SCRIPT_CMD_SUCCESS;
  10332. }
  10333. /// Ends one or all status effects on the target unit or on the attached player.
  10334. ///
  10335. /// sc_end <effect_id>{,<unit_id>};
  10336. BUILDIN_FUNC(sc_end)
  10337. {
  10338. struct block_list* bl;
  10339. int type;
  10340. type = script_getnum(st, 2);
  10341. if (script_hasdata(st, 3))
  10342. bl = map_id2bl(script_getnum(st, 3));
  10343. else
  10344. bl = map_id2bl(st->rid);
  10345. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10346. bl = map_id2bl(potion_target);
  10347. if (!bl)
  10348. return SCRIPT_CMD_SUCCESS;
  10349. if (type >= 0 && type < SC_MAX) {
  10350. struct status_change *sc = status_get_sc(bl);
  10351. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  10352. if (!sce)
  10353. return SCRIPT_CMD_SUCCESS;
  10354. switch (type) {
  10355. case SC_WEIGHT50:
  10356. case SC_WEIGHT90:
  10357. case SC_NOCHAT:
  10358. case SC_PUSH_CART:
  10359. case SC_ALL_RIDING:
  10360. case SC_STYLE_CHANGE:
  10361. case SC_MONSTER_TRANSFORM:
  10362. case SC_ACTIVE_MONSTER_TRANSFORM:
  10363. case SC_MTF_ASPD:
  10364. case SC_MTF_RANGEATK:
  10365. case SC_MTF_MATK:
  10366. case SC_MTF_MLEATKED:
  10367. case SC_MTF_CRIDAMAGE:
  10368. case SC_MTF_ASPD2:
  10369. case SC_MTF_RANGEATK2:
  10370. case SC_MTF_MATK2:
  10371. case SC_MTF_MHP:
  10372. case SC_MTF_MSP:
  10373. case SC_MTF_PUMPKIN:
  10374. case SC_MTF_HITFLEE:
  10375. case SC_ATTHASTE_CASH:
  10376. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  10377. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  10378. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  10379. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  10380. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  10381. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  10382. return SCRIPT_CMD_SUCCESS;
  10383. default:
  10384. break;
  10385. }
  10386. //This should help status_change_end force disabling the SC in case it has no limit.
  10387. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10388. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  10389. } else
  10390. status_change_clear(bl, 3); // remove all effects
  10391. return SCRIPT_CMD_SUCCESS;
  10392. }
  10393. /**
  10394. * Ends all status effects from any learned skill on the attached player.
  10395. * if <job_id> was given it will end the effect of that class for the attached player
  10396. * sc_end_class {<char_id>{,<job_id>}};
  10397. */
  10398. BUILDIN_FUNC(sc_end_class)
  10399. {
  10400. struct map_session_data *sd;
  10401. uint16 skill_id;
  10402. int class_;
  10403. if (!script_charid2sd(2, sd))
  10404. return SCRIPT_CMD_FAILURE;
  10405. if (script_hasdata(st, 3))
  10406. class_ = script_getnum(st, 3);
  10407. else
  10408. class_ = sd->status.class_;
  10409. if (!pcdb_checkid(class_)) {
  10410. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10411. return SCRIPT_CMD_FAILURE;
  10412. }
  10413. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(class_);
  10414. if( tree != nullptr ){
  10415. for (const auto &it : tree->skills) {
  10416. skill_id = it.first;
  10417. enum sc_type sc = status_skill2sc(skill_id);
  10418. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  10419. status_change_end(&sd->bl, sc, INVALID_TIMER);
  10420. }
  10421. }
  10422. return SCRIPT_CMD_SUCCESS;
  10423. }
  10424. /*==========================================
  10425. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10426. *------------------------------------------*/
  10427. BUILDIN_FUNC(getscrate)
  10428. {
  10429. struct block_list *bl;
  10430. int type;
  10431. t_tick rate;
  10432. type=script_getnum(st,2);
  10433. rate=script_getnum(st,3);
  10434. if( script_hasdata(st,4) ) //get for the bl assigned
  10435. bl = map_id2bl(script_getnum(st,4));
  10436. else
  10437. bl = map_id2bl(st->rid);
  10438. if (bl)
  10439. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10440. script_pushint(st,rate);
  10441. return SCRIPT_CMD_SUCCESS;
  10442. }
  10443. /**
  10444. * getstatus(<effect type>{,<type>{,<char_id>}});
  10445. **/
  10446. BUILDIN_FUNC(getstatus)
  10447. {
  10448. int id, type;
  10449. struct map_session_data* sd;
  10450. if (!script_charid2sd(4,sd))
  10451. return SCRIPT_CMD_FAILURE;
  10452. id = script_getnum(st, 2);
  10453. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10454. if( id <= SC_NONE || id >= SC_MAX )
  10455. {// invalid status type given
  10456. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10457. return SCRIPT_CMD_SUCCESS;
  10458. }
  10459. if( sd->sc.count == 0 || !sd->sc.data[id] )
  10460. {// no status is active
  10461. script_pushint(st, 0);
  10462. return SCRIPT_CMD_SUCCESS;
  10463. }
  10464. switch( type )
  10465. {
  10466. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  10467. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  10468. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  10469. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  10470. case 5:
  10471. {
  10472. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  10473. if( timer )
  10474. {// return the amount of time remaining
  10475. script_pushint(st, timer->tick - gettick());
  10476. } else {
  10477. script_pushint(st, -1);
  10478. }
  10479. }
  10480. break;
  10481. default: script_pushint(st, 1); break;
  10482. }
  10483. return SCRIPT_CMD_SUCCESS;
  10484. }
  10485. /*==========================================
  10486. *
  10487. *------------------------------------------*/
  10488. BUILDIN_FUNC(debugmes)
  10489. {
  10490. const char *str;
  10491. str=script_getstr(st,2);
  10492. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10493. return SCRIPT_CMD_SUCCESS;
  10494. }
  10495. BUILDIN_FUNC( errormes ){
  10496. ShowError( "%s\n", script_getstr( st, 2 ) );
  10497. script_reportsrc( st );
  10498. return SCRIPT_CMD_SUCCESS;
  10499. }
  10500. /*==========================================
  10501. *------------------------------------------*/
  10502. BUILDIN_FUNC(catchpet)
  10503. {
  10504. int pet_id;
  10505. TBL_PC *sd;
  10506. if( !script_rid2sd(sd) )
  10507. return SCRIPT_CMD_SUCCESS;
  10508. pet_id= script_getnum(st,2);
  10509. pet_catch_process1(sd,pet_id);
  10510. return SCRIPT_CMD_SUCCESS;
  10511. }
  10512. /*==========================================
  10513. * [orn]
  10514. *------------------------------------------*/
  10515. BUILDIN_FUNC(homunculus_evolution)
  10516. {
  10517. TBL_PC *sd;
  10518. if( !script_rid2sd(sd) )
  10519. return SCRIPT_CMD_SUCCESS;
  10520. if(hom_is_active(sd->hd))
  10521. {
  10522. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10523. hom_evolution(sd->hd);
  10524. else
  10525. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10526. }
  10527. return SCRIPT_CMD_SUCCESS;
  10528. }
  10529. /*==========================================
  10530. * Checks for vaporized morph state
  10531. * and deletes ITEMID_STRANGE_EMBRYO.
  10532. *------------------------------------------*/
  10533. BUILDIN_FUNC(homunculus_mutate)
  10534. {
  10535. int homun_id;
  10536. TBL_PC *sd;
  10537. if( !script_rid2sd(sd) || sd->hd == NULL )
  10538. return SCRIPT_CMD_SUCCESS;
  10539. if(script_hasdata(st,2))
  10540. homun_id = script_getnum(st,2);
  10541. else
  10542. homun_id = 6048 + (rnd() % 4);
  10543. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10544. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10545. int m_id = hom_class2mapid(homun_id);
  10546. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10547. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10548. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10549. hom_call(sd); // Respawn homunculus.
  10550. hom_mutate(sd->hd, homun_id);
  10551. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10552. script_pushint(st, 1);
  10553. return SCRIPT_CMD_SUCCESS;
  10554. } else
  10555. clif_emotion(&sd->bl, ET_SWEAT);
  10556. } else
  10557. clif_emotion(&sd->bl, ET_SWEAT);
  10558. script_pushint(st, 0);
  10559. return SCRIPT_CMD_SUCCESS;
  10560. }
  10561. /*==========================================
  10562. * Puts homunculus into morph state
  10563. * and gives ITEMID_STRANGE_EMBRYO.
  10564. *------------------------------------------*/
  10565. BUILDIN_FUNC(morphembryo)
  10566. {
  10567. struct item item_tmp;
  10568. TBL_PC *sd;
  10569. if( !script_rid2sd(sd) || sd->hd == NULL )
  10570. return SCRIPT_CMD_SUCCESS;
  10571. if( hom_is_active(sd->hd) ) {
  10572. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10573. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10574. char i;
  10575. memset(&item_tmp, 0, sizeof(item_tmp));
  10576. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10577. item_tmp.identify = 1;
  10578. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10579. clif_additem(sd, 0, 0, i);
  10580. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10581. } else {
  10582. hom_vaporize(sd, HOM_ST_MORPH);
  10583. script_pushint(st, 1);
  10584. return SCRIPT_CMD_SUCCESS;
  10585. }
  10586. } else
  10587. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10588. } else
  10589. clif_emotion(&sd->bl, ET_SWEAT);
  10590. script_pushint(st, 0);
  10591. return SCRIPT_CMD_SUCCESS;
  10592. }
  10593. // [Zephyrus]
  10594. BUILDIN_FUNC(homunculus_shuffle)
  10595. {
  10596. TBL_PC *sd;
  10597. if( !script_rid2sd(sd) )
  10598. return SCRIPT_CMD_SUCCESS;
  10599. if(hom_is_active(sd->hd))
  10600. hom_shuffle(sd->hd);
  10601. return SCRIPT_CMD_SUCCESS;
  10602. }
  10603. /*==========================================
  10604. * Check for homunculus state.
  10605. * Return: -1 = No homunculus
  10606. * 0 = Homunculus is active
  10607. * 1 = Homunculus is vaporized (rest)
  10608. * 2 = Homunculus is in morph state
  10609. *------------------------------------------*/
  10610. BUILDIN_FUNC(checkhomcall)
  10611. {
  10612. TBL_PC *sd;
  10613. TBL_HOM *hd;
  10614. if( !script_rid2sd(sd) )
  10615. return SCRIPT_CMD_SUCCESS;
  10616. hd = sd->hd;
  10617. if( !hd )
  10618. script_pushint(st, -1);
  10619. else
  10620. script_pushint(st, hd->homunculus.vaporize);
  10621. return SCRIPT_CMD_SUCCESS;
  10622. }
  10623. //These two functions bring the eA MAPID_* class functionality to scripts.
  10624. BUILDIN_FUNC(eaclass)
  10625. {
  10626. int class_;
  10627. if( script_hasdata(st,2) )
  10628. class_ = script_getnum(st,2);
  10629. else {
  10630. TBL_PC *sd;
  10631. if (!script_charid2sd(3,sd)) {
  10632. script_pushint(st,-1);
  10633. return SCRIPT_CMD_SUCCESS;
  10634. }
  10635. class_ = sd->status.class_;
  10636. }
  10637. script_pushint(st,pc_jobid2mapid(class_));
  10638. return SCRIPT_CMD_SUCCESS;
  10639. }
  10640. BUILDIN_FUNC(roclass)
  10641. {
  10642. int class_ =script_getnum(st,2);
  10643. int sex;
  10644. if( script_hasdata(st,3) )
  10645. sex = script_getnum(st,3);
  10646. else {
  10647. TBL_PC *sd;
  10648. if (st->rid && script_rid2sd(sd))
  10649. sex = sd->status.sex;
  10650. else
  10651. sex = SEX_MALE; //Just use male when not found.
  10652. }
  10653. script_pushint(st,pc_mapid2jobid(class_, sex));
  10654. return SCRIPT_CMD_SUCCESS;
  10655. }
  10656. /*==========================================
  10657. * Tells client to open a hatching window, used for pet incubator
  10658. *------------------------------------------*/
  10659. BUILDIN_FUNC(birthpet)
  10660. {
  10661. TBL_PC *sd;
  10662. if( !script_rid2sd(sd) )
  10663. return SCRIPT_CMD_SUCCESS;
  10664. if( sd->status.pet_id )
  10665. {// do not send egg list, when you already have a pet
  10666. return SCRIPT_CMD_SUCCESS;
  10667. }
  10668. clif_sendegg(sd);
  10669. return SCRIPT_CMD_SUCCESS;
  10670. }
  10671. /**
  10672. * resetlvl <action type>{,<char_id>};
  10673. * @param action_type:
  10674. * 1 : make like after rebirth
  10675. * 2 : blvl,jlvl=1, skillpoint=0
  10676. * 3 : don't reset skill, blvl=1
  10677. * 4 : jlvl=0
  10678. * @author AppleGirl
  10679. **/
  10680. BUILDIN_FUNC(resetlvl)
  10681. {
  10682. TBL_PC *sd;
  10683. int type=script_getnum(st,2);
  10684. if (!script_charid2sd(3,sd))
  10685. return SCRIPT_CMD_FAILURE;
  10686. pc_resetlvl(sd,type);
  10687. return SCRIPT_CMD_SUCCESS;
  10688. }
  10689. /**
  10690. * Reset a player status point
  10691. * resetstatus({<char_id>});
  10692. **/
  10693. BUILDIN_FUNC(resetstatus)
  10694. {
  10695. TBL_PC *sd;
  10696. if (!script_charid2sd(2,sd))
  10697. return SCRIPT_CMD_FAILURE;
  10698. pc_resetstate(sd);
  10699. return SCRIPT_CMD_SUCCESS;
  10700. }
  10701. /**
  10702. * Reset player's skill
  10703. * resetskill({<char_id>});
  10704. **/
  10705. BUILDIN_FUNC(resetskill)
  10706. {
  10707. TBL_PC *sd;
  10708. if (!script_charid2sd(2,sd))
  10709. return SCRIPT_CMD_FAILURE;
  10710. pc_resetskill(sd,1);
  10711. return SCRIPT_CMD_SUCCESS;
  10712. }
  10713. /**
  10714. * Reset SG designated maps
  10715. * resetfeel({<char_id>});
  10716. **/
  10717. BUILDIN_FUNC(resetfeel)
  10718. {
  10719. TBL_PC *sd;
  10720. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10721. return SCRIPT_CMD_FAILURE;
  10722. pc_resetfeel(sd);
  10723. return SCRIPT_CMD_SUCCESS;
  10724. }
  10725. /**
  10726. * Reset SG designated monsters
  10727. * resethate({<char_id>});
  10728. **/
  10729. BUILDIN_FUNC(resethate)
  10730. {
  10731. TBL_PC *sd;
  10732. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10733. return SCRIPT_CMD_FAILURE;
  10734. pc_resethate(sd);
  10735. return SCRIPT_CMD_SUCCESS;
  10736. }
  10737. /**
  10738. * Counts total amount of skill points.
  10739. * skillpointcount({<char_id>})
  10740. **/
  10741. BUILDIN_FUNC(skillpointcount)
  10742. {
  10743. TBL_PC *sd;
  10744. if (!script_charid2sd(2,sd))
  10745. return SCRIPT_CMD_FAILURE;
  10746. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10747. return SCRIPT_CMD_SUCCESS;
  10748. }
  10749. /*==========================================
  10750. *
  10751. *------------------------------------------*/
  10752. BUILDIN_FUNC(changebase)
  10753. {
  10754. TBL_PC *sd=NULL;
  10755. int vclass;
  10756. if( !script_mapid2sd(3,sd) )
  10757. return SCRIPT_CMD_SUCCESS;
  10758. vclass = script_getnum(st,2);
  10759. if(vclass == JOB_WEDDING)
  10760. {
  10761. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10762. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10763. )
  10764. return SCRIPT_CMD_SUCCESS;
  10765. }
  10766. if(!sd->disguise && vclass != sd->vd.class_) {
  10767. status_set_viewdata(&sd->bl, vclass);
  10768. //Updated client view. Base, Weapon and Cloth Colors.
  10769. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10770. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10771. if (sd->vd.cloth_color)
  10772. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10773. if (sd->vd.body_style)
  10774. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10775. clif_skillinfoblock(sd);
  10776. }
  10777. return SCRIPT_CMD_SUCCESS;
  10778. }
  10779. /**
  10780. * Change account sex and unequip all item and request for a changesex to char-serv
  10781. * changesex({<char_id>});
  10782. */
  10783. BUILDIN_FUNC(changesex)
  10784. {
  10785. int i;
  10786. TBL_PC *sd = NULL;
  10787. if (!script_charid2sd(2,sd))
  10788. return SCRIPT_CMD_FAILURE;
  10789. pc_resetskill(sd,4);
  10790. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10791. for(i = 0; i < EQI_MAX; i++) {
  10792. if (sd->equip_index[i] >= 0)
  10793. pc_unequipitem(sd, sd->equip_index[i], 3);
  10794. }
  10795. chrif_changesex(sd, true);
  10796. return SCRIPT_CMD_SUCCESS;
  10797. }
  10798. /**
  10799. * Change character's sex and unequip all item and request for a changesex to char-serv
  10800. * changecharsex({<char_id>});
  10801. */
  10802. BUILDIN_FUNC(changecharsex)
  10803. {
  10804. #if PACKETVER >= 20141016
  10805. int i;
  10806. TBL_PC *sd = NULL;
  10807. if (!script_charid2sd(2,sd))
  10808. return SCRIPT_CMD_FAILURE;
  10809. pc_resetskill(sd,4);
  10810. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10811. for (i = 0; i < EQI_MAX; i++) {
  10812. if (sd->equip_index[i] >= 0)
  10813. pc_unequipitem(sd, sd->equip_index[i], 3);
  10814. }
  10815. chrif_changesex(sd, false);
  10816. return SCRIPT_CMD_SUCCESS;
  10817. #else
  10818. return SCRIPT_CMD_FAILURE;
  10819. #endif
  10820. }
  10821. /*==========================================
  10822. * Works like 'announce' but outputs in the common chat window
  10823. *------------------------------------------*/
  10824. BUILDIN_FUNC(globalmes)
  10825. {
  10826. struct block_list *bl = map_id2bl(st->oid);
  10827. struct npc_data *nd = (struct npc_data *)bl;
  10828. const char *name=NULL,*mes;
  10829. mes=script_getstr(st,2);
  10830. if(mes==NULL)
  10831. return SCRIPT_CMD_SUCCESS;
  10832. if(script_hasdata(st,3)){ // npc name to display
  10833. name=script_getstr(st,3);
  10834. } else {
  10835. name=nd->name; //use current npc name
  10836. }
  10837. npc_globalmessage(name,mes); // broadcast to all players connected
  10838. return SCRIPT_CMD_SUCCESS;
  10839. }
  10840. /////////////////////////////////////////////////////////////////////
  10841. // NPC waiting room (chat room)
  10842. //
  10843. /// Creates a waiting room (chat room) for this npc.
  10844. ///
  10845. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10846. BUILDIN_FUNC(waitingroom)
  10847. {
  10848. struct npc_data* nd;
  10849. const char* title = script_getstr(st, 2);
  10850. int limit = script_getnum(st, 3);
  10851. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10852. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10853. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10854. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10855. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10856. nd = (struct npc_data *)map_id2bl(st->oid);
  10857. if( nd != NULL )
  10858. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10859. return SCRIPT_CMD_SUCCESS;
  10860. }
  10861. /// Removes the waiting room of the current or target npc.
  10862. ///
  10863. /// delwaitingroom "<npc_name>";
  10864. /// delwaitingroom;
  10865. BUILDIN_FUNC(delwaitingroom)
  10866. {
  10867. struct npc_data* nd;
  10868. if( script_hasdata(st,2) )
  10869. nd = npc_name2id(script_getstr(st, 2));
  10870. else
  10871. nd = (struct npc_data *)map_id2bl(st->oid);
  10872. if( nd != NULL )
  10873. chat_deletenpcchat(nd);
  10874. return SCRIPT_CMD_SUCCESS;
  10875. }
  10876. /// Kick the specified player from the waiting room of the target npc.
  10877. ///
  10878. /// waitingroomkick "<npc_name>", <kickusername>;
  10879. BUILDIN_FUNC(waitingroomkick)
  10880. {
  10881. struct npc_data* nd;
  10882. struct chat_data* cd;
  10883. const char* kickusername;
  10884. nd = npc_name2id(script_getstr(st,2));
  10885. kickusername = script_getstr(st,3);
  10886. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10887. chat_npckickchat(cd, kickusername);
  10888. return SCRIPT_CMD_SUCCESS;
  10889. }
  10890. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10891. /// Num users stored in .@waitingroom_usercount
  10892. ///
  10893. /// getwaitingroomusers "<npc_name>";
  10894. BUILDIN_FUNC(getwaitingroomusers)
  10895. {
  10896. struct npc_data* nd;
  10897. struct chat_data* cd;
  10898. int i, j=0;
  10899. if( script_hasdata(st,2) )
  10900. nd = npc_name2id(script_getstr(st, 2));
  10901. else
  10902. nd = (struct npc_data *)map_id2bl(st->oid);
  10903. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10904. for(i = 0; i < cd->users; ++i) {
  10905. setd_sub_num( st, NULL, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, NULL );
  10906. j++;
  10907. }
  10908. setd_sub_num( st, NULL, ".@waitingroom_usercount", 0, j, NULL );
  10909. }
  10910. return SCRIPT_CMD_SUCCESS;
  10911. }
  10912. /// Kicks all the players from the waiting room of the current or target npc.
  10913. ///
  10914. /// kickwaitingroomall "<npc_name>";
  10915. /// kickwaitingroomall;
  10916. BUILDIN_FUNC(waitingroomkickall)
  10917. {
  10918. struct npc_data* nd;
  10919. struct chat_data* cd;
  10920. if( script_hasdata(st,2) )
  10921. nd = npc_name2id(script_getstr(st,2));
  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. chat_npckickall(cd);
  10926. return SCRIPT_CMD_SUCCESS;
  10927. }
  10928. /// Enables the waiting room event of the current or target npc.
  10929. ///
  10930. /// enablewaitingroomevent "<npc_name>";
  10931. /// enablewaitingroomevent;
  10932. BUILDIN_FUNC(enablewaitingroomevent)
  10933. {
  10934. struct npc_data* nd;
  10935. struct chat_data* cd;
  10936. if( script_hasdata(st,2) )
  10937. nd = npc_name2id(script_getstr(st, 2));
  10938. else
  10939. nd = (struct npc_data *)map_id2bl(st->oid);
  10940. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10941. chat_enableevent(cd);
  10942. return SCRIPT_CMD_SUCCESS;
  10943. }
  10944. /// Disables the waiting room event of the current or target npc.
  10945. ///
  10946. /// disablewaitingroomevent "<npc_name>";
  10947. /// disablewaitingroomevent;
  10948. BUILDIN_FUNC(disablewaitingroomevent)
  10949. {
  10950. struct npc_data *nd;
  10951. struct chat_data *cd;
  10952. if( script_hasdata(st,2) )
  10953. nd = npc_name2id(script_getstr(st, 2));
  10954. else
  10955. nd = (struct npc_data *)map_id2bl(st->oid);
  10956. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10957. chat_disableevent(cd);
  10958. return SCRIPT_CMD_SUCCESS;
  10959. }
  10960. /// Returns info on the waiting room of the current or target npc.
  10961. /// Returns -1 if the type unknown
  10962. /// <type>=0 : current number of users
  10963. /// <type>=1 : maximum number of users allowed
  10964. /// <type>=2 : the number of users that trigger the event
  10965. /// <type>=3 : if the trigger is disabled
  10966. /// <type>=4 : the title of the waiting room
  10967. /// <type>=5 : the password of the waiting room
  10968. /// <type>=16 : the name of the waiting room event
  10969. /// <type>=32 : if the waiting room is full
  10970. /// <type>=33 : if there are enough users to trigger the event
  10971. ///
  10972. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10973. /// getwaitingroomstate(<type>) -> <info>
  10974. BUILDIN_FUNC(getwaitingroomstate)
  10975. {
  10976. struct npc_data *nd;
  10977. struct chat_data *cd;
  10978. int type;
  10979. type = script_getnum(st,2);
  10980. if( script_hasdata(st,3) )
  10981. nd = npc_name2id(script_getstr(st, 3));
  10982. else
  10983. nd = (struct npc_data *)map_id2bl(st->oid);
  10984. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10985. {
  10986. script_pushint(st, -1);
  10987. return SCRIPT_CMD_SUCCESS;
  10988. }
  10989. switch(type)
  10990. {
  10991. case 0: script_pushint(st, cd->users); break;
  10992. case 1: script_pushint(st, cd->limit); break;
  10993. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10994. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10995. case 4: script_pushstrcopy(st, cd->title); break;
  10996. case 5: script_pushstrcopy(st, cd->pass); break;
  10997. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10998. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10999. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  11000. default: script_pushint(st, -1); break;
  11001. }
  11002. return SCRIPT_CMD_SUCCESS;
  11003. }
  11004. /// Warps the trigger or target amount of players to the target map and position.
  11005. /// Players are automatically removed from the waiting room.
  11006. /// Those waiting the longest will get warped first.
  11007. /// The target map can be "Random" for a random position in the current map,
  11008. /// and "SavePoint" for the savepoint map+position.
  11009. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  11010. ///
  11011. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  11012. /// The total number of teleported players is put into $@warpwaitingpcnum
  11013. ///
  11014. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  11015. /// warpwaitingpc "<map name>",<x>,<y>;
  11016. BUILDIN_FUNC(warpwaitingpc)
  11017. {
  11018. int x;
  11019. int y;
  11020. int i;
  11021. int n;
  11022. const char* map_name;
  11023. struct npc_data* nd;
  11024. struct chat_data* cd;
  11025. nd = (struct npc_data *)map_id2bl(st->oid);
  11026. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11027. return SCRIPT_CMD_SUCCESS;
  11028. map_name = script_getstr(st,2);
  11029. x = script_getnum(st,3);
  11030. y = script_getnum(st,4);
  11031. n = cd->trigger&0x7f;
  11032. if( script_hasdata(st,5) )
  11033. n = script_getnum(st,5);
  11034. for( i = 0; i < n && cd->users > 0; i++ )
  11035. {
  11036. TBL_PC* sd = cd->usersd[0];
  11037. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  11038. {// can't teleport on this map
  11039. break;
  11040. }
  11041. if( cd->zeny )
  11042. {// fee set
  11043. if( (uint32)sd->status.zeny < cd->zeny )
  11044. {// no zeny to cover set fee
  11045. break;
  11046. }
  11047. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  11048. }
  11049. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  11050. if( strcmp(map_name,"Random") == 0 )
  11051. pc_randomwarp(sd,CLR_TELEPORT,true);
  11052. else if( strcmp(map_name,"SavePoint") == 0 )
  11053. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  11054. else
  11055. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  11056. }
  11057. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  11058. return SCRIPT_CMD_SUCCESS;
  11059. }
  11060. /////////////////////////////////////////////////////////////////////
  11061. // ...
  11062. //
  11063. /// Detaches a character from a script.
  11064. ///
  11065. /// @param st Script state to detach the character from.
  11066. void script_detach_rid(struct script_state* st)
  11067. {
  11068. if(st->rid)
  11069. {
  11070. script_detach_state(st, false);
  11071. st->rid = 0;
  11072. }
  11073. }
  11074. /*=========================================================================
  11075. * Attaches a set of RIDs to the current script. [digitalhamster]
  11076. * addrid(<type>{,<flag>{,<parameters>}});
  11077. * <type>:
  11078. * 0 : All players in the server.
  11079. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  11080. * 2 : Party members of a specified party ID.
  11081. * [ Parameters: <party id> ]
  11082. * 3 : Guild members of a specified guild ID.
  11083. * [ Parameters: <guild id> ]
  11084. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  11085. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  11086. * 5 : All players in the map.
  11087. * [ Parameters: "<map name>" ]
  11088. * Account ID: The specified account ID.
  11089. * <flag>:
  11090. * 0 : Players are always attached. (default)
  11091. * 1 : Players currently running another script will not be attached.
  11092. *-------------------------------------------------------------------------*/
  11093. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  11094. {
  11095. int forceflag;
  11096. struct map_session_data *sd = (TBL_PC *)bl;
  11097. struct script_state* st;
  11098. st = va_arg(ap,struct script_state*);
  11099. forceflag = va_arg(ap,int);
  11100. if(!forceflag || !sd->st)
  11101. if(sd->status.account_id != st->rid)
  11102. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11103. return 0;
  11104. }
  11105. BUILDIN_FUNC(addrid)
  11106. {
  11107. struct s_mapiterator* iter;
  11108. struct block_list *bl;
  11109. TBL_PC *sd;
  11110. if(st->rid < 1) {
  11111. st->state = END;
  11112. bl = map_id2bl(st->oid);
  11113. } else
  11114. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  11115. iter = mapit_getallusers();
  11116. switch(script_getnum(st,2)) {
  11117. case 0:
  11118. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11119. if(!script_getnum(st,3) || !sd->st)
  11120. if(sd->status.account_id != st->rid) //attached player already runs.
  11121. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11122. }
  11123. break;
  11124. case 1:
  11125. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11126. if(!script_getnum(st,3) || !sd->st)
  11127. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  11128. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11129. }
  11130. break;
  11131. case 2:
  11132. if(script_getnum(st,4) == 0) {
  11133. script_pushint(st,0);
  11134. mapit_free(iter);
  11135. return SCRIPT_CMD_SUCCESS;
  11136. }
  11137. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11138. if(!script_getnum(st,3) || !sd->st)
  11139. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  11140. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11141. }
  11142. break;
  11143. case 3:
  11144. if(script_getnum(st,4) == 0) {
  11145. script_pushint(st,0);
  11146. mapit_free(iter);
  11147. return SCRIPT_CMD_SUCCESS;
  11148. }
  11149. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11150. if(!script_getnum(st,3) || !sd->st)
  11151. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  11152. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  11153. }
  11154. break;
  11155. case 4:
  11156. map_foreachinallarea(buildin_addrid_sub,
  11157. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  11158. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  11159. break;
  11160. case 5:
  11161. if (script_getstr(st, 4) == NULL) {
  11162. script_pushint(st, 0);
  11163. mapit_free(iter);
  11164. return SCRIPT_CMD_FAILURE;
  11165. }
  11166. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  11167. script_pushint(st, 0);
  11168. mapit_free(iter);
  11169. return SCRIPT_CMD_FAILURE;
  11170. }
  11171. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  11172. break;
  11173. default:
  11174. mapit_free(iter);
  11175. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  11176. script_pushint(st,0);
  11177. return SCRIPT_CMD_SUCCESS;
  11178. }
  11179. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  11180. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  11181. script_pushint(st,1);
  11182. }
  11183. return SCRIPT_CMD_SUCCESS;
  11184. }
  11185. mapit_free(iter);
  11186. script_pushint(st,1);
  11187. return SCRIPT_CMD_SUCCESS;
  11188. }
  11189. /*==========================================
  11190. * Attach sd char id to script and detach current one if any
  11191. *------------------------------------------*/
  11192. BUILDIN_FUNC(attachrid)
  11193. {
  11194. int rid = script_getnum(st,2);
  11195. bool force;
  11196. if( script_hasdata(st,3) ){
  11197. force = script_getnum(st,3) != 0;
  11198. }else{
  11199. force = true;
  11200. }
  11201. struct map_session_data* sd = map_id2sd(rid);
  11202. if( sd != NULL && ( !sd->npc_id || force ) ){
  11203. script_detach_rid(st);
  11204. st->rid = rid;
  11205. script_attach_state(st);
  11206. script_pushint(st,true);
  11207. }else{
  11208. script_pushint(st,false);
  11209. }
  11210. return SCRIPT_CMD_SUCCESS;
  11211. }
  11212. /*==========================================
  11213. * Detach script to rid
  11214. *------------------------------------------*/
  11215. BUILDIN_FUNC(detachrid)
  11216. {
  11217. script_detach_rid(st);
  11218. return SCRIPT_CMD_SUCCESS;
  11219. }
  11220. /*==========================================
  11221. * Chk if account connected, (and charid from account if specified)
  11222. *------------------------------------------*/
  11223. BUILDIN_FUNC(isloggedin)
  11224. {
  11225. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11226. if (script_hasdata(st,3) && sd &&
  11227. sd->status.char_id != script_getnum(st,3))
  11228. sd = NULL;
  11229. push_val(st->stack,C_INT,sd!=NULL);
  11230. return SCRIPT_CMD_SUCCESS;
  11231. }
  11232. /*==========================================
  11233. *
  11234. *------------------------------------------*/
  11235. BUILDIN_FUNC(setmapflagnosave)
  11236. {
  11237. int16 m,x,y;
  11238. unsigned short mapindex;
  11239. const char *str,*str2;
  11240. union u_mapflag_args args = {};
  11241. str=script_getstr(st,2);
  11242. str2=script_getstr(st,3);
  11243. x=script_getnum(st,4);
  11244. y=script_getnum(st,5);
  11245. m = map_mapname2mapid(str);
  11246. mapindex = mapindex_name2id(str2);
  11247. if(m < 0) {
  11248. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11249. return SCRIPT_CMD_FAILURE;
  11250. }
  11251. args.nosave.map = mapindex;
  11252. args.nosave.x = x;
  11253. args.nosave.y = y;
  11254. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11255. return SCRIPT_CMD_SUCCESS;
  11256. }
  11257. BUILDIN_FUNC(getmapflag)
  11258. {
  11259. int16 m;
  11260. int mf;
  11261. const char *str;
  11262. str=script_getstr(st,2);
  11263. m = map_mapname2mapid(str);
  11264. if (m < 0) {
  11265. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11266. return SCRIPT_CMD_FAILURE;
  11267. }
  11268. mf = script_getnum(st, 3);
  11269. if( mf < MF_MIN || mf > MF_MAX ){
  11270. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11271. return SCRIPT_CMD_FAILURE;
  11272. }
  11273. union u_mapflag_args args = {};
  11274. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11275. args.flag_val = SKILLDMG_MAX;
  11276. else
  11277. FETCH(4, args.flag_val);
  11278. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11279. return SCRIPT_CMD_SUCCESS;
  11280. }
  11281. BUILDIN_FUNC(setmapflag)
  11282. {
  11283. int16 m;
  11284. int mf;
  11285. const char *str;
  11286. str = script_getstr(st,2);
  11287. m = map_mapname2mapid(str);
  11288. if (m < 0) {
  11289. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11290. return SCRIPT_CMD_FAILURE;
  11291. }
  11292. mf = script_getnum(st, 3);
  11293. if( mf < MF_MIN || mf > MF_MAX ){
  11294. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11295. return SCRIPT_CMD_FAILURE;
  11296. }
  11297. union u_mapflag_args args = {};
  11298. switch(mf) {
  11299. case MF_SKILL_DAMAGE:
  11300. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11301. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11302. else {
  11303. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11304. return SCRIPT_CMD_FAILURE;
  11305. }
  11306. break;
  11307. case MF_SKILL_DURATION:
  11308. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11309. args.skill_duration.skill_id = script_getnum(st, 4);
  11310. if (!skill_get_index(args.skill_duration.skill_id)) {
  11311. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11312. return SCRIPT_CMD_FAILURE;
  11313. }
  11314. args.skill_duration.per = script_getnum(st, 5);
  11315. } else {
  11316. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11317. return SCRIPT_CMD_FAILURE;
  11318. }
  11319. break;
  11320. case MF_NOSAVE: // Assume setting "SavePoint"
  11321. args.nosave.map = 0;
  11322. args.nosave.x = -1;
  11323. args.nosave.y = -1;
  11324. break;
  11325. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11326. args.nightmaredrop.drop_id = -1;
  11327. args.nightmaredrop.drop_per = 300;
  11328. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11329. break;
  11330. default:
  11331. FETCH(4, args.flag_val);
  11332. break;
  11333. }
  11334. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11335. return SCRIPT_CMD_SUCCESS;
  11336. }
  11337. BUILDIN_FUNC(removemapflag)
  11338. {
  11339. int16 m;
  11340. int mf;
  11341. const char *str;
  11342. union u_mapflag_args args = {};
  11343. str = script_getstr(st, 2);
  11344. m = map_mapname2mapid(str);
  11345. if (m < 0) {
  11346. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11347. return SCRIPT_CMD_FAILURE;
  11348. }
  11349. mf = script_getnum(st, 3);
  11350. if( mf < MF_MIN || mf > MF_MAX ){
  11351. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11352. return SCRIPT_CMD_FAILURE;
  11353. }
  11354. FETCH(4, args.flag_val);
  11355. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11356. return SCRIPT_CMD_SUCCESS;
  11357. }
  11358. BUILDIN_FUNC(pvpon)
  11359. {
  11360. int16 m;
  11361. const char *str = script_getstr(st,2);
  11362. m = map_mapname2mapid(str);
  11363. if (m < 0) {
  11364. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11365. return SCRIPT_CMD_FAILURE;
  11366. }
  11367. if (!map_getmapflag(m, MF_PVP))
  11368. map_setmapflag(m, MF_PVP, true);
  11369. return SCRIPT_CMD_SUCCESS;
  11370. }
  11371. BUILDIN_FUNC(pvpoff)
  11372. {
  11373. int16 m;
  11374. const char *str = script_getstr(st,2);
  11375. m = map_mapname2mapid(str);
  11376. if (m < 0) {
  11377. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11378. return SCRIPT_CMD_FAILURE;
  11379. }
  11380. if (map_getmapflag(m, MF_PVP))
  11381. map_setmapflag(m, MF_PVP, false);
  11382. return SCRIPT_CMD_SUCCESS;
  11383. }
  11384. BUILDIN_FUNC(gvgon)
  11385. {
  11386. int16 m;
  11387. const char *str = script_getstr(st,2);
  11388. m = map_mapname2mapid(str);
  11389. if (m < 0) {
  11390. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11391. return SCRIPT_CMD_FAILURE;
  11392. }
  11393. if (!map_getmapflag(m, MF_GVG))
  11394. map_setmapflag(m, MF_GVG, true);
  11395. return SCRIPT_CMD_SUCCESS;
  11396. }
  11397. BUILDIN_FUNC(gvgoff)
  11398. {
  11399. int16 m;
  11400. const char *str = script_getstr(st,2);
  11401. m = map_mapname2mapid(str);
  11402. if (m < 0) {
  11403. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11404. return SCRIPT_CMD_FAILURE;
  11405. }
  11406. if (map_getmapflag(m, MF_GVG))
  11407. map_setmapflag(m, MF_GVG, false);
  11408. return SCRIPT_CMD_SUCCESS;
  11409. }
  11410. BUILDIN_FUNC(gvgon3)
  11411. {
  11412. int16 m;
  11413. const char *str = script_getstr(st,2);
  11414. m = map_mapname2mapid(str);
  11415. if (m < 0) {
  11416. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11417. return SCRIPT_CMD_FAILURE;
  11418. }
  11419. if (!map_getmapflag(m, MF_GVG_TE))
  11420. map_setmapflag(m, MF_GVG_TE, true);
  11421. return SCRIPT_CMD_SUCCESS;
  11422. }
  11423. BUILDIN_FUNC(gvgoff3)
  11424. {
  11425. int16 m;
  11426. const char *str = script_getstr(st,2);
  11427. m = map_mapname2mapid(str);
  11428. if (m < 0) {
  11429. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11430. return SCRIPT_CMD_FAILURE;
  11431. }
  11432. if (map_getmapflag(m, MF_GVG_TE))
  11433. map_setmapflag(m, MF_GVG_TE, false);
  11434. return SCRIPT_CMD_SUCCESS;
  11435. }
  11436. /**
  11437. * Shows an emotion on top of a NPC by default or the given GID
  11438. * emotion <emotion ID>{,<target ID>};
  11439. */
  11440. BUILDIN_FUNC(emotion)
  11441. {
  11442. struct block_list *bl = NULL;
  11443. int type = script_getnum(st,2);
  11444. if (type < ET_SURPRISE || type >= ET_MAX) {
  11445. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11446. return SCRIPT_CMD_FAILURE;
  11447. }
  11448. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11449. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11450. return SCRIPT_CMD_FAILURE;
  11451. }
  11452. if (!bl)
  11453. bl = map_id2bl(st->oid);
  11454. clif_emotion(bl, type);
  11455. return SCRIPT_CMD_SUCCESS;
  11456. }
  11457. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11458. {
  11459. int16 m=va_arg(ap,int);
  11460. int g_id=va_arg(ap,int);
  11461. int flag=va_arg(ap,int);
  11462. if(!sd || sd->bl.m != m)
  11463. return 0;
  11464. if(
  11465. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11466. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11467. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11468. )
  11469. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11470. return 1;
  11471. }
  11472. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11473. {
  11474. struct mob_data *md=(struct mob_data *)bl;
  11475. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11476. status_kill(bl);
  11477. return 1;
  11478. }
  11479. /*
  11480. * Function to kick guild members out of a map and to their save points.
  11481. * m : mapid
  11482. * g_id : owner guild id
  11483. * flag & 1 : Warp out owners
  11484. * flag & 2 : Warp out outsiders
  11485. * flag & 4 : reserved for mobs
  11486. * */
  11487. BUILDIN_FUNC(maprespawnguildid)
  11488. {
  11489. const char *mapname=script_getstr(st,2);
  11490. int g_id=script_getnum(st,3);
  11491. int flag=script_getnum(st,4);
  11492. int16 m=map_mapname2mapid(mapname);
  11493. if(m == -1)
  11494. return SCRIPT_CMD_SUCCESS;
  11495. //Catch ALL players (in case some are 'between maps' on execution time)
  11496. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11497. if (flag&4) //Remove script mobs.
  11498. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11499. return SCRIPT_CMD_SUCCESS;
  11500. }
  11501. /// Siege commands
  11502. /**
  11503. * Start WoE:FE
  11504. * agitstart();
  11505. */
  11506. BUILDIN_FUNC(agitstart)
  11507. {
  11508. guild_agit_start();
  11509. return SCRIPT_CMD_SUCCESS;
  11510. }
  11511. /**
  11512. * End WoE:FE
  11513. * agitend();
  11514. */
  11515. BUILDIN_FUNC(agitend)
  11516. {
  11517. guild_agit_end();
  11518. return SCRIPT_CMD_SUCCESS;
  11519. }
  11520. /**
  11521. * Start WoE:SE
  11522. * agitstart2();
  11523. */
  11524. BUILDIN_FUNC(agitstart2)
  11525. {
  11526. guild_agit2_start();
  11527. return SCRIPT_CMD_SUCCESS;
  11528. }
  11529. /**
  11530. * End WoE:SE
  11531. * agitend();
  11532. */
  11533. BUILDIN_FUNC(agitend2)
  11534. {
  11535. guild_agit2_end();
  11536. return SCRIPT_CMD_SUCCESS;
  11537. }
  11538. /**
  11539. * Start WoE:TE
  11540. * agitstart3();
  11541. */
  11542. BUILDIN_FUNC(agitstart3)
  11543. {
  11544. guild_agit3_start();
  11545. return SCRIPT_CMD_SUCCESS;
  11546. }
  11547. /**
  11548. * End WoE:TE
  11549. * agitend3();
  11550. */
  11551. BUILDIN_FUNC(agitend3)
  11552. {
  11553. guild_agit3_end();
  11554. return SCRIPT_CMD_SUCCESS;
  11555. }
  11556. /**
  11557. * Returns whether WoE:FE is on or off.
  11558. * agitcheck();
  11559. */
  11560. BUILDIN_FUNC(agitcheck)
  11561. {
  11562. script_pushint(st, agit_flag);
  11563. return SCRIPT_CMD_SUCCESS;
  11564. }
  11565. /**
  11566. * Returns whether WoE:SE is on or off.
  11567. * agitcheck2();
  11568. */
  11569. BUILDIN_FUNC(agitcheck2)
  11570. {
  11571. script_pushint(st, agit2_flag);
  11572. return SCRIPT_CMD_SUCCESS;
  11573. }
  11574. /**
  11575. * Returns whether WoE:TE is on or off.
  11576. * agitcheck3();
  11577. */
  11578. BUILDIN_FUNC(agitcheck3)
  11579. {
  11580. script_pushint(st, agit3_flag);
  11581. return SCRIPT_CMD_SUCCESS;
  11582. }
  11583. /// Sets the guild_id of this npc.
  11584. ///
  11585. /// flagemblem <guild_id>;
  11586. BUILDIN_FUNC(flagemblem)
  11587. {
  11588. TBL_NPC* nd;
  11589. int g_id = script_getnum(st,2);
  11590. if(g_id < 0)
  11591. return SCRIPT_CMD_SUCCESS;
  11592. nd = (TBL_NPC*)map_id2nd(st->oid);
  11593. if( nd == NULL ) {
  11594. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11595. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11596. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11597. } else {
  11598. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11599. nd->u.scr.guild_id = g_id;
  11600. clif_guild_emblem_area(&nd->bl);
  11601. /* guild flag caching */
  11602. if( g_id ) /* adding a id */
  11603. guild_flag_add(nd);
  11604. else if( changed ) /* removing a flag */
  11605. guild_flag_remove(nd);
  11606. }
  11607. return SCRIPT_CMD_SUCCESS;
  11608. }
  11609. BUILDIN_FUNC(getcastlename)
  11610. {
  11611. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11612. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11613. const char* name = (gc) ? gc->castle_name : "";
  11614. script_pushstrcopy(st,name);
  11615. return SCRIPT_CMD_SUCCESS;
  11616. }
  11617. BUILDIN_FUNC(getcastledata)
  11618. {
  11619. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11620. int index = script_getnum(st,3);
  11621. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11622. if (gc == NULL) {
  11623. script_pushint(st,0);
  11624. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11625. return SCRIPT_CMD_FAILURE;
  11626. }
  11627. switch (index) {
  11628. case CD_GUILD_ID:
  11629. script_pushint(st,gc->guild_id); break;
  11630. case CD_CURRENT_ECONOMY:
  11631. script_pushint(st,gc->economy); break;
  11632. case CD_CURRENT_DEFENSE:
  11633. script_pushint(st,gc->defense); break;
  11634. case CD_INVESTED_ECONOMY:
  11635. script_pushint(st,gc->triggerE); break;
  11636. case CD_INVESTED_DEFENSE:
  11637. script_pushint(st,gc->triggerD); break;
  11638. case CD_NEXT_TIME:
  11639. script_pushint(st,gc->nextTime); break;
  11640. case CD_PAY_TIME:
  11641. script_pushint(st,gc->payTime); break;
  11642. case CD_CREATE_TIME:
  11643. script_pushint(st,gc->createTime); break;
  11644. case CD_ENABLED_KAFRA:
  11645. script_pushint(st,gc->visibleC); break;
  11646. default:
  11647. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11648. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11649. break;
  11650. }
  11651. script_pushint(st,0);
  11652. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11653. return SCRIPT_CMD_FAILURE;
  11654. }
  11655. return SCRIPT_CMD_SUCCESS;
  11656. }
  11657. BUILDIN_FUNC(setcastledata)
  11658. {
  11659. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11660. int index = script_getnum(st,3);
  11661. int value = script_getnum(st,4);
  11662. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  11663. if (gc == NULL) {
  11664. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11665. return SCRIPT_CMD_FAILURE;
  11666. }
  11667. if (index <= CD_NONE || index >= CD_MAX) {
  11668. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11669. return SCRIPT_CMD_FAILURE;
  11670. }
  11671. guild_castledatasave(gc->castle_id, index, value);
  11672. return SCRIPT_CMD_SUCCESS;
  11673. }
  11674. /* =====================================================================
  11675. * ---------------------------------------------------------------------*/
  11676. BUILDIN_FUNC(requestguildinfo)
  11677. {
  11678. int guild_id=script_getnum(st,2);
  11679. const char *event=NULL;
  11680. if( script_hasdata(st,3) ){
  11681. event=script_getstr(st,3);
  11682. check_event(st, event);
  11683. }
  11684. if(guild_id>0)
  11685. guild_npc_request_info(guild_id,event);
  11686. return SCRIPT_CMD_SUCCESS;
  11687. }
  11688. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11689. /// getequipcardcnt(<equipment slot>);
  11690. BUILDIN_FUNC(getequipcardcnt)
  11691. {
  11692. int i=-1,j,num;
  11693. TBL_PC *sd;
  11694. int count;
  11695. if( !script_rid2sd(sd) )
  11696. return SCRIPT_CMD_SUCCESS;
  11697. num=script_getnum(st,2);
  11698. if (equip_index_check(num))
  11699. i=pc_checkequip(sd,equip_bitmask[num]);
  11700. if (i < 0 || !sd->inventory_data[i]) {
  11701. script_pushint(st,0);
  11702. return SCRIPT_CMD_SUCCESS;
  11703. }
  11704. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11705. {
  11706. script_pushint(st,0);
  11707. return SCRIPT_CMD_SUCCESS;
  11708. }
  11709. count = 0;
  11710. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11711. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11712. count++;
  11713. script_pushint(st,count);
  11714. return SCRIPT_CMD_SUCCESS;
  11715. }
  11716. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11717. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11718. /// successremovecards <slot>;
  11719. BUILDIN_FUNC(successremovecards) {
  11720. int i=-1,c,cardflag=0;
  11721. TBL_PC* sd;
  11722. int num;
  11723. if( !script_rid2sd(sd) )
  11724. return SCRIPT_CMD_SUCCESS;
  11725. num = script_getnum(st,2);
  11726. if (equip_index_check(num))
  11727. i=pc_checkequip(sd,equip_bitmask[num]);
  11728. if (i < 0 || !sd->inventory_data[i]) {
  11729. return SCRIPT_CMD_SUCCESS;
  11730. }
  11731. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11732. return SCRIPT_CMD_SUCCESS;
  11733. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11734. 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
  11735. unsigned char flag = 0;
  11736. struct item item_tmp;
  11737. memset(&item_tmp,0,sizeof(item_tmp));
  11738. cardflag = 1;
  11739. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11740. item_tmp.identify = 1;
  11741. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11742. clif_additem(sd,0,0,flag);
  11743. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11744. }
  11745. }
  11746. }
  11747. if(cardflag == 1) {//if card was remove remplace item with no card
  11748. unsigned char flag = 0;
  11749. struct item item_tmp;
  11750. memset(&item_tmp,0,sizeof(item_tmp));
  11751. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11752. item_tmp.identify = 1;
  11753. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11754. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11755. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11756. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11757. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  11758. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11759. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11760. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11761. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11762. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11763. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11764. }
  11765. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11766. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11767. clif_additem(sd,0,0,flag);
  11768. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11769. }
  11770. clif_misceffect(&sd->bl,3);
  11771. }
  11772. return SCRIPT_CMD_SUCCESS;
  11773. }
  11774. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11775. /// failedremovecards <slot>, <type>;
  11776. /// <type>=0 : will destroy both the item and the cards.
  11777. /// <type>=1 : will keep the item, but destroy the cards.
  11778. /// <type>=2 : will keep the cards, but destroy the item.
  11779. /// <type>=? : will just display the failure effect.
  11780. BUILDIN_FUNC(failedremovecards) {
  11781. int i=-1,c,cardflag=0;
  11782. TBL_PC* sd;
  11783. int num;
  11784. int typefail;
  11785. if( !script_rid2sd(sd) )
  11786. return SCRIPT_CMD_SUCCESS;
  11787. num = script_getnum(st,2);
  11788. typefail = script_getnum(st,3);
  11789. if (equip_index_check(num))
  11790. i=pc_checkequip(sd,equip_bitmask[num]);
  11791. if (i < 0 || !sd->inventory_data[i])
  11792. return SCRIPT_CMD_SUCCESS;
  11793. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11794. return SCRIPT_CMD_SUCCESS;
  11795. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11796. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11797. cardflag = 1;
  11798. if(typefail == 2) {// add cards to inventory, clear
  11799. unsigned char flag = 0;
  11800. struct item item_tmp;
  11801. memset(&item_tmp,0,sizeof(item_tmp));
  11802. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11803. item_tmp.identify = 1;
  11804. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11805. clif_additem(sd,0,0,flag);
  11806. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11807. }
  11808. }
  11809. }
  11810. }
  11811. if(cardflag == 1) {
  11812. if(typefail == 0 || typefail == 2){ // destroy the item
  11813. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11814. }else if(typefail == 1){ // destroy the card
  11815. unsigned char flag = 0;
  11816. struct item item_tmp;
  11817. memset(&item_tmp,0,sizeof(item_tmp));
  11818. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11819. item_tmp.identify = 1;
  11820. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11821. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11822. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11823. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11824. item_tmp.enchantgrade = sd->inventory.u.items_inventory[i].enchantgrade;
  11825. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11826. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11827. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11828. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11829. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11830. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11831. }
  11832. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11833. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11834. clif_additem(sd,0,0,flag);
  11835. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11836. }
  11837. }
  11838. clif_misceffect(&sd->bl,2);
  11839. }
  11840. return SCRIPT_CMD_SUCCESS;
  11841. }
  11842. /* ================================================================
  11843. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11844. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11845. * improved by [Lance]
  11846. * ================================================================*/
  11847. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11848. {
  11849. int x,y,m,check_val=0,check_ID=0,i=0;
  11850. struct guild *g = NULL;
  11851. struct party_data *p = NULL;
  11852. const char *str;
  11853. const char *mapname;
  11854. unsigned int index;
  11855. mapname=script_getstr(st,2);
  11856. str=script_getstr(st,3);
  11857. x=script_getnum(st,4);
  11858. y=script_getnum(st,5);
  11859. if(script_hasdata(st,7)){
  11860. check_val=script_getnum(st,6);
  11861. check_ID=script_getnum(st,7);
  11862. }
  11863. if((m=map_mapname2mapid(mapname))< 0)
  11864. return SCRIPT_CMD_SUCCESS;
  11865. if(!(index=mapindex_name2id(str)))
  11866. return SCRIPT_CMD_SUCCESS;
  11867. switch(check_val){
  11868. case 1:
  11869. g = guild_search(check_ID);
  11870. if (g){
  11871. for( i=0; i < g->max_member; i++)
  11872. {
  11873. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11874. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11875. }
  11876. }
  11877. }
  11878. break;
  11879. case 2:
  11880. p = party_search(check_ID);
  11881. if(p){
  11882. for(i=0;i<MAX_PARTY; i++){
  11883. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11884. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11885. }
  11886. }
  11887. }
  11888. break;
  11889. default:
  11890. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11891. break;
  11892. }
  11893. return SCRIPT_CMD_SUCCESS;
  11894. }
  11895. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11896. {
  11897. char *event=va_arg(ap,char *);
  11898. struct mob_data *md = ((struct mob_data *)bl);
  11899. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11900. return 1;
  11901. return SCRIPT_CMD_SUCCESS;
  11902. }
  11903. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11904. {
  11905. const char *mapname,*event;
  11906. int16 m;
  11907. mapname=script_getstr(st,2);
  11908. event=script_getstr(st,3);
  11909. if( strcmp(event, "all") == 0 )
  11910. event = NULL;
  11911. else
  11912. check_event(st, event);
  11913. if( strcmp(mapname, "this") == 0 ) {
  11914. struct map_session_data *sd;
  11915. if( script_rid2sd(sd) )
  11916. m = sd->bl.m;
  11917. else {
  11918. script_pushint(st,-1);
  11919. return SCRIPT_CMD_SUCCESS;
  11920. }
  11921. }
  11922. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11923. script_pushint(st,-1);
  11924. return SCRIPT_CMD_SUCCESS;
  11925. }
  11926. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11927. return SCRIPT_CMD_SUCCESS;
  11928. }
  11929. BUILDIN_FUNC(marriage)
  11930. {
  11931. TBL_PC *sd;
  11932. TBL_PC *p_sd;
  11933. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11934. script_pushint(st,0);
  11935. return SCRIPT_CMD_SUCCESS;
  11936. }
  11937. script_pushint(st,1);
  11938. return SCRIPT_CMD_SUCCESS;
  11939. }
  11940. BUILDIN_FUNC(wedding_effect)
  11941. {
  11942. TBL_PC *sd;
  11943. struct block_list *bl;
  11944. if(!script_rid2sd(sd)) {
  11945. bl=map_id2bl(st->oid);
  11946. } else
  11947. bl=&sd->bl;
  11948. clif_wedding_effect(bl);
  11949. return SCRIPT_CMD_SUCCESS;
  11950. }
  11951. /**
  11952. * divorce({<char_id>})
  11953. **/
  11954. BUILDIN_FUNC(divorce)
  11955. {
  11956. TBL_PC *sd;
  11957. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11958. script_pushint(st,0);
  11959. return SCRIPT_CMD_FAILURE;
  11960. }
  11961. script_pushint(st,1);
  11962. return SCRIPT_CMD_SUCCESS;
  11963. }
  11964. /**
  11965. * ispartneron({<char_id>})
  11966. **/
  11967. BUILDIN_FUNC(ispartneron)
  11968. {
  11969. TBL_PC *sd;
  11970. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11971. map_charid2sd(sd->status.partner_id) == NULL)
  11972. {
  11973. script_pushint(st,0);
  11974. return SCRIPT_CMD_FAILURE;
  11975. }
  11976. script_pushint(st,1);
  11977. return SCRIPT_CMD_SUCCESS;
  11978. }
  11979. /**
  11980. * getpartnerid({<char_id>})
  11981. **/
  11982. BUILDIN_FUNC(getpartnerid)
  11983. {
  11984. TBL_PC *sd;
  11985. if (!script_charid2sd(2,sd)) {
  11986. script_pushint(st,0);
  11987. return SCRIPT_CMD_FAILURE;
  11988. }
  11989. script_pushint(st,sd->status.partner_id);
  11990. return SCRIPT_CMD_SUCCESS;
  11991. }
  11992. /**
  11993. * getchildid({<char_id>})
  11994. **/
  11995. BUILDIN_FUNC(getchildid)
  11996. {
  11997. TBL_PC *sd;
  11998. if (!script_charid2sd(2,sd)) {
  11999. script_pushint(st,0);
  12000. return SCRIPT_CMD_FAILURE;
  12001. }
  12002. script_pushint(st,sd->status.child);
  12003. return SCRIPT_CMD_SUCCESS;
  12004. }
  12005. /**
  12006. * getmotherid({<char_id>})
  12007. **/
  12008. BUILDIN_FUNC(getmotherid)
  12009. {
  12010. TBL_PC *sd;
  12011. if (!script_charid2sd(2,sd)) {
  12012. script_pushint(st,0);
  12013. return SCRIPT_CMD_FAILURE;
  12014. }
  12015. script_pushint(st,sd->status.mother);
  12016. return SCRIPT_CMD_SUCCESS;
  12017. }
  12018. /**
  12019. * getfatherid({<char_id>})
  12020. **/
  12021. BUILDIN_FUNC(getfatherid)
  12022. {
  12023. TBL_PC *sd;
  12024. if (!script_charid2sd(2,sd)) {
  12025. script_pushint(st,0);
  12026. return SCRIPT_CMD_FAILURE;
  12027. }
  12028. script_pushint(st,sd->status.father);
  12029. return SCRIPT_CMD_SUCCESS;
  12030. }
  12031. BUILDIN_FUNC(warppartner)
  12032. {
  12033. int x,y;
  12034. unsigned short mapindex;
  12035. const char *str;
  12036. TBL_PC *sd;
  12037. TBL_PC *p_sd;
  12038. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  12039. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  12040. script_pushint(st,0);
  12041. return SCRIPT_CMD_SUCCESS;
  12042. }
  12043. str=script_getstr(st,2);
  12044. x=script_getnum(st,3);
  12045. y=script_getnum(st,4);
  12046. mapindex = mapindex_name2id(str);
  12047. if (mapindex) {
  12048. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  12049. script_pushint(st,1);
  12050. } else
  12051. script_pushint(st,0);
  12052. return SCRIPT_CMD_SUCCESS;
  12053. }
  12054. /*================================================
  12055. * Script for Displaying MOB Information [Valaris]
  12056. *------------------------------------------------*/
  12057. BUILDIN_FUNC(strmobinfo)
  12058. {
  12059. int num=script_getnum(st,2);
  12060. int class_=script_getnum(st,3);
  12061. if(!mobdb_checkid(class_))
  12062. {
  12063. if (num < 3) //requested a string
  12064. script_pushconststr(st,"");
  12065. else
  12066. script_pushint(st,0);
  12067. return SCRIPT_CMD_SUCCESS;
  12068. }
  12069. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  12070. switch (num) {
  12071. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  12072. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  12073. case 3: script_pushint(st,mob->lv); break;
  12074. case 4: script_pushint(st,mob->status.max_hp); break;
  12075. case 5: script_pushint(st,mob->status.max_sp); break;
  12076. case 6: script_pushint(st,mob->base_exp); break;
  12077. case 7: script_pushint(st,mob->job_exp); break;
  12078. default:
  12079. script_pushint(st,0);
  12080. break;
  12081. }
  12082. return SCRIPT_CMD_SUCCESS;
  12083. }
  12084. /*==========================================
  12085. * Summon guardians [Valaris]
  12086. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  12087. *------------------------------------------*/
  12088. BUILDIN_FUNC(guardian)
  12089. {
  12090. int class_=0,x=0,y=0,guardian=0;
  12091. const char *str,*mapname,*evt="";
  12092. bool has_index = false;
  12093. mapname =script_getstr(st,2);
  12094. x =script_getnum(st,3);
  12095. y =script_getnum(st,4);
  12096. str =script_getstr(st,5);
  12097. class_=script_getnum(st,6);
  12098. if( script_hasdata(st,8) )
  12099. {// "<event label>",<guardian index>
  12100. evt=script_getstr(st,7);
  12101. guardian=script_getnum(st,8);
  12102. has_index = true;
  12103. } else if( script_hasdata(st,7) ){
  12104. if( script_isstring(st, 7) )
  12105. {// "<event label>"
  12106. evt=script_getstr(st,7);
  12107. } else
  12108. {// <guardian index>
  12109. guardian=script_getnum(st,7);
  12110. has_index = true;
  12111. }
  12112. }
  12113. check_event(st, evt);
  12114. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  12115. return SCRIPT_CMD_SUCCESS;
  12116. }
  12117. /*==========================================
  12118. * Invisible Walls [Zephyrus]
  12119. *------------------------------------------*/
  12120. BUILDIN_FUNC(setwall)
  12121. {
  12122. const char *mapname, *name;
  12123. int x, y, m, size, dir;
  12124. bool shootable;
  12125. mapname = script_getstr(st,2);
  12126. x = script_getnum(st,3);
  12127. y = script_getnum(st,4);
  12128. size = script_getnum(st,5);
  12129. dir = script_getnum(st,6);
  12130. shootable = script_getnum(st,7) != 0;
  12131. name = script_getstr(st,8);
  12132. if( (m = map_mapname2mapid(mapname)) < 0 )
  12133. return SCRIPT_CMD_SUCCESS; // Invalid Map
  12134. map_iwall_set(m, x, y, size, dir, shootable, name);
  12135. return SCRIPT_CMD_SUCCESS;
  12136. }
  12137. BUILDIN_FUNC(delwall)
  12138. {
  12139. const char *name = script_getstr(st,2);
  12140. if( !map_iwall_remove(name) ){
  12141. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  12142. return SCRIPT_CMD_FAILURE;
  12143. }
  12144. return SCRIPT_CMD_SUCCESS;
  12145. }
  12146. BUILDIN_FUNC(checkwall)
  12147. {
  12148. const char *wall_name = script_getstr(st, 2);
  12149. script_pushint(st, map_iwall_exist(wall_name));
  12150. return SCRIPT_CMD_SUCCESS;
  12151. }
  12152. /// Retrieves various information about the specified guardian.
  12153. ///
  12154. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  12155. /// type: 0 - whether it is deployed or not
  12156. /// 1 - maximum hp
  12157. /// 2 - current hp
  12158. ///
  12159. BUILDIN_FUNC(guardianinfo)
  12160. {
  12161. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  12162. int id = script_getnum(st,3);
  12163. int type = script_getnum(st,4);
  12164. std::shared_ptr<guild_castle> gc = castle_db.mapname2gc(mapname);
  12165. struct mob_data* gd;
  12166. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  12167. {
  12168. script_pushint(st,-1);
  12169. return SCRIPT_CMD_SUCCESS;
  12170. }
  12171. if( type == 0 )
  12172. script_pushint(st, gc->guardian[id].visible);
  12173. else
  12174. if( !gc->guardian[id].visible )
  12175. script_pushint(st,-1);
  12176. else
  12177. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  12178. script_pushint(st,-1);
  12179. else
  12180. {
  12181. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  12182. else if( type == 2 ) script_pushint(st,gd->status.hp);
  12183. else
  12184. script_pushint(st,-1);
  12185. }
  12186. return SCRIPT_CMD_SUCCESS;
  12187. }
  12188. /*==========================================
  12189. * Get the item name by item_id or null
  12190. *------------------------------------------*/
  12191. BUILDIN_FUNC(getitemname)
  12192. {
  12193. std::shared_ptr<item_data> i_data;
  12194. if( script_isstring(st, 2) ){
  12195. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12196. }else{
  12197. i_data = item_db.find( script_getnum( st, 2 ) );
  12198. }
  12199. if( i_data == nullptr ){
  12200. script_pushconststr(st,"null");
  12201. return SCRIPT_CMD_SUCCESS;
  12202. }
  12203. char* item_name = (char *)aMalloc( ITEM_NAME_LENGTH * sizeof( char ) );
  12204. memcpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12205. script_pushstr(st,item_name);
  12206. return SCRIPT_CMD_SUCCESS;
  12207. }
  12208. /*==========================================
  12209. * Returns number of slots an item has. [Skotlex]
  12210. *------------------------------------------*/
  12211. BUILDIN_FUNC(getitemslots)
  12212. {
  12213. struct item_data *i_data;
  12214. t_itemid item_id=script_getnum(st,2);
  12215. i_data = itemdb_exists(item_id);
  12216. if (i_data)
  12217. script_pushint(st,i_data->slots);
  12218. else
  12219. script_pushint(st,-1);
  12220. return SCRIPT_CMD_SUCCESS;
  12221. }
  12222. // TODO: add matk here if needed/once we get rid of RENEWAL
  12223. /*==========================================
  12224. * Returns some values of an item [Lupus]
  12225. * Price, Weight, etc...
  12226. *------------------------------------------*/
  12227. BUILDIN_FUNC(getiteminfo)
  12228. {
  12229. std::shared_ptr<item_data> i_data;
  12230. int type = script_getnum(st, 3);
  12231. if (script_isstring(st, 2))
  12232. i_data = item_db.searchname( script_getstr( st, 2 ) );
  12233. else
  12234. i_data = item_db.find( script_getnum( st, 2 ) );
  12235. if (i_data == nullptr) {
  12236. if (type != ITEMINFO_AEGISNAME)
  12237. script_pushint(st, -1);
  12238. else
  12239. script_pushstrcopy(st, "");
  12240. return SCRIPT_CMD_SUCCESS;
  12241. }
  12242. switch( type ) {
  12243. case ITEMINFO_BUY: script_pushint(st, i_data->value_buy); break;
  12244. case ITEMINFO_SELL: script_pushint(st, i_data->value_sell); break;
  12245. case ITEMINFO_TYPE: script_pushint(st, i_data->type); break;
  12246. case ITEMINFO_MAXCHANCE: script_pushint(st, i_data->maxchance); break;
  12247. case ITEMINFO_GENDER: script_pushint(st, i_data->sex); break;
  12248. case ITEMINFO_LOCATIONS: script_pushint(st, i_data->equip); break;
  12249. case ITEMINFO_WEIGHT: script_pushint(st, i_data->weight); break;
  12250. case ITEMINFO_ATTACK: script_pushint(st, i_data->atk); break;
  12251. case ITEMINFO_DEFENSE: script_pushint(st, i_data->def); break;
  12252. case ITEMINFO_RANGE: script_pushint(st, i_data->range); break;
  12253. case ITEMINFO_SLOT: script_pushint(st, i_data->slots); break;
  12254. case ITEMINFO_VIEW:
  12255. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12256. script_pushint(st, i_data->subtype);
  12257. } else {
  12258. script_pushint(st, i_data->look);
  12259. }
  12260. break;
  12261. case ITEMINFO_EQUIPLEVELMIN: script_pushint(st, i_data->elv); break;
  12262. case ITEMINFO_WEAPONLEVEL:
  12263. if( i_data->type == IT_WEAPON ){
  12264. script_pushint( st, i_data->weapon_level );
  12265. }else{
  12266. script_pushint( st, 0 );
  12267. }
  12268. break;
  12269. case ITEMINFO_ARMORLEVEL:
  12270. if( i_data->type == IT_ARMOR ){
  12271. script_pushint( st, i_data->armor_level );
  12272. }else{
  12273. script_pushint( st, 0 );
  12274. }
  12275. break;
  12276. case ITEMINFO_ALIASNAME: script_pushint(st, i_data->view_id); break;
  12277. case ITEMINFO_EQUIPLEVELMAX: script_pushint(st, i_data->elvmax); break;
  12278. case ITEMINFO_MAGICATTACK: {
  12279. #ifdef RENEWAL
  12280. script_pushint(st, i_data->matk);
  12281. #else
  12282. script_pushint(st, 0);
  12283. #endif
  12284. break;
  12285. }
  12286. case ITEMINFO_ID: script_pushint(st, i_data->nameid); break;
  12287. case ITEMINFO_AEGISNAME: script_pushstrcopy(st, i_data->name.c_str()); break;
  12288. case ITEMINFO_SUBTYPE: script_pushint(st, i_data->subtype); break;
  12289. default:
  12290. script_pushint(st, -1);
  12291. break;
  12292. }
  12293. return SCRIPT_CMD_SUCCESS;
  12294. }
  12295. /*==========================================
  12296. * Set some values of an item [Lupus]
  12297. * Price, Weight, etc...
  12298. *------------------------------------------*/
  12299. BUILDIN_FUNC(setiteminfo)
  12300. {
  12301. std::shared_ptr<item_data> i_data;
  12302. if (script_isstring(st, 2))
  12303. i_data = item_db.search_aegisname( script_getstr( st, 2 ) );
  12304. else
  12305. i_data = item_db.find( script_getnum( st, 2 ) );
  12306. if (i_data == nullptr) {
  12307. script_pushint(st, -1);
  12308. return SCRIPT_CMD_SUCCESS;
  12309. }
  12310. int value = script_getnum(st,4);
  12311. switch( script_getnum(st, 3) ) {
  12312. case ITEMINFO_BUY: i_data->value_buy = static_cast<uint32>(value); break;
  12313. case ITEMINFO_SELL: i_data->value_sell = static_cast<uint32>(value); break;
  12314. case ITEMINFO_TYPE: i_data->type = static_cast<item_types>(value); break;
  12315. case ITEMINFO_MAXCHANCE: i_data->maxchance = static_cast<int>(value); break;
  12316. case ITEMINFO_GENDER: i_data->sex = static_cast<uint8>(value); break;
  12317. case ITEMINFO_LOCATIONS: i_data->equip = static_cast<uint32>(value); break;
  12318. case ITEMINFO_WEIGHT: i_data->weight = static_cast<uint32>(value); break;
  12319. case ITEMINFO_ATTACK: i_data->atk = static_cast<uint32>(value); break;
  12320. case ITEMINFO_DEFENSE: i_data->def = static_cast<uint32>(value); break;
  12321. case ITEMINFO_RANGE: i_data->range = static_cast<uint16>(value); break;
  12322. case ITEMINFO_SLOT: i_data->slots = static_cast<uint16>(value); break;
  12323. case ITEMINFO_VIEW:
  12324. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12325. i_data->subtype = static_cast<uint8>(value);
  12326. } else {
  12327. i_data->look = static_cast<uint32>(value);
  12328. }
  12329. break;
  12330. case ITEMINFO_EQUIPLEVELMIN: i_data->elv = static_cast<uint16>(value); break;
  12331. case ITEMINFO_WEAPONLEVEL:
  12332. if( i_data->type == IT_WEAPON ){
  12333. if( value > MAX_WEAPON_LEVEL ){
  12334. ShowError( "buildin_setiteminfo: weapon level %d is above maximum %d.\n", value, MAX_WEAPON_LEVEL );
  12335. script_pushint( st, -1 );
  12336. return SCRIPT_CMD_FAILURE;
  12337. }
  12338. i_data->weapon_level = static_cast<uint16>(value);
  12339. break;
  12340. }else{
  12341. ShowError( "buildin_setiteminfo: Can not set a weapon level for %d.\n", i_data->nameid );
  12342. script_pushint( st, -1 );
  12343. return SCRIPT_CMD_FAILURE;
  12344. }
  12345. case ITEMINFO_ARMORLEVEL:
  12346. if( i_data->type == IT_ARMOR ){
  12347. if( value > MAX_ARMOR_LEVEL ){
  12348. ShowError( "buildin_setiteminfo: armor level %d is above maximum %d.\n", value, MAX_ARMOR_LEVEL );
  12349. script_pushint( st, -1 );
  12350. return SCRIPT_CMD_FAILURE;
  12351. }
  12352. i_data->armor_level = static_cast<uint16>(value);
  12353. break;
  12354. }else{
  12355. ShowError( "buildin_setiteminfo: Can not set an armor level for %d.\n", i_data->nameid );
  12356. script_pushint( st, -1 );
  12357. return SCRIPT_CMD_FAILURE;
  12358. }
  12359. case ITEMINFO_ALIASNAME: i_data->view_id = static_cast<t_itemid>(value); break;
  12360. case ITEMINFO_EQUIPLEVELMAX: i_data->elvmax = static_cast<uint16>(value); break;
  12361. case ITEMINFO_MAGICATTACK: {
  12362. #ifdef RENEWAL
  12363. i_data->matk = static_cast<uint32>(value);
  12364. #else
  12365. value = 0;
  12366. #endif
  12367. break;
  12368. }
  12369. case ITEMINFO_SUBTYPE: i_data->subtype = static_cast<uint8>(value); break;
  12370. default:
  12371. script_pushint(st, -1);
  12372. break;
  12373. }
  12374. script_pushint(st, value);
  12375. return SCRIPT_CMD_SUCCESS;
  12376. }
  12377. /*==========================================
  12378. * Returns value from equipped item slot n [Lupus]
  12379. getequipcardid(num,slot)
  12380. where
  12381. num = eqip position slot
  12382. slot = 0,1,2,3 (Card Slot N)
  12383. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12384. it's useful when you want to check item cards or if it's signed
  12385. Useful for such quests as "Sign this refined item with players name" etc
  12386. Hat[0] +4 -> Player's Hat[0] +4
  12387. *------------------------------------------*/
  12388. BUILDIN_FUNC(getequipcardid)
  12389. {
  12390. int i=-1,num,slot;
  12391. TBL_PC *sd;
  12392. if( !script_rid2sd(sd) )
  12393. return SCRIPT_CMD_SUCCESS;
  12394. num=script_getnum(st,2);
  12395. slot=script_getnum(st,3);
  12396. if (equip_index_check(num))
  12397. i=pc_checkequip(sd,equip_bitmask[num]);
  12398. if(i >= 0 && slot>=0 && slot<4)
  12399. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12400. else
  12401. script_pushint(st,0);
  12402. return SCRIPT_CMD_SUCCESS;
  12403. }
  12404. /*==========================================
  12405. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12406. *------------------------------------------*/
  12407. BUILDIN_FUNC(petskillbonus)
  12408. {
  12409. struct pet_data *pd;
  12410. TBL_PC *sd;
  12411. if(!script_rid2sd(sd) || sd->pd == NULL)
  12412. return SCRIPT_CMD_FAILURE;
  12413. pd = sd->pd;
  12414. if (pd->bonus)
  12415. { //Clear previous bonus
  12416. if (pd->bonus->timer != INVALID_TIMER)
  12417. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12418. } else //init
  12419. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12420. pd->bonus->type = script_getnum(st,2);
  12421. pd->bonus->val = script_getnum(st,3);
  12422. pd->bonus->duration = script_getnum(st,4);
  12423. pd->bonus->delay = script_getnum(st,5);
  12424. if (pd->state.skillbonus == 1)
  12425. pd->state.skillbonus = 0; // waiting state
  12426. // wait for timer to start
  12427. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12428. pd->bonus->timer = INVALID_TIMER;
  12429. else
  12430. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12431. return SCRIPT_CMD_SUCCESS;
  12432. }
  12433. /*==========================================
  12434. * pet looting [Valaris] //Rewritten by [Skotlex]
  12435. *------------------------------------------*/
  12436. BUILDIN_FUNC(petloot)
  12437. {
  12438. int max;
  12439. struct pet_data *pd;
  12440. TBL_PC *sd;
  12441. if(!script_rid2sd(sd) || sd->pd==NULL)
  12442. return SCRIPT_CMD_SUCCESS;
  12443. max=script_getnum(st,2);
  12444. if(max < 1)
  12445. max = 1; //Let'em loot at least 1 item.
  12446. else if (max > MAX_PETLOOT_SIZE)
  12447. max = MAX_PETLOOT_SIZE;
  12448. pd = sd->pd;
  12449. if (pd->loot != NULL)
  12450. { //Release whatever was there already and reallocate memory
  12451. pet_lootitem_drop(pd, pd->master);
  12452. aFree(pd->loot->item);
  12453. }
  12454. else
  12455. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12456. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12457. pd->loot->max=max;
  12458. pd->loot->count = 0;
  12459. pd->loot->weight = 0;
  12460. return SCRIPT_CMD_SUCCESS;
  12461. }
  12462. /*==========================================
  12463. * Set arrays with info of all sd inventory :
  12464. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12465. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12466. * @inventorylist_card(0..3), @inventorylist_expire
  12467. * @inventorylist_count = scalar
  12468. *------------------------------------------*/
  12469. BUILDIN_FUNC(getinventorylist)
  12470. {
  12471. TBL_PC *sd;
  12472. char card_var[NAME_LENGTH], randopt_var[50];
  12473. int i,j=0,k;
  12474. if (!script_charid2sd(2,sd))
  12475. return SCRIPT_CMD_FAILURE;
  12476. for(i=0;i<MAX_INVENTORY;i++){
  12477. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12478. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12479. pc_setreg(sd,reference_uid(add_str("@inventorylist_idx"), j),i);
  12480. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12481. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12482. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12483. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12484. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12485. for (k = 0; k < MAX_SLOTS; k++)
  12486. {
  12487. sprintf(card_var, "@inventorylist_card%d",k+1);
  12488. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12489. }
  12490. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12491. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12492. pc_setreg(sd,reference_uid(add_str("@inventorylist_enchantgrade"), j),sd->inventory.u.items_inventory[i].enchantgrade);
  12493. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12494. {
  12495. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12496. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12497. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12498. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12499. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12500. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12501. }
  12502. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12503. pc_setreg(sd,reference_uid(add_str("@inventorylist_favorite"), j),sd->inventory.u.items_inventory[i].favorite);
  12504. j++;
  12505. }
  12506. }
  12507. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12508. return SCRIPT_CMD_SUCCESS;
  12509. }
  12510. /**
  12511. * getskilllist ({<char_id>});
  12512. **/
  12513. BUILDIN_FUNC(getskilllist)
  12514. {
  12515. TBL_PC *sd;
  12516. int i,j=0;
  12517. if (!script_charid2sd(2,sd))
  12518. return SCRIPT_CMD_FAILURE;
  12519. for(i=0;i<MAX_SKILL;i++){
  12520. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12521. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12522. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12523. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12524. j++;
  12525. }
  12526. }
  12527. pc_setreg(sd,add_str("@skilllist_count"),j);
  12528. return SCRIPT_CMD_SUCCESS;
  12529. }
  12530. /**
  12531. * clearitem ({<char_id>});
  12532. **/
  12533. BUILDIN_FUNC(clearitem)
  12534. {
  12535. TBL_PC *sd;
  12536. int i;
  12537. if (!script_charid2sd(2,sd))
  12538. return SCRIPT_CMD_FAILURE;
  12539. for (i=0; i<MAX_INVENTORY; i++) {
  12540. if (sd->inventory.u.items_inventory[i].amount) {
  12541. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12542. }
  12543. }
  12544. return SCRIPT_CMD_SUCCESS;
  12545. }
  12546. /**
  12547. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12548. * disguise <Monster ID>{,<char_id>};
  12549. **/
  12550. BUILDIN_FUNC(disguise)
  12551. {
  12552. int id;
  12553. TBL_PC* sd;
  12554. if (!script_charid2sd(3,sd))
  12555. return SCRIPT_CMD_FAILURE;
  12556. id = script_getnum(st,2);
  12557. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12558. pc_disguise(sd, id);
  12559. script_pushint(st,id);
  12560. } else
  12561. script_pushint(st,0);
  12562. return SCRIPT_CMD_SUCCESS;
  12563. }
  12564. /**
  12565. * Undisguise Player (returns 1 if success, 0 on fail)
  12566. * undisguise {<char_id>};
  12567. **/
  12568. BUILDIN_FUNC(undisguise)
  12569. {
  12570. TBL_PC* sd;
  12571. if (!script_charid2sd(2,sd))
  12572. return SCRIPT_CMD_FAILURE;
  12573. if (sd->disguise) {
  12574. pc_disguise(sd, 0);
  12575. script_pushint(st,0);
  12576. } else {
  12577. script_pushint(st,1);
  12578. }
  12579. return SCRIPT_CMD_SUCCESS;
  12580. }
  12581. /**
  12582. * Transform an NPC to another _class
  12583. *
  12584. * classchange(<view id>{,"<NPC name>","<flag>"});
  12585. * @param flag: Specify target
  12586. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12587. * BC_SELF - Sprite is sent only to player attached.
  12588. */
  12589. BUILDIN_FUNC(classchange)
  12590. {
  12591. int _class, type = 1;
  12592. struct npc_data* nd = NULL;
  12593. TBL_PC *sd = map_id2sd(st->rid);
  12594. send_target target = AREA;
  12595. _class = script_getnum(st,2);
  12596. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12597. nd = npc_name2id(script_getstr(st, 3));
  12598. else
  12599. nd = (struct npc_data *)map_id2bl(st->oid);
  12600. if (nd == NULL)
  12601. return SCRIPT_CMD_FAILURE;
  12602. if (script_hasdata(st, 4)) {
  12603. switch(script_getnum(st, 4)) {
  12604. case BC_SELF: target = SELF; break;
  12605. case BC_AREA:
  12606. default: target = AREA; break;
  12607. }
  12608. }
  12609. if (target != SELF)
  12610. clif_class_change(&nd->bl,_class,type);
  12611. else if (sd == NULL)
  12612. return SCRIPT_CMD_FAILURE;
  12613. else
  12614. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12615. return SCRIPT_CMD_SUCCESS;
  12616. }
  12617. /*==========================================
  12618. * Display an effect
  12619. *------------------------------------------*/
  12620. BUILDIN_FUNC(misceffect)
  12621. {
  12622. int type;
  12623. type=script_getnum(st,2);
  12624. if( type <= EF_NONE || type >= EF_MAX ){
  12625. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12626. return SCRIPT_CMD_FAILURE;
  12627. }
  12628. if(st->oid && st->oid != fake_nd->bl.id) {
  12629. struct block_list *bl = map_id2bl(st->oid);
  12630. if (bl)
  12631. clif_specialeffect(bl,type,AREA);
  12632. } else{
  12633. TBL_PC *sd;
  12634. if(script_rid2sd(sd))
  12635. clif_specialeffect(&sd->bl,type,AREA);
  12636. }
  12637. return SCRIPT_CMD_SUCCESS;
  12638. }
  12639. /*==========================================
  12640. * Play a BGM on a single client [Rikter/Yommy]
  12641. *------------------------------------------*/
  12642. BUILDIN_FUNC(playBGM)
  12643. {
  12644. struct map_session_data* sd;
  12645. if( script_rid2sd(sd) ) {
  12646. clif_playBGM(sd, script_getstr(st,2));
  12647. }
  12648. return SCRIPT_CMD_SUCCESS;
  12649. }
  12650. static int playBGM_sub(struct block_list* bl,va_list ap)
  12651. {
  12652. const char* name = va_arg(ap,const char*);
  12653. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12654. return 0;
  12655. }
  12656. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12657. {
  12658. const char* name = va_arg(args, const char*);
  12659. clif_playBGM(sd, name);
  12660. return 0;
  12661. }
  12662. /*==========================================
  12663. * Play a BGM on multiple client [Rikter/Yommy]
  12664. *------------------------------------------*/
  12665. BUILDIN_FUNC(playBGMall)
  12666. {
  12667. const char* name;
  12668. name = script_getstr(st,2);
  12669. if( script_hasdata(st,7) ) {// specified part of map
  12670. const char* mapname = script_getstr(st,3);
  12671. int x0 = script_getnum(st,4);
  12672. int y0 = script_getnum(st,5);
  12673. int x1 = script_getnum(st,6);
  12674. int y1 = script_getnum(st,7);
  12675. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12676. }
  12677. else if( script_hasdata(st,3) ) {// entire map
  12678. const char* mapname = script_getstr(st,3);
  12679. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12680. }
  12681. else {// entire server
  12682. map_foreachpc(&playBGM_foreachpc_sub, name);
  12683. }
  12684. return SCRIPT_CMD_SUCCESS;
  12685. }
  12686. /*==========================================
  12687. * Play a .wav sound for sd
  12688. *------------------------------------------*/
  12689. BUILDIN_FUNC(soundeffect)
  12690. {
  12691. TBL_PC* sd;
  12692. if(script_rid2sd(sd)){
  12693. const char* name = script_getstr(st,2);
  12694. int type = script_getnum(st,3);
  12695. clif_soundeffect(sd,&sd->bl,name,type);
  12696. }
  12697. return SCRIPT_CMD_SUCCESS;
  12698. }
  12699. int soundeffect_sub(struct block_list* bl,va_list ap)
  12700. {
  12701. char* name = va_arg(ap,char*);
  12702. int type = va_arg(ap,int);
  12703. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12704. return 0;
  12705. }
  12706. /*==========================================
  12707. * Play a sound effect (.wav) on multiple clients
  12708. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12709. *------------------------------------------*/
  12710. BUILDIN_FUNC(soundeffectall)
  12711. {
  12712. struct block_list* bl;
  12713. struct map_session_data* sd;
  12714. const char* name;
  12715. int type;
  12716. if( st->rid && script_rid2sd(sd) )
  12717. bl = &sd->bl;
  12718. else
  12719. bl = map_id2bl(st->oid);
  12720. if (!bl)
  12721. return SCRIPT_CMD_SUCCESS;
  12722. name = script_getstr(st,2);
  12723. type = script_getnum(st,3);
  12724. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12725. if(!script_hasdata(st,4))
  12726. { // area around
  12727. clif_soundeffectall(bl, name, type, AREA);
  12728. }
  12729. else
  12730. if(!script_hasdata(st,5))
  12731. { // entire map
  12732. const char* mapname = script_getstr(st,4);
  12733. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12734. }
  12735. else
  12736. if(script_hasdata(st,8))
  12737. { // specified part of map
  12738. const char* mapname = script_getstr(st,4);
  12739. int x0 = script_getnum(st,5);
  12740. int y0 = script_getnum(st,6);
  12741. int x1 = script_getnum(st,7);
  12742. int y1 = script_getnum(st,8);
  12743. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12744. }
  12745. else
  12746. {
  12747. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12748. }
  12749. return SCRIPT_CMD_SUCCESS;
  12750. }
  12751. /*==========================================
  12752. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12753. *------------------------------------------*/
  12754. BUILDIN_FUNC(petrecovery)
  12755. {
  12756. struct pet_data *pd;
  12757. TBL_PC *sd;
  12758. int sc;
  12759. if(!script_rid2sd(sd) || sd->pd == NULL)
  12760. return SCRIPT_CMD_FAILURE;
  12761. sc = script_getnum(st,2);
  12762. if (sc <= SC_NONE || sc >= SC_MAX) {
  12763. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12764. return SCRIPT_CMD_FAILURE;
  12765. }
  12766. pd = sd->pd;
  12767. if (pd->recovery)
  12768. { //Halt previous bonus
  12769. if (pd->recovery->timer != INVALID_TIMER)
  12770. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12771. } else //Init
  12772. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12773. pd->recovery->type = (sc_type)sc;
  12774. pd->recovery->delay = script_getnum(st,3);
  12775. pd->recovery->timer = INVALID_TIMER;
  12776. return SCRIPT_CMD_SUCCESS;
  12777. }
  12778. /*==========================================
  12779. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12780. *------------------------------------------*/
  12781. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12782. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12783. BUILDIN_FUNC(petskillattack)
  12784. {
  12785. struct pet_data *pd;
  12786. TBL_PC *sd;
  12787. int id = 0;
  12788. if(!script_rid2sd(sd) || sd->pd == NULL)
  12789. return SCRIPT_CMD_FAILURE;
  12790. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12791. if (!id) {
  12792. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12793. return SCRIPT_CMD_FAILURE;
  12794. }
  12795. pd = sd->pd;
  12796. if (pd->a_skill == NULL)
  12797. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12798. pd->a_skill->id = id;
  12799. pd->a_skill->damage = 0;
  12800. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12801. pd->a_skill->div_ = 0;
  12802. pd->a_skill->rate = script_getnum(st,4);
  12803. pd->a_skill->bonusrate = script_getnum(st,5);
  12804. return SCRIPT_CMD_SUCCESS;
  12805. }
  12806. /*==========================================
  12807. * pet attack skills [Valaris]
  12808. *------------------------------------------*/
  12809. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12810. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12811. BUILDIN_FUNC(petskillattack2)
  12812. {
  12813. struct pet_data *pd;
  12814. TBL_PC *sd;
  12815. int id = 0;
  12816. if(!script_rid2sd(sd) || sd->pd == NULL)
  12817. return SCRIPT_CMD_FAILURE;
  12818. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12819. if (!id) {
  12820. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12821. return SCRIPT_CMD_FAILURE;
  12822. }
  12823. pd = sd->pd;
  12824. if (pd->a_skill == NULL)
  12825. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12826. pd->a_skill->id = id;
  12827. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12828. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12829. pd->a_skill->div_ = script_getnum(st,4);
  12830. pd->a_skill->rate = script_getnum(st,5);
  12831. pd->a_skill->bonusrate = script_getnum(st,6);
  12832. return SCRIPT_CMD_SUCCESS;
  12833. }
  12834. /*==========================================
  12835. * pet support skills [Skotlex]
  12836. *------------------------------------------*/
  12837. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12838. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12839. BUILDIN_FUNC(petskillsupport)
  12840. {
  12841. struct pet_data *pd;
  12842. TBL_PC *sd;
  12843. int id = 0;
  12844. if(!script_rid2sd(sd) || sd->pd == NULL)
  12845. return SCRIPT_CMD_FAILURE;
  12846. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12847. if (!id) {
  12848. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12849. return SCRIPT_CMD_FAILURE;
  12850. }
  12851. pd = sd->pd;
  12852. if (pd->s_skill)
  12853. { //Clear previous skill
  12854. if (pd->s_skill->timer != INVALID_TIMER)
  12855. {
  12856. if (pd->s_skill->id)
  12857. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12858. else
  12859. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12860. }
  12861. } else //init memory
  12862. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12863. pd->s_skill->id = id;
  12864. pd->s_skill->lv = script_getnum(st,3);
  12865. pd->s_skill->delay = script_getnum(st,4);
  12866. pd->s_skill->hp = script_getnum(st,5);
  12867. pd->s_skill->sp = script_getnum(st,6);
  12868. //Use delay as initial offset to avoid skill/heal exploits
  12869. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12870. pd->s_skill->timer = INVALID_TIMER;
  12871. else
  12872. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12873. return SCRIPT_CMD_SUCCESS;
  12874. }
  12875. static inline void script_skill_effect(block_list *bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y) {
  12876. nullpo_retv(bl);
  12877. switch (skill_get_casttype(skill_id)) {
  12878. case CAST_GROUND:
  12879. clif_skill_poseffect(bl, skill_id, skill_lv, x, y, gettick());
  12880. break;
  12881. case CAST_NODAMAGE:
  12882. clif_skill_nodamage(bl, bl, skill_id, skill_lv, 1);
  12883. break;
  12884. case CAST_DAMAGE:
  12885. 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));
  12886. break;
  12887. }
  12888. }
  12889. /*==========================================
  12890. * Scripted skill effects [Celest]
  12891. *------------------------------------------*/
  12892. /// skilleffect <skill id>,<level>
  12893. /// skilleffect "<skill name>",<level>
  12894. BUILDIN_FUNC(skilleffect)
  12895. {
  12896. TBL_PC *sd;
  12897. uint16 skill_id, skill_lv;
  12898. if( !script_rid2sd(sd) )
  12899. return SCRIPT_CMD_FAILURE;
  12900. skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12901. skill_lv = script_getnum(st,3);
  12902. if (skill_db.find(skill_id) == nullptr) {
  12903. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12904. return SCRIPT_CMD_FAILURE;
  12905. }
  12906. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12907. * which leaves the server thinking it still is sitting. */
  12908. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12909. skill_sit(sd, 0);
  12910. clif_standing(&sd->bl);
  12911. }
  12912. script_skill_effect(&sd->bl, skill_id, skill_lv, sd->bl.x, sd->bl.y);
  12913. return SCRIPT_CMD_SUCCESS;
  12914. }
  12915. /*==========================================
  12916. * NPC skill effects [Valaris]
  12917. *------------------------------------------*/
  12918. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12919. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12920. BUILDIN_FUNC(npcskilleffect)
  12921. {
  12922. struct block_list *bl= map_id2bl(st->oid);
  12923. if (bl == nullptr) {
  12924. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  12925. return SCRIPT_CMD_FAILURE;
  12926. }
  12927. uint16 skill_id, skill_lv;
  12928. int16 x, y;
  12929. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12930. skill_lv=script_getnum(st,3);
  12931. x=script_getnum(st,4);
  12932. y=script_getnum(st,5);
  12933. if (skill_db.find(skill_id) == nullptr) {
  12934. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12935. return SCRIPT_CMD_FAILURE;
  12936. }
  12937. script_skill_effect(bl, skill_id, skill_lv, bl->x, bl->y);
  12938. return SCRIPT_CMD_SUCCESS;
  12939. }
  12940. /*==========================================
  12941. * Special effects [Valaris]
  12942. *------------------------------------------*/
  12943. BUILDIN_FUNC(specialeffect)
  12944. {
  12945. struct block_list *bl=map_id2bl(st->oid);
  12946. int type = script_getnum(st,2);
  12947. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12948. if(bl==NULL)
  12949. return SCRIPT_CMD_SUCCESS;
  12950. if( type <= EF_NONE || type >= EF_MAX ){
  12951. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  12952. return SCRIPT_CMD_FAILURE;
  12953. }
  12954. if( script_hasdata(st,4) )
  12955. {
  12956. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12957. if(nd)
  12958. clif_specialeffect(&nd->bl, type, target);
  12959. }
  12960. else
  12961. {
  12962. if (target == SELF) {
  12963. TBL_PC *sd;
  12964. if (script_rid2sd(sd))
  12965. clif_specialeffect_single(bl,type,sd->fd);
  12966. } else {
  12967. clif_specialeffect(bl, type, target);
  12968. }
  12969. }
  12970. return SCRIPT_CMD_SUCCESS;
  12971. }
  12972. BUILDIN_FUNC(specialeffect2)
  12973. {
  12974. TBL_PC *sd;
  12975. if( script_nick2sd(4,sd) ){
  12976. int type = script_getnum(st,2);
  12977. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12978. if( type <= EF_NONE || type >= EF_MAX ){
  12979. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  12980. return SCRIPT_CMD_FAILURE;
  12981. }
  12982. clif_specialeffect(&sd->bl, type, target);
  12983. }
  12984. return SCRIPT_CMD_SUCCESS;
  12985. }
  12986. BUILDIN_FUNC(removespecialeffect)
  12987. {
  12988. const char* command = script_getfuncname(st);
  12989. #if PACKETVER < 20181002
  12990. ShowError("buildin_%s: This command is not supported for PACKETVER older than 2018-10-02.\n", command);
  12991. return SCRIPT_CMD_FAILURE;
  12992. #endif
  12993. int effect = script_getnum(st, 2);
  12994. if (effect <= EF_NONE || effect >= EF_MAX) {
  12995. ShowError("buildin_%s: unsupported effect id %d.\n", command, effect);
  12996. return SCRIPT_CMD_FAILURE;
  12997. }
  12998. send_target e_target = script_hasdata(st, 3) ? static_cast<send_target>(script_getnum(st, 3)) : AREA;
  12999. struct block_list *bl_src;
  13000. struct block_list *bl_target;
  13001. struct map_session_data *sd;
  13002. if( strcmp(command, "removespecialeffect") == 0 ) {
  13003. if (!script_hasdata(st, 4)) {
  13004. bl_src = map_id2bl(st->oid);
  13005. if (bl_src == nullptr) {
  13006. ShowError("buildin_%s: npc not attached.\n", command);
  13007. return SCRIPT_CMD_FAILURE;
  13008. }
  13009. }
  13010. else {
  13011. struct npc_data *nd = npc_name2id(script_getstr(st, 4));
  13012. if (nd == nullptr) {
  13013. ShowError("buildin_%s: unknown npc %s.\n", command, script_getstr(st, 4));
  13014. return SCRIPT_CMD_FAILURE;
  13015. }
  13016. bl_src = &nd->bl;
  13017. }
  13018. if (e_target != SELF)
  13019. bl_target = bl_src;
  13020. else {
  13021. if (!script_rid2sd(sd))
  13022. return SCRIPT_CMD_FAILURE;
  13023. bl_target = &sd->bl;
  13024. }
  13025. }else{
  13026. if (!script_nick2sd(4, sd))
  13027. return SCRIPT_CMD_FAILURE;
  13028. bl_src = bl_target = &sd->bl;
  13029. }
  13030. clif_specialeffect_remove(bl_src, effect, e_target, bl_target);
  13031. return SCRIPT_CMD_SUCCESS;
  13032. }
  13033. /**
  13034. * nude({<char_id>});
  13035. * @author [Valaris]
  13036. **/
  13037. BUILDIN_FUNC(nude)
  13038. {
  13039. TBL_PC *sd;
  13040. int i, calcflag = 0;
  13041. if (!script_charid2sd(2,sd))
  13042. return SCRIPT_CMD_FAILURE;
  13043. for( i = 0 ; i < EQI_MAX; i++ ) {
  13044. if( sd->equip_index[ i ] >= 0 ) {
  13045. if( !calcflag )
  13046. calcflag = 1;
  13047. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  13048. }
  13049. }
  13050. if( calcflag )
  13051. status_calc_pc(sd,SCO_NONE);
  13052. return SCRIPT_CMD_SUCCESS;
  13053. }
  13054. int atcommand_sub(struct script_state* st,int type) {
  13055. TBL_PC *sd, dummy_sd;
  13056. int fd;
  13057. const char *cmd;
  13058. cmd = script_getstr(st,2);
  13059. if (st->rid) {
  13060. if( !script_rid2sd(sd) )
  13061. return SCRIPT_CMD_SUCCESS;
  13062. fd = sd->fd;
  13063. } else { //Use a dummy character.
  13064. sd = &dummy_sd;
  13065. fd = 0;
  13066. memset(&dummy_sd, 0, sizeof(TBL_PC));
  13067. if (st->oid) {
  13068. struct block_list* bl = map_id2bl(st->oid);
  13069. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  13070. if (bl->type == BL_NPC)
  13071. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  13072. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  13073. }
  13074. // Init Group ID, Level, & permissions
  13075. sd->group_id = sd->group_level = 99;
  13076. sd->permissions |= PC_PERM_ALLPERMISSION;
  13077. }
  13078. if (!is_atcommand(fd, sd, cmd, type)) {
  13079. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  13080. script_reportsrc(st);
  13081. return SCRIPT_CMD_FAILURE;
  13082. }
  13083. return SCRIPT_CMD_SUCCESS;
  13084. }
  13085. /*==========================================
  13086. * gmcommand [MouseJstr]
  13087. *------------------------------------------*/
  13088. BUILDIN_FUNC(atcommand) {
  13089. return atcommand_sub(st,0);
  13090. }
  13091. /** Displays a message for the player only (like system messages like "you got an apple" )
  13092. * dispbottom("<message>"{,<color>{,<char_id>}})
  13093. * @param message
  13094. * @param color Hex color default (Green)
  13095. */
  13096. BUILDIN_FUNC(dispbottom)
  13097. {
  13098. TBL_PC *sd;
  13099. int color = 0;
  13100. const char *message;
  13101. if (!script_charid2sd(4,sd))
  13102. return SCRIPT_CMD_FAILURE;
  13103. message = script_getstr(st,2);
  13104. if (script_hasdata(st,3))
  13105. color = script_getnum(st,3); // <color>
  13106. if(sd) {
  13107. if (script_hasdata(st,3))
  13108. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  13109. else
  13110. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  13111. }
  13112. return SCRIPT_CMD_SUCCESS;
  13113. }
  13114. /*===================================
  13115. * Heal portion of recovery command
  13116. *-----------------------------------*/
  13117. int recovery_sub(struct map_session_data* sd, int revive)
  13118. {
  13119. if(revive&(1|4) && pc_isdead(sd)) {
  13120. status_revive(&sd->bl, 100, 100);
  13121. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  13122. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  13123. } else if(revive&(1|2) && !pc_isdead(sd)) {
  13124. status_percent_heal(&sd->bl, 100, 100);
  13125. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  13126. }
  13127. return SCRIPT_CMD_SUCCESS;
  13128. }
  13129. /*=========================================================================
  13130. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  13131. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  13132. * <type> determines <option>:
  13133. * 0 : char_id
  13134. * 1 : party_id
  13135. * 2 : guild_id
  13136. * 3 : map_name
  13137. * 4 : all characters
  13138. * <revive_flag>:
  13139. * 1 : Revive and heal all players (default)
  13140. * 2 : Heal living players only
  13141. * 4 : Revive dead players only
  13142. * <map name>:
  13143. * for types 1-2 : map_name (null = all maps)
  13144. *-------------------------------------------------------------------------*/
  13145. BUILDIN_FUNC(recovery)
  13146. {
  13147. TBL_PC *sd;
  13148. int map_idx = 0, type = 0, revive = 1;
  13149. type = script_getnum(st,2);
  13150. if(script_hasdata(st,4))
  13151. revive = script_getnum(st,4);
  13152. switch(type) {
  13153. case 0:
  13154. if(!script_charid2sd(3,sd))
  13155. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  13156. recovery_sub(sd, revive);
  13157. break;
  13158. case 1:
  13159. {
  13160. struct party_data* p;
  13161. //When no party given, we use invoker party
  13162. int p_id = 0, i;
  13163. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13164. map_idx = map_mapname2mapid(script_getstr(st,5));
  13165. if(map_idx < 1) { //But we'll check anyways
  13166. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13167. return SCRIPT_CMD_FAILURE;
  13168. }
  13169. }
  13170. if(script_hasdata(st,3))
  13171. p_id = script_getnum(st,3);
  13172. else if(script_rid2sd(sd))
  13173. p_id = sd->status.party_id;
  13174. p = party_search(p_id);
  13175. if(p == NULL)
  13176. return SCRIPT_CMD_SUCCESS;
  13177. for (i = 0; i < MAX_PARTY; i++) {
  13178. struct map_session_data* pl_sd;
  13179. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  13180. || (map_idx && pl_sd->bl.m != map_idx))
  13181. continue;
  13182. recovery_sub(pl_sd, revive);
  13183. }
  13184. break;
  13185. }
  13186. case 2:
  13187. {
  13188. struct guild* g;
  13189. //When no guild given, we use invoker guild
  13190. int g_id = 0, i;
  13191. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  13192. map_idx = map_mapname2mapid(script_getstr(st,5));
  13193. if(map_idx < 1) { //But we'll check anyways
  13194. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  13195. return SCRIPT_CMD_FAILURE;
  13196. }
  13197. }
  13198. if(script_hasdata(st,3))
  13199. g_id = script_getnum(st,3);
  13200. else if(script_rid2sd(sd))
  13201. g_id = sd->status.guild_id;
  13202. g = guild_search(g_id);
  13203. if(g == NULL)
  13204. return SCRIPT_CMD_SUCCESS;
  13205. for (i = 0; i < MAX_GUILD; i++) {
  13206. struct map_session_data* pl_sd;
  13207. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  13208. || (map_idx && pl_sd->bl.m != map_idx))
  13209. continue;
  13210. recovery_sub(pl_sd, revive);
  13211. }
  13212. break;
  13213. }
  13214. case 3:
  13215. if(script_hasdata(st,3))
  13216. map_idx = map_mapname2mapid(script_getstr(st,3));
  13217. else if(script_rid2sd(sd))
  13218. map_idx = sd->bl.m;
  13219. if(map_idx < 1)
  13220. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  13221. case 4:
  13222. {
  13223. struct s_mapiterator *iter;
  13224. if(script_hasdata(st,3) && script_isint(st, 3))
  13225. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  13226. iter = mapit_getallusers();
  13227. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  13228. if(type == 3 && sd->bl.m != map_idx)
  13229. continue;
  13230. recovery_sub(sd, revive);
  13231. }
  13232. mapit_free(iter);
  13233. break;
  13234. }
  13235. default:
  13236. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  13237. script_pushint(st,-1);
  13238. return SCRIPT_CMD_FAILURE;
  13239. }
  13240. script_pushint(st,1); //Successfully executed without errors
  13241. return SCRIPT_CMD_SUCCESS;
  13242. }
  13243. /*==========================================
  13244. * Get your pet info: getpetinfo <type>{,<char_id>}
  13245. * n -> 0:pet_id 1:pet_class 2:pet_name
  13246. * 3:friendly 4:hungry, 5: rename flag.6:level,
  13247. * 7:game id
  13248. *------------------------------------------*/
  13249. BUILDIN_FUNC(getpetinfo)
  13250. {
  13251. TBL_PC *sd;
  13252. TBL_PET *pd;
  13253. int type = script_getnum(st,2);
  13254. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  13255. if (type == 2)
  13256. script_pushconststr(st,"null");
  13257. else
  13258. script_pushint(st,0);
  13259. return SCRIPT_CMD_SUCCESS;
  13260. }
  13261. switch(type){
  13262. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13263. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13264. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13265. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13266. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13267. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13268. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13269. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13270. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13271. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13272. default:
  13273. script_pushint(st,0);
  13274. break;
  13275. }
  13276. return SCRIPT_CMD_SUCCESS;
  13277. }
  13278. /*==========================================
  13279. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13280. * type -> 0:hom_id 1:class 2:name
  13281. * 3:friendly 4:hungry, 5: rename flag.
  13282. * 6: level, 7: game id
  13283. *------------------------------------------*/
  13284. BUILDIN_FUNC(gethominfo)
  13285. {
  13286. TBL_PC *sd;
  13287. TBL_HOM *hd;
  13288. int type=script_getnum(st,2);
  13289. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13290. if (type == 2)
  13291. script_pushconststr(st,"null");
  13292. else
  13293. script_pushint(st,0);
  13294. return SCRIPT_CMD_SUCCESS;
  13295. }
  13296. switch(type){
  13297. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13298. case 1: script_pushint(st,hd->homunculus.class_); break;
  13299. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13300. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13301. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13302. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13303. case 6: script_pushint(st,hd->homunculus.level); break;
  13304. case 7: script_pushint(st,hd->bl.id); break;
  13305. default:
  13306. script_pushint(st,0);
  13307. break;
  13308. }
  13309. return SCRIPT_CMD_SUCCESS;
  13310. }
  13311. BUILDIN_FUNC(addhomintimacy)
  13312. {
  13313. map_session_data *sd;
  13314. homun_data *hd;
  13315. if (!script_charid2sd(3, sd) || !(hd = sd->hd))
  13316. return SCRIPT_CMD_FAILURE;
  13317. int32 value = script_getnum(st, 2);
  13318. if (value == 0) // Nothing to change
  13319. return SCRIPT_CMD_SUCCESS;
  13320. if (value > 0)
  13321. hom_increase_intimacy(hd, (uint32)value);
  13322. else
  13323. hom_decrease_intimacy(hd, (uint32)abs(value));
  13324. clif_send_homdata(sd, SP_INTIMATE, hd->homunculus.intimacy / 100);
  13325. return SCRIPT_CMD_SUCCESS;
  13326. }
  13327. /// Retrieves information about character's mercenary
  13328. /// getmercinfo <type>[,<char id>];
  13329. BUILDIN_FUNC(getmercinfo)
  13330. {
  13331. int type;
  13332. struct map_session_data* sd;
  13333. s_mercenary_data* md;
  13334. if( !script_charid2sd(3,sd) ){
  13335. script_pushnil(st);
  13336. return SCRIPT_CMD_FAILURE;
  13337. }
  13338. type = script_getnum(st,2);
  13339. md = sd->md;
  13340. if( md == NULL ){
  13341. if( type == 2 )
  13342. script_pushconststr(st,"");
  13343. else
  13344. script_pushint(st,0);
  13345. return SCRIPT_CMD_SUCCESS;
  13346. }
  13347. switch( type )
  13348. {
  13349. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13350. case 1: script_pushint(st,md->mercenary.class_); break;
  13351. case 2: script_pushstrcopy(st,md->db->name.c_str()); break;
  13352. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13353. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13354. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13355. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13356. case 7: script_pushint(st,md->db->lv); break;
  13357. case 8: script_pushint(st,md->bl.id); break;
  13358. default:
  13359. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13360. script_pushnil(st);
  13361. return SCRIPT_CMD_FAILURE;
  13362. }
  13363. return SCRIPT_CMD_SUCCESS;
  13364. }
  13365. /*==========================================
  13366. * Shows wether your inventory(and equips) contain
  13367. selected card or not.
  13368. checkequipedcard(4001);
  13369. *------------------------------------------*/
  13370. BUILDIN_FUNC(checkequipedcard)
  13371. {
  13372. TBL_PC *sd;
  13373. if(script_rid2sd(sd)){
  13374. int n,i,c=0;
  13375. c=script_getnum(st,2);
  13376. for(i=0;i<MAX_INVENTORY;i++){
  13377. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13378. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13379. continue;
  13380. for(n=0;n<sd->inventory_data[i]->slots;n++){
  13381. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13382. script_pushint(st,1);
  13383. return SCRIPT_CMD_SUCCESS;
  13384. }
  13385. }
  13386. }
  13387. }
  13388. }
  13389. script_pushint(st,0);
  13390. return SCRIPT_CMD_SUCCESS;
  13391. }
  13392. BUILDIN_FUNC(jump_zero)
  13393. {
  13394. int64 sel=script_getnum64(st,2);
  13395. if(!sel) {
  13396. int pos;
  13397. if( !data_islabel(script_getdata(st,3)) ){
  13398. ShowError("script: jump_zero: not label !\n");
  13399. st->state=END;
  13400. return SCRIPT_CMD_FAILURE;
  13401. }
  13402. pos=script_getnum(st,3);
  13403. st->pos=pos;
  13404. st->state=GOTO;
  13405. }
  13406. return SCRIPT_CMD_SUCCESS;
  13407. }
  13408. /*==========================================
  13409. * movenpc [MouseJstr]
  13410. *------------------------------------------*/
  13411. BUILDIN_FUNC(movenpc)
  13412. {
  13413. TBL_NPC *nd = NULL;
  13414. const char *npc;
  13415. int x,y;
  13416. npc = script_getstr(st,2);
  13417. x = script_getnum(st,3);
  13418. y = script_getnum(st,4);
  13419. if ((nd = npc_name2id(npc)) == NULL){
  13420. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13421. return -1;
  13422. }
  13423. if (script_hasdata(st,5))
  13424. nd->ud.dir = script_getnum(st,5) % 8;
  13425. npc_movenpc(nd, x, y);
  13426. return SCRIPT_CMD_SUCCESS;
  13427. }
  13428. /*==========================================
  13429. * message [MouseJstr]
  13430. *------------------------------------------*/
  13431. BUILDIN_FUNC(message)
  13432. {
  13433. const char *msg,*player;
  13434. TBL_PC *pl_sd = NULL;
  13435. player = script_getstr(st,2);
  13436. msg = script_getstr(st,3);
  13437. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13438. return SCRIPT_CMD_SUCCESS;
  13439. clif_displaymessage(pl_sd->fd, msg);
  13440. return SCRIPT_CMD_SUCCESS;
  13441. }
  13442. /**
  13443. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13444. * @param flag: Specify target
  13445. * BC_ALL - Broadcast message is sent server-wide.
  13446. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13447. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13448. * BC_SELF - Message is sent only to player attached.
  13449. */
  13450. BUILDIN_FUNC(npctalk)
  13451. {
  13452. struct npc_data* nd = NULL;
  13453. const char* str = script_getstr(st,2);
  13454. int color = 0xFFFFFF;
  13455. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13456. nd = npc_name2id(script_getstr(st, 3));
  13457. else
  13458. nd = (struct npc_data *)map_id2bl(st->oid);
  13459. if (script_hasdata(st, 5))
  13460. color = script_getnum(st, 5);
  13461. if (nd != NULL) {
  13462. send_target target = AREA;
  13463. char message[CHAT_SIZE_MAX];
  13464. if (script_hasdata(st, 4)) {
  13465. switch(script_getnum(st, 4)) {
  13466. case BC_ALL: target = ALL_CLIENT; break;
  13467. case BC_MAP: target = ALL_SAMEMAP; break;
  13468. case BC_SELF: target = SELF; break;
  13469. case BC_AREA:
  13470. default: target = AREA; break;
  13471. }
  13472. }
  13473. safesnprintf(message, sizeof(message), "%s", str);
  13474. if (target != SELF)
  13475. clif_messagecolor(&nd->bl, color, message, true, target);
  13476. else {
  13477. TBL_PC *sd = map_id2sd(st->rid);
  13478. if (sd == NULL)
  13479. return SCRIPT_CMD_FAILURE;
  13480. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13481. }
  13482. }
  13483. return SCRIPT_CMD_SUCCESS;
  13484. }
  13485. /**
  13486. * Sends a message to the waitingroom of the invoking NPC.
  13487. * chatmes "<message>"{,"<NPC name>"};
  13488. * @author Jey
  13489. */
  13490. BUILDIN_FUNC(chatmes)
  13491. {
  13492. struct npc_data* nd = NULL;
  13493. const char* str = script_getstr(st,2);
  13494. if (script_hasdata(st, 3))
  13495. nd = npc_name2id(script_getstr(st, 3));
  13496. else
  13497. nd = (struct npc_data *)map_id2bl(st->oid);
  13498. if (nd != NULL && nd->chat_id) {
  13499. char message[256];
  13500. safesnprintf(message, sizeof(message), "%s", str);
  13501. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13502. }
  13503. return SCRIPT_CMD_SUCCESS;
  13504. }
  13505. // change npc walkspeed [Valaris]
  13506. BUILDIN_FUNC(npcspeed)
  13507. {
  13508. struct npc_data* nd;
  13509. int speed;
  13510. speed = script_getnum(st,2);
  13511. nd =(struct npc_data *)map_id2bl(st->oid);
  13512. if( nd ) {
  13513. nd->speed = speed;
  13514. nd->ud.state.speed_changed = 1;
  13515. }
  13516. return SCRIPT_CMD_SUCCESS;
  13517. }
  13518. // make an npc walk to a position [Valaris]
  13519. BUILDIN_FUNC(npcwalkto)
  13520. {
  13521. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13522. int x=0,y=0;
  13523. x=script_getnum(st,2);
  13524. y=script_getnum(st,3);
  13525. if(nd) {
  13526. if (!nd->status.hp)
  13527. status_calc_npc(nd, SCO_FIRST);
  13528. else
  13529. status_calc_npc(nd, SCO_NONE);
  13530. unit_walktoxy(&nd->bl,x,y,0);
  13531. }
  13532. return SCRIPT_CMD_SUCCESS;
  13533. }
  13534. // stop an npc's movement [Valaris]
  13535. BUILDIN_FUNC(npcstop)
  13536. {
  13537. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13538. if(nd) {
  13539. unit_stop_walking(&nd->bl,1|4);
  13540. }
  13541. return SCRIPT_CMD_SUCCESS;
  13542. }
  13543. /**
  13544. * getlook(<type>{,<char_id>})
  13545. **/
  13546. BUILDIN_FUNC(getlook)
  13547. {
  13548. int type,val;
  13549. TBL_PC *sd;
  13550. if (!script_charid2sd(3,sd)) {
  13551. script_pushint(st,-1);
  13552. return SCRIPT_CMD_FAILURE;
  13553. }
  13554. type=script_getnum(st,2);
  13555. val=-1;
  13556. switch(type) {
  13557. // TODO: implement LOOK_BASE as stated in script doc
  13558. case LOOK_HAIR: val=sd->status.hair; break; //1
  13559. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13560. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13561. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13562. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13563. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13564. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13565. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13566. case LOOK_SHOES: break; //9
  13567. case LOOK_ROBE: val=sd->status.robe; break; //12
  13568. case LOOK_BODY2: val=sd->status.body; break; //13
  13569. }
  13570. script_pushint(st,val);
  13571. return SCRIPT_CMD_SUCCESS;
  13572. }
  13573. /**
  13574. * getsavepoint(<information type>{,<char_id>})
  13575. * @param type 0- map name, 1- x, 2- y
  13576. **/
  13577. BUILDIN_FUNC(getsavepoint)
  13578. {
  13579. TBL_PC* sd;
  13580. int type;
  13581. if (!script_charid2sd(3,sd)) {
  13582. script_pushint(st,0);
  13583. return SCRIPT_CMD_SUCCESS;
  13584. }
  13585. type = script_getnum(st,2);
  13586. switch(type) {
  13587. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13588. case 1: script_pushint(st,sd->status.save_point.x); break;
  13589. case 2: script_pushint(st,sd->status.save_point.y); break;
  13590. default:
  13591. script_pushint(st,0);
  13592. break;
  13593. }
  13594. return SCRIPT_CMD_SUCCESS;
  13595. }
  13596. /**
  13597. * Get position for BL objects.
  13598. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13599. * @param mapname: String variable for output map name
  13600. * @param x: Integer variable for output coord X
  13601. * @param y: Integer variable for output coord Y
  13602. * @param type: Type of object
  13603. * BL_PC - Character coord
  13604. * BL_NPC - NPC coord
  13605. * BL_PET - Pet coord
  13606. * BL_HOM - Homun coord
  13607. * BL_MER - Mercenary coord
  13608. * BL_ELEM - Elemental coord
  13609. * @param charname: Name object. If empty or "this" use the current object
  13610. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13611. */
  13612. BUILDIN_FUNC(getmapxy)
  13613. {
  13614. struct block_list *bl = NULL;
  13615. TBL_PC *sd=NULL;
  13616. int64 num;
  13617. const char *name;
  13618. char prefix;
  13619. int x,y,type = BL_PC;
  13620. char mapname[MAP_NAME_LENGTH];
  13621. if( !data_isreference(script_getdata(st,2)) ) {
  13622. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13623. script_pushint(st,-1);
  13624. return SCRIPT_CMD_FAILURE;
  13625. }
  13626. if( !data_isreference(script_getdata(st,3)) ) {
  13627. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13628. script_pushint(st,-1);
  13629. return SCRIPT_CMD_FAILURE;
  13630. }
  13631. if( !data_isreference(script_getdata(st,4)) ) {
  13632. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13633. script_pushint(st,-1);
  13634. return SCRIPT_CMD_FAILURE;
  13635. }
  13636. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13637. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13638. script_pushint(st,-1);
  13639. return SCRIPT_CMD_FAILURE;
  13640. }
  13641. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13642. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13643. script_pushint(st,-1);
  13644. return SCRIPT_CMD_FAILURE;
  13645. }
  13646. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13647. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13648. script_pushint(st,-1);
  13649. return SCRIPT_CMD_FAILURE;
  13650. }
  13651. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13652. if (script_hasdata(st, 5))
  13653. type = script_getnum(st, 5);
  13654. switch (type) {
  13655. case BL_PC: //Get Character Position
  13656. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  13657. bl = &sd->bl;
  13658. break;
  13659. case BL_NPC: //Get NPC Position
  13660. if (script_hasdata(st, 6)) {
  13661. struct npc_data *nd;
  13662. if (script_isstring(st, 6))
  13663. nd = npc_name2id(script_getstr(st, 6));
  13664. else
  13665. nd = map_id2nd(script_getnum(st, 6));
  13666. if (nd)
  13667. bl = &nd->bl;
  13668. } else //In case the origin is not an NPC?
  13669. bl = map_id2bl(st->oid);
  13670. break;
  13671. case BL_PET: //Get Pet Position
  13672. if (sd->pd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13673. bl = &sd->pd->bl;
  13674. break;
  13675. case BL_HOM: //Get Homun Position
  13676. if (sd->hd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13677. bl = &sd->hd->bl;
  13678. break;
  13679. case BL_MER: //Get Mercenary Position
  13680. if (sd->md && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13681. bl = &sd->md->bl;
  13682. break;
  13683. case BL_ELEM: //Get Elemental Position
  13684. if (sd->ed && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13685. bl = &sd->ed->bl;
  13686. break;
  13687. default:
  13688. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13689. script_pushint(st,-1);
  13690. return SCRIPT_CMD_FAILURE;
  13691. }
  13692. if (!bl) { //No object found.
  13693. script_pushint(st,-1);
  13694. return SCRIPT_CMD_SUCCESS;
  13695. }
  13696. x= bl->x;
  13697. y= bl->y;
  13698. if (bl->m >= 0)
  13699. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  13700. else
  13701. memset(mapname, '\0', sizeof(mapname));
  13702. //Set MapName$
  13703. num=st->stack->stack_data[st->start+2].u.num;
  13704. name=get_str(script_getvarid(num));
  13705. prefix=*name;
  13706. if(not_server_variable(prefix)){
  13707. if( !script_rid2sd(sd) ){
  13708. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13709. return SCRIPT_CMD_FAILURE;
  13710. }
  13711. }else
  13712. sd=NULL;
  13713. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  13714. //Set MapX
  13715. num=st->stack->stack_data[st->start+3].u.num;
  13716. name=get_str(script_getvarid(num));
  13717. prefix=*name;
  13718. if(not_server_variable(prefix)){
  13719. if( !script_rid2sd(sd) ){
  13720. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13721. return SCRIPT_CMD_FAILURE;
  13722. }
  13723. }else
  13724. sd=NULL;
  13725. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  13726. //Set MapY
  13727. num=st->stack->stack_data[st->start+4].u.num;
  13728. name=get_str(script_getvarid(num));
  13729. prefix=*name;
  13730. if(not_server_variable(prefix)){
  13731. if( !script_rid2sd(sd) ){
  13732. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13733. return SCRIPT_CMD_FAILURE;
  13734. }
  13735. }else
  13736. sd=NULL;
  13737. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  13738. //Return Success value
  13739. script_pushint(st,0);
  13740. return SCRIPT_CMD_SUCCESS;
  13741. }
  13742. /// Returns the map name of given map ID.
  13743. ///
  13744. /// mapid2name <map ID>;
  13745. BUILDIN_FUNC(mapid2name)
  13746. {
  13747. uint16 m = script_getnum(st, 2);
  13748. if (m < 0) {
  13749. script_pushconststr(st, "");
  13750. return SCRIPT_CMD_FAILURE;
  13751. }
  13752. script_pushstrcopy(st, map_mapid2mapname(m));
  13753. return SCRIPT_CMD_SUCCESS;
  13754. }
  13755. /*==========================================
  13756. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13757. *------------------------------------------*/
  13758. BUILDIN_FUNC(logmes)
  13759. {
  13760. const char *str;
  13761. struct map_session_data* sd = map_id2sd(st->rid);
  13762. str = script_getstr(st,2);
  13763. if( sd ){
  13764. log_npc(sd,str);
  13765. }else{
  13766. struct npc_data* nd = map_id2nd(st->oid);
  13767. if( !nd ){
  13768. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  13769. return SCRIPT_CMD_FAILURE;
  13770. }
  13771. log_npc(nd,str);
  13772. }
  13773. return SCRIPT_CMD_SUCCESS;
  13774. }
  13775. BUILDIN_FUNC(summon)
  13776. {
  13777. int _class, timeout=0;
  13778. const char *str,*event="";
  13779. TBL_PC *sd;
  13780. struct mob_data *md;
  13781. t_tick tick = gettick();
  13782. if (!script_rid2sd(sd))
  13783. return SCRIPT_CMD_SUCCESS;
  13784. str =script_getstr(st,2);
  13785. _class=script_getnum(st,3);
  13786. if( script_hasdata(st,4) )
  13787. timeout=script_getnum(st,4);
  13788. if( script_hasdata(st,5) ){
  13789. event=script_getstr(st,5);
  13790. check_event(st, event);
  13791. }
  13792. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13793. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13794. if (md) {
  13795. md->master_id=sd->bl.id;
  13796. md->special_state.ai = AI_ATTACK;
  13797. if( md->deletetimer != INVALID_TIMER )
  13798. delete_timer(md->deletetimer, mob_timer_delete);
  13799. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13800. mob_spawn (md); //Now it is ready for spawning.
  13801. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  13802. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13803. }
  13804. script_pushint(st, md->bl.id);
  13805. return SCRIPT_CMD_SUCCESS;
  13806. }
  13807. /*==========================================
  13808. * Checks whether it is daytime/nighttime
  13809. *------------------------------------------*/
  13810. BUILDIN_FUNC(isnight)
  13811. {
  13812. script_pushint(st,(night_flag == 1));
  13813. return SCRIPT_CMD_SUCCESS;
  13814. }
  13815. BUILDIN_FUNC(isday)
  13816. {
  13817. script_pushint(st,(night_flag == 0));
  13818. return SCRIPT_CMD_SUCCESS;
  13819. }
  13820. /*================================================
  13821. * Check how many items/cards in the list are
  13822. * equipped - used for 2/15's cards patch [celest]
  13823. *------------------------------------------------*/
  13824. BUILDIN_FUNC(isequippedcnt)
  13825. {
  13826. TBL_PC *sd;
  13827. if (!script_rid2sd(sd)) {
  13828. script_pushint(st,0);
  13829. return SCRIPT_CMD_SUCCESS;
  13830. }
  13831. int ret = 0;
  13832. int total = script_lastdata(st);
  13833. std::vector<int32> list(total);
  13834. for (int i = 2; i <= total; ++i) {
  13835. int id = script_getnum(st,i);
  13836. if (id <= 0)
  13837. continue;
  13838. if (std::find(list.begin(), list.end(), id) != list.end())
  13839. continue;
  13840. list.push_back(id);
  13841. for (short j = 0; j < EQI_MAX; j++) {
  13842. short index = sd->equip_index[j];
  13843. if (index < 0)
  13844. continue;
  13845. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13846. continue;
  13847. if (!sd->inventory_data[index])
  13848. continue;
  13849. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13850. if (sd->inventory_data[index]->nameid == id)
  13851. ret += sd->inventory.u.items_inventory[index].amount;
  13852. } else { //Count cards.
  13853. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13854. continue; //No cards
  13855. for (short k = 0; k < sd->inventory_data[index]->slots; k++) {
  13856. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13857. ret++; //[Lupus]
  13858. }
  13859. }
  13860. }
  13861. }
  13862. script_pushint(st,ret);
  13863. return SCRIPT_CMD_SUCCESS;
  13864. }
  13865. /*================================================
  13866. * Check whether another card has been
  13867. * equipped - used for 2/15's cards patch [celest]
  13868. * -- Items checked cannot be reused in another
  13869. * card set to prevent exploits
  13870. *------------------------------------------------*/
  13871. BUILDIN_FUNC(isequipped)
  13872. {
  13873. TBL_PC *sd;
  13874. int i, id = 1;
  13875. int ret = -1;
  13876. //Original hash to reverse it when full check fails.
  13877. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13878. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13879. script_pushint(st,0);
  13880. return SCRIPT_CMD_SUCCESS;
  13881. }
  13882. setitem_hash = sd->bonus.setitem_hash;
  13883. setitem_hash2 = sd->bonus.setitem_hash2;
  13884. for (i=0; id!=0; i++) {
  13885. int flag = 0;
  13886. short j;
  13887. FETCH (i+2, id) else id = 0;
  13888. if (id <= 0)
  13889. continue;
  13890. for (j=0; j<EQI_MAX; j++) {
  13891. short index = sd->equip_index[j];
  13892. if(index < 0)
  13893. continue;
  13894. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13895. continue;
  13896. if(!sd->inventory_data[index])
  13897. continue;
  13898. if (itemdb_type(id) != IT_CARD) {
  13899. if (sd->inventory_data[index]->nameid != id)
  13900. continue;
  13901. flag = 1;
  13902. break;
  13903. } else { //Cards
  13904. short k;
  13905. if (sd->inventory_data[index]->slots == 0 ||
  13906. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13907. continue;
  13908. for (k = 0; k < sd->inventory_data[index]->slots; k++)
  13909. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13910. unsigned int hash = 0;
  13911. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13912. continue;
  13913. hash = 1<<((j<5?j:j-5)*4 + k);
  13914. // check if card is already used by another set
  13915. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13916. continue;
  13917. // We have found a match
  13918. flag = 1;
  13919. // Set hash so this card cannot be used by another
  13920. if (j<5)
  13921. sd->bonus.setitem_hash |= hash;
  13922. else
  13923. sd->bonus.setitem_hash2 |= hash;
  13924. break;
  13925. }
  13926. }
  13927. if (flag) break; //Card found
  13928. }
  13929. if (ret == -1)
  13930. ret = flag;
  13931. else
  13932. ret &= flag;
  13933. if (!ret) break;
  13934. }
  13935. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13936. sd->bonus.setitem_hash = setitem_hash;
  13937. sd->bonus.setitem_hash2 = setitem_hash2;
  13938. }
  13939. script_pushint(st,ret);
  13940. return SCRIPT_CMD_SUCCESS;
  13941. }
  13942. /*================================================
  13943. * Check how many given inserted cards in the CURRENT
  13944. * weapon - used for 2/15's cards patch [Lupus]
  13945. *------------------------------------------------*/
  13946. BUILDIN_FUNC(cardscnt)
  13947. {
  13948. TBL_PC *sd;
  13949. int i, k, id = 1;
  13950. int ret = 0;
  13951. int index;
  13952. if( !script_rid2sd(sd) ){
  13953. script_pushint(st,0);
  13954. return SCRIPT_CMD_SUCCESS;
  13955. }
  13956. for (i=0; id!=0; i++) {
  13957. FETCH (i+2, id) else id = 0;
  13958. if (id <= 0)
  13959. continue;
  13960. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  13961. if(index < 0) continue;
  13962. if(!sd->inventory_data[index])
  13963. continue;
  13964. if(itemdb_type(id) != IT_CARD) {
  13965. if (sd->inventory_data[index]->nameid == id)
  13966. ret+= sd->inventory.u.items_inventory[index].amount;
  13967. } else {
  13968. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13969. continue;
  13970. for(k=0; k<sd->inventory_data[index]->slots; k++) {
  13971. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13972. ret++;
  13973. }
  13974. }
  13975. }
  13976. script_pushint(st,ret);
  13977. // script_pushint(st,current_equip_item_index);
  13978. return SCRIPT_CMD_SUCCESS;
  13979. }
  13980. /*=======================================================
  13981. * Returns the refined number of the current item, or an
  13982. * item with inventory index specified
  13983. *-------------------------------------------------------*/
  13984. BUILDIN_FUNC(getrefine)
  13985. {
  13986. TBL_PC *sd;
  13987. if (script_rid2sd(sd)){
  13988. if( current_equip_item_index == -1 ){
  13989. script_pushint(st, 0);
  13990. return SCRIPT_CMD_FAILURE;
  13991. }
  13992. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13993. }else
  13994. script_pushint(st,0);
  13995. return SCRIPT_CMD_SUCCESS;
  13996. }
  13997. /*=======================================================
  13998. * Day/Night controls
  13999. *-------------------------------------------------------*/
  14000. BUILDIN_FUNC(night)
  14001. {
  14002. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  14003. return SCRIPT_CMD_SUCCESS;
  14004. }
  14005. BUILDIN_FUNC(day)
  14006. {
  14007. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  14008. return SCRIPT_CMD_SUCCESS;
  14009. }
  14010. /**
  14011. * unequip <equipment slot>{,<char_id>};
  14012. * @author [Spectre]
  14013. **/
  14014. BUILDIN_FUNC(unequip) {
  14015. int pos;
  14016. TBL_PC *sd;
  14017. if (!script_charid2sd(3,sd))
  14018. return SCRIPT_CMD_FAILURE;
  14019. pos = script_getnum(st,2);
  14020. if (equip_index_check(pos)) {
  14021. short i = pc_checkequip(sd,equip_bitmask[pos]);
  14022. if (i >= 0) {
  14023. pc_unequipitem(sd,i,1|2);
  14024. script_pushint(st, 1);
  14025. return SCRIPT_CMD_SUCCESS;
  14026. }
  14027. }
  14028. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  14029. script_pushint(st, 0);
  14030. return SCRIPT_CMD_FAILURE;
  14031. }
  14032. /**
  14033. * equip <item id>{,<char_id>};
  14034. **/
  14035. BUILDIN_FUNC(equip) {
  14036. TBL_PC *sd;
  14037. struct item_data *item_data;
  14038. if (!script_charid2sd(3,sd))
  14039. return SCRIPT_CMD_FAILURE;
  14040. t_itemid nameid = script_getnum(st,2);
  14041. if ((item_data = itemdb_exists(nameid))) {
  14042. int i;
  14043. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  14044. if (i < MAX_INVENTORY) {
  14045. pc_equipitem(sd,i,item_data->equip);
  14046. script_pushint(st,1);
  14047. return SCRIPT_CMD_SUCCESS;
  14048. }
  14049. }
  14050. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  14051. script_pushint(st,0);
  14052. return SCRIPT_CMD_FAILURE;
  14053. }
  14054. BUILDIN_FUNC(autoequip)
  14055. {
  14056. int flag;
  14057. struct item_data *item_data;
  14058. t_itemid nameid=script_getnum(st,2);
  14059. flag=script_getnum(st,3);
  14060. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  14061. {
  14062. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  14063. return SCRIPT_CMD_FAILURE;
  14064. }
  14065. if( !itemdb_isequip2(item_data) )
  14066. {
  14067. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  14068. return SCRIPT_CMD_FAILURE;
  14069. }
  14070. item_data->flag.autoequip = flag>0?1:0;
  14071. return SCRIPT_CMD_SUCCESS;
  14072. }
  14073. /**
  14074. * Set the value of a given battle config
  14075. * setbattleflag "<battle flag>",<value>{,<reload>};
  14076. */
  14077. BUILDIN_FUNC(setbattleflag)
  14078. {
  14079. const char *flag, *value;
  14080. flag = script_getstr(st,2);
  14081. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  14082. if (battle_set_value(flag, value) == 0)
  14083. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  14084. else {
  14085. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  14086. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  14087. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  14088. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  14089. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  14090. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  14091. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  14092. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  14093. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  14094. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  14095. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  14096. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  14097. };
  14098. uint8 i;
  14099. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  14100. if (!strcmpi(flag, expdrop_flags[i])) {
  14101. mob_reload();
  14102. break;
  14103. }
  14104. }
  14105. }
  14106. }
  14107. return SCRIPT_CMD_SUCCESS;
  14108. }
  14109. /**
  14110. * Get the value of a given battle config
  14111. * getbattleflag("<battle flag>")
  14112. */
  14113. BUILDIN_FUNC(getbattleflag)
  14114. {
  14115. const char *flag = script_getstr(st,2);
  14116. script_pushint(st,battle_get_value(flag));
  14117. return SCRIPT_CMD_SUCCESS;
  14118. }
  14119. //=======================================================
  14120. // strlen [Valaris]
  14121. //-------------------------------------------------------
  14122. BUILDIN_FUNC(getstrlen)
  14123. {
  14124. const char *str = script_getstr(st,2);
  14125. int len = (str) ? (int)strlen(str) : 0;
  14126. script_pushint(st,len);
  14127. return SCRIPT_CMD_SUCCESS;
  14128. }
  14129. //=======================================================
  14130. // isalpha [Valaris]
  14131. //-------------------------------------------------------
  14132. BUILDIN_FUNC(charisalpha)
  14133. {
  14134. const char *str=script_getstr(st,2);
  14135. int pos=script_getnum(st,3);
  14136. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  14137. script_pushint(st,val);
  14138. return SCRIPT_CMD_SUCCESS;
  14139. }
  14140. //=======================================================
  14141. // charisupper <str>, <index>
  14142. //-------------------------------------------------------
  14143. BUILDIN_FUNC(charisupper)
  14144. {
  14145. const char *str = script_getstr(st,2);
  14146. int pos = script_getnum(st,3);
  14147. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  14148. script_pushint(st,val);
  14149. return SCRIPT_CMD_SUCCESS;
  14150. }
  14151. //=======================================================
  14152. // charislower <str>, <index>
  14153. //-------------------------------------------------------
  14154. BUILDIN_FUNC(charislower)
  14155. {
  14156. const char *str = script_getstr(st,2);
  14157. int pos = script_getnum(st,3);
  14158. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  14159. script_pushint(st,val);
  14160. return SCRIPT_CMD_SUCCESS;
  14161. }
  14162. //=======================================================
  14163. // charat <str>, <index>
  14164. //-------------------------------------------------------
  14165. BUILDIN_FUNC(charat) {
  14166. const char *str = script_getstr(st,2);
  14167. int pos = script_getnum(st,3);
  14168. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  14169. char output[2];
  14170. output[0] = str[pos];
  14171. output[1] = '\0';
  14172. script_pushstrcopy(st, output);
  14173. } else
  14174. script_pushconststr(st, "");
  14175. return SCRIPT_CMD_SUCCESS;
  14176. }
  14177. //=======================================================
  14178. // setchar <string>, <char>, <index>
  14179. //-------------------------------------------------------
  14180. BUILDIN_FUNC(setchar)
  14181. {
  14182. const char *str = script_getstr(st,2);
  14183. const char *c = script_getstr(st,3);
  14184. int index = script_getnum(st,4);
  14185. char *output = aStrdup(str);
  14186. if(index >= 0 && index < strlen(output))
  14187. output[index] = *c;
  14188. script_pushstr(st, output);
  14189. return SCRIPT_CMD_SUCCESS;
  14190. }
  14191. //=======================================================
  14192. // insertchar <string>, <char>, <index>
  14193. //-------------------------------------------------------
  14194. BUILDIN_FUNC(insertchar)
  14195. {
  14196. const char *str = script_getstr(st,2);
  14197. const char *c = script_getstr(st,3);
  14198. int index = script_getnum(st,4);
  14199. char *output;
  14200. size_t len = strlen(str);
  14201. if(index < 0)
  14202. index = 0;
  14203. else if(index > len)
  14204. index = len;
  14205. output = (char*)aMalloc(len + 2);
  14206. memcpy(output, str, index);
  14207. output[index] = c[0];
  14208. memcpy(&output[index+1], &str[index], len - index);
  14209. output[len+1] = '\0';
  14210. script_pushstr(st, output);
  14211. return SCRIPT_CMD_SUCCESS;
  14212. }
  14213. //=======================================================
  14214. // delchar <string>, <index>
  14215. //-------------------------------------------------------
  14216. BUILDIN_FUNC(delchar)
  14217. {
  14218. const char *str = script_getstr(st,2);
  14219. int index = script_getnum(st,3);
  14220. char *output;
  14221. size_t len = strlen(str);
  14222. if(index < 0 || index > len) {
  14223. //return original
  14224. output = aStrdup(str);
  14225. script_pushstr(st, output);
  14226. return SCRIPT_CMD_SUCCESS;
  14227. }
  14228. output = (char*)aMalloc(len);
  14229. memcpy(output, str, index);
  14230. memcpy(&output[index], &str[index+1], len - index);
  14231. script_pushstr(st, output);
  14232. return SCRIPT_CMD_SUCCESS;
  14233. }
  14234. //=======================================================
  14235. // strtoupper <str>
  14236. //-------------------------------------------------------
  14237. BUILDIN_FUNC(strtoupper)
  14238. {
  14239. const char *str = script_getstr(st,2);
  14240. char *output = aStrdup(str);
  14241. char *cursor = output;
  14242. while (*cursor != '\0') {
  14243. *cursor = TOUPPER(*cursor);
  14244. cursor++;
  14245. }
  14246. script_pushstr(st, output);
  14247. return SCRIPT_CMD_SUCCESS;
  14248. }
  14249. //=======================================================
  14250. // strtolower <str>
  14251. //-------------------------------------------------------
  14252. BUILDIN_FUNC(strtolower)
  14253. {
  14254. const char *str = script_getstr(st,2);
  14255. char *output = aStrdup(str);
  14256. char *cursor = output;
  14257. while (*cursor != '\0') {
  14258. *cursor = TOLOWER(*cursor);
  14259. cursor++;
  14260. }
  14261. script_pushstr(st, output);
  14262. return SCRIPT_CMD_SUCCESS;
  14263. }
  14264. //=======================================================
  14265. // substr <str>, <start>, <end>
  14266. //-------------------------------------------------------
  14267. BUILDIN_FUNC(substr)
  14268. {
  14269. const char *str = script_getstr(st,2);
  14270. char *output;
  14271. int start = script_getnum(st,3);
  14272. int end = script_getnum(st,4);
  14273. int len = 0;
  14274. if(start >= 0 && end < strlen(str) && start <= end) {
  14275. len = end - start + 1;
  14276. output = (char*)aMalloc(len + 1);
  14277. memcpy(output, &str[start], len);
  14278. } else
  14279. output = (char*)aMalloc(1);
  14280. output[len] = '\0';
  14281. script_pushstr(st, output);
  14282. return SCRIPT_CMD_SUCCESS;
  14283. }
  14284. //=======================================================
  14285. // explode <dest_string_array>, <str>, <delimiter>
  14286. // Note: delimiter is limited to 1 char
  14287. //-------------------------------------------------------
  14288. BUILDIN_FUNC(explode)
  14289. {
  14290. struct script_data* data = script_getdata(st, 2);
  14291. const char *str = script_getstr(st,3);
  14292. const char delimiter = script_getstr(st, 4)[0];
  14293. int32 id;
  14294. size_t len = strlen(str);
  14295. int i = 0, j = 0;
  14296. int start;
  14297. char *temp;
  14298. const char* name;
  14299. TBL_PC* sd = NULL;
  14300. temp = (char*)aMalloc(len + 1);
  14301. if( !data_isreference(data) ) {
  14302. ShowError("script:explode: not a variable\n");
  14303. script_reportdata(data);
  14304. st->state = END;
  14305. return SCRIPT_CMD_FAILURE;// not a variable
  14306. }
  14307. id = reference_getid(data);
  14308. start = reference_getindex(data);
  14309. name = reference_getname(data);
  14310. if( !is_string_variable(name) ) {
  14311. ShowError("script:explode: not string array\n");
  14312. script_reportdata(data);
  14313. st->state = END;
  14314. return SCRIPT_CMD_FAILURE;// data type mismatch
  14315. }
  14316. if( not_server_variable(*name) ) {
  14317. if( !script_rid2sd(sd) )
  14318. return SCRIPT_CMD_SUCCESS;// no player attached
  14319. }
  14320. while(str[i] != '\0') {
  14321. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14322. temp[j] = '\0';
  14323. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14324. j = 0;
  14325. ++i;
  14326. } else {
  14327. temp[j++] = str[i++];
  14328. }
  14329. }
  14330. //set last string
  14331. temp[j] = '\0';
  14332. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14333. aFree(temp);
  14334. return SCRIPT_CMD_SUCCESS;
  14335. }
  14336. //=======================================================
  14337. // implode <string_array>
  14338. // implode <string_array>, <glue>
  14339. //-------------------------------------------------------
  14340. BUILDIN_FUNC(implode)
  14341. {
  14342. struct script_data* data = script_getdata(st, 2);
  14343. const char *name;
  14344. uint32 glue_len = 0, array_size, id;
  14345. char *output;
  14346. TBL_PC* sd = NULL;
  14347. if( !data_isreference(data) ) {
  14348. ShowError("script:implode: not a variable\n");
  14349. script_reportdata(data);
  14350. st->state = END;
  14351. return SCRIPT_CMD_FAILURE;// not a variable
  14352. }
  14353. id = reference_getid(data);
  14354. name = reference_getname(data);
  14355. if( !is_string_variable(name) ) {
  14356. ShowError("script:implode: not string array\n");
  14357. script_reportdata(data);
  14358. st->state = END;
  14359. return SCRIPT_CMD_FAILURE;// data type mismatch
  14360. }
  14361. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14362. return SCRIPT_CMD_SUCCESS;// no player attached
  14363. }
  14364. //count chars
  14365. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  14366. if(array_size == -1) { //empty array check (AmsTaff)
  14367. ShowWarning("script:implode: array length = 0\n");
  14368. output = (char*)aMalloc(sizeof(char)*5);
  14369. sprintf(output,"%s","NULL");
  14370. } else {
  14371. const char *glue = NULL, *temp;
  14372. size_t len = 0;
  14373. int i, k = 0;
  14374. for(i = 0; i <= array_size; ++i) {
  14375. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14376. len += strlen(temp);
  14377. // Remove stack entry from get_val2_str
  14378. script_removetop( st, -1, 0 );
  14379. }
  14380. //allocate mem
  14381. if( script_hasdata(st,3) ) {
  14382. glue = script_getstr(st,3);
  14383. glue_len = strlen(glue);
  14384. len += glue_len * (array_size);
  14385. }
  14386. output = (char*)aMalloc(len + 1);
  14387. //build output
  14388. for(i = 0; i < array_size; ++i) {
  14389. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14390. len = strlen(temp);
  14391. memcpy(&output[k], temp, len);
  14392. k += len;
  14393. // Remove stack entry from get_val2_str
  14394. script_removetop( st, -1, 0 );
  14395. if(glue_len != 0) {
  14396. memcpy(&output[k], glue, glue_len);
  14397. k += glue_len;
  14398. }
  14399. }
  14400. temp = get_val2_str( st, reference_uid( id, array_size ), reference_getref( data ) );
  14401. len = strlen(temp);
  14402. memcpy(&output[k], temp, len);
  14403. k += len;
  14404. output[k] = '\0';
  14405. // Remove stack entry from get_val2_str
  14406. script_removetop( st, -1, 0 );
  14407. }
  14408. script_pushstr(st, output);
  14409. return SCRIPT_CMD_SUCCESS;
  14410. }
  14411. //=======================================================
  14412. // sprintf(<format>, ...);
  14413. // Implements C sprintf, except format %n. The resulting string is
  14414. // returned, instead of being saved in variable by reference.
  14415. //-------------------------------------------------------
  14416. BUILDIN_FUNC(sprintf)
  14417. {
  14418. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14419. const char* format;
  14420. char* p;
  14421. char* q;
  14422. char* buf = NULL;
  14423. char* buf2 = NULL;
  14424. struct script_data* data;
  14425. StringBuf final_buf;
  14426. // Fetch init data
  14427. format = script_getstr(st, 2);
  14428. argc = script_lastdata(st)-2;
  14429. len = strlen(format);
  14430. // Skip parsing, where no parsing is required.
  14431. if(len == 0) {
  14432. script_pushconststr(st,"");
  14433. return SCRIPT_CMD_SUCCESS;
  14434. }
  14435. // Pessimistic alloc
  14436. CREATE(buf, char, len+1);
  14437. // Need not be parsed, just solve stuff like %%.
  14438. if(argc == 0) {
  14439. memcpy(buf,format,len+1);
  14440. script_pushstrcopy(st, buf);
  14441. aFree(buf);
  14442. return SCRIPT_CMD_SUCCESS;
  14443. }
  14444. safestrncpy(buf, format, len+1);
  14445. // Issue sprintf for each parameter
  14446. StringBuf_Init(&final_buf);
  14447. q = buf;
  14448. while((p = strchr(q, '%')) != NULL) {
  14449. if(p != q) {
  14450. len = p - q + 1;
  14451. if(buf2_len < len) {
  14452. RECREATE(buf2, char, len);
  14453. buf2_len = len;
  14454. }
  14455. safestrncpy(buf2, q, len);
  14456. StringBuf_AppendStr(&final_buf, buf2);
  14457. q = p;
  14458. }
  14459. p = q + 1;
  14460. if(*p == '%') { // %%
  14461. StringBuf_AppendStr(&final_buf, "%");
  14462. q+=2;
  14463. continue;
  14464. }
  14465. if(*p == 'n') { // %n
  14466. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14467. script_reportsrc(st);
  14468. q+=2;
  14469. continue;
  14470. }
  14471. if(arg >= argc) {
  14472. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14473. if(buf) aFree(buf);
  14474. if(buf2) aFree(buf2);
  14475. StringBuf_Destroy(&final_buf);
  14476. script_pushconststr(st,"");
  14477. return SCRIPT_CMD_FAILURE;
  14478. }
  14479. if((p = strchr(q+1, '%')) == NULL)
  14480. p = strchr(q, 0); // EOS
  14481. len = p - q + 1;
  14482. if(buf2_len < len) {
  14483. RECREATE(buf2, char, len);
  14484. buf2_len = len;
  14485. }
  14486. safestrncpy(buf2, q, len);
  14487. q = p;
  14488. // Note: This assumes the passed value being the correct
  14489. // type to the current format specifier. If not, the server
  14490. // probably crashes or returns anything else, than expected,
  14491. // but it would behave in normal code the same way so it's
  14492. // the scripter's responsibility.
  14493. data = script_getdata(st, arg+3);
  14494. if(data_isstring(data)) // String
  14495. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14496. else if(data_isint(data)) // Number
  14497. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14498. else if(data_isreference(data)) { // Variable
  14499. char* name = reference_getname(data);
  14500. if(name[strlen(name)-1]=='$') // var Str
  14501. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14502. else // var Int
  14503. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14504. } else { // Unsupported type
  14505. ShowError("buildin_sprintf: Unknown argument type!\n");
  14506. if(buf) aFree(buf);
  14507. if(buf2) aFree(buf2);
  14508. StringBuf_Destroy(&final_buf);
  14509. script_pushconststr(st,"");
  14510. return SCRIPT_CMD_FAILURE;
  14511. }
  14512. arg++;
  14513. }
  14514. // Append anything left
  14515. if(*q)
  14516. StringBuf_AppendStr(&final_buf, q);
  14517. // Passed more, than needed
  14518. if(arg < argc) {
  14519. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14520. script_reportsrc(st);
  14521. }
  14522. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14523. if(buf) aFree(buf);
  14524. if(buf2) aFree(buf2);
  14525. StringBuf_Destroy(&final_buf);
  14526. return SCRIPT_CMD_SUCCESS;
  14527. }
  14528. //=======================================================
  14529. // sscanf(<str>, <format>, ...);
  14530. // Implements C sscanf.
  14531. //-------------------------------------------------------
  14532. BUILDIN_FUNC(sscanf){
  14533. unsigned int argc, arg = 0, len;
  14534. struct script_data* data;
  14535. struct map_session_data* sd = NULL;
  14536. const char* str;
  14537. const char* format;
  14538. const char* p;
  14539. const char* q;
  14540. char* buf = NULL;
  14541. char* buf_p;
  14542. char* ref_str = NULL;
  14543. int ref_int;
  14544. // Get data
  14545. str = script_getstr(st, 2);
  14546. format = script_getstr(st, 3);
  14547. argc = script_lastdata(st)-3;
  14548. len = strlen(format);
  14549. if (len != 0 && strlen(str) == 0) {
  14550. // If the source string is empty but the format string is not, we return -1
  14551. // according to the C specs. (if the format string is also empty, we shall
  14552. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14553. script_pushint(st, -1);
  14554. return SCRIPT_CMD_SUCCESS;
  14555. }
  14556. CREATE(buf, char, len*2+1);
  14557. // Issue sscanf for each parameter
  14558. *buf = 0;
  14559. q = format;
  14560. while((p = strchr(q, '%'))){
  14561. if(p!=q){
  14562. strncat(buf, q, (size_t)(p-q));
  14563. q = p;
  14564. }
  14565. p = q+1;
  14566. if(*p=='*' || *p=='%'){ // Skip
  14567. strncat(buf, q, 2);
  14568. q+=2;
  14569. continue;
  14570. }
  14571. if(arg>=argc){
  14572. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14573. script_pushint(st, -1);
  14574. if(buf) aFree(buf);
  14575. if(ref_str) aFree(ref_str);
  14576. return SCRIPT_CMD_FAILURE;
  14577. }
  14578. if((p = strchr(q+1, '%'))==NULL){
  14579. p = strchr(q, 0); // EOS
  14580. }
  14581. len = p-q;
  14582. strncat(buf, q, len);
  14583. q = p;
  14584. // Validate output
  14585. data = script_getdata(st, arg+4);
  14586. if(!data_isreference(data) || !reference_tovariable(data)){
  14587. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14588. script_pushint(st, -1);
  14589. if(buf) aFree(buf);
  14590. if(ref_str) aFree(ref_str);
  14591. return SCRIPT_CMD_FAILURE;
  14592. }
  14593. buf_p = reference_getname(data);
  14594. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14595. script_pushint(st, -1);
  14596. if(buf) aFree(buf);
  14597. if(ref_str) aFree(ref_str);
  14598. return SCRIPT_CMD_SUCCESS;
  14599. }
  14600. // Save value if any
  14601. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14602. if(ref_str==NULL){
  14603. CREATE(ref_str, char, strlen(str)+1);
  14604. }
  14605. if(sscanf(str, buf, ref_str)==0){
  14606. break;
  14607. }
  14608. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14609. } else { // Number
  14610. if(sscanf(str, buf, &ref_int)==0){
  14611. break;
  14612. }
  14613. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14614. }
  14615. arg++;
  14616. // Disable used format (%... -> %*...)
  14617. buf_p = strchr(buf, 0);
  14618. memmove(buf_p-len+2, buf_p-len+1, len);
  14619. *(buf_p-len+1) = '*';
  14620. }
  14621. script_pushint(st, arg);
  14622. if(buf) aFree(buf);
  14623. if(ref_str) aFree(ref_str);
  14624. return SCRIPT_CMD_SUCCESS;
  14625. }
  14626. //=======================================================
  14627. // strpos(<haystack>, <needle>)
  14628. // strpos(<haystack>, <needle>, <offset>)
  14629. //
  14630. // Implements PHP style strpos. Adapted from code from
  14631. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14632. //-------------------------------------------------------
  14633. BUILDIN_FUNC(strpos) {
  14634. const char *haystack = script_getstr(st,2);
  14635. const char *needle = script_getstr(st,3);
  14636. int i;
  14637. size_t len;
  14638. if( script_hasdata(st,4) )
  14639. i = script_getnum(st,4);
  14640. else
  14641. i = 0;
  14642. if (needle[0] == '\0') {
  14643. script_pushint(st, -1);
  14644. return SCRIPT_CMD_SUCCESS;
  14645. }
  14646. len = strlen(haystack);
  14647. for ( ; i < len; ++i ) {
  14648. if ( haystack[i] == *needle ) {
  14649. // matched starting char -- loop through remaining chars
  14650. const char *h, *n;
  14651. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14652. if ( *h != *n ) {
  14653. break;
  14654. }
  14655. }
  14656. if ( !*n ) { // matched all of 'needle' to null termination
  14657. script_pushint(st, i);
  14658. return SCRIPT_CMD_SUCCESS;
  14659. }
  14660. }
  14661. }
  14662. script_pushint(st, -1);
  14663. return SCRIPT_CMD_SUCCESS;
  14664. }
  14665. //===============================================================
  14666. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14667. //
  14668. // Note: Finds all instances of <search> in <input> and replaces
  14669. // with <replace>. If specified will only replace as many
  14670. // instances as specified in <count>. By default will be case
  14671. // sensitive.
  14672. //---------------------------------------------------------------
  14673. BUILDIN_FUNC(replacestr)
  14674. {
  14675. const char *input = script_getstr(st, 2);
  14676. const char *find = script_getstr(st, 3);
  14677. const char *replace = script_getstr(st, 4);
  14678. size_t inputlen = strlen(input);
  14679. size_t findlen = strlen(find);
  14680. struct StringBuf output;
  14681. bool usecase = true;
  14682. int count = 0;
  14683. int numFinds = 0;
  14684. int i = 0, f = 0;
  14685. if(findlen == 0) {
  14686. ShowError("script:replacestr: Invalid search length.\n");
  14687. st->state = END;
  14688. return SCRIPT_CMD_FAILURE;
  14689. }
  14690. if(script_hasdata(st, 5)) {
  14691. if( script_isint(st, 5) )
  14692. usecase = script_getnum(st, 5) != 0;
  14693. else {
  14694. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14695. st->state = END;
  14696. return SCRIPT_CMD_FAILURE;
  14697. }
  14698. }
  14699. if(script_hasdata(st, 6)) {
  14700. count = script_getnum(st, 6);
  14701. if(count == 0) {
  14702. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14703. st->state = END;
  14704. return SCRIPT_CMD_FAILURE;
  14705. }
  14706. }
  14707. StringBuf_Init(&output);
  14708. for(; i < inputlen; i++) {
  14709. if(count && count == numFinds) { //found enough, stop looking
  14710. break;
  14711. }
  14712. for(f = 0; f <= findlen; f++) {
  14713. if(f == findlen) { //complete match
  14714. numFinds++;
  14715. StringBuf_AppendStr(&output, replace);
  14716. i += findlen - 1;
  14717. break;
  14718. } else {
  14719. if(usecase) {
  14720. if((i + f) > inputlen || input[i + f] != find[f]) {
  14721. StringBuf_Printf(&output, "%c", input[i]);
  14722. break;
  14723. }
  14724. } else {
  14725. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14726. StringBuf_Printf(&output, "%c", input[i]);
  14727. break;
  14728. }
  14729. }
  14730. }
  14731. }
  14732. }
  14733. //append excess after enough found
  14734. if(i < inputlen)
  14735. StringBuf_AppendStr(&output, &(input[i]));
  14736. script_pushstrcopy(st, StringBuf_Value(&output));
  14737. StringBuf_Destroy(&output);
  14738. return SCRIPT_CMD_SUCCESS;
  14739. }
  14740. //========================================================
  14741. // countstr <input>, <search>{, <usecase>}
  14742. //
  14743. // Note: Counts the number of times <search> occurs in
  14744. // <input>. By default will be case sensitive.
  14745. //--------------------------------------------------------
  14746. BUILDIN_FUNC(countstr)
  14747. {
  14748. const char *input = script_getstr(st, 2);
  14749. const char *find = script_getstr(st, 3);
  14750. size_t inputlen = strlen(input);
  14751. size_t findlen = strlen(find);
  14752. bool usecase = true;
  14753. int numFinds = 0;
  14754. int i = 0, f = 0;
  14755. if(findlen == 0) {
  14756. ShowError("script:countstr: Invalid search length.\n");
  14757. st->state = END;
  14758. return SCRIPT_CMD_FAILURE;
  14759. }
  14760. if(script_hasdata(st, 4)) {
  14761. if( script_isint(st, 4) )
  14762. usecase = script_getnum(st, 4) != 0;
  14763. else {
  14764. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14765. st->state = END;
  14766. return SCRIPT_CMD_FAILURE;
  14767. }
  14768. }
  14769. for(; i < inputlen; i++) {
  14770. for(f = 0; f <= findlen; f++) {
  14771. if(f == findlen) { //complete match
  14772. numFinds++;
  14773. i += findlen - 1;
  14774. break;
  14775. } else {
  14776. if(usecase) {
  14777. if((i + f) > inputlen || input[i + f] != find[f]) {
  14778. break;
  14779. }
  14780. } else {
  14781. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14782. break;
  14783. }
  14784. }
  14785. }
  14786. }
  14787. }
  14788. script_pushint(st, numFinds);
  14789. return SCRIPT_CMD_SUCCESS;
  14790. }
  14791. /// Changes the display name and/or display class of the npc.
  14792. /// Returns 0 is successful, 1 if the npc does not exist.
  14793. ///
  14794. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14795. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14796. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14797. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14798. BUILDIN_FUNC(setnpcdisplay)
  14799. {
  14800. const char* name;
  14801. const char* newname = NULL;
  14802. int class_ = JT_FAKENPC, size = -1;
  14803. struct npc_data* nd;
  14804. name = script_getstr(st,2);
  14805. if( script_hasdata(st,4) )
  14806. class_ = script_getnum(st,4);
  14807. if( script_hasdata(st,5) )
  14808. size = script_getnum(st,5);
  14809. if( script_isstring(st, 3) )
  14810. newname = script_getstr(st, 3);
  14811. else
  14812. class_ = script_getnum(st, 3);
  14813. nd = npc_name2id(name);
  14814. if( nd == NULL )
  14815. {// not found
  14816. script_pushint(st,1);
  14817. return SCRIPT_CMD_SUCCESS;
  14818. }
  14819. // update npc
  14820. if( newname )
  14821. npc_setdisplayname(nd, newname);
  14822. if( size != -1 && size != (int)nd->size )
  14823. nd->size = size;
  14824. else
  14825. size = -1;
  14826. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  14827. npc_setclass(nd, class_);
  14828. else if( size != -1 )
  14829. unit_refresh(&nd->bl); // Required to update the visual size
  14830. script_pushint(st,0);
  14831. return SCRIPT_CMD_SUCCESS;
  14832. }
  14833. BUILDIN_FUNC(atoi)
  14834. {
  14835. const char *value;
  14836. value = script_getstr(st,2);
  14837. script_pushint(st,atoi(value));
  14838. return SCRIPT_CMD_SUCCESS;
  14839. }
  14840. BUILDIN_FUNC(axtoi)
  14841. {
  14842. const char *hex = script_getstr(st,2);
  14843. long value = strtol(hex, NULL, 16);
  14844. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14845. value = cap_value(value, INT_MIN, INT_MAX);
  14846. #endif
  14847. script_pushint(st, (int)value);
  14848. return SCRIPT_CMD_SUCCESS;
  14849. }
  14850. BUILDIN_FUNC(strtol)
  14851. {
  14852. const char *string = script_getstr(st, 2);
  14853. int base = script_getnum(st, 3);
  14854. long value = strtol(string, NULL, base);
  14855. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14856. value = cap_value(value, INT_MIN, INT_MAX);
  14857. #endif
  14858. script_pushint(st, (int)value);
  14859. return SCRIPT_CMD_SUCCESS;
  14860. }
  14861. // case-insensitive substring search [lordalfa]
  14862. BUILDIN_FUNC(compare)
  14863. {
  14864. const char *message;
  14865. const char *cmpstring;
  14866. message = script_getstr(st,2);
  14867. cmpstring = script_getstr(st,3);
  14868. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14869. return SCRIPT_CMD_SUCCESS;
  14870. }
  14871. BUILDIN_FUNC(strcmp)
  14872. {
  14873. const char *str1;
  14874. const char *str2;
  14875. str1 = script_getstr(st,2);
  14876. str2 = script_getstr(st,3);
  14877. script_pushint(st,strcmp(str1, str2));
  14878. return SCRIPT_CMD_SUCCESS;
  14879. }
  14880. // [zBuffer] List of mathematics commands --->
  14881. BUILDIN_FUNC(sqrt)
  14882. {
  14883. double i, a;
  14884. i = script_getnum(st,2);
  14885. a = sqrt(i);
  14886. script_pushint(st,(int)a);
  14887. return SCRIPT_CMD_SUCCESS;
  14888. }
  14889. BUILDIN_FUNC(pow)
  14890. {
  14891. double i, a, b;
  14892. a = script_getnum(st,2);
  14893. b = script_getnum(st,3);
  14894. i = pow(a,b);
  14895. script_pushint(st,(int)i);
  14896. return SCRIPT_CMD_SUCCESS;
  14897. }
  14898. BUILDIN_FUNC(distance)
  14899. {
  14900. int x0, y0, x1, y1;
  14901. x0 = script_getnum(st,2);
  14902. y0 = script_getnum(st,3);
  14903. x1 = script_getnum(st,4);
  14904. y1 = script_getnum(st,5);
  14905. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14906. return SCRIPT_CMD_SUCCESS;
  14907. }
  14908. // <--- [zBuffer] List of mathematics commands
  14909. BUILDIN_FUNC(md5)
  14910. {
  14911. const char *tmpstr;
  14912. char *md5str;
  14913. tmpstr = script_getstr(st,2);
  14914. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14915. MD5_String(tmpstr, md5str);
  14916. script_pushstr(st, md5str);
  14917. return SCRIPT_CMD_SUCCESS;
  14918. }
  14919. // [zBuffer] List of dynamic var commands --->
  14920. /**
  14921. * setd "<variable name>",<value>{,<char_id>};
  14922. **/
  14923. BUILDIN_FUNC(setd)
  14924. {
  14925. TBL_PC *sd = NULL;
  14926. char varname[100];
  14927. const char *buffer;
  14928. int elem;
  14929. buffer = script_getstr(st, 2);
  14930. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14931. elem = 0;
  14932. if( not_server_variable(*varname) ) {
  14933. if (!script_charid2sd(4,sd))
  14934. return SCRIPT_CMD_FAILURE;
  14935. }
  14936. if( is_string_variable(varname) ) {
  14937. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), NULL );
  14938. } else {
  14939. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), NULL );
  14940. }
  14941. return SCRIPT_CMD_SUCCESS;
  14942. }
  14943. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14944. {
  14945. int i, j;
  14946. TBL_PC* sd = NULL;
  14947. const char* query;
  14948. struct script_data* data;
  14949. const char* name;
  14950. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14951. int num_vars;
  14952. int num_cols;
  14953. // check target variables
  14954. for( i = 3; script_hasdata(st,i); ++i ) {
  14955. data = script_getdata(st, i);
  14956. if( data_isreference(data) ) { // it's a variable
  14957. name = reference_getname(data);
  14958. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14959. if( !script_rid2sd(sd) ) { // no player attached
  14960. script_reportdata(data);
  14961. st->state = END;
  14962. return SCRIPT_CMD_FAILURE;
  14963. }
  14964. }
  14965. } else {
  14966. ShowError("script:query_sql: not a variable\n");
  14967. script_reportdata(data);
  14968. st->state = END;
  14969. return SCRIPT_CMD_FAILURE;
  14970. }
  14971. }
  14972. num_vars = i - 3;
  14973. // Execute the query
  14974. query = script_getstr(st,2);
  14975. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14976. Sql_ShowDebug(handle);
  14977. script_pushint(st, -1);
  14978. return SCRIPT_CMD_FAILURE;
  14979. }
  14980. if( Sql_NumRows(handle) == 0 ) { // No data received
  14981. Sql_FreeResult(handle);
  14982. script_pushint(st, 0);
  14983. return SCRIPT_CMD_SUCCESS;
  14984. }
  14985. // Count the number of columns to store
  14986. num_cols = Sql_NumColumns(handle);
  14987. if( num_vars < num_cols ) {
  14988. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14989. script_reportsrc(st);
  14990. } else if( num_vars > num_cols ) {
  14991. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14992. script_reportsrc(st);
  14993. }
  14994. // Store data
  14995. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14996. for( j = 0; j < num_vars; ++j ) {
  14997. char* str = NULL;
  14998. if( j < num_cols )
  14999. Sql_GetData(handle, j, &str, NULL);
  15000. data = script_getdata(st, j+3);
  15001. name = reference_getname(data);
  15002. if( is_string_variable(name) )
  15003. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  15004. else
  15005. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  15006. }
  15007. }
  15008. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  15009. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  15010. script_reportsrc(st);
  15011. }
  15012. // Free data
  15013. Sql_FreeResult(handle);
  15014. script_pushint(st, i);
  15015. return SCRIPT_CMD_SUCCESS;
  15016. }
  15017. BUILDIN_FUNC(query_sql) {
  15018. return buildin_query_sql_sub(st, qsmysql_handle);
  15019. }
  15020. BUILDIN_FUNC(query_logsql) {
  15021. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  15022. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  15023. script_pushint(st,-1);
  15024. return SCRIPT_CMD_FAILURE;
  15025. }
  15026. return buildin_query_sql_sub(st, logmysql_handle);
  15027. }
  15028. //Allows escaping of a given string.
  15029. BUILDIN_FUNC(escape_sql)
  15030. {
  15031. const char *str;
  15032. char *esc_str;
  15033. size_t len;
  15034. str = script_getstr(st,2);
  15035. len = strlen(str);
  15036. esc_str = (char*)aMalloc(len*2+1);
  15037. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  15038. script_pushstr(st, esc_str);
  15039. return SCRIPT_CMD_SUCCESS;
  15040. }
  15041. BUILDIN_FUNC(getd)
  15042. {
  15043. char varname[100];
  15044. const char *buffer;
  15045. int elem;
  15046. buffer = script_getstr(st, 2);
  15047. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  15048. elem = 0;
  15049. // Push the 'pointer' so it's more flexible [Lance]
  15050. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  15051. return SCRIPT_CMD_SUCCESS;
  15052. }
  15053. BUILDIN_FUNC(callshop)
  15054. {
  15055. TBL_PC *sd = NULL;
  15056. struct npc_data *nd;
  15057. const char *shopname;
  15058. int flag = 0;
  15059. if (!script_rid2sd(sd)) {
  15060. script_pushint(st,0);
  15061. return SCRIPT_CMD_SUCCESS;
  15062. }
  15063. shopname = script_getstr(st, 2);
  15064. if (script_hasdata(st,3))
  15065. flag = script_getnum(st,3);
  15066. nd = npc_name2id(shopname);
  15067. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP && nd->subtype != NPCTYPE_BARTER) ) {
  15068. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  15069. script_pushint(st,0);
  15070. return SCRIPT_CMD_FAILURE;
  15071. }
  15072. if (nd->subtype == NPCTYPE_SHOP) {
  15073. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15074. sd->state.callshop = 1;
  15075. switch (flag) {
  15076. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  15077. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  15078. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  15079. }
  15080. }
  15081. #if PACKETVER >= 20131223
  15082. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15083. unsigned short i;
  15084. for (i = 0; i < nd->u.shop.count; i++) {
  15085. if (nd->u.shop.shop_item[i].qty)
  15086. break;
  15087. }
  15088. if (i == nd->u.shop.count) {
  15089. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  15090. return SCRIPT_CMD_FAILURE;
  15091. }
  15092. sd->npc_shopid = nd->bl.id;
  15093. clif_npc_market_open(sd, nd);
  15094. script_pushint(st,1);
  15095. return SCRIPT_CMD_SUCCESS;
  15096. }
  15097. #endif
  15098. else if( nd->subtype == NPCTYPE_BARTER ){
  15099. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  15100. sd->state.callshop = 1;
  15101. if( nd->u.barter.extended ){
  15102. clif_barter_extended_open( *sd, *nd );
  15103. }else{
  15104. clif_barter_open( *sd, *nd );
  15105. }
  15106. }else
  15107. clif_cashshop_show(sd, nd);
  15108. sd->npc_shopid = nd->bl.id;
  15109. script_pushint(st,1);
  15110. return SCRIPT_CMD_SUCCESS;
  15111. }
  15112. BUILDIN_FUNC(npcshopitem)
  15113. {
  15114. const char* npcname = script_getstr(st, 2);
  15115. struct npc_data* nd = npc_name2id(npcname);
  15116. int n, i;
  15117. int amount;
  15118. uint16 offs = 2;
  15119. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  15120. script_pushint(st,0);
  15121. return SCRIPT_CMD_SUCCESS;
  15122. }
  15123. #if PACKETVER >= 20131223
  15124. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15125. offs = 3;
  15126. npc_market_delfromsql_(nd->exname, 0, true);
  15127. }
  15128. #endif
  15129. // get the count of new entries
  15130. amount = (script_lastdata(st)-2)/offs;
  15131. // generate new shop item list
  15132. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  15133. nd->u.shop.count = 0;
  15134. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  15135. t_itemid nameid = script_getnum( st, i );
  15136. if( itemdb_exists( nameid ) == nullptr ){
  15137. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  15138. script_pushint( st, 0 );
  15139. return SCRIPT_CMD_FAILURE;
  15140. }
  15141. nd->u.shop.shop_item[n].nameid = nameid;
  15142. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  15143. #if PACKETVER >= 20131223
  15144. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15145. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  15146. nd->u.shop.shop_item[n].flag = 1;
  15147. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  15148. }
  15149. #endif
  15150. nd->u.shop.count++;
  15151. }
  15152. script_pushint(st,1);
  15153. return SCRIPT_CMD_SUCCESS;
  15154. }
  15155. BUILDIN_FUNC(npcshopadditem)
  15156. {
  15157. const char* npcname = script_getstr(st,2);
  15158. struct npc_data* nd = npc_name2id(npcname);
  15159. uint16 offs = 2, amount;
  15160. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15161. script_pushint(st,0);
  15162. return SCRIPT_CMD_SUCCESS;
  15163. }
  15164. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15165. offs = 3;
  15166. // get the count of new entries
  15167. amount = (script_lastdata(st)-2)/offs;
  15168. #if PACKETVER >= 20131223
  15169. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  15170. for (int n = 0, i = 3; n < amount; n++, i += offs) {
  15171. t_itemid nameid = script_getnum(st,i);
  15172. uint16 j;
  15173. if( itemdb_exists( nameid ) == nullptr ){
  15174. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15175. script_pushint( st, 0 );
  15176. return SCRIPT_CMD_FAILURE;
  15177. }
  15178. // Check existing entries
  15179. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  15180. if (j == nd->u.shop.count) {
  15181. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  15182. j = nd->u.shop.count;
  15183. nd->u.shop.shop_item[j].flag = 1;
  15184. nd->u.shop.count++;
  15185. }
  15186. int32 stock = script_getnum( st, i + 2 );
  15187. if( stock < -1 ){
  15188. ShowError( "builtin_npcshopadditem: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  15189. script_pushint( st, 0 );
  15190. return SCRIPT_CMD_FAILURE;
  15191. }
  15192. nd->u.shop.shop_item[j].nameid = nameid;
  15193. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  15194. nd->u.shop.shop_item[j].qty = stock;
  15195. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  15196. }
  15197. script_pushint(st,1);
  15198. return SCRIPT_CMD_SUCCESS;
  15199. }
  15200. #endif
  15201. // append new items to existing shop item list
  15202. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  15203. for (int n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  15204. {
  15205. t_itemid nameid = script_getnum( st, i );
  15206. if( itemdb_exists( nameid ) == nullptr ){
  15207. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  15208. script_pushint( st, 0 );
  15209. return SCRIPT_CMD_FAILURE;
  15210. }
  15211. nd->u.shop.shop_item[n].nameid = nameid;
  15212. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  15213. nd->u.shop.count++;
  15214. }
  15215. script_pushint(st,1);
  15216. return SCRIPT_CMD_SUCCESS;
  15217. }
  15218. BUILDIN_FUNC(npcshopdelitem)
  15219. {
  15220. const char* npcname = script_getstr(st,2);
  15221. struct npc_data* nd = npc_name2id(npcname);
  15222. int n, i, size;
  15223. unsigned short amount;
  15224. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15225. script_pushint(st,0);
  15226. return SCRIPT_CMD_SUCCESS;
  15227. }
  15228. amount = script_lastdata(st)-2;
  15229. size = nd->u.shop.count;
  15230. // remove specified items from the shop item list
  15231. for( i = 3; i < 3 + amount; i++ ) {
  15232. t_itemid nameid = script_getnum(st,i);
  15233. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  15234. if( n < size ) {
  15235. if (n+1 != size)
  15236. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  15237. #if PACKETVER >= 20131223
  15238. if (nd->subtype == NPCTYPE_MARKETSHOP)
  15239. npc_market_delfromsql_(nd->exname, nameid, false);
  15240. #endif
  15241. size--;
  15242. }
  15243. }
  15244. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  15245. nd->u.shop.count = size;
  15246. script_pushint(st,1);
  15247. return SCRIPT_CMD_SUCCESS;
  15248. }
  15249. /**
  15250. * Sets a script to attach to a shop npc.
  15251. * npcshopattach "<npc_name>";
  15252. **/
  15253. BUILDIN_FUNC(npcshopattach)
  15254. {
  15255. const char* npcname = script_getstr(st,2);
  15256. struct npc_data* nd = npc_name2id(npcname);
  15257. int flag = 1;
  15258. if( script_hasdata(st,3) )
  15259. flag = script_getnum(st,3);
  15260. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  15261. script_pushint(st,0);
  15262. return SCRIPT_CMD_SUCCESS;
  15263. }
  15264. if (flag)
  15265. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  15266. else
  15267. nd->master_nd = NULL;
  15268. script_pushint(st,1);
  15269. return SCRIPT_CMD_SUCCESS;
  15270. }
  15271. /*==========================================
  15272. * Returns some values of an item [Lupus]
  15273. * Price, Weight, etc...
  15274. setitemscript(itemID,"{new item bonus script}",[n]);
  15275. Where n:
  15276. 0 - script
  15277. 1 - Equip script
  15278. 2 - Unequip script
  15279. *------------------------------------------*/
  15280. BUILDIN_FUNC(setitemscript)
  15281. {
  15282. int n = 0;
  15283. const char *script;
  15284. struct item_data *i_data;
  15285. struct script_code **dstscript;
  15286. t_itemid item_id = script_getnum(st,2);
  15287. script = script_getstr(st,3);
  15288. if( script_hasdata(st,4) )
  15289. n=script_getnum(st,4);
  15290. i_data = itemdb_exists(item_id);
  15291. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15292. script_pushint(st,0);
  15293. return SCRIPT_CMD_SUCCESS;
  15294. }
  15295. switch (n) {
  15296. case 2:
  15297. dstscript = &i_data->unequip_script;
  15298. break;
  15299. case 1:
  15300. dstscript = &i_data->equip_script;
  15301. break;
  15302. default:
  15303. dstscript = &i_data->script;
  15304. break;
  15305. }
  15306. if(*dstscript)
  15307. script_free_code(*dstscript);
  15308. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15309. script_pushint(st,1);
  15310. return SCRIPT_CMD_SUCCESS;
  15311. }
  15312. /*=======================================================
  15313. * Add or Update a mob drop temporarily [Akinari]
  15314. * Original Idea By: [Lupus]
  15315. *
  15316. * addmonsterdrop <mob_id or name>,<item_id>,<rate>,{<steal protected>,{<random option group id>}};
  15317. *
  15318. * If given an item the mob already drops, the rate
  15319. * is updated to the new rate. Rate range is 1-10000.
  15320. * Returns true if succeeded (added/updated a mob drop) false otherwise.
  15321. *-------------------------------------------------------*/
  15322. BUILDIN_FUNC(addmonsterdrop)
  15323. {
  15324. std::shared_ptr<s_mob_db> mob;
  15325. if (script_isstring(st, 2))
  15326. mob = mob_db.find(mobdb_searchname(script_getstr(st, 2)));
  15327. else
  15328. mob = mob_db.find(script_getnum(st, 2));
  15329. if (mob == nullptr) {
  15330. if (script_isstring(st, 2))
  15331. ShowError("addmonsterdrop: bad mob name given %s\n", script_getstr(st, 2));
  15332. else
  15333. ShowError("addmonsterdrop: bad mob id given %d\n", script_getnum(st, 2));
  15334. script_pushint(st, false);
  15335. return SCRIPT_CMD_FAILURE;
  15336. }
  15337. t_itemid item_id = script_getnum(st, 3);
  15338. item_data *itm = itemdb_exists(item_id);
  15339. if (itm == nullptr) {
  15340. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id);
  15341. script_pushint(st, false);
  15342. return SCRIPT_CMD_FAILURE;
  15343. }
  15344. int rate = script_getnum(st, 4);
  15345. if (rate < 1 || rate > 10000) {
  15346. ShowError("addmonsterdrop: Invalid rate %d (min 1, max 10000).\n", rate);
  15347. script_pushint(st, false);
  15348. return SCRIPT_CMD_FAILURE;
  15349. }
  15350. uint16 c = 0;
  15351. for (uint16 i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15352. if (mob->dropitem[i].nameid > 0) {
  15353. if (mob->dropitem[i].nameid == item_id) { // If it equals item_id we update that drop
  15354. c = i;
  15355. break;
  15356. }
  15357. continue;
  15358. }
  15359. if (c == 0) // Accept first available slot only
  15360. c = i;
  15361. }
  15362. if (c == 0) { // No place to put the new drop
  15363. script_pushint(st, false);
  15364. return SCRIPT_CMD_SUCCESS;
  15365. }
  15366. int steal_protected = 0;
  15367. int group = 0;
  15368. if (script_hasdata(st,5))
  15369. steal_protected = script_getnum(st, 5);
  15370. if (script_hasdata(st,6)) {
  15371. group = script_getnum(st, 6);
  15372. if (!random_option_group.exists(group)) {
  15373. ShowError("buildin_addmonsterdrop: Unknown random option group %d.\n", group);
  15374. script_pushint(st, false);
  15375. return SCRIPT_CMD_FAILURE;
  15376. }
  15377. if (itm->type != IT_WEAPON && itm->type != IT_ARMOR && itm->type != IT_SHADOWGEAR) {
  15378. ShowError("buildin_addmonsterdrop: Random option group can't be used with this type of item (item Id: %d).\n", item_id);
  15379. script_pushint(st, false);
  15380. return SCRIPT_CMD_FAILURE;
  15381. }
  15382. }
  15383. // Fill in the slot with the item and rate
  15384. mob->dropitem[c].nameid = item_id;
  15385. mob->dropitem[c].rate = rate;
  15386. mob->dropitem[c].steal_protected = steal_protected > 0;
  15387. mob->dropitem[c].randomopt_group = group;
  15388. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15389. script_pushint(st, true);
  15390. return SCRIPT_CMD_SUCCESS;
  15391. }
  15392. /*=======================================================
  15393. * Delete a mob drop temporarily [Akinari]
  15394. * Original Idea By: [Lupus]
  15395. *
  15396. * delmonsterdrop <mob_id or name>,<item_id>;
  15397. *
  15398. * Returns 1 if succeeded (deleted a mob drop)
  15399. *-------------------------------------------------------*/
  15400. BUILDIN_FUNC(delmonsterdrop)
  15401. {
  15402. std::shared_ptr<s_mob_db> mob;
  15403. if(script_isstring(st, 2))
  15404. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15405. else
  15406. mob = mob_db.find(script_getnum(st,2));
  15407. t_itemid item_id=script_getnum(st,3);
  15408. if(!item_db.exists(item_id)){
  15409. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15410. return SCRIPT_CMD_FAILURE;
  15411. }
  15412. if(mob) { //We got a valid monster, check for item drop on monster
  15413. unsigned char i;
  15414. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15415. if(mob->dropitem[i].nameid == item_id) {
  15416. mob->dropitem[i].nameid = 0;
  15417. mob->dropitem[i].rate = 0;
  15418. mob->dropitem[i].steal_protected = false;
  15419. mob->dropitem[i].randomopt_group = 0;
  15420. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15421. script_pushint(st,1);
  15422. return SCRIPT_CMD_SUCCESS;
  15423. }
  15424. }
  15425. //No drop on that monster
  15426. script_pushint(st,0);
  15427. } else {
  15428. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15429. return SCRIPT_CMD_FAILURE;
  15430. }
  15431. return SCRIPT_CMD_SUCCESS;
  15432. }
  15433. /*==========================================
  15434. * Returns some values of a monster [Lupus]
  15435. * Name, Level, race, size, etc...
  15436. getmonsterinfo(monsterID,queryIndex);
  15437. *------------------------------------------*/
  15438. BUILDIN_FUNC(getmonsterinfo)
  15439. {
  15440. int mob_id;
  15441. mob_id = script_getnum(st,2);
  15442. if (!mobdb_checkid(mob_id)) {
  15443. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15444. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  15445. script_pushconststr(st,"null");
  15446. else
  15447. script_pushint(st,-1);
  15448. return SCRIPT_CMD_SUCCESS;
  15449. }
  15450. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  15451. switch ( script_getnum(st,3) ) {
  15452. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15453. case MOB_LV: script_pushint(st,mob->lv); break;
  15454. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15455. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15456. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15457. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15458. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15459. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15460. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15461. case MOB_RES: script_pushint(st, mob->status.res); break;
  15462. case MOB_MRES: script_pushint(st, mob->status.mres); break;
  15463. case MOB_STR: script_pushint(st,mob->status.str); break;
  15464. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15465. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15466. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15467. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15468. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15469. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15470. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15471. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15472. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15473. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15474. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15475. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15476. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15477. default: script_pushint(st,-1); //wrong Index
  15478. }
  15479. return SCRIPT_CMD_SUCCESS;
  15480. }
  15481. /**
  15482. * Check player's vending/buyingstore/autotrade state
  15483. * checkvending({"<Player Name>"})
  15484. * @author [Nab4]
  15485. */
  15486. BUILDIN_FUNC(checkvending) {
  15487. TBL_PC *sd = NULL;
  15488. if (!script_nick2sd(2,sd) ) {
  15489. script_pushint(st,0);
  15490. return SCRIPT_CMD_SUCCESS;
  15491. }
  15492. else {
  15493. int8 ret = 0;
  15494. if (sd->state.vending)
  15495. ret = 1;
  15496. else if (sd->state.buyingstore)
  15497. ret = 4;
  15498. if (sd->state.autotrade)
  15499. ret |= 2;
  15500. script_pushint(st, ret);
  15501. }
  15502. return SCRIPT_CMD_SUCCESS;
  15503. }
  15504. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15505. {
  15506. TBL_PC *sd = NULL;
  15507. if( script_nick2sd(2,sd) )
  15508. script_pushint(st,(sd->chatID != 0));
  15509. else
  15510. script_pushint(st,0);
  15511. return SCRIPT_CMD_SUCCESS;
  15512. }
  15513. BUILDIN_FUNC(checkidle)
  15514. {
  15515. TBL_PC *sd = NULL;
  15516. if( script_nick2sd(2,sd) )
  15517. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15518. else
  15519. script_pushint(st, 0);
  15520. return SCRIPT_CMD_SUCCESS;
  15521. }
  15522. BUILDIN_FUNC(checkidlehom)
  15523. {
  15524. TBL_PC *sd = NULL;
  15525. if( script_nick2sd(2,sd) )
  15526. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15527. else
  15528. script_pushint(st, 0);
  15529. return SCRIPT_CMD_SUCCESS;
  15530. }
  15531. BUILDIN_FUNC(checkidlemer)
  15532. {
  15533. TBL_PC *sd;
  15534. if( script_nick2sd(2,sd) )
  15535. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_mer));
  15536. else
  15537. script_pushint(st, 0);
  15538. return SCRIPT_CMD_SUCCESS;
  15539. }
  15540. BUILDIN_FUNC(searchitem)
  15541. {
  15542. struct script_data* data = script_getdata(st, 2);
  15543. const char *itemname = script_getstr(st,3);
  15544. struct item_data *items[MAX_SEARCH];
  15545. int count;
  15546. char* name;
  15547. int32 start;
  15548. int32 id;
  15549. int32 i;
  15550. TBL_PC* sd = NULL;
  15551. if ((items[0] = itemdb_exists(atoi(itemname))))
  15552. count = 1;
  15553. else
  15554. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  15555. if (!count) {
  15556. script_pushint(st, 0);
  15557. return SCRIPT_CMD_SUCCESS;
  15558. }
  15559. if( !data_isreference(data) )
  15560. {
  15561. ShowError("script:searchitem: not a variable\n");
  15562. script_reportdata(data);
  15563. st->state = END;
  15564. return SCRIPT_CMD_FAILURE;// not a variable
  15565. }
  15566. id = reference_getid(data);
  15567. start = reference_getindex(data);
  15568. name = reference_getname(data);
  15569. if( not_server_variable(*name) && !script_rid2sd(sd) )
  15570. {
  15571. return SCRIPT_CMD_SUCCESS;// no player attached
  15572. }
  15573. if( is_string_variable(name) )
  15574. {// string array
  15575. ShowError("script:searchitem: not an integer array reference\n");
  15576. script_reportdata(data);
  15577. st->state = END;
  15578. return SCRIPT_CMD_FAILURE;// not supported
  15579. }
  15580. for( i = 0; i < count; ++start, ++i )
  15581. {// Set array
  15582. set_reg_num( st, sd, reference_uid( id, start ), name, items[i]->nameid, reference_getref( data ) );
  15583. }
  15584. script_pushint(st, count);
  15585. return SCRIPT_CMD_SUCCESS;
  15586. }
  15587. // [zBuffer] List of player cont commands --->
  15588. BUILDIN_FUNC(rid2name)
  15589. {
  15590. struct block_list *bl = NULL;
  15591. int rid = script_getnum(st,2);
  15592. if((bl = map_id2bl(rid)))
  15593. {
  15594. switch(bl->type) {
  15595. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15596. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15597. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15598. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15599. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15600. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name.c_str()); break;
  15601. default:
  15602. ShowError("buildin_rid2name: BL type unknown.\n");
  15603. script_pushconststr(st,"");
  15604. break;
  15605. }
  15606. } else {
  15607. ShowError("buildin_rid2name: invalid RID\n");
  15608. script_pushconststr(st,"(null)");
  15609. }
  15610. return SCRIPT_CMD_SUCCESS;
  15611. }
  15612. /**
  15613. * Toggle a unit from moving.
  15614. * pcblockmove(<unit_id>,<option>);
  15615. */
  15616. BUILDIN_FUNC(pcblockmove)
  15617. {
  15618. struct block_list *bl = NULL;
  15619. if (script_getnum(st, 2))
  15620. bl = map_id2bl(script_getnum(st,2));
  15621. else
  15622. bl = map_id2bl(st->rid);
  15623. if (bl) {
  15624. struct unit_data *ud = unit_bl2ud(bl);
  15625. if (ud)
  15626. ud->state.blockedmove = script_getnum(st,3) > 0;
  15627. }
  15628. return SCRIPT_CMD_SUCCESS;
  15629. }
  15630. /**
  15631. * Toggle a unit from casting skills.
  15632. * pcblockskill(<unit_id>,<option>);
  15633. */
  15634. BUILDIN_FUNC(pcblockskill)
  15635. {
  15636. struct block_list *bl = NULL;
  15637. if (script_getnum(st, 2))
  15638. bl = map_id2bl(script_getnum(st,2));
  15639. else
  15640. bl = map_id2bl(st->rid);
  15641. if (bl) {
  15642. struct unit_data *ud = unit_bl2ud(bl);
  15643. if (ud)
  15644. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15645. }
  15646. return SCRIPT_CMD_SUCCESS;
  15647. }
  15648. BUILDIN_FUNC(setpcblock)
  15649. {
  15650. TBL_PC *sd;
  15651. if (script_mapid2sd(4, sd)) {
  15652. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  15653. if (script_getnum(st, 3) > 0)
  15654. sd->state.block_action |= type;
  15655. else
  15656. sd->state.block_action &= ~type;
  15657. }
  15658. return SCRIPT_CMD_SUCCESS;
  15659. }
  15660. BUILDIN_FUNC(getpcblock)
  15661. {
  15662. TBL_PC *sd;
  15663. if (script_mapid2sd(2, sd))
  15664. script_pushint(st, sd->state.block_action);
  15665. else
  15666. script_pushint(st, 0);
  15667. return SCRIPT_CMD_SUCCESS;
  15668. }
  15669. BUILDIN_FUNC(pcfollow)
  15670. {
  15671. TBL_PC *sd;
  15672. if( script_mapid2sd(2,sd) )
  15673. pc_follow(sd, script_getnum(st,3));
  15674. return SCRIPT_CMD_SUCCESS;
  15675. }
  15676. BUILDIN_FUNC(pcstopfollow)
  15677. {
  15678. TBL_PC *sd;
  15679. if(script_mapid2sd(2,sd))
  15680. pc_stop_following(sd);
  15681. return SCRIPT_CMD_SUCCESS;
  15682. }
  15683. // <--- [zBuffer] List of player cont commands
  15684. // [zBuffer] List of unit control commands --->
  15685. /// Checks to see if the unit exists.
  15686. ///
  15687. /// unitexists <unit id>;
  15688. BUILDIN_FUNC(unitexists)
  15689. {
  15690. struct block_list* bl;
  15691. bl = map_id2bl(script_getnum(st, 2));
  15692. if (!bl)
  15693. script_pushint(st, false);
  15694. else
  15695. script_pushint(st, true);
  15696. return SCRIPT_CMD_SUCCESS;
  15697. }
  15698. /// Gets the type of the given Game ID.
  15699. ///
  15700. /// getunittype <unit id>;
  15701. BUILDIN_FUNC(getunittype)
  15702. {
  15703. struct block_list* bl;
  15704. int value = 0;
  15705. if(!script_rid2bl(2,bl))
  15706. {
  15707. script_pushint(st, -1);
  15708. return SCRIPT_CMD_FAILURE;
  15709. }
  15710. switch (bl->type) {
  15711. case BL_PC:
  15712. case BL_NPC:
  15713. case BL_PET:
  15714. case BL_MOB:
  15715. case BL_HOM:
  15716. case BL_MER:
  15717. case BL_ELEM:
  15718. value = bl->type;
  15719. break;
  15720. default:
  15721. value = -1;
  15722. break;
  15723. }
  15724. script_pushint(st, value);
  15725. return SCRIPT_CMD_SUCCESS;
  15726. }
  15727. /// Gets specific live information of a bl.
  15728. ///
  15729. /// getunitdata <unit id>,<arrayname>;
  15730. BUILDIN_FUNC(getunitdata)
  15731. {
  15732. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15733. struct block_list* bl;
  15734. TBL_MOB* md = NULL;
  15735. TBL_HOM* hd = NULL;
  15736. TBL_MER* mc = NULL;
  15737. TBL_PET* pd = NULL;
  15738. TBL_ELEM* ed = NULL;
  15739. TBL_NPC* nd = NULL;
  15740. char* name;
  15741. struct script_data *data = script_getdata(st, 3);
  15742. if (!data_isreference(data)) {
  15743. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15744. return SCRIPT_CMD_FAILURE;
  15745. }
  15746. if(!script_rid2bl(2,bl))
  15747. {
  15748. script_pushint(st, -1);
  15749. return SCRIPT_CMD_FAILURE;
  15750. }
  15751. switch (bl->type) {
  15752. case BL_MOB: md = map_id2md(bl->id); break;
  15753. case BL_HOM: hd = map_id2hd(bl->id); break;
  15754. case BL_PET: pd = map_id2pd(bl->id); break;
  15755. case BL_MER: mc = map_id2mc(bl->id); break;
  15756. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15757. case BL_NPC: nd = map_id2nd(bl->id); break;
  15758. default:
  15759. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  15760. return SCRIPT_CMD_FAILURE;
  15761. }
  15762. name = reference_getname(data);
  15763. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  15764. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  15765. return SCRIPT_CMD_FAILURE;
  15766. }
  15767. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  15768. switch(bl->type) {
  15769. case BL_MOB:
  15770. if (!md) {
  15771. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  15772. return SCRIPT_CMD_FAILURE;
  15773. }
  15774. getunitdata_sub(UMOB_SIZE, md->status.size);
  15775. getunitdata_sub(UMOB_LEVEL, md->level);
  15776. getunitdata_sub(UMOB_HP, md->status.hp);
  15777. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  15778. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  15779. getunitdata_sub(UMOB_MAPID, md->bl.m);
  15780. getunitdata_sub(UMOB_X, md->bl.x);
  15781. getunitdata_sub(UMOB_Y, md->bl.y);
  15782. getunitdata_sub(UMOB_SPEED, md->status.speed);
  15783. getunitdata_sub(UMOB_MODE, md->status.mode);
  15784. getunitdata_sub(UMOB_AI, md->special_state.ai);
  15785. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  15786. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15787. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15788. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15789. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15790. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15791. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15792. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15793. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15794. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15795. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15796. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15797. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15798. getunitdata_sub(UMOB_STR, md->status.str);
  15799. getunitdata_sub(UMOB_AGI, md->status.agi);
  15800. getunitdata_sub(UMOB_VIT, md->status.vit);
  15801. getunitdata_sub(UMOB_INT, md->status.int_);
  15802. getunitdata_sub(UMOB_DEX, md->status.dex);
  15803. getunitdata_sub(UMOB_LUK, md->status.luk);
  15804. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15805. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15806. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15807. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15808. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15809. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15810. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15811. getunitdata_sub(UMOB_DEF, md->status.def);
  15812. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15813. getunitdata_sub(UMOB_HIT, md->status.hit);
  15814. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15815. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15816. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15817. getunitdata_sub(UMOB_RACE, md->status.race);
  15818. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15819. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15820. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15821. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15822. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15823. getunitdata_sub(UMOB_TARGETID, md->target_id);
  15824. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  15825. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  15826. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  15827. getunitdata_sub(UMOB_IGNORE_CELL_STACK_LIMIT, md->ud.state.ignore_cell_stack_limit);
  15828. getunitdata_sub(UMOB_RES, md->status.res);
  15829. getunitdata_sub(UMOB_MRES, md->status.mres);
  15830. break;
  15831. case BL_HOM:
  15832. if (!hd) {
  15833. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15834. return SCRIPT_CMD_FAILURE;
  15835. }
  15836. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15837. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15838. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15839. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15840. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15841. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15842. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15843. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15844. getunitdata_sub(UHOM_X, hd->bl.x);
  15845. getunitdata_sub(UHOM_Y, hd->bl.y);
  15846. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15847. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15848. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15849. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15850. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15851. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15852. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15853. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15854. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15855. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15856. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15857. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15858. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15859. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15860. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15861. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15862. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15863. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15864. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15865. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15866. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15867. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15868. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15869. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15870. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15871. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15872. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15873. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15874. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15875. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  15876. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  15877. break;
  15878. case BL_PET:
  15879. if (!pd) {
  15880. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15881. return SCRIPT_CMD_FAILURE;
  15882. }
  15883. getunitdata_sub(UPET_SIZE, pd->status.size);
  15884. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15885. getunitdata_sub(UPET_HP, pd->status.hp);
  15886. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15887. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15888. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15889. getunitdata_sub(UPET_X, pd->bl.x);
  15890. getunitdata_sub(UPET_Y, pd->bl.y);
  15891. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15892. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15893. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15894. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15895. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15896. getunitdata_sub(UPET_STR, pd->status.str);
  15897. getunitdata_sub(UPET_AGI, pd->status.agi);
  15898. getunitdata_sub(UPET_VIT, pd->status.vit);
  15899. getunitdata_sub(UPET_INT, pd->status.int_);
  15900. getunitdata_sub(UPET_DEX, pd->status.dex);
  15901. getunitdata_sub(UPET_LUK, pd->status.luk);
  15902. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15903. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15904. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15905. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15906. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15907. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15908. getunitdata_sub(UPET_DEF, pd->status.def);
  15909. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15910. getunitdata_sub(UPET_HIT, pd->status.hit);
  15911. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15912. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15913. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15914. getunitdata_sub(UPET_RACE, pd->status.race);
  15915. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15916. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15917. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15918. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15919. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15920. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  15921. break;
  15922. case BL_MER:
  15923. if (!mc) {
  15924. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15925. return SCRIPT_CMD_FAILURE;
  15926. }
  15927. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15928. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15929. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15930. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15931. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15932. getunitdata_sub(UMER_X, mc->bl.x);
  15933. getunitdata_sub(UMER_Y, mc->bl.y);
  15934. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15935. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15936. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15937. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15938. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15939. getunitdata_sub(UMER_STR, mc->base_status.str);
  15940. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15941. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15942. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15943. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15944. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15945. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15946. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15947. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15948. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15949. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15950. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15951. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15952. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15953. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15954. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15955. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15956. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15957. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15958. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15959. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15960. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15961. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15962. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15963. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  15964. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  15965. break;
  15966. case BL_ELEM:
  15967. if (!ed) {
  15968. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15969. return SCRIPT_CMD_FAILURE;
  15970. }
  15971. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15972. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15973. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15974. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15975. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15976. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15977. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15978. getunitdata_sub(UELE_X, ed->bl.x);
  15979. getunitdata_sub(UELE_Y, ed->bl.y);
  15980. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15981. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15982. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15983. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15984. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15985. getunitdata_sub(UELE_STR, ed->base_status.str);
  15986. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15987. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15988. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15989. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15990. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15991. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15992. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15993. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15994. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15995. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15996. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15997. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15998. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15999. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  16000. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  16001. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  16002. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  16003. getunitdata_sub(UELE_RACE, ed->base_status.race);
  16004. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  16005. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  16006. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  16007. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  16008. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  16009. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  16010. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  16011. break;
  16012. case BL_NPC:
  16013. if (!nd) {
  16014. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  16015. return SCRIPT_CMD_FAILURE;
  16016. }
  16017. getunitdata_sub(UNPC_LEVEL, nd->level);
  16018. getunitdata_sub(UNPC_HP, nd->status.hp);
  16019. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  16020. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  16021. getunitdata_sub(UNPC_X, nd->bl.x);
  16022. getunitdata_sub(UNPC_Y, nd->bl.y);
  16023. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  16024. getunitdata_sub(UNPC_STR, nd->status.str);
  16025. getunitdata_sub(UNPC_AGI, nd->status.agi);
  16026. getunitdata_sub(UNPC_VIT, nd->status.vit);
  16027. getunitdata_sub(UNPC_INT, nd->status.int_);
  16028. getunitdata_sub(UNPC_DEX, nd->status.dex);
  16029. getunitdata_sub(UNPC_LUK, nd->status.luk);
  16030. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  16031. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  16032. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  16033. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  16034. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  16035. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  16036. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  16037. getunitdata_sub(UNPC_DEF, nd->status.def);
  16038. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  16039. getunitdata_sub(UNPC_HIT, nd->status.hit);
  16040. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  16041. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  16042. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  16043. getunitdata_sub(UNPC_RACE, nd->status.race);
  16044. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  16045. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  16046. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  16047. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  16048. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  16049. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  16050. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  16051. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  16052. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  16053. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  16054. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  16055. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  16056. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  16057. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  16058. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  16059. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  16060. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  16061. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  16062. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  16063. break;
  16064. default:
  16065. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  16066. return SCRIPT_CMD_FAILURE;
  16067. }
  16068. #undef getunitdata_sub
  16069. return SCRIPT_CMD_SUCCESS;
  16070. }
  16071. /// Changes the live data of a bl.
  16072. ///
  16073. /// setunitdata <unit id>,<type>,<value>;
  16074. BUILDIN_FUNC(setunitdata)
  16075. {
  16076. struct block_list* bl;
  16077. if(!script_rid2bl(2,bl))
  16078. {
  16079. script_pushint(st, -1);
  16080. return SCRIPT_CMD_FAILURE;
  16081. }
  16082. TBL_MOB* md;
  16083. TBL_HOM* hd;
  16084. TBL_MER* mc;
  16085. TBL_PET* pd;
  16086. TBL_ELEM* ed;
  16087. TBL_NPC* nd;
  16088. switch (bl->type) {
  16089. case BL_MOB: md = map_id2md(bl->id); break;
  16090. case BL_HOM: hd = map_id2hd(bl->id); break;
  16091. case BL_PET: pd = map_id2pd(bl->id); break;
  16092. case BL_MER: mc = map_id2mc(bl->id); break;
  16093. case BL_ELEM: ed = map_id2ed(bl->id); break;
  16094. case BL_NPC:
  16095. nd = map_id2nd(bl->id);
  16096. if (!nd->status.hp)
  16097. status_calc_npc(nd, SCO_FIRST);
  16098. else
  16099. status_calc_npc(nd, SCO_NONE);
  16100. break;
  16101. default:
  16102. ShowError("buildin_setunitdata: Invalid object!\n");
  16103. return SCRIPT_CMD_FAILURE;
  16104. }
  16105. int type = script_getnum(st, 3), value = 0;
  16106. const char *mapname = NULL;
  16107. bool calc_status = false;
  16108. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  16109. mapname = script_getstr(st, 4);
  16110. else
  16111. value = script_getnum(st, 4);
  16112. switch (bl->type) {
  16113. case BL_MOB:
  16114. if (!md) {
  16115. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  16116. return SCRIPT_CMD_FAILURE;
  16117. }
  16118. if (!md->base_status) {
  16119. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  16120. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  16121. }
  16122. // Check if the view data will be modified
  16123. switch( type ){
  16124. case UMOB_SEX:
  16125. //case UMOB_CLASS: // Called by status_set_viewdata
  16126. case UMOB_HAIRSTYLE:
  16127. case UMOB_HAIRCOLOR:
  16128. case UMOB_HEADBOTTOM:
  16129. case UMOB_HEADMIDDLE:
  16130. case UMOB_HEADTOP:
  16131. case UMOB_CLOTHCOLOR:
  16132. case UMOB_SHIELD:
  16133. case UMOB_WEAPON:
  16134. case UMOB_ROBE:
  16135. case UMOB_BODY2:
  16136. mob_set_dynamic_viewdata( md );
  16137. break;
  16138. }
  16139. switch (type) {
  16140. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  16141. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  16142. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  16143. 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;
  16144. case UMOB_MASTERAID: md->master_id = value; break;
  16145. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16146. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16147. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  16148. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16149. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  16150. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  16151. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  16152. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  16153. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  16154. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16155. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16156. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16157. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16158. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16159. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16160. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16161. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16162. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16163. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16164. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16165. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16166. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16167. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16168. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16169. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  16170. case UMOB_SLAVECPYMSTRMD:
  16171. if (value > 0) {
  16172. TBL_MOB *md2 = NULL;
  16173. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  16174. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  16175. break;
  16176. }
  16177. md->base_status->mode = md2->status.mode;
  16178. md->state.copy_master_mode = 1;
  16179. } else
  16180. md->state.copy_master_mode = 0;
  16181. calc_status = true;
  16182. break;
  16183. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  16184. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  16185. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  16186. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16187. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  16188. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  16189. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  16190. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  16191. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  16192. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  16193. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  16194. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  16195. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  16196. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  16197. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  16198. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  16199. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  16200. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  16201. case UMOB_TARGETID: {
  16202. if (value==0) {
  16203. mob_unlocktarget(md,gettick());
  16204. break;
  16205. }
  16206. struct block_list* target = map_id2bl(value);
  16207. if (!target) {
  16208. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  16209. return SCRIPT_CMD_FAILURE;
  16210. }
  16211. mob_target(md,target,0);
  16212. break;
  16213. }
  16214. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16215. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16216. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  16217. case UMOB_IGNORE_CELL_STACK_LIMIT: md->ud.state.ignore_cell_stack_limit = value > 0; break;
  16218. case UMOB_RES: md->base_status->res = (short)value; calc_status = true; break;
  16219. case UMOB_MRES: md->base_status->mres = (short)value; calc_status = true; break;
  16220. default:
  16221. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  16222. return SCRIPT_CMD_FAILURE;
  16223. }
  16224. if (calc_status)
  16225. status_calc_bl(&md->bl, SCB_BATTLE);
  16226. break;
  16227. case BL_HOM:
  16228. if (!hd) {
  16229. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  16230. return SCRIPT_CMD_FAILURE;
  16231. }
  16232. switch (type) {
  16233. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  16234. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  16235. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16236. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16237. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16238. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16239. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  16240. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16241. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  16242. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  16243. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  16244. 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;
  16245. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16246. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16247. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16248. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16249. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16250. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16251. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16252. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16253. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  16254. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  16255. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16256. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16257. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16258. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16259. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16260. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  16261. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  16262. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  16263. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  16264. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  16265. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  16266. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  16267. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16268. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16269. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  16270. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  16271. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  16272. case UHOM_TARGETID: {
  16273. if (value==0) {
  16274. unit_stop_attack(&hd->bl);
  16275. break;
  16276. }
  16277. struct block_list* target = map_id2bl(value);
  16278. if (!target) {
  16279. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  16280. return SCRIPT_CMD_FAILURE;
  16281. }
  16282. unit_attack(&hd->bl, target->id, 1);
  16283. break;
  16284. }
  16285. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  16286. default:
  16287. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  16288. return SCRIPT_CMD_FAILURE;
  16289. }
  16290. if (calc_status)
  16291. status_calc_bl(&hd->bl, SCB_BATTLE);
  16292. break;
  16293. case BL_PET:
  16294. if (!pd) {
  16295. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  16296. return SCRIPT_CMD_FAILURE;
  16297. }
  16298. switch (type) {
  16299. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  16300. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  16301. case UPET_HP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16302. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16303. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  16304. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16305. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  16306. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  16307. 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;
  16308. 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;
  16309. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16310. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16311. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16312. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16313. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16314. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16315. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16316. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16317. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  16318. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  16319. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  16320. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  16321. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  16322. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  16323. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  16324. case UPET_DEF: pd->status.def = (defType)value; break;
  16325. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  16326. case UPET_HIT: pd->status.hit = (short)value; break;
  16327. case UPET_FLEE: pd->status.flee = (short)value; break;
  16328. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16329. case UPET_CRIT: pd->status.cri = (short)value; break;
  16330. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16331. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16332. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16333. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16334. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16335. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16336. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16337. default:
  16338. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16339. return SCRIPT_CMD_FAILURE;
  16340. }
  16341. break;
  16342. case BL_MER:
  16343. if (!mc) {
  16344. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  16345. return SCRIPT_CMD_FAILURE;
  16346. }
  16347. switch (type) {
  16348. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16349. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16350. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16351. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16352. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16353. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16354. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16355. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  16356. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  16357. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16358. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16359. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16360. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16361. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16362. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16363. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16364. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16365. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16366. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16367. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16368. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16369. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16370. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16371. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16372. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16373. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16374. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16375. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16376. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16377. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16378. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16379. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16380. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16381. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16382. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16383. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16384. case UMER_TARGETID: {
  16385. if (value==0) {
  16386. unit_stop_attack(&mc->bl);
  16387. break;
  16388. }
  16389. struct block_list* target = map_id2bl(value);
  16390. if (!target) {
  16391. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16392. return SCRIPT_CMD_FAILURE;
  16393. }
  16394. unit_attack(&mc->bl, target->id, 1);
  16395. break;
  16396. }
  16397. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16398. default:
  16399. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16400. return SCRIPT_CMD_FAILURE;
  16401. }
  16402. if (calc_status)
  16403. status_calc_bl(&mc->bl, SCB_BATTLE);
  16404. break;
  16405. case BL_ELEM:
  16406. if (!ed) {
  16407. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16408. return SCRIPT_CMD_FAILURE;
  16409. }
  16410. switch (type) {
  16411. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16412. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16413. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16414. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16415. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16416. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16417. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16418. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16419. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16420. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16421. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16422. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16423. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16424. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16425. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16426. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16427. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16428. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16429. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16430. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16431. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16432. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16433. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16434. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16435. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16436. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16437. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16438. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16439. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16440. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16441. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16442. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16443. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16444. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16445. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16446. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16447. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16448. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16449. case UELE_TARGETID: {
  16450. if (value==0) {
  16451. unit_stop_attack(&ed->bl);
  16452. break;
  16453. }
  16454. struct block_list* target = map_id2bl(value);
  16455. if (!target) {
  16456. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16457. return SCRIPT_CMD_FAILURE;
  16458. }
  16459. elemental_change_mode(ed, EL_MODE_AGGRESSIVE);
  16460. unit_attack(&ed->bl, target->id, 1);
  16461. break;
  16462. }
  16463. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16464. default:
  16465. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16466. return SCRIPT_CMD_FAILURE;
  16467. }
  16468. if (calc_status)
  16469. status_calc_bl(&ed->bl, SCB_BATTLE);
  16470. break;
  16471. case BL_NPC:
  16472. if (!nd) {
  16473. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16474. return SCRIPT_CMD_FAILURE;
  16475. }
  16476. switch (type) {
  16477. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16478. case UNPC_HP: nd->status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16479. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16480. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16481. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16482. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16483. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16484. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16485. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16486. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16487. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16488. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16489. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16490. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16491. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16492. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16493. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16494. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16495. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16496. case UNPC_DEF: nd->status.def = (defType)value; break;
  16497. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16498. case UNPC_HIT: nd->status.hit = (short)value; break;
  16499. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16500. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16501. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16502. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16503. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16504. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16505. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16506. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16507. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16508. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16509. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16510. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16511. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16512. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16513. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16514. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16515. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16516. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16517. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16518. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16519. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16520. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16521. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16522. default:
  16523. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16524. return SCRIPT_CMD_FAILURE;
  16525. }
  16526. break;
  16527. default:
  16528. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16529. return SCRIPT_CMD_FAILURE;
  16530. }
  16531. // Client information updates
  16532. switch (bl->type) {
  16533. case BL_HOM:
  16534. clif_send_homdata(hd->master, SP_ACK, 0);
  16535. break;
  16536. case BL_PET:
  16537. clif_send_petstatus(pd->master);
  16538. break;
  16539. case BL_MER:
  16540. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16541. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16542. break;
  16543. case BL_ELEM:
  16544. clif_elemental_info(ed->master);
  16545. break;
  16546. }
  16547. return SCRIPT_CMD_SUCCESS;
  16548. }
  16549. /// Gets the name of a bl.
  16550. /// Supported types are [MOB|HOM|PET|NPC].
  16551. /// MER and ELEM don't support custom names.
  16552. ///
  16553. /// getunitname <unit id>;
  16554. BUILDIN_FUNC(getunitname)
  16555. {
  16556. struct block_list* bl = NULL;
  16557. if(!script_rid2bl(2,bl)){
  16558. script_pushconststr(st, "Unknown");
  16559. return SCRIPT_CMD_FAILURE;
  16560. }
  16561. script_pushstrcopy(st, status_get_name(bl));
  16562. return SCRIPT_CMD_SUCCESS;
  16563. }
  16564. /// Changes the name of a bl.
  16565. /// Supported types are [MOB|HOM|PET].
  16566. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16567. ///
  16568. /// setunitname <unit id>,<name>;
  16569. BUILDIN_FUNC(setunitname)
  16570. {
  16571. struct block_list* bl = NULL;
  16572. TBL_MOB* md = NULL;
  16573. TBL_HOM* hd = NULL;
  16574. TBL_PET* pd = NULL;
  16575. if(!script_rid2bl(2,bl))
  16576. {
  16577. script_pushconststr(st, "Unknown");
  16578. return SCRIPT_CMD_FAILURE;
  16579. }
  16580. switch (bl->type) {
  16581. case BL_MOB: md = map_id2md(bl->id); break;
  16582. case BL_HOM: hd = map_id2hd(bl->id); break;
  16583. case BL_PET: pd = map_id2pd(bl->id); break;
  16584. default:
  16585. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16586. return SCRIPT_CMD_FAILURE;
  16587. }
  16588. switch (bl->type) {
  16589. case BL_MOB:
  16590. if (!md) {
  16591. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16592. return SCRIPT_CMD_FAILURE;
  16593. }
  16594. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16595. break;
  16596. case BL_HOM:
  16597. if (!hd) {
  16598. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16599. return SCRIPT_CMD_FAILURE;
  16600. }
  16601. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16602. break;
  16603. case BL_PET:
  16604. if (!pd) {
  16605. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16606. return SCRIPT_CMD_FAILURE;
  16607. }
  16608. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16609. break;
  16610. default:
  16611. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16612. return SCRIPT_CMD_FAILURE;
  16613. }
  16614. clif_name_area(bl); // Send update to client.
  16615. return SCRIPT_CMD_SUCCESS;
  16616. }
  16617. /**
  16618. * Sets a unit's title.
  16619. * setunittitle <GID>,<title>;
  16620. */
  16621. BUILDIN_FUNC(setunittitle)
  16622. {
  16623. int gid = script_getnum(st, 2);
  16624. block_list *bl = map_id2bl(gid);
  16625. if (bl == nullptr) {
  16626. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16627. return SCRIPT_CMD_FAILURE;
  16628. }
  16629. unit_data *ud = unit_bl2ud(bl);
  16630. if (ud == nullptr) {
  16631. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16632. return SCRIPT_CMD_FAILURE;
  16633. }
  16634. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16635. clif_name_area(bl);
  16636. return SCRIPT_CMD_SUCCESS;
  16637. }
  16638. /**
  16639. * Gets a unit's title.
  16640. * getunittitle <GID>;
  16641. */
  16642. BUILDIN_FUNC(getunittitle)
  16643. {
  16644. int gid = script_getnum(st, 2);
  16645. block_list *bl = map_id2bl(gid);
  16646. if (bl == nullptr) {
  16647. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16648. return SCRIPT_CMD_FAILURE;
  16649. }
  16650. unit_data *ud = unit_bl2ud(bl);
  16651. if (ud == nullptr) {
  16652. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16653. return SCRIPT_CMD_FAILURE;
  16654. }
  16655. script_pushstrcopy(st, ud->title);
  16656. return SCRIPT_CMD_SUCCESS;
  16657. }
  16658. /// Makes the unit walk to target position or map.
  16659. /// Returns if it was successful.
  16660. ///
  16661. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16662. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16663. BUILDIN_FUNC(unitwalk)
  16664. {
  16665. struct block_list* bl;
  16666. struct unit_data *ud = NULL;
  16667. const char *cmd = script_getfuncname(st), *done_label = "";
  16668. uint8 off = 5;
  16669. if(!script_rid2bl(2,bl))
  16670. {
  16671. script_pushint(st, 0);
  16672. return SCRIPT_CMD_FAILURE;
  16673. }
  16674. ud = unit_bl2ud(bl);
  16675. if (bl->type == BL_NPC) {
  16676. if (!((TBL_NPC*)bl)->status.hp)
  16677. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16678. else
  16679. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16680. }
  16681. if (!strcmp(cmd,"unitwalk")) {
  16682. int x = script_getnum(st,3);
  16683. int y = script_getnum(st,4);
  16684. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  16685. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  16686. } else {
  16687. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  16688. if (!tbl) {
  16689. ShowError("buildin_unitwalk: Bad target destination.\n");
  16690. script_pushint(st, 0);
  16691. return SCRIPT_CMD_FAILURE;
  16692. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  16693. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  16694. off = 4;
  16695. }
  16696. if (ud && script_hasdata(st, off)) {
  16697. done_label = script_getstr(st, off);
  16698. check_event(st, done_label);
  16699. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  16700. }
  16701. return SCRIPT_CMD_SUCCESS;
  16702. }
  16703. /// Kills the unit.
  16704. ///
  16705. /// unitkill <unit_id>;
  16706. BUILDIN_FUNC(unitkill)
  16707. {
  16708. struct block_list* bl;
  16709. if(script_rid2bl(2,bl))
  16710. status_kill(bl);
  16711. return SCRIPT_CMD_SUCCESS;
  16712. }
  16713. /// Warps the unit to the target position in the target map.
  16714. /// Returns if it was successful.
  16715. ///
  16716. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  16717. BUILDIN_FUNC(unitwarp)
  16718. {
  16719. int map_idx;
  16720. short x;
  16721. short y;
  16722. struct block_list* bl;
  16723. const char *mapname;
  16724. mapname = script_getstr(st, 3);
  16725. x = (short)script_getnum(st,4);
  16726. y = (short)script_getnum(st,5);
  16727. if(!script_rid2bl(2,bl))
  16728. {
  16729. script_pushint(st, 0);
  16730. return SCRIPT_CMD_SUCCESS;
  16731. }
  16732. if (!strcmp(mapname,"this"))
  16733. map_idx = bl?bl->m:-1;
  16734. else
  16735. map_idx = map_mapname2mapid(mapname);
  16736. if (map_idx >= 0 && bl != NULL)
  16737. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  16738. else
  16739. script_pushint(st, 0);
  16740. return SCRIPT_CMD_SUCCESS;
  16741. }
  16742. /// Makes the unit attack the target.
  16743. /// If the unit is a player and <action type> is not 0, it does a continuous
  16744. /// attack instead of a single attack.
  16745. /// Returns if the request was successful.
  16746. ///
  16747. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  16748. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  16749. BUILDIN_FUNC(unitattack)
  16750. {
  16751. struct block_list* unit_bl;
  16752. struct block_list* target_bl = NULL;
  16753. int actiontype = 0;
  16754. if (!script_rid2bl(2,unit_bl)) {
  16755. script_pushint(st, false);
  16756. return SCRIPT_CMD_FAILURE;
  16757. }
  16758. if (script_isstring(st, 3)) {
  16759. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  16760. if( sd != NULL )
  16761. target_bl = &sd->bl;
  16762. } else
  16763. target_bl = map_id2bl(script_getnum(st, 3));
  16764. if (!target_bl) {
  16765. script_pushint(st, false);
  16766. return SCRIPT_CMD_FAILURE;
  16767. }
  16768. if (script_hasdata(st,4))
  16769. actiontype = script_getnum(st,4);
  16770. switch(unit_bl->type) {
  16771. case BL_PC: {
  16772. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  16773. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  16774. script_pushint(st, sd->ud.target == target_bl->id);
  16775. return SCRIPT_CMD_SUCCESS;
  16776. }
  16777. case BL_MOB:
  16778. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  16779. break;
  16780. case BL_PET:
  16781. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  16782. break;
  16783. default:
  16784. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  16785. script_pushint(st, false);
  16786. return SCRIPT_CMD_FAILURE;
  16787. }
  16788. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  16789. return SCRIPT_CMD_SUCCESS;
  16790. }
  16791. /// Makes the unit stop attacking.
  16792. ///
  16793. /// unitstopattack <unit_id>;
  16794. BUILDIN_FUNC(unitstopattack)
  16795. {
  16796. struct block_list* bl;
  16797. if(script_rid2bl(2,bl))
  16798. {
  16799. unit_stop_attack(bl);
  16800. if (bl->type == BL_MOB)
  16801. ((TBL_MOB*)bl)->target_id = 0;
  16802. }
  16803. return SCRIPT_CMD_SUCCESS;
  16804. }
  16805. /// Makes the unit stop walking.
  16806. ///
  16807. /// unitstopwalk <unit_id>{,<flag>};
  16808. BUILDIN_FUNC(unitstopwalk)
  16809. {
  16810. struct block_list* bl;
  16811. int flag = USW_NONE;
  16812. if (script_hasdata(st, 3))
  16813. flag = script_getnum(st, 3);
  16814. if(script_rid2bl(2,bl))
  16815. unit_stop_walking(bl, flag);
  16816. return SCRIPT_CMD_SUCCESS;
  16817. }
  16818. /**
  16819. * Makes the unit say the given message.
  16820. *
  16821. * unittalk <unit_id>,"<message>"{,"<flag>"};
  16822. * @param flag: Specify target
  16823. * bc_area - Message is sent to players in the vicinity of the source (default).
  16824. * bc_self - Message is sent only to player attached.
  16825. */
  16826. BUILDIN_FUNC(unittalk)
  16827. {
  16828. const char* message;
  16829. struct block_list* bl;
  16830. message = script_getstr(st, 3);
  16831. if(script_rid2bl(2,bl))
  16832. {
  16833. send_target target = AREA;
  16834. struct StringBuf sbuf;
  16835. if (script_hasdata(st, 4)) {
  16836. if (script_getnum(st, 4) == BC_SELF) {
  16837. if (map_id2sd(bl->id) == NULL) {
  16838. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  16839. return SCRIPT_CMD_FAILURE;
  16840. }
  16841. target = SELF;
  16842. }
  16843. }
  16844. StringBuf_Init(&sbuf);
  16845. StringBuf_Printf(&sbuf, "%s", message);
  16846. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  16847. StringBuf_Destroy(&sbuf);
  16848. }
  16849. return SCRIPT_CMD_SUCCESS;
  16850. }
  16851. /// Makes the unit do an emotion.
  16852. ///
  16853. /// unitemote <unit_id>,<emotion>;
  16854. ///
  16855. /// @see ET_* in script_constants.hpp
  16856. BUILDIN_FUNC(unitemote)
  16857. {
  16858. int emotion;
  16859. struct block_list* bl;
  16860. emotion = script_getnum(st,3);
  16861. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  16862. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  16863. return SCRIPT_CMD_FAILURE;
  16864. }
  16865. if (script_rid2bl(2,bl))
  16866. clif_emotion(bl, emotion);
  16867. return SCRIPT_CMD_SUCCESS;
  16868. }
  16869. /// Makes the unit cast the skill on the target or self if no target is specified.
  16870. ///
  16871. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  16872. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>,<cancel>,<Line_ID>};
  16873. BUILDIN_FUNC(unitskilluseid)
  16874. {
  16875. int unit_id, target_id, casttime;
  16876. uint16 skill_id, skill_lv;
  16877. struct block_list* bl;
  16878. unit_id = script_getnum(st,2);
  16879. if (script_isstring(st, 3)) {
  16880. const char *name = script_getstr(st, 3);
  16881. if (!(skill_id = skill_name2id(name))) {
  16882. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  16883. return SCRIPT_CMD_FAILURE;
  16884. }
  16885. } else {
  16886. skill_id = script_getnum(st, 3);
  16887. if (!skill_get_index(skill_id)) {
  16888. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  16889. return SCRIPT_CMD_FAILURE;
  16890. }
  16891. }
  16892. skill_lv = script_getnum(st,4);
  16893. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  16894. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  16895. bool cancel = ( script_hasdata(st,7) ? script_getnum(st,7) > 0 : skill_get_castcancel(skill_id) );
  16896. int msg_id = (script_hasdata(st, 8) ? script_getnum(st, 8) : 0);
  16897. if(script_rid2bl(2,bl)){
  16898. if (msg_id > 0) {
  16899. if (bl->type != BL_MOB) {
  16900. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  16901. return SCRIPT_CMD_FAILURE;
  16902. }
  16903. TBL_MOB* md = map_id2md(bl->id);
  16904. if (md)
  16905. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  16906. }
  16907. if (bl->type == BL_NPC) {
  16908. if (!((TBL_NPC*)bl)->status.hp)
  16909. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16910. else
  16911. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16912. }
  16913. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel);
  16914. }
  16915. return SCRIPT_CMD_SUCCESS;
  16916. }
  16917. /// Makes the unit cast the skill on the target position.
  16918. ///
  16919. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  16920. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>,<cancel>,<Line_ID>};
  16921. BUILDIN_FUNC(unitskillusepos)
  16922. {
  16923. int skill_x, skill_y, casttime;
  16924. uint16 skill_id, skill_lv;
  16925. struct block_list* bl;
  16926. if (script_isstring(st, 3)) {
  16927. const char *name = script_getstr(st, 3);
  16928. if (!(skill_id = skill_name2id(name))) {
  16929. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  16930. return SCRIPT_CMD_FAILURE;
  16931. }
  16932. } else {
  16933. skill_id = script_getnum(st, 3);
  16934. if (!skill_get_index(skill_id)) {
  16935. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  16936. return SCRIPT_CMD_FAILURE;
  16937. }
  16938. }
  16939. skill_lv = script_getnum(st,4);
  16940. skill_x = script_getnum(st,5);
  16941. skill_y = script_getnum(st,6);
  16942. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16943. bool cancel = ( script_hasdata(st,8) ? script_getnum(st,8) > 0 : skill_get_castcancel(skill_id) );
  16944. int msg_id = (script_hasdata(st, 9) ? script_getnum(st, 9) : 0);
  16945. if(script_rid2bl(2,bl)){
  16946. if (msg_id > 0) {
  16947. if (bl->type != BL_MOB) {
  16948. ShowError("buildin_unitskilluseid: Msg can only be used for monster.\n");
  16949. return SCRIPT_CMD_FAILURE;
  16950. }
  16951. TBL_MOB* md = map_id2md(bl->id);
  16952. if (md)
  16953. mob_chat_display_message(*md, static_cast<uint16>(msg_id));
  16954. }
  16955. if (bl->type == BL_NPC) {
  16956. if (!((TBL_NPC*)bl)->status.hp)
  16957. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16958. else
  16959. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16960. }
  16961. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), cancel);
  16962. }
  16963. return SCRIPT_CMD_SUCCESS;
  16964. }
  16965. // <--- [zBuffer] List of unit control commands
  16966. /// Pauses the execution of the script, detaching the player
  16967. ///
  16968. /// sleep <milli seconds>;
  16969. BUILDIN_FUNC(sleep)
  16970. {
  16971. // First call(by function call)
  16972. if (st->sleep.tick == 0) {
  16973. int ticks;
  16974. ticks = script_getnum(st, 2);
  16975. if (ticks <= 0) {
  16976. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  16977. return SCRIPT_CMD_FAILURE;
  16978. }
  16979. // detach the player
  16980. script_detach_rid(st);
  16981. // sleep for the target amount of time
  16982. st->state = RERUNLINE;
  16983. st->sleep.tick = ticks;
  16984. // Second call(by timer after sleeping time is over)
  16985. } else {
  16986. // Continue the script
  16987. st->state = RUN;
  16988. st->sleep.tick = 0;
  16989. }
  16990. return SCRIPT_CMD_SUCCESS;
  16991. }
  16992. /// Pauses the execution of the script, keeping the unit attached
  16993. /// Stops the script if no unit is attached
  16994. ///
  16995. /// sleep2(<milli seconds>)
  16996. BUILDIN_FUNC(sleep2)
  16997. {
  16998. // First call(by function call)
  16999. if (st->sleep.tick == 0) {
  17000. int ticks;
  17001. ticks = script_getnum(st, 2);
  17002. if (ticks <= 0) {
  17003. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  17004. return SCRIPT_CMD_FAILURE;
  17005. }
  17006. if (map_id2bl(st->rid) == NULL) {
  17007. ShowError( "buildin_sleep2: no unit is attached\n" );
  17008. return SCRIPT_CMD_FAILURE;
  17009. }
  17010. // sleep for the target amount of time
  17011. st->state = RERUNLINE;
  17012. st->sleep.tick = ticks;
  17013. // Second call(by timer after sleeping time is over)
  17014. } else {
  17015. // Check if the unit is still attached
  17016. // NOTE: This should never happen, since run_script_timer already checks this
  17017. if (map_id2bl(st->rid) == NULL) {
  17018. // The unit is not attached anymore - terminate the script
  17019. st->rid = 0;
  17020. st->state = END;
  17021. } else {
  17022. // The unit is still attached - continue the script
  17023. st->state = RUN;
  17024. st->sleep.tick = 0;
  17025. }
  17026. }
  17027. return SCRIPT_CMD_SUCCESS;
  17028. }
  17029. /// Awakes all the sleep timers of the target npc
  17030. ///
  17031. /// awake "<npc name>";
  17032. BUILDIN_FUNC(awake)
  17033. {
  17034. DBIterator *iter;
  17035. struct script_state *tst;
  17036. struct npc_data* nd;
  17037. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  17038. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  17039. return SCRIPT_CMD_FAILURE;
  17040. }
  17041. int rid = st->rid;
  17042. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  17043. if( rid ){
  17044. script_detach_rid(st);
  17045. }
  17046. iter = db_iterator(st_db);
  17047. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  17048. if (tst->oid == nd->bl.id) {
  17049. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  17050. continue;
  17051. }
  17052. delete_timer(tst->sleep.timer, run_script_timer);
  17053. // Trigger the timer function
  17054. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  17055. }
  17056. }
  17057. dbi_destroy(iter);
  17058. // If a player had been attached, now is the time to restore it
  17059. if( rid ){
  17060. st->rid = rid;
  17061. script_attach_state(st);
  17062. }
  17063. return SCRIPT_CMD_SUCCESS;
  17064. }
  17065. /// Returns a reference to a variable of the target NPC.
  17066. /// Returns 0 if an error occurs.
  17067. ///
  17068. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  17069. BUILDIN_FUNC(getvariableofnpc)
  17070. {
  17071. struct script_data* data;
  17072. const char* name;
  17073. struct npc_data* nd;
  17074. data = script_getdata(st,2);
  17075. if( !data_isreference(data) )
  17076. {// Not a reference (aka varaible name)
  17077. ShowError("buildin_getvariableofnpc: not a variable\n");
  17078. script_reportdata(data);
  17079. script_pushnil(st);
  17080. st->state = END;
  17081. return SCRIPT_CMD_FAILURE;
  17082. }
  17083. name = reference_getname(data);
  17084. if( *name != '.' || name[1] == '@' )
  17085. {// not a npc variable
  17086. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  17087. script_reportdata(data);
  17088. script_pushnil(st);
  17089. st->state = END;
  17090. return SCRIPT_CMD_FAILURE;
  17091. }
  17092. nd = npc_name2id(script_getstr(st,3));
  17093. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  17094. {// NPC not found or has no script
  17095. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  17096. script_pushnil(st);
  17097. st->state = END;
  17098. return SCRIPT_CMD_FAILURE;
  17099. }
  17100. if (!nd->u.scr.script->local.vars)
  17101. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  17102. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  17103. return SCRIPT_CMD_SUCCESS;
  17104. }
  17105. /// Opens a warp portal.
  17106. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  17107. ///
  17108. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  17109. ///
  17110. /// @author blackhole89
  17111. BUILDIN_FUNC(warpportal)
  17112. {
  17113. int spx;
  17114. int spy;
  17115. unsigned short mapindex;
  17116. int tpx;
  17117. int tpy;
  17118. std::shared_ptr<s_skill_unit_group> group;
  17119. struct block_list* bl;
  17120. bl = map_id2bl(st->oid);
  17121. if( bl == NULL ) {
  17122. ShowError("buildin_warpportal: NPC is needed\n");
  17123. return SCRIPT_CMD_FAILURE;
  17124. }
  17125. spx = script_getnum(st,2);
  17126. spy = script_getnum(st,3);
  17127. mapindex = mapindex_name2id(script_getstr(st, 4));
  17128. tpx = script_getnum(st,5);
  17129. tpy = script_getnum(st,6);
  17130. if( mapindex == 0 ) {
  17131. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  17132. return SCRIPT_CMD_FAILURE;
  17133. }
  17134. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  17135. if( group == NULL )
  17136. return SCRIPT_CMD_FAILURE;// failed
  17137. group->val1 = (group->val1<<16)|(short)0;
  17138. group->val2 = (tpx<<16) | tpy;
  17139. group->val3 = mapindex;
  17140. return SCRIPT_CMD_SUCCESS;
  17141. }
  17142. /**
  17143. * openmail({<char_id>});
  17144. **/
  17145. BUILDIN_FUNC(openmail)
  17146. {
  17147. struct map_session_data* sd;
  17148. if (!script_charid2sd(2,sd))
  17149. return SCRIPT_CMD_FAILURE;
  17150. #if PACKETVER < 20150513
  17151. mail_openmail(sd);
  17152. return SCRIPT_CMD_SUCCESS;
  17153. #else
  17154. ShowError( "buildin_openmail: This command is not supported for PACKETVER 2015-05-13 or newer.\n" );
  17155. return SCRIPT_CMD_FAILURE;
  17156. #endif
  17157. }
  17158. /**
  17159. * openauction({<char_id>});
  17160. **/
  17161. BUILDIN_FUNC(openauction)
  17162. {
  17163. TBL_PC* sd;
  17164. if (!script_charid2sd(2,sd))
  17165. return SCRIPT_CMD_FAILURE;
  17166. if( !battle_config.feature_auction ) {
  17167. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  17168. return SCRIPT_CMD_SUCCESS;
  17169. }
  17170. clif_Auction_openwindow(sd);
  17171. return SCRIPT_CMD_SUCCESS;
  17172. }
  17173. /// Retrieves the value of the specified flag of the specified cell.
  17174. ///
  17175. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  17176. ///
  17177. /// @see cell_chk* constants in src/map/script_constants.hpp for the types
  17178. BUILDIN_FUNC(checkcell)
  17179. {
  17180. int16 m = map_mapname2mapid(script_getstr(st,2));
  17181. int16 x = script_getnum(st,3);
  17182. int16 y = script_getnum(st,4);
  17183. cell_chk type = (cell_chk)script_getnum(st,5);
  17184. script_pushint(st, map_getcell(m, x, y, type));
  17185. return SCRIPT_CMD_SUCCESS;
  17186. }
  17187. /// Modifies flags of cells in the specified area.
  17188. ///
  17189. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  17190. ///
  17191. /// @see cell_* constants in src/map/script_constants.hpp for the types
  17192. BUILDIN_FUNC(setcell)
  17193. {
  17194. int16 m = map_mapname2mapid(script_getstr(st,2));
  17195. int16 x1 = script_getnum(st,3);
  17196. int16 y1 = script_getnum(st,4);
  17197. int16 x2 = script_getnum(st,5);
  17198. int16 y2 = script_getnum(st,6);
  17199. cell_t type = (cell_t)script_getnum(st,7);
  17200. bool flag = script_getnum(st,8) != 0;
  17201. int x,y;
  17202. if( x1 > x2 ) SWAP(x1,x2);
  17203. if( y1 > y2 ) SWAP(y1,y2);
  17204. for( y = y1; y <= y2; ++y )
  17205. for( x = x1; x <= x2; ++x )
  17206. map_setcell(m, x, y, type, flag);
  17207. return SCRIPT_CMD_SUCCESS;
  17208. }
  17209. /**
  17210. * Gets a free cell in the specified area.
  17211. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  17212. */
  17213. BUILDIN_FUNC(getfreecell)
  17214. {
  17215. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  17216. struct script_data
  17217. *data_x = script_getdata(st, 3),
  17218. *data_y = script_getdata(st, 4);
  17219. struct block_list* bl = map_id2bl(st->rid);
  17220. struct map_session_data *sd = nullptr;
  17221. int16 m, x = 0, y = 0;
  17222. int rx = -1, ry = -1, flag = 1;
  17223. if (!data_isreference(data_x)) {
  17224. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  17225. return SCRIPT_CMD_FAILURE;
  17226. }
  17227. if (!data_isreference(data_y)) {
  17228. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  17229. return SCRIPT_CMD_FAILURE;
  17230. }
  17231. name_x = reference_getname(data_x),
  17232. name_y = reference_getname(data_y);
  17233. if (is_string_variable(name_x)) {
  17234. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  17235. return SCRIPT_CMD_FAILURE;
  17236. }
  17237. if (is_string_variable(name_y)) {
  17238. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  17239. return SCRIPT_CMD_FAILURE;
  17240. }
  17241. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  17242. if (!script_rid2sd(sd)) {
  17243. if (not_server_variable(*name_x))
  17244. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  17245. else
  17246. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  17247. return SCRIPT_CMD_FAILURE;
  17248. }
  17249. }
  17250. if (script_hasdata(st, 5))
  17251. x = script_getnum(st, 5);
  17252. if (script_hasdata(st, 6))
  17253. y = script_getnum(st, 6);
  17254. if (script_hasdata(st, 7))
  17255. rx = script_getnum(st, 7);
  17256. if (script_hasdata(st, 8))
  17257. ry = script_getnum(st, 8);
  17258. if (script_hasdata(st, 9))
  17259. flag = script_getnum(st, 9);
  17260. if (bl && strcmp(mapn, "this") == 0)
  17261. m = bl->m;
  17262. else
  17263. m = map_mapname2mapid(mapn);
  17264. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  17265. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  17266. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  17267. return SCRIPT_CMD_SUCCESS;
  17268. }
  17269. /*==========================================
  17270. * Mercenary Commands
  17271. *------------------------------------------*/
  17272. BUILDIN_FUNC(mercenary_create)
  17273. {
  17274. struct map_session_data *sd;
  17275. int class_, contract_time;
  17276. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  17277. return SCRIPT_CMD_SUCCESS;
  17278. class_ = script_getnum(st,2);
  17279. if( !mercenary_db.exists(class_) )
  17280. return SCRIPT_CMD_SUCCESS;
  17281. contract_time = script_getnum(st,3);
  17282. mercenary_create(sd, class_, contract_time);
  17283. return SCRIPT_CMD_SUCCESS;
  17284. }
  17285. BUILDIN_FUNC(mercenary_delete)
  17286. {
  17287. struct map_session_data *sd;
  17288. int type = 0;
  17289. if( !script_charid2sd(2, sd) )
  17290. return SCRIPT_CMD_FAILURE;
  17291. if( sd->md == nullptr ) {
  17292. 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);
  17293. return SCRIPT_CMD_FAILURE;
  17294. }
  17295. if( script_hasdata(st, 3) ) {
  17296. type = script_getnum(st, 3);
  17297. if( type < 0 || type > 3 ) {
  17298. ShowWarning("buildin_mercenary_delete: invalid type value of %d.\n", type);
  17299. return SCRIPT_CMD_FAILURE;
  17300. }
  17301. }
  17302. mercenary_delete(sd->md, type);
  17303. return SCRIPT_CMD_SUCCESS;
  17304. }
  17305. BUILDIN_FUNC(mercenary_heal)
  17306. {
  17307. struct map_session_data *sd;
  17308. int hp, sp;
  17309. if( !script_rid2sd(sd) || sd->md == NULL )
  17310. return SCRIPT_CMD_SUCCESS;
  17311. hp = script_getnum(st,2);
  17312. sp = script_getnum(st,3);
  17313. status_heal(&sd->md->bl, hp, sp, 0);
  17314. return SCRIPT_CMD_SUCCESS;
  17315. }
  17316. BUILDIN_FUNC(mercenary_sc_start)
  17317. {
  17318. struct map_session_data *sd;
  17319. enum sc_type type;
  17320. int tick, val1;
  17321. if( !script_rid2sd(sd) || sd->md == NULL )
  17322. return SCRIPT_CMD_SUCCESS;
  17323. type = (sc_type)script_getnum(st,2);
  17324. tick = script_getnum(st,3);
  17325. val1 = script_getnum(st,4);
  17326. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  17327. return SCRIPT_CMD_SUCCESS;
  17328. }
  17329. BUILDIN_FUNC(mercenary_get_calls)
  17330. {
  17331. struct map_session_data *sd;
  17332. int guild;
  17333. if( !script_rid2sd(sd) )
  17334. return SCRIPT_CMD_SUCCESS;
  17335. guild = script_getnum(st,2);
  17336. switch( guild )
  17337. {
  17338. case ARCH_MERC_GUILD:
  17339. script_pushint(st,sd->status.arch_calls);
  17340. break;
  17341. case SPEAR_MERC_GUILD:
  17342. script_pushint(st,sd->status.spear_calls);
  17343. break;
  17344. case SWORD_MERC_GUILD:
  17345. script_pushint(st,sd->status.sword_calls);
  17346. break;
  17347. default:
  17348. script_pushint(st,0);
  17349. break;
  17350. }
  17351. return SCRIPT_CMD_SUCCESS;
  17352. }
  17353. BUILDIN_FUNC(mercenary_set_calls)
  17354. {
  17355. struct map_session_data *sd;
  17356. int guild, value, *calls;
  17357. if( !script_rid2sd(sd) )
  17358. return SCRIPT_CMD_SUCCESS;
  17359. guild = script_getnum(st,2);
  17360. value = script_getnum(st,3);
  17361. switch( guild )
  17362. {
  17363. case ARCH_MERC_GUILD:
  17364. calls = &sd->status.arch_calls;
  17365. break;
  17366. case SPEAR_MERC_GUILD:
  17367. calls = &sd->status.spear_calls;
  17368. break;
  17369. case SWORD_MERC_GUILD:
  17370. calls = &sd->status.sword_calls;
  17371. break;
  17372. default:
  17373. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  17374. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17375. }
  17376. *calls += value;
  17377. *calls = cap_value(*calls, 0, INT_MAX);
  17378. return SCRIPT_CMD_SUCCESS;
  17379. }
  17380. BUILDIN_FUNC(mercenary_get_faith)
  17381. {
  17382. struct map_session_data *sd;
  17383. int guild;
  17384. if( !script_rid2sd(sd) )
  17385. return SCRIPT_CMD_SUCCESS;
  17386. guild = script_getnum(st,2);
  17387. switch( guild )
  17388. {
  17389. case ARCH_MERC_GUILD:
  17390. script_pushint(st,sd->status.arch_faith);
  17391. break;
  17392. case SPEAR_MERC_GUILD:
  17393. script_pushint(st,sd->status.spear_faith);
  17394. break;
  17395. case SWORD_MERC_GUILD:
  17396. script_pushint(st,sd->status.sword_faith);
  17397. break;
  17398. default:
  17399. script_pushint(st,0);
  17400. break;
  17401. }
  17402. return SCRIPT_CMD_SUCCESS;
  17403. }
  17404. BUILDIN_FUNC(mercenary_set_faith)
  17405. {
  17406. struct map_session_data *sd;
  17407. int guild, value, *calls;
  17408. if( !script_rid2sd(sd) )
  17409. return SCRIPT_CMD_SUCCESS;
  17410. guild = script_getnum(st,2);
  17411. value = script_getnum(st,3);
  17412. switch( guild )
  17413. {
  17414. case ARCH_MERC_GUILD:
  17415. calls = &sd->status.arch_faith;
  17416. break;
  17417. case SPEAR_MERC_GUILD:
  17418. calls = &sd->status.spear_faith;
  17419. break;
  17420. case SWORD_MERC_GUILD:
  17421. calls = &sd->status.sword_faith;
  17422. break;
  17423. default:
  17424. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17425. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17426. }
  17427. *calls += value;
  17428. *calls = cap_value(*calls, 0, INT_MAX);
  17429. if( mercenary_get_guild(sd->md) == guild )
  17430. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17431. return SCRIPT_CMD_SUCCESS;
  17432. }
  17433. /*------------------------------------------
  17434. * Book Reading
  17435. *------------------------------------------*/
  17436. BUILDIN_FUNC(readbook)
  17437. {
  17438. struct map_session_data *sd;
  17439. int book_id, page;
  17440. if( !script_rid2sd(sd) )
  17441. return SCRIPT_CMD_SUCCESS;
  17442. book_id = script_getnum(st,2);
  17443. page = script_getnum(st,3);
  17444. clif_readbook(sd->fd, book_id, page);
  17445. return SCRIPT_CMD_SUCCESS;
  17446. }
  17447. /******************
  17448. Questlog script commands
  17449. *******************/
  17450. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17451. BUILDIN_FUNC(questinfo)
  17452. {
  17453. TBL_NPC* nd = map_id2nd(st->oid);
  17454. if (!nd || nd->bl.m == -1) {
  17455. ShowError("buildin_questinfo: No NPC attached.\n");
  17456. return SCRIPT_CMD_FAILURE;
  17457. }
  17458. int icon = script_getnum(st, 2);
  17459. #if PACKETVER >= 20120410
  17460. switch(icon){
  17461. case QTYPE_QUEST:
  17462. case QTYPE_QUEST2:
  17463. case QTYPE_JOB:
  17464. case QTYPE_JOB2:
  17465. case QTYPE_EVENT:
  17466. case QTYPE_EVENT2:
  17467. // Warg icons were replaced in this client
  17468. #if PACKETVER < 20170315
  17469. case QTYPE_WARG:
  17470. case QTYPE_WARG2:
  17471. #else
  17472. case QTYPE_CLICKME:
  17473. case QTYPE_DAILYQUEST:
  17474. case QTYPE_EVENT3:
  17475. case QTYPE_JOBQUEST:
  17476. case QTYPE_JUMPING_PORING:
  17477. #endif
  17478. // Leave everything as it is
  17479. break;
  17480. case QTYPE_NONE:
  17481. default:
  17482. // Default to nothing if icon id is invalid.
  17483. icon = QTYPE_NONE;
  17484. break;
  17485. }
  17486. #else
  17487. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17488. icon = QTYPE_QUEST;
  17489. else
  17490. icon = icon + 1;
  17491. #endif
  17492. int color = QMARK_NONE;
  17493. if (script_hasdata(st, 3)) {
  17494. color = script_getnum(st, 3);
  17495. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17496. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17497. script_reportfunc(st);
  17498. color = QMARK_NONE;
  17499. }
  17500. }
  17501. struct script_code *script = nullptr;
  17502. if (script_hasdata(st, 4)) {
  17503. const char *str = script_getstr(st, 4);
  17504. if (str) {
  17505. std::string condition(str);
  17506. if (condition.find( "achievement_condition" ) == std::string::npos)
  17507. condition = "achievement_condition( " + condition + " );";
  17508. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17509. if (!script) {
  17510. st->state = END;
  17511. return SCRIPT_CMD_FAILURE;
  17512. }
  17513. }
  17514. }
  17515. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17516. qi->icon = static_cast<e_questinfo_types>(icon);
  17517. qi->color = static_cast<e_questinfo_markcolor>(color);
  17518. qi->condition = script;
  17519. nd->qi_data.push_back(qi);
  17520. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17521. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17522. mapdata->qi_npc.push_back(nd->bl.id);
  17523. return SCRIPT_CMD_SUCCESS;
  17524. }
  17525. /**
  17526. * questinfo_refresh {<char_id>};
  17527. **/
  17528. BUILDIN_FUNC(questinfo_refresh)
  17529. {
  17530. struct map_session_data *sd;
  17531. if (!script_charid2sd(2, sd))
  17532. return SCRIPT_CMD_FAILURE;
  17533. pc_show_questinfo(sd);
  17534. return SCRIPT_CMD_SUCCESS;
  17535. }
  17536. /**
  17537. * setquest <ID>{,<char_id>};
  17538. **/
  17539. BUILDIN_FUNC(setquest)
  17540. {
  17541. struct map_session_data *sd;
  17542. int quest_id;
  17543. quest_id = script_getnum(st, 2);
  17544. if (!script_charid2sd(3,sd))
  17545. return SCRIPT_CMD_FAILURE;
  17546. if( quest_add(sd, quest_id) == -1 ){
  17547. script_reportsrc(st);
  17548. script_reportfunc(st);
  17549. }
  17550. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17551. pc_show_questinfo(sd);
  17552. return SCRIPT_CMD_SUCCESS;
  17553. }
  17554. /**
  17555. * erasequest <ID>{,<char_id>};
  17556. **/
  17557. BUILDIN_FUNC(erasequest)
  17558. {
  17559. struct map_session_data *sd;
  17560. if (!script_charid2sd(3,sd))
  17561. return SCRIPT_CMD_FAILURE;
  17562. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17563. script_reportsrc(st);
  17564. script_reportfunc(st);
  17565. }
  17566. return SCRIPT_CMD_SUCCESS;
  17567. }
  17568. /**
  17569. * completequest <ID>{,<char_id>};
  17570. **/
  17571. BUILDIN_FUNC(completequest)
  17572. {
  17573. struct map_session_data *sd;
  17574. if (!script_charid2sd(3,sd))
  17575. return SCRIPT_CMD_FAILURE;
  17576. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17577. //20120410 or 20090218
  17578. pc_show_questinfo(sd);
  17579. return SCRIPT_CMD_SUCCESS;
  17580. }
  17581. /**
  17582. * changequest <ID>,<ID2>{,<char_id>};
  17583. **/
  17584. BUILDIN_FUNC(changequest)
  17585. {
  17586. struct map_session_data *sd;
  17587. if (!script_charid2sd(4,sd))
  17588. return SCRIPT_CMD_FAILURE;
  17589. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17590. script_reportsrc(st);
  17591. script_reportfunc(st);
  17592. }
  17593. //20120410 or 20090218
  17594. pc_show_questinfo(sd);
  17595. return SCRIPT_CMD_SUCCESS;
  17596. }
  17597. /**
  17598. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17599. **/
  17600. BUILDIN_FUNC(checkquest)
  17601. {
  17602. struct map_session_data *sd;
  17603. e_quest_check_type type = HAVEQUEST;
  17604. if( script_hasdata(st, 3) )
  17605. type = (e_quest_check_type)script_getnum(st, 3);
  17606. if (!script_charid2sd(4,sd))
  17607. return SCRIPT_CMD_FAILURE;
  17608. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17609. return SCRIPT_CMD_SUCCESS;
  17610. }
  17611. /**
  17612. * isbegin_quest(<ID>{,<char_id>})
  17613. **/
  17614. BUILDIN_FUNC(isbegin_quest)
  17615. {
  17616. struct map_session_data *sd;
  17617. if (!script_charid2sd(3,sd))
  17618. return SCRIPT_CMD_FAILURE;
  17619. int i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17620. script_pushint(st, i + (i < 1));
  17621. return SCRIPT_CMD_SUCCESS;
  17622. }
  17623. /**
  17624. * showevent <icon>{,<mark color>{,<char_id>}}
  17625. **/
  17626. BUILDIN_FUNC(showevent)
  17627. {
  17628. TBL_PC *sd;
  17629. if (!script_charid2sd(4,sd))
  17630. return SCRIPT_CMD_FAILURE;
  17631. struct npc_data *nd = map_id2nd(st->oid);
  17632. if (!nd)
  17633. return SCRIPT_CMD_SUCCESS;
  17634. int color = QMARK_NONE;
  17635. int icon = script_getnum(st, 2);
  17636. if (script_hasdata(st, 3)) {
  17637. color = script_getnum(st, 3);
  17638. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17639. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17640. script_reportfunc(st);
  17641. color = QMARK_NONE;
  17642. }
  17643. }
  17644. #if PACKETVER >= 20120410
  17645. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17646. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17647. #else
  17648. if (icon < 0 || icon > 7)
  17649. icon = 0;
  17650. else
  17651. icon = icon + 1;
  17652. #endif
  17653. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17654. return SCRIPT_CMD_SUCCESS;
  17655. }
  17656. /*==========================================
  17657. * BattleGround System
  17658. *------------------------------------------*/
  17659. BUILDIN_FUNC(waitingroom2bg)
  17660. {
  17661. struct npc_data *nd;
  17662. struct chat_data *cd;
  17663. const char *map_name;
  17664. int mapindex = 0, bg_id;
  17665. unsigned char i,c=0;
  17666. struct s_battleground_team team;
  17667. if( script_hasdata(st,7) )
  17668. nd = npc_name2id(script_getstr(st,7));
  17669. else
  17670. nd = (struct npc_data *)map_id2bl(st->oid);
  17671. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  17672. {
  17673. script_pushint(st,0);
  17674. return SCRIPT_CMD_SUCCESS;
  17675. }
  17676. map_name = script_getstr(st,2);
  17677. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17678. { // Invalid Map
  17679. script_pushint(st, 0);
  17680. return SCRIPT_CMD_SUCCESS;
  17681. }
  17682. team.warp_x = script_getnum(st,3);
  17683. team.warp_y = script_getnum(st,4);
  17684. if (script_hasdata(st,5)) {
  17685. team.quit_event = script_getstr(st,5); // Logout Event
  17686. check_event(st, team.quit_event.c_str());
  17687. } else
  17688. team.quit_event = "";
  17689. if (script_hasdata(st,6)) {
  17690. team.death_event = script_getstr(st,6); // Die Event
  17691. check_event(st, team.death_event.c_str());
  17692. } else
  17693. team.death_event = "";
  17694. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  17695. { // Creation failed
  17696. script_pushint(st,0);
  17697. return SCRIPT_CMD_SUCCESS;
  17698. }
  17699. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  17700. struct map_session_data *sd;
  17701. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd, false) ){
  17702. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  17703. ++c;
  17704. }
  17705. }
  17706. mapreg_setreg(add_str("$@arenamembersnum"), c);
  17707. script_pushint(st,bg_id);
  17708. return SCRIPT_CMD_SUCCESS;
  17709. }
  17710. BUILDIN_FUNC(waitingroom2bg_single)
  17711. {
  17712. const char* map_name;
  17713. struct npc_data *nd;
  17714. struct chat_data *cd;
  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. }
  17731. else {
  17732. mapindex = bg->cemetery.map;
  17733. x = bg->cemetery.x;
  17734. y = bg->cemetery.y;
  17735. }
  17736. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17737. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17738. script_pushint(st, false);
  17739. return SCRIPT_CMD_FAILURE;
  17740. }
  17741. nd = npc_name2id(script_getstr(st,6));
  17742. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  17743. return SCRIPT_CMD_SUCCESS;
  17744. if( (sd = cd->usersd[0]) == NULL )
  17745. return SCRIPT_CMD_SUCCESS;
  17746. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17747. {
  17748. script_pushint(st, true);
  17749. }
  17750. else
  17751. script_pushint(st, false);
  17752. return SCRIPT_CMD_SUCCESS;
  17753. }
  17754. /// Creates an instance of battleground battle group.
  17755. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  17756. /// @author [secretdataz]
  17757. BUILDIN_FUNC(bg_create) {
  17758. const char *map_name;
  17759. int mapindex = 0;
  17760. struct s_battleground_team team;
  17761. map_name = script_getstr(st, 2);
  17762. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17763. { // Invalid Map
  17764. script_pushint(st, 0);
  17765. return SCRIPT_CMD_SUCCESS;
  17766. }
  17767. team.warp_x = script_getnum(st,3);
  17768. team.warp_y = script_getnum(st,4);
  17769. if (script_hasdata(st,5)) {
  17770. team.quit_event = script_getstr(st,5); // Logout Event
  17771. check_event(st, team.quit_event.c_str());
  17772. } else
  17773. team.quit_event = "";
  17774. if (script_hasdata(st,6)) {
  17775. team.death_event = script_getstr(st,6); // Die Event
  17776. check_event(st, team.death_event.c_str());
  17777. } else
  17778. team.death_event = "";
  17779. script_pushint(st, bg_create(mapindex, &team));
  17780. return SCRIPT_CMD_SUCCESS;
  17781. }
  17782. /// Adds attached player or <char id> (if specified) to an existing
  17783. /// battleground group and warps it to the specified coordinates on
  17784. /// the given map.
  17785. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  17786. /// @author [secretdataz]
  17787. BUILDIN_FUNC(bg_join) {
  17788. const char* map_name;
  17789. struct map_session_data *sd;
  17790. int x, y, mapindex, bg_id = script_getnum(st, 2);
  17791. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17792. if (!bg) {
  17793. script_pushint(st, false);
  17794. return SCRIPT_CMD_SUCCESS;
  17795. }
  17796. if (script_hasdata(st, 3)) {
  17797. map_name = script_getstr(st, 3);
  17798. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17799. script_pushint(st, false);
  17800. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17801. }
  17802. x = script_getnum(st, 4);
  17803. y = script_getnum(st, 5);
  17804. } else {
  17805. mapindex = bg->cemetery.map;
  17806. x = bg->cemetery.x;
  17807. y = bg->cemetery.y;
  17808. }
  17809. if (!script_charid2sd(6, sd)) {
  17810. script_pushint(st, false);
  17811. return SCRIPT_CMD_FAILURE;
  17812. }
  17813. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17814. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17815. script_pushint(st, false);
  17816. return SCRIPT_CMD_FAILURE;
  17817. }
  17818. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17819. {
  17820. script_pushint(st, true);
  17821. }
  17822. else
  17823. script_pushint(st, false);
  17824. return SCRIPT_CMD_SUCCESS;
  17825. }
  17826. BUILDIN_FUNC(bg_team_setxy)
  17827. {
  17828. int bg_id = script_getnum(st,2);
  17829. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17830. if (bg) {
  17831. bg->cemetery.x = script_getnum(st, 3);
  17832. bg->cemetery.y = script_getnum(st, 4);
  17833. }
  17834. return SCRIPT_CMD_SUCCESS;
  17835. }
  17836. BUILDIN_FUNC(bg_warp)
  17837. {
  17838. int x, y, mapindex, bg_id;
  17839. const char* map_name;
  17840. bg_id = script_getnum(st,2);
  17841. map_name = script_getstr(st,3);
  17842. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  17843. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17844. x = script_getnum(st,4);
  17845. y = script_getnum(st,5);
  17846. bg_team_warp(bg_id, mapindex, x, y);
  17847. return SCRIPT_CMD_SUCCESS;
  17848. }
  17849. BUILDIN_FUNC(bg_monster)
  17850. {
  17851. int class_ = 0, x = 0, y = 0, bg_id = 0;
  17852. const char *str,*mapname, *evt="";
  17853. bg_id = script_getnum(st,2);
  17854. mapname = script_getstr(st,3);
  17855. x = script_getnum(st,4);
  17856. y = script_getnum(st,5);
  17857. str = script_getstr(st,6);
  17858. class_ = script_getnum(st,7);
  17859. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  17860. check_event(st, evt);
  17861. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  17862. return SCRIPT_CMD_SUCCESS;
  17863. }
  17864. BUILDIN_FUNC(bg_monster_set_team)
  17865. {
  17866. struct mob_data *md;
  17867. struct block_list *mbl;
  17868. int id = script_getnum(st,2),
  17869. bg_id = script_getnum(st,3);
  17870. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  17871. return SCRIPT_CMD_SUCCESS;
  17872. md = (TBL_MOB *)mbl;
  17873. md->bg_id = bg_id;
  17874. mob_stop_attack(md);
  17875. mob_stop_walking(md, 0);
  17876. md->target_id = md->attacked_id = 0;
  17877. clif_name_area(&md->bl);
  17878. return SCRIPT_CMD_SUCCESS;
  17879. }
  17880. BUILDIN_FUNC(bg_leave)
  17881. {
  17882. struct map_session_data *sd = NULL;
  17883. bool deserter = false;
  17884. if( !script_charid2sd(2,sd) || !sd->bg_id )
  17885. return SCRIPT_CMD_SUCCESS;
  17886. if (!strcmp(script_getfuncname(st), "bg_desert"))
  17887. deserter = true;
  17888. bg_team_leave(sd, false, deserter);
  17889. return SCRIPT_CMD_SUCCESS;
  17890. }
  17891. BUILDIN_FUNC(bg_destroy)
  17892. {
  17893. int bg_id = script_getnum(st,2);
  17894. bg_team_delete(bg_id);
  17895. return SCRIPT_CMD_SUCCESS;
  17896. }
  17897. BUILDIN_FUNC(bg_getareausers)
  17898. {
  17899. const char *str = script_getstr(st, 3);
  17900. int16 m, x0, y0, x1, y1;
  17901. int bg_id = script_getnum(st, 2), c = 0;
  17902. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17903. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  17904. script_pushint(st,0);
  17905. return SCRIPT_CMD_SUCCESS;
  17906. }
  17907. x0 = script_getnum(st,4);
  17908. y0 = script_getnum(st,5);
  17909. x1 = script_getnum(st,6);
  17910. y1 = script_getnum(st,7);
  17911. for (const auto &member : bg->members) {
  17912. 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 )
  17913. continue;
  17914. c++;
  17915. }
  17916. script_pushint(st,c);
  17917. return SCRIPT_CMD_SUCCESS;
  17918. }
  17919. BUILDIN_FUNC(bg_updatescore)
  17920. {
  17921. const char *str;
  17922. int16 m;
  17923. str = script_getstr(st,2);
  17924. if( (m = map_mapname2mapid(str)) < 0 )
  17925. return SCRIPT_CMD_SUCCESS;
  17926. struct map_data *mapdata = map_getmapdata(m);
  17927. mapdata->bgscore_lion = script_getnum(st,3);
  17928. mapdata->bgscore_eagle = script_getnum(st,4);
  17929. clif_bg_updatescore(m);
  17930. return SCRIPT_CMD_SUCCESS;
  17931. }
  17932. BUILDIN_FUNC(bg_get_data)
  17933. {
  17934. int bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  17935. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17936. if (bg) {
  17937. switch (type) {
  17938. case 0:
  17939. script_pushint(st, bg->members.size());
  17940. break;
  17941. case 1:
  17942. for (const auto &member : bg->members)
  17943. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  17944. mapreg_setreg(add_str("$@arenamemberscount"), i);
  17945. script_pushint(st, i);
  17946. break;
  17947. default:
  17948. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  17949. break;
  17950. }
  17951. } else
  17952. script_pushint(st, 0);
  17953. return SCRIPT_CMD_SUCCESS;
  17954. }
  17955. /**
  17956. * Reserves a slot for the given Battleground.
  17957. * bg_reserve("<battleground_map_name>"{,<ended>});
  17958. */
  17959. BUILDIN_FUNC(bg_reserve)
  17960. {
  17961. const char *str = script_getstr(st, 2);
  17962. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  17963. if (!bg_queue_reserve(str, ended))
  17964. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  17965. return SCRIPT_CMD_SUCCESS;
  17966. }
  17967. /**
  17968. * Removes a spot for the given Battleground.
  17969. * bg_unbook("<battleground_map_name>");
  17970. */
  17971. BUILDIN_FUNC(bg_unbook)
  17972. {
  17973. const char *str = script_getstr(st, 2);
  17974. if (!bg_queue_unbook(str))
  17975. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  17976. return SCRIPT_CMD_SUCCESS;
  17977. }
  17978. /**
  17979. * Gets battleground database information.
  17980. * bg_info("<battleground name>", <type>);
  17981. */
  17982. BUILDIN_FUNC(bg_info)
  17983. {
  17984. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  17985. if (!bg) {
  17986. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  17987. return SCRIPT_CMD_FAILURE;
  17988. }
  17989. int type = script_getnum(st, 3);
  17990. switch (type) {
  17991. case BG_INFO_ID:
  17992. script_pushint(st, bg->id);
  17993. break;
  17994. case BG_INFO_REQUIRED_PLAYERS:
  17995. script_pushint(st, bg->required_players);
  17996. break;
  17997. case BG_INFO_MAX_PLAYERS:
  17998. script_pushint(st, bg->max_players);
  17999. break;
  18000. case BG_INFO_MIN_LEVEL:
  18001. script_pushint(st, bg->min_lvl);
  18002. break;
  18003. case BG_INFO_MAX_LEVEL:
  18004. script_pushint(st, bg->max_lvl);
  18005. break;
  18006. case BG_INFO_MAPS: {
  18007. size_t i;
  18008. for (i = 0; i < bg->maps.size(); i++)
  18009. setd_sub_str(st, nullptr, ".@bgmaps$", i, mapindex_id2name(bg->maps[i].mapindex), nullptr);
  18010. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  18011. script_pushint(st, i);
  18012. break;
  18013. }
  18014. case BG_INFO_DESERTER_TIME:
  18015. script_pushint(st, bg->deserter_time);
  18016. break;
  18017. default:
  18018. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  18019. return SCRIPT_CMD_FAILURE;
  18020. }
  18021. return SCRIPT_CMD_SUCCESS;
  18022. }
  18023. /*==========================================
  18024. * Instancing System
  18025. *------------------------------------------*/
  18026. /**
  18027. * Returns an Instance ID.
  18028. * @param st: Script state
  18029. * @param mode: Instance mode
  18030. * @return instance ID on success or 0 otherwise
  18031. */
  18032. int script_instancegetid(struct script_state* st, e_instance_mode mode)
  18033. {
  18034. int instance_id = 0;
  18035. if (mode == IM_NONE) {
  18036. struct npc_data *nd = map_id2nd(st->oid);
  18037. if (nd->instance_id > 0)
  18038. instance_id = nd->instance_id;
  18039. } else {
  18040. struct map_session_data *sd = map_id2sd(st->rid);
  18041. if (sd) {
  18042. switch (mode) {
  18043. case IM_CHAR:
  18044. if (sd->instance_id > 0)
  18045. instance_id = sd->instance_id;
  18046. break;
  18047. case IM_PARTY: {
  18048. struct party_data *pd = party_search(sd->status.party_id);
  18049. if (pd && pd->instance_id > 0)
  18050. instance_id = pd->instance_id;
  18051. }
  18052. break;
  18053. case IM_GUILD: {
  18054. struct guild *gd = guild_search(sd->status.guild_id);
  18055. if (gd && gd->instance_id > 0)
  18056. instance_id = gd->instance_id;
  18057. }
  18058. break;
  18059. case IM_CLAN: {
  18060. struct clan *cd = clan_search(sd->status.clan_id);
  18061. if (cd && cd->instance_id > 0)
  18062. instance_id = cd->instance_id;
  18063. }
  18064. break;
  18065. default: // Unsupported type
  18066. break;
  18067. }
  18068. }
  18069. }
  18070. return instance_id;
  18071. }
  18072. /*==========================================
  18073. * Creates the instance
  18074. * Returns Instance ID if created successfully
  18075. *------------------------------------------*/
  18076. BUILDIN_FUNC(instance_create)
  18077. {
  18078. e_instance_mode mode = IM_PARTY;
  18079. int owner_id = 0;
  18080. if (script_hasdata(st, 3)) {
  18081. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18082. if (mode < IM_NONE || mode >= IM_MAX) {
  18083. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  18084. return SCRIPT_CMD_FAILURE;
  18085. }
  18086. }
  18087. if (script_hasdata(st, 4))
  18088. owner_id = script_getnum(st, 4);
  18089. else {
  18090. // If sd is NULL, instance_create will return -2.
  18091. struct map_session_data *sd = NULL;
  18092. switch(mode) {
  18093. case IM_NONE:
  18094. owner_id = st->oid;
  18095. break;
  18096. case IM_CHAR:
  18097. if (script_rid2sd(sd))
  18098. owner_id = sd->status.char_id;
  18099. break;
  18100. case IM_PARTY:
  18101. if (script_rid2sd(sd))
  18102. owner_id = sd->status.party_id;
  18103. break;
  18104. case IM_GUILD:
  18105. if (script_rid2sd(sd))
  18106. owner_id = sd->status.guild_id;
  18107. break;
  18108. case IM_CLAN:
  18109. if (script_rid2sd(sd))
  18110. owner_id = sd->status.clan_id;
  18111. break;
  18112. default:
  18113. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  18114. return SCRIPT_CMD_FAILURE;
  18115. }
  18116. }
  18117. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  18118. return SCRIPT_CMD_SUCCESS;
  18119. }
  18120. /*==========================================
  18121. * Destroys an instance (unofficial)
  18122. * Officially instances are only destroyed by timeout
  18123. *
  18124. * instance_destroy {<instance_id>};
  18125. *------------------------------------------*/
  18126. BUILDIN_FUNC(instance_destroy)
  18127. {
  18128. int instance_id;
  18129. if( script_hasdata(st,2) )
  18130. instance_id = script_getnum(st,2);
  18131. else
  18132. instance_id = script_instancegetid(st);
  18133. if( instance_id == 0 ) {
  18134. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  18135. return SCRIPT_CMD_FAILURE;
  18136. }
  18137. instance_destroy(instance_id);
  18138. return SCRIPT_CMD_SUCCESS;
  18139. }
  18140. /*==========================================
  18141. * Warps player to instance
  18142. * Results:
  18143. * IE_OK: Success
  18144. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  18145. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  18146. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  18147. *------------------------------------------*/
  18148. BUILDIN_FUNC(instance_enter)
  18149. {
  18150. struct map_session_data *sd = NULL;
  18151. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  18152. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  18153. int instance_id;
  18154. if (script_hasdata(st, 6))
  18155. instance_id = script_getnum(st, 6);
  18156. else
  18157. instance_id = script_instancegetid(st, IM_PARTY);
  18158. if (!script_charid2sd(5,sd))
  18159. return SCRIPT_CMD_FAILURE;
  18160. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  18161. return SCRIPT_CMD_SUCCESS;
  18162. }
  18163. /*==========================================
  18164. * Returns the name of a duplicated NPC
  18165. *
  18166. * instance_npcname <npc_name>{,<instance_id>};
  18167. * <npc_name> is the full name of an NPC.
  18168. *------------------------------------------*/
  18169. BUILDIN_FUNC(instance_npcname)
  18170. {
  18171. const char *str;
  18172. int instance_id;
  18173. struct npc_data *nd;
  18174. str = script_getstr(st,2);
  18175. if( script_hasdata(st,3) )
  18176. instance_id = script_getnum(st,3);
  18177. else
  18178. instance_id = script_instancegetid(st);
  18179. if( instance_id > 0 && (nd = npc_name2id(str)) != NULL ) {
  18180. static char npcname[NAME_LENGTH];
  18181. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  18182. script_pushconststr(st,npcname);
  18183. } else {
  18184. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  18185. st->state = END;
  18186. return SCRIPT_CMD_FAILURE;
  18187. }
  18188. return SCRIPT_CMD_SUCCESS;
  18189. }
  18190. /*==========================================
  18191. * Returns the name of a duplicated map
  18192. *
  18193. * instance_mapname <map_name>{,<instance_id>};
  18194. *------------------------------------------*/
  18195. BUILDIN_FUNC(instance_mapname)
  18196. {
  18197. const char *str;
  18198. int16 m;
  18199. int instance_id;
  18200. str = script_getstr(st,2);
  18201. if( script_hasdata(st,3) )
  18202. instance_id = script_getnum(st,3);
  18203. else
  18204. instance_id = script_instancegetid(st);
  18205. // Check that instance mapname is a valid map
  18206. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  18207. script_pushconststr(st, "");
  18208. else
  18209. script_pushconststr(st, map_getmapdata(m)->name);
  18210. return SCRIPT_CMD_SUCCESS;
  18211. }
  18212. /*==========================================
  18213. * Returns an Instance ID
  18214. *------------------------------------------*/
  18215. BUILDIN_FUNC(instance_id)
  18216. {
  18217. int mode = IM_NONE; // Default to the attached NPC
  18218. if (script_hasdata(st, 2)) {
  18219. mode = script_getnum(st, 2);
  18220. if (mode <= IM_NONE || mode >= IM_MAX) {
  18221. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  18222. script_pushint(st, 0);
  18223. return SCRIPT_CMD_SUCCESS;
  18224. }
  18225. }
  18226. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  18227. return SCRIPT_CMD_SUCCESS;
  18228. }
  18229. /*==========================================
  18230. * Warps all players inside an instance
  18231. *
  18232. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  18233. *------------------------------------------*/
  18234. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  18235. {
  18236. unsigned int m = va_arg(ap,unsigned int);
  18237. int x = va_arg(ap,int);
  18238. int y = va_arg(ap,int);
  18239. int instance_id = va_arg(ap, int);
  18240. struct map_session_data *sd;
  18241. nullpo_retr(0, bl);
  18242. if (bl->type != BL_PC)
  18243. return 0;
  18244. sd = (TBL_PC *)bl;
  18245. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18246. if (!idata)
  18247. return 0;
  18248. int owner_id = idata->owner_id;
  18249. switch(idata->mode) {
  18250. case IM_NONE:
  18251. break;
  18252. case IM_CHAR:
  18253. if (sd->status.char_id != owner_id)
  18254. return 0;
  18255. break;
  18256. case IM_PARTY:
  18257. if (sd->status.party_id != owner_id)
  18258. return 0;
  18259. break;
  18260. case IM_GUILD:
  18261. if (sd->status.guild_id != owner_id)
  18262. return 0;
  18263. case IM_CLAN:
  18264. if (sd->status.clan_id != owner_id)
  18265. return 0;
  18266. }
  18267. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  18268. return 1;
  18269. }
  18270. BUILDIN_FUNC(instance_warpall)
  18271. {
  18272. int16 m;
  18273. int instance_id;
  18274. const char *mapn;
  18275. int x, y;
  18276. mapn = script_getstr(st,2);
  18277. x = script_getnum(st,3);
  18278. y = script_getnum(st,4);
  18279. if( script_hasdata(st,5) )
  18280. instance_id = script_getnum(st,5);
  18281. else
  18282. instance_id = script_instancegetid(st, IM_PARTY);
  18283. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0)
  18284. return SCRIPT_CMD_FAILURE;
  18285. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18286. if (!idata) {
  18287. ShowError("buildin_instance_warpall: Instance is not found.\n");
  18288. return SCRIPT_CMD_FAILURE;
  18289. }
  18290. for(const auto &it : idata->map)
  18291. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id);
  18292. return SCRIPT_CMD_SUCCESS;
  18293. }
  18294. /*==========================================
  18295. * Broadcasts to all maps inside an instance
  18296. *
  18297. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  18298. * Using 0 for <instance id> will auto-detect the id.
  18299. *------------------------------------------*/
  18300. BUILDIN_FUNC(instance_announce) {
  18301. int instance_id = script_getnum(st,2);
  18302. const char *mes = script_getstr(st,3);
  18303. int flag = script_getnum(st,4);
  18304. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  18305. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  18306. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  18307. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  18308. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  18309. if (instance_id <= 0)
  18310. instance_id = script_instancegetid(st);
  18311. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  18312. if (instance_id <= 0 || !idata) {
  18313. ShowError("buildin_instance_announce: Instance not found.\n");
  18314. return SCRIPT_CMD_FAILURE;
  18315. }
  18316. for (const auto &it : idata->map)
  18317. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  18318. return SCRIPT_CMD_SUCCESS;
  18319. }
  18320. /*==========================================
  18321. * instance_check_party [malufett]
  18322. * Values:
  18323. * party_id : Party ID of the invoking character. [Required Parameter]
  18324. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  18325. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18326. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18327. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  18328. * Example 2: instance_check_party (getcharid(1),1,1,99);
  18329. *------------------------------------------*/
  18330. BUILDIN_FUNC(instance_check_party)
  18331. {
  18332. int amount, min, max, i, party_id, c = 0;
  18333. struct party_data *p;
  18334. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  18335. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18336. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18337. if( min < 1 || min > MAX_LEVEL) {
  18338. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  18339. return SCRIPT_CMD_FAILURE;
  18340. } else if( max < 1 || max > MAX_LEVEL) {
  18341. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  18342. return SCRIPT_CMD_FAILURE;
  18343. }
  18344. if( script_hasdata(st,2) )
  18345. party_id = script_getnum(st,2);
  18346. else return SCRIPT_CMD_FAILURE;
  18347. if( !(p = party_search(party_id)) ) {
  18348. script_pushint(st, 0); // Returns false if party does not exist.
  18349. return SCRIPT_CMD_FAILURE;
  18350. }
  18351. for( i = 0; i < MAX_PARTY; i++ ) {
  18352. struct map_session_data *pl_sd;
  18353. if( (pl_sd = p->data[i].sd) )
  18354. if(map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18355. if(pl_sd->status.base_level < min) {
  18356. script_pushint(st, 0);
  18357. return SCRIPT_CMD_SUCCESS;
  18358. } else if(pl_sd->status.base_level > max) {
  18359. script_pushint(st, 0);
  18360. return SCRIPT_CMD_SUCCESS;
  18361. }
  18362. c++;
  18363. }
  18364. }
  18365. if(c < amount)
  18366. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  18367. else
  18368. script_pushint(st, 1);
  18369. return SCRIPT_CMD_SUCCESS;
  18370. }
  18371. /*==========================================
  18372. * instance_check_guild
  18373. * Values:
  18374. * guild_id : Guild ID of the invoking character. [Required Parameter]
  18375. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  18376. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18377. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18378. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  18379. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  18380. *------------------------------------------*/
  18381. BUILDIN_FUNC(instance_check_guild)
  18382. {
  18383. int amount, min, max, i, guild_id = 0, c = 0;
  18384. struct guild *g = NULL;
  18385. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18386. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18387. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18388. if (min < 1 || min > MAX_LEVEL) {
  18389. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18390. return SCRIPT_CMD_FAILURE;
  18391. } else if (max < 1 || max > MAX_LEVEL) {
  18392. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18393. return SCRIPT_CMD_FAILURE;
  18394. }
  18395. if (script_hasdata(st,2))
  18396. guild_id = script_getnum(st,2);
  18397. else
  18398. return SCRIPT_CMD_FAILURE;
  18399. if (!(g = guild_search(guild_id))) {
  18400. script_pushint(st, 0); // Returns false if guild does not exist.
  18401. return SCRIPT_CMD_FAILURE;
  18402. }
  18403. for(i = 0; i < MAX_GUILD; i++) {
  18404. struct map_session_data *pl_sd;
  18405. if ((pl_sd = g->member[i].sd)) {
  18406. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18407. if (pl_sd->status.base_level < min) {
  18408. script_pushint(st, 0);
  18409. return SCRIPT_CMD_SUCCESS;
  18410. } else if (pl_sd->status.base_level > max) {
  18411. script_pushint(st, 0);
  18412. return SCRIPT_CMD_SUCCESS;
  18413. }
  18414. c++;
  18415. }
  18416. }
  18417. }
  18418. if (c < amount)
  18419. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18420. else
  18421. script_pushint(st, 1);
  18422. return SCRIPT_CMD_SUCCESS;
  18423. }
  18424. /*==========================================
  18425. * instance_check_clan
  18426. * Values:
  18427. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18428. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18429. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18430. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18431. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18432. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18433. *------------------------------------------*/
  18434. BUILDIN_FUNC(instance_check_clan)
  18435. {
  18436. int amount, min, max, i, clan_id = 0, c = 0;
  18437. struct clan *cd = NULL;
  18438. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18439. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18440. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18441. if (min < 1 || min > MAX_LEVEL) {
  18442. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18443. return SCRIPT_CMD_FAILURE;
  18444. } else if (max < 1 || max > MAX_LEVEL) {
  18445. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18446. return SCRIPT_CMD_FAILURE;
  18447. }
  18448. if (script_hasdata(st,2))
  18449. clan_id = script_getnum(st,2);
  18450. else
  18451. return SCRIPT_CMD_FAILURE;
  18452. if (!(cd = clan_search(clan_id))) {
  18453. script_pushint(st, 0); // Returns false if clan does not exist.
  18454. return SCRIPT_CMD_FAILURE;
  18455. }
  18456. for(i = 0; i < MAX_CLAN; i++) {
  18457. struct map_session_data *pl_sd;
  18458. if ((pl_sd = cd->members[i])) {
  18459. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18460. if (pl_sd->status.base_level < min) {
  18461. script_pushint(st, 0);
  18462. return SCRIPT_CMD_SUCCESS;
  18463. } else if (pl_sd->status.base_level > max) {
  18464. script_pushint(st, 0);
  18465. return SCRIPT_CMD_SUCCESS;
  18466. }
  18467. c++;
  18468. }
  18469. }
  18470. }
  18471. if (c < amount)
  18472. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18473. else
  18474. script_pushint(st, 1);
  18475. return SCRIPT_CMD_SUCCESS;
  18476. }
  18477. /*==========================================
  18478. * instance_info
  18479. * Values:
  18480. * name : name of the instance you want to look up. [Required Parameter]
  18481. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18482. * index : index of the map in the instance. [Optional Parameter]
  18483. *------------------------------------------*/
  18484. BUILDIN_FUNC(instance_info)
  18485. {
  18486. const char* name = script_getstr(st, 2);
  18487. int type = script_getnum(st, 3);
  18488. int index = 0;
  18489. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18490. if (!db) {
  18491. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18492. script_pushint(st, -1);
  18493. return SCRIPT_CMD_FAILURE;
  18494. }
  18495. switch( type ){
  18496. case IIT_ID:
  18497. script_pushint(st, db->id);
  18498. break;
  18499. case IIT_TIME_LIMIT:
  18500. script_pushint(st, db->limit);
  18501. break;
  18502. case IIT_IDLE_TIMEOUT:
  18503. script_pushint(st, db->timeout);
  18504. break;
  18505. case IIT_ENTER_MAP:
  18506. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18507. break;
  18508. case IIT_ENTER_X:
  18509. script_pushint(st, db->enter.x);
  18510. break;
  18511. case IIT_ENTER_Y:
  18512. script_pushint(st, db->enter.y);
  18513. break;
  18514. case IIT_MAPCOUNT:
  18515. script_pushint(st, db->maplist.size());
  18516. break;
  18517. case IIT_MAP:
  18518. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18519. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18520. script_pushconststr(st, "");
  18521. return SCRIPT_CMD_FAILURE;
  18522. }
  18523. index = script_getnum(st, 4);
  18524. if( index < 0 ){
  18525. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18526. script_pushconststr(st, "");
  18527. return SCRIPT_CMD_FAILURE;
  18528. }
  18529. if( index > UINT8_MAX ){
  18530. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18531. script_pushconststr(st, "");
  18532. return SCRIPT_CMD_FAILURE;
  18533. }
  18534. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18535. break;
  18536. default:
  18537. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18538. script_pushint(st, -1);
  18539. return SCRIPT_CMD_FAILURE;
  18540. }
  18541. return SCRIPT_CMD_SUCCESS;
  18542. }
  18543. /*------------------------------------------
  18544. *instance_live_info( <info type>{, <instance id>} );
  18545. - ILI_NAME : Instance Name
  18546. - ILI_MODE : Instance Mode
  18547. - ILI_OWNER : owner id
  18548. *------------------------------------------*/
  18549. BUILDIN_FUNC(instance_live_info)
  18550. {
  18551. int type = script_getnum(st, 2);
  18552. int id = 0;
  18553. if (type < ILI_NAME || type > ILI_OWNER) {
  18554. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18555. script_pushint(st, -1);
  18556. return SCRIPT_CMD_FAILURE;
  18557. }
  18558. if (!script_hasdata(st, 3))
  18559. id = script_instancegetid(st);
  18560. else
  18561. id = script_getnum(st, 3);
  18562. std::shared_ptr<s_instance_db> db = nullptr;
  18563. std::shared_ptr<s_instance_data> im = nullptr;
  18564. if (id > 0 && id < INT_MAX) {
  18565. im = util::umap_find(instances, id);
  18566. if (im)
  18567. db = instance_db.find(im->id);
  18568. }
  18569. if (!im || !db) {
  18570. if (type == ILI_NAME)
  18571. script_pushconststr(st, "");
  18572. else
  18573. script_pushint(st, -1);
  18574. return SCRIPT_CMD_SUCCESS;
  18575. }
  18576. switch( type ) {
  18577. case ILI_NAME:
  18578. script_pushstrcopy(st, db->name.c_str());
  18579. break;
  18580. case ILI_MODE:
  18581. script_pushint(st, im->mode);
  18582. break;
  18583. case ILI_OWNER:
  18584. script_pushint(st, im->owner_id);
  18585. break;
  18586. }
  18587. return SCRIPT_CMD_SUCCESS;
  18588. }
  18589. /*==========================================
  18590. * instance_list(<"map name">{,<instance mode>});
  18591. * set '.@instance_list' to a list of the live instance ids for the map with the mode.
  18592. * return the array size of '.@instance_list'
  18593. *------------------------------------------*/
  18594. BUILDIN_FUNC(instance_list)
  18595. {
  18596. int src_id = map_mapname2mapid(script_getstr(st, 2));
  18597. if (src_id == 0) {
  18598. ShowError("buildin_instance_list: map '%s' doesn't exist\n", script_getstr(st, 2));
  18599. return SCRIPT_CMD_FAILURE;
  18600. }
  18601. e_instance_mode mode = IM_MAX;
  18602. if (script_hasdata(st, 3)) {
  18603. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  18604. if (mode < IM_NONE || mode >= IM_MAX) {
  18605. ShowError("buildin_instance_list: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 3));
  18606. return SCRIPT_CMD_FAILURE;
  18607. }
  18608. }
  18609. int j = 0;
  18610. for (int i = instance_start; i < map_num; i++) {
  18611. struct map_data* mapdata = &map[i];
  18612. if (mapdata->instance_src_map == src_id) {
  18613. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, mapdata->instance_id);
  18614. if (idata && (mode == IM_MAX || idata->mode == mode)) {
  18615. setd_sub_num(st, nullptr, ".@instance_list", j, mapdata->instance_id, nullptr);
  18616. j++;
  18617. }
  18618. }
  18619. }
  18620. script_pushint(st, j);
  18621. return SCRIPT_CMD_SUCCESS;
  18622. }
  18623. /*==========================================
  18624. * Custom Fonts
  18625. *------------------------------------------*/
  18626. BUILDIN_FUNC(setfont)
  18627. {
  18628. struct map_session_data *sd;
  18629. int font;
  18630. if( !script_rid2sd(sd) )
  18631. return SCRIPT_CMD_SUCCESS;
  18632. font = script_getnum(st,2);
  18633. if( sd->status.font != font )
  18634. sd->status.font = font;
  18635. else
  18636. sd->status.font = 0;
  18637. clif_font(sd);
  18638. return SCRIPT_CMD_SUCCESS;
  18639. }
  18640. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  18641. {
  18642. TBL_MOB* md = (TBL_MOB*)bl;
  18643. struct block_list *tbl;
  18644. int mobid = va_arg(ap,int);
  18645. uint16 skill_id = va_arg(ap,int);
  18646. uint16 skill_lv = va_arg(ap,int);
  18647. int casttime = va_arg(ap,int);
  18648. int cancel = va_arg(ap,int);
  18649. int emotion = va_arg(ap,int);
  18650. int target = va_arg(ap,int);
  18651. if( md->mob_id != mobid )
  18652. return 0;
  18653. // 0:self, 1:target, 2:master, default:random
  18654. switch( target ) {
  18655. case 0: tbl = map_id2bl(md->bl.id); break;
  18656. case 1: tbl = map_id2bl(md->target_id); break;
  18657. case 2: tbl = map_id2bl(md->master_id); break;
  18658. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  18659. }
  18660. if( !tbl )
  18661. return 0;
  18662. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  18663. unit_skillcastcancel(bl,0);
  18664. if( skill_get_casttype(skill_id) == CAST_GROUND )
  18665. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  18666. else
  18667. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  18668. clif_emotion(&md->bl, emotion);
  18669. return 1;
  18670. }
  18671. /*==========================================
  18672. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  18673. *------------------------------------------*/
  18674. BUILDIN_FUNC(areamobuseskill)
  18675. {
  18676. struct block_list center;
  18677. int16 m;
  18678. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  18679. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  18680. ShowError("areamobuseskill: invalid map name.\n");
  18681. return SCRIPT_CMD_SUCCESS;
  18682. }
  18683. center.m = m;
  18684. center.x = script_getnum(st,3);
  18685. center.y = script_getnum(st,4);
  18686. range = script_getnum(st,5);
  18687. mobid = script_getnum(st,6);
  18688. if (script_isstring(st, 7)) {
  18689. const char *name = script_getstr(st, 7);
  18690. if (!(skill_id = skill_name2id(name))) {
  18691. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  18692. return SCRIPT_CMD_FAILURE;
  18693. }
  18694. } else {
  18695. skill_id = script_getnum(st, 7);
  18696. if (!skill_get_index(skill_id)) {
  18697. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  18698. return SCRIPT_CMD_FAILURE;
  18699. }
  18700. }
  18701. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  18702. skill_lv = battle_config.mob_max_skilllvl;
  18703. casttime = script_getnum(st,9);
  18704. cancel = script_getnum(st,10);
  18705. emotion = script_getnum(st,11);
  18706. target = script_getnum(st,12);
  18707. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  18708. return SCRIPT_CMD_SUCCESS;
  18709. }
  18710. /**
  18711. * Display a progress bar above a character
  18712. * progressbar "<color>",<seconds>;
  18713. */
  18714. BUILDIN_FUNC(progressbar)
  18715. {
  18716. struct map_session_data * sd;
  18717. const char * color;
  18718. unsigned int second;
  18719. if( !script_rid2sd(sd) )
  18720. return SCRIPT_CMD_SUCCESS;
  18721. st->state = STOP;
  18722. color = script_getstr(st,2);
  18723. second = script_getnum(st,3);
  18724. sd->progressbar.npc_id = st->oid;
  18725. sd->progressbar.timeout = gettick() + second*1000;
  18726. sd->state.workinprogress = WIP_DISABLE_ALL;
  18727. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  18728. return SCRIPT_CMD_SUCCESS;
  18729. }
  18730. /**
  18731. * Display a progress bar above an NPC
  18732. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  18733. */
  18734. BUILDIN_FUNC(progressbar_npc){
  18735. map_session_data *sd = map_id2sd(st->rid);
  18736. struct npc_data* nd = NULL;
  18737. if( script_hasdata(st, 4) ){
  18738. const char* name = script_getstr(st, 4);
  18739. nd = npc_name2id(name);
  18740. if( !nd ){
  18741. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  18742. return SCRIPT_CMD_FAILURE;
  18743. }
  18744. }else{
  18745. nd = map_id2nd(st->oid);
  18746. }
  18747. // First call(by function call)
  18748. if( !nd->progressbar.timeout ){
  18749. const char *color;
  18750. int second;
  18751. color = script_getstr(st, 2);
  18752. second = script_getnum(st, 3);
  18753. if( second < 0 ){
  18754. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  18755. return SCRIPT_CMD_FAILURE;
  18756. }
  18757. if (sd) { // Player attached - keep them from doing other things
  18758. sd->state.workinprogress = WIP_DISABLE_ALL;
  18759. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18760. }
  18761. // sleep for the target amount of time
  18762. st->state = RERUNLINE;
  18763. st->sleep.tick = second * 1000;
  18764. nd->progressbar.timeout = gettick() + second * 1000;
  18765. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  18766. clif_progressbar_npc_area(nd);
  18767. // Second call(by timer after sleeping time is over)
  18768. } else {
  18769. // Continue the script
  18770. if (sd) { // Player attached - remove restrictions
  18771. sd->state.workinprogress = WIP_DISABLE_NONE;
  18772. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18773. }
  18774. st->state = RUN;
  18775. st->sleep.tick = 0;
  18776. nd->progressbar.timeout = nd->progressbar.color = 0;
  18777. }
  18778. return SCRIPT_CMD_SUCCESS;
  18779. }
  18780. BUILDIN_FUNC(pushpc)
  18781. {
  18782. uint8 dir;
  18783. int cells, dx, dy;
  18784. struct map_session_data* sd;
  18785. if(!script_rid2sd(sd))
  18786. {
  18787. return SCRIPT_CMD_SUCCESS;
  18788. }
  18789. dir = script_getnum(st,2);
  18790. cells = script_getnum(st,3);
  18791. if(dir >= DIR_MAX)
  18792. {
  18793. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  18794. script_reportsrc(st);
  18795. dir%= DIR_MAX; // trim spin-over
  18796. }
  18797. if(!cells)
  18798. {// zero distance
  18799. return SCRIPT_CMD_SUCCESS;
  18800. }
  18801. else if(cells<0)
  18802. {// pushing backwards
  18803. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  18804. cells = -cells;
  18805. }
  18806. dx = dirx[dir];
  18807. dy = diry[dir];
  18808. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  18809. return SCRIPT_CMD_SUCCESS;
  18810. }
  18811. /// Invokes buying store preparation window
  18812. /// buyingstore <slots>;
  18813. BUILDIN_FUNC(buyingstore)
  18814. {
  18815. struct map_session_data* sd;
  18816. if( !script_rid2sd(sd) )
  18817. {
  18818. return SCRIPT_CMD_SUCCESS;
  18819. }
  18820. if( npc_isnear(&sd->bl) ) {
  18821. char output[150];
  18822. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  18823. clif_displaymessage(sd->fd, output);
  18824. return SCRIPT_CMD_SUCCESS;
  18825. }
  18826. buyingstore_setup(sd, script_getnum(st,2));
  18827. return SCRIPT_CMD_SUCCESS;
  18828. }
  18829. /// Invokes search store info window
  18830. /// searchstores <uses>,<effect>;
  18831. BUILDIN_FUNC(searchstores)
  18832. {
  18833. unsigned short effect;
  18834. unsigned int uses;
  18835. struct map_session_data* sd;
  18836. if( !script_rid2sd(sd) )
  18837. {
  18838. return SCRIPT_CMD_SUCCESS;
  18839. }
  18840. uses = script_getnum(st,2);
  18841. effect = script_getnum(st,3);
  18842. if( !uses )
  18843. {
  18844. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  18845. return SCRIPT_CMD_FAILURE;
  18846. }
  18847. if( effect > 1 )
  18848. {
  18849. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  18850. return SCRIPT_CMD_FAILURE;
  18851. }
  18852. searchstore_open(sd, uses, effect);
  18853. return SCRIPT_CMD_SUCCESS;
  18854. }
  18855. /// Displays a number as large digital clock.
  18856. /// showdigit <value>[,<type>];
  18857. BUILDIN_FUNC(showdigit)
  18858. {
  18859. unsigned int type = 0;
  18860. int value;
  18861. struct map_session_data* sd;
  18862. if( !script_rid2sd(sd) )
  18863. {
  18864. return SCRIPT_CMD_SUCCESS;
  18865. }
  18866. value = script_getnum(st,2);
  18867. if( script_hasdata(st,3) ){
  18868. type = script_getnum(st,3);
  18869. }
  18870. switch( type ){
  18871. case 0:
  18872. break;
  18873. case 1:
  18874. case 2:
  18875. // Use absolute value and then the negative value of it as starting value
  18876. // This is what gravity's client does for these counters
  18877. value = -abs(value);
  18878. break;
  18879. case 3:
  18880. value = abs(value);
  18881. if( value > 99 ){
  18882. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  18883. script_reportsrc(st);
  18884. value = 99;
  18885. }
  18886. break;
  18887. default:
  18888. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  18889. return SCRIPT_CMD_FAILURE;
  18890. }
  18891. clif_showdigit(sd, (unsigned char)type, value);
  18892. return SCRIPT_CMD_SUCCESS;
  18893. }
  18894. /**
  18895. * Rune Knight
  18896. * makerune <% success bonus>{,<char_id>};
  18897. **/
  18898. BUILDIN_FUNC(makerune) {
  18899. TBL_PC* sd;
  18900. if (!script_charid2sd(3,sd))
  18901. return SCRIPT_CMD_FAILURE;
  18902. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  18903. sd->itemid = script_getnum(st,2);
  18904. return SCRIPT_CMD_SUCCESS;
  18905. }
  18906. /**
  18907. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  18908. **/
  18909. BUILDIN_FUNC(checkdragon) {
  18910. TBL_PC* sd;
  18911. if (!script_charid2sd(2,sd))
  18912. return SCRIPT_CMD_FAILURE;
  18913. if( pc_isridingdragon(sd) )
  18914. script_pushint(st,1);
  18915. else
  18916. script_pushint(st,0);
  18917. return SCRIPT_CMD_SUCCESS;
  18918. }
  18919. /**
  18920. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  18921. * - Toggles the dragon on a RK if he can mount;
  18922. * @param Color - when not provided uses the green dragon;
  18923. * - 1 : Green Dragon
  18924. * - 2 : Brown Dragon
  18925. * - 3 : Gray Dragon
  18926. * - 4 : Blue Dragon
  18927. * - 5 : Red Dragon
  18928. **/
  18929. BUILDIN_FUNC(setdragon) {
  18930. TBL_PC* sd;
  18931. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  18932. if (!script_charid2sd(3,sd))
  18933. return SCRIPT_CMD_FAILURE;
  18934. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  18935. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  18936. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  18937. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  18938. script_pushint(st,1);
  18939. } else {//Not mounted; Mount now.
  18940. unsigned int option = OPTION_DRAGON1;
  18941. if( color ) {
  18942. option = ( color == 1 ? OPTION_DRAGON1 :
  18943. color == 2 ? OPTION_DRAGON2 :
  18944. color == 3 ? OPTION_DRAGON3 :
  18945. color == 4 ? OPTION_DRAGON4 :
  18946. color == 5 ? OPTION_DRAGON5 : 0);
  18947. if( !option ) {
  18948. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  18949. option = OPTION_DRAGON1;
  18950. }
  18951. }
  18952. pc_setoption(sd, sd->sc.option|option);
  18953. script_pushint(st,1);
  18954. }
  18955. return SCRIPT_CMD_SUCCESS;
  18956. }
  18957. /**
  18958. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  18959. **/
  18960. BUILDIN_FUNC(ismounting) {
  18961. TBL_PC* sd;
  18962. if (!script_charid2sd(2,sd))
  18963. return SCRIPT_CMD_FAILURE;
  18964. if( sd->sc.data[SC_ALL_RIDING] )
  18965. script_pushint(st,1);
  18966. else
  18967. script_pushint(st,0);
  18968. return SCRIPT_CMD_SUCCESS;
  18969. }
  18970. /**
  18971. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  18972. * - Toggles new mounts on a player when he can mount
  18973. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  18974. * - Will unmount the player is he is already mounting
  18975. **/
  18976. BUILDIN_FUNC(setmounting) {
  18977. TBL_PC* sd;
  18978. if (!script_charid2sd(2,sd))
  18979. return SCRIPT_CMD_FAILURE;
  18980. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  18981. clif_msg(sd, NEED_REINS_OF_MOUNT);
  18982. script_pushint(st,0); //can't mount with one of these
  18983. } 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]) {
  18984. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  18985. script_pushint(st, 0); // Silent failure
  18986. } else {
  18987. if( sd->sc.data[SC_ALL_RIDING] )
  18988. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  18989. else
  18990. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  18991. script_pushint(st,1);//in both cases, return 1.
  18992. }
  18993. return SCRIPT_CMD_SUCCESS;
  18994. }
  18995. /**
  18996. * Retrieves quantity of arguments provided to callfunc/callsub.
  18997. * getargcount() -> amount of arguments received in a function
  18998. **/
  18999. BUILDIN_FUNC(getargcount) {
  19000. struct script_retinfo* ri;
  19001. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  19002. ShowError("script:getargcount: used out of function or callsub label!\n");
  19003. st->state = END;
  19004. return SCRIPT_CMD_FAILURE;
  19005. }
  19006. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  19007. script_pushint(st, ri->nargs);
  19008. return SCRIPT_CMD_SUCCESS;
  19009. }
  19010. /**
  19011. * getcharip(<account ID>/<character ID>/<character name>)
  19012. **/
  19013. BUILDIN_FUNC(getcharip)
  19014. {
  19015. struct map_session_data* sd = NULL;
  19016. /* check if a character name is specified */
  19017. if( script_hasdata(st, 2) )
  19018. {
  19019. if (script_isstring(st, 2))
  19020. sd = map_nick2sd(script_getstr(st, 2),false);
  19021. else
  19022. {
  19023. int id = script_getnum(st, 2);
  19024. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  19025. }
  19026. }
  19027. else
  19028. script_rid2sd(sd);
  19029. /* check for sd and IP */
  19030. if (!sd || !session[sd->fd]->client_addr)
  19031. {
  19032. script_pushconststr(st, "");
  19033. return SCRIPT_CMD_SUCCESS;
  19034. }
  19035. /* return the client ip_addr converted for output */
  19036. if (sd && sd->fd && session[sd->fd])
  19037. {
  19038. /* initiliaze */
  19039. const char *ip_addr = NULL;
  19040. uint32 ip;
  19041. /* set ip, ip_addr and convert to ip and push str */
  19042. ip = session[sd->fd]->client_addr;
  19043. ip_addr = ip2str(ip, NULL);
  19044. script_pushstrcopy(st, ip_addr);
  19045. }
  19046. return SCRIPT_CMD_SUCCESS;
  19047. }
  19048. /**
  19049. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  19050. **/
  19051. BUILDIN_FUNC(is_function) {
  19052. const char* str = script_getstr(st,2);
  19053. if( strdb_exists(userfunc_db, str) )
  19054. script_pushint(st,1);
  19055. else
  19056. script_pushint(st,0);
  19057. return SCRIPT_CMD_SUCCESS;
  19058. }
  19059. /**
  19060. * get_revision() -> retrieves the current svn revision (if available)
  19061. **/
  19062. BUILDIN_FUNC(get_revision) {
  19063. const char *svn = get_svn_revision();
  19064. if ( svn[0] != UNKNOWN_VERSION )
  19065. script_pushint(st,atoi(svn));
  19066. else
  19067. script_pushint(st,-1); //unknown
  19068. return SCRIPT_CMD_SUCCESS;
  19069. }
  19070. /* get_hash() -> retrieves the current git hash (if available)*/
  19071. BUILDIN_FUNC(get_githash) {
  19072. const char* git = get_git_hash();
  19073. char buf[CHAT_SIZE_MAX];
  19074. safestrncpy(buf,git,strlen(git)+1);
  19075. if ( git[0] != UNKNOWN_VERSION )
  19076. script_pushstrcopy(st,buf);
  19077. else
  19078. script_pushconststr(st,"Unknown"); //unknown
  19079. return SCRIPT_CMD_SUCCESS;
  19080. }
  19081. /**
  19082. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  19083. **/
  19084. BUILDIN_FUNC(freeloop) {
  19085. if( script_hasdata(st,2) ) {
  19086. if( script_getnum(st,2) )
  19087. st->freeloop = 1;
  19088. else
  19089. st->freeloop = 0;
  19090. }
  19091. script_pushint(st, st->freeloop);
  19092. return SCRIPT_CMD_SUCCESS;
  19093. }
  19094. /**
  19095. * @commands (script based)
  19096. **/
  19097. BUILDIN_FUNC(bindatcmd) {
  19098. const char* atcmd;
  19099. const char* eventName;
  19100. int i, level = 0, level2 = 100;
  19101. bool create = false;
  19102. atcmd = script_getstr(st,2);
  19103. eventName = script_getstr(st,3);
  19104. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19105. atcmd++;
  19106. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  19107. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  19108. if( atcmd_binding_count == 0 ) {
  19109. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  19110. create = true;
  19111. } else {
  19112. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  19113. if( i < atcmd_binding_count ) {/* update existent entry */
  19114. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19115. atcmd_binding[i]->level = level;
  19116. atcmd_binding[i]->level2 = level2;
  19117. } else
  19118. create = true;
  19119. }
  19120. if( create ) {
  19121. i = atcmd_binding_count;
  19122. if( atcmd_binding_count++ != 0 )
  19123. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  19124. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  19125. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  19126. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  19127. atcmd_binding[i]->level = level;
  19128. atcmd_binding[i]->level2 = level2;
  19129. }
  19130. return SCRIPT_CMD_SUCCESS;
  19131. }
  19132. BUILDIN_FUNC(unbindatcmd) {
  19133. const char* atcmd;
  19134. int i = 0;
  19135. atcmd = script_getstr(st, 2);
  19136. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  19137. atcmd++;
  19138. if( atcmd_binding_count == 0 ) {
  19139. script_pushint(st, 0);
  19140. return SCRIPT_CMD_SUCCESS;
  19141. }
  19142. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  19143. if( i < atcmd_binding_count ) {
  19144. int cursor = 0;
  19145. aFree(atcmd_binding[i]);
  19146. atcmd_binding[i] = NULL;
  19147. /* compact the list now that we freed a slot somewhere */
  19148. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  19149. if( atcmd_binding[i] == NULL )
  19150. continue;
  19151. if( cursor != i ) {
  19152. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  19153. }
  19154. cursor++;
  19155. }
  19156. if( (atcmd_binding_count = cursor) == 0 )
  19157. aFree(atcmd_binding);
  19158. script_pushint(st, 1);
  19159. } else
  19160. script_pushint(st, 0);/* not found */
  19161. return SCRIPT_CMD_SUCCESS;
  19162. }
  19163. BUILDIN_FUNC(useatcmd) {
  19164. return atcommand_sub(st,3);
  19165. }
  19166. BUILDIN_FUNC(checkre)
  19167. {
  19168. int num;
  19169. num=script_getnum(st,2);
  19170. switch(num){
  19171. case 0:
  19172. #ifdef RENEWAL
  19173. script_pushint(st, 1);
  19174. #else
  19175. script_pushint(st, 0);
  19176. #endif
  19177. break;
  19178. case 1:
  19179. #ifdef RENEWAL_CAST
  19180. script_pushint(st, 1);
  19181. #else
  19182. script_pushint(st, 0);
  19183. #endif
  19184. break;
  19185. case 2:
  19186. #ifdef RENEWAL_DROP
  19187. script_pushint(st, 1);
  19188. #else
  19189. script_pushint(st, 0);
  19190. #endif
  19191. break;
  19192. case 3:
  19193. #ifdef RENEWAL_EXP
  19194. script_pushint(st, 1);
  19195. #else
  19196. script_pushint(st, 0);
  19197. #endif
  19198. break;
  19199. case 4:
  19200. #ifdef RENEWAL_LVDMG
  19201. script_pushint(st, 1);
  19202. #else
  19203. script_pushint(st, 0);
  19204. #endif
  19205. break;
  19206. case 5:
  19207. #ifdef RENEWAL_ASPD
  19208. script_pushint(st, 1);
  19209. #else
  19210. script_pushint(st, 0);
  19211. #endif
  19212. break;
  19213. default:
  19214. ShowWarning("buildin_checkre: unknown parameter.\n");
  19215. break;
  19216. }
  19217. return SCRIPT_CMD_SUCCESS;
  19218. }
  19219. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  19220. BUILDIN_FUNC(getrandgroupitem) {
  19221. TBL_PC* sd;
  19222. int i, get_count = 0, identify = 0;
  19223. uint16 group, qty = 0;
  19224. uint8 sub_group = 1;
  19225. struct item item_tmp;
  19226. if (!script_charid2sd(6, sd))
  19227. return SCRIPT_CMD_SUCCESS;
  19228. group = script_getnum(st,2);
  19229. if (!group) {
  19230. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  19231. return SCRIPT_CMD_FAILURE;
  19232. }
  19233. FETCH(3, qty);
  19234. FETCH(4, sub_group);
  19235. FETCH(5, identify);
  19236. std::shared_ptr<s_item_group_entry> entry = itemdb_group.get_random_entry(group,sub_group);
  19237. if (!entry)
  19238. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  19239. memset(&item_tmp,0,sizeof(item_tmp));
  19240. item_tmp.nameid = entry->nameid;
  19241. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  19242. if (!qty)
  19243. qty = entry->amount;
  19244. //Check if it's stackable.
  19245. if (!itemdb_isstackable(entry->nameid)) {
  19246. item_tmp.amount = 1;
  19247. get_count = qty;
  19248. }
  19249. else {
  19250. item_tmp.amount = qty;
  19251. get_count = 1;
  19252. }
  19253. for (i = 0; i < get_count; i++) {
  19254. // if not pet egg
  19255. if (!pet_create_egg(sd, entry->nameid)) {
  19256. unsigned char flag = 0;
  19257. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  19258. clif_additem(sd,0,0,flag);
  19259. if (pc_candrop(sd,&item_tmp))
  19260. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  19261. }
  19262. }
  19263. }
  19264. return SCRIPT_CMD_SUCCESS;
  19265. }
  19266. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  19267. * Gives item(s) to the attached player based on item group contents
  19268. */
  19269. BUILDIN_FUNC(getgroupitem) {
  19270. TBL_PC *sd;
  19271. int group_id = script_getnum(st,2);
  19272. if (!script_charid2sd(4,sd))
  19273. return SCRIPT_CMD_SUCCESS;
  19274. if (itemdb_group.pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  19275. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  19276. return SCRIPT_CMD_FAILURE;
  19277. }
  19278. return SCRIPT_CMD_SUCCESS;
  19279. }
  19280. /* cleanmap <map_name>;
  19281. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  19282. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  19283. {
  19284. nullpo_ret(bl);
  19285. map_clearflooritem(bl);
  19286. return SCRIPT_CMD_SUCCESS;
  19287. }
  19288. BUILDIN_FUNC(cleanmap)
  19289. {
  19290. const char *mapname;
  19291. int16 m;
  19292. mapname = script_getstr(st, 2);
  19293. m = map_mapname2mapid(mapname);
  19294. if (!m)
  19295. return SCRIPT_CMD_FAILURE;
  19296. if ((script_lastdata(st) - 2) < 4) {
  19297. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  19298. } else {
  19299. int16 x0 = script_getnum(st, 3);
  19300. int16 y0 = script_getnum(st, 4);
  19301. int16 x1 = script_getnum(st, 5);
  19302. int16 y1 = script_getnum(st, 6);
  19303. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  19304. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  19305. } else {
  19306. ShowError("cleanarea: invalid coordinate defined!\n");
  19307. return SCRIPT_CMD_FAILURE;
  19308. }
  19309. }
  19310. return SCRIPT_CMD_SUCCESS;
  19311. }
  19312. /* Cast a skill on the attached player.
  19313. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  19314. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  19315. BUILDIN_FUNC(npcskill)
  19316. {
  19317. uint16 skill_id;
  19318. unsigned short skill_level;
  19319. unsigned int stat_point;
  19320. unsigned int npc_level;
  19321. struct npc_data *nd;
  19322. struct map_session_data *sd;
  19323. if( !script_rid2sd(sd) )
  19324. return SCRIPT_CMD_SUCCESS;
  19325. if (script_isstring(st, 2)) {
  19326. const char *name = script_getstr(st, 2);
  19327. if (!(skill_id = skill_name2id(name))) {
  19328. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  19329. return SCRIPT_CMD_FAILURE;
  19330. }
  19331. } else {
  19332. skill_id = script_getnum(st, 2);
  19333. if (!skill_get_index(skill_id)) {
  19334. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  19335. return SCRIPT_CMD_FAILURE;
  19336. }
  19337. }
  19338. skill_level = script_getnum(st, 3);
  19339. stat_point = script_getnum(st, 4);
  19340. npc_level = script_getnum(st, 5);
  19341. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  19342. if (stat_point > battle_config.max_third_parameter) {
  19343. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  19344. return SCRIPT_CMD_FAILURE;
  19345. }
  19346. if (npc_level > MAX_LEVEL) {
  19347. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  19348. return SCRIPT_CMD_FAILURE;
  19349. }
  19350. if (nd == NULL) { //ain't possible, but I don't trust people.
  19351. return SCRIPT_CMD_FAILURE;
  19352. }
  19353. nd->level = npc_level;
  19354. nd->stat_point = stat_point;
  19355. if (!nd->status.hp)
  19356. status_calc_npc(nd, SCO_FIRST);
  19357. else
  19358. status_calc_npc(nd, SCO_NONE);
  19359. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  19360. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  19361. else
  19362. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  19363. return SCRIPT_CMD_SUCCESS;
  19364. }
  19365. /* Consumes an item.
  19366. * consumeitem <item id>{,<char_id>};
  19367. * consumeitem "<item name>"{,<char_id>};
  19368. * @param item: Item ID or name
  19369. */
  19370. BUILDIN_FUNC(consumeitem)
  19371. {
  19372. struct map_session_data *sd;
  19373. std::shared_ptr<item_data> item_data;
  19374. if (!script_charid2sd(3, sd))
  19375. return SCRIPT_CMD_FAILURE;
  19376. if( script_isstring(st, 2) ){
  19377. const char *name = script_getstr(st, 2);
  19378. item_data = item_db.searchname( name );
  19379. if( item_data == nullptr ){
  19380. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  19381. return SCRIPT_CMD_FAILURE;
  19382. }
  19383. } else {
  19384. t_itemid nameid = script_getnum(st, 2);
  19385. item_data = item_db.find( nameid );
  19386. if( item_data == nullptr ){
  19387. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  19388. return SCRIPT_CMD_FAILURE;
  19389. }
  19390. }
  19391. run_script( item_data->script, 0, sd->bl.id, 0 );
  19392. return SCRIPT_CMD_SUCCESS;
  19393. }
  19394. /** Makes a player sit/stand.
  19395. * sit {"<character name>"};
  19396. * stand {"<character name>"};
  19397. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  19398. * @param name: Player name that will be invoked
  19399. */
  19400. BUILDIN_FUNC(sit)
  19401. {
  19402. TBL_PC *sd;
  19403. if( !script_nick2sd(2,sd) )
  19404. return SCRIPT_CMD_FAILURE;
  19405. if( !pc_issit(sd) ) {
  19406. pc_setsit(sd);
  19407. skill_sit(sd, 1);
  19408. clif_sitting(&sd->bl);
  19409. }
  19410. return SCRIPT_CMD_SUCCESS;
  19411. }
  19412. /** Makes player to stand
  19413. * @param name: Player name that will be set
  19414. */
  19415. BUILDIN_FUNC(stand)
  19416. {
  19417. TBL_PC *sd;
  19418. if( !script_nick2sd(2,sd) )
  19419. return SCRIPT_CMD_FAILURE;
  19420. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19421. skill_sit(sd, 0);
  19422. clif_standing(&sd->bl);
  19423. }
  19424. return SCRIPT_CMD_SUCCESS;
  19425. }
  19426. /** Creates an array of bounded item IDs
  19427. * countbound {<type>{,<char_id>}};
  19428. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19429. * @return amt: Total number of different items type found
  19430. */
  19431. BUILDIN_FUNC(countbound)
  19432. {
  19433. TBL_PC *sd;
  19434. if (!script_charid2sd(3,sd))
  19435. return SCRIPT_CMD_FAILURE;
  19436. int i, k = 0;
  19437. int type = script_getnum(st,2);
  19438. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19439. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19440. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19441. ))
  19442. {
  19443. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19444. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19445. k++;
  19446. }
  19447. }
  19448. script_pushint(st,k);
  19449. return SCRIPT_CMD_SUCCESS;
  19450. }
  19451. /** Creates new party
  19452. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19453. * @param party_name: String of party name that will be created
  19454. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19455. * @param item_share: 0-Each Take. 1-Party Share
  19456. * @param item_share_type: 0-Each Take. 1-Even Share
  19457. * @return val: Result value
  19458. * -3 - party name is exist
  19459. * -2 - player is in party already
  19460. * -1 - player is not found
  19461. * 0 - unknown error
  19462. * 1 - success, will return party id $@party_create_id
  19463. */
  19464. BUILDIN_FUNC(party_create)
  19465. {
  19466. char party_name[NAME_LENGTH];
  19467. int item1 = 0, item2 = 0;
  19468. TBL_PC *sd = NULL;
  19469. if (!script_charid2sd(3, sd)) {
  19470. script_pushint(st,-1);
  19471. return SCRIPT_CMD_FAILURE;
  19472. }
  19473. if( sd->status.party_id ) {
  19474. script_pushint(st,-2);
  19475. return SCRIPT_CMD_FAILURE;
  19476. }
  19477. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19478. trim(party_name);
  19479. if( party_searchname(party_name) ) {
  19480. script_pushint(st,-3);
  19481. return SCRIPT_CMD_FAILURE;
  19482. }
  19483. if( script_getnum(st,4) )
  19484. item1 = 1;
  19485. if( script_getnum(st,5) )
  19486. item2 = 1;
  19487. party_create_byscript = 1;
  19488. script_pushint(st,party_create(sd,party_name,item1,item2));
  19489. return SCRIPT_CMD_SUCCESS;
  19490. }
  19491. /** Adds player to specified party
  19492. * party_addmember <party id>,<char id>;
  19493. * @param party_id: The party that will be entered by player
  19494. * @param char_id: Char id of player that will be joined to the party
  19495. * @return val: Result value
  19496. * -5 - another character of the same account is in the party
  19497. * -4 - party is full
  19498. * -3 - party is not found
  19499. * -2 - player is in party already
  19500. * -1 - player is not found
  19501. * 0 - unknown error
  19502. * 1 - success
  19503. */
  19504. BUILDIN_FUNC(party_addmember)
  19505. {
  19506. int party_id = script_getnum(st,2);
  19507. TBL_PC *sd;
  19508. struct party_data *party;
  19509. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19510. script_pushint(st,-1);
  19511. return SCRIPT_CMD_FAILURE;
  19512. }
  19513. if( sd->status.party_id ) {
  19514. script_pushint(st,-2);
  19515. return SCRIPT_CMD_FAILURE;
  19516. }
  19517. if( !(party = party_search(party_id)) ) {
  19518. script_pushint(st,-3);
  19519. return SCRIPT_CMD_FAILURE;
  19520. }
  19521. if (battle_config.block_account_in_same_party) {
  19522. int i;
  19523. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19524. if (i < MAX_PARTY) {
  19525. script_pushint(st,-5);
  19526. return SCRIPT_CMD_FAILURE;
  19527. }
  19528. }
  19529. if( party->party.count >= MAX_PARTY ) {
  19530. script_pushint(st,-4);
  19531. return SCRIPT_CMD_FAILURE;
  19532. }
  19533. sd->party_invite = party_id;
  19534. script_pushint(st,party_add_member(party_id,sd));
  19535. return SCRIPT_CMD_SUCCESS;
  19536. }
  19537. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19538. * party_delmember {<char id>,<party_id>};
  19539. * @param: char_id
  19540. * @param: party_id
  19541. * @return val: Result value
  19542. * -3 - player is not in party
  19543. * -2 - party is not found
  19544. * -1 - player is not found
  19545. * 0 - unknown error
  19546. * 1 - success
  19547. */
  19548. BUILDIN_FUNC(party_delmember)
  19549. {
  19550. TBL_PC *sd = NULL;
  19551. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19552. script_pushint(st,-1);
  19553. return SCRIPT_CMD_FAILURE;
  19554. }
  19555. if( sd || script_charid2sd(2,sd) )
  19556. script_pushint(st,party_removemember2(sd,0,0));
  19557. else
  19558. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  19559. return SCRIPT_CMD_SUCCESS;
  19560. }
  19561. /** Changes party leader of specified party (even the leader is offline)
  19562. * party_changeleader <party id>,<char id>;
  19563. * @param party_id: ID of party
  19564. * @param char_id: Char ID of new leader
  19565. * @return val: Result value
  19566. * -4 - player is party leader already
  19567. * -3 - player is not in this party
  19568. * -2 - player is not found
  19569. * -1 - party is not found
  19570. * 0 - unknown error
  19571. * 1 - success */
  19572. BUILDIN_FUNC(party_changeleader)
  19573. {
  19574. int i, party_id = script_getnum(st,2);
  19575. TBL_PC *sd = NULL;
  19576. TBL_PC *tsd = NULL;
  19577. struct party_data *party = NULL;
  19578. if( !(party = party_search(party_id)) ) {
  19579. script_pushint(st,-1);
  19580. return SCRIPT_CMD_FAILURE;
  19581. }
  19582. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19583. script_pushint(st,-2);
  19584. return SCRIPT_CMD_FAILURE;
  19585. }
  19586. if( tsd->status.party_id != party_id ) {
  19587. script_pushint(st,-3);
  19588. return SCRIPT_CMD_FAILURE;
  19589. }
  19590. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19591. if( i >= MAX_PARTY ) { //this is should impossible!
  19592. script_pushint(st,0);
  19593. return SCRIPT_CMD_FAILURE;
  19594. }
  19595. if( party->data[i].sd == tsd ) {
  19596. script_pushint(st,-4);
  19597. return SCRIPT_CMD_FAILURE;
  19598. }
  19599. script_pushint(st,party_changeleader(sd,tsd,party));
  19600. return SCRIPT_CMD_SUCCESS;
  19601. }
  19602. /** Changes party option
  19603. * party_changeoption <party id>,<option>,<flag>;
  19604. * @param party_id: ID of party that will be changed
  19605. * @param option: Type of option
  19606. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  19607. */
  19608. BUILDIN_FUNC(party_changeoption)
  19609. {
  19610. struct party_data *party;
  19611. if( !(party = party_search(script_getnum(st,2))) ) {
  19612. script_pushint(st,-1);
  19613. return SCRIPT_CMD_FAILURE;
  19614. }
  19615. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  19616. return SCRIPT_CMD_SUCCESS;
  19617. }
  19618. /** Destroys party with party id.
  19619. * party_destroy <party id>;
  19620. * @param party_id: ID of party that will be destroyed
  19621. */
  19622. BUILDIN_FUNC(party_destroy)
  19623. {
  19624. int i;
  19625. struct party_data *party;
  19626. if( !(party = party_search(script_getnum(st,2))) ) {
  19627. script_pushint(st,0);
  19628. return SCRIPT_CMD_FAILURE;
  19629. }
  19630. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19631. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  19632. int j;
  19633. for( j = 0; j < MAX_PARTY; j++ ) {
  19634. TBL_PC *sd = party->data[j].sd;
  19635. if(sd)
  19636. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  19637. else if( party->party.member[j].char_id )
  19638. 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);
  19639. }
  19640. party_broken(party->party.party_id);
  19641. script_pushint(st,1);
  19642. }
  19643. else //leader leave = party broken
  19644. script_pushint(st,party_leave(party->data[i].sd));
  19645. return SCRIPT_CMD_SUCCESS;
  19646. }
  19647. /** Returns various information about a player's VIP status. Need to enable VIP system
  19648. * vip_status <type>,{"<character name>"};
  19649. * @param type: Info type, see enum vip_status_type
  19650. * @param name: Character name (Optional)
  19651. */
  19652. BUILDIN_FUNC(vip_status) {
  19653. #ifdef VIP_ENABLE
  19654. TBL_PC *sd;
  19655. int type;
  19656. if( !script_nick2sd(3,sd) )
  19657. return SCRIPT_CMD_FAILURE;
  19658. type = script_getnum(st, 2);
  19659. switch(type) {
  19660. case VIP_STATUS_ACTIVE: // Get VIP status.
  19661. script_pushint(st, pc_isvip(sd));
  19662. break;
  19663. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  19664. if (pc_isvip(sd)) {
  19665. script_pushint(st, sd->vip.time);
  19666. } else
  19667. script_pushint(st, 0);
  19668. break;
  19669. case VIP_STATUS_REMAINING: // Get remaining time.
  19670. if (pc_isvip(sd)) {
  19671. script_pushint(st, sd->vip.time - time(NULL));
  19672. } else
  19673. script_pushint(st, 0);
  19674. break;
  19675. default:
  19676. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  19677. return SCRIPT_CMD_FAILURE;
  19678. }
  19679. #else
  19680. script_pushint(st, 0);
  19681. #endif
  19682. return SCRIPT_CMD_SUCCESS;
  19683. }
  19684. /** Adds or removes VIP time in minutes. Need to enable VIP system
  19685. * vip_time <time in mn>,{"<character name>"};
  19686. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  19687. * @param name: Character name (optional)
  19688. */
  19689. BUILDIN_FUNC(vip_time) {
  19690. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  19691. TBL_PC *sd;
  19692. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  19693. if( !script_nick2sd(3,sd) )
  19694. return SCRIPT_CMD_FAILURE;
  19695. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  19696. #endif
  19697. return SCRIPT_CMD_SUCCESS;
  19698. }
  19699. /**
  19700. * Turns a player into a monster and optionally can grant a SC attribute effect.
  19701. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19702. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19703. * @param monster: Monster ID or name
  19704. * @param duration: Transform duration in millisecond (ms)
  19705. * @param sc_type: Type of SC that will be affected during the transformation
  19706. * @param val1: Value for SC
  19707. * @param val2: Value for SC
  19708. * @param val3: Value for SC
  19709. * @param val4: Value for SC
  19710. * @author: malufett
  19711. */
  19712. BUILDIN_FUNC(montransform) {
  19713. TBL_PC *sd;
  19714. enum sc_type type;
  19715. int tick, mob_id, val1, val2, val3, val4;
  19716. val1 = val2 = val3 = val4 = 0;
  19717. if( !script_rid2sd(sd) )
  19718. return SCRIPT_CMD_FAILURE;
  19719. if( script_isstring(st, 2) )
  19720. mob_id = mobdb_searchname(script_getstr(st, 2));
  19721. else
  19722. mob_id = mobdb_checkid(script_getnum(st, 2));
  19723. tick = script_getnum(st, 3);
  19724. if (script_hasdata(st, 4))
  19725. type = (sc_type)script_getnum(st, 4);
  19726. else
  19727. type = SC_NONE;
  19728. if (mob_id == 0) {
  19729. if( script_isstring(st, 2) )
  19730. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  19731. else
  19732. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  19733. return SCRIPT_CMD_FAILURE;
  19734. }
  19735. if (mob_id == MOBID_EMPERIUM) {
  19736. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  19737. return SCRIPT_CMD_FAILURE;
  19738. }
  19739. if (!(type >= SC_NONE && type < SC_MAX)) {
  19740. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  19741. return SCRIPT_CMD_FAILURE;
  19742. }
  19743. if (script_hasdata(st, 5))
  19744. val1 = script_getnum(st, 5);
  19745. if (script_hasdata(st, 6))
  19746. val2 = script_getnum(st, 6);
  19747. if (script_hasdata(st, 7))
  19748. val3 = script_getnum(st, 7);
  19749. if (script_hasdata(st, 8))
  19750. val4 = script_getnum(st, 8);
  19751. if (tick != 0) {
  19752. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  19753. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  19754. return SCRIPT_CMD_FAILURE;
  19755. }
  19756. if (sd->disguise){
  19757. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  19758. return SCRIPT_CMD_FAILURE;
  19759. }
  19760. if (!strcmp(script_getfuncname(st), "active_transform")) {
  19761. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19762. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19763. } else {
  19764. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19765. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19766. }
  19767. if (type != SC_NONE)
  19768. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  19769. }
  19770. return SCRIPT_CMD_SUCCESS;
  19771. }
  19772. /**
  19773. * Attach script to player for certain duration
  19774. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  19775. * @param "script code"
  19776. * @param duration
  19777. * @param flag
  19778. * @param icon
  19779. * @param char_id
  19780. * @author [Cydh]
  19781. **/
  19782. BUILDIN_FUNC(bonus_script) {
  19783. uint16 flag = 0;
  19784. int16 icon = EFST_BLANK;
  19785. uint32 dur;
  19786. uint8 type = 0;
  19787. TBL_PC* sd;
  19788. const char *script_str = NULL;
  19789. struct s_bonus_script_entry *entry = NULL;
  19790. if ( !script_charid2sd(7,sd) )
  19791. return SCRIPT_CMD_FAILURE;
  19792. script_str = script_getstr(st,2);
  19793. dur = 1000 * abs(script_getnum(st,3));
  19794. FETCH(4, flag);
  19795. FETCH(5, type);
  19796. FETCH(6, icon);
  19797. // No Script string, No Duration!
  19798. if (script_str[0] == '\0' || !dur) {
  19799. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  19800. return SCRIPT_CMD_FAILURE;
  19801. }
  19802. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  19803. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  19804. return SCRIPT_CMD_FAILURE;
  19805. }
  19806. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  19807. icon = EFST_BLANK;
  19808. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_types)icon, flag, type))) {
  19809. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  19810. status_calc_pc(sd,SCO_NONE);
  19811. }
  19812. return SCRIPT_CMD_SUCCESS;
  19813. }
  19814. /**
  19815. * Removes all bonus script from player
  19816. * bonus_script_clear {<flag>,{<char_id>}};
  19817. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  19818. * @param char_id Clear script from this character
  19819. * @author [Cydh]
  19820. */
  19821. BUILDIN_FUNC(bonus_script_clear) {
  19822. TBL_PC* sd;
  19823. bool flag = false;
  19824. if (!script_charid2sd(3,sd))
  19825. return SCRIPT_CMD_FAILURE;
  19826. if (script_hasdata(st,2))
  19827. flag = script_getnum(st,2) != 0;
  19828. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  19829. return SCRIPT_CMD_SUCCESS;
  19830. }
  19831. /** Allows player to use atcommand while talking with NPC
  19832. * enable_command;
  19833. * @author [Cydh], [Kichi]
  19834. */
  19835. BUILDIN_FUNC(enable_command) {
  19836. TBL_PC* sd;
  19837. if (!script_rid2sd(sd))
  19838. return SCRIPT_CMD_FAILURE;
  19839. sd->state.disable_atcommand_on_npc = 0;
  19840. return SCRIPT_CMD_SUCCESS;
  19841. }
  19842. /** Prevents player to use atcommand while talking with NPC
  19843. * disable_command;
  19844. * @author [Cydh], [Kichi]
  19845. */
  19846. BUILDIN_FUNC(disable_command) {
  19847. TBL_PC* sd;
  19848. if (!script_rid2sd(sd))
  19849. return SCRIPT_CMD_FAILURE;
  19850. sd->state.disable_atcommand_on_npc = 1;
  19851. return SCRIPT_CMD_SUCCESS;
  19852. }
  19853. /** Get the information of the members of a guild by type.
  19854. * getguildmember <guild_id>{,<type>};
  19855. * @param guild_id: ID of guild
  19856. * @param type: Type of option (optional)
  19857. */
  19858. BUILDIN_FUNC(getguildmember)
  19859. {
  19860. struct guild *g = NULL;
  19861. uint8 j = 0;
  19862. g = guild_search(script_getnum(st,2));
  19863. if (g) {
  19864. uint8 i, type = 0;
  19865. struct script_data *data = NULL;
  19866. char *varname = NULL;
  19867. if (script_hasdata(st,3))
  19868. type = script_getnum(st,3);
  19869. if (script_hasdata(st,4)) {
  19870. data = script_getdata(st, 4);
  19871. if (!data_isreference(data)) {
  19872. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  19873. return SCRIPT_CMD_FAILURE;
  19874. }
  19875. varname = reference_getname(data);
  19876. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  19877. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  19878. return SCRIPT_CMD_FAILURE;
  19879. }
  19880. if (not_server_variable(*varname)) {
  19881. struct map_session_data *sd;
  19882. if (!script_rid2sd(sd)) {
  19883. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  19884. return SCRIPT_CMD_FAILURE;
  19885. }
  19886. }
  19887. }
  19888. for (i = 0; i < MAX_GUILD; i++) {
  19889. if (g->member[i].account_id) {
  19890. switch (type) {
  19891. case 2:
  19892. if (data)
  19893. setd_sub_num( st, NULL, varname, j, g->member[i].account_id, data->ref );
  19894. else
  19895. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  19896. break;
  19897. case 1:
  19898. if (data)
  19899. setd_sub_num( st, NULL, varname, j, g->member[i].char_id, data->ref );
  19900. else
  19901. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  19902. break;
  19903. default:
  19904. if (data)
  19905. setd_sub_str( st, NULL, varname, j, g->member[i].name, data->ref );
  19906. else
  19907. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  19908. break;
  19909. }
  19910. j++;
  19911. }
  19912. }
  19913. }
  19914. mapreg_setreg(add_str("$@guildmembercount"), j);
  19915. script_pushint(st, j);
  19916. return SCRIPT_CMD_SUCCESS;
  19917. }
  19918. /** Adds spirit ball to player for 'duration' in milisecond
  19919. * addspiritball <count>,<duration>{,<char_id>};
  19920. * @param count How many spirit ball will be added
  19921. * @param duration How long spiritball is active until it disappears
  19922. * @param char_id Target player (Optional)
  19923. * @author [Cydh]
  19924. */
  19925. BUILDIN_FUNC(addspiritball) {
  19926. uint8 i, count;
  19927. uint16 duration;
  19928. struct map_session_data *sd = NULL;
  19929. if (script_hasdata(st,4)) {
  19930. if (!script_isstring(st,4))
  19931. sd = map_charid2sd(script_getnum(st,4));
  19932. else
  19933. sd = map_nick2sd(script_getstr(st,4),false);
  19934. }
  19935. else
  19936. script_rid2sd(sd);
  19937. if (!sd)
  19938. return SCRIPT_CMD_FAILURE;
  19939. count = script_getnum(st,2);
  19940. if (count == 0)
  19941. return SCRIPT_CMD_SUCCESS;
  19942. duration = script_getnum(st,3);
  19943. for (i = 0; i < count; i++)
  19944. pc_addspiritball(sd,duration,10);
  19945. return SCRIPT_CMD_SUCCESS;
  19946. }
  19947. /** Deletes the spirit ball(s) from player
  19948. * delspiritball <count>{,<char_id>};
  19949. * @param count How many spirit ball will be deleted
  19950. * @param char_id Target player (Optional)
  19951. * @author [Cydh]
  19952. */
  19953. BUILDIN_FUNC(delspiritball) {
  19954. uint8 count;
  19955. struct map_session_data *sd = NULL;
  19956. if (script_hasdata(st,3)) {
  19957. if (!script_isstring(st,3))
  19958. sd = map_charid2sd(script_getnum(st,3));
  19959. else
  19960. sd = map_nick2sd(script_getstr(st,3),false);
  19961. }
  19962. else
  19963. script_rid2sd(sd);
  19964. if (!sd)
  19965. return SCRIPT_CMD_FAILURE;
  19966. count = script_getnum(st,2);
  19967. if (count == 0)
  19968. count = 1;
  19969. pc_delspiritball(sd,count,0);
  19970. return SCRIPT_CMD_SUCCESS;
  19971. }
  19972. /** Counts the spirit ball that player has
  19973. * countspiritball {<char_id>};
  19974. * @param char_id Target player (Optional)
  19975. * @author [Cydh]
  19976. */
  19977. BUILDIN_FUNC(countspiritball) {
  19978. struct map_session_data *sd;
  19979. if (script_hasdata(st,2)) {
  19980. if (!script_isstring(st,2))
  19981. sd = map_charid2sd(script_getnum(st,2));
  19982. else
  19983. sd = map_nick2sd(script_getstr(st,2),false);
  19984. }
  19985. else
  19986. script_rid2sd(sd);
  19987. if (!sd)
  19988. return SCRIPT_CMD_FAILURE;
  19989. script_pushint(st,sd->spiritball);
  19990. return SCRIPT_CMD_SUCCESS;
  19991. }
  19992. /** Merges separated stackable items because of guid
  19993. * mergeitem {<char_id>};
  19994. * @param char_id Char ID (Optional)
  19995. * @author [Cydh]
  19996. */
  19997. BUILDIN_FUNC(mergeitem) {
  19998. struct map_session_data *sd;
  19999. if (!script_charid2sd(2, sd))
  20000. return SCRIPT_CMD_FAILURE;
  20001. clif_merge_item_open(sd);
  20002. return SCRIPT_CMD_SUCCESS;
  20003. }
  20004. /** Merges separated stackable items because of guid (Unofficial)
  20005. * mergeitem2 {<item_id>,{<char_id>}};
  20006. * mergeitem2 {"<item name>",{<char_id>}};
  20007. * @param item Item ID/Name for merging specific item (Optional)
  20008. * @author [Cydh]
  20009. */
  20010. BUILDIN_FUNC(mergeitem2) {
  20011. struct map_session_data *sd;
  20012. struct item *items = NULL;
  20013. uint16 i, count = 0;
  20014. t_itemid nameid = 0;
  20015. if (!script_charid2sd(3, sd))
  20016. return SCRIPT_CMD_FAILURE;
  20017. if (script_hasdata(st, 2)) {
  20018. if (script_isstring(st, 2)) {// "<item name>"
  20019. const char *name = script_getstr(st, 2);
  20020. std::shared_ptr<item_data> id = item_db.searchname( name );
  20021. if( id == nullptr ){
  20022. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  20023. script_pushint(st, count);
  20024. return SCRIPT_CMD_FAILURE;
  20025. }
  20026. nameid = id->nameid;
  20027. } else {// <item id>
  20028. nameid = script_getnum(st, 2);
  20029. if (!itemdb_exists(nameid)) {
  20030. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  20031. script_pushint(st, count);
  20032. return SCRIPT_CMD_FAILURE;
  20033. }
  20034. }
  20035. }
  20036. for (i = 0; i < MAX_INVENTORY; i++) {
  20037. struct item *it = &sd->inventory.u.items_inventory[i];
  20038. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  20039. continue;
  20040. if ((!nameid || (nameid == it->nameid))) {
  20041. uint8 k;
  20042. if (!count) {
  20043. CREATE(items, struct item, 1);
  20044. memcpy(&items[count++], it, sizeof(struct item));
  20045. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20046. continue;
  20047. }
  20048. for (k = 0; k < count; k++) {
  20049. // Find Match
  20050. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  20051. items[k].amount += it->amount;
  20052. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20053. break;
  20054. }
  20055. }
  20056. if (k >= count) {
  20057. // New entry
  20058. RECREATE(items, struct item, count+1);
  20059. memcpy(&items[count++], it, sizeof(struct item));
  20060. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  20061. }
  20062. }
  20063. }
  20064. if (!items) // Nothing todo here
  20065. return SCRIPT_CMD_SUCCESS;
  20066. // Retrieve the items
  20067. for (i = 0; i < count; i++) {
  20068. uint8 flag = 0;
  20069. if (!&items[i])
  20070. continue;
  20071. items[i].id = 0;
  20072. items[i].unique_id = 0;
  20073. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  20074. clif_additem(sd, i, items[i].amount, flag);
  20075. }
  20076. aFree(items);
  20077. script_pushint(st, count);
  20078. return SCRIPT_CMD_SUCCESS;
  20079. }
  20080. /**
  20081. * Update an entry from NPC shop.
  20082. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  20083. **/
  20084. BUILDIN_FUNC(npcshopupdate) {
  20085. const char* npcname = script_getstr(st, 2);
  20086. struct npc_data* nd = npc_name2id(npcname);
  20087. t_itemid nameid = script_getnum(st, 3);
  20088. int price = script_getnum(st, 4);
  20089. #if PACKETVER >= 20131223
  20090. int32 stock = script_hasdata(st,5) ? script_getnum(st,5) : -1;
  20091. if( stock < -1 ){
  20092. ShowError( "buildin_npcshopupdate: Invalid stock amount in marketshop '%s'.\n", nd->exname );
  20093. script_pushint( st, 0 );
  20094. return SCRIPT_CMD_FAILURE;
  20095. }
  20096. #endif
  20097. int i;
  20098. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  20099. script_pushint(st,0);
  20100. return SCRIPT_CMD_FAILURE;
  20101. }
  20102. if (!nd->u.shop.count) {
  20103. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  20104. script_pushint(st,0);
  20105. return SCRIPT_CMD_FAILURE;
  20106. }
  20107. for (i = 0; i < nd->u.shop.count; i++) {
  20108. if (nd->u.shop.shop_item[i].nameid == nameid) {
  20109. if (price != 0)
  20110. nd->u.shop.shop_item[i].value = price;
  20111. #if PACKETVER >= 20131223
  20112. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  20113. nd->u.shop.shop_item[i].qty = stock;
  20114. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  20115. }
  20116. #endif
  20117. }
  20118. }
  20119. script_pushint(st,1);
  20120. return SCRIPT_CMD_SUCCESS;
  20121. }
  20122. // Clan System
  20123. BUILDIN_FUNC(clan_join){
  20124. struct map_session_data *sd;
  20125. int clan_id = script_getnum(st,2);
  20126. if( !script_charid2sd( 3, sd ) ){
  20127. script_pushint(st, false);
  20128. return SCRIPT_CMD_FAILURE;
  20129. }
  20130. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  20131. script_pushint(st, true);
  20132. else
  20133. script_pushint(st, false);
  20134. return SCRIPT_CMD_SUCCESS;
  20135. }
  20136. BUILDIN_FUNC(clan_leave){
  20137. struct map_session_data *sd;
  20138. if( !script_charid2sd( 2, sd ) ){
  20139. script_pushint(st, false);
  20140. return SCRIPT_CMD_FAILURE;
  20141. }
  20142. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  20143. script_pushint(st, true);
  20144. else
  20145. script_pushint(st, false);
  20146. return SCRIPT_CMD_SUCCESS;
  20147. }
  20148. /**
  20149. * Get rid from running script.
  20150. * getattachedrid();
  20151. **/
  20152. BUILDIN_FUNC(getattachedrid) {
  20153. script_pushint(st, st->rid);
  20154. return SCRIPT_CMD_SUCCESS;
  20155. }
  20156. /**
  20157. * Get variable from a Player
  20158. * getvar <variable>,<char_id>;
  20159. */
  20160. BUILDIN_FUNC(getvar) {
  20161. int char_id = script_getnum(st, 3);
  20162. struct map_session_data *sd = map_charid2sd(char_id);
  20163. struct script_data *data = NULL;
  20164. const char *name = NULL;
  20165. if (!sd) {
  20166. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  20167. return SCRIPT_CMD_FAILURE;
  20168. }
  20169. data = script_getdata(st, 2);
  20170. if (!data_isreference(data)) {
  20171. ShowError("buildin_getvar: Not a variable\n");
  20172. script_reportdata(data);
  20173. script_pushnil(st);
  20174. st->state = END;
  20175. return SCRIPT_CMD_FAILURE;
  20176. }
  20177. name = reference_getname(data);
  20178. if (reference_toparam(data)) {
  20179. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  20180. script_reportdata(data);
  20181. script_pushnil(st);
  20182. st->state = END;
  20183. return SCRIPT_CMD_FAILURE;
  20184. }
  20185. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  20186. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  20187. script_reportdata(data);
  20188. script_pushnil(st);
  20189. st->state = END;
  20190. return SCRIPT_CMD_FAILURE;
  20191. }
  20192. get_val_(st, data, sd);
  20193. if (data_isint(data))
  20194. script_pushint(st, conv_num_(st, data, sd));
  20195. else
  20196. script_pushstrcopy(st, conv_str_(st, data, sd));
  20197. return SCRIPT_CMD_SUCCESS;
  20198. }
  20199. /**
  20200. * Display script message
  20201. * showscript "<message>"{,<GID>,<flag>};
  20202. * @param flag: Specify target
  20203. * AREA - Message is sent to players in the vicinity of the source (default).
  20204. * SELF - Message is sent only to player attached.
  20205. **/
  20206. BUILDIN_FUNC(showscript) {
  20207. struct block_list *bl = NULL;
  20208. const char *msg = script_getstr(st,2);
  20209. int id = 0;
  20210. send_target target = AREA;
  20211. if (script_hasdata(st,3)) {
  20212. id = script_getnum(st,3);
  20213. bl = map_id2bl(id);
  20214. }
  20215. else {
  20216. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  20217. }
  20218. if (!bl) {
  20219. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  20220. script_pushint(st,0);
  20221. return SCRIPT_CMD_FAILURE;
  20222. }
  20223. if (script_hasdata(st, 4)) {
  20224. target = static_cast<send_target>(script_getnum(st, 4));
  20225. if (target == SELF && map_id2sd(bl->id) == NULL) {
  20226. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  20227. return SCRIPT_CMD_FAILURE;
  20228. }
  20229. }
  20230. clif_showscript(bl, msg, target);
  20231. script_pushint(st,1);
  20232. return SCRIPT_CMD_SUCCESS;
  20233. }
  20234. /**
  20235. * Ignore the SECURE_NPCTIMEOUT function.
  20236. * ignoretimeout <flag>{,<char_id>};
  20237. */
  20238. BUILDIN_FUNC(ignoretimeout)
  20239. {
  20240. #ifdef SECURE_NPCTIMEOUT
  20241. struct map_session_data *sd = NULL;
  20242. if (script_hasdata(st,3)) {
  20243. if (!script_isstring(st,3))
  20244. sd = map_charid2sd(script_getnum(st,3));
  20245. else
  20246. sd = map_nick2sd(script_getstr(st,3),false);
  20247. } else
  20248. script_rid2sd(sd);
  20249. if (!sd)
  20250. return SCRIPT_CMD_FAILURE;
  20251. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  20252. #endif
  20253. return SCRIPT_CMD_SUCCESS;
  20254. }
  20255. /**
  20256. * geteleminfo <type>{,<char_id>};
  20257. **/
  20258. BUILDIN_FUNC(geteleminfo) {
  20259. TBL_ELEM *ed = NULL;
  20260. TBL_PC *sd = NULL;
  20261. int type = script_getnum(st,2);
  20262. if (!script_charid2sd(3, sd)) {
  20263. script_pushint(st, 0);
  20264. return SCRIPT_CMD_SUCCESS;
  20265. }
  20266. if (!(ed = sd->ed)) {
  20267. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  20268. script_pushint(st, 0);
  20269. return SCRIPT_CMD_SUCCESS;
  20270. }
  20271. switch (type) {
  20272. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  20273. case 1: script_pushint(st, ed->bl.id); break;
  20274. default:
  20275. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  20276. script_pushint(st, 0);
  20277. return SCRIPT_CMD_FAILURE;
  20278. }
  20279. return SCRIPT_CMD_SUCCESS;
  20280. }
  20281. /**
  20282. * opendressroom(<flag>{,<char_id>});
  20283. */
  20284. BUILDIN_FUNC(opendressroom)
  20285. {
  20286. #if PACKETVER >= 20150513
  20287. int flag = 1;
  20288. TBL_PC* sd;
  20289. if( script_hasdata(st,2) )
  20290. flag = script_getnum(st,2);
  20291. if (!script_charid2sd(3, sd))
  20292. return SCRIPT_CMD_FAILURE;
  20293. clif_dressing_room(sd, flag);
  20294. return SCRIPT_CMD_SUCCESS;
  20295. #else
  20296. return SCRIPT_CMD_FAILURE;
  20297. #endif
  20298. }
  20299. /**
  20300. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  20301. */
  20302. BUILDIN_FUNC(navigateto){
  20303. #if PACKETVER >= 20111010
  20304. TBL_PC* sd;
  20305. const char *mapname;
  20306. uint16 x = 0, y = 0, monster_id = 0;
  20307. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  20308. bool hideWindow = true;
  20309. mapname = script_getstr(st,2);
  20310. if( script_hasdata(st,3) )
  20311. x = script_getnum(st,3);
  20312. if( script_hasdata(st,4) )
  20313. y = script_getnum(st,4);
  20314. if( script_hasdata(st,5) )
  20315. flag = (uint8)script_getnum(st,5);
  20316. if( script_hasdata(st,6) )
  20317. hideWindow = script_getnum(st,6) ? true : false;
  20318. if( script_hasdata(st,7) )
  20319. monster_id = script_getnum(st,7);
  20320. if (!script_charid2sd(8, sd))
  20321. return SCRIPT_CMD_FAILURE;
  20322. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  20323. return SCRIPT_CMD_SUCCESS;
  20324. #else
  20325. return SCRIPT_CMD_FAILURE;
  20326. #endif
  20327. }
  20328. /**
  20329. * adopt("<parent_name>","<baby_name>");
  20330. * adopt(<parent_id>,<baby_id>);
  20331. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  20332. */
  20333. BUILDIN_FUNC(adopt)
  20334. {
  20335. TBL_PC *sd, *b_sd;
  20336. enum adopt_responses response;
  20337. if (script_isstring(st, 2)) {
  20338. const char *name = script_getstr(st, 2);
  20339. sd = map_nick2sd(name,false);
  20340. if (sd == NULL) {
  20341. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  20342. return SCRIPT_CMD_FAILURE;
  20343. }
  20344. } else {
  20345. uint32 char_id = script_getnum(st, 2);
  20346. sd = map_charid2sd(char_id);
  20347. if (sd == NULL) {
  20348. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  20349. return SCRIPT_CMD_FAILURE;
  20350. }
  20351. }
  20352. if (script_isstring(st, 3)) {
  20353. const char *name = script_getstr(st, 3);
  20354. b_sd = map_nick2sd(name,false);
  20355. if (b_sd == NULL) {
  20356. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  20357. return SCRIPT_CMD_FAILURE;
  20358. }
  20359. } else {
  20360. uint32 char_id = script_getnum(st, 3);
  20361. b_sd = map_charid2sd(char_id);
  20362. if (b_sd == NULL) {
  20363. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  20364. return SCRIPT_CMD_FAILURE;
  20365. }
  20366. }
  20367. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  20368. if (response == ADOPT_ALLOWED) {
  20369. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  20370. b_sd->adopt_invite = sd->status.account_id;
  20371. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  20372. script_pushint(st, ADOPT_ALLOWED);
  20373. return SCRIPT_CMD_SUCCESS;
  20374. }
  20375. script_pushint(st, response);
  20376. return SCRIPT_CMD_FAILURE;
  20377. }
  20378. /**
  20379. * Returns the minimum or maximum of all the given parameters for integer variables.
  20380. *
  20381. * min( <value or array>{,value or array 2,...} );
  20382. * minimum( <value or array>{,value or array 2,...} );
  20383. * max( <value or array>{,value or array 2,...} );
  20384. * maximum( <value or array>{,value or array 2,...} );
  20385. */
  20386. BUILDIN_FUNC(minmax){
  20387. char *functionname;
  20388. unsigned int i;
  20389. int64 value;
  20390. // Function pointer for our comparison function (either min or max at the moment)
  20391. int64 (*func)(int64, int64);
  20392. // Get the real function name
  20393. functionname = script_getfuncname(st);
  20394. // Our data should start at offset 2
  20395. i = 2;
  20396. if( !script_hasdata( st, i ) ){
  20397. ShowError( "buildin_%s: no arguments given!\n", functionname );
  20398. st->state = END;
  20399. return SCRIPT_CMD_FAILURE;
  20400. }
  20401. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  20402. value = SCRIPT_INT_MAX;
  20403. func = i64min;
  20404. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  20405. value = SCRIPT_INT_MIN;
  20406. func = i64max;
  20407. }else{
  20408. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  20409. st->state = END;
  20410. return SCRIPT_CMD_FAILURE;
  20411. }
  20412. // As long as we have data on our script stack
  20413. while( script_hasdata(st,i) ){
  20414. struct script_data *data;
  20415. // Get the next piece of data from the script stack
  20416. data = script_getdata( st, i );
  20417. // Is the current parameter a single integer?
  20418. if( data_isint( data ) ){
  20419. value = func( value, script_getnum( st, i ) );
  20420. // Is the current parameter an array variable?
  20421. }else if( data_isreference( data ) ){
  20422. const char *name;
  20423. struct map_session_data* sd;
  20424. unsigned int start, end;
  20425. // Get the name of the variable
  20426. name = reference_getname(data);
  20427. // Check if it's a string variable
  20428. if( is_string_variable( name ) ){
  20429. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20430. script_reportdata( data );
  20431. st->state = END;
  20432. return SCRIPT_CMD_FAILURE;
  20433. }
  20434. // Get the session data, if a player is attached
  20435. sd = st->rid ? map_id2sd(st->rid) : NULL;
  20436. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20437. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20438. return SCRIPT_CMD_FAILURE;
  20439. }
  20440. // Try to find the array's source pointer
  20441. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20442. ShowError( "buildin_%s: not a array!\n", functionname );
  20443. script_reportdata( data );
  20444. st->state = END;
  20445. return SCRIPT_CMD_FAILURE;
  20446. }
  20447. // Get the start and end indices of the array
  20448. start = reference_getindex( data );
  20449. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20450. // Skip empty arrays
  20451. if( start < end ){
  20452. int id;
  20453. // For getting the values we need the id of the array
  20454. id = reference_getid( data );
  20455. // Loop through each value stored in the array
  20456. for( ; start < end; start++ ){
  20457. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20458. }
  20459. }
  20460. else {
  20461. value = func( value, 0 );
  20462. }
  20463. }else{
  20464. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20465. script_reportdata( data );
  20466. st->state = END;
  20467. return SCRIPT_CMD_FAILURE;
  20468. }
  20469. // Continue with the next stack entry
  20470. i++;
  20471. }
  20472. script_pushint( st, value );
  20473. return SCRIPT_CMD_SUCCESS;
  20474. }
  20475. /**
  20476. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20477. * Unlike `getexp` that affected by some adjustments.
  20478. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20479. * @author [Cydh]
  20480. **/
  20481. BUILDIN_FUNC(getexp2) {
  20482. TBL_PC *sd = NULL;
  20483. int64 base_exp = script_getnum64(st, 2);
  20484. int64 job_exp = script_getnum64(st, 3);
  20485. if (!script_charid2sd(4, sd))
  20486. return SCRIPT_CMD_FAILURE;
  20487. if( base_exp == 0 && job_exp == 0 ){
  20488. ShowError( "buildin_getexp2: Called with base and job exp 0.\n" );
  20489. return SCRIPT_CMD_FAILURE;
  20490. }
  20491. if (base_exp > 0)
  20492. pc_gainexp(sd, NULL, base_exp, 0, 2);
  20493. else if (base_exp < 0)
  20494. pc_lostexp(sd, base_exp * -1, 0);
  20495. if (job_exp > 0)
  20496. pc_gainexp(sd, NULL, 0, job_exp, 2);
  20497. else if (job_exp < 0)
  20498. pc_lostexp(sd, 0, job_exp * -1);
  20499. return SCRIPT_CMD_SUCCESS;
  20500. }
  20501. /**
  20502. * Force stat recalculation of sd
  20503. * recalculatestat;
  20504. * @author [secretdataz]
  20505. **/
  20506. BUILDIN_FUNC(recalculatestat) {
  20507. TBL_PC* sd;
  20508. if (!script_rid2sd(sd))
  20509. return SCRIPT_CMD_FAILURE;
  20510. status_calc_pc(sd, SCO_FORCE);
  20511. return SCRIPT_CMD_SUCCESS;
  20512. }
  20513. BUILDIN_FUNC(hateffect){
  20514. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20515. struct map_session_data* sd;
  20516. if( !script_rid2sd(sd) )
  20517. return SCRIPT_CMD_FAILURE;
  20518. int16 effectID = script_getnum(st,2);
  20519. bool enable = script_getnum(st,3) ? true : false;
  20520. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20521. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20522. return SCRIPT_CMD_FAILURE;
  20523. }
  20524. auto it = util::vector_get( sd->hatEffects, effectID );
  20525. if( enable ){
  20526. if( it != sd->hatEffects.end() ){
  20527. return SCRIPT_CMD_SUCCESS;
  20528. }
  20529. sd->hatEffects.push_back( effectID );
  20530. }else{
  20531. if( it == sd->hatEffects.end() ){
  20532. return SCRIPT_CMD_SUCCESS;
  20533. }
  20534. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20535. }
  20536. if( !sd->state.connect_new ){
  20537. clif_hat_effect_single( sd, effectID, enable );
  20538. }
  20539. #endif
  20540. return SCRIPT_CMD_SUCCESS;
  20541. }
  20542. /**
  20543. * Retrieves param of current random option. Intended for random option script only.
  20544. * getrandomoptinfo(<type>);
  20545. * @author [secretdataz]
  20546. **/
  20547. BUILDIN_FUNC(getrandomoptinfo) {
  20548. struct map_session_data *sd;
  20549. int val;
  20550. 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) {
  20551. int param = script_getnum(st, 2);
  20552. switch (param) {
  20553. case ROA_ID:
  20554. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20555. break;
  20556. case ROA_VALUE:
  20557. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20558. break;
  20559. case ROA_PARAM:
  20560. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  20561. break;
  20562. default:
  20563. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  20564. val = 0;
  20565. break;
  20566. }
  20567. script_pushint(st, val);
  20568. }
  20569. else {
  20570. script_pushint(st, 0);
  20571. }
  20572. return SCRIPT_CMD_SUCCESS;
  20573. }
  20574. /**
  20575. * Retrieves a random option on an equipped item.
  20576. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  20577. * @author [secretdataz]
  20578. */
  20579. BUILDIN_FUNC(getequiprandomoption) {
  20580. struct map_session_data *sd;
  20581. int val;
  20582. short i = -1;
  20583. int pos = script_getnum(st, 2);
  20584. int index = script_getnum(st, 3);
  20585. int type = script_getnum(st, 4);
  20586. if (!script_charid2sd(5, sd)) {
  20587. script_pushint(st, -1);
  20588. return SCRIPT_CMD_FAILURE;
  20589. }
  20590. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20591. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  20592. script_pushint(st, -1);
  20593. return SCRIPT_CMD_FAILURE;
  20594. }
  20595. if (equip_index_check(pos))
  20596. i = pc_checkequip(sd, equip_bitmask[pos]);
  20597. if (i < 0) {
  20598. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20599. script_pushint(st, -1);
  20600. return SCRIPT_CMD_FAILURE;
  20601. }
  20602. switch (type) {
  20603. case ROA_ID:
  20604. val = sd->inventory.u.items_inventory[i].option[index].id;
  20605. break;
  20606. case ROA_VALUE:
  20607. val = sd->inventory.u.items_inventory[i].option[index].value;
  20608. break;
  20609. case ROA_PARAM:
  20610. val = sd->inventory.u.items_inventory[i].option[index].param;
  20611. break;
  20612. default:
  20613. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  20614. val = 0;
  20615. break;
  20616. }
  20617. script_pushint(st, val);
  20618. return SCRIPT_CMD_SUCCESS;
  20619. }
  20620. /**
  20621. * Adds a random option on a random option slot on an equipped item and overwrites
  20622. * existing random option in the process.
  20623. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  20624. * @author [secretdataz]
  20625. */
  20626. BUILDIN_FUNC(setrandomoption) {
  20627. struct map_session_data *sd;
  20628. int pos, index, id, value, param, ep;
  20629. int i = -1;
  20630. if (!script_charid2sd(7, sd))
  20631. return SCRIPT_CMD_FAILURE;
  20632. pos = script_getnum(st, 2);
  20633. index = script_getnum(st, 3);
  20634. id = script_getnum(st, 4);
  20635. value = script_getnum(st, 5);
  20636. param = script_getnum(st, 6);
  20637. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  20638. if (opt == nullptr) {
  20639. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  20640. script_pushint(st, 0);
  20641. return SCRIPT_CMD_FAILURE;
  20642. }
  20643. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20644. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  20645. script_pushint(st, 0);
  20646. return SCRIPT_CMD_FAILURE;
  20647. }
  20648. if (equip_index_check(pos))
  20649. i = pc_checkequip(sd, equip_bitmask[pos]);
  20650. if (i >= 0) {
  20651. ep = sd->inventory.u.items_inventory[i].equip;
  20652. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20653. pc_unequipitem(sd, i, 2);
  20654. sd->inventory.u.items_inventory[i].option[index].id = id;
  20655. sd->inventory.u.items_inventory[i].option[index].value = value;
  20656. sd->inventory.u.items_inventory[i].option[index].param = param;
  20657. clif_delitem(sd, i, 1, 3);
  20658. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20659. clif_additem(sd, i, 1, 0);
  20660. pc_equipitem(sd, i, ep);
  20661. script_pushint(st, 1);
  20662. return SCRIPT_CMD_SUCCESS;
  20663. }
  20664. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20665. script_pushint(st, 0);
  20666. return SCRIPT_CMD_FAILURE;
  20667. }
  20668. /// Returns the number of stat points needed to change the specified stat by val.
  20669. /// If val is negative, returns the number of stat points that would be needed to
  20670. /// raise the specified stat from (current value - val) to current value.
  20671. /// *needed_status_point(<type>,<val>{,<char id>});
  20672. /// @author [secretdataz]
  20673. BUILDIN_FUNC(needed_status_point) {
  20674. struct map_session_data *sd;
  20675. int type, val;
  20676. if (!script_charid2sd(4, sd))
  20677. return SCRIPT_CMD_FAILURE;
  20678. type = script_getnum(st, 2);
  20679. val = script_getnum(st, 3);
  20680. script_pushint(st, pc_need_status_point(sd, type, val));
  20681. return SCRIPT_CMD_SUCCESS;
  20682. }
  20683. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  20684. /// If val is negative, returns the number of trait stat points that would be needed to
  20685. /// raise the specified trait stat from (current value - val) to current value.
  20686. /// *needed_trait_point(<type>,<val>{,<char id>});
  20687. BUILDIN_FUNC(needed_trait_point) {
  20688. struct map_session_data *sd;
  20689. if (!script_charid2sd(4, sd))
  20690. return SCRIPT_CMD_FAILURE;
  20691. int type = script_getnum( st, 2 );
  20692. if( type < SP_POW || type > SP_CRT ){
  20693. ShowError( "buildin_needed_trait_point: Unknown trait type %d\n", type );
  20694. return SCRIPT_CMD_FAILURE;
  20695. }
  20696. script_pushint( st, pc_need_trait_point( sd, type, script_getnum( st, 3 ) ) );
  20697. return SCRIPT_CMD_SUCCESS;
  20698. }
  20699. /**
  20700. * jobcanentermap("<mapname>"{,<JobID>});
  20701. * Check if (player with) JobID can enter the map.
  20702. * @param mapname Map name
  20703. * @param JobID Player's JobID (optional)
  20704. **/
  20705. BUILDIN_FUNC(jobcanentermap) {
  20706. const char *mapname = script_getstr(st, 2);
  20707. int mapidx = mapindex_name2id(mapname), m = -1;
  20708. int jobid = 0;
  20709. TBL_PC *sd = NULL;
  20710. if (!mapidx) {// Invalid map
  20711. script_pushint(st, false);
  20712. return SCRIPT_CMD_FAILURE;
  20713. }
  20714. m = map_mapindex2mapid(mapidx);
  20715. if (m == -1) { // Map is on different map server
  20716. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  20717. script_pushint(st, false);
  20718. return SCRIPT_CMD_FAILURE;
  20719. }
  20720. if (script_hasdata(st, 3)) {
  20721. jobid = script_getnum(st, 3);
  20722. } else {
  20723. if (!script_rid2sd(sd)) {
  20724. script_pushint(st, false);
  20725. return SCRIPT_CMD_FAILURE;
  20726. }
  20727. jobid = sd->status.class_;
  20728. }
  20729. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  20730. return SCRIPT_CMD_SUCCESS;
  20731. }
  20732. /**
  20733. * Return alliance information between the two guilds.
  20734. * getguildalliance(<guild id1>,<guild id2>);
  20735. * Return values:
  20736. * -2 - Guild ID1 does not exist
  20737. * -1 - Guild ID2 does not exist
  20738. * 0 - Both guilds have no relation OR guild ID aren't given
  20739. * 1 - Both guilds are allies
  20740. * 2 - Both guilds are antagonists
  20741. */
  20742. BUILDIN_FUNC(getguildalliance)
  20743. {
  20744. struct guild *guild_data1, *guild_data2;
  20745. int guild_id1, guild_id2, i = 0;
  20746. guild_id1 = script_getnum(st,2);
  20747. guild_id2 = script_getnum(st,3);
  20748. if (guild_id1 < 1 || guild_id2 < 1) {
  20749. script_pushint(st, 0);
  20750. return SCRIPT_CMD_SUCCESS;
  20751. }
  20752. if (guild_id1 == guild_id2) {
  20753. script_pushint(st, 1);
  20754. return SCRIPT_CMD_SUCCESS;
  20755. }
  20756. guild_data1 = guild_search(guild_id1);
  20757. guild_data2 = guild_search(guild_id2);
  20758. if (guild_data1 == NULL) {
  20759. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  20760. script_pushint(st, -2);
  20761. return SCRIPT_CMD_FAILURE;
  20762. }
  20763. if (guild_data2 == NULL) {
  20764. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  20765. script_pushint(st, -1);
  20766. return SCRIPT_CMD_FAILURE;
  20767. }
  20768. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  20769. if (i == MAX_GUILDALLIANCE) {
  20770. script_pushint(st, 0);
  20771. return SCRIPT_CMD_SUCCESS;
  20772. }
  20773. if (guild_data1->alliance[i].opposition)
  20774. script_pushint(st, 2);
  20775. else
  20776. script_pushint(st, 1);
  20777. return SCRIPT_CMD_SUCCESS;
  20778. }
  20779. /*
  20780. * openstorage2 <storage_id>,<mode>{,<account_id>}
  20781. * mode @see enum e_storage_mode
  20782. **/
  20783. BUILDIN_FUNC(openstorage2) {
  20784. int stor_id = script_getnum(st, 2);
  20785. TBL_PC *sd = NULL;
  20786. if (!script_accid2sd(4, sd)) {
  20787. script_pushint(st, 0);
  20788. return SCRIPT_CMD_FAILURE;
  20789. }
  20790. if (!storage_exists(stor_id)) {
  20791. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  20792. return SCRIPT_CMD_FAILURE;
  20793. }
  20794. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  20795. return SCRIPT_CMD_SUCCESS;
  20796. }
  20797. /**
  20798. * Create a new channel
  20799. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  20800. * @author [Cydh]
  20801. **/
  20802. BUILDIN_FUNC(channel_create) {
  20803. struct Channel tmp_chan, *ch = NULL;
  20804. const char *chname = script_getstr(st,2), *pass = NULL;
  20805. int i = channel_chk((char*)chname, NULL, 3);
  20806. TBL_PC *sd = NULL;
  20807. if (i != 0) {
  20808. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  20809. script_pushint(st,i);
  20810. return SCRIPT_CMD_FAILURE;
  20811. }
  20812. memset(&tmp_chan, 0, sizeof(struct Channel));
  20813. if (script_hasdata(st,8)) {
  20814. tmp_chan.char_id = script_getnum(st,8);
  20815. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  20816. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  20817. script_pushint(st,-5);
  20818. return SCRIPT_CMD_FAILURE;
  20819. }
  20820. tmp_chan.type = CHAN_TYPE_PRIVATE;
  20821. i = 1;
  20822. }
  20823. else {
  20824. tmp_chan.type = CHAN_TYPE_PUBLIC;
  20825. i = 0;
  20826. }
  20827. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  20828. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  20829. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  20830. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  20831. if (script_hasdata(st,5))
  20832. tmp_chan.opt = script_getnum(st,5);
  20833. else
  20834. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  20835. if (script_hasdata(st,6))
  20836. tmp_chan.msg_delay = script_getnum(st,6);
  20837. else
  20838. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  20839. if (script_hasdata(st,7))
  20840. tmp_chan.color = script_getnum(st,7);
  20841. else
  20842. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  20843. if (!(ch = channel_create(&tmp_chan))) {
  20844. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  20845. script_pushint(st,0);
  20846. return SCRIPT_CMD_FAILURE;
  20847. }
  20848. if (tmp_chan.char_id)
  20849. channel_join(ch, sd);
  20850. script_pushint(st,1);
  20851. return SCRIPT_CMD_SUCCESS;
  20852. }
  20853. /**
  20854. * Set channel option
  20855. * channel_setopt "<chname>",<option>,<value>;
  20856. * @author [Cydh]
  20857. **/
  20858. BUILDIN_FUNC(channel_setopt) {
  20859. struct Channel *ch = NULL;
  20860. const char *chname = script_getstr(st,2);
  20861. int opt = script_getnum(st,3), value = script_getnum(st,4);
  20862. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20863. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  20864. script_pushint(st,0);
  20865. return SCRIPT_CMD_FAILURE;
  20866. }
  20867. switch (opt) {
  20868. case CHAN_OPT_ANNOUNCE_SELF:
  20869. case CHAN_OPT_ANNOUNCE_JOIN:
  20870. case CHAN_OPT_ANNOUNCE_LEAVE:
  20871. case CHAN_OPT_COLOR_OVERRIDE:
  20872. case CHAN_OPT_CAN_CHAT:
  20873. case CHAN_OPT_CAN_LEAVE:
  20874. case CHAN_OPT_AUTOJOIN:
  20875. if (value)
  20876. ch->opt |= opt;
  20877. else
  20878. ch->opt &= ~opt;
  20879. break;
  20880. case CHAN_OPT_MSG_DELAY:
  20881. ch->msg_delay = value;
  20882. break;
  20883. default:
  20884. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  20885. script_pushint(st,0);
  20886. return SCRIPT_CMD_FAILURE;
  20887. }
  20888. script_pushint(st,1);
  20889. return SCRIPT_CMD_SUCCESS;
  20890. }
  20891. /**
  20892. * Get channel options
  20893. * channel_getopt <chname>,<option>;
  20894. */
  20895. BUILDIN_FUNC(channel_getopt) {
  20896. Channel *ch;
  20897. const char *chname = script_getstr(st, 2);
  20898. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20899. ShowError("buildin_channel_getopt: Channel name '%s' is invalid.\n", chname);
  20900. script_pushint(st, false);
  20901. return SCRIPT_CMD_FAILURE;
  20902. }
  20903. int opt = script_getnum(st, 3);
  20904. switch (opt) {
  20905. case CHAN_OPT_ANNOUNCE_SELF:
  20906. case CHAN_OPT_ANNOUNCE_JOIN:
  20907. case CHAN_OPT_ANNOUNCE_LEAVE:
  20908. case CHAN_OPT_COLOR_OVERRIDE:
  20909. case CHAN_OPT_CAN_CHAT:
  20910. case CHAN_OPT_CAN_LEAVE:
  20911. case CHAN_OPT_AUTOJOIN:
  20912. script_pushint(st, (ch->opt & opt) != 0);
  20913. break;
  20914. case CHAN_OPT_MSG_DELAY:
  20915. script_pushint(st, ch->msg_delay);
  20916. break;
  20917. default:
  20918. ShowError("buildin_channel_getopt: Invalid option %d!\n", opt);
  20919. script_pushint(st, false);
  20920. return SCRIPT_CMD_FAILURE;
  20921. }
  20922. return SCRIPT_CMD_SUCCESS;
  20923. }
  20924. /**
  20925. * Set channel color
  20926. * channel_setcolor "<chname>",<color>;
  20927. * @author [Cydh]
  20928. **/
  20929. BUILDIN_FUNC(channel_setcolor) {
  20930. struct Channel *ch = NULL;
  20931. const char *chname = script_getstr(st,2);
  20932. int color = script_getnum(st,3);
  20933. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20934. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  20935. script_pushint(st,0);
  20936. return SCRIPT_CMD_FAILURE;
  20937. }
  20938. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  20939. script_pushint(st,1);
  20940. return SCRIPT_CMD_SUCCESS;
  20941. }
  20942. /**
  20943. * Set channel password
  20944. * channel_setpass "<chname>","<password>";
  20945. * @author [Cydh]
  20946. **/
  20947. BUILDIN_FUNC(channel_setpass) {
  20948. struct Channel *ch = NULL;
  20949. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  20950. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20951. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  20952. script_pushint(st,0);
  20953. return SCRIPT_CMD_FAILURE;
  20954. }
  20955. if (!passwd || !strcmpi(passwd,"null"))
  20956. memset(ch->pass, '\0', sizeof(ch->pass));
  20957. else
  20958. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  20959. script_pushint(st,1);
  20960. return SCRIPT_CMD_SUCCESS;
  20961. }
  20962. /**
  20963. * Set authorized groups
  20964. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  20965. * @author [Cydh]
  20966. **/
  20967. BUILDIN_FUNC(channel_setgroup) {
  20968. struct Channel *ch = NULL;
  20969. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  20970. int i, n = 0, group = 0;
  20971. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20972. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  20973. script_pushint(st,0);
  20974. return SCRIPT_CMD_FAILURE;
  20975. }
  20976. if (funcname[strlen(funcname)-1] == '2') {
  20977. struct script_data *data = script_getdata(st,3);
  20978. const char *varname = reference_getname(data);
  20979. int32 id, idx;
  20980. if (varname[strlen(varname)-1] == '$') {
  20981. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  20982. script_pushint(st,0);
  20983. return SCRIPT_CMD_FAILURE;
  20984. }
  20985. if (not_server_variable(*varname)) {
  20986. struct map_session_data *sd;
  20987. if (!script_rid2sd(sd)) {
  20988. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  20989. return SCRIPT_CMD_FAILURE;
  20990. }
  20991. }
  20992. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  20993. if (n < 1) {
  20994. ShowError("buildin_channel_setgroup: No group id listed.\n");
  20995. script_pushint(st,0);
  20996. return SCRIPT_CMD_FAILURE;
  20997. }
  20998. if (ch->groups)
  20999. aFree(ch->groups);
  21000. ch->groups = NULL;
  21001. ch->group_count = 0;
  21002. id = reference_getid(data);
  21003. idx = reference_getindex(data);
  21004. for (i = 0; i < n; i++) {
  21005. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  21006. if (group == 0) {
  21007. script_pushint(st,1);
  21008. return SCRIPT_CMD_SUCCESS;
  21009. }
  21010. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  21011. ch->groups[ch->group_count-1] = group;
  21012. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21013. }
  21014. }
  21015. else {
  21016. group = script_getnum(st,3);
  21017. n = script_lastdata(st)-1;
  21018. if (n < 1) {
  21019. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  21020. script_pushint(st,0);
  21021. return SCRIPT_CMD_FAILURE;
  21022. }
  21023. if (ch->groups)
  21024. aFree(ch->groups);
  21025. ch->groups = NULL;
  21026. ch->group_count = 0;
  21027. if (group == 0) { // Removed group list
  21028. script_pushint(st,1);
  21029. return SCRIPT_CMD_SUCCESS;
  21030. }
  21031. CREATE(ch->groups, unsigned short, n);
  21032. for (i = 3; i < n+2; i++) {
  21033. ch->groups[ch->group_count++] = script_getnum(st,i);
  21034. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  21035. }
  21036. }
  21037. script_pushint(st,n);
  21038. return SCRIPT_CMD_SUCCESS;
  21039. }
  21040. /**
  21041. * Send message on channel
  21042. * channel_chat "<chname>","<message>"{,<color>};
  21043. * @author [Cydh]
  21044. **/
  21045. BUILDIN_FUNC(channel_chat) {
  21046. struct Channel *ch = NULL;
  21047. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  21048. char output[CHAT_SIZE_MAX+1];
  21049. unsigned long color = 0;
  21050. // Check also local channels
  21051. if (chname[0] != '#') { // By Map
  21052. int m = mapindex_name2id(chname);
  21053. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  21054. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  21055. script_pushint(st,0);
  21056. return SCRIPT_CMD_FAILURE;
  21057. }
  21058. if (!(ch = map_getmapdata(m)->channel)) {
  21059. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21060. script_pushint(st,0);
  21061. return SCRIPT_CMD_SUCCESS;
  21062. }
  21063. }
  21064. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  21065. TBL_NPC *nd = map_id2nd(st->oid);
  21066. if (!nd || nd->bl.m == -1) {
  21067. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  21068. script_pushint(st,0);
  21069. return SCRIPT_CMD_FAILURE;
  21070. }
  21071. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  21072. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  21073. script_pushint(st,0);
  21074. return SCRIPT_CMD_SUCCESS;
  21075. }
  21076. }
  21077. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21078. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  21079. script_pushint(st,0);
  21080. return SCRIPT_CMD_FAILURE;
  21081. }
  21082. if (!ch) {
  21083. script_pushint(st,0);
  21084. return SCRIPT_CMD_FAILURE;
  21085. }
  21086. color = ch->color;
  21087. FETCH(4, color);
  21088. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  21089. clif_channel_msg(ch,output,color);
  21090. script_pushint(st,1);
  21091. return SCRIPT_CMD_SUCCESS;
  21092. }
  21093. /**
  21094. * Ban player from a channel
  21095. * channel_ban "<chname>",<char_id>;
  21096. * @author [Cydh]
  21097. **/
  21098. BUILDIN_FUNC(channel_ban) {
  21099. struct Channel *ch = NULL;
  21100. const char *chname = script_getstr(st,2);
  21101. unsigned int char_id = script_getnum(st,3);
  21102. TBL_PC *tsd;
  21103. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21104. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  21105. script_pushint(st,0);
  21106. return SCRIPT_CMD_FAILURE;
  21107. }
  21108. if (ch->char_id == char_id) {
  21109. script_pushint(st,0);
  21110. return SCRIPT_CMD_SUCCESS;
  21111. }
  21112. tsd = map_charid2sd(char_id);
  21113. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21114. script_pushint(st,0);
  21115. return SCRIPT_CMD_SUCCESS;
  21116. }
  21117. if (!idb_exists(ch->banned, char_id)) {
  21118. struct chan_banentry *cbe;
  21119. char output[CHAT_SIZE_MAX+1];
  21120. CREATE(cbe, struct chan_banentry, 1);
  21121. cbe->char_id = char_id;
  21122. if (tsd) {
  21123. strcpy(cbe->char_name,tsd->status.name);
  21124. channel_clean(ch,tsd,0);
  21125. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  21126. }
  21127. else
  21128. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  21129. idb_put(ch->banned, char_id, cbe);
  21130. clif_channel_msg(ch,output,ch->color);
  21131. }
  21132. script_pushint(st,1);
  21133. return SCRIPT_CMD_SUCCESS;
  21134. }
  21135. /**
  21136. * Ban player from a channel
  21137. * channel_unban "<chname>",<char_id>;
  21138. * @author [Cydh]
  21139. **/
  21140. BUILDIN_FUNC(channel_unban) {
  21141. struct Channel *ch = NULL;
  21142. const char *chname = script_getstr(st,2);
  21143. unsigned int char_id = script_getnum(st,3);
  21144. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21145. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  21146. script_pushint(st,0);
  21147. return SCRIPT_CMD_FAILURE;
  21148. }
  21149. if (ch->char_id == char_id) {
  21150. script_pushint(st,0);
  21151. return SCRIPT_CMD_SUCCESS;
  21152. }
  21153. if (idb_exists(ch->banned, char_id)) {
  21154. char output[CHAT_SIZE_MAX+1];
  21155. TBL_PC *tsd = map_charid2sd(char_id);
  21156. if (tsd)
  21157. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  21158. else
  21159. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  21160. idb_remove(ch->banned, char_id);
  21161. clif_channel_msg(ch,output,ch->color);
  21162. }
  21163. script_pushint(st,1);
  21164. return SCRIPT_CMD_SUCCESS;
  21165. }
  21166. /**
  21167. * Kick player from a channel
  21168. * channel_kick "<chname>",<char_id>;
  21169. * channel_kick "<chname>","<char_name>";
  21170. * @author [Cydh]
  21171. **/
  21172. BUILDIN_FUNC(channel_kick) {
  21173. struct Channel *ch = NULL;
  21174. const char *chname = script_getstr(st,2);
  21175. TBL_PC *tsd = NULL;
  21176. int res = 1;
  21177. if (script_isstring(st, 3)) {
  21178. const char *name = script_getstr(st, 3);
  21179. if (!(tsd = map_nick2sd(name,false))) {
  21180. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  21181. script_pushint(st,0);
  21182. return SCRIPT_CMD_FAILURE;
  21183. }
  21184. } else {
  21185. int char_id = script_getnum(st, 3);
  21186. if (!(tsd = map_charid2sd(char_id))) {
  21187. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  21188. script_pushint(st,0);
  21189. return SCRIPT_CMD_FAILURE;
  21190. }
  21191. }
  21192. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  21193. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  21194. script_pushint(st,0);
  21195. return SCRIPT_CMD_FAILURE;
  21196. }
  21197. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  21198. script_pushint(st,0);
  21199. return SCRIPT_CMD_SUCCESS;
  21200. }
  21201. switch(ch->type){
  21202. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  21203. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  21204. default: res = channel_clean(ch,tsd,0); break;
  21205. }
  21206. if (res == 0) {
  21207. char output[CHAT_SIZE_MAX+1];
  21208. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  21209. clif_channel_msg(ch,output,ch->color);
  21210. }
  21211. script_pushint(st,1);
  21212. return SCRIPT_CMD_SUCCESS;
  21213. }
  21214. /**
  21215. * Delete a channel
  21216. * channel_delete "<chname>";
  21217. * @author [Cydh]
  21218. **/
  21219. BUILDIN_FUNC(channel_delete) {
  21220. struct Channel *ch = NULL;
  21221. const char *chname = script_getstr(st,2);
  21222. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  21223. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  21224. script_pushint(st,-1);
  21225. return SCRIPT_CMD_FAILURE;
  21226. }
  21227. script_pushint(st,channel_delete(ch,true));
  21228. return SCRIPT_CMD_SUCCESS;
  21229. }
  21230. BUILDIN_FUNC(unloadnpc) {
  21231. const char *name;
  21232. struct npc_data* nd;
  21233. name = script_getstr(st, 2);
  21234. nd = npc_name2id(name);
  21235. if( nd == NULL ){
  21236. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  21237. return SCRIPT_CMD_FAILURE;
  21238. } else if ( nd->bl.id == st->oid ) {
  21239. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  21240. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  21241. return SCRIPT_CMD_FAILURE;
  21242. }
  21243. npc_unload_duplicates(nd);
  21244. npc_unload(nd, true);
  21245. npc_read_event_script();
  21246. return SCRIPT_CMD_SUCCESS;
  21247. }
  21248. /**
  21249. * Add an achievement to the player's log
  21250. * achievementadd(<achievement ID>{,<char ID>});
  21251. */
  21252. BUILDIN_FUNC(achievementadd) {
  21253. struct map_session_data *sd;
  21254. int achievement_id = script_getnum(st, 2);
  21255. if (!script_charid2sd(3, sd)) {
  21256. script_pushint(st, false);
  21257. return SCRIPT_CMD_FAILURE;
  21258. }
  21259. if (achievement_db.exists(achievement_id) == false) {
  21260. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  21261. script_pushint(st, false);
  21262. return SCRIPT_CMD_FAILURE;
  21263. }
  21264. if( !sd->state.pc_loaded ){
  21265. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21266. return SCRIPT_CMD_SUCCESS;
  21267. }
  21268. if (achievement_add(sd, achievement_id))
  21269. script_pushint(st, true);
  21270. else
  21271. script_pushint(st, false);
  21272. return SCRIPT_CMD_SUCCESS;
  21273. }
  21274. /**
  21275. * Removes an achievement on a player.
  21276. * achievementremove(<achievement ID>{,<char ID>});
  21277. * Just for Atemo. ;)
  21278. */
  21279. BUILDIN_FUNC(achievementremove) {
  21280. struct map_session_data *sd;
  21281. int achievement_id = script_getnum(st, 2);
  21282. if (!script_charid2sd(3, sd)) {
  21283. script_pushint(st, false);
  21284. return SCRIPT_CMD_FAILURE;
  21285. }
  21286. if (achievement_db.exists(achievement_id) == false) {
  21287. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  21288. script_pushint(st, false);
  21289. return SCRIPT_CMD_SUCCESS;
  21290. }
  21291. if( !sd->state.pc_loaded ){
  21292. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21293. return SCRIPT_CMD_SUCCESS;
  21294. }
  21295. if (achievement_remove(sd, achievement_id))
  21296. script_pushint(st, true);
  21297. else
  21298. script_pushint(st, false);
  21299. return SCRIPT_CMD_SUCCESS;
  21300. }
  21301. /**
  21302. * Returns achievement progress
  21303. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  21304. */
  21305. BUILDIN_FUNC(achievementinfo) {
  21306. struct map_session_data *sd;
  21307. int achievement_id = script_getnum(st, 2);
  21308. if (!script_charid2sd(4, sd)) {
  21309. script_pushint(st, false);
  21310. return SCRIPT_CMD_FAILURE;
  21311. }
  21312. if (achievement_db.exists(achievement_id) == false) {
  21313. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  21314. script_pushint(st, false);
  21315. return SCRIPT_CMD_FAILURE;
  21316. }
  21317. if( !sd->state.pc_loaded ){
  21318. script_pushint(st, false);
  21319. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21320. return SCRIPT_CMD_SUCCESS;
  21321. }
  21322. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  21323. return SCRIPT_CMD_SUCCESS;
  21324. }
  21325. /**
  21326. * Award an achievement; Ignores requirements
  21327. * achievementcomplete(<achievement ID>{,<char ID>});
  21328. */
  21329. BUILDIN_FUNC(achievementcomplete) {
  21330. struct map_session_data *sd;
  21331. int i, achievement_id = script_getnum(st, 2);
  21332. if (!script_charid2sd(3, sd)) {
  21333. script_pushint(st, false);
  21334. return SCRIPT_CMD_FAILURE;
  21335. }
  21336. if (achievement_db.exists(achievement_id) == false) {
  21337. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  21338. script_pushint(st, false);
  21339. return SCRIPT_CMD_FAILURE;
  21340. }
  21341. if( !sd->state.pc_loaded ){
  21342. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21343. return SCRIPT_CMD_SUCCESS;
  21344. }
  21345. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21346. if (i == sd->achievement_data.count)
  21347. achievement_add(sd, achievement_id);
  21348. achievement_update_achievement(sd, achievement_id, true);
  21349. script_pushint(st, true);
  21350. return SCRIPT_CMD_SUCCESS;
  21351. }
  21352. /**
  21353. * Checks if the achievement exists on player.
  21354. * achievementexists(<achievement ID>{,<char ID>});
  21355. */
  21356. BUILDIN_FUNC(achievementexists) {
  21357. struct map_session_data *sd;
  21358. int i, achievement_id = script_getnum(st, 2);
  21359. if (!script_charid2sd(3, sd)) {
  21360. script_pushint(st, false);
  21361. return SCRIPT_CMD_FAILURE;
  21362. }
  21363. if (achievement_db.exists(achievement_id) == false) {
  21364. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  21365. script_pushint(st, false);
  21366. return SCRIPT_CMD_SUCCESS;
  21367. }
  21368. if( !sd->state.pc_loaded ){
  21369. script_pushint(st, false);
  21370. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21371. return SCRIPT_CMD_SUCCESS;
  21372. }
  21373. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  21374. script_pushint(st, i < sd->achievement_data.count ? true : false);
  21375. return SCRIPT_CMD_SUCCESS;
  21376. }
  21377. /**
  21378. * Updates an achievement's value.
  21379. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  21380. */
  21381. BUILDIN_FUNC(achievementupdate) {
  21382. struct map_session_data *sd;
  21383. int i, achievement_id, type, value;
  21384. achievement_id = script_getnum(st, 2);
  21385. type = script_getnum(st, 3);
  21386. value = script_getnum(st, 4);
  21387. if (!script_charid2sd(5, sd)) {
  21388. script_pushint(st, false);
  21389. return SCRIPT_CMD_FAILURE;
  21390. }
  21391. if (achievement_db.exists(achievement_id) == false) {
  21392. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  21393. script_pushint(st, false);
  21394. return SCRIPT_CMD_FAILURE;
  21395. }
  21396. if( !sd->state.pc_loaded ){
  21397. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  21398. return SCRIPT_CMD_SUCCESS;
  21399. }
  21400. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21401. if (i == sd->achievement_data.count)
  21402. achievement_add(sd, achievement_id);
  21403. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  21404. if (i == sd->achievement_data.count) {
  21405. script_pushint(st, false);
  21406. return SCRIPT_CMD_SUCCESS;
  21407. }
  21408. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  21409. sd->achievement_data.achievements[i].count[type - 1] = value;
  21410. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  21411. sd->achievement_data.achievements[i].completed = value;
  21412. else if (type == ACHIEVEINFO_GOTREWARD)
  21413. sd->achievement_data.achievements[i].rewarded = value;
  21414. else {
  21415. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  21416. script_pushint(st, false);
  21417. return SCRIPT_CMD_FAILURE;
  21418. }
  21419. achievement_update_achievement(sd, achievement_id, false);
  21420. script_pushint(st, true);
  21421. return SCRIPT_CMD_SUCCESS;
  21422. }
  21423. /**
  21424. * Get an equipment's refine cost
  21425. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  21426. * returns -1 on fail
  21427. */
  21428. BUILDIN_FUNC(getequiprefinecost) {
  21429. int i = -1, slot, type, info;
  21430. map_session_data *sd;
  21431. slot = script_getnum(st, 2);
  21432. type = script_getnum(st, 3);
  21433. info = script_getnum(st, 4);
  21434. if (!script_charid2sd(5, sd)) {
  21435. script_pushint(st, 0);
  21436. return SCRIPT_CMD_FAILURE;
  21437. }
  21438. if (type < 0 || type >= REFINE_COST_MAX) {
  21439. script_pushint(st, -1);
  21440. return SCRIPT_CMD_SUCCESS;
  21441. }
  21442. if (equip_index_check(slot))
  21443. i = pc_checkequip(sd, equip_bitmask[slot]);
  21444. if (i < 0) {
  21445. script_pushint(st, -1);
  21446. return SCRIPT_CMD_SUCCESS;
  21447. }
  21448. if( sd->inventory_data[i] == nullptr ){
  21449. script_pushint( st, -1 );
  21450. return SCRIPT_CMD_SUCCESS;
  21451. }
  21452. std::shared_ptr<s_refine_level_info> level_info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  21453. if( level_info == nullptr ){
  21454. script_pushint( st, -1 );
  21455. return SCRIPT_CMD_SUCCESS;
  21456. }
  21457. std::shared_ptr<s_refine_cost> cost = util::umap_find( level_info->costs, (uint16)type );
  21458. if( cost == nullptr ){
  21459. script_pushint( st, -1 );
  21460. return SCRIPT_CMD_SUCCESS;
  21461. }
  21462. switch( info ){
  21463. case REFINE_MATERIAL_ID:
  21464. script_pushint( st, cost->nameid );
  21465. break;
  21466. case REFINE_ZENY_COST:
  21467. script_pushint( st, cost->zeny );
  21468. break;
  21469. default:
  21470. script_pushint( st, -1 );
  21471. return SCRIPT_CMD_FAILURE;
  21472. }
  21473. return SCRIPT_CMD_SUCCESS;
  21474. }
  21475. /**
  21476. * Round, floor, ceiling a number to arbitrary integer precision.
  21477. * round(<number>,<precision>);
  21478. * ceil(<number>,<precision>);
  21479. * floor(<number>,<precision>);
  21480. */
  21481. BUILDIN_FUNC(round) {
  21482. int num = script_getnum(st, 2);
  21483. int precision = script_getnum(st, 3);
  21484. char* func = script_getfuncname(st);
  21485. if (precision <= 0) {
  21486. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  21487. return SCRIPT_CMD_FAILURE;
  21488. }
  21489. if (strcasecmp(func, "floor") == 0) {
  21490. script_pushint(st, num - (num % precision));
  21491. }
  21492. else if (strcasecmp(func, "ceil") == 0) {
  21493. script_pushint(st, num + precision - (num % precision));
  21494. }
  21495. else {
  21496. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  21497. }
  21498. return SCRIPT_CMD_SUCCESS;
  21499. }
  21500. BUILDIN_FUNC(getequiptradability) {
  21501. int i, num;
  21502. TBL_PC *sd;
  21503. num = script_getnum(st, 2);
  21504. if (!script_charid2sd(3, sd)) {
  21505. return SCRIPT_CMD_FAILURE;
  21506. }
  21507. if (equip_index_check(num))
  21508. i = pc_checkequip(sd, equip_bitmask[num]);
  21509. else{
  21510. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  21511. return SCRIPT_CMD_FAILURE;
  21512. }
  21513. if (i >= 0)
  21514. script_pushint(st, pc_can_trade_item(sd, i));
  21515. else
  21516. script_pushint(st, false);
  21517. return SCRIPT_CMD_SUCCESS;
  21518. }
  21519. 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 ){
  21520. const char *name;
  21521. // Check if it is a variable
  21522. if( !data_isreference(data) ){
  21523. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  21524. return false;
  21525. }
  21526. name = reference_getname(data);
  21527. if( is_string_variable(name) ){
  21528. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  21529. return false;
  21530. }
  21531. // Check if the variable requires a player
  21532. if( not_server_variable(*name) && sd == NULL ){
  21533. // If no player is attached
  21534. if( !script_rid2sd(sd) ){
  21535. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  21536. return false;
  21537. }
  21538. }
  21539. // Try to find the array's source pointer
  21540. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  21541. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  21542. return false;
  21543. }
  21544. // Store the name for later usage
  21545. *out_name = name;
  21546. // Get the start and end indices of the array
  21547. *start = reference_getindex(data);
  21548. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  21549. // For getting the values we need the id of the array
  21550. *id = reference_getid(data);
  21551. return true;
  21552. }
  21553. // 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>}}}}}}}}};
  21554. BUILDIN_FUNC(mail){
  21555. const char *sender, *title, *body, *name;
  21556. struct mail_message msg;
  21557. struct script_data *data;
  21558. struct map_session_data *sd = NULL;
  21559. unsigned int i, j, k, num_items, start, end;
  21560. int32 id;
  21561. memset(&msg, 0, sizeof(struct mail_message));
  21562. msg.dest_id = script_getnum(st,2);
  21563. sender = script_getstr(st, 3);
  21564. if( strlen(sender) > NAME_LENGTH ){
  21565. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  21566. return SCRIPT_CMD_FAILURE;
  21567. }
  21568. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  21569. title = script_getstr(st, 4);
  21570. if( strlen(title) > MAIL_TITLE_LENGTH ){
  21571. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  21572. return SCRIPT_CMD_FAILURE;
  21573. }
  21574. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  21575. body = script_getstr(st, 5);
  21576. if( strlen(body) > MAIL_BODY_LENGTH ){
  21577. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  21578. return SCRIPT_CMD_FAILURE;
  21579. }
  21580. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  21581. if( script_hasdata(st,6) ){
  21582. int zeny = script_getnum(st, 6);
  21583. if( zeny < 0 ){
  21584. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  21585. return SCRIPT_CMD_FAILURE;
  21586. }else if( zeny > MAX_ZENY ){
  21587. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  21588. zeny = MAX_ZENY;
  21589. }
  21590. msg.zeny = zeny;
  21591. }
  21592. // Items
  21593. num_items = 0;
  21594. while( script_hasdata(st,7) ){
  21595. data = script_getdata(st,7);
  21596. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  21597. return SCRIPT_CMD_FAILURE;
  21598. }
  21599. num_items = end - start;
  21600. if( num_items == 0 ){
  21601. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  21602. break;
  21603. }
  21604. if( num_items > MAIL_MAX_ITEM ){
  21605. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  21606. num_items = MAIL_MAX_ITEM;
  21607. }
  21608. for( i = 0; i < num_items && start < end; i++, start++ ){
  21609. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21610. msg.item[i].identify = 1;
  21611. if( !itemdb_exists(msg.item[i].nameid) ){
  21612. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  21613. return SCRIPT_CMD_FAILURE;
  21614. }
  21615. }
  21616. // Amounts
  21617. if( !script_hasdata(st,8) ){
  21618. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  21619. return SCRIPT_CMD_FAILURE;
  21620. }
  21621. data = script_getdata(st,8);
  21622. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  21623. return SCRIPT_CMD_FAILURE;
  21624. }
  21625. for( i = 0; i < num_items && start < end; i++, start++ ){
  21626. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21627. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21628. if( msg.item[i].amount <= 0 ){
  21629. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  21630. return SCRIPT_CMD_FAILURE;
  21631. }else if( itemdb_isstackable2(item) ){
  21632. uint16 max = item->stack.amount > 0 ? item->stack.amount : MAX_AMOUNT;
  21633. if( msg.item[i].amount > max ){
  21634. 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 );
  21635. msg.item[i].amount = max;
  21636. }
  21637. }else{
  21638. if( msg.item[i].amount > 1 ){
  21639. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  21640. msg.item[i].amount = 1;
  21641. }
  21642. }
  21643. }
  21644. // Refine
  21645. if (!script_hasdata(st, 9)) {
  21646. break;
  21647. }
  21648. data = script_getdata(st, 9);
  21649. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  21650. return SCRIPT_CMD_FAILURE;
  21651. }
  21652. for (i = 0; i < num_items && start < end; i++, start++) {
  21653. struct item_data* item = itemdb_exists(msg.item[i].nameid);
  21654. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21655. if (!item->flag.no_refine && (item->type == IT_WEAPON || item->type == IT_ARMOR || item->type == IT_SHADOWGEAR)) {
  21656. if (msg.item[i].refine > MAX_REFINE)
  21657. msg.item[i].refine = MAX_REFINE;
  21658. }
  21659. else
  21660. msg.item[i].refine = 0;
  21661. if (msg.item[i].refine < 0)
  21662. msg.item[i].refine = 0;
  21663. }
  21664. // Bound
  21665. if (!script_hasdata(st, 10)) {
  21666. break;
  21667. }
  21668. data = script_getdata(st,10);
  21669. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  21670. return SCRIPT_CMD_FAILURE;
  21671. }
  21672. for( i = 0; i < num_items && start < end; i++, start++ ){
  21673. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21674. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21675. if( msg.item[i].bound < BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  21676. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  21677. return SCRIPT_CMD_FAILURE;
  21678. }
  21679. }
  21680. // Cards
  21681. if( !script_hasdata(st,11) ){
  21682. break;
  21683. }
  21684. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  21685. data = script_getdata(st,j);
  21686. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21687. return SCRIPT_CMD_FAILURE;
  21688. }
  21689. for( k = 0; k < num_items && start < end; k++, start++ ){
  21690. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21691. if( msg.item[k].card[i] != 0 && !itemdb_exists(msg.item[k].card[i]) ){
  21692. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  21693. return SCRIPT_CMD_FAILURE;
  21694. }
  21695. }
  21696. }
  21697. // Random Options
  21698. if( !script_hasdata(st,11 + MAX_SLOTS) ){
  21699. break;
  21700. }
  21701. 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++ ){
  21702. // Option IDs
  21703. data = script_getdata(st, j);
  21704. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21705. return SCRIPT_CMD_FAILURE;
  21706. }
  21707. for( k = 0; k < num_items && start < end; k++, start++ ){
  21708. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21709. }
  21710. j++;
  21711. // Option values
  21712. data = script_getdata(st, j);
  21713. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21714. return SCRIPT_CMD_FAILURE;
  21715. }
  21716. for( k = 0; k < num_items && start < end; k++, start++ ){
  21717. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21718. }
  21719. j++;
  21720. // Option parameters
  21721. data = script_getdata(st, j);
  21722. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21723. return SCRIPT_CMD_FAILURE;
  21724. }
  21725. for( k = 0; k < num_items && start < end; k++, start++ ){
  21726. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21727. }
  21728. }
  21729. // Break the pseudo scope
  21730. break;
  21731. }
  21732. msg.status = MAIL_NEW;
  21733. msg.type = MAIL_INBOX_NORMAL;
  21734. msg.timestamp = time(NULL);
  21735. intif_Mail_send(0, &msg);
  21736. return SCRIPT_CMD_SUCCESS;
  21737. }
  21738. BUILDIN_FUNC(open_roulette){
  21739. #if PACKETVER < 20141022
  21740. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  21741. return SCRIPT_CMD_FAILURE;
  21742. #else
  21743. struct map_session_data* sd;
  21744. if( !battle_config.feature_roulette ){
  21745. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  21746. return SCRIPT_CMD_FAILURE;
  21747. }
  21748. if( !script_charid2sd( 2, sd ) ){
  21749. return SCRIPT_CMD_FAILURE;
  21750. }
  21751. clif_roulette_open(sd);
  21752. return SCRIPT_CMD_SUCCESS;
  21753. #endif
  21754. }
  21755. /*==========================================
  21756. * identifyall({<type>{,<account_id>}})
  21757. * <type>:
  21758. * true: identify the items and returns the count of unidentified items (default)
  21759. * false: returns the count of unidentified items only
  21760. *------------------------------------------*/
  21761. BUILDIN_FUNC(identifyall) {
  21762. TBL_PC *sd;
  21763. bool identify_item = true;
  21764. if (script_hasdata(st, 2))
  21765. identify_item = script_getnum(st, 2) != 0;
  21766. if( !script_hasdata(st, 3) )
  21767. script_rid2sd(sd);
  21768. else
  21769. sd = map_id2sd( script_getnum(st, 3) );
  21770. if (sd == NULL) {
  21771. script_pushint(st, -1);
  21772. return SCRIPT_CMD_SUCCESS;
  21773. }
  21774. script_pushint(st, pc_identifyall(sd, identify_item));
  21775. return SCRIPT_CMD_SUCCESS;
  21776. }
  21777. BUILDIN_FUNC(is_guild_leader)
  21778. {
  21779. struct map_session_data* sd;
  21780. struct guild* guild_data;
  21781. int guild_id;
  21782. if (!script_rid2sd(sd)) {
  21783. script_pushint(st, false);
  21784. return SCRIPT_CMD_FAILURE;
  21785. }
  21786. if (script_hasdata(st, 2))
  21787. guild_id = script_getnum(st, 2);
  21788. else
  21789. guild_id = sd->status.guild_id;
  21790. guild_data = guild_search(guild_id);
  21791. if (guild_data)
  21792. script_pushint(st, (guild_data->member[0].char_id == sd->status.char_id));
  21793. else
  21794. script_pushint(st, false);
  21795. return SCRIPT_CMD_SUCCESS;
  21796. }
  21797. BUILDIN_FUNC(is_party_leader)
  21798. {
  21799. struct map_session_data* sd;
  21800. struct party_data* p_data;
  21801. int p_id, i = 0;
  21802. if (!script_rid2sd(sd)) {
  21803. script_pushint(st, false);
  21804. return SCRIPT_CMD_FAILURE;
  21805. }
  21806. if (script_hasdata(st, 2))
  21807. p_id = script_getnum(st, 2);
  21808. else
  21809. p_id = sd->status.party_id;
  21810. p_data = party_search(p_id);
  21811. if (p_data) {
  21812. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  21813. if (i < MAX_PARTY){
  21814. script_pushint(st, p_data->party.member[i].leader);
  21815. return SCRIPT_CMD_SUCCESS;
  21816. }
  21817. }
  21818. script_pushint(st, false);
  21819. return SCRIPT_CMD_SUCCESS;
  21820. }
  21821. BUILDIN_FUNC( camerainfo ){
  21822. #if PACKETVER < 20160525
  21823. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  21824. return SCRIPT_CMD_FAILURE;
  21825. #else
  21826. struct map_session_data* sd;
  21827. if( !script_charid2sd( 5, sd ) ){
  21828. return SCRIPT_CMD_FAILURE;
  21829. }
  21830. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  21831. return SCRIPT_CMD_SUCCESS;
  21832. #endif
  21833. }
  21834. // This function is only meant to be used inside of achievement conditions
  21835. BUILDIN_FUNC(achievement_condition){
  21836. // Push what we get from the script
  21837. script_pushint( st, 2 );
  21838. // Otherwise the script is freed afterwards
  21839. st->state = RERUNLINE;
  21840. return SCRIPT_CMD_SUCCESS;
  21841. }
  21842. /// Returns a reference to a variable of the specific instance ID.
  21843. /// Returns 0 if an error occurs.
  21844. ///
  21845. /// getinstancevar(<variable>, <instance ID>) -> <reference>
  21846. BUILDIN_FUNC(getinstancevar)
  21847. {
  21848. struct script_data* data = script_getdata(st, 2);
  21849. if (!data_isreference(data)) {
  21850. ShowError("buildin_getinstancevar: %s is not a variable.\n", script_getstr(st, 2));
  21851. script_reportdata(data);
  21852. script_pushnil(st);
  21853. st->state = END;
  21854. return SCRIPT_CMD_FAILURE;
  21855. }
  21856. const char* name = reference_getname(data);
  21857. if (*name != '\'') {
  21858. ShowError("buildin_getinstancevar: Invalid scope. %s is not an instance variable.\n", name);
  21859. script_reportdata(data);
  21860. script_pushnil(st);
  21861. st->state = END;
  21862. return SCRIPT_CMD_FAILURE;
  21863. }
  21864. int instance_id = script_getnum(st, 3);
  21865. if (instance_id <= 0) {
  21866. ShowError("buildin_getinstancevar: Invalid instance ID %d.\n", instance_id);
  21867. script_pushnil(st);
  21868. st->state = END;
  21869. return SCRIPT_CMD_FAILURE;
  21870. }
  21871. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  21872. if (im->state != INSTANCE_BUSY) {
  21873. ShowError("buildin_getinstancevar: Unknown instance ID %d.\n", instance_id);
  21874. script_pushnil(st);
  21875. st->state = END;
  21876. return SCRIPT_CMD_FAILURE;
  21877. }
  21878. if (!im->regs.vars)
  21879. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  21880. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  21881. return SCRIPT_CMD_SUCCESS;
  21882. }
  21883. /// Sets the value of an instance variable.
  21884. ///
  21885. /// setinstancevar(<variable>,<value>,<instance ID>)
  21886. BUILDIN_FUNC(setinstancevar)
  21887. {
  21888. const char *command = script_getfuncname(st);
  21889. struct script_data* data = script_getdata(st, 2);
  21890. if (!data_isreference(data)) {
  21891. ShowError("buildin_%s: %s is not a variable.\n", command, script_getstr(st, 2));
  21892. script_reportdata(data);
  21893. script_pushnil(st);
  21894. st->state = END;
  21895. return SCRIPT_CMD_FAILURE;
  21896. }
  21897. const char* name = reference_getname(data);
  21898. if (*name != '\'') {
  21899. ShowError("buildin_%s: Invalid scope. %s is not an instance variable.\n", command, name);
  21900. script_reportdata(data);
  21901. script_pushnil(st);
  21902. st->state = END;
  21903. return SCRIPT_CMD_FAILURE;
  21904. }
  21905. int instance_id = script_getnum(st, 4);
  21906. if (instance_id <= 0) {
  21907. ShowError("buildin_%s: Invalid instance ID %d.\n", command, instance_id);
  21908. script_pushnil(st);
  21909. st->state = END;
  21910. return SCRIPT_CMD_FAILURE;
  21911. }
  21912. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  21913. if (im->state != INSTANCE_BUSY) {
  21914. ShowError("buildin_%s: Unknown instance ID %d.\n", command, instance_id);
  21915. script_pushnil(st);
  21916. st->state = END;
  21917. return SCRIPT_CMD_FAILURE;
  21918. }
  21919. script_pushcopy(st, 2);
  21920. struct map_session_data* sd = nullptr;
  21921. if( is_string_variable(name) )
  21922. set_reg_str( st, sd, reference_getuid(data), name, script_getstr( st, 3 ), &im->regs );
  21923. else
  21924. set_reg_num( st, sd, reference_getuid(data), name, script_getnum64( st, 3 ), &im->regs );
  21925. return SCRIPT_CMD_SUCCESS;
  21926. }
  21927. /*
  21928. convertpcinfo(<char_id>,<type>)
  21929. convertpcinfo(<account_id>,<type>)
  21930. convertpcinfo(<player_name>,<type>)
  21931. */
  21932. BUILDIN_FUNC(convertpcinfo) {
  21933. TBL_PC *sd;
  21934. if (script_isstring(st, 2))
  21935. sd = map_nick2sd(script_getstr(st, 2),false);
  21936. else {
  21937. int id = script_getnum(st, 2);
  21938. sd = map_id2sd(id);
  21939. if (!sd)
  21940. sd = map_charid2sd(id);
  21941. }
  21942. int type = script_getnum(st, 3);
  21943. switch (type) {
  21944. case CPC_NAME:
  21945. case CPC_CHAR:
  21946. case CPC_ACCOUNT:
  21947. break;
  21948. default:
  21949. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  21950. script_pushnil(st);
  21951. st->state = END;
  21952. return SCRIPT_CMD_FAILURE;
  21953. }
  21954. if (!sd) {
  21955. if (type == CPC_NAME)
  21956. script_pushstrcopy(st, "");
  21957. else
  21958. script_pushint(st, 0);
  21959. return SCRIPT_CMD_SUCCESS;
  21960. }
  21961. switch (type) {
  21962. case CPC_NAME:
  21963. script_pushstrcopy(st, sd->status.name);
  21964. break;
  21965. case CPC_CHAR:
  21966. script_pushint(st, sd->status.char_id);
  21967. break;
  21968. case CPC_ACCOUNT:
  21969. script_pushint(st, sd->status.account_id);
  21970. break;
  21971. }
  21972. return SCRIPT_CMD_SUCCESS;
  21973. }
  21974. BUILDIN_FUNC(isnpccloaked)
  21975. {
  21976. npc_data *nd;
  21977. if (script_hasdata(st, 2))
  21978. nd = npc_name2id(script_getstr(st, 2));
  21979. else
  21980. nd = map_id2nd(st->oid);
  21981. if (!nd) {
  21982. if (script_hasdata(st, 2))
  21983. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  21984. else
  21985. ShowError("buildin_isnpccloaked: non-existing NPC.\n");
  21986. return SCRIPT_CMD_FAILURE;
  21987. }
  21988. map_session_data* sd;
  21989. if (!script_charid2sd(3, sd))
  21990. return SCRIPT_CMD_FAILURE;
  21991. script_pushint(st, npc_is_cloaked(nd, sd));
  21992. return SCRIPT_CMD_SUCCESS;
  21993. }
  21994. BUILDIN_FUNC(refineui){
  21995. #if PACKETVER < 20161012
  21996. ShowError( "buildin_refineui: This command requires packet version 2016-10-12 or newer.\n" );
  21997. return SCRIPT_CMD_FAILURE;
  21998. #else
  21999. struct map_session_data* sd;
  22000. if( !script_charid2sd(2,sd) ){
  22001. return SCRIPT_CMD_FAILURE;
  22002. }
  22003. if( !battle_config.feature_refineui ){
  22004. ShowError( "buildin_refineui: This command is disabled via configuration.\n" );
  22005. return SCRIPT_CMD_FAILURE;
  22006. }
  22007. if( !sd->state.refineui_open ){
  22008. clif_refineui_open(sd);
  22009. }
  22010. return SCRIPT_CMD_SUCCESS;
  22011. #endif
  22012. }
  22013. BUILDIN_FUNC(getenchantgrade){
  22014. struct map_session_data *sd;
  22015. if (!script_charid2sd(3, sd)) {
  22016. script_pushint(st,-1);
  22017. return SCRIPT_CMD_FAILURE;
  22018. }
  22019. int index, position;
  22020. if (script_hasdata(st, 2))
  22021. position = script_getnum(st, 2);
  22022. else
  22023. position = EQI_COMPOUND_ON;
  22024. if (position == EQI_COMPOUND_ON)
  22025. index = current_equip_item_index;
  22026. else if (equip_index_check(position))
  22027. index = pc_checkequip(sd, equip_bitmask[position]);
  22028. else {
  22029. ShowError( "buildin_getenchantgrade: Unknown equip index '%d'\n", position );
  22030. script_pushint(st,-1);
  22031. return SCRIPT_CMD_FAILURE;
  22032. }
  22033. if (index < 0 || index >= MAX_INVENTORY || sd->inventory.u.items_inventory[index].nameid == 0)
  22034. script_pushint(st, -1);
  22035. else
  22036. script_pushint(st, sd->inventory.u.items_inventory[index].enchantgrade);
  22037. return SCRIPT_CMD_SUCCESS;
  22038. }
  22039. /*==========================================
  22040. * mob_setidleevent( <monster game ID>, "<event label>" )
  22041. *------------------------------------------*/
  22042. BUILDIN_FUNC(mob_setidleevent){
  22043. struct block_list* bl;
  22044. if( !script_rid2bl( 2, bl ) ){
  22045. return SCRIPT_CMD_FAILURE;
  22046. }
  22047. if( bl->type != BL_MOB ){
  22048. ShowError( "buildin_mob_setidleevent: the target GID was not a monster.\n" );
  22049. return SCRIPT_CMD_FAILURE;
  22050. }
  22051. struct mob_data* md = (struct mob_data*)bl;
  22052. if (md == nullptr)
  22053. return SCRIPT_CMD_FAILURE;
  22054. const char* idle_event = script_getstr( st, 3 );
  22055. check_event( st, idle_event );
  22056. safestrncpy( md->idle_event, idle_event, EVENT_NAME_LENGTH );
  22057. return SCRIPT_CMD_SUCCESS;
  22058. }
  22059. BUILDIN_FUNC( openstylist ){
  22060. #if PACKETVER >= 20151104
  22061. struct map_session_data* sd;
  22062. if( !script_charid2sd( 2, sd ) ){
  22063. return SCRIPT_CMD_FAILURE;
  22064. }
  22065. clif_ui_open( sd, OUT_UI_STYLIST, 0 );
  22066. return SCRIPT_CMD_SUCCESS;
  22067. #else
  22068. ShowError( "buildin_openstylist: This command requires packet version 2015-11-04 or newer.\n" );
  22069. return SCRIPT_CMD_FAILURE;
  22070. #endif
  22071. }
  22072. #include "../custom/script.inc"
  22073. // declarations that were supposed to be exported from npc_chat.cpp
  22074. #ifdef PCRE_SUPPORT
  22075. BUILDIN_FUNC(defpattern);
  22076. BUILDIN_FUNC(activatepset);
  22077. BUILDIN_FUNC(deactivatepset);
  22078. BUILDIN_FUNC(deletepset);
  22079. #endif
  22080. /** Regular expression matching
  22081. * preg_match(<pattern>,<string>{,<offset>})
  22082. */
  22083. BUILDIN_FUNC(preg_match) {
  22084. #ifdef PCRE_SUPPORT
  22085. pcre *re;
  22086. pcre_extra *pcreExtra;
  22087. const char *error;
  22088. int erroffset, r, offset = 0;
  22089. int subStrVec[30];
  22090. const char* pattern = script_getstr(st,2);
  22091. const char* subject = script_getstr(st,3);
  22092. if (script_hasdata(st,4))
  22093. offset = script_getnum(st,4);
  22094. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  22095. pcreExtra = pcre_study(re, 0, &error);
  22096. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  22097. pcre_free(re);
  22098. if (pcreExtra != NULL)
  22099. pcre_free(pcreExtra);
  22100. if (r < 0)
  22101. script_pushint(st,0);
  22102. else
  22103. script_pushint(st,(r > 0) ? r : 30 / 3);
  22104. return SCRIPT_CMD_SUCCESS;
  22105. #else
  22106. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  22107. script_pushint(st,0);
  22108. return SCRIPT_CMD_SUCCESS;
  22109. #endif
  22110. }
  22111. /// script command definitions
  22112. /// for an explanation on args, see add_buildin_func
  22113. struct script_function buildin_func[] = {
  22114. // NPC interaction
  22115. BUILDIN_DEF(mes,"s*"),
  22116. BUILDIN_DEF(next,""),
  22117. BUILDIN_DEF(clear,""),
  22118. BUILDIN_DEF(close,""),
  22119. BUILDIN_DEF(close2,""),
  22120. BUILDIN_DEF(menu,"sl*"),
  22121. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  22122. BUILDIN_DEF(prompt,"s*"),
  22123. //
  22124. BUILDIN_DEF(goto,"l"),
  22125. BUILDIN_DEF(callsub,"l*"),
  22126. BUILDIN_DEF(callfunc,"s*"),
  22127. BUILDIN_DEF(return,"?"),
  22128. BUILDIN_DEF(getarg,"i?"),
  22129. BUILDIN_DEF(jobchange,"i??"),
  22130. BUILDIN_DEF(jobname,"i"),
  22131. BUILDIN_DEF(input,"r??"),
  22132. BUILDIN_DEF(warp,"sii?"),
  22133. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  22134. BUILDIN_DEF(areawarp,"siiiisii??"),
  22135. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  22136. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  22137. BUILDIN_DEF(setlook,"ii?"),
  22138. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  22139. BUILDIN_DEF2(setr,"set","rv?"),
  22140. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  22141. BUILDIN_DEF(setarray,"rv*"),
  22142. BUILDIN_DEF(cleararray,"rvi"),
  22143. BUILDIN_DEF(copyarray,"rri"),
  22144. BUILDIN_DEF(getarraysize,"r"),
  22145. BUILDIN_DEF(deletearray,"r?"),
  22146. BUILDIN_DEF(getelementofarray,"ri"),
  22147. BUILDIN_DEF(inarray,"rv"),
  22148. BUILDIN_DEF(countinarray,"rr"),
  22149. BUILDIN_DEF(getitem,"vi?"),
  22150. BUILDIN_DEF(rentitem,"vi?"),
  22151. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  22152. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  22153. BUILDIN_DEF(getnameditem,"vv"),
  22154. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  22155. BUILDIN_DEF(makeitem,"visii?"),
  22156. BUILDIN_DEF(makeitem2,"visiiiiiiiii?"),
  22157. BUILDIN_DEF(delitem,"vi?"),
  22158. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  22159. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  22160. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  22161. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  22162. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  22163. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  22164. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  22165. BUILDIN_DEF(delitemidx,"i??"),
  22166. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  22167. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  22168. BUILDIN_DEF(cutin,"si"),
  22169. BUILDIN_DEF(viewpoint,"iiiii?"),
  22170. BUILDIN_DEF(heal,"ii?"),
  22171. BUILDIN_DEF(healap,"i?"),
  22172. BUILDIN_DEF(itemheal,"ii?"),
  22173. BUILDIN_DEF(percentheal,"ii?"),
  22174. BUILDIN_DEF(rand,"i?"),
  22175. BUILDIN_DEF(countitem,"v?"),
  22176. BUILDIN_DEF(storagecountitem,"v?"),
  22177. BUILDIN_DEF(guildstoragecountitem,"v?"),
  22178. BUILDIN_DEF(cartcountitem,"v?"),
  22179. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  22180. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  22181. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  22182. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  22183. BUILDIN_DEF(checkweight,"vi*"),
  22184. BUILDIN_DEF(checkweight2,"rr"),
  22185. BUILDIN_DEF(readparam,"i?"),
  22186. BUILDIN_DEF(getcharid,"i?"),
  22187. BUILDIN_DEF(getnpcid,"i?"),
  22188. BUILDIN_DEF(getpartyname,"i"),
  22189. BUILDIN_DEF(getpartymember,"i??"),
  22190. BUILDIN_DEF(getpartyleader,"i?"),
  22191. BUILDIN_DEF(getguildname,"i"),
  22192. BUILDIN_DEF(getguildmaster,"i"),
  22193. BUILDIN_DEF(getguildmasterid,"i"),
  22194. BUILDIN_DEF(strcharinfo,"i?"),
  22195. BUILDIN_DEF(strnpcinfo,"i"),
  22196. BUILDIN_DEF(getequipid,"??"),
  22197. BUILDIN_DEF(getequipuniqueid,"i?"),
  22198. BUILDIN_DEF(getequipname,"i?"),
  22199. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  22200. BUILDIN_DEF(repair,"i?"), // [Valaris]
  22201. BUILDIN_DEF(repairall,"?"),
  22202. BUILDIN_DEF(getequipisequiped,"i?"),
  22203. BUILDIN_DEF(getequipisenableref,"i?"),
  22204. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  22205. BUILDIN_DEF(getequipweaponlv,"??"),
  22206. BUILDIN_DEF(getequiparmorlv, "??"),
  22207. BUILDIN_DEF(getequippercentrefinery,"i?"),
  22208. BUILDIN_DEF(successrefitem,"i??"),
  22209. BUILDIN_DEF(failedrefitem,"i?"),
  22210. BUILDIN_DEF(downrefitem,"i??"),
  22211. BUILDIN_DEF(statusup,"i?"),
  22212. BUILDIN_DEF(statusup2,"ii?"),
  22213. BUILDIN_DEF(traitstatusup,"i?"),
  22214. BUILDIN_DEF(traitstatusup2,"ii?"),
  22215. BUILDIN_DEF(bonus,"i?"),
  22216. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  22217. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  22218. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  22219. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  22220. BUILDIN_DEF(autobonus,"sii??"),
  22221. BUILDIN_DEF(autobonus2,"sii??"),
  22222. BUILDIN_DEF(autobonus3,"siiv?"),
  22223. BUILDIN_DEF(skill,"vi?"),
  22224. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  22225. BUILDIN_DEF(guildskill,"vi"),
  22226. BUILDIN_DEF(getskilllv,"v"),
  22227. BUILDIN_DEF(getgdskilllv,"iv"),
  22228. BUILDIN_DEF(basicskillcheck,""),
  22229. BUILDIN_DEF(getgmlevel,"?"),
  22230. BUILDIN_DEF(getgroupid,"?"),
  22231. BUILDIN_DEF(end,""),
  22232. BUILDIN_DEF(checkoption,"i?"),
  22233. BUILDIN_DEF(setoption,"i??"),
  22234. BUILDIN_DEF(setcart,"??"),
  22235. BUILDIN_DEF(checkcart,"?"),
  22236. BUILDIN_DEF(setfalcon,"??"),
  22237. BUILDIN_DEF(checkfalcon,"?"),
  22238. BUILDIN_DEF(setriding,"??"),
  22239. BUILDIN_DEF(checkriding,"?"),
  22240. BUILDIN_DEF(checkwug,"?"),
  22241. BUILDIN_DEF(checkmadogear,"?"),
  22242. BUILDIN_DEF(setmadogear,"???"),
  22243. BUILDIN_DEF2(savepoint,"save","sii???"),
  22244. BUILDIN_DEF(savepoint,"sii???"),
  22245. BUILDIN_DEF(gettimetick,"i"),
  22246. BUILDIN_DEF(gettime,"i"),
  22247. BUILDIN_DEF(gettimestr,"si?"),
  22248. BUILDIN_DEF(openstorage,""),
  22249. BUILDIN_DEF(guildopenstorage,""),
  22250. BUILDIN_DEF(guildopenstorage_log,"?"),
  22251. BUILDIN_DEF(guild_has_permission,"i?"),
  22252. BUILDIN_DEF(itemskill,"vi?"),
  22253. BUILDIN_DEF(produce,"i"),
  22254. BUILDIN_DEF(cooking,"i"),
  22255. BUILDIN_DEF(monster,"siisii???"),
  22256. BUILDIN_DEF(getmobdrops,"i"),
  22257. BUILDIN_DEF(areamonster,"siiiisii???"),
  22258. BUILDIN_DEF(killmonster,"ss?"),
  22259. BUILDIN_DEF(killmonsterall,"s?"),
  22260. BUILDIN_DEF(clone,"siisi????"),
  22261. BUILDIN_DEF(doevent,"s"),
  22262. BUILDIN_DEF(donpcevent,"s"),
  22263. BUILDIN_DEF(cmdothernpc,"ss"),
  22264. BUILDIN_DEF(addtimer,"is"),
  22265. BUILDIN_DEF(deltimer,"s"),
  22266. BUILDIN_DEF(addtimercount,"is"),
  22267. BUILDIN_DEF(initnpctimer,"??"),
  22268. BUILDIN_DEF(stopnpctimer,"??"),
  22269. BUILDIN_DEF(startnpctimer,"??"),
  22270. BUILDIN_DEF(setnpctimer,"i?"),
  22271. BUILDIN_DEF(getnpctimer,"i?"),
  22272. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  22273. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  22274. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  22275. BUILDIN_DEF(announce,"si??????"),
  22276. BUILDIN_DEF(mapannounce,"ssi?????"),
  22277. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  22278. BUILDIN_DEF(getusers,"i"),
  22279. BUILDIN_DEF(getmapguildusers,"si"),
  22280. BUILDIN_DEF(getmapusers,"s"),
  22281. BUILDIN_DEF(getareausers,"siiii"),
  22282. BUILDIN_DEF(getunits, "i?"),
  22283. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  22284. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  22285. BUILDIN_DEF(getareadropitem,"siiiiv"),
  22286. BUILDIN_DEF(enablenpc,"?"),
  22287. BUILDIN_DEF2(enablenpc, "disablenpc", "?"),
  22288. BUILDIN_DEF2(enablenpc, "hideoffnpc", "?"),
  22289. BUILDIN_DEF2(enablenpc, "hideonnpc", "?"),
  22290. BUILDIN_DEF2(enablenpc, "cloakoffnpc", "??"),
  22291. BUILDIN_DEF2(enablenpc, "cloakonnpc", "??"),
  22292. BUILDIN_DEF(sc_start,"iii???"),
  22293. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  22294. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  22295. BUILDIN_DEF(sc_end,"i?"),
  22296. BUILDIN_DEF(sc_end_class,"??"),
  22297. BUILDIN_DEF(getstatus, "i??"),
  22298. BUILDIN_DEF(getscrate,"ii?"),
  22299. BUILDIN_DEF(debugmes,"s"),
  22300. BUILDIN_DEF(errormes,"s"),
  22301. BUILDIN_DEF2(catchpet,"pet","i"),
  22302. BUILDIN_DEF2(birthpet,"bpet",""),
  22303. BUILDIN_DEF(catchpet,"i"),
  22304. BUILDIN_DEF(birthpet,""),
  22305. BUILDIN_DEF(resetlvl,"i?"),
  22306. BUILDIN_DEF(resetstatus,"?"),
  22307. BUILDIN_DEF(resetskill,"?"),
  22308. BUILDIN_DEF(resetfeel,"?"),
  22309. BUILDIN_DEF(resethate,"?"),
  22310. BUILDIN_DEF(skillpointcount,"?"),
  22311. BUILDIN_DEF(changebase,"i?"),
  22312. BUILDIN_DEF(changesex,"?"),
  22313. BUILDIN_DEF(changecharsex,"?"),
  22314. BUILDIN_DEF(waitingroom,"si?????"),
  22315. BUILDIN_DEF(delwaitingroom,"?"),
  22316. BUILDIN_DEF(waitingroomkick,"ss"),
  22317. BUILDIN_DEF(getwaitingroomusers, "?"),
  22318. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  22319. BUILDIN_DEF(enablewaitingroomevent,"?"),
  22320. BUILDIN_DEF(disablewaitingroomevent,"?"),
  22321. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  22322. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  22323. BUILDIN_DEF(getwaitingroomstate,"i?"),
  22324. BUILDIN_DEF(warpwaitingpc,"sii?"),
  22325. BUILDIN_DEF(attachrid,"i?"),
  22326. BUILDIN_DEF(addrid,"i?????"),
  22327. BUILDIN_DEF(detachrid,""),
  22328. BUILDIN_DEF(isloggedin,"i?"),
  22329. BUILDIN_DEF(setmapflagnosave,"ssii"),
  22330. BUILDIN_DEF(getmapflag,"si?"),
  22331. BUILDIN_DEF(setmapflag,"si??"),
  22332. BUILDIN_DEF(removemapflag,"si?"),
  22333. BUILDIN_DEF(pvpon,"s"),
  22334. BUILDIN_DEF(pvpoff,"s"),
  22335. BUILDIN_DEF(gvgon,"s"),
  22336. BUILDIN_DEF(gvgoff,"s"),
  22337. BUILDIN_DEF(emotion,"i?"),
  22338. BUILDIN_DEF(maprespawnguildid,"sii"),
  22339. BUILDIN_DEF(agitstart,""), // <Agit>
  22340. BUILDIN_DEF(agitend,""),
  22341. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  22342. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  22343. BUILDIN_DEF(getcastlename,"s"),
  22344. BUILDIN_DEF(getcastledata,"si"),
  22345. BUILDIN_DEF(setcastledata,"sii"),
  22346. BUILDIN_DEF(requestguildinfo,"i?"),
  22347. BUILDIN_DEF(getequipcardcnt,"i"),
  22348. BUILDIN_DEF(successremovecards,"i"),
  22349. BUILDIN_DEF(failedremovecards,"ii"),
  22350. BUILDIN_DEF(marriage,"s"),
  22351. BUILDIN_DEF2(wedding_effect,"wedding",""),
  22352. BUILDIN_DEF(divorce,"?"),
  22353. BUILDIN_DEF(ispartneron,"?"),
  22354. BUILDIN_DEF(getpartnerid,"?"),
  22355. BUILDIN_DEF(getchildid,"?"),
  22356. BUILDIN_DEF(getmotherid,"?"),
  22357. BUILDIN_DEF(getfatherid,"?"),
  22358. BUILDIN_DEF(warppartner,"sii"),
  22359. BUILDIN_DEF(getitemname,"v"),
  22360. BUILDIN_DEF(getitemslots,"i"),
  22361. BUILDIN_DEF(makepet,"i"),
  22362. BUILDIN_DEF(getexp,"ii?"),
  22363. BUILDIN_DEF(getinventorylist,"?"),
  22364. BUILDIN_DEF(getskilllist,"?"),
  22365. BUILDIN_DEF(clearitem,"?"),
  22366. BUILDIN_DEF(classchange,"i??"),
  22367. BUILDIN_DEF(misceffect,"i"),
  22368. BUILDIN_DEF(playBGM,"s"),
  22369. BUILDIN_DEF(playBGMall,"s?????"),
  22370. BUILDIN_DEF(soundeffect,"si"),
  22371. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  22372. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  22373. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  22374. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  22375. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  22376. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  22377. BUILDIN_DEF(petloot,"i"), // [Valaris]
  22378. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  22379. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  22380. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  22381. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  22382. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  22383. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  22384. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  22385. BUILDIN_DEF(removespecialeffect,"i??"),
  22386. BUILDIN_DEF2(removespecialeffect,"removespecialeffect2","i??"),
  22387. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  22388. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  22389. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  22390. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  22391. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  22392. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  22393. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  22394. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  22395. BUILDIN_DEF(mobcount,"ss"),
  22396. BUILDIN_DEF(getlook,"i?"),
  22397. BUILDIN_DEF(getsavepoint,"i?"),
  22398. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  22399. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  22400. BUILDIN_DEF(npcstop,""), // [Valaris]
  22401. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  22402. BUILDIN_DEF(mapid2name,"i"),
  22403. BUILDIN_DEF(checkoption1,"i?"),
  22404. BUILDIN_DEF(checkoption2,"i?"),
  22405. BUILDIN_DEF(guildgetexp,"i"),
  22406. BUILDIN_DEF(guildchangegm,"is"),
  22407. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  22408. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  22409. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  22410. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  22411. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  22412. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  22413. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  22414. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  22415. BUILDIN_DEF(night,""), // sets the server to night time
  22416. BUILDIN_DEF(day,""), // sets the server to day time
  22417. #ifdef PCRE_SUPPORT
  22418. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  22419. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  22420. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  22421. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  22422. #endif
  22423. BUILDIN_DEF(preg_match,"ss?"),
  22424. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  22425. BUILDIN_DEF(recovery,"i???"),
  22426. BUILDIN_DEF(getpetinfo,"i?"),
  22427. BUILDIN_DEF(gethominfo,"i?"),
  22428. BUILDIN_DEF(addhomintimacy,"i?"),
  22429. BUILDIN_DEF(getmercinfo,"i?"),
  22430. BUILDIN_DEF(checkequipedcard,"i"),
  22431. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  22432. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  22433. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  22434. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  22435. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  22436. BUILDIN_DEF(charat,"si"),
  22437. BUILDIN_DEF(setchar,"ssi"),
  22438. BUILDIN_DEF(insertchar,"ssi"),
  22439. BUILDIN_DEF(delchar,"si"),
  22440. BUILDIN_DEF(strtoupper,"s"),
  22441. BUILDIN_DEF(strtolower,"s"),
  22442. BUILDIN_DEF(charisupper, "si"),
  22443. BUILDIN_DEF(charislower, "si"),
  22444. BUILDIN_DEF(substr,"sii"),
  22445. BUILDIN_DEF(explode, "rss"),
  22446. BUILDIN_DEF(implode, "r?"),
  22447. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  22448. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  22449. BUILDIN_DEF(strpos,"ss?"),
  22450. BUILDIN_DEF(replacestr,"sss??"),
  22451. BUILDIN_DEF(countstr,"ss?"),
  22452. BUILDIN_DEF(setnpcdisplay,"sv??"),
  22453. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  22454. BUILDIN_DEF(strcmp,"ss"),
  22455. BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
  22456. BUILDIN_DEF(setiteminfo,"vii"), //[Lupus] set Items Buy / sell Price, etc info
  22457. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  22458. // [zBuffer] List of mathematics commands --->
  22459. BUILDIN_DEF(sqrt,"i"),
  22460. BUILDIN_DEF(pow,"ii"),
  22461. BUILDIN_DEF(distance,"iiii"),
  22462. BUILDIN_DEF2(minmax,"min", "*"),
  22463. BUILDIN_DEF2(minmax,"minimum", "*"),
  22464. BUILDIN_DEF2(minmax,"max", "*"),
  22465. BUILDIN_DEF2(minmax,"maximum", "*"),
  22466. // <--- [zBuffer] List of mathematics commands
  22467. BUILDIN_DEF(md5,"s"),
  22468. // [zBuffer] List of dynamic var commands --->
  22469. BUILDIN_DEF(getd,"s"),
  22470. BUILDIN_DEF(setd,"sv?"),
  22471. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  22472. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  22473. BUILDIN_DEF(npcshopadditem,"sii*"),
  22474. BUILDIN_DEF(npcshopdelitem,"si*"),
  22475. BUILDIN_DEF(npcshopattach,"s?"),
  22476. BUILDIN_DEF(equip,"i?"),
  22477. BUILDIN_DEF(autoequip,"ii"),
  22478. BUILDIN_DEF(setbattleflag,"si?"),
  22479. BUILDIN_DEF(getbattleflag,"s"),
  22480. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  22481. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  22482. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  22483. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  22484. BUILDIN_DEF(addmonsterdrop,"vii??"), //Akinari [Lupus]
  22485. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  22486. BUILDIN_DEF(axtoi,"s"),
  22487. BUILDIN_DEF(query_sql,"s*"),
  22488. BUILDIN_DEF(query_logsql,"s*"),
  22489. BUILDIN_DEF(escape_sql,"v"),
  22490. BUILDIN_DEF(atoi,"s"),
  22491. BUILDIN_DEF(strtol,"si"),
  22492. // [zBuffer] List of player cont commands --->
  22493. BUILDIN_DEF(rid2name,"i"),
  22494. BUILDIN_DEF(pcfollow,"ii"),
  22495. BUILDIN_DEF(pcstopfollow,"i"),
  22496. BUILDIN_DEF(pcblockmove,"ii"),
  22497. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  22498. BUILDIN_DEF(pcblockskill,"ii"),
  22499. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  22500. BUILDIN_DEF(setpcblock, "ii?"),
  22501. BUILDIN_DEF(getpcblock, "?"),
  22502. // <--- [zBuffer] List of player cont commands
  22503. // [zBuffer] List of unit control commands --->
  22504. BUILDIN_DEF(unitexists,"i"),
  22505. BUILDIN_DEF(getunittype,"i"),
  22506. BUILDIN_DEF(getunitname,"i"),
  22507. BUILDIN_DEF(setunitname,"is"),
  22508. BUILDIN_DEF(setunittitle,"is"),
  22509. BUILDIN_DEF(getunittitle,"i"),
  22510. BUILDIN_DEF(getunitdata,"i*"),
  22511. BUILDIN_DEF(setunitdata,"iii"),
  22512. BUILDIN_DEF(unitwalk,"iii?"),
  22513. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  22514. BUILDIN_DEF(unitkill,"i"),
  22515. BUILDIN_DEF(unitwarp,"isii"),
  22516. BUILDIN_DEF(unitattack,"iv?"),
  22517. BUILDIN_DEF(unitstopattack,"i"),
  22518. BUILDIN_DEF(unitstopwalk,"i?"),
  22519. BUILDIN_DEF(unittalk,"is?"),
  22520. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  22521. BUILDIN_DEF(unitskilluseid,"ivi????"), // originally by Qamera [Celest]
  22522. BUILDIN_DEF(unitskillusepos,"iviii???"), // [Celest]
  22523. // <--- [zBuffer] List of unit control commands
  22524. BUILDIN_DEF(sleep,"i"),
  22525. BUILDIN_DEF(sleep2,"i"),
  22526. BUILDIN_DEF(awake,"s"),
  22527. BUILDIN_DEF(getvariableofnpc,"rs"),
  22528. BUILDIN_DEF(warpportal,"iisii"),
  22529. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  22530. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  22531. BUILDIN_DEF(morphembryo,""),
  22532. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  22533. BUILDIN_DEF(checkhomcall,""),
  22534. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  22535. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  22536. BUILDIN_DEF(checkvending,"?"),
  22537. BUILDIN_DEF(checkchatting,"?"),
  22538. BUILDIN_DEF(checkidle,"?"),
  22539. BUILDIN_DEF(checkidlehom,"?"),
  22540. BUILDIN_DEF(checkidlemer,"?"),
  22541. BUILDIN_DEF(openmail,"?"),
  22542. BUILDIN_DEF(openauction,"?"),
  22543. BUILDIN_DEF(checkcell,"siii"),
  22544. BUILDIN_DEF(setcell,"siiiiii"),
  22545. BUILDIN_DEF(getfreecell,"srr?????"),
  22546. BUILDIN_DEF(setwall,"siiiiis"),
  22547. BUILDIN_DEF(delwall,"s"),
  22548. BUILDIN_DEF(checkwall,"s"),
  22549. BUILDIN_DEF(searchitem,"rs"),
  22550. BUILDIN_DEF(mercenary_create,"ii"),
  22551. BUILDIN_DEF(mercenary_delete,"??"),
  22552. BUILDIN_DEF(mercenary_heal,"ii"),
  22553. BUILDIN_DEF(mercenary_sc_start,"iii"),
  22554. BUILDIN_DEF(mercenary_get_calls,"i"),
  22555. BUILDIN_DEF(mercenary_get_faith,"i"),
  22556. BUILDIN_DEF(mercenary_set_calls,"ii"),
  22557. BUILDIN_DEF(mercenary_set_faith,"ii"),
  22558. BUILDIN_DEF(readbook,"ii"),
  22559. BUILDIN_DEF(setfont,"i"),
  22560. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  22561. BUILDIN_DEF(progressbar,"si"),
  22562. BUILDIN_DEF(progressbar_npc, "si?"),
  22563. BUILDIN_DEF(pushpc,"ii"),
  22564. BUILDIN_DEF(buyingstore,"i"),
  22565. BUILDIN_DEF(searchstores,"ii"),
  22566. BUILDIN_DEF(showdigit,"i?"),
  22567. // WoE SE
  22568. BUILDIN_DEF(agitstart2,""),
  22569. BUILDIN_DEF(agitend2,""),
  22570. BUILDIN_DEF(agitcheck2,""),
  22571. // BattleGround
  22572. BUILDIN_DEF(waitingroom2bg,"sii???"),
  22573. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  22574. BUILDIN_DEF(bg_team_setxy,"iii"),
  22575. BUILDIN_DEF(bg_warp,"isii"),
  22576. BUILDIN_DEF(bg_monster,"isiisi?"),
  22577. BUILDIN_DEF(bg_monster_set_team,"ii"),
  22578. BUILDIN_DEF(bg_leave,"?"),
  22579. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  22580. BUILDIN_DEF(bg_destroy,"i"),
  22581. BUILDIN_DEF(areapercentheal,"siiiiii"),
  22582. BUILDIN_DEF(bg_get_data,"ii"),
  22583. BUILDIN_DEF(bg_getareausers,"isiiii"),
  22584. BUILDIN_DEF(bg_updatescore,"sii"),
  22585. BUILDIN_DEF(bg_join,"i????"),
  22586. BUILDIN_DEF(bg_create,"sii??"),
  22587. BUILDIN_DEF(bg_reserve,"s?"),
  22588. BUILDIN_DEF(bg_unbook,"s"),
  22589. BUILDIN_DEF(bg_info,"si"),
  22590. // Instancing
  22591. BUILDIN_DEF(instance_create,"s??"),
  22592. BUILDIN_DEF(instance_destroy,"?"),
  22593. BUILDIN_DEF(instance_id,"?"),
  22594. BUILDIN_DEF(instance_enter,"s????"),
  22595. BUILDIN_DEF(instance_npcname,"s?"),
  22596. BUILDIN_DEF(instance_mapname,"s?"),
  22597. BUILDIN_DEF(instance_warpall,"sii?"),
  22598. BUILDIN_DEF(instance_announce,"isi?????"),
  22599. BUILDIN_DEF(instance_check_party,"i???"),
  22600. BUILDIN_DEF(instance_check_guild,"i???"),
  22601. BUILDIN_DEF(instance_check_clan,"i???"),
  22602. BUILDIN_DEF(instance_info,"si?"),
  22603. BUILDIN_DEF(instance_live_info,"i?"),
  22604. BUILDIN_DEF(instance_list, "s?"),
  22605. /**
  22606. * 3rd-related
  22607. **/
  22608. BUILDIN_DEF(makerune,"i?"),
  22609. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  22610. BUILDIN_DEF(setdragon,"??"),//[Ind]
  22611. BUILDIN_DEF(ismounting,"?"),//[Ind]
  22612. BUILDIN_DEF(setmounting,"?"),//[Ind]
  22613. BUILDIN_DEF(checkre,"i"),
  22614. /**
  22615. * rAthena and beyond!
  22616. **/
  22617. BUILDIN_DEF(getargcount,""),
  22618. BUILDIN_DEF(getcharip,"?"),
  22619. BUILDIN_DEF(is_function,"s"),
  22620. BUILDIN_DEF(get_revision,""),
  22621. BUILDIN_DEF(get_githash,""),
  22622. BUILDIN_DEF(freeloop,"?"),
  22623. BUILDIN_DEF(getrandgroupitem,"i????"),
  22624. BUILDIN_DEF(cleanmap,"s"),
  22625. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  22626. BUILDIN_DEF(npcskill,"viii"),
  22627. BUILDIN_DEF(consumeitem,"v?"),
  22628. BUILDIN_DEF(delequip,"i?"),
  22629. BUILDIN_DEF(breakequip,"i?"),
  22630. BUILDIN_DEF(sit,"?"),
  22631. BUILDIN_DEF(stand,"?"),
  22632. //@commands (script based)
  22633. BUILDIN_DEF(bindatcmd, "ss??"),
  22634. BUILDIN_DEF(unbindatcmd, "s"),
  22635. BUILDIN_DEF(useatcmd, "s"),
  22636. //Quest Log System [Inkfish]
  22637. BUILDIN_DEF(questinfo, "i??"),
  22638. BUILDIN_DEF(setquest, "i?"),
  22639. BUILDIN_DEF(erasequest, "i?"),
  22640. BUILDIN_DEF(completequest, "i?"),
  22641. BUILDIN_DEF(checkquest, "i??"),
  22642. BUILDIN_DEF(isbegin_quest,"i?"),
  22643. BUILDIN_DEF(changequest, "ii?"),
  22644. BUILDIN_DEF(showevent, "i??"),
  22645. BUILDIN_DEF(questinfo_refresh, "?"),
  22646. //Bound items [Xantara] & [Akinari]
  22647. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  22648. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  22649. BUILDIN_DEF(countbound, "??"),
  22650. // Party related
  22651. BUILDIN_DEF(party_create,"s???"),
  22652. BUILDIN_DEF(party_addmember,"ii"),
  22653. BUILDIN_DEF(party_delmember,"??"),
  22654. BUILDIN_DEF(party_changeleader,"ii"),
  22655. BUILDIN_DEF(party_changeoption,"iii"),
  22656. BUILDIN_DEF(party_destroy,"i"),
  22657. // Clan system
  22658. BUILDIN_DEF(clan_join,"i?"),
  22659. BUILDIN_DEF(clan_leave,"?"),
  22660. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  22661. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  22662. BUILDIN_DEF(vip_status,"i?"),
  22663. BUILDIN_DEF(vip_time,"i?"),
  22664. BUILDIN_DEF(bonus_script,"si????"),
  22665. BUILDIN_DEF(bonus_script_clear,"??"),
  22666. BUILDIN_DEF(getgroupitem,"i??"),
  22667. BUILDIN_DEF(enable_command,""),
  22668. BUILDIN_DEF(disable_command,""),
  22669. BUILDIN_DEF(getguildmember,"i??"),
  22670. BUILDIN_DEF(addspiritball,"ii?"),
  22671. BUILDIN_DEF(delspiritball,"i?"),
  22672. BUILDIN_DEF(countspiritball,"?"),
  22673. BUILDIN_DEF(mergeitem,"?"),
  22674. BUILDIN_DEF(mergeitem2,"??"),
  22675. BUILDIN_DEF(npcshopupdate,"sii?"),
  22676. BUILDIN_DEF(getattachedrid,""),
  22677. BUILDIN_DEF(getvar,"vi"),
  22678. BUILDIN_DEF(showscript,"s??"),
  22679. BUILDIN_DEF(ignoretimeout,"i?"),
  22680. BUILDIN_DEF(geteleminfo,"i?"),
  22681. BUILDIN_DEF(opendressroom,"i?"),
  22682. BUILDIN_DEF(navigateto,"s???????"),
  22683. BUILDIN_DEF(getguildalliance,"ii"),
  22684. BUILDIN_DEF(adopt,"vv"),
  22685. BUILDIN_DEF(getexp2,"ii?"),
  22686. BUILDIN_DEF(recalculatestat,""),
  22687. BUILDIN_DEF(hateffect,"ii"),
  22688. BUILDIN_DEF(getrandomoptinfo, "i"),
  22689. BUILDIN_DEF(getequiprandomoption, "iii?"),
  22690. BUILDIN_DEF(setrandomoption,"iiiii?"),
  22691. BUILDIN_DEF(needed_status_point,"ii?"),
  22692. BUILDIN_DEF(needed_trait_point, "ii?"),
  22693. BUILDIN_DEF(jobcanentermap,"s?"),
  22694. BUILDIN_DEF(openstorage2,"ii?"),
  22695. BUILDIN_DEF(unloadnpc, "s"),
  22696. // WoE TE
  22697. BUILDIN_DEF(agitstart3,""),
  22698. BUILDIN_DEF(agitend3,""),
  22699. BUILDIN_DEF(agitcheck3,""),
  22700. BUILDIN_DEF(gvgon3,"s"),
  22701. BUILDIN_DEF(gvgoff3,"s"),
  22702. // Channel System
  22703. BUILDIN_DEF(channel_create,"ss?????"),
  22704. BUILDIN_DEF(channel_setopt,"sii"),
  22705. BUILDIN_DEF(channel_getopt,"si"),
  22706. BUILDIN_DEF(channel_setcolor,"si"),
  22707. BUILDIN_DEF(channel_setpass,"ss"),
  22708. BUILDIN_DEF(channel_setgroup,"si*"),
  22709. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  22710. BUILDIN_DEF(channel_chat,"ss?"),
  22711. BUILDIN_DEF(channel_ban,"si"),
  22712. BUILDIN_DEF(channel_unban,"si"),
  22713. BUILDIN_DEF(channel_kick,"sv"),
  22714. BUILDIN_DEF(channel_delete,"s"),
  22715. // Item Random Option Extension [Cydh]
  22716. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  22717. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  22718. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  22719. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr?"),
  22720. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  22721. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  22722. // Achievement System
  22723. BUILDIN_DEF(achievementinfo,"ii?"),
  22724. BUILDIN_DEF(achievementadd,"i?"),
  22725. BUILDIN_DEF(achievementremove,"i?"),
  22726. BUILDIN_DEF(achievementcomplete,"i?"),
  22727. BUILDIN_DEF(achievementexists,"i?"),
  22728. BUILDIN_DEF(achievementupdate,"iii?"),
  22729. BUILDIN_DEF(getequiprefinecost,"iii?"),
  22730. BUILDIN_DEF(refineui,"?"),
  22731. BUILDIN_DEF2(round, "round", "ii"),
  22732. BUILDIN_DEF2(round, "ceil", "ii"),
  22733. BUILDIN_DEF2(round, "floor", "ii"),
  22734. BUILDIN_DEF(getequiptradability, "i?"),
  22735. BUILDIN_DEF(mail, "isss*"),
  22736. BUILDIN_DEF(open_roulette,"?"),
  22737. BUILDIN_DEF(identifyall,"??"),
  22738. BUILDIN_DEF(is_guild_leader,"?"),
  22739. BUILDIN_DEF(is_party_leader,"?"),
  22740. BUILDIN_DEF(camerainfo,"iii?"),
  22741. BUILDIN_DEF(achievement_condition,"i"),
  22742. BUILDIN_DEF(getinstancevar,"ri"),
  22743. BUILDIN_DEF2_DEPRECATED(getinstancevar, "getvariableofinstance","ri", "2021-12-13"),
  22744. BUILDIN_DEF(convertpcinfo,"vi"),
  22745. BUILDIN_DEF(isnpccloaked, "??"),
  22746. BUILDIN_DEF(rentalcountitem, "v?"),
  22747. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  22748. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  22749. BUILDIN_DEF(getenchantgrade, "??"),
  22750. BUILDIN_DEF(mob_setidleevent, "is"),
  22751. BUILDIN_DEF(setinstancevar,"rvi"),
  22752. BUILDIN_DEF(openstylist, "?"),
  22753. #include "../custom/script_def.inc"
  22754. {NULL,NULL,NULL},
  22755. };