script.cpp 691 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663
  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);
  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);
  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]=tsd->status.char_id;
  6843. item_tmp.card[3]=tsd->status.char_id >> 16;
  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 && sd->inventory.u.items_inventory[i].refine < MAX_REFINE) {
  8029. enum refine_type type = REFINE_TYPE_SHADOW;
  8030. if (sd->inventory_data[i]->type != IT_SHADOWGEAR)
  8031. type = (enum refine_type)sd->inventory_data[i]->wlv;
  8032. script_pushint(st, status_get_refine_chance(type, (int)sd->inventory.u.items_inventory[i].refine, enriched));
  8033. }
  8034. else
  8035. script_pushint(st,0);
  8036. return SCRIPT_CMD_SUCCESS;
  8037. }
  8038. /*==========================================
  8039. * Refine +1 item at pos and log and display refine
  8040. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8041. *------------------------------------------*/
  8042. BUILDIN_FUNC(successrefitem) {
  8043. short i = -1, up = 1;
  8044. int pos;
  8045. TBL_PC *sd;
  8046. pos = script_getnum(st,2);
  8047. if (!script_charid2sd(4, sd)) {
  8048. script_pushint(st, -1);
  8049. return SCRIPT_CMD_FAILURE;
  8050. }
  8051. if (script_hasdata(st, 3))
  8052. up = script_getnum(st, 3);
  8053. if (equip_index_check(pos))
  8054. i = pc_checkequip(sd,equip_bitmask[pos]);
  8055. if (i >= 0) {
  8056. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8057. //Logs items, got from (N)PC scripts [Lupus]
  8058. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8059. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8060. script_pushint(st, MAX_REFINE);
  8061. return SCRIPT_CMD_SUCCESS;
  8062. }
  8063. sd->inventory.u.items_inventory[i].refine += up;
  8064. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8065. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8066. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8067. clif_delitem(sd,i,1,3);
  8068. //Logs items, got from (N)PC scripts [Lupus]
  8069. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8070. clif_additem(sd,i,1,0);
  8071. pc_equipitem(sd,i,ep);
  8072. clif_misceffect(&sd->bl,3);
  8073. achievement_update_objective(sd, AG_ENCHANT_SUCCESS, 2, sd->inventory_data[i]->wlv, sd->inventory.u.items_inventory[i].refine);
  8074. if (sd->inventory.u.items_inventory[i].refine == battle_config.blacksmith_fame_refine_threshold &&
  8075. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8076. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  8077. { // Fame point system [DracoRPG]
  8078. switch (sd->inventory_data[i]->wlv){
  8079. case 1:
  8080. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8081. break;
  8082. case 2:
  8083. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8084. break;
  8085. case 3:
  8086. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8087. break;
  8088. }
  8089. }
  8090. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8091. return SCRIPT_CMD_SUCCESS;
  8092. }
  8093. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8094. script_pushint(st, -1);
  8095. return SCRIPT_CMD_FAILURE;
  8096. }
  8097. /*==========================================
  8098. * Show a failed Refine +1 attempt
  8099. * failedrefitem <equipment slot>{,<char_id>}
  8100. *------------------------------------------*/
  8101. BUILDIN_FUNC(failedrefitem) {
  8102. short i = -1;
  8103. int pos;
  8104. TBL_PC *sd;
  8105. pos = script_getnum(st,2);
  8106. if (!script_charid2sd(3, sd)) {
  8107. script_pushint(st, 0);
  8108. return SCRIPT_CMD_FAILURE;
  8109. }
  8110. if (equip_index_check(pos))
  8111. i = pc_checkequip(sd,equip_bitmask[pos]);
  8112. if (i >= 0) {
  8113. sd->inventory.u.items_inventory[i].refine = 0;
  8114. pc_unequipitem(sd,i,3); //recalculate bonus
  8115. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8116. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8117. clif_misceffect(&sd->bl,2); // display failure effect
  8118. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, 1);
  8119. script_pushint(st, 1);
  8120. return SCRIPT_CMD_SUCCESS;
  8121. }
  8122. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8123. script_pushint(st, 0);
  8124. return SCRIPT_CMD_FAILURE;
  8125. }
  8126. /*==========================================
  8127. * Downgrades an Equipment Part by -1 . [Masao]
  8128. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8129. *------------------------------------------*/
  8130. BUILDIN_FUNC(downrefitem) {
  8131. short i = -1, down = 1;
  8132. int pos;
  8133. TBL_PC *sd;
  8134. if (!script_charid2sd(4, sd)) {
  8135. script_pushint(st, -1);
  8136. return SCRIPT_CMD_FAILURE;
  8137. }
  8138. pos = script_getnum(st,2);
  8139. if (script_hasdata(st, 3))
  8140. down = script_getnum(st, 3);
  8141. if (equip_index_check(pos))
  8142. i = pc_checkequip(sd,equip_bitmask[pos]);
  8143. if (i >= 0) {
  8144. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8145. //Logs items, got from (N)PC scripts [Lupus]
  8146. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8147. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8148. sd->inventory.u.items_inventory[i].refine -= down;
  8149. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8150. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8151. clif_delitem(sd,i,1,3);
  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. clif_additem(sd,i,1,0);
  8155. pc_equipitem(sd,i,ep);
  8156. clif_misceffect(&sd->bl,2);
  8157. achievement_update_objective(sd, AG_ENCHANT_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8158. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8159. return SCRIPT_CMD_SUCCESS;
  8160. }
  8161. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8162. script_pushint(st, -1);
  8163. return SCRIPT_CMD_FAILURE;
  8164. }
  8165. /**
  8166. * Delete the item equipped at pos.
  8167. * delequip <equipment slot>{,<char_id>};
  8168. **/
  8169. BUILDIN_FUNC(delequip) {
  8170. short i = -1;
  8171. int pos;
  8172. int8 ret;
  8173. TBL_PC *sd;
  8174. pos = script_getnum(st,2);
  8175. if (!script_charid2sd(3,sd)) {
  8176. st->state = END;
  8177. return SCRIPT_CMD_FAILURE;
  8178. }
  8179. if (equip_index_check(pos))
  8180. i = pc_checkequip(sd,equip_bitmask[pos]);
  8181. if (i >= 0) {
  8182. pc_unequipitem(sd,i,3); //recalculate bonus
  8183. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8184. }
  8185. else {
  8186. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8187. st->state = END;
  8188. return SCRIPT_CMD_FAILURE;
  8189. }
  8190. script_pushint(st,ret);
  8191. return SCRIPT_CMD_SUCCESS;
  8192. }
  8193. /**
  8194. * Break the item equipped at pos.
  8195. * breakequip <equipment slot>{,<char_id>};
  8196. **/
  8197. BUILDIN_FUNC(breakequip) {
  8198. short i = -1;
  8199. int pos;
  8200. TBL_PC *sd;
  8201. pos = script_getnum(st,2);
  8202. if (!script_charid2sd(3,sd))
  8203. return SCRIPT_CMD_FAILURE;
  8204. if (equip_index_check(pos))
  8205. i = pc_checkequip(sd,equip_bitmask[pos]);
  8206. if (i >= 0) {
  8207. sd->inventory.u.items_inventory[i].attribute = 1;
  8208. pc_unequipitem(sd,i,3);
  8209. clif_equiplist(sd);
  8210. script_pushint(st,1);
  8211. return SCRIPT_CMD_SUCCESS;
  8212. }
  8213. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8214. script_pushint(st,0);
  8215. return SCRIPT_CMD_FAILURE;
  8216. }
  8217. /**
  8218. * statusup <stat>{,<char_id>};
  8219. **/
  8220. BUILDIN_FUNC(statusup)
  8221. {
  8222. int type;
  8223. TBL_PC *sd;
  8224. type = script_getnum(st,2);
  8225. if (!script_charid2sd(3, sd))
  8226. return SCRIPT_CMD_FAILURE;
  8227. pc_statusup(sd, type, 1);
  8228. return SCRIPT_CMD_SUCCESS;
  8229. }
  8230. /**
  8231. * statusup2 <stat>,<amount>{,<char_id>};
  8232. **/
  8233. BUILDIN_FUNC(statusup2)
  8234. {
  8235. int type,val;
  8236. TBL_PC *sd;
  8237. type=script_getnum(st,2);
  8238. val=script_getnum(st,3);
  8239. if (!script_charid2sd(4, sd))
  8240. return SCRIPT_CMD_FAILURE;
  8241. pc_statusup2(sd,type,val);
  8242. return SCRIPT_CMD_SUCCESS;
  8243. }
  8244. /// See 'doc/item_bonus.txt'
  8245. ///
  8246. /// bonus <bonus type>,<val1>;
  8247. /// bonus2 <bonus type>,<val1>,<val2>;
  8248. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8249. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8250. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8251. BUILDIN_FUNC(bonus)
  8252. {
  8253. int type;
  8254. int val1 = 0;
  8255. int val2 = 0;
  8256. int val3 = 0;
  8257. int val4 = 0;
  8258. int val5 = 0;
  8259. TBL_PC* sd;
  8260. if( !script_rid2sd(sd) )
  8261. return SCRIPT_CMD_SUCCESS; // no player attached
  8262. type = script_getnum(st,2);
  8263. switch( type ) {
  8264. case SP_AUTOSPELL:
  8265. case SP_AUTOSPELL_WHENHIT:
  8266. case SP_AUTOSPELL_ONSKILL:
  8267. case SP_SKILL_ATK:
  8268. case SP_SKILL_HEAL:
  8269. case SP_SKILL_HEAL2:
  8270. case SP_ADD_SKILL_BLOW:
  8271. case SP_CASTRATE:
  8272. case SP_ADDEFF_ONSKILL:
  8273. case SP_SKILL_USE_SP_RATE:
  8274. case SP_SKILL_COOLDOWN:
  8275. case SP_SKILL_FIXEDCAST:
  8276. case SP_SKILL_VARIABLECAST:
  8277. case SP_VARCASTRATE:
  8278. case SP_FIXCASTRATE:
  8279. case SP_SKILL_DELAY:
  8280. case SP_SKILL_USE_SP:
  8281. case SP_SUB_SKILL:
  8282. // these bonuses support skill names
  8283. if (script_isstring(st, 3)) {
  8284. const char *name = script_getstr(st, 3);
  8285. if (!(val1 = skill_name2id(name))) {
  8286. ShowError("buildin_bonus: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8287. return SCRIPT_CMD_FAILURE;
  8288. }
  8289. } else {
  8290. val1 = script_getnum(st, 3);
  8291. if (strcmpi(script_getfuncname(st), "bonus") && !skill_get_index(val1)) { // Only check skill ID for bonus2, bonus3, bonus4, or bonus5
  8292. ShowError("buildin_bonus: Invalid skill ID %d passed to item bonus. Skipping.\n", val1);
  8293. return SCRIPT_CMD_FAILURE;
  8294. }
  8295. }
  8296. break;
  8297. default:
  8298. if (script_hasdata(st, 3))
  8299. val1 = script_getnum(st, 3);
  8300. break;
  8301. }
  8302. switch( script_lastdata(st)-2 ) {
  8303. case 0:
  8304. case 1:
  8305. pc_bonus(sd, type, val1);
  8306. break;
  8307. case 2:
  8308. val2 = script_getnum(st,4);
  8309. pc_bonus2(sd, type, val1, val2);
  8310. break;
  8311. case 3:
  8312. val2 = script_getnum(st,4);
  8313. val3 = script_getnum(st,5);
  8314. pc_bonus3(sd, type, val1, val2, val3);
  8315. break;
  8316. case 4:
  8317. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8318. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8319. else
  8320. val2 = script_getnum(st,4);
  8321. val3 = script_getnum(st,5);
  8322. val4 = script_getnum(st,6);
  8323. pc_bonus4(sd, type, val1, val2, val3, val4);
  8324. break;
  8325. case 5:
  8326. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st, 4) )
  8327. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8328. else
  8329. val2 = script_getnum(st,4);
  8330. val3 = script_getnum(st,5);
  8331. val4 = script_getnum(st,6);
  8332. val5 = script_getnum(st,7);
  8333. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8334. break;
  8335. default:
  8336. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8337. break;
  8338. }
  8339. return SCRIPT_CMD_SUCCESS;
  8340. }
  8341. BUILDIN_FUNC(autobonus)
  8342. {
  8343. unsigned int dur, pos;
  8344. short rate;
  8345. uint16 atk_type = 0;
  8346. TBL_PC* sd;
  8347. const char *bonus_script, *other_script = NULL;
  8348. if( !script_rid2sd(sd) )
  8349. return SCRIPT_CMD_SUCCESS; // no player attached
  8350. if (current_equip_combo_pos)
  8351. pos = current_equip_combo_pos;
  8352. else
  8353. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8354. if((sd->state.autobonus&pos) == pos)
  8355. return SCRIPT_CMD_SUCCESS;
  8356. rate = script_getnum(st,3);
  8357. dur = script_getnum(st,4);
  8358. bonus_script = script_getstr(st,2);
  8359. if( !rate || !dur || !pos || !bonus_script )
  8360. return SCRIPT_CMD_SUCCESS;
  8361. if( script_hasdata(st,5) )
  8362. atk_type = script_getnum(st,5);
  8363. if( script_hasdata(st,6) )
  8364. other_script = script_getstr(st,6);
  8365. if( pc_addautobonus(sd->autobonus, bonus_script, rate, dur, atk_type, other_script, pos, false) )
  8366. {
  8367. script_add_autobonus(bonus_script);
  8368. if( other_script )
  8369. script_add_autobonus(other_script);
  8370. }
  8371. return SCRIPT_CMD_SUCCESS;
  8372. }
  8373. BUILDIN_FUNC(autobonus2)
  8374. {
  8375. unsigned int dur, pos;
  8376. short rate;
  8377. uint16 atk_type = 0;
  8378. TBL_PC* sd;
  8379. const char *bonus_script, *other_script = NULL;
  8380. if( !script_rid2sd(sd) )
  8381. return SCRIPT_CMD_SUCCESS; // no player attached
  8382. if (current_equip_combo_pos)
  8383. pos = current_equip_combo_pos;
  8384. else
  8385. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8386. if((sd->state.autobonus&pos) == pos)
  8387. return SCRIPT_CMD_SUCCESS;
  8388. rate = script_getnum(st,3);
  8389. dur = script_getnum(st,4);
  8390. bonus_script = script_getstr(st,2);
  8391. if( !rate || !dur || !pos || !bonus_script )
  8392. return SCRIPT_CMD_SUCCESS;
  8393. if( script_hasdata(st,5) )
  8394. atk_type = script_getnum(st,5);
  8395. if( script_hasdata(st,6) )
  8396. other_script = script_getstr(st,6);
  8397. if( pc_addautobonus(sd->autobonus2, bonus_script, rate,dur, atk_type, other_script, pos, false) )
  8398. {
  8399. script_add_autobonus(bonus_script);
  8400. if( other_script )
  8401. script_add_autobonus(other_script);
  8402. }
  8403. return SCRIPT_CMD_SUCCESS;
  8404. }
  8405. BUILDIN_FUNC(autobonus3)
  8406. {
  8407. unsigned int dur, pos;
  8408. short rate;
  8409. uint16 skill_id = 0;
  8410. TBL_PC* sd;
  8411. const char *bonus_script, *other_script = NULL;
  8412. if( !script_rid2sd(sd) )
  8413. return SCRIPT_CMD_SUCCESS; // no player attached
  8414. if (current_equip_combo_pos)
  8415. pos = current_equip_combo_pos;
  8416. else
  8417. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8418. if((sd->state.autobonus&pos) == pos)
  8419. return SCRIPT_CMD_SUCCESS;
  8420. rate = script_getnum(st,3);
  8421. dur = script_getnum(st,4);
  8422. if (script_isstring(st, 5)) {
  8423. const char *name = script_getstr(st, 5);
  8424. if (!(skill_id = skill_name2id(name))) {
  8425. ShowError("buildin_autobonus3: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  8426. return SCRIPT_CMD_FAILURE;
  8427. }
  8428. } else {
  8429. skill_id = script_getnum(st, 5);
  8430. if (!skill_get_index(skill_id)) {
  8431. ShowError("buildin_autobonus3: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  8432. return SCRIPT_CMD_FAILURE;
  8433. }
  8434. }
  8435. bonus_script = script_getstr(st,2);
  8436. if( !rate || !dur || !pos || !bonus_script )
  8437. return SCRIPT_CMD_SUCCESS;
  8438. if( script_hasdata(st,6) )
  8439. other_script = script_getstr(st,6);
  8440. if( pc_addautobonus(sd->autobonus3, bonus_script, rate, dur, skill_id, other_script, pos, true) )
  8441. {
  8442. script_add_autobonus(bonus_script);
  8443. if( other_script )
  8444. script_add_autobonus(other_script);
  8445. }
  8446. return SCRIPT_CMD_SUCCESS;
  8447. }
  8448. /// Changes the level of a player skill.
  8449. /// <flag> defaults to 1
  8450. /// <flag>=0 : set the level of the skill
  8451. /// <flag>=1 : set the temporary level of the skill
  8452. /// <flag>=2 : add to the level of the skill
  8453. ///
  8454. /// skill <skill id>,<level>,<flag>
  8455. /// skill <skill id>,<level>
  8456. /// skill "<skill name>",<level>,<flag>
  8457. /// skill "<skill name>",<level>
  8458. BUILDIN_FUNC(skill)
  8459. {
  8460. int id;
  8461. int level;
  8462. int flag = ADDSKILL_TEMP;
  8463. TBL_PC* sd;
  8464. const char* command = script_getfuncname(st);
  8465. if( !script_rid2sd(sd) )
  8466. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8467. if (strcmpi(command, "addtoskill") == 0)
  8468. flag = ADDSKILL_TEMP_ADDLEVEL;
  8469. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8470. level = script_getnum(st,3);
  8471. if( script_hasdata(st,4) )
  8472. flag = script_getnum(st,4);
  8473. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8474. return SCRIPT_CMD_SUCCESS;
  8475. }
  8476. /// Increases the level of a guild skill.
  8477. ///
  8478. /// guildskill <skill id>,<amount>;
  8479. /// guildskill "<skill name>",<amount>;
  8480. BUILDIN_FUNC(guildskill)
  8481. {
  8482. int id;
  8483. int level;
  8484. TBL_PC* sd;
  8485. int i;
  8486. if( !script_rid2sd(sd) )
  8487. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8488. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8489. level = script_getnum(st,3);
  8490. for( i=0; i < level; i++ )
  8491. guild_skillup(sd, id);
  8492. return SCRIPT_CMD_SUCCESS;
  8493. }
  8494. /// Returns the level of the player skill.
  8495. ///
  8496. /// getskilllv(<skill id>) -> <level>
  8497. /// getskilllv("<skill name>") -> <level>
  8498. BUILDIN_FUNC(getskilllv)
  8499. {
  8500. int id;
  8501. TBL_PC* sd;
  8502. if( !script_rid2sd(sd) )
  8503. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8504. id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8505. script_pushint(st, pc_checkskill(sd,id));
  8506. return SCRIPT_CMD_SUCCESS;
  8507. }
  8508. /// Returns the level of the guild skill.
  8509. ///
  8510. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8511. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8512. BUILDIN_FUNC(getgdskilllv)
  8513. {
  8514. int guild_id;
  8515. uint16 skill_id;
  8516. struct guild* g;
  8517. guild_id = script_getnum(st,2);
  8518. skill_id = ( script_isstring(st, 3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8519. g = guild_search(guild_id);
  8520. if( g == NULL )
  8521. script_pushint(st, -1);
  8522. else
  8523. script_pushint(st, guild_checkskill(g,skill_id));
  8524. return SCRIPT_CMD_SUCCESS;
  8525. }
  8526. /// Returns the 'basic_skill_check' setting.
  8527. /// This config determines if the server checks the skill level of NV_BASIC
  8528. /// before allowing the basic actions.
  8529. ///
  8530. /// basicskillcheck() -> <bool>
  8531. BUILDIN_FUNC(basicskillcheck)
  8532. {
  8533. script_pushint(st, battle_config.basic_skill_check);
  8534. return SCRIPT_CMD_SUCCESS;
  8535. }
  8536. /// Returns the GM level of the player.
  8537. ///
  8538. /// getgmlevel({<char_id>}) -> <level>
  8539. BUILDIN_FUNC(getgmlevel)
  8540. {
  8541. TBL_PC* sd;
  8542. if (!script_charid2sd(2,sd))
  8543. return SCRIPT_CMD_FAILURE;
  8544. script_pushint(st, pc_get_group_level(sd));
  8545. return SCRIPT_CMD_SUCCESS;
  8546. }
  8547. /// Returns the group ID of the player.
  8548. ///
  8549. /// getgroupid({<char_id>}) -> <int>
  8550. BUILDIN_FUNC(getgroupid)
  8551. {
  8552. TBL_PC* sd;
  8553. if (!script_charid2sd(2,sd))
  8554. return SCRIPT_CMD_FAILURE;
  8555. script_pushint(st, pc_get_group_id(sd));
  8556. return SCRIPT_CMD_SUCCESS;
  8557. }
  8558. /// Terminates the execution of this script instance.
  8559. ///
  8560. /// end
  8561. BUILDIN_FUNC(end)
  8562. {
  8563. TBL_PC* sd;
  8564. sd = map_id2sd(st->rid);
  8565. st->state = END;
  8566. if( st->mes_active )
  8567. st->mes_active = 0;
  8568. if (sd){
  8569. if (sd->state.callshop == 0)
  8570. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8571. else
  8572. sd->state.callshop = 0;
  8573. }
  8574. return SCRIPT_CMD_SUCCESS;
  8575. }
  8576. /// Checks if the player has that effect state (option).
  8577. ///
  8578. /// checkoption(<option>{,<char_id>}) -> <bool>
  8579. BUILDIN_FUNC(checkoption)
  8580. {
  8581. int option;
  8582. TBL_PC* sd;
  8583. if (!script_charid2sd(3,sd))
  8584. return SCRIPT_CMD_FAILURE;
  8585. option = script_getnum(st,2);
  8586. if( sd->sc.option&option )
  8587. script_pushint(st, 1);
  8588. else
  8589. script_pushint(st, 0);
  8590. return SCRIPT_CMD_SUCCESS;
  8591. }
  8592. /// Checks if the player is in that body state (opt1).
  8593. ///
  8594. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8595. BUILDIN_FUNC(checkoption1)
  8596. {
  8597. int opt1;
  8598. TBL_PC* sd;
  8599. if (!script_charid2sd(3,sd))
  8600. return SCRIPT_CMD_FAILURE;
  8601. opt1 = script_getnum(st,2);
  8602. if( sd->sc.opt1 == opt1 )
  8603. script_pushint(st, 1);
  8604. else
  8605. script_pushint(st, 0);
  8606. return SCRIPT_CMD_SUCCESS;
  8607. }
  8608. /// Checks if the player has that health state (opt2).
  8609. ///
  8610. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8611. BUILDIN_FUNC(checkoption2)
  8612. {
  8613. int opt2;
  8614. TBL_PC* sd;
  8615. if (!script_charid2sd(3,sd))
  8616. return SCRIPT_CMD_FAILURE;
  8617. opt2 = script_getnum(st,2);
  8618. if( sd->sc.opt2&opt2 )
  8619. script_pushint(st, 1);
  8620. else
  8621. script_pushint(st, 0);
  8622. return SCRIPT_CMD_SUCCESS;
  8623. }
  8624. /// Changes the effect state (option) of the player.
  8625. /// <flag> defaults to 1
  8626. /// <flag>=0 : removes the option
  8627. /// <flag>=other : adds the option
  8628. ///
  8629. /// setoption <option>{,<flag>{,<char_id>}};
  8630. BUILDIN_FUNC(setoption)
  8631. {
  8632. int option;
  8633. int flag = 1;
  8634. TBL_PC* sd;
  8635. if (!script_charid2sd(4,sd))
  8636. return SCRIPT_CMD_FAILURE;
  8637. option = script_getnum(st,2);
  8638. if( script_hasdata(st,3) )
  8639. flag = script_getnum(st,3);
  8640. else if( !option ){// Request to remove everything.
  8641. flag = 0;
  8642. option = OPTION_FALCON|OPTION_RIDING;
  8643. #ifndef NEW_CARTS
  8644. option |= OPTION_CART;
  8645. #endif
  8646. }
  8647. if( flag ){// Add option
  8648. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8649. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8650. pc_setoption(sd, sd->sc.option|option);
  8651. } else// Remove option
  8652. pc_setoption(sd, sd->sc.option&~option);
  8653. return SCRIPT_CMD_SUCCESS;
  8654. }
  8655. /// Returns if the player has a cart.
  8656. ///
  8657. /// checkcart({char_id}) -> <bool>
  8658. ///
  8659. /// @author Valaris
  8660. BUILDIN_FUNC(checkcart)
  8661. {
  8662. TBL_PC* sd;
  8663. if (!script_charid2sd(2,sd))
  8664. return SCRIPT_CMD_FAILURE;
  8665. if( pc_iscarton(sd) )
  8666. script_pushint(st, 1);
  8667. else
  8668. script_pushint(st, 0);
  8669. return SCRIPT_CMD_SUCCESS;
  8670. }
  8671. /// Sets the cart of the player.
  8672. /// <type> defaults to 1
  8673. /// <type>=0 : removes the cart
  8674. /// <type>=1 : Normal cart
  8675. /// <type>=2 : Wooden cart
  8676. /// <type>=3 : Covered cart with flowers and ferns
  8677. /// <type>=4 : Wooden cart with a Panda doll on the back
  8678. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8679. ///
  8680. /// setcart {<type>{,<char_id>}};
  8681. BUILDIN_FUNC(setcart)
  8682. {
  8683. int type = 1;
  8684. TBL_PC* sd;
  8685. if (!script_charid2sd(3,sd))
  8686. return SCRIPT_CMD_FAILURE;
  8687. if( script_hasdata(st,2) )
  8688. type = script_getnum(st,2);
  8689. pc_setcart(sd, type);
  8690. return SCRIPT_CMD_SUCCESS;
  8691. }
  8692. /// Returns if the player has a falcon.
  8693. ///
  8694. /// checkfalcon({char_id}) -> <bool>
  8695. ///
  8696. /// @author Valaris
  8697. BUILDIN_FUNC(checkfalcon)
  8698. {
  8699. TBL_PC* sd;
  8700. if (!script_charid2sd(2,sd))
  8701. return SCRIPT_CMD_FAILURE;
  8702. if( pc_isfalcon(sd) )
  8703. script_pushint(st, 1);
  8704. else
  8705. script_pushint(st, 0);
  8706. return SCRIPT_CMD_SUCCESS;
  8707. }
  8708. /// Sets if the player has a falcon or not.
  8709. /// <flag> defaults to 1
  8710. ///
  8711. /// setfalcon {<flag>{,<char_id>}};
  8712. BUILDIN_FUNC(setfalcon)
  8713. {
  8714. int flag = 1;
  8715. TBL_PC* sd;
  8716. if (!script_charid2sd(3,sd))
  8717. return SCRIPT_CMD_FAILURE;
  8718. if( script_hasdata(st,2) )
  8719. flag = script_getnum(st,2);
  8720. pc_setfalcon(sd, flag);
  8721. return SCRIPT_CMD_SUCCESS;
  8722. }
  8723. /// Returns if the player is riding.
  8724. ///
  8725. /// checkriding({char_id}) -> <bool>
  8726. ///
  8727. /// @author Valaris
  8728. BUILDIN_FUNC(checkriding)
  8729. {
  8730. TBL_PC* sd;
  8731. if (!script_charid2sd(2,sd))
  8732. return SCRIPT_CMD_FAILURE;
  8733. if( pc_isriding(sd) )
  8734. script_pushint(st, 1);
  8735. else
  8736. script_pushint(st, 0);
  8737. return SCRIPT_CMD_SUCCESS;
  8738. }
  8739. /// Sets if the player is riding.
  8740. /// <flag> defaults to 1
  8741. ///
  8742. /// setriding {<flag>{,<char_id>}};
  8743. BUILDIN_FUNC(setriding)
  8744. {
  8745. int flag = 1;
  8746. TBL_PC* sd;
  8747. if (!script_charid2sd(3,sd))
  8748. return SCRIPT_CMD_FAILURE;
  8749. if( script_hasdata(st,2) )
  8750. flag = script_getnum(st,2);
  8751. pc_setriding(sd, flag);
  8752. return SCRIPT_CMD_SUCCESS;
  8753. }
  8754. /// Returns if the player has a warg.
  8755. ///
  8756. /// checkwug({char_id}) -> <bool>
  8757. ///
  8758. BUILDIN_FUNC(checkwug)
  8759. {
  8760. TBL_PC* sd;
  8761. if (!script_charid2sd(2,sd))
  8762. return SCRIPT_CMD_FAILURE;
  8763. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8764. script_pushint(st, 1);
  8765. else
  8766. script_pushint(st, 0);
  8767. return SCRIPT_CMD_SUCCESS;
  8768. }
  8769. /// Returns if the player is wearing MADO Gear.
  8770. ///
  8771. /// checkmadogear({<char_id>}) -> <bool>
  8772. ///
  8773. BUILDIN_FUNC(checkmadogear)
  8774. {
  8775. TBL_PC* sd;
  8776. if (!script_charid2sd(2,sd))
  8777. return SCRIPT_CMD_FAILURE;
  8778. if( pc_ismadogear(sd) )
  8779. script_pushint(st, 1);
  8780. else
  8781. script_pushint(st, 0);
  8782. return SCRIPT_CMD_SUCCESS;
  8783. }
  8784. /// Sets if the player is riding MADO Gear.
  8785. /// <flag> defaults to true
  8786. /// <type> defaults to MADO_ROBOT
  8787. ///
  8788. /// setmadogear {<flag>{,type{,<char_id>}}};
  8789. BUILDIN_FUNC(setmadogear)
  8790. {
  8791. bool flag = true;
  8792. TBL_PC* sd;
  8793. uint16 type = MADO_ROBOT;
  8794. if (!script_charid2sd(4,sd))
  8795. return SCRIPT_CMD_FAILURE;
  8796. if( script_hasdata(st,2) )
  8797. flag = script_getnum(st,2) != 0;
  8798. if (script_hasdata(st, 3)) {
  8799. type = script_getnum(st, 3);
  8800. if (type == MADO_UNUSED || type >= MADO_MAX) {
  8801. ShowError("buildin_setmadogear: Invalid mado gear type %hu, defaulting to robot...\n", type);
  8802. type = MADO_ROBOT;
  8803. }
  8804. }
  8805. pc_setmadogear(sd, flag, static_cast<e_mado_type>(type));
  8806. return SCRIPT_CMD_SUCCESS;
  8807. }
  8808. /// Sets the save point of the player.
  8809. ///
  8810. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8811. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8812. BUILDIN_FUNC(savepoint)
  8813. {
  8814. int x, y, m, cid_pos = 5;
  8815. unsigned short map_idx;
  8816. const char* str;
  8817. TBL_PC* sd;
  8818. if (script_lastdata(st) > 5)
  8819. cid_pos = 7;
  8820. if (!script_charid2sd(cid_pos,sd))
  8821. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8822. str = script_getstr(st, 2);
  8823. map_idx = mapindex_name2id(str);
  8824. if( !map_idx )
  8825. return SCRIPT_CMD_FAILURE;
  8826. x = script_getnum(st,3);
  8827. y = script_getnum(st,4);
  8828. m = map_mapindex2mapid(map_idx);
  8829. if (cid_pos == 7) {
  8830. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8831. x1 = x + dx, y1 = y + dy,
  8832. x0 = x - dx, y0 = y - dy;
  8833. uint8 n = 10;
  8834. do {
  8835. x = x0 + rnd()%(x1-x0+1);
  8836. y = y0 + rnd()%(y1-y0+1);
  8837. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8838. }
  8839. // Check for valid coordinates if map in local map-server
  8840. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8841. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8842. return SCRIPT_CMD_FAILURE;
  8843. }
  8844. pc_setsavepoint(sd, map_idx, x, y);
  8845. return SCRIPT_CMD_SUCCESS;
  8846. }
  8847. /*==========================================
  8848. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8849. *------------------------------------------*/
  8850. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8851. {
  8852. int type;
  8853. time_t timer;
  8854. struct tm *t;
  8855. type=script_getnum(st,2);
  8856. switch(type){
  8857. case 2:
  8858. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8859. // from the system clock.)
  8860. script_pushint(st,(int)time(NULL));
  8861. break;
  8862. case 1:
  8863. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8864. time(&timer);
  8865. t=localtime(&timer);
  8866. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8867. break;
  8868. case 0:
  8869. default:
  8870. //type 0:(System Ticks)
  8871. script_pushint(st,gettick());
  8872. break;
  8873. }
  8874. return SCRIPT_CMD_SUCCESS;
  8875. }
  8876. /*==========================================
  8877. * GetTime(Type)
  8878. *
  8879. * Returns the current value of a certain date type.
  8880. * Possible types are:
  8881. * - DT_SECOND Current seconds
  8882. * - DT_MINUTE Current minute
  8883. * - DT_HOUR Current hour
  8884. * - DT_DAYOFWEEK Day of current week
  8885. * - DT_DAYOFMONTH Day of current month
  8886. * - DT_MONTH Current month
  8887. * - DT_YEAR Current year
  8888. * - DT_DAYOFYEAR Day of current year
  8889. *
  8890. * If none of the above types is supplied -1 will be returned to the script
  8891. * and the script source will be reported into the mapserver console.
  8892. *------------------------------------------*/
  8893. BUILDIN_FUNC(gettime)
  8894. {
  8895. int type;
  8896. type = script_getnum(st,2);
  8897. if( type <= DT_MIN || type >= DT_MAX ){
  8898. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8899. script_reportsrc(st);
  8900. script_pushint(st,-1);
  8901. }else{
  8902. script_pushint(st,date_get((enum e_date_type)type));
  8903. }
  8904. return SCRIPT_CMD_SUCCESS;
  8905. }
  8906. /**
  8907. * Returns the current server time or the provided time in a readable format.
  8908. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  8909. */
  8910. BUILDIN_FUNC(gettimestr)
  8911. {
  8912. char *tmpstr;
  8913. const char *fmtstr;
  8914. int maxlen;
  8915. time_t now;
  8916. fmtstr = script_getstr(st,2);
  8917. maxlen = script_getnum(st,3);
  8918. if (script_hasdata(st, 4)) {
  8919. if (script_getnum(st, 4) < 0) {
  8920. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  8921. return SCRIPT_CMD_FAILURE;
  8922. } else
  8923. now = (time_t)script_getnum(st, 4);
  8924. } else
  8925. now = time(NULL);
  8926. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  8927. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8928. tmpstr[maxlen] = '\0';
  8929. script_pushstr(st,tmpstr);
  8930. return SCRIPT_CMD_SUCCESS;
  8931. }
  8932. /*==========================================
  8933. * Open player storage
  8934. *------------------------------------------*/
  8935. BUILDIN_FUNC(openstorage)
  8936. {
  8937. TBL_PC* sd;
  8938. if( !script_rid2sd(sd) )
  8939. return SCRIPT_CMD_SUCCESS;
  8940. storage_storageopen(sd);
  8941. return SCRIPT_CMD_SUCCESS;
  8942. }
  8943. BUILDIN_FUNC(guildopenstorage)
  8944. {
  8945. TBL_PC* sd;
  8946. int ret;
  8947. if( !script_rid2sd(sd) )
  8948. return SCRIPT_CMD_SUCCESS;
  8949. ret = storage_guild_storageopen(sd);
  8950. script_pushint(st,ret);
  8951. return SCRIPT_CMD_SUCCESS;
  8952. }
  8953. BUILDIN_FUNC(guildopenstorage_log){
  8954. #if PACKETVER < 20140205
  8955. ShowError( "buildin_guildopenstorage_log: This command requires PACKETVER 2014-02-05 or newer.\n" );
  8956. return SCRIPT_CMD_FAILURE;
  8957. #else
  8958. struct map_session_data* sd;
  8959. if( !script_charid2sd( 2, sd ) ){
  8960. return SCRIPT_CMD_FAILURE;
  8961. }
  8962. script_pushint( st, storage_guild_log_read( sd ) );
  8963. return SCRIPT_CMD_SUCCESS;
  8964. #endif
  8965. }
  8966. BUILDIN_FUNC(guild_has_permission){
  8967. struct map_session_data* sd;
  8968. if( !script_charid2sd( 3, sd ) ){
  8969. return SCRIPT_CMD_FAILURE;
  8970. }
  8971. int permission = script_getnum(st,2);
  8972. if( permission == 0 ){
  8973. ShowError( "buildin_guild_has_permission: No permission given.\n" );
  8974. return SCRIPT_CMD_FAILURE;
  8975. }
  8976. if( ( permission & GUILD_PERM_ALL ) == 0 ){
  8977. ShowError( "buildin_guild_has_permission: Invalid permission '%d'.\n", permission );
  8978. return SCRIPT_CMD_FAILURE;
  8979. }
  8980. if( !sd->guild ){
  8981. script_pushint( st, false );
  8982. return SCRIPT_CMD_SUCCESS;
  8983. }
  8984. int position = guild_getposition(sd);
  8985. if( position < 0 || ( sd->guild->position[position].mode&permission ) != permission ){
  8986. script_pushint( st, false );
  8987. return SCRIPT_CMD_SUCCESS;
  8988. }
  8989. script_pushint( st, true );
  8990. return SCRIPT_CMD_SUCCESS;
  8991. }
  8992. /*==========================================
  8993. * Make player use a skill trought item usage
  8994. *------------------------------------------*/
  8995. /// itemskill <skill id>,<level>
  8996. /// itemskill "<skill name>",<level>
  8997. BUILDIN_FUNC(itemskill)
  8998. {
  8999. int id;
  9000. int lv;
  9001. bool keep_requirement;
  9002. TBL_PC* sd;
  9003. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  9004. return SCRIPT_CMD_SUCCESS;
  9005. if (script_isstring(st, 2)) {
  9006. const char *name = script_getstr(st, 2);
  9007. if (!(id = skill_name2id(name))) {
  9008. ShowError("buildin_itemskill: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  9009. return SCRIPT_CMD_FAILURE;
  9010. }
  9011. } else {
  9012. id = script_getnum(st, 2);
  9013. if (!skill_get_index(id)) {
  9014. ShowError("buildin_itemskill: Invalid skill ID %d passed to item bonus. Skipping.\n", id);
  9015. return SCRIPT_CMD_FAILURE;
  9016. }
  9017. }
  9018. lv = script_getnum(st,3);
  9019. if (script_hasdata(st, 4)) {
  9020. keep_requirement = (script_getnum(st, 4) != 0);
  9021. } else {
  9022. keep_requirement = false;
  9023. }
  9024. sd->skillitem=id;
  9025. sd->skillitemlv=lv;
  9026. sd->skillitem_keep_requirement = keep_requirement;
  9027. clif_item_skill(sd,id,lv);
  9028. return SCRIPT_CMD_SUCCESS;
  9029. }
  9030. /*==========================================
  9031. * Attempt to create an item
  9032. *------------------------------------------*/
  9033. BUILDIN_FUNC(produce)
  9034. {
  9035. int trigger;
  9036. TBL_PC* sd;
  9037. if( !script_rid2sd(sd) )
  9038. return SCRIPT_CMD_SUCCESS;
  9039. trigger=script_getnum(st,2);
  9040. clif_skill_produce_mix_list(sd, -1, trigger);
  9041. return SCRIPT_CMD_SUCCESS;
  9042. }
  9043. /*==========================================
  9044. *
  9045. *------------------------------------------*/
  9046. BUILDIN_FUNC(cooking)
  9047. {
  9048. int trigger;
  9049. TBL_PC* sd;
  9050. if( !script_rid2sd(sd) )
  9051. return SCRIPT_CMD_SUCCESS;
  9052. trigger=script_getnum(st,2);
  9053. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  9054. return SCRIPT_CMD_SUCCESS;
  9055. }
  9056. /*==========================================
  9057. * Create a pet
  9058. *------------------------------------------*/
  9059. BUILDIN_FUNC(makepet)
  9060. {
  9061. struct map_session_data* sd;
  9062. uint16 mob_id;
  9063. if( !script_rid2sd(sd) )
  9064. return SCRIPT_CMD_FAILURE;
  9065. mob_id = script_getnum(st,2);
  9066. std::shared_ptr<s_pet_db> pet = pet_db.find(mob_id);
  9067. if( !pet ){
  9068. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9069. return SCRIPT_CMD_FAILURE;
  9070. }
  9071. sd->catch_target_class = mob_id;
  9072. std::shared_ptr<s_mob_db> mdb = mob_db.find(pet->class_);
  9073. 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() );
  9074. return SCRIPT_CMD_SUCCESS;
  9075. }
  9076. /**
  9077. * Give player exp base,job * quest_exp_rate/100
  9078. * getexp <base xp>,<job xp>{,<char_id>};
  9079. **/
  9080. BUILDIN_FUNC(getexp)
  9081. {
  9082. TBL_PC* sd;
  9083. int base=0,job=0;
  9084. double bonus;
  9085. if (!script_charid2sd(4,sd))
  9086. return SCRIPT_CMD_FAILURE;
  9087. base=script_getnum(st,2);
  9088. job =script_getnum(st,3);
  9089. if(base<0 || job<0)
  9090. return SCRIPT_CMD_SUCCESS;
  9091. // bonus for npc-given exp
  9092. bonus = battle_config.quest_exp_rate / 100.;
  9093. if (base)
  9094. base = (int) cap_value(base * bonus, 0, INT_MAX);
  9095. if (job)
  9096. job = (int) cap_value(job * bonus, 0, INT_MAX);
  9097. pc_gainexp(sd, NULL, base, job, 1);
  9098. #ifdef RENEWAL
  9099. if (base && sd->hd)
  9100. hom_gainexp(sd->hd, base * battle_config.homunculus_exp_gain / 100); // Homunculus only receive 10% of EXP
  9101. #endif
  9102. return SCRIPT_CMD_SUCCESS;
  9103. }
  9104. /*==========================================
  9105. * Gain guild exp [Celest]
  9106. *------------------------------------------*/
  9107. BUILDIN_FUNC(guildgetexp)
  9108. {
  9109. TBL_PC* sd;
  9110. int exp;
  9111. if( !script_rid2sd(sd) )
  9112. return SCRIPT_CMD_SUCCESS;
  9113. exp = script_getnum(st,2);
  9114. if(exp < 0)
  9115. return SCRIPT_CMD_SUCCESS;
  9116. if(sd && sd->status.guild_id > 0)
  9117. guild_getexp (sd, exp);
  9118. return SCRIPT_CMD_SUCCESS;
  9119. }
  9120. /*==========================================
  9121. * Changes the guild master of a guild [Skotlex]
  9122. *------------------------------------------*/
  9123. BUILDIN_FUNC(guildchangegm)
  9124. {
  9125. TBL_PC *sd;
  9126. int guild_id;
  9127. const char *name;
  9128. guild_id = script_getnum(st,2);
  9129. name = script_getstr(st,3);
  9130. sd=map_nick2sd(name,false);
  9131. if (!sd)
  9132. script_pushint(st,0);
  9133. else
  9134. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9135. return SCRIPT_CMD_SUCCESS;
  9136. }
  9137. /*==========================================
  9138. * Spawn a monster:
  9139. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9140. *------------------------------------------*/
  9141. BUILDIN_FUNC(monster)
  9142. {
  9143. const char* mapn = script_getstr(st,2);
  9144. int x = script_getnum(st,3);
  9145. int y = script_getnum(st,4);
  9146. const char* str = script_getstr(st,5);
  9147. int class_ = script_getnum(st,6);
  9148. int amount = script_getnum(st,7);
  9149. const char* event = "";
  9150. unsigned int size = SZ_SMALL;
  9151. enum mob_ai ai = AI_NONE;
  9152. struct map_session_data* sd;
  9153. int16 m;
  9154. int i;
  9155. if (script_hasdata(st, 8)) {
  9156. event = script_getstr(st, 8);
  9157. check_event(st, event);
  9158. }
  9159. if (script_hasdata(st, 9)) {
  9160. size = script_getnum(st, 9);
  9161. if (size > SZ_BIG) {
  9162. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9163. return SCRIPT_CMD_FAILURE;
  9164. }
  9165. }
  9166. if (script_hasdata(st, 10)) {
  9167. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9168. if (ai >= AI_MAX) {
  9169. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9170. return SCRIPT_CMD_FAILURE;
  9171. }
  9172. }
  9173. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9174. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9175. return SCRIPT_CMD_FAILURE;
  9176. }
  9177. sd = map_id2sd(st->rid);
  9178. if (sd && strcmp(mapn, "this") == 0)
  9179. m = sd->bl.m;
  9180. else
  9181. m = map_mapname2mapid(mapn);
  9182. for(i = 0; i < amount; i++) { //not optimised
  9183. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9184. if (mobid)
  9185. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9186. }
  9187. return SCRIPT_CMD_SUCCESS;
  9188. }
  9189. /*==========================================
  9190. * Request List of Monster Drops
  9191. *------------------------------------------*/
  9192. BUILDIN_FUNC(getmobdrops)
  9193. {
  9194. int class_ = script_getnum(st,2);
  9195. int i, j = 0;
  9196. if( !mobdb_checkid(class_) )
  9197. {
  9198. script_pushint(st, 0);
  9199. return SCRIPT_CMD_SUCCESS;
  9200. }
  9201. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  9202. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9203. {
  9204. if( mob->dropitem[i].nameid == 0 )
  9205. continue;
  9206. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  9207. continue;
  9208. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9209. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].rate);
  9210. j++;
  9211. }
  9212. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9213. script_pushint(st, 1);
  9214. return SCRIPT_CMD_SUCCESS;
  9215. }
  9216. /*==========================================
  9217. * Spawn a monster in a random location
  9218. * in x0,x1,y0,y1 area.
  9219. *------------------------------------------*/
  9220. BUILDIN_FUNC(areamonster)
  9221. {
  9222. const char* mapn = script_getstr(st,2);
  9223. int x0 = script_getnum(st,3);
  9224. int y0 = script_getnum(st,4);
  9225. int x1 = script_getnum(st,5);
  9226. int y1 = script_getnum(st,6);
  9227. const char* str = script_getstr(st,7);
  9228. int class_ = script_getnum(st,8);
  9229. int amount = script_getnum(st,9);
  9230. const char* event = "";
  9231. unsigned int size = SZ_SMALL;
  9232. enum mob_ai ai = AI_NONE;
  9233. struct map_session_data* sd;
  9234. int16 m;
  9235. int i;
  9236. if (script_hasdata(st,10)) {
  9237. event = script_getstr(st, 10);
  9238. check_event(st, event);
  9239. }
  9240. if (script_hasdata(st, 11)) {
  9241. size = script_getnum(st, 11);
  9242. if (size > 3) {
  9243. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9244. return SCRIPT_CMD_FAILURE;
  9245. }
  9246. }
  9247. if (script_hasdata(st, 12)) {
  9248. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9249. if (ai >= AI_MAX) {
  9250. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9251. return SCRIPT_CMD_FAILURE;
  9252. }
  9253. }
  9254. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9255. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9256. return SCRIPT_CMD_FAILURE;
  9257. }
  9258. sd = map_id2sd(st->rid);
  9259. if (sd && strcmp(mapn, "this") == 0)
  9260. m = sd->bl.m;
  9261. else
  9262. m = map_mapname2mapid(mapn);
  9263. for(i = 0; i < amount; i++) { //not optimised
  9264. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9265. if (mobid)
  9266. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9267. }
  9268. return SCRIPT_CMD_SUCCESS;
  9269. }
  9270. /*==========================================
  9271. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9272. *------------------------------------------*/
  9273. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9274. { //same fix but with killmonster instead - stripping events from mobs.
  9275. TBL_MOB* md = (TBL_MOB*)bl;
  9276. char *event=va_arg(ap,char *);
  9277. int allflag=va_arg(ap,int);
  9278. md->state.npc_killmonster = 1;
  9279. if(!allflag){
  9280. if(strcmp(event,md->npc_event)==0)
  9281. status_kill(bl);
  9282. }else{
  9283. if(!md->spawn)
  9284. status_kill(bl);
  9285. }
  9286. md->state.npc_killmonster = 0;
  9287. return SCRIPT_CMD_SUCCESS;
  9288. }
  9289. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9290. {
  9291. TBL_MOB* md = (TBL_MOB*)bl;
  9292. char *event=va_arg(ap,char *);
  9293. int allflag=va_arg(ap,int);
  9294. if(!allflag){
  9295. if(strcmp(event,md->npc_event)==0)
  9296. status_kill(bl);
  9297. }else{
  9298. if(!md->spawn)
  9299. status_kill(bl);
  9300. }
  9301. return SCRIPT_CMD_SUCCESS;
  9302. }
  9303. BUILDIN_FUNC(killmonster)
  9304. {
  9305. const char *mapname,*event;
  9306. int16 m,allflag=0;
  9307. mapname=script_getstr(st,2);
  9308. event=script_getstr(st,3);
  9309. if(strcmp(event,"All")==0)
  9310. allflag = 1;
  9311. else
  9312. check_event(st, event);
  9313. if( (m=map_mapname2mapid(mapname))<0 )
  9314. return SCRIPT_CMD_SUCCESS;
  9315. if( script_hasdata(st,4) ) {
  9316. if ( script_getnum(st,4) == 1 ) {
  9317. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9318. return SCRIPT_CMD_SUCCESS;
  9319. }
  9320. }
  9321. map_freeblock_lock();
  9322. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9323. map_freeblock_unlock();
  9324. return SCRIPT_CMD_SUCCESS;
  9325. }
  9326. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9327. { //Strips the event from the mob if it's killed the old method.
  9328. struct mob_data *md;
  9329. md = BL_CAST(BL_MOB, bl);
  9330. if (md->npc_event[0])
  9331. md->npc_event[0] = 0;
  9332. status_kill(bl);
  9333. return 0;
  9334. }
  9335. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9336. {
  9337. status_kill(bl);
  9338. return 0;
  9339. }
  9340. BUILDIN_FUNC(killmonsterall)
  9341. {
  9342. const char *mapname;
  9343. int16 m;
  9344. mapname=script_getstr(st,2);
  9345. if( (m = map_mapname2mapid(mapname))<0 )
  9346. return SCRIPT_CMD_SUCCESS;
  9347. if( script_hasdata(st,3) ) {
  9348. if ( script_getnum(st,3) == 1 ) {
  9349. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9350. return SCRIPT_CMD_SUCCESS;
  9351. }
  9352. }
  9353. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9354. return SCRIPT_CMD_SUCCESS;
  9355. }
  9356. /*==========================================
  9357. * Creates a clone of a player.
  9358. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9359. *------------------------------------------*/
  9360. BUILDIN_FUNC(clone)
  9361. {
  9362. TBL_PC *sd, *msd=NULL;
  9363. uint32 char_id, master_id = 0, x, y, flag = 0;
  9364. int16 m;
  9365. enum e_mode mode = MD_NONE;
  9366. unsigned int duration = 0;
  9367. const char *mapname,*event;
  9368. mapname=script_getstr(st,2);
  9369. x=script_getnum(st,3);
  9370. y=script_getnum(st,4);
  9371. event=script_getstr(st,5);
  9372. char_id=script_getnum(st,6);
  9373. if( script_hasdata(st,7) )
  9374. master_id=script_getnum(st,7);
  9375. if( script_hasdata(st,8) )
  9376. mode=static_cast<e_mode>(script_getnum(st,8));
  9377. if( script_hasdata(st,9) )
  9378. flag=script_getnum(st,9);
  9379. if( script_hasdata(st,10) )
  9380. duration=script_getnum(st,10);
  9381. check_event(st, event);
  9382. m = map_mapname2mapid(mapname);
  9383. if (m < 0)
  9384. return SCRIPT_CMD_SUCCESS;
  9385. sd = map_charid2sd(char_id);
  9386. if (master_id) {
  9387. msd = map_charid2sd(master_id);
  9388. if (msd)
  9389. master_id = msd->bl.id;
  9390. else
  9391. master_id = 0;
  9392. }
  9393. if (sd) //Return ID of newly crafted clone.
  9394. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9395. else //Failed to create clone.
  9396. script_pushint(st,0);
  9397. return SCRIPT_CMD_SUCCESS;
  9398. }
  9399. /*==========================================
  9400. *------------------------------------------*/
  9401. BUILDIN_FUNC(doevent)
  9402. {
  9403. const char* event;
  9404. struct map_session_data* sd;
  9405. if( !script_rid2sd(sd) )
  9406. return SCRIPT_CMD_SUCCESS;
  9407. event = script_getstr(st,2);
  9408. check_event(st, event);
  9409. npc_event(sd, event, 0);
  9410. return SCRIPT_CMD_SUCCESS;
  9411. }
  9412. /*==========================================
  9413. *------------------------------------------*/
  9414. BUILDIN_FUNC(donpcevent)
  9415. {
  9416. const char* event = script_getstr(st,2);
  9417. check_event(st, event);
  9418. if( !npc_event_do(event) ) {
  9419. struct npc_data * nd = map_id2nd(st->oid);
  9420. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9421. script_pushint(st, 0);
  9422. } else
  9423. script_pushint(st, 1);
  9424. return SCRIPT_CMD_SUCCESS;
  9425. }
  9426. /// for Aegis compatibility
  9427. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9428. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9429. {
  9430. const char* npc = script_getstr(st,2);
  9431. const char* command = script_getstr(st,3);
  9432. char event[EVENT_NAME_LENGTH];
  9433. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9434. check_event(st, event);
  9435. if( npc_event_do(event) ){
  9436. script_pushint(st, true);
  9437. }else{
  9438. struct npc_data * nd = map_id2nd(st->oid);
  9439. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9440. script_pushint(st, false);
  9441. }
  9442. return SCRIPT_CMD_SUCCESS;
  9443. }
  9444. /*==========================================
  9445. *------------------------------------------*/
  9446. BUILDIN_FUNC(addtimer)
  9447. {
  9448. int tick;
  9449. const char* event;
  9450. TBL_PC* sd;
  9451. if( !script_rid2sd(sd) )
  9452. return SCRIPT_CMD_SUCCESS;
  9453. tick = script_getnum(st,2);
  9454. event = script_getstr(st, 3);
  9455. check_event(st, event);
  9456. if (!pc_addeventtimer(sd,tick,event)) {
  9457. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9458. return SCRIPT_CMD_FAILURE;
  9459. }
  9460. return SCRIPT_CMD_SUCCESS;
  9461. }
  9462. /*==========================================
  9463. *------------------------------------------*/
  9464. BUILDIN_FUNC(deltimer)
  9465. {
  9466. const char *event;
  9467. TBL_PC* sd;
  9468. if( !script_rid2sd(sd) )
  9469. return SCRIPT_CMD_SUCCESS;
  9470. event=script_getstr(st, 2);
  9471. check_event(st, event);
  9472. pc_deleventtimer(sd,event);
  9473. return SCRIPT_CMD_SUCCESS;
  9474. }
  9475. /*==========================================
  9476. *------------------------------------------*/
  9477. BUILDIN_FUNC(addtimercount)
  9478. {
  9479. const char *event;
  9480. int tick;
  9481. TBL_PC* sd;
  9482. if( !script_rid2sd(sd) )
  9483. return SCRIPT_CMD_SUCCESS;
  9484. tick=script_getnum(st,2);
  9485. event=script_getstr(st,3);
  9486. check_event(st, event);
  9487. pc_addeventtimercount(sd,event,tick);
  9488. return SCRIPT_CMD_SUCCESS;
  9489. }
  9490. /*==========================================
  9491. *------------------------------------------*/
  9492. BUILDIN_FUNC(initnpctimer)
  9493. {
  9494. struct npc_data *nd;
  9495. int flag = 0;
  9496. if( script_hasdata(st,3) )
  9497. { //Two arguments: NPC name and attach flag.
  9498. nd = npc_name2id(script_getstr(st, 2));
  9499. flag = script_getnum(st,3);
  9500. }
  9501. else if( script_hasdata(st,2) )
  9502. { //Check if argument is numeric (flag) or string (npc name)
  9503. if( script_isstring(st, 2) ) //NPC name
  9504. nd = npc_name2id(script_getstr(st, 2));
  9505. else //Flag
  9506. {
  9507. nd = (struct npc_data *)map_id2bl(st->oid);
  9508. flag = script_getnum(st, 2);
  9509. }
  9510. }
  9511. else
  9512. nd = (struct npc_data *)map_id2bl(st->oid);
  9513. if( !nd )
  9514. return SCRIPT_CMD_SUCCESS;
  9515. if( flag ) //Attach
  9516. {
  9517. TBL_PC* sd;
  9518. if( !script_rid2sd(sd) )
  9519. return SCRIPT_CMD_SUCCESS;
  9520. nd->u.scr.rid = sd->bl.id;
  9521. }
  9522. nd->u.scr.timertick = 0;
  9523. npc_settimerevent_tick(nd,0);
  9524. npc_timerevent_start(nd, st->rid);
  9525. return SCRIPT_CMD_SUCCESS;
  9526. }
  9527. /*==========================================
  9528. *------------------------------------------*/
  9529. BUILDIN_FUNC(startnpctimer)
  9530. {
  9531. struct npc_data *nd;
  9532. int flag = 0;
  9533. if( script_hasdata(st,3) )
  9534. { //Two arguments: NPC name and attach flag.
  9535. nd = npc_name2id(script_getstr(st, 2));
  9536. flag = script_getnum(st,3);
  9537. }
  9538. else if( script_hasdata(st,2) )
  9539. { //Check if argument is numeric (flag) or string (npc name)
  9540. if( script_isstring(st, 2) ) //NPC name
  9541. nd = npc_name2id(script_getstr(st, 2));
  9542. else //Flag
  9543. {
  9544. nd = (struct npc_data *)map_id2bl(st->oid);
  9545. flag = script_getnum(st, 2);
  9546. }
  9547. }
  9548. else
  9549. nd=(struct npc_data *)map_id2bl(st->oid);
  9550. if( !nd )
  9551. return SCRIPT_CMD_SUCCESS;
  9552. if( flag ) //Attach
  9553. {
  9554. TBL_PC* sd;
  9555. if( !script_rid2sd(sd) )
  9556. return SCRIPT_CMD_SUCCESS;
  9557. nd->u.scr.rid = sd->bl.id;
  9558. }
  9559. npc_timerevent_start(nd, st->rid);
  9560. return SCRIPT_CMD_SUCCESS;
  9561. }
  9562. /*==========================================
  9563. *------------------------------------------*/
  9564. BUILDIN_FUNC(stopnpctimer)
  9565. {
  9566. struct npc_data *nd;
  9567. int flag = 0;
  9568. if( script_hasdata(st,3) )
  9569. { //Two arguments: NPC name and attach flag.
  9570. nd = npc_name2id(script_getstr(st, 2));
  9571. flag = script_getnum(st,3);
  9572. }
  9573. else if( script_hasdata(st,2) )
  9574. { //Check if argument is numeric (flag) or string (npc name)
  9575. if( script_isstring(st, 2) ) //NPC name
  9576. nd = npc_name2id(script_getstr(st, 2));
  9577. else //Flag
  9578. {
  9579. nd = (struct npc_data *)map_id2bl(st->oid);
  9580. flag = script_getnum(st, 2);
  9581. }
  9582. }
  9583. else
  9584. nd=(struct npc_data *)map_id2bl(st->oid);
  9585. if( !nd )
  9586. return SCRIPT_CMD_SUCCESS;
  9587. if( flag ) //Detach
  9588. nd->u.scr.rid = 0;
  9589. npc_timerevent_stop(nd);
  9590. return SCRIPT_CMD_SUCCESS;
  9591. }
  9592. /*==========================================
  9593. *------------------------------------------*/
  9594. BUILDIN_FUNC(getnpctimer)
  9595. {
  9596. struct npc_data *nd;
  9597. TBL_PC *sd;
  9598. int type = script_getnum(st,2);
  9599. t_tick val = 0;
  9600. if( script_hasdata(st,3) )
  9601. nd = npc_name2id(script_getstr(st,3));
  9602. else
  9603. nd = (struct npc_data *)map_id2bl(st->oid);
  9604. if( !nd || nd->bl.type != BL_NPC )
  9605. {
  9606. script_pushint(st,0);
  9607. ShowError("getnpctimer: Invalid NPC.\n");
  9608. return SCRIPT_CMD_FAILURE;
  9609. }
  9610. switch( type )
  9611. {
  9612. case 0: val = npc_gettimerevent_tick(nd); break;
  9613. case 1:
  9614. if( nd->u.scr.rid )
  9615. {
  9616. sd = map_id2sd(nd->u.scr.rid);
  9617. if( !sd )
  9618. {
  9619. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9620. break;
  9621. }
  9622. val = (sd->npc_timer_id != INVALID_TIMER);
  9623. }
  9624. else
  9625. val = (nd->u.scr.timerid != INVALID_TIMER);
  9626. break;
  9627. case 2: val = nd->u.scr.timeramount; break;
  9628. }
  9629. script_pushint(st,val);
  9630. return SCRIPT_CMD_SUCCESS;
  9631. }
  9632. /*==========================================
  9633. *------------------------------------------*/
  9634. BUILDIN_FUNC(setnpctimer)
  9635. {
  9636. int tick;
  9637. struct npc_data *nd;
  9638. tick = script_getnum(st,2);
  9639. if( script_hasdata(st,3) )
  9640. nd = npc_name2id(script_getstr(st,3));
  9641. else
  9642. nd = (struct npc_data *)map_id2bl(st->oid);
  9643. if( !nd || nd->bl.type != BL_NPC )
  9644. {
  9645. script_pushint(st,1);
  9646. ShowError("setnpctimer: Invalid NPC.\n");
  9647. return SCRIPT_CMD_FAILURE;
  9648. }
  9649. npc_settimerevent_tick(nd,tick);
  9650. script_pushint(st,0);
  9651. return SCRIPT_CMD_SUCCESS;
  9652. }
  9653. /*==========================================
  9654. * attaches the player rid to the timer [Celest]
  9655. *------------------------------------------*/
  9656. BUILDIN_FUNC(attachnpctimer)
  9657. {
  9658. TBL_PC *sd;
  9659. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9660. if( !nd || nd->bl.type != BL_NPC )
  9661. {
  9662. script_pushint(st,1);
  9663. ShowError("setnpctimer: Invalid NPC.\n");
  9664. return SCRIPT_CMD_FAILURE;
  9665. }
  9666. if( !script_nick2sd(2,sd) ){
  9667. script_pushint(st,1);
  9668. ShowWarning("attachnpctimer: Invalid player.\n");
  9669. return SCRIPT_CMD_FAILURE;
  9670. }
  9671. nd->u.scr.rid = sd->bl.id;
  9672. script_pushint(st,0);
  9673. return SCRIPT_CMD_SUCCESS;
  9674. }
  9675. /*==========================================
  9676. * detaches a player rid from the timer [Celest]
  9677. *------------------------------------------*/
  9678. BUILDIN_FUNC(detachnpctimer)
  9679. {
  9680. struct npc_data *nd;
  9681. if( script_hasdata(st,2) )
  9682. nd = npc_name2id(script_getstr(st,2));
  9683. else
  9684. nd = (struct npc_data *)map_id2bl(st->oid);
  9685. if( !nd || nd->bl.type != BL_NPC )
  9686. {
  9687. script_pushint(st,1);
  9688. ShowError("detachnpctimer: Invalid NPC.\n");
  9689. return SCRIPT_CMD_FAILURE;
  9690. }
  9691. nd->u.scr.rid = 0;
  9692. script_pushint(st,0);
  9693. return SCRIPT_CMD_SUCCESS;
  9694. }
  9695. /*==========================================
  9696. * To avoid "player not attached" script errors, this function is provided,
  9697. * it checks if there is a player attached to the current script. [Skotlex]
  9698. * If no, returns 0, if yes, returns the account_id of the attached player.
  9699. *------------------------------------------*/
  9700. BUILDIN_FUNC(playerattached)
  9701. {
  9702. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9703. script_pushint(st,0);
  9704. else
  9705. script_pushint(st,st->rid);
  9706. return SCRIPT_CMD_SUCCESS;
  9707. }
  9708. /*==========================================
  9709. *------------------------------------------*/
  9710. BUILDIN_FUNC(announce)
  9711. {
  9712. const char *mes = script_getstr(st,2);
  9713. int flag = script_getnum(st,3);
  9714. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9715. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9716. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9717. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9718. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9719. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9720. {
  9721. send_target target;
  9722. struct block_list *bl;
  9723. // If bc_npc flag is set, use NPC as broadcast source
  9724. if(flag&BC_NPC){
  9725. bl = map_id2bl(st->oid);
  9726. }else{
  9727. struct map_session_data* sd;
  9728. if(script_charid2sd(9, sd))
  9729. bl = &sd->bl;
  9730. else
  9731. bl = NULL;
  9732. }
  9733. if (bl == NULL)
  9734. return SCRIPT_CMD_SUCCESS;
  9735. switch (flag&BC_TARGET_MASK) {
  9736. case BC_MAP: target = ALL_SAMEMAP; break;
  9737. case BC_AREA: target = AREA; break;
  9738. case BC_SELF: target = SELF; break;
  9739. default: target = ALL_CLIENT; break; // BC_ALL
  9740. }
  9741. if (fontColor)
  9742. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9743. else
  9744. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9745. }
  9746. else
  9747. {
  9748. if (fontColor)
  9749. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9750. else
  9751. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9752. }
  9753. return SCRIPT_CMD_SUCCESS;
  9754. }
  9755. /*==========================================
  9756. *------------------------------------------*/
  9757. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9758. {
  9759. char *mes = va_arg(ap, char *);
  9760. int len = va_arg(ap, int);
  9761. int type = va_arg(ap, int);
  9762. char *fontColor = va_arg(ap, char *);
  9763. short fontType = (short)va_arg(ap, int);
  9764. short fontSize = (short)va_arg(ap, int);
  9765. short fontAlign = (short)va_arg(ap, int);
  9766. short fontY = (short)va_arg(ap, int);
  9767. if (fontColor)
  9768. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9769. else
  9770. clif_broadcast(bl, mes, len, type, SELF);
  9771. return SCRIPT_CMD_SUCCESS;
  9772. }
  9773. BUILDIN_FUNC(mapannounce)
  9774. {
  9775. const char *mapname = script_getstr(st,2);
  9776. const char *mes = script_getstr(st,3);
  9777. int flag = script_getnum(st,4);
  9778. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9779. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9780. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9781. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9782. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9783. int16 m;
  9784. if ((m = map_mapname2mapid(mapname)) < 0)
  9785. return SCRIPT_CMD_SUCCESS;
  9786. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9787. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9788. return SCRIPT_CMD_SUCCESS;
  9789. }
  9790. /*==========================================
  9791. *------------------------------------------*/
  9792. BUILDIN_FUNC(areaannounce)
  9793. {
  9794. const char *mapname = script_getstr(st,2);
  9795. int x0 = script_getnum(st,3);
  9796. int y0 = script_getnum(st,4);
  9797. int x1 = script_getnum(st,5);
  9798. int y1 = script_getnum(st,6);
  9799. const char *mes = script_getstr(st,7);
  9800. int flag = script_getnum(st,8);
  9801. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9802. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9803. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9804. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9805. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9806. int16 m;
  9807. if ((m = map_mapname2mapid(mapname)) < 0)
  9808. return SCRIPT_CMD_SUCCESS;
  9809. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9810. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9811. return SCRIPT_CMD_SUCCESS;
  9812. }
  9813. /*==========================================
  9814. *------------------------------------------*/
  9815. BUILDIN_FUNC(getusers)
  9816. {
  9817. int flag, val = 0;
  9818. struct map_session_data* sd;
  9819. struct block_list* bl = NULL;
  9820. flag = script_getnum(st,2);
  9821. switch(flag&0x07)
  9822. {
  9823. case 0:
  9824. if(flag&0x8)
  9825. {// npc
  9826. bl = map_id2bl(st->oid);
  9827. }
  9828. else if(script_rid2sd(sd))
  9829. {// pc
  9830. bl = &sd->bl;
  9831. }
  9832. if(bl)
  9833. {
  9834. val = map_getmapdata(bl->m)->users;
  9835. }
  9836. break;
  9837. case 1:
  9838. val = map_getusers();
  9839. break;
  9840. default:
  9841. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9842. script_pushint(st,0);
  9843. return SCRIPT_CMD_FAILURE;
  9844. }
  9845. script_pushint(st,val);
  9846. return SCRIPT_CMD_SUCCESS;
  9847. }
  9848. /*==========================================
  9849. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9850. *------------------------------------------*/
  9851. BUILDIN_FUNC(getmapguildusers)
  9852. {
  9853. const char *str;
  9854. int16 m;
  9855. int gid;
  9856. int c=0;
  9857. struct guild *g = NULL;
  9858. str=script_getstr(st,2);
  9859. gid=script_getnum(st,3);
  9860. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9861. script_pushint(st,-1);
  9862. return SCRIPT_CMD_SUCCESS;
  9863. }
  9864. g = guild_search(gid);
  9865. if (g){
  9866. unsigned short i;
  9867. for(i = 0; i < g->max_member; i++)
  9868. {
  9869. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9870. c++;
  9871. }
  9872. }
  9873. script_pushint(st,c);
  9874. return SCRIPT_CMD_SUCCESS;
  9875. }
  9876. /*==========================================
  9877. *------------------------------------------*/
  9878. BUILDIN_FUNC(getmapusers)
  9879. {
  9880. const char *str;
  9881. int16 m;
  9882. str=script_getstr(st,2);
  9883. if( (m=map_mapname2mapid(str))< 0){
  9884. script_pushint(st,-1);
  9885. return SCRIPT_CMD_SUCCESS;
  9886. }
  9887. script_pushint(st,map_getmapdata(m)->users);
  9888. return SCRIPT_CMD_SUCCESS;
  9889. }
  9890. /*==========================================
  9891. *------------------------------------------*/
  9892. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9893. {
  9894. int *users=va_arg(ap,int *);
  9895. (*users)++;
  9896. return SCRIPT_CMD_SUCCESS;
  9897. }
  9898. BUILDIN_FUNC(getareausers)
  9899. {
  9900. const char *str;
  9901. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9902. str=script_getstr(st,2);
  9903. x0=script_getnum(st,3);
  9904. y0=script_getnum(st,4);
  9905. x1=script_getnum(st,5);
  9906. y1=script_getnum(st,6);
  9907. if( (m=map_mapname2mapid(str))< 0){
  9908. script_pushint(st,-1);
  9909. return SCRIPT_CMD_SUCCESS;
  9910. }
  9911. map_foreachinallarea(buildin_getareausers_sub,
  9912. m,x0,y0,x1,y1,BL_PC,&users);
  9913. script_pushint(st,users);
  9914. return SCRIPT_CMD_SUCCESS;
  9915. }
  9916. /*==========================================
  9917. * getunits(<type>{,<array_variable>[<first value>]})
  9918. * getmapunits(<type>,<"map name">{,<array_variable>[<first value>]})
  9919. * getareaunits(<type>,<"map name">,<x1>,<y1>,<x2>,<y2>{,<array_variable>[<first value>]})
  9920. *------------------------------------------*/
  9921. BUILDIN_FUNC(getunits)
  9922. {
  9923. struct block_list *bl = NULL;
  9924. struct map_session_data *sd = NULL;
  9925. struct script_data *data = NULL;
  9926. char *command = (char *)script_getfuncname(st);
  9927. const char *str;
  9928. const char *name;
  9929. int type = script_getnum(st, 2);
  9930. int size = 0;
  9931. int32 idx, id;
  9932. int16 m = -1, x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  9933. if (!strcmp(command, "getmapunits"))
  9934. {
  9935. str = script_getstr(st, 3);
  9936. if ((m = map_mapname2mapid(str)) < 0) {
  9937. script_pushint(st, -1);
  9938. st->state = END;
  9939. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  9940. return SCRIPT_CMD_FAILURE;
  9941. }
  9942. if (script_hasdata(st, 4))
  9943. data = script_getdata(st, 4);
  9944. }
  9945. else if (!strcmp(command, "getareaunits"))
  9946. {
  9947. str = script_getstr(st, 3);
  9948. if ((m = map_mapname2mapid(str)) < 0) {
  9949. script_pushint(st, -1);
  9950. st->state = END;
  9951. ShowWarning("buildin_%s: Unknown map '%s'.\n", command, str);
  9952. return SCRIPT_CMD_FAILURE;
  9953. }
  9954. x0 = min(script_getnum(st, 4), script_getnum(st, 6));
  9955. y0 = min(script_getnum(st, 5), script_getnum(st, 7));
  9956. x1 = max(script_getnum(st, 4), script_getnum(st, 6));
  9957. y1 = max(script_getnum(st, 5), script_getnum(st, 7));
  9958. if (script_hasdata(st, 8))
  9959. data = script_getdata(st, 8);
  9960. }
  9961. else
  9962. {
  9963. if (script_hasdata(st, 3))
  9964. data = script_getdata(st, 3);
  9965. }
  9966. if (data)
  9967. {
  9968. if (!data_isreference(data))
  9969. {
  9970. ShowError("buildin_%s: not a variable\n", command);
  9971. script_reportdata(data);
  9972. st->state = END;
  9973. return SCRIPT_CMD_FAILURE;
  9974. }
  9975. id = reference_getid(data);
  9976. idx = reference_getindex(data);
  9977. name = reference_getname(data);
  9978. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  9979. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", command, name);
  9980. return SCRIPT_CMD_FAILURE;
  9981. }
  9982. }
  9983. struct s_mapiterator *iter = mapit_alloc(MAPIT_NORMAL, bl_type(type));
  9984. for (bl = (struct block_list*)mapit_first(iter); mapit_exists(iter); bl = (struct block_list*)mapit_next(iter))
  9985. {
  9986. 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)))
  9987. {
  9988. if( data ){
  9989. if( is_string_variable( name ) ){
  9990. set_reg_str( st, sd, reference_uid( id, idx + size ), name, status_get_name( bl ), reference_getref( data ) );
  9991. }else{
  9992. set_reg_num( st, sd, reference_uid( id, idx + size ), name, bl->id, reference_getref( data ) );
  9993. }
  9994. }
  9995. size++;
  9996. }
  9997. }
  9998. mapit_free(iter);
  9999. script_pushint(st, size);
  10000. return SCRIPT_CMD_SUCCESS;
  10001. }
  10002. /*==========================================
  10003. *------------------------------------------*/
  10004. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  10005. {
  10006. t_itemid nameid = va_arg(ap, t_itemid);
  10007. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  10008. struct flooritem_data *drop=(struct flooritem_data *)bl;
  10009. if(drop->item.nameid==nameid)
  10010. (*amount)+=drop->item.amount;
  10011. return SCRIPT_CMD_SUCCESS;
  10012. }
  10013. BUILDIN_FUNC(getareadropitem)
  10014. {
  10015. const char *str;
  10016. int16 m,x0,y0,x1,y1;
  10017. t_itemid nameid;
  10018. unsigned short amount = 0;
  10019. str=script_getstr(st,2);
  10020. x0=script_getnum(st,3);
  10021. y0=script_getnum(st,4);
  10022. x1=script_getnum(st,5);
  10023. y1=script_getnum(st,6);
  10024. if( script_isstring(st, 7) ){
  10025. const char *name = script_getstr(st, 7);
  10026. struct item_data *item_data = itemdb_searchname(name);
  10027. if( item_data )
  10028. nameid=item_data->nameid;
  10029. else{
  10030. ShowError( "buildin_getareadropitem: Unknown item %s\n", name );
  10031. return SCRIPT_CMD_FAILURE;
  10032. }
  10033. }else{
  10034. nameid = script_getnum(st, 7);
  10035. if( !itemdb_exists( nameid ) ){
  10036. ShowError( "buildin_getareadropitem: Unknown item id %u\n", nameid );
  10037. return SCRIPT_CMD_FAILURE;
  10038. }
  10039. }
  10040. if( (m=map_mapname2mapid(str))< 0){
  10041. script_pushint(st,-1);
  10042. return SCRIPT_CMD_SUCCESS;
  10043. }
  10044. map_foreachinallarea(buildin_getareadropitem_sub,
  10045. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  10046. script_pushint(st,amount);
  10047. return SCRIPT_CMD_SUCCESS;
  10048. }
  10049. /*==========================================
  10050. *------------------------------------------*/
  10051. BUILDIN_FUNC(enablenpc)
  10052. {
  10053. const char *str = script_getstr(st,2);
  10054. if (npc_enable(str,1))
  10055. return SCRIPT_CMD_SUCCESS;
  10056. return SCRIPT_CMD_FAILURE;
  10057. }
  10058. /*==========================================
  10059. *------------------------------------------*/
  10060. BUILDIN_FUNC(disablenpc)
  10061. {
  10062. const char *str = script_getstr(st,2);
  10063. if (npc_enable(str,0))
  10064. return SCRIPT_CMD_SUCCESS;
  10065. return SCRIPT_CMD_FAILURE;
  10066. }
  10067. /*==========================================
  10068. *------------------------------------------*/
  10069. BUILDIN_FUNC(hideoffnpc)
  10070. {
  10071. const char *str = script_getstr(st,2);
  10072. if (npc_enable(str,2))
  10073. return SCRIPT_CMD_SUCCESS;
  10074. return SCRIPT_CMD_FAILURE;
  10075. }
  10076. /*==========================================
  10077. *------------------------------------------*/
  10078. BUILDIN_FUNC(hideonnpc)
  10079. {
  10080. const char *str = script_getstr(st,2);
  10081. if (npc_enable(str,4))
  10082. return SCRIPT_CMD_SUCCESS;
  10083. return SCRIPT_CMD_FAILURE;
  10084. }
  10085. /* Starts a status effect on the target unit or on the attached player.
  10086. *
  10087. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  10088. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  10089. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  10090. * <flag>: enum e_status_change_start_flags
  10091. */
  10092. BUILDIN_FUNC(sc_start)
  10093. {
  10094. TBL_NPC * nd = map_id2nd(st->oid);
  10095. struct block_list* bl;
  10096. enum sc_type type;
  10097. int tick, val1, val2, val3, val4=0, rate, flag;
  10098. char start_type;
  10099. const char* command = script_getfuncname(st);
  10100. if(strstr(command, "4"))
  10101. start_type = 4;
  10102. else if(strstr(command, "2"))
  10103. start_type = 2;
  10104. else
  10105. start_type = 1;
  10106. type = (sc_type)script_getnum(st,2);
  10107. tick = script_getnum(st,3);
  10108. val1 = script_getnum(st,4);
  10109. //If from NPC we make default flag 1 to be unavoidable
  10110. if(nd && nd->bl.id == fake_nd->bl.id)
  10111. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  10112. else
  10113. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  10114. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  10115. if(script_hasdata(st,(6+start_type)))
  10116. bl = map_id2bl(script_getnum(st,(6+start_type)));
  10117. else
  10118. bl = map_id2bl(st->rid);
  10119. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  10120. {// When there isn't a duration specified, try to get it from the skill_db
  10121. tick = skill_get_time(status_sc2skill(type), val1);
  10122. }
  10123. if(potion_flag == 1 && potion_target) { //skill.cpp set the flags before running the script, this is a potion-pitched effect.
  10124. bl = map_id2bl(potion_target);
  10125. tick /= 2;// Thrown potions only last half.
  10126. val4 = 1;// Mark that this was a thrown sc_effect
  10127. }
  10128. if(!bl)
  10129. return SCRIPT_CMD_SUCCESS;
  10130. switch(start_type) {
  10131. case 1:
  10132. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10133. break;
  10134. case 2:
  10135. val2 = script_getnum(st,5);
  10136. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10137. break;
  10138. case 4:
  10139. val2 = script_getnum(st,5);
  10140. val3 = script_getnum(st,6);
  10141. val4 = script_getnum(st,7);
  10142. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10143. break;
  10144. }
  10145. return SCRIPT_CMD_SUCCESS;
  10146. }
  10147. /// Ends one or all status effects on the target unit or on the attached player.
  10148. ///
  10149. /// sc_end <effect_id>{,<unit_id>};
  10150. BUILDIN_FUNC(sc_end)
  10151. {
  10152. struct block_list* bl;
  10153. int type;
  10154. type = script_getnum(st, 2);
  10155. if (script_hasdata(st, 3))
  10156. bl = map_id2bl(script_getnum(st, 3));
  10157. else
  10158. bl = map_id2bl(st->rid);
  10159. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10160. bl = map_id2bl(potion_target);
  10161. if (!bl)
  10162. return SCRIPT_CMD_SUCCESS;
  10163. if (type >= 0 && type < SC_MAX) {
  10164. struct status_change *sc = status_get_sc(bl);
  10165. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  10166. if (!sce)
  10167. return SCRIPT_CMD_SUCCESS;
  10168. switch (type) {
  10169. case SC_WEIGHT50:
  10170. case SC_WEIGHT90:
  10171. case SC_NOCHAT:
  10172. case SC_PUSH_CART:
  10173. case SC_ALL_RIDING:
  10174. case SC_STYLE_CHANGE:
  10175. case SC_MONSTER_TRANSFORM:
  10176. case SC_ACTIVE_MONSTER_TRANSFORM:
  10177. case SC_MTF_ASPD:
  10178. case SC_MTF_RANGEATK:
  10179. case SC_MTF_MATK:
  10180. case SC_MTF_MLEATKED:
  10181. case SC_MTF_CRIDAMAGE:
  10182. case SC_MTF_ASPD2:
  10183. case SC_MTF_RANGEATK2:
  10184. case SC_MTF_MATK2:
  10185. case SC_MTF_MHP:
  10186. case SC_MTF_MSP:
  10187. case SC_MTF_PUMPKIN:
  10188. case SC_MTF_HITFLEE:
  10189. case SC_ATTHASTE_CASH:
  10190. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  10191. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  10192. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  10193. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  10194. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  10195. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  10196. return SCRIPT_CMD_SUCCESS;
  10197. default:
  10198. break;
  10199. }
  10200. //This should help status_change_end force disabling the SC in case it has no limit.
  10201. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10202. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  10203. } else
  10204. status_change_clear(bl, 3); // remove all effects
  10205. return SCRIPT_CMD_SUCCESS;
  10206. }
  10207. /**
  10208. * Ends all status effects from any learned skill on the attached player.
  10209. * if <job_id> was given it will end the effect of that class for the attached player
  10210. * sc_end_class {<char_id>{,<job_id>}};
  10211. */
  10212. BUILDIN_FUNC(sc_end_class)
  10213. {
  10214. struct map_session_data *sd;
  10215. uint16 skill_id;
  10216. int class_;
  10217. if (!script_charid2sd(2, sd))
  10218. return SCRIPT_CMD_FAILURE;
  10219. if (script_hasdata(st, 3))
  10220. class_ = script_getnum(st, 3);
  10221. else
  10222. class_ = sd->status.class_;
  10223. if (!pcdb_checkid(class_)) {
  10224. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10225. return SCRIPT_CMD_FAILURE;
  10226. }
  10227. for (int i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[pc_class2idx(class_)][i].skill_id) > 0; i++) {
  10228. enum sc_type sc = status_skill2sc(skill_id);
  10229. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  10230. status_change_end(&sd->bl, sc, INVALID_TIMER);
  10231. }
  10232. return SCRIPT_CMD_SUCCESS;
  10233. }
  10234. /*==========================================
  10235. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10236. *------------------------------------------*/
  10237. BUILDIN_FUNC(getscrate)
  10238. {
  10239. struct block_list *bl;
  10240. int type;
  10241. t_tick rate;
  10242. type=script_getnum(st,2);
  10243. rate=script_getnum(st,3);
  10244. if( script_hasdata(st,4) ) //get for the bl assigned
  10245. bl = map_id2bl(script_getnum(st,4));
  10246. else
  10247. bl = map_id2bl(st->rid);
  10248. if (bl)
  10249. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10250. script_pushint(st,rate);
  10251. return SCRIPT_CMD_SUCCESS;
  10252. }
  10253. /**
  10254. * getstatus(<effect type>{,<type>{,<char_id>}});
  10255. **/
  10256. BUILDIN_FUNC(getstatus)
  10257. {
  10258. int id, type;
  10259. struct map_session_data* sd;
  10260. if (!script_charid2sd(4,sd))
  10261. return SCRIPT_CMD_FAILURE;
  10262. id = script_getnum(st, 2);
  10263. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10264. if( id <= SC_NONE || id >= SC_MAX )
  10265. {// invalid status type given
  10266. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10267. return SCRIPT_CMD_SUCCESS;
  10268. }
  10269. if( sd->sc.count == 0 || !sd->sc.data[id] )
  10270. {// no status is active
  10271. script_pushint(st, 0);
  10272. return SCRIPT_CMD_SUCCESS;
  10273. }
  10274. switch( type )
  10275. {
  10276. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  10277. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  10278. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  10279. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  10280. case 5:
  10281. {
  10282. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  10283. if( timer )
  10284. {// return the amount of time remaining
  10285. script_pushint(st, timer->tick - gettick());
  10286. } else {
  10287. script_pushint(st, -1);
  10288. }
  10289. }
  10290. break;
  10291. default: script_pushint(st, 1); break;
  10292. }
  10293. return SCRIPT_CMD_SUCCESS;
  10294. }
  10295. /*==========================================
  10296. *
  10297. *------------------------------------------*/
  10298. BUILDIN_FUNC(debugmes)
  10299. {
  10300. const char *str;
  10301. str=script_getstr(st,2);
  10302. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10303. return SCRIPT_CMD_SUCCESS;
  10304. }
  10305. BUILDIN_FUNC( errormes ){
  10306. ShowError( "%s\n", script_getstr( st, 2 ) );
  10307. script_reportsrc( st );
  10308. return SCRIPT_CMD_SUCCESS;
  10309. }
  10310. /*==========================================
  10311. *------------------------------------------*/
  10312. BUILDIN_FUNC(catchpet)
  10313. {
  10314. int pet_id;
  10315. TBL_PC *sd;
  10316. if( !script_rid2sd(sd) )
  10317. return SCRIPT_CMD_SUCCESS;
  10318. pet_id= script_getnum(st,2);
  10319. pet_catch_process1(sd,pet_id);
  10320. return SCRIPT_CMD_SUCCESS;
  10321. }
  10322. /*==========================================
  10323. * [orn]
  10324. *------------------------------------------*/
  10325. BUILDIN_FUNC(homunculus_evolution)
  10326. {
  10327. TBL_PC *sd;
  10328. if( !script_rid2sd(sd) )
  10329. return SCRIPT_CMD_SUCCESS;
  10330. if(hom_is_active(sd->hd))
  10331. {
  10332. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10333. hom_evolution(sd->hd);
  10334. else
  10335. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10336. }
  10337. return SCRIPT_CMD_SUCCESS;
  10338. }
  10339. /*==========================================
  10340. * Checks for vaporized morph state
  10341. * and deletes ITEMID_STRANGE_EMBRYO.
  10342. *------------------------------------------*/
  10343. BUILDIN_FUNC(homunculus_mutate)
  10344. {
  10345. int homun_id;
  10346. TBL_PC *sd;
  10347. if( !script_rid2sd(sd) || sd->hd == NULL )
  10348. return SCRIPT_CMD_SUCCESS;
  10349. if(script_hasdata(st,2))
  10350. homun_id = script_getnum(st,2);
  10351. else
  10352. homun_id = 6048 + (rnd() % 4);
  10353. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10354. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10355. int m_id = hom_class2mapid(homun_id);
  10356. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10357. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10358. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10359. hom_call(sd); // Respawn homunculus.
  10360. hom_mutate(sd->hd, homun_id);
  10361. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10362. script_pushint(st, 1);
  10363. return SCRIPT_CMD_SUCCESS;
  10364. } else
  10365. clif_emotion(&sd->bl, ET_SWEAT);
  10366. } else
  10367. clif_emotion(&sd->bl, ET_SWEAT);
  10368. script_pushint(st, 0);
  10369. return SCRIPT_CMD_SUCCESS;
  10370. }
  10371. /*==========================================
  10372. * Puts homunculus into morph state
  10373. * and gives ITEMID_STRANGE_EMBRYO.
  10374. *------------------------------------------*/
  10375. BUILDIN_FUNC(morphembryo)
  10376. {
  10377. struct item item_tmp;
  10378. TBL_PC *sd;
  10379. if( !script_rid2sd(sd) || sd->hd == NULL )
  10380. return SCRIPT_CMD_SUCCESS;
  10381. if( hom_is_active(sd->hd) ) {
  10382. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10383. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10384. char i;
  10385. memset(&item_tmp, 0, sizeof(item_tmp));
  10386. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10387. item_tmp.identify = 1;
  10388. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10389. clif_additem(sd, 0, 0, i);
  10390. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10391. } else {
  10392. hom_vaporize(sd, HOM_ST_MORPH);
  10393. script_pushint(st, 1);
  10394. return SCRIPT_CMD_SUCCESS;
  10395. }
  10396. } else
  10397. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10398. } else
  10399. clif_emotion(&sd->bl, ET_SWEAT);
  10400. script_pushint(st, 0);
  10401. return SCRIPT_CMD_SUCCESS;
  10402. }
  10403. // [Zephyrus]
  10404. BUILDIN_FUNC(homunculus_shuffle)
  10405. {
  10406. TBL_PC *sd;
  10407. if( !script_rid2sd(sd) )
  10408. return SCRIPT_CMD_SUCCESS;
  10409. if(hom_is_active(sd->hd))
  10410. hom_shuffle(sd->hd);
  10411. return SCRIPT_CMD_SUCCESS;
  10412. }
  10413. /*==========================================
  10414. * Check for homunculus state.
  10415. * Return: -1 = No homunculus
  10416. * 0 = Homunculus is active
  10417. * 1 = Homunculus is vaporized (rest)
  10418. * 2 = Homunculus is in morph state
  10419. *------------------------------------------*/
  10420. BUILDIN_FUNC(checkhomcall)
  10421. {
  10422. TBL_PC *sd;
  10423. TBL_HOM *hd;
  10424. if( !script_rid2sd(sd) )
  10425. return SCRIPT_CMD_SUCCESS;
  10426. hd = sd->hd;
  10427. if( !hd )
  10428. script_pushint(st, -1);
  10429. else
  10430. script_pushint(st, hd->homunculus.vaporize);
  10431. return SCRIPT_CMD_SUCCESS;
  10432. }
  10433. //These two functions bring the eA MAPID_* class functionality to scripts.
  10434. BUILDIN_FUNC(eaclass)
  10435. {
  10436. int class_;
  10437. if( script_hasdata(st,2) )
  10438. class_ = script_getnum(st,2);
  10439. else {
  10440. TBL_PC *sd;
  10441. if (!script_charid2sd(3,sd)) {
  10442. script_pushint(st,-1);
  10443. return SCRIPT_CMD_SUCCESS;
  10444. }
  10445. class_ = sd->status.class_;
  10446. }
  10447. script_pushint(st,pc_jobid2mapid(class_));
  10448. return SCRIPT_CMD_SUCCESS;
  10449. }
  10450. BUILDIN_FUNC(roclass)
  10451. {
  10452. int class_ =script_getnum(st,2);
  10453. int sex;
  10454. if( script_hasdata(st,3) )
  10455. sex = script_getnum(st,3);
  10456. else {
  10457. TBL_PC *sd;
  10458. if (st->rid && script_rid2sd(sd))
  10459. sex = sd->status.sex;
  10460. else
  10461. sex = SEX_MALE; //Just use male when not found.
  10462. }
  10463. script_pushint(st,pc_mapid2jobid(class_, sex));
  10464. return SCRIPT_CMD_SUCCESS;
  10465. }
  10466. /*==========================================
  10467. * Tells client to open a hatching window, used for pet incubator
  10468. *------------------------------------------*/
  10469. BUILDIN_FUNC(birthpet)
  10470. {
  10471. TBL_PC *sd;
  10472. if( !script_rid2sd(sd) )
  10473. return SCRIPT_CMD_SUCCESS;
  10474. if( sd->status.pet_id )
  10475. {// do not send egg list, when you already have a pet
  10476. return SCRIPT_CMD_SUCCESS;
  10477. }
  10478. clif_sendegg(sd);
  10479. return SCRIPT_CMD_SUCCESS;
  10480. }
  10481. /**
  10482. * resetlvl <action type>{,<char_id>};
  10483. * @param action_type:
  10484. * 1 : make like after rebirth
  10485. * 2 : blvl,jlvl=1, skillpoint=0
  10486. * 3 : don't reset skill, blvl=1
  10487. * 4 : jlvl=0
  10488. * @author AppleGirl
  10489. **/
  10490. BUILDIN_FUNC(resetlvl)
  10491. {
  10492. TBL_PC *sd;
  10493. int type=script_getnum(st,2);
  10494. if (!script_charid2sd(3,sd))
  10495. return SCRIPT_CMD_FAILURE;
  10496. pc_resetlvl(sd,type);
  10497. return SCRIPT_CMD_SUCCESS;
  10498. }
  10499. /**
  10500. * Reset a player status point
  10501. * resetstatus({<char_id>});
  10502. **/
  10503. BUILDIN_FUNC(resetstatus)
  10504. {
  10505. TBL_PC *sd;
  10506. if (!script_charid2sd(2,sd))
  10507. return SCRIPT_CMD_FAILURE;
  10508. pc_resetstate(sd);
  10509. return SCRIPT_CMD_SUCCESS;
  10510. }
  10511. /**
  10512. * Reset player's skill
  10513. * resetskill({<char_id>});
  10514. **/
  10515. BUILDIN_FUNC(resetskill)
  10516. {
  10517. TBL_PC *sd;
  10518. if (!script_charid2sd(2,sd))
  10519. return SCRIPT_CMD_FAILURE;
  10520. pc_resetskill(sd,1);
  10521. return SCRIPT_CMD_SUCCESS;
  10522. }
  10523. /**
  10524. * Reset SG designated maps
  10525. * resetfeel({<char_id>});
  10526. **/
  10527. BUILDIN_FUNC(resetfeel)
  10528. {
  10529. TBL_PC *sd;
  10530. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10531. return SCRIPT_CMD_FAILURE;
  10532. pc_resetfeel(sd);
  10533. return SCRIPT_CMD_SUCCESS;
  10534. }
  10535. /**
  10536. * Reset SG designated monsters
  10537. * resethate({<char_id>});
  10538. **/
  10539. BUILDIN_FUNC(resethate)
  10540. {
  10541. TBL_PC *sd;
  10542. if (!script_charid2sd(2,sd) || (sd->class_&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR)
  10543. return SCRIPT_CMD_FAILURE;
  10544. pc_resethate(sd);
  10545. return SCRIPT_CMD_SUCCESS;
  10546. }
  10547. /**
  10548. * Counts total amount of skill points.
  10549. * skillpointcount({<char_id>})
  10550. **/
  10551. BUILDIN_FUNC(skillpointcount)
  10552. {
  10553. TBL_PC *sd;
  10554. if (!script_charid2sd(2,sd))
  10555. return SCRIPT_CMD_FAILURE;
  10556. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10557. return SCRIPT_CMD_SUCCESS;
  10558. }
  10559. /*==========================================
  10560. *
  10561. *------------------------------------------*/
  10562. BUILDIN_FUNC(changebase)
  10563. {
  10564. TBL_PC *sd=NULL;
  10565. int vclass;
  10566. if( !script_mapid2sd(3,sd) )
  10567. return SCRIPT_CMD_SUCCESS;
  10568. vclass = script_getnum(st,2);
  10569. if(vclass == JOB_WEDDING)
  10570. {
  10571. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10572. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10573. )
  10574. return SCRIPT_CMD_SUCCESS;
  10575. }
  10576. if(!sd->disguise && vclass != sd->vd.class_) {
  10577. status_set_viewdata(&sd->bl, vclass);
  10578. //Updated client view. Base, Weapon and Cloth Colors.
  10579. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10580. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10581. if (sd->vd.cloth_color)
  10582. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10583. if (sd->vd.body_style)
  10584. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10585. clif_skillinfoblock(sd);
  10586. }
  10587. return SCRIPT_CMD_SUCCESS;
  10588. }
  10589. /**
  10590. * Change account sex and unequip all item and request for a changesex to char-serv
  10591. * changesex({<char_id>});
  10592. */
  10593. BUILDIN_FUNC(changesex)
  10594. {
  10595. int i;
  10596. TBL_PC *sd = NULL;
  10597. if (!script_charid2sd(2,sd))
  10598. return SCRIPT_CMD_FAILURE;
  10599. pc_resetskill(sd,4);
  10600. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10601. for(i = 0; i < EQI_MAX; i++) {
  10602. if (sd->equip_index[i] >= 0)
  10603. pc_unequipitem(sd, sd->equip_index[i], 3);
  10604. }
  10605. chrif_changesex(sd, true);
  10606. return SCRIPT_CMD_SUCCESS;
  10607. }
  10608. /**
  10609. * Change character's sex and unequip all item and request for a changesex to char-serv
  10610. * changecharsex({<char_id>});
  10611. */
  10612. BUILDIN_FUNC(changecharsex)
  10613. {
  10614. #if PACKETVER >= 20141016
  10615. int i;
  10616. TBL_PC *sd = NULL;
  10617. if (!script_charid2sd(2,sd))
  10618. return SCRIPT_CMD_FAILURE;
  10619. pc_resetskill(sd,4);
  10620. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10621. for (i = 0; i < EQI_MAX; i++) {
  10622. if (sd->equip_index[i] >= 0)
  10623. pc_unequipitem(sd, sd->equip_index[i], 3);
  10624. }
  10625. chrif_changesex(sd, false);
  10626. return SCRIPT_CMD_SUCCESS;
  10627. #else
  10628. return SCRIPT_CMD_FAILURE;
  10629. #endif
  10630. }
  10631. /*==========================================
  10632. * Works like 'announce' but outputs in the common chat window
  10633. *------------------------------------------*/
  10634. BUILDIN_FUNC(globalmes)
  10635. {
  10636. struct block_list *bl = map_id2bl(st->oid);
  10637. struct npc_data *nd = (struct npc_data *)bl;
  10638. const char *name=NULL,*mes;
  10639. mes=script_getstr(st,2);
  10640. if(mes==NULL)
  10641. return SCRIPT_CMD_SUCCESS;
  10642. if(script_hasdata(st,3)){ // npc name to display
  10643. name=script_getstr(st,3);
  10644. } else {
  10645. name=nd->name; //use current npc name
  10646. }
  10647. npc_globalmessage(name,mes); // broadcast to all players connected
  10648. return SCRIPT_CMD_SUCCESS;
  10649. }
  10650. /////////////////////////////////////////////////////////////////////
  10651. // NPC waiting room (chat room)
  10652. //
  10653. /// Creates a waiting room (chat room) for this npc.
  10654. ///
  10655. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10656. BUILDIN_FUNC(waitingroom)
  10657. {
  10658. struct npc_data* nd;
  10659. const char* title = script_getstr(st, 2);
  10660. int limit = script_getnum(st, 3);
  10661. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10662. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10663. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10664. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10665. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10666. nd = (struct npc_data *)map_id2bl(st->oid);
  10667. if( nd != NULL )
  10668. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10669. return SCRIPT_CMD_SUCCESS;
  10670. }
  10671. /// Removes the waiting room of the current or target npc.
  10672. ///
  10673. /// delwaitingroom "<npc_name>";
  10674. /// delwaitingroom;
  10675. BUILDIN_FUNC(delwaitingroom)
  10676. {
  10677. struct npc_data* nd;
  10678. if( script_hasdata(st,2) )
  10679. nd = npc_name2id(script_getstr(st, 2));
  10680. else
  10681. nd = (struct npc_data *)map_id2bl(st->oid);
  10682. if( nd != NULL )
  10683. chat_deletenpcchat(nd);
  10684. return SCRIPT_CMD_SUCCESS;
  10685. }
  10686. /// Kick the specified player from the waiting room of the target npc.
  10687. ///
  10688. /// waitingroomkick "<npc_name>", <kickusername>;
  10689. BUILDIN_FUNC(waitingroomkick)
  10690. {
  10691. struct npc_data* nd;
  10692. struct chat_data* cd;
  10693. const char* kickusername;
  10694. nd = npc_name2id(script_getstr(st,2));
  10695. kickusername = script_getstr(st,3);
  10696. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10697. chat_npckickchat(cd, kickusername);
  10698. return SCRIPT_CMD_SUCCESS;
  10699. }
  10700. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10701. /// Num users stored in .@waitingroom_usercount
  10702. ///
  10703. /// getwaitingroomusers "<npc_name>";
  10704. BUILDIN_FUNC(getwaitingroomusers)
  10705. {
  10706. struct npc_data* nd;
  10707. struct chat_data* cd;
  10708. int i, j=0;
  10709. if( script_hasdata(st,2) )
  10710. nd = npc_name2id(script_getstr(st, 2));
  10711. else
  10712. nd = (struct npc_data *)map_id2bl(st->oid);
  10713. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10714. for(i = 0; i < cd->users; ++i) {
  10715. setd_sub_num( st, NULL, ".@waitingroom_users", j, cd->usersd[i]->status.account_id, NULL );
  10716. j++;
  10717. }
  10718. setd_sub_num( st, NULL, ".@waitingroom_usercount", 0, j, NULL );
  10719. }
  10720. return SCRIPT_CMD_SUCCESS;
  10721. }
  10722. /// Kicks all the players from the waiting room of the current or target npc.
  10723. ///
  10724. /// kickwaitingroomall "<npc_name>";
  10725. /// kickwaitingroomall;
  10726. BUILDIN_FUNC(waitingroomkickall)
  10727. {
  10728. struct npc_data* nd;
  10729. struct chat_data* cd;
  10730. if( script_hasdata(st,2) )
  10731. nd = npc_name2id(script_getstr(st,2));
  10732. else
  10733. nd = (struct npc_data *)map_id2bl(st->oid);
  10734. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10735. chat_npckickall(cd);
  10736. return SCRIPT_CMD_SUCCESS;
  10737. }
  10738. /// Enables the waiting room event of the current or target npc.
  10739. ///
  10740. /// enablewaitingroomevent "<npc_name>";
  10741. /// enablewaitingroomevent;
  10742. BUILDIN_FUNC(enablewaitingroomevent)
  10743. {
  10744. struct npc_data* nd;
  10745. struct chat_data* cd;
  10746. if( script_hasdata(st,2) )
  10747. nd = npc_name2id(script_getstr(st, 2));
  10748. else
  10749. nd = (struct npc_data *)map_id2bl(st->oid);
  10750. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10751. chat_enableevent(cd);
  10752. return SCRIPT_CMD_SUCCESS;
  10753. }
  10754. /// Disables the waiting room event of the current or target npc.
  10755. ///
  10756. /// disablewaitingroomevent "<npc_name>";
  10757. /// disablewaitingroomevent;
  10758. BUILDIN_FUNC(disablewaitingroomevent)
  10759. {
  10760. struct npc_data *nd;
  10761. struct chat_data *cd;
  10762. if( script_hasdata(st,2) )
  10763. nd = npc_name2id(script_getstr(st, 2));
  10764. else
  10765. nd = (struct npc_data *)map_id2bl(st->oid);
  10766. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10767. chat_disableevent(cd);
  10768. return SCRIPT_CMD_SUCCESS;
  10769. }
  10770. /// Returns info on the waiting room of the current or target npc.
  10771. /// Returns -1 if the type unknown
  10772. /// <type>=0 : current number of users
  10773. /// <type>=1 : maximum number of users allowed
  10774. /// <type>=2 : the number of users that trigger the event
  10775. /// <type>=3 : if the trigger is disabled
  10776. /// <type>=4 : the title of the waiting room
  10777. /// <type>=5 : the password of the waiting room
  10778. /// <type>=16 : the name of the waiting room event
  10779. /// <type>=32 : if the waiting room is full
  10780. /// <type>=33 : if there are enough users to trigger the event
  10781. ///
  10782. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10783. /// getwaitingroomstate(<type>) -> <info>
  10784. BUILDIN_FUNC(getwaitingroomstate)
  10785. {
  10786. struct npc_data *nd;
  10787. struct chat_data *cd;
  10788. int type;
  10789. type = script_getnum(st,2);
  10790. if( script_hasdata(st,3) )
  10791. nd = npc_name2id(script_getstr(st, 3));
  10792. else
  10793. nd = (struct npc_data *)map_id2bl(st->oid);
  10794. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10795. {
  10796. script_pushint(st, -1);
  10797. return SCRIPT_CMD_SUCCESS;
  10798. }
  10799. switch(type)
  10800. {
  10801. case 0: script_pushint(st, cd->users); break;
  10802. case 1: script_pushint(st, cd->limit); break;
  10803. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10804. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10805. case 4: script_pushstrcopy(st, cd->title); break;
  10806. case 5: script_pushstrcopy(st, cd->pass); break;
  10807. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10808. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10809. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10810. default: script_pushint(st, -1); break;
  10811. }
  10812. return SCRIPT_CMD_SUCCESS;
  10813. }
  10814. /// Warps the trigger or target amount of players to the target map and position.
  10815. /// Players are automatically removed from the waiting room.
  10816. /// Those waiting the longest will get warped first.
  10817. /// The target map can be "Random" for a random position in the current map,
  10818. /// and "SavePoint" for the savepoint map+position.
  10819. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10820. ///
  10821. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10822. /// The total number of teleported players is put into $@warpwaitingpcnum
  10823. ///
  10824. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10825. /// warpwaitingpc "<map name>",<x>,<y>;
  10826. BUILDIN_FUNC(warpwaitingpc)
  10827. {
  10828. int x;
  10829. int y;
  10830. int i;
  10831. int n;
  10832. const char* map_name;
  10833. struct npc_data* nd;
  10834. struct chat_data* cd;
  10835. nd = (struct npc_data *)map_id2bl(st->oid);
  10836. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10837. return SCRIPT_CMD_SUCCESS;
  10838. map_name = script_getstr(st,2);
  10839. x = script_getnum(st,3);
  10840. y = script_getnum(st,4);
  10841. n = cd->trigger&0x7f;
  10842. if( script_hasdata(st,5) )
  10843. n = script_getnum(st,5);
  10844. for( i = 0; i < n && cd->users > 0; i++ )
  10845. {
  10846. TBL_PC* sd = cd->usersd[0];
  10847. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  10848. {// can't teleport on this map
  10849. break;
  10850. }
  10851. if( cd->zeny )
  10852. {// fee set
  10853. if( (uint32)sd->status.zeny < cd->zeny )
  10854. {// no zeny to cover set fee
  10855. break;
  10856. }
  10857. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10858. }
  10859. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10860. if( strcmp(map_name,"Random") == 0 )
  10861. pc_randomwarp(sd,CLR_TELEPORT);
  10862. else if( strcmp(map_name,"SavePoint") == 0 )
  10863. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10864. else
  10865. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10866. }
  10867. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10868. return SCRIPT_CMD_SUCCESS;
  10869. }
  10870. /////////////////////////////////////////////////////////////////////
  10871. // ...
  10872. //
  10873. /// Detaches a character from a script.
  10874. ///
  10875. /// @param st Script state to detach the character from.
  10876. void script_detach_rid(struct script_state* st)
  10877. {
  10878. if(st->rid)
  10879. {
  10880. script_detach_state(st, false);
  10881. st->rid = 0;
  10882. }
  10883. }
  10884. /*=========================================================================
  10885. * Attaches a set of RIDs to the current script. [digitalhamster]
  10886. * addrid(<type>{,<flag>{,<parameters>}});
  10887. * <type>:
  10888. * 0 : All players in the server.
  10889. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10890. * 2 : Party members of a specified party ID.
  10891. * [ Parameters: <party id> ]
  10892. * 3 : Guild members of a specified guild ID.
  10893. * [ Parameters: <guild id> ]
  10894. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10895. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10896. * 5 : All players in the map.
  10897. * [ Parameters: "<map name>" ]
  10898. * Account ID: The specified account ID.
  10899. * <flag>:
  10900. * 0 : Players are always attached. (default)
  10901. * 1 : Players currently running another script will not be attached.
  10902. *-------------------------------------------------------------------------*/
  10903. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10904. {
  10905. int forceflag;
  10906. struct map_session_data *sd = (TBL_PC *)bl;
  10907. struct script_state* st;
  10908. st = va_arg(ap,struct script_state*);
  10909. forceflag = va_arg(ap,int);
  10910. if(!forceflag || !sd->st)
  10911. if(sd->status.account_id != st->rid)
  10912. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10913. return 0;
  10914. }
  10915. BUILDIN_FUNC(addrid)
  10916. {
  10917. struct s_mapiterator* iter;
  10918. struct block_list *bl;
  10919. TBL_PC *sd;
  10920. if(st->rid < 1) {
  10921. st->state = END;
  10922. bl = map_id2bl(st->oid);
  10923. } else
  10924. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10925. iter = mapit_getallusers();
  10926. switch(script_getnum(st,2)) {
  10927. case 0:
  10928. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10929. if(!script_getnum(st,3) || !sd->st)
  10930. if(sd->status.account_id != st->rid) //attached player already runs.
  10931. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10932. }
  10933. break;
  10934. case 1:
  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->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10938. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10939. }
  10940. break;
  10941. case 2:
  10942. if(script_getnum(st,4) == 0) {
  10943. script_pushint(st,0);
  10944. mapit_free(iter);
  10945. return SCRIPT_CMD_SUCCESS;
  10946. }
  10947. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10948. if(!script_getnum(st,3) || !sd->st)
  10949. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10950. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10951. }
  10952. break;
  10953. case 3:
  10954. if(script_getnum(st,4) == 0) {
  10955. script_pushint(st,0);
  10956. mapit_free(iter);
  10957. return SCRIPT_CMD_SUCCESS;
  10958. }
  10959. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10960. if(!script_getnum(st,3) || !sd->st)
  10961. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10962. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10963. }
  10964. break;
  10965. case 4:
  10966. map_foreachinallarea(buildin_addrid_sub,
  10967. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10968. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10969. break;
  10970. case 5:
  10971. if (script_getstr(st, 4) == NULL) {
  10972. script_pushint(st, 0);
  10973. mapit_free(iter);
  10974. return SCRIPT_CMD_FAILURE;
  10975. }
  10976. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10977. script_pushint(st, 0);
  10978. mapit_free(iter);
  10979. return SCRIPT_CMD_FAILURE;
  10980. }
  10981. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10982. break;
  10983. default:
  10984. mapit_free(iter);
  10985. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10986. script_pushint(st,0);
  10987. return SCRIPT_CMD_SUCCESS;
  10988. }
  10989. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10990. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10991. script_pushint(st,1);
  10992. }
  10993. return SCRIPT_CMD_SUCCESS;
  10994. }
  10995. mapit_free(iter);
  10996. script_pushint(st,1);
  10997. return SCRIPT_CMD_SUCCESS;
  10998. }
  10999. /*==========================================
  11000. * Attach sd char id to script and detach current one if any
  11001. *------------------------------------------*/
  11002. BUILDIN_FUNC(attachrid)
  11003. {
  11004. int rid = script_getnum(st,2);
  11005. bool force;
  11006. if( script_hasdata(st,3) ){
  11007. force = script_getnum(st,3) != 0;
  11008. }else{
  11009. force = true;
  11010. }
  11011. struct map_session_data* sd = map_id2sd(rid);
  11012. if( sd != NULL && ( !sd->npc_id || force ) ){
  11013. script_detach_rid(st);
  11014. st->rid = rid;
  11015. script_attach_state(st);
  11016. script_pushint(st,true);
  11017. }else{
  11018. script_pushint(st,false);
  11019. }
  11020. return SCRIPT_CMD_SUCCESS;
  11021. }
  11022. /*==========================================
  11023. * Detach script to rid
  11024. *------------------------------------------*/
  11025. BUILDIN_FUNC(detachrid)
  11026. {
  11027. script_detach_rid(st);
  11028. return SCRIPT_CMD_SUCCESS;
  11029. }
  11030. /*==========================================
  11031. * Chk if account connected, (and charid from account if specified)
  11032. *------------------------------------------*/
  11033. BUILDIN_FUNC(isloggedin)
  11034. {
  11035. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  11036. if (script_hasdata(st,3) && sd &&
  11037. sd->status.char_id != script_getnum(st,3))
  11038. sd = NULL;
  11039. push_val(st->stack,C_INT,sd!=NULL);
  11040. return SCRIPT_CMD_SUCCESS;
  11041. }
  11042. /*==========================================
  11043. *
  11044. *------------------------------------------*/
  11045. BUILDIN_FUNC(setmapflagnosave)
  11046. {
  11047. int16 m,x,y;
  11048. unsigned short mapindex;
  11049. const char *str,*str2;
  11050. union u_mapflag_args args = {};
  11051. str=script_getstr(st,2);
  11052. str2=script_getstr(st,3);
  11053. x=script_getnum(st,4);
  11054. y=script_getnum(st,5);
  11055. m = map_mapname2mapid(str);
  11056. mapindex = mapindex_name2id(str2);
  11057. if(m < 0) {
  11058. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  11059. return SCRIPT_CMD_FAILURE;
  11060. }
  11061. args.nosave.map = mapindex;
  11062. args.nosave.x = x;
  11063. args.nosave.y = y;
  11064. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  11065. return SCRIPT_CMD_SUCCESS;
  11066. }
  11067. BUILDIN_FUNC(getmapflag)
  11068. {
  11069. int16 m;
  11070. int mf;
  11071. const char *str;
  11072. str=script_getstr(st,2);
  11073. m = map_mapname2mapid(str);
  11074. if (m < 0) {
  11075. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  11076. return SCRIPT_CMD_FAILURE;
  11077. }
  11078. mf = script_getnum(st, 3);
  11079. if( mf < MF_MIN || mf > MF_MAX ){
  11080. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  11081. return SCRIPT_CMD_FAILURE;
  11082. }
  11083. union u_mapflag_args args = {};
  11084. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  11085. args.flag_val = SKILLDMG_MAX;
  11086. else
  11087. FETCH(4, args.flag_val);
  11088. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  11089. return SCRIPT_CMD_SUCCESS;
  11090. }
  11091. BUILDIN_FUNC(setmapflag)
  11092. {
  11093. int16 m;
  11094. int mf;
  11095. const char *str;
  11096. str = script_getstr(st,2);
  11097. m = map_mapname2mapid(str);
  11098. if (m < 0) {
  11099. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  11100. return SCRIPT_CMD_FAILURE;
  11101. }
  11102. mf = script_getnum(st, 3);
  11103. if( mf < MF_MIN || mf > MF_MAX ){
  11104. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  11105. return SCRIPT_CMD_FAILURE;
  11106. }
  11107. union u_mapflag_args args = {};
  11108. switch(mf) {
  11109. case MF_SKILL_DAMAGE:
  11110. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  11111. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  11112. else {
  11113. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  11114. return SCRIPT_CMD_FAILURE;
  11115. }
  11116. break;
  11117. case MF_SKILL_DURATION:
  11118. if (script_hasdata(st, 4) && script_hasdata(st, 5)) {
  11119. args.skill_duration.skill_id = script_getnum(st, 4);
  11120. if (!skill_get_index(args.skill_duration.skill_id)) {
  11121. ShowError("buildin_setmapflag: Invalid skill ID %d for skill_duration mapflag.\n", args.skill_duration.skill_id);
  11122. return SCRIPT_CMD_FAILURE;
  11123. }
  11124. args.skill_duration.per = script_getnum(st, 5);
  11125. } else {
  11126. ShowWarning("buildin_setmapflag: Unable to set skill_duration mapflag as flag data is missing.\n");
  11127. return SCRIPT_CMD_FAILURE;
  11128. }
  11129. break;
  11130. case MF_NOSAVE: // Assume setting "SavePoint"
  11131. args.nosave.map = 0;
  11132. args.nosave.x = -1;
  11133. args.nosave.y = -1;
  11134. break;
  11135. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  11136. args.nightmaredrop.drop_id = -1;
  11137. args.nightmaredrop.drop_per = 300;
  11138. args.nightmaredrop.drop_type = NMDT_EQUIP;
  11139. break;
  11140. default:
  11141. FETCH(4, args.flag_val);
  11142. break;
  11143. }
  11144. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  11145. return SCRIPT_CMD_SUCCESS;
  11146. }
  11147. BUILDIN_FUNC(removemapflag)
  11148. {
  11149. int16 m;
  11150. int mf;
  11151. const char *str;
  11152. union u_mapflag_args args = {};
  11153. str = script_getstr(st, 2);
  11154. m = map_mapname2mapid(str);
  11155. if (m < 0) {
  11156. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  11157. return SCRIPT_CMD_FAILURE;
  11158. }
  11159. mf = script_getnum(st, 3);
  11160. if( mf < MF_MIN || mf > MF_MAX ){
  11161. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  11162. return SCRIPT_CMD_FAILURE;
  11163. }
  11164. FETCH(4, args.flag_val);
  11165. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  11166. return SCRIPT_CMD_SUCCESS;
  11167. }
  11168. BUILDIN_FUNC(pvpon)
  11169. {
  11170. int16 m;
  11171. const char *str = script_getstr(st,2);
  11172. m = map_mapname2mapid(str);
  11173. if (m < 0) {
  11174. ShowWarning("buildin_pvpon: Unknown map '%s'.\n", str);
  11175. return SCRIPT_CMD_FAILURE;
  11176. }
  11177. if (!map_getmapflag(m, MF_PVP))
  11178. map_setmapflag(m, MF_PVP, true);
  11179. return SCRIPT_CMD_SUCCESS;
  11180. }
  11181. BUILDIN_FUNC(pvpoff)
  11182. {
  11183. int16 m;
  11184. const char *str = script_getstr(st,2);
  11185. m = map_mapname2mapid(str);
  11186. if (m < 0) {
  11187. ShowWarning("buildin_pvpoff: Unknown map '%s'.\n", str);
  11188. return SCRIPT_CMD_FAILURE;
  11189. }
  11190. if (map_getmapflag(m, MF_PVP))
  11191. map_setmapflag(m, MF_PVP, false);
  11192. return SCRIPT_CMD_SUCCESS;
  11193. }
  11194. BUILDIN_FUNC(gvgon)
  11195. {
  11196. int16 m;
  11197. const char *str = script_getstr(st,2);
  11198. m = map_mapname2mapid(str);
  11199. if (m < 0) {
  11200. ShowWarning("buildin_gvgon: Unknown map '%s'.\n", str);
  11201. return SCRIPT_CMD_FAILURE;
  11202. }
  11203. if (!map_getmapflag(m, MF_GVG))
  11204. map_setmapflag(m, MF_GVG, true);
  11205. return SCRIPT_CMD_SUCCESS;
  11206. }
  11207. BUILDIN_FUNC(gvgoff)
  11208. {
  11209. int16 m;
  11210. const char *str = script_getstr(st,2);
  11211. m = map_mapname2mapid(str);
  11212. if (m < 0) {
  11213. ShowWarning("buildin_gvgoff: Unknown map '%s'.\n", str);
  11214. return SCRIPT_CMD_FAILURE;
  11215. }
  11216. if (map_getmapflag(m, MF_GVG))
  11217. map_setmapflag(m, MF_GVG, false);
  11218. return SCRIPT_CMD_SUCCESS;
  11219. }
  11220. BUILDIN_FUNC(gvgon3)
  11221. {
  11222. int16 m;
  11223. const char *str = script_getstr(st,2);
  11224. m = map_mapname2mapid(str);
  11225. if (m < 0) {
  11226. ShowWarning("buildin_gvgon3: Unknown map '%s'.\n", str);
  11227. return SCRIPT_CMD_FAILURE;
  11228. }
  11229. if (!map_getmapflag(m, MF_GVG_TE))
  11230. map_setmapflag(m, MF_GVG_TE, true);
  11231. return SCRIPT_CMD_SUCCESS;
  11232. }
  11233. BUILDIN_FUNC(gvgoff3)
  11234. {
  11235. int16 m;
  11236. const char *str = script_getstr(st,2);
  11237. m = map_mapname2mapid(str);
  11238. if (m < 0) {
  11239. ShowWarning("buildin_gvgoff3: Unknown map '%s'.\n", str);
  11240. return SCRIPT_CMD_FAILURE;
  11241. }
  11242. if (map_getmapflag(m, MF_GVG_TE))
  11243. map_setmapflag(m, MF_GVG_TE, false);
  11244. return SCRIPT_CMD_SUCCESS;
  11245. }
  11246. /**
  11247. * Shows an emotion on top of a NPC by default or the given GID
  11248. * emotion <emotion ID>{,<target ID>};
  11249. */
  11250. BUILDIN_FUNC(emotion)
  11251. {
  11252. struct block_list *bl = NULL;
  11253. int type = script_getnum(st,2);
  11254. if (type < ET_SURPRISE || type >= ET_MAX) {
  11255. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11256. return SCRIPT_CMD_FAILURE;
  11257. }
  11258. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11259. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11260. return SCRIPT_CMD_FAILURE;
  11261. }
  11262. if (!bl)
  11263. bl = map_id2bl(st->oid);
  11264. clif_emotion(bl, type);
  11265. return SCRIPT_CMD_SUCCESS;
  11266. }
  11267. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11268. {
  11269. int16 m=va_arg(ap,int);
  11270. int g_id=va_arg(ap,int);
  11271. int flag=va_arg(ap,int);
  11272. if(!sd || sd->bl.m != m)
  11273. return 0;
  11274. if(
  11275. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11276. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11277. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11278. )
  11279. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11280. return 1;
  11281. }
  11282. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11283. {
  11284. struct mob_data *md=(struct mob_data *)bl;
  11285. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11286. status_kill(bl);
  11287. return 1;
  11288. }
  11289. /*
  11290. * Function to kick guild members out of a map and to their save points.
  11291. * m : mapid
  11292. * g_id : owner guild id
  11293. * flag & 1 : Warp out owners
  11294. * flag & 2 : Warp out outsiders
  11295. * flag & 4 : reserved for mobs
  11296. * */
  11297. BUILDIN_FUNC(maprespawnguildid)
  11298. {
  11299. const char *mapname=script_getstr(st,2);
  11300. int g_id=script_getnum(st,3);
  11301. int flag=script_getnum(st,4);
  11302. int16 m=map_mapname2mapid(mapname);
  11303. if(m == -1)
  11304. return SCRIPT_CMD_SUCCESS;
  11305. //Catch ALL players (in case some are 'between maps' on execution time)
  11306. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11307. if (flag&4) //Remove script mobs.
  11308. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11309. return SCRIPT_CMD_SUCCESS;
  11310. }
  11311. /// Siege commands
  11312. /**
  11313. * Start WoE:FE
  11314. * agitstart();
  11315. */
  11316. BUILDIN_FUNC(agitstart)
  11317. {
  11318. guild_agit_start();
  11319. return SCRIPT_CMD_SUCCESS;
  11320. }
  11321. /**
  11322. * End WoE:FE
  11323. * agitend();
  11324. */
  11325. BUILDIN_FUNC(agitend)
  11326. {
  11327. guild_agit_end();
  11328. return SCRIPT_CMD_SUCCESS;
  11329. }
  11330. /**
  11331. * Start WoE:SE
  11332. * agitstart2();
  11333. */
  11334. BUILDIN_FUNC(agitstart2)
  11335. {
  11336. guild_agit2_start();
  11337. return SCRIPT_CMD_SUCCESS;
  11338. }
  11339. /**
  11340. * End WoE:SE
  11341. * agitend();
  11342. */
  11343. BUILDIN_FUNC(agitend2)
  11344. {
  11345. guild_agit2_end();
  11346. return SCRIPT_CMD_SUCCESS;
  11347. }
  11348. /**
  11349. * Start WoE:TE
  11350. * agitstart3();
  11351. */
  11352. BUILDIN_FUNC(agitstart3)
  11353. {
  11354. guild_agit3_start();
  11355. return SCRIPT_CMD_SUCCESS;
  11356. }
  11357. /**
  11358. * End WoE:TE
  11359. * agitend3();
  11360. */
  11361. BUILDIN_FUNC(agitend3)
  11362. {
  11363. guild_agit3_end();
  11364. return SCRIPT_CMD_SUCCESS;
  11365. }
  11366. /**
  11367. * Returns whether WoE:FE is on or off.
  11368. * agitcheck();
  11369. */
  11370. BUILDIN_FUNC(agitcheck)
  11371. {
  11372. script_pushint(st, agit_flag);
  11373. return SCRIPT_CMD_SUCCESS;
  11374. }
  11375. /**
  11376. * Returns whether WoE:SE is on or off.
  11377. * agitcheck2();
  11378. */
  11379. BUILDIN_FUNC(agitcheck2)
  11380. {
  11381. script_pushint(st, agit2_flag);
  11382. return SCRIPT_CMD_SUCCESS;
  11383. }
  11384. /**
  11385. * Returns whether WoE:TE is on or off.
  11386. * agitcheck3();
  11387. */
  11388. BUILDIN_FUNC(agitcheck3)
  11389. {
  11390. script_pushint(st, agit3_flag);
  11391. return SCRIPT_CMD_SUCCESS;
  11392. }
  11393. /// Sets the guild_id of this npc.
  11394. ///
  11395. /// flagemblem <guild_id>;
  11396. BUILDIN_FUNC(flagemblem)
  11397. {
  11398. TBL_NPC* nd;
  11399. int g_id = script_getnum(st,2);
  11400. if(g_id < 0)
  11401. return SCRIPT_CMD_SUCCESS;
  11402. nd = (TBL_NPC*)map_id2nd(st->oid);
  11403. if( nd == NULL ) {
  11404. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11405. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11406. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11407. } else {
  11408. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11409. nd->u.scr.guild_id = g_id;
  11410. clif_guild_emblem_area(&nd->bl);
  11411. /* guild flag caching */
  11412. if( g_id ) /* adding a id */
  11413. guild_flag_add(nd);
  11414. else if( changed ) /* removing a flag */
  11415. guild_flag_remove(nd);
  11416. }
  11417. return SCRIPT_CMD_SUCCESS;
  11418. }
  11419. BUILDIN_FUNC(getcastlename)
  11420. {
  11421. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11422. struct guild_castle* gc = guild_mapname2gc(mapname);
  11423. const char* name = (gc) ? gc->castle_name : "";
  11424. script_pushstrcopy(st,name);
  11425. return SCRIPT_CMD_SUCCESS;
  11426. }
  11427. BUILDIN_FUNC(getcastledata)
  11428. {
  11429. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11430. int index = script_getnum(st,3);
  11431. struct guild_castle *gc = guild_mapname2gc(mapname);
  11432. if (gc == NULL) {
  11433. script_pushint(st,0);
  11434. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11435. return SCRIPT_CMD_FAILURE;
  11436. }
  11437. switch (index) {
  11438. case CD_GUILD_ID:
  11439. script_pushint(st,gc->guild_id); break;
  11440. case CD_CURRENT_ECONOMY:
  11441. script_pushint(st,gc->economy); break;
  11442. case CD_CURRENT_DEFENSE:
  11443. script_pushint(st,gc->defense); break;
  11444. case CD_INVESTED_ECONOMY:
  11445. script_pushint(st,gc->triggerE); break;
  11446. case CD_INVESTED_DEFENSE:
  11447. script_pushint(st,gc->triggerD); break;
  11448. case CD_NEXT_TIME:
  11449. script_pushint(st,gc->nextTime); break;
  11450. case CD_PAY_TIME:
  11451. script_pushint(st,gc->payTime); break;
  11452. case CD_CREATE_TIME:
  11453. script_pushint(st,gc->createTime); break;
  11454. case CD_ENABLED_KAFRA:
  11455. script_pushint(st,gc->visibleC); break;
  11456. default:
  11457. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  11458. script_pushint(st,gc->guardian[index - CD_ENABLED_GUARDIAN00].visible);
  11459. break;
  11460. }
  11461. script_pushint(st,0);
  11462. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11463. return SCRIPT_CMD_FAILURE;
  11464. }
  11465. return SCRIPT_CMD_SUCCESS;
  11466. }
  11467. BUILDIN_FUNC(setcastledata)
  11468. {
  11469. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11470. int index = script_getnum(st,3);
  11471. int value = script_getnum(st,4);
  11472. struct guild_castle *gc = guild_mapname2gc(mapname);
  11473. if (gc == NULL) {
  11474. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11475. return SCRIPT_CMD_FAILURE;
  11476. }
  11477. if (index <= CD_NONE || index >= CD_MAX) {
  11478. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11479. return SCRIPT_CMD_FAILURE;
  11480. }
  11481. guild_castledatasave(gc->castle_id, index, value);
  11482. return SCRIPT_CMD_SUCCESS;
  11483. }
  11484. /* =====================================================================
  11485. * ---------------------------------------------------------------------*/
  11486. BUILDIN_FUNC(requestguildinfo)
  11487. {
  11488. int guild_id=script_getnum(st,2);
  11489. const char *event=NULL;
  11490. if( script_hasdata(st,3) ){
  11491. event=script_getstr(st,3);
  11492. check_event(st, event);
  11493. }
  11494. if(guild_id>0)
  11495. guild_npc_request_info(guild_id,event);
  11496. return SCRIPT_CMD_SUCCESS;
  11497. }
  11498. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11499. /// getequipcardcnt(<equipment slot>);
  11500. BUILDIN_FUNC(getequipcardcnt)
  11501. {
  11502. int i=-1,j,num;
  11503. TBL_PC *sd;
  11504. int count;
  11505. if( !script_rid2sd(sd) )
  11506. return SCRIPT_CMD_SUCCESS;
  11507. num=script_getnum(st,2);
  11508. if (equip_index_check(num))
  11509. i=pc_checkequip(sd,equip_bitmask[num]);
  11510. if (i < 0 || !sd->inventory_data[i]) {
  11511. script_pushint(st,0);
  11512. return SCRIPT_CMD_SUCCESS;
  11513. }
  11514. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11515. {
  11516. script_pushint(st,0);
  11517. return SCRIPT_CMD_SUCCESS;
  11518. }
  11519. count = 0;
  11520. for( j = 0; j < sd->inventory_data[i]->slots; j++ )
  11521. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11522. count++;
  11523. script_pushint(st,count);
  11524. return SCRIPT_CMD_SUCCESS;
  11525. }
  11526. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11527. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11528. /// successremovecards <slot>;
  11529. BUILDIN_FUNC(successremovecards) {
  11530. int i=-1,c,cardflag=0;
  11531. TBL_PC* sd;
  11532. int num;
  11533. if( !script_rid2sd(sd) )
  11534. return SCRIPT_CMD_SUCCESS;
  11535. num = script_getnum(st,2);
  11536. if (equip_index_check(num))
  11537. i=pc_checkequip(sd,equip_bitmask[num]);
  11538. if (i < 0 || !sd->inventory_data[i]) {
  11539. return SCRIPT_CMD_SUCCESS;
  11540. }
  11541. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11542. return SCRIPT_CMD_SUCCESS;
  11543. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11544. 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
  11545. unsigned char flag = 0;
  11546. struct item item_tmp;
  11547. memset(&item_tmp,0,sizeof(item_tmp));
  11548. cardflag = 1;
  11549. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11550. item_tmp.identify = 1;
  11551. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11552. clif_additem(sd,0,0,flag);
  11553. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11554. }
  11555. }
  11556. }
  11557. if(cardflag == 1) {//if card was remove remplace item with no card
  11558. unsigned char flag = 0;
  11559. struct item item_tmp;
  11560. memset(&item_tmp,0,sizeof(item_tmp));
  11561. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11562. item_tmp.identify = 1;
  11563. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11564. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11565. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11566. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11567. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11568. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11569. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11570. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11571. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11572. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11573. }
  11574. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11575. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11576. clif_additem(sd,0,0,flag);
  11577. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11578. }
  11579. clif_misceffect(&sd->bl,3);
  11580. }
  11581. return SCRIPT_CMD_SUCCESS;
  11582. }
  11583. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11584. /// failedremovecards <slot>, <type>;
  11585. /// <type>=0 : will destroy both the item and the cards.
  11586. /// <type>=1 : will keep the item, but destroy the cards.
  11587. /// <type>=2 : will keep the cards, but destroy the item.
  11588. /// <type>=? : will just display the failure effect.
  11589. BUILDIN_FUNC(failedremovecards) {
  11590. int i=-1,c,cardflag=0;
  11591. TBL_PC* sd;
  11592. int num;
  11593. int typefail;
  11594. if( !script_rid2sd(sd) )
  11595. return SCRIPT_CMD_SUCCESS;
  11596. num = script_getnum(st,2);
  11597. typefail = script_getnum(st,3);
  11598. if (equip_index_check(num))
  11599. i=pc_checkequip(sd,equip_bitmask[num]);
  11600. if (i < 0 || !sd->inventory_data[i])
  11601. return SCRIPT_CMD_SUCCESS;
  11602. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11603. return SCRIPT_CMD_SUCCESS;
  11604. for( c = sd->inventory_data[i]->slots - 1; c >= 0; --c ) {
  11605. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11606. cardflag = 1;
  11607. if(typefail == 2) {// add cards to inventory, clear
  11608. unsigned char flag = 0;
  11609. struct item item_tmp;
  11610. memset(&item_tmp,0,sizeof(item_tmp));
  11611. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11612. item_tmp.identify = 1;
  11613. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11614. clif_additem(sd,0,0,flag);
  11615. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11616. }
  11617. }
  11618. }
  11619. }
  11620. if(cardflag == 1) {
  11621. if(typefail == 0 || typefail == 2){ // destroy the item
  11622. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11623. }else if(typefail == 1){ // destroy the card
  11624. unsigned char flag = 0;
  11625. struct item item_tmp;
  11626. memset(&item_tmp,0,sizeof(item_tmp));
  11627. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11628. item_tmp.identify = 1;
  11629. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11630. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11631. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11632. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11633. for (int j = sd->inventory_data[i]->slots; j < MAX_SLOTS; j++)
  11634. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11635. for (int j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11636. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11637. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11638. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11639. }
  11640. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11641. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11642. clif_additem(sd,0,0,flag);
  11643. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11644. }
  11645. }
  11646. clif_misceffect(&sd->bl,2);
  11647. }
  11648. return SCRIPT_CMD_SUCCESS;
  11649. }
  11650. /* ================================================================
  11651. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11652. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11653. * improved by [Lance]
  11654. * ================================================================*/
  11655. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11656. {
  11657. int x,y,m,check_val=0,check_ID=0,i=0;
  11658. struct guild *g = NULL;
  11659. struct party_data *p = NULL;
  11660. const char *str;
  11661. const char *mapname;
  11662. unsigned int index;
  11663. mapname=script_getstr(st,2);
  11664. str=script_getstr(st,3);
  11665. x=script_getnum(st,4);
  11666. y=script_getnum(st,5);
  11667. if(script_hasdata(st,7)){
  11668. check_val=script_getnum(st,6);
  11669. check_ID=script_getnum(st,7);
  11670. }
  11671. if((m=map_mapname2mapid(mapname))< 0)
  11672. return SCRIPT_CMD_SUCCESS;
  11673. if(!(index=mapindex_name2id(str)))
  11674. return SCRIPT_CMD_SUCCESS;
  11675. switch(check_val){
  11676. case 1:
  11677. g = guild_search(check_ID);
  11678. if (g){
  11679. for( i=0; i < g->max_member; i++)
  11680. {
  11681. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11682. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11683. }
  11684. }
  11685. }
  11686. break;
  11687. case 2:
  11688. p = party_search(check_ID);
  11689. if(p){
  11690. for(i=0;i<MAX_PARTY; i++){
  11691. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11692. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11693. }
  11694. }
  11695. }
  11696. break;
  11697. default:
  11698. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11699. break;
  11700. }
  11701. return SCRIPT_CMD_SUCCESS;
  11702. }
  11703. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11704. {
  11705. char *event=va_arg(ap,char *);
  11706. struct mob_data *md = ((struct mob_data *)bl);
  11707. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11708. return 1;
  11709. return SCRIPT_CMD_SUCCESS;
  11710. }
  11711. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11712. {
  11713. const char *mapname,*event;
  11714. int16 m;
  11715. mapname=script_getstr(st,2);
  11716. event=script_getstr(st,3);
  11717. if( strcmp(event, "all") == 0 )
  11718. event = NULL;
  11719. else
  11720. check_event(st, event);
  11721. if( strcmp(mapname, "this") == 0 ) {
  11722. struct map_session_data *sd;
  11723. if( script_rid2sd(sd) )
  11724. m = sd->bl.m;
  11725. else {
  11726. script_pushint(st,-1);
  11727. return SCRIPT_CMD_SUCCESS;
  11728. }
  11729. }
  11730. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11731. script_pushint(st,-1);
  11732. return SCRIPT_CMD_SUCCESS;
  11733. }
  11734. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11735. return SCRIPT_CMD_SUCCESS;
  11736. }
  11737. BUILDIN_FUNC(marriage)
  11738. {
  11739. TBL_PC *sd;
  11740. TBL_PC *p_sd;
  11741. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11742. script_pushint(st,0);
  11743. return SCRIPT_CMD_SUCCESS;
  11744. }
  11745. script_pushint(st,1);
  11746. return SCRIPT_CMD_SUCCESS;
  11747. }
  11748. BUILDIN_FUNC(wedding_effect)
  11749. {
  11750. TBL_PC *sd;
  11751. struct block_list *bl;
  11752. if(!script_rid2sd(sd)) {
  11753. bl=map_id2bl(st->oid);
  11754. } else
  11755. bl=&sd->bl;
  11756. clif_wedding_effect(bl);
  11757. return SCRIPT_CMD_SUCCESS;
  11758. }
  11759. /**
  11760. * divorce({<char_id>})
  11761. **/
  11762. BUILDIN_FUNC(divorce)
  11763. {
  11764. TBL_PC *sd;
  11765. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11766. script_pushint(st,0);
  11767. return SCRIPT_CMD_FAILURE;
  11768. }
  11769. script_pushint(st,1);
  11770. return SCRIPT_CMD_SUCCESS;
  11771. }
  11772. /**
  11773. * ispartneron({<char_id>})
  11774. **/
  11775. BUILDIN_FUNC(ispartneron)
  11776. {
  11777. TBL_PC *sd;
  11778. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11779. map_charid2sd(sd->status.partner_id) == NULL)
  11780. {
  11781. script_pushint(st,0);
  11782. return SCRIPT_CMD_FAILURE;
  11783. }
  11784. script_pushint(st,1);
  11785. return SCRIPT_CMD_SUCCESS;
  11786. }
  11787. /**
  11788. * getpartnerid({<char_id>})
  11789. **/
  11790. BUILDIN_FUNC(getpartnerid)
  11791. {
  11792. TBL_PC *sd;
  11793. if (!script_charid2sd(2,sd)) {
  11794. script_pushint(st,0);
  11795. return SCRIPT_CMD_FAILURE;
  11796. }
  11797. script_pushint(st,sd->status.partner_id);
  11798. return SCRIPT_CMD_SUCCESS;
  11799. }
  11800. /**
  11801. * getchildid({<char_id>})
  11802. **/
  11803. BUILDIN_FUNC(getchildid)
  11804. {
  11805. TBL_PC *sd;
  11806. if (!script_charid2sd(2,sd)) {
  11807. script_pushint(st,0);
  11808. return SCRIPT_CMD_FAILURE;
  11809. }
  11810. script_pushint(st,sd->status.child);
  11811. return SCRIPT_CMD_SUCCESS;
  11812. }
  11813. /**
  11814. * getmotherid({<char_id>})
  11815. **/
  11816. BUILDIN_FUNC(getmotherid)
  11817. {
  11818. TBL_PC *sd;
  11819. if (!script_charid2sd(2,sd)) {
  11820. script_pushint(st,0);
  11821. return SCRIPT_CMD_FAILURE;
  11822. }
  11823. script_pushint(st,sd->status.mother);
  11824. return SCRIPT_CMD_SUCCESS;
  11825. }
  11826. /**
  11827. * getfatherid({<char_id>})
  11828. **/
  11829. BUILDIN_FUNC(getfatherid)
  11830. {
  11831. TBL_PC *sd;
  11832. if (!script_charid2sd(2,sd)) {
  11833. script_pushint(st,0);
  11834. return SCRIPT_CMD_FAILURE;
  11835. }
  11836. script_pushint(st,sd->status.father);
  11837. return SCRIPT_CMD_SUCCESS;
  11838. }
  11839. BUILDIN_FUNC(warppartner)
  11840. {
  11841. int x,y;
  11842. unsigned short mapindex;
  11843. const char *str;
  11844. TBL_PC *sd;
  11845. TBL_PC *p_sd;
  11846. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  11847. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11848. script_pushint(st,0);
  11849. return SCRIPT_CMD_SUCCESS;
  11850. }
  11851. str=script_getstr(st,2);
  11852. x=script_getnum(st,3);
  11853. y=script_getnum(st,4);
  11854. mapindex = mapindex_name2id(str);
  11855. if (mapindex) {
  11856. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11857. script_pushint(st,1);
  11858. } else
  11859. script_pushint(st,0);
  11860. return SCRIPT_CMD_SUCCESS;
  11861. }
  11862. /*================================================
  11863. * Script for Displaying MOB Information [Valaris]
  11864. *------------------------------------------------*/
  11865. BUILDIN_FUNC(strmobinfo)
  11866. {
  11867. int num=script_getnum(st,2);
  11868. int class_=script_getnum(st,3);
  11869. if(!mobdb_checkid(class_))
  11870. {
  11871. if (num < 3) //requested a string
  11872. script_pushconststr(st,"");
  11873. else
  11874. script_pushint(st,0);
  11875. return SCRIPT_CMD_SUCCESS;
  11876. }
  11877. std::shared_ptr<s_mob_db> mob = mob_db.find(class_);
  11878. switch (num) {
  11879. case 1: script_pushstrcopy(st,mob->name.c_str()); break;
  11880. case 2: script_pushstrcopy(st, mob->jname.c_str()); break;
  11881. case 3: script_pushint(st,mob->lv); break;
  11882. case 4: script_pushint(st,mob->status.max_hp); break;
  11883. case 5: script_pushint(st,mob->status.max_sp); break;
  11884. case 6: script_pushint(st,mob->base_exp); break;
  11885. case 7: script_pushint(st,mob->job_exp); break;
  11886. default:
  11887. script_pushint(st,0);
  11888. break;
  11889. }
  11890. return SCRIPT_CMD_SUCCESS;
  11891. }
  11892. /*==========================================
  11893. * Summon guardians [Valaris]
  11894. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11895. *------------------------------------------*/
  11896. BUILDIN_FUNC(guardian)
  11897. {
  11898. int class_=0,x=0,y=0,guardian=0;
  11899. const char *str,*mapname,*evt="";
  11900. bool has_index = false;
  11901. mapname =script_getstr(st,2);
  11902. x =script_getnum(st,3);
  11903. y =script_getnum(st,4);
  11904. str =script_getstr(st,5);
  11905. class_=script_getnum(st,6);
  11906. if( script_hasdata(st,8) )
  11907. {// "<event label>",<guardian index>
  11908. evt=script_getstr(st,7);
  11909. guardian=script_getnum(st,8);
  11910. has_index = true;
  11911. } else if( script_hasdata(st,7) ){
  11912. if( script_isstring(st, 7) )
  11913. {// "<event label>"
  11914. evt=script_getstr(st,7);
  11915. } else
  11916. {// <guardian index>
  11917. guardian=script_getnum(st,7);
  11918. has_index = true;
  11919. }
  11920. }
  11921. check_event(st, evt);
  11922. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11923. return SCRIPT_CMD_SUCCESS;
  11924. }
  11925. /*==========================================
  11926. * Invisible Walls [Zephyrus]
  11927. *------------------------------------------*/
  11928. BUILDIN_FUNC(setwall)
  11929. {
  11930. const char *mapname, *name;
  11931. int x, y, m, size, dir;
  11932. bool shootable;
  11933. mapname = script_getstr(st,2);
  11934. x = script_getnum(st,3);
  11935. y = script_getnum(st,4);
  11936. size = script_getnum(st,5);
  11937. dir = script_getnum(st,6);
  11938. shootable = script_getnum(st,7) != 0;
  11939. name = script_getstr(st,8);
  11940. if( (m = map_mapname2mapid(mapname)) < 0 )
  11941. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11942. map_iwall_set(m, x, y, size, dir, shootable, name);
  11943. return SCRIPT_CMD_SUCCESS;
  11944. }
  11945. BUILDIN_FUNC(delwall)
  11946. {
  11947. const char *name = script_getstr(st,2);
  11948. if( !map_iwall_remove(name) ){
  11949. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  11950. return SCRIPT_CMD_FAILURE;
  11951. }
  11952. return SCRIPT_CMD_SUCCESS;
  11953. }
  11954. BUILDIN_FUNC(checkwall)
  11955. {
  11956. const char *wall_name = script_getstr(st, 2);
  11957. script_pushint(st, map_iwall_exist(wall_name));
  11958. return SCRIPT_CMD_SUCCESS;
  11959. }
  11960. /// Retrieves various information about the specified guardian.
  11961. ///
  11962. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11963. /// type: 0 - whether it is deployed or not
  11964. /// 1 - maximum hp
  11965. /// 2 - current hp
  11966. ///
  11967. BUILDIN_FUNC(guardianinfo)
  11968. {
  11969. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11970. int id = script_getnum(st,3);
  11971. int type = script_getnum(st,4);
  11972. struct guild_castle* gc = guild_mapname2gc(mapname);
  11973. struct mob_data* gd;
  11974. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11975. {
  11976. script_pushint(st,-1);
  11977. return SCRIPT_CMD_SUCCESS;
  11978. }
  11979. if( type == 0 )
  11980. script_pushint(st, gc->guardian[id].visible);
  11981. else
  11982. if( !gc->guardian[id].visible )
  11983. script_pushint(st,-1);
  11984. else
  11985. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11986. script_pushint(st,-1);
  11987. else
  11988. {
  11989. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11990. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11991. else
  11992. script_pushint(st,-1);
  11993. }
  11994. return SCRIPT_CMD_SUCCESS;
  11995. }
  11996. /*==========================================
  11997. * Get the item name by item_id or null
  11998. *------------------------------------------*/
  11999. BUILDIN_FUNC(getitemname)
  12000. {
  12001. t_itemid item_id = 0;
  12002. struct item_data *i_data;
  12003. char *item_name;
  12004. if( script_isstring(st, 2) ){
  12005. const char *name = script_getstr(st, 2);
  12006. struct item_data *item_data = itemdb_searchname(name);
  12007. if( item_data )
  12008. item_id=item_data->nameid;
  12009. }else
  12010. item_id = script_getnum(st, 2);
  12011. i_data = itemdb_exists(item_id);
  12012. if (i_data == NULL)
  12013. {
  12014. script_pushconststr(st,"null");
  12015. return SCRIPT_CMD_SUCCESS;
  12016. }
  12017. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  12018. memcpy(item_name, i_data->ename.c_str(), ITEM_NAME_LENGTH);
  12019. script_pushstr(st,item_name);
  12020. return SCRIPT_CMD_SUCCESS;
  12021. }
  12022. /*==========================================
  12023. * Returns number of slots an item has. [Skotlex]
  12024. *------------------------------------------*/
  12025. BUILDIN_FUNC(getitemslots)
  12026. {
  12027. struct item_data *i_data;
  12028. t_itemid item_id=script_getnum(st,2);
  12029. i_data = itemdb_exists(item_id);
  12030. if (i_data)
  12031. script_pushint(st,i_data->slots);
  12032. else
  12033. script_pushint(st,-1);
  12034. return SCRIPT_CMD_SUCCESS;
  12035. }
  12036. // TODO: add matk here if needed/once we get rid of RENEWAL
  12037. /*==========================================
  12038. * Returns some values of an item [Lupus]
  12039. * Price, Weight, etc...
  12040. getiteminfo(itemID,n), where n
  12041. 0 value_buy;
  12042. 1 value_sell;
  12043. 2 type;
  12044. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  12045. if = 0, then monsters don't drop it at all (rare or a quest item)
  12046. if = -1, then this item is sold in NPC shops only
  12047. 4 sex;
  12048. 5 equip;
  12049. 6 weight;
  12050. 7 atk;
  12051. 8 def;
  12052. 9 range;
  12053. 10 slot;
  12054. 11 look;
  12055. 12 elv;
  12056. 13 wlv;
  12057. 14 view id
  12058. 15 eLvmax
  12059. 16 matk (renewal)
  12060. *------------------------------------------*/
  12061. BUILDIN_FUNC(getiteminfo)
  12062. {
  12063. t_itemid item_id = script_getnum(st,2);
  12064. item_data *i_data = itemdb_exists(item_id);
  12065. if (i_data == nullptr) {
  12066. script_pushint(st, -1);
  12067. return SCRIPT_CMD_SUCCESS;
  12068. }
  12069. switch( script_getnum(st, 3) ) {
  12070. case 0: script_pushint(st, i_data->value_buy); break;
  12071. case 1: script_pushint(st, i_data->value_sell); break;
  12072. case 2: script_pushint(st, i_data->type); break;
  12073. case 3: script_pushint(st, i_data->maxchance); break;
  12074. case 4: script_pushint(st, i_data->sex); break;
  12075. case 5: script_pushint(st, i_data->equip); break;
  12076. case 6: script_pushint(st, i_data->weight); break;
  12077. case 7: script_pushint(st, i_data->atk); break;
  12078. case 8: script_pushint(st, i_data->def); break;
  12079. case 9: script_pushint(st, i_data->range); break;
  12080. case 10: script_pushint(st, i_data->slots); break;
  12081. case 11:
  12082. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12083. script_pushint(st, i_data->subtype);
  12084. } else {
  12085. script_pushint(st, i_data->look);
  12086. }
  12087. break;
  12088. case 12: script_pushint(st, i_data->elv); break;
  12089. case 13: script_pushint(st, i_data->wlv); break;
  12090. case 14: script_pushint(st, i_data->view_id); break;
  12091. case 15: script_pushint(st, i_data->elvmax); break;
  12092. case 16: {
  12093. #ifdef RENEWAL
  12094. script_pushint(st, i_data->matk);
  12095. #else
  12096. script_pushint(st, 0);
  12097. #endif
  12098. break;
  12099. }
  12100. default:
  12101. script_pushint(st, -1);
  12102. break;
  12103. }
  12104. return SCRIPT_CMD_SUCCESS;
  12105. }
  12106. /*==========================================
  12107. * Set some values of an item [Lupus]
  12108. * Price, Weight, etc...
  12109. setiteminfo(itemID,n,Value), where n
  12110. 0 value_buy;
  12111. 1 value_sell;
  12112. 2 type;
  12113. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  12114. if = 0, then monsters don't drop it at all (rare or a quest item)
  12115. if = -1, then this item is sold in NPC shops only
  12116. 4 sex;
  12117. 5 equip;
  12118. 6 weight;
  12119. 7 atk;
  12120. 8 def;
  12121. 9 range;
  12122. 10 slot;
  12123. 11 look;
  12124. 12 elv;
  12125. 13 wlv;
  12126. 14 view id
  12127. 15 eLvmax
  12128. 16 matk (renewal)
  12129. * Returns Value or -1 if the wrong field's been set
  12130. *------------------------------------------*/
  12131. BUILDIN_FUNC(setiteminfo)
  12132. {
  12133. t_itemid item_id = script_getnum(st,2);
  12134. item_data *i_data = itemdb_exists(item_id);
  12135. if (i_data == nullptr) {
  12136. script_pushint(st, -1);
  12137. return SCRIPT_CMD_SUCCESS;
  12138. }
  12139. int value = script_getnum(st,4);
  12140. switch( script_getnum(st, 3) ) {
  12141. case 0: i_data->value_buy = static_cast<uint32>(value); break;
  12142. case 1: i_data->value_sell = static_cast<uint32>(value); break;
  12143. case 2: i_data->type = static_cast<item_types>(value); break;
  12144. case 3: i_data->maxchance = static_cast<int>(value); break;
  12145. case 4: i_data->sex = static_cast<uint8>(value); break;
  12146. case 5: i_data->equip = static_cast<uint32>(value); break;
  12147. case 6: i_data->weight = static_cast<uint32>(value); break;
  12148. case 7: i_data->atk = static_cast<uint32>(value); break;
  12149. case 8: i_data->def = static_cast<uint32>(value); break;
  12150. case 9: i_data->range = static_cast<uint16>(value); break;
  12151. case 10: i_data->slots = static_cast<uint16>(value); break;
  12152. case 11:
  12153. if (i_data->type == IT_WEAPON || i_data->type == IT_AMMO) { // keep old compatibility
  12154. i_data->subtype = static_cast<uint8>(value);
  12155. } else {
  12156. i_data->look = static_cast<uint32>(value);
  12157. }
  12158. break;
  12159. case 12: i_data->elv = static_cast<uint16>(value); break;
  12160. case 13: i_data->wlv = static_cast<uint16>(value); break;
  12161. case 14: i_data->view_id = static_cast<t_itemid>(value); break;
  12162. case 15: i_data->elvmax = static_cast<uint16>(value); break;
  12163. case 16: {
  12164. #ifdef RENEWAL
  12165. i_data->matk = static_cast<uint32>(value);
  12166. #else
  12167. value = 0;
  12168. #endif
  12169. break;
  12170. }
  12171. default:
  12172. script_pushint(st, -1);
  12173. break;
  12174. }
  12175. script_pushint(st, value);
  12176. return SCRIPT_CMD_SUCCESS;
  12177. }
  12178. /*==========================================
  12179. * Returns value from equipped item slot n [Lupus]
  12180. getequipcardid(num,slot)
  12181. where
  12182. num = eqip position slot
  12183. slot = 0,1,2,3 (Card Slot N)
  12184. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12185. it's useful when you want to check item cards or if it's signed
  12186. Useful for such quests as "Sign this refined item with players name" etc
  12187. Hat[0] +4 -> Player's Hat[0] +4
  12188. *------------------------------------------*/
  12189. BUILDIN_FUNC(getequipcardid)
  12190. {
  12191. int i=-1,num,slot;
  12192. TBL_PC *sd;
  12193. if( !script_rid2sd(sd) )
  12194. return SCRIPT_CMD_SUCCESS;
  12195. num=script_getnum(st,2);
  12196. slot=script_getnum(st,3);
  12197. if (equip_index_check(num))
  12198. i=pc_checkequip(sd,equip_bitmask[num]);
  12199. if(i >= 0 && slot>=0 && slot<4)
  12200. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12201. else
  12202. script_pushint(st,0);
  12203. return SCRIPT_CMD_SUCCESS;
  12204. }
  12205. /*==========================================
  12206. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12207. *------------------------------------------*/
  12208. BUILDIN_FUNC(petskillbonus)
  12209. {
  12210. struct pet_data *pd;
  12211. TBL_PC *sd;
  12212. if(!script_rid2sd(sd) || sd->pd == NULL)
  12213. return SCRIPT_CMD_FAILURE;
  12214. pd = sd->pd;
  12215. if (pd->bonus)
  12216. { //Clear previous bonus
  12217. if (pd->bonus->timer != INVALID_TIMER)
  12218. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12219. } else //init
  12220. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12221. pd->bonus->type = script_getnum(st,2);
  12222. pd->bonus->val = script_getnum(st,3);
  12223. pd->bonus->duration = script_getnum(st,4);
  12224. pd->bonus->delay = script_getnum(st,5);
  12225. if (pd->state.skillbonus == 1)
  12226. pd->state.skillbonus = 0; // waiting state
  12227. // wait for timer to start
  12228. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12229. pd->bonus->timer = INVALID_TIMER;
  12230. else
  12231. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12232. return SCRIPT_CMD_SUCCESS;
  12233. }
  12234. /*==========================================
  12235. * pet looting [Valaris] //Rewritten by [Skotlex]
  12236. *------------------------------------------*/
  12237. BUILDIN_FUNC(petloot)
  12238. {
  12239. int max;
  12240. struct pet_data *pd;
  12241. TBL_PC *sd;
  12242. if(!script_rid2sd(sd) || sd->pd==NULL)
  12243. return SCRIPT_CMD_SUCCESS;
  12244. max=script_getnum(st,2);
  12245. if(max < 1)
  12246. max = 1; //Let'em loot at least 1 item.
  12247. else if (max > MAX_PETLOOT_SIZE)
  12248. max = MAX_PETLOOT_SIZE;
  12249. pd = sd->pd;
  12250. if (pd->loot != NULL)
  12251. { //Release whatever was there already and reallocate memory
  12252. pet_lootitem_drop(pd, pd->master);
  12253. aFree(pd->loot->item);
  12254. }
  12255. else
  12256. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12257. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12258. pd->loot->max=max;
  12259. pd->loot->count = 0;
  12260. pd->loot->weight = 0;
  12261. return SCRIPT_CMD_SUCCESS;
  12262. }
  12263. /*==========================================
  12264. * Set arrays with info of all sd inventory :
  12265. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12266. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12267. * @inventorylist_card(0..3), @inventorylist_expire
  12268. * @inventorylist_count = scalar
  12269. *------------------------------------------*/
  12270. BUILDIN_FUNC(getinventorylist)
  12271. {
  12272. TBL_PC *sd;
  12273. char card_var[NAME_LENGTH], randopt_var[50];
  12274. int i,j=0,k;
  12275. if (!script_charid2sd(2,sd))
  12276. return SCRIPT_CMD_FAILURE;
  12277. for(i=0;i<MAX_INVENTORY;i++){
  12278. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12279. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12280. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12281. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12282. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12283. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12284. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12285. for (k = 0; k < MAX_SLOTS; k++)
  12286. {
  12287. sprintf(card_var, "@inventorylist_card%d",k+1);
  12288. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12289. }
  12290. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12291. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12292. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12293. {
  12294. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12295. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12296. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12297. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12298. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12299. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12300. }
  12301. pc_setreg(sd,reference_uid(add_str("@inventorylist_tradable"), j),pc_can_trade_item(sd, i));
  12302. j++;
  12303. }
  12304. }
  12305. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12306. return SCRIPT_CMD_SUCCESS;
  12307. }
  12308. /**
  12309. * getskilllist ({<char_id>});
  12310. **/
  12311. BUILDIN_FUNC(getskilllist)
  12312. {
  12313. TBL_PC *sd;
  12314. int i,j=0;
  12315. if (!script_charid2sd(2,sd))
  12316. return SCRIPT_CMD_FAILURE;
  12317. for(i=0;i<MAX_SKILL;i++){
  12318. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12319. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12320. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12321. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12322. j++;
  12323. }
  12324. }
  12325. pc_setreg(sd,add_str("@skilllist_count"),j);
  12326. return SCRIPT_CMD_SUCCESS;
  12327. }
  12328. /**
  12329. * clearitem ({<char_id>});
  12330. **/
  12331. BUILDIN_FUNC(clearitem)
  12332. {
  12333. TBL_PC *sd;
  12334. int i;
  12335. if (!script_charid2sd(2,sd))
  12336. return SCRIPT_CMD_FAILURE;
  12337. for (i=0; i<MAX_INVENTORY; i++) {
  12338. if (sd->inventory.u.items_inventory[i].amount) {
  12339. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12340. }
  12341. }
  12342. return SCRIPT_CMD_SUCCESS;
  12343. }
  12344. /**
  12345. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12346. * disguise <Monster ID>{,<char_id>};
  12347. **/
  12348. BUILDIN_FUNC(disguise)
  12349. {
  12350. int id;
  12351. TBL_PC* sd;
  12352. if (!script_charid2sd(3,sd))
  12353. return SCRIPT_CMD_FAILURE;
  12354. id = script_getnum(st,2);
  12355. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12356. pc_disguise(sd, id);
  12357. script_pushint(st,id);
  12358. } else
  12359. script_pushint(st,0);
  12360. return SCRIPT_CMD_SUCCESS;
  12361. }
  12362. /**
  12363. * Undisguise Player (returns 1 if success, 0 on fail)
  12364. * undisguise {<char_id>};
  12365. **/
  12366. BUILDIN_FUNC(undisguise)
  12367. {
  12368. TBL_PC* sd;
  12369. if (!script_charid2sd(2,sd))
  12370. return SCRIPT_CMD_FAILURE;
  12371. if (sd->disguise) {
  12372. pc_disguise(sd, 0);
  12373. script_pushint(st,0);
  12374. } else {
  12375. script_pushint(st,1);
  12376. }
  12377. return SCRIPT_CMD_SUCCESS;
  12378. }
  12379. /**
  12380. * Transform an NPC to another _class
  12381. *
  12382. * classchange(<view id>{,"<NPC name>","<flag>"});
  12383. * @param flag: Specify target
  12384. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12385. * BC_SELF - Sprite is sent only to player attached.
  12386. */
  12387. BUILDIN_FUNC(classchange)
  12388. {
  12389. int _class, type = 1;
  12390. struct npc_data* nd = NULL;
  12391. TBL_PC *sd = map_id2sd(st->rid);
  12392. send_target target = AREA;
  12393. _class = script_getnum(st,2);
  12394. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12395. nd = npc_name2id(script_getstr(st, 3));
  12396. else
  12397. nd = (struct npc_data *)map_id2bl(st->oid);
  12398. if (nd == NULL)
  12399. return SCRIPT_CMD_FAILURE;
  12400. if (script_hasdata(st, 4)) {
  12401. switch(script_getnum(st, 4)) {
  12402. case BC_SELF: target = SELF; break;
  12403. case BC_AREA:
  12404. default: target = AREA; break;
  12405. }
  12406. }
  12407. if (target != SELF)
  12408. clif_class_change(&nd->bl,_class,type);
  12409. else if (sd == NULL)
  12410. return SCRIPT_CMD_FAILURE;
  12411. else
  12412. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12413. return SCRIPT_CMD_SUCCESS;
  12414. }
  12415. /*==========================================
  12416. * Display an effect
  12417. *------------------------------------------*/
  12418. BUILDIN_FUNC(misceffect)
  12419. {
  12420. int type;
  12421. type=script_getnum(st,2);
  12422. if( type <= EF_NONE || type >= EF_MAX ){
  12423. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12424. return SCRIPT_CMD_FAILURE;
  12425. }
  12426. if(st->oid && st->oid != fake_nd->bl.id) {
  12427. struct block_list *bl = map_id2bl(st->oid);
  12428. if (bl)
  12429. clif_specialeffect(bl,type,AREA);
  12430. } else{
  12431. TBL_PC *sd;
  12432. if(script_rid2sd(sd))
  12433. clif_specialeffect(&sd->bl,type,AREA);
  12434. }
  12435. return SCRIPT_CMD_SUCCESS;
  12436. }
  12437. /*==========================================
  12438. * Play a BGM on a single client [Rikter/Yommy]
  12439. *------------------------------------------*/
  12440. BUILDIN_FUNC(playBGM)
  12441. {
  12442. struct map_session_data* sd;
  12443. if( script_rid2sd(sd) ) {
  12444. clif_playBGM(sd, script_getstr(st,2));
  12445. }
  12446. return SCRIPT_CMD_SUCCESS;
  12447. }
  12448. static int playBGM_sub(struct block_list* bl,va_list ap)
  12449. {
  12450. const char* name = va_arg(ap,const char*);
  12451. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12452. return 0;
  12453. }
  12454. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12455. {
  12456. const char* name = va_arg(args, const char*);
  12457. clif_playBGM(sd, name);
  12458. return 0;
  12459. }
  12460. /*==========================================
  12461. * Play a BGM on multiple client [Rikter/Yommy]
  12462. *------------------------------------------*/
  12463. BUILDIN_FUNC(playBGMall)
  12464. {
  12465. const char* name;
  12466. name = script_getstr(st,2);
  12467. if( script_hasdata(st,7) ) {// specified part of map
  12468. const char* mapname = script_getstr(st,3);
  12469. int x0 = script_getnum(st,4);
  12470. int y0 = script_getnum(st,5);
  12471. int x1 = script_getnum(st,6);
  12472. int y1 = script_getnum(st,7);
  12473. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12474. }
  12475. else if( script_hasdata(st,3) ) {// entire map
  12476. const char* mapname = script_getstr(st,3);
  12477. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12478. }
  12479. else {// entire server
  12480. map_foreachpc(&playBGM_foreachpc_sub, name);
  12481. }
  12482. return SCRIPT_CMD_SUCCESS;
  12483. }
  12484. /*==========================================
  12485. * Play a .wav sound for sd
  12486. *------------------------------------------*/
  12487. BUILDIN_FUNC(soundeffect)
  12488. {
  12489. TBL_PC* sd;
  12490. if(script_rid2sd(sd)){
  12491. const char* name = script_getstr(st,2);
  12492. int type = script_getnum(st,3);
  12493. clif_soundeffect(sd,&sd->bl,name,type);
  12494. }
  12495. return SCRIPT_CMD_SUCCESS;
  12496. }
  12497. int soundeffect_sub(struct block_list* bl,va_list ap)
  12498. {
  12499. char* name = va_arg(ap,char*);
  12500. int type = va_arg(ap,int);
  12501. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12502. return 0;
  12503. }
  12504. /*==========================================
  12505. * Play a sound effect (.wav) on multiple clients
  12506. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12507. *------------------------------------------*/
  12508. BUILDIN_FUNC(soundeffectall)
  12509. {
  12510. struct block_list* bl;
  12511. struct map_session_data* sd;
  12512. const char* name;
  12513. int type;
  12514. if( st->rid && script_rid2sd(sd) )
  12515. bl = &sd->bl;
  12516. else
  12517. bl = map_id2bl(st->oid);
  12518. if (!bl)
  12519. return SCRIPT_CMD_SUCCESS;
  12520. name = script_getstr(st,2);
  12521. type = script_getnum(st,3);
  12522. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12523. if(!script_hasdata(st,4))
  12524. { // area around
  12525. clif_soundeffectall(bl, name, type, AREA);
  12526. }
  12527. else
  12528. if(!script_hasdata(st,5))
  12529. { // entire map
  12530. const char* mapname = script_getstr(st,4);
  12531. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12532. }
  12533. else
  12534. if(script_hasdata(st,8))
  12535. { // specified part of map
  12536. const char* mapname = script_getstr(st,4);
  12537. int x0 = script_getnum(st,5);
  12538. int y0 = script_getnum(st,6);
  12539. int x1 = script_getnum(st,7);
  12540. int y1 = script_getnum(st,8);
  12541. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12542. }
  12543. else
  12544. {
  12545. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12546. }
  12547. return SCRIPT_CMD_SUCCESS;
  12548. }
  12549. /*==========================================
  12550. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12551. *------------------------------------------*/
  12552. BUILDIN_FUNC(petrecovery)
  12553. {
  12554. struct pet_data *pd;
  12555. TBL_PC *sd;
  12556. int sc;
  12557. if(!script_rid2sd(sd) || sd->pd == NULL)
  12558. return SCRIPT_CMD_FAILURE;
  12559. sc = script_getnum(st,2);
  12560. if (sc <= SC_NONE || sc >= SC_MAX) {
  12561. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12562. return SCRIPT_CMD_FAILURE;
  12563. }
  12564. pd = sd->pd;
  12565. if (pd->recovery)
  12566. { //Halt previous bonus
  12567. if (pd->recovery->timer != INVALID_TIMER)
  12568. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12569. } else //Init
  12570. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12571. pd->recovery->type = (sc_type)sc;
  12572. pd->recovery->delay = script_getnum(st,3);
  12573. pd->recovery->timer = INVALID_TIMER;
  12574. return SCRIPT_CMD_SUCCESS;
  12575. }
  12576. /*==========================================
  12577. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12578. *------------------------------------------*/
  12579. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12580. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12581. BUILDIN_FUNC(petskillattack)
  12582. {
  12583. struct pet_data *pd;
  12584. TBL_PC *sd;
  12585. int id = 0;
  12586. if(!script_rid2sd(sd) || sd->pd == NULL)
  12587. return SCRIPT_CMD_FAILURE;
  12588. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12589. if (!id) {
  12590. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12591. return SCRIPT_CMD_FAILURE;
  12592. }
  12593. pd = sd->pd;
  12594. if (pd->a_skill == NULL)
  12595. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12596. pd->a_skill->id = id;
  12597. pd->a_skill->damage = 0;
  12598. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12599. pd->a_skill->div_ = 0;
  12600. pd->a_skill->rate = script_getnum(st,4);
  12601. pd->a_skill->bonusrate = script_getnum(st,5);
  12602. return SCRIPT_CMD_SUCCESS;
  12603. }
  12604. /*==========================================
  12605. * pet attack skills [Valaris]
  12606. *------------------------------------------*/
  12607. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12608. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12609. BUILDIN_FUNC(petskillattack2)
  12610. {
  12611. struct pet_data *pd;
  12612. TBL_PC *sd;
  12613. int id = 0;
  12614. if(!script_rid2sd(sd) || sd->pd == NULL)
  12615. return SCRIPT_CMD_FAILURE;
  12616. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12617. if (!id) {
  12618. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12619. return SCRIPT_CMD_FAILURE;
  12620. }
  12621. pd = sd->pd;
  12622. if (pd->a_skill == NULL)
  12623. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12624. pd->a_skill->id = id;
  12625. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12626. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12627. pd->a_skill->div_ = script_getnum(st,4);
  12628. pd->a_skill->rate = script_getnum(st,5);
  12629. pd->a_skill->bonusrate = script_getnum(st,6);
  12630. return SCRIPT_CMD_SUCCESS;
  12631. }
  12632. /*==========================================
  12633. * pet support skills [Skotlex]
  12634. *------------------------------------------*/
  12635. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12636. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12637. BUILDIN_FUNC(petskillsupport)
  12638. {
  12639. struct pet_data *pd;
  12640. TBL_PC *sd;
  12641. int id = 0;
  12642. if(!script_rid2sd(sd) || sd->pd == NULL)
  12643. return SCRIPT_CMD_FAILURE;
  12644. id = (script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12645. if (!id) {
  12646. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12647. return SCRIPT_CMD_FAILURE;
  12648. }
  12649. pd = sd->pd;
  12650. if (pd->s_skill)
  12651. { //Clear previous skill
  12652. if (pd->s_skill->timer != INVALID_TIMER)
  12653. {
  12654. if (pd->s_skill->id)
  12655. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12656. else
  12657. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12658. }
  12659. } else //init memory
  12660. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12661. pd->s_skill->id = id;
  12662. pd->s_skill->lv = script_getnum(st,3);
  12663. pd->s_skill->delay = script_getnum(st,4);
  12664. pd->s_skill->hp = script_getnum(st,5);
  12665. pd->s_skill->sp = script_getnum(st,6);
  12666. //Use delay as initial offset to avoid skill/heal exploits
  12667. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12668. pd->s_skill->timer = INVALID_TIMER;
  12669. else
  12670. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12671. return SCRIPT_CMD_SUCCESS;
  12672. }
  12673. static inline void script_skill_effect(block_list *bl, uint16 skill_id, uint16 skill_lv, int16 x, int16 y) {
  12674. nullpo_retv(bl);
  12675. switch (skill_get_casttype(skill_id)) {
  12676. case CAST_GROUND:
  12677. clif_skill_poseffect(bl, skill_id, skill_lv, x, y, gettick());
  12678. break;
  12679. case CAST_NODAMAGE:
  12680. clif_skill_nodamage(bl, bl, skill_id, skill_lv, 1);
  12681. break;
  12682. case CAST_DAMAGE:
  12683. 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));
  12684. break;
  12685. }
  12686. }
  12687. /*==========================================
  12688. * Scripted skill effects [Celest]
  12689. *------------------------------------------*/
  12690. /// skilleffect <skill id>,<level>
  12691. /// skilleffect "<skill name>",<level>
  12692. BUILDIN_FUNC(skilleffect)
  12693. {
  12694. TBL_PC *sd;
  12695. uint16 skill_id, skill_lv;
  12696. if( !script_rid2sd(sd) )
  12697. return SCRIPT_CMD_FAILURE;
  12698. skill_id = ( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12699. skill_lv = script_getnum(st,3);
  12700. if (skill_db.find(skill_id) == nullptr) {
  12701. ShowError("buildin_skilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12702. return SCRIPT_CMD_FAILURE;
  12703. }
  12704. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12705. * which leaves the server thinking it still is sitting. */
  12706. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12707. skill_sit(sd, 0);
  12708. clif_standing(&sd->bl);
  12709. }
  12710. script_skill_effect(&sd->bl, skill_id, skill_lv, sd->bl.x, sd->bl.y);
  12711. return SCRIPT_CMD_SUCCESS;
  12712. }
  12713. /*==========================================
  12714. * NPC skill effects [Valaris]
  12715. *------------------------------------------*/
  12716. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12717. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12718. BUILDIN_FUNC(npcskilleffect)
  12719. {
  12720. struct block_list *bl= map_id2bl(st->oid);
  12721. if (bl == nullptr) {
  12722. ShowError("buildin_npcskilleffect: Invalid object attached to NPC.");
  12723. return SCRIPT_CMD_FAILURE;
  12724. }
  12725. uint16 skill_id, skill_lv;
  12726. int16 x, y;
  12727. skill_id=( script_isstring(st, 2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12728. skill_lv=script_getnum(st,3);
  12729. x=script_getnum(st,4);
  12730. y=script_getnum(st,5);
  12731. if (skill_db.find(skill_id) == nullptr) {
  12732. ShowError("buildin_npcskilleffect: Invalid skill defined (%s)!\n", script_getstr(st, 2));
  12733. return SCRIPT_CMD_FAILURE;
  12734. }
  12735. script_skill_effect(bl, skill_id, skill_lv, bl->x, bl->y);
  12736. return SCRIPT_CMD_SUCCESS;
  12737. }
  12738. /*==========================================
  12739. * Special effects [Valaris]
  12740. *------------------------------------------*/
  12741. BUILDIN_FUNC(specialeffect)
  12742. {
  12743. struct block_list *bl=map_id2bl(st->oid);
  12744. int type = script_getnum(st,2);
  12745. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12746. if(bl==NULL)
  12747. return SCRIPT_CMD_SUCCESS;
  12748. if( type <= EF_NONE || type >= EF_MAX ){
  12749. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  12750. return SCRIPT_CMD_FAILURE;
  12751. }
  12752. if( script_hasdata(st,4) )
  12753. {
  12754. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12755. if(nd)
  12756. clif_specialeffect(&nd->bl, type, target);
  12757. }
  12758. else
  12759. {
  12760. if (target == SELF) {
  12761. TBL_PC *sd;
  12762. if (script_rid2sd(sd))
  12763. clif_specialeffect_single(bl,type,sd->fd);
  12764. } else {
  12765. clif_specialeffect(bl, type, target);
  12766. }
  12767. }
  12768. return SCRIPT_CMD_SUCCESS;
  12769. }
  12770. BUILDIN_FUNC(specialeffect2)
  12771. {
  12772. TBL_PC *sd;
  12773. if( script_nick2sd(4,sd) ){
  12774. int type = script_getnum(st,2);
  12775. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12776. if( type <= EF_NONE || type >= EF_MAX ){
  12777. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  12778. return SCRIPT_CMD_FAILURE;
  12779. }
  12780. clif_specialeffect(&sd->bl, type, target);
  12781. }
  12782. return SCRIPT_CMD_SUCCESS;
  12783. }
  12784. /**
  12785. * nude({<char_id>});
  12786. * @author [Valaris]
  12787. **/
  12788. BUILDIN_FUNC(nude)
  12789. {
  12790. TBL_PC *sd;
  12791. int i, calcflag = 0;
  12792. if (!script_charid2sd(2,sd))
  12793. return SCRIPT_CMD_FAILURE;
  12794. for( i = 0 ; i < EQI_MAX; i++ ) {
  12795. if( sd->equip_index[ i ] >= 0 ) {
  12796. if( !calcflag )
  12797. calcflag = 1;
  12798. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12799. }
  12800. }
  12801. if( calcflag )
  12802. status_calc_pc(sd,SCO_NONE);
  12803. return SCRIPT_CMD_SUCCESS;
  12804. }
  12805. int atcommand_sub(struct script_state* st,int type) {
  12806. TBL_PC *sd, dummy_sd;
  12807. int fd;
  12808. const char *cmd;
  12809. cmd = script_getstr(st,2);
  12810. if (st->rid) {
  12811. if( !script_rid2sd(sd) )
  12812. return SCRIPT_CMD_SUCCESS;
  12813. fd = sd->fd;
  12814. } else { //Use a dummy character.
  12815. sd = &dummy_sd;
  12816. fd = 0;
  12817. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12818. if (st->oid) {
  12819. struct block_list* bl = map_id2bl(st->oid);
  12820. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12821. if (bl->type == BL_NPC)
  12822. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12823. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12824. }
  12825. // Init Group ID, Level, & permissions
  12826. sd->group_id = sd->group_level = 99;
  12827. sd->permissions |= PC_PERM_ALLPERMISSION;
  12828. }
  12829. if (!is_atcommand(fd, sd, cmd, type)) {
  12830. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12831. script_reportsrc(st);
  12832. return SCRIPT_CMD_FAILURE;
  12833. }
  12834. return SCRIPT_CMD_SUCCESS;
  12835. }
  12836. /*==========================================
  12837. * gmcommand [MouseJstr]
  12838. *------------------------------------------*/
  12839. BUILDIN_FUNC(atcommand) {
  12840. return atcommand_sub(st,0);
  12841. }
  12842. /** Displays a message for the player only (like system messages like "you got an apple" )
  12843. * dispbottom("<message>"{,<color>{,<char_id>}})
  12844. * @param message
  12845. * @param color Hex color default (Green)
  12846. */
  12847. BUILDIN_FUNC(dispbottom)
  12848. {
  12849. TBL_PC *sd;
  12850. int color = 0;
  12851. const char *message;
  12852. if (!script_charid2sd(4,sd))
  12853. return SCRIPT_CMD_FAILURE;
  12854. message = script_getstr(st,2);
  12855. if (script_hasdata(st,3))
  12856. color = script_getnum(st,3); // <color>
  12857. if(sd) {
  12858. if (script_hasdata(st,3))
  12859. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12860. else
  12861. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12862. }
  12863. return SCRIPT_CMD_SUCCESS;
  12864. }
  12865. /*===================================
  12866. * Heal portion of recovery command
  12867. *-----------------------------------*/
  12868. int recovery_sub(struct map_session_data* sd, int revive)
  12869. {
  12870. if(revive&(1|4) && pc_isdead(sd)) {
  12871. status_revive(&sd->bl, 100, 100);
  12872. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12873. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  12874. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12875. status_percent_heal(&sd->bl, 100, 100);
  12876. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12877. }
  12878. return SCRIPT_CMD_SUCCESS;
  12879. }
  12880. /*=========================================================================
  12881. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12882. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12883. * <type> determines <option>:
  12884. * 0 : char_id
  12885. * 1 : party_id
  12886. * 2 : guild_id
  12887. * 3 : map_name
  12888. * 4 : all characters
  12889. * <revive_flag>:
  12890. * 1 : Revive and heal all players (default)
  12891. * 2 : Heal living players only
  12892. * 4 : Revive dead players only
  12893. * <map name>:
  12894. * for types 1-2 : map_name (null = all maps)
  12895. *-------------------------------------------------------------------------*/
  12896. BUILDIN_FUNC(recovery)
  12897. {
  12898. TBL_PC *sd;
  12899. int map_idx = 0, type = 0, revive = 1;
  12900. type = script_getnum(st,2);
  12901. if(script_hasdata(st,4))
  12902. revive = script_getnum(st,4);
  12903. switch(type) {
  12904. case 0:
  12905. if(!script_charid2sd(3,sd))
  12906. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12907. recovery_sub(sd, revive);
  12908. break;
  12909. case 1:
  12910. {
  12911. struct party_data* p;
  12912. //When no party given, we use invoker party
  12913. int p_id = 0, i;
  12914. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12915. map_idx = map_mapname2mapid(script_getstr(st,5));
  12916. if(map_idx < 1) { //But we'll check anyways
  12917. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12918. return SCRIPT_CMD_FAILURE;
  12919. }
  12920. }
  12921. if(script_hasdata(st,3))
  12922. p_id = script_getnum(st,3);
  12923. else if(script_rid2sd(sd))
  12924. p_id = sd->status.party_id;
  12925. p = party_search(p_id);
  12926. if(p == NULL)
  12927. return SCRIPT_CMD_SUCCESS;
  12928. for (i = 0; i < MAX_PARTY; i++) {
  12929. struct map_session_data* pl_sd;
  12930. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12931. || (map_idx && pl_sd->bl.m != map_idx))
  12932. continue;
  12933. recovery_sub(pl_sd, revive);
  12934. }
  12935. break;
  12936. }
  12937. case 2:
  12938. {
  12939. struct guild* g;
  12940. //When no guild given, we use invoker guild
  12941. int g_id = 0, i;
  12942. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12943. map_idx = map_mapname2mapid(script_getstr(st,5));
  12944. if(map_idx < 1) { //But we'll check anyways
  12945. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12946. return SCRIPT_CMD_FAILURE;
  12947. }
  12948. }
  12949. if(script_hasdata(st,3))
  12950. g_id = script_getnum(st,3);
  12951. else if(script_rid2sd(sd))
  12952. g_id = sd->status.guild_id;
  12953. g = guild_search(g_id);
  12954. if(g == NULL)
  12955. return SCRIPT_CMD_SUCCESS;
  12956. for (i = 0; i < MAX_GUILD; i++) {
  12957. struct map_session_data* pl_sd;
  12958. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12959. || (map_idx && pl_sd->bl.m != map_idx))
  12960. continue;
  12961. recovery_sub(pl_sd, revive);
  12962. }
  12963. break;
  12964. }
  12965. case 3:
  12966. if(script_hasdata(st,3))
  12967. map_idx = map_mapname2mapid(script_getstr(st,3));
  12968. else if(script_rid2sd(sd))
  12969. map_idx = sd->bl.m;
  12970. if(map_idx < 1)
  12971. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12972. case 4:
  12973. {
  12974. struct s_mapiterator *iter;
  12975. if(script_hasdata(st,3) && script_isint(st, 3))
  12976. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12977. iter = mapit_getallusers();
  12978. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12979. if(type == 3 && sd->bl.m != map_idx)
  12980. continue;
  12981. recovery_sub(sd, revive);
  12982. }
  12983. mapit_free(iter);
  12984. break;
  12985. }
  12986. default:
  12987. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12988. script_pushint(st,-1);
  12989. return SCRIPT_CMD_FAILURE;
  12990. }
  12991. script_pushint(st,1); //Successfully executed without errors
  12992. return SCRIPT_CMD_SUCCESS;
  12993. }
  12994. /*==========================================
  12995. * Get your pet info: getpetinfo <type>{,<char_id>}
  12996. * n -> 0:pet_id 1:pet_class 2:pet_name
  12997. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12998. * 7:game id
  12999. *------------------------------------------*/
  13000. BUILDIN_FUNC(getpetinfo)
  13001. {
  13002. TBL_PC *sd;
  13003. TBL_PET *pd;
  13004. int type = script_getnum(st,2);
  13005. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  13006. if (type == 2)
  13007. script_pushconststr(st,"null");
  13008. else
  13009. script_pushint(st,0);
  13010. return SCRIPT_CMD_SUCCESS;
  13011. }
  13012. switch(type){
  13013. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13014. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13015. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13016. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13017. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13018. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13019. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13020. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13021. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13022. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13023. default:
  13024. script_pushint(st,0);
  13025. break;
  13026. }
  13027. return SCRIPT_CMD_SUCCESS;
  13028. }
  13029. /*==========================================
  13030. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13031. * type -> 0:hom_id 1:class 2:name
  13032. * 3:friendly 4:hungry, 5: rename flag.
  13033. * 6: level, 7: game id
  13034. *------------------------------------------*/
  13035. BUILDIN_FUNC(gethominfo)
  13036. {
  13037. TBL_PC *sd;
  13038. TBL_HOM *hd;
  13039. int type=script_getnum(st,2);
  13040. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13041. if (type == 2)
  13042. script_pushconststr(st,"null");
  13043. else
  13044. script_pushint(st,0);
  13045. return SCRIPT_CMD_SUCCESS;
  13046. }
  13047. switch(type){
  13048. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13049. case 1: script_pushint(st,hd->homunculus.class_); break;
  13050. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13051. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13052. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13053. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13054. case 6: script_pushint(st,hd->homunculus.level); break;
  13055. case 7: script_pushint(st,hd->bl.id); break;
  13056. default:
  13057. script_pushint(st,0);
  13058. break;
  13059. }
  13060. return SCRIPT_CMD_SUCCESS;
  13061. }
  13062. /// Retrieves information about character's mercenary
  13063. /// getmercinfo <type>[,<char id>];
  13064. BUILDIN_FUNC(getmercinfo)
  13065. {
  13066. int type;
  13067. struct map_session_data* sd;
  13068. struct mercenary_data* md;
  13069. if( !script_charid2sd(3,sd) ){
  13070. script_pushnil(st);
  13071. return SCRIPT_CMD_FAILURE;
  13072. }
  13073. type = script_getnum(st,2);
  13074. md = sd->md;
  13075. if( md == NULL ){
  13076. if( type == 2 )
  13077. script_pushconststr(st,"");
  13078. else
  13079. script_pushint(st,0);
  13080. return SCRIPT_CMD_SUCCESS;
  13081. }
  13082. switch( type )
  13083. {
  13084. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13085. case 1: script_pushint(st,md->mercenary.class_); break;
  13086. case 2: script_pushstrcopy(st,md->db->name); break;
  13087. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13088. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13089. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13090. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13091. case 7: script_pushint(st,md->db->lv); break;
  13092. case 8: script_pushint(st,md->bl.id); break;
  13093. default:
  13094. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13095. script_pushnil(st);
  13096. return SCRIPT_CMD_FAILURE;
  13097. }
  13098. return SCRIPT_CMD_SUCCESS;
  13099. }
  13100. /*==========================================
  13101. * Shows wether your inventory(and equips) contain
  13102. selected card or not.
  13103. checkequipedcard(4001);
  13104. *------------------------------------------*/
  13105. BUILDIN_FUNC(checkequipedcard)
  13106. {
  13107. TBL_PC *sd;
  13108. if(script_rid2sd(sd)){
  13109. int n,i,c=0;
  13110. c=script_getnum(st,2);
  13111. for(i=0;i<MAX_INVENTORY;i++){
  13112. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13113. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13114. continue;
  13115. for(n=0;n<sd->inventory_data[i]->slots;n++){
  13116. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13117. script_pushint(st,1);
  13118. return SCRIPT_CMD_SUCCESS;
  13119. }
  13120. }
  13121. }
  13122. }
  13123. }
  13124. script_pushint(st,0);
  13125. return SCRIPT_CMD_SUCCESS;
  13126. }
  13127. BUILDIN_FUNC(jump_zero)
  13128. {
  13129. int64 sel=script_getnum64(st,2);
  13130. if(!sel) {
  13131. int pos;
  13132. if( !data_islabel(script_getdata(st,3)) ){
  13133. ShowError("script: jump_zero: not label !\n");
  13134. st->state=END;
  13135. return SCRIPT_CMD_FAILURE;
  13136. }
  13137. pos=script_getnum(st,3);
  13138. st->pos=pos;
  13139. st->state=GOTO;
  13140. }
  13141. return SCRIPT_CMD_SUCCESS;
  13142. }
  13143. /*==========================================
  13144. * movenpc [MouseJstr]
  13145. *------------------------------------------*/
  13146. BUILDIN_FUNC(movenpc)
  13147. {
  13148. TBL_NPC *nd = NULL;
  13149. const char *npc;
  13150. int x,y;
  13151. npc = script_getstr(st,2);
  13152. x = script_getnum(st,3);
  13153. y = script_getnum(st,4);
  13154. if ((nd = npc_name2id(npc)) == NULL){
  13155. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13156. return -1;
  13157. }
  13158. if (script_hasdata(st,5))
  13159. nd->ud.dir = script_getnum(st,5) % 8;
  13160. npc_movenpc(nd, x, y);
  13161. return SCRIPT_CMD_SUCCESS;
  13162. }
  13163. /*==========================================
  13164. * message [MouseJstr]
  13165. *------------------------------------------*/
  13166. BUILDIN_FUNC(message)
  13167. {
  13168. const char *msg,*player;
  13169. TBL_PC *pl_sd = NULL;
  13170. player = script_getstr(st,2);
  13171. msg = script_getstr(st,3);
  13172. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13173. return SCRIPT_CMD_SUCCESS;
  13174. clif_displaymessage(pl_sd->fd, msg);
  13175. return SCRIPT_CMD_SUCCESS;
  13176. }
  13177. /**
  13178. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13179. * @param flag: Specify target
  13180. * BC_ALL - Broadcast message is sent server-wide.
  13181. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13182. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13183. * BC_SELF - Message is sent only to player attached.
  13184. */
  13185. BUILDIN_FUNC(npctalk)
  13186. {
  13187. struct npc_data* nd = NULL;
  13188. const char* str = script_getstr(st,2);
  13189. int color = 0xFFFFFF;
  13190. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13191. nd = npc_name2id(script_getstr(st, 3));
  13192. else
  13193. nd = (struct npc_data *)map_id2bl(st->oid);
  13194. if (script_hasdata(st, 5))
  13195. color = script_getnum(st, 5);
  13196. if (nd != NULL) {
  13197. send_target target = AREA;
  13198. char message[CHAT_SIZE_MAX];
  13199. if (script_hasdata(st, 4)) {
  13200. switch(script_getnum(st, 4)) {
  13201. case BC_ALL: target = ALL_CLIENT; break;
  13202. case BC_MAP: target = ALL_SAMEMAP; break;
  13203. case BC_SELF: target = SELF; break;
  13204. case BC_AREA:
  13205. default: target = AREA; break;
  13206. }
  13207. }
  13208. safesnprintf(message, sizeof(message), "%s", str);
  13209. if (target != SELF)
  13210. clif_messagecolor(&nd->bl, color, message, true, target);
  13211. else {
  13212. TBL_PC *sd = map_id2sd(st->rid);
  13213. if (sd == NULL)
  13214. return SCRIPT_CMD_FAILURE;
  13215. clif_messagecolor_target(&nd->bl, color, message, true, target, sd);
  13216. }
  13217. }
  13218. return SCRIPT_CMD_SUCCESS;
  13219. }
  13220. /**
  13221. * Sends a message to the waitingroom of the invoking NPC.
  13222. * chatmes "<message>"{,"<NPC name>"};
  13223. * @author Jey
  13224. */
  13225. BUILDIN_FUNC(chatmes)
  13226. {
  13227. struct npc_data* nd = NULL;
  13228. const char* str = script_getstr(st,2);
  13229. if (script_hasdata(st, 3))
  13230. nd = npc_name2id(script_getstr(st, 3));
  13231. else
  13232. nd = (struct npc_data *)map_id2bl(st->oid);
  13233. if (nd != NULL && nd->chat_id) {
  13234. char message[256];
  13235. safesnprintf(message, sizeof(message), "%s", str);
  13236. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13237. }
  13238. return SCRIPT_CMD_SUCCESS;
  13239. }
  13240. // change npc walkspeed [Valaris]
  13241. BUILDIN_FUNC(npcspeed)
  13242. {
  13243. struct npc_data* nd;
  13244. int speed;
  13245. speed = script_getnum(st,2);
  13246. nd =(struct npc_data *)map_id2bl(st->oid);
  13247. if( nd ) {
  13248. nd->speed = speed;
  13249. nd->ud.state.speed_changed = 1;
  13250. }
  13251. return SCRIPT_CMD_SUCCESS;
  13252. }
  13253. // make an npc walk to a position [Valaris]
  13254. BUILDIN_FUNC(npcwalkto)
  13255. {
  13256. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13257. int x=0,y=0;
  13258. x=script_getnum(st,2);
  13259. y=script_getnum(st,3);
  13260. if(nd) {
  13261. if (!nd->status.hp)
  13262. status_calc_npc(nd, SCO_FIRST);
  13263. else
  13264. status_calc_npc(nd, SCO_NONE);
  13265. unit_walktoxy(&nd->bl,x,y,0);
  13266. }
  13267. return SCRIPT_CMD_SUCCESS;
  13268. }
  13269. // stop an npc's movement [Valaris]
  13270. BUILDIN_FUNC(npcstop)
  13271. {
  13272. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13273. if(nd) {
  13274. unit_stop_walking(&nd->bl,1|4);
  13275. }
  13276. return SCRIPT_CMD_SUCCESS;
  13277. }
  13278. /**
  13279. * getlook(<type>{,<char_id>})
  13280. **/
  13281. BUILDIN_FUNC(getlook)
  13282. {
  13283. int type,val;
  13284. TBL_PC *sd;
  13285. if (!script_charid2sd(3,sd)) {
  13286. script_pushint(st,-1);
  13287. return SCRIPT_CMD_FAILURE;
  13288. }
  13289. type=script_getnum(st,2);
  13290. val=-1;
  13291. switch(type) {
  13292. // TODO: implement LOOK_BASE as stated in script doc
  13293. case LOOK_HAIR: val=sd->status.hair; break; //1
  13294. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13295. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13296. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13297. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13298. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13299. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13300. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13301. case LOOK_SHOES: break; //9
  13302. case LOOK_ROBE: val=sd->status.robe; break; //12
  13303. case LOOK_BODY2: val=sd->status.body; break; //13
  13304. }
  13305. script_pushint(st,val);
  13306. return SCRIPT_CMD_SUCCESS;
  13307. }
  13308. /**
  13309. * getsavepoint(<information type>{,<char_id>})
  13310. * @param type 0- map name, 1- x, 2- y
  13311. **/
  13312. BUILDIN_FUNC(getsavepoint)
  13313. {
  13314. TBL_PC* sd;
  13315. int type;
  13316. if (!script_charid2sd(3,sd)) {
  13317. script_pushint(st,0);
  13318. return SCRIPT_CMD_SUCCESS;
  13319. }
  13320. type = script_getnum(st,2);
  13321. switch(type) {
  13322. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13323. case 1: script_pushint(st,sd->status.save_point.x); break;
  13324. case 2: script_pushint(st,sd->status.save_point.y); break;
  13325. default:
  13326. script_pushint(st,0);
  13327. break;
  13328. }
  13329. return SCRIPT_CMD_SUCCESS;
  13330. }
  13331. /**
  13332. * Get position for BL objects.
  13333. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13334. * @param mapname: String variable for output map name
  13335. * @param x: Integer variable for output coord X
  13336. * @param y: Integer variable for output coord Y
  13337. * @param type: Type of object
  13338. * BL_PC - Character coord
  13339. * BL_NPC - NPC coord
  13340. * BL_PET - Pet coord
  13341. * BL_HOM - Homun coord
  13342. * BL_MER - Mercenary coord
  13343. * BL_ELEM - Elemental coord
  13344. * @param charname: Name object. If empty or "this" use the current object
  13345. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13346. */
  13347. BUILDIN_FUNC(getmapxy)
  13348. {
  13349. struct block_list *bl = NULL;
  13350. TBL_PC *sd=NULL;
  13351. int64 num;
  13352. const char *name;
  13353. char prefix;
  13354. int x,y,type = BL_PC;
  13355. char mapname[MAP_NAME_LENGTH];
  13356. if( !data_isreference(script_getdata(st,2)) ) {
  13357. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13358. script_pushint(st,-1);
  13359. return SCRIPT_CMD_FAILURE;
  13360. }
  13361. if( !data_isreference(script_getdata(st,3)) ) {
  13362. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13363. script_pushint(st,-1);
  13364. return SCRIPT_CMD_FAILURE;
  13365. }
  13366. if( !data_isreference(script_getdata(st,4)) ) {
  13367. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13368. script_pushint(st,-1);
  13369. return SCRIPT_CMD_FAILURE;
  13370. }
  13371. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13372. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13373. script_pushint(st,-1);
  13374. return SCRIPT_CMD_FAILURE;
  13375. }
  13376. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13377. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13378. script_pushint(st,-1);
  13379. return SCRIPT_CMD_FAILURE;
  13380. }
  13381. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13382. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13383. script_pushint(st,-1);
  13384. return SCRIPT_CMD_FAILURE;
  13385. }
  13386. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13387. if (script_hasdata(st, 5))
  13388. type = script_getnum(st, 5);
  13389. switch (type) {
  13390. case BL_PC: //Get Character Position
  13391. if ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd))
  13392. bl = &sd->bl;
  13393. break;
  13394. case BL_NPC: //Get NPC Position
  13395. if (script_hasdata(st, 6)) {
  13396. struct npc_data *nd;
  13397. if (script_isstring(st, 6))
  13398. nd = npc_name2id(script_getstr(st, 6));
  13399. else
  13400. nd = map_id2nd(script_getnum(st, 6));
  13401. if (nd)
  13402. bl = &nd->bl;
  13403. } else //In case the origin is not an NPC?
  13404. bl = map_id2bl(st->oid);
  13405. break;
  13406. case BL_PET: //Get Pet Position
  13407. if (sd->pd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13408. bl = &sd->pd->bl;
  13409. break;
  13410. case BL_HOM: //Get Homun Position
  13411. if (sd->hd && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13412. bl = &sd->hd->bl;
  13413. break;
  13414. case BL_MER: //Get Mercenary Position
  13415. if (sd->md && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13416. bl = &sd->md->bl;
  13417. break;
  13418. case BL_ELEM: //Get Elemental Position
  13419. if (sd->ed && ((script_isstring(st, 6) && script_nick2sd(6, sd)) || script_mapid2sd(6, sd)))
  13420. bl = &sd->ed->bl;
  13421. break;
  13422. default:
  13423. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13424. script_pushint(st,-1);
  13425. return SCRIPT_CMD_FAILURE;
  13426. }
  13427. if (!bl) { //No object found.
  13428. script_pushint(st,-1);
  13429. return SCRIPT_CMD_SUCCESS;
  13430. }
  13431. x= bl->x;
  13432. y= bl->y;
  13433. if (bl->m >= 0)
  13434. safestrncpy(mapname, map_getmapdata(bl->m)->name, MAP_NAME_LENGTH);
  13435. else
  13436. memset(mapname, '\0', sizeof(mapname));
  13437. //Set MapName$
  13438. num=st->stack->stack_data[st->start+2].u.num;
  13439. name=get_str(script_getvarid(num));
  13440. prefix=*name;
  13441. if(not_server_variable(prefix)){
  13442. if( !script_rid2sd(sd) ){
  13443. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13444. return SCRIPT_CMD_FAILURE;
  13445. }
  13446. }else
  13447. sd=NULL;
  13448. set_reg_str( st, sd, num, name, mapname, script_getref( st, 2 ) );
  13449. //Set MapX
  13450. num=st->stack->stack_data[st->start+3].u.num;
  13451. name=get_str(script_getvarid(num));
  13452. prefix=*name;
  13453. if(not_server_variable(prefix)){
  13454. if( !script_rid2sd(sd) ){
  13455. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13456. return SCRIPT_CMD_FAILURE;
  13457. }
  13458. }else
  13459. sd=NULL;
  13460. set_reg_num( st, sd, num, name, x, script_getref( st, 3 ) );
  13461. //Set MapY
  13462. num=st->stack->stack_data[st->start+4].u.num;
  13463. name=get_str(script_getvarid(num));
  13464. prefix=*name;
  13465. if(not_server_variable(prefix)){
  13466. if( !script_rid2sd(sd) ){
  13467. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13468. return SCRIPT_CMD_FAILURE;
  13469. }
  13470. }else
  13471. sd=NULL;
  13472. set_reg_num( st, sd, num, name, y, script_getref( st, 4 ) );
  13473. //Return Success value
  13474. script_pushint(st,0);
  13475. return SCRIPT_CMD_SUCCESS;
  13476. }
  13477. /// Returns the map name of given map ID.
  13478. ///
  13479. /// mapid2name <map ID>;
  13480. BUILDIN_FUNC(mapid2name)
  13481. {
  13482. uint16 m = script_getnum(st, 2);
  13483. if (m < 0) {
  13484. script_pushconststr(st, "");
  13485. return SCRIPT_CMD_FAILURE;
  13486. }
  13487. script_pushstrcopy(st, map_mapid2mapname(m));
  13488. return SCRIPT_CMD_SUCCESS;
  13489. }
  13490. /*==========================================
  13491. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13492. *------------------------------------------*/
  13493. BUILDIN_FUNC(logmes)
  13494. {
  13495. const char *str;
  13496. struct map_session_data* sd = map_id2sd(st->rid);
  13497. str = script_getstr(st,2);
  13498. if( sd ){
  13499. log_npc(sd,str);
  13500. }else{
  13501. struct npc_data* nd = map_id2nd(st->oid);
  13502. if( !nd ){
  13503. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  13504. return SCRIPT_CMD_FAILURE;
  13505. }
  13506. log_npc(nd,str);
  13507. }
  13508. return SCRIPT_CMD_SUCCESS;
  13509. }
  13510. BUILDIN_FUNC(summon)
  13511. {
  13512. int _class, timeout=0;
  13513. const char *str,*event="";
  13514. TBL_PC *sd;
  13515. struct mob_data *md;
  13516. t_tick tick = gettick();
  13517. if (!script_rid2sd(sd))
  13518. return SCRIPT_CMD_SUCCESS;
  13519. str =script_getstr(st,2);
  13520. _class=script_getnum(st,3);
  13521. if( script_hasdata(st,4) )
  13522. timeout=script_getnum(st,4);
  13523. if( script_hasdata(st,5) ){
  13524. event=script_getstr(st,5);
  13525. check_event(st, event);
  13526. }
  13527. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13528. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13529. if (md) {
  13530. md->master_id=sd->bl.id;
  13531. md->special_state.ai = AI_ATTACK;
  13532. if( md->deletetimer != INVALID_TIMER )
  13533. delete_timer(md->deletetimer, mob_timer_delete);
  13534. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13535. mob_spawn (md); //Now it is ready for spawning.
  13536. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  13537. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13538. }
  13539. script_pushint(st, md->bl.id);
  13540. return SCRIPT_CMD_SUCCESS;
  13541. }
  13542. /*==========================================
  13543. * Checks whether it is daytime/nighttime
  13544. *------------------------------------------*/
  13545. BUILDIN_FUNC(isnight)
  13546. {
  13547. script_pushint(st,(night_flag == 1));
  13548. return SCRIPT_CMD_SUCCESS;
  13549. }
  13550. BUILDIN_FUNC(isday)
  13551. {
  13552. script_pushint(st,(night_flag == 0));
  13553. return SCRIPT_CMD_SUCCESS;
  13554. }
  13555. /*================================================
  13556. * Check how many items/cards in the list are
  13557. * equipped - used for 2/15's cards patch [celest]
  13558. *------------------------------------------------*/
  13559. BUILDIN_FUNC(isequippedcnt)
  13560. {
  13561. TBL_PC *sd;
  13562. if (!script_rid2sd(sd)) {
  13563. script_pushint(st,0);
  13564. return SCRIPT_CMD_SUCCESS;
  13565. }
  13566. int ret = 0;
  13567. int total = script_lastdata(st);
  13568. std::vector<int32> list(total);
  13569. for (int i = 2; i <= total; ++i) {
  13570. int id = script_getnum(st,i);
  13571. if (id <= 0)
  13572. continue;
  13573. if (std::find(list.begin(), list.end(), id) != list.end())
  13574. continue;
  13575. list.push_back(id);
  13576. for (short j = 0; j < EQI_MAX; j++) {
  13577. short index = sd->equip_index[j];
  13578. if (index < 0)
  13579. continue;
  13580. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13581. continue;
  13582. if (!sd->inventory_data[index])
  13583. continue;
  13584. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13585. if (sd->inventory_data[index]->nameid == id)
  13586. ret += sd->inventory.u.items_inventory[index].amount;
  13587. } else { //Count cards.
  13588. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13589. continue; //No cards
  13590. for (short k = 0; k < sd->inventory_data[index]->slots; k++) {
  13591. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13592. ret++; //[Lupus]
  13593. }
  13594. }
  13595. }
  13596. }
  13597. script_pushint(st,ret);
  13598. return SCRIPT_CMD_SUCCESS;
  13599. }
  13600. /*================================================
  13601. * Check whether another card has been
  13602. * equipped - used for 2/15's cards patch [celest]
  13603. * -- Items checked cannot be reused in another
  13604. * card set to prevent exploits
  13605. *------------------------------------------------*/
  13606. BUILDIN_FUNC(isequipped)
  13607. {
  13608. TBL_PC *sd;
  13609. int i, id = 1;
  13610. int ret = -1;
  13611. //Original hash to reverse it when full check fails.
  13612. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13613. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13614. script_pushint(st,0);
  13615. return SCRIPT_CMD_SUCCESS;
  13616. }
  13617. setitem_hash = sd->bonus.setitem_hash;
  13618. setitem_hash2 = sd->bonus.setitem_hash2;
  13619. for (i=0; id!=0; i++) {
  13620. int flag = 0;
  13621. short j;
  13622. FETCH (i+2, id) else id = 0;
  13623. if (id <= 0)
  13624. continue;
  13625. for (j=0; j<EQI_MAX; j++) {
  13626. short index = sd->equip_index[j];
  13627. if(index < 0)
  13628. continue;
  13629. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13630. continue;
  13631. if(!sd->inventory_data[index])
  13632. continue;
  13633. if (itemdb_type(id) != IT_CARD) {
  13634. if (sd->inventory_data[index]->nameid != id)
  13635. continue;
  13636. flag = 1;
  13637. break;
  13638. } else { //Cards
  13639. short k;
  13640. if (sd->inventory_data[index]->slots == 0 ||
  13641. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13642. continue;
  13643. for (k = 0; k < sd->inventory_data[index]->slots; k++)
  13644. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13645. unsigned int hash = 0;
  13646. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13647. continue;
  13648. hash = 1<<((j<5?j:j-5)*4 + k);
  13649. // check if card is already used by another set
  13650. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13651. continue;
  13652. // We have found a match
  13653. flag = 1;
  13654. // Set hash so this card cannot be used by another
  13655. if (j<5)
  13656. sd->bonus.setitem_hash |= hash;
  13657. else
  13658. sd->bonus.setitem_hash2 |= hash;
  13659. break;
  13660. }
  13661. }
  13662. if (flag) break; //Card found
  13663. }
  13664. if (ret == -1)
  13665. ret = flag;
  13666. else
  13667. ret &= flag;
  13668. if (!ret) break;
  13669. }
  13670. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13671. sd->bonus.setitem_hash = setitem_hash;
  13672. sd->bonus.setitem_hash2 = setitem_hash2;
  13673. }
  13674. script_pushint(st,ret);
  13675. return SCRIPT_CMD_SUCCESS;
  13676. }
  13677. /*================================================
  13678. * Check how many given inserted cards in the CURRENT
  13679. * weapon - used for 2/15's cards patch [Lupus]
  13680. *------------------------------------------------*/
  13681. BUILDIN_FUNC(cardscnt)
  13682. {
  13683. TBL_PC *sd;
  13684. int i, k, id = 1;
  13685. int ret = 0;
  13686. int index;
  13687. if( !script_rid2sd(sd) ){
  13688. script_pushint(st,0);
  13689. return SCRIPT_CMD_SUCCESS;
  13690. }
  13691. for (i=0; id!=0; i++) {
  13692. FETCH (i+2, id) else id = 0;
  13693. if (id <= 0)
  13694. continue;
  13695. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.cpp [Lupus]
  13696. if(index < 0) continue;
  13697. if(!sd->inventory_data[index])
  13698. continue;
  13699. if(itemdb_type(id) != IT_CARD) {
  13700. if (sd->inventory_data[index]->nameid == id)
  13701. ret+= sd->inventory.u.items_inventory[index].amount;
  13702. } else {
  13703. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13704. continue;
  13705. for(k=0; k<sd->inventory_data[index]->slots; k++) {
  13706. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13707. ret++;
  13708. }
  13709. }
  13710. }
  13711. script_pushint(st,ret);
  13712. // script_pushint(st,current_equip_item_index);
  13713. return SCRIPT_CMD_SUCCESS;
  13714. }
  13715. /*=======================================================
  13716. * Returns the refined number of the current item, or an
  13717. * item with inventory index specified
  13718. *-------------------------------------------------------*/
  13719. BUILDIN_FUNC(getrefine)
  13720. {
  13721. TBL_PC *sd;
  13722. if (script_rid2sd(sd)){
  13723. if( current_equip_item_index == -1 ){
  13724. script_pushint(st, 0);
  13725. return SCRIPT_CMD_FAILURE;
  13726. }
  13727. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13728. }else
  13729. script_pushint(st,0);
  13730. return SCRIPT_CMD_SUCCESS;
  13731. }
  13732. /*=======================================================
  13733. * Day/Night controls
  13734. *-------------------------------------------------------*/
  13735. BUILDIN_FUNC(night)
  13736. {
  13737. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13738. return SCRIPT_CMD_SUCCESS;
  13739. }
  13740. BUILDIN_FUNC(day)
  13741. {
  13742. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13743. return SCRIPT_CMD_SUCCESS;
  13744. }
  13745. /**
  13746. * unequip <equipment slot>{,<char_id>};
  13747. * @author [Spectre]
  13748. **/
  13749. BUILDIN_FUNC(unequip) {
  13750. int pos;
  13751. TBL_PC *sd;
  13752. if (!script_charid2sd(3,sd))
  13753. return SCRIPT_CMD_FAILURE;
  13754. pos = script_getnum(st,2);
  13755. if (equip_index_check(pos)) {
  13756. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13757. if (i >= 0) {
  13758. pc_unequipitem(sd,i,1|2);
  13759. script_pushint(st, 1);
  13760. return SCRIPT_CMD_SUCCESS;
  13761. }
  13762. }
  13763. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13764. script_pushint(st, 0);
  13765. return SCRIPT_CMD_FAILURE;
  13766. }
  13767. /**
  13768. * equip <item id>{,<char_id>};
  13769. **/
  13770. BUILDIN_FUNC(equip) {
  13771. TBL_PC *sd;
  13772. struct item_data *item_data;
  13773. if (!script_charid2sd(3,sd))
  13774. return SCRIPT_CMD_FAILURE;
  13775. t_itemid nameid = script_getnum(st,2);
  13776. if ((item_data = itemdb_exists(nameid))) {
  13777. int i;
  13778. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13779. if (i < MAX_INVENTORY) {
  13780. pc_equipitem(sd,i,item_data->equip);
  13781. script_pushint(st,1);
  13782. return SCRIPT_CMD_SUCCESS;
  13783. }
  13784. }
  13785. ShowError("buildin_equip: Item %u cannot be equipped\n",nameid);
  13786. script_pushint(st,0);
  13787. return SCRIPT_CMD_FAILURE;
  13788. }
  13789. BUILDIN_FUNC(autoequip)
  13790. {
  13791. int flag;
  13792. struct item_data *item_data;
  13793. t_itemid nameid=script_getnum(st,2);
  13794. flag=script_getnum(st,3);
  13795. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13796. {
  13797. ShowError("buildin_autoequip: Invalid item '%u'.\n", nameid);
  13798. return SCRIPT_CMD_FAILURE;
  13799. }
  13800. if( !itemdb_isequip2(item_data) )
  13801. {
  13802. ShowError("buildin_autoequip: Item '%u' cannot be equipped.\n", nameid);
  13803. return SCRIPT_CMD_FAILURE;
  13804. }
  13805. item_data->flag.autoequip = flag>0?1:0;
  13806. return SCRIPT_CMD_SUCCESS;
  13807. }
  13808. /**
  13809. * Set the value of a given battle config
  13810. * setbattleflag "<battle flag>",<value>{,<reload>};
  13811. */
  13812. BUILDIN_FUNC(setbattleflag)
  13813. {
  13814. const char *flag, *value;
  13815. flag = script_getstr(st,2);
  13816. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13817. if (battle_set_value(flag, value) == 0)
  13818. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13819. else {
  13820. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13821. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  13822. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  13823. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  13824. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  13825. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  13826. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  13827. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  13828. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  13829. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  13830. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  13831. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  13832. };
  13833. uint8 i;
  13834. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  13835. if (!strcmpi(flag, expdrop_flags[i])) {
  13836. mob_reload();
  13837. break;
  13838. }
  13839. }
  13840. }
  13841. }
  13842. return SCRIPT_CMD_SUCCESS;
  13843. }
  13844. /**
  13845. * Get the value of a given battle config
  13846. * getbattleflag("<battle flag>")
  13847. */
  13848. BUILDIN_FUNC(getbattleflag)
  13849. {
  13850. const char *flag = script_getstr(st,2);
  13851. script_pushint(st,battle_get_value(flag));
  13852. return SCRIPT_CMD_SUCCESS;
  13853. }
  13854. //=======================================================
  13855. // strlen [Valaris]
  13856. //-------------------------------------------------------
  13857. BUILDIN_FUNC(getstrlen)
  13858. {
  13859. const char *str = script_getstr(st,2);
  13860. int len = (str) ? (int)strlen(str) : 0;
  13861. script_pushint(st,len);
  13862. return SCRIPT_CMD_SUCCESS;
  13863. }
  13864. //=======================================================
  13865. // isalpha [Valaris]
  13866. //-------------------------------------------------------
  13867. BUILDIN_FUNC(charisalpha)
  13868. {
  13869. const char *str=script_getstr(st,2);
  13870. int pos=script_getnum(st,3);
  13871. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13872. script_pushint(st,val);
  13873. return SCRIPT_CMD_SUCCESS;
  13874. }
  13875. //=======================================================
  13876. // charisupper <str>, <index>
  13877. //-------------------------------------------------------
  13878. BUILDIN_FUNC(charisupper)
  13879. {
  13880. const char *str = script_getstr(st,2);
  13881. int pos = script_getnum(st,3);
  13882. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13883. script_pushint(st,val);
  13884. return SCRIPT_CMD_SUCCESS;
  13885. }
  13886. //=======================================================
  13887. // charislower <str>, <index>
  13888. //-------------------------------------------------------
  13889. BUILDIN_FUNC(charislower)
  13890. {
  13891. const char *str = script_getstr(st,2);
  13892. int pos = script_getnum(st,3);
  13893. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13894. script_pushint(st,val);
  13895. return SCRIPT_CMD_SUCCESS;
  13896. }
  13897. //=======================================================
  13898. // charat <str>, <index>
  13899. //-------------------------------------------------------
  13900. BUILDIN_FUNC(charat) {
  13901. const char *str = script_getstr(st,2);
  13902. int pos = script_getnum(st,3);
  13903. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13904. char output[2];
  13905. output[0] = str[pos];
  13906. output[1] = '\0';
  13907. script_pushstrcopy(st, output);
  13908. } else
  13909. script_pushconststr(st, "");
  13910. return SCRIPT_CMD_SUCCESS;
  13911. }
  13912. //=======================================================
  13913. // setchar <string>, <char>, <index>
  13914. //-------------------------------------------------------
  13915. BUILDIN_FUNC(setchar)
  13916. {
  13917. const char *str = script_getstr(st,2);
  13918. const char *c = script_getstr(st,3);
  13919. int index = script_getnum(st,4);
  13920. char *output = aStrdup(str);
  13921. if(index >= 0 && index < strlen(output))
  13922. output[index] = *c;
  13923. script_pushstr(st, output);
  13924. return SCRIPT_CMD_SUCCESS;
  13925. }
  13926. //=======================================================
  13927. // insertchar <string>, <char>, <index>
  13928. //-------------------------------------------------------
  13929. BUILDIN_FUNC(insertchar)
  13930. {
  13931. const char *str = script_getstr(st,2);
  13932. const char *c = script_getstr(st,3);
  13933. int index = script_getnum(st,4);
  13934. char *output;
  13935. size_t len = strlen(str);
  13936. if(index < 0)
  13937. index = 0;
  13938. else if(index > len)
  13939. index = len;
  13940. output = (char*)aMalloc(len + 2);
  13941. memcpy(output, str, index);
  13942. output[index] = c[0];
  13943. memcpy(&output[index+1], &str[index], len - index);
  13944. output[len+1] = '\0';
  13945. script_pushstr(st, output);
  13946. return SCRIPT_CMD_SUCCESS;
  13947. }
  13948. //=======================================================
  13949. // delchar <string>, <index>
  13950. //-------------------------------------------------------
  13951. BUILDIN_FUNC(delchar)
  13952. {
  13953. const char *str = script_getstr(st,2);
  13954. int index = script_getnum(st,3);
  13955. char *output;
  13956. size_t len = strlen(str);
  13957. if(index < 0 || index > len) {
  13958. //return original
  13959. output = aStrdup(str);
  13960. script_pushstr(st, output);
  13961. return SCRIPT_CMD_SUCCESS;
  13962. }
  13963. output = (char*)aMalloc(len);
  13964. memcpy(output, str, index);
  13965. memcpy(&output[index], &str[index+1], len - index);
  13966. script_pushstr(st, output);
  13967. return SCRIPT_CMD_SUCCESS;
  13968. }
  13969. //=======================================================
  13970. // strtoupper <str>
  13971. //-------------------------------------------------------
  13972. BUILDIN_FUNC(strtoupper)
  13973. {
  13974. const char *str = script_getstr(st,2);
  13975. char *output = aStrdup(str);
  13976. char *cursor = output;
  13977. while (*cursor != '\0') {
  13978. *cursor = TOUPPER(*cursor);
  13979. cursor++;
  13980. }
  13981. script_pushstr(st, output);
  13982. return SCRIPT_CMD_SUCCESS;
  13983. }
  13984. //=======================================================
  13985. // strtolower <str>
  13986. //-------------------------------------------------------
  13987. BUILDIN_FUNC(strtolower)
  13988. {
  13989. const char *str = script_getstr(st,2);
  13990. char *output = aStrdup(str);
  13991. char *cursor = output;
  13992. while (*cursor != '\0') {
  13993. *cursor = TOLOWER(*cursor);
  13994. cursor++;
  13995. }
  13996. script_pushstr(st, output);
  13997. return SCRIPT_CMD_SUCCESS;
  13998. }
  13999. //=======================================================
  14000. // substr <str>, <start>, <end>
  14001. //-------------------------------------------------------
  14002. BUILDIN_FUNC(substr)
  14003. {
  14004. const char *str = script_getstr(st,2);
  14005. char *output;
  14006. int start = script_getnum(st,3);
  14007. int end = script_getnum(st,4);
  14008. int len = 0;
  14009. if(start >= 0 && end < strlen(str) && start <= end) {
  14010. len = end - start + 1;
  14011. output = (char*)aMalloc(len + 1);
  14012. memcpy(output, &str[start], len);
  14013. } else
  14014. output = (char*)aMalloc(1);
  14015. output[len] = '\0';
  14016. script_pushstr(st, output);
  14017. return SCRIPT_CMD_SUCCESS;
  14018. }
  14019. //=======================================================
  14020. // explode <dest_string_array>, <str>, <delimiter>
  14021. // Note: delimiter is limited to 1 char
  14022. //-------------------------------------------------------
  14023. BUILDIN_FUNC(explode)
  14024. {
  14025. struct script_data* data = script_getdata(st, 2);
  14026. const char *str = script_getstr(st,3);
  14027. const char delimiter = script_getstr(st, 4)[0];
  14028. int32 id;
  14029. size_t len = strlen(str);
  14030. int i = 0, j = 0;
  14031. int start;
  14032. char *temp;
  14033. const char* name;
  14034. TBL_PC* sd = NULL;
  14035. temp = (char*)aMalloc(len + 1);
  14036. if( !data_isreference(data) ) {
  14037. ShowError("script:explode: not a variable\n");
  14038. script_reportdata(data);
  14039. st->state = END;
  14040. return SCRIPT_CMD_FAILURE;// not a variable
  14041. }
  14042. id = reference_getid(data);
  14043. start = reference_getindex(data);
  14044. name = reference_getname(data);
  14045. if( !is_string_variable(name) ) {
  14046. ShowError("script:explode: not string array\n");
  14047. script_reportdata(data);
  14048. st->state = END;
  14049. return SCRIPT_CMD_FAILURE;// data type mismatch
  14050. }
  14051. if( not_server_variable(*name) ) {
  14052. if( !script_rid2sd(sd) )
  14053. return SCRIPT_CMD_SUCCESS;// no player attached
  14054. }
  14055. while(str[i] != '\0') {
  14056. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14057. temp[j] = '\0';
  14058. set_reg_str( st, sd, reference_uid( id, start++ ), name, temp, reference_getref( data ) );
  14059. j = 0;
  14060. ++i;
  14061. } else {
  14062. temp[j++] = str[i++];
  14063. }
  14064. }
  14065. //set last string
  14066. temp[j] = '\0';
  14067. set_reg_str( st, sd, reference_uid( id, start ), name, temp, reference_getref( data ) );
  14068. aFree(temp);
  14069. return SCRIPT_CMD_SUCCESS;
  14070. }
  14071. //=======================================================
  14072. // implode <string_array>
  14073. // implode <string_array>, <glue>
  14074. //-------------------------------------------------------
  14075. BUILDIN_FUNC(implode)
  14076. {
  14077. struct script_data* data = script_getdata(st, 2);
  14078. const char *name;
  14079. uint32 glue_len = 0, array_size, id;
  14080. char *output;
  14081. TBL_PC* sd = NULL;
  14082. if( !data_isreference(data) ) {
  14083. ShowError("script:implode: not a variable\n");
  14084. script_reportdata(data);
  14085. st->state = END;
  14086. return SCRIPT_CMD_FAILURE;// not a variable
  14087. }
  14088. id = reference_getid(data);
  14089. name = reference_getname(data);
  14090. if( !is_string_variable(name) ) {
  14091. ShowError("script:implode: not string array\n");
  14092. script_reportdata(data);
  14093. st->state = END;
  14094. return SCRIPT_CMD_FAILURE;// data type mismatch
  14095. }
  14096. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14097. return SCRIPT_CMD_SUCCESS;// no player attached
  14098. }
  14099. //count chars
  14100. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  14101. if(array_size == -1) { //empty array check (AmsTaff)
  14102. ShowWarning("script:implode: array length = 0\n");
  14103. output = (char*)aMalloc(sizeof(char)*5);
  14104. sprintf(output,"%s","NULL");
  14105. } else {
  14106. const char *glue = NULL, *temp;
  14107. size_t len = 0;
  14108. int i, k = 0;
  14109. for(i = 0; i <= array_size; ++i) {
  14110. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14111. len += strlen(temp);
  14112. // Remove stack entry from get_val2_str
  14113. script_removetop( st, -1, 0 );
  14114. }
  14115. //allocate mem
  14116. if( script_hasdata(st,3) ) {
  14117. glue = script_getstr(st,3);
  14118. glue_len = strlen(glue);
  14119. len += glue_len * (array_size);
  14120. }
  14121. output = (char*)aMalloc(len + 1);
  14122. //build output
  14123. for(i = 0; i < array_size; ++i) {
  14124. temp = get_val2_str( st, reference_uid( id, i ), reference_getref( data ) );
  14125. len = strlen(temp);
  14126. memcpy(&output[k], temp, len);
  14127. k += len;
  14128. // Remove stack entry from get_val2_str
  14129. script_removetop( st, -1, 0 );
  14130. if(glue_len != 0) {
  14131. memcpy(&output[k], glue, glue_len);
  14132. k += glue_len;
  14133. }
  14134. }
  14135. temp = get_val2_str( st, reference_uid( id, array_size ), reference_getref( data ) );
  14136. len = strlen(temp);
  14137. memcpy(&output[k], temp, len);
  14138. k += len;
  14139. output[k] = '\0';
  14140. // Remove stack entry from get_val2_str
  14141. script_removetop( st, -1, 0 );
  14142. }
  14143. script_pushstr(st, output);
  14144. return SCRIPT_CMD_SUCCESS;
  14145. }
  14146. //=======================================================
  14147. // sprintf(<format>, ...);
  14148. // Implements C sprintf, except format %n. The resulting string is
  14149. // returned, instead of being saved in variable by reference.
  14150. //-------------------------------------------------------
  14151. BUILDIN_FUNC(sprintf)
  14152. {
  14153. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14154. const char* format;
  14155. char* p;
  14156. char* q;
  14157. char* buf = NULL;
  14158. char* buf2 = NULL;
  14159. struct script_data* data;
  14160. StringBuf final_buf;
  14161. // Fetch init data
  14162. format = script_getstr(st, 2);
  14163. argc = script_lastdata(st)-2;
  14164. len = strlen(format);
  14165. // Skip parsing, where no parsing is required.
  14166. if(len == 0) {
  14167. script_pushconststr(st,"");
  14168. return SCRIPT_CMD_SUCCESS;
  14169. }
  14170. // Pessimistic alloc
  14171. CREATE(buf, char, len+1);
  14172. // Need not be parsed, just solve stuff like %%.
  14173. if(argc == 0) {
  14174. memcpy(buf,format,len+1);
  14175. script_pushstrcopy(st, buf);
  14176. aFree(buf);
  14177. return SCRIPT_CMD_SUCCESS;
  14178. }
  14179. safestrncpy(buf, format, len+1);
  14180. // Issue sprintf for each parameter
  14181. StringBuf_Init(&final_buf);
  14182. q = buf;
  14183. while((p = strchr(q, '%')) != NULL) {
  14184. if(p != q) {
  14185. len = p - q + 1;
  14186. if(buf2_len < len) {
  14187. RECREATE(buf2, char, len);
  14188. buf2_len = len;
  14189. }
  14190. safestrncpy(buf2, q, len);
  14191. StringBuf_AppendStr(&final_buf, buf2);
  14192. q = p;
  14193. }
  14194. p = q + 1;
  14195. if(*p == '%') { // %%
  14196. StringBuf_AppendStr(&final_buf, "%");
  14197. q+=2;
  14198. continue;
  14199. }
  14200. if(*p == 'n') { // %n
  14201. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14202. script_reportsrc(st);
  14203. q+=2;
  14204. continue;
  14205. }
  14206. if(arg >= argc) {
  14207. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14208. if(buf) aFree(buf);
  14209. if(buf2) aFree(buf2);
  14210. StringBuf_Destroy(&final_buf);
  14211. script_pushconststr(st,"");
  14212. return SCRIPT_CMD_FAILURE;
  14213. }
  14214. if((p = strchr(q+1, '%')) == NULL)
  14215. p = strchr(q, 0); // EOS
  14216. len = p - q + 1;
  14217. if(buf2_len < len) {
  14218. RECREATE(buf2, char, len);
  14219. buf2_len = len;
  14220. }
  14221. safestrncpy(buf2, q, len);
  14222. q = p;
  14223. // Note: This assumes the passed value being the correct
  14224. // type to the current format specifier. If not, the server
  14225. // probably crashes or returns anything else, than expected,
  14226. // but it would behave in normal code the same way so it's
  14227. // the scripter's responsibility.
  14228. data = script_getdata(st, arg+3);
  14229. if(data_isstring(data)) // String
  14230. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14231. else if(data_isint(data)) // Number
  14232. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14233. else if(data_isreference(data)) { // Variable
  14234. char* name = reference_getname(data);
  14235. if(name[strlen(name)-1]=='$') // var Str
  14236. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14237. else // var Int
  14238. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14239. } else { // Unsupported type
  14240. ShowError("buildin_sprintf: Unknown argument type!\n");
  14241. if(buf) aFree(buf);
  14242. if(buf2) aFree(buf2);
  14243. StringBuf_Destroy(&final_buf);
  14244. script_pushconststr(st,"");
  14245. return SCRIPT_CMD_FAILURE;
  14246. }
  14247. arg++;
  14248. }
  14249. // Append anything left
  14250. if(*q)
  14251. StringBuf_AppendStr(&final_buf, q);
  14252. // Passed more, than needed
  14253. if(arg < argc) {
  14254. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14255. script_reportsrc(st);
  14256. }
  14257. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14258. if(buf) aFree(buf);
  14259. if(buf2) aFree(buf2);
  14260. StringBuf_Destroy(&final_buf);
  14261. return SCRIPT_CMD_SUCCESS;
  14262. }
  14263. //=======================================================
  14264. // sscanf(<str>, <format>, ...);
  14265. // Implements C sscanf.
  14266. //-------------------------------------------------------
  14267. BUILDIN_FUNC(sscanf){
  14268. unsigned int argc, arg = 0, len;
  14269. struct script_data* data;
  14270. struct map_session_data* sd = NULL;
  14271. const char* str;
  14272. const char* format;
  14273. const char* p;
  14274. const char* q;
  14275. char* buf = NULL;
  14276. char* buf_p;
  14277. char* ref_str = NULL;
  14278. int ref_int;
  14279. // Get data
  14280. str = script_getstr(st, 2);
  14281. format = script_getstr(st, 3);
  14282. argc = script_lastdata(st)-3;
  14283. len = strlen(format);
  14284. if (len != 0 && strlen(str) == 0) {
  14285. // If the source string is empty but the format string is not, we return -1
  14286. // according to the C specs. (if the format string is also empty, we shall
  14287. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14288. script_pushint(st, -1);
  14289. return SCRIPT_CMD_SUCCESS;
  14290. }
  14291. CREATE(buf, char, len*2+1);
  14292. // Issue sscanf for each parameter
  14293. *buf = 0;
  14294. q = format;
  14295. while((p = strchr(q, '%'))){
  14296. if(p!=q){
  14297. strncat(buf, q, (size_t)(p-q));
  14298. q = p;
  14299. }
  14300. p = q+1;
  14301. if(*p=='*' || *p=='%'){ // Skip
  14302. strncat(buf, q, 2);
  14303. q+=2;
  14304. continue;
  14305. }
  14306. if(arg>=argc){
  14307. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14308. script_pushint(st, -1);
  14309. if(buf) aFree(buf);
  14310. if(ref_str) aFree(ref_str);
  14311. return SCRIPT_CMD_FAILURE;
  14312. }
  14313. if((p = strchr(q+1, '%'))==NULL){
  14314. p = strchr(q, 0); // EOS
  14315. }
  14316. len = p-q;
  14317. strncat(buf, q, len);
  14318. q = p;
  14319. // Validate output
  14320. data = script_getdata(st, arg+4);
  14321. if(!data_isreference(data) || !reference_tovariable(data)){
  14322. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14323. script_pushint(st, -1);
  14324. if(buf) aFree(buf);
  14325. if(ref_str) aFree(ref_str);
  14326. return SCRIPT_CMD_FAILURE;
  14327. }
  14328. buf_p = reference_getname(data);
  14329. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14330. script_pushint(st, -1);
  14331. if(buf) aFree(buf);
  14332. if(ref_str) aFree(ref_str);
  14333. return SCRIPT_CMD_SUCCESS;
  14334. }
  14335. // Save value if any
  14336. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14337. if(ref_str==NULL){
  14338. CREATE(ref_str, char, strlen(str)+1);
  14339. }
  14340. if(sscanf(str, buf, ref_str)==0){
  14341. break;
  14342. }
  14343. set_reg_str( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_str, reference_getref( data ) );
  14344. } else { // Number
  14345. if(sscanf(str, buf, &ref_int)==0){
  14346. break;
  14347. }
  14348. set_reg_num( st, sd, reference_uid( reference_getid( data ), reference_getindex( data ) ), buf_p, ref_int, reference_getref( data ) );
  14349. }
  14350. arg++;
  14351. // Disable used format (%... -> %*...)
  14352. buf_p = strchr(buf, 0);
  14353. memmove(buf_p-len+2, buf_p-len+1, len);
  14354. *(buf_p-len+1) = '*';
  14355. }
  14356. script_pushint(st, arg);
  14357. if(buf) aFree(buf);
  14358. if(ref_str) aFree(ref_str);
  14359. return SCRIPT_CMD_SUCCESS;
  14360. }
  14361. //=======================================================
  14362. // strpos(<haystack>, <needle>)
  14363. // strpos(<haystack>, <needle>, <offset>)
  14364. //
  14365. // Implements PHP style strpos. Adapted from code from
  14366. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14367. //-------------------------------------------------------
  14368. BUILDIN_FUNC(strpos) {
  14369. const char *haystack = script_getstr(st,2);
  14370. const char *needle = script_getstr(st,3);
  14371. int i;
  14372. size_t len;
  14373. if( script_hasdata(st,4) )
  14374. i = script_getnum(st,4);
  14375. else
  14376. i = 0;
  14377. if (needle[0] == '\0') {
  14378. script_pushint(st, -1);
  14379. return SCRIPT_CMD_SUCCESS;
  14380. }
  14381. len = strlen(haystack);
  14382. for ( ; i < len; ++i ) {
  14383. if ( haystack[i] == *needle ) {
  14384. // matched starting char -- loop through remaining chars
  14385. const char *h, *n;
  14386. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14387. if ( *h != *n ) {
  14388. break;
  14389. }
  14390. }
  14391. if ( !*n ) { // matched all of 'needle' to null termination
  14392. script_pushint(st, i);
  14393. return SCRIPT_CMD_SUCCESS;
  14394. }
  14395. }
  14396. }
  14397. script_pushint(st, -1);
  14398. return SCRIPT_CMD_SUCCESS;
  14399. }
  14400. //===============================================================
  14401. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14402. //
  14403. // Note: Finds all instances of <search> in <input> and replaces
  14404. // with <replace>. If specified will only replace as many
  14405. // instances as specified in <count>. By default will be case
  14406. // sensitive.
  14407. //---------------------------------------------------------------
  14408. BUILDIN_FUNC(replacestr)
  14409. {
  14410. const char *input = script_getstr(st, 2);
  14411. const char *find = script_getstr(st, 3);
  14412. const char *replace = script_getstr(st, 4);
  14413. size_t inputlen = strlen(input);
  14414. size_t findlen = strlen(find);
  14415. struct StringBuf output;
  14416. bool usecase = true;
  14417. int count = 0;
  14418. int numFinds = 0;
  14419. int i = 0, f = 0;
  14420. if(findlen == 0) {
  14421. ShowError("script:replacestr: Invalid search length.\n");
  14422. st->state = END;
  14423. return SCRIPT_CMD_FAILURE;
  14424. }
  14425. if(script_hasdata(st, 5)) {
  14426. if( script_isint(st, 5) )
  14427. usecase = script_getnum(st, 5) != 0;
  14428. else {
  14429. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14430. st->state = END;
  14431. return SCRIPT_CMD_FAILURE;
  14432. }
  14433. }
  14434. if(script_hasdata(st, 6)) {
  14435. count = script_getnum(st, 6);
  14436. if(count == 0) {
  14437. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14438. st->state = END;
  14439. return SCRIPT_CMD_FAILURE;
  14440. }
  14441. }
  14442. StringBuf_Init(&output);
  14443. for(; i < inputlen; i++) {
  14444. if(count && count == numFinds) { //found enough, stop looking
  14445. break;
  14446. }
  14447. for(f = 0; f <= findlen; f++) {
  14448. if(f == findlen) { //complete match
  14449. numFinds++;
  14450. StringBuf_AppendStr(&output, replace);
  14451. i += findlen - 1;
  14452. break;
  14453. } else {
  14454. if(usecase) {
  14455. if((i + f) > inputlen || input[i + f] != find[f]) {
  14456. StringBuf_Printf(&output, "%c", input[i]);
  14457. break;
  14458. }
  14459. } else {
  14460. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14461. StringBuf_Printf(&output, "%c", input[i]);
  14462. break;
  14463. }
  14464. }
  14465. }
  14466. }
  14467. }
  14468. //append excess after enough found
  14469. if(i < inputlen)
  14470. StringBuf_AppendStr(&output, &(input[i]));
  14471. script_pushstrcopy(st, StringBuf_Value(&output));
  14472. StringBuf_Destroy(&output);
  14473. return SCRIPT_CMD_SUCCESS;
  14474. }
  14475. //========================================================
  14476. // countstr <input>, <search>{, <usecase>}
  14477. //
  14478. // Note: Counts the number of times <search> occurs in
  14479. // <input>. By default will be case sensitive.
  14480. //--------------------------------------------------------
  14481. BUILDIN_FUNC(countstr)
  14482. {
  14483. const char *input = script_getstr(st, 2);
  14484. const char *find = script_getstr(st, 3);
  14485. size_t inputlen = strlen(input);
  14486. size_t findlen = strlen(find);
  14487. bool usecase = true;
  14488. int numFinds = 0;
  14489. int i = 0, f = 0;
  14490. if(findlen == 0) {
  14491. ShowError("script:countstr: Invalid search length.\n");
  14492. st->state = END;
  14493. return SCRIPT_CMD_FAILURE;
  14494. }
  14495. if(script_hasdata(st, 4)) {
  14496. if( script_isint(st, 4) )
  14497. usecase = script_getnum(st, 4) != 0;
  14498. else {
  14499. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14500. st->state = END;
  14501. return SCRIPT_CMD_FAILURE;
  14502. }
  14503. }
  14504. for(; i < inputlen; i++) {
  14505. for(f = 0; f <= findlen; f++) {
  14506. if(f == findlen) { //complete match
  14507. numFinds++;
  14508. i += findlen - 1;
  14509. break;
  14510. } else {
  14511. if(usecase) {
  14512. if((i + f) > inputlen || input[i + f] != find[f]) {
  14513. break;
  14514. }
  14515. } else {
  14516. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14517. break;
  14518. }
  14519. }
  14520. }
  14521. }
  14522. }
  14523. script_pushint(st, numFinds);
  14524. return SCRIPT_CMD_SUCCESS;
  14525. }
  14526. /// Changes the display name and/or display class of the npc.
  14527. /// Returns 0 is successful, 1 if the npc does not exist.
  14528. ///
  14529. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14530. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14531. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14532. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14533. BUILDIN_FUNC(setnpcdisplay)
  14534. {
  14535. const char* name;
  14536. const char* newname = NULL;
  14537. int class_ = JT_FAKENPC, size = -1;
  14538. struct npc_data* nd;
  14539. name = script_getstr(st,2);
  14540. if( script_hasdata(st,4) )
  14541. class_ = script_getnum(st,4);
  14542. if( script_hasdata(st,5) )
  14543. size = script_getnum(st,5);
  14544. if( script_isstring(st, 3) )
  14545. newname = script_getstr(st, 3);
  14546. else
  14547. class_ = script_getnum(st, 3);
  14548. nd = npc_name2id(name);
  14549. if( nd == NULL )
  14550. {// not found
  14551. script_pushint(st,1);
  14552. return SCRIPT_CMD_SUCCESS;
  14553. }
  14554. // update npc
  14555. if( newname )
  14556. npc_setdisplayname(nd, newname);
  14557. if( size != -1 && size != (int)nd->size )
  14558. nd->size = size;
  14559. else
  14560. size = -1;
  14561. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  14562. npc_setclass(nd, class_);
  14563. else if( size != -1 )
  14564. unit_refresh(&nd->bl); // Required to update the visual size
  14565. script_pushint(st,0);
  14566. return SCRIPT_CMD_SUCCESS;
  14567. }
  14568. BUILDIN_FUNC(atoi)
  14569. {
  14570. const char *value;
  14571. value = script_getstr(st,2);
  14572. script_pushint(st,atoi(value));
  14573. return SCRIPT_CMD_SUCCESS;
  14574. }
  14575. BUILDIN_FUNC(axtoi)
  14576. {
  14577. const char *hex = script_getstr(st,2);
  14578. long value = strtol(hex, NULL, 16);
  14579. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14580. value = cap_value(value, INT_MIN, INT_MAX);
  14581. #endif
  14582. script_pushint(st, (int)value);
  14583. return SCRIPT_CMD_SUCCESS;
  14584. }
  14585. BUILDIN_FUNC(strtol)
  14586. {
  14587. const char *string = script_getstr(st, 2);
  14588. int base = script_getnum(st, 3);
  14589. long value = strtol(string, NULL, base);
  14590. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14591. value = cap_value(value, INT_MIN, INT_MAX);
  14592. #endif
  14593. script_pushint(st, (int)value);
  14594. return SCRIPT_CMD_SUCCESS;
  14595. }
  14596. // case-insensitive substring search [lordalfa]
  14597. BUILDIN_FUNC(compare)
  14598. {
  14599. const char *message;
  14600. const char *cmpstring;
  14601. message = script_getstr(st,2);
  14602. cmpstring = script_getstr(st,3);
  14603. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14604. return SCRIPT_CMD_SUCCESS;
  14605. }
  14606. BUILDIN_FUNC(strcmp)
  14607. {
  14608. const char *str1;
  14609. const char *str2;
  14610. str1 = script_getstr(st,2);
  14611. str2 = script_getstr(st,3);
  14612. script_pushint(st,strcmp(str1, str2));
  14613. return SCRIPT_CMD_SUCCESS;
  14614. }
  14615. // [zBuffer] List of mathematics commands --->
  14616. BUILDIN_FUNC(sqrt)
  14617. {
  14618. double i, a;
  14619. i = script_getnum(st,2);
  14620. a = sqrt(i);
  14621. script_pushint(st,(int)a);
  14622. return SCRIPT_CMD_SUCCESS;
  14623. }
  14624. BUILDIN_FUNC(pow)
  14625. {
  14626. double i, a, b;
  14627. a = script_getnum(st,2);
  14628. b = script_getnum(st,3);
  14629. i = pow(a,b);
  14630. script_pushint(st,(int)i);
  14631. return SCRIPT_CMD_SUCCESS;
  14632. }
  14633. BUILDIN_FUNC(distance)
  14634. {
  14635. int x0, y0, x1, y1;
  14636. x0 = script_getnum(st,2);
  14637. y0 = script_getnum(st,3);
  14638. x1 = script_getnum(st,4);
  14639. y1 = script_getnum(st,5);
  14640. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14641. return SCRIPT_CMD_SUCCESS;
  14642. }
  14643. // <--- [zBuffer] List of mathematics commands
  14644. BUILDIN_FUNC(md5)
  14645. {
  14646. const char *tmpstr;
  14647. char *md5str;
  14648. tmpstr = script_getstr(st,2);
  14649. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14650. MD5_String(tmpstr, md5str);
  14651. script_pushstr(st, md5str);
  14652. return SCRIPT_CMD_SUCCESS;
  14653. }
  14654. // [zBuffer] List of dynamic var commands --->
  14655. /**
  14656. * setd "<variable name>",<value>{,<char_id>};
  14657. **/
  14658. BUILDIN_FUNC(setd)
  14659. {
  14660. TBL_PC *sd = NULL;
  14661. char varname[100];
  14662. const char *buffer;
  14663. int elem;
  14664. buffer = script_getstr(st, 2);
  14665. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14666. elem = 0;
  14667. if( not_server_variable(*varname) ) {
  14668. if (!script_charid2sd(4,sd))
  14669. return SCRIPT_CMD_FAILURE;
  14670. }
  14671. if( is_string_variable(varname) ) {
  14672. setd_sub_str( st, sd, varname, elem, script_getstr( st, 3 ), NULL );
  14673. } else {
  14674. setd_sub_num( st, sd, varname, elem, script_getnum64( st, 3 ), NULL );
  14675. }
  14676. return SCRIPT_CMD_SUCCESS;
  14677. }
  14678. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14679. {
  14680. int i, j;
  14681. TBL_PC* sd = NULL;
  14682. const char* query;
  14683. struct script_data* data;
  14684. const char* name;
  14685. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14686. int num_vars;
  14687. int num_cols;
  14688. // check target variables
  14689. for( i = 3; script_hasdata(st,i); ++i ) {
  14690. data = script_getdata(st, i);
  14691. if( data_isreference(data) ) { // it's a variable
  14692. name = reference_getname(data);
  14693. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14694. if( !script_rid2sd(sd) ) { // no player attached
  14695. script_reportdata(data);
  14696. st->state = END;
  14697. return SCRIPT_CMD_FAILURE;
  14698. }
  14699. }
  14700. } else {
  14701. ShowError("script:query_sql: not a variable\n");
  14702. script_reportdata(data);
  14703. st->state = END;
  14704. return SCRIPT_CMD_FAILURE;
  14705. }
  14706. }
  14707. num_vars = i - 3;
  14708. // Execute the query
  14709. query = script_getstr(st,2);
  14710. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14711. Sql_ShowDebug(handle);
  14712. script_pushint(st, -1);
  14713. return SCRIPT_CMD_FAILURE;
  14714. }
  14715. if( Sql_NumRows(handle) == 0 ) { // No data received
  14716. Sql_FreeResult(handle);
  14717. script_pushint(st, 0);
  14718. return SCRIPT_CMD_SUCCESS;
  14719. }
  14720. // Count the number of columns to store
  14721. num_cols = Sql_NumColumns(handle);
  14722. if( num_vars < num_cols ) {
  14723. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14724. script_reportsrc(st);
  14725. } else if( num_vars > num_cols ) {
  14726. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14727. script_reportsrc(st);
  14728. }
  14729. // Store data
  14730. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14731. for( j = 0; j < num_vars; ++j ) {
  14732. char* str = NULL;
  14733. if( j < num_cols )
  14734. Sql_GetData(handle, j, &str, NULL);
  14735. data = script_getdata(st, j+3);
  14736. name = reference_getname(data);
  14737. if( is_string_variable(name) )
  14738. setd_sub_str( st, sd, name, i, str ? str : "", reference_getref( data ) );
  14739. else
  14740. setd_sub_num( st, sd, name, i, str ? strtoll( str, nullptr, 10 ) : 0, reference_getref( data ) );
  14741. }
  14742. }
  14743. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14744. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14745. script_reportsrc(st);
  14746. }
  14747. // Free data
  14748. Sql_FreeResult(handle);
  14749. script_pushint(st, i);
  14750. return SCRIPT_CMD_SUCCESS;
  14751. }
  14752. BUILDIN_FUNC(query_sql) {
  14753. return buildin_query_sql_sub(st, qsmysql_handle);
  14754. }
  14755. BUILDIN_FUNC(query_logsql) {
  14756. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14757. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14758. script_pushint(st,-1);
  14759. return SCRIPT_CMD_FAILURE;
  14760. }
  14761. return buildin_query_sql_sub(st, logmysql_handle);
  14762. }
  14763. //Allows escaping of a given string.
  14764. BUILDIN_FUNC(escape_sql)
  14765. {
  14766. const char *str;
  14767. char *esc_str;
  14768. size_t len;
  14769. str = script_getstr(st,2);
  14770. len = strlen(str);
  14771. esc_str = (char*)aMalloc(len*2+1);
  14772. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14773. script_pushstr(st, esc_str);
  14774. return SCRIPT_CMD_SUCCESS;
  14775. }
  14776. BUILDIN_FUNC(getd)
  14777. {
  14778. char varname[100];
  14779. const char *buffer;
  14780. int elem;
  14781. buffer = script_getstr(st, 2);
  14782. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14783. elem = 0;
  14784. // Push the 'pointer' so it's more flexible [Lance]
  14785. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14786. return SCRIPT_CMD_SUCCESS;
  14787. }
  14788. BUILDIN_FUNC(callshop)
  14789. {
  14790. TBL_PC *sd = NULL;
  14791. struct npc_data *nd;
  14792. const char *shopname;
  14793. int flag = 0;
  14794. if (!script_rid2sd(sd)) {
  14795. script_pushint(st,0);
  14796. return SCRIPT_CMD_SUCCESS;
  14797. }
  14798. shopname = script_getstr(st, 2);
  14799. if (script_hasdata(st,3))
  14800. flag = script_getnum(st,3);
  14801. nd = npc_name2id(shopname);
  14802. 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) ) {
  14803. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14804. script_pushint(st,0);
  14805. return SCRIPT_CMD_FAILURE;
  14806. }
  14807. if (nd->subtype == NPCTYPE_SHOP) {
  14808. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14809. sd->state.callshop = 1;
  14810. switch (flag) {
  14811. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14812. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14813. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14814. }
  14815. }
  14816. #if PACKETVER >= 20131223
  14817. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14818. unsigned short i;
  14819. for (i = 0; i < nd->u.shop.count; i++) {
  14820. if (nd->u.shop.shop_item[i].qty)
  14821. break;
  14822. }
  14823. if (i == nd->u.shop.count) {
  14824. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14825. return SCRIPT_CMD_FAILURE;
  14826. }
  14827. sd->npc_shopid = nd->bl.id;
  14828. clif_npc_market_open(sd, nd);
  14829. script_pushint(st,1);
  14830. return SCRIPT_CMD_SUCCESS;
  14831. }
  14832. #endif
  14833. else
  14834. clif_cashshop_show(sd, nd);
  14835. sd->npc_shopid = nd->bl.id;
  14836. script_pushint(st,1);
  14837. return SCRIPT_CMD_SUCCESS;
  14838. }
  14839. BUILDIN_FUNC(npcshopitem)
  14840. {
  14841. const char* npcname = script_getstr(st, 2);
  14842. struct npc_data* nd = npc_name2id(npcname);
  14843. int n, i;
  14844. int amount;
  14845. uint16 offs = 2;
  14846. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14847. script_pushint(st,0);
  14848. return SCRIPT_CMD_SUCCESS;
  14849. }
  14850. #if PACKETVER >= 20131223
  14851. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14852. offs = 3;
  14853. npc_market_delfromsql_(nd->exname, 0, true);
  14854. }
  14855. #endif
  14856. // get the count of new entries
  14857. amount = (script_lastdata(st)-2)/offs;
  14858. // generate new shop item list
  14859. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14860. nd->u.shop.count = 0;
  14861. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14862. t_itemid nameid = script_getnum( st, i );
  14863. if( itemdb_exists( nameid ) == nullptr ){
  14864. ShowError( "builtin_npcshopitem: Item ID %u does not exist.\n", nameid );
  14865. script_pushint( st, 0 );
  14866. return SCRIPT_CMD_FAILURE;
  14867. }
  14868. nd->u.shop.shop_item[n].nameid = nameid;
  14869. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14870. #if PACKETVER >= 20131223
  14871. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14872. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14873. nd->u.shop.shop_item[n].flag = 1;
  14874. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14875. }
  14876. #endif
  14877. nd->u.shop.count++;
  14878. }
  14879. script_pushint(st,1);
  14880. return SCRIPT_CMD_SUCCESS;
  14881. }
  14882. BUILDIN_FUNC(npcshopadditem)
  14883. {
  14884. const char* npcname = script_getstr(st,2);
  14885. struct npc_data* nd = npc_name2id(npcname);
  14886. uint16 offs = 2, amount;
  14887. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14888. script_pushint(st,0);
  14889. return SCRIPT_CMD_SUCCESS;
  14890. }
  14891. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14892. offs = 3;
  14893. // get the count of new entries
  14894. amount = (script_lastdata(st)-2)/offs;
  14895. #if PACKETVER >= 20131223
  14896. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14897. for (int n = 0, i = 3; n < amount; n++, i += offs) {
  14898. t_itemid nameid = script_getnum(st,i);
  14899. uint16 j;
  14900. if( itemdb_exists( nameid ) == nullptr ){
  14901. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  14902. script_pushint( st, 0 );
  14903. return SCRIPT_CMD_FAILURE;
  14904. }
  14905. // Check existing entries
  14906. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14907. if (j == nd->u.shop.count) {
  14908. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14909. j = nd->u.shop.count;
  14910. nd->u.shop.shop_item[j].flag = 1;
  14911. nd->u.shop.count++;
  14912. }
  14913. nd->u.shop.shop_item[j].nameid = nameid;
  14914. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14915. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14916. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14917. }
  14918. script_pushint(st,1);
  14919. return SCRIPT_CMD_SUCCESS;
  14920. }
  14921. #endif
  14922. // append new items to existing shop item list
  14923. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14924. for (int n = nd->u.shop.count, i = 3, j = 0; j < amount; n++, i+=offs, j++)
  14925. {
  14926. t_itemid nameid = script_getnum( st, i );
  14927. if( itemdb_exists( nameid ) == nullptr ){
  14928. ShowError( "builtin_npcshopadditem: Item ID %u does not exist.\n", nameid );
  14929. script_pushint( st, 0 );
  14930. return SCRIPT_CMD_FAILURE;
  14931. }
  14932. nd->u.shop.shop_item[n].nameid = nameid;
  14933. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14934. nd->u.shop.count++;
  14935. }
  14936. script_pushint(st,1);
  14937. return SCRIPT_CMD_SUCCESS;
  14938. }
  14939. BUILDIN_FUNC(npcshopdelitem)
  14940. {
  14941. const char* npcname = script_getstr(st,2);
  14942. struct npc_data* nd = npc_name2id(npcname);
  14943. int n, i, size;
  14944. unsigned short amount;
  14945. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14946. script_pushint(st,0);
  14947. return SCRIPT_CMD_SUCCESS;
  14948. }
  14949. amount = script_lastdata(st)-2;
  14950. size = nd->u.shop.count;
  14951. // remove specified items from the shop item list
  14952. for( i = 3; i < 3 + amount; i++ ) {
  14953. t_itemid nameid = script_getnum(st,i);
  14954. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14955. if( n < size ) {
  14956. if (n+1 != size)
  14957. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14958. #if PACKETVER >= 20131223
  14959. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14960. npc_market_delfromsql_(nd->exname, nameid, false);
  14961. #endif
  14962. size--;
  14963. }
  14964. }
  14965. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14966. nd->u.shop.count = size;
  14967. script_pushint(st,1);
  14968. return SCRIPT_CMD_SUCCESS;
  14969. }
  14970. /**
  14971. * Sets a script to attach to a shop npc.
  14972. * npcshopattach "<npc_name>";
  14973. **/
  14974. BUILDIN_FUNC(npcshopattach)
  14975. {
  14976. const char* npcname = script_getstr(st,2);
  14977. struct npc_data* nd = npc_name2id(npcname);
  14978. int flag = 1;
  14979. if( script_hasdata(st,3) )
  14980. flag = script_getnum(st,3);
  14981. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14982. script_pushint(st,0);
  14983. return SCRIPT_CMD_SUCCESS;
  14984. }
  14985. if (flag)
  14986. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14987. else
  14988. nd->master_nd = NULL;
  14989. script_pushint(st,1);
  14990. return SCRIPT_CMD_SUCCESS;
  14991. }
  14992. /*==========================================
  14993. * Returns some values of an item [Lupus]
  14994. * Price, Weight, etc...
  14995. setitemscript(itemID,"{new item bonus script}",[n]);
  14996. Where n:
  14997. 0 - script
  14998. 1 - Equip script
  14999. 2 - Unequip script
  15000. *------------------------------------------*/
  15001. BUILDIN_FUNC(setitemscript)
  15002. {
  15003. int n = 0;
  15004. const char *script;
  15005. struct item_data *i_data;
  15006. struct script_code **dstscript;
  15007. t_itemid item_id = script_getnum(st,2);
  15008. script = script_getstr(st,3);
  15009. if( script_hasdata(st,4) )
  15010. n=script_getnum(st,4);
  15011. i_data = itemdb_exists(item_id);
  15012. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15013. script_pushint(st,0);
  15014. return SCRIPT_CMD_SUCCESS;
  15015. }
  15016. switch (n) {
  15017. case 2:
  15018. dstscript = &i_data->unequip_script;
  15019. break;
  15020. case 1:
  15021. dstscript = &i_data->equip_script;
  15022. break;
  15023. default:
  15024. dstscript = &i_data->script;
  15025. break;
  15026. }
  15027. if(*dstscript)
  15028. script_free_code(*dstscript);
  15029. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15030. script_pushint(st,1);
  15031. return SCRIPT_CMD_SUCCESS;
  15032. }
  15033. /*=======================================================
  15034. * Add or Update a mob drop temporarily [Akinari]
  15035. * Original Idea By: [Lupus]
  15036. *
  15037. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  15038. *
  15039. * If given an item the mob already drops, the rate
  15040. * is updated to the new rate. Rate cannot exceed 10000
  15041. * Returns 1 if succeeded (added/updated a mob drop)
  15042. *-------------------------------------------------------*/
  15043. BUILDIN_FUNC(addmonsterdrop)
  15044. {
  15045. std::shared_ptr<s_mob_db> mob;
  15046. int rate;
  15047. if(script_isstring(st, 2))
  15048. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15049. else
  15050. mob = mob_db.find(script_getnum(st,2));
  15051. t_itemid item_id=script_getnum(st,3);
  15052. rate=script_getnum(st,4);
  15053. if(!itemdb_exists(item_id)){
  15054. ShowError("addmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15055. return SCRIPT_CMD_FAILURE;
  15056. }
  15057. if(mob) { //We got a valid monster, check for available drop slot
  15058. unsigned char i, c = 0;
  15059. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15060. if(mob->dropitem[i].nameid) {
  15061. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  15062. c = i;
  15063. break;
  15064. }
  15065. continue;
  15066. }
  15067. if(!c) //Accept first available slot only
  15068. c = i;
  15069. }
  15070. if(c) { //Fill in the slot with the item and rate
  15071. mob->dropitem[c].nameid = item_id;
  15072. mob->dropitem[c].rate = (rate > 10000)?10000:rate;
  15073. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15074. script_pushint(st,1);
  15075. } else //No place to put the new drop
  15076. script_pushint(st,0);
  15077. } else {
  15078. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15079. return SCRIPT_CMD_FAILURE;
  15080. }
  15081. return SCRIPT_CMD_SUCCESS;
  15082. }
  15083. /*=======================================================
  15084. * Delete a mob drop temporarily [Akinari]
  15085. * Original Idea By: [Lupus]
  15086. *
  15087. * delmonsterdrop <mob_id or name>,<item_id>;
  15088. *
  15089. * Returns 1 if succeeded (deleted a mob drop)
  15090. *-------------------------------------------------------*/
  15091. BUILDIN_FUNC(delmonsterdrop)
  15092. {
  15093. std::shared_ptr<s_mob_db> mob;
  15094. if(script_isstring(st, 2))
  15095. mob = mob_db.find(mobdb_searchname(script_getstr(st,2)));
  15096. else
  15097. mob = mob_db.find(script_getnum(st,2));
  15098. t_itemid item_id=script_getnum(st,3);
  15099. if(!itemdb_exists(item_id)){
  15100. ShowError("delmonsterdrop: Nonexistant item %u requested.\n", item_id );
  15101. return SCRIPT_CMD_FAILURE;
  15102. }
  15103. if(mob) { //We got a valid monster, check for item drop on monster
  15104. unsigned char i;
  15105. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15106. if(mob->dropitem[i].nameid == item_id) {
  15107. mob->dropitem[i].nameid = 0;
  15108. mob->dropitem[i].rate = 0;
  15109. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15110. script_pushint(st,1);
  15111. return SCRIPT_CMD_SUCCESS;
  15112. }
  15113. }
  15114. //No drop on that monster
  15115. script_pushint(st,0);
  15116. } else {
  15117. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15118. return SCRIPT_CMD_FAILURE;
  15119. }
  15120. return SCRIPT_CMD_SUCCESS;
  15121. }
  15122. /*==========================================
  15123. * Returns some values of a monster [Lupus]
  15124. * Name, Level, race, size, etc...
  15125. getmonsterinfo(monsterID,queryIndex);
  15126. *------------------------------------------*/
  15127. BUILDIN_FUNC(getmonsterinfo)
  15128. {
  15129. int mob_id;
  15130. mob_id = script_getnum(st,2);
  15131. if (!mobdb_checkid(mob_id)) {
  15132. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15133. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  15134. script_pushconststr(st,"null");
  15135. else
  15136. script_pushint(st,-1);
  15137. return SCRIPT_CMD_SUCCESS;
  15138. }
  15139. std::shared_ptr<s_mob_db> mob = mob_db.find(mob_id);
  15140. switch ( script_getnum(st,3) ) {
  15141. case MOB_NAME: script_pushstrcopy(st,mob->jname.c_str()); break;
  15142. case MOB_LV: script_pushint(st,mob->lv); break;
  15143. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15144. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15145. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15146. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15147. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15148. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15149. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15150. case MOB_STR: script_pushint(st,mob->status.str); break;
  15151. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15152. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15153. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15154. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15155. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15156. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15157. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15158. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15159. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15160. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15161. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15162. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15163. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15164. default: script_pushint(st,-1); //wrong Index
  15165. }
  15166. return SCRIPT_CMD_SUCCESS;
  15167. }
  15168. /**
  15169. * Check player's vending/buyingstore/autotrade state
  15170. * checkvending({"<Player Name>"})
  15171. * @author [Nab4]
  15172. */
  15173. BUILDIN_FUNC(checkvending) {
  15174. TBL_PC *sd = NULL;
  15175. if (!script_nick2sd(2,sd) ) {
  15176. script_pushint(st,0);
  15177. return SCRIPT_CMD_SUCCESS;
  15178. }
  15179. else {
  15180. int8 ret = 0;
  15181. if (sd->state.vending)
  15182. ret = 1;
  15183. else if (sd->state.buyingstore)
  15184. ret = 4;
  15185. if (sd->state.autotrade)
  15186. ret |= 2;
  15187. script_pushint(st, ret);
  15188. }
  15189. return SCRIPT_CMD_SUCCESS;
  15190. }
  15191. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15192. {
  15193. TBL_PC *sd = NULL;
  15194. if( script_nick2sd(2,sd) )
  15195. script_pushint(st,(sd->chatID != 0));
  15196. else
  15197. script_pushint(st,0);
  15198. return SCRIPT_CMD_SUCCESS;
  15199. }
  15200. BUILDIN_FUNC(checkidle)
  15201. {
  15202. TBL_PC *sd = NULL;
  15203. if( script_nick2sd(2,sd) )
  15204. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15205. else
  15206. script_pushint(st, 0);
  15207. return SCRIPT_CMD_SUCCESS;
  15208. }
  15209. BUILDIN_FUNC(checkidlehom)
  15210. {
  15211. TBL_PC *sd = NULL;
  15212. if( script_nick2sd(2,sd) )
  15213. script_pushint(st, DIFF_TICK(last_tick, sd->idletime_hom));
  15214. else
  15215. script_pushint(st, 0);
  15216. return SCRIPT_CMD_SUCCESS;
  15217. }
  15218. BUILDIN_FUNC(searchitem)
  15219. {
  15220. struct script_data* data = script_getdata(st, 2);
  15221. const char *itemname = script_getstr(st,3);
  15222. struct item_data *items[MAX_SEARCH];
  15223. int count;
  15224. char* name;
  15225. int32 start;
  15226. int32 id;
  15227. int32 i;
  15228. TBL_PC* sd = NULL;
  15229. if ((items[0] = itemdb_exists(atoi(itemname))))
  15230. count = 1;
  15231. else
  15232. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  15233. if (!count) {
  15234. script_pushint(st, 0);
  15235. return SCRIPT_CMD_SUCCESS;
  15236. }
  15237. if( !data_isreference(data) )
  15238. {
  15239. ShowError("script:searchitem: not a variable\n");
  15240. script_reportdata(data);
  15241. st->state = END;
  15242. return SCRIPT_CMD_FAILURE;// not a variable
  15243. }
  15244. id = reference_getid(data);
  15245. start = reference_getindex(data);
  15246. name = reference_getname(data);
  15247. if( not_server_variable(*name) && !script_rid2sd(sd) )
  15248. {
  15249. return SCRIPT_CMD_SUCCESS;// no player attached
  15250. }
  15251. if( is_string_variable(name) )
  15252. {// string array
  15253. ShowError("script:searchitem: not an integer array reference\n");
  15254. script_reportdata(data);
  15255. st->state = END;
  15256. return SCRIPT_CMD_FAILURE;// not supported
  15257. }
  15258. for( i = 0; i < count; ++start, ++i )
  15259. {// Set array
  15260. set_reg_num( st, sd, reference_uid( id, start ), name, items[i]->nameid, reference_getref( data ) );
  15261. }
  15262. script_pushint(st, count);
  15263. return SCRIPT_CMD_SUCCESS;
  15264. }
  15265. // [zBuffer] List of player cont commands --->
  15266. BUILDIN_FUNC(rid2name)
  15267. {
  15268. struct block_list *bl = NULL;
  15269. int rid = script_getnum(st,2);
  15270. if((bl = map_id2bl(rid)))
  15271. {
  15272. switch(bl->type) {
  15273. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15274. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15275. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15276. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15277. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15278. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  15279. default:
  15280. ShowError("buildin_rid2name: BL type unknown.\n");
  15281. script_pushconststr(st,"");
  15282. break;
  15283. }
  15284. } else {
  15285. ShowError("buildin_rid2name: invalid RID\n");
  15286. script_pushconststr(st,"(null)");
  15287. }
  15288. return SCRIPT_CMD_SUCCESS;
  15289. }
  15290. /**
  15291. * Toggle a unit from moving.
  15292. * pcblockmove(<unit_id>,<option>);
  15293. */
  15294. BUILDIN_FUNC(pcblockmove)
  15295. {
  15296. struct block_list *bl = NULL;
  15297. if (script_getnum(st, 2))
  15298. bl = map_id2bl(script_getnum(st,2));
  15299. else
  15300. bl = map_id2bl(st->rid);
  15301. if (bl) {
  15302. struct unit_data *ud = unit_bl2ud(bl);
  15303. if (ud)
  15304. ud->state.blockedmove = script_getnum(st,3) > 0;
  15305. }
  15306. return SCRIPT_CMD_SUCCESS;
  15307. }
  15308. /**
  15309. * Toggle a unit from casting skills.
  15310. * pcblockskill(<unit_id>,<option>);
  15311. */
  15312. BUILDIN_FUNC(pcblockskill)
  15313. {
  15314. struct block_list *bl = NULL;
  15315. if (script_getnum(st, 2))
  15316. bl = map_id2bl(script_getnum(st,2));
  15317. else
  15318. bl = map_id2bl(st->rid);
  15319. if (bl) {
  15320. struct unit_data *ud = unit_bl2ud(bl);
  15321. if (ud)
  15322. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15323. }
  15324. return SCRIPT_CMD_SUCCESS;
  15325. }
  15326. BUILDIN_FUNC(setpcblock)
  15327. {
  15328. TBL_PC *sd;
  15329. if (script_mapid2sd(4, sd)) {
  15330. enum e_pcblock_action_flag type = (e_pcblock_action_flag)script_getnum(st, 2);
  15331. if (script_getnum(st, 3) > 0)
  15332. sd->state.block_action |= type;
  15333. else
  15334. sd->state.block_action &= ~type;
  15335. }
  15336. return SCRIPT_CMD_SUCCESS;
  15337. }
  15338. BUILDIN_FUNC(getpcblock)
  15339. {
  15340. TBL_PC *sd;
  15341. if (script_mapid2sd(2, sd))
  15342. script_pushint(st, sd->state.block_action);
  15343. else
  15344. script_pushint(st, 0);
  15345. return SCRIPT_CMD_SUCCESS;
  15346. }
  15347. BUILDIN_FUNC(pcfollow)
  15348. {
  15349. TBL_PC *sd;
  15350. if( script_mapid2sd(2,sd) )
  15351. pc_follow(sd, script_getnum(st,3));
  15352. return SCRIPT_CMD_SUCCESS;
  15353. }
  15354. BUILDIN_FUNC(pcstopfollow)
  15355. {
  15356. TBL_PC *sd;
  15357. if(script_mapid2sd(2,sd))
  15358. pc_stop_following(sd);
  15359. return SCRIPT_CMD_SUCCESS;
  15360. }
  15361. // <--- [zBuffer] List of player cont commands
  15362. // [zBuffer] List of unit control commands --->
  15363. /// Checks to see if the unit exists.
  15364. ///
  15365. /// unitexists <unit id>;
  15366. BUILDIN_FUNC(unitexists)
  15367. {
  15368. struct block_list* bl;
  15369. bl = map_id2bl(script_getnum(st, 2));
  15370. if (!bl)
  15371. script_pushint(st, false);
  15372. else
  15373. script_pushint(st, true);
  15374. return SCRIPT_CMD_SUCCESS;
  15375. }
  15376. /// Gets the type of the given Game ID.
  15377. ///
  15378. /// getunittype <unit id>;
  15379. BUILDIN_FUNC(getunittype)
  15380. {
  15381. struct block_list* bl;
  15382. int value = 0;
  15383. if(!script_rid2bl(2,bl))
  15384. {
  15385. script_pushint(st, -1);
  15386. return SCRIPT_CMD_FAILURE;
  15387. }
  15388. switch (bl->type) {
  15389. case BL_PC:
  15390. case BL_NPC:
  15391. case BL_PET:
  15392. case BL_MOB:
  15393. case BL_HOM:
  15394. case BL_MER:
  15395. case BL_ELEM:
  15396. value = bl->type;
  15397. break;
  15398. default:
  15399. value = -1;
  15400. break;
  15401. }
  15402. script_pushint(st, value);
  15403. return SCRIPT_CMD_SUCCESS;
  15404. }
  15405. /// Gets specific live information of a bl.
  15406. ///
  15407. /// getunitdata <unit id>,<arrayname>;
  15408. BUILDIN_FUNC(getunitdata)
  15409. {
  15410. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15411. struct block_list* bl;
  15412. TBL_MOB* md = NULL;
  15413. TBL_HOM* hd = NULL;
  15414. TBL_MER* mc = NULL;
  15415. TBL_PET* pd = NULL;
  15416. TBL_ELEM* ed = NULL;
  15417. TBL_NPC* nd = NULL;
  15418. char* name;
  15419. struct script_data *data = script_getdata(st, 3);
  15420. if (!data_isreference(data)) {
  15421. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15422. return SCRIPT_CMD_FAILURE;
  15423. }
  15424. if(!script_rid2bl(2,bl))
  15425. {
  15426. script_pushint(st, -1);
  15427. return SCRIPT_CMD_FAILURE;
  15428. }
  15429. switch (bl->type) {
  15430. case BL_MOB: md = map_id2md(bl->id); break;
  15431. case BL_HOM: hd = map_id2hd(bl->id); break;
  15432. case BL_PET: pd = map_id2pd(bl->id); break;
  15433. case BL_MER: mc = map_id2mc(bl->id); break;
  15434. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15435. case BL_NPC: nd = map_id2nd(bl->id); break;
  15436. default:
  15437. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  15438. return SCRIPT_CMD_FAILURE;
  15439. }
  15440. name = reference_getname(data);
  15441. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  15442. ShowError("buildin_getunitdata: Cannot use a player variable '%s' if no player is attached.\n", name);
  15443. return SCRIPT_CMD_FAILURE;
  15444. }
  15445. #define getunitdata_sub(idx__,var__) setd_sub_num(st,sd,name,(idx__),(var__),data->ref)
  15446. switch(bl->type) {
  15447. case BL_MOB:
  15448. if (!md) {
  15449. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  15450. return SCRIPT_CMD_FAILURE;
  15451. }
  15452. getunitdata_sub(UMOB_SIZE, md->status.size);
  15453. getunitdata_sub(UMOB_LEVEL, md->level);
  15454. getunitdata_sub(UMOB_HP, md->status.hp);
  15455. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  15456. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  15457. getunitdata_sub(UMOB_MAPID, md->bl.m);
  15458. getunitdata_sub(UMOB_X, md->bl.x);
  15459. getunitdata_sub(UMOB_Y, md->bl.y);
  15460. getunitdata_sub(UMOB_SPEED, md->status.speed);
  15461. getunitdata_sub(UMOB_MODE, md->status.mode);
  15462. getunitdata_sub(UMOB_AI, md->special_state.ai);
  15463. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  15464. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15465. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15466. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15467. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15468. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15469. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15470. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15471. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15472. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15473. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15474. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15475. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15476. getunitdata_sub(UMOB_STR, md->status.str);
  15477. getunitdata_sub(UMOB_AGI, md->status.agi);
  15478. getunitdata_sub(UMOB_VIT, md->status.vit);
  15479. getunitdata_sub(UMOB_INT, md->status.int_);
  15480. getunitdata_sub(UMOB_DEX, md->status.dex);
  15481. getunitdata_sub(UMOB_LUK, md->status.luk);
  15482. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15483. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15484. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15485. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15486. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15487. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15488. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15489. getunitdata_sub(UMOB_DEF, md->status.def);
  15490. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15491. getunitdata_sub(UMOB_HIT, md->status.hit);
  15492. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15493. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15494. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15495. getunitdata_sub(UMOB_RACE, md->status.race);
  15496. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15497. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15498. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15499. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15500. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15501. getunitdata_sub(UMOB_TARGETID, md->target_id);
  15502. getunitdata_sub(UMOB_ROBE, md->vd->robe);
  15503. getunitdata_sub(UMOB_BODY2, md->vd->body_style);
  15504. getunitdata_sub(UMOB_GROUP_ID, md->ud.group_id);
  15505. break;
  15506. case BL_HOM:
  15507. if (!hd) {
  15508. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15509. return SCRIPT_CMD_FAILURE;
  15510. }
  15511. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15512. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15513. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15514. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15515. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15516. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15517. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15518. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15519. getunitdata_sub(UHOM_X, hd->bl.x);
  15520. getunitdata_sub(UHOM_Y, hd->bl.y);
  15521. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15522. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15523. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15524. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15525. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15526. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15527. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15528. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15529. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15530. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15531. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15532. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15533. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15534. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15535. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15536. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15537. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15538. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15539. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15540. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15541. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15542. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15543. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15544. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15545. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15546. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15547. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15548. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15549. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15550. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  15551. getunitdata_sub(UHOM_GROUP_ID, hd->ud.group_id);
  15552. break;
  15553. case BL_PET:
  15554. if (!pd) {
  15555. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15556. return SCRIPT_CMD_FAILURE;
  15557. }
  15558. getunitdata_sub(UPET_SIZE, pd->status.size);
  15559. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15560. getunitdata_sub(UPET_HP, pd->status.hp);
  15561. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15562. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15563. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15564. getunitdata_sub(UPET_X, pd->bl.x);
  15565. getunitdata_sub(UPET_Y, pd->bl.y);
  15566. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15567. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15568. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15569. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15570. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15571. getunitdata_sub(UPET_STR, pd->status.str);
  15572. getunitdata_sub(UPET_AGI, pd->status.agi);
  15573. getunitdata_sub(UPET_VIT, pd->status.vit);
  15574. getunitdata_sub(UPET_INT, pd->status.int_);
  15575. getunitdata_sub(UPET_DEX, pd->status.dex);
  15576. getunitdata_sub(UPET_LUK, pd->status.luk);
  15577. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15578. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15579. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15580. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15581. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15582. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15583. getunitdata_sub(UPET_DEF, pd->status.def);
  15584. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15585. getunitdata_sub(UPET_HIT, pd->status.hit);
  15586. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15587. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15588. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15589. getunitdata_sub(UPET_RACE, pd->status.race);
  15590. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15591. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15592. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15593. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15594. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15595. getunitdata_sub(UPET_GROUP_ID, pd->ud.group_id);
  15596. break;
  15597. case BL_MER:
  15598. if (!mc) {
  15599. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15600. return SCRIPT_CMD_FAILURE;
  15601. }
  15602. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15603. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15604. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15605. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15606. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15607. getunitdata_sub(UMER_X, mc->bl.x);
  15608. getunitdata_sub(UMER_Y, mc->bl.y);
  15609. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15610. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15611. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15612. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15613. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15614. getunitdata_sub(UMER_STR, mc->base_status.str);
  15615. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15616. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15617. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15618. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15619. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15620. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15621. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15622. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15623. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15624. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15625. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15626. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15627. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15628. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15629. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15630. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15631. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15632. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15633. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15634. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15635. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15636. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15637. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15638. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  15639. getunitdata_sub(UMER_GROUP_ID, mc->ud.group_id);
  15640. break;
  15641. case BL_ELEM:
  15642. if (!ed) {
  15643. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15644. return SCRIPT_CMD_FAILURE;
  15645. }
  15646. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15647. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15648. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15649. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15650. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15651. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15652. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15653. getunitdata_sub(UELE_X, ed->bl.x);
  15654. getunitdata_sub(UELE_Y, ed->bl.y);
  15655. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15656. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15657. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15658. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15659. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15660. getunitdata_sub(UELE_STR, ed->base_status.str);
  15661. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15662. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15663. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15664. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15665. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15666. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15667. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15668. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15669. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15670. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15671. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15672. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15673. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15674. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15675. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15676. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15677. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15678. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15679. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15680. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15681. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15682. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15683. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15684. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  15685. getunitdata_sub(UELE_GROUP_ID, ed->ud.group_id);
  15686. break;
  15687. case BL_NPC:
  15688. if (!nd) {
  15689. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15690. return SCRIPT_CMD_FAILURE;
  15691. }
  15692. getunitdata_sub(UNPC_LEVEL, nd->level);
  15693. getunitdata_sub(UNPC_HP, nd->status.hp);
  15694. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15695. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15696. getunitdata_sub(UNPC_X, nd->bl.x);
  15697. getunitdata_sub(UNPC_Y, nd->bl.y);
  15698. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15699. getunitdata_sub(UNPC_STR, nd->status.str);
  15700. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15701. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15702. getunitdata_sub(UNPC_INT, nd->status.int_);
  15703. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15704. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15705. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15706. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15707. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15708. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15709. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15710. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15711. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15712. getunitdata_sub(UNPC_DEF, nd->status.def);
  15713. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15714. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15715. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15716. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15717. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15718. getunitdata_sub(UNPC_RACE, nd->status.race);
  15719. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15720. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15721. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15722. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15723. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15724. getunitdata_sub(UNPC_SEX, nd->vd.sex);
  15725. getunitdata_sub(UNPC_CLASS, nd->vd.class_);
  15726. getunitdata_sub(UNPC_HAIRSTYLE, nd->vd.hair_style);
  15727. getunitdata_sub(UNPC_HAIRCOLOR, nd->vd.hair_color);
  15728. getunitdata_sub(UNPC_HEADBOTTOM, nd->vd.head_bottom);
  15729. getunitdata_sub(UNPC_HEADMIDDLE, nd->vd.head_mid);
  15730. getunitdata_sub(UNPC_HEADTOP, nd->vd.head_top);
  15731. getunitdata_sub(UNPC_CLOTHCOLOR, nd->vd.cloth_color);
  15732. getunitdata_sub(UNPC_SHIELD, nd->vd.shield);
  15733. getunitdata_sub(UNPC_WEAPON, nd->vd.weapon);
  15734. getunitdata_sub(UNPC_ROBE, nd->vd.robe);
  15735. getunitdata_sub(UNPC_BODY2, nd->vd.body_style);
  15736. getunitdata_sub(UNPC_DEADSIT, nd->vd.dead_sit);
  15737. getunitdata_sub(UNPC_GROUP_ID, nd->ud.group_id);
  15738. break;
  15739. default:
  15740. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15741. return SCRIPT_CMD_FAILURE;
  15742. }
  15743. #undef getunitdata_sub
  15744. return SCRIPT_CMD_SUCCESS;
  15745. }
  15746. /// Changes the live data of a bl.
  15747. ///
  15748. /// setunitdata <unit id>,<type>,<value>;
  15749. BUILDIN_FUNC(setunitdata)
  15750. {
  15751. struct block_list* bl;
  15752. if(!script_rid2bl(2,bl))
  15753. {
  15754. script_pushint(st, -1);
  15755. return SCRIPT_CMD_FAILURE;
  15756. }
  15757. TBL_MOB* md;
  15758. TBL_HOM* hd;
  15759. TBL_MER* mc;
  15760. TBL_PET* pd;
  15761. TBL_ELEM* ed;
  15762. TBL_NPC* nd;
  15763. switch (bl->type) {
  15764. case BL_MOB: md = map_id2md(bl->id); break;
  15765. case BL_HOM: hd = map_id2hd(bl->id); break;
  15766. case BL_PET: pd = map_id2pd(bl->id); break;
  15767. case BL_MER: mc = map_id2mc(bl->id); break;
  15768. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15769. case BL_NPC:
  15770. nd = map_id2nd(bl->id);
  15771. if (!nd->status.hp)
  15772. status_calc_npc(nd, SCO_FIRST);
  15773. else
  15774. status_calc_npc(nd, SCO_NONE);
  15775. break;
  15776. default:
  15777. ShowError("buildin_setunitdata: Invalid object!\n");
  15778. return SCRIPT_CMD_FAILURE;
  15779. }
  15780. int type = script_getnum(st, 3), value = 0;
  15781. const char *mapname = NULL;
  15782. bool calc_status = false;
  15783. if ((type == UMOB_MAPID || type == UHOM_MAPID || type == UPET_MAPID || type == UMER_MAPID || type == UELE_MAPID || type == UNPC_MAPID) && script_isstring(st, 4))
  15784. mapname = script_getstr(st, 4);
  15785. else
  15786. value = script_getnum(st, 4);
  15787. switch (bl->type) {
  15788. case BL_MOB:
  15789. if (!md) {
  15790. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15791. return SCRIPT_CMD_FAILURE;
  15792. }
  15793. if (!md->base_status) {
  15794. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  15795. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  15796. }
  15797. // Check if the view data will be modified
  15798. switch( type ){
  15799. case UMOB_SEX:
  15800. //case UMOB_CLASS: // Called by status_set_viewdata
  15801. case UMOB_HAIRSTYLE:
  15802. case UMOB_HAIRCOLOR:
  15803. case UMOB_HEADBOTTOM:
  15804. case UMOB_HEADMIDDLE:
  15805. case UMOB_HEADTOP:
  15806. case UMOB_CLOTHCOLOR:
  15807. case UMOB_SHIELD:
  15808. case UMOB_WEAPON:
  15809. case UMOB_ROBE:
  15810. case UMOB_BODY2:
  15811. mob_set_dynamic_viewdata( md );
  15812. break;
  15813. }
  15814. switch (type) {
  15815. case UMOB_SIZE: md->status.size = md->base_status->size = (unsigned char)value; break;
  15816. case UMOB_LEVEL: md->level = (unsigned short)value; clif_name_area(&md->bl); break;
  15817. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15818. 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;
  15819. case UMOB_MASTERAID: md->master_id = value; break;
  15820. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15821. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15822. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15823. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15824. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  15825. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15826. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15827. case UMOB_SEX: md->vd->sex = (char)value; unit_refresh(bl); break;
  15828. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); unit_refresh(bl); break;
  15829. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15830. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15831. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15832. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15833. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15834. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15835. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15836. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15837. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15838. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15839. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15840. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15841. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15842. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15843. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15844. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15845. case UMOB_SLAVECPYMSTRMD:
  15846. if (value > 0) {
  15847. TBL_MOB *md2 = NULL;
  15848. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15849. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15850. break;
  15851. }
  15852. md->base_status->mode = md2->status.mode;
  15853. md->state.copy_master_mode = 1;
  15854. } else
  15855. md->state.copy_master_mode = 0;
  15856. calc_status = true;
  15857. break;
  15858. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  15859. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  15860. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  15861. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15862. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  15863. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  15864. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  15865. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  15866. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  15867. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  15868. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  15869. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  15870. case UMOB_RACE: md->status.race = md->base_status->race = (unsigned char)value; break;
  15871. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  15872. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  15873. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  15874. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  15875. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  15876. case UMOB_TARGETID: {
  15877. if (value==0) {
  15878. mob_unlocktarget(md,gettick());
  15879. break;
  15880. }
  15881. struct block_list* target = map_id2bl(value);
  15882. if (!target) {
  15883. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  15884. return SCRIPT_CMD_FAILURE;
  15885. }
  15886. mob_target(md,target,0);
  15887. break;
  15888. }
  15889. case UMOB_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  15890. case UMOB_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  15891. case UMOB_GROUP_ID: md->ud.group_id = value; unit_refresh(bl); break;
  15892. default:
  15893. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15894. return SCRIPT_CMD_FAILURE;
  15895. }
  15896. if (calc_status)
  15897. status_calc_bl(&md->bl, SCB_BATTLE);
  15898. break;
  15899. case BL_HOM:
  15900. if (!hd) {
  15901. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15902. return SCRIPT_CMD_FAILURE;
  15903. }
  15904. switch (type) {
  15905. case UHOM_SIZE: hd->battle_status.size = hd->base_status.size = (unsigned char)value; break;
  15906. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15907. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15908. case UHOM_MAXHP: hd->base_status.hp = hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15909. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15910. case UHOM_MAXSP: hd->base_status.sp = hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15911. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15912. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15913. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15914. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15915. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15916. 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;
  15917. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15918. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15919. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15920. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15921. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15922. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15923. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15924. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15925. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15926. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  15927. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15928. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15929. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15930. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15931. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15932. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  15933. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  15934. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  15935. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  15936. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  15937. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  15938. case UHOM_RACE: hd->battle_status.race = hd->base_status.race = (unsigned char)value; break;
  15939. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15940. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15941. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  15942. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  15943. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  15944. case UHOM_TARGETID: {
  15945. if (value==0) {
  15946. unit_stop_attack(&hd->bl);
  15947. break;
  15948. }
  15949. struct block_list* target = map_id2bl(value);
  15950. if (!target) {
  15951. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  15952. return SCRIPT_CMD_FAILURE;
  15953. }
  15954. unit_attack(&hd->bl, target->id, 1);
  15955. break;
  15956. }
  15957. case UHOM_GROUP_ID: hd->ud.group_id = value; unit_refresh(bl); break;
  15958. default:
  15959. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15960. return SCRIPT_CMD_FAILURE;
  15961. }
  15962. if (calc_status)
  15963. status_calc_bl(&hd->bl, SCB_BATTLE);
  15964. break;
  15965. case BL_PET:
  15966. if (!pd) {
  15967. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15968. return SCRIPT_CMD_FAILURE;
  15969. }
  15970. switch (type) {
  15971. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15972. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15973. case UPET_HP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15974. case UPET_MAXHP: pd->status.hp = pd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15975. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15976. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15977. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15978. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15979. 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;
  15980. 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;
  15981. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15982. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15983. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15984. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15985. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15986. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15987. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15988. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15989. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15990. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  15991. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15992. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15993. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15994. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15995. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15996. case UPET_DEF: pd->status.def = (defType)value; break;
  15997. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15998. case UPET_HIT: pd->status.hit = (short)value; break;
  15999. case UPET_FLEE: pd->status.flee = (short)value; break;
  16000. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  16001. case UPET_CRIT: pd->status.cri = (short)value; break;
  16002. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  16003. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  16004. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  16005. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  16006. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  16007. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  16008. case UPET_GROUP_ID: pd->ud.group_id = value; unit_refresh(bl); break;
  16009. default:
  16010. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  16011. return SCRIPT_CMD_FAILURE;
  16012. }
  16013. break;
  16014. case BL_MER:
  16015. if (!mc) {
  16016. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  16017. return SCRIPT_CMD_FAILURE;
  16018. }
  16019. switch (type) {
  16020. case UMER_SIZE: mc->battle_status.size = mc->base_status.size = (unsigned char)value; break;
  16021. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16022. case UMER_MAXHP: mc->base_status.hp = mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16023. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  16024. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16025. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  16026. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  16027. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  16028. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  16029. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16030. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16031. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16032. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16033. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16034. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16035. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16036. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16037. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  16038. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  16039. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16040. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16041. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16042. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16043. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16044. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  16045. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  16046. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  16047. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  16048. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  16049. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  16050. case UMER_RACE: mc->battle_status.race = mc->base_status.race = (unsigned char)value; break;
  16051. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16052. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16053. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  16054. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  16055. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  16056. case UMER_TARGETID: {
  16057. if (value==0) {
  16058. unit_stop_attack(&mc->bl);
  16059. break;
  16060. }
  16061. struct block_list* target = map_id2bl(value);
  16062. if (!target) {
  16063. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  16064. return SCRIPT_CMD_FAILURE;
  16065. }
  16066. unit_attack(&mc->bl, target->id, 1);
  16067. break;
  16068. }
  16069. case UMER_GROUP_ID: mc->ud.group_id = value; unit_refresh(bl); break;
  16070. default:
  16071. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16072. return SCRIPT_CMD_FAILURE;
  16073. }
  16074. if (calc_status)
  16075. status_calc_bl(&mc->bl, SCB_BATTLE);
  16076. break;
  16077. case BL_ELEM:
  16078. if (!ed) {
  16079. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16080. return SCRIPT_CMD_FAILURE;
  16081. }
  16082. switch (type) {
  16083. case UELE_SIZE: ed->battle_status.size = ed->base_status.size = (unsigned char)value; break;
  16084. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16085. case UELE_MAXHP: ed->base_status.hp = ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16086. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16087. case UELE_MAXSP: ed->base_status.sp = ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16088. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16089. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16090. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16091. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16092. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16093. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16094. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16095. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16096. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16097. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16098. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16099. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16100. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16101. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16102. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16103. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16104. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16105. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16106. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16107. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16108. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16109. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16110. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16111. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16112. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16113. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16114. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16115. case UELE_RACE: ed->battle_status.race = ed->base_status.race = (unsigned char)value; break;
  16116. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16117. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16118. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16119. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16120. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16121. case UELE_TARGETID: {
  16122. if (value==0) {
  16123. unit_stop_attack(&ed->bl);
  16124. break;
  16125. }
  16126. struct block_list* target = map_id2bl(value);
  16127. if (!target) {
  16128. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16129. return SCRIPT_CMD_FAILURE;
  16130. }
  16131. elemental_change_mode(ed, static_cast<e_mode>(EL_MODE_AGGRESSIVE));
  16132. unit_attack(&ed->bl, target->id, 1);
  16133. break;
  16134. }
  16135. case UELE_GROUP_ID: ed->ud.group_id = value; unit_refresh(bl); break;
  16136. default:
  16137. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16138. return SCRIPT_CMD_FAILURE;
  16139. }
  16140. if (calc_status)
  16141. status_calc_bl(&ed->bl, SCB_BATTLE);
  16142. break;
  16143. case BL_NPC:
  16144. if (!nd) {
  16145. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16146. return SCRIPT_CMD_FAILURE;
  16147. }
  16148. switch (type) {
  16149. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16150. case UNPC_HP: nd->status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16151. case UNPC_MAXHP: nd->status.hp = nd->status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16152. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16153. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16154. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16155. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16156. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16157. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16158. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16159. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16160. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16161. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16162. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16163. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16164. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16165. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16166. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16167. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16168. case UNPC_DEF: nd->status.def = (defType)value; break;
  16169. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16170. case UNPC_HIT: nd->status.hit = (short)value; break;
  16171. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16172. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16173. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16174. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16175. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16176. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16177. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16178. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16179. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16180. case UNPC_SEX: nd->vd.sex = (char)value; unit_refresh(bl); break;
  16181. case UNPC_CLASS: npc_setclass(nd, (short)value); break;
  16182. case UNPC_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  16183. case UNPC_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  16184. case UNPC_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  16185. case UNPC_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  16186. case UNPC_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  16187. case UNPC_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  16188. case UNPC_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  16189. case UNPC_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  16190. case UNPC_ROBE: clif_changelook(bl, LOOK_ROBE, (unsigned short)value); break;
  16191. case UNPC_BODY2: clif_changelook(bl, LOOK_BODY2, (unsigned short)value); break;
  16192. case UNPC_DEADSIT: nd->vd.dead_sit = (char)value; unit_refresh(bl); break;
  16193. case UNPC_GROUP_ID: nd->ud.group_id = value; unit_refresh(bl); break;
  16194. default:
  16195. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16196. return SCRIPT_CMD_FAILURE;
  16197. }
  16198. break;
  16199. default:
  16200. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16201. return SCRIPT_CMD_FAILURE;
  16202. }
  16203. // Client information updates
  16204. switch (bl->type) {
  16205. case BL_HOM:
  16206. clif_send_homdata(hd->master, SP_ACK, 0);
  16207. break;
  16208. case BL_PET:
  16209. clif_send_petstatus(pd->master);
  16210. break;
  16211. case BL_MER:
  16212. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16213. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16214. break;
  16215. case BL_ELEM:
  16216. clif_elemental_info(ed->master);
  16217. break;
  16218. }
  16219. return SCRIPT_CMD_SUCCESS;
  16220. }
  16221. /// Gets the name of a bl.
  16222. /// Supported types are [MOB|HOM|PET|NPC].
  16223. /// MER and ELEM don't support custom names.
  16224. ///
  16225. /// getunitname <unit id>;
  16226. BUILDIN_FUNC(getunitname)
  16227. {
  16228. struct block_list* bl = NULL;
  16229. if(!script_rid2bl(2,bl)){
  16230. script_pushconststr(st, "Unknown");
  16231. return SCRIPT_CMD_FAILURE;
  16232. }
  16233. script_pushstrcopy(st, status_get_name(bl));
  16234. return SCRIPT_CMD_SUCCESS;
  16235. }
  16236. /// Changes the name of a bl.
  16237. /// Supported types are [MOB|HOM|PET].
  16238. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16239. ///
  16240. /// setunitname <unit id>,<name>;
  16241. BUILDIN_FUNC(setunitname)
  16242. {
  16243. struct block_list* bl = NULL;
  16244. TBL_MOB* md = NULL;
  16245. TBL_HOM* hd = NULL;
  16246. TBL_PET* pd = NULL;
  16247. if(!script_rid2bl(2,bl))
  16248. {
  16249. script_pushconststr(st, "Unknown");
  16250. return SCRIPT_CMD_FAILURE;
  16251. }
  16252. switch (bl->type) {
  16253. case BL_MOB: md = map_id2md(bl->id); break;
  16254. case BL_HOM: hd = map_id2hd(bl->id); break;
  16255. case BL_PET: pd = map_id2pd(bl->id); break;
  16256. default:
  16257. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16258. return SCRIPT_CMD_FAILURE;
  16259. }
  16260. switch (bl->type) {
  16261. case BL_MOB:
  16262. if (!md) {
  16263. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16264. return SCRIPT_CMD_FAILURE;
  16265. }
  16266. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16267. break;
  16268. case BL_HOM:
  16269. if (!hd) {
  16270. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16271. return SCRIPT_CMD_FAILURE;
  16272. }
  16273. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16274. break;
  16275. case BL_PET:
  16276. if (!pd) {
  16277. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16278. return SCRIPT_CMD_FAILURE;
  16279. }
  16280. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16281. break;
  16282. default:
  16283. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16284. return SCRIPT_CMD_FAILURE;
  16285. }
  16286. clif_name_area(bl); // Send update to client.
  16287. return SCRIPT_CMD_SUCCESS;
  16288. }
  16289. /**
  16290. * Sets a unit's title.
  16291. * setunittitle <GID>,<title>;
  16292. */
  16293. BUILDIN_FUNC(setunittitle)
  16294. {
  16295. int gid = script_getnum(st, 2);
  16296. block_list *bl = map_id2bl(gid);
  16297. if (bl == nullptr) {
  16298. ShowWarning("buildin_setunittitle: Unable to find object with given game ID %d!\n", gid);
  16299. return SCRIPT_CMD_FAILURE;
  16300. }
  16301. unit_data *ud = unit_bl2ud(bl);
  16302. if (ud == nullptr) {
  16303. ShowWarning("buildin_setunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16304. return SCRIPT_CMD_FAILURE;
  16305. }
  16306. safestrncpy(ud->title, script_getstr(st, 3), NAME_LENGTH);
  16307. clif_name_area(bl);
  16308. return SCRIPT_CMD_SUCCESS;
  16309. }
  16310. /**
  16311. * Gets a unit's title.
  16312. * getunittitle <GID>;
  16313. */
  16314. BUILDIN_FUNC(getunittitle)
  16315. {
  16316. int gid = script_getnum(st, 2);
  16317. block_list *bl = map_id2bl(gid);
  16318. if (bl == nullptr) {
  16319. ShowWarning("buildin_getunittitle: Unable to find object with given game ID %d!\n", gid);
  16320. return SCRIPT_CMD_FAILURE;
  16321. }
  16322. unit_data *ud = unit_bl2ud(bl);
  16323. if (ud == nullptr) {
  16324. ShowWarning("buildin_getunittitle: Unable to find unit_data for given game ID %d!\n", gid);
  16325. return SCRIPT_CMD_FAILURE;
  16326. }
  16327. script_pushstrcopy(st, ud->title);
  16328. return SCRIPT_CMD_SUCCESS;
  16329. }
  16330. /// Makes the unit walk to target position or map.
  16331. /// Returns if it was successful.
  16332. ///
  16333. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16334. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16335. BUILDIN_FUNC(unitwalk)
  16336. {
  16337. struct block_list* bl;
  16338. struct unit_data *ud = NULL;
  16339. const char *cmd = script_getfuncname(st), *done_label = "";
  16340. uint8 off = 5;
  16341. if(!script_rid2bl(2,bl))
  16342. {
  16343. script_pushint(st, 0);
  16344. return SCRIPT_CMD_FAILURE;
  16345. }
  16346. ud = unit_bl2ud(bl);
  16347. if (!strcmp(cmd,"unitwalk")) {
  16348. int x = script_getnum(st,3);
  16349. int y = script_getnum(st,4);
  16350. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  16351. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  16352. } else {
  16353. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  16354. if (!tbl) {
  16355. ShowError("buildin_unitwalk: Bad target destination.\n");
  16356. script_pushint(st, 0);
  16357. return SCRIPT_CMD_FAILURE;
  16358. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  16359. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  16360. off = 4;
  16361. }
  16362. if (ud && script_hasdata(st, off)) {
  16363. done_label = script_getstr(st, off);
  16364. check_event(st, done_label);
  16365. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  16366. }
  16367. return SCRIPT_CMD_SUCCESS;
  16368. }
  16369. /// Kills the unit.
  16370. ///
  16371. /// unitkill <unit_id>;
  16372. BUILDIN_FUNC(unitkill)
  16373. {
  16374. struct block_list* bl;
  16375. if(script_rid2bl(2,bl))
  16376. status_kill(bl);
  16377. return SCRIPT_CMD_SUCCESS;
  16378. }
  16379. /// Warps the unit to the target position in the target map.
  16380. /// Returns if it was successful.
  16381. ///
  16382. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  16383. BUILDIN_FUNC(unitwarp)
  16384. {
  16385. int map_idx;
  16386. short x;
  16387. short y;
  16388. struct block_list* bl;
  16389. const char *mapname;
  16390. mapname = script_getstr(st, 3);
  16391. x = (short)script_getnum(st,4);
  16392. y = (short)script_getnum(st,5);
  16393. if(!script_rid2bl(2,bl))
  16394. {
  16395. script_pushint(st, 0);
  16396. return SCRIPT_CMD_SUCCESS;
  16397. }
  16398. if (!strcmp(mapname,"this"))
  16399. map_idx = bl?bl->m:-1;
  16400. else
  16401. map_idx = map_mapname2mapid(mapname);
  16402. if (map_idx >= 0 && bl != NULL)
  16403. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  16404. else
  16405. script_pushint(st, 0);
  16406. return SCRIPT_CMD_SUCCESS;
  16407. }
  16408. /// Makes the unit attack the target.
  16409. /// If the unit is a player and <action type> is not 0, it does a continuous
  16410. /// attack instead of a single attack.
  16411. /// Returns if the request was successful.
  16412. ///
  16413. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  16414. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  16415. BUILDIN_FUNC(unitattack)
  16416. {
  16417. struct block_list* unit_bl;
  16418. struct block_list* target_bl = NULL;
  16419. int actiontype = 0;
  16420. if (!script_rid2bl(2,unit_bl)) {
  16421. script_pushint(st, false);
  16422. return SCRIPT_CMD_FAILURE;
  16423. }
  16424. if (script_isstring(st, 3)) {
  16425. TBL_PC* sd = map_nick2sd(script_getstr(st, 3),false);
  16426. if( sd != NULL )
  16427. target_bl = &sd->bl;
  16428. } else
  16429. target_bl = map_id2bl(script_getnum(st, 3));
  16430. if (!target_bl) {
  16431. script_pushint(st, false);
  16432. return SCRIPT_CMD_FAILURE;
  16433. }
  16434. if (script_hasdata(st,4))
  16435. actiontype = script_getnum(st,4);
  16436. switch(unit_bl->type) {
  16437. case BL_PC: {
  16438. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  16439. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  16440. script_pushint(st, sd->ud.target == target_bl->id);
  16441. return SCRIPT_CMD_SUCCESS;
  16442. }
  16443. case BL_MOB:
  16444. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  16445. break;
  16446. case BL_PET:
  16447. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  16448. break;
  16449. default:
  16450. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  16451. script_pushint(st, false);
  16452. return SCRIPT_CMD_FAILURE;
  16453. }
  16454. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  16455. return SCRIPT_CMD_SUCCESS;
  16456. }
  16457. /// Makes the unit stop attacking.
  16458. ///
  16459. /// unitstopattack <unit_id>;
  16460. BUILDIN_FUNC(unitstopattack)
  16461. {
  16462. struct block_list* bl;
  16463. if(script_rid2bl(2,bl))
  16464. {
  16465. unit_stop_attack(bl);
  16466. if (bl->type == BL_MOB)
  16467. ((TBL_MOB*)bl)->target_id = 0;
  16468. }
  16469. return SCRIPT_CMD_SUCCESS;
  16470. }
  16471. /// Makes the unit stop walking.
  16472. ///
  16473. /// unitstopwalk <unit_id>{,<flag>};
  16474. BUILDIN_FUNC(unitstopwalk)
  16475. {
  16476. struct block_list* bl;
  16477. int flag = USW_NONE;
  16478. if (script_hasdata(st, 3))
  16479. flag = script_getnum(st, 3);
  16480. if(script_rid2bl(2,bl))
  16481. unit_stop_walking(bl, flag);
  16482. return SCRIPT_CMD_SUCCESS;
  16483. }
  16484. /**
  16485. * Makes the unit say the given message.
  16486. *
  16487. * unittalk <unit_id>,"<message>"{,"<flag>"};
  16488. * @param flag: Specify target
  16489. * bc_area - Message is sent to players in the vicinity of the source (default).
  16490. * bc_self - Message is sent only to player attached.
  16491. */
  16492. BUILDIN_FUNC(unittalk)
  16493. {
  16494. const char* message;
  16495. struct block_list* bl;
  16496. message = script_getstr(st, 3);
  16497. if(script_rid2bl(2,bl))
  16498. {
  16499. send_target target = AREA;
  16500. struct StringBuf sbuf;
  16501. if (script_hasdata(st, 4)) {
  16502. if (script_getnum(st, 4) == BC_SELF) {
  16503. if (map_id2sd(bl->id) == NULL) {
  16504. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  16505. return SCRIPT_CMD_FAILURE;
  16506. }
  16507. target = SELF;
  16508. }
  16509. }
  16510. StringBuf_Init(&sbuf);
  16511. StringBuf_Printf(&sbuf, "%s", message);
  16512. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  16513. StringBuf_Destroy(&sbuf);
  16514. }
  16515. return SCRIPT_CMD_SUCCESS;
  16516. }
  16517. /// Makes the unit do an emotion.
  16518. ///
  16519. /// unitemote <unit_id>,<emotion>;
  16520. ///
  16521. /// @see ET_* in script_constants.hpp
  16522. BUILDIN_FUNC(unitemote)
  16523. {
  16524. int emotion;
  16525. struct block_list* bl;
  16526. emotion = script_getnum(st,3);
  16527. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  16528. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  16529. return SCRIPT_CMD_FAILURE;
  16530. }
  16531. if (script_rid2bl(2,bl))
  16532. clif_emotion(bl, emotion);
  16533. return SCRIPT_CMD_SUCCESS;
  16534. }
  16535. /// Makes the unit cast the skill on the target or self if no target is specified.
  16536. ///
  16537. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  16538. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  16539. BUILDIN_FUNC(unitskilluseid)
  16540. {
  16541. int unit_id, target_id, casttime;
  16542. uint16 skill_id, skill_lv;
  16543. struct block_list* bl;
  16544. unit_id = script_getnum(st,2);
  16545. if (script_isstring(st, 3)) {
  16546. const char *name = script_getstr(st, 3);
  16547. if (!(skill_id = skill_name2id(name))) {
  16548. ShowError("buildin_unitskilluseid: Invalid skill name %s passed to item bonus. Skipping.\n", name);
  16549. return SCRIPT_CMD_FAILURE;
  16550. }
  16551. } else {
  16552. skill_id = script_getnum(st, 3);
  16553. if (!skill_get_index(skill_id)) {
  16554. ShowError("buildin_unitskilluseid: Invalid skill ID %d passed to item bonus. Skipping.\n", skill_id);
  16555. return SCRIPT_CMD_FAILURE;
  16556. }
  16557. }
  16558. skill_lv = script_getnum(st,4);
  16559. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  16560. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  16561. if(script_rid2bl(2,bl)){
  16562. if (bl->type == BL_NPC) {
  16563. if (!((TBL_NPC*)bl)->status.hp)
  16564. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16565. else
  16566. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16567. }
  16568. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  16569. }
  16570. return SCRIPT_CMD_SUCCESS;
  16571. }
  16572. /// Makes the unit cast the skill on the target position.
  16573. ///
  16574. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16575. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16576. BUILDIN_FUNC(unitskillusepos)
  16577. {
  16578. int skill_x, skill_y, casttime;
  16579. uint16 skill_id, skill_lv;
  16580. struct block_list* bl;
  16581. if (script_isstring(st, 3)) {
  16582. const char *name = script_getstr(st, 3);
  16583. if (!(skill_id = skill_name2id(name))) {
  16584. ShowError("buildin_unitskillusepos: Invalid skill name %s.\n", name);
  16585. return SCRIPT_CMD_FAILURE;
  16586. }
  16587. } else {
  16588. skill_id = script_getnum(st, 3);
  16589. if (!skill_get_index(skill_id)) {
  16590. ShowError("buildin_unitskillusepos: Invalid skill ID %d.\n", skill_id);
  16591. return SCRIPT_CMD_FAILURE;
  16592. }
  16593. }
  16594. skill_lv = script_getnum(st,4);
  16595. skill_x = script_getnum(st,5);
  16596. skill_y = script_getnum(st,6);
  16597. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16598. if(script_rid2bl(2,bl)){
  16599. if (bl->type == BL_NPC) {
  16600. if (!((TBL_NPC*)bl)->status.hp)
  16601. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16602. else
  16603. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16604. }
  16605. 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));
  16606. }
  16607. return SCRIPT_CMD_SUCCESS;
  16608. }
  16609. // <--- [zBuffer] List of unit control commands
  16610. /// Pauses the execution of the script, detaching the player
  16611. ///
  16612. /// sleep <milli seconds>;
  16613. BUILDIN_FUNC(sleep)
  16614. {
  16615. // First call(by function call)
  16616. if (st->sleep.tick == 0) {
  16617. int ticks;
  16618. ticks = script_getnum(st, 2);
  16619. if (ticks <= 0) {
  16620. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  16621. return SCRIPT_CMD_FAILURE;
  16622. }
  16623. // detach the player
  16624. script_detach_rid(st);
  16625. // sleep for the target amount of time
  16626. st->state = RERUNLINE;
  16627. st->sleep.tick = ticks;
  16628. // Second call(by timer after sleeping time is over)
  16629. } else {
  16630. // Continue the script
  16631. st->state = RUN;
  16632. st->sleep.tick = 0;
  16633. }
  16634. return SCRIPT_CMD_SUCCESS;
  16635. }
  16636. /// Pauses the execution of the script, keeping the unit attached
  16637. /// Stops the script if no unit is attached
  16638. ///
  16639. /// sleep2(<milli seconds>)
  16640. BUILDIN_FUNC(sleep2)
  16641. {
  16642. // First call(by function call)
  16643. if (st->sleep.tick == 0) {
  16644. int ticks;
  16645. ticks = script_getnum(st, 2);
  16646. if (ticks <= 0) {
  16647. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  16648. return SCRIPT_CMD_FAILURE;
  16649. }
  16650. if (map_id2bl(st->rid) == NULL) {
  16651. ShowError( "buildin_sleep2: no unit is attached\n" );
  16652. return SCRIPT_CMD_FAILURE;
  16653. }
  16654. // sleep for the target amount of time
  16655. st->state = RERUNLINE;
  16656. st->sleep.tick = ticks;
  16657. // Second call(by timer after sleeping time is over)
  16658. } else {
  16659. // Check if the unit is still attached
  16660. // NOTE: This should never happen, since run_script_timer already checks this
  16661. if (map_id2bl(st->rid) == NULL) {
  16662. // The unit is not attached anymore - terminate the script
  16663. st->rid = 0;
  16664. st->state = END;
  16665. } else {
  16666. // The unit is still attached - continue the script
  16667. st->state = RUN;
  16668. st->sleep.tick = 0;
  16669. }
  16670. }
  16671. return SCRIPT_CMD_SUCCESS;
  16672. }
  16673. /// Awakes all the sleep timers of the target npc
  16674. ///
  16675. /// awake "<npc name>";
  16676. BUILDIN_FUNC(awake)
  16677. {
  16678. DBIterator *iter;
  16679. struct script_state *tst;
  16680. struct npc_data* nd;
  16681. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  16682. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  16683. return SCRIPT_CMD_FAILURE;
  16684. }
  16685. int rid = st->rid;
  16686. // No need to keep the player attached if we are going to run other scripts now, where he might get attached
  16687. if( rid ){
  16688. script_detach_rid(st);
  16689. }
  16690. iter = db_iterator(st_db);
  16691. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  16692. if (tst->oid == nd->bl.id) {
  16693. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  16694. continue;
  16695. }
  16696. delete_timer(tst->sleep.timer, run_script_timer);
  16697. // Trigger the timer function
  16698. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  16699. }
  16700. }
  16701. dbi_destroy(iter);
  16702. // If a player had been attached, now is the time to restore it
  16703. if( rid ){
  16704. st->rid = rid;
  16705. script_attach_state(st);
  16706. }
  16707. return SCRIPT_CMD_SUCCESS;
  16708. }
  16709. /// Returns a reference to a variable of the target NPC.
  16710. /// Returns 0 if an error occurs.
  16711. ///
  16712. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  16713. BUILDIN_FUNC(getvariableofnpc)
  16714. {
  16715. struct script_data* data;
  16716. const char* name;
  16717. struct npc_data* nd;
  16718. data = script_getdata(st,2);
  16719. if( !data_isreference(data) )
  16720. {// Not a reference (aka varaible name)
  16721. ShowError("buildin_getvariableofnpc: not a variable\n");
  16722. script_reportdata(data);
  16723. script_pushnil(st);
  16724. st->state = END;
  16725. return SCRIPT_CMD_FAILURE;
  16726. }
  16727. name = reference_getname(data);
  16728. if( *name != '.' || name[1] == '@' )
  16729. {// not a npc variable
  16730. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  16731. script_reportdata(data);
  16732. script_pushnil(st);
  16733. st->state = END;
  16734. return SCRIPT_CMD_FAILURE;
  16735. }
  16736. nd = npc_name2id(script_getstr(st,3));
  16737. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  16738. {// NPC not found or has no script
  16739. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  16740. script_pushnil(st);
  16741. st->state = END;
  16742. return SCRIPT_CMD_FAILURE;
  16743. }
  16744. if (!nd->u.scr.script->local.vars)
  16745. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  16746. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  16747. return SCRIPT_CMD_SUCCESS;
  16748. }
  16749. /// Opens a warp portal.
  16750. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  16751. ///
  16752. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  16753. ///
  16754. /// @author blackhole89
  16755. BUILDIN_FUNC(warpportal)
  16756. {
  16757. int spx;
  16758. int spy;
  16759. unsigned short mapindex;
  16760. int tpx;
  16761. int tpy;
  16762. struct skill_unit_group* group;
  16763. struct block_list* bl;
  16764. bl = map_id2bl(st->oid);
  16765. if( bl == NULL ) {
  16766. ShowError("buildin_warpportal: NPC is needed\n");
  16767. return SCRIPT_CMD_FAILURE;
  16768. }
  16769. spx = script_getnum(st,2);
  16770. spy = script_getnum(st,3);
  16771. mapindex = mapindex_name2id(script_getstr(st, 4));
  16772. tpx = script_getnum(st,5);
  16773. tpy = script_getnum(st,6);
  16774. if( mapindex == 0 ) {
  16775. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  16776. return SCRIPT_CMD_FAILURE;
  16777. }
  16778. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  16779. if( group == NULL )
  16780. return SCRIPT_CMD_FAILURE;// failed
  16781. group->val1 = (group->val1<<16)|(short)0;
  16782. group->val2 = (tpx<<16) | tpy;
  16783. group->val3 = mapindex;
  16784. return SCRIPT_CMD_SUCCESS;
  16785. }
  16786. /**
  16787. * openmail({<char_id>});
  16788. **/
  16789. BUILDIN_FUNC(openmail)
  16790. {
  16791. TBL_PC* sd;
  16792. if (!script_charid2sd(2,sd))
  16793. return SCRIPT_CMD_FAILURE;
  16794. mail_openmail(sd);
  16795. return SCRIPT_CMD_SUCCESS;
  16796. }
  16797. /**
  16798. * openauction({<char_id>});
  16799. **/
  16800. BUILDIN_FUNC(openauction)
  16801. {
  16802. TBL_PC* sd;
  16803. if (!script_charid2sd(2,sd))
  16804. return SCRIPT_CMD_FAILURE;
  16805. if( !battle_config.feature_auction ) {
  16806. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  16807. return SCRIPT_CMD_SUCCESS;
  16808. }
  16809. clif_Auction_openwindow(sd);
  16810. return SCRIPT_CMD_SUCCESS;
  16811. }
  16812. /// Retrieves the value of the specified flag of the specified cell.
  16813. ///
  16814. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  16815. ///
  16816. /// @see cell_chk* constants in const.txt for the types
  16817. BUILDIN_FUNC(checkcell)
  16818. {
  16819. int16 m = map_mapname2mapid(script_getstr(st,2));
  16820. int16 x = script_getnum(st,3);
  16821. int16 y = script_getnum(st,4);
  16822. cell_chk type = (cell_chk)script_getnum(st,5);
  16823. script_pushint(st, map_getcell(m, x, y, type));
  16824. return SCRIPT_CMD_SUCCESS;
  16825. }
  16826. /// Modifies flags of cells in the specified area.
  16827. ///
  16828. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16829. ///
  16830. /// @see cell_* constants in const.txt for the types
  16831. BUILDIN_FUNC(setcell)
  16832. {
  16833. int16 m = map_mapname2mapid(script_getstr(st,2));
  16834. int16 x1 = script_getnum(st,3);
  16835. int16 y1 = script_getnum(st,4);
  16836. int16 x2 = script_getnum(st,5);
  16837. int16 y2 = script_getnum(st,6);
  16838. cell_t type = (cell_t)script_getnum(st,7);
  16839. bool flag = script_getnum(st,8) != 0;
  16840. int x,y;
  16841. if( x1 > x2 ) SWAP(x1,x2);
  16842. if( y1 > y2 ) SWAP(y1,y2);
  16843. for( y = y1; y <= y2; ++y )
  16844. for( x = x1; x <= x2; ++x )
  16845. map_setcell(m, x, y, type, flag);
  16846. return SCRIPT_CMD_SUCCESS;
  16847. }
  16848. /**
  16849. * Gets a free cell in the specified area.
  16850. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  16851. */
  16852. BUILDIN_FUNC(getfreecell)
  16853. {
  16854. const char *mapn = script_getstr(st, 2), *name_x, *name_y;
  16855. struct script_data
  16856. *data_x = script_getdata(st, 3),
  16857. *data_y = script_getdata(st, 4);
  16858. struct block_list* bl = map_id2bl(st->rid);
  16859. struct map_session_data *sd = nullptr;
  16860. int16 m, x = 0, y = 0;
  16861. int rx = -1, ry = -1, flag = 1;
  16862. if (!data_isreference(data_x)) {
  16863. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  16864. return SCRIPT_CMD_FAILURE;
  16865. }
  16866. if (!data_isreference(data_y)) {
  16867. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  16868. return SCRIPT_CMD_FAILURE;
  16869. }
  16870. name_x = reference_getname(data_x),
  16871. name_y = reference_getname(data_y);
  16872. if (is_string_variable(name_x)) {
  16873. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  16874. return SCRIPT_CMD_FAILURE;
  16875. }
  16876. if (is_string_variable(name_y)) {
  16877. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  16878. return SCRIPT_CMD_FAILURE;
  16879. }
  16880. if (not_server_variable(*name_x) || not_server_variable(*name_y)) {
  16881. if (!script_rid2sd(sd)) {
  16882. if (not_server_variable(*name_x))
  16883. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name_x );
  16884. else
  16885. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name_y );
  16886. return SCRIPT_CMD_FAILURE;
  16887. }
  16888. }
  16889. if (script_hasdata(st, 5))
  16890. x = script_getnum(st, 5);
  16891. if (script_hasdata(st, 6))
  16892. y = script_getnum(st, 6);
  16893. if (script_hasdata(st, 7))
  16894. rx = script_getnum(st, 7);
  16895. if (script_hasdata(st, 8))
  16896. ry = script_getnum(st, 8);
  16897. if (script_hasdata(st, 9))
  16898. flag = script_getnum(st, 9);
  16899. if (bl && strcmp(mapn, "this") == 0)
  16900. m = bl->m;
  16901. else
  16902. m = map_mapname2mapid(mapn);
  16903. map_search_freecell(bl, m, &x, &y, rx, ry, flag);
  16904. set_reg_num(st, sd, reference_getuid(data_x), name_x, x, data_x->ref);
  16905. set_reg_num(st, sd, reference_getuid(data_y), name_y, y, data_y->ref);
  16906. return SCRIPT_CMD_SUCCESS;
  16907. }
  16908. /*==========================================
  16909. * Mercenary Commands
  16910. *------------------------------------------*/
  16911. BUILDIN_FUNC(mercenary_create)
  16912. {
  16913. struct map_session_data *sd;
  16914. int class_, contract_time;
  16915. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  16916. return SCRIPT_CMD_SUCCESS;
  16917. class_ = script_getnum(st,2);
  16918. if( !mercenary_db(class_) )
  16919. return SCRIPT_CMD_SUCCESS;
  16920. contract_time = script_getnum(st,3);
  16921. mercenary_create(sd, class_, contract_time);
  16922. return SCRIPT_CMD_SUCCESS;
  16923. }
  16924. BUILDIN_FUNC(mercenary_heal)
  16925. {
  16926. struct map_session_data *sd;
  16927. int hp, sp;
  16928. if( !script_rid2sd(sd) || sd->md == NULL )
  16929. return SCRIPT_CMD_SUCCESS;
  16930. hp = script_getnum(st,2);
  16931. sp = script_getnum(st,3);
  16932. status_heal(&sd->md->bl, hp, sp, 0);
  16933. return SCRIPT_CMD_SUCCESS;
  16934. }
  16935. BUILDIN_FUNC(mercenary_sc_start)
  16936. {
  16937. struct map_session_data *sd;
  16938. enum sc_type type;
  16939. int tick, val1;
  16940. if( !script_rid2sd(sd) || sd->md == NULL )
  16941. return SCRIPT_CMD_SUCCESS;
  16942. type = (sc_type)script_getnum(st,2);
  16943. tick = script_getnum(st,3);
  16944. val1 = script_getnum(st,4);
  16945. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16946. return SCRIPT_CMD_SUCCESS;
  16947. }
  16948. BUILDIN_FUNC(mercenary_get_calls)
  16949. {
  16950. struct map_session_data *sd;
  16951. int guild;
  16952. if( !script_rid2sd(sd) )
  16953. return SCRIPT_CMD_SUCCESS;
  16954. guild = script_getnum(st,2);
  16955. switch( guild )
  16956. {
  16957. case ARCH_MERC_GUILD:
  16958. script_pushint(st,sd->status.arch_calls);
  16959. break;
  16960. case SPEAR_MERC_GUILD:
  16961. script_pushint(st,sd->status.spear_calls);
  16962. break;
  16963. case SWORD_MERC_GUILD:
  16964. script_pushint(st,sd->status.sword_calls);
  16965. break;
  16966. default:
  16967. script_pushint(st,0);
  16968. break;
  16969. }
  16970. return SCRIPT_CMD_SUCCESS;
  16971. }
  16972. BUILDIN_FUNC(mercenary_set_calls)
  16973. {
  16974. struct map_session_data *sd;
  16975. int guild, value, *calls;
  16976. if( !script_rid2sd(sd) )
  16977. return SCRIPT_CMD_SUCCESS;
  16978. guild = script_getnum(st,2);
  16979. value = script_getnum(st,3);
  16980. switch( guild )
  16981. {
  16982. case ARCH_MERC_GUILD:
  16983. calls = &sd->status.arch_calls;
  16984. break;
  16985. case SPEAR_MERC_GUILD:
  16986. calls = &sd->status.spear_calls;
  16987. break;
  16988. case SWORD_MERC_GUILD:
  16989. calls = &sd->status.sword_calls;
  16990. break;
  16991. default:
  16992. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16993. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16994. }
  16995. *calls += value;
  16996. *calls = cap_value(*calls, 0, INT_MAX);
  16997. return SCRIPT_CMD_SUCCESS;
  16998. }
  16999. BUILDIN_FUNC(mercenary_get_faith)
  17000. {
  17001. struct map_session_data *sd;
  17002. int guild;
  17003. if( !script_rid2sd(sd) )
  17004. return SCRIPT_CMD_SUCCESS;
  17005. guild = script_getnum(st,2);
  17006. switch( guild )
  17007. {
  17008. case ARCH_MERC_GUILD:
  17009. script_pushint(st,sd->status.arch_faith);
  17010. break;
  17011. case SPEAR_MERC_GUILD:
  17012. script_pushint(st,sd->status.spear_faith);
  17013. break;
  17014. case SWORD_MERC_GUILD:
  17015. script_pushint(st,sd->status.sword_faith);
  17016. break;
  17017. default:
  17018. script_pushint(st,0);
  17019. break;
  17020. }
  17021. return SCRIPT_CMD_SUCCESS;
  17022. }
  17023. BUILDIN_FUNC(mercenary_set_faith)
  17024. {
  17025. struct map_session_data *sd;
  17026. int guild, value, *calls;
  17027. if( !script_rid2sd(sd) )
  17028. return SCRIPT_CMD_SUCCESS;
  17029. guild = script_getnum(st,2);
  17030. value = script_getnum(st,3);
  17031. switch( guild )
  17032. {
  17033. case ARCH_MERC_GUILD:
  17034. calls = &sd->status.arch_faith;
  17035. break;
  17036. case SPEAR_MERC_GUILD:
  17037. calls = &sd->status.spear_faith;
  17038. break;
  17039. case SWORD_MERC_GUILD:
  17040. calls = &sd->status.sword_faith;
  17041. break;
  17042. default:
  17043. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  17044. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  17045. }
  17046. *calls += value;
  17047. *calls = cap_value(*calls, 0, INT_MAX);
  17048. if( mercenary_get_guild(sd->md) == guild )
  17049. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  17050. return SCRIPT_CMD_SUCCESS;
  17051. }
  17052. /*------------------------------------------
  17053. * Book Reading
  17054. *------------------------------------------*/
  17055. BUILDIN_FUNC(readbook)
  17056. {
  17057. struct map_session_data *sd;
  17058. int book_id, page;
  17059. if( !script_rid2sd(sd) )
  17060. return SCRIPT_CMD_SUCCESS;
  17061. book_id = script_getnum(st,2);
  17062. page = script_getnum(st,3);
  17063. clif_readbook(sd->fd, book_id, page);
  17064. return SCRIPT_CMD_SUCCESS;
  17065. }
  17066. /******************
  17067. Questlog script commands
  17068. *******************/
  17069. /// questinfo <Icon>{,<Map Mark Color>{,<condition>}};
  17070. BUILDIN_FUNC(questinfo)
  17071. {
  17072. TBL_NPC* nd = map_id2nd(st->oid);
  17073. if (!nd || nd->bl.m == -1) {
  17074. ShowError("buildin_questinfo: No NPC attached.\n");
  17075. return SCRIPT_CMD_FAILURE;
  17076. }
  17077. int icon = script_getnum(st, 2);
  17078. #if PACKETVER >= 20120410
  17079. switch(icon){
  17080. case QTYPE_QUEST:
  17081. case QTYPE_QUEST2:
  17082. case QTYPE_JOB:
  17083. case QTYPE_JOB2:
  17084. case QTYPE_EVENT:
  17085. case QTYPE_EVENT2:
  17086. // Warg icons were replaced in this client
  17087. #if PACKETVER < 20170315
  17088. case QTYPE_WARG:
  17089. case QTYPE_WARG2:
  17090. #else
  17091. case QTYPE_CLICKME:
  17092. case QTYPE_DAILYQUEST:
  17093. case QTYPE_EVENT3:
  17094. case QTYPE_JOBQUEST:
  17095. case QTYPE_JUMPING_PORING:
  17096. #endif
  17097. // Leave everything as it is
  17098. break;
  17099. case QTYPE_NONE:
  17100. default:
  17101. // Default to nothing if icon id is invalid.
  17102. icon = QTYPE_NONE;
  17103. break;
  17104. }
  17105. #else
  17106. if (icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  17107. icon = QTYPE_QUEST;
  17108. else
  17109. icon = icon + 1;
  17110. #endif
  17111. int color = QMARK_NONE;
  17112. if (script_hasdata(st, 3)) {
  17113. color = script_getnum(st, 3);
  17114. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17115. ShowWarning("buildin_questinfo: invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17116. script_reportfunc(st);
  17117. color = QMARK_NONE;
  17118. }
  17119. }
  17120. struct script_code *script = nullptr;
  17121. if (script_hasdata(st, 4)) {
  17122. const char *str = script_getstr(st, 4);
  17123. if (str) {
  17124. std::string condition(str);
  17125. if (condition.find( "achievement_condition" ) == std::string::npos)
  17126. condition = "achievement_condition( " + condition + " );";
  17127. script = parse_script(condition.c_str(), "questinfoparsing", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  17128. if (!script) {
  17129. st->state = END;
  17130. return SCRIPT_CMD_FAILURE;
  17131. }
  17132. }
  17133. }
  17134. std::shared_ptr<s_questinfo> qi = std::make_shared<s_questinfo>();
  17135. qi->icon = static_cast<e_questinfo_types>(icon);
  17136. qi->color = static_cast<e_questinfo_markcolor>(color);
  17137. qi->condition = script;
  17138. nd->qi_data.push_back(qi);
  17139. struct map_data *mapdata = map_getmapdata(nd->bl.m);
  17140. if (mapdata && !util::vector_exists(mapdata->qi_npc, nd->bl.id))
  17141. mapdata->qi_npc.push_back(nd->bl.id);
  17142. return SCRIPT_CMD_SUCCESS;
  17143. }
  17144. /**
  17145. * questinfo_refresh {<char_id>};
  17146. **/
  17147. BUILDIN_FUNC(questinfo_refresh)
  17148. {
  17149. struct map_session_data *sd;
  17150. if (!script_charid2sd(2, sd))
  17151. return SCRIPT_CMD_FAILURE;
  17152. pc_show_questinfo(sd);
  17153. return SCRIPT_CMD_SUCCESS;
  17154. }
  17155. /**
  17156. * setquest <ID>{,<char_id>};
  17157. **/
  17158. BUILDIN_FUNC(setquest)
  17159. {
  17160. struct map_session_data *sd;
  17161. int quest_id;
  17162. quest_id = script_getnum(st, 2);
  17163. if (!script_charid2sd(3,sd))
  17164. return SCRIPT_CMD_FAILURE;
  17165. if( quest_add(sd, quest_id) == -1 ){
  17166. script_reportsrc(st);
  17167. script_reportfunc(st);
  17168. }
  17169. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17170. pc_show_questinfo(sd);
  17171. return SCRIPT_CMD_SUCCESS;
  17172. }
  17173. /**
  17174. * erasequest <ID>{,<char_id>};
  17175. **/
  17176. BUILDIN_FUNC(erasequest)
  17177. {
  17178. struct map_session_data *sd;
  17179. if (!script_charid2sd(3,sd))
  17180. return SCRIPT_CMD_FAILURE;
  17181. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17182. script_reportsrc(st);
  17183. script_reportfunc(st);
  17184. }
  17185. return SCRIPT_CMD_SUCCESS;
  17186. }
  17187. /**
  17188. * completequest <ID>{,<char_id>};
  17189. **/
  17190. BUILDIN_FUNC(completequest)
  17191. {
  17192. struct map_session_data *sd;
  17193. if (!script_charid2sd(3,sd))
  17194. return SCRIPT_CMD_FAILURE;
  17195. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17196. //20120410 or 20090218
  17197. pc_show_questinfo(sd);
  17198. return SCRIPT_CMD_SUCCESS;
  17199. }
  17200. /**
  17201. * changequest <ID>,<ID2>{,<char_id>};
  17202. **/
  17203. BUILDIN_FUNC(changequest)
  17204. {
  17205. struct map_session_data *sd;
  17206. if (!script_charid2sd(4,sd))
  17207. return SCRIPT_CMD_FAILURE;
  17208. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17209. script_reportsrc(st);
  17210. script_reportfunc(st);
  17211. }
  17212. //20120410 or 20090218
  17213. pc_show_questinfo(sd);
  17214. return SCRIPT_CMD_SUCCESS;
  17215. }
  17216. /**
  17217. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17218. **/
  17219. BUILDIN_FUNC(checkquest)
  17220. {
  17221. struct map_session_data *sd;
  17222. e_quest_check_type type = HAVEQUEST;
  17223. if( script_hasdata(st, 3) )
  17224. type = (e_quest_check_type)script_getnum(st, 3);
  17225. if (!script_charid2sd(4,sd))
  17226. return SCRIPT_CMD_FAILURE;
  17227. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17228. return SCRIPT_CMD_SUCCESS;
  17229. }
  17230. /**
  17231. * isbegin_quest(<ID>{,<char_id>})
  17232. **/
  17233. BUILDIN_FUNC(isbegin_quest)
  17234. {
  17235. struct map_session_data *sd;
  17236. if (!script_charid2sd(3,sd))
  17237. return SCRIPT_CMD_FAILURE;
  17238. int i = quest_check(sd, script_getnum(st, 2), HAVEQUEST);
  17239. script_pushint(st, i + (i < 1));
  17240. return SCRIPT_CMD_SUCCESS;
  17241. }
  17242. /**
  17243. * showevent <icon>{,<mark color>{,<char_id>}}
  17244. **/
  17245. BUILDIN_FUNC(showevent)
  17246. {
  17247. TBL_PC *sd;
  17248. if (!script_charid2sd(4,sd))
  17249. return SCRIPT_CMD_FAILURE;
  17250. struct npc_data *nd = map_id2nd(st->oid);
  17251. if (!nd)
  17252. return SCRIPT_CMD_SUCCESS;
  17253. int color = QMARK_NONE;
  17254. int icon = script_getnum(st, 2);
  17255. if (script_hasdata(st, 3)) {
  17256. color = script_getnum(st, 3);
  17257. if (color < QMARK_NONE || color >= QMARK_MAX) {
  17258. ShowWarning("buildin_showevent: Invalid color '%d', defaulting to QMARK_NONE.\n",color);
  17259. script_reportfunc(st);
  17260. color = QMARK_NONE;
  17261. }
  17262. }
  17263. #if PACKETVER >= 20120410
  17264. if (icon < 0 || (icon > 8 && icon != QTYPE_NONE) || icon == 7)
  17265. icon = QTYPE_NONE; // Default to nothing if icon id is invalid.
  17266. #else
  17267. if (icon < 0 || icon > 7)
  17268. icon = 0;
  17269. else
  17270. icon = icon + 1;
  17271. #endif
  17272. clif_quest_show_event(sd, &nd->bl, static_cast<e_questinfo_types>(icon), static_cast<e_questinfo_markcolor>(color));
  17273. return SCRIPT_CMD_SUCCESS;
  17274. }
  17275. /*==========================================
  17276. * BattleGround System
  17277. *------------------------------------------*/
  17278. BUILDIN_FUNC(waitingroom2bg)
  17279. {
  17280. struct npc_data *nd;
  17281. struct chat_data *cd;
  17282. const char *map_name;
  17283. int mapindex = 0, bg_id;
  17284. unsigned char i,c=0;
  17285. struct s_battleground_team team;
  17286. if( script_hasdata(st,7) )
  17287. nd = npc_name2id(script_getstr(st,7));
  17288. else
  17289. nd = (struct npc_data *)map_id2bl(st->oid);
  17290. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  17291. {
  17292. script_pushint(st,0);
  17293. return SCRIPT_CMD_SUCCESS;
  17294. }
  17295. map_name = script_getstr(st,2);
  17296. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17297. { // Invalid Map
  17298. script_pushint(st, 0);
  17299. return SCRIPT_CMD_SUCCESS;
  17300. }
  17301. team.warp_x = script_getnum(st,3);
  17302. team.warp_y = script_getnum(st,4);
  17303. if (script_hasdata(st,5)) {
  17304. team.quit_event = script_getstr(st,5); // Logout Event
  17305. check_event(st, team.quit_event.c_str());
  17306. } else
  17307. team.quit_event = "";
  17308. if (script_hasdata(st,6)) {
  17309. team.death_event = script_getstr(st,6); // Die Event
  17310. check_event(st, team.death_event.c_str());
  17311. } else
  17312. team.death_event = "";
  17313. if( (bg_id = bg_create(mapindex, &team)) == 0 )
  17314. { // Creation failed
  17315. script_pushint(st,0);
  17316. return SCRIPT_CMD_SUCCESS;
  17317. }
  17318. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  17319. struct map_session_data *sd;
  17320. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd, false) ){
  17321. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  17322. ++c;
  17323. }
  17324. }
  17325. mapreg_setreg(add_str("$@arenamembersnum"), c);
  17326. script_pushint(st,bg_id);
  17327. return SCRIPT_CMD_SUCCESS;
  17328. }
  17329. BUILDIN_FUNC(waitingroom2bg_single)
  17330. {
  17331. const char* map_name;
  17332. struct npc_data *nd;
  17333. struct chat_data *cd;
  17334. struct map_session_data *sd;
  17335. int x, y, mapindex, bg_id = script_getnum(st,2);
  17336. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17337. if (!bg) {
  17338. script_pushint(st, false);
  17339. return SCRIPT_CMD_SUCCESS;
  17340. }
  17341. if (script_hasdata(st, 3)) {
  17342. map_name = script_getstr(st, 3);
  17343. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17344. script_pushint(st, false);
  17345. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17346. }
  17347. x = script_getnum(st, 4);
  17348. y = script_getnum(st, 5);
  17349. }
  17350. else {
  17351. mapindex = bg->cemetery.map;
  17352. x = bg->cemetery.x;
  17353. y = bg->cemetery.y;
  17354. }
  17355. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17356. ShowWarning("buildin_waitingroom2bg_single: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17357. script_pushint(st, false);
  17358. return SCRIPT_CMD_FAILURE;
  17359. }
  17360. nd = npc_name2id(script_getstr(st,6));
  17361. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  17362. return SCRIPT_CMD_SUCCESS;
  17363. if( (sd = cd->usersd[0]) == NULL )
  17364. return SCRIPT_CMD_SUCCESS;
  17365. if( bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17366. {
  17367. script_pushint(st, true);
  17368. }
  17369. else
  17370. script_pushint(st, false);
  17371. return SCRIPT_CMD_SUCCESS;
  17372. }
  17373. /// Creates an instance of battleground battle group.
  17374. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  17375. /// @author [secretdataz]
  17376. BUILDIN_FUNC(bg_create) {
  17377. const char *map_name;
  17378. int mapindex = 0;
  17379. struct s_battleground_team team;
  17380. map_name = script_getstr(st, 2);
  17381. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17382. { // Invalid Map
  17383. script_pushint(st, 0);
  17384. return SCRIPT_CMD_SUCCESS;
  17385. }
  17386. team.warp_x = script_getnum(st,3);
  17387. team.warp_y = script_getnum(st,4);
  17388. if (script_hasdata(st,5)) {
  17389. team.quit_event = script_getstr(st,5); // Logout Event
  17390. check_event(st, team.quit_event.c_str());
  17391. } else
  17392. team.quit_event = "";
  17393. if (script_hasdata(st,6)) {
  17394. team.death_event = script_getstr(st,6); // Die Event
  17395. check_event(st, team.death_event.c_str());
  17396. } else
  17397. team.death_event = "";
  17398. script_pushint(st, bg_create(mapindex, &team));
  17399. return SCRIPT_CMD_SUCCESS;
  17400. }
  17401. /// Adds attached player or <char id> (if specified) to an existing
  17402. /// battleground group and warps it to the specified coordinates on
  17403. /// the given map.
  17404. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  17405. /// @author [secretdataz]
  17406. BUILDIN_FUNC(bg_join) {
  17407. const char* map_name;
  17408. struct map_session_data *sd;
  17409. int x, y, mapindex, bg_id = script_getnum(st, 2);
  17410. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17411. if (!bg) {
  17412. script_pushint(st, false);
  17413. return SCRIPT_CMD_SUCCESS;
  17414. }
  17415. if (script_hasdata(st, 3)) {
  17416. map_name = script_getstr(st, 3);
  17417. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17418. script_pushint(st, false);
  17419. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17420. }
  17421. x = script_getnum(st, 4);
  17422. y = script_getnum(st, 5);
  17423. } else {
  17424. mapindex = bg->cemetery.map;
  17425. x = bg->cemetery.x;
  17426. y = bg->cemetery.y;
  17427. }
  17428. if (!script_charid2sd(6, sd)) {
  17429. script_pushint(st, false);
  17430. return SCRIPT_CMD_FAILURE;
  17431. }
  17432. if (!map_getmapflag(map_mapindex2mapid(mapindex), MF_BATTLEGROUND)) {
  17433. ShowWarning("buildin_bg_join: Map %s requires the mapflag MF_BATTLEGROUND.\n", mapindex_id2name(mapindex));
  17434. script_pushint(st, false);
  17435. return SCRIPT_CMD_FAILURE;
  17436. }
  17437. if (bg_team_join(bg_id, sd, false) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17438. {
  17439. script_pushint(st, true);
  17440. }
  17441. else
  17442. script_pushint(st, false);
  17443. return SCRIPT_CMD_SUCCESS;
  17444. }
  17445. BUILDIN_FUNC(bg_team_setxy)
  17446. {
  17447. int bg_id = script_getnum(st,2);
  17448. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17449. if (bg) {
  17450. bg->cemetery.x = script_getnum(st, 3);
  17451. bg->cemetery.y = script_getnum(st, 4);
  17452. }
  17453. return SCRIPT_CMD_SUCCESS;
  17454. }
  17455. BUILDIN_FUNC(bg_warp)
  17456. {
  17457. int x, y, mapindex, bg_id;
  17458. const char* map_name;
  17459. bg_id = script_getnum(st,2);
  17460. map_name = script_getstr(st,3);
  17461. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  17462. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17463. x = script_getnum(st,4);
  17464. y = script_getnum(st,5);
  17465. bg_team_warp(bg_id, mapindex, x, y);
  17466. return SCRIPT_CMD_SUCCESS;
  17467. }
  17468. BUILDIN_FUNC(bg_monster)
  17469. {
  17470. int class_ = 0, x = 0, y = 0, bg_id = 0;
  17471. const char *str,*mapname, *evt="";
  17472. bg_id = script_getnum(st,2);
  17473. mapname = script_getstr(st,3);
  17474. x = script_getnum(st,4);
  17475. y = script_getnum(st,5);
  17476. str = script_getstr(st,6);
  17477. class_ = script_getnum(st,7);
  17478. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  17479. check_event(st, evt);
  17480. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  17481. return SCRIPT_CMD_SUCCESS;
  17482. }
  17483. BUILDIN_FUNC(bg_monster_set_team)
  17484. {
  17485. struct mob_data *md;
  17486. struct block_list *mbl;
  17487. int id = script_getnum(st,2),
  17488. bg_id = script_getnum(st,3);
  17489. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  17490. return SCRIPT_CMD_SUCCESS;
  17491. md = (TBL_MOB *)mbl;
  17492. md->bg_id = bg_id;
  17493. mob_stop_attack(md);
  17494. mob_stop_walking(md, 0);
  17495. md->target_id = md->attacked_id = 0;
  17496. clif_name_area(&md->bl);
  17497. return SCRIPT_CMD_SUCCESS;
  17498. }
  17499. BUILDIN_FUNC(bg_leave)
  17500. {
  17501. struct map_session_data *sd = NULL;
  17502. bool deserter = false;
  17503. if( !script_charid2sd(2,sd) || !sd->bg_id )
  17504. return SCRIPT_CMD_SUCCESS;
  17505. if (!strcmp(script_getfuncname(st), "bg_desert"))
  17506. deserter = true;
  17507. bg_team_leave(sd, false, deserter);
  17508. return SCRIPT_CMD_SUCCESS;
  17509. }
  17510. BUILDIN_FUNC(bg_destroy)
  17511. {
  17512. int bg_id = script_getnum(st,2);
  17513. bg_team_delete(bg_id);
  17514. return SCRIPT_CMD_SUCCESS;
  17515. }
  17516. BUILDIN_FUNC(bg_getareausers)
  17517. {
  17518. const char *str = script_getstr(st, 3);
  17519. int16 m, x0, y0, x1, y1;
  17520. int bg_id = script_getnum(st, 2), c = 0;
  17521. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17522. if (!bg || (m = map_mapname2mapid(str)) < 0) {
  17523. script_pushint(st,0);
  17524. return SCRIPT_CMD_SUCCESS;
  17525. }
  17526. x0 = script_getnum(st,4);
  17527. y0 = script_getnum(st,5);
  17528. x1 = script_getnum(st,6);
  17529. y1 = script_getnum(st,7);
  17530. for (const auto &member : bg->members) {
  17531. 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 )
  17532. continue;
  17533. c++;
  17534. }
  17535. script_pushint(st,c);
  17536. return SCRIPT_CMD_SUCCESS;
  17537. }
  17538. BUILDIN_FUNC(bg_updatescore)
  17539. {
  17540. const char *str;
  17541. int16 m;
  17542. str = script_getstr(st,2);
  17543. if( (m = map_mapname2mapid(str)) < 0 )
  17544. return SCRIPT_CMD_SUCCESS;
  17545. struct map_data *mapdata = map_getmapdata(m);
  17546. mapdata->bgscore_lion = script_getnum(st,3);
  17547. mapdata->bgscore_eagle = script_getnum(st,4);
  17548. clif_bg_updatescore(m);
  17549. return SCRIPT_CMD_SUCCESS;
  17550. }
  17551. BUILDIN_FUNC(bg_get_data)
  17552. {
  17553. int bg_id = script_getnum(st,2), type = script_getnum(st,3), i = 0;
  17554. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_id);
  17555. if (bg) {
  17556. switch (type) {
  17557. case 0:
  17558. script_pushint(st, bg->members.size());
  17559. break;
  17560. case 1:
  17561. for (const auto &member : bg->members)
  17562. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i++), member.sd->bl.id);
  17563. mapreg_setreg(add_str("$@arenamemberscount"), i);
  17564. script_pushint(st, i);
  17565. break;
  17566. default:
  17567. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  17568. break;
  17569. }
  17570. } else
  17571. script_pushint(st, 0);
  17572. return SCRIPT_CMD_SUCCESS;
  17573. }
  17574. /**
  17575. * Reserves a slot for the given Battleground.
  17576. * bg_reserve("<battleground_map_name>"{,<ended>});
  17577. */
  17578. BUILDIN_FUNC(bg_reserve)
  17579. {
  17580. const char *str = script_getstr(st, 2);
  17581. bool ended = script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false;
  17582. if (!bg_queue_reserve(str, ended))
  17583. ShowWarning("buildin_bg_reserve: Could not reserve battleground with name %s\n", str);
  17584. return SCRIPT_CMD_SUCCESS;
  17585. }
  17586. /**
  17587. * Removes a spot for the given Battleground.
  17588. * bg_unbook("<battleground_map_name>");
  17589. */
  17590. BUILDIN_FUNC(bg_unbook)
  17591. {
  17592. const char *str = script_getstr(st, 2);
  17593. if (!bg_queue_unbook(str))
  17594. ShowWarning("buildin_bg_unbook: Could not unreserve battleground with name %s\n", str);
  17595. return SCRIPT_CMD_SUCCESS;
  17596. }
  17597. /**
  17598. * Gets battleground database information.
  17599. * bg_info("<battleground name>", <type>);
  17600. */
  17601. BUILDIN_FUNC(bg_info)
  17602. {
  17603. std::shared_ptr<s_battleground_type> bg = bg_search_name(script_getstr(st, 2));
  17604. if (!bg) {
  17605. ShowError("bg_info: Invalid Battleground name %s.\n", script_getstr(st, 2));
  17606. return SCRIPT_CMD_FAILURE;
  17607. }
  17608. int type = script_getnum(st, 3);
  17609. switch (type) {
  17610. case BG_INFO_ID:
  17611. script_pushint(st, bg->id);
  17612. break;
  17613. case BG_INFO_REQUIRED_PLAYERS:
  17614. script_pushint(st, bg->required_players);
  17615. break;
  17616. case BG_INFO_MAX_PLAYERS:
  17617. script_pushint(st, bg->max_players);
  17618. break;
  17619. case BG_INFO_MIN_LEVEL:
  17620. script_pushint(st, bg->min_lvl);
  17621. break;
  17622. case BG_INFO_MAX_LEVEL:
  17623. script_pushint(st, bg->max_lvl);
  17624. break;
  17625. case BG_INFO_MAPS: {
  17626. size_t i;
  17627. for (i = 0; i < bg->maps.size(); i++)
  17628. setd_sub_str(st, nullptr, ".@bgmaps$", i, mapindex_id2name(bg->maps[i].mapindex), nullptr);
  17629. setd_sub_num(st, nullptr, ".@bgmapscount", 0, i, nullptr);
  17630. script_pushint(st, i);
  17631. break;
  17632. }
  17633. case BG_INFO_DESERTER_TIME:
  17634. script_pushint(st, bg->deserter_time);
  17635. break;
  17636. default:
  17637. ShowError("bg_info: Unknown battleground info type %d given.\n", type);
  17638. return SCRIPT_CMD_FAILURE;
  17639. }
  17640. return SCRIPT_CMD_SUCCESS;
  17641. }
  17642. /*==========================================
  17643. * Instancing System
  17644. *------------------------------------------*/
  17645. /**
  17646. * Returns an Instance ID.
  17647. * @param st: Script state
  17648. * @param mode: Instance mode
  17649. * @return instance ID on success or 0 otherwise
  17650. */
  17651. int script_instancegetid(struct script_state* st, e_instance_mode mode)
  17652. {
  17653. int instance_id = 0;
  17654. if (mode == IM_NONE) {
  17655. struct npc_data *nd = map_id2nd(st->oid);
  17656. if (nd->instance_id > 0)
  17657. instance_id = nd->instance_id;
  17658. } else {
  17659. struct map_session_data *sd = map_id2sd(st->rid);
  17660. if (sd) {
  17661. switch (mode) {
  17662. case IM_CHAR:
  17663. if (sd->instance_id > 0)
  17664. instance_id = sd->instance_id;
  17665. break;
  17666. case IM_PARTY: {
  17667. struct party_data *pd = party_search(sd->status.party_id);
  17668. if (pd && pd->instance_id > 0)
  17669. instance_id = pd->instance_id;
  17670. }
  17671. break;
  17672. case IM_GUILD: {
  17673. struct guild *gd = guild_search(sd->status.guild_id);
  17674. if (gd && gd->instance_id > 0)
  17675. instance_id = gd->instance_id;
  17676. }
  17677. break;
  17678. case IM_CLAN: {
  17679. struct clan *cd = clan_search(sd->status.clan_id);
  17680. if (cd && cd->instance_id > 0)
  17681. instance_id = cd->instance_id;
  17682. }
  17683. break;
  17684. default: // Unsupported type
  17685. break;
  17686. }
  17687. }
  17688. }
  17689. return instance_id;
  17690. }
  17691. /*==========================================
  17692. * Creates the instance
  17693. * Returns Instance ID if created successfully
  17694. *------------------------------------------*/
  17695. BUILDIN_FUNC(instance_create)
  17696. {
  17697. e_instance_mode mode = IM_PARTY;
  17698. int owner_id = 0;
  17699. if (script_hasdata(st, 3)) {
  17700. mode = static_cast<e_instance_mode>(script_getnum(st, 3));
  17701. if (mode < IM_NONE || mode >= IM_MAX) {
  17702. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  17703. return SCRIPT_CMD_FAILURE;
  17704. }
  17705. }
  17706. if (script_hasdata(st, 4))
  17707. owner_id = script_getnum(st, 4);
  17708. else {
  17709. // If sd is NULL, instance_create will return -2.
  17710. struct map_session_data *sd = NULL;
  17711. switch(mode) {
  17712. case IM_NONE:
  17713. owner_id = st->oid;
  17714. break;
  17715. case IM_CHAR:
  17716. if (script_rid2sd(sd))
  17717. owner_id = sd->status.char_id;
  17718. break;
  17719. case IM_PARTY:
  17720. if (script_rid2sd(sd))
  17721. owner_id = sd->status.party_id;
  17722. break;
  17723. case IM_GUILD:
  17724. if (script_rid2sd(sd))
  17725. owner_id = sd->status.guild_id;
  17726. break;
  17727. case IM_CLAN:
  17728. if (script_rid2sd(sd))
  17729. owner_id = sd->status.clan_id;
  17730. break;
  17731. default:
  17732. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  17733. return SCRIPT_CMD_FAILURE;
  17734. }
  17735. }
  17736. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  17737. return SCRIPT_CMD_SUCCESS;
  17738. }
  17739. /*==========================================
  17740. * Destroys an instance (unofficial)
  17741. * Officially instances are only destroyed by timeout
  17742. *
  17743. * instance_destroy {<instance_id>};
  17744. *------------------------------------------*/
  17745. BUILDIN_FUNC(instance_destroy)
  17746. {
  17747. int instance_id;
  17748. if( script_hasdata(st,2) )
  17749. instance_id = script_getnum(st,2);
  17750. else
  17751. instance_id = script_instancegetid(st);
  17752. if( instance_id == 0 ) {
  17753. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  17754. return SCRIPT_CMD_FAILURE;
  17755. }
  17756. instance_destroy(instance_id);
  17757. return SCRIPT_CMD_SUCCESS;
  17758. }
  17759. /*==========================================
  17760. * Warps player to instance
  17761. * Results:
  17762. * IE_OK: Success
  17763. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  17764. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  17765. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  17766. *------------------------------------------*/
  17767. BUILDIN_FUNC(instance_enter)
  17768. {
  17769. struct map_session_data *sd = NULL;
  17770. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  17771. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  17772. int instance_id;
  17773. if (script_hasdata(st, 6))
  17774. instance_id = script_getnum(st, 6);
  17775. else
  17776. instance_id = script_instancegetid(st, IM_PARTY);
  17777. if (!script_charid2sd(5,sd))
  17778. return SCRIPT_CMD_FAILURE;
  17779. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  17780. return SCRIPT_CMD_SUCCESS;
  17781. }
  17782. /*==========================================
  17783. * Returns the name of a duplicated NPC
  17784. *
  17785. * instance_npcname <npc_name>{,<instance_id>};
  17786. * <npc_name> is the full name of an NPC.
  17787. *------------------------------------------*/
  17788. BUILDIN_FUNC(instance_npcname)
  17789. {
  17790. const char *str;
  17791. int instance_id;
  17792. struct npc_data *nd;
  17793. str = script_getstr(st,2);
  17794. if( script_hasdata(st,3) )
  17795. instance_id = script_getnum(st,3);
  17796. else
  17797. instance_id = script_instancegetid(st);
  17798. if( instance_id > 0 && (nd = npc_name2id(str)) != NULL ) {
  17799. static char npcname[NAME_LENGTH];
  17800. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  17801. script_pushconststr(st,npcname);
  17802. } else {
  17803. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  17804. st->state = END;
  17805. return SCRIPT_CMD_FAILURE;
  17806. }
  17807. return SCRIPT_CMD_SUCCESS;
  17808. }
  17809. /*==========================================
  17810. * Returns the name of a duplicated map
  17811. *
  17812. * instance_mapname <map_name>{,<instance_id>};
  17813. *------------------------------------------*/
  17814. BUILDIN_FUNC(instance_mapname)
  17815. {
  17816. const char *str;
  17817. int16 m;
  17818. int instance_id;
  17819. str = script_getstr(st,2);
  17820. if( script_hasdata(st,3) )
  17821. instance_id = script_getnum(st,3);
  17822. else
  17823. instance_id = script_instancegetid(st);
  17824. // Check that instance mapname is a valid map
  17825. if(instance_id <= 0 || (m = instance_mapid(map_mapname2mapid(str), instance_id)) < 0)
  17826. script_pushconststr(st, "");
  17827. else
  17828. script_pushconststr(st, map_getmapdata(m)->name);
  17829. return SCRIPT_CMD_SUCCESS;
  17830. }
  17831. /*==========================================
  17832. * Returns an Instance ID
  17833. *------------------------------------------*/
  17834. BUILDIN_FUNC(instance_id)
  17835. {
  17836. int mode = IM_NONE; // Default to the attached NPC
  17837. if (script_hasdata(st, 2)) {
  17838. mode = script_getnum(st, 2);
  17839. if (mode <= IM_NONE || mode >= IM_MAX) {
  17840. ShowError("buildin_instance_id: Unknown instance mode %d.\n", mode);
  17841. script_pushint(st, 0);
  17842. return SCRIPT_CMD_SUCCESS;
  17843. }
  17844. }
  17845. script_pushint(st, script_instancegetid(st, static_cast<e_instance_mode>(mode)));
  17846. return SCRIPT_CMD_SUCCESS;
  17847. }
  17848. /*==========================================
  17849. * Warps all players inside an instance
  17850. *
  17851. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  17852. *------------------------------------------*/
  17853. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  17854. {
  17855. unsigned int m = va_arg(ap,unsigned int);
  17856. int x = va_arg(ap,int);
  17857. int y = va_arg(ap,int);
  17858. int instance_id = va_arg(ap, int);
  17859. struct map_session_data *sd;
  17860. nullpo_retr(0, bl);
  17861. if (bl->type != BL_PC)
  17862. return 0;
  17863. sd = (TBL_PC *)bl;
  17864. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  17865. if (!idata)
  17866. return 0;
  17867. int owner_id = idata->owner_id;
  17868. switch(idata->mode) {
  17869. case IM_NONE:
  17870. break;
  17871. case IM_CHAR:
  17872. if (sd->status.char_id != owner_id)
  17873. return 0;
  17874. break;
  17875. case IM_PARTY:
  17876. if (sd->status.party_id != owner_id)
  17877. return 0;
  17878. break;
  17879. case IM_GUILD:
  17880. if (sd->status.guild_id != owner_id)
  17881. return 0;
  17882. case IM_CLAN:
  17883. if (sd->status.clan_id != owner_id)
  17884. return 0;
  17885. }
  17886. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  17887. return 1;
  17888. }
  17889. BUILDIN_FUNC(instance_warpall)
  17890. {
  17891. int16 m;
  17892. int instance_id;
  17893. const char *mapn;
  17894. int x, y;
  17895. mapn = script_getstr(st,2);
  17896. x = script_getnum(st,3);
  17897. y = script_getnum(st,4);
  17898. if( script_hasdata(st,5) )
  17899. instance_id = script_getnum(st,5);
  17900. else
  17901. instance_id = script_instancegetid(st, IM_PARTY);
  17902. if( instance_id <= 0 || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapid(m, instance_id)) < 0)
  17903. return SCRIPT_CMD_FAILURE;
  17904. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  17905. if (!idata) {
  17906. ShowError("buildin_instance_warpall: Instance is not found.\n");
  17907. return SCRIPT_CMD_FAILURE;
  17908. }
  17909. for(const auto &it : idata->map)
  17910. map_foreachinmap(buildin_instance_warpall_sub, it.m, BL_PC, map_id2index(m), x, y, instance_id);
  17911. return SCRIPT_CMD_SUCCESS;
  17912. }
  17913. /*==========================================
  17914. * Broadcasts to all maps inside an instance
  17915. *
  17916. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  17917. * Using 0 for <instance id> will auto-detect the id.
  17918. *------------------------------------------*/
  17919. BUILDIN_FUNC(instance_announce) {
  17920. int instance_id = script_getnum(st,2);
  17921. const char *mes = script_getstr(st,3);
  17922. int flag = script_getnum(st,4);
  17923. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  17924. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  17925. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  17926. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  17927. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  17928. if (instance_id <= 0)
  17929. instance_id = script_instancegetid(st);
  17930. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  17931. if (instance_id <= 0 || !idata) {
  17932. ShowError("buildin_instance_announce: Instance not found.\n");
  17933. return SCRIPT_CMD_FAILURE;
  17934. }
  17935. for (const auto &it : idata->map)
  17936. map_foreachinmap(buildin_announce_sub, it.m, BL_PC, mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  17937. return SCRIPT_CMD_SUCCESS;
  17938. }
  17939. /*==========================================
  17940. * instance_check_party [malufett]
  17941. * Values:
  17942. * party_id : Party ID of the invoking character. [Required Parameter]
  17943. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  17944. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17945. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17946. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  17947. * Example 2: instance_check_party (getcharid(1),1,1,99);
  17948. *------------------------------------------*/
  17949. BUILDIN_FUNC(instance_check_party)
  17950. {
  17951. int amount, min, max, i, party_id, c = 0;
  17952. struct party_data *p;
  17953. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  17954. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17955. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17956. if( min < 1 || min > MAX_LEVEL) {
  17957. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  17958. return SCRIPT_CMD_FAILURE;
  17959. } else if( max < 1 || max > MAX_LEVEL) {
  17960. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  17961. return SCRIPT_CMD_FAILURE;
  17962. }
  17963. if( script_hasdata(st,2) )
  17964. party_id = script_getnum(st,2);
  17965. else return SCRIPT_CMD_FAILURE;
  17966. if( !(p = party_search(party_id)) ) {
  17967. script_pushint(st, 0); // Returns false if party does not exist.
  17968. return SCRIPT_CMD_FAILURE;
  17969. }
  17970. for( i = 0; i < MAX_PARTY; i++ ) {
  17971. struct map_session_data *pl_sd;
  17972. if( (pl_sd = p->data[i].sd) )
  17973. if(map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  17974. if(pl_sd->status.base_level < min) {
  17975. script_pushint(st, 0);
  17976. return SCRIPT_CMD_SUCCESS;
  17977. } else if(pl_sd->status.base_level > max) {
  17978. script_pushint(st, 0);
  17979. return SCRIPT_CMD_SUCCESS;
  17980. }
  17981. c++;
  17982. }
  17983. }
  17984. if(c < amount)
  17985. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  17986. else
  17987. script_pushint(st, 1);
  17988. return SCRIPT_CMD_SUCCESS;
  17989. }
  17990. /*==========================================
  17991. * instance_check_guild
  17992. * Values:
  17993. * guild_id : Guild ID of the invoking character. [Required Parameter]
  17994. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  17995. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17996. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17997. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  17998. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  17999. *------------------------------------------*/
  18000. BUILDIN_FUNC(instance_check_guild)
  18001. {
  18002. int amount, min, max, i, guild_id = 0, c = 0;
  18003. struct guild *g = NULL;
  18004. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  18005. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18006. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18007. if (min < 1 || min > MAX_LEVEL) {
  18008. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  18009. return SCRIPT_CMD_FAILURE;
  18010. } else if (max < 1 || max > MAX_LEVEL) {
  18011. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  18012. return SCRIPT_CMD_FAILURE;
  18013. }
  18014. if (script_hasdata(st,2))
  18015. guild_id = script_getnum(st,2);
  18016. else
  18017. return SCRIPT_CMD_FAILURE;
  18018. if (!(g = guild_search(guild_id))) {
  18019. script_pushint(st, 0); // Returns false if guild does not exist.
  18020. return SCRIPT_CMD_FAILURE;
  18021. }
  18022. for(i = 0; i < MAX_GUILD; i++) {
  18023. struct map_session_data *pl_sd;
  18024. if ((pl_sd = g->member[i].sd)) {
  18025. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18026. if (pl_sd->status.base_level < min) {
  18027. script_pushint(st, 0);
  18028. return SCRIPT_CMD_SUCCESS;
  18029. } else if (pl_sd->status.base_level > max) {
  18030. script_pushint(st, 0);
  18031. return SCRIPT_CMD_SUCCESS;
  18032. }
  18033. c++;
  18034. }
  18035. }
  18036. }
  18037. if (c < amount)
  18038. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  18039. else
  18040. script_pushint(st, 1);
  18041. return SCRIPT_CMD_SUCCESS;
  18042. }
  18043. /*==========================================
  18044. * instance_check_clan
  18045. * Values:
  18046. * clan_id : Clan ID of the invoking character. [Required Parameter]
  18047. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  18048. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  18049. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  18050. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  18051. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  18052. *------------------------------------------*/
  18053. BUILDIN_FUNC(instance_check_clan)
  18054. {
  18055. int amount, min, max, i, clan_id = 0, c = 0;
  18056. struct clan *cd = NULL;
  18057. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  18058. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  18059. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  18060. if (min < 1 || min > MAX_LEVEL) {
  18061. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  18062. return SCRIPT_CMD_FAILURE;
  18063. } else if (max < 1 || max > MAX_LEVEL) {
  18064. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  18065. return SCRIPT_CMD_FAILURE;
  18066. }
  18067. if (script_hasdata(st,2))
  18068. clan_id = script_getnum(st,2);
  18069. else
  18070. return SCRIPT_CMD_FAILURE;
  18071. if (!(cd = clan_search(clan_id))) {
  18072. script_pushint(st, 0); // Returns false if clan does not exist.
  18073. return SCRIPT_CMD_FAILURE;
  18074. }
  18075. for(i = 0; i < MAX_CLAN; i++) {
  18076. struct map_session_data *pl_sd;
  18077. if ((pl_sd = cd->members[i])) {
  18078. if (map_id2bl(pl_sd->bl.id) && !pl_sd->state.autotrade) {
  18079. if (pl_sd->status.base_level < min) {
  18080. script_pushint(st, 0);
  18081. return SCRIPT_CMD_SUCCESS;
  18082. } else if (pl_sd->status.base_level > max) {
  18083. script_pushint(st, 0);
  18084. return SCRIPT_CMD_SUCCESS;
  18085. }
  18086. c++;
  18087. }
  18088. }
  18089. }
  18090. if (c < amount)
  18091. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  18092. else
  18093. script_pushint(st, 1);
  18094. return SCRIPT_CMD_SUCCESS;
  18095. }
  18096. /*==========================================
  18097. * instance_info
  18098. * Values:
  18099. * name : name of the instance you want to look up. [Required Parameter]
  18100. * type : type of information you want to look up for the specified instance. [Required Parameter]
  18101. * index : index of the map in the instance. [Optional Parameter]
  18102. *------------------------------------------*/
  18103. BUILDIN_FUNC(instance_info)
  18104. {
  18105. const char* name = script_getstr(st, 2);
  18106. int type = script_getnum(st, 3);
  18107. int index = 0;
  18108. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  18109. if (!db) {
  18110. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  18111. script_pushint(st, -1);
  18112. return SCRIPT_CMD_FAILURE;
  18113. }
  18114. switch( type ){
  18115. case IIT_ID:
  18116. script_pushint(st, db->id);
  18117. break;
  18118. case IIT_TIME_LIMIT:
  18119. script_pushint(st, db->limit);
  18120. break;
  18121. case IIT_IDLE_TIMEOUT:
  18122. script_pushint(st, db->timeout);
  18123. break;
  18124. case IIT_ENTER_MAP:
  18125. script_pushstrcopy(st, map_mapid2mapname(db->enter.map));
  18126. break;
  18127. case IIT_ENTER_X:
  18128. script_pushint(st, db->enter.x);
  18129. break;
  18130. case IIT_ENTER_Y:
  18131. script_pushint(st, db->enter.y);
  18132. break;
  18133. case IIT_MAPCOUNT:
  18134. script_pushint(st, db->maplist.size());
  18135. break;
  18136. case IIT_MAP:
  18137. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  18138. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  18139. script_pushconststr(st, "");
  18140. return SCRIPT_CMD_FAILURE;
  18141. }
  18142. index = script_getnum(st, 4);
  18143. if( index < 0 ){
  18144. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  18145. script_pushconststr(st, "");
  18146. return SCRIPT_CMD_FAILURE;
  18147. }
  18148. if( index > UINT8_MAX ){
  18149. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  18150. script_pushconststr(st, "");
  18151. return SCRIPT_CMD_FAILURE;
  18152. }
  18153. script_pushstrcopy(st, map_mapid2mapname(db->maplist[index]));
  18154. break;
  18155. default:
  18156. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  18157. script_pushint(st, -1);
  18158. return SCRIPT_CMD_FAILURE;
  18159. }
  18160. return SCRIPT_CMD_SUCCESS;
  18161. }
  18162. /*------------------------------------------
  18163. *instance_live_info( <info type>{, <instance id>} );
  18164. - ILI_NAME : Instance Name
  18165. - ILI_MODE : Instance Mode
  18166. - ILI_OWNER : owner id
  18167. *------------------------------------------*/
  18168. BUILDIN_FUNC(instance_live_info)
  18169. {
  18170. int type = script_getnum(st, 2);
  18171. int id = 0;
  18172. if (type < ILI_NAME || type > ILI_OWNER) {
  18173. ShowError("buildin_instance_live_info: Unknown instance information type \"%d\".\n", type);
  18174. script_pushint(st, -1);
  18175. return SCRIPT_CMD_FAILURE;
  18176. }
  18177. if (!script_hasdata(st, 3))
  18178. id = script_instancegetid(st);
  18179. else
  18180. id = script_getnum(st, 3);
  18181. std::shared_ptr<s_instance_db> db = nullptr;
  18182. std::shared_ptr<s_instance_data> im = nullptr;
  18183. if (id > 0 && id < INT_MAX) {
  18184. im = util::umap_find(instances, id);
  18185. if (im)
  18186. db = instance_db.find(im->id);
  18187. }
  18188. if (!im || !db) {
  18189. if (type == ILI_NAME)
  18190. script_pushconststr(st, "");
  18191. else
  18192. script_pushint(st, -1);
  18193. return SCRIPT_CMD_SUCCESS;
  18194. }
  18195. switch( type ) {
  18196. case ILI_NAME:
  18197. script_pushstrcopy(st, db->name.c_str());
  18198. break;
  18199. case ILI_MODE:
  18200. script_pushint(st, im->mode);
  18201. break;
  18202. case ILI_OWNER:
  18203. script_pushint(st, im->owner_id);
  18204. break;
  18205. }
  18206. return SCRIPT_CMD_SUCCESS;
  18207. }
  18208. /*==========================================
  18209. * Custom Fonts
  18210. *------------------------------------------*/
  18211. BUILDIN_FUNC(setfont)
  18212. {
  18213. struct map_session_data *sd;
  18214. int font;
  18215. if( !script_rid2sd(sd) )
  18216. return SCRIPT_CMD_SUCCESS;
  18217. font = script_getnum(st,2);
  18218. if( sd->status.font != font )
  18219. sd->status.font = font;
  18220. else
  18221. sd->status.font = 0;
  18222. clif_font(sd);
  18223. return SCRIPT_CMD_SUCCESS;
  18224. }
  18225. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  18226. {
  18227. TBL_MOB* md = (TBL_MOB*)bl;
  18228. struct block_list *tbl;
  18229. int mobid = va_arg(ap,int);
  18230. uint16 skill_id = va_arg(ap,int);
  18231. uint16 skill_lv = va_arg(ap,int);
  18232. int casttime = va_arg(ap,int);
  18233. int cancel = va_arg(ap,int);
  18234. int emotion = va_arg(ap,int);
  18235. int target = va_arg(ap,int);
  18236. if( md->mob_id != mobid )
  18237. return 0;
  18238. // 0:self, 1:target, 2:master, default:random
  18239. switch( target ) {
  18240. case 0: tbl = map_id2bl(md->bl.id); break;
  18241. case 1: tbl = map_id2bl(md->target_id); break;
  18242. case 2: tbl = map_id2bl(md->master_id); break;
  18243. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  18244. }
  18245. if( !tbl )
  18246. return 0;
  18247. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  18248. unit_skillcastcancel(bl,0);
  18249. if( skill_get_casttype(skill_id) == CAST_GROUND )
  18250. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  18251. else
  18252. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  18253. clif_emotion(&md->bl, emotion);
  18254. return 1;
  18255. }
  18256. /*==========================================
  18257. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  18258. *------------------------------------------*/
  18259. BUILDIN_FUNC(areamobuseskill)
  18260. {
  18261. struct block_list center;
  18262. int16 m;
  18263. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  18264. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  18265. ShowError("areamobuseskill: invalid map name.\n");
  18266. return SCRIPT_CMD_SUCCESS;
  18267. }
  18268. center.m = m;
  18269. center.x = script_getnum(st,3);
  18270. center.y = script_getnum(st,4);
  18271. range = script_getnum(st,5);
  18272. mobid = script_getnum(st,6);
  18273. if (script_isstring(st, 7)) {
  18274. const char *name = script_getstr(st, 7);
  18275. if (!(skill_id = skill_name2id(name))) {
  18276. ShowError("buildin_areamobuseskill: Invalid skill name %s.\n", name);
  18277. return SCRIPT_CMD_FAILURE;
  18278. }
  18279. } else {
  18280. skill_id = script_getnum(st, 7);
  18281. if (!skill_get_index(skill_id)) {
  18282. ShowError("buildin_areamobuseskill: Invalid skill ID %d.\n", skill_id);
  18283. return SCRIPT_CMD_FAILURE;
  18284. }
  18285. }
  18286. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  18287. skill_lv = battle_config.mob_max_skilllvl;
  18288. casttime = script_getnum(st,9);
  18289. cancel = script_getnum(st,10);
  18290. emotion = script_getnum(st,11);
  18291. target = script_getnum(st,12);
  18292. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  18293. return SCRIPT_CMD_SUCCESS;
  18294. }
  18295. /**
  18296. * Display a progress bar above a character
  18297. * progressbar "<color>",<seconds>;
  18298. */
  18299. BUILDIN_FUNC(progressbar)
  18300. {
  18301. struct map_session_data * sd;
  18302. const char * color;
  18303. unsigned int second;
  18304. if( !script_rid2sd(sd) )
  18305. return SCRIPT_CMD_SUCCESS;
  18306. st->state = STOP;
  18307. color = script_getstr(st,2);
  18308. second = script_getnum(st,3);
  18309. sd->progressbar.npc_id = st->oid;
  18310. sd->progressbar.timeout = gettick() + second*1000;
  18311. sd->state.workinprogress = WIP_DISABLE_ALL;
  18312. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  18313. return SCRIPT_CMD_SUCCESS;
  18314. }
  18315. /**
  18316. * Display a progress bar above an NPC
  18317. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  18318. */
  18319. BUILDIN_FUNC(progressbar_npc){
  18320. map_session_data *sd = map_id2sd(st->rid);
  18321. struct npc_data* nd = NULL;
  18322. if( script_hasdata(st, 4) ){
  18323. const char* name = script_getstr(st, 4);
  18324. nd = npc_name2id(name);
  18325. if( !nd ){
  18326. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  18327. return SCRIPT_CMD_FAILURE;
  18328. }
  18329. }else{
  18330. nd = map_id2nd(st->oid);
  18331. }
  18332. // First call(by function call)
  18333. if( !nd->progressbar.timeout ){
  18334. const char *color;
  18335. int second;
  18336. color = script_getstr(st, 2);
  18337. second = script_getnum(st, 3);
  18338. if( second < 0 ){
  18339. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  18340. return SCRIPT_CMD_FAILURE;
  18341. }
  18342. if (sd) { // Player attached - keep them from doing other things
  18343. sd->state.workinprogress = WIP_DISABLE_ALL;
  18344. sd->state.block_action |= (PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18345. }
  18346. // sleep for the target amount of time
  18347. st->state = RERUNLINE;
  18348. st->sleep.tick = second * 1000;
  18349. nd->progressbar.timeout = gettick() + second * 1000;
  18350. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  18351. clif_progressbar_npc_area(nd);
  18352. // Second call(by timer after sleeping time is over)
  18353. } else {
  18354. // Continue the script
  18355. if (sd) { // Player attached - remove restrictions
  18356. sd->state.workinprogress = WIP_DISABLE_NONE;
  18357. sd->state.block_action &= ~(PCBLOCK_MOVE | PCBLOCK_ATTACK | PCBLOCK_SKILL);
  18358. }
  18359. st->state = RUN;
  18360. st->sleep.tick = 0;
  18361. nd->progressbar.timeout = nd->progressbar.color = 0;
  18362. }
  18363. return SCRIPT_CMD_SUCCESS;
  18364. }
  18365. BUILDIN_FUNC(pushpc)
  18366. {
  18367. uint8 dir;
  18368. int cells, dx, dy;
  18369. struct map_session_data* sd;
  18370. if(!script_rid2sd(sd))
  18371. {
  18372. return SCRIPT_CMD_SUCCESS;
  18373. }
  18374. dir = script_getnum(st,2);
  18375. cells = script_getnum(st,3);
  18376. if(dir >= DIR_MAX)
  18377. {
  18378. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  18379. script_reportsrc(st);
  18380. dir%= DIR_MAX; // trim spin-over
  18381. }
  18382. if(!cells)
  18383. {// zero distance
  18384. return SCRIPT_CMD_SUCCESS;
  18385. }
  18386. else if(cells<0)
  18387. {// pushing backwards
  18388. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  18389. cells = -cells;
  18390. }
  18391. dx = dirx[dir];
  18392. dy = diry[dir];
  18393. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  18394. return SCRIPT_CMD_SUCCESS;
  18395. }
  18396. /// Invokes buying store preparation window
  18397. /// buyingstore <slots>;
  18398. BUILDIN_FUNC(buyingstore)
  18399. {
  18400. struct map_session_data* sd;
  18401. if( !script_rid2sd(sd) )
  18402. {
  18403. return SCRIPT_CMD_SUCCESS;
  18404. }
  18405. if( npc_isnear(&sd->bl) ) {
  18406. char output[150];
  18407. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  18408. clif_displaymessage(sd->fd, output);
  18409. return SCRIPT_CMD_SUCCESS;
  18410. }
  18411. buyingstore_setup(sd, script_getnum(st,2));
  18412. return SCRIPT_CMD_SUCCESS;
  18413. }
  18414. /// Invokes search store info window
  18415. /// searchstores <uses>,<effect>;
  18416. BUILDIN_FUNC(searchstores)
  18417. {
  18418. unsigned short effect;
  18419. unsigned int uses;
  18420. struct map_session_data* sd;
  18421. if( !script_rid2sd(sd) )
  18422. {
  18423. return SCRIPT_CMD_SUCCESS;
  18424. }
  18425. uses = script_getnum(st,2);
  18426. effect = script_getnum(st,3);
  18427. if( !uses )
  18428. {
  18429. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  18430. return SCRIPT_CMD_FAILURE;
  18431. }
  18432. if( effect > 1 )
  18433. {
  18434. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  18435. return SCRIPT_CMD_FAILURE;
  18436. }
  18437. searchstore_open(sd, uses, effect);
  18438. return SCRIPT_CMD_SUCCESS;
  18439. }
  18440. /// Displays a number as large digital clock.
  18441. /// showdigit <value>[,<type>];
  18442. BUILDIN_FUNC(showdigit)
  18443. {
  18444. unsigned int type = 0;
  18445. int value;
  18446. struct map_session_data* sd;
  18447. if( !script_rid2sd(sd) )
  18448. {
  18449. return SCRIPT_CMD_SUCCESS;
  18450. }
  18451. value = script_getnum(st,2);
  18452. if( script_hasdata(st,3) ){
  18453. type = script_getnum(st,3);
  18454. }
  18455. switch( type ){
  18456. case 0:
  18457. break;
  18458. case 1:
  18459. case 2:
  18460. // Use absolute value and then the negative value of it as starting value
  18461. // This is what gravity's client does for these counters
  18462. value = -abs(value);
  18463. break;
  18464. case 3:
  18465. value = abs(value);
  18466. if( value > 99 ){
  18467. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  18468. script_reportsrc(st);
  18469. value = 99;
  18470. }
  18471. break;
  18472. default:
  18473. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  18474. return SCRIPT_CMD_FAILURE;
  18475. }
  18476. clif_showdigit(sd, (unsigned char)type, value);
  18477. return SCRIPT_CMD_SUCCESS;
  18478. }
  18479. /**
  18480. * Rune Knight
  18481. * makerune <% success bonus>{,<char_id>};
  18482. **/
  18483. BUILDIN_FUNC(makerune) {
  18484. TBL_PC* sd;
  18485. if (!script_charid2sd(3,sd))
  18486. return SCRIPT_CMD_FAILURE;
  18487. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  18488. sd->itemid = script_getnum(st,2);
  18489. return SCRIPT_CMD_SUCCESS;
  18490. }
  18491. /**
  18492. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  18493. **/
  18494. BUILDIN_FUNC(checkdragon) {
  18495. TBL_PC* sd;
  18496. if (!script_charid2sd(2,sd))
  18497. return SCRIPT_CMD_FAILURE;
  18498. if( pc_isridingdragon(sd) )
  18499. script_pushint(st,1);
  18500. else
  18501. script_pushint(st,0);
  18502. return SCRIPT_CMD_SUCCESS;
  18503. }
  18504. /**
  18505. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  18506. * - Toggles the dragon on a RK if he can mount;
  18507. * @param Color - when not provided uses the green dragon;
  18508. * - 1 : Green Dragon
  18509. * - 2 : Brown Dragon
  18510. * - 3 : Gray Dragon
  18511. * - 4 : Blue Dragon
  18512. * - 5 : Red Dragon
  18513. **/
  18514. BUILDIN_FUNC(setdragon) {
  18515. TBL_PC* sd;
  18516. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  18517. if (!script_charid2sd(3,sd))
  18518. return SCRIPT_CMD_FAILURE;
  18519. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  18520. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  18521. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  18522. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  18523. script_pushint(st,1);
  18524. } else {//Not mounted; Mount now.
  18525. unsigned int option = OPTION_DRAGON1;
  18526. if( color ) {
  18527. option = ( color == 1 ? OPTION_DRAGON1 :
  18528. color == 2 ? OPTION_DRAGON2 :
  18529. color == 3 ? OPTION_DRAGON3 :
  18530. color == 4 ? OPTION_DRAGON4 :
  18531. color == 5 ? OPTION_DRAGON5 : 0);
  18532. if( !option ) {
  18533. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  18534. option = OPTION_DRAGON1;
  18535. }
  18536. }
  18537. pc_setoption(sd, sd->sc.option|option);
  18538. script_pushint(st,1);
  18539. }
  18540. return SCRIPT_CMD_SUCCESS;
  18541. }
  18542. /**
  18543. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  18544. **/
  18545. BUILDIN_FUNC(ismounting) {
  18546. TBL_PC* sd;
  18547. if (!script_charid2sd(2,sd))
  18548. return SCRIPT_CMD_FAILURE;
  18549. if( sd->sc.data[SC_ALL_RIDING] )
  18550. script_pushint(st,1);
  18551. else
  18552. script_pushint(st,0);
  18553. return SCRIPT_CMD_SUCCESS;
  18554. }
  18555. /**
  18556. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  18557. * - Toggles new mounts on a player when he can mount
  18558. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  18559. * - Will unmount the player is he is already mounting
  18560. **/
  18561. BUILDIN_FUNC(setmounting) {
  18562. TBL_PC* sd;
  18563. if (!script_charid2sd(2,sd))
  18564. return SCRIPT_CMD_FAILURE;
  18565. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  18566. clif_msg(sd, NEED_REINS_OF_MOUNT);
  18567. script_pushint(st,0); //can't mount with one of these
  18568. } 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]) {
  18569. // SC_HIDING, SC__INVISIBILITY, SC__SHADOWFORM, SC_SUHIDE already disable item usage
  18570. script_pushint(st, 0); // Silent failure
  18571. } else {
  18572. if( sd->sc.data[SC_ALL_RIDING] )
  18573. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  18574. else
  18575. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INFINITE_TICK); //mount
  18576. script_pushint(st,1);//in both cases, return 1.
  18577. }
  18578. return SCRIPT_CMD_SUCCESS;
  18579. }
  18580. /**
  18581. * Retrieves quantity of arguments provided to callfunc/callsub.
  18582. * getargcount() -> amount of arguments received in a function
  18583. **/
  18584. BUILDIN_FUNC(getargcount) {
  18585. struct script_retinfo* ri;
  18586. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  18587. ShowError("script:getargcount: used out of function or callsub label!\n");
  18588. st->state = END;
  18589. return SCRIPT_CMD_FAILURE;
  18590. }
  18591. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  18592. script_pushint(st, ri->nargs);
  18593. return SCRIPT_CMD_SUCCESS;
  18594. }
  18595. /**
  18596. * getcharip(<account ID>/<character ID>/<character name>)
  18597. **/
  18598. BUILDIN_FUNC(getcharip)
  18599. {
  18600. struct map_session_data* sd = NULL;
  18601. /* check if a character name is specified */
  18602. if( script_hasdata(st, 2) )
  18603. {
  18604. if (script_isstring(st, 2))
  18605. sd = map_nick2sd(script_getstr(st, 2),false);
  18606. else
  18607. {
  18608. int id = script_getnum(st, 2);
  18609. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  18610. }
  18611. }
  18612. else
  18613. script_rid2sd(sd);
  18614. /* check for sd and IP */
  18615. if (!sd || !session[sd->fd]->client_addr)
  18616. {
  18617. script_pushconststr(st, "");
  18618. return SCRIPT_CMD_SUCCESS;
  18619. }
  18620. /* return the client ip_addr converted for output */
  18621. if (sd && sd->fd && session[sd->fd])
  18622. {
  18623. /* initiliaze */
  18624. const char *ip_addr = NULL;
  18625. uint32 ip;
  18626. /* set ip, ip_addr and convert to ip and push str */
  18627. ip = session[sd->fd]->client_addr;
  18628. ip_addr = ip2str(ip, NULL);
  18629. script_pushstrcopy(st, ip_addr);
  18630. }
  18631. return SCRIPT_CMD_SUCCESS;
  18632. }
  18633. /**
  18634. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  18635. **/
  18636. BUILDIN_FUNC(is_function) {
  18637. const char* str = script_getstr(st,2);
  18638. if( strdb_exists(userfunc_db, str) )
  18639. script_pushint(st,1);
  18640. else
  18641. script_pushint(st,0);
  18642. return SCRIPT_CMD_SUCCESS;
  18643. }
  18644. /**
  18645. * get_revision() -> retrieves the current svn revision (if available)
  18646. **/
  18647. BUILDIN_FUNC(get_revision) {
  18648. const char *svn = get_svn_revision();
  18649. if ( svn[0] != UNKNOWN_VERSION )
  18650. script_pushint(st,atoi(svn));
  18651. else
  18652. script_pushint(st,-1); //unknown
  18653. return SCRIPT_CMD_SUCCESS;
  18654. }
  18655. /* get_hash() -> retrieves the current git hash (if available)*/
  18656. BUILDIN_FUNC(get_githash) {
  18657. const char* git = get_git_hash();
  18658. char buf[CHAT_SIZE_MAX];
  18659. safestrncpy(buf,git,strlen(git)+1);
  18660. if ( git[0] != UNKNOWN_VERSION )
  18661. script_pushstr(st,buf);
  18662. else
  18663. script_pushconststr(st,"Unknown"); //unknown
  18664. return SCRIPT_CMD_SUCCESS;
  18665. }
  18666. /**
  18667. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  18668. **/
  18669. BUILDIN_FUNC(freeloop) {
  18670. if( script_hasdata(st,2) ) {
  18671. if( script_getnum(st,2) )
  18672. st->freeloop = 1;
  18673. else
  18674. st->freeloop = 0;
  18675. }
  18676. script_pushint(st, st->freeloop);
  18677. return SCRIPT_CMD_SUCCESS;
  18678. }
  18679. /**
  18680. * @commands (script based)
  18681. **/
  18682. BUILDIN_FUNC(bindatcmd) {
  18683. const char* atcmd;
  18684. const char* eventName;
  18685. int i, level = 0, level2 = 100;
  18686. bool create = false;
  18687. atcmd = script_getstr(st,2);
  18688. eventName = script_getstr(st,3);
  18689. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18690. atcmd++;
  18691. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  18692. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  18693. if( atcmd_binding_count == 0 ) {
  18694. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  18695. create = true;
  18696. } else {
  18697. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  18698. if( i < atcmd_binding_count ) {/* update existent entry */
  18699. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18700. atcmd_binding[i]->level = level;
  18701. atcmd_binding[i]->level2 = level2;
  18702. } else
  18703. create = true;
  18704. }
  18705. if( create ) {
  18706. i = atcmd_binding_count;
  18707. if( atcmd_binding_count++ != 0 )
  18708. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  18709. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  18710. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  18711. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18712. atcmd_binding[i]->level = level;
  18713. atcmd_binding[i]->level2 = level2;
  18714. }
  18715. return SCRIPT_CMD_SUCCESS;
  18716. }
  18717. BUILDIN_FUNC(unbindatcmd) {
  18718. const char* atcmd;
  18719. int i = 0;
  18720. atcmd = script_getstr(st, 2);
  18721. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18722. atcmd++;
  18723. if( atcmd_binding_count == 0 ) {
  18724. script_pushint(st, 0);
  18725. return SCRIPT_CMD_SUCCESS;
  18726. }
  18727. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  18728. if( i < atcmd_binding_count ) {
  18729. int cursor = 0;
  18730. aFree(atcmd_binding[i]);
  18731. atcmd_binding[i] = NULL;
  18732. /* compact the list now that we freed a slot somewhere */
  18733. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  18734. if( atcmd_binding[i] == NULL )
  18735. continue;
  18736. if( cursor != i ) {
  18737. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  18738. }
  18739. cursor++;
  18740. }
  18741. if( (atcmd_binding_count = cursor) == 0 )
  18742. aFree(atcmd_binding);
  18743. script_pushint(st, 1);
  18744. } else
  18745. script_pushint(st, 0);/* not found */
  18746. return SCRIPT_CMD_SUCCESS;
  18747. }
  18748. BUILDIN_FUNC(useatcmd) {
  18749. return atcommand_sub(st,3);
  18750. }
  18751. BUILDIN_FUNC(checkre)
  18752. {
  18753. int num;
  18754. num=script_getnum(st,2);
  18755. switch(num){
  18756. case 0:
  18757. #ifdef RENEWAL
  18758. script_pushint(st, 1);
  18759. #else
  18760. script_pushint(st, 0);
  18761. #endif
  18762. break;
  18763. case 1:
  18764. #ifdef RENEWAL_CAST
  18765. script_pushint(st, 1);
  18766. #else
  18767. script_pushint(st, 0);
  18768. #endif
  18769. break;
  18770. case 2:
  18771. #ifdef RENEWAL_DROP
  18772. script_pushint(st, 1);
  18773. #else
  18774. script_pushint(st, 0);
  18775. #endif
  18776. break;
  18777. case 3:
  18778. #ifdef RENEWAL_EXP
  18779. script_pushint(st, 1);
  18780. #else
  18781. script_pushint(st, 0);
  18782. #endif
  18783. break;
  18784. case 4:
  18785. #ifdef RENEWAL_LVDMG
  18786. script_pushint(st, 1);
  18787. #else
  18788. script_pushint(st, 0);
  18789. #endif
  18790. break;
  18791. case 5:
  18792. #ifdef RENEWAL_ASPD
  18793. script_pushint(st, 1);
  18794. #else
  18795. script_pushint(st, 0);
  18796. #endif
  18797. break;
  18798. default:
  18799. ShowWarning("buildin_checkre: unknown parameter.\n");
  18800. break;
  18801. }
  18802. return SCRIPT_CMD_SUCCESS;
  18803. }
  18804. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  18805. BUILDIN_FUNC(getrandgroupitem) {
  18806. TBL_PC* sd;
  18807. int i, get_count = 0, identify = 0;
  18808. uint16 group, qty = 0;
  18809. uint8 sub_group = 1;
  18810. struct item item_tmp;
  18811. struct s_item_group_entry *entry = NULL;
  18812. if (!script_charid2sd(6, sd))
  18813. return SCRIPT_CMD_SUCCESS;
  18814. group = script_getnum(st,2);
  18815. if (!group) {
  18816. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  18817. return SCRIPT_CMD_FAILURE;
  18818. }
  18819. FETCH(3, qty);
  18820. FETCH(4, sub_group);
  18821. FETCH(5, identify);
  18822. entry = itemdb_get_randgroupitem(group,sub_group);
  18823. if (!entry)
  18824. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  18825. memset(&item_tmp,0,sizeof(item_tmp));
  18826. item_tmp.nameid = entry->nameid;
  18827. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  18828. if (!qty)
  18829. qty = entry->amount;
  18830. //Check if it's stackable.
  18831. if (!itemdb_isstackable(entry->nameid)) {
  18832. item_tmp.amount = 1;
  18833. get_count = qty;
  18834. }
  18835. else {
  18836. item_tmp.amount = qty;
  18837. get_count = 1;
  18838. }
  18839. for (i = 0; i < get_count; i++) {
  18840. // if not pet egg
  18841. if (!pet_create_egg(sd, entry->nameid)) {
  18842. unsigned char flag = 0;
  18843. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  18844. clif_additem(sd,0,0,flag);
  18845. if (pc_candrop(sd,&item_tmp))
  18846. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  18847. }
  18848. }
  18849. }
  18850. return SCRIPT_CMD_SUCCESS;
  18851. }
  18852. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  18853. * Gives item(s) to the attached player based on item group contents
  18854. */
  18855. BUILDIN_FUNC(getgroupitem) {
  18856. TBL_PC *sd;
  18857. int group_id = script_getnum(st,2);
  18858. if (!script_charid2sd(4,sd))
  18859. return SCRIPT_CMD_SUCCESS;
  18860. if (itemdb_pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  18861. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  18862. return SCRIPT_CMD_FAILURE;
  18863. }
  18864. return SCRIPT_CMD_SUCCESS;
  18865. }
  18866. /* cleanmap <map_name>;
  18867. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  18868. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  18869. {
  18870. nullpo_ret(bl);
  18871. map_clearflooritem(bl);
  18872. return SCRIPT_CMD_SUCCESS;
  18873. }
  18874. BUILDIN_FUNC(cleanmap)
  18875. {
  18876. const char *mapname;
  18877. int16 m;
  18878. mapname = script_getstr(st, 2);
  18879. m = map_mapname2mapid(mapname);
  18880. if (!m)
  18881. return SCRIPT_CMD_FAILURE;
  18882. if ((script_lastdata(st) - 2) < 4) {
  18883. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  18884. } else {
  18885. int16 x0 = script_getnum(st, 3);
  18886. int16 y0 = script_getnum(st, 4);
  18887. int16 x1 = script_getnum(st, 5);
  18888. int16 y1 = script_getnum(st, 6);
  18889. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  18890. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  18891. } else {
  18892. ShowError("cleanarea: invalid coordinate defined!\n");
  18893. return SCRIPT_CMD_FAILURE;
  18894. }
  18895. }
  18896. return SCRIPT_CMD_SUCCESS;
  18897. }
  18898. /* Cast a skill on the attached player.
  18899. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  18900. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  18901. BUILDIN_FUNC(npcskill)
  18902. {
  18903. uint16 skill_id;
  18904. unsigned short skill_level;
  18905. unsigned int stat_point;
  18906. unsigned int npc_level;
  18907. struct npc_data *nd;
  18908. struct map_session_data *sd;
  18909. if( !script_rid2sd(sd) )
  18910. return SCRIPT_CMD_SUCCESS;
  18911. if (script_isstring(st, 2)) {
  18912. const char *name = script_getstr(st, 2);
  18913. if (!(skill_id = skill_name2id(name))) {
  18914. ShowError("buildin_npcskill: Invalid skill name %s.\n", name);
  18915. return SCRIPT_CMD_FAILURE;
  18916. }
  18917. } else {
  18918. skill_id = script_getnum(st, 2);
  18919. if (!skill_get_index(skill_id)) {
  18920. ShowError("buildin_npcskill: Invalid skill ID %d.\n", skill_id);
  18921. return SCRIPT_CMD_FAILURE;
  18922. }
  18923. }
  18924. skill_level = script_getnum(st, 3);
  18925. stat_point = script_getnum(st, 4);
  18926. npc_level = script_getnum(st, 5);
  18927. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  18928. if (stat_point > battle_config.max_third_parameter) {
  18929. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  18930. return SCRIPT_CMD_FAILURE;
  18931. }
  18932. if (npc_level > MAX_LEVEL) {
  18933. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  18934. return SCRIPT_CMD_FAILURE;
  18935. }
  18936. if (nd == NULL) { //ain't possible, but I don't trust people.
  18937. return SCRIPT_CMD_FAILURE;
  18938. }
  18939. nd->level = npc_level;
  18940. nd->stat_point = stat_point;
  18941. if (!nd->status.hp)
  18942. status_calc_npc(nd, SCO_FIRST);
  18943. else
  18944. status_calc_npc(nd, SCO_NONE);
  18945. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  18946. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  18947. else
  18948. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  18949. return SCRIPT_CMD_SUCCESS;
  18950. }
  18951. /* Consumes an item.
  18952. * consumeitem <item id>{,<char_id>};
  18953. * consumeitem "<item name>"{,<char_id>};
  18954. * @param item: Item ID or name
  18955. */
  18956. BUILDIN_FUNC(consumeitem)
  18957. {
  18958. TBL_PC *sd;
  18959. struct item_data *item_data;
  18960. if (!script_charid2sd(3, sd))
  18961. return SCRIPT_CMD_FAILURE;
  18962. if( script_isstring(st, 2) ){
  18963. const char *name = script_getstr(st, 2);
  18964. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  18965. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  18966. return SCRIPT_CMD_FAILURE;
  18967. }
  18968. } else {
  18969. t_itemid nameid = script_getnum(st, 2);
  18970. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  18971. ShowError("buildin_consumeitem: Nonexistant item %u requested.\n", nameid );
  18972. return SCRIPT_CMD_FAILURE;
  18973. }
  18974. }
  18975. run_script( item_data->script, 0, sd->bl.id, 0 );
  18976. return SCRIPT_CMD_SUCCESS;
  18977. }
  18978. /** Makes a player sit/stand.
  18979. * sit {"<character name>"};
  18980. * stand {"<character name>"};
  18981. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  18982. * @param name: Player name that will be invoked
  18983. */
  18984. BUILDIN_FUNC(sit)
  18985. {
  18986. TBL_PC *sd;
  18987. if( !script_nick2sd(2,sd) )
  18988. return SCRIPT_CMD_FAILURE;
  18989. if( !pc_issit(sd) ) {
  18990. pc_setsit(sd);
  18991. skill_sit(sd, 1);
  18992. clif_sitting(&sd->bl);
  18993. }
  18994. return SCRIPT_CMD_SUCCESS;
  18995. }
  18996. /** Makes player to stand
  18997. * @param name: Player name that will be set
  18998. */
  18999. BUILDIN_FUNC(stand)
  19000. {
  19001. TBL_PC *sd;
  19002. if( !script_nick2sd(2,sd) )
  19003. return SCRIPT_CMD_FAILURE;
  19004. if( pc_issit(sd) && pc_setstand(sd, false)) {
  19005. skill_sit(sd, 0);
  19006. clif_standing(&sd->bl);
  19007. }
  19008. return SCRIPT_CMD_SUCCESS;
  19009. }
  19010. /** Creates an array of bounded item IDs
  19011. * countbound {<type>{,<char_id>}};
  19012. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  19013. * @return amt: Total number of different items type found
  19014. */
  19015. BUILDIN_FUNC(countbound)
  19016. {
  19017. TBL_PC *sd;
  19018. if (!script_charid2sd(3,sd))
  19019. return SCRIPT_CMD_FAILURE;
  19020. int i, k = 0;
  19021. int type = script_getnum(st,2);
  19022. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  19023. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  19024. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  19025. ))
  19026. {
  19027. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  19028. pc_setreg(sd,reference_uid(add_str("@bound_amount"), k),sd->inventory.u.items_inventory[i].amount);
  19029. k++;
  19030. }
  19031. }
  19032. script_pushint(st,k);
  19033. return SCRIPT_CMD_SUCCESS;
  19034. }
  19035. /** Creates new party
  19036. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  19037. * @param party_name: String of party name that will be created
  19038. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  19039. * @param item_share: 0-Each Take. 1-Party Share
  19040. * @param item_share_type: 0-Each Take. 1-Even Share
  19041. * @return val: Result value
  19042. * -3 - party name is exist
  19043. * -2 - player is in party already
  19044. * -1 - player is not found
  19045. * 0 - unknown error
  19046. * 1 - success, will return party id $@party_create_id
  19047. */
  19048. BUILDIN_FUNC(party_create)
  19049. {
  19050. char party_name[NAME_LENGTH];
  19051. int item1 = 0, item2 = 0;
  19052. TBL_PC *sd = NULL;
  19053. if (!script_charid2sd(3, sd)) {
  19054. script_pushint(st,-1);
  19055. return SCRIPT_CMD_FAILURE;
  19056. }
  19057. if( sd->status.party_id ) {
  19058. script_pushint(st,-2);
  19059. return SCRIPT_CMD_FAILURE;
  19060. }
  19061. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  19062. trim(party_name);
  19063. if( party_searchname(party_name) ) {
  19064. script_pushint(st,-3);
  19065. return SCRIPT_CMD_FAILURE;
  19066. }
  19067. if( script_getnum(st,4) )
  19068. item1 = 1;
  19069. if( script_getnum(st,5) )
  19070. item2 = 1;
  19071. party_create_byscript = 1;
  19072. script_pushint(st,party_create(sd,party_name,item1,item2));
  19073. return SCRIPT_CMD_SUCCESS;
  19074. }
  19075. /** Adds player to specified party
  19076. * party_addmember <party id>,<char id>;
  19077. * @param party_id: The party that will be entered by player
  19078. * @param char_id: Char id of player that will be joined to the party
  19079. * @return val: Result value
  19080. * -5 - another character of the same account is in the party
  19081. * -4 - party is full
  19082. * -3 - party is not found
  19083. * -2 - player is in party already
  19084. * -1 - player is not found
  19085. * 0 - unknown error
  19086. * 1 - success
  19087. */
  19088. BUILDIN_FUNC(party_addmember)
  19089. {
  19090. int party_id = script_getnum(st,2);
  19091. TBL_PC *sd;
  19092. struct party_data *party;
  19093. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  19094. script_pushint(st,-1);
  19095. return SCRIPT_CMD_FAILURE;
  19096. }
  19097. if( sd->status.party_id ) {
  19098. script_pushint(st,-2);
  19099. return SCRIPT_CMD_FAILURE;
  19100. }
  19101. if( !(party = party_search(party_id)) ) {
  19102. script_pushint(st,-3);
  19103. return SCRIPT_CMD_FAILURE;
  19104. }
  19105. if (battle_config.block_account_in_same_party) {
  19106. int i;
  19107. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  19108. if (i < MAX_PARTY) {
  19109. script_pushint(st,-5);
  19110. return SCRIPT_CMD_FAILURE;
  19111. }
  19112. }
  19113. if( party->party.count >= MAX_PARTY ) {
  19114. script_pushint(st,-4);
  19115. return SCRIPT_CMD_FAILURE;
  19116. }
  19117. sd->party_invite = party_id;
  19118. script_pushint(st,party_add_member(party_id,sd));
  19119. return SCRIPT_CMD_SUCCESS;
  19120. }
  19121. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  19122. * party_delmember {<char id>,<party_id>};
  19123. * @param: char_id
  19124. * @param: party_id
  19125. * @return val: Result value
  19126. * -3 - player is not in party
  19127. * -2 - party is not found
  19128. * -1 - player is not found
  19129. * 0 - unknown error
  19130. * 1 - success
  19131. */
  19132. BUILDIN_FUNC(party_delmember)
  19133. {
  19134. TBL_PC *sd = NULL;
  19135. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  19136. script_pushint(st,-1);
  19137. return SCRIPT_CMD_FAILURE;
  19138. }
  19139. if( sd || script_charid2sd(2,sd) )
  19140. script_pushint(st,party_removemember2(sd,0,0));
  19141. else
  19142. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  19143. return SCRIPT_CMD_SUCCESS;
  19144. }
  19145. /** Changes party leader of specified party (even the leader is offline)
  19146. * party_changeleader <party id>,<char id>;
  19147. * @param party_id: ID of party
  19148. * @param char_id: Char ID of new leader
  19149. * @return val: Result value
  19150. * -4 - player is party leader already
  19151. * -3 - player is not in this party
  19152. * -2 - player is not found
  19153. * -1 - party is not found
  19154. * 0 - unknown error
  19155. * 1 - success */
  19156. BUILDIN_FUNC(party_changeleader)
  19157. {
  19158. int i, party_id = script_getnum(st,2);
  19159. TBL_PC *sd = NULL;
  19160. TBL_PC *tsd = NULL;
  19161. struct party_data *party = NULL;
  19162. if( !(party = party_search(party_id)) ) {
  19163. script_pushint(st,-1);
  19164. return SCRIPT_CMD_FAILURE;
  19165. }
  19166. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  19167. script_pushint(st,-2);
  19168. return SCRIPT_CMD_FAILURE;
  19169. }
  19170. if( tsd->status.party_id != party_id ) {
  19171. script_pushint(st,-3);
  19172. return SCRIPT_CMD_FAILURE;
  19173. }
  19174. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19175. if( i >= MAX_PARTY ) { //this is should impossible!
  19176. script_pushint(st,0);
  19177. return SCRIPT_CMD_FAILURE;
  19178. }
  19179. if( party->data[i].sd == tsd ) {
  19180. script_pushint(st,-4);
  19181. return SCRIPT_CMD_FAILURE;
  19182. }
  19183. script_pushint(st,party_changeleader(sd,tsd,party));
  19184. return SCRIPT_CMD_SUCCESS;
  19185. }
  19186. /** Changes party option
  19187. * party_changeoption <party id>,<option>,<flag>;
  19188. * @param party_id: ID of party that will be changed
  19189. * @param option: Type of option
  19190. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  19191. */
  19192. BUILDIN_FUNC(party_changeoption)
  19193. {
  19194. struct party_data *party;
  19195. if( !(party = party_search(script_getnum(st,2))) ) {
  19196. script_pushint(st,-1);
  19197. return SCRIPT_CMD_FAILURE;
  19198. }
  19199. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  19200. return SCRIPT_CMD_SUCCESS;
  19201. }
  19202. /** Destroys party with party id.
  19203. * party_destroy <party id>;
  19204. * @param party_id: ID of party that will be destroyed
  19205. */
  19206. BUILDIN_FUNC(party_destroy)
  19207. {
  19208. int i;
  19209. struct party_data *party;
  19210. if( !(party = party_search(script_getnum(st,2))) ) {
  19211. script_pushint(st,0);
  19212. return SCRIPT_CMD_FAILURE;
  19213. }
  19214. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  19215. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  19216. int j;
  19217. for( j = 0; j < MAX_PARTY; j++ ) {
  19218. TBL_PC *sd = party->data[j].sd;
  19219. if(sd)
  19220. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  19221. else if( party->party.member[j].char_id )
  19222. 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);
  19223. }
  19224. party_broken(party->party.party_id);
  19225. script_pushint(st,1);
  19226. }
  19227. else //leader leave = party broken
  19228. script_pushint(st,party_leave(party->data[i].sd));
  19229. return SCRIPT_CMD_SUCCESS;
  19230. }
  19231. /** Returns various information about a player's VIP status. Need to enable VIP system
  19232. * vip_status <type>,{"<character name>"};
  19233. * @param type: Info type, see enum vip_status_type
  19234. * @param name: Character name (Optional)
  19235. */
  19236. BUILDIN_FUNC(vip_status) {
  19237. #ifdef VIP_ENABLE
  19238. TBL_PC *sd;
  19239. int type;
  19240. if( !script_nick2sd(3,sd) )
  19241. return SCRIPT_CMD_FAILURE;
  19242. type = script_getnum(st, 2);
  19243. switch(type) {
  19244. case VIP_STATUS_ACTIVE: // Get VIP status.
  19245. script_pushint(st, pc_isvip(sd));
  19246. break;
  19247. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  19248. if (pc_isvip(sd)) {
  19249. script_pushint(st, sd->vip.time);
  19250. } else
  19251. script_pushint(st, 0);
  19252. break;
  19253. case VIP_STATUS_REMAINING: // Get remaining time.
  19254. if (pc_isvip(sd)) {
  19255. script_pushint(st, sd->vip.time - time(NULL));
  19256. } else
  19257. script_pushint(st, 0);
  19258. break;
  19259. default:
  19260. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  19261. return SCRIPT_CMD_FAILURE;
  19262. }
  19263. #else
  19264. script_pushint(st, 0);
  19265. #endif
  19266. return SCRIPT_CMD_SUCCESS;
  19267. }
  19268. /** Adds or removes VIP time in minutes. Need to enable VIP system
  19269. * vip_time <time in mn>,{"<character name>"};
  19270. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  19271. * @param name: Character name (optional)
  19272. */
  19273. BUILDIN_FUNC(vip_time) {
  19274. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  19275. TBL_PC *sd;
  19276. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  19277. if( !script_nick2sd(3,sd) )
  19278. return SCRIPT_CMD_FAILURE;
  19279. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  19280. #endif
  19281. return SCRIPT_CMD_SUCCESS;
  19282. }
  19283. /**
  19284. * Turns a player into a monster and optionally can grant a SC attribute effect.
  19285. * transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19286. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  19287. * @param monster: Monster ID or name
  19288. * @param duration: Transform duration in millisecond (ms)
  19289. * @param sc_type: Type of SC that will be affected during the transformation
  19290. * @param val1: Value for SC
  19291. * @param val2: Value for SC
  19292. * @param val3: Value for SC
  19293. * @param val4: Value for SC
  19294. * @author: malufett
  19295. */
  19296. BUILDIN_FUNC(montransform) {
  19297. TBL_PC *sd;
  19298. enum sc_type type;
  19299. int tick, mob_id, val1, val2, val3, val4;
  19300. val1 = val2 = val3 = val4 = 0;
  19301. if( !script_rid2sd(sd) )
  19302. return SCRIPT_CMD_FAILURE;
  19303. if( script_isstring(st, 2) )
  19304. mob_id = mobdb_searchname(script_getstr(st, 2));
  19305. else
  19306. mob_id = mobdb_checkid(script_getnum(st, 2));
  19307. tick = script_getnum(st, 3);
  19308. if (script_hasdata(st, 4))
  19309. type = (sc_type)script_getnum(st, 4);
  19310. else
  19311. type = SC_NONE;
  19312. if (mob_id == 0) {
  19313. if( script_isstring(st, 2) )
  19314. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  19315. else
  19316. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  19317. return SCRIPT_CMD_FAILURE;
  19318. }
  19319. if (mob_id == MOBID_EMPERIUM) {
  19320. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  19321. return SCRIPT_CMD_FAILURE;
  19322. }
  19323. if (!(type >= SC_NONE && type < SC_MAX)) {
  19324. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  19325. return SCRIPT_CMD_FAILURE;
  19326. }
  19327. if (script_hasdata(st, 5))
  19328. val1 = script_getnum(st, 5);
  19329. if (script_hasdata(st, 6))
  19330. val2 = script_getnum(st, 6);
  19331. if (script_hasdata(st, 7))
  19332. val3 = script_getnum(st, 7);
  19333. if (script_hasdata(st, 8))
  19334. val4 = script_getnum(st, 8);
  19335. if (tick != 0) {
  19336. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  19337. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  19338. return SCRIPT_CMD_FAILURE;
  19339. }
  19340. if (sd->disguise){
  19341. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  19342. return SCRIPT_CMD_FAILURE;
  19343. }
  19344. if (!strcmp(script_getfuncname(st), "active_transform")) {
  19345. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19346. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19347. } else {
  19348. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19349. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19350. }
  19351. if (type != SC_NONE)
  19352. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  19353. }
  19354. return SCRIPT_CMD_SUCCESS;
  19355. }
  19356. /**
  19357. * Attach script to player for certain duration
  19358. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  19359. * @param "script code"
  19360. * @param duration
  19361. * @param flag
  19362. * @param icon
  19363. * @param char_id
  19364. * @author [Cydh]
  19365. **/
  19366. BUILDIN_FUNC(bonus_script) {
  19367. uint16 flag = 0;
  19368. int16 icon = EFST_BLANK;
  19369. uint32 dur;
  19370. uint8 type = 0;
  19371. TBL_PC* sd;
  19372. const char *script_str = NULL;
  19373. struct s_bonus_script_entry *entry = NULL;
  19374. if ( !script_charid2sd(7,sd) )
  19375. return SCRIPT_CMD_FAILURE;
  19376. script_str = script_getstr(st,2);
  19377. dur = 1000 * abs(script_getnum(st,3));
  19378. FETCH(4, flag);
  19379. FETCH(5, type);
  19380. FETCH(6, icon);
  19381. // No Script string, No Duration!
  19382. if (script_str[0] == '\0' || !dur) {
  19383. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  19384. return SCRIPT_CMD_FAILURE;
  19385. }
  19386. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  19387. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  19388. return SCRIPT_CMD_FAILURE;
  19389. }
  19390. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  19391. icon = EFST_BLANK;
  19392. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_types)icon, flag, type))) {
  19393. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  19394. status_calc_pc(sd,SCO_NONE);
  19395. }
  19396. return SCRIPT_CMD_SUCCESS;
  19397. }
  19398. /**
  19399. * Removes all bonus script from player
  19400. * bonus_script_clear {<flag>,{<char_id>}};
  19401. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  19402. * @param char_id Clear script from this character
  19403. * @author [Cydh]
  19404. */
  19405. BUILDIN_FUNC(bonus_script_clear) {
  19406. TBL_PC* sd;
  19407. bool flag = false;
  19408. if (!script_charid2sd(3,sd))
  19409. return SCRIPT_CMD_FAILURE;
  19410. if (script_hasdata(st,2))
  19411. flag = script_getnum(st,2) != 0;
  19412. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  19413. return SCRIPT_CMD_SUCCESS;
  19414. }
  19415. /** Allows player to use atcommand while talking with NPC
  19416. * enable_command;
  19417. * @author [Cydh], [Kichi]
  19418. */
  19419. BUILDIN_FUNC(enable_command) {
  19420. TBL_PC* sd;
  19421. if (!script_rid2sd(sd))
  19422. return SCRIPT_CMD_FAILURE;
  19423. sd->state.disable_atcommand_on_npc = 0;
  19424. return SCRIPT_CMD_SUCCESS;
  19425. }
  19426. /** Prevents player to use atcommand while talking with NPC
  19427. * disable_command;
  19428. * @author [Cydh], [Kichi]
  19429. */
  19430. BUILDIN_FUNC(disable_command) {
  19431. TBL_PC* sd;
  19432. if (!script_rid2sd(sd))
  19433. return SCRIPT_CMD_FAILURE;
  19434. sd->state.disable_atcommand_on_npc = 1;
  19435. return SCRIPT_CMD_SUCCESS;
  19436. }
  19437. /** Get the information of the members of a guild by type.
  19438. * getguildmember <guild_id>{,<type>};
  19439. * @param guild_id: ID of guild
  19440. * @param type: Type of option (optional)
  19441. */
  19442. BUILDIN_FUNC(getguildmember)
  19443. {
  19444. struct guild *g = NULL;
  19445. uint8 j = 0;
  19446. g = guild_search(script_getnum(st,2));
  19447. if (g) {
  19448. uint8 i, type = 0;
  19449. struct script_data *data = NULL;
  19450. char *varname = NULL;
  19451. if (script_hasdata(st,3))
  19452. type = script_getnum(st,3);
  19453. if (script_hasdata(st,4)) {
  19454. data = script_getdata(st, 4);
  19455. if (!data_isreference(data)) {
  19456. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  19457. return SCRIPT_CMD_FAILURE;
  19458. }
  19459. varname = reference_getname(data);
  19460. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  19461. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  19462. return SCRIPT_CMD_FAILURE;
  19463. }
  19464. if (not_server_variable(*varname)) {
  19465. struct map_session_data *sd;
  19466. if (!script_rid2sd(sd)) {
  19467. ShowError("buildin_getguildmember: Cannot use a player variable '%s' if no player is attached.\n", varname);
  19468. return SCRIPT_CMD_FAILURE;
  19469. }
  19470. }
  19471. }
  19472. for (i = 0; i < MAX_GUILD; i++) {
  19473. if (g->member[i].account_id) {
  19474. switch (type) {
  19475. case 2:
  19476. if (data)
  19477. setd_sub_num( st, NULL, varname, j, g->member[i].account_id, data->ref );
  19478. else
  19479. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  19480. break;
  19481. case 1:
  19482. if (data)
  19483. setd_sub_num( st, NULL, varname, j, g->member[i].char_id, data->ref );
  19484. else
  19485. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  19486. break;
  19487. default:
  19488. if (data)
  19489. setd_sub_str( st, NULL, varname, j, g->member[i].name, data->ref );
  19490. else
  19491. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  19492. break;
  19493. }
  19494. j++;
  19495. }
  19496. }
  19497. }
  19498. mapreg_setreg(add_str("$@guildmembercount"), j);
  19499. script_pushint(st, j);
  19500. return SCRIPT_CMD_SUCCESS;
  19501. }
  19502. /** Adds spirit ball to player for 'duration' in milisecond
  19503. * addspiritball <count>,<duration>{,<char_id>};
  19504. * @param count How many spirit ball will be added
  19505. * @param duration How long spiritball is active until it disappears
  19506. * @param char_id Target player (Optional)
  19507. * @author [Cydh]
  19508. */
  19509. BUILDIN_FUNC(addspiritball) {
  19510. uint8 i, count;
  19511. uint16 duration;
  19512. struct map_session_data *sd = NULL;
  19513. if (script_hasdata(st,4)) {
  19514. if (!script_isstring(st,4))
  19515. sd = map_charid2sd(script_getnum(st,4));
  19516. else
  19517. sd = map_nick2sd(script_getstr(st,4),false);
  19518. }
  19519. else
  19520. script_rid2sd(sd);
  19521. if (!sd)
  19522. return SCRIPT_CMD_FAILURE;
  19523. count = script_getnum(st,2);
  19524. if (count == 0)
  19525. return SCRIPT_CMD_SUCCESS;
  19526. duration = script_getnum(st,3);
  19527. for (i = 0; i < count; i++)
  19528. pc_addspiritball(sd,duration,10);
  19529. return SCRIPT_CMD_SUCCESS;
  19530. }
  19531. /** Deletes the spirit ball(s) from player
  19532. * delspiritball <count>{,<char_id>};
  19533. * @param count How many spirit ball will be deleted
  19534. * @param char_id Target player (Optional)
  19535. * @author [Cydh]
  19536. */
  19537. BUILDIN_FUNC(delspiritball) {
  19538. uint8 count;
  19539. struct map_session_data *sd = NULL;
  19540. if (script_hasdata(st,3)) {
  19541. if (!script_isstring(st,3))
  19542. sd = map_charid2sd(script_getnum(st,3));
  19543. else
  19544. sd = map_nick2sd(script_getstr(st,3),false);
  19545. }
  19546. else
  19547. script_rid2sd(sd);
  19548. if (!sd)
  19549. return SCRIPT_CMD_FAILURE;
  19550. count = script_getnum(st,2);
  19551. if (count == 0)
  19552. count = 1;
  19553. pc_delspiritball(sd,count,0);
  19554. return SCRIPT_CMD_SUCCESS;
  19555. }
  19556. /** Counts the spirit ball that player has
  19557. * countspiritball {<char_id>};
  19558. * @param char_id Target player (Optional)
  19559. * @author [Cydh]
  19560. */
  19561. BUILDIN_FUNC(countspiritball) {
  19562. struct map_session_data *sd;
  19563. if (script_hasdata(st,2)) {
  19564. if (!script_isstring(st,2))
  19565. sd = map_charid2sd(script_getnum(st,2));
  19566. else
  19567. sd = map_nick2sd(script_getstr(st,2),false);
  19568. }
  19569. else
  19570. script_rid2sd(sd);
  19571. if (!sd)
  19572. return SCRIPT_CMD_FAILURE;
  19573. script_pushint(st,sd->spiritball);
  19574. return SCRIPT_CMD_SUCCESS;
  19575. }
  19576. /** Merges separated stackable items because of guid
  19577. * mergeitem {<char_id>};
  19578. * @param char_id Char ID (Optional)
  19579. * @author [Cydh]
  19580. */
  19581. BUILDIN_FUNC(mergeitem) {
  19582. struct map_session_data *sd;
  19583. if (!script_charid2sd(2, sd))
  19584. return SCRIPT_CMD_FAILURE;
  19585. clif_merge_item_open(sd);
  19586. return SCRIPT_CMD_SUCCESS;
  19587. }
  19588. /** Merges separated stackable items because of guid (Unofficial)
  19589. * mergeitem2 {<item_id>,{<char_id>}};
  19590. * mergeitem2 {"<item name>",{<char_id>}};
  19591. * @param item Item ID/Name for merging specific item (Optional)
  19592. * @author [Cydh]
  19593. */
  19594. BUILDIN_FUNC(mergeitem2) {
  19595. struct map_session_data *sd;
  19596. struct item *items = NULL;
  19597. uint16 i, count = 0;
  19598. t_itemid nameid = 0;
  19599. if (!script_charid2sd(3, sd))
  19600. return SCRIPT_CMD_FAILURE;
  19601. if (script_hasdata(st, 2)) {
  19602. if (script_isstring(st, 2)) {// "<item name>"
  19603. const char *name = script_getstr(st, 2);
  19604. struct item_data *id;
  19605. if (!(id = itemdb_searchname(name))) {
  19606. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  19607. script_pushint(st, count);
  19608. return SCRIPT_CMD_FAILURE;
  19609. }
  19610. nameid = id->nameid;
  19611. } else {// <item id>
  19612. nameid = script_getnum(st, 2);
  19613. if (!itemdb_exists(nameid)) {
  19614. ShowError("buildin_mergeitem: Nonexistant item %u requested.\n", nameid);
  19615. script_pushint(st, count);
  19616. return SCRIPT_CMD_FAILURE;
  19617. }
  19618. }
  19619. }
  19620. for (i = 0; i < MAX_INVENTORY; i++) {
  19621. struct item *it = &sd->inventory.u.items_inventory[i];
  19622. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  19623. continue;
  19624. if ((!nameid || (nameid == it->nameid))) {
  19625. uint8 k;
  19626. if (!count) {
  19627. CREATE(items, struct item, 1);
  19628. memcpy(&items[count++], it, sizeof(struct item));
  19629. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19630. continue;
  19631. }
  19632. for (k = 0; k < count; k++) {
  19633. // Find Match
  19634. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  19635. items[k].amount += it->amount;
  19636. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19637. break;
  19638. }
  19639. }
  19640. if (k >= count) {
  19641. // New entry
  19642. RECREATE(items, struct item, count+1);
  19643. memcpy(&items[count++], it, sizeof(struct item));
  19644. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19645. }
  19646. }
  19647. }
  19648. if (!items) // Nothing todo here
  19649. return SCRIPT_CMD_SUCCESS;
  19650. // Retrieve the items
  19651. for (i = 0; i < count; i++) {
  19652. uint8 flag = 0;
  19653. if (!&items[i])
  19654. continue;
  19655. items[i].id = 0;
  19656. items[i].unique_id = 0;
  19657. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  19658. clif_additem(sd, i, items[i].amount, flag);
  19659. }
  19660. aFree(items);
  19661. script_pushint(st, count);
  19662. return SCRIPT_CMD_SUCCESS;
  19663. }
  19664. /**
  19665. * Update an entry from NPC shop.
  19666. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  19667. **/
  19668. BUILDIN_FUNC(npcshopupdate) {
  19669. const char* npcname = script_getstr(st, 2);
  19670. struct npc_data* nd = npc_name2id(npcname);
  19671. t_itemid nameid = script_getnum(st, 3);
  19672. int price = script_getnum(st, 4);
  19673. #if PACKETVER >= 20131223
  19674. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  19675. #endif
  19676. int i;
  19677. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  19678. script_pushint(st,0);
  19679. return SCRIPT_CMD_FAILURE;
  19680. }
  19681. if (!nd->u.shop.count) {
  19682. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  19683. script_pushint(st,0);
  19684. return SCRIPT_CMD_FAILURE;
  19685. }
  19686. for (i = 0; i < nd->u.shop.count; i++) {
  19687. if (nd->u.shop.shop_item[i].nameid == nameid) {
  19688. if (price != 0)
  19689. nd->u.shop.shop_item[i].value = price;
  19690. #if PACKETVER >= 20131223
  19691. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  19692. nd->u.shop.shop_item[i].qty = stock;
  19693. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  19694. }
  19695. #endif
  19696. }
  19697. }
  19698. script_pushint(st,1);
  19699. return SCRIPT_CMD_SUCCESS;
  19700. }
  19701. // Clan System
  19702. BUILDIN_FUNC(clan_join){
  19703. struct map_session_data *sd;
  19704. int clan_id = script_getnum(st,2);
  19705. if( !script_charid2sd( 3, sd ) ){
  19706. script_pushint(st, false);
  19707. return SCRIPT_CMD_FAILURE;
  19708. }
  19709. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  19710. script_pushint(st, true);
  19711. else
  19712. script_pushint(st, false);
  19713. return SCRIPT_CMD_SUCCESS;
  19714. }
  19715. BUILDIN_FUNC(clan_leave){
  19716. struct map_session_data *sd;
  19717. if( !script_charid2sd( 2, sd ) ){
  19718. script_pushint(st, false);
  19719. return SCRIPT_CMD_FAILURE;
  19720. }
  19721. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  19722. script_pushint(st, true);
  19723. else
  19724. script_pushint(st, false);
  19725. return SCRIPT_CMD_SUCCESS;
  19726. }
  19727. /**
  19728. * Get rid from running script.
  19729. * getattachedrid();
  19730. **/
  19731. BUILDIN_FUNC(getattachedrid) {
  19732. script_pushint(st, st->rid);
  19733. return SCRIPT_CMD_SUCCESS;
  19734. }
  19735. /**
  19736. * Get variable from a Player
  19737. * getvar <variable>,<char_id>;
  19738. */
  19739. BUILDIN_FUNC(getvar) {
  19740. int char_id = script_getnum(st, 3);
  19741. struct map_session_data *sd = map_charid2sd(char_id);
  19742. struct script_data *data = NULL;
  19743. const char *name = NULL;
  19744. if (!sd) {
  19745. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  19746. return SCRIPT_CMD_FAILURE;
  19747. }
  19748. data = script_getdata(st, 2);
  19749. if (!data_isreference(data)) {
  19750. ShowError("buildin_getvar: Not a variable\n");
  19751. script_reportdata(data);
  19752. script_pushnil(st);
  19753. st->state = END;
  19754. return SCRIPT_CMD_FAILURE;
  19755. }
  19756. name = reference_getname(data);
  19757. if (reference_toparam(data)) {
  19758. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  19759. script_reportdata(data);
  19760. script_pushnil(st);
  19761. st->state = END;
  19762. return SCRIPT_CMD_FAILURE;
  19763. }
  19764. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  19765. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  19766. script_reportdata(data);
  19767. script_pushnil(st);
  19768. st->state = END;
  19769. return SCRIPT_CMD_FAILURE;
  19770. }
  19771. get_val_(st, data, sd);
  19772. if (data_isint(data))
  19773. script_pushint(st, conv_num_(st, data, sd));
  19774. else
  19775. script_pushstrcopy(st, conv_str_(st, data, sd));
  19776. return SCRIPT_CMD_SUCCESS;
  19777. }
  19778. /**
  19779. * Display script message
  19780. * showscript "<message>"{,<GID>,<flag>};
  19781. * @param flag: Specify target
  19782. * AREA - Message is sent to players in the vicinity of the source (default).
  19783. * SELF - Message is sent only to player attached.
  19784. **/
  19785. BUILDIN_FUNC(showscript) {
  19786. struct block_list *bl = NULL;
  19787. const char *msg = script_getstr(st,2);
  19788. int id = 0;
  19789. send_target target = AREA;
  19790. if (script_hasdata(st,3)) {
  19791. id = script_getnum(st,3);
  19792. bl = map_id2bl(id);
  19793. }
  19794. else {
  19795. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  19796. }
  19797. if (!bl) {
  19798. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  19799. script_pushint(st,0);
  19800. return SCRIPT_CMD_FAILURE;
  19801. }
  19802. if (script_hasdata(st, 4)) {
  19803. target = static_cast<send_target>(script_getnum(st, 4));
  19804. if (target == SELF && map_id2sd(bl->id) == NULL) {
  19805. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  19806. return SCRIPT_CMD_FAILURE;
  19807. }
  19808. }
  19809. clif_showscript(bl, msg, target);
  19810. script_pushint(st,1);
  19811. return SCRIPT_CMD_SUCCESS;
  19812. }
  19813. /**
  19814. * Ignore the SECURE_NPCTIMEOUT function.
  19815. * ignoretimeout <flag>{,<char_id>};
  19816. */
  19817. BUILDIN_FUNC(ignoretimeout)
  19818. {
  19819. #ifdef SECURE_NPCTIMEOUT
  19820. struct map_session_data *sd = NULL;
  19821. if (script_hasdata(st,3)) {
  19822. if (!script_isstring(st,3))
  19823. sd = map_charid2sd(script_getnum(st,3));
  19824. else
  19825. sd = map_nick2sd(script_getstr(st,3),false);
  19826. } else
  19827. script_rid2sd(sd);
  19828. if (!sd)
  19829. return SCRIPT_CMD_FAILURE;
  19830. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  19831. #endif
  19832. return SCRIPT_CMD_SUCCESS;
  19833. }
  19834. /**
  19835. * geteleminfo <type>{,<char_id>};
  19836. **/
  19837. BUILDIN_FUNC(geteleminfo) {
  19838. TBL_ELEM *ed = NULL;
  19839. TBL_PC *sd = NULL;
  19840. int type = script_getnum(st,2);
  19841. if (!script_charid2sd(3, sd)) {
  19842. script_pushint(st, 0);
  19843. return SCRIPT_CMD_SUCCESS;
  19844. }
  19845. if (!(ed = sd->ed)) {
  19846. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  19847. script_pushint(st, 0);
  19848. return SCRIPT_CMD_SUCCESS;
  19849. }
  19850. switch (type) {
  19851. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  19852. case 1: script_pushint(st, ed->bl.id); break;
  19853. default:
  19854. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  19855. script_pushint(st, 0);
  19856. return SCRIPT_CMD_FAILURE;
  19857. }
  19858. return SCRIPT_CMD_SUCCESS;
  19859. }
  19860. /**
  19861. * opendressroom(<flag>{,<char_id>});
  19862. */
  19863. BUILDIN_FUNC(opendressroom)
  19864. {
  19865. #if PACKETVER >= 20150513
  19866. int flag = 1;
  19867. TBL_PC* sd;
  19868. if( script_hasdata(st,2) )
  19869. flag = script_getnum(st,2);
  19870. if (!script_charid2sd(3, sd))
  19871. return SCRIPT_CMD_FAILURE;
  19872. clif_dressing_room(sd, flag);
  19873. return SCRIPT_CMD_SUCCESS;
  19874. #else
  19875. return SCRIPT_CMD_FAILURE;
  19876. #endif
  19877. }
  19878. /**
  19879. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  19880. */
  19881. BUILDIN_FUNC(navigateto){
  19882. #if PACKETVER >= 20111010
  19883. TBL_PC* sd;
  19884. const char *mapname;
  19885. uint16 x = 0, y = 0, monster_id = 0;
  19886. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  19887. bool hideWindow = true;
  19888. mapname = script_getstr(st,2);
  19889. if( script_hasdata(st,3) )
  19890. x = script_getnum(st,3);
  19891. if( script_hasdata(st,4) )
  19892. y = script_getnum(st,4);
  19893. if( script_hasdata(st,5) )
  19894. flag = (uint8)script_getnum(st,5);
  19895. if( script_hasdata(st,6) )
  19896. hideWindow = script_getnum(st,6) ? true : false;
  19897. if( script_hasdata(st,7) )
  19898. monster_id = script_getnum(st,7);
  19899. if (!script_charid2sd(8, sd))
  19900. return SCRIPT_CMD_FAILURE;
  19901. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  19902. return SCRIPT_CMD_SUCCESS;
  19903. #else
  19904. return SCRIPT_CMD_FAILURE;
  19905. #endif
  19906. }
  19907. /**
  19908. * adopt("<parent_name>","<baby_name>");
  19909. * adopt(<parent_id>,<baby_id>);
  19910. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  19911. */
  19912. BUILDIN_FUNC(adopt)
  19913. {
  19914. TBL_PC *sd, *b_sd;
  19915. enum adopt_responses response;
  19916. if (script_isstring(st, 2)) {
  19917. const char *name = script_getstr(st, 2);
  19918. sd = map_nick2sd(name,false);
  19919. if (sd == NULL) {
  19920. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  19921. return SCRIPT_CMD_FAILURE;
  19922. }
  19923. } else {
  19924. uint32 char_id = script_getnum(st, 2);
  19925. sd = map_charid2sd(char_id);
  19926. if (sd == NULL) {
  19927. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  19928. return SCRIPT_CMD_FAILURE;
  19929. }
  19930. }
  19931. if (script_isstring(st, 3)) {
  19932. const char *name = script_getstr(st, 3);
  19933. b_sd = map_nick2sd(name,false);
  19934. if (b_sd == NULL) {
  19935. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  19936. return SCRIPT_CMD_FAILURE;
  19937. }
  19938. } else {
  19939. uint32 char_id = script_getnum(st, 3);
  19940. b_sd = map_charid2sd(char_id);
  19941. if (b_sd == NULL) {
  19942. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  19943. return SCRIPT_CMD_FAILURE;
  19944. }
  19945. }
  19946. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  19947. if (response == ADOPT_ALLOWED) {
  19948. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  19949. b_sd->adopt_invite = sd->status.account_id;
  19950. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  19951. script_pushint(st, ADOPT_ALLOWED);
  19952. return SCRIPT_CMD_SUCCESS;
  19953. }
  19954. script_pushint(st, response);
  19955. return SCRIPT_CMD_FAILURE;
  19956. }
  19957. /**
  19958. * Returns the minimum or maximum of all the given parameters for integer variables.
  19959. *
  19960. * min( <value or array>{,value or array 2,...} );
  19961. * minimum( <value or array>{,value or array 2,...} );
  19962. * max( <value or array>{,value or array 2,...} );
  19963. * maximum( <value or array>{,value or array 2,...} );
  19964. */
  19965. BUILDIN_FUNC(minmax){
  19966. char *functionname;
  19967. unsigned int i;
  19968. int64 value;
  19969. // Function pointer for our comparison function (either min or max at the moment)
  19970. int64 (*func)(int64, int64);
  19971. // Get the real function name
  19972. functionname = script_getfuncname(st);
  19973. // Our data should start at offset 2
  19974. i = 2;
  19975. if( !script_hasdata( st, i ) ){
  19976. ShowError( "buildin_%s: no arguments given!\n", functionname );
  19977. st->state = END;
  19978. return SCRIPT_CMD_FAILURE;
  19979. }
  19980. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  19981. value = SCRIPT_INT_MAX;
  19982. func = i64min;
  19983. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  19984. value = SCRIPT_INT_MIN;
  19985. func = i64max;
  19986. }else{
  19987. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  19988. st->state = END;
  19989. return SCRIPT_CMD_FAILURE;
  19990. }
  19991. // As long as we have data on our script stack
  19992. while( script_hasdata(st,i) ){
  19993. struct script_data *data;
  19994. // Get the next piece of data from the script stack
  19995. data = script_getdata( st, i );
  19996. // Is the current parameter a single integer?
  19997. if( data_isint( data ) ){
  19998. value = func( value, script_getnum( st, i ) );
  19999. // Is the current parameter an array variable?
  20000. }else if( data_isreference( data ) ){
  20001. const char *name;
  20002. struct map_session_data* sd;
  20003. unsigned int start, end;
  20004. // Get the name of the variable
  20005. name = reference_getname(data);
  20006. // Check if it's a string variable
  20007. if( is_string_variable( name ) ){
  20008. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  20009. script_reportdata( data );
  20010. st->state = END;
  20011. return SCRIPT_CMD_FAILURE;
  20012. }
  20013. // Get the session data, if a player is attached
  20014. sd = st->rid ? map_id2sd(st->rid) : NULL;
  20015. if (not_server_variable(*name) && !script_rid2sd(sd)) {
  20016. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", functionname, name);
  20017. return SCRIPT_CMD_FAILURE;
  20018. }
  20019. // Try to find the array's source pointer
  20020. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  20021. ShowError( "buildin_%s: not a array!\n", functionname );
  20022. script_reportdata( data );
  20023. st->state = END;
  20024. return SCRIPT_CMD_FAILURE;
  20025. }
  20026. // Get the start and end indices of the array
  20027. start = reference_getindex( data );
  20028. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  20029. // Skip empty arrays
  20030. if( start < end ){
  20031. int id;
  20032. // For getting the values we need the id of the array
  20033. id = reference_getid( data );
  20034. // Loop through each value stored in the array
  20035. for( ; start < end; start++ ){
  20036. value = func( value, get_val2_num( st, reference_uid( id, start ), reference_getref( data ) ) );
  20037. }
  20038. }
  20039. else {
  20040. value = func( value, 0 );
  20041. }
  20042. }else{
  20043. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  20044. script_reportdata( data );
  20045. st->state = END;
  20046. return SCRIPT_CMD_FAILURE;
  20047. }
  20048. // Continue with the next stack entry
  20049. i++;
  20050. }
  20051. script_pushint( st, value );
  20052. return SCRIPT_CMD_SUCCESS;
  20053. }
  20054. /**
  20055. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  20056. * Unlike `getexp` that affected by some adjustments.
  20057. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  20058. * @author [Cydh]
  20059. **/
  20060. BUILDIN_FUNC(getexp2) {
  20061. TBL_PC *sd = NULL;
  20062. int base_exp = script_getnum(st, 2);
  20063. int job_exp = script_getnum(st, 3);
  20064. if (!script_charid2sd(4, sd))
  20065. return SCRIPT_CMD_FAILURE;
  20066. if (base_exp == 0 && job_exp == 0)
  20067. return SCRIPT_CMD_SUCCESS;
  20068. if (base_exp > 0)
  20069. pc_gainexp(sd, NULL, base_exp, 0, 2);
  20070. else if (base_exp < 0)
  20071. pc_lostexp(sd, base_exp * -1, 0);
  20072. if (job_exp > 0)
  20073. pc_gainexp(sd, NULL, 0, job_exp, 2);
  20074. else if (job_exp < 0)
  20075. pc_lostexp(sd, 0, job_exp * -1);
  20076. return SCRIPT_CMD_SUCCESS;
  20077. }
  20078. /**
  20079. * Force stat recalculation of sd
  20080. * recalculatestat;
  20081. * @author [secretdataz]
  20082. **/
  20083. BUILDIN_FUNC(recalculatestat) {
  20084. TBL_PC* sd;
  20085. if (!script_rid2sd(sd))
  20086. return SCRIPT_CMD_FAILURE;
  20087. status_calc_pc(sd, SCO_FORCE);
  20088. return SCRIPT_CMD_SUCCESS;
  20089. }
  20090. BUILDIN_FUNC(hateffect){
  20091. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  20092. struct map_session_data* sd;
  20093. if( !script_rid2sd(sd) )
  20094. return SCRIPT_CMD_FAILURE;
  20095. int16 effectID = script_getnum(st,2);
  20096. bool enable = script_getnum(st,3) ? true : false;
  20097. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  20098. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  20099. return SCRIPT_CMD_FAILURE;
  20100. }
  20101. auto it = util::vector_get( sd->hatEffects, effectID );
  20102. if( enable ){
  20103. if( it != sd->hatEffects.end() ){
  20104. return SCRIPT_CMD_SUCCESS;
  20105. }
  20106. sd->hatEffects.push_back( effectID );
  20107. }else{
  20108. if( it == sd->hatEffects.end() ){
  20109. return SCRIPT_CMD_SUCCESS;
  20110. }
  20111. util::vector_erase_if_exists( sd->hatEffects, effectID );
  20112. }
  20113. if( !sd->state.connect_new ){
  20114. clif_hat_effect_single( sd, effectID, enable );
  20115. }
  20116. #endif
  20117. return SCRIPT_CMD_SUCCESS;
  20118. }
  20119. /**
  20120. * Retrieves param of current random option. Intended for random option script only.
  20121. * getrandomoptinfo(<type>);
  20122. * @author [secretdataz]
  20123. **/
  20124. BUILDIN_FUNC(getrandomoptinfo) {
  20125. struct map_session_data *sd;
  20126. int val;
  20127. 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) {
  20128. int param = script_getnum(st, 2);
  20129. switch (param) {
  20130. case ROA_ID:
  20131. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  20132. break;
  20133. case ROA_VALUE:
  20134. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  20135. break;
  20136. case ROA_PARAM:
  20137. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  20138. break;
  20139. default:
  20140. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  20141. val = 0;
  20142. break;
  20143. }
  20144. script_pushint(st, val);
  20145. }
  20146. else {
  20147. script_pushint(st, 0);
  20148. }
  20149. return SCRIPT_CMD_SUCCESS;
  20150. }
  20151. /**
  20152. * Retrieves a random option on an equipped item.
  20153. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  20154. * @author [secretdataz]
  20155. */
  20156. BUILDIN_FUNC(getequiprandomoption) {
  20157. struct map_session_data *sd;
  20158. int val;
  20159. short i = -1;
  20160. int pos = script_getnum(st, 2);
  20161. int index = script_getnum(st, 3);
  20162. int type = script_getnum(st, 4);
  20163. if (!script_charid2sd(5, sd)) {
  20164. script_pushint(st, -1);
  20165. return SCRIPT_CMD_FAILURE;
  20166. }
  20167. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20168. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  20169. script_pushint(st, -1);
  20170. return SCRIPT_CMD_FAILURE;
  20171. }
  20172. if (equip_index_check(pos))
  20173. i = pc_checkequip(sd, equip_bitmask[pos]);
  20174. if (i < 0) {
  20175. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20176. script_pushint(st, -1);
  20177. return SCRIPT_CMD_FAILURE;
  20178. }
  20179. switch (type) {
  20180. case ROA_ID:
  20181. val = sd->inventory.u.items_inventory[i].option[index].id;
  20182. break;
  20183. case ROA_VALUE:
  20184. val = sd->inventory.u.items_inventory[i].option[index].value;
  20185. break;
  20186. case ROA_PARAM:
  20187. val = sd->inventory.u.items_inventory[i].option[index].param;
  20188. break;
  20189. default:
  20190. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  20191. val = 0;
  20192. break;
  20193. }
  20194. script_pushint(st, val);
  20195. return SCRIPT_CMD_SUCCESS;
  20196. }
  20197. /**
  20198. * Adds a random option on a random option slot on an equipped item and overwrites
  20199. * existing random option in the process.
  20200. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  20201. * @author [secretdataz]
  20202. */
  20203. BUILDIN_FUNC(setrandomoption) {
  20204. struct map_session_data *sd;
  20205. int pos, index, id, value, param, ep;
  20206. int i = -1;
  20207. if (!script_charid2sd(7, sd))
  20208. return SCRIPT_CMD_FAILURE;
  20209. pos = script_getnum(st, 2);
  20210. index = script_getnum(st, 3);
  20211. id = script_getnum(st, 4);
  20212. value = script_getnum(st, 5);
  20213. param = script_getnum(st, 6);
  20214. std::shared_ptr<s_random_opt_data> opt = random_option_db.find(static_cast<uint16>(id));
  20215. if (opt == nullptr) {
  20216. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  20217. script_pushint(st, 0);
  20218. return SCRIPT_CMD_FAILURE;
  20219. }
  20220. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  20221. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  20222. script_pushint(st, 0);
  20223. return SCRIPT_CMD_FAILURE;
  20224. }
  20225. if (equip_index_check(pos))
  20226. i = pc_checkequip(sd, equip_bitmask[pos]);
  20227. if (i >= 0) {
  20228. ep = sd->inventory.u.items_inventory[i].equip;
  20229. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20230. pc_unequipitem(sd, i, 2);
  20231. sd->inventory.u.items_inventory[i].option[index].id = id;
  20232. sd->inventory.u.items_inventory[i].option[index].value = value;
  20233. sd->inventory.u.items_inventory[i].option[index].param = param;
  20234. clif_delitem(sd, i, 1, 3);
  20235. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20236. clif_additem(sd, i, 1, 0);
  20237. pc_equipitem(sd, i, ep);
  20238. script_pushint(st, 1);
  20239. return SCRIPT_CMD_SUCCESS;
  20240. }
  20241. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20242. script_pushint(st, 0);
  20243. return SCRIPT_CMD_FAILURE;
  20244. }
  20245. /// Returns the number of stat points needed to change the specified stat by val.
  20246. /// If val is negative, returns the number of stat points that would be needed to
  20247. /// raise the specified stat from (current value - val) to current value.
  20248. /// *needed_status_point(<type>,<val>{,<char id>});
  20249. /// @author [secretdataz]
  20250. BUILDIN_FUNC(needed_status_point) {
  20251. struct map_session_data *sd;
  20252. int type, val;
  20253. if (!script_charid2sd(4, sd))
  20254. return SCRIPT_CMD_FAILURE;
  20255. type = script_getnum(st, 2);
  20256. val = script_getnum(st, 3);
  20257. script_pushint(st, pc_need_status_point(sd, type, val));
  20258. return SCRIPT_CMD_SUCCESS;
  20259. }
  20260. /**
  20261. * jobcanentermap("<mapname>"{,<JobID>});
  20262. * Check if (player with) JobID can enter the map.
  20263. * @param mapname Map name
  20264. * @param JobID Player's JobID (optional)
  20265. **/
  20266. BUILDIN_FUNC(jobcanentermap) {
  20267. const char *mapname = script_getstr(st, 2);
  20268. int mapidx = mapindex_name2id(mapname), m = -1;
  20269. int jobid = 0;
  20270. TBL_PC *sd = NULL;
  20271. if (!mapidx) {// Invalid map
  20272. script_pushint(st, false);
  20273. return SCRIPT_CMD_FAILURE;
  20274. }
  20275. m = map_mapindex2mapid(mapidx);
  20276. if (m == -1) { // Map is on different map server
  20277. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  20278. script_pushint(st, false);
  20279. return SCRIPT_CMD_FAILURE;
  20280. }
  20281. if (script_hasdata(st, 3)) {
  20282. jobid = script_getnum(st, 3);
  20283. } else {
  20284. if (!script_rid2sd(sd)) {
  20285. script_pushint(st, false);
  20286. return SCRIPT_CMD_FAILURE;
  20287. }
  20288. jobid = sd->status.class_;
  20289. }
  20290. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  20291. return SCRIPT_CMD_SUCCESS;
  20292. }
  20293. /**
  20294. * Return alliance information between the two guilds.
  20295. * getguildalliance(<guild id1>,<guild id2>);
  20296. * Return values:
  20297. * -2 - Guild ID1 does not exist
  20298. * -1 - Guild ID2 does not exist
  20299. * 0 - Both guilds have no relation OR guild ID aren't given
  20300. * 1 - Both guilds are allies
  20301. * 2 - Both guilds are antagonists
  20302. */
  20303. BUILDIN_FUNC(getguildalliance)
  20304. {
  20305. struct guild *guild_data1, *guild_data2;
  20306. int guild_id1, guild_id2, i = 0;
  20307. guild_id1 = script_getnum(st,2);
  20308. guild_id2 = script_getnum(st,3);
  20309. if (guild_id1 < 1 || guild_id2 < 1) {
  20310. script_pushint(st, 0);
  20311. return SCRIPT_CMD_SUCCESS;
  20312. }
  20313. if (guild_id1 == guild_id2) {
  20314. script_pushint(st, 1);
  20315. return SCRIPT_CMD_SUCCESS;
  20316. }
  20317. guild_data1 = guild_search(guild_id1);
  20318. guild_data2 = guild_search(guild_id2);
  20319. if (guild_data1 == NULL) {
  20320. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  20321. script_pushint(st, -2);
  20322. return SCRIPT_CMD_FAILURE;
  20323. }
  20324. if (guild_data2 == NULL) {
  20325. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  20326. script_pushint(st, -1);
  20327. return SCRIPT_CMD_FAILURE;
  20328. }
  20329. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  20330. if (i == MAX_GUILDALLIANCE) {
  20331. script_pushint(st, 0);
  20332. return SCRIPT_CMD_SUCCESS;
  20333. }
  20334. if (guild_data1->alliance[i].opposition)
  20335. script_pushint(st, 2);
  20336. else
  20337. script_pushint(st, 1);
  20338. return SCRIPT_CMD_SUCCESS;
  20339. }
  20340. /*
  20341. * openstorage2 <storage_id>,<mode>{,<account_id>}
  20342. * mode @see enum e_storage_mode
  20343. **/
  20344. BUILDIN_FUNC(openstorage2) {
  20345. int stor_id = script_getnum(st, 2);
  20346. TBL_PC *sd = NULL;
  20347. if (!script_accid2sd(4, sd)) {
  20348. script_pushint(st, 0);
  20349. return SCRIPT_CMD_FAILURE;
  20350. }
  20351. if (!storage_exists(stor_id)) {
  20352. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  20353. return SCRIPT_CMD_FAILURE;
  20354. }
  20355. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  20356. return SCRIPT_CMD_SUCCESS;
  20357. }
  20358. /**
  20359. * Create a new channel
  20360. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  20361. * @author [Cydh]
  20362. **/
  20363. BUILDIN_FUNC(channel_create) {
  20364. struct Channel tmp_chan, *ch = NULL;
  20365. const char *chname = script_getstr(st,2), *pass = NULL;
  20366. int i = channel_chk((char*)chname, NULL, 3);
  20367. TBL_PC *sd = NULL;
  20368. if (i != 0) {
  20369. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  20370. script_pushint(st,i);
  20371. return SCRIPT_CMD_FAILURE;
  20372. }
  20373. memset(&tmp_chan, 0, sizeof(struct Channel));
  20374. if (script_hasdata(st,8)) {
  20375. tmp_chan.char_id = script_getnum(st,8);
  20376. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  20377. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  20378. script_pushint(st,-5);
  20379. return SCRIPT_CMD_FAILURE;
  20380. }
  20381. tmp_chan.type = CHAN_TYPE_PRIVATE;
  20382. i = 1;
  20383. }
  20384. else {
  20385. tmp_chan.type = CHAN_TYPE_PUBLIC;
  20386. i = 0;
  20387. }
  20388. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  20389. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  20390. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  20391. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  20392. if (script_hasdata(st,5))
  20393. tmp_chan.opt = script_getnum(st,5);
  20394. else
  20395. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  20396. if (script_hasdata(st,6))
  20397. tmp_chan.msg_delay = script_getnum(st,6);
  20398. else
  20399. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  20400. if (script_hasdata(st,7))
  20401. tmp_chan.color = script_getnum(st,7);
  20402. else
  20403. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  20404. if (!(ch = channel_create(&tmp_chan))) {
  20405. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  20406. script_pushint(st,0);
  20407. return SCRIPT_CMD_FAILURE;
  20408. }
  20409. if (tmp_chan.char_id)
  20410. channel_join(ch, sd);
  20411. script_pushint(st,1);
  20412. return SCRIPT_CMD_SUCCESS;
  20413. }
  20414. /**
  20415. * Set channel option
  20416. * channel_setopt "<chname>",<option>,<value>;
  20417. * @author [Cydh]
  20418. **/
  20419. BUILDIN_FUNC(channel_setopt) {
  20420. struct Channel *ch = NULL;
  20421. const char *chname = script_getstr(st,2);
  20422. int opt = script_getnum(st,3), value = script_getnum(st,4);
  20423. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20424. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  20425. script_pushint(st,0);
  20426. return SCRIPT_CMD_FAILURE;
  20427. }
  20428. switch (opt) {
  20429. case CHAN_OPT_ANNOUNCE_SELF:
  20430. case CHAN_OPT_ANNOUNCE_JOIN:
  20431. case CHAN_OPT_ANNOUNCE_LEAVE:
  20432. case CHAN_OPT_COLOR_OVERRIDE:
  20433. case CHAN_OPT_CAN_CHAT:
  20434. case CHAN_OPT_CAN_LEAVE:
  20435. case CHAN_OPT_AUTOJOIN:
  20436. if (value)
  20437. ch->opt |= opt;
  20438. else
  20439. ch->opt &= ~opt;
  20440. break;
  20441. case CHAN_OPT_MSG_DELAY:
  20442. ch->msg_delay = value;
  20443. break;
  20444. default:
  20445. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  20446. script_pushint(st,0);
  20447. return SCRIPT_CMD_FAILURE;
  20448. }
  20449. script_pushint(st,1);
  20450. return SCRIPT_CMD_SUCCESS;
  20451. }
  20452. /**
  20453. * Set channel color
  20454. * channel_setcolor "<chname>",<color>;
  20455. * @author [Cydh]
  20456. **/
  20457. BUILDIN_FUNC(channel_setcolor) {
  20458. struct Channel *ch = NULL;
  20459. const char *chname = script_getstr(st,2);
  20460. int color = script_getnum(st,3);
  20461. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20462. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  20463. script_pushint(st,0);
  20464. return SCRIPT_CMD_FAILURE;
  20465. }
  20466. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  20467. script_pushint(st,1);
  20468. return SCRIPT_CMD_SUCCESS;
  20469. }
  20470. /**
  20471. * Set channel password
  20472. * channel_setpass "<chname>","<password>";
  20473. * @author [Cydh]
  20474. **/
  20475. BUILDIN_FUNC(channel_setpass) {
  20476. struct Channel *ch = NULL;
  20477. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  20478. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20479. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  20480. script_pushint(st,0);
  20481. return SCRIPT_CMD_FAILURE;
  20482. }
  20483. if (!passwd || !strcmpi(passwd,"null"))
  20484. memset(ch->pass, '\0', sizeof(ch->pass));
  20485. else
  20486. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  20487. script_pushint(st,1);
  20488. return SCRIPT_CMD_SUCCESS;
  20489. }
  20490. /**
  20491. * Set authorized groups
  20492. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  20493. * @author [Cydh]
  20494. **/
  20495. BUILDIN_FUNC(channel_setgroup) {
  20496. struct Channel *ch = NULL;
  20497. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  20498. int i, n = 0, group = 0;
  20499. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20500. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  20501. script_pushint(st,0);
  20502. return SCRIPT_CMD_FAILURE;
  20503. }
  20504. if (funcname[strlen(funcname)-1] == '2') {
  20505. struct script_data *data = script_getdata(st,3);
  20506. const char *varname = reference_getname(data);
  20507. int32 id, idx;
  20508. if (varname[strlen(varname)-1] == '$') {
  20509. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  20510. script_pushint(st,0);
  20511. return SCRIPT_CMD_FAILURE;
  20512. }
  20513. if (not_server_variable(*varname)) {
  20514. struct map_session_data *sd;
  20515. if (!script_rid2sd(sd)) {
  20516. ShowError("buildin_%s: Cannot use a player variable '%s' if no player is attached.\n", funcname, varname);
  20517. return SCRIPT_CMD_FAILURE;
  20518. }
  20519. }
  20520. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  20521. if (n < 1) {
  20522. ShowError("buildin_channel_setgroup: No group id listed.\n");
  20523. script_pushint(st,0);
  20524. return SCRIPT_CMD_FAILURE;
  20525. }
  20526. if (ch->groups)
  20527. aFree(ch->groups);
  20528. ch->groups = NULL;
  20529. ch->group_count = 0;
  20530. id = reference_getid(data);
  20531. idx = reference_getindex(data);
  20532. for (i = 0; i < n; i++) {
  20533. group = (int32)get_val2_num( st, reference_uid( id, idx + i ), reference_getref( data ) );
  20534. if (group == 0) {
  20535. script_pushint(st,1);
  20536. return SCRIPT_CMD_SUCCESS;
  20537. }
  20538. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  20539. ch->groups[ch->group_count-1] = group;
  20540. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20541. }
  20542. }
  20543. else {
  20544. group = script_getnum(st,3);
  20545. n = script_lastdata(st)-1;
  20546. if (n < 1) {
  20547. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  20548. script_pushint(st,0);
  20549. return SCRIPT_CMD_FAILURE;
  20550. }
  20551. if (ch->groups)
  20552. aFree(ch->groups);
  20553. ch->groups = NULL;
  20554. ch->group_count = 0;
  20555. if (group == 0) { // Removed group list
  20556. script_pushint(st,1);
  20557. return SCRIPT_CMD_SUCCESS;
  20558. }
  20559. CREATE(ch->groups, unsigned short, n);
  20560. for (i = 3; i < n+2; i++) {
  20561. ch->groups[ch->group_count++] = script_getnum(st,i);
  20562. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20563. }
  20564. }
  20565. script_pushint(st,n);
  20566. return SCRIPT_CMD_SUCCESS;
  20567. }
  20568. /**
  20569. * Send message on channel
  20570. * channel_chat "<chname>","<message>"{,<color>};
  20571. * @author [Cydh]
  20572. **/
  20573. BUILDIN_FUNC(channel_chat) {
  20574. struct Channel *ch = NULL;
  20575. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  20576. char output[CHAT_SIZE_MAX+1];
  20577. unsigned long color = 0;
  20578. // Check also local channels
  20579. if (chname[0] != '#') { // By Map
  20580. int m = mapindex_name2id(chname);
  20581. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  20582. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  20583. script_pushint(st,0);
  20584. return SCRIPT_CMD_FAILURE;
  20585. }
  20586. if (!(ch = map_getmapdata(m)->channel)) {
  20587. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20588. script_pushint(st,0);
  20589. return SCRIPT_CMD_SUCCESS;
  20590. }
  20591. }
  20592. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  20593. TBL_NPC *nd = map_id2nd(st->oid);
  20594. if (!nd || nd->bl.m == -1) {
  20595. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  20596. script_pushint(st,0);
  20597. return SCRIPT_CMD_FAILURE;
  20598. }
  20599. if (!(ch = map_getmapdata(nd->bl.m)->channel)) {
  20600. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20601. script_pushint(st,0);
  20602. return SCRIPT_CMD_SUCCESS;
  20603. }
  20604. }
  20605. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20606. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  20607. script_pushint(st,0);
  20608. return SCRIPT_CMD_FAILURE;
  20609. }
  20610. if (!ch) {
  20611. script_pushint(st,0);
  20612. return SCRIPT_CMD_FAILURE;
  20613. }
  20614. color = ch->color;
  20615. FETCH(4, color);
  20616. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  20617. clif_channel_msg(ch,output,color);
  20618. script_pushint(st,1);
  20619. return SCRIPT_CMD_SUCCESS;
  20620. }
  20621. /**
  20622. * Ban player from a channel
  20623. * channel_ban "<chname>",<char_id>;
  20624. * @author [Cydh]
  20625. **/
  20626. BUILDIN_FUNC(channel_ban) {
  20627. struct Channel *ch = NULL;
  20628. const char *chname = script_getstr(st,2);
  20629. unsigned int char_id = script_getnum(st,3);
  20630. TBL_PC *tsd;
  20631. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20632. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  20633. script_pushint(st,0);
  20634. return SCRIPT_CMD_FAILURE;
  20635. }
  20636. if (ch->char_id == char_id) {
  20637. script_pushint(st,0);
  20638. return SCRIPT_CMD_SUCCESS;
  20639. }
  20640. tsd = map_charid2sd(char_id);
  20641. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20642. script_pushint(st,0);
  20643. return SCRIPT_CMD_SUCCESS;
  20644. }
  20645. if (!idb_exists(ch->banned, char_id)) {
  20646. struct chan_banentry *cbe;
  20647. char output[CHAT_SIZE_MAX+1];
  20648. CREATE(cbe, struct chan_banentry, 1);
  20649. cbe->char_id = char_id;
  20650. if (tsd) {
  20651. strcpy(cbe->char_name,tsd->status.name);
  20652. channel_clean(ch,tsd,0);
  20653. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  20654. }
  20655. else
  20656. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  20657. idb_put(ch->banned, char_id, cbe);
  20658. clif_channel_msg(ch,output,ch->color);
  20659. }
  20660. script_pushint(st,1);
  20661. return SCRIPT_CMD_SUCCESS;
  20662. }
  20663. /**
  20664. * Ban player from a channel
  20665. * channel_unban "<chname>",<char_id>;
  20666. * @author [Cydh]
  20667. **/
  20668. BUILDIN_FUNC(channel_unban) {
  20669. struct Channel *ch = NULL;
  20670. const char *chname = script_getstr(st,2);
  20671. unsigned int char_id = script_getnum(st,3);
  20672. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20673. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  20674. script_pushint(st,0);
  20675. return SCRIPT_CMD_FAILURE;
  20676. }
  20677. if (ch->char_id == char_id) {
  20678. script_pushint(st,0);
  20679. return SCRIPT_CMD_SUCCESS;
  20680. }
  20681. if (idb_exists(ch->banned, char_id)) {
  20682. char output[CHAT_SIZE_MAX+1];
  20683. TBL_PC *tsd = map_charid2sd(char_id);
  20684. if (tsd)
  20685. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  20686. else
  20687. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  20688. idb_remove(ch->banned, char_id);
  20689. clif_channel_msg(ch,output,ch->color);
  20690. }
  20691. script_pushint(st,1);
  20692. return SCRIPT_CMD_SUCCESS;
  20693. }
  20694. /**
  20695. * Kick player from a channel
  20696. * channel_kick "<chname>",<char_id>;
  20697. * channel_kick "<chname>","<char_name>";
  20698. * @author [Cydh]
  20699. **/
  20700. BUILDIN_FUNC(channel_kick) {
  20701. struct Channel *ch = NULL;
  20702. const char *chname = script_getstr(st,2);
  20703. TBL_PC *tsd = NULL;
  20704. int res = 1;
  20705. if (script_isstring(st, 3)) {
  20706. const char *name = script_getstr(st, 3);
  20707. if (!(tsd = map_nick2sd(name,false))) {
  20708. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", name);
  20709. script_pushint(st,0);
  20710. return SCRIPT_CMD_FAILURE;
  20711. }
  20712. } else {
  20713. int char_id = script_getnum(st, 3);
  20714. if (!(tsd = map_charid2sd(char_id))) {
  20715. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", char_id);
  20716. script_pushint(st,0);
  20717. return SCRIPT_CMD_FAILURE;
  20718. }
  20719. }
  20720. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  20721. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  20722. script_pushint(st,0);
  20723. return SCRIPT_CMD_FAILURE;
  20724. }
  20725. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20726. script_pushint(st,0);
  20727. return SCRIPT_CMD_SUCCESS;
  20728. }
  20729. switch(ch->type){
  20730. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  20731. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  20732. default: res = channel_clean(ch,tsd,0); break;
  20733. }
  20734. if (res == 0) {
  20735. char output[CHAT_SIZE_MAX+1];
  20736. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  20737. clif_channel_msg(ch,output,ch->color);
  20738. }
  20739. script_pushint(st,1);
  20740. return SCRIPT_CMD_SUCCESS;
  20741. }
  20742. /**
  20743. * Delete a channel
  20744. * channel_delete "<chname>";
  20745. * @author [Cydh]
  20746. **/
  20747. BUILDIN_FUNC(channel_delete) {
  20748. struct Channel *ch = NULL;
  20749. const char *chname = script_getstr(st,2);
  20750. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20751. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  20752. script_pushint(st,-1);
  20753. return SCRIPT_CMD_FAILURE;
  20754. }
  20755. script_pushint(st,channel_delete(ch,true));
  20756. return SCRIPT_CMD_SUCCESS;
  20757. }
  20758. BUILDIN_FUNC(unloadnpc) {
  20759. const char *name;
  20760. struct npc_data* nd;
  20761. name = script_getstr(st, 2);
  20762. nd = npc_name2id(name);
  20763. if( nd == NULL ){
  20764. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  20765. return SCRIPT_CMD_FAILURE;
  20766. } else if ( nd->bl.id == st->oid ) {
  20767. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  20768. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  20769. return SCRIPT_CMD_FAILURE;
  20770. }
  20771. npc_unload_duplicates(nd);
  20772. npc_unload(nd, true);
  20773. npc_read_event_script();
  20774. return SCRIPT_CMD_SUCCESS;
  20775. }
  20776. /**
  20777. * Add an achievement to the player's log
  20778. * achievementadd(<achievement ID>{,<char ID>});
  20779. */
  20780. BUILDIN_FUNC(achievementadd) {
  20781. struct map_session_data *sd;
  20782. int achievement_id = script_getnum(st, 2);
  20783. if (!script_charid2sd(3, sd)) {
  20784. script_pushint(st, false);
  20785. return SCRIPT_CMD_FAILURE;
  20786. }
  20787. if (achievement_db.exists(achievement_id) == false) {
  20788. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  20789. script_pushint(st, false);
  20790. return SCRIPT_CMD_FAILURE;
  20791. }
  20792. if( !sd->state.pc_loaded ){
  20793. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20794. return SCRIPT_CMD_SUCCESS;
  20795. }
  20796. if (achievement_add(sd, achievement_id))
  20797. script_pushint(st, true);
  20798. else
  20799. script_pushint(st, false);
  20800. return SCRIPT_CMD_SUCCESS;
  20801. }
  20802. /**
  20803. * Removes an achievement on a player.
  20804. * achievementremove(<achievement ID>{,<char ID>});
  20805. * Just for Atemo. ;)
  20806. */
  20807. BUILDIN_FUNC(achievementremove) {
  20808. struct map_session_data *sd;
  20809. int achievement_id = script_getnum(st, 2);
  20810. if (!script_charid2sd(3, sd)) {
  20811. script_pushint(st, false);
  20812. return SCRIPT_CMD_FAILURE;
  20813. }
  20814. if (achievement_db.exists(achievement_id) == false) {
  20815. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  20816. script_pushint(st, false);
  20817. return SCRIPT_CMD_SUCCESS;
  20818. }
  20819. if( !sd->state.pc_loaded ){
  20820. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20821. return SCRIPT_CMD_SUCCESS;
  20822. }
  20823. if (achievement_remove(sd, achievement_id))
  20824. script_pushint(st, true);
  20825. else
  20826. script_pushint(st, false);
  20827. return SCRIPT_CMD_SUCCESS;
  20828. }
  20829. /**
  20830. * Returns achievement progress
  20831. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  20832. */
  20833. BUILDIN_FUNC(achievementinfo) {
  20834. struct map_session_data *sd;
  20835. int achievement_id = script_getnum(st, 2);
  20836. if (!script_charid2sd(4, sd)) {
  20837. script_pushint(st, false);
  20838. return SCRIPT_CMD_FAILURE;
  20839. }
  20840. if (achievement_db.exists(achievement_id) == false) {
  20841. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  20842. script_pushint(st, false);
  20843. return SCRIPT_CMD_FAILURE;
  20844. }
  20845. if( !sd->state.pc_loaded ){
  20846. script_pushint(st, false);
  20847. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20848. return SCRIPT_CMD_SUCCESS;
  20849. }
  20850. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  20851. return SCRIPT_CMD_SUCCESS;
  20852. }
  20853. /**
  20854. * Award an achievement; Ignores requirements
  20855. * achievementcomplete(<achievement ID>{,<char ID>});
  20856. */
  20857. BUILDIN_FUNC(achievementcomplete) {
  20858. struct map_session_data *sd;
  20859. int i, achievement_id = script_getnum(st, 2);
  20860. if (!script_charid2sd(3, sd)) {
  20861. script_pushint(st, false);
  20862. return SCRIPT_CMD_FAILURE;
  20863. }
  20864. if (achievement_db.exists(achievement_id) == false) {
  20865. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  20866. script_pushint(st, false);
  20867. return SCRIPT_CMD_FAILURE;
  20868. }
  20869. if( !sd->state.pc_loaded ){
  20870. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20871. return SCRIPT_CMD_SUCCESS;
  20872. }
  20873. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20874. if (i == sd->achievement_data.count)
  20875. achievement_add(sd, achievement_id);
  20876. achievement_update_achievement(sd, achievement_id, true);
  20877. script_pushint(st, true);
  20878. return SCRIPT_CMD_SUCCESS;
  20879. }
  20880. /**
  20881. * Checks if the achievement exists on player.
  20882. * achievementexists(<achievement ID>{,<char ID>});
  20883. */
  20884. BUILDIN_FUNC(achievementexists) {
  20885. struct map_session_data *sd;
  20886. int i, achievement_id = script_getnum(st, 2);
  20887. if (!script_charid2sd(3, sd)) {
  20888. script_pushint(st, false);
  20889. return SCRIPT_CMD_FAILURE;
  20890. }
  20891. if (achievement_db.exists(achievement_id) == false) {
  20892. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  20893. script_pushint(st, false);
  20894. return SCRIPT_CMD_SUCCESS;
  20895. }
  20896. if( !sd->state.pc_loaded ){
  20897. script_pushint(st, false);
  20898. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20899. return SCRIPT_CMD_SUCCESS;
  20900. }
  20901. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id && sd->achievement_data.achievements[i].completed > 0 );
  20902. script_pushint(st, i < sd->achievement_data.count ? true : false);
  20903. return SCRIPT_CMD_SUCCESS;
  20904. }
  20905. /**
  20906. * Updates an achievement's value.
  20907. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  20908. */
  20909. BUILDIN_FUNC(achievementupdate) {
  20910. struct map_session_data *sd;
  20911. int i, achievement_id, type, value;
  20912. achievement_id = script_getnum(st, 2);
  20913. type = script_getnum(st, 3);
  20914. value = script_getnum(st, 4);
  20915. if (!script_charid2sd(5, sd)) {
  20916. script_pushint(st, false);
  20917. return SCRIPT_CMD_FAILURE;
  20918. }
  20919. if (achievement_db.exists(achievement_id) == false) {
  20920. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  20921. script_pushint(st, false);
  20922. return SCRIPT_CMD_FAILURE;
  20923. }
  20924. if( !sd->state.pc_loaded ){
  20925. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20926. return SCRIPT_CMD_SUCCESS;
  20927. }
  20928. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20929. if (i == sd->achievement_data.count)
  20930. achievement_add(sd, achievement_id);
  20931. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20932. if (i == sd->achievement_data.count) {
  20933. script_pushint(st, false);
  20934. return SCRIPT_CMD_SUCCESS;
  20935. }
  20936. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  20937. sd->achievement_data.achievements[i].count[type - 1] = value;
  20938. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  20939. sd->achievement_data.achievements[i].completed = value;
  20940. else if (type == ACHIEVEINFO_GOTREWARD)
  20941. sd->achievement_data.achievements[i].rewarded = value;
  20942. else {
  20943. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  20944. script_pushint(st, false);
  20945. return SCRIPT_CMD_FAILURE;
  20946. }
  20947. achievement_update_achievement(sd, achievement_id, false);
  20948. script_pushint(st, true);
  20949. return SCRIPT_CMD_SUCCESS;
  20950. }
  20951. /**
  20952. * Get an equipment's refine cost
  20953. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  20954. * returns -1 on fail
  20955. */
  20956. BUILDIN_FUNC(getequiprefinecost) {
  20957. int i = -1, slot, type, info;
  20958. map_session_data *sd;
  20959. slot = script_getnum(st, 2);
  20960. type = script_getnum(st, 3);
  20961. info = script_getnum(st, 4);
  20962. if (!script_charid2sd(5, sd)) {
  20963. script_pushint(st, 0);
  20964. return SCRIPT_CMD_FAILURE;
  20965. }
  20966. if (type < 0 || type >= REFINE_COST_MAX) {
  20967. script_pushint(st, -1);
  20968. return SCRIPT_CMD_SUCCESS;
  20969. }
  20970. if (equip_index_check(slot))
  20971. i = pc_checkequip(sd, equip_bitmask[slot]);
  20972. if (i < 0) {
  20973. script_pushint(st, -1);
  20974. return SCRIPT_CMD_SUCCESS;
  20975. }
  20976. int weapon_lv = sd->inventory_data[i]->wlv;
  20977. if (sd->inventory_data[i]->type == IT_SHADOWGEAR) {
  20978. if (sd->inventory_data[i]->equip == EQP_SHADOW_WEAPON)
  20979. weapon_lv = REFINE_TYPE_WEAPON4;
  20980. else
  20981. weapon_lv = REFINE_TYPE_SHADOW;
  20982. }
  20983. script_pushint(st, status_get_refine_cost(weapon_lv, type, info != 0));
  20984. return SCRIPT_CMD_SUCCESS;
  20985. }
  20986. /**
  20987. * Round, floor, ceiling a number to arbitrary integer precision.
  20988. * round(<number>,<precision>);
  20989. * ceil(<number>,<precision>);
  20990. * floor(<number>,<precision>);
  20991. */
  20992. BUILDIN_FUNC(round) {
  20993. int num = script_getnum(st, 2);
  20994. int precision = script_getnum(st, 3);
  20995. char* func = script_getfuncname(st);
  20996. if (precision <= 0) {
  20997. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  20998. return SCRIPT_CMD_FAILURE;
  20999. }
  21000. if (strcasecmp(func, "floor") == 0) {
  21001. script_pushint(st, num - (num % precision));
  21002. }
  21003. else if (strcasecmp(func, "ceil") == 0) {
  21004. script_pushint(st, num + precision - (num % precision));
  21005. }
  21006. else {
  21007. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  21008. }
  21009. return SCRIPT_CMD_SUCCESS;
  21010. }
  21011. BUILDIN_FUNC(getequiptradability) {
  21012. int i, num;
  21013. TBL_PC *sd;
  21014. num = script_getnum(st, 2);
  21015. if (!script_charid2sd(3, sd)) {
  21016. return SCRIPT_CMD_FAILURE;
  21017. }
  21018. if (equip_index_check(num))
  21019. i = pc_checkequip(sd, equip_bitmask[num]);
  21020. else{
  21021. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  21022. return SCRIPT_CMD_FAILURE;
  21023. }
  21024. if (i >= 0)
  21025. script_pushint(st, pc_can_trade_item(sd, i));
  21026. else
  21027. script_pushint(st, false);
  21028. return SCRIPT_CMD_SUCCESS;
  21029. }
  21030. 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 ){
  21031. const char *name;
  21032. // Check if it is a variable
  21033. if( !data_isreference(data) ){
  21034. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  21035. return false;
  21036. }
  21037. name = reference_getname(data);
  21038. if( is_string_variable(name) ){
  21039. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  21040. return false;
  21041. }
  21042. // Check if the variable requires a player
  21043. if( not_server_variable(*name) && sd == NULL ){
  21044. // If no player is attached
  21045. if( !script_rid2sd(sd) ){
  21046. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  21047. return false;
  21048. }
  21049. }
  21050. // Try to find the array's source pointer
  21051. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  21052. ShowError( "buildin_mail: variable \"%s\" is not an array.\n", name );
  21053. return false;
  21054. }
  21055. // Store the name for later usage
  21056. *out_name = name;
  21057. // Get the start and end indices of the array
  21058. *start = reference_getindex(data);
  21059. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  21060. // For getting the values we need the id of the array
  21061. *id = reference_getid(data);
  21062. return true;
  21063. }
  21064. // 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>}}}}}}}}};
  21065. BUILDIN_FUNC(mail){
  21066. const char *sender, *title, *body, *name;
  21067. struct mail_message msg;
  21068. struct script_data *data;
  21069. struct map_session_data *sd = NULL;
  21070. unsigned int i, j, k, num_items, start, end;
  21071. int32 id;
  21072. memset(&msg, 0, sizeof(struct mail_message));
  21073. msg.dest_id = script_getnum(st,2);
  21074. sender = script_getstr(st, 3);
  21075. if( strlen(sender) > NAME_LENGTH ){
  21076. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  21077. return SCRIPT_CMD_FAILURE;
  21078. }
  21079. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  21080. title = script_getstr(st, 4);
  21081. if( strlen(title) > MAIL_TITLE_LENGTH ){
  21082. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  21083. return SCRIPT_CMD_FAILURE;
  21084. }
  21085. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  21086. body = script_getstr(st, 5);
  21087. if( strlen(body) > MAIL_BODY_LENGTH ){
  21088. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  21089. return SCRIPT_CMD_FAILURE;
  21090. }
  21091. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  21092. if( script_hasdata(st,6) ){
  21093. int zeny = script_getnum(st, 6);
  21094. if( zeny < 0 ){
  21095. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  21096. return SCRIPT_CMD_FAILURE;
  21097. }else if( zeny > MAX_ZENY ){
  21098. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  21099. zeny = MAX_ZENY;
  21100. }
  21101. msg.zeny = zeny;
  21102. }
  21103. // Items
  21104. num_items = 0;
  21105. while( script_hasdata(st,7) ){
  21106. data = script_getdata(st,7);
  21107. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  21108. return SCRIPT_CMD_FAILURE;
  21109. }
  21110. num_items = end - start;
  21111. if( num_items == 0 ){
  21112. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  21113. break;
  21114. }
  21115. if( num_items > MAIL_MAX_ITEM ){
  21116. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  21117. num_items = MAIL_MAX_ITEM;
  21118. }
  21119. for( i = 0; i < num_items && start < end; i++, start++ ){
  21120. msg.item[i].nameid = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21121. msg.item[i].identify = 1;
  21122. if( !itemdb_exists(msg.item[i].nameid) ){
  21123. ShowError( "buildin_mail: invalid item id %u.\n", msg.item[i].nameid );
  21124. return SCRIPT_CMD_FAILURE;
  21125. }
  21126. }
  21127. // Amounts
  21128. if( !script_hasdata(st,8) ){
  21129. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  21130. return SCRIPT_CMD_FAILURE;
  21131. }
  21132. data = script_getdata(st,8);
  21133. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  21134. return SCRIPT_CMD_FAILURE;
  21135. }
  21136. for( i = 0; i < num_items && start < end; i++, start++ ){
  21137. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21138. msg.item[i].amount = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21139. if( msg.item[i].amount <= 0 ){
  21140. ShowError( "buildin_mail: amount %d for item %u is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  21141. return SCRIPT_CMD_FAILURE;
  21142. }else if( itemdb_isstackable2(item) ){
  21143. uint16 max = item->stack.amount > 0 ? item->stack.amount : MAX_AMOUNT;
  21144. if( msg.item[i].amount > max ){
  21145. 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 );
  21146. msg.item[i].amount = max;
  21147. }
  21148. }else{
  21149. if( msg.item[i].amount > 1 ){
  21150. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %u.\n", msg.item[i].amount, msg.item[i].nameid );
  21151. msg.item[i].amount = 1;
  21152. }
  21153. }
  21154. }
  21155. // Refine
  21156. if (!script_hasdata(st, 9)) {
  21157. break;
  21158. }
  21159. data = script_getdata(st, 9);
  21160. if (!mail_sub(st, data, sd, 9, &name, &start, &end, &id)) {
  21161. return SCRIPT_CMD_FAILURE;
  21162. }
  21163. for (i = 0; i < num_items && start < end; i++, start++) {
  21164. struct item_data* item = itemdb_exists(msg.item[i].nameid);
  21165. msg.item[i].refine = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21166. script_removetop(st, -1, 0);
  21167. if (!item->flag.no_refine && (item->type == IT_WEAPON || item->type == IT_ARMOR || item->type == IT_SHADOWGEAR)) {
  21168. if (msg.item[i].refine > MAX_REFINE)
  21169. msg.item[i].refine = MAX_REFINE;
  21170. }
  21171. else
  21172. msg.item[i].refine = 0;
  21173. if (msg.item[i].refine < 0)
  21174. msg.item[i].refine = 0;
  21175. }
  21176. // Bound
  21177. if (!script_hasdata(st, 10)) {
  21178. break;
  21179. }
  21180. data = script_getdata(st,10);
  21181. if( !mail_sub( st, data, sd, 10, &name, &start, &end, &id ) ){
  21182. return SCRIPT_CMD_FAILURE;
  21183. }
  21184. for( i = 0; i < num_items && start < end; i++, start++ ){
  21185. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  21186. msg.item[i].bound = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21187. script_removetop(st, -1, 0);
  21188. if( msg.item[i].bound <= BOUND_NONE || msg.item[i].bound >= BOUND_MAX ){
  21189. ShowError( "buildin_mail: bound %d for item %u is invalid.\n", msg.item[i].bound, msg.item[i].nameid );
  21190. return SCRIPT_CMD_FAILURE;
  21191. }
  21192. }
  21193. // Cards
  21194. if( !script_hasdata(st,11) ){
  21195. break;
  21196. }
  21197. for( i = 0, j = 11; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  21198. data = script_getdata(st,j);
  21199. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21200. return SCRIPT_CMD_FAILURE;
  21201. }
  21202. for( k = 0; k < num_items && start < end; k++, start++ ){
  21203. msg.item[k].card[i] = (t_itemid)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21204. if( msg.item[k].card[i] != 0 && !itemdb_exists(msg.item[k].card[i]) ){
  21205. ShowError( "buildin_mail: invalid card id %u.\n", msg.item[k].card[i] );
  21206. return SCRIPT_CMD_FAILURE;
  21207. }
  21208. }
  21209. }
  21210. // Random Options
  21211. if( !script_hasdata(st,12 + MAX_SLOTS) ){
  21212. break;
  21213. }
  21214. 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++ ){
  21215. // Option IDs
  21216. data = script_getdata(st, j);
  21217. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21218. return SCRIPT_CMD_FAILURE;
  21219. }
  21220. for( k = 0; k < num_items && start < end; k++, start++ ){
  21221. msg.item[k].option[i].id = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21222. }
  21223. j++;
  21224. // Option values
  21225. data = script_getdata(st, j);
  21226. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21227. return SCRIPT_CMD_FAILURE;
  21228. }
  21229. for( k = 0; k < num_items && start < end; k++, start++ ){
  21230. msg.item[k].option[i].value = (short)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21231. }
  21232. j++;
  21233. // Option parameters
  21234. data = script_getdata(st, j);
  21235. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  21236. return SCRIPT_CMD_FAILURE;
  21237. }
  21238. for( k = 0; k < num_items && start < end; k++, start++ ){
  21239. msg.item[k].option[i].param = (char)get_val2_num( st, reference_uid( id, start ), reference_getref( data ) );
  21240. }
  21241. }
  21242. // Break the pseudo scope
  21243. break;
  21244. }
  21245. msg.status = MAIL_NEW;
  21246. msg.type = MAIL_INBOX_NORMAL;
  21247. msg.timestamp = time(NULL);
  21248. intif_Mail_send(0, &msg);
  21249. return SCRIPT_CMD_SUCCESS;
  21250. }
  21251. BUILDIN_FUNC(open_roulette){
  21252. #if PACKETVER < 20141022
  21253. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  21254. return SCRIPT_CMD_FAILURE;
  21255. #else
  21256. struct map_session_data* sd;
  21257. if( !battle_config.feature_roulette ){
  21258. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  21259. return SCRIPT_CMD_FAILURE;
  21260. }
  21261. if( !script_charid2sd( 2, sd ) ){
  21262. return SCRIPT_CMD_FAILURE;
  21263. }
  21264. clif_roulette_open(sd);
  21265. return SCRIPT_CMD_SUCCESS;
  21266. #endif
  21267. }
  21268. /*==========================================
  21269. * identifyall({<type>{,<account_id>}})
  21270. * <type>:
  21271. * true: identify the items and returns the count of unidentified items (default)
  21272. * false: returns the count of unidentified items only
  21273. *------------------------------------------*/
  21274. BUILDIN_FUNC(identifyall) {
  21275. TBL_PC *sd;
  21276. bool identify_item = true;
  21277. if (script_hasdata(st, 2))
  21278. identify_item = script_getnum(st, 2) != 0;
  21279. if( !script_hasdata(st, 3) )
  21280. script_rid2sd(sd);
  21281. else
  21282. sd = map_id2sd( script_getnum(st, 3) );
  21283. if (sd == NULL) {
  21284. script_pushint(st, -1);
  21285. return SCRIPT_CMD_SUCCESS;
  21286. }
  21287. script_pushint(st, pc_identifyall(sd, identify_item));
  21288. return SCRIPT_CMD_SUCCESS;
  21289. }
  21290. BUILDIN_FUNC(is_guild_leader)
  21291. {
  21292. struct map_session_data* sd;
  21293. struct guild* guild_data;
  21294. int guild_id;
  21295. if (!script_rid2sd(sd)) {
  21296. script_pushint(st, false);
  21297. return SCRIPT_CMD_FAILURE;
  21298. }
  21299. if (script_hasdata(st, 2))
  21300. guild_id = script_getnum(st, 2);
  21301. else
  21302. guild_id = sd->status.guild_id;
  21303. guild_data = guild_search(guild_id);
  21304. if (guild_data)
  21305. script_pushint(st, (guild_data->member[0].char_id == sd->status.char_id));
  21306. else
  21307. script_pushint(st, false);
  21308. return SCRIPT_CMD_SUCCESS;
  21309. }
  21310. BUILDIN_FUNC(is_party_leader)
  21311. {
  21312. struct map_session_data* sd;
  21313. struct party_data* p_data;
  21314. int p_id, i = 0;
  21315. if (!script_rid2sd(sd)) {
  21316. script_pushint(st, false);
  21317. return SCRIPT_CMD_FAILURE;
  21318. }
  21319. if (script_hasdata(st, 2))
  21320. p_id = script_getnum(st, 2);
  21321. else
  21322. p_id = sd->status.party_id;
  21323. p_data = party_search(p_id);
  21324. if (p_data) {
  21325. ARR_FIND( 0, MAX_PARTY, i, p_data->data[i].sd == sd );
  21326. if (i < MAX_PARTY){
  21327. script_pushint(st, p_data->party.member[i].leader);
  21328. return SCRIPT_CMD_SUCCESS;
  21329. }
  21330. }
  21331. script_pushint(st, false);
  21332. return SCRIPT_CMD_SUCCESS;
  21333. }
  21334. BUILDIN_FUNC( camerainfo ){
  21335. #if PACKETVER < 20160525
  21336. ShowError("buildin_camerainfo: This command requires PACKETVER 2016-05-25 or newer.\n");
  21337. return SCRIPT_CMD_FAILURE;
  21338. #else
  21339. struct map_session_data* sd;
  21340. if( !script_charid2sd( 5, sd ) ){
  21341. return SCRIPT_CMD_FAILURE;
  21342. }
  21343. clif_camerainfo( sd, false, script_getnum( st, 2 ) / 100.0f, script_getnum( st, 3 ) / 100.0f, script_getnum( st, 4 ) / 100.0f );
  21344. return SCRIPT_CMD_SUCCESS;
  21345. #endif
  21346. }
  21347. // This function is only meant to be used inside of achievement conditions
  21348. BUILDIN_FUNC(achievement_condition){
  21349. // Push what we get from the script
  21350. script_pushint( st, 2 );
  21351. // Otherwise the script is freed afterwards
  21352. st->state = RERUNLINE;
  21353. return SCRIPT_CMD_SUCCESS;
  21354. }
  21355. /// Returns a reference to a variable of the specific instance ID.
  21356. /// Returns 0 if an error occurs.
  21357. ///
  21358. /// getvariableofinstance(<variable>, <instance ID>) -> <reference>
  21359. BUILDIN_FUNC(getvariableofinstance)
  21360. {
  21361. struct script_data* data = script_getdata(st, 2);
  21362. if (!data_isreference(data)) {
  21363. ShowError("buildin_getvariableofinstance: %s is not a variable.\n", script_getstr(st, 2));
  21364. script_reportdata(data);
  21365. script_pushnil(st);
  21366. st->state = END;
  21367. return SCRIPT_CMD_FAILURE;
  21368. }
  21369. const char* name = reference_getname(data);
  21370. if (*name != '\'') {
  21371. ShowError("buildin_getvariableofinstance: Invalid scope. %s is not an instance variable.\n", name);
  21372. script_reportdata(data);
  21373. script_pushnil(st);
  21374. st->state = END;
  21375. return SCRIPT_CMD_FAILURE;
  21376. }
  21377. int instance_id = script_getnum(st, 3);
  21378. if (instance_id <= 0) {
  21379. ShowError("buildin_getvariableofinstance: Invalid instance ID %d.\n", instance_id);
  21380. script_pushnil(st);
  21381. st->state = END;
  21382. return SCRIPT_CMD_FAILURE;
  21383. }
  21384. std::shared_ptr<s_instance_data> im = util::umap_find(instances, instance_id);
  21385. if (im->state != INSTANCE_BUSY) {
  21386. ShowError("buildin_getvariableofinstance: Unknown instance ID %d.\n", instance_id);
  21387. script_pushnil(st);
  21388. st->state = END;
  21389. return SCRIPT_CMD_FAILURE;
  21390. }
  21391. if (!im->regs.vars)
  21392. im->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  21393. push_val2(st->stack, C_NAME, reference_getuid(data), &im->regs);
  21394. return SCRIPT_CMD_SUCCESS;
  21395. }
  21396. /*
  21397. convertpcinfo(<char_id>,<type>)
  21398. convertpcinfo(<account_id>,<type>)
  21399. convertpcinfo(<player_name>,<type>)
  21400. */
  21401. BUILDIN_FUNC(convertpcinfo) {
  21402. TBL_PC *sd;
  21403. if (script_isstring(st, 2))
  21404. sd = map_nick2sd(script_getstr(st, 2),false);
  21405. else {
  21406. int id = script_getnum(st, 2);
  21407. sd = map_id2sd(id);
  21408. if (!sd)
  21409. sd = map_charid2sd(id);
  21410. }
  21411. int type = script_getnum(st, 3);
  21412. switch (type) {
  21413. case CPC_NAME:
  21414. case CPC_CHAR:
  21415. case CPC_ACCOUNT:
  21416. break;
  21417. default:
  21418. ShowError("buildin_convertpcinfo: Unknown type %d.\n", type);
  21419. script_pushnil(st);
  21420. st->state = END;
  21421. return SCRIPT_CMD_FAILURE;
  21422. }
  21423. if (!sd) {
  21424. if (type == CPC_NAME)
  21425. script_pushstrcopy(st, "");
  21426. else
  21427. script_pushint(st, 0);
  21428. return SCRIPT_CMD_SUCCESS;
  21429. }
  21430. switch (type) {
  21431. case CPC_NAME:
  21432. script_pushstrcopy(st, sd->status.name);
  21433. break;
  21434. case CPC_CHAR:
  21435. script_pushint(st, sd->status.char_id);
  21436. break;
  21437. case CPC_ACCOUNT:
  21438. script_pushint(st, sd->status.account_id);
  21439. break;
  21440. }
  21441. return SCRIPT_CMD_SUCCESS;
  21442. }
  21443. BUILDIN_FUNC(cloakoffnpc)
  21444. {
  21445. if (npc_enable_target(script_getstr(st, 2), script_hasdata(st, 3) ? script_getnum(st, 3) : 0, 8))
  21446. return SCRIPT_CMD_SUCCESS;
  21447. return SCRIPT_CMD_FAILURE;
  21448. }
  21449. BUILDIN_FUNC(cloakonnpc)
  21450. {
  21451. if (npc_enable_target(script_getstr(st, 2), script_hasdata(st, 3) ? script_getnum(st, 3) : 0, 16))
  21452. return SCRIPT_CMD_SUCCESS;
  21453. return SCRIPT_CMD_FAILURE;
  21454. }
  21455. BUILDIN_FUNC(isnpccloaked)
  21456. {
  21457. struct npc_data *nd = npc_name2id(script_getstr(st, 2));
  21458. if (!nd) {
  21459. ShowError("buildin_isnpccloaked: %s is a non-existing NPC.\n", script_getstr(st, 2));
  21460. return SCRIPT_CMD_FAILURE;
  21461. }
  21462. map_session_data* sd;
  21463. if (!script_charid2sd(3, sd))
  21464. return SCRIPT_CMD_FAILURE;
  21465. script_pushint(st, npc_is_cloaked(nd, sd));
  21466. return SCRIPT_CMD_SUCCESS;
  21467. }
  21468. #include "../custom/script.inc"
  21469. // declarations that were supposed to be exported from npc_chat.cpp
  21470. #ifdef PCRE_SUPPORT
  21471. BUILDIN_FUNC(defpattern);
  21472. BUILDIN_FUNC(activatepset);
  21473. BUILDIN_FUNC(deactivatepset);
  21474. BUILDIN_FUNC(deletepset);
  21475. #endif
  21476. /** Regular expression matching
  21477. * preg_match(<pattern>,<string>{,<offset>})
  21478. */
  21479. BUILDIN_FUNC(preg_match) {
  21480. #ifdef PCRE_SUPPORT
  21481. pcre *re;
  21482. pcre_extra *pcreExtra;
  21483. const char *error;
  21484. int erroffset, r, offset = 0;
  21485. int subStrVec[30];
  21486. const char* pattern = script_getstr(st,2);
  21487. const char* subject = script_getstr(st,3);
  21488. if (script_hasdata(st,4))
  21489. offset = script_getnum(st,4);
  21490. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  21491. pcreExtra = pcre_study(re, 0, &error);
  21492. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  21493. pcre_free(re);
  21494. if (pcreExtra != NULL)
  21495. pcre_free(pcreExtra);
  21496. if (r < 0)
  21497. script_pushint(st,0);
  21498. else
  21499. script_pushint(st,(r > 0) ? r : 30 / 3);
  21500. return SCRIPT_CMD_SUCCESS;
  21501. #else
  21502. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  21503. script_pushint(st,0);
  21504. return SCRIPT_CMD_SUCCESS;
  21505. #endif
  21506. }
  21507. /// script command definitions
  21508. /// for an explanation on args, see add_buildin_func
  21509. struct script_function buildin_func[] = {
  21510. // NPC interaction
  21511. BUILDIN_DEF(mes,"s*"),
  21512. BUILDIN_DEF(next,""),
  21513. BUILDIN_DEF(clear,""),
  21514. BUILDIN_DEF(close,""),
  21515. BUILDIN_DEF(close2,""),
  21516. BUILDIN_DEF(menu,"sl*"),
  21517. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  21518. BUILDIN_DEF(prompt,"s*"),
  21519. //
  21520. BUILDIN_DEF(goto,"l"),
  21521. BUILDIN_DEF(callsub,"l*"),
  21522. BUILDIN_DEF(callfunc,"s*"),
  21523. BUILDIN_DEF(return,"?"),
  21524. BUILDIN_DEF(getarg,"i?"),
  21525. BUILDIN_DEF(jobchange,"i??"),
  21526. BUILDIN_DEF(jobname,"i"),
  21527. BUILDIN_DEF(input,"r??"),
  21528. BUILDIN_DEF(warp,"sii?"),
  21529. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  21530. BUILDIN_DEF(areawarp,"siiiisii??"),
  21531. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  21532. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  21533. BUILDIN_DEF(setlook,"ii?"),
  21534. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  21535. BUILDIN_DEF2(setr,"set","rv?"),
  21536. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  21537. BUILDIN_DEF(setarray,"rv*"),
  21538. BUILDIN_DEF(cleararray,"rvi"),
  21539. BUILDIN_DEF(copyarray,"rri"),
  21540. BUILDIN_DEF(getarraysize,"r"),
  21541. BUILDIN_DEF(deletearray,"r?"),
  21542. BUILDIN_DEF(getelementofarray,"ri"),
  21543. BUILDIN_DEF(inarray,"rv"),
  21544. BUILDIN_DEF(countinarray,"rr"),
  21545. BUILDIN_DEF(getitem,"vi?"),
  21546. BUILDIN_DEF(rentitem,"vi?"),
  21547. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  21548. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  21549. BUILDIN_DEF(getnameditem,"vv"),
  21550. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  21551. BUILDIN_DEF(makeitem,"visii"),
  21552. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  21553. BUILDIN_DEF(delitem,"vi?"),
  21554. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  21555. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  21556. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  21557. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  21558. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  21559. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  21560. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  21561. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  21562. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  21563. BUILDIN_DEF(cutin,"si"),
  21564. BUILDIN_DEF(viewpoint,"iiiii?"),
  21565. BUILDIN_DEF(heal,"ii?"),
  21566. BUILDIN_DEF(itemheal,"ii?"),
  21567. BUILDIN_DEF(percentheal,"ii?"),
  21568. BUILDIN_DEF(rand,"i?"),
  21569. BUILDIN_DEF(countitem,"v?"),
  21570. BUILDIN_DEF(storagecountitem,"v?"),
  21571. BUILDIN_DEF(guildstoragecountitem,"v?"),
  21572. BUILDIN_DEF(cartcountitem,"v?"),
  21573. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  21574. BUILDIN_DEF2(storagecountitem,"storagecountitem2","viiiiiii?"),
  21575. BUILDIN_DEF2(guildstoragecountitem,"guildstoragecountitem2","viiiiiii?"),
  21576. BUILDIN_DEF2(cartcountitem,"cartcountitem2","viiiiiii?"),
  21577. BUILDIN_DEF(checkweight,"vi*"),
  21578. BUILDIN_DEF(checkweight2,"rr"),
  21579. BUILDIN_DEF(readparam,"i?"),
  21580. BUILDIN_DEF(getcharid,"i?"),
  21581. BUILDIN_DEF(getnpcid,"i?"),
  21582. BUILDIN_DEF(getpartyname,"i"),
  21583. BUILDIN_DEF(getpartymember,"i??"),
  21584. BUILDIN_DEF(getpartyleader,"i?"),
  21585. BUILDIN_DEF(getguildname,"i"),
  21586. BUILDIN_DEF(getguildmaster,"i"),
  21587. BUILDIN_DEF(getguildmasterid,"i"),
  21588. BUILDIN_DEF(strcharinfo,"i?"),
  21589. BUILDIN_DEF(strnpcinfo,"i"),
  21590. BUILDIN_DEF(getequipid,"??"),
  21591. BUILDIN_DEF(getequipuniqueid,"i?"),
  21592. BUILDIN_DEF(getequipname,"i?"),
  21593. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  21594. BUILDIN_DEF(repair,"i?"), // [Valaris]
  21595. BUILDIN_DEF(repairall,"?"),
  21596. BUILDIN_DEF(getequipisequiped,"i?"),
  21597. BUILDIN_DEF(getequipisenableref,"i?"),
  21598. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  21599. BUILDIN_DEF(getequipweaponlv,"i?"),
  21600. BUILDIN_DEF(getequippercentrefinery,"i?"),
  21601. BUILDIN_DEF(successrefitem,"i??"),
  21602. BUILDIN_DEF(failedrefitem,"i?"),
  21603. BUILDIN_DEF(downrefitem,"i??"),
  21604. BUILDIN_DEF(statusup,"i?"),
  21605. BUILDIN_DEF(statusup2,"ii?"),
  21606. BUILDIN_DEF(bonus,"i?"),
  21607. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  21608. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  21609. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  21610. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  21611. BUILDIN_DEF(autobonus,"sii??"),
  21612. BUILDIN_DEF(autobonus2,"sii??"),
  21613. BUILDIN_DEF(autobonus3,"siiv?"),
  21614. BUILDIN_DEF(skill,"vi?"),
  21615. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  21616. BUILDIN_DEF(guildskill,"vi"),
  21617. BUILDIN_DEF(getskilllv,"v"),
  21618. BUILDIN_DEF(getgdskilllv,"iv"),
  21619. BUILDIN_DEF(basicskillcheck,""),
  21620. BUILDIN_DEF(getgmlevel,"?"),
  21621. BUILDIN_DEF(getgroupid,"?"),
  21622. BUILDIN_DEF(end,""),
  21623. BUILDIN_DEF(checkoption,"i?"),
  21624. BUILDIN_DEF(setoption,"i??"),
  21625. BUILDIN_DEF(setcart,"??"),
  21626. BUILDIN_DEF(checkcart,"?"),
  21627. BUILDIN_DEF(setfalcon,"??"),
  21628. BUILDIN_DEF(checkfalcon,"?"),
  21629. BUILDIN_DEF(setriding,"??"),
  21630. BUILDIN_DEF(checkriding,"?"),
  21631. BUILDIN_DEF(checkwug,"?"),
  21632. BUILDIN_DEF(checkmadogear,"?"),
  21633. BUILDIN_DEF(setmadogear,"???"),
  21634. BUILDIN_DEF2(savepoint,"save","sii???"),
  21635. BUILDIN_DEF(savepoint,"sii???"),
  21636. BUILDIN_DEF(gettimetick,"i"),
  21637. BUILDIN_DEF(gettime,"i"),
  21638. BUILDIN_DEF(gettimestr,"si?"),
  21639. BUILDIN_DEF(openstorage,""),
  21640. BUILDIN_DEF(guildopenstorage,""),
  21641. BUILDIN_DEF(guildopenstorage_log,"?"),
  21642. BUILDIN_DEF(guild_has_permission,"i?"),
  21643. BUILDIN_DEF(itemskill,"vi?"),
  21644. BUILDIN_DEF(produce,"i"),
  21645. BUILDIN_DEF(cooking,"i"),
  21646. BUILDIN_DEF(monster,"siisii???"),
  21647. BUILDIN_DEF(getmobdrops,"i"),
  21648. BUILDIN_DEF(areamonster,"siiiisii???"),
  21649. BUILDIN_DEF(killmonster,"ss?"),
  21650. BUILDIN_DEF(killmonsterall,"s?"),
  21651. BUILDIN_DEF(clone,"siisi????"),
  21652. BUILDIN_DEF(doevent,"s"),
  21653. BUILDIN_DEF(donpcevent,"s"),
  21654. BUILDIN_DEF(cmdothernpc,"ss"),
  21655. BUILDIN_DEF(addtimer,"is"),
  21656. BUILDIN_DEF(deltimer,"s"),
  21657. BUILDIN_DEF(addtimercount,"is"),
  21658. BUILDIN_DEF(initnpctimer,"??"),
  21659. BUILDIN_DEF(stopnpctimer,"??"),
  21660. BUILDIN_DEF(startnpctimer,"??"),
  21661. BUILDIN_DEF(setnpctimer,"i?"),
  21662. BUILDIN_DEF(getnpctimer,"i?"),
  21663. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  21664. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  21665. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  21666. BUILDIN_DEF(announce,"si??????"),
  21667. BUILDIN_DEF(mapannounce,"ssi?????"),
  21668. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  21669. BUILDIN_DEF(getusers,"i"),
  21670. BUILDIN_DEF(getmapguildusers,"si"),
  21671. BUILDIN_DEF(getmapusers,"s"),
  21672. BUILDIN_DEF(getareausers,"siiii"),
  21673. BUILDIN_DEF(getunits, "i?"),
  21674. BUILDIN_DEF2(getunits, "getmapunits", "is?"),
  21675. BUILDIN_DEF2(getunits, "getareaunits", "isiiii?"),
  21676. BUILDIN_DEF(getareadropitem,"siiiiv"),
  21677. BUILDIN_DEF(enablenpc,"s"),
  21678. BUILDIN_DEF(disablenpc,"s"),
  21679. BUILDIN_DEF(hideoffnpc,"s"),
  21680. BUILDIN_DEF(hideonnpc,"s"),
  21681. BUILDIN_DEF(sc_start,"iii???"),
  21682. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  21683. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  21684. BUILDIN_DEF(sc_end,"i?"),
  21685. BUILDIN_DEF(sc_end_class,"??"),
  21686. BUILDIN_DEF(getstatus, "i??"),
  21687. BUILDIN_DEF(getscrate,"ii?"),
  21688. BUILDIN_DEF(debugmes,"s"),
  21689. BUILDIN_DEF(errormes,"s"),
  21690. BUILDIN_DEF2(catchpet,"pet","i"),
  21691. BUILDIN_DEF2(birthpet,"bpet",""),
  21692. BUILDIN_DEF(catchpet,"i"),
  21693. BUILDIN_DEF(birthpet,""),
  21694. BUILDIN_DEF(resetlvl,"i?"),
  21695. BUILDIN_DEF(resetstatus,"?"),
  21696. BUILDIN_DEF(resetskill,"?"),
  21697. BUILDIN_DEF(resetfeel,"?"),
  21698. BUILDIN_DEF(resethate,"?"),
  21699. BUILDIN_DEF(skillpointcount,"?"),
  21700. BUILDIN_DEF(changebase,"i?"),
  21701. BUILDIN_DEF(changesex,"?"),
  21702. BUILDIN_DEF(changecharsex,"?"),
  21703. BUILDIN_DEF(waitingroom,"si?????"),
  21704. BUILDIN_DEF(delwaitingroom,"?"),
  21705. BUILDIN_DEF(waitingroomkick,"ss"),
  21706. BUILDIN_DEF(getwaitingroomusers, "?"),
  21707. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  21708. BUILDIN_DEF(enablewaitingroomevent,"?"),
  21709. BUILDIN_DEF(disablewaitingroomevent,"?"),
  21710. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  21711. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  21712. BUILDIN_DEF(getwaitingroomstate,"i?"),
  21713. BUILDIN_DEF(warpwaitingpc,"sii?"),
  21714. BUILDIN_DEF(attachrid,"i?"),
  21715. BUILDIN_DEF(addrid,"i?????"),
  21716. BUILDIN_DEF(detachrid,""),
  21717. BUILDIN_DEF(isloggedin,"i?"),
  21718. BUILDIN_DEF(setmapflagnosave,"ssii"),
  21719. BUILDIN_DEF(getmapflag,"si?"),
  21720. BUILDIN_DEF(setmapflag,"si??"),
  21721. BUILDIN_DEF(removemapflag,"si?"),
  21722. BUILDIN_DEF(pvpon,"s"),
  21723. BUILDIN_DEF(pvpoff,"s"),
  21724. BUILDIN_DEF(gvgon,"s"),
  21725. BUILDIN_DEF(gvgoff,"s"),
  21726. BUILDIN_DEF(emotion,"i?"),
  21727. BUILDIN_DEF(maprespawnguildid,"sii"),
  21728. BUILDIN_DEF(agitstart,""), // <Agit>
  21729. BUILDIN_DEF(agitend,""),
  21730. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  21731. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  21732. BUILDIN_DEF(getcastlename,"s"),
  21733. BUILDIN_DEF(getcastledata,"si"),
  21734. BUILDIN_DEF(setcastledata,"sii"),
  21735. BUILDIN_DEF(requestguildinfo,"i?"),
  21736. BUILDIN_DEF(getequipcardcnt,"i"),
  21737. BUILDIN_DEF(successremovecards,"i"),
  21738. BUILDIN_DEF(failedremovecards,"ii"),
  21739. BUILDIN_DEF(marriage,"s"),
  21740. BUILDIN_DEF2(wedding_effect,"wedding",""),
  21741. BUILDIN_DEF(divorce,"?"),
  21742. BUILDIN_DEF(ispartneron,"?"),
  21743. BUILDIN_DEF(getpartnerid,"?"),
  21744. BUILDIN_DEF(getchildid,"?"),
  21745. BUILDIN_DEF(getmotherid,"?"),
  21746. BUILDIN_DEF(getfatherid,"?"),
  21747. BUILDIN_DEF(warppartner,"sii"),
  21748. BUILDIN_DEF(getitemname,"v"),
  21749. BUILDIN_DEF(getitemslots,"i"),
  21750. BUILDIN_DEF(makepet,"i"),
  21751. BUILDIN_DEF(getexp,"ii?"),
  21752. BUILDIN_DEF(getinventorylist,"?"),
  21753. BUILDIN_DEF(getskilllist,"?"),
  21754. BUILDIN_DEF(clearitem,"?"),
  21755. BUILDIN_DEF(classchange,"i??"),
  21756. BUILDIN_DEF(misceffect,"i"),
  21757. BUILDIN_DEF(playBGM,"s"),
  21758. BUILDIN_DEF(playBGMall,"s?????"),
  21759. BUILDIN_DEF(soundeffect,"si"),
  21760. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  21761. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  21762. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  21763. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  21764. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  21765. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  21766. BUILDIN_DEF(petloot,"i"), // [Valaris]
  21767. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  21768. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  21769. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  21770. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  21771. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  21772. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  21773. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  21774. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  21775. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  21776. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  21777. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  21778. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  21779. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  21780. BUILDIN_DEF(npctalk,"s???"), // [Valaris]
  21781. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  21782. BUILDIN_DEF(mobcount,"ss"),
  21783. BUILDIN_DEF(getlook,"i?"),
  21784. BUILDIN_DEF(getsavepoint,"i?"),
  21785. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  21786. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  21787. BUILDIN_DEF(npcstop,""), // [Valaris]
  21788. BUILDIN_DEF(getmapxy,"rrr??"), //by Lorky [Lupus]
  21789. BUILDIN_DEF(mapid2name,"i"),
  21790. BUILDIN_DEF(checkoption1,"i?"),
  21791. BUILDIN_DEF(checkoption2,"i?"),
  21792. BUILDIN_DEF(guildgetexp,"i"),
  21793. BUILDIN_DEF(guildchangegm,"is"),
  21794. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  21795. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  21796. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  21797. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  21798. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  21799. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  21800. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  21801. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  21802. BUILDIN_DEF(night,""), // sets the server to night time
  21803. BUILDIN_DEF(day,""), // sets the server to day time
  21804. #ifdef PCRE_SUPPORT
  21805. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  21806. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  21807. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  21808. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  21809. #endif
  21810. BUILDIN_DEF(preg_match,"ss?"),
  21811. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  21812. BUILDIN_DEF(recovery,"i???"),
  21813. BUILDIN_DEF(getpetinfo,"i?"),
  21814. BUILDIN_DEF(gethominfo,"i?"),
  21815. BUILDIN_DEF(getmercinfo,"i?"),
  21816. BUILDIN_DEF(checkequipedcard,"i"),
  21817. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  21818. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  21819. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  21820. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  21821. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  21822. BUILDIN_DEF(charat,"si"),
  21823. BUILDIN_DEF(setchar,"ssi"),
  21824. BUILDIN_DEF(insertchar,"ssi"),
  21825. BUILDIN_DEF(delchar,"si"),
  21826. BUILDIN_DEF(strtoupper,"s"),
  21827. BUILDIN_DEF(strtolower,"s"),
  21828. BUILDIN_DEF(charisupper, "si"),
  21829. BUILDIN_DEF(charislower, "si"),
  21830. BUILDIN_DEF(substr,"sii"),
  21831. BUILDIN_DEF(explode, "rss"),
  21832. BUILDIN_DEF(implode, "r?"),
  21833. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  21834. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  21835. BUILDIN_DEF(strpos,"ss?"),
  21836. BUILDIN_DEF(replacestr,"sss??"),
  21837. BUILDIN_DEF(countstr,"ss?"),
  21838. BUILDIN_DEF(setnpcdisplay,"sv??"),
  21839. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  21840. BUILDIN_DEF(strcmp,"ss"),
  21841. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  21842. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  21843. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  21844. // [zBuffer] List of mathematics commands --->
  21845. BUILDIN_DEF(sqrt,"i"),
  21846. BUILDIN_DEF(pow,"ii"),
  21847. BUILDIN_DEF(distance,"iiii"),
  21848. BUILDIN_DEF2(minmax,"min", "*"),
  21849. BUILDIN_DEF2(minmax,"minimum", "*"),
  21850. BUILDIN_DEF2(minmax,"max", "*"),
  21851. BUILDIN_DEF2(minmax,"maximum", "*"),
  21852. // <--- [zBuffer] List of mathematics commands
  21853. BUILDIN_DEF(md5,"s"),
  21854. // [zBuffer] List of dynamic var commands --->
  21855. BUILDIN_DEF(getd,"s"),
  21856. BUILDIN_DEF(setd,"sv?"),
  21857. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  21858. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  21859. BUILDIN_DEF(npcshopadditem,"sii*"),
  21860. BUILDIN_DEF(npcshopdelitem,"si*"),
  21861. BUILDIN_DEF(npcshopattach,"s?"),
  21862. BUILDIN_DEF(equip,"i?"),
  21863. BUILDIN_DEF(autoequip,"ii"),
  21864. BUILDIN_DEF(setbattleflag,"si?"),
  21865. BUILDIN_DEF(getbattleflag,"s"),
  21866. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  21867. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  21868. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  21869. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  21870. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  21871. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  21872. BUILDIN_DEF(axtoi,"s"),
  21873. BUILDIN_DEF(query_sql,"s*"),
  21874. BUILDIN_DEF(query_logsql,"s*"),
  21875. BUILDIN_DEF(escape_sql,"v"),
  21876. BUILDIN_DEF(atoi,"s"),
  21877. BUILDIN_DEF(strtol,"si"),
  21878. // [zBuffer] List of player cont commands --->
  21879. BUILDIN_DEF(rid2name,"i"),
  21880. BUILDIN_DEF(pcfollow,"ii"),
  21881. BUILDIN_DEF(pcstopfollow,"i"),
  21882. BUILDIN_DEF(pcblockmove,"ii"),
  21883. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  21884. BUILDIN_DEF(pcblockskill,"ii"),
  21885. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  21886. BUILDIN_DEF(setpcblock, "ii?"),
  21887. BUILDIN_DEF(getpcblock, "?"),
  21888. // <--- [zBuffer] List of player cont commands
  21889. // [zBuffer] List of unit control commands --->
  21890. BUILDIN_DEF(unitexists,"i"),
  21891. BUILDIN_DEF(getunittype,"i"),
  21892. BUILDIN_DEF(getunitname,"i"),
  21893. BUILDIN_DEF(setunitname,"is"),
  21894. BUILDIN_DEF(setunittitle,"is"),
  21895. BUILDIN_DEF(getunittitle,"i"),
  21896. BUILDIN_DEF(getunitdata,"i*"),
  21897. BUILDIN_DEF(setunitdata,"iii"),
  21898. BUILDIN_DEF(unitwalk,"iii?"),
  21899. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  21900. BUILDIN_DEF(unitkill,"i"),
  21901. BUILDIN_DEF(unitwarp,"isii"),
  21902. BUILDIN_DEF(unitattack,"iv?"),
  21903. BUILDIN_DEF(unitstopattack,"i"),
  21904. BUILDIN_DEF(unitstopwalk,"i?"),
  21905. BUILDIN_DEF(unittalk,"is?"),
  21906. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  21907. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  21908. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  21909. // <--- [zBuffer] List of unit control commands
  21910. BUILDIN_DEF(sleep,"i"),
  21911. BUILDIN_DEF(sleep2,"i"),
  21912. BUILDIN_DEF(awake,"s"),
  21913. BUILDIN_DEF(getvariableofnpc,"rs"),
  21914. BUILDIN_DEF(warpportal,"iisii"),
  21915. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  21916. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  21917. BUILDIN_DEF(morphembryo,""),
  21918. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  21919. BUILDIN_DEF(checkhomcall,""),
  21920. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  21921. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  21922. BUILDIN_DEF(checkvending,"?"),
  21923. BUILDIN_DEF(checkchatting,"?"),
  21924. BUILDIN_DEF(checkidle,"?"),
  21925. BUILDIN_DEF(checkidlehom,"?"),
  21926. BUILDIN_DEF(openmail,"?"),
  21927. BUILDIN_DEF(openauction,"?"),
  21928. BUILDIN_DEF(checkcell,"siii"),
  21929. BUILDIN_DEF(setcell,"siiiiii"),
  21930. BUILDIN_DEF(getfreecell,"srr?????"),
  21931. BUILDIN_DEF(setwall,"siiiiis"),
  21932. BUILDIN_DEF(delwall,"s"),
  21933. BUILDIN_DEF(checkwall,"s"),
  21934. BUILDIN_DEF(searchitem,"rs"),
  21935. BUILDIN_DEF(mercenary_create,"ii"),
  21936. BUILDIN_DEF(mercenary_heal,"ii"),
  21937. BUILDIN_DEF(mercenary_sc_start,"iii"),
  21938. BUILDIN_DEF(mercenary_get_calls,"i"),
  21939. BUILDIN_DEF(mercenary_get_faith,"i"),
  21940. BUILDIN_DEF(mercenary_set_calls,"ii"),
  21941. BUILDIN_DEF(mercenary_set_faith,"ii"),
  21942. BUILDIN_DEF(readbook,"ii"),
  21943. BUILDIN_DEF(setfont,"i"),
  21944. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  21945. BUILDIN_DEF(progressbar,"si"),
  21946. BUILDIN_DEF(progressbar_npc, "si?"),
  21947. BUILDIN_DEF(pushpc,"ii"),
  21948. BUILDIN_DEF(buyingstore,"i"),
  21949. BUILDIN_DEF(searchstores,"ii"),
  21950. BUILDIN_DEF(showdigit,"i?"),
  21951. // WoE SE
  21952. BUILDIN_DEF(agitstart2,""),
  21953. BUILDIN_DEF(agitend2,""),
  21954. BUILDIN_DEF(agitcheck2,""),
  21955. // BattleGround
  21956. BUILDIN_DEF(waitingroom2bg,"sii???"),
  21957. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  21958. BUILDIN_DEF(bg_team_setxy,"iii"),
  21959. BUILDIN_DEF(bg_warp,"isii"),
  21960. BUILDIN_DEF(bg_monster,"isiisi?"),
  21961. BUILDIN_DEF(bg_monster_set_team,"ii"),
  21962. BUILDIN_DEF(bg_leave,"?"),
  21963. BUILDIN_DEF2(bg_leave,"bg_desert","?"),
  21964. BUILDIN_DEF(bg_destroy,"i"),
  21965. BUILDIN_DEF(areapercentheal,"siiiiii"),
  21966. BUILDIN_DEF(bg_get_data,"ii"),
  21967. BUILDIN_DEF(bg_getareausers,"isiiii"),
  21968. BUILDIN_DEF(bg_updatescore,"sii"),
  21969. BUILDIN_DEF(bg_join,"i????"),
  21970. BUILDIN_DEF(bg_create,"sii??"),
  21971. BUILDIN_DEF(bg_reserve,"s?"),
  21972. BUILDIN_DEF(bg_unbook,"s"),
  21973. BUILDIN_DEF(bg_info,"si"),
  21974. // Instancing
  21975. BUILDIN_DEF(instance_create,"s??"),
  21976. BUILDIN_DEF(instance_destroy,"?"),
  21977. BUILDIN_DEF(instance_id,"?"),
  21978. BUILDIN_DEF(instance_enter,"s????"),
  21979. BUILDIN_DEF(instance_npcname,"s?"),
  21980. BUILDIN_DEF(instance_mapname,"s?"),
  21981. BUILDIN_DEF(instance_warpall,"sii?"),
  21982. BUILDIN_DEF(instance_announce,"isi?????"),
  21983. BUILDIN_DEF(instance_check_party,"i???"),
  21984. BUILDIN_DEF(instance_check_guild,"i???"),
  21985. BUILDIN_DEF(instance_check_clan,"i???"),
  21986. BUILDIN_DEF(instance_info,"si?"),
  21987. BUILDIN_DEF(instance_live_info,"i?"),
  21988. /**
  21989. * 3rd-related
  21990. **/
  21991. BUILDIN_DEF(makerune,"i?"),
  21992. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  21993. BUILDIN_DEF(setdragon,"??"),//[Ind]
  21994. BUILDIN_DEF(ismounting,"?"),//[Ind]
  21995. BUILDIN_DEF(setmounting,"?"),//[Ind]
  21996. BUILDIN_DEF(checkre,"i"),
  21997. /**
  21998. * rAthena and beyond!
  21999. **/
  22000. BUILDIN_DEF(getargcount,""),
  22001. BUILDIN_DEF(getcharip,"?"),
  22002. BUILDIN_DEF(is_function,"s"),
  22003. BUILDIN_DEF(get_revision,""),
  22004. BUILDIN_DEF(get_githash,""),
  22005. BUILDIN_DEF(freeloop,"?"),
  22006. BUILDIN_DEF(getrandgroupitem,"i????"),
  22007. BUILDIN_DEF(cleanmap,"s"),
  22008. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  22009. BUILDIN_DEF(npcskill,"viii"),
  22010. BUILDIN_DEF(consumeitem,"v?"),
  22011. BUILDIN_DEF(delequip,"i?"),
  22012. BUILDIN_DEF(breakequip,"i?"),
  22013. BUILDIN_DEF(sit,"?"),
  22014. BUILDIN_DEF(stand,"?"),
  22015. //@commands (script based)
  22016. BUILDIN_DEF(bindatcmd, "ss??"),
  22017. BUILDIN_DEF(unbindatcmd, "s"),
  22018. BUILDIN_DEF(useatcmd, "s"),
  22019. //Quest Log System [Inkfish]
  22020. BUILDIN_DEF(questinfo, "i??"),
  22021. BUILDIN_DEF(setquest, "i?"),
  22022. BUILDIN_DEF(erasequest, "i?"),
  22023. BUILDIN_DEF(completequest, "i?"),
  22024. BUILDIN_DEF(checkquest, "i??"),
  22025. BUILDIN_DEF(isbegin_quest,"i?"),
  22026. BUILDIN_DEF(changequest, "ii?"),
  22027. BUILDIN_DEF(showevent, "i??"),
  22028. BUILDIN_DEF(questinfo_refresh, "?"),
  22029. //Bound items [Xantara] & [Akinari]
  22030. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  22031. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  22032. BUILDIN_DEF(countbound, "??"),
  22033. // Party related
  22034. BUILDIN_DEF(party_create,"s???"),
  22035. BUILDIN_DEF(party_addmember,"ii"),
  22036. BUILDIN_DEF(party_delmember,"??"),
  22037. BUILDIN_DEF(party_changeleader,"ii"),
  22038. BUILDIN_DEF(party_changeoption,"iii"),
  22039. BUILDIN_DEF(party_destroy,"i"),
  22040. // Clan system
  22041. BUILDIN_DEF(clan_join,"i?"),
  22042. BUILDIN_DEF(clan_leave,"?"),
  22043. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  22044. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  22045. BUILDIN_DEF(vip_status,"i?"),
  22046. BUILDIN_DEF(vip_time,"i?"),
  22047. BUILDIN_DEF(bonus_script,"si????"),
  22048. BUILDIN_DEF(bonus_script_clear,"??"),
  22049. BUILDIN_DEF(getgroupitem,"i??"),
  22050. BUILDIN_DEF(enable_command,""),
  22051. BUILDIN_DEF(disable_command,""),
  22052. BUILDIN_DEF(getguildmember,"i??"),
  22053. BUILDIN_DEF(addspiritball,"ii?"),
  22054. BUILDIN_DEF(delspiritball,"i?"),
  22055. BUILDIN_DEF(countspiritball,"?"),
  22056. BUILDIN_DEF(mergeitem,"?"),
  22057. BUILDIN_DEF(mergeitem2,"??"),
  22058. BUILDIN_DEF(npcshopupdate,"sii?"),
  22059. BUILDIN_DEF(getattachedrid,""),
  22060. BUILDIN_DEF(getvar,"vi"),
  22061. BUILDIN_DEF(showscript,"s??"),
  22062. BUILDIN_DEF(ignoretimeout,"i?"),
  22063. BUILDIN_DEF(geteleminfo,"i?"),
  22064. BUILDIN_DEF(opendressroom,"i?"),
  22065. BUILDIN_DEF(navigateto,"s???????"),
  22066. BUILDIN_DEF(getguildalliance,"ii"),
  22067. BUILDIN_DEF(adopt,"vv"),
  22068. BUILDIN_DEF(getexp2,"ii?"),
  22069. BUILDIN_DEF(recalculatestat,""),
  22070. BUILDIN_DEF(hateffect,"ii"),
  22071. BUILDIN_DEF(getrandomoptinfo, "i"),
  22072. BUILDIN_DEF(getequiprandomoption, "iii?"),
  22073. BUILDIN_DEF(setrandomoption,"iiiii?"),
  22074. BUILDIN_DEF(needed_status_point,"ii?"),
  22075. BUILDIN_DEF(jobcanentermap,"s?"),
  22076. BUILDIN_DEF(openstorage2,"ii?"),
  22077. BUILDIN_DEF(unloadnpc, "s"),
  22078. // WoE TE
  22079. BUILDIN_DEF(agitstart3,""),
  22080. BUILDIN_DEF(agitend3,""),
  22081. BUILDIN_DEF(agitcheck3,""),
  22082. BUILDIN_DEF(gvgon3,"s"),
  22083. BUILDIN_DEF(gvgoff3,"s"),
  22084. // Channel System
  22085. BUILDIN_DEF(channel_create,"ss?????"),
  22086. BUILDIN_DEF(channel_setopt,"sii"),
  22087. BUILDIN_DEF(channel_setcolor,"si"),
  22088. BUILDIN_DEF(channel_setpass,"ss"),
  22089. BUILDIN_DEF(channel_setgroup,"si*"),
  22090. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  22091. BUILDIN_DEF(channel_chat,"ss?"),
  22092. BUILDIN_DEF(channel_ban,"si"),
  22093. BUILDIN_DEF(channel_unban,"si"),
  22094. BUILDIN_DEF(channel_kick,"sv"),
  22095. BUILDIN_DEF(channel_delete,"s"),
  22096. // Item Random Option Extension [Cydh]
  22097. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  22098. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  22099. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  22100. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr"),
  22101. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  22102. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  22103. // Achievement System
  22104. BUILDIN_DEF(achievementinfo,"ii?"),
  22105. BUILDIN_DEF(achievementadd,"i?"),
  22106. BUILDIN_DEF(achievementremove,"i?"),
  22107. BUILDIN_DEF(achievementcomplete,"i?"),
  22108. BUILDIN_DEF(achievementexists,"i?"),
  22109. BUILDIN_DEF(achievementupdate,"iii?"),
  22110. BUILDIN_DEF(getequiprefinecost,"iii?"),
  22111. BUILDIN_DEF2(round, "round", "ii"),
  22112. BUILDIN_DEF2(round, "ceil", "ii"),
  22113. BUILDIN_DEF2(round, "floor", "ii"),
  22114. BUILDIN_DEF(getequiptradability, "i?"),
  22115. BUILDIN_DEF(mail, "isss*"),
  22116. BUILDIN_DEF(open_roulette,"?"),
  22117. BUILDIN_DEF(identifyall,"??"),
  22118. BUILDIN_DEF(is_guild_leader,"?"),
  22119. BUILDIN_DEF(is_party_leader,"?"),
  22120. BUILDIN_DEF(camerainfo,"iii?"),
  22121. BUILDIN_DEF(achievement_condition,"i"),
  22122. BUILDIN_DEF(getvariableofinstance,"ri"),
  22123. BUILDIN_DEF(convertpcinfo,"vi"),
  22124. BUILDIN_DEF(cloakoffnpc, "s?"),
  22125. BUILDIN_DEF(cloakonnpc, "s?"),
  22126. BUILDIN_DEF(isnpccloaked, "s?"),
  22127. BUILDIN_DEF(rentalcountitem, "v?"),
  22128. BUILDIN_DEF2(rentalcountitem, "rentalcountitem2", "viiiiiii?"),
  22129. BUILDIN_DEF2(rentalcountitem, "rentalcountitem3", "viiiiiiirrr?"),
  22130. #include "../custom/script_def.inc"
  22131. {NULL,NULL,NULL},
  22132. };