script.cpp 694 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766
  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. if( bl->m >= 0 )
  447. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map_mapid2mapname(bl->m), bl->x, bl->y);
  448. else
  449. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  450. break;
  451. default:
  452. if( bl->m >= 0 )
  453. 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);
  454. else
  455. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  456. break;
  457. }
  458. }
  459. /// Reports on the console information about the script data.
  460. static void script_reportdata(struct script_data* data)
  461. {
  462. if( data == NULL )
  463. return;
  464. switch( data->type ) {
  465. case C_NOP:// no value
  466. ShowDebug("Data: nothing (nil)\n");
  467. break;
  468. case C_INT:// number
  469. ShowDebug("Data: number value=%" PRId64 "\n", data->u.num);
  470. break;
  471. case C_STR:
  472. case C_CONSTSTR:// string
  473. if( data->u.str ) {
  474. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  475. } else {
  476. ShowDebug("Data: string value=NULL\n");
  477. }
  478. break;
  479. case C_NAME:// reference
  480. if( reference_tovariable(data) ) {// variable
  481. const char* name = reference_getname(data);
  482. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  483. } else if( reference_toconstant(data) ) {// constant
  484. ShowDebug("Data: constant name='%s' value=%" PRId64 "\n", reference_getname(data), reference_getconstant(data));
  485. } else if( reference_toparam(data) ) {// param
  486. ShowDebug("Data: param name='%s' type=%" PRId64 "\n", reference_getname(data), reference_getparamtype(data));
  487. } else {// ???
  488. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  489. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  490. }
  491. break;
  492. case C_POS:// label
  493. ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num);
  494. break;
  495. default:
  496. ShowDebug("Data: %s\n", script_op2name(data->type));
  497. break;
  498. }
  499. }
  500. /// Reports on the console information about the current built-in function.
  501. static void script_reportfunc(struct script_state* st)
  502. {
  503. int params, id;
  504. struct script_data* data;
  505. if( !script_hasdata(st,0) )
  506. {// no stack
  507. return;
  508. }
  509. data = script_getdata(st,0);
  510. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  511. {// script currently not executing a built-in function or corrupt stack
  512. return;
  513. }
  514. id = reference_getid(data);
  515. params = script_lastdata(st)-1;
  516. if( params > 0 )
  517. {
  518. int i;
  519. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  520. for( i = 2; i <= script_lastdata(st); i++ )
  521. {
  522. script_reportdata(script_getdata(st,i));
  523. }
  524. }
  525. else
  526. {
  527. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  528. }
  529. }
  530. /*==========================================
  531. * Output error message
  532. *------------------------------------------*/
  533. static void disp_error_message2(const char *mes,const char *pos,int report)
  534. {
  535. error_msg = aStrdup(mes);
  536. error_pos = pos;
  537. error_report = report;
  538. longjmp( error_jump, 1 );
  539. }
  540. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  541. static void disp_warning_message(const char *mes, const char *pos) {
  542. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  543. }
  544. /// Checks event parameter validity
  545. static void check_event(struct script_state *st, const char *evt)
  546. {
  547. if( evt && evt[0] && !stristr(evt, "::On") )
  548. {
  549. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  550. script_reportsrc(st);
  551. }
  552. }
  553. /*==========================================
  554. * Hashes the input string
  555. *------------------------------------------*/
  556. static unsigned int calc_hash(const char* p)
  557. {
  558. unsigned int h;
  559. #if defined(SCRIPT_HASH_DJB2)
  560. h = 5381;
  561. while( *p ) // hash*33 + c
  562. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  563. #elif defined(SCRIPT_HASH_SDBM)
  564. h = 0;
  565. while( *p ) // hash*65599 + c
  566. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  567. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  568. h = 0;
  569. while( *p ){
  570. unsigned int g;
  571. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  572. g = h & 0xF0000000;
  573. if( g )
  574. {
  575. h ^= g >> 24;
  576. h &= ~g;
  577. }
  578. }
  579. #else // athena hash
  580. h = 0;
  581. while( *p )
  582. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  583. #endif
  584. return h % SCRIPT_HASH_SIZE;
  585. }
  586. bool script_check_RegistryVariableLength(int pType, const char *val, size_t* vlen)
  587. {
  588. size_t len = strlen(val);
  589. if (vlen)
  590. *vlen = len;
  591. switch (pType) {
  592. case 0:
  593. return (len < 33); // key check
  594. case 1:
  595. return (len < 255); // value check
  596. default:
  597. return false;
  598. }
  599. }
  600. /*==========================================
  601. * str_data manipulation functions
  602. *------------------------------------------*/
  603. /// Looks up string using the provided id.
  604. const char* get_str(int id)
  605. {
  606. Assert( id >= LABEL_START && id < str_size );
  607. return str_buf+str_data[id].str;
  608. }
  609. /// Returns the uid of the string, or -1.
  610. static int search_str(const char* p)
  611. {
  612. int i;
  613. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  614. if( strcasecmp(get_str(i),p) == 0 )
  615. return i;
  616. return -1;
  617. }
  618. /// Stores a copy of the string and returns its id.
  619. /// If an identical string is already present, returns its id instead.
  620. int add_str(const char* p)
  621. {
  622. int h;
  623. int len;
  624. h = calc_hash(p);
  625. if( str_hash[h] == 0 )
  626. {// empty bucket, add new node here
  627. str_hash[h] = str_num;
  628. }
  629. else
  630. {// scan for end of list, or occurence of identical string
  631. int i;
  632. for( i = str_hash[h]; ; i = str_data[i].next )
  633. {
  634. if( strcasecmp(get_str(i),p) == 0 )
  635. return i; // string already in list
  636. if( str_data[i].next == 0 )
  637. break; // reached the end
  638. }
  639. // append node to end of list
  640. str_data[i].next = str_num;
  641. }
  642. // grow list if neccessary
  643. if( str_num >= str_data_size )
  644. {
  645. str_data_size += 128;
  646. RECREATE(str_data,struct str_data_struct,str_data_size);
  647. memset(str_data + (str_data_size - 128), '\0', 128);
  648. }
  649. len=(int)strlen(p);
  650. // grow string buffer if neccessary
  651. while( str_pos+len+1 >= str_size )
  652. {
  653. str_size += 256;
  654. RECREATE(str_buf,char,str_size);
  655. memset(str_buf + (str_size - 256), '\0', 256);
  656. }
  657. safestrncpy(str_buf+str_pos, p, len+1);
  658. str_data[str_num].type = C_NOP;
  659. str_data[str_num].str = str_pos;
  660. str_data[str_num].next = 0;
  661. str_data[str_num].func = NULL;
  662. str_data[str_num].backpatch = -1;
  663. str_data[str_num].label = -1;
  664. str_pos += len+1;
  665. return str_num++;
  666. }
  667. /// Appends 1 byte to the script buffer.
  668. static void add_scriptb(uint8 a)
  669. {
  670. if( script_pos+1 >= script_size )
  671. {
  672. script_size += SCRIPT_BLOCK_SIZE;
  673. RECREATE(script_buf,unsigned char,script_size);
  674. }
  675. script_buf[script_pos++] = a;
  676. }
  677. /// Appends a c_op value to the script buffer.
  678. /// The value is variable-length encoded into 8-bit blocks.
  679. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  680. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  681. static void add_scriptc(int a)
  682. {
  683. while( a >= 0x40 )
  684. {
  685. add_scriptb((a&0x3f)|0x40);
  686. a = (a - 0x40) >> 6;
  687. }
  688. add_scriptb(a);
  689. }
  690. /// Appends an integer value to the script buffer.
  691. /// The value is variable-length encoded into 8-bit blocks.
  692. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  693. /// All blocks hold 6 bits of data.
  694. static void add_scripti(int64 a){
  695. while( a > 0x3f ){
  696. add_scriptb((a & (int64)0x3f)|(int64)0xc0);
  697. a >>= 6;
  698. }
  699. add_scriptb(((a & (int64)0x3f)|(int64)0x80));
  700. }
  701. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  702. ///
  703. /// @param l The id of the str_data entry
  704. // Maximum up to 16M
  705. static void add_scriptl(int l)
  706. {
  707. int backpatch = str_data[l].backpatch;
  708. switch(str_data[l].type){
  709. case C_POS:
  710. case C_USERFUNC_POS:
  711. add_scriptc(C_POS);
  712. add_scriptb(str_data[l].label);
  713. add_scriptb(str_data[l].label>>8);
  714. add_scriptb(str_data[l].label>>16);
  715. break;
  716. case C_NOP:
  717. case C_USERFUNC:
  718. // Embedded data backpatch there is a possibility of label
  719. add_scriptc(C_NAME);
  720. str_data[l].backpatch = script_pos;
  721. add_scriptb(backpatch);
  722. add_scriptb(backpatch>>8);
  723. add_scriptb(backpatch>>16);
  724. break;
  725. case C_INT:
  726. add_scripti(std::abs(str_data[l].val));
  727. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  728. add_scriptc(C_NEG);
  729. break;
  730. default: // assume C_NAME
  731. add_scriptc(C_NAME);
  732. add_scriptb(l);
  733. add_scriptb(l>>8);
  734. add_scriptb(l>>16);
  735. break;
  736. }
  737. }
  738. /*==========================================
  739. * Resolve the label
  740. *------------------------------------------*/
  741. void set_label(int l,int pos, const char* script_pos_cur)
  742. {
  743. int i;
  744. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  745. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  746. disp_error_message("set_label: invalid label name",script_pos_cur);
  747. return;
  748. }
  749. if(str_data[l].label!=-1){
  750. disp_error_message("set_label: dup label ",script_pos_cur);
  751. return;
  752. }
  753. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  754. str_data[l].label=pos;
  755. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  756. int next=GETVALUE(script_buf,i);
  757. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  758. SETVALUE(script_buf,i,pos);
  759. i=next;
  760. }
  761. }
  762. /// Skips spaces and/or comments.
  763. const char* skip_space(const char* p)
  764. {
  765. if( p == NULL )
  766. return NULL;
  767. for(;;)
  768. {
  769. while( ISSPACE(*p) )
  770. ++p;
  771. if( *p == '/' && p[1] == '/' )
  772. {// line comment
  773. while(*p && *p!='\n')
  774. ++p;
  775. }
  776. else if( *p == '/' && p[1] == '*' )
  777. {// block comment
  778. p += 2;
  779. for(;;)
  780. {
  781. if( *p == '\0' ) {
  782. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p);
  783. return p;
  784. }
  785. if( *p == '*' && p[1] == '/' )
  786. {// end of block comment
  787. p += 2;
  788. break;
  789. }
  790. ++p;
  791. }
  792. }
  793. else
  794. break;
  795. }
  796. return p;
  797. }
  798. /// Skips a word.
  799. /// A word consists of undercores and/or alphanumeric characters,
  800. /// and valid variable prefixes/postfixes.
  801. static const char* skip_word(const char* p)
  802. {
  803. // prefix
  804. switch( *p ) {
  805. case '@':// temporary char variable
  806. ++p; break;
  807. case '#':// account variable
  808. p += ( p[1] == '#' ? 2 : 1 ); break;
  809. case '\'':// instance variable
  810. ++p; break;
  811. case '.':// npc variable
  812. p += ( p[1] == '@' ? 2 : 1 ); break;
  813. case '$':// global variable
  814. p += ( p[1] == '@' ? 2 : 1 ); break;
  815. }
  816. while( ISALNUM(*p) || *p == '_' )
  817. ++p;
  818. // postfix
  819. if( *p == '$' )// string
  820. p++;
  821. return p;
  822. }
  823. /// Adds a word to str_data.
  824. /// @see skip_word
  825. /// @see add_str
  826. static int add_word(const char* p)
  827. {
  828. char* word;
  829. int len;
  830. int i;
  831. // Check for a word
  832. len = skip_word(p) - p;
  833. if( len == 0 )
  834. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  835. // Duplicate the word
  836. word = (char*)aMalloc(len+1);
  837. memcpy(word, p, len);
  838. word[len] = 0;
  839. // add the word
  840. i = add_str(word);
  841. aFree(word);
  842. return i;
  843. }
  844. /// Parses a function call.
  845. /// The argument list can have parenthesis or not.
  846. /// The number of arguments is checked.
  847. static
  848. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  849. {
  850. const char* p2;
  851. const char* arg=NULL;
  852. int func;
  853. func = add_word(p);
  854. if( str_data[func].type == C_FUNC ){
  855. // buildin function
  856. add_scriptl(func);
  857. add_scriptc(C_ARG);
  858. arg = buildin_func[str_data[func].val].arg;
  859. #if defined(SCRIPT_COMMAND_DEPRECATION)
  860. if( str_data[func].deprecated ){
  861. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  862. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  863. }
  864. #endif
  865. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  866. // script defined function
  867. add_scriptl(buildin_callsub_ref);
  868. add_scriptc(C_ARG);
  869. add_scriptl(func);
  870. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  871. if( *arg == 0 )
  872. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  873. if( *arg != '*' )
  874. ++arg; // count func as argument
  875. } else {
  876. const char* name = get_str(func);
  877. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  878. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  879. } else {;
  880. add_scriptl(buildin_callfunc_ref);
  881. add_scriptc(C_ARG);
  882. add_scriptc(C_STR);
  883. while( *name ) add_scriptb(*name ++);
  884. add_scriptb(0);
  885. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  886. if( *arg != '*' ) ++ arg;
  887. }
  888. }
  889. p = skip_word(p);
  890. p = skip_space(p);
  891. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  892. syntax.curly[syntax.curly_count].count = 0;
  893. if( *p == ';' )
  894. {// <func name> ';'
  895. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  896. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  897. {// <func name> '(' ')'
  898. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  899. p = p2;
  900. /*
  901. } else if( 0 && require_paren && *p != '(' )
  902. {// <func name>
  903. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  904. */
  905. } else
  906. {// <func name> <arg list>
  907. if( require_paren ){
  908. if( *p != '(' )
  909. disp_error_message("need '('",p);
  910. ++p; // skip '('
  911. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  912. } else if( *p == '(' ){
  913. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  914. } else {
  915. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  916. }
  917. ++syntax.curly_count;
  918. while( *arg ) {
  919. p2=parse_subexpr(p,-1);
  920. if( p == p2 )
  921. break; // not an argument
  922. if( *arg != '*' )
  923. ++arg; // next argument
  924. p=skip_space(p2);
  925. if( *arg == 0 || *p != ',' )
  926. break; // no more arguments
  927. ++p; // skip comma
  928. }
  929. --syntax.curly_count;
  930. }
  931. if( *arg && *arg != '?' && *arg != '*' )
  932. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  933. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  934. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  935. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  936. if( *p != ')' )
  937. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  938. ++p;
  939. }
  940. add_scriptc(C_FUNC);
  941. return p;
  942. }
  943. /// Processes end of logical script line.
  944. /// @param first When true, only fix up scheduling data is initialized
  945. /// @param p Script position for error reporting in set_label
  946. static void parse_nextline(bool first, const char* p)
  947. {
  948. if( !first )
  949. {
  950. add_scriptc(C_EOL); // mark end of line for stack cleanup
  951. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  952. }
  953. // initialize data for new '-' label fix up scheduling
  954. str_data[LABEL_NEXTLINE].type = C_NOP;
  955. str_data[LABEL_NEXTLINE].backpatch = -1;
  956. str_data[LABEL_NEXTLINE].label = -1;
  957. }
  958. /**
  959. * Pushes a variable into stack, processing its array index if needed.
  960. * @see parse_variable
  961. */
  962. void parse_variable_sub_push(int word, const char *p2)
  963. {
  964. if( p2 ) { // Process the variable index
  965. const char *p3 = NULL;
  966. // Push the getelementofarray method into the stack
  967. add_scriptl(buildin_getelementofarray_ref);
  968. add_scriptc(C_ARG);
  969. add_scriptl(word);
  970. // Process the sub-expression for this assignment
  971. p3 = parse_subexpr(p2 + 1, 1);
  972. p3 = skip_space(p3);
  973. if( *p3 != ']' ) // Closing parenthesis is required for this script
  974. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  975. // Push the closing function stack operator onto the stack
  976. add_scriptc(C_FUNC);
  977. p3++;
  978. } else // No array index, simply push the variable or value onto the stack
  979. add_scriptl(word);
  980. }
  981. /// Parse a variable assignment using the direct equals operator
  982. /// @param p script position where the function should run from
  983. /// @return NULL if not a variable assignment, the new position otherwise
  984. const char* parse_variable(const char* p) {
  985. int word;
  986. c_op type = C_NOP;
  987. const char *p2 = NULL;
  988. const char *var = p;
  989. if( p[0] == '+' && p[1] == '+' ){
  990. type = C_ADD_PRE; // pre ++
  991. }else if( p[0] == '-' && p[1] == '-' ){
  992. type = C_SUB_PRE; // pre --
  993. }
  994. if( type != C_NOP ){
  995. var = p = skip_space(&p[2]);
  996. }
  997. // skip the variable where applicable
  998. p = skip_word(p);
  999. p = skip_space(p);
  1000. if( p == NULL ) {// end of the line or invalid buffer
  1001. return NULL;
  1002. }
  1003. if( *p == '[' ) {// array variable so process the array as appropriate
  1004. int i,j;
  1005. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1006. if( *p ++ == ']' && --(j) == 0 ) break;
  1007. if( *p == '[' ) ++ j;
  1008. }
  1009. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1010. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1011. }
  1012. }
  1013. if( type == C_NOP ){
  1014. if( p[0] == '=' && p[1] != '=' ){
  1015. type = C_EQ; // =
  1016. }else if( p[0] == '+' && p[1] == '=' ){
  1017. type = C_ADD; // +=
  1018. }else if( p[0] == '-' && p[1] == '=' ){
  1019. type = C_SUB; // -=
  1020. }else if( p[0] == '^' && p[1] == '=' ){
  1021. type = C_XOR; // ^=
  1022. }else if( p[0] == '|' && p[1] == '=' ){
  1023. type = C_OR; // |=
  1024. }else if( p[0] == '&' && p[1] == '=' ){
  1025. type = C_AND; // &=
  1026. }else if( p[0] == '*' && p[1] == '=' ){
  1027. type = C_MUL; // *=
  1028. }else if( p[0] == '/' && p[1] == '=' ){
  1029. type = C_DIV; // /=
  1030. }else if( p[0] == '%' && p[1] == '=' ){
  1031. type = C_MOD; // %=
  1032. }else if( p[0] == '~' && p[1] == '=' ){
  1033. type = C_NOT; // ~=
  1034. }else if( p[0] == '+' && p[1] == '+' ){
  1035. type = C_ADD_POST; // post ++
  1036. }else if( p[0] == '-' && p[1] == '-' ){
  1037. type = C_SUB_POST; // post --
  1038. }else if( p[0] == '<' && p[1] == '<' && p[2] == '=' ){
  1039. type = C_L_SHIFT; // <<=
  1040. }else if( p[0] == '>' && p[1] == '>' && p[2] == '=' ){
  1041. type = C_R_SHIFT; // >>=
  1042. }else{
  1043. // failed to find a matching operator combination so invalid
  1044. return nullptr;
  1045. }
  1046. }
  1047. switch( type ) {
  1048. case C_ADD_PRE: // pre ++
  1049. case C_SUB_PRE: // pre --
  1050. // Nothing more to skip
  1051. break;
  1052. case C_EQ: {// incremental modifier
  1053. p = skip_space( &p[1] );
  1054. }
  1055. break;
  1056. case C_L_SHIFT:
  1057. case C_R_SHIFT: {// left or right shift modifier
  1058. p = skip_space( &p[3] );
  1059. }
  1060. break;
  1061. default: {// normal incremental command
  1062. p = skip_space( &p[2] );
  1063. }
  1064. }
  1065. if( p == NULL ) {// end of line or invalid buffer
  1066. return NULL;
  1067. }
  1068. // push the set function onto the stack
  1069. add_scriptl(buildin_set_ref);
  1070. add_scriptc(C_ARG);
  1071. // always append parenthesis to avoid errors
  1072. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1073. syntax.curly[syntax.curly_count].count = 0;
  1074. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1075. // increment the total curly count for the position in the script
  1076. ++ syntax.curly_count;
  1077. // parse the variable currently being modified
  1078. word = add_word(var);
  1079. 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
  1080. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1081. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1082. if( type != C_EQ )
  1083. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1084. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1085. add_scripti(1);
  1086. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1087. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1088. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1089. add_scripti(1);
  1090. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1091. } else { // Process the value as an expression
  1092. p = parse_subexpr(p, -1);
  1093. if( type != C_EQ )
  1094. {// push the type of modifier onto the stack
  1095. add_scriptc(type);
  1096. }
  1097. }
  1098. // decrement the curly count for the position within the script
  1099. -- syntax.curly_count;
  1100. // close the script by appending the function operator
  1101. add_scriptc(C_FUNC);
  1102. // push the buffer from the method
  1103. return p;
  1104. }
  1105. /*
  1106. * Checks whether the gives string is a number literal
  1107. *
  1108. * Mainly necessary to differentiate between number literals and NPC name
  1109. * constants, since several of those start with a digit.
  1110. *
  1111. * All this does is to check if the string begins with an optional + or - sign,
  1112. * followed by a hexadecimal or decimal number literal literal and is NOT
  1113. * followed by a underscore or letter.
  1114. *
  1115. * @author : Hercules.ws
  1116. * @param p Pointer to the string to check
  1117. * @return Whether the string is a number literal
  1118. */
  1119. bool is_number(const char *p) {
  1120. const char *np;
  1121. if (!p)
  1122. return false;
  1123. if (*p == '-' || *p == '+')
  1124. p++;
  1125. np = p;
  1126. if (*p == '0' && p[1] == 'x') {
  1127. p+=2;
  1128. np = p;
  1129. // Hexadecimal
  1130. while (ISXDIGIT(*np))
  1131. np++;
  1132. } else {
  1133. // Decimal
  1134. while (ISDIGIT(*np))
  1135. np++;
  1136. }
  1137. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1138. return true;
  1139. return false;
  1140. }
  1141. /*==========================================
  1142. * Analysis section
  1143. *------------------------------------------*/
  1144. const char* parse_simpleexpr(const char *p)
  1145. {
  1146. int64 i;
  1147. p=skip_space(p);
  1148. if(*p==';' || *p==',')
  1149. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1150. if(*p=='('){
  1151. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1152. ++syntax.curly[i].count;
  1153. p=parse_subexpr(p+1,-1);
  1154. p=skip_space(p);
  1155. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1156. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1157. ){
  1158. if( *p == ',' ){
  1159. syntax.curly[i].flag = ARGLIST_PAREN;
  1160. return p;
  1161. } else
  1162. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1163. }
  1164. if( *p != ')' )
  1165. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1166. ++p;
  1167. } else if(is_number(p)) {
  1168. char *np;
  1169. bool error;
  1170. // Skip leading zeroes
  1171. while(*p == '0' && ISDIGIT(p[1])) p++;
  1172. errno = 0;
  1173. i=strtoll(p,&np,0);
  1174. error = (errno == ERANGE);
  1175. if( i < SCRIPT_INT_MIN || ( error && i == INT64_MIN ) ){
  1176. i = SCRIPT_INT_MIN;
  1177. disp_warning_message( "parse_simpleexpr: underflow detected, capping value to SCRIPT_INT_MIN", p );
  1178. }else if( i > SCRIPT_INT_MAX || ( error && i == INT64_MAX ) ){
  1179. i = SCRIPT_INT_MAX;
  1180. disp_warning_message( "parse_simpleexpr: overflow detected, capping value to SCRIPT_INT_MAX", p );
  1181. }
  1182. add_scripti(i);
  1183. p=np;
  1184. } else if(*p=='"'){
  1185. add_scriptc(C_STR);
  1186. p++;
  1187. while( *p && *p != '"' ){
  1188. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1189. {
  1190. char buf[8];
  1191. size_t len = skip_escaped_c(p) - p;
  1192. size_t n = sv_unescape_c(buf, p, len);
  1193. if( n != 1 )
  1194. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1195. p += len;
  1196. add_scriptb(*buf);
  1197. continue;
  1198. }
  1199. else if( *p == '\n' )
  1200. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1201. add_scriptb(*p++);
  1202. }
  1203. if(!*p)
  1204. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1205. add_scriptb(0);
  1206. p++; //'"'
  1207. } else {
  1208. int l;
  1209. const char* pv;
  1210. // label , register , function etc
  1211. if(skip_word(p)==p)
  1212. disp_error_message("parse_simpleexpr: unexpected character",p);
  1213. l=add_word(p);
  1214. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1215. return parse_callfunc(p,1,0);
  1216. else {
  1217. const char* name = get_str(l);
  1218. if( strdb_get(userfunc_db,name) != NULL ) {
  1219. return parse_callfunc(p,1,1);
  1220. }
  1221. }
  1222. if( (pv = parse_variable(p)) )
  1223. {// successfully processed a variable assignment
  1224. return pv;
  1225. }
  1226. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1227. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1228. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1229. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1230. if (str_data[l].name)
  1231. ShowWarning( "Please use '%s' instead!\n", str_data[l].name );
  1232. }
  1233. #endif
  1234. p=skip_word(p);
  1235. if( *p == '[' ){
  1236. // array(name[i] => getelementofarray(name,i) )
  1237. add_scriptl(buildin_getelementofarray_ref);
  1238. add_scriptc(C_ARG);
  1239. add_scriptl(l);
  1240. p=parse_subexpr(p+1,-1);
  1241. p=skip_space(p);
  1242. if( *p != ']' )
  1243. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1244. ++p;
  1245. add_scriptc(C_FUNC);
  1246. }else
  1247. add_scriptl(l);
  1248. }
  1249. return p;
  1250. }
  1251. /*==========================================
  1252. * Analysis of the expression
  1253. *------------------------------------------*/
  1254. const char* parse_subexpr(const char* p,int limit)
  1255. {
  1256. int op,opl,len;
  1257. p=skip_space(p);
  1258. if( *p == '-' ){
  1259. const char* tmpp = skip_space(p+1);
  1260. if( *tmpp == ';' || *tmpp == ',' ){
  1261. add_scriptl(LABEL_NEXTLINE);
  1262. p++;
  1263. return p;
  1264. }
  1265. }
  1266. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1267. p = parse_variable(p);
  1268. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1269. p = parse_subexpr(p + 1, 11);
  1270. add_scriptc(op);
  1271. } else
  1272. p = parse_simpleexpr(p);
  1273. p = skip_space(p);
  1274. while((
  1275. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1276. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1277. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1278. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1279. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1280. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1281. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1282. (op=C_AND,opl=5,len=1,*p=='&') ||
  1283. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1284. (op=C_OR,opl=3,len=1,*p=='|') ||
  1285. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1286. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1287. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1288. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1289. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1290. (op=C_GT,opl=7,len=1,*p=='>') ||
  1291. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1292. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1293. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1294. p+=len;
  1295. if(op == C_OP3) {
  1296. p=parse_subexpr(p,-1);
  1297. p=skip_space(p);
  1298. if( *(p++) != ':')
  1299. disp_error_message("parse_subexpr: expected ':'", p-1);
  1300. p=parse_subexpr(p,-1);
  1301. } else {
  1302. p=parse_subexpr(p,opl);
  1303. }
  1304. add_scriptc(op);
  1305. p=skip_space(p);
  1306. }
  1307. return p; /* return first untreated operator */
  1308. }
  1309. /*==========================================
  1310. * Evaluation of the expression
  1311. *------------------------------------------*/
  1312. const char* parse_expr(const char *p)
  1313. {
  1314. switch(*p){
  1315. case ')': case ';': case ':': case '[': case ']':
  1316. case '}':
  1317. disp_error_message("parse_expr: unexpected character",p);
  1318. }
  1319. p=parse_subexpr(p,-1);
  1320. return p;
  1321. }
  1322. /*==========================================
  1323. * Analysis of the line
  1324. *------------------------------------------*/
  1325. const char* parse_line(const char* p)
  1326. {
  1327. const char* p2;
  1328. p=skip_space(p);
  1329. if(*p==';') {
  1330. //Close decision for if(); for(); while();
  1331. p = parse_syntax_close(p + 1);
  1332. return p;
  1333. }
  1334. if(*p==')' && parse_syntax_for_flag)
  1335. return p+1;
  1336. p = skip_space(p);
  1337. if(p[0] == '{') {
  1338. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1339. syntax.curly[syntax.curly_count].count = -1;
  1340. syntax.curly[syntax.curly_count].index = -1;
  1341. syntax.curly_count++;
  1342. return p + 1;
  1343. } else if(p[0] == '}') {
  1344. return parse_curly_close(p);
  1345. }
  1346. // Syntax-related processing
  1347. p2 = parse_syntax(p);
  1348. if(p2 != NULL)
  1349. return p2;
  1350. // attempt to process a variable assignment
  1351. p2 = parse_variable(p);
  1352. if( p2 != NULL )
  1353. {// variable assignment processed so leave the method
  1354. return parse_syntax_close(p2 + 1);
  1355. }
  1356. p = parse_callfunc(p,0,0);
  1357. p = skip_space(p);
  1358. if(parse_syntax_for_flag) {
  1359. if( *p != ')' )
  1360. disp_error_message("parse_line: expected ')'",p);
  1361. } else {
  1362. if( *p != ';' )
  1363. disp_error_message("parse_line: expected ';'",p);
  1364. }
  1365. //Binding decision for if(), for(), while()
  1366. p = parse_syntax_close(p+1);
  1367. return p;
  1368. }
  1369. // { ... } Closing process
  1370. const char* parse_curly_close(const char* p)
  1371. {
  1372. if(syntax.curly_count <= 0) {
  1373. disp_error_message("parse_curly_close: unexpected string",p);
  1374. return p + 1;
  1375. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1376. syntax.curly_count--;
  1377. //Close decision if, for , while
  1378. p = parse_syntax_close(p + 1);
  1379. return p;
  1380. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1381. //Closing switch()
  1382. int pos = syntax.curly_count-1;
  1383. char label[256];
  1384. int l;
  1385. // Remove temporary variables
  1386. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1387. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1388. parse_line(label);
  1389. syntax.curly_count--;
  1390. // Go to the end pointer unconditionally
  1391. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1392. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1393. parse_line(label);
  1394. syntax.curly_count--;
  1395. // You are here labeled
  1396. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1397. l=add_str(label);
  1398. set_label(l,script_pos, p);
  1399. if(syntax.curly[pos].flag) {
  1400. //Exists default
  1401. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1402. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1403. parse_line(label);
  1404. syntax.curly_count--;
  1405. }
  1406. // Label end
  1407. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1408. l=add_str(label);
  1409. set_label(l,script_pos, p);
  1410. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1411. syntax.curly_count--;
  1412. //Closing decision if, for , while
  1413. p = parse_syntax_close(p + 1);
  1414. return p;
  1415. } else {
  1416. disp_error_message("parse_curly_close: unexpected string",p);
  1417. return p + 1;
  1418. }
  1419. }
  1420. // Syntax-related processing
  1421. // break, case, continue, default, do, for, function,
  1422. // if, switch, while ? will handle this internally.
  1423. const char* parse_syntax(const char* p)
  1424. {
  1425. const char *p2 = skip_word(p);
  1426. switch(*p) {
  1427. case 'B':
  1428. case 'b':
  1429. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1430. // break Processing
  1431. char label[256];
  1432. int pos = syntax.curly_count - 1;
  1433. while(pos >= 0) {
  1434. if(syntax.curly[pos].type == TYPE_DO) {
  1435. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1436. break;
  1437. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1438. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1439. break;
  1440. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1441. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1442. break;
  1443. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1444. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1445. break;
  1446. }
  1447. pos--;
  1448. }
  1449. if(pos < 0) {
  1450. disp_error_message("parse_syntax: unexpected 'break'",p);
  1451. } else {
  1452. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1453. parse_line(label);
  1454. syntax.curly_count--;
  1455. }
  1456. p = skip_space(p2);
  1457. if(*p != ';')
  1458. disp_error_message("parse_syntax: expected ';'",p);
  1459. // Closing decision if, for , while
  1460. p = parse_syntax_close(p + 1);
  1461. return p;
  1462. }
  1463. break;
  1464. case 'c':
  1465. case 'C':
  1466. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1467. //Processing case
  1468. int pos = syntax.curly_count-1;
  1469. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1470. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1471. return p+1;
  1472. } else {
  1473. char label[256];
  1474. int l;
  1475. int64 v;
  1476. char *np;
  1477. if(syntax.curly[pos].count != 1) {
  1478. //Jump for FALLTHRU
  1479. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1480. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1481. parse_line(label);
  1482. syntax.curly_count--;
  1483. // You are here labeled
  1484. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1485. l=add_str(label);
  1486. set_label(l,script_pos, p);
  1487. }
  1488. //Decision statement switch
  1489. p = skip_space(p2);
  1490. if(p == p2) {
  1491. disp_error_message("parse_syntax: expected a space ' '",p);
  1492. }
  1493. // check whether case label is integer or not
  1494. if(is_number(p)) {
  1495. //Numeric value
  1496. v = (int)strtol(p,&np,0);
  1497. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1498. p++;
  1499. p = skip_word(p);
  1500. if(np != p)
  1501. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1502. } else {
  1503. //Check for constants
  1504. p2 = skip_word(p);
  1505. v = (int)(size_t) (p2-p); // length of word at p2
  1506. memcpy(label,p,static_cast<int>(v));
  1507. label[v]='\0';
  1508. if( !script_get_constant(label, &v) )
  1509. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1510. p = skip_word(p);
  1511. }
  1512. p = skip_space(p);
  1513. if(*p != ':')
  1514. disp_error_message("parse_syntax: expect ':'",p);
  1515. sprintf(label,"if(%" PRId64 " != $@__SW%x_VAL) goto __SW%x_%x;",
  1516. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1517. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1518. // Bad I do not parse twice
  1519. p2 = parse_line(label);
  1520. parse_line(p2);
  1521. syntax.curly_count--;
  1522. if(syntax.curly[pos].count != 1) {
  1523. // Label after the completion of FALLTHRU
  1524. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1525. l=add_str(label);
  1526. set_label(l,script_pos,p);
  1527. }
  1528. // check duplication of case label [Rayce]
  1529. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1530. disp_error_message("parse_syntax: dup 'case'",p);
  1531. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1532. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1533. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1534. parse_line(label);
  1535. syntax.curly_count--;
  1536. syntax.curly[pos].count++;
  1537. }
  1538. return p + 1;
  1539. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1540. // Processing continue
  1541. char label[256];
  1542. int pos = syntax.curly_count - 1;
  1543. while(pos >= 0) {
  1544. if(syntax.curly[pos].type == TYPE_DO) {
  1545. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1546. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1547. break;
  1548. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1549. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1550. break;
  1551. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1552. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1553. break;
  1554. }
  1555. pos--;
  1556. }
  1557. if(pos < 0) {
  1558. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1559. } else {
  1560. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1561. parse_line(label);
  1562. syntax.curly_count--;
  1563. }
  1564. p = skip_space(p2);
  1565. if(*p != ';')
  1566. disp_error_message("parse_syntax: expected ';'",p);
  1567. //Closing decision if, for , while
  1568. p = parse_syntax_close(p + 1);
  1569. return p;
  1570. }
  1571. break;
  1572. case 'd':
  1573. case 'D':
  1574. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1575. // Switch - default processing
  1576. int pos = syntax.curly_count-1;
  1577. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1578. disp_error_message("parse_syntax: unexpected 'default'",p);
  1579. } else if(syntax.curly[pos].flag) {
  1580. disp_error_message("parse_syntax: dup 'default'",p);
  1581. } else {
  1582. char label[256];
  1583. int l;
  1584. // Put the label location
  1585. p = skip_space(p2);
  1586. if(*p != ':') {
  1587. disp_error_message("parse_syntax: expected ':'",p);
  1588. }
  1589. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1590. l=add_str(label);
  1591. set_label(l,script_pos,p);
  1592. // Skip to the next link w/o condition
  1593. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1594. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1595. parse_line(label);
  1596. syntax.curly_count--;
  1597. // The default label
  1598. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1599. l=add_str(label);
  1600. set_label(l,script_pos,p);
  1601. syntax.curly[syntax.curly_count - 1].flag = 1;
  1602. syntax.curly[pos].count++;
  1603. }
  1604. return p + 1;
  1605. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1606. int l;
  1607. char label[256];
  1608. p=skip_space(p2);
  1609. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1610. syntax.curly[syntax.curly_count].count = 1;
  1611. syntax.curly[syntax.curly_count].index = syntax.index++;
  1612. syntax.curly[syntax.curly_count].flag = 0;
  1613. // Label of the (do) form here
  1614. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1615. l=add_str(label);
  1616. set_label(l,script_pos,p);
  1617. syntax.curly_count++;
  1618. return p;
  1619. }
  1620. break;
  1621. case 'f':
  1622. case 'F':
  1623. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1624. int l;
  1625. char label[256];
  1626. int pos = syntax.curly_count;
  1627. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1628. syntax.curly[syntax.curly_count].count = 1;
  1629. syntax.curly[syntax.curly_count].index = syntax.index++;
  1630. syntax.curly[syntax.curly_count].flag = 0;
  1631. syntax.curly_count++;
  1632. p=skip_space(p2);
  1633. if(*p != '(')
  1634. disp_error_message("parse_syntax: expected '('",p);
  1635. p++;
  1636. // Execute the initialization statement
  1637. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1638. p=parse_line(p);
  1639. syntax.curly_count--;
  1640. // Form the start of label decision
  1641. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1642. l=add_str(label);
  1643. set_label(l,script_pos,p);
  1644. p=skip_space(p);
  1645. if(*p == ';') {
  1646. // For (; Because the pattern of always true ;)
  1647. ;
  1648. } else {
  1649. // Skip to the end point if the condition is false
  1650. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1651. add_scriptl(add_str("jump_zero"));
  1652. add_scriptc(C_ARG);
  1653. p=parse_expr(p);
  1654. p=skip_space(p);
  1655. add_scriptl(add_str(label));
  1656. add_scriptc(C_FUNC);
  1657. }
  1658. if(*p != ';')
  1659. disp_error_message("parse_syntax: expected ';'",p);
  1660. p++;
  1661. // Skip to the beginning of the loop
  1662. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1663. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1664. parse_line(label);
  1665. syntax.curly_count--;
  1666. // Labels to form the next loop
  1667. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1668. l=add_str(label);
  1669. set_label(l,script_pos,p);
  1670. // Process the next time you enter the loop
  1671. // A ')' last for; flag to be treated as'
  1672. parse_syntax_for_flag = 1;
  1673. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1674. p=parse_line(p);
  1675. syntax.curly_count--;
  1676. parse_syntax_for_flag = 0;
  1677. // Skip to the determination process conditions
  1678. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1679. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1680. parse_line(label);
  1681. syntax.curly_count--;
  1682. // Loop start labeling
  1683. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1684. l=add_str(label);
  1685. set_label(l,script_pos,p);
  1686. return p;
  1687. }
  1688. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1689. {// internal script function
  1690. const char *func_name;
  1691. func_name = skip_space(p2);
  1692. p = skip_word(func_name);
  1693. if( p == func_name )
  1694. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1695. p2 = skip_space(p);
  1696. if( *p2 == ';' )
  1697. {// function <name> ;
  1698. // function declaration - just register the name
  1699. int l;
  1700. l = add_word(func_name);
  1701. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1702. str_data[l].type = C_USERFUNC;
  1703. else if( str_data[l].type == C_USERFUNC )
  1704. ; // already registered
  1705. else
  1706. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1707. // Close condition of if, for, while
  1708. p = parse_syntax_close(p2 + 1);
  1709. return p;
  1710. }
  1711. else if(*p2 == '{')
  1712. {// function <name> <line/block of code>
  1713. char label[256];
  1714. int l;
  1715. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1716. syntax.curly[syntax.curly_count].count = 1;
  1717. syntax.curly[syntax.curly_count].index = syntax.index++;
  1718. syntax.curly[syntax.curly_count].flag = 0;
  1719. ++syntax.curly_count;
  1720. // Jump over the function code
  1721. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1722. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1723. ++syntax.curly_count;
  1724. parse_line(label);
  1725. --syntax.curly_count;
  1726. // Set the position of the function (label)
  1727. l=add_word(func_name);
  1728. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1729. {
  1730. str_data[l].type = C_USERFUNC;
  1731. set_label(l, script_pos, p);
  1732. if( parse_options&SCRIPT_USE_LABEL_DB )
  1733. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1734. }
  1735. else
  1736. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1737. return skip_space(p);
  1738. }
  1739. else
  1740. {
  1741. disp_error_message("expect ';' or '{' at function syntax",p);
  1742. }
  1743. }
  1744. break;
  1745. case 'i':
  1746. case 'I':
  1747. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1748. // If process
  1749. char label[256];
  1750. p=skip_space(p2);
  1751. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1752. disp_error_message("need '('",p);
  1753. }
  1754. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1755. syntax.curly[syntax.curly_count].count = 1;
  1756. syntax.curly[syntax.curly_count].index = syntax.index++;
  1757. syntax.curly[syntax.curly_count].flag = 0;
  1758. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1759. syntax.curly_count++;
  1760. add_scriptl(add_str("jump_zero"));
  1761. add_scriptc(C_ARG);
  1762. p=parse_expr(p);
  1763. p=skip_space(p);
  1764. add_scriptl(add_str(label));
  1765. add_scriptc(C_FUNC);
  1766. return p;
  1767. }
  1768. break;
  1769. case 's':
  1770. case 'S':
  1771. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1772. // Processing of switch ()
  1773. char label[256];
  1774. p=skip_space(p2);
  1775. if(*p != '(') {
  1776. disp_error_message("need '('",p);
  1777. }
  1778. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1779. syntax.curly[syntax.curly_count].count = 1;
  1780. syntax.curly[syntax.curly_count].index = syntax.index++;
  1781. syntax.curly[syntax.curly_count].flag = 0;
  1782. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1783. syntax.curly_count++;
  1784. add_scriptl(add_str("set"));
  1785. add_scriptc(C_ARG);
  1786. add_scriptl(add_str(label));
  1787. p=parse_expr(p);
  1788. p=skip_space(p);
  1789. if(*p != '{') {
  1790. disp_error_message("parse_syntax: expected '{'",p);
  1791. }
  1792. add_scriptc(C_FUNC);
  1793. return p + 1;
  1794. }
  1795. break;
  1796. case 'w':
  1797. case 'W':
  1798. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1799. int l;
  1800. char label[256];
  1801. p=skip_space(p2);
  1802. if(*p != '(') {
  1803. disp_error_message("need '('",p);
  1804. }
  1805. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1806. syntax.curly[syntax.curly_count].count = 1;
  1807. syntax.curly[syntax.curly_count].index = syntax.index++;
  1808. syntax.curly[syntax.curly_count].flag = 0;
  1809. // Form the start of label decision
  1810. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1811. l=add_str(label);
  1812. set_label(l,script_pos,p);
  1813. // Skip to the end point if the condition is false
  1814. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1815. syntax.curly_count++;
  1816. add_scriptl(add_str("jump_zero"));
  1817. add_scriptc(C_ARG);
  1818. p=parse_expr(p);
  1819. p=skip_space(p);
  1820. add_scriptl(add_str(label));
  1821. add_scriptc(C_FUNC);
  1822. return p;
  1823. }
  1824. break;
  1825. }
  1826. return NULL;
  1827. }
  1828. const char* parse_syntax_close(const char *p) {
  1829. // If (...) for (...) hoge (); as to make sure closed closed once again
  1830. int flag;
  1831. do {
  1832. p = parse_syntax_close_sub(p,&flag);
  1833. } while(flag);
  1834. return p;
  1835. }
  1836. // Close judgment if, for, while, of do
  1837. // flag == 1 : closed
  1838. // flag == 0 : not closed
  1839. const char* parse_syntax_close_sub(const char* p,int* flag)
  1840. {
  1841. char label[256];
  1842. int pos = syntax.curly_count - 1;
  1843. int l;
  1844. *flag = 1;
  1845. if(syntax.curly_count <= 0) {
  1846. *flag = 0;
  1847. return p;
  1848. } else if(syntax.curly[pos].type == TYPE_IF) {
  1849. const char *bp = p;
  1850. const char *p2;
  1851. // if-block and else-block end is a new line
  1852. parse_nextline(false, p);
  1853. // Skip to the last location if
  1854. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1855. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1856. parse_line(label);
  1857. syntax.curly_count--;
  1858. // Put the label of the location
  1859. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1860. l=add_str(label);
  1861. set_label(l,script_pos,p);
  1862. syntax.curly[pos].count++;
  1863. p = skip_space(p);
  1864. p2 = skip_word(p);
  1865. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1866. // else or else - if
  1867. p = skip_space(p2);
  1868. p2 = skip_word(p);
  1869. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1870. // else - if
  1871. p=skip_space(p2);
  1872. if(*p != '(') {
  1873. disp_error_message("need '('",p);
  1874. }
  1875. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1876. add_scriptl(add_str("jump_zero"));
  1877. add_scriptc(C_ARG);
  1878. p=parse_expr(p);
  1879. p=skip_space(p);
  1880. add_scriptl(add_str(label));
  1881. add_scriptc(C_FUNC);
  1882. *flag = 0;
  1883. return p;
  1884. } else {
  1885. // else
  1886. if(!syntax.curly[pos].flag) {
  1887. syntax.curly[pos].flag = 1;
  1888. *flag = 0;
  1889. return p;
  1890. }
  1891. }
  1892. }
  1893. // Close if
  1894. syntax.curly_count--;
  1895. // Put the label of the final location
  1896. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1897. l=add_str(label);
  1898. set_label(l,script_pos,p);
  1899. if(syntax.curly[pos].flag == 1) {
  1900. // Because the position of the pointer is the same if not else for this
  1901. return bp;
  1902. }
  1903. return p;
  1904. } else if(syntax.curly[pos].type == TYPE_DO) {
  1905. int l2;
  1906. char label2[256];
  1907. const char *p2;
  1908. if(syntax.curly[pos].flag) {
  1909. // (Come here continue) to form the label here
  1910. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1911. l2=add_str(label2);
  1912. set_label(l2,script_pos,p);
  1913. }
  1914. // Skip to the end point if the condition is false
  1915. p = skip_space(p);
  1916. p2 = skip_word(p);
  1917. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1918. disp_error_message("parse_syntax: expected 'while'",p);
  1919. p = skip_space(p2);
  1920. if(*p != '(') {
  1921. disp_error_message("need '('",p);
  1922. }
  1923. // do-block end is a new line
  1924. parse_nextline(false, p);
  1925. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1926. add_scriptl(add_str("jump_zero"));
  1927. add_scriptc(C_ARG);
  1928. p=parse_expr(p);
  1929. p=skip_space(p);
  1930. add_scriptl(add_str(label2));
  1931. add_scriptc(C_FUNC);
  1932. // Skip to the starting point
  1933. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1934. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1935. parse_line(label2);
  1936. syntax.curly_count--;
  1937. // Form label of the end point conditions
  1938. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1939. l2=add_str(label2);
  1940. set_label(l2,script_pos,p);
  1941. p = skip_space(p);
  1942. if(*p != ';') {
  1943. disp_error_message("parse_syntax: expected ';'",p);
  1944. return p+1;
  1945. }
  1946. p++;
  1947. syntax.curly_count--;
  1948. return p;
  1949. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1950. // for-block end is a new line
  1951. parse_nextline(false, p);
  1952. // Skip to the next loop
  1953. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1954. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1955. parse_line(label);
  1956. syntax.curly_count--;
  1957. // End for labeling
  1958. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1959. l=add_str(label);
  1960. set_label(l,script_pos,p);
  1961. syntax.curly_count--;
  1962. return p;
  1963. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1964. // while-block end is a new line
  1965. parse_nextline(false, p);
  1966. // Skip to the decision while
  1967. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1968. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1969. parse_line(label);
  1970. syntax.curly_count--;
  1971. // End while labeling
  1972. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1973. l=add_str(label);
  1974. set_label(l,script_pos,p);
  1975. syntax.curly_count--;
  1976. return p;
  1977. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1978. int pos2 = syntax.curly_count-1;
  1979. char label2[256];
  1980. int l2;
  1981. // Back
  1982. sprintf(label2,"return;");
  1983. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1984. parse_line(label2);
  1985. syntax.curly_count--;
  1986. // Put the label of the location
  1987. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1988. l2=add_str(label2);
  1989. set_label(l2,script_pos,p);
  1990. syntax.curly_count--;
  1991. return p;
  1992. } else {
  1993. *flag = 0;
  1994. return p;
  1995. }
  1996. }
  1997. /*==========================================
  1998. * Added built-in functions
  1999. *------------------------------------------*/
  2000. static void add_buildin_func(void)
  2001. {
  2002. int i;
  2003. for( i = 0; buildin_func[i].func; i++ )
  2004. {
  2005. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2006. // 'v' - value (either string or int or reference)
  2007. // 's' - string
  2008. // 'i' - int
  2009. // 'r' - reference (of a variable)
  2010. // 'l' - label
  2011. // '?' - one optional parameter
  2012. // '*' - unknown number of optional parameters
  2013. const char* p = buildin_func[i].arg;
  2014. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2015. while( *p == '?' ) ++p;
  2016. if( *p == '*' ) ++p;
  2017. if( *p != 0){
  2018. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2019. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2020. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2021. } else {
  2022. int n = add_str(buildin_func[i].name);
  2023. str_data[n].type = C_FUNC;
  2024. str_data[n].val = i;
  2025. str_data[n].func = buildin_func[i].func;
  2026. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  2027. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2028. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2029. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2030. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2031. }
  2032. }
  2033. }
  2034. /**
  2035. * String comparison with a char array to a script constant
  2036. * @param prefix: Char array to compare
  2037. * @param value: Script constant
  2038. * @return Constant name as char array or NULL otherwise
  2039. */
  2040. const char* script_get_constant_str( const char* prefix, int64 value ){
  2041. const char* name;
  2042. for(int i = 0; i < str_data_size; i++ ){
  2043. // Check if it is a constant
  2044. if( str_data[i].type != C_INT ){
  2045. continue;
  2046. }
  2047. // Check if the value matches
  2048. if( str_data[i].val != value ){
  2049. continue;
  2050. }
  2051. // Look up the actual string
  2052. name = get_str(i);
  2053. // Compare the prefix
  2054. if( !strncasecmp( name, prefix, strlen(prefix) ) ){
  2055. // We found a match
  2056. return name;
  2057. }
  2058. }
  2059. return NULL;
  2060. }
  2061. /// Retrieves the value of a constant parameter.
  2062. bool script_get_parameter(const char* name, int64* value)
  2063. {
  2064. int n = search_str(name);
  2065. if (n == -1 || str_data[n].type != C_PARAM)
  2066. {// not found or not a parameter
  2067. return false;
  2068. }
  2069. value[0] = str_data[n].val;
  2070. return true;
  2071. }
  2072. /// Retrieves the value of a constant.
  2073. bool script_get_constant(const char* name, int64* value)
  2074. {
  2075. int n = search_str(name);
  2076. if( n == -1 || str_data[n].type != C_INT )
  2077. {// not found or not a constant
  2078. return false;
  2079. }
  2080. value[0] = str_data[n].val;
  2081. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2082. if( str_data[n].deprecated ){
  2083. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2084. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2085. if (str_data[n].name)
  2086. ShowWarning( "Please use '%s' instead!\n", str_data[n].name );
  2087. }
  2088. #endif
  2089. return true;
  2090. }
  2091. /// Creates new constant or parameter with given value.
  2092. void script_set_constant_(const char* name, int64 value, const char* constant_name, bool isparameter, bool deprecated)
  2093. {
  2094. int n = add_str(name);
  2095. if( str_data[n].type == C_NOP )
  2096. {// new
  2097. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2098. str_data[n].val = value;
  2099. str_data[n].deprecated = deprecated;
  2100. str_data[n].name = constant_name;
  2101. }
  2102. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2103. {// existing parameter or constant
  2104. 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);
  2105. }
  2106. else
  2107. {// existing name
  2108. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2109. }
  2110. }
  2111. static bool read_constdb_sub( char* fields[], int columns, int current ){
  2112. char name[1024], val[1024];
  2113. int type = 0;
  2114. if( columns > 1 ){
  2115. if( sscanf(fields[0], "%1023[A-Za-z0-9/_]", name) != 1 ||
  2116. sscanf(fields[1], "%1023[A-Za-z0-9/_]", val) != 1 ||
  2117. ( columns >= 2 && sscanf(fields[2], "%11d", &type) != 1 ) ){
  2118. ShowWarning("Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current);
  2119. return false;
  2120. }
  2121. }else{
  2122. if( sscanf(fields[0], "%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d", name, val, &type) < 2 ){
  2123. ShowWarning( "Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current );
  2124. return false;
  2125. }
  2126. }
  2127. script_set_constant(name, (int)strtol(val, NULL, 0), (type != 0), false);
  2128. return true;
  2129. }
  2130. /*==========================================
  2131. * Reading constant databases
  2132. * const.txt
  2133. *------------------------------------------*/
  2134. static void read_constdb(void){
  2135. const char* dbsubpath[] = {
  2136. "",
  2137. "/" DBIMPORT,
  2138. };
  2139. for( int i = 0; i < ARRAYLENGTH(dbsubpath); i++ ){
  2140. int n2 = strlen(db_path) + strlen(dbsubpath[i]) + 1;
  2141. char* dbsubpath2 = (char*)aMalloc(n2 + 1);
  2142. bool silent = i > 0;
  2143. safesnprintf(dbsubpath2, n2, "%s%s", db_path, dbsubpath[i]);
  2144. sv_readdb(dbsubpath2, "const.txt", ',', 1, 3, -1, &read_constdb_sub, silent);
  2145. aFree(dbsubpath2);
  2146. }
  2147. }
  2148. /**
  2149. * Sets source-end constants for NPC scripts to access.
  2150. **/
  2151. void script_hardcoded_constants(void) {
  2152. #include "script_constants.hpp"
  2153. }
  2154. /*==========================================
  2155. * Display emplacement line of script
  2156. *------------------------------------------*/
  2157. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2158. {
  2159. int i;
  2160. if( p == NULL || !p[0] ) return NULL;
  2161. if( line < 0 )
  2162. StringBuf_Printf(buf, "*% 5d : ", -line);
  2163. else
  2164. StringBuf_Printf(buf, " % 5d : ", line);
  2165. for(i=0;p[i] && p[i] != '\n';i++){
  2166. if(p + i != mark)
  2167. StringBuf_Printf(buf, "%c", p[i]);
  2168. else
  2169. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2170. }
  2171. StringBuf_AppendStr(buf, "\n");
  2172. return p+i+(p[i] == '\n' ? 1 : 0);
  2173. }
  2174. 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) {
  2175. // Find the line where the error occurred
  2176. int j;
  2177. int line = start_line;
  2178. const char *p;
  2179. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2180. for(p=src;p && *p;line++){
  2181. const char *lineend=strchr(p,'\n');
  2182. if(lineend==NULL || error_pos_cur<lineend){
  2183. break;
  2184. }
  2185. for( j = 0; j < 4; j++ ) {
  2186. linestart[j] = linestart[j+1];
  2187. }
  2188. linestart[4] = p;
  2189. p=lineend+1;
  2190. }
  2191. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2192. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2193. for(j = 0; j < 5; j++ ) {
  2194. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2195. }
  2196. p = script_print_line(buf, p, error_pos_cur, -line);
  2197. for(j = 0; j < 5; j++) {
  2198. p = script_print_line(buf, p, NULL, line + j + 1 );
  2199. }
  2200. }
  2201. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2202. StringBuf buf;
  2203. StringBuf_Init(&buf);
  2204. StringBuf_AppendStr(&buf, "\a\n");
  2205. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2206. ShowError("%s", StringBuf_Value(&buf));
  2207. StringBuf_Destroy(&buf);
  2208. }
  2209. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2210. StringBuf buf;
  2211. StringBuf_Init(&buf);
  2212. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2213. ShowWarning("%s", StringBuf_Value(&buf));
  2214. StringBuf_Destroy(&buf);
  2215. }
  2216. /*==========================================
  2217. * Analysis of the script
  2218. *------------------------------------------*/
  2219. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2220. {
  2221. const char *p,*tmpp;
  2222. int i;
  2223. struct script_code* code = NULL;
  2224. char end;
  2225. bool unresolved_names = false;
  2226. parser_current_src = src;
  2227. parser_current_file = file;
  2228. parser_current_line = line;
  2229. if( src == NULL )
  2230. return NULL;// empty script
  2231. memset(&syntax,0,sizeof(syntax));
  2232. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2233. script_pos=0;
  2234. script_size=SCRIPT_BLOCK_SIZE;
  2235. parse_nextline(true, NULL);
  2236. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2237. if( options&SCRIPT_USE_LABEL_DB )
  2238. db_clear(scriptlabel_db);
  2239. parse_options = options;
  2240. if( setjmp( error_jump ) != 0 ) {
  2241. //Restore program state when script has problems. [from jA]
  2242. int j;
  2243. const int size = ARRAYLENGTH(syntax.curly);
  2244. if( error_report )
  2245. script_error(src,file,line,error_msg,error_pos);
  2246. aFree( error_msg );
  2247. aFree( script_buf );
  2248. script_pos = 0;
  2249. script_size = 0;
  2250. script_buf = NULL;
  2251. for(j=LABEL_START;j<str_num;j++)
  2252. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2253. for(j=0; j<size; j++)
  2254. linkdb_final(&syntax.curly[j].case_label);
  2255. return NULL;
  2256. }
  2257. parse_syntax_for_flag=0;
  2258. p=src;
  2259. p=skip_space(p);
  2260. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2261. {// does not require brackets around the script
  2262. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2263. {// empty script and can return NULL
  2264. aFree( script_buf );
  2265. script_pos = 0;
  2266. script_size = 0;
  2267. script_buf = NULL;
  2268. return NULL;
  2269. }
  2270. end = '\0';
  2271. }
  2272. else
  2273. {// requires brackets around the script
  2274. if( *p != '{' )
  2275. disp_error_message("not found '{'",p);
  2276. p = skip_space(p+1);
  2277. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2278. {// empty script and can return NULL
  2279. aFree( script_buf );
  2280. script_pos = 0;
  2281. script_size = 0;
  2282. script_buf = NULL;
  2283. return NULL;
  2284. }
  2285. end = '}';
  2286. }
  2287. // clear references of labels, variables and internal functions
  2288. for(i=LABEL_START;i<str_num;i++){
  2289. if(
  2290. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2291. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2292. ){
  2293. str_data[i].type=C_NOP;
  2294. str_data[i].backpatch=-1;
  2295. str_data[i].label=-1;
  2296. }
  2297. }
  2298. while( syntax.curly_count != 0 || *p != end )
  2299. {
  2300. if( *p == '\0' )
  2301. disp_error_message("unexpected end of script",p);
  2302. // Special handling only label
  2303. tmpp=skip_space(skip_word(p));
  2304. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2305. i=add_word(p);
  2306. set_label(i,script_pos,p);
  2307. if( parse_options&SCRIPT_USE_LABEL_DB )
  2308. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2309. p=tmpp+1;
  2310. p=skip_space(p);
  2311. continue;
  2312. }
  2313. // All other lumped
  2314. p=parse_line(p);
  2315. p=skip_space(p);
  2316. parse_nextline(false, p);
  2317. }
  2318. add_scriptc(C_NOP);
  2319. // trim code to size
  2320. script_size = script_pos;
  2321. RECREATE(script_buf,unsigned char,script_pos);
  2322. // default unknown references to variables
  2323. for(i=LABEL_START;i<str_num;i++){
  2324. if(str_data[i].type==C_NOP){
  2325. int j;
  2326. str_data[i].type=C_NAME;
  2327. str_data[i].label=i;
  2328. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2329. int next=GETVALUE(script_buf,j);
  2330. SETVALUE(script_buf,j,i);
  2331. j=next;
  2332. }
  2333. }
  2334. else if( str_data[i].type == C_USERFUNC )
  2335. {// 'function name;' without follow-up code
  2336. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2337. unresolved_names = true;
  2338. }
  2339. }
  2340. if( unresolved_names )
  2341. {
  2342. disp_error_message("parse_script: unresolved function references", p);
  2343. }
  2344. #ifdef DEBUG_DISP
  2345. for(i=0;i<script_pos;i++){
  2346. if((i&15)==0) ShowMessage("%04x : ",i);
  2347. ShowMessage("%02x ",script_buf[i]);
  2348. if((i&15)==15) ShowMessage("\n");
  2349. }
  2350. ShowMessage("\n");
  2351. #endif
  2352. #ifdef DEBUG_DISASM
  2353. {
  2354. int i = 0,j;
  2355. while(i < script_pos) {
  2356. c_op op = get_com(script_buf,&i);
  2357. ShowMessage("%06x %s", i, script_op2name(op));
  2358. j = i;
  2359. switch(op) {
  2360. case C_INT:
  2361. ShowMessage(" %d", get_num(script_buf,&i));
  2362. break;
  2363. case C_POS:
  2364. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2365. i += 3;
  2366. break;
  2367. case C_NAME:
  2368. j = (*(int*)(script_buf+i)&0xffffff);
  2369. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2370. i += 3;
  2371. break;
  2372. case C_STR:
  2373. j = strlen(script_buf + i);
  2374. ShowMessage(" %s", script_buf + i);
  2375. i += j+1;
  2376. break;
  2377. }
  2378. ShowMessage(CL_CLL"\n");
  2379. }
  2380. }
  2381. #endif
  2382. CREATE(code,struct script_code,1);
  2383. code->script_buf = script_buf;
  2384. code->script_size = script_size;
  2385. code->local.vars = NULL;
  2386. code->local.arrays = NULL;
  2387. return code;
  2388. }
  2389. /// Returns the player attached to this script, identified by the rid.
  2390. /// If there is no player attached, the script is terminated.
  2391. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){
  2392. *sd = map_id2sd( st->rid );
  2393. if( *sd ){
  2394. return true;
  2395. }else{
  2396. ShowError("%s: fatal error ! player not attached!\n",func);
  2397. script_reportfunc(st);
  2398. script_reportsrc(st);
  2399. st->state = END;
  2400. return false;
  2401. }
  2402. }
  2403. /**
  2404. * Dereferences a variable/constant, replacing it with a copy of the value.
  2405. * @param st Script state
  2406. * @param data Variable/constant
  2407. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2408. */
  2409. struct script_data *get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2410. {
  2411. const char* name;
  2412. char prefix;
  2413. char postfix;
  2414. if( !data_isreference(data) )
  2415. return data;// not a variable/constant
  2416. name = reference_getname(data);
  2417. prefix = name[0];
  2418. postfix = name[strlen(name) - 1];
  2419. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2420. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2421. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2422. if( postfix == '$' ) {// string variable
  2423. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2424. data->type = C_CONSTSTR;
  2425. data->u.str = const_cast<char *>("");
  2426. } else {// integer variable
  2427. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2428. data->type = C_INT;
  2429. data->u.num = 0;
  2430. }
  2431. return data;
  2432. }
  2433. }
  2434. if( postfix == '$' ) {// string variable
  2435. switch( prefix ) {
  2436. case '@':
  2437. data->u.str = pc_readregstr(sd, data->u.num);
  2438. break;
  2439. case '$':
  2440. data->u.str = mapreg_readregstr(data->u.num);
  2441. break;
  2442. case '#':
  2443. if( name[1] == '#' )
  2444. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2445. else
  2446. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2447. break;
  2448. case '.':
  2449. {
  2450. struct DBMap* n = data->ref ?
  2451. data->ref->vars : name[1] == '@' ?
  2452. st->stack->scope.vars : // instance/scope variable
  2453. st->script->local.vars; // npc variable
  2454. if( n )
  2455. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2456. else
  2457. data->u.str = NULL;
  2458. }
  2459. break;
  2460. case '\'':
  2461. {
  2462. struct DBMap* n = nullptr;
  2463. if (data->ref)
  2464. n = data->ref->vars;
  2465. else {
  2466. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2467. if (idata)
  2468. n = idata->regs.vars;
  2469. }
  2470. if (n)
  2471. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2472. else {
  2473. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2474. data->u.str = NULL;
  2475. }
  2476. break;
  2477. }
  2478. default:
  2479. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2480. break;
  2481. }
  2482. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2483. data->type = C_CONSTSTR;
  2484. data->u.str = const_cast<char *>("");
  2485. } else {// duplicate string
  2486. data->type = C_STR;
  2487. data->u.str = aStrdup(data->u.str);
  2488. }
  2489. } else {// integer variable
  2490. data->type = C_INT;
  2491. if( reference_toconstant(data) ) {
  2492. data->u.num = reference_getconstant(data);
  2493. } else if( reference_toparam(data) ) {
  2494. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2495. } else
  2496. switch( prefix ) {
  2497. case '@':
  2498. data->u.num = pc_readreg(sd, data->u.num);
  2499. break;
  2500. case '$':
  2501. data->u.num = mapreg_readreg(data->u.num);
  2502. break;
  2503. case '#':
  2504. if( name[1] == '#' )
  2505. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2506. else
  2507. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2508. break;
  2509. case '.':
  2510. {
  2511. struct DBMap* n = data->ref ?
  2512. data->ref->vars : name[1] == '@' ?
  2513. st->stack->scope.vars : // instance/scope variable
  2514. st->script->local.vars; // npc variable
  2515. if( n )
  2516. data->u.num = i64db_i64get(n,reference_getuid(data));
  2517. else
  2518. data->u.num = 0;
  2519. }
  2520. break;
  2521. case '\'':
  2522. {
  2523. struct DBMap* n = nullptr;
  2524. if (data->ref)
  2525. n = data->ref->vars;
  2526. else {
  2527. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2528. if (idata)
  2529. n = idata->regs.vars;
  2530. }
  2531. if (n)
  2532. data->u.num = i64db_i64get(n,reference_getuid(data));
  2533. else {
  2534. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2535. data->u.num = 0;
  2536. }
  2537. break;
  2538. }
  2539. default:
  2540. data->u.num = pc_readglobalreg(sd, data->u.num);
  2541. break;
  2542. }
  2543. }
  2544. data->ref = NULL;
  2545. return data;
  2546. }
  2547. struct script_data *get_val(struct script_state* st, struct script_data* data)
  2548. {
  2549. return get_val_(st,data,NULL);
  2550. }
  2551. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2552. const char* get_val2_str( struct script_state* st, int64 uid, struct reg_db* ref ){
  2553. push_val2( st->stack, C_NAME, uid, ref );
  2554. struct script_data* data = script_getdatatop( st, -1 );
  2555. get_val( st, data );
  2556. const char* value = "";
  2557. if( data->type == C_INT ){
  2558. ShowError( "get_val2_num: Invalid call. Variable %s is a numeric type.\n", reference_getname( data ) );
  2559. }else{
  2560. value = data->u.str;
  2561. }
  2562. // Do NOT remove the value from stack here, the pointer is returned here and will be used by the caller [Lemongrass]
  2563. // script_removetop( st, -1, 0 );
  2564. return value;
  2565. }
  2566. int64 get_val2_num( struct script_state* st, int64 uid, struct reg_db* ref ){
  2567. push_val2( st->stack, C_NAME, uid, ref );
  2568. struct script_data* data = script_getdatatop( st, -1 );
  2569. get_val( st, data );
  2570. int64 value = 0;
  2571. if( data->type == C_INT ){
  2572. value = data->u.num;
  2573. }else{
  2574. ShowError( "get_val2_num: Invalid call. Variable %s is not a numeric type.\n", reference_getname( data ) );
  2575. }
  2576. script_removetop( st, -1, 0 );
  2577. return value;
  2578. }
  2579. /**
  2580. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2581. * Will be gone as soon as undefined var feature is implemented
  2582. **/
  2583. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2584. {
  2585. const char *name = get_str(script_getvarid(uid));
  2586. // is here st can be null pointer and st->rid is wrong?
  2587. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2588. bool insert = false;
  2589. if (sd && !st) {
  2590. // when sd comes, st isn't available
  2591. insert = true;
  2592. } else {
  2593. if( is_string_variable(name) ) {
  2594. const char* str = get_val2_str( st, uid, ref );
  2595. if( str && *str )
  2596. insert = true;
  2597. // Remove stack entry from get_val2_str
  2598. script_removetop( st, -1, 0 );
  2599. } else {
  2600. int64 num = get_val2_num( st, uid, ref );
  2601. if( num )
  2602. insert = true;
  2603. }
  2604. }
  2605. if (src && src->arrays) {
  2606. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2607. if (sa) {
  2608. unsigned int i;
  2609. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2610. if( i != sa->size ) {
  2611. if( !insert )
  2612. script_array_remove_member(src,sa,i);
  2613. return;
  2614. }
  2615. script_array_add_member(sa,0);
  2616. } else if (insert) {
  2617. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2618. }
  2619. }
  2620. }
  2621. /**
  2622. * Returns array size by ID
  2623. **/
  2624. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2625. {
  2626. struct script_array *sa = NULL;
  2627. struct reg_db *src = script_array_src(st, sd, name, ref);
  2628. if (src && src->arrays)
  2629. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2630. return sa ? sa->size : 0;
  2631. }
  2632. /**
  2633. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2634. **/
  2635. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2636. {
  2637. struct script_array *sa = NULL;
  2638. struct reg_db *src = script_array_src(st, sd, name, ref);
  2639. if (src && src->arrays) {
  2640. int key = add_word(name);
  2641. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2642. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2643. unsigned int i, highest_key = 0;
  2644. for(i = 0; i < sa->size; i++) {
  2645. if( sa->members[i] > highest_key )
  2646. highest_key = sa->members[i];
  2647. }
  2648. return sa->size ? highest_key + 1 : 0;
  2649. }
  2650. }
  2651. return SCRIPT_CMD_SUCCESS;
  2652. }
  2653. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2654. {
  2655. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2656. aFree(sa->members);
  2657. ers_free(array_ers, sa);
  2658. return SCRIPT_CMD_SUCCESS;
  2659. }
  2660. /**
  2661. * Clears script_array and removes it from script->array_db
  2662. **/
  2663. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2664. {
  2665. aFree(sa->members);
  2666. idb_remove(src->arrays, sa->id);
  2667. ers_free(array_ers, sa);
  2668. }
  2669. /**
  2670. * Removes a member from a script_array list
  2671. *
  2672. * @param idx the index of the member in script_array struct list, not of the actual array member
  2673. **/
  2674. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2675. {
  2676. unsigned int i, cursor;
  2677. // it's the only member left, no need to do anything other than delete the array data
  2678. if( sa->size == 1 ) {
  2679. script_array_delete(src,sa);
  2680. return;
  2681. }
  2682. sa->members[idx] = UINT_MAX;
  2683. for(i = 0, cursor = 0; i < sa->size; i++) {
  2684. if( sa->members[i] == UINT_MAX )
  2685. continue;
  2686. if( i != cursor )
  2687. sa->members[cursor] = sa->members[i];
  2688. cursor++;
  2689. }
  2690. sa->size = cursor;
  2691. }
  2692. /**
  2693. * Appends a new array index to the list in script_array
  2694. *
  2695. * @param idx the index of the array member being inserted
  2696. **/
  2697. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2698. {
  2699. RECREATE(sa->members, unsigned int, ++sa->size);
  2700. sa->members[sa->size - 1] = idx;
  2701. }
  2702. /**
  2703. * Obtains the source of the array database for this type and scenario
  2704. * Initializes such database when not yet initialized.
  2705. **/
  2706. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2707. {
  2708. struct reg_db *src = NULL;
  2709. switch( name[0] ) {
  2710. // from player
  2711. default: // char reg
  2712. case '@': // temp char reg
  2713. case '#': // account reg
  2714. src = &sd->regs;
  2715. break;
  2716. case '$': // map reg
  2717. src = &regs;
  2718. break;
  2719. case '.': // npc/script
  2720. if (ref)
  2721. src = ref;
  2722. else
  2723. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2724. break;
  2725. case '\'': // instance
  2726. {
  2727. if (ref)
  2728. src = ref;
  2729. else {
  2730. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2731. if (idata)
  2732. src = &idata->regs;
  2733. }
  2734. break;
  2735. }
  2736. }
  2737. if( src ) {
  2738. if( !src->arrays )
  2739. src->arrays = idb_alloc(DB_OPT_BASE);
  2740. return src;
  2741. }
  2742. return NULL;
  2743. }
  2744. /**
  2745. * Processes a array member modification, and update data accordingly
  2746. *
  2747. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2748. * @param num[in] Variable ID
  2749. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2750. **/
  2751. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2752. {
  2753. struct script_array *sa = NULL;
  2754. int id = script_getvarid(num);
  2755. unsigned int index = script_getvaridx(num);
  2756. if (!src->arrays) {
  2757. src->arrays = idb_alloc(DB_OPT_BASE);
  2758. } else {
  2759. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2760. }
  2761. if( sa ) {
  2762. unsigned int i;
  2763. // search
  2764. for(i = 0; i < sa->size; i++) {
  2765. if( sa->members[i] == index )
  2766. break;
  2767. }
  2768. // if existent
  2769. if( i != sa->size ) {
  2770. // if empty, we gotta remove it
  2771. if( empty ) {
  2772. script_array_remove_member(src, sa, i);
  2773. }
  2774. } else if( !empty ) { /* new entry */
  2775. script_array_add_member(sa,index);
  2776. // we do nothing if its empty, no point in modifying array data for a new empty member
  2777. }
  2778. } else if ( !empty ) { // we only move to create if not empty
  2779. sa = ers_alloc(array_ers, struct script_array);
  2780. sa->id = id;
  2781. sa->members = NULL;
  2782. sa->size = 0;
  2783. script_array_add_member(sa,index);
  2784. idb_put(src->arrays, id, sa);
  2785. }
  2786. }
  2787. /**
  2788. * Stores the value of a script variable
  2789. *
  2790. * @param st: current script state.
  2791. * @param sd: current character data.
  2792. * @param num: variable identifier.
  2793. * @param name: variable name.
  2794. * @param value: new value.
  2795. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2796. * @return: 0 failure, 1 success.
  2797. *
  2798. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2799. *------------------------------------------*/
  2800. 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 ){
  2801. char prefix = name[0];
  2802. size_t vlen = 0;
  2803. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2804. 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 );
  2805. return false;
  2806. }
  2807. if( !is_string_variable( name ) ){
  2808. // integer variable
  2809. return false;
  2810. }
  2811. switch( prefix ){
  2812. case '@':
  2813. pc_setregstr( sd, num, value );
  2814. return true;
  2815. case '$':
  2816. return mapreg_setregstr( num, value );
  2817. case '#':
  2818. return ( name[1] == '#' ) ? pc_setaccountreg2str( sd, num, value ) : pc_setaccountregstr( sd, num, value );
  2819. case '.': {
  2820. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2821. if( n ){
  2822. if( value[0] ){
  2823. i64db_put( n->vars, num, aStrdup( value ) );
  2824. if( script_getvaridx( num ) ){
  2825. script_array_update( n, num, false );
  2826. }
  2827. }else{
  2828. i64db_remove( n->vars, num );
  2829. if( script_getvaridx( num ) ){
  2830. script_array_update( n, num, true );
  2831. }
  2832. }
  2833. }
  2834. }
  2835. return true;
  2836. case '\'': {
  2837. struct reg_db *src = nullptr;
  2838. if( ref ){
  2839. src = ref;
  2840. }else{
  2841. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2842. if (idata)
  2843. src = &idata->regs;
  2844. }
  2845. if( src ){
  2846. bool empty;
  2847. if( value[0] ){
  2848. i64db_put( src->vars, num, aStrdup( value ) );
  2849. empty = false;
  2850. }else{
  2851. i64db_remove( src->vars, num );
  2852. empty = true;
  2853. }
  2854. if( script_getvaridx( num ) != 0 ){
  2855. script_array_update( src, num, empty );
  2856. }
  2857. }else{
  2858. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2859. script_reportsrc( st );
  2860. }
  2861. return true;
  2862. }
  2863. default:
  2864. return pc_setglobalreg_str( sd, num, value );
  2865. }
  2866. }
  2867. bool set_reg_num( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, int64 value, struct reg_db *ref ){
  2868. char prefix = name[0];
  2869. size_t vlen = 0;
  2870. if( !script_check_RegistryVariableLength( 0, name, &vlen ) ){
  2871. 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 );
  2872. return false;
  2873. }
  2874. if( is_string_variable( name ) ){
  2875. // string variable
  2876. return false;
  2877. }
  2878. if( str_data[script_getvarid(num)].type == C_PARAM ){
  2879. if( pc_setparam( sd, str_data[script_getvarid(num)].val, value ) == 0 ){
  2880. if( st != NULL ) {
  2881. ShowError( "script_set_reg: failed to set param '%s' to %" PRId64 ".\n", name, value );
  2882. script_reportsrc( st );
  2883. st->state = END;
  2884. }
  2885. return false;
  2886. }
  2887. return true;
  2888. }
  2889. switch( prefix ){
  2890. case '@':
  2891. pc_setreg( sd, num, value );
  2892. return true;
  2893. case '$':
  2894. return mapreg_setreg( num, value );
  2895. case '#':
  2896. return ( name[1] == '#' ) ? pc_setaccountreg2(sd, num, value) : pc_setaccountreg( sd, num, value );
  2897. case '.': {
  2898. struct reg_db *n = ( ref ) ? ref : ( name[1] == '@' ) ? &st->stack->scope : &st->script->local;
  2899. if( n ){
  2900. if( value != 0 ){
  2901. i64db_i64put( n->vars, num, value );
  2902. if( script_getvaridx( num ) ){
  2903. script_array_update( n, num, false );
  2904. }
  2905. }else{
  2906. i64db_remove( n->vars, num );
  2907. if( script_getvaridx( num ) ){
  2908. script_array_update( n, num, true );
  2909. }
  2910. }
  2911. }
  2912. }
  2913. return true;
  2914. case '\'': {
  2915. struct reg_db *src = nullptr;
  2916. if( ref ){
  2917. src = ref;
  2918. }else{
  2919. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, script_instancegetid(st));
  2920. if (idata)
  2921. src = &idata->regs;
  2922. }
  2923. if( src ){
  2924. bool empty;
  2925. if( value != 0 ){
  2926. i64db_i64put( src->vars, num, value );
  2927. empty = false;
  2928. }else{
  2929. i64db_remove( src->vars, num );
  2930. empty = true;
  2931. }
  2932. if( script_getvaridx( num ) != 0 ){
  2933. script_array_update( src, num, empty );
  2934. }
  2935. }else{
  2936. ShowError( "script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name );
  2937. script_reportsrc( st );
  2938. }
  2939. return true;
  2940. }
  2941. default:
  2942. return pc_setglobalreg( sd, num, value );
  2943. }
  2944. }
  2945. bool set_var_str( struct map_session_data* sd, const char* name, const char* val ){
  2946. return set_reg_str( NULL, sd, reference_uid( add_str( name ), 0 ), name, val, NULL );
  2947. }
  2948. bool clear_reg( struct script_state* st, struct map_session_data* sd, int64 num, const char* name, struct reg_db *ref ){
  2949. if( is_string_variable( name ) ){
  2950. return set_reg_str( st, sd, num, name, "", ref );
  2951. }else{
  2952. return set_reg_num( st, sd, num, name, 0, ref );
  2953. }
  2954. }
  2955. void setd_sub_num( struct script_state* st, struct map_session_data* sd, const char* varname, int elem, int64 value, struct reg_db* ref ){
  2956. set_reg_num( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2957. }
  2958. 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 ){
  2959. set_reg_str( st, sd, reference_uid( add_str( varname ), elem ), varname, value, ref );
  2960. }
  2961. /**
  2962. * Converts the data to a string
  2963. * @param st
  2964. * @param data
  2965. * @param sd
  2966. */
  2967. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2968. {
  2969. char* p;
  2970. get_val_(st, data, sd);
  2971. if( data_isstring(data) )
  2972. {// nothing to convert
  2973. }
  2974. else if( data_isint(data) )
  2975. {// int -> string
  2976. CREATE(p, char, ITEM_NAME_LENGTH);
  2977. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2978. p[ITEM_NAME_LENGTH-1] = '\0';
  2979. data->type = C_STR;
  2980. data->u.str = p;
  2981. }
  2982. else if( data_isreference(data) )
  2983. {// reference -> string
  2984. //##TODO when does this happen (check get_val) [FlavioJS] -- at getd!!
  2985. data->type = C_CONSTSTR;
  2986. data->u.str = reference_getname(data);
  2987. }
  2988. else
  2989. {// unsupported data type
  2990. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2991. script_reportdata(data);
  2992. script_reportsrc(st);
  2993. data->type = C_CONSTSTR;
  2994. data->u.str = const_cast<char *>("");
  2995. }
  2996. return data->u.str;
  2997. }
  2998. const char* conv_str(struct script_state* st, struct script_data* data)
  2999. {
  3000. return conv_str_(st, data, NULL);
  3001. }
  3002. /**
  3003. * Converts the data to an int
  3004. * @param st
  3005. * @param data
  3006. * @param sd
  3007. */
  3008. int64 conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  3009. {
  3010. get_val_(st, data, sd);
  3011. if( data_isint(data) )
  3012. {// nothing to convert
  3013. }
  3014. else if( data_isstring(data) )
  3015. {// string -> int
  3016. // the result does not overflow or underflow, it is capped instead
  3017. // ex: 999999999999 is capped to INT_MAX (2147483647)
  3018. char* p = data->u.str;
  3019. int64 num;
  3020. bool error;
  3021. errno = 0;
  3022. num = strtoll(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  3023. error = (errno == ERANGE);
  3024. if( num < SCRIPT_INT_MIN || ( error && num == INT64_MIN ) ){
  3025. num = SCRIPT_INT_MIN;
  3026. ShowError( "script:conv_num: underflow detected, capping value to %" PRId64 "", SCRIPT_INT_MIN );
  3027. script_reportdata(data);
  3028. script_reportsrc(st);
  3029. }else if( num > SCRIPT_INT_MAX || ( error && num == INT64_MAX ) ){
  3030. num = SCRIPT_INT_MAX;
  3031. ShowError( "script:conv_num: overflow detected, capping value to %" PRId64 "", SCRIPT_INT_MAX );
  3032. script_reportdata(data);
  3033. script_reportsrc(st);
  3034. }
  3035. if( data->type == C_STR )
  3036. aFree(p);
  3037. data->type = C_INT;
  3038. data->u.num = num;
  3039. }
  3040. #if 0
  3041. // FIXME this function is being used to retrieve the position of labels and
  3042. // probably other stuff [FlavioJS]
  3043. else
  3044. {// unsupported data type
  3045. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3046. script_reportdata(data);
  3047. script_reportsrc(st);
  3048. data->type = C_INT;
  3049. data->u.num = 0;
  3050. }
  3051. #endif
  3052. return data->u.num;
  3053. }
  3054. int64 conv_num64(struct script_state* st, struct script_data* data)
  3055. {
  3056. return conv_num_(st, data, NULL);
  3057. }
  3058. int conv_num(struct script_state* st, struct script_data* data)
  3059. {
  3060. return static_cast<int>(conv_num_(st, data, NULL));
  3061. }
  3062. //
  3063. // Stack operations
  3064. //
  3065. /// Increases the size of the stack
  3066. void stack_expand(struct script_stack* stack)
  3067. {
  3068. stack->sp_max += 64;
  3069. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3070. stack->sp_max * sizeof(stack->stack_data[0]) );
  3071. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3072. 64 * sizeof(stack->stack_data[0]) );
  3073. }
  3074. /// Pushes a value into the stack
  3075. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  3076. /// Pushes a value into the stack (with reference)
  3077. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3078. {
  3079. if( stack->sp >= stack->sp_max )
  3080. stack_expand(stack);
  3081. stack->stack_data[stack->sp].type = type;
  3082. stack->stack_data[stack->sp].u.num = val;
  3083. stack->stack_data[stack->sp].ref = ref;
  3084. stack->sp++;
  3085. return &stack->stack_data[stack->sp-1];
  3086. }
  3087. /// Pushes a string into the stack
  3088. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3089. {
  3090. if( stack->sp >= stack->sp_max )
  3091. stack_expand(stack);
  3092. stack->stack_data[stack->sp].type = type;
  3093. stack->stack_data[stack->sp].u.str = str;
  3094. stack->stack_data[stack->sp].ref = NULL;
  3095. stack->sp++;
  3096. return &stack->stack_data[stack->sp-1];
  3097. }
  3098. /// Pushes a retinfo into the stack
  3099. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3100. {
  3101. if( stack->sp >= stack->sp_max )
  3102. stack_expand(stack);
  3103. stack->stack_data[stack->sp].type = C_RETINFO;
  3104. stack->stack_data[stack->sp].u.ri = ri;
  3105. stack->stack_data[stack->sp].ref = ref;
  3106. stack->sp++;
  3107. return &stack->stack_data[stack->sp-1];
  3108. }
  3109. /// Pushes a copy of the target position into the stack
  3110. struct script_data* push_copy(struct script_stack* stack, int pos)
  3111. {
  3112. switch( stack->stack_data[pos].type ) {
  3113. case C_CONSTSTR:
  3114. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3115. break;
  3116. case C_STR:
  3117. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3118. break;
  3119. case C_RETINFO:
  3120. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3121. exit(1);
  3122. break;
  3123. default:
  3124. return push_val2(
  3125. stack,stack->stack_data[pos].type,
  3126. stack->stack_data[pos].u.num,
  3127. stack->stack_data[pos].ref
  3128. );
  3129. break;
  3130. }
  3131. }
  3132. /// Removes the values in indexes [start,end] from the stack.
  3133. /// Adjusts all stack pointers.
  3134. void pop_stack(struct script_state* st, int start, int end)
  3135. {
  3136. struct script_stack* stack = st->stack;
  3137. struct script_data* data;
  3138. int i;
  3139. if( start < 0 )
  3140. start = 0;
  3141. if( end > stack->sp )
  3142. end = stack->sp;
  3143. if( start >= end )
  3144. return;// nothing to pop
  3145. // free stack elements
  3146. for( i = start; i < end; i++ )
  3147. {
  3148. data = &stack->stack_data[i];
  3149. if( data->type == C_STR )
  3150. aFree(data->u.str);
  3151. if( data->type == C_RETINFO ) {
  3152. struct script_retinfo* ri = data->u.ri;
  3153. if (ri->scope.vars) {
  3154. script_free_vars(ri->scope.vars);
  3155. ri->scope.vars = NULL;
  3156. }
  3157. if (ri->scope.arrays) {
  3158. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3159. ri->scope.arrays = NULL;
  3160. }
  3161. if( data->ref )
  3162. aFree(data->ref);
  3163. aFree(ri);
  3164. }
  3165. data->type = C_NOP;
  3166. }
  3167. // move the rest of the elements
  3168. if( stack->sp > end )
  3169. {
  3170. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3171. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3172. stack->stack_data[i].type = C_NOP;
  3173. }
  3174. // adjust stack pointers
  3175. if( st->start > end ){
  3176. st->start -= end - start;
  3177. }else if( st->start > start ){
  3178. st->start = start;
  3179. }
  3180. if( st->end > end ){
  3181. st->end -= end - start;
  3182. }else if( st->end > start ){
  3183. st->end = start;
  3184. }
  3185. if( stack->defsp > end ){
  3186. stack->defsp -= end - start;
  3187. }else if( stack->defsp > start ){
  3188. stack->defsp = start;
  3189. }
  3190. stack->sp -= end - start;
  3191. }
  3192. ///
  3193. ///
  3194. ///
  3195. /*==========================================
  3196. * Release script dependent variable, dependent variable of function
  3197. *------------------------------------------*/
  3198. void script_free_vars(struct DBMap* storage)
  3199. {
  3200. if( storage ) {
  3201. // destroy the storage construct containing the variables
  3202. db_destroy(storage);
  3203. }
  3204. }
  3205. void script_free_code(struct script_code* code)
  3206. {
  3207. nullpo_retv(code);
  3208. if (code->instances)
  3209. script_stop_scriptinstances(code);
  3210. script_free_vars(code->local.vars);
  3211. if (code->local.arrays)
  3212. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3213. aFree(code->script_buf);
  3214. aFree(code);
  3215. }
  3216. /// Creates a new script state.
  3217. ///
  3218. /// @param script Script code
  3219. /// @param pos Position in the code
  3220. /// @param rid Who is running the script (attached player)
  3221. /// @param oid Where the code is being run (npc 'object')
  3222. /// @return Script state
  3223. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3224. {
  3225. struct script_state* st;
  3226. st = ers_alloc(st_ers, struct script_state);
  3227. st->stack = ers_alloc(stack_ers, struct script_stack);
  3228. st->stack->sp = 0;
  3229. st->stack->sp_max = 64;
  3230. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3231. st->stack->defsp = st->stack->sp;
  3232. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3233. st->stack->scope.arrays = NULL;
  3234. st->state = RUN;
  3235. st->script = rootscript;
  3236. st->pos = pos;
  3237. st->rid = rid;
  3238. st->oid = oid;
  3239. st->sleep.timer = INVALID_TIMER;
  3240. st->npc_item_flag = battle_config.item_enabled_npc;
  3241. if( st->script->instances != USHRT_MAX )
  3242. st->script->instances++;
  3243. else {
  3244. struct npc_data *nd = map_id2nd(oid);
  3245. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3246. }
  3247. if (!st->script->local.vars)
  3248. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3249. st->id = next_id++;
  3250. active_scripts++;
  3251. idb_put(st_db, st->id, st);
  3252. return st;
  3253. }
  3254. /// Frees a script state.
  3255. ///
  3256. /// @param st Script state
  3257. void script_free_state(struct script_state* st)
  3258. {
  3259. if (idb_exists(st_db, st->id)) {
  3260. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3261. if (st->bk_st) // backup was not restored
  3262. 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);
  3263. if (sd && sd->st == st) { // Current script is aborted.
  3264. if(sd->state.using_fake_npc) {
  3265. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3266. sd->state.using_fake_npc = 0;
  3267. }
  3268. sd->st = NULL;
  3269. sd->npc_id = 0;
  3270. }
  3271. if (st->sleep.timer != INVALID_TIMER)
  3272. delete_timer(st->sleep.timer, run_script_timer);
  3273. if (st->stack) {
  3274. script_free_vars(st->stack->scope.vars);
  3275. if (st->stack->scope.arrays)
  3276. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3277. pop_stack(st, 0, st->stack->sp);
  3278. aFree(st->stack->stack_data);
  3279. ers_free(stack_ers, st->stack);
  3280. st->stack = NULL;
  3281. }
  3282. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3283. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3284. script_free_vars(st->script->local.vars);
  3285. st->script->local.vars = NULL;
  3286. }
  3287. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3288. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3289. st->script->local.arrays = NULL;
  3290. }
  3291. }
  3292. st->pos = -1;
  3293. idb_remove(st_db, st->id);
  3294. ers_free(st_ers, st);
  3295. if (--active_scripts == 0)
  3296. next_id = 0;
  3297. }
  3298. }
  3299. //
  3300. // Main execution unit
  3301. //
  3302. /*==========================================
  3303. * Read command
  3304. *------------------------------------------*/
  3305. c_op get_com(unsigned char *script,int *pos)
  3306. {
  3307. int i = 0, j = 0;
  3308. if(script[*pos]>=0x80){
  3309. return C_INT;
  3310. }
  3311. while(script[*pos]>=0x40){
  3312. i=script[(*pos)++]<<j;
  3313. j+=6;
  3314. }
  3315. return (c_op)(i+(script[(*pos)++]<<j));
  3316. }
  3317. /*==========================================
  3318. * Income figures
  3319. *------------------------------------------*/
  3320. int64 get_num(unsigned char *script,int *pos)
  3321. {
  3322. int i, end;
  3323. int64 value;
  3324. // Find the end of the data
  3325. for( i = 0; script[*pos+i] >= 0xc0; i++ ) ; // do nothing just find it
  3326. end = i; // store the end for later
  3327. value = 0; // initialize empty value
  3328. // If more than one byte of data exists
  3329. for( ; i > 0; i-- ){
  3330. value |= (int64)( script[*pos+i] & (int64)0x3f );
  3331. value <<= 6;
  3332. }
  3333. // Put our last piece of data into the return value
  3334. value |= (script[*pos+i] & (int64)0x3f);
  3335. *pos += end + 1; // one byte after the last byte of data
  3336. return value;
  3337. }
  3338. /// Ternary operators
  3339. /// test ? if_true : if_false
  3340. void op_3(struct script_state* st, int op)
  3341. {
  3342. struct script_data* data;
  3343. int flag = 0;
  3344. data = script_getdatatop(st, -3);
  3345. get_val(st, data);
  3346. if( data_isstring(data) )
  3347. flag = data->u.str[0];// "" -> false
  3348. else if( data_isint(data) )
  3349. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3350. else
  3351. {
  3352. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3353. script_reportdata(data);
  3354. script_reportsrc(st);
  3355. script_removetop(st, -3, 0);
  3356. script_pushnil(st);
  3357. return;
  3358. }
  3359. if( flag )
  3360. script_pushcopytop(st, -2);
  3361. else
  3362. script_pushcopytop(st, -1);
  3363. script_removetop(st, -4, -1);
  3364. }
  3365. /// Binary string operators
  3366. /// s1 EQ s2 -> i
  3367. /// s1 NE s2 -> i
  3368. /// s1 GT s2 -> i
  3369. /// s1 GE s2 -> i
  3370. /// s1 LT s2 -> i
  3371. /// s1 LE s2 -> i
  3372. /// s1 ADD s2 -> s
  3373. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3374. {
  3375. int a = 0;
  3376. switch(op){
  3377. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3378. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3379. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3380. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3381. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3382. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3383. case C_ADD:
  3384. {
  3385. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3386. strcpy(buf, s1);
  3387. strcat(buf, s2);
  3388. script_pushstr(st, buf);
  3389. return;
  3390. }
  3391. default:
  3392. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3393. script_reportsrc(st);
  3394. script_pushnil(st);
  3395. st->state = END;
  3396. return;
  3397. }
  3398. script_pushint(st,a);
  3399. }
  3400. /// Binary number operators
  3401. /// i OP i -> i
  3402. void op_2num( struct script_state* st, int op, int64 i1, int64 i2 ){
  3403. int64 ret;
  3404. switch( op ) {
  3405. case C_AND: ret = i1 & i2; break;
  3406. case C_OR: ret = i1 | i2; break;
  3407. case C_XOR: ret = i1 ^ i2; break;
  3408. case C_LAND: ret = (i1 && i2); break;
  3409. case C_LOR: ret = (i1 || i2); break;
  3410. case C_EQ: ret = (i1 == i2); break;
  3411. case C_NE: ret = (i1 != i2); break;
  3412. case C_GT: ret = (i1 > i2); break;
  3413. case C_GE: ret = (i1 >= i2); break;
  3414. case C_LT: ret = (i1 < i2); break;
  3415. case C_LE: ret = (i1 <= i2); break;
  3416. case C_R_SHIFT: ret = i1>>i2; break;
  3417. case C_L_SHIFT: ret = i1<<i2; break;
  3418. case C_DIV:
  3419. case C_MOD:
  3420. if( i2 == 0 )
  3421. {
  3422. ShowError("script:op_2num: division by zero detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3423. script_reportsrc(st);
  3424. script_pushnil(st);
  3425. st->state = END;
  3426. return;
  3427. }
  3428. else if( op == C_DIV )
  3429. ret = i1 / i2;
  3430. else//if( op == C_MOD )
  3431. ret = i1 % i2;
  3432. break;
  3433. default:
  3434. bool overflow;
  3435. // operators that can overflow/underflow
  3436. switch( op ) {
  3437. case C_ADD: overflow = util::safe_addition( i1, i2, ret ); break;
  3438. case C_SUB: overflow = util::safe_substraction( i1, i2, ret ); break;
  3439. case C_MUL: overflow = util::safe_multiplication( i1, i2, ret ); break;
  3440. default:
  3441. ShowError("script:op_2num: unexpected number operator %s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3442. script_reportsrc(st);
  3443. script_pushnil(st);
  3444. return;
  3445. }
  3446. if( overflow ){
  3447. ShowWarning("script:op_2num: overflow detected op=%s i1=%" PRId64 " i2=%" PRId64 "\n", script_op2name(op), i1, i2);
  3448. script_reportsrc(st);
  3449. script_pushnil(st);
  3450. st->state = END;
  3451. return;
  3452. }
  3453. }
  3454. script_pushint(st, ret);
  3455. }
  3456. /// Binary operators
  3457. void op_2(struct script_state *st, int op)
  3458. {
  3459. struct script_data* left, leftref;
  3460. struct script_data* right;
  3461. leftref.type = C_NOP;
  3462. left = script_getdatatop(st, -2);
  3463. right = script_getdatatop(st, -1);
  3464. if (st->op2ref) {
  3465. if (data_isreference(left)) {
  3466. leftref = *left;
  3467. }
  3468. st->op2ref = 0;
  3469. }
  3470. get_val(st, left);
  3471. get_val(st, right);
  3472. // automatic conversions
  3473. switch( op )
  3474. {
  3475. case C_ADD:
  3476. if( data_isint(left) && data_isstring(right) )
  3477. {// convert int-string to string-string
  3478. conv_str(st, left);
  3479. }
  3480. else if( data_isstring(left) && data_isint(right) )
  3481. {// convert string-int to string-string
  3482. conv_str(st, right);
  3483. }
  3484. break;
  3485. }
  3486. if( data_isstring(left) && data_isstring(right) )
  3487. {// ss => op_2str
  3488. op_2str(st, op, left->u.str, right->u.str);
  3489. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3490. if (leftref.type != C_NOP)
  3491. {
  3492. if (left->type == C_STR) // don't free C_CONSTSTR
  3493. aFree(left->u.str);
  3494. *left = leftref;
  3495. }
  3496. }
  3497. else if( data_isint(left) && data_isint(right) )
  3498. {// ii => op_2num
  3499. int64 i1 = left->u.num;
  3500. int64 i2 = right->u.num;
  3501. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3502. op_2num(st, op, i1, i2);
  3503. if (leftref.type != C_NOP)
  3504. *left = leftref;
  3505. }
  3506. else
  3507. {// invalid argument
  3508. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3509. script_reportdata(left);
  3510. script_reportdata(right);
  3511. script_reportsrc(st);
  3512. script_removetop(st, -2, 0);
  3513. script_pushnil(st);
  3514. st->state = END;
  3515. }
  3516. }
  3517. /// Unary operators
  3518. /// NEG i -> i
  3519. /// NOT i -> i
  3520. /// LNOT i -> i
  3521. void op_1(struct script_state* st, int op)
  3522. {
  3523. struct script_data* data;
  3524. data = script_getdatatop(st, -1);
  3525. get_val(st, data);
  3526. if( !data_isint(data) )
  3527. {// not a number
  3528. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3529. script_reportdata(data);
  3530. script_reportsrc(st);
  3531. script_pushnil(st);
  3532. st->state = END;
  3533. return;
  3534. }
  3535. int64 i1 = data->u.num;
  3536. script_removetop(st, -1, 0);
  3537. switch( op )
  3538. {
  3539. case C_NEG: i1 = -i1; break;
  3540. case C_NOT: i1 = ~i1; break;
  3541. case C_LNOT: i1 = !i1; break;
  3542. default:
  3543. ShowError("script:op_1: unexpected operator %s i1=%" PRId64 "\n", script_op2name(op), i1);
  3544. script_reportsrc(st);
  3545. script_pushnil(st);
  3546. st->state = END;
  3547. return;
  3548. }
  3549. script_pushint(st, i1);
  3550. }
  3551. /// Checks the type of all arguments passed to a built-in function.
  3552. ///
  3553. /// @param st Script state whose stack arguments should be inspected.
  3554. /// @param func Built-in function for which the arguments are intended.
  3555. static void script_check_buildin_argtype(struct script_state* st, int func)
  3556. {
  3557. int idx, invalid = 0;
  3558. for( idx = 2; script_hasdata(st, idx); idx++ )
  3559. {
  3560. struct script_data* data = script_getdata(st, idx);
  3561. script_function* sf = &buildin_func[str_data[func].val];
  3562. char type = sf->arg[idx-2];
  3563. if( type == '?' || type == '*' )
  3564. {// optional argument or unknown number of optional parameters ( no types are after this )
  3565. break;
  3566. }
  3567. else if( type == 0 )
  3568. {// more arguments than necessary ( should not happen, as it is checked before )
  3569. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3570. invalid++;
  3571. break;
  3572. }
  3573. else
  3574. {
  3575. const char* name = NULL;
  3576. if( data_isreference(data) )
  3577. {// get name for variables to determine the type they refer to
  3578. name = reference_getname(data);
  3579. }
  3580. switch( type )
  3581. {
  3582. case 'v':
  3583. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3584. {// variant
  3585. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3586. script_reportdata(data);
  3587. invalid++;
  3588. }
  3589. break;
  3590. case 's':
  3591. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3592. {// string
  3593. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3594. script_reportdata(data);
  3595. invalid++;
  3596. }
  3597. break;
  3598. case 'i':
  3599. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3600. {// int ( params and constants are always int )
  3601. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3602. script_reportdata(data);
  3603. invalid++;
  3604. }
  3605. break;
  3606. case 'r':
  3607. if( !data_isreference(data) )
  3608. {// variables
  3609. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3610. script_reportdata(data);
  3611. invalid++;
  3612. }
  3613. break;
  3614. case 'l':
  3615. if( !data_islabel(data) && !data_isfunclabel(data) )
  3616. {// label
  3617. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3618. script_reportdata(data);
  3619. invalid++;
  3620. }
  3621. break;
  3622. }
  3623. }
  3624. }
  3625. if(invalid)
  3626. {
  3627. ShowDebug("Function: %s\n", get_str(func));
  3628. script_reportsrc(st);
  3629. }
  3630. }
  3631. /// Executes a buildin command.
  3632. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3633. int run_func(struct script_state *st)
  3634. {
  3635. struct script_data* data;
  3636. int i,start_sp,end_sp,func;
  3637. end_sp = st->stack->sp;// position after the last argument
  3638. for( i = end_sp-1; i > 0 ; --i )
  3639. if( st->stack->stack_data[i].type == C_ARG )
  3640. break;
  3641. if( i == 0 ) {
  3642. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3643. st->state = END;
  3644. script_reportsrc(st);
  3645. return 1;
  3646. }
  3647. start_sp = i-1;// C_NAME of the command
  3648. st->start = start_sp;
  3649. st->end = end_sp;
  3650. data = &st->stack->stack_data[st->start];
  3651. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3652. func = (int)data->u.num;
  3653. st->funcname = reference_getname(data);
  3654. } else {
  3655. ShowError("script:run_func: not a buildin command.\n");
  3656. script_reportdata(data);
  3657. script_reportsrc(st);
  3658. st->state = END;
  3659. return 1;
  3660. }
  3661. if( script_config.warn_func_mismatch_argtypes ) {
  3662. script_check_buildin_argtype(st, func);
  3663. }
  3664. if(str_data[func].func) {
  3665. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3666. if( buildin_func[str_data[func].val].deprecated ){
  3667. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3668. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3669. script_reportsrc(st);
  3670. }
  3671. #endif
  3672. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3673. script_reportsrc(st);
  3674. } else {
  3675. 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));
  3676. script_reportsrc(st);
  3677. st->state = END;
  3678. }
  3679. // Stack's datum are used when re-running functions [Eoe]
  3680. if( st->state == RERUNLINE )
  3681. return 0;
  3682. pop_stack(st, st->start, st->end);
  3683. if( st->state == RETFUNC ) {// return from a user-defined function
  3684. struct script_retinfo* ri;
  3685. int olddefsp = st->stack->defsp;
  3686. int nargs;
  3687. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3688. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3689. {
  3690. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3691. script_reportsrc(st);
  3692. st->state = END;
  3693. return 1;
  3694. }
  3695. script_free_vars(st->stack->scope.vars);
  3696. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3697. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3698. nargs = ri->nargs;
  3699. st->pos = ri->pos;
  3700. st->script = ri->script;
  3701. st->stack->scope.vars = ri->scope.vars;
  3702. st->stack->scope.arrays = ri->scope.arrays;
  3703. st->stack->defsp = ri->defsp;
  3704. memset(ri, 0, sizeof(struct script_retinfo));
  3705. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3706. st->state = GOTO;
  3707. }
  3708. return 0;
  3709. }
  3710. /*==========================================
  3711. * script execution
  3712. *------------------------------------------*/
  3713. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3714. {
  3715. struct script_state *st;
  3716. if( rootscript == NULL || pos < 0 )
  3717. return;
  3718. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3719. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3720. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3721. st = script_alloc_state(rootscript, pos, rid, oid);
  3722. run_script_main(st);
  3723. }
  3724. /**
  3725. * Free all related script code
  3726. * @param code: Script code to free
  3727. */
  3728. void script_stop_scriptinstances(struct script_code *code) {
  3729. DBIterator *iter;
  3730. struct script_state* st;
  3731. if( !active_scripts )
  3732. return; // Don't even bother.
  3733. iter = db_iterator(st_db);
  3734. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3735. if( st->script == code )
  3736. script_free_state(st);
  3737. }
  3738. dbi_destroy(iter);
  3739. }
  3740. /*==========================================
  3741. * Timer function for sleep
  3742. *------------------------------------------*/
  3743. TIMER_FUNC(run_script_timer){
  3744. struct script_state *st = (struct script_state *)data;
  3745. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3746. // If it was a player before going to sleep and there is still a unit attached to the script
  3747. if( id != 0 && st->rid ){
  3748. struct map_session_data *sd = map_id2sd(st->rid);
  3749. // Attached player is offline(logout) or another unit type(should not happen)
  3750. if( !sd ){
  3751. st->rid = 0;
  3752. st->state = END;
  3753. // Character mismatch. Cancel execution.
  3754. }else if( sd->status.char_id != id ){
  3755. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3756. script_reportsrc(st);
  3757. st->rid = 0;
  3758. st->state = END;
  3759. }
  3760. }
  3761. while (node && st->sleep.timer != INVALID_TIMER) {
  3762. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3763. script_erase_sleepdb(node);
  3764. st->sleep.timer = INVALID_TIMER;
  3765. break;
  3766. }
  3767. node = node->next;
  3768. }
  3769. if(st->state != RERUNLINE)
  3770. st->sleep.tick = 0;
  3771. run_script_main(st);
  3772. return 0;
  3773. }
  3774. /**
  3775. * Remove sleep timers from the NPC
  3776. * @param id: NPC ID
  3777. */
  3778. void script_stop_sleeptimers(int id) {
  3779. for (;;) {
  3780. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3781. if (!st)
  3782. break; // No more sleep timers
  3783. if (st->oid == id)
  3784. script_free_state(st);
  3785. }
  3786. }
  3787. /**
  3788. * Delete the specified node from sleep_db
  3789. * @param n: Linked list of sleep timers
  3790. */
  3791. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3792. struct linkdb_node *retnode;
  3793. if (!n)
  3794. return NULL;
  3795. if (!n->prev)
  3796. sleep_db = n->next;
  3797. else
  3798. n->prev->next = n->next;
  3799. if (n->next)
  3800. n->next->prev = n->prev;
  3801. retnode = n->next;
  3802. aFree(n);
  3803. return retnode;
  3804. }
  3805. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3806. ///
  3807. /// @param st Script state to detach.
  3808. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3809. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3810. {
  3811. struct map_session_data* sd;
  3812. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3813. if( sd->state.using_fake_npc ){
  3814. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, sd->fd );
  3815. sd->state.using_fake_npc = 0;
  3816. }
  3817. sd->st = st->bk_st;
  3818. sd->npc_id = st->bk_npcid;
  3819. sd->state.disable_atcommand_on_npc = 0;
  3820. if(st->bk_st) {
  3821. //Remove tag for removal.
  3822. st->bk_st = NULL;
  3823. st->bk_npcid = 0;
  3824. } else if(dequeue_event) {
  3825. #ifdef SECURE_NPCTIMEOUT
  3826. /**
  3827. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3828. **/
  3829. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3830. delete_timer(sd->npc_idle_timer,npc_secure_timeout_timer);
  3831. sd->npc_idle_timer = INVALID_TIMER;
  3832. }
  3833. #endif
  3834. npc_event_dequeue(sd);
  3835. }
  3836. }
  3837. else if(st->bk_st)
  3838. {// rid was set to 0, before detaching the script state
  3839. 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);
  3840. script_reportsrc(st->bk_st);
  3841. script_free_state(st->bk_st);
  3842. st->bk_st = NULL;
  3843. }
  3844. }
  3845. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3846. ///
  3847. /// @param st Script state to attach.
  3848. void script_attach_state(struct script_state* st){
  3849. struct map_session_data* sd;
  3850. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3851. {
  3852. if(st!=sd->st)
  3853. {
  3854. if(st->bk_st)
  3855. {// there is already a backup
  3856. 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);
  3857. }
  3858. st->bk_st = sd->st;
  3859. st->bk_npcid = sd->npc_id;
  3860. }
  3861. sd->st = st;
  3862. sd->npc_id = st->oid;
  3863. sd->npc_item_flag = st->npc_item_flag; // load default.
  3864. sd->state.disable_atcommand_on_npc = battle_config.atcommand_disable_npc && (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3865. #ifdef SECURE_NPCTIMEOUT
  3866. if( sd->npc_idle_timer == INVALID_TIMER && !sd->state.ignoretimeout )
  3867. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_secure_timeout_timer,sd->bl.id,0);
  3868. sd->npc_idle_tick = gettick();
  3869. #endif
  3870. }
  3871. }
  3872. /*==========================================
  3873. * The main part of the script execution
  3874. *------------------------------------------*/
  3875. void run_script_main(struct script_state *st)
  3876. {
  3877. int cmdcount = script_config.check_cmdcount;
  3878. int gotocount = script_config.check_gotocount;
  3879. TBL_PC *sd;
  3880. struct script_stack *stack = st->stack;
  3881. script_attach_state(st);
  3882. if(st->state == RERUNLINE) {
  3883. run_func(st);
  3884. if(st->state == GOTO)
  3885. st->state = RUN;
  3886. } else if(st->state != END)
  3887. st->state = RUN;
  3888. while(st->state == RUN) {
  3889. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3890. switch(c){
  3891. case C_EOL:
  3892. if( stack->defsp > stack->sp )
  3893. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3894. else
  3895. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3896. break;
  3897. case C_INT:
  3898. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3899. break;
  3900. case C_POS:
  3901. case C_NAME:
  3902. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3903. st->pos+=3;
  3904. break;
  3905. case C_ARG:
  3906. push_val(stack,c,0);
  3907. break;
  3908. case C_STR:
  3909. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3910. while(st->script->script_buf[st->pos++]);
  3911. break;
  3912. case C_FUNC:
  3913. run_func(st);
  3914. if(st->state==GOTO){
  3915. st->state = RUN;
  3916. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3917. ShowError("script:run_script_main: infinity loop !\n");
  3918. script_reportsrc(st);
  3919. st->state=END;
  3920. }
  3921. }
  3922. break;
  3923. case C_REF:
  3924. st->op2ref = 1;
  3925. break;
  3926. case C_NEG:
  3927. case C_NOT:
  3928. case C_LNOT:
  3929. op_1(st ,c);
  3930. break;
  3931. case C_ADD:
  3932. case C_SUB:
  3933. case C_MUL:
  3934. case C_DIV:
  3935. case C_MOD:
  3936. case C_EQ:
  3937. case C_NE:
  3938. case C_GT:
  3939. case C_GE:
  3940. case C_LT:
  3941. case C_LE:
  3942. case C_AND:
  3943. case C_OR:
  3944. case C_XOR:
  3945. case C_LAND:
  3946. case C_LOR:
  3947. case C_R_SHIFT:
  3948. case C_L_SHIFT:
  3949. op_2(st, c);
  3950. break;
  3951. case C_OP3:
  3952. op_3(st, c);
  3953. break;
  3954. case C_NOP:
  3955. st->state=END;
  3956. break;
  3957. default:
  3958. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3959. st->state=END;
  3960. break;
  3961. }
  3962. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3963. ShowError("script:run_script_main: infinity loop !\n");
  3964. script_reportsrc(st);
  3965. st->state=END;
  3966. }
  3967. }
  3968. if(st->sleep.tick > 0) {
  3969. //Restore previous script
  3970. script_detach_state(st, false);
  3971. //Delay execution
  3972. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3973. st->sleep.charid = sd?sd->status.char_id:0;
  3974. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3975. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3976. } else if(st->state != END && st->rid) {
  3977. //Resume later (st is already attached to player).
  3978. if(st->bk_st) {
  3979. ShowWarning("Unable to restore stack! Double continuation!\n");
  3980. //Report BOTH scripts to see if that can help somehow.
  3981. ShowDebug("Previous script (lost):\n");
  3982. script_reportsrc(st->bk_st);
  3983. ShowDebug("Current script:\n");
  3984. script_reportsrc(st);
  3985. script_free_state(st->bk_st);
  3986. st->bk_st = NULL;
  3987. }
  3988. } else {
  3989. if (st->stack && st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp - 1].type == C_RETINFO) {
  3990. for (int i = 0; i < st->stack->sp; i++) {
  3991. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  3992. st->script = st->stack->stack_data[i].u.ri->script;
  3993. break;
  3994. }
  3995. }
  3996. }
  3997. //Dispose of script.
  3998. if ((sd = map_id2sd(st->rid))!=NULL)
  3999. { //Restore previous stack and save char.
  4000. if(sd->state.using_fake_npc){
  4001. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  4002. sd->state.using_fake_npc = 0;
  4003. }
  4004. //Restore previous script if any.
  4005. script_detach_state(st, true);
  4006. if (sd->vars_dirty)
  4007. intif_saveregistry(sd);
  4008. }
  4009. script_free_state(st);
  4010. }
  4011. }
  4012. int script_config_read(const char *cfgName)
  4013. {
  4014. int i;
  4015. char line[1024],w1[1024],w2[1024];
  4016. FILE *fp;
  4017. fp=fopen(cfgName,"r");
  4018. if(fp==NULL){
  4019. ShowError("File not found: %s\n", cfgName);
  4020. return 1;
  4021. }
  4022. while(fgets(line, sizeof(line), fp))
  4023. {
  4024. if(line[0] == '/' && line[1] == '/')
  4025. continue;
  4026. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  4027. if(i!=2)
  4028. continue;
  4029. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  4030. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  4031. }
  4032. else if(strcmpi(w1,"check_cmdcount")==0) {
  4033. script_config.check_cmdcount = config_switch(w2);
  4034. }
  4035. else if(strcmpi(w1,"check_gotocount")==0) {
  4036. script_config.check_gotocount = config_switch(w2);
  4037. }
  4038. else if(strcmpi(w1,"input_min_value")==0) {
  4039. script_config.input_min_value = config_switch(w2);
  4040. }
  4041. else if(strcmpi(w1,"input_max_value")==0) {
  4042. script_config.input_max_value = config_switch(w2);
  4043. }
  4044. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  4045. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  4046. }
  4047. else if(strcmpi(w1,"import")==0){
  4048. script_config_read(w2);
  4049. }
  4050. else {
  4051. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  4052. }
  4053. }
  4054. fclose(fp);
  4055. return 0;
  4056. }
  4057. /**
  4058. * @see DBApply
  4059. */
  4060. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  4061. {
  4062. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  4063. if (code)
  4064. script_free_code(code);
  4065. return 0;
  4066. }
  4067. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  4068. {
  4069. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4070. if( script )
  4071. {
  4072. int j;
  4073. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4074. if( j < EQI_MAX ) {
  4075. //Single item autobonus
  4076. current_equip_item_index = sd->equip_index[j];
  4077. current_equip_combo_pos = 0;
  4078. } else {
  4079. //Combo autobonus
  4080. current_equip_item_index = -1;
  4081. current_equip_combo_pos = pos;
  4082. }
  4083. run_script(script,0,sd->bl.id,0);
  4084. }
  4085. }
  4086. void script_add_autobonus(const char *autobonus)
  4087. {
  4088. if( strdb_get(autobonus_db, autobonus) == NULL )
  4089. {
  4090. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4091. if( script )
  4092. strdb_put(autobonus_db, autobonus, script);
  4093. }
  4094. }
  4095. /// resets a temporary character array variable to given value
  4096. void script_cleararray_pc( struct map_session_data* sd, const char* varname ){
  4097. struct script_array *sa = NULL;
  4098. struct reg_db *src = NULL;
  4099. unsigned int i, *list = NULL, size = 0;
  4100. int key;
  4101. key = add_str(varname);
  4102. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4103. return;
  4104. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4105. return;
  4106. size = sa->size;
  4107. list = script_array_cpy_list(sa);
  4108. for( i = 0; i < size; i++ ){
  4109. clear_reg( NULL, sd, reference_uid( key, list[i] ), varname, NULL );
  4110. }
  4111. }
  4112. /// sets a temporary character array variable element idx to given value
  4113. /// @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.
  4114. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, int64 value, int* refcache)
  4115. {
  4116. int key;
  4117. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4118. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4119. return;
  4120. }
  4121. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4122. set_reg_num( NULL, sd, reference_uid( key, idx ), varname, value, NULL );
  4123. if( refcache ) {
  4124. // save to avoid repeated script->add_str calls
  4125. refcache[0] = key;
  4126. }
  4127. }
  4128. /**
  4129. * Clears persistent variables from memory
  4130. **/
  4131. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4132. {
  4133. struct script_reg_state *src;
  4134. if( data->type != DB_DATA_PTR ) // got no need for those!
  4135. return 0;
  4136. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4137. if( src->type ) {
  4138. struct script_reg_str *p = (struct script_reg_str *)src;
  4139. if( p->value )
  4140. aFree(p->value);
  4141. ers_free(str_reg_ers,p);
  4142. } else {
  4143. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4144. }
  4145. return 0;
  4146. }
  4147. /**
  4148. * Clears a single persistent variable
  4149. **/
  4150. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4151. {
  4152. i64db_remove(sd->regs.vars, reg);
  4153. if( data->type ) {
  4154. struct script_reg_str *p = (struct script_reg_str*)data;
  4155. if( p->value )
  4156. aFree(p->value);
  4157. ers_free(str_reg_ers,p);
  4158. } else {
  4159. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4160. }
  4161. }
  4162. unsigned int *script_array_cpy_list(struct script_array *sa)
  4163. {
  4164. if( sa->size > generic_ui_array_size )
  4165. script_generic_ui_array_expand(sa->size);
  4166. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4167. return generic_ui_array;
  4168. }
  4169. void script_generic_ui_array_expand (unsigned int plus)
  4170. {
  4171. generic_ui_array_size += plus + 100;
  4172. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4173. }
  4174. /*==========================================
  4175. * Destructor
  4176. *------------------------------------------*/
  4177. void do_final_script() {
  4178. int i;
  4179. DBIterator *iter;
  4180. struct script_state *st;
  4181. #ifdef DEBUG_HASH
  4182. if (battle_config.etc_log)
  4183. {
  4184. FILE *fp = fopen("hash_dump.txt","wt");
  4185. if(fp) {
  4186. int count[SCRIPT_HASH_SIZE];
  4187. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4188. int n=0;
  4189. int min=INT_MAX,max=0,zero=0;
  4190. double mean=0.0f;
  4191. double median=0.0f;
  4192. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4193. memset(count, 0, sizeof(count));
  4194. fprintf(fp,"num : hash : data_name\n");
  4195. fprintf(fp,"---------------------------------------------------------------\n");
  4196. for(i=LABEL_START; i<str_num; i++) {
  4197. unsigned int h = calc_hash(get_str(i));
  4198. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4199. ++count[h];
  4200. }
  4201. fprintf(fp,"--------------------\n\n");
  4202. memset(count2, 0, sizeof(count2));
  4203. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4204. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4205. if(min > count[i])
  4206. min = count[i]; // minimun count of collision
  4207. if(max < count[i])
  4208. max = count[i]; // maximun count of collision
  4209. if(count[i] == 0)
  4210. zero++;
  4211. ++count2[count[i]];
  4212. }
  4213. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4214. for( i=min; i <= max; ++i ){
  4215. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4216. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4217. }
  4218. for( i=min; i <= max; ++i ){
  4219. n += count2[i];
  4220. if( n*2 >= SCRIPT_HASH_SIZE )
  4221. {
  4222. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4223. median = (i+i+1)/2.0f;
  4224. else
  4225. median = i;
  4226. break;
  4227. }
  4228. }
  4229. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4230. fclose(fp);
  4231. }
  4232. }
  4233. #endif
  4234. mapreg_final();
  4235. db_destroy(scriptlabel_db);
  4236. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4237. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4238. ers_destroy(array_ers);
  4239. if (generic_ui_array)
  4240. aFree(generic_ui_array);
  4241. iter = db_iterator(st_db);
  4242. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4243. script_free_state(st);
  4244. dbi_destroy(iter);
  4245. if (str_data)
  4246. aFree(str_data);
  4247. if (str_buf)
  4248. aFree(str_buf);
  4249. for( i = 0; i < atcmd_binding_count; i++ ) {
  4250. aFree(atcmd_binding[i]);
  4251. }
  4252. if( atcmd_binding_count != 0 )
  4253. aFree(atcmd_binding);
  4254. ers_destroy(st_ers);
  4255. ers_destroy(stack_ers);
  4256. db_destroy(st_db);
  4257. }
  4258. /*==========================================
  4259. * Initialization
  4260. *------------------------------------------*/
  4261. void do_init_script(void) {
  4262. st_db = idb_alloc(DB_OPT_BASE);
  4263. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4264. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4265. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4266. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4267. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4268. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4269. ers_chunk_size(st_ers, 10);
  4270. ers_chunk_size(stack_ers, 10);
  4271. active_scripts = 0;
  4272. next_id = 0;
  4273. mapreg_init();
  4274. add_buildin_func();
  4275. read_constdb();
  4276. script_hardcoded_constants();
  4277. }
  4278. void script_reload(void) {
  4279. int i;
  4280. DBIterator *iter;
  4281. struct script_state *st;
  4282. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4283. db_clear(scriptlabel_db);
  4284. // @commands (script based)
  4285. // Clear bindings
  4286. for( i = 0; i < atcmd_binding_count; i++ ) {
  4287. aFree(atcmd_binding[i]);
  4288. }
  4289. if( atcmd_binding_count != 0 )
  4290. aFree(atcmd_binding);
  4291. atcmd_binding_count = 0;
  4292. iter = db_iterator(st_db);
  4293. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4294. script_free_state(st);
  4295. dbi_destroy(iter);
  4296. db_clear(st_db);
  4297. mapreg_reload();
  4298. }
  4299. //-----------------------------------------------------------------------------
  4300. // buildin functions
  4301. //
  4302. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4303. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4304. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4305. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4306. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4307. /////////////////////////////////////////////////////////////////////
  4308. // NPC interaction
  4309. //
  4310. /// Appends a message to the npc dialog.
  4311. /// If a dialog doesn't exist yet, one is created.
  4312. ///
  4313. /// mes "<message>";
  4314. BUILDIN_FUNC(mes)
  4315. {
  4316. TBL_PC* sd;
  4317. if( !script_rid2sd(sd) )
  4318. return SCRIPT_CMD_SUCCESS;
  4319. if( !script_hasdata(st, 3) )
  4320. {// only a single line detected in the script
  4321. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4322. }
  4323. else
  4324. {// parse multiple lines as they exist
  4325. int i;
  4326. for( i = 2; script_hasdata(st, i); i++ )
  4327. {
  4328. // send the message to the client
  4329. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4330. }
  4331. }
  4332. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4333. return SCRIPT_CMD_SUCCESS;
  4334. }
  4335. /// Displays the button 'next' in the npc dialog.
  4336. /// The dialog text is cleared and the script continues when the button is pressed.
  4337. ///
  4338. /// next;
  4339. BUILDIN_FUNC(next)
  4340. {
  4341. TBL_PC* sd;
  4342. if (!st->mes_active) {
  4343. ShowWarning("buildin_next: There is no mes active.\n");
  4344. return SCRIPT_CMD_FAILURE;
  4345. }
  4346. if( !script_rid2sd(sd) )
  4347. return SCRIPT_CMD_SUCCESS;
  4348. #ifdef SECURE_NPCTIMEOUT
  4349. sd->npc_idle_type = NPCT_WAIT;
  4350. #endif
  4351. st->state = STOP;
  4352. clif_scriptnext(sd, st->oid);
  4353. return SCRIPT_CMD_SUCCESS;
  4354. }
  4355. /// Clears the dialog and continues the script without a next button.
  4356. ///
  4357. /// clear;
  4358. BUILDIN_FUNC(clear)
  4359. {
  4360. TBL_PC* sd;
  4361. if (!st->mes_active) {
  4362. ShowWarning("buildin_clear: There is no mes active.\n");
  4363. return SCRIPT_CMD_FAILURE;
  4364. }
  4365. if (!script_rid2sd(sd))
  4366. return SCRIPT_CMD_FAILURE;
  4367. clif_scriptclear(sd, st->oid);
  4368. return SCRIPT_CMD_SUCCESS;
  4369. }
  4370. /// Ends the script and displays the button 'close' on the npc dialog.
  4371. /// The dialog is closed when the button is pressed.
  4372. ///
  4373. /// close;
  4374. BUILDIN_FUNC(close)
  4375. {
  4376. TBL_PC* sd;
  4377. if( !script_rid2sd(sd) )
  4378. return SCRIPT_CMD_SUCCESS;
  4379. if( !st->mes_active ) {
  4380. st->state = END; // Keep backwards compatibility.
  4381. ShowWarning("Incorrect use of 'close' command!\n");
  4382. script_reportsrc(st);
  4383. } else {
  4384. st->state = CLOSE;
  4385. st->mes_active = 0;
  4386. }
  4387. clif_scriptclose(sd, st->oid);
  4388. return SCRIPT_CMD_SUCCESS;
  4389. }
  4390. /// Displays the button 'close' on the npc dialog.
  4391. /// The dialog is closed and the script continues when the button is pressed.
  4392. ///
  4393. /// close2;
  4394. BUILDIN_FUNC(close2)
  4395. {
  4396. TBL_PC* sd;
  4397. if( !script_rid2sd(sd) )
  4398. return SCRIPT_CMD_SUCCESS;
  4399. st->state = STOP;
  4400. if( st->mes_active )
  4401. st->mes_active = 0;
  4402. clif_scriptclose(sd, st->oid);
  4403. return SCRIPT_CMD_SUCCESS;
  4404. }
  4405. /// Counts the number of valid and total number of options in 'str'
  4406. /// If max_count > 0 the counting stops when that valid option is reached
  4407. /// total is incremented for each option (NULL is supported)
  4408. static int menu_countoptions(const char* str, int max_count, int* total)
  4409. {
  4410. int count = 0;
  4411. int bogus_total;
  4412. if( total == NULL )
  4413. total = &bogus_total;
  4414. ++(*total);
  4415. // initial empty options
  4416. while( *str == ':' )
  4417. {
  4418. ++str;
  4419. ++(*total);
  4420. }
  4421. // count menu options
  4422. while( *str != '\0' )
  4423. {
  4424. ++count;
  4425. --max_count;
  4426. if( max_count == 0 )
  4427. break;
  4428. while( *str != ':' && *str != '\0' )
  4429. ++str;
  4430. while( *str == ':' )
  4431. {
  4432. ++str;
  4433. ++(*total);
  4434. }
  4435. }
  4436. return count;
  4437. }
  4438. /// Displays a menu with options and goes to the target label.
  4439. /// The script is stopped if cancel is pressed.
  4440. /// Options with no text are not displayed in the client.
  4441. ///
  4442. /// Options can be grouped together, separated by the character ':' in the text:
  4443. /// ex: menu "A:B:C",L_target;
  4444. /// All these options go to the specified target label.
  4445. ///
  4446. /// The index of the selected option is put in the variable @menu.
  4447. /// Indexes start with 1 and are consistent with grouped and empty options.
  4448. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4449. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4450. ///
  4451. /// NOTE: the client closes the npc dialog when cancel is pressed
  4452. ///
  4453. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4454. BUILDIN_FUNC(menu)
  4455. {
  4456. int i;
  4457. const char* text;
  4458. TBL_PC* sd;
  4459. if( !script_rid2sd(sd) )
  4460. return SCRIPT_CMD_SUCCESS;
  4461. #ifdef SECURE_NPCTIMEOUT
  4462. sd->npc_idle_type = NPCT_MENU;
  4463. #endif
  4464. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4465. if( sd->state.menu_or_input == 0 )
  4466. {
  4467. struct StringBuf buf;
  4468. if( script_lastdata(st) % 2 == 0 )
  4469. {// argument count is not even (1st argument is at index 2)
  4470. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4471. st->state = END;
  4472. return SCRIPT_CMD_FAILURE;
  4473. }
  4474. StringBuf_Init(&buf);
  4475. sd->npc_menu = 0;
  4476. for( i = 2; i < script_lastdata(st); i += 2 )
  4477. {
  4478. struct script_data* data;
  4479. // menu options
  4480. text = script_getstr(st, i);
  4481. // target label
  4482. data = script_getdata(st, i+1);
  4483. if( !data_islabel(data) )
  4484. {// not a label
  4485. StringBuf_Destroy(&buf);
  4486. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4487. script_reportdata(data);
  4488. st->state = END;
  4489. return SCRIPT_CMD_FAILURE;
  4490. }
  4491. // append option(s)
  4492. if( text[0] == '\0' )
  4493. continue;// empty string, ignore
  4494. if( sd->npc_menu > 0 )
  4495. StringBuf_AppendStr(&buf, ":");
  4496. StringBuf_AppendStr(&buf, text);
  4497. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4498. }
  4499. st->state = RERUNLINE;
  4500. sd->state.menu_or_input = 1;
  4501. /**
  4502. * menus beyond this length crash the client (see bugreport:6402)
  4503. **/
  4504. if( StringBuf_Length(&buf) >= 2047 ) {
  4505. struct npc_data * nd = map_id2nd(st->oid);
  4506. char* menu;
  4507. CREATE(menu, char, 2048);
  4508. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4509. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4510. clif_scriptmenu(sd, st->oid, menu);
  4511. aFree(menu);
  4512. } else
  4513. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4514. StringBuf_Destroy(&buf);
  4515. if( sd->npc_menu >= 0xff )
  4516. {// 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
  4517. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4518. script_reportsrc(st);
  4519. }
  4520. }
  4521. else if( sd->npc_menu == 0xff )
  4522. {// Cancel was pressed
  4523. sd->state.menu_or_input = 0;
  4524. st->state = END;
  4525. }
  4526. else
  4527. {// goto target label
  4528. int menu = 0;
  4529. sd->state.menu_or_input = 0;
  4530. if( sd->npc_menu <= 0 )
  4531. {
  4532. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4533. st->state = END;
  4534. return SCRIPT_CMD_FAILURE;
  4535. }
  4536. // get target label
  4537. for( i = 2; i < script_lastdata(st); i += 2 )
  4538. {
  4539. text = script_getstr(st, i);
  4540. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4541. if( sd->npc_menu <= 0 )
  4542. break;// entry found
  4543. }
  4544. if( sd->npc_menu > 0 )
  4545. {// Invalid selection
  4546. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4547. st->state = END;
  4548. return SCRIPT_CMD_FAILURE;
  4549. }
  4550. if( !data_islabel(script_getdata(st, i + 1)) )
  4551. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4552. ShowError("buildin_menu: Unexpected data in label argument\n");
  4553. script_reportdata(script_getdata(st, i + 1));
  4554. st->state = END;
  4555. return SCRIPT_CMD_FAILURE;
  4556. }
  4557. pc_setreg(sd, add_str("@menu"), menu);
  4558. st->pos = script_getnum(st, i + 1);
  4559. st->state = GOTO;
  4560. }
  4561. return SCRIPT_CMD_SUCCESS;
  4562. }
  4563. /// Displays a menu with options and returns the selected option.
  4564. /// Behaves like 'menu' without the target labels.
  4565. ///
  4566. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4567. ///
  4568. /// @see menu
  4569. BUILDIN_FUNC(select)
  4570. {
  4571. int i;
  4572. const char* text;
  4573. TBL_PC* sd;
  4574. if( !script_rid2sd(sd) )
  4575. return SCRIPT_CMD_SUCCESS;
  4576. #ifdef SECURE_NPCTIMEOUT
  4577. sd->npc_idle_type = NPCT_MENU;
  4578. #endif
  4579. if( sd->state.menu_or_input == 0 ) {
  4580. struct StringBuf buf;
  4581. StringBuf_Init(&buf);
  4582. sd->npc_menu = 0;
  4583. for( i = 2; i <= script_lastdata(st); ++i ) {
  4584. text = script_getstr(st, i);
  4585. if( sd->npc_menu > 0 )
  4586. StringBuf_AppendStr(&buf, ":");
  4587. StringBuf_AppendStr(&buf, text);
  4588. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4589. }
  4590. st->state = RERUNLINE;
  4591. sd->state.menu_or_input = 1;
  4592. /**
  4593. * menus beyond this length crash the client (see bugreport:6402)
  4594. **/
  4595. if( StringBuf_Length(&buf) >= 2047 ) {
  4596. struct npc_data * nd = map_id2nd(st->oid);
  4597. char* menu;
  4598. CREATE(menu, char, 2048);
  4599. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4600. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4601. clif_scriptmenu(sd, st->oid, menu);
  4602. aFree(menu);
  4603. } else
  4604. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4605. StringBuf_Destroy(&buf);
  4606. if( sd->npc_menu >= 0xff ) {
  4607. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4608. script_reportsrc(st);
  4609. }
  4610. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4611. sd->state.menu_or_input = 0;
  4612. st->state = END;
  4613. } else {// return selected option
  4614. int menu = 0;
  4615. sd->state.menu_or_input = 0;
  4616. for( i = 2; i <= script_lastdata(st); ++i ) {
  4617. text = script_getstr(st, i);
  4618. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4619. if( sd->npc_menu <= 0 )
  4620. break;// entry found
  4621. }
  4622. pc_setreg(sd, add_str("@menu"), menu);
  4623. script_pushint(st, menu);
  4624. st->state = RUN;
  4625. }
  4626. return SCRIPT_CMD_SUCCESS;
  4627. }
  4628. /// Displays a menu with options and returns the selected option.
  4629. /// Behaves like 'menu' without the target labels, except when cancel is
  4630. /// pressed.
  4631. /// When cancel is pressed, the script continues and 255 is returned.
  4632. ///
  4633. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4634. ///
  4635. /// @see menu
  4636. BUILDIN_FUNC(prompt)
  4637. {
  4638. int i;
  4639. const char *text;
  4640. TBL_PC* sd;
  4641. if( !script_rid2sd(sd) )
  4642. return SCRIPT_CMD_SUCCESS;
  4643. #ifdef SECURE_NPCTIMEOUT
  4644. sd->npc_idle_type = NPCT_MENU;
  4645. #endif
  4646. if( sd->state.menu_or_input == 0 )
  4647. {
  4648. struct StringBuf buf;
  4649. StringBuf_Init(&buf);
  4650. sd->npc_menu = 0;
  4651. for( i = 2; i <= script_lastdata(st); ++i )
  4652. {
  4653. text = script_getstr(st, i);
  4654. if( sd->npc_menu > 0 )
  4655. StringBuf_AppendStr(&buf, ":");
  4656. StringBuf_AppendStr(&buf, text);
  4657. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4658. }
  4659. st->state = RERUNLINE;
  4660. sd->state.menu_or_input = 1;
  4661. /**
  4662. * menus beyond this length crash the client (see bugreport:6402)
  4663. **/
  4664. if( StringBuf_Length(&buf) >= 2047 ) {
  4665. struct npc_data * nd = map_id2nd(st->oid);
  4666. char* menu;
  4667. CREATE(menu, char, 2048);
  4668. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4669. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4670. clif_scriptmenu(sd, st->oid, menu);
  4671. aFree(menu);
  4672. } else
  4673. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4674. StringBuf_Destroy(&buf);
  4675. if( sd->npc_menu >= 0xff )
  4676. {
  4677. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4678. script_reportsrc(st);
  4679. }
  4680. }
  4681. else if( sd->npc_menu == 0xff )
  4682. {// Cancel was pressed
  4683. sd->state.menu_or_input = 0;
  4684. pc_setreg(sd, add_str("@menu"), 0xff);
  4685. script_pushint(st, 0xff);
  4686. st->state = RUN;
  4687. }
  4688. else
  4689. {// return selected option
  4690. int menu = 0;
  4691. sd->state.menu_or_input = 0;
  4692. for( i = 2; i <= script_lastdata(st); ++i )
  4693. {
  4694. text = script_getstr(st, i);
  4695. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4696. if( sd->npc_menu <= 0 )
  4697. break;// entry found
  4698. }
  4699. pc_setreg(sd, add_str("@menu"), menu);
  4700. script_pushint(st, menu);
  4701. st->state = RUN;
  4702. }
  4703. return SCRIPT_CMD_SUCCESS;
  4704. }
  4705. /////////////////////////////////////////////////////////////////////
  4706. // ...
  4707. //
  4708. /// Jumps to the target script label.
  4709. ///
  4710. /// goto <label>;
  4711. BUILDIN_FUNC(goto)
  4712. {
  4713. if( !data_islabel(script_getdata(st,2)) )
  4714. {
  4715. ShowError("buildin_goto: Not a label\n");
  4716. script_reportdata(script_getdata(st,2));
  4717. st->state = END;
  4718. return SCRIPT_CMD_FAILURE;
  4719. }
  4720. st->pos = script_getnum(st,2);
  4721. st->state = GOTO;
  4722. return SCRIPT_CMD_SUCCESS;
  4723. }
  4724. /*==========================================
  4725. * user-defined function call
  4726. *------------------------------------------*/
  4727. BUILDIN_FUNC(callfunc)
  4728. {
  4729. int i, j;
  4730. struct script_retinfo* ri;
  4731. struct script_code* scr;
  4732. const char* str = script_getstr(st,2);
  4733. struct reg_db *ref = NULL;
  4734. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4735. if(!scr) {
  4736. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4737. st->state = END;
  4738. return SCRIPT_CMD_FAILURE;
  4739. }
  4740. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4741. ref[0].vars = st->stack->scope.vars;
  4742. if (!st->stack->scope.arrays)
  4743. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4744. ref[0].arrays = st->stack->scope.arrays;
  4745. ref[1].vars = st->script->local.vars;
  4746. if (!st->script->local.arrays)
  4747. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4748. ref[1].arrays = st->script->local.arrays;
  4749. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4750. struct script_data* data = push_copy(st->stack,i);
  4751. if (data_isreference(data) && !data->ref) {
  4752. const char* name = reference_getname(data);
  4753. if (name[0] == '.')
  4754. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4755. }
  4756. }
  4757. CREATE(ri, struct script_retinfo, 1);
  4758. ri->script = st->script; // script code
  4759. ri->scope.vars = st->stack->scope.vars; // scope variables
  4760. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4761. ri->pos = st->pos; // script location
  4762. ri->nargs = j; // argument count
  4763. ri->defsp = st->stack->defsp; // default stack pointer
  4764. push_retinfo(st->stack, ri, ref);
  4765. st->pos = 0;
  4766. st->script = scr;
  4767. st->stack->defsp = st->stack->sp;
  4768. st->state = GOTO;
  4769. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4770. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4771. if (!st->script->local.vars)
  4772. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4773. return SCRIPT_CMD_SUCCESS;
  4774. }
  4775. /*==========================================
  4776. * subroutine call
  4777. *------------------------------------------*/
  4778. BUILDIN_FUNC(callsub)
  4779. {
  4780. int i,j;
  4781. struct script_retinfo* ri;
  4782. int pos = script_getnum(st,2);
  4783. struct reg_db *ref = NULL;
  4784. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4785. ShowError("buildin_callsub: Argument is not a label\n");
  4786. script_reportdata(script_getdata(st,2));
  4787. st->state = END;
  4788. return SCRIPT_CMD_FAILURE;
  4789. }
  4790. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4791. ref[0].vars = st->stack->scope.vars;
  4792. if (!st->stack->scope.arrays)
  4793. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4794. ref[0].arrays = st->stack->scope.arrays;
  4795. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4796. struct script_data* data = push_copy(st->stack,i);
  4797. if (data_isreference(data) && !data->ref) {
  4798. const char* name = reference_getname(data);
  4799. if (name[0] == '.' && name[1] == '@')
  4800. data->ref = &ref[0];
  4801. }
  4802. }
  4803. CREATE(ri, struct script_retinfo, 1);
  4804. ri->script = st->script; // script code
  4805. ri->scope.vars = st->stack->scope.vars; // scope variables
  4806. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4807. ri->pos = st->pos; // script location
  4808. ri->nargs = j; // argument count
  4809. ri->defsp = st->stack->defsp; // default stack pointer
  4810. push_retinfo(st->stack, ri, ref);
  4811. st->pos = pos;
  4812. st->stack->defsp = st->stack->sp;
  4813. st->state = GOTO;
  4814. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4815. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4816. return SCRIPT_CMD_SUCCESS;
  4817. }
  4818. /// Retrieves an argument provided to callfunc/callsub.
  4819. /// If the argument doesn't exist
  4820. ///
  4821. /// getarg(<index>{,<default_value>}) -> <value>
  4822. BUILDIN_FUNC(getarg)
  4823. {
  4824. struct script_retinfo* ri;
  4825. int idx;
  4826. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4827. {
  4828. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4829. st->state = END;
  4830. return SCRIPT_CMD_FAILURE;
  4831. }
  4832. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4833. idx = script_getnum(st,2);
  4834. if( idx >= 0 && idx < ri->nargs )
  4835. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4836. else if( script_hasdata(st,3) )
  4837. script_pushcopy(st, 3);
  4838. else
  4839. {
  4840. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4841. st->state = END;
  4842. return SCRIPT_CMD_FAILURE;
  4843. }
  4844. return SCRIPT_CMD_SUCCESS;
  4845. }
  4846. /// Returns from the current function, optionaly returning a value from the functions.
  4847. /// Don't use outside script functions.
  4848. ///
  4849. /// return;
  4850. /// return <value>;
  4851. BUILDIN_FUNC(return)
  4852. {
  4853. if( script_hasdata(st,2) )
  4854. {// return value
  4855. struct script_data* data;
  4856. script_pushcopy(st, 2);
  4857. data = script_getdatatop(st, -1);
  4858. if( data_isreference(data) ) {
  4859. const char* name = reference_getname(data);
  4860. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4861. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4862. get_val(st, data); // current scope, convert to value
  4863. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4864. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4865. }
  4866. }
  4867. }
  4868. else
  4869. {// no return value
  4870. script_pushnil(st);
  4871. }
  4872. st->state = RETFUNC;
  4873. return SCRIPT_CMD_SUCCESS;
  4874. }
  4875. /// Returns a random number from 0 to <range>-1.
  4876. /// Or returns a random number from <min> to <max>.
  4877. /// If <min> is greater than <max>, their numbers are switched.
  4878. /// rand(<range>) -> <int>
  4879. /// rand(<min>,<max>) -> <int>
  4880. BUILDIN_FUNC(rand)
  4881. {
  4882. int range;
  4883. int min;
  4884. if( script_hasdata(st,3) )
  4885. {// min,max
  4886. int max = script_getnum(st,3);
  4887. min = script_getnum(st,2);
  4888. if( max < min )
  4889. SWAP(min, max);
  4890. range = max - min + 1;
  4891. }
  4892. else
  4893. {// range
  4894. min = 0;
  4895. range = script_getnum(st,2);
  4896. }
  4897. if( range <= 1 )
  4898. script_pushint(st, min);
  4899. else
  4900. script_pushint(st, rnd()%range + min);
  4901. return SCRIPT_CMD_SUCCESS;
  4902. }
  4903. /*==========================================
  4904. * Warp sd to str,x,y or Random or SavePoint/Save
  4905. *------------------------------------------*/
  4906. BUILDIN_FUNC(warp)
  4907. {
  4908. int ret;
  4909. int x,y;
  4910. const char* str;
  4911. struct map_session_data* sd;
  4912. if(!script_charid2sd(5, sd))
  4913. return SCRIPT_CMD_SUCCESS;
  4914. str = script_getstr(st,2);
  4915. x = script_getnum(st,3);
  4916. y = script_getnum(st,4);
  4917. if(strcmp(str,"Random")==0)
  4918. ret = pc_randomwarp(sd,CLR_TELEPORT,true);
  4919. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4920. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4921. else
  4922. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4923. if( ret ) {
  4924. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4925. return SCRIPT_CMD_FAILURE;
  4926. }
  4927. return SCRIPT_CMD_SUCCESS;
  4928. }
  4929. /*==========================================
  4930. * Warp a specified area
  4931. *------------------------------------------*/
  4932. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4933. {
  4934. int x2,y2,x3,y3;
  4935. unsigned int index;
  4936. index = va_arg(ap,unsigned int);
  4937. x2 = va_arg(ap,int);
  4938. y2 = va_arg(ap,int);
  4939. x3 = va_arg(ap,int);
  4940. y3 = va_arg(ap,int);
  4941. if(index == 0)
  4942. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT,true);
  4943. else if(x3 && y3) {
  4944. int max, tx, ty, j = 0;
  4945. int16 m;
  4946. m = map_mapindex2mapid(index);
  4947. // choose a suitable max number of attempts
  4948. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4949. max = 1000;
  4950. // find a suitable map cell
  4951. do {
  4952. tx = rnd()%(x3-x2+1)+x2;
  4953. ty = rnd()%(y3-y2+1)+y2;
  4954. j++;
  4955. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4956. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4957. }
  4958. else
  4959. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4960. return 0;
  4961. }
  4962. BUILDIN_FUNC(areawarp)
  4963. {
  4964. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4965. unsigned int index;
  4966. const char *str;
  4967. const char *mapname;
  4968. mapname = script_getstr(st,2);
  4969. x0 = script_getnum(st,3);
  4970. y0 = script_getnum(st,4);
  4971. x1 = script_getnum(st,5);
  4972. y1 = script_getnum(st,6);
  4973. str = script_getstr(st,7);
  4974. x2 = script_getnum(st,8);
  4975. y2 = script_getnum(st,9);
  4976. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4977. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4978. x3 = 0;
  4979. y3 = 0;
  4980. } else if( x3 && y3 ) {
  4981. // normalize x3/y3 coordinates
  4982. if( x3 < x2 ) SWAP(x3,x2);
  4983. if( y3 < y2 ) SWAP(y3,y2);
  4984. }
  4985. }
  4986. if( (m = map_mapname2mapid(mapname)) < 0 )
  4987. return SCRIPT_CMD_FAILURE;
  4988. if( strcmp(str,"Random") == 0 )
  4989. index = 0;
  4990. else if( !(index=mapindex_name2id(str)) )
  4991. return SCRIPT_CMD_FAILURE;
  4992. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4993. return SCRIPT_CMD_SUCCESS;
  4994. }
  4995. /*==========================================
  4996. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4997. *------------------------------------------*/
  4998. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4999. {
  5000. int hp, sp;
  5001. hp = va_arg(ap, int);
  5002. sp = va_arg(ap, int);
  5003. pc_percentheal((TBL_PC *)bl,hp,sp);
  5004. return 0;
  5005. }
  5006. BUILDIN_FUNC(areapercentheal)
  5007. {
  5008. int hp,sp,m;
  5009. const char *mapname;
  5010. int x0,y0,x1,y1;
  5011. mapname=script_getstr(st,2);
  5012. x0=script_getnum(st,3);
  5013. y0=script_getnum(st,4);
  5014. x1=script_getnum(st,5);
  5015. y1=script_getnum(st,6);
  5016. hp=script_getnum(st,7);
  5017. sp=script_getnum(st,8);
  5018. if( (m=map_mapname2mapid(mapname))< 0)
  5019. return SCRIPT_CMD_FAILURE;
  5020. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5021. return SCRIPT_CMD_SUCCESS;
  5022. }
  5023. /*==========================================
  5024. * Warpparty - [Fredzilla] [Paradox924X]
  5025. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5026. * If 'from_mapname' is specified, only the party members on that map will be warped
  5027. *------------------------------------------*/
  5028. BUILDIN_FUNC(warpparty)
  5029. {
  5030. TBL_PC *sd = NULL;
  5031. TBL_PC *pl_sd;
  5032. struct party_data* p;
  5033. int type, mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5034. const char* str = script_getstr(st,2);
  5035. int x = script_getnum(st,3);
  5036. int y = script_getnum(st,4);
  5037. int p_id = script_getnum(st,5);
  5038. const char* str2 = NULL;
  5039. if ( script_hasdata(st,6) )
  5040. str2 = script_getstr(st,6);
  5041. if (script_hasdata(st, 7))
  5042. rx = script_getnum(st, 7);
  5043. if (script_hasdata(st, 8))
  5044. ry = script_getnum(st, 8);
  5045. p = party_search(p_id);
  5046. if(!p)
  5047. return SCRIPT_CMD_SUCCESS;
  5048. type = ( strcmp(str,"Random")==0 ) ? 0
  5049. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5050. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5051. : ( strcmp(str,"Leader")==0 ) ? 3
  5052. : 4;
  5053. switch (type)
  5054. {
  5055. case 3:
  5056. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5057. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5058. return SCRIPT_CMD_FAILURE;
  5059. pl_sd = p->data[i].sd;
  5060. mapindex = pl_sd->mapindex;
  5061. m = map_mapindex2mapid(mapindex);
  5062. x = pl_sd->bl.x;
  5063. y = pl_sd->bl.y;
  5064. break;
  5065. case 4:
  5066. mapindex = mapindex_name2id(str);
  5067. if (!mapindex) {// Invalid map
  5068. return SCRIPT_CMD_FAILURE;
  5069. }
  5070. m = map_mapindex2mapid(mapindex);
  5071. break;
  5072. case 2:
  5073. //"SavePoint" uses save point of the currently attached player
  5074. if ( !script_rid2sd(sd) )
  5075. return SCRIPT_CMD_SUCCESS;
  5076. break;
  5077. }
  5078. for (i = 0; i < MAX_PARTY; i++)
  5079. {
  5080. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5081. continue;
  5082. if( str2 && strcmp(str2, map_getmapdata(pl_sd->bl.m)->name) != 0 )
  5083. continue;
  5084. if( pc_isdead(pl_sd) )
  5085. continue;
  5086. switch( type )
  5087. {
  5088. case 0: // Random
  5089. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5090. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5091. break;
  5092. case 1: // SavePointAll
  5093. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5094. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5095. break;
  5096. case 2: // SavePoint
  5097. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5098. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5099. break;
  5100. case 3: // Leader
  5101. if (p->party.member[i].leader)
  5102. continue;
  5103. case 4: // m,x,y
  5104. if (rx || ry) {
  5105. int x1 = x + rx, y1 = y + ry,
  5106. x0 = x - rx, y0 = y - ry;
  5107. uint8 attempts = 10;
  5108. do {
  5109. x = x0 + rnd()%(x1 - x0 + 1);
  5110. y = y0 + rnd()%(y1 - y0 + 1);
  5111. } while ((--attempts) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  5112. }
  5113. 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))
  5114. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5115. break;
  5116. }
  5117. }
  5118. return SCRIPT_CMD_SUCCESS;
  5119. }
  5120. /*==========================================
  5121. * Warpguild - [Fredzilla]
  5122. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5123. *------------------------------------------*/
  5124. BUILDIN_FUNC(warpguild)
  5125. {
  5126. TBL_PC *sd = NULL;
  5127. TBL_PC *pl_sd;
  5128. struct guild* g;
  5129. struct s_mapiterator* iter;
  5130. int type, mapindex = 0, m = -1;
  5131. const char* str = script_getstr(st,2);
  5132. int x = script_getnum(st,3);
  5133. int y = script_getnum(st,4);
  5134. int gid = script_getnum(st,5);
  5135. g = guild_search(gid);
  5136. if( g == NULL )
  5137. return SCRIPT_CMD_SUCCESS;
  5138. type = ( strcmp(str,"Random")==0 ) ? 0
  5139. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5140. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5141. : 3;
  5142. if( type == 2 && !script_rid2sd(sd) )
  5143. {// "SavePoint" uses save point of the currently attached player
  5144. return SCRIPT_CMD_SUCCESS;
  5145. }
  5146. switch (type) {
  5147. case 3:
  5148. mapindex = mapindex_name2id(str);
  5149. if (!mapindex)
  5150. return SCRIPT_CMD_FAILURE;
  5151. m = map_mapindex2mapid(mapindex);
  5152. break;
  5153. }
  5154. iter = mapit_getallusers();
  5155. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5156. {
  5157. if( pl_sd->status.guild_id != gid )
  5158. continue;
  5159. switch( type )
  5160. {
  5161. case 0: // Random
  5162. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5163. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5164. break;
  5165. case 1: // SavePointAll
  5166. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5167. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5168. break;
  5169. case 2: // SavePoint
  5170. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5171. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5172. break;
  5173. case 3: // m,x,y
  5174. 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))
  5175. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5176. break;
  5177. }
  5178. }
  5179. mapit_free(iter);
  5180. return SCRIPT_CMD_SUCCESS;
  5181. }
  5182. /*==========================================
  5183. * Force Heal a player (hp and sp)
  5184. *------------------------------------------*/
  5185. BUILDIN_FUNC(heal)
  5186. {
  5187. TBL_PC *sd;
  5188. int hp,sp;
  5189. if (!script_charid2sd(4,sd))
  5190. return SCRIPT_CMD_SUCCESS;
  5191. hp=script_getnum(st,2);
  5192. sp=script_getnum(st,3);
  5193. status_heal(&sd->bl, hp, sp, 1);
  5194. return SCRIPT_CMD_SUCCESS;
  5195. }
  5196. /*==========================================
  5197. * Heal a player by item (get vit bonus etc)
  5198. *------------------------------------------*/
  5199. BUILDIN_FUNC(itemheal)
  5200. {
  5201. TBL_PC *sd;
  5202. int hp,sp;
  5203. hp=script_getnum(st,2);
  5204. sp=script_getnum(st,3);
  5205. if(potion_flag==1) {
  5206. potion_hp = hp;
  5207. potion_sp = sp;
  5208. return SCRIPT_CMD_SUCCESS;
  5209. }
  5210. if (!script_charid2sd(4,sd))
  5211. return SCRIPT_CMD_SUCCESS;
  5212. pc_itemheal(sd,sd->itemid,hp,sp);
  5213. return SCRIPT_CMD_SUCCESS;
  5214. }
  5215. /*==========================================
  5216. *
  5217. *------------------------------------------*/
  5218. BUILDIN_FUNC(percentheal)
  5219. {
  5220. int hp,sp;
  5221. TBL_PC* sd;
  5222. hp=script_getnum(st,2);
  5223. sp=script_getnum(st,3);
  5224. if(potion_flag==1) {
  5225. potion_per_hp = hp;
  5226. potion_per_sp = sp;
  5227. return SCRIPT_CMD_SUCCESS;
  5228. }
  5229. if (!script_charid2sd(4,sd))
  5230. return SCRIPT_CMD_SUCCESS;
  5231. #ifdef RENEWAL
  5232. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5233. sp = 0;
  5234. #endif
  5235. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5236. hp = 0;
  5237. sp = 0;
  5238. }
  5239. if (sd->sc.data[SC_BITESCAR])
  5240. hp = 0;
  5241. pc_percentheal(sd,hp,sp);
  5242. return SCRIPT_CMD_SUCCESS;
  5243. }
  5244. /*==========================================
  5245. *
  5246. *------------------------------------------*/
  5247. BUILDIN_FUNC(jobchange)
  5248. {
  5249. int job, upper=-1;
  5250. job=script_getnum(st,2);
  5251. if( script_hasdata(st,3) )
  5252. upper=script_getnum(st,3);
  5253. if (pcdb_checkid(job))
  5254. {
  5255. TBL_PC* sd;
  5256. if (!script_charid2sd(4,sd))
  5257. return SCRIPT_CMD_SUCCESS;
  5258. pc_jobchange(sd, job, upper);
  5259. }
  5260. return SCRIPT_CMD_SUCCESS;
  5261. }
  5262. /*==========================================
  5263. *
  5264. *------------------------------------------*/
  5265. BUILDIN_FUNC(jobname)
  5266. {
  5267. int class_=script_getnum(st,2);
  5268. script_pushconststr(st, (char*)job_name(class_));
  5269. return SCRIPT_CMD_SUCCESS;
  5270. }
  5271. /// Get input from the player.
  5272. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5273. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5274. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5275. /// shorter than 'min' and 0 otherwise.
  5276. ///
  5277. /// input(<var>{,<min>{,<max>}}) -> <int>
  5278. BUILDIN_FUNC(input)
  5279. {
  5280. TBL_PC* sd;
  5281. struct script_data* data;
  5282. int64 uid;
  5283. const char* name;
  5284. int min;
  5285. int max;
  5286. if( !script_rid2sd(sd) )
  5287. return SCRIPT_CMD_SUCCESS;
  5288. data = script_getdata(st,2);
  5289. if( !data_isreference(data) ){
  5290. ShowError("script:input: not a variable\n");
  5291. script_reportdata(data);
  5292. st->state = END;
  5293. return SCRIPT_CMD_FAILURE;
  5294. }
  5295. uid = reference_getuid(data);
  5296. name = reference_getname(data);
  5297. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5298. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5299. #ifdef SECURE_NPCTIMEOUT
  5300. sd->npc_idle_type = NPCT_WAIT;
  5301. #endif
  5302. if( !sd->state.menu_or_input )
  5303. { // first invocation, display npc input box
  5304. sd->state.menu_or_input = 1;
  5305. st->state = RERUNLINE;
  5306. if( is_string_variable(name) )
  5307. clif_scriptinputstr(sd,st->oid);
  5308. else
  5309. clif_scriptinput(sd,st->oid);
  5310. }
  5311. else
  5312. { // take received text/value and store it in the designated variable
  5313. sd->state.menu_or_input = 0;
  5314. if( is_string_variable(name) )
  5315. {
  5316. int len = (int)strlen(sd->npc_str);
  5317. set_reg_str(st, sd, uid, name, sd->npc_str, script_getref(st,2));
  5318. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5319. }
  5320. else
  5321. {
  5322. int amount = sd->npc_amount;
  5323. set_reg_num(st, sd, uid, name, cap_value(amount,min,max), script_getref(st,2));
  5324. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5325. }
  5326. st->state = RUN;
  5327. }
  5328. return SCRIPT_CMD_SUCCESS;
  5329. }
  5330. // declare the copyarray method here for future reference
  5331. BUILDIN_FUNC(copyarray);
  5332. /// Sets the value of a variable.
  5333. /// The value is converted to the type of the variable.
  5334. ///
  5335. /// set(<variable>,<value>{,<charid>})
  5336. BUILDIN_FUNC(setr)
  5337. {
  5338. TBL_PC* sd = NULL;
  5339. struct script_data* data;
  5340. //struct script_data* datavalue;
  5341. int64 num;
  5342. uint8 pos = 4;
  5343. const char* name, *command = script_getfuncname(st);
  5344. char prefix;
  5345. data = script_getdata(st,2);
  5346. //datavalue = script_getdata(st,3);
  5347. if( !data_isreference(data) )
  5348. {
  5349. ShowError("script:set: not a variable\n");
  5350. script_reportdata(script_getdata(st,2));
  5351. st->state = END;
  5352. return SCRIPT_CMD_FAILURE;
  5353. }
  5354. num = reference_getuid(data);
  5355. name = reference_getname(data);
  5356. prefix = *name;
  5357. if (!strcmp(command, "setr"))
  5358. pos = 5;
  5359. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5360. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5361. return SCRIPT_CMD_FAILURE;
  5362. }
  5363. #if 0
  5364. if( data_isreference(datavalue) )
  5365. {// the value being referenced is a variable
  5366. const char* namevalue = reference_getname(datavalue);
  5367. if( !not_array_variable(*namevalue) )
  5368. {// array variable being copied into another array variable
  5369. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5370. {// player must be attached in order to copy a player variable
  5371. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5372. return SCRIPT_CMD_SUCCESS;
  5373. }
  5374. if( is_string_variable(namevalue) != is_string_variable(name) )
  5375. {// non-matching array value types
  5376. ShowWarning("script:set: two array variables do not match in type.\n");
  5377. return SCRIPT_CMD_SUCCESS;
  5378. }
  5379. // push the maximum number of array values to the stack
  5380. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5381. // call the copy array method directly
  5382. return buildin_copyarray(st);
  5383. }
  5384. }
  5385. #endif
  5386. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5387. if( is_string_variable(name) )
  5388. script_pushstrcopy(st,script_getstr(st, 4));
  5389. else
  5390. script_pushint(st,script_getnum64(st, 4));
  5391. } else // Return a copy of the variable reference
  5392. script_pushcopy(st, 2);
  5393. if( is_string_variable(name) )
  5394. set_reg_str( st, sd, num, name, script_getstr( st, 3 ), script_getref( st, 2 ) );
  5395. else
  5396. set_reg_num( st, sd, num, name, script_getnum64( st, 3 ), script_getref( st, 2 ) );
  5397. return SCRIPT_CMD_SUCCESS;
  5398. }
  5399. /////////////////////////////////////////////////////////////////////
  5400. /// Array variables
  5401. ///
  5402. /// Sets values of an array, from the starting index.
  5403. /// ex: setarray arr[1],1,2,3;
  5404. ///
  5405. /// setarray <array variable>,<value1>{,<value2>...};
  5406. BUILDIN_FUNC(setarray)
  5407. {
  5408. struct script_data* data;
  5409. const char* name;
  5410. uint32 start;
  5411. uint32 end;
  5412. int32 id;
  5413. int32 i;
  5414. TBL_PC* sd = NULL;
  5415. data = script_getdata(st, 2);
  5416. if( !data_isreference(data) )
  5417. {
  5418. ShowError("script:setarray: not a variable\n");
  5419. script_reportdata(data);
  5420. st->state = END;
  5421. return SCRIPT_CMD_FAILURE;// not a variable
  5422. }
  5423. id = reference_getid(data);
  5424. start = reference_getindex(data);
  5425. name = reference_getname(data);
  5426. if( not_server_variable(*name) )
  5427. {
  5428. if( !script_rid2sd(sd) )
  5429. return SCRIPT_CMD_SUCCESS;// no player attached
  5430. }
  5431. end = start + script_lastdata(st) - 2;
  5432. if( end > SCRIPT_MAX_ARRAYSIZE )
  5433. end = SCRIPT_MAX_ARRAYSIZE;
  5434. if( is_string_variable(name) )
  5435. {// string array
  5436. for( i = 3; start < end; ++start, ++i )
  5437. set_reg_str( st, sd, reference_uid( id, start ), name, script_getstr( st, i ), reference_getref( data ) );
  5438. }
  5439. else
  5440. {// int array
  5441. for( i = 3; start < end; ++start, ++i )
  5442. set_reg_num( st, sd, reference_uid( id, start ), name, script_getnum64( st, i ), reference_getref( data ) );
  5443. }
  5444. return SCRIPT_CMD_SUCCESS;
  5445. }
  5446. /// Sets count values of an array, from the starting index.
  5447. /// ex: cleararray arr[0],0,1;
  5448. ///
  5449. /// cleararray <array variable>,<value>,<count>;
  5450. BUILDIN_FUNC(cleararray)
  5451. {
  5452. struct script_data* data;
  5453. const char* name;
  5454. uint32 start;
  5455. uint32 end;
  5456. int32 id;
  5457. TBL_PC* sd = NULL;
  5458. data = script_getdata(st, 2);
  5459. if( !data_isreference(data) )
  5460. {
  5461. ShowError("script:cleararray: not a variable\n");
  5462. script_reportdata(data);
  5463. st->state = END;
  5464. return SCRIPT_CMD_FAILURE;// not a variable
  5465. }
  5466. id = reference_getid(data);
  5467. start = reference_getindex(data);
  5468. name = reference_getname(data);
  5469. if( not_server_variable(*name) )
  5470. {
  5471. if( !script_rid2sd(sd) )
  5472. return SCRIPT_CMD_SUCCESS;// no player attached
  5473. }
  5474. end = start + script_getnum(st, 4);
  5475. if( end > SCRIPT_MAX_ARRAYSIZE )
  5476. end = SCRIPT_MAX_ARRAYSIZE;
  5477. if( is_string_variable( name ) ){
  5478. const char* value = script_getstr( st, 3 );
  5479. for( ; start < end; ++start ){
  5480. set_reg_str( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5481. }
  5482. }else{
  5483. int64 value = script_getnum64( st, 3 );
  5484. for( ; start < end; ++start ){
  5485. set_reg_num( st, sd, reference_uid( id, start ), name, value, script_getref( st,2 ) );
  5486. }
  5487. }
  5488. return SCRIPT_CMD_SUCCESS;
  5489. }
  5490. /// Copies data from one array to another.
  5491. /// ex: copyarray arr[0],arr[2],2;
  5492. ///
  5493. /// copyarray <destination array variable>,<source array variable>,<count>;
  5494. BUILDIN_FUNC(copyarray)
  5495. {
  5496. struct script_data* data1;
  5497. struct script_data* data2;
  5498. const char* name1;
  5499. const char* name2;
  5500. int32 idx1;
  5501. int32 idx2;
  5502. int32 id1;
  5503. int32 id2;
  5504. int32 i;
  5505. uint32 count;
  5506. TBL_PC* sd = NULL;
  5507. data1 = script_getdata(st, 2);
  5508. data2 = script_getdata(st, 3);
  5509. if( !data_isreference(data1) || !data_isreference(data2) )
  5510. {
  5511. ShowError("script:copyarray: not a variable\n");
  5512. script_reportdata(data1);
  5513. script_reportdata(data2);
  5514. st->state = END;
  5515. return SCRIPT_CMD_FAILURE;// not a variable
  5516. }
  5517. id1 = reference_getid(data1);
  5518. id2 = reference_getid(data2);
  5519. idx1 = reference_getindex(data1);
  5520. idx2 = reference_getindex(data2);
  5521. name1 = reference_getname(data1);
  5522. name2 = reference_getname(data2);
  5523. bool is_string = is_string_variable( name1 );
  5524. if( is_string != is_string_variable( name2 ) ){
  5525. ShowError("script:copyarray: type mismatch\n");
  5526. script_reportdata(data1);
  5527. script_reportdata(data2);
  5528. st->state = END;
  5529. return SCRIPT_CMD_FAILURE;// data type mismatch
  5530. }
  5531. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5532. {
  5533. if( !script_rid2sd(sd) )
  5534. return SCRIPT_CMD_SUCCESS;// no player attached
  5535. }
  5536. count = script_getnum(st, 4);
  5537. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5538. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5539. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5540. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5541. if( id1 == id2 && idx1 > idx2 ){
  5542. // destination might be overlapping the source - copy in reverse order
  5543. for( i = count - 1; i >= 0; --i ){
  5544. if( is_string ){
  5545. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5546. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5547. // Remove stack entry from get_val2_str
  5548. script_removetop( st, -1, 0 );
  5549. }else{
  5550. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5551. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5552. }
  5553. }
  5554. }else{
  5555. // normal copy
  5556. for( i = 0; i < count; ++i ){
  5557. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ){
  5558. if( is_string ){
  5559. const char* value = get_val2_str( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5560. set_reg_str( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5561. // Remove stack entry from get_val2_str
  5562. script_removetop( st, -1, 0 );
  5563. }else{
  5564. int64 value = get_val2_num( st, reference_uid( id2, idx2 + i ), reference_getref( data2 ) );
  5565. set_reg_num( st, sd, reference_uid( id1, idx1 + i ), name1, value, reference_getref( data1 ) );
  5566. }
  5567. }else{
  5568. // out of range - assume ""/0
  5569. clear_reg( st, sd, reference_uid( id1, idx1 + i ), name1, reference_getref( data1 ) );
  5570. }
  5571. }
  5572. }
  5573. return SCRIPT_CMD_SUCCESS;
  5574. }
  5575. /// Returns the size of the array.
  5576. /// Assumes that everything before the starting index exists.
  5577. /// ex: getarraysize(arr[3])
  5578. ///
  5579. /// getarraysize(<array variable>) -> <int>
  5580. BUILDIN_FUNC(getarraysize)
  5581. {
  5582. struct script_data* data;
  5583. const char* name;
  5584. struct map_session_data* sd = NULL;
  5585. data = script_getdata(st, 2);
  5586. if( !data_isreference(data) )
  5587. {
  5588. ShowError("script:getarraysize: not a variable\n");
  5589. script_reportdata(data);
  5590. script_pushnil(st);
  5591. st->state = END;
  5592. return SCRIPT_CMD_FAILURE;// not a variable
  5593. }
  5594. name = reference_getname(data);
  5595. if( not_server_variable(*name) ){
  5596. if (!script_rid2sd(sd))
  5597. return SCRIPT_CMD_SUCCESS;// no player attached
  5598. }
  5599. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5600. return SCRIPT_CMD_SUCCESS;
  5601. }
  5602. int script_array_index_cmp(const void *a, const void *b) {
  5603. return ( *(unsigned int*)a - *(unsigned int*)b );
  5604. }
  5605. /// Deletes count or all the elements in an array, from the starting index.
  5606. /// ex: deletearray arr[4],2;
  5607. ///
  5608. /// deletearray <array variable>;
  5609. /// deletearray <array variable>,<count>;
  5610. BUILDIN_FUNC(deletearray)
  5611. {
  5612. struct script_data* data;
  5613. const char* name;
  5614. unsigned int start, end, i;
  5615. int id;
  5616. TBL_PC *sd = NULL;
  5617. struct script_array *sa = NULL;
  5618. struct reg_db *src = NULL;
  5619. data = script_getdata(st, 2);
  5620. if( !data_isreference(data) ) {
  5621. ShowError("script:deletearray: not a variable\n");
  5622. script_reportdata(data);
  5623. st->state = END;
  5624. return SCRIPT_CMD_FAILURE;// not a variable
  5625. }
  5626. id = reference_getid(data);
  5627. start = reference_getindex(data);
  5628. name = reference_getname(data);
  5629. if( not_server_variable(*name) ) {
  5630. if( !script_rid2sd(sd) )
  5631. return SCRIPT_CMD_SUCCESS;// no player attached
  5632. }
  5633. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5634. ShowError("script:deletearray: not a array\n");
  5635. script_reportdata(data);
  5636. st->state = END;
  5637. return SCRIPT_CMD_FAILURE;// not a variable
  5638. }
  5639. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5640. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5641. return SCRIPT_CMD_SUCCESS;// not a variable
  5642. }
  5643. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5644. if( start >= end )
  5645. return SCRIPT_CMD_SUCCESS;// nothing to free
  5646. if( script_hasdata(st,3) ) {
  5647. unsigned int count = script_getnum(st, 3);
  5648. if( count > end - start )
  5649. count = end - start;
  5650. if( count <= 0 )
  5651. return SCRIPT_CMD_SUCCESS;// nothing to free
  5652. bool is_string = is_string_variable( name );
  5653. if( end - start < sa->size ) {
  5654. // Better to iterate directly on the array, no speed-up from using sa
  5655. for( ; start + count < end; ++start ) {
  5656. // Compact and overwrite
  5657. if( is_string ){
  5658. const char* value = get_val2_str( st, reference_uid( id, start + count ), reference_getref( data ) );
  5659. set_reg_str( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5660. // Remove stack entry from get_val2_str
  5661. script_removetop( st, -1, 0 );
  5662. }else{
  5663. int64 value = get_val2_num( st, reference_uid( id, start + count ), reference_getref( data ) );
  5664. set_reg_num( st, sd, reference_uid( id, start ), name, value, reference_getref( data ) );
  5665. }
  5666. }
  5667. for( ; start < end; start++ ) {
  5668. // Clean up any leftovers that weren't overwritten
  5669. clear_reg( st, sd, reference_uid( id, start ), name, reference_getref( data ) );
  5670. }
  5671. } else {
  5672. // using sa to speed up
  5673. unsigned int *list = NULL, size = 0;
  5674. list = script_array_cpy_list(sa);
  5675. size = sa->size;
  5676. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5677. ARR_FIND(0, size, i, list[i] >= start);
  5678. for( ; i < size && list[i] < start + count; i++ ) {
  5679. // Clear any entries between start and start+count, if they exist
  5680. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5681. }
  5682. for( ; i < size && list[i] < end; i++ ) {
  5683. // Move back count positions any entries between start+count to fill the gaps
  5684. if( is_string ){
  5685. const char* value = get_val2_str( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5686. set_reg_str( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5687. // Remove stack entry from get_val2_str
  5688. script_removetop( st, -1, 0 );
  5689. }else{
  5690. int64 value = get_val2_num( st, reference_uid( id, list[i] ), reference_getref( data ) );
  5691. set_reg_num( st, sd, reference_uid( id, list[i] - count ), name, value, reference_getref( data ) );
  5692. }
  5693. // Clear their originals
  5694. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5695. }
  5696. }
  5697. } else {
  5698. unsigned int *list = NULL, size = 0;
  5699. list = script_array_cpy_list(sa);
  5700. size = sa->size;
  5701. for(i = 0; i < size; i++) {
  5702. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5703. clear_reg( st, sd, reference_uid( id, list[i] ), name, reference_getref( data ) );
  5704. }
  5705. }
  5706. return SCRIPT_CMD_SUCCESS;
  5707. }
  5708. /// Returns a reference to the target index of the array variable.
  5709. /// Equivalent to var[index].
  5710. ///
  5711. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5712. BUILDIN_FUNC(getelementofarray)
  5713. {
  5714. struct script_data* data;
  5715. int32 id;
  5716. int64 i;
  5717. data = script_getdata(st, 2);
  5718. if( !data_isreference(data) )
  5719. {
  5720. ShowError("script:getelementofarray: not a variable\n");
  5721. script_reportdata(data);
  5722. script_pushnil(st);
  5723. st->state = END;
  5724. return SCRIPT_CMD_SUCCESS;// not a variable
  5725. }
  5726. id = reference_getid(data);
  5727. i = script_getnum(st, 3);
  5728. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5729. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5730. script_reportdata(data);
  5731. script_pushnil(st);
  5732. st->state = END;
  5733. return SCRIPT_CMD_FAILURE;// out of range
  5734. }
  5735. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5736. return SCRIPT_CMD_SUCCESS;
  5737. }
  5738. /// Return the index number of the first matching value in an array.
  5739. /// ex: inarray arr,1;
  5740. ///
  5741. /// inarray <array variable>,<value>;
  5742. BUILDIN_FUNC(inarray)
  5743. {
  5744. struct script_data *data;
  5745. const char* name;
  5746. int id, i, array_size;
  5747. struct map_session_data* sd = NULL;
  5748. struct reg_db *ref = NULL;
  5749. data = script_getdata(st, 2);
  5750. if (!data_isreference(data))
  5751. {
  5752. ShowError("buildin_inarray: not a variable\n");
  5753. script_reportdata(data);
  5754. st->state = END;
  5755. return SCRIPT_CMD_FAILURE;
  5756. }
  5757. name = reference_getname(data);
  5758. ref = reference_getref(data);
  5759. if (not_server_variable(*name) && !script_rid2sd(sd))
  5760. return SCRIPT_CMD_FAILURE;
  5761. array_size = script_array_highest_key(st, sd, name, ref) - 1;
  5762. if (array_size < 0)
  5763. {
  5764. script_pushint(st, -1);
  5765. return SCRIPT_CMD_SUCCESS;
  5766. }
  5767. if (array_size > SCRIPT_MAX_ARRAYSIZE)
  5768. {
  5769. ShowError("buildin_inarray: The array is too large.\n");
  5770. script_reportdata(data);
  5771. st->state = END;
  5772. return SCRIPT_CMD_FAILURE;
  5773. }
  5774. id = reference_getid(data);
  5775. if( is_string_variable( name ) ){
  5776. const char* value = script_getstr( st, 3 );
  5777. for( i = 0; i <= array_size; ++i ){
  5778. const char* temp = get_val2_str( st, reference_uid( id, i ), ref );
  5779. if( !strcmp( temp, value ) ){
  5780. // Remove stack entry from get_val2_str
  5781. script_removetop( st, -1, 0 );
  5782. script_pushint( st, i );
  5783. return SCRIPT_CMD_SUCCESS;
  5784. }
  5785. // Remove stack entry from get_val2_str
  5786. script_removetop( st, -1, 0 );
  5787. }
  5788. }else{
  5789. int64 value = script_getnum64( st, 3 );
  5790. for( i = 0; i <= array_size; ++i ){
  5791. int64 temp = get_val2_num( st, reference_uid( id, i ), ref );
  5792. if( temp == value ){
  5793. script_pushint( st, i );
  5794. return SCRIPT_CMD_SUCCESS;
  5795. }
  5796. }
  5797. }
  5798. script_pushint(st, -1);
  5799. return SCRIPT_CMD_SUCCESS;
  5800. }
  5801. /// Return the number of matches in two arrays.
  5802. /// ex: countinarray arr[0],arr1[0];
  5803. ///
  5804. /// countinarray <array variable>,<array variable>;
  5805. BUILDIN_FUNC(countinarray)
  5806. {
  5807. struct script_data *data1 , *data2;
  5808. const char* name1;
  5809. const char* name2;
  5810. int id1, id2, i, j, array_size1, array_size2, case_count = 0;
  5811. struct map_session_data* sd = NULL;
  5812. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5813. data1 = script_getdata(st, 2);
  5814. data2 = script_getdata(st, 3);
  5815. if (!data_isreference(data1) || !data_isreference(data2))
  5816. {
  5817. ShowError("buildin_countinarray: not a variable\n");
  5818. script_reportdata(data1);
  5819. script_reportdata(data2);
  5820. st->state = END;
  5821. return SCRIPT_CMD_FAILURE;
  5822. }
  5823. name1 = reference_getname(data1);
  5824. name2 = reference_getname(data2);
  5825. ref1 = reference_getref(data1);
  5826. ref2 = reference_getref(data2);
  5827. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5828. return SCRIPT_CMD_FAILURE;
  5829. array_size1 = script_array_highest_key(st, sd, name1, ref1) - 1;
  5830. array_size2 = script_array_highest_key(st, sd, name2, ref2) - 1;
  5831. if (array_size1 < 0 || array_size2 < 0)
  5832. {
  5833. script_pushint(st, 0);
  5834. return SCRIPT_CMD_SUCCESS;
  5835. }
  5836. if (array_size1 > SCRIPT_MAX_ARRAYSIZE || array_size2 > SCRIPT_MAX_ARRAYSIZE)
  5837. {
  5838. ShowError("buildin_countinarray: The array is too large.\n");
  5839. script_reportdata(data1);
  5840. script_reportdata(data2);
  5841. st->state = END;
  5842. return SCRIPT_CMD_FAILURE;
  5843. }
  5844. i = reference_getindex(data1);
  5845. j = reference_getindex(data2);
  5846. if (array_size1 < i || array_size2 < j)
  5847. { //To prevent unintended behavior
  5848. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5849. script_reportdata(data1);
  5850. script_reportdata(data2);
  5851. st->state = END;
  5852. return SCRIPT_CMD_FAILURE;
  5853. }
  5854. id1 = reference_getid(data1);
  5855. id2 = reference_getid(data2);
  5856. if( is_string_variable( name1 ) && is_string_variable( name2 ) ){
  5857. for( ; i <= array_size1; ++i ){
  5858. const char* temp1 = get_val2_str( st, reference_uid( id1, i ), ref1 );
  5859. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5860. const char* temp2 = get_val2_str( st, reference_uid( id2, j ), ref2 );
  5861. if( !strcmp( temp1, temp2 ) ){
  5862. case_count++;
  5863. }
  5864. // Remove stack entry from get_val2_str
  5865. script_removetop( st, -1, 0 );
  5866. }
  5867. // Remove stack entry from get_val2_str
  5868. script_removetop( st, -1, 0 );
  5869. }
  5870. }else if( !is_string_variable( name1 ) && !is_string_variable( name2 ) ){
  5871. for( ; i <= array_size1; ++i ){
  5872. int64 temp1 = get_val2_num( st, reference_uid( id1, i ), ref1 );
  5873. for( j = reference_getindex( data2 ); j <= array_size2; j++ ){
  5874. int64 temp2 = get_val2_num( st, reference_uid( id2, j ), ref2 );
  5875. if( temp1 == temp2 ){
  5876. case_count++;
  5877. }
  5878. }
  5879. }
  5880. }else{
  5881. ShowError("buildin_countinarray: Arrays do not match , You can't compare an int array to a string array.\n");
  5882. script_reportdata(data1);
  5883. script_reportdata(data2);
  5884. st->state = END;
  5885. return SCRIPT_CMD_FAILURE;
  5886. }
  5887. script_pushint(st, case_count);
  5888. return SCRIPT_CMD_SUCCESS;
  5889. }
  5890. /////////////////////////////////////////////////////////////////////
  5891. /// ...
  5892. ///
  5893. /*==========================================
  5894. *
  5895. *------------------------------------------*/
  5896. BUILDIN_FUNC(setlook)
  5897. {
  5898. int type,val;
  5899. TBL_PC* sd;
  5900. type = script_getnum(st,2);
  5901. val = script_getnum(st,3);
  5902. if (!script_charid2sd(4,sd))
  5903. return SCRIPT_CMD_SUCCESS;
  5904. pc_changelook(sd,type,val);
  5905. return SCRIPT_CMD_SUCCESS;
  5906. }
  5907. BUILDIN_FUNC(changelook)
  5908. { // As setlook but only client side
  5909. int type,val;
  5910. TBL_PC* sd;
  5911. type = script_getnum(st,2);
  5912. val = script_getnum(st,3);
  5913. if (!script_charid2sd(4,sd))
  5914. return SCRIPT_CMD_SUCCESS;
  5915. switch(type) {
  5916. case LOOK_HAIR:
  5917. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  5918. break;
  5919. case LOOK_HAIR_COLOR:
  5920. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  5921. break;
  5922. case LOOK_CLOTHES_COLOR:
  5923. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  5924. break;
  5925. case LOOK_BODY2:
  5926. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  5927. break;
  5928. default:
  5929. break;
  5930. }
  5931. clif_changelook(&sd->bl, type, val);
  5932. return SCRIPT_CMD_SUCCESS;
  5933. }
  5934. /*==========================================
  5935. *
  5936. *------------------------------------------*/
  5937. BUILDIN_FUNC(cutin)
  5938. {
  5939. TBL_PC* sd;
  5940. if( !script_rid2sd(sd) )
  5941. return SCRIPT_CMD_SUCCESS;
  5942. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5943. return SCRIPT_CMD_SUCCESS;
  5944. }
  5945. /*==========================================
  5946. *
  5947. *------------------------------------------*/
  5948. BUILDIN_FUNC(viewpoint)
  5949. {
  5950. int type,x,y,id,color;
  5951. TBL_PC* sd;
  5952. if (!script_charid2sd(7, sd)) {
  5953. st->state = END;
  5954. return SCRIPT_CMD_FAILURE;
  5955. }
  5956. type=script_getnum(st,2);
  5957. x=script_getnum(st,3);
  5958. y=script_getnum(st,4);
  5959. id=script_getnum(st,5);
  5960. color=script_getnum(st,6);
  5961. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5962. return SCRIPT_CMD_SUCCESS;
  5963. }
  5964. /**
  5965. * Set random options for new item
  5966. * @param st Script state
  5967. * @param it Temporary item data
  5968. * @param funcname Function name
  5969. * @param x First position of random option id array from the script
  5970. **/
  5971. static int script_getitem_randomoption(struct script_state *st, struct map_session_data* sd, struct item *it, const char *funcname, int x) {
  5972. int i, opt_id_n;
  5973. struct script_data *opt_id = script_getdata(st,x);
  5974. struct script_data *opt_val = script_getdata(st,x+1);
  5975. struct script_data *opt_param = script_getdata(st,x+2);
  5976. const char *opt_id_var = reference_getname(opt_id);
  5977. const char *opt_val_var = reference_getname(opt_val);
  5978. const char *opt_param_var = reference_getname(opt_param);
  5979. int32 opt_id_id, opt_id_idx;
  5980. int32 opt_val_id, opt_val_idx;
  5981. int32 opt_param_id, opt_param_idx;
  5982. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  5983. // Check if the variable requires a player
  5984. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  5985. // If no player is attached
  5986. if( !script_rid2sd(sd) ){
  5987. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  5988. return SCRIPT_CMD_FAILURE;
  5989. }
  5990. }
  5991. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  5992. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  5993. return SCRIPT_CMD_FAILURE;
  5994. }
  5995. if (is_string_variable(opt_id_var)) {
  5996. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  5997. return SCRIPT_CMD_FAILURE;
  5998. }
  5999. // Check if the variable requires a player
  6000. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  6001. // If no player is attached
  6002. if( !script_rid2sd(sd) ){
  6003. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  6004. return SCRIPT_CMD_FAILURE;
  6005. }
  6006. }
  6007. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  6008. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  6009. return SCRIPT_CMD_FAILURE;
  6010. }
  6011. if (is_string_variable(opt_val_var)) {
  6012. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6013. return SCRIPT_CMD_FAILURE;
  6014. }
  6015. // Check if the variable requires a player
  6016. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  6017. // If no player is attached
  6018. if( !script_rid2sd(sd) ){
  6019. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  6020. return SCRIPT_CMD_FAILURE;
  6021. }
  6022. }
  6023. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  6024. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  6025. return SCRIPT_CMD_FAILURE;
  6026. }
  6027. if (is_string_variable(opt_param_var)) {
  6028. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6029. return SCRIPT_CMD_FAILURE;
  6030. }
  6031. opt_id_ref = reference_getref(opt_id);
  6032. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  6033. opt_val_ref = reference_getref(opt_val);
  6034. opt_param_ref = reference_getref(opt_param);
  6035. opt_id_id = reference_getid(opt_id);
  6036. opt_val_id = reference_getid(opt_val);
  6037. opt_param_id = reference_getid(opt_param);
  6038. opt_id_idx = reference_getindex(opt_id);
  6039. opt_val_idx = reference_getindex(opt_val);
  6040. opt_param_idx = reference_getindex(opt_param);
  6041. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6042. it->option[i].id = (short)get_val2_num( st, reference_uid( opt_id_id, opt_id_idx + i ), opt_id_ref );
  6043. it->option[i].value = (short)get_val2_num( st, reference_uid( opt_val_id, opt_val_idx + i ), opt_val_ref );
  6044. it->option[i].param = (char)get_val2_num( st, reference_uid( opt_param_id, opt_param_idx + i ), opt_param_ref );
  6045. }
  6046. return SCRIPT_CMD_SUCCESS;
  6047. }
  6048. /**
  6049. * Sub function for counting items
  6050. * @param items: Item array to search
  6051. * @param id: Item data to search for
  6052. * @param size: Maximum size of array
  6053. * @param expanded: If the script command has extra arguments
  6054. * @param random_option: If the struct command has random option arguments
  6055. * @param st: Script state (required for random options)
  6056. * @param sd: Player data (required for random options)
  6057. * @param rental: Whether or not to count rental items
  6058. * @return Total count of item being searched
  6059. */
  6060. static int script_countitem_sub(struct item *items, struct item_data *id, int size, bool expanded, bool random_option, struct script_state *st, struct map_session_data *sd = nullptr, bool rental = false) {
  6061. nullpo_retr(-1, items);
  6062. nullpo_retr(-1, st);
  6063. int count = 0;
  6064. if (!expanded) { // For non-expanded functions
  6065. t_itemid nameid = id->nameid;
  6066. for (int i = 0; i < size; i++) {
  6067. item *itm = &items[i];
  6068. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1 || (!rental && itm->expire_time > 0))
  6069. continue;
  6070. if (itm->nameid == nameid)
  6071. count += itm->amount;
  6072. }
  6073. } else { // For expanded functions
  6074. item it;
  6075. memset(&it, 0, sizeof(it));
  6076. it.nameid = id->nameid;
  6077. it.identify = script_getnum(st,3);
  6078. it.refine = script_getnum(st,4);
  6079. it.attribute = script_getnum(st,5);
  6080. it.card[0] = script_getnum(st,6);
  6081. it.card[1] = script_getnum(st,7);
  6082. it.card[2] = script_getnum(st,8);
  6083. it.card[3] = script_getnum(st,9);
  6084. if (random_option) {
  6085. if (!sd) {
  6086. ShowError("buildin_countitem3: Player not attached.\n");
  6087. return -1;
  6088. }
  6089. int res = script_getitem_randomoption(st, sd, &it, "countitem3", 10);
  6090. if (res != SCRIPT_CMD_SUCCESS)
  6091. return -1;
  6092. }
  6093. for (int i = 0; i < size; i++) {
  6094. item *itm = &items[i];
  6095. if (itm == nullptr || itm->nameid == 0 || itm->amount < 1 || (!rental && items[i].expire_time > 0))
  6096. continue;
  6097. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute)
  6098. continue;
  6099. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6100. continue;
  6101. if (random_option) {
  6102. uint8 j;
  6103. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6104. 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)
  6105. break;
  6106. }
  6107. if (j != MAX_ITEM_RDM_OPT)
  6108. continue;
  6109. }
  6110. count += items[i].amount;
  6111. }
  6112. }
  6113. return count;
  6114. }
  6115. /**
  6116. * Returns number of items in inventory
  6117. * countitem(<nameID>{,<accountID>})
  6118. * countitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6119. * countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6120. * countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6121. */
  6122. BUILDIN_FUNC(countitem)
  6123. {
  6124. TBL_PC *sd;
  6125. char *command = (char *)script_getfuncname(st);
  6126. int aid = 3;
  6127. bool random_option = false;
  6128. if (command[strlen(command)-1] == '2')
  6129. aid = 10;
  6130. else if (command[strlen(command)-1] == '3') {
  6131. aid = 13;
  6132. random_option = true;
  6133. }
  6134. if (!script_accid2sd(aid, sd))
  6135. return SCRIPT_CMD_FAILURE;
  6136. struct item_data *id;
  6137. if (script_isstring(st, 2)) // item name
  6138. id = itemdb_searchname(script_getstr(st, 2));
  6139. else // item id
  6140. id = itemdb_exists(script_getnum(st, 2));
  6141. if (!id) {
  6142. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6143. script_pushint(st, 0);
  6144. return SCRIPT_CMD_FAILURE;
  6145. }
  6146. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, (aid > 3) ? true : false, random_option, st, sd);
  6147. if (count < 0) {
  6148. st->state = END;
  6149. return SCRIPT_CMD_FAILURE;
  6150. }
  6151. script_pushint(st, count);
  6152. return SCRIPT_CMD_SUCCESS;
  6153. }
  6154. /**
  6155. * Returns number of items in cart
  6156. * cartcountitem(<nameID>{,<accountID>})
  6157. * cartcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6158. */
  6159. BUILDIN_FUNC(cartcountitem)
  6160. {
  6161. TBL_PC *sd;
  6162. char *command = (char *)script_getfuncname(st);
  6163. int aid = 3;
  6164. if (command[strlen(command) - 1] == '2')
  6165. aid = 10;
  6166. if (!script_accid2sd(aid, sd))
  6167. return SCRIPT_CMD_FAILURE;
  6168. if (!pc_iscarton(sd)) {
  6169. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6170. script_pushint(st, -1);
  6171. return SCRIPT_CMD_FAILURE;
  6172. }
  6173. struct item_data *id;
  6174. if (script_isstring(st, 2)) // item name
  6175. id = itemdb_searchname(script_getstr(st, 2));
  6176. else // item id
  6177. id = itemdb_exists(script_getnum(st, 2));
  6178. if (!id) {
  6179. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6180. script_pushint(st, 0);
  6181. return SCRIPT_CMD_FAILURE;
  6182. }
  6183. int count = script_countitem_sub(sd->cart.u.items_cart, id, MAX_CART, (aid > 3) ? true : false, false, st);
  6184. if (count < 0) {
  6185. st->state = END;
  6186. return SCRIPT_CMD_FAILURE;
  6187. }
  6188. script_pushint(st, count);
  6189. return SCRIPT_CMD_SUCCESS;
  6190. }
  6191. /**
  6192. * Returns number of items in storage
  6193. * storagecountitem(<nameID>{,<accountID>})
  6194. * storagecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6195. */
  6196. BUILDIN_FUNC(storagecountitem)
  6197. {
  6198. TBL_PC *sd;
  6199. char *command = (char *)script_getfuncname(st);
  6200. int aid = 3;
  6201. if (command[strlen(command) - 1] == '2')
  6202. aid = 10;
  6203. if (!script_accid2sd(aid, sd))
  6204. return SCRIPT_CMD_FAILURE;
  6205. struct item_data *id;
  6206. if (script_isstring(st, 2)) // item name
  6207. id = itemdb_searchname(script_getstr(st, 2));
  6208. else // item id
  6209. id = itemdb_exists(script_getnum(st, 2));
  6210. if (!id) {
  6211. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6212. script_pushint(st, 0);
  6213. return SCRIPT_CMD_FAILURE;
  6214. }
  6215. if (sd->state.storage_flag != 0) {
  6216. script_pushint(st, -1);
  6217. return SCRIPT_CMD_SUCCESS;
  6218. }
  6219. int count = script_countitem_sub(sd->storage.u.items_storage, id, MAX_STORAGE, (aid > 3) ? true : false, false, st);
  6220. if (count < 0) {
  6221. st->state = END;
  6222. return SCRIPT_CMD_FAILURE;
  6223. }
  6224. script_pushint(st, count);
  6225. return SCRIPT_CMD_SUCCESS;
  6226. }
  6227. /**
  6228. * Returns number of items in guild storage
  6229. * guildstoragecountitem(<nameID>{,<accountID>})
  6230. * guildstoragecountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6231. */
  6232. BUILDIN_FUNC(guildstoragecountitem)
  6233. {
  6234. TBL_PC *sd;
  6235. char *command = (char *)script_getfuncname(st);
  6236. int aid = 3;
  6237. if (command[strlen(command) - 1] == '2')
  6238. aid = 10;
  6239. if (!script_accid2sd(aid, sd))
  6240. return SCRIPT_CMD_FAILURE;
  6241. struct item_data *id;
  6242. if (script_isstring(st, 2)) // item name
  6243. id = itemdb_searchname(script_getstr(st, 2));
  6244. else // item id
  6245. id = itemdb_exists(script_getnum(st, 2));
  6246. if (!id) {
  6247. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6248. script_pushint(st, 0);
  6249. return SCRIPT_CMD_FAILURE;
  6250. }
  6251. struct s_storage *gstor = guild2storage(sd->status.guild_id);
  6252. if (!gstor || (gstor && sd->state.storage_flag != 0)) {
  6253. script_pushint(st, -1);
  6254. return SCRIPT_CMD_SUCCESS;
  6255. }
  6256. gstor->lock = true;
  6257. int count = script_countitem_sub(gstor->u.items_guild, id, MAX_GUILD_STORAGE, (aid > 3) ? true : false, false, st);
  6258. storage_guild_storageclose(sd);
  6259. gstor->lock = false;
  6260. if (count < 0) {
  6261. st->state = END;
  6262. return SCRIPT_CMD_FAILURE;
  6263. }
  6264. script_pushint(st, count);
  6265. return SCRIPT_CMD_SUCCESS;
  6266. }
  6267. /**
  6268. * Returns number of rental items in inventory
  6269. * rentalcountitem(<nameID>{,<accountID>})
  6270. * rentalcountitem2(<nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6271. * rentalcountitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6272. * rentalcountitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<accountID>})
  6273. */
  6274. BUILDIN_FUNC(rentalcountitem)
  6275. {
  6276. char *command = (char *)script_getfuncname(st);
  6277. int aid = 3;
  6278. bool random_option = false;
  6279. if (command[strlen(command) - 1] == '2')
  6280. aid = 10;
  6281. else if (command[strlen(command) - 1] == '3') {
  6282. aid = 13;
  6283. random_option = true;
  6284. }
  6285. map_session_data *sd;
  6286. if (!script_accid2sd(aid, sd))
  6287. return SCRIPT_CMD_FAILURE;
  6288. item_data *id;
  6289. if (script_isstring(st, 2)) // item name
  6290. id = itemdb_searchname(script_getstr(st, 2));
  6291. else // item id
  6292. id = itemdb_exists(script_getnum(st, 2));
  6293. if (!id) {
  6294. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st, 2)); // returns string, regardless of what it was
  6295. script_pushint(st, 0);
  6296. return SCRIPT_CMD_FAILURE;
  6297. }
  6298. int count = script_countitem_sub(sd->inventory.u.items_inventory, id, MAX_INVENTORY, (aid > 3) ? true : false, random_option, st, sd, true);
  6299. if (count < 0) {
  6300. st->state = END;
  6301. return SCRIPT_CMD_FAILURE;
  6302. }
  6303. script_pushint(st, count);
  6304. return SCRIPT_CMD_SUCCESS;
  6305. }
  6306. /*==========================================
  6307. * Check if item with this amount can fit in inventory
  6308. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6309. * Return
  6310. * 0 : fail
  6311. * 1 : success (npc side only)
  6312. *------------------------------------------*/
  6313. BUILDIN_FUNC(checkweight)
  6314. {
  6315. int slots = 0;
  6316. unsigned short amount2 = 0;
  6317. unsigned int weight = 0, i, nbargs;
  6318. struct item_data* id = NULL;
  6319. struct map_session_data* sd;
  6320. if( !script_rid2sd(sd) )
  6321. return SCRIPT_CMD_SUCCESS;
  6322. nbargs = script_lastdata(st)+1;
  6323. if(nbargs%2) {
  6324. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6325. script_pushint(st,0);
  6326. return SCRIPT_CMD_FAILURE;
  6327. }
  6328. slots = pc_inventoryblank(sd); //nb of empty slot
  6329. for (i = 2; i < nbargs; i += 2) {
  6330. t_itemid nameid;
  6331. unsigned short amount;
  6332. if( script_isstring(st, i) ) // item name
  6333. id = itemdb_searchname(script_getstr(st, i));
  6334. else // item id
  6335. id = itemdb_exists(script_getnum(st, i));
  6336. if( id == NULL ) {
  6337. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6338. script_pushint(st,0);
  6339. return SCRIPT_CMD_FAILURE;
  6340. }
  6341. nameid = id->nameid;
  6342. amount = script_getnum(st,i+1);
  6343. if( amount < 1 ) {
  6344. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6345. script_pushint(st,0);
  6346. return SCRIPT_CMD_FAILURE;
  6347. }
  6348. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6349. if( weight + sd->weight > sd->max_weight )
  6350. {// too heavy
  6351. script_pushint(st,0);
  6352. return SCRIPT_CMD_SUCCESS;
  6353. }
  6354. switch( pc_checkadditem(sd, nameid, amount) ) {
  6355. case CHKADDITEM_EXIST:
  6356. // item is already in inventory, but there is still space for the requested amount
  6357. break;
  6358. case CHKADDITEM_NEW:
  6359. if( itemdb_isstackable(nameid) ) {
  6360. // stackable
  6361. amount2++;
  6362. if( slots < amount2 ) {
  6363. script_pushint(st,0);
  6364. return SCRIPT_CMD_SUCCESS;
  6365. }
  6366. } else {
  6367. // non-stackable
  6368. amount2 += amount;
  6369. if( slots < amount2) {
  6370. script_pushint(st,0);
  6371. return SCRIPT_CMD_SUCCESS;
  6372. }
  6373. }
  6374. break;
  6375. case CHKADDITEM_OVERAMOUNT:
  6376. script_pushint(st,0);
  6377. return SCRIPT_CMD_SUCCESS;
  6378. }
  6379. }
  6380. script_pushint(st,1);
  6381. return SCRIPT_CMD_SUCCESS;
  6382. }
  6383. BUILDIN_FUNC(checkweight2)
  6384. {
  6385. //variable sub checkweight
  6386. int i = 0, slots = 0, weight = 0;
  6387. short fail = 0;
  6388. unsigned short amount2 = 0;
  6389. //variable for array parsing
  6390. struct script_data* data_it;
  6391. struct script_data* data_nb;
  6392. const char* name_it;
  6393. const char* name_nb;
  6394. int32 id_it, id_nb;
  6395. int32 idx_it, idx_nb;
  6396. int nb_it, nb_nb; //array size
  6397. TBL_PC *sd;
  6398. if( !script_rid2sd(sd) )
  6399. return SCRIPT_CMD_SUCCESS;
  6400. data_it = script_getdata(st, 2);
  6401. data_nb = script_getdata(st, 3);
  6402. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6403. ShowError("buildin_checkweight2: parameter not a variable\n");
  6404. script_pushint(st,0);
  6405. return SCRIPT_CMD_FAILURE;// not a variable
  6406. }
  6407. id_it = reference_getid(data_it);
  6408. id_nb = reference_getid(data_nb);
  6409. idx_it = reference_getindex(data_it);
  6410. idx_nb = reference_getindex(data_nb);
  6411. name_it = reference_getname(data_it);
  6412. name_nb = reference_getname(data_nb);
  6413. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6414. ShowError("buildin_checkweight2: illegal type, need int\n");
  6415. script_pushint(st,0);
  6416. return SCRIPT_CMD_FAILURE;// not supported
  6417. }
  6418. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6419. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6420. if(nb_it != nb_nb) {
  6421. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6422. fail = 1;
  6423. }
  6424. slots = pc_inventoryblank(sd);
  6425. for(i=0; i<nb_it; i++) {
  6426. t_itemid nameid = (t_itemid)get_val2_num( st, reference_uid( id_it, idx_it + i ), reference_getref( data_it ) );
  6427. unsigned short amount = (unsigned short)get_val2_num( st, reference_uid( id_nb, idx_nb + i ), reference_getref( data_nb ) );
  6428. if(fail)
  6429. continue; //cpntonie to depop rest
  6430. if(itemdb_exists(nameid) == NULL ) {
  6431. ShowError("buildin_checkweight2: Invalid item '%u'.\n", nameid);
  6432. fail=1;
  6433. continue;
  6434. }
  6435. if(amount < 0 ) {
  6436. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6437. fail = 1;
  6438. continue;
  6439. }
  6440. weight += itemdb_weight(nameid)*amount;
  6441. if( weight + sd->weight > sd->max_weight ) {
  6442. fail = 1;
  6443. continue;
  6444. }
  6445. switch( pc_checkadditem(sd, nameid, amount) ) {
  6446. case CHKADDITEM_EXIST:
  6447. // item is already in inventory, but there is still space for the requested amount
  6448. break;
  6449. case CHKADDITEM_NEW:
  6450. if( itemdb_isstackable(nameid) ) {// stackable
  6451. amount2++;
  6452. if( slots < amount2 )
  6453. fail = 1;
  6454. } else {// non-stackable
  6455. amount2 += amount;
  6456. if( slots < amount2 ) {
  6457. fail = 1;
  6458. }
  6459. }
  6460. break;
  6461. case CHKADDITEM_OVERAMOUNT:
  6462. fail = 1;
  6463. } //end switch
  6464. } //end loop DO NOT break it prematurly we need to depop all stack
  6465. fail ? script_pushint(st,0) : script_pushint(st,1);
  6466. return SCRIPT_CMD_SUCCESS;
  6467. }
  6468. /*==========================================
  6469. * getitem <item id>,<amount>{,<account ID>};
  6470. * getitem "<item name>",<amount>{,<account ID>};
  6471. *
  6472. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6473. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6474. * Type:
  6475. * 0 - No bound
  6476. * 1 - Account Bound
  6477. * 2 - Guild Bound
  6478. * 3 - Party Bound
  6479. * 4 - Character Bound
  6480. *------------------------------------------*/
  6481. BUILDIN_FUNC(getitem)
  6482. {
  6483. int get_count, i;
  6484. t_itemid nameid;
  6485. unsigned short amount;
  6486. struct item it;
  6487. TBL_PC *sd;
  6488. unsigned char flag = 0;
  6489. const char* command = script_getfuncname(st);
  6490. struct item_data *id = NULL;
  6491. if( script_isstring(st, 2) ) {// "<item name>"
  6492. const char *name = script_getstr(st, 2);
  6493. id = itemdb_searchname(name);
  6494. if( id == NULL ){
  6495. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6496. return SCRIPT_CMD_FAILURE; //No item created.
  6497. }
  6498. nameid = id->nameid;
  6499. } else {// <item id>
  6500. nameid = script_getnum(st, 2);
  6501. if( !(id = itemdb_exists(nameid)) ){
  6502. ShowError("buildin_getitem: Nonexistant item %u requested.\n", nameid);
  6503. return SCRIPT_CMD_FAILURE; //No item created.
  6504. }
  6505. }
  6506. // <amount>
  6507. if( (amount = script_getnum(st,3)) <= 0)
  6508. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6509. memset(&it,0,sizeof(it));
  6510. it.nameid = nameid;
  6511. it.identify = 1;
  6512. it.bound = BOUND_NONE;
  6513. if( !strcmp(command,"getitembound") ) {
  6514. char bound = script_getnum(st,4);
  6515. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6516. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6517. return SCRIPT_CMD_FAILURE;
  6518. }
  6519. script_mapid2sd(5,sd);
  6520. it.bound = bound;
  6521. } else {
  6522. script_mapid2sd(4,sd);
  6523. }
  6524. if( sd == NULL ) // no target
  6525. return SCRIPT_CMD_SUCCESS;
  6526. //Check if it's stackable.
  6527. if (!itemdb_isstackable2(id))
  6528. get_count = 1;
  6529. else
  6530. get_count = amount;
  6531. for (i = 0; i < amount; i += get_count)
  6532. {
  6533. // if not pet egg
  6534. if (!pet_create_egg(sd, nameid))
  6535. {
  6536. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6537. {
  6538. clif_additem(sd, 0, 0, flag);
  6539. if( pc_candrop(sd,&it) )
  6540. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6541. }
  6542. }
  6543. }
  6544. return SCRIPT_CMD_SUCCESS;
  6545. }
  6546. /*==========================================
  6547. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6548. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6549. *
  6550. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6551. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6552. *
  6553. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6554. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6555. *
  6556. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6557. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6558. * Type:
  6559. * 0 - No bound
  6560. * 1 - Account Bound
  6561. * 2 - Guild Bound
  6562. * 3 - Party Bound
  6563. * 4 - Character Bound
  6564. *------------------------------------------*/
  6565. BUILDIN_FUNC(getitem2)
  6566. {
  6567. t_itemid nameid;
  6568. unsigned short amount;
  6569. int iden, ref, attr;
  6570. t_itemid c1, c2, c3, c4;
  6571. char bound = BOUND_NONE;
  6572. struct item_data *item_data = NULL;
  6573. struct item item_tmp;
  6574. TBL_PC *sd;
  6575. const char* command = script_getfuncname(st);
  6576. int offset = 0;
  6577. if( !strncmp(command,"getitembound",12) ) {
  6578. int aid_pos = 12;
  6579. bound = script_getnum(st,11);
  6580. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6581. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6582. return SCRIPT_CMD_FAILURE;
  6583. }
  6584. if (command[strlen(command)-1] == '3') {
  6585. offset = 12;
  6586. aid_pos = 15;
  6587. }
  6588. script_mapid2sd(aid_pos,sd);
  6589. } else {
  6590. int aid_pos = 11;
  6591. if (strcmpi(command,"getitem3") == 0) {
  6592. offset = 11;
  6593. aid_pos = 14;
  6594. }
  6595. script_mapid2sd(aid_pos,sd);
  6596. }
  6597. if( sd == NULL ) // no target
  6598. return SCRIPT_CMD_SUCCESS;
  6599. if( script_isstring(st, 2) ) {
  6600. const char *name = script_getstr(st, 2);
  6601. if( (item_data = itemdb_searchname(name)) == NULL ){
  6602. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6603. return SCRIPT_CMD_FAILURE; //No item created.
  6604. }
  6605. nameid = item_data->nameid;
  6606. } else {
  6607. nameid = script_getnum(st, 2);
  6608. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6609. ShowError("buildin_getitem2: Nonexistant item %u requested (by conv_num).\n", nameid);
  6610. return SCRIPT_CMD_FAILURE; //No item created.
  6611. }
  6612. }
  6613. amount = script_getnum(st,3);
  6614. iden = script_getnum(st,4);
  6615. ref = script_getnum(st,5);
  6616. attr = script_getnum(st,6);
  6617. c1 = script_getnum(st,7);
  6618. c2 = script_getnum(st,8);
  6619. c3 = script_getnum(st,9);
  6620. c4 = script_getnum(st,10);
  6621. if( item_data ) {
  6622. int get_count = 0, i;
  6623. memset(&item_tmp,0,sizeof(item_tmp));
  6624. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6625. if(ref > MAX_REFINE)
  6626. ref = MAX_REFINE;
  6627. }
  6628. else if( item_data->type == IT_PETEGG ) {
  6629. iden = 1;
  6630. ref = 0;
  6631. }
  6632. else {
  6633. iden = 1;
  6634. ref = attr = 0;
  6635. }
  6636. item_tmp.nameid = nameid;
  6637. item_tmp.identify = iden;
  6638. item_tmp.refine = ref;
  6639. item_tmp.attribute = attr;
  6640. item_tmp.card[0] = c1;
  6641. item_tmp.card[1] = c2;
  6642. item_tmp.card[2] = c3;
  6643. item_tmp.card[3] = c4;
  6644. item_tmp.bound = bound;
  6645. if (offset != 0) {
  6646. int res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6647. if (res == SCRIPT_CMD_FAILURE)
  6648. return SCRIPT_CMD_FAILURE;
  6649. }
  6650. //Check if it's stackable.
  6651. if (!itemdb_isstackable2(item_data))
  6652. get_count = 1;
  6653. else
  6654. get_count = amount;
  6655. for (i = 0; i < amount; i += get_count)
  6656. {
  6657. // if not pet egg
  6658. if (!pet_create_egg(sd, nameid))
  6659. {
  6660. unsigned char flag = 0;
  6661. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6662. {
  6663. clif_additem(sd, 0, 0, flag);
  6664. if( pc_candrop(sd,&item_tmp) )
  6665. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6666. }
  6667. }
  6668. }
  6669. }
  6670. return SCRIPT_CMD_SUCCESS;
  6671. }
  6672. /** Gives rental item to player
  6673. * rentitem <item id>,<seconds>{,<account_id>}
  6674. * rentitem "<item name>",<seconds>{,<account_id>}
  6675. */
  6676. BUILDIN_FUNC(rentitem) {
  6677. struct map_session_data *sd;
  6678. struct item it;
  6679. int seconds;
  6680. t_itemid nameid = 0;
  6681. unsigned char flag = 0;
  6682. if (!script_accid2sd(4,sd))
  6683. return SCRIPT_CMD_FAILURE;
  6684. if( script_isstring(st, 2) )
  6685. {
  6686. const char *name = script_getstr(st, 2);
  6687. struct item_data *itd = itemdb_searchname(name);
  6688. if( itd == NULL )
  6689. {
  6690. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6691. return SCRIPT_CMD_FAILURE;
  6692. }
  6693. nameid = itd->nameid;
  6694. }
  6695. else
  6696. {
  6697. nameid = script_getnum(st, 2);
  6698. if( nameid == 0 || !itemdb_exists(nameid) )
  6699. {
  6700. ShowError("buildin_rentitem: Nonexistant item %u requested.\n", nameid);
  6701. return SCRIPT_CMD_FAILURE;
  6702. }
  6703. }
  6704. seconds = script_getnum(st,3);
  6705. memset(&it, 0, sizeof(it));
  6706. it.nameid = nameid;
  6707. it.identify = 1;
  6708. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6709. it.bound = BOUND_NONE;
  6710. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6711. {
  6712. clif_additem(sd, 0, 0, flag);
  6713. return SCRIPT_CMD_FAILURE;
  6714. }
  6715. return SCRIPT_CMD_SUCCESS;
  6716. }
  6717. /**
  6718. * Gives rental item to player with advanced option
  6719. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6720. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6721. *
  6722. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6723. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6724. */
  6725. BUILDIN_FUNC(rentitem2) {
  6726. struct map_session_data *sd;
  6727. struct item it;
  6728. struct item_data *id;
  6729. int seconds;
  6730. t_itemid nameid = 0;
  6731. unsigned char flag = 0;
  6732. int iden,ref,attr;
  6733. t_itemid c1,c2,c3,c4;
  6734. const char *funcname = script_getfuncname(st);
  6735. if (funcname[strlen(funcname)-1] == '3') {
  6736. if (!script_accid2sd(14,sd))
  6737. return SCRIPT_CMD_FAILURE;
  6738. } else if (!script_accid2sd(11,sd))
  6739. return SCRIPT_CMD_FAILURE;
  6740. if( script_isstring(st, 2) ) {
  6741. const char *name = script_getstr(st, 2);
  6742. id = itemdb_searchname(name);
  6743. if( id == NULL ) {
  6744. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6745. return SCRIPT_CMD_FAILURE;
  6746. }
  6747. nameid = id->nameid;
  6748. } else {
  6749. nameid = script_getnum(st, 2);
  6750. if( !(id = itemdb_search(nameid))) {
  6751. ShowError("buildin_rentitem2: Nonexistant item %u requested.\n", nameid);
  6752. return SCRIPT_CMD_FAILURE;
  6753. }
  6754. }
  6755. seconds = script_getnum(st,3);
  6756. iden = script_getnum(st,4);
  6757. ref = script_getnum(st,5);
  6758. attr = script_getnum(st,6);
  6759. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6760. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6761. }
  6762. else if (id->type==IT_PETEGG) {
  6763. iden = 1;
  6764. ref = 0;
  6765. }
  6766. else {
  6767. iden = 1;
  6768. ref = attr = 0;
  6769. }
  6770. c1 = script_getnum(st,7);
  6771. c2 = script_getnum(st,8);
  6772. c3 = script_getnum(st,9);
  6773. c4 = script_getnum(st,10);
  6774. memset(&it, 0, sizeof(it));
  6775. it.nameid = nameid;
  6776. it.identify = iden;
  6777. it.refine = ref;
  6778. it.attribute = attr;
  6779. it.card[0] = c1;
  6780. it.card[1] = c2;
  6781. it.card[2] = c3;
  6782. it.card[3] = c4;
  6783. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6784. it.bound = BOUND_NONE;
  6785. if (funcname[strlen(funcname)-1] == '3') {
  6786. int res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6787. if (res != SCRIPT_CMD_SUCCESS)
  6788. return res;
  6789. }
  6790. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6791. clif_additem(sd, 0, 0, flag);
  6792. return SCRIPT_CMD_FAILURE;
  6793. }
  6794. return SCRIPT_CMD_SUCCESS;
  6795. }
  6796. /*==========================================
  6797. * gets an item with someone's name inscribed [Skotlex]
  6798. * getinscribeditem item_num, character_name
  6799. * Returned Qty is always 1, only works on equip-able
  6800. * equipment
  6801. *------------------------------------------*/
  6802. BUILDIN_FUNC(getnameditem)
  6803. {
  6804. t_itemid nameid;
  6805. struct item item_tmp;
  6806. TBL_PC *sd, *tsd;
  6807. if (!script_rid2sd(sd))
  6808. { //Player not attached!
  6809. script_pushint(st,0);
  6810. return SCRIPT_CMD_SUCCESS;
  6811. }
  6812. if( script_isstring(st, 2) ){
  6813. const char *name = script_getstr(st, 2);
  6814. struct item_data *item_data = itemdb_searchname(name);
  6815. if( item_data == NULL)
  6816. { //Failed
  6817. script_pushint(st,0);
  6818. return SCRIPT_CMD_SUCCESS;
  6819. }
  6820. nameid = item_data->nameid;
  6821. }else
  6822. nameid = script_getnum(st, 2);
  6823. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6824. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6825. script_pushint(st,0);
  6826. return SCRIPT_CMD_SUCCESS;
  6827. }
  6828. if( script_isstring(st, 3) ) //Char Name
  6829. tsd = map_nick2sd(script_getstr(st, 3),false);
  6830. else //Char Id was given
  6831. tsd = map_charid2sd(script_getnum(st, 3));
  6832. if( tsd == NULL )
  6833. { //Failed
  6834. script_pushint(st,0);
  6835. return SCRIPT_CMD_SUCCESS;
  6836. }
  6837. memset(&item_tmp,0,sizeof(item_tmp));
  6838. item_tmp.nameid=nameid;
  6839. item_tmp.amount=1;
  6840. item_tmp.identify=1;
  6841. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6842. item_tmp.card[2]=GetWord(tsd->status.char_id,0);
  6843. item_tmp.card[3]=GetWord(tsd->status.char_id,1);
  6844. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6845. script_pushint(st,0);
  6846. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6847. }
  6848. script_pushint(st,1);
  6849. return SCRIPT_CMD_SUCCESS;
  6850. }
  6851. /*==========================================
  6852. * gets a random item ID from an item group [Skotlex]
  6853. * groupranditem <group_num>{,<sub_group>};
  6854. *------------------------------------------*/
  6855. BUILDIN_FUNC(grouprandomitem) {
  6856. struct s_item_group_entry *entry = NULL;
  6857. int sub_group = 1;
  6858. FETCH(3, sub_group);
  6859. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6860. if (!entry) {
  6861. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6862. script_pushint(st,UNKNOWN_ITEM_ID);
  6863. return SCRIPT_CMD_FAILURE;
  6864. }
  6865. script_pushint(st,entry->nameid);
  6866. return SCRIPT_CMD_SUCCESS;
  6867. }
  6868. /**
  6869. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6870. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6871. */
  6872. BUILDIN_FUNC(makeitem) {
  6873. t_itemid nameid;
  6874. uint16 amount, flag = 0, x, y;
  6875. const char *mapname;
  6876. int m;
  6877. struct item item_tmp;
  6878. if( script_isstring(st, 2) ){
  6879. const char *name = script_getstr(st, 2);
  6880. struct item_data *item_data = itemdb_searchname(name);
  6881. if( item_data )
  6882. nameid = item_data->nameid;
  6883. else{
  6884. ShowError( "buildin_makeitem: Unknown item %s\n", name );
  6885. return SCRIPT_CMD_FAILURE;
  6886. }
  6887. }
  6888. else {
  6889. int32 val = script_getnum( st, 2 );
  6890. if( val < 0 ){
  6891. flag = 1;
  6892. nameid = (t_itemid)( -1 * val );
  6893. }else{
  6894. nameid = (t_itemid)val;
  6895. }
  6896. if( !itemdb_exists( nameid ) ){
  6897. ShowError( "buildin_makeitem: Unknown item id %u\n", nameid );
  6898. return SCRIPT_CMD_FAILURE;
  6899. }
  6900. }
  6901. amount = script_getnum(st,3);
  6902. mapname = script_getstr(st,4);
  6903. x = script_getnum(st,5);
  6904. y = script_getnum(st,6);
  6905. if(strcmp(mapname,"this")==0) {
  6906. TBL_PC *sd;
  6907. if (!script_rid2sd(sd))
  6908. return SCRIPT_CMD_SUCCESS; //Failed...
  6909. m = sd->bl.m;
  6910. } else
  6911. m = map_mapname2mapid(mapname);
  6912. memset(&item_tmp,0,sizeof(item_tmp));
  6913. item_tmp.nameid = nameid;
  6914. if (!flag)
  6915. item_tmp.identify = 1;
  6916. else
  6917. item_tmp.identify = itemdb_isidentified(nameid);
  6918. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6919. return SCRIPT_CMD_SUCCESS;
  6920. }
  6921. /**
  6922. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6923. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6924. *
  6925. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6926. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6927. */
  6928. BUILDIN_FUNC(makeitem2) {
  6929. t_itemid nameid;
  6930. uint16 amount, x, y;
  6931. const char *mapname;
  6932. int m;
  6933. struct item item_tmp;
  6934. struct item_data *id;
  6935. const char *funcname = script_getfuncname(st);
  6936. if( script_isstring( st, 2 ) ){
  6937. const char *name = script_getstr( st, 2 );
  6938. struct item_data *item_data = itemdb_searchname( name );
  6939. if( item_data ){
  6940. nameid = item_data->nameid;
  6941. }else{
  6942. ShowError( "buildin_%s: Unknown item %s\n", funcname, name );
  6943. return SCRIPT_CMD_FAILURE;
  6944. }
  6945. }else{
  6946. nameid = (t_itemid)script_getnum( st, 2 );
  6947. if( !itemdb_exists( nameid ) ){
  6948. ShowError( "buildin_%s: Unknown item id %u\n", funcname, nameid );
  6949. return SCRIPT_CMD_FAILURE;
  6950. }
  6951. }
  6952. amount = script_getnum(st,3);
  6953. mapname = script_getstr(st,4);
  6954. x = script_getnum(st,5);
  6955. y = script_getnum(st,6);
  6956. if (strcmp(mapname,"this")==0) {
  6957. TBL_PC *sd;
  6958. if (!script_rid2sd(sd))
  6959. return SCRIPT_CMD_SUCCESS; //Failed...
  6960. m = sd->bl.m;
  6961. }
  6962. else
  6963. m = map_mapname2mapid(mapname);
  6964. if ((id = itemdb_search(nameid))) {
  6965. char iden, ref, attr;
  6966. memset(&item_tmp,0,sizeof(item_tmp));
  6967. item_tmp.nameid = nameid;
  6968. iden = (char)script_getnum(st,7);
  6969. ref = (char)script_getnum(st,8);
  6970. attr = (char)script_getnum(st,9);
  6971. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6972. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6973. }
  6974. else if (id->type==IT_PETEGG) {
  6975. iden = 1;
  6976. ref = 0;
  6977. }
  6978. else {
  6979. iden = 1;
  6980. ref = attr = 0;
  6981. }
  6982. item_tmp.identify = iden;
  6983. item_tmp.refine = ref;
  6984. item_tmp.attribute = attr;
  6985. item_tmp.card[0] = script_getnum(st,10);
  6986. item_tmp.card[1] = script_getnum(st,11);
  6987. item_tmp.card[2] = script_getnum(st,12);
  6988. item_tmp.card[3] = script_getnum(st,13);
  6989. if (funcname[strlen(funcname)-1] == '3') {
  6990. int res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, 14);
  6991. if (res != SCRIPT_CMD_SUCCESS)
  6992. return res;
  6993. }
  6994. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6995. }
  6996. else
  6997. return SCRIPT_CMD_FAILURE;
  6998. return SCRIPT_CMD_SUCCESS;
  6999. }
  7000. /// Counts / deletes the current item given by idx.
  7001. /// Used by buildin_delitem_search
  7002. /// Relies on all input data being already fully valid.
  7003. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  7004. {
  7005. int delamount;
  7006. struct item *itm = NULL;
  7007. struct s_storage *gstor = NULL;
  7008. switch(loc) {
  7009. case TABLE_CART:
  7010. itm = &sd->cart.u.items_cart[idx];
  7011. break;
  7012. case TABLE_STORAGE:
  7013. itm = &sd->storage.u.items_storage[idx];
  7014. break;
  7015. case TABLE_GUILD_STORAGE:
  7016. {
  7017. gstor = guild2storage2(sd->status.guild_id);
  7018. itm = &gstor->u.items_guild[idx];
  7019. }
  7020. break;
  7021. default: // TABLE_INVENTORY
  7022. itm = &sd->inventory.u.items_inventory[idx];
  7023. break;
  7024. }
  7025. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7026. if( delete_items )
  7027. {
  7028. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7029. {// delete associated pet
  7030. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7031. }
  7032. switch(loc) {
  7033. case TABLE_CART:
  7034. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7035. break;
  7036. case TABLE_STORAGE:
  7037. storage_delitem(sd,&sd->storage,idx,delamount);
  7038. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7039. break;
  7040. case TABLE_GUILD_STORAGE:
  7041. gstor->lock = true;
  7042. storage_guild_delitem(sd, gstor, idx, delamount);
  7043. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7044. storage_guild_storageclose(sd);
  7045. gstor->lock = false;
  7046. break;
  7047. default: // TABLE_INVENTORY
  7048. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7049. break;
  7050. }
  7051. }
  7052. amount[0]-= delamount;
  7053. }
  7054. /// Searches for item(s) and checks, if there is enough of them.
  7055. /// Used by delitem and delitem2
  7056. /// Relies on all input data being already fully valid.
  7057. /// @param exact_match will also match item by specified attributes
  7058. /// 0x0: Only item id
  7059. /// 0x1: identify, attributes, cards
  7060. /// 0x2: random option
  7061. /// @return true when all items could be deleted, false when there were not enough items to delete
  7062. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7063. {
  7064. bool delete_items = false;
  7065. int i, amount, size;
  7066. struct item *items;
  7067. // prefer always non-equipped items
  7068. it->equip = 0;
  7069. // when searching for nameid only, prefer additionally
  7070. if( !exact_match )
  7071. {
  7072. // non-refined items
  7073. it->refine = 0;
  7074. // card-less items
  7075. memset(it->card, 0, sizeof(it->card));
  7076. }
  7077. switch(loc) {
  7078. case TABLE_CART:
  7079. size = MAX_CART;
  7080. items = sd->cart.u.items_cart;
  7081. break;
  7082. case TABLE_STORAGE:
  7083. size = MAX_STORAGE;
  7084. items = sd->storage.u.items_storage;
  7085. break;
  7086. case TABLE_GUILD_STORAGE:
  7087. {
  7088. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7089. size = MAX_GUILD_STORAGE;
  7090. items = gstor->u.items_guild;
  7091. }
  7092. break;
  7093. default: // TABLE_INVENTORY
  7094. size = MAX_INVENTORY;
  7095. items = sd->inventory.u.items_inventory;
  7096. break;
  7097. }
  7098. for(;;)
  7099. {
  7100. unsigned short important = 0;
  7101. amount = it->amount;
  7102. // 1st pass -- less important items / exact match
  7103. for( i = 0; amount && i < size; i++ )
  7104. {
  7105. struct item *itm = NULL;
  7106. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7107. {// wrong/invalid item
  7108. continue;
  7109. }
  7110. if( itm->equip != it->equip || itm->refine != it->refine )
  7111. {// not matching attributes
  7112. important++;
  7113. continue;
  7114. }
  7115. if( exact_match )
  7116. {
  7117. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7118. {// not matching exact attributes
  7119. continue;
  7120. }
  7121. if (exact_match&0x2) {
  7122. uint8 j;
  7123. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7124. 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)
  7125. break;
  7126. }
  7127. if (j != MAX_ITEM_RDM_OPT)
  7128. continue;
  7129. }
  7130. }
  7131. else
  7132. {
  7133. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7134. {
  7135. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7136. {// pet which cannot be deleted
  7137. continue;
  7138. }
  7139. }
  7140. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7141. {// named/carded item
  7142. important++;
  7143. continue;
  7144. }
  7145. }
  7146. // count / delete item
  7147. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7148. }
  7149. // 2nd pass -- any matching item
  7150. if( amount == 0 || important == 0 )
  7151. {// either everything was already consumed or no items were skipped
  7152. ;
  7153. }
  7154. else for( i = 0; amount && i < size; i++ )
  7155. {
  7156. struct item *itm = NULL;
  7157. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7158. {// wrong/invalid item
  7159. continue;
  7160. }
  7161. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7162. {// pet which cannot be deleted
  7163. continue;
  7164. }
  7165. if( exact_match )
  7166. {
  7167. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7168. {// not matching attributes
  7169. continue;
  7170. }
  7171. if (exact_match&0x2) {
  7172. uint8 j;
  7173. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7174. 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)
  7175. break;
  7176. }
  7177. if (j != MAX_ITEM_RDM_OPT)
  7178. continue;
  7179. }
  7180. }
  7181. // count / delete item
  7182. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7183. }
  7184. if( amount )
  7185. {// not enough items
  7186. return false;
  7187. }
  7188. else if( delete_items )
  7189. {// we are done with the work
  7190. return true;
  7191. }
  7192. else
  7193. {// get rid of the items now
  7194. delete_items = true;
  7195. }
  7196. }
  7197. }
  7198. /// Deletes items from the target/attached player.
  7199. /// Prioritizes ordinary items.
  7200. ///
  7201. /// delitem <item id>,<amount>{,<account id>}
  7202. /// delitem "<item name>",<amount>{,<account id>}
  7203. /// cartdelitem <item id>,<amount>{,<account id>}
  7204. /// cartdelitem "<item name>",<amount>{,<account id>}
  7205. /// storagedelitem <item id>,<amount>{,<account id>}
  7206. /// storagedelitem "<item name>",<amount>{,<account id>}
  7207. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7208. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7209. BUILDIN_FUNC(delitem)
  7210. {
  7211. TBL_PC *sd;
  7212. struct item it;
  7213. uint8 loc = 0;
  7214. char* command = (char*)script_getfuncname(st);
  7215. if(!strncmp(command, "cart", 4))
  7216. loc = TABLE_CART;
  7217. else if(!strncmp(command, "storage", 7))
  7218. loc = TABLE_STORAGE;
  7219. else if(!strncmp(command, "guildstorage", 12))
  7220. loc = TABLE_GUILD_STORAGE;
  7221. if( !script_accid2sd(4,sd) ){
  7222. // In any case cancel script execution
  7223. st->state = END;
  7224. return SCRIPT_CMD_SUCCESS;
  7225. }
  7226. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7227. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7228. script_pushint(st, -1);
  7229. return SCRIPT_CMD_FAILURE;
  7230. }
  7231. if (loc == TABLE_GUILD_STORAGE) {
  7232. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7233. if (gstor == NULL || sd->state.storage_flag) {
  7234. script_pushint(st, -1);
  7235. return SCRIPT_CMD_FAILURE;
  7236. }
  7237. }
  7238. if( script_isstring(st, 2) )
  7239. {
  7240. const char* item_name = script_getstr(st, 2);
  7241. struct item_data* id = itemdb_searchname(item_name);
  7242. if( id == NULL )
  7243. {
  7244. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7245. st->state = END;
  7246. return SCRIPT_CMD_FAILURE;
  7247. }
  7248. it.nameid = id->nameid;// "<item name>"
  7249. }
  7250. else
  7251. {
  7252. it.nameid = script_getnum(st, 2);// <item id>
  7253. if( !itemdb_exists( it.nameid ) )
  7254. {
  7255. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7256. st->state = END;
  7257. return SCRIPT_CMD_FAILURE;
  7258. }
  7259. }
  7260. it.amount=script_getnum(st,3);
  7261. if( it.amount <= 0 )
  7262. return SCRIPT_CMD_SUCCESS;// nothing to do
  7263. if( buildin_delitem_search(sd, &it, 0, loc) )
  7264. {// success
  7265. return SCRIPT_CMD_SUCCESS;
  7266. }
  7267. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7268. st->state = END;
  7269. st->mes_active = 0;
  7270. clif_scriptclose(sd, st->oid);
  7271. return SCRIPT_CMD_FAILURE;
  7272. }
  7273. /// Deletes items from the target/attached player.
  7274. ///
  7275. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7276. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7277. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7278. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7279. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7280. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7281. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7282. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7283. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7284. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7285. BUILDIN_FUNC(delitem2)
  7286. {
  7287. TBL_PC *sd;
  7288. struct item it;
  7289. uint8 loc = 0;
  7290. char* command = (char*)script_getfuncname(st);
  7291. int aid_pos = 11;
  7292. uint8 flag = 0x1;
  7293. if(!strncmp(command, "cart", 4))
  7294. loc = TABLE_CART;
  7295. else if(!strncmp(command, "storage", 7))
  7296. loc = TABLE_STORAGE;
  7297. else if(!strncmp(command, "guildstorage", 12))
  7298. loc = TABLE_GUILD_STORAGE;
  7299. if (command[strlen(command)-1] == '3')
  7300. aid_pos = 14;
  7301. if( !script_accid2sd(aid_pos,sd) ){
  7302. // In any case cancel script execution
  7303. st->state = END;
  7304. return SCRIPT_CMD_SUCCESS;
  7305. }
  7306. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7307. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7308. script_pushint(st,-1);
  7309. return SCRIPT_CMD_FAILURE;
  7310. }
  7311. if (loc == TABLE_GUILD_STORAGE) {
  7312. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7313. if (gstor == NULL || sd->state.storage_flag) {
  7314. script_pushint(st, -1);
  7315. return SCRIPT_CMD_FAILURE;
  7316. }
  7317. }
  7318. memset(&it, 0, sizeof(it));
  7319. if( script_isstring(st, 2) )
  7320. {
  7321. const char* item_name = script_getstr(st, 2);
  7322. struct item_data* id = itemdb_searchname(item_name);
  7323. if( id == NULL )
  7324. {
  7325. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7326. st->state = END;
  7327. return SCRIPT_CMD_FAILURE;
  7328. }
  7329. it.nameid = id->nameid;// "<item name>"
  7330. }
  7331. else
  7332. {
  7333. it.nameid = script_getnum(st, 2);// <item id>
  7334. if( !itemdb_exists( it.nameid ) )
  7335. {
  7336. ShowError("buildin_%s: unknown item \"%u\".\n", command, it.nameid);
  7337. st->state = END;
  7338. return SCRIPT_CMD_FAILURE;
  7339. }
  7340. }
  7341. it.amount=script_getnum(st,3);
  7342. it.identify=script_getnum(st,4);
  7343. it.refine=script_getnum(st,5);
  7344. it.attribute=script_getnum(st,6);
  7345. it.card[0]=script_getnum(st,7);
  7346. it.card[1]=script_getnum(st,8);
  7347. it.card[2]=script_getnum(st,9);
  7348. it.card[3]=script_getnum(st,10);
  7349. if (command[strlen(command)-1] == '3') {
  7350. int res = script_getitem_randomoption(st, sd, &it, command, 11);
  7351. if (res != SCRIPT_CMD_SUCCESS)
  7352. return SCRIPT_CMD_FAILURE;
  7353. flag |= 0x2;
  7354. }
  7355. if( it.amount <= 0 )
  7356. return SCRIPT_CMD_SUCCESS;// nothing to do
  7357. if( buildin_delitem_search(sd, &it, flag, loc) )
  7358. {// success
  7359. return SCRIPT_CMD_SUCCESS;
  7360. }
  7361. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%u).\n", command, it.amount, sd->status.account_id, it.nameid);
  7362. st->state = END;
  7363. st->mes_active = 0;
  7364. clif_scriptclose(sd, st->oid);
  7365. return SCRIPT_CMD_FAILURE;
  7366. }
  7367. /*==========================================
  7368. * Enables/Disables use of items while in an NPC [Skotlex]
  7369. *------------------------------------------*/
  7370. BUILDIN_FUNC(enableitemuse)
  7371. {
  7372. TBL_PC *sd;
  7373. if (script_rid2sd(sd))
  7374. st->npc_item_flag = sd->npc_item_flag = 1;
  7375. return SCRIPT_CMD_SUCCESS;
  7376. }
  7377. BUILDIN_FUNC(disableitemuse)
  7378. {
  7379. TBL_PC *sd;
  7380. if (script_rid2sd(sd))
  7381. st->npc_item_flag = sd->npc_item_flag = 0;
  7382. return SCRIPT_CMD_SUCCESS;
  7383. }
  7384. /*==========================================
  7385. * Returns a character's specified stat.
  7386. * Check pc_readparam for available options.
  7387. * readparam <param>{,"<nick>"}
  7388. * readparam <param>{,<char_id>}
  7389. *------------------------------------------*/
  7390. BUILDIN_FUNC(readparam)
  7391. {
  7392. int value;
  7393. struct script_data *data = script_getdata(st, 2);
  7394. TBL_PC *sd = NULL;
  7395. if( script_hasdata(st, 3) ){
  7396. if (script_isint(st, 3))
  7397. script_charid2sd(3, sd);
  7398. else
  7399. script_nick2sd(3, sd);
  7400. }else{
  7401. script_rid2sd(sd);
  7402. }
  7403. if( !sd ){
  7404. script_pushint(st, -1);
  7405. return SCRIPT_CMD_FAILURE;
  7406. }
  7407. // If you use a parameter, return the value behind it
  7408. if( reference_toparam(data) ){
  7409. get_val_(st, data, sd);
  7410. value = (int)data->u.num;
  7411. }else{
  7412. value = static_cast<int>(pc_readparam(sd,script_getnum(st, 2)));
  7413. }
  7414. script_pushint(st,value);
  7415. return SCRIPT_CMD_SUCCESS;
  7416. }
  7417. /*==========================================
  7418. * Return charid identification
  7419. * return by @num :
  7420. * 0 : char_id
  7421. * 1 : party_id
  7422. * 2 : guild_id
  7423. * 3 : account_id
  7424. * 4 : bg_id
  7425. * 5 : clan_id
  7426. *------------------------------------------*/
  7427. BUILDIN_FUNC(getcharid)
  7428. {
  7429. int num;
  7430. TBL_PC *sd;
  7431. num = script_getnum(st,2);
  7432. if( !script_nick2sd(3,sd) ){
  7433. script_pushint(st,0); //return 0, according docs
  7434. return SCRIPT_CMD_SUCCESS;
  7435. }
  7436. switch( num ) {
  7437. case 0: script_pushint(st,sd->status.char_id); break;
  7438. case 1: script_pushint(st,sd->status.party_id); break;
  7439. case 2: script_pushint(st,sd->status.guild_id); break;
  7440. case 3: script_pushint(st,sd->status.account_id); break;
  7441. case 4: script_pushint(st,sd->bg_id); break;
  7442. case 5: script_pushint(st,sd->status.clan_id); break;
  7443. default:
  7444. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7445. script_pushint(st,0);
  7446. break;
  7447. }
  7448. return SCRIPT_CMD_SUCCESS;
  7449. }
  7450. /*==========================================
  7451. * returns the GID of an NPC
  7452. *------------------------------------------*/
  7453. BUILDIN_FUNC(getnpcid)
  7454. {
  7455. int num = script_getnum(st,2);
  7456. struct npc_data* nd = NULL;
  7457. if( script_hasdata(st,3) )
  7458. {// unique npc name
  7459. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7460. {
  7461. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7462. script_pushint(st,0);
  7463. return SCRIPT_CMD_FAILURE;
  7464. }
  7465. }
  7466. switch (num) {
  7467. case 0:
  7468. script_pushint(st,nd ? nd->bl.id : st->oid);
  7469. break;
  7470. default:
  7471. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7472. script_pushint(st,0);
  7473. return SCRIPT_CMD_FAILURE;
  7474. }
  7475. return SCRIPT_CMD_SUCCESS;
  7476. }
  7477. /*==========================================
  7478. * Return the name of the party_id
  7479. * null if not found
  7480. *------------------------------------------*/
  7481. BUILDIN_FUNC(getpartyname)
  7482. {
  7483. int party_id;
  7484. struct party_data* p;
  7485. party_id = script_getnum(st,2);
  7486. if( ( p = party_search(party_id) ) != NULL )
  7487. {
  7488. script_pushstrcopy(st,p->party.name);
  7489. }
  7490. else
  7491. {
  7492. script_pushconststr(st,"null");
  7493. }
  7494. return SCRIPT_CMD_SUCCESS;
  7495. }
  7496. /*==========================================
  7497. * Get the information of the members of a party by type
  7498. * @party_id, @type
  7499. * return by @type :
  7500. * - : nom des membres
  7501. * 1 : char_id des membres
  7502. * 2 : account_id des membres
  7503. *------------------------------------------*/
  7504. BUILDIN_FUNC(getpartymember)
  7505. {
  7506. struct party_data *p;
  7507. uint8 j = 0;
  7508. p = party_search(script_getnum(st,2));
  7509. if (p != NULL) {
  7510. uint8 i, type = 0;
  7511. struct script_data *data = NULL;
  7512. char *varname = NULL;
  7513. if (script_hasdata(st,3))
  7514. type = script_getnum(st,3);
  7515. if (script_hasdata(st,4)) {
  7516. data = script_getdata(st, 4);
  7517. if (!data_isreference(data)) {
  7518. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7519. return SCRIPT_CMD_FAILURE;
  7520. }
  7521. varname = reference_getname(data);
  7522. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7523. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7524. return SCRIPT_CMD_FAILURE;
  7525. }
  7526. if (not_server_variable(*varname)) {
  7527. struct map_session_data *sd;
  7528. if (!script_rid2sd(sd)) {
  7529. ShowError("buildin_getpartymember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  7530. return SCRIPT_CMD_FAILURE;
  7531. }
  7532. }
  7533. }
  7534. for (i = 0; i < MAX_PARTY; i++) {
  7535. if (p->party.member[i].account_id) {
  7536. switch (type) {
  7537. case 2:
  7538. if (data)
  7539. setd_sub_num( st, NULL, varname, j, p->party.member[i].account_id, data->ref );
  7540. else
  7541. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7542. break;
  7543. case 1:
  7544. if (data)
  7545. setd_sub_num( st, NULL, varname, j, p->party.member[i].char_id, data->ref );
  7546. else
  7547. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7548. break;
  7549. default:
  7550. if (data)
  7551. setd_sub_str( st, NULL, varname, j, p->party.member[i].name, data->ref );
  7552. else
  7553. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7554. break;
  7555. }
  7556. j++;
  7557. }
  7558. }
  7559. }
  7560. mapreg_setreg(add_str("$@partymembercount"),j);
  7561. script_pushint(st, j);
  7562. return SCRIPT_CMD_SUCCESS;
  7563. }
  7564. /*==========================================
  7565. * Retrieves party leader. if flag is specified,
  7566. * return some of the leader data. Otherwise, return name.
  7567. *------------------------------------------*/
  7568. BUILDIN_FUNC(getpartyleader)
  7569. {
  7570. int party_id, type = 0, i=0;
  7571. struct party_data *p;
  7572. party_id=script_getnum(st,2);
  7573. if( script_hasdata(st,3) )
  7574. type=script_getnum(st,3);
  7575. p=party_search(party_id);
  7576. if (p) //Search leader
  7577. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7578. if (!p || i == MAX_PARTY) { //leader not found
  7579. if (type)
  7580. script_pushint(st,-1);
  7581. else
  7582. script_pushconststr(st,"null");
  7583. return SCRIPT_CMD_SUCCESS;
  7584. }
  7585. switch (type) {
  7586. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7587. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7588. case 3: script_pushint(st,p->party.member[i].class_); break;
  7589. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7590. case 5: script_pushint(st,p->party.member[i].lv); break;
  7591. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7592. }
  7593. return SCRIPT_CMD_SUCCESS;
  7594. }
  7595. /*==========================================
  7596. * Return the name of the @guild_id
  7597. * null if not found
  7598. *------------------------------------------*/
  7599. BUILDIN_FUNC(getguildname)
  7600. {
  7601. int guild_id;
  7602. struct guild* g;
  7603. guild_id = script_getnum(st,2);
  7604. if( ( g = guild_search(guild_id) ) != NULL )
  7605. script_pushstrcopy(st,g->name);
  7606. else
  7607. script_pushconststr(st,"null");
  7608. return SCRIPT_CMD_SUCCESS;
  7609. }
  7610. /*==========================================
  7611. * Return the name of the guild master of @guild_id
  7612. * null if not found
  7613. *------------------------------------------*/
  7614. BUILDIN_FUNC(getguildmaster)
  7615. {
  7616. int guild_id;
  7617. struct guild* g;
  7618. guild_id = script_getnum(st,2);
  7619. if( ( g = guild_search(guild_id) ) != NULL )
  7620. script_pushstrcopy(st,g->member[0].name);
  7621. else
  7622. script_pushconststr(st,"null");
  7623. return SCRIPT_CMD_SUCCESS;
  7624. }
  7625. BUILDIN_FUNC(getguildmasterid)
  7626. {
  7627. int guild_id;
  7628. struct guild* g;
  7629. guild_id = script_getnum(st,2);
  7630. if( ( g = guild_search(guild_id) ) != NULL )
  7631. script_pushint(st,g->member[0].char_id);
  7632. else
  7633. script_pushint(st,0);
  7634. return SCRIPT_CMD_SUCCESS;
  7635. }
  7636. /*==========================================
  7637. * Get char string information by type :
  7638. * Return by @type :
  7639. * 0 : char_name
  7640. * 1 : party_name or ""
  7641. * 2 : guild_name or ""
  7642. * 3 : map_name
  7643. * - : ""
  7644. * strcharinfo(<type>{,<char_id>})
  7645. *------------------------------------------*/
  7646. BUILDIN_FUNC(strcharinfo)
  7647. {
  7648. TBL_PC *sd;
  7649. int num;
  7650. struct guild* g;
  7651. struct party_data* p;
  7652. if (!script_charid2sd(3,sd)) {
  7653. script_pushconststr(st,"");
  7654. return SCRIPT_CMD_FAILURE;
  7655. }
  7656. num=script_getnum(st,2);
  7657. switch(num){
  7658. case 0:
  7659. script_pushstrcopy(st,sd->status.name);
  7660. break;
  7661. case 1:
  7662. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7663. script_pushstrcopy(st,p->party.name);
  7664. } else {
  7665. script_pushconststr(st,"");
  7666. }
  7667. break;
  7668. case 2:
  7669. if( ( g = sd->guild ) != NULL ) {
  7670. script_pushstrcopy(st,g->name);
  7671. } else {
  7672. script_pushconststr(st,"");
  7673. }
  7674. break;
  7675. case 3:
  7676. script_pushconststr(st,map_getmapdata(sd->bl.m)->name);
  7677. break;
  7678. default:
  7679. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7680. script_pushconststr(st,"");
  7681. break;
  7682. }
  7683. return SCRIPT_CMD_SUCCESS;
  7684. }
  7685. /*==========================================
  7686. * Get npc string information by type
  7687. * return by @type:
  7688. * 0 : name
  7689. * 1 : str#
  7690. * 2 : #str
  7691. * 3 : ::str
  7692. * 4 : map name
  7693. *------------------------------------------*/
  7694. BUILDIN_FUNC(strnpcinfo)
  7695. {
  7696. TBL_NPC* nd;
  7697. int num;
  7698. char *buf,*name=NULL;
  7699. nd = map_id2nd(st->oid);
  7700. if (!nd) {
  7701. script_pushconststr(st, "");
  7702. return SCRIPT_CMD_SUCCESS;
  7703. }
  7704. num = script_getnum(st,2);
  7705. switch(num){
  7706. case 0: // display name
  7707. name = aStrdup(nd->name);
  7708. break;
  7709. case 1: // visible part of display name
  7710. if((buf = strchr(nd->name,'#')) != NULL)
  7711. {
  7712. name = aStrdup(nd->name);
  7713. name[buf - nd->name] = 0;
  7714. } else // Return the name, there is no '#' present
  7715. name = aStrdup(nd->name);
  7716. break;
  7717. case 2: // # fragment
  7718. if((buf = strchr(nd->name,'#')) != NULL)
  7719. name = aStrdup(buf+1);
  7720. break;
  7721. case 3: // unique name
  7722. name = aStrdup(nd->exname);
  7723. break;
  7724. case 4: // map name
  7725. if (nd->bl.m >= 0)
  7726. name = aStrdup(map_getmapdata(nd->bl.m)->name);
  7727. break;
  7728. }
  7729. if(name)
  7730. script_pushstr(st, name);
  7731. else
  7732. script_pushconststr(st, "");
  7733. return SCRIPT_CMD_SUCCESS;
  7734. }
  7735. /**
  7736. * getequipid({<equipment slot>,<char_id>})
  7737. **/
  7738. BUILDIN_FUNC(getequipid)
  7739. {
  7740. int i, num;
  7741. TBL_PC* sd;
  7742. if (!script_charid2sd(3, sd)) {
  7743. script_pushint(st,-1);
  7744. return SCRIPT_CMD_FAILURE;
  7745. }
  7746. if (script_hasdata(st, 2))
  7747. num = script_getnum(st, 2);
  7748. else
  7749. num = EQI_COMPOUND_ON;
  7750. if (num == EQI_COMPOUND_ON)
  7751. i = current_equip_item_index;
  7752. else if (equip_index_check(num)) // get inventory position of item
  7753. i = pc_checkequip(sd, equip_bitmask[num]);
  7754. else {
  7755. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7756. script_pushint(st,-1);
  7757. return SCRIPT_CMD_FAILURE;
  7758. }
  7759. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7760. script_pushint(st, sd->inventory_data[i]->nameid);
  7761. else
  7762. script_pushint(st, -1);
  7763. return SCRIPT_CMD_SUCCESS;
  7764. }
  7765. /*==========================================
  7766. * getequipuniqueid(<equipment slot>{,<char_id>})
  7767. *------------------------------------------*/
  7768. BUILDIN_FUNC(getequipuniqueid)
  7769. {
  7770. int i, num;
  7771. TBL_PC* sd;
  7772. struct item* item;
  7773. if (!script_charid2sd(3, sd)) {
  7774. script_pushconststr(st, "");
  7775. return SCRIPT_CMD_FAILURE;
  7776. }
  7777. num = script_getnum(st,2);
  7778. if ( !equip_index_check(num) ) {
  7779. script_pushconststr(st, "");
  7780. return SCRIPT_CMD_FAILURE;
  7781. }
  7782. // get inventory position of item
  7783. i = pc_checkequip(sd,equip_bitmask[num]);
  7784. if (i < 0) {
  7785. script_pushconststr(st, "");
  7786. return SCRIPT_CMD_FAILURE;
  7787. }
  7788. item = &sd->inventory.u.items_inventory[i];
  7789. if (item != 0) {
  7790. int maxlen = 256;
  7791. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7792. memset(buf, 0, maxlen);
  7793. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7794. script_pushstr(st, buf);
  7795. } else
  7796. script_pushconststr(st, "");
  7797. return SCRIPT_CMD_SUCCESS;
  7798. }
  7799. /*==========================================
  7800. * Get the equipement name at pos
  7801. * return item jname or ""
  7802. * getequipname(<equipment slot>{,<char_id>})
  7803. *------------------------------------------*/
  7804. BUILDIN_FUNC(getequipname)
  7805. {
  7806. int i, num;
  7807. TBL_PC* sd;
  7808. struct item_data* item;
  7809. if (!script_charid2sd(3, sd)) {
  7810. script_pushconststr(st, "");
  7811. return SCRIPT_CMD_FAILURE;
  7812. }
  7813. num = script_getnum(st,2);
  7814. if( !equip_index_check(num) )
  7815. {
  7816. script_pushconststr(st,"");
  7817. return SCRIPT_CMD_SUCCESS;
  7818. }
  7819. // get inventory position of item
  7820. i = pc_checkequip(sd,equip_bitmask[num]);
  7821. if( i < 0 )
  7822. {
  7823. script_pushconststr(st,"");
  7824. return SCRIPT_CMD_SUCCESS;
  7825. }
  7826. item = sd->inventory_data[i];
  7827. if( item != 0 )
  7828. script_pushstrcopy(st,item->ename.c_str());
  7829. else
  7830. script_pushconststr(st,"");
  7831. return SCRIPT_CMD_SUCCESS;
  7832. }
  7833. /*==========================================
  7834. * getbrokenid [Valaris]
  7835. * getbrokenid(<number>{,<char_id>})
  7836. *------------------------------------------*/
  7837. BUILDIN_FUNC(getbrokenid)
  7838. {
  7839. int i,num,id = 0,brokencounter = 0;
  7840. TBL_PC *sd;
  7841. if (!script_charid2sd(3, sd)) {
  7842. script_pushint(st,0);
  7843. return SCRIPT_CMD_FAILURE;
  7844. }
  7845. num = script_getnum(st,2);
  7846. for(i = 0; i < MAX_INVENTORY; i++) {
  7847. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  7848. brokencounter++;
  7849. if(num == brokencounter){
  7850. id = sd->inventory.u.items_inventory[i].nameid;
  7851. break;
  7852. }
  7853. }
  7854. }
  7855. script_pushint(st,id);
  7856. return SCRIPT_CMD_SUCCESS;
  7857. }
  7858. /*==========================================
  7859. * repair [Valaris]
  7860. * repair <num>{,<char_id>};
  7861. *------------------------------------------*/
  7862. BUILDIN_FUNC(repair)
  7863. {
  7864. int i,num;
  7865. int repaircounter = 0;
  7866. TBL_PC *sd;
  7867. if (!script_charid2sd(3,sd))
  7868. return SCRIPT_CMD_FAILURE;
  7869. num = script_getnum(st,2);
  7870. for(i = 0; i < MAX_INVENTORY; i++) {
  7871. if( sd->inventory.u.items_inventory[i].attribute == 1 && !itemdb_ishatched_egg( &sd->inventory.u.items_inventory[i] ) ){
  7872. repaircounter++;
  7873. if(num == repaircounter) {
  7874. sd->inventory.u.items_inventory[i].attribute = 0;
  7875. clif_equiplist(sd);
  7876. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7877. clif_misceffect(&sd->bl, 3);
  7878. break;
  7879. }
  7880. }
  7881. }
  7882. return SCRIPT_CMD_SUCCESS;
  7883. }
  7884. /*==========================================
  7885. * repairall {<char_id>}
  7886. *------------------------------------------*/
  7887. BUILDIN_FUNC(repairall)
  7888. {
  7889. int i, repaircounter = 0;
  7890. TBL_PC *sd;
  7891. if (!script_charid2sd(2,sd))
  7892. return SCRIPT_CMD_FAILURE;
  7893. for(i = 0; i < MAX_INVENTORY; i++)
  7894. {
  7895. 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] ) ){
  7896. sd->inventory.u.items_inventory[i].attribute = 0;
  7897. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7898. repaircounter++;
  7899. }
  7900. }
  7901. if(repaircounter)
  7902. {
  7903. clif_misceffect(&sd->bl, 3);
  7904. clif_equiplist(sd);
  7905. }
  7906. return SCRIPT_CMD_SUCCESS;
  7907. }
  7908. /*==========================================
  7909. * Chk if player have something equiped at pos
  7910. * getequipisequiped <pos>{,<char_id>}
  7911. *------------------------------------------*/
  7912. BUILDIN_FUNC(getequipisequiped)
  7913. {
  7914. int i = -1,num;
  7915. TBL_PC *sd;
  7916. num = script_getnum(st,2);
  7917. if (!script_charid2sd(3,sd))
  7918. return SCRIPT_CMD_FAILURE;
  7919. if ( equip_index_check(num) )
  7920. i=pc_checkequip(sd,equip_bitmask[num]);
  7921. if(i >= 0)
  7922. script_pushint(st,1);
  7923. else
  7924. script_pushint(st,0);
  7925. return SCRIPT_CMD_SUCCESS;
  7926. }
  7927. /*==========================================
  7928. * Chk if the player have something equiped at pos
  7929. * if so chk if this item ain't marked not refinable or rental
  7930. * return (npc)
  7931. * 1 : true
  7932. * 0 : false
  7933. * getequipisenableref(<equipment slot>{,<char_id>})
  7934. *------------------------------------------*/
  7935. BUILDIN_FUNC(getequipisenableref)
  7936. {
  7937. int i = -1,num;
  7938. TBL_PC *sd;
  7939. num = script_getnum(st,2);
  7940. if (!script_charid2sd(3, sd)) {
  7941. script_pushint(st,0);
  7942. return SCRIPT_CMD_FAILURE;
  7943. }
  7944. if( equip_index_check(num) )
  7945. i = pc_checkequip(sd,equip_bitmask[num]);
  7946. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7947. script_pushint(st,1);
  7948. else
  7949. script_pushint(st,0);
  7950. return SCRIPT_CMD_SUCCESS;
  7951. }
  7952. /*==========================================
  7953. * Get the item refined value at pos
  7954. * return (npc)
  7955. * x : refine amount
  7956. * 0 : false (not refined)
  7957. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7958. *------------------------------------------*/
  7959. BUILDIN_FUNC(getequiprefinerycnt)
  7960. {
  7961. int i = -1,num;
  7962. TBL_PC *sd;
  7963. num = script_getnum(st,2);
  7964. if (!script_charid2sd(3, sd)) {
  7965. script_pushint(st,0);
  7966. return SCRIPT_CMD_FAILURE;
  7967. }
  7968. if (equip_index_check(num))
  7969. i=pc_checkequip(sd,equip_bitmask[num]);
  7970. if(i >= 0)
  7971. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7972. else
  7973. script_pushint(st,0);
  7974. return SCRIPT_CMD_SUCCESS;
  7975. }
  7976. /*==========================================
  7977. * Get the weapon level value at pos
  7978. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7979. * return (npc)
  7980. * x : weapon level
  7981. * 0 : false
  7982. * getequipweaponlv(<equipment slot>{,<char_id>})
  7983. *------------------------------------------*/
  7984. BUILDIN_FUNC(getequipweaponlv)
  7985. {
  7986. int i = -1, num;
  7987. TBL_PC *sd;
  7988. num = script_getnum(st, 2);
  7989. if (!script_charid2sd(3, sd)) {
  7990. script_pushint(st, 0);
  7991. return SCRIPT_CMD_FAILURE;
  7992. }
  7993. if (num == -1){
  7994. if( current_equip_item_index == -1 ){
  7995. script_pushint(st, 0);
  7996. return SCRIPT_CMD_FAILURE;
  7997. }
  7998. i = current_equip_item_index;
  7999. }else if (equip_index_check(num))
  8000. i = pc_checkequip(sd, equip_bitmask[num]);
  8001. if (i >= 0 && sd->inventory_data[i])
  8002. script_pushint(st, sd->inventory_data[i]->wlv);
  8003. else
  8004. script_pushint(st, 0);
  8005. return SCRIPT_CMD_SUCCESS;
  8006. }
  8007. /*==========================================
  8008. * Get the item refine chance (from refine.txt) for item at pos
  8009. * return (npc)
  8010. * x : refine chance
  8011. * 0 : false (max refine level or unequip..)
  8012. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8013. *------------------------------------------*/
  8014. BUILDIN_FUNC(getequippercentrefinery)
  8015. {
  8016. int i = -1,num;
  8017. bool enriched = false;
  8018. TBL_PC *sd;
  8019. num = script_getnum(st,2);
  8020. if (script_hasdata(st, 3))
  8021. enriched = script_getnum(st, 3) != 0;
  8022. if (!script_charid2sd(4, sd)) {
  8023. script_pushint(st,0);
  8024. return SCRIPT_CMD_FAILURE;
  8025. }
  8026. if (equip_index_check(num))
  8027. i = pc_checkequip(sd,equip_bitmask[num]);
  8028. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid) {
  8029. std::shared_ptr<s_refine_level_info> info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  8030. if( info == nullptr ){
  8031. script_pushint( st, 0 );
  8032. return SCRIPT_CMD_SUCCESS;
  8033. }
  8034. std::shared_ptr<s_refine_cost> cost = util::umap_find( info->costs, (uint16)( enriched ? REFINE_COST_ENRICHED : REFINE_COST_NORMAL ) );
  8035. if( cost == nullptr ){
  8036. script_pushint( st, 0 );
  8037. return SCRIPT_CMD_SUCCESS;
  8038. }
  8039. script_pushint( st, cost->chance / 100 );
  8040. }
  8041. else
  8042. script_pushint(st,0);
  8043. return SCRIPT_CMD_SUCCESS;
  8044. }
  8045. /*==========================================
  8046. * Refine +1 item at pos and log and display refine
  8047. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8048. *------------------------------------------*/
  8049. BUILDIN_FUNC(successrefitem) {
  8050. short i = -1, up = 1;
  8051. int pos;
  8052. TBL_PC *sd;
  8053. pos = script_getnum(st,2);
  8054. if (!script_charid2sd(4, sd)) {
  8055. script_pushint(st, -1);
  8056. return SCRIPT_CMD_FAILURE;
  8057. }
  8058. if (script_hasdata(st, 3))
  8059. up = script_getnum(st, 3);
  8060. if (equip_index_check(pos))
  8061. i = pc_checkequip(sd,equip_bitmask[pos]);
  8062. if (i >= 0) {
  8063. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8064. //Logs items, got from (N)PC scripts [Lupus]
  8065. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8066. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8067. script_pushint(st, MAX_REFINE);
  8068. return SCRIPT_CMD_SUCCESS;
  8069. }
  8070. sd->inventory.u.items_inventory[i].refine += up;
  8071. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8072. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8073. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8074. clif_delitem(sd,i,1,3);
  8075. //Logs items, got from (N)PC scripts [Lupus]
  8076. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8077. clif_additem(sd,i,1,0);
  8078. pc_equipitem(sd,i,ep);
  8079. clif_misceffect(&sd->bl,3);
  8080. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->wlv, sd->inventory.u.items_inventory[i].refine);
  8081. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8082. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8083. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  8084. { // Fame point system [DracoRPG]
  8085. switch (sd->inventory_data[i]->wlv){
  8086. case 1:
  8087. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8088. break;
  8089. case 2:
  8090. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8091. break;
  8092. case 3:
  8093. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8094. break;
  8095. }
  8096. }
  8097. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8098. return SCRIPT_CMD_SUCCESS;
  8099. }
  8100. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8101. script_pushint(st, -1);
  8102. return SCRIPT_CMD_FAILURE;
  8103. }
  8104. /*==========================================
  8105. * Show a failed Refine +1 attempt
  8106. * failedrefitem <equipment slot>{,<char_id>}
  8107. *------------------------------------------*/
  8108. BUILDIN_FUNC(failedrefitem) {
  8109. short i = -1;
  8110. int pos;
  8111. TBL_PC *sd;
  8112. pos = script_getnum(st,2);
  8113. if (!script_charid2sd(3, sd)) {
  8114. script_pushint(st, 0);
  8115. return SCRIPT_CMD_FAILURE;
  8116. }
  8117. if (equip_index_check(pos))
  8118. i = pc_checkequip(sd,equip_bitmask[pos]);
  8119. if (i >= 0) {
  8120. sd->inventory.u.items_inventory[i].refine = 0;
  8121. pc_unequipitem(sd,i,3); //recalculate bonus
  8122. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8123. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8124. clif_misceffect(&sd->bl,2); // display failure effect
  8125. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8126. script_pushint(st, 1);
  8127. return SCRIPT_CMD_SUCCESS;
  8128. }
  8129. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8130. script_pushint(st, 0);
  8131. return SCRIPT_CMD_FAILURE;
  8132. }
  8133. /*==========================================
  8134. * Downgrades an Equipment Part by -1 . [Masao]
  8135. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8136. *------------------------------------------*/
  8137. BUILDIN_FUNC(downrefitem) {
  8138. short i = -1, down = 1;
  8139. int pos;
  8140. TBL_PC *sd;
  8141. if (!script_charid2sd(4, sd)) {
  8142. script_pushint(st, -1);
  8143. return SCRIPT_CMD_FAILURE;
  8144. }
  8145. pos = script_getnum(st,2);
  8146. if (script_hasdata(st, 3))
  8147. down = script_getnum(st, 3);
  8148. if (equip_index_check(pos))
  8149. i = pc_checkequip(sd,equip_bitmask[pos]);
  8150. if (i >= 0) {
  8151. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8152. //Logs items, got from (N)PC scripts [Lupus]
  8153. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8154. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8155. sd->inventory.u.items_inventory[i].refine -= down;
  8156. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8157. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8158. clif_delitem(sd,i,1,3);
  8159. //Logs items, got from (N)PC scripts [Lupus]
  8160. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8161. clif_additem(sd,i,1,0);
  8162. pc_equipitem(sd,i,ep);
  8163. clif_misceffect(&sd->bl,2);
  8164. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8165. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8166. return SCRIPT_CMD_SUCCESS;
  8167. }
  8168. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8169. script_pushint(st, -1);
  8170. return SCRIPT_CMD_FAILURE;
  8171. }
  8172. /**
  8173. * Delete the item equipped at pos.
  8174. * delequip <equipment slot>{,<char_id>};
  8175. **/
  8176. BUILDIN_FUNC(delequip) {
  8177. short i = -1;
  8178. int pos;
  8179. int8 ret;
  8180. TBL_PC *sd;
  8181. pos = script_getnum(st,2);
  8182. if (!script_charid2sd(3,sd)) {
  8183. st->state = END;
  8184. return SCRIPT_CMD_FAILURE;
  8185. }
  8186. if (equip_index_check(pos))
  8187. i = pc_checkequip(sd,equip_bitmask[pos]);
  8188. if (i >= 0) {
  8189. pc_unequipitem(sd,i,3); //recalculate bonus
  8190. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8191. }
  8192. else {
  8193. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8194. st->state = END;
  8195. return SCRIPT_CMD_FAILURE;
  8196. }
  8197. script_pushint(st,ret);
  8198. return SCRIPT_CMD_SUCCESS;
  8199. }
  8200. /**
  8201. * Break the item equipped at pos.
  8202. * breakequip <equipment slot>{,<char_id>};
  8203. **/
  8204. BUILDIN_FUNC(breakequip) {
  8205. short i = -1;
  8206. int pos;
  8207. TBL_PC *sd;
  8208. pos = script_getnum(st,2);
  8209. if (!script_charid2sd(3,sd))
  8210. return SCRIPT_CMD_FAILURE;
  8211. if (equip_index_check(pos))
  8212. i = pc_checkequip(sd,equip_bitmask[pos]);
  8213. if (i >= 0) {
  8214. sd->inventory.u.items_inventory[i].attribute = 1;
  8215. pc_unequipitem(sd,i,3);
  8216. clif_equiplist(sd);
  8217. script_pushint(st,1);
  8218. return SCRIPT_CMD_SUCCESS;
  8219. }
  8220. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8221. script_pushint(st,0);
  8222. return SCRIPT_CMD_FAILURE;
  8223. }
  8224. /**
  8225. * statusup <stat>{,<char_id>};
  8226. **/
  8227. BUILDIN_FUNC(statusup)
  8228. {
  8229. int type;
  8230. TBL_PC *sd;
  8231. type = script_getnum(st,2);
  8232. if (!script_charid2sd(3, sd))
  8233. return SCRIPT_CMD_FAILURE;
  8234. pc_statusup(sd, type, 1);
  8235. return SCRIPT_CMD_SUCCESS;
  8236. }
  8237. /**
  8238. * statusup2 <stat>,<amount>{,<char_id>};
  8239. **/
  8240. BUILDIN_FUNC(statusup2)
  8241. {
  8242. int type,val;
  8243. TBL_PC *sd;
  8244. type=script_getnum(st,2);
  8245. val=script_getnum(st,3);
  8246. if (!script_charid2sd(4, sd))
  8247. return SCRIPT_CMD_FAILURE;
  8248. pc_statusup2(sd,type,val);
  8249. return SCRIPT_CMD_SUCCESS;
  8250. }
  8251. /// See 'doc/item_bonus.txt'
  8252. ///
  8253. /// bonus <bonus type>,<val1>;
  8254. /// bonus2 <bonus type>,<val1>,<val2>;
  8255. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8256. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8257. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8258. BUILDIN_FUNC(bonus)
  8259. {
  8260. int type;
  8261. int val1 = 0;
  8262. int val2 = 0;
  8263. int val3 = 0;
  8264. int val4 = 0;
  8265. int val5 = 0;
  8266. TBL_PC* sd;
  8267. if( !script_rid2sd(sd) )
  8268. return SCRIPT_CMD_SUCCESS; // no player attached
  8269. type = script_getnum(st,2);
  8270. switch( type ) {
  8271. case SP_AUTOSPELL:
  8272. case SP_AUTOSPELL_WHENHIT:
  8273. case SP_AUTOSPELL_ONSKILL:
  8274. case SP_SKILL_ATK:
  8275. case SP_SKILL_HEAL:
  8276. case SP_SKILL_HEAL2:
  8277. case SP_ADD_SKILL_BLOW:
  8278. case SP_CASTRATE:
  8279. case SP_ADDEFF_ONSKILL:
  8280. case SP_SKILL_USE_SP_RATE:
  8281. case SP_SKILL_COOLDOWN:
  8282. case SP_SKILL_FIXEDCAST:
  8283. case SP_SKILL_VARIABLECAST:
  8284. case SP_VARCASTRATE:
  8285. case SP_FIXCASTRATE:
  8286. case SP_SKILL_DELAY:
  8287. case SP_SKILL_USE_SP:
  8288. case SP_SUB_SKILL:
  8289. // these bonuses support skill names
  8290. if (script_isstring(st, 3)) {
  8291. const char *name = script_getstr(st, 3);
  8292. if (!(val1 = skill_name2id(name))) {
  8293. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8294. return SCRIPT_CMD_FAILURE;
  8295. }
  8296. } else {
  8297. val1 = script_getnum(st, 3);
  8298. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8299. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8300. return SCRIPT_CMD_FAILURE;
  8301. }
  8302. }
  8303. break;
  8304. default:
  8305. if (script_hasdata(st, 3))
  8306. val1 = script_getnum(st, 3);
  8307. break;
  8308. }
  8309. switch( script_lastdata(st)-2 ) {
  8310. case 0:
  8311. case 1:
  8312. pc_bonus(sd, type, val1);
  8313. break;
  8314. case 2:
  8315. val2 = script_getnum(st,4);
  8316. pc_bonus2(sd, type, val1, val2);
  8317. break;
  8318. case 3:
  8319. val2 = script_getnum(st,4);
  8320. val3 = script_getnum(st,5);
  8321. pc_bonus3(sd, type, val1, val2, val3);
  8322. break;
  8323. case 4:
  8324. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8325. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8326. else
  8327. val2 = script_getnum(st,4);
  8328. val3 = script_getnum(st,5);
  8329. val4 = script_getnum(st,6);
  8330. pc_bonus4(sd, type, val1, val2, val3, val4);
  8331. break;
  8332. case 5:
  8333. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8334. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8335. else
  8336. val2 = script_getnum(st,4);
  8337. val3 = script_getnum(st,5);
  8338. val4 = script_getnum(st,6);
  8339. val5 = script_getnum(st,7);
  8340. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8341. break;
  8342. default:
  8343. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8344. break;
  8345. }
  8346. return SCRIPT_CMD_SUCCESS;
  8347. }
  8348. BUILDIN_FUNC(autobonus)
  8349. {
  8350. unsigned int dur, pos;
  8351. short rate;
  8352. uint16 atk_type = 0;
  8353. TBL_PC* sd;
  8354. const char *bonus_script, *other_script = NULL;
  8355. if( !script_rid2sd(sd) )
  8356. return SCRIPT_CMD_SUCCESS; // no player attached
  8357. if (current_equip_combo_pos)
  8358. pos = current_equip_combo_pos;
  8359. else
  8360. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8361. if((sd->state.autobonus&pos) == pos)
  8362. return SCRIPT_CMD_SUCCESS;
  8363. rate = script_getnum(st,3);
  8364. dur = script_getnum(st,4);
  8365. bonus_script = script_getstr(st,2);
  8366. if( !rate || !dur || !pos || !bonus_script )
  8367. return SCRIPT_CMD_SUCCESS;
  8368. if( script_hasdata(st,5) )
  8369. atk_type = script_getnum(st,5);
  8370. if( script_hasdata(st,6) )
  8371. other_script = script_getstr(st,6);
  8372. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8373. {
  8374. script_add_autobonus(bonus_script);
  8375. if( other_script )
  8376. script_add_autobonus(other_script);
  8377. }
  8378. return SCRIPT_CMD_SUCCESS;
  8379. }
  8380. BUILDIN_FUNC(autobonus2)
  8381. {
  8382. unsigned int dur, pos;
  8383. short rate;
  8384. uint16 atk_type = 0;
  8385. TBL_PC* sd;
  8386. const char *bonus_script, *other_script = NULL;
  8387. if( !script_rid2sd(sd) )
  8388. return SCRIPT_CMD_SUCCESS; // no player attached
  8389. if (current_equip_combo_pos)
  8390. pos = current_equip_combo_pos;
  8391. else
  8392. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8393. if((sd->state.autobonus&pos) == pos)
  8394. return SCRIPT_CMD_SUCCESS;
  8395. rate = script_getnum(st,3);
  8396. dur = script_getnum(st,4);
  8397. bonus_script = script_getstr(st,2);
  8398. if( !rate || !dur || !pos || !bonus_script )
  8399. return SCRIPT_CMD_SUCCESS;
  8400. if( script_hasdata(st,5) )
  8401. atk_type = script_getnum(st,5);
  8402. if( script_hasdata(st,6) )
  8403. other_script = script_getstr(st,6);
  8404. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8405. {
  8406. script_add_autobonus(bonus_script);
  8407. if( other_script )
  8408. script_add_autobonus(other_script);
  8409. }
  8410. return SCRIPT_CMD_SUCCESS;
  8411. }
  8412. BUILDIN_FUNC(autobonus3)
  8413. {
  8414. unsigned int dur, pos;
  8415. short rate;
  8416. uint16 skill_id = 0;
  8417. TBL_PC* sd;
  8418. const char *bonus_script, *other_script = NULL;
  8419. if( !script_rid2sd(sd) )
  8420. return SCRIPT_CMD_SUCCESS; // no player attached
  8421. if (current_equip_combo_pos)
  8422. pos = current_equip_combo_pos;
  8423. else
  8424. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8425. if((sd->state.autobonus&pos) == pos)
  8426. return SCRIPT_CMD_SUCCESS;
  8427. rate = script_getnum(st,3);
  8428. dur = script_getnum(st,4);
  8429. if (script_isstring(st, 5)) {
  8430. const char *name = script_getstr(st, 5);
  8431. if (!(skill_id = skill_name2id(name))) {
  8432. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8433. return SCRIPT_CMD_FAILURE;
  8434. }
  8435. } else {
  8436. skill_id = script_getnum(st, 5);
  8437. if (!skill_get_index(skill_id)) {
  8438. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8439. return SCRIPT_CMD_FAILURE;
  8440. }
  8441. }
  8442. bonus_script = script_getstr(st,2);
  8443. if( !rate || !dur || !pos || !bonus_script )
  8444. return SCRIPT_CMD_SUCCESS;
  8445. if( script_hasdata(st,6) )
  8446. other_script = script_getstr(st,6);
  8447. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8448. {
  8449. script_add_autobonus(bonus_script);
  8450. if( other_script )
  8451. script_add_autobonus(other_script);
  8452. }
  8453. return SCRIPT_CMD_SUCCESS;
  8454. }
  8455. /// Changes the level of a player skill.
  8456. /// <flag> defaults to 1
  8457. /// <flag>=0 : set the level of the skill
  8458. /// <flag>=1 : set the temporary level of the skill
  8459. /// <flag>=2 : add to the level of the skill
  8460. ///
  8461. /// skill <skill id>,<level>,<flag>
  8462. /// skill <skill id>,<level>
  8463. /// skill "<skill name>",<level>,<flag>
  8464. /// skill "<skill name>",<level>
  8465. BUILDIN_FUNC(skill)
  8466. {
  8467. int id;
  8468. int level;
  8469. int flag = ADDSKILL_TEMP;
  8470. TBL_PC* sd;
  8471. const char* command = script_getfuncname(st);
  8472. if( !script_rid2sd(sd) )
  8473. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8474. if (strcmpi(command, "addtoskill") == 0)
  8475. flag = ADDSKILL_TEMP_ADDLEVEL;
  8476. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8477. level = script_getnum(st,3);
  8478. if( script_hasdata(st,4) )
  8479. flag = script_getnum(st,4);
  8480. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8481. return SCRIPT_CMD_SUCCESS;
  8482. }
  8483. /// Increases the level of a guild skill.
  8484. ///
  8485. /// guildskill <skill id>,<amount>;
  8486. /// guildskill "<skill name>",<amount>;
  8487. BUILDIN_FUNC(guildskill)
  8488. {
  8489. int id;
  8490. int level;
  8491. TBL_PC* sd;
  8492. int i;
  8493. if( !script_rid2sd(sd) )
  8494. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8495. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8496. level = script_getnum(st,3);
  8497. for( i=0; i < level; i++ )
  8498. guild_skillup(sd, id);
  8499. return SCRIPT_CMD_SUCCESS;
  8500. }
  8501. /// Returns the level of the player skill.
  8502. ///
  8503. /// getskilllv(<skill id>) -> <level>
  8504. /// getskilllv("<skill name>") -> <level>
  8505. BUILDIN_FUNC(getskilllv)
  8506. {
  8507. int id;
  8508. TBL_PC* sd;
  8509. if( !script_rid2sd(sd) )
  8510. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8511. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8512. script_pushint(st, pc_checkskill(sd,id));
  8513. return SCRIPT_CMD_SUCCESS;
  8514. }
  8515. /// Returns the level of the guild skill.
  8516. ///
  8517. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8518. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8519. BUILDIN_FUNC(getgdskilllv)
  8520. {
  8521. int guild_id;
  8522. uint16 skill_id;
  8523. struct guild* g;
  8524. guild_id = script_getnum(st,2);
  8525. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8526. g = guild_search(guild_id);
  8527. if( g == NULL )
  8528. script_pushint(st, -1);
  8529. else
  8530. script_pushint(st, guild_checkskill(g,skill_id));
  8531. return SCRIPT_CMD_SUCCESS;
  8532. }
  8533. /// Returns the 'basic_skill_check' setting.
  8534. /// This config determines if the server checks the skill level of NV_BASIC
  8535. /// before allowing the basic actions.
  8536. ///
  8537. /// basicskillcheck() -> <bool>
  8538. BUILDIN_FUNC(basicskillcheck)
  8539. {
  8540. script_pushint(st, battle_config.basic_skill_check);
  8541. return SCRIPT_CMD_SUCCESS;
  8542. }
  8543. /// Returns the GM level of the player.
  8544. ///
  8545. /// getgmlevel({<char_id>}) -> <level>
  8546. BUILDIN_FUNC(getgmlevel)
  8547. {
  8548. TBL_PC* sd;
  8549. if (!script_charid2sd(2,sd))
  8550. return SCRIPT_CMD_FAILURE;
  8551. script_pushint(st, pc_get_group_level(sd));
  8552. return SCRIPT_CMD_SUCCESS;
  8553. }
  8554. /// Returns the group ID of the player.
  8555. ///
  8556. /// getgroupid({<char_id>}) -> <int>
  8557. BUILDIN_FUNC(getgroupid)
  8558. {
  8559. TBL_PC* sd;
  8560. if (!script_charid2sd(2,sd))
  8561. return SCRIPT_CMD_FAILURE;
  8562. script_pushint(st, pc_get_group_id(sd));
  8563. return SCRIPT_CMD_SUCCESS;
  8564. }
  8565. /// Terminates the execution of this script instance.
  8566. ///
  8567. /// end
  8568. BUILDIN_FUNC(end)
  8569. {
  8570. TBL_PC* sd;
  8571. sd = map_id2sd(st->rid);
  8572. st->state = END;
  8573. if( st->mes_active )
  8574. st->mes_active = 0;
  8575. if (sd){
  8576. if (sd->state.callshop == 0)
  8577. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8578. else
  8579. sd->state.callshop = 0;
  8580. }
  8581. return SCRIPT_CMD_SUCCESS;
  8582. }
  8583. /// Checks if the player has that effect state (option).
  8584. ///
  8585. /// checkoption(<option>{,<char_id>}) -> <bool>
  8586. BUILDIN_FUNC(checkoption)
  8587. {
  8588. int option;
  8589. TBL_PC* sd;
  8590. if (!script_charid2sd(3,sd))
  8591. return SCRIPT_CMD_FAILURE;
  8592. option = script_getnum(st,2);
  8593. if( sd->sc.option&option )
  8594. script_pushint(st, 1);
  8595. else
  8596. script_pushint(st, 0);
  8597. return SCRIPT_CMD_SUCCESS;
  8598. }
  8599. /// Checks if the player is in that body state (opt1).
  8600. ///
  8601. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8602. BUILDIN_FUNC(checkoption1)
  8603. {
  8604. int opt1;
  8605. TBL_PC* sd;
  8606. if (!script_charid2sd(3,sd))
  8607. return SCRIPT_CMD_FAILURE;
  8608. opt1 = script_getnum(st,2);
  8609. if( sd->sc.opt1 == opt1 )
  8610. script_pushint(st, 1);
  8611. else
  8612. script_pushint(st, 0);
  8613. return SCRIPT_CMD_SUCCESS;
  8614. }
  8615. /// Checks if the player has that health state (opt2).
  8616. ///
  8617. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8618. BUILDIN_FUNC(checkoption2)
  8619. {
  8620. int opt2;
  8621. TBL_PC* sd;
  8622. if (!script_charid2sd(3,sd))
  8623. return SCRIPT_CMD_FAILURE;
  8624. opt2 = script_getnum(st,2);
  8625. if( sd->sc.opt2&opt2 )
  8626. script_pushint(st, 1);
  8627. else
  8628. script_pushint(st, 0);
  8629. return SCRIPT_CMD_SUCCESS;
  8630. }
  8631. /// Changes the effect state (option) of the player.
  8632. /// <flag> defaults to 1
  8633. /// <flag>=0 : removes the option
  8634. /// <flag>=other : adds the option
  8635. ///
  8636. /// setoption <option>{,<flag>{,<char_id>}};
  8637. BUILDIN_FUNC(setoption)
  8638. {
  8639. int option;
  8640. int flag = 1;
  8641. TBL_PC* sd;
  8642. if (!script_charid2sd(4,sd))
  8643. return SCRIPT_CMD_FAILURE;
  8644. option = script_getnum(st,2);
  8645. if( script_hasdata(st,3) )
  8646. flag = script_getnum(st,3);
  8647. else if( !option ){// Request to remove everything.
  8648. flag = 0;
  8649. option = OPTION_FALCON|OPTION_RIDING;
  8650. #ifndef NEW_CARTS
  8651. option |= OPTION_CART;
  8652. #endif
  8653. }
  8654. if( flag ){// Add option
  8655. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8656. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8657. pc_setoption(sd, sd->sc.option|option);
  8658. } else// Remove option
  8659. pc_setoption(sd, sd->sc.option&~option);
  8660. return SCRIPT_CMD_SUCCESS;
  8661. }
  8662. /// Returns if the player has a cart.
  8663. ///
  8664. /// checkcart({char_id}) -> <bool>
  8665. ///
  8666. /// @author Valaris
  8667. BUILDIN_FUNC(checkcart)
  8668. {
  8669. TBL_PC* sd;
  8670. if (!script_charid2sd(2,sd))
  8671. return SCRIPT_CMD_FAILURE;
  8672. if( pc_iscarton(sd) )
  8673. script_pushint(st, 1);
  8674. else
  8675. script_pushint(st, 0);
  8676. return SCRIPT_CMD_SUCCESS;
  8677. }
  8678. /// Sets the cart of the player.
  8679. /// <type> defaults to 1
  8680. /// <type>=0 : removes the cart
  8681. /// <type>=1 : Normal cart
  8682. /// <type>=2 : Wooden cart
  8683. /// <type>=3 : Covered cart with flowers and ferns
  8684. /// <type>=4 : Wooden cart with a Panda doll on the back
  8685. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8686. ///
  8687. /// setcart {<type>{,<char_id>}};
  8688. BUILDIN_FUNC(setcart)
  8689. {
  8690. int type = 1;
  8691. TBL_PC* sd;
  8692. if (!script_charid2sd(3,sd))
  8693. return SCRIPT_CMD_FAILURE;
  8694. if( script_hasdata(st,2) )
  8695. type = script_getnum(st,2);
  8696. pc_setcart(sd, type);
  8697. return SCRIPT_CMD_SUCCESS;
  8698. }
  8699. /// Returns if the player has a falcon.
  8700. ///
  8701. /// checkfalcon({char_id}) -> <bool>
  8702. ///
  8703. /// @author Valaris
  8704. BUILDIN_FUNC(checkfalcon)
  8705. {
  8706. TBL_PC* sd;
  8707. if (!script_charid2sd(2,sd))
  8708. return SCRIPT_CMD_FAILURE;
  8709. if( pc_isfalcon(sd) )
  8710. script_pushint(st, 1);
  8711. else
  8712. script_pushint(st, 0);
  8713. return SCRIPT_CMD_SUCCESS;
  8714. }
  8715. /// Sets if the player has a falcon or not.
  8716. /// <flag> defaults to 1
  8717. ///
  8718. /// setfalcon {<flag>{,<char_id>}};
  8719. BUILDIN_FUNC(setfalcon)
  8720. {
  8721. int flag = 1;
  8722. TBL_PC* sd;
  8723. if (!script_charid2sd(3,sd))
  8724. return SCRIPT_CMD_FAILURE;
  8725. if( script_hasdata(st,2) )
  8726. flag = script_getnum(st,2);
  8727. pc_setfalcon(sd, flag);
  8728. return SCRIPT_CMD_SUCCESS;
  8729. }
  8730. /// Returns if the player is riding.
  8731. ///
  8732. /// checkriding({char_id}) -> <bool>
  8733. ///
  8734. /// @author Valaris
  8735. BUILDIN_FUNC(checkriding)
  8736. {
  8737. TBL_PC* sd;
  8738. if (!script_charid2sd(2,sd))
  8739. return SCRIPT_CMD_FAILURE;
  8740. if( pc_isriding(sd) )
  8741. script_pushint(st, 1);
  8742. else
  8743. script_pushint(st, 0);
  8744. return SCRIPT_CMD_SUCCESS;
  8745. }
  8746. /// Sets if the player is riding.
  8747. /// <flag> defaults to 1
  8748. ///
  8749. /// setriding {<flag>{,<char_id>}};
  8750. BUILDIN_FUNC(setriding)
  8751. {
  8752. int flag = 1;
  8753. TBL_PC* sd;
  8754. if (!script_charid2sd(3,sd))
  8755. return SCRIPT_CMD_FAILURE;
  8756. if( script_hasdata(st,2) )
  8757. flag = script_getnum(st,2);
  8758. pc_setriding(sd, flag);
  8759. return SCRIPT_CMD_SUCCESS;
  8760. }
  8761. /// Returns if the player has a warg.
  8762. ///
  8763. /// checkwug({char_id}) -> <bool>
  8764. ///
  8765. BUILDIN_FUNC(checkwug)
  8766. {
  8767. TBL_PC* sd;
  8768. if (!script_charid2sd(2,sd))
  8769. return SCRIPT_CMD_FAILURE;
  8770. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8771. script_pushint(st, 1);
  8772. else
  8773. script_pushint(st, 0);
  8774. return SCRIPT_CMD_SUCCESS;
  8775. }
  8776. /// Returns if the player is wearing MADO Gear.
  8777. ///
  8778. /// checkmadogear({<char_id>}) -> <bool>
  8779. ///
  8780. BUILDIN_FUNC(checkmadogear)
  8781. {
  8782. TBL_PC* sd;
  8783. if (!script_charid2sd(2,sd))
  8784. return SCRIPT_CMD_FAILURE;
  8785. if( pc_ismadogear(sd) )
  8786. script_pushint(st, 1);
  8787. else
  8788. script_pushint(st, 0);
  8789. return SCRIPT_CMD_SUCCESS;
  8790. }
  8791. /// Sets if the player is riding MADO Gear.
  8792. /// <flag> defaults to true
  8793. /// <type> defaults to MADO_ROBOT
  8794. ///
  8795. /// setmadogear {<flag>{,type{,<char_id>}}};
  8796. BUILDIN_FUNC(setmadogear)
  8797. {
  8798. bool flag = true;
  8799. TBL_PC* sd;
  8800. uint16 type = MADO_ROBOT;
  8801. if (!script_charid2sd(4,sd))
  8802. return SCRIPT_CMD_FAILURE;
  8803. if( script_hasdata(st,2) )
  8804. flag = script_getnum(st,2) != 0;
  8805. if (script_hasdata(st, 3)) {
  8806. type = script_getnum(st, 3);
  8807. if (type == MADO_UNUSED || type >= MADO_MAX) {
  8808. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  8809. type = MADO_ROBOT;
  8810. }
  8811. }
  8812. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  8813. return SCRIPT_CMD_SUCCESS;
  8814. }
  8815. /// Sets the save point of the player.
  8816. ///
  8817. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8818. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8819. BUILDIN_FUNC(savepoint)
  8820. {
  8821. int x, y, m, cid_pos = 5;
  8822. unsigned short map_idx;
  8823. const char* str;
  8824. TBL_PC* sd;
  8825. if (script_lastdata(st) > 5)
  8826. cid_pos = 7;
  8827. if (!script_charid2sd(cid_pos,sd))
  8828. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8829. str = script_getstr(st, 2);
  8830. map_idx = mapindex_name2id(str);
  8831. if( !map_idx )
  8832. return SCRIPT_CMD_FAILURE;
  8833. x = script_getnum(st,3);
  8834. y = script_getnum(st,4);
  8835. m = map_mapindex2mapid(map_idx);
  8836. if (cid_pos == 7) {
  8837. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8838. x1 = x + dx, y1 = y + dy,
  8839. x0 = x - dx, y0 = y - dy;
  8840. uint8 n = 10;
  8841. do {
  8842. x = x0 + rnd()%(x1-x0+1);
  8843. y = y0 + rnd()%(y1-y0+1);
  8844. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8845. }
  8846. // Check for valid coordinates if map in local map-server
  8847. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8848. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8849. return SCRIPT_CMD_FAILURE;
  8850. }
  8851. pc_setsavepoint(sd, map_idx, x, y);
  8852. return SCRIPT_CMD_SUCCESS;
  8853. }
  8854. /*==========================================
  8855. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8856. *------------------------------------------*/
  8857. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8858. {
  8859. int type;
  8860. time_t timer;
  8861. struct tm *t;
  8862. type=script_getnum(st,2);
  8863. switch(type){
  8864. case 2:
  8865. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8866. // from the system clock.)
  8867. script_pushint(st,(int)time(NULL));
  8868. break;
  8869. case 1:
  8870. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8871. time(&timer);
  8872. t=localtime(&timer);
  8873. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8874. break;
  8875. case 0:
  8876. default:
  8877. //type 0:(System Ticks)
  8878. script_pushint(st,gettick());
  8879. break;
  8880. }
  8881. return SCRIPT_CMD_SUCCESS;
  8882. }
  8883. /*==========================================
  8884. * GetTime(Type)
  8885. *
  8886. * Returns the current value of a certain date type.
  8887. * Possible types are:
  8888. * - DT_SECOND Current seconds
  8889. * - DT_MINUTE Current minute
  8890. * - DT_HOUR Current hour
  8891. * - DT_DAYOFWEEK Day of current week
  8892. * - DT_DAYOFMONTH Day of current month
  8893. * - DT_MONTH Current month
  8894. * - DT_YEAR Current year
  8895. * - DT_DAYOFYEAR Day of current year
  8896. *
  8897. * If none of the above types is supplied -1 will be returned to the script
  8898. * and the script source will be reported into the mapserver console.
  8899. *------------------------------------------*/
  8900. BUILDIN_FUNC(gettime)
  8901. {
  8902. int type;
  8903. type = script_getnum(st,2);
  8904. if( type <= DT_MIN || type >= DT_MAX ){
  8905. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8906. script_reportsrc(st);
  8907. script_pushint(st,-1);
  8908. }else{
  8909. script_pushint(st,date_get((enum e_date_type)type));
  8910. }
  8911. return SCRIPT_CMD_SUCCESS;
  8912. }
  8913. /**
  8914. * Returns the current server time or the provided time in a readable format.
  8915. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  8916. */
  8917. BUILDIN_FUNC(gettimestr)
  8918. {
  8919. char *tmpstr;
  8920. const char *fmtstr;
  8921. int maxlen;
  8922. time_t now;
  8923. fmtstr = script_getstr(st,2);
  8924. maxlen = script_getnum(st,3);
  8925. if (script_hasdata(st, 4)) {
  8926. if (script_getnum(st, 4) < 0) {
  8927. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  8928. return SCRIPT_CMD_FAILURE;
  8929. } else
  8930. now = (time_t)script_getnum(st, 4);
  8931. } else
  8932. now = time(NULL);
  8933. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  8934. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8935. tmpstr[maxlen] = '\0';
  8936. script_pushstr(st,tmpstr);
  8937. return SCRIPT_CMD_SUCCESS;
  8938. }
  8939. /*==========================================
  8940. * Open player storage
  8941. *------------------------------------------*/
  8942. BUILDIN_FUNC(openstorage)
  8943. {
  8944. TBL_PC* sd;
  8945. if( !script_rid2sd(sd) )
  8946. return SCRIPT_CMD_SUCCESS;
  8947. storage_storageopen(sd);
  8948. return SCRIPT_CMD_SUCCESS;
  8949. }
  8950. BUILDIN_FUNC(guildopenstorage)
  8951. {
  8952. TBL_PC* sd;
  8953. int ret;
  8954. if( !script_rid2sd(sd) )
  8955. return SCRIPT_CMD_SUCCESS;
  8956. ret = storage_guild_storageopen(sd);
  8957. script_pushint(st,ret);
  8958. return SCRIPT_CMD_SUCCESS;
  8959. }
  8960. BUILDIN_FUNC(guildopenstorage_log){
  8961. #if PACKETVER < 20140205
  8962. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  8963. return SCRIPT_CMD_FAILURE;
  8964. #else
  8965. struct map_session_data* sd;
  8966. if( !script_charid2sd( 2, sd ) ){
  8967. return SCRIPT_CMD_FAILURE;
  8968. }
  8969. script_pushint( st, storage_guild_log_read( sd ) );
  8970. return SCRIPT_CMD_SUCCESS;
  8971. #endif
  8972. }
  8973. BUILDIN_FUNC(guild_has_permission){
  8974. struct map_session_data* sd;
  8975. if( !script_charid2sd( 3, sd ) ){
  8976. return SCRIPT_CMD_FAILURE;
  8977. }
  8978. int permission = script_getnum(st,2);
  8979. if( permission == 0 ){
  8980. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  8981. return SCRIPT_CMD_FAILURE;
  8982. }
  8983. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  8984. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  8985. return SCRIPT_CMD_FAILURE;
  8986. }
  8987. if( !sd->guild ){
  8988. script_pushint( st, false );
  8989. return SCRIPT_CMD_SUCCESS;
  8990. }
  8991. int position = guild_getposition(sd);
  8992. if( position < 0 || ( sd->guild->position[position].mode&permission ) != permission ){
  8993. script_pushint( st, false );
  8994. return SCRIPT_CMD_SUCCESS;
  8995. }
  8996. script_pushint( st, true );
  8997. return SCRIPT_CMD_SUCCESS;
  8998. }
  8999. /*==========================================
  9000. * Make player use a skill trought item usage
  9001. *------------------------------------------*/
  9002. /// itemskill <skill id>,<level>
  9003. /// itemskill "<skill name>",<level>
  9004. BUILDIN_FUNC(itemskill)
  9005. {
  9006. int id;
  9007. int lv;
  9008. bool keep_requirement;
  9009. TBL_PC* sd;
  9010. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9011. return SCRIPT_CMD_SUCCESS;
  9012. if (script_isstring(st, 2)) {
  9013. const char *name = script_getstr(st, 2);
  9014. if (!(id = skill_name2id(name))) {
  9015. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9016. return SCRIPT_CMD_FAILURE;
  9017. }
  9018. } else {
  9019. id = script_getnum(st, 2);
  9020. if (!skill_get_index(id)) {
  9021. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9022. return SCRIPT_CMD_FAILURE;
  9023. }
  9024. }
  9025. lv = script_getnum(st,3);
  9026. if (script_hasdata(st, 4)) {
  9027. keep_requirement = (script_getnum(st, 4) != 0);
  9028. } else {
  9029. keep_requirement = false;
  9030. }
  9031. sd->skillitem=id;
  9032. sd->skillitemlv=lv;
  9033. sd->skillitem_keep_requirement = keep_requirement;
  9034. clif_item_skill(sd,id,lv);
  9035. return SCRIPT_CMD_SUCCESS;
  9036. }
  9037. /*==========================================
  9038. * Attempt to create an item
  9039. *------------------------------------------*/
  9040. BUILDIN_FUNC(produce)
  9041. {
  9042. int trigger;
  9043. TBL_PC* sd;
  9044. if( !script_rid2sd(sd) )
  9045. return SCRIPT_CMD_SUCCESS;
  9046. trigger=script_getnum(st,2);
  9047. clif_skill_produce_mix_list(sd, -1, trigger);
  9048. return SCRIPT_CMD_SUCCESS;
  9049. }
  9050. /*==========================================
  9051. *
  9052. *------------------------------------------*/
  9053. BUILDIN_FUNC(cooking)
  9054. {
  9055. int trigger;
  9056. TBL_PC* sd;
  9057. if( !script_rid2sd(sd) )
  9058. return SCRIPT_CMD_SUCCESS;
  9059. trigger=script_getnum(st,2);
  9060. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  9061. return SCRIPT_CMD_SUCCESS;
  9062. }
  9063. /*==========================================
  9064. * Create a pet
  9065. *------------------------------------------*/
  9066. BUILDIN_FUNC(makepet)
  9067. {
  9068. struct map_session_data* sd;
  9069. uint16 mob_id;
  9070. if( !script_rid2sd(sd) )
  9071. return SCRIPT_CMD_FAILURE;
  9072. mob_id = script_getnum(st,2);
  9073. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9074. if( !pet ){
  9075. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9076. return SCRIPT_CMD_FAILURE;
  9077. }
  9078. sd->catch_target_class = mob_id;
  9079. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9080. 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() );
  9081. return SCRIPT_CMD_SUCCESS;
  9082. }
  9083. /**
  9084. * Give player exp base,job * quest_exp_rate/100
  9085. * getexp <base xp>,<job xp>{,<char_id>};
  9086. **/
  9087. BUILDIN_FUNC(getexp)
  9088. {
  9089. TBL_PC* sd;
  9090. int base=0,job=0;
  9091. double bonus;
  9092. if (!script_charid2sd(4,sd))
  9093. return SCRIPT_CMD_FAILURE;
  9094. base=script_getnum(st,2);
  9095. job =script_getnum(st,3);
  9096. if(base<0 || job<0)
  9097. return SCRIPT_CMD_SUCCESS;
  9098. // bonus for npc-given exp
  9099. bonus = battle_config.quest_exp_rate / 100.;
  9100. if (base)
  9101. base = (int) cap_value(base * bonus, 0, INT_MAX);
  9102. if (job)
  9103. job = (int) cap_value(job * bonus, 0, INT_MAX);
  9104. pc_gainexp(sd, NULL, base, job, 1);
  9105. #ifdef RENEWAL
  9106. if (base && sd->hd)
  9107. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9108. #endif
  9109. return SCRIPT_CMD_SUCCESS;
  9110. }
  9111. /*==========================================
  9112. * Gain guild exp [Celest]
  9113. *------------------------------------------*/
  9114. BUILDIN_FUNC(guildgetexp)
  9115. {
  9116. TBL_PC* sd;
  9117. int exp;
  9118. if( !script_rid2sd(sd) )
  9119. return SCRIPT_CMD_SUCCESS;
  9120. exp = script_getnum(st,2);
  9121. if(exp < 0)
  9122. return SCRIPT_CMD_SUCCESS;
  9123. if(sd && sd->status.guild_id > 0)
  9124. guild_getexp (sd, exp);
  9125. return SCRIPT_CMD_SUCCESS;
  9126. }
  9127. /*==========================================
  9128. * Changes the guild master of a guild [Skotlex]
  9129. *------------------------------------------*/
  9130. BUILDIN_FUNC(guildchangegm)
  9131. {
  9132. TBL_PC *sd;
  9133. int guild_id;
  9134. const char *name;
  9135. guild_id = script_getnum(st,2);
  9136. name = script_getstr(st,3);
  9137. sd=map_nick2sd(name,false);
  9138. if (!sd)
  9139. script_pushint(st,0);
  9140. else
  9141. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9142. return SCRIPT_CMD_SUCCESS;
  9143. }
  9144. /*==========================================
  9145. * Spawn a monster:
  9146. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9147. *------------------------------------------*/
  9148. BUILDIN_FUNC(monster)
  9149. {
  9150. const char* mapn = script_getstr(st,2);
  9151. int x = script_getnum(st,3);
  9152. int y = script_getnum(st,4);
  9153. const char* str = script_getstr(st,5);
  9154. int class_ = script_getnum(st,6);
  9155. int amount = script_getnum(st,7);
  9156. const char* event = "";
  9157. unsigned int size = SZ_SMALL;
  9158. enum mob_ai ai = AI_NONE;
  9159. struct map_session_data* sd;
  9160. int16 m;
  9161. int i;
  9162. if (script_hasdata(st, 8)) {
  9163. event = script_getstr(st, 8);
  9164. check_event(st, event);
  9165. }
  9166. if (script_hasdata(st, 9)) {
  9167. size = script_getnum(st, 9);
  9168. if (size > SZ_BIG) {
  9169. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9170. return SCRIPT_CMD_FAILURE;
  9171. }
  9172. }
  9173. if (script_hasdata(st, 10)) {
  9174. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9175. if (ai >= AI_MAX) {
  9176. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9177. return SCRIPT_CMD_FAILURE;
  9178. }
  9179. }
  9180. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9181. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9182. return SCRIPT_CMD_FAILURE;
  9183. }
  9184. sd = map_id2sd(st->rid);
  9185. if (sd && strcmp(mapn, "this") == 0)
  9186. m = sd->bl.m;
  9187. else
  9188. m = map_mapname2mapid(mapn);
  9189. for(i = 0; i < amount; i++) { //not optimised
  9190. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9191. if (mobid)
  9192. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9193. }
  9194. return SCRIPT_CMD_SUCCESS;
  9195. }
  9196. /*==========================================
  9197. * Request List of Monster Drops
  9198. *------------------------------------------*/
  9199. BUILDIN_FUNC(getmobdrops)
  9200. {
  9201. int class_ = script_getnum(st,2);
  9202. int i, j = 0;
  9203. if( !mobdb_checkid(class_) )
  9204. {
  9205. script_pushint(st, 0);
  9206. return SCRIPT_CMD_SUCCESS;
  9207. }
  9208. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9209. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9210. {
  9211. if( mob->dropitem[i].nameid == 0 )
  9212. continue;
  9213. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  9214. continue;
  9215. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9216. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9217. j++;
  9218. }
  9219. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9220. script_pushint(st, 1);
  9221. return SCRIPT_CMD_SUCCESS;
  9222. }
  9223. /*==========================================
  9224. * Spawn a monster in a random location
  9225. * in x0,x1,y0,y1 area.
  9226. *------------------------------------------*/
  9227. BUILDIN_FUNC(areamonster)
  9228. {
  9229. const char* mapn = script_getstr(st,2);
  9230. int x0 = script_getnum(st,3);
  9231. int y0 = script_getnum(st,4);
  9232. int x1 = script_getnum(st,5);
  9233. int y1 = script_getnum(st,6);
  9234. const char* str = script_getstr(st,7);
  9235. int class_ = script_getnum(st,8);
  9236. int amount = script_getnum(st,9);
  9237. const char* event = "";
  9238. unsigned int size = SZ_SMALL;
  9239. enum mob_ai ai = AI_NONE;
  9240. struct map_session_data* sd;
  9241. int16 m;
  9242. int i;
  9243. if (script_hasdata(st,10)) {
  9244. event = script_getstr(st, 10);
  9245. check_event(st, event);
  9246. }
  9247. if (script_hasdata(st, 11)) {
  9248. size = script_getnum(st, 11);
  9249. if (size > 3) {
  9250. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9251. return SCRIPT_CMD_FAILURE;
  9252. }
  9253. }
  9254. if (script_hasdata(st, 12)) {
  9255. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9256. if (ai >= AI_MAX) {
  9257. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9258. return SCRIPT_CMD_FAILURE;
  9259. }
  9260. }
  9261. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9262. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9263. return SCRIPT_CMD_FAILURE;
  9264. }
  9265. sd = map_id2sd(st->rid);
  9266. if (sd && strcmp(mapn, "this") == 0)
  9267. m = sd->bl.m;
  9268. else
  9269. m = map_mapname2mapid(mapn);
  9270. for(i = 0; i < amount; i++) { //not optimised
  9271. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9272. if (mobid)
  9273. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9274. }
  9275. return SCRIPT_CMD_SUCCESS;
  9276. }
  9277. /*==========================================
  9278. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9279. *------------------------------------------*/
  9280. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9281. { //same fix but with killmonster instead - stripping events from mobs.
  9282. TBL_MOB* md = (TBL_MOB*)bl;
  9283. char *event=va_arg(ap,char *);
  9284. int allflag=va_arg(ap,int);
  9285. md->state.npc_killmonster = 1;
  9286. if(!allflag){
  9287. if(strcmp(event,md->npc_event)==0)
  9288. status_kill(bl);
  9289. }else{
  9290. if(!md->spawn)
  9291. status_kill(bl);
  9292. }
  9293. md->state.npc_killmonster = 0;
  9294. return SCRIPT_CMD_SUCCESS;
  9295. }
  9296. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9297. {
  9298. TBL_MOB* md = (TBL_MOB*)bl;
  9299. char *event=va_arg(ap,char *);
  9300. int allflag=va_arg(ap,int);
  9301. if(!allflag){
  9302. if(strcmp(event,md->npc_event)==0)
  9303. status_kill(bl);
  9304. }else{
  9305. if(!md->spawn)
  9306. status_kill(bl);
  9307. }
  9308. return SCRIPT_CMD_SUCCESS;
  9309. }
  9310. BUILDIN_FUNC(killmonster)
  9311. {
  9312. const char *mapname,*event;
  9313. int16 m,allflag=0;
  9314. mapname=script_getstr(st,2);
  9315. event=script_getstr(st,3);
  9316. if(strcmp(event,"All")==0)
  9317. allflag = 1;
  9318. else
  9319. check_event(st, event);
  9320. if( (m=map_mapname2mapid(mapname))<0 )
  9321. return SCRIPT_CMD_SUCCESS;
  9322. if( script_hasdata(st,4) ) {
  9323. if ( script_getnum(st,4) == 1 ) {
  9324. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9325. return SCRIPT_CMD_SUCCESS;
  9326. }
  9327. }
  9328. map_freeblock_lock();
  9329. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9330. map_freeblock_unlock();
  9331. return SCRIPT_CMD_SUCCESS;
  9332. }
  9333. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9334. { //Strips the event from the mob if it's killed the old method.
  9335. struct mob_data *md;
  9336. md = BL_CAST(BL_MOB, bl);
  9337. if (md->npc_event[0])
  9338. md->npc_event[0] = 0;
  9339. status_kill(bl);
  9340. return 0;
  9341. }
  9342. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9343. {
  9344. status_kill(bl);
  9345. return 0;
  9346. }
  9347. BUILDIN_FUNC(killmonsterall)
  9348. {
  9349. const char *mapname;
  9350. int16 m;
  9351. mapname=script_getstr(st,2);
  9352. if( (m = map_mapname2mapid(mapname))<0 )
  9353. return SCRIPT_CMD_SUCCESS;
  9354. if( script_hasdata(st,3) ) {
  9355. if ( script_getnum(st,3) == 1 ) {
  9356. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9357. return SCRIPT_CMD_SUCCESS;
  9358. }
  9359. }
  9360. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9361. return SCRIPT_CMD_SUCCESS;
  9362. }
  9363. /*==========================================
  9364. * Creates a clone of a player.
  9365. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9366. *------------------------------------------*/
  9367. BUILDIN_FUNC(clone)
  9368. {
  9369. TBL_PC *sd, *msd=NULL;
  9370. uint32 char_id, master_id = 0, x, y, flag = 0;
  9371. int16 m;
  9372. enum e_mode mode = MD_NONE;
  9373. unsigned int duration = 0;
  9374. const char *mapname,*event;
  9375. mapname=script_getstr(st,2);
  9376. x=script_getnum(st,3);
  9377. y=script_getnum(st,4);
  9378. event=script_getstr(st,5);
  9379. char_id=script_getnum(st,6);
  9380. if( script_hasdata(st,7) )
  9381. master_id=script_getnum(st,7);
  9382. if( script_hasdata(st,8) )
  9383. mode=static_cast<e_mode>(script_getnum(st,8));
  9384. if( script_hasdata(st,9) )
  9385. flag=script_getnum(st,9);
  9386. if( script_hasdata(st,10) )
  9387. duration=script_getnum(st,10);
  9388. check_event(st, event);
  9389. m = map_mapname2mapid(mapname);
  9390. if (m < 0)
  9391. return SCRIPT_CMD_SUCCESS;
  9392. sd = map_charid2sd(char_id);
  9393. if (master_id) {
  9394. msd = map_charid2sd(master_id);
  9395. if (msd)
  9396. master_id = msd->bl.id;
  9397. else
  9398. master_id = 0;
  9399. }
  9400. if (sd) //Return ID of newly crafted clone.
  9401. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9402. else //Failed to create clone.
  9403. script_pushint(st,0);
  9404. return SCRIPT_CMD_SUCCESS;
  9405. }
  9406. /*==========================================
  9407. *------------------------------------------*/
  9408. BUILDIN_FUNC(doevent)
  9409. {
  9410. const char* event;
  9411. struct map_session_data* sd;
  9412. if( !script_rid2sd(sd) )
  9413. return SCRIPT_CMD_SUCCESS;
  9414. event = script_getstr(st,2);
  9415. check_event(st, event);
  9416. npc_event(sd, event, 0);
  9417. return SCRIPT_CMD_SUCCESS;
  9418. }
  9419. /*==========================================
  9420. *------------------------------------------*/
  9421. BUILDIN_FUNC(donpcevent)
  9422. {
  9423. const char* event = script_getstr(st,2);
  9424. check_event(st, event);
  9425. if( !npc_event_do(event) ) {
  9426. struct npc_data * nd = map_id2nd(st->oid);
  9427. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9428. script_pushint(st, 0);
  9429. } else
  9430. script_pushint(st, 1);
  9431. return SCRIPT_CMD_SUCCESS;
  9432. }
  9433. /// for Aegis compatibility
  9434. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9435. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9436. {
  9437. const char* npc = script_getstr(st,2);
  9438. const char* command = script_getstr(st,3);
  9439. char event[EVENT_NAME_LENGTH];
  9440. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9441. check_event(st, event);
  9442. if( npc_event_do(event) ){
  9443. script_pushint(st, true);
  9444. }else{
  9445. struct npc_data * nd = map_id2nd(st->oid);
  9446. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9447. script_pushint(st, false);
  9448. }
  9449. return SCRIPT_CMD_SUCCESS;
  9450. }
  9451. /*==========================================
  9452. *------------------------------------------*/
  9453. BUILDIN_FUNC(addtimer)
  9454. {
  9455. int tick;
  9456. const char* event;
  9457. TBL_PC* sd;
  9458. if( !script_rid2sd(sd) )
  9459. return SCRIPT_CMD_SUCCESS;
  9460. tick = script_getnum(st,2);
  9461. event = script_getstr(st, 3);
  9462. check_event(st, event);
  9463. if (!pc_addeventtimer(sd,tick,event)) {
  9464. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9465. return SCRIPT_CMD_FAILURE;
  9466. }
  9467. return SCRIPT_CMD_SUCCESS;
  9468. }
  9469. /*==========================================
  9470. *------------------------------------------*/
  9471. BUILDIN_FUNC(deltimer)
  9472. {
  9473. const char *event;
  9474. TBL_PC* sd;
  9475. if( !script_rid2sd(sd) )
  9476. return SCRIPT_CMD_SUCCESS;
  9477. event=script_getstr(st, 2);
  9478. check_event(st, event);
  9479. pc_deleventtimer(sd,event);
  9480. return SCRIPT_CMD_SUCCESS;
  9481. }
  9482. /*==========================================
  9483. *------------------------------------------*/
  9484. BUILDIN_FUNC(addtimercount)
  9485. {
  9486. const char *event;
  9487. int tick;
  9488. TBL_PC* sd;
  9489. if( !script_rid2sd(sd) )
  9490. return SCRIPT_CMD_SUCCESS;
  9491. tick=script_getnum(st,2);
  9492. event=script_getstr(st,3);
  9493. check_event(st, event);
  9494. pc_addeventtimercount(sd,event,tick);
  9495. return SCRIPT_CMD_SUCCESS;
  9496. }
  9497. /*==========================================
  9498. *------------------------------------------*/
  9499. BUILDIN_FUNC(initnpctimer)
  9500. {
  9501. struct npc_data *nd;
  9502. int flag = 0;
  9503. if( script_hasdata(st,3) )
  9504. { //Two arguments: NPC name and attach flag.
  9505. nd = npc_name2id(script_getstr(st, 2));
  9506. flag = script_getnum(st,3);
  9507. }
  9508. else if( script_hasdata(st,2) )
  9509. { //Check if argument is numeric (flag) or string (npc name)
  9510. if( script_isstring(st, 2) ) //NPC name
  9511. nd = npc_name2id(script_getstr(st, 2));
  9512. else //Flag
  9513. {
  9514. nd = (struct npc_data *)map_id2bl(st->oid);
  9515. flag = script_getnum(st, 2);
  9516. }
  9517. }
  9518. else
  9519. nd = (struct npc_data *)map_id2bl(st->oid);
  9520. if( !nd )
  9521. return SCRIPT_CMD_SUCCESS;
  9522. if( flag ) //Attach
  9523. {
  9524. TBL_PC* sd;
  9525. if( !script_rid2sd(sd) )
  9526. return SCRIPT_CMD_SUCCESS;
  9527. nd->u.scr.rid = sd->bl.id;
  9528. }
  9529. nd->u.scr.timertick = 0;
  9530. npc_settimerevent_tick(nd,0);
  9531. npc_timerevent_start(nd, st->rid);
  9532. return SCRIPT_CMD_SUCCESS;
  9533. }
  9534. /*==========================================
  9535. *------------------------------------------*/
  9536. BUILDIN_FUNC(startnpctimer)
  9537. {
  9538. struct npc_data *nd;
  9539. int flag = 0;
  9540. if( script_hasdata(st,3) )
  9541. { //Two arguments: NPC name and attach flag.
  9542. nd = npc_name2id(script_getstr(st, 2));
  9543. flag = script_getnum(st,3);
  9544. }
  9545. else if( script_hasdata(st,2) )
  9546. { //Check if argument is numeric (flag) or string (npc name)
  9547. if( script_isstring(st, 2) ) //NPC name
  9548. nd = npc_name2id(script_getstr(st, 2));
  9549. else //Flag
  9550. {
  9551. nd = (struct npc_data *)map_id2bl(st->oid);
  9552. flag = script_getnum(st, 2);
  9553. }
  9554. }
  9555. else
  9556. nd=(struct npc_data *)map_id2bl(st->oid);
  9557. if( !nd )
  9558. return SCRIPT_CMD_SUCCESS;
  9559. if( flag ) //Attach
  9560. {
  9561. TBL_PC* sd;
  9562. if( !script_rid2sd(sd) )
  9563. return SCRIPT_CMD_SUCCESS;
  9564. nd->u.scr.rid = sd->bl.id;
  9565. }
  9566. npc_timerevent_start(nd, st->rid);
  9567. return SCRIPT_CMD_SUCCESS;
  9568. }
  9569. /*==========================================
  9570. *------------------------------------------*/
  9571. BUILDIN_FUNC(stopnpctimer)
  9572. {
  9573. struct npc_data *nd;
  9574. int flag = 0;
  9575. if( script_hasdata(st,3) )
  9576. { //Two arguments: NPC name and attach flag.
  9577. nd = npc_name2id(script_getstr(st, 2));
  9578. flag = script_getnum(st,3);
  9579. }
  9580. else if( script_hasdata(st,2) )
  9581. { //Check if argument is numeric (flag) or string (npc name)
  9582. if( script_isstring(st, 2) ) //NPC name
  9583. nd = npc_name2id(script_getstr(st, 2));
  9584. else //Flag
  9585. {
  9586. nd = (struct npc_data *)map_id2bl(st->oid);
  9587. flag = script_getnum(st, 2);
  9588. }
  9589. }
  9590. else
  9591. nd=(struct npc_data *)map_id2bl(st->oid);
  9592. if( !nd )
  9593. return SCRIPT_CMD_SUCCESS;
  9594. if( flag ) //Detach
  9595. nd->u.scr.rid = 0;
  9596. npc_timerevent_stop(nd);
  9597. return SCRIPT_CMD_SUCCESS;
  9598. }
  9599. /*==========================================
  9600. *------------------------------------------*/
  9601. BUILDIN_FUNC(getnpctimer)
  9602. {
  9603. struct npc_data *nd;
  9604. TBL_PC *sd;
  9605. int type = script_getnum(st,2);
  9606. t_tick val = 0;
  9607. if( script_hasdata(st,3) )
  9608. nd = npc_name2id(script_getstr(st,3));
  9609. else
  9610. nd = (struct npc_data *)map_id2bl(st->oid);
  9611. if( !nd || nd->bl.type != BL_NPC )
  9612. {
  9613. script_pushint(st,0);
  9614. ShowError("getnpctimer: Invalid NPC.\n");
  9615. return SCRIPT_CMD_FAILURE;
  9616. }
  9617. switch( type )
  9618. {
  9619. case 0: val = npc_gettimerevent_tick(nd); break;
  9620. case 1:
  9621. if( nd->u.scr.rid )
  9622. {
  9623. sd = map_id2sd(nd->u.scr.rid);
  9624. if( !sd )
  9625. {
  9626. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9627. break;
  9628. }
  9629. val = (sd->npc_timer_id != INVALID_TIMER);
  9630. }
  9631. else
  9632. val = (nd->u.scr.timerid != INVALID_TIMER);
  9633. break;
  9634. case 2: val = nd->u.scr.timeramount; break;
  9635. }
  9636. script_pushint(st,val);
  9637. return SCRIPT_CMD_SUCCESS;
  9638. }
  9639. /*==========================================
  9640. *------------------------------------------*/
  9641. BUILDIN_FUNC(setnpctimer)
  9642. {
  9643. int tick;
  9644. struct npc_data *nd;
  9645. tick = script_getnum(st,2);
  9646. if( script_hasdata(st,3) )
  9647. nd = npc_name2id(script_getstr(st,3));
  9648. else
  9649. nd = (struct npc_data *)map_id2bl(st->oid);
  9650. if( !nd || nd->bl.type != BL_NPC )
  9651. {
  9652. script_pushint(st,1);
  9653. ShowError("setnpctimer: Invalid NPC.\n");
  9654. return SCRIPT_CMD_FAILURE;
  9655. }
  9656. npc_settimerevent_tick(nd,tick);
  9657. script_pushint(st,0);
  9658. return SCRIPT_CMD_SUCCESS;
  9659. }
  9660. /*==========================================
  9661. * attaches the player rid to the timer [Celest]
  9662. *------------------------------------------*/
  9663. BUILDIN_FUNC(attachnpctimer)
  9664. {
  9665. TBL_PC *sd;
  9666. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9667. if( !nd || nd->bl.type != BL_NPC )
  9668. {
  9669. script_pushint(st,1);
  9670. ShowError("setnpctimer: Invalid NPC.\n");
  9671. return SCRIPT_CMD_FAILURE;
  9672. }
  9673. if( !script_nick2sd(2,sd) ){
  9674. script_pushint(st,1);
  9675. ShowWarning("attachnpctimer: Invalid player.\n");
  9676. return SCRIPT_CMD_FAILURE;
  9677. }
  9678. nd->u.scr.rid = sd->bl.id;
  9679. script_pushint(st,0);
  9680. return SCRIPT_CMD_SUCCESS;
  9681. }
  9682. /*==========================================
  9683. * detaches a player rid from the timer [Celest]
  9684. *------------------------------------------*/
  9685. BUILDIN_FUNC(detachnpctimer)
  9686. {
  9687. struct npc_data *nd;
  9688. if( script_hasdata(st,2) )
  9689. nd = npc_name2id(script_getstr(st,2));
  9690. else
  9691. nd = (struct npc_data *)map_id2bl(st->oid);
  9692. if( !nd || nd->bl.type != BL_NPC )
  9693. {
  9694. script_pushint(st,1);
  9695. ShowError("detachnpctimer: Invalid NPC.\n");
  9696. return SCRIPT_CMD_FAILURE;
  9697. }
  9698. nd->u.scr.rid = 0;
  9699. script_pushint(st,0);
  9700. return SCRIPT_CMD_SUCCESS;
  9701. }
  9702. /*==========================================
  9703. * To avoid "player not attached" script errors, this function is provided,
  9704. * it checks if there is a player attached to the current script. [Skotlex]
  9705. * If no, returns 0, if yes, returns the account_id of the attached player.
  9706. *------------------------------------------*/
  9707. BUILDIN_FUNC(playerattached)
  9708. {
  9709. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9710. script_pushint(st,0);
  9711. else
  9712. script_pushint(st,st->rid);
  9713. return SCRIPT_CMD_SUCCESS;
  9714. }
  9715. /*==========================================
  9716. *------------------------------------------*/
  9717. BUILDIN_FUNC(announce)
  9718. {
  9719. const char *mes = script_getstr(st,2);
  9720. int flag = script_getnum(st,3);
  9721. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9722. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9723. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9724. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9725. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9726. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9727. {
  9728. send_target target;
  9729. struct block_list *bl;
  9730. // If bc_npc flag is set, use NPC as broadcast source
  9731. if(flag&BC_NPC){
  9732. bl = map_id2bl(st->oid);
  9733. }else{
  9734. struct map_session_data* sd;
  9735. if(script_charid2sd(9, sd))
  9736. bl = &sd->bl;
  9737. else
  9738. bl = NULL;
  9739. }
  9740. if (bl == NULL)
  9741. return SCRIPT_CMD_SUCCESS;
  9742. switch (flag&BC_TARGET_MASK) {
  9743. case BC_MAP: target = ALL_SAMEMAP; break;
  9744. case BC_AREA: target = AREA; break;
  9745. case BC_SELF: target = SELF; break;
  9746. default: target = ALL_CLIENT; break; // BC_ALL
  9747. }
  9748. if (fontColor)
  9749. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9750. else
  9751. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9752. }
  9753. else
  9754. {
  9755. if (fontColor)
  9756. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9757. else
  9758. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9759. }
  9760. return SCRIPT_CMD_SUCCESS;
  9761. }
  9762. /*==========================================
  9763. *------------------------------------------*/
  9764. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9765. {
  9766. char *mes = va_arg(ap, char *);
  9767. int len = va_arg(ap, int);
  9768. int type = va_arg(ap, int);
  9769. char *fontColor = va_arg(ap, char *);
  9770. short fontType = (short)va_arg(ap, int);
  9771. short fontSize = (short)va_arg(ap, int);
  9772. short fontAlign = (short)va_arg(ap, int);
  9773. short fontY = (short)va_arg(ap, int);
  9774. if (fontColor)
  9775. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9776. else
  9777. clif_broadcast(bl, mes, len, type, SELF);
  9778. return SCRIPT_CMD_SUCCESS;
  9779. }
  9780. BUILDIN_FUNC(mapannounce)
  9781. {
  9782. const char *mapname = script_getstr(st,2);
  9783. const char *mes = script_getstr(st,3);
  9784. int flag = script_getnum(st,4);
  9785. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9786. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9787. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9788. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9789. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9790. int16 m;
  9791. if ((m = map_mapname2mapid(mapname)) < 0)
  9792. return SCRIPT_CMD_SUCCESS;
  9793. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9794. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9795. return SCRIPT_CMD_SUCCESS;
  9796. }
  9797. /*==========================================
  9798. *------------------------------------------*/
  9799. BUILDIN_FUNC(areaannounce)
  9800. {
  9801. const char *mapname = script_getstr(st,2);
  9802. int x0 = script_getnum(st,3);
  9803. int y0 = script_getnum(st,4);
  9804. int x1 = script_getnum(st,5);
  9805. int y1 = script_getnum(st,6);
  9806. const char *mes = script_getstr(st,7);
  9807. int flag = script_getnum(st,8);
  9808. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9809. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9810. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9811. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9812. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9813. int16 m;
  9814. if ((m = map_mapname2mapid(mapname)) < 0)
  9815. return SCRIPT_CMD_SUCCESS;
  9816. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9817. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9818. return SCRIPT_CMD_SUCCESS;
  9819. }
  9820. /*==========================================
  9821. *------------------------------------------*/
  9822. BUILDIN_FUNC(getusers)
  9823. {
  9824. int flag, val = 0;
  9825. struct map_session_data* sd;
  9826. struct block_list* bl = NULL;
  9827. flag = script_getnum(st,2);
  9828. switch(flag&0x07)
  9829. {
  9830. case 0:
  9831. if(flag&0x8)
  9832. {// npc
  9833. bl = map_id2bl(st->oid);
  9834. }
  9835. else if(script_rid2sd(sd))
  9836. {// pc
  9837. bl = &sd->bl;
  9838. }
  9839. if(bl)
  9840. {
  9841. val = map_getmapdata(bl->m)->users;
  9842. }
  9843. break;
  9844. case 1:
  9845. val = map_getusers();
  9846. break;
  9847. default:
  9848. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9849. script_pushint(st,0);
  9850. return SCRIPT_CMD_FAILURE;
  9851. }
  9852. script_pushint(st,val);
  9853. return SCRIPT_CMD_SUCCESS;
  9854. }
  9855. /*==========================================
  9856. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9857. *------------------------------------------*/
  9858. BUILDIN_FUNC(getmapguildusers)
  9859. {
  9860. const char *str;
  9861. int16 m;
  9862. int gid;
  9863. int c=0;
  9864. struct guild *g = NULL;
  9865. str=script_getstr(st,2);
  9866. gid=script_getnum(st,3);
  9867. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9868. script_pushint(st,-1);
  9869. return SCRIPT_CMD_SUCCESS;
  9870. }
  9871. g = guild_search(gid);
  9872. if (g){
  9873. unsigned short i;
  9874. for(i = 0; i < g->max_member; i++)
  9875. {
  9876. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9877. c++;
  9878. }
  9879. }
  9880. script_pushint(st,c);
  9881. return SCRIPT_CMD_SUCCESS;
  9882. }
  9883. /*==========================================
  9884. *------------------------------------------*/
  9885. BUILDIN_FUNC(getmapusers)
  9886. {
  9887. const char *str;
  9888. int16 m;
  9889. str=script_getstr(st,2);
  9890. if( (m=map_mapname2mapid(str))< 0){
  9891. script_pushint(st,-1);
  9892. return SCRIPT_CMD_SUCCESS;
  9893. }
  9894. script_pushint(st,map_getmapdata(m)->users);
  9895. return SCRIPT_CMD_SUCCESS;
  9896. }
  9897. /*==========================================
  9898. *------------------------------------------*/
  9899. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9900. {
  9901. int *users=va_arg(ap,int *);
  9902. (*users)++;
  9903. return SCRIPT_CMD_SUCCESS;
  9904. }
  9905. BUILDIN_FUNC(getareausers)
  9906. {
  9907. const char *str;
  9908. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9909. str=script_getstr(st,2);
  9910. x0=script_getnum(st,3);
  9911. y0=script_getnum(st,4);
  9912. x1=script_getnum(st,5);
  9913. y1=script_getnum(st,6);
  9914. if( (m=map_mapname2mapid(str))< 0){
  9915. script_pushint(st,-1);
  9916. return SCRIPT_CMD_SUCCESS;
  9917. }
  9918. map_foreachinallarea(buildin_getareausers_sub,
  9919. m,x0,y0,x1,y1,BL_PC,&users);
  9920. script_pushint(st,users);
  9921. return SCRIPT_CMD_SUCCESS;
  9922. }
  9923. /*==========================================
  9924. * getunits(<type>{,<array_variable>[<first value>]})
  9925. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  9926. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  9927. *------------------------------------------*/
  9928. BUILDIN_FUNC(getunits)
  9929. {
  9930. struct block_list *bl = NULL;
  9931. struct map_session_data *sd = NULL;
  9932. struct script_data *data = NULL;
  9933. char *command = (char *)script_getfuncname(st);
  9934. const char *str;
  9935. const char *name;
  9936. int type = script_getnum(st, 2);
  9937. int size = 0;
  9938. int32 idx, id;
  9939. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  9940. if (!strcmp(command, "getmapunits"))
  9941. {
  9942. str = script_getstr(st, 3);
  9943. if ((m = map_mapname2mapid(str)) < 0) {
  9944. script_pushint(st, -1);
  9945. st->state = END;
  9946. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  9947. return SCRIPT_CMD_FAILURE;
  9948. }
  9949. if (script_hasdata(st, 4))
  9950. data = script_getdata(st, 4);
  9951. }
  9952. else if (!strcmp(command, "getareaunits"))
  9953. {
  9954. str = script_getstr(st, 3);
  9955. if ((m = map_mapname2mapid(str)) < 0) {
  9956. script_pushint(st, -1);
  9957. st->state = END;
  9958. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  9959. return SCRIPT_CMD_FAILURE;
  9960. }
  9961. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  9962. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  9963. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  9964. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  9965. if (script_hasdata(st, 8))
  9966. data = script_getdata(st, 8);
  9967. }
  9968. else
  9969. {
  9970. if (script_hasdata(st, 3))
  9971. data = script_getdata(st, 3);
  9972. }
  9973. if (data)
  9974. {
  9975. if (!data_isreference(data))
  9976. {
  9977. ShowError("buildin_%s: not a variable\n", command);
  9978. script_reportdata(data);
  9979. st->state = END;
  9980. return SCRIPT_CMD_FAILURE;
  9981. }
  9982. id = reference_getid(data);
  9983. idx = reference_getindex(data);
  9984. name = reference_getname(data);
  9985. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  9986. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  9987. return SCRIPT_CMD_FAILURE;
  9988. }
  9989. }
  9990. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  9991. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  9992. {
  9993. 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)))
  9994. {
  9995. if( data ){
  9996. if( is_string_variable( name ) ){
  9997. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  9998. }else{
  9999. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  10000. }
  10001. }
  10002. size++;
  10003. }
  10004. }
  10005. mapit_free(iter);
  10006. script_pushint(st, size);
  10007. return SCRIPT_CMD_SUCCESS;
  10008. }
  10009. /*==========================================
  10010. *------------------------------------------*/
  10011. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10012. {
  10013. t_itemid nameid = va_arg(ap, t_itemid);
  10014. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  10015. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10016. if(drop->item.nameid==nameid)
  10017. (*amount)+=drop->item.amount;
  10018. return SCRIPT_CMD_SUCCESS;
  10019. }
  10020. BUILDIN_FUNC(getareadropitem)
  10021. {
  10022. const char *str;
  10023. int16 m,x0,y0,x1,y1;
  10024. t_itemid nameid;
  10025. unsigned short amount = 0;
  10026. str=script_getstr(st,2);
  10027. x0=script_getnum(st,3);
  10028. y0=script_getnum(st,4);
  10029. x1=script_getnum(st,5);
  10030. y1=script_getnum(st,6);
  10031. if( script_isstring(st, 7) ){
  10032. const char *name = script_getstr(st, 7);
  10033. struct item_data *item_data = itemdb_searchname(name);
  10034. if( item_data )
  10035. nameid=item_data->nameid;
  10036. else{
  10037. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10038. return SCRIPT_CMD_FAILURE;
  10039. }
  10040. }else{
  10041. nameid = script_getnum(st, 7);
  10042. if( !itemdb_exists( nameid ) ){
  10043. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10044. return SCRIPT_CMD_FAILURE;
  10045. }
  10046. }
  10047. if( (m=map_mapname2mapid(str))< 0){
  10048. script_pushint(st,-1);
  10049. return SCRIPT_CMD_SUCCESS;
  10050. }
  10051. map_foreachinallarea(buildin_getareadropitem_sub,
  10052. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10053. script_pushint(st,amount);
  10054. return SCRIPT_CMD_SUCCESS;
  10055. }
  10056. /*==========================================
  10057. *------------------------------------------*/
  10058. BUILDIN_FUNC(enablenpc)
  10059. {
  10060. const char *str = script_getstr(st,2);
  10061. if (npc_enable(str,1))
  10062. return SCRIPT_CMD_SUCCESS;
  10063. return SCRIPT_CMD_FAILURE;
  10064. }
  10065. /*==========================================
  10066. *------------------------------------------*/
  10067. BUILDIN_FUNC(disablenpc)
  10068. {
  10069. const char *str = script_getstr(st,2);
  10070. if (npc_enable(str,0))
  10071. return SCRIPT_CMD_SUCCESS;
  10072. return SCRIPT_CMD_FAILURE;
  10073. }
  10074. /*==========================================
  10075. *------------------------------------------*/
  10076. BUILDIN_FUNC(hideoffnpc)
  10077. {
  10078. const char *str = script_getstr(st,2);
  10079. if (npc_enable(str,2))
  10080. return SCRIPT_CMD_SUCCESS;
  10081. return SCRIPT_CMD_FAILURE;
  10082. }
  10083. /*==========================================
  10084. *------------------------------------------*/
  10085. BUILDIN_FUNC(hideonnpc)
  10086. {
  10087. const char *str = script_getstr(st,2);
  10088. if (npc_enable(str,4))
  10089. return SCRIPT_CMD_SUCCESS;
  10090. return SCRIPT_CMD_FAILURE;
  10091. }
  10092. /* Starts a status effect on the target unit or on the attached player.
  10093. *
  10094. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10095. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10096. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10097. * <flag>: enum e_status_change_start_flags
  10098. */
  10099. BUILDIN_FUNC(sc_start)
  10100. {
  10101. TBL_NPC * nd = map_id2nd(st->oid);
  10102. struct block_list* bl;
  10103. enum sc_type type;
  10104. int tick, val1, val2, val3, val4=0, rate, flag;
  10105. char start_type;
  10106. const char* command = script_getfuncname(st);
  10107. if(strstr(command, "4"))
  10108. start_type = 4;
  10109. else if(strstr(command, "2"))
  10110. start_type = 2;
  10111. else
  10112. start_type = 1;
  10113. type = (sc_type)script_getnum(st,2);
  10114. tick = script_getnum(st,3);
  10115. val1 = script_getnum(st,4);
  10116. //If from NPC we make default flag 1 to be unavoidable
  10117. if(nd && nd->bl.id == fake_nd->bl.id)
  10118. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10119. else
  10120. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10121. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10122. if(script_hasdata(st,(6+start_type)))
  10123. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10124. else
  10125. bl = map_id2bl(st->rid);
  10126. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  10127. {// When there isn't a duration specified, try to get it from the skill_db
  10128. tick = skill_get_time(status_sc2skill(type), val1);
  10129. }
  10130. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10131. bl = map_id2bl(potion_target);
  10132. tick /= 2;// Thrown potions only last half.
  10133. val4 = 1;// Mark that this was a thrown sc_effect
  10134. }
  10135. if(!bl)
  10136. return SCRIPT_CMD_SUCCESS;
  10137. switch(start_type) {
  10138. case 1:
  10139. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10140. break;
  10141. case 2:
  10142. val2 = script_getnum(st,5);
  10143. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10144. break;
  10145. case 4:
  10146. val2 = script_getnum(st,5);
  10147. val3 = script_getnum(st,6);
  10148. val4 = script_getnum(st,7);
  10149. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10150. break;
  10151. }
  10152. return SCRIPT_CMD_SUCCESS;
  10153. }
  10154. /// Ends one or all status effects on the target unit or on the attached player.
  10155. ///
  10156. /// sc_end <effect_id>{,<unit_id>};
  10157. BUILDIN_FUNC(sc_end)
  10158. {
  10159. struct block_list* bl;
  10160. int type;
  10161. type = script_getnum(st, 2);
  10162. if (script_hasdata(st, 3))
  10163. bl = map_id2bl(script_getnum(st, 3));
  10164. else
  10165. bl = map_id2bl(st->rid);
  10166. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10167. bl = map_id2bl(potion_target);
  10168. if (!bl)
  10169. return SCRIPT_CMD_SUCCESS;
  10170. if (type >= 0 && type < SC_MAX) {
  10171. struct status_change *sc = status_get_sc(bl);
  10172. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  10173. if (!sce)
  10174. return SCRIPT_CMD_SUCCESS;
  10175. switch (type) {
  10176. case SC_WEIGHT50:
  10177. case SC_WEIGHT90:
  10178. case SC_NOCHAT:
  10179. case SC_PUSH_CART:
  10180. case SC_ALL_RIDING:
  10181. case SC_STYLE_CHANGE:
  10182. case SC_MONSTER_TRANSFORM:
  10183. case SC_ACTIVE_MONSTER_TRANSFORM:
  10184. case SC_MTF_ASPD:
  10185. case SC_MTF_RANGEATK:
  10186. case SC_MTF_MATK:
  10187. case SC_MTF_MLEATKED:
  10188. case SC_MTF_CRIDAMAGE:
  10189. case SC_MTF_ASPD2:
  10190. case SC_MTF_RANGEATK2:
  10191. case SC_MTF_MATK2:
  10192. case SC_MTF_MHP:
  10193. case SC_MTF_MSP:
  10194. case SC_MTF_PUMPKIN:
  10195. case SC_MTF_HITFLEE:
  10196. case SC_ATTHASTE_CASH:
  10197. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  10198. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  10199. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  10200. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  10201. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  10202. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  10203. return SCRIPT_CMD_SUCCESS;
  10204. default:
  10205. break;
  10206. }
  10207. //This should help status_change_end force disabling the SC in case it has no limit.
  10208. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10209. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  10210. } else
  10211. status_change_clear(bl, 3); // remove all effects
  10212. return SCRIPT_CMD_SUCCESS;
  10213. }
  10214. /**
  10215. * Ends all status effects from any learned skill on the attached player.
  10216. * if <job_id> was given it will end the effect of that class for the attached player
  10217. * sc_end_class {<char_id>{,<job_id>}};
  10218. */
  10219. BUILDIN_FUNC(sc_end_class)
  10220. {
  10221. struct map_session_data *sd;
  10222. uint16 skill_id;
  10223. int class_;
  10224. if (!script_charid2sd(2, sd))
  10225. return SCRIPT_CMD_FAILURE;
  10226. if (script_hasdata(st, 3))
  10227. class_ = script_getnum(st, 3);
  10228. else
  10229. class_ = sd->status.class_;
  10230. if (!pcdb_checkid(class_)) {
  10231. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10232. return SCRIPT_CMD_FAILURE;
  10233. }
  10234. for (int i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[pc_class2idx(class_)][i].skill_id) > 0; i++) {
  10235. enum sc_type sc = status_skill2sc(skill_id);
  10236. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  10237. status_change_end(&sd->bl, sc, INVALID_TIMER);
  10238. }
  10239. return SCRIPT_CMD_SUCCESS;
  10240. }
  10241. /*==========================================
  10242. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10243. *------------------------------------------*/
  10244. BUILDIN_FUNC(getscrate)
  10245. {
  10246. struct block_list *bl;
  10247. int type;
  10248. t_tick rate;
  10249. type=script_getnum(st,2);
  10250. rate=script_getnum(st,3);
  10251. if( script_hasdata(st,4) ) //get for the bl assigned
  10252. bl = map_id2bl(script_getnum(st,4));
  10253. else
  10254. bl = map_id2bl(st->rid);
  10255. if (bl)
  10256. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10257. script_pushint(st,rate);
  10258. return SCRIPT_CMD_SUCCESS;
  10259. }
  10260. /**
  10261. * getstatus(<effect type>{,<type>{,<char_id>}});
  10262. **/
  10263. BUILDIN_FUNC(getstatus)
  10264. {
  10265. int id, type;
  10266. struct map_session_data* sd;
  10267. if (!script_charid2sd(4,sd))
  10268. return SCRIPT_CMD_FAILURE;
  10269. id = script_getnum(st, 2);
  10270. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10271. if( id <= SC_NONE || id >= SC_MAX )
  10272. {// invalid status type given
  10273. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10274. return SCRIPT_CMD_SUCCESS;
  10275. }
  10276. if( sd->sc.count == 0 || !sd->sc.data[id] )
  10277. {// no status is active
  10278. script_pushint(st, 0);
  10279. return SCRIPT_CMD_SUCCESS;
  10280. }
  10281. switch( type )
  10282. {
  10283. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  10284. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  10285. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  10286. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  10287. case 5:
  10288. {
  10289. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  10290. if( timer )
  10291. {// return the amount of time remaining
  10292. script_pushint(st, timer->tick - gettick());
  10293. } else {
  10294. script_pushint(st, -1);
  10295. }
  10296. }
  10297. break;
  10298. default: script_pushint(st, 1); break;
  10299. }
  10300. return SCRIPT_CMD_SUCCESS;
  10301. }
  10302. /*==========================================
  10303. *
  10304. *------------------------------------------*/
  10305. BUILDIN_FUNC(debugmes)
  10306. {
  10307. const char *str;
  10308. str=script_getstr(st,2);
  10309. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10310. return SCRIPT_CMD_SUCCESS;
  10311. }
  10312. BUILDIN_FUNC( errormes ){
  10313. ShowError( "%s\n", script_getstr( st, 2 ) );
  10314. script_reportsrc( st );
  10315. return SCRIPT_CMD_SUCCESS;
  10316. }
  10317. /*==========================================
  10318. *------------------------------------------*/
  10319. BUILDIN_FUNC(catchpet)
  10320. {
  10321. int pet_id;
  10322. TBL_PC *sd;
  10323. if( !script_rid2sd(sd) )
  10324. return SCRIPT_CMD_SUCCESS;
  10325. pet_id= script_getnum(st,2);
  10326. pet_catch_process1(sd,pet_id);
  10327. return SCRIPT_CMD_SUCCESS;
  10328. }
  10329. /*==========================================
  10330. * [orn]
  10331. *------------------------------------------*/
  10332. BUILDIN_FUNC(homunculus_evolution)
  10333. {
  10334. TBL_PC *sd;
  10335. if( !script_rid2sd(sd) )
  10336. return SCRIPT_CMD_SUCCESS;
  10337. if(hom_is_active(sd->hd))
  10338. {
  10339. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10340. hom_evolution(sd->hd);
  10341. else
  10342. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10343. }
  10344. return SCRIPT_CMD_SUCCESS;
  10345. }
  10346. /*==========================================
  10347. * Checks for vaporized morph state
  10348. * and deletes ITEMID_STRANGE_EMBRYO.
  10349. *------------------------------------------*/
  10350. BUILDIN_FUNC(homunculus_mutate)
  10351. {
  10352. int homun_id;
  10353. TBL_PC *sd;
  10354. if( !script_rid2sd(sd) || sd->hd == NULL )
  10355. return SCRIPT_CMD_SUCCESS;
  10356. if(script_hasdata(st,2))
  10357. homun_id = script_getnum(st,2);
  10358. else
  10359. homun_id = 6048 + (rnd() % 4);
  10360. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10361. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10362. int m_id = hom_class2mapid(homun_id);
  10363. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10364. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10365. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10366. hom_call(sd); // Respawn homunculus.
  10367. hom_mutate(sd->hd, homun_id);
  10368. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10369. script_pushint(st, 1);
  10370. return SCRIPT_CMD_SUCCESS;
  10371. } else
  10372. clif_emotion(&sd->bl, ET_SWEAT);
  10373. } else
  10374. clif_emotion(&sd->bl, ET_SWEAT);
  10375. script_pushint(st, 0);
  10376. return SCRIPT_CMD_SUCCESS;
  10377. }
  10378. /*==========================================
  10379. * Puts homunculus into morph state
  10380. * and gives ITEMID_STRANGE_EMBRYO.
  10381. *------------------------------------------*/
  10382. BUILDIN_FUNC(morphembryo)
  10383. {
  10384. struct item item_tmp;
  10385. TBL_PC *sd;
  10386. if( !script_rid2sd(sd) || sd->hd == NULL )
  10387. return SCRIPT_CMD_SUCCESS;
  10388. if( hom_is_active(sd->hd) ) {
  10389. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10390. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10391. char i;
  10392. memset(&item_tmp, 0, sizeof(item_tmp));
  10393. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10394. item_tmp.identify = 1;
  10395. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10396. clif_additem(sd, 0, 0, i);
  10397. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10398. } else {
  10399. hom_vaporize(sd, HOM_ST_MORPH);
  10400. script_pushint(st, 1);
  10401. return SCRIPT_CMD_SUCCESS;
  10402. }
  10403. } else
  10404. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10405. } else
  10406. clif_emotion(&sd->bl, ET_SWEAT);
  10407. script_pushint(st, 0);
  10408. return SCRIPT_CMD_SUCCESS;
  10409. }
  10410. // [Zephyrus]
  10411. BUILDIN_FUNC(homunculus_shuffle)
  10412. {
  10413. TBL_PC *sd;
  10414. if( !script_rid2sd(sd) )
  10415. return SCRIPT_CMD_SUCCESS;
  10416. if(hom_is_active(sd->hd))
  10417. hom_shuffle(sd->hd);
  10418. return SCRIPT_CMD_SUCCESS;
  10419. }
  10420. /*==========================================
  10421. * Check for homunculus state.
  10422. * Return: -1 = No homunculus
  10423. * 0 = Homunculus is active
  10424. * 1 = Homunculus is vaporized (rest)
  10425. * 2 = Homunculus is in morph state
  10426. *------------------------------------------*/
  10427. BUILDIN_FUNC(checkhomcall)
  10428. {
  10429. TBL_PC *sd;
  10430. TBL_HOM *hd;
  10431. if( !script_rid2sd(sd) )
  10432. return SCRIPT_CMD_SUCCESS;
  10433. hd = sd->hd;
  10434. if( !hd )
  10435. script_pushint(st, -1);
  10436. else
  10437. script_pushint(st, hd->homunculus.vaporize);
  10438. return SCRIPT_CMD_SUCCESS;
  10439. }
  10440. //These two functions bring the eA MAPID_* class functionality to scripts.
  10441. BUILDIN_FUNC(eaclass)
  10442. {
  10443. int class_;
  10444. if( script_hasdata(st,2) )
  10445. class_ = script_getnum(st,2);
  10446. else {
  10447. TBL_PC *sd;
  10448. if (!script_charid2sd(3,sd)) {
  10449. script_pushint(st,-1);
  10450. return SCRIPT_CMD_SUCCESS;
  10451. }
  10452. class_ = sd->status.class_;
  10453. }
  10454. script_pushint(st,pc_jobid2mapid(class_));
  10455. return SCRIPT_CMD_SUCCESS;
  10456. }
  10457. BUILDIN_FUNC(roclass)
  10458. {
  10459. int class_ =script_getnum(st,2);
  10460. int sex;
  10461. if( script_hasdata(st,3) )
  10462. sex = script_getnum(st,3);
  10463. else {
  10464. TBL_PC *sd;
  10465. if (st->rid && script_rid2sd(sd))
  10466. sex = sd->status.sex;
  10467. else
  10468. sex = SEX_MALE; //Just use male when not found.
  10469. }
  10470. script_pushint(st,pc_mapid2jobid(class_, sex));
  10471. return SCRIPT_CMD_SUCCESS;
  10472. }
  10473. /*==========================================
  10474. * Tells client to open a hatching window, used for pet incubator
  10475. *------------------------------------------*/
  10476. BUILDIN_FUNC(birthpet)
  10477. {
  10478. TBL_PC *sd;
  10479. if( !script_rid2sd(sd) )
  10480. return SCRIPT_CMD_SUCCESS;
  10481. if( sd->status.pet_id )
  10482. {// do not send egg list, when you already have a pet
  10483. return SCRIPT_CMD_SUCCESS;
  10484. }
  10485. clif_sendegg(sd);
  10486. return SCRIPT_CMD_SUCCESS;
  10487. }
  10488. /**
  10489. * resetlvl <action type>{,<char_id>};
  10490. * @param action_type:
  10491. * 1 : make like after rebirth
  10492. * 2 : blvl,jlvl=1, skillpoint=0
  10493. * 3 : don't reset skill, blvl=1
  10494. * 4 : jlvl=0
  10495. * @author AppleGirl
  10496. **/
  10497. BUILDIN_FUNC(resetlvl)
  10498. {
  10499. TBL_PC *sd;
  10500. int type=script_getnum(st,2);
  10501. if (!script_charid2sd(3,sd))
  10502. return SCRIPT_CMD_FAILURE;
  10503. pc_resetlvl(sd,type);
  10504. return SCRIPT_CMD_SUCCESS;
  10505. }
  10506. /**
  10507. * Reset a player status point
  10508. * resetstatus({<char_id>});
  10509. **/
  10510. BUILDIN_FUNC(resetstatus)
  10511. {
  10512. TBL_PC *sd;
  10513. if (!script_charid2sd(2,sd))
  10514. return SCRIPT_CMD_FAILURE;
  10515. pc_resetstate(sd);
  10516. return SCRIPT_CMD_SUCCESS;
  10517. }
  10518. /**
  10519. * Reset player's skill
  10520. * resetskill({<char_id>});
  10521. **/
  10522. BUILDIN_FUNC(resetskill)
  10523. {
  10524. TBL_PC *sd;
  10525. if (!script_charid2sd(2,sd))
  10526. return SCRIPT_CMD_FAILURE;
  10527. pc_resetskill(sd,1);
  10528. return SCRIPT_CMD_SUCCESS;
  10529. }
  10530. /**
  10531. * Reset SG designated maps
  10532. * resetfeel({<char_id>});
  10533. **/
  10534. BUILDIN_FUNC(resetfeel)
  10535. {
  10536. TBL_PC *sd;
  10537. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10538. return SCRIPT_CMD_FAILURE;
  10539. pc_resetfeel(sd);
  10540. return SCRIPT_CMD_SUCCESS;
  10541. }
  10542. /**
  10543. * Reset SG designated monsters
  10544. * resethate({<char_id>});
  10545. **/
  10546. BUILDIN_FUNC(resethate)
  10547. {
  10548. TBL_PC *sd;
  10549. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10550. return SCRIPT_CMD_FAILURE;
  10551. pc_resethate(sd);
  10552. return SCRIPT_CMD_SUCCESS;
  10553. }
  10554. /**
  10555. * Counts total amount of skill points.
  10556. * skillpointcount({<char_id>})
  10557. **/
  10558. BUILDIN_FUNC(skillpointcount)
  10559. {
  10560. TBL_PC *sd;
  10561. if (!script_charid2sd(2,sd))
  10562. return SCRIPT_CMD_FAILURE;
  10563. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10564. return SCRIPT_CMD_SUCCESS;
  10565. }
  10566. /*==========================================
  10567. *
  10568. *------------------------------------------*/
  10569. BUILDIN_FUNC(changebase)
  10570. {
  10571. TBL_PC *sd=NULL;
  10572. int vclass;
  10573. if( !script_mapid2sd(3,sd) )
  10574. return SCRIPT_CMD_SUCCESS;
  10575. vclass = script_getnum(st,2);
  10576. if(vclass == JOB_WEDDING)
  10577. {
  10578. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10579. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10580. )
  10581. return SCRIPT_CMD_SUCCESS;
  10582. }
  10583. if(!sd->disguise && vclass != sd->vd.class_) {
  10584. status_set_viewdata(&sd->bl, vclass);
  10585. //Updated client view. Base, Weapon and Cloth Colors.
  10586. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10587. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10588. if (sd->vd.cloth_color)
  10589. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10590. if (sd->vd.body_style)
  10591. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10592. clif_skillinfoblock(sd);
  10593. }
  10594. return SCRIPT_CMD_SUCCESS;
  10595. }
  10596. /**
  10597. * Change account sex and unequip all item and request for a changesex to char-serv
  10598. * changesex({<char_id>});
  10599. */
  10600. BUILDIN_FUNC(changesex)
  10601. {
  10602. int i;
  10603. TBL_PC *sd = NULL;
  10604. if (!script_charid2sd(2,sd))
  10605. return SCRIPT_CMD_FAILURE;
  10606. pc_resetskill(sd,4);
  10607. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10608. for(i = 0; i < EQI_MAX; i++) {
  10609. if (sd->equip_index[i] >= 0)
  10610. pc_unequipitem(sd, sd->equip_index[i], 3);
  10611. }
  10612. chrif_changesex(sd, true);
  10613. return SCRIPT_CMD_SUCCESS;
  10614. }
  10615. /**
  10616. * Change character's sex and unequip all item and request for a changesex to char-serv
  10617. * changecharsex({<char_id>});
  10618. */
  10619. BUILDIN_FUNC(changecharsex)
  10620. {
  10621. #if PACKETVER >= 20141016
  10622. int i;
  10623. TBL_PC *sd = NULL;
  10624. if (!script_charid2sd(2,sd))
  10625. return SCRIPT_CMD_FAILURE;
  10626. pc_resetskill(sd,4);
  10627. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10628. for (i = 0; i < EQI_MAX; i++) {
  10629. if (sd->equip_index[i] >= 0)
  10630. pc_unequipitem(sd, sd->equip_index[i], 3);
  10631. }
  10632. chrif_changesex(sd, false);
  10633. return SCRIPT_CMD_SUCCESS;
  10634. #else
  10635. return SCRIPT_CMD_FAILURE;
  10636. #endif
  10637. }
  10638. /*==========================================
  10639. * Works like 'announce' but outputs in the common chat window
  10640. *------------------------------------------*/
  10641. BUILDIN_FUNC(globalmes)
  10642. {
  10643. struct block_list *bl = map_id2bl(st->oid);
  10644. struct npc_data *nd = (struct npc_data *)bl;
  10645. const char *name=NULL,*mes;
  10646. mes=script_getstr(st,2);
  10647. if(mes==NULL)
  10648. return SCRIPT_CMD_SUCCESS;
  10649. if(script_hasdata(st,3)){ // npc name to display
  10650. name=script_getstr(st,3);
  10651. } else {
  10652. name=nd->name; //use current npc name
  10653. }
  10654. npc_globalmessage(name,mes); // broadcast to all players connected
  10655. return SCRIPT_CMD_SUCCESS;
  10656. }
  10657. /////////////////////////////////////////////////////////////////////
  10658. // NPC waiting room (chat room)
  10659. //
  10660. /// Creates a waiting room (chat room) for this npc.
  10661. ///
  10662. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10663. BUILDIN_FUNC(waitingroom)
  10664. {
  10665. struct npc_data* nd;
  10666. const char* title = script_getstr(st, 2);
  10667. int limit = script_getnum(st, 3);
  10668. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10669. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10670. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10671. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10672. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10673. nd = (struct npc_data *)map_id2bl(st->oid);
  10674. if( nd != NULL )
  10675. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10676. return SCRIPT_CMD_SUCCESS;
  10677. }
  10678. /// Removes the waiting room of the current or target npc.
  10679. ///
  10680. /// delwaitingroom "<npc_name>";
  10681. /// delwaitingroom;
  10682. BUILDIN_FUNC(delwaitingroom)
  10683. {
  10684. struct npc_data* nd;
  10685. if( script_hasdata(st,2) )
  10686. nd = npc_name2id(script_getstr(st, 2));
  10687. else
  10688. nd = (struct npc_data *)map_id2bl(st->oid);
  10689. if( nd != NULL )
  10690. chat_deletenpcchat(nd);
  10691. return SCRIPT_CMD_SUCCESS;
  10692. }
  10693. /// Kick the specified player from the waiting room of the target npc.
  10694. ///
  10695. /// waitingroomkick "<npc_name>", <kickusername>;
  10696. BUILDIN_FUNC(waitingroomkick)
  10697. {
  10698. struct npc_data* nd;
  10699. struct chat_data* cd;
  10700. const char* kickusername;
  10701. nd = npc_name2id(script_getstr(st,2));
  10702. kickusername = script_getstr(st,3);
  10703. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10704. chat_npckickchat(cd, kickusername);
  10705. return SCRIPT_CMD_SUCCESS;
  10706. }
  10707. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10708. /// Num users stored in .@waitingroom_usercount
  10709. ///
  10710. /// getwaitingroomusers "<npc_name>";
  10711. BUILDIN_FUNC(getwaitingroomusers)
  10712. {
  10713. struct npc_data* nd;
  10714. struct chat_data* cd;
  10715. int i, j=0;
  10716. if( script_hasdata(st,2) )
  10717. nd = npc_name2id(script_getstr(st, 2));
  10718. else
  10719. nd = (struct npc_data *)map_id2bl(st->oid);
  10720. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10721. for(i = 0; i < cd->users; ++i) {
  10722. setd_sub_num( st, NULL, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, NULL );
  10723. j++;
  10724. }
  10725. setd_sub_num( st, NULL, ".@waitingroom_usercount", 0, j, NULL );
  10726. }
  10727. return SCRIPT_CMD_SUCCESS;
  10728. }
  10729. /// Kicks all the players from the waiting room of the current or target npc.
  10730. ///
  10731. /// kickwaitingroomall "<npc_name>";
  10732. /// kickwaitingroomall;
  10733. BUILDIN_FUNC(waitingroomkickall)
  10734. {
  10735. struct npc_data* nd;
  10736. struct chat_data* cd;
  10737. if( script_hasdata(st,2) )
  10738. nd = npc_name2id(script_getstr(st,2));
  10739. else
  10740. nd = (struct npc_data *)map_id2bl(st->oid);
  10741. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10742. chat_npckickall(cd);
  10743. return SCRIPT_CMD_SUCCESS;
  10744. }
  10745. /// Enables the waiting room event of the current or target npc.
  10746. ///
  10747. /// enablewaitingroomevent "<npc_name>";
  10748. /// enablewaitingroomevent;
  10749. BUILDIN_FUNC(enablewaitingroomevent)
  10750. {
  10751. struct npc_data* nd;
  10752. struct chat_data* cd;
  10753. if( script_hasdata(st,2) )
  10754. nd = npc_name2id(script_getstr(st, 2));
  10755. else
  10756. nd = (struct npc_data *)map_id2bl(st->oid);
  10757. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10758. chat_enableevent(cd);
  10759. return SCRIPT_CMD_SUCCESS;
  10760. }
  10761. /// Disables the waiting room event of the current or target npc.
  10762. ///
  10763. /// disablewaitingroomevent "<npc_name>";
  10764. /// disablewaitingroomevent;
  10765. BUILDIN_FUNC(disablewaitingroomevent)
  10766. {
  10767. struct npc_data *nd;
  10768. struct chat_data *cd;
  10769. if( script_hasdata(st,2) )
  10770. nd = npc_name2id(script_getstr(st, 2));
  10771. else
  10772. nd = (struct npc_data *)map_id2bl(st->oid);
  10773. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10774. chat_disableevent(cd);
  10775. return SCRIPT_CMD_SUCCESS;
  10776. }
  10777. /// Returns info on the waiting room of the current or target npc.
  10778. /// Returns -1 if the type unknown
  10779. /// <type>=0 : current number of users
  10780. /// <type>=1 : maximum number of users allowed
  10781. /// <type>=2 : the number of users that trigger the event
  10782. /// <type>=3 : if the trigger is disabled
  10783. /// <type>=4 : the title of the waiting room
  10784. /// <type>=5 : the password of the waiting room
  10785. /// <type>=16 : the name of the waiting room event
  10786. /// <type>=32 : if the waiting room is full
  10787. /// <type>=33 : if there are enough users to trigger the event
  10788. ///
  10789. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10790. /// getwaitingroomstate(<type>) -> <info>
  10791. BUILDIN_FUNC(getwaitingroomstate)
  10792. {
  10793. struct npc_data *nd;
  10794. struct chat_data *cd;
  10795. int type;
  10796. type = script_getnum(st,2);
  10797. if( script_hasdata(st,3) )
  10798. nd = npc_name2id(script_getstr(st, 3));
  10799. else
  10800. nd = (struct npc_data *)map_id2bl(st->oid);
  10801. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10802. {
  10803. script_pushint(st, -1);
  10804. return SCRIPT_CMD_SUCCESS;
  10805. }
  10806. switch(type)
  10807. {
  10808. case 0: script_pushint(st, cd->users); break;
  10809. case 1: script_pushint(st, cd->limit); break;
  10810. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10811. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10812. case 4: script_pushstrcopy(st, cd->title); break;
  10813. case 5: script_pushstrcopy(st, cd->pass); break;
  10814. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10815. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10816. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10817. default: script_pushint(st, -1); break;
  10818. }
  10819. return SCRIPT_CMD_SUCCESS;
  10820. }
  10821. /// Warps the trigger or target amount of players to the target map and position.
  10822. /// Players are automatically removed from the waiting room.
  10823. /// Those waiting the longest will get warped first.
  10824. /// The target map can be "Random" for a random position in the current map,
  10825. /// and "SavePoint" for the savepoint map+position.
  10826. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10827. ///
  10828. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10829. /// The total number of teleported players is put into $@warpwaitingpcnum
  10830. ///
  10831. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10832. /// warpwaitingpc "<map name>",<x>,<y>;
  10833. BUILDIN_FUNC(warpwaitingpc)
  10834. {
  10835. int x;
  10836. int y;
  10837. int i;
  10838. int n;
  10839. const char* map_name;
  10840. struct npc_data* nd;
  10841. struct chat_data* cd;
  10842. nd = (struct npc_data *)map_id2bl(st->oid);
  10843. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10844. return SCRIPT_CMD_SUCCESS;
  10845. map_name = script_getstr(st,2);
  10846. x = script_getnum(st,3);
  10847. y = script_getnum(st,4);
  10848. n = cd->trigger&0x7f;
  10849. if( script_hasdata(st,5) )
  10850. n = script_getnum(st,5);
  10851. for( i = 0; i < n && cd->users > 0; i++ )
  10852. {
  10853. TBL_PC* sd = cd->usersd[0];
  10854. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  10855. {// can't teleport on this map
  10856. break;
  10857. }
  10858. if( cd->zeny )
  10859. {// fee set
  10860. if( (uint32)sd->status.zeny < cd->zeny )
  10861. {// no zeny to cover set fee
  10862. break;
  10863. }
  10864. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10865. }
  10866. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10867. if( strcmp(map_name,"Random") == 0 )
  10868. pc_randomwarp(sd,CLR_TELEPORT,true);
  10869. else if( strcmp(map_name,"SavePoint") == 0 )
  10870. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10871. else
  10872. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10873. }
  10874. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10875. return SCRIPT_CMD_SUCCESS;
  10876. }
  10877. /////////////////////////////////////////////////////////////////////
  10878. // ...
  10879. //
  10880. /// Detaches a character from a script.
  10881. ///
  10882. /// @param st Script state to detach the character from.
  10883. void script_detach_rid(struct script_state* st)
  10884. {
  10885. if(st->rid)
  10886. {
  10887. script_detach_state(st, false);
  10888. st->rid = 0;
  10889. }
  10890. }
  10891. /*=========================================================================
  10892. * Attaches a set of RIDs to the current script. [digitalhamster]
  10893. * addrid(<type>{,<flag>{,<parameters>}});
  10894. * <type>:
  10895. * 0 : All players in the server.
  10896. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10897. * 2 : Party members of a specified party ID.
  10898. * [ Parameters: <party id> ]
  10899. * 3 : Guild members of a specified guild ID.
  10900. * [ Parameters: <guild id> ]
  10901. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10902. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10903. * 5 : All players in the map.
  10904. * [ Parameters: "<map name>" ]
  10905. * Account ID: The specified account ID.
  10906. * <flag>:
  10907. * 0 : Players are always attached. (default)
  10908. * 1 : Players currently running another script will not be attached.
  10909. *-------------------------------------------------------------------------*/
  10910. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10911. {
  10912. int forceflag;
  10913. struct map_session_data *sd = (TBL_PC *)bl;
  10914. struct script_state* st;
  10915. st = va_arg(ap,struct script_state*);
  10916. forceflag = va_arg(ap,int);
  10917. if(!forceflag || !sd->st)
  10918. if(sd->status.account_id != st->rid)
  10919. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10920. return 0;
  10921. }
  10922. BUILDIN_FUNC(addrid)
  10923. {
  10924. struct s_mapiterator* iter;
  10925. struct block_list *bl;
  10926. TBL_PC *sd;
  10927. if(st->rid < 1) {
  10928. st->state = END;
  10929. bl = map_id2bl(st->oid);
  10930. } else
  10931. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10932. iter = mapit_getallusers();
  10933. switch(script_getnum(st,2)) {
  10934. case 0:
  10935. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10936. if(!script_getnum(st,3) || !sd->st)
  10937. if(sd->status.account_id != st->rid) //attached player already runs.
  10938. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10939. }
  10940. break;
  10941. case 1:
  10942. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10943. if(!script_getnum(st,3) || !sd->st)
  10944. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10945. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10946. }
  10947. break;
  10948. case 2:
  10949. if(script_getnum(st,4) == 0) {
  10950. script_pushint(st,0);
  10951. mapit_free(iter);
  10952. return SCRIPT_CMD_SUCCESS;
  10953. }
  10954. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10955. if(!script_getnum(st,3) || !sd->st)
  10956. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10957. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10958. }
  10959. break;
  10960. case 3:
  10961. if(script_getnum(st,4) == 0) {
  10962. script_pushint(st,0);
  10963. mapit_free(iter);
  10964. return SCRIPT_CMD_SUCCESS;
  10965. }
  10966. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10967. if(!script_getnum(st,3) || !sd->st)
  10968. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10969. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10970. }
  10971. break;
  10972. case 4:
  10973. map_foreachinallarea(buildin_addrid_sub,
  10974. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10975. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10976. break;
  10977. case 5:
  10978. if (script_getstr(st, 4) == NULL) {
  10979. script_pushint(st, 0);
  10980. mapit_free(iter);
  10981. return SCRIPT_CMD_FAILURE;
  10982. }
  10983. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10984. script_pushint(st, 0);
  10985. mapit_free(iter);
  10986. return SCRIPT_CMD_FAILURE;
  10987. }
  10988. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10989. break;
  10990. default:
  10991. mapit_free(iter);
  10992. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10993. script_pushint(st,0);
  10994. return SCRIPT_CMD_SUCCESS;
  10995. }
  10996. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10997. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10998. script_pushint(st,1);
  10999. }
  11000. return SCRIPT_CMD_SUCCESS;
  11001. }
  11002. mapit_free(iter);
  11003. script_pushint(st,1);
  11004. return SCRIPT_CMD_SUCCESS;
  11005. }
  11006. /*==========================================
  11007. * Attach sd char id to script and detach current one if any
  11008. *------------------------------------------*/
  11009. BUILDIN_FUNC(attachrid)
  11010. {
  11011. int rid = script_getnum(st,2);
  11012. bool force;
  11013. if( script_hasdata(st,3) ){
  11014. force = script_getnum(st,3) != 0;
  11015. }else{
  11016. force = true;
  11017. }
  11018. struct map_session_data* sd = map_id2sd(rid);
  11019. if( sd != NULL && ( !sd->npc_id || force ) ){
  11020. script_detach_rid(st);
  11021. st->rid = rid;
  11022. script_attach_state(st);
  11023. script_pushint(st,true);
  11024. }else{
  11025. script_pushint(st,false);
  11026. }
  11027. return SCRIPT_CMD_SUCCESS;
  11028. }
  11029. /*==========================================
  11030. * Detach script to rid
  11031. *------------------------------------------*/
  11032. BUILDIN_FUNC(detachrid)
  11033. {
  11034. script_detach_rid(st);
  11035. return SCRIPT_CMD_SUCCESS;
  11036. }
  11037. /*==========================================
  11038. * Chk if account connected, (and charid from account if specified)
  11039. *------------------------------------------*/
  11040. BUILDIN_FUNC(isloggedin)
  11041. {
  11042. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11043. if (script_hasdata(st,3) && sd &&
  11044. sd->status.char_id != script_getnum(st,3))
  11045. sd = NULL;
  11046. push_val(st->stack,C_INT,sd!=NULL);
  11047. return SCRIPT_CMD_SUCCESS;
  11048. }
  11049. /*==========================================
  11050. *
  11051. *------------------------------------------*/
  11052. BUILDIN_FUNC(setmapflagnosave)
  11053. {
  11054. int16 m,x,y;
  11055. unsigned short mapindex;
  11056. const char *str,*str2;
  11057. union u_mapflag_args args = {};
  11058. str=script_getstr(st,2);
  11059. str2=script_getstr(st,3);
  11060. x=script_getnum(st,4);
  11061. y=script_getnum(st,5);
  11062. m = map_mapname2mapid(str);
  11063. mapindex = mapindex_name2id(str2);
  11064. if(m < 0) {
  11065. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11066. return SCRIPT_CMD_FAILURE;
  11067. }
  11068. args.nosave.map = mapindex;
  11069. args.nosave.x = x;
  11070. args.nosave.y = y;
  11071. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11072. return SCRIPT_CMD_SUCCESS;
  11073. }
  11074. BUILDIN_FUNC(getmapflag)
  11075. {
  11076. int16 m;
  11077. int mf;
  11078. const char *str;
  11079. str=script_getstr(st,2);
  11080. m = map_mapname2mapid(str);
  11081. if (m < 0) {
  11082. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11083. return SCRIPT_CMD_FAILURE;
  11084. }
  11085. mf = script_getnum(st, 3);
  11086. if( mf < MF_MIN || mf > MF_MAX ){
  11087. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11088. return SCRIPT_CMD_FAILURE;
  11089. }
  11090. union u_mapflag_args args = {};
  11091. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11092. args.flag_val = SKILLDMG_MAX;
  11093. else
  11094. FETCH(4, args.flag_val);
  11095. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11096. return SCRIPT_CMD_SUCCESS;
  11097. }
  11098. BUILDIN_FUNC(setmapflag)
  11099. {
  11100. int16 m;
  11101. int mf;
  11102. const char *str;
  11103. str = script_getstr(st,2);
  11104. m = map_mapname2mapid(str);
  11105. if (m < 0) {
  11106. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11107. return SCRIPT_CMD_FAILURE;
  11108. }
  11109. mf = script_getnum(st, 3);
  11110. if( mf < MF_MIN || mf > MF_MAX ){
  11111. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11112. return SCRIPT_CMD_FAILURE;
  11113. }
  11114. union u_mapflag_args args = {};
  11115. switch(mf) {
  11116. case MF_SKILL_DAMAGE:
  11117. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11118. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11119. else {
  11120. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11121. return SCRIPT_CMD_FAILURE;
  11122. }
  11123. break;
  11124. case MF_SKILL_DURATION:
  11125. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11126. args.skill_duration.skill_id = script_getnum(st, 4);
  11127. if (!skill_get_index(args.skill_duration.skill_id)) {
  11128. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11129. return SCRIPT_CMD_FAILURE;
  11130. }
  11131. args.skill_duration.per = script_getnum(st, 5);
  11132. } else {
  11133. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11134. return SCRIPT_CMD_FAILURE;
  11135. }
  11136. break;
  11137. case MF_NOSAVE: // Assume setting "SavePoint"
  11138. args.nosave.map = 0;
  11139. args.nosave.x = -1;
  11140. args.nosave.y = -1;
  11141. break;
  11142. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11143. args.nightmaredrop.drop_id = -1;
  11144. args.nightmaredrop.drop_per = 300;
  11145. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11146. break;
  11147. default:
  11148. FETCH(4, args.flag_val);
  11149. break;
  11150. }
  11151. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11152. return SCRIPT_CMD_SUCCESS;
  11153. }
  11154. BUILDIN_FUNC(removemapflag)
  11155. {
  11156. int16 m;
  11157. int mf;
  11158. const char *str;
  11159. union u_mapflag_args args = {};
  11160. str = script_getstr(st, 2);
  11161. m = map_mapname2mapid(str);
  11162. if (m < 0) {
  11163. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11164. return SCRIPT_CMD_FAILURE;
  11165. }
  11166. mf = script_getnum(st, 3);
  11167. if( mf < MF_MIN || mf > MF_MAX ){
  11168. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11169. return SCRIPT_CMD_FAILURE;
  11170. }
  11171. FETCH(4, args.flag_val);
  11172. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11173. return SCRIPT_CMD_SUCCESS;
  11174. }
  11175. BUILDIN_FUNC(pvpon)
  11176. {
  11177. int16 m;
  11178. const char *str = script_getstr(st,2);
  11179. m = map_mapname2mapid(str);
  11180. if (m < 0) {
  11181. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11182. return SCRIPT_CMD_FAILURE;
  11183. }
  11184. if (!map_getmapflag(m, MF_PVP))
  11185. map_setmapflag(m, MF_PVP, true);
  11186. return SCRIPT_CMD_SUCCESS;
  11187. }
  11188. BUILDIN_FUNC(pvpoff)
  11189. {
  11190. int16 m;
  11191. const char *str = script_getstr(st,2);
  11192. m = map_mapname2mapid(str);
  11193. if (m < 0) {
  11194. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11195. return SCRIPT_CMD_FAILURE;
  11196. }
  11197. if (map_getmapflag(m, MF_PVP))
  11198. map_setmapflag(m, MF_PVP, false);
  11199. return SCRIPT_CMD_SUCCESS;
  11200. }
  11201. BUILDIN_FUNC(gvgon)
  11202. {
  11203. int16 m;
  11204. const char *str = script_getstr(st,2);
  11205. m = map_mapname2mapid(str);
  11206. if (m < 0) {
  11207. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11208. return SCRIPT_CMD_FAILURE;
  11209. }
  11210. if (!map_getmapflag(m, MF_GVG))
  11211. map_setmapflag(m, MF_GVG, true);
  11212. return SCRIPT_CMD_SUCCESS;
  11213. }
  11214. BUILDIN_FUNC(gvgoff)
  11215. {
  11216. int16 m;
  11217. const char *str = script_getstr(st,2);
  11218. m = map_mapname2mapid(str);
  11219. if (m < 0) {
  11220. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11221. return SCRIPT_CMD_FAILURE;
  11222. }
  11223. if (map_getmapflag(m, MF_GVG))
  11224. map_setmapflag(m, MF_GVG, false);
  11225. return SCRIPT_CMD_SUCCESS;
  11226. }
  11227. BUILDIN_FUNC(gvgon3)
  11228. {
  11229. int16 m;
  11230. const char *str = script_getstr(st,2);
  11231. m = map_mapname2mapid(str);
  11232. if (m < 0) {
  11233. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11234. return SCRIPT_CMD_FAILURE;
  11235. }
  11236. if (!map_getmapflag(m, MF_GVG_TE))
  11237. map_setmapflag(m, MF_GVG_TE, true);
  11238. return SCRIPT_CMD_SUCCESS;
  11239. }
  11240. BUILDIN_FUNC(gvgoff3)
  11241. {
  11242. int16 m;
  11243. const char *str = script_getstr(st,2);
  11244. m = map_mapname2mapid(str);
  11245. if (m < 0) {
  11246. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11247. return SCRIPT_CMD_FAILURE;
  11248. }
  11249. if (map_getmapflag(m, MF_GVG_TE))
  11250. map_setmapflag(m, MF_GVG_TE, false);
  11251. return SCRIPT_CMD_SUCCESS;
  11252. }
  11253. /**
  11254. * Shows an emotion on top of a NPC by default or the given GID
  11255. * emotion <emotion ID>{,<target ID>};
  11256. */
  11257. BUILDIN_FUNC(emotion)
  11258. {
  11259. struct block_list *bl = NULL;
  11260. int type = script_getnum(st,2);
  11261. if (type < ET_SURPRISE || type >= ET_MAX) {
  11262. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11263. return SCRIPT_CMD_FAILURE;
  11264. }
  11265. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11266. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11267. return SCRIPT_CMD_FAILURE;
  11268. }
  11269. if (!bl)
  11270. bl = map_id2bl(st->oid);
  11271. clif_emotion(bl, type);
  11272. return SCRIPT_CMD_SUCCESS;
  11273. }
  11274. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11275. {
  11276. int16 m=va_arg(ap,int);
  11277. int g_id=va_arg(ap,int);
  11278. int flag=va_arg(ap,int);
  11279. if(!sd || sd->bl.m != m)
  11280. return 0;
  11281. if(
  11282. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11283. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11284. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11285. )
  11286. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11287. return 1;
  11288. }
  11289. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11290. {
  11291. struct mob_data *md=(struct mob_data *)bl;
  11292. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11293. status_kill(bl);
  11294. return 1;
  11295. }
  11296. /*
  11297. * Function to kick guild members out of a map and to their save points.
  11298. * m : mapid
  11299. * g_id : owner guild id
  11300. * flag & 1 : Warp out owners
  11301. * flag & 2 : Warp out outsiders
  11302. * flag & 4 : reserved for mobs
  11303. * */
  11304. BUILDIN_FUNC(maprespawnguildid)
  11305. {
  11306. const char *mapname=script_getstr(st,2);
  11307. int g_id=script_getnum(st,3);
  11308. int flag=script_getnum(st,4);
  11309. int16 m=map_mapname2mapid(mapname);
  11310. if(m == -1)
  11311. return SCRIPT_CMD_SUCCESS;
  11312. //Catch ALL players (in case some are 'between maps' on execution time)
  11313. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11314. if (flag&4) //Remove script mobs.
  11315. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11316. return SCRIPT_CMD_SUCCESS;
  11317. }
  11318. /// Siege commands
  11319. /**
  11320. * Start WoE:FE
  11321. * agitstart();
  11322. */
  11323. BUILDIN_FUNC(agitstart)
  11324. {
  11325. guild_agit_start();
  11326. return SCRIPT_CMD_SUCCESS;
  11327. }
  11328. /**
  11329. * End WoE:FE
  11330. * agitend();
  11331. */
  11332. BUILDIN_FUNC(agitend)
  11333. {
  11334. guild_agit_end();
  11335. return SCRIPT_CMD_SUCCESS;
  11336. }
  11337. /**
  11338. * Start WoE:SE
  11339. * agitstart2();
  11340. */
  11341. BUILDIN_FUNC(agitstart2)
  11342. {
  11343. guild_agit2_start();
  11344. return SCRIPT_CMD_SUCCESS;
  11345. }
  11346. /**
  11347. * End WoE:SE
  11348. * agitend();
  11349. */
  11350. BUILDIN_FUNC(agitend2)
  11351. {
  11352. guild_agit2_end();
  11353. return SCRIPT_CMD_SUCCESS;
  11354. }
  11355. /**
  11356. * Start WoE:TE
  11357. * agitstart3();
  11358. */
  11359. BUILDIN_FUNC(agitstart3)
  11360. {
  11361. guild_agit3_start();
  11362. return SCRIPT_CMD_SUCCESS;
  11363. }
  11364. /**
  11365. * End WoE:TE
  11366. * agitend3();
  11367. */
  11368. BUILDIN_FUNC(agitend3)
  11369. {
  11370. guild_agit3_end();
  11371. return SCRIPT_CMD_SUCCESS;
  11372. }
  11373. /**
  11374. * Returns whether WoE:FE is on or off.
  11375. * agitcheck();
  11376. */
  11377. BUILDIN_FUNC(agitcheck)
  11378. {
  11379. script_pushint(st, agit_flag);
  11380. return SCRIPT_CMD_SUCCESS;
  11381. }
  11382. /**
  11383. * Returns whether WoE:SE is on or off.
  11384. * agitcheck2();
  11385. */
  11386. BUILDIN_FUNC(agitcheck2)
  11387. {
  11388. script_pushint(st, agit2_flag);
  11389. return SCRIPT_CMD_SUCCESS;
  11390. }
  11391. /**
  11392. * Returns whether WoE:TE is on or off.
  11393. * agitcheck3();
  11394. */
  11395. BUILDIN_FUNC(agitcheck3)
  11396. {
  11397. script_pushint(st, agit3_flag);
  11398. return SCRIPT_CMD_SUCCESS;
  11399. }
  11400. /// Sets the guild_id of this npc.
  11401. ///
  11402. /// flagemblem <guild_id>;
  11403. BUILDIN_FUNC(flagemblem)
  11404. {
  11405. TBL_NPC* nd;
  11406. int g_id = script_getnum(st,2);
  11407. if(g_id < 0)
  11408. return SCRIPT_CMD_SUCCESS;
  11409. nd = (TBL_NPC*)map_id2nd(st->oid);
  11410. if( nd == NULL ) {
  11411. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11412. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11413. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11414. } else {
  11415. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11416. nd->u.scr.guild_id = g_id;
  11417. clif_guild_emblem_area(&nd->bl);
  11418. /* guild flag caching */
  11419. if( g_id ) /* adding a id */
  11420. guild_flag_add(nd);
  11421. else if( changed ) /* removing a flag */
  11422. guild_flag_remove(nd);
  11423. }
  11424. return SCRIPT_CMD_SUCCESS;
  11425. }
  11426. BUILDIN_FUNC(getcastlename)
  11427. {
  11428. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11429. struct guild_castle* gc = guild_mapname2gc(mapname);
  11430. const char* name = (gc) ? gc->castle_name : "";
  11431. script_pushstrcopy(st,name);
  11432. return SCRIPT_CMD_SUCCESS;
  11433. }
  11434. BUILDIN_FUNC(getcastledata)
  11435. {
  11436. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11437. int index = script_getnum(st,3);
  11438. struct guild_castle *gc = guild_mapname2gc(mapname);
  11439. if (gc == NULL) {
  11440. script_pushint(st,0);
  11441. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11442. return SCRIPT_CMD_FAILURE;
  11443. }
  11444. switch (index) {
  11445. case CD_GUILD_ID:
  11446. script_pushint(st,gc->guild_id); break;
  11447. case CD_CURRENT_ECONOMY:
  11448. script_pushint(st,gc->economy); break;
  11449. case CD_CURRENT_DEFENSE:
  11450. script_pushint(st,gc->defense); break;
  11451. case CD_INVESTED_ECONOMY:
  11452. script_pushint(st,gc->triggerE); break;
  11453. case CD_INVESTED_DEFENSE:
  11454. script_pushint(st,gc->triggerD); break;
  11455. case CD_NEXT_TIME:
  11456. script_pushint(st,gc->nextTime); break;
  11457. case CD_PAY_TIME:
  11458. script_pushint(st,gc->payTime); break;
  11459. case CD_CREATE_TIME:
  11460. script_pushint(st,gc->createTime); break;
  11461. case CD_ENABLED_KAFRA:
  11462. script_pushint(st,gc->visibleC); break;
  11463. default:
  11464. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11465. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11466. break;
  11467. }
  11468. script_pushint(st,0);
  11469. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11470. return SCRIPT_CMD_FAILURE;
  11471. }
  11472. return SCRIPT_CMD_SUCCESS;
  11473. }
  11474. BUILDIN_FUNC(setcastledata)
  11475. {
  11476. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11477. int index = script_getnum(st,3);
  11478. int value = script_getnum(st,4);
  11479. struct guild_castle *gc = guild_mapname2gc(mapname);
  11480. if (gc == NULL) {
  11481. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11482. return SCRIPT_CMD_FAILURE;
  11483. }
  11484. if (index <= CD_NONE || index >= CD_MAX) {
  11485. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11486. return SCRIPT_CMD_FAILURE;
  11487. }
  11488. guild_castledatasave(gc->castle_id, index, value);
  11489. return SCRIPT_CMD_SUCCESS;
  11490. }
  11491. /* =====================================================================
  11492. * ---------------------------------------------------------------------*/
  11493. BUILDIN_FUNC(requestguildinfo)
  11494. {
  11495. int guild_id=script_getnum(st,2);
  11496. const char *event=NULL;
  11497. if( script_hasdata(st,3) ){
  11498. event=script_getstr(st,3);
  11499. check_event(st, event);
  11500. }
  11501. if(guild_id>0)
  11502. guild_npc_request_info(guild_id,event);
  11503. return SCRIPT_CMD_SUCCESS;
  11504. }
  11505. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11506. /// getequipcardcnt(<equipment slot>);
  11507. BUILDIN_FUNC(getequipcardcnt)
  11508. {
  11509. int i=-1,j,num;
  11510. TBL_PC *sd;
  11511. int count;
  11512. if( !script_rid2sd(sd) )
  11513. return SCRIPT_CMD_SUCCESS;
  11514. num=script_getnum(st,2);
  11515. if (equip_index_check(num))
  11516. i=pc_checkequip(sd,equip_bitmask[num]);
  11517. if (i < 0 || !sd->inventory_data[i]) {
  11518. script_pushint(st,0);
  11519. return SCRIPT_CMD_SUCCESS;
  11520. }
  11521. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11522. {
  11523. script_pushint(st,0);
  11524. return SCRIPT_CMD_SUCCESS;
  11525. }
  11526. count = 0;
  11527. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11528. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11529. count++;
  11530. script_pushint(st,count);
  11531. return SCRIPT_CMD_SUCCESS;
  11532. }
  11533. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11534. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11535. /// successremovecards <slot>;
  11536. BUILDIN_FUNC(successremovecards) {
  11537. int i=-1,c,cardflag=0;
  11538. TBL_PC* sd;
  11539. int num;
  11540. if( !script_rid2sd(sd) )
  11541. return SCRIPT_CMD_SUCCESS;
  11542. num = script_getnum(st,2);
  11543. if (equip_index_check(num))
  11544. i=pc_checkequip(sd,equip_bitmask[num]);
  11545. if (i < 0 || !sd->inventory_data[i]) {
  11546. return SCRIPT_CMD_SUCCESS;
  11547. }
  11548. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11549. return SCRIPT_CMD_SUCCESS;
  11550. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11551. 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
  11552. unsigned char flag = 0;
  11553. struct item item_tmp;
  11554. memset(&item_tmp,0,sizeof(item_tmp));
  11555. cardflag = 1;
  11556. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11557. item_tmp.identify = 1;
  11558. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11559. clif_additem(sd,0,0,flag);
  11560. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11561. }
  11562. }
  11563. }
  11564. if(cardflag == 1) {//if card was remove remplace item with no card
  11565. unsigned char flag = 0;
  11566. struct item item_tmp;
  11567. memset(&item_tmp,0,sizeof(item_tmp));
  11568. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11569. item_tmp.identify = 1;
  11570. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11571. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11572. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11573. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11574. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11575. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11576. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11577. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11578. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11579. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11580. }
  11581. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11582. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11583. clif_additem(sd,0,0,flag);
  11584. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11585. }
  11586. clif_misceffect(&sd->bl,3);
  11587. }
  11588. return SCRIPT_CMD_SUCCESS;
  11589. }
  11590. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11591. /// failedremovecards <slot>, <type>;
  11592. /// <type>=0 : will destroy both the item and the cards.
  11593. /// <type>=1 : will keep the item, but destroy the cards.
  11594. /// <type>=2 : will keep the cards, but destroy the item.
  11595. /// <type>=? : will just display the failure effect.
  11596. BUILDIN_FUNC(failedremovecards) {
  11597. int i=-1,c,cardflag=0;
  11598. TBL_PC* sd;
  11599. int num;
  11600. int typefail;
  11601. if( !script_rid2sd(sd) )
  11602. return SCRIPT_CMD_SUCCESS;
  11603. num = script_getnum(st,2);
  11604. typefail = script_getnum(st,3);
  11605. if (equip_index_check(num))
  11606. i=pc_checkequip(sd,equip_bitmask[num]);
  11607. if (i < 0 || !sd->inventory_data[i])
  11608. return SCRIPT_CMD_SUCCESS;
  11609. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11610. return SCRIPT_CMD_SUCCESS;
  11611. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11612. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11613. cardflag = 1;
  11614. if(typefail == 2) {// add cards to inventory, clear
  11615. unsigned char flag = 0;
  11616. struct item item_tmp;
  11617. memset(&item_tmp,0,sizeof(item_tmp));
  11618. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11619. item_tmp.identify = 1;
  11620. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11621. clif_additem(sd,0,0,flag);
  11622. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11623. }
  11624. }
  11625. }
  11626. }
  11627. if(cardflag == 1) {
  11628. if(typefail == 0 || typefail == 2){ // destroy the item
  11629. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11630. }else if(typefail == 1){ // destroy the card
  11631. unsigned char flag = 0;
  11632. struct item item_tmp;
  11633. memset(&item_tmp,0,sizeof(item_tmp));
  11634. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11635. item_tmp.identify = 1;
  11636. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11637. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11638. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11639. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11640. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11641. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11642. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11643. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11644. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11645. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11646. }
  11647. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11648. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11649. clif_additem(sd,0,0,flag);
  11650. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11651. }
  11652. }
  11653. clif_misceffect(&sd->bl,2);
  11654. }
  11655. return SCRIPT_CMD_SUCCESS;
  11656. }
  11657. /* ================================================================
  11658. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11659. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11660. * improved by [Lance]
  11661. * ================================================================*/
  11662. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11663. {
  11664. int x,y,m,check_val=0,check_ID=0,i=0;
  11665. struct guild *g = NULL;
  11666. struct party_data *p = NULL;
  11667. const char *str;
  11668. const char *mapname;
  11669. unsigned int index;
  11670. mapname=script_getstr(st,2);
  11671. str=script_getstr(st,3);
  11672. x=script_getnum(st,4);
  11673. y=script_getnum(st,5);
  11674. if(script_hasdata(st,7)){
  11675. check_val=script_getnum(st,6);
  11676. check_ID=script_getnum(st,7);
  11677. }
  11678. if((m=map_mapname2mapid(mapname))< 0)
  11679. return SCRIPT_CMD_SUCCESS;
  11680. if(!(index=mapindex_name2id(str)))
  11681. return SCRIPT_CMD_SUCCESS;
  11682. switch(check_val){
  11683. case 1:
  11684. g = guild_search(check_ID);
  11685. if (g){
  11686. for( i=0; i < g->max_member; i++)
  11687. {
  11688. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11689. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11690. }
  11691. }
  11692. }
  11693. break;
  11694. case 2:
  11695. p = party_search(check_ID);
  11696. if(p){
  11697. for(i=0;i<MAX_PARTY; i++){
  11698. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11699. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11700. }
  11701. }
  11702. }
  11703. break;
  11704. default:
  11705. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11706. break;
  11707. }
  11708. return SCRIPT_CMD_SUCCESS;
  11709. }
  11710. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11711. {
  11712. char *event=va_arg(ap,char *);
  11713. struct mob_data *md = ((struct mob_data *)bl);
  11714. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11715. return 1;
  11716. return SCRIPT_CMD_SUCCESS;
  11717. }
  11718. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11719. {
  11720. const char *mapname,*event;
  11721. int16 m;
  11722. mapname=script_getstr(st,2);
  11723. event=script_getstr(st,3);
  11724. if( strcmp(event, "all") == 0 )
  11725. event = NULL;
  11726. else
  11727. check_event(st, event);
  11728. if( strcmp(mapname, "this") == 0 ) {
  11729. struct map_session_data *sd;
  11730. if( script_rid2sd(sd) )
  11731. m = sd->bl.m;
  11732. else {
  11733. script_pushint(st,-1);
  11734. return SCRIPT_CMD_SUCCESS;
  11735. }
  11736. }
  11737. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11738. script_pushint(st,-1);
  11739. return SCRIPT_CMD_SUCCESS;
  11740. }
  11741. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11742. return SCRIPT_CMD_SUCCESS;
  11743. }
  11744. BUILDIN_FUNC(marriage)
  11745. {
  11746. TBL_PC *sd;
  11747. TBL_PC *p_sd;
  11748. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11749. script_pushint(st,0);
  11750. return SCRIPT_CMD_SUCCESS;
  11751. }
  11752. script_pushint(st,1);
  11753. return SCRIPT_CMD_SUCCESS;
  11754. }
  11755. BUILDIN_FUNC(wedding_effect)
  11756. {
  11757. TBL_PC *sd;
  11758. struct block_list *bl;
  11759. if(!script_rid2sd(sd)) {
  11760. bl=map_id2bl(st->oid);
  11761. } else
  11762. bl=&sd->bl;
  11763. clif_wedding_effect(bl);
  11764. return SCRIPT_CMD_SUCCESS;
  11765. }
  11766. /**
  11767. * divorce({<char_id>})
  11768. **/
  11769. BUILDIN_FUNC(divorce)
  11770. {
  11771. TBL_PC *sd;
  11772. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11773. script_pushint(st,0);
  11774. return SCRIPT_CMD_FAILURE;
  11775. }
  11776. script_pushint(st,1);
  11777. return SCRIPT_CMD_SUCCESS;
  11778. }
  11779. /**
  11780. * ispartneron({<char_id>})
  11781. **/
  11782. BUILDIN_FUNC(ispartneron)
  11783. {
  11784. TBL_PC *sd;
  11785. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11786. map_charid2sd(sd->status.partner_id) == NULL)
  11787. {
  11788. script_pushint(st,0);
  11789. return SCRIPT_CMD_FAILURE;
  11790. }
  11791. script_pushint(st,1);
  11792. return SCRIPT_CMD_SUCCESS;
  11793. }
  11794. /**
  11795. * getpartnerid({<char_id>})
  11796. **/
  11797. BUILDIN_FUNC(getpartnerid)
  11798. {
  11799. TBL_PC *sd;
  11800. if (!script_charid2sd(2,sd)) {
  11801. script_pushint(st,0);
  11802. return SCRIPT_CMD_FAILURE;
  11803. }
  11804. script_pushint(st,sd->status.partner_id);
  11805. return SCRIPT_CMD_SUCCESS;
  11806. }
  11807. /**
  11808. * getchildid({<char_id>})
  11809. **/
  11810. BUILDIN_FUNC(getchildid)
  11811. {
  11812. TBL_PC *sd;
  11813. if (!script_charid2sd(2,sd)) {
  11814. script_pushint(st,0);
  11815. return SCRIPT_CMD_FAILURE;
  11816. }
  11817. script_pushint(st,sd->status.child);
  11818. return SCRIPT_CMD_SUCCESS;
  11819. }
  11820. /**
  11821. * getmotherid({<char_id>})
  11822. **/
  11823. BUILDIN_FUNC(getmotherid)
  11824. {
  11825. TBL_PC *sd;
  11826. if (!script_charid2sd(2,sd)) {
  11827. script_pushint(st,0);
  11828. return SCRIPT_CMD_FAILURE;
  11829. }
  11830. script_pushint(st,sd->status.mother);
  11831. return SCRIPT_CMD_SUCCESS;
  11832. }
  11833. /**
  11834. * getfatherid({<char_id>})
  11835. **/
  11836. BUILDIN_FUNC(getfatherid)
  11837. {
  11838. TBL_PC *sd;
  11839. if (!script_charid2sd(2,sd)) {
  11840. script_pushint(st,0);
  11841. return SCRIPT_CMD_FAILURE;
  11842. }
  11843. script_pushint(st,sd->status.father);
  11844. return SCRIPT_CMD_SUCCESS;
  11845. }
  11846. BUILDIN_FUNC(warppartner)
  11847. {
  11848. int x,y;
  11849. unsigned short mapindex;
  11850. const char *str;
  11851. TBL_PC *sd;
  11852. TBL_PC *p_sd;
  11853. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  11854. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11855. script_pushint(st,0);
  11856. return SCRIPT_CMD_SUCCESS;
  11857. }
  11858. str=script_getstr(st,2);
  11859. x=script_getnum(st,3);
  11860. y=script_getnum(st,4);
  11861. mapindex = mapindex_name2id(str);
  11862. if (mapindex) {
  11863. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11864. script_pushint(st,1);
  11865. } else
  11866. script_pushint(st,0);
  11867. return SCRIPT_CMD_SUCCESS;
  11868. }
  11869. /*================================================
  11870. * Script for Displaying MOB Information [Valaris]
  11871. *------------------------------------------------*/
  11872. BUILDIN_FUNC(strmobinfo)
  11873. {
  11874. int num=script_getnum(st,2);
  11875. int class_=script_getnum(st,3);
  11876. if(!mobdb_checkid(class_))
  11877. {
  11878. if (num < 3) //requested a string
  11879. script_pushconststr(st,"");
  11880. else
  11881. script_pushint(st,0);
  11882. return SCRIPT_CMD_SUCCESS;
  11883. }
  11884. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  11885. switch (num) {
  11886. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  11887. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  11888. case 3: script_pushint(st,mob->lv); break;
  11889. case 4: script_pushint(st,mob->status.max_hp); break;
  11890. case 5: script_pushint(st,mob->status.max_sp); break;
  11891. case 6: script_pushint(st,mob->base_exp); break;
  11892. case 7: script_pushint(st,mob->job_exp); break;
  11893. default:
  11894. script_pushint(st,0);
  11895. break;
  11896. }
  11897. return SCRIPT_CMD_SUCCESS;
  11898. }
  11899. /*==========================================
  11900. * Summon guardians [Valaris]
  11901. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11902. *------------------------------------------*/
  11903. BUILDIN_FUNC(guardian)
  11904. {
  11905. int class_=0,x=0,y=0,guardian=0;
  11906. const char *str,*mapname,*evt="";
  11907. bool has_index = false;
  11908. mapname =script_getstr(st,2);
  11909. x =script_getnum(st,3);
  11910. y =script_getnum(st,4);
  11911. str =script_getstr(st,5);
  11912. class_=script_getnum(st,6);
  11913. if( script_hasdata(st,8) )
  11914. {// "<event label>",<guardian index>
  11915. evt=script_getstr(st,7);
  11916. guardian=script_getnum(st,8);
  11917. has_index = true;
  11918. } else if( script_hasdata(st,7) ){
  11919. if( script_isstring(st, 7) )
  11920. {// "<event label>"
  11921. evt=script_getstr(st,7);
  11922. } else
  11923. {// <guardian index>
  11924. guardian=script_getnum(st,7);
  11925. has_index = true;
  11926. }
  11927. }
  11928. check_event(st, evt);
  11929. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11930. return SCRIPT_CMD_SUCCESS;
  11931. }
  11932. /*==========================================
  11933. * Invisible Walls [Zephyrus]
  11934. *------------------------------------------*/
  11935. BUILDIN_FUNC(setwall)
  11936. {
  11937. const char *mapname, *name;
  11938. int x, y, m, size, dir;
  11939. bool shootable;
  11940. mapname = script_getstr(st,2);
  11941. x = script_getnum(st,3);
  11942. y = script_getnum(st,4);
  11943. size = script_getnum(st,5);
  11944. dir = script_getnum(st,6);
  11945. shootable = script_getnum(st,7) != 0;
  11946. name = script_getstr(st,8);
  11947. if( (m = map_mapname2mapid(mapname)) < 0 )
  11948. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11949. map_iwall_set(m, x, y, size, dir, shootable, name);
  11950. return SCRIPT_CMD_SUCCESS;
  11951. }
  11952. BUILDIN_FUNC(delwall)
  11953. {
  11954. const char *name = script_getstr(st,2);
  11955. if( !map_iwall_remove(name) ){
  11956. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  11957. return SCRIPT_CMD_FAILURE;
  11958. }
  11959. return SCRIPT_CMD_SUCCESS;
  11960. }
  11961. BUILDIN_FUNC(checkwall)
  11962. {
  11963. const char *wall_name = script_getstr(st, 2);
  11964. script_pushint(st, map_iwall_exist(wall_name));
  11965. return SCRIPT_CMD_SUCCESS;
  11966. }
  11967. /// Retrieves various information about the specified guardian.
  11968. ///
  11969. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11970. /// type: 0 - whether it is deployed or not
  11971. /// 1 - maximum hp
  11972. /// 2 - current hp
  11973. ///
  11974. BUILDIN_FUNC(guardianinfo)
  11975. {
  11976. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11977. int id = script_getnum(st,3);
  11978. int type = script_getnum(st,4);
  11979. struct guild_castle* gc = guild_mapname2gc(mapname);
  11980. struct mob_data* gd;
  11981. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11982. {
  11983. script_pushint(st,-1);
  11984. return SCRIPT_CMD_SUCCESS;
  11985. }
  11986. if( type == 0 )
  11987. script_pushint(st, gc->guardian[id].visible);
  11988. else
  11989. if( !gc->guardian[id].visible )
  11990. script_pushint(st,-1);
  11991. else
  11992. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11993. script_pushint(st,-1);
  11994. else
  11995. {
  11996. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11997. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11998. else
  11999. script_pushint(st,-1);
  12000. }
  12001. return SCRIPT_CMD_SUCCESS;
  12002. }
  12003. /*==========================================
  12004. * Get the item name by item_id or null
  12005. *------------------------------------------*/
  12006. BUILDIN_FUNC(getitemname)
  12007. {
  12008. t_itemid item_id = 0;
  12009. struct item_data *i_data;
  12010. char *item_name;
  12011. if( script_isstring(st, 2) ){
  12012. const char *name = script_getstr(st, 2);
  12013. struct item_data *item_data = itemdb_searchname(name);
  12014. if( item_data )
  12015. item_id=item_data->nameid;
  12016. }else
  12017. item_id = script_getnum(st, 2);
  12018. i_data = itemdb_exists(item_id);
  12019. if (i_data == NULL)
  12020. {
  12021. script_pushconststr(st,"null");
  12022. return SCRIPT_CMD_SUCCESS;
  12023. }
  12024. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  12025. memcpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12026. script_pushstr(st,item_name);
  12027. return SCRIPT_CMD_SUCCESS;
  12028. }
  12029. /*==========================================
  12030. * Returns number of slots an item has. [Skotlex]
  12031. *------------------------------------------*/
  12032. BUILDIN_FUNC(getitemslots)
  12033. {
  12034. struct item_data *i_data;
  12035. t_itemid item_id=script_getnum(st,2);
  12036. i_data = itemdb_exists(item_id);
  12037. if (i_data)
  12038. script_pushint(st,i_data->slots);
  12039. else
  12040. script_pushint(st,-1);
  12041. return SCRIPT_CMD_SUCCESS;
  12042. }
  12043. // TODO: add matk here if needed/once we get rid of RENEWAL
  12044. /*==========================================
  12045. * Returns some values of an item [Lupus]
  12046. * Price, Weight, etc...
  12047. *------------------------------------------*/
  12048. BUILDIN_FUNC(getiteminfo)
  12049. {
  12050. item_data *i_data;
  12051. int type = script_getnum(st, 3);
  12052. if (script_isstring(st, 2))
  12053. i_data = itemdb_searchname(script_getstr(st, 2));
  12054. else
  12055. i_data = itemdb_exists(script_getnum(st, 2));
  12056. if (i_data == nullptr) {
  12057. if (type != ITEMINFO_AEGISNAME)
  12058. script_pushint(st, -1);
  12059. else
  12060. script_pushstrcopy(st, "");
  12061. return SCRIPT_CMD_SUCCESS;
  12062. }
  12063. switch( type ) {
  12064. case ITEMINFO_BUY: script_pushint(st, i_data->value_buy); break;
  12065. case ITEMINFO_SELL: script_pushint(st, i_data->value_sell); break;
  12066. case ITEMINFO_TYPE: script_pushint(st, i_data->type); break;
  12067. case ITEMINFO_MAXCHANCE: script_pushint(st, i_data->maxchance); break;
  12068. case ITEMINFO_GENDER: script_pushint(st, i_data->sex); break;
  12069. case ITEMINFO_LOCATIONS: script_pushint(st, i_data->equip); break;
  12070. case ITEMINFO_WEIGHT: script_pushint(st, i_data->weight); break;
  12071. case ITEMINFO_ATTACK: script_pushint(st, i_data->atk); break;
  12072. case ITEMINFO_DEFENSE: script_pushint(st, i_data->def); break;
  12073. case ITEMINFO_RANGE: script_pushint(st, i_data->range); break;
  12074. case ITEMINFO_SLOT: script_pushint(st, i_data->slots); break;
  12075. case ITEMINFO_VIEW:
  12076. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12077. script_pushint(st, i_data->subtype);
  12078. } else {
  12079. script_pushint(st, i_data->look);
  12080. }
  12081. break;
  12082. case ITEMINFO_EQUIPLEVELMIN: script_pushint(st, i_data->elv); break;
  12083. case ITEMINFO_WEAPONLEVEL: script_pushint(st, i_data->wlv); break;
  12084. case ITEMINFO_ALIASNAME: script_pushint(st, i_data->view_id); break;
  12085. case ITEMINFO_EQUIPLEVELMAX: script_pushint(st, i_data->elvmax); break;
  12086. case ITEMINFO_MAGICATTACK: {
  12087. #ifdef RENEWAL
  12088. script_pushint(st, i_data->matk);
  12089. #else
  12090. script_pushint(st, 0);
  12091. #endif
  12092. break;
  12093. }
  12094. case ITEMINFO_ID: script_pushint(st, i_data->nameid); break;
  12095. case ITEMINFO_AEGISNAME: script_pushstrcopy(st, i_data->name.c_str()); break;
  12096. default:
  12097. script_pushint(st, -1);
  12098. break;
  12099. }
  12100. return SCRIPT_CMD_SUCCESS;
  12101. }
  12102. /*==========================================
  12103. * Set some values of an item [Lupus]
  12104. * Price, Weight, etc...
  12105. *------------------------------------------*/
  12106. BUILDIN_FUNC(setiteminfo)
  12107. {
  12108. item_data *i_data;
  12109. if (script_isstring(st, 2))
  12110. i_data = itemdb_search_aegisname(script_getstr(st, 2));
  12111. else
  12112. i_data = itemdb_exists(script_getnum(st, 2));
  12113. if (i_data == nullptr) {
  12114. script_pushint(st, -1);
  12115. return SCRIPT_CMD_SUCCESS;
  12116. }
  12117. int value = script_getnum(st,4);
  12118. switch( script_getnum(st, 3) ) {
  12119. case ITEMINFO_BUY: i_data->value_buy = static_cast<uint32>(value); break;
  12120. case ITEMINFO_SELL: i_data->value_sell = static_cast<uint32>(value); break;
  12121. case ITEMINFO_TYPE: i_data->type = static_cast<item_types>(value); break;
  12122. case ITEMINFO_MAXCHANCE: i_data->maxchance = static_cast<int>(value); break;
  12123. case ITEMINFO_GENDER: i_data->sex = static_cast<uint8>(value); break;
  12124. case ITEMINFO_LOCATIONS: i_data->equip = static_cast<uint32>(value); break;
  12125. case ITEMINFO_WEIGHT: i_data->weight = static_cast<uint32>(value); break;
  12126. case ITEMINFO_ATTACK: i_data->atk = static_cast<uint32>(value); break;
  12127. case ITEMINFO_DEFENSE: i_data->def = static_cast<uint32>(value); break;
  12128. case ITEMINFO_RANGE: i_data->range = static_cast<uint16>(value); break;
  12129. case ITEMINFO_SLOT: i_data->slots = static_cast<uint16>(value); break;
  12130. case ITEMINFO_VIEW:
  12131. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12132. i_data->subtype = static_cast<uint8>(value);
  12133. } else {
  12134. i_data->look = static_cast<uint32>(value);
  12135. }
  12136. break;
  12137. case ITEMINFO_EQUIPLEVELMIN: i_data->elv = static_cast<uint16>(value); break;
  12138. case ITEMINFO_WEAPONLEVEL: i_data->wlv = static_cast<uint16>(value); break;
  12139. case ITEMINFO_ALIASNAME: i_data->view_id = static_cast<t_itemid>(value); break;
  12140. case ITEMINFO_EQUIPLEVELMAX: i_data->elvmax = static_cast<uint16>(value); break;
  12141. case ITEMINFO_MAGICATTACK: {
  12142. #ifdef RENEWAL
  12143. i_data->matk = static_cast<uint32>(value);
  12144. #else
  12145. value = 0;
  12146. #endif
  12147. break;
  12148. }
  12149. default:
  12150. script_pushint(st, -1);
  12151. break;
  12152. }
  12153. script_pushint(st, value);
  12154. return SCRIPT_CMD_SUCCESS;
  12155. }
  12156. /*==========================================
  12157. * Returns value from equipped item slot n [Lupus]
  12158. getequipcardid(num,slot)
  12159. where
  12160. num = eqip position slot
  12161. slot = 0,1,2,3 (Card Slot N)
  12162. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12163. it's useful when you want to check item cards or if it's signed
  12164. Useful for such quests as "Sign this refined item with players name" etc
  12165. Hat[0] +4 -> Player's Hat[0] +4
  12166. *------------------------------------------*/
  12167. BUILDIN_FUNC(getequipcardid)
  12168. {
  12169. int i=-1,num,slot;
  12170. TBL_PC *sd;
  12171. if( !script_rid2sd(sd) )
  12172. return SCRIPT_CMD_SUCCESS;
  12173. num=script_getnum(st,2);
  12174. slot=script_getnum(st,3);
  12175. if (equip_index_check(num))
  12176. i=pc_checkequip(sd,equip_bitmask[num]);
  12177. if(i >= 0 && slot>=0 && slot<4)
  12178. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12179. else
  12180. script_pushint(st,0);
  12181. return SCRIPT_CMD_SUCCESS;
  12182. }
  12183. /*==========================================
  12184. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12185. *------------------------------------------*/
  12186. BUILDIN_FUNC(petskillbonus)
  12187. {
  12188. struct pet_data *pd;
  12189. TBL_PC *sd;
  12190. if(!script_rid2sd(sd) || sd->pd == NULL)
  12191. return SCRIPT_CMD_FAILURE;
  12192. pd = sd->pd;
  12193. if (pd->bonus)
  12194. { //Clear previous bonus
  12195. if (pd->bonus->timer != INVALID_TIMER)
  12196. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12197. } else //init
  12198. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12199. pd->bonus->type = script_getnum(st,2);
  12200. pd->bonus->val = script_getnum(st,3);
  12201. pd->bonus->duration = script_getnum(st,4);
  12202. pd->bonus->delay = script_getnum(st,5);
  12203. if (pd->state.skillbonus == 1)
  12204. pd->state.skillbonus = 0; // waiting state
  12205. // wait for timer to start
  12206. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12207. pd->bonus->timer = INVALID_TIMER;
  12208. else
  12209. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12210. return SCRIPT_CMD_SUCCESS;
  12211. }
  12212. /*==========================================
  12213. * pet looting [Valaris] //Rewritten by [Skotlex]
  12214. *------------------------------------------*/
  12215. BUILDIN_FUNC(petloot)
  12216. {
  12217. int max;
  12218. struct pet_data *pd;
  12219. TBL_PC *sd;
  12220. if(!script_rid2sd(sd) || sd->pd==NULL)
  12221. return SCRIPT_CMD_SUCCESS;
  12222. max=script_getnum(st,2);
  12223. if(max < 1)
  12224. max = 1; //Let'em loot at least 1 item.
  12225. else if (max > MAX_PETLOOT_SIZE)
  12226. max = MAX_PETLOOT_SIZE;
  12227. pd = sd->pd;
  12228. if (pd->loot != NULL)
  12229. { //Release whatever was there already and reallocate memory
  12230. pet_lootitem_drop(pd, pd->master);
  12231. aFree(pd->loot->item);
  12232. }
  12233. else
  12234. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12235. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12236. pd->loot->max=max;
  12237. pd->loot->count = 0;
  12238. pd->loot->weight = 0;
  12239. return SCRIPT_CMD_SUCCESS;
  12240. }
  12241. /*==========================================
  12242. * Set arrays with info of all sd inventory :
  12243. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12244. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12245. * @inventorylist_card(0..3), @inventorylist_expire
  12246. * @inventorylist_count = scalar
  12247. *------------------------------------------*/
  12248. BUILDIN_FUNC(getinventorylist)
  12249. {
  12250. TBL_PC *sd;
  12251. char card_var[NAME_LENGTH], randopt_var[50];
  12252. int i,j=0,k;
  12253. if (!script_charid2sd(2,sd))
  12254. return SCRIPT_CMD_FAILURE;
  12255. for(i=0;i<MAX_INVENTORY;i++){
  12256. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12257. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12258. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12259. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12260. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12261. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12262. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12263. for (k = 0; k < MAX_SLOTS; k++)
  12264. {
  12265. sprintf(card_var, "@inventorylist_card%d",k+1);
  12266. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12267. }
  12268. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12269. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12270. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12271. {
  12272. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12273. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12274. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12275. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12276. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12277. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12278. }
  12279. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12280. j++;
  12281. }
  12282. }
  12283. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12284. return SCRIPT_CMD_SUCCESS;
  12285. }
  12286. /**
  12287. * getskilllist ({<char_id>});
  12288. **/
  12289. BUILDIN_FUNC(getskilllist)
  12290. {
  12291. TBL_PC *sd;
  12292. int i,j=0;
  12293. if (!script_charid2sd(2,sd))
  12294. return SCRIPT_CMD_FAILURE;
  12295. for(i=0;i<MAX_SKILL;i++){
  12296. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12297. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12298. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12299. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12300. j++;
  12301. }
  12302. }
  12303. pc_setreg(sd,add_str("@skilllist_count"),j);
  12304. return SCRIPT_CMD_SUCCESS;
  12305. }
  12306. /**
  12307. * clearitem ({<char_id>});
  12308. **/
  12309. BUILDIN_FUNC(clearitem)
  12310. {
  12311. TBL_PC *sd;
  12312. int i;
  12313. if (!script_charid2sd(2,sd))
  12314. return SCRIPT_CMD_FAILURE;
  12315. for (i=0; i<MAX_INVENTORY; i++) {
  12316. if (sd->inventory.u.items_inventory[i].amount) {
  12317. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12318. }
  12319. }
  12320. return SCRIPT_CMD_SUCCESS;
  12321. }
  12322. /**
  12323. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12324. * disguise <Monster ID>{,<char_id>};
  12325. **/
  12326. BUILDIN_FUNC(disguise)
  12327. {
  12328. int id;
  12329. TBL_PC* sd;
  12330. if (!script_charid2sd(3,sd))
  12331. return SCRIPT_CMD_FAILURE;
  12332. id = script_getnum(st,2);
  12333. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12334. pc_disguise(sd, id);
  12335. script_pushint(st,id);
  12336. } else
  12337. script_pushint(st,0);
  12338. return SCRIPT_CMD_SUCCESS;
  12339. }
  12340. /**
  12341. * Undisguise Player (returns 1 if success, 0 on fail)
  12342. * undisguise {<char_id>};
  12343. **/
  12344. BUILDIN_FUNC(undisguise)
  12345. {
  12346. TBL_PC* sd;
  12347. if (!script_charid2sd(2,sd))
  12348. return SCRIPT_CMD_FAILURE;
  12349. if (sd->disguise) {
  12350. pc_disguise(sd, 0);
  12351. script_pushint(st,0);
  12352. } else {
  12353. script_pushint(st,1);
  12354. }
  12355. return SCRIPT_CMD_SUCCESS;
  12356. }
  12357. /**
  12358. * Transform an NPC to another _class
  12359. *
  12360. * classchange(<view id>{,"<NPC name>","<flag>"});
  12361. * @param flag: Specify target
  12362. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12363. * BC_SELF - Sprite is sent only to player attached.
  12364. */
  12365. BUILDIN_FUNC(classchange)
  12366. {
  12367. int _class, type = 1;
  12368. struct npc_data* nd = NULL;
  12369. TBL_PC *sd = map_id2sd(st->rid);
  12370. send_target target = AREA;
  12371. _class = script_getnum(st,2);
  12372. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12373. nd = npc_name2id(script_getstr(st, 3));
  12374. else
  12375. nd = (struct npc_data *)map_id2bl(st->oid);
  12376. if (nd == NULL)
  12377. return SCRIPT_CMD_FAILURE;
  12378. if (script_hasdata(st, 4)) {
  12379. switch(script_getnum(st, 4)) {
  12380. case BC_SELF: target = SELF; break;
  12381. case BC_AREA:
  12382. default: target = AREA; break;
  12383. }
  12384. }
  12385. if (target != SELF)
  12386. clif_class_change(&nd->bl,_class,type);
  12387. else if (sd == NULL)
  12388. return SCRIPT_CMD_FAILURE;
  12389. else
  12390. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12391. return SCRIPT_CMD_SUCCESS;
  12392. }
  12393. /*==========================================
  12394. * Display an effect
  12395. *------------------------------------------*/
  12396. BUILDIN_FUNC(misceffect)
  12397. {
  12398. int type;
  12399. type=script_getnum(st,2);
  12400. if( type <= EF_NONE || type >= EF_MAX ){
  12401. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12402. return SCRIPT_CMD_FAILURE;
  12403. }
  12404. if(st->oid && st->oid != fake_nd->bl.id) {
  12405. struct block_list *bl = map_id2bl(st->oid);
  12406. if (bl)
  12407. clif_specialeffect(bl,type,AREA);
  12408. } else{
  12409. TBL_PC *sd;
  12410. if(script_rid2sd(sd))
  12411. clif_specialeffect(&sd->bl,type,AREA);
  12412. }
  12413. return SCRIPT_CMD_SUCCESS;
  12414. }
  12415. /*==========================================
  12416. * Play a BGM on a single client [Rikter/Yommy]
  12417. *------------------------------------------*/
  12418. BUILDIN_FUNC(playBGM)
  12419. {
  12420. struct map_session_data* sd;
  12421. if( script_rid2sd(sd) ) {
  12422. clif_playBGM(sd, script_getstr(st,2));
  12423. }
  12424. return SCRIPT_CMD_SUCCESS;
  12425. }
  12426. static int playBGM_sub(struct block_list* bl,va_list ap)
  12427. {
  12428. const char* name = va_arg(ap,const char*);
  12429. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12430. return 0;
  12431. }
  12432. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12433. {
  12434. const char* name = va_arg(args, const char*);
  12435. clif_playBGM(sd, name);
  12436. return 0;
  12437. }
  12438. /*==========================================
  12439. * Play a BGM on multiple client [Rikter/Yommy]
  12440. *------------------------------------------*/
  12441. BUILDIN_FUNC(playBGMall)
  12442. {
  12443. const char* name;
  12444. name = script_getstr(st,2);
  12445. if( script_hasdata(st,7) ) {// specified part of map
  12446. const char* mapname = script_getstr(st,3);
  12447. int x0 = script_getnum(st,4);
  12448. int y0 = script_getnum(st,5);
  12449. int x1 = script_getnum(st,6);
  12450. int y1 = script_getnum(st,7);
  12451. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12452. }
  12453. else if( script_hasdata(st,3) ) {// entire map
  12454. const char* mapname = script_getstr(st,3);
  12455. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12456. }
  12457. else {// entire server
  12458. map_foreachpc(&playBGM_foreachpc_sub, name);
  12459. }
  12460. return SCRIPT_CMD_SUCCESS;
  12461. }
  12462. /*==========================================
  12463. * Play a .wav sound for sd
  12464. *------------------------------------------*/
  12465. BUILDIN_FUNC(soundeffect)
  12466. {
  12467. TBL_PC* sd;
  12468. if(script_rid2sd(sd)){
  12469. const char* name = script_getstr(st,2);
  12470. int type = script_getnum(st,3);
  12471. clif_soundeffect(sd,&sd->bl,name,type);
  12472. }
  12473. return SCRIPT_CMD_SUCCESS;
  12474. }
  12475. int soundeffect_sub(struct block_list* bl,va_list ap)
  12476. {
  12477. char* name = va_arg(ap,char*);
  12478. int type = va_arg(ap,int);
  12479. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12480. return 0;
  12481. }
  12482. /*==========================================
  12483. * Play a sound effect (.wav) on multiple clients
  12484. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12485. *------------------------------------------*/
  12486. BUILDIN_FUNC(soundeffectall)
  12487. {
  12488. struct block_list* bl;
  12489. struct map_session_data* sd;
  12490. const char* name;
  12491. int type;
  12492. if( st->rid && script_rid2sd(sd) )
  12493. bl = &sd->bl;
  12494. else
  12495. bl = map_id2bl(st->oid);
  12496. if (!bl)
  12497. return SCRIPT_CMD_SUCCESS;
  12498. name = script_getstr(st,2);
  12499. type = script_getnum(st,3);
  12500. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12501. if(!script_hasdata(st,4))
  12502. { // area around
  12503. clif_soundeffectall(bl, name, type, AREA);
  12504. }
  12505. else
  12506. if(!script_hasdata(st,5))
  12507. { // entire map
  12508. const char* mapname = script_getstr(st,4);
  12509. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12510. }
  12511. else
  12512. if(script_hasdata(st,8))
  12513. { // specified part of map
  12514. const char* mapname = script_getstr(st,4);
  12515. int x0 = script_getnum(st,5);
  12516. int y0 = script_getnum(st,6);
  12517. int x1 = script_getnum(st,7);
  12518. int y1 = script_getnum(st,8);
  12519. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12520. }
  12521. else
  12522. {
  12523. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12524. }
  12525. return SCRIPT_CMD_SUCCESS;
  12526. }
  12527. /*==========================================
  12528. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12529. *------------------------------------------*/
  12530. BUILDIN_FUNC(petrecovery)
  12531. {
  12532. struct pet_data *pd;
  12533. TBL_PC *sd;
  12534. int sc;
  12535. if(!script_rid2sd(sd) || sd->pd == NULL)
  12536. return SCRIPT_CMD_FAILURE;
  12537. sc = script_getnum(st,2);
  12538. if (sc <= SC_NONE || sc >= SC_MAX) {
  12539. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12540. return SCRIPT_CMD_FAILURE;
  12541. }
  12542. pd = sd->pd;
  12543. if (pd->recovery)
  12544. { //Halt previous bonus
  12545. if (pd->recovery->timer != INVALID_TIMER)
  12546. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12547. } else //Init
  12548. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12549. pd->recovery->type = (sc_type)sc;
  12550. pd->recovery->delay = script_getnum(st,3);
  12551. pd->recovery->timer = INVALID_TIMER;
  12552. return SCRIPT_CMD_SUCCESS;
  12553. }
  12554. /*==========================================
  12555. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12556. *------------------------------------------*/
  12557. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12558. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12559. BUILDIN_FUNC(petskillattack)
  12560. {
  12561. struct pet_data *pd;
  12562. TBL_PC *sd;
  12563. int id = 0;
  12564. if(!script_rid2sd(sd) || sd->pd == NULL)
  12565. return SCRIPT_CMD_FAILURE;
  12566. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12567. if (!id) {
  12568. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12569. return SCRIPT_CMD_FAILURE;
  12570. }
  12571. pd = sd->pd;
  12572. if (pd->a_skill == NULL)
  12573. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12574. pd->a_skill->id = id;
  12575. pd->a_skill->damage = 0;
  12576. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12577. pd->a_skill->div_ = 0;
  12578. pd->a_skill->rate = script_getnum(st,4);
  12579. pd->a_skill->bonusrate = script_getnum(st,5);
  12580. return SCRIPT_CMD_SUCCESS;
  12581. }
  12582. /*==========================================
  12583. * pet attack skills [Valaris]
  12584. *------------------------------------------*/
  12585. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12586. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12587. BUILDIN_FUNC(petskillattack2)
  12588. {
  12589. struct pet_data *pd;
  12590. TBL_PC *sd;
  12591. int id = 0;
  12592. if(!script_rid2sd(sd) || sd->pd == NULL)
  12593. return SCRIPT_CMD_FAILURE;
  12594. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12595. if (!id) {
  12596. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12597. return SCRIPT_CMD_FAILURE;
  12598. }
  12599. pd = sd->pd;
  12600. if (pd->a_skill == NULL)
  12601. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12602. pd->a_skill->id = id;
  12603. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12604. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12605. pd->a_skill->div_ = script_getnum(st,4);
  12606. pd->a_skill->rate = script_getnum(st,5);
  12607. pd->a_skill->bonusrate = script_getnum(st,6);
  12608. return SCRIPT_CMD_SUCCESS;
  12609. }
  12610. /*==========================================
  12611. * pet support skills [Skotlex]
  12612. *------------------------------------------*/
  12613. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12614. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12615. BUILDIN_FUNC(petskillsupport)
  12616. {
  12617. struct pet_data *pd;
  12618. TBL_PC *sd;
  12619. int id = 0;
  12620. if(!script_rid2sd(sd) || sd->pd == NULL)
  12621. return SCRIPT_CMD_FAILURE;
  12622. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12623. if (!id) {
  12624. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12625. return SCRIPT_CMD_FAILURE;
  12626. }
  12627. pd = sd->pd;
  12628. if (pd->s_skill)
  12629. { //Clear previous skill
  12630. if (pd->s_skill->timer != INVALID_TIMER)
  12631. {
  12632. if (pd->s_skill->id)
  12633. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12634. else
  12635. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12636. }
  12637. } else //init memory
  12638. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12639. pd->s_skill->id = id;
  12640. pd->s_skill->lv = script_getnum(st,3);
  12641. pd->s_skill->delay = script_getnum(st,4);
  12642. pd->s_skill->hp = script_getnum(st,5);
  12643. pd->s_skill->sp = script_getnum(st,6);
  12644. //Use delay as initial offset to avoid skill/heal exploits
  12645. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12646. pd->s_skill->timer = INVALID_TIMER;
  12647. else
  12648. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12649. return SCRIPT_CMD_SUCCESS;
  12650. }
  12651. static inline void script_skill_effect(block_list *bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y) {
  12652. nullpo_retv(bl);
  12653. switch (skill_get_casttype(skill_id)) {
  12654. case CAST_GROUND:
  12655. clif_skill_poseffect(bl, skill_id, skill_lv, x, y, gettick());
  12656. break;
  12657. case CAST_NODAMAGE:
  12658. clif_skill_nodamage(bl, bl, skill_id, skill_lv, 1);
  12659. break;
  12660. case CAST_DAMAGE:
  12661. 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));
  12662. break;
  12663. }
  12664. }
  12665. /*==========================================
  12666. * Scripted skill effects [Celest]
  12667. *------------------------------------------*/
  12668. /// skilleffect <skill id>,<level>
  12669. /// skilleffect "<skill name>",<level>
  12670. BUILDIN_FUNC(skilleffect)
  12671. {
  12672. TBL_PC *sd;
  12673. uint16 skill_id, skill_lv;
  12674. if( !script_rid2sd(sd) )
  12675. return SCRIPT_CMD_FAILURE;
  12676. skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12677. skill_lv = script_getnum(st,3);
  12678. if (skill_db.find(skill_id) == nullptr) {
  12679. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12680. return SCRIPT_CMD_FAILURE;
  12681. }
  12682. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12683. * which leaves the server thinking it still is sitting. */
  12684. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12685. skill_sit(sd, 0);
  12686. clif_standing(&sd->bl);
  12687. }
  12688. script_skill_effect(&sd->bl, skill_id, skill_lv, sd->bl.x, sd->bl.y);
  12689. return SCRIPT_CMD_SUCCESS;
  12690. }
  12691. /*==========================================
  12692. * NPC skill effects [Valaris]
  12693. *------------------------------------------*/
  12694. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12695. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12696. BUILDIN_FUNC(npcskilleffect)
  12697. {
  12698. struct block_list *bl= map_id2bl(st->oid);
  12699. if (bl == nullptr) {
  12700. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  12701. return SCRIPT_CMD_FAILURE;
  12702. }
  12703. uint16 skill_id, skill_lv;
  12704. int16 x, y;
  12705. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12706. skill_lv=script_getnum(st,3);
  12707. x=script_getnum(st,4);
  12708. y=script_getnum(st,5);
  12709. if (skill_db.find(skill_id) == nullptr) {
  12710. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12711. return SCRIPT_CMD_FAILURE;
  12712. }
  12713. script_skill_effect(bl, skill_id, skill_lv, bl->x, bl->y);
  12714. return SCRIPT_CMD_SUCCESS;
  12715. }
  12716. /*==========================================
  12717. * Special effects [Valaris]
  12718. *------------------------------------------*/
  12719. BUILDIN_FUNC(specialeffect)
  12720. {
  12721. struct block_list *bl=map_id2bl(st->oid);
  12722. int type = script_getnum(st,2);
  12723. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12724. if(bl==NULL)
  12725. return SCRIPT_CMD_SUCCESS;
  12726. if( type <= EF_NONE || type >= EF_MAX ){
  12727. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  12728. return SCRIPT_CMD_FAILURE;
  12729. }
  12730. if( script_hasdata(st,4) )
  12731. {
  12732. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12733. if(nd)
  12734. clif_specialeffect(&nd->bl, type, target);
  12735. }
  12736. else
  12737. {
  12738. if (target == SELF) {
  12739. TBL_PC *sd;
  12740. if (script_rid2sd(sd))
  12741. clif_specialeffect_single(bl,type,sd->fd);
  12742. } else {
  12743. clif_specialeffect(bl, type, target);
  12744. }
  12745. }
  12746. return SCRIPT_CMD_SUCCESS;
  12747. }
  12748. BUILDIN_FUNC(specialeffect2)
  12749. {
  12750. TBL_PC *sd;
  12751. if( script_nick2sd(4,sd) ){
  12752. int type = script_getnum(st,2);
  12753. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12754. if( type <= EF_NONE || type >= EF_MAX ){
  12755. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  12756. return SCRIPT_CMD_FAILURE;
  12757. }
  12758. clif_specialeffect(&sd->bl, type, target);
  12759. }
  12760. return SCRIPT_CMD_SUCCESS;
  12761. }
  12762. /**
  12763. * nude({<char_id>});
  12764. * @author [Valaris]
  12765. **/
  12766. BUILDIN_FUNC(nude)
  12767. {
  12768. TBL_PC *sd;
  12769. int i, calcflag = 0;
  12770. if (!script_charid2sd(2,sd))
  12771. return SCRIPT_CMD_FAILURE;
  12772. for( i = 0 ; i < EQI_MAX; i++ ) {
  12773. if( sd->equip_index[ i ] >= 0 ) {
  12774. if( !calcflag )
  12775. calcflag = 1;
  12776. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12777. }
  12778. }
  12779. if( calcflag )
  12780. status_calc_pc(sd,SCO_NONE);
  12781. return SCRIPT_CMD_SUCCESS;
  12782. }
  12783. int atcommand_sub(struct script_state* st,int type) {
  12784. TBL_PC *sd, dummy_sd;
  12785. int fd;
  12786. const char *cmd;
  12787. cmd = script_getstr(st,2);
  12788. if (st->rid) {
  12789. if( !script_rid2sd(sd) )
  12790. return SCRIPT_CMD_SUCCESS;
  12791. fd = sd->fd;
  12792. } else { //Use a dummy character.
  12793. sd = &dummy_sd;
  12794. fd = 0;
  12795. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12796. if (st->oid) {
  12797. struct block_list* bl = map_id2bl(st->oid);
  12798. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12799. if (bl->type == BL_NPC)
  12800. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12801. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12802. }
  12803. // Init Group ID, Level, & permissions
  12804. sd->group_id = sd->group_level = 99;
  12805. sd->permissions |= PC_PERM_ALLPERMISSION;
  12806. }
  12807. if (!is_atcommand(fd, sd, cmd, type)) {
  12808. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12809. script_reportsrc(st);
  12810. return SCRIPT_CMD_FAILURE;
  12811. }
  12812. return SCRIPT_CMD_SUCCESS;
  12813. }
  12814. /*==========================================
  12815. * gmcommand [MouseJstr]
  12816. *------------------------------------------*/
  12817. BUILDIN_FUNC(atcommand) {
  12818. return atcommand_sub(st,0);
  12819. }
  12820. /** Displays a message for the player only (like system messages like "you got an apple" )
  12821. * dispbottom("<message>"{,<color>{,<char_id>}})
  12822. * @param message
  12823. * @param color Hex color default (Green)
  12824. */
  12825. BUILDIN_FUNC(dispbottom)
  12826. {
  12827. TBL_PC *sd;
  12828. int color = 0;
  12829. const char *message;
  12830. if (!script_charid2sd(4,sd))
  12831. return SCRIPT_CMD_FAILURE;
  12832. message = script_getstr(st,2);
  12833. if (script_hasdata(st,3))
  12834. color = script_getnum(st,3); // <color>
  12835. if(sd) {
  12836. if (script_hasdata(st,3))
  12837. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12838. else
  12839. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12840. }
  12841. return SCRIPT_CMD_SUCCESS;
  12842. }
  12843. /*===================================
  12844. * Heal portion of recovery command
  12845. *-----------------------------------*/
  12846. int recovery_sub(struct map_session_data* sd, int revive)
  12847. {
  12848. if(revive&(1|4) && pc_isdead(sd)) {
  12849. status_revive(&sd->bl, 100, 100);
  12850. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12851. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  12852. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12853. status_percent_heal(&sd->bl, 100, 100);
  12854. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12855. }
  12856. return SCRIPT_CMD_SUCCESS;
  12857. }
  12858. /*=========================================================================
  12859. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12860. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12861. * <type> determines <option>:
  12862. * 0 : char_id
  12863. * 1 : party_id
  12864. * 2 : guild_id
  12865. * 3 : map_name
  12866. * 4 : all characters
  12867. * <revive_flag>:
  12868. * 1 : Revive and heal all players (default)
  12869. * 2 : Heal living players only
  12870. * 4 : Revive dead players only
  12871. * <map name>:
  12872. * for types 1-2 : map_name (null = all maps)
  12873. *-------------------------------------------------------------------------*/
  12874. BUILDIN_FUNC(recovery)
  12875. {
  12876. TBL_PC *sd;
  12877. int map_idx = 0, type = 0, revive = 1;
  12878. type = script_getnum(st,2);
  12879. if(script_hasdata(st,4))
  12880. revive = script_getnum(st,4);
  12881. switch(type) {
  12882. case 0:
  12883. if(!script_charid2sd(3,sd))
  12884. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12885. recovery_sub(sd, revive);
  12886. break;
  12887. case 1:
  12888. {
  12889. struct party_data* p;
  12890. //When no party given, we use invoker party
  12891. int p_id = 0, i;
  12892. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12893. map_idx = map_mapname2mapid(script_getstr(st,5));
  12894. if(map_idx < 1) { //But we'll check anyways
  12895. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12896. return SCRIPT_CMD_FAILURE;
  12897. }
  12898. }
  12899. if(script_hasdata(st,3))
  12900. p_id = script_getnum(st,3);
  12901. else if(script_rid2sd(sd))
  12902. p_id = sd->status.party_id;
  12903. p = party_search(p_id);
  12904. if(p == NULL)
  12905. return SCRIPT_CMD_SUCCESS;
  12906. for (i = 0; i < MAX_PARTY; i++) {
  12907. struct map_session_data* pl_sd;
  12908. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12909. || (map_idx && pl_sd->bl.m != map_idx))
  12910. continue;
  12911. recovery_sub(pl_sd, revive);
  12912. }
  12913. break;
  12914. }
  12915. case 2:
  12916. {
  12917. struct guild* g;
  12918. //When no guild given, we use invoker guild
  12919. int g_id = 0, i;
  12920. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12921. map_idx = map_mapname2mapid(script_getstr(st,5));
  12922. if(map_idx < 1) { //But we'll check anyways
  12923. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12924. return SCRIPT_CMD_FAILURE;
  12925. }
  12926. }
  12927. if(script_hasdata(st,3))
  12928. g_id = script_getnum(st,3);
  12929. else if(script_rid2sd(sd))
  12930. g_id = sd->status.guild_id;
  12931. g = guild_search(g_id);
  12932. if(g == NULL)
  12933. return SCRIPT_CMD_SUCCESS;
  12934. for (i = 0; i < MAX_GUILD; i++) {
  12935. struct map_session_data* pl_sd;
  12936. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12937. || (map_idx && pl_sd->bl.m != map_idx))
  12938. continue;
  12939. recovery_sub(pl_sd, revive);
  12940. }
  12941. break;
  12942. }
  12943. case 3:
  12944. if(script_hasdata(st,3))
  12945. map_idx = map_mapname2mapid(script_getstr(st,3));
  12946. else if(script_rid2sd(sd))
  12947. map_idx = sd->bl.m;
  12948. if(map_idx < 1)
  12949. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12950. case 4:
  12951. {
  12952. struct s_mapiterator *iter;
  12953. if(script_hasdata(st,3) && script_isint(st, 3))
  12954. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12955. iter = mapit_getallusers();
  12956. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12957. if(type == 3 && sd->bl.m != map_idx)
  12958. continue;
  12959. recovery_sub(sd, revive);
  12960. }
  12961. mapit_free(iter);
  12962. break;
  12963. }
  12964. default:
  12965. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12966. script_pushint(st,-1);
  12967. return SCRIPT_CMD_FAILURE;
  12968. }
  12969. script_pushint(st,1); //Successfully executed without errors
  12970. return SCRIPT_CMD_SUCCESS;
  12971. }
  12972. /*==========================================
  12973. * Get your pet info: getpetinfo <type>{,<char_id>}
  12974. * n -> 0:pet_id 1:pet_class 2:pet_name
  12975. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12976. * 7:game id
  12977. *------------------------------------------*/
  12978. BUILDIN_FUNC(getpetinfo)
  12979. {
  12980. TBL_PC *sd;
  12981. TBL_PET *pd;
  12982. int type = script_getnum(st,2);
  12983. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12984. if (type == 2)
  12985. script_pushconststr(st,"null");
  12986. else
  12987. script_pushint(st,0);
  12988. return SCRIPT_CMD_SUCCESS;
  12989. }
  12990. switch(type){
  12991. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12992. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12993. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12994. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12995. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12996. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12997. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12998. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12999. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13000. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13001. default:
  13002. script_pushint(st,0);
  13003. break;
  13004. }
  13005. return SCRIPT_CMD_SUCCESS;
  13006. }
  13007. /*==========================================
  13008. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13009. * type -> 0:hom_id 1:class 2:name
  13010. * 3:friendly 4:hungry, 5: rename flag.
  13011. * 6: level, 7: game id
  13012. *------------------------------------------*/
  13013. BUILDIN_FUNC(gethominfo)
  13014. {
  13015. TBL_PC *sd;
  13016. TBL_HOM *hd;
  13017. int type=script_getnum(st,2);
  13018. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13019. if (type == 2)
  13020. script_pushconststr(st,"null");
  13021. else
  13022. script_pushint(st,0);
  13023. return SCRIPT_CMD_SUCCESS;
  13024. }
  13025. switch(type){
  13026. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13027. case 1: script_pushint(st,hd->homunculus.class_); break;
  13028. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13029. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13030. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13031. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13032. case 6: script_pushint(st,hd->homunculus.level); break;
  13033. case 7: script_pushint(st,hd->bl.id); break;
  13034. default:
  13035. script_pushint(st,0);
  13036. break;
  13037. }
  13038. return SCRIPT_CMD_SUCCESS;
  13039. }
  13040. BUILDIN_FUNC(addhomintimacy)
  13041. {
  13042. map_session_data *sd;
  13043. homun_data *hd;
  13044. if (!script_charid2sd(3, sd) || !(hd = sd->hd))
  13045. return SCRIPT_CMD_FAILURE;
  13046. int32 value = script_getnum(st, 2);
  13047. if (value == 0) // Nothing to change
  13048. return SCRIPT_CMD_SUCCESS;
  13049. if (value > 0)
  13050. hom_increase_intimacy(hd, (uint32)value);
  13051. else
  13052. hom_decrease_intimacy(hd, (uint32)abs(value));
  13053. clif_send_homdata(sd, SP_INTIMATE, hd->homunculus.intimacy / 100);
  13054. return SCRIPT_CMD_SUCCESS;
  13055. }
  13056. /// Retrieves information about character's mercenary
  13057. /// getmercinfo <type>[,<char id>];
  13058. BUILDIN_FUNC(getmercinfo)
  13059. {
  13060. int type;
  13061. struct map_session_data* sd;
  13062. struct mercenary_data* md;
  13063. if( !script_charid2sd(3,sd) ){
  13064. script_pushnil(st);
  13065. return SCRIPT_CMD_FAILURE;
  13066. }
  13067. type = script_getnum(st,2);
  13068. md = sd->md;
  13069. if( md == NULL ){
  13070. if( type == 2 )
  13071. script_pushconststr(st,"");
  13072. else
  13073. script_pushint(st,0);
  13074. return SCRIPT_CMD_SUCCESS;
  13075. }
  13076. switch( type )
  13077. {
  13078. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13079. case 1: script_pushint(st,md->mercenary.class_); break;
  13080. case 2: script_pushstrcopy(st,md->db->name); break;
  13081. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13082. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13083. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13084. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13085. case 7: script_pushint(st,md->db->lv); break;
  13086. case 8: script_pushint(st,md->bl.id); break;
  13087. default:
  13088. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13089. script_pushnil(st);
  13090. return SCRIPT_CMD_FAILURE;
  13091. }
  13092. return SCRIPT_CMD_SUCCESS;
  13093. }
  13094. /*==========================================
  13095. * Shows wether your inventory(and equips) contain
  13096. selected card or not.
  13097. checkequipedcard(4001);
  13098. *------------------------------------------*/
  13099. BUILDIN_FUNC(checkequipedcard)
  13100. {
  13101. TBL_PC *sd;
  13102. if(script_rid2sd(sd)){
  13103. int n,i,c=0;
  13104. c=script_getnum(st,2);
  13105. for(i=0;i<MAX_INVENTORY;i++){
  13106. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13107. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13108. continue;
  13109. for(n=0;n<sd->inventory_data[i]->slots;n++){
  13110. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13111. script_pushint(st,1);
  13112. return SCRIPT_CMD_SUCCESS;
  13113. }
  13114. }
  13115. }
  13116. }
  13117. }
  13118. script_pushint(st,0);
  13119. return SCRIPT_CMD_SUCCESS;
  13120. }
  13121. BUILDIN_FUNC(jump_zero)
  13122. {
  13123. int64 sel=script_getnum64(st,2);
  13124. if(!sel) {
  13125. int pos;
  13126. if( !data_islabel(script_getdata(st,3)) ){
  13127. ShowError("script: jump_zero: not label !\n");
  13128. st->state=END;
  13129. return SCRIPT_CMD_FAILURE;
  13130. }
  13131. pos=script_getnum(st,3);
  13132. st->pos=pos;
  13133. st->state=GOTO;
  13134. }
  13135. return SCRIPT_CMD_SUCCESS;
  13136. }
  13137. /*==========================================
  13138. * movenpc [MouseJstr]
  13139. *------------------------------------------*/
  13140. BUILDIN_FUNC(movenpc)
  13141. {
  13142. TBL_NPC *nd = NULL;
  13143. const char *npc;
  13144. int x,y;
  13145. npc = script_getstr(st,2);
  13146. x = script_getnum(st,3);
  13147. y = script_getnum(st,4);
  13148. if ((nd = npc_name2id(npc)) == NULL){
  13149. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13150. return -1;
  13151. }
  13152. if (script_hasdata(st,5))
  13153. nd->ud.dir = script_getnum(st,5) % 8;
  13154. npc_movenpc(nd, x, y);
  13155. return SCRIPT_CMD_SUCCESS;
  13156. }
  13157. /*==========================================
  13158. * message [MouseJstr]
  13159. *------------------------------------------*/
  13160. BUILDIN_FUNC(message)
  13161. {
  13162. const char *msg,*player;
  13163. TBL_PC *pl_sd = NULL;
  13164. player = script_getstr(st,2);
  13165. msg = script_getstr(st,3);
  13166. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13167. return SCRIPT_CMD_SUCCESS;
  13168. clif_displaymessage(pl_sd->fd, msg);
  13169. return SCRIPT_CMD_SUCCESS;
  13170. }
  13171. /**
  13172. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13173. * @param flag: Specify target
  13174. * BC_ALL - Broadcast message is sent server-wide.
  13175. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13176. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13177. * BC_SELF - Message is sent only to player attached.
  13178. */
  13179. BUILDIN_FUNC(npctalk)
  13180. {
  13181. struct npc_data* nd = NULL;
  13182. const char* str = script_getstr(st,2);
  13183. int color = 0xFFFFFF;
  13184. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13185. nd = npc_name2id(script_getstr(st, 3));
  13186. else
  13187. nd = (struct npc_data *)map_id2bl(st->oid);
  13188. if (script_hasdata(st, 5))
  13189. color = script_getnum(st, 5);
  13190. if (nd != NULL) {
  13191. send_target target = AREA;
  13192. char message[CHAT_SIZE_MAX];
  13193. if (script_hasdata(st, 4)) {
  13194. switch(script_getnum(st, 4)) {
  13195. case BC_ALL: target = ALL_CLIENT; break;
  13196. case BC_MAP: target = ALL_SAMEMAP; break;
  13197. case BC_SELF: target = SELF; break;
  13198. case BC_AREA:
  13199. default: target = AREA; break;
  13200. }
  13201. }
  13202. safesnprintf(message, sizeof(message), "%s", str);
  13203. if (target != SELF)
  13204. clif_messagecolor(&nd->bl, color, message, true, target);
  13205. else {
  13206. TBL_PC *sd = map_id2sd(st->rid);
  13207. if (sd == NULL)
  13208. return SCRIPT_CMD_FAILURE;
  13209. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13210. }
  13211. }
  13212. return SCRIPT_CMD_SUCCESS;
  13213. }
  13214. /**
  13215. * Sends a message to the waitingroom of the invoking NPC.
  13216. * chatmes "<message>"{,"<NPC name>"};
  13217. * @author Jey
  13218. */
  13219. BUILDIN_FUNC(chatmes)
  13220. {
  13221. struct npc_data* nd = NULL;
  13222. const char* str = script_getstr(st,2);
  13223. if (script_hasdata(st, 3))
  13224. nd = npc_name2id(script_getstr(st, 3));
  13225. else
  13226. nd = (struct npc_data *)map_id2bl(st->oid);
  13227. if (nd != NULL && nd->chat_id) {
  13228. char message[256];
  13229. safesnprintf(message, sizeof(message), "%s", str);
  13230. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13231. }
  13232. return SCRIPT_CMD_SUCCESS;
  13233. }
  13234. // change npc walkspeed [Valaris]
  13235. BUILDIN_FUNC(npcspeed)
  13236. {
  13237. struct npc_data* nd;
  13238. int speed;
  13239. speed = script_getnum(st,2);
  13240. nd =(struct npc_data *)map_id2bl(st->oid);
  13241. if( nd ) {
  13242. nd->speed = speed;
  13243. nd->ud.state.speed_changed = 1;
  13244. }
  13245. return SCRIPT_CMD_SUCCESS;
  13246. }
  13247. // make an npc walk to a position [Valaris]
  13248. BUILDIN_FUNC(npcwalkto)
  13249. {
  13250. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13251. int x=0,y=0;
  13252. x=script_getnum(st,2);
  13253. y=script_getnum(st,3);
  13254. if(nd) {
  13255. if (!nd->status.hp)
  13256. status_calc_npc(nd, SCO_FIRST);
  13257. else
  13258. status_calc_npc(nd, SCO_NONE);
  13259. unit_walktoxy(&nd->bl,x,y,0);
  13260. }
  13261. return SCRIPT_CMD_SUCCESS;
  13262. }
  13263. // stop an npc's movement [Valaris]
  13264. BUILDIN_FUNC(npcstop)
  13265. {
  13266. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13267. if(nd) {
  13268. unit_stop_walking(&nd->bl,1|4);
  13269. }
  13270. return SCRIPT_CMD_SUCCESS;
  13271. }
  13272. /**
  13273. * getlook(<type>{,<char_id>})
  13274. **/
  13275. BUILDIN_FUNC(getlook)
  13276. {
  13277. int type,val;
  13278. TBL_PC *sd;
  13279. if (!script_charid2sd(3,sd)) {
  13280. script_pushint(st,-1);
  13281. return SCRIPT_CMD_FAILURE;
  13282. }
  13283. type=script_getnum(st,2);
  13284. val=-1;
  13285. switch(type) {
  13286. // TODO: implement LOOK_BASE as stated in script doc
  13287. case LOOK_HAIR: val=sd->status.hair; break; //1
  13288. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13289. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13290. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13291. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13292. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13293. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13294. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13295. case LOOK_SHOES: break; //9
  13296. case LOOK_ROBE: val=sd->status.robe; break; //12
  13297. case LOOK_BODY2: val=sd->status.body; break; //13
  13298. }
  13299. script_pushint(st,val);
  13300. return SCRIPT_CMD_SUCCESS;
  13301. }
  13302. /**
  13303. * getsavepoint(<information type>{,<char_id>})
  13304. * @param type 0- map name, 1- x, 2- y
  13305. **/
  13306. BUILDIN_FUNC(getsavepoint)
  13307. {
  13308. TBL_PC* sd;
  13309. int type;
  13310. if (!script_charid2sd(3,sd)) {
  13311. script_pushint(st,0);
  13312. return SCRIPT_CMD_SUCCESS;
  13313. }
  13314. type = script_getnum(st,2);
  13315. switch(type) {
  13316. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13317. case 1: script_pushint(st,sd->status.save_point.x); break;
  13318. case 2: script_pushint(st,sd->status.save_point.y); break;
  13319. default:
  13320. script_pushint(st,0);
  13321. break;
  13322. }
  13323. return SCRIPT_CMD_SUCCESS;
  13324. }
  13325. /**
  13326. * Get position for BL objects.
  13327. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13328. * @param mapname: String variable for output map name
  13329. * @param x: Integer variable for output coord X
  13330. * @param y: Integer variable for output coord Y
  13331. * @param type: Type of object
  13332. * BL_PC - Character coord
  13333. * BL_NPC - NPC coord
  13334. * BL_PET - Pet coord
  13335. * BL_HOM - Homun coord
  13336. * BL_MER - Mercenary coord
  13337. * BL_ELEM - Elemental coord
  13338. * @param charname: Name object. If empty or "this" use the current object
  13339. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13340. */
  13341. BUILDIN_FUNC(getmapxy)
  13342. {
  13343. struct block_list *bl = NULL;
  13344. TBL_PC *sd=NULL;
  13345. int64 num;
  13346. const char *name;
  13347. char prefix;
  13348. int x,y,type = BL_PC;
  13349. char mapname[MAP_NAME_LENGTH];
  13350. if( !data_isreference(script_getdata(st,2)) ) {
  13351. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13352. script_pushint(st,-1);
  13353. return SCRIPT_CMD_FAILURE;
  13354. }
  13355. if( !data_isreference(script_getdata(st,3)) ) {
  13356. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13357. script_pushint(st,-1);
  13358. return SCRIPT_CMD_FAILURE;
  13359. }
  13360. if( !data_isreference(script_getdata(st,4)) ) {
  13361. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13362. script_pushint(st,-1);
  13363. return SCRIPT_CMD_FAILURE;
  13364. }
  13365. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13366. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13367. script_pushint(st,-1);
  13368. return SCRIPT_CMD_FAILURE;
  13369. }
  13370. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13371. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13372. script_pushint(st,-1);
  13373. return SCRIPT_CMD_FAILURE;
  13374. }
  13375. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13376. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13377. script_pushint(st,-1);
  13378. return SCRIPT_CMD_FAILURE;
  13379. }
  13380. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13381. if (script_hasdata(st, 5))
  13382. type = script_getnum(st, 5);
  13383. switch (type) {
  13384. case BL_PC: //Get Character Position
  13385. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  13386. bl = &sd->bl;
  13387. break;
  13388. case BL_NPC: //Get NPC Position
  13389. if (script_hasdata(st, 6)) {
  13390. struct npc_data *nd;
  13391. if (script_isstring(st, 6))
  13392. nd = npc_name2id(script_getstr(st, 6));
  13393. else
  13394. nd = map_id2nd(script_getnum(st, 6));
  13395. if (nd)
  13396. bl = &nd->bl;
  13397. } else //In case the origin is not an NPC?
  13398. bl = map_id2bl(st->oid);
  13399. break;
  13400. case BL_PET: //Get Pet Position
  13401. if (sd->pd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13402. bl = &sd->pd->bl;
  13403. break;
  13404. case BL_HOM: //Get Homun Position
  13405. if (sd->hd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13406. bl = &sd->hd->bl;
  13407. break;
  13408. case BL_MER: //Get Mercenary Position
  13409. if (sd->md && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13410. bl = &sd->md->bl;
  13411. break;
  13412. case BL_ELEM: //Get Elemental Position
  13413. if (sd->ed && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13414. bl = &sd->ed->bl;
  13415. break;
  13416. default:
  13417. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13418. script_pushint(st,-1);
  13419. return SCRIPT_CMD_FAILURE;
  13420. }
  13421. if (!bl) { //No object found.
  13422. script_pushint(st,-1);
  13423. return SCRIPT_CMD_SUCCESS;
  13424. }
  13425. x= bl->x;
  13426. y= bl->y;
  13427. if (bl->m >= 0)
  13428. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  13429. else
  13430. memset(mapname, '\0', sizeof(mapname));
  13431. //Set MapName$
  13432. num=st->stack->stack_data[st->start+2].u.num;
  13433. name=get_str(script_getvarid(num));
  13434. prefix=*name;
  13435. if(not_server_variable(prefix)){
  13436. if( !script_rid2sd(sd) ){
  13437. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13438. return SCRIPT_CMD_FAILURE;
  13439. }
  13440. }else
  13441. sd=NULL;
  13442. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  13443. //Set MapX
  13444. num=st->stack->stack_data[st->start+3].u.num;
  13445. name=get_str(script_getvarid(num));
  13446. prefix=*name;
  13447. if(not_server_variable(prefix)){
  13448. if( !script_rid2sd(sd) ){
  13449. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13450. return SCRIPT_CMD_FAILURE;
  13451. }
  13452. }else
  13453. sd=NULL;
  13454. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  13455. //Set MapY
  13456. num=st->stack->stack_data[st->start+4].u.num;
  13457. name=get_str(script_getvarid(num));
  13458. prefix=*name;
  13459. if(not_server_variable(prefix)){
  13460. if( !script_rid2sd(sd) ){
  13461. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13462. return SCRIPT_CMD_FAILURE;
  13463. }
  13464. }else
  13465. sd=NULL;
  13466. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  13467. //Return Success value
  13468. script_pushint(st,0);
  13469. return SCRIPT_CMD_SUCCESS;
  13470. }
  13471. /// Returns the map name of given map ID.
  13472. ///
  13473. /// mapid2name <map ID>;
  13474. BUILDIN_FUNC(mapid2name)
  13475. {
  13476. uint16 m = script_getnum(st, 2);
  13477. if (m < 0) {
  13478. script_pushconststr(st, "");
  13479. return SCRIPT_CMD_FAILURE;
  13480. }
  13481. script_pushstrcopy(st, map_mapid2mapname(m));
  13482. return SCRIPT_CMD_SUCCESS;
  13483. }
  13484. /*==========================================
  13485. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13486. *------------------------------------------*/
  13487. BUILDIN_FUNC(logmes)
  13488. {
  13489. const char *str;
  13490. struct map_session_data* sd = map_id2sd(st->rid);
  13491. str = script_getstr(st,2);
  13492. if( sd ){
  13493. log_npc(sd,str);
  13494. }else{
  13495. struct npc_data* nd = map_id2nd(st->oid);
  13496. if( !nd ){
  13497. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  13498. return SCRIPT_CMD_FAILURE;
  13499. }
  13500. log_npc(nd,str);
  13501. }
  13502. return SCRIPT_CMD_SUCCESS;
  13503. }
  13504. BUILDIN_FUNC(summon)
  13505. {
  13506. int _class, timeout=0;
  13507. const char *str,*event="";
  13508. TBL_PC *sd;
  13509. struct mob_data *md;
  13510. t_tick tick = gettick();
  13511. if (!script_rid2sd(sd))
  13512. return SCRIPT_CMD_SUCCESS;
  13513. str =script_getstr(st,2);
  13514. _class=script_getnum(st,3);
  13515. if( script_hasdata(st,4) )
  13516. timeout=script_getnum(st,4);
  13517. if( script_hasdata(st,5) ){
  13518. event=script_getstr(st,5);
  13519. check_event(st, event);
  13520. }
  13521. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13522. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13523. if (md) {
  13524. md->master_id=sd->bl.id;
  13525. md->special_state.ai = AI_ATTACK;
  13526. if( md->deletetimer != INVALID_TIMER )
  13527. delete_timer(md->deletetimer, mob_timer_delete);
  13528. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13529. mob_spawn (md); //Now it is ready for spawning.
  13530. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  13531. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13532. }
  13533. script_pushint(st, md->bl.id);
  13534. return SCRIPT_CMD_SUCCESS;
  13535. }
  13536. /*==========================================
  13537. * Checks whether it is daytime/nighttime
  13538. *------------------------------------------*/
  13539. BUILDIN_FUNC(isnight)
  13540. {
  13541. script_pushint(st,(night_flag == 1));
  13542. return SCRIPT_CMD_SUCCESS;
  13543. }
  13544. BUILDIN_FUNC(isday)
  13545. {
  13546. script_pushint(st,(night_flag == 0));
  13547. return SCRIPT_CMD_SUCCESS;
  13548. }
  13549. /*================================================
  13550. * Check how many items/cards in the list are
  13551. * equipped - used for 2/15's cards patch [celest]
  13552. *------------------------------------------------*/
  13553. BUILDIN_FUNC(isequippedcnt)
  13554. {
  13555. TBL_PC *sd;
  13556. if (!script_rid2sd(sd)) {
  13557. script_pushint(st,0);
  13558. return SCRIPT_CMD_SUCCESS;
  13559. }
  13560. int ret = 0;
  13561. int total = script_lastdata(st);
  13562. std::vector<int32> list(total);
  13563. for (int i = 2; i <= total; ++i) {
  13564. int id = script_getnum(st,i);
  13565. if (id <= 0)
  13566. continue;
  13567. if (std::find(list.begin(), list.end(), id) != list.end())
  13568. continue;
  13569. list.push_back(id);
  13570. for (short j = 0; j < EQI_MAX; j++) {
  13571. short index = sd->equip_index[j];
  13572. if (index < 0)
  13573. continue;
  13574. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13575. continue;
  13576. if (!sd->inventory_data[index])
  13577. continue;
  13578. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13579. if (sd->inventory_data[index]->nameid == id)
  13580. ret += sd->inventory.u.items_inventory[index].amount;
  13581. } else { //Count cards.
  13582. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13583. continue; //No cards
  13584. for (short k = 0; k < sd->inventory_data[index]->slots; k++) {
  13585. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13586. ret++; //[Lupus]
  13587. }
  13588. }
  13589. }
  13590. }
  13591. script_pushint(st,ret);
  13592. return SCRIPT_CMD_SUCCESS;
  13593. }
  13594. /*================================================
  13595. * Check whether another card has been
  13596. * equipped - used for 2/15's cards patch [celest]
  13597. * -- Items checked cannot be reused in another
  13598. * card set to prevent exploits
  13599. *------------------------------------------------*/
  13600. BUILDIN_FUNC(isequipped)
  13601. {
  13602. TBL_PC *sd;
  13603. int i, id = 1;
  13604. int ret = -1;
  13605. //Original hash to reverse it when full check fails.
  13606. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13607. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13608. script_pushint(st,0);
  13609. return SCRIPT_CMD_SUCCESS;
  13610. }
  13611. setitem_hash = sd->bonus.setitem_hash;
  13612. setitem_hash2 = sd->bonus.setitem_hash2;
  13613. for (i=0; id!=0; i++) {
  13614. int flag = 0;
  13615. short j;
  13616. FETCH (i+2, id) else id = 0;
  13617. if (id <= 0)
  13618. continue;
  13619. for (j=0; j<EQI_MAX; j++) {
  13620. short index = sd->equip_index[j];
  13621. if(index < 0)
  13622. continue;
  13623. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13624. continue;
  13625. if(!sd->inventory_data[index])
  13626. continue;
  13627. if (itemdb_type(id) != IT_CARD) {
  13628. if (sd->inventory_data[index]->nameid != id)
  13629. continue;
  13630. flag = 1;
  13631. break;
  13632. } else { //Cards
  13633. short k;
  13634. if (sd->inventory_data[index]->slots == 0 ||
  13635. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13636. continue;
  13637. for (k = 0; k < sd->inventory_data[index]->slots; k++)
  13638. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13639. unsigned int hash = 0;
  13640. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13641. continue;
  13642. hash = 1<<((j<5?j:j-5)*4 + k);
  13643. // check if card is already used by another set
  13644. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13645. continue;
  13646. // We have found a match
  13647. flag = 1;
  13648. // Set hash so this card cannot be used by another
  13649. if (j<5)
  13650. sd->bonus.setitem_hash |= hash;
  13651. else
  13652. sd->bonus.setitem_hash2 |= hash;
  13653. break;
  13654. }
  13655. }
  13656. if (flag) break; //Card found
  13657. }
  13658. if (ret == -1)
  13659. ret = flag;
  13660. else
  13661. ret &= flag;
  13662. if (!ret) break;
  13663. }
  13664. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13665. sd->bonus.setitem_hash = setitem_hash;
  13666. sd->bonus.setitem_hash2 = setitem_hash2;
  13667. }
  13668. script_pushint(st,ret);
  13669. return SCRIPT_CMD_SUCCESS;
  13670. }
  13671. /*================================================
  13672. * Check how many given inserted cards in the CURRENT
  13673. * weapon - used for 2/15's cards patch [Lupus]
  13674. *------------------------------------------------*/
  13675. BUILDIN_FUNC(cardscnt)
  13676. {
  13677. TBL_PC *sd;
  13678. int i, k, id = 1;
  13679. int ret = 0;
  13680. int index;
  13681. if( !script_rid2sd(sd) ){
  13682. script_pushint(st,0);
  13683. return SCRIPT_CMD_SUCCESS;
  13684. }
  13685. for (i=0; id!=0; i++) {
  13686. FETCH (i+2, id) else id = 0;
  13687. if (id <= 0)
  13688. continue;
  13689. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  13690. if(index < 0) continue;
  13691. if(!sd->inventory_data[index])
  13692. continue;
  13693. if(itemdb_type(id) != IT_CARD) {
  13694. if (sd->inventory_data[index]->nameid == id)
  13695. ret+= sd->inventory.u.items_inventory[index].amount;
  13696. } else {
  13697. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13698. continue;
  13699. for(k=0; k<sd->inventory_data[index]->slots; k++) {
  13700. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13701. ret++;
  13702. }
  13703. }
  13704. }
  13705. script_pushint(st,ret);
  13706. // script_pushint(st,current_equip_item_index);
  13707. return SCRIPT_CMD_SUCCESS;
  13708. }
  13709. /*=======================================================
  13710. * Returns the refined number of the current item, or an
  13711. * item with inventory index specified
  13712. *-------------------------------------------------------*/
  13713. BUILDIN_FUNC(getrefine)
  13714. {
  13715. TBL_PC *sd;
  13716. if (script_rid2sd(sd)){
  13717. if( current_equip_item_index == -1 ){
  13718. script_pushint(st, 0);
  13719. return SCRIPT_CMD_FAILURE;
  13720. }
  13721. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13722. }else
  13723. script_pushint(st,0);
  13724. return SCRIPT_CMD_SUCCESS;
  13725. }
  13726. /*=======================================================
  13727. * Day/Night controls
  13728. *-------------------------------------------------------*/
  13729. BUILDIN_FUNC(night)
  13730. {
  13731. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13732. return SCRIPT_CMD_SUCCESS;
  13733. }
  13734. BUILDIN_FUNC(day)
  13735. {
  13736. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13737. return SCRIPT_CMD_SUCCESS;
  13738. }
  13739. /**
  13740. * unequip <equipment slot>{,<char_id>};
  13741. * @author [Spectre]
  13742. **/
  13743. BUILDIN_FUNC(unequip) {
  13744. int pos;
  13745. TBL_PC *sd;
  13746. if (!script_charid2sd(3,sd))
  13747. return SCRIPT_CMD_FAILURE;
  13748. pos = script_getnum(st,2);
  13749. if (equip_index_check(pos)) {
  13750. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13751. if (i >= 0) {
  13752. pc_unequipitem(sd,i,1|2);
  13753. script_pushint(st, 1);
  13754. return SCRIPT_CMD_SUCCESS;
  13755. }
  13756. }
  13757. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13758. script_pushint(st, 0);
  13759. return SCRIPT_CMD_FAILURE;
  13760. }
  13761. /**
  13762. * equip <item id>{,<char_id>};
  13763. **/
  13764. BUILDIN_FUNC(equip) {
  13765. TBL_PC *sd;
  13766. struct item_data *item_data;
  13767. if (!script_charid2sd(3,sd))
  13768. return SCRIPT_CMD_FAILURE;
  13769. t_itemid nameid = script_getnum(st,2);
  13770. if ((item_data = itemdb_exists(nameid))) {
  13771. int i;
  13772. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13773. if (i < MAX_INVENTORY) {
  13774. pc_equipitem(sd,i,item_data->equip);
  13775. script_pushint(st,1);
  13776. return SCRIPT_CMD_SUCCESS;
  13777. }
  13778. }
  13779. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  13780. script_pushint(st,0);
  13781. return SCRIPT_CMD_FAILURE;
  13782. }
  13783. BUILDIN_FUNC(autoequip)
  13784. {
  13785. int flag;
  13786. struct item_data *item_data;
  13787. t_itemid nameid=script_getnum(st,2);
  13788. flag=script_getnum(st,3);
  13789. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13790. {
  13791. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  13792. return SCRIPT_CMD_FAILURE;
  13793. }
  13794. if( !itemdb_isequip2(item_data) )
  13795. {
  13796. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  13797. return SCRIPT_CMD_FAILURE;
  13798. }
  13799. item_data->flag.autoequip = flag>0?1:0;
  13800. return SCRIPT_CMD_SUCCESS;
  13801. }
  13802. /**
  13803. * Set the value of a given battle config
  13804. * setbattleflag "<battle flag>",<value>{,<reload>};
  13805. */
  13806. BUILDIN_FUNC(setbattleflag)
  13807. {
  13808. const char *flag, *value;
  13809. flag = script_getstr(st,2);
  13810. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13811. if (battle_set_value(flag, value) == 0)
  13812. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13813. else {
  13814. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13815. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  13816. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  13817. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  13818. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  13819. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  13820. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  13821. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  13822. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  13823. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  13824. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  13825. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  13826. };
  13827. uint8 i;
  13828. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  13829. if (!strcmpi(flag, expdrop_flags[i])) {
  13830. mob_reload();
  13831. break;
  13832. }
  13833. }
  13834. }
  13835. }
  13836. return SCRIPT_CMD_SUCCESS;
  13837. }
  13838. /**
  13839. * Get the value of a given battle config
  13840. * getbattleflag("<battle flag>")
  13841. */
  13842. BUILDIN_FUNC(getbattleflag)
  13843. {
  13844. const char *flag = script_getstr(st,2);
  13845. script_pushint(st,battle_get_value(flag));
  13846. return SCRIPT_CMD_SUCCESS;
  13847. }
  13848. //=======================================================
  13849. // strlen [Valaris]
  13850. //-------------------------------------------------------
  13851. BUILDIN_FUNC(getstrlen)
  13852. {
  13853. const char *str = script_getstr(st,2);
  13854. int len = (str) ? (int)strlen(str) : 0;
  13855. script_pushint(st,len);
  13856. return SCRIPT_CMD_SUCCESS;
  13857. }
  13858. //=======================================================
  13859. // isalpha [Valaris]
  13860. //-------------------------------------------------------
  13861. BUILDIN_FUNC(charisalpha)
  13862. {
  13863. const char *str=script_getstr(st,2);
  13864. int pos=script_getnum(st,3);
  13865. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13866. script_pushint(st,val);
  13867. return SCRIPT_CMD_SUCCESS;
  13868. }
  13869. //=======================================================
  13870. // charisupper <str>, <index>
  13871. //-------------------------------------------------------
  13872. BUILDIN_FUNC(charisupper)
  13873. {
  13874. const char *str = script_getstr(st,2);
  13875. int pos = script_getnum(st,3);
  13876. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13877. script_pushint(st,val);
  13878. return SCRIPT_CMD_SUCCESS;
  13879. }
  13880. //=======================================================
  13881. // charislower <str>, <index>
  13882. //-------------------------------------------------------
  13883. BUILDIN_FUNC(charislower)
  13884. {
  13885. const char *str = script_getstr(st,2);
  13886. int pos = script_getnum(st,3);
  13887. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13888. script_pushint(st,val);
  13889. return SCRIPT_CMD_SUCCESS;
  13890. }
  13891. //=======================================================
  13892. // charat <str>, <index>
  13893. //-------------------------------------------------------
  13894. BUILDIN_FUNC(charat) {
  13895. const char *str = script_getstr(st,2);
  13896. int pos = script_getnum(st,3);
  13897. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13898. char output[2];
  13899. output[0] = str[pos];
  13900. output[1] = '\0';
  13901. script_pushstrcopy(st, output);
  13902. } else
  13903. script_pushconststr(st, "");
  13904. return SCRIPT_CMD_SUCCESS;
  13905. }
  13906. //=======================================================
  13907. // setchar <string>, <char>, <index>
  13908. //-------------------------------------------------------
  13909. BUILDIN_FUNC(setchar)
  13910. {
  13911. const char *str = script_getstr(st,2);
  13912. const char *c = script_getstr(st,3);
  13913. int index = script_getnum(st,4);
  13914. char *output = aStrdup(str);
  13915. if(index >= 0 && index < strlen(output))
  13916. output[index] = *c;
  13917. script_pushstr(st, output);
  13918. return SCRIPT_CMD_SUCCESS;
  13919. }
  13920. //=======================================================
  13921. // insertchar <string>, <char>, <index>
  13922. //-------------------------------------------------------
  13923. BUILDIN_FUNC(insertchar)
  13924. {
  13925. const char *str = script_getstr(st,2);
  13926. const char *c = script_getstr(st,3);
  13927. int index = script_getnum(st,4);
  13928. char *output;
  13929. size_t len = strlen(str);
  13930. if(index < 0)
  13931. index = 0;
  13932. else if(index > len)
  13933. index = len;
  13934. output = (char*)aMalloc(len + 2);
  13935. memcpy(output, str, index);
  13936. output[index] = c[0];
  13937. memcpy(&output[index+1], &str[index], len - index);
  13938. output[len+1] = '\0';
  13939. script_pushstr(st, output);
  13940. return SCRIPT_CMD_SUCCESS;
  13941. }
  13942. //=======================================================
  13943. // delchar <string>, <index>
  13944. //-------------------------------------------------------
  13945. BUILDIN_FUNC(delchar)
  13946. {
  13947. const char *str = script_getstr(st,2);
  13948. int index = script_getnum(st,3);
  13949. char *output;
  13950. size_t len = strlen(str);
  13951. if(index < 0 || index > len) {
  13952. //return original
  13953. output = aStrdup(str);
  13954. script_pushstr(st, output);
  13955. return SCRIPT_CMD_SUCCESS;
  13956. }
  13957. output = (char*)aMalloc(len);
  13958. memcpy(output, str, index);
  13959. memcpy(&output[index], &str[index+1], len - index);
  13960. script_pushstr(st, output);
  13961. return SCRIPT_CMD_SUCCESS;
  13962. }
  13963. //=======================================================
  13964. // strtoupper <str>
  13965. //-------------------------------------------------------
  13966. BUILDIN_FUNC(strtoupper)
  13967. {
  13968. const char *str = script_getstr(st,2);
  13969. char *output = aStrdup(str);
  13970. char *cursor = output;
  13971. while (*cursor != '\0') {
  13972. *cursor = TOUPPER(*cursor);
  13973. cursor++;
  13974. }
  13975. script_pushstr(st, output);
  13976. return SCRIPT_CMD_SUCCESS;
  13977. }
  13978. //=======================================================
  13979. // strtolower <str>
  13980. //-------------------------------------------------------
  13981. BUILDIN_FUNC(strtolower)
  13982. {
  13983. const char *str = script_getstr(st,2);
  13984. char *output = aStrdup(str);
  13985. char *cursor = output;
  13986. while (*cursor != '\0') {
  13987. *cursor = TOLOWER(*cursor);
  13988. cursor++;
  13989. }
  13990. script_pushstr(st, output);
  13991. return SCRIPT_CMD_SUCCESS;
  13992. }
  13993. //=======================================================
  13994. // substr <str>, <start>, <end>
  13995. //-------------------------------------------------------
  13996. BUILDIN_FUNC(substr)
  13997. {
  13998. const char *str = script_getstr(st,2);
  13999. char *output;
  14000. int start = script_getnum(st,3);
  14001. int end = script_getnum(st,4);
  14002. int len = 0;
  14003. if(start >= 0 && end < strlen(str) && start <= end) {
  14004. len = end - start + 1;
  14005. output = (char*)aMalloc(len + 1);
  14006. memcpy(output, &str[start], len);
  14007. } else
  14008. output = (char*)aMalloc(1);
  14009. output[len] = '\0';
  14010. script_pushstr(st, output);
  14011. return SCRIPT_CMD_SUCCESS;
  14012. }
  14013. //=======================================================
  14014. // explode <dest_string_array>, <str>, <delimiter>
  14015. // Note: delimiter is limited to 1 char
  14016. //-------------------------------------------------------
  14017. BUILDIN_FUNC(explode)
  14018. {
  14019. struct script_data* data = script_getdata(st, 2);
  14020. const char *str = script_getstr(st,3);
  14021. const char delimiter = script_getstr(st, 4)[0];
  14022. int32 id;
  14023. size_t len = strlen(str);
  14024. int i = 0, j = 0;
  14025. int start;
  14026. char *temp;
  14027. const char* name;
  14028. TBL_PC* sd = NULL;
  14029. temp = (char*)aMalloc(len + 1);
  14030. if( !data_isreference(data) ) {
  14031. ShowError("script:explode: not a variable\n");
  14032. script_reportdata(data);
  14033. st->state = END;
  14034. return SCRIPT_CMD_FAILURE;// not a variable
  14035. }
  14036. id = reference_getid(data);
  14037. start = reference_getindex(data);
  14038. name = reference_getname(data);
  14039. if( !is_string_variable(name) ) {
  14040. ShowError("script:explode: not string array\n");
  14041. script_reportdata(data);
  14042. st->state = END;
  14043. return SCRIPT_CMD_FAILURE;// data type mismatch
  14044. }
  14045. if( not_server_variable(*name) ) {
  14046. if( !script_rid2sd(sd) )
  14047. return SCRIPT_CMD_SUCCESS;// no player attached
  14048. }
  14049. while(str[i] != '\0') {
  14050. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14051. temp[j] = '\0';
  14052. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14053. j = 0;
  14054. ++i;
  14055. } else {
  14056. temp[j++] = str[i++];
  14057. }
  14058. }
  14059. //set last string
  14060. temp[j] = '\0';
  14061. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14062. aFree(temp);
  14063. return SCRIPT_CMD_SUCCESS;
  14064. }
  14065. //=======================================================
  14066. // implode <string_array>
  14067. // implode <string_array>, <glue>
  14068. //-------------------------------------------------------
  14069. BUILDIN_FUNC(implode)
  14070. {
  14071. struct script_data* data = script_getdata(st, 2);
  14072. const char *name;
  14073. uint32 glue_len = 0, array_size, id;
  14074. char *output;
  14075. TBL_PC* sd = NULL;
  14076. if( !data_isreference(data) ) {
  14077. ShowError("script:implode: not a variable\n");
  14078. script_reportdata(data);
  14079. st->state = END;
  14080. return SCRIPT_CMD_FAILURE;// not a variable
  14081. }
  14082. id = reference_getid(data);
  14083. name = reference_getname(data);
  14084. if( !is_string_variable(name) ) {
  14085. ShowError("script:implode: not string array\n");
  14086. script_reportdata(data);
  14087. st->state = END;
  14088. return SCRIPT_CMD_FAILURE;// data type mismatch
  14089. }
  14090. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14091. return SCRIPT_CMD_SUCCESS;// no player attached
  14092. }
  14093. //count chars
  14094. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  14095. if(array_size == -1) { //empty array check (AmsTaff)
  14096. ShowWarning("script:implode: array length = 0\n");
  14097. output = (char*)aMalloc(sizeof(char)*5);
  14098. sprintf(output,"%s","NULL");
  14099. } else {
  14100. const char *glue = NULL, *temp;
  14101. size_t len = 0;
  14102. int i, k = 0;
  14103. for(i = 0; i <= array_size; ++i) {
  14104. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14105. len += strlen(temp);
  14106. // Remove stack entry from get_val2_str
  14107. script_removetop( st, -1, 0 );
  14108. }
  14109. //allocate mem
  14110. if( script_hasdata(st,3) ) {
  14111. glue = script_getstr(st,3);
  14112. glue_len = strlen(glue);
  14113. len += glue_len * (array_size);
  14114. }
  14115. output = (char*)aMalloc(len + 1);
  14116. //build output
  14117. for(i = 0; i < array_size; ++i) {
  14118. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14119. len = strlen(temp);
  14120. memcpy(&output[k], temp, len);
  14121. k += len;
  14122. // Remove stack entry from get_val2_str
  14123. script_removetop( st, -1, 0 );
  14124. if(glue_len != 0) {
  14125. memcpy(&output[k], glue, glue_len);
  14126. k += glue_len;
  14127. }
  14128. }
  14129. temp = get_val2_str( st, reference_uid( id, array_size ), reference_getref( data ) );
  14130. len = strlen(temp);
  14131. memcpy(&output[k], temp, len);
  14132. k += len;
  14133. output[k] = '\0';
  14134. // Remove stack entry from get_val2_str
  14135. script_removetop( st, -1, 0 );
  14136. }
  14137. script_pushstr(st, output);
  14138. return SCRIPT_CMD_SUCCESS;
  14139. }
  14140. //=======================================================
  14141. // sprintf(<format>, ...);
  14142. // Implements C sprintf, except format %n. The resulting string is
  14143. // returned, instead of being saved in variable by reference.
  14144. //-------------------------------------------------------
  14145. BUILDIN_FUNC(sprintf)
  14146. {
  14147. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14148. const char* format;
  14149. char* p;
  14150. char* q;
  14151. char* buf = NULL;
  14152. char* buf2 = NULL;
  14153. struct script_data* data;
  14154. StringBuf final_buf;
  14155. // Fetch init data
  14156. format = script_getstr(st, 2);
  14157. argc = script_lastdata(st)-2;
  14158. len = strlen(format);
  14159. // Skip parsing, where no parsing is required.
  14160. if(len == 0) {
  14161. script_pushconststr(st,"");
  14162. return SCRIPT_CMD_SUCCESS;
  14163. }
  14164. // Pessimistic alloc
  14165. CREATE(buf, char, len+1);
  14166. // Need not be parsed, just solve stuff like %%.
  14167. if(argc == 0) {
  14168. memcpy(buf,format,len+1);
  14169. script_pushstrcopy(st, buf);
  14170. aFree(buf);
  14171. return SCRIPT_CMD_SUCCESS;
  14172. }
  14173. safestrncpy(buf, format, len+1);
  14174. // Issue sprintf for each parameter
  14175. StringBuf_Init(&final_buf);
  14176. q = buf;
  14177. while((p = strchr(q, '%')) != NULL) {
  14178. if(p != q) {
  14179. len = p - q + 1;
  14180. if(buf2_len < len) {
  14181. RECREATE(buf2, char, len);
  14182. buf2_len = len;
  14183. }
  14184. safestrncpy(buf2, q, len);
  14185. StringBuf_AppendStr(&final_buf, buf2);
  14186. q = p;
  14187. }
  14188. p = q + 1;
  14189. if(*p == '%') { // %%
  14190. StringBuf_AppendStr(&final_buf, "%");
  14191. q+=2;
  14192. continue;
  14193. }
  14194. if(*p == 'n') { // %n
  14195. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14196. script_reportsrc(st);
  14197. q+=2;
  14198. continue;
  14199. }
  14200. if(arg >= argc) {
  14201. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14202. if(buf) aFree(buf);
  14203. if(buf2) aFree(buf2);
  14204. StringBuf_Destroy(&final_buf);
  14205. script_pushconststr(st,"");
  14206. return SCRIPT_CMD_FAILURE;
  14207. }
  14208. if((p = strchr(q+1, '%')) == NULL)
  14209. p = strchr(q, 0); // EOS
  14210. len = p - q + 1;
  14211. if(buf2_len < len) {
  14212. RECREATE(buf2, char, len);
  14213. buf2_len = len;
  14214. }
  14215. safestrncpy(buf2, q, len);
  14216. q = p;
  14217. // Note: This assumes the passed value being the correct
  14218. // type to the current format specifier. If not, the server
  14219. // probably crashes or returns anything else, than expected,
  14220. // but it would behave in normal code the same way so it's
  14221. // the scripter's responsibility.
  14222. data = script_getdata(st, arg+3);
  14223. if(data_isstring(data)) // String
  14224. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14225. else if(data_isint(data)) // Number
  14226. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14227. else if(data_isreference(data)) { // Variable
  14228. char* name = reference_getname(data);
  14229. if(name[strlen(name)-1]=='$') // var Str
  14230. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14231. else // var Int
  14232. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14233. } else { // Unsupported type
  14234. ShowError("buildin_sprintf: Unknown argument type!\n");
  14235. if(buf) aFree(buf);
  14236. if(buf2) aFree(buf2);
  14237. StringBuf_Destroy(&final_buf);
  14238. script_pushconststr(st,"");
  14239. return SCRIPT_CMD_FAILURE;
  14240. }
  14241. arg++;
  14242. }
  14243. // Append anything left
  14244. if(*q)
  14245. StringBuf_AppendStr(&final_buf, q);
  14246. // Passed more, than needed
  14247. if(arg < argc) {
  14248. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14249. script_reportsrc(st);
  14250. }
  14251. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14252. if(buf) aFree(buf);
  14253. if(buf2) aFree(buf2);
  14254. StringBuf_Destroy(&final_buf);
  14255. return SCRIPT_CMD_SUCCESS;
  14256. }
  14257. //=======================================================
  14258. // sscanf(<str>, <format>, ...);
  14259. // Implements C sscanf.
  14260. //-------------------------------------------------------
  14261. BUILDIN_FUNC(sscanf){
  14262. unsigned int argc, arg = 0, len;
  14263. struct script_data* data;
  14264. struct map_session_data* sd = NULL;
  14265. const char* str;
  14266. const char* format;
  14267. const char* p;
  14268. const char* q;
  14269. char* buf = NULL;
  14270. char* buf_p;
  14271. char* ref_str = NULL;
  14272. int ref_int;
  14273. // Get data
  14274. str = script_getstr(st, 2);
  14275. format = script_getstr(st, 3);
  14276. argc = script_lastdata(st)-3;
  14277. len = strlen(format);
  14278. if (len != 0 && strlen(str) == 0) {
  14279. // If the source string is empty but the format string is not, we return -1
  14280. // according to the C specs. (if the format string is also empty, we shall
  14281. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14282. script_pushint(st, -1);
  14283. return SCRIPT_CMD_SUCCESS;
  14284. }
  14285. CREATE(buf, char, len*2+1);
  14286. // Issue sscanf for each parameter
  14287. *buf = 0;
  14288. q = format;
  14289. while((p = strchr(q, '%'))){
  14290. if(p!=q){
  14291. strncat(buf, q, (size_t)(p-q));
  14292. q = p;
  14293. }
  14294. p = q+1;
  14295. if(*p=='*' || *p=='%'){ // Skip
  14296. strncat(buf, q, 2);
  14297. q+=2;
  14298. continue;
  14299. }
  14300. if(arg>=argc){
  14301. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14302. script_pushint(st, -1);
  14303. if(buf) aFree(buf);
  14304. if(ref_str) aFree(ref_str);
  14305. return SCRIPT_CMD_FAILURE;
  14306. }
  14307. if((p = strchr(q+1, '%'))==NULL){
  14308. p = strchr(q, 0); // EOS
  14309. }
  14310. len = p-q;
  14311. strncat(buf, q, len);
  14312. q = p;
  14313. // Validate output
  14314. data = script_getdata(st, arg+4);
  14315. if(!data_isreference(data) || !reference_tovariable(data)){
  14316. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14317. script_pushint(st, -1);
  14318. if(buf) aFree(buf);
  14319. if(ref_str) aFree(ref_str);
  14320. return SCRIPT_CMD_FAILURE;
  14321. }
  14322. buf_p = reference_getname(data);
  14323. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14324. script_pushint(st, -1);
  14325. if(buf) aFree(buf);
  14326. if(ref_str) aFree(ref_str);
  14327. return SCRIPT_CMD_SUCCESS;
  14328. }
  14329. // Save value if any
  14330. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14331. if(ref_str==NULL){
  14332. CREATE(ref_str, char, strlen(str)+1);
  14333. }
  14334. if(sscanf(str, buf, ref_str)==0){
  14335. break;
  14336. }
  14337. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14338. } else { // Number
  14339. if(sscanf(str, buf, &ref_int)==0){
  14340. break;
  14341. }
  14342. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14343. }
  14344. arg++;
  14345. // Disable used format (%... -> %*...)
  14346. buf_p = strchr(buf, 0);
  14347. memmove(buf_p-len+2, buf_p-len+1, len);
  14348. *(buf_p-len+1) = '*';
  14349. }
  14350. script_pushint(st, arg);
  14351. if(buf) aFree(buf);
  14352. if(ref_str) aFree(ref_str);
  14353. return SCRIPT_CMD_SUCCESS;
  14354. }
  14355. //=======================================================
  14356. // strpos(<haystack>, <needle>)
  14357. // strpos(<haystack>, <needle>, <offset>)
  14358. //
  14359. // Implements PHP style strpos. Adapted from code from
  14360. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14361. //-------------------------------------------------------
  14362. BUILDIN_FUNC(strpos) {
  14363. const char *haystack = script_getstr(st,2);
  14364. const char *needle = script_getstr(st,3);
  14365. int i;
  14366. size_t len;
  14367. if( script_hasdata(st,4) )
  14368. i = script_getnum(st,4);
  14369. else
  14370. i = 0;
  14371. if (needle[0] == '\0') {
  14372. script_pushint(st, -1);
  14373. return SCRIPT_CMD_SUCCESS;
  14374. }
  14375. len = strlen(haystack);
  14376. for ( ; i < len; ++i ) {
  14377. if ( haystack[i] == *needle ) {
  14378. // matched starting char -- loop through remaining chars
  14379. const char *h, *n;
  14380. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14381. if ( *h != *n ) {
  14382. break;
  14383. }
  14384. }
  14385. if ( !*n ) { // matched all of 'needle' to null termination
  14386. script_pushint(st, i);
  14387. return SCRIPT_CMD_SUCCESS;
  14388. }
  14389. }
  14390. }
  14391. script_pushint(st, -1);
  14392. return SCRIPT_CMD_SUCCESS;
  14393. }
  14394. //===============================================================
  14395. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14396. //
  14397. // Note: Finds all instances of <search> in <input> and replaces
  14398. // with <replace>. If specified will only replace as many
  14399. // instances as specified in <count>. By default will be case
  14400. // sensitive.
  14401. //---------------------------------------------------------------
  14402. BUILDIN_FUNC(replacestr)
  14403. {
  14404. const char *input = script_getstr(st, 2);
  14405. const char *find = script_getstr(st, 3);
  14406. const char *replace = script_getstr(st, 4);
  14407. size_t inputlen = strlen(input);
  14408. size_t findlen = strlen(find);
  14409. struct StringBuf output;
  14410. bool usecase = true;
  14411. int count = 0;
  14412. int numFinds = 0;
  14413. int i = 0, f = 0;
  14414. if(findlen == 0) {
  14415. ShowError("script:replacestr: Invalid search length.\n");
  14416. st->state = END;
  14417. return SCRIPT_CMD_FAILURE;
  14418. }
  14419. if(script_hasdata(st, 5)) {
  14420. if( script_isint(st, 5) )
  14421. usecase = script_getnum(st, 5) != 0;
  14422. else {
  14423. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14424. st->state = END;
  14425. return SCRIPT_CMD_FAILURE;
  14426. }
  14427. }
  14428. if(script_hasdata(st, 6)) {
  14429. count = script_getnum(st, 6);
  14430. if(count == 0) {
  14431. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14432. st->state = END;
  14433. return SCRIPT_CMD_FAILURE;
  14434. }
  14435. }
  14436. StringBuf_Init(&output);
  14437. for(; i < inputlen; i++) {
  14438. if(count && count == numFinds) { //found enough, stop looking
  14439. break;
  14440. }
  14441. for(f = 0; f <= findlen; f++) {
  14442. if(f == findlen) { //complete match
  14443. numFinds++;
  14444. StringBuf_AppendStr(&output, replace);
  14445. i += findlen - 1;
  14446. break;
  14447. } else {
  14448. if(usecase) {
  14449. if((i + f) > inputlen || input[i + f] != find[f]) {
  14450. StringBuf_Printf(&output, "%c", input[i]);
  14451. break;
  14452. }
  14453. } else {
  14454. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14455. StringBuf_Printf(&output, "%c", input[i]);
  14456. break;
  14457. }
  14458. }
  14459. }
  14460. }
  14461. }
  14462. //append excess after enough found
  14463. if(i < inputlen)
  14464. StringBuf_AppendStr(&output, &(input[i]));
  14465. script_pushstrcopy(st, StringBuf_Value(&output));
  14466. StringBuf_Destroy(&output);
  14467. return SCRIPT_CMD_SUCCESS;
  14468. }
  14469. //========================================================
  14470. // countstr <input>, <search>{, <usecase>}
  14471. //
  14472. // Note: Counts the number of times <search> occurs in
  14473. // <input>. By default will be case sensitive.
  14474. //--------------------------------------------------------
  14475. BUILDIN_FUNC(countstr)
  14476. {
  14477. const char *input = script_getstr(st, 2);
  14478. const char *find = script_getstr(st, 3);
  14479. size_t inputlen = strlen(input);
  14480. size_t findlen = strlen(find);
  14481. bool usecase = true;
  14482. int numFinds = 0;
  14483. int i = 0, f = 0;
  14484. if(findlen == 0) {
  14485. ShowError("script:countstr: Invalid search length.\n");
  14486. st->state = END;
  14487. return SCRIPT_CMD_FAILURE;
  14488. }
  14489. if(script_hasdata(st, 4)) {
  14490. if( script_isint(st, 4) )
  14491. usecase = script_getnum(st, 4) != 0;
  14492. else {
  14493. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14494. st->state = END;
  14495. return SCRIPT_CMD_FAILURE;
  14496. }
  14497. }
  14498. for(; i < inputlen; i++) {
  14499. for(f = 0; f <= findlen; f++) {
  14500. if(f == findlen) { //complete match
  14501. numFinds++;
  14502. i += findlen - 1;
  14503. break;
  14504. } else {
  14505. if(usecase) {
  14506. if((i + f) > inputlen || input[i + f] != find[f]) {
  14507. break;
  14508. }
  14509. } else {
  14510. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14511. break;
  14512. }
  14513. }
  14514. }
  14515. }
  14516. }
  14517. script_pushint(st, numFinds);
  14518. return SCRIPT_CMD_SUCCESS;
  14519. }
  14520. /// Changes the display name and/or display class of the npc.
  14521. /// Returns 0 is successful, 1 if the npc does not exist.
  14522. ///
  14523. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14524. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14525. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14526. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14527. BUILDIN_FUNC(setnpcdisplay)
  14528. {
  14529. const char* name;
  14530. const char* newname = NULL;
  14531. int class_ = JT_FAKENPC, size = -1;
  14532. struct npc_data* nd;
  14533. name = script_getstr(st,2);
  14534. if( script_hasdata(st,4) )
  14535. class_ = script_getnum(st,4);
  14536. if( script_hasdata(st,5) )
  14537. size = script_getnum(st,5);
  14538. if( script_isstring(st, 3) )
  14539. newname = script_getstr(st, 3);
  14540. else
  14541. class_ = script_getnum(st, 3);
  14542. nd = npc_name2id(name);
  14543. if( nd == NULL )
  14544. {// not found
  14545. script_pushint(st,1);
  14546. return SCRIPT_CMD_SUCCESS;
  14547. }
  14548. // update npc
  14549. if( newname )
  14550. npc_setdisplayname(nd, newname);
  14551. if( size != -1 && size != (int)nd->size )
  14552. nd->size = size;
  14553. else
  14554. size = -1;
  14555. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  14556. npc_setclass(nd, class_);
  14557. else if( size != -1 )
  14558. unit_refresh(&nd->bl); // Required to update the visual size
  14559. script_pushint(st,0);
  14560. return SCRIPT_CMD_SUCCESS;
  14561. }
  14562. BUILDIN_FUNC(atoi)
  14563. {
  14564. const char *value;
  14565. value = script_getstr(st,2);
  14566. script_pushint(st,atoi(value));
  14567. return SCRIPT_CMD_SUCCESS;
  14568. }
  14569. BUILDIN_FUNC(axtoi)
  14570. {
  14571. const char *hex = script_getstr(st,2);
  14572. long value = strtol(hex, NULL, 16);
  14573. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14574. value = cap_value(value, INT_MIN, INT_MAX);
  14575. #endif
  14576. script_pushint(st, (int)value);
  14577. return SCRIPT_CMD_SUCCESS;
  14578. }
  14579. BUILDIN_FUNC(strtol)
  14580. {
  14581. const char *string = script_getstr(st, 2);
  14582. int base = script_getnum(st, 3);
  14583. long value = strtol(string, NULL, base);
  14584. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14585. value = cap_value(value, INT_MIN, INT_MAX);
  14586. #endif
  14587. script_pushint(st, (int)value);
  14588. return SCRIPT_CMD_SUCCESS;
  14589. }
  14590. // case-insensitive substring search [lordalfa]
  14591. BUILDIN_FUNC(compare)
  14592. {
  14593. const char *message;
  14594. const char *cmpstring;
  14595. message = script_getstr(st,2);
  14596. cmpstring = script_getstr(st,3);
  14597. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14598. return SCRIPT_CMD_SUCCESS;
  14599. }
  14600. BUILDIN_FUNC(strcmp)
  14601. {
  14602. const char *str1;
  14603. const char *str2;
  14604. str1 = script_getstr(st,2);
  14605. str2 = script_getstr(st,3);
  14606. script_pushint(st,strcmp(str1, str2));
  14607. return SCRIPT_CMD_SUCCESS;
  14608. }
  14609. // [zBuffer] List of mathematics commands --->
  14610. BUILDIN_FUNC(sqrt)
  14611. {
  14612. double i, a;
  14613. i = script_getnum(st,2);
  14614. a = sqrt(i);
  14615. script_pushint(st,(int)a);
  14616. return SCRIPT_CMD_SUCCESS;
  14617. }
  14618. BUILDIN_FUNC(pow)
  14619. {
  14620. double i, a, b;
  14621. a = script_getnum(st,2);
  14622. b = script_getnum(st,3);
  14623. i = pow(a,b);
  14624. script_pushint(st,(int)i);
  14625. return SCRIPT_CMD_SUCCESS;
  14626. }
  14627. BUILDIN_FUNC(distance)
  14628. {
  14629. int x0, y0, x1, y1;
  14630. x0 = script_getnum(st,2);
  14631. y0 = script_getnum(st,3);
  14632. x1 = script_getnum(st,4);
  14633. y1 = script_getnum(st,5);
  14634. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14635. return SCRIPT_CMD_SUCCESS;
  14636. }
  14637. // <--- [zBuffer] List of mathematics commands
  14638. BUILDIN_FUNC(md5)
  14639. {
  14640. const char *tmpstr;
  14641. char *md5str;
  14642. tmpstr = script_getstr(st,2);
  14643. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14644. MD5_String(tmpstr, md5str);
  14645. script_pushstr(st, md5str);
  14646. return SCRIPT_CMD_SUCCESS;
  14647. }
  14648. // [zBuffer] List of dynamic var commands --->
  14649. /**
  14650. * setd "<variable name>",<value>{,<char_id>};
  14651. **/
  14652. BUILDIN_FUNC(setd)
  14653. {
  14654. TBL_PC *sd = NULL;
  14655. char varname[100];
  14656. const char *buffer;
  14657. int elem;
  14658. buffer = script_getstr(st, 2);
  14659. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14660. elem = 0;
  14661. if( not_server_variable(*varname) ) {
  14662. if (!script_charid2sd(4,sd))
  14663. return SCRIPT_CMD_FAILURE;
  14664. }
  14665. if( is_string_variable(varname) ) {
  14666. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), NULL );
  14667. } else {
  14668. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), NULL );
  14669. }
  14670. return SCRIPT_CMD_SUCCESS;
  14671. }
  14672. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14673. {
  14674. int i, j;
  14675. TBL_PC* sd = NULL;
  14676. const char* query;
  14677. struct script_data* data;
  14678. const char* name;
  14679. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14680. int num_vars;
  14681. int num_cols;
  14682. // check target variables
  14683. for( i = 3; script_hasdata(st,i); ++i ) {
  14684. data = script_getdata(st, i);
  14685. if( data_isreference(data) ) { // it's a variable
  14686. name = reference_getname(data);
  14687. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14688. if( !script_rid2sd(sd) ) { // no player attached
  14689. script_reportdata(data);
  14690. st->state = END;
  14691. return SCRIPT_CMD_FAILURE;
  14692. }
  14693. }
  14694. } else {
  14695. ShowError("script:query_sql: not a variable\n");
  14696. script_reportdata(data);
  14697. st->state = END;
  14698. return SCRIPT_CMD_FAILURE;
  14699. }
  14700. }
  14701. num_vars = i - 3;
  14702. // Execute the query
  14703. query = script_getstr(st,2);
  14704. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14705. Sql_ShowDebug(handle);
  14706. script_pushint(st, -1);
  14707. return SCRIPT_CMD_FAILURE;
  14708. }
  14709. if( Sql_NumRows(handle) == 0 ) { // No data received
  14710. Sql_FreeResult(handle);
  14711. script_pushint(st, 0);
  14712. return SCRIPT_CMD_SUCCESS;
  14713. }
  14714. // Count the number of columns to store
  14715. num_cols = Sql_NumColumns(handle);
  14716. if( num_vars < num_cols ) {
  14717. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14718. script_reportsrc(st);
  14719. } else if( num_vars > num_cols ) {
  14720. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14721. script_reportsrc(st);
  14722. }
  14723. // Store data
  14724. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14725. for( j = 0; j < num_vars; ++j ) {
  14726. char* str = NULL;
  14727. if( j < num_cols )
  14728. Sql_GetData(handle, j, &str, NULL);
  14729. data = script_getdata(st, j+3);
  14730. name = reference_getname(data);
  14731. if( is_string_variable(name) )
  14732. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  14733. else
  14734. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  14735. }
  14736. }
  14737. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14738. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14739. script_reportsrc(st);
  14740. }
  14741. // Free data
  14742. Sql_FreeResult(handle);
  14743. script_pushint(st, i);
  14744. return SCRIPT_CMD_SUCCESS;
  14745. }
  14746. BUILDIN_FUNC(query_sql) {
  14747. return buildin_query_sql_sub(st, qsmysql_handle);
  14748. }
  14749. BUILDIN_FUNC(query_logsql) {
  14750. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14751. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14752. script_pushint(st,-1);
  14753. return SCRIPT_CMD_FAILURE;
  14754. }
  14755. return buildin_query_sql_sub(st, logmysql_handle);
  14756. }
  14757. //Allows escaping of a given string.
  14758. BUILDIN_FUNC(escape_sql)
  14759. {
  14760. const char *str;
  14761. char *esc_str;
  14762. size_t len;
  14763. str = script_getstr(st,2);
  14764. len = strlen(str);
  14765. esc_str = (char*)aMalloc(len*2+1);
  14766. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14767. script_pushstr(st, esc_str);
  14768. return SCRIPT_CMD_SUCCESS;
  14769. }
  14770. BUILDIN_FUNC(getd)
  14771. {
  14772. char varname[100];
  14773. const char *buffer;
  14774. int elem;
  14775. buffer = script_getstr(st, 2);
  14776. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14777. elem = 0;
  14778. // Push the 'pointer' so it's more flexible [Lance]
  14779. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14780. return SCRIPT_CMD_SUCCESS;
  14781. }
  14782. BUILDIN_FUNC(callshop)
  14783. {
  14784. TBL_PC *sd = NULL;
  14785. struct npc_data *nd;
  14786. const char *shopname;
  14787. int flag = 0;
  14788. if (!script_rid2sd(sd)) {
  14789. script_pushint(st,0);
  14790. return SCRIPT_CMD_SUCCESS;
  14791. }
  14792. shopname = script_getstr(st, 2);
  14793. if (script_hasdata(st,3))
  14794. flag = script_getnum(st,3);
  14795. nd = npc_name2id(shopname);
  14796. 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) ) {
  14797. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14798. script_pushint(st,0);
  14799. return SCRIPT_CMD_FAILURE;
  14800. }
  14801. if (nd->subtype == NPCTYPE_SHOP) {
  14802. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14803. sd->state.callshop = 1;
  14804. switch (flag) {
  14805. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14806. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14807. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14808. }
  14809. }
  14810. #if PACKETVER >= 20131223
  14811. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14812. unsigned short i;
  14813. for (i = 0; i < nd->u.shop.count; i++) {
  14814. if (nd->u.shop.shop_item[i].qty)
  14815. break;
  14816. }
  14817. if (i == nd->u.shop.count) {
  14818. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14819. return SCRIPT_CMD_FAILURE;
  14820. }
  14821. sd->npc_shopid = nd->bl.id;
  14822. clif_npc_market_open(sd, nd);
  14823. script_pushint(st,1);
  14824. return SCRIPT_CMD_SUCCESS;
  14825. }
  14826. #endif
  14827. else
  14828. clif_cashshop_show(sd, nd);
  14829. sd->npc_shopid = nd->bl.id;
  14830. script_pushint(st,1);
  14831. return SCRIPT_CMD_SUCCESS;
  14832. }
  14833. BUILDIN_FUNC(npcshopitem)
  14834. {
  14835. const char* npcname = script_getstr(st, 2);
  14836. struct npc_data* nd = npc_name2id(npcname);
  14837. int n, i;
  14838. int amount;
  14839. uint16 offs = 2;
  14840. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14841. script_pushint(st,0);
  14842. return SCRIPT_CMD_SUCCESS;
  14843. }
  14844. #if PACKETVER >= 20131223
  14845. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14846. offs = 3;
  14847. npc_market_delfromsql_(nd->exname, 0, true);
  14848. }
  14849. #endif
  14850. // get the count of new entries
  14851. amount = (script_lastdata(st)-2)/offs;
  14852. // generate new shop item list
  14853. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14854. nd->u.shop.count = 0;
  14855. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14856. t_itemid nameid = script_getnum( st, i );
  14857. if( itemdb_exists( nameid ) == nullptr ){
  14858. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  14859. script_pushint( st, 0 );
  14860. return SCRIPT_CMD_FAILURE;
  14861. }
  14862. nd->u.shop.shop_item[n].nameid = nameid;
  14863. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14864. #if PACKETVER >= 20131223
  14865. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14866. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14867. nd->u.shop.shop_item[n].flag = 1;
  14868. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14869. }
  14870. #endif
  14871. nd->u.shop.count++;
  14872. }
  14873. script_pushint(st,1);
  14874. return SCRIPT_CMD_SUCCESS;
  14875. }
  14876. BUILDIN_FUNC(npcshopadditem)
  14877. {
  14878. const char* npcname = script_getstr(st,2);
  14879. struct npc_data* nd = npc_name2id(npcname);
  14880. uint16 offs = 2, amount;
  14881. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14882. script_pushint(st,0);
  14883. return SCRIPT_CMD_SUCCESS;
  14884. }
  14885. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14886. offs = 3;
  14887. // get the count of new entries
  14888. amount = (script_lastdata(st)-2)/offs;
  14889. #if PACKETVER >= 20131223
  14890. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14891. for (int n = 0, i = 3; n < amount; n++, i += offs) {
  14892. t_itemid nameid = script_getnum(st,i);
  14893. uint16 j;
  14894. if( itemdb_exists( nameid ) == nullptr ){
  14895. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  14896. script_pushint( st, 0 );
  14897. return SCRIPT_CMD_FAILURE;
  14898. }
  14899. // Check existing entries
  14900. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14901. if (j == nd->u.shop.count) {
  14902. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14903. j = nd->u.shop.count;
  14904. nd->u.shop.shop_item[j].flag = 1;
  14905. nd->u.shop.count++;
  14906. }
  14907. nd->u.shop.shop_item[j].nameid = nameid;
  14908. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14909. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14910. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14911. }
  14912. script_pushint(st,1);
  14913. return SCRIPT_CMD_SUCCESS;
  14914. }
  14915. #endif
  14916. // append new items to existing shop item list
  14917. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14918. for (int n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  14919. {
  14920. t_itemid nameid = script_getnum( st, i );
  14921. if( itemdb_exists( nameid ) == nullptr ){
  14922. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  14923. script_pushint( st, 0 );
  14924. return SCRIPT_CMD_FAILURE;
  14925. }
  14926. nd->u.shop.shop_item[n].nameid = nameid;
  14927. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14928. nd->u.shop.count++;
  14929. }
  14930. script_pushint(st,1);
  14931. return SCRIPT_CMD_SUCCESS;
  14932. }
  14933. BUILDIN_FUNC(npcshopdelitem)
  14934. {
  14935. const char* npcname = script_getstr(st,2);
  14936. struct npc_data* nd = npc_name2id(npcname);
  14937. int n, i, size;
  14938. unsigned short amount;
  14939. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14940. script_pushint(st,0);
  14941. return SCRIPT_CMD_SUCCESS;
  14942. }
  14943. amount = script_lastdata(st)-2;
  14944. size = nd->u.shop.count;
  14945. // remove specified items from the shop item list
  14946. for( i = 3; i < 3 + amount; i++ ) {
  14947. t_itemid nameid = script_getnum(st,i);
  14948. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14949. if( n < size ) {
  14950. if (n+1 != size)
  14951. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14952. #if PACKETVER >= 20131223
  14953. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14954. npc_market_delfromsql_(nd->exname, nameid, false);
  14955. #endif
  14956. size--;
  14957. }
  14958. }
  14959. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14960. nd->u.shop.count = size;
  14961. script_pushint(st,1);
  14962. return SCRIPT_CMD_SUCCESS;
  14963. }
  14964. /**
  14965. * Sets a script to attach to a shop npc.
  14966. * npcshopattach "<npc_name>";
  14967. **/
  14968. BUILDIN_FUNC(npcshopattach)
  14969. {
  14970. const char* npcname = script_getstr(st,2);
  14971. struct npc_data* nd = npc_name2id(npcname);
  14972. int flag = 1;
  14973. if( script_hasdata(st,3) )
  14974. flag = script_getnum(st,3);
  14975. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14976. script_pushint(st,0);
  14977. return SCRIPT_CMD_SUCCESS;
  14978. }
  14979. if (flag)
  14980. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14981. else
  14982. nd->master_nd = NULL;
  14983. script_pushint(st,1);
  14984. return SCRIPT_CMD_SUCCESS;
  14985. }
  14986. /*==========================================
  14987. * Returns some values of an item [Lupus]
  14988. * Price, Weight, etc...
  14989. setitemscript(itemID,"{new item bonus script}",[n]);
  14990. Where n:
  14991. 0 - script
  14992. 1 - Equip script
  14993. 2 - Unequip script
  14994. *------------------------------------------*/
  14995. BUILDIN_FUNC(setitemscript)
  14996. {
  14997. int n = 0;
  14998. const char *script;
  14999. struct item_data *i_data;
  15000. struct script_code **dstscript;
  15001. t_itemid item_id = script_getnum(st,2);
  15002. script = script_getstr(st,3);
  15003. if( script_hasdata(st,4) )
  15004. n=script_getnum(st,4);
  15005. i_data = itemdb_exists(item_id);
  15006. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15007. script_pushint(st,0);
  15008. return SCRIPT_CMD_SUCCESS;
  15009. }
  15010. switch (n) {
  15011. case 2:
  15012. dstscript = &i_data->unequip_script;
  15013. break;
  15014. case 1:
  15015. dstscript = &i_data->equip_script;
  15016. break;
  15017. default:
  15018. dstscript = &i_data->script;
  15019. break;
  15020. }
  15021. if(*dstscript)
  15022. script_free_code(*dstscript);
  15023. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15024. script_pushint(st,1);
  15025. return SCRIPT_CMD_SUCCESS;
  15026. }
  15027. /*=======================================================
  15028. * Add or Update a mob drop temporarily [Akinari]
  15029. * Original Idea By: [Lupus]
  15030. *
  15031. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  15032. *
  15033. * If given an item the mob already drops, the rate
  15034. * is updated to the new rate. Rate cannot exceed 10000
  15035. * Returns 1 if succeeded (added/updated a mob drop)
  15036. *-------------------------------------------------------*/
  15037. BUILDIN_FUNC(addmonsterdrop)
  15038. {
  15039. std::shared_ptr<s_mob_db> mob;
  15040. int rate;
  15041. if(script_isstring(st, 2))
  15042. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15043. else
  15044. mob = mob_db.find(script_getnum(st,2));
  15045. t_itemid item_id=script_getnum(st,3);
  15046. rate=script_getnum(st,4);
  15047. if(!itemdb_exists(item_id)){
  15048. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15049. return SCRIPT_CMD_FAILURE;
  15050. }
  15051. if(mob) { //We got a valid monster, check for available drop slot
  15052. unsigned char i, c = 0;
  15053. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15054. if(mob->dropitem[i].nameid) {
  15055. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  15056. c = i;
  15057. break;
  15058. }
  15059. continue;
  15060. }
  15061. if(!c) //Accept first available slot only
  15062. c = i;
  15063. }
  15064. if(c) { //Fill in the slot with the item and rate
  15065. mob->dropitem[c].nameid = item_id;
  15066. mob->dropitem[c].rate = (rate > 10000)?10000:rate;
  15067. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15068. script_pushint(st,1);
  15069. } else //No place to put the new drop
  15070. script_pushint(st,0);
  15071. } else {
  15072. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15073. return SCRIPT_CMD_FAILURE;
  15074. }
  15075. return SCRIPT_CMD_SUCCESS;
  15076. }
  15077. /*=======================================================
  15078. * Delete a mob drop temporarily [Akinari]
  15079. * Original Idea By: [Lupus]
  15080. *
  15081. * delmonsterdrop <mob_id or name>,<item_id>;
  15082. *
  15083. * Returns 1 if succeeded (deleted a mob drop)
  15084. *-------------------------------------------------------*/
  15085. BUILDIN_FUNC(delmonsterdrop)
  15086. {
  15087. std::shared_ptr<s_mob_db> mob;
  15088. if(script_isstring(st, 2))
  15089. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15090. else
  15091. mob = mob_db.find(script_getnum(st,2));
  15092. t_itemid item_id=script_getnum(st,3);
  15093. if(!itemdb_exists(item_id)){
  15094. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15095. return SCRIPT_CMD_FAILURE;
  15096. }
  15097. if(mob) { //We got a valid monster, check for item drop on monster
  15098. unsigned char i;
  15099. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15100. if(mob->dropitem[i].nameid == item_id) {
  15101. mob->dropitem[i].nameid = 0;
  15102. mob->dropitem[i].rate = 0;
  15103. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15104. script_pushint(st,1);
  15105. return SCRIPT_CMD_SUCCESS;
  15106. }
  15107. }
  15108. //No drop on that monster
  15109. script_pushint(st,0);
  15110. } else {
  15111. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15112. return SCRIPT_CMD_FAILURE;
  15113. }
  15114. return SCRIPT_CMD_SUCCESS;
  15115. }
  15116. /*==========================================
  15117. * Returns some values of a monster [Lupus]
  15118. * Name, Level, race, size, etc...
  15119. getmonsterinfo(monsterID,queryIndex);
  15120. *------------------------------------------*/
  15121. BUILDIN_FUNC(getmonsterinfo)
  15122. {
  15123. int mob_id;
  15124. mob_id = script_getnum(st,2);
  15125. if (!mobdb_checkid(mob_id)) {
  15126. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15127. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  15128. script_pushconststr(st,"null");
  15129. else
  15130. script_pushint(st,-1);
  15131. return SCRIPT_CMD_SUCCESS;
  15132. }
  15133. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  15134. switch ( script_getnum(st,3) ) {
  15135. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15136. case MOB_LV: script_pushint(st,mob->lv); break;
  15137. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15138. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15139. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15140. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15141. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15142. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15143. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15144. case MOB_STR: script_pushint(st,mob->status.str); break;
  15145. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15146. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15147. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15148. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15149. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15150. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15151. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15152. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15153. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15154. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15155. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15156. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15157. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15158. default: script_pushint(st,-1); //wrong Index
  15159. }
  15160. return SCRIPT_CMD_SUCCESS;
  15161. }
  15162. /**
  15163. * Check player's vending/buyingstore/autotrade state
  15164. * checkvending({"<Player Name>"})
  15165. * @author [Nab4]
  15166. */
  15167. BUILDIN_FUNC(checkvending) {
  15168. TBL_PC *sd = NULL;
  15169. if (!script_nick2sd(2,sd) ) {
  15170. script_pushint(st,0);
  15171. return SCRIPT_CMD_SUCCESS;
  15172. }
  15173. else {
  15174. int8 ret = 0;
  15175. if (sd->state.vending)
  15176. ret = 1;
  15177. else if (sd->state.buyingstore)
  15178. ret = 4;
  15179. if (sd->state.autotrade)
  15180. ret |= 2;
  15181. script_pushint(st, ret);
  15182. }
  15183. return SCRIPT_CMD_SUCCESS;
  15184. }
  15185. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15186. {
  15187. TBL_PC *sd = NULL;
  15188. if( script_nick2sd(2,sd) )
  15189. script_pushint(st,(sd->chatID != 0));
  15190. else
  15191. script_pushint(st,0);
  15192. return SCRIPT_CMD_SUCCESS;
  15193. }
  15194. BUILDIN_FUNC(checkidle)
  15195. {
  15196. TBL_PC *sd = NULL;
  15197. if( script_nick2sd(2,sd) )
  15198. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15199. else
  15200. script_pushint(st, 0);
  15201. return SCRIPT_CMD_SUCCESS;
  15202. }
  15203. BUILDIN_FUNC(checkidlehom)
  15204. {
  15205. TBL_PC *sd = NULL;
  15206. if( script_nick2sd(2,sd) )
  15207. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15208. else
  15209. script_pushint(st, 0);
  15210. return SCRIPT_CMD_SUCCESS;
  15211. }
  15212. BUILDIN_FUNC(checkidlemer)
  15213. {
  15214. TBL_PC *sd;
  15215. if( script_nick2sd(2,sd) )
  15216. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_mer));
  15217. else
  15218. script_pushint(st, 0);
  15219. return SCRIPT_CMD_SUCCESS;
  15220. }
  15221. BUILDIN_FUNC(searchitem)
  15222. {
  15223. struct script_data* data = script_getdata(st, 2);
  15224. const char *itemname = script_getstr(st,3);
  15225. struct item_data *items[MAX_SEARCH];
  15226. int count;
  15227. char* name;
  15228. int32 start;
  15229. int32 id;
  15230. int32 i;
  15231. TBL_PC* sd = NULL;
  15232. if ((items[0] = itemdb_exists(atoi(itemname))))
  15233. count = 1;
  15234. else
  15235. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  15236. if (!count) {
  15237. script_pushint(st, 0);
  15238. return SCRIPT_CMD_SUCCESS;
  15239. }
  15240. if( !data_isreference(data) )
  15241. {
  15242. ShowError("script:searchitem: not a variable\n");
  15243. script_reportdata(data);
  15244. st->state = END;
  15245. return SCRIPT_CMD_FAILURE;// not a variable
  15246. }
  15247. id = reference_getid(data);
  15248. start = reference_getindex(data);
  15249. name = reference_getname(data);
  15250. if( not_server_variable(*name) && !script_rid2sd(sd) )
  15251. {
  15252. return SCRIPT_CMD_SUCCESS;// no player attached
  15253. }
  15254. if( is_string_variable(name) )
  15255. {// string array
  15256. ShowError("script:searchitem: not an integer array reference\n");
  15257. script_reportdata(data);
  15258. st->state = END;
  15259. return SCRIPT_CMD_FAILURE;// not supported
  15260. }
  15261. for( i = 0; i < count; ++start, ++i )
  15262. {// Set array
  15263. set_reg_num( st, sd, reference_uid( id, start ), name, items[i]->nameid, reference_getref( data ) );
  15264. }
  15265. script_pushint(st, count);
  15266. return SCRIPT_CMD_SUCCESS;
  15267. }
  15268. // [zBuffer] List of player cont commands --->
  15269. BUILDIN_FUNC(rid2name)
  15270. {
  15271. struct block_list *bl = NULL;
  15272. int rid = script_getnum(st,2);
  15273. if((bl = map_id2bl(rid)))
  15274. {
  15275. switch(bl->type) {
  15276. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15277. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15278. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15279. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15280. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15281. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  15282. default:
  15283. ShowError("buildin_rid2name: BL type unknown.\n");
  15284. script_pushconststr(st,"");
  15285. break;
  15286. }
  15287. } else {
  15288. ShowError("buildin_rid2name: invalid RID\n");
  15289. script_pushconststr(st,"(null)");
  15290. }
  15291. return SCRIPT_CMD_SUCCESS;
  15292. }
  15293. /**
  15294. * Toggle a unit from moving.
  15295. * pcblockmove(<unit_id>,<option>);
  15296. */
  15297. BUILDIN_FUNC(pcblockmove)
  15298. {
  15299. struct block_list *bl = NULL;
  15300. if (script_getnum(st, 2))
  15301. bl = map_id2bl(script_getnum(st,2));
  15302. else
  15303. bl = map_id2bl(st->rid);
  15304. if (bl) {
  15305. struct unit_data *ud = unit_bl2ud(bl);
  15306. if (ud)
  15307. ud->state.blockedmove = script_getnum(st,3) > 0;
  15308. }
  15309. return SCRIPT_CMD_SUCCESS;
  15310. }
  15311. /**
  15312. * Toggle a unit from casting skills.
  15313. * pcblockskill(<unit_id>,<option>);
  15314. */
  15315. BUILDIN_FUNC(pcblockskill)
  15316. {
  15317. struct block_list *bl = NULL;
  15318. if (script_getnum(st, 2))
  15319. bl = map_id2bl(script_getnum(st,2));
  15320. else
  15321. bl = map_id2bl(st->rid);
  15322. if (bl) {
  15323. struct unit_data *ud = unit_bl2ud(bl);
  15324. if (ud)
  15325. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15326. }
  15327. return SCRIPT_CMD_SUCCESS;
  15328. }
  15329. BUILDIN_FUNC(setpcblock)
  15330. {
  15331. TBL_PC *sd;
  15332. if (script_mapid2sd(4, sd)) {
  15333. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  15334. if (script_getnum(st, 3) > 0)
  15335. sd->state.block_action |= type;
  15336. else
  15337. sd->state.block_action &= ~type;
  15338. }
  15339. return SCRIPT_CMD_SUCCESS;
  15340. }
  15341. BUILDIN_FUNC(getpcblock)
  15342. {
  15343. TBL_PC *sd;
  15344. if (script_mapid2sd(2, sd))
  15345. script_pushint(st, sd->state.block_action);
  15346. else
  15347. script_pushint(st, 0);
  15348. return SCRIPT_CMD_SUCCESS;
  15349. }
  15350. BUILDIN_FUNC(pcfollow)
  15351. {
  15352. TBL_PC *sd;
  15353. if( script_mapid2sd(2,sd) )
  15354. pc_follow(sd, script_getnum(st,3));
  15355. return SCRIPT_CMD_SUCCESS;
  15356. }
  15357. BUILDIN_FUNC(pcstopfollow)
  15358. {
  15359. TBL_PC *sd;
  15360. if(script_mapid2sd(2,sd))
  15361. pc_stop_following(sd);
  15362. return SCRIPT_CMD_SUCCESS;
  15363. }
  15364. // <--- [zBuffer] List of player cont commands
  15365. // [zBuffer] List of unit control commands --->
  15366. /// Checks to see if the unit exists.
  15367. ///
  15368. /// unitexists <unit id>;
  15369. BUILDIN_FUNC(unitexists)
  15370. {
  15371. struct block_list* bl;
  15372. bl = map_id2bl(script_getnum(st, 2));
  15373. if (!bl)
  15374. script_pushint(st, false);
  15375. else
  15376. script_pushint(st, true);
  15377. return SCRIPT_CMD_SUCCESS;
  15378. }
  15379. /// Gets the type of the given Game ID.
  15380. ///
  15381. /// getunittype <unit id>;
  15382. BUILDIN_FUNC(getunittype)
  15383. {
  15384. struct block_list* bl;
  15385. int value = 0;
  15386. if(!script_rid2bl(2,bl))
  15387. {
  15388. script_pushint(st, -1);
  15389. return SCRIPT_CMD_FAILURE;
  15390. }
  15391. switch (bl->type) {
  15392. case BL_PC:
  15393. case BL_NPC:
  15394. case BL_PET:
  15395. case BL_MOB:
  15396. case BL_HOM:
  15397. case BL_MER:
  15398. case BL_ELEM:
  15399. value = bl->type;
  15400. break;
  15401. default:
  15402. value = -1;
  15403. break;
  15404. }
  15405. script_pushint(st, value);
  15406. return SCRIPT_CMD_SUCCESS;
  15407. }
  15408. /// Gets specific live information of a bl.
  15409. ///
  15410. /// getunitdata <unit id>,<arrayname>;
  15411. BUILDIN_FUNC(getunitdata)
  15412. {
  15413. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15414. struct block_list* bl;
  15415. TBL_MOB* md = NULL;
  15416. TBL_HOM* hd = NULL;
  15417. TBL_MER* mc = NULL;
  15418. TBL_PET* pd = NULL;
  15419. TBL_ELEM* ed = NULL;
  15420. TBL_NPC* nd = NULL;
  15421. char* name;
  15422. struct script_data *data = script_getdata(st, 3);
  15423. if (!data_isreference(data)) {
  15424. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15425. return SCRIPT_CMD_FAILURE;
  15426. }
  15427. if(!script_rid2bl(2,bl))
  15428. {
  15429. script_pushint(st, -1);
  15430. return SCRIPT_CMD_FAILURE;
  15431. }
  15432. switch (bl->type) {
  15433. case BL_MOB: md = map_id2md(bl->id); break;
  15434. case BL_HOM: hd = map_id2hd(bl->id); break;
  15435. case BL_PET: pd = map_id2pd(bl->id); break;
  15436. case BL_MER: mc = map_id2mc(bl->id); break;
  15437. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15438. case BL_NPC: nd = map_id2nd(bl->id); break;
  15439. default:
  15440. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  15441. return SCRIPT_CMD_FAILURE;
  15442. }
  15443. name = reference_getname(data);
  15444. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  15445. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  15446. return SCRIPT_CMD_FAILURE;
  15447. }
  15448. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  15449. switch(bl->type) {
  15450. case BL_MOB:
  15451. if (!md) {
  15452. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  15453. return SCRIPT_CMD_FAILURE;
  15454. }
  15455. getunitdata_sub(UMOB_SIZE, md->status.size);
  15456. getunitdata_sub(UMOB_LEVEL, md->level);
  15457. getunitdata_sub(UMOB_HP, md->status.hp);
  15458. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  15459. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  15460. getunitdata_sub(UMOB_MAPID, md->bl.m);
  15461. getunitdata_sub(UMOB_X, md->bl.x);
  15462. getunitdata_sub(UMOB_Y, md->bl.y);
  15463. getunitdata_sub(UMOB_SPEED, md->status.speed);
  15464. getunitdata_sub(UMOB_MODE, md->status.mode);
  15465. getunitdata_sub(UMOB_AI, md->special_state.ai);
  15466. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  15467. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15468. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15469. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15470. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15471. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15472. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15473. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15474. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15475. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15476. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15477. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15478. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15479. getunitdata_sub(UMOB_STR, md->status.str);
  15480. getunitdata_sub(UMOB_AGI, md->status.agi);
  15481. getunitdata_sub(UMOB_VIT, md->status.vit);
  15482. getunitdata_sub(UMOB_INT, md->status.int_);
  15483. getunitdata_sub(UMOB_DEX, md->status.dex);
  15484. getunitdata_sub(UMOB_LUK, md->status.luk);
  15485. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15486. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15487. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15488. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15489. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15490. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15491. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15492. getunitdata_sub(UMOB_DEF, md->status.def);
  15493. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15494. getunitdata_sub(UMOB_HIT, md->status.hit);
  15495. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15496. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15497. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15498. getunitdata_sub(UMOB_RACE, md->status.race);
  15499. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15500. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15501. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15502. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15503. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15504. getunitdata_sub(UMOB_TARGETID, md->target_id);
  15505. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  15506. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  15507. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  15508. break;
  15509. case BL_HOM:
  15510. if (!hd) {
  15511. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15512. return SCRIPT_CMD_FAILURE;
  15513. }
  15514. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15515. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15516. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15517. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15518. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15519. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15520. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15521. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15522. getunitdata_sub(UHOM_X, hd->bl.x);
  15523. getunitdata_sub(UHOM_Y, hd->bl.y);
  15524. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15525. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15526. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15527. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15528. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15529. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15530. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15531. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15532. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15533. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15534. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15535. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15536. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15537. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15538. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15539. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15540. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15541. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15542. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15543. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15544. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15545. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15546. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15547. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15548. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15549. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15550. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15551. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15552. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15553. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  15554. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  15555. break;
  15556. case BL_PET:
  15557. if (!pd) {
  15558. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15559. return SCRIPT_CMD_FAILURE;
  15560. }
  15561. getunitdata_sub(UPET_SIZE, pd->status.size);
  15562. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15563. getunitdata_sub(UPET_HP, pd->status.hp);
  15564. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15565. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15566. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15567. getunitdata_sub(UPET_X, pd->bl.x);
  15568. getunitdata_sub(UPET_Y, pd->bl.y);
  15569. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15570. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15571. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15572. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15573. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15574. getunitdata_sub(UPET_STR, pd->status.str);
  15575. getunitdata_sub(UPET_AGI, pd->status.agi);
  15576. getunitdata_sub(UPET_VIT, pd->status.vit);
  15577. getunitdata_sub(UPET_INT, pd->status.int_);
  15578. getunitdata_sub(UPET_DEX, pd->status.dex);
  15579. getunitdata_sub(UPET_LUK, pd->status.luk);
  15580. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15581. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15582. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15583. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15584. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15585. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15586. getunitdata_sub(UPET_DEF, pd->status.def);
  15587. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15588. getunitdata_sub(UPET_HIT, pd->status.hit);
  15589. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15590. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15591. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15592. getunitdata_sub(UPET_RACE, pd->status.race);
  15593. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15594. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15595. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15596. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15597. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15598. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  15599. break;
  15600. case BL_MER:
  15601. if (!mc) {
  15602. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15603. return SCRIPT_CMD_FAILURE;
  15604. }
  15605. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15606. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15607. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15608. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15609. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15610. getunitdata_sub(UMER_X, mc->bl.x);
  15611. getunitdata_sub(UMER_Y, mc->bl.y);
  15612. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15613. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15614. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15615. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15616. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15617. getunitdata_sub(UMER_STR, mc->base_status.str);
  15618. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15619. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15620. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15621. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15622. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15623. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15624. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15625. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15626. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15627. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15628. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15629. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15630. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15631. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15632. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15633. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15634. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15635. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15636. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15637. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15638. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15639. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15640. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15641. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  15642. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  15643. break;
  15644. case BL_ELEM:
  15645. if (!ed) {
  15646. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15647. return SCRIPT_CMD_FAILURE;
  15648. }
  15649. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15650. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15651. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15652. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15653. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15654. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15655. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15656. getunitdata_sub(UELE_X, ed->bl.x);
  15657. getunitdata_sub(UELE_Y, ed->bl.y);
  15658. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15659. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15660. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15661. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15662. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15663. getunitdata_sub(UELE_STR, ed->base_status.str);
  15664. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15665. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15666. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15667. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15668. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15669. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15670. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15671. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15672. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15673. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15674. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15675. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15676. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15677. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15678. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15679. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15680. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15681. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15682. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15683. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15684. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15685. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15686. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15687. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  15688. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  15689. break;
  15690. case BL_NPC:
  15691. if (!nd) {
  15692. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15693. return SCRIPT_CMD_FAILURE;
  15694. }
  15695. getunitdata_sub(UNPC_LEVEL, nd->level);
  15696. getunitdata_sub(UNPC_HP, nd->status.hp);
  15697. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15698. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15699. getunitdata_sub(UNPC_X, nd->bl.x);
  15700. getunitdata_sub(UNPC_Y, nd->bl.y);
  15701. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15702. getunitdata_sub(UNPC_STR, nd->status.str);
  15703. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15704. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15705. getunitdata_sub(UNPC_INT, nd->status.int_);
  15706. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15707. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15708. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15709. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15710. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15711. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15712. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15713. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15714. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15715. getunitdata_sub(UNPC_DEF, nd->status.def);
  15716. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15717. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15718. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15719. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15720. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15721. getunitdata_sub(UNPC_RACE, nd->status.race);
  15722. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15723. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15724. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15725. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15726. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15727. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  15728. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  15729. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  15730. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  15731. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  15732. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  15733. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  15734. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  15735. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  15736. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  15737. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  15738. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  15739. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  15740. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  15741. break;
  15742. default:
  15743. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15744. return SCRIPT_CMD_FAILURE;
  15745. }
  15746. #undef getunitdata_sub
  15747. return SCRIPT_CMD_SUCCESS;
  15748. }
  15749. /// Changes the live data of a bl.
  15750. ///
  15751. /// setunitdata <unit id>,<type>,<value>;
  15752. BUILDIN_FUNC(setunitdata)
  15753. {
  15754. struct block_list* bl;
  15755. if(!script_rid2bl(2,bl))
  15756. {
  15757. script_pushint(st, -1);
  15758. return SCRIPT_CMD_FAILURE;
  15759. }
  15760. TBL_MOB* md;
  15761. TBL_HOM* hd;
  15762. TBL_MER* mc;
  15763. TBL_PET* pd;
  15764. TBL_ELEM* ed;
  15765. TBL_NPC* nd;
  15766. switch (bl->type) {
  15767. case BL_MOB: md = map_id2md(bl->id); break;
  15768. case BL_HOM: hd = map_id2hd(bl->id); break;
  15769. case BL_PET: pd = map_id2pd(bl->id); break;
  15770. case BL_MER: mc = map_id2mc(bl->id); break;
  15771. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15772. case BL_NPC:
  15773. nd = map_id2nd(bl->id);
  15774. if (!nd->status.hp)
  15775. status_calc_npc(nd, SCO_FIRST);
  15776. else
  15777. status_calc_npc(nd, SCO_NONE);
  15778. break;
  15779. default:
  15780. ShowError("buildin_setunitdata: Invalid object!\n");
  15781. return SCRIPT_CMD_FAILURE;
  15782. }
  15783. int type = script_getnum(st, 3), value = 0;
  15784. const char *mapname = NULL;
  15785. bool calc_status = false;
  15786. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  15787. mapname = script_getstr(st, 4);
  15788. else
  15789. value = script_getnum(st, 4);
  15790. switch (bl->type) {
  15791. case BL_MOB:
  15792. if (!md) {
  15793. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15794. return SCRIPT_CMD_FAILURE;
  15795. }
  15796. if (!md->base_status) {
  15797. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  15798. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  15799. }
  15800. // Check if the view data will be modified
  15801. switch( type ){
  15802. case UMOB_SEX:
  15803. //case UMOB_CLASS: // Called by status_set_viewdata
  15804. case UMOB_HAIRSTYLE:
  15805. case UMOB_HAIRCOLOR:
  15806. case UMOB_HEADBOTTOM:
  15807. case UMOB_HEADMIDDLE:
  15808. case UMOB_HEADTOP:
  15809. case UMOB_CLOTHCOLOR:
  15810. case UMOB_SHIELD:
  15811. case UMOB_WEAPON:
  15812. case UMOB_ROBE:
  15813. case UMOB_BODY2:
  15814. mob_set_dynamic_viewdata( md );
  15815. break;
  15816. }
  15817. switch (type) {
  15818. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  15819. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  15820. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15821. 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;
  15822. case UMOB_MASTERAID: md->master_id = value; break;
  15823. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15824. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15825. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15826. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15827. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  15828. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15829. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15830. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  15831. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  15832. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15833. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15834. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15835. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15836. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15837. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15838. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15839. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15840. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15841. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15842. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15843. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15844. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15845. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15846. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15847. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15848. case UMOB_SLAVECPYMSTRMD:
  15849. if (value > 0) {
  15850. TBL_MOB *md2 = NULL;
  15851. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15852. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15853. break;
  15854. }
  15855. md->base_status->mode = md2->status.mode;
  15856. md->state.copy_master_mode = 1;
  15857. } else
  15858. md->state.copy_master_mode = 0;
  15859. calc_status = true;
  15860. break;
  15861. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  15862. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  15863. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  15864. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15865. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  15866. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  15867. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  15868. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  15869. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  15870. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  15871. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  15872. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  15873. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  15874. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  15875. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  15876. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  15877. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  15878. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  15879. case UMOB_TARGETID: {
  15880. if (value==0) {
  15881. mob_unlocktarget(md,gettick());
  15882. break;
  15883. }
  15884. struct block_list* target = map_id2bl(value);
  15885. if (!target) {
  15886. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  15887. return SCRIPT_CMD_FAILURE;
  15888. }
  15889. mob_target(md,target,0);
  15890. break;
  15891. }
  15892. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  15893. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  15894. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  15895. default:
  15896. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15897. return SCRIPT_CMD_FAILURE;
  15898. }
  15899. if (calc_status)
  15900. status_calc_bl(&md->bl, SCB_BATTLE);
  15901. break;
  15902. case BL_HOM:
  15903. if (!hd) {
  15904. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15905. return SCRIPT_CMD_FAILURE;
  15906. }
  15907. switch (type) {
  15908. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  15909. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15910. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15911. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15912. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15913. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15914. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15915. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15916. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15917. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15918. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15919. 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;
  15920. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15921. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15922. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15923. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15924. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15925. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15926. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15927. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15928. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15929. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  15930. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15931. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15932. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15933. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15934. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15935. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  15936. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  15937. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  15938. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  15939. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  15940. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  15941. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  15942. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15943. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15944. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  15945. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  15946. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  15947. case UHOM_TARGETID: {
  15948. if (value==0) {
  15949. unit_stop_attack(&hd->bl);
  15950. break;
  15951. }
  15952. struct block_list* target = map_id2bl(value);
  15953. if (!target) {
  15954. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  15955. return SCRIPT_CMD_FAILURE;
  15956. }
  15957. unit_attack(&hd->bl, target->id, 1);
  15958. break;
  15959. }
  15960. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  15961. default:
  15962. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15963. return SCRIPT_CMD_FAILURE;
  15964. }
  15965. if (calc_status)
  15966. status_calc_bl(&hd->bl, SCB_BATTLE);
  15967. break;
  15968. case BL_PET:
  15969. if (!pd) {
  15970. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15971. return SCRIPT_CMD_FAILURE;
  15972. }
  15973. switch (type) {
  15974. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15975. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15976. case UPET_HP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15977. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15978. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15979. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15980. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15981. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15982. 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;
  15983. 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;
  15984. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15985. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15986. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15987. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15988. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15989. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15990. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15991. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15992. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15993. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  15994. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15995. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15996. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15997. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15998. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15999. case UPET_DEF: pd->status.def = (defType)value; break;
  16000. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  16001. case UPET_HIT: pd->status.hit = (short)value; break;
  16002. case UPET_FLEE: pd->status.flee = (short)value; break;
  16003. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16004. case UPET_CRIT: pd->status.cri = (short)value; break;
  16005. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16006. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16007. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16008. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16009. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16010. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16011. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16012. default:
  16013. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16014. return SCRIPT_CMD_FAILURE;
  16015. }
  16016. break;
  16017. case BL_MER:
  16018. if (!mc) {
  16019. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  16020. return SCRIPT_CMD_FAILURE;
  16021. }
  16022. switch (type) {
  16023. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16024. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16025. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16026. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16027. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16028. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16029. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16030. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  16031. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  16032. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16033. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16034. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16035. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16036. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16037. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16038. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16039. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16040. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16041. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16042. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16043. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16044. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16045. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16046. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16047. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16048. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16049. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16050. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16051. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16052. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16053. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16054. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16055. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16056. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16057. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16058. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16059. case UMER_TARGETID: {
  16060. if (value==0) {
  16061. unit_stop_attack(&mc->bl);
  16062. break;
  16063. }
  16064. struct block_list* target = map_id2bl(value);
  16065. if (!target) {
  16066. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16067. return SCRIPT_CMD_FAILURE;
  16068. }
  16069. unit_attack(&mc->bl, target->id, 1);
  16070. break;
  16071. }
  16072. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16073. default:
  16074. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16075. return SCRIPT_CMD_FAILURE;
  16076. }
  16077. if (calc_status)
  16078. status_calc_bl(&mc->bl, SCB_BATTLE);
  16079. break;
  16080. case BL_ELEM:
  16081. if (!ed) {
  16082. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16083. return SCRIPT_CMD_FAILURE;
  16084. }
  16085. switch (type) {
  16086. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16087. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16088. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16089. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16090. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16091. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16092. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16093. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16094. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16095. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16096. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16097. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16098. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16099. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16100. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16101. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16102. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16103. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16104. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16105. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16106. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16107. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16108. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16109. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16110. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16111. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16112. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16113. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16114. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16115. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16116. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16117. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16118. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16119. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16120. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16121. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16122. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16123. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16124. case UELE_TARGETID: {
  16125. if (value==0) {
  16126. unit_stop_attack(&ed->bl);
  16127. break;
  16128. }
  16129. struct block_list* target = map_id2bl(value);
  16130. if (!target) {
  16131. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16132. return SCRIPT_CMD_FAILURE;
  16133. }
  16134. elemental_change_mode(ed, static_cast<e_mode>(EL_MODE_AGGRESSIVE));
  16135. unit_attack(&ed->bl, target->id, 1);
  16136. break;
  16137. }
  16138. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16139. default:
  16140. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16141. return SCRIPT_CMD_FAILURE;
  16142. }
  16143. if (calc_status)
  16144. status_calc_bl(&ed->bl, SCB_BATTLE);
  16145. break;
  16146. case BL_NPC:
  16147. if (!nd) {
  16148. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16149. return SCRIPT_CMD_FAILURE;
  16150. }
  16151. switch (type) {
  16152. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16153. case UNPC_HP: nd->status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16154. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16155. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16156. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16157. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16158. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16159. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16160. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16161. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16162. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16163. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16164. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16165. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16166. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16167. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16168. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16169. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16170. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16171. case UNPC_DEF: nd->status.def = (defType)value; break;
  16172. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16173. case UNPC_HIT: nd->status.hit = (short)value; break;
  16174. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16175. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16176. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16177. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16178. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16179. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16180. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16181. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16182. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16183. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16184. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16185. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16186. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16187. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16188. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16189. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16190. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16191. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16192. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16193. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16194. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16195. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16196. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16197. default:
  16198. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16199. return SCRIPT_CMD_FAILURE;
  16200. }
  16201. break;
  16202. default:
  16203. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16204. return SCRIPT_CMD_FAILURE;
  16205. }
  16206. // Client information updates
  16207. switch (bl->type) {
  16208. case BL_HOM:
  16209. clif_send_homdata(hd->master, SP_ACK, 0);
  16210. break;
  16211. case BL_PET:
  16212. clif_send_petstatus(pd->master);
  16213. break;
  16214. case BL_MER:
  16215. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16216. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16217. break;
  16218. case BL_ELEM:
  16219. clif_elemental_info(ed->master);
  16220. break;
  16221. }
  16222. return SCRIPT_CMD_SUCCESS;
  16223. }
  16224. /// Gets the name of a bl.
  16225. /// Supported types are [MOB|HOM|PET|NPC].
  16226. /// MER and ELEM don't support custom names.
  16227. ///
  16228. /// getunitname <unit id>;
  16229. BUILDIN_FUNC(getunitname)
  16230. {
  16231. struct block_list* bl = NULL;
  16232. if(!script_rid2bl(2,bl)){
  16233. script_pushconststr(st, "Unknown");
  16234. return SCRIPT_CMD_FAILURE;
  16235. }
  16236. script_pushstrcopy(st, status_get_name(bl));
  16237. return SCRIPT_CMD_SUCCESS;
  16238. }
  16239. /// Changes the name of a bl.
  16240. /// Supported types are [MOB|HOM|PET].
  16241. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16242. ///
  16243. /// setunitname <unit id>,<name>;
  16244. BUILDIN_FUNC(setunitname)
  16245. {
  16246. struct block_list* bl = NULL;
  16247. TBL_MOB* md = NULL;
  16248. TBL_HOM* hd = NULL;
  16249. TBL_PET* pd = NULL;
  16250. if(!script_rid2bl(2,bl))
  16251. {
  16252. script_pushconststr(st, "Unknown");
  16253. return SCRIPT_CMD_FAILURE;
  16254. }
  16255. switch (bl->type) {
  16256. case BL_MOB: md = map_id2md(bl->id); break;
  16257. case BL_HOM: hd = map_id2hd(bl->id); break;
  16258. case BL_PET: pd = map_id2pd(bl->id); break;
  16259. default:
  16260. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16261. return SCRIPT_CMD_FAILURE;
  16262. }
  16263. switch (bl->type) {
  16264. case BL_MOB:
  16265. if (!md) {
  16266. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16267. return SCRIPT_CMD_FAILURE;
  16268. }
  16269. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16270. break;
  16271. case BL_HOM:
  16272. if (!hd) {
  16273. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16274. return SCRIPT_CMD_FAILURE;
  16275. }
  16276. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16277. break;
  16278. case BL_PET:
  16279. if (!pd) {
  16280. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16281. return SCRIPT_CMD_FAILURE;
  16282. }
  16283. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16284. break;
  16285. default:
  16286. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16287. return SCRIPT_CMD_FAILURE;
  16288. }
  16289. clif_name_area(bl); // Send update to client.
  16290. return SCRIPT_CMD_SUCCESS;
  16291. }
  16292. /**
  16293. * Sets a unit's title.
  16294. * setunittitle <GID>,<title>;
  16295. */
  16296. BUILDIN_FUNC(setunittitle)
  16297. {
  16298. int gid = script_getnum(st, 2);
  16299. block_list *bl = map_id2bl(gid);
  16300. if (bl == nullptr) {
  16301. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16302. return SCRIPT_CMD_FAILURE;
  16303. }
  16304. unit_data *ud = unit_bl2ud(bl);
  16305. if (ud == nullptr) {
  16306. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16307. return SCRIPT_CMD_FAILURE;
  16308. }
  16309. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16310. clif_name_area(bl);
  16311. return SCRIPT_CMD_SUCCESS;
  16312. }
  16313. /**
  16314. * Gets a unit's title.
  16315. * getunittitle <GID>;
  16316. */
  16317. BUILDIN_FUNC(getunittitle)
  16318. {
  16319. int gid = script_getnum(st, 2);
  16320. block_list *bl = map_id2bl(gid);
  16321. if (bl == nullptr) {
  16322. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16323. return SCRIPT_CMD_FAILURE;
  16324. }
  16325. unit_data *ud = unit_bl2ud(bl);
  16326. if (ud == nullptr) {
  16327. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16328. return SCRIPT_CMD_FAILURE;
  16329. }
  16330. script_pushstrcopy(st, ud->title);
  16331. return SCRIPT_CMD_SUCCESS;
  16332. }
  16333. /// Makes the unit walk to target position or map.
  16334. /// Returns if it was successful.
  16335. ///
  16336. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16337. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16338. BUILDIN_FUNC(unitwalk)
  16339. {
  16340. struct block_list* bl;
  16341. struct unit_data *ud = NULL;
  16342. const char *cmd = script_getfuncname(st), *done_label = "";
  16343. uint8 off = 5;
  16344. if(!script_rid2bl(2,bl))
  16345. {
  16346. script_pushint(st, 0);
  16347. return SCRIPT_CMD_FAILURE;
  16348. }
  16349. ud = unit_bl2ud(bl);
  16350. if (!strcmp(cmd,"unitwalk")) {
  16351. int x = script_getnum(st,3);
  16352. int y = script_getnum(st,4);
  16353. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  16354. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  16355. } else {
  16356. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  16357. if (!tbl) {
  16358. ShowError("buildin_unitwalk: Bad target destination.\n");
  16359. script_pushint(st, 0);
  16360. return SCRIPT_CMD_FAILURE;
  16361. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  16362. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  16363. off = 4;
  16364. }
  16365. if (ud && script_hasdata(st, off)) {
  16366. done_label = script_getstr(st, off);
  16367. check_event(st, done_label);
  16368. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  16369. }
  16370. return SCRIPT_CMD_SUCCESS;
  16371. }
  16372. /// Kills the unit.
  16373. ///
  16374. /// unitkill <unit_id>;
  16375. BUILDIN_FUNC(unitkill)
  16376. {
  16377. struct block_list* bl;
  16378. if(script_rid2bl(2,bl))
  16379. status_kill(bl);
  16380. return SCRIPT_CMD_SUCCESS;
  16381. }
  16382. /// Warps the unit to the target position in the target map.
  16383. /// Returns if it was successful.
  16384. ///
  16385. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  16386. BUILDIN_FUNC(unitwarp)
  16387. {
  16388. int map_idx;
  16389. short x;
  16390. short y;
  16391. struct block_list* bl;
  16392. const char *mapname;
  16393. mapname = script_getstr(st, 3);
  16394. x = (short)script_getnum(st,4);
  16395. y = (short)script_getnum(st,5);
  16396. if(!script_rid2bl(2,bl))
  16397. {
  16398. script_pushint(st, 0);
  16399. return SCRIPT_CMD_SUCCESS;
  16400. }
  16401. if (!strcmp(mapname,"this"))
  16402. map_idx = bl?bl->m:-1;
  16403. else
  16404. map_idx = map_mapname2mapid(mapname);
  16405. if (map_idx >= 0 && bl != NULL)
  16406. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  16407. else
  16408. script_pushint(st, 0);
  16409. return SCRIPT_CMD_SUCCESS;
  16410. }
  16411. /// Makes the unit attack the target.
  16412. /// If the unit is a player and <action type> is not 0, it does a continuous
  16413. /// attack instead of a single attack.
  16414. /// Returns if the request was successful.
  16415. ///
  16416. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  16417. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  16418. BUILDIN_FUNC(unitattack)
  16419. {
  16420. struct block_list* unit_bl;
  16421. struct block_list* target_bl = NULL;
  16422. int actiontype = 0;
  16423. if (!script_rid2bl(2,unit_bl)) {
  16424. script_pushint(st, false);
  16425. return SCRIPT_CMD_FAILURE;
  16426. }
  16427. if (script_isstring(st, 3)) {
  16428. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  16429. if( sd != NULL )
  16430. target_bl = &sd->bl;
  16431. } else
  16432. target_bl = map_id2bl(script_getnum(st, 3));
  16433. if (!target_bl) {
  16434. script_pushint(st, false);
  16435. return SCRIPT_CMD_FAILURE;
  16436. }
  16437. if (script_hasdata(st,4))
  16438. actiontype = script_getnum(st,4);
  16439. switch(unit_bl->type) {
  16440. case BL_PC: {
  16441. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  16442. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  16443. script_pushint(st, sd->ud.target == target_bl->id);
  16444. return SCRIPT_CMD_SUCCESS;
  16445. }
  16446. case BL_MOB:
  16447. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  16448. break;
  16449. case BL_PET:
  16450. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  16451. break;
  16452. default:
  16453. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  16454. script_pushint(st, false);
  16455. return SCRIPT_CMD_FAILURE;
  16456. }
  16457. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  16458. return SCRIPT_CMD_SUCCESS;
  16459. }
  16460. /// Makes the unit stop attacking.
  16461. ///
  16462. /// unitstopattack <unit_id>;
  16463. BUILDIN_FUNC(unitstopattack)
  16464. {
  16465. struct block_list* bl;
  16466. if(script_rid2bl(2,bl))
  16467. {
  16468. unit_stop_attack(bl);
  16469. if (bl->type == BL_MOB)
  16470. ((TBL_MOB*)bl)->target_id = 0;
  16471. }
  16472. return SCRIPT_CMD_SUCCESS;
  16473. }
  16474. /// Makes the unit stop walking.
  16475. ///
  16476. /// unitstopwalk <unit_id>{,<flag>};
  16477. BUILDIN_FUNC(unitstopwalk)
  16478. {
  16479. struct block_list* bl;
  16480. int flag = USW_NONE;
  16481. if (script_hasdata(st, 3))
  16482. flag = script_getnum(st, 3);
  16483. if(script_rid2bl(2,bl))
  16484. unit_stop_walking(bl, flag);
  16485. return SCRIPT_CMD_SUCCESS;
  16486. }
  16487. /**
  16488. * Makes the unit say the given message.
  16489. *
  16490. * unittalk <unit_id>,"<message>"{,"<flag>"};
  16491. * @param flag: Specify target
  16492. * bc_area - Message is sent to players in the vicinity of the source (default).
  16493. * bc_self - Message is sent only to player attached.
  16494. */
  16495. BUILDIN_FUNC(unittalk)
  16496. {
  16497. const char* message;
  16498. struct block_list* bl;
  16499. message = script_getstr(st, 3);
  16500. if(script_rid2bl(2,bl))
  16501. {
  16502. send_target target = AREA;
  16503. struct StringBuf sbuf;
  16504. if (script_hasdata(st, 4)) {
  16505. if (script_getnum(st, 4) == BC_SELF) {
  16506. if (map_id2sd(bl->id) == NULL) {
  16507. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  16508. return SCRIPT_CMD_FAILURE;
  16509. }
  16510. target = SELF;
  16511. }
  16512. }
  16513. StringBuf_Init(&sbuf);
  16514. StringBuf_Printf(&sbuf, "%s", message);
  16515. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  16516. StringBuf_Destroy(&sbuf);
  16517. }
  16518. return SCRIPT_CMD_SUCCESS;
  16519. }
  16520. /// Makes the unit do an emotion.
  16521. ///
  16522. /// unitemote <unit_id>,<emotion>;
  16523. ///
  16524. /// @see ET_* in script_constants.hpp
  16525. BUILDIN_FUNC(unitemote)
  16526. {
  16527. int emotion;
  16528. struct block_list* bl;
  16529. emotion = script_getnum(st,3);
  16530. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  16531. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  16532. return SCRIPT_CMD_FAILURE;
  16533. }
  16534. if (script_rid2bl(2,bl))
  16535. clif_emotion(bl, emotion);
  16536. return SCRIPT_CMD_SUCCESS;
  16537. }
  16538. /// Makes the unit cast the skill on the target or self if no target is specified.
  16539. ///
  16540. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  16541. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  16542. BUILDIN_FUNC(unitskilluseid)
  16543. {
  16544. int unit_id, target_id, casttime;
  16545. uint16 skill_id, skill_lv;
  16546. struct block_list* bl;
  16547. unit_id = script_getnum(st,2);
  16548. if (script_isstring(st, 3)) {
  16549. const char *name = script_getstr(st, 3);
  16550. if (!(skill_id = skill_name2id(name))) {
  16551. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  16552. return SCRIPT_CMD_FAILURE;
  16553. }
  16554. } else {
  16555. skill_id = script_getnum(st, 3);
  16556. if (!skill_get_index(skill_id)) {
  16557. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  16558. return SCRIPT_CMD_FAILURE;
  16559. }
  16560. }
  16561. skill_lv = script_getnum(st,4);
  16562. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  16563. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  16564. if(script_rid2bl(2,bl)){
  16565. if (bl->type == BL_NPC) {
  16566. if (!((TBL_NPC*)bl)->status.hp)
  16567. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16568. else
  16569. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16570. }
  16571. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  16572. }
  16573. return SCRIPT_CMD_SUCCESS;
  16574. }
  16575. /// Makes the unit cast the skill on the target position.
  16576. ///
  16577. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16578. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16579. BUILDIN_FUNC(unitskillusepos)
  16580. {
  16581. int skill_x, skill_y, casttime;
  16582. uint16 skill_id, skill_lv;
  16583. struct block_list* bl;
  16584. if (script_isstring(st, 3)) {
  16585. const char *name = script_getstr(st, 3);
  16586. if (!(skill_id = skill_name2id(name))) {
  16587. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  16588. return SCRIPT_CMD_FAILURE;
  16589. }
  16590. } else {
  16591. skill_id = script_getnum(st, 3);
  16592. if (!skill_get_index(skill_id)) {
  16593. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  16594. return SCRIPT_CMD_FAILURE;
  16595. }
  16596. }
  16597. skill_lv = script_getnum(st,4);
  16598. skill_x = script_getnum(st,5);
  16599. skill_y = script_getnum(st,6);
  16600. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16601. if(script_rid2bl(2,bl)){
  16602. if (bl->type == BL_NPC) {
  16603. if (!((TBL_NPC*)bl)->status.hp)
  16604. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16605. else
  16606. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16607. }
  16608. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  16609. }
  16610. return SCRIPT_CMD_SUCCESS;
  16611. }
  16612. // <--- [zBuffer] List of unit control commands
  16613. /// Pauses the execution of the script, detaching the player
  16614. ///
  16615. /// sleep <milli seconds>;
  16616. BUILDIN_FUNC(sleep)
  16617. {
  16618. // First call(by function call)
  16619. if (st->sleep.tick == 0) {
  16620. int ticks;
  16621. ticks = script_getnum(st, 2);
  16622. if (ticks <= 0) {
  16623. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  16624. return SCRIPT_CMD_FAILURE;
  16625. }
  16626. // detach the player
  16627. script_detach_rid(st);
  16628. // sleep for the target amount of time
  16629. st->state = RERUNLINE;
  16630. st->sleep.tick = ticks;
  16631. // Second call(by timer after sleeping time is over)
  16632. } else {
  16633. // Continue the script
  16634. st->state = RUN;
  16635. st->sleep.tick = 0;
  16636. }
  16637. return SCRIPT_CMD_SUCCESS;
  16638. }
  16639. /// Pauses the execution of the script, keeping the unit attached
  16640. /// Stops the script if no unit is attached
  16641. ///
  16642. /// sleep2(<milli seconds>)
  16643. BUILDIN_FUNC(sleep2)
  16644. {
  16645. // First call(by function call)
  16646. if (st->sleep.tick == 0) {
  16647. int ticks;
  16648. ticks = script_getnum(st, 2);
  16649. if (ticks <= 0) {
  16650. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  16651. return SCRIPT_CMD_FAILURE;
  16652. }
  16653. if (map_id2bl(st->rid) == NULL) {
  16654. ShowError( "buildin_sleep2: no unit is attached\n" );
  16655. return SCRIPT_CMD_FAILURE;
  16656. }
  16657. // sleep for the target amount of time
  16658. st->state = RERUNLINE;
  16659. st->sleep.tick = ticks;
  16660. // Second call(by timer after sleeping time is over)
  16661. } else {
  16662. // Check if the unit is still attached
  16663. // NOTE: This should never happen, since run_script_timer already checks this
  16664. if (map_id2bl(st->rid) == NULL) {
  16665. // The unit is not attached anymore - terminate the script
  16666. st->rid = 0;
  16667. st->state = END;
  16668. } else {
  16669. // The unit is still attached - continue the script
  16670. st->state = RUN;
  16671. st->sleep.tick = 0;
  16672. }
  16673. }
  16674. return SCRIPT_CMD_SUCCESS;
  16675. }
  16676. /// Awakes all the sleep timers of the target npc
  16677. ///
  16678. /// awake "<npc name>";
  16679. BUILDIN_FUNC(awake)
  16680. {
  16681. DBIterator *iter;
  16682. struct script_state *tst;
  16683. struct npc_data* nd;
  16684. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  16685. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  16686. return SCRIPT_CMD_FAILURE;
  16687. }
  16688. int rid = st->rid;
  16689. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  16690. if( rid ){
  16691. script_detach_rid(st);
  16692. }
  16693. iter = db_iterator(st_db);
  16694. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  16695. if (tst->oid == nd->bl.id) {
  16696. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  16697. continue;
  16698. }
  16699. delete_timer(tst->sleep.timer, run_script_timer);
  16700. // Trigger the timer function
  16701. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  16702. }
  16703. }
  16704. dbi_destroy(iter);
  16705. // If a player had been attached, now is the time to restore it
  16706. if( rid ){
  16707. st->rid = rid;
  16708. script_attach_state(st);
  16709. }
  16710. return SCRIPT_CMD_SUCCESS;
  16711. }
  16712. /// Returns a reference to a variable of the target NPC.
  16713. /// Returns 0 if an error occurs.
  16714. ///
  16715. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  16716. BUILDIN_FUNC(getvariableofnpc)
  16717. {
  16718. struct script_data* data;
  16719. const char* name;
  16720. struct npc_data* nd;
  16721. data = script_getdata(st,2);
  16722. if( !data_isreference(data) )
  16723. {// Not a reference (aka varaible name)
  16724. ShowError("buildin_getvariableofnpc: not a variable\n");
  16725. script_reportdata(data);
  16726. script_pushnil(st);
  16727. st->state = END;
  16728. return SCRIPT_CMD_FAILURE;
  16729. }
  16730. name = reference_getname(data);
  16731. if( *name != '.' || name[1] == '@' )
  16732. {// not a npc variable
  16733. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  16734. script_reportdata(data);
  16735. script_pushnil(st);
  16736. st->state = END;
  16737. return SCRIPT_CMD_FAILURE;
  16738. }
  16739. nd = npc_name2id(script_getstr(st,3));
  16740. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  16741. {// NPC not found or has no script
  16742. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  16743. script_pushnil(st);
  16744. st->state = END;
  16745. return SCRIPT_CMD_FAILURE;
  16746. }
  16747. if (!nd->u.scr.script->local.vars)
  16748. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  16749. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  16750. return SCRIPT_CMD_SUCCESS;
  16751. }
  16752. /// Opens a warp portal.
  16753. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  16754. ///
  16755. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  16756. ///
  16757. /// @author blackhole89
  16758. BUILDIN_FUNC(warpportal)
  16759. {
  16760. int spx;
  16761. int spy;
  16762. unsigned short mapindex;
  16763. int tpx;
  16764. int tpy;
  16765. struct skill_unit_group* group;
  16766. struct block_list* bl;
  16767. bl = map_id2bl(st->oid);
  16768. if( bl == NULL ) {
  16769. ShowError("buildin_warpportal: NPC is needed\n");
  16770. return SCRIPT_CMD_FAILURE;
  16771. }
  16772. spx = script_getnum(st,2);
  16773. spy = script_getnum(st,3);
  16774. mapindex = mapindex_name2id(script_getstr(st, 4));
  16775. tpx = script_getnum(st,5);
  16776. tpy = script_getnum(st,6);
  16777. if( mapindex == 0 ) {
  16778. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  16779. return SCRIPT_CMD_FAILURE;
  16780. }
  16781. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  16782. if( group == NULL )
  16783. return SCRIPT_CMD_FAILURE;// failed
  16784. group->val1 = (group->val1<<16)|(short)0;
  16785. group->val2 = (tpx<<16) | tpy;
  16786. group->val3 = mapindex;
  16787. return SCRIPT_CMD_SUCCESS;
  16788. }
  16789. /**
  16790. * openmail({<char_id>});
  16791. **/
  16792. BUILDIN_FUNC(openmail)
  16793. {
  16794. TBL_PC* sd;
  16795. if (!script_charid2sd(2,sd))
  16796. return SCRIPT_CMD_FAILURE;
  16797. mail_openmail(sd);
  16798. return SCRIPT_CMD_SUCCESS;
  16799. }
  16800. /**
  16801. * openauction({<char_id>});
  16802. **/
  16803. BUILDIN_FUNC(openauction)
  16804. {
  16805. TBL_PC* sd;
  16806. if (!script_charid2sd(2,sd))
  16807. return SCRIPT_CMD_FAILURE;
  16808. if( !battle_config.feature_auction ) {
  16809. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  16810. return SCRIPT_CMD_SUCCESS;
  16811. }
  16812. clif_Auction_openwindow(sd);
  16813. return SCRIPT_CMD_SUCCESS;
  16814. }
  16815. /// Retrieves the value of the specified flag of the specified cell.
  16816. ///
  16817. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  16818. ///
  16819. /// @see cell_chk* constants in const.txt for the types
  16820. BUILDIN_FUNC(checkcell)
  16821. {
  16822. int16 m = map_mapname2mapid(script_getstr(st,2));
  16823. int16 x = script_getnum(st,3);
  16824. int16 y = script_getnum(st,4);
  16825. cell_chk type = (cell_chk)script_getnum(st,5);
  16826. script_pushint(st, map_getcell(m, x, y, type));
  16827. return SCRIPT_CMD_SUCCESS;
  16828. }
  16829. /// Modifies flags of cells in the specified area.
  16830. ///
  16831. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16832. ///
  16833. /// @see cell_* constants in const.txt for the types
  16834. BUILDIN_FUNC(setcell)
  16835. {
  16836. int16 m = map_mapname2mapid(script_getstr(st,2));
  16837. int16 x1 = script_getnum(st,3);
  16838. int16 y1 = script_getnum(st,4);
  16839. int16 x2 = script_getnum(st,5);
  16840. int16 y2 = script_getnum(st,6);
  16841. cell_t type = (cell_t)script_getnum(st,7);
  16842. bool flag = script_getnum(st,8) != 0;
  16843. int x,y;
  16844. if( x1 > x2 ) SWAP(x1,x2);
  16845. if( y1 > y2 ) SWAP(y1,y2);
  16846. for( y = y1; y <= y2; ++y )
  16847. for( x = x1; x <= x2; ++x )
  16848. map_setcell(m, x, y, type, flag);
  16849. return SCRIPT_CMD_SUCCESS;
  16850. }
  16851. /**
  16852. * Gets a free cell in the specified area.
  16853. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  16854. */
  16855. BUILDIN_FUNC(getfreecell)
  16856. {
  16857. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  16858. struct script_data
  16859. *data_x = script_getdata(st, 3),
  16860. *data_y = script_getdata(st, 4);
  16861. struct block_list* bl = map_id2bl(st->rid);
  16862. struct map_session_data *sd = nullptr;
  16863. int16 m, x = 0, y = 0;
  16864. int rx = -1, ry = -1, flag = 1;
  16865. if (!data_isreference(data_x)) {
  16866. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  16867. return SCRIPT_CMD_FAILURE;
  16868. }
  16869. if (!data_isreference(data_y)) {
  16870. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  16871. return SCRIPT_CMD_FAILURE;
  16872. }
  16873. name_x = reference_getname(data_x),
  16874. name_y = reference_getname(data_y);
  16875. if (is_string_variable(name_x)) {
  16876. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  16877. return SCRIPT_CMD_FAILURE;
  16878. }
  16879. if (is_string_variable(name_y)) {
  16880. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  16881. return SCRIPT_CMD_FAILURE;
  16882. }
  16883. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  16884. if (!script_rid2sd(sd)) {
  16885. if (not_server_variable(*name_x))
  16886. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  16887. else
  16888. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  16889. return SCRIPT_CMD_FAILURE;
  16890. }
  16891. }
  16892. if (script_hasdata(st, 5))
  16893. x = script_getnum(st, 5);
  16894. if (script_hasdata(st, 6))
  16895. y = script_getnum(st, 6);
  16896. if (script_hasdata(st, 7))
  16897. rx = script_getnum(st, 7);
  16898. if (script_hasdata(st, 8))
  16899. ry = script_getnum(st, 8);
  16900. if (script_hasdata(st, 9))
  16901. flag = script_getnum(st, 9);
  16902. if (bl && strcmp(mapn, "this") == 0)
  16903. m = bl->m;
  16904. else
  16905. m = map_mapname2mapid(mapn);
  16906. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  16907. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  16908. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  16909. return SCRIPT_CMD_SUCCESS;
  16910. }
  16911. /*==========================================
  16912. * Mercenary Commands
  16913. *------------------------------------------*/
  16914. BUILDIN_FUNC(mercenary_create)
  16915. {
  16916. struct map_session_data *sd;
  16917. int class_, contract_time;
  16918. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  16919. return SCRIPT_CMD_SUCCESS;
  16920. class_ = script_getnum(st,2);
  16921. if( !mercenary_db(class_) )
  16922. return SCRIPT_CMD_SUCCESS;
  16923. contract_time = script_getnum(st,3);
  16924. mercenary_create(sd, class_, contract_time);
  16925. return SCRIPT_CMD_SUCCESS;
  16926. }
  16927. BUILDIN_FUNC(mercenary_heal)
  16928. {
  16929. struct map_session_data *sd;
  16930. int hp, sp;
  16931. if( !script_rid2sd(sd) || sd->md == NULL )
  16932. return SCRIPT_CMD_SUCCESS;
  16933. hp = script_getnum(st,2);
  16934. sp = script_getnum(st,3);
  16935. status_heal(&sd->md->bl, hp, sp, 0);
  16936. return SCRIPT_CMD_SUCCESS;
  16937. }
  16938. BUILDIN_FUNC(mercenary_sc_start)
  16939. {
  16940. struct map_session_data *sd;
  16941. enum sc_type type;
  16942. int tick, val1;
  16943. if( !script_rid2sd(sd) || sd->md == NULL )
  16944. return SCRIPT_CMD_SUCCESS;
  16945. type = (sc_type)script_getnum(st,2);
  16946. tick = script_getnum(st,3);
  16947. val1 = script_getnum(st,4);
  16948. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16949. return SCRIPT_CMD_SUCCESS;
  16950. }
  16951. BUILDIN_FUNC(mercenary_get_calls)
  16952. {
  16953. struct map_session_data *sd;
  16954. int guild;
  16955. if( !script_rid2sd(sd) )
  16956. return SCRIPT_CMD_SUCCESS;
  16957. guild = script_getnum(st,2);
  16958. switch( guild )
  16959. {
  16960. case ARCH_MERC_GUILD:
  16961. script_pushint(st,sd->status.arch_calls);
  16962. break;
  16963. case SPEAR_MERC_GUILD:
  16964. script_pushint(st,sd->status.spear_calls);
  16965. break;
  16966. case SWORD_MERC_GUILD:
  16967. script_pushint(st,sd->status.sword_calls);
  16968. break;
  16969. default:
  16970. script_pushint(st,0);
  16971. break;
  16972. }
  16973. return SCRIPT_CMD_SUCCESS;
  16974. }
  16975. BUILDIN_FUNC(mercenary_set_calls)
  16976. {
  16977. struct map_session_data *sd;
  16978. int guild, value, *calls;
  16979. if( !script_rid2sd(sd) )
  16980. return SCRIPT_CMD_SUCCESS;
  16981. guild = script_getnum(st,2);
  16982. value = script_getnum(st,3);
  16983. switch( guild )
  16984. {
  16985. case ARCH_MERC_GUILD:
  16986. calls = &sd->status.arch_calls;
  16987. break;
  16988. case SPEAR_MERC_GUILD:
  16989. calls = &sd->status.spear_calls;
  16990. break;
  16991. case SWORD_MERC_GUILD:
  16992. calls = &sd->status.sword_calls;
  16993. break;
  16994. default:
  16995. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16996. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16997. }
  16998. *calls += value;
  16999. *calls = cap_value(*calls, 0, INT_MAX);
  17000. return SCRIPT_CMD_SUCCESS;
  17001. }
  17002. BUILDIN_FUNC(mercenary_get_faith)
  17003. {
  17004. struct map_session_data *sd;
  17005. int guild;
  17006. if( !script_rid2sd(sd) )
  17007. return SCRIPT_CMD_SUCCESS;
  17008. guild = script_getnum(st,2);
  17009. switch( guild )
  17010. {
  17011. case ARCH_MERC_GUILD:
  17012. script_pushint(st,sd->status.arch_faith);
  17013. break;
  17014. case SPEAR_MERC_GUILD:
  17015. script_pushint(st,sd->status.spear_faith);
  17016. break;
  17017. case SWORD_MERC_GUILD:
  17018. script_pushint(st,sd->status.sword_faith);
  17019. break;
  17020. default:
  17021. script_pushint(st,0);
  17022. break;
  17023. }
  17024. return SCRIPT_CMD_SUCCESS;
  17025. }
  17026. BUILDIN_FUNC(mercenary_set_faith)
  17027. {
  17028. struct map_session_data *sd;
  17029. int guild, value, *calls;
  17030. if( !script_rid2sd(sd) )
  17031. return SCRIPT_CMD_SUCCESS;
  17032. guild = script_getnum(st,2);
  17033. value = script_getnum(st,3);
  17034. switch( guild )
  17035. {
  17036. case ARCH_MERC_GUILD:
  17037. calls = &sd->status.arch_faith;
  17038. break;
  17039. case SPEAR_MERC_GUILD:
  17040. calls = &sd->status.spear_faith;
  17041. break;
  17042. case SWORD_MERC_GUILD:
  17043. calls = &sd->status.sword_faith;
  17044. break;
  17045. default:
  17046. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17047. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17048. }
  17049. *calls += value;
  17050. *calls = cap_value(*calls, 0, INT_MAX);
  17051. if( mercenary_get_guild(sd->md) == guild )
  17052. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17053. return SCRIPT_CMD_SUCCESS;
  17054. }
  17055. /*------------------------------------------
  17056. * Book Reading
  17057. *------------------------------------------*/
  17058. BUILDIN_FUNC(readbook)
  17059. {
  17060. struct map_session_data *sd;
  17061. int book_id, page;
  17062. if( !script_rid2sd(sd) )
  17063. return SCRIPT_CMD_SUCCESS;
  17064. book_id = script_getnum(st,2);
  17065. page = script_getnum(st,3);
  17066. clif_readbook(sd->fd, book_id, page);
  17067. return SCRIPT_CMD_SUCCESS;
  17068. }
  17069. /******************
  17070. Questlog script commands
  17071. *******************/
  17072. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17073. BUILDIN_FUNC(questinfo)
  17074. {
  17075. TBL_NPC* nd = map_id2nd(st->oid);
  17076. if (!nd || nd->bl.m == -1) {
  17077. ShowError("buildin_questinfo: No NPC attached.\n");
  17078. return SCRIPT_CMD_FAILURE;
  17079. }
  17080. int icon = script_getnum(st, 2);
  17081. #if PACKETVER >= 20120410
  17082. switch(icon){
  17083. case QTYPE_QUEST:
  17084. case QTYPE_QUEST2:
  17085. case QTYPE_JOB:
  17086. case QTYPE_JOB2:
  17087. case QTYPE_EVENT:
  17088. case QTYPE_EVENT2:
  17089. // Warg icons were replaced in this client
  17090. #if PACKETVER < 20170315
  17091. case QTYPE_WARG:
  17092. case QTYPE_WARG2:
  17093. #else
  17094. case QTYPE_CLICKME:
  17095. case QTYPE_DAILYQUEST:
  17096. case QTYPE_EVENT3:
  17097. case QTYPE_JOBQUEST:
  17098. case QTYPE_JUMPING_PORING:
  17099. #endif
  17100. // Leave everything as it is
  17101. break;
  17102. case QTYPE_NONE:
  17103. default:
  17104. // Default to nothing if icon id is invalid.
  17105. icon = QTYPE_NONE;
  17106. break;
  17107. }
  17108. #else
  17109. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17110. icon = QTYPE_QUEST;
  17111. else
  17112. icon = icon + 1;
  17113. #endif
  17114. int color = QMARK_NONE;
  17115. if (script_hasdata(st, 3)) {
  17116. color = script_getnum(st, 3);
  17117. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17118. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17119. script_reportfunc(st);
  17120. color = QMARK_NONE;
  17121. }
  17122. }
  17123. struct script_code *script = nullptr;
  17124. if (script_hasdata(st, 4)) {
  17125. const char *str = script_getstr(st, 4);
  17126. if (str) {
  17127. std::string condition(str);
  17128. if (condition.find( "achievement_condition" ) == std::string::npos)
  17129. condition = "achievement_condition( " + condition + " );";
  17130. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17131. if (!script) {
  17132. st->state = END;
  17133. return SCRIPT_CMD_FAILURE;
  17134. }
  17135. }
  17136. }
  17137. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17138. qi->icon = static_cast<e_questinfo_types>(icon);
  17139. qi->color = static_cast<e_questinfo_markcolor>(color);
  17140. qi->condition = script;
  17141. nd->qi_data.push_back(qi);
  17142. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17143. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17144. mapdata->qi_npc.push_back(nd->bl.id);
  17145. return SCRIPT_CMD_SUCCESS;
  17146. }
  17147. /**
  17148. * questinfo_refresh {<char_id>};
  17149. **/
  17150. BUILDIN_FUNC(questinfo_refresh)
  17151. {
  17152. struct map_session_data *sd;
  17153. if (!script_charid2sd(2, sd))
  17154. return SCRIPT_CMD_FAILURE;
  17155. pc_show_questinfo(sd);
  17156. return SCRIPT_CMD_SUCCESS;
  17157. }
  17158. /**
  17159. * setquest <ID>{,<char_id>};
  17160. **/
  17161. BUILDIN_FUNC(setquest)
  17162. {
  17163. struct map_session_data *sd;
  17164. int quest_id;
  17165. quest_id = script_getnum(st, 2);
  17166. if (!script_charid2sd(3,sd))
  17167. return SCRIPT_CMD_FAILURE;
  17168. if( quest_add(sd, quest_id) == -1 ){
  17169. script_reportsrc(st);
  17170. script_reportfunc(st);
  17171. }
  17172. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17173. pc_show_questinfo(sd);
  17174. return SCRIPT_CMD_SUCCESS;
  17175. }
  17176. /**
  17177. * erasequest <ID>{,<char_id>};
  17178. **/
  17179. BUILDIN_FUNC(erasequest)
  17180. {
  17181. struct map_session_data *sd;
  17182. if (!script_charid2sd(3,sd))
  17183. return SCRIPT_CMD_FAILURE;
  17184. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17185. script_reportsrc(st);
  17186. script_reportfunc(st);
  17187. }
  17188. return SCRIPT_CMD_SUCCESS;
  17189. }
  17190. /**
  17191. * completequest <ID>{,<char_id>};
  17192. **/
  17193. BUILDIN_FUNC(completequest)
  17194. {
  17195. struct map_session_data *sd;
  17196. if (!script_charid2sd(3,sd))
  17197. return SCRIPT_CMD_FAILURE;
  17198. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17199. //20120410 or 20090218
  17200. pc_show_questinfo(sd);
  17201. return SCRIPT_CMD_SUCCESS;
  17202. }
  17203. /**
  17204. * changequest <ID>,<ID2>{,<char_id>};
  17205. **/
  17206. BUILDIN_FUNC(changequest)
  17207. {
  17208. struct map_session_data *sd;
  17209. if (!script_charid2sd(4,sd))
  17210. return SCRIPT_CMD_FAILURE;
  17211. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17212. script_reportsrc(st);
  17213. script_reportfunc(st);
  17214. }
  17215. //20120410 or 20090218
  17216. pc_show_questinfo(sd);
  17217. return SCRIPT_CMD_SUCCESS;
  17218. }
  17219. /**
  17220. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17221. **/
  17222. BUILDIN_FUNC(checkquest)
  17223. {
  17224. struct map_session_data *sd;
  17225. e_quest_check_type type = HAVEQUEST;
  17226. if( script_hasdata(st, 3) )
  17227. type = (e_quest_check_type)script_getnum(st, 3);
  17228. if (!script_charid2sd(4,sd))
  17229. return SCRIPT_CMD_FAILURE;
  17230. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17231. return SCRIPT_CMD_SUCCESS;
  17232. }
  17233. /**
  17234. * isbegin_quest(<ID>{,<char_id>})
  17235. **/
  17236. BUILDIN_FUNC(isbegin_quest)
  17237. {
  17238. struct map_session_data *sd;
  17239. if (!script_charid2sd(3,sd))
  17240. return SCRIPT_CMD_FAILURE;
  17241. int i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17242. script_pushint(st, i + (i < 1));
  17243. return SCRIPT_CMD_SUCCESS;
  17244. }
  17245. /**
  17246. * showevent <icon>{,<mark color>{,<char_id>}}
  17247. **/
  17248. BUILDIN_FUNC(showevent)
  17249. {
  17250. TBL_PC *sd;
  17251. if (!script_charid2sd(4,sd))
  17252. return SCRIPT_CMD_FAILURE;
  17253. struct npc_data *nd = map_id2nd(st->oid);
  17254. if (!nd)
  17255. return SCRIPT_CMD_SUCCESS;
  17256. int color = QMARK_NONE;
  17257. int icon = script_getnum(st, 2);
  17258. if (script_hasdata(st, 3)) {
  17259. color = script_getnum(st, 3);
  17260. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17261. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17262. script_reportfunc(st);
  17263. color = QMARK_NONE;
  17264. }
  17265. }
  17266. #if PACKETVER >= 20120410
  17267. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17268. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17269. #else
  17270. if (icon < 0 || icon > 7)
  17271. icon = 0;
  17272. else
  17273. icon = icon + 1;
  17274. #endif
  17275. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17276. return SCRIPT_CMD_SUCCESS;
  17277. }
  17278. /*==========================================
  17279. * BattleGround System
  17280. *------------------------------------------*/
  17281. BUILDIN_FUNC(waitingroom2bg)
  17282. {
  17283. struct npc_data *nd;
  17284. struct chat_data *cd;
  17285. const char *map_name;
  17286. int mapindex = 0, bg_id;
  17287. unsigned char i,c=0;
  17288. struct s_battleground_team team;
  17289. if( script_hasdata(st,7) )
  17290. nd = npc_name2id(script_getstr(st,7));
  17291. else
  17292. nd = (struct npc_data *)map_id2bl(st->oid);
  17293. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  17294. {
  17295. script_pushint(st,0);
  17296. return SCRIPT_CMD_SUCCESS;
  17297. }
  17298. map_name = script_getstr(st,2);
  17299. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17300. { // Invalid Map
  17301. script_pushint(st, 0);
  17302. return SCRIPT_CMD_SUCCESS;
  17303. }
  17304. team.warp_x = script_getnum(st,3);
  17305. team.warp_y = script_getnum(st,4);
  17306. if (script_hasdata(st,5)) {
  17307. team.quit_event = script_getstr(st,5); // Logout Event
  17308. check_event(st, team.quit_event.c_str());
  17309. } else
  17310. team.quit_event = "";
  17311. if (script_hasdata(st,6)) {
  17312. team.death_event = script_getstr(st,6); // Die Event
  17313. check_event(st, team.death_event.c_str());
  17314. } else
  17315. team.death_event = "";
  17316. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  17317. { // Creation failed
  17318. script_pushint(st,0);
  17319. return SCRIPT_CMD_SUCCESS;
  17320. }
  17321. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  17322. struct map_session_data *sd;
  17323. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd, false) ){
  17324. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  17325. ++c;
  17326. }
  17327. }
  17328. mapreg_setreg(add_str("$@arenamembersnum"), c);
  17329. script_pushint(st,bg_id);
  17330. return SCRIPT_CMD_SUCCESS;
  17331. }
  17332. BUILDIN_FUNC(waitingroom2bg_single)
  17333. {
  17334. const char* map_name;
  17335. struct npc_data *nd;
  17336. struct chat_data *cd;
  17337. struct map_session_data *sd;
  17338. int x, y, mapindex, bg_id = script_getnum(st,2);
  17339. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17340. if (!bg) {
  17341. script_pushint(st, false);
  17342. return SCRIPT_CMD_SUCCESS;
  17343. }
  17344. if (script_hasdata(st, 3)) {
  17345. map_name = script_getstr(st, 3);
  17346. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17347. script_pushint(st, false);
  17348. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17349. }
  17350. x = script_getnum(st, 4);
  17351. y = script_getnum(st, 5);
  17352. }
  17353. else {
  17354. mapindex = bg->cemetery.map;
  17355. x = bg->cemetery.x;
  17356. y = bg->cemetery.y;
  17357. }
  17358. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17359. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17360. script_pushint(st, false);
  17361. return SCRIPT_CMD_FAILURE;
  17362. }
  17363. nd = npc_name2id(script_getstr(st,6));
  17364. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  17365. return SCRIPT_CMD_SUCCESS;
  17366. if( (sd = cd->usersd[0]) == NULL )
  17367. return SCRIPT_CMD_SUCCESS;
  17368. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17369. {
  17370. script_pushint(st, true);
  17371. }
  17372. else
  17373. script_pushint(st, false);
  17374. return SCRIPT_CMD_SUCCESS;
  17375. }
  17376. /// Creates an instance of battleground battle group.
  17377. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  17378. /// @author [secretdataz]
  17379. BUILDIN_FUNC(bg_create) {
  17380. const char *map_name;
  17381. int mapindex = 0;
  17382. struct s_battleground_team team;
  17383. map_name = script_getstr(st, 2);
  17384. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17385. { // Invalid Map
  17386. script_pushint(st, 0);
  17387. return SCRIPT_CMD_SUCCESS;
  17388. }
  17389. team.warp_x = script_getnum(st,3);
  17390. team.warp_y = script_getnum(st,4);
  17391. if (script_hasdata(st,5)) {
  17392. team.quit_event = script_getstr(st,5); // Logout Event
  17393. check_event(st, team.quit_event.c_str());
  17394. } else
  17395. team.quit_event = "";
  17396. if (script_hasdata(st,6)) {
  17397. team.death_event = script_getstr(st,6); // Die Event
  17398. check_event(st, team.death_event.c_str());
  17399. } else
  17400. team.death_event = "";
  17401. script_pushint(st, bg_create(mapindex, &team));
  17402. return SCRIPT_CMD_SUCCESS;
  17403. }
  17404. /// Adds attached player or <char id> (if specified) to an existing
  17405. /// battleground group and warps it to the specified coordinates on
  17406. /// the given map.
  17407. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  17408. /// @author [secretdataz]
  17409. BUILDIN_FUNC(bg_join) {
  17410. const char* map_name;
  17411. struct map_session_data *sd;
  17412. int x, y, mapindex, bg_id = script_getnum(st, 2);
  17413. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17414. if (!bg) {
  17415. script_pushint(st, false);
  17416. return SCRIPT_CMD_SUCCESS;
  17417. }
  17418. if (script_hasdata(st, 3)) {
  17419. map_name = script_getstr(st, 3);
  17420. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17421. script_pushint(st, false);
  17422. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17423. }
  17424. x = script_getnum(st, 4);
  17425. y = script_getnum(st, 5);
  17426. } else {
  17427. mapindex = bg->cemetery.map;
  17428. x = bg->cemetery.x;
  17429. y = bg->cemetery.y;
  17430. }
  17431. if (!script_charid2sd(6, sd)) {
  17432. script_pushint(st, false);
  17433. return SCRIPT_CMD_FAILURE;
  17434. }
  17435. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17436. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17437. script_pushint(st, false);
  17438. return SCRIPT_CMD_FAILURE;
  17439. }
  17440. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17441. {
  17442. script_pushint(st, true);
  17443. }
  17444. else
  17445. script_pushint(st, false);
  17446. return SCRIPT_CMD_SUCCESS;
  17447. }
  17448. BUILDIN_FUNC(bg_team_setxy)
  17449. {
  17450. int bg_id = script_getnum(st,2);
  17451. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17452. if (bg) {
  17453. bg->cemetery.x = script_getnum(st, 3);
  17454. bg->cemetery.y = script_getnum(st, 4);
  17455. }
  17456. return SCRIPT_CMD_SUCCESS;
  17457. }
  17458. BUILDIN_FUNC(bg_warp)
  17459. {
  17460. int x, y, mapindex, bg_id;
  17461. const char* map_name;
  17462. bg_id = script_getnum(st,2);
  17463. map_name = script_getstr(st,3);
  17464. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  17465. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17466. x = script_getnum(st,4);
  17467. y = script_getnum(st,5);
  17468. bg_team_warp(bg_id, mapindex, x, y);
  17469. return SCRIPT_CMD_SUCCESS;
  17470. }
  17471. BUILDIN_FUNC(bg_monster)
  17472. {
  17473. int class_ = 0, x = 0, y = 0, bg_id = 0;
  17474. const char *str,*mapname, *evt="";
  17475. bg_id = script_getnum(st,2);
  17476. mapname = script_getstr(st,3);
  17477. x = script_getnum(st,4);
  17478. y = script_getnum(st,5);
  17479. str = script_getstr(st,6);
  17480. class_ = script_getnum(st,7);
  17481. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  17482. check_event(st, evt);
  17483. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  17484. return SCRIPT_CMD_SUCCESS;
  17485. }
  17486. BUILDIN_FUNC(bg_monster_set_team)
  17487. {
  17488. struct mob_data *md;
  17489. struct block_list *mbl;
  17490. int id = script_getnum(st,2),
  17491. bg_id = script_getnum(st,3);
  17492. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  17493. return SCRIPT_CMD_SUCCESS;
  17494. md = (TBL_MOB *)mbl;
  17495. md->bg_id = bg_id;
  17496. mob_stop_attack(md);
  17497. mob_stop_walking(md, 0);
  17498. md->target_id = md->attacked_id = 0;
  17499. clif_name_area(&md->bl);
  17500. return SCRIPT_CMD_SUCCESS;
  17501. }
  17502. BUILDIN_FUNC(bg_leave)
  17503. {
  17504. struct map_session_data *sd = NULL;
  17505. bool deserter = false;
  17506. if( !script_charid2sd(2,sd) || !sd->bg_id )
  17507. return SCRIPT_CMD_SUCCESS;
  17508. if (!strcmp(script_getfuncname(st), "bg_desert"))
  17509. deserter = true;
  17510. bg_team_leave(sd, false, deserter);
  17511. return SCRIPT_CMD_SUCCESS;
  17512. }
  17513. BUILDIN_FUNC(bg_destroy)
  17514. {
  17515. int bg_id = script_getnum(st,2);
  17516. bg_team_delete(bg_id);
  17517. return SCRIPT_CMD_SUCCESS;
  17518. }
  17519. BUILDIN_FUNC(bg_getareausers)
  17520. {
  17521. const char *str = script_getstr(st, 3);
  17522. int16 m, x0, y0, x1, y1;
  17523. int bg_id = script_getnum(st, 2), c = 0;
  17524. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17525. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  17526. script_pushint(st,0);
  17527. return SCRIPT_CMD_SUCCESS;
  17528. }
  17529. x0 = script_getnum(st,4);
  17530. y0 = script_getnum(st,5);
  17531. x1 = script_getnum(st,6);
  17532. y1 = script_getnum(st,7);
  17533. for (const auto &member : bg->members) {
  17534. 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 )
  17535. continue;
  17536. c++;
  17537. }
  17538. script_pushint(st,c);
  17539. return SCRIPT_CMD_SUCCESS;
  17540. }
  17541. BUILDIN_FUNC(bg_updatescore)
  17542. {
  17543. const char *str;
  17544. int16 m;
  17545. str = script_getstr(st,2);
  17546. if( (m = map_mapname2mapid(str)) < 0 )
  17547. return SCRIPT_CMD_SUCCESS;
  17548. struct map_data *mapdata = map_getmapdata(m);
  17549. mapdata->bgscore_lion = script_getnum(st,3);
  17550. mapdata->bgscore_eagle = script_getnum(st,4);
  17551. clif_bg_updatescore(m);
  17552. return SCRIPT_CMD_SUCCESS;
  17553. }
  17554. BUILDIN_FUNC(bg_get_data)
  17555. {
  17556. int bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  17557. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17558. if (bg) {
  17559. switch (type) {
  17560. case 0:
  17561. script_pushint(st, bg->members.size());
  17562. break;
  17563. case 1:
  17564. for (const auto &member : bg->members)
  17565. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  17566. mapreg_setreg(add_str("$@arenamemberscount"), i);
  17567. script_pushint(st, i);
  17568. break;
  17569. default:
  17570. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  17571. break;
  17572. }
  17573. } else
  17574. script_pushint(st, 0);
  17575. return SCRIPT_CMD_SUCCESS;
  17576. }
  17577. /**
  17578. * Reserves a slot for the given Battleground.
  17579. * bg_reserve("<battleground_map_name>"{,<ended>});
  17580. */
  17581. BUILDIN_FUNC(bg_reserve)
  17582. {
  17583. const char *str = script_getstr(st, 2);
  17584. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  17585. if (!bg_queue_reserve(str, ended))
  17586. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  17587. return SCRIPT_CMD_SUCCESS;
  17588. }
  17589. /**
  17590. * Removes a spot for the given Battleground.
  17591. * bg_unbook("<battleground_map_name>");
  17592. */
  17593. BUILDIN_FUNC(bg_unbook)
  17594. {
  17595. const char *str = script_getstr(st, 2);
  17596. if (!bg_queue_unbook(str))
  17597. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  17598. return SCRIPT_CMD_SUCCESS;
  17599. }
  17600. /**
  17601. * Gets battleground database information.
  17602. * bg_info("<battleground name>", <type>);
  17603. */
  17604. BUILDIN_FUNC(bg_info)
  17605. {
  17606. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  17607. if (!bg) {
  17608. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  17609. return SCRIPT_CMD_FAILURE;
  17610. }
  17611. int type = script_getnum(st, 3);
  17612. switch (type) {
  17613. case BG_INFO_ID:
  17614. script_pushint(st, bg->id);
  17615. break;
  17616. case BG_INFO_REQUIRED_PLAYERS:
  17617. script_pushint(st, bg->required_players);
  17618. break;
  17619. case BG_INFO_MAX_PLAYERS:
  17620. script_pushint(st, bg->max_players);
  17621. break;
  17622. case BG_INFO_MIN_LEVEL:
  17623. script_pushint(st, bg->min_lvl);
  17624. break;
  17625. case BG_INFO_MAX_LEVEL:
  17626. script_pushint(st, bg->max_lvl);
  17627. break;
  17628. case BG_INFO_MAPS: {
  17629. size_t i;
  17630. for (i = 0; i < bg->maps.size(); i++)
  17631. setd_sub_str(st, nullptr, ".@bgmaps$", i, mapindex_id2name(bg->maps[i].mapindex), nullptr);
  17632. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  17633. script_pushint(st, i);
  17634. break;
  17635. }
  17636. case BG_INFO_DESERTER_TIME:
  17637. script_pushint(st, bg->deserter_time);
  17638. break;
  17639. default:
  17640. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  17641. return SCRIPT_CMD_FAILURE;
  17642. }
  17643. return SCRIPT_CMD_SUCCESS;
  17644. }
  17645. /*==========================================
  17646. * Instancing System
  17647. *------------------------------------------*/
  17648. /**
  17649. * Returns an Instance ID.
  17650. * @param st: Script state
  17651. * @param mode: Instance mode
  17652. * @return instance ID on success or 0 otherwise
  17653. */
  17654. int script_instancegetid(struct script_state* st, e_instance_mode mode)
  17655. {
  17656. int instance_id = 0;
  17657. if (mode == IM_NONE) {
  17658. struct npc_data *nd = map_id2nd(st->oid);
  17659. if (nd->instance_id > 0)
  17660. instance_id = nd->instance_id;
  17661. } else {
  17662. struct map_session_data *sd = map_id2sd(st->rid);
  17663. if (sd) {
  17664. switch (mode) {
  17665. case IM_CHAR:
  17666. if (sd->instance_id > 0)
  17667. instance_id = sd->instance_id;
  17668. break;
  17669. case IM_PARTY: {
  17670. struct party_data *pd = party_search(sd->status.party_id);
  17671. if (pd && pd->instance_id > 0)
  17672. instance_id = pd->instance_id;
  17673. }
  17674. break;
  17675. case IM_GUILD: {
  17676. struct guild *gd = guild_search(sd->status.guild_id);
  17677. if (gd && gd->instance_id > 0)
  17678. instance_id = gd->instance_id;
  17679. }
  17680. break;
  17681. case IM_CLAN: {
  17682. struct clan *cd = clan_search(sd->status.clan_id);
  17683. if (cd && cd->instance_id > 0)
  17684. instance_id = cd->instance_id;
  17685. }
  17686. break;
  17687. default: // Unsupported type
  17688. break;
  17689. }
  17690. }
  17691. }
  17692. return instance_id;
  17693. }
  17694. /*==========================================
  17695. * Creates the instance
  17696. * Returns Instance ID if created successfully
  17697. *------------------------------------------*/
  17698. BUILDIN_FUNC(instance_create)
  17699. {
  17700. e_instance_mode mode = IM_PARTY;
  17701. int owner_id = 0;
  17702. if (script_hasdata(st, 3)) {
  17703. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  17704. if (mode < IM_NONE || mode >= IM_MAX) {
  17705. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  17706. return SCRIPT_CMD_FAILURE;
  17707. }
  17708. }
  17709. if (script_hasdata(st, 4))
  17710. owner_id = script_getnum(st, 4);
  17711. else {
  17712. // If sd is NULL, instance_create will return -2.
  17713. struct map_session_data *sd = NULL;
  17714. switch(mode) {
  17715. case IM_NONE:
  17716. owner_id = st->oid;
  17717. break;
  17718. case IM_CHAR:
  17719. if (script_rid2sd(sd))
  17720. owner_id = sd->status.char_id;
  17721. break;
  17722. case IM_PARTY:
  17723. if (script_rid2sd(sd))
  17724. owner_id = sd->status.party_id;
  17725. break;
  17726. case IM_GUILD:
  17727. if (script_rid2sd(sd))
  17728. owner_id = sd->status.guild_id;
  17729. break;
  17730. case IM_CLAN:
  17731. if (script_rid2sd(sd))
  17732. owner_id = sd->status.clan_id;
  17733. break;
  17734. default:
  17735. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  17736. return SCRIPT_CMD_FAILURE;
  17737. }
  17738. }
  17739. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  17740. return SCRIPT_CMD_SUCCESS;
  17741. }
  17742. /*==========================================
  17743. * Destroys an instance (unofficial)
  17744. * Officially instances are only destroyed by timeout
  17745. *
  17746. * instance_destroy {<instance_id>};
  17747. *------------------------------------------*/
  17748. BUILDIN_FUNC(instance_destroy)
  17749. {
  17750. int instance_id;
  17751. if( script_hasdata(st,2) )
  17752. instance_id = script_getnum(st,2);
  17753. else
  17754. instance_id = script_instancegetid(st);
  17755. if( instance_id == 0 ) {
  17756. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  17757. return SCRIPT_CMD_FAILURE;
  17758. }
  17759. instance_destroy(instance_id);
  17760. return SCRIPT_CMD_SUCCESS;
  17761. }
  17762. /*==========================================
  17763. * Warps player to instance
  17764. * Results:
  17765. * IE_OK: Success
  17766. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  17767. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  17768. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  17769. *------------------------------------------*/
  17770. BUILDIN_FUNC(instance_enter)
  17771. {
  17772. struct map_session_data *sd = NULL;
  17773. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  17774. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  17775. int instance_id;
  17776. if (script_hasdata(st, 6))
  17777. instance_id = script_getnum(st, 6);
  17778. else
  17779. instance_id = script_instancegetid(st, IM_PARTY);
  17780. if (!script_charid2sd(5,sd))
  17781. return SCRIPT_CMD_FAILURE;
  17782. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  17783. return SCRIPT_CMD_SUCCESS;
  17784. }
  17785. /*==========================================
  17786. * Returns the name of a duplicated NPC
  17787. *
  17788. * instance_npcname <npc_name>{,<instance_id>};
  17789. * <npc_name> is the full name of an NPC.
  17790. *------------------------------------------*/
  17791. BUILDIN_FUNC(instance_npcname)
  17792. {
  17793. const char *str;
  17794. int instance_id;
  17795. struct npc_data *nd;
  17796. str = script_getstr(st,2);
  17797. if( script_hasdata(st,3) )
  17798. instance_id = script_getnum(st,3);
  17799. else
  17800. instance_id = script_instancegetid(st);
  17801. if( instance_id > 0 && (nd = npc_name2id(str)) != NULL ) {
  17802. static char npcname[NAME_LENGTH];
  17803. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  17804. script_pushconststr(st,npcname);
  17805. } else {
  17806. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  17807. st->state = END;
  17808. return SCRIPT_CMD_FAILURE;
  17809. }
  17810. return SCRIPT_CMD_SUCCESS;
  17811. }
  17812. /*==========================================
  17813. * Returns the name of a duplicated map
  17814. *
  17815. * instance_mapname <map_name>{,<instance_id>};
  17816. *------------------------------------------*/
  17817. BUILDIN_FUNC(instance_mapname)
  17818. {
  17819. const char *str;
  17820. int16 m;
  17821. int instance_id;
  17822. str = script_getstr(st,2);
  17823. if( script_hasdata(st,3) )
  17824. instance_id = script_getnum(st,3);
  17825. else
  17826. instance_id = script_instancegetid(st);
  17827. // Check that instance mapname is a valid map
  17828. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  17829. script_pushconststr(st, "");
  17830. else
  17831. script_pushconststr(st, map_getmapdata(m)->name);
  17832. return SCRIPT_CMD_SUCCESS;
  17833. }
  17834. /*==========================================
  17835. * Returns an Instance ID
  17836. *------------------------------------------*/
  17837. BUILDIN_FUNC(instance_id)
  17838. {
  17839. int mode = IM_NONE; // Default to the attached NPC
  17840. if (script_hasdata(st, 2)) {
  17841. mode = script_getnum(st, 2);
  17842. if (mode <= IM_NONE || mode >= IM_MAX) {
  17843. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  17844. script_pushint(st, 0);
  17845. return SCRIPT_CMD_SUCCESS;
  17846. }
  17847. }
  17848. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  17849. return SCRIPT_CMD_SUCCESS;
  17850. }
  17851. /*==========================================
  17852. * Warps all players inside an instance
  17853. *
  17854. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  17855. *------------------------------------------*/
  17856. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  17857. {
  17858. unsigned int m = va_arg(ap,unsigned int);
  17859. int x = va_arg(ap,int);
  17860. int y = va_arg(ap,int);
  17861. int instance_id = va_arg(ap, int);
  17862. struct map_session_data *sd;
  17863. nullpo_retr(0, bl);
  17864. if (bl->type != BL_PC)
  17865. return 0;
  17866. sd = (TBL_PC *)bl;
  17867. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  17868. if (!idata)
  17869. return 0;
  17870. int owner_id = idata->owner_id;
  17871. switch(idata->mode) {
  17872. case IM_NONE:
  17873. break;
  17874. case IM_CHAR:
  17875. if (sd->status.char_id != owner_id)
  17876. return 0;
  17877. break;
  17878. case IM_PARTY:
  17879. if (sd->status.party_id != owner_id)
  17880. return 0;
  17881. break;
  17882. case IM_GUILD:
  17883. if (sd->status.guild_id != owner_id)
  17884. return 0;
  17885. case IM_CLAN:
  17886. if (sd->status.clan_id != owner_id)
  17887. return 0;
  17888. }
  17889. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  17890. return 1;
  17891. }
  17892. BUILDIN_FUNC(instance_warpall)
  17893. {
  17894. int16 m;
  17895. int instance_id;
  17896. const char *mapn;
  17897. int x, y;
  17898. mapn = script_getstr(st,2);
  17899. x = script_getnum(st,3);
  17900. y = script_getnum(st,4);
  17901. if( script_hasdata(st,5) )
  17902. instance_id = script_getnum(st,5);
  17903. else
  17904. instance_id = script_instancegetid(st, IM_PARTY);
  17905. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0)
  17906. return SCRIPT_CMD_FAILURE;
  17907. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  17908. if (!idata) {
  17909. ShowError("buildin_instance_warpall: Instance is not found.\n");
  17910. return SCRIPT_CMD_FAILURE;
  17911. }
  17912. for(const auto &it : idata->map)
  17913. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id);
  17914. return SCRIPT_CMD_SUCCESS;
  17915. }
  17916. /*==========================================
  17917. * Broadcasts to all maps inside an instance
  17918. *
  17919. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  17920. * Using 0 for <instance id> will auto-detect the id.
  17921. *------------------------------------------*/
  17922. BUILDIN_FUNC(instance_announce) {
  17923. int instance_id = script_getnum(st,2);
  17924. const char *mes = script_getstr(st,3);
  17925. int flag = script_getnum(st,4);
  17926. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  17927. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  17928. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  17929. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  17930. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  17931. if (instance_id <= 0)
  17932. instance_id = script_instancegetid(st);
  17933. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  17934. if (instance_id <= 0 || !idata) {
  17935. ShowError("buildin_instance_announce: Instance not found.\n");
  17936. return SCRIPT_CMD_FAILURE;
  17937. }
  17938. for (const auto &it : idata->map)
  17939. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  17940. return SCRIPT_CMD_SUCCESS;
  17941. }
  17942. /*==========================================
  17943. * instance_check_party [malufett]
  17944. * Values:
  17945. * party_id : Party ID of the invoking character. [Required Parameter]
  17946. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  17947. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17948. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17949. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  17950. * Example 2: instance_check_party (getcharid(1),1,1,99);
  17951. *------------------------------------------*/
  17952. BUILDIN_FUNC(instance_check_party)
  17953. {
  17954. int amount, min, max, i, party_id, c = 0;
  17955. struct party_data *p;
  17956. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  17957. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17958. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17959. if( min < 1 || min > MAX_LEVEL) {
  17960. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  17961. return SCRIPT_CMD_FAILURE;
  17962. } else if( max < 1 || max > MAX_LEVEL) {
  17963. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  17964. return SCRIPT_CMD_FAILURE;
  17965. }
  17966. if( script_hasdata(st,2) )
  17967. party_id = script_getnum(st,2);
  17968. else return SCRIPT_CMD_FAILURE;
  17969. if( !(p = party_search(party_id)) ) {
  17970. script_pushint(st, 0); // Returns false if party does not exist.
  17971. return SCRIPT_CMD_FAILURE;
  17972. }
  17973. for( i = 0; i < MAX_PARTY; i++ ) {
  17974. struct map_session_data *pl_sd;
  17975. if( (pl_sd = p->data[i].sd) )
  17976. if(map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  17977. if(pl_sd->status.base_level < min) {
  17978. script_pushint(st, 0);
  17979. return SCRIPT_CMD_SUCCESS;
  17980. } else if(pl_sd->status.base_level > max) {
  17981. script_pushint(st, 0);
  17982. return SCRIPT_CMD_SUCCESS;
  17983. }
  17984. c++;
  17985. }
  17986. }
  17987. if(c < amount)
  17988. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  17989. else
  17990. script_pushint(st, 1);
  17991. return SCRIPT_CMD_SUCCESS;
  17992. }
  17993. /*==========================================
  17994. * instance_check_guild
  17995. * Values:
  17996. * guild_id : Guild ID of the invoking character. [Required Parameter]
  17997. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  17998. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17999. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18000. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  18001. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  18002. *------------------------------------------*/
  18003. BUILDIN_FUNC(instance_check_guild)
  18004. {
  18005. int amount, min, max, i, guild_id = 0, c = 0;
  18006. struct guild *g = NULL;
  18007. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18008. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18009. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18010. if (min < 1 || min > MAX_LEVEL) {
  18011. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18012. return SCRIPT_CMD_FAILURE;
  18013. } else if (max < 1 || max > MAX_LEVEL) {
  18014. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18015. return SCRIPT_CMD_FAILURE;
  18016. }
  18017. if (script_hasdata(st,2))
  18018. guild_id = script_getnum(st,2);
  18019. else
  18020. return SCRIPT_CMD_FAILURE;
  18021. if (!(g = guild_search(guild_id))) {
  18022. script_pushint(st, 0); // Returns false if guild does not exist.
  18023. return SCRIPT_CMD_FAILURE;
  18024. }
  18025. for(i = 0; i < MAX_GUILD; i++) {
  18026. struct map_session_data *pl_sd;
  18027. if ((pl_sd = g->member[i].sd)) {
  18028. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18029. if (pl_sd->status.base_level < min) {
  18030. script_pushint(st, 0);
  18031. return SCRIPT_CMD_SUCCESS;
  18032. } else if (pl_sd->status.base_level > max) {
  18033. script_pushint(st, 0);
  18034. return SCRIPT_CMD_SUCCESS;
  18035. }
  18036. c++;
  18037. }
  18038. }
  18039. }
  18040. if (c < amount)
  18041. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18042. else
  18043. script_pushint(st, 1);
  18044. return SCRIPT_CMD_SUCCESS;
  18045. }
  18046. /*==========================================
  18047. * instance_check_clan
  18048. * Values:
  18049. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18050. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18051. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18052. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18053. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18054. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18055. *------------------------------------------*/
  18056. BUILDIN_FUNC(instance_check_clan)
  18057. {
  18058. int amount, min, max, i, clan_id = 0, c = 0;
  18059. struct clan *cd = NULL;
  18060. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18061. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18062. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18063. if (min < 1 || min > MAX_LEVEL) {
  18064. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18065. return SCRIPT_CMD_FAILURE;
  18066. } else if (max < 1 || max > MAX_LEVEL) {
  18067. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18068. return SCRIPT_CMD_FAILURE;
  18069. }
  18070. if (script_hasdata(st,2))
  18071. clan_id = script_getnum(st,2);
  18072. else
  18073. return SCRIPT_CMD_FAILURE;
  18074. if (!(cd = clan_search(clan_id))) {
  18075. script_pushint(st, 0); // Returns false if clan does not exist.
  18076. return SCRIPT_CMD_FAILURE;
  18077. }
  18078. for(i = 0; i < MAX_CLAN; i++) {
  18079. struct map_session_data *pl_sd;
  18080. if ((pl_sd = cd->members[i])) {
  18081. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18082. if (pl_sd->status.base_level < min) {
  18083. script_pushint(st, 0);
  18084. return SCRIPT_CMD_SUCCESS;
  18085. } else if (pl_sd->status.base_level > max) {
  18086. script_pushint(st, 0);
  18087. return SCRIPT_CMD_SUCCESS;
  18088. }
  18089. c++;
  18090. }
  18091. }
  18092. }
  18093. if (c < amount)
  18094. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18095. else
  18096. script_pushint(st, 1);
  18097. return SCRIPT_CMD_SUCCESS;
  18098. }
  18099. /*==========================================
  18100. * instance_info
  18101. * Values:
  18102. * name : name of the instance you want to look up. [Required Parameter]
  18103. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18104. * index : index of the map in the instance. [Optional Parameter]
  18105. *------------------------------------------*/
  18106. BUILDIN_FUNC(instance_info)
  18107. {
  18108. const char* name = script_getstr(st, 2);
  18109. int type = script_getnum(st, 3);
  18110. int index = 0;
  18111. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18112. if (!db) {
  18113. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18114. script_pushint(st, -1);
  18115. return SCRIPT_CMD_FAILURE;
  18116. }
  18117. switch( type ){
  18118. case IIT_ID:
  18119. script_pushint(st, db->id);
  18120. break;
  18121. case IIT_TIME_LIMIT:
  18122. script_pushint(st, db->limit);
  18123. break;
  18124. case IIT_IDLE_TIMEOUT:
  18125. script_pushint(st, db->timeout);
  18126. break;
  18127. case IIT_ENTER_MAP:
  18128. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18129. break;
  18130. case IIT_ENTER_X:
  18131. script_pushint(st, db->enter.x);
  18132. break;
  18133. case IIT_ENTER_Y:
  18134. script_pushint(st, db->enter.y);
  18135. break;
  18136. case IIT_MAPCOUNT:
  18137. script_pushint(st, db->maplist.size());
  18138. break;
  18139. case IIT_MAP:
  18140. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18141. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18142. script_pushconststr(st, "");
  18143. return SCRIPT_CMD_FAILURE;
  18144. }
  18145. index = script_getnum(st, 4);
  18146. if( index < 0 ){
  18147. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18148. script_pushconststr(st, "");
  18149. return SCRIPT_CMD_FAILURE;
  18150. }
  18151. if( index > UINT8_MAX ){
  18152. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18153. script_pushconststr(st, "");
  18154. return SCRIPT_CMD_FAILURE;
  18155. }
  18156. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18157. break;
  18158. default:
  18159. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18160. script_pushint(st, -1);
  18161. return SCRIPT_CMD_FAILURE;
  18162. }
  18163. return SCRIPT_CMD_SUCCESS;
  18164. }
  18165. /*------------------------------------------
  18166. *instance_live_info( <info type>{, <instance id>} );
  18167. - ILI_NAME : Instance Name
  18168. - ILI_MODE : Instance Mode
  18169. - ILI_OWNER : owner id
  18170. *------------------------------------------*/
  18171. BUILDIN_FUNC(instance_live_info)
  18172. {
  18173. int type = script_getnum(st, 2);
  18174. int id = 0;
  18175. if (type < ILI_NAME || type > ILI_OWNER) {
  18176. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18177. script_pushint(st, -1);
  18178. return SCRIPT_CMD_FAILURE;
  18179. }
  18180. if (!script_hasdata(st, 3))
  18181. id = script_instancegetid(st);
  18182. else
  18183. id = script_getnum(st, 3);
  18184. std::shared_ptr<s_instance_db> db = nullptr;
  18185. std::shared_ptr<s_instance_data> im = nullptr;
  18186. if (id > 0 && id < INT_MAX) {
  18187. im = util::umap_find(instances, id);
  18188. if (im)
  18189. db = instance_db.find(im->id);
  18190. }
  18191. if (!im || !db) {
  18192. if (type == ILI_NAME)
  18193. script_pushconststr(st, "");
  18194. else
  18195. script_pushint(st, -1);
  18196. return SCRIPT_CMD_SUCCESS;
  18197. }
  18198. switch( type ) {
  18199. case ILI_NAME:
  18200. script_pushstrcopy(st, db->name.c_str());
  18201. break;
  18202. case ILI_MODE:
  18203. script_pushint(st, im->mode);
  18204. break;
  18205. case ILI_OWNER:
  18206. script_pushint(st, im->owner_id);
  18207. break;
  18208. }
  18209. return SCRIPT_CMD_SUCCESS;
  18210. }
  18211. /*==========================================
  18212. * Custom Fonts
  18213. *------------------------------------------*/
  18214. BUILDIN_FUNC(setfont)
  18215. {
  18216. struct map_session_data *sd;
  18217. int font;
  18218. if( !script_rid2sd(sd) )
  18219. return SCRIPT_CMD_SUCCESS;
  18220. font = script_getnum(st,2);
  18221. if( sd->status.font != font )
  18222. sd->status.font = font;
  18223. else
  18224. sd->status.font = 0;
  18225. clif_font(sd);
  18226. return SCRIPT_CMD_SUCCESS;
  18227. }
  18228. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  18229. {
  18230. TBL_MOB* md = (TBL_MOB*)bl;
  18231. struct block_list *tbl;
  18232. int mobid = va_arg(ap,int);
  18233. uint16 skill_id = va_arg(ap,int);
  18234. uint16 skill_lv = va_arg(ap,int);
  18235. int casttime = va_arg(ap,int);
  18236. int cancel = va_arg(ap,int);
  18237. int emotion = va_arg(ap,int);
  18238. int target = va_arg(ap,int);
  18239. if( md->mob_id != mobid )
  18240. return 0;
  18241. // 0:self, 1:target, 2:master, default:random
  18242. switch( target ) {
  18243. case 0: tbl = map_id2bl(md->bl.id); break;
  18244. case 1: tbl = map_id2bl(md->target_id); break;
  18245. case 2: tbl = map_id2bl(md->master_id); break;
  18246. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  18247. }
  18248. if( !tbl )
  18249. return 0;
  18250. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  18251. unit_skillcastcancel(bl,0);
  18252. if( skill_get_casttype(skill_id) == CAST_GROUND )
  18253. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  18254. else
  18255. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  18256. clif_emotion(&md->bl, emotion);
  18257. return 1;
  18258. }
  18259. /*==========================================
  18260. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  18261. *------------------------------------------*/
  18262. BUILDIN_FUNC(areamobuseskill)
  18263. {
  18264. struct block_list center;
  18265. int16 m;
  18266. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  18267. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  18268. ShowError("areamobuseskill: invalid map name.\n");
  18269. return SCRIPT_CMD_SUCCESS;
  18270. }
  18271. center.m = m;
  18272. center.x = script_getnum(st,3);
  18273. center.y = script_getnum(st,4);
  18274. range = script_getnum(st,5);
  18275. mobid = script_getnum(st,6);
  18276. if (script_isstring(st, 7)) {
  18277. const char *name = script_getstr(st, 7);
  18278. if (!(skill_id = skill_name2id(name))) {
  18279. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  18280. return SCRIPT_CMD_FAILURE;
  18281. }
  18282. } else {
  18283. skill_id = script_getnum(st, 7);
  18284. if (!skill_get_index(skill_id)) {
  18285. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  18286. return SCRIPT_CMD_FAILURE;
  18287. }
  18288. }
  18289. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  18290. skill_lv = battle_config.mob_max_skilllvl;
  18291. casttime = script_getnum(st,9);
  18292. cancel = script_getnum(st,10);
  18293. emotion = script_getnum(st,11);
  18294. target = script_getnum(st,12);
  18295. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  18296. return SCRIPT_CMD_SUCCESS;
  18297. }
  18298. /**
  18299. * Display a progress bar above a character
  18300. * progressbar "<color>",<seconds>;
  18301. */
  18302. BUILDIN_FUNC(progressbar)
  18303. {
  18304. struct map_session_data * sd;
  18305. const char * color;
  18306. unsigned int second;
  18307. if( !script_rid2sd(sd) )
  18308. return SCRIPT_CMD_SUCCESS;
  18309. st->state = STOP;
  18310. color = script_getstr(st,2);
  18311. second = script_getnum(st,3);
  18312. sd->progressbar.npc_id = st->oid;
  18313. sd->progressbar.timeout = gettick() + second*1000;
  18314. sd->state.workinprogress = WIP_DISABLE_ALL;
  18315. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  18316. return SCRIPT_CMD_SUCCESS;
  18317. }
  18318. /**
  18319. * Display a progress bar above an NPC
  18320. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  18321. */
  18322. BUILDIN_FUNC(progressbar_npc){
  18323. map_session_data *sd = map_id2sd(st->rid);
  18324. struct npc_data* nd = NULL;
  18325. if( script_hasdata(st, 4) ){
  18326. const char* name = script_getstr(st, 4);
  18327. nd = npc_name2id(name);
  18328. if( !nd ){
  18329. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  18330. return SCRIPT_CMD_FAILURE;
  18331. }
  18332. }else{
  18333. nd = map_id2nd(st->oid);
  18334. }
  18335. // First call(by function call)
  18336. if( !nd->progressbar.timeout ){
  18337. const char *color;
  18338. int second;
  18339. color = script_getstr(st, 2);
  18340. second = script_getnum(st, 3);
  18341. if( second < 0 ){
  18342. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  18343. return SCRIPT_CMD_FAILURE;
  18344. }
  18345. if (sd) { // Player attached - keep them from doing other things
  18346. sd->state.workinprogress = WIP_DISABLE_ALL;
  18347. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18348. }
  18349. // sleep for the target amount of time
  18350. st->state = RERUNLINE;
  18351. st->sleep.tick = second * 1000;
  18352. nd->progressbar.timeout = gettick() + second * 1000;
  18353. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  18354. clif_progressbar_npc_area(nd);
  18355. // Second call(by timer after sleeping time is over)
  18356. } else {
  18357. // Continue the script
  18358. if (sd) { // Player attached - remove restrictions
  18359. sd->state.workinprogress = WIP_DISABLE_NONE;
  18360. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18361. }
  18362. st->state = RUN;
  18363. st->sleep.tick = 0;
  18364. nd->progressbar.timeout = nd->progressbar.color = 0;
  18365. }
  18366. return SCRIPT_CMD_SUCCESS;
  18367. }
  18368. BUILDIN_FUNC(pushpc)
  18369. {
  18370. uint8 dir;
  18371. int cells, dx, dy;
  18372. struct map_session_data* sd;
  18373. if(!script_rid2sd(sd))
  18374. {
  18375. return SCRIPT_CMD_SUCCESS;
  18376. }
  18377. dir = script_getnum(st,2);
  18378. cells = script_getnum(st,3);
  18379. if(dir >= DIR_MAX)
  18380. {
  18381. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  18382. script_reportsrc(st);
  18383. dir%= DIR_MAX; // trim spin-over
  18384. }
  18385. if(!cells)
  18386. {// zero distance
  18387. return SCRIPT_CMD_SUCCESS;
  18388. }
  18389. else if(cells<0)
  18390. {// pushing backwards
  18391. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  18392. cells = -cells;
  18393. }
  18394. dx = dirx[dir];
  18395. dy = diry[dir];
  18396. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  18397. return SCRIPT_CMD_SUCCESS;
  18398. }
  18399. /// Invokes buying store preparation window
  18400. /// buyingstore <slots>;
  18401. BUILDIN_FUNC(buyingstore)
  18402. {
  18403. struct map_session_data* sd;
  18404. if( !script_rid2sd(sd) )
  18405. {
  18406. return SCRIPT_CMD_SUCCESS;
  18407. }
  18408. if( npc_isnear(&sd->bl) ) {
  18409. char output[150];
  18410. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  18411. clif_displaymessage(sd->fd, output);
  18412. return SCRIPT_CMD_SUCCESS;
  18413. }
  18414. buyingstore_setup(sd, script_getnum(st,2));
  18415. return SCRIPT_CMD_SUCCESS;
  18416. }
  18417. /// Invokes search store info window
  18418. /// searchstores <uses>,<effect>;
  18419. BUILDIN_FUNC(searchstores)
  18420. {
  18421. unsigned short effect;
  18422. unsigned int uses;
  18423. struct map_session_data* sd;
  18424. if( !script_rid2sd(sd) )
  18425. {
  18426. return SCRIPT_CMD_SUCCESS;
  18427. }
  18428. uses = script_getnum(st,2);
  18429. effect = script_getnum(st,3);
  18430. if( !uses )
  18431. {
  18432. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  18433. return SCRIPT_CMD_FAILURE;
  18434. }
  18435. if( effect > 1 )
  18436. {
  18437. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  18438. return SCRIPT_CMD_FAILURE;
  18439. }
  18440. searchstore_open(sd, uses, effect);
  18441. return SCRIPT_CMD_SUCCESS;
  18442. }
  18443. /// Displays a number as large digital clock.
  18444. /// showdigit <value>[,<type>];
  18445. BUILDIN_FUNC(showdigit)
  18446. {
  18447. unsigned int type = 0;
  18448. int value;
  18449. struct map_session_data* sd;
  18450. if( !script_rid2sd(sd) )
  18451. {
  18452. return SCRIPT_CMD_SUCCESS;
  18453. }
  18454. value = script_getnum(st,2);
  18455. if( script_hasdata(st,3) ){
  18456. type = script_getnum(st,3);
  18457. }
  18458. switch( type ){
  18459. case 0:
  18460. break;
  18461. case 1:
  18462. case 2:
  18463. // Use absolute value and then the negative value of it as starting value
  18464. // This is what gravity's client does for these counters
  18465. value = -abs(value);
  18466. break;
  18467. case 3:
  18468. value = abs(value);
  18469. if( value > 99 ){
  18470. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  18471. script_reportsrc(st);
  18472. value = 99;
  18473. }
  18474. break;
  18475. default:
  18476. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  18477. return SCRIPT_CMD_FAILURE;
  18478. }
  18479. clif_showdigit(sd, (unsigned char)type, value);
  18480. return SCRIPT_CMD_SUCCESS;
  18481. }
  18482. /**
  18483. * Rune Knight
  18484. * makerune <% success bonus>{,<char_id>};
  18485. **/
  18486. BUILDIN_FUNC(makerune) {
  18487. TBL_PC* sd;
  18488. if (!script_charid2sd(3,sd))
  18489. return SCRIPT_CMD_FAILURE;
  18490. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  18491. sd->itemid = script_getnum(st,2);
  18492. return SCRIPT_CMD_SUCCESS;
  18493. }
  18494. /**
  18495. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  18496. **/
  18497. BUILDIN_FUNC(checkdragon) {
  18498. TBL_PC* sd;
  18499. if (!script_charid2sd(2,sd))
  18500. return SCRIPT_CMD_FAILURE;
  18501. if( pc_isridingdragon(sd) )
  18502. script_pushint(st,1);
  18503. else
  18504. script_pushint(st,0);
  18505. return SCRIPT_CMD_SUCCESS;
  18506. }
  18507. /**
  18508. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  18509. * - Toggles the dragon on a RK if he can mount;
  18510. * @param Color - when not provided uses the green dragon;
  18511. * - 1 : Green Dragon
  18512. * - 2 : Brown Dragon
  18513. * - 3 : Gray Dragon
  18514. * - 4 : Blue Dragon
  18515. * - 5 : Red Dragon
  18516. **/
  18517. BUILDIN_FUNC(setdragon) {
  18518. TBL_PC* sd;
  18519. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  18520. if (!script_charid2sd(3,sd))
  18521. return SCRIPT_CMD_FAILURE;
  18522. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  18523. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  18524. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  18525. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  18526. script_pushint(st,1);
  18527. } else {//Not mounted; Mount now.
  18528. unsigned int option = OPTION_DRAGON1;
  18529. if( color ) {
  18530. option = ( color == 1 ? OPTION_DRAGON1 :
  18531. color == 2 ? OPTION_DRAGON2 :
  18532. color == 3 ? OPTION_DRAGON3 :
  18533. color == 4 ? OPTION_DRAGON4 :
  18534. color == 5 ? OPTION_DRAGON5 : 0);
  18535. if( !option ) {
  18536. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  18537. option = OPTION_DRAGON1;
  18538. }
  18539. }
  18540. pc_setoption(sd, sd->sc.option|option);
  18541. script_pushint(st,1);
  18542. }
  18543. return SCRIPT_CMD_SUCCESS;
  18544. }
  18545. /**
  18546. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  18547. **/
  18548. BUILDIN_FUNC(ismounting) {
  18549. TBL_PC* sd;
  18550. if (!script_charid2sd(2,sd))
  18551. return SCRIPT_CMD_FAILURE;
  18552. if( sd->sc.data[SC_ALL_RIDING] )
  18553. script_pushint(st,1);
  18554. else
  18555. script_pushint(st,0);
  18556. return SCRIPT_CMD_SUCCESS;
  18557. }
  18558. /**
  18559. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  18560. * - Toggles new mounts on a player when he can mount
  18561. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  18562. * - Will unmount the player is he is already mounting
  18563. **/
  18564. BUILDIN_FUNC(setmounting) {
  18565. TBL_PC* sd;
  18566. if (!script_charid2sd(2,sd))
  18567. return SCRIPT_CMD_FAILURE;
  18568. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  18569. clif_msg(sd, NEED_REINS_OF_MOUNT);
  18570. script_pushint(st,0); //can't mount with one of these
  18571. } 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]) {
  18572. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  18573. script_pushint(st, 0); // Silent failure
  18574. } else {
  18575. if( sd->sc.data[SC_ALL_RIDING] )
  18576. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  18577. else
  18578. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  18579. script_pushint(st,1);//in both cases, return 1.
  18580. }
  18581. return SCRIPT_CMD_SUCCESS;
  18582. }
  18583. /**
  18584. * Retrieves quantity of arguments provided to callfunc/callsub.
  18585. * getargcount() -> amount of arguments received in a function
  18586. **/
  18587. BUILDIN_FUNC(getargcount) {
  18588. struct script_retinfo* ri;
  18589. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  18590. ShowError("script:getargcount: used out of function or callsub label!\n");
  18591. st->state = END;
  18592. return SCRIPT_CMD_FAILURE;
  18593. }
  18594. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  18595. script_pushint(st, ri->nargs);
  18596. return SCRIPT_CMD_SUCCESS;
  18597. }
  18598. /**
  18599. * getcharip(<account ID>/<character ID>/<character name>)
  18600. **/
  18601. BUILDIN_FUNC(getcharip)
  18602. {
  18603. struct map_session_data* sd = NULL;
  18604. /* check if a character name is specified */
  18605. if( script_hasdata(st, 2) )
  18606. {
  18607. if (script_isstring(st, 2))
  18608. sd = map_nick2sd(script_getstr(st, 2),false);
  18609. else
  18610. {
  18611. int id = script_getnum(st, 2);
  18612. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  18613. }
  18614. }
  18615. else
  18616. script_rid2sd(sd);
  18617. /* check for sd and IP */
  18618. if (!sd || !session[sd->fd]->client_addr)
  18619. {
  18620. script_pushconststr(st, "");
  18621. return SCRIPT_CMD_SUCCESS;
  18622. }
  18623. /* return the client ip_addr converted for output */
  18624. if (sd && sd->fd && session[sd->fd])
  18625. {
  18626. /* initiliaze */
  18627. const char *ip_addr = NULL;
  18628. uint32 ip;
  18629. /* set ip, ip_addr and convert to ip and push str */
  18630. ip = session[sd->fd]->client_addr;
  18631. ip_addr = ip2str(ip, NULL);
  18632. script_pushstrcopy(st, ip_addr);
  18633. }
  18634. return SCRIPT_CMD_SUCCESS;
  18635. }
  18636. /**
  18637. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  18638. **/
  18639. BUILDIN_FUNC(is_function) {
  18640. const char* str = script_getstr(st,2);
  18641. if( strdb_exists(userfunc_db, str) )
  18642. script_pushint(st,1);
  18643. else
  18644. script_pushint(st,0);
  18645. return SCRIPT_CMD_SUCCESS;
  18646. }
  18647. /**
  18648. * get_revision() -> retrieves the current svn revision (if available)
  18649. **/
  18650. BUILDIN_FUNC(get_revision) {
  18651. const char *svn = get_svn_revision();
  18652. if ( svn[0] != UNKNOWN_VERSION )
  18653. script_pushint(st,atoi(svn));
  18654. else
  18655. script_pushint(st,-1); //unknown
  18656. return SCRIPT_CMD_SUCCESS;
  18657. }
  18658. /* get_hash() -> retrieves the current git hash (if available)*/
  18659. BUILDIN_FUNC(get_githash) {
  18660. const char* git = get_git_hash();
  18661. char buf[CHAT_SIZE_MAX];
  18662. safestrncpy(buf,git,strlen(git)+1);
  18663. if ( git[0] != UNKNOWN_VERSION )
  18664. script_pushstrcopy(st,buf);
  18665. else
  18666. script_pushconststr(st,"Unknown"); //unknown
  18667. return SCRIPT_CMD_SUCCESS;
  18668. }
  18669. /**
  18670. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  18671. **/
  18672. BUILDIN_FUNC(freeloop) {
  18673. if( script_hasdata(st,2) ) {
  18674. if( script_getnum(st,2) )
  18675. st->freeloop = 1;
  18676. else
  18677. st->freeloop = 0;
  18678. }
  18679. script_pushint(st, st->freeloop);
  18680. return SCRIPT_CMD_SUCCESS;
  18681. }
  18682. /**
  18683. * @commands (script based)
  18684. **/
  18685. BUILDIN_FUNC(bindatcmd) {
  18686. const char* atcmd;
  18687. const char* eventName;
  18688. int i, level = 0, level2 = 100;
  18689. bool create = false;
  18690. atcmd = script_getstr(st,2);
  18691. eventName = script_getstr(st,3);
  18692. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18693. atcmd++;
  18694. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  18695. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  18696. if( atcmd_binding_count == 0 ) {
  18697. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  18698. create = true;
  18699. } else {
  18700. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  18701. if( i < atcmd_binding_count ) {/* update existent entry */
  18702. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18703. atcmd_binding[i]->level = level;
  18704. atcmd_binding[i]->level2 = level2;
  18705. } else
  18706. create = true;
  18707. }
  18708. if( create ) {
  18709. i = atcmd_binding_count;
  18710. if( atcmd_binding_count++ != 0 )
  18711. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  18712. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  18713. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  18714. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18715. atcmd_binding[i]->level = level;
  18716. atcmd_binding[i]->level2 = level2;
  18717. }
  18718. return SCRIPT_CMD_SUCCESS;
  18719. }
  18720. BUILDIN_FUNC(unbindatcmd) {
  18721. const char* atcmd;
  18722. int i = 0;
  18723. atcmd = script_getstr(st, 2);
  18724. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18725. atcmd++;
  18726. if( atcmd_binding_count == 0 ) {
  18727. script_pushint(st, 0);
  18728. return SCRIPT_CMD_SUCCESS;
  18729. }
  18730. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  18731. if( i < atcmd_binding_count ) {
  18732. int cursor = 0;
  18733. aFree(atcmd_binding[i]);
  18734. atcmd_binding[i] = NULL;
  18735. /* compact the list now that we freed a slot somewhere */
  18736. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  18737. if( atcmd_binding[i] == NULL )
  18738. continue;
  18739. if( cursor != i ) {
  18740. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  18741. }
  18742. cursor++;
  18743. }
  18744. if( (atcmd_binding_count = cursor) == 0 )
  18745. aFree(atcmd_binding);
  18746. script_pushint(st, 1);
  18747. } else
  18748. script_pushint(st, 0);/* not found */
  18749. return SCRIPT_CMD_SUCCESS;
  18750. }
  18751. BUILDIN_FUNC(useatcmd) {
  18752. return atcommand_sub(st,3);
  18753. }
  18754. BUILDIN_FUNC(checkre)
  18755. {
  18756. int num;
  18757. num=script_getnum(st,2);
  18758. switch(num){
  18759. case 0:
  18760. #ifdef RENEWAL
  18761. script_pushint(st, 1);
  18762. #else
  18763. script_pushint(st, 0);
  18764. #endif
  18765. break;
  18766. case 1:
  18767. #ifdef RENEWAL_CAST
  18768. script_pushint(st, 1);
  18769. #else
  18770. script_pushint(st, 0);
  18771. #endif
  18772. break;
  18773. case 2:
  18774. #ifdef RENEWAL_DROP
  18775. script_pushint(st, 1);
  18776. #else
  18777. script_pushint(st, 0);
  18778. #endif
  18779. break;
  18780. case 3:
  18781. #ifdef RENEWAL_EXP
  18782. script_pushint(st, 1);
  18783. #else
  18784. script_pushint(st, 0);
  18785. #endif
  18786. break;
  18787. case 4:
  18788. #ifdef RENEWAL_LVDMG
  18789. script_pushint(st, 1);
  18790. #else
  18791. script_pushint(st, 0);
  18792. #endif
  18793. break;
  18794. case 5:
  18795. #ifdef RENEWAL_ASPD
  18796. script_pushint(st, 1);
  18797. #else
  18798. script_pushint(st, 0);
  18799. #endif
  18800. break;
  18801. default:
  18802. ShowWarning("buildin_checkre: unknown parameter.\n");
  18803. break;
  18804. }
  18805. return SCRIPT_CMD_SUCCESS;
  18806. }
  18807. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  18808. BUILDIN_FUNC(getrandgroupitem) {
  18809. TBL_PC* sd;
  18810. int i, get_count = 0, identify = 0;
  18811. uint16 group, qty = 0;
  18812. uint8 sub_group = 1;
  18813. struct item item_tmp;
  18814. struct s_item_group_entry *entry = NULL;
  18815. if (!script_charid2sd(6, sd))
  18816. return SCRIPT_CMD_SUCCESS;
  18817. group = script_getnum(st,2);
  18818. if (!group) {
  18819. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  18820. return SCRIPT_CMD_FAILURE;
  18821. }
  18822. FETCH(3, qty);
  18823. FETCH(4, sub_group);
  18824. FETCH(5, identify);
  18825. entry = itemdb_get_randgroupitem(group,sub_group);
  18826. if (!entry)
  18827. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  18828. memset(&item_tmp,0,sizeof(item_tmp));
  18829. item_tmp.nameid = entry->nameid;
  18830. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  18831. if (!qty)
  18832. qty = entry->amount;
  18833. //Check if it's stackable.
  18834. if (!itemdb_isstackable(entry->nameid)) {
  18835. item_tmp.amount = 1;
  18836. get_count = qty;
  18837. }
  18838. else {
  18839. item_tmp.amount = qty;
  18840. get_count = 1;
  18841. }
  18842. for (i = 0; i < get_count; i++) {
  18843. // if not pet egg
  18844. if (!pet_create_egg(sd, entry->nameid)) {
  18845. unsigned char flag = 0;
  18846. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  18847. clif_additem(sd,0,0,flag);
  18848. if (pc_candrop(sd,&item_tmp))
  18849. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  18850. }
  18851. }
  18852. }
  18853. return SCRIPT_CMD_SUCCESS;
  18854. }
  18855. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  18856. * Gives item(s) to the attached player based on item group contents
  18857. */
  18858. BUILDIN_FUNC(getgroupitem) {
  18859. TBL_PC *sd;
  18860. int group_id = script_getnum(st,2);
  18861. if (!script_charid2sd(4,sd))
  18862. return SCRIPT_CMD_SUCCESS;
  18863. if (itemdb_pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  18864. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  18865. return SCRIPT_CMD_FAILURE;
  18866. }
  18867. return SCRIPT_CMD_SUCCESS;
  18868. }
  18869. /* cleanmap <map_name>;
  18870. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  18871. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  18872. {
  18873. nullpo_ret(bl);
  18874. map_clearflooritem(bl);
  18875. return SCRIPT_CMD_SUCCESS;
  18876. }
  18877. BUILDIN_FUNC(cleanmap)
  18878. {
  18879. const char *mapname;
  18880. int16 m;
  18881. mapname = script_getstr(st, 2);
  18882. m = map_mapname2mapid(mapname);
  18883. if (!m)
  18884. return SCRIPT_CMD_FAILURE;
  18885. if ((script_lastdata(st) - 2) < 4) {
  18886. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  18887. } else {
  18888. int16 x0 = script_getnum(st, 3);
  18889. int16 y0 = script_getnum(st, 4);
  18890. int16 x1 = script_getnum(st, 5);
  18891. int16 y1 = script_getnum(st, 6);
  18892. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  18893. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  18894. } else {
  18895. ShowError("cleanarea: invalid coordinate defined!\n");
  18896. return SCRIPT_CMD_FAILURE;
  18897. }
  18898. }
  18899. return SCRIPT_CMD_SUCCESS;
  18900. }
  18901. /* Cast a skill on the attached player.
  18902. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  18903. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  18904. BUILDIN_FUNC(npcskill)
  18905. {
  18906. uint16 skill_id;
  18907. unsigned short skill_level;
  18908. unsigned int stat_point;
  18909. unsigned int npc_level;
  18910. struct npc_data *nd;
  18911. struct map_session_data *sd;
  18912. if( !script_rid2sd(sd) )
  18913. return SCRIPT_CMD_SUCCESS;
  18914. if (script_isstring(st, 2)) {
  18915. const char *name = script_getstr(st, 2);
  18916. if (!(skill_id = skill_name2id(name))) {
  18917. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  18918. return SCRIPT_CMD_FAILURE;
  18919. }
  18920. } else {
  18921. skill_id = script_getnum(st, 2);
  18922. if (!skill_get_index(skill_id)) {
  18923. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  18924. return SCRIPT_CMD_FAILURE;
  18925. }
  18926. }
  18927. skill_level = script_getnum(st, 3);
  18928. stat_point = script_getnum(st, 4);
  18929. npc_level = script_getnum(st, 5);
  18930. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  18931. if (stat_point > battle_config.max_third_parameter) {
  18932. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  18933. return SCRIPT_CMD_FAILURE;
  18934. }
  18935. if (npc_level > MAX_LEVEL) {
  18936. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  18937. return SCRIPT_CMD_FAILURE;
  18938. }
  18939. if (nd == NULL) { //ain't possible, but I don't trust people.
  18940. return SCRIPT_CMD_FAILURE;
  18941. }
  18942. nd->level = npc_level;
  18943. nd->stat_point = stat_point;
  18944. if (!nd->status.hp)
  18945. status_calc_npc(nd, SCO_FIRST);
  18946. else
  18947. status_calc_npc(nd, SCO_NONE);
  18948. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  18949. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  18950. else
  18951. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  18952. return SCRIPT_CMD_SUCCESS;
  18953. }
  18954. /* Consumes an item.
  18955. * consumeitem <item id>{,<char_id>};
  18956. * consumeitem "<item name>"{,<char_id>};
  18957. * @param item: Item ID or name
  18958. */
  18959. BUILDIN_FUNC(consumeitem)
  18960. {
  18961. TBL_PC *sd;
  18962. struct item_data *item_data;
  18963. if (!script_charid2sd(3, sd))
  18964. return SCRIPT_CMD_FAILURE;
  18965. if( script_isstring(st, 2) ){
  18966. const char *name = script_getstr(st, 2);
  18967. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  18968. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  18969. return SCRIPT_CMD_FAILURE;
  18970. }
  18971. } else {
  18972. t_itemid nameid = script_getnum(st, 2);
  18973. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  18974. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  18975. return SCRIPT_CMD_FAILURE;
  18976. }
  18977. }
  18978. run_script( item_data->script, 0, sd->bl.id, 0 );
  18979. return SCRIPT_CMD_SUCCESS;
  18980. }
  18981. /** Makes a player sit/stand.
  18982. * sit {"<character name>"};
  18983. * stand {"<character name>"};
  18984. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  18985. * @param name: Player name that will be invoked
  18986. */
  18987. BUILDIN_FUNC(sit)
  18988. {
  18989. TBL_PC *sd;
  18990. if( !script_nick2sd(2,sd) )
  18991. return SCRIPT_CMD_FAILURE;
  18992. if( !pc_issit(sd) ) {
  18993. pc_setsit(sd);
  18994. skill_sit(sd, 1);
  18995. clif_sitting(&sd->bl);
  18996. }
  18997. return SCRIPT_CMD_SUCCESS;
  18998. }
  18999. /** Makes player to stand
  19000. * @param name: Player name that will be set
  19001. */
  19002. BUILDIN_FUNC(stand)
  19003. {
  19004. TBL_PC *sd;
  19005. if( !script_nick2sd(2,sd) )
  19006. return SCRIPT_CMD_FAILURE;
  19007. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19008. skill_sit(sd, 0);
  19009. clif_standing(&sd->bl);
  19010. }
  19011. return SCRIPT_CMD_SUCCESS;
  19012. }
  19013. /** Creates an array of bounded item IDs
  19014. * countbound {<type>{,<char_id>}};
  19015. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19016. * @return amt: Total number of different items type found
  19017. */
  19018. BUILDIN_FUNC(countbound)
  19019. {
  19020. TBL_PC *sd;
  19021. if (!script_charid2sd(3,sd))
  19022. return SCRIPT_CMD_FAILURE;
  19023. int i, k = 0;
  19024. int type = script_getnum(st,2);
  19025. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19026. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19027. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19028. ))
  19029. {
  19030. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19031. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19032. k++;
  19033. }
  19034. }
  19035. script_pushint(st,k);
  19036. return SCRIPT_CMD_SUCCESS;
  19037. }
  19038. /** Creates new party
  19039. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19040. * @param party_name: String of party name that will be created
  19041. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19042. * @param item_share: 0-Each Take. 1-Party Share
  19043. * @param item_share_type: 0-Each Take. 1-Even Share
  19044. * @return val: Result value
  19045. * -3 - party name is exist
  19046. * -2 - player is in party already
  19047. * -1 - player is not found
  19048. * 0 - unknown error
  19049. * 1 - success, will return party id $@party_create_id
  19050. */
  19051. BUILDIN_FUNC(party_create)
  19052. {
  19053. char party_name[NAME_LENGTH];
  19054. int item1 = 0, item2 = 0;
  19055. TBL_PC *sd = NULL;
  19056. if (!script_charid2sd(3, sd)) {
  19057. script_pushint(st,-1);
  19058. return SCRIPT_CMD_FAILURE;
  19059. }
  19060. if( sd->status.party_id ) {
  19061. script_pushint(st,-2);
  19062. return SCRIPT_CMD_FAILURE;
  19063. }
  19064. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19065. trim(party_name);
  19066. if( party_searchname(party_name) ) {
  19067. script_pushint(st,-3);
  19068. return SCRIPT_CMD_FAILURE;
  19069. }
  19070. if( script_getnum(st,4) )
  19071. item1 = 1;
  19072. if( script_getnum(st,5) )
  19073. item2 = 1;
  19074. party_create_byscript = 1;
  19075. script_pushint(st,party_create(sd,party_name,item1,item2));
  19076. return SCRIPT_CMD_SUCCESS;
  19077. }
  19078. /** Adds player to specified party
  19079. * party_addmember <party id>,<char id>;
  19080. * @param party_id: The party that will be entered by player
  19081. * @param char_id: Char id of player that will be joined to the party
  19082. * @return val: Result value
  19083. * -5 - another character of the same account is in the party
  19084. * -4 - party is full
  19085. * -3 - party is not found
  19086. * -2 - player is in party already
  19087. * -1 - player is not found
  19088. * 0 - unknown error
  19089. * 1 - success
  19090. */
  19091. BUILDIN_FUNC(party_addmember)
  19092. {
  19093. int party_id = script_getnum(st,2);
  19094. TBL_PC *sd;
  19095. struct party_data *party;
  19096. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19097. script_pushint(st,-1);
  19098. return SCRIPT_CMD_FAILURE;
  19099. }
  19100. if( sd->status.party_id ) {
  19101. script_pushint(st,-2);
  19102. return SCRIPT_CMD_FAILURE;
  19103. }
  19104. if( !(party = party_search(party_id)) ) {
  19105. script_pushint(st,-3);
  19106. return SCRIPT_CMD_FAILURE;
  19107. }
  19108. if (battle_config.block_account_in_same_party) {
  19109. int i;
  19110. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19111. if (i < MAX_PARTY) {
  19112. script_pushint(st,-5);
  19113. return SCRIPT_CMD_FAILURE;
  19114. }
  19115. }
  19116. if( party->party.count >= MAX_PARTY ) {
  19117. script_pushint(st,-4);
  19118. return SCRIPT_CMD_FAILURE;
  19119. }
  19120. sd->party_invite = party_id;
  19121. script_pushint(st,party_add_member(party_id,sd));
  19122. return SCRIPT_CMD_SUCCESS;
  19123. }
  19124. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19125. * party_delmember {<char id>,<party_id>};
  19126. * @param: char_id
  19127. * @param: party_id
  19128. * @return val: Result value
  19129. * -3 - player is not in party
  19130. * -2 - party is not found
  19131. * -1 - player is not found
  19132. * 0 - unknown error
  19133. * 1 - success
  19134. */
  19135. BUILDIN_FUNC(party_delmember)
  19136. {
  19137. TBL_PC *sd = NULL;
  19138. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19139. script_pushint(st,-1);
  19140. return SCRIPT_CMD_FAILURE;
  19141. }
  19142. if( sd || script_charid2sd(2,sd) )
  19143. script_pushint(st,party_removemember2(sd,0,0));
  19144. else
  19145. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  19146. return SCRIPT_CMD_SUCCESS;
  19147. }
  19148. /** Changes party leader of specified party (even the leader is offline)
  19149. * party_changeleader <party id>,<char id>;
  19150. * @param party_id: ID of party
  19151. * @param char_id: Char ID of new leader
  19152. * @return val: Result value
  19153. * -4 - player is party leader already
  19154. * -3 - player is not in this party
  19155. * -2 - player is not found
  19156. * -1 - party is not found
  19157. * 0 - unknown error
  19158. * 1 - success */
  19159. BUILDIN_FUNC(party_changeleader)
  19160. {
  19161. int i, party_id = script_getnum(st,2);
  19162. TBL_PC *sd = NULL;
  19163. TBL_PC *tsd = NULL;
  19164. struct party_data *party = NULL;
  19165. if( !(party = party_search(party_id)) ) {
  19166. script_pushint(st,-1);
  19167. return SCRIPT_CMD_FAILURE;
  19168. }
  19169. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19170. script_pushint(st,-2);
  19171. return SCRIPT_CMD_FAILURE;
  19172. }
  19173. if( tsd->status.party_id != party_id ) {
  19174. script_pushint(st,-3);
  19175. return SCRIPT_CMD_FAILURE;
  19176. }
  19177. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19178. if( i >= MAX_PARTY ) { //this is should impossible!
  19179. script_pushint(st,0);
  19180. return SCRIPT_CMD_FAILURE;
  19181. }
  19182. if( party->data[i].sd == tsd ) {
  19183. script_pushint(st,-4);
  19184. return SCRIPT_CMD_FAILURE;
  19185. }
  19186. script_pushint(st,party_changeleader(sd,tsd,party));
  19187. return SCRIPT_CMD_SUCCESS;
  19188. }
  19189. /** Changes party option
  19190. * party_changeoption <party id>,<option>,<flag>;
  19191. * @param party_id: ID of party that will be changed
  19192. * @param option: Type of option
  19193. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  19194. */
  19195. BUILDIN_FUNC(party_changeoption)
  19196. {
  19197. struct party_data *party;
  19198. if( !(party = party_search(script_getnum(st,2))) ) {
  19199. script_pushint(st,-1);
  19200. return SCRIPT_CMD_FAILURE;
  19201. }
  19202. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  19203. return SCRIPT_CMD_SUCCESS;
  19204. }
  19205. /** Destroys party with party id.
  19206. * party_destroy <party id>;
  19207. * @param party_id: ID of party that will be destroyed
  19208. */
  19209. BUILDIN_FUNC(party_destroy)
  19210. {
  19211. int i;
  19212. struct party_data *party;
  19213. if( !(party = party_search(script_getnum(st,2))) ) {
  19214. script_pushint(st,0);
  19215. return SCRIPT_CMD_FAILURE;
  19216. }
  19217. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19218. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  19219. int j;
  19220. for( j = 0; j < MAX_PARTY; j++ ) {
  19221. TBL_PC *sd = party->data[j].sd;
  19222. if(sd)
  19223. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  19224. else if( party->party.member[j].char_id )
  19225. 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);
  19226. }
  19227. party_broken(party->party.party_id);
  19228. script_pushint(st,1);
  19229. }
  19230. else //leader leave = party broken
  19231. script_pushint(st,party_leave(party->data[i].sd));
  19232. return SCRIPT_CMD_SUCCESS;
  19233. }
  19234. /** Returns various information about a player's VIP status. Need to enable VIP system
  19235. * vip_status <type>,{"<character name>"};
  19236. * @param type: Info type, see enum vip_status_type
  19237. * @param name: Character name (Optional)
  19238. */
  19239. BUILDIN_FUNC(vip_status) {
  19240. #ifdef VIP_ENABLE
  19241. TBL_PC *sd;
  19242. int type;
  19243. if( !script_nick2sd(3,sd) )
  19244. return SCRIPT_CMD_FAILURE;
  19245. type = script_getnum(st, 2);
  19246. switch(type) {
  19247. case VIP_STATUS_ACTIVE: // Get VIP status.
  19248. script_pushint(st, pc_isvip(sd));
  19249. break;
  19250. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  19251. if (pc_isvip(sd)) {
  19252. script_pushint(st, sd->vip.time);
  19253. } else
  19254. script_pushint(st, 0);
  19255. break;
  19256. case VIP_STATUS_REMAINING: // Get remaining time.
  19257. if (pc_isvip(sd)) {
  19258. script_pushint(st, sd->vip.time - time(NULL));
  19259. } else
  19260. script_pushint(st, 0);
  19261. break;
  19262. default:
  19263. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  19264. return SCRIPT_CMD_FAILURE;
  19265. }
  19266. #else
  19267. script_pushint(st, 0);
  19268. #endif
  19269. return SCRIPT_CMD_SUCCESS;
  19270. }
  19271. /** Adds or removes VIP time in minutes. Need to enable VIP system
  19272. * vip_time <time in mn>,{"<character name>"};
  19273. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  19274. * @param name: Character name (optional)
  19275. */
  19276. BUILDIN_FUNC(vip_time) {
  19277. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  19278. TBL_PC *sd;
  19279. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  19280. if( !script_nick2sd(3,sd) )
  19281. return SCRIPT_CMD_FAILURE;
  19282. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  19283. #endif
  19284. return SCRIPT_CMD_SUCCESS;
  19285. }
  19286. /**
  19287. * Turns a player into a monster and optionally can grant a SC attribute effect.
  19288. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19289. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19290. * @param monster: Monster ID or name
  19291. * @param duration: Transform duration in millisecond (ms)
  19292. * @param sc_type: Type of SC that will be affected during the transformation
  19293. * @param val1: Value for SC
  19294. * @param val2: Value for SC
  19295. * @param val3: Value for SC
  19296. * @param val4: Value for SC
  19297. * @author: malufett
  19298. */
  19299. BUILDIN_FUNC(montransform) {
  19300. TBL_PC *sd;
  19301. enum sc_type type;
  19302. int tick, mob_id, val1, val2, val3, val4;
  19303. val1 = val2 = val3 = val4 = 0;
  19304. if( !script_rid2sd(sd) )
  19305. return SCRIPT_CMD_FAILURE;
  19306. if( script_isstring(st, 2) )
  19307. mob_id = mobdb_searchname(script_getstr(st, 2));
  19308. else
  19309. mob_id = mobdb_checkid(script_getnum(st, 2));
  19310. tick = script_getnum(st, 3);
  19311. if (script_hasdata(st, 4))
  19312. type = (sc_type)script_getnum(st, 4);
  19313. else
  19314. type = SC_NONE;
  19315. if (mob_id == 0) {
  19316. if( script_isstring(st, 2) )
  19317. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  19318. else
  19319. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  19320. return SCRIPT_CMD_FAILURE;
  19321. }
  19322. if (mob_id == MOBID_EMPERIUM) {
  19323. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  19324. return SCRIPT_CMD_FAILURE;
  19325. }
  19326. if (!(type >= SC_NONE && type < SC_MAX)) {
  19327. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  19328. return SCRIPT_CMD_FAILURE;
  19329. }
  19330. if (script_hasdata(st, 5))
  19331. val1 = script_getnum(st, 5);
  19332. if (script_hasdata(st, 6))
  19333. val2 = script_getnum(st, 6);
  19334. if (script_hasdata(st, 7))
  19335. val3 = script_getnum(st, 7);
  19336. if (script_hasdata(st, 8))
  19337. val4 = script_getnum(st, 8);
  19338. if (tick != 0) {
  19339. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  19340. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  19341. return SCRIPT_CMD_FAILURE;
  19342. }
  19343. if (sd->disguise){
  19344. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  19345. return SCRIPT_CMD_FAILURE;
  19346. }
  19347. if (!strcmp(script_getfuncname(st), "active_transform")) {
  19348. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19349. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19350. } else {
  19351. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19352. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19353. }
  19354. if (type != SC_NONE)
  19355. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  19356. }
  19357. return SCRIPT_CMD_SUCCESS;
  19358. }
  19359. /**
  19360. * Attach script to player for certain duration
  19361. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  19362. * @param "script code"
  19363. * @param duration
  19364. * @param flag
  19365. * @param icon
  19366. * @param char_id
  19367. * @author [Cydh]
  19368. **/
  19369. BUILDIN_FUNC(bonus_script) {
  19370. uint16 flag = 0;
  19371. int16 icon = EFST_BLANK;
  19372. uint32 dur;
  19373. uint8 type = 0;
  19374. TBL_PC* sd;
  19375. const char *script_str = NULL;
  19376. struct s_bonus_script_entry *entry = NULL;
  19377. if ( !script_charid2sd(7,sd) )
  19378. return SCRIPT_CMD_FAILURE;
  19379. script_str = script_getstr(st,2);
  19380. dur = 1000 * abs(script_getnum(st,3));
  19381. FETCH(4, flag);
  19382. FETCH(5, type);
  19383. FETCH(6, icon);
  19384. // No Script string, No Duration!
  19385. if (script_str[0] == '\0' || !dur) {
  19386. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  19387. return SCRIPT_CMD_FAILURE;
  19388. }
  19389. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  19390. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  19391. return SCRIPT_CMD_FAILURE;
  19392. }
  19393. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  19394. icon = EFST_BLANK;
  19395. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_types)icon, flag, type))) {
  19396. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  19397. status_calc_pc(sd,SCO_NONE);
  19398. }
  19399. return SCRIPT_CMD_SUCCESS;
  19400. }
  19401. /**
  19402. * Removes all bonus script from player
  19403. * bonus_script_clear {<flag>,{<char_id>}};
  19404. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  19405. * @param char_id Clear script from this character
  19406. * @author [Cydh]
  19407. */
  19408. BUILDIN_FUNC(bonus_script_clear) {
  19409. TBL_PC* sd;
  19410. bool flag = false;
  19411. if (!script_charid2sd(3,sd))
  19412. return SCRIPT_CMD_FAILURE;
  19413. if (script_hasdata(st,2))
  19414. flag = script_getnum(st,2) != 0;
  19415. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  19416. return SCRIPT_CMD_SUCCESS;
  19417. }
  19418. /** Allows player to use atcommand while talking with NPC
  19419. * enable_command;
  19420. * @author [Cydh], [Kichi]
  19421. */
  19422. BUILDIN_FUNC(enable_command) {
  19423. TBL_PC* sd;
  19424. if (!script_rid2sd(sd))
  19425. return SCRIPT_CMD_FAILURE;
  19426. sd->state.disable_atcommand_on_npc = 0;
  19427. return SCRIPT_CMD_SUCCESS;
  19428. }
  19429. /** Prevents player to use atcommand while talking with NPC
  19430. * disable_command;
  19431. * @author [Cydh], [Kichi]
  19432. */
  19433. BUILDIN_FUNC(disable_command) {
  19434. TBL_PC* sd;
  19435. if (!script_rid2sd(sd))
  19436. return SCRIPT_CMD_FAILURE;
  19437. sd->state.disable_atcommand_on_npc = 1;
  19438. return SCRIPT_CMD_SUCCESS;
  19439. }
  19440. /** Get the information of the members of a guild by type.
  19441. * getguildmember <guild_id>{,<type>};
  19442. * @param guild_id: ID of guild
  19443. * @param type: Type of option (optional)
  19444. */
  19445. BUILDIN_FUNC(getguildmember)
  19446. {
  19447. struct guild *g = NULL;
  19448. uint8 j = 0;
  19449. g = guild_search(script_getnum(st,2));
  19450. if (g) {
  19451. uint8 i, type = 0;
  19452. struct script_data *data = NULL;
  19453. char *varname = NULL;
  19454. if (script_hasdata(st,3))
  19455. type = script_getnum(st,3);
  19456. if (script_hasdata(st,4)) {
  19457. data = script_getdata(st, 4);
  19458. if (!data_isreference(data)) {
  19459. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  19460. return SCRIPT_CMD_FAILURE;
  19461. }
  19462. varname = reference_getname(data);
  19463. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  19464. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  19465. return SCRIPT_CMD_FAILURE;
  19466. }
  19467. if (not_server_variable(*varname)) {
  19468. struct map_session_data *sd;
  19469. if (!script_rid2sd(sd)) {
  19470. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  19471. return SCRIPT_CMD_FAILURE;
  19472. }
  19473. }
  19474. }
  19475. for (i = 0; i < MAX_GUILD; i++) {
  19476. if (g->member[i].account_id) {
  19477. switch (type) {
  19478. case 2:
  19479. if (data)
  19480. setd_sub_num( st, NULL, varname, j, g->member[i].account_id, data->ref );
  19481. else
  19482. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  19483. break;
  19484. case 1:
  19485. if (data)
  19486. setd_sub_num( st, NULL, varname, j, g->member[i].char_id, data->ref );
  19487. else
  19488. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  19489. break;
  19490. default:
  19491. if (data)
  19492. setd_sub_str( st, NULL, varname, j, g->member[i].name, data->ref );
  19493. else
  19494. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  19495. break;
  19496. }
  19497. j++;
  19498. }
  19499. }
  19500. }
  19501. mapreg_setreg(add_str("$@guildmembercount"), j);
  19502. script_pushint(st, j);
  19503. return SCRIPT_CMD_SUCCESS;
  19504. }
  19505. /** Adds spirit ball to player for 'duration' in milisecond
  19506. * addspiritball <count>,<duration>{,<char_id>};
  19507. * @param count How many spirit ball will be added
  19508. * @param duration How long spiritball is active until it disappears
  19509. * @param char_id Target player (Optional)
  19510. * @author [Cydh]
  19511. */
  19512. BUILDIN_FUNC(addspiritball) {
  19513. uint8 i, count;
  19514. uint16 duration;
  19515. struct map_session_data *sd = NULL;
  19516. if (script_hasdata(st,4)) {
  19517. if (!script_isstring(st,4))
  19518. sd = map_charid2sd(script_getnum(st,4));
  19519. else
  19520. sd = map_nick2sd(script_getstr(st,4),false);
  19521. }
  19522. else
  19523. script_rid2sd(sd);
  19524. if (!sd)
  19525. return SCRIPT_CMD_FAILURE;
  19526. count = script_getnum(st,2);
  19527. if (count == 0)
  19528. return SCRIPT_CMD_SUCCESS;
  19529. duration = script_getnum(st,3);
  19530. for (i = 0; i < count; i++)
  19531. pc_addspiritball(sd,duration,10);
  19532. return SCRIPT_CMD_SUCCESS;
  19533. }
  19534. /** Deletes the spirit ball(s) from player
  19535. * delspiritball <count>{,<char_id>};
  19536. * @param count How many spirit ball will be deleted
  19537. * @param char_id Target player (Optional)
  19538. * @author [Cydh]
  19539. */
  19540. BUILDIN_FUNC(delspiritball) {
  19541. uint8 count;
  19542. struct map_session_data *sd = NULL;
  19543. if (script_hasdata(st,3)) {
  19544. if (!script_isstring(st,3))
  19545. sd = map_charid2sd(script_getnum(st,3));
  19546. else
  19547. sd = map_nick2sd(script_getstr(st,3),false);
  19548. }
  19549. else
  19550. script_rid2sd(sd);
  19551. if (!sd)
  19552. return SCRIPT_CMD_FAILURE;
  19553. count = script_getnum(st,2);
  19554. if (count == 0)
  19555. count = 1;
  19556. pc_delspiritball(sd,count,0);
  19557. return SCRIPT_CMD_SUCCESS;
  19558. }
  19559. /** Counts the spirit ball that player has
  19560. * countspiritball {<char_id>};
  19561. * @param char_id Target player (Optional)
  19562. * @author [Cydh]
  19563. */
  19564. BUILDIN_FUNC(countspiritball) {
  19565. struct map_session_data *sd;
  19566. if (script_hasdata(st,2)) {
  19567. if (!script_isstring(st,2))
  19568. sd = map_charid2sd(script_getnum(st,2));
  19569. else
  19570. sd = map_nick2sd(script_getstr(st,2),false);
  19571. }
  19572. else
  19573. script_rid2sd(sd);
  19574. if (!sd)
  19575. return SCRIPT_CMD_FAILURE;
  19576. script_pushint(st,sd->spiritball);
  19577. return SCRIPT_CMD_SUCCESS;
  19578. }
  19579. /** Merges separated stackable items because of guid
  19580. * mergeitem {<char_id>};
  19581. * @param char_id Char ID (Optional)
  19582. * @author [Cydh]
  19583. */
  19584. BUILDIN_FUNC(mergeitem) {
  19585. struct map_session_data *sd;
  19586. if (!script_charid2sd(2, sd))
  19587. return SCRIPT_CMD_FAILURE;
  19588. clif_merge_item_open(sd);
  19589. return SCRIPT_CMD_SUCCESS;
  19590. }
  19591. /** Merges separated stackable items because of guid (Unofficial)
  19592. * mergeitem2 {<item_id>,{<char_id>}};
  19593. * mergeitem2 {"<item name>",{<char_id>}};
  19594. * @param item Item ID/Name for merging specific item (Optional)
  19595. * @author [Cydh]
  19596. */
  19597. BUILDIN_FUNC(mergeitem2) {
  19598. struct map_session_data *sd;
  19599. struct item *items = NULL;
  19600. uint16 i, count = 0;
  19601. t_itemid nameid = 0;
  19602. if (!script_charid2sd(3, sd))
  19603. return SCRIPT_CMD_FAILURE;
  19604. if (script_hasdata(st, 2)) {
  19605. if (script_isstring(st, 2)) {// "<item name>"
  19606. const char *name = script_getstr(st, 2);
  19607. struct item_data *id;
  19608. if (!(id = itemdb_searchname(name))) {
  19609. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  19610. script_pushint(st, count);
  19611. return SCRIPT_CMD_FAILURE;
  19612. }
  19613. nameid = id->nameid;
  19614. } else {// <item id>
  19615. nameid = script_getnum(st, 2);
  19616. if (!itemdb_exists(nameid)) {
  19617. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  19618. script_pushint(st, count);
  19619. return SCRIPT_CMD_FAILURE;
  19620. }
  19621. }
  19622. }
  19623. for (i = 0; i < MAX_INVENTORY; i++) {
  19624. struct item *it = &sd->inventory.u.items_inventory[i];
  19625. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  19626. continue;
  19627. if ((!nameid || (nameid == it->nameid))) {
  19628. uint8 k;
  19629. if (!count) {
  19630. CREATE(items, struct item, 1);
  19631. memcpy(&items[count++], it, sizeof(struct item));
  19632. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19633. continue;
  19634. }
  19635. for (k = 0; k < count; k++) {
  19636. // Find Match
  19637. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  19638. items[k].amount += it->amount;
  19639. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19640. break;
  19641. }
  19642. }
  19643. if (k >= count) {
  19644. // New entry
  19645. RECREATE(items, struct item, count+1);
  19646. memcpy(&items[count++], it, sizeof(struct item));
  19647. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19648. }
  19649. }
  19650. }
  19651. if (!items) // Nothing todo here
  19652. return SCRIPT_CMD_SUCCESS;
  19653. // Retrieve the items
  19654. for (i = 0; i < count; i++) {
  19655. uint8 flag = 0;
  19656. if (!&items[i])
  19657. continue;
  19658. items[i].id = 0;
  19659. items[i].unique_id = 0;
  19660. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  19661. clif_additem(sd, i, items[i].amount, flag);
  19662. }
  19663. aFree(items);
  19664. script_pushint(st, count);
  19665. return SCRIPT_CMD_SUCCESS;
  19666. }
  19667. /**
  19668. * Update an entry from NPC shop.
  19669. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  19670. **/
  19671. BUILDIN_FUNC(npcshopupdate) {
  19672. const char* npcname = script_getstr(st, 2);
  19673. struct npc_data* nd = npc_name2id(npcname);
  19674. t_itemid nameid = script_getnum(st, 3);
  19675. int price = script_getnum(st, 4);
  19676. #if PACKETVER >= 20131223
  19677. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  19678. #endif
  19679. int i;
  19680. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  19681. script_pushint(st,0);
  19682. return SCRIPT_CMD_FAILURE;
  19683. }
  19684. if (!nd->u.shop.count) {
  19685. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  19686. script_pushint(st,0);
  19687. return SCRIPT_CMD_FAILURE;
  19688. }
  19689. for (i = 0; i < nd->u.shop.count; i++) {
  19690. if (nd->u.shop.shop_item[i].nameid == nameid) {
  19691. if (price != 0)
  19692. nd->u.shop.shop_item[i].value = price;
  19693. #if PACKETVER >= 20131223
  19694. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  19695. nd->u.shop.shop_item[i].qty = stock;
  19696. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  19697. }
  19698. #endif
  19699. }
  19700. }
  19701. script_pushint(st,1);
  19702. return SCRIPT_CMD_SUCCESS;
  19703. }
  19704. // Clan System
  19705. BUILDIN_FUNC(clan_join){
  19706. struct map_session_data *sd;
  19707. int clan_id = script_getnum(st,2);
  19708. if( !script_charid2sd( 3, sd ) ){
  19709. script_pushint(st, false);
  19710. return SCRIPT_CMD_FAILURE;
  19711. }
  19712. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  19713. script_pushint(st, true);
  19714. else
  19715. script_pushint(st, false);
  19716. return SCRIPT_CMD_SUCCESS;
  19717. }
  19718. BUILDIN_FUNC(clan_leave){
  19719. struct map_session_data *sd;
  19720. if( !script_charid2sd( 2, sd ) ){
  19721. script_pushint(st, false);
  19722. return SCRIPT_CMD_FAILURE;
  19723. }
  19724. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  19725. script_pushint(st, true);
  19726. else
  19727. script_pushint(st, false);
  19728. return SCRIPT_CMD_SUCCESS;
  19729. }
  19730. /**
  19731. * Get rid from running script.
  19732. * getattachedrid();
  19733. **/
  19734. BUILDIN_FUNC(getattachedrid) {
  19735. script_pushint(st, st->rid);
  19736. return SCRIPT_CMD_SUCCESS;
  19737. }
  19738. /**
  19739. * Get variable from a Player
  19740. * getvar <variable>,<char_id>;
  19741. */
  19742. BUILDIN_FUNC(getvar) {
  19743. int char_id = script_getnum(st, 3);
  19744. struct map_session_data *sd = map_charid2sd(char_id);
  19745. struct script_data *data = NULL;
  19746. const char *name = NULL;
  19747. if (!sd) {
  19748. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  19749. return SCRIPT_CMD_FAILURE;
  19750. }
  19751. data = script_getdata(st, 2);
  19752. if (!data_isreference(data)) {
  19753. ShowError("buildin_getvar: Not a variable\n");
  19754. script_reportdata(data);
  19755. script_pushnil(st);
  19756. st->state = END;
  19757. return SCRIPT_CMD_FAILURE;
  19758. }
  19759. name = reference_getname(data);
  19760. if (reference_toparam(data)) {
  19761. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  19762. script_reportdata(data);
  19763. script_pushnil(st);
  19764. st->state = END;
  19765. return SCRIPT_CMD_FAILURE;
  19766. }
  19767. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  19768. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  19769. script_reportdata(data);
  19770. script_pushnil(st);
  19771. st->state = END;
  19772. return SCRIPT_CMD_FAILURE;
  19773. }
  19774. get_val_(st, data, sd);
  19775. if (data_isint(data))
  19776. script_pushint(st, conv_num_(st, data, sd));
  19777. else
  19778. script_pushstrcopy(st, conv_str_(st, data, sd));
  19779. return SCRIPT_CMD_SUCCESS;
  19780. }
  19781. /**
  19782. * Display script message
  19783. * showscript "<message>"{,<GID>,<flag>};
  19784. * @param flag: Specify target
  19785. * AREA - Message is sent to players in the vicinity of the source (default).
  19786. * SELF - Message is sent only to player attached.
  19787. **/
  19788. BUILDIN_FUNC(showscript) {
  19789. struct block_list *bl = NULL;
  19790. const char *msg = script_getstr(st,2);
  19791. int id = 0;
  19792. send_target target = AREA;
  19793. if (script_hasdata(st,3)) {
  19794. id = script_getnum(st,3);
  19795. bl = map_id2bl(id);
  19796. }
  19797. else {
  19798. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  19799. }
  19800. if (!bl) {
  19801. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  19802. script_pushint(st,0);
  19803. return SCRIPT_CMD_FAILURE;
  19804. }
  19805. if (script_hasdata(st, 4)) {
  19806. target = static_cast<send_target>(script_getnum(st, 4));
  19807. if (target == SELF && map_id2sd(bl->id) == NULL) {
  19808. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  19809. return SCRIPT_CMD_FAILURE;
  19810. }
  19811. }
  19812. clif_showscript(bl, msg, target);
  19813. script_pushint(st,1);
  19814. return SCRIPT_CMD_SUCCESS;
  19815. }
  19816. /**
  19817. * Ignore the SECURE_NPCTIMEOUT function.
  19818. * ignoretimeout <flag>{,<char_id>};
  19819. */
  19820. BUILDIN_FUNC(ignoretimeout)
  19821. {
  19822. #ifdef SECURE_NPCTIMEOUT
  19823. struct map_session_data *sd = NULL;
  19824. if (script_hasdata(st,3)) {
  19825. if (!script_isstring(st,3))
  19826. sd = map_charid2sd(script_getnum(st,3));
  19827. else
  19828. sd = map_nick2sd(script_getstr(st,3),false);
  19829. } else
  19830. script_rid2sd(sd);
  19831. if (!sd)
  19832. return SCRIPT_CMD_FAILURE;
  19833. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  19834. #endif
  19835. return SCRIPT_CMD_SUCCESS;
  19836. }
  19837. /**
  19838. * geteleminfo <type>{,<char_id>};
  19839. **/
  19840. BUILDIN_FUNC(geteleminfo) {
  19841. TBL_ELEM *ed = NULL;
  19842. TBL_PC *sd = NULL;
  19843. int type = script_getnum(st,2);
  19844. if (!script_charid2sd(3, sd)) {
  19845. script_pushint(st, 0);
  19846. return SCRIPT_CMD_SUCCESS;
  19847. }
  19848. if (!(ed = sd->ed)) {
  19849. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  19850. script_pushint(st, 0);
  19851. return SCRIPT_CMD_SUCCESS;
  19852. }
  19853. switch (type) {
  19854. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  19855. case 1: script_pushint(st, ed->bl.id); break;
  19856. default:
  19857. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  19858. script_pushint(st, 0);
  19859. return SCRIPT_CMD_FAILURE;
  19860. }
  19861. return SCRIPT_CMD_SUCCESS;
  19862. }
  19863. /**
  19864. * opendressroom(<flag>{,<char_id>});
  19865. */
  19866. BUILDIN_FUNC(opendressroom)
  19867. {
  19868. #if PACKETVER >= 20150513
  19869. int flag = 1;
  19870. TBL_PC* sd;
  19871. if( script_hasdata(st,2) )
  19872. flag = script_getnum(st,2);
  19873. if (!script_charid2sd(3, sd))
  19874. return SCRIPT_CMD_FAILURE;
  19875. clif_dressing_room(sd, flag);
  19876. return SCRIPT_CMD_SUCCESS;
  19877. #else
  19878. return SCRIPT_CMD_FAILURE;
  19879. #endif
  19880. }
  19881. /**
  19882. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  19883. */
  19884. BUILDIN_FUNC(navigateto){
  19885. #if PACKETVER >= 20111010
  19886. TBL_PC* sd;
  19887. const char *mapname;
  19888. uint16 x = 0, y = 0, monster_id = 0;
  19889. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  19890. bool hideWindow = true;
  19891. mapname = script_getstr(st,2);
  19892. if( script_hasdata(st,3) )
  19893. x = script_getnum(st,3);
  19894. if( script_hasdata(st,4) )
  19895. y = script_getnum(st,4);
  19896. if( script_hasdata(st,5) )
  19897. flag = (uint8)script_getnum(st,5);
  19898. if( script_hasdata(st,6) )
  19899. hideWindow = script_getnum(st,6) ? true : false;
  19900. if( script_hasdata(st,7) )
  19901. monster_id = script_getnum(st,7);
  19902. if (!script_charid2sd(8, sd))
  19903. return SCRIPT_CMD_FAILURE;
  19904. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  19905. return SCRIPT_CMD_SUCCESS;
  19906. #else
  19907. return SCRIPT_CMD_FAILURE;
  19908. #endif
  19909. }
  19910. /**
  19911. * adopt("<parent_name>","<baby_name>");
  19912. * adopt(<parent_id>,<baby_id>);
  19913. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  19914. */
  19915. BUILDIN_FUNC(adopt)
  19916. {
  19917. TBL_PC *sd, *b_sd;
  19918. enum adopt_responses response;
  19919. if (script_isstring(st, 2)) {
  19920. const char *name = script_getstr(st, 2);
  19921. sd = map_nick2sd(name,false);
  19922. if (sd == NULL) {
  19923. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  19924. return SCRIPT_CMD_FAILURE;
  19925. }
  19926. } else {
  19927. uint32 char_id = script_getnum(st, 2);
  19928. sd = map_charid2sd(char_id);
  19929. if (sd == NULL) {
  19930. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  19931. return SCRIPT_CMD_FAILURE;
  19932. }
  19933. }
  19934. if (script_isstring(st, 3)) {
  19935. const char *name = script_getstr(st, 3);
  19936. b_sd = map_nick2sd(name,false);
  19937. if (b_sd == NULL) {
  19938. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  19939. return SCRIPT_CMD_FAILURE;
  19940. }
  19941. } else {
  19942. uint32 char_id = script_getnum(st, 3);
  19943. b_sd = map_charid2sd(char_id);
  19944. if (b_sd == NULL) {
  19945. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  19946. return SCRIPT_CMD_FAILURE;
  19947. }
  19948. }
  19949. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  19950. if (response == ADOPT_ALLOWED) {
  19951. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  19952. b_sd->adopt_invite = sd->status.account_id;
  19953. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  19954. script_pushint(st, ADOPT_ALLOWED);
  19955. return SCRIPT_CMD_SUCCESS;
  19956. }
  19957. script_pushint(st, response);
  19958. return SCRIPT_CMD_FAILURE;
  19959. }
  19960. /**
  19961. * Returns the minimum or maximum of all the given parameters for integer variables.
  19962. *
  19963. * min( <value or array>{,value or array 2,...} );
  19964. * minimum( <value or array>{,value or array 2,...} );
  19965. * max( <value or array>{,value or array 2,...} );
  19966. * maximum( <value or array>{,value or array 2,...} );
  19967. */
  19968. BUILDIN_FUNC(minmax){
  19969. char *functionname;
  19970. unsigned int i;
  19971. int64 value;
  19972. // Function pointer for our comparison function (either min or max at the moment)
  19973. int64 (*func)(int64, int64);
  19974. // Get the real function name
  19975. functionname = script_getfuncname(st);
  19976. // Our data should start at offset 2
  19977. i = 2;
  19978. if( !script_hasdata( st, i ) ){
  19979. ShowError( "buildin_%s: no arguments given!\n", functionname );
  19980. st->state = END;
  19981. return SCRIPT_CMD_FAILURE;
  19982. }
  19983. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  19984. value = SCRIPT_INT_MAX;
  19985. func = i64min;
  19986. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  19987. value = SCRIPT_INT_MIN;
  19988. func = i64max;
  19989. }else{
  19990. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  19991. st->state = END;
  19992. return SCRIPT_CMD_FAILURE;
  19993. }
  19994. // As long as we have data on our script stack
  19995. while( script_hasdata(st,i) ){
  19996. struct script_data *data;
  19997. // Get the next piece of data from the script stack
  19998. data = script_getdata( st, i );
  19999. // Is the current parameter a single integer?
  20000. if( data_isint( data ) ){
  20001. value = func( value, script_getnum( st, i ) );
  20002. // Is the current parameter an array variable?
  20003. }else if( data_isreference( data ) ){
  20004. const char *name;
  20005. struct map_session_data* sd;
  20006. unsigned int start, end;
  20007. // Get the name of the variable
  20008. name = reference_getname(data);
  20009. // Check if it's a string variable
  20010. if( is_string_variable( name ) ){
  20011. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20012. script_reportdata( data );
  20013. st->state = END;
  20014. return SCRIPT_CMD_FAILURE;
  20015. }
  20016. // Get the session data, if a player is attached
  20017. sd = st->rid ? map_id2sd(st->rid) : NULL;
  20018. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20019. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20020. return SCRIPT_CMD_FAILURE;
  20021. }
  20022. // Try to find the array's source pointer
  20023. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20024. ShowError( "buildin_%s: not a array!\n", functionname );
  20025. script_reportdata( data );
  20026. st->state = END;
  20027. return SCRIPT_CMD_FAILURE;
  20028. }
  20029. // Get the start and end indices of the array
  20030. start = reference_getindex( data );
  20031. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20032. // Skip empty arrays
  20033. if( start < end ){
  20034. int id;
  20035. // For getting the values we need the id of the array
  20036. id = reference_getid( data );
  20037. // Loop through each value stored in the array
  20038. for( ; start < end; start++ ){
  20039. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20040. }
  20041. }
  20042. else {
  20043. value = func( value, 0 );
  20044. }
  20045. }else{
  20046. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20047. script_reportdata( data );
  20048. st->state = END;
  20049. return SCRIPT_CMD_FAILURE;
  20050. }
  20051. // Continue with the next stack entry
  20052. i++;
  20053. }
  20054. script_pushint( st, value );
  20055. return SCRIPT_CMD_SUCCESS;
  20056. }
  20057. /**
  20058. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20059. * Unlike `getexp` that affected by some adjustments.
  20060. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20061. * @author [Cydh]
  20062. **/
  20063. BUILDIN_FUNC(getexp2) {
  20064. TBL_PC *sd = NULL;
  20065. int base_exp = script_getnum(st, 2);
  20066. int job_exp = script_getnum(st, 3);
  20067. if (!script_charid2sd(4, sd))
  20068. return SCRIPT_CMD_FAILURE;
  20069. if (base_exp == 0 && job_exp == 0)
  20070. return SCRIPT_CMD_SUCCESS;
  20071. if (base_exp > 0)
  20072. pc_gainexp(sd, NULL, base_exp, 0, 2);
  20073. else if (base_exp < 0)
  20074. pc_lostexp(sd, base_exp * -1, 0);
  20075. if (job_exp > 0)
  20076. pc_gainexp(sd, NULL, 0, job_exp, 2);
  20077. else if (job_exp < 0)
  20078. pc_lostexp(sd, 0, job_exp * -1);
  20079. return SCRIPT_CMD_SUCCESS;
  20080. }
  20081. /**
  20082. * Force stat recalculation of sd
  20083. * recalculatestat;
  20084. * @author [secretdataz]
  20085. **/
  20086. BUILDIN_FUNC(recalculatestat) {
  20087. TBL_PC* sd;
  20088. if (!script_rid2sd(sd))
  20089. return SCRIPT_CMD_FAILURE;
  20090. status_calc_pc(sd, SCO_FORCE);
  20091. return SCRIPT_CMD_SUCCESS;
  20092. }
  20093. BUILDIN_FUNC(hateffect){
  20094. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20095. struct map_session_data* sd;
  20096. if( !script_rid2sd(sd) )
  20097. return SCRIPT_CMD_FAILURE;
  20098. int16 effectID = script_getnum(st,2);
  20099. bool enable = script_getnum(st,3) ? true : false;
  20100. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20101. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20102. return SCRIPT_CMD_FAILURE;
  20103. }
  20104. auto it = util::vector_get( sd->hatEffects, effectID );
  20105. if( enable ){
  20106. if( it != sd->hatEffects.end() ){
  20107. return SCRIPT_CMD_SUCCESS;
  20108. }
  20109. sd->hatEffects.push_back( effectID );
  20110. }else{
  20111. if( it == sd->hatEffects.end() ){
  20112. return SCRIPT_CMD_SUCCESS;
  20113. }
  20114. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20115. }
  20116. if( !sd->state.connect_new ){
  20117. clif_hat_effect_single( sd, effectID, enable );
  20118. }
  20119. #endif
  20120. return SCRIPT_CMD_SUCCESS;
  20121. }
  20122. /**
  20123. * Retrieves param of current random option. Intended for random option script only.
  20124. * getrandomoptinfo(<type>);
  20125. * @author [secretdataz]
  20126. **/
  20127. BUILDIN_FUNC(getrandomoptinfo) {
  20128. struct map_session_data *sd;
  20129. int val;
  20130. 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) {
  20131. int param = script_getnum(st, 2);
  20132. switch (param) {
  20133. case ROA_ID:
  20134. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20135. break;
  20136. case ROA_VALUE:
  20137. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20138. break;
  20139. case ROA_PARAM:
  20140. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  20141. break;
  20142. default:
  20143. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  20144. val = 0;
  20145. break;
  20146. }
  20147. script_pushint(st, val);
  20148. }
  20149. else {
  20150. script_pushint(st, 0);
  20151. }
  20152. return SCRIPT_CMD_SUCCESS;
  20153. }
  20154. /**
  20155. * Retrieves a random option on an equipped item.
  20156. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  20157. * @author [secretdataz]
  20158. */
  20159. BUILDIN_FUNC(getequiprandomoption) {
  20160. struct map_session_data *sd;
  20161. int val;
  20162. short i = -1;
  20163. int pos = script_getnum(st, 2);
  20164. int index = script_getnum(st, 3);
  20165. int type = script_getnum(st, 4);
  20166. if (!script_charid2sd(5, sd)) {
  20167. script_pushint(st, -1);
  20168. return SCRIPT_CMD_FAILURE;
  20169. }
  20170. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20171. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  20172. script_pushint(st, -1);
  20173. return SCRIPT_CMD_FAILURE;
  20174. }
  20175. if (equip_index_check(pos))
  20176. i = pc_checkequip(sd, equip_bitmask[pos]);
  20177. if (i < 0) {
  20178. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20179. script_pushint(st, -1);
  20180. return SCRIPT_CMD_FAILURE;
  20181. }
  20182. switch (type) {
  20183. case ROA_ID:
  20184. val = sd->inventory.u.items_inventory[i].option[index].id;
  20185. break;
  20186. case ROA_VALUE:
  20187. val = sd->inventory.u.items_inventory[i].option[index].value;
  20188. break;
  20189. case ROA_PARAM:
  20190. val = sd->inventory.u.items_inventory[i].option[index].param;
  20191. break;
  20192. default:
  20193. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  20194. val = 0;
  20195. break;
  20196. }
  20197. script_pushint(st, val);
  20198. return SCRIPT_CMD_SUCCESS;
  20199. }
  20200. /**
  20201. * Adds a random option on a random option slot on an equipped item and overwrites
  20202. * existing random option in the process.
  20203. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  20204. * @author [secretdataz]
  20205. */
  20206. BUILDIN_FUNC(setrandomoption) {
  20207. struct map_session_data *sd;
  20208. int pos, index, id, value, param, ep;
  20209. int i = -1;
  20210. if (!script_charid2sd(7, sd))
  20211. return SCRIPT_CMD_FAILURE;
  20212. pos = script_getnum(st, 2);
  20213. index = script_getnum(st, 3);
  20214. id = script_getnum(st, 4);
  20215. value = script_getnum(st, 5);
  20216. param = script_getnum(st, 6);
  20217. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  20218. if (opt == nullptr) {
  20219. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  20220. script_pushint(st, 0);
  20221. return SCRIPT_CMD_FAILURE;
  20222. }
  20223. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20224. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  20225. script_pushint(st, 0);
  20226. return SCRIPT_CMD_FAILURE;
  20227. }
  20228. if (equip_index_check(pos))
  20229. i = pc_checkequip(sd, equip_bitmask[pos]);
  20230. if (i >= 0) {
  20231. ep = sd->inventory.u.items_inventory[i].equip;
  20232. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20233. pc_unequipitem(sd, i, 2);
  20234. sd->inventory.u.items_inventory[i].option[index].id = id;
  20235. sd->inventory.u.items_inventory[i].option[index].value = value;
  20236. sd->inventory.u.items_inventory[i].option[index].param = param;
  20237. clif_delitem(sd, i, 1, 3);
  20238. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20239. clif_additem(sd, i, 1, 0);
  20240. pc_equipitem(sd, i, ep);
  20241. script_pushint(st, 1);
  20242. return SCRIPT_CMD_SUCCESS;
  20243. }
  20244. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20245. script_pushint(st, 0);
  20246. return SCRIPT_CMD_FAILURE;
  20247. }
  20248. /// Returns the number of stat points needed to change the specified stat by val.
  20249. /// If val is negative, returns the number of stat points that would be needed to
  20250. /// raise the specified stat from (current value - val) to current value.
  20251. /// *needed_status_point(<type>,<val>{,<char id>});
  20252. /// @author [secretdataz]
  20253. BUILDIN_FUNC(needed_status_point) {
  20254. struct map_session_data *sd;
  20255. int type, val;
  20256. if (!script_charid2sd(4, sd))
  20257. return SCRIPT_CMD_FAILURE;
  20258. type = script_getnum(st, 2);
  20259. val = script_getnum(st, 3);
  20260. script_pushint(st, pc_need_status_point(sd, type, val));
  20261. return SCRIPT_CMD_SUCCESS;
  20262. }
  20263. /**
  20264. * jobcanentermap("<mapname>"{,<JobID>});
  20265. * Check if (player with) JobID can enter the map.
  20266. * @param mapname Map name
  20267. * @param JobID Player's JobID (optional)
  20268. **/
  20269. BUILDIN_FUNC(jobcanentermap) {
  20270. const char *mapname = script_getstr(st, 2);
  20271. int mapidx = mapindex_name2id(mapname), m = -1;
  20272. int jobid = 0;
  20273. TBL_PC *sd = NULL;
  20274. if (!mapidx) {// Invalid map
  20275. script_pushint(st, false);
  20276. return SCRIPT_CMD_FAILURE;
  20277. }
  20278. m = map_mapindex2mapid(mapidx);
  20279. if (m == -1) { // Map is on different map server
  20280. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  20281. script_pushint(st, false);
  20282. return SCRIPT_CMD_FAILURE;
  20283. }
  20284. if (script_hasdata(st, 3)) {
  20285. jobid = script_getnum(st, 3);
  20286. } else {
  20287. if (!script_rid2sd(sd)) {
  20288. script_pushint(st, false);
  20289. return SCRIPT_CMD_FAILURE;
  20290. }
  20291. jobid = sd->status.class_;
  20292. }
  20293. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  20294. return SCRIPT_CMD_SUCCESS;
  20295. }
  20296. /**
  20297. * Return alliance information between the two guilds.
  20298. * getguildalliance(<guild id1>,<guild id2>);
  20299. * Return values:
  20300. * -2 - Guild ID1 does not exist
  20301. * -1 - Guild ID2 does not exist
  20302. * 0 - Both guilds have no relation OR guild ID aren't given
  20303. * 1 - Both guilds are allies
  20304. * 2 - Both guilds are antagonists
  20305. */
  20306. BUILDIN_FUNC(getguildalliance)
  20307. {
  20308. struct guild *guild_data1, *guild_data2;
  20309. int guild_id1, guild_id2, i = 0;
  20310. guild_id1 = script_getnum(st,2);
  20311. guild_id2 = script_getnum(st,3);
  20312. if (guild_id1 < 1 || guild_id2 < 1) {
  20313. script_pushint(st, 0);
  20314. return SCRIPT_CMD_SUCCESS;
  20315. }
  20316. if (guild_id1 == guild_id2) {
  20317. script_pushint(st, 1);
  20318. return SCRIPT_CMD_SUCCESS;
  20319. }
  20320. guild_data1 = guild_search(guild_id1);
  20321. guild_data2 = guild_search(guild_id2);
  20322. if (guild_data1 == NULL) {
  20323. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  20324. script_pushint(st, -2);
  20325. return SCRIPT_CMD_FAILURE;
  20326. }
  20327. if (guild_data2 == NULL) {
  20328. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  20329. script_pushint(st, -1);
  20330. return SCRIPT_CMD_FAILURE;
  20331. }
  20332. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  20333. if (i == MAX_GUILDALLIANCE) {
  20334. script_pushint(st, 0);
  20335. return SCRIPT_CMD_SUCCESS;
  20336. }
  20337. if (guild_data1->alliance[i].opposition)
  20338. script_pushint(st, 2);
  20339. else
  20340. script_pushint(st, 1);
  20341. return SCRIPT_CMD_SUCCESS;
  20342. }
  20343. /*
  20344. * openstorage2 <storage_id>,<mode>{,<account_id>}
  20345. * mode @see enum e_storage_mode
  20346. **/
  20347. BUILDIN_FUNC(openstorage2) {
  20348. int stor_id = script_getnum(st, 2);
  20349. TBL_PC *sd = NULL;
  20350. if (!script_accid2sd(4, sd)) {
  20351. script_pushint(st, 0);
  20352. return SCRIPT_CMD_FAILURE;
  20353. }
  20354. if (!storage_exists(stor_id)) {
  20355. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  20356. return SCRIPT_CMD_FAILURE;
  20357. }
  20358. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  20359. return SCRIPT_CMD_SUCCESS;
  20360. }
  20361. /**
  20362. * Create a new channel
  20363. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  20364. * @author [Cydh]
  20365. **/
  20366. BUILDIN_FUNC(channel_create) {
  20367. struct Channel tmp_chan, *ch = NULL;
  20368. const char *chname = script_getstr(st,2), *pass = NULL;
  20369. int i = channel_chk((char*)chname, NULL, 3);
  20370. TBL_PC *sd = NULL;
  20371. if (i != 0) {
  20372. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  20373. script_pushint(st,i);
  20374. return SCRIPT_CMD_FAILURE;
  20375. }
  20376. memset(&tmp_chan, 0, sizeof(struct Channel));
  20377. if (script_hasdata(st,8)) {
  20378. tmp_chan.char_id = script_getnum(st,8);
  20379. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  20380. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  20381. script_pushint(st,-5);
  20382. return SCRIPT_CMD_FAILURE;
  20383. }
  20384. tmp_chan.type = CHAN_TYPE_PRIVATE;
  20385. i = 1;
  20386. }
  20387. else {
  20388. tmp_chan.type = CHAN_TYPE_PUBLIC;
  20389. i = 0;
  20390. }
  20391. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  20392. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  20393. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  20394. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  20395. if (script_hasdata(st,5))
  20396. tmp_chan.opt = script_getnum(st,5);
  20397. else
  20398. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  20399. if (script_hasdata(st,6))
  20400. tmp_chan.msg_delay = script_getnum(st,6);
  20401. else
  20402. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  20403. if (script_hasdata(st,7))
  20404. tmp_chan.color = script_getnum(st,7);
  20405. else
  20406. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  20407. if (!(ch = channel_create(&tmp_chan))) {
  20408. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  20409. script_pushint(st,0);
  20410. return SCRIPT_CMD_FAILURE;
  20411. }
  20412. if (tmp_chan.char_id)
  20413. channel_join(ch, sd);
  20414. script_pushint(st,1);
  20415. return SCRIPT_CMD_SUCCESS;
  20416. }
  20417. /**
  20418. * Set channel option
  20419. * channel_setopt "<chname>",<option>,<value>;
  20420. * @author [Cydh]
  20421. **/
  20422. BUILDIN_FUNC(channel_setopt) {
  20423. struct Channel *ch = NULL;
  20424. const char *chname = script_getstr(st,2);
  20425. int opt = script_getnum(st,3), value = script_getnum(st,4);
  20426. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20427. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  20428. script_pushint(st,0);
  20429. return SCRIPT_CMD_FAILURE;
  20430. }
  20431. switch (opt) {
  20432. case CHAN_OPT_ANNOUNCE_SELF:
  20433. case CHAN_OPT_ANNOUNCE_JOIN:
  20434. case CHAN_OPT_ANNOUNCE_LEAVE:
  20435. case CHAN_OPT_COLOR_OVERRIDE:
  20436. case CHAN_OPT_CAN_CHAT:
  20437. case CHAN_OPT_CAN_LEAVE:
  20438. case CHAN_OPT_AUTOJOIN:
  20439. if (value)
  20440. ch->opt |= opt;
  20441. else
  20442. ch->opt &= ~opt;
  20443. break;
  20444. case CHAN_OPT_MSG_DELAY:
  20445. ch->msg_delay = value;
  20446. break;
  20447. default:
  20448. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  20449. script_pushint(st,0);
  20450. return SCRIPT_CMD_FAILURE;
  20451. }
  20452. script_pushint(st,1);
  20453. return SCRIPT_CMD_SUCCESS;
  20454. }
  20455. /**
  20456. * Get channel options
  20457. * channel_getopt <chname>,<option>;
  20458. */
  20459. BUILDIN_FUNC(channel_getopt) {
  20460. Channel *ch;
  20461. const char *chname = script_getstr(st, 2);
  20462. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20463. ShowError("buildin_channel_getopt: Channel name '%s' is invalid.\n", chname);
  20464. script_pushint(st, false);
  20465. return SCRIPT_CMD_FAILURE;
  20466. }
  20467. int opt = script_getnum(st, 3);
  20468. switch (opt) {
  20469. case CHAN_OPT_ANNOUNCE_SELF:
  20470. case CHAN_OPT_ANNOUNCE_JOIN:
  20471. case CHAN_OPT_ANNOUNCE_LEAVE:
  20472. case CHAN_OPT_COLOR_OVERRIDE:
  20473. case CHAN_OPT_CAN_CHAT:
  20474. case CHAN_OPT_CAN_LEAVE:
  20475. case CHAN_OPT_AUTOJOIN:
  20476. script_pushint(st, (ch->opt & opt) != 0);
  20477. break;
  20478. case CHAN_OPT_MSG_DELAY:
  20479. script_pushint(st, ch->msg_delay);
  20480. break;
  20481. default:
  20482. ShowError("buildin_channel_getopt: Invalid option %d!\n", opt);
  20483. script_pushint(st, false);
  20484. return SCRIPT_CMD_FAILURE;
  20485. }
  20486. return SCRIPT_CMD_SUCCESS;
  20487. }
  20488. /**
  20489. * Set channel color
  20490. * channel_setcolor "<chname>",<color>;
  20491. * @author [Cydh]
  20492. **/
  20493. BUILDIN_FUNC(channel_setcolor) {
  20494. struct Channel *ch = NULL;
  20495. const char *chname = script_getstr(st,2);
  20496. int color = script_getnum(st,3);
  20497. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20498. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  20499. script_pushint(st,0);
  20500. return SCRIPT_CMD_FAILURE;
  20501. }
  20502. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  20503. script_pushint(st,1);
  20504. return SCRIPT_CMD_SUCCESS;
  20505. }
  20506. /**
  20507. * Set channel password
  20508. * channel_setpass "<chname>","<password>";
  20509. * @author [Cydh]
  20510. **/
  20511. BUILDIN_FUNC(channel_setpass) {
  20512. struct Channel *ch = NULL;
  20513. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  20514. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20515. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  20516. script_pushint(st,0);
  20517. return SCRIPT_CMD_FAILURE;
  20518. }
  20519. if (!passwd || !strcmpi(passwd,"null"))
  20520. memset(ch->pass, '\0', sizeof(ch->pass));
  20521. else
  20522. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  20523. script_pushint(st,1);
  20524. return SCRIPT_CMD_SUCCESS;
  20525. }
  20526. /**
  20527. * Set authorized groups
  20528. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  20529. * @author [Cydh]
  20530. **/
  20531. BUILDIN_FUNC(channel_setgroup) {
  20532. struct Channel *ch = NULL;
  20533. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  20534. int i, n = 0, group = 0;
  20535. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20536. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  20537. script_pushint(st,0);
  20538. return SCRIPT_CMD_FAILURE;
  20539. }
  20540. if (funcname[strlen(funcname)-1] == '2') {
  20541. struct script_data *data = script_getdata(st,3);
  20542. const char *varname = reference_getname(data);
  20543. int32 id, idx;
  20544. if (varname[strlen(varname)-1] == '$') {
  20545. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  20546. script_pushint(st,0);
  20547. return SCRIPT_CMD_FAILURE;
  20548. }
  20549. if (not_server_variable(*varname)) {
  20550. struct map_session_data *sd;
  20551. if (!script_rid2sd(sd)) {
  20552. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  20553. return SCRIPT_CMD_FAILURE;
  20554. }
  20555. }
  20556. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  20557. if (n < 1) {
  20558. ShowError("buildin_channel_setgroup: No group id listed.\n");
  20559. script_pushint(st,0);
  20560. return SCRIPT_CMD_FAILURE;
  20561. }
  20562. if (ch->groups)
  20563. aFree(ch->groups);
  20564. ch->groups = NULL;
  20565. ch->group_count = 0;
  20566. id = reference_getid(data);
  20567. idx = reference_getindex(data);
  20568. for (i = 0; i < n; i++) {
  20569. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  20570. if (group == 0) {
  20571. script_pushint(st,1);
  20572. return SCRIPT_CMD_SUCCESS;
  20573. }
  20574. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  20575. ch->groups[ch->group_count-1] = group;
  20576. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20577. }
  20578. }
  20579. else {
  20580. group = script_getnum(st,3);
  20581. n = script_lastdata(st)-1;
  20582. if (n < 1) {
  20583. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  20584. script_pushint(st,0);
  20585. return SCRIPT_CMD_FAILURE;
  20586. }
  20587. if (ch->groups)
  20588. aFree(ch->groups);
  20589. ch->groups = NULL;
  20590. ch->group_count = 0;
  20591. if (group == 0) { // Removed group list
  20592. script_pushint(st,1);
  20593. return SCRIPT_CMD_SUCCESS;
  20594. }
  20595. CREATE(ch->groups, unsigned short, n);
  20596. for (i = 3; i < n+2; i++) {
  20597. ch->groups[ch->group_count++] = script_getnum(st,i);
  20598. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20599. }
  20600. }
  20601. script_pushint(st,n);
  20602. return SCRIPT_CMD_SUCCESS;
  20603. }
  20604. /**
  20605. * Send message on channel
  20606. * channel_chat "<chname>","<message>"{,<color>};
  20607. * @author [Cydh]
  20608. **/
  20609. BUILDIN_FUNC(channel_chat) {
  20610. struct Channel *ch = NULL;
  20611. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  20612. char output[CHAT_SIZE_MAX+1];
  20613. unsigned long color = 0;
  20614. // Check also local channels
  20615. if (chname[0] != '#') { // By Map
  20616. int m = mapindex_name2id(chname);
  20617. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  20618. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  20619. script_pushint(st,0);
  20620. return SCRIPT_CMD_FAILURE;
  20621. }
  20622. if (!(ch = map_getmapdata(m)->channel)) {
  20623. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20624. script_pushint(st,0);
  20625. return SCRIPT_CMD_SUCCESS;
  20626. }
  20627. }
  20628. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  20629. TBL_NPC *nd = map_id2nd(st->oid);
  20630. if (!nd || nd->bl.m == -1) {
  20631. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  20632. script_pushint(st,0);
  20633. return SCRIPT_CMD_FAILURE;
  20634. }
  20635. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  20636. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20637. script_pushint(st,0);
  20638. return SCRIPT_CMD_SUCCESS;
  20639. }
  20640. }
  20641. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20642. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  20643. script_pushint(st,0);
  20644. return SCRIPT_CMD_FAILURE;
  20645. }
  20646. if (!ch) {
  20647. script_pushint(st,0);
  20648. return SCRIPT_CMD_FAILURE;
  20649. }
  20650. color = ch->color;
  20651. FETCH(4, color);
  20652. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  20653. clif_channel_msg(ch,output,color);
  20654. script_pushint(st,1);
  20655. return SCRIPT_CMD_SUCCESS;
  20656. }
  20657. /**
  20658. * Ban player from a channel
  20659. * channel_ban "<chname>",<char_id>;
  20660. * @author [Cydh]
  20661. **/
  20662. BUILDIN_FUNC(channel_ban) {
  20663. struct Channel *ch = NULL;
  20664. const char *chname = script_getstr(st,2);
  20665. unsigned int char_id = script_getnum(st,3);
  20666. TBL_PC *tsd;
  20667. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20668. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  20669. script_pushint(st,0);
  20670. return SCRIPT_CMD_FAILURE;
  20671. }
  20672. if (ch->char_id == char_id) {
  20673. script_pushint(st,0);
  20674. return SCRIPT_CMD_SUCCESS;
  20675. }
  20676. tsd = map_charid2sd(char_id);
  20677. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20678. script_pushint(st,0);
  20679. return SCRIPT_CMD_SUCCESS;
  20680. }
  20681. if (!idb_exists(ch->banned, char_id)) {
  20682. struct chan_banentry *cbe;
  20683. char output[CHAT_SIZE_MAX+1];
  20684. CREATE(cbe, struct chan_banentry, 1);
  20685. cbe->char_id = char_id;
  20686. if (tsd) {
  20687. strcpy(cbe->char_name,tsd->status.name);
  20688. channel_clean(ch,tsd,0);
  20689. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  20690. }
  20691. else
  20692. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  20693. idb_put(ch->banned, char_id, cbe);
  20694. clif_channel_msg(ch,output,ch->color);
  20695. }
  20696. script_pushint(st,1);
  20697. return SCRIPT_CMD_SUCCESS;
  20698. }
  20699. /**
  20700. * Ban player from a channel
  20701. * channel_unban "<chname>",<char_id>;
  20702. * @author [Cydh]
  20703. **/
  20704. BUILDIN_FUNC(channel_unban) {
  20705. struct Channel *ch = NULL;
  20706. const char *chname = script_getstr(st,2);
  20707. unsigned int char_id = script_getnum(st,3);
  20708. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20709. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  20710. script_pushint(st,0);
  20711. return SCRIPT_CMD_FAILURE;
  20712. }
  20713. if (ch->char_id == char_id) {
  20714. script_pushint(st,0);
  20715. return SCRIPT_CMD_SUCCESS;
  20716. }
  20717. if (idb_exists(ch->banned, char_id)) {
  20718. char output[CHAT_SIZE_MAX+1];
  20719. TBL_PC *tsd = map_charid2sd(char_id);
  20720. if (tsd)
  20721. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  20722. else
  20723. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  20724. idb_remove(ch->banned, char_id);
  20725. clif_channel_msg(ch,output,ch->color);
  20726. }
  20727. script_pushint(st,1);
  20728. return SCRIPT_CMD_SUCCESS;
  20729. }
  20730. /**
  20731. * Kick player from a channel
  20732. * channel_kick "<chname>",<char_id>;
  20733. * channel_kick "<chname>","<char_name>";
  20734. * @author [Cydh]
  20735. **/
  20736. BUILDIN_FUNC(channel_kick) {
  20737. struct Channel *ch = NULL;
  20738. const char *chname = script_getstr(st,2);
  20739. TBL_PC *tsd = NULL;
  20740. int res = 1;
  20741. if (script_isstring(st, 3)) {
  20742. const char *name = script_getstr(st, 3);
  20743. if (!(tsd = map_nick2sd(name,false))) {
  20744. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  20745. script_pushint(st,0);
  20746. return SCRIPT_CMD_FAILURE;
  20747. }
  20748. } else {
  20749. int char_id = script_getnum(st, 3);
  20750. if (!(tsd = map_charid2sd(char_id))) {
  20751. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  20752. script_pushint(st,0);
  20753. return SCRIPT_CMD_FAILURE;
  20754. }
  20755. }
  20756. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  20757. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  20758. script_pushint(st,0);
  20759. return SCRIPT_CMD_FAILURE;
  20760. }
  20761. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20762. script_pushint(st,0);
  20763. return SCRIPT_CMD_SUCCESS;
  20764. }
  20765. switch(ch->type){
  20766. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  20767. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  20768. default: res = channel_clean(ch,tsd,0); break;
  20769. }
  20770. if (res == 0) {
  20771. char output[CHAT_SIZE_MAX+1];
  20772. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  20773. clif_channel_msg(ch,output,ch->color);
  20774. }
  20775. script_pushint(st,1);
  20776. return SCRIPT_CMD_SUCCESS;
  20777. }
  20778. /**
  20779. * Delete a channel
  20780. * channel_delete "<chname>";
  20781. * @author [Cydh]
  20782. **/
  20783. BUILDIN_FUNC(channel_delete) {
  20784. struct Channel *ch = NULL;
  20785. const char *chname = script_getstr(st,2);
  20786. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20787. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  20788. script_pushint(st,-1);
  20789. return SCRIPT_CMD_FAILURE;
  20790. }
  20791. script_pushint(st,channel_delete(ch,true));
  20792. return SCRIPT_CMD_SUCCESS;
  20793. }
  20794. BUILDIN_FUNC(unloadnpc) {
  20795. const char *name;
  20796. struct npc_data* nd;
  20797. name = script_getstr(st, 2);
  20798. nd = npc_name2id(name);
  20799. if( nd == NULL ){
  20800. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  20801. return SCRIPT_CMD_FAILURE;
  20802. } else if ( nd->bl.id == st->oid ) {
  20803. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  20804. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  20805. return SCRIPT_CMD_FAILURE;
  20806. }
  20807. npc_unload_duplicates(nd);
  20808. npc_unload(nd, true);
  20809. npc_read_event_script();
  20810. return SCRIPT_CMD_SUCCESS;
  20811. }
  20812. /**
  20813. * Add an achievement to the player's log
  20814. * achievementadd(<achievement ID>{,<char ID>});
  20815. */
  20816. BUILDIN_FUNC(achievementadd) {
  20817. struct map_session_data *sd;
  20818. int achievement_id = script_getnum(st, 2);
  20819. if (!script_charid2sd(3, sd)) {
  20820. script_pushint(st, false);
  20821. return SCRIPT_CMD_FAILURE;
  20822. }
  20823. if (achievement_db.exists(achievement_id) == false) {
  20824. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  20825. script_pushint(st, false);
  20826. return SCRIPT_CMD_FAILURE;
  20827. }
  20828. if( !sd->state.pc_loaded ){
  20829. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20830. return SCRIPT_CMD_SUCCESS;
  20831. }
  20832. if (achievement_add(sd, achievement_id))
  20833. script_pushint(st, true);
  20834. else
  20835. script_pushint(st, false);
  20836. return SCRIPT_CMD_SUCCESS;
  20837. }
  20838. /**
  20839. * Removes an achievement on a player.
  20840. * achievementremove(<achievement ID>{,<char ID>});
  20841. * Just for Atemo. ;)
  20842. */
  20843. BUILDIN_FUNC(achievementremove) {
  20844. struct map_session_data *sd;
  20845. int achievement_id = script_getnum(st, 2);
  20846. if (!script_charid2sd(3, sd)) {
  20847. script_pushint(st, false);
  20848. return SCRIPT_CMD_FAILURE;
  20849. }
  20850. if (achievement_db.exists(achievement_id) == false) {
  20851. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  20852. script_pushint(st, false);
  20853. return SCRIPT_CMD_SUCCESS;
  20854. }
  20855. if( !sd->state.pc_loaded ){
  20856. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20857. return SCRIPT_CMD_SUCCESS;
  20858. }
  20859. if (achievement_remove(sd, achievement_id))
  20860. script_pushint(st, true);
  20861. else
  20862. script_pushint(st, false);
  20863. return SCRIPT_CMD_SUCCESS;
  20864. }
  20865. /**
  20866. * Returns achievement progress
  20867. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  20868. */
  20869. BUILDIN_FUNC(achievementinfo) {
  20870. struct map_session_data *sd;
  20871. int achievement_id = script_getnum(st, 2);
  20872. if (!script_charid2sd(4, sd)) {
  20873. script_pushint(st, false);
  20874. return SCRIPT_CMD_FAILURE;
  20875. }
  20876. if (achievement_db.exists(achievement_id) == false) {
  20877. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  20878. script_pushint(st, false);
  20879. return SCRIPT_CMD_FAILURE;
  20880. }
  20881. if( !sd->state.pc_loaded ){
  20882. script_pushint(st, false);
  20883. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20884. return SCRIPT_CMD_SUCCESS;
  20885. }
  20886. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  20887. return SCRIPT_CMD_SUCCESS;
  20888. }
  20889. /**
  20890. * Award an achievement; Ignores requirements
  20891. * achievementcomplete(<achievement ID>{,<char ID>});
  20892. */
  20893. BUILDIN_FUNC(achievementcomplete) {
  20894. struct map_session_data *sd;
  20895. int i, achievement_id = script_getnum(st, 2);
  20896. if (!script_charid2sd(3, sd)) {
  20897. script_pushint(st, false);
  20898. return SCRIPT_CMD_FAILURE;
  20899. }
  20900. if (achievement_db.exists(achievement_id) == false) {
  20901. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  20902. script_pushint(st, false);
  20903. return SCRIPT_CMD_FAILURE;
  20904. }
  20905. if( !sd->state.pc_loaded ){
  20906. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20907. return SCRIPT_CMD_SUCCESS;
  20908. }
  20909. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20910. if (i == sd->achievement_data.count)
  20911. achievement_add(sd, achievement_id);
  20912. achievement_update_achievement(sd, achievement_id, true);
  20913. script_pushint(st, true);
  20914. return SCRIPT_CMD_SUCCESS;
  20915. }
  20916. /**
  20917. * Checks if the achievement exists on player.
  20918. * achievementexists(<achievement ID>{,<char ID>});
  20919. */
  20920. BUILDIN_FUNC(achievementexists) {
  20921. struct map_session_data *sd;
  20922. int i, achievement_id = script_getnum(st, 2);
  20923. if (!script_charid2sd(3, sd)) {
  20924. script_pushint(st, false);
  20925. return SCRIPT_CMD_FAILURE;
  20926. }
  20927. if (achievement_db.exists(achievement_id) == false) {
  20928. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  20929. script_pushint(st, false);
  20930. return SCRIPT_CMD_SUCCESS;
  20931. }
  20932. if( !sd->state.pc_loaded ){
  20933. script_pushint(st, false);
  20934. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20935. return SCRIPT_CMD_SUCCESS;
  20936. }
  20937. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  20938. script_pushint(st, i < sd->achievement_data.count ? true : false);
  20939. return SCRIPT_CMD_SUCCESS;
  20940. }
  20941. /**
  20942. * Updates an achievement's value.
  20943. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  20944. */
  20945. BUILDIN_FUNC(achievementupdate) {
  20946. struct map_session_data *sd;
  20947. int i, achievement_id, type, value;
  20948. achievement_id = script_getnum(st, 2);
  20949. type = script_getnum(st, 3);
  20950. value = script_getnum(st, 4);
  20951. if (!script_charid2sd(5, sd)) {
  20952. script_pushint(st, false);
  20953. return SCRIPT_CMD_FAILURE;
  20954. }
  20955. if (achievement_db.exists(achievement_id) == false) {
  20956. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  20957. script_pushint(st, false);
  20958. return SCRIPT_CMD_FAILURE;
  20959. }
  20960. if( !sd->state.pc_loaded ){
  20961. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20962. return SCRIPT_CMD_SUCCESS;
  20963. }
  20964. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20965. if (i == sd->achievement_data.count)
  20966. achievement_add(sd, achievement_id);
  20967. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20968. if (i == sd->achievement_data.count) {
  20969. script_pushint(st, false);
  20970. return SCRIPT_CMD_SUCCESS;
  20971. }
  20972. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  20973. sd->achievement_data.achievements[i].count[type - 1] = value;
  20974. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  20975. sd->achievement_data.achievements[i].completed = value;
  20976. else if (type == ACHIEVEINFO_GOTREWARD)
  20977. sd->achievement_data.achievements[i].rewarded = value;
  20978. else {
  20979. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  20980. script_pushint(st, false);
  20981. return SCRIPT_CMD_FAILURE;
  20982. }
  20983. achievement_update_achievement(sd, achievement_id, false);
  20984. script_pushint(st, true);
  20985. return SCRIPT_CMD_SUCCESS;
  20986. }
  20987. /**
  20988. * Get an equipment's refine cost
  20989. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  20990. * returns -1 on fail
  20991. */
  20992. BUILDIN_FUNC(getequiprefinecost) {
  20993. int i = -1, slot, type, info;
  20994. map_session_data *sd;
  20995. slot = script_getnum(st, 2);
  20996. type = script_getnum(st, 3);
  20997. info = script_getnum(st, 4);
  20998. if (!script_charid2sd(5, sd)) {
  20999. script_pushint(st, 0);
  21000. return SCRIPT_CMD_FAILURE;
  21001. }
  21002. if (type < 0 || type >= REFINE_COST_MAX) {
  21003. script_pushint(st, -1);
  21004. return SCRIPT_CMD_SUCCESS;
  21005. }
  21006. if (equip_index_check(slot))
  21007. i = pc_checkequip(sd, equip_bitmask[slot]);
  21008. if (i < 0) {
  21009. script_pushint(st, -1);
  21010. return SCRIPT_CMD_SUCCESS;
  21011. }
  21012. if( sd->inventory_data[i] == nullptr ){
  21013. script_pushint( st, -1 );
  21014. return SCRIPT_CMD_SUCCESS;
  21015. }
  21016. std::shared_ptr<s_refine_level_info> level_info = refine_db.findLevelInfo( *sd->inventory_data[i], sd->inventory.u.items_inventory[i] );
  21017. if( level_info == nullptr ){
  21018. script_pushint( st, -1 );
  21019. return SCRIPT_CMD_SUCCESS;
  21020. }
  21021. std::shared_ptr<s_refine_cost> cost = util::umap_find( level_info->costs, (uint16)type );
  21022. if( cost == nullptr ){
  21023. script_pushint( st, -1 );
  21024. return SCRIPT_CMD_SUCCESS;
  21025. }
  21026. switch( info ){
  21027. case REFINE_MATERIAL_ID:
  21028. script_pushint( st, cost->nameid );
  21029. break;
  21030. case REFINE_ZENY_COST:
  21031. script_pushint( st, cost->zeny );
  21032. break;
  21033. default:
  21034. script_pushint( st, -1 );
  21035. return SCRIPT_CMD_FAILURE;
  21036. }
  21037. return SCRIPT_CMD_SUCCESS;
  21038. }
  21039. /**
  21040. * Round, floor, ceiling a number to arbitrary integer precision.
  21041. * round(<number>,<precision>);
  21042. * ceil(<number>,<precision>);
  21043. * floor(<number>,<precision>);
  21044. */
  21045. BUILDIN_FUNC(round) {
  21046. int num = script_getnum(st, 2);
  21047. int precision = script_getnum(st, 3);
  21048. char* func = script_getfuncname(st);
  21049. if (precision <= 0) {
  21050. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  21051. return SCRIPT_CMD_FAILURE;
  21052. }
  21053. if (strcasecmp(func, "floor") == 0) {
  21054. script_pushint(st, num - (num % precision));
  21055. }
  21056. else if (strcasecmp(func, "ceil") == 0) {
  21057. script_pushint(st, num + precision - (num % precision));
  21058. }
  21059. else {
  21060. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  21061. }
  21062. return SCRIPT_CMD_SUCCESS;
  21063. }
  21064. BUILDIN_FUNC(getequiptradability) {
  21065. int i, num;
  21066. TBL_PC *sd;
  21067. num = script_getnum(st, 2);
  21068. if (!script_charid2sd(3, sd)) {
  21069. return SCRIPT_CMD_FAILURE;
  21070. }
  21071. if (equip_index_check(num))
  21072. i = pc_checkequip(sd, equip_bitmask[num]);
  21073. else{
  21074. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  21075. return SCRIPT_CMD_FAILURE;
  21076. }
  21077. if (i >= 0)
  21078. script_pushint(st, pc_can_trade_item(sd, i));
  21079. else
  21080. script_pushint(st, false);
  21081. return SCRIPT_CMD_SUCCESS;
  21082. }
  21083. 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 ){
  21084. const char *name;
  21085. // Check if it is a variable
  21086. if( !data_isreference(data) ){
  21087. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  21088. return false;
  21089. }
  21090. name = reference_getname(data);
  21091. if( is_string_variable(name) ){
  21092. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  21093. return false;
  21094. }
  21095. // Check if the variable requires a player
  21096. if( not_server_variable(*name) && sd == NULL ){
  21097. // If no player is attached
  21098. if( !script_rid2sd(sd) ){
  21099. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  21100. return false;
  21101. }
  21102. }
  21103. // Try to find the array's source pointer
  21104. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  21105. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  21106. return false;
  21107. }
  21108. // Store the name for later usage
  21109. *out_name = name;
  21110. // Get the start and end indices of the array
  21111. *start = reference_getindex(data);
  21112. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  21113. // For getting the values we need the id of the array
  21114. *id = reference_getid(data);
  21115. return true;
  21116. }
  21117. // 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>}}}}}}}}};
  21118. BUILDIN_FUNC(mail){
  21119. const char *sender, *title, *body, *name;
  21120. struct mail_message msg;
  21121. struct script_data *data;
  21122. struct map_session_data *sd = NULL;
  21123. unsigned int i, j, k, num_items, start, end;
  21124. int32 id;
  21125. memset(&msg, 0, sizeof(struct mail_message));
  21126. msg.dest_id = script_getnum(st,2);
  21127. sender = script_getstr(st, 3);
  21128. if( strlen(sender) > NAME_LENGTH ){
  21129. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  21130. return SCRIPT_CMD_FAILURE;
  21131. }
  21132. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  21133. title = script_getstr(st, 4);
  21134. if( strlen(title) > MAIL_TITLE_LENGTH ){
  21135. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  21136. return SCRIPT_CMD_FAILURE;
  21137. }
  21138. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  21139. body = script_getstr(st, 5);
  21140. if( strlen(body) > MAIL_BODY_LENGTH ){
  21141. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  21142. return SCRIPT_CMD_FAILURE;
  21143. }
  21144. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  21145. if( script_hasdata(st,6) ){
  21146. int zeny = script_getnum(st, 6);
  21147. if( zeny < 0 ){
  21148. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  21149. return SCRIPT_CMD_FAILURE;
  21150. }else if( zeny > MAX_ZENY ){
  21151. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  21152. zeny = MAX_ZENY;
  21153. }
  21154. msg.zeny = zeny;
  21155. }
  21156. // Items
  21157. num_items = 0;
  21158. while( script_hasdata(st,7) ){
  21159. data = script_getdata(st,7);
  21160. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  21161. return SCRIPT_CMD_FAILURE;
  21162. }
  21163. num_items = end - start;
  21164. if( num_items == 0 ){
  21165. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  21166. break;
  21167. }
  21168. if( num_items > MAIL_MAX_ITEM ){
  21169. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  21170. num_items = MAIL_MAX_ITEM;
  21171. }
  21172. for( i = 0; i < num_items && start < end; i++, start++ ){
  21173. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21174. msg.item[i].identify = 1;
  21175. if( !itemdb_exists(msg.item[i].nameid) ){
  21176. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  21177. return SCRIPT_CMD_FAILURE;
  21178. }
  21179. }
  21180. // Amounts
  21181. if( !script_hasdata(st,8) ){
  21182. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  21183. return SCRIPT_CMD_FAILURE;
  21184. }
  21185. data = script_getdata(st,8);
  21186. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  21187. return SCRIPT_CMD_FAILURE;
  21188. }
  21189. for( i = 0; i < num_items && start < end; i++, start++ ){
  21190. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21191. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21192. if( msg.item[i].amount <= 0 ){
  21193. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  21194. return SCRIPT_CMD_FAILURE;
  21195. }else if( itemdb_isstackable2(item) ){
  21196. uint16 max = item->stack.amount > 0 ? item->stack.amount : MAX_AMOUNT;
  21197. if( msg.item[i].amount > max ){
  21198. 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 );
  21199. msg.item[i].amount = max;
  21200. }
  21201. }else{
  21202. if( msg.item[i].amount > 1 ){
  21203. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  21204. msg.item[i].amount = 1;
  21205. }
  21206. }
  21207. }
  21208. // Refine
  21209. if (!script_hasdata(st, 9)) {
  21210. break;
  21211. }
  21212. data = script_getdata(st, 9);
  21213. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  21214. return SCRIPT_CMD_FAILURE;
  21215. }
  21216. for (i = 0; i < num_items && start < end; i++, start++) {
  21217. struct item_data* item = itemdb_exists(msg.item[i].nameid);
  21218. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21219. script_removetop(st, -1, 0);
  21220. if (!item->flag.no_refine && (item->type == IT_WEAPON || item->type == IT_ARMOR || item->type == IT_SHADOWGEAR)) {
  21221. if (msg.item[i].refine > MAX_REFINE)
  21222. msg.item[i].refine = MAX_REFINE;
  21223. }
  21224. else
  21225. msg.item[i].refine = 0;
  21226. if (msg.item[i].refine < 0)
  21227. msg.item[i].refine = 0;
  21228. }
  21229. // Bound
  21230. if (!script_hasdata(st, 10)) {
  21231. break;
  21232. }
  21233. data = script_getdata(st,10);
  21234. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  21235. return SCRIPT_CMD_FAILURE;
  21236. }
  21237. for( i = 0; i < num_items && start < end; i++, start++ ){
  21238. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21239. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21240. script_removetop(st, -1, 0);
  21241. if( msg.item[i].bound <= BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  21242. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  21243. return SCRIPT_CMD_FAILURE;
  21244. }
  21245. }
  21246. // Cards
  21247. if( !script_hasdata(st,11) ){
  21248. break;
  21249. }
  21250. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  21251. data = script_getdata(st,j);
  21252. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21253. return SCRIPT_CMD_FAILURE;
  21254. }
  21255. for( k = 0; k < num_items && start < end; k++, start++ ){
  21256. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21257. if( msg.item[k].card[i] != 0 && !itemdb_exists(msg.item[k].card[i]) ){
  21258. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  21259. return SCRIPT_CMD_FAILURE;
  21260. }
  21261. }
  21262. }
  21263. // Random Options
  21264. if( !script_hasdata(st,12 + MAX_SLOTS) ){
  21265. break;
  21266. }
  21267. for( i = 0, j = 12 + MAX_SLOTS; i < MAX_ITEM_RDM_OPT && script_hasdata(st,j) && script_hasdata(st,j + 1) && script_hasdata(st,j + 2); i++, j++ ){
  21268. // Option IDs
  21269. data = script_getdata(st, j);
  21270. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21271. return SCRIPT_CMD_FAILURE;
  21272. }
  21273. for( k = 0; k < num_items && start < end; k++, start++ ){
  21274. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21275. }
  21276. j++;
  21277. // Option values
  21278. data = script_getdata(st, j);
  21279. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21280. return SCRIPT_CMD_FAILURE;
  21281. }
  21282. for( k = 0; k < num_items && start < end; k++, start++ ){
  21283. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21284. }
  21285. j++;
  21286. // Option parameters
  21287. data = script_getdata(st, j);
  21288. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21289. return SCRIPT_CMD_FAILURE;
  21290. }
  21291. for( k = 0; k < num_items && start < end; k++, start++ ){
  21292. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21293. }
  21294. }
  21295. // Break the pseudo scope
  21296. break;
  21297. }
  21298. msg.status = MAIL_NEW;
  21299. msg.type = MAIL_INBOX_NORMAL;
  21300. msg.timestamp = time(NULL);
  21301. intif_Mail_send(0, &msg);
  21302. return SCRIPT_CMD_SUCCESS;
  21303. }
  21304. BUILDIN_FUNC(open_roulette){
  21305. #if PACKETVER < 20141022
  21306. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  21307. return SCRIPT_CMD_FAILURE;
  21308. #else
  21309. struct map_session_data* sd;
  21310. if( !battle_config.feature_roulette ){
  21311. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  21312. return SCRIPT_CMD_FAILURE;
  21313. }
  21314. if( !script_charid2sd( 2, sd ) ){
  21315. return SCRIPT_CMD_FAILURE;
  21316. }
  21317. clif_roulette_open(sd);
  21318. return SCRIPT_CMD_SUCCESS;
  21319. #endif
  21320. }
  21321. /*==========================================
  21322. * identifyall({<type>{,<account_id>}})
  21323. * <type>:
  21324. * true: identify the items and returns the count of unidentified items (default)
  21325. * false: returns the count of unidentified items only
  21326. *------------------------------------------*/
  21327. BUILDIN_FUNC(identifyall) {
  21328. TBL_PC *sd;
  21329. bool identify_item = true;
  21330. if (script_hasdata(st, 2))
  21331. identify_item = script_getnum(st, 2) != 0;
  21332. if( !script_hasdata(st, 3) )
  21333. script_rid2sd(sd);
  21334. else
  21335. sd = map_id2sd( script_getnum(st, 3) );
  21336. if (sd == NULL) {
  21337. script_pushint(st, -1);
  21338. return SCRIPT_CMD_SUCCESS;
  21339. }
  21340. script_pushint(st, pc_identifyall(sd, identify_item));
  21341. return SCRIPT_CMD_SUCCESS;
  21342. }
  21343. BUILDIN_FUNC(is_guild_leader)
  21344. {
  21345. struct map_session_data* sd;
  21346. struct guild* guild_data;
  21347. int guild_id;
  21348. if (!script_rid2sd(sd)) {
  21349. script_pushint(st, false);
  21350. return SCRIPT_CMD_FAILURE;
  21351. }
  21352. if (script_hasdata(st, 2))
  21353. guild_id = script_getnum(st, 2);
  21354. else
  21355. guild_id = sd->status.guild_id;
  21356. guild_data = guild_search(guild_id);
  21357. if (guild_data)
  21358. script_pushint(st, (guild_data->member[0].char_id == sd->status.char_id));
  21359. else
  21360. script_pushint(st, false);
  21361. return SCRIPT_CMD_SUCCESS;
  21362. }
  21363. BUILDIN_FUNC(is_party_leader)
  21364. {
  21365. struct map_session_data* sd;
  21366. struct party_data* p_data;
  21367. int p_id, i = 0;
  21368. if (!script_rid2sd(sd)) {
  21369. script_pushint(st, false);
  21370. return SCRIPT_CMD_FAILURE;
  21371. }
  21372. if (script_hasdata(st, 2))
  21373. p_id = script_getnum(st, 2);
  21374. else
  21375. p_id = sd->status.party_id;
  21376. p_data = party_search(p_id);
  21377. if (p_data) {
  21378. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  21379. if (i < MAX_PARTY){
  21380. script_pushint(st, p_data->party.member[i].leader);
  21381. return SCRIPT_CMD_SUCCESS;
  21382. }
  21383. }
  21384. script_pushint(st, false);
  21385. return SCRIPT_CMD_SUCCESS;
  21386. }
  21387. BUILDIN_FUNC( camerainfo ){
  21388. #if PACKETVER < 20160525
  21389. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  21390. return SCRIPT_CMD_FAILURE;
  21391. #else
  21392. struct map_session_data* sd;
  21393. if( !script_charid2sd( 5, sd ) ){
  21394. return SCRIPT_CMD_FAILURE;
  21395. }
  21396. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  21397. return SCRIPT_CMD_SUCCESS;
  21398. #endif
  21399. }
  21400. // This function is only meant to be used inside of achievement conditions
  21401. BUILDIN_FUNC(achievement_condition){
  21402. // Push what we get from the script
  21403. script_pushint( st, 2 );
  21404. // Otherwise the script is freed afterwards
  21405. st->state = RERUNLINE;
  21406. return SCRIPT_CMD_SUCCESS;
  21407. }
  21408. /// Returns a reference to a variable of the specific instance ID.
  21409. /// Returns 0 if an error occurs.
  21410. ///
  21411. /// getvariableofinstance(<variable>, <instance ID>) -> <reference>
  21412. BUILDIN_FUNC(getvariableofinstance)
  21413. {
  21414. struct script_data* data = script_getdata(st, 2);
  21415. if (!data_isreference(data)) {
  21416. ShowError("buildin_getvariableofinstance: %s is not a variable.\n", script_getstr(st, 2));
  21417. script_reportdata(data);
  21418. script_pushnil(st);
  21419. st->state = END;
  21420. return SCRIPT_CMD_FAILURE;
  21421. }
  21422. const char* name = reference_getname(data);
  21423. if (*name != '\'') {
  21424. ShowError("buildin_getvariableofinstance: Invalid scope. %s is not an instance variable.\n", name);
  21425. script_reportdata(data);
  21426. script_pushnil(st);
  21427. st->state = END;
  21428. return SCRIPT_CMD_FAILURE;
  21429. }
  21430. int instance_id = script_getnum(st, 3);
  21431. if (instance_id <= 0) {
  21432. ShowError("buildin_getvariableofinstance: Invalid instance ID %d.\n", instance_id);
  21433. script_pushnil(st);
  21434. st->state = END;
  21435. return SCRIPT_CMD_FAILURE;
  21436. }
  21437. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  21438. if (im->state != INSTANCE_BUSY) {
  21439. ShowError("buildin_getvariableofinstance: Unknown instance ID %d.\n", instance_id);
  21440. script_pushnil(st);
  21441. st->state = END;
  21442. return SCRIPT_CMD_FAILURE;
  21443. }
  21444. if (!im->regs.vars)
  21445. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  21446. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  21447. return SCRIPT_CMD_SUCCESS;
  21448. }
  21449. /*
  21450. convertpcinfo(<char_id>,<type>)
  21451. convertpcinfo(<account_id>,<type>)
  21452. convertpcinfo(<player_name>,<type>)
  21453. */
  21454. BUILDIN_FUNC(convertpcinfo) {
  21455. TBL_PC *sd;
  21456. if (script_isstring(st, 2))
  21457. sd = map_nick2sd(script_getstr(st, 2),false);
  21458. else {
  21459. int id = script_getnum(st, 2);
  21460. sd = map_id2sd(id);
  21461. if (!sd)
  21462. sd = map_charid2sd(id);
  21463. }
  21464. int type = script_getnum(st, 3);
  21465. switch (type) {
  21466. case CPC_NAME:
  21467. case CPC_CHAR:
  21468. case CPC_ACCOUNT:
  21469. break;
  21470. default:
  21471. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  21472. script_pushnil(st);
  21473. st->state = END;
  21474. return SCRIPT_CMD_FAILURE;
  21475. }
  21476. if (!sd) {
  21477. if (type == CPC_NAME)
  21478. script_pushstrcopy(st, "");
  21479. else
  21480. script_pushint(st, 0);
  21481. return SCRIPT_CMD_SUCCESS;
  21482. }
  21483. switch (type) {
  21484. case CPC_NAME:
  21485. script_pushstrcopy(st, sd->status.name);
  21486. break;
  21487. case CPC_CHAR:
  21488. script_pushint(st, sd->status.char_id);
  21489. break;
  21490. case CPC_ACCOUNT:
  21491. script_pushint(st, sd->status.account_id);
  21492. break;
  21493. }
  21494. return SCRIPT_CMD_SUCCESS;
  21495. }
  21496. BUILDIN_FUNC(cloakoffnpc)
  21497. {
  21498. if (npc_enable_target(script_getstr(st, 2), script_hasdata(st, 3) ? script_getnum(st, 3) : 0, 8))
  21499. return SCRIPT_CMD_SUCCESS;
  21500. return SCRIPT_CMD_FAILURE;
  21501. }
  21502. BUILDIN_FUNC(cloakonnpc)
  21503. {
  21504. if (npc_enable_target(script_getstr(st, 2), script_hasdata(st, 3) ? script_getnum(st, 3) : 0, 16))
  21505. return SCRIPT_CMD_SUCCESS;
  21506. return SCRIPT_CMD_FAILURE;
  21507. }
  21508. BUILDIN_FUNC(isnpccloaked)
  21509. {
  21510. struct npc_data *nd = npc_name2id(script_getstr(st, 2));
  21511. if (!nd) {
  21512. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  21513. return SCRIPT_CMD_FAILURE;
  21514. }
  21515. map_session_data* sd;
  21516. if (!script_charid2sd(3, sd))
  21517. return SCRIPT_CMD_FAILURE;
  21518. script_pushint(st, npc_is_cloaked(nd, sd));
  21519. return SCRIPT_CMD_SUCCESS;
  21520. }
  21521. BUILDIN_FUNC(refineui){
  21522. #if PACKETVER < 20161012
  21523. ShowError( "buildin_refineui: This command requires packet version 2016-10-12 or newer.\n" );
  21524. return SCRIPT_CMD_FAILURE;
  21525. #else
  21526. struct map_session_data* sd;
  21527. if( !script_charid2sd(2,sd) ){
  21528. return SCRIPT_CMD_FAILURE;
  21529. }
  21530. if( !battle_config.feature_refineui ){
  21531. ShowError( "buildin_refineui: This command is disabled via configuration.\n" );
  21532. return SCRIPT_CMD_FAILURE;
  21533. }
  21534. if( !sd->state.refineui_open ){
  21535. clif_refineui_open(sd);
  21536. }
  21537. return SCRIPT_CMD_SUCCESS;
  21538. #endif
  21539. }
  21540. #include "../custom/script.inc"
  21541. // declarations that were supposed to be exported from npc_chat.cpp
  21542. #ifdef PCRE_SUPPORT
  21543. BUILDIN_FUNC(defpattern);
  21544. BUILDIN_FUNC(activatepset);
  21545. BUILDIN_FUNC(deactivatepset);
  21546. BUILDIN_FUNC(deletepset);
  21547. #endif
  21548. /** Regular expression matching
  21549. * preg_match(<pattern>,<string>{,<offset>})
  21550. */
  21551. BUILDIN_FUNC(preg_match) {
  21552. #ifdef PCRE_SUPPORT
  21553. pcre *re;
  21554. pcre_extra *pcreExtra;
  21555. const char *error;
  21556. int erroffset, r, offset = 0;
  21557. int subStrVec[30];
  21558. const char* pattern = script_getstr(st,2);
  21559. const char* subject = script_getstr(st,3);
  21560. if (script_hasdata(st,4))
  21561. offset = script_getnum(st,4);
  21562. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  21563. pcreExtra = pcre_study(re, 0, &error);
  21564. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  21565. pcre_free(re);
  21566. if (pcreExtra != NULL)
  21567. pcre_free(pcreExtra);
  21568. if (r < 0)
  21569. script_pushint(st,0);
  21570. else
  21571. script_pushint(st,(r > 0) ? r : 30 / 3);
  21572. return SCRIPT_CMD_SUCCESS;
  21573. #else
  21574. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  21575. script_pushint(st,0);
  21576. return SCRIPT_CMD_SUCCESS;
  21577. #endif
  21578. }
  21579. /// script command definitions
  21580. /// for an explanation on args, see add_buildin_func
  21581. struct script_function buildin_func[] = {
  21582. // NPC interaction
  21583. BUILDIN_DEF(mes,"s*"),
  21584. BUILDIN_DEF(next,""),
  21585. BUILDIN_DEF(clear,""),
  21586. BUILDIN_DEF(close,""),
  21587. BUILDIN_DEF(close2,""),
  21588. BUILDIN_DEF(menu,"sl*"),
  21589. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  21590. BUILDIN_DEF(prompt,"s*"),
  21591. //
  21592. BUILDIN_DEF(goto,"l"),
  21593. BUILDIN_DEF(callsub,"l*"),
  21594. BUILDIN_DEF(callfunc,"s*"),
  21595. BUILDIN_DEF(return,"?"),
  21596. BUILDIN_DEF(getarg,"i?"),
  21597. BUILDIN_DEF(jobchange,"i??"),
  21598. BUILDIN_DEF(jobname,"i"),
  21599. BUILDIN_DEF(input,"r??"),
  21600. BUILDIN_DEF(warp,"sii?"),
  21601. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  21602. BUILDIN_DEF(areawarp,"siiiisii??"),
  21603. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  21604. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  21605. BUILDIN_DEF(setlook,"ii?"),
  21606. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  21607. BUILDIN_DEF2(setr,"set","rv?"),
  21608. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  21609. BUILDIN_DEF(setarray,"rv*"),
  21610. BUILDIN_DEF(cleararray,"rvi"),
  21611. BUILDIN_DEF(copyarray,"rri"),
  21612. BUILDIN_DEF(getarraysize,"r"),
  21613. BUILDIN_DEF(deletearray,"r?"),
  21614. BUILDIN_DEF(getelementofarray,"ri"),
  21615. BUILDIN_DEF(inarray,"rv"),
  21616. BUILDIN_DEF(countinarray,"rr"),
  21617. BUILDIN_DEF(getitem,"vi?"),
  21618. BUILDIN_DEF(rentitem,"vi?"),
  21619. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  21620. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  21621. BUILDIN_DEF(getnameditem,"vv"),
  21622. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  21623. BUILDIN_DEF(makeitem,"visii"),
  21624. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  21625. BUILDIN_DEF(delitem,"vi?"),
  21626. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  21627. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  21628. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  21629. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  21630. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  21631. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  21632. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  21633. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  21634. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  21635. BUILDIN_DEF(cutin,"si"),
  21636. BUILDIN_DEF(viewpoint,"iiiii?"),
  21637. BUILDIN_DEF(heal,"ii?"),
  21638. BUILDIN_DEF(itemheal,"ii?"),
  21639. BUILDIN_DEF(percentheal,"ii?"),
  21640. BUILDIN_DEF(rand,"i?"),
  21641. BUILDIN_DEF(countitem,"v?"),
  21642. BUILDIN_DEF(storagecountitem,"v?"),
  21643. BUILDIN_DEF(guildstoragecountitem,"v?"),
  21644. BUILDIN_DEF(cartcountitem,"v?"),
  21645. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  21646. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  21647. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  21648. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  21649. BUILDIN_DEF(checkweight,"vi*"),
  21650. BUILDIN_DEF(checkweight2,"rr"),
  21651. BUILDIN_DEF(readparam,"i?"),
  21652. BUILDIN_DEF(getcharid,"i?"),
  21653. BUILDIN_DEF(getnpcid,"i?"),
  21654. BUILDIN_DEF(getpartyname,"i"),
  21655. BUILDIN_DEF(getpartymember,"i??"),
  21656. BUILDIN_DEF(getpartyleader,"i?"),
  21657. BUILDIN_DEF(getguildname,"i"),
  21658. BUILDIN_DEF(getguildmaster,"i"),
  21659. BUILDIN_DEF(getguildmasterid,"i"),
  21660. BUILDIN_DEF(strcharinfo,"i?"),
  21661. BUILDIN_DEF(strnpcinfo,"i"),
  21662. BUILDIN_DEF(getequipid,"??"),
  21663. BUILDIN_DEF(getequipuniqueid,"i?"),
  21664. BUILDIN_DEF(getequipname,"i?"),
  21665. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  21666. BUILDIN_DEF(repair,"i?"), // [Valaris]
  21667. BUILDIN_DEF(repairall,"?"),
  21668. BUILDIN_DEF(getequipisequiped,"i?"),
  21669. BUILDIN_DEF(getequipisenableref,"i?"),
  21670. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  21671. BUILDIN_DEF(getequipweaponlv,"i?"),
  21672. BUILDIN_DEF(getequippercentrefinery,"i?"),
  21673. BUILDIN_DEF(successrefitem,"i??"),
  21674. BUILDIN_DEF(failedrefitem,"i?"),
  21675. BUILDIN_DEF(downrefitem,"i??"),
  21676. BUILDIN_DEF(statusup,"i?"),
  21677. BUILDIN_DEF(statusup2,"ii?"),
  21678. BUILDIN_DEF(bonus,"i?"),
  21679. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  21680. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  21681. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  21682. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  21683. BUILDIN_DEF(autobonus,"sii??"),
  21684. BUILDIN_DEF(autobonus2,"sii??"),
  21685. BUILDIN_DEF(autobonus3,"siiv?"),
  21686. BUILDIN_DEF(skill,"vi?"),
  21687. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  21688. BUILDIN_DEF(guildskill,"vi"),
  21689. BUILDIN_DEF(getskilllv,"v"),
  21690. BUILDIN_DEF(getgdskilllv,"iv"),
  21691. BUILDIN_DEF(basicskillcheck,""),
  21692. BUILDIN_DEF(getgmlevel,"?"),
  21693. BUILDIN_DEF(getgroupid,"?"),
  21694. BUILDIN_DEF(end,""),
  21695. BUILDIN_DEF(checkoption,"i?"),
  21696. BUILDIN_DEF(setoption,"i??"),
  21697. BUILDIN_DEF(setcart,"??"),
  21698. BUILDIN_DEF(checkcart,"?"),
  21699. BUILDIN_DEF(setfalcon,"??"),
  21700. BUILDIN_DEF(checkfalcon,"?"),
  21701. BUILDIN_DEF(setriding,"??"),
  21702. BUILDIN_DEF(checkriding,"?"),
  21703. BUILDIN_DEF(checkwug,"?"),
  21704. BUILDIN_DEF(checkmadogear,"?"),
  21705. BUILDIN_DEF(setmadogear,"???"),
  21706. BUILDIN_DEF2(savepoint,"save","sii???"),
  21707. BUILDIN_DEF(savepoint,"sii???"),
  21708. BUILDIN_DEF(gettimetick,"i"),
  21709. BUILDIN_DEF(gettime,"i"),
  21710. BUILDIN_DEF(gettimestr,"si?"),
  21711. BUILDIN_DEF(openstorage,""),
  21712. BUILDIN_DEF(guildopenstorage,""),
  21713. BUILDIN_DEF(guildopenstorage_log,"?"),
  21714. BUILDIN_DEF(guild_has_permission,"i?"),
  21715. BUILDIN_DEF(itemskill,"vi?"),
  21716. BUILDIN_DEF(produce,"i"),
  21717. BUILDIN_DEF(cooking,"i"),
  21718. BUILDIN_DEF(monster,"siisii???"),
  21719. BUILDIN_DEF(getmobdrops,"i"),
  21720. BUILDIN_DEF(areamonster,"siiiisii???"),
  21721. BUILDIN_DEF(killmonster,"ss?"),
  21722. BUILDIN_DEF(killmonsterall,"s?"),
  21723. BUILDIN_DEF(clone,"siisi????"),
  21724. BUILDIN_DEF(doevent,"s"),
  21725. BUILDIN_DEF(donpcevent,"s"),
  21726. BUILDIN_DEF(cmdothernpc,"ss"),
  21727. BUILDIN_DEF(addtimer,"is"),
  21728. BUILDIN_DEF(deltimer,"s"),
  21729. BUILDIN_DEF(addtimercount,"is"),
  21730. BUILDIN_DEF(initnpctimer,"??"),
  21731. BUILDIN_DEF(stopnpctimer,"??"),
  21732. BUILDIN_DEF(startnpctimer,"??"),
  21733. BUILDIN_DEF(setnpctimer,"i?"),
  21734. BUILDIN_DEF(getnpctimer,"i?"),
  21735. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  21736. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  21737. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  21738. BUILDIN_DEF(announce,"si??????"),
  21739. BUILDIN_DEF(mapannounce,"ssi?????"),
  21740. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  21741. BUILDIN_DEF(getusers,"i"),
  21742. BUILDIN_DEF(getmapguildusers,"si"),
  21743. BUILDIN_DEF(getmapusers,"s"),
  21744. BUILDIN_DEF(getareausers,"siiii"),
  21745. BUILDIN_DEF(getunits, "i?"),
  21746. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  21747. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  21748. BUILDIN_DEF(getareadropitem,"siiiiv"),
  21749. BUILDIN_DEF(enablenpc,"s"),
  21750. BUILDIN_DEF(disablenpc,"s"),
  21751. BUILDIN_DEF(hideoffnpc,"s"),
  21752. BUILDIN_DEF(hideonnpc,"s"),
  21753. BUILDIN_DEF(sc_start,"iii???"),
  21754. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  21755. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  21756. BUILDIN_DEF(sc_end,"i?"),
  21757. BUILDIN_DEF(sc_end_class,"??"),
  21758. BUILDIN_DEF(getstatus, "i??"),
  21759. BUILDIN_DEF(getscrate,"ii?"),
  21760. BUILDIN_DEF(debugmes,"s"),
  21761. BUILDIN_DEF(errormes,"s"),
  21762. BUILDIN_DEF2(catchpet,"pet","i"),
  21763. BUILDIN_DEF2(birthpet,"bpet",""),
  21764. BUILDIN_DEF(catchpet,"i"),
  21765. BUILDIN_DEF(birthpet,""),
  21766. BUILDIN_DEF(resetlvl,"i?"),
  21767. BUILDIN_DEF(resetstatus,"?"),
  21768. BUILDIN_DEF(resetskill,"?"),
  21769. BUILDIN_DEF(resetfeel,"?"),
  21770. BUILDIN_DEF(resethate,"?"),
  21771. BUILDIN_DEF(skillpointcount,"?"),
  21772. BUILDIN_DEF(changebase,"i?"),
  21773. BUILDIN_DEF(changesex,"?"),
  21774. BUILDIN_DEF(changecharsex,"?"),
  21775. BUILDIN_DEF(waitingroom,"si?????"),
  21776. BUILDIN_DEF(delwaitingroom,"?"),
  21777. BUILDIN_DEF(waitingroomkick,"ss"),
  21778. BUILDIN_DEF(getwaitingroomusers, "?"),
  21779. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  21780. BUILDIN_DEF(enablewaitingroomevent,"?"),
  21781. BUILDIN_DEF(disablewaitingroomevent,"?"),
  21782. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  21783. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  21784. BUILDIN_DEF(getwaitingroomstate,"i?"),
  21785. BUILDIN_DEF(warpwaitingpc,"sii?"),
  21786. BUILDIN_DEF(attachrid,"i?"),
  21787. BUILDIN_DEF(addrid,"i?????"),
  21788. BUILDIN_DEF(detachrid,""),
  21789. BUILDIN_DEF(isloggedin,"i?"),
  21790. BUILDIN_DEF(setmapflagnosave,"ssii"),
  21791. BUILDIN_DEF(getmapflag,"si?"),
  21792. BUILDIN_DEF(setmapflag,"si??"),
  21793. BUILDIN_DEF(removemapflag,"si?"),
  21794. BUILDIN_DEF(pvpon,"s"),
  21795. BUILDIN_DEF(pvpoff,"s"),
  21796. BUILDIN_DEF(gvgon,"s"),
  21797. BUILDIN_DEF(gvgoff,"s"),
  21798. BUILDIN_DEF(emotion,"i?"),
  21799. BUILDIN_DEF(maprespawnguildid,"sii"),
  21800. BUILDIN_DEF(agitstart,""), // <Agit>
  21801. BUILDIN_DEF(agitend,""),
  21802. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  21803. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  21804. BUILDIN_DEF(getcastlename,"s"),
  21805. BUILDIN_DEF(getcastledata,"si"),
  21806. BUILDIN_DEF(setcastledata,"sii"),
  21807. BUILDIN_DEF(requestguildinfo,"i?"),
  21808. BUILDIN_DEF(getequipcardcnt,"i"),
  21809. BUILDIN_DEF(successremovecards,"i"),
  21810. BUILDIN_DEF(failedremovecards,"ii"),
  21811. BUILDIN_DEF(marriage,"s"),
  21812. BUILDIN_DEF2(wedding_effect,"wedding",""),
  21813. BUILDIN_DEF(divorce,"?"),
  21814. BUILDIN_DEF(ispartneron,"?"),
  21815. BUILDIN_DEF(getpartnerid,"?"),
  21816. BUILDIN_DEF(getchildid,"?"),
  21817. BUILDIN_DEF(getmotherid,"?"),
  21818. BUILDIN_DEF(getfatherid,"?"),
  21819. BUILDIN_DEF(warppartner,"sii"),
  21820. BUILDIN_DEF(getitemname,"v"),
  21821. BUILDIN_DEF(getitemslots,"i"),
  21822. BUILDIN_DEF(makepet,"i"),
  21823. BUILDIN_DEF(getexp,"ii?"),
  21824. BUILDIN_DEF(getinventorylist,"?"),
  21825. BUILDIN_DEF(getskilllist,"?"),
  21826. BUILDIN_DEF(clearitem,"?"),
  21827. BUILDIN_DEF(classchange,"i??"),
  21828. BUILDIN_DEF(misceffect,"i"),
  21829. BUILDIN_DEF(playBGM,"s"),
  21830. BUILDIN_DEF(playBGMall,"s?????"),
  21831. BUILDIN_DEF(soundeffect,"si"),
  21832. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  21833. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  21834. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  21835. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  21836. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  21837. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  21838. BUILDIN_DEF(petloot,"i"), // [Valaris]
  21839. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  21840. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  21841. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  21842. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  21843. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  21844. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  21845. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  21846. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  21847. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  21848. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  21849. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  21850. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  21851. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  21852. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  21853. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  21854. BUILDIN_DEF(mobcount,"ss"),
  21855. BUILDIN_DEF(getlook,"i?"),
  21856. BUILDIN_DEF(getsavepoint,"i?"),
  21857. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  21858. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  21859. BUILDIN_DEF(npcstop,""), // [Valaris]
  21860. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  21861. BUILDIN_DEF(mapid2name,"i"),
  21862. BUILDIN_DEF(checkoption1,"i?"),
  21863. BUILDIN_DEF(checkoption2,"i?"),
  21864. BUILDIN_DEF(guildgetexp,"i"),
  21865. BUILDIN_DEF(guildchangegm,"is"),
  21866. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  21867. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  21868. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  21869. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  21870. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  21871. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  21872. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  21873. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  21874. BUILDIN_DEF(night,""), // sets the server to night time
  21875. BUILDIN_DEF(day,""), // sets the server to day time
  21876. #ifdef PCRE_SUPPORT
  21877. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  21878. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  21879. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  21880. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  21881. #endif
  21882. BUILDIN_DEF(preg_match,"ss?"),
  21883. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  21884. BUILDIN_DEF(recovery,"i???"),
  21885. BUILDIN_DEF(getpetinfo,"i?"),
  21886. BUILDIN_DEF(gethominfo,"i?"),
  21887. BUILDIN_DEF(addhomintimacy,"i?"),
  21888. BUILDIN_DEF(getmercinfo,"i?"),
  21889. BUILDIN_DEF(checkequipedcard,"i"),
  21890. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  21891. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  21892. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  21893. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  21894. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  21895. BUILDIN_DEF(charat,"si"),
  21896. BUILDIN_DEF(setchar,"ssi"),
  21897. BUILDIN_DEF(insertchar,"ssi"),
  21898. BUILDIN_DEF(delchar,"si"),
  21899. BUILDIN_DEF(strtoupper,"s"),
  21900. BUILDIN_DEF(strtolower,"s"),
  21901. BUILDIN_DEF(charisupper, "si"),
  21902. BUILDIN_DEF(charislower, "si"),
  21903. BUILDIN_DEF(substr,"sii"),
  21904. BUILDIN_DEF(explode, "rss"),
  21905. BUILDIN_DEF(implode, "r?"),
  21906. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  21907. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  21908. BUILDIN_DEF(strpos,"ss?"),
  21909. BUILDIN_DEF(replacestr,"sss??"),
  21910. BUILDIN_DEF(countstr,"ss?"),
  21911. BUILDIN_DEF(setnpcdisplay,"sv??"),
  21912. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  21913. BUILDIN_DEF(strcmp,"ss"),
  21914. BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
  21915. BUILDIN_DEF(setiteminfo,"vii"), //[Lupus] set Items Buy / sell Price, etc info
  21916. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  21917. // [zBuffer] List of mathematics commands --->
  21918. BUILDIN_DEF(sqrt,"i"),
  21919. BUILDIN_DEF(pow,"ii"),
  21920. BUILDIN_DEF(distance,"iiii"),
  21921. BUILDIN_DEF2(minmax,"min", "*"),
  21922. BUILDIN_DEF2(minmax,"minimum", "*"),
  21923. BUILDIN_DEF2(minmax,"max", "*"),
  21924. BUILDIN_DEF2(minmax,"maximum", "*"),
  21925. // <--- [zBuffer] List of mathematics commands
  21926. BUILDIN_DEF(md5,"s"),
  21927. // [zBuffer] List of dynamic var commands --->
  21928. BUILDIN_DEF(getd,"s"),
  21929. BUILDIN_DEF(setd,"sv?"),
  21930. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  21931. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  21932. BUILDIN_DEF(npcshopadditem,"sii*"),
  21933. BUILDIN_DEF(npcshopdelitem,"si*"),
  21934. BUILDIN_DEF(npcshopattach,"s?"),
  21935. BUILDIN_DEF(equip,"i?"),
  21936. BUILDIN_DEF(autoequip,"ii"),
  21937. BUILDIN_DEF(setbattleflag,"si?"),
  21938. BUILDIN_DEF(getbattleflag,"s"),
  21939. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  21940. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  21941. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  21942. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  21943. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  21944. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  21945. BUILDIN_DEF(axtoi,"s"),
  21946. BUILDIN_DEF(query_sql,"s*"),
  21947. BUILDIN_DEF(query_logsql,"s*"),
  21948. BUILDIN_DEF(escape_sql,"v"),
  21949. BUILDIN_DEF(atoi,"s"),
  21950. BUILDIN_DEF(strtol,"si"),
  21951. // [zBuffer] List of player cont commands --->
  21952. BUILDIN_DEF(rid2name,"i"),
  21953. BUILDIN_DEF(pcfollow,"ii"),
  21954. BUILDIN_DEF(pcstopfollow,"i"),
  21955. BUILDIN_DEF(pcblockmove,"ii"),
  21956. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  21957. BUILDIN_DEF(pcblockskill,"ii"),
  21958. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  21959. BUILDIN_DEF(setpcblock, "ii?"),
  21960. BUILDIN_DEF(getpcblock, "?"),
  21961. // <--- [zBuffer] List of player cont commands
  21962. // [zBuffer] List of unit control commands --->
  21963. BUILDIN_DEF(unitexists,"i"),
  21964. BUILDIN_DEF(getunittype,"i"),
  21965. BUILDIN_DEF(getunitname,"i"),
  21966. BUILDIN_DEF(setunitname,"is"),
  21967. BUILDIN_DEF(setunittitle,"is"),
  21968. BUILDIN_DEF(getunittitle,"i"),
  21969. BUILDIN_DEF(getunitdata,"i*"),
  21970. BUILDIN_DEF(setunitdata,"iii"),
  21971. BUILDIN_DEF(unitwalk,"iii?"),
  21972. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  21973. BUILDIN_DEF(unitkill,"i"),
  21974. BUILDIN_DEF(unitwarp,"isii"),
  21975. BUILDIN_DEF(unitattack,"iv?"),
  21976. BUILDIN_DEF(unitstopattack,"i"),
  21977. BUILDIN_DEF(unitstopwalk,"i?"),
  21978. BUILDIN_DEF(unittalk,"is?"),
  21979. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  21980. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  21981. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  21982. // <--- [zBuffer] List of unit control commands
  21983. BUILDIN_DEF(sleep,"i"),
  21984. BUILDIN_DEF(sleep2,"i"),
  21985. BUILDIN_DEF(awake,"s"),
  21986. BUILDIN_DEF(getvariableofnpc,"rs"),
  21987. BUILDIN_DEF(warpportal,"iisii"),
  21988. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  21989. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  21990. BUILDIN_DEF(morphembryo,""),
  21991. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  21992. BUILDIN_DEF(checkhomcall,""),
  21993. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  21994. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  21995. BUILDIN_DEF(checkvending,"?"),
  21996. BUILDIN_DEF(checkchatting,"?"),
  21997. BUILDIN_DEF(checkidle,"?"),
  21998. BUILDIN_DEF(checkidlehom,"?"),
  21999. BUILDIN_DEF(checkidlemer,"?"),
  22000. BUILDIN_DEF(openmail,"?"),
  22001. BUILDIN_DEF(openauction,"?"),
  22002. BUILDIN_DEF(checkcell,"siii"),
  22003. BUILDIN_DEF(setcell,"siiiiii"),
  22004. BUILDIN_DEF(getfreecell,"srr?????"),
  22005. BUILDIN_DEF(setwall,"siiiiis"),
  22006. BUILDIN_DEF(delwall,"s"),
  22007. BUILDIN_DEF(checkwall,"s"),
  22008. BUILDIN_DEF(searchitem,"rs"),
  22009. BUILDIN_DEF(mercenary_create,"ii"),
  22010. BUILDIN_DEF(mercenary_heal,"ii"),
  22011. BUILDIN_DEF(mercenary_sc_start,"iii"),
  22012. BUILDIN_DEF(mercenary_get_calls,"i"),
  22013. BUILDIN_DEF(mercenary_get_faith,"i"),
  22014. BUILDIN_DEF(mercenary_set_calls,"ii"),
  22015. BUILDIN_DEF(mercenary_set_faith,"ii"),
  22016. BUILDIN_DEF(readbook,"ii"),
  22017. BUILDIN_DEF(setfont,"i"),
  22018. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  22019. BUILDIN_DEF(progressbar,"si"),
  22020. BUILDIN_DEF(progressbar_npc, "si?"),
  22021. BUILDIN_DEF(pushpc,"ii"),
  22022. BUILDIN_DEF(buyingstore,"i"),
  22023. BUILDIN_DEF(searchstores,"ii"),
  22024. BUILDIN_DEF(showdigit,"i?"),
  22025. // WoE SE
  22026. BUILDIN_DEF(agitstart2,""),
  22027. BUILDIN_DEF(agitend2,""),
  22028. BUILDIN_DEF(agitcheck2,""),
  22029. // BattleGround
  22030. BUILDIN_DEF(waitingroom2bg,"sii???"),
  22031. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  22032. BUILDIN_DEF(bg_team_setxy,"iii"),
  22033. BUILDIN_DEF(bg_warp,"isii"),
  22034. BUILDIN_DEF(bg_monster,"isiisi?"),
  22035. BUILDIN_DEF(bg_monster_set_team,"ii"),
  22036. BUILDIN_DEF(bg_leave,"?"),
  22037. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  22038. BUILDIN_DEF(bg_destroy,"i"),
  22039. BUILDIN_DEF(areapercentheal,"siiiiii"),
  22040. BUILDIN_DEF(bg_get_data,"ii"),
  22041. BUILDIN_DEF(bg_getareausers,"isiiii"),
  22042. BUILDIN_DEF(bg_updatescore,"sii"),
  22043. BUILDIN_DEF(bg_join,"i????"),
  22044. BUILDIN_DEF(bg_create,"sii??"),
  22045. BUILDIN_DEF(bg_reserve,"s?"),
  22046. BUILDIN_DEF(bg_unbook,"s"),
  22047. BUILDIN_DEF(bg_info,"si"),
  22048. // Instancing
  22049. BUILDIN_DEF(instance_create,"s??"),
  22050. BUILDIN_DEF(instance_destroy,"?"),
  22051. BUILDIN_DEF(instance_id,"?"),
  22052. BUILDIN_DEF(instance_enter,"s????"),
  22053. BUILDIN_DEF(instance_npcname,"s?"),
  22054. BUILDIN_DEF(instance_mapname,"s?"),
  22055. BUILDIN_DEF(instance_warpall,"sii?"),
  22056. BUILDIN_DEF(instance_announce,"isi?????"),
  22057. BUILDIN_DEF(instance_check_party,"i???"),
  22058. BUILDIN_DEF(instance_check_guild,"i???"),
  22059. BUILDIN_DEF(instance_check_clan,"i???"),
  22060. BUILDIN_DEF(instance_info,"si?"),
  22061. BUILDIN_DEF(instance_live_info,"i?"),
  22062. /**
  22063. * 3rd-related
  22064. **/
  22065. BUILDIN_DEF(makerune,"i?"),
  22066. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  22067. BUILDIN_DEF(setdragon,"??"),//[Ind]
  22068. BUILDIN_DEF(ismounting,"?"),//[Ind]
  22069. BUILDIN_DEF(setmounting,"?"),//[Ind]
  22070. BUILDIN_DEF(checkre,"i"),
  22071. /**
  22072. * rAthena and beyond!
  22073. **/
  22074. BUILDIN_DEF(getargcount,""),
  22075. BUILDIN_DEF(getcharip,"?"),
  22076. BUILDIN_DEF(is_function,"s"),
  22077. BUILDIN_DEF(get_revision,""),
  22078. BUILDIN_DEF(get_githash,""),
  22079. BUILDIN_DEF(freeloop,"?"),
  22080. BUILDIN_DEF(getrandgroupitem,"i????"),
  22081. BUILDIN_DEF(cleanmap,"s"),
  22082. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  22083. BUILDIN_DEF(npcskill,"viii"),
  22084. BUILDIN_DEF(consumeitem,"v?"),
  22085. BUILDIN_DEF(delequip,"i?"),
  22086. BUILDIN_DEF(breakequip,"i?"),
  22087. BUILDIN_DEF(sit,"?"),
  22088. BUILDIN_DEF(stand,"?"),
  22089. //@commands (script based)
  22090. BUILDIN_DEF(bindatcmd, "ss??"),
  22091. BUILDIN_DEF(unbindatcmd, "s"),
  22092. BUILDIN_DEF(useatcmd, "s"),
  22093. //Quest Log System [Inkfish]
  22094. BUILDIN_DEF(questinfo, "i??"),
  22095. BUILDIN_DEF(setquest, "i?"),
  22096. BUILDIN_DEF(erasequest, "i?"),
  22097. BUILDIN_DEF(completequest, "i?"),
  22098. BUILDIN_DEF(checkquest, "i??"),
  22099. BUILDIN_DEF(isbegin_quest,"i?"),
  22100. BUILDIN_DEF(changequest, "ii?"),
  22101. BUILDIN_DEF(showevent, "i??"),
  22102. BUILDIN_DEF(questinfo_refresh, "?"),
  22103. //Bound items [Xantara] & [Akinari]
  22104. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  22105. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  22106. BUILDIN_DEF(countbound, "??"),
  22107. // Party related
  22108. BUILDIN_DEF(party_create,"s???"),
  22109. BUILDIN_DEF(party_addmember,"ii"),
  22110. BUILDIN_DEF(party_delmember,"??"),
  22111. BUILDIN_DEF(party_changeleader,"ii"),
  22112. BUILDIN_DEF(party_changeoption,"iii"),
  22113. BUILDIN_DEF(party_destroy,"i"),
  22114. // Clan system
  22115. BUILDIN_DEF(clan_join,"i?"),
  22116. BUILDIN_DEF(clan_leave,"?"),
  22117. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  22118. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  22119. BUILDIN_DEF(vip_status,"i?"),
  22120. BUILDIN_DEF(vip_time,"i?"),
  22121. BUILDIN_DEF(bonus_script,"si????"),
  22122. BUILDIN_DEF(bonus_script_clear,"??"),
  22123. BUILDIN_DEF(getgroupitem,"i??"),
  22124. BUILDIN_DEF(enable_command,""),
  22125. BUILDIN_DEF(disable_command,""),
  22126. BUILDIN_DEF(getguildmember,"i??"),
  22127. BUILDIN_DEF(addspiritball,"ii?"),
  22128. BUILDIN_DEF(delspiritball,"i?"),
  22129. BUILDIN_DEF(countspiritball,"?"),
  22130. BUILDIN_DEF(mergeitem,"?"),
  22131. BUILDIN_DEF(mergeitem2,"??"),
  22132. BUILDIN_DEF(npcshopupdate,"sii?"),
  22133. BUILDIN_DEF(getattachedrid,""),
  22134. BUILDIN_DEF(getvar,"vi"),
  22135. BUILDIN_DEF(showscript,"s??"),
  22136. BUILDIN_DEF(ignoretimeout,"i?"),
  22137. BUILDIN_DEF(geteleminfo,"i?"),
  22138. BUILDIN_DEF(opendressroom,"i?"),
  22139. BUILDIN_DEF(navigateto,"s???????"),
  22140. BUILDIN_DEF(getguildalliance,"ii"),
  22141. BUILDIN_DEF(adopt,"vv"),
  22142. BUILDIN_DEF(getexp2,"ii?"),
  22143. BUILDIN_DEF(recalculatestat,""),
  22144. BUILDIN_DEF(hateffect,"ii"),
  22145. BUILDIN_DEF(getrandomoptinfo, "i"),
  22146. BUILDIN_DEF(getequiprandomoption, "iii?"),
  22147. BUILDIN_DEF(setrandomoption,"iiiii?"),
  22148. BUILDIN_DEF(needed_status_point,"ii?"),
  22149. BUILDIN_DEF(jobcanentermap,"s?"),
  22150. BUILDIN_DEF(openstorage2,"ii?"),
  22151. BUILDIN_DEF(unloadnpc, "s"),
  22152. // WoE TE
  22153. BUILDIN_DEF(agitstart3,""),
  22154. BUILDIN_DEF(agitend3,""),
  22155. BUILDIN_DEF(agitcheck3,""),
  22156. BUILDIN_DEF(gvgon3,"s"),
  22157. BUILDIN_DEF(gvgoff3,"s"),
  22158. // Channel System
  22159. BUILDIN_DEF(channel_create,"ss?????"),
  22160. BUILDIN_DEF(channel_setopt,"sii"),
  22161. BUILDIN_DEF(channel_getopt,"si"),
  22162. BUILDIN_DEF(channel_setcolor,"si"),
  22163. BUILDIN_DEF(channel_setpass,"ss"),
  22164. BUILDIN_DEF(channel_setgroup,"si*"),
  22165. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  22166. BUILDIN_DEF(channel_chat,"ss?"),
  22167. BUILDIN_DEF(channel_ban,"si"),
  22168. BUILDIN_DEF(channel_unban,"si"),
  22169. BUILDIN_DEF(channel_kick,"sv"),
  22170. BUILDIN_DEF(channel_delete,"s"),
  22171. // Item Random Option Extension [Cydh]
  22172. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  22173. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  22174. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  22175. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr"),
  22176. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  22177. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  22178. // Achievement System
  22179. BUILDIN_DEF(achievementinfo,"ii?"),
  22180. BUILDIN_DEF(achievementadd,"i?"),
  22181. BUILDIN_DEF(achievementremove,"i?"),
  22182. BUILDIN_DEF(achievementcomplete,"i?"),
  22183. BUILDIN_DEF(achievementexists,"i?"),
  22184. BUILDIN_DEF(achievementupdate,"iii?"),
  22185. BUILDIN_DEF(getequiprefinecost,"iii?"),
  22186. BUILDIN_DEF(refineui,"?"),
  22187. BUILDIN_DEF2(round, "round", "ii"),
  22188. BUILDIN_DEF2(round, "ceil", "ii"),
  22189. BUILDIN_DEF2(round, "floor", "ii"),
  22190. BUILDIN_DEF(getequiptradability, "i?"),
  22191. BUILDIN_DEF(mail, "isss*"),
  22192. BUILDIN_DEF(open_roulette,"?"),
  22193. BUILDIN_DEF(identifyall,"??"),
  22194. BUILDIN_DEF(is_guild_leader,"?"),
  22195. BUILDIN_DEF(is_party_leader,"?"),
  22196. BUILDIN_DEF(camerainfo,"iii?"),
  22197. BUILDIN_DEF(achievement_condition,"i"),
  22198. BUILDIN_DEF(getvariableofinstance,"ri"),
  22199. BUILDIN_DEF(convertpcinfo,"vi"),
  22200. BUILDIN_DEF(cloakoffnpc, "s?"),
  22201. BUILDIN_DEF(cloakonnpc, "s?"),
  22202. BUILDIN_DEF(isnpccloaked, "s?"),
  22203. BUILDIN_DEF(rentalcountitem, "v?"),
  22204. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  22205. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  22206. #include "../custom/script_def.inc"
  22207. {NULL,NULL,NULL},
  22208. };