script.cpp 659 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463
  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/utils.hpp"
  27. #include "achievement.hpp"
  28. #include "atcommand.hpp"
  29. #include "battle.hpp"
  30. #include "battleground.hpp"
  31. #include "channel.hpp"
  32. #include "chat.hpp"
  33. #include "chrif.hpp"
  34. #include "clan.hpp"
  35. #include "clif.hpp"
  36. #include "date.hpp" // date type enum, date_get()
  37. #include "elemental.hpp"
  38. #include "guild.hpp"
  39. #include "homunculus.hpp"
  40. #include "instance.hpp"
  41. #include "intif.hpp"
  42. #include "itemdb.hpp"
  43. #include "log.hpp"
  44. #include "mail.hpp"
  45. #include "map.hpp"
  46. #include "mapreg.hpp"
  47. #include "mercenary.hpp"
  48. #include "mob.hpp"
  49. #include "npc.hpp"
  50. #include "party.hpp"
  51. #include "path.hpp"
  52. #include "pc.hpp"
  53. #include "pc_groups.hpp"
  54. #include "pet.hpp"
  55. #include "quest.hpp"
  56. #include "storage.hpp"
  57. struct eri *array_ers;
  58. DBMap *st_db;
  59. unsigned int active_scripts;
  60. unsigned int next_id;
  61. struct eri *st_ers;
  62. struct eri *stack_ers;
  63. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func );
  64. /**
  65. * Get `sd` from a account id in `loc` param instead of attached rid
  66. * @param st Script
  67. * @param loc Location to look account id in script parameter
  68. * @param sd Variable that will be assigned
  69. * @return True if `sd` is assigned, false otherwise
  70. **/
  71. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  72. if (script_hasdata(st, loc)) {
  73. int id_ = script_getnum(st, loc);
  74. if (!(*sd = map_id2sd(id_))){
  75. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  76. return false;
  77. }else{
  78. return true;
  79. }
  80. }
  81. else
  82. return script_rid2sd_(st,sd,func);
  83. }
  84. /**
  85. * Get `sd` from a char id in `loc` param instead of attached rid
  86. * @param st Script
  87. * @param loc Location to look char id in script parameter
  88. * @param sd Variable that will be assigned
  89. * @return True if `sd` is assigned, false otherwise
  90. **/
  91. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  92. if (script_hasdata(st, loc)) {
  93. int id_ = script_getnum(st, loc);
  94. if (!(*sd = map_charid2sd(id_))){
  95. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  96. return false;
  97. }else{
  98. return true;
  99. }
  100. }
  101. else
  102. return script_rid2sd_(st,sd,func);
  103. }
  104. /**
  105. * Get `sd` from a nick in `loc` param instead of attached rid
  106. * @param st Script
  107. * @param loc Location to look nick in script parameter
  108. * @param sd Variable that will be assigned
  109. * @return True if `sd` is assigned, false otherwise
  110. **/
  111. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  112. if (script_hasdata(st, loc)) {
  113. const char *name_ = script_getstr(st, loc);
  114. if (!(*sd = map_nick2sd(name_,false))){
  115. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  116. return false;
  117. }else{
  118. return true;
  119. }
  120. }
  121. else
  122. return script_rid2sd_(st,sd,func);
  123. }
  124. /**
  125. * Get `sd` from a mapid in `loc` param instead of attached rid
  126. * @param st Script
  127. * @param loc Location to look mapid in script parameter
  128. * @param sd Variable that will be assigned
  129. * @return True if `sd` is assigned, false otherwise
  130. **/
  131. static bool script_mapid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  132. if (script_hasdata(st, loc)) {
  133. int id_ = script_getnum(st, loc);
  134. if (!(*sd = map_id2sd(id_))){
  135. ShowError("%s: Player with map id '%d' is not found.\n", func, id_);
  136. return false;
  137. }else{
  138. return true;
  139. }
  140. }
  141. else
  142. return script_rid2sd_(st,sd,func);
  143. }
  144. /**
  145. * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator.
  146. * @param st Script
  147. * @param loc Location to look for ID in script parameter
  148. * @param bl Variable that will be assigned
  149. * @return True if `bl` is assigned, false otherwise
  150. **/
  151. static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) {
  152. int unit_id;
  153. if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0)
  154. unit_id = st->rid;
  155. *bl = map_id2bl(unit_id);
  156. if ( *bl )
  157. return true;
  158. else {
  159. ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id);
  160. return false;
  161. }
  162. }
  163. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  164. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  165. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  166. #define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__)
  167. #define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__)
  168. #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__)
  169. /// temporary buffer for passing around compiled bytecode
  170. /// @see add_scriptb, set_label, parse_script
  171. static unsigned char* script_buf = NULL;
  172. static int script_pos = 0, script_size = 0;
  173. static inline int GETVALUE(const unsigned char* buf, int i)
  174. {
  175. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  176. }
  177. static inline void SETVALUE(unsigned char* buf, int i, int n)
  178. {
  179. buf[i] = GetByte(n, 0);
  180. buf[i+1] = GetByte(n, 1);
  181. buf[i+2] = GetByte(n, 2);
  182. }
  183. // String buffer structures.
  184. // str_data stores string information
  185. static struct str_data_struct {
  186. enum c_op type;
  187. int str;
  188. int backpatch;
  189. int label;
  190. int (*func)(struct script_state *st);
  191. int val;
  192. int next;
  193. bool deprecated;
  194. } *str_data = NULL;
  195. static int str_data_size = 0; // size of the data
  196. static int str_num = LABEL_START; // next id to be assigned
  197. // str_buf holds the strings themselves
  198. static char *str_buf;
  199. static int str_size = 0; // size of the buffer
  200. static int str_pos = 0; // next position to be assigned
  201. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  202. #define SCRIPT_HASH_SIZE 1021
  203. int str_hash[SCRIPT_HASH_SIZE];
  204. // Specifies which string hashing method to use
  205. //#define SCRIPT_HASH_DJB2
  206. //#define SCRIPT_HASH_SDBM
  207. #define SCRIPT_HASH_ELF
  208. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  209. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  210. static int parse_options = 0;
  211. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  212. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  213. // important buildin function references for usage in scripts
  214. static int buildin_set_ref = 0;
  215. static int buildin_callsub_ref = 0;
  216. static int buildin_callfunc_ref = 0;
  217. static int buildin_getelementofarray_ref = 0;
  218. // Caches compiled autoscript item code.
  219. // Note: This is not cleared when reloading itemdb.
  220. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  221. struct Script_Config script_config = {
  222. 1, // warn_func_mismatch_argtypes
  223. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  224. 0, INT_MAX, // input_min_value/input_max_value
  225. // NOTE: None of these event labels should be longer than <EVENT_NAME_LENGTH> characters
  226. // PC related
  227. "OnPCDieEvent", //die_event_name
  228. "OnPCKillEvent", //kill_pc_event_name
  229. "OnNPCKillEvent", //kill_mob_event_name
  230. "OnPCLoginEvent", //login_event_name
  231. "OnPCLogoutEvent", //logout_event_name
  232. "OnPCLoadMapEvent", //loadmap_event_name
  233. "OnPCBaseLvUpEvent", //baselvup_event_name
  234. "OnPCJobLvUpEvent", //joblvup_event_name
  235. "OnPCStatCalcEvent", //stat_calc_event_name
  236. // NPC related
  237. "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves)
  238. "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area)
  239. "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area)
  240. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  241. "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc)
  242. "OnBuyItem", //onbuy_event_name (is executed when items are bought)
  243. "OnSellItem", //onsell_event_name (is executed when items are sold)
  244. // Init related
  245. "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded)
  246. "OnInterIfInit", //inter_init_event_name (is executed on inter server connection)
  247. "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection)
  248. // Guild related
  249. "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken)
  250. "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started)
  251. "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded)
  252. "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended)
  253. "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started)
  254. "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded)
  255. "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended)
  256. "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started)
  257. "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded)
  258. "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended)
  259. // Timer related
  260. "OnTimer", //timer_event_name (is executed by a timer at the specific second)
  261. "OnTimerQuit", //timer_quit_event_name (is executed when a timer is aborted)
  262. "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute)
  263. "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour)
  264. "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute)
  265. "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day)
  266. "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute)
  267. "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute)
  268. "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute)
  269. "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute)
  270. "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute)
  271. "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute)
  272. "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute)
  273. // Instance related
  274. "OnInstanceInit", //instance_init_event_name (is executed right after instance creation)
  275. "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction)
  276. };
  277. static jmp_buf error_jump;
  278. static char* error_msg;
  279. static const char* error_pos;
  280. static int error_report; // if the error should produce output
  281. // Used by disp_warning_message
  282. static const char* parser_current_src;
  283. static const char* parser_current_file;
  284. static int parser_current_line;
  285. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  286. // [Eoe / jA 1080, 1081, 1094, 1164]
  287. enum curly_type {
  288. TYPE_NULL = 0,
  289. TYPE_IF,
  290. TYPE_SWITCH,
  291. TYPE_WHILE,
  292. TYPE_FOR,
  293. TYPE_DO,
  294. TYPE_USERFUNC,
  295. TYPE_ARGLIST // function argument list
  296. };
  297. enum e_arglist
  298. {
  299. ARGLIST_UNDEFINED = 0,
  300. ARGLIST_NO_PAREN = 1,
  301. ARGLIST_PAREN = 2,
  302. };
  303. static struct {
  304. struct {
  305. enum curly_type type;
  306. int index;
  307. int count;
  308. int flag;
  309. struct linkdb_node *case_label;
  310. } curly[256]; // Information right parenthesis
  311. int curly_count; // The number of right brackets
  312. int index; // Number of the syntax used in the script
  313. } syntax;
  314. const char* parse_curly_close(const char* p);
  315. const char* parse_syntax_close(const char* p);
  316. const char* parse_syntax_close_sub(const char* p,int* flag);
  317. const char* parse_syntax(const char* p);
  318. static int parse_syntax_for_flag = 0;
  319. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  320. extern unsigned int current_equip_combo_pos;
  321. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  322. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  323. int potion_target = 0;
  324. unsigned int *generic_ui_array = NULL;
  325. unsigned int generic_ui_array_size = 0;
  326. c_op get_com(unsigned char *script,int *pos);
  327. int get_num(unsigned char *script,int *pos);
  328. typedef struct script_function {
  329. int (*func)(struct script_state *st);
  330. const char *name;
  331. const char *arg;
  332. const char *deprecated;
  333. } script_function;
  334. extern script_function buildin_func[];
  335. static struct linkdb_node *sleep_db; // int oid -> struct script_state *
  336. /*==========================================
  337. * (Only those needed) local declaration prototype
  338. *------------------------------------------*/
  339. const char* parse_subexpr(const char* p,int limit);
  340. int run_func(struct script_state *st);
  341. unsigned short script_instancegetid(struct script_state *st);
  342. const char* script_op2name(int op)
  343. {
  344. #define RETURN_OP_NAME(type) case type: return #type
  345. switch( op )
  346. {
  347. RETURN_OP_NAME(C_NOP);
  348. RETURN_OP_NAME(C_POS);
  349. RETURN_OP_NAME(C_INT);
  350. RETURN_OP_NAME(C_PARAM);
  351. RETURN_OP_NAME(C_FUNC);
  352. RETURN_OP_NAME(C_STR);
  353. RETURN_OP_NAME(C_CONSTSTR);
  354. RETURN_OP_NAME(C_ARG);
  355. RETURN_OP_NAME(C_NAME);
  356. RETURN_OP_NAME(C_EOL);
  357. RETURN_OP_NAME(C_RETINFO);
  358. RETURN_OP_NAME(C_USERFUNC);
  359. RETURN_OP_NAME(C_USERFUNC_POS);
  360. RETURN_OP_NAME(C_REF);
  361. // operators
  362. RETURN_OP_NAME(C_OP3);
  363. RETURN_OP_NAME(C_LOR);
  364. RETURN_OP_NAME(C_LAND);
  365. RETURN_OP_NAME(C_LE);
  366. RETURN_OP_NAME(C_LT);
  367. RETURN_OP_NAME(C_GE);
  368. RETURN_OP_NAME(C_GT);
  369. RETURN_OP_NAME(C_EQ);
  370. RETURN_OP_NAME(C_NE);
  371. RETURN_OP_NAME(C_XOR);
  372. RETURN_OP_NAME(C_OR);
  373. RETURN_OP_NAME(C_AND);
  374. RETURN_OP_NAME(C_ADD);
  375. RETURN_OP_NAME(C_SUB);
  376. RETURN_OP_NAME(C_MUL);
  377. RETURN_OP_NAME(C_DIV);
  378. RETURN_OP_NAME(C_MOD);
  379. RETURN_OP_NAME(C_NEG);
  380. RETURN_OP_NAME(C_LNOT);
  381. RETURN_OP_NAME(C_NOT);
  382. RETURN_OP_NAME(C_R_SHIFT);
  383. RETURN_OP_NAME(C_L_SHIFT);
  384. RETURN_OP_NAME(C_ADD_POST);
  385. RETURN_OP_NAME(C_SUB_POST);
  386. RETURN_OP_NAME(C_ADD_PRE);
  387. RETURN_OP_NAME(C_SUB_PRE);
  388. default:
  389. ShowDebug("script_op2name: unexpected op=%d\n", op);
  390. return "???";
  391. }
  392. #undef RETURN_OP_NAME
  393. }
  394. #ifdef DEBUG_DUMP_STACK
  395. static void script_dump_stack(struct script_state* st)
  396. {
  397. int i;
  398. ShowMessage("\tstart = %d\n", st->start);
  399. ShowMessage("\tend = %d\n", st->end);
  400. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  401. ShowMessage("\tsp = %d\n", st->stack->sp);
  402. for( i = 0; i < st->stack->sp; ++i )
  403. {
  404. struct script_data* data = &st->stack->stack_data[i];
  405. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  406. switch( data->type )
  407. {
  408. case C_INT:
  409. case C_POS:
  410. ShowMessage(" %d\n", data->u.num);
  411. break;
  412. case C_STR:
  413. case C_CONSTSTR:
  414. ShowMessage(" \"%s\"\n", data->u.str);
  415. break;
  416. case C_NAME:
  417. 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));
  418. break;
  419. case C_RETINFO:
  420. {
  421. struct script_retinfo* ri = data->u.ri;
  422. 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);
  423. }
  424. break;
  425. default:
  426. ShowMessage("\n");
  427. break;
  428. }
  429. }
  430. }
  431. #endif
  432. /// Reports on the console the src of a script error.
  433. static void script_reportsrc(struct script_state *st)
  434. {
  435. struct block_list* bl;
  436. if( st->oid == 0 )
  437. return; //Can't report source.
  438. bl = map_id2bl(st->oid);
  439. if( bl == NULL )
  440. return;
  441. switch( bl->type ) {
  442. case BL_NPC:
  443. if( bl->m >= 0 )
  444. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  445. else
  446. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  447. break;
  448. default:
  449. if( bl->m >= 0 )
  450. ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status_get_name(bl), map[bl->m].name, bl->x, bl->y);
  451. else
  452. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  453. break;
  454. }
  455. }
  456. /// Reports on the console information about the script data.
  457. static void script_reportdata(struct script_data* data)
  458. {
  459. if( data == NULL )
  460. return;
  461. switch( data->type ) {
  462. case C_NOP:// no value
  463. ShowDebug("Data: nothing (nil)\n");
  464. break;
  465. case C_INT:// number
  466. ShowDebug("Data: number value=%" PRId64 "\n", data->u.num);
  467. break;
  468. case C_STR:
  469. case C_CONSTSTR:// string
  470. if( data->u.str ) {
  471. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  472. } else {
  473. ShowDebug("Data: string value=NULL\n");
  474. }
  475. break;
  476. case C_NAME:// reference
  477. if( reference_tovariable(data) ) {// variable
  478. const char* name = reference_getname(data);
  479. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  480. } else if( reference_toconstant(data) ) {// constant
  481. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  482. } else if( reference_toparam(data) ) {// param
  483. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  484. } else {// ???
  485. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  486. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  487. }
  488. break;
  489. case C_POS:// label
  490. ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num);
  491. break;
  492. default:
  493. ShowDebug("Data: %s\n", script_op2name(data->type));
  494. break;
  495. }
  496. }
  497. /// Reports on the console information about the current built-in function.
  498. static void script_reportfunc(struct script_state* st)
  499. {
  500. int params, id;
  501. struct script_data* data;
  502. if( !script_hasdata(st,0) )
  503. {// no stack
  504. return;
  505. }
  506. data = script_getdata(st,0);
  507. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  508. {// script currently not executing a built-in function or corrupt stack
  509. return;
  510. }
  511. id = reference_getid(data);
  512. params = script_lastdata(st)-1;
  513. if( params > 0 )
  514. {
  515. int i;
  516. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  517. for( i = 2; i <= script_lastdata(st); i++ )
  518. {
  519. script_reportdata(script_getdata(st,i));
  520. }
  521. }
  522. else
  523. {
  524. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  525. }
  526. }
  527. /*==========================================
  528. * Output error message
  529. *------------------------------------------*/
  530. static void disp_error_message2(const char *mes,const char *pos,int report)
  531. {
  532. error_msg = aStrdup(mes);
  533. error_pos = pos;
  534. error_report = report;
  535. longjmp( error_jump, 1 );
  536. }
  537. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  538. static void disp_warning_message(const char *mes, const char *pos) {
  539. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  540. }
  541. /// Checks event parameter validity
  542. static void check_event(struct script_state *st, const char *evt)
  543. {
  544. if( evt && evt[0] && !stristr(evt, "::On") )
  545. {
  546. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  547. script_reportsrc(st);
  548. }
  549. }
  550. /*==========================================
  551. * Hashes the input string
  552. *------------------------------------------*/
  553. static unsigned int calc_hash(const char* p)
  554. {
  555. unsigned int h;
  556. #if defined(SCRIPT_HASH_DJB2)
  557. h = 5381;
  558. while( *p ) // hash*33 + c
  559. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  560. #elif defined(SCRIPT_HASH_SDBM)
  561. h = 0;
  562. while( *p ) // hash*65599 + c
  563. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  564. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  565. h = 0;
  566. while( *p ){
  567. unsigned int g;
  568. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  569. g = h & 0xF0000000;
  570. if( g )
  571. {
  572. h ^= g >> 24;
  573. h &= ~g;
  574. }
  575. }
  576. #else // athena hash
  577. h = 0;
  578. while( *p )
  579. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  580. #endif
  581. return h % SCRIPT_HASH_SIZE;
  582. }
  583. bool script_check_RegistryVariableLength(int pType, const char *val, size_t* vlen)
  584. {
  585. size_t len = strlen(val);
  586. if (vlen)
  587. *vlen = len;
  588. switch (pType) {
  589. case 0:
  590. return (len < 33); // key check
  591. case 1:
  592. return (len < 255); // value check
  593. default:
  594. return false;
  595. }
  596. }
  597. /*==========================================
  598. * str_data manipulation functions
  599. *------------------------------------------*/
  600. /// Looks up string using the provided id.
  601. const char* get_str(int id)
  602. {
  603. Assert( id >= LABEL_START && id < str_size );
  604. return str_buf+str_data[id].str;
  605. }
  606. /// Returns the uid of the string, or -1.
  607. static int search_str(const char* p)
  608. {
  609. int i;
  610. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  611. if( strcasecmp(get_str(i),p) == 0 )
  612. return i;
  613. return -1;
  614. }
  615. /// Stores a copy of the string and returns its id.
  616. /// If an identical string is already present, returns its id instead.
  617. int add_str(const char* p)
  618. {
  619. int h;
  620. int len;
  621. h = calc_hash(p);
  622. if( str_hash[h] == 0 )
  623. {// empty bucket, add new node here
  624. str_hash[h] = str_num;
  625. }
  626. else
  627. {// scan for end of list, or occurence of identical string
  628. int i;
  629. for( i = str_hash[h]; ; i = str_data[i].next )
  630. {
  631. if( strcasecmp(get_str(i),p) == 0 )
  632. return i; // string already in list
  633. if( str_data[i].next == 0 )
  634. break; // reached the end
  635. }
  636. // append node to end of list
  637. str_data[i].next = str_num;
  638. }
  639. // grow list if neccessary
  640. if( str_num >= str_data_size )
  641. {
  642. str_data_size += 128;
  643. RECREATE(str_data,struct str_data_struct,str_data_size);
  644. memset(str_data + (str_data_size - 128), '\0', 128);
  645. }
  646. len=(int)strlen(p);
  647. // grow string buffer if neccessary
  648. while( str_pos+len+1 >= str_size )
  649. {
  650. str_size += 256;
  651. RECREATE(str_buf,char,str_size);
  652. memset(str_buf + (str_size - 256), '\0', 256);
  653. }
  654. safestrncpy(str_buf+str_pos, p, len+1);
  655. str_data[str_num].type = C_NOP;
  656. str_data[str_num].str = str_pos;
  657. str_data[str_num].next = 0;
  658. str_data[str_num].func = NULL;
  659. str_data[str_num].backpatch = -1;
  660. str_data[str_num].label = -1;
  661. str_pos += len+1;
  662. return str_num++;
  663. }
  664. /// Appends 1 byte to the script buffer.
  665. static void add_scriptb(int a)
  666. {
  667. if( script_pos+1 >= script_size )
  668. {
  669. script_size += SCRIPT_BLOCK_SIZE;
  670. RECREATE(script_buf,unsigned char,script_size);
  671. }
  672. script_buf[script_pos++] = (uint8)(a);
  673. }
  674. /// Appends a c_op value to the script buffer.
  675. /// The value is variable-length encoded into 8-bit blocks.
  676. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  677. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  678. static void add_scriptc(int a)
  679. {
  680. while( a >= 0x40 )
  681. {
  682. add_scriptb((a&0x3f)|0x40);
  683. a = (a - 0x40) >> 6;
  684. }
  685. add_scriptb(a);
  686. }
  687. /// Appends an integer value to the script buffer.
  688. /// The value is variable-length encoded into 8-bit blocks.
  689. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  690. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  691. static void add_scripti(int a)
  692. {
  693. while( a >= 0x40 )
  694. {
  695. add_scriptb((a&0x3f)|0xc0);
  696. a = (a - 0x40) >> 6;
  697. }
  698. add_scriptb(a|0x80);
  699. }
  700. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  701. ///
  702. /// @param l The id of the str_data entry
  703. // Maximum up to 16M
  704. static void add_scriptl(int l)
  705. {
  706. int backpatch = str_data[l].backpatch;
  707. switch(str_data[l].type){
  708. case C_POS:
  709. case C_USERFUNC_POS:
  710. add_scriptc(C_POS);
  711. add_scriptb(str_data[l].label);
  712. add_scriptb(str_data[l].label>>8);
  713. add_scriptb(str_data[l].label>>16);
  714. break;
  715. case C_NOP:
  716. case C_USERFUNC:
  717. // Embedded data backpatch there is a possibility of label
  718. add_scriptc(C_NAME);
  719. str_data[l].backpatch = script_pos;
  720. add_scriptb(backpatch);
  721. add_scriptb(backpatch>>8);
  722. add_scriptb(backpatch>>16);
  723. break;
  724. case C_INT:
  725. add_scripti(abs(str_data[l].val));
  726. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  727. add_scriptc(C_NEG);
  728. break;
  729. default: // assume C_NAME
  730. add_scriptc(C_NAME);
  731. add_scriptb(l);
  732. add_scriptb(l>>8);
  733. add_scriptb(l>>16);
  734. break;
  735. }
  736. }
  737. /*==========================================
  738. * Resolve the label
  739. *------------------------------------------*/
  740. void set_label(int l,int pos, const char* script_pos_cur)
  741. {
  742. int i;
  743. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  744. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  745. disp_error_message("set_label: invalid label name",script_pos_cur);
  746. return;
  747. }
  748. if(str_data[l].label!=-1){
  749. disp_error_message("set_label: dup label ",script_pos_cur);
  750. return;
  751. }
  752. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  753. str_data[l].label=pos;
  754. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  755. int next=GETVALUE(script_buf,i);
  756. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  757. SETVALUE(script_buf,i,pos);
  758. i=next;
  759. }
  760. }
  761. /// Skips spaces and/or comments.
  762. const char* skip_space(const char* p)
  763. {
  764. if( p == NULL )
  765. return NULL;
  766. for(;;)
  767. {
  768. while( ISSPACE(*p) )
  769. ++p;
  770. if( *p == '/' && p[1] == '/' )
  771. {// line comment
  772. while(*p && *p!='\n')
  773. ++p;
  774. }
  775. else if( *p == '/' && p[1] == '*' )
  776. {// block comment
  777. p += 2;
  778. for(;;)
  779. {
  780. if( *p == '\0' ) {
  781. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p);
  782. return p;
  783. }
  784. if( *p == '*' && p[1] == '/' )
  785. {// end of block comment
  786. p += 2;
  787. break;
  788. }
  789. ++p;
  790. }
  791. }
  792. else
  793. break;
  794. }
  795. return p;
  796. }
  797. /// Skips a word.
  798. /// A word consists of undercores and/or alphanumeric characters,
  799. /// and valid variable prefixes/postfixes.
  800. static const char* skip_word(const char* p)
  801. {
  802. // prefix
  803. switch( *p ) {
  804. case '@':// temporary char variable
  805. ++p; break;
  806. case '#':// account variable
  807. p += ( p[1] == '#' ? 2 : 1 ); break;
  808. case '\'':// instance variable
  809. ++p; break;
  810. case '.':// npc variable
  811. p += ( p[1] == '@' ? 2 : 1 ); break;
  812. case '$':// global variable
  813. p += ( p[1] == '@' ? 2 : 1 ); break;
  814. }
  815. while( ISALNUM(*p) || *p == '_' )
  816. ++p;
  817. // postfix
  818. if( *p == '$' )// string
  819. p++;
  820. return p;
  821. }
  822. /// Adds a word to str_data.
  823. /// @see skip_word
  824. /// @see add_str
  825. static int add_word(const char* p)
  826. {
  827. char* word;
  828. int len;
  829. int i;
  830. // Check for a word
  831. len = skip_word(p) - p;
  832. if( len == 0 )
  833. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  834. // Duplicate the word
  835. word = (char*)aMalloc(len+1);
  836. memcpy(word, p, len);
  837. word[len] = 0;
  838. // add the word
  839. i = add_str(word);
  840. aFree(word);
  841. return i;
  842. }
  843. /// Parses a function call.
  844. /// The argument list can have parenthesis or not.
  845. /// The number of arguments is checked.
  846. static
  847. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  848. {
  849. const char* p2;
  850. const char* arg=NULL;
  851. int func;
  852. func = add_word(p);
  853. if( str_data[func].type == C_FUNC ){
  854. // buildin function
  855. add_scriptl(func);
  856. add_scriptc(C_ARG);
  857. arg = buildin_func[str_data[func].val].arg;
  858. #if defined(SCRIPT_COMMAND_DEPRECATION)
  859. if( str_data[func].deprecated ){
  860. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  861. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  862. }
  863. #endif
  864. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  865. // script defined function
  866. add_scriptl(buildin_callsub_ref);
  867. add_scriptc(C_ARG);
  868. add_scriptl(func);
  869. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  870. if( *arg == 0 )
  871. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  872. if( *arg != '*' )
  873. ++arg; // count func as argument
  874. } else {
  875. const char* name = get_str(func);
  876. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  877. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  878. } else {;
  879. add_scriptl(buildin_callfunc_ref);
  880. add_scriptc(C_ARG);
  881. add_scriptc(C_STR);
  882. while( *name ) add_scriptb(*name ++);
  883. add_scriptb(0);
  884. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  885. if( *arg != '*' ) ++ arg;
  886. }
  887. }
  888. p = skip_word(p);
  889. p = skip_space(p);
  890. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  891. syntax.curly[syntax.curly_count].count = 0;
  892. if( *p == ';' )
  893. {// <func name> ';'
  894. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  895. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  896. {// <func name> '(' ')'
  897. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  898. p = p2;
  899. /*
  900. } else if( 0 && require_paren && *p != '(' )
  901. {// <func name>
  902. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  903. */
  904. } else
  905. {// <func name> <arg list>
  906. if( require_paren ){
  907. if( *p != '(' )
  908. disp_error_message("need '('",p);
  909. ++p; // skip '('
  910. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  911. } else if( *p == '(' ){
  912. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  913. } else {
  914. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  915. }
  916. ++syntax.curly_count;
  917. while( *arg ) {
  918. p2=parse_subexpr(p,-1);
  919. if( p == p2 )
  920. break; // not an argument
  921. if( *arg != '*' )
  922. ++arg; // next argument
  923. p=skip_space(p2);
  924. if( *arg == 0 || *p != ',' )
  925. break; // no more arguments
  926. ++p; // skip comma
  927. }
  928. --syntax.curly_count;
  929. }
  930. if( *arg && *arg != '?' && *arg != '*' )
  931. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  932. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  933. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  934. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  935. if( *p != ')' )
  936. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  937. ++p;
  938. }
  939. add_scriptc(C_FUNC);
  940. return p;
  941. }
  942. /// Processes end of logical script line.
  943. /// @param first When true, only fix up scheduling data is initialized
  944. /// @param p Script position for error reporting in set_label
  945. static void parse_nextline(bool first, const char* p)
  946. {
  947. if( !first )
  948. {
  949. add_scriptc(C_EOL); // mark end of line for stack cleanup
  950. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  951. }
  952. // initialize data for new '-' label fix up scheduling
  953. str_data[LABEL_NEXTLINE].type = C_NOP;
  954. str_data[LABEL_NEXTLINE].backpatch = -1;
  955. str_data[LABEL_NEXTLINE].label = -1;
  956. }
  957. /**
  958. * Pushes a variable into stack, processing its array index if needed.
  959. * @see parse_variable
  960. */
  961. void parse_variable_sub_push(int word, const char *p2)
  962. {
  963. if( p2 ) { // Process the variable index
  964. const char *p3 = NULL;
  965. // Push the getelementofarray method into the stack
  966. add_scriptl(buildin_getelementofarray_ref);
  967. add_scriptc(C_ARG);
  968. add_scriptl(word);
  969. // Process the sub-expression for this assignment
  970. p3 = parse_subexpr(p2 + 1, 1);
  971. p3 = skip_space(p3);
  972. if( *p3 != ']' ) // Closing parenthesis is required for this script
  973. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  974. // Push the closing function stack operator onto the stack
  975. add_scriptc(C_FUNC);
  976. p3++;
  977. } else // No array index, simply push the variable or value onto the stack
  978. add_scriptl(word);
  979. }
  980. /// Parse a variable assignment using the direct equals operator
  981. /// @param p script position where the function should run from
  982. /// @return NULL if not a variable assignment, the new position otherwise
  983. const char* parse_variable(const char* p) {
  984. int word;
  985. c_op type = C_NOP;
  986. const char *p2 = NULL;
  987. const char *var = p;
  988. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  989. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  990. var = p = skip_space(&p[2]);
  991. // skip the variable where applicable
  992. p = skip_word(p);
  993. p = skip_space(p);
  994. if( p == NULL ) {// end of the line or invalid buffer
  995. return NULL;
  996. }
  997. if( *p == '[' ) {// array variable so process the array as appropriate
  998. int i,j;
  999. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1000. if( *p ++ == ']' && --(j) == 0 ) break;
  1001. if( *p == '[' ) ++ j;
  1002. }
  1003. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1004. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1005. }
  1006. }
  1007. if( type == C_NOP &&
  1008. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1009. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1010. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1011. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1012. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1013. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1014. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1015. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1016. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1017. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1018. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  1019. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  1020. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1021. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1022. ) )
  1023. {// failed to find a matching operator combination so invalid
  1024. return NULL;
  1025. }
  1026. switch( type ) {
  1027. case C_ADD_PRE: // pre ++
  1028. case C_SUB_PRE: // pre --
  1029. // Nothing more to skip
  1030. break;
  1031. case C_EQ: {// incremental modifier
  1032. p = skip_space( &p[1] );
  1033. }
  1034. break;
  1035. case C_L_SHIFT:
  1036. case C_R_SHIFT: {// left or right shift modifier
  1037. p = skip_space( &p[3] );
  1038. }
  1039. break;
  1040. default: {// normal incremental command
  1041. p = skip_space( &p[2] );
  1042. }
  1043. }
  1044. if( p == NULL ) {// end of line or invalid buffer
  1045. return NULL;
  1046. }
  1047. // push the set function onto the stack
  1048. add_scriptl(buildin_set_ref);
  1049. add_scriptc(C_ARG);
  1050. // always append parenthesis to avoid errors
  1051. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1052. syntax.curly[syntax.curly_count].count = 0;
  1053. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1054. // increment the total curly count for the position in the script
  1055. ++ syntax.curly_count;
  1056. // parse the variable currently being modified
  1057. word = add_word(var);
  1058. 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
  1059. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1060. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1061. if( type != C_EQ )
  1062. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1063. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1064. add_scripti(1);
  1065. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1066. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1067. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1068. add_scripti(1);
  1069. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1070. } else { // Process the value as an expression
  1071. p = parse_subexpr(p, -1);
  1072. if( type != C_EQ )
  1073. {// push the type of modifier onto the stack
  1074. add_scriptc(type);
  1075. }
  1076. }
  1077. // decrement the curly count for the position within the script
  1078. -- syntax.curly_count;
  1079. // close the script by appending the function operator
  1080. add_scriptc(C_FUNC);
  1081. // push the buffer from the method
  1082. return p;
  1083. }
  1084. /*
  1085. * Checks whether the gives string is a number literal
  1086. *
  1087. * Mainly necessary to differentiate between number literals and NPC name
  1088. * constants, since several of those start with a digit.
  1089. *
  1090. * All this does is to check if the string begins with an optional + or - sign,
  1091. * followed by a hexadecimal or decimal number literal literal and is NOT
  1092. * followed by a underscore or letter.
  1093. *
  1094. * @author : Hercules.ws
  1095. * @param p Pointer to the string to check
  1096. * @return Whether the string is a number literal
  1097. */
  1098. bool is_number(const char *p) {
  1099. const char *np;
  1100. if (!p)
  1101. return false;
  1102. if (*p == '-' || *p == '+')
  1103. p++;
  1104. np = p;
  1105. if (*p == '0' && p[1] == 'x') {
  1106. p+=2;
  1107. np = p;
  1108. // Hexadecimal
  1109. while (ISXDIGIT(*np))
  1110. np++;
  1111. } else {
  1112. // Decimal
  1113. while (ISDIGIT(*np))
  1114. np++;
  1115. }
  1116. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1117. return true;
  1118. return false;
  1119. }
  1120. /*==========================================
  1121. * Analysis section
  1122. *------------------------------------------*/
  1123. const char* parse_simpleexpr(const char *p)
  1124. {
  1125. long long i;
  1126. p=skip_space(p);
  1127. if(*p==';' || *p==',')
  1128. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1129. if(*p=='('){
  1130. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1131. ++syntax.curly[i].count;
  1132. p=parse_subexpr(p+1,-1);
  1133. p=skip_space(p);
  1134. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1135. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1136. ){
  1137. if( *p == ',' ){
  1138. syntax.curly[i].flag = ARGLIST_PAREN;
  1139. return p;
  1140. } else
  1141. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1142. }
  1143. if( *p != ')' )
  1144. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1145. ++p;
  1146. } else if(is_number(p)) {
  1147. char *np;
  1148. while(*p == '0' && ISDIGIT(p[1])) p++;
  1149. i=strtoll(p,&np,0);
  1150. if( i < INT_MIN ) {
  1151. i = INT_MIN;
  1152. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1153. } else if( i > INT_MAX ) {
  1154. i = INT_MAX;
  1155. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1156. }
  1157. add_scripti((int)i);
  1158. p=np;
  1159. } else if(*p=='"'){
  1160. add_scriptc(C_STR);
  1161. p++;
  1162. while( *p && *p != '"' ){
  1163. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1164. {
  1165. char buf[8];
  1166. size_t len = skip_escaped_c(p) - p;
  1167. size_t n = sv_unescape_c(buf, p, len);
  1168. if( n != 1 )
  1169. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1170. p += len;
  1171. add_scriptb(*buf);
  1172. continue;
  1173. }
  1174. else if( *p == '\n' )
  1175. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1176. add_scriptb(*p++);
  1177. }
  1178. if(!*p)
  1179. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1180. add_scriptb(0);
  1181. p++; //'"'
  1182. } else {
  1183. int l;
  1184. const char* pv;
  1185. // label , register , function etc
  1186. if(skip_word(p)==p)
  1187. disp_error_message("parse_simpleexpr: unexpected character",p);
  1188. l=add_word(p);
  1189. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1190. return parse_callfunc(p,1,0);
  1191. else {
  1192. const char* name = get_str(l);
  1193. if( strdb_get(userfunc_db,name) != NULL ) {
  1194. return parse_callfunc(p,1,1);
  1195. }
  1196. }
  1197. if( (pv = parse_variable(p)) )
  1198. {// successfully processed a variable assignment
  1199. return pv;
  1200. }
  1201. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1202. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1203. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1204. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1205. }
  1206. #endif
  1207. p=skip_word(p);
  1208. if( *p == '[' ){
  1209. // array(name[i] => getelementofarray(name,i) )
  1210. add_scriptl(buildin_getelementofarray_ref);
  1211. add_scriptc(C_ARG);
  1212. add_scriptl(l);
  1213. p=parse_subexpr(p+1,-1);
  1214. p=skip_space(p);
  1215. if( *p != ']' )
  1216. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1217. ++p;
  1218. add_scriptc(C_FUNC);
  1219. }else
  1220. add_scriptl(l);
  1221. }
  1222. return p;
  1223. }
  1224. /*==========================================
  1225. * Analysis of the expression
  1226. *------------------------------------------*/
  1227. const char* parse_subexpr(const char* p,int limit)
  1228. {
  1229. int op,opl,len;
  1230. p=skip_space(p);
  1231. if( *p == '-' ){
  1232. const char* tmpp = skip_space(p+1);
  1233. if( *tmpp == ';' || *tmpp == ',' ){
  1234. add_scriptl(LABEL_NEXTLINE);
  1235. p++;
  1236. return p;
  1237. }
  1238. }
  1239. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1240. p = parse_variable(p);
  1241. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1242. p = parse_subexpr(p + 1, 11);
  1243. add_scriptc(op);
  1244. } else
  1245. p = parse_simpleexpr(p);
  1246. p = skip_space(p);
  1247. while((
  1248. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1249. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1250. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1251. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1252. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1253. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1254. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1255. (op=C_AND,opl=5,len=1,*p=='&') ||
  1256. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1257. (op=C_OR,opl=3,len=1,*p=='|') ||
  1258. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1259. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1260. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1261. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1262. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1263. (op=C_GT,opl=7,len=1,*p=='>') ||
  1264. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1265. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1266. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1267. p+=len;
  1268. if(op == C_OP3) {
  1269. p=parse_subexpr(p,-1);
  1270. p=skip_space(p);
  1271. if( *(p++) != ':')
  1272. disp_error_message("parse_subexpr: expected ':'", p-1);
  1273. p=parse_subexpr(p,-1);
  1274. } else {
  1275. p=parse_subexpr(p,opl);
  1276. }
  1277. add_scriptc(op);
  1278. p=skip_space(p);
  1279. }
  1280. return p; /* return first untreated operator */
  1281. }
  1282. /*==========================================
  1283. * Evaluation of the expression
  1284. *------------------------------------------*/
  1285. const char* parse_expr(const char *p)
  1286. {
  1287. switch(*p){
  1288. case ')': case ';': case ':': case '[': case ']':
  1289. case '}':
  1290. disp_error_message("parse_expr: unexpected character",p);
  1291. }
  1292. p=parse_subexpr(p,-1);
  1293. return p;
  1294. }
  1295. /*==========================================
  1296. * Analysis of the line
  1297. *------------------------------------------*/
  1298. const char* parse_line(const char* p)
  1299. {
  1300. const char* p2;
  1301. p=skip_space(p);
  1302. if(*p==';') {
  1303. //Close decision for if(); for(); while();
  1304. p = parse_syntax_close(p + 1);
  1305. return p;
  1306. }
  1307. if(*p==')' && parse_syntax_for_flag)
  1308. return p+1;
  1309. p = skip_space(p);
  1310. if(p[0] == '{') {
  1311. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1312. syntax.curly[syntax.curly_count].count = -1;
  1313. syntax.curly[syntax.curly_count].index = -1;
  1314. syntax.curly_count++;
  1315. return p + 1;
  1316. } else if(p[0] == '}') {
  1317. return parse_curly_close(p);
  1318. }
  1319. // Syntax-related processing
  1320. p2 = parse_syntax(p);
  1321. if(p2 != NULL)
  1322. return p2;
  1323. // attempt to process a variable assignment
  1324. p2 = parse_variable(p);
  1325. if( p2 != NULL )
  1326. {// variable assignment processed so leave the method
  1327. return parse_syntax_close(p2 + 1);
  1328. }
  1329. p = parse_callfunc(p,0,0);
  1330. p = skip_space(p);
  1331. if(parse_syntax_for_flag) {
  1332. if( *p != ')' )
  1333. disp_error_message("parse_line: expected ')'",p);
  1334. } else {
  1335. if( *p != ';' )
  1336. disp_error_message("parse_line: expected ';'",p);
  1337. }
  1338. //Binding decision for if(), for(), while()
  1339. p = parse_syntax_close(p+1);
  1340. return p;
  1341. }
  1342. // { ... } Closing process
  1343. const char* parse_curly_close(const char* p)
  1344. {
  1345. if(syntax.curly_count <= 0) {
  1346. disp_error_message("parse_curly_close: unexpected string",p);
  1347. return p + 1;
  1348. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1349. syntax.curly_count--;
  1350. //Close decision if, for , while
  1351. p = parse_syntax_close(p + 1);
  1352. return p;
  1353. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1354. //Closing switch()
  1355. int pos = syntax.curly_count-1;
  1356. char label[256];
  1357. int l;
  1358. // Remove temporary variables
  1359. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1360. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1361. parse_line(label);
  1362. syntax.curly_count--;
  1363. // Go to the end pointer unconditionally
  1364. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1365. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1366. parse_line(label);
  1367. syntax.curly_count--;
  1368. // You are here labeled
  1369. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1370. l=add_str(label);
  1371. set_label(l,script_pos, p);
  1372. if(syntax.curly[pos].flag) {
  1373. //Exists default
  1374. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1375. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1376. parse_line(label);
  1377. syntax.curly_count--;
  1378. }
  1379. // Label end
  1380. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1381. l=add_str(label);
  1382. set_label(l,script_pos, p);
  1383. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1384. syntax.curly_count--;
  1385. //Closing decision if, for , while
  1386. p = parse_syntax_close(p + 1);
  1387. return p;
  1388. } else {
  1389. disp_error_message("parse_curly_close: unexpected string",p);
  1390. return p + 1;
  1391. }
  1392. }
  1393. // Syntax-related processing
  1394. // break, case, continue, default, do, for, function,
  1395. // if, switch, while ? will handle this internally.
  1396. const char* parse_syntax(const char* p)
  1397. {
  1398. const char *p2 = skip_word(p);
  1399. switch(*p) {
  1400. case 'B':
  1401. case 'b':
  1402. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1403. // break Processing
  1404. char label[256];
  1405. int pos = syntax.curly_count - 1;
  1406. while(pos >= 0) {
  1407. if(syntax.curly[pos].type == TYPE_DO) {
  1408. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1409. break;
  1410. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1411. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1412. break;
  1413. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1414. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1415. break;
  1416. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1417. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1418. break;
  1419. }
  1420. pos--;
  1421. }
  1422. if(pos < 0) {
  1423. disp_error_message("parse_syntax: unexpected 'break'",p);
  1424. } else {
  1425. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1426. parse_line(label);
  1427. syntax.curly_count--;
  1428. }
  1429. p = skip_space(p2);
  1430. if(*p != ';')
  1431. disp_error_message("parse_syntax: expected ';'",p);
  1432. // Closing decision if, for , while
  1433. p = parse_syntax_close(p + 1);
  1434. return p;
  1435. }
  1436. break;
  1437. case 'c':
  1438. case 'C':
  1439. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1440. //Processing case
  1441. int pos = syntax.curly_count-1;
  1442. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1443. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1444. return p+1;
  1445. } else {
  1446. char label[256];
  1447. int l,v;
  1448. char *np;
  1449. if(syntax.curly[pos].count != 1) {
  1450. //Jump for FALLTHRU
  1451. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1452. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1453. parse_line(label);
  1454. syntax.curly_count--;
  1455. // You are here labeled
  1456. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1457. l=add_str(label);
  1458. set_label(l,script_pos, p);
  1459. }
  1460. //Decision statement switch
  1461. p = skip_space(p2);
  1462. if(p == p2) {
  1463. disp_error_message("parse_syntax: expected a space ' '",p);
  1464. }
  1465. // check whether case label is integer or not
  1466. if(is_number(p)) {
  1467. //Numeric value
  1468. v = (int)strtol(p,&np,0);
  1469. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1470. p++;
  1471. p = skip_word(p);
  1472. if(np != p)
  1473. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1474. } else {
  1475. //Check for constants
  1476. p2 = skip_word(p);
  1477. v = (int)(size_t) (p2-p); // length of word at p2
  1478. memcpy(label,p,v);
  1479. label[v]='\0';
  1480. if( !script_get_constant(label, &v) )
  1481. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1482. p = skip_word(p);
  1483. }
  1484. p = skip_space(p);
  1485. if(*p != ':')
  1486. disp_error_message("parse_syntax: expect ':'",p);
  1487. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1488. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1489. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1490. // Bad I do not parse twice
  1491. p2 = parse_line(label);
  1492. parse_line(p2);
  1493. syntax.curly_count--;
  1494. if(syntax.curly[pos].count != 1) {
  1495. // Label after the completion of FALLTHRU
  1496. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1497. l=add_str(label);
  1498. set_label(l,script_pos,p);
  1499. }
  1500. // check duplication of case label [Rayce]
  1501. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1502. disp_error_message("parse_syntax: dup 'case'",p);
  1503. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1504. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1505. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1506. parse_line(label);
  1507. syntax.curly_count--;
  1508. syntax.curly[pos].count++;
  1509. }
  1510. return p + 1;
  1511. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1512. // Processing continue
  1513. char label[256];
  1514. int pos = syntax.curly_count - 1;
  1515. while(pos >= 0) {
  1516. if(syntax.curly[pos].type == TYPE_DO) {
  1517. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1518. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1519. break;
  1520. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1521. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1522. break;
  1523. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1524. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1525. break;
  1526. }
  1527. pos--;
  1528. }
  1529. if(pos < 0) {
  1530. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1531. } else {
  1532. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1533. parse_line(label);
  1534. syntax.curly_count--;
  1535. }
  1536. p = skip_space(p2);
  1537. if(*p != ';')
  1538. disp_error_message("parse_syntax: expected ';'",p);
  1539. //Closing decision if, for , while
  1540. p = parse_syntax_close(p + 1);
  1541. return p;
  1542. }
  1543. break;
  1544. case 'd':
  1545. case 'D':
  1546. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1547. // Switch - default processing
  1548. int pos = syntax.curly_count-1;
  1549. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1550. disp_error_message("parse_syntax: unexpected 'default'",p);
  1551. } else if(syntax.curly[pos].flag) {
  1552. disp_error_message("parse_syntax: dup 'default'",p);
  1553. } else {
  1554. char label[256];
  1555. int l;
  1556. // Put the label location
  1557. p = skip_space(p2);
  1558. if(*p != ':') {
  1559. disp_error_message("parse_syntax: expected ':'",p);
  1560. }
  1561. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1562. l=add_str(label);
  1563. set_label(l,script_pos,p);
  1564. // Skip to the next link w/o condition
  1565. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1566. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1567. parse_line(label);
  1568. syntax.curly_count--;
  1569. // The default label
  1570. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1571. l=add_str(label);
  1572. set_label(l,script_pos,p);
  1573. syntax.curly[syntax.curly_count - 1].flag = 1;
  1574. syntax.curly[pos].count++;
  1575. }
  1576. return p + 1;
  1577. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1578. int l;
  1579. char label[256];
  1580. p=skip_space(p2);
  1581. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1582. syntax.curly[syntax.curly_count].count = 1;
  1583. syntax.curly[syntax.curly_count].index = syntax.index++;
  1584. syntax.curly[syntax.curly_count].flag = 0;
  1585. // Label of the (do) form here
  1586. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1587. l=add_str(label);
  1588. set_label(l,script_pos,p);
  1589. syntax.curly_count++;
  1590. return p;
  1591. }
  1592. break;
  1593. case 'f':
  1594. case 'F':
  1595. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1596. int l;
  1597. char label[256];
  1598. int pos = syntax.curly_count;
  1599. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1600. syntax.curly[syntax.curly_count].count = 1;
  1601. syntax.curly[syntax.curly_count].index = syntax.index++;
  1602. syntax.curly[syntax.curly_count].flag = 0;
  1603. syntax.curly_count++;
  1604. p=skip_space(p2);
  1605. if(*p != '(')
  1606. disp_error_message("parse_syntax: expected '('",p);
  1607. p++;
  1608. // Execute the initialization statement
  1609. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1610. p=parse_line(p);
  1611. syntax.curly_count--;
  1612. // Form the start of label decision
  1613. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1614. l=add_str(label);
  1615. set_label(l,script_pos,p);
  1616. p=skip_space(p);
  1617. if(*p == ';') {
  1618. // For (; Because the pattern of always true ;)
  1619. ;
  1620. } else {
  1621. // Skip to the end point if the condition is false
  1622. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1623. add_scriptl(add_str("jump_zero"));
  1624. add_scriptc(C_ARG);
  1625. p=parse_expr(p);
  1626. p=skip_space(p);
  1627. add_scriptl(add_str(label));
  1628. add_scriptc(C_FUNC);
  1629. }
  1630. if(*p != ';')
  1631. disp_error_message("parse_syntax: expected ';'",p);
  1632. p++;
  1633. // Skip to the beginning of the loop
  1634. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1635. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1636. parse_line(label);
  1637. syntax.curly_count--;
  1638. // Labels to form the next loop
  1639. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1640. l=add_str(label);
  1641. set_label(l,script_pos,p);
  1642. // Process the next time you enter the loop
  1643. // A ')' last for; flag to be treated as'
  1644. parse_syntax_for_flag = 1;
  1645. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1646. p=parse_line(p);
  1647. syntax.curly_count--;
  1648. parse_syntax_for_flag = 0;
  1649. // Skip to the determination process conditions
  1650. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1651. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1652. parse_line(label);
  1653. syntax.curly_count--;
  1654. // Loop start labeling
  1655. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1656. l=add_str(label);
  1657. set_label(l,script_pos,p);
  1658. return p;
  1659. }
  1660. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1661. {// internal script function
  1662. const char *func_name;
  1663. func_name = skip_space(p2);
  1664. p = skip_word(func_name);
  1665. if( p == func_name )
  1666. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1667. p2 = skip_space(p);
  1668. if( *p2 == ';' )
  1669. {// function <name> ;
  1670. // function declaration - just register the name
  1671. int l;
  1672. l = add_word(func_name);
  1673. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1674. str_data[l].type = C_USERFUNC;
  1675. else if( str_data[l].type == C_USERFUNC )
  1676. ; // already registered
  1677. else
  1678. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1679. // Close condition of if, for, while
  1680. p = parse_syntax_close(p2 + 1);
  1681. return p;
  1682. }
  1683. else if(*p2 == '{')
  1684. {// function <name> <line/block of code>
  1685. char label[256];
  1686. int l;
  1687. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1688. syntax.curly[syntax.curly_count].count = 1;
  1689. syntax.curly[syntax.curly_count].index = syntax.index++;
  1690. syntax.curly[syntax.curly_count].flag = 0;
  1691. ++syntax.curly_count;
  1692. // Jump over the function code
  1693. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1694. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1695. ++syntax.curly_count;
  1696. parse_line(label);
  1697. --syntax.curly_count;
  1698. // Set the position of the function (label)
  1699. l=add_word(func_name);
  1700. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1701. {
  1702. str_data[l].type = C_USERFUNC;
  1703. set_label(l, script_pos, p);
  1704. if( parse_options&SCRIPT_USE_LABEL_DB )
  1705. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1706. }
  1707. else
  1708. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1709. return skip_space(p);
  1710. }
  1711. else
  1712. {
  1713. disp_error_message("expect ';' or '{' at function syntax",p);
  1714. }
  1715. }
  1716. break;
  1717. case 'i':
  1718. case 'I':
  1719. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1720. // If process
  1721. char label[256];
  1722. p=skip_space(p2);
  1723. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1724. disp_error_message("need '('",p);
  1725. }
  1726. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1727. syntax.curly[syntax.curly_count].count = 1;
  1728. syntax.curly[syntax.curly_count].index = syntax.index++;
  1729. syntax.curly[syntax.curly_count].flag = 0;
  1730. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1731. syntax.curly_count++;
  1732. add_scriptl(add_str("jump_zero"));
  1733. add_scriptc(C_ARG);
  1734. p=parse_expr(p);
  1735. p=skip_space(p);
  1736. add_scriptl(add_str(label));
  1737. add_scriptc(C_FUNC);
  1738. return p;
  1739. }
  1740. break;
  1741. case 's':
  1742. case 'S':
  1743. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1744. // Processing of switch ()
  1745. char label[256];
  1746. p=skip_space(p2);
  1747. if(*p != '(') {
  1748. disp_error_message("need '('",p);
  1749. }
  1750. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1751. syntax.curly[syntax.curly_count].count = 1;
  1752. syntax.curly[syntax.curly_count].index = syntax.index++;
  1753. syntax.curly[syntax.curly_count].flag = 0;
  1754. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1755. syntax.curly_count++;
  1756. add_scriptl(add_str("set"));
  1757. add_scriptc(C_ARG);
  1758. add_scriptl(add_str(label));
  1759. p=parse_expr(p);
  1760. p=skip_space(p);
  1761. if(*p != '{') {
  1762. disp_error_message("parse_syntax: expected '{'",p);
  1763. }
  1764. add_scriptc(C_FUNC);
  1765. return p + 1;
  1766. }
  1767. break;
  1768. case 'w':
  1769. case 'W':
  1770. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1771. int l;
  1772. char label[256];
  1773. p=skip_space(p2);
  1774. if(*p != '(') {
  1775. disp_error_message("need '('",p);
  1776. }
  1777. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1778. syntax.curly[syntax.curly_count].count = 1;
  1779. syntax.curly[syntax.curly_count].index = syntax.index++;
  1780. syntax.curly[syntax.curly_count].flag = 0;
  1781. // Form the start of label decision
  1782. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1783. l=add_str(label);
  1784. set_label(l,script_pos,p);
  1785. // Skip to the end point if the condition is false
  1786. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1787. syntax.curly_count++;
  1788. add_scriptl(add_str("jump_zero"));
  1789. add_scriptc(C_ARG);
  1790. p=parse_expr(p);
  1791. p=skip_space(p);
  1792. add_scriptl(add_str(label));
  1793. add_scriptc(C_FUNC);
  1794. return p;
  1795. }
  1796. break;
  1797. }
  1798. return NULL;
  1799. }
  1800. const char* parse_syntax_close(const char *p) {
  1801. // If (...) for (...) hoge (); as to make sure closed closed once again
  1802. int flag;
  1803. do {
  1804. p = parse_syntax_close_sub(p,&flag);
  1805. } while(flag);
  1806. return p;
  1807. }
  1808. // Close judgment if, for, while, of do
  1809. // flag == 1 : closed
  1810. // flag == 0 : not closed
  1811. const char* parse_syntax_close_sub(const char* p,int* flag)
  1812. {
  1813. char label[256];
  1814. int pos = syntax.curly_count - 1;
  1815. int l;
  1816. *flag = 1;
  1817. if(syntax.curly_count <= 0) {
  1818. *flag = 0;
  1819. return p;
  1820. } else if(syntax.curly[pos].type == TYPE_IF) {
  1821. const char *bp = p;
  1822. const char *p2;
  1823. // if-block and else-block end is a new line
  1824. parse_nextline(false, p);
  1825. // Skip to the last location if
  1826. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1827. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1828. parse_line(label);
  1829. syntax.curly_count--;
  1830. // Put the label of the location
  1831. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1832. l=add_str(label);
  1833. set_label(l,script_pos,p);
  1834. syntax.curly[pos].count++;
  1835. p = skip_space(p);
  1836. p2 = skip_word(p);
  1837. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1838. // else or else - if
  1839. p = skip_space(p2);
  1840. p2 = skip_word(p);
  1841. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1842. // else - if
  1843. p=skip_space(p2);
  1844. if(*p != '(') {
  1845. disp_error_message("need '('",p);
  1846. }
  1847. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1848. add_scriptl(add_str("jump_zero"));
  1849. add_scriptc(C_ARG);
  1850. p=parse_expr(p);
  1851. p=skip_space(p);
  1852. add_scriptl(add_str(label));
  1853. add_scriptc(C_FUNC);
  1854. *flag = 0;
  1855. return p;
  1856. } else {
  1857. // else
  1858. if(!syntax.curly[pos].flag) {
  1859. syntax.curly[pos].flag = 1;
  1860. *flag = 0;
  1861. return p;
  1862. }
  1863. }
  1864. }
  1865. // Close if
  1866. syntax.curly_count--;
  1867. // Put the label of the final location
  1868. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1869. l=add_str(label);
  1870. set_label(l,script_pos,p);
  1871. if(syntax.curly[pos].flag == 1) {
  1872. // Because the position of the pointer is the same if not else for this
  1873. return bp;
  1874. }
  1875. return p;
  1876. } else if(syntax.curly[pos].type == TYPE_DO) {
  1877. int l2;
  1878. char label2[256];
  1879. const char *p2;
  1880. if(syntax.curly[pos].flag) {
  1881. // (Come here continue) to form the label here
  1882. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1883. l2=add_str(label2);
  1884. set_label(l2,script_pos,p);
  1885. }
  1886. // Skip to the end point if the condition is false
  1887. p = skip_space(p);
  1888. p2 = skip_word(p);
  1889. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1890. disp_error_message("parse_syntax: expected 'while'",p);
  1891. p = skip_space(p2);
  1892. if(*p != '(') {
  1893. disp_error_message("need '('",p);
  1894. }
  1895. // do-block end is a new line
  1896. parse_nextline(false, p);
  1897. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1898. add_scriptl(add_str("jump_zero"));
  1899. add_scriptc(C_ARG);
  1900. p=parse_expr(p);
  1901. p=skip_space(p);
  1902. add_scriptl(add_str(label2));
  1903. add_scriptc(C_FUNC);
  1904. // Skip to the starting point
  1905. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1906. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1907. parse_line(label2);
  1908. syntax.curly_count--;
  1909. // Form label of the end point conditions
  1910. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1911. l2=add_str(label2);
  1912. set_label(l2,script_pos,p);
  1913. p = skip_space(p);
  1914. if(*p != ';') {
  1915. disp_error_message("parse_syntax: expected ';'",p);
  1916. return p+1;
  1917. }
  1918. p++;
  1919. syntax.curly_count--;
  1920. return p;
  1921. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1922. // for-block end is a new line
  1923. parse_nextline(false, p);
  1924. // Skip to the next loop
  1925. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1926. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1927. parse_line(label);
  1928. syntax.curly_count--;
  1929. // End for labeling
  1930. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1931. l=add_str(label);
  1932. set_label(l,script_pos,p);
  1933. syntax.curly_count--;
  1934. return p;
  1935. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1936. // while-block end is a new line
  1937. parse_nextline(false, p);
  1938. // Skip to the decision while
  1939. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1940. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1941. parse_line(label);
  1942. syntax.curly_count--;
  1943. // End while labeling
  1944. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1945. l=add_str(label);
  1946. set_label(l,script_pos,p);
  1947. syntax.curly_count--;
  1948. return p;
  1949. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1950. int pos2 = syntax.curly_count-1;
  1951. char label2[256];
  1952. int l2;
  1953. // Back
  1954. sprintf(label2,"return;");
  1955. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1956. parse_line(label2);
  1957. syntax.curly_count--;
  1958. // Put the label of the location
  1959. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1960. l2=add_str(label2);
  1961. set_label(l2,script_pos,p);
  1962. syntax.curly_count--;
  1963. return p;
  1964. } else {
  1965. *flag = 0;
  1966. return p;
  1967. }
  1968. }
  1969. /*==========================================
  1970. * Added built-in functions
  1971. *------------------------------------------*/
  1972. static void add_buildin_func(void)
  1973. {
  1974. int i;
  1975. for( i = 0; buildin_func[i].func; i++ )
  1976. {
  1977. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1978. // 'v' - value (either string or int or reference)
  1979. // 's' - string
  1980. // 'i' - int
  1981. // 'r' - reference (of a variable)
  1982. // 'l' - label
  1983. // '?' - one optional parameter
  1984. // '*' - unknown number of optional parameters
  1985. const char* p = buildin_func[i].arg;
  1986. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1987. while( *p == '?' ) ++p;
  1988. if( *p == '*' ) ++p;
  1989. if( *p != 0){
  1990. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1991. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1992. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1993. } else {
  1994. int n = add_str(buildin_func[i].name);
  1995. str_data[n].type = C_FUNC;
  1996. str_data[n].val = i;
  1997. str_data[n].func = buildin_func[i].func;
  1998. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  1999. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2000. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2001. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2002. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2003. }
  2004. }
  2005. }
  2006. /**
  2007. * String comparison with a char array to a script constant
  2008. * @param prefix: Char array to compare
  2009. * @param value: Script constant
  2010. * @return Constant name as char array or NULL otherwise
  2011. */
  2012. const char* script_get_constant_str( const char* prefix, int64 value ){
  2013. const char* name;
  2014. for(int i = 0; i < str_data_size; i++ ){
  2015. // Check if it is a constant
  2016. if( str_data[i].type != C_INT ){
  2017. continue;
  2018. }
  2019. // Check if the value matches
  2020. if( str_data[i].val != value ){
  2021. continue;
  2022. }
  2023. // Look up the actual string
  2024. name = get_str(i);
  2025. // Compare the prefix
  2026. if( !strncasecmp( name, prefix, strlen(prefix) ) ){
  2027. // We found a match
  2028. return name;
  2029. }
  2030. }
  2031. return NULL;
  2032. }
  2033. /// Retrieves the value of a constant parameter.
  2034. bool script_get_parameter(const char* name, int* value)
  2035. {
  2036. int n = search_str(name);
  2037. if (n == -1 || str_data[n].type != C_PARAM)
  2038. {// not found or not a parameter
  2039. return false;
  2040. }
  2041. value[0] = str_data[n].val;
  2042. return true;
  2043. }
  2044. /// Retrieves the value of a constant.
  2045. bool script_get_constant(const char* name, int* value)
  2046. {
  2047. int n = search_str(name);
  2048. if( n == -1 || str_data[n].type != C_INT )
  2049. {// not found or not a constant
  2050. return false;
  2051. }
  2052. value[0] = str_data[n].val;
  2053. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2054. if( str_data[n].deprecated ){
  2055. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2056. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2057. }
  2058. #endif
  2059. return true;
  2060. }
  2061. /// Creates new constant or parameter with given value.
  2062. void script_set_constant(const char* name, int value, bool isparameter, bool deprecated)
  2063. {
  2064. int n = add_str(name);
  2065. if( str_data[n].type == C_NOP )
  2066. {// new
  2067. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2068. str_data[n].val = value;
  2069. str_data[n].deprecated = deprecated;
  2070. }
  2071. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2072. {// existing parameter or constant
  2073. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  2074. }
  2075. else
  2076. {// existing name
  2077. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2078. }
  2079. }
  2080. static bool read_constdb_sub( char* fields[], int columns, int current ){
  2081. char name[1024], val[1024];
  2082. int type = 0;
  2083. if( columns > 1 ){
  2084. if( sscanf(fields[0], "%1023[A-Za-z0-9/_]", name) != 1 ||
  2085. sscanf(fields[1], "%1023[A-Za-z0-9/_]", val) != 1 ||
  2086. ( columns >= 2 && sscanf(fields[2], "%11d", &type) != 1 ) ){
  2087. ShowWarning("Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current);
  2088. return false;
  2089. }
  2090. }else{
  2091. if( sscanf(fields[0], "%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d", name, val, &type) < 2 ){
  2092. ShowWarning( "Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current );
  2093. return false;
  2094. }
  2095. }
  2096. script_set_constant(name, (int)strtol(val, NULL, 0), (type != 0), false);
  2097. return true;
  2098. }
  2099. /*==========================================
  2100. * Reading constant databases
  2101. * const.txt
  2102. *------------------------------------------*/
  2103. static void read_constdb(void){
  2104. const char* dbsubpath[] = {
  2105. "",
  2106. "/" DBIMPORT,
  2107. };
  2108. for( int i = 0; i < ARRAYLENGTH(dbsubpath); i++ ){
  2109. int n2 = strlen(db_path) + strlen(dbsubpath[i]) + 1;
  2110. char* dbsubpath2 = (char*)aMalloc(n2 + 1);
  2111. bool silent = i > 0;
  2112. safesnprintf(dbsubpath2, n2, "%s%s", db_path, dbsubpath[i]);
  2113. sv_readdb(dbsubpath2, "const.txt", ',', 1, 3, -1, &read_constdb_sub, silent);
  2114. aFree(dbsubpath2);
  2115. }
  2116. }
  2117. /**
  2118. * Sets source-end constants for NPC scripts to access.
  2119. **/
  2120. void script_hardcoded_constants(void) {
  2121. #include "script_constants.hpp"
  2122. }
  2123. /*==========================================
  2124. * Display emplacement line of script
  2125. *------------------------------------------*/
  2126. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2127. {
  2128. int i;
  2129. if( p == NULL || !p[0] ) return NULL;
  2130. if( line < 0 )
  2131. StringBuf_Printf(buf, "*% 5d : ", -line);
  2132. else
  2133. StringBuf_Printf(buf, " % 5d : ", line);
  2134. for(i=0;p[i] && p[i] != '\n';i++){
  2135. if(p + i != mark)
  2136. StringBuf_Printf(buf, "%c", p[i]);
  2137. else
  2138. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2139. }
  2140. StringBuf_AppendStr(buf, "\n");
  2141. return p+i+(p[i] == '\n' ? 1 : 0);
  2142. }
  2143. 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) {
  2144. // Find the line where the error occurred
  2145. int j;
  2146. int line = start_line;
  2147. const char *p;
  2148. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2149. for(p=src;p && *p;line++){
  2150. const char *lineend=strchr(p,'\n');
  2151. if(lineend==NULL || error_pos_cur<lineend){
  2152. break;
  2153. }
  2154. for( j = 0; j < 4; j++ ) {
  2155. linestart[j] = linestart[j+1];
  2156. }
  2157. linestart[4] = p;
  2158. p=lineend+1;
  2159. }
  2160. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2161. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2162. for(j = 0; j < 5; j++ ) {
  2163. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2164. }
  2165. p = script_print_line(buf, p, error_pos_cur, -line);
  2166. for(j = 0; j < 5; j++) {
  2167. p = script_print_line(buf, p, NULL, line + j + 1 );
  2168. }
  2169. }
  2170. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2171. StringBuf buf;
  2172. StringBuf_Init(&buf);
  2173. StringBuf_AppendStr(&buf, "\a\n");
  2174. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2175. ShowError("%s", StringBuf_Value(&buf));
  2176. StringBuf_Destroy(&buf);
  2177. }
  2178. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2179. StringBuf buf;
  2180. StringBuf_Init(&buf);
  2181. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2182. ShowWarning("%s", StringBuf_Value(&buf));
  2183. StringBuf_Destroy(&buf);
  2184. }
  2185. /*==========================================
  2186. * Analysis of the script
  2187. *------------------------------------------*/
  2188. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2189. {
  2190. const char *p,*tmpp;
  2191. int i;
  2192. struct script_code* code = NULL;
  2193. static bool first=true;
  2194. char end;
  2195. bool unresolved_names = false;
  2196. parser_current_src = src;
  2197. parser_current_file = file;
  2198. parser_current_line = line;
  2199. if( src == NULL )
  2200. return NULL;// empty script
  2201. memset(&syntax,0,sizeof(syntax));
  2202. if(first){
  2203. add_buildin_func();
  2204. read_constdb();
  2205. script_hardcoded_constants();
  2206. first=false;
  2207. }
  2208. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2209. script_pos=0;
  2210. script_size=SCRIPT_BLOCK_SIZE;
  2211. parse_nextline(true, NULL);
  2212. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2213. if( options&SCRIPT_USE_LABEL_DB )
  2214. db_clear(scriptlabel_db);
  2215. parse_options = options;
  2216. if( setjmp( error_jump ) != 0 ) {
  2217. //Restore program state when script has problems. [from jA]
  2218. int j;
  2219. const int size = ARRAYLENGTH(syntax.curly);
  2220. if( error_report )
  2221. script_error(src,file,line,error_msg,error_pos);
  2222. aFree( error_msg );
  2223. aFree( script_buf );
  2224. script_pos = 0;
  2225. script_size = 0;
  2226. script_buf = NULL;
  2227. for(j=LABEL_START;j<str_num;j++)
  2228. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2229. for(j=0; j<size; j++)
  2230. linkdb_final(&syntax.curly[j].case_label);
  2231. return NULL;
  2232. }
  2233. parse_syntax_for_flag=0;
  2234. p=src;
  2235. p=skip_space(p);
  2236. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2237. {// does not require brackets around the script
  2238. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2239. {// empty script and can return NULL
  2240. aFree( script_buf );
  2241. script_pos = 0;
  2242. script_size = 0;
  2243. script_buf = NULL;
  2244. return NULL;
  2245. }
  2246. end = '\0';
  2247. }
  2248. else
  2249. {// requires brackets around the script
  2250. if( *p != '{' )
  2251. disp_error_message("not found '{'",p);
  2252. p = skip_space(p+1);
  2253. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2254. {// empty script and can return NULL
  2255. aFree( script_buf );
  2256. script_pos = 0;
  2257. script_size = 0;
  2258. script_buf = NULL;
  2259. return NULL;
  2260. }
  2261. end = '}';
  2262. }
  2263. // clear references of labels, variables and internal functions
  2264. for(i=LABEL_START;i<str_num;i++){
  2265. if(
  2266. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2267. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2268. ){
  2269. str_data[i].type=C_NOP;
  2270. str_data[i].backpatch=-1;
  2271. str_data[i].label=-1;
  2272. }
  2273. }
  2274. while( syntax.curly_count != 0 || *p != end )
  2275. {
  2276. if( *p == '\0' )
  2277. disp_error_message("unexpected end of script",p);
  2278. // Special handling only label
  2279. tmpp=skip_space(skip_word(p));
  2280. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2281. i=add_word(p);
  2282. set_label(i,script_pos,p);
  2283. if( parse_options&SCRIPT_USE_LABEL_DB )
  2284. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2285. p=tmpp+1;
  2286. p=skip_space(p);
  2287. continue;
  2288. }
  2289. // All other lumped
  2290. p=parse_line(p);
  2291. p=skip_space(p);
  2292. parse_nextline(false, p);
  2293. }
  2294. add_scriptc(C_NOP);
  2295. // trim code to size
  2296. script_size = script_pos;
  2297. RECREATE(script_buf,unsigned char,script_pos);
  2298. // default unknown references to variables
  2299. for(i=LABEL_START;i<str_num;i++){
  2300. if(str_data[i].type==C_NOP){
  2301. int j;
  2302. str_data[i].type=C_NAME;
  2303. str_data[i].label=i;
  2304. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2305. int next=GETVALUE(script_buf,j);
  2306. SETVALUE(script_buf,j,i);
  2307. j=next;
  2308. }
  2309. }
  2310. else if( str_data[i].type == C_USERFUNC )
  2311. {// 'function name;' without follow-up code
  2312. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2313. unresolved_names = true;
  2314. }
  2315. }
  2316. if( unresolved_names )
  2317. {
  2318. disp_error_message("parse_script: unresolved function references", p);
  2319. }
  2320. #ifdef DEBUG_DISP
  2321. for(i=0;i<script_pos;i++){
  2322. if((i&15)==0) ShowMessage("%04x : ",i);
  2323. ShowMessage("%02x ",script_buf[i]);
  2324. if((i&15)==15) ShowMessage("\n");
  2325. }
  2326. ShowMessage("\n");
  2327. #endif
  2328. #ifdef DEBUG_DISASM
  2329. {
  2330. int i = 0,j;
  2331. while(i < script_pos) {
  2332. c_op op = get_com(script_buf,&i);
  2333. ShowMessage("%06x %s", i, script_op2name(op));
  2334. j = i;
  2335. switch(op) {
  2336. case C_INT:
  2337. ShowMessage(" %d", get_num(script_buf,&i));
  2338. break;
  2339. case C_POS:
  2340. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2341. i += 3;
  2342. break;
  2343. case C_NAME:
  2344. j = (*(int*)(script_buf+i)&0xffffff);
  2345. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2346. i += 3;
  2347. break;
  2348. case C_STR:
  2349. j = strlen(script_buf + i);
  2350. ShowMessage(" %s", script_buf + i);
  2351. i += j+1;
  2352. break;
  2353. }
  2354. ShowMessage(CL_CLL"\n");
  2355. }
  2356. }
  2357. #endif
  2358. CREATE(code,struct script_code,1);
  2359. code->script_buf = script_buf;
  2360. code->script_size = script_size;
  2361. code->local.vars = NULL;
  2362. code->local.arrays = NULL;
  2363. return code;
  2364. }
  2365. /// Returns the player attached to this script, identified by the rid.
  2366. /// If there is no player attached, the script is terminated.
  2367. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){
  2368. *sd = map_id2sd( st->rid );
  2369. if( *sd ){
  2370. return true;
  2371. }else{
  2372. ShowError("%s: fatal error ! player not attached!\n",func);
  2373. script_reportfunc(st);
  2374. script_reportsrc(st);
  2375. st->state = END;
  2376. return false;
  2377. }
  2378. }
  2379. /**
  2380. * Dereferences a variable/constant, replacing it with a copy of the value.
  2381. * @param st Script state
  2382. * @param data Variable/constant
  2383. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2384. */
  2385. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2386. {
  2387. const char* name;
  2388. char prefix;
  2389. char postfix;
  2390. if( !data_isreference(data) )
  2391. return;// not a variable/constant
  2392. name = reference_getname(data);
  2393. prefix = name[0];
  2394. postfix = name[strlen(name) - 1];
  2395. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2396. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2397. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2398. if( postfix == '$' ) {// string variable
  2399. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2400. data->type = C_CONSTSTR;
  2401. data->u.str = const_cast<char *>("");
  2402. } else {// integer variable
  2403. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2404. data->type = C_INT;
  2405. data->u.num = 0;
  2406. }
  2407. return;
  2408. }
  2409. }
  2410. if( postfix == '$' ) {// string variable
  2411. switch( prefix ) {
  2412. case '@':
  2413. data->u.str = pc_readregstr(sd, data->u.num);
  2414. break;
  2415. case '$':
  2416. data->u.str = mapreg_readregstr(data->u.num);
  2417. break;
  2418. case '#':
  2419. if( name[1] == '#' )
  2420. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2421. else
  2422. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2423. break;
  2424. case '.':
  2425. {
  2426. struct DBMap* n = data->ref ?
  2427. data->ref->vars : name[1] == '@' ?
  2428. st->stack->scope.vars : // instance/scope variable
  2429. st->script->local.vars; // npc variable
  2430. if( n )
  2431. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2432. else
  2433. data->u.str = NULL;
  2434. }
  2435. break;
  2436. case '\'':
  2437. {
  2438. unsigned short instance_id = script_instancegetid(st);
  2439. if( instance_id )
  2440. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2441. else {
  2442. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2443. data->u.str = NULL;
  2444. }
  2445. }
  2446. break;
  2447. default:
  2448. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2449. break;
  2450. }
  2451. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2452. data->type = C_CONSTSTR;
  2453. data->u.str = const_cast<char *>("");
  2454. } else {// duplicate string
  2455. data->type = C_STR;
  2456. data->u.str = aStrdup(data->u.str);
  2457. }
  2458. } else {// integer variable
  2459. data->type = C_INT;
  2460. if( reference_toconstant(data) ) {
  2461. data->u.num = reference_getconstant(data);
  2462. } else if( reference_toparam(data) ) {
  2463. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2464. } else
  2465. switch( prefix ) {
  2466. case '@':
  2467. data->u.num = pc_readreg(sd, data->u.num);
  2468. break;
  2469. case '$':
  2470. data->u.num = mapreg_readreg(data->u.num);
  2471. break;
  2472. case '#':
  2473. if( name[1] == '#' )
  2474. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2475. else
  2476. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2477. break;
  2478. case '.':
  2479. {
  2480. struct DBMap* n = data->ref ?
  2481. data->ref->vars : name[1] == '@' ?
  2482. st->stack->scope.vars : // instance/scope variable
  2483. st->script->local.vars; // npc variable
  2484. if( n )
  2485. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2486. else
  2487. data->u.num = 0;
  2488. }
  2489. break;
  2490. case '\'':
  2491. {
  2492. unsigned short instance_id = script_instancegetid(st);
  2493. if( instance_id )
  2494. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2495. else {
  2496. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2497. data->u.num = 0;
  2498. }
  2499. }
  2500. break;
  2501. default:
  2502. data->u.num = pc_readglobalreg(sd, data->u.num);
  2503. break;
  2504. }
  2505. }
  2506. data->ref = NULL;
  2507. return;
  2508. }
  2509. void get_val(struct script_state* st, struct script_data* data)
  2510. {
  2511. get_val_(st,data,NULL);
  2512. }
  2513. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2514. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2515. /// The value is left in the top of the stack and needs to be removed manually.
  2516. /// @param st[in]: script state.
  2517. /// @param uid[in]: reference identifier.
  2518. /// @param ref[in]: the container to look up the reference into.
  2519. /// @return: the retrieved value of the reference.
  2520. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2521. {
  2522. struct script_data* data;
  2523. push_val2(st->stack, C_NAME, uid, ref);
  2524. data = script_getdatatop(st, -1);
  2525. get_val(st, data);
  2526. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2527. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2528. }
  2529. /**
  2530. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2531. * Will be gone as soon as undefined var feature is implemented
  2532. **/
  2533. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2534. {
  2535. const char *name = get_str(script_getvarid(uid));
  2536. // is here st can be null pointer and st->rid is wrong?
  2537. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2538. bool insert = false;
  2539. if (sd && !st) {
  2540. // when sd comes, st isn't available
  2541. insert = true;
  2542. } else {
  2543. if( is_string_variable(name) ) {
  2544. char* str = (char*)get_val2(st, uid, ref);
  2545. if( str && *str )
  2546. insert = true;
  2547. script_removetop(st, -1, 0);
  2548. } else {
  2549. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2550. if( num )
  2551. insert = true;
  2552. script_removetop(st, -1, 0);
  2553. }
  2554. }
  2555. if (src && src->arrays) {
  2556. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2557. if (sa) {
  2558. unsigned int i;
  2559. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2560. if( i != sa->size ) {
  2561. if( !insert )
  2562. script_array_remove_member(src,sa,i);
  2563. return;
  2564. }
  2565. script_array_add_member(sa,0);
  2566. } else if (insert) {
  2567. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2568. }
  2569. }
  2570. }
  2571. /**
  2572. * Returns array size by ID
  2573. **/
  2574. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2575. {
  2576. struct script_array *sa = NULL;
  2577. struct reg_db *src = script_array_src(st, sd, name, ref);
  2578. if (src && src->arrays)
  2579. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2580. return sa ? sa->size : 0;
  2581. }
  2582. /**
  2583. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2584. **/
  2585. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2586. {
  2587. struct script_array *sa = NULL;
  2588. struct reg_db *src = script_array_src(st, sd, name, ref);
  2589. if (src && src->arrays) {
  2590. int key = add_word(name);
  2591. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2592. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2593. unsigned int i, highest_key = 0;
  2594. for(i = 0; i < sa->size; i++) {
  2595. if( sa->members[i] > highest_key )
  2596. highest_key = sa->members[i];
  2597. }
  2598. return sa->size ? highest_key + 1 : 0;
  2599. }
  2600. }
  2601. return SCRIPT_CMD_SUCCESS;
  2602. }
  2603. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2604. {
  2605. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2606. aFree(sa->members);
  2607. ers_free(array_ers, sa);
  2608. return SCRIPT_CMD_SUCCESS;
  2609. }
  2610. /**
  2611. * Clears script_array and removes it from script->array_db
  2612. **/
  2613. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2614. {
  2615. aFree(sa->members);
  2616. idb_remove(src->arrays, sa->id);
  2617. ers_free(array_ers, sa);
  2618. }
  2619. /**
  2620. * Removes a member from a script_array list
  2621. *
  2622. * @param idx the index of the member in script_array struct list, not of the actual array member
  2623. **/
  2624. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2625. {
  2626. unsigned int i, cursor;
  2627. // it's the only member left, no need to do anything other than delete the array data
  2628. if( sa->size == 1 ) {
  2629. script_array_delete(src,sa);
  2630. return;
  2631. }
  2632. sa->members[idx] = UINT_MAX;
  2633. for(i = 0, cursor = 0; i < sa->size; i++) {
  2634. if( sa->members[i] == UINT_MAX )
  2635. continue;
  2636. if( i != cursor )
  2637. sa->members[cursor] = sa->members[i];
  2638. cursor++;
  2639. }
  2640. sa->size = cursor;
  2641. }
  2642. /**
  2643. * Appends a new array index to the list in script_array
  2644. *
  2645. * @param idx the index of the array member being inserted
  2646. **/
  2647. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2648. {
  2649. RECREATE(sa->members, unsigned int, ++sa->size);
  2650. sa->members[sa->size - 1] = idx;
  2651. }
  2652. /**
  2653. * Obtains the source of the array database for this type and scenario
  2654. * Initializes such database when not yet initialized.
  2655. **/
  2656. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2657. {
  2658. struct reg_db *src = NULL;
  2659. switch( name[0] ) {
  2660. // from player
  2661. default: // char reg
  2662. case '@': // temp char reg
  2663. case '#': // account reg
  2664. src = &sd->regs;
  2665. break;
  2666. case '$': // map reg
  2667. src = &regs;
  2668. break;
  2669. case '.': // npc/script
  2670. if (ref)
  2671. src = ref;
  2672. else
  2673. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2674. break;
  2675. case '\'': // instance
  2676. {
  2677. unsigned short instance_id = script_instancegetid(st);
  2678. if( instance_id ) {
  2679. src = &instance_data[instance_id].regs;
  2680. }
  2681. break;
  2682. }
  2683. }
  2684. if( src ) {
  2685. if( !src->arrays )
  2686. src->arrays = idb_alloc(DB_OPT_BASE);
  2687. return src;
  2688. }
  2689. return NULL;
  2690. }
  2691. /**
  2692. * Processes a array member modification, and update data accordingly
  2693. *
  2694. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2695. * @param num[in] Variable ID
  2696. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2697. **/
  2698. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2699. {
  2700. struct script_array *sa = NULL;
  2701. int id = script_getvarid(num);
  2702. unsigned int index = script_getvaridx(num);
  2703. if (!src->arrays) {
  2704. src->arrays = idb_alloc(DB_OPT_BASE);
  2705. } else {
  2706. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2707. }
  2708. if( sa ) {
  2709. unsigned int i;
  2710. // search
  2711. for(i = 0; i < sa->size; i++) {
  2712. if( sa->members[i] == index )
  2713. break;
  2714. }
  2715. // if existent
  2716. if( i != sa->size ) {
  2717. // if empty, we gotta remove it
  2718. if( empty ) {
  2719. script_array_remove_member(src, sa, i);
  2720. }
  2721. } else if( !empty ) { /* new entry */
  2722. script_array_add_member(sa,index);
  2723. // we do nothing if its empty, no point in modifying array data for a new empty member
  2724. }
  2725. } else if ( !empty ) { // we only move to create if not empty
  2726. sa = ers_alloc(array_ers, struct script_array);
  2727. sa->id = id;
  2728. sa->members = NULL;
  2729. sa->size = 0;
  2730. script_array_add_member(sa,index);
  2731. idb_put(src->arrays, id, sa);
  2732. }
  2733. }
  2734. /**
  2735. * Stores the value of a script variable
  2736. *
  2737. * @param st: current script state.
  2738. * @param sd: current character data.
  2739. * @param num: variable identifier.
  2740. * @param name: variable name.
  2741. * @param value: new value.
  2742. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2743. * @return: 0 failure, 1 success.
  2744. *
  2745. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2746. *------------------------------------------*/
  2747. int set_reg(struct script_state* st, struct map_session_data* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2748. {
  2749. char prefix = name[0];
  2750. size_t vlen = 0;
  2751. if ( !script_check_RegistryVariableLength(0,name,&vlen) )
  2752. {
  2753. ShowError("set_reg: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%d\n", sd?sd->status.account_id:-1, sd?sd->status.char_id:-1, name, vlen);
  2754. return 0;
  2755. }
  2756. if( is_string_variable(name) ) {// string variable
  2757. const char *str = (const char*)value;
  2758. switch (prefix) {
  2759. case '@':
  2760. pc_setregstr(sd, num, str);
  2761. return 1;
  2762. case '$':
  2763. return mapreg_setregstr(num, str);
  2764. case '#':
  2765. return (name[1] == '#') ?
  2766. pc_setaccountreg2str(sd, num, str) :
  2767. pc_setaccountregstr(sd, num, str);
  2768. case '.':
  2769. {
  2770. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2771. if( n ) {
  2772. if (str[0]) {
  2773. i64db_put(n->vars, num, aStrdup(str));
  2774. if( script_getvaridx(num) )
  2775. script_array_update(n, num, false);
  2776. } else {
  2777. i64db_remove(n->vars, num);
  2778. if( script_getvaridx(num) )
  2779. script_array_update(n, num, true);
  2780. }
  2781. }
  2782. }
  2783. return 1;
  2784. case '\'':
  2785. {
  2786. unsigned short instance_id = script_instancegetid(st);
  2787. if( instance_id ) {
  2788. if( str[0] ) {
  2789. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2790. if( script_getvaridx(num) )
  2791. script_array_update(&instance_data[instance_id].regs, num, false);
  2792. } else {
  2793. i64db_remove(instance_data[instance_id].regs.vars, num);
  2794. if (script_getvaridx(num))
  2795. script_array_update(&instance_data[instance_id].regs, num, true);
  2796. }
  2797. } else {
  2798. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2799. script_reportsrc(st);
  2800. }
  2801. return 1;
  2802. }
  2803. default:
  2804. return pc_setglobalreg_str(sd, num, str);
  2805. }
  2806. } else {// integer variable
  2807. int val = (int)__64BPRTSIZE(value);
  2808. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2809. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2810. if( st != NULL ) {
  2811. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2812. script_reportsrc(st);
  2813. st->state = END;
  2814. }
  2815. return 0;
  2816. }
  2817. return 1;
  2818. }
  2819. switch (prefix) {
  2820. case '@':
  2821. pc_setreg(sd, num, val);
  2822. return 1;
  2823. case '$':
  2824. return mapreg_setreg(num, val);
  2825. case '#':
  2826. return (name[1] == '#') ?
  2827. pc_setaccountreg2(sd, num, val) :
  2828. pc_setaccountreg(sd, num, val);
  2829. case '.':
  2830. {
  2831. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2832. if( n ) {
  2833. if( val != 0 ) {
  2834. i64db_iput(n->vars, num, val);
  2835. if( script_getvaridx(num) )
  2836. script_array_update(n, num, false);
  2837. } else {
  2838. i64db_remove(n->vars, num);
  2839. if( script_getvaridx(num) )
  2840. script_array_update(n, num, true);
  2841. }
  2842. }
  2843. }
  2844. return 1;
  2845. case '\'':
  2846. {
  2847. unsigned short instance_id = script_instancegetid(st);
  2848. if( instance_id ) {
  2849. if( val != 0 ) {
  2850. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2851. if( script_getvaridx(num) )
  2852. script_array_update(&instance_data[instance_id].regs, num, false);
  2853. } else {
  2854. i64db_remove(instance_data[instance_id].regs.vars, num);
  2855. if (script_getvaridx(num))
  2856. script_array_update(&instance_data[instance_id].regs, num, true);
  2857. }
  2858. } else {
  2859. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2860. script_reportsrc(st);
  2861. }
  2862. return 1;
  2863. }
  2864. default:
  2865. return pc_setglobalreg(sd, num, val);
  2866. }
  2867. }
  2868. }
  2869. int set_var(struct map_session_data* sd, char* name, void* val)
  2870. {
  2871. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2872. }
  2873. void setd_sub(struct script_state *st, struct map_session_data *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2874. {
  2875. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2876. }
  2877. /**
  2878. * Converts the data to a string
  2879. * @param st
  2880. * @param data
  2881. * @param sd
  2882. */
  2883. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2884. {
  2885. char* p;
  2886. get_val_(st, data, sd);
  2887. if( data_isstring(data) )
  2888. {// nothing to convert
  2889. }
  2890. else if( data_isint(data) )
  2891. {// int -> string
  2892. CREATE(p, char, ITEM_NAME_LENGTH);
  2893. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2894. p[ITEM_NAME_LENGTH-1] = '\0';
  2895. data->type = C_STR;
  2896. data->u.str = p;
  2897. }
  2898. else if( data_isreference(data) )
  2899. {// reference -> string
  2900. //##TODO when does this happen (check get_val) [FlavioJS]
  2901. data->type = C_CONSTSTR;
  2902. data->u.str = reference_getname(data);
  2903. }
  2904. else
  2905. {// unsupported data type
  2906. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2907. script_reportdata(data);
  2908. script_reportsrc(st);
  2909. data->type = C_CONSTSTR;
  2910. data->u.str = const_cast<char *>("");
  2911. }
  2912. return data->u.str;
  2913. }
  2914. const char* conv_str(struct script_state* st, struct script_data* data)
  2915. {
  2916. return conv_str_(st, data, NULL);
  2917. }
  2918. /**
  2919. * Converts the data to an int
  2920. * @param st
  2921. * @param data
  2922. * @param sd
  2923. */
  2924. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2925. {
  2926. get_val_(st, data, sd);
  2927. if( data_isint(data) )
  2928. {// nothing to convert
  2929. }
  2930. else if( data_isstring(data) )
  2931. {// string -> int
  2932. // the result does not overflow or underflow, it is capped instead
  2933. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2934. char* p = data->u.str;
  2935. long num;
  2936. errno = 0;
  2937. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2938. if( errno == ERANGE
  2939. #if LONG_MAX > INT_MAX
  2940. || num < INT_MIN || num > INT_MAX
  2941. #endif
  2942. )
  2943. {
  2944. if( num <= INT_MIN )
  2945. {
  2946. num = INT_MIN;
  2947. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2948. }
  2949. else//if( num >= INT_MAX )
  2950. {
  2951. num = INT_MAX;
  2952. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2953. }
  2954. script_reportdata(data);
  2955. script_reportsrc(st);
  2956. }
  2957. if( data->type == C_STR )
  2958. aFree(p);
  2959. data->type = C_INT;
  2960. data->u.num = (int)num;
  2961. }
  2962. #if 0
  2963. // FIXME this function is being used to retrieve the position of labels and
  2964. // probably other stuff [FlavioJS]
  2965. else
  2966. {// unsupported data type
  2967. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2968. script_reportdata(data);
  2969. script_reportsrc(st);
  2970. data->type = C_INT;
  2971. data->u.num = 0;
  2972. }
  2973. #endif
  2974. return (int)data->u.num;
  2975. }
  2976. int conv_num(struct script_state* st, struct script_data* data)
  2977. {
  2978. return conv_num_(st, data, NULL);
  2979. }
  2980. //
  2981. // Stack operations
  2982. //
  2983. /// Increases the size of the stack
  2984. void stack_expand(struct script_stack* stack)
  2985. {
  2986. stack->sp_max += 64;
  2987. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2988. stack->sp_max * sizeof(stack->stack_data[0]) );
  2989. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2990. 64 * sizeof(stack->stack_data[0]) );
  2991. }
  2992. /// Pushes a value into the stack
  2993. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2994. /// Pushes a value into the stack (with reference)
  2995. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2996. {
  2997. if( stack->sp >= stack->sp_max )
  2998. stack_expand(stack);
  2999. stack->stack_data[stack->sp].type = type;
  3000. stack->stack_data[stack->sp].u.num = val;
  3001. stack->stack_data[stack->sp].ref = ref;
  3002. stack->sp++;
  3003. return &stack->stack_data[stack->sp-1];
  3004. }
  3005. /// Pushes a string into the stack
  3006. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3007. {
  3008. if( stack->sp >= stack->sp_max )
  3009. stack_expand(stack);
  3010. stack->stack_data[stack->sp].type = type;
  3011. stack->stack_data[stack->sp].u.str = str;
  3012. stack->stack_data[stack->sp].ref = NULL;
  3013. stack->sp++;
  3014. return &stack->stack_data[stack->sp-1];
  3015. }
  3016. /// Pushes a retinfo into the stack
  3017. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3018. {
  3019. if( stack->sp >= stack->sp_max )
  3020. stack_expand(stack);
  3021. stack->stack_data[stack->sp].type = C_RETINFO;
  3022. stack->stack_data[stack->sp].u.ri = ri;
  3023. stack->stack_data[stack->sp].ref = ref;
  3024. stack->sp++;
  3025. return &stack->stack_data[stack->sp-1];
  3026. }
  3027. /// Pushes a copy of the target position into the stack
  3028. struct script_data* push_copy(struct script_stack* stack, int pos)
  3029. {
  3030. switch( stack->stack_data[pos].type ) {
  3031. case C_CONSTSTR:
  3032. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3033. break;
  3034. case C_STR:
  3035. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3036. break;
  3037. case C_RETINFO:
  3038. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3039. exit(1);
  3040. break;
  3041. default:
  3042. return push_val2(
  3043. stack,stack->stack_data[pos].type,
  3044. stack->stack_data[pos].u.num,
  3045. stack->stack_data[pos].ref
  3046. );
  3047. break;
  3048. }
  3049. }
  3050. /// Removes the values in indexes [start,end] from the stack.
  3051. /// Adjusts all stack pointers.
  3052. void pop_stack(struct script_state* st, int start, int end)
  3053. {
  3054. struct script_stack* stack = st->stack;
  3055. struct script_data* data;
  3056. int i;
  3057. if( start < 0 )
  3058. start = 0;
  3059. if( end > stack->sp )
  3060. end = stack->sp;
  3061. if( start >= end )
  3062. return;// nothing to pop
  3063. // free stack elements
  3064. for( i = start; i < end; i++ )
  3065. {
  3066. data = &stack->stack_data[i];
  3067. if( data->type == C_STR )
  3068. aFree(data->u.str);
  3069. if( data->type == C_RETINFO ) {
  3070. struct script_retinfo* ri = data->u.ri;
  3071. if (ri->scope.vars) {
  3072. script_free_vars(ri->scope.vars);
  3073. ri->scope.vars = NULL;
  3074. }
  3075. if (ri->scope.arrays) {
  3076. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3077. ri->scope.arrays = NULL;
  3078. }
  3079. if( data->ref )
  3080. aFree(data->ref);
  3081. aFree(ri);
  3082. }
  3083. data->type = C_NOP;
  3084. }
  3085. // move the rest of the elements
  3086. if( stack->sp > end )
  3087. {
  3088. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3089. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3090. stack->stack_data[i].type = C_NOP;
  3091. }
  3092. // adjust stack pointers
  3093. if( st->start > end ){
  3094. st->start -= end - start;
  3095. }else if( st->start > start ){
  3096. st->start = start;
  3097. }
  3098. if( st->end > end ){
  3099. st->end -= end - start;
  3100. }else if( st->end > start ){
  3101. st->end = start;
  3102. }
  3103. if( stack->defsp > end ){
  3104. stack->defsp -= end - start;
  3105. }else if( stack->defsp > start ){
  3106. stack->defsp = start;
  3107. }
  3108. stack->sp -= end - start;
  3109. }
  3110. ///
  3111. ///
  3112. ///
  3113. /*==========================================
  3114. * Release script dependent variable, dependent variable of function
  3115. *------------------------------------------*/
  3116. void script_free_vars(struct DBMap* storage)
  3117. {
  3118. if( storage ) {
  3119. // destroy the storage construct containing the variables
  3120. db_destroy(storage);
  3121. }
  3122. }
  3123. void script_free_code(struct script_code* code)
  3124. {
  3125. nullpo_retv(code);
  3126. if (code->instances)
  3127. script_stop_scriptinstances(code);
  3128. script_free_vars(code->local.vars);
  3129. if (code->local.arrays)
  3130. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3131. aFree(code->script_buf);
  3132. aFree(code);
  3133. }
  3134. /// Creates a new script state.
  3135. ///
  3136. /// @param script Script code
  3137. /// @param pos Position in the code
  3138. /// @param rid Who is running the script (attached player)
  3139. /// @param oid Where the code is being run (npc 'object')
  3140. /// @return Script state
  3141. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3142. {
  3143. struct script_state* st;
  3144. st = ers_alloc(st_ers, struct script_state);
  3145. st->stack = ers_alloc(stack_ers, struct script_stack);
  3146. st->stack->sp = 0;
  3147. st->stack->sp_max = 64;
  3148. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3149. st->stack->defsp = st->stack->sp;
  3150. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3151. st->stack->scope.arrays = NULL;
  3152. st->state = RUN;
  3153. st->script = rootscript;
  3154. st->pos = pos;
  3155. st->rid = rid;
  3156. st->oid = oid;
  3157. st->sleep.timer = INVALID_TIMER;
  3158. st->npc_item_flag = battle_config.item_enabled_npc;
  3159. if( st->script->instances != USHRT_MAX )
  3160. st->script->instances++;
  3161. else {
  3162. struct npc_data *nd = map_id2nd(oid);
  3163. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3164. }
  3165. if (!st->script->local.vars)
  3166. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3167. st->id = next_id++;
  3168. active_scripts++;
  3169. idb_put(st_db, st->id, st);
  3170. return st;
  3171. }
  3172. /// Frees a script state.
  3173. ///
  3174. /// @param st Script state
  3175. void script_free_state(struct script_state* st)
  3176. {
  3177. if (idb_exists(st_db, st->id)) {
  3178. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3179. if (st->bk_st) // backup was not restored
  3180. 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);
  3181. if (sd && sd->st == st) { // Current script is aborted.
  3182. if(sd->state.using_fake_npc) {
  3183. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3184. sd->state.using_fake_npc = 0;
  3185. }
  3186. sd->st = NULL;
  3187. sd->npc_id = 0;
  3188. }
  3189. if (st->sleep.timer != INVALID_TIMER)
  3190. delete_timer(st->sleep.timer, run_script_timer);
  3191. if (st->stack) {
  3192. script_free_vars(st->stack->scope.vars);
  3193. if (st->stack->scope.arrays)
  3194. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3195. pop_stack(st, 0, st->stack->sp);
  3196. aFree(st->stack->stack_data);
  3197. ers_free(stack_ers, st->stack);
  3198. st->stack = NULL;
  3199. }
  3200. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3201. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3202. script_free_vars(st->script->local.vars);
  3203. st->script->local.vars = NULL;
  3204. }
  3205. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3206. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3207. st->script->local.arrays = NULL;
  3208. }
  3209. }
  3210. st->pos = -1;
  3211. idb_remove(st_db, st->id);
  3212. ers_free(st_ers, st);
  3213. if (--active_scripts == 0)
  3214. next_id = 0;
  3215. }
  3216. }
  3217. //
  3218. // Main execution unit
  3219. //
  3220. /*==========================================
  3221. * Read command
  3222. *------------------------------------------*/
  3223. c_op get_com(unsigned char *script,int *pos)
  3224. {
  3225. int i = 0, j = 0;
  3226. if(script[*pos]>=0x80){
  3227. return C_INT;
  3228. }
  3229. while(script[*pos]>=0x40){
  3230. i=script[(*pos)++]<<j;
  3231. j+=6;
  3232. }
  3233. return (c_op)(i+(script[(*pos)++]<<j));
  3234. }
  3235. /*==========================================
  3236. * Income figures
  3237. *------------------------------------------*/
  3238. int get_num(unsigned char *script,int *pos)
  3239. {
  3240. int i,j;
  3241. i=0; j=0;
  3242. while(script[*pos]>=0xc0){
  3243. i+=(script[(*pos)++]&0x7f)<<j;
  3244. j+=6;
  3245. }
  3246. return i+((script[(*pos)++]&0x7f)<<j);
  3247. }
  3248. /// Ternary operators
  3249. /// test ? if_true : if_false
  3250. void op_3(struct script_state* st, int op)
  3251. {
  3252. struct script_data* data;
  3253. int flag = 0;
  3254. data = script_getdatatop(st, -3);
  3255. get_val(st, data);
  3256. if( data_isstring(data) )
  3257. flag = data->u.str[0];// "" -> false
  3258. else if( data_isint(data) )
  3259. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3260. else
  3261. {
  3262. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3263. script_reportdata(data);
  3264. script_reportsrc(st);
  3265. script_removetop(st, -3, 0);
  3266. script_pushnil(st);
  3267. return;
  3268. }
  3269. if( flag )
  3270. script_pushcopytop(st, -2);
  3271. else
  3272. script_pushcopytop(st, -1);
  3273. script_removetop(st, -4, -1);
  3274. }
  3275. /// Binary string operators
  3276. /// s1 EQ s2 -> i
  3277. /// s1 NE s2 -> i
  3278. /// s1 GT s2 -> i
  3279. /// s1 GE s2 -> i
  3280. /// s1 LT s2 -> i
  3281. /// s1 LE s2 -> i
  3282. /// s1 ADD s2 -> s
  3283. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3284. {
  3285. int a = 0;
  3286. switch(op){
  3287. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3288. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3289. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3290. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3291. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3292. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3293. case C_ADD:
  3294. {
  3295. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3296. strcpy(buf, s1);
  3297. strcat(buf, s2);
  3298. script_pushstr(st, buf);
  3299. return;
  3300. }
  3301. default:
  3302. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3303. script_reportsrc(st);
  3304. script_pushnil(st);
  3305. st->state = END;
  3306. return;
  3307. }
  3308. script_pushint(st,a);
  3309. }
  3310. /// Binary number operators
  3311. /// i OP i -> i
  3312. void op_2num(struct script_state* st, int op, int i1, int i2)
  3313. {
  3314. int ret;
  3315. double ret_double;
  3316. switch( op ) {
  3317. case C_AND: ret = i1 & i2; break;
  3318. case C_OR: ret = i1 | i2; break;
  3319. case C_XOR: ret = i1 ^ i2; break;
  3320. case C_LAND: ret = (i1 && i2); break;
  3321. case C_LOR: ret = (i1 || i2); break;
  3322. case C_EQ: ret = (i1 == i2); break;
  3323. case C_NE: ret = (i1 != i2); break;
  3324. case C_GT: ret = (i1 > i2); break;
  3325. case C_GE: ret = (i1 >= i2); break;
  3326. case C_LT: ret = (i1 < i2); break;
  3327. case C_LE: ret = (i1 <= i2); break;
  3328. case C_R_SHIFT: ret = i1>>i2; break;
  3329. case C_L_SHIFT: ret = i1<<i2; break;
  3330. case C_DIV:
  3331. case C_MOD:
  3332. if( i2 == 0 )
  3333. {
  3334. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3335. script_reportsrc(st);
  3336. script_pushnil(st);
  3337. st->state = END;
  3338. return;
  3339. }
  3340. else if( op == C_DIV )
  3341. ret = i1 / i2;
  3342. else//if( op == C_MOD )
  3343. ret = i1 % i2;
  3344. break;
  3345. default:
  3346. switch( op ) {// operators that can overflow/underflow
  3347. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3348. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3349. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3350. default:
  3351. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3352. script_reportsrc(st);
  3353. script_pushnil(st);
  3354. return;
  3355. }
  3356. if( ret_double < (double)INT_MIN ) {
  3357. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3358. script_reportsrc(st);
  3359. ret = INT_MIN;
  3360. }
  3361. else if( ret_double > (double)INT_MAX ) {
  3362. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3363. script_reportsrc(st);
  3364. ret = INT_MAX;
  3365. }
  3366. }
  3367. script_pushint(st, ret);
  3368. }
  3369. /// Binary operators
  3370. void op_2(struct script_state *st, int op)
  3371. {
  3372. struct script_data* left, leftref;
  3373. struct script_data* right;
  3374. leftref.type = C_NOP;
  3375. left = script_getdatatop(st, -2);
  3376. right = script_getdatatop(st, -1);
  3377. if (st->op2ref) {
  3378. if (data_isreference(left)) {
  3379. leftref = *left;
  3380. }
  3381. st->op2ref = 0;
  3382. }
  3383. get_val(st, left);
  3384. get_val(st, right);
  3385. // automatic conversions
  3386. switch( op )
  3387. {
  3388. case C_ADD:
  3389. if( data_isint(left) && data_isstring(right) )
  3390. {// convert int-string to string-string
  3391. conv_str(st, left);
  3392. }
  3393. else if( data_isstring(left) && data_isint(right) )
  3394. {// convert string-int to string-string
  3395. conv_str(st, right);
  3396. }
  3397. break;
  3398. }
  3399. if( data_isstring(left) && data_isstring(right) )
  3400. {// ss => op_2str
  3401. op_2str(st, op, left->u.str, right->u.str);
  3402. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3403. if (leftref.type != C_NOP)
  3404. {
  3405. if (left->type == C_STR) // don't free C_CONSTSTR
  3406. aFree(left->u.str);
  3407. *left = leftref;
  3408. }
  3409. }
  3410. else if( data_isint(left) && data_isint(right) )
  3411. {// ii => op_2num
  3412. int i1 = (int)left->u.num;
  3413. int i2 = (int)right->u.num;
  3414. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3415. op_2num(st, op, i1, i2);
  3416. if (leftref.type != C_NOP)
  3417. *left = leftref;
  3418. }
  3419. else
  3420. {// invalid argument
  3421. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3422. script_reportdata(left);
  3423. script_reportdata(right);
  3424. script_reportsrc(st);
  3425. script_removetop(st, -2, 0);
  3426. script_pushnil(st);
  3427. st->state = END;
  3428. }
  3429. }
  3430. /// Unary operators
  3431. /// NEG i -> i
  3432. /// NOT i -> i
  3433. /// LNOT i -> i
  3434. void op_1(struct script_state* st, int op)
  3435. {
  3436. struct script_data* data;
  3437. int i1;
  3438. data = script_getdatatop(st, -1);
  3439. get_val(st, data);
  3440. if( !data_isint(data) )
  3441. {// not a number
  3442. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3443. script_reportdata(data);
  3444. script_reportsrc(st);
  3445. script_pushnil(st);
  3446. st->state = END;
  3447. return;
  3448. }
  3449. i1 = (int)data->u.num;
  3450. script_removetop(st, -1, 0);
  3451. switch( op )
  3452. {
  3453. case C_NEG: i1 = -i1; break;
  3454. case C_NOT: i1 = ~i1; break;
  3455. case C_LNOT: i1 = !i1; break;
  3456. default:
  3457. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3458. script_reportsrc(st);
  3459. script_pushnil(st);
  3460. st->state = END;
  3461. return;
  3462. }
  3463. script_pushint(st, i1);
  3464. }
  3465. /// Checks the type of all arguments passed to a built-in function.
  3466. ///
  3467. /// @param st Script state whose stack arguments should be inspected.
  3468. /// @param func Built-in function for which the arguments are intended.
  3469. static void script_check_buildin_argtype(struct script_state* st, int func)
  3470. {
  3471. int idx, invalid = 0;
  3472. for( idx = 2; script_hasdata(st, idx); idx++ )
  3473. {
  3474. struct script_data* data = script_getdata(st, idx);
  3475. script_function* sf = &buildin_func[str_data[func].val];
  3476. char type = sf->arg[idx-2];
  3477. if( type == '?' || type == '*' )
  3478. {// optional argument or unknown number of optional parameters ( no types are after this )
  3479. break;
  3480. }
  3481. else if( type == 0 )
  3482. {// more arguments than necessary ( should not happen, as it is checked before )
  3483. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3484. invalid++;
  3485. break;
  3486. }
  3487. else
  3488. {
  3489. const char* name = NULL;
  3490. if( data_isreference(data) )
  3491. {// get name for variables to determine the type they refer to
  3492. name = reference_getname(data);
  3493. }
  3494. switch( type )
  3495. {
  3496. case 'v':
  3497. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3498. {// variant
  3499. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3500. script_reportdata(data);
  3501. invalid++;
  3502. }
  3503. break;
  3504. case 's':
  3505. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3506. {// string
  3507. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3508. script_reportdata(data);
  3509. invalid++;
  3510. }
  3511. break;
  3512. case 'i':
  3513. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3514. {// int ( params and constants are always int )
  3515. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3516. script_reportdata(data);
  3517. invalid++;
  3518. }
  3519. break;
  3520. case 'r':
  3521. if( !data_isreference(data) )
  3522. {// variables
  3523. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3524. script_reportdata(data);
  3525. invalid++;
  3526. }
  3527. break;
  3528. case 'l':
  3529. if( !data_islabel(data) && !data_isfunclabel(data) )
  3530. {// label
  3531. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3532. script_reportdata(data);
  3533. invalid++;
  3534. }
  3535. break;
  3536. }
  3537. }
  3538. }
  3539. if(invalid)
  3540. {
  3541. ShowDebug("Function: %s\n", get_str(func));
  3542. script_reportsrc(st);
  3543. }
  3544. }
  3545. /// Executes a buildin command.
  3546. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3547. int run_func(struct script_state *st)
  3548. {
  3549. struct script_data* data;
  3550. int i,start_sp,end_sp,func;
  3551. end_sp = st->stack->sp;// position after the last argument
  3552. for( i = end_sp-1; i > 0 ; --i )
  3553. if( st->stack->stack_data[i].type == C_ARG )
  3554. break;
  3555. if( i == 0 ) {
  3556. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3557. st->state = END;
  3558. script_reportsrc(st);
  3559. return 1;
  3560. }
  3561. start_sp = i-1;// C_NAME of the command
  3562. st->start = start_sp;
  3563. st->end = end_sp;
  3564. data = &st->stack->stack_data[st->start];
  3565. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3566. func = (int)data->u.num;
  3567. st->funcname = reference_getname(data);
  3568. } else {
  3569. ShowError("script:run_func: not a buildin command.\n");
  3570. script_reportdata(data);
  3571. script_reportsrc(st);
  3572. st->state = END;
  3573. return 1;
  3574. }
  3575. if( script_config.warn_func_mismatch_argtypes ) {
  3576. script_check_buildin_argtype(st, func);
  3577. }
  3578. if(str_data[func].func) {
  3579. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3580. if( buildin_func[str_data[func].val].deprecated ){
  3581. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3582. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3583. script_reportsrc(st);
  3584. }
  3585. #endif
  3586. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3587. script_reportsrc(st);
  3588. } else {
  3589. 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));
  3590. script_reportsrc(st);
  3591. st->state = END;
  3592. }
  3593. // Stack's datum are used when re-running functions [Eoe]
  3594. if( st->state == RERUNLINE )
  3595. return 0;
  3596. pop_stack(st, st->start, st->end);
  3597. if( st->state == RETFUNC ) {// return from a user-defined function
  3598. struct script_retinfo* ri;
  3599. int olddefsp = st->stack->defsp;
  3600. int nargs;
  3601. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3602. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3603. {
  3604. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3605. script_reportsrc(st);
  3606. st->state = END;
  3607. return 1;
  3608. }
  3609. script_free_vars(st->stack->scope.vars);
  3610. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3611. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3612. nargs = ri->nargs;
  3613. st->pos = ri->pos;
  3614. st->script = ri->script;
  3615. st->stack->scope.vars = ri->scope.vars;
  3616. st->stack->scope.arrays = ri->scope.arrays;
  3617. st->stack->defsp = ri->defsp;
  3618. memset(ri, 0, sizeof(struct script_retinfo));
  3619. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3620. st->state = GOTO;
  3621. }
  3622. return 0;
  3623. }
  3624. /*==========================================
  3625. * script execution
  3626. *------------------------------------------*/
  3627. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3628. {
  3629. struct script_state *st;
  3630. if( rootscript == NULL || pos < 0 )
  3631. return;
  3632. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3633. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3634. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3635. st = script_alloc_state(rootscript, pos, rid, oid);
  3636. run_script_main(st);
  3637. }
  3638. /**
  3639. * Free all related script code
  3640. * @param code: Script code to free
  3641. */
  3642. void script_stop_scriptinstances(struct script_code *code) {
  3643. DBIterator *iter;
  3644. struct script_state* st;
  3645. if( !active_scripts )
  3646. return; // Don't even bother.
  3647. iter = db_iterator(st_db);
  3648. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3649. if( st->script == code )
  3650. script_free_state(st);
  3651. }
  3652. dbi_destroy(iter);
  3653. }
  3654. /*==========================================
  3655. * Timer function for sleep
  3656. *------------------------------------------*/
  3657. TIMER_FUNC(run_script_timer){
  3658. struct script_state *st = (struct script_state *)data;
  3659. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3660. // If it was a player before going to sleep and there is still a unit attached to the script
  3661. if( id != 0 && st->rid ){
  3662. struct map_session_data *sd = map_id2sd(st->rid);
  3663. // Attached player is offline(logout) or another unit type(should not happen)
  3664. if( !sd ){
  3665. st->rid = 0;
  3666. st->state = END;
  3667. // Character mismatch. Cancel execution.
  3668. }else if( sd->status.char_id != id ){
  3669. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3670. script_reportsrc(st);
  3671. st->rid = 0;
  3672. st->state = END;
  3673. }
  3674. }
  3675. while (node && st->sleep.timer != INVALID_TIMER) {
  3676. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3677. script_erase_sleepdb(node);
  3678. st->sleep.timer = INVALID_TIMER;
  3679. break;
  3680. }
  3681. node = node->next;
  3682. }
  3683. if(st->state != RERUNLINE)
  3684. st->sleep.tick = 0;
  3685. run_script_main(st);
  3686. return 0;
  3687. }
  3688. /**
  3689. * Remove sleep timers from the NPC
  3690. * @param id: NPC ID
  3691. */
  3692. void script_stop_sleeptimers(int id) {
  3693. for (;;) {
  3694. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3695. if (!st)
  3696. break; // No more sleep timers
  3697. if (st->oid == id)
  3698. script_free_state(st);
  3699. }
  3700. }
  3701. /**
  3702. * Delete the specified node from sleep_db
  3703. * @param n: Linked list of sleep timers
  3704. */
  3705. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3706. struct linkdb_node *retnode;
  3707. if (!n)
  3708. return NULL;
  3709. if (!n->prev)
  3710. sleep_db = n->next;
  3711. else
  3712. n->prev->next = n->next;
  3713. if (n->next)
  3714. n->next->prev = n->prev;
  3715. retnode = n->next;
  3716. aFree(n);
  3717. return retnode;
  3718. }
  3719. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3720. ///
  3721. /// @param st Script state to detach.
  3722. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3723. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3724. {
  3725. struct map_session_data* sd;
  3726. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3727. sd->st = st->bk_st;
  3728. sd->npc_id = st->bk_npcid;
  3729. sd->state.disable_atcommand_on_npc = 0;
  3730. if(st->bk_st) {
  3731. //Remove tag for removal.
  3732. st->bk_st = NULL;
  3733. st->bk_npcid = 0;
  3734. } else if(dequeue_event) {
  3735. #ifdef SECURE_NPCTIMEOUT
  3736. /**
  3737. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3738. **/
  3739. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3740. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3741. sd->npc_idle_timer = INVALID_TIMER;
  3742. }
  3743. #endif
  3744. npc_event_dequeue(sd);
  3745. }
  3746. }
  3747. else if(st->bk_st)
  3748. {// rid was set to 0, before detaching the script state
  3749. 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);
  3750. script_reportsrc(st->bk_st);
  3751. script_free_state(st->bk_st);
  3752. st->bk_st = NULL;
  3753. }
  3754. }
  3755. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3756. ///
  3757. /// @param st Script state to attach.
  3758. void script_attach_state(struct script_state* st){
  3759. struct map_session_data* sd;
  3760. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3761. {
  3762. if(st!=sd->st)
  3763. {
  3764. if(st->bk_st)
  3765. {// there is already a backup
  3766. 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);
  3767. }
  3768. st->bk_st = sd->st;
  3769. st->bk_npcid = sd->npc_id;
  3770. }
  3771. sd->st = st;
  3772. sd->npc_id = st->oid;
  3773. sd->npc_item_flag = st->npc_item_flag; // load default.
  3774. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3775. #ifdef SECURE_NPCTIMEOUT
  3776. if( sd->npc_idle_timer == INVALID_TIMER )
  3777. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3778. sd->npc_idle_tick = gettick();
  3779. #endif
  3780. }
  3781. }
  3782. /*==========================================
  3783. * The main part of the script execution
  3784. *------------------------------------------*/
  3785. void run_script_main(struct script_state *st)
  3786. {
  3787. int cmdcount = script_config.check_cmdcount;
  3788. int gotocount = script_config.check_gotocount;
  3789. TBL_PC *sd;
  3790. struct script_stack *stack = st->stack;
  3791. script_attach_state(st);
  3792. if(st->state == RERUNLINE) {
  3793. run_func(st);
  3794. if(st->state == GOTO)
  3795. st->state = RUN;
  3796. } else if(st->state != END)
  3797. st->state = RUN;
  3798. while(st->state == RUN) {
  3799. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3800. switch(c){
  3801. case C_EOL:
  3802. if( stack->defsp > stack->sp )
  3803. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3804. else
  3805. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3806. break;
  3807. case C_INT:
  3808. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3809. break;
  3810. case C_POS:
  3811. case C_NAME:
  3812. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3813. st->pos+=3;
  3814. break;
  3815. case C_ARG:
  3816. push_val(stack,c,0);
  3817. break;
  3818. case C_STR:
  3819. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3820. while(st->script->script_buf[st->pos++]);
  3821. break;
  3822. case C_FUNC:
  3823. run_func(st);
  3824. if(st->state==GOTO){
  3825. st->state = RUN;
  3826. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3827. ShowError("script:run_script_main: infinity loop !\n");
  3828. script_reportsrc(st);
  3829. st->state=END;
  3830. }
  3831. }
  3832. break;
  3833. case C_REF:
  3834. st->op2ref = 1;
  3835. break;
  3836. case C_NEG:
  3837. case C_NOT:
  3838. case C_LNOT:
  3839. op_1(st ,c);
  3840. break;
  3841. case C_ADD:
  3842. case C_SUB:
  3843. case C_MUL:
  3844. case C_DIV:
  3845. case C_MOD:
  3846. case C_EQ:
  3847. case C_NE:
  3848. case C_GT:
  3849. case C_GE:
  3850. case C_LT:
  3851. case C_LE:
  3852. case C_AND:
  3853. case C_OR:
  3854. case C_XOR:
  3855. case C_LAND:
  3856. case C_LOR:
  3857. case C_R_SHIFT:
  3858. case C_L_SHIFT:
  3859. op_2(st, c);
  3860. break;
  3861. case C_OP3:
  3862. op_3(st, c);
  3863. break;
  3864. case C_NOP:
  3865. st->state=END;
  3866. break;
  3867. default:
  3868. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3869. st->state=END;
  3870. break;
  3871. }
  3872. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3873. ShowError("script:run_script_main: infinity loop !\n");
  3874. script_reportsrc(st);
  3875. st->state=END;
  3876. }
  3877. }
  3878. if(st->sleep.tick > 0) {
  3879. //Restore previous script
  3880. script_detach_state(st, false);
  3881. //Delay execution
  3882. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3883. st->sleep.charid = sd?sd->status.char_id:0;
  3884. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3885. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3886. } else if(st->state != END && st->rid) {
  3887. //Resume later (st is already attached to player).
  3888. if(st->bk_st) {
  3889. ShowWarning("Unable to restore stack! Double continuation!\n");
  3890. //Report BOTH scripts to see if that can help somehow.
  3891. ShowDebug("Previous script (lost):\n");
  3892. script_reportsrc(st->bk_st);
  3893. ShowDebug("Current script:\n");
  3894. script_reportsrc(st);
  3895. script_free_state(st->bk_st);
  3896. st->bk_st = NULL;
  3897. }
  3898. } else {
  3899. //Dispose of script.
  3900. if ((sd = map_id2sd(st->rid))!=NULL)
  3901. { //Restore previous stack and save char.
  3902. if(sd->state.using_fake_npc){
  3903. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3904. sd->state.using_fake_npc = 0;
  3905. }
  3906. //Restore previous script if any.
  3907. script_detach_state(st, true);
  3908. if (sd->vars_dirty)
  3909. intif_saveregistry(sd);
  3910. }
  3911. script_free_state(st);
  3912. }
  3913. }
  3914. int script_config_read(const char *cfgName)
  3915. {
  3916. int i;
  3917. char line[1024],w1[1024],w2[1024];
  3918. FILE *fp;
  3919. fp=fopen(cfgName,"r");
  3920. if(fp==NULL){
  3921. ShowError("File not found: %s\n", cfgName);
  3922. return 1;
  3923. }
  3924. while(fgets(line, sizeof(line), fp))
  3925. {
  3926. if(line[0] == '/' && line[1] == '/')
  3927. continue;
  3928. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3929. if(i!=2)
  3930. continue;
  3931. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3932. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3933. }
  3934. else if(strcmpi(w1,"check_cmdcount")==0) {
  3935. script_config.check_cmdcount = config_switch(w2);
  3936. }
  3937. else if(strcmpi(w1,"check_gotocount")==0) {
  3938. script_config.check_gotocount = config_switch(w2);
  3939. }
  3940. else if(strcmpi(w1,"input_min_value")==0) {
  3941. script_config.input_min_value = config_switch(w2);
  3942. }
  3943. else if(strcmpi(w1,"input_max_value")==0) {
  3944. script_config.input_max_value = config_switch(w2);
  3945. }
  3946. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3947. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3948. }
  3949. else if(strcmpi(w1,"import")==0){
  3950. script_config_read(w2);
  3951. }
  3952. else {
  3953. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3954. }
  3955. }
  3956. fclose(fp);
  3957. return 0;
  3958. }
  3959. /**
  3960. * @see DBApply
  3961. */
  3962. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3963. {
  3964. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  3965. if (code)
  3966. script_free_code(code);
  3967. return 0;
  3968. }
  3969. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3970. {
  3971. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3972. if( script )
  3973. {
  3974. int j;
  3975. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  3976. if( j < EQI_MAX ) {
  3977. //Single item autobonus
  3978. current_equip_item_index = sd->equip_index[j];
  3979. current_equip_combo_pos = 0;
  3980. } else {
  3981. //Combo autobonus
  3982. current_equip_item_index = -1;
  3983. current_equip_combo_pos = pos;
  3984. }
  3985. run_script(script,0,sd->bl.id,0);
  3986. }
  3987. }
  3988. void script_add_autobonus(const char *autobonus)
  3989. {
  3990. if( strdb_get(autobonus_db, autobonus) == NULL )
  3991. {
  3992. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3993. if( script )
  3994. strdb_put(autobonus_db, autobonus, script);
  3995. }
  3996. }
  3997. /// resets a temporary character array variable to given value
  3998. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3999. {
  4000. struct script_array *sa = NULL;
  4001. struct reg_db *src = NULL;
  4002. unsigned int i, *list = NULL, size = 0;
  4003. int key;
  4004. key = add_str(varname);
  4005. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4006. return;
  4007. if( value )
  4008. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  4009. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4010. return;
  4011. size = sa->size;
  4012. list = script_array_cpy_list(sa);
  4013. for(i = 0; i < size; i++) {
  4014. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  4015. }
  4016. }
  4017. /// sets a temporary character array variable element idx to given value
  4018. /// @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.
  4019. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  4020. {
  4021. int key;
  4022. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4023. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4024. return;
  4025. }
  4026. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4027. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  4028. if( refcache ) {
  4029. // save to avoid repeated script->add_str calls
  4030. refcache[0] = key;
  4031. }
  4032. }
  4033. /**
  4034. * Clears persistent variables from memory
  4035. **/
  4036. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4037. {
  4038. struct script_reg_state *src;
  4039. if( data->type != DB_DATA_PTR ) // got no need for those!
  4040. return 0;
  4041. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4042. if( src->type ) {
  4043. struct script_reg_str *p = (struct script_reg_str *)src;
  4044. if( p->value )
  4045. aFree(p->value);
  4046. ers_free(str_reg_ers,p);
  4047. } else {
  4048. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4049. }
  4050. return 0;
  4051. }
  4052. /**
  4053. * Clears a single persistent variable
  4054. **/
  4055. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4056. {
  4057. i64db_remove(sd->regs.vars, reg);
  4058. if( data->type ) {
  4059. struct script_reg_str *p = (struct script_reg_str*)data;
  4060. if( p->value )
  4061. aFree(p->value);
  4062. ers_free(str_reg_ers,p);
  4063. } else {
  4064. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4065. }
  4066. }
  4067. unsigned int *script_array_cpy_list(struct script_array *sa)
  4068. {
  4069. if( sa->size > generic_ui_array_size )
  4070. script_generic_ui_array_expand(sa->size);
  4071. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4072. return generic_ui_array;
  4073. }
  4074. void script_generic_ui_array_expand (unsigned int plus)
  4075. {
  4076. generic_ui_array_size += plus + 100;
  4077. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4078. }
  4079. /*==========================================
  4080. * Destructor
  4081. *------------------------------------------*/
  4082. void do_final_script() {
  4083. int i;
  4084. DBIterator *iter;
  4085. struct script_state *st;
  4086. #ifdef DEBUG_HASH
  4087. if (battle_config.etc_log)
  4088. {
  4089. FILE *fp = fopen("hash_dump.txt","wt");
  4090. if(fp) {
  4091. int count[SCRIPT_HASH_SIZE];
  4092. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4093. int n=0;
  4094. int min=INT_MAX,max=0,zero=0;
  4095. double mean=0.0f;
  4096. double median=0.0f;
  4097. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4098. memset(count, 0, sizeof(count));
  4099. fprintf(fp,"num : hash : data_name\n");
  4100. fprintf(fp,"---------------------------------------------------------------\n");
  4101. for(i=LABEL_START; i<str_num; i++) {
  4102. unsigned int h = calc_hash(get_str(i));
  4103. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4104. ++count[h];
  4105. }
  4106. fprintf(fp,"--------------------\n\n");
  4107. memset(count2, 0, sizeof(count2));
  4108. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4109. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4110. if(min > count[i])
  4111. min = count[i]; // minimun count of collision
  4112. if(max < count[i])
  4113. max = count[i]; // maximun count of collision
  4114. if(count[i] == 0)
  4115. zero++;
  4116. ++count2[count[i]];
  4117. }
  4118. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4119. for( i=min; i <= max; ++i ){
  4120. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4121. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4122. }
  4123. for( i=min; i <= max; ++i ){
  4124. n += count2[i];
  4125. if( n*2 >= SCRIPT_HASH_SIZE )
  4126. {
  4127. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4128. median = (i+i+1)/2.0f;
  4129. else
  4130. median = i;
  4131. break;
  4132. }
  4133. }
  4134. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4135. fclose(fp);
  4136. }
  4137. }
  4138. #endif
  4139. mapreg_final();
  4140. db_destroy(scriptlabel_db);
  4141. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4142. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4143. ers_destroy(array_ers);
  4144. if (generic_ui_array)
  4145. aFree(generic_ui_array);
  4146. iter = db_iterator(st_db);
  4147. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4148. script_free_state(st);
  4149. dbi_destroy(iter);
  4150. if (str_data)
  4151. aFree(str_data);
  4152. if (str_buf)
  4153. aFree(str_buf);
  4154. for( i = 0; i < atcmd_binding_count; i++ ) {
  4155. aFree(atcmd_binding[i]);
  4156. }
  4157. if( atcmd_binding_count != 0 )
  4158. aFree(atcmd_binding);
  4159. ers_destroy(st_ers);
  4160. ers_destroy(stack_ers);
  4161. db_destroy(st_db);
  4162. }
  4163. /*==========================================
  4164. * Initialization
  4165. *------------------------------------------*/
  4166. void do_init_script(void) {
  4167. st_db = idb_alloc(DB_OPT_BASE);
  4168. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4169. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4170. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4171. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4172. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4173. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4174. ers_chunk_size(st_ers, 10);
  4175. ers_chunk_size(stack_ers, 10);
  4176. active_scripts = 0;
  4177. next_id = 0;
  4178. mapreg_init();
  4179. }
  4180. void script_reload(void) {
  4181. int i;
  4182. DBIterator *iter;
  4183. struct script_state *st;
  4184. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4185. db_clear(scriptlabel_db);
  4186. // @commands (script based)
  4187. // Clear bindings
  4188. for( i = 0; i < atcmd_binding_count; i++ ) {
  4189. aFree(atcmd_binding[i]);
  4190. }
  4191. if( atcmd_binding_count != 0 )
  4192. aFree(atcmd_binding);
  4193. atcmd_binding_count = 0;
  4194. iter = db_iterator(st_db);
  4195. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4196. script_free_state(st);
  4197. dbi_destroy(iter);
  4198. db_clear(st_db);
  4199. mapreg_reload();
  4200. }
  4201. //-----------------------------------------------------------------------------
  4202. // buildin functions
  4203. //
  4204. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4205. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4206. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4207. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4208. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4209. /////////////////////////////////////////////////////////////////////
  4210. // NPC interaction
  4211. //
  4212. /// Appends a message to the npc dialog.
  4213. /// If a dialog doesn't exist yet, one is created.
  4214. ///
  4215. /// mes "<message>";
  4216. BUILDIN_FUNC(mes)
  4217. {
  4218. TBL_PC* sd;
  4219. if( !script_rid2sd(sd) )
  4220. return SCRIPT_CMD_SUCCESS;
  4221. if( !script_hasdata(st, 3) )
  4222. {// only a single line detected in the script
  4223. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4224. }
  4225. else
  4226. {// parse multiple lines as they exist
  4227. int i;
  4228. for( i = 2; script_hasdata(st, i); i++ )
  4229. {
  4230. // send the message to the client
  4231. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4232. }
  4233. }
  4234. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4235. return SCRIPT_CMD_SUCCESS;
  4236. }
  4237. /// Displays the button 'next' in the npc dialog.
  4238. /// The dialog text is cleared and the script continues when the button is pressed.
  4239. ///
  4240. /// next;
  4241. BUILDIN_FUNC(next)
  4242. {
  4243. TBL_PC* sd;
  4244. if( !script_rid2sd(sd) )
  4245. return SCRIPT_CMD_SUCCESS;
  4246. #ifdef SECURE_NPCTIMEOUT
  4247. sd->npc_idle_type = NPCT_WAIT;
  4248. #endif
  4249. st->state = STOP;
  4250. clif_scriptnext(sd, st->oid);
  4251. return SCRIPT_CMD_SUCCESS;
  4252. }
  4253. /// Clears the dialog and continues the script without a next button.
  4254. ///
  4255. /// clear;
  4256. BUILDIN_FUNC(clear)
  4257. {
  4258. TBL_PC* sd;
  4259. if (!script_rid2sd(sd))
  4260. return SCRIPT_CMD_FAILURE;
  4261. clif_scriptclear(sd, st->oid);
  4262. return SCRIPT_CMD_SUCCESS;
  4263. }
  4264. /// Ends the script and displays the button 'close' on the npc dialog.
  4265. /// The dialog is closed when the button is pressed.
  4266. ///
  4267. /// close;
  4268. BUILDIN_FUNC(close)
  4269. {
  4270. TBL_PC* sd;
  4271. if( !script_rid2sd(sd) )
  4272. return SCRIPT_CMD_SUCCESS;
  4273. if( !st->mes_active ) {
  4274. TBL_NPC* nd = map_id2nd(st->oid);
  4275. st->state = END; // Keep backwards compatibility.
  4276. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4277. } else {
  4278. st->state = CLOSE;
  4279. st->mes_active = 0;
  4280. }
  4281. clif_scriptclose(sd, st->oid);
  4282. return SCRIPT_CMD_SUCCESS;
  4283. }
  4284. /// Displays the button 'close' on the npc dialog.
  4285. /// The dialog is closed and the script continues when the button is pressed.
  4286. ///
  4287. /// close2;
  4288. BUILDIN_FUNC(close2)
  4289. {
  4290. TBL_PC* sd;
  4291. if( !script_rid2sd(sd) )
  4292. return SCRIPT_CMD_SUCCESS;
  4293. st->state = STOP;
  4294. if( st->mes_active )
  4295. st->mes_active = 0;
  4296. clif_scriptclose(sd, st->oid);
  4297. return SCRIPT_CMD_SUCCESS;
  4298. }
  4299. /// Counts the number of valid and total number of options in 'str'
  4300. /// If max_count > 0 the counting stops when that valid option is reached
  4301. /// total is incremented for each option (NULL is supported)
  4302. static int menu_countoptions(const char* str, int max_count, int* total)
  4303. {
  4304. int count = 0;
  4305. int bogus_total;
  4306. if( total == NULL )
  4307. total = &bogus_total;
  4308. ++(*total);
  4309. // initial empty options
  4310. while( *str == ':' )
  4311. {
  4312. ++str;
  4313. ++(*total);
  4314. }
  4315. // count menu options
  4316. while( *str != '\0' )
  4317. {
  4318. ++count;
  4319. --max_count;
  4320. if( max_count == 0 )
  4321. break;
  4322. while( *str != ':' && *str != '\0' )
  4323. ++str;
  4324. while( *str == ':' )
  4325. {
  4326. ++str;
  4327. ++(*total);
  4328. }
  4329. }
  4330. return count;
  4331. }
  4332. /// Displays a menu with options and goes to the target label.
  4333. /// The script is stopped if cancel is pressed.
  4334. /// Options with no text are not displayed in the client.
  4335. ///
  4336. /// Options can be grouped together, separated by the character ':' in the text:
  4337. /// ex: menu "A:B:C",L_target;
  4338. /// All these options go to the specified target label.
  4339. ///
  4340. /// The index of the selected option is put in the variable @menu.
  4341. /// Indexes start with 1 and are consistent with grouped and empty options.
  4342. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4343. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4344. ///
  4345. /// NOTE: the client closes the npc dialog when cancel is pressed
  4346. ///
  4347. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4348. BUILDIN_FUNC(menu)
  4349. {
  4350. int i;
  4351. const char* text;
  4352. TBL_PC* sd;
  4353. if( !script_rid2sd(sd) )
  4354. return SCRIPT_CMD_SUCCESS;
  4355. #ifdef SECURE_NPCTIMEOUT
  4356. sd->npc_idle_type = NPCT_MENU;
  4357. #endif
  4358. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4359. if( sd->state.menu_or_input == 0 )
  4360. {
  4361. struct StringBuf buf;
  4362. if( script_lastdata(st) % 2 == 0 )
  4363. {// argument count is not even (1st argument is at index 2)
  4364. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4365. st->state = END;
  4366. return SCRIPT_CMD_FAILURE;
  4367. }
  4368. StringBuf_Init(&buf);
  4369. sd->npc_menu = 0;
  4370. for( i = 2; i < script_lastdata(st); i += 2 )
  4371. {
  4372. struct script_data* data;
  4373. // menu options
  4374. text = script_getstr(st, i);
  4375. // target label
  4376. data = script_getdata(st, i+1);
  4377. if( !data_islabel(data) )
  4378. {// not a label
  4379. StringBuf_Destroy(&buf);
  4380. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4381. script_reportdata(data);
  4382. st->state = END;
  4383. return SCRIPT_CMD_FAILURE;
  4384. }
  4385. // append option(s)
  4386. if( text[0] == '\0' )
  4387. continue;// empty string, ignore
  4388. if( sd->npc_menu > 0 )
  4389. StringBuf_AppendStr(&buf, ":");
  4390. StringBuf_AppendStr(&buf, text);
  4391. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4392. }
  4393. st->state = RERUNLINE;
  4394. sd->state.menu_or_input = 1;
  4395. /**
  4396. * menus beyond this length crash the client (see bugreport:6402)
  4397. **/
  4398. if( StringBuf_Length(&buf) >= 2047 ) {
  4399. struct npc_data * nd = map_id2nd(st->oid);
  4400. char* menu;
  4401. CREATE(menu, char, 2048);
  4402. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4403. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4404. clif_scriptmenu(sd, st->oid, menu);
  4405. aFree(menu);
  4406. } else
  4407. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4408. StringBuf_Destroy(&buf);
  4409. if( sd->npc_menu >= 0xff )
  4410. {// 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
  4411. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4412. script_reportsrc(st);
  4413. }
  4414. }
  4415. else if( sd->npc_menu == 0xff )
  4416. {// Cancel was pressed
  4417. sd->state.menu_or_input = 0;
  4418. st->state = END;
  4419. }
  4420. else
  4421. {// goto target label
  4422. int menu = 0;
  4423. sd->state.menu_or_input = 0;
  4424. if( sd->npc_menu <= 0 )
  4425. {
  4426. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4427. st->state = END;
  4428. return SCRIPT_CMD_FAILURE;
  4429. }
  4430. // get target label
  4431. for( i = 2; i < script_lastdata(st); i += 2 )
  4432. {
  4433. text = script_getstr(st, i);
  4434. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4435. if( sd->npc_menu <= 0 )
  4436. break;// entry found
  4437. }
  4438. if( sd->npc_menu > 0 )
  4439. {// Invalid selection
  4440. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4441. st->state = END;
  4442. return SCRIPT_CMD_FAILURE;
  4443. }
  4444. if( !data_islabel(script_getdata(st, i + 1)) )
  4445. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4446. ShowError("buildin_menu: Unexpected data in label argument\n");
  4447. script_reportdata(script_getdata(st, i + 1));
  4448. st->state = END;
  4449. return SCRIPT_CMD_FAILURE;
  4450. }
  4451. pc_setreg(sd, add_str("@menu"), menu);
  4452. st->pos = script_getnum(st, i + 1);
  4453. st->state = GOTO;
  4454. }
  4455. return SCRIPT_CMD_SUCCESS;
  4456. }
  4457. /// Displays a menu with options and returns the selected option.
  4458. /// Behaves like 'menu' without the target labels.
  4459. ///
  4460. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4461. ///
  4462. /// @see menu
  4463. BUILDIN_FUNC(select)
  4464. {
  4465. int i;
  4466. const char* text;
  4467. TBL_PC* sd;
  4468. if( !script_rid2sd(sd) )
  4469. return SCRIPT_CMD_SUCCESS;
  4470. #ifdef SECURE_NPCTIMEOUT
  4471. sd->npc_idle_type = NPCT_MENU;
  4472. #endif
  4473. if( sd->state.menu_or_input == 0 ) {
  4474. struct StringBuf buf;
  4475. StringBuf_Init(&buf);
  4476. sd->npc_menu = 0;
  4477. for( i = 2; i <= script_lastdata(st); ++i ) {
  4478. text = script_getstr(st, i);
  4479. if( sd->npc_menu > 0 )
  4480. StringBuf_AppendStr(&buf, ":");
  4481. StringBuf_AppendStr(&buf, text);
  4482. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4483. }
  4484. st->state = RERUNLINE;
  4485. sd->state.menu_or_input = 1;
  4486. /**
  4487. * menus beyond this length crash the client (see bugreport:6402)
  4488. **/
  4489. if( StringBuf_Length(&buf) >= 2047 ) {
  4490. struct npc_data * nd = map_id2nd(st->oid);
  4491. char* menu;
  4492. CREATE(menu, char, 2048);
  4493. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4494. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4495. clif_scriptmenu(sd, st->oid, menu);
  4496. aFree(menu);
  4497. } else
  4498. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4499. StringBuf_Destroy(&buf);
  4500. if( sd->npc_menu >= 0xff ) {
  4501. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4502. script_reportsrc(st);
  4503. }
  4504. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4505. sd->state.menu_or_input = 0;
  4506. st->state = END;
  4507. } else {// return selected option
  4508. int menu = 0;
  4509. sd->state.menu_or_input = 0;
  4510. for( i = 2; i <= script_lastdata(st); ++i ) {
  4511. text = script_getstr(st, i);
  4512. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4513. if( sd->npc_menu <= 0 )
  4514. break;// entry found
  4515. }
  4516. pc_setreg(sd, add_str("@menu"), menu);
  4517. script_pushint(st, menu);
  4518. st->state = RUN;
  4519. }
  4520. return SCRIPT_CMD_SUCCESS;
  4521. }
  4522. /// Displays a menu with options and returns the selected option.
  4523. /// Behaves like 'menu' without the target labels, except when cancel is
  4524. /// pressed.
  4525. /// When cancel is pressed, the script continues and 255 is returned.
  4526. ///
  4527. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4528. ///
  4529. /// @see menu
  4530. BUILDIN_FUNC(prompt)
  4531. {
  4532. int i;
  4533. const char *text;
  4534. TBL_PC* sd;
  4535. if( !script_rid2sd(sd) )
  4536. return SCRIPT_CMD_SUCCESS;
  4537. #ifdef SECURE_NPCTIMEOUT
  4538. sd->npc_idle_type = NPCT_MENU;
  4539. #endif
  4540. if( sd->state.menu_or_input == 0 )
  4541. {
  4542. struct StringBuf buf;
  4543. StringBuf_Init(&buf);
  4544. sd->npc_menu = 0;
  4545. for( i = 2; i <= script_lastdata(st); ++i )
  4546. {
  4547. text = script_getstr(st, i);
  4548. if( sd->npc_menu > 0 )
  4549. StringBuf_AppendStr(&buf, ":");
  4550. StringBuf_AppendStr(&buf, text);
  4551. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4552. }
  4553. st->state = RERUNLINE;
  4554. sd->state.menu_or_input = 1;
  4555. /**
  4556. * menus beyond this length crash the client (see bugreport:6402)
  4557. **/
  4558. if( StringBuf_Length(&buf) >= 2047 ) {
  4559. struct npc_data * nd = map_id2nd(st->oid);
  4560. char* menu;
  4561. CREATE(menu, char, 2048);
  4562. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4563. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4564. clif_scriptmenu(sd, st->oid, menu);
  4565. aFree(menu);
  4566. } else
  4567. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4568. StringBuf_Destroy(&buf);
  4569. if( sd->npc_menu >= 0xff )
  4570. {
  4571. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4572. script_reportsrc(st);
  4573. }
  4574. }
  4575. else if( sd->npc_menu == 0xff )
  4576. {// Cancel was pressed
  4577. sd->state.menu_or_input = 0;
  4578. pc_setreg(sd, add_str("@menu"), 0xff);
  4579. script_pushint(st, 0xff);
  4580. st->state = RUN;
  4581. }
  4582. else
  4583. {// return selected option
  4584. int menu = 0;
  4585. sd->state.menu_or_input = 0;
  4586. for( i = 2; i <= script_lastdata(st); ++i )
  4587. {
  4588. text = script_getstr(st, i);
  4589. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4590. if( sd->npc_menu <= 0 )
  4591. break;// entry found
  4592. }
  4593. pc_setreg(sd, add_str("@menu"), menu);
  4594. script_pushint(st, menu);
  4595. st->state = RUN;
  4596. }
  4597. return SCRIPT_CMD_SUCCESS;
  4598. }
  4599. /////////////////////////////////////////////////////////////////////
  4600. // ...
  4601. //
  4602. /// Jumps to the target script label.
  4603. ///
  4604. /// goto <label>;
  4605. BUILDIN_FUNC(goto)
  4606. {
  4607. if( !data_islabel(script_getdata(st,2)) )
  4608. {
  4609. ShowError("buildin_goto: Not a label\n");
  4610. script_reportdata(script_getdata(st,2));
  4611. st->state = END;
  4612. return SCRIPT_CMD_FAILURE;
  4613. }
  4614. st->pos = script_getnum(st,2);
  4615. st->state = GOTO;
  4616. return SCRIPT_CMD_SUCCESS;
  4617. }
  4618. /*==========================================
  4619. * user-defined function call
  4620. *------------------------------------------*/
  4621. BUILDIN_FUNC(callfunc)
  4622. {
  4623. int i, j;
  4624. struct script_retinfo* ri;
  4625. struct script_code* scr;
  4626. const char* str = script_getstr(st,2);
  4627. struct reg_db *ref = NULL;
  4628. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4629. if(!scr) {
  4630. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4631. st->state = END;
  4632. return SCRIPT_CMD_FAILURE;
  4633. }
  4634. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4635. ref[0].vars = st->stack->scope.vars;
  4636. if (!st->stack->scope.arrays)
  4637. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4638. ref[0].arrays = st->stack->scope.arrays;
  4639. ref[1].vars = st->script->local.vars;
  4640. if (!st->script->local.arrays)
  4641. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4642. ref[1].arrays = st->script->local.arrays;
  4643. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4644. struct script_data* data = push_copy(st->stack,i);
  4645. if (data_isreference(data) && !data->ref) {
  4646. const char* name = reference_getname(data);
  4647. if (name[0] == '.')
  4648. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4649. }
  4650. }
  4651. CREATE(ri, struct script_retinfo, 1);
  4652. ri->script = st->script; // script code
  4653. ri->scope.vars = st->stack->scope.vars; // scope variables
  4654. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4655. ri->pos = st->pos; // script location
  4656. ri->nargs = j; // argument count
  4657. ri->defsp = st->stack->defsp; // default stack pointer
  4658. push_retinfo(st->stack, ri, ref);
  4659. st->pos = 0;
  4660. st->script = scr;
  4661. st->stack->defsp = st->stack->sp;
  4662. st->state = GOTO;
  4663. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4664. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4665. if (!st->script->local.vars)
  4666. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4667. return SCRIPT_CMD_SUCCESS;
  4668. }
  4669. /*==========================================
  4670. * subroutine call
  4671. *------------------------------------------*/
  4672. BUILDIN_FUNC(callsub)
  4673. {
  4674. int i,j;
  4675. struct script_retinfo* ri;
  4676. int pos = script_getnum(st,2);
  4677. struct reg_db *ref = NULL;
  4678. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4679. ShowError("buildin_callsub: Argument is not a label\n");
  4680. script_reportdata(script_getdata(st,2));
  4681. st->state = END;
  4682. return SCRIPT_CMD_FAILURE;
  4683. }
  4684. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4685. ref[0].vars = st->stack->scope.vars;
  4686. if (!st->stack->scope.arrays)
  4687. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4688. ref[0].arrays = st->stack->scope.arrays;
  4689. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4690. struct script_data* data = push_copy(st->stack,i);
  4691. if (data_isreference(data) && !data->ref) {
  4692. const char* name = reference_getname(data);
  4693. if (name[0] == '.' && name[1] == '@')
  4694. data->ref = &ref[0];
  4695. }
  4696. }
  4697. CREATE(ri, struct script_retinfo, 1);
  4698. ri->script = st->script; // script code
  4699. ri->scope.vars = st->stack->scope.vars; // scope variables
  4700. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4701. ri->pos = st->pos; // script location
  4702. ri->nargs = j; // argument count
  4703. ri->defsp = st->stack->defsp; // default stack pointer
  4704. push_retinfo(st->stack, ri, ref);
  4705. st->pos = pos;
  4706. st->stack->defsp = st->stack->sp;
  4707. st->state = GOTO;
  4708. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4709. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4710. return SCRIPT_CMD_SUCCESS;
  4711. }
  4712. /// Retrieves an argument provided to callfunc/callsub.
  4713. /// If the argument doesn't exist
  4714. ///
  4715. /// getarg(<index>{,<default_value>}) -> <value>
  4716. BUILDIN_FUNC(getarg)
  4717. {
  4718. struct script_retinfo* ri;
  4719. int idx;
  4720. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4721. {
  4722. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4723. st->state = END;
  4724. return SCRIPT_CMD_FAILURE;
  4725. }
  4726. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4727. idx = script_getnum(st,2);
  4728. if( idx >= 0 && idx < ri->nargs )
  4729. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4730. else if( script_hasdata(st,3) )
  4731. script_pushcopy(st, 3);
  4732. else
  4733. {
  4734. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4735. st->state = END;
  4736. return SCRIPT_CMD_FAILURE;
  4737. }
  4738. return SCRIPT_CMD_SUCCESS;
  4739. }
  4740. /// Returns from the current function, optionaly returning a value from the functions.
  4741. /// Don't use outside script functions.
  4742. ///
  4743. /// return;
  4744. /// return <value>;
  4745. BUILDIN_FUNC(return)
  4746. {
  4747. if( script_hasdata(st,2) )
  4748. {// return value
  4749. struct script_data* data;
  4750. script_pushcopy(st, 2);
  4751. data = script_getdatatop(st, -1);
  4752. if( data_isreference(data) ) {
  4753. const char* name = reference_getname(data);
  4754. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4755. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4756. get_val(st, data); // current scope, convert to value
  4757. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4758. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4759. }
  4760. }
  4761. }
  4762. else
  4763. {// no return value
  4764. script_pushnil(st);
  4765. }
  4766. st->state = RETFUNC;
  4767. return SCRIPT_CMD_SUCCESS;
  4768. }
  4769. /// Returns a random number from 0 to <range>-1.
  4770. /// Or returns a random number from <min> to <max>.
  4771. /// If <min> is greater than <max>, their numbers are switched.
  4772. /// rand(<range>) -> <int>
  4773. /// rand(<min>,<max>) -> <int>
  4774. BUILDIN_FUNC(rand)
  4775. {
  4776. int range;
  4777. int min;
  4778. if( script_hasdata(st,3) )
  4779. {// min,max
  4780. int max = script_getnum(st,3);
  4781. min = script_getnum(st,2);
  4782. if( max < min )
  4783. SWAP(min, max);
  4784. range = max - min + 1;
  4785. }
  4786. else
  4787. {// range
  4788. min = 0;
  4789. range = script_getnum(st,2);
  4790. }
  4791. if( range <= 1 )
  4792. script_pushint(st, min);
  4793. else
  4794. script_pushint(st, rnd()%range + min);
  4795. return SCRIPT_CMD_SUCCESS;
  4796. }
  4797. /*==========================================
  4798. * Warp sd to str,x,y or Random or SavePoint/Save
  4799. *------------------------------------------*/
  4800. BUILDIN_FUNC(warp)
  4801. {
  4802. int ret;
  4803. int x,y;
  4804. const char* str;
  4805. struct map_session_data* sd;
  4806. if(!script_charid2sd(5, sd))
  4807. return SCRIPT_CMD_SUCCESS;
  4808. str = script_getstr(st,2);
  4809. x = script_getnum(st,3);
  4810. y = script_getnum(st,4);
  4811. if(strcmp(str,"Random")==0)
  4812. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4813. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4814. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4815. else
  4816. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4817. if( ret ) {
  4818. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4819. return SCRIPT_CMD_FAILURE;
  4820. }
  4821. return SCRIPT_CMD_SUCCESS;
  4822. }
  4823. /*==========================================
  4824. * Warp a specified area
  4825. *------------------------------------------*/
  4826. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4827. {
  4828. int x2,y2,x3,y3;
  4829. unsigned int index;
  4830. index = va_arg(ap,unsigned int);
  4831. x2 = va_arg(ap,int);
  4832. y2 = va_arg(ap,int);
  4833. x3 = va_arg(ap,int);
  4834. y3 = va_arg(ap,int);
  4835. if(index == 0)
  4836. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4837. else if(x3 && y3) {
  4838. int max, tx, ty, j = 0;
  4839. int16 m;
  4840. m = map_mapindex2mapid(index);
  4841. // choose a suitable max number of attempts
  4842. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4843. max = 1000;
  4844. // find a suitable map cell
  4845. do {
  4846. tx = rnd()%(x3-x2+1)+x2;
  4847. ty = rnd()%(y3-y2+1)+y2;
  4848. j++;
  4849. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4850. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4851. }
  4852. else
  4853. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4854. return 0;
  4855. }
  4856. BUILDIN_FUNC(areawarp)
  4857. {
  4858. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4859. unsigned int index;
  4860. const char *str;
  4861. const char *mapname;
  4862. mapname = script_getstr(st,2);
  4863. x0 = script_getnum(st,3);
  4864. y0 = script_getnum(st,4);
  4865. x1 = script_getnum(st,5);
  4866. y1 = script_getnum(st,6);
  4867. str = script_getstr(st,7);
  4868. x2 = script_getnum(st,8);
  4869. y2 = script_getnum(st,9);
  4870. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4871. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4872. x3 = 0;
  4873. y3 = 0;
  4874. } else if( x3 && y3 ) {
  4875. // normalize x3/y3 coordinates
  4876. if( x3 < x2 ) SWAP(x3,x2);
  4877. if( y3 < y2 ) SWAP(y3,y2);
  4878. }
  4879. }
  4880. if( (m = map_mapname2mapid(mapname)) < 0 )
  4881. return SCRIPT_CMD_FAILURE;
  4882. if( strcmp(str,"Random") == 0 )
  4883. index = 0;
  4884. else if( !(index=mapindex_name2id(str)) )
  4885. return SCRIPT_CMD_FAILURE;
  4886. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4887. return SCRIPT_CMD_SUCCESS;
  4888. }
  4889. /*==========================================
  4890. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4891. *------------------------------------------*/
  4892. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4893. {
  4894. int hp, sp;
  4895. hp = va_arg(ap, int);
  4896. sp = va_arg(ap, int);
  4897. pc_percentheal((TBL_PC *)bl,hp,sp);
  4898. return 0;
  4899. }
  4900. BUILDIN_FUNC(areapercentheal)
  4901. {
  4902. int hp,sp,m;
  4903. const char *mapname;
  4904. int x0,y0,x1,y1;
  4905. mapname=script_getstr(st,2);
  4906. x0=script_getnum(st,3);
  4907. y0=script_getnum(st,4);
  4908. x1=script_getnum(st,5);
  4909. y1=script_getnum(st,6);
  4910. hp=script_getnum(st,7);
  4911. sp=script_getnum(st,8);
  4912. if( (m=map_mapname2mapid(mapname))< 0)
  4913. return SCRIPT_CMD_FAILURE;
  4914. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4915. return SCRIPT_CMD_SUCCESS;
  4916. }
  4917. /*==========================================
  4918. * Warpparty - [Fredzilla] [Paradox924X]
  4919. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  4920. * If 'from_mapname' is specified, only the party members on that map will be warped
  4921. *------------------------------------------*/
  4922. BUILDIN_FUNC(warpparty)
  4923. {
  4924. TBL_PC *sd = NULL;
  4925. TBL_PC *pl_sd;
  4926. struct party_data* p;
  4927. int type, mapindex = 0, m = -1, i, rx = 0, ry = 0;
  4928. const char* str = script_getstr(st,2);
  4929. int x = script_getnum(st,3);
  4930. int y = script_getnum(st,4);
  4931. int p_id = script_getnum(st,5);
  4932. const char* str2 = NULL;
  4933. if ( script_hasdata(st,6) )
  4934. str2 = script_getstr(st,6);
  4935. if (script_hasdata(st, 7))
  4936. rx = script_getnum(st, 7);
  4937. if (script_hasdata(st, 8))
  4938. ry = script_getnum(st, 8);
  4939. p = party_search(p_id);
  4940. if(!p)
  4941. return SCRIPT_CMD_SUCCESS;
  4942. type = ( strcmp(str,"Random")==0 ) ? 0
  4943. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4944. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4945. : ( strcmp(str,"Leader")==0 ) ? 3
  4946. : 4;
  4947. switch (type)
  4948. {
  4949. case 3:
  4950. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4951. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4952. return SCRIPT_CMD_FAILURE;
  4953. pl_sd = p->data[i].sd;
  4954. mapindex = pl_sd->mapindex;
  4955. m = map_mapindex2mapid(mapindex);
  4956. x = pl_sd->bl.x;
  4957. y = pl_sd->bl.y;
  4958. break;
  4959. case 4:
  4960. mapindex = mapindex_name2id(str);
  4961. if (!mapindex) {// Invalid map
  4962. return SCRIPT_CMD_FAILURE;
  4963. }
  4964. m = map_mapindex2mapid(mapindex);
  4965. break;
  4966. case 2:
  4967. //"SavePoint" uses save point of the currently attached player
  4968. if ( !script_rid2sd(sd) )
  4969. return SCRIPT_CMD_SUCCESS;
  4970. break;
  4971. }
  4972. for (i = 0; i < MAX_PARTY; i++)
  4973. {
  4974. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4975. continue;
  4976. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4977. continue;
  4978. if( pc_isdead(pl_sd) )
  4979. continue;
  4980. switch( type )
  4981. {
  4982. case 0: // Random
  4983. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  4984. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4985. break;
  4986. case 1: // SavePointAll
  4987. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  4988. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4989. break;
  4990. case 2: // SavePoint
  4991. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  4992. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4993. break;
  4994. case 3: // Leader
  4995. if (p->party.member[i].leader)
  4996. continue;
  4997. case 4: // m,x,y
  4998. if (rx || ry) {
  4999. int x1 = x + rx, y1 = y + ry,
  5000. x0 = x - rx, y0 = y - ry;
  5001. uint8 attempts = 10;
  5002. do {
  5003. x = x0 + rnd()%(x1 - x0 + 1);
  5004. y = y0 + rnd()%(y1 - y0 + 1);
  5005. } while ((--attempts) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  5006. }
  5007. 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))
  5008. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5009. break;
  5010. }
  5011. }
  5012. return SCRIPT_CMD_SUCCESS;
  5013. }
  5014. /*==========================================
  5015. * Warpguild - [Fredzilla]
  5016. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5017. *------------------------------------------*/
  5018. BUILDIN_FUNC(warpguild)
  5019. {
  5020. TBL_PC *sd = NULL;
  5021. TBL_PC *pl_sd;
  5022. struct guild* g;
  5023. struct s_mapiterator* iter;
  5024. int type, mapindex = 0, m = -1;
  5025. const char* str = script_getstr(st,2);
  5026. int x = script_getnum(st,3);
  5027. int y = script_getnum(st,4);
  5028. int gid = script_getnum(st,5);
  5029. g = guild_search(gid);
  5030. if( g == NULL )
  5031. return SCRIPT_CMD_SUCCESS;
  5032. type = ( strcmp(str,"Random")==0 ) ? 0
  5033. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5034. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5035. : 3;
  5036. if( type == 2 && !script_rid2sd(sd) )
  5037. {// "SavePoint" uses save point of the currently attached player
  5038. return SCRIPT_CMD_SUCCESS;
  5039. }
  5040. switch (type) {
  5041. case 3:
  5042. mapindex = mapindex_name2id(str);
  5043. if (!mapindex)
  5044. return SCRIPT_CMD_FAILURE;
  5045. m = map_mapindex2mapid(mapindex);
  5046. break;
  5047. }
  5048. iter = mapit_getallusers();
  5049. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5050. {
  5051. if( pl_sd->status.guild_id != gid )
  5052. continue;
  5053. switch( type )
  5054. {
  5055. case 0: // Random
  5056. if(!map_getmapflag(pl_sd->bl.m, MF_NOWARP))
  5057. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5058. break;
  5059. case 1: // SavePointAll
  5060. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5061. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5062. break;
  5063. case 2: // SavePoint
  5064. if(!map_getmapflag(pl_sd->bl.m, MF_NORETURN))
  5065. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5066. break;
  5067. case 3: // m,x,y
  5068. 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))
  5069. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5070. break;
  5071. }
  5072. }
  5073. mapit_free(iter);
  5074. return SCRIPT_CMD_SUCCESS;
  5075. }
  5076. /*==========================================
  5077. * Force Heal a player (hp and sp)
  5078. *------------------------------------------*/
  5079. BUILDIN_FUNC(heal)
  5080. {
  5081. TBL_PC *sd;
  5082. int hp,sp;
  5083. if (!script_charid2sd(4,sd))
  5084. return SCRIPT_CMD_SUCCESS;
  5085. hp=script_getnum(st,2);
  5086. sp=script_getnum(st,3);
  5087. status_heal(&sd->bl, hp, sp, 1);
  5088. return SCRIPT_CMD_SUCCESS;
  5089. }
  5090. /*==========================================
  5091. * Heal a player by item (get vit bonus etc)
  5092. *------------------------------------------*/
  5093. BUILDIN_FUNC(itemheal)
  5094. {
  5095. TBL_PC *sd;
  5096. int hp,sp;
  5097. hp=script_getnum(st,2);
  5098. sp=script_getnum(st,3);
  5099. if(potion_flag==1) {
  5100. potion_hp = hp;
  5101. potion_sp = sp;
  5102. return SCRIPT_CMD_SUCCESS;
  5103. }
  5104. if (!script_charid2sd(4,sd))
  5105. return SCRIPT_CMD_SUCCESS;
  5106. pc_itemheal(sd,sd->itemid,hp,sp);
  5107. return SCRIPT_CMD_SUCCESS;
  5108. }
  5109. /*==========================================
  5110. *
  5111. *------------------------------------------*/
  5112. BUILDIN_FUNC(percentheal)
  5113. {
  5114. int hp,sp;
  5115. TBL_PC* sd;
  5116. hp=script_getnum(st,2);
  5117. sp=script_getnum(st,3);
  5118. if(potion_flag==1) {
  5119. potion_per_hp = hp;
  5120. potion_per_sp = sp;
  5121. return SCRIPT_CMD_SUCCESS;
  5122. }
  5123. if (!script_charid2sd(4,sd))
  5124. return SCRIPT_CMD_SUCCESS;
  5125. #ifdef RENEWAL
  5126. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5127. sp = 0;
  5128. #endif
  5129. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5130. hp = 0;
  5131. sp = 0;
  5132. }
  5133. if (sd->sc.data[SC_BITESCAR])
  5134. hp = 0;
  5135. pc_percentheal(sd,hp,sp);
  5136. return SCRIPT_CMD_SUCCESS;
  5137. }
  5138. /*==========================================
  5139. *
  5140. *------------------------------------------*/
  5141. BUILDIN_FUNC(jobchange)
  5142. {
  5143. int job, upper=-1;
  5144. job=script_getnum(st,2);
  5145. if( script_hasdata(st,3) )
  5146. upper=script_getnum(st,3);
  5147. if (pcdb_checkid(job))
  5148. {
  5149. TBL_PC* sd;
  5150. if (!script_charid2sd(4,sd))
  5151. return SCRIPT_CMD_SUCCESS;
  5152. pc_jobchange(sd, job, upper);
  5153. }
  5154. return SCRIPT_CMD_SUCCESS;
  5155. }
  5156. /*==========================================
  5157. *
  5158. *------------------------------------------*/
  5159. BUILDIN_FUNC(jobname)
  5160. {
  5161. int class_=script_getnum(st,2);
  5162. script_pushconststr(st, (char*)job_name(class_));
  5163. return SCRIPT_CMD_SUCCESS;
  5164. }
  5165. /// Get input from the player.
  5166. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5167. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5168. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5169. /// shorter than 'min' and 0 otherwise.
  5170. ///
  5171. /// input(<var>{,<min>{,<max>}}) -> <int>
  5172. BUILDIN_FUNC(input)
  5173. {
  5174. TBL_PC* sd;
  5175. struct script_data* data;
  5176. int64 uid;
  5177. const char* name;
  5178. int min;
  5179. int max;
  5180. if( !script_rid2sd(sd) )
  5181. return SCRIPT_CMD_SUCCESS;
  5182. data = script_getdata(st,2);
  5183. if( !data_isreference(data) ){
  5184. ShowError("script:input: not a variable\n");
  5185. script_reportdata(data);
  5186. st->state = END;
  5187. return SCRIPT_CMD_FAILURE;
  5188. }
  5189. uid = reference_getuid(data);
  5190. name = reference_getname(data);
  5191. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5192. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5193. #ifdef SECURE_NPCTIMEOUT
  5194. sd->npc_idle_type = NPCT_WAIT;
  5195. #endif
  5196. if( !sd->state.menu_or_input )
  5197. { // first invocation, display npc input box
  5198. sd->state.menu_or_input = 1;
  5199. st->state = RERUNLINE;
  5200. if( is_string_variable(name) )
  5201. clif_scriptinputstr(sd,st->oid);
  5202. else
  5203. clif_scriptinput(sd,st->oid);
  5204. }
  5205. else
  5206. { // take received text/value and store it in the designated variable
  5207. sd->state.menu_or_input = 0;
  5208. if( is_string_variable(name) )
  5209. {
  5210. int len = (int)strlen(sd->npc_str);
  5211. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5212. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5213. }
  5214. else
  5215. {
  5216. int amount = sd->npc_amount;
  5217. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5218. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5219. }
  5220. st->state = RUN;
  5221. }
  5222. return SCRIPT_CMD_SUCCESS;
  5223. }
  5224. // declare the copyarray method here for future reference
  5225. BUILDIN_FUNC(copyarray);
  5226. /// Sets the value of a variable.
  5227. /// The value is converted to the type of the variable.
  5228. ///
  5229. /// set(<variable>,<value>{,<charid>})
  5230. BUILDIN_FUNC(setr)
  5231. {
  5232. TBL_PC* sd = NULL;
  5233. struct script_data* data;
  5234. //struct script_data* datavalue;
  5235. int64 num;
  5236. uint8 pos = 4;
  5237. const char* name, *command = script_getfuncname(st);
  5238. char prefix;
  5239. data = script_getdata(st,2);
  5240. //datavalue = script_getdata(st,3);
  5241. if( !data_isreference(data) )
  5242. {
  5243. ShowError("script:set: not a variable\n");
  5244. script_reportdata(script_getdata(st,2));
  5245. st->state = END;
  5246. return SCRIPT_CMD_FAILURE;
  5247. }
  5248. num = reference_getuid(data);
  5249. name = reference_getname(data);
  5250. prefix = *name;
  5251. if (!strcmp(command, "setr"))
  5252. pos = 5;
  5253. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5254. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5255. return SCRIPT_CMD_FAILURE;
  5256. }
  5257. #if 0
  5258. if( data_isreference(datavalue) )
  5259. {// the value being referenced is a variable
  5260. const char* namevalue = reference_getname(datavalue);
  5261. if( !not_array_variable(*namevalue) )
  5262. {// array variable being copied into another array variable
  5263. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5264. {// player must be attached in order to copy a player variable
  5265. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5266. return SCRIPT_CMD_SUCCESS;
  5267. }
  5268. if( is_string_variable(namevalue) != is_string_variable(name) )
  5269. {// non-matching array value types
  5270. ShowWarning("script:set: two array variables do not match in type.\n");
  5271. return SCRIPT_CMD_SUCCESS;
  5272. }
  5273. // push the maximum number of array values to the stack
  5274. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5275. // call the copy array method directly
  5276. return buildin_copyarray(st);
  5277. }
  5278. }
  5279. #endif
  5280. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5281. if( is_string_variable(name) )
  5282. script_pushstrcopy(st,script_getstr(st, 4));
  5283. else
  5284. script_pushint(st,script_getnum(st, 4));
  5285. } else // Return a copy of the variable reference
  5286. script_pushcopy(st, 2);
  5287. if( is_string_variable(name) )
  5288. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5289. else
  5290. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5291. return SCRIPT_CMD_SUCCESS;
  5292. }
  5293. /////////////////////////////////////////////////////////////////////
  5294. /// Array variables
  5295. ///
  5296. /// Sets values of an array, from the starting index.
  5297. /// ex: setarray arr[1],1,2,3;
  5298. ///
  5299. /// setarray <array variable>,<value1>{,<value2>...};
  5300. BUILDIN_FUNC(setarray)
  5301. {
  5302. struct script_data* data;
  5303. const char* name;
  5304. uint32 start;
  5305. uint32 end;
  5306. int32 id;
  5307. int32 i;
  5308. TBL_PC* sd = NULL;
  5309. data = script_getdata(st, 2);
  5310. if( !data_isreference(data) )
  5311. {
  5312. ShowError("script:setarray: not a variable\n");
  5313. script_reportdata(data);
  5314. st->state = END;
  5315. return SCRIPT_CMD_FAILURE;// not a variable
  5316. }
  5317. id = reference_getid(data);
  5318. start = reference_getindex(data);
  5319. name = reference_getname(data);
  5320. if( not_server_variable(*name) )
  5321. {
  5322. if( !script_rid2sd(sd) )
  5323. return SCRIPT_CMD_SUCCESS;// no player attached
  5324. }
  5325. end = start + script_lastdata(st) - 2;
  5326. if( end > SCRIPT_MAX_ARRAYSIZE )
  5327. end = SCRIPT_MAX_ARRAYSIZE;
  5328. if( is_string_variable(name) )
  5329. {// string array
  5330. for( i = 3; start < end; ++start, ++i )
  5331. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5332. }
  5333. else
  5334. {// int array
  5335. for( i = 3; start < end; ++start, ++i )
  5336. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5337. }
  5338. return SCRIPT_CMD_SUCCESS;
  5339. }
  5340. /// Sets count values of an array, from the starting index.
  5341. /// ex: cleararray arr[0],0,1;
  5342. ///
  5343. /// cleararray <array variable>,<value>,<count>;
  5344. BUILDIN_FUNC(cleararray)
  5345. {
  5346. struct script_data* data;
  5347. const char* name;
  5348. uint32 start;
  5349. uint32 end;
  5350. int32 id;
  5351. void* v;
  5352. TBL_PC* sd = NULL;
  5353. data = script_getdata(st, 2);
  5354. if( !data_isreference(data) )
  5355. {
  5356. ShowError("script:cleararray: not a variable\n");
  5357. script_reportdata(data);
  5358. st->state = END;
  5359. return SCRIPT_CMD_FAILURE;// not a variable
  5360. }
  5361. id = reference_getid(data);
  5362. start = reference_getindex(data);
  5363. name = reference_getname(data);
  5364. if( not_server_variable(*name) )
  5365. {
  5366. if( !script_rid2sd(sd) )
  5367. return SCRIPT_CMD_SUCCESS;// no player attached
  5368. }
  5369. if( is_string_variable(name) )
  5370. v = (void*)script_getstr(st, 3);
  5371. else
  5372. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5373. end = start + script_getnum(st, 4);
  5374. if( end > SCRIPT_MAX_ARRAYSIZE )
  5375. end = SCRIPT_MAX_ARRAYSIZE;
  5376. for( ; start < end; ++start )
  5377. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5378. return SCRIPT_CMD_SUCCESS;
  5379. }
  5380. /// Copies data from one array to another.
  5381. /// ex: copyarray arr[0],arr[2],2;
  5382. ///
  5383. /// copyarray <destination array variable>,<source array variable>,<count>;
  5384. BUILDIN_FUNC(copyarray)
  5385. {
  5386. struct script_data* data1;
  5387. struct script_data* data2;
  5388. const char* name1;
  5389. const char* name2;
  5390. int32 idx1;
  5391. int32 idx2;
  5392. int32 id1;
  5393. int32 id2;
  5394. void* v;
  5395. int32 i;
  5396. uint32 count;
  5397. TBL_PC* sd = NULL;
  5398. data1 = script_getdata(st, 2);
  5399. data2 = script_getdata(st, 3);
  5400. if( !data_isreference(data1) || !data_isreference(data2) )
  5401. {
  5402. ShowError("script:copyarray: not a variable\n");
  5403. script_reportdata(data1);
  5404. script_reportdata(data2);
  5405. st->state = END;
  5406. return SCRIPT_CMD_FAILURE;// not a variable
  5407. }
  5408. id1 = reference_getid(data1);
  5409. id2 = reference_getid(data2);
  5410. idx1 = reference_getindex(data1);
  5411. idx2 = reference_getindex(data2);
  5412. name1 = reference_getname(data1);
  5413. name2 = reference_getname(data2);
  5414. if( is_string_variable(name1) != is_string_variable(name2) )
  5415. {
  5416. ShowError("script:copyarray: type mismatch\n");
  5417. script_reportdata(data1);
  5418. script_reportdata(data2);
  5419. st->state = END;
  5420. return SCRIPT_CMD_FAILURE;// data type mismatch
  5421. }
  5422. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5423. {
  5424. if( !script_rid2sd(sd) )
  5425. return SCRIPT_CMD_SUCCESS;// no player attached
  5426. }
  5427. count = script_getnum(st, 4);
  5428. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5429. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5430. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5431. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5432. if( id1 == id2 && idx1 > idx2 )
  5433. {// destination might be overlapping the source - copy in reverse order
  5434. for( i = count - 1; i >= 0; --i )
  5435. {
  5436. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5437. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5438. script_removetop(st, -1, 0);
  5439. }
  5440. }
  5441. else
  5442. {// normal copy
  5443. for( i = 0; i < count; ++i )
  5444. {
  5445. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5446. {
  5447. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5448. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5449. script_removetop(st, -1, 0);
  5450. }
  5451. else// out of range - assume ""/0
  5452. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5453. }
  5454. }
  5455. return SCRIPT_CMD_SUCCESS;
  5456. }
  5457. /// Returns the size of the array.
  5458. /// Assumes that everything before the starting index exists.
  5459. /// ex: getarraysize(arr[3])
  5460. ///
  5461. /// getarraysize(<array variable>) -> <int>
  5462. BUILDIN_FUNC(getarraysize)
  5463. {
  5464. struct script_data* data;
  5465. const char* name;
  5466. struct map_session_data* sd = NULL;
  5467. data = script_getdata(st, 2);
  5468. if( !data_isreference(data) )
  5469. {
  5470. ShowError("script:getarraysize: not a variable\n");
  5471. script_reportdata(data);
  5472. script_pushnil(st);
  5473. st->state = END;
  5474. return SCRIPT_CMD_FAILURE;// not a variable
  5475. }
  5476. name = reference_getname(data);
  5477. if( not_server_variable(*name) ){
  5478. if (!script_rid2sd(sd))
  5479. return SCRIPT_CMD_SUCCESS;// no player attached
  5480. }
  5481. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5482. return SCRIPT_CMD_SUCCESS;
  5483. }
  5484. int script_array_index_cmp(const void *a, const void *b) {
  5485. return ( *(unsigned int*)a - *(unsigned int*)b );
  5486. }
  5487. /// Deletes count or all the elements in an array, from the starting index.
  5488. /// ex: deletearray arr[4],2;
  5489. ///
  5490. /// deletearray <array variable>;
  5491. /// deletearray <array variable>,<count>;
  5492. BUILDIN_FUNC(deletearray)
  5493. {
  5494. struct script_data* data;
  5495. const char* name;
  5496. unsigned int start, end, i;
  5497. int id;
  5498. TBL_PC *sd = NULL;
  5499. struct script_array *sa = NULL;
  5500. struct reg_db *src = NULL;
  5501. void *value;
  5502. data = script_getdata(st, 2);
  5503. if( !data_isreference(data) ) {
  5504. ShowError("script:deletearray: not a variable\n");
  5505. script_reportdata(data);
  5506. st->state = END;
  5507. return SCRIPT_CMD_FAILURE;// not a variable
  5508. }
  5509. id = reference_getid(data);
  5510. start = reference_getindex(data);
  5511. name = reference_getname(data);
  5512. if( not_server_variable(*name) ) {
  5513. if( !script_rid2sd(sd) )
  5514. return SCRIPT_CMD_SUCCESS;// no player attached
  5515. }
  5516. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5517. ShowError("script:deletearray: not a array\n");
  5518. script_reportdata(data);
  5519. st->state = END;
  5520. return SCRIPT_CMD_FAILURE;// not a variable
  5521. }
  5522. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5523. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5524. return SCRIPT_CMD_SUCCESS;// not a variable
  5525. }
  5526. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5527. if( start >= end )
  5528. return SCRIPT_CMD_SUCCESS;// nothing to free
  5529. if( is_string_variable(name) )
  5530. value = (void *)"";
  5531. else
  5532. value = (void *)0;
  5533. if( script_hasdata(st,3) ) {
  5534. unsigned int count = script_getnum(st, 3);
  5535. if( count > end - start )
  5536. count = end - start;
  5537. if( count <= 0 )
  5538. return SCRIPT_CMD_SUCCESS;// nothing to free
  5539. if( end - start < sa->size ) {
  5540. // Better to iterate directly on the array, no speed-up from using sa
  5541. for( ; start + count < end; ++start ) {
  5542. // Compact and overwrite
  5543. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5544. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5545. script_removetop(st, -1, 0);
  5546. }
  5547. for( ; start < end; start++ ) {
  5548. // Clean up any leftovers that weren't overwritten
  5549. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5550. }
  5551. } else {
  5552. // using sa to speed up
  5553. unsigned int *list = NULL, size = 0;
  5554. list = script_array_cpy_list(sa);
  5555. size = sa->size;
  5556. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5557. ARR_FIND(0, size, i, list[i] >= start);
  5558. for( ; i < size && list[i] < start + count; i++ ) {
  5559. // Clear any entries between start and start+count, if they exist
  5560. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5561. }
  5562. for( ; i < size && list[i] < end; i++ ) {
  5563. // Move back count positions any entries between start+count to fill the gaps
  5564. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5565. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5566. script_removetop(st, -1, 0);
  5567. // Clear their originals
  5568. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5569. }
  5570. }
  5571. } else {
  5572. unsigned int *list = NULL, size = 0;
  5573. list = script_array_cpy_list(sa);
  5574. size = sa->size;
  5575. for(i = 0; i < size; i++) {
  5576. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5577. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5578. }
  5579. }
  5580. return SCRIPT_CMD_SUCCESS;
  5581. }
  5582. /// Returns a reference to the target index of the array variable.
  5583. /// Equivalent to var[index].
  5584. ///
  5585. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5586. BUILDIN_FUNC(getelementofarray)
  5587. {
  5588. struct script_data* data;
  5589. int32 id;
  5590. int64 i;
  5591. data = script_getdata(st, 2);
  5592. if( !data_isreference(data) )
  5593. {
  5594. ShowError("script:getelementofarray: not a variable\n");
  5595. script_reportdata(data);
  5596. script_pushnil(st);
  5597. st->state = END;
  5598. return SCRIPT_CMD_SUCCESS;// not a variable
  5599. }
  5600. id = reference_getid(data);
  5601. i = script_getnum(st, 3);
  5602. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5603. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5604. script_reportdata(data);
  5605. script_pushnil(st);
  5606. st->state = END;
  5607. return SCRIPT_CMD_FAILURE;// out of range
  5608. }
  5609. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5610. return SCRIPT_CMD_SUCCESS;
  5611. }
  5612. /// Return the index number of the first matching value in an array.
  5613. /// ex: inarray arr,1;
  5614. ///
  5615. /// inarray <array variable>,<value>;
  5616. BUILDIN_FUNC(inarray)
  5617. {
  5618. struct script_data *data;
  5619. const char* name;
  5620. int id, i, array_size;
  5621. struct map_session_data* sd = NULL;
  5622. struct reg_db *ref = NULL;
  5623. data = script_getdata(st, 2);
  5624. if (!data_isreference(data))
  5625. {
  5626. ShowError("buildin_inarray: not a variable\n");
  5627. script_reportdata(data);
  5628. st->state = END;
  5629. return SCRIPT_CMD_FAILURE;
  5630. }
  5631. name = reference_getname(data);
  5632. ref = reference_getref(data);
  5633. if (not_server_variable(*name) && !script_rid2sd(sd))
  5634. return SCRIPT_CMD_FAILURE;
  5635. array_size = script_array_highest_key(st, sd, name, ref) - 1;
  5636. if (array_size < 0)
  5637. {
  5638. script_pushint(st, -1);
  5639. return SCRIPT_CMD_SUCCESS;
  5640. }
  5641. if (array_size > SCRIPT_MAX_ARRAYSIZE)
  5642. {
  5643. ShowError("buildin_inarray: The array is too large.\n");
  5644. script_reportdata(data);
  5645. st->state = END;
  5646. return SCRIPT_CMD_FAILURE;
  5647. }
  5648. id = reference_getid(data);
  5649. if (is_string_variable(name))
  5650. {
  5651. const char* temp;
  5652. const char* value;
  5653. value = script_getstr(st, 3);
  5654. for (i = 0; i <= array_size; ++i)
  5655. {
  5656. temp = (char*)get_val2(st, reference_uid(id, i), ref);
  5657. script_removetop(st, -1, 0);
  5658. if (!strcmp(temp, value))
  5659. {
  5660. script_pushint(st, i);
  5661. return SCRIPT_CMD_SUCCESS;
  5662. }
  5663. }
  5664. }
  5665. else
  5666. {
  5667. int temp, value;
  5668. value = script_getnum(st, 3);
  5669. for (i = 0; i <= array_size; ++i)
  5670. {
  5671. temp = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, i), ref));
  5672. script_removetop(st, -1, 0);
  5673. if (temp == value)
  5674. {
  5675. script_pushint(st, i);
  5676. return SCRIPT_CMD_SUCCESS;
  5677. }
  5678. }
  5679. }
  5680. script_pushint(st, -1);
  5681. return SCRIPT_CMD_SUCCESS;
  5682. }
  5683. /// Return the number of matches in two arrays.
  5684. /// ex: countinarray arr[0],arr1[0];
  5685. ///
  5686. /// countinarray <array variable>,<array variable>;
  5687. BUILDIN_FUNC(countinarray)
  5688. {
  5689. struct script_data *data1 , *data2;
  5690. const char* name1;
  5691. const char* name2;
  5692. int id1, id2, i, j, array_size1, array_size2, case_count = 0;
  5693. struct map_session_data* sd = NULL;
  5694. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5695. data1 = script_getdata(st, 2);
  5696. data2 = script_getdata(st, 3);
  5697. if (!data_isreference(data1) || !data_isreference(data2))
  5698. {
  5699. ShowError("buildin_countinarray: not a variable\n");
  5700. script_reportdata(data1);
  5701. script_reportdata(data2);
  5702. st->state = END;
  5703. return SCRIPT_CMD_FAILURE;
  5704. }
  5705. name1 = reference_getname(data1);
  5706. name2 = reference_getname(data2);
  5707. ref1 = reference_getref(data1);
  5708. ref2 = reference_getref(data2);
  5709. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5710. return SCRIPT_CMD_FAILURE;
  5711. array_size1 = script_array_highest_key(st, sd, name1, ref1) - 1;
  5712. array_size2 = script_array_highest_key(st, sd, name2, ref2) - 1;
  5713. if (array_size1 < 0 || array_size2 < 0)
  5714. {
  5715. script_pushint(st, 0);
  5716. return SCRIPT_CMD_SUCCESS;
  5717. }
  5718. if (array_size1 > SCRIPT_MAX_ARRAYSIZE || array_size2 > SCRIPT_MAX_ARRAYSIZE)
  5719. {
  5720. ShowError("buildin_countinarray: The array is too large.\n");
  5721. script_reportdata(data1);
  5722. script_reportdata(data2);
  5723. st->state = END;
  5724. return SCRIPT_CMD_FAILURE;
  5725. }
  5726. i = reference_getindex(data1);
  5727. j = reference_getindex(data2);
  5728. if (array_size1 < i || array_size2 < j)
  5729. { //To prevent unintended behavior
  5730. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5731. script_reportdata(data1);
  5732. script_reportdata(data2);
  5733. st->state = END;
  5734. return SCRIPT_CMD_FAILURE;
  5735. }
  5736. id1 = reference_getid(data1);
  5737. id2 = reference_getid(data2);
  5738. if (is_string_variable(name1) && is_string_variable(name2))
  5739. {
  5740. const char* temp1;
  5741. const char* temp2;
  5742. for (; i <= array_size1; ++i)
  5743. {
  5744. temp1 = (char*)get_val2(st, reference_uid(id1, i), ref1);
  5745. for (j = reference_getindex(data2); j <= array_size2; j++)
  5746. {
  5747. temp2 = (char*)get_val2(st, reference_uid(id2, j), ref2);
  5748. if (!strcmp(temp1, temp2))
  5749. {
  5750. case_count++;
  5751. }
  5752. script_removetop(st, -1, 0);
  5753. }
  5754. script_removetop(st, -1, 0);
  5755. }
  5756. }
  5757. else if (!is_string_variable(name1) && !is_string_variable(name2))
  5758. {
  5759. int temp1, temp2;
  5760. for (; i <= array_size1; ++i)
  5761. {
  5762. temp1 = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id1, i), ref1));
  5763. for (j = reference_getindex(data2); j <= array_size2; j++)
  5764. {
  5765. temp2 = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id2, j), ref2));
  5766. if (temp1 == temp2)
  5767. {
  5768. case_count++;
  5769. }
  5770. script_removetop(st, -1, 0);
  5771. }
  5772. script_removetop(st, -1, 0);
  5773. }
  5774. }
  5775. else
  5776. {
  5777. ShowError("buildin_countinarray: Arrays does not match , You can't compare int array to string array.\n");
  5778. script_reportdata(data1);
  5779. script_reportdata(data2);
  5780. st->state = END;
  5781. return SCRIPT_CMD_FAILURE;
  5782. }
  5783. script_pushint(st, case_count);
  5784. return SCRIPT_CMD_SUCCESS;
  5785. }
  5786. /////////////////////////////////////////////////////////////////////
  5787. /// ...
  5788. ///
  5789. /*==========================================
  5790. *
  5791. *------------------------------------------*/
  5792. BUILDIN_FUNC(setlook)
  5793. {
  5794. int type,val;
  5795. TBL_PC* sd;
  5796. type = script_getnum(st,2);
  5797. val = script_getnum(st,3);
  5798. if (!script_charid2sd(4,sd))
  5799. return SCRIPT_CMD_SUCCESS;
  5800. pc_changelook(sd,type,val);
  5801. return SCRIPT_CMD_SUCCESS;
  5802. }
  5803. BUILDIN_FUNC(changelook)
  5804. { // As setlook but only client side
  5805. int type,val;
  5806. TBL_PC* sd;
  5807. type = script_getnum(st,2);
  5808. val = script_getnum(st,3);
  5809. if (!script_charid2sd(4,sd))
  5810. return SCRIPT_CMD_SUCCESS;
  5811. clif_changelook(&sd->bl,type,val);
  5812. return SCRIPT_CMD_SUCCESS;
  5813. }
  5814. /*==========================================
  5815. *
  5816. *------------------------------------------*/
  5817. BUILDIN_FUNC(cutin)
  5818. {
  5819. TBL_PC* sd;
  5820. if( !script_rid2sd(sd) )
  5821. return SCRIPT_CMD_SUCCESS;
  5822. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5823. return SCRIPT_CMD_SUCCESS;
  5824. }
  5825. /*==========================================
  5826. *
  5827. *------------------------------------------*/
  5828. BUILDIN_FUNC(viewpoint)
  5829. {
  5830. int type,x,y,id,color;
  5831. TBL_PC* sd;
  5832. type=script_getnum(st,2);
  5833. x=script_getnum(st,3);
  5834. y=script_getnum(st,4);
  5835. id=script_getnum(st,5);
  5836. color=script_getnum(st,6);
  5837. if( !script_rid2sd(sd) )
  5838. return SCRIPT_CMD_SUCCESS;
  5839. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5840. return SCRIPT_CMD_SUCCESS;
  5841. }
  5842. /**
  5843. * Set random options for new item
  5844. * @param st Script state
  5845. * @param it Temporary item data
  5846. * @param funcname Function name
  5847. * @param x First position of random option id array from the script
  5848. **/
  5849. static int script_getitem_randomoption(struct script_state *st, struct map_session_data* sd, struct item *it, const char *funcname, int x) {
  5850. int i, opt_id_n;
  5851. struct script_data *opt_id = script_getdata(st,x);
  5852. struct script_data *opt_val = script_getdata(st,x+1);
  5853. struct script_data *opt_param = script_getdata(st,x+2);
  5854. const char *opt_id_var = reference_getname(opt_id);
  5855. const char *opt_val_var = reference_getname(opt_val);
  5856. const char *opt_param_var = reference_getname(opt_param);
  5857. int32 opt_id_id, opt_id_idx;
  5858. int32 opt_val_id, opt_val_idx;
  5859. int32 opt_param_id, opt_param_idx;
  5860. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  5861. // Check if the variable requires a player
  5862. if( not_server_variable(opt_id_var[0]) && sd == nullptr ){
  5863. // If no player is attached
  5864. if( !script_rid2sd(sd) ){
  5865. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_id_var );
  5866. return false;
  5867. }
  5868. }
  5869. if( !data_isreference(opt_id) || !script_array_src( st, sd, opt_id_var, reference_getref(opt_id) ) ){
  5870. ShowError( "buildin_%s: The option id parameter is not an array.\n", funcname );
  5871. return SCRIPT_CMD_FAILURE;
  5872. }
  5873. if (is_string_variable(opt_id_var)) {
  5874. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  5875. return SCRIPT_CMD_FAILURE;
  5876. }
  5877. // Check if the variable requires a player
  5878. if( not_server_variable(opt_val_var[0]) && sd == nullptr ){
  5879. // If no player is attached
  5880. if( !script_rid2sd(sd) ){
  5881. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_val_var );
  5882. return false;
  5883. }
  5884. }
  5885. if( !data_isreference(opt_val) || !script_array_src( st, sd, opt_val_var, reference_getref(opt_val) ) ){
  5886. ShowError( "buildin_%s: The option value parameter is not an array.\n", funcname );
  5887. return SCRIPT_CMD_FAILURE;
  5888. }
  5889. if (is_string_variable(opt_val_var)) {
  5890. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  5891. return SCRIPT_CMD_FAILURE;
  5892. }
  5893. // Check if the variable requires a player
  5894. if( not_server_variable(opt_param_var[0]) && sd == nullptr ){
  5895. // If no player is attached
  5896. if( !script_rid2sd(sd) ){
  5897. ShowError( "buildin_%s: variable \"%s\" was not a server variable, but no player was attached.\n", funcname, opt_param_var );
  5898. return false;
  5899. }
  5900. }
  5901. if( !data_isreference(opt_param) || !script_array_src( st, sd, opt_param_var, reference_getref(opt_param) ) ){
  5902. ShowError( "buildin_%s: The option param parameter is not an array.\n", funcname );
  5903. return SCRIPT_CMD_FAILURE;
  5904. }
  5905. if (is_string_variable(opt_param_var)) {
  5906. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  5907. return SCRIPT_CMD_FAILURE;
  5908. }
  5909. opt_id_ref = reference_getref(opt_id);
  5910. opt_id_n = script_array_highest_key(st, sd, opt_id_var, opt_id_ref);
  5911. if (opt_id_n < 1) {
  5912. ShowError("buildin_%s: No option id listed.\n", funcname);
  5913. return SCRIPT_CMD_FAILURE;
  5914. }
  5915. opt_val_ref = reference_getref(opt_val);
  5916. opt_param_ref = reference_getref(opt_param);
  5917. opt_id_id = reference_getid(opt_id);
  5918. opt_val_id = reference_getid(opt_val);
  5919. opt_param_id = reference_getid(opt_param);
  5920. opt_id_idx = reference_getindex(opt_id);
  5921. opt_val_idx = reference_getindex(opt_val);
  5922. opt_param_idx = reference_getindex(opt_param);
  5923. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  5924. it->option[i].id = static_cast<short>((int32)__64BPRTSIZE(get_val2(st,reference_uid(opt_id_id,opt_id_idx+i),opt_id_ref)));
  5925. script_removetop(st, -1, 0);
  5926. it->option[i].value = static_cast<short>((int32)__64BPRTSIZE(get_val2(st,reference_uid(opt_val_id,opt_val_idx+i),opt_val_ref)));
  5927. script_removetop(st, -1, 0);
  5928. it->option[i].param = static_cast<char>((int32)__64BPRTSIZE(get_val2(st,reference_uid(opt_param_id,opt_param_idx+i),opt_param_ref)));
  5929. script_removetop(st, -1, 0);
  5930. }
  5931. return SCRIPT_CMD_SUCCESS;
  5932. }
  5933. /// Returns number of items in inventory/cart/storage
  5934. /// countitem <nameID>{,<accountID>});
  5935. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5936. /// cartcountitem <nameID>{,<accountID>});
  5937. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5938. /// storagecountitem <nameID>{,<accountID>});
  5939. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5940. /// guildstoragecountitem <nameID>{,<accountID>});
  5941. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5942. /// countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>)
  5943. /// countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>)
  5944. BUILDIN_FUNC(countitem)
  5945. {
  5946. int i = 0, aid = 3;
  5947. struct item_data* id = NULL;
  5948. struct script_data* data;
  5949. char *command = (char *)script_getfuncname(st);
  5950. uint8 loc = 0;
  5951. uint16 size, count = 0;
  5952. struct item *items;
  5953. TBL_PC *sd = NULL;
  5954. struct s_storage *gstor = NULL;
  5955. if( command[strlen(command)-1] == '2' ) {
  5956. i = 1;
  5957. aid = 10;
  5958. }
  5959. else if (command[strlen(command)-1] == '3') {
  5960. i = 1;
  5961. aid = 13;
  5962. }
  5963. if( script_hasdata(st,aid) ) {
  5964. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5965. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5966. st->state = END;
  5967. return SCRIPT_CMD_FAILURE;
  5968. }
  5969. }
  5970. else {
  5971. if( !script_rid2sd(sd) )
  5972. return SCRIPT_CMD_SUCCESS;
  5973. }
  5974. if( !strncmp(command, "cart", 4) ) {
  5975. loc = TABLE_CART;
  5976. size = MAX_CART;
  5977. items = sd->cart.u.items_cart;
  5978. }
  5979. else if( !strncmp(command, "storage", 7) ) {
  5980. loc = TABLE_STORAGE;
  5981. size = MAX_STORAGE;
  5982. items = sd->storage.u.items_storage;
  5983. }
  5984. else if( !strncmp(command, "guildstorage", 12) ) {
  5985. gstor = guild2storage2(sd->status.guild_id);
  5986. if (gstor && !sd->state.storage_flag) {
  5987. loc = TABLE_GUILD_STORAGE;
  5988. size = MAX_GUILD_STORAGE;
  5989. items = gstor->u.items_guild;
  5990. } else {
  5991. script_pushint(st, -1);
  5992. return SCRIPT_CMD_SUCCESS;
  5993. }
  5994. }
  5995. else {
  5996. size = MAX_INVENTORY;
  5997. items = sd->inventory.u.items_inventory;
  5998. }
  5999. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  6000. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6001. script_pushint(st,-1);
  6002. return SCRIPT_CMD_SUCCESS;
  6003. }
  6004. data = script_getdata(st, 2);
  6005. get_val(st, data); // Convert into value in case of a variable
  6006. if( data_isstring(data) ) // item name
  6007. id = itemdb_searchname(conv_str(st, data));
  6008. else // item id
  6009. id = itemdb_exists(conv_num(st, data));
  6010. if( id == NULL ) {
  6011. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  6012. script_pushint(st,0);
  6013. return SCRIPT_CMD_FAILURE;
  6014. }
  6015. if (loc == TABLE_GUILD_STORAGE)
  6016. gstor->lock = true;
  6017. if( !i ) { // For count/cart/storagecountitem function
  6018. unsigned short nameid = id->nameid;
  6019. for( i = 0; i < size; i++ )
  6020. if( &items[i] && items[i].nameid == nameid )
  6021. count += items[i].amount;
  6022. }
  6023. else { // For count/cart/storagecountitem2 function
  6024. struct item it;
  6025. bool check_randomopt = false;
  6026. memset(&it, 0, sizeof(it));
  6027. it.nameid = id->nameid;
  6028. it.identify = script_getnum(st,3);
  6029. it.refine = script_getnum(st,4);
  6030. it.attribute = script_getnum(st,5);
  6031. it.card[0] = script_getnum(st,6);
  6032. it.card[1] = script_getnum(st,7);
  6033. it.card[2] = script_getnum(st,8);
  6034. it.card[3] = script_getnum(st,9);
  6035. if (command[strlen(command)-1] == '3') {
  6036. int res = script_getitem_randomoption(st, sd, &it, command, 10);
  6037. if (res != SCRIPT_CMD_SUCCESS)
  6038. return SCRIPT_CMD_FAILURE;
  6039. check_randomopt = true;
  6040. }
  6041. for( i = 0; i < size; i++ ) {
  6042. struct item *itm = &items[i];
  6043. if (!itm || !itm->nameid || itm->amount < 1)
  6044. continue;
  6045. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute)
  6046. continue;
  6047. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6048. continue;
  6049. if (check_randomopt) {
  6050. uint8 j;
  6051. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6052. 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)
  6053. break;
  6054. }
  6055. if (j != MAX_ITEM_RDM_OPT)
  6056. continue;
  6057. }
  6058. count += items[i].amount;
  6059. }
  6060. }
  6061. if (loc == TABLE_GUILD_STORAGE) {
  6062. storage_guild_storageclose(sd);
  6063. gstor->lock = false;
  6064. }
  6065. script_pushint(st, count);
  6066. return SCRIPT_CMD_SUCCESS;
  6067. }
  6068. /*==========================================
  6069. * Check if item with this amount can fit in inventory
  6070. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6071. * Return
  6072. * 0 : fail
  6073. * 1 : success (npc side only)
  6074. *------------------------------------------*/
  6075. BUILDIN_FUNC(checkweight)
  6076. {
  6077. int slots = 0;
  6078. unsigned short amount2 = 0;
  6079. unsigned int weight = 0, i, nbargs;
  6080. struct item_data* id = NULL;
  6081. struct map_session_data* sd;
  6082. if( !script_rid2sd(sd) )
  6083. return SCRIPT_CMD_SUCCESS;
  6084. nbargs = script_lastdata(st)+1;
  6085. if(nbargs%2) {
  6086. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6087. script_pushint(st,0);
  6088. return SCRIPT_CMD_FAILURE;
  6089. }
  6090. slots = pc_inventoryblank(sd); //nb of empty slot
  6091. for (i = 2; i < nbargs; i += 2) {
  6092. unsigned short nameid, amount;
  6093. struct script_data* data = script_getdata(st,i);
  6094. get_val(st, data); // Convert into value in case of a variable
  6095. if( data_isstring(data) ) // item name
  6096. id = itemdb_searchname(conv_str(st, data));
  6097. else // item id
  6098. id = itemdb_exists(conv_num(st, data));
  6099. if( id == NULL ) {
  6100. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6101. script_pushint(st,0);
  6102. return SCRIPT_CMD_FAILURE;
  6103. }
  6104. nameid = id->nameid;
  6105. amount = script_getnum(st,i+1);
  6106. if( amount < 1 ) {
  6107. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6108. script_pushint(st,0);
  6109. return SCRIPT_CMD_FAILURE;
  6110. }
  6111. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6112. if( weight + sd->weight > sd->max_weight )
  6113. {// too heavy
  6114. script_pushint(st,0);
  6115. return SCRIPT_CMD_SUCCESS;
  6116. }
  6117. switch( pc_checkadditem(sd, nameid, amount) ) {
  6118. case CHKADDITEM_EXIST:
  6119. // item is already in inventory, but there is still space for the requested amount
  6120. break;
  6121. case CHKADDITEM_NEW:
  6122. if( itemdb_isstackable(nameid) ) {
  6123. // stackable
  6124. amount2++;
  6125. if( slots < amount2 ) {
  6126. script_pushint(st,0);
  6127. return SCRIPT_CMD_SUCCESS;
  6128. }
  6129. } else {
  6130. // non-stackable
  6131. amount2 += amount;
  6132. if( slots < amount2) {
  6133. script_pushint(st,0);
  6134. return SCRIPT_CMD_SUCCESS;
  6135. }
  6136. }
  6137. break;
  6138. case CHKADDITEM_OVERAMOUNT:
  6139. script_pushint(st,0);
  6140. return SCRIPT_CMD_SUCCESS;
  6141. }
  6142. }
  6143. script_pushint(st,1);
  6144. return SCRIPT_CMD_SUCCESS;
  6145. }
  6146. BUILDIN_FUNC(checkweight2)
  6147. {
  6148. //variable sub checkweight
  6149. int i = 0, slots = 0, weight = 0;
  6150. short fail = 0;
  6151. unsigned short amount2 = 0;
  6152. //variable for array parsing
  6153. struct script_data* data_it;
  6154. struct script_data* data_nb;
  6155. const char* name_it;
  6156. const char* name_nb;
  6157. int32 id_it, id_nb;
  6158. int32 idx_it, idx_nb;
  6159. int nb_it, nb_nb; //array size
  6160. TBL_PC *sd;
  6161. if( !script_rid2sd(sd) )
  6162. return SCRIPT_CMD_SUCCESS;
  6163. data_it = script_getdata(st, 2);
  6164. data_nb = script_getdata(st, 3);
  6165. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6166. ShowError("buildin_checkweight2: parameter not a variable\n");
  6167. script_pushint(st,0);
  6168. return SCRIPT_CMD_FAILURE;// not a variable
  6169. }
  6170. id_it = reference_getid(data_it);
  6171. id_nb = reference_getid(data_nb);
  6172. idx_it = reference_getindex(data_it);
  6173. idx_nb = reference_getindex(data_nb);
  6174. name_it = reference_getname(data_it);
  6175. name_nb = reference_getname(data_nb);
  6176. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6177. ShowError("buildin_checkweight2: illegal type, need int\n");
  6178. script_pushint(st,0);
  6179. return SCRIPT_CMD_FAILURE;// not supported
  6180. }
  6181. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6182. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6183. if(nb_it != nb_nb) {
  6184. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6185. fail = 1;
  6186. }
  6187. slots = pc_inventoryblank(sd);
  6188. for(i=0; i<nb_it; i++) {
  6189. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  6190. script_removetop(st, -1, 0);
  6191. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  6192. script_removetop(st, -1, 0);
  6193. if(fail)
  6194. continue; //cpntonie to depop rest
  6195. if(itemdb_exists(nameid) == NULL ) {
  6196. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  6197. fail=1;
  6198. continue;
  6199. }
  6200. if(amount < 0 ) {
  6201. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6202. fail = 1;
  6203. continue;
  6204. }
  6205. weight += itemdb_weight(nameid)*amount;
  6206. if( weight + sd->weight > sd->max_weight ) {
  6207. fail = 1;
  6208. continue;
  6209. }
  6210. switch( pc_checkadditem(sd, nameid, amount) ) {
  6211. case CHKADDITEM_EXIST:
  6212. // item is already in inventory, but there is still space for the requested amount
  6213. break;
  6214. case CHKADDITEM_NEW:
  6215. if( itemdb_isstackable(nameid) ) {// stackable
  6216. amount2++;
  6217. if( slots < amount2 )
  6218. fail = 1;
  6219. } else {// non-stackable
  6220. amount2 += amount;
  6221. if( slots < amount2 ) {
  6222. fail = 1;
  6223. }
  6224. }
  6225. break;
  6226. case CHKADDITEM_OVERAMOUNT:
  6227. fail = 1;
  6228. } //end switch
  6229. } //end loop DO NOT break it prematurly we need to depop all stack
  6230. fail ? script_pushint(st,0) : script_pushint(st,1);
  6231. return SCRIPT_CMD_SUCCESS;
  6232. }
  6233. /*==========================================
  6234. * getitem <item id>,<amount>{,<account ID>};
  6235. * getitem "<item name>",<amount>{,<account ID>};
  6236. *
  6237. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6238. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6239. * Type:
  6240. * 0 - No bound
  6241. * 1 - Account Bound
  6242. * 2 - Guild Bound
  6243. * 3 - Party Bound
  6244. * 4 - Character Bound
  6245. *------------------------------------------*/
  6246. BUILDIN_FUNC(getitem)
  6247. {
  6248. int get_count, i;
  6249. unsigned short nameid, amount;
  6250. struct item it;
  6251. TBL_PC *sd;
  6252. struct script_data *data;
  6253. unsigned char flag = 0;
  6254. const char* command = script_getfuncname(st);
  6255. struct item_data *id = NULL;
  6256. data = script_getdata(st,2);
  6257. get_val(st,data);
  6258. if( data_isstring(data) ) {// "<item name>"
  6259. const char *name = conv_str(st,data);
  6260. id = itemdb_searchname(name);
  6261. if( id == NULL ){
  6262. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6263. return SCRIPT_CMD_FAILURE; //No item created.
  6264. }
  6265. nameid = id->nameid;
  6266. } else if( data_isint(data) ) {// <item id>
  6267. nameid = conv_num(st,data);
  6268. if( !(id = itemdb_exists(nameid)) ){
  6269. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6270. return SCRIPT_CMD_FAILURE; //No item created.
  6271. }
  6272. } else {
  6273. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6274. return SCRIPT_CMD_FAILURE;
  6275. }
  6276. // <amount>
  6277. if( (amount = script_getnum(st,3)) <= 0)
  6278. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6279. memset(&it,0,sizeof(it));
  6280. it.nameid = nameid;
  6281. it.identify = 1;
  6282. it.bound = BOUND_NONE;
  6283. if( !strcmp(command,"getitembound") ) {
  6284. char bound = script_getnum(st,4);
  6285. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6286. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6287. return SCRIPT_CMD_FAILURE;
  6288. }
  6289. script_mapid2sd(5,sd);
  6290. it.bound = bound;
  6291. } else {
  6292. script_mapid2sd(4,sd);
  6293. }
  6294. if( sd == NULL ) // no target
  6295. return SCRIPT_CMD_SUCCESS;
  6296. //Check if it's stackable.
  6297. if (!itemdb_isstackable2(id))
  6298. get_count = 1;
  6299. else
  6300. get_count = amount;
  6301. for (i = 0; i < amount; i += get_count)
  6302. {
  6303. // if not pet egg
  6304. if (!pet_create_egg(sd, nameid))
  6305. {
  6306. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6307. {
  6308. clif_additem(sd, 0, 0, flag);
  6309. if( pc_candrop(sd,&it) )
  6310. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6311. }
  6312. }
  6313. }
  6314. return SCRIPT_CMD_SUCCESS;
  6315. }
  6316. /*==========================================
  6317. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6318. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6319. *
  6320. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6321. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6322. *
  6323. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6324. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6325. *
  6326. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6327. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6328. * Type:
  6329. * 0 - No bound
  6330. * 1 - Account Bound
  6331. * 2 - Guild Bound
  6332. * 3 - Party Bound
  6333. * 4 - Character Bound
  6334. *------------------------------------------*/
  6335. BUILDIN_FUNC(getitem2)
  6336. {
  6337. unsigned short nameid, amount;
  6338. int iden, ref, attr;
  6339. unsigned short c1, c2, c3, c4;
  6340. char bound = BOUND_NONE;
  6341. struct item_data *item_data = NULL;
  6342. struct item item_tmp;
  6343. TBL_PC *sd;
  6344. struct script_data *data;
  6345. const char* command = script_getfuncname(st);
  6346. int offset = 0;
  6347. if( !strncmp(command,"getitembound",12) ) {
  6348. int aid_pos = 12;
  6349. bound = script_getnum(st,11);
  6350. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6351. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6352. return SCRIPT_CMD_FAILURE;
  6353. }
  6354. if (command[strlen(command)-1] == '3') {
  6355. offset = 12;
  6356. aid_pos = 15;
  6357. }
  6358. script_mapid2sd(aid_pos,sd);
  6359. } else {
  6360. int aid_pos = 11;
  6361. if (strcmpi(command,"getitem3") == 0) {
  6362. offset = 11;
  6363. aid_pos = 14;
  6364. }
  6365. script_mapid2sd(aid_pos,sd);
  6366. }
  6367. if( sd == NULL ) // no target
  6368. return SCRIPT_CMD_SUCCESS;
  6369. data = script_getdata(st,2);
  6370. get_val(st,data);
  6371. if( data_isstring(data) ) {
  6372. const char *name = conv_str(st,data);
  6373. if( (item_data = itemdb_searchname(name)) == NULL ){
  6374. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6375. return SCRIPT_CMD_FAILURE; //No item created.
  6376. }
  6377. nameid = item_data->nameid;
  6378. } else {
  6379. nameid = conv_num(st,data);
  6380. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6381. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6382. return SCRIPT_CMD_FAILURE; //No item created.
  6383. }
  6384. }
  6385. amount = script_getnum(st,3);
  6386. iden = script_getnum(st,4);
  6387. ref = script_getnum(st,5);
  6388. attr = script_getnum(st,6);
  6389. c1 = (unsigned short)script_getnum(st,7);
  6390. c2 = (unsigned short)script_getnum(st,8);
  6391. c3 = (unsigned short)script_getnum(st,9);
  6392. c4 = (unsigned short)script_getnum(st,10);
  6393. if( item_data ) {
  6394. int get_count = 0, i;
  6395. memset(&item_tmp,0,sizeof(item_tmp));
  6396. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6397. if(ref > MAX_REFINE)
  6398. ref = MAX_REFINE;
  6399. }
  6400. else if( item_data->type == IT_PETEGG ) {
  6401. iden = 1;
  6402. ref = 0;
  6403. }
  6404. else {
  6405. iden = 1;
  6406. ref = attr = 0;
  6407. }
  6408. item_tmp.nameid = nameid;
  6409. item_tmp.identify = iden;
  6410. item_tmp.refine = ref;
  6411. item_tmp.attribute = attr;
  6412. item_tmp.card[0] = c1;
  6413. item_tmp.card[1] = c2;
  6414. item_tmp.card[2] = c3;
  6415. item_tmp.card[3] = c4;
  6416. item_tmp.bound = bound;
  6417. if (offset != 0) {
  6418. int res = script_getitem_randomoption(st, sd, &item_tmp, command, offset);
  6419. if (res == SCRIPT_CMD_FAILURE)
  6420. return SCRIPT_CMD_FAILURE;
  6421. }
  6422. //Check if it's stackable.
  6423. if (!itemdb_isstackable2(item_data))
  6424. get_count = 1;
  6425. else
  6426. get_count = amount;
  6427. for (i = 0; i < amount; i += get_count)
  6428. {
  6429. // if not pet egg
  6430. if (!pet_create_egg(sd, nameid))
  6431. {
  6432. unsigned char flag = 0;
  6433. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6434. {
  6435. clif_additem(sd, 0, 0, flag);
  6436. if( pc_candrop(sd,&item_tmp) )
  6437. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6438. }
  6439. }
  6440. }
  6441. }
  6442. return SCRIPT_CMD_SUCCESS;
  6443. }
  6444. /** Gives rental item to player
  6445. * rentitem <item id>,<seconds>{,<account_id>}
  6446. * rentitem "<item name>",<seconds>{,<account_id>}
  6447. */
  6448. BUILDIN_FUNC(rentitem) {
  6449. struct map_session_data *sd;
  6450. struct script_data *data;
  6451. struct item it;
  6452. int seconds;
  6453. unsigned short nameid = 0;
  6454. unsigned char flag = 0;
  6455. data = script_getdata(st,2);
  6456. get_val(st,data);
  6457. if (!script_accid2sd(4,sd))
  6458. return SCRIPT_CMD_FAILURE;
  6459. if( data_isstring(data) )
  6460. {
  6461. const char *name = conv_str(st,data);
  6462. struct item_data *itd = itemdb_searchname(name);
  6463. if( itd == NULL )
  6464. {
  6465. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6466. return SCRIPT_CMD_FAILURE;
  6467. }
  6468. nameid = itd->nameid;
  6469. }
  6470. else if( data_isint(data) )
  6471. {
  6472. nameid = conv_num(st,data);
  6473. if( nameid == 0 || !itemdb_exists(nameid) )
  6474. {
  6475. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6476. return SCRIPT_CMD_FAILURE;
  6477. }
  6478. }
  6479. else
  6480. {
  6481. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6482. return SCRIPT_CMD_FAILURE;
  6483. }
  6484. seconds = script_getnum(st,3);
  6485. memset(&it, 0, sizeof(it));
  6486. it.nameid = nameid;
  6487. it.identify = 1;
  6488. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6489. it.bound = BOUND_NONE;
  6490. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6491. {
  6492. clif_additem(sd, 0, 0, flag);
  6493. return SCRIPT_CMD_FAILURE;
  6494. }
  6495. return SCRIPT_CMD_SUCCESS;
  6496. }
  6497. /**
  6498. * Gives rental item to player with advanced option
  6499. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6500. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6501. *
  6502. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6503. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6504. */
  6505. BUILDIN_FUNC(rentitem2) {
  6506. struct map_session_data *sd;
  6507. struct script_data *data;
  6508. struct item it;
  6509. struct item_data *id;
  6510. int seconds;
  6511. unsigned short nameid = 0;
  6512. unsigned char flag = 0;
  6513. int iden,ref,attr,c1,c2,c3,c4;
  6514. const char *funcname = script_getfuncname(st);
  6515. data = script_getdata(st,2);
  6516. get_val(st,data);
  6517. if (funcname[strlen(funcname)-1] == '3') {
  6518. if (!script_accid2sd(14,sd))
  6519. return SCRIPT_CMD_FAILURE;
  6520. } else if (!script_accid2sd(11,sd))
  6521. return SCRIPT_CMD_FAILURE;
  6522. if( data_isstring(data) ) {
  6523. const char *name = conv_str(st,data);
  6524. id = itemdb_searchname(name);
  6525. if( id == NULL ) {
  6526. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6527. return SCRIPT_CMD_FAILURE;
  6528. }
  6529. nameid = id->nameid;
  6530. }
  6531. else if( data_isint(data) ) {
  6532. nameid = conv_num(st,data);
  6533. if( !(id = itemdb_search(nameid))) {
  6534. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6535. return SCRIPT_CMD_FAILURE;
  6536. }
  6537. }
  6538. else {
  6539. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6540. return SCRIPT_CMD_FAILURE;
  6541. }
  6542. seconds = script_getnum(st,3);
  6543. iden = script_getnum(st,4);
  6544. ref = script_getnum(st,5);
  6545. attr = script_getnum(st,6);
  6546. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6547. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6548. }
  6549. else if (id->type==IT_PETEGG) {
  6550. iden = 1;
  6551. ref = 0;
  6552. }
  6553. else {
  6554. iden = 1;
  6555. ref = attr = 0;
  6556. }
  6557. c1 = (short)script_getnum(st,7);
  6558. c2 = (short)script_getnum(st,8);
  6559. c3 = (short)script_getnum(st,9);
  6560. c4 = (short)script_getnum(st,10);
  6561. memset(&it, 0, sizeof(it));
  6562. it.nameid = nameid;
  6563. it.identify = iden;
  6564. it.refine = ref;
  6565. it.attribute = attr;
  6566. it.card[0] = (short)c1;
  6567. it.card[1] = (short)c2;
  6568. it.card[2] = (short)c3;
  6569. it.card[3] = (short)c4;
  6570. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6571. if (funcname[strlen(funcname)-1] == '3') {
  6572. int res = script_getitem_randomoption(st, sd, &it, funcname, 11);
  6573. if (res != SCRIPT_CMD_SUCCESS)
  6574. return res;
  6575. }
  6576. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6577. clif_additem(sd, 0, 0, flag);
  6578. return SCRIPT_CMD_FAILURE;
  6579. }
  6580. return SCRIPT_CMD_SUCCESS;
  6581. }
  6582. /*==========================================
  6583. * gets an item with someone's name inscribed [Skotlex]
  6584. * getinscribeditem item_num, character_name
  6585. * Returned Qty is always 1, only works on equip-able
  6586. * equipment
  6587. *------------------------------------------*/
  6588. BUILDIN_FUNC(getnameditem)
  6589. {
  6590. unsigned short nameid;
  6591. struct item item_tmp;
  6592. TBL_PC *sd, *tsd;
  6593. struct script_data *data;
  6594. if (!script_rid2sd(sd))
  6595. { //Player not attached!
  6596. script_pushint(st,0);
  6597. return SCRIPT_CMD_SUCCESS;
  6598. }
  6599. data=script_getdata(st,2);
  6600. get_val(st,data);
  6601. if( data_isstring(data) ){
  6602. const char *name=conv_str(st,data);
  6603. struct item_data *item_data = itemdb_searchname(name);
  6604. if( item_data == NULL)
  6605. { //Failed
  6606. script_pushint(st,0);
  6607. return SCRIPT_CMD_SUCCESS;
  6608. }
  6609. nameid = item_data->nameid;
  6610. }else
  6611. nameid = conv_num(st,data);
  6612. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6613. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6614. script_pushint(st,0);
  6615. return SCRIPT_CMD_SUCCESS;
  6616. }
  6617. data=script_getdata(st,3);
  6618. get_val(st,data);
  6619. if( data_isstring(data) ) //Char Name
  6620. tsd=map_nick2sd(conv_str(st,data),false);
  6621. else //Char Id was given
  6622. tsd=map_charid2sd(conv_num(st,data));
  6623. if( tsd == NULL )
  6624. { //Failed
  6625. script_pushint(st,0);
  6626. return SCRIPT_CMD_SUCCESS;
  6627. }
  6628. memset(&item_tmp,0,sizeof(item_tmp));
  6629. item_tmp.nameid=nameid;
  6630. item_tmp.amount=1;
  6631. item_tmp.identify=1;
  6632. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6633. item_tmp.card[2]=tsd->status.char_id;
  6634. item_tmp.card[3]=tsd->status.char_id >> 16;
  6635. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6636. script_pushint(st,0);
  6637. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6638. }
  6639. script_pushint(st,1);
  6640. return SCRIPT_CMD_SUCCESS;
  6641. }
  6642. /*==========================================
  6643. * gets a random item ID from an item group [Skotlex]
  6644. * groupranditem <group_num>{,<sub_group>};
  6645. *------------------------------------------*/
  6646. BUILDIN_FUNC(grouprandomitem) {
  6647. struct s_item_group_entry *entry = NULL;
  6648. int sub_group = 1;
  6649. FETCH(3, sub_group);
  6650. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6651. if (!entry) {
  6652. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6653. script_pushint(st,UNKNOWN_ITEM_ID);
  6654. return SCRIPT_CMD_FAILURE;
  6655. }
  6656. script_pushint(st,entry->nameid);
  6657. return SCRIPT_CMD_SUCCESS;
  6658. }
  6659. /**
  6660. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6661. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6662. */
  6663. BUILDIN_FUNC(makeitem) {
  6664. uint16 nameid, amount, flag = 0, x, y;
  6665. const char *mapname;
  6666. int m;
  6667. struct item item_tmp;
  6668. struct script_data *data;
  6669. data = script_getdata(st,2);
  6670. get_val(st,data);
  6671. if( data_isstring(data) ){
  6672. const char *name = conv_str(st,data);
  6673. struct item_data *item_data = itemdb_searchname(name);
  6674. if( item_data )
  6675. nameid = item_data->nameid;
  6676. else
  6677. nameid = UNKNOWN_ITEM_ID;
  6678. }
  6679. else
  6680. nameid = conv_num(st,data);
  6681. amount = script_getnum(st,3);
  6682. mapname = script_getstr(st,4);
  6683. x = script_getnum(st,5);
  6684. y = script_getnum(st,6);
  6685. if(strcmp(mapname,"this")==0) {
  6686. TBL_PC *sd;
  6687. if (!script_rid2sd(sd))
  6688. return SCRIPT_CMD_SUCCESS; //Failed...
  6689. m = sd->bl.m;
  6690. } else
  6691. m = map_mapname2mapid(mapname);
  6692. if(nameid<0) {
  6693. nameid = -nameid;
  6694. flag = 1;
  6695. }
  6696. if(nameid > 0) {
  6697. memset(&item_tmp,0,sizeof(item_tmp));
  6698. item_tmp.nameid = nameid;
  6699. if(!flag)
  6700. item_tmp.identify = 1;
  6701. else
  6702. item_tmp.identify = itemdb_isidentified(nameid);
  6703. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6704. }
  6705. return SCRIPT_CMD_SUCCESS;
  6706. }
  6707. /**
  6708. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6709. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6710. *
  6711. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6712. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6713. */
  6714. BUILDIN_FUNC(makeitem2) {
  6715. uint16 nameid, amount, x, y;
  6716. const char *mapname;
  6717. int m;
  6718. struct item item_tmp;
  6719. struct script_data *data;
  6720. struct item_data *id;
  6721. const char *funcname = script_getfuncname(st);
  6722. data = script_getdata(st,2);
  6723. get_val(st,data);
  6724. if( data_isstring(data) ){
  6725. const char *name = conv_str(st,data);
  6726. struct item_data *item_data = itemdb_searchname(name);
  6727. if( item_data )
  6728. nameid = item_data->nameid;
  6729. else
  6730. nameid = UNKNOWN_ITEM_ID;
  6731. }
  6732. else
  6733. nameid = conv_num(st,data);
  6734. amount = script_getnum(st,3);
  6735. mapname = script_getstr(st,4);
  6736. x = script_getnum(st,5);
  6737. y = script_getnum(st,6);
  6738. if (strcmp(mapname,"this")==0) {
  6739. TBL_PC *sd;
  6740. if (!script_rid2sd(sd))
  6741. return SCRIPT_CMD_SUCCESS; //Failed...
  6742. m = sd->bl.m;
  6743. }
  6744. else
  6745. m = map_mapname2mapid(mapname);
  6746. if ((id = itemdb_search(nameid))) {
  6747. char iden, ref, attr;
  6748. memset(&item_tmp,0,sizeof(item_tmp));
  6749. item_tmp.nameid = nameid;
  6750. iden = (char)script_getnum(st,7);
  6751. ref = (char)script_getnum(st,8);
  6752. attr = (char)script_getnum(st,9);
  6753. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6754. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6755. }
  6756. else if (id->type==IT_PETEGG) {
  6757. iden = 1;
  6758. ref = 0;
  6759. }
  6760. else {
  6761. iden = 1;
  6762. ref = attr = 0;
  6763. }
  6764. item_tmp.identify = iden;
  6765. item_tmp.refine = ref;
  6766. item_tmp.attribute = attr;
  6767. item_tmp.card[0] = script_getnum(st,10);
  6768. item_tmp.card[1] = script_getnum(st,11);
  6769. item_tmp.card[2] = script_getnum(st,12);
  6770. item_tmp.card[3] = script_getnum(st,13);
  6771. if (funcname[strlen(funcname)-1] == '3') {
  6772. int res = script_getitem_randomoption(st, nullptr, &item_tmp, funcname, 14);
  6773. if (res != SCRIPT_CMD_SUCCESS)
  6774. return res;
  6775. }
  6776. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6777. }
  6778. else
  6779. return SCRIPT_CMD_FAILURE;
  6780. return SCRIPT_CMD_SUCCESS;
  6781. }
  6782. /// Counts / deletes the current item given by idx.
  6783. /// Used by buildin_delitem_search
  6784. /// Relies on all input data being already fully valid.
  6785. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6786. {
  6787. int delamount;
  6788. struct item *itm = NULL;
  6789. struct s_storage *gstor = NULL;
  6790. switch(loc) {
  6791. case TABLE_CART:
  6792. itm = &sd->cart.u.items_cart[idx];
  6793. break;
  6794. case TABLE_STORAGE:
  6795. itm = &sd->storage.u.items_storage[idx];
  6796. break;
  6797. case TABLE_GUILD_STORAGE:
  6798. {
  6799. gstor = guild2storage2(sd->status.guild_id);
  6800. itm = &gstor->u.items_guild[idx];
  6801. }
  6802. break;
  6803. default: // TABLE_INVENTORY
  6804. itm = &sd->inventory.u.items_inventory[idx];
  6805. break;
  6806. }
  6807. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6808. if( delete_items )
  6809. {
  6810. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6811. {// delete associated pet
  6812. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6813. }
  6814. switch(loc) {
  6815. case TABLE_CART:
  6816. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6817. break;
  6818. case TABLE_STORAGE:
  6819. storage_delitem(sd,&sd->storage,idx,delamount);
  6820. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6821. break;
  6822. case TABLE_GUILD_STORAGE:
  6823. gstor->lock = true;
  6824. storage_guild_delitem(sd, gstor, idx, delamount);
  6825. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6826. storage_guild_storageclose(sd);
  6827. gstor->lock = false;
  6828. break;
  6829. default: // TABLE_INVENTORY
  6830. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6831. break;
  6832. }
  6833. }
  6834. amount[0]-= delamount;
  6835. }
  6836. /// Searches for item(s) and checks, if there is enough of them.
  6837. /// Used by delitem and delitem2
  6838. /// Relies on all input data being already fully valid.
  6839. /// @param exact_match will also match item by specified attributes
  6840. /// 0x0: Only item id
  6841. /// 0x1: identify, attributes, cards
  6842. /// 0x2: random option
  6843. /// @return true when all items could be deleted, false when there were not enough items to delete
  6844. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  6845. {
  6846. bool delete_items = false;
  6847. int i, amount, size;
  6848. struct item *items;
  6849. // prefer always non-equipped items
  6850. it->equip = 0;
  6851. // when searching for nameid only, prefer additionally
  6852. if( !exact_match )
  6853. {
  6854. // non-refined items
  6855. it->refine = 0;
  6856. // card-less items
  6857. memset(it->card, 0, sizeof(it->card));
  6858. }
  6859. switch(loc) {
  6860. case TABLE_CART:
  6861. size = MAX_CART;
  6862. items = sd->cart.u.items_cart;
  6863. break;
  6864. case TABLE_STORAGE:
  6865. size = MAX_STORAGE;
  6866. items = sd->storage.u.items_storage;
  6867. break;
  6868. case TABLE_GUILD_STORAGE:
  6869. {
  6870. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6871. size = MAX_GUILD_STORAGE;
  6872. items = gstor->u.items_guild;
  6873. }
  6874. break;
  6875. default: // TABLE_INVENTORY
  6876. size = MAX_INVENTORY;
  6877. items = sd->inventory.u.items_inventory;
  6878. break;
  6879. }
  6880. for(;;)
  6881. {
  6882. unsigned short important = 0;
  6883. amount = it->amount;
  6884. // 1st pass -- less important items / exact match
  6885. for( i = 0; amount && i < size; i++ )
  6886. {
  6887. struct item *itm = NULL;
  6888. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6889. {// wrong/invalid item
  6890. continue;
  6891. }
  6892. if( itm->equip != it->equip || itm->refine != it->refine )
  6893. {// not matching attributes
  6894. important++;
  6895. continue;
  6896. }
  6897. if( exact_match )
  6898. {
  6899. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  6900. {// not matching exact attributes
  6901. continue;
  6902. }
  6903. if (exact_match&0x2) {
  6904. uint8 j;
  6905. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6906. 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)
  6907. break;
  6908. }
  6909. if (j != MAX_ITEM_RDM_OPT)
  6910. continue;
  6911. }
  6912. }
  6913. else
  6914. {
  6915. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6916. {
  6917. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6918. {// pet which cannot be deleted
  6919. continue;
  6920. }
  6921. }
  6922. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6923. {// named/carded item
  6924. important++;
  6925. continue;
  6926. }
  6927. }
  6928. // count / delete item
  6929. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6930. }
  6931. // 2nd pass -- any matching item
  6932. if( amount == 0 || important == 0 )
  6933. {// either everything was already consumed or no items were skipped
  6934. ;
  6935. }
  6936. else for( i = 0; amount && i < size; i++ )
  6937. {
  6938. struct item *itm = NULL;
  6939. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6940. {// wrong/invalid item
  6941. continue;
  6942. }
  6943. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6944. {// pet which cannot be deleted
  6945. continue;
  6946. }
  6947. if( exact_match )
  6948. {
  6949. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  6950. {// not matching attributes
  6951. continue;
  6952. }
  6953. if (exact_match&0x2) {
  6954. uint8 j;
  6955. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6956. 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)
  6957. break;
  6958. }
  6959. if (j != MAX_ITEM_RDM_OPT)
  6960. continue;
  6961. }
  6962. }
  6963. // count / delete item
  6964. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6965. }
  6966. if( amount )
  6967. {// not enough items
  6968. return false;
  6969. }
  6970. else if( delete_items )
  6971. {// we are done with the work
  6972. return true;
  6973. }
  6974. else
  6975. {// get rid of the items now
  6976. delete_items = true;
  6977. }
  6978. }
  6979. }
  6980. /// Deletes items from the target/attached player.
  6981. /// Prioritizes ordinary items.
  6982. ///
  6983. /// delitem <item id>,<amount>{,<account id>}
  6984. /// delitem "<item name>",<amount>{,<account id>}
  6985. /// cartdelitem <item id>,<amount>{,<account id>}
  6986. /// cartdelitem "<item name>",<amount>{,<account id>}
  6987. /// storagedelitem <item id>,<amount>{,<account id>}
  6988. /// storagedelitem "<item name>",<amount>{,<account id>}
  6989. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6990. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  6991. BUILDIN_FUNC(delitem)
  6992. {
  6993. TBL_PC *sd;
  6994. struct item it;
  6995. struct script_data *data;
  6996. uint8 loc = 0;
  6997. char* command = (char*)script_getfuncname(st);
  6998. if(!strncmp(command, "cart", 4))
  6999. loc = TABLE_CART;
  7000. else if(!strncmp(command, "storage", 7))
  7001. loc = TABLE_STORAGE;
  7002. else if(!strncmp(command, "guildstorage", 12))
  7003. loc = TABLE_GUILD_STORAGE;
  7004. if( !script_accid2sd(4,sd) ){
  7005. // In any case cancel script execution
  7006. st->state = END;
  7007. return SCRIPT_CMD_SUCCESS;
  7008. }
  7009. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7010. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7011. script_pushint(st, -1);
  7012. return SCRIPT_CMD_FAILURE;
  7013. }
  7014. if (loc == TABLE_GUILD_STORAGE) {
  7015. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7016. if (gstor == NULL || sd->state.storage_flag) {
  7017. script_pushint(st, -1);
  7018. return SCRIPT_CMD_FAILURE;
  7019. }
  7020. }
  7021. data = script_getdata(st,2);
  7022. get_val(st,data);
  7023. if( data_isstring(data) )
  7024. {
  7025. const char* item_name = conv_str(st,data);
  7026. struct item_data* id = itemdb_searchname(item_name);
  7027. if( id == NULL )
  7028. {
  7029. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7030. st->state = END;
  7031. return SCRIPT_CMD_FAILURE;
  7032. }
  7033. it.nameid = id->nameid;// "<item name>"
  7034. }
  7035. else
  7036. {
  7037. it.nameid = conv_num(st,data);// <item id>
  7038. if( !itemdb_exists( it.nameid ) )
  7039. {
  7040. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  7041. st->state = END;
  7042. return SCRIPT_CMD_FAILURE;
  7043. }
  7044. }
  7045. it.amount=script_getnum(st,3);
  7046. if( it.amount <= 0 )
  7047. return SCRIPT_CMD_SUCCESS;// nothing to do
  7048. if( buildin_delitem_search(sd, &it, 0, loc) )
  7049. {// success
  7050. return SCRIPT_CMD_SUCCESS;
  7051. }
  7052. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  7053. st->state = END;
  7054. st->mes_active = 0;
  7055. clif_scriptclose(sd, st->oid);
  7056. return SCRIPT_CMD_FAILURE;
  7057. }
  7058. /// Deletes items from the target/attached player.
  7059. ///
  7060. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7061. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7062. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7063. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7064. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7065. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7066. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7067. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7068. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7069. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7070. BUILDIN_FUNC(delitem2)
  7071. {
  7072. TBL_PC *sd;
  7073. struct item it;
  7074. struct script_data *data;
  7075. uint8 loc = 0;
  7076. char* command = (char*)script_getfuncname(st);
  7077. int aid_pos = 11;
  7078. uint8 flag = 0x1;
  7079. if(!strncmp(command, "cart", 4))
  7080. loc = TABLE_CART;
  7081. else if(!strncmp(command, "storage", 7))
  7082. loc = TABLE_STORAGE;
  7083. else if(!strncmp(command, "guildstorage", 12))
  7084. loc = TABLE_GUILD_STORAGE;
  7085. if (command[strlen(command)-1] == '3')
  7086. aid_pos = 14;
  7087. if( !script_accid2sd(aid_pos,sd) ){
  7088. // In any case cancel script execution
  7089. st->state = END;
  7090. return SCRIPT_CMD_SUCCESS;
  7091. }
  7092. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7093. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7094. script_pushint(st,-1);
  7095. return SCRIPT_CMD_FAILURE;
  7096. }
  7097. if (loc == TABLE_GUILD_STORAGE) {
  7098. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7099. if (gstor == NULL || sd->state.storage_flag) {
  7100. script_pushint(st, -1);
  7101. return SCRIPT_CMD_FAILURE;
  7102. }
  7103. }
  7104. memset(&it, 0, sizeof(it));
  7105. data = script_getdata(st,2);
  7106. get_val(st,data);
  7107. if( data_isstring(data) )
  7108. {
  7109. const char* item_name = conv_str(st,data);
  7110. struct item_data* id = itemdb_searchname(item_name);
  7111. if( id == NULL )
  7112. {
  7113. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7114. st->state = END;
  7115. return SCRIPT_CMD_FAILURE;
  7116. }
  7117. it.nameid = id->nameid;// "<item name>"
  7118. }
  7119. else
  7120. {
  7121. it.nameid = conv_num(st,data);// <item id>
  7122. if( !itemdb_exists( it.nameid ) )
  7123. {
  7124. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  7125. st->state = END;
  7126. return SCRIPT_CMD_FAILURE;
  7127. }
  7128. }
  7129. it.amount=script_getnum(st,3);
  7130. it.identify=script_getnum(st,4);
  7131. it.refine=script_getnum(st,5);
  7132. it.attribute=script_getnum(st,6);
  7133. it.card[0]=(short)script_getnum(st,7);
  7134. it.card[1]=(short)script_getnum(st,8);
  7135. it.card[2]=(short)script_getnum(st,9);
  7136. it.card[3]=(short)script_getnum(st,10);
  7137. if (command[strlen(command)-1] == '3') {
  7138. int res = script_getitem_randomoption(st, sd, &it, command, 11);
  7139. if (res != SCRIPT_CMD_SUCCESS)
  7140. return SCRIPT_CMD_FAILURE;
  7141. flag |= 0x2;
  7142. }
  7143. if( it.amount <= 0 )
  7144. return SCRIPT_CMD_SUCCESS;// nothing to do
  7145. if( buildin_delitem_search(sd, &it, flag, loc) )
  7146. {// success
  7147. return SCRIPT_CMD_SUCCESS;
  7148. }
  7149. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  7150. st->state = END;
  7151. st->mes_active = 0;
  7152. clif_scriptclose(sd, st->oid);
  7153. return SCRIPT_CMD_FAILURE;
  7154. }
  7155. /*==========================================
  7156. * Enables/Disables use of items while in an NPC [Skotlex]
  7157. *------------------------------------------*/
  7158. BUILDIN_FUNC(enableitemuse)
  7159. {
  7160. TBL_PC *sd;
  7161. if (script_rid2sd(sd))
  7162. st->npc_item_flag = sd->npc_item_flag = 1;
  7163. return SCRIPT_CMD_SUCCESS;
  7164. }
  7165. BUILDIN_FUNC(disableitemuse)
  7166. {
  7167. TBL_PC *sd;
  7168. if (script_rid2sd(sd))
  7169. st->npc_item_flag = sd->npc_item_flag = 0;
  7170. return SCRIPT_CMD_SUCCESS;
  7171. }
  7172. /*==========================================
  7173. * Returns a character's specified stat.
  7174. * Check pc_readparam for available options.
  7175. * readparam <param>{,"<nick>"}
  7176. * readparam <param>{,<char_id>}
  7177. *------------------------------------------*/
  7178. BUILDIN_FUNC(readparam)
  7179. {
  7180. int value;
  7181. struct script_data *data = script_getdata(st, 2);
  7182. TBL_PC *sd = NULL;
  7183. if( script_hasdata(st, 3) ){
  7184. struct script_data *data2 = script_getdata(st, 3);
  7185. get_val(st, data2);
  7186. if (data_isint(data2) || script_getnum(st, 3)) {
  7187. script_charid2sd(3, sd);
  7188. } else if (data_isstring(data2)) {
  7189. script_nick2sd(3, sd);
  7190. }
  7191. }else{
  7192. script_rid2sd(sd);
  7193. }
  7194. if( !sd ){
  7195. script_pushint(st, -1);
  7196. return SCRIPT_CMD_FAILURE;
  7197. }
  7198. // If you use a parameter, return the value behind it
  7199. if( reference_toparam(data) ){
  7200. get_val_(st, data, sd);
  7201. value = (int)data->u.num;
  7202. }else{
  7203. value = pc_readparam(sd,script_getnum(st, 2));
  7204. }
  7205. script_pushint(st,value);
  7206. return SCRIPT_CMD_SUCCESS;
  7207. }
  7208. /*==========================================
  7209. * Return charid identification
  7210. * return by @num :
  7211. * 0 : char_id
  7212. * 1 : party_id
  7213. * 2 : guild_id
  7214. * 3 : account_id
  7215. * 4 : bg_id
  7216. * 5 : clan_id
  7217. *------------------------------------------*/
  7218. BUILDIN_FUNC(getcharid)
  7219. {
  7220. int num;
  7221. TBL_PC *sd;
  7222. num = script_getnum(st,2);
  7223. if( !script_nick2sd(3,sd) ){
  7224. script_pushint(st,0); //return 0, according docs
  7225. return SCRIPT_CMD_SUCCESS;
  7226. }
  7227. switch( num ) {
  7228. case 0: script_pushint(st,sd->status.char_id); break;
  7229. case 1: script_pushint(st,sd->status.party_id); break;
  7230. case 2: script_pushint(st,sd->status.guild_id); break;
  7231. case 3: script_pushint(st,sd->status.account_id); break;
  7232. case 4: script_pushint(st,sd->bg_id); break;
  7233. case 5: script_pushint(st,sd->status.clan_id); break;
  7234. default:
  7235. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7236. script_pushint(st,0);
  7237. break;
  7238. }
  7239. return SCRIPT_CMD_SUCCESS;
  7240. }
  7241. /*==========================================
  7242. * returns the GID of an NPC
  7243. *------------------------------------------*/
  7244. BUILDIN_FUNC(getnpcid)
  7245. {
  7246. int num = script_getnum(st,2);
  7247. struct npc_data* nd = NULL;
  7248. if( script_hasdata(st,3) )
  7249. {// unique npc name
  7250. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7251. {
  7252. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7253. script_pushint(st,0);
  7254. return SCRIPT_CMD_FAILURE;
  7255. }
  7256. }
  7257. switch (num) {
  7258. case 0:
  7259. script_pushint(st,nd ? nd->bl.id : st->oid);
  7260. break;
  7261. default:
  7262. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7263. script_pushint(st,0);
  7264. return SCRIPT_CMD_FAILURE;
  7265. }
  7266. return SCRIPT_CMD_SUCCESS;
  7267. }
  7268. /*==========================================
  7269. * Return the name of the party_id
  7270. * null if not found
  7271. *------------------------------------------*/
  7272. BUILDIN_FUNC(getpartyname)
  7273. {
  7274. int party_id;
  7275. struct party_data* p;
  7276. party_id = script_getnum(st,2);
  7277. if( ( p = party_search(party_id) ) != NULL )
  7278. {
  7279. script_pushstrcopy(st,p->party.name);
  7280. }
  7281. else
  7282. {
  7283. script_pushconststr(st,"null");
  7284. }
  7285. return SCRIPT_CMD_SUCCESS;
  7286. }
  7287. /*==========================================
  7288. * Get the information of the members of a party by type
  7289. * @party_id, @type
  7290. * return by @type :
  7291. * - : nom des membres
  7292. * 1 : char_id des membres
  7293. * 2 : account_id des membres
  7294. *------------------------------------------*/
  7295. BUILDIN_FUNC(getpartymember)
  7296. {
  7297. struct party_data *p;
  7298. uint8 j = 0;
  7299. p = party_search(script_getnum(st,2));
  7300. if (p != NULL) {
  7301. uint8 i, type = 0;
  7302. struct script_data *data = NULL;
  7303. char *varname = NULL;
  7304. if (script_hasdata(st,3))
  7305. type = script_getnum(st,3);
  7306. if (script_hasdata(st,4)) {
  7307. data = script_getdata(st, 4);
  7308. if (!data_isreference(data)) {
  7309. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7310. return SCRIPT_CMD_FAILURE;
  7311. }
  7312. varname = reference_getname(data);
  7313. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7314. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7315. return SCRIPT_CMD_FAILURE;
  7316. }
  7317. }
  7318. for (i = 0; i < MAX_PARTY; i++) {
  7319. if (p->party.member[i].account_id) {
  7320. switch (type) {
  7321. case 2:
  7322. if (data)
  7323. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  7324. else
  7325. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7326. break;
  7327. case 1:
  7328. if (data)
  7329. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7330. else
  7331. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7332. break;
  7333. default:
  7334. if (data)
  7335. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7336. else
  7337. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7338. break;
  7339. }
  7340. j++;
  7341. }
  7342. }
  7343. }
  7344. mapreg_setreg(add_str("$@partymembercount"),j);
  7345. script_pushint(st, j);
  7346. return SCRIPT_CMD_SUCCESS;
  7347. }
  7348. /*==========================================
  7349. * Retrieves party leader. if flag is specified,
  7350. * return some of the leader data. Otherwise, return name.
  7351. *------------------------------------------*/
  7352. BUILDIN_FUNC(getpartyleader)
  7353. {
  7354. int party_id, type = 0, i=0;
  7355. struct party_data *p;
  7356. party_id=script_getnum(st,2);
  7357. if( script_hasdata(st,3) )
  7358. type=script_getnum(st,3);
  7359. p=party_search(party_id);
  7360. if (p) //Search leader
  7361. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7362. if (!p || i == MAX_PARTY) { //leader not found
  7363. if (type)
  7364. script_pushint(st,-1);
  7365. else
  7366. script_pushconststr(st,"null");
  7367. return SCRIPT_CMD_SUCCESS;
  7368. }
  7369. switch (type) {
  7370. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7371. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7372. case 3: script_pushint(st,p->party.member[i].class_); break;
  7373. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7374. case 5: script_pushint(st,p->party.member[i].lv); break;
  7375. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7376. }
  7377. return SCRIPT_CMD_SUCCESS;
  7378. }
  7379. /*==========================================
  7380. * Return the name of the @guild_id
  7381. * null if not found
  7382. *------------------------------------------*/
  7383. BUILDIN_FUNC(getguildname)
  7384. {
  7385. int guild_id;
  7386. struct guild* g;
  7387. guild_id = script_getnum(st,2);
  7388. if( ( g = guild_search(guild_id) ) != NULL )
  7389. script_pushstrcopy(st,g->name);
  7390. else
  7391. script_pushconststr(st,"null");
  7392. return SCRIPT_CMD_SUCCESS;
  7393. }
  7394. /*==========================================
  7395. * Return the name of the guild master of @guild_id
  7396. * null if not found
  7397. *------------------------------------------*/
  7398. BUILDIN_FUNC(getguildmaster)
  7399. {
  7400. int guild_id;
  7401. struct guild* g;
  7402. guild_id = script_getnum(st,2);
  7403. if( ( g = guild_search(guild_id) ) != NULL )
  7404. script_pushstrcopy(st,g->member[0].name);
  7405. else
  7406. script_pushconststr(st,"null");
  7407. return SCRIPT_CMD_SUCCESS;
  7408. }
  7409. BUILDIN_FUNC(getguildmasterid)
  7410. {
  7411. int guild_id;
  7412. struct guild* g;
  7413. guild_id = script_getnum(st,2);
  7414. if( ( g = guild_search(guild_id) ) != NULL )
  7415. script_pushint(st,g->member[0].char_id);
  7416. else
  7417. script_pushint(st,0);
  7418. return SCRIPT_CMD_SUCCESS;
  7419. }
  7420. /*==========================================
  7421. * Get char string information by type :
  7422. * Return by @type :
  7423. * 0 : char_name
  7424. * 1 : party_name or ""
  7425. * 2 : guild_name or ""
  7426. * 3 : map_name
  7427. * - : ""
  7428. * strcharinfo(<type>{,<char_id>})
  7429. *------------------------------------------*/
  7430. BUILDIN_FUNC(strcharinfo)
  7431. {
  7432. TBL_PC *sd;
  7433. int num;
  7434. struct guild* g;
  7435. struct party_data* p;
  7436. if (!script_charid2sd(3,sd)) {
  7437. script_pushconststr(st,"");
  7438. return SCRIPT_CMD_FAILURE;
  7439. }
  7440. num=script_getnum(st,2);
  7441. switch(num){
  7442. case 0:
  7443. script_pushstrcopy(st,sd->status.name);
  7444. break;
  7445. case 1:
  7446. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7447. script_pushstrcopy(st,p->party.name);
  7448. } else {
  7449. script_pushconststr(st,"");
  7450. }
  7451. break;
  7452. case 2:
  7453. if( ( g = sd->guild ) != NULL ) {
  7454. script_pushstrcopy(st,g->name);
  7455. } else {
  7456. script_pushconststr(st,"");
  7457. }
  7458. break;
  7459. case 3:
  7460. script_pushconststr(st,map[sd->bl.m].name);
  7461. break;
  7462. default:
  7463. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7464. script_pushconststr(st,"");
  7465. break;
  7466. }
  7467. return SCRIPT_CMD_SUCCESS;
  7468. }
  7469. /*==========================================
  7470. * Get npc string information by type
  7471. * return by @type:
  7472. * 0 : name
  7473. * 1 : str#
  7474. * 2 : #str
  7475. * 3 : ::str
  7476. * 4 : map name
  7477. *------------------------------------------*/
  7478. BUILDIN_FUNC(strnpcinfo)
  7479. {
  7480. TBL_NPC* nd;
  7481. int num;
  7482. char *buf,*name=NULL;
  7483. nd = map_id2nd(st->oid);
  7484. if (!nd) {
  7485. script_pushconststr(st, "");
  7486. return SCRIPT_CMD_SUCCESS;
  7487. }
  7488. num = script_getnum(st,2);
  7489. switch(num){
  7490. case 0: // display name
  7491. name = aStrdup(nd->name);
  7492. break;
  7493. case 1: // visible part of display name
  7494. if((buf = strchr(nd->name,'#')) != NULL)
  7495. {
  7496. name = aStrdup(nd->name);
  7497. name[buf - nd->name] = 0;
  7498. } else // Return the name, there is no '#' present
  7499. name = aStrdup(nd->name);
  7500. break;
  7501. case 2: // # fragment
  7502. if((buf = strchr(nd->name,'#')) != NULL)
  7503. name = aStrdup(buf+1);
  7504. break;
  7505. case 3: // unique name
  7506. name = aStrdup(nd->exname);
  7507. break;
  7508. case 4: // map name
  7509. if (nd->bl.m >= 0)
  7510. name = aStrdup(map[nd->bl.m].name);
  7511. break;
  7512. }
  7513. if(name)
  7514. script_pushstr(st, name);
  7515. else
  7516. script_pushconststr(st, "");
  7517. return SCRIPT_CMD_SUCCESS;
  7518. }
  7519. /**
  7520. * getequipid({<equipment slot>,<char_id>})
  7521. **/
  7522. BUILDIN_FUNC(getequipid)
  7523. {
  7524. int i, num;
  7525. TBL_PC* sd;
  7526. if (!script_charid2sd(3, sd)) {
  7527. script_pushint(st,-1);
  7528. return SCRIPT_CMD_FAILURE;
  7529. }
  7530. if (script_hasdata(st, 2))
  7531. num = script_getnum(st, 2);
  7532. else
  7533. num = EQI_COMPOUND_ON;
  7534. if (num == EQI_COMPOUND_ON)
  7535. i = current_equip_item_index;
  7536. else if (equip_index_check(num)) // get inventory position of item
  7537. i = pc_checkequip(sd, equip_bitmask[num]);
  7538. else {
  7539. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7540. script_pushint(st,-1);
  7541. return SCRIPT_CMD_FAILURE;
  7542. }
  7543. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7544. script_pushint(st, sd->inventory_data[i]->nameid);
  7545. else
  7546. script_pushint(st, -1);
  7547. return SCRIPT_CMD_SUCCESS;
  7548. }
  7549. /*==========================================
  7550. * getequipuniqueid(<equipment slot>{,<char_id>})
  7551. *------------------------------------------*/
  7552. BUILDIN_FUNC(getequipuniqueid)
  7553. {
  7554. int i, num;
  7555. TBL_PC* sd;
  7556. struct item* item;
  7557. if (!script_charid2sd(3, sd)) {
  7558. script_pushconststr(st, "");
  7559. return SCRIPT_CMD_FAILURE;
  7560. }
  7561. num = script_getnum(st,2);
  7562. if ( !equip_index_check(num) ) {
  7563. script_pushconststr(st, "");
  7564. return SCRIPT_CMD_FAILURE;
  7565. }
  7566. // get inventory position of item
  7567. i = pc_checkequip(sd,equip_bitmask[num]);
  7568. if (i < 0) {
  7569. script_pushconststr(st, "");
  7570. return SCRIPT_CMD_FAILURE;
  7571. }
  7572. item = &sd->inventory.u.items_inventory[i];
  7573. if (item != 0) {
  7574. int maxlen = 256;
  7575. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7576. memset(buf, 0, maxlen);
  7577. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7578. script_pushstr(st, buf);
  7579. } else
  7580. script_pushconststr(st, "");
  7581. return SCRIPT_CMD_SUCCESS;
  7582. }
  7583. /*==========================================
  7584. * Get the equipement name at pos
  7585. * return item jname or ""
  7586. * getequipname(<equipment slot>{,<char_id>})
  7587. *------------------------------------------*/
  7588. BUILDIN_FUNC(getequipname)
  7589. {
  7590. int i, num;
  7591. TBL_PC* sd;
  7592. struct item_data* item;
  7593. if (!script_charid2sd(3, sd)) {
  7594. script_pushconststr(st, "");
  7595. return SCRIPT_CMD_FAILURE;
  7596. }
  7597. num = script_getnum(st,2);
  7598. if( !equip_index_check(num) )
  7599. {
  7600. script_pushconststr(st,"");
  7601. return SCRIPT_CMD_SUCCESS;
  7602. }
  7603. // get inventory position of item
  7604. i = pc_checkequip(sd,equip_bitmask[num]);
  7605. if( i < 0 )
  7606. {
  7607. script_pushconststr(st,"");
  7608. return SCRIPT_CMD_SUCCESS;
  7609. }
  7610. item = sd->inventory_data[i];
  7611. if( item != 0 )
  7612. script_pushstrcopy(st,item->jname);
  7613. else
  7614. script_pushconststr(st,"");
  7615. return SCRIPT_CMD_SUCCESS;
  7616. }
  7617. /*==========================================
  7618. * getbrokenid [Valaris]
  7619. * getbrokenid(<number>{,<char_id>})
  7620. *------------------------------------------*/
  7621. BUILDIN_FUNC(getbrokenid)
  7622. {
  7623. int i,num,id = 0,brokencounter = 0;
  7624. TBL_PC *sd;
  7625. if (!script_charid2sd(3, sd)) {
  7626. script_pushint(st,0);
  7627. return SCRIPT_CMD_FAILURE;
  7628. }
  7629. num = script_getnum(st,2);
  7630. for(i = 0; i < MAX_INVENTORY; i++) {
  7631. if(sd->inventory.u.items_inventory[i].attribute) {
  7632. brokencounter++;
  7633. if(num == brokencounter){
  7634. id = sd->inventory.u.items_inventory[i].nameid;
  7635. break;
  7636. }
  7637. }
  7638. }
  7639. script_pushint(st,id);
  7640. return SCRIPT_CMD_SUCCESS;
  7641. }
  7642. /*==========================================
  7643. * repair [Valaris]
  7644. * repair <num>{,<char_id>};
  7645. *------------------------------------------*/
  7646. BUILDIN_FUNC(repair)
  7647. {
  7648. int i,num;
  7649. int repaircounter = 0;
  7650. TBL_PC *sd;
  7651. if (!script_charid2sd(3,sd))
  7652. return SCRIPT_CMD_FAILURE;
  7653. num = script_getnum(st,2);
  7654. for(i = 0; i < MAX_INVENTORY; i++) {
  7655. if(sd->inventory.u.items_inventory[i].attribute) {
  7656. repaircounter++;
  7657. if(num == repaircounter) {
  7658. sd->inventory.u.items_inventory[i].attribute = 0;
  7659. clif_equiplist(sd);
  7660. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7661. clif_misceffect(&sd->bl, 3);
  7662. break;
  7663. }
  7664. }
  7665. }
  7666. return SCRIPT_CMD_SUCCESS;
  7667. }
  7668. /*==========================================
  7669. * repairall {<char_id>}
  7670. *------------------------------------------*/
  7671. BUILDIN_FUNC(repairall)
  7672. {
  7673. int i, repaircounter = 0;
  7674. TBL_PC *sd;
  7675. if (!script_charid2sd(2,sd))
  7676. return SCRIPT_CMD_FAILURE;
  7677. for(i = 0; i < MAX_INVENTORY; i++)
  7678. {
  7679. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7680. {
  7681. sd->inventory.u.items_inventory[i].attribute = 0;
  7682. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7683. repaircounter++;
  7684. }
  7685. }
  7686. if(repaircounter)
  7687. {
  7688. clif_misceffect(&sd->bl, 3);
  7689. clif_equiplist(sd);
  7690. }
  7691. return SCRIPT_CMD_SUCCESS;
  7692. }
  7693. /*==========================================
  7694. * Chk if player have something equiped at pos
  7695. * getequipisequiped <pos>{,<char_id>}
  7696. *------------------------------------------*/
  7697. BUILDIN_FUNC(getequipisequiped)
  7698. {
  7699. int i = -1,num;
  7700. TBL_PC *sd;
  7701. num = script_getnum(st,2);
  7702. if (!script_charid2sd(3,sd))
  7703. return SCRIPT_CMD_FAILURE;
  7704. if ( equip_index_check(num) )
  7705. i=pc_checkequip(sd,equip_bitmask[num]);
  7706. if(i >= 0)
  7707. script_pushint(st,1);
  7708. else
  7709. script_pushint(st,0);
  7710. return SCRIPT_CMD_SUCCESS;
  7711. }
  7712. /*==========================================
  7713. * Chk if the player have something equiped at pos
  7714. * if so chk if this item ain't marked not refinable or rental
  7715. * return (npc)
  7716. * 1 : true
  7717. * 0 : false
  7718. * getequipisenableref(<equipment slot>{,<char_id>})
  7719. *------------------------------------------*/
  7720. BUILDIN_FUNC(getequipisenableref)
  7721. {
  7722. int i = -1,num;
  7723. TBL_PC *sd;
  7724. num = script_getnum(st,2);
  7725. if (!script_charid2sd(3, sd)) {
  7726. script_pushint(st,0);
  7727. return SCRIPT_CMD_FAILURE;
  7728. }
  7729. if( equip_index_check(num) )
  7730. i = pc_checkequip(sd,equip_bitmask[num]);
  7731. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7732. script_pushint(st,1);
  7733. else
  7734. script_pushint(st,0);
  7735. return SCRIPT_CMD_SUCCESS;
  7736. }
  7737. /*==========================================
  7738. * Get the item refined value at pos
  7739. * return (npc)
  7740. * x : refine amount
  7741. * 0 : false (not refined)
  7742. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7743. *------------------------------------------*/
  7744. BUILDIN_FUNC(getequiprefinerycnt)
  7745. {
  7746. int i = -1,num;
  7747. TBL_PC *sd;
  7748. num = script_getnum(st,2);
  7749. if (!script_charid2sd(3, sd)) {
  7750. script_pushint(st,0);
  7751. return SCRIPT_CMD_FAILURE;
  7752. }
  7753. if (equip_index_check(num))
  7754. i=pc_checkequip(sd,equip_bitmask[num]);
  7755. if(i >= 0)
  7756. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7757. else
  7758. script_pushint(st,0);
  7759. return SCRIPT_CMD_SUCCESS;
  7760. }
  7761. /*==========================================
  7762. * Get the weapon level value at pos
  7763. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7764. * return (npc)
  7765. * x : weapon level
  7766. * 0 : false
  7767. * getequipweaponlv(<equipment slot>{,<char_id>})
  7768. *------------------------------------------*/
  7769. BUILDIN_FUNC(getequipweaponlv)
  7770. {
  7771. int i = -1, num;
  7772. TBL_PC *sd;
  7773. num = script_getnum(st, 2);
  7774. if (!script_charid2sd(3, sd)) {
  7775. script_pushint(st, 0);
  7776. return SCRIPT_CMD_FAILURE;
  7777. }
  7778. if (num == -1){
  7779. if( current_equip_item_index == -1 ){
  7780. script_pushint(st, 0);
  7781. return SCRIPT_CMD_FAILURE;
  7782. }
  7783. i = current_equip_item_index;
  7784. }else if (equip_index_check(num))
  7785. i = pc_checkequip(sd, equip_bitmask[num]);
  7786. if (i >= 0 && sd->inventory_data[i])
  7787. script_pushint(st, sd->inventory_data[i]->wlv);
  7788. else
  7789. script_pushint(st, 0);
  7790. return SCRIPT_CMD_SUCCESS;
  7791. }
  7792. /*==========================================
  7793. * Get the item refine chance (from refine.txt) for item at pos
  7794. * return (npc)
  7795. * x : refine chance
  7796. * 0 : false (max refine level or unequip..)
  7797. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  7798. *------------------------------------------*/
  7799. BUILDIN_FUNC(getequippercentrefinery)
  7800. {
  7801. int i = -1,num;
  7802. bool enriched = false;
  7803. TBL_PC *sd;
  7804. num = script_getnum(st,2);
  7805. if (script_hasdata(st, 3))
  7806. enriched = script_getnum(st, 3) != 0;
  7807. if (!script_charid2sd(4, sd)) {
  7808. script_pushint(st,0);
  7809. return SCRIPT_CMD_FAILURE;
  7810. }
  7811. if (equip_index_check(num))
  7812. i = pc_checkequip(sd,equip_bitmask[num]);
  7813. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE) {
  7814. enum refine_type type = REFINE_TYPE_SHADOW;
  7815. if (sd->inventory_data[i]->type != IT_SHADOWGEAR)
  7816. type = (enum refine_type)sd->inventory_data[i]->wlv;
  7817. script_pushint(st, status_get_refine_chance(type, (int)sd->inventory.u.items_inventory[i].refine, enriched));
  7818. }
  7819. else
  7820. script_pushint(st,0);
  7821. return SCRIPT_CMD_SUCCESS;
  7822. }
  7823. /*==========================================
  7824. * Refine +1 item at pos and log and display refine
  7825. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7826. *------------------------------------------*/
  7827. BUILDIN_FUNC(successrefitem) {
  7828. short i = -1, up = 1;
  7829. int pos;
  7830. TBL_PC *sd;
  7831. pos = script_getnum(st,2);
  7832. if (!script_charid2sd(4, sd)) {
  7833. script_pushint(st, -1);
  7834. return SCRIPT_CMD_FAILURE;
  7835. }
  7836. if (script_hasdata(st, 3))
  7837. up = script_getnum(st, 3);
  7838. if (equip_index_check(pos))
  7839. i = pc_checkequip(sd,equip_bitmask[pos]);
  7840. if (i >= 0) {
  7841. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7842. //Logs items, got from (N)PC scripts [Lupus]
  7843. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7844. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7845. script_pushint(st, MAX_REFINE);
  7846. return SCRIPT_CMD_SUCCESS;
  7847. }
  7848. sd->inventory.u.items_inventory[i].refine += up;
  7849. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7850. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7851. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7852. clif_delitem(sd,i,1,3);
  7853. //Logs items, got from (N)PC scripts [Lupus]
  7854. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7855. clif_additem(sd,i,1,0);
  7856. pc_equipitem(sd,i,ep);
  7857. clif_misceffect(&sd->bl,3);
  7858. achievement_update_objective(sd, AG_REFINE_SUCCESS, 2, sd->inventory_data[i]->wlv, sd->inventory.u.items_inventory[i].refine);
  7859. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7860. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7861. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7862. { // Fame point system [DracoRPG]
  7863. switch (sd->inventory_data[i]->wlv){
  7864. case 1:
  7865. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7866. break;
  7867. case 2:
  7868. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7869. break;
  7870. case 3:
  7871. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7872. break;
  7873. }
  7874. }
  7875. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7876. return SCRIPT_CMD_SUCCESS;
  7877. }
  7878. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7879. script_pushint(st, -1);
  7880. return SCRIPT_CMD_FAILURE;
  7881. }
  7882. /*==========================================
  7883. * Show a failed Refine +1 attempt
  7884. * failedrefitem <equipment slot>{,<char_id>}
  7885. *------------------------------------------*/
  7886. BUILDIN_FUNC(failedrefitem) {
  7887. short i = -1;
  7888. int pos;
  7889. TBL_PC *sd;
  7890. pos = script_getnum(st,2);
  7891. if (!script_charid2sd(3, sd)) {
  7892. script_pushint(st, 0);
  7893. return SCRIPT_CMD_FAILURE;
  7894. }
  7895. if (equip_index_check(pos))
  7896. i = pc_checkequip(sd,equip_bitmask[pos]);
  7897. if (i >= 0) {
  7898. sd->inventory.u.items_inventory[i].refine = 0;
  7899. pc_unequipitem(sd,i,3); //recalculate bonus
  7900. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7901. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7902. clif_misceffect(&sd->bl,2); // display failure effect
  7903. achievement_update_objective(sd, AG_REFINE_FAIL, 1, 1);
  7904. script_pushint(st, 1);
  7905. return SCRIPT_CMD_SUCCESS;
  7906. }
  7907. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7908. script_pushint(st, 0);
  7909. return SCRIPT_CMD_FAILURE;
  7910. }
  7911. /*==========================================
  7912. * Downgrades an Equipment Part by -1 . [Masao]
  7913. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7914. *------------------------------------------*/
  7915. BUILDIN_FUNC(downrefitem) {
  7916. short i = -1, down = 1;
  7917. int pos;
  7918. TBL_PC *sd;
  7919. if (!script_charid2sd(4, sd)) {
  7920. script_pushint(st, -1);
  7921. return SCRIPT_CMD_FAILURE;
  7922. }
  7923. pos = script_getnum(st,2);
  7924. if (script_hasdata(st, 3))
  7925. down = script_getnum(st, 3);
  7926. if (equip_index_check(pos))
  7927. i = pc_checkequip(sd,equip_bitmask[pos]);
  7928. if (i >= 0) {
  7929. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7930. //Logs items, got from (N)PC scripts [Lupus]
  7931. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7932. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7933. sd->inventory.u.items_inventory[i].refine -= down;
  7934. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7935. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7936. clif_delitem(sd,i,1,3);
  7937. //Logs items, got from (N)PC scripts [Lupus]
  7938. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7939. clif_additem(sd,i,1,0);
  7940. pc_equipitem(sd,i,ep);
  7941. clif_misceffect(&sd->bl,2);
  7942. achievement_update_objective(sd, AG_REFINE_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  7943. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7944. return SCRIPT_CMD_SUCCESS;
  7945. }
  7946. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7947. script_pushint(st, -1);
  7948. return SCRIPT_CMD_FAILURE;
  7949. }
  7950. /**
  7951. * Delete the item equipped at pos.
  7952. * delequip <equipment slot>{,<char_id>};
  7953. **/
  7954. BUILDIN_FUNC(delequip) {
  7955. short i = -1;
  7956. int pos;
  7957. int8 ret;
  7958. TBL_PC *sd;
  7959. pos = script_getnum(st,2);
  7960. if (!script_charid2sd(3,sd)) {
  7961. st->state = END;
  7962. return SCRIPT_CMD_FAILURE;
  7963. }
  7964. if (equip_index_check(pos))
  7965. i = pc_checkequip(sd,equip_bitmask[pos]);
  7966. if (i >= 0) {
  7967. pc_unequipitem(sd,i,3); //recalculate bonus
  7968. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7969. }
  7970. else {
  7971. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7972. st->state = END;
  7973. return SCRIPT_CMD_FAILURE;
  7974. }
  7975. script_pushint(st,ret);
  7976. return SCRIPT_CMD_SUCCESS;
  7977. }
  7978. /**
  7979. * Break the item equipped at pos.
  7980. * breakequip <equipment slot>{,<char_id>};
  7981. **/
  7982. BUILDIN_FUNC(breakequip) {
  7983. short i = -1;
  7984. int pos;
  7985. TBL_PC *sd;
  7986. pos = script_getnum(st,2);
  7987. if (!script_charid2sd(3,sd))
  7988. return SCRIPT_CMD_FAILURE;
  7989. if (equip_index_check(pos))
  7990. i = pc_checkequip(sd,equip_bitmask[pos]);
  7991. if (i >= 0) {
  7992. sd->inventory.u.items_inventory[i].attribute = 1;
  7993. pc_unequipitem(sd,i,3);
  7994. clif_equiplist(sd);
  7995. script_pushint(st,1);
  7996. return SCRIPT_CMD_SUCCESS;
  7997. }
  7998. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7999. script_pushint(st,0);
  8000. return SCRIPT_CMD_FAILURE;
  8001. }
  8002. /**
  8003. * statusup <stat>{,<char_id>};
  8004. **/
  8005. BUILDIN_FUNC(statusup)
  8006. {
  8007. int type;
  8008. TBL_PC *sd;
  8009. type = script_getnum(st,2);
  8010. if (!script_charid2sd(3, sd))
  8011. return SCRIPT_CMD_FAILURE;
  8012. pc_statusup(sd, type, 1);
  8013. return SCRIPT_CMD_SUCCESS;
  8014. }
  8015. /**
  8016. * statusup2 <stat>,<amount>{,<char_id>};
  8017. **/
  8018. BUILDIN_FUNC(statusup2)
  8019. {
  8020. int type,val;
  8021. TBL_PC *sd;
  8022. type=script_getnum(st,2);
  8023. val=script_getnum(st,3);
  8024. if (!script_charid2sd(4, sd))
  8025. return SCRIPT_CMD_FAILURE;
  8026. pc_statusup2(sd,type,val);
  8027. return SCRIPT_CMD_SUCCESS;
  8028. }
  8029. /// See 'doc/item_bonus.txt'
  8030. ///
  8031. /// bonus <bonus type>,<val1>;
  8032. /// bonus2 <bonus type>,<val1>,<val2>;
  8033. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8034. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8035. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8036. BUILDIN_FUNC(bonus)
  8037. {
  8038. int type;
  8039. int val1 = 0;
  8040. int val2 = 0;
  8041. int val3 = 0;
  8042. int val4 = 0;
  8043. int val5 = 0;
  8044. TBL_PC* sd;
  8045. struct script_data *data;
  8046. if( !script_rid2sd(sd) )
  8047. return SCRIPT_CMD_SUCCESS; // no player attached
  8048. type = script_getnum(st,2);
  8049. switch( type ) {
  8050. case SP_AUTOSPELL:
  8051. case SP_AUTOSPELL_WHENHIT:
  8052. case SP_AUTOSPELL_ONSKILL:
  8053. case SP_SKILL_ATK:
  8054. case SP_SKILL_HEAL:
  8055. case SP_SKILL_HEAL2:
  8056. case SP_ADD_SKILL_BLOW:
  8057. case SP_CASTRATE:
  8058. case SP_ADDEFF_ONSKILL:
  8059. case SP_SKILL_USE_SP_RATE:
  8060. case SP_SKILL_COOLDOWN:
  8061. case SP_SKILL_FIXEDCAST:
  8062. case SP_SKILL_VARIABLECAST:
  8063. case SP_VARCASTRATE:
  8064. case SP_FIXCASTRATE:
  8065. case SP_SKILL_DELAY:
  8066. case SP_SKILL_USE_SP:
  8067. case SP_SUB_SKILL:
  8068. // these bonuses support skill names
  8069. data = script_getdata(st, 3);
  8070. get_val(st, data); // Convert into value in case of a variable
  8071. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8072. break;
  8073. default:
  8074. if (script_hasdata(st, 3))
  8075. val1 = script_getnum(st, 3);
  8076. break;
  8077. }
  8078. switch( script_lastdata(st)-2 ) {
  8079. case 0:
  8080. case 1:
  8081. pc_bonus(sd, type, val1);
  8082. break;
  8083. case 2:
  8084. val2 = script_getnum(st,4);
  8085. pc_bonus2(sd, type, val1, val2);
  8086. break;
  8087. case 3:
  8088. val2 = script_getnum(st,4);
  8089. val3 = script_getnum(st,5);
  8090. pc_bonus3(sd, type, val1, val2, val3);
  8091. break;
  8092. case 4:
  8093. data = script_getdata(st, 4);
  8094. get_val(st, data); // Convert into value in case of a variable
  8095. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  8096. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8097. else
  8098. val2 = script_getnum(st,4);
  8099. val3 = script_getnum(st,5);
  8100. val4 = script_getnum(st,6);
  8101. pc_bonus4(sd, type, val1, val2, val3, val4);
  8102. break;
  8103. case 5:
  8104. data = script_getdata(st, 4);
  8105. get_val(st, data); // Convert into value in case of a variable
  8106. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  8107. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8108. else
  8109. val2 = script_getnum(st,4);
  8110. val3 = script_getnum(st,5);
  8111. val4 = script_getnum(st,6);
  8112. val5 = script_getnum(st,7);
  8113. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8114. break;
  8115. default:
  8116. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8117. break;
  8118. }
  8119. return SCRIPT_CMD_SUCCESS;
  8120. }
  8121. BUILDIN_FUNC(autobonus)
  8122. {
  8123. unsigned int dur, pos;
  8124. short rate;
  8125. short atk_type = 0;
  8126. TBL_PC* sd;
  8127. const char *bonus_script, *other_script = NULL;
  8128. if( !script_rid2sd(sd) )
  8129. return SCRIPT_CMD_SUCCESS; // no player attached
  8130. if (current_equip_combo_pos)
  8131. pos = current_equip_combo_pos;
  8132. else
  8133. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8134. if((sd->state.autobonus&pos) == pos)
  8135. return SCRIPT_CMD_SUCCESS;
  8136. rate = script_getnum(st,3);
  8137. dur = script_getnum(st,4);
  8138. bonus_script = script_getstr(st,2);
  8139. if( !rate || !dur || !pos || !bonus_script )
  8140. return SCRIPT_CMD_SUCCESS;
  8141. if( script_hasdata(st,5) )
  8142. atk_type = script_getnum(st,5);
  8143. if( script_hasdata(st,6) )
  8144. other_script = script_getstr(st,6);
  8145. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  8146. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  8147. {
  8148. script_add_autobonus(bonus_script);
  8149. if( other_script )
  8150. script_add_autobonus(other_script);
  8151. }
  8152. return SCRIPT_CMD_SUCCESS;
  8153. }
  8154. BUILDIN_FUNC(autobonus2)
  8155. {
  8156. unsigned int dur, pos;
  8157. short rate;
  8158. short atk_type = 0;
  8159. TBL_PC* sd;
  8160. const char *bonus_script, *other_script = NULL;
  8161. if( !script_rid2sd(sd) )
  8162. return SCRIPT_CMD_SUCCESS; // no player attached
  8163. if (current_equip_combo_pos)
  8164. pos = current_equip_combo_pos;
  8165. else
  8166. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8167. if((sd->state.autobonus&pos) == pos)
  8168. return SCRIPT_CMD_SUCCESS;
  8169. rate = script_getnum(st,3);
  8170. dur = script_getnum(st,4);
  8171. bonus_script = script_getstr(st,2);
  8172. if( !rate || !dur || !pos || !bonus_script )
  8173. return SCRIPT_CMD_SUCCESS;
  8174. if( script_hasdata(st,5) )
  8175. atk_type = script_getnum(st,5);
  8176. if( script_hasdata(st,6) )
  8177. other_script = script_getstr(st,6);
  8178. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  8179. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  8180. {
  8181. script_add_autobonus(bonus_script);
  8182. if( other_script )
  8183. script_add_autobonus(other_script);
  8184. }
  8185. return SCRIPT_CMD_SUCCESS;
  8186. }
  8187. BUILDIN_FUNC(autobonus3)
  8188. {
  8189. unsigned int dur, pos;
  8190. short rate,atk_type;
  8191. TBL_PC* sd;
  8192. const char *bonus_script, *other_script = NULL;
  8193. struct script_data *data;
  8194. if( !script_rid2sd(sd) )
  8195. return SCRIPT_CMD_SUCCESS; // no player attached
  8196. if (current_equip_combo_pos)
  8197. pos = current_equip_combo_pos;
  8198. else
  8199. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8200. if((sd->state.autobonus&pos) == pos)
  8201. return SCRIPT_CMD_SUCCESS;
  8202. rate = script_getnum(st,3);
  8203. dur = script_getnum(st,4);
  8204. data = script_getdata(st, 5);
  8205. get_val(st, data); // Convert into value in case of a variable
  8206. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  8207. bonus_script = script_getstr(st,2);
  8208. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  8209. return SCRIPT_CMD_SUCCESS;
  8210. if( script_hasdata(st,6) )
  8211. other_script = script_getstr(st,6);
  8212. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  8213. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  8214. {
  8215. script_add_autobonus(bonus_script);
  8216. if( other_script )
  8217. script_add_autobonus(other_script);
  8218. }
  8219. return SCRIPT_CMD_SUCCESS;
  8220. }
  8221. /// Changes the level of a player skill.
  8222. /// <flag> defaults to 1
  8223. /// <flag>=0 : set the level of the skill
  8224. /// <flag>=1 : set the temporary level of the skill
  8225. /// <flag>=2 : add to the level of the skill
  8226. ///
  8227. /// skill <skill id>,<level>,<flag>
  8228. /// skill <skill id>,<level>
  8229. /// skill "<skill name>",<level>,<flag>
  8230. /// skill "<skill name>",<level>
  8231. BUILDIN_FUNC(skill)
  8232. {
  8233. int id;
  8234. int level;
  8235. int flag = ADDSKILL_TEMP;
  8236. TBL_PC* sd;
  8237. struct script_data *data;
  8238. const char* command = script_getfuncname(st);
  8239. if( !script_rid2sd(sd) )
  8240. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8241. if (strcmpi(command, "addtoskill") == 0)
  8242. flag = ADDSKILL_TEMP_ADDLEVEL;
  8243. data = script_getdata(st, 2);
  8244. get_val(st, data); // Convert into value in case of a variable
  8245. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8246. level = script_getnum(st,3);
  8247. if( script_hasdata(st,4) )
  8248. flag = script_getnum(st,4);
  8249. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8250. return SCRIPT_CMD_SUCCESS;
  8251. }
  8252. /// Increases the level of a guild skill.
  8253. ///
  8254. /// guildskill <skill id>,<amount>;
  8255. /// guildskill "<skill name>",<amount>;
  8256. BUILDIN_FUNC(guildskill)
  8257. {
  8258. int id;
  8259. int level;
  8260. TBL_PC* sd;
  8261. int i;
  8262. struct script_data *data;
  8263. if( !script_rid2sd(sd) )
  8264. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8265. data = script_getdata(st, 2);
  8266. get_val(st, data); // Convert into value in case of a variable
  8267. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8268. level = script_getnum(st,3);
  8269. for( i=0; i < level; i++ )
  8270. guild_skillup(sd, id);
  8271. return SCRIPT_CMD_SUCCESS;
  8272. }
  8273. /// Returns the level of the player skill.
  8274. ///
  8275. /// getskilllv(<skill id>) -> <level>
  8276. /// getskilllv("<skill name>") -> <level>
  8277. BUILDIN_FUNC(getskilllv)
  8278. {
  8279. int id;
  8280. TBL_PC* sd;
  8281. struct script_data *data;
  8282. if( !script_rid2sd(sd) )
  8283. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8284. data = script_getdata(st, 2);
  8285. get_val(st, data); // Convert into value in case of a variable
  8286. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8287. script_pushint(st, pc_checkskill(sd,id));
  8288. return SCRIPT_CMD_SUCCESS;
  8289. }
  8290. /// Returns the level of the guild skill.
  8291. ///
  8292. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8293. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8294. BUILDIN_FUNC(getgdskilllv)
  8295. {
  8296. int guild_id;
  8297. uint16 skill_id;
  8298. struct guild* g;
  8299. struct script_data *data;
  8300. guild_id = script_getnum(st,2);
  8301. data = script_getdata(st, 3);
  8302. get_val(st, data); // Convert into value in case of a variable
  8303. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8304. g = guild_search(guild_id);
  8305. if( g == NULL )
  8306. script_pushint(st, -1);
  8307. else
  8308. script_pushint(st, guild_checkskill(g,skill_id));
  8309. return SCRIPT_CMD_SUCCESS;
  8310. }
  8311. /// Returns the 'basic_skill_check' setting.
  8312. /// This config determines if the server checks the skill level of NV_BASIC
  8313. /// before allowing the basic actions.
  8314. ///
  8315. /// basicskillcheck() -> <bool>
  8316. BUILDIN_FUNC(basicskillcheck)
  8317. {
  8318. script_pushint(st, battle_config.basic_skill_check);
  8319. return SCRIPT_CMD_SUCCESS;
  8320. }
  8321. /// Returns the GM level of the player.
  8322. ///
  8323. /// getgmlevel({<char_id>}) -> <level>
  8324. BUILDIN_FUNC(getgmlevel)
  8325. {
  8326. TBL_PC* sd;
  8327. if (!script_charid2sd(2,sd))
  8328. return SCRIPT_CMD_FAILURE;
  8329. script_pushint(st, pc_get_group_level(sd));
  8330. return SCRIPT_CMD_SUCCESS;
  8331. }
  8332. /// Returns the group ID of the player.
  8333. ///
  8334. /// getgroupid({<char_id>}) -> <int>
  8335. BUILDIN_FUNC(getgroupid)
  8336. {
  8337. TBL_PC* sd;
  8338. if (!script_charid2sd(2,sd))
  8339. return SCRIPT_CMD_FAILURE;
  8340. script_pushint(st, pc_get_group_id(sd));
  8341. return SCRIPT_CMD_SUCCESS;
  8342. }
  8343. /// Terminates the execution of this script instance.
  8344. ///
  8345. /// end
  8346. BUILDIN_FUNC(end)
  8347. {
  8348. TBL_PC* sd;
  8349. sd = map_id2sd(st->rid);
  8350. st->state = END;
  8351. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8352. int i;
  8353. for(i = 0; i < st->stack->sp; i++) {
  8354. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8355. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8356. st->script = ri->script;
  8357. break;
  8358. }
  8359. }
  8360. }
  8361. if( st->mes_active )
  8362. st->mes_active = 0;
  8363. if (sd){
  8364. if (sd->state.callshop == 0)
  8365. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8366. else
  8367. sd->state.callshop = 0;
  8368. }
  8369. return SCRIPT_CMD_SUCCESS;
  8370. }
  8371. /// Checks if the player has that effect state (option).
  8372. ///
  8373. /// checkoption(<option>{,<char_id>}) -> <bool>
  8374. BUILDIN_FUNC(checkoption)
  8375. {
  8376. int option;
  8377. TBL_PC* sd;
  8378. if (!script_charid2sd(3,sd))
  8379. return SCRIPT_CMD_FAILURE;
  8380. option = script_getnum(st,2);
  8381. if( sd->sc.option&option )
  8382. script_pushint(st, 1);
  8383. else
  8384. script_pushint(st, 0);
  8385. return SCRIPT_CMD_SUCCESS;
  8386. }
  8387. /// Checks if the player is in that body state (opt1).
  8388. ///
  8389. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8390. BUILDIN_FUNC(checkoption1)
  8391. {
  8392. int opt1;
  8393. TBL_PC* sd;
  8394. if (!script_charid2sd(3,sd))
  8395. return SCRIPT_CMD_FAILURE;
  8396. opt1 = script_getnum(st,2);
  8397. if( sd->sc.opt1 == opt1 )
  8398. script_pushint(st, 1);
  8399. else
  8400. script_pushint(st, 0);
  8401. return SCRIPT_CMD_SUCCESS;
  8402. }
  8403. /// Checks if the player has that health state (opt2).
  8404. ///
  8405. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8406. BUILDIN_FUNC(checkoption2)
  8407. {
  8408. int opt2;
  8409. TBL_PC* sd;
  8410. if (!script_charid2sd(3,sd))
  8411. return SCRIPT_CMD_FAILURE;
  8412. opt2 = script_getnum(st,2);
  8413. if( sd->sc.opt2&opt2 )
  8414. script_pushint(st, 1);
  8415. else
  8416. script_pushint(st, 0);
  8417. return SCRIPT_CMD_SUCCESS;
  8418. }
  8419. /// Changes the effect state (option) of the player.
  8420. /// <flag> defaults to 1
  8421. /// <flag>=0 : removes the option
  8422. /// <flag>=other : adds the option
  8423. ///
  8424. /// setoption <option>{,<flag>{,<char_id>}};
  8425. BUILDIN_FUNC(setoption)
  8426. {
  8427. int option;
  8428. int flag = 1;
  8429. TBL_PC* sd;
  8430. if (!script_charid2sd(4,sd))
  8431. return SCRIPT_CMD_FAILURE;
  8432. option = script_getnum(st,2);
  8433. if( script_hasdata(st,3) )
  8434. flag = script_getnum(st,3);
  8435. else if( !option ){// Request to remove everything.
  8436. flag = 0;
  8437. option = OPTION_FALCON|OPTION_RIDING;
  8438. #ifndef NEW_CARTS
  8439. option |= OPTION_CART;
  8440. #endif
  8441. }
  8442. if( flag ){// Add option
  8443. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8444. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8445. pc_setoption(sd, sd->sc.option|option);
  8446. } else// Remove option
  8447. pc_setoption(sd, sd->sc.option&~option);
  8448. return SCRIPT_CMD_SUCCESS;
  8449. }
  8450. /// Returns if the player has a cart.
  8451. ///
  8452. /// checkcart({char_id}) -> <bool>
  8453. ///
  8454. /// @author Valaris
  8455. BUILDIN_FUNC(checkcart)
  8456. {
  8457. TBL_PC* sd;
  8458. if (!script_charid2sd(2,sd))
  8459. return SCRIPT_CMD_FAILURE;
  8460. if( pc_iscarton(sd) )
  8461. script_pushint(st, 1);
  8462. else
  8463. script_pushint(st, 0);
  8464. return SCRIPT_CMD_SUCCESS;
  8465. }
  8466. /// Sets the cart of the player.
  8467. /// <type> defaults to 1
  8468. /// <type>=0 : removes the cart
  8469. /// <type>=1 : Normal cart
  8470. /// <type>=2 : Wooden cart
  8471. /// <type>=3 : Covered cart with flowers and ferns
  8472. /// <type>=4 : Wooden cart with a Panda doll on the back
  8473. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8474. ///
  8475. /// setcart {<type>{,<char_id>}};
  8476. BUILDIN_FUNC(setcart)
  8477. {
  8478. int type = 1;
  8479. TBL_PC* sd;
  8480. if (!script_charid2sd(3,sd))
  8481. return SCRIPT_CMD_FAILURE;
  8482. if( script_hasdata(st,2) )
  8483. type = script_getnum(st,2);
  8484. pc_setcart(sd, type);
  8485. return SCRIPT_CMD_SUCCESS;
  8486. }
  8487. /// Returns if the player has a falcon.
  8488. ///
  8489. /// checkfalcon({char_id}) -> <bool>
  8490. ///
  8491. /// @author Valaris
  8492. BUILDIN_FUNC(checkfalcon)
  8493. {
  8494. TBL_PC* sd;
  8495. if (!script_charid2sd(2,sd))
  8496. return SCRIPT_CMD_FAILURE;
  8497. if( pc_isfalcon(sd) )
  8498. script_pushint(st, 1);
  8499. else
  8500. script_pushint(st, 0);
  8501. return SCRIPT_CMD_SUCCESS;
  8502. }
  8503. /// Sets if the player has a falcon or not.
  8504. /// <flag> defaults to 1
  8505. ///
  8506. /// setfalcon {<flag>{,<char_id>}};
  8507. BUILDIN_FUNC(setfalcon)
  8508. {
  8509. int flag = 1;
  8510. TBL_PC* sd;
  8511. if (!script_charid2sd(3,sd))
  8512. return SCRIPT_CMD_FAILURE;
  8513. if( script_hasdata(st,2) )
  8514. flag = script_getnum(st,2);
  8515. pc_setfalcon(sd, flag);
  8516. return SCRIPT_CMD_SUCCESS;
  8517. }
  8518. /// Returns if the player is riding.
  8519. ///
  8520. /// checkriding({char_id}) -> <bool>
  8521. ///
  8522. /// @author Valaris
  8523. BUILDIN_FUNC(checkriding)
  8524. {
  8525. TBL_PC* sd;
  8526. if (!script_charid2sd(2,sd))
  8527. return SCRIPT_CMD_FAILURE;
  8528. if( pc_isriding(sd) )
  8529. script_pushint(st, 1);
  8530. else
  8531. script_pushint(st, 0);
  8532. return SCRIPT_CMD_SUCCESS;
  8533. }
  8534. /// Sets if the player is riding.
  8535. /// <flag> defaults to 1
  8536. ///
  8537. /// setriding {<flag>{,<char_id>}};
  8538. BUILDIN_FUNC(setriding)
  8539. {
  8540. int flag = 1;
  8541. TBL_PC* sd;
  8542. if (!script_charid2sd(3,sd))
  8543. return SCRIPT_CMD_FAILURE;
  8544. if( script_hasdata(st,2) )
  8545. flag = script_getnum(st,2);
  8546. pc_setriding(sd, flag);
  8547. return SCRIPT_CMD_SUCCESS;
  8548. }
  8549. /// Returns if the player has a warg.
  8550. ///
  8551. /// checkwug({char_id}) -> <bool>
  8552. ///
  8553. BUILDIN_FUNC(checkwug)
  8554. {
  8555. TBL_PC* sd;
  8556. if (!script_charid2sd(2,sd))
  8557. return SCRIPT_CMD_FAILURE;
  8558. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8559. script_pushint(st, 1);
  8560. else
  8561. script_pushint(st, 0);
  8562. return SCRIPT_CMD_SUCCESS;
  8563. }
  8564. /// Returns if the player is wearing MADO Gear.
  8565. ///
  8566. /// checkmadogear({<char_id>}) -> <bool>
  8567. ///
  8568. BUILDIN_FUNC(checkmadogear)
  8569. {
  8570. TBL_PC* sd;
  8571. if (!script_charid2sd(2,sd))
  8572. return SCRIPT_CMD_FAILURE;
  8573. if( pc_ismadogear(sd) )
  8574. script_pushint(st, 1);
  8575. else
  8576. script_pushint(st, 0);
  8577. return SCRIPT_CMD_SUCCESS;
  8578. }
  8579. /// Sets if the player is riding MADO Gear.
  8580. /// <flag> defaults to 1
  8581. ///
  8582. /// setmadogear {<flag>{,<char_id>}};
  8583. BUILDIN_FUNC(setmadogear)
  8584. {
  8585. int flag = 1;
  8586. TBL_PC* sd;
  8587. if (!script_charid2sd(3,sd))
  8588. return SCRIPT_CMD_FAILURE;
  8589. if( script_hasdata(st,2) )
  8590. flag = script_getnum(st,2);
  8591. pc_setmadogear(sd, flag);
  8592. return SCRIPT_CMD_SUCCESS;
  8593. }
  8594. /// Sets the save point of the player.
  8595. ///
  8596. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8597. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8598. BUILDIN_FUNC(savepoint)
  8599. {
  8600. int x, y, m, cid_pos = 5;
  8601. unsigned short map_idx;
  8602. const char* str;
  8603. TBL_PC* sd;
  8604. if (script_lastdata(st) > 5)
  8605. cid_pos = 7;
  8606. if (!script_charid2sd(cid_pos,sd))
  8607. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8608. str = script_getstr(st, 2);
  8609. map_idx = mapindex_name2id(str);
  8610. if( !map_idx )
  8611. return SCRIPT_CMD_FAILURE;
  8612. x = script_getnum(st,3);
  8613. y = script_getnum(st,4);
  8614. m = map_mapindex2mapid(map_idx);
  8615. if (cid_pos == 7) {
  8616. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8617. x1 = x + dx, y1 = y + dy,
  8618. x0 = x - dx, y0 = y - dy;
  8619. uint8 n = 10;
  8620. do {
  8621. x = x0 + rnd()%(x1-x0+1);
  8622. y = y0 + rnd()%(y1-y0+1);
  8623. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8624. }
  8625. // Check for valid coordinates if map in local map-server
  8626. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8627. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8628. return SCRIPT_CMD_FAILURE;
  8629. }
  8630. pc_setsavepoint(sd, map_idx, x, y);
  8631. return SCRIPT_CMD_SUCCESS;
  8632. }
  8633. /*==========================================
  8634. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8635. *------------------------------------------*/
  8636. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8637. {
  8638. int type;
  8639. time_t timer;
  8640. struct tm *t;
  8641. type=script_getnum(st,2);
  8642. switch(type){
  8643. case 2:
  8644. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8645. // from the system clock.)
  8646. script_pushint(st,(int)time(NULL));
  8647. break;
  8648. case 1:
  8649. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8650. time(&timer);
  8651. t=localtime(&timer);
  8652. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8653. break;
  8654. case 0:
  8655. default:
  8656. //type 0:(System Ticks)
  8657. script_pushint(st,gettick());
  8658. break;
  8659. }
  8660. return SCRIPT_CMD_SUCCESS;
  8661. }
  8662. /*==========================================
  8663. * GetTime(Type)
  8664. *
  8665. * Returns the current value of a certain date type.
  8666. * Possible types are:
  8667. * - DT_SECOND Current seconds
  8668. * - DT_MINUTE Current minute
  8669. * - DT_HOUR Current hour
  8670. * - DT_DAYOFWEEK Day of current week
  8671. * - DT_DAYOFMONTH Day of current month
  8672. * - DT_MONTH Current month
  8673. * - DT_YEAR Current year
  8674. * - DT_DAYOFYEAR Day of current year
  8675. *
  8676. * If none of the above types is supplied -1 will be returned to the script
  8677. * and the script source will be reported into the mapserver console.
  8678. *------------------------------------------*/
  8679. BUILDIN_FUNC(gettime)
  8680. {
  8681. int type;
  8682. type = script_getnum(st,2);
  8683. if( type <= DT_MIN || type >= DT_MAX ){
  8684. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8685. script_reportsrc(st);
  8686. script_pushint(st,-1);
  8687. }else{
  8688. script_pushint(st,date_get((enum e_date_type)type));
  8689. }
  8690. return SCRIPT_CMD_SUCCESS;
  8691. }
  8692. /**
  8693. * Returns the current server time or the provided time in a readable format.
  8694. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  8695. */
  8696. BUILDIN_FUNC(gettimestr)
  8697. {
  8698. char *tmpstr;
  8699. const char *fmtstr;
  8700. int maxlen;
  8701. time_t now;
  8702. fmtstr = script_getstr(st,2);
  8703. maxlen = script_getnum(st,3);
  8704. if (script_hasdata(st, 4)) {
  8705. if (script_getnum(st, 4) < 0) {
  8706. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  8707. return SCRIPT_CMD_FAILURE;
  8708. } else
  8709. now = (time_t)script_getnum(st, 4);
  8710. } else
  8711. now = time(NULL);
  8712. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  8713. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8714. tmpstr[maxlen] = '\0';
  8715. script_pushstr(st,tmpstr);
  8716. return SCRIPT_CMD_SUCCESS;
  8717. }
  8718. /*==========================================
  8719. * Open player storage
  8720. *------------------------------------------*/
  8721. BUILDIN_FUNC(openstorage)
  8722. {
  8723. TBL_PC* sd;
  8724. if( !script_rid2sd(sd) )
  8725. return SCRIPT_CMD_SUCCESS;
  8726. storage_storageopen(sd);
  8727. return SCRIPT_CMD_SUCCESS;
  8728. }
  8729. BUILDIN_FUNC(guildopenstorage)
  8730. {
  8731. TBL_PC* sd;
  8732. int ret;
  8733. if( !script_rid2sd(sd) )
  8734. return SCRIPT_CMD_SUCCESS;
  8735. ret = storage_guild_storageopen(sd);
  8736. script_pushint(st,ret);
  8737. return SCRIPT_CMD_SUCCESS;
  8738. }
  8739. /*==========================================
  8740. * Make player use a skill trought item usage
  8741. *------------------------------------------*/
  8742. /// itemskill <skill id>,<level>
  8743. /// itemskill "<skill name>",<level>
  8744. BUILDIN_FUNC(itemskill)
  8745. {
  8746. int id;
  8747. int lv;
  8748. bool keep_requirement;
  8749. TBL_PC* sd;
  8750. struct script_data *data;
  8751. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  8752. return SCRIPT_CMD_SUCCESS;
  8753. data = script_getdata(st, 2);
  8754. get_val(st, data); // Convert into value in case of a variable
  8755. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8756. lv = script_getnum(st,3);
  8757. if (script_hasdata(st, 4)) {
  8758. keep_requirement = (script_getnum(st, 4) != 0);
  8759. } else {
  8760. keep_requirement = false;
  8761. }
  8762. sd->skillitem=id;
  8763. sd->skillitemlv=lv;
  8764. sd->skillitem_keep_requirement = keep_requirement;
  8765. clif_item_skill(sd,id,lv);
  8766. return SCRIPT_CMD_SUCCESS;
  8767. }
  8768. /*==========================================
  8769. * Attempt to create an item
  8770. *------------------------------------------*/
  8771. BUILDIN_FUNC(produce)
  8772. {
  8773. int trigger;
  8774. TBL_PC* sd;
  8775. if( !script_rid2sd(sd) )
  8776. return SCRIPT_CMD_SUCCESS;
  8777. trigger=script_getnum(st,2);
  8778. clif_skill_produce_mix_list(sd, -1, trigger);
  8779. return SCRIPT_CMD_SUCCESS;
  8780. }
  8781. /*==========================================
  8782. *
  8783. *------------------------------------------*/
  8784. BUILDIN_FUNC(cooking)
  8785. {
  8786. int trigger;
  8787. TBL_PC* sd;
  8788. if( !script_rid2sd(sd) )
  8789. return SCRIPT_CMD_SUCCESS;
  8790. trigger=script_getnum(st,2);
  8791. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8792. return SCRIPT_CMD_SUCCESS;
  8793. }
  8794. /*==========================================
  8795. * Create a pet
  8796. *------------------------------------------*/
  8797. BUILDIN_FUNC(makepet)
  8798. {
  8799. struct map_session_data* sd;
  8800. uint16 mob_id;
  8801. struct s_pet_db* pet;
  8802. if( !script_rid2sd(sd) )
  8803. return SCRIPT_CMD_FAILURE;
  8804. mob_id = script_getnum(st,2);
  8805. pet = pet_db(mob_id);
  8806. if( !pet ){
  8807. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  8808. return SCRIPT_CMD_FAILURE;
  8809. }
  8810. sd->catch_target_class = mob_id;
  8811. intif_create_pet( sd->status.account_id, sd->status.char_id, pet->class_, mob_db(pet->class_)->lv, pet->EggID, 0, pet->intimate, 100, 0, 1, pet->jname );
  8812. return SCRIPT_CMD_SUCCESS;
  8813. }
  8814. /**
  8815. * Give player exp base,job * quest_exp_rate/100
  8816. * getexp <base xp>,<job xp>{,<char_id>};
  8817. **/
  8818. BUILDIN_FUNC(getexp)
  8819. {
  8820. TBL_PC* sd;
  8821. int base=0,job=0;
  8822. double bonus;
  8823. if (!script_charid2sd(4,sd))
  8824. return SCRIPT_CMD_FAILURE;
  8825. base=script_getnum(st,2);
  8826. job =script_getnum(st,3);
  8827. if(base<0 || job<0)
  8828. return SCRIPT_CMD_SUCCESS;
  8829. // bonus for npc-given exp
  8830. bonus = battle_config.quest_exp_rate / 100.;
  8831. if (base)
  8832. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8833. if (job)
  8834. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8835. pc_gainexp(sd, NULL, base, job, 1);
  8836. return SCRIPT_CMD_SUCCESS;
  8837. }
  8838. /*==========================================
  8839. * Gain guild exp [Celest]
  8840. *------------------------------------------*/
  8841. BUILDIN_FUNC(guildgetexp)
  8842. {
  8843. TBL_PC* sd;
  8844. int exp;
  8845. if( !script_rid2sd(sd) )
  8846. return SCRIPT_CMD_SUCCESS;
  8847. exp = script_getnum(st,2);
  8848. if(exp < 0)
  8849. return SCRIPT_CMD_SUCCESS;
  8850. if(sd && sd->status.guild_id > 0)
  8851. guild_getexp (sd, exp);
  8852. return SCRIPT_CMD_SUCCESS;
  8853. }
  8854. /*==========================================
  8855. * Changes the guild master of a guild [Skotlex]
  8856. *------------------------------------------*/
  8857. BUILDIN_FUNC(guildchangegm)
  8858. {
  8859. TBL_PC *sd;
  8860. int guild_id;
  8861. const char *name;
  8862. guild_id = script_getnum(st,2);
  8863. name = script_getstr(st,3);
  8864. sd=map_nick2sd(name,false);
  8865. if (!sd)
  8866. script_pushint(st,0);
  8867. else
  8868. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  8869. return SCRIPT_CMD_SUCCESS;
  8870. }
  8871. /*==========================================
  8872. * Spawn a monster:
  8873. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8874. *------------------------------------------*/
  8875. BUILDIN_FUNC(monster)
  8876. {
  8877. const char* mapn = script_getstr(st,2);
  8878. int x = script_getnum(st,3);
  8879. int y = script_getnum(st,4);
  8880. const char* str = script_getstr(st,5);
  8881. int class_ = script_getnum(st,6);
  8882. int amount = script_getnum(st,7);
  8883. const char* event = "";
  8884. unsigned int size = SZ_SMALL;
  8885. enum mob_ai ai = AI_NONE;
  8886. struct map_session_data* sd;
  8887. int16 m;
  8888. int i;
  8889. if (script_hasdata(st, 8)) {
  8890. event = script_getstr(st, 8);
  8891. check_event(st, event);
  8892. }
  8893. if (script_hasdata(st, 9)) {
  8894. size = script_getnum(st, 9);
  8895. if (size > SZ_BIG) {
  8896. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8897. return SCRIPT_CMD_FAILURE;
  8898. }
  8899. }
  8900. if (script_hasdata(st, 10)) {
  8901. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  8902. if (ai >= AI_MAX) {
  8903. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8904. return SCRIPT_CMD_FAILURE;
  8905. }
  8906. }
  8907. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8908. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8909. return SCRIPT_CMD_FAILURE;
  8910. }
  8911. sd = map_id2sd(st->rid);
  8912. if (sd && strcmp(mapn, "this") == 0)
  8913. m = sd->bl.m;
  8914. else
  8915. m = map_mapname2mapid(mapn);
  8916. for(i = 0; i < amount; i++) { //not optimised
  8917. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8918. if (mobid)
  8919. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8920. }
  8921. return SCRIPT_CMD_SUCCESS;
  8922. }
  8923. /*==========================================
  8924. * Request List of Monster Drops
  8925. *------------------------------------------*/
  8926. BUILDIN_FUNC(getmobdrops)
  8927. {
  8928. int class_ = script_getnum(st,2);
  8929. int i, j = 0;
  8930. struct mob_db *mob;
  8931. if( !mobdb_checkid(class_) )
  8932. {
  8933. script_pushint(st, 0);
  8934. return SCRIPT_CMD_SUCCESS;
  8935. }
  8936. mob = mob_db(class_);
  8937. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  8938. {
  8939. if( mob->dropitem[i].nameid < 1 )
  8940. continue;
  8941. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8942. continue;
  8943. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8944. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8945. j++;
  8946. }
  8947. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8948. script_pushint(st, 1);
  8949. return SCRIPT_CMD_SUCCESS;
  8950. }
  8951. /*==========================================
  8952. * Spawn a monster in a random location
  8953. * in x0,x1,y0,y1 area.
  8954. *------------------------------------------*/
  8955. BUILDIN_FUNC(areamonster)
  8956. {
  8957. const char* mapn = script_getstr(st,2);
  8958. int x0 = script_getnum(st,3);
  8959. int y0 = script_getnum(st,4);
  8960. int x1 = script_getnum(st,5);
  8961. int y1 = script_getnum(st,6);
  8962. const char* str = script_getstr(st,7);
  8963. int class_ = script_getnum(st,8);
  8964. int amount = script_getnum(st,9);
  8965. const char* event = "";
  8966. unsigned int size = SZ_SMALL;
  8967. enum mob_ai ai = AI_NONE;
  8968. struct map_session_data* sd;
  8969. int16 m;
  8970. int i;
  8971. if (script_hasdata(st,10)) {
  8972. event = script_getstr(st, 10);
  8973. check_event(st, event);
  8974. }
  8975. if (script_hasdata(st, 11)) {
  8976. size = script_getnum(st, 11);
  8977. if (size > 3) {
  8978. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8979. return SCRIPT_CMD_FAILURE;
  8980. }
  8981. }
  8982. if (script_hasdata(st, 12)) {
  8983. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  8984. if (ai >= AI_MAX) {
  8985. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8986. return SCRIPT_CMD_FAILURE;
  8987. }
  8988. }
  8989. sd = map_id2sd(st->rid);
  8990. if (sd && strcmp(mapn, "this") == 0)
  8991. m = sd->bl.m;
  8992. else
  8993. m = map_mapname2mapid(mapn);
  8994. for(i = 0; i < amount; i++) { //not optimised
  8995. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8996. if (mobid)
  8997. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8998. }
  8999. return SCRIPT_CMD_SUCCESS;
  9000. }
  9001. /*==========================================
  9002. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9003. *------------------------------------------*/
  9004. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9005. { //same fix but with killmonster instead - stripping events from mobs.
  9006. TBL_MOB* md = (TBL_MOB*)bl;
  9007. char *event=va_arg(ap,char *);
  9008. int allflag=va_arg(ap,int);
  9009. md->state.npc_killmonster = 1;
  9010. if(!allflag){
  9011. if(strcmp(event,md->npc_event)==0)
  9012. status_kill(bl);
  9013. }else{
  9014. if(!md->spawn)
  9015. status_kill(bl);
  9016. }
  9017. md->state.npc_killmonster = 0;
  9018. return SCRIPT_CMD_SUCCESS;
  9019. }
  9020. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9021. {
  9022. TBL_MOB* md = (TBL_MOB*)bl;
  9023. char *event=va_arg(ap,char *);
  9024. int allflag=va_arg(ap,int);
  9025. if(!allflag){
  9026. if(strcmp(event,md->npc_event)==0)
  9027. status_kill(bl);
  9028. }else{
  9029. if(!md->spawn)
  9030. status_kill(bl);
  9031. }
  9032. return SCRIPT_CMD_SUCCESS;
  9033. }
  9034. BUILDIN_FUNC(killmonster)
  9035. {
  9036. const char *mapname,*event;
  9037. int16 m,allflag=0;
  9038. mapname=script_getstr(st,2);
  9039. event=script_getstr(st,3);
  9040. if(strcmp(event,"All")==0)
  9041. allflag = 1;
  9042. else
  9043. check_event(st, event);
  9044. if( (m=map_mapname2mapid(mapname))<0 )
  9045. return SCRIPT_CMD_SUCCESS;
  9046. if( script_hasdata(st,4) ) {
  9047. if ( script_getnum(st,4) == 1 ) {
  9048. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9049. return SCRIPT_CMD_SUCCESS;
  9050. }
  9051. }
  9052. map_freeblock_lock();
  9053. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9054. map_freeblock_unlock();
  9055. return SCRIPT_CMD_SUCCESS;
  9056. }
  9057. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9058. { //Strips the event from the mob if it's killed the old method.
  9059. struct mob_data *md;
  9060. md = BL_CAST(BL_MOB, bl);
  9061. if (md->npc_event[0])
  9062. md->npc_event[0] = 0;
  9063. status_kill(bl);
  9064. return 0;
  9065. }
  9066. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9067. {
  9068. status_kill(bl);
  9069. return 0;
  9070. }
  9071. BUILDIN_FUNC(killmonsterall)
  9072. {
  9073. const char *mapname;
  9074. int16 m;
  9075. mapname=script_getstr(st,2);
  9076. if( (m = map_mapname2mapid(mapname))<0 )
  9077. return SCRIPT_CMD_SUCCESS;
  9078. if( script_hasdata(st,3) ) {
  9079. if ( script_getnum(st,3) == 1 ) {
  9080. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9081. return SCRIPT_CMD_SUCCESS;
  9082. }
  9083. }
  9084. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9085. return SCRIPT_CMD_SUCCESS;
  9086. }
  9087. /*==========================================
  9088. * Creates a clone of a player.
  9089. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9090. *------------------------------------------*/
  9091. BUILDIN_FUNC(clone)
  9092. {
  9093. TBL_PC *sd, *msd=NULL;
  9094. uint32 char_id, master_id = 0, x, y, flag = 0;
  9095. int16 m;
  9096. enum e_mode mode = MD_NONE;
  9097. unsigned int duration = 0;
  9098. const char *mapname,*event;
  9099. mapname=script_getstr(st,2);
  9100. x=script_getnum(st,3);
  9101. y=script_getnum(st,4);
  9102. event=script_getstr(st,5);
  9103. char_id=script_getnum(st,6);
  9104. if( script_hasdata(st,7) )
  9105. master_id=script_getnum(st,7);
  9106. if( script_hasdata(st,8) )
  9107. mode=static_cast<e_mode>(script_getnum(st,8));
  9108. if( script_hasdata(st,9) )
  9109. flag=script_getnum(st,9);
  9110. if( script_hasdata(st,10) )
  9111. duration=script_getnum(st,10);
  9112. check_event(st, event);
  9113. m = map_mapname2mapid(mapname);
  9114. if (m < 0)
  9115. return SCRIPT_CMD_SUCCESS;
  9116. sd = map_charid2sd(char_id);
  9117. if (master_id) {
  9118. msd = map_charid2sd(master_id);
  9119. if (msd)
  9120. master_id = msd->bl.id;
  9121. else
  9122. master_id = 0;
  9123. }
  9124. if (sd) //Return ID of newly crafted clone.
  9125. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9126. else //Failed to create clone.
  9127. script_pushint(st,0);
  9128. return SCRIPT_CMD_SUCCESS;
  9129. }
  9130. /*==========================================
  9131. *------------------------------------------*/
  9132. BUILDIN_FUNC(doevent)
  9133. {
  9134. const char* event;
  9135. struct map_session_data* sd;
  9136. if( !script_rid2sd(sd) )
  9137. return SCRIPT_CMD_SUCCESS;
  9138. event = script_getstr(st,2);
  9139. check_event(st, event);
  9140. npc_event(sd, event, 0);
  9141. return SCRIPT_CMD_SUCCESS;
  9142. }
  9143. /*==========================================
  9144. *------------------------------------------*/
  9145. BUILDIN_FUNC(donpcevent)
  9146. {
  9147. const char* event = script_getstr(st,2);
  9148. check_event(st, event);
  9149. if( !npc_event_do(event) ) {
  9150. struct npc_data * nd = map_id2nd(st->oid);
  9151. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9152. script_pushint(st, 0);
  9153. } else
  9154. script_pushint(st, 1);
  9155. return SCRIPT_CMD_SUCCESS;
  9156. }
  9157. /// for Aegis compatibility
  9158. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9159. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9160. {
  9161. const char* npc = script_getstr(st,2);
  9162. const char* command = script_getstr(st,3);
  9163. char event[EVENT_NAME_LENGTH];
  9164. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9165. check_event(st, event);
  9166. if( npc_event_do(event) ){
  9167. script_pushint(st, true);
  9168. }else{
  9169. struct npc_data * nd = map_id2nd(st->oid);
  9170. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9171. script_pushint(st, false);
  9172. }
  9173. return SCRIPT_CMD_SUCCESS;
  9174. }
  9175. /*==========================================
  9176. *------------------------------------------*/
  9177. BUILDIN_FUNC(addtimer)
  9178. {
  9179. int tick;
  9180. const char* event;
  9181. TBL_PC* sd;
  9182. if( !script_rid2sd(sd) )
  9183. return SCRIPT_CMD_SUCCESS;
  9184. tick = script_getnum(st,2);
  9185. event = script_getstr(st, 3);
  9186. check_event(st, event);
  9187. if (!pc_addeventtimer(sd,tick,event)) {
  9188. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9189. return SCRIPT_CMD_FAILURE;
  9190. }
  9191. return SCRIPT_CMD_SUCCESS;
  9192. }
  9193. /*==========================================
  9194. *------------------------------------------*/
  9195. BUILDIN_FUNC(deltimer)
  9196. {
  9197. const char *event;
  9198. TBL_PC* sd;
  9199. if( !script_rid2sd(sd) )
  9200. return SCRIPT_CMD_SUCCESS;
  9201. event=script_getstr(st, 2);
  9202. check_event(st, event);
  9203. pc_deleventtimer(sd,event);
  9204. return SCRIPT_CMD_SUCCESS;
  9205. }
  9206. /*==========================================
  9207. *------------------------------------------*/
  9208. BUILDIN_FUNC(addtimercount)
  9209. {
  9210. const char *event;
  9211. int tick;
  9212. TBL_PC* sd;
  9213. if( !script_rid2sd(sd) )
  9214. return SCRIPT_CMD_SUCCESS;
  9215. tick=script_getnum(st,2);
  9216. event=script_getstr(st,3);
  9217. check_event(st, event);
  9218. pc_addeventtimercount(sd,event,tick);
  9219. return SCRIPT_CMD_SUCCESS;
  9220. }
  9221. /*==========================================
  9222. *------------------------------------------*/
  9223. BUILDIN_FUNC(initnpctimer)
  9224. {
  9225. struct npc_data *nd;
  9226. int flag = 0;
  9227. if( script_hasdata(st,3) )
  9228. { //Two arguments: NPC name and attach flag.
  9229. nd = npc_name2id(script_getstr(st, 2));
  9230. flag = script_getnum(st,3);
  9231. }
  9232. else if( script_hasdata(st,2) )
  9233. { //Check if argument is numeric (flag) or string (npc name)
  9234. struct script_data *data;
  9235. data = script_getdata(st,2);
  9236. get_val(st,data);
  9237. if( data_isstring(data) ) //NPC name
  9238. nd = npc_name2id(conv_str(st, data));
  9239. else if( data_isint(data) ) //Flag
  9240. {
  9241. nd = (struct npc_data *)map_id2bl(st->oid);
  9242. flag = conv_num(st,data);
  9243. }
  9244. else
  9245. {
  9246. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9247. return SCRIPT_CMD_FAILURE;
  9248. }
  9249. }
  9250. else
  9251. nd = (struct npc_data *)map_id2bl(st->oid);
  9252. if( !nd )
  9253. return SCRIPT_CMD_SUCCESS;
  9254. if( flag ) //Attach
  9255. {
  9256. TBL_PC* sd;
  9257. if( !script_rid2sd(sd) )
  9258. return SCRIPT_CMD_SUCCESS;
  9259. nd->u.scr.rid = sd->bl.id;
  9260. }
  9261. nd->u.scr.timertick = 0;
  9262. npc_settimerevent_tick(nd,0);
  9263. npc_timerevent_start(nd, st->rid);
  9264. return SCRIPT_CMD_SUCCESS;
  9265. }
  9266. /*==========================================
  9267. *------------------------------------------*/
  9268. BUILDIN_FUNC(startnpctimer)
  9269. {
  9270. struct npc_data *nd;
  9271. int flag = 0;
  9272. if( script_hasdata(st,3) )
  9273. { //Two arguments: NPC name and attach flag.
  9274. nd = npc_name2id(script_getstr(st, 2));
  9275. flag = script_getnum(st,3);
  9276. }
  9277. else if( script_hasdata(st,2) )
  9278. { //Check if argument is numeric (flag) or string (npc name)
  9279. struct script_data *data;
  9280. data = script_getdata(st,2);
  9281. get_val(st,data);
  9282. if( data_isstring(data) ) //NPC name
  9283. nd = npc_name2id(conv_str(st, data));
  9284. else if( data_isint(data) ) //Flag
  9285. {
  9286. nd = (struct npc_data *)map_id2bl(st->oid);
  9287. flag = conv_num(st,data);
  9288. }
  9289. else
  9290. {
  9291. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9292. return SCRIPT_CMD_FAILURE;
  9293. }
  9294. }
  9295. else
  9296. nd=(struct npc_data *)map_id2bl(st->oid);
  9297. if( !nd )
  9298. return SCRIPT_CMD_SUCCESS;
  9299. if( flag ) //Attach
  9300. {
  9301. TBL_PC* sd;
  9302. if( !script_rid2sd(sd) )
  9303. return SCRIPT_CMD_SUCCESS;
  9304. nd->u.scr.rid = sd->bl.id;
  9305. }
  9306. npc_timerevent_start(nd, st->rid);
  9307. return SCRIPT_CMD_SUCCESS;
  9308. }
  9309. /*==========================================
  9310. *------------------------------------------*/
  9311. BUILDIN_FUNC(stopnpctimer)
  9312. {
  9313. struct npc_data *nd;
  9314. int flag = 0;
  9315. if( script_hasdata(st,3) )
  9316. { //Two arguments: NPC name and attach flag.
  9317. nd = npc_name2id(script_getstr(st, 2));
  9318. flag = script_getnum(st,3);
  9319. }
  9320. else if( script_hasdata(st,2) )
  9321. { //Check if argument is numeric (flag) or string (npc name)
  9322. struct script_data *data;
  9323. data = script_getdata(st,2);
  9324. get_val(st,data);
  9325. if( data_isstring(data) ) //NPC name
  9326. nd = npc_name2id(conv_str(st, data));
  9327. else if( data_isint(data) ) //Flag
  9328. {
  9329. nd = (struct npc_data *)map_id2bl(st->oid);
  9330. flag = conv_num(st,data);
  9331. }
  9332. else
  9333. {
  9334. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9335. return SCRIPT_CMD_FAILURE;
  9336. }
  9337. }
  9338. else
  9339. nd=(struct npc_data *)map_id2bl(st->oid);
  9340. if( !nd )
  9341. return SCRIPT_CMD_SUCCESS;
  9342. if( flag ) //Detach
  9343. nd->u.scr.rid = 0;
  9344. npc_timerevent_stop(nd);
  9345. return SCRIPT_CMD_SUCCESS;
  9346. }
  9347. /*==========================================
  9348. *------------------------------------------*/
  9349. BUILDIN_FUNC(getnpctimer)
  9350. {
  9351. struct npc_data *nd;
  9352. TBL_PC *sd;
  9353. int type = script_getnum(st,2);
  9354. int val = 0;
  9355. if( script_hasdata(st,3) )
  9356. nd = npc_name2id(script_getstr(st,3));
  9357. else
  9358. nd = (struct npc_data *)map_id2bl(st->oid);
  9359. if( !nd || nd->bl.type != BL_NPC )
  9360. {
  9361. script_pushint(st,0);
  9362. ShowError("getnpctimer: Invalid NPC.\n");
  9363. return SCRIPT_CMD_FAILURE;
  9364. }
  9365. switch( type )
  9366. {
  9367. case 0: val = npc_gettimerevent_tick(nd); break;
  9368. case 1:
  9369. if( nd->u.scr.rid )
  9370. {
  9371. sd = map_id2sd(nd->u.scr.rid);
  9372. if( !sd )
  9373. {
  9374. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9375. break;
  9376. }
  9377. val = (sd->npc_timer_id != INVALID_TIMER);
  9378. }
  9379. else
  9380. val = (nd->u.scr.timerid != INVALID_TIMER);
  9381. break;
  9382. case 2: val = nd->u.scr.timeramount; break;
  9383. }
  9384. script_pushint(st,val);
  9385. return SCRIPT_CMD_SUCCESS;
  9386. }
  9387. /*==========================================
  9388. *------------------------------------------*/
  9389. BUILDIN_FUNC(setnpctimer)
  9390. {
  9391. int tick;
  9392. struct npc_data *nd;
  9393. tick = script_getnum(st,2);
  9394. if( script_hasdata(st,3) )
  9395. nd = npc_name2id(script_getstr(st,3));
  9396. else
  9397. nd = (struct npc_data *)map_id2bl(st->oid);
  9398. if( !nd || nd->bl.type != BL_NPC )
  9399. {
  9400. script_pushint(st,1);
  9401. ShowError("setnpctimer: Invalid NPC.\n");
  9402. return SCRIPT_CMD_FAILURE;
  9403. }
  9404. npc_settimerevent_tick(nd,tick);
  9405. script_pushint(st,0);
  9406. return SCRIPT_CMD_SUCCESS;
  9407. }
  9408. /*==========================================
  9409. * attaches the player rid to the timer [Celest]
  9410. *------------------------------------------*/
  9411. BUILDIN_FUNC(attachnpctimer)
  9412. {
  9413. TBL_PC *sd;
  9414. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9415. if( !nd || nd->bl.type != BL_NPC )
  9416. {
  9417. script_pushint(st,1);
  9418. ShowError("setnpctimer: Invalid NPC.\n");
  9419. return SCRIPT_CMD_FAILURE;
  9420. }
  9421. if( !script_nick2sd(2,sd) ){
  9422. script_pushint(st,1);
  9423. ShowWarning("attachnpctimer: Invalid player.\n");
  9424. return SCRIPT_CMD_FAILURE;
  9425. }
  9426. nd->u.scr.rid = sd->bl.id;
  9427. script_pushint(st,0);
  9428. return SCRIPT_CMD_SUCCESS;
  9429. }
  9430. /*==========================================
  9431. * detaches a player rid from the timer [Celest]
  9432. *------------------------------------------*/
  9433. BUILDIN_FUNC(detachnpctimer)
  9434. {
  9435. struct npc_data *nd;
  9436. if( script_hasdata(st,2) )
  9437. nd = npc_name2id(script_getstr(st,2));
  9438. else
  9439. nd = (struct npc_data *)map_id2bl(st->oid);
  9440. if( !nd || nd->bl.type != BL_NPC )
  9441. {
  9442. script_pushint(st,1);
  9443. ShowError("detachnpctimer: Invalid NPC.\n");
  9444. return SCRIPT_CMD_FAILURE;
  9445. }
  9446. nd->u.scr.rid = 0;
  9447. script_pushint(st,0);
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. /*==========================================
  9451. * To avoid "player not attached" script errors, this function is provided,
  9452. * it checks if there is a player attached to the current script. [Skotlex]
  9453. * If no, returns 0, if yes, returns the account_id of the attached player.
  9454. *------------------------------------------*/
  9455. BUILDIN_FUNC(playerattached)
  9456. {
  9457. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9458. script_pushint(st,0);
  9459. else
  9460. script_pushint(st,st->rid);
  9461. return SCRIPT_CMD_SUCCESS;
  9462. }
  9463. /*==========================================
  9464. *------------------------------------------*/
  9465. BUILDIN_FUNC(announce)
  9466. {
  9467. const char *mes = script_getstr(st,2);
  9468. int flag = script_getnum(st,3);
  9469. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9470. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9471. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9472. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9473. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9474. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9475. {
  9476. send_target target;
  9477. struct block_list *bl;
  9478. // If bc_npc flag is set, use NPC as broadcast source
  9479. if(flag&BC_NPC){
  9480. bl = map_id2bl(st->oid);
  9481. }else{
  9482. struct map_session_data* sd;
  9483. if( script_rid2sd(sd) )
  9484. bl = &sd->bl;
  9485. else
  9486. bl = NULL;
  9487. }
  9488. if (bl == NULL)
  9489. return SCRIPT_CMD_SUCCESS;
  9490. switch (flag&BC_TARGET_MASK) {
  9491. case BC_MAP: target = ALL_SAMEMAP; break;
  9492. case BC_AREA: target = AREA; break;
  9493. case BC_SELF: target = SELF; break;
  9494. default: target = ALL_CLIENT; break; // BC_ALL
  9495. }
  9496. if (fontColor)
  9497. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9498. else
  9499. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9500. }
  9501. else
  9502. {
  9503. if (fontColor)
  9504. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9505. else
  9506. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9507. }
  9508. return SCRIPT_CMD_SUCCESS;
  9509. }
  9510. /*==========================================
  9511. *------------------------------------------*/
  9512. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9513. {
  9514. char *mes = va_arg(ap, char *);
  9515. int len = va_arg(ap, int);
  9516. int type = va_arg(ap, int);
  9517. char *fontColor = va_arg(ap, char *);
  9518. short fontType = (short)va_arg(ap, int);
  9519. short fontSize = (short)va_arg(ap, int);
  9520. short fontAlign = (short)va_arg(ap, int);
  9521. short fontY = (short)va_arg(ap, int);
  9522. if (fontColor)
  9523. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9524. else
  9525. clif_broadcast(bl, mes, len, type, SELF);
  9526. return SCRIPT_CMD_SUCCESS;
  9527. }
  9528. BUILDIN_FUNC(mapannounce)
  9529. {
  9530. const char *mapname = script_getstr(st,2);
  9531. const char *mes = script_getstr(st,3);
  9532. int flag = script_getnum(st,4);
  9533. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9534. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9535. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9536. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9537. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9538. int16 m;
  9539. if ((m = map_mapname2mapid(mapname)) < 0)
  9540. return SCRIPT_CMD_SUCCESS;
  9541. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9542. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9543. return SCRIPT_CMD_SUCCESS;
  9544. }
  9545. /*==========================================
  9546. *------------------------------------------*/
  9547. BUILDIN_FUNC(areaannounce)
  9548. {
  9549. const char *mapname = script_getstr(st,2);
  9550. int x0 = script_getnum(st,3);
  9551. int y0 = script_getnum(st,4);
  9552. int x1 = script_getnum(st,5);
  9553. int y1 = script_getnum(st,6);
  9554. const char *mes = script_getstr(st,7);
  9555. int flag = script_getnum(st,8);
  9556. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9557. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9558. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9559. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9560. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9561. int16 m;
  9562. if ((m = map_mapname2mapid(mapname)) < 0)
  9563. return SCRIPT_CMD_SUCCESS;
  9564. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9565. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9566. return SCRIPT_CMD_SUCCESS;
  9567. }
  9568. /*==========================================
  9569. *------------------------------------------*/
  9570. BUILDIN_FUNC(getusers)
  9571. {
  9572. int flag, val = 0;
  9573. struct map_session_data* sd;
  9574. struct block_list* bl = NULL;
  9575. flag = script_getnum(st,2);
  9576. switch(flag&0x07)
  9577. {
  9578. case 0:
  9579. if(flag&0x8)
  9580. {// npc
  9581. bl = map_id2bl(st->oid);
  9582. }
  9583. else if(script_rid2sd(sd))
  9584. {// pc
  9585. bl = &sd->bl;
  9586. }
  9587. if(bl)
  9588. {
  9589. val = map[bl->m].users;
  9590. }
  9591. break;
  9592. case 1:
  9593. val = map_getusers();
  9594. break;
  9595. default:
  9596. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9597. script_pushint(st,0);
  9598. return SCRIPT_CMD_FAILURE;
  9599. }
  9600. script_pushint(st,val);
  9601. return SCRIPT_CMD_SUCCESS;
  9602. }
  9603. /*==========================================
  9604. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9605. *------------------------------------------*/
  9606. BUILDIN_FUNC(getmapguildusers)
  9607. {
  9608. const char *str;
  9609. int16 m;
  9610. int gid;
  9611. int c=0;
  9612. struct guild *g = NULL;
  9613. str=script_getstr(st,2);
  9614. gid=script_getnum(st,3);
  9615. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9616. script_pushint(st,-1);
  9617. return SCRIPT_CMD_SUCCESS;
  9618. }
  9619. g = guild_search(gid);
  9620. if (g){
  9621. unsigned short i;
  9622. for(i = 0; i < g->max_member; i++)
  9623. {
  9624. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9625. c++;
  9626. }
  9627. }
  9628. script_pushint(st,c);
  9629. return SCRIPT_CMD_SUCCESS;
  9630. }
  9631. /*==========================================
  9632. *------------------------------------------*/
  9633. BUILDIN_FUNC(getmapusers)
  9634. {
  9635. const char *str;
  9636. int16 m;
  9637. str=script_getstr(st,2);
  9638. if( (m=map_mapname2mapid(str))< 0){
  9639. script_pushint(st,-1);
  9640. return SCRIPT_CMD_SUCCESS;
  9641. }
  9642. script_pushint(st,map[m].users);
  9643. return SCRIPT_CMD_SUCCESS;
  9644. }
  9645. /*==========================================
  9646. *------------------------------------------*/
  9647. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9648. {
  9649. int *users=va_arg(ap,int *);
  9650. (*users)++;
  9651. return SCRIPT_CMD_SUCCESS;
  9652. }
  9653. BUILDIN_FUNC(getareausers)
  9654. {
  9655. const char *str;
  9656. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9657. str=script_getstr(st,2);
  9658. x0=script_getnum(st,3);
  9659. y0=script_getnum(st,4);
  9660. x1=script_getnum(st,5);
  9661. y1=script_getnum(st,6);
  9662. if( (m=map_mapname2mapid(str))< 0){
  9663. script_pushint(st,-1);
  9664. return SCRIPT_CMD_SUCCESS;
  9665. }
  9666. map_foreachinallarea(buildin_getareausers_sub,
  9667. m,x0,y0,x1,y1,BL_PC,&users);
  9668. script_pushint(st,users);
  9669. return SCRIPT_CMD_SUCCESS;
  9670. }
  9671. /*==========================================
  9672. *------------------------------------------*/
  9673. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9674. {
  9675. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9676. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9677. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9678. if(drop->item.nameid==nameid)
  9679. (*amount)+=drop->item.amount;
  9680. return SCRIPT_CMD_SUCCESS;
  9681. }
  9682. BUILDIN_FUNC(getareadropitem)
  9683. {
  9684. const char *str;
  9685. int16 m,x0,y0,x1,y1;
  9686. unsigned short nameid, amount = 0;
  9687. struct script_data *data;
  9688. str=script_getstr(st,2);
  9689. x0=script_getnum(st,3);
  9690. y0=script_getnum(st,4);
  9691. x1=script_getnum(st,5);
  9692. y1=script_getnum(st,6);
  9693. data=script_getdata(st,7);
  9694. get_val(st,data);
  9695. if( data_isstring(data) ){
  9696. const char *name=conv_str(st,data);
  9697. struct item_data *item_data = itemdb_searchname(name);
  9698. nameid=UNKNOWN_ITEM_ID;
  9699. if( item_data )
  9700. nameid=item_data->nameid;
  9701. }else
  9702. nameid=conv_num(st,data);
  9703. if( (m=map_mapname2mapid(str))< 0){
  9704. script_pushint(st,-1);
  9705. return SCRIPT_CMD_SUCCESS;
  9706. }
  9707. map_foreachinallarea(buildin_getareadropitem_sub,
  9708. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9709. script_pushint(st,amount);
  9710. return SCRIPT_CMD_SUCCESS;
  9711. }
  9712. /*==========================================
  9713. *------------------------------------------*/
  9714. BUILDIN_FUNC(enablenpc)
  9715. {
  9716. const char *str;
  9717. str=script_getstr(st,2);
  9718. npc_enable(str,1);
  9719. return SCRIPT_CMD_SUCCESS;
  9720. }
  9721. /*==========================================
  9722. *------------------------------------------*/
  9723. BUILDIN_FUNC(disablenpc)
  9724. {
  9725. const char *str;
  9726. str=script_getstr(st,2);
  9727. npc_enable(str,0);
  9728. return SCRIPT_CMD_SUCCESS;
  9729. }
  9730. /*==========================================
  9731. *------------------------------------------*/
  9732. BUILDIN_FUNC(hideoffnpc)
  9733. {
  9734. const char *str;
  9735. str=script_getstr(st,2);
  9736. npc_enable(str,2);
  9737. return SCRIPT_CMD_SUCCESS;
  9738. }
  9739. /*==========================================
  9740. *------------------------------------------*/
  9741. BUILDIN_FUNC(hideonnpc)
  9742. {
  9743. const char *str;
  9744. str=script_getstr(st,2);
  9745. npc_enable(str,4);
  9746. return SCRIPT_CMD_SUCCESS;
  9747. }
  9748. /* Starts a status effect on the target unit or on the attached player.
  9749. *
  9750. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9751. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9752. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9753. * <flag>: enum e_status_change_start_flags
  9754. */
  9755. BUILDIN_FUNC(sc_start)
  9756. {
  9757. TBL_NPC * nd = map_id2nd(st->oid);
  9758. struct block_list* bl;
  9759. enum sc_type type;
  9760. int tick, val1, val2, val3, val4=0, rate, flag;
  9761. char start_type;
  9762. const char* command = script_getfuncname(st);
  9763. if(strstr(command, "4"))
  9764. start_type = 4;
  9765. else if(strstr(command, "2"))
  9766. start_type = 2;
  9767. else
  9768. start_type = 1;
  9769. type = (sc_type)script_getnum(st,2);
  9770. tick = script_getnum(st,3);
  9771. val1 = script_getnum(st,4);
  9772. //If from NPC we make default flag 1 to be unavoidable
  9773. if(nd && nd->bl.id == fake_nd->bl.id)
  9774. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9775. else
  9776. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9777. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9778. if(script_hasdata(st,(6+start_type)))
  9779. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9780. else
  9781. bl = map_id2bl(st->rid);
  9782. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9783. {// When there isn't a duration specified, try to get it from the skill_db
  9784. tick = skill_get_time(status_sc2skill(type), val1);
  9785. }
  9786. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9787. bl = map_id2bl(potion_target);
  9788. tick /= 2;// Thrown potions only last half.
  9789. val4 = 1;// Mark that this was a thrown sc_effect
  9790. }
  9791. if(!bl)
  9792. return SCRIPT_CMD_SUCCESS;
  9793. switch(start_type) {
  9794. case 1:
  9795. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9796. break;
  9797. case 2:
  9798. val2 = script_getnum(st,5);
  9799. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9800. break;
  9801. case 4:
  9802. val2 = script_getnum(st,5);
  9803. val3 = script_getnum(st,6);
  9804. val4 = script_getnum(st,7);
  9805. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9806. break;
  9807. }
  9808. return SCRIPT_CMD_SUCCESS;
  9809. }
  9810. /// Ends one or all status effects on the target unit or on the attached player.
  9811. ///
  9812. /// sc_end <effect_id>{,<unit_id>};
  9813. BUILDIN_FUNC(sc_end)
  9814. {
  9815. struct block_list* bl;
  9816. int type;
  9817. type = script_getnum(st, 2);
  9818. if (script_hasdata(st, 3))
  9819. bl = map_id2bl(script_getnum(st, 3));
  9820. else
  9821. bl = map_id2bl(st->rid);
  9822. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9823. bl = map_id2bl(potion_target);
  9824. if (!bl)
  9825. return SCRIPT_CMD_SUCCESS;
  9826. if (type >= 0 && type < SC_MAX) {
  9827. struct status_change *sc = status_get_sc(bl);
  9828. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9829. if (!sce)
  9830. return SCRIPT_CMD_SUCCESS;
  9831. switch (type) {
  9832. case SC_WEIGHT50:
  9833. case SC_WEIGHT90:
  9834. case SC_NOCHAT:
  9835. case SC_PUSH_CART:
  9836. case SC_ALL_RIDING:
  9837. case SC_STYLE_CHANGE:
  9838. case SC_MONSTER_TRANSFORM:
  9839. case SC_ACTIVE_MONSTER_TRANSFORM:
  9840. case SC_MTF_ASPD:
  9841. case SC_MTF_RANGEATK:
  9842. case SC_MTF_MATK:
  9843. case SC_MTF_MLEATKED:
  9844. case SC_MTF_CRIDAMAGE:
  9845. case SC_MTF_ASPD2:
  9846. case SC_MTF_RANGEATK2:
  9847. case SC_MTF_MATK2:
  9848. case SC_MTF_MHP:
  9849. case SC_MTF_MSP:
  9850. case SC_MTF_PUMPKIN:
  9851. case SC_MTF_HITFLEE:
  9852. case SC_ATTHASTE_CASH:
  9853. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9854. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9855. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9856. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9857. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9858. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9859. return SCRIPT_CMD_SUCCESS;
  9860. default:
  9861. break;
  9862. }
  9863. //This should help status_change_end force disabling the SC in case it has no limit.
  9864. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9865. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9866. } else
  9867. status_change_clear(bl, 3); // remove all effects
  9868. return SCRIPT_CMD_SUCCESS;
  9869. }
  9870. /**
  9871. * Ends all status effects from any learned skill on the attached player.
  9872. * if <job_id> was given it will end the effect of that class for the attached player
  9873. * sc_end_class {<char_id>{,<job_id>}};
  9874. */
  9875. BUILDIN_FUNC(sc_end_class)
  9876. {
  9877. struct map_session_data *sd;
  9878. uint16 skill_id;
  9879. int class_;
  9880. if (!script_charid2sd(2, sd))
  9881. return SCRIPT_CMD_FAILURE;
  9882. if (script_hasdata(st, 3))
  9883. class_ = script_getnum(st, 3);
  9884. else
  9885. class_ = sd->status.class_;
  9886. if (!pcdb_checkid(class_)) {
  9887. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  9888. return SCRIPT_CMD_FAILURE;
  9889. }
  9890. for (int i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[pc_class2idx(class_)][i].skill_id) > 0; i++) {
  9891. enum sc_type sc = status_skill2sc(skill_id);
  9892. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  9893. status_change_end(&sd->bl, sc, INVALID_TIMER);
  9894. }
  9895. return SCRIPT_CMD_SUCCESS;
  9896. }
  9897. /*==========================================
  9898. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9899. *------------------------------------------*/
  9900. BUILDIN_FUNC(getscrate)
  9901. {
  9902. struct block_list *bl;
  9903. int type,rate;
  9904. type=script_getnum(st,2);
  9905. rate=script_getnum(st,3);
  9906. if( script_hasdata(st,4) ) //get for the bl assigned
  9907. bl = map_id2bl(script_getnum(st,4));
  9908. else
  9909. bl = map_id2bl(st->rid);
  9910. if (bl)
  9911. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9912. script_pushint(st,rate);
  9913. return SCRIPT_CMD_SUCCESS;
  9914. }
  9915. /**
  9916. * getstatus(<effect type>{,<type>{,<char_id>}});
  9917. **/
  9918. BUILDIN_FUNC(getstatus)
  9919. {
  9920. int id, type;
  9921. struct map_session_data* sd;
  9922. if (!script_charid2sd(4,sd))
  9923. return SCRIPT_CMD_FAILURE;
  9924. id = script_getnum(st, 2);
  9925. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9926. if( id <= SC_NONE || id >= SC_MAX )
  9927. {// invalid status type given
  9928. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  9929. return SCRIPT_CMD_SUCCESS;
  9930. }
  9931. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9932. {// no status is active
  9933. script_pushint(st, 0);
  9934. return SCRIPT_CMD_SUCCESS;
  9935. }
  9936. switch( type )
  9937. {
  9938. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9939. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9940. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9941. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9942. case 5:
  9943. {
  9944. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9945. if( timer )
  9946. {// return the amount of time remaining
  9947. script_pushint(st, timer->tick - gettick());
  9948. } else {
  9949. script_pushint(st, -1);
  9950. }
  9951. }
  9952. break;
  9953. default: script_pushint(st, 1); break;
  9954. }
  9955. return SCRIPT_CMD_SUCCESS;
  9956. }
  9957. /*==========================================
  9958. *
  9959. *------------------------------------------*/
  9960. BUILDIN_FUNC(debugmes)
  9961. {
  9962. const char *str;
  9963. str=script_getstr(st,2);
  9964. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9965. return SCRIPT_CMD_SUCCESS;
  9966. }
  9967. /*==========================================
  9968. *------------------------------------------*/
  9969. BUILDIN_FUNC(catchpet)
  9970. {
  9971. int pet_id;
  9972. TBL_PC *sd;
  9973. if( !script_rid2sd(sd) )
  9974. return SCRIPT_CMD_SUCCESS;
  9975. pet_id= script_getnum(st,2);
  9976. pet_catch_process1(sd,pet_id);
  9977. return SCRIPT_CMD_SUCCESS;
  9978. }
  9979. /*==========================================
  9980. * [orn]
  9981. *------------------------------------------*/
  9982. BUILDIN_FUNC(homunculus_evolution)
  9983. {
  9984. TBL_PC *sd;
  9985. if( !script_rid2sd(sd) )
  9986. return SCRIPT_CMD_SUCCESS;
  9987. if(hom_is_active(sd->hd))
  9988. {
  9989. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9990. hom_evolution(sd->hd);
  9991. else
  9992. clif_emotion(&sd->hd->bl, ET_SWEAT);
  9993. }
  9994. return SCRIPT_CMD_SUCCESS;
  9995. }
  9996. /*==========================================
  9997. * Checks for vaporized morph state
  9998. * and deletes ITEMID_STRANGE_EMBRYO.
  9999. *------------------------------------------*/
  10000. BUILDIN_FUNC(homunculus_mutate)
  10001. {
  10002. int homun_id;
  10003. TBL_PC *sd;
  10004. if( !script_rid2sd(sd) || sd->hd == NULL )
  10005. return SCRIPT_CMD_SUCCESS;
  10006. if(script_hasdata(st,2))
  10007. homun_id = script_getnum(st,2);
  10008. else
  10009. homun_id = 6048 + (rnd() % 4);
  10010. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10011. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10012. int m_id = hom_class2mapid(homun_id);
  10013. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10014. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10015. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10016. hom_call(sd); // Respawn homunculus.
  10017. hom_mutate(sd->hd, homun_id);
  10018. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10019. script_pushint(st, 1);
  10020. return SCRIPT_CMD_SUCCESS;
  10021. } else
  10022. clif_emotion(&sd->bl, ET_SWEAT);
  10023. } else
  10024. clif_emotion(&sd->bl, ET_SWEAT);
  10025. script_pushint(st, 0);
  10026. return SCRIPT_CMD_SUCCESS;
  10027. }
  10028. /*==========================================
  10029. * Puts homunculus into morph state
  10030. * and gives ITEMID_STRANGE_EMBRYO.
  10031. *------------------------------------------*/
  10032. BUILDIN_FUNC(morphembryo)
  10033. {
  10034. struct item item_tmp;
  10035. TBL_PC *sd;
  10036. if( !script_rid2sd(sd) || sd->hd == NULL )
  10037. return SCRIPT_CMD_SUCCESS;
  10038. if( hom_is_active(sd->hd) ) {
  10039. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10040. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10041. char i;
  10042. memset(&item_tmp, 0, sizeof(item_tmp));
  10043. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10044. item_tmp.identify = 1;
  10045. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10046. clif_additem(sd, 0, 0, i);
  10047. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10048. } else {
  10049. hom_vaporize(sd, HOM_ST_MORPH);
  10050. script_pushint(st, 1);
  10051. return SCRIPT_CMD_SUCCESS;
  10052. }
  10053. } else
  10054. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10055. } else
  10056. clif_emotion(&sd->bl, ET_SWEAT);
  10057. script_pushint(st, 0);
  10058. return SCRIPT_CMD_SUCCESS;
  10059. }
  10060. // [Zephyrus]
  10061. BUILDIN_FUNC(homunculus_shuffle)
  10062. {
  10063. TBL_PC *sd;
  10064. if( !script_rid2sd(sd) )
  10065. return SCRIPT_CMD_SUCCESS;
  10066. if(hom_is_active(sd->hd))
  10067. hom_shuffle(sd->hd);
  10068. return SCRIPT_CMD_SUCCESS;
  10069. }
  10070. /*==========================================
  10071. * Check for homunculus state.
  10072. * Return: -1 = No homunculus
  10073. * 0 = Homunculus is active
  10074. * 1 = Homunculus is vaporized (rest)
  10075. * 2 = Homunculus is in morph state
  10076. *------------------------------------------*/
  10077. BUILDIN_FUNC(checkhomcall)
  10078. {
  10079. TBL_PC *sd;
  10080. TBL_HOM *hd;
  10081. if( !script_rid2sd(sd) )
  10082. return SCRIPT_CMD_SUCCESS;
  10083. hd = sd->hd;
  10084. if( !hd )
  10085. script_pushint(st, -1);
  10086. else
  10087. script_pushint(st, hd->homunculus.vaporize);
  10088. return SCRIPT_CMD_SUCCESS;
  10089. }
  10090. //These two functions bring the eA MAPID_* class functionality to scripts.
  10091. BUILDIN_FUNC(eaclass)
  10092. {
  10093. int class_;
  10094. if( script_hasdata(st,2) )
  10095. class_ = script_getnum(st,2);
  10096. else {
  10097. TBL_PC *sd;
  10098. if (!script_charid2sd(3,sd)) {
  10099. script_pushint(st,-1);
  10100. return SCRIPT_CMD_SUCCESS;
  10101. }
  10102. class_ = sd->status.class_;
  10103. }
  10104. script_pushint(st,pc_jobid2mapid(class_));
  10105. return SCRIPT_CMD_SUCCESS;
  10106. }
  10107. BUILDIN_FUNC(roclass)
  10108. {
  10109. int class_ =script_getnum(st,2);
  10110. int sex;
  10111. if( script_hasdata(st,3) )
  10112. sex = script_getnum(st,3);
  10113. else {
  10114. TBL_PC *sd;
  10115. if (st->rid && script_rid2sd(sd))
  10116. sex = sd->status.sex;
  10117. else
  10118. sex = SEX_MALE; //Just use male when not found.
  10119. }
  10120. script_pushint(st,pc_mapid2jobid(class_, sex));
  10121. return SCRIPT_CMD_SUCCESS;
  10122. }
  10123. /*==========================================
  10124. * Tells client to open a hatching window, used for pet incubator
  10125. *------------------------------------------*/
  10126. BUILDIN_FUNC(birthpet)
  10127. {
  10128. TBL_PC *sd;
  10129. if( !script_rid2sd(sd) )
  10130. return SCRIPT_CMD_SUCCESS;
  10131. if( sd->status.pet_id )
  10132. {// do not send egg list, when you already have a pet
  10133. return SCRIPT_CMD_SUCCESS;
  10134. }
  10135. clif_sendegg(sd);
  10136. return SCRIPT_CMD_SUCCESS;
  10137. }
  10138. /**
  10139. * resetlvl <action type>{,<char_id>};
  10140. * @param action_type:
  10141. * 1 : make like after rebirth
  10142. * 2 : blvl,jlvl=1, skillpoint=0
  10143. * 3 : don't reset skill, blvl=1
  10144. * 4 : jlvl=0
  10145. * @author AppleGirl
  10146. **/
  10147. BUILDIN_FUNC(resetlvl)
  10148. {
  10149. TBL_PC *sd;
  10150. int type=script_getnum(st,2);
  10151. if (!script_charid2sd(3,sd))
  10152. return SCRIPT_CMD_FAILURE;
  10153. pc_resetlvl(sd,type);
  10154. return SCRIPT_CMD_SUCCESS;
  10155. }
  10156. /**
  10157. * Reset a player status point
  10158. * resetstatus({<char_id>});
  10159. **/
  10160. BUILDIN_FUNC(resetstatus)
  10161. {
  10162. TBL_PC *sd;
  10163. if (!script_charid2sd(2,sd))
  10164. return SCRIPT_CMD_FAILURE;
  10165. pc_resetstate(sd);
  10166. return SCRIPT_CMD_SUCCESS;
  10167. }
  10168. /**
  10169. * Reset player's skill
  10170. * resetskill({<char_id>});
  10171. **/
  10172. BUILDIN_FUNC(resetskill)
  10173. {
  10174. TBL_PC *sd;
  10175. if (!script_charid2sd(2,sd))
  10176. return SCRIPT_CMD_FAILURE;
  10177. pc_resetskill(sd,1);
  10178. return SCRIPT_CMD_SUCCESS;
  10179. }
  10180. /**
  10181. * Counts total amount of skill points.
  10182. * skillpointcount({<char_id>})
  10183. **/
  10184. BUILDIN_FUNC(skillpointcount)
  10185. {
  10186. TBL_PC *sd;
  10187. if (!script_charid2sd(2,sd))
  10188. return SCRIPT_CMD_FAILURE;
  10189. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10190. return SCRIPT_CMD_SUCCESS;
  10191. }
  10192. /*==========================================
  10193. *
  10194. *------------------------------------------*/
  10195. BUILDIN_FUNC(changebase)
  10196. {
  10197. TBL_PC *sd=NULL;
  10198. int vclass;
  10199. if( !script_mapid2sd(3,sd) )
  10200. return SCRIPT_CMD_SUCCESS;
  10201. vclass = script_getnum(st,2);
  10202. if(vclass == JOB_WEDDING)
  10203. {
  10204. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10205. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10206. )
  10207. return SCRIPT_CMD_SUCCESS;
  10208. }
  10209. if(!sd->disguise && vclass != sd->vd.class_) {
  10210. status_set_viewdata(&sd->bl, vclass);
  10211. //Updated client view. Base, Weapon and Cloth Colors.
  10212. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10213. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10214. if (sd->vd.cloth_color)
  10215. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10216. if (sd->vd.body_style)
  10217. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10218. clif_skillinfoblock(sd);
  10219. }
  10220. return SCRIPT_CMD_SUCCESS;
  10221. }
  10222. /**
  10223. * Change account sex and unequip all item and request for a changesex to char-serv
  10224. * changesex({<char_id>});
  10225. */
  10226. BUILDIN_FUNC(changesex)
  10227. {
  10228. int i;
  10229. TBL_PC *sd = NULL;
  10230. if (!script_charid2sd(2,sd))
  10231. return SCRIPT_CMD_FAILURE;
  10232. pc_resetskill(sd,4);
  10233. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10234. for(i = 0; i < EQI_MAX; i++) {
  10235. if (sd->equip_index[i] >= 0)
  10236. pc_unequipitem(sd, sd->equip_index[i], 3);
  10237. }
  10238. chrif_changesex(sd, true);
  10239. return SCRIPT_CMD_SUCCESS;
  10240. }
  10241. /**
  10242. * Change character's sex and unequip all item and request for a changesex to char-serv
  10243. * changecharsex({<char_id>});
  10244. */
  10245. BUILDIN_FUNC(changecharsex)
  10246. {
  10247. #if PACKETVER >= 20141016
  10248. int i;
  10249. TBL_PC *sd = NULL;
  10250. if (!script_charid2sd(2,sd))
  10251. return SCRIPT_CMD_FAILURE;
  10252. pc_resetskill(sd,4);
  10253. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10254. for (i = 0; i < EQI_MAX; i++) {
  10255. if (sd->equip_index[i] >= 0)
  10256. pc_unequipitem(sd, sd->equip_index[i], 3);
  10257. }
  10258. chrif_changesex(sd, false);
  10259. return SCRIPT_CMD_SUCCESS;
  10260. #else
  10261. return SCRIPT_CMD_FAILURE;
  10262. #endif
  10263. }
  10264. /*==========================================
  10265. * Works like 'announce' but outputs in the common chat window
  10266. *------------------------------------------*/
  10267. BUILDIN_FUNC(globalmes)
  10268. {
  10269. struct block_list *bl = map_id2bl(st->oid);
  10270. struct npc_data *nd = (struct npc_data *)bl;
  10271. const char *name=NULL,*mes;
  10272. mes=script_getstr(st,2);
  10273. if(mes==NULL)
  10274. return SCRIPT_CMD_SUCCESS;
  10275. if(script_hasdata(st,3)){ // npc name to display
  10276. name=script_getstr(st,3);
  10277. } else {
  10278. name=nd->name; //use current npc name
  10279. }
  10280. npc_globalmessage(name,mes); // broadcast to all players connected
  10281. return SCRIPT_CMD_SUCCESS;
  10282. }
  10283. /////////////////////////////////////////////////////////////////////
  10284. // NPC waiting room (chat room)
  10285. //
  10286. /// Creates a waiting room (chat room) for this npc.
  10287. ///
  10288. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10289. BUILDIN_FUNC(waitingroom)
  10290. {
  10291. struct npc_data* nd;
  10292. const char* title = script_getstr(st, 2);
  10293. int limit = script_getnum(st, 3);
  10294. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10295. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10296. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10297. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10298. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10299. nd = (struct npc_data *)map_id2bl(st->oid);
  10300. if( nd != NULL )
  10301. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10302. return SCRIPT_CMD_SUCCESS;
  10303. }
  10304. /// Removes the waiting room of the current or target npc.
  10305. ///
  10306. /// delwaitingroom "<npc_name>";
  10307. /// delwaitingroom;
  10308. BUILDIN_FUNC(delwaitingroom)
  10309. {
  10310. struct npc_data* nd;
  10311. if( script_hasdata(st,2) )
  10312. nd = npc_name2id(script_getstr(st, 2));
  10313. else
  10314. nd = (struct npc_data *)map_id2bl(st->oid);
  10315. if( nd != NULL )
  10316. chat_deletenpcchat(nd);
  10317. return SCRIPT_CMD_SUCCESS;
  10318. }
  10319. /// Kick the specified player from the waiting room of the target npc.
  10320. ///
  10321. /// waitingroomkick "<npc_name>", <kickusername>;
  10322. BUILDIN_FUNC(waitingroomkick)
  10323. {
  10324. struct npc_data* nd;
  10325. struct chat_data* cd;
  10326. const char* kickusername;
  10327. nd = npc_name2id(script_getstr(st,2));
  10328. kickusername = script_getstr(st,3);
  10329. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10330. chat_npckickchat(cd, kickusername);
  10331. return SCRIPT_CMD_SUCCESS;
  10332. }
  10333. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10334. /// Num users stored in .@waitingroom_usercount
  10335. ///
  10336. /// getwaitingroomusers "<npc_name>";
  10337. BUILDIN_FUNC(getwaitingroomusers)
  10338. {
  10339. struct npc_data* nd;
  10340. struct chat_data* cd;
  10341. int i, j=0;
  10342. if( script_hasdata(st,2) )
  10343. nd = npc_name2id(script_getstr(st, 2));
  10344. else
  10345. nd = (struct npc_data *)map_id2bl(st->oid);
  10346. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10347. for(i = 0; i < cd->users; ++i) {
  10348. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  10349. j++;
  10350. }
  10351. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  10352. }
  10353. return SCRIPT_CMD_SUCCESS;
  10354. }
  10355. /// Kicks all the players from the waiting room of the current or target npc.
  10356. ///
  10357. /// kickwaitingroomall "<npc_name>";
  10358. /// kickwaitingroomall;
  10359. BUILDIN_FUNC(waitingroomkickall)
  10360. {
  10361. struct npc_data* nd;
  10362. struct chat_data* cd;
  10363. if( script_hasdata(st,2) )
  10364. nd = npc_name2id(script_getstr(st,2));
  10365. else
  10366. nd = (struct npc_data *)map_id2bl(st->oid);
  10367. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10368. chat_npckickall(cd);
  10369. return SCRIPT_CMD_SUCCESS;
  10370. }
  10371. /// Enables the waiting room event of the current or target npc.
  10372. ///
  10373. /// enablewaitingroomevent "<npc_name>";
  10374. /// enablewaitingroomevent;
  10375. BUILDIN_FUNC(enablewaitingroomevent)
  10376. {
  10377. struct npc_data* nd;
  10378. struct chat_data* cd;
  10379. if( script_hasdata(st,2) )
  10380. nd = npc_name2id(script_getstr(st, 2));
  10381. else
  10382. nd = (struct npc_data *)map_id2bl(st->oid);
  10383. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10384. chat_enableevent(cd);
  10385. return SCRIPT_CMD_SUCCESS;
  10386. }
  10387. /// Disables the waiting room event of the current or target npc.
  10388. ///
  10389. /// disablewaitingroomevent "<npc_name>";
  10390. /// disablewaitingroomevent;
  10391. BUILDIN_FUNC(disablewaitingroomevent)
  10392. {
  10393. struct npc_data *nd;
  10394. struct chat_data *cd;
  10395. if( script_hasdata(st,2) )
  10396. nd = npc_name2id(script_getstr(st, 2));
  10397. else
  10398. nd = (struct npc_data *)map_id2bl(st->oid);
  10399. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10400. chat_disableevent(cd);
  10401. return SCRIPT_CMD_SUCCESS;
  10402. }
  10403. /// Returns info on the waiting room of the current or target npc.
  10404. /// Returns -1 if the type unknown
  10405. /// <type>=0 : current number of users
  10406. /// <type>=1 : maximum number of users allowed
  10407. /// <type>=2 : the number of users that trigger the event
  10408. /// <type>=3 : if the trigger is disabled
  10409. /// <type>=4 : the title of the waiting room
  10410. /// <type>=5 : the password of the waiting room
  10411. /// <type>=16 : the name of the waiting room event
  10412. /// <type>=32 : if the waiting room is full
  10413. /// <type>=33 : if there are enough users to trigger the event
  10414. ///
  10415. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10416. /// getwaitingroomstate(<type>) -> <info>
  10417. BUILDIN_FUNC(getwaitingroomstate)
  10418. {
  10419. struct npc_data *nd;
  10420. struct chat_data *cd;
  10421. int type;
  10422. type = script_getnum(st,2);
  10423. if( script_hasdata(st,3) )
  10424. nd = npc_name2id(script_getstr(st, 3));
  10425. else
  10426. nd = (struct npc_data *)map_id2bl(st->oid);
  10427. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10428. {
  10429. script_pushint(st, -1);
  10430. return SCRIPT_CMD_SUCCESS;
  10431. }
  10432. switch(type)
  10433. {
  10434. case 0: script_pushint(st, cd->users); break;
  10435. case 1: script_pushint(st, cd->limit); break;
  10436. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10437. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10438. case 4: script_pushstrcopy(st, cd->title); break;
  10439. case 5: script_pushstrcopy(st, cd->pass); break;
  10440. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10441. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10442. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10443. default: script_pushint(st, -1); break;
  10444. }
  10445. return SCRIPT_CMD_SUCCESS;
  10446. }
  10447. /// Warps the trigger or target amount of players to the target map and position.
  10448. /// Players are automatically removed from the waiting room.
  10449. /// Those waiting the longest will get warped first.
  10450. /// The target map can be "Random" for a random position in the current map,
  10451. /// and "SavePoint" for the savepoint map+position.
  10452. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10453. ///
  10454. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10455. /// The total number of teleported players is put into $@warpwaitingpcnum
  10456. ///
  10457. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10458. /// warpwaitingpc "<map name>",<x>,<y>;
  10459. BUILDIN_FUNC(warpwaitingpc)
  10460. {
  10461. int x;
  10462. int y;
  10463. int i;
  10464. int n;
  10465. const char* map_name;
  10466. struct npc_data* nd;
  10467. struct chat_data* cd;
  10468. nd = (struct npc_data *)map_id2bl(st->oid);
  10469. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10470. return SCRIPT_CMD_SUCCESS;
  10471. map_name = script_getstr(st,2);
  10472. x = script_getnum(st,3);
  10473. y = script_getnum(st,4);
  10474. n = cd->trigger&0x7f;
  10475. if( script_hasdata(st,5) )
  10476. n = script_getnum(st,5);
  10477. for( i = 0; i < n && cd->users > 0; i++ )
  10478. {
  10479. TBL_PC* sd = cd->usersd[0];
  10480. if( strcmp(map_name,"SavePoint") == 0 && map_getmapflag(sd->bl.m, MF_NOTELEPORT) )
  10481. {// can't teleport on this map
  10482. break;
  10483. }
  10484. if( cd->zeny )
  10485. {// fee set
  10486. if( (uint32)sd->status.zeny < cd->zeny )
  10487. {// no zeny to cover set fee
  10488. break;
  10489. }
  10490. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10491. }
  10492. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10493. if( strcmp(map_name,"Random") == 0 )
  10494. pc_randomwarp(sd,CLR_TELEPORT);
  10495. else if( strcmp(map_name,"SavePoint") == 0 )
  10496. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10497. else
  10498. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10499. }
  10500. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10501. return SCRIPT_CMD_SUCCESS;
  10502. }
  10503. /////////////////////////////////////////////////////////////////////
  10504. // ...
  10505. //
  10506. /// Detaches a character from a script.
  10507. ///
  10508. /// @param st Script state to detach the character from.
  10509. static void script_detach_rid(struct script_state* st)
  10510. {
  10511. if(st->rid)
  10512. {
  10513. script_detach_state(st, false);
  10514. st->rid = 0;
  10515. }
  10516. }
  10517. /*=========================================================================
  10518. * Attaches a set of RIDs to the current script. [digitalhamster]
  10519. * addrid(<type>{,<flag>{,<parameters>}});
  10520. * <type>:
  10521. * 0 : All players in the server.
  10522. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10523. * 2 : Party members of a specified party ID.
  10524. * [ Parameters: <party id> ]
  10525. * 3 : Guild members of a specified guild ID.
  10526. * [ Parameters: <guild id> ]
  10527. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10528. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10529. * 5 : All players in the map.
  10530. * [ Parameters: "<map name>" ]
  10531. * Account ID: The specified account ID.
  10532. * <flag>:
  10533. * 0 : Players are always attached. (default)
  10534. * 1 : Players currently running another script will not be attached.
  10535. *-------------------------------------------------------------------------*/
  10536. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10537. {
  10538. int forceflag;
  10539. struct map_session_data *sd = (TBL_PC *)bl;
  10540. struct script_state* st;
  10541. st = va_arg(ap,struct script_state*);
  10542. forceflag = va_arg(ap,int);
  10543. if(!forceflag || !sd->st)
  10544. if(sd->status.account_id != st->rid)
  10545. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10546. return 0;
  10547. }
  10548. BUILDIN_FUNC(addrid)
  10549. {
  10550. struct s_mapiterator* iter;
  10551. struct block_list *bl;
  10552. TBL_PC *sd;
  10553. if(st->rid < 1) {
  10554. st->state = END;
  10555. bl = map_id2bl(st->oid);
  10556. } else
  10557. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10558. iter = mapit_getallusers();
  10559. switch(script_getnum(st,2)) {
  10560. case 0:
  10561. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10562. if(!script_getnum(st,3) || !sd->st)
  10563. if(sd->status.account_id != st->rid) //attached player already runs.
  10564. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10565. }
  10566. break;
  10567. case 1:
  10568. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10569. if(!script_getnum(st,3) || !sd->st)
  10570. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10571. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10572. }
  10573. break;
  10574. case 2:
  10575. if(script_getnum(st,4) == 0) {
  10576. script_pushint(st,0);
  10577. return SCRIPT_CMD_SUCCESS;
  10578. }
  10579. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10580. if(!script_getnum(st,3) || !sd->st)
  10581. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10582. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10583. }
  10584. break;
  10585. case 3:
  10586. if(script_getnum(st,4) == 0) {
  10587. script_pushint(st,0);
  10588. return SCRIPT_CMD_SUCCESS;
  10589. }
  10590. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10591. if(!script_getnum(st,3) || !sd->st)
  10592. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10593. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10594. }
  10595. break;
  10596. case 4:
  10597. map_foreachinallarea(buildin_addrid_sub,
  10598. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10599. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10600. break;
  10601. case 5:
  10602. if (script_getstr(st, 4) == NULL) {
  10603. script_pushint(st, 0);
  10604. return SCRIPT_CMD_FAILURE;
  10605. }
  10606. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10607. script_pushint(st, 0);
  10608. return SCRIPT_CMD_FAILURE;
  10609. }
  10610. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10611. break;
  10612. default:
  10613. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10614. script_pushint(st,0);
  10615. return SCRIPT_CMD_SUCCESS;
  10616. }
  10617. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10618. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10619. script_pushint(st,1);
  10620. }
  10621. return SCRIPT_CMD_SUCCESS;
  10622. }
  10623. mapit_free(iter);
  10624. script_pushint(st,1);
  10625. return SCRIPT_CMD_SUCCESS;
  10626. }
  10627. /*==========================================
  10628. * Attach sd char id to script and detach current one if any
  10629. *------------------------------------------*/
  10630. BUILDIN_FUNC(attachrid)
  10631. {
  10632. int rid = script_getnum(st,2);
  10633. bool force;
  10634. if( script_hasdata(st,3) ){
  10635. force = script_getnum(st,3) != 0;
  10636. }else{
  10637. force = true;
  10638. }
  10639. struct map_session_data* sd = map_id2sd(rid);
  10640. if( sd != NULL && ( !sd->npc_id || force ) ){
  10641. script_detach_rid(st);
  10642. st->rid = rid;
  10643. script_attach_state(st);
  10644. script_pushint(st,true);
  10645. }else{
  10646. script_pushint(st,false);
  10647. }
  10648. return SCRIPT_CMD_SUCCESS;
  10649. }
  10650. /*==========================================
  10651. * Detach script to rid
  10652. *------------------------------------------*/
  10653. BUILDIN_FUNC(detachrid)
  10654. {
  10655. script_detach_rid(st);
  10656. return SCRIPT_CMD_SUCCESS;
  10657. }
  10658. /*==========================================
  10659. * Chk if account connected, (and charid from account if specified)
  10660. *------------------------------------------*/
  10661. BUILDIN_FUNC(isloggedin)
  10662. {
  10663. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10664. if (script_hasdata(st,3) && sd &&
  10665. sd->status.char_id != script_getnum(st,3))
  10666. sd = NULL;
  10667. push_val(st->stack,C_INT,sd!=NULL);
  10668. return SCRIPT_CMD_SUCCESS;
  10669. }
  10670. /*==========================================
  10671. *
  10672. *------------------------------------------*/
  10673. BUILDIN_FUNC(setmapflagnosave)
  10674. {
  10675. int16 m,x,y;
  10676. unsigned short mapindex;
  10677. const char *str,*str2;
  10678. union u_mapflag_args args = {};
  10679. str=script_getstr(st,2);
  10680. str2=script_getstr(st,3);
  10681. x=script_getnum(st,4);
  10682. y=script_getnum(st,5);
  10683. m = map_mapname2mapid(str);
  10684. mapindex = mapindex_name2id(str2);
  10685. if(m < 0) {
  10686. ShowWarning("buildin_setmapflagnosave: Invalid map name %s.\n", str);
  10687. return SCRIPT_CMD_FAILURE;
  10688. }
  10689. args.nosave.map = mapindex;
  10690. args.nosave.x = x;
  10691. args.nosave.y = y;
  10692. map_setmapflag_sub(m, MF_NOSAVE, true, &args);
  10693. return SCRIPT_CMD_SUCCESS;
  10694. }
  10695. BUILDIN_FUNC(getmapflag)
  10696. {
  10697. int16 m;
  10698. int mf;
  10699. const char *str;
  10700. str=script_getstr(st,2);
  10701. m = map_mapname2mapid(str);
  10702. if (m < 0) {
  10703. ShowWarning("buildin_getmapflag: Invalid map name %s.\n", str);
  10704. return SCRIPT_CMD_FAILURE;
  10705. }
  10706. mf = script_getnum(st, 3);
  10707. if( mf < MF_MIN || mf > MF_MAX ){
  10708. ShowError( "buildin_getmapflag: Unsupported mapflag '%d'.\n", mf );
  10709. return SCRIPT_CMD_FAILURE;
  10710. }
  10711. union u_mapflag_args args = {};
  10712. if (mf == MF_SKILL_DAMAGE && !script_hasdata(st, 4))
  10713. args.flag_val = SKILLDMG_MAX;
  10714. else
  10715. FETCH(4, args.flag_val);
  10716. script_pushint(st, map_getmapflag_sub(m, static_cast<e_mapflag>(mf), &args));
  10717. return SCRIPT_CMD_SUCCESS;
  10718. }
  10719. BUILDIN_FUNC(setmapflag)
  10720. {
  10721. int16 m;
  10722. int mf;
  10723. const char *str;
  10724. str = script_getstr(st,2);
  10725. m = map_mapname2mapid(str);
  10726. if (m < 0) {
  10727. ShowWarning("buildin_setmapflag: Invalid map name %s.\n", str);
  10728. return SCRIPT_CMD_FAILURE;
  10729. }
  10730. mf = script_getnum(st, 3);
  10731. if( mf < MF_MIN || mf > MF_MAX ){
  10732. ShowError( "buildin_setmapflag: Unsupported mapflag '%d'.\n", mf );
  10733. return SCRIPT_CMD_FAILURE;
  10734. }
  10735. union u_mapflag_args args = {};
  10736. switch(mf) {
  10737. case MF_SKILL_DAMAGE:
  10738. if (script_hasdata(st, 4) && script_hasdata(st, 5))
  10739. args.skill_damage.rate[script_getnum(st, 5)] = script_getnum(st, 4);
  10740. else {
  10741. ShowWarning("buildin_setmapflag: Unable to set skill_damage mapflag as flag data is missing.\n");
  10742. return SCRIPT_CMD_FAILURE;
  10743. }
  10744. break;
  10745. case MF_NOSAVE: // Assume setting "SavePoint"
  10746. args.nosave.map = 0;
  10747. args.nosave.x = -1;
  10748. args.nosave.y = -1;
  10749. break;
  10750. case MF_PVP_NIGHTMAREDROP: // Assume setting standard drops
  10751. args.nightmaredrop.drop_id = -1;
  10752. args.nightmaredrop.drop_per = 300;
  10753. args.nightmaredrop.drop_type = NMDT_EQUIP;
  10754. break;
  10755. default:
  10756. FETCH(4, args.flag_val);
  10757. break;
  10758. }
  10759. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), true, &args);
  10760. return SCRIPT_CMD_SUCCESS;
  10761. }
  10762. BUILDIN_FUNC(removemapflag)
  10763. {
  10764. int16 m;
  10765. int mf;
  10766. const char *str;
  10767. union u_mapflag_args args = {};
  10768. str = script_getstr(st, 2);
  10769. m = map_mapname2mapid(str);
  10770. if (m < 0) {
  10771. ShowWarning("buildin_removemapflag: Invalid map name %s.\n", str);
  10772. return SCRIPT_CMD_FAILURE;
  10773. }
  10774. mf = script_getnum(st, 3);
  10775. if( mf < MF_MIN || mf > MF_MAX ){
  10776. ShowError( "buildin_removemapflag: Unsupported mapflag '%d'.\n", mf );
  10777. return SCRIPT_CMD_FAILURE;
  10778. }
  10779. FETCH(4, args.flag_val);
  10780. map_setmapflag_sub(m, static_cast<e_mapflag>(mf), false, &args);
  10781. return SCRIPT_CMD_SUCCESS;
  10782. }
  10783. BUILDIN_FUNC(pvpon)
  10784. {
  10785. int16 m;
  10786. const char *str;
  10787. TBL_PC* sd = NULL;
  10788. struct s_mapiterator* iter;
  10789. str = script_getstr(st,2);
  10790. m = map_mapname2mapid(str);
  10791. if( m < 0 || map_getmapflag(m, MF_PVP) )
  10792. return SCRIPT_CMD_SUCCESS; // nothing to do
  10793. map_setmapflag(m, MF_PVP, true);
  10794. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10795. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10796. return SCRIPT_CMD_SUCCESS;
  10797. iter = mapit_getallusers();
  10798. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10799. {
  10800. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10801. continue; // not applicable
  10802. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10803. sd->pvp_rank = 0;
  10804. sd->pvp_lastusers = 0;
  10805. sd->pvp_point = 5;
  10806. sd->pvp_won = 0;
  10807. sd->pvp_lost = 0;
  10808. }
  10809. mapit_free(iter);
  10810. return SCRIPT_CMD_SUCCESS;
  10811. }
  10812. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10813. {
  10814. TBL_PC* sd = (TBL_PC*)bl;
  10815. clif_pvpset(sd, 0, 0, 2);
  10816. if (sd->pvp_timer != INVALID_TIMER) {
  10817. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10818. sd->pvp_timer = INVALID_TIMER;
  10819. }
  10820. return 0;
  10821. }
  10822. BUILDIN_FUNC(pvpoff)
  10823. {
  10824. int16 m;
  10825. const char *str;
  10826. str=script_getstr(st,2);
  10827. m = map_mapname2mapid(str);
  10828. if(m < 0 || !map_getmapflag(m, MF_PVP))
  10829. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10830. map_setmapflag(m, MF_PVP, false);
  10831. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10832. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10833. return SCRIPT_CMD_SUCCESS;
  10834. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10835. return SCRIPT_CMD_SUCCESS;
  10836. }
  10837. BUILDIN_FUNC(gvgon)
  10838. {
  10839. int16 m;
  10840. const char *str;
  10841. str=script_getstr(st,2);
  10842. m = map_mapname2mapid(str);
  10843. if(m >= 0 && !map_getmapflag(m, MF_GVG)) {
  10844. map_setmapflag(m, MF_GVG, true);
  10845. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10846. }
  10847. return SCRIPT_CMD_SUCCESS;
  10848. }
  10849. BUILDIN_FUNC(gvgoff)
  10850. {
  10851. int16 m;
  10852. const char *str;
  10853. str=script_getstr(st,2);
  10854. m = map_mapname2mapid(str);
  10855. if(m >= 0 && map_getmapflag(m, MF_GVG)) {
  10856. map_setmapflag(m, MF_GVG, false);
  10857. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10858. }
  10859. return SCRIPT_CMD_SUCCESS;
  10860. }
  10861. BUILDIN_FUNC(gvgon3)
  10862. {
  10863. int16 m;
  10864. const char *str;
  10865. str = script_getstr(st,2);
  10866. m = map_mapname2mapid(str);
  10867. if (m >= 0 && !map_getmapflag(m, MF_GVG_TE)) {
  10868. map_setmapflag(m, MF_GVG_TE, true);
  10869. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10870. }
  10871. return SCRIPT_CMD_SUCCESS;
  10872. }
  10873. BUILDIN_FUNC(gvgoff3)
  10874. {
  10875. int16 m;
  10876. const char *str;
  10877. str = script_getstr(st,2);
  10878. m = map_mapname2mapid(str);
  10879. if (m >= 0 && map_getmapflag(m, MF_GVG_TE)) {
  10880. map_setmapflag(m, MF_GVG_TE, false);
  10881. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10882. }
  10883. return SCRIPT_CMD_SUCCESS;
  10884. }
  10885. /**
  10886. * Shows an emotion on top of a NPC by default or the given GID
  10887. * emotion <emotion ID>{,<target ID>};
  10888. */
  10889. BUILDIN_FUNC(emotion)
  10890. {
  10891. struct block_list *bl = NULL;
  10892. int type = script_getnum(st,2);
  10893. if (type < ET_SURPRISE || type >= ET_MAX) {
  10894. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  10895. return SCRIPT_CMD_FAILURE;
  10896. }
  10897. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  10898. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  10899. return SCRIPT_CMD_FAILURE;
  10900. }
  10901. if (!bl)
  10902. bl = map_id2bl(st->oid);
  10903. clif_emotion(bl, type);
  10904. return SCRIPT_CMD_SUCCESS;
  10905. }
  10906. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10907. {
  10908. int16 m=va_arg(ap,int);
  10909. int g_id=va_arg(ap,int);
  10910. int flag=va_arg(ap,int);
  10911. if(!sd || sd->bl.m != m)
  10912. return 0;
  10913. if(
  10914. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10915. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10916. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10917. )
  10918. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10919. return 1;
  10920. }
  10921. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10922. {
  10923. struct mob_data *md=(struct mob_data *)bl;
  10924. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  10925. status_kill(bl);
  10926. return 1;
  10927. }
  10928. /*
  10929. * Function to kick guild members out of a map and to their save points.
  10930. * m : mapid
  10931. * g_id : owner guild id
  10932. * flag & 1 : Warp out owners
  10933. * flag & 2 : Warp out outsiders
  10934. * flag & 4 : reserved for mobs
  10935. * */
  10936. BUILDIN_FUNC(maprespawnguildid)
  10937. {
  10938. const char *mapname=script_getstr(st,2);
  10939. int g_id=script_getnum(st,3);
  10940. int flag=script_getnum(st,4);
  10941. int16 m=map_mapname2mapid(mapname);
  10942. if(m == -1)
  10943. return SCRIPT_CMD_SUCCESS;
  10944. //Catch ALL players (in case some are 'between maps' on execution time)
  10945. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10946. if (flag&4) //Remove script mobs.
  10947. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10948. return SCRIPT_CMD_SUCCESS;
  10949. }
  10950. /// Siege commands
  10951. /**
  10952. * Start WoE:FE
  10953. * agitstart();
  10954. */
  10955. BUILDIN_FUNC(agitstart)
  10956. {
  10957. guild_agit_start();
  10958. return SCRIPT_CMD_SUCCESS;
  10959. }
  10960. /**
  10961. * End WoE:FE
  10962. * agitend();
  10963. */
  10964. BUILDIN_FUNC(agitend)
  10965. {
  10966. guild_agit_end();
  10967. return SCRIPT_CMD_SUCCESS;
  10968. }
  10969. /**
  10970. * Start WoE:SE
  10971. * agitstart2();
  10972. */
  10973. BUILDIN_FUNC(agitstart2)
  10974. {
  10975. guild_agit2_start();
  10976. return SCRIPT_CMD_SUCCESS;
  10977. }
  10978. /**
  10979. * End WoE:SE
  10980. * agitend();
  10981. */
  10982. BUILDIN_FUNC(agitend2)
  10983. {
  10984. guild_agit2_end();
  10985. return SCRIPT_CMD_SUCCESS;
  10986. }
  10987. /**
  10988. * Start WoE:TE
  10989. * agitstart3();
  10990. */
  10991. BUILDIN_FUNC(agitstart3)
  10992. {
  10993. guild_agit3_start();
  10994. return SCRIPT_CMD_SUCCESS;
  10995. }
  10996. /**
  10997. * End WoE:TE
  10998. * agitend3();
  10999. */
  11000. BUILDIN_FUNC(agitend3)
  11001. {
  11002. guild_agit3_end();
  11003. return SCRIPT_CMD_SUCCESS;
  11004. }
  11005. /**
  11006. * Returns whether WoE:FE is on or off.
  11007. * agitcheck();
  11008. */
  11009. BUILDIN_FUNC(agitcheck)
  11010. {
  11011. script_pushint(st, agit_flag);
  11012. return SCRIPT_CMD_SUCCESS;
  11013. }
  11014. /**
  11015. * Returns whether WoE:SE is on or off.
  11016. * agitcheck2();
  11017. */
  11018. BUILDIN_FUNC(agitcheck2)
  11019. {
  11020. script_pushint(st, agit2_flag);
  11021. return SCRIPT_CMD_SUCCESS;
  11022. }
  11023. /**
  11024. * Returns whether WoE:TE is on or off.
  11025. * agitcheck3();
  11026. */
  11027. BUILDIN_FUNC(agitcheck3)
  11028. {
  11029. script_pushint(st, agit3_flag);
  11030. return SCRIPT_CMD_SUCCESS;
  11031. }
  11032. /// Sets the guild_id of this npc.
  11033. ///
  11034. /// flagemblem <guild_id>;
  11035. BUILDIN_FUNC(flagemblem)
  11036. {
  11037. TBL_NPC* nd;
  11038. int g_id = script_getnum(st,2);
  11039. if(g_id < 0)
  11040. return SCRIPT_CMD_SUCCESS;
  11041. nd = (TBL_NPC*)map_id2nd(st->oid);
  11042. if( nd == NULL ) {
  11043. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11044. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11045. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11046. } else {
  11047. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11048. nd->u.scr.guild_id = g_id;
  11049. clif_guild_emblem_area(&nd->bl);
  11050. /* guild flag caching */
  11051. if( g_id ) /* adding a id */
  11052. guild_flag_add(nd);
  11053. else if( changed ) /* removing a flag */
  11054. guild_flag_remove(nd);
  11055. }
  11056. return SCRIPT_CMD_SUCCESS;
  11057. }
  11058. BUILDIN_FUNC(getcastlename)
  11059. {
  11060. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11061. struct guild_castle* gc = guild_mapname2gc(mapname);
  11062. const char* name = (gc) ? gc->castle_name : "";
  11063. script_pushstrcopy(st,name);
  11064. return SCRIPT_CMD_SUCCESS;
  11065. }
  11066. BUILDIN_FUNC(getcastledata)
  11067. {
  11068. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11069. int index = script_getnum(st,3);
  11070. struct guild_castle *gc = guild_mapname2gc(mapname);
  11071. if (gc == NULL) {
  11072. script_pushint(st,0);
  11073. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11074. return SCRIPT_CMD_FAILURE;
  11075. }
  11076. switch (index) {
  11077. case 1:
  11078. script_pushint(st,gc->guild_id); break;
  11079. case 2:
  11080. script_pushint(st,gc->economy); break;
  11081. case 3:
  11082. script_pushint(st,gc->defense); break;
  11083. case 4:
  11084. script_pushint(st,gc->triggerE); break;
  11085. case 5:
  11086. script_pushint(st,gc->triggerD); break;
  11087. case 6:
  11088. script_pushint(st,gc->nextTime); break;
  11089. case 7:
  11090. script_pushint(st,gc->payTime); break;
  11091. case 8:
  11092. script_pushint(st,gc->createTime); break;
  11093. case 9:
  11094. script_pushint(st,gc->visibleC); break;
  11095. default:
  11096. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  11097. script_pushint(st,gc->guardian[index-10].visible);
  11098. break;
  11099. }
  11100. script_pushint(st,0);
  11101. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11102. return SCRIPT_CMD_FAILURE;
  11103. }
  11104. return SCRIPT_CMD_SUCCESS;
  11105. }
  11106. BUILDIN_FUNC(setcastledata)
  11107. {
  11108. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11109. int index = script_getnum(st,3);
  11110. int value = script_getnum(st,4);
  11111. struct guild_castle *gc = guild_mapname2gc(mapname);
  11112. if (gc == NULL) {
  11113. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11114. return SCRIPT_CMD_FAILURE;
  11115. }
  11116. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  11117. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11118. return SCRIPT_CMD_FAILURE;
  11119. }
  11120. guild_castledatasave(gc->castle_id, index, value);
  11121. return SCRIPT_CMD_SUCCESS;
  11122. }
  11123. /* =====================================================================
  11124. * ---------------------------------------------------------------------*/
  11125. BUILDIN_FUNC(requestguildinfo)
  11126. {
  11127. int guild_id=script_getnum(st,2);
  11128. const char *event=NULL;
  11129. if( script_hasdata(st,3) ){
  11130. event=script_getstr(st,3);
  11131. check_event(st, event);
  11132. }
  11133. if(guild_id>0)
  11134. guild_npc_request_info(guild_id,event);
  11135. return SCRIPT_CMD_SUCCESS;
  11136. }
  11137. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11138. /// getequipcardcnt(<equipment slot>);
  11139. BUILDIN_FUNC(getequipcardcnt)
  11140. {
  11141. int i=-1,j,num;
  11142. TBL_PC *sd;
  11143. int count;
  11144. if( !script_rid2sd(sd) )
  11145. return SCRIPT_CMD_SUCCESS;
  11146. num=script_getnum(st,2);
  11147. if (equip_index_check(num))
  11148. i=pc_checkequip(sd,equip_bitmask[num]);
  11149. if (i < 0 || !sd->inventory_data[i]) {
  11150. script_pushint(st,0);
  11151. return SCRIPT_CMD_SUCCESS;
  11152. }
  11153. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11154. {
  11155. script_pushint(st,0);
  11156. return SCRIPT_CMD_SUCCESS;
  11157. }
  11158. count = 0;
  11159. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11160. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11161. count++;
  11162. script_pushint(st,count);
  11163. return SCRIPT_CMD_SUCCESS;
  11164. }
  11165. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11166. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11167. /// successremovecards <slot>;
  11168. BUILDIN_FUNC(successremovecards) {
  11169. int i=-1,c,cardflag=0;
  11170. TBL_PC* sd;
  11171. int num;
  11172. if( !script_rid2sd(sd) )
  11173. return SCRIPT_CMD_SUCCESS;
  11174. num = script_getnum(st,2);
  11175. if (equip_index_check(num))
  11176. i=pc_checkequip(sd,equip_bitmask[num]);
  11177. if (i < 0 || !sd->inventory_data[i]) {
  11178. return SCRIPT_CMD_SUCCESS;
  11179. }
  11180. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11181. return SCRIPT_CMD_SUCCESS;
  11182. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11183. 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
  11184. unsigned char flag = 0;
  11185. struct item item_tmp;
  11186. memset(&item_tmp,0,sizeof(item_tmp));
  11187. cardflag = 1;
  11188. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11189. item_tmp.identify = 1;
  11190. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11191. clif_additem(sd,0,0,flag);
  11192. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11193. }
  11194. }
  11195. }
  11196. if(cardflag == 1) {//if card was remove remplace item with no card
  11197. unsigned char flag = 0, j;
  11198. struct item item_tmp;
  11199. memset(&item_tmp,0,sizeof(item_tmp));
  11200. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11201. item_tmp.identify = 1;
  11202. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11203. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11204. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11205. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11206. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11207. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11208. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11209. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11210. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11211. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11212. }
  11213. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11214. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11215. clif_additem(sd,0,0,flag);
  11216. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11217. }
  11218. clif_misceffect(&sd->bl,3);
  11219. }
  11220. return SCRIPT_CMD_SUCCESS;
  11221. }
  11222. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11223. /// failedremovecards <slot>, <type>;
  11224. /// <type>=0 : will destroy both the item and the cards.
  11225. /// <type>=1 : will keep the item, but destroy the cards.
  11226. /// <type>=2 : will keep the cards, but destroy the item.
  11227. /// <type>=? : will just display the failure effect.
  11228. BUILDIN_FUNC(failedremovecards) {
  11229. int i=-1,c,cardflag=0;
  11230. TBL_PC* sd;
  11231. int num;
  11232. int typefail;
  11233. if( !script_rid2sd(sd) )
  11234. return SCRIPT_CMD_SUCCESS;
  11235. num = script_getnum(st,2);
  11236. typefail = script_getnum(st,3);
  11237. if (equip_index_check(num))
  11238. i=pc_checkequip(sd,equip_bitmask[num]);
  11239. if (i < 0 || !sd->inventory_data[i])
  11240. return SCRIPT_CMD_SUCCESS;
  11241. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11242. return SCRIPT_CMD_SUCCESS;
  11243. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11244. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11245. cardflag = 1;
  11246. if(typefail == 2) {// add cards to inventory, clear
  11247. unsigned char flag = 0;
  11248. struct item item_tmp;
  11249. memset(&item_tmp,0,sizeof(item_tmp));
  11250. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11251. item_tmp.identify = 1;
  11252. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11253. clif_additem(sd,0,0,flag);
  11254. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11255. }
  11256. }
  11257. }
  11258. }
  11259. if(cardflag == 1) {
  11260. if(typefail == 0 || typefail == 2){ // destroy the item
  11261. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11262. }else if(typefail == 1){ // destroy the card
  11263. unsigned char flag = 0, j;
  11264. struct item item_tmp;
  11265. memset(&item_tmp,0,sizeof(item_tmp));
  11266. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11267. item_tmp.identify = 1;
  11268. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11269. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11270. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11271. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11272. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11273. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11274. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11275. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11276. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11277. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11278. }
  11279. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11280. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11281. clif_additem(sd,0,0,flag);
  11282. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11283. }
  11284. }
  11285. clif_misceffect(&sd->bl,2);
  11286. }
  11287. return SCRIPT_CMD_SUCCESS;
  11288. }
  11289. /* ================================================================
  11290. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11291. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11292. * improved by [Lance]
  11293. * ================================================================*/
  11294. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11295. {
  11296. int x,y,m,check_val=0,check_ID=0,i=0;
  11297. struct guild *g = NULL;
  11298. struct party_data *p = NULL;
  11299. const char *str;
  11300. const char *mapname;
  11301. unsigned int index;
  11302. mapname=script_getstr(st,2);
  11303. str=script_getstr(st,3);
  11304. x=script_getnum(st,4);
  11305. y=script_getnum(st,5);
  11306. if(script_hasdata(st,7)){
  11307. check_val=script_getnum(st,6);
  11308. check_ID=script_getnum(st,7);
  11309. }
  11310. if((m=map_mapname2mapid(mapname))< 0)
  11311. return SCRIPT_CMD_SUCCESS;
  11312. if(!(index=mapindex_name2id(str)))
  11313. return SCRIPT_CMD_SUCCESS;
  11314. switch(check_val){
  11315. case 1:
  11316. g = guild_search(check_ID);
  11317. if (g){
  11318. for( i=0; i < g->max_member; i++)
  11319. {
  11320. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11321. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11322. }
  11323. }
  11324. }
  11325. break;
  11326. case 2:
  11327. p = party_search(check_ID);
  11328. if(p){
  11329. for(i=0;i<MAX_PARTY; i++){
  11330. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11331. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11332. }
  11333. }
  11334. }
  11335. break;
  11336. default:
  11337. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11338. break;
  11339. }
  11340. return SCRIPT_CMD_SUCCESS;
  11341. }
  11342. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11343. {
  11344. char *event=va_arg(ap,char *);
  11345. struct mob_data *md = ((struct mob_data *)bl);
  11346. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11347. return 1;
  11348. return SCRIPT_CMD_SUCCESS;
  11349. }
  11350. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11351. {
  11352. const char *mapname,*event;
  11353. int16 m;
  11354. mapname=script_getstr(st,2);
  11355. event=script_getstr(st,3);
  11356. if( strcmp(event, "all") == 0 )
  11357. event = NULL;
  11358. else
  11359. check_event(st, event);
  11360. if( strcmp(mapname, "this") == 0 ) {
  11361. struct map_session_data *sd;
  11362. if( script_rid2sd(sd) )
  11363. m = sd->bl.m;
  11364. else {
  11365. script_pushint(st,-1);
  11366. return SCRIPT_CMD_SUCCESS;
  11367. }
  11368. }
  11369. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11370. script_pushint(st,-1);
  11371. return SCRIPT_CMD_SUCCESS;
  11372. }
  11373. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11374. return SCRIPT_CMD_SUCCESS;
  11375. }
  11376. BUILDIN_FUNC(marriage)
  11377. {
  11378. TBL_PC *sd;
  11379. TBL_PC *p_sd;
  11380. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11381. script_pushint(st,0);
  11382. return SCRIPT_CMD_SUCCESS;
  11383. }
  11384. script_pushint(st,1);
  11385. return SCRIPT_CMD_SUCCESS;
  11386. }
  11387. BUILDIN_FUNC(wedding_effect)
  11388. {
  11389. TBL_PC *sd;
  11390. struct block_list *bl;
  11391. if(!script_rid2sd(sd)) {
  11392. bl=map_id2bl(st->oid);
  11393. } else
  11394. bl=&sd->bl;
  11395. clif_wedding_effect(bl);
  11396. return SCRIPT_CMD_SUCCESS;
  11397. }
  11398. /**
  11399. * divorce({<char_id>})
  11400. **/
  11401. BUILDIN_FUNC(divorce)
  11402. {
  11403. TBL_PC *sd;
  11404. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11405. script_pushint(st,0);
  11406. return SCRIPT_CMD_FAILURE;
  11407. }
  11408. script_pushint(st,1);
  11409. return SCRIPT_CMD_SUCCESS;
  11410. }
  11411. /**
  11412. * ispartneron({<char_id>})
  11413. **/
  11414. BUILDIN_FUNC(ispartneron)
  11415. {
  11416. TBL_PC *sd;
  11417. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11418. map_charid2sd(sd->status.partner_id) == NULL)
  11419. {
  11420. script_pushint(st,0);
  11421. return SCRIPT_CMD_FAILURE;
  11422. }
  11423. script_pushint(st,1);
  11424. return SCRIPT_CMD_SUCCESS;
  11425. }
  11426. /**
  11427. * getpartnerid({<char_id>})
  11428. **/
  11429. BUILDIN_FUNC(getpartnerid)
  11430. {
  11431. TBL_PC *sd;
  11432. if (!script_charid2sd(2,sd)) {
  11433. script_pushint(st,0);
  11434. return SCRIPT_CMD_FAILURE;
  11435. }
  11436. script_pushint(st,sd->status.partner_id);
  11437. return SCRIPT_CMD_SUCCESS;
  11438. }
  11439. /**
  11440. * getchildid({<char_id>})
  11441. **/
  11442. BUILDIN_FUNC(getchildid)
  11443. {
  11444. TBL_PC *sd;
  11445. if (!script_charid2sd(2,sd)) {
  11446. script_pushint(st,0);
  11447. return SCRIPT_CMD_FAILURE;
  11448. }
  11449. script_pushint(st,sd->status.child);
  11450. return SCRIPT_CMD_SUCCESS;
  11451. }
  11452. /**
  11453. * getmotherid({<char_id>})
  11454. **/
  11455. BUILDIN_FUNC(getmotherid)
  11456. {
  11457. TBL_PC *sd;
  11458. if (!script_charid2sd(2,sd)) {
  11459. script_pushint(st,0);
  11460. return SCRIPT_CMD_FAILURE;
  11461. }
  11462. script_pushint(st,sd->status.mother);
  11463. return SCRIPT_CMD_SUCCESS;
  11464. }
  11465. /**
  11466. * getfatherid({<char_id>})
  11467. **/
  11468. BUILDIN_FUNC(getfatherid)
  11469. {
  11470. TBL_PC *sd;
  11471. if (!script_charid2sd(2,sd)) {
  11472. script_pushint(st,0);
  11473. return SCRIPT_CMD_FAILURE;
  11474. }
  11475. script_pushint(st,sd->status.father);
  11476. return SCRIPT_CMD_SUCCESS;
  11477. }
  11478. BUILDIN_FUNC(warppartner)
  11479. {
  11480. int x,y;
  11481. unsigned short mapindex;
  11482. const char *str;
  11483. TBL_PC *sd;
  11484. TBL_PC *p_sd;
  11485. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  11486. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11487. script_pushint(st,0);
  11488. return SCRIPT_CMD_SUCCESS;
  11489. }
  11490. str=script_getstr(st,2);
  11491. x=script_getnum(st,3);
  11492. y=script_getnum(st,4);
  11493. mapindex = mapindex_name2id(str);
  11494. if (mapindex) {
  11495. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11496. script_pushint(st,1);
  11497. } else
  11498. script_pushint(st,0);
  11499. return SCRIPT_CMD_SUCCESS;
  11500. }
  11501. /*================================================
  11502. * Script for Displaying MOB Information [Valaris]
  11503. *------------------------------------------------*/
  11504. BUILDIN_FUNC(strmobinfo)
  11505. {
  11506. int num=script_getnum(st,2);
  11507. int class_=script_getnum(st,3);
  11508. if(!mobdb_checkid(class_))
  11509. {
  11510. if (num < 3) //requested a string
  11511. script_pushconststr(st,"");
  11512. else
  11513. script_pushint(st,0);
  11514. return SCRIPT_CMD_SUCCESS;
  11515. }
  11516. switch (num) {
  11517. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11518. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11519. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11520. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11521. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11522. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11523. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11524. default:
  11525. script_pushint(st,0);
  11526. break;
  11527. }
  11528. return SCRIPT_CMD_SUCCESS;
  11529. }
  11530. /*==========================================
  11531. * Summon guardians [Valaris]
  11532. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11533. *------------------------------------------*/
  11534. BUILDIN_FUNC(guardian)
  11535. {
  11536. int class_=0,x=0,y=0,guardian=0;
  11537. const char *str,*mapname,*evt="";
  11538. bool has_index = false;
  11539. mapname =script_getstr(st,2);
  11540. x =script_getnum(st,3);
  11541. y =script_getnum(st,4);
  11542. str =script_getstr(st,5);
  11543. class_=script_getnum(st,6);
  11544. if( script_hasdata(st,8) )
  11545. {// "<event label>",<guardian index>
  11546. evt=script_getstr(st,7);
  11547. guardian=script_getnum(st,8);
  11548. has_index = true;
  11549. } else if( script_hasdata(st,7) ){
  11550. struct script_data *data = script_getdata(st,7);
  11551. get_val(st,data);
  11552. if( data_isstring(data) )
  11553. {// "<event label>"
  11554. evt=script_getstr(st,7);
  11555. } else if( data_isint(data) )
  11556. {// <guardian index>
  11557. guardian=script_getnum(st,7);
  11558. has_index = true;
  11559. } else {
  11560. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11561. script_reportdata(data);
  11562. return SCRIPT_CMD_FAILURE;
  11563. }
  11564. }
  11565. check_event(st, evt);
  11566. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11567. return SCRIPT_CMD_SUCCESS;
  11568. }
  11569. /*==========================================
  11570. * Invisible Walls [Zephyrus]
  11571. *------------------------------------------*/
  11572. BUILDIN_FUNC(setwall)
  11573. {
  11574. const char *mapname, *name;
  11575. int x, y, m, size, dir;
  11576. bool shootable;
  11577. mapname = script_getstr(st,2);
  11578. x = script_getnum(st,3);
  11579. y = script_getnum(st,4);
  11580. size = script_getnum(st,5);
  11581. dir = script_getnum(st,6);
  11582. shootable = script_getnum(st,7) != 0;
  11583. name = script_getstr(st,8);
  11584. if( (m = map_mapname2mapid(mapname)) < 0 )
  11585. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11586. map_iwall_set(m, x, y, size, dir, shootable, name);
  11587. return SCRIPT_CMD_SUCCESS;
  11588. }
  11589. BUILDIN_FUNC(delwall)
  11590. {
  11591. const char *name = script_getstr(st,2);
  11592. if( !map_iwall_remove(name) ){
  11593. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  11594. return SCRIPT_CMD_FAILURE;
  11595. }
  11596. return SCRIPT_CMD_SUCCESS;
  11597. }
  11598. /// Retrieves various information about the specified guardian.
  11599. ///
  11600. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11601. /// type: 0 - whether it is deployed or not
  11602. /// 1 - maximum hp
  11603. /// 2 - current hp
  11604. ///
  11605. BUILDIN_FUNC(guardianinfo)
  11606. {
  11607. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11608. int id = script_getnum(st,3);
  11609. int type = script_getnum(st,4);
  11610. struct guild_castle* gc = guild_mapname2gc(mapname);
  11611. struct mob_data* gd;
  11612. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11613. {
  11614. script_pushint(st,-1);
  11615. return SCRIPT_CMD_SUCCESS;
  11616. }
  11617. if( type == 0 )
  11618. script_pushint(st, gc->guardian[id].visible);
  11619. else
  11620. if( !gc->guardian[id].visible )
  11621. script_pushint(st,-1);
  11622. else
  11623. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11624. script_pushint(st,-1);
  11625. else
  11626. {
  11627. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11628. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11629. else
  11630. script_pushint(st,-1);
  11631. }
  11632. return SCRIPT_CMD_SUCCESS;
  11633. }
  11634. /*==========================================
  11635. * Get the item name by item_id or null
  11636. *------------------------------------------*/
  11637. BUILDIN_FUNC(getitemname)
  11638. {
  11639. unsigned short item_id = 0;
  11640. struct item_data *i_data;
  11641. char *item_name;
  11642. struct script_data *data;
  11643. data=script_getdata(st,2);
  11644. get_val(st,data);
  11645. if( data_isstring(data) ){
  11646. const char *name=conv_str(st,data);
  11647. struct item_data *item_data = itemdb_searchname(name);
  11648. if( item_data )
  11649. item_id=item_data->nameid;
  11650. }else
  11651. item_id=conv_num(st,data);
  11652. i_data = itemdb_exists(item_id);
  11653. if (i_data == NULL)
  11654. {
  11655. script_pushconststr(st,"null");
  11656. return SCRIPT_CMD_SUCCESS;
  11657. }
  11658. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11659. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11660. script_pushstr(st,item_name);
  11661. return SCRIPT_CMD_SUCCESS;
  11662. }
  11663. /*==========================================
  11664. * Returns number of slots an item has. [Skotlex]
  11665. *------------------------------------------*/
  11666. BUILDIN_FUNC(getitemslots)
  11667. {
  11668. unsigned short item_id;
  11669. struct item_data *i_data;
  11670. item_id=script_getnum(st,2);
  11671. i_data = itemdb_exists(item_id);
  11672. if (i_data)
  11673. script_pushint(st,i_data->slot);
  11674. else
  11675. script_pushint(st,-1);
  11676. return SCRIPT_CMD_SUCCESS;
  11677. }
  11678. // TODO: add matk here if needed/once we get rid of RENEWAL
  11679. /*==========================================
  11680. * Returns some values of an item [Lupus]
  11681. * Price, Weight, etc...
  11682. getiteminfo(itemID,n), where n
  11683. 0 value_buy;
  11684. 1 value_sell;
  11685. 2 type;
  11686. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11687. if = 0, then monsters don't drop it at all (rare or a quest item)
  11688. if = -1, then this item is sold in NPC shops only
  11689. 4 sex;
  11690. 5 equip;
  11691. 6 weight;
  11692. 7 atk;
  11693. 8 def;
  11694. 9 range;
  11695. 10 slot;
  11696. 11 look;
  11697. 12 elv;
  11698. 13 wlv;
  11699. 14 view id
  11700. 15 eLvmax
  11701. 16 matk (renewal)
  11702. *------------------------------------------*/
  11703. BUILDIN_FUNC(getiteminfo)
  11704. {
  11705. unsigned short item_id,n;
  11706. struct item_data *i_data;
  11707. item_id = script_getnum(st,2);
  11708. n = script_getnum(st,3);
  11709. i_data = itemdb_exists(item_id);
  11710. if (i_data && n <= 16) {
  11711. int *item_arr = (int*)&i_data->value_buy;
  11712. #ifndef RENEWAL
  11713. if (n == 16)
  11714. script_pushint(st,0);
  11715. else
  11716. #endif
  11717. script_pushint(st,item_arr[n]);
  11718. } else
  11719. script_pushint(st,-1);
  11720. return SCRIPT_CMD_SUCCESS;
  11721. }
  11722. /*==========================================
  11723. * Set some values of an item [Lupus]
  11724. * Price, Weight, etc...
  11725. setiteminfo(itemID,n,Value), where n
  11726. 0 value_buy;
  11727. 1 value_sell;
  11728. 2 type;
  11729. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11730. if = 0, then monsters don't drop it at all (rare or a quest item)
  11731. if = -1, then this item is sold in NPC shops only
  11732. 4 sex;
  11733. 5 equip;
  11734. 6 weight;
  11735. 7 atk;
  11736. 8 def;
  11737. 9 range;
  11738. 10 slot;
  11739. 11 look;
  11740. 12 elv;
  11741. 13 wlv;
  11742. 14 view id
  11743. * Returns Value or -1 if the wrong field's been set
  11744. *------------------------------------------*/
  11745. BUILDIN_FUNC(setiteminfo)
  11746. {
  11747. unsigned short item_id;
  11748. int n,value;
  11749. struct item_data *i_data;
  11750. item_id = script_getnum(st,2);
  11751. n = script_getnum(st,3);
  11752. value = script_getnum(st,4);
  11753. i_data = itemdb_exists(item_id);
  11754. if (i_data && n>=0 && n<=14) {
  11755. int *item_arr = (int*)&i_data->value_buy;
  11756. item_arr[n] = value;
  11757. script_pushint(st,value);
  11758. } else
  11759. script_pushint(st,-1);
  11760. return SCRIPT_CMD_SUCCESS;
  11761. }
  11762. /*==========================================
  11763. * Returns value from equipped item slot n [Lupus]
  11764. getequipcardid(num,slot)
  11765. where
  11766. num = eqip position slot
  11767. slot = 0,1,2,3 (Card Slot N)
  11768. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11769. it's useful when you want to check item cards or if it's signed
  11770. Useful for such quests as "Sign this refined item with players name" etc
  11771. Hat[0] +4 -> Player's Hat[0] +4
  11772. *------------------------------------------*/
  11773. BUILDIN_FUNC(getequipcardid)
  11774. {
  11775. int i=-1,num,slot;
  11776. TBL_PC *sd;
  11777. if( !script_rid2sd(sd) )
  11778. return SCRIPT_CMD_SUCCESS;
  11779. num=script_getnum(st,2);
  11780. slot=script_getnum(st,3);
  11781. if (equip_index_check(num))
  11782. i=pc_checkequip(sd,equip_bitmask[num]);
  11783. if(i >= 0 && slot>=0 && slot<4)
  11784. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11785. else
  11786. script_pushint(st,0);
  11787. return SCRIPT_CMD_SUCCESS;
  11788. }
  11789. /*==========================================
  11790. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11791. *------------------------------------------*/
  11792. BUILDIN_FUNC(petskillbonus)
  11793. {
  11794. struct pet_data *pd;
  11795. TBL_PC *sd;
  11796. if(!script_rid2sd(sd) || sd->pd == NULL)
  11797. return SCRIPT_CMD_FAILURE;
  11798. pd = sd->pd;
  11799. if (pd->bonus)
  11800. { //Clear previous bonus
  11801. if (pd->bonus->timer != INVALID_TIMER)
  11802. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11803. } else //init
  11804. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11805. pd->bonus->type = script_getnum(st,2);
  11806. pd->bonus->val = script_getnum(st,3);
  11807. pd->bonus->duration = script_getnum(st,4);
  11808. pd->bonus->delay = script_getnum(st,5);
  11809. if (pd->state.skillbonus == 1)
  11810. pd->state.skillbonus = 0; // waiting state
  11811. // wait for timer to start
  11812. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11813. pd->bonus->timer = INVALID_TIMER;
  11814. else
  11815. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11816. return SCRIPT_CMD_SUCCESS;
  11817. }
  11818. /*==========================================
  11819. * pet looting [Valaris] //Rewritten by [Skotlex]
  11820. *------------------------------------------*/
  11821. BUILDIN_FUNC(petloot)
  11822. {
  11823. int max;
  11824. struct pet_data *pd;
  11825. TBL_PC *sd;
  11826. if(!script_rid2sd(sd) || sd->pd==NULL)
  11827. return SCRIPT_CMD_SUCCESS;
  11828. max=script_getnum(st,2);
  11829. if(max < 1)
  11830. max = 1; //Let'em loot at least 1 item.
  11831. else if (max > MAX_PETLOOT_SIZE)
  11832. max = MAX_PETLOOT_SIZE;
  11833. pd = sd->pd;
  11834. if (pd->loot != NULL)
  11835. { //Release whatever was there already and reallocate memory
  11836. pet_lootitem_drop(pd, pd->master);
  11837. aFree(pd->loot->item);
  11838. }
  11839. else
  11840. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11841. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11842. pd->loot->max=max;
  11843. pd->loot->count = 0;
  11844. pd->loot->weight = 0;
  11845. return SCRIPT_CMD_SUCCESS;
  11846. }
  11847. /*==========================================
  11848. * Set arrays with info of all sd inventory :
  11849. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11850. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11851. * @inventorylist_card(0..3), @inventorylist_expire
  11852. * @inventorylist_count = scalar
  11853. *------------------------------------------*/
  11854. BUILDIN_FUNC(getinventorylist)
  11855. {
  11856. TBL_PC *sd;
  11857. char card_var[NAME_LENGTH], randopt_var[50];
  11858. int i,j=0,k;
  11859. if (!script_charid2sd(2,sd))
  11860. return SCRIPT_CMD_FAILURE;
  11861. for(i=0;i<MAX_INVENTORY;i++){
  11862. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  11863. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  11864. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  11865. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  11866. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  11867. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  11868. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  11869. for (k = 0; k < MAX_SLOTS; k++)
  11870. {
  11871. sprintf(card_var, "@inventorylist_card%d",k+1);
  11872. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  11873. }
  11874. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  11875. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  11876. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  11877. {
  11878. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  11879. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  11880. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  11881. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  11882. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  11883. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  11884. }
  11885. j++;
  11886. }
  11887. }
  11888. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11889. return SCRIPT_CMD_SUCCESS;
  11890. }
  11891. /**
  11892. * getskilllist ({<char_id>});
  11893. **/
  11894. BUILDIN_FUNC(getskilllist)
  11895. {
  11896. TBL_PC *sd;
  11897. int i,j=0;
  11898. if (!script_charid2sd(2,sd))
  11899. return SCRIPT_CMD_FAILURE;
  11900. for(i=0;i<MAX_SKILL;i++){
  11901. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11902. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11903. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11904. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11905. j++;
  11906. }
  11907. }
  11908. pc_setreg(sd,add_str("@skilllist_count"),j);
  11909. return SCRIPT_CMD_SUCCESS;
  11910. }
  11911. /**
  11912. * clearitem ({<char_id>});
  11913. **/
  11914. BUILDIN_FUNC(clearitem)
  11915. {
  11916. TBL_PC *sd;
  11917. int i;
  11918. if (!script_charid2sd(2,sd))
  11919. return SCRIPT_CMD_FAILURE;
  11920. for (i=0; i<MAX_INVENTORY; i++) {
  11921. if (sd->inventory.u.items_inventory[i].amount) {
  11922. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11923. }
  11924. }
  11925. return SCRIPT_CMD_SUCCESS;
  11926. }
  11927. /**
  11928. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11929. * disguise <Monster ID>{,<char_id>};
  11930. **/
  11931. BUILDIN_FUNC(disguise)
  11932. {
  11933. int id;
  11934. TBL_PC* sd;
  11935. if (!script_charid2sd(3,sd))
  11936. return SCRIPT_CMD_FAILURE;
  11937. id = script_getnum(st,2);
  11938. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11939. pc_disguise(sd, id);
  11940. script_pushint(st,id);
  11941. } else
  11942. script_pushint(st,0);
  11943. return SCRIPT_CMD_SUCCESS;
  11944. }
  11945. /**
  11946. * Undisguise Player (returns 1 if success, 0 on fail)
  11947. * undisguise {<char_id>};
  11948. **/
  11949. BUILDIN_FUNC(undisguise)
  11950. {
  11951. TBL_PC* sd;
  11952. if (!script_charid2sd(2,sd))
  11953. return SCRIPT_CMD_FAILURE;
  11954. if (sd->disguise) {
  11955. pc_disguise(sd, 0);
  11956. script_pushint(st,0);
  11957. } else {
  11958. script_pushint(st,1);
  11959. }
  11960. return SCRIPT_CMD_SUCCESS;
  11961. }
  11962. /**
  11963. * Transform an NPC to another _class
  11964. *
  11965. * classchange(<view id>{,"<NPC name>","<flag>"});
  11966. * @param flag: Specify target
  11967. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  11968. * BC_SELF - Sprite is sent only to player attached.
  11969. */
  11970. BUILDIN_FUNC(classchange)
  11971. {
  11972. int _class, type = 1;
  11973. struct npc_data* nd = NULL;
  11974. TBL_PC *sd = map_id2sd(st->rid);
  11975. send_target target = AREA;
  11976. _class = script_getnum(st,2);
  11977. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  11978. nd = npc_name2id(script_getstr(st, 3));
  11979. else
  11980. nd = (struct npc_data *)map_id2bl(st->oid);
  11981. if (nd == NULL)
  11982. return SCRIPT_CMD_FAILURE;
  11983. if (script_hasdata(st, 4)) {
  11984. switch(script_getnum(st, 4)) {
  11985. case BC_SELF: target = SELF; break;
  11986. case BC_AREA:
  11987. default: target = AREA; break;
  11988. }
  11989. }
  11990. if (target != SELF)
  11991. clif_class_change(&nd->bl,_class,type);
  11992. else if (sd == NULL)
  11993. return SCRIPT_CMD_FAILURE;
  11994. else
  11995. clif_class_change_target(&nd->bl,_class,type,target,sd);
  11996. return SCRIPT_CMD_SUCCESS;
  11997. }
  11998. /*==========================================
  11999. * Display an effect
  12000. *------------------------------------------*/
  12001. BUILDIN_FUNC(misceffect)
  12002. {
  12003. int type;
  12004. type=script_getnum(st,2);
  12005. if( type <= EF_NONE || type >= EF_MAX ){
  12006. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12007. return SCRIPT_CMD_FAILURE;
  12008. }
  12009. if(st->oid && st->oid != fake_nd->bl.id) {
  12010. struct block_list *bl = map_id2bl(st->oid);
  12011. if (bl)
  12012. clif_specialeffect(bl,type,AREA);
  12013. } else{
  12014. TBL_PC *sd;
  12015. if(script_rid2sd(sd))
  12016. clif_specialeffect(&sd->bl,type,AREA);
  12017. }
  12018. return SCRIPT_CMD_SUCCESS;
  12019. }
  12020. /*==========================================
  12021. * Play a BGM on a single client [Rikter/Yommy]
  12022. *------------------------------------------*/
  12023. BUILDIN_FUNC(playBGM)
  12024. {
  12025. struct map_session_data* sd;
  12026. if( script_rid2sd(sd) ) {
  12027. clif_playBGM(sd, script_getstr(st,2));
  12028. }
  12029. return SCRIPT_CMD_SUCCESS;
  12030. }
  12031. static int playBGM_sub(struct block_list* bl,va_list ap)
  12032. {
  12033. const char* name = va_arg(ap,const char*);
  12034. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12035. return 0;
  12036. }
  12037. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12038. {
  12039. const char* name = va_arg(args, const char*);
  12040. clif_playBGM(sd, name);
  12041. return 0;
  12042. }
  12043. /*==========================================
  12044. * Play a BGM on multiple client [Rikter/Yommy]
  12045. *------------------------------------------*/
  12046. BUILDIN_FUNC(playBGMall)
  12047. {
  12048. const char* name;
  12049. name = script_getstr(st,2);
  12050. if( script_hasdata(st,7) ) {// specified part of map
  12051. const char* mapname = script_getstr(st,3);
  12052. int x0 = script_getnum(st,4);
  12053. int y0 = script_getnum(st,5);
  12054. int x1 = script_getnum(st,6);
  12055. int y1 = script_getnum(st,7);
  12056. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12057. }
  12058. else if( script_hasdata(st,3) ) {// entire map
  12059. const char* mapname = script_getstr(st,3);
  12060. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12061. }
  12062. else {// entire server
  12063. map_foreachpc(&playBGM_foreachpc_sub, name);
  12064. }
  12065. return SCRIPT_CMD_SUCCESS;
  12066. }
  12067. /*==========================================
  12068. * Play a .wav sound for sd
  12069. *------------------------------------------*/
  12070. BUILDIN_FUNC(soundeffect)
  12071. {
  12072. TBL_PC* sd;
  12073. if(script_rid2sd(sd)){
  12074. const char* name = script_getstr(st,2);
  12075. int type = script_getnum(st,3);
  12076. clif_soundeffect(sd,&sd->bl,name,type);
  12077. }
  12078. return SCRIPT_CMD_SUCCESS;
  12079. }
  12080. int soundeffect_sub(struct block_list* bl,va_list ap)
  12081. {
  12082. char* name = va_arg(ap,char*);
  12083. int type = va_arg(ap,int);
  12084. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12085. return 0;
  12086. }
  12087. /*==========================================
  12088. * Play a sound effect (.wav) on multiple clients
  12089. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12090. *------------------------------------------*/
  12091. BUILDIN_FUNC(soundeffectall)
  12092. {
  12093. struct block_list* bl;
  12094. struct map_session_data* sd;
  12095. const char* name;
  12096. int type;
  12097. if( st->rid && script_rid2sd(sd) )
  12098. bl = &sd->bl;
  12099. else
  12100. bl = map_id2bl(st->oid);
  12101. if (!bl)
  12102. return SCRIPT_CMD_SUCCESS;
  12103. name = script_getstr(st,2);
  12104. type = script_getnum(st,3);
  12105. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12106. if(!script_hasdata(st,4))
  12107. { // area around
  12108. clif_soundeffectall(bl, name, type, AREA);
  12109. }
  12110. else
  12111. if(!script_hasdata(st,5))
  12112. { // entire map
  12113. const char* mapname = script_getstr(st,4);
  12114. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12115. }
  12116. else
  12117. if(script_hasdata(st,8))
  12118. { // specified part of map
  12119. const char* mapname = script_getstr(st,4);
  12120. int x0 = script_getnum(st,5);
  12121. int y0 = script_getnum(st,6);
  12122. int x1 = script_getnum(st,7);
  12123. int y1 = script_getnum(st,8);
  12124. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12125. }
  12126. else
  12127. {
  12128. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12129. }
  12130. return SCRIPT_CMD_SUCCESS;
  12131. }
  12132. /*==========================================
  12133. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12134. *------------------------------------------*/
  12135. BUILDIN_FUNC(petrecovery)
  12136. {
  12137. struct pet_data *pd;
  12138. TBL_PC *sd;
  12139. int sc;
  12140. if(!script_rid2sd(sd) || sd->pd == NULL)
  12141. return SCRIPT_CMD_FAILURE;
  12142. sc = script_getnum(st,2);
  12143. if (sc <= SC_NONE || sc >= SC_MAX) {
  12144. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12145. return SCRIPT_CMD_FAILURE;
  12146. }
  12147. pd = sd->pd;
  12148. if (pd->recovery)
  12149. { //Halt previous bonus
  12150. if (pd->recovery->timer != INVALID_TIMER)
  12151. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12152. } else //Init
  12153. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12154. pd->recovery->type = (sc_type)sc;
  12155. pd->recovery->delay = script_getnum(st,3);
  12156. pd->recovery->timer = INVALID_TIMER;
  12157. return SCRIPT_CMD_SUCCESS;
  12158. }
  12159. /*==========================================
  12160. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12161. *------------------------------------------*/
  12162. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12163. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12164. BUILDIN_FUNC(petskillattack)
  12165. {
  12166. struct pet_data *pd;
  12167. struct script_data *data;
  12168. TBL_PC *sd;
  12169. int id = 0;
  12170. if(!script_rid2sd(sd) || sd->pd == NULL)
  12171. return SCRIPT_CMD_FAILURE;
  12172. data = script_getdata(st, 2);
  12173. get_val(st, data);
  12174. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12175. if (!id) {
  12176. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12177. return SCRIPT_CMD_FAILURE;
  12178. }
  12179. pd = sd->pd;
  12180. if (pd->a_skill == NULL)
  12181. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12182. pd->a_skill->id = id;
  12183. pd->a_skill->damage = 0;
  12184. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12185. pd->a_skill->div_ = 0;
  12186. pd->a_skill->rate = script_getnum(st,4);
  12187. pd->a_skill->bonusrate = script_getnum(st,5);
  12188. return SCRIPT_CMD_SUCCESS;
  12189. }
  12190. /*==========================================
  12191. * pet attack skills [Valaris]
  12192. *------------------------------------------*/
  12193. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12194. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12195. BUILDIN_FUNC(petskillattack2)
  12196. {
  12197. struct pet_data *pd;
  12198. struct script_data *data;
  12199. TBL_PC *sd;
  12200. int id = 0;
  12201. if(!script_rid2sd(sd) || sd->pd == NULL)
  12202. return SCRIPT_CMD_FAILURE;
  12203. data = script_getdata(st, 2);
  12204. get_val(st, data);
  12205. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12206. if (!id) {
  12207. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12208. return SCRIPT_CMD_FAILURE;
  12209. }
  12210. pd = sd->pd;
  12211. if (pd->a_skill == NULL)
  12212. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12213. pd->a_skill->id = id;
  12214. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12215. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12216. pd->a_skill->div_ = script_getnum(st,4);
  12217. pd->a_skill->rate = script_getnum(st,5);
  12218. pd->a_skill->bonusrate = script_getnum(st,6);
  12219. return SCRIPT_CMD_SUCCESS;
  12220. }
  12221. /*==========================================
  12222. * pet support skills [Skotlex]
  12223. *------------------------------------------*/
  12224. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12225. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12226. BUILDIN_FUNC(petskillsupport)
  12227. {
  12228. struct pet_data *pd;
  12229. struct script_data *data;
  12230. TBL_PC *sd;
  12231. int id = 0;
  12232. if(!script_rid2sd(sd) || sd->pd == NULL)
  12233. return SCRIPT_CMD_FAILURE;
  12234. data = script_getdata(st, 2);
  12235. get_val(st, data);
  12236. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12237. if (!id) {
  12238. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12239. return SCRIPT_CMD_FAILURE;
  12240. }
  12241. pd = sd->pd;
  12242. if (pd->s_skill)
  12243. { //Clear previous skill
  12244. if (pd->s_skill->timer != INVALID_TIMER)
  12245. {
  12246. if (pd->s_skill->id)
  12247. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12248. else
  12249. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12250. }
  12251. } else //init memory
  12252. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12253. pd->s_skill->id = id;
  12254. pd->s_skill->lv = script_getnum(st,3);
  12255. pd->s_skill->delay = script_getnum(st,4);
  12256. pd->s_skill->hp = script_getnum(st,5);
  12257. pd->s_skill->sp = script_getnum(st,6);
  12258. //Use delay as initial offset to avoid skill/heal exploits
  12259. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12260. pd->s_skill->timer = INVALID_TIMER;
  12261. else
  12262. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12263. return SCRIPT_CMD_SUCCESS;
  12264. }
  12265. /*==========================================
  12266. * Scripted skill effects [Celest]
  12267. *------------------------------------------*/
  12268. /// skilleffect <skill id>,<level>
  12269. /// skilleffect "<skill name>",<level>
  12270. BUILDIN_FUNC(skilleffect)
  12271. {
  12272. TBL_PC *sd;
  12273. uint16 skill_id, skill_lv;
  12274. struct script_data *data;
  12275. if( !script_rid2sd(sd) )
  12276. return SCRIPT_CMD_SUCCESS;
  12277. data = script_getdata(st, 2);
  12278. get_val(st, data); // Convert into value in case of a variable
  12279. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12280. skill_lv = script_getnum(st,3);
  12281. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12282. * which leaves the server thinking it still is sitting. */
  12283. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12284. skill_sit(sd, 0);
  12285. clif_standing(&sd->bl);
  12286. }
  12287. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12288. return SCRIPT_CMD_SUCCESS;
  12289. }
  12290. /*==========================================
  12291. * NPC skill effects [Valaris]
  12292. *------------------------------------------*/
  12293. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12294. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12295. BUILDIN_FUNC(npcskilleffect)
  12296. {
  12297. struct block_list *bl= map_id2bl(st->oid);
  12298. uint16 skill_id, skill_lv;
  12299. int x, y;
  12300. struct script_data *data = script_getdata(st, 2);
  12301. get_val(st, data); // Convert into value in case of a variable
  12302. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12303. skill_lv=script_getnum(st,3);
  12304. x=script_getnum(st,4);
  12305. y=script_getnum(st,5);
  12306. if (bl)
  12307. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12308. return SCRIPT_CMD_SUCCESS;
  12309. }
  12310. /*==========================================
  12311. * Special effects [Valaris]
  12312. *------------------------------------------*/
  12313. BUILDIN_FUNC(specialeffect)
  12314. {
  12315. struct block_list *bl=map_id2bl(st->oid);
  12316. int type = script_getnum(st,2);
  12317. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12318. if(bl==NULL)
  12319. return SCRIPT_CMD_SUCCESS;
  12320. if( type <= EF_NONE || type >= EF_MAX ){
  12321. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  12322. return SCRIPT_CMD_FAILURE;
  12323. }
  12324. if( script_hasdata(st,4) )
  12325. {
  12326. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12327. if(nd)
  12328. clif_specialeffect(&nd->bl, type, target);
  12329. }
  12330. else
  12331. {
  12332. if (target == SELF) {
  12333. TBL_PC *sd;
  12334. if (script_rid2sd(sd))
  12335. clif_specialeffect_single(bl,type,sd->fd);
  12336. } else {
  12337. clif_specialeffect(bl, type, target);
  12338. }
  12339. }
  12340. return SCRIPT_CMD_SUCCESS;
  12341. }
  12342. BUILDIN_FUNC(specialeffect2)
  12343. {
  12344. TBL_PC *sd;
  12345. if( script_nick2sd(4,sd) ){
  12346. int type = script_getnum(st,2);
  12347. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12348. if( type <= EF_NONE || type >= EF_MAX ){
  12349. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  12350. return SCRIPT_CMD_FAILURE;
  12351. }
  12352. clif_specialeffect(&sd->bl, type, target);
  12353. }
  12354. return SCRIPT_CMD_SUCCESS;
  12355. }
  12356. /**
  12357. * nude({<char_id>});
  12358. * @author [Valaris]
  12359. **/
  12360. BUILDIN_FUNC(nude)
  12361. {
  12362. TBL_PC *sd;
  12363. int i, calcflag = 0;
  12364. if (!script_charid2sd(2,sd))
  12365. return SCRIPT_CMD_FAILURE;
  12366. for( i = 0 ; i < EQI_MAX; i++ ) {
  12367. if( sd->equip_index[ i ] >= 0 ) {
  12368. if( !calcflag )
  12369. calcflag = 1;
  12370. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12371. }
  12372. }
  12373. if( calcflag )
  12374. status_calc_pc(sd,SCO_NONE);
  12375. return SCRIPT_CMD_SUCCESS;
  12376. }
  12377. int atcommand_sub(struct script_state* st,int type) {
  12378. TBL_PC *sd, dummy_sd;
  12379. int fd;
  12380. const char *cmd;
  12381. cmd = script_getstr(st,2);
  12382. if (st->rid) {
  12383. if( !script_rid2sd(sd) )
  12384. return SCRIPT_CMD_SUCCESS;
  12385. fd = sd->fd;
  12386. } else { //Use a dummy character.
  12387. sd = &dummy_sd;
  12388. fd = 0;
  12389. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12390. if (st->oid) {
  12391. struct block_list* bl = map_id2bl(st->oid);
  12392. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12393. if (bl->type == BL_NPC)
  12394. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12395. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12396. }
  12397. // Init Group ID, Level, & permissions
  12398. sd->group_id = sd->group_level = 99;
  12399. sd->permissions |= PC_PERM_ALLPERMISSION;
  12400. }
  12401. if (!is_atcommand(fd, sd, cmd, type)) {
  12402. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12403. script_reportsrc(st);
  12404. return SCRIPT_CMD_FAILURE;
  12405. }
  12406. return SCRIPT_CMD_SUCCESS;
  12407. }
  12408. /*==========================================
  12409. * gmcommand [MouseJstr]
  12410. *------------------------------------------*/
  12411. BUILDIN_FUNC(atcommand) {
  12412. return atcommand_sub(st,0);
  12413. }
  12414. /** Displays a message for the player only (like system messages like "you got an apple" )
  12415. * dispbottom("<message>"{,<color>{,<char_id>}})
  12416. * @param message
  12417. * @param color Hex color default (Green)
  12418. */
  12419. BUILDIN_FUNC(dispbottom)
  12420. {
  12421. TBL_PC *sd;
  12422. int color = 0;
  12423. const char *message;
  12424. if (!script_charid2sd(4,sd))
  12425. return SCRIPT_CMD_FAILURE;
  12426. message = script_getstr(st,2);
  12427. if (script_hasdata(st,3))
  12428. color = script_getnum(st,3); // <color>
  12429. if(sd) {
  12430. if (script_hasdata(st,3))
  12431. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12432. else
  12433. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12434. }
  12435. return SCRIPT_CMD_SUCCESS;
  12436. }
  12437. /*===================================
  12438. * Heal portion of recovery command
  12439. *-----------------------------------*/
  12440. int recovery_sub(struct map_session_data* sd, int revive)
  12441. {
  12442. if(revive&(1|4) && pc_isdead(sd)) {
  12443. status_revive(&sd->bl, 100, 100);
  12444. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12445. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  12446. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12447. status_percent_heal(&sd->bl, 100, 100);
  12448. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12449. }
  12450. return SCRIPT_CMD_SUCCESS;
  12451. }
  12452. /*=========================================================================
  12453. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12454. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12455. * <type> determines <option>:
  12456. * 0 : char_id
  12457. * 1 : party_id
  12458. * 2 : guild_id
  12459. * 3 : map_name
  12460. * 4 : all characters
  12461. * <revive_flag>:
  12462. * 1 : Revive and heal all players (default)
  12463. * 2 : Heal living players only
  12464. * 4 : Revive dead players only
  12465. * <map name>:
  12466. * for types 1-2 : map_name (null = all maps)
  12467. *-------------------------------------------------------------------------*/
  12468. BUILDIN_FUNC(recovery)
  12469. {
  12470. TBL_PC *sd;
  12471. int map_idx = 0, type = 0, revive = 1;
  12472. type = script_getnum(st,2);
  12473. if(script_hasdata(st,4))
  12474. revive = script_getnum(st,4);
  12475. switch(type) {
  12476. case 0:
  12477. if(!script_charid2sd(3,sd))
  12478. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12479. recovery_sub(sd, revive);
  12480. break;
  12481. case 1:
  12482. {
  12483. struct party_data* p;
  12484. //When no party given, we use invoker party
  12485. int p_id = 0, i;
  12486. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12487. map_idx = map_mapname2mapid(script_getstr(st,5));
  12488. if(map_idx < 1) { //But we'll check anyways
  12489. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12490. return SCRIPT_CMD_FAILURE;
  12491. }
  12492. }
  12493. if(script_hasdata(st,3))
  12494. p_id = script_getnum(st,3);
  12495. else if(script_rid2sd(sd))
  12496. p_id = sd->status.party_id;
  12497. p = party_search(p_id);
  12498. if(p == NULL)
  12499. return SCRIPT_CMD_SUCCESS;
  12500. for (i = 0; i < MAX_PARTY; i++) {
  12501. struct map_session_data* pl_sd;
  12502. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12503. || (map_idx && pl_sd->bl.m != map_idx))
  12504. continue;
  12505. recovery_sub(pl_sd, revive);
  12506. }
  12507. break;
  12508. }
  12509. case 2:
  12510. {
  12511. struct guild* g;
  12512. //When no guild given, we use invoker guild
  12513. int g_id = 0, i;
  12514. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12515. map_idx = map_mapname2mapid(script_getstr(st,5));
  12516. if(map_idx < 1) { //But we'll check anyways
  12517. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12518. return SCRIPT_CMD_FAILURE;
  12519. }
  12520. }
  12521. if(script_hasdata(st,3))
  12522. g_id = script_getnum(st,3);
  12523. else if(script_rid2sd(sd))
  12524. g_id = sd->status.guild_id;
  12525. g = guild_search(g_id);
  12526. if(g == NULL)
  12527. return SCRIPT_CMD_SUCCESS;
  12528. for (i = 0; i < MAX_GUILD; i++) {
  12529. struct map_session_data* pl_sd;
  12530. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12531. || (map_idx && pl_sd->bl.m != map_idx))
  12532. continue;
  12533. recovery_sub(pl_sd, revive);
  12534. }
  12535. break;
  12536. }
  12537. case 3:
  12538. if(script_hasdata(st,3))
  12539. map_idx = map_mapname2mapid(script_getstr(st,3));
  12540. else if(script_rid2sd(sd))
  12541. map_idx = sd->bl.m;
  12542. if(map_idx < 1)
  12543. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12544. case 4:
  12545. {
  12546. struct s_mapiterator *iter;
  12547. struct script_data *data = script_getdata(st, 3);
  12548. get_val(st, data); // Convert into value in case of a variable
  12549. if(script_hasdata(st,3) && !data_isstring(data))
  12550. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12551. iter = mapit_getallusers();
  12552. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12553. if(type == 3 && sd->bl.m != map_idx)
  12554. continue;
  12555. recovery_sub(sd, revive);
  12556. }
  12557. mapit_free(iter);
  12558. break;
  12559. }
  12560. default:
  12561. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12562. script_pushint(st,-1);
  12563. return SCRIPT_CMD_FAILURE;
  12564. }
  12565. script_pushint(st,1); //Successfully executed without errors
  12566. return SCRIPT_CMD_SUCCESS;
  12567. }
  12568. /*==========================================
  12569. * Get your pet info: getpetinfo <type>{,<char_id>}
  12570. * n -> 0:pet_id 1:pet_class 2:pet_name
  12571. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12572. * 7:game id
  12573. *------------------------------------------*/
  12574. BUILDIN_FUNC(getpetinfo)
  12575. {
  12576. TBL_PC *sd;
  12577. TBL_PET *pd;
  12578. int type = script_getnum(st,2);
  12579. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12580. if (type == 2)
  12581. script_pushconststr(st,"null");
  12582. else
  12583. script_pushint(st,0);
  12584. return SCRIPT_CMD_SUCCESS;
  12585. }
  12586. switch(type){
  12587. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12588. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12589. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12590. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12591. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12592. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12593. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12594. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12595. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  12596. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  12597. default:
  12598. script_pushint(st,0);
  12599. break;
  12600. }
  12601. return SCRIPT_CMD_SUCCESS;
  12602. }
  12603. /*==========================================
  12604. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12605. * type -> 0:hom_id 1:class 2:name
  12606. * 3:friendly 4:hungry, 5: rename flag.
  12607. * 6: level, 7: game id
  12608. *------------------------------------------*/
  12609. BUILDIN_FUNC(gethominfo)
  12610. {
  12611. TBL_PC *sd;
  12612. TBL_HOM *hd;
  12613. int type=script_getnum(st,2);
  12614. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12615. if (type == 2)
  12616. script_pushconststr(st,"null");
  12617. else
  12618. script_pushint(st,0);
  12619. return SCRIPT_CMD_SUCCESS;
  12620. }
  12621. switch(type){
  12622. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12623. case 1: script_pushint(st,hd->homunculus.class_); break;
  12624. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12625. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12626. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12627. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12628. case 6: script_pushint(st,hd->homunculus.level); break;
  12629. case 7: script_pushint(st,hd->bl.id); break;
  12630. default:
  12631. script_pushint(st,0);
  12632. break;
  12633. }
  12634. return SCRIPT_CMD_SUCCESS;
  12635. }
  12636. /// Retrieves information about character's mercenary
  12637. /// getmercinfo <type>[,<char id>];
  12638. BUILDIN_FUNC(getmercinfo)
  12639. {
  12640. int type;
  12641. struct map_session_data* sd;
  12642. struct mercenary_data* md;
  12643. if( !script_charid2sd(3,sd) ){
  12644. script_pushnil(st);
  12645. return SCRIPT_CMD_FAILURE;
  12646. }
  12647. type = script_getnum(st,2);
  12648. md = sd->md;
  12649. if( md == NULL ){
  12650. if( type == 2 )
  12651. script_pushconststr(st,"");
  12652. else
  12653. script_pushint(st,0);
  12654. return SCRIPT_CMD_SUCCESS;
  12655. }
  12656. switch( type )
  12657. {
  12658. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  12659. case 1: script_pushint(st,md->mercenary.class_); break;
  12660. case 2: script_pushstrcopy(st,md->db->name); break;
  12661. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  12662. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  12663. case 5: script_pushint(st,md->mercenary.kill_count); break;
  12664. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  12665. case 7: script_pushint(st,md->db->lv); break;
  12666. case 8: script_pushint(st,md->bl.id); break;
  12667. default:
  12668. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12669. script_pushnil(st);
  12670. return SCRIPT_CMD_FAILURE;
  12671. }
  12672. return SCRIPT_CMD_SUCCESS;
  12673. }
  12674. /*==========================================
  12675. * Shows wether your inventory(and equips) contain
  12676. selected card or not.
  12677. checkequipedcard(4001);
  12678. *------------------------------------------*/
  12679. BUILDIN_FUNC(checkequipedcard)
  12680. {
  12681. TBL_PC *sd;
  12682. if(script_rid2sd(sd)){
  12683. int n,i,c=0;
  12684. c=script_getnum(st,2);
  12685. for(i=0;i<MAX_INVENTORY;i++){
  12686. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12687. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12688. continue;
  12689. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12690. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12691. script_pushint(st,1);
  12692. return SCRIPT_CMD_SUCCESS;
  12693. }
  12694. }
  12695. }
  12696. }
  12697. }
  12698. script_pushint(st,0);
  12699. return SCRIPT_CMD_SUCCESS;
  12700. }
  12701. BUILDIN_FUNC(jump_zero)
  12702. {
  12703. int sel;
  12704. sel=script_getnum(st,2);
  12705. if(!sel) {
  12706. int pos;
  12707. if( !data_islabel(script_getdata(st,3)) ){
  12708. ShowError("script: jump_zero: not label !\n");
  12709. st->state=END;
  12710. return SCRIPT_CMD_FAILURE;
  12711. }
  12712. pos=script_getnum(st,3);
  12713. st->pos=pos;
  12714. st->state=GOTO;
  12715. }
  12716. return SCRIPT_CMD_SUCCESS;
  12717. }
  12718. /*==========================================
  12719. * movenpc [MouseJstr]
  12720. *------------------------------------------*/
  12721. BUILDIN_FUNC(movenpc)
  12722. {
  12723. TBL_NPC *nd = NULL;
  12724. const char *npc;
  12725. int x,y;
  12726. npc = script_getstr(st,2);
  12727. x = script_getnum(st,3);
  12728. y = script_getnum(st,4);
  12729. if ((nd = npc_name2id(npc)) == NULL){
  12730. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12731. return -1;
  12732. }
  12733. if (script_hasdata(st,5))
  12734. nd->ud.dir = script_getnum(st,5) % 8;
  12735. npc_movenpc(nd, x, y);
  12736. return SCRIPT_CMD_SUCCESS;
  12737. }
  12738. /*==========================================
  12739. * message [MouseJstr]
  12740. *------------------------------------------*/
  12741. BUILDIN_FUNC(message)
  12742. {
  12743. const char *msg,*player;
  12744. TBL_PC *pl_sd = NULL;
  12745. player = script_getstr(st,2);
  12746. msg = script_getstr(st,3);
  12747. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  12748. return SCRIPT_CMD_SUCCESS;
  12749. clif_displaymessage(pl_sd->fd, msg);
  12750. return SCRIPT_CMD_SUCCESS;
  12751. }
  12752. /**
  12753. * npctalk("<message>"{,"<NPC name>","<flag>"});
  12754. * @param flag: Specify target
  12755. * BC_ALL - Broadcast message is sent server-wide.
  12756. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  12757. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  12758. * BC_SELF - Message is sent only to player attached.
  12759. */
  12760. BUILDIN_FUNC(npctalk)
  12761. {
  12762. struct npc_data* nd = NULL;
  12763. const char* str = script_getstr(st,2);
  12764. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12765. nd = npc_name2id(script_getstr(st, 3));
  12766. else
  12767. nd = (struct npc_data *)map_id2bl(st->oid);
  12768. if (nd != NULL) {
  12769. send_target target = AREA;
  12770. char message[CHAT_SIZE_MAX];
  12771. if (script_hasdata(st, 4)) {
  12772. switch(script_getnum(st, 4)) {
  12773. case BC_ALL: target = ALL_CLIENT; break;
  12774. case BC_MAP: target = ALL_SAMEMAP; break;
  12775. case BC_SELF: target = SELF; break;
  12776. case BC_AREA:
  12777. default: target = AREA; break;
  12778. }
  12779. }
  12780. safesnprintf(message, sizeof(message), "%s", str);
  12781. if (target != SELF)
  12782. clif_messagecolor(&nd->bl, color_table[COLOR_WHITE], message, false, target);
  12783. else {
  12784. TBL_PC *sd = map_id2sd(st->rid);
  12785. if (sd == NULL)
  12786. return SCRIPT_CMD_FAILURE;
  12787. clif_messagecolor_target(&nd->bl, color_table[COLOR_WHITE], message, false, target, sd);
  12788. }
  12789. }
  12790. return SCRIPT_CMD_SUCCESS;
  12791. }
  12792. /**
  12793. * Sends a message to the waitingroom of the invoking NPC.
  12794. * chatmes "<message>"{,"<NPC name>"};
  12795. * @author Jey
  12796. */
  12797. BUILDIN_FUNC(chatmes)
  12798. {
  12799. struct npc_data* nd = NULL;
  12800. const char* str = script_getstr(st,2);
  12801. if (script_hasdata(st, 3))
  12802. nd = npc_name2id(script_getstr(st, 3));
  12803. else
  12804. nd = (struct npc_data *)map_id2bl(st->oid);
  12805. if (nd != NULL && nd->chat_id) {
  12806. char message[256];
  12807. safesnprintf(message, sizeof(message), "%s", str);
  12808. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  12809. }
  12810. return SCRIPT_CMD_SUCCESS;
  12811. }
  12812. // change npc walkspeed [Valaris]
  12813. BUILDIN_FUNC(npcspeed)
  12814. {
  12815. struct npc_data* nd;
  12816. int speed;
  12817. speed = script_getnum(st,2);
  12818. nd =(struct npc_data *)map_id2bl(st->oid);
  12819. if( nd ) {
  12820. nd->speed = speed;
  12821. nd->ud.state.speed_changed = 1;
  12822. }
  12823. return SCRIPT_CMD_SUCCESS;
  12824. }
  12825. // make an npc walk to a position [Valaris]
  12826. BUILDIN_FUNC(npcwalkto)
  12827. {
  12828. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12829. int x=0,y=0;
  12830. x=script_getnum(st,2);
  12831. y=script_getnum(st,3);
  12832. if(nd) {
  12833. if (!nd->status.hp)
  12834. status_calc_npc(nd, SCO_FIRST);
  12835. else
  12836. status_calc_npc(nd, SCO_NONE);
  12837. unit_walktoxy(&nd->bl,x,y,0);
  12838. }
  12839. return SCRIPT_CMD_SUCCESS;
  12840. }
  12841. // stop an npc's movement [Valaris]
  12842. BUILDIN_FUNC(npcstop)
  12843. {
  12844. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12845. if(nd) {
  12846. unit_stop_walking(&nd->bl,1|4);
  12847. }
  12848. return SCRIPT_CMD_SUCCESS;
  12849. }
  12850. /**
  12851. * getlook(<type>{,<char_id>})
  12852. **/
  12853. BUILDIN_FUNC(getlook)
  12854. {
  12855. int type,val;
  12856. TBL_PC *sd;
  12857. if (!script_charid2sd(3,sd)) {
  12858. script_pushint(st,-1);
  12859. return SCRIPT_CMD_FAILURE;
  12860. }
  12861. type=script_getnum(st,2);
  12862. val=-1;
  12863. switch(type) {
  12864. // TODO: implement LOOK_BASE as stated in script doc
  12865. case LOOK_HAIR: val=sd->status.hair; break; //1
  12866. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12867. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12868. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12869. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12870. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12871. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12872. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12873. case LOOK_SHOES: break; //9
  12874. case LOOK_ROBE: val=sd->status.robe; break; //12
  12875. case LOOK_BODY2: val=sd->status.body; break; //13
  12876. }
  12877. script_pushint(st,val);
  12878. return SCRIPT_CMD_SUCCESS;
  12879. }
  12880. /**
  12881. * getsavepoint(<information type>{,<char_id>})
  12882. * @param type 0- map name, 1- x, 2- y
  12883. **/
  12884. BUILDIN_FUNC(getsavepoint)
  12885. {
  12886. TBL_PC* sd;
  12887. int type;
  12888. if (!script_charid2sd(3,sd)) {
  12889. script_pushint(st,0);
  12890. return SCRIPT_CMD_SUCCESS;
  12891. }
  12892. type = script_getnum(st,2);
  12893. switch(type) {
  12894. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12895. case 1: script_pushint(st,sd->status.save_point.x); break;
  12896. case 2: script_pushint(st,sd->status.save_point.y); break;
  12897. default:
  12898. script_pushint(st,0);
  12899. break;
  12900. }
  12901. return SCRIPT_CMD_SUCCESS;
  12902. }
  12903. /**
  12904. * Get position for BL objects.
  12905. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12906. * @param mapname: String variable for output map name
  12907. * @param x: Integer variable for output coord X
  12908. * @param y: Integer variable for output coord Y
  12909. * @param type: Type of object
  12910. * UNITTYPE_PC - Character coord
  12911. * UNITTYPE_NPC - NPC coord
  12912. * UNITTYPE_PET - Pet coord
  12913. * UNITTYPE_HOM - Homun coord
  12914. * UNITTYPE_MER - Mercenary coord
  12915. * UNITTYPE_ELEM - Elemental coord
  12916. * @param charname: Name object. If empty or "this" use the current object
  12917. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12918. */
  12919. BUILDIN_FUNC(getmapxy)
  12920. {
  12921. struct block_list *bl = NULL;
  12922. TBL_PC *sd=NULL;
  12923. int64 num;
  12924. const char *name;
  12925. char prefix;
  12926. int x,y,type;
  12927. char mapname[MAP_NAME_LENGTH];
  12928. if( !data_isreference(script_getdata(st,2)) ) {
  12929. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12930. script_pushint(st,-1);
  12931. return SCRIPT_CMD_FAILURE;
  12932. }
  12933. if( !data_isreference(script_getdata(st,3)) ) {
  12934. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12935. script_pushint(st,-1);
  12936. return SCRIPT_CMD_FAILURE;
  12937. }
  12938. if( !data_isreference(script_getdata(st,4)) ) {
  12939. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12940. script_pushint(st,-1);
  12941. return SCRIPT_CMD_FAILURE;
  12942. }
  12943. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12944. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12945. script_pushint(st,-1);
  12946. return SCRIPT_CMD_FAILURE;
  12947. }
  12948. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12949. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12950. script_pushint(st,-1);
  12951. return SCRIPT_CMD_FAILURE;
  12952. }
  12953. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12954. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12955. script_pushint(st,-1);
  12956. return SCRIPT_CMD_FAILURE;
  12957. }
  12958. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12959. type=script_getnum(st,5);
  12960. switch (type) {
  12961. case UNITTYPE_PC: //Get Character Position
  12962. if( script_nick2sd(6,sd) )
  12963. bl = &sd->bl;
  12964. break;
  12965. case UNITTYPE_NPC: //Get NPC Position
  12966. if( script_hasdata(st,6) )
  12967. {
  12968. struct npc_data *nd;
  12969. nd=npc_name2id(script_getstr(st,6));
  12970. if (nd)
  12971. bl = &nd->bl;
  12972. } else //In case the origin is not an npc?
  12973. bl=map_id2bl(st->oid);
  12974. break;
  12975. case UNITTYPE_PET: //Get Pet Position
  12976. if( script_nick2sd(6, sd) && sd->pd )
  12977. bl = &sd->pd->bl;
  12978. break;
  12979. case UNITTYPE_HOM: //Get Homun Position
  12980. if( script_nick2sd(6, sd) && sd->hd )
  12981. bl = &sd->hd->bl;
  12982. break;
  12983. case UNITTYPE_MER: //Get Mercenary Position
  12984. if( script_nick2sd(6, sd) && sd->md )
  12985. bl = &sd->md->bl;
  12986. break;
  12987. case UNITTYPE_ELEM: //Get Elemental Position
  12988. if( script_nick2sd(6, sd) && sd->ed )
  12989. bl = &sd->ed->bl;
  12990. break;
  12991. default:
  12992. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12993. script_pushint(st,-1);
  12994. return SCRIPT_CMD_FAILURE;
  12995. }
  12996. if (!bl) { //No object found.
  12997. script_pushint(st,-1);
  12998. return SCRIPT_CMD_SUCCESS;
  12999. }
  13000. x= bl->x;
  13001. y= bl->y;
  13002. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  13003. //Set MapName$
  13004. num=st->stack->stack_data[st->start+2].u.num;
  13005. name=get_str(script_getvarid(num));
  13006. prefix=*name;
  13007. if(not_server_variable(prefix)){
  13008. if( !script_rid2sd(sd) ){
  13009. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13010. return SCRIPT_CMD_FAILURE;
  13011. }
  13012. }else
  13013. sd=NULL;
  13014. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  13015. //Set MapX
  13016. num=st->stack->stack_data[st->start+3].u.num;
  13017. name=get_str(script_getvarid(num));
  13018. prefix=*name;
  13019. if(not_server_variable(prefix)){
  13020. if( !script_rid2sd(sd) ){
  13021. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13022. return SCRIPT_CMD_FAILURE;
  13023. }
  13024. }else
  13025. sd=NULL;
  13026. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  13027. //Set MapY
  13028. num=st->stack->stack_data[st->start+4].u.num;
  13029. name=get_str(script_getvarid(num));
  13030. prefix=*name;
  13031. if(not_server_variable(prefix)){
  13032. if( !script_rid2sd(sd) ){
  13033. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13034. return SCRIPT_CMD_FAILURE;
  13035. }
  13036. }else
  13037. sd=NULL;
  13038. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  13039. //Return Success value
  13040. script_pushint(st,0);
  13041. return SCRIPT_CMD_SUCCESS;
  13042. }
  13043. /// Returns the map name of given map ID.
  13044. ///
  13045. /// mapid2name <map ID>;
  13046. BUILDIN_FUNC(mapid2name)
  13047. {
  13048. uint16 m = script_getnum(st, 2);
  13049. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  13050. script_pushconststr(st, "");
  13051. return SCRIPT_CMD_FAILURE;
  13052. }
  13053. script_pushstrcopy(st, map_mapid2mapname(m));
  13054. return SCRIPT_CMD_SUCCESS;
  13055. }
  13056. /*==========================================
  13057. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13058. *------------------------------------------*/
  13059. BUILDIN_FUNC(logmes)
  13060. {
  13061. const char *str;
  13062. struct map_session_data* sd = map_id2sd(st->rid);
  13063. str = script_getstr(st,2);
  13064. if( sd ){
  13065. log_npc(sd,str);
  13066. }else{
  13067. struct npc_data* nd = map_id2nd(st->oid);
  13068. if( !nd ){
  13069. ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
  13070. return SCRIPT_CMD_FAILURE;
  13071. }
  13072. log_npc(nd,str);
  13073. }
  13074. return SCRIPT_CMD_SUCCESS;
  13075. }
  13076. BUILDIN_FUNC(summon)
  13077. {
  13078. int _class, timeout=0;
  13079. const char *str,*event="";
  13080. TBL_PC *sd;
  13081. struct mob_data *md;
  13082. int tick = gettick();
  13083. if (!script_rid2sd(sd))
  13084. return SCRIPT_CMD_SUCCESS;
  13085. str =script_getstr(st,2);
  13086. _class=script_getnum(st,3);
  13087. if( script_hasdata(st,4) )
  13088. timeout=script_getnum(st,4);
  13089. if( script_hasdata(st,5) ){
  13090. event=script_getstr(st,5);
  13091. check_event(st, event);
  13092. }
  13093. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13094. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13095. if (md) {
  13096. md->master_id=sd->bl.id;
  13097. md->special_state.ai = AI_ATTACK;
  13098. if( md->deletetimer != INVALID_TIMER )
  13099. delete_timer(md->deletetimer, mob_timer_delete);
  13100. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13101. mob_spawn (md); //Now it is ready for spawning.
  13102. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  13103. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13104. }
  13105. script_pushint(st, md->bl.id);
  13106. return SCRIPT_CMD_SUCCESS;
  13107. }
  13108. /*==========================================
  13109. * Checks whether it is daytime/nighttime
  13110. *------------------------------------------*/
  13111. BUILDIN_FUNC(isnight)
  13112. {
  13113. script_pushint(st,(night_flag == 1));
  13114. return SCRIPT_CMD_SUCCESS;
  13115. }
  13116. BUILDIN_FUNC(isday)
  13117. {
  13118. script_pushint(st,(night_flag == 0));
  13119. return SCRIPT_CMD_SUCCESS;
  13120. }
  13121. /*================================================
  13122. * Check how many items/cards in the list are
  13123. * equipped - used for 2/15's cards patch [celest]
  13124. *------------------------------------------------*/
  13125. BUILDIN_FUNC(isequippedcnt)
  13126. {
  13127. TBL_PC *sd;
  13128. int i, id = 1;
  13129. int ret = 0;
  13130. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13131. script_pushint(st,0);
  13132. return SCRIPT_CMD_SUCCESS;
  13133. }
  13134. for (i=0; id!=0; i++) {
  13135. short j;
  13136. FETCH (i+2, id) else id = 0;
  13137. if (id <= 0)
  13138. continue;
  13139. for (j=0; j<EQI_MAX; j++) {
  13140. short index = sd->equip_index[j];
  13141. if(index < 0)
  13142. continue;
  13143. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13144. continue;
  13145. if(!sd->inventory_data[index])
  13146. continue;
  13147. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13148. if (sd->inventory_data[index]->nameid == id)
  13149. ret+= sd->inventory.u.items_inventory[index].amount;
  13150. } else { //Count cards.
  13151. short k;
  13152. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13153. continue; //No cards
  13154. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13155. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13156. ret++; //[Lupus]
  13157. }
  13158. }
  13159. }
  13160. }
  13161. script_pushint(st,ret);
  13162. return SCRIPT_CMD_SUCCESS;
  13163. }
  13164. /*================================================
  13165. * Check whether another card has been
  13166. * equipped - used for 2/15's cards patch [celest]
  13167. * -- Items checked cannot be reused in another
  13168. * card set to prevent exploits
  13169. *------------------------------------------------*/
  13170. BUILDIN_FUNC(isequipped)
  13171. {
  13172. TBL_PC *sd;
  13173. int i, id = 1;
  13174. int ret = -1;
  13175. //Original hash to reverse it when full check fails.
  13176. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13177. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13178. script_pushint(st,0);
  13179. return SCRIPT_CMD_SUCCESS;
  13180. }
  13181. setitem_hash = sd->bonus.setitem_hash;
  13182. setitem_hash2 = sd->bonus.setitem_hash2;
  13183. for (i=0; id!=0; i++) {
  13184. int flag = 0;
  13185. short j;
  13186. FETCH (i+2, id) else id = 0;
  13187. if (id <= 0)
  13188. continue;
  13189. for (j=0; j<EQI_MAX; j++) {
  13190. short index = sd->equip_index[j];
  13191. if(index < 0)
  13192. continue;
  13193. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13194. continue;
  13195. if(!sd->inventory_data[index])
  13196. continue;
  13197. if (itemdb_type(id) != IT_CARD) {
  13198. if (sd->inventory_data[index]->nameid != id)
  13199. continue;
  13200. flag = 1;
  13201. break;
  13202. } else { //Cards
  13203. short k;
  13204. if (sd->inventory_data[index]->slot == 0 ||
  13205. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13206. continue;
  13207. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13208. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13209. unsigned int hash = 0;
  13210. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13211. continue;
  13212. hash = 1<<((j<5?j:j-5)*4 + k);
  13213. // check if card is already used by another set
  13214. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13215. continue;
  13216. // We have found a match
  13217. flag = 1;
  13218. // Set hash so this card cannot be used by another
  13219. if (j<5)
  13220. sd->bonus.setitem_hash |= hash;
  13221. else
  13222. sd->bonus.setitem_hash2 |= hash;
  13223. break;
  13224. }
  13225. }
  13226. if (flag) break; //Card found
  13227. }
  13228. if (ret == -1)
  13229. ret = flag;
  13230. else
  13231. ret &= flag;
  13232. if (!ret) break;
  13233. }
  13234. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13235. sd->bonus.setitem_hash = setitem_hash;
  13236. sd->bonus.setitem_hash2 = setitem_hash2;
  13237. }
  13238. script_pushint(st,ret);
  13239. return SCRIPT_CMD_SUCCESS;
  13240. }
  13241. /*================================================
  13242. * Check how many given inserted cards in the CURRENT
  13243. * weapon - used for 2/15's cards patch [Lupus]
  13244. *------------------------------------------------*/
  13245. BUILDIN_FUNC(cardscnt)
  13246. {
  13247. TBL_PC *sd;
  13248. int i, k, id = 1;
  13249. int ret = 0;
  13250. int index;
  13251. if( !script_rid2sd(sd) ){
  13252. script_pushint(st,0);
  13253. return SCRIPT_CMD_SUCCESS;
  13254. }
  13255. for (i=0; id!=0; i++) {
  13256. FETCH (i+2, id) else id = 0;
  13257. if (id <= 0)
  13258. continue;
  13259. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13260. if(index < 0) continue;
  13261. if(!sd->inventory_data[index])
  13262. continue;
  13263. if(itemdb_type(id) != IT_CARD) {
  13264. if (sd->inventory_data[index]->nameid == id)
  13265. ret+= sd->inventory.u.items_inventory[index].amount;
  13266. } else {
  13267. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13268. continue;
  13269. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13270. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13271. ret++;
  13272. }
  13273. }
  13274. }
  13275. script_pushint(st,ret);
  13276. // script_pushint(st,current_equip_item_index);
  13277. return SCRIPT_CMD_SUCCESS;
  13278. }
  13279. /*=======================================================
  13280. * Returns the refined number of the current item, or an
  13281. * item with inventory index specified
  13282. *-------------------------------------------------------*/
  13283. BUILDIN_FUNC(getrefine)
  13284. {
  13285. TBL_PC *sd;
  13286. if (script_rid2sd(sd)){
  13287. if( current_equip_item_index == -1 ){
  13288. script_pushint(st, 0);
  13289. return SCRIPT_CMD_FAILURE;
  13290. }
  13291. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13292. }else
  13293. script_pushint(st,0);
  13294. return SCRIPT_CMD_SUCCESS;
  13295. }
  13296. /*=======================================================
  13297. * Day/Night controls
  13298. *-------------------------------------------------------*/
  13299. BUILDIN_FUNC(night)
  13300. {
  13301. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13302. return SCRIPT_CMD_SUCCESS;
  13303. }
  13304. BUILDIN_FUNC(day)
  13305. {
  13306. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13307. return SCRIPT_CMD_SUCCESS;
  13308. }
  13309. /**
  13310. * unequip <equipment slot>{,<char_id>};
  13311. * @author [Spectre]
  13312. **/
  13313. BUILDIN_FUNC(unequip) {
  13314. int pos;
  13315. TBL_PC *sd;
  13316. if (!script_charid2sd(3,sd))
  13317. return SCRIPT_CMD_FAILURE;
  13318. pos = script_getnum(st,2);
  13319. if (equip_index_check(pos)) {
  13320. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13321. if (i >= 0) {
  13322. pc_unequipitem(sd,i,1|2);
  13323. script_pushint(st, 1);
  13324. return SCRIPT_CMD_SUCCESS;
  13325. }
  13326. }
  13327. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13328. script_pushint(st, 0);
  13329. return SCRIPT_CMD_FAILURE;
  13330. }
  13331. /**
  13332. * equip <item id>{,<char_id>};
  13333. **/
  13334. BUILDIN_FUNC(equip) {
  13335. unsigned short nameid = 0;
  13336. TBL_PC *sd;
  13337. struct item_data *item_data;
  13338. if (!script_charid2sd(3,sd))
  13339. return SCRIPT_CMD_FAILURE;
  13340. nameid = script_getnum(st,2);
  13341. if ((item_data = itemdb_exists(nameid))) {
  13342. int i;
  13343. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13344. if (i < MAX_INVENTORY) {
  13345. pc_equipitem(sd,i,item_data->equip);
  13346. script_pushint(st,1);
  13347. return SCRIPT_CMD_SUCCESS;
  13348. }
  13349. }
  13350. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13351. script_pushint(st,0);
  13352. return SCRIPT_CMD_FAILURE;
  13353. }
  13354. BUILDIN_FUNC(autoequip)
  13355. {
  13356. unsigned short nameid;
  13357. int flag;
  13358. struct item_data *item_data;
  13359. nameid=script_getnum(st,2);
  13360. flag=script_getnum(st,3);
  13361. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13362. {
  13363. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13364. return SCRIPT_CMD_FAILURE;
  13365. }
  13366. if( !itemdb_isequip2(item_data) )
  13367. {
  13368. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13369. return SCRIPT_CMD_FAILURE;
  13370. }
  13371. item_data->flag.autoequip = flag>0?1:0;
  13372. return SCRIPT_CMD_SUCCESS;
  13373. }
  13374. /**
  13375. * Set the value of a given battle config
  13376. * setbattleflag "<battle flag>",<value>{,<reload>};
  13377. */
  13378. BUILDIN_FUNC(setbattleflag)
  13379. {
  13380. const char *flag, *value;
  13381. flag = script_getstr(st,2);
  13382. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13383. if (battle_set_value(flag, value) == 0)
  13384. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13385. else {
  13386. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13387. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  13388. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  13389. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  13390. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  13391. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  13392. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  13393. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  13394. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  13395. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  13396. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  13397. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  13398. };
  13399. uint8 i;
  13400. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  13401. if (!strcmpi(flag, expdrop_flags[i])) {
  13402. mob_reload();
  13403. break;
  13404. }
  13405. }
  13406. }
  13407. }
  13408. return SCRIPT_CMD_SUCCESS;
  13409. }
  13410. /**
  13411. * Get the value of a given battle config
  13412. * getbattleflag("<battle flag>")
  13413. */
  13414. BUILDIN_FUNC(getbattleflag)
  13415. {
  13416. const char *flag = script_getstr(st,2);
  13417. script_pushint(st,battle_get_value(flag));
  13418. return SCRIPT_CMD_SUCCESS;
  13419. }
  13420. //=======================================================
  13421. // strlen [Valaris]
  13422. //-------------------------------------------------------
  13423. BUILDIN_FUNC(getstrlen)
  13424. {
  13425. const char *str = script_getstr(st,2);
  13426. int len = (str) ? (int)strlen(str) : 0;
  13427. script_pushint(st,len);
  13428. return SCRIPT_CMD_SUCCESS;
  13429. }
  13430. //=======================================================
  13431. // isalpha [Valaris]
  13432. //-------------------------------------------------------
  13433. BUILDIN_FUNC(charisalpha)
  13434. {
  13435. const char *str=script_getstr(st,2);
  13436. int pos=script_getnum(st,3);
  13437. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13438. script_pushint(st,val);
  13439. return SCRIPT_CMD_SUCCESS;
  13440. }
  13441. //=======================================================
  13442. // charisupper <str>, <index>
  13443. //-------------------------------------------------------
  13444. BUILDIN_FUNC(charisupper)
  13445. {
  13446. const char *str = script_getstr(st,2);
  13447. int pos = script_getnum(st,3);
  13448. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13449. script_pushint(st,val);
  13450. return SCRIPT_CMD_SUCCESS;
  13451. }
  13452. //=======================================================
  13453. // charislower <str>, <index>
  13454. //-------------------------------------------------------
  13455. BUILDIN_FUNC(charislower)
  13456. {
  13457. const char *str = script_getstr(st,2);
  13458. int pos = script_getnum(st,3);
  13459. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13460. script_pushint(st,val);
  13461. return SCRIPT_CMD_SUCCESS;
  13462. }
  13463. //=======================================================
  13464. // charat <str>, <index>
  13465. //-------------------------------------------------------
  13466. BUILDIN_FUNC(charat) {
  13467. const char *str = script_getstr(st,2);
  13468. int pos = script_getnum(st,3);
  13469. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13470. char output[2];
  13471. output[0] = str[pos];
  13472. output[1] = '\0';
  13473. script_pushstrcopy(st, output);
  13474. } else
  13475. script_pushconststr(st, "");
  13476. return SCRIPT_CMD_SUCCESS;
  13477. }
  13478. //=======================================================
  13479. // setchar <string>, <char>, <index>
  13480. //-------------------------------------------------------
  13481. BUILDIN_FUNC(setchar)
  13482. {
  13483. const char *str = script_getstr(st,2);
  13484. const char *c = script_getstr(st,3);
  13485. int index = script_getnum(st,4);
  13486. char *output = aStrdup(str);
  13487. if(index >= 0 && index < strlen(output))
  13488. output[index] = *c;
  13489. script_pushstr(st, output);
  13490. return SCRIPT_CMD_SUCCESS;
  13491. }
  13492. //=======================================================
  13493. // insertchar <string>, <char>, <index>
  13494. //-------------------------------------------------------
  13495. BUILDIN_FUNC(insertchar)
  13496. {
  13497. const char *str = script_getstr(st,2);
  13498. const char *c = script_getstr(st,3);
  13499. int index = script_getnum(st,4);
  13500. char *output;
  13501. size_t len = strlen(str);
  13502. if(index < 0)
  13503. index = 0;
  13504. else if(index > len)
  13505. index = len;
  13506. output = (char*)aMalloc(len + 2);
  13507. memcpy(output, str, index);
  13508. output[index] = c[0];
  13509. memcpy(&output[index+1], &str[index], len - index);
  13510. output[len+1] = '\0';
  13511. script_pushstr(st, output);
  13512. return SCRIPT_CMD_SUCCESS;
  13513. }
  13514. //=======================================================
  13515. // delchar <string>, <index>
  13516. //-------------------------------------------------------
  13517. BUILDIN_FUNC(delchar)
  13518. {
  13519. const char *str = script_getstr(st,2);
  13520. int index = script_getnum(st,3);
  13521. char *output;
  13522. size_t len = strlen(str);
  13523. if(index < 0 || index > len) {
  13524. //return original
  13525. output = aStrdup(str);
  13526. script_pushstr(st, output);
  13527. return SCRIPT_CMD_SUCCESS;
  13528. }
  13529. output = (char*)aMalloc(len);
  13530. memcpy(output, str, index);
  13531. memcpy(&output[index], &str[index+1], len - index);
  13532. script_pushstr(st, output);
  13533. return SCRIPT_CMD_SUCCESS;
  13534. }
  13535. //=======================================================
  13536. // strtoupper <str>
  13537. //-------------------------------------------------------
  13538. BUILDIN_FUNC(strtoupper)
  13539. {
  13540. const char *str = script_getstr(st,2);
  13541. char *output = aStrdup(str);
  13542. char *cursor = output;
  13543. while (*cursor != '\0') {
  13544. *cursor = TOUPPER(*cursor);
  13545. cursor++;
  13546. }
  13547. script_pushstr(st, output);
  13548. return SCRIPT_CMD_SUCCESS;
  13549. }
  13550. //=======================================================
  13551. // strtolower <str>
  13552. //-------------------------------------------------------
  13553. BUILDIN_FUNC(strtolower)
  13554. {
  13555. const char *str = script_getstr(st,2);
  13556. char *output = aStrdup(str);
  13557. char *cursor = output;
  13558. while (*cursor != '\0') {
  13559. *cursor = TOLOWER(*cursor);
  13560. cursor++;
  13561. }
  13562. script_pushstr(st, output);
  13563. return SCRIPT_CMD_SUCCESS;
  13564. }
  13565. //=======================================================
  13566. // substr <str>, <start>, <end>
  13567. //-------------------------------------------------------
  13568. BUILDIN_FUNC(substr)
  13569. {
  13570. const char *str = script_getstr(st,2);
  13571. char *output;
  13572. int start = script_getnum(st,3);
  13573. int end = script_getnum(st,4);
  13574. int len = 0;
  13575. if(start >= 0 && end < strlen(str) && start <= end) {
  13576. len = end - start + 1;
  13577. output = (char*)aMalloc(len + 1);
  13578. memcpy(output, &str[start], len);
  13579. } else
  13580. output = (char*)aMalloc(1);
  13581. output[len] = '\0';
  13582. script_pushstr(st, output);
  13583. return SCRIPT_CMD_SUCCESS;
  13584. }
  13585. //=======================================================
  13586. // explode <dest_string_array>, <str>, <delimiter>
  13587. // Note: delimiter is limited to 1 char
  13588. //-------------------------------------------------------
  13589. BUILDIN_FUNC(explode)
  13590. {
  13591. struct script_data* data = script_getdata(st, 2);
  13592. const char *str = script_getstr(st,3);
  13593. const char delimiter = script_getstr(st, 4)[0];
  13594. int32 id;
  13595. size_t len = strlen(str);
  13596. int i = 0, j = 0;
  13597. int start;
  13598. char *temp;
  13599. const char* name;
  13600. TBL_PC* sd = NULL;
  13601. temp = (char*)aMalloc(len + 1);
  13602. if( !data_isreference(data) ) {
  13603. ShowError("script:explode: not a variable\n");
  13604. script_reportdata(data);
  13605. st->state = END;
  13606. return SCRIPT_CMD_FAILURE;// not a variable
  13607. }
  13608. id = reference_getid(data);
  13609. start = reference_getindex(data);
  13610. name = reference_getname(data);
  13611. if( !is_string_variable(name) ) {
  13612. ShowError("script:explode: not string array\n");
  13613. script_reportdata(data);
  13614. st->state = END;
  13615. return SCRIPT_CMD_FAILURE;// data type mismatch
  13616. }
  13617. if( not_server_variable(*name) ) {
  13618. if( !script_rid2sd(sd) )
  13619. return SCRIPT_CMD_SUCCESS;// no player attached
  13620. }
  13621. while(str[i] != '\0') {
  13622. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13623. temp[j] = '\0';
  13624. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13625. j = 0;
  13626. ++i;
  13627. } else {
  13628. temp[j++] = str[i++];
  13629. }
  13630. }
  13631. //set last string
  13632. temp[j] = '\0';
  13633. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13634. aFree(temp);
  13635. return SCRIPT_CMD_SUCCESS;
  13636. }
  13637. //=======================================================
  13638. // implode <string_array>
  13639. // implode <string_array>, <glue>
  13640. //-------------------------------------------------------
  13641. BUILDIN_FUNC(implode)
  13642. {
  13643. struct script_data* data = script_getdata(st, 2);
  13644. const char *name;
  13645. uint32 glue_len = 0, array_size, id;
  13646. char *output;
  13647. TBL_PC* sd = NULL;
  13648. if( !data_isreference(data) ) {
  13649. ShowError("script:implode: not a variable\n");
  13650. script_reportdata(data);
  13651. st->state = END;
  13652. return SCRIPT_CMD_FAILURE;// not a variable
  13653. }
  13654. id = reference_getid(data);
  13655. name = reference_getname(data);
  13656. if( !is_string_variable(name) ) {
  13657. ShowError("script:implode: not string array\n");
  13658. script_reportdata(data);
  13659. st->state = END;
  13660. return SCRIPT_CMD_FAILURE;// data type mismatch
  13661. }
  13662. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  13663. return SCRIPT_CMD_SUCCESS;// no player attached
  13664. }
  13665. //count chars
  13666. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13667. if(array_size == -1) { //empty array check (AmsTaff)
  13668. ShowWarning("script:implode: array length = 0\n");
  13669. output = (char*)aMalloc(sizeof(char)*5);
  13670. sprintf(output,"%s","NULL");
  13671. } else {
  13672. const char *glue = NULL, *temp;
  13673. size_t len = 0;
  13674. int i, k = 0;
  13675. for(i = 0; i <= array_size; ++i) {
  13676. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13677. len += strlen(temp);
  13678. script_removetop(st, -1, 0);
  13679. }
  13680. //allocate mem
  13681. if( script_hasdata(st,3) ) {
  13682. glue = script_getstr(st,3);
  13683. glue_len = strlen(glue);
  13684. len += glue_len * (array_size);
  13685. }
  13686. output = (char*)aMalloc(len + 1);
  13687. //build output
  13688. for(i = 0; i < array_size; ++i) {
  13689. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13690. len = strlen(temp);
  13691. memcpy(&output[k], temp, len);
  13692. k += len;
  13693. if(glue_len != 0) {
  13694. memcpy(&output[k], glue, glue_len);
  13695. k += glue_len;
  13696. }
  13697. script_removetop(st, -1, 0);
  13698. }
  13699. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13700. len = strlen(temp);
  13701. memcpy(&output[k], temp, len);
  13702. k += len;
  13703. script_removetop(st, -1, 0);
  13704. output[k] = '\0';
  13705. }
  13706. script_pushstr(st, output);
  13707. return SCRIPT_CMD_SUCCESS;
  13708. }
  13709. //=======================================================
  13710. // sprintf(<format>, ...);
  13711. // Implements C sprintf, except format %n. The resulting string is
  13712. // returned, instead of being saved in variable by reference.
  13713. //-------------------------------------------------------
  13714. BUILDIN_FUNC(sprintf)
  13715. {
  13716. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13717. const char* format;
  13718. char* p;
  13719. char* q;
  13720. char* buf = NULL;
  13721. char* buf2 = NULL;
  13722. struct script_data* data;
  13723. StringBuf final_buf;
  13724. // Fetch init data
  13725. format = script_getstr(st, 2);
  13726. argc = script_lastdata(st)-2;
  13727. len = strlen(format);
  13728. // Skip parsing, where no parsing is required.
  13729. if(len == 0) {
  13730. script_pushconststr(st,"");
  13731. return SCRIPT_CMD_SUCCESS;
  13732. }
  13733. // Pessimistic alloc
  13734. CREATE(buf, char, len+1);
  13735. // Need not be parsed, just solve stuff like %%.
  13736. if(argc == 0) {
  13737. memcpy(buf,format,len+1);
  13738. script_pushstrcopy(st, buf);
  13739. aFree(buf);
  13740. return SCRIPT_CMD_SUCCESS;
  13741. }
  13742. safestrncpy(buf, format, len+1);
  13743. // Issue sprintf for each parameter
  13744. StringBuf_Init(&final_buf);
  13745. q = buf;
  13746. while((p = strchr(q, '%')) != NULL) {
  13747. if(p != q) {
  13748. len = p - q + 1;
  13749. if(buf2_len < len) {
  13750. RECREATE(buf2, char, len);
  13751. buf2_len = len;
  13752. }
  13753. safestrncpy(buf2, q, len);
  13754. StringBuf_AppendStr(&final_buf, buf2);
  13755. q = p;
  13756. }
  13757. p = q + 1;
  13758. if(*p == '%') { // %%
  13759. StringBuf_AppendStr(&final_buf, "%");
  13760. q+=2;
  13761. continue;
  13762. }
  13763. if(*p == 'n') { // %n
  13764. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13765. script_reportsrc(st);
  13766. q+=2;
  13767. continue;
  13768. }
  13769. if(arg >= argc) {
  13770. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13771. if(buf) aFree(buf);
  13772. if(buf2) aFree(buf2);
  13773. StringBuf_Destroy(&final_buf);
  13774. script_pushconststr(st,"");
  13775. return SCRIPT_CMD_FAILURE;
  13776. }
  13777. if((p = strchr(q+1, '%')) == NULL)
  13778. p = strchr(q, 0); // EOS
  13779. len = p - q + 1;
  13780. if(buf2_len < len) {
  13781. RECREATE(buf2, char, len);
  13782. buf2_len = len;
  13783. }
  13784. safestrncpy(buf2, q, len);
  13785. q = p;
  13786. // Note: This assumes the passed value being the correct
  13787. // type to the current format specifier. If not, the server
  13788. // probably crashes or returns anything else, than expected,
  13789. // but it would behave in normal code the same way so it's
  13790. // the scripter's responsibility.
  13791. data = script_getdata(st, arg+3);
  13792. if(data_isstring(data)) // String
  13793. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13794. else if(data_isint(data)) // Number
  13795. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13796. else if(data_isreference(data)) { // Variable
  13797. char* name = reference_getname(data);
  13798. if(name[strlen(name)-1]=='$') // var Str
  13799. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13800. else // var Int
  13801. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13802. } else { // Unsupported type
  13803. ShowError("buildin_sprintf: Unknown argument type!\n");
  13804. if(buf) aFree(buf);
  13805. if(buf2) aFree(buf2);
  13806. StringBuf_Destroy(&final_buf);
  13807. script_pushconststr(st,"");
  13808. return SCRIPT_CMD_FAILURE;
  13809. }
  13810. arg++;
  13811. }
  13812. // Append anything left
  13813. if(*q)
  13814. StringBuf_AppendStr(&final_buf, q);
  13815. // Passed more, than needed
  13816. if(arg < argc) {
  13817. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13818. script_reportsrc(st);
  13819. }
  13820. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13821. if(buf) aFree(buf);
  13822. if(buf2) aFree(buf2);
  13823. StringBuf_Destroy(&final_buf);
  13824. return SCRIPT_CMD_SUCCESS;
  13825. }
  13826. //=======================================================
  13827. // sscanf(<str>, <format>, ...);
  13828. // Implements C sscanf.
  13829. //-------------------------------------------------------
  13830. BUILDIN_FUNC(sscanf){
  13831. unsigned int argc, arg = 0, len;
  13832. struct script_data* data;
  13833. struct map_session_data* sd = NULL;
  13834. const char* str;
  13835. const char* format;
  13836. const char* p;
  13837. const char* q;
  13838. char* buf = NULL;
  13839. char* buf_p;
  13840. char* ref_str = NULL;
  13841. int ref_int;
  13842. // Get data
  13843. str = script_getstr(st, 2);
  13844. format = script_getstr(st, 3);
  13845. argc = script_lastdata(st)-3;
  13846. len = strlen(format);
  13847. if (len != 0 && strlen(str) == 0) {
  13848. // If the source string is empty but the format string is not, we return -1
  13849. // according to the C specs. (if the format string is also empty, we shall
  13850. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13851. script_pushint(st, -1);
  13852. return SCRIPT_CMD_SUCCESS;
  13853. }
  13854. CREATE(buf, char, len*2+1);
  13855. // Issue sscanf for each parameter
  13856. *buf = 0;
  13857. q = format;
  13858. while((p = strchr(q, '%'))){
  13859. if(p!=q){
  13860. strncat(buf, q, (size_t)(p-q));
  13861. q = p;
  13862. }
  13863. p = q+1;
  13864. if(*p=='*' || *p=='%'){ // Skip
  13865. strncat(buf, q, 2);
  13866. q+=2;
  13867. continue;
  13868. }
  13869. if(arg>=argc){
  13870. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13871. script_pushint(st, -1);
  13872. if(buf) aFree(buf);
  13873. if(ref_str) aFree(ref_str);
  13874. return SCRIPT_CMD_FAILURE;
  13875. }
  13876. if((p = strchr(q+1, '%'))==NULL){
  13877. p = strchr(q, 0); // EOS
  13878. }
  13879. len = p-q;
  13880. strncat(buf, q, len);
  13881. q = p;
  13882. // Validate output
  13883. data = script_getdata(st, arg+4);
  13884. if(!data_isreference(data) || !reference_tovariable(data)){
  13885. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13886. script_pushint(st, -1);
  13887. if(buf) aFree(buf);
  13888. if(ref_str) aFree(ref_str);
  13889. return SCRIPT_CMD_FAILURE;
  13890. }
  13891. buf_p = reference_getname(data);
  13892. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  13893. script_pushint(st, -1);
  13894. if(buf) aFree(buf);
  13895. if(ref_str) aFree(ref_str);
  13896. return SCRIPT_CMD_SUCCESS;
  13897. }
  13898. // Save value if any
  13899. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13900. if(ref_str==NULL){
  13901. CREATE(ref_str, char, strlen(str)+1);
  13902. }
  13903. if(sscanf(str, buf, ref_str)==0){
  13904. break;
  13905. }
  13906. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13907. } else { // Number
  13908. if(sscanf(str, buf, &ref_int)==0){
  13909. break;
  13910. }
  13911. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13912. }
  13913. arg++;
  13914. // Disable used format (%... -> %*...)
  13915. buf_p = strchr(buf, 0);
  13916. memmove(buf_p-len+2, buf_p-len+1, len);
  13917. *(buf_p-len+1) = '*';
  13918. }
  13919. script_pushint(st, arg);
  13920. if(buf) aFree(buf);
  13921. if(ref_str) aFree(ref_str);
  13922. return SCRIPT_CMD_SUCCESS;
  13923. }
  13924. //=======================================================
  13925. // strpos(<haystack>, <needle>)
  13926. // strpos(<haystack>, <needle>, <offset>)
  13927. //
  13928. // Implements PHP style strpos. Adapted from code from
  13929. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13930. //-------------------------------------------------------
  13931. BUILDIN_FUNC(strpos) {
  13932. const char *haystack = script_getstr(st,2);
  13933. const char *needle = script_getstr(st,3);
  13934. int i;
  13935. size_t len;
  13936. if( script_hasdata(st,4) )
  13937. i = script_getnum(st,4);
  13938. else
  13939. i = 0;
  13940. if (needle[0] == '\0') {
  13941. script_pushint(st, -1);
  13942. return SCRIPT_CMD_SUCCESS;
  13943. }
  13944. len = strlen(haystack);
  13945. for ( ; i < len; ++i ) {
  13946. if ( haystack[i] == *needle ) {
  13947. // matched starting char -- loop through remaining chars
  13948. const char *h, *n;
  13949. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13950. if ( *h != *n ) {
  13951. break;
  13952. }
  13953. }
  13954. if ( !*n ) { // matched all of 'needle' to null termination
  13955. script_pushint(st, i);
  13956. return SCRIPT_CMD_SUCCESS;
  13957. }
  13958. }
  13959. }
  13960. script_pushint(st, -1);
  13961. return SCRIPT_CMD_SUCCESS;
  13962. }
  13963. //===============================================================
  13964. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13965. //
  13966. // Note: Finds all instances of <search> in <input> and replaces
  13967. // with <replace>. If specified will only replace as many
  13968. // instances as specified in <count>. By default will be case
  13969. // sensitive.
  13970. //---------------------------------------------------------------
  13971. BUILDIN_FUNC(replacestr)
  13972. {
  13973. const char *input = script_getstr(st, 2);
  13974. const char *find = script_getstr(st, 3);
  13975. const char *replace = script_getstr(st, 4);
  13976. size_t inputlen = strlen(input);
  13977. size_t findlen = strlen(find);
  13978. struct StringBuf output;
  13979. bool usecase = true;
  13980. int count = 0;
  13981. int numFinds = 0;
  13982. int i = 0, f = 0;
  13983. if(findlen == 0) {
  13984. ShowError("script:replacestr: Invalid search length.\n");
  13985. st->state = END;
  13986. return SCRIPT_CMD_FAILURE;
  13987. }
  13988. if(script_hasdata(st, 5)) {
  13989. struct script_data *data = script_getdata(st, 5);
  13990. get_val(st, data); // Convert into value in case of a variable
  13991. if( !data_isstring(data) )
  13992. usecase = script_getnum(st, 5) != 0;
  13993. else {
  13994. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13995. st->state = END;
  13996. return SCRIPT_CMD_FAILURE;
  13997. }
  13998. }
  13999. if(script_hasdata(st, 6)) {
  14000. count = script_getnum(st, 6);
  14001. if(count == 0) {
  14002. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14003. st->state = END;
  14004. return SCRIPT_CMD_FAILURE;
  14005. }
  14006. }
  14007. StringBuf_Init(&output);
  14008. for(; i < inputlen; i++) {
  14009. if(count && count == numFinds) { //found enough, stop looking
  14010. break;
  14011. }
  14012. for(f = 0; f <= findlen; f++) {
  14013. if(f == findlen) { //complete match
  14014. numFinds++;
  14015. StringBuf_AppendStr(&output, replace);
  14016. i += findlen - 1;
  14017. break;
  14018. } else {
  14019. if(usecase) {
  14020. if((i + f) > inputlen || input[i + f] != find[f]) {
  14021. StringBuf_Printf(&output, "%c", input[i]);
  14022. break;
  14023. }
  14024. } else {
  14025. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14026. StringBuf_Printf(&output, "%c", input[i]);
  14027. break;
  14028. }
  14029. }
  14030. }
  14031. }
  14032. }
  14033. //append excess after enough found
  14034. if(i < inputlen)
  14035. StringBuf_AppendStr(&output, &(input[i]));
  14036. script_pushstrcopy(st, StringBuf_Value(&output));
  14037. StringBuf_Destroy(&output);
  14038. return SCRIPT_CMD_SUCCESS;
  14039. }
  14040. //========================================================
  14041. // countstr <input>, <search>{, <usecase>}
  14042. //
  14043. // Note: Counts the number of times <search> occurs in
  14044. // <input>. By default will be case sensitive.
  14045. //--------------------------------------------------------
  14046. BUILDIN_FUNC(countstr)
  14047. {
  14048. const char *input = script_getstr(st, 2);
  14049. const char *find = script_getstr(st, 3);
  14050. size_t inputlen = strlen(input);
  14051. size_t findlen = strlen(find);
  14052. bool usecase = true;
  14053. int numFinds = 0;
  14054. int i = 0, f = 0;
  14055. if(findlen == 0) {
  14056. ShowError("script:countstr: Invalid search length.\n");
  14057. st->state = END;
  14058. return SCRIPT_CMD_FAILURE;
  14059. }
  14060. if(script_hasdata(st, 4)) {
  14061. struct script_data *data;
  14062. data = script_getdata(st, 4);
  14063. get_val(st, data); // Convert into value in case of a variable
  14064. if( !data_isstring(data) )
  14065. usecase = script_getnum(st, 4) != 0;
  14066. else {
  14067. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14068. st->state = END;
  14069. return SCRIPT_CMD_FAILURE;
  14070. }
  14071. }
  14072. for(; i < inputlen; i++) {
  14073. for(f = 0; f <= findlen; f++) {
  14074. if(f == findlen) { //complete match
  14075. numFinds++;
  14076. i += findlen - 1;
  14077. break;
  14078. } else {
  14079. if(usecase) {
  14080. if((i + f) > inputlen || input[i + f] != find[f]) {
  14081. break;
  14082. }
  14083. } else {
  14084. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14085. break;
  14086. }
  14087. }
  14088. }
  14089. }
  14090. }
  14091. script_pushint(st, numFinds);
  14092. return SCRIPT_CMD_SUCCESS;
  14093. }
  14094. /// Changes the display name and/or display class of the npc.
  14095. /// Returns 0 is successful, 1 if the npc does not exist.
  14096. ///
  14097. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14098. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14099. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14100. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14101. BUILDIN_FUNC(setnpcdisplay)
  14102. {
  14103. const char* name;
  14104. const char* newname = NULL;
  14105. int class_ = JT_FAKENPC, size = -1;
  14106. struct script_data* data;
  14107. struct npc_data* nd;
  14108. name = script_getstr(st,2);
  14109. data = script_getdata(st,3);
  14110. if( script_hasdata(st,4) )
  14111. class_ = script_getnum(st,4);
  14112. if( script_hasdata(st,5) )
  14113. size = script_getnum(st,5);
  14114. get_val(st, data);
  14115. if( data_isstring(data) )
  14116. newname = conv_str(st,data);
  14117. else if( data_isint(data) )
  14118. class_ = conv_num(st,data);
  14119. else
  14120. {
  14121. ShowError("script:setnpcdisplay: expected string or number\n");
  14122. script_reportdata(data);
  14123. return SCRIPT_CMD_FAILURE;
  14124. }
  14125. nd = npc_name2id(name);
  14126. if( nd == NULL )
  14127. {// not found
  14128. script_pushint(st,1);
  14129. return SCRIPT_CMD_SUCCESS;
  14130. }
  14131. // update npc
  14132. if( newname )
  14133. npc_setdisplayname(nd, newname);
  14134. if( size != -1 && size != (int)nd->size )
  14135. nd->size = size;
  14136. else
  14137. size = -1;
  14138. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  14139. npc_setclass(nd, class_);
  14140. else if( size != -1 )
  14141. { // Required to update the visual size
  14142. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  14143. clif_spawn(&nd->bl);
  14144. }
  14145. script_pushint(st,0);
  14146. return SCRIPT_CMD_SUCCESS;
  14147. }
  14148. BUILDIN_FUNC(atoi)
  14149. {
  14150. const char *value;
  14151. value = script_getstr(st,2);
  14152. script_pushint(st,atoi(value));
  14153. return SCRIPT_CMD_SUCCESS;
  14154. }
  14155. BUILDIN_FUNC(axtoi)
  14156. {
  14157. const char *hex = script_getstr(st,2);
  14158. long value = strtol(hex, NULL, 16);
  14159. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14160. value = cap_value(value, INT_MIN, INT_MAX);
  14161. #endif
  14162. script_pushint(st, (int)value);
  14163. return SCRIPT_CMD_SUCCESS;
  14164. }
  14165. BUILDIN_FUNC(strtol)
  14166. {
  14167. const char *string = script_getstr(st, 2);
  14168. int base = script_getnum(st, 3);
  14169. long value = strtol(string, NULL, base);
  14170. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14171. value = cap_value(value, INT_MIN, INT_MAX);
  14172. #endif
  14173. script_pushint(st, (int)value);
  14174. return SCRIPT_CMD_SUCCESS;
  14175. }
  14176. // case-insensitive substring search [lordalfa]
  14177. BUILDIN_FUNC(compare)
  14178. {
  14179. const char *message;
  14180. const char *cmpstring;
  14181. message = script_getstr(st,2);
  14182. cmpstring = script_getstr(st,3);
  14183. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14184. return SCRIPT_CMD_SUCCESS;
  14185. }
  14186. BUILDIN_FUNC(strcmp)
  14187. {
  14188. const char *str1;
  14189. const char *str2;
  14190. str1 = script_getstr(st,2);
  14191. str2 = script_getstr(st,3);
  14192. script_pushint(st,strcmp(str1, str2));
  14193. return SCRIPT_CMD_SUCCESS;
  14194. }
  14195. // [zBuffer] List of mathematics commands --->
  14196. BUILDIN_FUNC(sqrt)
  14197. {
  14198. double i, a;
  14199. i = script_getnum(st,2);
  14200. a = sqrt(i);
  14201. script_pushint(st,(int)a);
  14202. return SCRIPT_CMD_SUCCESS;
  14203. }
  14204. BUILDIN_FUNC(pow)
  14205. {
  14206. double i, a, b;
  14207. a = script_getnum(st,2);
  14208. b = script_getnum(st,3);
  14209. i = pow(a,b);
  14210. script_pushint(st,(int)i);
  14211. return SCRIPT_CMD_SUCCESS;
  14212. }
  14213. BUILDIN_FUNC(distance)
  14214. {
  14215. int x0, y0, x1, y1;
  14216. x0 = script_getnum(st,2);
  14217. y0 = script_getnum(st,3);
  14218. x1 = script_getnum(st,4);
  14219. y1 = script_getnum(st,5);
  14220. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14221. return SCRIPT_CMD_SUCCESS;
  14222. }
  14223. // <--- [zBuffer] List of mathematics commands
  14224. BUILDIN_FUNC(md5)
  14225. {
  14226. const char *tmpstr;
  14227. char *md5str;
  14228. tmpstr = script_getstr(st,2);
  14229. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14230. MD5_String(tmpstr, md5str);
  14231. script_pushstr(st, md5str);
  14232. return SCRIPT_CMD_SUCCESS;
  14233. }
  14234. // [zBuffer] List of dynamic var commands --->
  14235. /**
  14236. * setd "<variable name>",<value>{,<char_id>};
  14237. **/
  14238. BUILDIN_FUNC(setd)
  14239. {
  14240. TBL_PC *sd = NULL;
  14241. char varname[100];
  14242. const char *buffer;
  14243. int elem;
  14244. buffer = script_getstr(st, 2);
  14245. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14246. elem = 0;
  14247. if( not_server_variable(*varname) ) {
  14248. if (!script_charid2sd(4,sd))
  14249. return SCRIPT_CMD_FAILURE;
  14250. }
  14251. if( is_string_variable(varname) ) {
  14252. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14253. } else {
  14254. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14255. }
  14256. return SCRIPT_CMD_SUCCESS;
  14257. }
  14258. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14259. {
  14260. int i, j;
  14261. TBL_PC* sd = NULL;
  14262. const char* query;
  14263. struct script_data* data;
  14264. const char* name;
  14265. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14266. int num_vars;
  14267. int num_cols;
  14268. // check target variables
  14269. for( i = 3; script_hasdata(st,i); ++i ) {
  14270. data = script_getdata(st, i);
  14271. if( data_isreference(data) ) { // it's a variable
  14272. name = reference_getname(data);
  14273. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14274. if( !script_rid2sd(sd) ) { // no player attached
  14275. script_reportdata(data);
  14276. st->state = END;
  14277. return SCRIPT_CMD_FAILURE;
  14278. }
  14279. }
  14280. } else {
  14281. ShowError("script:query_sql: not a variable\n");
  14282. script_reportdata(data);
  14283. st->state = END;
  14284. return SCRIPT_CMD_FAILURE;
  14285. }
  14286. }
  14287. num_vars = i - 3;
  14288. // Execute the query
  14289. query = script_getstr(st,2);
  14290. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14291. Sql_ShowDebug(handle);
  14292. script_pushint(st, -1);
  14293. return SCRIPT_CMD_FAILURE;
  14294. }
  14295. if( Sql_NumRows(handle) == 0 ) { // No data received
  14296. Sql_FreeResult(handle);
  14297. script_pushint(st, 0);
  14298. return SCRIPT_CMD_SUCCESS;
  14299. }
  14300. // Count the number of columns to store
  14301. num_cols = Sql_NumColumns(handle);
  14302. if( num_vars < num_cols ) {
  14303. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14304. script_reportsrc(st);
  14305. } else if( num_vars > num_cols ) {
  14306. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14307. script_reportsrc(st);
  14308. }
  14309. // Store data
  14310. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14311. for( j = 0; j < num_vars; ++j ) {
  14312. char* str = NULL;
  14313. if( j < num_cols )
  14314. Sql_GetData(handle, j, &str, NULL);
  14315. data = script_getdata(st, j+3);
  14316. name = reference_getname(data);
  14317. if( is_string_variable(name) )
  14318. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14319. else
  14320. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14321. }
  14322. }
  14323. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14324. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14325. script_reportsrc(st);
  14326. }
  14327. // Free data
  14328. Sql_FreeResult(handle);
  14329. script_pushint(st, i);
  14330. return SCRIPT_CMD_SUCCESS;
  14331. }
  14332. BUILDIN_FUNC(query_sql) {
  14333. return buildin_query_sql_sub(st, qsmysql_handle);
  14334. }
  14335. BUILDIN_FUNC(query_logsql) {
  14336. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14337. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14338. script_pushint(st,-1);
  14339. return SCRIPT_CMD_FAILURE;
  14340. }
  14341. return buildin_query_sql_sub(st, logmysql_handle);
  14342. }
  14343. //Allows escaping of a given string.
  14344. BUILDIN_FUNC(escape_sql)
  14345. {
  14346. const char *str;
  14347. char *esc_str;
  14348. size_t len;
  14349. str = script_getstr(st,2);
  14350. len = strlen(str);
  14351. esc_str = (char*)aMalloc(len*2+1);
  14352. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14353. script_pushstr(st, esc_str);
  14354. return SCRIPT_CMD_SUCCESS;
  14355. }
  14356. BUILDIN_FUNC(getd)
  14357. {
  14358. char varname[100];
  14359. const char *buffer;
  14360. int elem;
  14361. buffer = script_getstr(st, 2);
  14362. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14363. elem = 0;
  14364. // Push the 'pointer' so it's more flexible [Lance]
  14365. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14366. return SCRIPT_CMD_SUCCESS;
  14367. }
  14368. BUILDIN_FUNC(callshop)
  14369. {
  14370. TBL_PC *sd = NULL;
  14371. struct npc_data *nd;
  14372. const char *shopname;
  14373. int flag = 0;
  14374. if (!script_rid2sd(sd)) {
  14375. script_pushint(st,0);
  14376. return SCRIPT_CMD_SUCCESS;
  14377. }
  14378. shopname = script_getstr(st, 2);
  14379. if (script_hasdata(st,3))
  14380. flag = script_getnum(st,3);
  14381. nd = npc_name2id(shopname);
  14382. 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) ) {
  14383. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14384. script_pushint(st,0);
  14385. return SCRIPT_CMD_FAILURE;
  14386. }
  14387. if (nd->subtype == NPCTYPE_SHOP) {
  14388. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14389. sd->state.callshop = 1;
  14390. switch (flag) {
  14391. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14392. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14393. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14394. }
  14395. }
  14396. #if PACKETVER >= 20131223
  14397. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14398. unsigned short i;
  14399. for (i = 0; i < nd->u.shop.count; i++) {
  14400. if (nd->u.shop.shop_item[i].qty)
  14401. break;
  14402. }
  14403. if (i == nd->u.shop.count) {
  14404. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14405. return SCRIPT_CMD_FAILURE;
  14406. }
  14407. sd->npc_shopid = nd->bl.id;
  14408. clif_npc_market_open(sd, nd);
  14409. script_pushint(st,1);
  14410. return SCRIPT_CMD_SUCCESS;
  14411. }
  14412. #endif
  14413. else
  14414. clif_cashshop_show(sd, nd);
  14415. sd->npc_shopid = nd->bl.id;
  14416. script_pushint(st,1);
  14417. return SCRIPT_CMD_SUCCESS;
  14418. }
  14419. BUILDIN_FUNC(npcshopitem)
  14420. {
  14421. const char* npcname = script_getstr(st, 2);
  14422. struct npc_data* nd = npc_name2id(npcname);
  14423. int n, i;
  14424. int amount;
  14425. uint16 offs = 2;
  14426. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14427. script_pushint(st,0);
  14428. return SCRIPT_CMD_SUCCESS;
  14429. }
  14430. #if PACKETVER >= 20131223
  14431. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14432. offs = 3;
  14433. npc_market_delfromsql_(nd->exname, 0, true);
  14434. }
  14435. #endif
  14436. // get the count of new entries
  14437. amount = (script_lastdata(st)-2)/offs;
  14438. // generate new shop item list
  14439. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14440. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14441. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14442. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14443. #if PACKETVER >= 20131223
  14444. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14445. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14446. nd->u.shop.shop_item[n].flag = 1;
  14447. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14448. }
  14449. #endif
  14450. }
  14451. nd->u.shop.count = n;
  14452. script_pushint(st,1);
  14453. return SCRIPT_CMD_SUCCESS;
  14454. }
  14455. BUILDIN_FUNC(npcshopadditem)
  14456. {
  14457. const char* npcname = script_getstr(st,2);
  14458. struct npc_data* nd = npc_name2id(npcname);
  14459. int n, i;
  14460. uint16 offs = 2, amount;
  14461. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14462. script_pushint(st,0);
  14463. return SCRIPT_CMD_SUCCESS;
  14464. }
  14465. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14466. offs = 3;
  14467. // get the count of new entries
  14468. amount = (script_lastdata(st)-2)/offs;
  14469. #if PACKETVER >= 20131223
  14470. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14471. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14472. uint16 nameid = script_getnum(st,i), j;
  14473. // Check existing entries
  14474. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14475. if (j == nd->u.shop.count) {
  14476. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14477. j = nd->u.shop.count;
  14478. nd->u.shop.shop_item[j].flag = 1;
  14479. nd->u.shop.count++;
  14480. }
  14481. nd->u.shop.shop_item[j].nameid = nameid;
  14482. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14483. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14484. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14485. }
  14486. script_pushint(st,1);
  14487. return SCRIPT_CMD_SUCCESS;
  14488. }
  14489. #endif
  14490. // append new items to existing shop item list
  14491. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14492. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14493. {
  14494. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14495. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14496. }
  14497. nd->u.shop.count = n;
  14498. script_pushint(st,1);
  14499. return SCRIPT_CMD_SUCCESS;
  14500. }
  14501. BUILDIN_FUNC(npcshopdelitem)
  14502. {
  14503. const char* npcname = script_getstr(st,2);
  14504. struct npc_data* nd = npc_name2id(npcname);
  14505. int n, i, size;
  14506. unsigned short amount;
  14507. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14508. script_pushint(st,0);
  14509. return SCRIPT_CMD_SUCCESS;
  14510. }
  14511. amount = script_lastdata(st)-2;
  14512. size = nd->u.shop.count;
  14513. // remove specified items from the shop item list
  14514. for( i = 3; i < 3 + amount; i++ ) {
  14515. unsigned short nameid = script_getnum(st,i);
  14516. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14517. if( n < size ) {
  14518. if (n+1 != size)
  14519. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14520. #if PACKETVER >= 20131223
  14521. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14522. npc_market_delfromsql_(nd->exname, nameid, false);
  14523. #endif
  14524. size--;
  14525. }
  14526. }
  14527. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14528. nd->u.shop.count = size;
  14529. script_pushint(st,1);
  14530. return SCRIPT_CMD_SUCCESS;
  14531. }
  14532. /**
  14533. * Sets a script to attach to a shop npc.
  14534. * npcshopattach "<npc_name>";
  14535. **/
  14536. BUILDIN_FUNC(npcshopattach)
  14537. {
  14538. const char* npcname = script_getstr(st,2);
  14539. struct npc_data* nd = npc_name2id(npcname);
  14540. int flag = 1;
  14541. if( script_hasdata(st,3) )
  14542. flag = script_getnum(st,3);
  14543. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14544. script_pushint(st,0);
  14545. return SCRIPT_CMD_SUCCESS;
  14546. }
  14547. if (flag)
  14548. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14549. else
  14550. nd->master_nd = NULL;
  14551. script_pushint(st,1);
  14552. return SCRIPT_CMD_SUCCESS;
  14553. }
  14554. /*==========================================
  14555. * Returns some values of an item [Lupus]
  14556. * Price, Weight, etc...
  14557. setitemscript(itemID,"{new item bonus script}",[n]);
  14558. Where n:
  14559. 0 - script
  14560. 1 - Equip script
  14561. 2 - Unequip script
  14562. *------------------------------------------*/
  14563. BUILDIN_FUNC(setitemscript)
  14564. {
  14565. unsigned short item_id;
  14566. int n = 0;
  14567. const char *script;
  14568. struct item_data *i_data;
  14569. struct script_code **dstscript;
  14570. item_id = script_getnum(st,2);
  14571. script = script_getstr(st,3);
  14572. if( script_hasdata(st,4) )
  14573. n=script_getnum(st,4);
  14574. i_data = itemdb_exists(item_id);
  14575. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14576. script_pushint(st,0);
  14577. return SCRIPT_CMD_SUCCESS;
  14578. }
  14579. switch (n) {
  14580. case 2:
  14581. dstscript = &i_data->unequip_script;
  14582. break;
  14583. case 1:
  14584. dstscript = &i_data->equip_script;
  14585. break;
  14586. default:
  14587. dstscript = &i_data->script;
  14588. break;
  14589. }
  14590. if(*dstscript)
  14591. script_free_code(*dstscript);
  14592. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14593. script_pushint(st,1);
  14594. return SCRIPT_CMD_SUCCESS;
  14595. }
  14596. /*=======================================================
  14597. * Add or Update a mob drop temporarily [Akinari]
  14598. * Original Idea By: [Lupus]
  14599. *
  14600. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14601. *
  14602. * If given an item the mob already drops, the rate
  14603. * is updated to the new rate. Rate cannot exceed 10000
  14604. * Returns 1 if succeeded (added/updated a mob drop)
  14605. *-------------------------------------------------------*/
  14606. BUILDIN_FUNC(addmonsterdrop)
  14607. {
  14608. struct mob_db *mob;
  14609. struct script_data *data;
  14610. unsigned short item_id;
  14611. int rate;
  14612. data = script_getdata(st, 2);
  14613. get_val(st, data); // Convert into value in case of a variable
  14614. if(data_isstring(data))
  14615. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14616. else
  14617. mob = mob_db(script_getnum(st,2));
  14618. item_id=script_getnum(st,3);
  14619. rate=script_getnum(st,4);
  14620. if(!itemdb_exists(item_id)){
  14621. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14622. return SCRIPT_CMD_FAILURE;
  14623. }
  14624. if(mob) { //We got a valid monster, check for available drop slot
  14625. unsigned char i, c = 0;
  14626. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  14627. if(mob->dropitem[i].nameid) {
  14628. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14629. c = i;
  14630. break;
  14631. }
  14632. continue;
  14633. }
  14634. if(!c) //Accept first available slot only
  14635. c = i;
  14636. }
  14637. if(c) { //Fill in the slot with the item and rate
  14638. mob->dropitem[c].nameid = item_id;
  14639. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14640. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  14641. script_pushint(st,1);
  14642. } else //No place to put the new drop
  14643. script_pushint(st,0);
  14644. } else {
  14645. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14646. return SCRIPT_CMD_FAILURE;
  14647. }
  14648. return SCRIPT_CMD_SUCCESS;
  14649. }
  14650. /*=======================================================
  14651. * Delete a mob drop temporarily [Akinari]
  14652. * Original Idea By: [Lupus]
  14653. *
  14654. * delmonsterdrop <mob_id or name>,<item_id>;
  14655. *
  14656. * Returns 1 if succeeded (deleted a mob drop)
  14657. *-------------------------------------------------------*/
  14658. BUILDIN_FUNC(delmonsterdrop)
  14659. {
  14660. struct mob_db *mob;
  14661. struct script_data *data;
  14662. unsigned short item_id;
  14663. data = script_getdata(st, 2);
  14664. get_val(st, data); // Convert into value in case of a variable
  14665. if(data_isstring(data))
  14666. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14667. else
  14668. mob = mob_db(script_getnum(st,2));
  14669. item_id=script_getnum(st,3);
  14670. if(!itemdb_exists(item_id)){
  14671. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14672. return SCRIPT_CMD_FAILURE;
  14673. }
  14674. if(mob) { //We got a valid monster, check for item drop on monster
  14675. unsigned char i;
  14676. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  14677. if(mob->dropitem[i].nameid == item_id) {
  14678. mob->dropitem[i].nameid = 0;
  14679. mob->dropitem[i].p = 0;
  14680. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  14681. script_pushint(st,1);
  14682. return SCRIPT_CMD_SUCCESS;
  14683. }
  14684. }
  14685. //No drop on that monster
  14686. script_pushint(st,0);
  14687. } else {
  14688. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14689. return SCRIPT_CMD_FAILURE;
  14690. }
  14691. return SCRIPT_CMD_SUCCESS;
  14692. }
  14693. /*==========================================
  14694. * Returns some values of a monster [Lupus]
  14695. * Name, Level, race, size, etc...
  14696. getmonsterinfo(monsterID,queryIndex);
  14697. *------------------------------------------*/
  14698. BUILDIN_FUNC(getmonsterinfo)
  14699. {
  14700. struct mob_db *mob;
  14701. int mob_id;
  14702. mob_id = script_getnum(st,2);
  14703. if (!mobdb_checkid(mob_id)) {
  14704. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14705. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14706. script_pushconststr(st,"null");
  14707. else
  14708. script_pushint(st,-1);
  14709. return SCRIPT_CMD_SUCCESS;
  14710. }
  14711. mob = mob_db(mob_id);
  14712. switch ( script_getnum(st,3) ) {
  14713. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14714. case MOB_LV: script_pushint(st,mob->lv); break;
  14715. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14716. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14717. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14718. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14719. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14720. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14721. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14722. case MOB_STR: script_pushint(st,mob->status.str); break;
  14723. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14724. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14725. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14726. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14727. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14728. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14729. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14730. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14731. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14732. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14733. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14734. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14735. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14736. default: script_pushint(st,-1); //wrong Index
  14737. }
  14738. return SCRIPT_CMD_SUCCESS;
  14739. }
  14740. /**
  14741. * Check player's vending/buyingstore/autotrade state
  14742. * checkvending({"<Player Name>"})
  14743. * @author [Nab4]
  14744. */
  14745. BUILDIN_FUNC(checkvending) {
  14746. TBL_PC *sd = NULL;
  14747. if (!script_nick2sd(2,sd) ) {
  14748. script_pushint(st,0);
  14749. return SCRIPT_CMD_SUCCESS;
  14750. }
  14751. else {
  14752. int8 ret = 0;
  14753. if (sd->state.vending)
  14754. ret = 1;
  14755. else if (sd->state.buyingstore)
  14756. ret = 4;
  14757. if (sd->state.autotrade)
  14758. ret |= 2;
  14759. script_pushint(st, ret);
  14760. }
  14761. return SCRIPT_CMD_SUCCESS;
  14762. }
  14763. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14764. {
  14765. TBL_PC *sd = NULL;
  14766. if( script_nick2sd(2,sd) )
  14767. script_pushint(st,(sd->chatID != 0));
  14768. else
  14769. script_pushint(st,0);
  14770. return SCRIPT_CMD_SUCCESS;
  14771. }
  14772. BUILDIN_FUNC(checkidle)
  14773. {
  14774. TBL_PC *sd = NULL;
  14775. if( script_nick2sd(2,sd) )
  14776. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14777. else
  14778. script_pushint(st, 0);
  14779. return SCRIPT_CMD_SUCCESS;
  14780. }
  14781. BUILDIN_FUNC(searchitem)
  14782. {
  14783. struct script_data* data = script_getdata(st, 2);
  14784. const char *itemname = script_getstr(st,3);
  14785. struct item_data *items[MAX_SEARCH];
  14786. int count;
  14787. char* name;
  14788. int32 start;
  14789. int32 id;
  14790. int32 i;
  14791. TBL_PC* sd = NULL;
  14792. if ((items[0] = itemdb_exists(atoi(itemname))))
  14793. count = 1;
  14794. else
  14795. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14796. if (!count) {
  14797. script_pushint(st, 0);
  14798. return SCRIPT_CMD_SUCCESS;
  14799. }
  14800. if( !data_isreference(data) )
  14801. {
  14802. ShowError("script:searchitem: not a variable\n");
  14803. script_reportdata(data);
  14804. st->state = END;
  14805. return SCRIPT_CMD_FAILURE;// not a variable
  14806. }
  14807. id = reference_getid(data);
  14808. start = reference_getindex(data);
  14809. name = reference_getname(data);
  14810. if( not_server_variable(*name) && !script_rid2sd(sd) )
  14811. {
  14812. return SCRIPT_CMD_SUCCESS;// no player attached
  14813. }
  14814. if( is_string_variable(name) )
  14815. {// string array
  14816. ShowError("script:searchitem: not an integer array reference\n");
  14817. script_reportdata(data);
  14818. st->state = END;
  14819. return SCRIPT_CMD_FAILURE;// not supported
  14820. }
  14821. for( i = 0; i < count; ++start, ++i )
  14822. {// Set array
  14823. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14824. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14825. }
  14826. script_pushint(st, count);
  14827. return SCRIPT_CMD_SUCCESS;
  14828. }
  14829. // [zBuffer] List of player cont commands --->
  14830. BUILDIN_FUNC(rid2name)
  14831. {
  14832. struct block_list *bl = NULL;
  14833. int rid = script_getnum(st,2);
  14834. if((bl = map_id2bl(rid)))
  14835. {
  14836. switch(bl->type) {
  14837. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14838. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14839. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14840. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14841. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14842. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14843. default:
  14844. ShowError("buildin_rid2name: BL type unknown.\n");
  14845. script_pushconststr(st,"");
  14846. break;
  14847. }
  14848. } else {
  14849. ShowError("buildin_rid2name: invalid RID\n");
  14850. script_pushconststr(st,"(null)");
  14851. }
  14852. return SCRIPT_CMD_SUCCESS;
  14853. }
  14854. /**
  14855. * Toggle a unit from moving.
  14856. * pcblockmove(<unit_id>,<option>);
  14857. */
  14858. BUILDIN_FUNC(pcblockmove)
  14859. {
  14860. struct block_list *bl = NULL;
  14861. if (script_getnum(st, 2))
  14862. bl = map_id2bl(script_getnum(st,2));
  14863. else
  14864. bl = map_id2bl(st->rid);
  14865. if (bl) {
  14866. struct unit_data *ud = unit_bl2ud(bl);
  14867. if (ud)
  14868. ud->state.blockedmove = script_getnum(st,3) > 0;
  14869. }
  14870. return SCRIPT_CMD_SUCCESS;
  14871. }
  14872. /**
  14873. * Toggle a unit from casting skills.
  14874. * pcblockskill(<unit_id>,<option>);
  14875. */
  14876. BUILDIN_FUNC(pcblockskill)
  14877. {
  14878. struct block_list *bl = NULL;
  14879. if (script_getnum(st, 2))
  14880. bl = map_id2bl(script_getnum(st,2));
  14881. else
  14882. bl = map_id2bl(st->rid);
  14883. if (bl) {
  14884. struct unit_data *ud = unit_bl2ud(bl);
  14885. if (ud)
  14886. ud->state.blockedskill = script_getnum(st, 3) > 0;
  14887. }
  14888. return SCRIPT_CMD_SUCCESS;
  14889. }
  14890. BUILDIN_FUNC(pcfollow)
  14891. {
  14892. TBL_PC *sd;
  14893. if( script_mapid2sd(2,sd) )
  14894. pc_follow(sd, script_getnum(st,3));
  14895. return SCRIPT_CMD_SUCCESS;
  14896. }
  14897. BUILDIN_FUNC(pcstopfollow)
  14898. {
  14899. TBL_PC *sd;
  14900. if(script_mapid2sd(2,sd))
  14901. pc_stop_following(sd);
  14902. return SCRIPT_CMD_SUCCESS;
  14903. }
  14904. // <--- [zBuffer] List of player cont commands
  14905. // [zBuffer] List of unit control commands --->
  14906. /// Checks to see if the unit exists.
  14907. ///
  14908. /// unitexists <unit id>;
  14909. BUILDIN_FUNC(unitexists)
  14910. {
  14911. struct block_list* bl;
  14912. bl = map_id2bl(script_getnum(st, 2));
  14913. if (!bl)
  14914. script_pushint(st, false);
  14915. else
  14916. script_pushint(st, true);
  14917. return SCRIPT_CMD_SUCCESS;
  14918. }
  14919. /// Gets the type of the given Game ID.
  14920. ///
  14921. /// getunittype <unit id>;
  14922. BUILDIN_FUNC(getunittype)
  14923. {
  14924. struct block_list* bl;
  14925. uint8 value = 0;
  14926. if(!script_rid2bl(2,bl))
  14927. {
  14928. script_pushint(st, -1);
  14929. return SCRIPT_CMD_FAILURE;
  14930. }
  14931. switch (bl->type) {
  14932. case BL_PC: value = UNITTYPE_PC; break;
  14933. case BL_NPC: value = UNITTYPE_NPC; break;
  14934. case BL_PET: value = UNITTYPE_PET; break;
  14935. case BL_MOB: value = UNITTYPE_MOB; break;
  14936. case BL_HOM: value = UNITTYPE_HOM; break;
  14937. case BL_MER: value = UNITTYPE_MER; break;
  14938. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14939. default: value = -1; break;
  14940. }
  14941. script_pushint(st, value);
  14942. return SCRIPT_CMD_SUCCESS;
  14943. }
  14944. /// Gets specific live information of a bl.
  14945. ///
  14946. /// getunitdata <unit id>,<arrayname>;
  14947. BUILDIN_FUNC(getunitdata)
  14948. {
  14949. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14950. struct block_list* bl;
  14951. TBL_MOB* md = NULL;
  14952. TBL_HOM* hd = NULL;
  14953. TBL_MER* mc = NULL;
  14954. TBL_PET* pd = NULL;
  14955. TBL_ELEM* ed = NULL;
  14956. TBL_NPC* nd = NULL;
  14957. char* name;
  14958. struct script_data *data = script_getdata(st, 3);
  14959. if (!data_isreference(data)) {
  14960. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14961. return SCRIPT_CMD_FAILURE;
  14962. }
  14963. if(!script_rid2bl(2,bl))
  14964. {
  14965. script_pushint(st, -1);
  14966. return SCRIPT_CMD_FAILURE;
  14967. }
  14968. switch (bl->type) {
  14969. case BL_MOB: md = map_id2md(bl->id); break;
  14970. case BL_HOM: hd = map_id2hd(bl->id); break;
  14971. case BL_PET: pd = map_id2pd(bl->id); break;
  14972. case BL_MER: mc = map_id2mc(bl->id); break;
  14973. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14974. case BL_NPC: nd = map_id2nd(bl->id); break;
  14975. default:
  14976. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  14977. return SCRIPT_CMD_FAILURE;
  14978. }
  14979. name = reference_getname(data);
  14980. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14981. switch(bl->type) {
  14982. case BL_MOB:
  14983. if (!md) {
  14984. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14985. return SCRIPT_CMD_FAILURE;
  14986. }
  14987. getunitdata_sub(UMOB_SIZE, md->status.size);
  14988. getunitdata_sub(UMOB_LEVEL, md->level);
  14989. getunitdata_sub(UMOB_HP, md->status.hp);
  14990. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14991. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14992. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14993. getunitdata_sub(UMOB_X, md->bl.x);
  14994. getunitdata_sub(UMOB_Y, md->bl.y);
  14995. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14996. getunitdata_sub(UMOB_MODE, md->status.mode);
  14997. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14998. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14999. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15000. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15001. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15002. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15003. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15004. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15005. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15006. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15007. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15008. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15009. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15010. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15011. getunitdata_sub(UMOB_STR, md->status.str);
  15012. getunitdata_sub(UMOB_AGI, md->status.agi);
  15013. getunitdata_sub(UMOB_VIT, md->status.vit);
  15014. getunitdata_sub(UMOB_INT, md->status.int_);
  15015. getunitdata_sub(UMOB_DEX, md->status.dex);
  15016. getunitdata_sub(UMOB_LUK, md->status.luk);
  15017. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15018. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15019. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15020. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15021. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15022. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15023. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15024. getunitdata_sub(UMOB_DEF, md->status.def);
  15025. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15026. getunitdata_sub(UMOB_HIT, md->status.hit);
  15027. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15028. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15029. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15030. getunitdata_sub(UMOB_RACE, md->status.race);
  15031. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15032. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15033. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15034. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15035. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15036. getunitdata_sub(UMOB_TARGETID, md->target_id);
  15037. break;
  15038. case BL_HOM:
  15039. if (!hd) {
  15040. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15041. return SCRIPT_CMD_FAILURE;
  15042. }
  15043. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15044. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15045. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15046. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15047. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15048. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15049. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15050. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15051. getunitdata_sub(UHOM_X, hd->bl.x);
  15052. getunitdata_sub(UHOM_Y, hd->bl.y);
  15053. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15054. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15055. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15056. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15057. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15058. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15059. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15060. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15061. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15062. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15063. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15064. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15065. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15066. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15067. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15068. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15069. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15070. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15071. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15072. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15073. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15074. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15075. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15076. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15077. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15078. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15079. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15080. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15081. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15082. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  15083. break;
  15084. case BL_PET:
  15085. if (!pd) {
  15086. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15087. return SCRIPT_CMD_FAILURE;
  15088. }
  15089. getunitdata_sub(UPET_SIZE, pd->status.size);
  15090. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15091. getunitdata_sub(UPET_HP, pd->status.hp);
  15092. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15093. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15094. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15095. getunitdata_sub(UPET_X, pd->bl.x);
  15096. getunitdata_sub(UPET_Y, pd->bl.y);
  15097. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15098. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15099. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15100. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15101. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15102. getunitdata_sub(UPET_STR, pd->status.str);
  15103. getunitdata_sub(UPET_AGI, pd->status.agi);
  15104. getunitdata_sub(UPET_VIT, pd->status.vit);
  15105. getunitdata_sub(UPET_INT, pd->status.int_);
  15106. getunitdata_sub(UPET_DEX, pd->status.dex);
  15107. getunitdata_sub(UPET_LUK, pd->status.luk);
  15108. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15109. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15110. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15111. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15112. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15113. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15114. getunitdata_sub(UPET_DEF, pd->status.def);
  15115. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15116. getunitdata_sub(UPET_HIT, pd->status.hit);
  15117. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15118. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15119. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15120. getunitdata_sub(UPET_RACE, pd->status.race);
  15121. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15122. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15123. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15124. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15125. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15126. break;
  15127. case BL_MER:
  15128. if (!mc) {
  15129. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15130. return SCRIPT_CMD_FAILURE;
  15131. }
  15132. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15133. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15134. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15135. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15136. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15137. getunitdata_sub(UMER_X, mc->bl.x);
  15138. getunitdata_sub(UMER_Y, mc->bl.y);
  15139. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15140. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15141. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15142. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15143. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15144. getunitdata_sub(UMER_STR, mc->base_status.str);
  15145. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15146. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15147. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15148. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15149. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15150. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15151. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15152. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15153. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15154. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15155. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15156. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15157. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15158. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15159. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15160. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15161. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15162. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15163. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15164. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15165. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15166. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15167. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15168. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  15169. break;
  15170. case BL_ELEM:
  15171. if (!ed) {
  15172. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15173. return SCRIPT_CMD_FAILURE;
  15174. }
  15175. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15176. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15177. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15178. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15179. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15180. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15181. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15182. getunitdata_sub(UELE_X, ed->bl.x);
  15183. getunitdata_sub(UELE_Y, ed->bl.y);
  15184. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15185. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15186. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15187. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15188. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15189. getunitdata_sub(UELE_STR, ed->base_status.str);
  15190. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15191. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15192. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15193. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15194. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15195. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15196. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15197. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15198. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15199. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15200. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15201. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15202. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15203. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15204. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15205. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15206. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15207. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15208. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15209. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15210. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15211. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15212. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15213. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  15214. break;
  15215. case BL_NPC:
  15216. if (!nd) {
  15217. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15218. return SCRIPT_CMD_FAILURE;
  15219. }
  15220. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15221. getunitdata_sub(UNPC_LEVEL, nd->level);
  15222. getunitdata_sub(UNPC_HP, nd->status.hp);
  15223. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15224. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15225. getunitdata_sub(UNPC_X, nd->bl.x);
  15226. getunitdata_sub(UNPC_Y, nd->bl.y);
  15227. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15228. getunitdata_sub(UNPC_STR, nd->status.str);
  15229. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15230. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15231. getunitdata_sub(UNPC_INT, nd->status.int_);
  15232. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15233. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15234. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15235. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15236. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15237. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15238. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15239. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15240. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15241. getunitdata_sub(UNPC_DEF, nd->status.def);
  15242. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15243. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15244. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15245. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15246. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15247. getunitdata_sub(UNPC_RACE, nd->status.race);
  15248. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15249. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15250. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15251. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15252. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15253. break;
  15254. default:
  15255. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15256. return SCRIPT_CMD_FAILURE;
  15257. }
  15258. #undef getunitdata_sub
  15259. return SCRIPT_CMD_SUCCESS;
  15260. }
  15261. /// Changes the live data of a bl.
  15262. ///
  15263. /// setunitdata <unit id>,<type>,<value>;
  15264. BUILDIN_FUNC(setunitdata)
  15265. {
  15266. struct block_list* bl = NULL;
  15267. struct script_data* data;
  15268. const char *mapname = NULL;
  15269. TBL_MOB* md = NULL;
  15270. TBL_HOM* hd = NULL;
  15271. TBL_MER* mc = NULL;
  15272. TBL_PET* pd = NULL;
  15273. TBL_ELEM* ed = NULL;
  15274. TBL_NPC* nd = NULL;
  15275. int type, value = 0;
  15276. bool calc_status = false;
  15277. if(!script_rid2bl(2,bl))
  15278. {
  15279. script_pushint(st, -1);
  15280. return SCRIPT_CMD_FAILURE;
  15281. }
  15282. switch (bl->type) {
  15283. case BL_MOB: md = map_id2md(bl->id); break;
  15284. case BL_HOM: hd = map_id2hd(bl->id); break;
  15285. case BL_PET: pd = map_id2pd(bl->id); break;
  15286. case BL_MER: mc = map_id2mc(bl->id); break;
  15287. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15288. case BL_NPC:
  15289. nd = map_id2nd(bl->id);
  15290. if (!nd->status.hp)
  15291. status_calc_npc(nd, SCO_FIRST);
  15292. else
  15293. status_calc_npc(nd, SCO_NONE);
  15294. break;
  15295. default:
  15296. ShowError("buildin_setunitdata: Invalid object!\n");
  15297. return SCRIPT_CMD_FAILURE;
  15298. }
  15299. type = script_getnum(st, 3);
  15300. data = script_getdata(st, 4);
  15301. get_val(st, data);
  15302. if (type == 5 && data_isstring(data))
  15303. mapname = conv_str(st, data);
  15304. else if (data_isint(data))
  15305. value = conv_num(st, data);
  15306. else {
  15307. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15308. return SCRIPT_CMD_FAILURE;
  15309. }
  15310. switch (bl->type) {
  15311. case BL_MOB:
  15312. if (!md) {
  15313. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15314. return SCRIPT_CMD_FAILURE;
  15315. }
  15316. if (!md->base_status) {
  15317. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  15318. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  15319. }
  15320. // Check if the view data will be modified
  15321. switch( type ){
  15322. case UMOB_SEX:
  15323. //case UMOB_CLASS: // Called by status_set_viewdata
  15324. case UMOB_HAIRSTYLE:
  15325. case UMOB_HAIRCOLOR:
  15326. case UMOB_HEADBOTTOM:
  15327. case UMOB_HEADMIDDLE:
  15328. case UMOB_HEADTOP:
  15329. case UMOB_CLOTHCOLOR:
  15330. case UMOB_SHIELD:
  15331. case UMOB_WEAPON:
  15332. mob_set_dynamic_viewdata( md );
  15333. break;
  15334. }
  15335. switch (type) {
  15336. case UMOB_SIZE: md->base_status->size = (unsigned char)value; calc_status = true; break;
  15337. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15338. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15339. case UMOB_MAXHP: md->base_status->max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15340. case UMOB_MASTERAID: md->master_id = value; break;
  15341. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15342. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15343. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15344. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15345. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  15346. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15347. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15348. case UMOB_SEX: md->vd->sex = (char)value; clif_clearunit_area(bl, CLR_OUTSIGHT); clif_spawn(bl); break;
  15349. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); clif_clearunit_area(bl, CLR_OUTSIGHT); clif_spawn(bl); break;
  15350. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15351. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15352. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15353. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15354. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15355. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15356. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15357. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15358. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15359. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15360. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15361. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15362. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15363. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15364. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15365. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15366. case UMOB_SLAVECPYMSTRMD:
  15367. if (value > 0) {
  15368. TBL_MOB *md2 = NULL;
  15369. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15370. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15371. break;
  15372. }
  15373. md->base_status->mode = md2->status.mode;
  15374. md->state.copy_master_mode = 1;
  15375. } else
  15376. md->state.copy_master_mode = 0;
  15377. calc_status = true;
  15378. break;
  15379. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  15380. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  15381. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  15382. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15383. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  15384. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  15385. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  15386. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  15387. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  15388. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  15389. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  15390. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  15391. case UMOB_RACE: md->base_status->race = (unsigned char)value; calc_status = true; break;
  15392. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  15393. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  15394. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  15395. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  15396. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  15397. case UMOB_TARGETID: {
  15398. struct block_list* target = map_id2bl(value);
  15399. if (!target) {
  15400. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  15401. return SCRIPT_CMD_FAILURE;
  15402. }
  15403. mob_target(md,target,0);
  15404. break;
  15405. }
  15406. default:
  15407. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15408. return SCRIPT_CMD_FAILURE;
  15409. }
  15410. if (calc_status)
  15411. status_calc_bl(&md->bl, SCB_BATTLE);
  15412. break;
  15413. case BL_HOM:
  15414. if (!hd) {
  15415. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15416. return SCRIPT_CMD_FAILURE;
  15417. }
  15418. switch (type) {
  15419. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; calc_status = true; break;
  15420. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15421. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15422. case UHOM_MAXHP: hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15423. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15424. case UHOM_MAXSP: hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15425. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15426. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15427. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15428. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15429. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15430. 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;
  15431. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15432. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15433. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15434. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15435. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15436. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15437. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15438. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15439. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15440. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  15441. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15442. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15443. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15444. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15445. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15446. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  15447. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  15448. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  15449. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  15450. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  15451. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  15452. case UHOM_RACE: hd->base_status.race = (unsigned char)value; calc_status = true; break;
  15453. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15454. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15455. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  15456. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  15457. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  15458. case UHOM_TARGETID: {
  15459. struct block_list* target = map_id2bl(value);
  15460. if (!target) {
  15461. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  15462. return SCRIPT_CMD_FAILURE;
  15463. }
  15464. unit_attack(&hd->bl, target->id, 1);
  15465. break;
  15466. }
  15467. default:
  15468. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15469. return SCRIPT_CMD_FAILURE;
  15470. }
  15471. if (calc_status)
  15472. status_calc_bl(&hd->bl, SCB_BATTLE);
  15473. break;
  15474. case BL_PET:
  15475. if (!pd) {
  15476. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15477. return SCRIPT_CMD_FAILURE;
  15478. }
  15479. switch (type) {
  15480. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15481. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15482. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15483. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15484. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15485. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15486. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15487. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15488. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15489. 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;
  15490. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15491. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15492. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15493. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15494. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15495. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15496. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15497. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15498. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15499. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  15500. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15501. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15502. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15503. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15504. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15505. case UPET_DEF: pd->status.def = (defType)value; break;
  15506. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15507. case UPET_HIT: pd->status.hit = (short)value; break;
  15508. case UPET_FLEE: pd->status.flee = (short)value; break;
  15509. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15510. case UPET_CRIT: pd->status.cri = (short)value; break;
  15511. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15512. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15513. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15514. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15515. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15516. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15517. default:
  15518. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15519. return SCRIPT_CMD_FAILURE;
  15520. }
  15521. break;
  15522. case BL_MER:
  15523. if (!mc) {
  15524. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15525. return SCRIPT_CMD_FAILURE;
  15526. }
  15527. switch (type) {
  15528. case UMER_SIZE: mc->base_status.size = (unsigned char)value; calc_status = true; break;
  15529. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15530. case UMER_MAXHP: mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15531. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15532. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15533. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15534. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15535. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15536. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15537. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15538. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15539. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15540. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15541. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15542. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15543. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15544. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15545. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15546. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  15547. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15548. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15549. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15550. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15551. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15552. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  15553. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  15554. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  15555. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  15556. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  15557. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  15558. case UMER_RACE: mc->base_status.race = (unsigned char)value; calc_status = true; break;
  15559. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15560. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15561. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  15562. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  15563. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  15564. case UMER_TARGETID: {
  15565. struct block_list* target = map_id2bl(value);
  15566. if (!target) {
  15567. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  15568. return SCRIPT_CMD_FAILURE;
  15569. }
  15570. unit_attack(&mc->bl, target->id, 1);
  15571. break;
  15572. }
  15573. default:
  15574. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15575. return SCRIPT_CMD_FAILURE;
  15576. }
  15577. if (calc_status)
  15578. status_calc_bl(&mc->bl, SCB_BATTLE);
  15579. break;
  15580. case BL_ELEM:
  15581. if (!ed) {
  15582. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15583. return SCRIPT_CMD_FAILURE;
  15584. }
  15585. switch (type) {
  15586. case UELE_SIZE: ed->base_status.size = (unsigned char)value; calc_status = true; break;
  15587. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15588. case UELE_MAXHP: ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15589. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15590. case UELE_MAXSP: ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15591. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15592. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15593. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15594. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15595. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15596. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  15597. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15598. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15599. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15600. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15601. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15602. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15603. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15604. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15605. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15606. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  15607. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15608. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15609. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15610. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15611. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15612. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  15613. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  15614. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  15615. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  15616. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  15617. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  15618. case UELE_RACE: ed->base_status.race = (unsigned char)value; calc_status = true; break;
  15619. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15620. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15621. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  15622. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  15623. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  15624. case UELE_TARGETID: {
  15625. struct block_list* target = map_id2bl(value);
  15626. if (!target) {
  15627. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  15628. return SCRIPT_CMD_FAILURE;
  15629. }
  15630. elemental_change_mode(ed, static_cast<e_mode>(EL_MODE_AGGRESSIVE));
  15631. unit_attack(&ed->bl, target->id, 1);
  15632. break;
  15633. }
  15634. default:
  15635. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15636. return SCRIPT_CMD_FAILURE;
  15637. }
  15638. if (calc_status)
  15639. status_calc_bl(&ed->bl, SCB_BATTLE);
  15640. break;
  15641. case BL_NPC:
  15642. if (!nd) {
  15643. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15644. return SCRIPT_CMD_FAILURE;
  15645. }
  15646. switch (type) {
  15647. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15648. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15649. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15650. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15651. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15652. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15653. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15654. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15655. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15656. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15657. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15658. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15659. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15660. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15661. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15662. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15663. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15664. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15665. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15666. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15667. case UNPC_DEF: nd->status.def = (defType)value; break;
  15668. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15669. case UNPC_HIT: nd->status.hit = (short)value; break;
  15670. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15671. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15672. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15673. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15674. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15675. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15676. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15677. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15678. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15679. default:
  15680. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15681. return SCRIPT_CMD_FAILURE;
  15682. }
  15683. break;
  15684. default:
  15685. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15686. return SCRIPT_CMD_FAILURE;
  15687. }
  15688. // Client information updates
  15689. switch (bl->type) {
  15690. case BL_HOM:
  15691. clif_send_homdata(hd->master, SP_ACK, 0);
  15692. break;
  15693. case BL_PET:
  15694. clif_send_petstatus(pd->master);
  15695. break;
  15696. case BL_MER:
  15697. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15698. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15699. break;
  15700. case BL_ELEM:
  15701. clif_elemental_info(ed->master);
  15702. break;
  15703. }
  15704. return SCRIPT_CMD_SUCCESS;
  15705. }
  15706. /// Gets the name of a bl.
  15707. /// Supported types are [MOB|HOM|PET|NPC].
  15708. /// MER and ELEM don't support custom names.
  15709. ///
  15710. /// getunitname <unit id>;
  15711. BUILDIN_FUNC(getunitname)
  15712. {
  15713. struct block_list* bl = NULL;
  15714. if(!script_rid2bl(2,bl)){
  15715. script_pushconststr(st, "Unknown");
  15716. return SCRIPT_CMD_FAILURE;
  15717. }
  15718. script_pushstrcopy(st, status_get_name(bl));
  15719. return SCRIPT_CMD_SUCCESS;
  15720. }
  15721. /// Changes the name of a bl.
  15722. /// Supported types are [MOB|HOM|PET].
  15723. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15724. ///
  15725. /// setunitname <unit id>,<name>;
  15726. BUILDIN_FUNC(setunitname)
  15727. {
  15728. struct block_list* bl = NULL;
  15729. TBL_MOB* md = NULL;
  15730. TBL_HOM* hd = NULL;
  15731. TBL_PET* pd = NULL;
  15732. if(!script_rid2bl(2,bl))
  15733. {
  15734. script_pushconststr(st, "Unknown");
  15735. return SCRIPT_CMD_FAILURE;
  15736. }
  15737. switch (bl->type) {
  15738. case BL_MOB: md = map_id2md(bl->id); break;
  15739. case BL_HOM: hd = map_id2hd(bl->id); break;
  15740. case BL_PET: pd = map_id2pd(bl->id); break;
  15741. default:
  15742. ShowWarning("buildin_setunitname: Invalid object type!\n");
  15743. return SCRIPT_CMD_FAILURE;
  15744. }
  15745. switch (bl->type) {
  15746. case BL_MOB:
  15747. if (!md) {
  15748. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15749. return SCRIPT_CMD_FAILURE;
  15750. }
  15751. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15752. break;
  15753. case BL_HOM:
  15754. if (!hd) {
  15755. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15756. return SCRIPT_CMD_FAILURE;
  15757. }
  15758. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15759. break;
  15760. case BL_PET:
  15761. if (!pd) {
  15762. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15763. return SCRIPT_CMD_FAILURE;
  15764. }
  15765. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15766. break;
  15767. default:
  15768. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15769. return SCRIPT_CMD_FAILURE;
  15770. }
  15771. clif_name_area(bl); // Send update to client.
  15772. return SCRIPT_CMD_SUCCESS;
  15773. }
  15774. /// Makes the unit walk to target position or map.
  15775. /// Returns if it was successful.
  15776. ///
  15777. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15778. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15779. BUILDIN_FUNC(unitwalk)
  15780. {
  15781. struct block_list* bl;
  15782. struct unit_data *ud = NULL;
  15783. const char *cmd = script_getfuncname(st), *done_label = "";
  15784. uint8 off = 5;
  15785. if(!script_rid2bl(2,bl))
  15786. {
  15787. script_pushint(st, 0);
  15788. return SCRIPT_CMD_FAILURE;
  15789. }
  15790. ud = unit_bl2ud(bl);
  15791. if (!strcmp(cmd,"unitwalk")) {
  15792. int x = script_getnum(st,3);
  15793. int y = script_getnum(st,4);
  15794. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15795. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15796. } else {
  15797. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15798. if (!tbl) {
  15799. ShowError("buildin_unitwalk: Bad target destination.\n");
  15800. script_pushint(st, 0);
  15801. return SCRIPT_CMD_FAILURE;
  15802. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15803. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15804. off = 4;
  15805. }
  15806. if (ud && script_hasdata(st, off)) {
  15807. done_label = script_getstr(st, off);
  15808. check_event(st, done_label);
  15809. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15810. }
  15811. return SCRIPT_CMD_SUCCESS;
  15812. }
  15813. /// Kills the unit.
  15814. ///
  15815. /// unitkill <unit_id>;
  15816. BUILDIN_FUNC(unitkill)
  15817. {
  15818. struct block_list* bl;
  15819. if(script_rid2bl(2,bl))
  15820. status_kill(bl);
  15821. return SCRIPT_CMD_SUCCESS;
  15822. }
  15823. /// Warps the unit to the target position in the target map.
  15824. /// Returns if it was successful.
  15825. ///
  15826. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15827. BUILDIN_FUNC(unitwarp)
  15828. {
  15829. int map_idx;
  15830. short x;
  15831. short y;
  15832. struct block_list* bl;
  15833. const char *mapname;
  15834. mapname = script_getstr(st, 3);
  15835. x = (short)script_getnum(st,4);
  15836. y = (short)script_getnum(st,5);
  15837. if(!script_rid2bl(2,bl))
  15838. {
  15839. script_pushint(st, 0);
  15840. return SCRIPT_CMD_SUCCESS;
  15841. }
  15842. if (!strcmp(mapname,"this"))
  15843. map_idx = bl?bl->m:-1;
  15844. else
  15845. map_idx = map_mapname2mapid(mapname);
  15846. if (map_idx >= 0 && bl != NULL)
  15847. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15848. else
  15849. script_pushint(st, 0);
  15850. return SCRIPT_CMD_SUCCESS;
  15851. }
  15852. /// Makes the unit attack the target.
  15853. /// If the unit is a player and <action type> is not 0, it does a continuous
  15854. /// attack instead of a single attack.
  15855. /// Returns if the request was successful.
  15856. ///
  15857. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15858. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15859. BUILDIN_FUNC(unitattack)
  15860. {
  15861. struct block_list* unit_bl;
  15862. struct block_list* target_bl = NULL;
  15863. struct script_data* data;
  15864. int actiontype = 0;
  15865. if (!script_rid2bl(2,unit_bl)) {
  15866. script_pushint(st, false);
  15867. return SCRIPT_CMD_FAILURE;
  15868. }
  15869. data = script_getdata(st, 3);
  15870. get_val(st, data);
  15871. if (data_isstring(data)) {
  15872. TBL_PC* sd = map_nick2sd(conv_str(st, data),false);
  15873. if( sd != NULL )
  15874. target_bl = &sd->bl;
  15875. } else
  15876. target_bl = map_id2bl(conv_num(st, data));
  15877. if (!target_bl) {
  15878. script_pushint(st, false);
  15879. return SCRIPT_CMD_FAILURE;
  15880. }
  15881. if (script_hasdata(st,4))
  15882. actiontype = script_getnum(st,4);
  15883. switch(unit_bl->type) {
  15884. case BL_PC: {
  15885. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  15886. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15887. script_pushint(st, sd->ud.target == target_bl->id);
  15888. return SCRIPT_CMD_SUCCESS;
  15889. }
  15890. case BL_MOB:
  15891. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15892. break;
  15893. case BL_PET:
  15894. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15895. break;
  15896. default:
  15897. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15898. script_pushint(st, false);
  15899. return SCRIPT_CMD_FAILURE;
  15900. }
  15901. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15902. return SCRIPT_CMD_SUCCESS;
  15903. }
  15904. /// Makes the unit stop attacking.
  15905. ///
  15906. /// unitstopattack <unit_id>;
  15907. BUILDIN_FUNC(unitstopattack)
  15908. {
  15909. struct block_list* bl;
  15910. if(script_rid2bl(2,bl))
  15911. {
  15912. unit_stop_attack(bl);
  15913. if (bl->type == BL_MOB)
  15914. ((TBL_MOB*)bl)->target_id = 0;
  15915. }
  15916. return SCRIPT_CMD_SUCCESS;
  15917. }
  15918. /// Makes the unit stop walking.
  15919. ///
  15920. /// unitstopwalk <unit_id>{,<flag>};
  15921. BUILDIN_FUNC(unitstopwalk)
  15922. {
  15923. struct block_list* bl;
  15924. int flag = USW_NONE;
  15925. if (script_hasdata(st, 3))
  15926. flag = script_getnum(st, 3);
  15927. if(script_rid2bl(2,bl))
  15928. unit_stop_walking(bl, flag);
  15929. return SCRIPT_CMD_SUCCESS;
  15930. }
  15931. /**
  15932. * Makes the unit say the given message.
  15933. *
  15934. * unittalk <unit_id>,"<message>"{,"<flag>"};
  15935. * @param flag: Specify target
  15936. * bc_area - Message is sent to players in the vicinity of the source (default).
  15937. * bc_self - Message is sent only to player attached.
  15938. */
  15939. BUILDIN_FUNC(unittalk)
  15940. {
  15941. const char* message;
  15942. struct block_list* bl;
  15943. message = script_getstr(st, 3);
  15944. if(script_rid2bl(2,bl))
  15945. {
  15946. send_target target = AREA;
  15947. struct StringBuf sbuf;
  15948. if (script_hasdata(st, 4)) {
  15949. if (script_getnum(st, 4) == BC_SELF) {
  15950. if (map_id2sd(bl->id) == NULL) {
  15951. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  15952. return SCRIPT_CMD_FAILURE;
  15953. }
  15954. target = SELF;
  15955. }
  15956. }
  15957. StringBuf_Init(&sbuf);
  15958. StringBuf_Printf(&sbuf, "%s", message);
  15959. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  15960. StringBuf_Destroy(&sbuf);
  15961. }
  15962. return SCRIPT_CMD_SUCCESS;
  15963. }
  15964. /// Makes the unit do an emotion.
  15965. ///
  15966. /// unitemote <unit_id>,<emotion>;
  15967. ///
  15968. /// @see ET_* in script_constants.h
  15969. BUILDIN_FUNC(unitemote)
  15970. {
  15971. int emotion;
  15972. struct block_list* bl;
  15973. emotion = script_getnum(st,3);
  15974. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  15975. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  15976. return SCRIPT_CMD_FAILURE;
  15977. }
  15978. if (script_rid2bl(2,bl))
  15979. clif_emotion(bl, emotion);
  15980. return SCRIPT_CMD_SUCCESS;
  15981. }
  15982. /// Makes the unit cast the skill on the target or self if no target is specified.
  15983. ///
  15984. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15985. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15986. BUILDIN_FUNC(unitskilluseid)
  15987. {
  15988. int unit_id, target_id, casttime;
  15989. uint16 skill_id, skill_lv;
  15990. struct block_list* bl;
  15991. struct script_data *data;
  15992. unit_id = script_getnum(st,2);
  15993. data = script_getdata(st, 3);
  15994. get_val(st, data); // Convert into value in case of a variable
  15995. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15996. skill_lv = script_getnum(st,4);
  15997. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15998. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15999. if(script_rid2bl(2,bl)){
  16000. if (bl->type == BL_NPC) {
  16001. if (!((TBL_NPC*)bl)->status.hp)
  16002. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16003. else
  16004. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16005. }
  16006. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  16007. }
  16008. return SCRIPT_CMD_SUCCESS;
  16009. }
  16010. /// Makes the unit cast the skill on the target position.
  16011. ///
  16012. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16013. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16014. BUILDIN_FUNC(unitskillusepos)
  16015. {
  16016. int skill_x, skill_y, casttime;
  16017. uint16 skill_id, skill_lv;
  16018. struct block_list* bl;
  16019. struct script_data *data;
  16020. data = script_getdata(st, 3);
  16021. get_val(st, data); // Convert into value in case of a variable
  16022. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  16023. skill_lv = script_getnum(st,4);
  16024. skill_x = script_getnum(st,5);
  16025. skill_y = script_getnum(st,6);
  16026. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16027. if(script_rid2bl(2,bl)){
  16028. if (bl->type == BL_NPC) {
  16029. if (!((TBL_NPC*)bl)->status.hp)
  16030. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16031. else
  16032. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16033. }
  16034. 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));
  16035. }
  16036. return SCRIPT_CMD_SUCCESS;
  16037. }
  16038. // <--- [zBuffer] List of unit control commands
  16039. /// Pauses the execution of the script, detaching the player
  16040. ///
  16041. /// sleep <milli seconds>;
  16042. BUILDIN_FUNC(sleep)
  16043. {
  16044. // First call(by function call)
  16045. if (st->sleep.tick == 0) {
  16046. int ticks;
  16047. ticks = script_getnum(st, 2);
  16048. if (ticks <= 0) {
  16049. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  16050. return SCRIPT_CMD_FAILURE;
  16051. }
  16052. // detach the player
  16053. script_detach_rid(st);
  16054. // sleep for the target amount of time
  16055. st->state = RERUNLINE;
  16056. st->sleep.tick = ticks;
  16057. // Second call(by timer after sleeping time is over)
  16058. } else {
  16059. // Continue the script
  16060. st->state = RUN;
  16061. st->sleep.tick = 0;
  16062. }
  16063. return SCRIPT_CMD_SUCCESS;
  16064. }
  16065. /// Pauses the execution of the script, keeping the unit attached
  16066. /// Stops the script if no unit is attached
  16067. ///
  16068. /// sleep2(<milli seconds>)
  16069. BUILDIN_FUNC(sleep2)
  16070. {
  16071. // First call(by function call)
  16072. if (st->sleep.tick == 0) {
  16073. int ticks;
  16074. ticks = script_getnum(st, 2);
  16075. if (ticks <= 0) {
  16076. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  16077. return SCRIPT_CMD_FAILURE;
  16078. }
  16079. if (map_id2bl(st->rid) == NULL) {
  16080. ShowError( "buildin_sleep2: no unit is attached\n" );
  16081. return SCRIPT_CMD_FAILURE;
  16082. }
  16083. // sleep for the target amount of time
  16084. st->state = RERUNLINE;
  16085. st->sleep.tick = ticks;
  16086. // Second call(by timer after sleeping time is over)
  16087. } else {
  16088. // Check if the unit is still attached
  16089. // NOTE: This should never happen, since run_script_timer already checks this
  16090. if (map_id2bl(st->rid) == NULL) {
  16091. // The unit is not attached anymore - terminate the script
  16092. st->rid = 0;
  16093. st->state = END;
  16094. } else {
  16095. // The unit is still attached - continue the script
  16096. st->state = RUN;
  16097. st->sleep.tick = 0;
  16098. }
  16099. }
  16100. return SCRIPT_CMD_SUCCESS;
  16101. }
  16102. /// Awakes all the sleep timers of the target npc
  16103. ///
  16104. /// awake "<npc name>";
  16105. BUILDIN_FUNC(awake)
  16106. {
  16107. DBIterator *iter;
  16108. struct script_state *tst;
  16109. struct npc_data* nd;
  16110. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  16111. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  16112. return SCRIPT_CMD_FAILURE;
  16113. }
  16114. iter = db_iterator(st_db);
  16115. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  16116. if (tst->oid == nd->bl.id) {
  16117. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  16118. continue;
  16119. }
  16120. delete_timer(tst->sleep.timer, run_script_timer);
  16121. // Trigger the timer function
  16122. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  16123. }
  16124. }
  16125. dbi_destroy(iter);
  16126. return SCRIPT_CMD_SUCCESS;
  16127. }
  16128. /// Returns a reference to a variable of the target NPC.
  16129. /// Returns 0 if an error occurs.
  16130. ///
  16131. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  16132. BUILDIN_FUNC(getvariableofnpc)
  16133. {
  16134. struct script_data* data;
  16135. const char* name;
  16136. struct npc_data* nd;
  16137. data = script_getdata(st,2);
  16138. if( !data_isreference(data) )
  16139. {// Not a reference (aka varaible name)
  16140. ShowError("buildin_getvariableofnpc: not a variable\n");
  16141. script_reportdata(data);
  16142. script_pushnil(st);
  16143. st->state = END;
  16144. return SCRIPT_CMD_FAILURE;
  16145. }
  16146. name = reference_getname(data);
  16147. if( *name != '.' || name[1] == '@' )
  16148. {// not a npc variable
  16149. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  16150. script_reportdata(data);
  16151. script_pushnil(st);
  16152. st->state = END;
  16153. return SCRIPT_CMD_FAILURE;
  16154. }
  16155. nd = npc_name2id(script_getstr(st,3));
  16156. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  16157. {// NPC not found or has no script
  16158. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  16159. script_pushnil(st);
  16160. st->state = END;
  16161. return SCRIPT_CMD_FAILURE;
  16162. }
  16163. if (!nd->u.scr.script->local.vars)
  16164. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  16165. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  16166. return SCRIPT_CMD_SUCCESS;
  16167. }
  16168. /// Opens a warp portal.
  16169. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  16170. ///
  16171. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  16172. ///
  16173. /// @author blackhole89
  16174. BUILDIN_FUNC(warpportal)
  16175. {
  16176. int spx;
  16177. int spy;
  16178. unsigned short mapindex;
  16179. int tpx;
  16180. int tpy;
  16181. struct skill_unit_group* group;
  16182. struct block_list* bl;
  16183. bl = map_id2bl(st->oid);
  16184. if( bl == NULL ) {
  16185. ShowError("buildin_warpportal: NPC is needed\n");
  16186. return SCRIPT_CMD_FAILURE;
  16187. }
  16188. spx = script_getnum(st,2);
  16189. spy = script_getnum(st,3);
  16190. mapindex = mapindex_name2id(script_getstr(st, 4));
  16191. tpx = script_getnum(st,5);
  16192. tpy = script_getnum(st,6);
  16193. if( mapindex == 0 ) {
  16194. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  16195. return SCRIPT_CMD_FAILURE;
  16196. }
  16197. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  16198. if( group == NULL )
  16199. return SCRIPT_CMD_FAILURE;// failed
  16200. group->val1 = (group->val1<<16)|(short)0;
  16201. group->val2 = (tpx<<16) | tpy;
  16202. group->val3 = mapindex;
  16203. return SCRIPT_CMD_SUCCESS;
  16204. }
  16205. /**
  16206. * openmail({<char_id>});
  16207. **/
  16208. BUILDIN_FUNC(openmail)
  16209. {
  16210. TBL_PC* sd;
  16211. if (!script_charid2sd(2,sd))
  16212. return SCRIPT_CMD_FAILURE;
  16213. mail_openmail(sd);
  16214. return SCRIPT_CMD_SUCCESS;
  16215. }
  16216. /**
  16217. * openauction({<char_id>});
  16218. **/
  16219. BUILDIN_FUNC(openauction)
  16220. {
  16221. TBL_PC* sd;
  16222. if (!script_charid2sd(2,sd))
  16223. return SCRIPT_CMD_FAILURE;
  16224. if( !battle_config.feature_auction ) {
  16225. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  16226. return SCRIPT_CMD_SUCCESS;
  16227. }
  16228. clif_Auction_openwindow(sd);
  16229. return SCRIPT_CMD_SUCCESS;
  16230. }
  16231. /// Retrieves the value of the specified flag of the specified cell.
  16232. ///
  16233. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  16234. ///
  16235. /// @see cell_chk* constants in const.txt for the types
  16236. BUILDIN_FUNC(checkcell)
  16237. {
  16238. int16 m = map_mapname2mapid(script_getstr(st,2));
  16239. int16 x = script_getnum(st,3);
  16240. int16 y = script_getnum(st,4);
  16241. cell_chk type = (cell_chk)script_getnum(st,5);
  16242. script_pushint(st, map_getcell(m, x, y, type));
  16243. return SCRIPT_CMD_SUCCESS;
  16244. }
  16245. /// Modifies flags of cells in the specified area.
  16246. ///
  16247. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16248. ///
  16249. /// @see cell_* constants in const.txt for the types
  16250. BUILDIN_FUNC(setcell)
  16251. {
  16252. int16 m = map_mapname2mapid(script_getstr(st,2));
  16253. int16 x1 = script_getnum(st,3);
  16254. int16 y1 = script_getnum(st,4);
  16255. int16 x2 = script_getnum(st,5);
  16256. int16 y2 = script_getnum(st,6);
  16257. cell_t type = (cell_t)script_getnum(st,7);
  16258. bool flag = script_getnum(st,8) != 0;
  16259. int x,y;
  16260. if( x1 > x2 ) SWAP(x1,x2);
  16261. if( y1 > y2 ) SWAP(y1,y2);
  16262. for( y = y1; y <= y2; ++y )
  16263. for( x = x1; x <= x2; ++x )
  16264. map_setcell(m, x, y, type, flag);
  16265. return SCRIPT_CMD_SUCCESS;
  16266. }
  16267. /**
  16268. * Gets a free cell in the specified area.
  16269. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  16270. */
  16271. BUILDIN_FUNC(getfreecell)
  16272. {
  16273. const char *mapn = script_getstr(st, 2), *name;
  16274. char prefix;
  16275. struct map_session_data *sd;
  16276. int64 num;
  16277. int16 m, x = 0, y = 0;
  16278. int rx = -1, ry = -1, flag = 1;
  16279. sd = map_id2sd(st->rid);
  16280. if (!data_isreference(script_getdata(st, 3))) {
  16281. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  16282. script_pushint(st, -1);
  16283. return SCRIPT_CMD_FAILURE;
  16284. }
  16285. if (!data_isreference(script_getdata(st, 4))) {
  16286. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  16287. script_pushint(st, -1);
  16288. return SCRIPT_CMD_FAILURE;
  16289. }
  16290. if (is_string_variable(reference_getname(script_getdata(st, 3)))) {
  16291. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  16292. script_pushint(st, -1);
  16293. return SCRIPT_CMD_FAILURE;
  16294. }
  16295. if (is_string_variable(reference_getname(script_getdata(st, 4)))) {
  16296. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  16297. script_pushint(st, -1);
  16298. return SCRIPT_CMD_FAILURE;
  16299. }
  16300. if (script_hasdata(st, 5))
  16301. x = script_getnum(st, 5);
  16302. if (script_hasdata(st, 6))
  16303. y = script_getnum(st, 6);
  16304. if (script_hasdata(st, 7))
  16305. rx = script_getnum(st, 7);
  16306. if (script_hasdata(st, 8))
  16307. ry = script_getnum(st, 8);
  16308. if (script_hasdata(st, 9))
  16309. flag = script_getnum(st, 9);
  16310. if (sd && strcmp(mapn, "this") == 0)
  16311. m = sd->bl.m;
  16312. else
  16313. m = map_mapname2mapid(mapn);
  16314. map_search_freecell(NULL, m, &x, &y, rx, ry, flag);
  16315. // Set MapX
  16316. num = st->stack->stack_data[st->start + 3].u.num;
  16317. name = get_str(num&0x00ffffff);
  16318. prefix = *name;
  16319. if (not_server_variable(prefix)){
  16320. if( !script_rid2sd(sd) ){
  16321. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  16322. return SCRIPT_CMD_FAILURE;
  16323. }
  16324. }else
  16325. sd = NULL;
  16326. set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)x), script_getref(st, 3));
  16327. // Set MapY
  16328. num = st->stack->stack_data[st->start + 4].u.num;
  16329. name = get_str(num&0x00ffffff);
  16330. prefix = *name;
  16331. if (not_server_variable(prefix)){
  16332. if( !script_rid2sd(sd) ){
  16333. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  16334. return SCRIPT_CMD_FAILURE;
  16335. }
  16336. }else
  16337. sd = NULL;
  16338. set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)y), script_getref(st, 4));
  16339. return SCRIPT_CMD_SUCCESS;
  16340. }
  16341. /*==========================================
  16342. * Mercenary Commands
  16343. *------------------------------------------*/
  16344. BUILDIN_FUNC(mercenary_create)
  16345. {
  16346. struct map_session_data *sd;
  16347. int class_, contract_time;
  16348. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  16349. return SCRIPT_CMD_SUCCESS;
  16350. class_ = script_getnum(st,2);
  16351. if( !mercenary_db(class_) )
  16352. return SCRIPT_CMD_SUCCESS;
  16353. contract_time = script_getnum(st,3);
  16354. mercenary_create(sd, class_, contract_time);
  16355. return SCRIPT_CMD_SUCCESS;
  16356. }
  16357. BUILDIN_FUNC(mercenary_heal)
  16358. {
  16359. struct map_session_data *sd;
  16360. int hp, sp;
  16361. if( !script_rid2sd(sd) || sd->md == NULL )
  16362. return SCRIPT_CMD_SUCCESS;
  16363. hp = script_getnum(st,2);
  16364. sp = script_getnum(st,3);
  16365. status_heal(&sd->md->bl, hp, sp, 0);
  16366. return SCRIPT_CMD_SUCCESS;
  16367. }
  16368. BUILDIN_FUNC(mercenary_sc_start)
  16369. {
  16370. struct map_session_data *sd;
  16371. enum sc_type type;
  16372. int tick, val1;
  16373. if( !script_rid2sd(sd) || sd->md == NULL )
  16374. return SCRIPT_CMD_SUCCESS;
  16375. type = (sc_type)script_getnum(st,2);
  16376. tick = script_getnum(st,3);
  16377. val1 = script_getnum(st,4);
  16378. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16379. return SCRIPT_CMD_SUCCESS;
  16380. }
  16381. BUILDIN_FUNC(mercenary_get_calls)
  16382. {
  16383. struct map_session_data *sd;
  16384. int guild;
  16385. if( !script_rid2sd(sd) )
  16386. return SCRIPT_CMD_SUCCESS;
  16387. guild = script_getnum(st,2);
  16388. switch( guild )
  16389. {
  16390. case ARCH_MERC_GUILD:
  16391. script_pushint(st,sd->status.arch_calls);
  16392. break;
  16393. case SPEAR_MERC_GUILD:
  16394. script_pushint(st,sd->status.spear_calls);
  16395. break;
  16396. case SWORD_MERC_GUILD:
  16397. script_pushint(st,sd->status.sword_calls);
  16398. break;
  16399. default:
  16400. script_pushint(st,0);
  16401. break;
  16402. }
  16403. return SCRIPT_CMD_SUCCESS;
  16404. }
  16405. BUILDIN_FUNC(mercenary_set_calls)
  16406. {
  16407. struct map_session_data *sd;
  16408. int guild, value, *calls;
  16409. if( !script_rid2sd(sd) )
  16410. return SCRIPT_CMD_SUCCESS;
  16411. guild = script_getnum(st,2);
  16412. value = script_getnum(st,3);
  16413. switch( guild )
  16414. {
  16415. case ARCH_MERC_GUILD:
  16416. calls = &sd->status.arch_calls;
  16417. break;
  16418. case SPEAR_MERC_GUILD:
  16419. calls = &sd->status.spear_calls;
  16420. break;
  16421. case SWORD_MERC_GUILD:
  16422. calls = &sd->status.sword_calls;
  16423. break;
  16424. default:
  16425. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16426. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16427. }
  16428. *calls += value;
  16429. *calls = cap_value(*calls, 0, INT_MAX);
  16430. return SCRIPT_CMD_SUCCESS;
  16431. }
  16432. BUILDIN_FUNC(mercenary_get_faith)
  16433. {
  16434. struct map_session_data *sd;
  16435. int guild;
  16436. if( !script_rid2sd(sd) )
  16437. return SCRIPT_CMD_SUCCESS;
  16438. guild = script_getnum(st,2);
  16439. switch( guild )
  16440. {
  16441. case ARCH_MERC_GUILD:
  16442. script_pushint(st,sd->status.arch_faith);
  16443. break;
  16444. case SPEAR_MERC_GUILD:
  16445. script_pushint(st,sd->status.spear_faith);
  16446. break;
  16447. case SWORD_MERC_GUILD:
  16448. script_pushint(st,sd->status.sword_faith);
  16449. break;
  16450. default:
  16451. script_pushint(st,0);
  16452. break;
  16453. }
  16454. return SCRIPT_CMD_SUCCESS;
  16455. }
  16456. BUILDIN_FUNC(mercenary_set_faith)
  16457. {
  16458. struct map_session_data *sd;
  16459. int guild, value, *calls;
  16460. if( !script_rid2sd(sd) )
  16461. return SCRIPT_CMD_SUCCESS;
  16462. guild = script_getnum(st,2);
  16463. value = script_getnum(st,3);
  16464. switch( guild )
  16465. {
  16466. case ARCH_MERC_GUILD:
  16467. calls = &sd->status.arch_faith;
  16468. break;
  16469. case SPEAR_MERC_GUILD:
  16470. calls = &sd->status.spear_faith;
  16471. break;
  16472. case SWORD_MERC_GUILD:
  16473. calls = &sd->status.sword_faith;
  16474. break;
  16475. default:
  16476. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16477. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16478. }
  16479. *calls += value;
  16480. *calls = cap_value(*calls, 0, INT_MAX);
  16481. if( mercenary_get_guild(sd->md) == guild )
  16482. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16483. return SCRIPT_CMD_SUCCESS;
  16484. }
  16485. /*------------------------------------------
  16486. * Book Reading
  16487. *------------------------------------------*/
  16488. BUILDIN_FUNC(readbook)
  16489. {
  16490. struct map_session_data *sd;
  16491. int book_id, page;
  16492. if( !script_rid2sd(sd) )
  16493. return SCRIPT_CMD_SUCCESS;
  16494. book_id = script_getnum(st,2);
  16495. page = script_getnum(st,3);
  16496. clif_readbook(sd->fd, book_id, page);
  16497. return SCRIPT_CMD_SUCCESS;
  16498. }
  16499. /******************
  16500. Questlog script commands
  16501. *******************/
  16502. /**
  16503. * Add job criteria to questinfo
  16504. * @param qi Quest Info
  16505. * @param job
  16506. * @author [Cydh]
  16507. **/
  16508. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16509. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16510. qi->jobid[qi->jobid_count++] = job;
  16511. }
  16512. /**
  16513. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16514. **/
  16515. BUILDIN_FUNC(questinfo)
  16516. {
  16517. TBL_NPC* nd = map_id2nd(st->oid);
  16518. int quest_id, icon, color = 0;
  16519. struct questinfo qi, *q2;
  16520. if( nd == NULL || nd->bl.m == -1 ) {
  16521. ShowError("buildin_questinfo: No NPC attached.\n");
  16522. return SCRIPT_CMD_FAILURE;
  16523. }
  16524. quest_id = script_getnum(st, 2);
  16525. icon = script_getnum(st, 3);
  16526. #if PACKETVER >= 20120410
  16527. switch(icon){
  16528. case QTYPE_QUEST:
  16529. case QTYPE_QUEST2:
  16530. case QTYPE_JOB:
  16531. case QTYPE_JOB2:
  16532. case QTYPE_EVENT:
  16533. case QTYPE_EVENT2:
  16534. case QTYPE_WARG:
  16535. case QTYPE_WARG2:
  16536. // Leave everything as it is
  16537. break;
  16538. case QTYPE_NONE:
  16539. default:
  16540. // Default to nothing if icon id is invalid.
  16541. icon = QTYPE_NONE;
  16542. break;
  16543. }
  16544. #else
  16545. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16546. icon = QTYPE_QUEST;
  16547. else
  16548. icon = icon + 1;
  16549. #endif
  16550. qi.quest_id = quest_id;
  16551. qi.icon = (unsigned char)icon;
  16552. qi.nd = nd;
  16553. if( script_hasdata(st, 4) ) {
  16554. color = script_getnum(st, 4);
  16555. if( color < 0 || color > 3 ) {
  16556. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16557. script_reportfunc(st);
  16558. color = 0;
  16559. }
  16560. }
  16561. qi.color = (unsigned char)color;
  16562. qi.min_level = 1;
  16563. qi.max_level = MAX_LEVEL;
  16564. q2 = map_add_questinfo(nd->bl.m, &qi);
  16565. q2->req = NULL;
  16566. q2->req_count = 0;
  16567. q2->jobid = NULL;
  16568. q2->jobid_count = 0;
  16569. if(script_hasdata(st, 5)) {
  16570. int job = script_getnum(st, 5);
  16571. if (!pcdb_checkid(job))
  16572. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16573. else {
  16574. buildin_questinfo_setjob(q2, job);
  16575. }
  16576. }
  16577. return SCRIPT_CMD_SUCCESS;
  16578. }
  16579. /**
  16580. * setquest <ID>{,<char_id>};
  16581. **/
  16582. BUILDIN_FUNC(setquest)
  16583. {
  16584. struct map_session_data *sd;
  16585. int quest_id;
  16586. quest_id = script_getnum(st, 2);
  16587. if (!script_charid2sd(3,sd))
  16588. return SCRIPT_CMD_FAILURE;
  16589. if( quest_add(sd, quest_id) == -1 ){
  16590. script_reportsrc(st);
  16591. script_reportfunc(st);
  16592. }
  16593. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16594. pc_show_questinfo(sd);
  16595. return SCRIPT_CMD_SUCCESS;
  16596. }
  16597. /**
  16598. * erasequest <ID>{,<char_id>};
  16599. **/
  16600. BUILDIN_FUNC(erasequest)
  16601. {
  16602. struct map_session_data *sd;
  16603. if (!script_charid2sd(3,sd))
  16604. return SCRIPT_CMD_FAILURE;
  16605. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16606. script_reportsrc(st);
  16607. script_reportfunc(st);
  16608. }
  16609. return SCRIPT_CMD_SUCCESS;
  16610. }
  16611. /**
  16612. * completequest <ID>{,<char_id>};
  16613. **/
  16614. BUILDIN_FUNC(completequest)
  16615. {
  16616. struct map_session_data *sd;
  16617. if (!script_charid2sd(3,sd))
  16618. return SCRIPT_CMD_FAILURE;
  16619. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16620. //20120410 or 20090218
  16621. pc_show_questinfo(sd);
  16622. return SCRIPT_CMD_SUCCESS;
  16623. }
  16624. /**
  16625. * changequest <ID>,<ID2>{,<char_id>};
  16626. **/
  16627. BUILDIN_FUNC(changequest)
  16628. {
  16629. struct map_session_data *sd;
  16630. if (!script_charid2sd(4,sd))
  16631. return SCRIPT_CMD_FAILURE;
  16632. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16633. script_reportsrc(st);
  16634. script_reportfunc(st);
  16635. }
  16636. //20120410 or 20090218
  16637. pc_show_questinfo(sd);
  16638. return SCRIPT_CMD_SUCCESS;
  16639. }
  16640. /**
  16641. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16642. **/
  16643. BUILDIN_FUNC(checkquest)
  16644. {
  16645. struct map_session_data *sd;
  16646. enum quest_check_type type = HAVEQUEST;
  16647. if( script_hasdata(st, 3) )
  16648. type = (enum quest_check_type)script_getnum(st, 3);
  16649. if (!script_charid2sd(4,sd))
  16650. return SCRIPT_CMD_FAILURE;
  16651. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16652. return SCRIPT_CMD_SUCCESS;
  16653. }
  16654. /**
  16655. * isbegin_quest(<ID>{,<char_id>})
  16656. **/
  16657. BUILDIN_FUNC(isbegin_quest)
  16658. {
  16659. struct map_session_data *sd;
  16660. int i;
  16661. if (!script_charid2sd(3,sd))
  16662. return SCRIPT_CMD_FAILURE;
  16663. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16664. script_pushint(st, i + (i < 1));
  16665. return SCRIPT_CMD_SUCCESS;
  16666. }
  16667. /**
  16668. * showevent <icon>{,<mark color>{,<char_id>}}
  16669. **/
  16670. BUILDIN_FUNC(showevent)
  16671. {
  16672. TBL_PC *sd;
  16673. struct npc_data *nd = map_id2nd(st->oid);
  16674. int icon, color = 0;
  16675. if (!script_charid2sd(4,sd))
  16676. return SCRIPT_CMD_FAILURE;
  16677. if( sd == NULL || nd == NULL )
  16678. return SCRIPT_CMD_SUCCESS;
  16679. icon = script_getnum(st, 2);
  16680. if( script_hasdata(st, 3) ) {
  16681. color = script_getnum(st, 3);
  16682. if( color < 0 || color > 3 ) {
  16683. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16684. script_reportfunc(st);
  16685. color = 0;
  16686. }
  16687. }
  16688. #if PACKETVER >= 20120410
  16689. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16690. icon = 9999; // Default to nothing if icon id is invalid.
  16691. #else
  16692. if(icon < 0 || icon > 7)
  16693. icon = 0;
  16694. else
  16695. icon = icon + 1;
  16696. #endif
  16697. clif_quest_show_event(sd, &nd->bl, icon, color);
  16698. return SCRIPT_CMD_SUCCESS;
  16699. }
  16700. /*==========================================
  16701. * BattleGround System
  16702. *------------------------------------------*/
  16703. BUILDIN_FUNC(waitingroom2bg)
  16704. {
  16705. struct npc_data *nd;
  16706. struct chat_data *cd;
  16707. const char *map_name, *ev = "", *dev = "";
  16708. int x, y, mapindex = 0, bg_id;
  16709. unsigned char i,c=0;
  16710. if( script_hasdata(st,7) )
  16711. nd = npc_name2id(script_getstr(st,7));
  16712. else
  16713. nd = (struct npc_data *)map_id2bl(st->oid);
  16714. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16715. {
  16716. script_pushint(st,0);
  16717. return SCRIPT_CMD_SUCCESS;
  16718. }
  16719. map_name = script_getstr(st,2);
  16720. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16721. { // Invalid Map
  16722. script_pushint(st, 0);
  16723. return SCRIPT_CMD_SUCCESS;
  16724. }
  16725. x = script_getnum(st,3);
  16726. y = script_getnum(st,4);
  16727. if(script_hasdata(st,5))
  16728. ev = script_getstr(st,5); // Logout Event
  16729. if(script_hasdata(st,6))
  16730. dev = script_getstr(st,6); // Die Event
  16731. check_event(st, ev);
  16732. check_event(st, dev);
  16733. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16734. { // Creation failed
  16735. script_pushint(st,0);
  16736. return SCRIPT_CMD_SUCCESS;
  16737. }
  16738. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16739. struct map_session_data *sd;
  16740. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16741. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16742. ++c;
  16743. }
  16744. }
  16745. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16746. script_pushint(st,bg_id);
  16747. return SCRIPT_CMD_SUCCESS;
  16748. }
  16749. BUILDIN_FUNC(waitingroom2bg_single)
  16750. {
  16751. const char* map_name;
  16752. struct npc_data *nd;
  16753. struct chat_data *cd;
  16754. struct map_session_data *sd;
  16755. struct battleground_data *bg;
  16756. int x, y, mapindex, bg_id;
  16757. bg_id = script_getnum(st,2);
  16758. if ((bg = bg_team_search(bg_id)) == NULL) {
  16759. script_pushint(st, false);
  16760. return SCRIPT_CMD_SUCCESS;
  16761. }
  16762. if (script_hasdata(st, 3)) {
  16763. map_name = script_getstr(st, 3);
  16764. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16765. script_pushint(st, false);
  16766. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16767. }
  16768. x = script_getnum(st, 4);
  16769. y = script_getnum(st, 5);
  16770. }
  16771. else {
  16772. mapindex = bg->mapindex;
  16773. x = bg->x;
  16774. y = bg->y;
  16775. }
  16776. nd = npc_name2id(script_getstr(st,6));
  16777. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16778. return SCRIPT_CMD_SUCCESS;
  16779. if( (sd = cd->usersd[0]) == NULL )
  16780. return SCRIPT_CMD_SUCCESS;
  16781. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16782. {
  16783. script_pushint(st, true);
  16784. }
  16785. else
  16786. script_pushint(st, false);
  16787. return SCRIPT_CMD_SUCCESS;
  16788. }
  16789. /// Creates an instance of battleground battle group.
  16790. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  16791. /// @author [secretdataz]
  16792. BUILDIN_FUNC(bg_create) {
  16793. const char *map_name, *ev = "", *dev = "";
  16794. int x, y, mapindex = 0;
  16795. map_name = script_getstr(st, 2);
  16796. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16797. { // Invalid Map
  16798. script_pushint(st, 0);
  16799. return SCRIPT_CMD_SUCCESS;
  16800. }
  16801. x = script_getnum(st, 3);
  16802. y = script_getnum(st, 4);
  16803. if(script_hasdata(st, 5))
  16804. ev = script_getstr(st, 5); // Logout Event
  16805. if(script_hasdata(st, 6))
  16806. dev = script_getstr(st, 6); // Die Event
  16807. check_event(st, ev);
  16808. check_event(st, dev);
  16809. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  16810. return SCRIPT_CMD_SUCCESS;
  16811. }
  16812. /// Adds attached player or <char id> (if specified) to an existing
  16813. /// battleground group and warps it to the specified coordinates on
  16814. /// the given map.
  16815. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  16816. /// @author [secretdataz]
  16817. BUILDIN_FUNC(bg_join) {
  16818. const char* map_name;
  16819. struct map_session_data *sd;
  16820. struct battleground_data *bg;
  16821. int x, y, bg_id, mapindex;
  16822. bg_id = script_getnum(st, 2);
  16823. if ((bg = bg_team_search(bg_id)) == NULL) {
  16824. script_pushint(st, false);
  16825. return SCRIPT_CMD_SUCCESS;
  16826. }
  16827. if (script_hasdata(st, 3)) {
  16828. map_name = script_getstr(st, 3);
  16829. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16830. script_pushint(st, false);
  16831. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16832. }
  16833. x = script_getnum(st, 4);
  16834. y = script_getnum(st, 5);
  16835. } else {
  16836. mapindex = bg->mapindex;
  16837. x = bg->x;
  16838. y = bg->y;
  16839. }
  16840. if (!script_charid2sd(6, sd)) {
  16841. script_pushint(st, false);
  16842. return SCRIPT_CMD_FAILURE;
  16843. }
  16844. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16845. {
  16846. script_pushint(st, true);
  16847. }
  16848. else
  16849. script_pushint(st, false);
  16850. return SCRIPT_CMD_SUCCESS;
  16851. }
  16852. BUILDIN_FUNC(bg_team_setxy)
  16853. {
  16854. struct battleground_data *bg;
  16855. int bg_id;
  16856. bg_id = script_getnum(st,2);
  16857. if( (bg = bg_team_search(bg_id)) == NULL )
  16858. return SCRIPT_CMD_SUCCESS;
  16859. bg->x = script_getnum(st,3);
  16860. bg->y = script_getnum(st,4);
  16861. return SCRIPT_CMD_SUCCESS;
  16862. }
  16863. BUILDIN_FUNC(bg_warp)
  16864. {
  16865. int x, y, mapindex, bg_id;
  16866. const char* map_name;
  16867. bg_id = script_getnum(st,2);
  16868. map_name = script_getstr(st,3);
  16869. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16870. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16871. x = script_getnum(st,4);
  16872. y = script_getnum(st,5);
  16873. bg_team_warp(bg_id, mapindex, x, y);
  16874. return SCRIPT_CMD_SUCCESS;
  16875. }
  16876. BUILDIN_FUNC(bg_monster)
  16877. {
  16878. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16879. const char *str,*mapname, *evt="";
  16880. bg_id = script_getnum(st,2);
  16881. mapname = script_getstr(st,3);
  16882. x = script_getnum(st,4);
  16883. y = script_getnum(st,5);
  16884. str = script_getstr(st,6);
  16885. class_ = script_getnum(st,7);
  16886. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16887. check_event(st, evt);
  16888. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16889. return SCRIPT_CMD_SUCCESS;
  16890. }
  16891. BUILDIN_FUNC(bg_monster_set_team)
  16892. {
  16893. struct mob_data *md;
  16894. struct block_list *mbl;
  16895. int id = script_getnum(st,2),
  16896. bg_id = script_getnum(st,3);
  16897. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16898. return SCRIPT_CMD_SUCCESS;
  16899. md = (TBL_MOB *)mbl;
  16900. md->bg_id = bg_id;
  16901. mob_stop_attack(md);
  16902. mob_stop_walking(md, 0);
  16903. md->target_id = md->attacked_id = 0;
  16904. clif_name_area(&md->bl);
  16905. return SCRIPT_CMD_SUCCESS;
  16906. }
  16907. BUILDIN_FUNC(bg_leave)
  16908. {
  16909. struct map_session_data *sd = NULL;
  16910. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16911. return SCRIPT_CMD_SUCCESS;
  16912. bg_team_leave(sd,0);
  16913. return SCRIPT_CMD_SUCCESS;
  16914. }
  16915. BUILDIN_FUNC(bg_destroy)
  16916. {
  16917. int bg_id = script_getnum(st,2);
  16918. bg_team_delete(bg_id);
  16919. return SCRIPT_CMD_SUCCESS;
  16920. }
  16921. BUILDIN_FUNC(bg_getareausers)
  16922. {
  16923. const char *str;
  16924. int16 m, x0, y0, x1, y1;
  16925. int bg_id;
  16926. int i = 0, c = 0;
  16927. struct battleground_data *bg = NULL;
  16928. bg_id = script_getnum(st,2);
  16929. str = script_getstr(st,3);
  16930. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16931. {
  16932. script_pushint(st,0);
  16933. return SCRIPT_CMD_SUCCESS;
  16934. }
  16935. x0 = script_getnum(st,4);
  16936. y0 = script_getnum(st,5);
  16937. x1 = script_getnum(st,6);
  16938. y1 = script_getnum(st,7);
  16939. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16940. {
  16941. struct map_session_data *sd;
  16942. if( (sd = bg->members[i].sd) == NULL )
  16943. continue;
  16944. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16945. continue;
  16946. c++;
  16947. }
  16948. script_pushint(st,c);
  16949. return SCRIPT_CMD_SUCCESS;
  16950. }
  16951. BUILDIN_FUNC(bg_updatescore)
  16952. {
  16953. const char *str;
  16954. int16 m;
  16955. str = script_getstr(st,2);
  16956. if( (m = map_mapname2mapid(str)) < 0 )
  16957. return SCRIPT_CMD_SUCCESS;
  16958. map[m].bgscore_lion = script_getnum(st,3);
  16959. map[m].bgscore_eagle = script_getnum(st,4);
  16960. clif_bg_updatescore(m);
  16961. return SCRIPT_CMD_SUCCESS;
  16962. }
  16963. BUILDIN_FUNC(bg_get_data)
  16964. {
  16965. struct battleground_data *bg;
  16966. int bg_id = script_getnum(st,2),
  16967. type = script_getnum(st,3), i;
  16968. if( (bg = bg_team_search(bg_id)) == NULL )
  16969. {
  16970. script_pushint(st,0);
  16971. return SCRIPT_CMD_SUCCESS;
  16972. }
  16973. switch( type )
  16974. {
  16975. case 0: script_pushint(st, bg->count); break;
  16976. case 1:
  16977. for (i = 0; bg->members[i].sd != NULL; i++)
  16978. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), bg->members[i].sd->bl.id);
  16979. mapreg_setreg(add_str("$@arenamemberscount"), i);
  16980. script_pushint(st, i);
  16981. break;
  16982. default:
  16983. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16984. break;
  16985. }
  16986. return SCRIPT_CMD_SUCCESS;
  16987. }
  16988. /*==========================================
  16989. * Instancing System
  16990. *------------------------------------------*/
  16991. //Returns an Instance ID
  16992. //Checks NPC first, then if player is attached we check
  16993. unsigned short script_instancegetid(struct script_state* st)
  16994. {
  16995. unsigned short instance_id = 0;
  16996. struct npc_data *nd;
  16997. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16998. instance_id = nd->instance_id;
  16999. else {
  17000. struct map_session_data *sd = NULL;
  17001. struct party_data *pd = NULL;
  17002. struct guild *gd = NULL;
  17003. struct clan *cd = NULL;
  17004. if ((sd = map_id2sd(st->rid))) {
  17005. if (sd->instance_id)
  17006. instance_id = sd->instance_id;
  17007. if (instance_id == 0 && sd->status.party_id && (pd = party_search(sd->status.party_id)) != NULL && pd->instance_id)
  17008. instance_id = pd->instance_id;
  17009. if (instance_id == 0 && sd->status.guild_id && (gd = guild_search(sd->status.guild_id)) != NULL && gd->instance_id)
  17010. instance_id = gd->instance_id;
  17011. if (instance_id == 0 && sd->status.clan_id && (cd = clan_search(sd->status.clan_id)) != NULL && cd->instance_id)
  17012. instance_id = cd->instance_id;
  17013. }
  17014. }
  17015. return instance_id;
  17016. }
  17017. /*==========================================
  17018. * Creates the instance
  17019. * Returns Instance ID if created successfully
  17020. *------------------------------------------*/
  17021. BUILDIN_FUNC(instance_create)
  17022. {
  17023. enum instance_mode mode = IM_PARTY;
  17024. int owner_id = 0;
  17025. if (script_hasdata(st, 3)) {
  17026. mode = static_cast<instance_mode>(script_getnum(st, 3));
  17027. if (mode < IM_NONE || mode >= IM_MAX) {
  17028. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  17029. return SCRIPT_CMD_FAILURE;
  17030. }
  17031. }
  17032. if (script_hasdata(st, 4))
  17033. owner_id = script_getnum(st, 4);
  17034. else {
  17035. // If sd is NULL, instance_create will return -2.
  17036. struct map_session_data *sd = NULL;
  17037. switch(mode) {
  17038. case IM_NONE:
  17039. owner_id = st->oid;
  17040. break;
  17041. case IM_CHAR:
  17042. if (script_rid2sd(sd))
  17043. owner_id = sd->status.char_id;
  17044. break;
  17045. case IM_PARTY:
  17046. if (script_rid2sd(sd))
  17047. owner_id = sd->status.party_id;
  17048. break;
  17049. case IM_GUILD:
  17050. if (script_rid2sd(sd))
  17051. owner_id = sd->status.guild_id;
  17052. break;
  17053. case IM_CLAN:
  17054. if (script_rid2sd(sd))
  17055. owner_id = sd->status.clan_id;
  17056. break;
  17057. default:
  17058. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  17059. return SCRIPT_CMD_FAILURE;
  17060. }
  17061. }
  17062. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  17063. return SCRIPT_CMD_SUCCESS;
  17064. }
  17065. /*==========================================
  17066. * Destroys an instance (unofficial)
  17067. * Officially instances are only destroyed by timeout
  17068. *
  17069. * instance_destroy {<instance_id>};
  17070. *------------------------------------------*/
  17071. BUILDIN_FUNC(instance_destroy)
  17072. {
  17073. unsigned short instance_id;
  17074. if( script_hasdata(st,2) )
  17075. instance_id = script_getnum(st,2);
  17076. else
  17077. instance_id = script_instancegetid(st);
  17078. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  17079. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  17080. return SCRIPT_CMD_FAILURE;
  17081. }
  17082. instance_destroy(instance_id);
  17083. return SCRIPT_CMD_SUCCESS;
  17084. }
  17085. /*==========================================
  17086. * Warps player to instance
  17087. * Results:
  17088. * IE_OK: Success
  17089. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  17090. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  17091. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  17092. *------------------------------------------*/
  17093. BUILDIN_FUNC(instance_enter)
  17094. {
  17095. struct map_session_data *sd = NULL;
  17096. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  17097. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  17098. unsigned short instance_id;
  17099. if (script_hasdata(st, 6))
  17100. instance_id = script_getnum(st, 6);
  17101. else
  17102. instance_id = script_instancegetid(st);
  17103. if (!script_charid2sd(5,sd))
  17104. return SCRIPT_CMD_FAILURE;
  17105. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  17106. return SCRIPT_CMD_SUCCESS;
  17107. }
  17108. /*==========================================
  17109. * Returns the name of a duplicated NPC
  17110. *
  17111. * instance_npcname <npc_name>{,<instance_id>};
  17112. * <npc_name> is the full name of an NPC.
  17113. *------------------------------------------*/
  17114. BUILDIN_FUNC(instance_npcname)
  17115. {
  17116. const char *str;
  17117. unsigned short instance_id = 0;
  17118. struct npc_data *nd;
  17119. str = script_getstr(st,2);
  17120. if( script_hasdata(st,3) )
  17121. instance_id = script_getnum(st,3);
  17122. else
  17123. instance_id = script_instancegetid(st);
  17124. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  17125. static char npcname[NAME_LENGTH];
  17126. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  17127. script_pushconststr(st,npcname);
  17128. } else {
  17129. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  17130. st->state = END;
  17131. return SCRIPT_CMD_FAILURE;
  17132. }
  17133. return SCRIPT_CMD_SUCCESS;
  17134. }
  17135. /*==========================================
  17136. * Returns the name of a duplicated map
  17137. *
  17138. * instance_mapname <map_name>{,<instance_id>};
  17139. *------------------------------------------*/
  17140. BUILDIN_FUNC(instance_mapname)
  17141. {
  17142. const char *str;
  17143. int16 m;
  17144. unsigned short instance_id = 0;
  17145. str = script_getstr(st,2);
  17146. if( script_hasdata(st,3) )
  17147. instance_id = script_getnum(st,3);
  17148. else
  17149. instance_id = script_instancegetid(st);
  17150. // Check that instance mapname is a valid map
  17151. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  17152. script_pushconststr(st, "");
  17153. else
  17154. script_pushconststr(st, map[m].name);
  17155. return SCRIPT_CMD_SUCCESS;
  17156. }
  17157. /*==========================================
  17158. * Returns an Instance ID
  17159. *------------------------------------------*/
  17160. BUILDIN_FUNC(instance_id)
  17161. {
  17162. script_pushint(st, script_instancegetid(st));
  17163. return SCRIPT_CMD_SUCCESS;
  17164. }
  17165. /*==========================================
  17166. * Warps all players inside an instance
  17167. *
  17168. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  17169. *------------------------------------------*/
  17170. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  17171. {
  17172. unsigned int m = va_arg(ap,unsigned int);
  17173. int x = va_arg(ap,int);
  17174. int y = va_arg(ap,int);
  17175. unsigned short instance_id = va_arg(ap,unsigned int);
  17176. struct map_session_data *sd = NULL;
  17177. int owner_id = 0;
  17178. nullpo_retr(0, bl);
  17179. if (bl->type != BL_PC)
  17180. return 0;
  17181. sd = (TBL_PC *)bl;
  17182. owner_id = instance_data[instance_id].owner_id;
  17183. switch(instance_data[instance_id].mode) {
  17184. case IM_NONE:
  17185. break;
  17186. case IM_CHAR:
  17187. if (sd->status.char_id != owner_id)
  17188. return 0;
  17189. break;
  17190. case IM_PARTY:
  17191. if (sd->status.party_id != owner_id)
  17192. return 0;
  17193. break;
  17194. case IM_GUILD:
  17195. if (sd->status.guild_id != owner_id)
  17196. return 0;
  17197. case IM_CLAN:
  17198. if (sd->status.clan_id != owner_id)
  17199. return 0;
  17200. }
  17201. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  17202. return 1;
  17203. }
  17204. BUILDIN_FUNC(instance_warpall)
  17205. {
  17206. int16 m, i;
  17207. unsigned short instance_id;
  17208. const char *mapn;
  17209. int x, y;
  17210. mapn = script_getstr(st,2);
  17211. x = script_getnum(st,3);
  17212. y = script_getnum(st,4);
  17213. if( script_hasdata(st,5) )
  17214. instance_id = script_getnum(st,5);
  17215. else
  17216. instance_id = script_instancegetid(st);
  17217. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  17218. return SCRIPT_CMD_FAILURE;
  17219. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  17220. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  17221. return SCRIPT_CMD_SUCCESS;
  17222. }
  17223. /*==========================================
  17224. * Broadcasts to all maps inside an instance
  17225. *
  17226. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  17227. * Using 0 for <instance id> will auto-detect the id.
  17228. *------------------------------------------*/
  17229. BUILDIN_FUNC(instance_announce) {
  17230. unsigned short instance_id = script_getnum(st,2);
  17231. const char *mes = script_getstr(st,3);
  17232. int flag = script_getnum(st,4);
  17233. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  17234. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  17235. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  17236. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  17237. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  17238. int i;
  17239. if( instance_id == 0 ) {
  17240. instance_id = script_instancegetid(st);
  17241. }
  17242. if( !instance_id && &instance_data[instance_id] != NULL) {
  17243. ShowError("buildin_instance_announce: Intance is not found.\n");
  17244. return SCRIPT_CMD_FAILURE;
  17245. }
  17246. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  17247. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  17248. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  17249. return SCRIPT_CMD_SUCCESS;
  17250. }
  17251. /*==========================================
  17252. * instance_check_party [malufett]
  17253. * Values:
  17254. * party_id : Party ID of the invoking character. [Required Parameter]
  17255. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  17256. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17257. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17258. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  17259. * Example 2: instance_check_party (getcharid(1),1,1,99);
  17260. *------------------------------------------*/
  17261. BUILDIN_FUNC(instance_check_party)
  17262. {
  17263. int amount, min, max, i, party_id, c = 0;
  17264. struct party_data *p = NULL;
  17265. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  17266. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17267. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17268. if( min < 1 || min > MAX_LEVEL) {
  17269. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  17270. return SCRIPT_CMD_FAILURE;
  17271. } else if( max < 1 || max > MAX_LEVEL) {
  17272. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  17273. return SCRIPT_CMD_FAILURE;
  17274. }
  17275. if( script_hasdata(st,2) )
  17276. party_id = script_getnum(st,2);
  17277. else return SCRIPT_CMD_FAILURE;
  17278. if( !(p = party_search(party_id)) ) {
  17279. script_pushint(st, 0); // Returns false if party does not exist.
  17280. return SCRIPT_CMD_FAILURE;
  17281. }
  17282. for( i = 0; i < MAX_PARTY; i++ ) {
  17283. struct map_session_data *pl_sd;
  17284. if( (pl_sd = p->data[i].sd) )
  17285. if(map_id2bl(pl_sd->bl.id)) {
  17286. if(pl_sd->status.base_level < min) {
  17287. script_pushint(st, 0);
  17288. return SCRIPT_CMD_SUCCESS;
  17289. } else if(pl_sd->status.base_level > max) {
  17290. script_pushint(st, 0);
  17291. return SCRIPT_CMD_SUCCESS;
  17292. }
  17293. c++;
  17294. }
  17295. }
  17296. if(c < amount)
  17297. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  17298. else
  17299. script_pushint(st, 1);
  17300. return SCRIPT_CMD_SUCCESS;
  17301. }
  17302. /*==========================================
  17303. * instance_check_guild
  17304. * Values:
  17305. * guild_id : Guild ID of the invoking character. [Required Parameter]
  17306. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  17307. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17308. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17309. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  17310. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  17311. *------------------------------------------*/
  17312. BUILDIN_FUNC(instance_check_guild)
  17313. {
  17314. int amount, min, max, i, guild_id = 0, c = 0;
  17315. struct guild *g = NULL;
  17316. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  17317. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17318. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17319. if (min < 1 || min > MAX_LEVEL) {
  17320. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  17321. return SCRIPT_CMD_FAILURE;
  17322. } else if (max < 1 || max > MAX_LEVEL) {
  17323. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  17324. return SCRIPT_CMD_FAILURE;
  17325. }
  17326. if (script_hasdata(st,2))
  17327. guild_id = script_getnum(st,2);
  17328. else
  17329. return SCRIPT_CMD_FAILURE;
  17330. if (!(g = guild_search(guild_id))) {
  17331. script_pushint(st, 0); // Returns false if guild does not exist.
  17332. return SCRIPT_CMD_FAILURE;
  17333. }
  17334. for(i = 0; i < MAX_GUILD; i++) {
  17335. struct map_session_data *pl_sd;
  17336. if ((pl_sd = g->member[i].sd)) {
  17337. if (map_id2bl(pl_sd->bl.id)) {
  17338. if (pl_sd->status.base_level < min) {
  17339. script_pushint(st, 0);
  17340. return SCRIPT_CMD_SUCCESS;
  17341. } else if (pl_sd->status.base_level > max) {
  17342. script_pushint(st, 0);
  17343. return SCRIPT_CMD_SUCCESS;
  17344. }
  17345. c++;
  17346. }
  17347. }
  17348. }
  17349. if (c < amount)
  17350. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  17351. else
  17352. script_pushint(st, 1);
  17353. return SCRIPT_CMD_SUCCESS;
  17354. }
  17355. /*==========================================
  17356. * instance_check_clan
  17357. * Values:
  17358. * clan_id : Clan ID of the invoking character. [Required Parameter]
  17359. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  17360. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17361. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17362. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  17363. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  17364. *------------------------------------------*/
  17365. BUILDIN_FUNC(instance_check_clan)
  17366. {
  17367. int amount, min, max, i, clan_id = 0, c = 0;
  17368. struct clan *cd = NULL;
  17369. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  17370. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17371. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17372. if (min < 1 || min > MAX_LEVEL) {
  17373. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  17374. return SCRIPT_CMD_FAILURE;
  17375. } else if (max < 1 || max > MAX_LEVEL) {
  17376. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  17377. return SCRIPT_CMD_FAILURE;
  17378. }
  17379. if (script_hasdata(st,2))
  17380. clan_id = script_getnum(st,2);
  17381. else
  17382. return SCRIPT_CMD_FAILURE;
  17383. if (!(cd = clan_search(clan_id))) {
  17384. script_pushint(st, 0); // Returns false if clan does not exist.
  17385. return SCRIPT_CMD_FAILURE;
  17386. }
  17387. for(i = 0; i < MAX_CLAN; i++) {
  17388. struct map_session_data *pl_sd;
  17389. if ((pl_sd = cd->members[i])) {
  17390. if (map_id2bl(pl_sd->bl.id)) {
  17391. if (pl_sd->status.base_level < min) {
  17392. script_pushint(st, 0);
  17393. return SCRIPT_CMD_SUCCESS;
  17394. } else if (pl_sd->status.base_level > max) {
  17395. script_pushint(st, 0);
  17396. return SCRIPT_CMD_SUCCESS;
  17397. }
  17398. c++;
  17399. }
  17400. }
  17401. }
  17402. if (c < amount)
  17403. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  17404. else
  17405. script_pushint(st, 1);
  17406. return SCRIPT_CMD_SUCCESS;
  17407. }
  17408. /*==========================================
  17409. * instance_info
  17410. * Values:
  17411. * name : name of the instance you want to look up. [Required Parameter]
  17412. * type : type of information you want to look up for the specified instance. [Required Parameter]
  17413. * index : index of the map in the instance. [Optional Parameter]
  17414. *------------------------------------------*/
  17415. BUILDIN_FUNC(instance_info)
  17416. {
  17417. const char* name = script_getstr(st, 2);
  17418. int type = script_getnum(st, 3);
  17419. int index = 0;
  17420. struct instance_db *db = instance_searchname_db(name);
  17421. if( !db ){
  17422. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  17423. script_pushint(st, -1);
  17424. return SCRIPT_CMD_FAILURE;
  17425. }
  17426. switch( type ){
  17427. case IIT_ID:
  17428. script_pushint(st, db->id);
  17429. break;
  17430. case IIT_TIME_LIMIT:
  17431. script_pushint(st, db->limit);
  17432. break;
  17433. case IIT_IDLE_TIMEOUT:
  17434. script_pushint(st, db->timeout);
  17435. break;
  17436. case IIT_ENTER_MAP:
  17437. script_pushstrcopy(st, StringBuf_Value(db->enter.mapname));
  17438. break;
  17439. case IIT_ENTER_X:
  17440. script_pushint(st, db->enter.x);
  17441. break;
  17442. case IIT_ENTER_Y:
  17443. script_pushint(st, db->enter.y);
  17444. break;
  17445. case IIT_MAPCOUNT:
  17446. script_pushint(st, db->maplist_count);
  17447. break;
  17448. case IIT_MAP:
  17449. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  17450. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  17451. script_pushconststr(st, "");
  17452. return SCRIPT_CMD_FAILURE;
  17453. }
  17454. index = script_getnum(st, 4);
  17455. if( index < 0 ){
  17456. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  17457. script_pushconststr(st, "");
  17458. return SCRIPT_CMD_FAILURE;
  17459. }
  17460. if( index > UINT8_MAX ){
  17461. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  17462. script_pushconststr(st, "");
  17463. return SCRIPT_CMD_FAILURE;
  17464. }
  17465. script_pushstrcopy(st, StringBuf_Value(db->maplist[index]));
  17466. break;
  17467. default:
  17468. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  17469. script_pushint(st, -1);
  17470. return SCRIPT_CMD_FAILURE;
  17471. }
  17472. return SCRIPT_CMD_SUCCESS;
  17473. }
  17474. /*==========================================
  17475. * Custom Fonts
  17476. *------------------------------------------*/
  17477. BUILDIN_FUNC(setfont)
  17478. {
  17479. struct map_session_data *sd;
  17480. int font;
  17481. if( !script_rid2sd(sd) )
  17482. return SCRIPT_CMD_SUCCESS;
  17483. font = script_getnum(st,2);
  17484. if( sd->status.font != font )
  17485. sd->status.font = font;
  17486. else
  17487. sd->status.font = 0;
  17488. clif_font(sd);
  17489. return SCRIPT_CMD_SUCCESS;
  17490. }
  17491. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17492. {
  17493. TBL_MOB* md = (TBL_MOB*)bl;
  17494. struct block_list *tbl;
  17495. int mobid = va_arg(ap,int);
  17496. uint16 skill_id = va_arg(ap,int);
  17497. uint16 skill_lv = va_arg(ap,int);
  17498. int casttime = va_arg(ap,int);
  17499. int cancel = va_arg(ap,int);
  17500. int emotion = va_arg(ap,int);
  17501. int target = va_arg(ap,int);
  17502. if( md->mob_id != mobid )
  17503. return 0;
  17504. // 0:self, 1:target, 2:master, default:random
  17505. switch( target ) {
  17506. case 0: tbl = map_id2bl(md->bl.id); break;
  17507. case 1: tbl = map_id2bl(md->target_id); break;
  17508. case 2: tbl = map_id2bl(md->master_id); break;
  17509. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17510. }
  17511. if( !tbl )
  17512. return 0;
  17513. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17514. unit_skillcastcancel(bl,0);
  17515. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17516. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17517. else
  17518. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17519. clif_emotion(&md->bl, emotion);
  17520. return 1;
  17521. }
  17522. /*==========================================
  17523. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17524. *------------------------------------------*/
  17525. BUILDIN_FUNC(areamobuseskill)
  17526. {
  17527. struct block_list center;
  17528. struct script_data *data;
  17529. int16 m;
  17530. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17531. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17532. ShowError("areamobuseskill: invalid map name.\n");
  17533. return SCRIPT_CMD_SUCCESS;
  17534. }
  17535. center.m = m;
  17536. center.x = script_getnum(st,3);
  17537. center.y = script_getnum(st,4);
  17538. range = script_getnum(st,5);
  17539. mobid = script_getnum(st,6);
  17540. data = script_getdata(st, 7);
  17541. get_val(st, data); // Convert into value in case of a variable
  17542. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17543. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17544. skill_lv = battle_config.mob_max_skilllvl;
  17545. casttime = script_getnum(st,9);
  17546. cancel = script_getnum(st,10);
  17547. emotion = script_getnum(st,11);
  17548. target = script_getnum(st,12);
  17549. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17550. return SCRIPT_CMD_SUCCESS;
  17551. }
  17552. /**
  17553. * Display a progress bar above a character
  17554. * progressbar "<color>",<seconds>;
  17555. */
  17556. BUILDIN_FUNC(progressbar)
  17557. {
  17558. struct map_session_data * sd;
  17559. const char * color;
  17560. unsigned int second;
  17561. if( !script_rid2sd(sd) )
  17562. return SCRIPT_CMD_SUCCESS;
  17563. st->state = STOP;
  17564. color = script_getstr(st,2);
  17565. second = script_getnum(st,3);
  17566. sd->progressbar.npc_id = st->oid;
  17567. sd->progressbar.timeout = gettick() + second*1000;
  17568. sd->state.workinprogress = WIP_DISABLE_ALL;
  17569. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17570. return SCRIPT_CMD_SUCCESS;
  17571. }
  17572. /**
  17573. * Display a progress bar above an NPC
  17574. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  17575. */
  17576. BUILDIN_FUNC(progressbar_npc){
  17577. struct npc_data* nd = NULL;
  17578. if( script_hasdata(st, 4) ){
  17579. const char* name = script_getstr(st, 4);
  17580. nd = npc_name2id(name);
  17581. if( !nd ){
  17582. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  17583. return SCRIPT_CMD_FAILURE;
  17584. }
  17585. }else{
  17586. nd = map_id2nd(st->oid);
  17587. }
  17588. // First call(by function call)
  17589. if( !nd->progressbar.timeout ){
  17590. const char *color;
  17591. int second;
  17592. color = script_getstr(st, 2);
  17593. second = script_getnum(st, 3);
  17594. if( second < 0 ){
  17595. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  17596. return SCRIPT_CMD_FAILURE;
  17597. }
  17598. // detach the player
  17599. script_detach_rid(st);
  17600. // sleep for the target amount of time
  17601. st->state = RERUNLINE;
  17602. st->sleep.tick = second * 1000;
  17603. nd->progressbar.timeout = gettick() + second * 1000;
  17604. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  17605. clif_progressbar_npc_area(nd);
  17606. // Second call(by timer after sleeping time is over)
  17607. } else {
  17608. // Continue the script
  17609. st->state = RUN;
  17610. st->sleep.tick = 0;
  17611. nd->progressbar.timeout = nd->progressbar.color = 0;
  17612. }
  17613. return SCRIPT_CMD_SUCCESS;
  17614. }
  17615. BUILDIN_FUNC(pushpc)
  17616. {
  17617. uint8 dir;
  17618. int cells, dx, dy;
  17619. struct map_session_data* sd;
  17620. if(!script_rid2sd(sd))
  17621. {
  17622. return SCRIPT_CMD_SUCCESS;
  17623. }
  17624. dir = script_getnum(st,2);
  17625. cells = script_getnum(st,3);
  17626. if(dir >= DIR_MAX)
  17627. {
  17628. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  17629. script_reportsrc(st);
  17630. dir%= DIR_MAX; // trim spin-over
  17631. }
  17632. if(!cells)
  17633. {// zero distance
  17634. return SCRIPT_CMD_SUCCESS;
  17635. }
  17636. else if(cells<0)
  17637. {// pushing backwards
  17638. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  17639. cells = -cells;
  17640. }
  17641. dx = dirx[dir];
  17642. dy = diry[dir];
  17643. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  17644. return SCRIPT_CMD_SUCCESS;
  17645. }
  17646. /// Invokes buying store preparation window
  17647. /// buyingstore <slots>;
  17648. BUILDIN_FUNC(buyingstore)
  17649. {
  17650. struct map_session_data* sd;
  17651. if( !script_rid2sd(sd) )
  17652. {
  17653. return SCRIPT_CMD_SUCCESS;
  17654. }
  17655. if( npc_isnear(&sd->bl) ) {
  17656. char output[150];
  17657. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  17658. clif_displaymessage(sd->fd, output);
  17659. return SCRIPT_CMD_SUCCESS;
  17660. }
  17661. buyingstore_setup(sd, script_getnum(st,2));
  17662. return SCRIPT_CMD_SUCCESS;
  17663. }
  17664. /// Invokes search store info window
  17665. /// searchstores <uses>,<effect>;
  17666. BUILDIN_FUNC(searchstores)
  17667. {
  17668. unsigned short effect;
  17669. unsigned int uses;
  17670. struct map_session_data* sd;
  17671. if( !script_rid2sd(sd) )
  17672. {
  17673. return SCRIPT_CMD_SUCCESS;
  17674. }
  17675. uses = script_getnum(st,2);
  17676. effect = script_getnum(st,3);
  17677. if( !uses )
  17678. {
  17679. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  17680. return SCRIPT_CMD_FAILURE;
  17681. }
  17682. if( effect > 1 )
  17683. {
  17684. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  17685. return SCRIPT_CMD_FAILURE;
  17686. }
  17687. searchstore_open(sd, uses, effect);
  17688. return SCRIPT_CMD_SUCCESS;
  17689. }
  17690. /// Displays a number as large digital clock.
  17691. /// showdigit <value>[,<type>];
  17692. BUILDIN_FUNC(showdigit)
  17693. {
  17694. unsigned int type = 0;
  17695. int value;
  17696. struct map_session_data* sd;
  17697. if( !script_rid2sd(sd) )
  17698. {
  17699. return SCRIPT_CMD_SUCCESS;
  17700. }
  17701. value = script_getnum(st,2);
  17702. if( script_hasdata(st,3) ){
  17703. type = script_getnum(st,3);
  17704. }
  17705. switch( type ){
  17706. case 0:
  17707. break;
  17708. case 1:
  17709. case 2:
  17710. // Use absolute value and then the negative value of it as starting value
  17711. // This is what gravity's client does for these counters
  17712. value = -abs(value);
  17713. break;
  17714. case 3:
  17715. value = abs(value);
  17716. if( value > 99 ){
  17717. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  17718. script_reportsrc(st);
  17719. value = 99;
  17720. }
  17721. break;
  17722. default:
  17723. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  17724. return SCRIPT_CMD_FAILURE;
  17725. }
  17726. clif_showdigit(sd, (unsigned char)type, value);
  17727. return SCRIPT_CMD_SUCCESS;
  17728. }
  17729. /**
  17730. * Rune Knight
  17731. * makerune <% success bonus>{,<char_id>};
  17732. **/
  17733. BUILDIN_FUNC(makerune) {
  17734. TBL_PC* sd;
  17735. if (!script_charid2sd(3,sd))
  17736. return SCRIPT_CMD_FAILURE;
  17737. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  17738. sd->itemid = script_getnum(st,2);
  17739. return SCRIPT_CMD_SUCCESS;
  17740. }
  17741. /**
  17742. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  17743. **/
  17744. BUILDIN_FUNC(checkdragon) {
  17745. TBL_PC* sd;
  17746. if (!script_charid2sd(2,sd))
  17747. return SCRIPT_CMD_FAILURE;
  17748. if( pc_isridingdragon(sd) )
  17749. script_pushint(st,1);
  17750. else
  17751. script_pushint(st,0);
  17752. return SCRIPT_CMD_SUCCESS;
  17753. }
  17754. /**
  17755. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  17756. * - Toggles the dragon on a RK if he can mount;
  17757. * @param Color - when not provided uses the green dragon;
  17758. * - 1 : Green Dragon
  17759. * - 2 : Brown Dragon
  17760. * - 3 : Gray Dragon
  17761. * - 4 : Blue Dragon
  17762. * - 5 : Red Dragon
  17763. **/
  17764. BUILDIN_FUNC(setdragon) {
  17765. TBL_PC* sd;
  17766. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17767. if (!script_charid2sd(3,sd))
  17768. return SCRIPT_CMD_FAILURE;
  17769. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17770. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17771. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17772. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17773. script_pushint(st,1);
  17774. } else {//Not mounted; Mount now.
  17775. unsigned int option = OPTION_DRAGON1;
  17776. if( color ) {
  17777. option = ( color == 1 ? OPTION_DRAGON1 :
  17778. color == 2 ? OPTION_DRAGON2 :
  17779. color == 3 ? OPTION_DRAGON3 :
  17780. color == 4 ? OPTION_DRAGON4 :
  17781. color == 5 ? OPTION_DRAGON5 : 0);
  17782. if( !option ) {
  17783. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17784. option = OPTION_DRAGON1;
  17785. }
  17786. }
  17787. pc_setoption(sd, sd->sc.option|option);
  17788. script_pushint(st,1);
  17789. }
  17790. return SCRIPT_CMD_SUCCESS;
  17791. }
  17792. /**
  17793. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17794. **/
  17795. BUILDIN_FUNC(ismounting) {
  17796. TBL_PC* sd;
  17797. if (!script_charid2sd(2,sd))
  17798. return SCRIPT_CMD_FAILURE;
  17799. if( sd->sc.data[SC_ALL_RIDING] )
  17800. script_pushint(st,1);
  17801. else
  17802. script_pushint(st,0);
  17803. return SCRIPT_CMD_SUCCESS;
  17804. }
  17805. /**
  17806. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17807. * - Toggles new mounts on a player when he can mount
  17808. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17809. * - Will unmount the player is he is already mounting
  17810. **/
  17811. BUILDIN_FUNC(setmounting) {
  17812. TBL_PC* sd;
  17813. if (!script_charid2sd(2,sd))
  17814. return SCRIPT_CMD_FAILURE;
  17815. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17816. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17817. script_pushint(st,0); //can't mount with one of these
  17818. } else {
  17819. if( sd->sc.data[SC_ALL_RIDING] )
  17820. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17821. else
  17822. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17823. script_pushint(st,1);//in both cases, return 1.
  17824. }
  17825. return SCRIPT_CMD_SUCCESS;
  17826. }
  17827. /**
  17828. * Retrieves quantity of arguments provided to callfunc/callsub.
  17829. * getargcount() -> amount of arguments received in a function
  17830. **/
  17831. BUILDIN_FUNC(getargcount) {
  17832. struct script_retinfo* ri;
  17833. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17834. ShowError("script:getargcount: used out of function or callsub label!\n");
  17835. st->state = END;
  17836. return SCRIPT_CMD_FAILURE;
  17837. }
  17838. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17839. script_pushint(st, ri->nargs);
  17840. return SCRIPT_CMD_SUCCESS;
  17841. }
  17842. /**
  17843. * getcharip(<account ID>/<character ID>/<character name>)
  17844. **/
  17845. BUILDIN_FUNC(getcharip)
  17846. {
  17847. struct map_session_data* sd = NULL;
  17848. /* check if a character name is specified */
  17849. if( script_hasdata(st, 2) )
  17850. {
  17851. struct script_data *data;
  17852. data = script_getdata(st, 2);
  17853. get_val(st, data); // Convert into value in case of a variable
  17854. if (data_isstring(data))
  17855. sd = map_nick2sd(script_getstr(st, 2),false);
  17856. else if (data_isint(data) || script_getnum(st, 2))
  17857. {
  17858. int id = 0;
  17859. id = script_getnum(st, 2);
  17860. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17861. }
  17862. }
  17863. else
  17864. script_rid2sd(sd);
  17865. /* check for sd and IP */
  17866. if (!sd || !session[sd->fd]->client_addr)
  17867. {
  17868. script_pushconststr(st, "");
  17869. return SCRIPT_CMD_SUCCESS;
  17870. }
  17871. /* return the client ip_addr converted for output */
  17872. if (sd && sd->fd && session[sd->fd])
  17873. {
  17874. /* initiliaze */
  17875. const char *ip_addr = NULL;
  17876. uint32 ip;
  17877. /* set ip, ip_addr and convert to ip and push str */
  17878. ip = session[sd->fd]->client_addr;
  17879. ip_addr = ip2str(ip, NULL);
  17880. script_pushstrcopy(st, ip_addr);
  17881. }
  17882. return SCRIPT_CMD_SUCCESS;
  17883. }
  17884. /**
  17885. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17886. **/
  17887. BUILDIN_FUNC(is_function) {
  17888. const char* str = script_getstr(st,2);
  17889. if( strdb_exists(userfunc_db, str) )
  17890. script_pushint(st,1);
  17891. else
  17892. script_pushint(st,0);
  17893. return SCRIPT_CMD_SUCCESS;
  17894. }
  17895. /**
  17896. * get_revision() -> retrieves the current svn revision (if available)
  17897. **/
  17898. BUILDIN_FUNC(get_revision) {
  17899. const char *svn = get_svn_revision();
  17900. if ( svn[0] != UNKNOWN_VERSION )
  17901. script_pushint(st,atoi(svn));
  17902. else
  17903. script_pushint(st,-1); //unknown
  17904. return SCRIPT_CMD_SUCCESS;
  17905. }
  17906. /* get_hash() -> retrieves the current git hash (if available)*/
  17907. BUILDIN_FUNC(get_githash) {
  17908. const char* git = get_git_hash();
  17909. char buf[CHAT_SIZE_MAX];
  17910. safestrncpy(buf,git,strlen(git)+1);
  17911. if ( git[0] != UNKNOWN_VERSION )
  17912. script_pushstr(st,buf);
  17913. else
  17914. script_pushconststr(st,"Unknown"); //unknown
  17915. return SCRIPT_CMD_SUCCESS;
  17916. }
  17917. /**
  17918. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17919. **/
  17920. BUILDIN_FUNC(freeloop) {
  17921. if( script_hasdata(st,2) ) {
  17922. if( script_getnum(st,2) )
  17923. st->freeloop = 1;
  17924. else
  17925. st->freeloop = 0;
  17926. }
  17927. script_pushint(st, st->freeloop);
  17928. return SCRIPT_CMD_SUCCESS;
  17929. }
  17930. /**
  17931. * @commands (script based)
  17932. **/
  17933. BUILDIN_FUNC(bindatcmd) {
  17934. const char* atcmd;
  17935. const char* eventName;
  17936. int i, level = 0, level2 = 100;
  17937. bool create = false;
  17938. atcmd = script_getstr(st,2);
  17939. eventName = script_getstr(st,3);
  17940. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17941. atcmd++;
  17942. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17943. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17944. if( atcmd_binding_count == 0 ) {
  17945. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17946. create = true;
  17947. } else {
  17948. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17949. if( i < atcmd_binding_count ) {/* update existent entry */
  17950. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  17951. atcmd_binding[i]->level = level;
  17952. atcmd_binding[i]->level2 = level2;
  17953. } else
  17954. create = true;
  17955. }
  17956. if( create ) {
  17957. i = atcmd_binding_count;
  17958. if( atcmd_binding_count++ != 0 )
  17959. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17960. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17961. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17962. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  17963. atcmd_binding[i]->level = level;
  17964. atcmd_binding[i]->level2 = level2;
  17965. }
  17966. return SCRIPT_CMD_SUCCESS;
  17967. }
  17968. BUILDIN_FUNC(unbindatcmd) {
  17969. const char* atcmd;
  17970. int i = 0;
  17971. atcmd = script_getstr(st, 2);
  17972. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17973. atcmd++;
  17974. if( atcmd_binding_count == 0 ) {
  17975. script_pushint(st, 0);
  17976. return SCRIPT_CMD_SUCCESS;
  17977. }
  17978. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17979. if( i < atcmd_binding_count ) {
  17980. int cursor = 0;
  17981. aFree(atcmd_binding[i]);
  17982. atcmd_binding[i] = NULL;
  17983. /* compact the list now that we freed a slot somewhere */
  17984. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17985. if( atcmd_binding[i] == NULL )
  17986. continue;
  17987. if( cursor != i ) {
  17988. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17989. }
  17990. cursor++;
  17991. }
  17992. if( (atcmd_binding_count = cursor) == 0 )
  17993. aFree(atcmd_binding);
  17994. script_pushint(st, 1);
  17995. } else
  17996. script_pushint(st, 0);/* not found */
  17997. return SCRIPT_CMD_SUCCESS;
  17998. }
  17999. BUILDIN_FUNC(useatcmd) {
  18000. return atcommand_sub(st,3);
  18001. }
  18002. BUILDIN_FUNC(checkre)
  18003. {
  18004. int num;
  18005. num=script_getnum(st,2);
  18006. switch(num){
  18007. case 0:
  18008. #ifdef RENEWAL
  18009. script_pushint(st, 1);
  18010. #else
  18011. script_pushint(st, 0);
  18012. #endif
  18013. break;
  18014. case 1:
  18015. #ifdef RENEWAL_CAST
  18016. script_pushint(st, 1);
  18017. #else
  18018. script_pushint(st, 0);
  18019. #endif
  18020. break;
  18021. case 2:
  18022. #ifdef RENEWAL_DROP
  18023. script_pushint(st, 1);
  18024. #else
  18025. script_pushint(st, 0);
  18026. #endif
  18027. break;
  18028. case 3:
  18029. #ifdef RENEWAL_EXP
  18030. script_pushint(st, 1);
  18031. #else
  18032. script_pushint(st, 0);
  18033. #endif
  18034. break;
  18035. case 4:
  18036. #ifdef RENEWAL_LVDMG
  18037. script_pushint(st, 1);
  18038. #else
  18039. script_pushint(st, 0);
  18040. #endif
  18041. break;
  18042. case 5:
  18043. #ifdef RENEWAL_ASPD
  18044. script_pushint(st, 1);
  18045. #else
  18046. script_pushint(st, 0);
  18047. #endif
  18048. break;
  18049. default:
  18050. ShowWarning("buildin_checkre: unknown parameter.\n");
  18051. break;
  18052. }
  18053. return SCRIPT_CMD_SUCCESS;
  18054. }
  18055. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  18056. BUILDIN_FUNC(getrandgroupitem) {
  18057. TBL_PC* sd;
  18058. int i, get_count = 0, identify = 0;
  18059. uint16 group, qty = 0;
  18060. uint8 sub_group = 1;
  18061. struct item item_tmp;
  18062. struct s_item_group_entry *entry = NULL;
  18063. if (!script_charid2sd(6, sd))
  18064. return SCRIPT_CMD_SUCCESS;
  18065. group = script_getnum(st,2);
  18066. if (!group) {
  18067. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  18068. return SCRIPT_CMD_FAILURE;
  18069. }
  18070. FETCH(3, qty);
  18071. FETCH(4, sub_group);
  18072. FETCH(5, identify);
  18073. entry = itemdb_get_randgroupitem(group,sub_group);
  18074. if (!entry)
  18075. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  18076. memset(&item_tmp,0,sizeof(item_tmp));
  18077. item_tmp.nameid = entry->nameid;
  18078. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  18079. if (!qty)
  18080. qty = entry->amount;
  18081. //Check if it's stackable.
  18082. if (!itemdb_isstackable(entry->nameid)) {
  18083. item_tmp.amount = 1;
  18084. get_count = qty;
  18085. }
  18086. else {
  18087. item_tmp.amount = qty;
  18088. get_count = 1;
  18089. }
  18090. for (i = 0; i < get_count; i++) {
  18091. // if not pet egg
  18092. if (!pet_create_egg(sd, entry->nameid)) {
  18093. unsigned char flag = 0;
  18094. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  18095. clif_additem(sd,0,0,flag);
  18096. if (pc_candrop(sd,&item_tmp))
  18097. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  18098. }
  18099. }
  18100. }
  18101. return SCRIPT_CMD_SUCCESS;
  18102. }
  18103. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  18104. * Gives item(s) to the attached player based on item group contents
  18105. */
  18106. BUILDIN_FUNC(getgroupitem) {
  18107. TBL_PC *sd;
  18108. int group_id = script_getnum(st,2);
  18109. if (!script_charid2sd(4,sd))
  18110. return SCRIPT_CMD_SUCCESS;
  18111. if (itemdb_pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  18112. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  18113. return SCRIPT_CMD_FAILURE;
  18114. }
  18115. return SCRIPT_CMD_SUCCESS;
  18116. }
  18117. /* cleanmap <map_name>;
  18118. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  18119. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  18120. {
  18121. nullpo_ret(bl);
  18122. map_clearflooritem(bl);
  18123. return SCRIPT_CMD_SUCCESS;
  18124. }
  18125. BUILDIN_FUNC(cleanmap)
  18126. {
  18127. const char *mapname;
  18128. int16 m;
  18129. mapname = script_getstr(st, 2);
  18130. m = map_mapname2mapid(mapname);
  18131. if (!m)
  18132. return SCRIPT_CMD_FAILURE;
  18133. if ((script_lastdata(st) - 2) < 4) {
  18134. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  18135. } else {
  18136. int16 x0 = script_getnum(st, 3);
  18137. int16 y0 = script_getnum(st, 4);
  18138. int16 x1 = script_getnum(st, 5);
  18139. int16 y1 = script_getnum(st, 6);
  18140. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  18141. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  18142. } else {
  18143. ShowError("cleanarea: invalid coordinate defined!\n");
  18144. return SCRIPT_CMD_FAILURE;
  18145. }
  18146. }
  18147. return SCRIPT_CMD_SUCCESS;
  18148. }
  18149. /* Cast a skill on the attached player.
  18150. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  18151. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  18152. BUILDIN_FUNC(npcskill)
  18153. {
  18154. uint16 skill_id;
  18155. unsigned short skill_level;
  18156. unsigned int stat_point;
  18157. unsigned int npc_level;
  18158. struct npc_data *nd;
  18159. struct map_session_data *sd;
  18160. struct script_data *data;
  18161. if( !script_rid2sd(sd) )
  18162. return SCRIPT_CMD_SUCCESS;
  18163. data = script_getdata(st, 2);
  18164. get_val(st, data); // Convert into value in case of a variable
  18165. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  18166. skill_level = script_getnum(st, 3);
  18167. stat_point = script_getnum(st, 4);
  18168. npc_level = script_getnum(st, 5);
  18169. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  18170. if (stat_point > battle_config.max_third_parameter) {
  18171. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  18172. return SCRIPT_CMD_FAILURE;
  18173. }
  18174. if (npc_level > MAX_LEVEL) {
  18175. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  18176. return SCRIPT_CMD_FAILURE;
  18177. }
  18178. if (nd == NULL) { //ain't possible, but I don't trust people.
  18179. return SCRIPT_CMD_FAILURE;
  18180. }
  18181. nd->level = npc_level;
  18182. nd->stat_point = stat_point;
  18183. if (!nd->status.hp)
  18184. status_calc_npc(nd, SCO_FIRST);
  18185. else
  18186. status_calc_npc(nd, SCO_NONE);
  18187. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  18188. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  18189. else
  18190. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  18191. return SCRIPT_CMD_SUCCESS;
  18192. }
  18193. /* Consumes an item.
  18194. * consumeitem <item id>{,<char_id>};
  18195. * consumeitem "<item name>"{,<char_id>};
  18196. * @param item: Item ID or name
  18197. */
  18198. BUILDIN_FUNC(consumeitem)
  18199. {
  18200. TBL_PC *sd;
  18201. struct script_data *data;
  18202. struct item_data *item_data;
  18203. if (!script_charid2sd(3, sd))
  18204. return SCRIPT_CMD_FAILURE;
  18205. data = script_getdata( st, 2 );
  18206. get_val( st, data );
  18207. if( data_isstring( data ) ){
  18208. const char *name = conv_str( st, data );
  18209. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  18210. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  18211. return SCRIPT_CMD_FAILURE;
  18212. }
  18213. }else if( data_isint( data ) ){
  18214. unsigned short nameid = conv_num( st, data );
  18215. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  18216. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  18217. return SCRIPT_CMD_FAILURE;
  18218. }
  18219. }else{
  18220. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  18221. return SCRIPT_CMD_FAILURE;
  18222. }
  18223. run_script( item_data->script, 0, sd->bl.id, 0 );
  18224. return SCRIPT_CMD_SUCCESS;
  18225. }
  18226. /** Makes a player sit/stand.
  18227. * sit {"<character name>"};
  18228. * stand {"<character name>"};
  18229. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  18230. * @param name: Player name that will be invoked
  18231. */
  18232. BUILDIN_FUNC(sit)
  18233. {
  18234. TBL_PC *sd;
  18235. if( !script_nick2sd(2,sd) )
  18236. return SCRIPT_CMD_FAILURE;
  18237. if( !pc_issit(sd) ) {
  18238. pc_setsit(sd);
  18239. skill_sit(sd, 1);
  18240. clif_sitting(&sd->bl);
  18241. }
  18242. return SCRIPT_CMD_SUCCESS;
  18243. }
  18244. /** Makes player to stand
  18245. * @param name: Player name that will be set
  18246. */
  18247. BUILDIN_FUNC(stand)
  18248. {
  18249. TBL_PC *sd;
  18250. if( !script_nick2sd(2,sd) )
  18251. return SCRIPT_CMD_FAILURE;
  18252. if( pc_issit(sd) && pc_setstand(sd, false)) {
  18253. skill_sit(sd, 0);
  18254. clif_standing(&sd->bl);
  18255. }
  18256. return SCRIPT_CMD_SUCCESS;
  18257. }
  18258. /** Creates an array of bounded item IDs
  18259. * countbound {<type>{,<char_id>}};
  18260. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  18261. * @return amt: Amount of items found
  18262. */
  18263. BUILDIN_FUNC(countbound)
  18264. {
  18265. int i, type, j = 0, k = 0;
  18266. TBL_PC *sd;
  18267. if (!script_charid2sd(3,sd))
  18268. return SCRIPT_CMD_FAILURE;
  18269. type = script_getnum(st,2);
  18270. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  18271. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  18272. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  18273. ))
  18274. {
  18275. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  18276. k++;
  18277. j += sd->inventory.u.items_inventory[i].amount;
  18278. }
  18279. }
  18280. script_pushint(st,j);
  18281. return SCRIPT_CMD_SUCCESS;
  18282. }
  18283. /** Creates new party
  18284. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  18285. * @param party_name: String of party name that will be created
  18286. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  18287. * @param item_share: 0-Each Take. 1-Party Share
  18288. * @param item_share_type: 0-Each Take. 1-Even Share
  18289. * @return val: Result value
  18290. * -3 - party name is exist
  18291. * -2 - player is in party already
  18292. * -1 - player is not found
  18293. * 0 - unknown error
  18294. * 1 - success, will return party id $@party_create_id
  18295. */
  18296. BUILDIN_FUNC(party_create)
  18297. {
  18298. char party_name[NAME_LENGTH];
  18299. int item1 = 0, item2 = 0;
  18300. TBL_PC *sd = NULL;
  18301. if (!script_charid2sd(3, sd)) {
  18302. script_pushint(st,-1);
  18303. return SCRIPT_CMD_FAILURE;
  18304. }
  18305. if( sd->status.party_id ) {
  18306. script_pushint(st,-2);
  18307. return SCRIPT_CMD_FAILURE;
  18308. }
  18309. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  18310. trim(party_name);
  18311. if( party_searchname(party_name) ) {
  18312. script_pushint(st,-3);
  18313. return SCRIPT_CMD_FAILURE;
  18314. }
  18315. if( script_getnum(st,4) )
  18316. item1 = 1;
  18317. if( script_getnum(st,5) )
  18318. item2 = 1;
  18319. party_create_byscript = 1;
  18320. script_pushint(st,party_create(sd,party_name,item1,item2));
  18321. return SCRIPT_CMD_SUCCESS;
  18322. }
  18323. /** Adds player to specified party
  18324. * party_addmember <party id>,<char id>;
  18325. * @param party_id: The party that will be entered by player
  18326. * @param char_id: Char id of player that will be joined to the party
  18327. * @return val: Result value
  18328. * -5 - another character of the same account is in the party
  18329. * -4 - party is full
  18330. * -3 - party is not found
  18331. * -2 - player is in party already
  18332. * -1 - player is not found
  18333. * 0 - unknown error
  18334. * 1 - success
  18335. */
  18336. BUILDIN_FUNC(party_addmember)
  18337. {
  18338. int party_id = script_getnum(st,2);
  18339. TBL_PC *sd;
  18340. struct party_data *party;
  18341. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  18342. script_pushint(st,-1);
  18343. return SCRIPT_CMD_FAILURE;
  18344. }
  18345. if( sd->status.party_id ) {
  18346. script_pushint(st,-2);
  18347. return SCRIPT_CMD_FAILURE;
  18348. }
  18349. if( !(party = party_search(party_id)) ) {
  18350. script_pushint(st,-3);
  18351. return SCRIPT_CMD_FAILURE;
  18352. }
  18353. if (battle_config.block_account_in_same_party) {
  18354. int i;
  18355. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  18356. if (i < MAX_PARTY) {
  18357. script_pushint(st,-5);
  18358. return SCRIPT_CMD_FAILURE;
  18359. }
  18360. }
  18361. if( party->party.count >= MAX_PARTY ) {
  18362. script_pushint(st,-4);
  18363. return SCRIPT_CMD_FAILURE;
  18364. }
  18365. sd->party_invite = party_id;
  18366. script_pushint(st,party_add_member(party_id,sd));
  18367. return SCRIPT_CMD_SUCCESS;
  18368. }
  18369. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  18370. * party_delmember {<char id>,<party_id>};
  18371. * @param: char_id
  18372. * @param: party_id
  18373. * @return val: Result value
  18374. * -3 - player is not in party
  18375. * -2 - party is not found
  18376. * -1 - player is not found
  18377. * 0 - unknown error
  18378. * 1 - success
  18379. */
  18380. BUILDIN_FUNC(party_delmember)
  18381. {
  18382. TBL_PC *sd = NULL;
  18383. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  18384. script_pushint(st,-1);
  18385. return SCRIPT_CMD_FAILURE;
  18386. }
  18387. if( sd || script_charid2sd(2,sd) )
  18388. script_pushint(st,party_removemember2(sd,0,0));
  18389. else
  18390. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  18391. return SCRIPT_CMD_SUCCESS;
  18392. }
  18393. /** Changes party leader of specified party (even the leader is offline)
  18394. * party_changeleader <party id>,<char id>;
  18395. * @param party_id: ID of party
  18396. * @param char_id: Char ID of new leader
  18397. * @return val: Result value
  18398. * -4 - player is party leader already
  18399. * -3 - player is not in this party
  18400. * -2 - player is not found
  18401. * -1 - party is not found
  18402. * 0 - unknown error
  18403. * 1 - success */
  18404. BUILDIN_FUNC(party_changeleader)
  18405. {
  18406. int i, party_id = script_getnum(st,2);
  18407. TBL_PC *sd = NULL;
  18408. TBL_PC *tsd = NULL;
  18409. struct party_data *party = NULL;
  18410. if( !(party = party_search(party_id)) ) {
  18411. script_pushint(st,-1);
  18412. return SCRIPT_CMD_FAILURE;
  18413. }
  18414. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  18415. script_pushint(st,-2);
  18416. return SCRIPT_CMD_FAILURE;
  18417. }
  18418. if( tsd->status.party_id != party_id ) {
  18419. script_pushint(st,-3);
  18420. return SCRIPT_CMD_FAILURE;
  18421. }
  18422. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18423. if( i >= MAX_PARTY ) { //this is should impossible!
  18424. script_pushint(st,0);
  18425. return SCRIPT_CMD_FAILURE;
  18426. }
  18427. if( party->data[i].sd == tsd ) {
  18428. script_pushint(st,-4);
  18429. return SCRIPT_CMD_FAILURE;
  18430. }
  18431. script_pushint(st,party_changeleader(sd,tsd,party));
  18432. return SCRIPT_CMD_SUCCESS;
  18433. }
  18434. /** Changes party option
  18435. * party_changeoption <party id>,<option>,<flag>;
  18436. * @param party_id: ID of party that will be changed
  18437. * @param option: Type of option
  18438. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  18439. */
  18440. BUILDIN_FUNC(party_changeoption)
  18441. {
  18442. struct party_data *party;
  18443. if( !(party = party_search(script_getnum(st,2))) ) {
  18444. script_pushint(st,-1);
  18445. return SCRIPT_CMD_FAILURE;
  18446. }
  18447. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  18448. return SCRIPT_CMD_SUCCESS;
  18449. }
  18450. /** Destroys party with party id.
  18451. * party_destroy <party id>;
  18452. * @param party_id: ID of party that will be destroyed
  18453. */
  18454. BUILDIN_FUNC(party_destroy)
  18455. {
  18456. int i;
  18457. struct party_data *party;
  18458. if( !(party = party_search(script_getnum(st,2))) ) {
  18459. script_pushint(st,0);
  18460. return SCRIPT_CMD_FAILURE;
  18461. }
  18462. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18463. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  18464. int j;
  18465. for( j = 0; j < MAX_PARTY; j++ ) {
  18466. TBL_PC *sd = party->data[j].sd;
  18467. if(sd)
  18468. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18469. else if( party->party.member[j].char_id )
  18470. 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);
  18471. }
  18472. party_broken(party->party.party_id);
  18473. script_pushint(st,1);
  18474. }
  18475. else //leader leave = party broken
  18476. script_pushint(st,party_leave(party->data[i].sd));
  18477. return SCRIPT_CMD_SUCCESS;
  18478. }
  18479. /** Returns various information about a player's VIP status. Need to enable VIP system
  18480. * vip_status <type>,{"<character name>"};
  18481. * @param type: Info type, see enum vip_status_type
  18482. * @param name: Character name (Optional)
  18483. */
  18484. BUILDIN_FUNC(vip_status) {
  18485. #ifdef VIP_ENABLE
  18486. TBL_PC *sd;
  18487. int type;
  18488. if( !script_nick2sd(3,sd) )
  18489. return SCRIPT_CMD_FAILURE;
  18490. type = script_getnum(st, 2);
  18491. switch(type) {
  18492. case VIP_STATUS_ACTIVE: // Get VIP status.
  18493. script_pushint(st, pc_isvip(sd));
  18494. break;
  18495. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  18496. if (pc_isvip(sd)) {
  18497. script_pushint(st, sd->vip.time);
  18498. } else
  18499. script_pushint(st, 0);
  18500. break;
  18501. case VIP_STATUS_REMAINING: // Get remaining time.
  18502. if (pc_isvip(sd)) {
  18503. script_pushint(st, sd->vip.time - time(NULL));
  18504. } else
  18505. script_pushint(st, 0);
  18506. break;
  18507. default:
  18508. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  18509. return SCRIPT_CMD_FAILURE;
  18510. }
  18511. #else
  18512. script_pushint(st, 0);
  18513. #endif
  18514. return SCRIPT_CMD_SUCCESS;
  18515. }
  18516. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18517. * vip_time <time in mn>,{"<character name>"};
  18518. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18519. * @param name: Character name (optional)
  18520. */
  18521. BUILDIN_FUNC(vip_time) {
  18522. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18523. TBL_PC *sd;
  18524. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18525. if( !script_nick2sd(3,sd) )
  18526. return SCRIPT_CMD_FAILURE;
  18527. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18528. #endif
  18529. return SCRIPT_CMD_SUCCESS;
  18530. }
  18531. /**
  18532. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18533. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18534. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18535. * @param monster: Monster ID or name
  18536. * @param duration: Transform duration in millisecond (ms)
  18537. * @param sc_type: Type of SC that will be affected during the transformation
  18538. * @param val1: Value for SC
  18539. * @param val2: Value for SC
  18540. * @param val3: Value for SC
  18541. * @param val4: Value for SC
  18542. * @author: malufett
  18543. */
  18544. BUILDIN_FUNC(montransform) {
  18545. TBL_PC *sd;
  18546. enum sc_type type;
  18547. int tick, mob_id, val1, val2, val3, val4;
  18548. struct script_data *data;
  18549. val1 = val2 = val3 = val4 = 0;
  18550. if( !script_rid2sd(sd) )
  18551. return SCRIPT_CMD_FAILURE;
  18552. data = script_getdata(st, 2);
  18553. get_val(st, data); // Convert into value in case of a variable
  18554. if( data_isstring(data) )
  18555. mob_id = mobdb_searchname(script_getstr(st, 2));
  18556. else
  18557. mob_id = mobdb_checkid(script_getnum(st, 2));
  18558. tick = script_getnum(st, 3);
  18559. if (script_hasdata(st, 4))
  18560. type = (sc_type)script_getnum(st, 4);
  18561. else
  18562. type = SC_NONE;
  18563. if (mob_id == 0) {
  18564. if( data_isstring(data) )
  18565. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18566. else
  18567. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18568. return SCRIPT_CMD_FAILURE;
  18569. }
  18570. if (mob_id == MOBID_EMPERIUM) {
  18571. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  18572. return SCRIPT_CMD_FAILURE;
  18573. }
  18574. if (!(type >= SC_NONE && type < SC_MAX)) {
  18575. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  18576. return SCRIPT_CMD_FAILURE;
  18577. }
  18578. if (script_hasdata(st, 5))
  18579. val1 = script_getnum(st, 5);
  18580. if (script_hasdata(st, 6))
  18581. val2 = script_getnum(st, 6);
  18582. if (script_hasdata(st, 7))
  18583. val3 = script_getnum(st, 7);
  18584. if (script_hasdata(st, 8))
  18585. val4 = script_getnum(st, 8);
  18586. if (tick != 0) {
  18587. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  18588. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  18589. return SCRIPT_CMD_FAILURE;
  18590. }
  18591. if (sd->disguise){
  18592. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  18593. return SCRIPT_CMD_FAILURE;
  18594. }
  18595. if (!strcmp(script_getfuncname(st), "active_transform")) {
  18596. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18597. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18598. } else {
  18599. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18600. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18601. }
  18602. if (type != SC_NONE)
  18603. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  18604. }
  18605. return SCRIPT_CMD_SUCCESS;
  18606. }
  18607. /**
  18608. * Attach script to player for certain duration
  18609. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  18610. * @param "script code"
  18611. * @param duration
  18612. * @param flag
  18613. * @param icon
  18614. * @param char_id
  18615. * @author [Cydh]
  18616. **/
  18617. BUILDIN_FUNC(bonus_script) {
  18618. uint16 flag = 0;
  18619. int16 icon = EFST_BLANK;
  18620. uint32 dur;
  18621. uint8 type = 0;
  18622. TBL_PC* sd;
  18623. const char *script_str = NULL;
  18624. struct s_bonus_script_entry *entry = NULL;
  18625. if ( !script_charid2sd(7,sd) )
  18626. return SCRIPT_CMD_FAILURE;
  18627. script_str = script_getstr(st,2);
  18628. dur = 1000 * abs(script_getnum(st,3));
  18629. FETCH(4, flag);
  18630. FETCH(5, type);
  18631. FETCH(6, icon);
  18632. // No Script string, No Duration!
  18633. if (script_str[0] == '\0' || !dur) {
  18634. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  18635. return SCRIPT_CMD_FAILURE;
  18636. }
  18637. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  18638. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  18639. return SCRIPT_CMD_FAILURE;
  18640. }
  18641. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  18642. icon = EFST_BLANK;
  18643. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_types)icon, flag, type))) {
  18644. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  18645. status_calc_pc(sd,SCO_NONE);
  18646. }
  18647. return SCRIPT_CMD_SUCCESS;
  18648. }
  18649. /**
  18650. * Removes all bonus script from player
  18651. * bonus_script_clear {<flag>,{<char_id>}};
  18652. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  18653. * @param char_id Clear script from this character
  18654. * @author [Cydh]
  18655. */
  18656. BUILDIN_FUNC(bonus_script_clear) {
  18657. TBL_PC* sd;
  18658. bool flag = false;
  18659. if (!script_charid2sd(3,sd))
  18660. return SCRIPT_CMD_FAILURE;
  18661. if (script_hasdata(st,2))
  18662. flag = script_getnum(st,2) != 0;
  18663. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  18664. return SCRIPT_CMD_SUCCESS;
  18665. }
  18666. /** Allows player to use atcommand while talking with NPC
  18667. * enable_command;
  18668. * @author [Cydh], [Kichi]
  18669. */
  18670. BUILDIN_FUNC(enable_command) {
  18671. TBL_PC* sd;
  18672. if (!script_rid2sd(sd))
  18673. return SCRIPT_CMD_FAILURE;
  18674. sd->state.disable_atcommand_on_npc = 0;
  18675. return SCRIPT_CMD_SUCCESS;
  18676. }
  18677. /** Prevents player to use atcommand while talking with NPC
  18678. * disable_command;
  18679. * @author [Cydh], [Kichi]
  18680. */
  18681. BUILDIN_FUNC(disable_command) {
  18682. TBL_PC* sd;
  18683. if (!script_rid2sd(sd))
  18684. return SCRIPT_CMD_FAILURE;
  18685. sd->state.disable_atcommand_on_npc = 1;
  18686. return SCRIPT_CMD_SUCCESS;
  18687. }
  18688. /** Get the information of the members of a guild by type.
  18689. * getguildmember <guild_id>{,<type>};
  18690. * @param guild_id: ID of guild
  18691. * @param type: Type of option (optional)
  18692. */
  18693. BUILDIN_FUNC(getguildmember)
  18694. {
  18695. struct guild *g = NULL;
  18696. uint8 j = 0;
  18697. g = guild_search(script_getnum(st,2));
  18698. if (g) {
  18699. uint8 i, type = 0;
  18700. struct script_data *data = NULL;
  18701. char *varname = NULL;
  18702. if (script_hasdata(st,3))
  18703. type = script_getnum(st,3);
  18704. if (script_hasdata(st,4)) {
  18705. data = script_getdata(st, 4);
  18706. if (!data_isreference(data)) {
  18707. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  18708. return SCRIPT_CMD_FAILURE;
  18709. }
  18710. varname = reference_getname(data);
  18711. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  18712. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  18713. return SCRIPT_CMD_FAILURE;
  18714. }
  18715. }
  18716. for (i = 0; i < MAX_GUILD; i++) {
  18717. if (g->member[i].account_id) {
  18718. switch (type) {
  18719. case 2:
  18720. if (data)
  18721. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  18722. else
  18723. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18724. break;
  18725. case 1:
  18726. if (data)
  18727. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18728. else
  18729. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18730. break;
  18731. default:
  18732. if (data)
  18733. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18734. else
  18735. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18736. break;
  18737. }
  18738. j++;
  18739. }
  18740. }
  18741. }
  18742. mapreg_setreg(add_str("$@guildmembercount"), j);
  18743. script_pushint(st, j);
  18744. return SCRIPT_CMD_SUCCESS;
  18745. }
  18746. /** Adds spirit ball to player for 'duration' in milisecond
  18747. * addspiritball <count>,<duration>{,<char_id>};
  18748. * @param count How many spirit ball will be added
  18749. * @param duration How long spiritball is active until it disappears
  18750. * @param char_id Target player (Optional)
  18751. * @author [Cydh]
  18752. */
  18753. BUILDIN_FUNC(addspiritball) {
  18754. uint8 i, count;
  18755. uint16 duration;
  18756. struct map_session_data *sd = NULL;
  18757. if (script_hasdata(st,4)) {
  18758. if (!script_isstring(st,4))
  18759. sd = map_charid2sd(script_getnum(st,4));
  18760. else
  18761. sd = map_nick2sd(script_getstr(st,4),false);
  18762. }
  18763. else
  18764. script_rid2sd(sd);
  18765. if (!sd)
  18766. return SCRIPT_CMD_FAILURE;
  18767. count = script_getnum(st,2);
  18768. if (count == 0)
  18769. return SCRIPT_CMD_SUCCESS;
  18770. duration = script_getnum(st,3);
  18771. for (i = 0; i < count; i++)
  18772. pc_addspiritball(sd,duration,10);
  18773. return SCRIPT_CMD_SUCCESS;
  18774. }
  18775. /** Deletes the spirit ball(s) from player
  18776. * delspiritball <count>{,<char_id>};
  18777. * @param count How many spirit ball will be deleted
  18778. * @param char_id Target player (Optional)
  18779. * @author [Cydh]
  18780. */
  18781. BUILDIN_FUNC(delspiritball) {
  18782. uint8 count;
  18783. struct map_session_data *sd = NULL;
  18784. if (script_hasdata(st,3)) {
  18785. if (!script_isstring(st,3))
  18786. sd = map_charid2sd(script_getnum(st,3));
  18787. else
  18788. sd = map_nick2sd(script_getstr(st,3),false);
  18789. }
  18790. else
  18791. script_rid2sd(sd);
  18792. if (!sd)
  18793. return SCRIPT_CMD_FAILURE;
  18794. count = script_getnum(st,2);
  18795. if (count == 0)
  18796. count = 1;
  18797. pc_delspiritball(sd,count,0);
  18798. return SCRIPT_CMD_SUCCESS;
  18799. }
  18800. /** Counts the spirit ball that player has
  18801. * countspiritball {<char_id>};
  18802. * @param char_id Target player (Optional)
  18803. * @author [Cydh]
  18804. */
  18805. BUILDIN_FUNC(countspiritball) {
  18806. struct map_session_data *sd;
  18807. if (script_hasdata(st,2)) {
  18808. if (!script_isstring(st,2))
  18809. sd = map_charid2sd(script_getnum(st,2));
  18810. else
  18811. sd = map_nick2sd(script_getstr(st,2),false);
  18812. }
  18813. else
  18814. script_rid2sd(sd);
  18815. if (!sd)
  18816. return SCRIPT_CMD_FAILURE;
  18817. script_pushint(st,sd->spiritball);
  18818. return SCRIPT_CMD_SUCCESS;
  18819. }
  18820. /** Merges separated stackable items because of guid
  18821. * mergeitem {<char_id>};
  18822. * @param char_id Char ID (Optional)
  18823. * @author [Cydh]
  18824. */
  18825. BUILDIN_FUNC(mergeitem) {
  18826. struct map_session_data *sd;
  18827. if (!script_charid2sd(2, sd))
  18828. return SCRIPT_CMD_FAILURE;
  18829. clif_merge_item_open(sd);
  18830. return SCRIPT_CMD_SUCCESS;
  18831. }
  18832. /** Merges separated stackable items because of guid (Unofficial)
  18833. * mergeitem2 {<item_id>,{<char_id>}};
  18834. * mergeitem2 {"<item name>",{<char_id>}};
  18835. * @param item Item ID/Name for merging specific item (Optional)
  18836. * @author [Cydh]
  18837. */
  18838. BUILDIN_FUNC(mergeitem2) {
  18839. struct map_session_data *sd;
  18840. struct item *items = NULL;
  18841. uint16 i, count = 0, nameid = 0;
  18842. if (!script_charid2sd(3, sd))
  18843. return SCRIPT_CMD_FAILURE;
  18844. if (script_hasdata(st, 2)) {
  18845. struct script_data *data = script_getdata(st, 2);
  18846. get_val(st, data);
  18847. if (data_isstring(data)) {// "<item name>"
  18848. const char *name = conv_str(st,data);
  18849. struct item_data *id;
  18850. if (!(id = itemdb_searchname(name))) {
  18851. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18852. script_pushint(st, count);
  18853. return SCRIPT_CMD_FAILURE;
  18854. }
  18855. nameid = id->nameid;
  18856. }
  18857. else if (data_isint(data)) {// <item id>
  18858. nameid = conv_num(st,data);
  18859. if (!itemdb_exists(nameid)) {
  18860. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18861. script_pushint(st, count);
  18862. return SCRIPT_CMD_FAILURE;
  18863. }
  18864. }
  18865. }
  18866. for (i = 0; i < MAX_INVENTORY; i++) {
  18867. struct item *it = &sd->inventory.u.items_inventory[i];
  18868. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18869. continue;
  18870. if ((!nameid || (nameid == it->nameid))) {
  18871. uint8 k;
  18872. if (!count) {
  18873. CREATE(items, struct item, 1);
  18874. memcpy(&items[count++], it, sizeof(struct item));
  18875. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18876. continue;
  18877. }
  18878. for (k = 0; k < count; k++) {
  18879. // Find Match
  18880. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18881. items[k].amount += it->amount;
  18882. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18883. break;
  18884. }
  18885. }
  18886. if (k >= count) {
  18887. // New entry
  18888. RECREATE(items, struct item, count+1);
  18889. memcpy(&items[count++], it, sizeof(struct item));
  18890. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18891. }
  18892. }
  18893. }
  18894. if (!items) // Nothing todo here
  18895. return SCRIPT_CMD_SUCCESS;
  18896. // Retrieve the items
  18897. for (i = 0; i < count; i++) {
  18898. uint8 flag = 0;
  18899. if (!&items[i])
  18900. continue;
  18901. items[i].id = 0;
  18902. items[i].unique_id = 0;
  18903. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18904. clif_additem(sd, i, items[i].amount, flag);
  18905. }
  18906. aFree(items);
  18907. script_pushint(st, count);
  18908. return SCRIPT_CMD_SUCCESS;
  18909. }
  18910. /**
  18911. * Update an entry from NPC shop.
  18912. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18913. **/
  18914. BUILDIN_FUNC(npcshopupdate) {
  18915. const char* npcname = script_getstr(st, 2);
  18916. struct npc_data* nd = npc_name2id(npcname);
  18917. uint16 nameid = script_getnum(st, 3);
  18918. int price = script_getnum(st, 4);
  18919. #if PACKETVER >= 20131223
  18920. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18921. #endif
  18922. int i;
  18923. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18924. script_pushint(st,0);
  18925. return SCRIPT_CMD_FAILURE;
  18926. }
  18927. if (!nd->u.shop.count) {
  18928. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18929. script_pushint(st,0);
  18930. return SCRIPT_CMD_FAILURE;
  18931. }
  18932. for (i = 0; i < nd->u.shop.count; i++) {
  18933. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18934. if (price != 0)
  18935. nd->u.shop.shop_item[i].value = price;
  18936. #if PACKETVER >= 20131223
  18937. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18938. nd->u.shop.shop_item[i].qty = stock;
  18939. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18940. }
  18941. #endif
  18942. }
  18943. }
  18944. script_pushint(st,1);
  18945. return SCRIPT_CMD_SUCCESS;
  18946. }
  18947. // Clan System
  18948. BUILDIN_FUNC(clan_join){
  18949. struct map_session_data *sd;
  18950. int clan_id = script_getnum(st,2);
  18951. if( !script_charid2sd( 3, sd ) ){
  18952. script_pushint(st, false);
  18953. return SCRIPT_CMD_FAILURE;
  18954. }
  18955. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  18956. script_pushint(st, true);
  18957. else
  18958. script_pushint(st, false);
  18959. return SCRIPT_CMD_SUCCESS;
  18960. }
  18961. BUILDIN_FUNC(clan_leave){
  18962. struct map_session_data *sd;
  18963. if( !script_charid2sd( 2, sd ) ){
  18964. script_pushint(st, false);
  18965. return SCRIPT_CMD_FAILURE;
  18966. }
  18967. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  18968. script_pushint(st, true);
  18969. else
  18970. script_pushint(st, false);
  18971. return SCRIPT_CMD_SUCCESS;
  18972. }
  18973. /**
  18974. * Get rid from running script.
  18975. * getattachedrid();
  18976. **/
  18977. BUILDIN_FUNC(getattachedrid) {
  18978. script_pushint(st, st->rid);
  18979. return SCRIPT_CMD_SUCCESS;
  18980. }
  18981. /**
  18982. * Get variable from a Player
  18983. * getvar <variable>,<char_id>;
  18984. */
  18985. BUILDIN_FUNC(getvar) {
  18986. int char_id = script_getnum(st, 3);
  18987. struct map_session_data *sd = map_charid2sd(char_id);
  18988. struct script_data *data = NULL;
  18989. const char *name = NULL;
  18990. if (!sd) {
  18991. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18992. return SCRIPT_CMD_FAILURE;
  18993. }
  18994. data = script_getdata(st, 2);
  18995. if (!data_isreference(data)) {
  18996. ShowError("buildin_getvar: Not a variable\n");
  18997. script_reportdata(data);
  18998. script_pushnil(st);
  18999. st->state = END;
  19000. return SCRIPT_CMD_FAILURE;
  19001. }
  19002. name = reference_getname(data);
  19003. if (reference_toparam(data)) {
  19004. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  19005. script_reportdata(data);
  19006. script_pushnil(st);
  19007. st->state = END;
  19008. return SCRIPT_CMD_FAILURE;
  19009. }
  19010. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  19011. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  19012. script_reportdata(data);
  19013. script_pushnil(st);
  19014. st->state = END;
  19015. return SCRIPT_CMD_FAILURE;
  19016. }
  19017. get_val_(st, data, sd);
  19018. if (data_isint(data))
  19019. script_pushint(st, conv_num_(st, data, sd));
  19020. else
  19021. script_pushstrcopy(st, conv_str_(st, data, sd));
  19022. return SCRIPT_CMD_SUCCESS;
  19023. }
  19024. /**
  19025. * Display script message
  19026. * showscript "<message>"{,<GID>,<flag>};
  19027. * @param flag: Specify target
  19028. * AREA - Message is sent to players in the vicinity of the source (default).
  19029. * SELF - Message is sent only to player attached.
  19030. **/
  19031. BUILDIN_FUNC(showscript) {
  19032. struct block_list *bl = NULL;
  19033. const char *msg = script_getstr(st,2);
  19034. int id = 0;
  19035. send_target target = AREA;
  19036. if (script_hasdata(st,3)) {
  19037. id = script_getnum(st,3);
  19038. bl = map_id2bl(id);
  19039. }
  19040. else {
  19041. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  19042. }
  19043. if (!bl) {
  19044. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  19045. script_pushint(st,0);
  19046. return SCRIPT_CMD_FAILURE;
  19047. }
  19048. if (script_hasdata(st, 4)) {
  19049. target = static_cast<send_target>(script_getnum(st, 4));
  19050. if (target == SELF && map_id2sd(bl->id) == NULL) {
  19051. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  19052. return SCRIPT_CMD_FAILURE;
  19053. }
  19054. }
  19055. clif_showscript(bl, msg, target);
  19056. script_pushint(st,1);
  19057. return SCRIPT_CMD_SUCCESS;
  19058. }
  19059. /**
  19060. * Ignore the SECURE_NPCTIMEOUT function.
  19061. * ignoretimeout <flag>{,<char_id>};
  19062. */
  19063. BUILDIN_FUNC(ignoretimeout)
  19064. {
  19065. #ifdef SECURE_NPCTIMEOUT
  19066. struct map_session_data *sd = NULL;
  19067. if (script_hasdata(st,3)) {
  19068. if (!script_isstring(st,3))
  19069. sd = map_charid2sd(script_getnum(st,3));
  19070. else
  19071. sd = map_nick2sd(script_getstr(st,3),false);
  19072. } else
  19073. script_rid2sd(sd);
  19074. if (!sd)
  19075. return SCRIPT_CMD_FAILURE;
  19076. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  19077. #endif
  19078. return SCRIPT_CMD_SUCCESS;
  19079. }
  19080. /**
  19081. * geteleminfo <type>{,<char_id>};
  19082. **/
  19083. BUILDIN_FUNC(geteleminfo) {
  19084. TBL_ELEM *ed = NULL;
  19085. TBL_PC *sd = NULL;
  19086. int type = script_getnum(st,2);
  19087. if (!script_charid2sd(3, sd)) {
  19088. script_pushint(st, 0);
  19089. return SCRIPT_CMD_SUCCESS;
  19090. }
  19091. if (!(ed = sd->ed)) {
  19092. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  19093. script_pushint(st, 0);
  19094. return SCRIPT_CMD_SUCCESS;
  19095. }
  19096. switch (type) {
  19097. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  19098. case 1: script_pushint(st, ed->bl.id); break;
  19099. default:
  19100. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  19101. script_pushint(st, 0);
  19102. return SCRIPT_CMD_FAILURE;
  19103. }
  19104. return SCRIPT_CMD_SUCCESS;
  19105. }
  19106. /**
  19107. * Set the quest info of quest_id only showed on player in level range.
  19108. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  19109. * @author [Cydh]
  19110. **/
  19111. BUILDIN_FUNC(setquestinfo_level) {
  19112. TBL_NPC* nd = map_id2nd(st->oid);
  19113. int quest_id = script_getnum(st, 2);
  19114. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19115. if (!qi) {
  19116. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  19117. return SCRIPT_CMD_FAILURE;
  19118. }
  19119. qi->min_level = script_getnum(st, 3);
  19120. qi->max_level = script_getnum(st, 4);
  19121. if (!qi->max_level)
  19122. qi->max_level = MAX_LEVEL;
  19123. return SCRIPT_CMD_SUCCESS;
  19124. }
  19125. /**
  19126. * Set the quest info of quest_id only showed for player that has quest criteria
  19127. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  19128. * @author [Cydh]
  19129. **/
  19130. BUILDIN_FUNC(setquestinfo_req) {
  19131. TBL_NPC* nd = map_id2nd(st->oid);
  19132. int quest_id = script_getnum(st, 2);
  19133. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19134. uint8 i = 0;
  19135. uint8 num = script_lastdata(st);
  19136. if (!qi) {
  19137. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  19138. return SCRIPT_CMD_FAILURE;
  19139. }
  19140. if (quest_search(quest_id) == &quest_dummy) {
  19141. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  19142. return SCRIPT_CMD_FAILURE;
  19143. }
  19144. if (num%2) {
  19145. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  19146. return SCRIPT_CMD_FAILURE;
  19147. }
  19148. for (i = 3; i <= num; i += 2) {
  19149. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  19150. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  19151. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  19152. qi->req_count++;
  19153. }
  19154. return SCRIPT_CMD_SUCCESS;
  19155. }
  19156. /**
  19157. * Set the quest info of quest_id only showed for player that has specified Job
  19158. * setquestinfo_job <quest_id>,<job>{,<job>...};
  19159. * @author [Cydh]
  19160. **/
  19161. BUILDIN_FUNC(setquestinfo_job) {
  19162. TBL_NPC* nd = map_id2nd(st->oid);
  19163. int quest_id = script_getnum(st, 2);
  19164. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19165. int job_id = 0;
  19166. uint8 i = 0;
  19167. uint8 num = script_lastdata(st)+1;
  19168. if (!qi) {
  19169. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  19170. return SCRIPT_CMD_FAILURE;
  19171. }
  19172. for (i = 3; i < num; i++) {
  19173. job_id = script_getnum(st, i);
  19174. if (!pcdb_checkid(job_id)) {
  19175. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  19176. continue;
  19177. }
  19178. buildin_questinfo_setjob(qi, job_id);
  19179. }
  19180. return SCRIPT_CMD_SUCCESS;
  19181. }
  19182. /**
  19183. * opendressroom(<flag>{,<char_id>});
  19184. */
  19185. BUILDIN_FUNC(opendressroom)
  19186. {
  19187. #if PACKETVER >= 20150513
  19188. int flag = 1;
  19189. TBL_PC* sd;
  19190. if( script_hasdata(st,2) )
  19191. flag = script_getnum(st,2);
  19192. if (!script_charid2sd(3, sd))
  19193. return SCRIPT_CMD_FAILURE;
  19194. clif_dressing_room(sd, flag);
  19195. return SCRIPT_CMD_SUCCESS;
  19196. #else
  19197. return SCRIPT_CMD_FAILURE;
  19198. #endif
  19199. }
  19200. /**
  19201. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  19202. */
  19203. BUILDIN_FUNC(navigateto){
  19204. #if PACKETVER >= 20111010
  19205. TBL_PC* sd;
  19206. const char *mapname;
  19207. uint16 x = 0, y = 0, monster_id = 0;
  19208. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  19209. bool hideWindow = true;
  19210. mapname = script_getstr(st,2);
  19211. if( script_hasdata(st,3) )
  19212. x = script_getnum(st,3);
  19213. if( script_hasdata(st,4) )
  19214. y = script_getnum(st,4);
  19215. if( script_hasdata(st,5) )
  19216. flag = (uint8)script_getnum(st,5);
  19217. if( script_hasdata(st,6) )
  19218. hideWindow = script_getnum(st,6) ? true : false;
  19219. if( script_hasdata(st,7) )
  19220. monster_id = script_getnum(st,7);
  19221. if (!script_charid2sd(8, sd))
  19222. return SCRIPT_CMD_FAILURE;
  19223. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  19224. return SCRIPT_CMD_SUCCESS;
  19225. #else
  19226. return SCRIPT_CMD_FAILURE;
  19227. #endif
  19228. }
  19229. /**
  19230. * adopt("<parent_name>","<baby_name>");
  19231. * adopt(<parent_id>,<baby_id>);
  19232. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  19233. */
  19234. BUILDIN_FUNC(adopt)
  19235. {
  19236. TBL_PC *sd, *b_sd;
  19237. struct script_data *data;
  19238. enum adopt_responses response;
  19239. data = script_getdata(st, 2);
  19240. get_val(st, data);
  19241. if (data_isstring(data)) {
  19242. const char *name = conv_str(st, data);
  19243. sd = map_nick2sd(name,false);
  19244. if (sd == NULL) {
  19245. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  19246. return SCRIPT_CMD_FAILURE;
  19247. }
  19248. } else if (data_isint(data)) {
  19249. uint32 char_id = conv_num(st, data);
  19250. sd = map_charid2sd(char_id);
  19251. if (sd == NULL) {
  19252. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  19253. return SCRIPT_CMD_FAILURE;
  19254. }
  19255. } else {
  19256. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  19257. return SCRIPT_CMD_FAILURE;
  19258. }
  19259. data = script_getdata(st, 3);
  19260. get_val(st, data);
  19261. if (data_isstring(data)) {
  19262. const char *name = conv_str(st, data);
  19263. b_sd = map_nick2sd(name,false);
  19264. if (b_sd == NULL) {
  19265. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  19266. return SCRIPT_CMD_FAILURE;
  19267. }
  19268. } else if (data_isint(data)) {
  19269. uint32 char_id = conv_num(st, data);
  19270. b_sd = map_charid2sd(char_id);
  19271. if (b_sd == NULL) {
  19272. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  19273. return SCRIPT_CMD_FAILURE;
  19274. }
  19275. } else {
  19276. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  19277. return SCRIPT_CMD_FAILURE;
  19278. }
  19279. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  19280. if (response == ADOPT_ALLOWED) {
  19281. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  19282. b_sd->adopt_invite = sd->status.account_id;
  19283. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  19284. script_pushint(st, ADOPT_ALLOWED);
  19285. return SCRIPT_CMD_SUCCESS;
  19286. }
  19287. script_pushint(st, response);
  19288. return SCRIPT_CMD_FAILURE;
  19289. }
  19290. /**
  19291. * Returns the minimum or maximum of all the given parameters for integer variables.
  19292. *
  19293. * min( <value or array>{,value or array 2,...} );
  19294. * minimum( <value or array>{,value or array 2,...} );
  19295. * max( <value or array>{,value or array 2,...} );
  19296. * maximum( <value or array>{,value or array 2,...} );
  19297. */
  19298. BUILDIN_FUNC(minmax){
  19299. char *functionname;
  19300. unsigned int i;
  19301. int value;
  19302. // Function pointer for our comparison function (either min or max at the moment)
  19303. int32 (*func)(int32, int32);
  19304. // Get the real function name
  19305. functionname = script_getfuncname(st);
  19306. // Our data should start at offset 2
  19307. i = 2;
  19308. if( !script_hasdata( st, i ) ){
  19309. ShowError( "buildin_%s: no arguments given!\n", functionname );
  19310. st->state = END;
  19311. return SCRIPT_CMD_FAILURE;
  19312. }
  19313. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  19314. value = INT_MAX;
  19315. func = i32min;
  19316. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  19317. value = INT_MIN;
  19318. func = i32max;
  19319. }else{
  19320. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  19321. st->state = END;
  19322. return SCRIPT_CMD_FAILURE;
  19323. }
  19324. // As long as we have data on our script stack
  19325. while( script_hasdata(st,i) ){
  19326. struct script_data *data;
  19327. // Get the next piece of data from the script stack
  19328. data = script_getdata( st, i );
  19329. // Is the current parameter a single integer?
  19330. if( data_isint( data ) ){
  19331. value = func( value, script_getnum( st, i ) );
  19332. // Is the current parameter an array variable?
  19333. }else if( data_isreference( data ) ){
  19334. const char *name;
  19335. struct map_session_data* sd;
  19336. unsigned int start, end;
  19337. // Get the name of the variable
  19338. name = reference_getname(data);
  19339. // Check if it's a string variable
  19340. if( is_string_variable( name ) ){
  19341. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  19342. script_reportdata( data );
  19343. st->state = END;
  19344. return SCRIPT_CMD_FAILURE;
  19345. }
  19346. // Get the session data, if a player is attached
  19347. sd = st->rid ? map_id2sd(st->rid) : NULL;
  19348. // Try to find the array's source pointer
  19349. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  19350. ShowError( "buildin_%s: not a array!\n", functionname );
  19351. script_reportdata( data );
  19352. st->state = END;
  19353. return SCRIPT_CMD_FAILURE;
  19354. }
  19355. // Get the start and end indices of the array
  19356. start = reference_getindex( data );
  19357. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  19358. // Skip empty arrays
  19359. if( start < end ){
  19360. int id;
  19361. // For getting the values we need the id of the array
  19362. id = reference_getid( data );
  19363. // Loop through each value stored in the array
  19364. for( ; start < end; start++ ){
  19365. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  19366. script_removetop( st, -1, 0 );
  19367. }
  19368. }
  19369. }else{
  19370. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  19371. script_reportdata( data );
  19372. st->state = END;
  19373. return SCRIPT_CMD_FAILURE;
  19374. }
  19375. // Continue with the next stack entry
  19376. i++;
  19377. }
  19378. script_pushint( st, value );
  19379. return SCRIPT_CMD_SUCCESS;
  19380. }
  19381. /**
  19382. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  19383. * Unlike `getexp` that affected by some adjustments.
  19384. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  19385. * @author [Cydh]
  19386. **/
  19387. BUILDIN_FUNC(getexp2) {
  19388. TBL_PC *sd = NULL;
  19389. int base_exp = script_getnum(st, 2);
  19390. int job_exp = script_getnum(st, 3);
  19391. if (!script_charid2sd(4, sd))
  19392. return SCRIPT_CMD_FAILURE;
  19393. if (base_exp == 0 && job_exp == 0)
  19394. return SCRIPT_CMD_SUCCESS;
  19395. if (base_exp > 0)
  19396. pc_gainexp(sd, NULL, base_exp, 0, 2);
  19397. else if (base_exp < 0)
  19398. pc_lostexp(sd, base_exp * -1, 0);
  19399. if (job_exp > 0)
  19400. pc_gainexp(sd, NULL, 0, job_exp, 2);
  19401. else if (job_exp < 0)
  19402. pc_lostexp(sd, 0, job_exp * -1);
  19403. return SCRIPT_CMD_SUCCESS;
  19404. }
  19405. /**
  19406. * Force stat recalculation of sd
  19407. * recalculatestat;
  19408. * @author [secretdataz]
  19409. **/
  19410. BUILDIN_FUNC(recalculatestat) {
  19411. TBL_PC* sd;
  19412. if (!script_rid2sd(sd))
  19413. return SCRIPT_CMD_FAILURE;
  19414. status_calc_pc(sd, SCO_FORCE);
  19415. return SCRIPT_CMD_SUCCESS;
  19416. }
  19417. BUILDIN_FUNC(hateffect){
  19418. #if PACKETVER >= 20150513
  19419. struct map_session_data* sd;
  19420. bool enable;
  19421. int i, effectID;
  19422. if( !script_rid2sd(sd) )
  19423. return SCRIPT_CMD_FAILURE;
  19424. effectID = script_getnum(st,2);
  19425. enable = script_getnum(st,3) ? true : false;
  19426. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  19427. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  19428. return SCRIPT_CMD_FAILURE;
  19429. }
  19430. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  19431. if( enable ){
  19432. if( i < sd->hatEffectCount ){
  19433. return SCRIPT_CMD_SUCCESS;
  19434. }
  19435. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  19436. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  19437. sd->hatEffectCount++;
  19438. }else{
  19439. if( i == sd->hatEffectCount ){
  19440. return SCRIPT_CMD_SUCCESS;
  19441. }
  19442. for( ; i < sd->hatEffectCount - 1; i++ ){
  19443. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  19444. }
  19445. sd->hatEffectCount--;
  19446. if( !sd->hatEffectCount ){
  19447. aFree(sd->hatEffectIDs);
  19448. sd->hatEffectIDs = NULL;
  19449. }
  19450. }
  19451. if( !sd->state.connect_new ){
  19452. clif_hat_effect_single( sd, effectID, enable );
  19453. }
  19454. #endif
  19455. return SCRIPT_CMD_SUCCESS;
  19456. }
  19457. /**
  19458. * Retrieves param of current random option. Intended for random option script only.
  19459. * getrandomoptinfo(<type>);
  19460. * @author [secretdataz]
  19461. **/
  19462. BUILDIN_FUNC(getrandomoptinfo) {
  19463. struct map_session_data *sd;
  19464. int val;
  19465. 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) {
  19466. int param = script_getnum(st, 2);
  19467. switch (param) {
  19468. case ROA_ID:
  19469. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  19470. break;
  19471. case ROA_VALUE:
  19472. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  19473. break;
  19474. case ROA_PARAM:
  19475. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  19476. break;
  19477. default:
  19478. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  19479. val = 0;
  19480. break;
  19481. }
  19482. script_pushint(st, val);
  19483. }
  19484. else {
  19485. script_pushint(st, 0);
  19486. }
  19487. return SCRIPT_CMD_SUCCESS;
  19488. }
  19489. /**
  19490. * Retrieves a random option on an equipped item.
  19491. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  19492. * @author [secretdataz]
  19493. */
  19494. BUILDIN_FUNC(getequiprandomoption) {
  19495. struct map_session_data *sd;
  19496. int val;
  19497. short i = -1;
  19498. int pos = script_getnum(st, 2);
  19499. int index = script_getnum(st, 3);
  19500. int type = script_getnum(st, 4);
  19501. if (!script_charid2sd(5, sd)) {
  19502. script_pushint(st, -1);
  19503. return SCRIPT_CMD_FAILURE;
  19504. }
  19505. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19506. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  19507. script_pushint(st, -1);
  19508. return SCRIPT_CMD_FAILURE;
  19509. }
  19510. if (equip_index_check(pos))
  19511. i = pc_checkequip(sd, equip_bitmask[pos]);
  19512. if (i < 0) {
  19513. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19514. script_pushint(st, -1);
  19515. return SCRIPT_CMD_FAILURE;
  19516. }
  19517. switch (type) {
  19518. case ROA_ID:
  19519. val = sd->inventory.u.items_inventory[i].option[index].id;
  19520. break;
  19521. case ROA_VALUE:
  19522. val = sd->inventory.u.items_inventory[i].option[index].value;
  19523. break;
  19524. case ROA_PARAM:
  19525. val = sd->inventory.u.items_inventory[i].option[index].param;
  19526. break;
  19527. default:
  19528. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19529. val = 0;
  19530. break;
  19531. }
  19532. script_pushint(st, val);
  19533. return SCRIPT_CMD_SUCCESS;
  19534. }
  19535. /**
  19536. * Adds a random option on a random option slot on an equipped item and overwrites
  19537. * existing random option in the process.
  19538. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19539. * @author [secretdataz]
  19540. */
  19541. BUILDIN_FUNC(setrandomoption) {
  19542. struct map_session_data *sd;
  19543. struct s_random_opt_data *opt;
  19544. int pos, index, id, value, param, ep;
  19545. int i = -1;
  19546. if (!script_charid2sd(7, sd))
  19547. return SCRIPT_CMD_FAILURE;
  19548. pos = script_getnum(st, 2);
  19549. index = script_getnum(st, 3);
  19550. id = script_getnum(st, 4);
  19551. value = script_getnum(st, 5);
  19552. param = script_getnum(st, 6);
  19553. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19554. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19555. script_pushint(st, 0);
  19556. return SCRIPT_CMD_FAILURE;
  19557. }
  19558. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19559. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19560. script_pushint(st, 0);
  19561. return SCRIPT_CMD_FAILURE;
  19562. }
  19563. if (equip_index_check(pos))
  19564. i = pc_checkequip(sd, equip_bitmask[pos]);
  19565. if (i >= 0) {
  19566. ep = sd->inventory.u.items_inventory[i].equip;
  19567. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19568. pc_unequipitem(sd, i, 2);
  19569. sd->inventory.u.items_inventory[i].option[index].id = id;
  19570. sd->inventory.u.items_inventory[i].option[index].value = value;
  19571. sd->inventory.u.items_inventory[i].option[index].param = param;
  19572. clif_delitem(sd, i, 1, 3);
  19573. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19574. clif_additem(sd, i, 1, 0);
  19575. pc_equipitem(sd, i, ep);
  19576. script_pushint(st, 1);
  19577. return SCRIPT_CMD_SUCCESS;
  19578. }
  19579. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19580. script_pushint(st, 0);
  19581. return SCRIPT_CMD_FAILURE;
  19582. }
  19583. /// Returns the number of stat points needed to change the specified stat by val.
  19584. /// If val is negative, returns the number of stat points that would be needed to
  19585. /// raise the specified stat from (current value - val) to current value.
  19586. /// *needed_status_point(<type>,<val>{,<char id>});
  19587. /// @author [secretdataz]
  19588. BUILDIN_FUNC(needed_status_point) {
  19589. struct map_session_data *sd;
  19590. int type, val;
  19591. if (!script_charid2sd(4, sd))
  19592. return SCRIPT_CMD_FAILURE;
  19593. type = script_getnum(st, 2);
  19594. val = script_getnum(st, 3);
  19595. script_pushint(st, pc_need_status_point(sd, type, val));
  19596. return SCRIPT_CMD_SUCCESS;
  19597. }
  19598. /**
  19599. * jobcanentermap("<mapname>"{,<JobID>});
  19600. * Check if (player with) JobID can enter the map.
  19601. * @param mapname Map name
  19602. * @param JobID Player's JobID (optional)
  19603. **/
  19604. BUILDIN_FUNC(jobcanentermap) {
  19605. const char *mapname = script_getstr(st, 2);
  19606. int mapidx = mapindex_name2id(mapname), m = -1;
  19607. int jobid = 0;
  19608. TBL_PC *sd = NULL;
  19609. if (!mapidx) {// Invalid map
  19610. script_pushint(st, false);
  19611. return SCRIPT_CMD_FAILURE;
  19612. }
  19613. m = map_mapindex2mapid(mapidx);
  19614. if (m == -1) { // Map is on different map server
  19615. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  19616. script_pushint(st, false);
  19617. return SCRIPT_CMD_FAILURE;
  19618. }
  19619. if (script_hasdata(st, 3)) {
  19620. jobid = script_getnum(st, 3);
  19621. } else {
  19622. if (!script_rid2sd(sd)) {
  19623. script_pushint(st, false);
  19624. return SCRIPT_CMD_FAILURE;
  19625. }
  19626. jobid = sd->status.class_;
  19627. }
  19628. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  19629. return SCRIPT_CMD_SUCCESS;
  19630. }
  19631. /**
  19632. * Return alliance information between the two guilds.
  19633. * getguildalliance(<guild id1>,<guild id2>);
  19634. * Return values:
  19635. * -2 - Guild ID1 does not exist
  19636. * -1 - Guild ID2 does not exist
  19637. * 0 - Both guilds have no relation OR guild ID aren't given
  19638. * 1 - Both guilds are allies
  19639. * 2 - Both guilds are antagonists
  19640. */
  19641. BUILDIN_FUNC(getguildalliance)
  19642. {
  19643. struct guild *guild_data1, *guild_data2;
  19644. int guild_id1, guild_id2, i = 0;
  19645. guild_id1 = script_getnum(st,2);
  19646. guild_id2 = script_getnum(st,3);
  19647. if (guild_id1 < 1 || guild_id2 < 1) {
  19648. script_pushint(st, 0);
  19649. return SCRIPT_CMD_SUCCESS;
  19650. }
  19651. if (guild_id1 == guild_id2) {
  19652. script_pushint(st, 1);
  19653. return SCRIPT_CMD_SUCCESS;
  19654. }
  19655. guild_data1 = guild_search(guild_id1);
  19656. guild_data2 = guild_search(guild_id2);
  19657. if (guild_data1 == NULL) {
  19658. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  19659. script_pushint(st, -2);
  19660. return SCRIPT_CMD_FAILURE;
  19661. }
  19662. if (guild_data2 == NULL) {
  19663. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  19664. script_pushint(st, -1);
  19665. return SCRIPT_CMD_FAILURE;
  19666. }
  19667. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  19668. if (i == MAX_GUILDALLIANCE) {
  19669. script_pushint(st, 0);
  19670. return SCRIPT_CMD_SUCCESS;
  19671. }
  19672. if (guild_data1->alliance[i].opposition)
  19673. script_pushint(st, 2);
  19674. else
  19675. script_pushint(st, 1);
  19676. return SCRIPT_CMD_SUCCESS;
  19677. }
  19678. /*
  19679. * openstorage2 <storage_id>,<mode>{,<account_id>}
  19680. * mode @see enum e_storage_mode
  19681. **/
  19682. BUILDIN_FUNC(openstorage2) {
  19683. int stor_id = script_getnum(st, 2);
  19684. TBL_PC *sd = NULL;
  19685. if (!script_accid2sd(4, sd)) {
  19686. script_pushint(st, 0);
  19687. return SCRIPT_CMD_FAILURE;
  19688. }
  19689. if (!storage_exists(stor_id)) {
  19690. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  19691. return SCRIPT_CMD_FAILURE;
  19692. }
  19693. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  19694. return SCRIPT_CMD_SUCCESS;
  19695. }
  19696. /**
  19697. * Create a new channel
  19698. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  19699. * @author [Cydh]
  19700. **/
  19701. BUILDIN_FUNC(channel_create) {
  19702. struct Channel tmp_chan, *ch = NULL;
  19703. const char *chname = script_getstr(st,2), *pass = NULL;
  19704. int i = channel_chk((char*)chname, NULL, 3);
  19705. TBL_PC *sd = NULL;
  19706. if (i != 0) {
  19707. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  19708. script_pushint(st,i);
  19709. return SCRIPT_CMD_FAILURE;
  19710. }
  19711. memset(&tmp_chan, 0, sizeof(struct Channel));
  19712. if (script_hasdata(st,8)) {
  19713. tmp_chan.char_id = script_getnum(st,8);
  19714. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  19715. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  19716. script_pushint(st,-5);
  19717. return SCRIPT_CMD_FAILURE;
  19718. }
  19719. tmp_chan.type = CHAN_TYPE_PRIVATE;
  19720. i = 1;
  19721. }
  19722. else {
  19723. tmp_chan.type = CHAN_TYPE_PUBLIC;
  19724. i = 0;
  19725. }
  19726. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  19727. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  19728. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  19729. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  19730. if (script_hasdata(st,5))
  19731. tmp_chan.opt = script_getnum(st,5);
  19732. else
  19733. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  19734. if (script_hasdata(st,6))
  19735. tmp_chan.msg_delay = script_getnum(st,6);
  19736. else
  19737. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  19738. if (script_hasdata(st,7))
  19739. tmp_chan.color = script_getnum(st,7);
  19740. else
  19741. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  19742. if (!(ch = channel_create(&tmp_chan))) {
  19743. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  19744. script_pushint(st,0);
  19745. return SCRIPT_CMD_FAILURE;
  19746. }
  19747. if (tmp_chan.char_id)
  19748. channel_join(ch, sd);
  19749. script_pushint(st,1);
  19750. return SCRIPT_CMD_SUCCESS;
  19751. }
  19752. /**
  19753. * Set channel option
  19754. * channel_setopt "<chname>",<option>,<value>;
  19755. * @author [Cydh]
  19756. **/
  19757. BUILDIN_FUNC(channel_setopt) {
  19758. struct Channel *ch = NULL;
  19759. const char *chname = script_getstr(st,2);
  19760. int opt = script_getnum(st,3), value = script_getnum(st,4);
  19761. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19762. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  19763. script_pushint(st,0);
  19764. return SCRIPT_CMD_FAILURE;
  19765. }
  19766. switch (opt) {
  19767. case CHAN_OPT_ANNOUNCE_SELF:
  19768. case CHAN_OPT_ANNOUNCE_JOIN:
  19769. case CHAN_OPT_ANNOUNCE_LEAVE:
  19770. case CHAN_OPT_COLOR_OVERRIDE:
  19771. case CHAN_OPT_CAN_CHAT:
  19772. case CHAN_OPT_CAN_LEAVE:
  19773. case CHAN_OPT_AUTOJOIN:
  19774. if (value)
  19775. ch->opt |= opt;
  19776. else
  19777. ch->opt &= ~opt;
  19778. break;
  19779. case CHAN_OPT_MSG_DELAY:
  19780. ch->msg_delay = value;
  19781. break;
  19782. default:
  19783. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  19784. script_pushint(st,0);
  19785. return SCRIPT_CMD_FAILURE;
  19786. }
  19787. script_pushint(st,1);
  19788. return SCRIPT_CMD_SUCCESS;
  19789. }
  19790. /**
  19791. * Set channel color
  19792. * channel_setcolor "<chname>",<color>;
  19793. * @author [Cydh]
  19794. **/
  19795. BUILDIN_FUNC(channel_setcolor) {
  19796. struct Channel *ch = NULL;
  19797. const char *chname = script_getstr(st,2);
  19798. int color = script_getnum(st,3);
  19799. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19800. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  19801. script_pushint(st,0);
  19802. return SCRIPT_CMD_FAILURE;
  19803. }
  19804. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  19805. script_pushint(st,1);
  19806. return SCRIPT_CMD_SUCCESS;
  19807. }
  19808. /**
  19809. * Set channel password
  19810. * channel_setpass "<chname>","<password>";
  19811. * @author [Cydh]
  19812. **/
  19813. BUILDIN_FUNC(channel_setpass) {
  19814. struct Channel *ch = NULL;
  19815. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  19816. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19817. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  19818. script_pushint(st,0);
  19819. return SCRIPT_CMD_FAILURE;
  19820. }
  19821. if (!passwd || !strcmpi(passwd,"null"))
  19822. memset(ch->pass, '\0', sizeof(ch->pass));
  19823. else
  19824. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  19825. script_pushint(st,1);
  19826. return SCRIPT_CMD_SUCCESS;
  19827. }
  19828. /**
  19829. * Set authorized groups
  19830. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  19831. * @author [Cydh]
  19832. **/
  19833. BUILDIN_FUNC(channel_setgroup) {
  19834. struct Channel *ch = NULL;
  19835. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  19836. int i, n = 0, group = 0;
  19837. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19838. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  19839. script_pushint(st,0);
  19840. return SCRIPT_CMD_FAILURE;
  19841. }
  19842. if (funcname[strlen(funcname)-1] == '2') {
  19843. struct script_data *data = script_getdata(st,3);
  19844. const char *varname = reference_getname(data);
  19845. int32 id, idx;
  19846. if (varname[strlen(varname)-1] == '$') {
  19847. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  19848. script_pushint(st,0);
  19849. return SCRIPT_CMD_FAILURE;
  19850. }
  19851. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  19852. if (n < 1) {
  19853. ShowError("buildin_channel_setgroup: No group id listed.\n");
  19854. script_pushint(st,0);
  19855. return SCRIPT_CMD_FAILURE;
  19856. }
  19857. if (ch->groups)
  19858. aFree(ch->groups);
  19859. ch->groups = NULL;
  19860. ch->group_count = 0;
  19861. id = reference_getid(data);
  19862. idx = reference_getindex(data);
  19863. for (i = 0; i < n; i++) {
  19864. group = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id,idx+i),reference_getref(data)));
  19865. if (group == 0) {
  19866. script_pushint(st,1);
  19867. return SCRIPT_CMD_SUCCESS;
  19868. }
  19869. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  19870. ch->groups[ch->group_count-1] = group;
  19871. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  19872. }
  19873. }
  19874. else {
  19875. group = script_getnum(st,3);
  19876. n = script_lastdata(st)-1;
  19877. if (n < 1) {
  19878. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  19879. script_pushint(st,0);
  19880. return SCRIPT_CMD_FAILURE;
  19881. }
  19882. if (ch->groups)
  19883. aFree(ch->groups);
  19884. ch->groups = NULL;
  19885. ch->group_count = 0;
  19886. if (group == 0) { // Removed group list
  19887. script_pushint(st,1);
  19888. return SCRIPT_CMD_SUCCESS;
  19889. }
  19890. CREATE(ch->groups, unsigned short, n);
  19891. for (i = 3; i < n+2; i++) {
  19892. ch->groups[ch->group_count++] = script_getnum(st,i);
  19893. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  19894. }
  19895. }
  19896. script_pushint(st,n);
  19897. return SCRIPT_CMD_SUCCESS;
  19898. }
  19899. /**
  19900. * Send message on channel
  19901. * channel_chat "<chname>","<message>"{,<color>};
  19902. * @author [Cydh]
  19903. **/
  19904. BUILDIN_FUNC(channel_chat) {
  19905. struct Channel *ch = NULL;
  19906. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  19907. char output[CHAT_SIZE_MAX+1];
  19908. unsigned long color = 0;
  19909. // Check also local channels
  19910. if (chname[0] != '#') { // By Map
  19911. int m = mapindex_name2id(chname);
  19912. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  19913. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  19914. script_pushint(st,0);
  19915. return SCRIPT_CMD_FAILURE;
  19916. }
  19917. if (!(ch = map[m].channel)) {
  19918. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  19919. script_pushint(st,0);
  19920. return SCRIPT_CMD_SUCCESS;
  19921. }
  19922. }
  19923. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  19924. TBL_NPC *nd = map_id2nd(st->oid);
  19925. if (!nd || nd->bl.m == -1) {
  19926. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  19927. script_pushint(st,0);
  19928. return SCRIPT_CMD_FAILURE;
  19929. }
  19930. if (!(ch = map[nd->bl.m].channel)) {
  19931. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  19932. script_pushint(st,0);
  19933. return SCRIPT_CMD_SUCCESS;
  19934. }
  19935. }
  19936. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19937. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  19938. script_pushint(st,0);
  19939. return SCRIPT_CMD_FAILURE;
  19940. }
  19941. if (!ch) {
  19942. script_pushint(st,0);
  19943. return SCRIPT_CMD_FAILURE;
  19944. }
  19945. color = ch->color;
  19946. FETCH(4, color);
  19947. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  19948. clif_channel_msg(ch,output,color);
  19949. script_pushint(st,1);
  19950. return SCRIPT_CMD_SUCCESS;
  19951. }
  19952. /**
  19953. * Ban player from a channel
  19954. * channel_ban "<chname>",<char_id>;
  19955. * @author [Cydh]
  19956. **/
  19957. BUILDIN_FUNC(channel_ban) {
  19958. struct Channel *ch = NULL;
  19959. const char *chname = script_getstr(st,2);
  19960. unsigned int char_id = script_getnum(st,3);
  19961. TBL_PC *tsd;
  19962. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19963. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  19964. script_pushint(st,0);
  19965. return SCRIPT_CMD_FAILURE;
  19966. }
  19967. if (ch->char_id == char_id) {
  19968. script_pushint(st,0);
  19969. return SCRIPT_CMD_SUCCESS;
  19970. }
  19971. tsd = map_charid2sd(char_id);
  19972. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  19973. script_pushint(st,0);
  19974. return SCRIPT_CMD_SUCCESS;
  19975. }
  19976. if (!idb_exists(ch->banned, char_id)) {
  19977. struct chan_banentry *cbe;
  19978. char output[CHAT_SIZE_MAX+1];
  19979. CREATE(cbe, struct chan_banentry, 1);
  19980. cbe->char_id = char_id;
  19981. if (tsd) {
  19982. strcpy(cbe->char_name,tsd->status.name);
  19983. channel_clean(ch,tsd,0);
  19984. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  19985. }
  19986. else
  19987. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  19988. idb_put(ch->banned, char_id, cbe);
  19989. clif_channel_msg(ch,output,ch->color);
  19990. }
  19991. script_pushint(st,1);
  19992. return SCRIPT_CMD_SUCCESS;
  19993. }
  19994. /**
  19995. * Ban player from a channel
  19996. * channel_unban "<chname>",<char_id>;
  19997. * @author [Cydh]
  19998. **/
  19999. BUILDIN_FUNC(channel_unban) {
  20000. struct Channel *ch = NULL;
  20001. const char *chname = script_getstr(st,2);
  20002. unsigned int char_id = script_getnum(st,3);
  20003. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20004. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  20005. script_pushint(st,0);
  20006. return SCRIPT_CMD_FAILURE;
  20007. }
  20008. if (ch->char_id == char_id) {
  20009. script_pushint(st,0);
  20010. return SCRIPT_CMD_SUCCESS;
  20011. }
  20012. if (idb_exists(ch->banned, char_id)) {
  20013. char output[CHAT_SIZE_MAX+1];
  20014. TBL_PC *tsd = map_charid2sd(char_id);
  20015. if (tsd)
  20016. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  20017. else
  20018. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  20019. idb_remove(ch->banned, char_id);
  20020. clif_channel_msg(ch,output,ch->color);
  20021. }
  20022. script_pushint(st,1);
  20023. return SCRIPT_CMD_SUCCESS;
  20024. }
  20025. /**
  20026. * Kick player from a channel
  20027. * channel_kick "<chname>",<char_id>;
  20028. * channel_kick "<chname>","<char_name>";
  20029. * @author [Cydh]
  20030. **/
  20031. BUILDIN_FUNC(channel_kick) {
  20032. struct Channel *ch = NULL;
  20033. const char *chname = script_getstr(st,2);
  20034. struct script_data *data = script_getdata(st,3);
  20035. TBL_PC *tsd = NULL;
  20036. int res = 1;
  20037. get_val(st, data);
  20038. if (data_isstring(data)) {
  20039. if (!(tsd = map_nick2sd(conv_str(st,data),false))) {
  20040. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", conv_str(st,data));
  20041. script_pushint(st,0);
  20042. return SCRIPT_CMD_FAILURE;
  20043. }
  20044. }
  20045. else {
  20046. if (!(tsd = map_charid2sd(conv_num(st,data)))) {
  20047. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", conv_num(st,data));
  20048. script_pushint(st,0);
  20049. return SCRIPT_CMD_FAILURE;
  20050. }
  20051. }
  20052. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  20053. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  20054. script_pushint(st,0);
  20055. return SCRIPT_CMD_FAILURE;
  20056. }
  20057. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20058. script_pushint(st,0);
  20059. return SCRIPT_CMD_SUCCESS;
  20060. }
  20061. switch(ch->type){
  20062. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  20063. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  20064. default: res = channel_clean(ch,tsd,0); break;
  20065. }
  20066. if (res == 0) {
  20067. char output[CHAT_SIZE_MAX+1];
  20068. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  20069. clif_channel_msg(ch,output,ch->color);
  20070. }
  20071. script_pushint(st,1);
  20072. return SCRIPT_CMD_SUCCESS;
  20073. }
  20074. /**
  20075. * Delete a channel
  20076. * channel_delete "<chname>";
  20077. * @author [Cydh]
  20078. **/
  20079. BUILDIN_FUNC(channel_delete) {
  20080. struct Channel *ch = NULL;
  20081. const char *chname = script_getstr(st,2);
  20082. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20083. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  20084. script_pushint(st,-1);
  20085. return SCRIPT_CMD_FAILURE;
  20086. }
  20087. script_pushint(st,channel_delete(ch,true));
  20088. return SCRIPT_CMD_SUCCESS;
  20089. }
  20090. BUILDIN_FUNC(unloadnpc) {
  20091. const char *name;
  20092. struct npc_data* nd;
  20093. name = script_getstr(st, 2);
  20094. nd = npc_name2id(name);
  20095. if( nd == NULL ){
  20096. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  20097. return SCRIPT_CMD_FAILURE;
  20098. } else if ( nd->bl.id == st->oid ) {
  20099. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  20100. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  20101. return SCRIPT_CMD_FAILURE;
  20102. }
  20103. npc_unload_duplicates(nd);
  20104. npc_unload(nd, true);
  20105. npc_read_event_script();
  20106. return SCRIPT_CMD_SUCCESS;
  20107. }
  20108. /**
  20109. * Add an achievement to the player's log
  20110. * achievementadd(<achievement ID>{,<char ID>});
  20111. */
  20112. BUILDIN_FUNC(achievementadd) {
  20113. struct map_session_data *sd;
  20114. int achievement_id = script_getnum(st, 2);
  20115. if (!script_charid2sd(3, sd)) {
  20116. script_pushint(st, false);
  20117. return SCRIPT_CMD_FAILURE;
  20118. }
  20119. if (achievement_exists(achievement_id) == false) {
  20120. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  20121. script_pushint(st, false);
  20122. return SCRIPT_CMD_FAILURE;
  20123. }
  20124. if( !sd->state.pc_loaded ){
  20125. if( !running_npc_stat_calc_event ){
  20126. ShowError( "buildin_achievementadd: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20127. return SCRIPT_CMD_FAILURE;
  20128. }else{
  20129. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20130. return SCRIPT_CMD_SUCCESS;
  20131. }
  20132. }
  20133. if (achievement_add(sd, achievement_id))
  20134. script_pushint(st, true);
  20135. else
  20136. script_pushint(st, false);
  20137. return SCRIPT_CMD_SUCCESS;
  20138. }
  20139. /**
  20140. * Removes an achievement on a player.
  20141. * achievementremove(<achievement ID>{,<char ID>});
  20142. * Just for Atemo. ;)
  20143. */
  20144. BUILDIN_FUNC(achievementremove) {
  20145. struct map_session_data *sd;
  20146. int achievement_id = script_getnum(st, 2);
  20147. if (!script_charid2sd(3, sd)) {
  20148. script_pushint(st, false);
  20149. return SCRIPT_CMD_FAILURE;
  20150. }
  20151. if (achievement_exists(achievement_id) == false) {
  20152. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  20153. script_pushint(st, false);
  20154. return SCRIPT_CMD_SUCCESS;
  20155. }
  20156. if( !sd->state.pc_loaded ){
  20157. if( !running_npc_stat_calc_event ){
  20158. ShowError( "buildin_achievementremove: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20159. return SCRIPT_CMD_FAILURE;
  20160. }else{
  20161. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20162. return SCRIPT_CMD_SUCCESS;
  20163. }
  20164. }
  20165. if (achievement_remove(sd, achievement_id))
  20166. script_pushint(st, true);
  20167. else
  20168. script_pushint(st, false);
  20169. return SCRIPT_CMD_SUCCESS;
  20170. }
  20171. /**
  20172. * Returns achievement progress
  20173. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  20174. */
  20175. BUILDIN_FUNC(achievementinfo) {
  20176. struct map_session_data *sd;
  20177. int achievement_id = script_getnum(st, 2);
  20178. if (!script_charid2sd(4, sd)) {
  20179. script_pushint(st, false);
  20180. return SCRIPT_CMD_FAILURE;
  20181. }
  20182. if (achievement_exists(achievement_id) == false) {
  20183. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  20184. script_pushint(st, false);
  20185. return SCRIPT_CMD_FAILURE;
  20186. }
  20187. if( !sd->state.pc_loaded ){
  20188. script_pushint(st, false);
  20189. if( !running_npc_stat_calc_event ){
  20190. ShowError( "buildin_achievementinfo: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20191. return SCRIPT_CMD_FAILURE;
  20192. }else{
  20193. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20194. return SCRIPT_CMD_SUCCESS;
  20195. }
  20196. }
  20197. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  20198. return SCRIPT_CMD_SUCCESS;
  20199. }
  20200. /**
  20201. * Award an achievement; Ignores requirements
  20202. * achievementcomplete(<achievement ID>{,<char ID>});
  20203. */
  20204. BUILDIN_FUNC(achievementcomplete) {
  20205. struct map_session_data *sd;
  20206. int i, achievement_id = script_getnum(st, 2);
  20207. if (!script_charid2sd(3, sd)) {
  20208. script_pushint(st, false);
  20209. return SCRIPT_CMD_FAILURE;
  20210. }
  20211. if (achievement_exists(achievement_id) == false) {
  20212. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  20213. script_pushint(st, false);
  20214. return SCRIPT_CMD_FAILURE;
  20215. }
  20216. if( !sd->state.pc_loaded ){
  20217. if( !running_npc_stat_calc_event ){
  20218. ShowError( "buildin_achievementcomplete: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20219. return SCRIPT_CMD_FAILURE;
  20220. }else{
  20221. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20222. return SCRIPT_CMD_SUCCESS;
  20223. }
  20224. }
  20225. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20226. if (i == sd->achievement_data.count)
  20227. achievement_add(sd, achievement_id);
  20228. achievement_update_achievement(sd, achievement_id, true);
  20229. script_pushint(st, true);
  20230. return SCRIPT_CMD_SUCCESS;
  20231. }
  20232. /**
  20233. * Checks if the achievement exists on player.
  20234. * achievementexists(<achievement ID>{,<char ID>});
  20235. */
  20236. BUILDIN_FUNC(achievementexists) {
  20237. struct map_session_data *sd;
  20238. int i, achievement_id = script_getnum(st, 2);
  20239. if (!script_charid2sd(3, sd)) {
  20240. script_pushint(st, false);
  20241. return SCRIPT_CMD_FAILURE;
  20242. }
  20243. if (achievement_exists(achievement_id) == false) {
  20244. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  20245. script_pushint(st, false);
  20246. return SCRIPT_CMD_SUCCESS;
  20247. }
  20248. if( !sd->state.pc_loaded ){
  20249. script_pushint(st, false);
  20250. if( !running_npc_stat_calc_event ){
  20251. ShowError( "buildin_achievementexists: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20252. return SCRIPT_CMD_FAILURE;
  20253. }else{
  20254. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20255. return SCRIPT_CMD_SUCCESS;
  20256. }
  20257. }
  20258. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20259. script_pushint(st, i < sd->achievement_data.count ? true : false);
  20260. return SCRIPT_CMD_SUCCESS;
  20261. }
  20262. /**
  20263. * Updates an achievement's value.
  20264. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  20265. */
  20266. BUILDIN_FUNC(achievementupdate) {
  20267. struct map_session_data *sd;
  20268. int i, achievement_id, type, value;
  20269. achievement_id = script_getnum(st, 2);
  20270. type = script_getnum(st, 3);
  20271. value = script_getnum(st, 4);
  20272. if (!script_charid2sd(5, sd)) {
  20273. script_pushint(st, false);
  20274. return SCRIPT_CMD_FAILURE;
  20275. }
  20276. if (achievement_exists(achievement_id) == false) {
  20277. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  20278. script_pushint(st, false);
  20279. return SCRIPT_CMD_FAILURE;
  20280. }
  20281. if( !sd->state.pc_loaded ){
  20282. if( !running_npc_stat_calc_event ){
  20283. ShowError( "buildin_achievementupdate: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20284. return SCRIPT_CMD_FAILURE;
  20285. }else{
  20286. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20287. return SCRIPT_CMD_SUCCESS;
  20288. }
  20289. }
  20290. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20291. if (i == sd->achievement_data.count)
  20292. achievement_add(sd, achievement_id);
  20293. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20294. if (i == sd->achievement_data.count) {
  20295. script_pushint(st, false);
  20296. return SCRIPT_CMD_SUCCESS;
  20297. }
  20298. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  20299. sd->achievement_data.achievements[i].count[type - 1] = value;
  20300. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  20301. sd->achievement_data.achievements[i].completed = value;
  20302. else if (type == ACHIEVEINFO_GOTREWARD)
  20303. sd->achievement_data.achievements[i].rewarded = value;
  20304. else {
  20305. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  20306. script_pushint(st, false);
  20307. return SCRIPT_CMD_FAILURE;
  20308. }
  20309. achievement_update_achievement(sd, achievement_id, false);
  20310. script_pushint(st, true);
  20311. return SCRIPT_CMD_SUCCESS;
  20312. }
  20313. /**
  20314. * Get an equipment's refine cost
  20315. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  20316. * returns -1 on fail
  20317. */
  20318. BUILDIN_FUNC(getequiprefinecost) {
  20319. int i = -1, slot, type, info;
  20320. map_session_data *sd;
  20321. slot = script_getnum(st, 2);
  20322. type = script_getnum(st, 3);
  20323. info = script_getnum(st, 4);
  20324. if (!script_charid2sd(5, sd)) {
  20325. script_pushint(st, 0);
  20326. return SCRIPT_CMD_FAILURE;
  20327. }
  20328. if (type < 0 || type >= REFINE_COST_MAX) {
  20329. script_pushint(st, -1);
  20330. return SCRIPT_CMD_SUCCESS;
  20331. }
  20332. if (equip_index_check(slot))
  20333. i = pc_checkequip(sd, equip_bitmask[slot]);
  20334. if (i < 0) {
  20335. script_pushint(st, -1);
  20336. return SCRIPT_CMD_SUCCESS;
  20337. }
  20338. int weapon_lv = sd->inventory_data[i]->wlv;
  20339. if (sd->inventory_data[i]->type == IT_SHADOWGEAR) {
  20340. if (sd->inventory_data[i]->equip == EQP_SHADOW_WEAPON)
  20341. weapon_lv = REFINE_TYPE_WEAPON4;
  20342. else
  20343. weapon_lv = REFINE_TYPE_SHADOW;
  20344. }
  20345. script_pushint(st, status_get_refine_cost(weapon_lv, type, info != 0));
  20346. return SCRIPT_CMD_SUCCESS;
  20347. }
  20348. /**
  20349. * Round, floor, ceiling a number to arbitrary integer precision.
  20350. * round(<number>,<precision>);
  20351. * ceil(<number>,<precision>);
  20352. * floor(<number>,<precision>);
  20353. */
  20354. BUILDIN_FUNC(round) {
  20355. int num = script_getnum(st, 2);
  20356. int precision = script_getnum(st, 3);
  20357. char* func = script_getfuncname(st);
  20358. if (precision <= 0) {
  20359. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  20360. return SCRIPT_CMD_FAILURE;
  20361. }
  20362. if (strcasecmp(func, "floor") == 0) {
  20363. script_pushint(st, num - (num % precision));
  20364. }
  20365. else if (strcasecmp(func, "ceil") == 0) {
  20366. script_pushint(st, num + precision - (num % precision));
  20367. }
  20368. else {
  20369. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  20370. }
  20371. return SCRIPT_CMD_SUCCESS;
  20372. }
  20373. BUILDIN_FUNC(getequiptradability) {
  20374. int i, num;
  20375. TBL_PC *sd;
  20376. num = script_getnum(st, 2);
  20377. if (!script_charid2sd(3, sd)) {
  20378. return SCRIPT_CMD_FAILURE;
  20379. }
  20380. if (equip_index_check(num))
  20381. i = pc_checkequip(sd, equip_bitmask[num]);
  20382. else{
  20383. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  20384. return SCRIPT_CMD_FAILURE;
  20385. }
  20386. if (i >= 0) {
  20387. bool tradable = (sd->inventory.u.items_inventory[i].expire_time == 0 &&
  20388. (!sd->inventory.u.items_inventory[i].bound || pc_can_give_bounded_items(sd)) &&
  20389. itemdb_cantrade(&sd->inventory.u.items_inventory[i], pc_get_group_level(sd), pc_get_group_level(sd))
  20390. );
  20391. script_pushint(st, tradable);
  20392. }
  20393. else
  20394. script_pushint(st, false);
  20395. return SCRIPT_CMD_SUCCESS;
  20396. }
  20397. 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 ){
  20398. const char *name;
  20399. // Check if it is a variable
  20400. if( !data_isreference(data) ){
  20401. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  20402. return false;
  20403. }
  20404. name = reference_getname(data);
  20405. if( is_string_variable(name) ){
  20406. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  20407. return false;
  20408. }
  20409. // Check if the variable requires a player
  20410. if( not_server_variable(*name) && sd == NULL ){
  20411. // If no player is attached
  20412. if( !script_rid2sd(sd) ){
  20413. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  20414. return false;
  20415. }
  20416. }
  20417. // Try to find the array's source pointer
  20418. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  20419. ShowError( "buildin_mail: variable \"%s\" is not an array.\n" );
  20420. return false;
  20421. }
  20422. // Store the name for later usage
  20423. *out_name = name;
  20424. // Get the start and end indices of the array
  20425. *start = reference_getindex(data);
  20426. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  20427. // For getting the values we need the id of the array
  20428. *id = reference_getid(data);
  20429. return true;
  20430. }
  20431. BUILDIN_FUNC(mail){
  20432. const char *sender, *title, *body, *name;
  20433. struct mail_message msg;
  20434. struct script_data *data;
  20435. struct map_session_data *sd = NULL;
  20436. unsigned int i, j, k, num_items, start, end;
  20437. int32 id;
  20438. memset(&msg, 0, sizeof(struct mail_message));
  20439. msg.dest_id = script_getnum(st,2);
  20440. sender = script_getstr(st, 3);
  20441. if( strlen(sender) > NAME_LENGTH ){
  20442. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  20443. return SCRIPT_CMD_FAILURE;
  20444. }
  20445. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  20446. title = script_getstr(st, 4);
  20447. if( strlen(title) > MAIL_TITLE_LENGTH ){
  20448. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  20449. return SCRIPT_CMD_FAILURE;
  20450. }
  20451. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  20452. body = script_getstr(st, 5);
  20453. if( strlen(body) > MAIL_BODY_LENGTH ){
  20454. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  20455. return SCRIPT_CMD_FAILURE;
  20456. }
  20457. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  20458. if( script_hasdata(st,6) ){
  20459. int zeny = script_getnum(st, 6);
  20460. if( zeny < 0 ){
  20461. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  20462. return SCRIPT_CMD_FAILURE;
  20463. }else if( zeny > MAX_ZENY ){
  20464. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  20465. zeny = MAX_ZENY;
  20466. }
  20467. msg.zeny = zeny;
  20468. }
  20469. // Items
  20470. num_items = 0;
  20471. while( script_hasdata(st,7) ){
  20472. data = script_getdata(st,7);
  20473. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  20474. return SCRIPT_CMD_FAILURE;
  20475. }
  20476. num_items = end - start;
  20477. if( num_items == 0 ){
  20478. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  20479. break;
  20480. }
  20481. if( num_items > MAIL_MAX_ITEM ){
  20482. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  20483. num_items = MAIL_MAX_ITEM;
  20484. }
  20485. for( i = 0; i < num_items && start < end; i++, start++ ){
  20486. msg.item[i].nameid = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20487. msg.item[i].identify = 1;
  20488. script_removetop(st, -1, 0);
  20489. if( !itemdb_exists(msg.item[i].nameid) ){
  20490. ShowError( "buildin_mail: invalid item id %hu.\n", msg.item[i].nameid );
  20491. return SCRIPT_CMD_FAILURE;
  20492. }
  20493. }
  20494. // Amounts
  20495. if( !script_hasdata(st,8) ){
  20496. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  20497. return SCRIPT_CMD_FAILURE;
  20498. }
  20499. data = script_getdata(st,8);
  20500. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  20501. return SCRIPT_CMD_FAILURE;
  20502. }
  20503. for( i = 0; i < num_items && start < end; i++, start++ ){
  20504. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  20505. msg.item[i].amount = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20506. script_removetop(st, -1, 0);
  20507. if( msg.item[i].amount <= 0 ){
  20508. ShowError( "buildin_mail: amount %d for item %hu is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  20509. return SCRIPT_CMD_FAILURE;
  20510. }else if( itemdb_isstackable2(item) ){
  20511. uint16 max = item->stack.amount > 0 ? item->stack.amount : MAX_AMOUNT;
  20512. if( msg.item[i].amount > max ){
  20513. ShowWarning( "buildin_mail: amount %d for item %hu is exceeding the maximum of %d. Capping...\n", msg.item[i].amount, msg.item[i].nameid, max );
  20514. msg.item[i].amount = max;
  20515. }
  20516. }else{
  20517. if( msg.item[i].amount > 1 ){
  20518. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %hu.\n", msg.item[i].amount, msg.item[i].nameid );
  20519. msg.item[i].amount = 1;
  20520. }
  20521. }
  20522. }
  20523. // Cards
  20524. if( !script_hasdata(st,9) ){
  20525. break;
  20526. }
  20527. for( i = 0, j = 9; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  20528. data = script_getdata(st,j);
  20529. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20530. return SCRIPT_CMD_FAILURE;
  20531. }
  20532. for( k = 0; k < num_items && start < end; k++, start++ ){
  20533. msg.item[k].card[i] = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20534. script_removetop(st, -1, 0);
  20535. if( msg.item[k].card[i] != 0 && !itemdb_exists(msg.item[k].card[i]) ){
  20536. ShowError( "buildin_mail: invalid card id %hu.\n", msg.item[k].card[i] );
  20537. return SCRIPT_CMD_FAILURE;
  20538. }
  20539. }
  20540. }
  20541. // Random Options
  20542. if( !script_hasdata(st,9 + MAX_SLOTS) ){
  20543. break;
  20544. }
  20545. for( i = 0, j = 9 + MAX_SLOTS; i < MAX_ITEM_RDM_OPT && script_hasdata(st,j) && script_hasdata(st,j + 1) && script_hasdata(st,j + 2); i++, j++ ){
  20546. // Option IDs
  20547. data = script_getdata(st, j);
  20548. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20549. return SCRIPT_CMD_FAILURE;
  20550. }
  20551. for( k = 0; k < num_items && start < end; k++, start++ ){
  20552. msg.item[k].option[i].id = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20553. script_removetop(st, -1, 0);
  20554. }
  20555. j++;
  20556. // Option values
  20557. data = script_getdata(st, j);
  20558. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20559. return SCRIPT_CMD_FAILURE;
  20560. }
  20561. for( k = 0; k < num_items && start < end; k++, start++ ){
  20562. msg.item[k].option[i].value = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20563. script_removetop(st, -1, 0);
  20564. }
  20565. j++;
  20566. // Option parameters
  20567. data = script_getdata(st, j);
  20568. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20569. return SCRIPT_CMD_FAILURE;
  20570. }
  20571. for( k = 0; k < num_items && start < end; k++, start++ ){
  20572. msg.item[k].option[i].param = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20573. script_removetop(st, -1, 0);
  20574. }
  20575. }
  20576. // Break the pseudo scope
  20577. break;
  20578. }
  20579. msg.status = MAIL_NEW;
  20580. msg.type = MAIL_INBOX_NORMAL;
  20581. msg.timestamp = time(NULL);
  20582. intif_Mail_send(0, &msg);
  20583. return SCRIPT_CMD_SUCCESS;
  20584. }
  20585. BUILDIN_FUNC(open_roulette){
  20586. #if PACKETVER < 20141022
  20587. ShowError( "buildin_open_roulette: This command requires PACKETVER 2014-10-22 or newer.\n" );
  20588. return SCRIPT_CMD_FAILURE;
  20589. #else
  20590. struct map_session_data* sd;
  20591. if( !battle_config.feature_roulette ){
  20592. ShowError( "buildin_open_roulette: Roulette system is disabled.\n" );
  20593. return SCRIPT_CMD_FAILURE;
  20594. }
  20595. if( !script_charid2sd( 2, sd ) ){
  20596. return SCRIPT_CMD_FAILURE;
  20597. }
  20598. clif_roulette_open(sd);
  20599. return SCRIPT_CMD_SUCCESS;
  20600. #endif
  20601. }
  20602. #include "../custom/script.inc"
  20603. // declarations that were supposed to be exported from npc_chat.c
  20604. #ifdef PCRE_SUPPORT
  20605. BUILDIN_FUNC(defpattern);
  20606. BUILDIN_FUNC(activatepset);
  20607. BUILDIN_FUNC(deactivatepset);
  20608. BUILDIN_FUNC(deletepset);
  20609. #endif
  20610. /** Regular expression matching
  20611. * preg_match(<pattern>,<string>{,<offset>})
  20612. */
  20613. BUILDIN_FUNC(preg_match) {
  20614. #ifdef PCRE_SUPPORT
  20615. pcre *re;
  20616. pcre_extra *pcreExtra;
  20617. const char *error;
  20618. int erroffset, r, offset = 0;
  20619. int subStrVec[30];
  20620. const char* pattern = script_getstr(st,2);
  20621. const char* subject = script_getstr(st,3);
  20622. if (script_hasdata(st,4))
  20623. offset = script_getnum(st,4);
  20624. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  20625. pcreExtra = pcre_study(re, 0, &error);
  20626. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  20627. pcre_free(re);
  20628. if (pcreExtra != NULL)
  20629. pcre_free(pcreExtra);
  20630. if (r < 0)
  20631. script_pushint(st,0);
  20632. else
  20633. script_pushint(st,(r > 0) ? r : 30 / 3);
  20634. return SCRIPT_CMD_SUCCESS;
  20635. #else
  20636. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  20637. script_pushint(st,0);
  20638. return SCRIPT_CMD_SUCCESS;
  20639. #endif
  20640. }
  20641. /// script command definitions
  20642. /// for an explanation on args, see add_buildin_func
  20643. struct script_function buildin_func[] = {
  20644. // NPC interaction
  20645. BUILDIN_DEF(mes,"s*"),
  20646. BUILDIN_DEF(next,""),
  20647. BUILDIN_DEF(clear,""),
  20648. BUILDIN_DEF(close,""),
  20649. BUILDIN_DEF(close2,""),
  20650. BUILDIN_DEF(menu,"sl*"),
  20651. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  20652. BUILDIN_DEF(prompt,"s*"),
  20653. //
  20654. BUILDIN_DEF(goto,"l"),
  20655. BUILDIN_DEF(callsub,"l*"),
  20656. BUILDIN_DEF(callfunc,"s*"),
  20657. BUILDIN_DEF(return,"?"),
  20658. BUILDIN_DEF(getarg,"i?"),
  20659. BUILDIN_DEF(jobchange,"i??"),
  20660. BUILDIN_DEF(jobname,"i"),
  20661. BUILDIN_DEF(input,"r??"),
  20662. BUILDIN_DEF(warp,"sii?"),
  20663. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  20664. BUILDIN_DEF(areawarp,"siiiisii??"),
  20665. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  20666. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  20667. BUILDIN_DEF(setlook,"ii?"),
  20668. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  20669. BUILDIN_DEF2(setr,"set","rv?"),
  20670. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  20671. BUILDIN_DEF(setarray,"rv*"),
  20672. BUILDIN_DEF(cleararray,"rvi"),
  20673. BUILDIN_DEF(copyarray,"rri"),
  20674. BUILDIN_DEF(getarraysize,"r"),
  20675. BUILDIN_DEF(deletearray,"r?"),
  20676. BUILDIN_DEF(getelementofarray,"ri"),
  20677. BUILDIN_DEF(inarray,"rv"),
  20678. BUILDIN_DEF(countinarray,"rr"),
  20679. BUILDIN_DEF(getitem,"vi?"),
  20680. BUILDIN_DEF(rentitem,"vi?"),
  20681. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  20682. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  20683. BUILDIN_DEF(getnameditem,"vv"),
  20684. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  20685. BUILDIN_DEF(makeitem,"visii"),
  20686. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  20687. BUILDIN_DEF(delitem,"vi?"),
  20688. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  20689. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  20690. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  20691. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  20692. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  20693. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  20694. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  20695. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  20696. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  20697. BUILDIN_DEF(cutin,"si"),
  20698. BUILDIN_DEF(viewpoint,"iiiii"),
  20699. BUILDIN_DEF(heal,"ii?"),
  20700. BUILDIN_DEF(itemheal,"ii?"),
  20701. BUILDIN_DEF(percentheal,"ii?"),
  20702. BUILDIN_DEF(rand,"i?"),
  20703. BUILDIN_DEF(countitem,"v?"),
  20704. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  20705. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  20706. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  20707. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  20708. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  20709. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  20710. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  20711. BUILDIN_DEF(checkweight,"vi*"),
  20712. BUILDIN_DEF(checkweight2,"rr"),
  20713. BUILDIN_DEF(readparam,"i?"),
  20714. BUILDIN_DEF(getcharid,"i?"),
  20715. BUILDIN_DEF(getnpcid,"i?"),
  20716. BUILDIN_DEF(getpartyname,"i"),
  20717. BUILDIN_DEF(getpartymember,"i??"),
  20718. BUILDIN_DEF(getpartyleader,"i?"),
  20719. BUILDIN_DEF(getguildname,"i"),
  20720. BUILDIN_DEF(getguildmaster,"i"),
  20721. BUILDIN_DEF(getguildmasterid,"i"),
  20722. BUILDIN_DEF(strcharinfo,"i?"),
  20723. BUILDIN_DEF(strnpcinfo,"i"),
  20724. BUILDIN_DEF(getequipid,"??"),
  20725. BUILDIN_DEF(getequipuniqueid,"i?"),
  20726. BUILDIN_DEF(getequipname,"i?"),
  20727. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  20728. BUILDIN_DEF(repair,"i?"), // [Valaris]
  20729. BUILDIN_DEF(repairall,"?"),
  20730. BUILDIN_DEF(getequipisequiped,"i?"),
  20731. BUILDIN_DEF(getequipisenableref,"i?"),
  20732. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  20733. BUILDIN_DEF(getequipweaponlv,"i?"),
  20734. BUILDIN_DEF(getequippercentrefinery,"i?"),
  20735. BUILDIN_DEF(successrefitem,"i??"),
  20736. BUILDIN_DEF(failedrefitem,"i?"),
  20737. BUILDIN_DEF(downrefitem,"i??"),
  20738. BUILDIN_DEF(statusup,"i?"),
  20739. BUILDIN_DEF(statusup2,"ii?"),
  20740. BUILDIN_DEF(bonus,"i?"),
  20741. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  20742. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  20743. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  20744. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  20745. BUILDIN_DEF(autobonus,"sii??"),
  20746. BUILDIN_DEF(autobonus2,"sii??"),
  20747. BUILDIN_DEF(autobonus3,"siiv?"),
  20748. BUILDIN_DEF(skill,"vi?"),
  20749. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  20750. BUILDIN_DEF(guildskill,"vi"),
  20751. BUILDIN_DEF(getskilllv,"v"),
  20752. BUILDIN_DEF(getgdskilllv,"iv"),
  20753. BUILDIN_DEF(basicskillcheck,""),
  20754. BUILDIN_DEF(getgmlevel,"?"),
  20755. BUILDIN_DEF(getgroupid,"?"),
  20756. BUILDIN_DEF(end,""),
  20757. BUILDIN_DEF(checkoption,"i?"),
  20758. BUILDIN_DEF(setoption,"i??"),
  20759. BUILDIN_DEF(setcart,"??"),
  20760. BUILDIN_DEF(checkcart,"?"),
  20761. BUILDIN_DEF(setfalcon,"??"),
  20762. BUILDIN_DEF(checkfalcon,"?"),
  20763. BUILDIN_DEF(setriding,"??"),
  20764. BUILDIN_DEF(checkriding,"?"),
  20765. BUILDIN_DEF(checkwug,"?"),
  20766. BUILDIN_DEF(checkmadogear,"?"),
  20767. BUILDIN_DEF(setmadogear,"??"),
  20768. BUILDIN_DEF2(savepoint,"save","sii???"),
  20769. BUILDIN_DEF(savepoint,"sii???"),
  20770. BUILDIN_DEF(gettimetick,"i"),
  20771. BUILDIN_DEF(gettime,"i"),
  20772. BUILDIN_DEF(gettimestr,"si?"),
  20773. BUILDIN_DEF(openstorage,""),
  20774. BUILDIN_DEF(guildopenstorage,""),
  20775. BUILDIN_DEF(itemskill,"vi?"),
  20776. BUILDIN_DEF(produce,"i"),
  20777. BUILDIN_DEF(cooking,"i"),
  20778. BUILDIN_DEF(monster,"siisii???"),
  20779. BUILDIN_DEF(getmobdrops,"i"),
  20780. BUILDIN_DEF(areamonster,"siiiisii???"),
  20781. BUILDIN_DEF(killmonster,"ss?"),
  20782. BUILDIN_DEF(killmonsterall,"s?"),
  20783. BUILDIN_DEF(clone,"siisi????"),
  20784. BUILDIN_DEF(doevent,"s"),
  20785. BUILDIN_DEF(donpcevent,"s"),
  20786. BUILDIN_DEF(cmdothernpc,"ss"),
  20787. BUILDIN_DEF(addtimer,"is"),
  20788. BUILDIN_DEF(deltimer,"s"),
  20789. BUILDIN_DEF(addtimercount,"is"),
  20790. BUILDIN_DEF(initnpctimer,"??"),
  20791. BUILDIN_DEF(stopnpctimer,"??"),
  20792. BUILDIN_DEF(startnpctimer,"??"),
  20793. BUILDIN_DEF(setnpctimer,"i?"),
  20794. BUILDIN_DEF(getnpctimer,"i?"),
  20795. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  20796. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  20797. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  20798. BUILDIN_DEF(announce,"si?????"),
  20799. BUILDIN_DEF(mapannounce,"ssi?????"),
  20800. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  20801. BUILDIN_DEF(getusers,"i"),
  20802. BUILDIN_DEF(getmapguildusers,"si"),
  20803. BUILDIN_DEF(getmapusers,"s"),
  20804. BUILDIN_DEF(getareausers,"siiii"),
  20805. BUILDIN_DEF(getareadropitem,"siiiiv"),
  20806. BUILDIN_DEF(enablenpc,"s"),
  20807. BUILDIN_DEF(disablenpc,"s"),
  20808. BUILDIN_DEF(hideoffnpc,"s"),
  20809. BUILDIN_DEF(hideonnpc,"s"),
  20810. BUILDIN_DEF(sc_start,"iii???"),
  20811. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  20812. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  20813. BUILDIN_DEF(sc_end,"i?"),
  20814. BUILDIN_DEF(sc_end_class,"??"),
  20815. BUILDIN_DEF(getstatus, "i??"),
  20816. BUILDIN_DEF(getscrate,"ii?"),
  20817. BUILDIN_DEF(debugmes,"s"),
  20818. BUILDIN_DEF2(catchpet,"pet","i"),
  20819. BUILDIN_DEF2(birthpet,"bpet",""),
  20820. BUILDIN_DEF(catchpet,"i"),
  20821. BUILDIN_DEF(birthpet,""),
  20822. BUILDIN_DEF(resetlvl,"i?"),
  20823. BUILDIN_DEF(resetstatus,"?"),
  20824. BUILDIN_DEF(resetskill,"?"),
  20825. BUILDIN_DEF(skillpointcount,"?"),
  20826. BUILDIN_DEF(changebase,"i?"),
  20827. BUILDIN_DEF(changesex,"?"),
  20828. BUILDIN_DEF(changecharsex,"?"),
  20829. BUILDIN_DEF(waitingroom,"si?????"),
  20830. BUILDIN_DEF(delwaitingroom,"?"),
  20831. BUILDIN_DEF(waitingroomkick,"ss"),
  20832. BUILDIN_DEF(getwaitingroomusers, "?"),
  20833. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  20834. BUILDIN_DEF(enablewaitingroomevent,"?"),
  20835. BUILDIN_DEF(disablewaitingroomevent,"?"),
  20836. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  20837. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  20838. BUILDIN_DEF(getwaitingroomstate,"i?"),
  20839. BUILDIN_DEF(warpwaitingpc,"sii?"),
  20840. BUILDIN_DEF(attachrid,"i?"),
  20841. BUILDIN_DEF(addrid,"i?????"),
  20842. BUILDIN_DEF(detachrid,""),
  20843. BUILDIN_DEF(isloggedin,"i?"),
  20844. BUILDIN_DEF(setmapflagnosave,"ssii"),
  20845. BUILDIN_DEF(getmapflag,"si?"),
  20846. BUILDIN_DEF(setmapflag,"si??"),
  20847. BUILDIN_DEF(removemapflag,"si?"),
  20848. BUILDIN_DEF(pvpon,"s"),
  20849. BUILDIN_DEF(pvpoff,"s"),
  20850. BUILDIN_DEF(gvgon,"s"),
  20851. BUILDIN_DEF(gvgoff,"s"),
  20852. BUILDIN_DEF(emotion,"i?"),
  20853. BUILDIN_DEF(maprespawnguildid,"sii"),
  20854. BUILDIN_DEF(agitstart,""), // <Agit>
  20855. BUILDIN_DEF(agitend,""),
  20856. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  20857. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  20858. BUILDIN_DEF(getcastlename,"s"),
  20859. BUILDIN_DEF(getcastledata,"si"),
  20860. BUILDIN_DEF(setcastledata,"sii"),
  20861. BUILDIN_DEF(requestguildinfo,"i?"),
  20862. BUILDIN_DEF(getequipcardcnt,"i"),
  20863. BUILDIN_DEF(successremovecards,"i"),
  20864. BUILDIN_DEF(failedremovecards,"ii"),
  20865. BUILDIN_DEF(marriage,"s"),
  20866. BUILDIN_DEF2(wedding_effect,"wedding",""),
  20867. BUILDIN_DEF(divorce,"?"),
  20868. BUILDIN_DEF(ispartneron,"?"),
  20869. BUILDIN_DEF(getpartnerid,"?"),
  20870. BUILDIN_DEF(getchildid,"?"),
  20871. BUILDIN_DEF(getmotherid,"?"),
  20872. BUILDIN_DEF(getfatherid,"?"),
  20873. BUILDIN_DEF(warppartner,"sii"),
  20874. BUILDIN_DEF(getitemname,"v"),
  20875. BUILDIN_DEF(getitemslots,"i"),
  20876. BUILDIN_DEF(makepet,"i"),
  20877. BUILDIN_DEF(getexp,"ii?"),
  20878. BUILDIN_DEF(getinventorylist,"?"),
  20879. BUILDIN_DEF(getskilllist,"?"),
  20880. BUILDIN_DEF(clearitem,"?"),
  20881. BUILDIN_DEF(classchange,"i??"),
  20882. BUILDIN_DEF(misceffect,"i"),
  20883. BUILDIN_DEF(playBGM,"s"),
  20884. BUILDIN_DEF(playBGMall,"s?????"),
  20885. BUILDIN_DEF(soundeffect,"si"),
  20886. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  20887. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  20888. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  20889. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  20890. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  20891. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  20892. BUILDIN_DEF(petloot,"i"), // [Valaris]
  20893. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  20894. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  20895. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  20896. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  20897. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  20898. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  20899. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  20900. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  20901. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  20902. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  20903. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  20904. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  20905. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  20906. BUILDIN_DEF(npctalk,"s??"), // [Valaris]
  20907. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  20908. BUILDIN_DEF(mobcount,"ss"),
  20909. BUILDIN_DEF(getlook,"i?"),
  20910. BUILDIN_DEF(getsavepoint,"i?"),
  20911. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  20912. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  20913. BUILDIN_DEF(npcstop,""), // [Valaris]
  20914. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  20915. BUILDIN_DEF(mapid2name,"i"),
  20916. BUILDIN_DEF(checkoption1,"i?"),
  20917. BUILDIN_DEF(checkoption2,"i?"),
  20918. BUILDIN_DEF(guildgetexp,"i"),
  20919. BUILDIN_DEF(guildchangegm,"is"),
  20920. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  20921. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  20922. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  20923. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  20924. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  20925. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  20926. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  20927. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  20928. BUILDIN_DEF(night,""), // sets the server to night time
  20929. BUILDIN_DEF(day,""), // sets the server to day time
  20930. #ifdef PCRE_SUPPORT
  20931. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  20932. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  20933. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  20934. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  20935. #endif
  20936. BUILDIN_DEF(preg_match,"ss?"),
  20937. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  20938. BUILDIN_DEF(recovery,"i???"),
  20939. BUILDIN_DEF(getpetinfo,"i?"),
  20940. BUILDIN_DEF(gethominfo,"i?"),
  20941. BUILDIN_DEF(getmercinfo,"i?"),
  20942. BUILDIN_DEF(checkequipedcard,"i"),
  20943. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  20944. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  20945. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  20946. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  20947. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  20948. BUILDIN_DEF(charat,"si"),
  20949. BUILDIN_DEF(setchar,"ssi"),
  20950. BUILDIN_DEF(insertchar,"ssi"),
  20951. BUILDIN_DEF(delchar,"si"),
  20952. BUILDIN_DEF(strtoupper,"s"),
  20953. BUILDIN_DEF(strtolower,"s"),
  20954. BUILDIN_DEF(charisupper, "si"),
  20955. BUILDIN_DEF(charislower, "si"),
  20956. BUILDIN_DEF(substr,"sii"),
  20957. BUILDIN_DEF(explode, "rss"),
  20958. BUILDIN_DEF(implode, "r?"),
  20959. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  20960. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  20961. BUILDIN_DEF(strpos,"ss?"),
  20962. BUILDIN_DEF(replacestr,"sss??"),
  20963. BUILDIN_DEF(countstr,"ss?"),
  20964. BUILDIN_DEF(setnpcdisplay,"sv??"),
  20965. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  20966. BUILDIN_DEF(strcmp,"ss"),
  20967. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  20968. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  20969. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  20970. // [zBuffer] List of mathematics commands --->
  20971. BUILDIN_DEF(sqrt,"i"),
  20972. BUILDIN_DEF(pow,"ii"),
  20973. BUILDIN_DEF(distance,"iiii"),
  20974. BUILDIN_DEF2(minmax,"min", "*"),
  20975. BUILDIN_DEF2(minmax,"minimum", "*"),
  20976. BUILDIN_DEF2(minmax,"max", "*"),
  20977. BUILDIN_DEF2(minmax,"maximum", "*"),
  20978. // <--- [zBuffer] List of mathematics commands
  20979. BUILDIN_DEF(md5,"s"),
  20980. // [zBuffer] List of dynamic var commands --->
  20981. BUILDIN_DEF(getd,"s"),
  20982. BUILDIN_DEF(setd,"sv?"),
  20983. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  20984. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  20985. BUILDIN_DEF(npcshopadditem,"sii*"),
  20986. BUILDIN_DEF(npcshopdelitem,"si*"),
  20987. BUILDIN_DEF(npcshopattach,"s?"),
  20988. BUILDIN_DEF(equip,"i?"),
  20989. BUILDIN_DEF(autoequip,"ii"),
  20990. BUILDIN_DEF(setbattleflag,"si?"),
  20991. BUILDIN_DEF(getbattleflag,"s"),
  20992. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  20993. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  20994. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  20995. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  20996. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  20997. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  20998. BUILDIN_DEF(axtoi,"s"),
  20999. BUILDIN_DEF(query_sql,"s*"),
  21000. BUILDIN_DEF(query_logsql,"s*"),
  21001. BUILDIN_DEF(escape_sql,"v"),
  21002. BUILDIN_DEF(atoi,"s"),
  21003. BUILDIN_DEF(strtol,"si"),
  21004. // [zBuffer] List of player cont commands --->
  21005. BUILDIN_DEF(rid2name,"i"),
  21006. BUILDIN_DEF(pcfollow,"ii"),
  21007. BUILDIN_DEF(pcstopfollow,"i"),
  21008. BUILDIN_DEF(pcblockmove,"ii"),
  21009. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  21010. BUILDIN_DEF(pcblockskill,"ii"),
  21011. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  21012. // <--- [zBuffer] List of player cont commands
  21013. // [zBuffer] List of unit control commands --->
  21014. BUILDIN_DEF(unitexists,"i"),
  21015. BUILDIN_DEF(getunittype,"i"),
  21016. BUILDIN_DEF(getunitname,"i"),
  21017. BUILDIN_DEF(setunitname,"is"),
  21018. BUILDIN_DEF(getunitdata,"i*"),
  21019. BUILDIN_DEF(setunitdata,"iii"),
  21020. BUILDIN_DEF(unitwalk,"iii?"),
  21021. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  21022. BUILDIN_DEF(unitkill,"i"),
  21023. BUILDIN_DEF(unitwarp,"isii"),
  21024. BUILDIN_DEF(unitattack,"iv?"),
  21025. BUILDIN_DEF(unitstopattack,"i"),
  21026. BUILDIN_DEF(unitstopwalk,"i?"),
  21027. BUILDIN_DEF(unittalk,"is?"),
  21028. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  21029. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  21030. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  21031. // <--- [zBuffer] List of unit control commands
  21032. BUILDIN_DEF(sleep,"i"),
  21033. BUILDIN_DEF(sleep2,"i"),
  21034. BUILDIN_DEF(awake,"s"),
  21035. BUILDIN_DEF(getvariableofnpc,"rs"),
  21036. BUILDIN_DEF(warpportal,"iisii"),
  21037. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  21038. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  21039. BUILDIN_DEF(morphembryo,""),
  21040. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  21041. BUILDIN_DEF(checkhomcall,""),
  21042. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  21043. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  21044. BUILDIN_DEF(checkvending,"?"),
  21045. BUILDIN_DEF(checkchatting,"?"),
  21046. BUILDIN_DEF(checkidle,"?"),
  21047. BUILDIN_DEF(openmail,"?"),
  21048. BUILDIN_DEF(openauction,"?"),
  21049. BUILDIN_DEF(checkcell,"siii"),
  21050. BUILDIN_DEF(setcell,"siiiiii"),
  21051. BUILDIN_DEF(getfreecell,"srr?????"),
  21052. BUILDIN_DEF(setwall,"siiiiis"),
  21053. BUILDIN_DEF(delwall,"s"),
  21054. BUILDIN_DEF(searchitem,"rs"),
  21055. BUILDIN_DEF(mercenary_create,"ii"),
  21056. BUILDIN_DEF(mercenary_heal,"ii"),
  21057. BUILDIN_DEF(mercenary_sc_start,"iii"),
  21058. BUILDIN_DEF(mercenary_get_calls,"i"),
  21059. BUILDIN_DEF(mercenary_get_faith,"i"),
  21060. BUILDIN_DEF(mercenary_set_calls,"ii"),
  21061. BUILDIN_DEF(mercenary_set_faith,"ii"),
  21062. BUILDIN_DEF(readbook,"ii"),
  21063. BUILDIN_DEF(setfont,"i"),
  21064. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  21065. BUILDIN_DEF(progressbar,"si"),
  21066. BUILDIN_DEF(progressbar_npc, "si?"),
  21067. BUILDIN_DEF(pushpc,"ii"),
  21068. BUILDIN_DEF(buyingstore,"i"),
  21069. BUILDIN_DEF(searchstores,"ii"),
  21070. BUILDIN_DEF(showdigit,"i?"),
  21071. // WoE SE
  21072. BUILDIN_DEF(agitstart2,""),
  21073. BUILDIN_DEF(agitend2,""),
  21074. BUILDIN_DEF(agitcheck2,""),
  21075. // BattleGround
  21076. BUILDIN_DEF(waitingroom2bg,"sii???"),
  21077. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  21078. BUILDIN_DEF(bg_team_setxy,"iii"),
  21079. BUILDIN_DEF(bg_warp,"isii"),
  21080. BUILDIN_DEF(bg_monster,"isiisi?"),
  21081. BUILDIN_DEF(bg_monster_set_team,"ii"),
  21082. BUILDIN_DEF(bg_leave,"?"),
  21083. BUILDIN_DEF(bg_destroy,"i"),
  21084. BUILDIN_DEF(areapercentheal,"siiiiii"),
  21085. BUILDIN_DEF(bg_get_data,"ii"),
  21086. BUILDIN_DEF(bg_getareausers,"isiiii"),
  21087. BUILDIN_DEF(bg_updatescore,"sii"),
  21088. BUILDIN_DEF(bg_join,"i????"),
  21089. BUILDIN_DEF(bg_create,"sii??"),
  21090. // Instancing
  21091. BUILDIN_DEF(instance_create,"s??"),
  21092. BUILDIN_DEF(instance_destroy,"?"),
  21093. BUILDIN_DEF(instance_id,""),
  21094. BUILDIN_DEF(instance_enter,"s????"),
  21095. BUILDIN_DEF(instance_npcname,"s?"),
  21096. BUILDIN_DEF(instance_mapname,"s?"),
  21097. BUILDIN_DEF(instance_warpall,"sii?"),
  21098. BUILDIN_DEF(instance_announce,"isi?????"),
  21099. BUILDIN_DEF(instance_check_party,"i???"),
  21100. BUILDIN_DEF(instance_check_guild,"i???"),
  21101. BUILDIN_DEF(instance_check_clan,"i???"),
  21102. BUILDIN_DEF(instance_info,"si?"),
  21103. /**
  21104. * 3rd-related
  21105. **/
  21106. BUILDIN_DEF(makerune,"i?"),
  21107. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  21108. BUILDIN_DEF(setdragon,"??"),//[Ind]
  21109. BUILDIN_DEF(ismounting,"?"),//[Ind]
  21110. BUILDIN_DEF(setmounting,"?"),//[Ind]
  21111. BUILDIN_DEF(checkre,"i"),
  21112. /**
  21113. * rAthena and beyond!
  21114. **/
  21115. BUILDIN_DEF(getargcount,""),
  21116. BUILDIN_DEF(getcharip,"?"),
  21117. BUILDIN_DEF(is_function,"s"),
  21118. BUILDIN_DEF(get_revision,""),
  21119. BUILDIN_DEF(get_githash,""),
  21120. BUILDIN_DEF(freeloop,"?"),
  21121. BUILDIN_DEF(getrandgroupitem,"i????"),
  21122. BUILDIN_DEF(cleanmap,"s"),
  21123. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  21124. BUILDIN_DEF(npcskill,"viii"),
  21125. BUILDIN_DEF(consumeitem,"v?"),
  21126. BUILDIN_DEF(delequip,"i?"),
  21127. BUILDIN_DEF(breakequip,"i?"),
  21128. BUILDIN_DEF(sit,"?"),
  21129. BUILDIN_DEF(stand,"?"),
  21130. //@commands (script based)
  21131. BUILDIN_DEF(bindatcmd, "ss??"),
  21132. BUILDIN_DEF(unbindatcmd, "s"),
  21133. BUILDIN_DEF(useatcmd, "s"),
  21134. //Quest Log System [Inkfish]
  21135. BUILDIN_DEF(questinfo, "ii??"),
  21136. BUILDIN_DEF(setquest, "i?"),
  21137. BUILDIN_DEF(erasequest, "i?"),
  21138. BUILDIN_DEF(completequest, "i?"),
  21139. BUILDIN_DEF(checkquest, "i??"),
  21140. BUILDIN_DEF(isbegin_quest,"i?"),
  21141. BUILDIN_DEF(changequest, "ii?"),
  21142. BUILDIN_DEF(showevent, "i??"),
  21143. //Bound items [Xantara] & [Akinari]
  21144. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  21145. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  21146. BUILDIN_DEF(countbound, "??"),
  21147. // Party related
  21148. BUILDIN_DEF(party_create,"s???"),
  21149. BUILDIN_DEF(party_addmember,"ii"),
  21150. BUILDIN_DEF(party_delmember,"??"),
  21151. BUILDIN_DEF(party_changeleader,"ii"),
  21152. BUILDIN_DEF(party_changeoption,"iii"),
  21153. BUILDIN_DEF(party_destroy,"i"),
  21154. // Clan system
  21155. BUILDIN_DEF(clan_join,"i?"),
  21156. BUILDIN_DEF(clan_leave,"?"),
  21157. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  21158. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  21159. BUILDIN_DEF(vip_status,"i?"),
  21160. BUILDIN_DEF(vip_time,"i?"),
  21161. BUILDIN_DEF(bonus_script,"si????"),
  21162. BUILDIN_DEF(bonus_script_clear,"??"),
  21163. BUILDIN_DEF(getgroupitem,"i??"),
  21164. BUILDIN_DEF(enable_command,""),
  21165. BUILDIN_DEF(disable_command,""),
  21166. BUILDIN_DEF(getguildmember,"i??"),
  21167. BUILDIN_DEF(addspiritball,"ii?"),
  21168. BUILDIN_DEF(delspiritball,"i?"),
  21169. BUILDIN_DEF(countspiritball,"?"),
  21170. BUILDIN_DEF(mergeitem,"?"),
  21171. BUILDIN_DEF(mergeitem2,"??"),
  21172. BUILDIN_DEF(npcshopupdate,"sii?"),
  21173. BUILDIN_DEF(getattachedrid,""),
  21174. BUILDIN_DEF(getvar,"vi"),
  21175. BUILDIN_DEF(showscript,"s??"),
  21176. BUILDIN_DEF(ignoretimeout,"i?"),
  21177. BUILDIN_DEF(geteleminfo,"i?"),
  21178. BUILDIN_DEF(setquestinfo_level,"iii"),
  21179. BUILDIN_DEF(setquestinfo_req,"iii*"),
  21180. BUILDIN_DEF(setquestinfo_job,"ii*"),
  21181. BUILDIN_DEF(opendressroom,"i?"),
  21182. BUILDIN_DEF(navigateto,"s???????"),
  21183. BUILDIN_DEF(getguildalliance,"ii"),
  21184. BUILDIN_DEF(adopt,"vv"),
  21185. BUILDIN_DEF(getexp2,"ii?"),
  21186. BUILDIN_DEF(recalculatestat,""),
  21187. BUILDIN_DEF(hateffect,"ii"),
  21188. BUILDIN_DEF(getrandomoptinfo, "i"),
  21189. BUILDIN_DEF(getequiprandomoption, "iii?"),
  21190. BUILDIN_DEF(setrandomoption,"iiiii?"),
  21191. BUILDIN_DEF(needed_status_point,"ii?"),
  21192. BUILDIN_DEF(jobcanentermap,"s?"),
  21193. BUILDIN_DEF(openstorage2,"ii?"),
  21194. BUILDIN_DEF(unloadnpc, "s"),
  21195. // WoE TE
  21196. BUILDIN_DEF(agitstart3,""),
  21197. BUILDIN_DEF(agitend3,""),
  21198. BUILDIN_DEF(agitcheck3,""),
  21199. BUILDIN_DEF(gvgon3,"s"),
  21200. BUILDIN_DEF(gvgoff3,"s"),
  21201. // Channel System
  21202. BUILDIN_DEF(channel_create,"ss?????"),
  21203. BUILDIN_DEF(channel_setopt,"sii"),
  21204. BUILDIN_DEF(channel_setcolor,"si"),
  21205. BUILDIN_DEF(channel_setpass,"ss"),
  21206. BUILDIN_DEF(channel_setgroup,"si*"),
  21207. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  21208. BUILDIN_DEF(channel_chat,"ss?"),
  21209. BUILDIN_DEF(channel_ban,"si"),
  21210. BUILDIN_DEF(channel_unban,"si"),
  21211. BUILDIN_DEF(channel_kick,"sv"),
  21212. BUILDIN_DEF(channel_delete,"s"),
  21213. // Item Random Option Extension [Cydh]
  21214. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  21215. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  21216. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  21217. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr"),
  21218. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  21219. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  21220. // Achievement System
  21221. BUILDIN_DEF(achievementinfo,"ii?"),
  21222. BUILDIN_DEF(achievementadd,"i?"),
  21223. BUILDIN_DEF(achievementremove,"i?"),
  21224. BUILDIN_DEF(achievementcomplete,"i?"),
  21225. BUILDIN_DEF(achievementexists,"i?"),
  21226. BUILDIN_DEF(achievementupdate,"iii?"),
  21227. BUILDIN_DEF(getequiprefinecost,"iii?"),
  21228. BUILDIN_DEF2(round, "round", "i"),
  21229. BUILDIN_DEF2(round, "ceil", "i"),
  21230. BUILDIN_DEF2(round, "floor", "i"),
  21231. BUILDIN_DEF(getequiptradability, "i?"),
  21232. BUILDIN_DEF(mail, "isss*"),
  21233. BUILDIN_DEF(open_roulette,"?"),
  21234. #include "../custom/script_def.inc"
  21235. {NULL,NULL,NULL},
  21236. };