script.cpp 676 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900
  1. // Copyright (c) Athena 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 <math.h>
  10. #include <stdlib.h> // atoi, strtol, strtoll, exit
  11. #include <setjmp.h>
  12. #include <errno.h>
  13. #ifdef PCRE_SUPPORT
  14. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  15. #endif
  16. #include "../common/cbasetypes.h"
  17. #include "../common/malloc.h"
  18. #include "../common/md5calc.h"
  19. #include "../common/nullpo.h"
  20. #include "../common/random.h"
  21. #include "../common/showmsg.h"
  22. #include "../common/socket.h"
  23. #include "../common/strlib.h"
  24. #include "../common/timer.h"
  25. #include "../common/utils.h"
  26. #include "../common/ers.h" // ers_destroy
  27. #ifdef BETA_THREAD_TEST
  28. #include "../common/atomic.h"
  29. #include "../common/spinlock.h"
  30. #include "../common/thread.h"
  31. #include "../common/mutex.h"
  32. #endif
  33. #include "map.hpp"
  34. #include "path.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "chrif.hpp"
  38. #include "date.hpp" // date type enum, date_get()
  39. #include "itemdb.hpp"
  40. #include "pc.hpp"
  41. #include "pc_groups.hpp"
  42. #include "storage.hpp"
  43. #include "pet.hpp"
  44. #include "mapreg.hpp"
  45. #include "homunculus.hpp"
  46. #include "instance.hpp"
  47. #include "mercenary.hpp"
  48. #include "intif.hpp"
  49. #include "chat.hpp"
  50. #include "battleground.hpp"
  51. #include "party.hpp"
  52. #include "mail.hpp"
  53. #include "quest.hpp"
  54. #include "elemental.hpp"
  55. #include "npc.hpp"
  56. #include "guild.hpp"
  57. #include "atcommand.hpp"
  58. #include "battle.hpp"
  59. #include "log.hpp"
  60. #include "mob.hpp"
  61. #include "channel.hpp"
  62. #include "achievement.hpp"
  63. struct eri *array_ers;
  64. DBMap *st_db;
  65. unsigned int active_scripts;
  66. unsigned int next_id;
  67. struct eri *st_ers;
  68. struct eri *stack_ers;
  69. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func );
  70. /**
  71. * Get `sd` from a account id in `loc` param instead of attached rid
  72. * @param st Script
  73. * @param loc Location to look account id in script parameter
  74. * @param sd Variable that will be assigned
  75. * @return True if `sd` is assigned, false otherwise
  76. **/
  77. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  78. if (script_hasdata(st, loc)) {
  79. int id_ = script_getnum(st, loc);
  80. if (!(*sd = map_id2sd(id_))){
  81. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  82. return false;
  83. }else{
  84. return true;
  85. }
  86. }
  87. else
  88. return script_rid2sd_(st,sd,func);
  89. }
  90. /**
  91. * Get `sd` from a char id in `loc` param instead of attached rid
  92. * @param st Script
  93. * @param loc Location to look char id in script parameter
  94. * @param sd Variable that will be assigned
  95. * @return True if `sd` is assigned, false otherwise
  96. **/
  97. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  98. if (script_hasdata(st, loc)) {
  99. int id_ = script_getnum(st, loc);
  100. if (!(*sd = map_charid2sd(id_))){
  101. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  102. return false;
  103. }else{
  104. return true;
  105. }
  106. }
  107. else
  108. return script_rid2sd_(st,sd,func);
  109. }
  110. /**
  111. * Get `sd` from a nick in `loc` param instead of attached rid
  112. * @param st Script
  113. * @param loc Location to look nick in script parameter
  114. * @param sd Variable that will be assigned
  115. * @return True if `sd` is assigned, false otherwise
  116. **/
  117. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  118. if (script_hasdata(st, loc)) {
  119. const char *name_ = script_getstr(st, loc);
  120. if (!(*sd = map_nick2sd(name_,false))){
  121. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  122. return false;
  123. }else{
  124. return true;
  125. }
  126. }
  127. else
  128. return script_rid2sd_(st,sd,func);
  129. }
  130. /**
  131. * Get `sd` from a mapid in `loc` param instead of attached rid
  132. * @param st Script
  133. * @param loc Location to look mapid in script parameter
  134. * @param sd Variable that will be assigned
  135. * @return True if `sd` is assigned, false otherwise
  136. **/
  137. static bool script_mapid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  138. if (script_hasdata(st, loc)) {
  139. int id_ = script_getnum(st, loc);
  140. if (!(*sd = map_id2sd(id_))){
  141. ShowError("%s: Player with map id '%d' is not found.\n", func, id_);
  142. return false;
  143. }else{
  144. return true;
  145. }
  146. }
  147. else
  148. return script_rid2sd_(st,sd,func);
  149. }
  150. /**
  151. * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator.
  152. * @param st Script
  153. * @param loc Location to look for ID in script parameter
  154. * @param bl Variable that will be assigned
  155. * @return True if `bl` is assigned, false otherwise
  156. **/
  157. static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) {
  158. int unit_id;
  159. if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0)
  160. unit_id = st->rid;
  161. *bl = map_id2bl(unit_id);
  162. if ( *bl )
  163. return true;
  164. else {
  165. ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id);
  166. return false;
  167. }
  168. }
  169. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  170. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  171. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  172. #define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__)
  173. #define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__)
  174. #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__)
  175. /// temporary buffer for passing around compiled bytecode
  176. /// @see add_scriptb, set_label, parse_script
  177. static unsigned char* script_buf = NULL;
  178. static int script_pos = 0, script_size = 0;
  179. static inline int GETVALUE(const unsigned char* buf, int i)
  180. {
  181. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  182. }
  183. static inline void SETVALUE(unsigned char* buf, int i, int n)
  184. {
  185. buf[i] = GetByte(n, 0);
  186. buf[i+1] = GetByte(n, 1);
  187. buf[i+2] = GetByte(n, 2);
  188. }
  189. // String buffer structures.
  190. // str_data stores string information
  191. static struct str_data_struct {
  192. enum c_op type;
  193. int str;
  194. int backpatch;
  195. int label;
  196. int (*func)(struct script_state *st);
  197. int val;
  198. int next;
  199. bool deprecated;
  200. } *str_data = NULL;
  201. static int str_data_size = 0; // size of the data
  202. static int str_num = LABEL_START; // next id to be assigned
  203. // str_buf holds the strings themselves
  204. static char *str_buf;
  205. static int str_size = 0; // size of the buffer
  206. static int str_pos = 0; // next position to be assigned
  207. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  208. #define SCRIPT_HASH_SIZE 1021
  209. int str_hash[SCRIPT_HASH_SIZE];
  210. // Specifies which string hashing method to use
  211. //#define SCRIPT_HASH_DJB2
  212. //#define SCRIPT_HASH_SDBM
  213. #define SCRIPT_HASH_ELF
  214. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  215. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  216. static int parse_options = 0;
  217. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  218. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  219. // important buildin function references for usage in scripts
  220. static int buildin_set_ref = 0;
  221. static int buildin_callsub_ref = 0;
  222. static int buildin_callfunc_ref = 0;
  223. static int buildin_getelementofarray_ref = 0;
  224. // Caches compiled autoscript item code.
  225. // Note: This is not cleared when reloading itemdb.
  226. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  227. struct Script_Config script_config = {
  228. 1, // warn_func_mismatch_argtypes
  229. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  230. 0, INT_MAX, // input_min_value/input_max_value
  231. // NOTE: None of these event labels should be longer than <EVENT_NAME_LENGTH> characters
  232. // PC related
  233. "OnPCDieEvent", //die_event_name
  234. "OnPCKillEvent", //kill_pc_event_name
  235. "OnNPCKillEvent", //kill_mob_event_name
  236. "OnPCLoginEvent", //login_event_name
  237. "OnPCLogoutEvent", //logout_event_name
  238. "OnPCLoadMapEvent", //loadmap_event_name
  239. "OnPCBaseLvUpEvent", //baselvup_event_name
  240. "OnPCJobLvUpEvent", //joblvup_event_name
  241. "OnPCStatCalcEvent", //stat_calc_event_name
  242. // NPC related
  243. "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves)
  244. "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area)
  245. "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area)
  246. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  247. "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc)
  248. "OnBuyItem", //onbuy_event_name (is executed when items are bought)
  249. "OnSellItem", //onsell_event_name (is executed when items are sold)
  250. // Init related
  251. "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded)
  252. "OnInterIfInit", //inter_init_event_name (is executed on inter server connection)
  253. "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection)
  254. // Guild related
  255. "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken)
  256. "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started)
  257. "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded)
  258. "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended)
  259. "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started)
  260. "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded)
  261. "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended)
  262. "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started)
  263. "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded)
  264. "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended)
  265. // Timer related
  266. "OnTimer", //timer_event_name (is executed by a timer at the specific second)
  267. "OnTimerQuit", //timer_quit_event_name (is executed when a timer is aborted)
  268. "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute)
  269. "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour)
  270. "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute)
  271. "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day)
  272. "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute)
  273. "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute)
  274. "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute)
  275. "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute)
  276. "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute)
  277. "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute)
  278. "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute)
  279. // Instance related
  280. "OnInstanceInit", //instance_init_event_name (is executed right after instance creation)
  281. "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction)
  282. };
  283. static jmp_buf error_jump;
  284. static char* error_msg;
  285. static const char* error_pos;
  286. static int error_report; // if the error should produce output
  287. // Used by disp_warning_message
  288. static const char* parser_current_src;
  289. static const char* parser_current_file;
  290. static int parser_current_line;
  291. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  292. // [Eoe / jA 1080, 1081, 1094, 1164]
  293. enum curly_type {
  294. TYPE_NULL = 0,
  295. TYPE_IF,
  296. TYPE_SWITCH,
  297. TYPE_WHILE,
  298. TYPE_FOR,
  299. TYPE_DO,
  300. TYPE_USERFUNC,
  301. TYPE_ARGLIST // function argument list
  302. };
  303. enum e_arglist
  304. {
  305. ARGLIST_UNDEFINED = 0,
  306. ARGLIST_NO_PAREN = 1,
  307. ARGLIST_PAREN = 2,
  308. };
  309. static struct {
  310. struct {
  311. enum curly_type type;
  312. int index;
  313. int count;
  314. int flag;
  315. struct linkdb_node *case_label;
  316. } curly[256]; // Information right parenthesis
  317. int curly_count; // The number of right brackets
  318. int index; // Number of the syntax used in the script
  319. } syntax;
  320. const char* parse_curly_close(const char* p);
  321. const char* parse_syntax_close(const char* p);
  322. const char* parse_syntax_close_sub(const char* p,int* flag);
  323. const char* parse_syntax(const char* p);
  324. static int parse_syntax_for_flag = 0;
  325. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  326. extern unsigned int current_equip_combo_pos;
  327. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  328. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  329. int potion_target = 0;
  330. unsigned int *generic_ui_array = NULL;
  331. unsigned int generic_ui_array_size = 0;
  332. c_op get_com(unsigned char *script,int *pos);
  333. int get_num(unsigned char *script,int *pos);
  334. typedef struct script_function {
  335. int (*func)(struct script_state *st);
  336. const char *name;
  337. const char *arg;
  338. const char *deprecated;
  339. } script_function;
  340. extern script_function buildin_func[];
  341. static struct linkdb_node *sleep_db; // int oid -> struct script_state *
  342. #ifdef BETA_THREAD_TEST
  343. /**
  344. * MySQL Query Slave
  345. **/
  346. static SPIN_LOCK queryThreadLock;
  347. static rAthread queryThread = NULL;
  348. static ramutex queryThreadMutex = NULL;
  349. static racond queryThreadCond = NULL;
  350. static volatile int32 queryThreadTerminate = 0;
  351. struct queryThreadEntry {
  352. bool ok;
  353. bool type; /* main db or log db? */
  354. struct script_state *st;
  355. };
  356. /* Ladies and Gentleman the Manager! */
  357. struct {
  358. struct queryThreadEntry **entry;/* array of structs */
  359. int count;
  360. int timer;/* used to receive processed entries */
  361. } queryThreadData;
  362. #endif
  363. /*==========================================
  364. * (Only those needed) local declaration prototype
  365. *------------------------------------------*/
  366. const char* parse_subexpr(const char* p,int limit);
  367. int run_func(struct script_state *st);
  368. unsigned short script_instancegetid(struct script_state *st);
  369. enum {
  370. MF_NOMEMO, //0
  371. MF_NOTELEPORT,
  372. MF_NOSAVE,
  373. MF_NOBRANCH,
  374. MF_NOPENALTY,
  375. MF_NOZENYPENALTY,
  376. MF_PVP,
  377. MF_PVP_NOPARTY,
  378. MF_PVP_NOGUILD,
  379. MF_GVG,
  380. MF_GVG_NOPARTY, //10
  381. MF_NOTRADE,
  382. MF_NOSKILL,
  383. MF_NOWARP,
  384. MF_PARTYLOCK,
  385. MF_NOICEWALL,
  386. MF_SNOW,
  387. MF_FOG,
  388. MF_SAKURA,
  389. MF_LEAVES,
  390. /**
  391. * No longer available, keeping here just in case it's back someday. [Ind]
  392. **/
  393. //MF_RAIN, //20
  394. // 21 free
  395. MF_NOGO = 22,
  396. MF_CLOUDS,
  397. MF_CLOUDS2,
  398. MF_FIREWORKS,
  399. MF_GVG_CASTLE,
  400. MF_GVG_DUNGEON,
  401. MF_NIGHTENABLED,
  402. MF_NOBASEEXP,
  403. MF_NOJOBEXP, //30
  404. MF_NOMOBLOOT,
  405. MF_NOMVPLOOT,
  406. MF_NORETURN,
  407. MF_NOWARPTO,
  408. MF_NIGHTMAREDROP,
  409. MF_RESTRICTED,
  410. MF_NOCOMMAND,
  411. MF_NODROP,
  412. MF_JEXP,
  413. MF_BEXP, //40
  414. MF_NOVENDING,
  415. MF_LOADEVENT,
  416. MF_NOCHAT,
  417. MF_NOEXPPENALTY,
  418. MF_GUILDLOCK,
  419. MF_TOWN,
  420. MF_AUTOTRADE,
  421. MF_ALLOWKS,
  422. MF_MONSTER_NOTELEPORT,
  423. MF_PVP_NOCALCRANK, //50
  424. MF_BATTLEGROUND,
  425. MF_RESET,
  426. MF_CHANNELAUTOJOIN,
  427. MF_NOUSECART,
  428. MF_NOITEMCONSUMPTION,
  429. MF_SUMSTARTMIRACLE,
  430. MF_NOMINEEFFECT,
  431. MF_NOLOCKON,
  432. MF_NOTOMB,
  433. MF_SKILL_DAMAGE, //60
  434. MF_NOCOSTUME,
  435. MF_GVG_TE_CASTLE,
  436. MF_GVG_TE,
  437. MF_HIDEMOBHPBAR,
  438. };
  439. const char* script_op2name(int op)
  440. {
  441. #define RETURN_OP_NAME(type) case type: return #type
  442. switch( op )
  443. {
  444. RETURN_OP_NAME(C_NOP);
  445. RETURN_OP_NAME(C_POS);
  446. RETURN_OP_NAME(C_INT);
  447. RETURN_OP_NAME(C_PARAM);
  448. RETURN_OP_NAME(C_FUNC);
  449. RETURN_OP_NAME(C_STR);
  450. RETURN_OP_NAME(C_CONSTSTR);
  451. RETURN_OP_NAME(C_ARG);
  452. RETURN_OP_NAME(C_NAME);
  453. RETURN_OP_NAME(C_EOL);
  454. RETURN_OP_NAME(C_RETINFO);
  455. RETURN_OP_NAME(C_USERFUNC);
  456. RETURN_OP_NAME(C_USERFUNC_POS);
  457. RETURN_OP_NAME(C_REF);
  458. // operators
  459. RETURN_OP_NAME(C_OP3);
  460. RETURN_OP_NAME(C_LOR);
  461. RETURN_OP_NAME(C_LAND);
  462. RETURN_OP_NAME(C_LE);
  463. RETURN_OP_NAME(C_LT);
  464. RETURN_OP_NAME(C_GE);
  465. RETURN_OP_NAME(C_GT);
  466. RETURN_OP_NAME(C_EQ);
  467. RETURN_OP_NAME(C_NE);
  468. RETURN_OP_NAME(C_XOR);
  469. RETURN_OP_NAME(C_OR);
  470. RETURN_OP_NAME(C_AND);
  471. RETURN_OP_NAME(C_ADD);
  472. RETURN_OP_NAME(C_SUB);
  473. RETURN_OP_NAME(C_MUL);
  474. RETURN_OP_NAME(C_DIV);
  475. RETURN_OP_NAME(C_MOD);
  476. RETURN_OP_NAME(C_NEG);
  477. RETURN_OP_NAME(C_LNOT);
  478. RETURN_OP_NAME(C_NOT);
  479. RETURN_OP_NAME(C_R_SHIFT);
  480. RETURN_OP_NAME(C_L_SHIFT);
  481. RETURN_OP_NAME(C_ADD_POST);
  482. RETURN_OP_NAME(C_SUB_POST);
  483. RETURN_OP_NAME(C_ADD_PRE);
  484. RETURN_OP_NAME(C_SUB_PRE);
  485. default:
  486. ShowDebug("script_op2name: unexpected op=%d\n", op);
  487. return "???";
  488. }
  489. #undef RETURN_OP_NAME
  490. }
  491. #ifdef DEBUG_DUMP_STACK
  492. static void script_dump_stack(struct script_state* st)
  493. {
  494. int i;
  495. ShowMessage("\tstart = %d\n", st->start);
  496. ShowMessage("\tend = %d\n", st->end);
  497. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  498. ShowMessage("\tsp = %d\n", st->stack->sp);
  499. for( i = 0; i < st->stack->sp; ++i )
  500. {
  501. struct script_data* data = &st->stack->stack_data[i];
  502. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  503. switch( data->type )
  504. {
  505. case C_INT:
  506. case C_POS:
  507. ShowMessage(" %d\n", data->u.num);
  508. break;
  509. case C_STR:
  510. case C_CONSTSTR:
  511. ShowMessage(" \"%s\"\n", data->u.str);
  512. break;
  513. case C_NAME:
  514. 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));
  515. break;
  516. case C_RETINFO:
  517. {
  518. struct script_retinfo* ri = data->u.ri;
  519. 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);
  520. }
  521. break;
  522. default:
  523. ShowMessage("\n");
  524. break;
  525. }
  526. }
  527. }
  528. #endif
  529. /// Reports on the console the src of a script error.
  530. static void script_reportsrc(struct script_state *st)
  531. {
  532. struct block_list* bl;
  533. if( st->oid == 0 )
  534. return; //Can't report source.
  535. bl = map_id2bl(st->oid);
  536. if( bl == NULL )
  537. return;
  538. switch( bl->type ) {
  539. case BL_NPC:
  540. if( bl->m >= 0 )
  541. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  542. else
  543. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  544. break;
  545. default:
  546. if( bl->m >= 0 )
  547. 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);
  548. else
  549. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  550. break;
  551. }
  552. }
  553. /// Reports on the console information about the script data.
  554. static void script_reportdata(struct script_data* data)
  555. {
  556. if( data == NULL )
  557. return;
  558. switch( data->type ) {
  559. case C_NOP:// no value
  560. ShowDebug("Data: nothing (nil)\n");
  561. break;
  562. case C_INT:// number
  563. ShowDebug("Data: number value=%" PRId64 "\n", data->u.num);
  564. break;
  565. case C_STR:
  566. case C_CONSTSTR:// string
  567. if( data->u.str ) {
  568. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  569. } else {
  570. ShowDebug("Data: string value=NULL\n");
  571. }
  572. break;
  573. case C_NAME:// reference
  574. if( reference_tovariable(data) ) {// variable
  575. const char* name = reference_getname(data);
  576. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  577. } else if( reference_toconstant(data) ) {// constant
  578. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  579. } else if( reference_toparam(data) ) {// param
  580. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  581. } else {// ???
  582. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  583. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  584. }
  585. break;
  586. case C_POS:// label
  587. ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num);
  588. break;
  589. default:
  590. ShowDebug("Data: %s\n", script_op2name(data->type));
  591. break;
  592. }
  593. }
  594. /// Reports on the console information about the current built-in function.
  595. static void script_reportfunc(struct script_state* st)
  596. {
  597. int params, id;
  598. struct script_data* data;
  599. if( !script_hasdata(st,0) )
  600. {// no stack
  601. return;
  602. }
  603. data = script_getdata(st,0);
  604. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  605. {// script currently not executing a built-in function or corrupt stack
  606. return;
  607. }
  608. id = reference_getid(data);
  609. params = script_lastdata(st)-1;
  610. if( params > 0 )
  611. {
  612. int i;
  613. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  614. for( i = 2; i <= script_lastdata(st); i++ )
  615. {
  616. script_reportdata(script_getdata(st,i));
  617. }
  618. }
  619. else
  620. {
  621. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  622. }
  623. }
  624. /*==========================================
  625. * Output error message
  626. *------------------------------------------*/
  627. static void disp_error_message2(const char *mes,const char *pos,int report)
  628. {
  629. error_msg = aStrdup(mes);
  630. error_pos = pos;
  631. error_report = report;
  632. longjmp( error_jump, 1 );
  633. }
  634. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  635. static void disp_warning_message(const char *mes, const char *pos) {
  636. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  637. }
  638. /// Checks event parameter validity
  639. static void check_event(struct script_state *st, const char *evt)
  640. {
  641. if( evt && evt[0] && !stristr(evt, "::On") )
  642. {
  643. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  644. script_reportsrc(st);
  645. }
  646. }
  647. /*==========================================
  648. * Hashes the input string
  649. *------------------------------------------*/
  650. static unsigned int calc_hash(const char* p)
  651. {
  652. unsigned int h;
  653. #if defined(SCRIPT_HASH_DJB2)
  654. h = 5381;
  655. while( *p ) // hash*33 + c
  656. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  657. #elif defined(SCRIPT_HASH_SDBM)
  658. h = 0;
  659. while( *p ) // hash*65599 + c
  660. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  661. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  662. h = 0;
  663. while( *p ){
  664. unsigned int g;
  665. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  666. g = h & 0xF0000000;
  667. if( g )
  668. {
  669. h ^= g >> 24;
  670. h &= ~g;
  671. }
  672. }
  673. #else // athena hash
  674. h = 0;
  675. while( *p )
  676. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  677. #endif
  678. return h % SCRIPT_HASH_SIZE;
  679. }
  680. bool script_check_RegistryVariableLength(int pType, const char *val, size_t* vlen)
  681. {
  682. size_t len = strlen(val);
  683. if (vlen)
  684. *vlen = len;
  685. switch (pType) {
  686. case 0:
  687. return (len < 33); // key check
  688. case 1:
  689. return (len < 255); // value check
  690. default:
  691. return false;
  692. }
  693. }
  694. /*==========================================
  695. * str_data manipulation functions
  696. *------------------------------------------*/
  697. /// Looks up string using the provided id.
  698. const char* get_str(int id)
  699. {
  700. Assert( id >= LABEL_START && id < str_size );
  701. return str_buf+str_data[id].str;
  702. }
  703. /// Returns the uid of the string, or -1.
  704. static int search_str(const char* p)
  705. {
  706. int i;
  707. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  708. if( strcasecmp(get_str(i),p) == 0 )
  709. return i;
  710. return -1;
  711. }
  712. /// Stores a copy of the string and returns its id.
  713. /// If an identical string is already present, returns its id instead.
  714. int add_str(const char* p)
  715. {
  716. int h;
  717. int len;
  718. h = calc_hash(p);
  719. if( str_hash[h] == 0 )
  720. {// empty bucket, add new node here
  721. str_hash[h] = str_num;
  722. }
  723. else
  724. {// scan for end of list, or occurence of identical string
  725. int i;
  726. for( i = str_hash[h]; ; i = str_data[i].next )
  727. {
  728. if( strcasecmp(get_str(i),p) == 0 )
  729. return i; // string already in list
  730. if( str_data[i].next == 0 )
  731. break; // reached the end
  732. }
  733. // append node to end of list
  734. str_data[i].next = str_num;
  735. }
  736. // grow list if neccessary
  737. if( str_num >= str_data_size )
  738. {
  739. str_data_size += 128;
  740. RECREATE(str_data,struct str_data_struct,str_data_size);
  741. memset(str_data + (str_data_size - 128), '\0', 128);
  742. }
  743. len=(int)strlen(p);
  744. // grow string buffer if neccessary
  745. while( str_pos+len+1 >= str_size )
  746. {
  747. str_size += 256;
  748. RECREATE(str_buf,char,str_size);
  749. memset(str_buf + (str_size - 256), '\0', 256);
  750. }
  751. safestrncpy(str_buf+str_pos, p, len+1);
  752. str_data[str_num].type = C_NOP;
  753. str_data[str_num].str = str_pos;
  754. str_data[str_num].next = 0;
  755. str_data[str_num].func = NULL;
  756. str_data[str_num].backpatch = -1;
  757. str_data[str_num].label = -1;
  758. str_pos += len+1;
  759. return str_num++;
  760. }
  761. /// Appends 1 byte to the script buffer.
  762. static void add_scriptb(int a)
  763. {
  764. if( script_pos+1 >= script_size )
  765. {
  766. script_size += SCRIPT_BLOCK_SIZE;
  767. RECREATE(script_buf,unsigned char,script_size);
  768. }
  769. script_buf[script_pos++] = (uint8)(a);
  770. }
  771. /// Appends a c_op value to the script buffer.
  772. /// The value is variable-length encoded into 8-bit blocks.
  773. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  774. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  775. static void add_scriptc(int a)
  776. {
  777. while( a >= 0x40 )
  778. {
  779. add_scriptb((a&0x3f)|0x40);
  780. a = (a - 0x40) >> 6;
  781. }
  782. add_scriptb(a);
  783. }
  784. /// Appends an integer value to the script buffer.
  785. /// The value is variable-length encoded into 8-bit blocks.
  786. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  787. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  788. static void add_scripti(int a)
  789. {
  790. while( a >= 0x40 )
  791. {
  792. add_scriptb((a&0x3f)|0xc0);
  793. a = (a - 0x40) >> 6;
  794. }
  795. add_scriptb(a|0x80);
  796. }
  797. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  798. ///
  799. /// @param l The id of the str_data entry
  800. // Maximum up to 16M
  801. static void add_scriptl(int l)
  802. {
  803. int backpatch = str_data[l].backpatch;
  804. switch(str_data[l].type){
  805. case C_POS:
  806. case C_USERFUNC_POS:
  807. add_scriptc(C_POS);
  808. add_scriptb(str_data[l].label);
  809. add_scriptb(str_data[l].label>>8);
  810. add_scriptb(str_data[l].label>>16);
  811. break;
  812. case C_NOP:
  813. case C_USERFUNC:
  814. // Embedded data backpatch there is a possibility of label
  815. add_scriptc(C_NAME);
  816. str_data[l].backpatch = script_pos;
  817. add_scriptb(backpatch);
  818. add_scriptb(backpatch>>8);
  819. add_scriptb(backpatch>>16);
  820. break;
  821. case C_INT:
  822. add_scripti(abs(str_data[l].val));
  823. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  824. add_scriptc(C_NEG);
  825. break;
  826. default: // assume C_NAME
  827. add_scriptc(C_NAME);
  828. add_scriptb(l);
  829. add_scriptb(l>>8);
  830. add_scriptb(l>>16);
  831. break;
  832. }
  833. }
  834. /*==========================================
  835. * Resolve the label
  836. *------------------------------------------*/
  837. void set_label(int l,int pos, const char* script_pos_cur)
  838. {
  839. int i;
  840. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  841. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  842. disp_error_message("set_label: invalid label name",script_pos_cur);
  843. return;
  844. }
  845. if(str_data[l].label!=-1){
  846. disp_error_message("set_label: dup label ",script_pos_cur);
  847. return;
  848. }
  849. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  850. str_data[l].label=pos;
  851. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  852. int next=GETVALUE(script_buf,i);
  853. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  854. SETVALUE(script_buf,i,pos);
  855. i=next;
  856. }
  857. }
  858. /// Skips spaces and/or comments.
  859. const char* skip_space(const char* p)
  860. {
  861. if( p == NULL )
  862. return NULL;
  863. for(;;)
  864. {
  865. while( ISSPACE(*p) )
  866. ++p;
  867. if( *p == '/' && p[1] == '/' )
  868. {// line comment
  869. while(*p && *p!='\n')
  870. ++p;
  871. }
  872. else if( *p == '/' && p[1] == '*' )
  873. {// block comment
  874. p += 2;
  875. for(;;)
  876. {
  877. if( *p == '\0' ) {
  878. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p);
  879. return p;
  880. }
  881. if( *p == '*' && p[1] == '/' )
  882. {// end of block comment
  883. p += 2;
  884. break;
  885. }
  886. ++p;
  887. }
  888. }
  889. else
  890. break;
  891. }
  892. return p;
  893. }
  894. /// Skips a word.
  895. /// A word consists of undercores and/or alphanumeric characters,
  896. /// and valid variable prefixes/postfixes.
  897. static const char* skip_word(const char* p)
  898. {
  899. // prefix
  900. switch( *p ) {
  901. case '@':// temporary char variable
  902. ++p; break;
  903. case '#':// account variable
  904. p += ( p[1] == '#' ? 2 : 1 ); break;
  905. case '\'':// instance variable
  906. ++p; break;
  907. case '.':// npc variable
  908. p += ( p[1] == '@' ? 2 : 1 ); break;
  909. case '$':// global variable
  910. p += ( p[1] == '@' ? 2 : 1 ); break;
  911. }
  912. while( ISALNUM(*p) || *p == '_' )
  913. ++p;
  914. // postfix
  915. if( *p == '$' )// string
  916. p++;
  917. return p;
  918. }
  919. /// Adds a word to str_data.
  920. /// @see skip_word
  921. /// @see add_str
  922. static int add_word(const char* p)
  923. {
  924. char* word;
  925. int len;
  926. int i;
  927. // Check for a word
  928. len = skip_word(p) - p;
  929. if( len == 0 )
  930. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  931. // Duplicate the word
  932. word = (char*)aMalloc(len+1);
  933. memcpy(word, p, len);
  934. word[len] = 0;
  935. // add the word
  936. i = add_str(word);
  937. aFree(word);
  938. return i;
  939. }
  940. /// Parses a function call.
  941. /// The argument list can have parenthesis or not.
  942. /// The number of arguments is checked.
  943. static
  944. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  945. {
  946. const char* p2;
  947. const char* arg=NULL;
  948. int func;
  949. func = add_word(p);
  950. if( str_data[func].type == C_FUNC ){
  951. // buildin function
  952. add_scriptl(func);
  953. add_scriptc(C_ARG);
  954. arg = buildin_func[str_data[func].val].arg;
  955. #if defined(SCRIPT_COMMAND_DEPRECATION)
  956. if( str_data[func].deprecated ){
  957. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  958. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  959. }
  960. #endif
  961. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  962. // script defined function
  963. add_scriptl(buildin_callsub_ref);
  964. add_scriptc(C_ARG);
  965. add_scriptl(func);
  966. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  967. if( *arg == 0 )
  968. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  969. if( *arg != '*' )
  970. ++arg; // count func as argument
  971. } else {
  972. const char* name = get_str(func);
  973. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  974. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  975. } else {;
  976. add_scriptl(buildin_callfunc_ref);
  977. add_scriptc(C_ARG);
  978. add_scriptc(C_STR);
  979. while( *name ) add_scriptb(*name ++);
  980. add_scriptb(0);
  981. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  982. if( *arg != '*' ) ++ arg;
  983. }
  984. }
  985. p = skip_word(p);
  986. p = skip_space(p);
  987. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  988. syntax.curly[syntax.curly_count].count = 0;
  989. if( *p == ';' )
  990. {// <func name> ';'
  991. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  992. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  993. {// <func name> '(' ')'
  994. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  995. p = p2;
  996. /*
  997. } else if( 0 && require_paren && *p != '(' )
  998. {// <func name>
  999. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  1000. */
  1001. } else
  1002. {// <func name> <arg list>
  1003. if( require_paren ){
  1004. if( *p != '(' )
  1005. disp_error_message("need '('",p);
  1006. ++p; // skip '('
  1007. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1008. } else if( *p == '(' ){
  1009. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  1010. } else {
  1011. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  1012. }
  1013. ++syntax.curly_count;
  1014. while( *arg ) {
  1015. p2=parse_subexpr(p,-1);
  1016. if( p == p2 )
  1017. break; // not an argument
  1018. if( *arg != '*' )
  1019. ++arg; // next argument
  1020. p=skip_space(p2);
  1021. if( *arg == 0 || *p != ',' )
  1022. break; // no more arguments
  1023. ++p; // skip comma
  1024. }
  1025. --syntax.curly_count;
  1026. }
  1027. if( *arg && *arg != '?' && *arg != '*' )
  1028. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  1029. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  1030. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  1031. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  1032. if( *p != ')' )
  1033. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  1034. ++p;
  1035. }
  1036. add_scriptc(C_FUNC);
  1037. return p;
  1038. }
  1039. /// Processes end of logical script line.
  1040. /// @param first When true, only fix up scheduling data is initialized
  1041. /// @param p Script position for error reporting in set_label
  1042. static void parse_nextline(bool first, const char* p)
  1043. {
  1044. if( !first )
  1045. {
  1046. add_scriptc(C_EOL); // mark end of line for stack cleanup
  1047. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  1048. }
  1049. // initialize data for new '-' label fix up scheduling
  1050. str_data[LABEL_NEXTLINE].type = C_NOP;
  1051. str_data[LABEL_NEXTLINE].backpatch = -1;
  1052. str_data[LABEL_NEXTLINE].label = -1;
  1053. }
  1054. /**
  1055. * Pushes a variable into stack, processing its array index if needed.
  1056. * @see parse_variable
  1057. */
  1058. void parse_variable_sub_push(int word, const char *p2)
  1059. {
  1060. if( p2 ) { // Process the variable index
  1061. const char *p3 = NULL;
  1062. // Push the getelementofarray method into the stack
  1063. add_scriptl(buildin_getelementofarray_ref);
  1064. add_scriptc(C_ARG);
  1065. add_scriptl(word);
  1066. // Process the sub-expression for this assignment
  1067. p3 = parse_subexpr(p2 + 1, 1);
  1068. p3 = skip_space(p3);
  1069. if( *p3 != ']' ) // Closing parenthesis is required for this script
  1070. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1071. // Push the closing function stack operator onto the stack
  1072. add_scriptc(C_FUNC);
  1073. p3++;
  1074. } else // No array index, simply push the variable or value onto the stack
  1075. add_scriptl(word);
  1076. }
  1077. /// Parse a variable assignment using the direct equals operator
  1078. /// @param p script position where the function should run from
  1079. /// @return NULL if not a variable assignment, the new position otherwise
  1080. const char* parse_variable(const char* p) {
  1081. int word;
  1082. c_op type = C_NOP;
  1083. const char *p2 = NULL;
  1084. const char *var = p;
  1085. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  1086. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  1087. var = p = skip_space(&p[2]);
  1088. // skip the variable where applicable
  1089. p = skip_word(p);
  1090. p = skip_space(p);
  1091. if( p == NULL ) {// end of the line or invalid buffer
  1092. return NULL;
  1093. }
  1094. if( *p == '[' ) {// array variable so process the array as appropriate
  1095. int i,j;
  1096. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1097. if( *p ++ == ']' && --(j) == 0 ) break;
  1098. if( *p == '[' ) ++ j;
  1099. }
  1100. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1101. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1102. }
  1103. }
  1104. if( type == C_NOP &&
  1105. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1106. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1107. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1108. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1109. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1110. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1111. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1112. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1113. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1114. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1115. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  1116. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  1117. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1118. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1119. ) )
  1120. {// failed to find a matching operator combination so invalid
  1121. return NULL;
  1122. }
  1123. switch( type ) {
  1124. case C_ADD_PRE: // pre ++
  1125. case C_SUB_PRE: // pre --
  1126. // Nothing more to skip
  1127. break;
  1128. case C_EQ: {// incremental modifier
  1129. p = skip_space( &p[1] );
  1130. }
  1131. break;
  1132. case C_L_SHIFT:
  1133. case C_R_SHIFT: {// left or right shift modifier
  1134. p = skip_space( &p[3] );
  1135. }
  1136. break;
  1137. default: {// normal incremental command
  1138. p = skip_space( &p[2] );
  1139. }
  1140. }
  1141. if( p == NULL ) {// end of line or invalid buffer
  1142. return NULL;
  1143. }
  1144. // push the set function onto the stack
  1145. add_scriptl(buildin_set_ref);
  1146. add_scriptc(C_ARG);
  1147. // always append parenthesis to avoid errors
  1148. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1149. syntax.curly[syntax.curly_count].count = 0;
  1150. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1151. // increment the total curly count for the position in the script
  1152. ++ syntax.curly_count;
  1153. // parse the variable currently being modified
  1154. word = add_word(var);
  1155. 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
  1156. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1157. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1158. if( type != C_EQ )
  1159. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1160. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1161. add_scripti(1);
  1162. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1163. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1164. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1165. add_scripti(1);
  1166. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1167. } else { // Process the value as an expression
  1168. p = parse_subexpr(p, -1);
  1169. if( type != C_EQ )
  1170. {// push the type of modifier onto the stack
  1171. add_scriptc(type);
  1172. }
  1173. }
  1174. // decrement the curly count for the position within the script
  1175. -- syntax.curly_count;
  1176. // close the script by appending the function operator
  1177. add_scriptc(C_FUNC);
  1178. // push the buffer from the method
  1179. return p;
  1180. }
  1181. /*
  1182. * Checks whether the gives string is a number literal
  1183. *
  1184. * Mainly necessary to differentiate between number literals and NPC name
  1185. * constants, since several of those start with a digit.
  1186. *
  1187. * All this does is to check if the string begins with an optional + or - sign,
  1188. * followed by a hexadecimal or decimal number literal literal and is NOT
  1189. * followed by a underscore or letter.
  1190. *
  1191. * @author : Hercules.ws
  1192. * @param p Pointer to the string to check
  1193. * @return Whether the string is a number literal
  1194. */
  1195. bool is_number(const char *p) {
  1196. const char *np;
  1197. if (!p)
  1198. return false;
  1199. if (*p == '-' || *p == '+')
  1200. p++;
  1201. np = p;
  1202. if (*p == '0' && p[1] == 'x') {
  1203. p+=2;
  1204. np = p;
  1205. // Hexadecimal
  1206. while (ISXDIGIT(*np))
  1207. np++;
  1208. } else {
  1209. // Decimal
  1210. while (ISDIGIT(*np))
  1211. np++;
  1212. }
  1213. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1214. return true;
  1215. return false;
  1216. }
  1217. /*==========================================
  1218. * Analysis section
  1219. *------------------------------------------*/
  1220. const char* parse_simpleexpr(const char *p)
  1221. {
  1222. long long i;
  1223. p=skip_space(p);
  1224. if(*p==';' || *p==',')
  1225. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1226. if(*p=='('){
  1227. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1228. ++syntax.curly[i].count;
  1229. p=parse_subexpr(p+1,-1);
  1230. p=skip_space(p);
  1231. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1232. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1233. ){
  1234. if( *p == ',' ){
  1235. syntax.curly[i].flag = ARGLIST_PAREN;
  1236. return p;
  1237. } else
  1238. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1239. }
  1240. if( *p != ')' )
  1241. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1242. ++p;
  1243. } else if(is_number(p)) {
  1244. char *np;
  1245. while(*p == '0' && ISDIGIT(p[1])) p++;
  1246. i=strtoll(p,&np,0);
  1247. if( i < INT_MIN ) {
  1248. i = INT_MIN;
  1249. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1250. } else if( i > INT_MAX ) {
  1251. i = INT_MAX;
  1252. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1253. }
  1254. add_scripti((int)i);
  1255. p=np;
  1256. } else if(*p=='"'){
  1257. add_scriptc(C_STR);
  1258. p++;
  1259. while( *p && *p != '"' ){
  1260. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1261. {
  1262. char buf[8];
  1263. size_t len = skip_escaped_c(p) - p;
  1264. size_t n = sv_unescape_c(buf, p, len);
  1265. if( n != 1 )
  1266. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1267. p += len;
  1268. add_scriptb(*buf);
  1269. continue;
  1270. }
  1271. else if( *p == '\n' )
  1272. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1273. add_scriptb(*p++);
  1274. }
  1275. if(!*p)
  1276. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1277. add_scriptb(0);
  1278. p++; //'"'
  1279. } else {
  1280. int l;
  1281. const char* pv;
  1282. // label , register , function etc
  1283. if(skip_word(p)==p)
  1284. disp_error_message("parse_simpleexpr: unexpected character",p);
  1285. l=add_word(p);
  1286. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1287. return parse_callfunc(p,1,0);
  1288. else {
  1289. const char* name = get_str(l);
  1290. if( strdb_get(userfunc_db,name) != NULL ) {
  1291. return parse_callfunc(p,1,1);
  1292. }
  1293. }
  1294. if( (pv = parse_variable(p)) )
  1295. {// successfully processed a variable assignment
  1296. return pv;
  1297. }
  1298. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1299. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1300. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1301. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1302. }
  1303. #endif
  1304. p=skip_word(p);
  1305. if( *p == '[' ){
  1306. // array(name[i] => getelementofarray(name,i) )
  1307. add_scriptl(buildin_getelementofarray_ref);
  1308. add_scriptc(C_ARG);
  1309. add_scriptl(l);
  1310. p=parse_subexpr(p+1,-1);
  1311. p=skip_space(p);
  1312. if( *p != ']' )
  1313. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1314. ++p;
  1315. add_scriptc(C_FUNC);
  1316. }else
  1317. add_scriptl(l);
  1318. }
  1319. return p;
  1320. }
  1321. /*==========================================
  1322. * Analysis of the expression
  1323. *------------------------------------------*/
  1324. const char* parse_subexpr(const char* p,int limit)
  1325. {
  1326. int op,opl,len;
  1327. p=skip_space(p);
  1328. if( *p == '-' ){
  1329. const char* tmpp = skip_space(p+1);
  1330. if( *tmpp == ';' || *tmpp == ',' ){
  1331. add_scriptl(LABEL_NEXTLINE);
  1332. p++;
  1333. return p;
  1334. }
  1335. }
  1336. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1337. p = parse_variable(p);
  1338. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1339. p = parse_subexpr(p + 1, 11);
  1340. add_scriptc(op);
  1341. } else
  1342. p = parse_simpleexpr(p);
  1343. p = skip_space(p);
  1344. while((
  1345. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1346. ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') ||
  1347. ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') ||
  1348. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1349. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1350. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1351. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1352. (op=C_AND,opl=5,len=1,*p=='&') ||
  1353. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1354. (op=C_OR,opl=3,len=1,*p=='|') ||
  1355. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1356. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1357. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1358. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1359. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1360. (op=C_GT,opl=7,len=1,*p=='>') ||
  1361. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1362. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1363. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1364. p+=len;
  1365. if(op == C_OP3) {
  1366. p=parse_subexpr(p,-1);
  1367. p=skip_space(p);
  1368. if( *(p++) != ':')
  1369. disp_error_message("parse_subexpr: expected ':'", p-1);
  1370. p=parse_subexpr(p,-1);
  1371. } else {
  1372. p=parse_subexpr(p,opl);
  1373. }
  1374. add_scriptc(op);
  1375. p=skip_space(p);
  1376. }
  1377. return p; /* return first untreated operator */
  1378. }
  1379. /*==========================================
  1380. * Evaluation of the expression
  1381. *------------------------------------------*/
  1382. const char* parse_expr(const char *p)
  1383. {
  1384. switch(*p){
  1385. case ')': case ';': case ':': case '[': case ']':
  1386. case '}':
  1387. disp_error_message("parse_expr: unexpected character",p);
  1388. }
  1389. p=parse_subexpr(p,-1);
  1390. return p;
  1391. }
  1392. /*==========================================
  1393. * Analysis of the line
  1394. *------------------------------------------*/
  1395. const char* parse_line(const char* p)
  1396. {
  1397. const char* p2;
  1398. p=skip_space(p);
  1399. if(*p==';') {
  1400. //Close decision for if(); for(); while();
  1401. p = parse_syntax_close(p + 1);
  1402. return p;
  1403. }
  1404. if(*p==')' && parse_syntax_for_flag)
  1405. return p+1;
  1406. p = skip_space(p);
  1407. if(p[0] == '{') {
  1408. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1409. syntax.curly[syntax.curly_count].count = -1;
  1410. syntax.curly[syntax.curly_count].index = -1;
  1411. syntax.curly_count++;
  1412. return p + 1;
  1413. } else if(p[0] == '}') {
  1414. return parse_curly_close(p);
  1415. }
  1416. // Syntax-related processing
  1417. p2 = parse_syntax(p);
  1418. if(p2 != NULL)
  1419. return p2;
  1420. // attempt to process a variable assignment
  1421. p2 = parse_variable(p);
  1422. if( p2 != NULL )
  1423. {// variable assignment processed so leave the method
  1424. return parse_syntax_close(p2 + 1);
  1425. }
  1426. p = parse_callfunc(p,0,0);
  1427. p = skip_space(p);
  1428. if(parse_syntax_for_flag) {
  1429. if( *p != ')' )
  1430. disp_error_message("parse_line: expected ')'",p);
  1431. } else {
  1432. if( *p != ';' )
  1433. disp_error_message("parse_line: expected ';'",p);
  1434. }
  1435. //Binding decision for if(), for(), while()
  1436. p = parse_syntax_close(p+1);
  1437. return p;
  1438. }
  1439. // { ... } Closing process
  1440. const char* parse_curly_close(const char* p)
  1441. {
  1442. if(syntax.curly_count <= 0) {
  1443. disp_error_message("parse_curly_close: unexpected string",p);
  1444. return p + 1;
  1445. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1446. syntax.curly_count--;
  1447. //Close decision if, for , while
  1448. p = parse_syntax_close(p + 1);
  1449. return p;
  1450. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1451. //Closing switch()
  1452. int pos = syntax.curly_count-1;
  1453. char label[256];
  1454. int l;
  1455. // Remove temporary variables
  1456. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1457. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1458. parse_line(label);
  1459. syntax.curly_count--;
  1460. // Go to the end pointer unconditionally
  1461. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1462. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1463. parse_line(label);
  1464. syntax.curly_count--;
  1465. // You are here labeled
  1466. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1467. l=add_str(label);
  1468. set_label(l,script_pos, p);
  1469. if(syntax.curly[pos].flag) {
  1470. //Exists default
  1471. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1472. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1473. parse_line(label);
  1474. syntax.curly_count--;
  1475. }
  1476. // Label end
  1477. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1478. l=add_str(label);
  1479. set_label(l,script_pos, p);
  1480. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1481. syntax.curly_count--;
  1482. //Closing decision if, for , while
  1483. p = parse_syntax_close(p + 1);
  1484. return p;
  1485. } else {
  1486. disp_error_message("parse_curly_close: unexpected string",p);
  1487. return p + 1;
  1488. }
  1489. }
  1490. // Syntax-related processing
  1491. // break, case, continue, default, do, for, function,
  1492. // if, switch, while ? will handle this internally.
  1493. const char* parse_syntax(const char* p)
  1494. {
  1495. const char *p2 = skip_word(p);
  1496. switch(*p) {
  1497. case 'B':
  1498. case 'b':
  1499. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1500. // break Processing
  1501. char label[256];
  1502. int pos = syntax.curly_count - 1;
  1503. while(pos >= 0) {
  1504. if(syntax.curly[pos].type == TYPE_DO) {
  1505. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1506. break;
  1507. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1508. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1509. break;
  1510. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1511. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1512. break;
  1513. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1514. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1515. break;
  1516. }
  1517. pos--;
  1518. }
  1519. if(pos < 0) {
  1520. disp_error_message("parse_syntax: unexpected 'break'",p);
  1521. } else {
  1522. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1523. parse_line(label);
  1524. syntax.curly_count--;
  1525. }
  1526. p = skip_space(p2);
  1527. if(*p != ';')
  1528. disp_error_message("parse_syntax: expected ';'",p);
  1529. // Closing decision if, for , while
  1530. p = parse_syntax_close(p + 1);
  1531. return p;
  1532. }
  1533. break;
  1534. case 'c':
  1535. case 'C':
  1536. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1537. //Processing case
  1538. int pos = syntax.curly_count-1;
  1539. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1540. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1541. return p+1;
  1542. } else {
  1543. char label[256];
  1544. int l,v;
  1545. char *np;
  1546. if(syntax.curly[pos].count != 1) {
  1547. //Jump for FALLTHRU
  1548. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1549. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1550. parse_line(label);
  1551. syntax.curly_count--;
  1552. // You are here labeled
  1553. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1554. l=add_str(label);
  1555. set_label(l,script_pos, p);
  1556. }
  1557. //Decision statement switch
  1558. p = skip_space(p2);
  1559. if(p == p2) {
  1560. disp_error_message("parse_syntax: expected a space ' '",p);
  1561. }
  1562. // check whether case label is integer or not
  1563. if(is_number(p)) {
  1564. //Numeric value
  1565. v = (int)strtol(p,&np,0);
  1566. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1567. p++;
  1568. p = skip_word(p);
  1569. if(np != p)
  1570. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1571. } else {
  1572. //Check for constants
  1573. p2 = skip_word(p);
  1574. v = (int)(size_t) (p2-p); // length of word at p2
  1575. memcpy(label,p,v);
  1576. label[v]='\0';
  1577. if( !script_get_constant(label, &v) )
  1578. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1579. p = skip_word(p);
  1580. }
  1581. p = skip_space(p);
  1582. if(*p != ':')
  1583. disp_error_message("parse_syntax: expect ':'",p);
  1584. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1585. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1586. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1587. // Bad I do not parse twice
  1588. p2 = parse_line(label);
  1589. parse_line(p2);
  1590. syntax.curly_count--;
  1591. if(syntax.curly[pos].count != 1) {
  1592. // Label after the completion of FALLTHRU
  1593. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1594. l=add_str(label);
  1595. set_label(l,script_pos,p);
  1596. }
  1597. // check duplication of case label [Rayce]
  1598. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1599. disp_error_message("parse_syntax: dup 'case'",p);
  1600. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1601. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1602. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1603. parse_line(label);
  1604. syntax.curly_count--;
  1605. syntax.curly[pos].count++;
  1606. }
  1607. return p + 1;
  1608. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1609. // Processing continue
  1610. char label[256];
  1611. int pos = syntax.curly_count - 1;
  1612. while(pos >= 0) {
  1613. if(syntax.curly[pos].type == TYPE_DO) {
  1614. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1615. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1616. break;
  1617. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1618. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1619. break;
  1620. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1621. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1622. break;
  1623. }
  1624. pos--;
  1625. }
  1626. if(pos < 0) {
  1627. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1628. } else {
  1629. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1630. parse_line(label);
  1631. syntax.curly_count--;
  1632. }
  1633. p = skip_space(p2);
  1634. if(*p != ';')
  1635. disp_error_message("parse_syntax: expected ';'",p);
  1636. //Closing decision if, for , while
  1637. p = parse_syntax_close(p + 1);
  1638. return p;
  1639. }
  1640. break;
  1641. case 'd':
  1642. case 'D':
  1643. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1644. // Switch - default processing
  1645. int pos = syntax.curly_count-1;
  1646. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1647. disp_error_message("parse_syntax: unexpected 'default'",p);
  1648. } else if(syntax.curly[pos].flag) {
  1649. disp_error_message("parse_syntax: dup 'default'",p);
  1650. } else {
  1651. char label[256];
  1652. int l;
  1653. // Put the label location
  1654. p = skip_space(p2);
  1655. if(*p != ':') {
  1656. disp_error_message("parse_syntax: expected ':'",p);
  1657. }
  1658. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1659. l=add_str(label);
  1660. set_label(l,script_pos,p);
  1661. // Skip to the next link w/o condition
  1662. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1663. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1664. parse_line(label);
  1665. syntax.curly_count--;
  1666. // The default label
  1667. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1668. l=add_str(label);
  1669. set_label(l,script_pos,p);
  1670. syntax.curly[syntax.curly_count - 1].flag = 1;
  1671. syntax.curly[pos].count++;
  1672. }
  1673. return p + 1;
  1674. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1675. int l;
  1676. char label[256];
  1677. p=skip_space(p2);
  1678. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1679. syntax.curly[syntax.curly_count].count = 1;
  1680. syntax.curly[syntax.curly_count].index = syntax.index++;
  1681. syntax.curly[syntax.curly_count].flag = 0;
  1682. // Label of the (do) form here
  1683. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1684. l=add_str(label);
  1685. set_label(l,script_pos,p);
  1686. syntax.curly_count++;
  1687. return p;
  1688. }
  1689. break;
  1690. case 'f':
  1691. case 'F':
  1692. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1693. int l;
  1694. char label[256];
  1695. int pos = syntax.curly_count;
  1696. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1697. syntax.curly[syntax.curly_count].count = 1;
  1698. syntax.curly[syntax.curly_count].index = syntax.index++;
  1699. syntax.curly[syntax.curly_count].flag = 0;
  1700. syntax.curly_count++;
  1701. p=skip_space(p2);
  1702. if(*p != '(')
  1703. disp_error_message("parse_syntax: expected '('",p);
  1704. p++;
  1705. // Execute the initialization statement
  1706. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1707. p=parse_line(p);
  1708. syntax.curly_count--;
  1709. // Form the start of label decision
  1710. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1711. l=add_str(label);
  1712. set_label(l,script_pos,p);
  1713. p=skip_space(p);
  1714. if(*p == ';') {
  1715. // For (; Because the pattern of always true ;)
  1716. ;
  1717. } else {
  1718. // Skip to the end point if the condition is false
  1719. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1720. add_scriptl(add_str("jump_zero"));
  1721. add_scriptc(C_ARG);
  1722. p=parse_expr(p);
  1723. p=skip_space(p);
  1724. add_scriptl(add_str(label));
  1725. add_scriptc(C_FUNC);
  1726. }
  1727. if(*p != ';')
  1728. disp_error_message("parse_syntax: expected ';'",p);
  1729. p++;
  1730. // Skip to the beginning of the loop
  1731. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1732. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1733. parse_line(label);
  1734. syntax.curly_count--;
  1735. // Labels to form the next loop
  1736. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1737. l=add_str(label);
  1738. set_label(l,script_pos,p);
  1739. // Process the next time you enter the loop
  1740. // A ')' last for; flag to be treated as'
  1741. parse_syntax_for_flag = 1;
  1742. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1743. p=parse_line(p);
  1744. syntax.curly_count--;
  1745. parse_syntax_for_flag = 0;
  1746. // Skip to the determination process conditions
  1747. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1748. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1749. parse_line(label);
  1750. syntax.curly_count--;
  1751. // Loop start labeling
  1752. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1753. l=add_str(label);
  1754. set_label(l,script_pos,p);
  1755. return p;
  1756. }
  1757. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1758. {// internal script function
  1759. const char *func_name;
  1760. func_name = skip_space(p2);
  1761. p = skip_word(func_name);
  1762. if( p == func_name )
  1763. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1764. p2 = skip_space(p);
  1765. if( *p2 == ';' )
  1766. {// function <name> ;
  1767. // function declaration - just register the name
  1768. int l;
  1769. l = add_word(func_name);
  1770. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1771. str_data[l].type = C_USERFUNC;
  1772. else if( str_data[l].type == C_USERFUNC )
  1773. ; // already registered
  1774. else
  1775. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1776. // Close condition of if, for, while
  1777. p = parse_syntax_close(p2 + 1);
  1778. return p;
  1779. }
  1780. else if(*p2 == '{')
  1781. {// function <name> <line/block of code>
  1782. char label[256];
  1783. int l;
  1784. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1785. syntax.curly[syntax.curly_count].count = 1;
  1786. syntax.curly[syntax.curly_count].index = syntax.index++;
  1787. syntax.curly[syntax.curly_count].flag = 0;
  1788. ++syntax.curly_count;
  1789. // Jump over the function code
  1790. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1791. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1792. ++syntax.curly_count;
  1793. parse_line(label);
  1794. --syntax.curly_count;
  1795. // Set the position of the function (label)
  1796. l=add_word(func_name);
  1797. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1798. {
  1799. str_data[l].type = C_USERFUNC;
  1800. set_label(l, script_pos, p);
  1801. if( parse_options&SCRIPT_USE_LABEL_DB )
  1802. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1803. }
  1804. else
  1805. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1806. return skip_space(p);
  1807. }
  1808. else
  1809. {
  1810. disp_error_message("expect ';' or '{' at function syntax",p);
  1811. }
  1812. }
  1813. break;
  1814. case 'i':
  1815. case 'I':
  1816. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1817. // If process
  1818. char label[256];
  1819. p=skip_space(p2);
  1820. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1821. disp_error_message("need '('",p);
  1822. }
  1823. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1824. syntax.curly[syntax.curly_count].count = 1;
  1825. syntax.curly[syntax.curly_count].index = syntax.index++;
  1826. syntax.curly[syntax.curly_count].flag = 0;
  1827. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1828. syntax.curly_count++;
  1829. add_scriptl(add_str("jump_zero"));
  1830. add_scriptc(C_ARG);
  1831. p=parse_expr(p);
  1832. p=skip_space(p);
  1833. add_scriptl(add_str(label));
  1834. add_scriptc(C_FUNC);
  1835. return p;
  1836. }
  1837. break;
  1838. case 's':
  1839. case 'S':
  1840. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1841. // Processing of switch ()
  1842. char label[256];
  1843. p=skip_space(p2);
  1844. if(*p != '(') {
  1845. disp_error_message("need '('",p);
  1846. }
  1847. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1848. syntax.curly[syntax.curly_count].count = 1;
  1849. syntax.curly[syntax.curly_count].index = syntax.index++;
  1850. syntax.curly[syntax.curly_count].flag = 0;
  1851. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1852. syntax.curly_count++;
  1853. add_scriptl(add_str("set"));
  1854. add_scriptc(C_ARG);
  1855. add_scriptl(add_str(label));
  1856. p=parse_expr(p);
  1857. p=skip_space(p);
  1858. if(*p != '{') {
  1859. disp_error_message("parse_syntax: expected '{'",p);
  1860. }
  1861. add_scriptc(C_FUNC);
  1862. return p + 1;
  1863. }
  1864. break;
  1865. case 'w':
  1866. case 'W':
  1867. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1868. int l;
  1869. char label[256];
  1870. p=skip_space(p2);
  1871. if(*p != '(') {
  1872. disp_error_message("need '('",p);
  1873. }
  1874. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1875. syntax.curly[syntax.curly_count].count = 1;
  1876. syntax.curly[syntax.curly_count].index = syntax.index++;
  1877. syntax.curly[syntax.curly_count].flag = 0;
  1878. // Form the start of label decision
  1879. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1880. l=add_str(label);
  1881. set_label(l,script_pos,p);
  1882. // Skip to the end point if the condition is false
  1883. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1884. syntax.curly_count++;
  1885. add_scriptl(add_str("jump_zero"));
  1886. add_scriptc(C_ARG);
  1887. p=parse_expr(p);
  1888. p=skip_space(p);
  1889. add_scriptl(add_str(label));
  1890. add_scriptc(C_FUNC);
  1891. return p;
  1892. }
  1893. break;
  1894. }
  1895. return NULL;
  1896. }
  1897. const char* parse_syntax_close(const char *p) {
  1898. // If (...) for (...) hoge (); as to make sure closed closed once again
  1899. int flag;
  1900. do {
  1901. p = parse_syntax_close_sub(p,&flag);
  1902. } while(flag);
  1903. return p;
  1904. }
  1905. // Close judgment if, for, while, of do
  1906. // flag == 1 : closed
  1907. // flag == 0 : not closed
  1908. const char* parse_syntax_close_sub(const char* p,int* flag)
  1909. {
  1910. char label[256];
  1911. int pos = syntax.curly_count - 1;
  1912. int l;
  1913. *flag = 1;
  1914. if(syntax.curly_count <= 0) {
  1915. *flag = 0;
  1916. return p;
  1917. } else if(syntax.curly[pos].type == TYPE_IF) {
  1918. const char *bp = p;
  1919. const char *p2;
  1920. // if-block and else-block end is a new line
  1921. parse_nextline(false, p);
  1922. // Skip to the last location if
  1923. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1924. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1925. parse_line(label);
  1926. syntax.curly_count--;
  1927. // Put the label of the location
  1928. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1929. l=add_str(label);
  1930. set_label(l,script_pos,p);
  1931. syntax.curly[pos].count++;
  1932. p = skip_space(p);
  1933. p2 = skip_word(p);
  1934. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1935. // else or else - if
  1936. p = skip_space(p2);
  1937. p2 = skip_word(p);
  1938. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1939. // else - if
  1940. p=skip_space(p2);
  1941. if(*p != '(') {
  1942. disp_error_message("need '('",p);
  1943. }
  1944. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1945. add_scriptl(add_str("jump_zero"));
  1946. add_scriptc(C_ARG);
  1947. p=parse_expr(p);
  1948. p=skip_space(p);
  1949. add_scriptl(add_str(label));
  1950. add_scriptc(C_FUNC);
  1951. *flag = 0;
  1952. return p;
  1953. } else {
  1954. // else
  1955. if(!syntax.curly[pos].flag) {
  1956. syntax.curly[pos].flag = 1;
  1957. *flag = 0;
  1958. return p;
  1959. }
  1960. }
  1961. }
  1962. // Close if
  1963. syntax.curly_count--;
  1964. // Put the label of the final location
  1965. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1966. l=add_str(label);
  1967. set_label(l,script_pos,p);
  1968. if(syntax.curly[pos].flag == 1) {
  1969. // Because the position of the pointer is the same if not else for this
  1970. return bp;
  1971. }
  1972. return p;
  1973. } else if(syntax.curly[pos].type == TYPE_DO) {
  1974. int l2;
  1975. char label2[256];
  1976. const char *p2;
  1977. if(syntax.curly[pos].flag) {
  1978. // (Come here continue) to form the label here
  1979. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1980. l2=add_str(label2);
  1981. set_label(l2,script_pos,p);
  1982. }
  1983. // Skip to the end point if the condition is false
  1984. p = skip_space(p);
  1985. p2 = skip_word(p);
  1986. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1987. disp_error_message("parse_syntax: expected 'while'",p);
  1988. p = skip_space(p2);
  1989. if(*p != '(') {
  1990. disp_error_message("need '('",p);
  1991. }
  1992. // do-block end is a new line
  1993. parse_nextline(false, p);
  1994. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1995. add_scriptl(add_str("jump_zero"));
  1996. add_scriptc(C_ARG);
  1997. p=parse_expr(p);
  1998. p=skip_space(p);
  1999. add_scriptl(add_str(label2));
  2000. add_scriptc(C_FUNC);
  2001. // Skip to the starting point
  2002. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  2003. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2004. parse_line(label2);
  2005. syntax.curly_count--;
  2006. // Form label of the end point conditions
  2007. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  2008. l2=add_str(label2);
  2009. set_label(l2,script_pos,p);
  2010. p = skip_space(p);
  2011. if(*p != ';') {
  2012. disp_error_message("parse_syntax: expected ';'",p);
  2013. return p+1;
  2014. }
  2015. p++;
  2016. syntax.curly_count--;
  2017. return p;
  2018. } else if(syntax.curly[pos].type == TYPE_FOR) {
  2019. // for-block end is a new line
  2020. parse_nextline(false, p);
  2021. // Skip to the next loop
  2022. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  2023. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2024. parse_line(label);
  2025. syntax.curly_count--;
  2026. // End for labeling
  2027. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  2028. l=add_str(label);
  2029. set_label(l,script_pos,p);
  2030. syntax.curly_count--;
  2031. return p;
  2032. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  2033. // while-block end is a new line
  2034. parse_nextline(false, p);
  2035. // Skip to the decision while
  2036. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  2037. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2038. parse_line(label);
  2039. syntax.curly_count--;
  2040. // End while labeling
  2041. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  2042. l=add_str(label);
  2043. set_label(l,script_pos,p);
  2044. syntax.curly_count--;
  2045. return p;
  2046. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  2047. int pos2 = syntax.curly_count-1;
  2048. char label2[256];
  2049. int l2;
  2050. // Back
  2051. sprintf(label2,"return;");
  2052. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2053. parse_line(label2);
  2054. syntax.curly_count--;
  2055. // Put the label of the location
  2056. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  2057. l2=add_str(label2);
  2058. set_label(l2,script_pos,p);
  2059. syntax.curly_count--;
  2060. return p;
  2061. } else {
  2062. *flag = 0;
  2063. return p;
  2064. }
  2065. }
  2066. /*==========================================
  2067. * Added built-in functions
  2068. *------------------------------------------*/
  2069. static void add_buildin_func(void)
  2070. {
  2071. int i;
  2072. for( i = 0; buildin_func[i].func; i++ )
  2073. {
  2074. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2075. // 'v' - value (either string or int or reference)
  2076. // 's' - string
  2077. // 'i' - int
  2078. // 'r' - reference (of a variable)
  2079. // 'l' - label
  2080. // '?' - one optional parameter
  2081. // '*' - unknown number of optional parameters
  2082. const char* p = buildin_func[i].arg;
  2083. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2084. while( *p == '?' ) ++p;
  2085. if( *p == '*' ) ++p;
  2086. if( *p != 0){
  2087. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2088. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2089. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2090. } else {
  2091. int n = add_str(buildin_func[i].name);
  2092. str_data[n].type = C_FUNC;
  2093. str_data[n].val = i;
  2094. str_data[n].func = buildin_func[i].func;
  2095. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  2096. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2097. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2098. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2099. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2100. }
  2101. }
  2102. }
  2103. /// Retrieves the value of a constant parameter.
  2104. bool script_get_parameter(const char* name, int* value)
  2105. {
  2106. int n = search_str(name);
  2107. if (n == -1 || str_data[n].type != C_PARAM)
  2108. {// not found or not a parameter
  2109. return false;
  2110. }
  2111. value[0] = str_data[n].val;
  2112. return true;
  2113. }
  2114. /// Retrieves the value of a constant.
  2115. bool script_get_constant(const char* name, int* value)
  2116. {
  2117. int n = search_str(name);
  2118. if( n == -1 || str_data[n].type != C_INT )
  2119. {// not found or not a constant
  2120. return false;
  2121. }
  2122. value[0] = str_data[n].val;
  2123. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2124. if( str_data[n].deprecated ){
  2125. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2126. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2127. }
  2128. #endif
  2129. return true;
  2130. }
  2131. /// Creates new constant or parameter with given value.
  2132. void script_set_constant(const char* name, int value, bool isparameter, bool deprecated)
  2133. {
  2134. int n = add_str(name);
  2135. if( str_data[n].type == C_NOP )
  2136. {// new
  2137. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2138. str_data[n].val = value;
  2139. str_data[n].deprecated = deprecated;
  2140. }
  2141. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2142. {// existing parameter or constant
  2143. 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);
  2144. }
  2145. else
  2146. {// existing name
  2147. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2148. }
  2149. }
  2150. static bool read_constdb_sub( char* fields[], int columns, int current ){
  2151. char name[1024], val[1024];
  2152. int type = 0;
  2153. if( columns > 1 ){
  2154. if( sscanf(fields[0], "%1023[A-Za-z0-9/_]", name) != 1 ||
  2155. sscanf(fields[1], "%1023[A-Za-z0-9/_]", val) != 1 ||
  2156. ( columns >= 2 && sscanf(fields[2], "%11d", &type) != 1 ) ){
  2157. ShowWarning("Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current);
  2158. return false;
  2159. }
  2160. }else{
  2161. if( sscanf(fields[0], "%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d", name, val, &type) < 2 ){
  2162. ShowWarning( "Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current );
  2163. return false;
  2164. }
  2165. }
  2166. script_set_constant(name, (int)strtol(val, NULL, 0), (type != 0), false);
  2167. return true;
  2168. }
  2169. /*==========================================
  2170. * Reading constant databases
  2171. * const.txt
  2172. *------------------------------------------*/
  2173. static void read_constdb(void){
  2174. const char* dbsubpath[] = {
  2175. "",
  2176. "/" DBIMPORT,
  2177. };
  2178. for( int i = 0; i < ARRAYLENGTH(dbsubpath); i++ ){
  2179. int n2 = strlen(db_path) + strlen(dbsubpath[i]) + 1;
  2180. char* dbsubpath2 = (char*)aMalloc(n2 + 1);
  2181. bool silent = i > 0;
  2182. safesnprintf(dbsubpath2, n2, "%s%s", db_path, dbsubpath[i]);
  2183. sv_readdb(dbsubpath2, "const.txt", ',', 1, 3, -1, &read_constdb_sub, silent);
  2184. aFree(dbsubpath2);
  2185. }
  2186. }
  2187. /**
  2188. * Sets source-end constants for NPC scripts to access.
  2189. **/
  2190. void script_hardcoded_constants(void) {
  2191. #include "script_constants.hpp"
  2192. }
  2193. /*==========================================
  2194. * Display emplacement line of script
  2195. *------------------------------------------*/
  2196. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2197. {
  2198. int i;
  2199. if( p == NULL || !p[0] ) return NULL;
  2200. if( line < 0 )
  2201. StringBuf_Printf(buf, "*% 5d : ", -line);
  2202. else
  2203. StringBuf_Printf(buf, " % 5d : ", line);
  2204. for(i=0;p[i] && p[i] != '\n';i++){
  2205. if(p + i != mark)
  2206. StringBuf_Printf(buf, "%c", p[i]);
  2207. else
  2208. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2209. }
  2210. StringBuf_AppendStr(buf, "\n");
  2211. return p+i+(p[i] == '\n' ? 1 : 0);
  2212. }
  2213. 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) {
  2214. // Find the line where the error occurred
  2215. int j;
  2216. int line = start_line;
  2217. const char *p;
  2218. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2219. for(p=src;p && *p;line++){
  2220. const char *lineend=strchr(p,'\n');
  2221. if(lineend==NULL || error_pos_cur<lineend){
  2222. break;
  2223. }
  2224. for( j = 0; j < 4; j++ ) {
  2225. linestart[j] = linestart[j+1];
  2226. }
  2227. linestart[4] = p;
  2228. p=lineend+1;
  2229. }
  2230. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2231. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2232. for(j = 0; j < 5; j++ ) {
  2233. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2234. }
  2235. p = script_print_line(buf, p, error_pos_cur, -line);
  2236. for(j = 0; j < 5; j++) {
  2237. p = script_print_line(buf, p, NULL, line + j + 1 );
  2238. }
  2239. }
  2240. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2241. StringBuf buf;
  2242. StringBuf_Init(&buf);
  2243. StringBuf_AppendStr(&buf, "\a\n");
  2244. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2245. ShowError("%s", StringBuf_Value(&buf));
  2246. StringBuf_Destroy(&buf);
  2247. }
  2248. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2249. StringBuf buf;
  2250. StringBuf_Init(&buf);
  2251. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2252. ShowWarning("%s", StringBuf_Value(&buf));
  2253. StringBuf_Destroy(&buf);
  2254. }
  2255. /*==========================================
  2256. * Analysis of the script
  2257. *------------------------------------------*/
  2258. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2259. {
  2260. const char *p,*tmpp;
  2261. int i;
  2262. struct script_code* code = NULL;
  2263. static bool first=true;
  2264. char end;
  2265. bool unresolved_names = false;
  2266. parser_current_src = src;
  2267. parser_current_file = file;
  2268. parser_current_line = line;
  2269. if( src == NULL )
  2270. return NULL;// empty script
  2271. memset(&syntax,0,sizeof(syntax));
  2272. if(first){
  2273. add_buildin_func();
  2274. read_constdb();
  2275. script_hardcoded_constants();
  2276. first=false;
  2277. }
  2278. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2279. script_pos=0;
  2280. script_size=SCRIPT_BLOCK_SIZE;
  2281. parse_nextline(true, NULL);
  2282. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2283. if( options&SCRIPT_USE_LABEL_DB )
  2284. db_clear(scriptlabel_db);
  2285. parse_options = options;
  2286. if( setjmp( error_jump ) != 0 ) {
  2287. //Restore program state when script has problems. [from jA]
  2288. int j;
  2289. const int size = ARRAYLENGTH(syntax.curly);
  2290. if( error_report )
  2291. script_error(src,file,line,error_msg,error_pos);
  2292. aFree( error_msg );
  2293. aFree( script_buf );
  2294. script_pos = 0;
  2295. script_size = 0;
  2296. script_buf = NULL;
  2297. for(j=LABEL_START;j<str_num;j++)
  2298. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2299. for(j=0; j<size; j++)
  2300. linkdb_final(&syntax.curly[j].case_label);
  2301. return NULL;
  2302. }
  2303. parse_syntax_for_flag=0;
  2304. p=src;
  2305. p=skip_space(p);
  2306. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2307. {// does not require brackets around the script
  2308. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2309. {// empty script and can return NULL
  2310. aFree( script_buf );
  2311. script_pos = 0;
  2312. script_size = 0;
  2313. script_buf = NULL;
  2314. return NULL;
  2315. }
  2316. end = '\0';
  2317. }
  2318. else
  2319. {// requires brackets around the script
  2320. if( *p != '{' )
  2321. disp_error_message("not found '{'",p);
  2322. p = skip_space(p+1);
  2323. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2324. {// empty script and can return NULL
  2325. aFree( script_buf );
  2326. script_pos = 0;
  2327. script_size = 0;
  2328. script_buf = NULL;
  2329. return NULL;
  2330. }
  2331. end = '}';
  2332. }
  2333. // clear references of labels, variables and internal functions
  2334. for(i=LABEL_START;i<str_num;i++){
  2335. if(
  2336. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2337. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2338. ){
  2339. str_data[i].type=C_NOP;
  2340. str_data[i].backpatch=-1;
  2341. str_data[i].label=-1;
  2342. }
  2343. }
  2344. while( syntax.curly_count != 0 || *p != end )
  2345. {
  2346. if( *p == '\0' )
  2347. disp_error_message("unexpected end of script",p);
  2348. // Special handling only label
  2349. tmpp=skip_space(skip_word(p));
  2350. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2351. i=add_word(p);
  2352. set_label(i,script_pos,p);
  2353. if( parse_options&SCRIPT_USE_LABEL_DB )
  2354. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2355. p=tmpp+1;
  2356. p=skip_space(p);
  2357. continue;
  2358. }
  2359. // All other lumped
  2360. p=parse_line(p);
  2361. p=skip_space(p);
  2362. parse_nextline(false, p);
  2363. }
  2364. add_scriptc(C_NOP);
  2365. // trim code to size
  2366. script_size = script_pos;
  2367. RECREATE(script_buf,unsigned char,script_pos);
  2368. // default unknown references to variables
  2369. for(i=LABEL_START;i<str_num;i++){
  2370. if(str_data[i].type==C_NOP){
  2371. int j;
  2372. str_data[i].type=C_NAME;
  2373. str_data[i].label=i;
  2374. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2375. int next=GETVALUE(script_buf,j);
  2376. SETVALUE(script_buf,j,i);
  2377. j=next;
  2378. }
  2379. }
  2380. else if( str_data[i].type == C_USERFUNC )
  2381. {// 'function name;' without follow-up code
  2382. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2383. unresolved_names = true;
  2384. }
  2385. }
  2386. if( unresolved_names )
  2387. {
  2388. disp_error_message("parse_script: unresolved function references", p);
  2389. }
  2390. #ifdef DEBUG_DISP
  2391. for(i=0;i<script_pos;i++){
  2392. if((i&15)==0) ShowMessage("%04x : ",i);
  2393. ShowMessage("%02x ",script_buf[i]);
  2394. if((i&15)==15) ShowMessage("\n");
  2395. }
  2396. ShowMessage("\n");
  2397. #endif
  2398. #ifdef DEBUG_DISASM
  2399. {
  2400. int i = 0,j;
  2401. while(i < script_pos) {
  2402. c_op op = get_com(script_buf,&i);
  2403. ShowMessage("%06x %s", i, script_op2name(op));
  2404. j = i;
  2405. switch(op) {
  2406. case C_INT:
  2407. ShowMessage(" %d", get_num(script_buf,&i));
  2408. break;
  2409. case C_POS:
  2410. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2411. i += 3;
  2412. break;
  2413. case C_NAME:
  2414. j = (*(int*)(script_buf+i)&0xffffff);
  2415. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2416. i += 3;
  2417. break;
  2418. case C_STR:
  2419. j = strlen(script_buf + i);
  2420. ShowMessage(" %s", script_buf + i);
  2421. i += j+1;
  2422. break;
  2423. }
  2424. ShowMessage(CL_CLL"\n");
  2425. }
  2426. }
  2427. #endif
  2428. CREATE(code,struct script_code,1);
  2429. code->script_buf = script_buf;
  2430. code->script_size = script_size;
  2431. code->local.vars = NULL;
  2432. code->local.arrays = NULL;
  2433. return code;
  2434. }
  2435. /// Returns the player attached to this script, identified by the rid.
  2436. /// If there is no player attached, the script is terminated.
  2437. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){
  2438. *sd = map_id2sd( st->rid );
  2439. if( *sd ){
  2440. return true;
  2441. }else{
  2442. ShowError("%s: fatal error ! player not attached!\n",func);
  2443. script_reportfunc(st);
  2444. script_reportsrc(st);
  2445. st->state = END;
  2446. return false;
  2447. }
  2448. }
  2449. /**
  2450. * Dereferences a variable/constant, replacing it with a copy of the value.
  2451. * @param st Script state
  2452. * @param data Variable/constant
  2453. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2454. */
  2455. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2456. {
  2457. const char* name;
  2458. char prefix;
  2459. char postfix;
  2460. if( !data_isreference(data) )
  2461. return;// not a variable/constant
  2462. name = reference_getname(data);
  2463. prefix = name[0];
  2464. postfix = name[strlen(name) - 1];
  2465. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2466. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2467. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2468. if( postfix == '$' ) {// string variable
  2469. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2470. data->type = C_CONSTSTR;
  2471. data->u.str = const_cast<char *>("");
  2472. } else {// integer variable
  2473. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2474. data->type = C_INT;
  2475. data->u.num = 0;
  2476. }
  2477. return;
  2478. }
  2479. }
  2480. if( postfix == '$' ) {// string variable
  2481. switch( prefix ) {
  2482. case '@':
  2483. data->u.str = pc_readregstr(sd, data->u.num);
  2484. break;
  2485. case '$':
  2486. data->u.str = mapreg_readregstr(data->u.num);
  2487. break;
  2488. case '#':
  2489. if( name[1] == '#' )
  2490. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2491. else
  2492. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2493. break;
  2494. case '.':
  2495. {
  2496. struct DBMap* n = data->ref ?
  2497. data->ref->vars : name[1] == '@' ?
  2498. st->stack->scope.vars : // instance/scope variable
  2499. st->script->local.vars; // npc variable
  2500. if( n )
  2501. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2502. else
  2503. data->u.str = NULL;
  2504. }
  2505. break;
  2506. case '\'':
  2507. {
  2508. unsigned short instance_id = script_instancegetid(st);
  2509. if( instance_id )
  2510. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2511. else {
  2512. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2513. data->u.str = NULL;
  2514. }
  2515. }
  2516. break;
  2517. default:
  2518. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2519. break;
  2520. }
  2521. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2522. data->type = C_CONSTSTR;
  2523. data->u.str = const_cast<char *>("");
  2524. } else {// duplicate string
  2525. data->type = C_STR;
  2526. data->u.str = aStrdup(data->u.str);
  2527. }
  2528. } else {// integer variable
  2529. data->type = C_INT;
  2530. if( reference_toconstant(data) ) {
  2531. data->u.num = reference_getconstant(data);
  2532. } else if( reference_toparam(data) ) {
  2533. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2534. } else
  2535. switch( prefix ) {
  2536. case '@':
  2537. data->u.num = pc_readreg(sd, data->u.num);
  2538. break;
  2539. case '$':
  2540. data->u.num = mapreg_readreg(data->u.num);
  2541. break;
  2542. case '#':
  2543. if( name[1] == '#' )
  2544. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2545. else
  2546. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2547. break;
  2548. case '.':
  2549. {
  2550. struct DBMap* n = data->ref ?
  2551. data->ref->vars : name[1] == '@' ?
  2552. st->stack->scope.vars : // instance/scope variable
  2553. st->script->local.vars; // npc variable
  2554. if( n )
  2555. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2556. else
  2557. data->u.num = 0;
  2558. }
  2559. break;
  2560. case '\'':
  2561. {
  2562. unsigned short instance_id = script_instancegetid(st);
  2563. if( instance_id )
  2564. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2565. else {
  2566. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2567. data->u.num = 0;
  2568. }
  2569. }
  2570. break;
  2571. default:
  2572. data->u.num = pc_readglobalreg(sd, data->u.num);
  2573. break;
  2574. }
  2575. }
  2576. data->ref = NULL;
  2577. return;
  2578. }
  2579. void get_val(struct script_state* st, struct script_data* data)
  2580. {
  2581. get_val_(st,data,NULL);
  2582. }
  2583. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2584. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2585. /// The value is left in the top of the stack and needs to be removed manually.
  2586. /// @param st[in]: script state.
  2587. /// @param uid[in]: reference identifier.
  2588. /// @param ref[in]: the container to look up the reference into.
  2589. /// @return: the retrieved value of the reference.
  2590. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2591. {
  2592. struct script_data* data;
  2593. push_val2(st->stack, C_NAME, uid, ref);
  2594. data = script_getdatatop(st, -1);
  2595. get_val(st, data);
  2596. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2597. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2598. }
  2599. /**
  2600. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2601. * Will be gone as soon as undefined var feature is implemented
  2602. **/
  2603. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2604. {
  2605. const char *name = get_str(script_getvarid(uid));
  2606. // is here st can be null pointer and st->rid is wrong?
  2607. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2608. bool insert = false;
  2609. if (sd && !st) {
  2610. // when sd comes, st isn't available
  2611. insert = true;
  2612. } else {
  2613. if( is_string_variable(name) ) {
  2614. char* str = (char*)get_val2(st, uid, ref);
  2615. if( str && *str )
  2616. insert = true;
  2617. script_removetop(st, -1, 0);
  2618. } else {
  2619. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2620. if( num )
  2621. insert = true;
  2622. script_removetop(st, -1, 0);
  2623. }
  2624. }
  2625. if (src && src->arrays) {
  2626. struct script_array *sa = static_cast<script_array *>(idb_get(src->arrays, script_getvarid(uid)));
  2627. if (sa) {
  2628. unsigned int i;
  2629. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2630. if( i != sa->size ) {
  2631. if( !insert )
  2632. script_array_remove_member(src,sa,i);
  2633. return;
  2634. }
  2635. script_array_add_member(sa,0);
  2636. } else if (insert) {
  2637. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2638. }
  2639. }
  2640. }
  2641. /**
  2642. * Returns array size by ID
  2643. **/
  2644. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2645. {
  2646. struct script_array *sa = NULL;
  2647. struct reg_db *src = script_array_src(st, sd, name, ref);
  2648. if (src && src->arrays)
  2649. sa = static_cast<script_array *>(idb_get(src->arrays, search_str(name)));
  2650. return sa ? sa->size : 0;
  2651. }
  2652. /**
  2653. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2654. **/
  2655. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2656. {
  2657. struct script_array *sa = NULL;
  2658. struct reg_db *src = script_array_src(st, sd, name, ref);
  2659. if (src && src->arrays) {
  2660. int key = add_word(name);
  2661. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2662. if( ( sa = static_cast<script_array *>(idb_get(src->arrays, key)) ) ) {
  2663. unsigned int i, highest_key = 0;
  2664. for(i = 0; i < sa->size; i++) {
  2665. if( sa->members[i] > highest_key )
  2666. highest_key = sa->members[i];
  2667. }
  2668. return sa->size ? highest_key + 1 : 0;
  2669. }
  2670. }
  2671. return SCRIPT_CMD_SUCCESS;
  2672. }
  2673. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2674. {
  2675. struct script_array *sa = static_cast<script_array *>(db_data2ptr(data));
  2676. aFree(sa->members);
  2677. ers_free(array_ers, sa);
  2678. return SCRIPT_CMD_SUCCESS;
  2679. }
  2680. /**
  2681. * Clears script_array and removes it from script->array_db
  2682. **/
  2683. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2684. {
  2685. aFree(sa->members);
  2686. idb_remove(src->arrays, sa->id);
  2687. ers_free(array_ers, sa);
  2688. }
  2689. /**
  2690. * Removes a member from a script_array list
  2691. *
  2692. * @param idx the index of the member in script_array struct list, not of the actual array member
  2693. **/
  2694. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2695. {
  2696. unsigned int i, cursor;
  2697. // it's the only member left, no need to do anything other than delete the array data
  2698. if( sa->size == 1 ) {
  2699. script_array_delete(src,sa);
  2700. return;
  2701. }
  2702. sa->members[idx] = UINT_MAX;
  2703. for(i = 0, cursor = 0; i < sa->size; i++) {
  2704. if( sa->members[i] == UINT_MAX )
  2705. continue;
  2706. if( i != cursor )
  2707. sa->members[cursor] = sa->members[i];
  2708. cursor++;
  2709. }
  2710. sa->size = cursor;
  2711. }
  2712. /**
  2713. * Appends a new array index to the list in script_array
  2714. *
  2715. * @param idx the index of the array member being inserted
  2716. **/
  2717. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2718. {
  2719. RECREATE(sa->members, unsigned int, ++sa->size);
  2720. sa->members[sa->size - 1] = idx;
  2721. }
  2722. /**
  2723. * Obtains the source of the array database for this type and scenario
  2724. * Initializes such database when not yet initialized.
  2725. **/
  2726. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2727. {
  2728. struct reg_db *src = NULL;
  2729. switch( name[0] ) {
  2730. // from player
  2731. default: // char reg
  2732. case '@': // temp char reg
  2733. case '#': // account reg
  2734. src = &sd->regs;
  2735. break;
  2736. case '$': // map reg
  2737. src = &regs;
  2738. break;
  2739. case '.': // npc/script
  2740. if (ref)
  2741. src = ref;
  2742. else
  2743. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2744. break;
  2745. case '\'': // instance
  2746. {
  2747. unsigned short instance_id = script_instancegetid(st);
  2748. if( instance_id ) {
  2749. src = &instance_data[instance_id].regs;
  2750. }
  2751. break;
  2752. }
  2753. }
  2754. if( src ) {
  2755. if( !src->arrays )
  2756. src->arrays = idb_alloc(DB_OPT_BASE);
  2757. return src;
  2758. }
  2759. return NULL;
  2760. }
  2761. /**
  2762. * Processes a array member modification, and update data accordingly
  2763. *
  2764. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2765. * @param num[in] Variable ID
  2766. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2767. **/
  2768. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2769. {
  2770. struct script_array *sa = NULL;
  2771. int id = script_getvarid(num);
  2772. unsigned int index = script_getvaridx(num);
  2773. if (!src->arrays) {
  2774. src->arrays = idb_alloc(DB_OPT_BASE);
  2775. } else {
  2776. sa = static_cast<script_array *>(idb_get(src->arrays, id));
  2777. }
  2778. if( sa ) {
  2779. unsigned int i;
  2780. // search
  2781. for(i = 0; i < sa->size; i++) {
  2782. if( sa->members[i] == index )
  2783. break;
  2784. }
  2785. // if existent
  2786. if( i != sa->size ) {
  2787. // if empty, we gotta remove it
  2788. if( empty ) {
  2789. script_array_remove_member(src, sa, i);
  2790. }
  2791. } else if( !empty ) { /* new entry */
  2792. script_array_add_member(sa,index);
  2793. // we do nothing if its empty, no point in modifying array data for a new empty member
  2794. }
  2795. } else if ( !empty ) { // we only move to create if not empty
  2796. sa = ers_alloc(array_ers, struct script_array);
  2797. sa->id = id;
  2798. sa->members = NULL;
  2799. sa->size = 0;
  2800. script_array_add_member(sa,index);
  2801. idb_put(src->arrays, id, sa);
  2802. }
  2803. }
  2804. /**
  2805. * Stores the value of a script variable
  2806. *
  2807. * @param st: current script state.
  2808. * @param sd: current character data.
  2809. * @param num: variable identifier.
  2810. * @param name: variable name.
  2811. * @param value: new value.
  2812. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2813. * @return: 0 failure, 1 success.
  2814. *
  2815. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2816. *------------------------------------------*/
  2817. int set_reg(struct script_state* st, struct map_session_data* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2818. {
  2819. char prefix = name[0];
  2820. size_t vlen = 0;
  2821. if ( !script_check_RegistryVariableLength(0,name,&vlen) )
  2822. {
  2823. 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);
  2824. return 0;
  2825. }
  2826. if( is_string_variable(name) ) {// string variable
  2827. const char *str = (const char*)value;
  2828. switch (prefix) {
  2829. case '@':
  2830. pc_setregstr(sd, num, str);
  2831. return 1;
  2832. case '$':
  2833. return mapreg_setregstr(num, str);
  2834. case '#':
  2835. return (name[1] == '#') ?
  2836. pc_setaccountreg2str(sd, num, str) :
  2837. pc_setaccountregstr(sd, num, str);
  2838. case '.':
  2839. {
  2840. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2841. if( n ) {
  2842. if (str[0]) {
  2843. i64db_put(n->vars, num, aStrdup(str));
  2844. if( script_getvaridx(num) )
  2845. script_array_update(n, num, false);
  2846. } else {
  2847. i64db_remove(n->vars, num);
  2848. if( script_getvaridx(num) )
  2849. script_array_update(n, num, true);
  2850. }
  2851. }
  2852. }
  2853. return 1;
  2854. case '\'':
  2855. {
  2856. unsigned short instance_id = script_instancegetid(st);
  2857. if( instance_id ) {
  2858. if( str[0] ) {
  2859. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2860. if( script_getvaridx(num) )
  2861. script_array_update(&instance_data[instance_id].regs, num, false);
  2862. } else {
  2863. i64db_remove(instance_data[instance_id].regs.vars, num);
  2864. if (script_getvaridx(num))
  2865. script_array_update(&instance_data[instance_id].regs, num, true);
  2866. }
  2867. } else {
  2868. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2869. script_reportsrc(st);
  2870. }
  2871. return 1;
  2872. }
  2873. default:
  2874. return pc_setglobalreg_str(sd, num, str);
  2875. }
  2876. } else {// integer variable
  2877. int val = (int)__64BPRTSIZE(value);
  2878. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2879. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2880. if( st != NULL ) {
  2881. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2882. script_reportsrc(st);
  2883. st->state = END;
  2884. }
  2885. return 0;
  2886. }
  2887. return 1;
  2888. }
  2889. switch (prefix) {
  2890. case '@':
  2891. pc_setreg(sd, num, val);
  2892. return 1;
  2893. case '$':
  2894. return mapreg_setreg(num, val);
  2895. case '#':
  2896. return (name[1] == '#') ?
  2897. pc_setaccountreg2(sd, num, val) :
  2898. pc_setaccountreg(sd, num, val);
  2899. case '.':
  2900. {
  2901. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2902. if( n ) {
  2903. if( val != 0 ) {
  2904. i64db_iput(n->vars, num, val);
  2905. if( script_getvaridx(num) )
  2906. script_array_update(n, num, false);
  2907. } else {
  2908. i64db_remove(n->vars, num);
  2909. if( script_getvaridx(num) )
  2910. script_array_update(n, num, true);
  2911. }
  2912. }
  2913. }
  2914. return 1;
  2915. case '\'':
  2916. {
  2917. unsigned short instance_id = script_instancegetid(st);
  2918. if( instance_id ) {
  2919. if( val != 0 ) {
  2920. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2921. if( script_getvaridx(num) )
  2922. script_array_update(&instance_data[instance_id].regs, num, false);
  2923. } else {
  2924. i64db_remove(instance_data[instance_id].regs.vars, num);
  2925. if (script_getvaridx(num))
  2926. script_array_update(&instance_data[instance_id].regs, num, true);
  2927. }
  2928. } else {
  2929. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2930. script_reportsrc(st);
  2931. }
  2932. return 1;
  2933. }
  2934. default:
  2935. return pc_setglobalreg(sd, num, val);
  2936. }
  2937. }
  2938. }
  2939. int set_var(struct map_session_data* sd, char* name, void* val)
  2940. {
  2941. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2942. }
  2943. void setd_sub(struct script_state *st, struct map_session_data *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2944. {
  2945. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2946. }
  2947. /**
  2948. * Converts the data to a string
  2949. * @param st
  2950. * @param data
  2951. * @param sd
  2952. */
  2953. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2954. {
  2955. char* p;
  2956. get_val_(st, data, sd);
  2957. if( data_isstring(data) )
  2958. {// nothing to convert
  2959. }
  2960. else if( data_isint(data) )
  2961. {// int -> string
  2962. CREATE(p, char, ITEM_NAME_LENGTH);
  2963. snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num);
  2964. p[ITEM_NAME_LENGTH-1] = '\0';
  2965. data->type = C_STR;
  2966. data->u.str = p;
  2967. }
  2968. else if( data_isreference(data) )
  2969. {// reference -> string
  2970. //##TODO when does this happen (check get_val) [FlavioJS]
  2971. data->type = C_CONSTSTR;
  2972. data->u.str = reference_getname(data);
  2973. }
  2974. else
  2975. {// unsupported data type
  2976. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2977. script_reportdata(data);
  2978. script_reportsrc(st);
  2979. data->type = C_CONSTSTR;
  2980. data->u.str = const_cast<char *>("");
  2981. }
  2982. return data->u.str;
  2983. }
  2984. const char* conv_str(struct script_state* st, struct script_data* data)
  2985. {
  2986. return conv_str_(st, data, NULL);
  2987. }
  2988. /**
  2989. * Converts the data to an int
  2990. * @param st
  2991. * @param data
  2992. * @param sd
  2993. */
  2994. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2995. {
  2996. get_val_(st, data, sd);
  2997. if( data_isint(data) )
  2998. {// nothing to convert
  2999. }
  3000. else if( data_isstring(data) )
  3001. {// string -> int
  3002. // the result does not overflow or underflow, it is capped instead
  3003. // ex: 999999999999 is capped to INT_MAX (2147483647)
  3004. char* p = data->u.str;
  3005. long num;
  3006. errno = 0;
  3007. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  3008. if( errno == ERANGE
  3009. #if LONG_MAX > INT_MAX
  3010. || num < INT_MIN || num > INT_MAX
  3011. #endif
  3012. )
  3013. {
  3014. if( num <= INT_MIN )
  3015. {
  3016. num = INT_MIN;
  3017. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  3018. }
  3019. else//if( num >= INT_MAX )
  3020. {
  3021. num = INT_MAX;
  3022. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  3023. }
  3024. script_reportdata(data);
  3025. script_reportsrc(st);
  3026. }
  3027. if( data->type == C_STR )
  3028. aFree(p);
  3029. data->type = C_INT;
  3030. data->u.num = (int)num;
  3031. }
  3032. #if 0
  3033. // FIXME this function is being used to retrieve the position of labels and
  3034. // probably other stuff [FlavioJS]
  3035. else
  3036. {// unsupported data type
  3037. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3038. script_reportdata(data);
  3039. script_reportsrc(st);
  3040. data->type = C_INT;
  3041. data->u.num = 0;
  3042. }
  3043. #endif
  3044. return (int)data->u.num;
  3045. }
  3046. int conv_num(struct script_state* st, struct script_data* data)
  3047. {
  3048. return conv_num_(st, data, NULL);
  3049. }
  3050. //
  3051. // Stack operations
  3052. //
  3053. /// Increases the size of the stack
  3054. void stack_expand(struct script_stack* stack)
  3055. {
  3056. stack->sp_max += 64;
  3057. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3058. stack->sp_max * sizeof(stack->stack_data[0]) );
  3059. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3060. 64 * sizeof(stack->stack_data[0]) );
  3061. }
  3062. /// Pushes a value into the stack
  3063. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  3064. /// Pushes a value into the stack (with reference)
  3065. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3066. {
  3067. if( stack->sp >= stack->sp_max )
  3068. stack_expand(stack);
  3069. stack->stack_data[stack->sp].type = type;
  3070. stack->stack_data[stack->sp].u.num = val;
  3071. stack->stack_data[stack->sp].ref = ref;
  3072. stack->sp++;
  3073. return &stack->stack_data[stack->sp-1];
  3074. }
  3075. /// Pushes a string into the stack
  3076. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3077. {
  3078. if( stack->sp >= stack->sp_max )
  3079. stack_expand(stack);
  3080. stack->stack_data[stack->sp].type = type;
  3081. stack->stack_data[stack->sp].u.str = str;
  3082. stack->stack_data[stack->sp].ref = NULL;
  3083. stack->sp++;
  3084. return &stack->stack_data[stack->sp-1];
  3085. }
  3086. /// Pushes a retinfo into the stack
  3087. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3088. {
  3089. if( stack->sp >= stack->sp_max )
  3090. stack_expand(stack);
  3091. stack->stack_data[stack->sp].type = C_RETINFO;
  3092. stack->stack_data[stack->sp].u.ri = ri;
  3093. stack->stack_data[stack->sp].ref = ref;
  3094. stack->sp++;
  3095. return &stack->stack_data[stack->sp-1];
  3096. }
  3097. /// Pushes a copy of the target position into the stack
  3098. struct script_data* push_copy(struct script_stack* stack, int pos)
  3099. {
  3100. switch( stack->stack_data[pos].type ) {
  3101. case C_CONSTSTR:
  3102. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3103. break;
  3104. case C_STR:
  3105. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3106. break;
  3107. case C_RETINFO:
  3108. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3109. exit(1);
  3110. break;
  3111. default:
  3112. return push_val2(
  3113. stack,stack->stack_data[pos].type,
  3114. stack->stack_data[pos].u.num,
  3115. stack->stack_data[pos].ref
  3116. );
  3117. break;
  3118. }
  3119. }
  3120. /// Removes the values in indexes [start,end] from the stack.
  3121. /// Adjusts all stack pointers.
  3122. void pop_stack(struct script_state* st, int start, int end)
  3123. {
  3124. struct script_stack* stack = st->stack;
  3125. struct script_data* data;
  3126. int i;
  3127. if( start < 0 )
  3128. start = 0;
  3129. if( end > stack->sp )
  3130. end = stack->sp;
  3131. if( start >= end )
  3132. return;// nothing to pop
  3133. // free stack elements
  3134. for( i = start; i < end; i++ )
  3135. {
  3136. data = &stack->stack_data[i];
  3137. if( data->type == C_STR )
  3138. aFree(data->u.str);
  3139. if( data->type == C_RETINFO ) {
  3140. struct script_retinfo* ri = data->u.ri;
  3141. if (ri->scope.vars) {
  3142. script_free_vars(ri->scope.vars);
  3143. ri->scope.vars = NULL;
  3144. }
  3145. if (ri->scope.arrays) {
  3146. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3147. ri->scope.arrays = NULL;
  3148. }
  3149. if( data->ref )
  3150. aFree(data->ref);
  3151. aFree(ri);
  3152. }
  3153. data->type = C_NOP;
  3154. }
  3155. // move the rest of the elements
  3156. if( stack->sp > end )
  3157. {
  3158. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3159. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3160. stack->stack_data[i].type = C_NOP;
  3161. }
  3162. // adjust stack pointers
  3163. if( st->start > end ){
  3164. st->start -= end - start;
  3165. }else if( st->start > start ){
  3166. st->start = start;
  3167. }
  3168. if( st->end > end ){
  3169. st->end -= end - start;
  3170. }else if( st->end > start ){
  3171. st->end = start;
  3172. }
  3173. if( stack->defsp > end ){
  3174. stack->defsp -= end - start;
  3175. }else if( stack->defsp > start ){
  3176. stack->defsp = start;
  3177. }
  3178. stack->sp -= end - start;
  3179. }
  3180. ///
  3181. ///
  3182. ///
  3183. /*==========================================
  3184. * Release script dependent variable, dependent variable of function
  3185. *------------------------------------------*/
  3186. void script_free_vars(struct DBMap* storage)
  3187. {
  3188. if( storage ) {
  3189. // destroy the storage construct containing the variables
  3190. db_destroy(storage);
  3191. }
  3192. }
  3193. void script_free_code(struct script_code* code)
  3194. {
  3195. nullpo_retv(code);
  3196. if (code->instances)
  3197. script_stop_scriptinstances(code);
  3198. script_free_vars(code->local.vars);
  3199. if (code->local.arrays)
  3200. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3201. aFree(code->script_buf);
  3202. aFree(code);
  3203. }
  3204. /// Creates a new script state.
  3205. ///
  3206. /// @param script Script code
  3207. /// @param pos Position in the code
  3208. /// @param rid Who is running the script (attached player)
  3209. /// @param oid Where the code is being run (npc 'object')
  3210. /// @return Script state
  3211. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3212. {
  3213. struct script_state* st;
  3214. st = ers_alloc(st_ers, struct script_state);
  3215. st->stack = ers_alloc(stack_ers, struct script_stack);
  3216. st->stack->sp = 0;
  3217. st->stack->sp_max = 64;
  3218. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3219. st->stack->defsp = st->stack->sp;
  3220. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3221. st->stack->scope.arrays = NULL;
  3222. st->state = RUN;
  3223. st->script = rootscript;
  3224. st->pos = pos;
  3225. st->rid = rid;
  3226. st->oid = oid;
  3227. st->sleep.timer = INVALID_TIMER;
  3228. st->npc_item_flag = battle_config.item_enabled_npc;
  3229. if( st->script->instances != USHRT_MAX )
  3230. st->script->instances++;
  3231. else {
  3232. struct npc_data *nd = map_id2nd(oid);
  3233. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3234. }
  3235. if (!st->script->local.vars)
  3236. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3237. st->id = next_id++;
  3238. active_scripts++;
  3239. idb_put(st_db, st->id, st);
  3240. return st;
  3241. }
  3242. /// Frees a script state.
  3243. ///
  3244. /// @param st Script state
  3245. void script_free_state(struct script_state* st)
  3246. {
  3247. if (idb_exists(st_db, st->id)) {
  3248. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3249. if (st->bk_st) // backup was not restored
  3250. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3251. if (sd && sd->st == st) { // Current script is aborted.
  3252. if(sd->state.using_fake_npc) {
  3253. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3254. sd->state.using_fake_npc = 0;
  3255. }
  3256. sd->st = NULL;
  3257. sd->npc_id = 0;
  3258. }
  3259. if (st->sleep.timer != INVALID_TIMER)
  3260. delete_timer(st->sleep.timer, run_script_timer);
  3261. if (st->stack) {
  3262. script_free_vars(st->stack->scope.vars);
  3263. if (st->stack->scope.arrays)
  3264. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3265. pop_stack(st, 0, st->stack->sp);
  3266. aFree(st->stack->stack_data);
  3267. ers_free(stack_ers, st->stack);
  3268. st->stack = NULL;
  3269. }
  3270. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3271. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3272. script_free_vars(st->script->local.vars);
  3273. st->script->local.vars = NULL;
  3274. }
  3275. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3276. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3277. st->script->local.arrays = NULL;
  3278. }
  3279. }
  3280. st->pos = -1;
  3281. idb_remove(st_db, st->id);
  3282. ers_free(st_ers, st);
  3283. if (--active_scripts == 0)
  3284. next_id = 0;
  3285. }
  3286. }
  3287. //
  3288. // Main execution unit
  3289. //
  3290. /*==========================================
  3291. * Read command
  3292. *------------------------------------------*/
  3293. c_op get_com(unsigned char *script,int *pos)
  3294. {
  3295. int i = 0, j = 0;
  3296. if(script[*pos]>=0x80){
  3297. return C_INT;
  3298. }
  3299. while(script[*pos]>=0x40){
  3300. i=script[(*pos)++]<<j;
  3301. j+=6;
  3302. }
  3303. return (c_op)(i+(script[(*pos)++]<<j));
  3304. }
  3305. /*==========================================
  3306. * Income figures
  3307. *------------------------------------------*/
  3308. int get_num(unsigned char *script,int *pos)
  3309. {
  3310. int i,j;
  3311. i=0; j=0;
  3312. while(script[*pos]>=0xc0){
  3313. i+=(script[(*pos)++]&0x7f)<<j;
  3314. j+=6;
  3315. }
  3316. return i+((script[(*pos)++]&0x7f)<<j);
  3317. }
  3318. /// Ternary operators
  3319. /// test ? if_true : if_false
  3320. void op_3(struct script_state* st, int op)
  3321. {
  3322. struct script_data* data;
  3323. int flag = 0;
  3324. data = script_getdatatop(st, -3);
  3325. get_val(st, data);
  3326. if( data_isstring(data) )
  3327. flag = data->u.str[0];// "" -> false
  3328. else if( data_isint(data) )
  3329. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3330. else
  3331. {
  3332. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3333. script_reportdata(data);
  3334. script_reportsrc(st);
  3335. script_removetop(st, -3, 0);
  3336. script_pushnil(st);
  3337. return;
  3338. }
  3339. if( flag )
  3340. script_pushcopytop(st, -2);
  3341. else
  3342. script_pushcopytop(st, -1);
  3343. script_removetop(st, -4, -1);
  3344. }
  3345. /// Binary string operators
  3346. /// s1 EQ s2 -> i
  3347. /// s1 NE s2 -> i
  3348. /// s1 GT s2 -> i
  3349. /// s1 GE s2 -> i
  3350. /// s1 LT s2 -> i
  3351. /// s1 LE s2 -> i
  3352. /// s1 ADD s2 -> s
  3353. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3354. {
  3355. int a = 0;
  3356. switch(op){
  3357. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3358. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3359. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3360. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3361. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3362. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3363. case C_ADD:
  3364. {
  3365. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3366. strcpy(buf, s1);
  3367. strcat(buf, s2);
  3368. script_pushstr(st, buf);
  3369. return;
  3370. }
  3371. default:
  3372. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3373. script_reportsrc(st);
  3374. script_pushnil(st);
  3375. st->state = END;
  3376. return;
  3377. }
  3378. script_pushint(st,a);
  3379. }
  3380. /// Binary number operators
  3381. /// i OP i -> i
  3382. void op_2num(struct script_state* st, int op, int i1, int i2)
  3383. {
  3384. int ret;
  3385. double ret_double;
  3386. switch( op ) {
  3387. case C_AND: ret = i1 & i2; break;
  3388. case C_OR: ret = i1 | i2; break;
  3389. case C_XOR: ret = i1 ^ i2; break;
  3390. case C_LAND: ret = (i1 && i2); break;
  3391. case C_LOR: ret = (i1 || i2); break;
  3392. case C_EQ: ret = (i1 == i2); break;
  3393. case C_NE: ret = (i1 != i2); break;
  3394. case C_GT: ret = (i1 > i2); break;
  3395. case C_GE: ret = (i1 >= i2); break;
  3396. case C_LT: ret = (i1 < i2); break;
  3397. case C_LE: ret = (i1 <= i2); break;
  3398. case C_R_SHIFT: ret = i1>>i2; break;
  3399. case C_L_SHIFT: ret = i1<<i2; break;
  3400. case C_DIV:
  3401. case C_MOD:
  3402. if( i2 == 0 )
  3403. {
  3404. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3405. script_reportsrc(st);
  3406. script_pushnil(st);
  3407. st->state = END;
  3408. return;
  3409. }
  3410. else if( op == C_DIV )
  3411. ret = i1 / i2;
  3412. else//if( op == C_MOD )
  3413. ret = i1 % i2;
  3414. break;
  3415. default:
  3416. switch( op ) {// operators that can overflow/underflow
  3417. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3418. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3419. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3420. default:
  3421. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3422. script_reportsrc(st);
  3423. script_pushnil(st);
  3424. return;
  3425. }
  3426. if( ret_double < (double)INT_MIN ) {
  3427. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3428. script_reportsrc(st);
  3429. ret = INT_MIN;
  3430. }
  3431. else if( ret_double > (double)INT_MAX ) {
  3432. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3433. script_reportsrc(st);
  3434. ret = INT_MAX;
  3435. }
  3436. }
  3437. script_pushint(st, ret);
  3438. }
  3439. /// Binary operators
  3440. void op_2(struct script_state *st, int op)
  3441. {
  3442. struct script_data* left, leftref;
  3443. struct script_data* right;
  3444. leftref.type = C_NOP;
  3445. left = script_getdatatop(st, -2);
  3446. right = script_getdatatop(st, -1);
  3447. if (st->op2ref) {
  3448. if (data_isreference(left)) {
  3449. leftref = *left;
  3450. }
  3451. st->op2ref = 0;
  3452. }
  3453. get_val(st, left);
  3454. get_val(st, right);
  3455. // automatic conversions
  3456. switch( op )
  3457. {
  3458. case C_ADD:
  3459. if( data_isint(left) && data_isstring(right) )
  3460. {// convert int-string to string-string
  3461. conv_str(st, left);
  3462. }
  3463. else if( data_isstring(left) && data_isint(right) )
  3464. {// convert string-int to string-string
  3465. conv_str(st, right);
  3466. }
  3467. break;
  3468. }
  3469. if( data_isstring(left) && data_isstring(right) )
  3470. {// ss => op_2str
  3471. op_2str(st, op, left->u.str, right->u.str);
  3472. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3473. if (leftref.type != C_NOP)
  3474. {
  3475. if (left->type == C_STR) // don't free C_CONSTSTR
  3476. aFree(left->u.str);
  3477. *left = leftref;
  3478. }
  3479. }
  3480. else if( data_isint(left) && data_isint(right) )
  3481. {// ii => op_2num
  3482. int i1 = (int)left->u.num;
  3483. int i2 = (int)right->u.num;
  3484. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3485. op_2num(st, op, i1, i2);
  3486. if (leftref.type != C_NOP)
  3487. *left = leftref;
  3488. }
  3489. else
  3490. {// invalid argument
  3491. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3492. script_reportdata(left);
  3493. script_reportdata(right);
  3494. script_reportsrc(st);
  3495. script_removetop(st, -2, 0);
  3496. script_pushnil(st);
  3497. st->state = END;
  3498. }
  3499. }
  3500. /// Unary operators
  3501. /// NEG i -> i
  3502. /// NOT i -> i
  3503. /// LNOT i -> i
  3504. void op_1(struct script_state* st, int op)
  3505. {
  3506. struct script_data* data;
  3507. int i1;
  3508. data = script_getdatatop(st, -1);
  3509. get_val(st, data);
  3510. if( !data_isint(data) )
  3511. {// not a number
  3512. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3513. script_reportdata(data);
  3514. script_reportsrc(st);
  3515. script_pushnil(st);
  3516. st->state = END;
  3517. return;
  3518. }
  3519. i1 = (int)data->u.num;
  3520. script_removetop(st, -1, 0);
  3521. switch( op )
  3522. {
  3523. case C_NEG: i1 = -i1; break;
  3524. case C_NOT: i1 = ~i1; break;
  3525. case C_LNOT: i1 = !i1; break;
  3526. default:
  3527. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3528. script_reportsrc(st);
  3529. script_pushnil(st);
  3530. st->state = END;
  3531. return;
  3532. }
  3533. script_pushint(st, i1);
  3534. }
  3535. /// Checks the type of all arguments passed to a built-in function.
  3536. ///
  3537. /// @param st Script state whose stack arguments should be inspected.
  3538. /// @param func Built-in function for which the arguments are intended.
  3539. static void script_check_buildin_argtype(struct script_state* st, int func)
  3540. {
  3541. int idx, invalid = 0;
  3542. for( idx = 2; script_hasdata(st, idx); idx++ )
  3543. {
  3544. struct script_data* data = script_getdata(st, idx);
  3545. script_function* sf = &buildin_func[str_data[func].val];
  3546. char type = sf->arg[idx-2];
  3547. if( type == '?' || type == '*' )
  3548. {// optional argument or unknown number of optional parameters ( no types are after this )
  3549. break;
  3550. }
  3551. else if( type == 0 )
  3552. {// more arguments than necessary ( should not happen, as it is checked before )
  3553. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3554. invalid++;
  3555. break;
  3556. }
  3557. else
  3558. {
  3559. const char* name = NULL;
  3560. if( data_isreference(data) )
  3561. {// get name for variables to determine the type they refer to
  3562. name = reference_getname(data);
  3563. }
  3564. switch( type )
  3565. {
  3566. case 'v':
  3567. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3568. {// variant
  3569. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3570. script_reportdata(data);
  3571. invalid++;
  3572. }
  3573. break;
  3574. case 's':
  3575. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3576. {// string
  3577. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3578. script_reportdata(data);
  3579. invalid++;
  3580. }
  3581. break;
  3582. case 'i':
  3583. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3584. {// int ( params and constants are always int )
  3585. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3586. script_reportdata(data);
  3587. invalid++;
  3588. }
  3589. break;
  3590. case 'r':
  3591. if( !data_isreference(data) )
  3592. {// variables
  3593. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3594. script_reportdata(data);
  3595. invalid++;
  3596. }
  3597. break;
  3598. case 'l':
  3599. if( !data_islabel(data) && !data_isfunclabel(data) )
  3600. {// label
  3601. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3602. script_reportdata(data);
  3603. invalid++;
  3604. }
  3605. break;
  3606. }
  3607. }
  3608. }
  3609. if(invalid)
  3610. {
  3611. ShowDebug("Function: %s\n", get_str(func));
  3612. script_reportsrc(st);
  3613. }
  3614. }
  3615. /// Executes a buildin command.
  3616. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3617. int run_func(struct script_state *st)
  3618. {
  3619. struct script_data* data;
  3620. int i,start_sp,end_sp,func;
  3621. end_sp = st->stack->sp;// position after the last argument
  3622. for( i = end_sp-1; i > 0 ; --i )
  3623. if( st->stack->stack_data[i].type == C_ARG )
  3624. break;
  3625. if( i == 0 ) {
  3626. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3627. st->state = END;
  3628. script_reportsrc(st);
  3629. return 1;
  3630. }
  3631. start_sp = i-1;// C_NAME of the command
  3632. st->start = start_sp;
  3633. st->end = end_sp;
  3634. data = &st->stack->stack_data[st->start];
  3635. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3636. func = (int)data->u.num;
  3637. st->funcname = reference_getname(data);
  3638. } else {
  3639. ShowError("script:run_func: not a buildin command.\n");
  3640. script_reportdata(data);
  3641. script_reportsrc(st);
  3642. st->state = END;
  3643. return 1;
  3644. }
  3645. if( script_config.warn_func_mismatch_argtypes ) {
  3646. script_check_buildin_argtype(st, func);
  3647. }
  3648. if(str_data[func].func) {
  3649. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3650. if( buildin_func[str_data[func].val].deprecated ){
  3651. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3652. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3653. script_reportsrc(st);
  3654. }
  3655. #endif
  3656. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3657. script_reportsrc(st);
  3658. } else {
  3659. 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));
  3660. script_reportsrc(st);
  3661. st->state = END;
  3662. }
  3663. // Stack's datum are used when re-running functions [Eoe]
  3664. if( st->state == RERUNLINE )
  3665. return 0;
  3666. pop_stack(st, st->start, st->end);
  3667. if( st->state == RETFUNC ) {// return from a user-defined function
  3668. struct script_retinfo* ri;
  3669. int olddefsp = st->stack->defsp;
  3670. int nargs;
  3671. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3672. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3673. {
  3674. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3675. script_reportsrc(st);
  3676. st->state = END;
  3677. return 1;
  3678. }
  3679. script_free_vars(st->stack->scope.vars);
  3680. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3681. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3682. nargs = ri->nargs;
  3683. st->pos = ri->pos;
  3684. st->script = ri->script;
  3685. st->stack->scope.vars = ri->scope.vars;
  3686. st->stack->scope.arrays = ri->scope.arrays;
  3687. st->stack->defsp = ri->defsp;
  3688. memset(ri, 0, sizeof(struct script_retinfo));
  3689. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3690. st->state = GOTO;
  3691. }
  3692. return 0;
  3693. }
  3694. /*==========================================
  3695. * script execution
  3696. *------------------------------------------*/
  3697. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3698. {
  3699. struct script_state *st;
  3700. if( rootscript == NULL || pos < 0 )
  3701. return;
  3702. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3703. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3704. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3705. st = script_alloc_state(rootscript, pos, rid, oid);
  3706. run_script_main(st);
  3707. }
  3708. /**
  3709. * Free all related script code
  3710. * @param code: Script code to free
  3711. */
  3712. void script_stop_scriptinstances(struct script_code *code) {
  3713. DBIterator *iter;
  3714. struct script_state* st;
  3715. if( !active_scripts )
  3716. return; // Don't even bother.
  3717. iter = db_iterator(st_db);
  3718. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) ) {
  3719. if( st->script == code )
  3720. script_free_state(st);
  3721. }
  3722. dbi_destroy(iter);
  3723. }
  3724. /*==========================================
  3725. * Timer function for sleep
  3726. *------------------------------------------*/
  3727. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3728. {
  3729. struct script_state *st = (struct script_state *)data;
  3730. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3731. // If it was a player before going to sleep and there is still a unit attached to the script
  3732. if( id != 0 && st->rid ){
  3733. struct map_session_data *sd = map_id2sd(st->rid);
  3734. // Attached player is offline(logout) or another unit type(should not happen)
  3735. if( !sd ){
  3736. st->rid = 0;
  3737. st->state = END;
  3738. // Character mismatch. Cancel execution.
  3739. }else if( sd->status.char_id != id ){
  3740. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3741. script_reportsrc(st);
  3742. st->rid = 0;
  3743. st->state = END;
  3744. }
  3745. }
  3746. while (node && st->sleep.timer != INVALID_TIMER) {
  3747. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3748. script_erase_sleepdb(node);
  3749. st->sleep.timer = INVALID_TIMER;
  3750. break;
  3751. }
  3752. node = node->next;
  3753. }
  3754. if(st->state != RERUNLINE)
  3755. st->sleep.tick = 0;
  3756. run_script_main(st);
  3757. return 0;
  3758. }
  3759. /**
  3760. * Remove sleep timers from the NPC
  3761. * @param id: NPC ID
  3762. */
  3763. void script_stop_sleeptimers(int id) {
  3764. for (;;) {
  3765. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3766. if (!st)
  3767. break; // No more sleep timers
  3768. if (st->oid == id)
  3769. script_free_state(st);
  3770. }
  3771. }
  3772. /**
  3773. * Delete the specified node from sleep_db
  3774. * @param n: Linked list of sleep timers
  3775. */
  3776. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3777. struct linkdb_node *retnode;
  3778. if (!n)
  3779. return NULL;
  3780. if (!n->prev)
  3781. sleep_db = n->next;
  3782. else
  3783. n->prev->next = n->next;
  3784. if (n->next)
  3785. n->next->prev = n->prev;
  3786. retnode = n->next;
  3787. aFree(n);
  3788. return retnode;
  3789. }
  3790. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3791. ///
  3792. /// @param st Script state to detach.
  3793. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3794. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3795. {
  3796. struct map_session_data* sd;
  3797. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3798. sd->st = st->bk_st;
  3799. sd->npc_id = st->bk_npcid;
  3800. sd->state.disable_atcommand_on_npc = 0;
  3801. if(st->bk_st) {
  3802. //Remove tag for removal.
  3803. st->bk_st = NULL;
  3804. st->bk_npcid = 0;
  3805. } else if(dequeue_event) {
  3806. #ifdef SECURE_NPCTIMEOUT
  3807. /**
  3808. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3809. **/
  3810. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3811. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3812. sd->npc_idle_timer = INVALID_TIMER;
  3813. }
  3814. #endif
  3815. npc_event_dequeue(sd);
  3816. }
  3817. }
  3818. else if(st->bk_st)
  3819. {// rid was set to 0, before detaching the script state
  3820. 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);
  3821. script_reportsrc(st->bk_st);
  3822. script_free_state(st->bk_st);
  3823. st->bk_st = NULL;
  3824. }
  3825. }
  3826. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3827. ///
  3828. /// @param st Script state to attach.
  3829. static void script_attach_state(struct script_state* st)
  3830. {
  3831. struct map_session_data* sd;
  3832. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3833. {
  3834. if(st!=sd->st)
  3835. {
  3836. if(st->bk_st)
  3837. {// there is already a backup
  3838. 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);
  3839. }
  3840. st->bk_st = sd->st;
  3841. st->bk_npcid = sd->npc_id;
  3842. }
  3843. sd->st = st;
  3844. sd->npc_id = st->oid;
  3845. sd->npc_item_flag = st->npc_item_flag; // load default.
  3846. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3847. #ifdef SECURE_NPCTIMEOUT
  3848. if( sd->npc_idle_timer == INVALID_TIMER )
  3849. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3850. sd->npc_idle_tick = gettick();
  3851. #endif
  3852. }
  3853. }
  3854. /*==========================================
  3855. * The main part of the script execution
  3856. *------------------------------------------*/
  3857. void run_script_main(struct script_state *st)
  3858. {
  3859. int cmdcount = script_config.check_cmdcount;
  3860. int gotocount = script_config.check_gotocount;
  3861. TBL_PC *sd;
  3862. struct script_stack *stack = st->stack;
  3863. script_attach_state(st);
  3864. if(st->state == RERUNLINE) {
  3865. run_func(st);
  3866. if(st->state == GOTO)
  3867. st->state = RUN;
  3868. } else if(st->state != END)
  3869. st->state = RUN;
  3870. while(st->state == RUN) {
  3871. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3872. switch(c){
  3873. case C_EOL:
  3874. if( stack->defsp > stack->sp )
  3875. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3876. else
  3877. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3878. break;
  3879. case C_INT:
  3880. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3881. break;
  3882. case C_POS:
  3883. case C_NAME:
  3884. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3885. st->pos+=3;
  3886. break;
  3887. case C_ARG:
  3888. push_val(stack,c,0);
  3889. break;
  3890. case C_STR:
  3891. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3892. while(st->script->script_buf[st->pos++]);
  3893. break;
  3894. case C_FUNC:
  3895. run_func(st);
  3896. if(st->state==GOTO){
  3897. st->state = RUN;
  3898. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3899. ShowError("script:run_script_main: infinity loop !\n");
  3900. script_reportsrc(st);
  3901. st->state=END;
  3902. }
  3903. }
  3904. break;
  3905. case C_REF:
  3906. st->op2ref = 1;
  3907. break;
  3908. case C_NEG:
  3909. case C_NOT:
  3910. case C_LNOT:
  3911. op_1(st ,c);
  3912. break;
  3913. case C_ADD:
  3914. case C_SUB:
  3915. case C_MUL:
  3916. case C_DIV:
  3917. case C_MOD:
  3918. case C_EQ:
  3919. case C_NE:
  3920. case C_GT:
  3921. case C_GE:
  3922. case C_LT:
  3923. case C_LE:
  3924. case C_AND:
  3925. case C_OR:
  3926. case C_XOR:
  3927. case C_LAND:
  3928. case C_LOR:
  3929. case C_R_SHIFT:
  3930. case C_L_SHIFT:
  3931. op_2(st, c);
  3932. break;
  3933. case C_OP3:
  3934. op_3(st, c);
  3935. break;
  3936. case C_NOP:
  3937. st->state=END;
  3938. break;
  3939. default:
  3940. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3941. st->state=END;
  3942. break;
  3943. }
  3944. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3945. ShowError("script:run_script_main: infinity loop !\n");
  3946. script_reportsrc(st);
  3947. st->state=END;
  3948. }
  3949. }
  3950. if(st->sleep.tick > 0) {
  3951. //Restore previous script
  3952. script_detach_state(st, false);
  3953. //Delay execution
  3954. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3955. st->sleep.charid = sd?sd->status.char_id:0;
  3956. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3957. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3958. } else if(st->state != END && st->rid) {
  3959. //Resume later (st is already attached to player).
  3960. if(st->bk_st) {
  3961. ShowWarning("Unable to restore stack! Double continuation!\n");
  3962. //Report BOTH scripts to see if that can help somehow.
  3963. ShowDebug("Previous script (lost):\n");
  3964. script_reportsrc(st->bk_st);
  3965. ShowDebug("Current script:\n");
  3966. script_reportsrc(st);
  3967. script_free_state(st->bk_st);
  3968. st->bk_st = NULL;
  3969. }
  3970. } else {
  3971. //Dispose of script.
  3972. if ((sd = map_id2sd(st->rid))!=NULL)
  3973. { //Restore previous stack and save char.
  3974. if(sd->state.using_fake_npc){
  3975. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3976. sd->state.using_fake_npc = 0;
  3977. }
  3978. //Restore previous script if any.
  3979. script_detach_state(st, true);
  3980. if (sd->vars_dirty)
  3981. intif_saveregistry(sd);
  3982. }
  3983. script_free_state(st);
  3984. }
  3985. }
  3986. int script_config_read(const char *cfgName)
  3987. {
  3988. int i;
  3989. char line[1024],w1[1024],w2[1024];
  3990. FILE *fp;
  3991. fp=fopen(cfgName,"r");
  3992. if(fp==NULL){
  3993. ShowError("File not found: %s\n", cfgName);
  3994. return 1;
  3995. }
  3996. while(fgets(line, sizeof(line), fp))
  3997. {
  3998. if(line[0] == '/' && line[1] == '/')
  3999. continue;
  4000. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  4001. if(i!=2)
  4002. continue;
  4003. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  4004. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  4005. }
  4006. else if(strcmpi(w1,"check_cmdcount")==0) {
  4007. script_config.check_cmdcount = config_switch(w2);
  4008. }
  4009. else if(strcmpi(w1,"check_gotocount")==0) {
  4010. script_config.check_gotocount = config_switch(w2);
  4011. }
  4012. else if(strcmpi(w1,"input_min_value")==0) {
  4013. script_config.input_min_value = config_switch(w2);
  4014. }
  4015. else if(strcmpi(w1,"input_max_value")==0) {
  4016. script_config.input_max_value = config_switch(w2);
  4017. }
  4018. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  4019. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  4020. }
  4021. else if(strcmpi(w1,"import")==0){
  4022. script_config_read(w2);
  4023. }
  4024. else {
  4025. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  4026. }
  4027. }
  4028. fclose(fp);
  4029. return 0;
  4030. }
  4031. /**
  4032. * @see DBApply
  4033. */
  4034. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  4035. {
  4036. struct script_code *code = static_cast<script_code *>(db_data2ptr(data));
  4037. if (code)
  4038. script_free_code(code);
  4039. return 0;
  4040. }
  4041. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  4042. {
  4043. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4044. if( script )
  4045. {
  4046. int j;
  4047. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4048. if( j < EQI_MAX ) {
  4049. //Single item autobonus
  4050. current_equip_item_index = sd->equip_index[j];
  4051. current_equip_combo_pos = 0;
  4052. } else {
  4053. //Combo autobonus
  4054. current_equip_item_index = -1;
  4055. current_equip_combo_pos = pos;
  4056. }
  4057. run_script(script,0,sd->bl.id,0);
  4058. }
  4059. }
  4060. void script_add_autobonus(const char *autobonus)
  4061. {
  4062. if( strdb_get(autobonus_db, autobonus) == NULL )
  4063. {
  4064. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4065. if( script )
  4066. strdb_put(autobonus_db, autobonus, script);
  4067. }
  4068. }
  4069. /// resets a temporary character array variable to given value
  4070. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  4071. {
  4072. struct script_array *sa = NULL;
  4073. struct reg_db *src = NULL;
  4074. unsigned int i, *list = NULL, size = 0;
  4075. int key;
  4076. key = add_str(varname);
  4077. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4078. return;
  4079. if( value )
  4080. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  4081. if( !(sa = static_cast<script_array *>(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */
  4082. return;
  4083. size = sa->size;
  4084. list = script_array_cpy_list(sa);
  4085. for(i = 0; i < size; i++) {
  4086. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  4087. }
  4088. }
  4089. /// sets a temporary character array variable element idx to given value
  4090. /// @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.
  4091. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  4092. {
  4093. int key;
  4094. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4095. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4096. return;
  4097. }
  4098. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4099. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  4100. if( refcache ) {
  4101. // save to avoid repeated script->add_str calls
  4102. refcache[0] = key;
  4103. }
  4104. }
  4105. /**
  4106. * Clears persistent variables from memory
  4107. **/
  4108. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4109. {
  4110. struct script_reg_state *src;
  4111. if( data->type != DB_DATA_PTR ) // got no need for those!
  4112. return 0;
  4113. src = static_cast<script_reg_state *>(db_data2ptr(data));
  4114. if( src->type ) {
  4115. struct script_reg_str *p = (struct script_reg_str *)src;
  4116. if( p->value )
  4117. aFree(p->value);
  4118. ers_free(str_reg_ers,p);
  4119. } else {
  4120. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4121. }
  4122. return 0;
  4123. }
  4124. /**
  4125. * Clears a single persistent variable
  4126. **/
  4127. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4128. {
  4129. i64db_remove(sd->regs.vars, reg);
  4130. if( data->type ) {
  4131. struct script_reg_str *p = (struct script_reg_str*)data;
  4132. if( p->value )
  4133. aFree(p->value);
  4134. ers_free(str_reg_ers,p);
  4135. } else {
  4136. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4137. }
  4138. }
  4139. unsigned int *script_array_cpy_list(struct script_array *sa)
  4140. {
  4141. if( sa->size > generic_ui_array_size )
  4142. script_generic_ui_array_expand(sa->size);
  4143. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4144. return generic_ui_array;
  4145. }
  4146. void script_generic_ui_array_expand (unsigned int plus)
  4147. {
  4148. generic_ui_array_size += plus + 100;
  4149. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4150. }
  4151. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  4152. #ifdef BETA_THREAD_TEST
  4153. /* used to receive items the queryThread has already processed */
  4154. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  4155. int i, cursor = 0;
  4156. bool allOk = true;
  4157. EnterSpinLock(&queryThreadLock);
  4158. for( i = 0; i < queryThreadData.count; i++ ) {
  4159. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4160. if( !entry->ok ) {
  4161. allOk = false;
  4162. continue;
  4163. }
  4164. run_script_main(entry->st);
  4165. entry->st = NULL;/* empty entries */
  4166. aFree(entry);
  4167. queryThreadData.entry[i] = NULL;
  4168. }
  4169. if( allOk ) {
  4170. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  4171. delete_timer(queryThreadData.timer, queryThread_timer);
  4172. queryThreadData.timer = INVALID_TIMER;
  4173. }
  4174. /* now lets clear the mess. */
  4175. for( i = 0; i < queryThreadData.count; i++ ) {
  4176. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4177. if( entry == NULL )
  4178. continue;/* entry on hold */
  4179. /* move */
  4180. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  4181. cursor++;
  4182. }
  4183. queryThreadData.count = cursor;
  4184. LeaveSpinLock(&queryThreadLock);
  4185. return 0;
  4186. }
  4187. void queryThread_add(struct script_state *st, bool type) {
  4188. int idx = 0;
  4189. struct queryThreadEntry* entry = NULL;
  4190. EnterSpinLock(&queryThreadLock);
  4191. if( queryThreadData.count++ != 0 )
  4192. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  4193. idx = queryThreadData.count-1;
  4194. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  4195. entry = queryThreadData.entry[idx];
  4196. entry->st = st;
  4197. entry->ok = false;
  4198. entry->type = type;
  4199. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  4200. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  4201. }
  4202. LeaveSpinLock(&queryThreadLock);
  4203. /* unlock the queryThread */
  4204. racond_signal(queryThreadCond);
  4205. }
  4206. /* adds a new log to the queue */
  4207. void queryThread_log(char * entry, int length) {
  4208. int idx = logThreadData.count;
  4209. EnterSpinLock(&queryThreadLock);
  4210. if( logThreadData.count++ != 0 )
  4211. RECREATE(logThreadData.entry, char* , logThreadData.count);
  4212. CREATE(logThreadData.entry[idx], char, length + 1 );
  4213. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  4214. LeaveSpinLock(&queryThreadLock);
  4215. /* unlock the queryThread */
  4216. racond_signal(queryThreadCond);
  4217. }
  4218. /* queryThread_main */
  4219. static void *queryThread_main(void *x) {
  4220. Sql *queryThread_handle = Sql_Malloc();
  4221. int i;
  4222. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4223. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4224. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4225. Sql_ShowDebug(queryThread_handle);
  4226. Sql_Free(queryThread_handle);
  4227. exit(EXIT_FAILURE);
  4228. }
  4229. if( strlen(default_codepage) > 0 )
  4230. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4231. Sql_ShowDebug(queryThread_handle);
  4232. if( log_config.sql_logs ) {
  4233. logmysql_handle = Sql_Malloc();
  4234. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4235. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4236. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4237. Sql_ShowDebug(logmysql_handle);
  4238. Sql_Free(logmysql_handle);
  4239. exit(EXIT_FAILURE);
  4240. }
  4241. if( strlen(default_codepage) > 0 )
  4242. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4243. Sql_ShowDebug(logmysql_handle);
  4244. }
  4245. while( 1 ) {
  4246. if(queryThreadTerminate > 0)
  4247. break;
  4248. EnterSpinLock(&queryThreadLock);
  4249. /* mess with queryThreadData within the lock */
  4250. for( i = 0; i < queryThreadData.count; i++ ) {
  4251. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4252. if( entry->ok )
  4253. continue;
  4254. else if ( !entry->st || !entry->st->stack ) {
  4255. entry->ok = true;/* dispose */
  4256. continue;
  4257. }
  4258. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4259. entry->ok = true;/* we're done with this */
  4260. }
  4261. /* also check for any logs in need to be sent */
  4262. if( log_config.sql_logs ) {
  4263. for( i = 0; i < logThreadData.count; i++ ) {
  4264. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4265. Sql_ShowDebug(logmysql_handle);
  4266. aFree(logThreadData.entry[i]);
  4267. }
  4268. logThreadData.count = 0;
  4269. }
  4270. LeaveSpinLock(&queryThreadLock);
  4271. ramutex_lock( queryThreadMutex );
  4272. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4273. ramutex_unlock( queryThreadMutex );
  4274. }
  4275. Sql_Free(queryThread_handle);
  4276. if( log_config.sql_logs ) {
  4277. Sql_Free(logmysql_handle);
  4278. }
  4279. return NULL;
  4280. }
  4281. #endif
  4282. /*==========================================
  4283. * Destructor
  4284. *------------------------------------------*/
  4285. void do_final_script() {
  4286. int i;
  4287. DBIterator *iter;
  4288. struct script_state *st;
  4289. #ifdef DEBUG_HASH
  4290. if (battle_config.etc_log)
  4291. {
  4292. FILE *fp = fopen("hash_dump.txt","wt");
  4293. if(fp) {
  4294. int count[SCRIPT_HASH_SIZE];
  4295. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4296. int n=0;
  4297. int min=INT_MAX,max=0,zero=0;
  4298. double mean=0.0f;
  4299. double median=0.0f;
  4300. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4301. memset(count, 0, sizeof(count));
  4302. fprintf(fp,"num : hash : data_name\n");
  4303. fprintf(fp,"---------------------------------------------------------------\n");
  4304. for(i=LABEL_START; i<str_num; i++) {
  4305. unsigned int h = calc_hash(get_str(i));
  4306. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4307. ++count[h];
  4308. }
  4309. fprintf(fp,"--------------------\n\n");
  4310. memset(count2, 0, sizeof(count2));
  4311. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4312. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4313. if(min > count[i])
  4314. min = count[i]; // minimun count of collision
  4315. if(max < count[i])
  4316. max = count[i]; // maximun count of collision
  4317. if(count[i] == 0)
  4318. zero++;
  4319. ++count2[count[i]];
  4320. }
  4321. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4322. for( i=min; i <= max; ++i ){
  4323. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4324. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4325. }
  4326. for( i=min; i <= max; ++i ){
  4327. n += count2[i];
  4328. if( n*2 >= SCRIPT_HASH_SIZE )
  4329. {
  4330. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4331. median = (i+i+1)/2.0f;
  4332. else
  4333. median = i;
  4334. break;
  4335. }
  4336. }
  4337. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4338. fclose(fp);
  4339. }
  4340. }
  4341. #endif
  4342. mapreg_final();
  4343. db_destroy(scriptlabel_db);
  4344. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4345. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4346. ers_destroy(array_ers);
  4347. if (generic_ui_array)
  4348. aFree(generic_ui_array);
  4349. iter = db_iterator(st_db);
  4350. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4351. script_free_state(st);
  4352. dbi_destroy(iter);
  4353. if (str_data)
  4354. aFree(str_data);
  4355. if (str_buf)
  4356. aFree(str_buf);
  4357. for( i = 0; i < atcmd_binding_count; i++ ) {
  4358. aFree(atcmd_binding[i]);
  4359. }
  4360. if( atcmd_binding_count != 0 )
  4361. aFree(atcmd_binding);
  4362. ers_destroy(st_ers);
  4363. ers_destroy(stack_ers);
  4364. db_destroy(st_db);
  4365. #ifdef BETA_THREAD_TEST
  4366. /* QueryThread */
  4367. InterlockedIncrement(&queryThreadTerminate);
  4368. racond_signal(queryThreadCond);
  4369. rathread_wait(queryThread, NULL);
  4370. // Destroy cond var and mutex.
  4371. racond_destroy( queryThreadCond );
  4372. ramutex_destroy( queryThreadMutex );
  4373. /* Clear missing vars */
  4374. for( i = 0; i < queryThreadData.count; i++ ) {
  4375. aFree(queryThreadData.entry[i]);
  4376. }
  4377. aFree(queryThreadData.entry);
  4378. for( i = 0; i < logThreadData.count; i++ ) {
  4379. aFree(logThreadData.entry[i]);
  4380. }
  4381. aFree(logThreadData.entry);
  4382. #endif
  4383. }
  4384. /*==========================================
  4385. * Initialization
  4386. *------------------------------------------*/
  4387. void do_init_script(void) {
  4388. st_db = idb_alloc(DB_OPT_BASE);
  4389. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4390. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4391. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4392. st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS);
  4393. stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK);
  4394. array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS);
  4395. ers_chunk_size(st_ers, 10);
  4396. ers_chunk_size(stack_ers, 10);
  4397. active_scripts = 0;
  4398. next_id = 0;
  4399. mapreg_init();
  4400. #ifdef BETA_THREAD_TEST
  4401. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4402. queryThreadData.count = 0;
  4403. CREATE(logThreadData.entry, char *, 1);
  4404. logThreadData.count = 0;
  4405. /* QueryThread Start */
  4406. InitializeSpinLock(&queryThreadLock);
  4407. queryThreadData.timer = INVALID_TIMER;
  4408. queryThreadTerminate = 0;
  4409. queryThreadMutex = ramutex_create();
  4410. queryThreadCond = racond_create();
  4411. queryThread = rathread_create(queryThread_main, NULL);
  4412. if(queryThread == NULL){
  4413. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4414. exit(EXIT_FAILURE);
  4415. }
  4416. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4417. #endif
  4418. }
  4419. void script_reload(void) {
  4420. int i;
  4421. DBIterator *iter;
  4422. struct script_state *st;
  4423. #ifdef BETA_THREAD_TEST
  4424. /* we're reloading so any queries undergoing should be...exterminated. */
  4425. EnterSpinLock(&queryThreadLock);
  4426. for( i = 0; i < queryThreadData.count; i++ ) {
  4427. aFree(queryThreadData.entry[i]);
  4428. }
  4429. queryThreadData.count = 0;
  4430. if( queryThreadData.timer != INVALID_TIMER ) {
  4431. delete_timer(queryThreadData.timer, queryThread_timer);
  4432. queryThreadData.timer = INVALID_TIMER;
  4433. }
  4434. LeaveSpinLock(&queryThreadLock);
  4435. #endif
  4436. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4437. db_clear(scriptlabel_db);
  4438. // @commands (script based)
  4439. // Clear bindings
  4440. for( i = 0; i < atcmd_binding_count; i++ ) {
  4441. aFree(atcmd_binding[i]);
  4442. }
  4443. if( atcmd_binding_count != 0 )
  4444. aFree(atcmd_binding);
  4445. atcmd_binding_count = 0;
  4446. iter = db_iterator(st_db);
  4447. for( st = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); st = static_cast<script_state *>(dbi_next(iter)) )
  4448. script_free_state(st);
  4449. dbi_destroy(iter);
  4450. db_clear(st_db);
  4451. mapreg_reload();
  4452. }
  4453. //-----------------------------------------------------------------------------
  4454. // buildin functions
  4455. //
  4456. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4457. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4458. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4459. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4460. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4461. /////////////////////////////////////////////////////////////////////
  4462. // NPC interaction
  4463. //
  4464. /// Appends a message to the npc dialog.
  4465. /// If a dialog doesn't exist yet, one is created.
  4466. ///
  4467. /// mes "<message>";
  4468. BUILDIN_FUNC(mes)
  4469. {
  4470. TBL_PC* sd;
  4471. if( !script_rid2sd(sd) )
  4472. return SCRIPT_CMD_SUCCESS;
  4473. if( !script_hasdata(st, 3) )
  4474. {// only a single line detected in the script
  4475. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4476. }
  4477. else
  4478. {// parse multiple lines as they exist
  4479. int i;
  4480. for( i = 2; script_hasdata(st, i); i++ )
  4481. {
  4482. // send the message to the client
  4483. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4484. }
  4485. }
  4486. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4487. return SCRIPT_CMD_SUCCESS;
  4488. }
  4489. /// Displays the button 'next' in the npc dialog.
  4490. /// The dialog text is cleared and the script continues when the button is pressed.
  4491. ///
  4492. /// next;
  4493. BUILDIN_FUNC(next)
  4494. {
  4495. TBL_PC* sd;
  4496. if( !script_rid2sd(sd) )
  4497. return SCRIPT_CMD_SUCCESS;
  4498. #ifdef SECURE_NPCTIMEOUT
  4499. sd->npc_idle_type = NPCT_WAIT;
  4500. #endif
  4501. st->state = STOP;
  4502. clif_scriptnext(sd, st->oid);
  4503. return SCRIPT_CMD_SUCCESS;
  4504. }
  4505. /// Clears the dialog and continues the script without a next button.
  4506. ///
  4507. /// clear;
  4508. BUILDIN_FUNC(clear)
  4509. {
  4510. TBL_PC* sd;
  4511. if (!script_rid2sd(sd))
  4512. return SCRIPT_CMD_FAILURE;
  4513. clif_scriptclear(sd, st->oid);
  4514. return SCRIPT_CMD_SUCCESS;
  4515. }
  4516. /// Ends the script and displays the button 'close' on the npc dialog.
  4517. /// The dialog is closed when the button is pressed.
  4518. ///
  4519. /// close;
  4520. BUILDIN_FUNC(close)
  4521. {
  4522. TBL_PC* sd;
  4523. if( !script_rid2sd(sd) )
  4524. return SCRIPT_CMD_SUCCESS;
  4525. if( !st->mes_active ) {
  4526. TBL_NPC* nd = map_id2nd(st->oid);
  4527. st->state = END; // Keep backwards compatibility.
  4528. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4529. } else {
  4530. st->state = CLOSE;
  4531. st->mes_active = 0;
  4532. }
  4533. clif_scriptclose(sd, st->oid);
  4534. return SCRIPT_CMD_SUCCESS;
  4535. }
  4536. /// Displays the button 'close' on the npc dialog.
  4537. /// The dialog is closed and the script continues when the button is pressed.
  4538. ///
  4539. /// close2;
  4540. BUILDIN_FUNC(close2)
  4541. {
  4542. TBL_PC* sd;
  4543. if( !script_rid2sd(sd) )
  4544. return SCRIPT_CMD_SUCCESS;
  4545. st->state = STOP;
  4546. if( st->mes_active )
  4547. st->mes_active = 0;
  4548. clif_scriptclose(sd, st->oid);
  4549. return SCRIPT_CMD_SUCCESS;
  4550. }
  4551. /// Counts the number of valid and total number of options in 'str'
  4552. /// If max_count > 0 the counting stops when that valid option is reached
  4553. /// total is incremented for each option (NULL is supported)
  4554. static int menu_countoptions(const char* str, int max_count, int* total)
  4555. {
  4556. int count = 0;
  4557. int bogus_total;
  4558. if( total == NULL )
  4559. total = &bogus_total;
  4560. ++(*total);
  4561. // initial empty options
  4562. while( *str == ':' )
  4563. {
  4564. ++str;
  4565. ++(*total);
  4566. }
  4567. // count menu options
  4568. while( *str != '\0' )
  4569. {
  4570. ++count;
  4571. --max_count;
  4572. if( max_count == 0 )
  4573. break;
  4574. while( *str != ':' && *str != '\0' )
  4575. ++str;
  4576. while( *str == ':' )
  4577. {
  4578. ++str;
  4579. ++(*total);
  4580. }
  4581. }
  4582. return count;
  4583. }
  4584. /// Displays a menu with options and goes to the target label.
  4585. /// The script is stopped if cancel is pressed.
  4586. /// Options with no text are not displayed in the client.
  4587. ///
  4588. /// Options can be grouped together, separated by the character ':' in the text:
  4589. /// ex: menu "A:B:C",L_target;
  4590. /// All these options go to the specified target label.
  4591. ///
  4592. /// The index of the selected option is put in the variable @menu.
  4593. /// Indexes start with 1 and are consistent with grouped and empty options.
  4594. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4595. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4596. ///
  4597. /// NOTE: the client closes the npc dialog when cancel is pressed
  4598. ///
  4599. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4600. BUILDIN_FUNC(menu)
  4601. {
  4602. int i;
  4603. const char* text;
  4604. TBL_PC* sd;
  4605. if( !script_rid2sd(sd) )
  4606. return SCRIPT_CMD_SUCCESS;
  4607. #ifdef SECURE_NPCTIMEOUT
  4608. sd->npc_idle_type = NPCT_MENU;
  4609. #endif
  4610. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4611. if( sd->state.menu_or_input == 0 )
  4612. {
  4613. struct StringBuf buf;
  4614. if( script_lastdata(st) % 2 == 0 )
  4615. {// argument count is not even (1st argument is at index 2)
  4616. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4617. st->state = END;
  4618. return SCRIPT_CMD_FAILURE;
  4619. }
  4620. StringBuf_Init(&buf);
  4621. sd->npc_menu = 0;
  4622. for( i = 2; i < script_lastdata(st); i += 2 )
  4623. {
  4624. struct script_data* data;
  4625. // menu options
  4626. text = script_getstr(st, i);
  4627. // target label
  4628. data = script_getdata(st, i+1);
  4629. if( !data_islabel(data) )
  4630. {// not a label
  4631. StringBuf_Destroy(&buf);
  4632. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4633. script_reportdata(data);
  4634. st->state = END;
  4635. return SCRIPT_CMD_FAILURE;
  4636. }
  4637. // append option(s)
  4638. if( text[0] == '\0' )
  4639. continue;// empty string, ignore
  4640. if( sd->npc_menu > 0 )
  4641. StringBuf_AppendStr(&buf, ":");
  4642. StringBuf_AppendStr(&buf, text);
  4643. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4644. }
  4645. st->state = RERUNLINE;
  4646. sd->state.menu_or_input = 1;
  4647. /**
  4648. * menus beyond this length crash the client (see bugreport:6402)
  4649. **/
  4650. if( StringBuf_Length(&buf) >= 2047 ) {
  4651. struct npc_data * nd = map_id2nd(st->oid);
  4652. char* menu;
  4653. CREATE(menu, char, 2048);
  4654. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4655. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4656. clif_scriptmenu(sd, st->oid, menu);
  4657. aFree(menu);
  4658. } else
  4659. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4660. StringBuf_Destroy(&buf);
  4661. if( sd->npc_menu >= 0xff )
  4662. {// 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
  4663. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4664. script_reportsrc(st);
  4665. }
  4666. }
  4667. else if( sd->npc_menu == 0xff )
  4668. {// Cancel was pressed
  4669. sd->state.menu_or_input = 0;
  4670. st->state = END;
  4671. }
  4672. else
  4673. {// goto target label
  4674. int menu = 0;
  4675. sd->state.menu_or_input = 0;
  4676. if( sd->npc_menu <= 0 )
  4677. {
  4678. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4679. st->state = END;
  4680. return SCRIPT_CMD_FAILURE;
  4681. }
  4682. // get target label
  4683. for( i = 2; i < script_lastdata(st); i += 2 )
  4684. {
  4685. text = script_getstr(st, i);
  4686. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4687. if( sd->npc_menu <= 0 )
  4688. break;// entry found
  4689. }
  4690. if( sd->npc_menu > 0 )
  4691. {// Invalid selection
  4692. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4693. st->state = END;
  4694. return SCRIPT_CMD_FAILURE;
  4695. }
  4696. if( !data_islabel(script_getdata(st, i + 1)) )
  4697. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4698. ShowError("buildin_menu: Unexpected data in label argument\n");
  4699. script_reportdata(script_getdata(st, i + 1));
  4700. st->state = END;
  4701. return SCRIPT_CMD_FAILURE;
  4702. }
  4703. pc_setreg(sd, add_str("@menu"), menu);
  4704. st->pos = script_getnum(st, i + 1);
  4705. st->state = GOTO;
  4706. }
  4707. return SCRIPT_CMD_SUCCESS;
  4708. }
  4709. /// Displays a menu with options and returns the selected option.
  4710. /// Behaves like 'menu' without the target labels.
  4711. ///
  4712. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4713. ///
  4714. /// @see menu
  4715. BUILDIN_FUNC(select)
  4716. {
  4717. int i;
  4718. const char* text;
  4719. TBL_PC* sd;
  4720. if( !script_rid2sd(sd) )
  4721. return SCRIPT_CMD_SUCCESS;
  4722. #ifdef SECURE_NPCTIMEOUT
  4723. sd->npc_idle_type = NPCT_MENU;
  4724. #endif
  4725. if( sd->state.menu_or_input == 0 ) {
  4726. struct StringBuf buf;
  4727. StringBuf_Init(&buf);
  4728. sd->npc_menu = 0;
  4729. for( i = 2; i <= script_lastdata(st); ++i ) {
  4730. text = script_getstr(st, i);
  4731. if( sd->npc_menu > 0 )
  4732. StringBuf_AppendStr(&buf, ":");
  4733. StringBuf_AppendStr(&buf, text);
  4734. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4735. }
  4736. st->state = RERUNLINE;
  4737. sd->state.menu_or_input = 1;
  4738. /**
  4739. * menus beyond this length crash the client (see bugreport:6402)
  4740. **/
  4741. if( StringBuf_Length(&buf) >= 2047 ) {
  4742. struct npc_data * nd = map_id2nd(st->oid);
  4743. char* menu;
  4744. CREATE(menu, char, 2048);
  4745. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4746. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4747. clif_scriptmenu(sd, st->oid, menu);
  4748. aFree(menu);
  4749. } else
  4750. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4751. StringBuf_Destroy(&buf);
  4752. if( sd->npc_menu >= 0xff ) {
  4753. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4754. script_reportsrc(st);
  4755. }
  4756. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4757. sd->state.menu_or_input = 0;
  4758. st->state = END;
  4759. } else {// return selected option
  4760. int menu = 0;
  4761. sd->state.menu_or_input = 0;
  4762. for( i = 2; i <= script_lastdata(st); ++i ) {
  4763. text = script_getstr(st, i);
  4764. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4765. if( sd->npc_menu <= 0 )
  4766. break;// entry found
  4767. }
  4768. pc_setreg(sd, add_str("@menu"), menu);
  4769. script_pushint(st, menu);
  4770. st->state = RUN;
  4771. }
  4772. return SCRIPT_CMD_SUCCESS;
  4773. }
  4774. /// Displays a menu with options and returns the selected option.
  4775. /// Behaves like 'menu' without the target labels, except when cancel is
  4776. /// pressed.
  4777. /// When cancel is pressed, the script continues and 255 is returned.
  4778. ///
  4779. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4780. ///
  4781. /// @see menu
  4782. BUILDIN_FUNC(prompt)
  4783. {
  4784. int i;
  4785. const char *text;
  4786. TBL_PC* sd;
  4787. if( !script_rid2sd(sd) )
  4788. return SCRIPT_CMD_SUCCESS;
  4789. #ifdef SECURE_NPCTIMEOUT
  4790. sd->npc_idle_type = NPCT_MENU;
  4791. #endif
  4792. if( sd->state.menu_or_input == 0 )
  4793. {
  4794. struct StringBuf buf;
  4795. StringBuf_Init(&buf);
  4796. sd->npc_menu = 0;
  4797. for( i = 2; i <= script_lastdata(st); ++i )
  4798. {
  4799. text = script_getstr(st, i);
  4800. if( sd->npc_menu > 0 )
  4801. StringBuf_AppendStr(&buf, ":");
  4802. StringBuf_AppendStr(&buf, text);
  4803. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4804. }
  4805. st->state = RERUNLINE;
  4806. sd->state.menu_or_input = 1;
  4807. /**
  4808. * menus beyond this length crash the client (see bugreport:6402)
  4809. **/
  4810. if( StringBuf_Length(&buf) >= 2047 ) {
  4811. struct npc_data * nd = map_id2nd(st->oid);
  4812. char* menu;
  4813. CREATE(menu, char, 2048);
  4814. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4815. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4816. clif_scriptmenu(sd, st->oid, menu);
  4817. aFree(menu);
  4818. } else
  4819. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4820. StringBuf_Destroy(&buf);
  4821. if( sd->npc_menu >= 0xff )
  4822. {
  4823. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4824. script_reportsrc(st);
  4825. }
  4826. }
  4827. else if( sd->npc_menu == 0xff )
  4828. {// Cancel was pressed
  4829. sd->state.menu_or_input = 0;
  4830. pc_setreg(sd, add_str("@menu"), 0xff);
  4831. script_pushint(st, 0xff);
  4832. st->state = RUN;
  4833. }
  4834. else
  4835. {// return selected option
  4836. int menu = 0;
  4837. sd->state.menu_or_input = 0;
  4838. for( i = 2; i <= script_lastdata(st); ++i )
  4839. {
  4840. text = script_getstr(st, i);
  4841. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4842. if( sd->npc_menu <= 0 )
  4843. break;// entry found
  4844. }
  4845. pc_setreg(sd, add_str("@menu"), menu);
  4846. script_pushint(st, menu);
  4847. st->state = RUN;
  4848. }
  4849. return SCRIPT_CMD_SUCCESS;
  4850. }
  4851. /////////////////////////////////////////////////////////////////////
  4852. // ...
  4853. //
  4854. /// Jumps to the target script label.
  4855. ///
  4856. /// goto <label>;
  4857. BUILDIN_FUNC(goto)
  4858. {
  4859. if( !data_islabel(script_getdata(st,2)) )
  4860. {
  4861. ShowError("buildin_goto: Not a label\n");
  4862. script_reportdata(script_getdata(st,2));
  4863. st->state = END;
  4864. return SCRIPT_CMD_FAILURE;
  4865. }
  4866. st->pos = script_getnum(st,2);
  4867. st->state = GOTO;
  4868. return SCRIPT_CMD_SUCCESS;
  4869. }
  4870. /*==========================================
  4871. * user-defined function call
  4872. *------------------------------------------*/
  4873. BUILDIN_FUNC(callfunc)
  4874. {
  4875. int i, j;
  4876. struct script_retinfo* ri;
  4877. struct script_code* scr;
  4878. const char* str = script_getstr(st,2);
  4879. struct reg_db *ref = NULL;
  4880. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4881. if(!scr) {
  4882. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4883. st->state = END;
  4884. return SCRIPT_CMD_FAILURE;
  4885. }
  4886. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4887. ref[0].vars = st->stack->scope.vars;
  4888. if (!st->stack->scope.arrays)
  4889. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4890. ref[0].arrays = st->stack->scope.arrays;
  4891. ref[1].vars = st->script->local.vars;
  4892. if (!st->script->local.arrays)
  4893. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4894. ref[1].arrays = st->script->local.arrays;
  4895. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4896. struct script_data* data = push_copy(st->stack,i);
  4897. if (data_isreference(data) && !data->ref) {
  4898. const char* name = reference_getname(data);
  4899. if (name[0] == '.')
  4900. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4901. }
  4902. }
  4903. CREATE(ri, struct script_retinfo, 1);
  4904. ri->script = st->script; // script code
  4905. ri->scope.vars = st->stack->scope.vars; // scope variables
  4906. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4907. ri->pos = st->pos; // script location
  4908. ri->nargs = j; // argument count
  4909. ri->defsp = st->stack->defsp; // default stack pointer
  4910. push_retinfo(st->stack, ri, ref);
  4911. st->pos = 0;
  4912. st->script = scr;
  4913. st->stack->defsp = st->stack->sp;
  4914. st->state = GOTO;
  4915. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4916. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4917. if (!st->script->local.vars)
  4918. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4919. return SCRIPT_CMD_SUCCESS;
  4920. }
  4921. /*==========================================
  4922. * subroutine call
  4923. *------------------------------------------*/
  4924. BUILDIN_FUNC(callsub)
  4925. {
  4926. int i,j;
  4927. struct script_retinfo* ri;
  4928. int pos = script_getnum(st,2);
  4929. struct reg_db *ref = NULL;
  4930. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4931. ShowError("buildin_callsub: Argument is not a label\n");
  4932. script_reportdata(script_getdata(st,2));
  4933. st->state = END;
  4934. return SCRIPT_CMD_FAILURE;
  4935. }
  4936. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4937. ref[0].vars = st->stack->scope.vars;
  4938. if (!st->stack->scope.arrays)
  4939. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4940. ref[0].arrays = st->stack->scope.arrays;
  4941. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4942. struct script_data* data = push_copy(st->stack,i);
  4943. if (data_isreference(data) && !data->ref) {
  4944. const char* name = reference_getname(data);
  4945. if (name[0] == '.' && name[1] == '@')
  4946. data->ref = &ref[0];
  4947. }
  4948. }
  4949. CREATE(ri, struct script_retinfo, 1);
  4950. ri->script = st->script; // script code
  4951. ri->scope.vars = st->stack->scope.vars; // scope variables
  4952. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4953. ri->pos = st->pos; // script location
  4954. ri->nargs = j; // argument count
  4955. ri->defsp = st->stack->defsp; // default stack pointer
  4956. push_retinfo(st->stack, ri, ref);
  4957. st->pos = pos;
  4958. st->stack->defsp = st->stack->sp;
  4959. st->state = GOTO;
  4960. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4961. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4962. return SCRIPT_CMD_SUCCESS;
  4963. }
  4964. /// Retrieves an argument provided to callfunc/callsub.
  4965. /// If the argument doesn't exist
  4966. ///
  4967. /// getarg(<index>{,<default_value>}) -> <value>
  4968. BUILDIN_FUNC(getarg)
  4969. {
  4970. struct script_retinfo* ri;
  4971. int idx;
  4972. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4973. {
  4974. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4975. st->state = END;
  4976. return SCRIPT_CMD_FAILURE;
  4977. }
  4978. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4979. idx = script_getnum(st,2);
  4980. if( idx >= 0 && idx < ri->nargs )
  4981. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4982. else if( script_hasdata(st,3) )
  4983. script_pushcopy(st, 3);
  4984. else
  4985. {
  4986. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4987. st->state = END;
  4988. return SCRIPT_CMD_FAILURE;
  4989. }
  4990. return SCRIPT_CMD_SUCCESS;
  4991. }
  4992. /// Returns from the current function, optionaly returning a value from the functions.
  4993. /// Don't use outside script functions.
  4994. ///
  4995. /// return;
  4996. /// return <value>;
  4997. BUILDIN_FUNC(return)
  4998. {
  4999. if( script_hasdata(st,2) )
  5000. {// return value
  5001. struct script_data* data;
  5002. script_pushcopy(st, 2);
  5003. data = script_getdatatop(st, -1);
  5004. if( data_isreference(data) ) {
  5005. const char* name = reference_getname(data);
  5006. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  5007. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  5008. get_val(st, data); // current scope, convert to value
  5009. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  5010. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  5011. }
  5012. }
  5013. }
  5014. else
  5015. {// no return value
  5016. script_pushnil(st);
  5017. }
  5018. st->state = RETFUNC;
  5019. return SCRIPT_CMD_SUCCESS;
  5020. }
  5021. /// Returns a random number from 0 to <range>-1.
  5022. /// Or returns a random number from <min> to <max>.
  5023. /// If <min> is greater than <max>, their numbers are switched.
  5024. /// rand(<range>) -> <int>
  5025. /// rand(<min>,<max>) -> <int>
  5026. BUILDIN_FUNC(rand)
  5027. {
  5028. int range;
  5029. int min;
  5030. if( script_hasdata(st,3) )
  5031. {// min,max
  5032. int max = script_getnum(st,3);
  5033. min = script_getnum(st,2);
  5034. if( max < min )
  5035. SWAP(min, max);
  5036. range = max - min + 1;
  5037. }
  5038. else
  5039. {// range
  5040. min = 0;
  5041. range = script_getnum(st,2);
  5042. }
  5043. if( range <= 1 )
  5044. script_pushint(st, min);
  5045. else
  5046. script_pushint(st, rnd()%range + min);
  5047. return SCRIPT_CMD_SUCCESS;
  5048. }
  5049. /*==========================================
  5050. * Warp sd to str,x,y or Random or SavePoint/Save
  5051. *------------------------------------------*/
  5052. BUILDIN_FUNC(warp)
  5053. {
  5054. int ret;
  5055. int x,y;
  5056. const char* str;
  5057. struct map_session_data* sd;
  5058. if(!script_charid2sd(5, sd))
  5059. return SCRIPT_CMD_SUCCESS;
  5060. str = script_getstr(st,2);
  5061. x = script_getnum(st,3);
  5062. y = script_getnum(st,4);
  5063. if(strcmp(str,"Random")==0)
  5064. ret = pc_randomwarp(sd,CLR_TELEPORT);
  5065. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  5066. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5067. else
  5068. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  5069. if( ret ) {
  5070. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  5071. return SCRIPT_CMD_FAILURE;
  5072. }
  5073. return SCRIPT_CMD_SUCCESS;
  5074. }
  5075. /*==========================================
  5076. * Warp a specified area
  5077. *------------------------------------------*/
  5078. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  5079. {
  5080. int x2,y2,x3,y3;
  5081. unsigned int index;
  5082. index = va_arg(ap,unsigned int);
  5083. x2 = va_arg(ap,int);
  5084. y2 = va_arg(ap,int);
  5085. x3 = va_arg(ap,int);
  5086. y3 = va_arg(ap,int);
  5087. if(index == 0)
  5088. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  5089. else if(x3 && y3) {
  5090. int max, tx, ty, j = 0;
  5091. int16 m;
  5092. m = map_mapindex2mapid(index);
  5093. // choose a suitable max number of attempts
  5094. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  5095. max = 1000;
  5096. // find a suitable map cell
  5097. do {
  5098. tx = rnd()%(x3-x2+1)+x2;
  5099. ty = rnd()%(y3-y2+1)+y2;
  5100. j++;
  5101. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  5102. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  5103. }
  5104. else
  5105. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  5106. return 0;
  5107. }
  5108. BUILDIN_FUNC(areawarp)
  5109. {
  5110. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  5111. unsigned int index;
  5112. const char *str;
  5113. const char *mapname;
  5114. mapname = script_getstr(st,2);
  5115. x0 = script_getnum(st,3);
  5116. y0 = script_getnum(st,4);
  5117. x1 = script_getnum(st,5);
  5118. y1 = script_getnum(st,6);
  5119. str = script_getstr(st,7);
  5120. x2 = script_getnum(st,8);
  5121. y2 = script_getnum(st,9);
  5122. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  5123. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  5124. x3 = 0;
  5125. y3 = 0;
  5126. } else if( x3 && y3 ) {
  5127. // normalize x3/y3 coordinates
  5128. if( x3 < x2 ) SWAP(x3,x2);
  5129. if( y3 < y2 ) SWAP(y3,y2);
  5130. }
  5131. }
  5132. if( (m = map_mapname2mapid(mapname)) < 0 )
  5133. return SCRIPT_CMD_FAILURE;
  5134. if( strcmp(str,"Random") == 0 )
  5135. index = 0;
  5136. else if( !(index=mapindex_name2id(str)) )
  5137. return SCRIPT_CMD_FAILURE;
  5138. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  5139. return SCRIPT_CMD_SUCCESS;
  5140. }
  5141. /*==========================================
  5142. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  5143. *------------------------------------------*/
  5144. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  5145. {
  5146. int hp, sp;
  5147. hp = va_arg(ap, int);
  5148. sp = va_arg(ap, int);
  5149. pc_percentheal((TBL_PC *)bl,hp,sp);
  5150. return 0;
  5151. }
  5152. BUILDIN_FUNC(areapercentheal)
  5153. {
  5154. int hp,sp,m;
  5155. const char *mapname;
  5156. int x0,y0,x1,y1;
  5157. mapname=script_getstr(st,2);
  5158. x0=script_getnum(st,3);
  5159. y0=script_getnum(st,4);
  5160. x1=script_getnum(st,5);
  5161. y1=script_getnum(st,6);
  5162. hp=script_getnum(st,7);
  5163. sp=script_getnum(st,8);
  5164. if( (m=map_mapname2mapid(mapname))< 0)
  5165. return SCRIPT_CMD_FAILURE;
  5166. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5167. return SCRIPT_CMD_SUCCESS;
  5168. }
  5169. /*==========================================
  5170. * Warpparty - [Fredzilla] [Paradox924X]
  5171. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5172. * If 'from_mapname' is specified, only the party members on that map will be warped
  5173. *------------------------------------------*/
  5174. BUILDIN_FUNC(warpparty)
  5175. {
  5176. TBL_PC *sd = NULL;
  5177. TBL_PC *pl_sd;
  5178. struct party_data* p;
  5179. int type, mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5180. const char* str = script_getstr(st,2);
  5181. int x = script_getnum(st,3);
  5182. int y = script_getnum(st,4);
  5183. int p_id = script_getnum(st,5);
  5184. const char* str2 = NULL;
  5185. if ( script_hasdata(st,6) )
  5186. str2 = script_getstr(st,6);
  5187. if (script_hasdata(st, 7))
  5188. rx = script_getnum(st, 7);
  5189. if (script_hasdata(st, 8))
  5190. ry = script_getnum(st, 8);
  5191. p = party_search(p_id);
  5192. if(!p)
  5193. return SCRIPT_CMD_SUCCESS;
  5194. type = ( strcmp(str,"Random")==0 ) ? 0
  5195. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5196. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5197. : ( strcmp(str,"Leader")==0 ) ? 3
  5198. : 4;
  5199. switch (type)
  5200. {
  5201. case 3:
  5202. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5203. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5204. return SCRIPT_CMD_FAILURE;
  5205. pl_sd = p->data[i].sd;
  5206. mapindex = pl_sd->mapindex;
  5207. m = map_mapindex2mapid(mapindex);
  5208. x = pl_sd->bl.x;
  5209. y = pl_sd->bl.y;
  5210. break;
  5211. case 4:
  5212. mapindex = mapindex_name2id(str);
  5213. if (!mapindex) {// Invalid map
  5214. return SCRIPT_CMD_FAILURE;
  5215. }
  5216. m = map_mapindex2mapid(mapindex);
  5217. break;
  5218. case 2:
  5219. //"SavePoint" uses save point of the currently attached player
  5220. if ( !script_rid2sd(sd) )
  5221. return SCRIPT_CMD_SUCCESS;
  5222. break;
  5223. }
  5224. for (i = 0; i < MAX_PARTY; i++)
  5225. {
  5226. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5227. continue;
  5228. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5229. continue;
  5230. if( pc_isdead(pl_sd) )
  5231. continue;
  5232. switch( type )
  5233. {
  5234. case 0: // Random
  5235. if(!map[pl_sd->bl.m].flag.nowarp)
  5236. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5237. break;
  5238. case 1: // SavePointAll
  5239. if(!map[pl_sd->bl.m].flag.noreturn)
  5240. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5241. break;
  5242. case 2: // SavePoint
  5243. if(!map[pl_sd->bl.m].flag.noreturn)
  5244. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5245. break;
  5246. case 3: // Leader
  5247. if (p->party.member[i].leader)
  5248. continue;
  5249. case 4: // m,x,y
  5250. if (rx || ry) {
  5251. int x1 = x + rx, y1 = y + ry,
  5252. x0 = x - rx, y0 = y - ry;
  5253. uint8 attempts = 10;
  5254. do {
  5255. x = x0 + rnd()%(x1 - x0 + 1);
  5256. y = y0 + rnd()%(y1 - y0 + 1);
  5257. } while ((--attempts) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  5258. }
  5259. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pl_sd->group_level))
  5260. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5261. break;
  5262. }
  5263. }
  5264. return SCRIPT_CMD_SUCCESS;
  5265. }
  5266. /*==========================================
  5267. * Warpguild - [Fredzilla]
  5268. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5269. *------------------------------------------*/
  5270. BUILDIN_FUNC(warpguild)
  5271. {
  5272. TBL_PC *sd = NULL;
  5273. TBL_PC *pl_sd;
  5274. struct guild* g;
  5275. struct s_mapiterator* iter;
  5276. int type, mapindex = 0, m = -1;
  5277. const char* str = script_getstr(st,2);
  5278. int x = script_getnum(st,3);
  5279. int y = script_getnum(st,4);
  5280. int gid = script_getnum(st,5);
  5281. g = guild_search(gid);
  5282. if( g == NULL )
  5283. return SCRIPT_CMD_SUCCESS;
  5284. type = ( strcmp(str,"Random")==0 ) ? 0
  5285. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5286. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5287. : 3;
  5288. if( type == 2 && !script_rid2sd(sd) )
  5289. {// "SavePoint" uses save point of the currently attached player
  5290. return SCRIPT_CMD_SUCCESS;
  5291. }
  5292. switch (type) {
  5293. case 3:
  5294. mapindex = mapindex_name2id(str);
  5295. if (!mapindex)
  5296. return SCRIPT_CMD_FAILURE;
  5297. m = map_mapindex2mapid(mapindex);
  5298. break;
  5299. }
  5300. iter = mapit_getallusers();
  5301. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5302. {
  5303. if( pl_sd->status.guild_id != gid )
  5304. continue;
  5305. switch( type )
  5306. {
  5307. case 0: // Random
  5308. if(!map[pl_sd->bl.m].flag.nowarp)
  5309. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5310. break;
  5311. case 1: // SavePointAll
  5312. if(!map[pl_sd->bl.m].flag.noreturn)
  5313. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5314. break;
  5315. case 2: // SavePoint
  5316. if(!map[pl_sd->bl.m].flag.noreturn)
  5317. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5318. break;
  5319. case 3: // m,x,y
  5320. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pl_sd->group_level))
  5321. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5322. break;
  5323. }
  5324. }
  5325. mapit_free(iter);
  5326. return SCRIPT_CMD_SUCCESS;
  5327. }
  5328. /*==========================================
  5329. * Force Heal a player (hp and sp)
  5330. *------------------------------------------*/
  5331. BUILDIN_FUNC(heal)
  5332. {
  5333. TBL_PC *sd;
  5334. int hp,sp;
  5335. if (!script_charid2sd(4,sd))
  5336. return SCRIPT_CMD_SUCCESS;
  5337. hp=script_getnum(st,2);
  5338. sp=script_getnum(st,3);
  5339. status_heal(&sd->bl, hp, sp, 1);
  5340. return SCRIPT_CMD_SUCCESS;
  5341. }
  5342. /*==========================================
  5343. * Heal a player by item (get vit bonus etc)
  5344. *------------------------------------------*/
  5345. BUILDIN_FUNC(itemheal)
  5346. {
  5347. TBL_PC *sd;
  5348. int hp,sp;
  5349. hp=script_getnum(st,2);
  5350. sp=script_getnum(st,3);
  5351. if(potion_flag==1) {
  5352. potion_hp = hp;
  5353. potion_sp = sp;
  5354. return SCRIPT_CMD_SUCCESS;
  5355. }
  5356. if (!script_charid2sd(4,sd))
  5357. return SCRIPT_CMD_SUCCESS;
  5358. pc_itemheal(sd,sd->itemid,hp,sp);
  5359. return SCRIPT_CMD_SUCCESS;
  5360. }
  5361. /*==========================================
  5362. *
  5363. *------------------------------------------*/
  5364. BUILDIN_FUNC(percentheal)
  5365. {
  5366. int hp,sp;
  5367. TBL_PC* sd;
  5368. hp=script_getnum(st,2);
  5369. sp=script_getnum(st,3);
  5370. if(potion_flag==1) {
  5371. potion_per_hp = hp;
  5372. potion_per_sp = sp;
  5373. return SCRIPT_CMD_SUCCESS;
  5374. }
  5375. if (!script_charid2sd(4,sd))
  5376. return SCRIPT_CMD_SUCCESS;
  5377. #ifdef RENEWAL
  5378. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5379. sp = 0;
  5380. #endif
  5381. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5382. hp = 0;
  5383. sp = 0;
  5384. }
  5385. if (sd->sc.data[SC_BITESCAR])
  5386. hp = 0;
  5387. pc_percentheal(sd,hp,sp);
  5388. return SCRIPT_CMD_SUCCESS;
  5389. }
  5390. /*==========================================
  5391. *
  5392. *------------------------------------------*/
  5393. BUILDIN_FUNC(jobchange)
  5394. {
  5395. int job, upper=-1;
  5396. job=script_getnum(st,2);
  5397. if( script_hasdata(st,3) )
  5398. upper=script_getnum(st,3);
  5399. if (pcdb_checkid(job))
  5400. {
  5401. TBL_PC* sd;
  5402. if (!script_charid2sd(4,sd))
  5403. return SCRIPT_CMD_SUCCESS;
  5404. pc_jobchange(sd, job, upper);
  5405. }
  5406. return SCRIPT_CMD_SUCCESS;
  5407. }
  5408. /*==========================================
  5409. *
  5410. *------------------------------------------*/
  5411. BUILDIN_FUNC(jobname)
  5412. {
  5413. int class_=script_getnum(st,2);
  5414. script_pushconststr(st, (char*)job_name(class_));
  5415. return SCRIPT_CMD_SUCCESS;
  5416. }
  5417. /// Get input from the player.
  5418. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5419. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5420. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5421. /// shorter than 'min' and 0 otherwise.
  5422. ///
  5423. /// input(<var>{,<min>{,<max>}}) -> <int>
  5424. BUILDIN_FUNC(input)
  5425. {
  5426. TBL_PC* sd;
  5427. struct script_data* data;
  5428. int64 uid;
  5429. const char* name;
  5430. int min;
  5431. int max;
  5432. if( !script_rid2sd(sd) )
  5433. return SCRIPT_CMD_SUCCESS;
  5434. data = script_getdata(st,2);
  5435. if( !data_isreference(data) ){
  5436. ShowError("script:input: not a variable\n");
  5437. script_reportdata(data);
  5438. st->state = END;
  5439. return SCRIPT_CMD_FAILURE;
  5440. }
  5441. uid = reference_getuid(data);
  5442. name = reference_getname(data);
  5443. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5444. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5445. #ifdef SECURE_NPCTIMEOUT
  5446. sd->npc_idle_type = NPCT_WAIT;
  5447. #endif
  5448. if( !sd->state.menu_or_input )
  5449. { // first invocation, display npc input box
  5450. sd->state.menu_or_input = 1;
  5451. st->state = RERUNLINE;
  5452. if( is_string_variable(name) )
  5453. clif_scriptinputstr(sd,st->oid);
  5454. else
  5455. clif_scriptinput(sd,st->oid);
  5456. }
  5457. else
  5458. { // take received text/value and store it in the designated variable
  5459. sd->state.menu_or_input = 0;
  5460. if( is_string_variable(name) )
  5461. {
  5462. int len = (int)strlen(sd->npc_str);
  5463. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5464. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5465. }
  5466. else
  5467. {
  5468. int amount = sd->npc_amount;
  5469. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5470. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5471. }
  5472. st->state = RUN;
  5473. }
  5474. return SCRIPT_CMD_SUCCESS;
  5475. }
  5476. // declare the copyarray method here for future reference
  5477. BUILDIN_FUNC(copyarray);
  5478. /// Sets the value of a variable.
  5479. /// The value is converted to the type of the variable.
  5480. ///
  5481. /// set(<variable>,<value>{,<charid>})
  5482. BUILDIN_FUNC(setr)
  5483. {
  5484. TBL_PC* sd = NULL;
  5485. struct script_data* data;
  5486. //struct script_data* datavalue;
  5487. int64 num;
  5488. uint8 pos = 4;
  5489. const char* name, *command = script_getfuncname(st);
  5490. char prefix;
  5491. data = script_getdata(st,2);
  5492. //datavalue = script_getdata(st,3);
  5493. if( !data_isreference(data) )
  5494. {
  5495. ShowError("script:set: not a variable\n");
  5496. script_reportdata(script_getdata(st,2));
  5497. st->state = END;
  5498. return SCRIPT_CMD_FAILURE;
  5499. }
  5500. num = reference_getuid(data);
  5501. name = reference_getname(data);
  5502. prefix = *name;
  5503. if (!strcmp(command, "setr"))
  5504. pos = 5;
  5505. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5506. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5507. return SCRIPT_CMD_FAILURE;
  5508. }
  5509. #if 0
  5510. if( data_isreference(datavalue) )
  5511. {// the value being referenced is a variable
  5512. const char* namevalue = reference_getname(datavalue);
  5513. if( !not_array_variable(*namevalue) )
  5514. {// array variable being copied into another array variable
  5515. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5516. {// player must be attached in order to copy a player variable
  5517. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5518. return SCRIPT_CMD_SUCCESS;
  5519. }
  5520. if( is_string_variable(namevalue) != is_string_variable(name) )
  5521. {// non-matching array value types
  5522. ShowWarning("script:set: two array variables do not match in type.\n");
  5523. return SCRIPT_CMD_SUCCESS;
  5524. }
  5525. // push the maximum number of array values to the stack
  5526. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5527. // call the copy array method directly
  5528. return buildin_copyarray(st);
  5529. }
  5530. }
  5531. #endif
  5532. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5533. if( is_string_variable(name) )
  5534. script_pushstrcopy(st,script_getstr(st, 4));
  5535. else
  5536. script_pushint(st,script_getnum(st, 4));
  5537. } else // Return a copy of the variable reference
  5538. script_pushcopy(st, 2);
  5539. if( is_string_variable(name) )
  5540. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5541. else
  5542. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5543. return SCRIPT_CMD_SUCCESS;
  5544. }
  5545. /////////////////////////////////////////////////////////////////////
  5546. /// Array variables
  5547. ///
  5548. /// Sets values of an array, from the starting index.
  5549. /// ex: setarray arr[1],1,2,3;
  5550. ///
  5551. /// setarray <array variable>,<value1>{,<value2>...};
  5552. BUILDIN_FUNC(setarray)
  5553. {
  5554. struct script_data* data;
  5555. const char* name;
  5556. uint32 start;
  5557. uint32 end;
  5558. int32 id;
  5559. int32 i;
  5560. TBL_PC* sd = NULL;
  5561. data = script_getdata(st, 2);
  5562. if( !data_isreference(data) )
  5563. {
  5564. ShowError("script:setarray: not a variable\n");
  5565. script_reportdata(data);
  5566. st->state = END;
  5567. return SCRIPT_CMD_FAILURE;// not a variable
  5568. }
  5569. id = reference_getid(data);
  5570. start = reference_getindex(data);
  5571. name = reference_getname(data);
  5572. if( not_server_variable(*name) )
  5573. {
  5574. if( !script_rid2sd(sd) )
  5575. return SCRIPT_CMD_SUCCESS;// no player attached
  5576. }
  5577. end = start + script_lastdata(st) - 2;
  5578. if( end > SCRIPT_MAX_ARRAYSIZE )
  5579. end = SCRIPT_MAX_ARRAYSIZE;
  5580. if( is_string_variable(name) )
  5581. {// string array
  5582. for( i = 3; start < end; ++start, ++i )
  5583. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5584. }
  5585. else
  5586. {// int array
  5587. for( i = 3; start < end; ++start, ++i )
  5588. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5589. }
  5590. return SCRIPT_CMD_SUCCESS;
  5591. }
  5592. /// Sets count values of an array, from the starting index.
  5593. /// ex: cleararray arr[0],0,1;
  5594. ///
  5595. /// cleararray <array variable>,<value>,<count>;
  5596. BUILDIN_FUNC(cleararray)
  5597. {
  5598. struct script_data* data;
  5599. const char* name;
  5600. uint32 start;
  5601. uint32 end;
  5602. int32 id;
  5603. void* v;
  5604. TBL_PC* sd = NULL;
  5605. data = script_getdata(st, 2);
  5606. if( !data_isreference(data) )
  5607. {
  5608. ShowError("script:cleararray: not a variable\n");
  5609. script_reportdata(data);
  5610. st->state = END;
  5611. return SCRIPT_CMD_FAILURE;// not a variable
  5612. }
  5613. id = reference_getid(data);
  5614. start = reference_getindex(data);
  5615. name = reference_getname(data);
  5616. if( not_server_variable(*name) )
  5617. {
  5618. if( !script_rid2sd(sd) )
  5619. return SCRIPT_CMD_SUCCESS;// no player attached
  5620. }
  5621. if( is_string_variable(name) )
  5622. v = (void*)script_getstr(st, 3);
  5623. else
  5624. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5625. end = start + script_getnum(st, 4);
  5626. if( end > SCRIPT_MAX_ARRAYSIZE )
  5627. end = SCRIPT_MAX_ARRAYSIZE;
  5628. for( ; start < end; ++start )
  5629. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5630. return SCRIPT_CMD_SUCCESS;
  5631. }
  5632. /// Copies data from one array to another.
  5633. /// ex: copyarray arr[0],arr[2],2;
  5634. ///
  5635. /// copyarray <destination array variable>,<source array variable>,<count>;
  5636. BUILDIN_FUNC(copyarray)
  5637. {
  5638. struct script_data* data1;
  5639. struct script_data* data2;
  5640. const char* name1;
  5641. const char* name2;
  5642. int32 idx1;
  5643. int32 idx2;
  5644. int32 id1;
  5645. int32 id2;
  5646. void* v;
  5647. int32 i;
  5648. uint32 count;
  5649. TBL_PC* sd = NULL;
  5650. data1 = script_getdata(st, 2);
  5651. data2 = script_getdata(st, 3);
  5652. if( !data_isreference(data1) || !data_isreference(data2) )
  5653. {
  5654. ShowError("script:copyarray: not a variable\n");
  5655. script_reportdata(data1);
  5656. script_reportdata(data2);
  5657. st->state = END;
  5658. return SCRIPT_CMD_FAILURE;// not a variable
  5659. }
  5660. id1 = reference_getid(data1);
  5661. id2 = reference_getid(data2);
  5662. idx1 = reference_getindex(data1);
  5663. idx2 = reference_getindex(data2);
  5664. name1 = reference_getname(data1);
  5665. name2 = reference_getname(data2);
  5666. if( is_string_variable(name1) != is_string_variable(name2) )
  5667. {
  5668. ShowError("script:copyarray: type mismatch\n");
  5669. script_reportdata(data1);
  5670. script_reportdata(data2);
  5671. st->state = END;
  5672. return SCRIPT_CMD_FAILURE;// data type mismatch
  5673. }
  5674. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5675. {
  5676. if( !script_rid2sd(sd) )
  5677. return SCRIPT_CMD_SUCCESS;// no player attached
  5678. }
  5679. count = script_getnum(st, 4);
  5680. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5681. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5682. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5683. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5684. if( id1 == id2 && idx1 > idx2 )
  5685. {// destination might be overlapping the source - copy in reverse order
  5686. for( i = count - 1; i >= 0; --i )
  5687. {
  5688. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5689. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5690. script_removetop(st, -1, 0);
  5691. }
  5692. }
  5693. else
  5694. {// normal copy
  5695. for( i = 0; i < count; ++i )
  5696. {
  5697. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5698. {
  5699. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5700. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5701. script_removetop(st, -1, 0);
  5702. }
  5703. else// out of range - assume ""/0
  5704. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5705. }
  5706. }
  5707. return SCRIPT_CMD_SUCCESS;
  5708. }
  5709. /// Returns the size of the array.
  5710. /// Assumes that everything before the starting index exists.
  5711. /// ex: getarraysize(arr[3])
  5712. ///
  5713. /// getarraysize(<array variable>) -> <int>
  5714. BUILDIN_FUNC(getarraysize)
  5715. {
  5716. struct script_data* data;
  5717. const char* name;
  5718. struct map_session_data* sd = NULL;
  5719. data = script_getdata(st, 2);
  5720. if( !data_isreference(data) )
  5721. {
  5722. ShowError("script:getarraysize: not a variable\n");
  5723. script_reportdata(data);
  5724. script_pushnil(st);
  5725. st->state = END;
  5726. return SCRIPT_CMD_FAILURE;// not a variable
  5727. }
  5728. name = reference_getname(data);
  5729. if( not_server_variable(*name) ){
  5730. if (!script_rid2sd(sd))
  5731. return SCRIPT_CMD_SUCCESS;// no player attached
  5732. }
  5733. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5734. return SCRIPT_CMD_SUCCESS;
  5735. }
  5736. int script_array_index_cmp(const void *a, const void *b) {
  5737. return ( *(unsigned int*)a - *(unsigned int*)b );
  5738. }
  5739. /// Deletes count or all the elements in an array, from the starting index.
  5740. /// ex: deletearray arr[4],2;
  5741. ///
  5742. /// deletearray <array variable>;
  5743. /// deletearray <array variable>,<count>;
  5744. BUILDIN_FUNC(deletearray)
  5745. {
  5746. struct script_data* data;
  5747. const char* name;
  5748. unsigned int start, end, i;
  5749. int id;
  5750. TBL_PC *sd = NULL;
  5751. struct script_array *sa = NULL;
  5752. struct reg_db *src = NULL;
  5753. void *value;
  5754. data = script_getdata(st, 2);
  5755. if( !data_isreference(data) ) {
  5756. ShowError("script:deletearray: not a variable\n");
  5757. script_reportdata(data);
  5758. st->state = END;
  5759. return SCRIPT_CMD_FAILURE;// not a variable
  5760. }
  5761. id = reference_getid(data);
  5762. start = reference_getindex(data);
  5763. name = reference_getname(data);
  5764. if( not_server_variable(*name) ) {
  5765. if( !script_rid2sd(sd) )
  5766. return SCRIPT_CMD_SUCCESS;// no player attached
  5767. }
  5768. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5769. ShowError("script:deletearray: not a array\n");
  5770. script_reportdata(data);
  5771. st->state = END;
  5772. return SCRIPT_CMD_FAILURE;// not a variable
  5773. }
  5774. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5775. if ( !(sa = static_cast<script_array *>(idb_get(src->arrays, id))) ) { // non-existent array, nothing to empty
  5776. return SCRIPT_CMD_SUCCESS;// not a variable
  5777. }
  5778. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5779. if( start >= end )
  5780. return SCRIPT_CMD_SUCCESS;// nothing to free
  5781. if( is_string_variable(name) )
  5782. value = (void *)"";
  5783. else
  5784. value = (void *)0;
  5785. if( script_hasdata(st,3) ) {
  5786. unsigned int count = script_getnum(st, 3);
  5787. if( count > end - start )
  5788. count = end - start;
  5789. if( count <= 0 )
  5790. return SCRIPT_CMD_SUCCESS;// nothing to free
  5791. if( end - start < sa->size ) {
  5792. // Better to iterate directly on the array, no speed-up from using sa
  5793. for( ; start + count < end; ++start ) {
  5794. // Compact and overwrite
  5795. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5796. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5797. script_removetop(st, -1, 0);
  5798. }
  5799. for( ; start < end; start++ ) {
  5800. // Clean up any leftovers that weren't overwritten
  5801. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5802. }
  5803. } else {
  5804. // using sa to speed up
  5805. unsigned int *list = NULL, size = 0;
  5806. list = script_array_cpy_list(sa);
  5807. size = sa->size;
  5808. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5809. ARR_FIND(0, size, i, list[i] >= start);
  5810. for( ; i < size && list[i] < start + count; i++ ) {
  5811. // Clear any entries between start and start+count, if they exist
  5812. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5813. }
  5814. for( ; i < size && list[i] < end; i++ ) {
  5815. // Move back count positions any entries between start+count to fill the gaps
  5816. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5817. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5818. script_removetop(st, -1, 0);
  5819. // Clear their originals
  5820. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5821. }
  5822. }
  5823. } else {
  5824. unsigned int *list = NULL, size = 0;
  5825. list = script_array_cpy_list(sa);
  5826. size = sa->size;
  5827. for(i = 0; i < size; i++) {
  5828. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5829. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5830. }
  5831. }
  5832. return SCRIPT_CMD_SUCCESS;
  5833. }
  5834. /// Returns a reference to the target index of the array variable.
  5835. /// Equivalent to var[index].
  5836. ///
  5837. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5838. BUILDIN_FUNC(getelementofarray)
  5839. {
  5840. struct script_data* data;
  5841. int32 id;
  5842. int64 i;
  5843. data = script_getdata(st, 2);
  5844. if( !data_isreference(data) )
  5845. {
  5846. ShowError("script:getelementofarray: not a variable\n");
  5847. script_reportdata(data);
  5848. script_pushnil(st);
  5849. st->state = END;
  5850. return SCRIPT_CMD_SUCCESS;// not a variable
  5851. }
  5852. id = reference_getid(data);
  5853. i = script_getnum(st, 3);
  5854. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5855. ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i);
  5856. script_reportdata(data);
  5857. script_pushnil(st);
  5858. st->state = END;
  5859. return SCRIPT_CMD_FAILURE;// out of range
  5860. }
  5861. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5862. return SCRIPT_CMD_SUCCESS;
  5863. }
  5864. /// Return the index number of the first matching value in an array.
  5865. /// ex: inarray arr,1;
  5866. ///
  5867. /// inarray <array variable>,<value>;
  5868. BUILDIN_FUNC(inarray)
  5869. {
  5870. struct script_data *data;
  5871. const char* name;
  5872. int id, i, array_size;
  5873. struct map_session_data* sd = NULL;
  5874. struct reg_db *ref = NULL;
  5875. data = script_getdata(st, 2);
  5876. if (!data_isreference(data))
  5877. {
  5878. ShowError("buildin_inarray: not a variable\n");
  5879. script_reportdata(data);
  5880. st->state = END;
  5881. return SCRIPT_CMD_FAILURE;
  5882. }
  5883. name = reference_getname(data);
  5884. ref = reference_getref(data);
  5885. if (not_server_variable(*name) && !script_rid2sd(sd))
  5886. return SCRIPT_CMD_FAILURE;
  5887. array_size = script_array_highest_key(st, sd, name, ref) - 1;
  5888. if (array_size > SCRIPT_MAX_ARRAYSIZE)
  5889. {
  5890. ShowError("buildin_inarray: The array is too large.\n");
  5891. script_reportdata(data);
  5892. st->state = END;
  5893. return SCRIPT_CMD_FAILURE;
  5894. }
  5895. id = reference_getid(data);
  5896. if (is_string_variable(name))
  5897. {
  5898. const char* temp;
  5899. const char* value;
  5900. value = script_getstr(st, 3);
  5901. for (i = 0; i <= array_size; ++i)
  5902. {
  5903. temp = (char*)get_val2(st, reference_uid(id, i), ref);
  5904. script_removetop(st, -1, 0);
  5905. if (!strcmp(temp, value))
  5906. {
  5907. script_pushint(st, i);
  5908. return SCRIPT_CMD_SUCCESS;
  5909. }
  5910. }
  5911. }
  5912. else
  5913. {
  5914. int temp, value;
  5915. value = script_getnum(st, 3);
  5916. for (i = 0; i <= array_size; ++i)
  5917. {
  5918. temp = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, i), ref));
  5919. script_removetop(st, -1, 0);
  5920. if (temp == value)
  5921. {
  5922. script_pushint(st, i);
  5923. return SCRIPT_CMD_SUCCESS;
  5924. }
  5925. }
  5926. }
  5927. script_pushint(st, -1);
  5928. return SCRIPT_CMD_SUCCESS;
  5929. }
  5930. /// Return the number of matches in two arrays.
  5931. /// ex: countinarray arr[0],arr1[0];
  5932. ///
  5933. /// countinarray <array variable>,<array variable>;
  5934. BUILDIN_FUNC(countinarray)
  5935. {
  5936. struct script_data *data1 , *data2;
  5937. const char* name1;
  5938. const char* name2;
  5939. int id1, id2, i, j, array_size1, array_size2, case_count = 0;
  5940. struct map_session_data* sd = NULL;
  5941. struct reg_db *ref1 = NULL, *ref2 = NULL;
  5942. data1 = script_getdata(st, 2);
  5943. data2 = script_getdata(st, 3);
  5944. if (!data_isreference(data1) || !data_isreference(data2))
  5945. {
  5946. ShowError("buildin_countinarray: not a variable\n");
  5947. script_reportdata(data1);
  5948. script_reportdata(data2);
  5949. st->state = END;
  5950. return SCRIPT_CMD_FAILURE;
  5951. }
  5952. name1 = reference_getname(data1);
  5953. name2 = reference_getname(data2);
  5954. ref1 = reference_getref(data1);
  5955. ref2 = reference_getref(data2);
  5956. if (not_server_variable(*name1) && not_server_variable(*name2) && !script_rid2sd(sd))
  5957. return SCRIPT_CMD_FAILURE;
  5958. array_size1 = script_array_highest_key(st, sd, name1, ref1) - 1;
  5959. array_size2 = script_array_highest_key(st, sd, name2, ref2) - 1;
  5960. if (array_size1 > SCRIPT_MAX_ARRAYSIZE || array_size2 > SCRIPT_MAX_ARRAYSIZE)
  5961. {
  5962. ShowError("buildin_countinarray: The array is too large.\n");
  5963. script_reportdata(data1);
  5964. script_reportdata(data2);
  5965. st->state = END;
  5966. return SCRIPT_CMD_FAILURE;
  5967. }
  5968. i = reference_getindex(data1);
  5969. j = reference_getindex(data2);
  5970. if (array_size1 < i || array_size2 < j)
  5971. { //To prevent unintended behavior
  5972. ShowError("buildin_countinarray: The given index of the array is higher than the array size.\n");
  5973. script_reportdata(data1);
  5974. script_reportdata(data2);
  5975. st->state = END;
  5976. return SCRIPT_CMD_FAILURE;
  5977. }
  5978. id1 = reference_getid(data1);
  5979. id2 = reference_getid(data2);
  5980. if (is_string_variable(name1) && is_string_variable(name2))
  5981. {
  5982. const char* temp1;
  5983. const char* temp2;
  5984. for (; i <= array_size1; ++i)
  5985. {
  5986. temp1 = (char*)get_val2(st, reference_uid(id1, i), ref1);
  5987. for (j = reference_getindex(data2); j <= array_size2; j++)
  5988. {
  5989. temp2 = (char*)get_val2(st, reference_uid(id2, j), ref2);
  5990. if (!strcmp(temp1, temp2))
  5991. {
  5992. case_count++;
  5993. }
  5994. script_removetop(st, -1, 0);
  5995. }
  5996. script_removetop(st, -1, 0);
  5997. }
  5998. }
  5999. else if (!is_string_variable(name1) && !is_string_variable(name2))
  6000. {
  6001. int temp1, temp2;
  6002. for (; i <= array_size1; ++i)
  6003. {
  6004. temp1 = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id1, i), ref1));
  6005. for (j = reference_getindex(data2); j <= array_size2; j++)
  6006. {
  6007. temp2 = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id2, j), ref2));
  6008. if (temp1 == temp2)
  6009. {
  6010. case_count++;
  6011. }
  6012. script_removetop(st, -1, 0);
  6013. }
  6014. script_removetop(st, -1, 0);
  6015. }
  6016. }
  6017. else
  6018. {
  6019. ShowError("buildin_countinarray: Arrays does not match , You can't compare int array to string array.\n");
  6020. script_reportdata(data1);
  6021. script_reportdata(data2);
  6022. st->state = END;
  6023. return SCRIPT_CMD_FAILURE;
  6024. }
  6025. script_pushint(st, case_count);
  6026. return SCRIPT_CMD_SUCCESS;
  6027. }
  6028. /////////////////////////////////////////////////////////////////////
  6029. /// ...
  6030. ///
  6031. /*==========================================
  6032. *
  6033. *------------------------------------------*/
  6034. BUILDIN_FUNC(setlook)
  6035. {
  6036. int type,val;
  6037. TBL_PC* sd;
  6038. type = script_getnum(st,2);
  6039. val = script_getnum(st,3);
  6040. if (!script_charid2sd(4,sd))
  6041. return SCRIPT_CMD_SUCCESS;
  6042. pc_changelook(sd,type,val);
  6043. return SCRIPT_CMD_SUCCESS;
  6044. }
  6045. BUILDIN_FUNC(changelook)
  6046. { // As setlook but only client side
  6047. int type,val;
  6048. TBL_PC* sd;
  6049. type = script_getnum(st,2);
  6050. val = script_getnum(st,3);
  6051. if (!script_charid2sd(4,sd))
  6052. return SCRIPT_CMD_SUCCESS;
  6053. clif_changelook(&sd->bl,type,val);
  6054. return SCRIPT_CMD_SUCCESS;
  6055. }
  6056. /*==========================================
  6057. *
  6058. *------------------------------------------*/
  6059. BUILDIN_FUNC(cutin)
  6060. {
  6061. TBL_PC* sd;
  6062. if( !script_rid2sd(sd) )
  6063. return SCRIPT_CMD_SUCCESS;
  6064. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  6065. return SCRIPT_CMD_SUCCESS;
  6066. }
  6067. /*==========================================
  6068. *
  6069. *------------------------------------------*/
  6070. BUILDIN_FUNC(viewpoint)
  6071. {
  6072. int type,x,y,id,color;
  6073. TBL_PC* sd;
  6074. type=script_getnum(st,2);
  6075. x=script_getnum(st,3);
  6076. y=script_getnum(st,4);
  6077. id=script_getnum(st,5);
  6078. color=script_getnum(st,6);
  6079. if( !script_rid2sd(sd) )
  6080. return SCRIPT_CMD_SUCCESS;
  6081. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  6082. return SCRIPT_CMD_SUCCESS;
  6083. }
  6084. /**
  6085. * Set random options for new item
  6086. * @param st Script state
  6087. * @param it Temporary item data
  6088. * @param funcname Function name
  6089. * @param x First position of random option id array from the script
  6090. **/
  6091. static int script_getitem_randomoption(struct script_state *st, struct item *it, const char *funcname, int x) {
  6092. int i, opt_id_n;
  6093. struct script_data *opt_id = script_getdata(st,x);
  6094. struct script_data *opt_val = script_getdata(st,x+1);
  6095. struct script_data *opt_param = script_getdata(st,x+2);
  6096. const char *opt_id_var = reference_getname(opt_id);
  6097. const char *opt_val_var = reference_getname(opt_val);
  6098. const char *opt_param_var = reference_getname(opt_param);
  6099. int32 opt_id_id, opt_id_idx;
  6100. int32 opt_val_id, opt_val_idx;
  6101. int32 opt_param_id, opt_param_idx;
  6102. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  6103. if (is_string_variable(opt_id_var)) {
  6104. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  6105. return SCRIPT_CMD_FAILURE;
  6106. }
  6107. if (is_string_variable(opt_val_var)) {
  6108. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  6109. return SCRIPT_CMD_FAILURE;
  6110. }
  6111. if (is_string_variable(opt_param_var)) {
  6112. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  6113. return SCRIPT_CMD_FAILURE;
  6114. }
  6115. opt_id_ref = reference_getref(opt_id);
  6116. opt_id_n = script_array_highest_key(st, NULL, opt_id_var, opt_id_ref);
  6117. if (opt_id_n < 1) {
  6118. ShowError("buildin_%s: No option id listed.\n", funcname);
  6119. return SCRIPT_CMD_FAILURE;
  6120. }
  6121. opt_val_ref = reference_getref(opt_val);
  6122. opt_param_ref = reference_getref(opt_param);
  6123. opt_id_id = reference_getid(opt_id);
  6124. opt_val_id = reference_getid(opt_val);
  6125. opt_param_id = reference_getid(opt_param);
  6126. opt_id_idx = reference_getindex(opt_id);
  6127. opt_val_idx = reference_getindex(opt_val);
  6128. opt_param_idx = reference_getindex(opt_param);
  6129. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  6130. it->option[i].id = static_cast<short>((int32)__64BPRTSIZE(get_val2(st,reference_uid(opt_id_id,opt_id_idx+i),opt_id_ref)));
  6131. script_removetop(st, -1, 0);
  6132. it->option[i].value = static_cast<short>((int32)__64BPRTSIZE(get_val2(st,reference_uid(opt_val_id,opt_val_idx+i),opt_val_ref)));
  6133. script_removetop(st, -1, 0);
  6134. it->option[i].param = static_cast<char>((int32)__64BPRTSIZE(get_val2(st,reference_uid(opt_param_id,opt_param_idx+i),opt_param_ref)));
  6135. script_removetop(st, -1, 0);
  6136. }
  6137. return SCRIPT_CMD_SUCCESS;
  6138. }
  6139. /// Returns number of items in inventory/cart/storage
  6140. /// countitem <nameID>{,<accountID>});
  6141. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  6142. /// cartcountitem <nameID>{,<accountID>});
  6143. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6144. /// storagecountitem <nameID>{,<accountID>});
  6145. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6146. /// guildstoragecountitem <nameID>{,<accountID>});
  6147. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  6148. /// countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>)
  6149. /// countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>)
  6150. BUILDIN_FUNC(countitem)
  6151. {
  6152. int i = 0, aid = 3;
  6153. struct item_data* id = NULL;
  6154. struct script_data* data;
  6155. char *command = (char *)script_getfuncname(st);
  6156. uint8 loc = 0;
  6157. uint16 size, count = 0;
  6158. struct item *items;
  6159. TBL_PC *sd = NULL;
  6160. struct s_storage *gstor = NULL;
  6161. if( command[strlen(command)-1] == '2' ) {
  6162. i = 1;
  6163. aid = 10;
  6164. }
  6165. else if (command[strlen(command)-1] == '3') {
  6166. i = 1;
  6167. aid = 13;
  6168. }
  6169. if( script_hasdata(st,aid) ) {
  6170. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  6171. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  6172. st->state = END;
  6173. return SCRIPT_CMD_FAILURE;
  6174. }
  6175. }
  6176. else {
  6177. if( !script_rid2sd(sd) )
  6178. return SCRIPT_CMD_SUCCESS;
  6179. }
  6180. if( !strncmp(command, "cart", 4) ) {
  6181. loc = TABLE_CART;
  6182. size = MAX_CART;
  6183. items = sd->cart.u.items_cart;
  6184. }
  6185. else if( !strncmp(command, "storage", 7) ) {
  6186. loc = TABLE_STORAGE;
  6187. size = MAX_STORAGE;
  6188. items = sd->storage.u.items_storage;
  6189. }
  6190. else if( !strncmp(command, "guildstorage", 12) ) {
  6191. gstor = guild2storage2(sd->status.guild_id);
  6192. if (gstor && !sd->state.storage_flag) {
  6193. loc = TABLE_GUILD_STORAGE;
  6194. size = MAX_GUILD_STORAGE;
  6195. items = gstor->u.items_guild;
  6196. } else {
  6197. script_pushint(st, -1);
  6198. return SCRIPT_CMD_SUCCESS;
  6199. }
  6200. }
  6201. else {
  6202. size = MAX_INVENTORY;
  6203. items = sd->inventory.u.items_inventory;
  6204. }
  6205. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  6206. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6207. script_pushint(st,-1);
  6208. return SCRIPT_CMD_SUCCESS;
  6209. }
  6210. data = script_getdata(st, 2);
  6211. get_val(st, data); // Convert into value in case of a variable
  6212. if( data_isstring(data) ) // item name
  6213. id = itemdb_searchname(conv_str(st, data));
  6214. else // item id
  6215. id = itemdb_exists(conv_num(st, data));
  6216. if( id == NULL ) {
  6217. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  6218. script_pushint(st,0);
  6219. return SCRIPT_CMD_FAILURE;
  6220. }
  6221. if (loc == TABLE_GUILD_STORAGE)
  6222. gstor->lock = true;
  6223. if( !i ) { // For count/cart/storagecountitem function
  6224. unsigned short nameid = id->nameid;
  6225. for( i = 0; i < size; i++ )
  6226. if( &items[i] && items[i].nameid == nameid )
  6227. count += items[i].amount;
  6228. }
  6229. else { // For count/cart/storagecountitem2 function
  6230. struct item it;
  6231. bool check_randomopt = false;
  6232. memset(&it, 0, sizeof(it));
  6233. it.nameid = id->nameid;
  6234. it.identify = script_getnum(st,3);
  6235. it.refine = script_getnum(st,4);
  6236. it.attribute = script_getnum(st,5);
  6237. it.card[0] = script_getnum(st,6);
  6238. it.card[1] = script_getnum(st,7);
  6239. it.card[2] = script_getnum(st,8);
  6240. it.card[3] = script_getnum(st,9);
  6241. if (command[strlen(command)-1] == '3') {
  6242. int res = script_getitem_randomoption(st, &it, command, 10);
  6243. if (res != SCRIPT_CMD_SUCCESS)
  6244. return SCRIPT_CMD_FAILURE;
  6245. check_randomopt = true;
  6246. }
  6247. for( i = 0; i < size; i++ ) {
  6248. struct item *itm = &items[i];
  6249. if (!itm || !itm->nameid || itm->amount < 1)
  6250. continue;
  6251. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute)
  6252. continue;
  6253. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6254. continue;
  6255. if (check_randomopt) {
  6256. uint8 j;
  6257. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6258. 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)
  6259. break;
  6260. }
  6261. if (j != MAX_ITEM_RDM_OPT)
  6262. continue;
  6263. }
  6264. count += items[i].amount;
  6265. }
  6266. }
  6267. if (loc == TABLE_GUILD_STORAGE) {
  6268. storage_guild_storageclose(sd);
  6269. gstor->lock = false;
  6270. }
  6271. script_pushint(st, count);
  6272. return SCRIPT_CMD_SUCCESS;
  6273. }
  6274. /*==========================================
  6275. * Check if item with this amount can fit in inventory
  6276. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6277. * Return
  6278. * 0 : fail
  6279. * 1 : success (npc side only)
  6280. *------------------------------------------*/
  6281. BUILDIN_FUNC(checkweight)
  6282. {
  6283. int slots = 0;
  6284. unsigned short amount2 = 0;
  6285. unsigned int weight = 0, i, nbargs;
  6286. struct item_data* id = NULL;
  6287. struct map_session_data* sd;
  6288. if( !script_rid2sd(sd) )
  6289. return SCRIPT_CMD_SUCCESS;
  6290. nbargs = script_lastdata(st)+1;
  6291. if(nbargs%2) {
  6292. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6293. script_pushint(st,0);
  6294. return SCRIPT_CMD_FAILURE;
  6295. }
  6296. slots = pc_inventoryblank(sd); //nb of empty slot
  6297. for (i = 2; i < nbargs; i += 2) {
  6298. unsigned short nameid, amount;
  6299. struct script_data* data = script_getdata(st,i);
  6300. get_val(st, data); // Convert into value in case of a variable
  6301. if( data_isstring(data) ) // item name
  6302. id = itemdb_searchname(conv_str(st, data));
  6303. else // item id
  6304. id = itemdb_exists(conv_num(st, data));
  6305. if( id == NULL ) {
  6306. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6307. script_pushint(st,0);
  6308. return SCRIPT_CMD_FAILURE;
  6309. }
  6310. nameid = id->nameid;
  6311. amount = script_getnum(st,i+1);
  6312. if( amount < 1 ) {
  6313. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6314. script_pushint(st,0);
  6315. return SCRIPT_CMD_FAILURE;
  6316. }
  6317. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6318. if( weight + sd->weight > sd->max_weight )
  6319. {// too heavy
  6320. script_pushint(st,0);
  6321. return SCRIPT_CMD_SUCCESS;
  6322. }
  6323. switch( pc_checkadditem(sd, nameid, amount) ) {
  6324. case CHKADDITEM_EXIST:
  6325. // item is already in inventory, but there is still space for the requested amount
  6326. break;
  6327. case CHKADDITEM_NEW:
  6328. if( itemdb_isstackable(nameid) ) {
  6329. // stackable
  6330. amount2++;
  6331. if( slots < amount2 ) {
  6332. script_pushint(st,0);
  6333. return SCRIPT_CMD_SUCCESS;
  6334. }
  6335. } else {
  6336. // non-stackable
  6337. amount2 += amount;
  6338. if( slots < amount2) {
  6339. script_pushint(st,0);
  6340. return SCRIPT_CMD_SUCCESS;
  6341. }
  6342. }
  6343. break;
  6344. case CHKADDITEM_OVERAMOUNT:
  6345. script_pushint(st,0);
  6346. return SCRIPT_CMD_SUCCESS;
  6347. }
  6348. }
  6349. script_pushint(st,1);
  6350. return SCRIPT_CMD_SUCCESS;
  6351. }
  6352. BUILDIN_FUNC(checkweight2)
  6353. {
  6354. //variable sub checkweight
  6355. int i = 0, slots = 0, weight = 0;
  6356. short fail = 0;
  6357. unsigned short amount2 = 0;
  6358. //variable for array parsing
  6359. struct script_data* data_it;
  6360. struct script_data* data_nb;
  6361. const char* name_it;
  6362. const char* name_nb;
  6363. int32 id_it, id_nb;
  6364. int32 idx_it, idx_nb;
  6365. int nb_it, nb_nb; //array size
  6366. TBL_PC *sd;
  6367. if( !script_rid2sd(sd) )
  6368. return SCRIPT_CMD_SUCCESS;
  6369. data_it = script_getdata(st, 2);
  6370. data_nb = script_getdata(st, 3);
  6371. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6372. ShowError("buildin_checkweight2: parameter not a variable\n");
  6373. script_pushint(st,0);
  6374. return SCRIPT_CMD_FAILURE;// not a variable
  6375. }
  6376. id_it = reference_getid(data_it);
  6377. id_nb = reference_getid(data_nb);
  6378. idx_it = reference_getindex(data_it);
  6379. idx_nb = reference_getindex(data_nb);
  6380. name_it = reference_getname(data_it);
  6381. name_nb = reference_getname(data_nb);
  6382. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6383. ShowError("buildin_checkweight2: illegal type, need int\n");
  6384. script_pushint(st,0);
  6385. return SCRIPT_CMD_FAILURE;// not supported
  6386. }
  6387. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6388. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6389. if(nb_it != nb_nb) {
  6390. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6391. fail = 1;
  6392. }
  6393. slots = pc_inventoryblank(sd);
  6394. for(i=0; i<nb_it; i++) {
  6395. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  6396. script_removetop(st, -1, 0);
  6397. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  6398. script_removetop(st, -1, 0);
  6399. if(fail)
  6400. continue; //cpntonie to depop rest
  6401. if(itemdb_exists(nameid) == NULL ) {
  6402. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  6403. fail=1;
  6404. continue;
  6405. }
  6406. if(amount < 0 ) {
  6407. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6408. fail = 1;
  6409. continue;
  6410. }
  6411. weight += itemdb_weight(nameid)*amount;
  6412. if( weight + sd->weight > sd->max_weight ) {
  6413. fail = 1;
  6414. continue;
  6415. }
  6416. switch( pc_checkadditem(sd, nameid, amount) ) {
  6417. case CHKADDITEM_EXIST:
  6418. // item is already in inventory, but there is still space for the requested amount
  6419. break;
  6420. case CHKADDITEM_NEW:
  6421. if( itemdb_isstackable(nameid) ) {// stackable
  6422. amount2++;
  6423. if( slots < amount2 )
  6424. fail = 1;
  6425. } else {// non-stackable
  6426. amount2 += amount;
  6427. if( slots < amount2 ) {
  6428. fail = 1;
  6429. }
  6430. }
  6431. break;
  6432. case CHKADDITEM_OVERAMOUNT:
  6433. fail = 1;
  6434. } //end switch
  6435. } //end loop DO NOT break it prematurly we need to depop all stack
  6436. fail ? script_pushint(st,0) : script_pushint(st,1);
  6437. return SCRIPT_CMD_SUCCESS;
  6438. }
  6439. /*==========================================
  6440. * getitem <item id>,<amount>{,<account ID>};
  6441. * getitem "<item name>",<amount>{,<account ID>};
  6442. *
  6443. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6444. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6445. * Type:
  6446. * 0 - No bound
  6447. * 1 - Account Bound
  6448. * 2 - Guild Bound
  6449. * 3 - Party Bound
  6450. * 4 - Character Bound
  6451. *------------------------------------------*/
  6452. BUILDIN_FUNC(getitem)
  6453. {
  6454. int get_count, i;
  6455. unsigned short nameid, amount;
  6456. struct item it;
  6457. TBL_PC *sd;
  6458. struct script_data *data;
  6459. unsigned char flag = 0;
  6460. const char* command = script_getfuncname(st);
  6461. struct item_data *id = NULL;
  6462. data = script_getdata(st,2);
  6463. get_val(st,data);
  6464. if( data_isstring(data) ) {// "<item name>"
  6465. const char *name = conv_str(st,data);
  6466. id = itemdb_searchname(name);
  6467. if( id == NULL ){
  6468. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6469. return SCRIPT_CMD_FAILURE; //No item created.
  6470. }
  6471. nameid = id->nameid;
  6472. } else if( data_isint(data) ) {// <item id>
  6473. nameid = conv_num(st,data);
  6474. if( !(id = itemdb_exists(nameid)) ){
  6475. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6476. return SCRIPT_CMD_FAILURE; //No item created.
  6477. }
  6478. } else {
  6479. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6480. return SCRIPT_CMD_FAILURE;
  6481. }
  6482. // <amount>
  6483. if( (amount = script_getnum(st,3)) <= 0)
  6484. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6485. memset(&it,0,sizeof(it));
  6486. it.nameid = nameid;
  6487. it.identify = 1;
  6488. it.bound = BOUND_NONE;
  6489. if( !strcmp(command,"getitembound") ) {
  6490. char bound = script_getnum(st,4);
  6491. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6492. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6493. return SCRIPT_CMD_FAILURE;
  6494. }
  6495. script_mapid2sd(5,sd);
  6496. it.bound = bound;
  6497. } else {
  6498. script_mapid2sd(4,sd);
  6499. }
  6500. if( sd == NULL ) // no target
  6501. return SCRIPT_CMD_SUCCESS;
  6502. //Check if it's stackable.
  6503. if (!itemdb_isstackable2(id))
  6504. get_count = 1;
  6505. else
  6506. get_count = amount;
  6507. for (i = 0; i < amount; i += get_count)
  6508. {
  6509. // if not pet egg
  6510. if (!pet_create_egg(sd, nameid))
  6511. {
  6512. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6513. {
  6514. clif_additem(sd, 0, 0, flag);
  6515. if( pc_candrop(sd,&it) )
  6516. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6517. }
  6518. }
  6519. }
  6520. return SCRIPT_CMD_SUCCESS;
  6521. }
  6522. /*==========================================
  6523. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6524. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6525. *
  6526. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6527. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6528. *
  6529. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6530. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6531. *
  6532. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6533. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6534. * Type:
  6535. * 0 - No bound
  6536. * 1 - Account Bound
  6537. * 2 - Guild Bound
  6538. * 3 - Party Bound
  6539. * 4 - Character Bound
  6540. *------------------------------------------*/
  6541. BUILDIN_FUNC(getitem2)
  6542. {
  6543. unsigned short nameid, amount;
  6544. int iden, ref, attr;
  6545. unsigned short c1, c2, c3, c4;
  6546. char bound = BOUND_NONE;
  6547. struct item_data *item_data = NULL;
  6548. struct item item_tmp;
  6549. TBL_PC *sd;
  6550. struct script_data *data;
  6551. const char* command = script_getfuncname(st);
  6552. int offset = 0;
  6553. if( !strncmp(command,"getitembound",12) ) {
  6554. int aid_pos = 12;
  6555. bound = script_getnum(st,11);
  6556. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6557. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6558. return SCRIPT_CMD_FAILURE;
  6559. }
  6560. if (command[strlen(command)-1] == '3') {
  6561. offset = 12;
  6562. aid_pos = 15;
  6563. }
  6564. script_mapid2sd(aid_pos,sd);
  6565. } else {
  6566. int aid_pos = 11;
  6567. if (strcmpi(command,"getitem3") == 0) {
  6568. offset = 11;
  6569. aid_pos = 14;
  6570. }
  6571. script_mapid2sd(aid_pos,sd);
  6572. }
  6573. if( sd == NULL ) // no target
  6574. return SCRIPT_CMD_SUCCESS;
  6575. data = script_getdata(st,2);
  6576. get_val(st,data);
  6577. if( data_isstring(data) ) {
  6578. const char *name = conv_str(st,data);
  6579. if( (item_data = itemdb_searchname(name)) == NULL ){
  6580. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6581. return SCRIPT_CMD_FAILURE; //No item created.
  6582. }
  6583. nameid = item_data->nameid;
  6584. } else {
  6585. nameid = conv_num(st,data);
  6586. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6587. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6588. return SCRIPT_CMD_FAILURE; //No item created.
  6589. }
  6590. }
  6591. amount = script_getnum(st,3);
  6592. iden = script_getnum(st,4);
  6593. ref = script_getnum(st,5);
  6594. attr = script_getnum(st,6);
  6595. c1 = (unsigned short)script_getnum(st,7);
  6596. c2 = (unsigned short)script_getnum(st,8);
  6597. c3 = (unsigned short)script_getnum(st,9);
  6598. c4 = (unsigned short)script_getnum(st,10);
  6599. if( item_data ) {
  6600. int get_count = 0, i;
  6601. memset(&item_tmp,0,sizeof(item_tmp));
  6602. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6603. if(ref > MAX_REFINE)
  6604. ref = MAX_REFINE;
  6605. }
  6606. else if( item_data->type == IT_PETEGG ) {
  6607. iden = 1;
  6608. ref = 0;
  6609. }
  6610. else {
  6611. iden = 1;
  6612. ref = attr = 0;
  6613. }
  6614. item_tmp.nameid = nameid;
  6615. item_tmp.identify = iden;
  6616. item_tmp.refine = ref;
  6617. item_tmp.attribute = attr;
  6618. item_tmp.card[0] = c1;
  6619. item_tmp.card[1] = c2;
  6620. item_tmp.card[2] = c3;
  6621. item_tmp.card[3] = c4;
  6622. item_tmp.bound = bound;
  6623. if (offset != 0) {
  6624. int res = script_getitem_randomoption(st, &item_tmp, command, offset);
  6625. if (res == SCRIPT_CMD_FAILURE)
  6626. return SCRIPT_CMD_FAILURE;
  6627. }
  6628. //Check if it's stackable.
  6629. if (!itemdb_isstackable2(item_data))
  6630. get_count = 1;
  6631. else
  6632. get_count = amount;
  6633. for (i = 0; i < amount; i += get_count)
  6634. {
  6635. // if not pet egg
  6636. if (!pet_create_egg(sd, nameid))
  6637. {
  6638. unsigned char flag = 0;
  6639. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6640. {
  6641. clif_additem(sd, 0, 0, flag);
  6642. if( pc_candrop(sd,&item_tmp) )
  6643. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6644. }
  6645. }
  6646. }
  6647. }
  6648. return SCRIPT_CMD_SUCCESS;
  6649. }
  6650. /** Gives rental item to player
  6651. * rentitem <item id>,<seconds>{,<account_id>}
  6652. * rentitem "<item name>",<seconds>{,<account_id>}
  6653. */
  6654. BUILDIN_FUNC(rentitem) {
  6655. struct map_session_data *sd;
  6656. struct script_data *data;
  6657. struct item it;
  6658. int seconds;
  6659. unsigned short nameid = 0;
  6660. unsigned char flag = 0;
  6661. data = script_getdata(st,2);
  6662. get_val(st,data);
  6663. if (!script_accid2sd(4,sd))
  6664. return SCRIPT_CMD_FAILURE;
  6665. if( data_isstring(data) )
  6666. {
  6667. const char *name = conv_str(st,data);
  6668. struct item_data *itd = itemdb_searchname(name);
  6669. if( itd == NULL )
  6670. {
  6671. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6672. return SCRIPT_CMD_FAILURE;
  6673. }
  6674. nameid = itd->nameid;
  6675. }
  6676. else if( data_isint(data) )
  6677. {
  6678. nameid = conv_num(st,data);
  6679. if( nameid == 0 || !itemdb_exists(nameid) )
  6680. {
  6681. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6682. return SCRIPT_CMD_FAILURE;
  6683. }
  6684. }
  6685. else
  6686. {
  6687. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6688. return SCRIPT_CMD_FAILURE;
  6689. }
  6690. seconds = script_getnum(st,3);
  6691. memset(&it, 0, sizeof(it));
  6692. it.nameid = nameid;
  6693. it.identify = 1;
  6694. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6695. it.bound = BOUND_NONE;
  6696. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6697. {
  6698. clif_additem(sd, 0, 0, flag);
  6699. return SCRIPT_CMD_FAILURE;
  6700. }
  6701. return SCRIPT_CMD_SUCCESS;
  6702. }
  6703. /**
  6704. * Gives rental item to player with advanced option
  6705. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6706. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6707. *
  6708. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6709. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6710. */
  6711. BUILDIN_FUNC(rentitem2) {
  6712. struct map_session_data *sd;
  6713. struct script_data *data;
  6714. struct item it;
  6715. struct item_data *id;
  6716. int seconds;
  6717. unsigned short nameid = 0;
  6718. unsigned char flag = 0;
  6719. int iden,ref,attr,c1,c2,c3,c4;
  6720. const char *funcname = script_getfuncname(st);
  6721. data = script_getdata(st,2);
  6722. get_val(st,data);
  6723. if (funcname[strlen(funcname)-1] == '3') {
  6724. if (!script_accid2sd(14,sd))
  6725. return SCRIPT_CMD_FAILURE;
  6726. } else if (!script_accid2sd(11,sd))
  6727. return SCRIPT_CMD_FAILURE;
  6728. if( data_isstring(data) ) {
  6729. const char *name = conv_str(st,data);
  6730. id = itemdb_searchname(name);
  6731. if( id == NULL ) {
  6732. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6733. return SCRIPT_CMD_FAILURE;
  6734. }
  6735. nameid = id->nameid;
  6736. }
  6737. else if( data_isint(data) ) {
  6738. nameid = conv_num(st,data);
  6739. if( !(id = itemdb_search(nameid))) {
  6740. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6741. return SCRIPT_CMD_FAILURE;
  6742. }
  6743. }
  6744. else {
  6745. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6746. return SCRIPT_CMD_FAILURE;
  6747. }
  6748. seconds = script_getnum(st,3);
  6749. iden = script_getnum(st,4);
  6750. ref = script_getnum(st,5);
  6751. attr = script_getnum(st,6);
  6752. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6753. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6754. }
  6755. else if (id->type==IT_PETEGG) {
  6756. iden = 1;
  6757. ref = 0;
  6758. }
  6759. else {
  6760. iden = 1;
  6761. ref = attr = 0;
  6762. }
  6763. c1 = (short)script_getnum(st,7);
  6764. c2 = (short)script_getnum(st,8);
  6765. c3 = (short)script_getnum(st,9);
  6766. c4 = (short)script_getnum(st,10);
  6767. memset(&it, 0, sizeof(it));
  6768. it.nameid = nameid;
  6769. it.identify = iden;
  6770. it.refine = ref;
  6771. it.attribute = attr;
  6772. it.card[0] = (short)c1;
  6773. it.card[1] = (short)c2;
  6774. it.card[2] = (short)c3;
  6775. it.card[3] = (short)c4;
  6776. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6777. if (funcname[strlen(funcname)-1] == '3') {
  6778. int res = script_getitem_randomoption(st, &it, funcname, 11);
  6779. if (res != SCRIPT_CMD_SUCCESS)
  6780. return res;
  6781. }
  6782. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6783. clif_additem(sd, 0, 0, flag);
  6784. return SCRIPT_CMD_FAILURE;
  6785. }
  6786. return SCRIPT_CMD_SUCCESS;
  6787. }
  6788. /*==========================================
  6789. * gets an item with someone's name inscribed [Skotlex]
  6790. * getinscribeditem item_num, character_name
  6791. * Returned Qty is always 1, only works on equip-able
  6792. * equipment
  6793. *------------------------------------------*/
  6794. BUILDIN_FUNC(getnameditem)
  6795. {
  6796. unsigned short nameid;
  6797. struct item item_tmp;
  6798. TBL_PC *sd, *tsd;
  6799. struct script_data *data;
  6800. if (!script_rid2sd(sd))
  6801. { //Player not attached!
  6802. script_pushint(st,0);
  6803. return SCRIPT_CMD_SUCCESS;
  6804. }
  6805. data=script_getdata(st,2);
  6806. get_val(st,data);
  6807. if( data_isstring(data) ){
  6808. const char *name=conv_str(st,data);
  6809. struct item_data *item_data = itemdb_searchname(name);
  6810. if( item_data == NULL)
  6811. { //Failed
  6812. script_pushint(st,0);
  6813. return SCRIPT_CMD_SUCCESS;
  6814. }
  6815. nameid = item_data->nameid;
  6816. }else
  6817. nameid = conv_num(st,data);
  6818. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6819. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6820. script_pushint(st,0);
  6821. return SCRIPT_CMD_SUCCESS;
  6822. }
  6823. data=script_getdata(st,3);
  6824. get_val(st,data);
  6825. if( data_isstring(data) ) //Char Name
  6826. tsd=map_nick2sd(conv_str(st,data),false);
  6827. else //Char Id was given
  6828. tsd=map_charid2sd(conv_num(st,data));
  6829. if( tsd == NULL )
  6830. { //Failed
  6831. script_pushint(st,0);
  6832. return SCRIPT_CMD_SUCCESS;
  6833. }
  6834. memset(&item_tmp,0,sizeof(item_tmp));
  6835. item_tmp.nameid=nameid;
  6836. item_tmp.amount=1;
  6837. item_tmp.identify=1;
  6838. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6839. item_tmp.card[2]=tsd->status.char_id;
  6840. item_tmp.card[3]=tsd->status.char_id >> 16;
  6841. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6842. script_pushint(st,0);
  6843. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6844. }
  6845. script_pushint(st,1);
  6846. return SCRIPT_CMD_SUCCESS;
  6847. }
  6848. /*==========================================
  6849. * gets a random item ID from an item group [Skotlex]
  6850. * groupranditem <group_num>{,<sub_group>};
  6851. *------------------------------------------*/
  6852. BUILDIN_FUNC(grouprandomitem) {
  6853. struct s_item_group_entry *entry = NULL;
  6854. int sub_group = 1;
  6855. FETCH(3, sub_group);
  6856. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6857. if (!entry) {
  6858. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6859. script_pushint(st,UNKNOWN_ITEM_ID);
  6860. return SCRIPT_CMD_FAILURE;
  6861. }
  6862. script_pushint(st,entry->nameid);
  6863. return SCRIPT_CMD_SUCCESS;
  6864. }
  6865. /**
  6866. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6867. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6868. */
  6869. BUILDIN_FUNC(makeitem) {
  6870. uint16 nameid, amount, flag = 0, x, y;
  6871. const char *mapname;
  6872. int m;
  6873. struct item item_tmp;
  6874. struct script_data *data;
  6875. data = script_getdata(st,2);
  6876. get_val(st,data);
  6877. if( data_isstring(data) ){
  6878. const char *name = conv_str(st,data);
  6879. struct item_data *item_data = itemdb_searchname(name);
  6880. if( item_data )
  6881. nameid = item_data->nameid;
  6882. else
  6883. nameid = UNKNOWN_ITEM_ID;
  6884. }
  6885. else
  6886. nameid = conv_num(st,data);
  6887. amount = script_getnum(st,3);
  6888. mapname = script_getstr(st,4);
  6889. x = script_getnum(st,5);
  6890. y = script_getnum(st,6);
  6891. if(strcmp(mapname,"this")==0) {
  6892. TBL_PC *sd;
  6893. if (!script_rid2sd(sd))
  6894. return SCRIPT_CMD_SUCCESS; //Failed...
  6895. m = sd->bl.m;
  6896. } else
  6897. m = map_mapname2mapid(mapname);
  6898. if(nameid<0) {
  6899. nameid = -nameid;
  6900. flag = 1;
  6901. }
  6902. if(nameid > 0) {
  6903. memset(&item_tmp,0,sizeof(item_tmp));
  6904. item_tmp.nameid = nameid;
  6905. if(!flag)
  6906. item_tmp.identify = 1;
  6907. else
  6908. item_tmp.identify = itemdb_isidentified(nameid);
  6909. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6910. }
  6911. return SCRIPT_CMD_SUCCESS;
  6912. }
  6913. /**
  6914. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6915. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6916. *
  6917. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6918. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6919. */
  6920. BUILDIN_FUNC(makeitem2) {
  6921. uint16 nameid, amount, x, y;
  6922. const char *mapname;
  6923. int m;
  6924. struct item item_tmp;
  6925. struct script_data *data;
  6926. struct item_data *id;
  6927. const char *funcname = script_getfuncname(st);
  6928. data = script_getdata(st,2);
  6929. get_val(st,data);
  6930. if( data_isstring(data) ){
  6931. const char *name = conv_str(st,data);
  6932. struct item_data *item_data = itemdb_searchname(name);
  6933. if( item_data )
  6934. nameid = item_data->nameid;
  6935. else
  6936. nameid = UNKNOWN_ITEM_ID;
  6937. }
  6938. else
  6939. nameid = conv_num(st,data);
  6940. amount = script_getnum(st,3);
  6941. mapname = script_getstr(st,4);
  6942. x = script_getnum(st,5);
  6943. y = script_getnum(st,6);
  6944. if (strcmp(mapname,"this")==0) {
  6945. TBL_PC *sd;
  6946. if (!script_rid2sd(sd))
  6947. return SCRIPT_CMD_SUCCESS; //Failed...
  6948. m = sd->bl.m;
  6949. }
  6950. else
  6951. m = map_mapname2mapid(mapname);
  6952. if ((id = itemdb_search(nameid))) {
  6953. char iden, ref, attr;
  6954. memset(&item_tmp,0,sizeof(item_tmp));
  6955. item_tmp.nameid = nameid;
  6956. iden = (char)script_getnum(st,7);
  6957. ref = (char)script_getnum(st,8);
  6958. attr = (char)script_getnum(st,9);
  6959. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6960. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6961. }
  6962. else if (id->type==IT_PETEGG) {
  6963. iden = 1;
  6964. ref = 0;
  6965. }
  6966. else {
  6967. iden = 1;
  6968. ref = attr = 0;
  6969. }
  6970. item_tmp.identify = iden;
  6971. item_tmp.refine = ref;
  6972. item_tmp.attribute = attr;
  6973. item_tmp.card[0] = script_getnum(st,10);
  6974. item_tmp.card[1] = script_getnum(st,11);
  6975. item_tmp.card[2] = script_getnum(st,12);
  6976. item_tmp.card[3] = script_getnum(st,13);
  6977. if (funcname[strlen(funcname)-1] == '3') {
  6978. int res = script_getitem_randomoption(st, &item_tmp, funcname, 14);
  6979. if (res != SCRIPT_CMD_SUCCESS)
  6980. return res;
  6981. }
  6982. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6983. }
  6984. else
  6985. return SCRIPT_CMD_FAILURE;
  6986. return SCRIPT_CMD_SUCCESS;
  6987. }
  6988. /// Counts / deletes the current item given by idx.
  6989. /// Used by buildin_delitem_search
  6990. /// Relies on all input data being already fully valid.
  6991. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6992. {
  6993. int delamount;
  6994. struct item *itm = NULL;
  6995. struct s_storage *gstor = NULL;
  6996. switch(loc) {
  6997. case TABLE_CART:
  6998. itm = &sd->cart.u.items_cart[idx];
  6999. break;
  7000. case TABLE_STORAGE:
  7001. itm = &sd->storage.u.items_storage[idx];
  7002. break;
  7003. case TABLE_GUILD_STORAGE:
  7004. {
  7005. gstor = guild2storage2(sd->status.guild_id);
  7006. itm = &gstor->u.items_guild[idx];
  7007. }
  7008. break;
  7009. default: // TABLE_INVENTORY
  7010. itm = &sd->inventory.u.items_inventory[idx];
  7011. break;
  7012. }
  7013. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  7014. if( delete_items )
  7015. {
  7016. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  7017. {// delete associated pet
  7018. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  7019. }
  7020. switch(loc) {
  7021. case TABLE_CART:
  7022. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  7023. break;
  7024. case TABLE_STORAGE:
  7025. storage_delitem(sd,&sd->storage,idx,delamount);
  7026. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  7027. break;
  7028. case TABLE_GUILD_STORAGE:
  7029. gstor->lock = true;
  7030. storage_guild_delitem(sd, gstor, idx, delamount);
  7031. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  7032. storage_guild_storageclose(sd);
  7033. gstor->lock = false;
  7034. break;
  7035. default: // TABLE_INVENTORY
  7036. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  7037. break;
  7038. }
  7039. }
  7040. amount[0]-= delamount;
  7041. }
  7042. /// Searches for item(s) and checks, if there is enough of them.
  7043. /// Used by delitem and delitem2
  7044. /// Relies on all input data being already fully valid.
  7045. /// @param exact_match will also match item by specified attributes
  7046. /// 0x0: Only item id
  7047. /// 0x1: identify, attributes, cards
  7048. /// 0x2: random option
  7049. /// @return true when all items could be deleted, false when there were not enough items to delete
  7050. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  7051. {
  7052. bool delete_items = false;
  7053. int i, amount, size;
  7054. struct item *items;
  7055. // prefer always non-equipped items
  7056. it->equip = 0;
  7057. // when searching for nameid only, prefer additionally
  7058. if( !exact_match )
  7059. {
  7060. // non-refined items
  7061. it->refine = 0;
  7062. // card-less items
  7063. memset(it->card, 0, sizeof(it->card));
  7064. }
  7065. switch(loc) {
  7066. case TABLE_CART:
  7067. size = MAX_CART;
  7068. items = sd->cart.u.items_cart;
  7069. break;
  7070. case TABLE_STORAGE:
  7071. size = MAX_STORAGE;
  7072. items = sd->storage.u.items_storage;
  7073. break;
  7074. case TABLE_GUILD_STORAGE:
  7075. {
  7076. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7077. size = MAX_GUILD_STORAGE;
  7078. items = gstor->u.items_guild;
  7079. }
  7080. break;
  7081. default: // TABLE_INVENTORY
  7082. size = MAX_INVENTORY;
  7083. items = sd->inventory.u.items_inventory;
  7084. break;
  7085. }
  7086. for(;;)
  7087. {
  7088. unsigned short important = 0;
  7089. amount = it->amount;
  7090. // 1st pass -- less important items / exact match
  7091. for( i = 0; amount && i < size; i++ )
  7092. {
  7093. struct item *itm = NULL;
  7094. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7095. {// wrong/invalid item
  7096. continue;
  7097. }
  7098. if( itm->equip != it->equip || itm->refine != it->refine )
  7099. {// not matching attributes
  7100. important++;
  7101. continue;
  7102. }
  7103. if( exact_match )
  7104. {
  7105. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7106. {// not matching exact attributes
  7107. continue;
  7108. }
  7109. if (exact_match&0x2) {
  7110. uint8 j;
  7111. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7112. 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)
  7113. break;
  7114. }
  7115. if (j != MAX_ITEM_RDM_OPT)
  7116. continue;
  7117. }
  7118. }
  7119. else
  7120. {
  7121. if( itemdb_type(itm->nameid) == IT_PETEGG )
  7122. {
  7123. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  7124. {// pet which cannot be deleted
  7125. continue;
  7126. }
  7127. }
  7128. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  7129. {// named/carded item
  7130. important++;
  7131. continue;
  7132. }
  7133. }
  7134. // count / delete item
  7135. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7136. }
  7137. // 2nd pass -- any matching item
  7138. if( amount == 0 || important == 0 )
  7139. {// either everything was already consumed or no items were skipped
  7140. ;
  7141. }
  7142. else for( i = 0; amount && i < size; i++ )
  7143. {
  7144. struct item *itm = NULL;
  7145. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  7146. {// wrong/invalid item
  7147. continue;
  7148. }
  7149. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  7150. {// pet which cannot be deleted
  7151. continue;
  7152. }
  7153. if( exact_match )
  7154. {
  7155. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  7156. {// not matching attributes
  7157. continue;
  7158. }
  7159. if (exact_match&0x2) {
  7160. uint8 j;
  7161. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  7162. 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)
  7163. break;
  7164. }
  7165. if (j != MAX_ITEM_RDM_OPT)
  7166. continue;
  7167. }
  7168. }
  7169. // count / delete item
  7170. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  7171. }
  7172. if( amount )
  7173. {// not enough items
  7174. return false;
  7175. }
  7176. else if( delete_items )
  7177. {// we are done with the work
  7178. return true;
  7179. }
  7180. else
  7181. {// get rid of the items now
  7182. delete_items = true;
  7183. }
  7184. }
  7185. }
  7186. /// Deletes items from the target/attached player.
  7187. /// Prioritizes ordinary items.
  7188. ///
  7189. /// delitem <item id>,<amount>{,<account id>}
  7190. /// delitem "<item name>",<amount>{,<account id>}
  7191. /// cartdelitem <item id>,<amount>{,<account id>}
  7192. /// cartdelitem "<item name>",<amount>{,<account id>}
  7193. /// storagedelitem <item id>,<amount>{,<account id>}
  7194. /// storagedelitem "<item name>",<amount>{,<account id>}
  7195. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  7196. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7197. BUILDIN_FUNC(delitem)
  7198. {
  7199. TBL_PC *sd;
  7200. struct item it;
  7201. struct script_data *data;
  7202. uint8 loc = 0;
  7203. char* command = (char*)script_getfuncname(st);
  7204. if(!strncmp(command, "cart", 4))
  7205. loc = TABLE_CART;
  7206. else if(!strncmp(command, "storage", 7))
  7207. loc = TABLE_STORAGE;
  7208. else if(!strncmp(command, "guildstorage", 12))
  7209. loc = TABLE_GUILD_STORAGE;
  7210. if( !script_accid2sd(4,sd) ){
  7211. // In any case cancel script execution
  7212. st->state = END;
  7213. return SCRIPT_CMD_SUCCESS;
  7214. }
  7215. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7216. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7217. script_pushint(st, -1);
  7218. return SCRIPT_CMD_FAILURE;
  7219. }
  7220. if (loc == TABLE_GUILD_STORAGE) {
  7221. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7222. if (gstor == NULL || sd->state.storage_flag) {
  7223. script_pushint(st, -1);
  7224. return SCRIPT_CMD_FAILURE;
  7225. }
  7226. }
  7227. data = script_getdata(st,2);
  7228. get_val(st,data);
  7229. if( data_isstring(data) )
  7230. {
  7231. const char* item_name = conv_str(st,data);
  7232. struct item_data* id = itemdb_searchname(item_name);
  7233. if( id == NULL )
  7234. {
  7235. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7236. st->state = END;
  7237. return SCRIPT_CMD_FAILURE;
  7238. }
  7239. it.nameid = id->nameid;// "<item name>"
  7240. }
  7241. else
  7242. {
  7243. it.nameid = conv_num(st,data);// <item id>
  7244. if( !itemdb_exists( it.nameid ) )
  7245. {
  7246. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  7247. st->state = END;
  7248. return SCRIPT_CMD_FAILURE;
  7249. }
  7250. }
  7251. it.amount=script_getnum(st,3);
  7252. if( it.amount <= 0 )
  7253. return SCRIPT_CMD_SUCCESS;// nothing to do
  7254. if( buildin_delitem_search(sd, &it, 0, loc) )
  7255. {// success
  7256. return SCRIPT_CMD_SUCCESS;
  7257. }
  7258. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  7259. st->state = END;
  7260. st->mes_active = 0;
  7261. clif_scriptclose(sd, st->oid);
  7262. return SCRIPT_CMD_FAILURE;
  7263. }
  7264. /// Deletes items from the target/attached player.
  7265. ///
  7266. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7267. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7268. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7269. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7270. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7271. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7272. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7273. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7274. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7275. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7276. BUILDIN_FUNC(delitem2)
  7277. {
  7278. TBL_PC *sd;
  7279. struct item it;
  7280. struct script_data *data;
  7281. uint8 loc = 0;
  7282. char* command = (char*)script_getfuncname(st);
  7283. int aid_pos = 11;
  7284. uint8 flag = 0x1;
  7285. if(!strncmp(command, "cart", 4))
  7286. loc = TABLE_CART;
  7287. else if(!strncmp(command, "storage", 7))
  7288. loc = TABLE_STORAGE;
  7289. else if(!strncmp(command, "guildstorage", 12))
  7290. loc = TABLE_GUILD_STORAGE;
  7291. if (command[strlen(command)-1] == '3')
  7292. aid_pos = 14;
  7293. if( !script_accid2sd(aid_pos,sd) ){
  7294. // In any case cancel script execution
  7295. st->state = END;
  7296. return SCRIPT_CMD_SUCCESS;
  7297. }
  7298. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7299. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7300. script_pushint(st,-1);
  7301. return SCRIPT_CMD_FAILURE;
  7302. }
  7303. if (loc == TABLE_GUILD_STORAGE) {
  7304. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7305. if (gstor == NULL || sd->state.storage_flag) {
  7306. script_pushint(st, -1);
  7307. return SCRIPT_CMD_FAILURE;
  7308. }
  7309. }
  7310. memset(&it, 0, sizeof(it));
  7311. data = script_getdata(st,2);
  7312. get_val(st,data);
  7313. if( data_isstring(data) )
  7314. {
  7315. const char* item_name = conv_str(st,data);
  7316. struct item_data* id = itemdb_searchname(item_name);
  7317. if( id == NULL )
  7318. {
  7319. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7320. st->state = END;
  7321. return SCRIPT_CMD_FAILURE;
  7322. }
  7323. it.nameid = id->nameid;// "<item name>"
  7324. }
  7325. else
  7326. {
  7327. it.nameid = conv_num(st,data);// <item id>
  7328. if( !itemdb_exists( it.nameid ) )
  7329. {
  7330. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  7331. st->state = END;
  7332. return SCRIPT_CMD_FAILURE;
  7333. }
  7334. }
  7335. it.amount=script_getnum(st,3);
  7336. it.identify=script_getnum(st,4);
  7337. it.refine=script_getnum(st,5);
  7338. it.attribute=script_getnum(st,6);
  7339. it.card[0]=(short)script_getnum(st,7);
  7340. it.card[1]=(short)script_getnum(st,8);
  7341. it.card[2]=(short)script_getnum(st,9);
  7342. it.card[3]=(short)script_getnum(st,10);
  7343. if (command[strlen(command)-1] == '3') {
  7344. int res = script_getitem_randomoption(st, &it, command, 11);
  7345. if (res != SCRIPT_CMD_SUCCESS)
  7346. return SCRIPT_CMD_FAILURE;
  7347. flag |= 0x2;
  7348. }
  7349. if( it.amount <= 0 )
  7350. return SCRIPT_CMD_SUCCESS;// nothing to do
  7351. if( buildin_delitem_search(sd, &it, flag, loc) )
  7352. {// success
  7353. return SCRIPT_CMD_SUCCESS;
  7354. }
  7355. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  7356. st->state = END;
  7357. st->mes_active = 0;
  7358. clif_scriptclose(sd, st->oid);
  7359. return SCRIPT_CMD_FAILURE;
  7360. }
  7361. /*==========================================
  7362. * Enables/Disables use of items while in an NPC [Skotlex]
  7363. *------------------------------------------*/
  7364. BUILDIN_FUNC(enableitemuse)
  7365. {
  7366. TBL_PC *sd;
  7367. if (script_rid2sd(sd))
  7368. st->npc_item_flag = sd->npc_item_flag = 1;
  7369. return SCRIPT_CMD_SUCCESS;
  7370. }
  7371. BUILDIN_FUNC(disableitemuse)
  7372. {
  7373. TBL_PC *sd;
  7374. if (script_rid2sd(sd))
  7375. st->npc_item_flag = sd->npc_item_flag = 0;
  7376. return SCRIPT_CMD_SUCCESS;
  7377. }
  7378. /*==========================================
  7379. * Returns a character's specified stat.
  7380. * Check pc_readparam for available options.
  7381. * readparam <param>{,"<nick>"}
  7382. * readparam <param>{,<char_id>}
  7383. *------------------------------------------*/
  7384. BUILDIN_FUNC(readparam)
  7385. {
  7386. int value;
  7387. struct script_data *data = script_getdata(st, 2);
  7388. TBL_PC *sd = NULL;
  7389. if( script_hasdata(st, 3) ){
  7390. struct script_data *data2 = script_getdata(st, 3);
  7391. get_val(st, data2);
  7392. if (data_isint(data2) || script_getnum(st, 3)) {
  7393. script_charid2sd(3, sd);
  7394. } else if (data_isstring(data2)) {
  7395. script_nick2sd(3, sd);
  7396. }
  7397. }else{
  7398. script_rid2sd(sd);
  7399. }
  7400. if( !sd ){
  7401. script_pushint(st, -1);
  7402. return SCRIPT_CMD_FAILURE;
  7403. }
  7404. // If you use a parameter, return the value behind it
  7405. if( reference_toparam(data) ){
  7406. get_val_(st, data, sd);
  7407. value = (int)data->u.num;
  7408. }else{
  7409. value = pc_readparam(sd,script_getnum(st, 2));
  7410. }
  7411. script_pushint(st,value);
  7412. return SCRIPT_CMD_SUCCESS;
  7413. }
  7414. /*==========================================
  7415. * Return charid identification
  7416. * return by @num :
  7417. * 0 : char_id
  7418. * 1 : party_id
  7419. * 2 : guild_id
  7420. * 3 : account_id
  7421. * 4 : bg_id
  7422. * 5 : clan_id
  7423. *------------------------------------------*/
  7424. BUILDIN_FUNC(getcharid)
  7425. {
  7426. int num;
  7427. TBL_PC *sd;
  7428. num = script_getnum(st,2);
  7429. if( !script_nick2sd(3,sd) ){
  7430. script_pushint(st,0); //return 0, according docs
  7431. return SCRIPT_CMD_SUCCESS;
  7432. }
  7433. switch( num ) {
  7434. case 0: script_pushint(st,sd->status.char_id); break;
  7435. case 1: script_pushint(st,sd->status.party_id); break;
  7436. case 2: script_pushint(st,sd->status.guild_id); break;
  7437. case 3: script_pushint(st,sd->status.account_id); break;
  7438. case 4: script_pushint(st,sd->bg_id); break;
  7439. case 5: script_pushint(st,sd->status.clan_id); break;
  7440. default:
  7441. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7442. script_pushint(st,0);
  7443. break;
  7444. }
  7445. return SCRIPT_CMD_SUCCESS;
  7446. }
  7447. /*==========================================
  7448. * returns the GID of an NPC
  7449. *------------------------------------------*/
  7450. BUILDIN_FUNC(getnpcid)
  7451. {
  7452. int num = script_getnum(st,2);
  7453. struct npc_data* nd = NULL;
  7454. if( script_hasdata(st,3) )
  7455. {// unique npc name
  7456. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7457. {
  7458. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7459. script_pushint(st,0);
  7460. return SCRIPT_CMD_FAILURE;
  7461. }
  7462. }
  7463. switch (num) {
  7464. case 0:
  7465. script_pushint(st,nd ? nd->bl.id : st->oid);
  7466. break;
  7467. default:
  7468. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7469. script_pushint(st,0);
  7470. return SCRIPT_CMD_FAILURE;
  7471. }
  7472. return SCRIPT_CMD_SUCCESS;
  7473. }
  7474. /*==========================================
  7475. * Return the name of the party_id
  7476. * null if not found
  7477. *------------------------------------------*/
  7478. BUILDIN_FUNC(getpartyname)
  7479. {
  7480. int party_id;
  7481. struct party_data* p;
  7482. party_id = script_getnum(st,2);
  7483. if( ( p = party_search(party_id) ) != NULL )
  7484. {
  7485. script_pushstrcopy(st,p->party.name);
  7486. }
  7487. else
  7488. {
  7489. script_pushconststr(st,"null");
  7490. }
  7491. return SCRIPT_CMD_SUCCESS;
  7492. }
  7493. /*==========================================
  7494. * Get the information of the members of a party by type
  7495. * @party_id, @type
  7496. * return by @type :
  7497. * - : nom des membres
  7498. * 1 : char_id des membres
  7499. * 2 : account_id des membres
  7500. *------------------------------------------*/
  7501. BUILDIN_FUNC(getpartymember)
  7502. {
  7503. struct party_data *p;
  7504. uint8 j = 0;
  7505. p = party_search(script_getnum(st,2));
  7506. if (p != NULL) {
  7507. uint8 i, type = 0;
  7508. struct script_data *data = NULL;
  7509. char *varname = NULL;
  7510. if (script_hasdata(st,3))
  7511. type = script_getnum(st,3);
  7512. if (script_hasdata(st,4)) {
  7513. data = script_getdata(st, 4);
  7514. if (!data_isreference(data)) {
  7515. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7516. return SCRIPT_CMD_FAILURE;
  7517. }
  7518. varname = reference_getname(data);
  7519. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7520. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7521. return SCRIPT_CMD_FAILURE;
  7522. }
  7523. }
  7524. for (i = 0; i < MAX_PARTY; i++) {
  7525. if (p->party.member[i].account_id) {
  7526. switch (type) {
  7527. case 2:
  7528. if (data)
  7529. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  7530. else
  7531. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7532. break;
  7533. case 1:
  7534. if (data)
  7535. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7536. else
  7537. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7538. break;
  7539. default:
  7540. if (data)
  7541. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7542. else
  7543. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7544. break;
  7545. }
  7546. j++;
  7547. }
  7548. }
  7549. }
  7550. mapreg_setreg(add_str("$@partymembercount"),j);
  7551. script_pushint(st, j);
  7552. return SCRIPT_CMD_SUCCESS;
  7553. }
  7554. /*==========================================
  7555. * Retrieves party leader. if flag is specified,
  7556. * return some of the leader data. Otherwise, return name.
  7557. *------------------------------------------*/
  7558. BUILDIN_FUNC(getpartyleader)
  7559. {
  7560. int party_id, type = 0, i=0;
  7561. struct party_data *p;
  7562. party_id=script_getnum(st,2);
  7563. if( script_hasdata(st,3) )
  7564. type=script_getnum(st,3);
  7565. p=party_search(party_id);
  7566. if (p) //Search leader
  7567. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7568. if (!p || i == MAX_PARTY) { //leader not found
  7569. if (type)
  7570. script_pushint(st,-1);
  7571. else
  7572. script_pushconststr(st,"null");
  7573. return SCRIPT_CMD_SUCCESS;
  7574. }
  7575. switch (type) {
  7576. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7577. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7578. case 3: script_pushint(st,p->party.member[i].class_); break;
  7579. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7580. case 5: script_pushint(st,p->party.member[i].lv); break;
  7581. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7582. }
  7583. return SCRIPT_CMD_SUCCESS;
  7584. }
  7585. /*==========================================
  7586. * Return the name of the @guild_id
  7587. * null if not found
  7588. *------------------------------------------*/
  7589. BUILDIN_FUNC(getguildname)
  7590. {
  7591. int guild_id;
  7592. struct guild* g;
  7593. guild_id = script_getnum(st,2);
  7594. if( ( g = guild_search(guild_id) ) != NULL )
  7595. script_pushstrcopy(st,g->name);
  7596. else
  7597. script_pushconststr(st,"null");
  7598. return SCRIPT_CMD_SUCCESS;
  7599. }
  7600. /*==========================================
  7601. * Return the name of the guild master of @guild_id
  7602. * null if not found
  7603. *------------------------------------------*/
  7604. BUILDIN_FUNC(getguildmaster)
  7605. {
  7606. int guild_id;
  7607. struct guild* g;
  7608. guild_id = script_getnum(st,2);
  7609. if( ( g = guild_search(guild_id) ) != NULL )
  7610. script_pushstrcopy(st,g->member[0].name);
  7611. else
  7612. script_pushconststr(st,"null");
  7613. return SCRIPT_CMD_SUCCESS;
  7614. }
  7615. BUILDIN_FUNC(getguildmasterid)
  7616. {
  7617. int guild_id;
  7618. struct guild* g;
  7619. guild_id = script_getnum(st,2);
  7620. if( ( g = guild_search(guild_id) ) != NULL )
  7621. script_pushint(st,g->member[0].char_id);
  7622. else
  7623. script_pushint(st,0);
  7624. return SCRIPT_CMD_SUCCESS;
  7625. }
  7626. /*==========================================
  7627. * Get char string information by type :
  7628. * Return by @type :
  7629. * 0 : char_name
  7630. * 1 : party_name or ""
  7631. * 2 : guild_name or ""
  7632. * 3 : map_name
  7633. * - : ""
  7634. * strcharinfo(<type>{,<char_id>})
  7635. *------------------------------------------*/
  7636. BUILDIN_FUNC(strcharinfo)
  7637. {
  7638. TBL_PC *sd;
  7639. int num;
  7640. struct guild* g;
  7641. struct party_data* p;
  7642. if (!script_charid2sd(3,sd)) {
  7643. script_pushconststr(st,"");
  7644. return SCRIPT_CMD_FAILURE;
  7645. }
  7646. num=script_getnum(st,2);
  7647. switch(num){
  7648. case 0:
  7649. script_pushstrcopy(st,sd->status.name);
  7650. break;
  7651. case 1:
  7652. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7653. script_pushstrcopy(st,p->party.name);
  7654. } else {
  7655. script_pushconststr(st,"");
  7656. }
  7657. break;
  7658. case 2:
  7659. if( ( g = sd->guild ) != NULL ) {
  7660. script_pushstrcopy(st,g->name);
  7661. } else {
  7662. script_pushconststr(st,"");
  7663. }
  7664. break;
  7665. case 3:
  7666. script_pushconststr(st,map[sd->bl.m].name);
  7667. break;
  7668. default:
  7669. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7670. script_pushconststr(st,"");
  7671. break;
  7672. }
  7673. return SCRIPT_CMD_SUCCESS;
  7674. }
  7675. /*==========================================
  7676. * Get npc string information by type
  7677. * return by @type:
  7678. * 0 : name
  7679. * 1 : str#
  7680. * 2 : #str
  7681. * 3 : ::str
  7682. * 4 : map name
  7683. *------------------------------------------*/
  7684. BUILDIN_FUNC(strnpcinfo)
  7685. {
  7686. TBL_NPC* nd;
  7687. int num;
  7688. char *buf,*name=NULL;
  7689. nd = map_id2nd(st->oid);
  7690. if (!nd) {
  7691. script_pushconststr(st, "");
  7692. return SCRIPT_CMD_SUCCESS;
  7693. }
  7694. num = script_getnum(st,2);
  7695. switch(num){
  7696. case 0: // display name
  7697. name = aStrdup(nd->name);
  7698. break;
  7699. case 1: // visible part of display name
  7700. if((buf = strchr(nd->name,'#')) != NULL)
  7701. {
  7702. name = aStrdup(nd->name);
  7703. name[buf - nd->name] = 0;
  7704. } else // Return the name, there is no '#' present
  7705. name = aStrdup(nd->name);
  7706. break;
  7707. case 2: // # fragment
  7708. if((buf = strchr(nd->name,'#')) != NULL)
  7709. name = aStrdup(buf+1);
  7710. break;
  7711. case 3: // unique name
  7712. name = aStrdup(nd->exname);
  7713. break;
  7714. case 4: // map name
  7715. if (nd->bl.m >= 0)
  7716. name = aStrdup(map[nd->bl.m].name);
  7717. break;
  7718. }
  7719. if(name)
  7720. script_pushstr(st, name);
  7721. else
  7722. script_pushconststr(st, "");
  7723. return SCRIPT_CMD_SUCCESS;
  7724. }
  7725. /**
  7726. * getequipid({<equipment slot>,<char_id>})
  7727. **/
  7728. BUILDIN_FUNC(getequipid)
  7729. {
  7730. int i, num;
  7731. TBL_PC* sd;
  7732. if (!script_charid2sd(3, sd)) {
  7733. script_pushint(st,-1);
  7734. return SCRIPT_CMD_FAILURE;
  7735. }
  7736. if (script_hasdata(st, 2))
  7737. num = script_getnum(st, 2);
  7738. else
  7739. num = EQI_COMPOUND_ON;
  7740. if (num == EQI_COMPOUND_ON)
  7741. i = current_equip_item_index;
  7742. else if (equip_index_check(num)) // get inventory position of item
  7743. i = pc_checkequip(sd, equip_bitmask[num]);
  7744. else {
  7745. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7746. script_pushint(st,-1);
  7747. return SCRIPT_CMD_FAILURE;
  7748. }
  7749. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7750. script_pushint(st, sd->inventory_data[i]->nameid);
  7751. else
  7752. script_pushint(st, -1);
  7753. return SCRIPT_CMD_SUCCESS;
  7754. }
  7755. /*==========================================
  7756. * getequipuniqueid(<equipment slot>{,<char_id>})
  7757. *------------------------------------------*/
  7758. BUILDIN_FUNC(getequipuniqueid)
  7759. {
  7760. int i, num;
  7761. TBL_PC* sd;
  7762. struct item* item;
  7763. if (!script_charid2sd(3, sd)) {
  7764. script_pushconststr(st, "");
  7765. return SCRIPT_CMD_FAILURE;
  7766. }
  7767. num = script_getnum(st,2);
  7768. if ( !equip_index_check(num) ) {
  7769. script_pushconststr(st, "");
  7770. return SCRIPT_CMD_FAILURE;
  7771. }
  7772. // get inventory position of item
  7773. i = pc_checkequip(sd,equip_bitmask[num]);
  7774. if (i < 0) {
  7775. script_pushconststr(st, "");
  7776. return SCRIPT_CMD_FAILURE;
  7777. }
  7778. item = &sd->inventory.u.items_inventory[i];
  7779. if (item != 0) {
  7780. int maxlen = 256;
  7781. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7782. memset(buf, 0, maxlen);
  7783. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7784. script_pushstr(st, buf);
  7785. } else
  7786. script_pushconststr(st, "");
  7787. return SCRIPT_CMD_SUCCESS;
  7788. }
  7789. /*==========================================
  7790. * Get the equipement name at pos
  7791. * return item jname or ""
  7792. * getequipname(<equipment slot>{,<char_id>})
  7793. *------------------------------------------*/
  7794. BUILDIN_FUNC(getequipname)
  7795. {
  7796. int i, num;
  7797. TBL_PC* sd;
  7798. struct item_data* item;
  7799. if (!script_charid2sd(3, sd)) {
  7800. script_pushconststr(st, "");
  7801. return SCRIPT_CMD_FAILURE;
  7802. }
  7803. num = script_getnum(st,2);
  7804. if( !equip_index_check(num) )
  7805. {
  7806. script_pushconststr(st,"");
  7807. return SCRIPT_CMD_SUCCESS;
  7808. }
  7809. // get inventory position of item
  7810. i = pc_checkequip(sd,equip_bitmask[num]);
  7811. if( i < 0 )
  7812. {
  7813. script_pushconststr(st,"");
  7814. return SCRIPT_CMD_SUCCESS;
  7815. }
  7816. item = sd->inventory_data[i];
  7817. if( item != 0 )
  7818. script_pushstrcopy(st,item->jname);
  7819. else
  7820. script_pushconststr(st,"");
  7821. return SCRIPT_CMD_SUCCESS;
  7822. }
  7823. /*==========================================
  7824. * getbrokenid [Valaris]
  7825. * getbrokenid(<number>{,<char_id>})
  7826. *------------------------------------------*/
  7827. BUILDIN_FUNC(getbrokenid)
  7828. {
  7829. int i,num,id = 0,brokencounter = 0;
  7830. TBL_PC *sd;
  7831. if (!script_charid2sd(3, sd)) {
  7832. script_pushint(st,0);
  7833. return SCRIPT_CMD_FAILURE;
  7834. }
  7835. num = script_getnum(st,2);
  7836. for(i = 0; i < MAX_INVENTORY; i++) {
  7837. if(sd->inventory.u.items_inventory[i].attribute) {
  7838. brokencounter++;
  7839. if(num == brokencounter){
  7840. id = sd->inventory.u.items_inventory[i].nameid;
  7841. break;
  7842. }
  7843. }
  7844. }
  7845. script_pushint(st,id);
  7846. return SCRIPT_CMD_SUCCESS;
  7847. }
  7848. /*==========================================
  7849. * repair [Valaris]
  7850. * repair <num>{,<char_id>};
  7851. *------------------------------------------*/
  7852. BUILDIN_FUNC(repair)
  7853. {
  7854. int i,num;
  7855. int repaircounter = 0;
  7856. TBL_PC *sd;
  7857. if (!script_charid2sd(3,sd))
  7858. return SCRIPT_CMD_FAILURE;
  7859. num = script_getnum(st,2);
  7860. for(i = 0; i < MAX_INVENTORY; i++) {
  7861. if(sd->inventory.u.items_inventory[i].attribute) {
  7862. repaircounter++;
  7863. if(num == repaircounter) {
  7864. sd->inventory.u.items_inventory[i].attribute = 0;
  7865. clif_equiplist(sd);
  7866. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7867. clif_misceffect(&sd->bl, 3);
  7868. break;
  7869. }
  7870. }
  7871. }
  7872. return SCRIPT_CMD_SUCCESS;
  7873. }
  7874. /*==========================================
  7875. * repairall {<char_id>}
  7876. *------------------------------------------*/
  7877. BUILDIN_FUNC(repairall)
  7878. {
  7879. int i, repaircounter = 0;
  7880. TBL_PC *sd;
  7881. if (!script_charid2sd(2,sd))
  7882. return SCRIPT_CMD_FAILURE;
  7883. for(i = 0; i < MAX_INVENTORY; i++)
  7884. {
  7885. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7886. {
  7887. sd->inventory.u.items_inventory[i].attribute = 0;
  7888. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7889. repaircounter++;
  7890. }
  7891. }
  7892. if(repaircounter)
  7893. {
  7894. clif_misceffect(&sd->bl, 3);
  7895. clif_equiplist(sd);
  7896. }
  7897. return SCRIPT_CMD_SUCCESS;
  7898. }
  7899. /*==========================================
  7900. * Chk if player have something equiped at pos
  7901. * getequipisequiped <pos>{,<char_id>}
  7902. *------------------------------------------*/
  7903. BUILDIN_FUNC(getequipisequiped)
  7904. {
  7905. int i = -1,num;
  7906. TBL_PC *sd;
  7907. num = script_getnum(st,2);
  7908. if (!script_charid2sd(3,sd))
  7909. return SCRIPT_CMD_FAILURE;
  7910. if ( equip_index_check(num) )
  7911. i=pc_checkequip(sd,equip_bitmask[num]);
  7912. if(i >= 0)
  7913. script_pushint(st,1);
  7914. else
  7915. script_pushint(st,0);
  7916. return SCRIPT_CMD_SUCCESS;
  7917. }
  7918. /*==========================================
  7919. * Chk if the player have something equiped at pos
  7920. * if so chk if this item ain't marked not refinable or rental
  7921. * return (npc)
  7922. * 1 : true
  7923. * 0 : false
  7924. * getequipisenableref(<equipment slot>{,<char_id>})
  7925. *------------------------------------------*/
  7926. BUILDIN_FUNC(getequipisenableref)
  7927. {
  7928. int i = -1,num;
  7929. TBL_PC *sd;
  7930. num = script_getnum(st,2);
  7931. if (!script_charid2sd(3, sd)) {
  7932. script_pushint(st,0);
  7933. return SCRIPT_CMD_FAILURE;
  7934. }
  7935. if( equip_index_check(num) )
  7936. i = pc_checkequip(sd,equip_bitmask[num]);
  7937. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7938. script_pushint(st,1);
  7939. else
  7940. script_pushint(st,0);
  7941. return SCRIPT_CMD_SUCCESS;
  7942. }
  7943. /*==========================================
  7944. * Get the item refined value at pos
  7945. * return (npc)
  7946. * x : refine amount
  7947. * 0 : false (not refined)
  7948. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7949. *------------------------------------------*/
  7950. BUILDIN_FUNC(getequiprefinerycnt)
  7951. {
  7952. int i = -1,num;
  7953. TBL_PC *sd;
  7954. num = script_getnum(st,2);
  7955. if (!script_charid2sd(3, sd)) {
  7956. script_pushint(st,0);
  7957. return SCRIPT_CMD_FAILURE;
  7958. }
  7959. if (equip_index_check(num))
  7960. i=pc_checkequip(sd,equip_bitmask[num]);
  7961. if(i >= 0)
  7962. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7963. else
  7964. script_pushint(st,0);
  7965. return SCRIPT_CMD_SUCCESS;
  7966. }
  7967. /*==========================================
  7968. * Get the weapon level value at pos
  7969. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7970. * return (npc)
  7971. * x : weapon level
  7972. * 0 : false
  7973. * getequipweaponlv(<equipment slot>{,<char_id>})
  7974. *------------------------------------------*/
  7975. BUILDIN_FUNC(getequipweaponlv)
  7976. {
  7977. int i = -1, num;
  7978. TBL_PC *sd;
  7979. num = script_getnum(st, 2);
  7980. if (!script_charid2sd(3, sd)) {
  7981. script_pushint(st, 0);
  7982. return SCRIPT_CMD_FAILURE;
  7983. }
  7984. if (num == -1){
  7985. if( current_equip_item_index == -1 ){
  7986. script_pushint(st, 0);
  7987. return SCRIPT_CMD_FAILURE;
  7988. }
  7989. i = current_equip_item_index;
  7990. }else if (equip_index_check(num))
  7991. i = pc_checkequip(sd, equip_bitmask[num]);
  7992. if (i >= 0 && sd->inventory_data[i])
  7993. script_pushint(st, sd->inventory_data[i]->wlv);
  7994. else
  7995. script_pushint(st, 0);
  7996. return SCRIPT_CMD_SUCCESS;
  7997. }
  7998. /*==========================================
  7999. * Get the item refine chance (from refine.txt) for item at pos
  8000. * return (npc)
  8001. * x : refine chance
  8002. * 0 : false (max refine level or unequip..)
  8003. * getequippercentrefinery(<equipment slot>{,<enriched>,<char_id>})
  8004. *------------------------------------------*/
  8005. BUILDIN_FUNC(getequippercentrefinery)
  8006. {
  8007. int i = -1,num;
  8008. bool enriched = false;
  8009. TBL_PC *sd;
  8010. num = script_getnum(st,2);
  8011. if (script_hasdata(st, 3))
  8012. enriched = script_getnum(st, 3) != 0;
  8013. if (!script_charid2sd(4, sd)) {
  8014. script_pushint(st,0);
  8015. return SCRIPT_CMD_FAILURE;
  8016. }
  8017. if (equip_index_check(num))
  8018. i = pc_checkequip(sd,equip_bitmask[num]);
  8019. if (i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE) {
  8020. enum refine_type type = REFINE_TYPE_SHADOW;
  8021. if (sd->inventory_data[i]->type != IT_SHADOWGEAR)
  8022. type = (enum refine_type)sd->inventory_data[i]->wlv;
  8023. script_pushint(st, status_get_refine_chance(type, (int)sd->inventory.u.items_inventory[i].refine, enriched));
  8024. }
  8025. else
  8026. script_pushint(st,0);
  8027. return SCRIPT_CMD_SUCCESS;
  8028. }
  8029. /*==========================================
  8030. * Refine +1 item at pos and log and display refine
  8031. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  8032. *------------------------------------------*/
  8033. BUILDIN_FUNC(successrefitem) {
  8034. short i = -1, up = 1;
  8035. int pos;
  8036. TBL_PC *sd;
  8037. pos = script_getnum(st,2);
  8038. if (!script_charid2sd(4, sd)) {
  8039. script_pushint(st, -1);
  8040. return SCRIPT_CMD_FAILURE;
  8041. }
  8042. if (script_hasdata(st, 3))
  8043. up = script_getnum(st, 3);
  8044. if (equip_index_check(pos))
  8045. i = pc_checkequip(sd,equip_bitmask[pos]);
  8046. if (i >= 0) {
  8047. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8048. //Logs items, got from (N)PC scripts [Lupus]
  8049. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8050. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  8051. script_pushint(st, MAX_REFINE);
  8052. return SCRIPT_CMD_SUCCESS;
  8053. }
  8054. sd->inventory.u.items_inventory[i].refine += up;
  8055. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8056. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8057. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  8058. clif_delitem(sd,i,1,3);
  8059. //Logs items, got from (N)PC scripts [Lupus]
  8060. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8061. clif_additem(sd,i,1,0);
  8062. pc_equipitem(sd,i,ep);
  8063. clif_misceffect(&sd->bl,3);
  8064. achievement_update_objective(sd, AG_REFINE_SUCCESS, 2, sd->inventory_data[i]->wlv, sd->inventory.u.items_inventory[i].refine);
  8065. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  8066. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  8067. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  8068. { // Fame point system [DracoRPG]
  8069. switch (sd->inventory_data[i]->wlv){
  8070. case 1:
  8071. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  8072. break;
  8073. case 2:
  8074. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  8075. break;
  8076. case 3:
  8077. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  8078. break;
  8079. }
  8080. }
  8081. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8082. return SCRIPT_CMD_SUCCESS;
  8083. }
  8084. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8085. script_pushint(st, -1);
  8086. return SCRIPT_CMD_FAILURE;
  8087. }
  8088. /*==========================================
  8089. * Show a failed Refine +1 attempt
  8090. * failedrefitem <equipment slot>{,<char_id>}
  8091. *------------------------------------------*/
  8092. BUILDIN_FUNC(failedrefitem) {
  8093. short i = -1;
  8094. int pos;
  8095. TBL_PC *sd;
  8096. pos = script_getnum(st,2);
  8097. if (!script_charid2sd(3, sd)) {
  8098. script_pushint(st, 0);
  8099. return SCRIPT_CMD_FAILURE;
  8100. }
  8101. if (equip_index_check(pos))
  8102. i = pc_checkequip(sd,equip_bitmask[pos]);
  8103. if (i >= 0) {
  8104. sd->inventory.u.items_inventory[i].refine = 0;
  8105. pc_unequipitem(sd,i,3); //recalculate bonus
  8106. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  8107. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  8108. clif_misceffect(&sd->bl,2); // display failure effect
  8109. achievement_update_objective(sd, AG_REFINE_FAIL, 1, 1);
  8110. script_pushint(st, 1);
  8111. return SCRIPT_CMD_SUCCESS;
  8112. }
  8113. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8114. script_pushint(st, 0);
  8115. return SCRIPT_CMD_FAILURE;
  8116. }
  8117. /*==========================================
  8118. * Downgrades an Equipment Part by -1 . [Masao]
  8119. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  8120. *------------------------------------------*/
  8121. BUILDIN_FUNC(downrefitem) {
  8122. short i = -1, down = 1;
  8123. int pos;
  8124. TBL_PC *sd;
  8125. if (!script_charid2sd(4, sd)) {
  8126. script_pushint(st, -1);
  8127. return SCRIPT_CMD_FAILURE;
  8128. }
  8129. pos = script_getnum(st,2);
  8130. if (script_hasdata(st, 3))
  8131. down = script_getnum(st, 3);
  8132. if (equip_index_check(pos))
  8133. i = pc_checkequip(sd,equip_bitmask[pos]);
  8134. if (i >= 0) {
  8135. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  8136. //Logs items, got from (N)PC scripts [Lupus]
  8137. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  8138. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  8139. sd->inventory.u.items_inventory[i].refine -= down;
  8140. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  8141. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  8142. clif_delitem(sd,i,1,3);
  8143. //Logs items, got from (N)PC scripts [Lupus]
  8144. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  8145. clif_additem(sd,i,1,0);
  8146. pc_equipitem(sd,i,ep);
  8147. clif_misceffect(&sd->bl,2);
  8148. achievement_update_objective(sd, AG_REFINE_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  8149. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  8150. return SCRIPT_CMD_SUCCESS;
  8151. }
  8152. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8153. script_pushint(st, -1);
  8154. return SCRIPT_CMD_FAILURE;
  8155. }
  8156. /**
  8157. * Delete the item equipped at pos.
  8158. * delequip <equipment slot>{,<char_id>};
  8159. **/
  8160. BUILDIN_FUNC(delequip) {
  8161. short i = -1;
  8162. int pos;
  8163. int8 ret;
  8164. TBL_PC *sd;
  8165. pos = script_getnum(st,2);
  8166. if (!script_charid2sd(3,sd)) {
  8167. st->state = END;
  8168. return SCRIPT_CMD_FAILURE;
  8169. }
  8170. if (equip_index_check(pos))
  8171. i = pc_checkequip(sd,equip_bitmask[pos]);
  8172. if (i >= 0) {
  8173. pc_unequipitem(sd,i,3); //recalculate bonus
  8174. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  8175. }
  8176. else {
  8177. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8178. st->state = END;
  8179. return SCRIPT_CMD_FAILURE;
  8180. }
  8181. script_pushint(st,ret);
  8182. return SCRIPT_CMD_SUCCESS;
  8183. }
  8184. /**
  8185. * Break the item equipped at pos.
  8186. * breakequip <equipment slot>{,<char_id>};
  8187. **/
  8188. BUILDIN_FUNC(breakequip) {
  8189. short i = -1;
  8190. int pos;
  8191. TBL_PC *sd;
  8192. pos = script_getnum(st,2);
  8193. if (!script_charid2sd(3,sd))
  8194. return SCRIPT_CMD_FAILURE;
  8195. if (equip_index_check(pos))
  8196. i = pc_checkequip(sd,equip_bitmask[pos]);
  8197. if (i >= 0) {
  8198. sd->inventory.u.items_inventory[i].attribute = 1;
  8199. pc_unequipitem(sd,i,3);
  8200. clif_equiplist(sd);
  8201. script_pushint(st,1);
  8202. return SCRIPT_CMD_SUCCESS;
  8203. }
  8204. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  8205. script_pushint(st,0);
  8206. return SCRIPT_CMD_FAILURE;
  8207. }
  8208. /**
  8209. * statusup <stat>{,<char_id>};
  8210. **/
  8211. BUILDIN_FUNC(statusup)
  8212. {
  8213. int type;
  8214. TBL_PC *sd;
  8215. type = script_getnum(st,2);
  8216. if (!script_charid2sd(3, sd))
  8217. return SCRIPT_CMD_FAILURE;
  8218. pc_statusup(sd, type, 1);
  8219. return SCRIPT_CMD_SUCCESS;
  8220. }
  8221. /**
  8222. * statusup2 <stat>,<amount>{,<char_id>};
  8223. **/
  8224. BUILDIN_FUNC(statusup2)
  8225. {
  8226. int type,val;
  8227. TBL_PC *sd;
  8228. type=script_getnum(st,2);
  8229. val=script_getnum(st,3);
  8230. if (!script_charid2sd(4, sd))
  8231. return SCRIPT_CMD_FAILURE;
  8232. pc_statusup2(sd,type,val);
  8233. return SCRIPT_CMD_SUCCESS;
  8234. }
  8235. /// See 'doc/item_bonus.txt'
  8236. ///
  8237. /// bonus <bonus type>,<val1>;
  8238. /// bonus2 <bonus type>,<val1>,<val2>;
  8239. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8240. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8241. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8242. BUILDIN_FUNC(bonus)
  8243. {
  8244. int type;
  8245. int val1 = 0;
  8246. int val2 = 0;
  8247. int val3 = 0;
  8248. int val4 = 0;
  8249. int val5 = 0;
  8250. TBL_PC* sd;
  8251. struct script_data *data;
  8252. if( !script_rid2sd(sd) )
  8253. return SCRIPT_CMD_SUCCESS; // no player attached
  8254. type = script_getnum(st,2);
  8255. switch( type ) {
  8256. case SP_AUTOSPELL:
  8257. case SP_AUTOSPELL_WHENHIT:
  8258. case SP_AUTOSPELL_ONSKILL:
  8259. case SP_SKILL_ATK:
  8260. case SP_SKILL_HEAL:
  8261. case SP_SKILL_HEAL2:
  8262. case SP_ADD_SKILL_BLOW:
  8263. case SP_CASTRATE:
  8264. case SP_ADDEFF_ONSKILL:
  8265. case SP_SKILL_USE_SP_RATE:
  8266. case SP_SKILL_COOLDOWN:
  8267. case SP_SKILL_FIXEDCAST:
  8268. case SP_SKILL_VARIABLECAST:
  8269. case SP_VARCASTRATE:
  8270. case SP_FIXCASTRATE:
  8271. case SP_SKILL_DELAY:
  8272. case SP_SKILL_USE_SP:
  8273. case SP_SUB_SKILL:
  8274. // these bonuses support skill names
  8275. data = script_getdata(st, 3);
  8276. get_val(st, data); // Convert into value in case of a variable
  8277. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8278. break;
  8279. default:
  8280. if (script_hasdata(st, 3))
  8281. val1 = script_getnum(st, 3);
  8282. break;
  8283. }
  8284. switch( script_lastdata(st)-2 ) {
  8285. case 0:
  8286. case 1:
  8287. pc_bonus(sd, type, val1);
  8288. break;
  8289. case 2:
  8290. val2 = script_getnum(st,4);
  8291. pc_bonus2(sd, type, val1, val2);
  8292. break;
  8293. case 3:
  8294. val2 = script_getnum(st,4);
  8295. val3 = script_getnum(st,5);
  8296. pc_bonus3(sd, type, val1, val2, val3);
  8297. break;
  8298. case 4:
  8299. data = script_getdata(st, 4);
  8300. get_val(st, data); // Convert into value in case of a variable
  8301. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  8302. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8303. else
  8304. val2 = script_getnum(st,4);
  8305. val3 = script_getnum(st,5);
  8306. val4 = script_getnum(st,6);
  8307. pc_bonus4(sd, type, val1, val2, val3, val4);
  8308. break;
  8309. case 5:
  8310. data = script_getdata(st, 4);
  8311. get_val(st, data); // Convert into value in case of a variable
  8312. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  8313. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8314. else
  8315. val2 = script_getnum(st,4);
  8316. val3 = script_getnum(st,5);
  8317. val4 = script_getnum(st,6);
  8318. val5 = script_getnum(st,7);
  8319. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8320. break;
  8321. default:
  8322. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8323. break;
  8324. }
  8325. return SCRIPT_CMD_SUCCESS;
  8326. }
  8327. BUILDIN_FUNC(autobonus)
  8328. {
  8329. unsigned int dur, pos;
  8330. short rate;
  8331. short atk_type = 0;
  8332. TBL_PC* sd;
  8333. const char *bonus_script, *other_script = NULL;
  8334. if( !script_rid2sd(sd) )
  8335. return SCRIPT_CMD_SUCCESS; // no player attached
  8336. if (current_equip_combo_pos)
  8337. pos = current_equip_combo_pos;
  8338. else
  8339. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8340. if((sd->state.autobonus&pos) == pos)
  8341. return SCRIPT_CMD_SUCCESS;
  8342. rate = script_getnum(st,3);
  8343. dur = script_getnum(st,4);
  8344. bonus_script = script_getstr(st,2);
  8345. if( !rate || !dur || !pos || !bonus_script )
  8346. return SCRIPT_CMD_SUCCESS;
  8347. if( script_hasdata(st,5) )
  8348. atk_type = script_getnum(st,5);
  8349. if( script_hasdata(st,6) )
  8350. other_script = script_getstr(st,6);
  8351. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  8352. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  8353. {
  8354. script_add_autobonus(bonus_script);
  8355. if( other_script )
  8356. script_add_autobonus(other_script);
  8357. }
  8358. return SCRIPT_CMD_SUCCESS;
  8359. }
  8360. BUILDIN_FUNC(autobonus2)
  8361. {
  8362. unsigned int dur, pos;
  8363. short rate;
  8364. short atk_type = 0;
  8365. TBL_PC* sd;
  8366. const char *bonus_script, *other_script = NULL;
  8367. if( !script_rid2sd(sd) )
  8368. return SCRIPT_CMD_SUCCESS; // no player attached
  8369. if (current_equip_combo_pos)
  8370. pos = current_equip_combo_pos;
  8371. else
  8372. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8373. if((sd->state.autobonus&pos) == pos)
  8374. return SCRIPT_CMD_SUCCESS;
  8375. rate = script_getnum(st,3);
  8376. dur = script_getnum(st,4);
  8377. bonus_script = script_getstr(st,2);
  8378. if( !rate || !dur || !pos || !bonus_script )
  8379. return SCRIPT_CMD_SUCCESS;
  8380. if( script_hasdata(st,5) )
  8381. atk_type = script_getnum(st,5);
  8382. if( script_hasdata(st,6) )
  8383. other_script = script_getstr(st,6);
  8384. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  8385. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  8386. {
  8387. script_add_autobonus(bonus_script);
  8388. if( other_script )
  8389. script_add_autobonus(other_script);
  8390. }
  8391. return SCRIPT_CMD_SUCCESS;
  8392. }
  8393. BUILDIN_FUNC(autobonus3)
  8394. {
  8395. unsigned int dur, pos;
  8396. short rate,atk_type;
  8397. TBL_PC* sd;
  8398. const char *bonus_script, *other_script = NULL;
  8399. struct script_data *data;
  8400. if( !script_rid2sd(sd) )
  8401. return SCRIPT_CMD_SUCCESS; // no player attached
  8402. if (current_equip_combo_pos)
  8403. pos = current_equip_combo_pos;
  8404. else
  8405. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8406. if((sd->state.autobonus&pos) == pos)
  8407. return SCRIPT_CMD_SUCCESS;
  8408. rate = script_getnum(st,3);
  8409. dur = script_getnum(st,4);
  8410. data = script_getdata(st, 5);
  8411. get_val(st, data); // Convert into value in case of a variable
  8412. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  8413. bonus_script = script_getstr(st,2);
  8414. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  8415. return SCRIPT_CMD_SUCCESS;
  8416. if( script_hasdata(st,6) )
  8417. other_script = script_getstr(st,6);
  8418. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  8419. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  8420. {
  8421. script_add_autobonus(bonus_script);
  8422. if( other_script )
  8423. script_add_autobonus(other_script);
  8424. }
  8425. return SCRIPT_CMD_SUCCESS;
  8426. }
  8427. /// Changes the level of a player skill.
  8428. /// <flag> defaults to 1
  8429. /// <flag>=0 : set the level of the skill
  8430. /// <flag>=1 : set the temporary level of the skill
  8431. /// <flag>=2 : add to the level of the skill
  8432. ///
  8433. /// skill <skill id>,<level>,<flag>
  8434. /// skill <skill id>,<level>
  8435. /// skill "<skill name>",<level>,<flag>
  8436. /// skill "<skill name>",<level>
  8437. BUILDIN_FUNC(skill)
  8438. {
  8439. int id;
  8440. int level;
  8441. int flag = ADDSKILL_TEMP;
  8442. TBL_PC* sd;
  8443. struct script_data *data;
  8444. const char* command = script_getfuncname(st);
  8445. if( !script_rid2sd(sd) )
  8446. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8447. if (strcmpi(command, "addtoskill") == 0)
  8448. flag = ADDSKILL_TEMP_ADDLEVEL;
  8449. data = script_getdata(st, 2);
  8450. get_val(st, data); // Convert into value in case of a variable
  8451. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8452. level = script_getnum(st,3);
  8453. if( script_hasdata(st,4) )
  8454. flag = script_getnum(st,4);
  8455. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8456. return SCRIPT_CMD_SUCCESS;
  8457. }
  8458. /// Increases the level of a guild skill.
  8459. ///
  8460. /// guildskill <skill id>,<amount>;
  8461. /// guildskill "<skill name>",<amount>;
  8462. BUILDIN_FUNC(guildskill)
  8463. {
  8464. int id;
  8465. int level;
  8466. TBL_PC* sd;
  8467. int i;
  8468. struct script_data *data;
  8469. if( !script_rid2sd(sd) )
  8470. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8471. data = script_getdata(st, 2);
  8472. get_val(st, data); // Convert into value in case of a variable
  8473. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8474. level = script_getnum(st,3);
  8475. for( i=0; i < level; i++ )
  8476. guild_skillup(sd, id);
  8477. return SCRIPT_CMD_SUCCESS;
  8478. }
  8479. /// Returns the level of the player skill.
  8480. ///
  8481. /// getskilllv(<skill id>) -> <level>
  8482. /// getskilllv("<skill name>") -> <level>
  8483. BUILDIN_FUNC(getskilllv)
  8484. {
  8485. int id;
  8486. TBL_PC* sd;
  8487. struct script_data *data;
  8488. if( !script_rid2sd(sd) )
  8489. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8490. data = script_getdata(st, 2);
  8491. get_val(st, data); // Convert into value in case of a variable
  8492. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8493. script_pushint(st, pc_checkskill(sd,id));
  8494. return SCRIPT_CMD_SUCCESS;
  8495. }
  8496. /// Returns the level of the guild skill.
  8497. ///
  8498. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8499. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8500. BUILDIN_FUNC(getgdskilllv)
  8501. {
  8502. int guild_id;
  8503. uint16 skill_id;
  8504. struct guild* g;
  8505. struct script_data *data;
  8506. guild_id = script_getnum(st,2);
  8507. data = script_getdata(st, 3);
  8508. get_val(st, data); // Convert into value in case of a variable
  8509. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8510. g = guild_search(guild_id);
  8511. if( g == NULL )
  8512. script_pushint(st, -1);
  8513. else
  8514. script_pushint(st, guild_checkskill(g,skill_id));
  8515. return SCRIPT_CMD_SUCCESS;
  8516. }
  8517. /// Returns the 'basic_skill_check' setting.
  8518. /// This config determines if the server checks the skill level of NV_BASIC
  8519. /// before allowing the basic actions.
  8520. ///
  8521. /// basicskillcheck() -> <bool>
  8522. BUILDIN_FUNC(basicskillcheck)
  8523. {
  8524. script_pushint(st, battle_config.basic_skill_check);
  8525. return SCRIPT_CMD_SUCCESS;
  8526. }
  8527. /// Returns the GM level of the player.
  8528. ///
  8529. /// getgmlevel({<char_id>}) -> <level>
  8530. BUILDIN_FUNC(getgmlevel)
  8531. {
  8532. TBL_PC* sd;
  8533. if (!script_charid2sd(2,sd))
  8534. return SCRIPT_CMD_FAILURE;
  8535. script_pushint(st, pc_get_group_level(sd));
  8536. return SCRIPT_CMD_SUCCESS;
  8537. }
  8538. /// Returns the group ID of the player.
  8539. ///
  8540. /// getgroupid({<char_id>}) -> <int>
  8541. BUILDIN_FUNC(getgroupid)
  8542. {
  8543. TBL_PC* sd;
  8544. if (!script_charid2sd(2,sd))
  8545. return SCRIPT_CMD_FAILURE;
  8546. script_pushint(st, pc_get_group_id(sd));
  8547. return SCRIPT_CMD_SUCCESS;
  8548. }
  8549. /// Terminates the execution of this script instance.
  8550. ///
  8551. /// end
  8552. BUILDIN_FUNC(end)
  8553. {
  8554. TBL_PC* sd;
  8555. sd = map_id2sd(st->rid);
  8556. st->state = END;
  8557. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8558. int i;
  8559. for(i = 0; i < st->stack->sp; i++) {
  8560. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8561. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8562. st->script = ri->script;
  8563. break;
  8564. }
  8565. }
  8566. }
  8567. if( st->mes_active )
  8568. st->mes_active = 0;
  8569. if (sd){
  8570. if (sd->state.callshop == 0)
  8571. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8572. else
  8573. sd->state.callshop = 0;
  8574. }
  8575. return SCRIPT_CMD_SUCCESS;
  8576. }
  8577. /// Checks if the player has that effect state (option).
  8578. ///
  8579. /// checkoption(<option>{,<char_id>}) -> <bool>
  8580. BUILDIN_FUNC(checkoption)
  8581. {
  8582. int option;
  8583. TBL_PC* sd;
  8584. if (!script_charid2sd(3,sd))
  8585. return SCRIPT_CMD_FAILURE;
  8586. option = script_getnum(st,2);
  8587. if( sd->sc.option&option )
  8588. script_pushint(st, 1);
  8589. else
  8590. script_pushint(st, 0);
  8591. return SCRIPT_CMD_SUCCESS;
  8592. }
  8593. /// Checks if the player is in that body state (opt1).
  8594. ///
  8595. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8596. BUILDIN_FUNC(checkoption1)
  8597. {
  8598. int opt1;
  8599. TBL_PC* sd;
  8600. if (!script_charid2sd(3,sd))
  8601. return SCRIPT_CMD_FAILURE;
  8602. opt1 = script_getnum(st,2);
  8603. if( sd->sc.opt1 == opt1 )
  8604. script_pushint(st, 1);
  8605. else
  8606. script_pushint(st, 0);
  8607. return SCRIPT_CMD_SUCCESS;
  8608. }
  8609. /// Checks if the player has that health state (opt2).
  8610. ///
  8611. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8612. BUILDIN_FUNC(checkoption2)
  8613. {
  8614. int opt2;
  8615. TBL_PC* sd;
  8616. if (!script_charid2sd(3,sd))
  8617. return SCRIPT_CMD_FAILURE;
  8618. opt2 = script_getnum(st,2);
  8619. if( sd->sc.opt2&opt2 )
  8620. script_pushint(st, 1);
  8621. else
  8622. script_pushint(st, 0);
  8623. return SCRIPT_CMD_SUCCESS;
  8624. }
  8625. /// Changes the effect state (option) of the player.
  8626. /// <flag> defaults to 1
  8627. /// <flag>=0 : removes the option
  8628. /// <flag>=other : adds the option
  8629. ///
  8630. /// setoption <option>{,<flag>{,<char_id>}};
  8631. BUILDIN_FUNC(setoption)
  8632. {
  8633. int option;
  8634. int flag = 1;
  8635. TBL_PC* sd;
  8636. if (!script_charid2sd(4,sd))
  8637. return SCRIPT_CMD_FAILURE;
  8638. option = script_getnum(st,2);
  8639. if( script_hasdata(st,3) )
  8640. flag = script_getnum(st,3);
  8641. else if( !option ){// Request to remove everything.
  8642. flag = 0;
  8643. option = OPTION_FALCON|OPTION_RIDING;
  8644. #ifndef NEW_CARTS
  8645. option |= OPTION_CART;
  8646. #endif
  8647. }
  8648. if( flag ){// Add option
  8649. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8650. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8651. pc_setoption(sd, sd->sc.option|option);
  8652. } else// Remove option
  8653. pc_setoption(sd, sd->sc.option&~option);
  8654. return SCRIPT_CMD_SUCCESS;
  8655. }
  8656. /// Returns if the player has a cart.
  8657. ///
  8658. /// checkcart({char_id}) -> <bool>
  8659. ///
  8660. /// @author Valaris
  8661. BUILDIN_FUNC(checkcart)
  8662. {
  8663. TBL_PC* sd;
  8664. if (!script_charid2sd(2,sd))
  8665. return SCRIPT_CMD_FAILURE;
  8666. if( pc_iscarton(sd) )
  8667. script_pushint(st, 1);
  8668. else
  8669. script_pushint(st, 0);
  8670. return SCRIPT_CMD_SUCCESS;
  8671. }
  8672. /// Sets the cart of the player.
  8673. /// <type> defaults to 1
  8674. /// <type>=0 : removes the cart
  8675. /// <type>=1 : Normal cart
  8676. /// <type>=2 : Wooden cart
  8677. /// <type>=3 : Covered cart with flowers and ferns
  8678. /// <type>=4 : Wooden cart with a Panda doll on the back
  8679. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8680. ///
  8681. /// setcart {<type>{,<char_id>}};
  8682. BUILDIN_FUNC(setcart)
  8683. {
  8684. int type = 1;
  8685. TBL_PC* sd;
  8686. if (!script_charid2sd(3,sd))
  8687. return SCRIPT_CMD_FAILURE;
  8688. if( script_hasdata(st,2) )
  8689. type = script_getnum(st,2);
  8690. pc_setcart(sd, type);
  8691. return SCRIPT_CMD_SUCCESS;
  8692. }
  8693. /// Returns if the player has a falcon.
  8694. ///
  8695. /// checkfalcon({char_id}) -> <bool>
  8696. ///
  8697. /// @author Valaris
  8698. BUILDIN_FUNC(checkfalcon)
  8699. {
  8700. TBL_PC* sd;
  8701. if (!script_charid2sd(2,sd))
  8702. return SCRIPT_CMD_FAILURE;
  8703. if( pc_isfalcon(sd) )
  8704. script_pushint(st, 1);
  8705. else
  8706. script_pushint(st, 0);
  8707. return SCRIPT_CMD_SUCCESS;
  8708. }
  8709. /// Sets if the player has a falcon or not.
  8710. /// <flag> defaults to 1
  8711. ///
  8712. /// setfalcon {<flag>{,<char_id>}};
  8713. BUILDIN_FUNC(setfalcon)
  8714. {
  8715. int flag = 1;
  8716. TBL_PC* sd;
  8717. if (!script_charid2sd(3,sd))
  8718. return SCRIPT_CMD_FAILURE;
  8719. if( script_hasdata(st,2) )
  8720. flag = script_getnum(st,2);
  8721. pc_setfalcon(sd, flag);
  8722. return SCRIPT_CMD_SUCCESS;
  8723. }
  8724. /// Returns if the player is riding.
  8725. ///
  8726. /// checkriding({char_id}) -> <bool>
  8727. ///
  8728. /// @author Valaris
  8729. BUILDIN_FUNC(checkriding)
  8730. {
  8731. TBL_PC* sd;
  8732. if (!script_charid2sd(2,sd))
  8733. return SCRIPT_CMD_FAILURE;
  8734. if( pc_isriding(sd) )
  8735. script_pushint(st, 1);
  8736. else
  8737. script_pushint(st, 0);
  8738. return SCRIPT_CMD_SUCCESS;
  8739. }
  8740. /// Sets if the player is riding.
  8741. /// <flag> defaults to 1
  8742. ///
  8743. /// setriding {<flag>{,<char_id>}};
  8744. BUILDIN_FUNC(setriding)
  8745. {
  8746. int flag = 1;
  8747. TBL_PC* sd;
  8748. if (!script_charid2sd(3,sd))
  8749. return SCRIPT_CMD_FAILURE;
  8750. if( script_hasdata(st,2) )
  8751. flag = script_getnum(st,2);
  8752. pc_setriding(sd, flag);
  8753. return SCRIPT_CMD_SUCCESS;
  8754. }
  8755. /// Returns if the player has a warg.
  8756. ///
  8757. /// checkwug({char_id}) -> <bool>
  8758. ///
  8759. BUILDIN_FUNC(checkwug)
  8760. {
  8761. TBL_PC* sd;
  8762. if (!script_charid2sd(2,sd))
  8763. return SCRIPT_CMD_FAILURE;
  8764. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8765. script_pushint(st, 1);
  8766. else
  8767. script_pushint(st, 0);
  8768. return SCRIPT_CMD_SUCCESS;
  8769. }
  8770. /// Returns if the player is wearing MADO Gear.
  8771. ///
  8772. /// checkmadogear({<char_id>}) -> <bool>
  8773. ///
  8774. BUILDIN_FUNC(checkmadogear)
  8775. {
  8776. TBL_PC* sd;
  8777. if (!script_charid2sd(2,sd))
  8778. return SCRIPT_CMD_FAILURE;
  8779. if( pc_ismadogear(sd) )
  8780. script_pushint(st, 1);
  8781. else
  8782. script_pushint(st, 0);
  8783. return SCRIPT_CMD_SUCCESS;
  8784. }
  8785. /// Sets if the player is riding MADO Gear.
  8786. /// <flag> defaults to 1
  8787. ///
  8788. /// setmadogear {<flag>{,<char_id>}};
  8789. BUILDIN_FUNC(setmadogear)
  8790. {
  8791. int flag = 1;
  8792. TBL_PC* sd;
  8793. if (!script_charid2sd(3,sd))
  8794. return SCRIPT_CMD_FAILURE;
  8795. if( script_hasdata(st,2) )
  8796. flag = script_getnum(st,2);
  8797. pc_setmadogear(sd, flag);
  8798. return SCRIPT_CMD_SUCCESS;
  8799. }
  8800. /// Sets the save point of the player.
  8801. ///
  8802. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8803. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8804. BUILDIN_FUNC(savepoint)
  8805. {
  8806. int x, y, m, cid_pos = 5;
  8807. unsigned short map_idx;
  8808. const char* str;
  8809. TBL_PC* sd;
  8810. if (script_lastdata(st) > 5)
  8811. cid_pos = 7;
  8812. if (!script_charid2sd(cid_pos,sd))
  8813. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8814. str = script_getstr(st, 2);
  8815. map_idx = mapindex_name2id(str);
  8816. if( !map_idx )
  8817. return SCRIPT_CMD_FAILURE;
  8818. x = script_getnum(st,3);
  8819. y = script_getnum(st,4);
  8820. m = map_mapindex2mapid(map_idx);
  8821. if (cid_pos == 7) {
  8822. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8823. x1 = x + dx, y1 = y + dy,
  8824. x0 = x - dx, y0 = y - dy;
  8825. uint8 n = 10;
  8826. do {
  8827. x = x0 + rnd()%(x1-x0+1);
  8828. y = y0 + rnd()%(y1-y0+1);
  8829. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8830. }
  8831. // Check for valid coordinates if map in local map-server
  8832. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8833. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8834. return SCRIPT_CMD_FAILURE;
  8835. }
  8836. pc_setsavepoint(sd, map_idx, x, y);
  8837. return SCRIPT_CMD_SUCCESS;
  8838. }
  8839. /*==========================================
  8840. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8841. *------------------------------------------*/
  8842. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8843. {
  8844. int type;
  8845. time_t timer;
  8846. struct tm *t;
  8847. type=script_getnum(st,2);
  8848. switch(type){
  8849. case 2:
  8850. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8851. // from the system clock.)
  8852. script_pushint(st,(int)time(NULL));
  8853. break;
  8854. case 1:
  8855. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8856. time(&timer);
  8857. t=localtime(&timer);
  8858. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8859. break;
  8860. case 0:
  8861. default:
  8862. //type 0:(System Ticks)
  8863. script_pushint(st,gettick());
  8864. break;
  8865. }
  8866. return SCRIPT_CMD_SUCCESS;
  8867. }
  8868. /*==========================================
  8869. * GetTime(Type)
  8870. *
  8871. * Returns the current value of a certain date type.
  8872. * Possible types are:
  8873. * - DT_SECOND Current seconds
  8874. * - DT_MINUTE Current minute
  8875. * - DT_HOUR Current hour
  8876. * - DT_DAYOFWEEK Day of current week
  8877. * - DT_DAYOFMONTH Day of current month
  8878. * - DT_MONTH Current month
  8879. * - DT_YEAR Current year
  8880. * - DT_DAYOFYEAR Day of current year
  8881. *
  8882. * If none of the above types is supplied -1 will be returned to the script
  8883. * and the script source will be reported into the mapserver console.
  8884. *------------------------------------------*/
  8885. BUILDIN_FUNC(gettime)
  8886. {
  8887. int type;
  8888. type = script_getnum(st,2);
  8889. if( type <= DT_MIN || type >= DT_MAX ){
  8890. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8891. script_reportsrc(st);
  8892. script_pushint(st,-1);
  8893. }else{
  8894. script_pushint(st,date_get((enum e_date_type)type));
  8895. }
  8896. return SCRIPT_CMD_SUCCESS;
  8897. }
  8898. /**
  8899. * Returns the current server time or the provided time in a readable format.
  8900. * gettimestr(<"time_format">,<max_length>{,<time_tick>});
  8901. */
  8902. BUILDIN_FUNC(gettimestr)
  8903. {
  8904. char *tmpstr;
  8905. const char *fmtstr;
  8906. int maxlen;
  8907. time_t now;
  8908. fmtstr = script_getstr(st,2);
  8909. maxlen = script_getnum(st,3);
  8910. if (script_hasdata(st, 4)) {
  8911. if (script_getnum(st, 4) < 0) {
  8912. ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
  8913. return SCRIPT_CMD_FAILURE;
  8914. } else
  8915. now = (time_t)script_getnum(st, 4);
  8916. } else
  8917. now = time(NULL);
  8918. tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
  8919. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8920. tmpstr[maxlen] = '\0';
  8921. script_pushstr(st,tmpstr);
  8922. return SCRIPT_CMD_SUCCESS;
  8923. }
  8924. /*==========================================
  8925. * Open player storage
  8926. *------------------------------------------*/
  8927. BUILDIN_FUNC(openstorage)
  8928. {
  8929. TBL_PC* sd;
  8930. if( !script_rid2sd(sd) )
  8931. return SCRIPT_CMD_SUCCESS;
  8932. storage_storageopen(sd);
  8933. return SCRIPT_CMD_SUCCESS;
  8934. }
  8935. BUILDIN_FUNC(guildopenstorage)
  8936. {
  8937. TBL_PC* sd;
  8938. int ret;
  8939. if( !script_rid2sd(sd) )
  8940. return SCRIPT_CMD_SUCCESS;
  8941. ret = storage_guild_storageopen(sd);
  8942. script_pushint(st,ret);
  8943. return SCRIPT_CMD_SUCCESS;
  8944. }
  8945. /*==========================================
  8946. * Make player use a skill trought item usage
  8947. *------------------------------------------*/
  8948. /// itemskill <skill id>,<level>
  8949. /// itemskill "<skill name>",<level>
  8950. BUILDIN_FUNC(itemskill)
  8951. {
  8952. int id;
  8953. int lv;
  8954. bool keep_requirement;
  8955. TBL_PC* sd;
  8956. struct script_data *data;
  8957. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  8958. return SCRIPT_CMD_SUCCESS;
  8959. data = script_getdata(st, 2);
  8960. get_val(st, data); // Convert into value in case of a variable
  8961. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8962. lv = script_getnum(st,3);
  8963. if (script_hasdata(st, 4)) {
  8964. keep_requirement = (script_getnum(st, 4) != 0);
  8965. } else {
  8966. keep_requirement = false;
  8967. }
  8968. sd->skillitem=id;
  8969. sd->skillitemlv=lv;
  8970. sd->skillitem_keep_requirement = keep_requirement;
  8971. clif_item_skill(sd,id,lv);
  8972. return SCRIPT_CMD_SUCCESS;
  8973. }
  8974. /*==========================================
  8975. * Attempt to create an item
  8976. *------------------------------------------*/
  8977. BUILDIN_FUNC(produce)
  8978. {
  8979. int trigger;
  8980. TBL_PC* sd;
  8981. if( !script_rid2sd(sd) )
  8982. return SCRIPT_CMD_SUCCESS;
  8983. trigger=script_getnum(st,2);
  8984. clif_skill_produce_mix_list(sd, -1, trigger);
  8985. return SCRIPT_CMD_SUCCESS;
  8986. }
  8987. /*==========================================
  8988. *
  8989. *------------------------------------------*/
  8990. BUILDIN_FUNC(cooking)
  8991. {
  8992. int trigger;
  8993. TBL_PC* sd;
  8994. if( !script_rid2sd(sd) )
  8995. return SCRIPT_CMD_SUCCESS;
  8996. trigger=script_getnum(st,2);
  8997. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8998. return SCRIPT_CMD_SUCCESS;
  8999. }
  9000. /*==========================================
  9001. * Create a pet
  9002. *------------------------------------------*/
  9003. BUILDIN_FUNC(makepet)
  9004. {
  9005. struct map_session_data* sd;
  9006. uint16 mob_id;
  9007. struct s_pet_db* pet;
  9008. if( !script_rid2sd(sd) )
  9009. return SCRIPT_CMD_FAILURE;
  9010. mob_id = script_getnum(st,2);
  9011. pet = pet_db(mob_id);
  9012. if( !pet ){
  9013. ShowError( "buildin_makepet: failed to create a pet with mob id %hu\n", mob_id);
  9014. return SCRIPT_CMD_FAILURE;
  9015. }
  9016. sd->catch_target_class = mob_id;
  9017. 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 );
  9018. return SCRIPT_CMD_SUCCESS;
  9019. }
  9020. /**
  9021. * Give player exp base,job * quest_exp_rate/100
  9022. * getexp <base xp>,<job xp>{,<char_id>};
  9023. **/
  9024. BUILDIN_FUNC(getexp)
  9025. {
  9026. TBL_PC* sd;
  9027. int base=0,job=0;
  9028. double bonus;
  9029. if (!script_charid2sd(4,sd))
  9030. return SCRIPT_CMD_FAILURE;
  9031. base=script_getnum(st,2);
  9032. job =script_getnum(st,3);
  9033. if(base<0 || job<0)
  9034. return SCRIPT_CMD_SUCCESS;
  9035. // bonus for npc-given exp
  9036. bonus = battle_config.quest_exp_rate / 100.;
  9037. if (base)
  9038. base = (int) cap_value(base * bonus, 0, INT_MAX);
  9039. if (job)
  9040. job = (int) cap_value(job * bonus, 0, INT_MAX);
  9041. pc_gainexp(sd, NULL, base, job, 1);
  9042. return SCRIPT_CMD_SUCCESS;
  9043. }
  9044. /*==========================================
  9045. * Gain guild exp [Celest]
  9046. *------------------------------------------*/
  9047. BUILDIN_FUNC(guildgetexp)
  9048. {
  9049. TBL_PC* sd;
  9050. int exp;
  9051. if( !script_rid2sd(sd) )
  9052. return SCRIPT_CMD_SUCCESS;
  9053. exp = script_getnum(st,2);
  9054. if(exp < 0)
  9055. return SCRIPT_CMD_SUCCESS;
  9056. if(sd && sd->status.guild_id > 0)
  9057. guild_getexp (sd, exp);
  9058. return SCRIPT_CMD_SUCCESS;
  9059. }
  9060. /*==========================================
  9061. * Changes the guild master of a guild [Skotlex]
  9062. *------------------------------------------*/
  9063. BUILDIN_FUNC(guildchangegm)
  9064. {
  9065. TBL_PC *sd;
  9066. int guild_id;
  9067. const char *name;
  9068. guild_id = script_getnum(st,2);
  9069. name = script_getstr(st,3);
  9070. sd=map_nick2sd(name,false);
  9071. if (!sd)
  9072. script_pushint(st,0);
  9073. else
  9074. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  9075. return SCRIPT_CMD_SUCCESS;
  9076. }
  9077. /*==========================================
  9078. * Spawn a monster:
  9079. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  9080. *------------------------------------------*/
  9081. BUILDIN_FUNC(monster)
  9082. {
  9083. const char* mapn = script_getstr(st,2);
  9084. int x = script_getnum(st,3);
  9085. int y = script_getnum(st,4);
  9086. const char* str = script_getstr(st,5);
  9087. int class_ = script_getnum(st,6);
  9088. int amount = script_getnum(st,7);
  9089. const char* event = "";
  9090. unsigned int size = SZ_SMALL;
  9091. enum mob_ai ai = AI_NONE;
  9092. struct map_session_data* sd;
  9093. int16 m;
  9094. int i;
  9095. if (script_hasdata(st, 8)) {
  9096. event = script_getstr(st, 8);
  9097. check_event(st, event);
  9098. }
  9099. if (script_hasdata(st, 9)) {
  9100. size = script_getnum(st, 9);
  9101. if (size > SZ_BIG) {
  9102. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9103. return SCRIPT_CMD_FAILURE;
  9104. }
  9105. }
  9106. if (script_hasdata(st, 10)) {
  9107. ai = static_cast<enum mob_ai>(script_getnum(st, 10));
  9108. if (ai >= AI_MAX) {
  9109. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9110. return SCRIPT_CMD_FAILURE;
  9111. }
  9112. }
  9113. if (class_ >= 0 && !mobdb_checkid(class_)) {
  9114. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  9115. return SCRIPT_CMD_FAILURE;
  9116. }
  9117. sd = map_id2sd(st->rid);
  9118. if (sd && strcmp(mapn, "this") == 0)
  9119. m = sd->bl.m;
  9120. else
  9121. m = map_mapname2mapid(mapn);
  9122. for(i = 0; i < amount; i++) { //not optimised
  9123. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  9124. if (mobid)
  9125. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9126. }
  9127. return SCRIPT_CMD_SUCCESS;
  9128. }
  9129. /*==========================================
  9130. * Request List of Monster Drops
  9131. *------------------------------------------*/
  9132. BUILDIN_FUNC(getmobdrops)
  9133. {
  9134. int class_ = script_getnum(st,2);
  9135. int i, j = 0;
  9136. struct mob_db *mob;
  9137. if( !mobdb_checkid(class_) )
  9138. {
  9139. script_pushint(st, 0);
  9140. return SCRIPT_CMD_SUCCESS;
  9141. }
  9142. mob = mob_db(class_);
  9143. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  9144. {
  9145. if( mob->dropitem[i].nameid < 1 )
  9146. continue;
  9147. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  9148. continue;
  9149. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  9150. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  9151. j++;
  9152. }
  9153. mapreg_setreg(add_str("$@MobDrop_count"), j);
  9154. script_pushint(st, 1);
  9155. return SCRIPT_CMD_SUCCESS;
  9156. }
  9157. /*==========================================
  9158. * Spawn a monster in a random location
  9159. * in x0,x1,y0,y1 area.
  9160. *------------------------------------------*/
  9161. BUILDIN_FUNC(areamonster)
  9162. {
  9163. const char* mapn = script_getstr(st,2);
  9164. int x0 = script_getnum(st,3);
  9165. int y0 = script_getnum(st,4);
  9166. int x1 = script_getnum(st,5);
  9167. int y1 = script_getnum(st,6);
  9168. const char* str = script_getstr(st,7);
  9169. int class_ = script_getnum(st,8);
  9170. int amount = script_getnum(st,9);
  9171. const char* event = "";
  9172. unsigned int size = SZ_SMALL;
  9173. enum mob_ai ai = AI_NONE;
  9174. struct map_session_data* sd;
  9175. int16 m;
  9176. int i;
  9177. if (script_hasdata(st,10)) {
  9178. event = script_getstr(st, 10);
  9179. check_event(st, event);
  9180. }
  9181. if (script_hasdata(st, 11)) {
  9182. size = script_getnum(st, 11);
  9183. if (size > 3) {
  9184. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  9185. return SCRIPT_CMD_FAILURE;
  9186. }
  9187. }
  9188. if (script_hasdata(st, 12)) {
  9189. ai = static_cast<enum mob_ai>(script_getnum(st, 12));
  9190. if (ai >= AI_MAX) {
  9191. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  9192. return SCRIPT_CMD_FAILURE;
  9193. }
  9194. }
  9195. sd = map_id2sd(st->rid);
  9196. if (sd && strcmp(mapn, "this") == 0)
  9197. m = sd->bl.m;
  9198. else
  9199. m = map_mapname2mapid(mapn);
  9200. for(i = 0; i < amount; i++) { //not optimised
  9201. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  9202. if (mobid)
  9203. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  9204. }
  9205. return SCRIPT_CMD_SUCCESS;
  9206. }
  9207. /*==========================================
  9208. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  9209. *------------------------------------------*/
  9210. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  9211. { //same fix but with killmonster instead - stripping events from mobs.
  9212. TBL_MOB* md = (TBL_MOB*)bl;
  9213. char *event=va_arg(ap,char *);
  9214. int allflag=va_arg(ap,int);
  9215. md->state.npc_killmonster = 1;
  9216. if(!allflag){
  9217. if(strcmp(event,md->npc_event)==0)
  9218. status_kill(bl);
  9219. }else{
  9220. if(!md->spawn)
  9221. status_kill(bl);
  9222. }
  9223. md->state.npc_killmonster = 0;
  9224. return SCRIPT_CMD_SUCCESS;
  9225. }
  9226. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9227. {
  9228. TBL_MOB* md = (TBL_MOB*)bl;
  9229. char *event=va_arg(ap,char *);
  9230. int allflag=va_arg(ap,int);
  9231. if(!allflag){
  9232. if(strcmp(event,md->npc_event)==0)
  9233. status_kill(bl);
  9234. }else{
  9235. if(!md->spawn)
  9236. status_kill(bl);
  9237. }
  9238. return SCRIPT_CMD_SUCCESS;
  9239. }
  9240. BUILDIN_FUNC(killmonster)
  9241. {
  9242. const char *mapname,*event;
  9243. int16 m,allflag=0;
  9244. mapname=script_getstr(st,2);
  9245. event=script_getstr(st,3);
  9246. if(strcmp(event,"All")==0)
  9247. allflag = 1;
  9248. else
  9249. check_event(st, event);
  9250. if( (m=map_mapname2mapid(mapname))<0 )
  9251. return SCRIPT_CMD_SUCCESS;
  9252. if( script_hasdata(st,4) ) {
  9253. if ( script_getnum(st,4) == 1 ) {
  9254. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9255. return SCRIPT_CMD_SUCCESS;
  9256. }
  9257. }
  9258. map_freeblock_lock();
  9259. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9260. map_freeblock_unlock();
  9261. return SCRIPT_CMD_SUCCESS;
  9262. }
  9263. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9264. { //Strips the event from the mob if it's killed the old method.
  9265. struct mob_data *md;
  9266. md = BL_CAST(BL_MOB, bl);
  9267. if (md->npc_event[0])
  9268. md->npc_event[0] = 0;
  9269. status_kill(bl);
  9270. return 0;
  9271. }
  9272. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9273. {
  9274. status_kill(bl);
  9275. return 0;
  9276. }
  9277. BUILDIN_FUNC(killmonsterall)
  9278. {
  9279. const char *mapname;
  9280. int16 m;
  9281. mapname=script_getstr(st,2);
  9282. if( (m = map_mapname2mapid(mapname))<0 )
  9283. return SCRIPT_CMD_SUCCESS;
  9284. if( script_hasdata(st,3) ) {
  9285. if ( script_getnum(st,3) == 1 ) {
  9286. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9287. return SCRIPT_CMD_SUCCESS;
  9288. }
  9289. }
  9290. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9291. return SCRIPT_CMD_SUCCESS;
  9292. }
  9293. /*==========================================
  9294. * Creates a clone of a player.
  9295. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9296. *------------------------------------------*/
  9297. BUILDIN_FUNC(clone)
  9298. {
  9299. TBL_PC *sd, *msd=NULL;
  9300. uint32 char_id, master_id = 0, x, y, flag = 0;
  9301. int16 m;
  9302. enum e_mode mode = MD_NONE;
  9303. unsigned int duration = 0;
  9304. const char *mapname,*event;
  9305. mapname=script_getstr(st,2);
  9306. x=script_getnum(st,3);
  9307. y=script_getnum(st,4);
  9308. event=script_getstr(st,5);
  9309. char_id=script_getnum(st,6);
  9310. if( script_hasdata(st,7) )
  9311. master_id=script_getnum(st,7);
  9312. if( script_hasdata(st,8) )
  9313. mode=static_cast<e_mode>(script_getnum(st,8));
  9314. if( script_hasdata(st,9) )
  9315. flag=script_getnum(st,9);
  9316. if( script_hasdata(st,10) )
  9317. duration=script_getnum(st,10);
  9318. check_event(st, event);
  9319. m = map_mapname2mapid(mapname);
  9320. if (m < 0)
  9321. return SCRIPT_CMD_SUCCESS;
  9322. sd = map_charid2sd(char_id);
  9323. if (master_id) {
  9324. msd = map_charid2sd(master_id);
  9325. if (msd)
  9326. master_id = msd->bl.id;
  9327. else
  9328. master_id = 0;
  9329. }
  9330. if (sd) //Return ID of newly crafted clone.
  9331. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9332. else //Failed to create clone.
  9333. script_pushint(st,0);
  9334. return SCRIPT_CMD_SUCCESS;
  9335. }
  9336. /*==========================================
  9337. *------------------------------------------*/
  9338. BUILDIN_FUNC(doevent)
  9339. {
  9340. const char* event;
  9341. struct map_session_data* sd;
  9342. if( !script_rid2sd(sd) )
  9343. return SCRIPT_CMD_SUCCESS;
  9344. event = script_getstr(st,2);
  9345. check_event(st, event);
  9346. npc_event(sd, event, 0);
  9347. return SCRIPT_CMD_SUCCESS;
  9348. }
  9349. /*==========================================
  9350. *------------------------------------------*/
  9351. BUILDIN_FUNC(donpcevent)
  9352. {
  9353. const char* event = script_getstr(st,2);
  9354. check_event(st, event);
  9355. if( !npc_event_do(event) ) {
  9356. struct npc_data * nd = map_id2nd(st->oid);
  9357. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9358. script_pushint(st, 0);
  9359. } else
  9360. script_pushint(st, 1);
  9361. return SCRIPT_CMD_SUCCESS;
  9362. }
  9363. /// for Aegis compatibility
  9364. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9365. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9366. {
  9367. const char* npc = script_getstr(st,2);
  9368. const char* command = script_getstr(st,3);
  9369. char event[EVENT_NAME_LENGTH];
  9370. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9371. check_event(st, event);
  9372. if( npc_event_do(event) ){
  9373. script_pushint(st, true);
  9374. }else{
  9375. struct npc_data * nd = map_id2nd(st->oid);
  9376. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9377. script_pushint(st, false);
  9378. }
  9379. return SCRIPT_CMD_SUCCESS;
  9380. }
  9381. /*==========================================
  9382. *------------------------------------------*/
  9383. BUILDIN_FUNC(addtimer)
  9384. {
  9385. int tick;
  9386. const char* event;
  9387. TBL_PC* sd;
  9388. if( !script_rid2sd(sd) )
  9389. return SCRIPT_CMD_SUCCESS;
  9390. tick = script_getnum(st,2);
  9391. event = script_getstr(st, 3);
  9392. check_event(st, event);
  9393. if (!pc_addeventtimer(sd,tick,event)) {
  9394. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9395. return SCRIPT_CMD_FAILURE;
  9396. }
  9397. return SCRIPT_CMD_SUCCESS;
  9398. }
  9399. /*==========================================
  9400. *------------------------------------------*/
  9401. BUILDIN_FUNC(deltimer)
  9402. {
  9403. const char *event;
  9404. TBL_PC* sd;
  9405. if( !script_rid2sd(sd) )
  9406. return SCRIPT_CMD_SUCCESS;
  9407. event=script_getstr(st, 2);
  9408. check_event(st, event);
  9409. pc_deleventtimer(sd,event);
  9410. return SCRIPT_CMD_SUCCESS;
  9411. }
  9412. /*==========================================
  9413. *------------------------------------------*/
  9414. BUILDIN_FUNC(addtimercount)
  9415. {
  9416. const char *event;
  9417. int tick;
  9418. TBL_PC* sd;
  9419. if( !script_rid2sd(sd) )
  9420. return SCRIPT_CMD_SUCCESS;
  9421. tick=script_getnum(st,2);
  9422. event=script_getstr(st,3);
  9423. check_event(st, event);
  9424. pc_addeventtimercount(sd,event,tick);
  9425. return SCRIPT_CMD_SUCCESS;
  9426. }
  9427. /*==========================================
  9428. *------------------------------------------*/
  9429. BUILDIN_FUNC(initnpctimer)
  9430. {
  9431. struct npc_data *nd;
  9432. int flag = 0;
  9433. if( script_hasdata(st,3) )
  9434. { //Two arguments: NPC name and attach flag.
  9435. nd = npc_name2id(script_getstr(st, 2));
  9436. flag = script_getnum(st,3);
  9437. }
  9438. else if( script_hasdata(st,2) )
  9439. { //Check if argument is numeric (flag) or string (npc name)
  9440. struct script_data *data;
  9441. data = script_getdata(st,2);
  9442. get_val(st,data);
  9443. if( data_isstring(data) ) //NPC name
  9444. nd = npc_name2id(conv_str(st, data));
  9445. else if( data_isint(data) ) //Flag
  9446. {
  9447. nd = (struct npc_data *)map_id2bl(st->oid);
  9448. flag = conv_num(st,data);
  9449. }
  9450. else
  9451. {
  9452. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9453. return SCRIPT_CMD_FAILURE;
  9454. }
  9455. }
  9456. else
  9457. nd = (struct npc_data *)map_id2bl(st->oid);
  9458. if( !nd )
  9459. return SCRIPT_CMD_SUCCESS;
  9460. if( flag ) //Attach
  9461. {
  9462. TBL_PC* sd;
  9463. if( !script_rid2sd(sd) )
  9464. return SCRIPT_CMD_SUCCESS;
  9465. nd->u.scr.rid = sd->bl.id;
  9466. }
  9467. nd->u.scr.timertick = 0;
  9468. npc_settimerevent_tick(nd,0);
  9469. npc_timerevent_start(nd, st->rid);
  9470. return SCRIPT_CMD_SUCCESS;
  9471. }
  9472. /*==========================================
  9473. *------------------------------------------*/
  9474. BUILDIN_FUNC(startnpctimer)
  9475. {
  9476. struct npc_data *nd;
  9477. int flag = 0;
  9478. if( script_hasdata(st,3) )
  9479. { //Two arguments: NPC name and attach flag.
  9480. nd = npc_name2id(script_getstr(st, 2));
  9481. flag = script_getnum(st,3);
  9482. }
  9483. else if( script_hasdata(st,2) )
  9484. { //Check if argument is numeric (flag) or string (npc name)
  9485. struct script_data *data;
  9486. data = script_getdata(st,2);
  9487. get_val(st,data);
  9488. if( data_isstring(data) ) //NPC name
  9489. nd = npc_name2id(conv_str(st, data));
  9490. else if( data_isint(data) ) //Flag
  9491. {
  9492. nd = (struct npc_data *)map_id2bl(st->oid);
  9493. flag = conv_num(st,data);
  9494. }
  9495. else
  9496. {
  9497. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9498. return SCRIPT_CMD_FAILURE;
  9499. }
  9500. }
  9501. else
  9502. nd=(struct npc_data *)map_id2bl(st->oid);
  9503. if( !nd )
  9504. return SCRIPT_CMD_SUCCESS;
  9505. if( flag ) //Attach
  9506. {
  9507. TBL_PC* sd;
  9508. if( !script_rid2sd(sd) )
  9509. return SCRIPT_CMD_SUCCESS;
  9510. nd->u.scr.rid = sd->bl.id;
  9511. }
  9512. npc_timerevent_start(nd, st->rid);
  9513. return SCRIPT_CMD_SUCCESS;
  9514. }
  9515. /*==========================================
  9516. *------------------------------------------*/
  9517. BUILDIN_FUNC(stopnpctimer)
  9518. {
  9519. struct npc_data *nd;
  9520. int flag = 0;
  9521. if( script_hasdata(st,3) )
  9522. { //Two arguments: NPC name and attach flag.
  9523. nd = npc_name2id(script_getstr(st, 2));
  9524. flag = script_getnum(st,3);
  9525. }
  9526. else if( script_hasdata(st,2) )
  9527. { //Check if argument is numeric (flag) or string (npc name)
  9528. struct script_data *data;
  9529. data = script_getdata(st,2);
  9530. get_val(st,data);
  9531. if( data_isstring(data) ) //NPC name
  9532. nd = npc_name2id(conv_str(st, data));
  9533. else if( data_isint(data) ) //Flag
  9534. {
  9535. nd = (struct npc_data *)map_id2bl(st->oid);
  9536. flag = conv_num(st,data);
  9537. }
  9538. else
  9539. {
  9540. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9541. return SCRIPT_CMD_FAILURE;
  9542. }
  9543. }
  9544. else
  9545. nd=(struct npc_data *)map_id2bl(st->oid);
  9546. if( !nd )
  9547. return SCRIPT_CMD_SUCCESS;
  9548. if( flag ) //Detach
  9549. nd->u.scr.rid = 0;
  9550. npc_timerevent_stop(nd);
  9551. return SCRIPT_CMD_SUCCESS;
  9552. }
  9553. /*==========================================
  9554. *------------------------------------------*/
  9555. BUILDIN_FUNC(getnpctimer)
  9556. {
  9557. struct npc_data *nd;
  9558. TBL_PC *sd;
  9559. int type = script_getnum(st,2);
  9560. int val = 0;
  9561. if( script_hasdata(st,3) )
  9562. nd = npc_name2id(script_getstr(st,3));
  9563. else
  9564. nd = (struct npc_data *)map_id2bl(st->oid);
  9565. if( !nd || nd->bl.type != BL_NPC )
  9566. {
  9567. script_pushint(st,0);
  9568. ShowError("getnpctimer: Invalid NPC.\n");
  9569. return SCRIPT_CMD_FAILURE;
  9570. }
  9571. switch( type )
  9572. {
  9573. case 0: val = npc_gettimerevent_tick(nd); break;
  9574. case 1:
  9575. if( nd->u.scr.rid )
  9576. {
  9577. sd = map_id2sd(nd->u.scr.rid);
  9578. if( !sd )
  9579. {
  9580. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9581. break;
  9582. }
  9583. val = (sd->npc_timer_id != INVALID_TIMER);
  9584. }
  9585. else
  9586. val = (nd->u.scr.timerid != INVALID_TIMER);
  9587. break;
  9588. case 2: val = nd->u.scr.timeramount; break;
  9589. }
  9590. script_pushint(st,val);
  9591. return SCRIPT_CMD_SUCCESS;
  9592. }
  9593. /*==========================================
  9594. *------------------------------------------*/
  9595. BUILDIN_FUNC(setnpctimer)
  9596. {
  9597. int tick;
  9598. struct npc_data *nd;
  9599. tick = script_getnum(st,2);
  9600. if( script_hasdata(st,3) )
  9601. nd = npc_name2id(script_getstr(st,3));
  9602. else
  9603. nd = (struct npc_data *)map_id2bl(st->oid);
  9604. if( !nd || nd->bl.type != BL_NPC )
  9605. {
  9606. script_pushint(st,1);
  9607. ShowError("setnpctimer: Invalid NPC.\n");
  9608. return SCRIPT_CMD_FAILURE;
  9609. }
  9610. npc_settimerevent_tick(nd,tick);
  9611. script_pushint(st,0);
  9612. return SCRIPT_CMD_SUCCESS;
  9613. }
  9614. /*==========================================
  9615. * attaches the player rid to the timer [Celest]
  9616. *------------------------------------------*/
  9617. BUILDIN_FUNC(attachnpctimer)
  9618. {
  9619. TBL_PC *sd;
  9620. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9621. if( !nd || nd->bl.type != BL_NPC )
  9622. {
  9623. script_pushint(st,1);
  9624. ShowError("setnpctimer: Invalid NPC.\n");
  9625. return SCRIPT_CMD_FAILURE;
  9626. }
  9627. if( !script_nick2sd(2,sd) ){
  9628. script_pushint(st,1);
  9629. ShowWarning("attachnpctimer: Invalid player.\n");
  9630. return SCRIPT_CMD_FAILURE;
  9631. }
  9632. nd->u.scr.rid = sd->bl.id;
  9633. script_pushint(st,0);
  9634. return SCRIPT_CMD_SUCCESS;
  9635. }
  9636. /*==========================================
  9637. * detaches a player rid from the timer [Celest]
  9638. *------------------------------------------*/
  9639. BUILDIN_FUNC(detachnpctimer)
  9640. {
  9641. struct npc_data *nd;
  9642. if( script_hasdata(st,2) )
  9643. nd = npc_name2id(script_getstr(st,2));
  9644. else
  9645. nd = (struct npc_data *)map_id2bl(st->oid);
  9646. if( !nd || nd->bl.type != BL_NPC )
  9647. {
  9648. script_pushint(st,1);
  9649. ShowError("detachnpctimer: Invalid NPC.\n");
  9650. return SCRIPT_CMD_FAILURE;
  9651. }
  9652. nd->u.scr.rid = 0;
  9653. script_pushint(st,0);
  9654. return SCRIPT_CMD_SUCCESS;
  9655. }
  9656. /*==========================================
  9657. * To avoid "player not attached" script errors, this function is provided,
  9658. * it checks if there is a player attached to the current script. [Skotlex]
  9659. * If no, returns 0, if yes, returns the account_id of the attached player.
  9660. *------------------------------------------*/
  9661. BUILDIN_FUNC(playerattached)
  9662. {
  9663. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9664. script_pushint(st,0);
  9665. else
  9666. script_pushint(st,st->rid);
  9667. return SCRIPT_CMD_SUCCESS;
  9668. }
  9669. /*==========================================
  9670. *------------------------------------------*/
  9671. BUILDIN_FUNC(announce)
  9672. {
  9673. const char *mes = script_getstr(st,2);
  9674. int flag = script_getnum(st,3);
  9675. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9676. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9677. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9678. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9679. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9680. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9681. {
  9682. send_target target;
  9683. struct block_list *bl;
  9684. // If bc_npc flag is set, use NPC as broadcast source
  9685. if(flag&BC_NPC){
  9686. bl = map_id2bl(st->oid);
  9687. }else{
  9688. struct map_session_data* sd;
  9689. if( script_rid2sd(sd) )
  9690. bl = &sd->bl;
  9691. else
  9692. bl = NULL;
  9693. }
  9694. if (bl == NULL)
  9695. return SCRIPT_CMD_SUCCESS;
  9696. switch (flag&BC_TARGET_MASK) {
  9697. case BC_MAP: target = ALL_SAMEMAP; break;
  9698. case BC_AREA: target = AREA; break;
  9699. case BC_SELF: target = SELF; break;
  9700. default: target = ALL_CLIENT; break; // BC_ALL
  9701. }
  9702. if (fontColor)
  9703. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9704. else
  9705. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9706. }
  9707. else
  9708. {
  9709. if (fontColor)
  9710. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9711. else
  9712. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9713. }
  9714. return SCRIPT_CMD_SUCCESS;
  9715. }
  9716. /*==========================================
  9717. *------------------------------------------*/
  9718. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9719. {
  9720. char *mes = va_arg(ap, char *);
  9721. int len = va_arg(ap, int);
  9722. int type = va_arg(ap, int);
  9723. char *fontColor = va_arg(ap, char *);
  9724. short fontType = (short)va_arg(ap, int);
  9725. short fontSize = (short)va_arg(ap, int);
  9726. short fontAlign = (short)va_arg(ap, int);
  9727. short fontY = (short)va_arg(ap, int);
  9728. if (fontColor)
  9729. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9730. else
  9731. clif_broadcast(bl, mes, len, type, SELF);
  9732. return SCRIPT_CMD_SUCCESS;
  9733. }
  9734. BUILDIN_FUNC(mapannounce)
  9735. {
  9736. const char *mapname = script_getstr(st,2);
  9737. const char *mes = script_getstr(st,3);
  9738. int flag = script_getnum(st,4);
  9739. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9740. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9741. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9742. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9743. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9744. int16 m;
  9745. if ((m = map_mapname2mapid(mapname)) < 0)
  9746. return SCRIPT_CMD_SUCCESS;
  9747. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9748. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9749. return SCRIPT_CMD_SUCCESS;
  9750. }
  9751. /*==========================================
  9752. *------------------------------------------*/
  9753. BUILDIN_FUNC(areaannounce)
  9754. {
  9755. const char *mapname = script_getstr(st,2);
  9756. int x0 = script_getnum(st,3);
  9757. int y0 = script_getnum(st,4);
  9758. int x1 = script_getnum(st,5);
  9759. int y1 = script_getnum(st,6);
  9760. const char *mes = script_getstr(st,7);
  9761. int flag = script_getnum(st,8);
  9762. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9763. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9764. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9765. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9766. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9767. int16 m;
  9768. if ((m = map_mapname2mapid(mapname)) < 0)
  9769. return SCRIPT_CMD_SUCCESS;
  9770. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9771. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9772. return SCRIPT_CMD_SUCCESS;
  9773. }
  9774. /*==========================================
  9775. *------------------------------------------*/
  9776. BUILDIN_FUNC(getusers)
  9777. {
  9778. int flag, val = 0;
  9779. struct map_session_data* sd;
  9780. struct block_list* bl = NULL;
  9781. flag = script_getnum(st,2);
  9782. switch(flag&0x07)
  9783. {
  9784. case 0:
  9785. if(flag&0x8)
  9786. {// npc
  9787. bl = map_id2bl(st->oid);
  9788. }
  9789. else if(script_rid2sd(sd))
  9790. {// pc
  9791. bl = &sd->bl;
  9792. }
  9793. if(bl)
  9794. {
  9795. val = map[bl->m].users;
  9796. }
  9797. break;
  9798. case 1:
  9799. val = map_getusers();
  9800. break;
  9801. default:
  9802. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9803. script_pushint(st,0);
  9804. return SCRIPT_CMD_FAILURE;
  9805. }
  9806. script_pushint(st,val);
  9807. return SCRIPT_CMD_SUCCESS;
  9808. }
  9809. /*==========================================
  9810. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9811. *------------------------------------------*/
  9812. BUILDIN_FUNC(getmapguildusers)
  9813. {
  9814. const char *str;
  9815. int16 m;
  9816. int gid;
  9817. int c=0;
  9818. struct guild *g = NULL;
  9819. str=script_getstr(st,2);
  9820. gid=script_getnum(st,3);
  9821. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9822. script_pushint(st,-1);
  9823. return SCRIPT_CMD_SUCCESS;
  9824. }
  9825. g = guild_search(gid);
  9826. if (g){
  9827. unsigned short i;
  9828. for(i = 0; i < g->max_member; i++)
  9829. {
  9830. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9831. c++;
  9832. }
  9833. }
  9834. script_pushint(st,c);
  9835. return SCRIPT_CMD_SUCCESS;
  9836. }
  9837. /*==========================================
  9838. *------------------------------------------*/
  9839. BUILDIN_FUNC(getmapusers)
  9840. {
  9841. const char *str;
  9842. int16 m;
  9843. str=script_getstr(st,2);
  9844. if( (m=map_mapname2mapid(str))< 0){
  9845. script_pushint(st,-1);
  9846. return SCRIPT_CMD_SUCCESS;
  9847. }
  9848. script_pushint(st,map[m].users);
  9849. return SCRIPT_CMD_SUCCESS;
  9850. }
  9851. /*==========================================
  9852. *------------------------------------------*/
  9853. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9854. {
  9855. int *users=va_arg(ap,int *);
  9856. (*users)++;
  9857. return SCRIPT_CMD_SUCCESS;
  9858. }
  9859. BUILDIN_FUNC(getareausers)
  9860. {
  9861. const char *str;
  9862. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9863. str=script_getstr(st,2);
  9864. x0=script_getnum(st,3);
  9865. y0=script_getnum(st,4);
  9866. x1=script_getnum(st,5);
  9867. y1=script_getnum(st,6);
  9868. if( (m=map_mapname2mapid(str))< 0){
  9869. script_pushint(st,-1);
  9870. return SCRIPT_CMD_SUCCESS;
  9871. }
  9872. map_foreachinallarea(buildin_getareausers_sub,
  9873. m,x0,y0,x1,y1,BL_PC,&users);
  9874. script_pushint(st,users);
  9875. return SCRIPT_CMD_SUCCESS;
  9876. }
  9877. /*==========================================
  9878. *------------------------------------------*/
  9879. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9880. {
  9881. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9882. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9883. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9884. if(drop->item.nameid==nameid)
  9885. (*amount)+=drop->item.amount;
  9886. return SCRIPT_CMD_SUCCESS;
  9887. }
  9888. BUILDIN_FUNC(getareadropitem)
  9889. {
  9890. const char *str;
  9891. int16 m,x0,y0,x1,y1;
  9892. unsigned short nameid, amount = 0;
  9893. struct script_data *data;
  9894. str=script_getstr(st,2);
  9895. x0=script_getnum(st,3);
  9896. y0=script_getnum(st,4);
  9897. x1=script_getnum(st,5);
  9898. y1=script_getnum(st,6);
  9899. data=script_getdata(st,7);
  9900. get_val(st,data);
  9901. if( data_isstring(data) ){
  9902. const char *name=conv_str(st,data);
  9903. struct item_data *item_data = itemdb_searchname(name);
  9904. nameid=UNKNOWN_ITEM_ID;
  9905. if( item_data )
  9906. nameid=item_data->nameid;
  9907. }else
  9908. nameid=conv_num(st,data);
  9909. if( (m=map_mapname2mapid(str))< 0){
  9910. script_pushint(st,-1);
  9911. return SCRIPT_CMD_SUCCESS;
  9912. }
  9913. map_foreachinallarea(buildin_getareadropitem_sub,
  9914. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9915. script_pushint(st,amount);
  9916. return SCRIPT_CMD_SUCCESS;
  9917. }
  9918. /*==========================================
  9919. *------------------------------------------*/
  9920. BUILDIN_FUNC(enablenpc)
  9921. {
  9922. const char *str;
  9923. str=script_getstr(st,2);
  9924. npc_enable(str,1);
  9925. return SCRIPT_CMD_SUCCESS;
  9926. }
  9927. /*==========================================
  9928. *------------------------------------------*/
  9929. BUILDIN_FUNC(disablenpc)
  9930. {
  9931. const char *str;
  9932. str=script_getstr(st,2);
  9933. npc_enable(str,0);
  9934. return SCRIPT_CMD_SUCCESS;
  9935. }
  9936. /*==========================================
  9937. *------------------------------------------*/
  9938. BUILDIN_FUNC(hideoffnpc)
  9939. {
  9940. const char *str;
  9941. str=script_getstr(st,2);
  9942. npc_enable(str,2);
  9943. return SCRIPT_CMD_SUCCESS;
  9944. }
  9945. /*==========================================
  9946. *------------------------------------------*/
  9947. BUILDIN_FUNC(hideonnpc)
  9948. {
  9949. const char *str;
  9950. str=script_getstr(st,2);
  9951. npc_enable(str,4);
  9952. return SCRIPT_CMD_SUCCESS;
  9953. }
  9954. /* Starts a status effect on the target unit or on the attached player.
  9955. *
  9956. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9957. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9958. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9959. * <flag>: enum e_status_change_start_flags
  9960. */
  9961. BUILDIN_FUNC(sc_start)
  9962. {
  9963. TBL_NPC * nd = map_id2nd(st->oid);
  9964. struct block_list* bl;
  9965. enum sc_type type;
  9966. int tick, val1, val2, val3, val4=0, rate, flag;
  9967. char start_type;
  9968. const char* command = script_getfuncname(st);
  9969. if(strstr(command, "4"))
  9970. start_type = 4;
  9971. else if(strstr(command, "2"))
  9972. start_type = 2;
  9973. else
  9974. start_type = 1;
  9975. type = (sc_type)script_getnum(st,2);
  9976. tick = script_getnum(st,3);
  9977. val1 = script_getnum(st,4);
  9978. //If from NPC we make default flag 1 to be unavoidable
  9979. if(nd && nd->bl.id == fake_nd->bl.id)
  9980. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9981. else
  9982. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9983. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9984. if(script_hasdata(st,(6+start_type)))
  9985. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9986. else
  9987. bl = map_id2bl(st->rid);
  9988. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9989. {// When there isn't a duration specified, try to get it from the skill_db
  9990. tick = skill_get_time(status_sc2skill(type), val1);
  9991. }
  9992. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9993. bl = map_id2bl(potion_target);
  9994. tick /= 2;// Thrown potions only last half.
  9995. val4 = 1;// Mark that this was a thrown sc_effect
  9996. }
  9997. if(!bl)
  9998. return SCRIPT_CMD_SUCCESS;
  9999. switch(start_type) {
  10000. case 1:
  10001. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  10002. break;
  10003. case 2:
  10004. val2 = script_getnum(st,5);
  10005. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  10006. break;
  10007. case 4:
  10008. val2 = script_getnum(st,5);
  10009. val3 = script_getnum(st,6);
  10010. val4 = script_getnum(st,7);
  10011. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  10012. break;
  10013. }
  10014. return SCRIPT_CMD_SUCCESS;
  10015. }
  10016. /// Ends one or all status effects on the target unit or on the attached player.
  10017. ///
  10018. /// sc_end <effect_id>{,<unit_id>};
  10019. BUILDIN_FUNC(sc_end)
  10020. {
  10021. struct block_list* bl;
  10022. int type;
  10023. type = script_getnum(st, 2);
  10024. if (script_hasdata(st, 3))
  10025. bl = map_id2bl(script_getnum(st, 3));
  10026. else
  10027. bl = map_id2bl(st->rid);
  10028. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  10029. bl = map_id2bl(potion_target);
  10030. if (!bl)
  10031. return SCRIPT_CMD_SUCCESS;
  10032. if (type >= 0 && type < SC_MAX) {
  10033. struct status_change *sc = status_get_sc(bl);
  10034. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  10035. if (!sce)
  10036. return SCRIPT_CMD_SUCCESS;
  10037. switch (type) {
  10038. case SC_WEIGHT50:
  10039. case SC_WEIGHT90:
  10040. case SC_NOCHAT:
  10041. case SC_PUSH_CART:
  10042. case SC_ALL_RIDING:
  10043. case SC_STYLE_CHANGE:
  10044. case SC_MONSTER_TRANSFORM:
  10045. case SC_ACTIVE_MONSTER_TRANSFORM:
  10046. case SC_MTF_ASPD:
  10047. case SC_MTF_RANGEATK:
  10048. case SC_MTF_MATK:
  10049. case SC_MTF_MLEATKED:
  10050. case SC_MTF_CRIDAMAGE:
  10051. case SC_MTF_ASPD2:
  10052. case SC_MTF_RANGEATK2:
  10053. case SC_MTF_MATK2:
  10054. case SC_MTF_MHP:
  10055. case SC_MTF_MSP:
  10056. case SC_MTF_PUMPKIN:
  10057. case SC_MTF_HITFLEE:
  10058. case SC_ATTHASTE_CASH:
  10059. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  10060. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  10061. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  10062. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  10063. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  10064. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  10065. return SCRIPT_CMD_SUCCESS;
  10066. default:
  10067. break;
  10068. }
  10069. //This should help status_change_end force disabling the SC in case it has no limit.
  10070. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  10071. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  10072. } else
  10073. status_change_clear(bl, 3); // remove all effects
  10074. return SCRIPT_CMD_SUCCESS;
  10075. }
  10076. /**
  10077. * Ends all status effects from any learned skill on the attached player.
  10078. * if <job_id> was given it will end the effect of that class for the attached player
  10079. * sc_end_class {<char_id>{,<job_id>}};
  10080. */
  10081. BUILDIN_FUNC(sc_end_class)
  10082. {
  10083. struct map_session_data *sd;
  10084. uint16 skill_id;
  10085. int class_;
  10086. if (!script_charid2sd(2, sd))
  10087. return SCRIPT_CMD_FAILURE;
  10088. if (script_hasdata(st, 3))
  10089. class_ = script_getnum(st, 3);
  10090. else
  10091. class_ = sd->status.class_;
  10092. if (!pcdb_checkid(class_)) {
  10093. ShowError("buildin_sc_end_class: Invalid job ID '%d' given.\n", script_getnum(st, 3));
  10094. return SCRIPT_CMD_FAILURE;
  10095. }
  10096. for (int i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[pc_class2idx(class_)][i].skill_id) > 0; i++) {
  10097. enum sc_type sc = status_skill2sc(skill_id);
  10098. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  10099. status_change_end(&sd->bl, sc, INVALID_TIMER);
  10100. }
  10101. return SCRIPT_CMD_SUCCESS;
  10102. }
  10103. /*==========================================
  10104. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  10105. *------------------------------------------*/
  10106. BUILDIN_FUNC(getscrate)
  10107. {
  10108. struct block_list *bl;
  10109. int type,rate;
  10110. type=script_getnum(st,2);
  10111. rate=script_getnum(st,3);
  10112. if( script_hasdata(st,4) ) //get for the bl assigned
  10113. bl = map_id2bl(script_getnum(st,4));
  10114. else
  10115. bl = map_id2bl(st->rid);
  10116. if (bl)
  10117. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  10118. script_pushint(st,rate);
  10119. return SCRIPT_CMD_SUCCESS;
  10120. }
  10121. /**
  10122. * getstatus(<effect type>{,<type>{,<char_id>}});
  10123. **/
  10124. BUILDIN_FUNC(getstatus)
  10125. {
  10126. int id, type;
  10127. struct map_session_data* sd;
  10128. if (!script_charid2sd(4,sd))
  10129. return SCRIPT_CMD_FAILURE;
  10130. id = script_getnum(st, 2);
  10131. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  10132. if( id <= SC_NONE || id >= SC_MAX )
  10133. {// invalid status type given
  10134. ShowWarning("script.cpp:getstatus: Invalid status type given (%d).\n", id);
  10135. return SCRIPT_CMD_SUCCESS;
  10136. }
  10137. if( sd->sc.count == 0 || !sd->sc.data[id] )
  10138. {// no status is active
  10139. script_pushint(st, 0);
  10140. return SCRIPT_CMD_SUCCESS;
  10141. }
  10142. switch( type )
  10143. {
  10144. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  10145. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  10146. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  10147. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  10148. case 5:
  10149. {
  10150. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  10151. if( timer )
  10152. {// return the amount of time remaining
  10153. script_pushint(st, timer->tick - gettick());
  10154. } else {
  10155. script_pushint(st, -1);
  10156. }
  10157. }
  10158. break;
  10159. default: script_pushint(st, 1); break;
  10160. }
  10161. return SCRIPT_CMD_SUCCESS;
  10162. }
  10163. /*==========================================
  10164. *
  10165. *------------------------------------------*/
  10166. BUILDIN_FUNC(debugmes)
  10167. {
  10168. const char *str;
  10169. str=script_getstr(st,2);
  10170. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  10171. return SCRIPT_CMD_SUCCESS;
  10172. }
  10173. /*==========================================
  10174. *------------------------------------------*/
  10175. BUILDIN_FUNC(catchpet)
  10176. {
  10177. int pet_id;
  10178. TBL_PC *sd;
  10179. if( !script_rid2sd(sd) )
  10180. return SCRIPT_CMD_SUCCESS;
  10181. pet_id= script_getnum(st,2);
  10182. pet_catch_process1(sd,pet_id);
  10183. return SCRIPT_CMD_SUCCESS;
  10184. }
  10185. /*==========================================
  10186. * [orn]
  10187. *------------------------------------------*/
  10188. BUILDIN_FUNC(homunculus_evolution)
  10189. {
  10190. TBL_PC *sd;
  10191. if( !script_rid2sd(sd) )
  10192. return SCRIPT_CMD_SUCCESS;
  10193. if(hom_is_active(sd->hd))
  10194. {
  10195. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  10196. hom_evolution(sd->hd);
  10197. else
  10198. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10199. }
  10200. return SCRIPT_CMD_SUCCESS;
  10201. }
  10202. /*==========================================
  10203. * Checks for vaporized morph state
  10204. * and deletes ITEMID_STRANGE_EMBRYO.
  10205. *------------------------------------------*/
  10206. BUILDIN_FUNC(homunculus_mutate)
  10207. {
  10208. int homun_id;
  10209. TBL_PC *sd;
  10210. if( !script_rid2sd(sd) || sd->hd == NULL )
  10211. return SCRIPT_CMD_SUCCESS;
  10212. if(script_hasdata(st,2))
  10213. homun_id = script_getnum(st,2);
  10214. else
  10215. homun_id = 6048 + (rnd() % 4);
  10216. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  10217. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10218. int m_id = hom_class2mapid(homun_id);
  10219. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  10220. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  10221. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  10222. hom_call(sd); // Respawn homunculus.
  10223. hom_mutate(sd->hd, homun_id);
  10224. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  10225. script_pushint(st, 1);
  10226. return SCRIPT_CMD_SUCCESS;
  10227. } else
  10228. clif_emotion(&sd->bl, ET_SWEAT);
  10229. } else
  10230. clif_emotion(&sd->bl, ET_SWEAT);
  10231. script_pushint(st, 0);
  10232. return SCRIPT_CMD_SUCCESS;
  10233. }
  10234. /*==========================================
  10235. * Puts homunculus into morph state
  10236. * and gives ITEMID_STRANGE_EMBRYO.
  10237. *------------------------------------------*/
  10238. BUILDIN_FUNC(morphembryo)
  10239. {
  10240. struct item item_tmp;
  10241. TBL_PC *sd;
  10242. if( !script_rid2sd(sd) || sd->hd == NULL )
  10243. return SCRIPT_CMD_SUCCESS;
  10244. if( hom_is_active(sd->hd) ) {
  10245. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10246. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10247. char i;
  10248. memset(&item_tmp, 0, sizeof(item_tmp));
  10249. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10250. item_tmp.identify = 1;
  10251. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10252. clif_additem(sd, 0, 0, i);
  10253. clif_emotion(&sd->bl, ET_SWEAT); // Fail to avoid item drop exploit.
  10254. } else {
  10255. hom_vaporize(sd, HOM_ST_MORPH);
  10256. script_pushint(st, 1);
  10257. return SCRIPT_CMD_SUCCESS;
  10258. }
  10259. } else
  10260. clif_emotion(&sd->hd->bl, ET_SWEAT);
  10261. } else
  10262. clif_emotion(&sd->bl, ET_SWEAT);
  10263. script_pushint(st, 0);
  10264. return SCRIPT_CMD_SUCCESS;
  10265. }
  10266. // [Zephyrus]
  10267. BUILDIN_FUNC(homunculus_shuffle)
  10268. {
  10269. TBL_PC *sd;
  10270. if( !script_rid2sd(sd) )
  10271. return SCRIPT_CMD_SUCCESS;
  10272. if(hom_is_active(sd->hd))
  10273. hom_shuffle(sd->hd);
  10274. return SCRIPT_CMD_SUCCESS;
  10275. }
  10276. /*==========================================
  10277. * Check for homunculus state.
  10278. * Return: -1 = No homunculus
  10279. * 0 = Homunculus is active
  10280. * 1 = Homunculus is vaporized (rest)
  10281. * 2 = Homunculus is in morph state
  10282. *------------------------------------------*/
  10283. BUILDIN_FUNC(checkhomcall)
  10284. {
  10285. TBL_PC *sd;
  10286. TBL_HOM *hd;
  10287. if( !script_rid2sd(sd) )
  10288. return SCRIPT_CMD_SUCCESS;
  10289. hd = sd->hd;
  10290. if( !hd )
  10291. script_pushint(st, -1);
  10292. else
  10293. script_pushint(st, hd->homunculus.vaporize);
  10294. return SCRIPT_CMD_SUCCESS;
  10295. }
  10296. //These two functions bring the eA MAPID_* class functionality to scripts.
  10297. BUILDIN_FUNC(eaclass)
  10298. {
  10299. int class_;
  10300. if( script_hasdata(st,2) )
  10301. class_ = script_getnum(st,2);
  10302. else {
  10303. TBL_PC *sd;
  10304. if (!script_charid2sd(3,sd)) {
  10305. script_pushint(st,-1);
  10306. return SCRIPT_CMD_SUCCESS;
  10307. }
  10308. class_ = sd->status.class_;
  10309. }
  10310. script_pushint(st,pc_jobid2mapid(class_));
  10311. return SCRIPT_CMD_SUCCESS;
  10312. }
  10313. BUILDIN_FUNC(roclass)
  10314. {
  10315. int class_ =script_getnum(st,2);
  10316. int sex;
  10317. if( script_hasdata(st,3) )
  10318. sex = script_getnum(st,3);
  10319. else {
  10320. TBL_PC *sd;
  10321. if (st->rid && script_rid2sd(sd))
  10322. sex = sd->status.sex;
  10323. else
  10324. sex = SEX_MALE; //Just use male when not found.
  10325. }
  10326. script_pushint(st,pc_mapid2jobid(class_, sex));
  10327. return SCRIPT_CMD_SUCCESS;
  10328. }
  10329. /*==========================================
  10330. * Tells client to open a hatching window, used for pet incubator
  10331. *------------------------------------------*/
  10332. BUILDIN_FUNC(birthpet)
  10333. {
  10334. TBL_PC *sd;
  10335. if( !script_rid2sd(sd) )
  10336. return SCRIPT_CMD_SUCCESS;
  10337. if( sd->status.pet_id )
  10338. {// do not send egg list, when you already have a pet
  10339. return SCRIPT_CMD_SUCCESS;
  10340. }
  10341. clif_sendegg(sd);
  10342. return SCRIPT_CMD_SUCCESS;
  10343. }
  10344. /**
  10345. * resetlvl <action type>{,<char_id>};
  10346. * @param action_type:
  10347. * 1 : make like after rebirth
  10348. * 2 : blvl,jlvl=1, skillpoint=0
  10349. * 3 : don't reset skill, blvl=1
  10350. * 4 : jlvl=0
  10351. * @author AppleGirl
  10352. **/
  10353. BUILDIN_FUNC(resetlvl)
  10354. {
  10355. TBL_PC *sd;
  10356. int type=script_getnum(st,2);
  10357. if (!script_charid2sd(3,sd))
  10358. return SCRIPT_CMD_FAILURE;
  10359. pc_resetlvl(sd,type);
  10360. return SCRIPT_CMD_SUCCESS;
  10361. }
  10362. /**
  10363. * Reset a player status point
  10364. * resetstatus({<char_id>});
  10365. **/
  10366. BUILDIN_FUNC(resetstatus)
  10367. {
  10368. TBL_PC *sd;
  10369. if (!script_charid2sd(2,sd))
  10370. return SCRIPT_CMD_FAILURE;
  10371. pc_resetstate(sd);
  10372. return SCRIPT_CMD_SUCCESS;
  10373. }
  10374. /**
  10375. * Reset player's skill
  10376. * resetskill({<char_id>});
  10377. **/
  10378. BUILDIN_FUNC(resetskill)
  10379. {
  10380. TBL_PC *sd;
  10381. if (!script_charid2sd(2,sd))
  10382. return SCRIPT_CMD_FAILURE;
  10383. pc_resetskill(sd,1);
  10384. return SCRIPT_CMD_SUCCESS;
  10385. }
  10386. /**
  10387. * Counts total amount of skill points.
  10388. * skillpointcount({<char_id>})
  10389. **/
  10390. BUILDIN_FUNC(skillpointcount)
  10391. {
  10392. TBL_PC *sd;
  10393. if (!script_charid2sd(2,sd))
  10394. return SCRIPT_CMD_FAILURE;
  10395. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10396. return SCRIPT_CMD_SUCCESS;
  10397. }
  10398. /*==========================================
  10399. *
  10400. *------------------------------------------*/
  10401. BUILDIN_FUNC(changebase)
  10402. {
  10403. TBL_PC *sd=NULL;
  10404. int vclass;
  10405. if( !script_mapid2sd(3,sd) )
  10406. return SCRIPT_CMD_SUCCESS;
  10407. vclass = script_getnum(st,2);
  10408. if(vclass == JOB_WEDDING)
  10409. {
  10410. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10411. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10412. )
  10413. return SCRIPT_CMD_SUCCESS;
  10414. }
  10415. if(!sd->disguise && vclass != sd->vd.class_) {
  10416. status_set_viewdata(&sd->bl, vclass);
  10417. //Updated client view. Base, Weapon and Cloth Colors.
  10418. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10419. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10420. if (sd->vd.cloth_color)
  10421. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10422. if (sd->vd.body_style)
  10423. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10424. clif_skillinfoblock(sd);
  10425. }
  10426. return SCRIPT_CMD_SUCCESS;
  10427. }
  10428. /**
  10429. * Change account sex and unequip all item and request for a changesex to char-serv
  10430. * changesex({<char_id>});
  10431. */
  10432. BUILDIN_FUNC(changesex)
  10433. {
  10434. int i;
  10435. TBL_PC *sd = NULL;
  10436. if (!script_charid2sd(2,sd))
  10437. return SCRIPT_CMD_FAILURE;
  10438. pc_resetskill(sd,4);
  10439. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10440. for(i = 0; i < EQI_MAX; i++) {
  10441. if (sd->equip_index[i] >= 0)
  10442. pc_unequipitem(sd, sd->equip_index[i], 3);
  10443. }
  10444. chrif_changesex(sd, true);
  10445. return SCRIPT_CMD_SUCCESS;
  10446. }
  10447. /**
  10448. * Change character's sex and unequip all item and request for a changesex to char-serv
  10449. * changecharsex({<char_id>});
  10450. */
  10451. BUILDIN_FUNC(changecharsex)
  10452. {
  10453. #if PACKETVER >= 20141016
  10454. int i;
  10455. TBL_PC *sd = NULL;
  10456. if (!script_charid2sd(2,sd))
  10457. return SCRIPT_CMD_FAILURE;
  10458. pc_resetskill(sd,4);
  10459. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10460. for (i = 0; i < EQI_MAX; i++) {
  10461. if (sd->equip_index[i] >= 0)
  10462. pc_unequipitem(sd, sd->equip_index[i], 3);
  10463. }
  10464. chrif_changesex(sd, false);
  10465. return SCRIPT_CMD_SUCCESS;
  10466. #else
  10467. return SCRIPT_CMD_FAILURE;
  10468. #endif
  10469. }
  10470. /*==========================================
  10471. * Works like 'announce' but outputs in the common chat window
  10472. *------------------------------------------*/
  10473. BUILDIN_FUNC(globalmes)
  10474. {
  10475. struct block_list *bl = map_id2bl(st->oid);
  10476. struct npc_data *nd = (struct npc_data *)bl;
  10477. const char *name=NULL,*mes;
  10478. mes=script_getstr(st,2);
  10479. if(mes==NULL)
  10480. return SCRIPT_CMD_SUCCESS;
  10481. if(script_hasdata(st,3)){ // npc name to display
  10482. name=script_getstr(st,3);
  10483. } else {
  10484. name=nd->name; //use current npc name
  10485. }
  10486. npc_globalmessage(name,mes); // broadcast to all players connected
  10487. return SCRIPT_CMD_SUCCESS;
  10488. }
  10489. /////////////////////////////////////////////////////////////////////
  10490. // NPC waiting room (chat room)
  10491. //
  10492. /// Creates a waiting room (chat room) for this npc.
  10493. ///
  10494. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10495. BUILDIN_FUNC(waitingroom)
  10496. {
  10497. struct npc_data* nd;
  10498. const char* title = script_getstr(st, 2);
  10499. int limit = script_getnum(st, 3);
  10500. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10501. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10502. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10503. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10504. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10505. nd = (struct npc_data *)map_id2bl(st->oid);
  10506. if( nd != NULL )
  10507. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10508. return SCRIPT_CMD_SUCCESS;
  10509. }
  10510. /// Removes the waiting room of the current or target npc.
  10511. ///
  10512. /// delwaitingroom "<npc_name>";
  10513. /// delwaitingroom;
  10514. BUILDIN_FUNC(delwaitingroom)
  10515. {
  10516. struct npc_data* nd;
  10517. if( script_hasdata(st,2) )
  10518. nd = npc_name2id(script_getstr(st, 2));
  10519. else
  10520. nd = (struct npc_data *)map_id2bl(st->oid);
  10521. if( nd != NULL )
  10522. chat_deletenpcchat(nd);
  10523. return SCRIPT_CMD_SUCCESS;
  10524. }
  10525. /// Kick the specified player from the waiting room of the target npc.
  10526. ///
  10527. /// waitingroomkick "<npc_name>", <kickusername>;
  10528. BUILDIN_FUNC(waitingroomkick)
  10529. {
  10530. struct npc_data* nd;
  10531. struct chat_data* cd;
  10532. const char* kickusername;
  10533. nd = npc_name2id(script_getstr(st,2));
  10534. kickusername = script_getstr(st,3);
  10535. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10536. chat_npckickchat(cd, kickusername);
  10537. return SCRIPT_CMD_SUCCESS;
  10538. }
  10539. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10540. /// Num users stored in .@waitingroom_usercount
  10541. ///
  10542. /// getwaitingroomusers "<npc_name>";
  10543. BUILDIN_FUNC(getwaitingroomusers)
  10544. {
  10545. struct npc_data* nd;
  10546. struct chat_data* cd;
  10547. int i, j=0;
  10548. if( script_hasdata(st,2) )
  10549. nd = npc_name2id(script_getstr(st, 2));
  10550. else
  10551. nd = (struct npc_data *)map_id2bl(st->oid);
  10552. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10553. for(i = 0; i < cd->users; ++i) {
  10554. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  10555. j++;
  10556. }
  10557. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  10558. }
  10559. return SCRIPT_CMD_SUCCESS;
  10560. }
  10561. /// Kicks all the players from the waiting room of the current or target npc.
  10562. ///
  10563. /// kickwaitingroomall "<npc_name>";
  10564. /// kickwaitingroomall;
  10565. BUILDIN_FUNC(waitingroomkickall)
  10566. {
  10567. struct npc_data* nd;
  10568. struct chat_data* cd;
  10569. if( script_hasdata(st,2) )
  10570. nd = npc_name2id(script_getstr(st,2));
  10571. else
  10572. nd = (struct npc_data *)map_id2bl(st->oid);
  10573. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10574. chat_npckickall(cd);
  10575. return SCRIPT_CMD_SUCCESS;
  10576. }
  10577. /// Enables the waiting room event of the current or target npc.
  10578. ///
  10579. /// enablewaitingroomevent "<npc_name>";
  10580. /// enablewaitingroomevent;
  10581. BUILDIN_FUNC(enablewaitingroomevent)
  10582. {
  10583. struct npc_data* nd;
  10584. struct chat_data* cd;
  10585. if( script_hasdata(st,2) )
  10586. nd = npc_name2id(script_getstr(st, 2));
  10587. else
  10588. nd = (struct npc_data *)map_id2bl(st->oid);
  10589. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10590. chat_enableevent(cd);
  10591. return SCRIPT_CMD_SUCCESS;
  10592. }
  10593. /// Disables the waiting room event of the current or target npc.
  10594. ///
  10595. /// disablewaitingroomevent "<npc_name>";
  10596. /// disablewaitingroomevent;
  10597. BUILDIN_FUNC(disablewaitingroomevent)
  10598. {
  10599. struct npc_data *nd;
  10600. struct chat_data *cd;
  10601. if( script_hasdata(st,2) )
  10602. nd = npc_name2id(script_getstr(st, 2));
  10603. else
  10604. nd = (struct npc_data *)map_id2bl(st->oid);
  10605. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10606. chat_disableevent(cd);
  10607. return SCRIPT_CMD_SUCCESS;
  10608. }
  10609. /// Returns info on the waiting room of the current or target npc.
  10610. /// Returns -1 if the type unknown
  10611. /// <type>=0 : current number of users
  10612. /// <type>=1 : maximum number of users allowed
  10613. /// <type>=2 : the number of users that trigger the event
  10614. /// <type>=3 : if the trigger is disabled
  10615. /// <type>=4 : the title of the waiting room
  10616. /// <type>=5 : the password of the waiting room
  10617. /// <type>=16 : the name of the waiting room event
  10618. /// <type>=32 : if the waiting room is full
  10619. /// <type>=33 : if there are enough users to trigger the event
  10620. ///
  10621. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10622. /// getwaitingroomstate(<type>) -> <info>
  10623. BUILDIN_FUNC(getwaitingroomstate)
  10624. {
  10625. struct npc_data *nd;
  10626. struct chat_data *cd;
  10627. int type;
  10628. type = script_getnum(st,2);
  10629. if( script_hasdata(st,3) )
  10630. nd = npc_name2id(script_getstr(st, 3));
  10631. else
  10632. nd = (struct npc_data *)map_id2bl(st->oid);
  10633. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10634. {
  10635. script_pushint(st, -1);
  10636. return SCRIPT_CMD_SUCCESS;
  10637. }
  10638. switch(type)
  10639. {
  10640. case 0: script_pushint(st, cd->users); break;
  10641. case 1: script_pushint(st, cd->limit); break;
  10642. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10643. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10644. case 4: script_pushstrcopy(st, cd->title); break;
  10645. case 5: script_pushstrcopy(st, cd->pass); break;
  10646. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10647. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10648. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10649. default: script_pushint(st, -1); break;
  10650. }
  10651. return SCRIPT_CMD_SUCCESS;
  10652. }
  10653. /// Warps the trigger or target amount of players to the target map and position.
  10654. /// Players are automatically removed from the waiting room.
  10655. /// Those waiting the longest will get warped first.
  10656. /// The target map can be "Random" for a random position in the current map,
  10657. /// and "SavePoint" for the savepoint map+position.
  10658. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10659. ///
  10660. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10661. /// The total number of teleported players is put into $@warpwaitingpcnum
  10662. ///
  10663. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10664. /// warpwaitingpc "<map name>",<x>,<y>;
  10665. BUILDIN_FUNC(warpwaitingpc)
  10666. {
  10667. int x;
  10668. int y;
  10669. int i;
  10670. int n;
  10671. const char* map_name;
  10672. struct npc_data* nd;
  10673. struct chat_data* cd;
  10674. nd = (struct npc_data *)map_id2bl(st->oid);
  10675. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10676. return SCRIPT_CMD_SUCCESS;
  10677. map_name = script_getstr(st,2);
  10678. x = script_getnum(st,3);
  10679. y = script_getnum(st,4);
  10680. n = cd->trigger&0x7f;
  10681. if( script_hasdata(st,5) )
  10682. n = script_getnum(st,5);
  10683. for( i = 0; i < n && cd->users > 0; i++ )
  10684. {
  10685. TBL_PC* sd = cd->usersd[0];
  10686. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10687. {// can't teleport on this map
  10688. break;
  10689. }
  10690. if( cd->zeny )
  10691. {// fee set
  10692. if( (uint32)sd->status.zeny < cd->zeny )
  10693. {// no zeny to cover set fee
  10694. break;
  10695. }
  10696. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10697. }
  10698. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10699. if( strcmp(map_name,"Random") == 0 )
  10700. pc_randomwarp(sd,CLR_TELEPORT);
  10701. else if( strcmp(map_name,"SavePoint") == 0 )
  10702. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10703. else
  10704. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10705. }
  10706. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10707. return SCRIPT_CMD_SUCCESS;
  10708. }
  10709. /////////////////////////////////////////////////////////////////////
  10710. // ...
  10711. //
  10712. /// Detaches a character from a script.
  10713. ///
  10714. /// @param st Script state to detach the character from.
  10715. static void script_detach_rid(struct script_state* st)
  10716. {
  10717. if(st->rid)
  10718. {
  10719. script_detach_state(st, false);
  10720. st->rid = 0;
  10721. }
  10722. }
  10723. /*=========================================================================
  10724. * Attaches a set of RIDs to the current script. [digitalhamster]
  10725. * addrid(<type>{,<flag>{,<parameters>}});
  10726. * <type>:
  10727. * 0 : All players in the server.
  10728. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10729. * 2 : Party members of a specified party ID.
  10730. * [ Parameters: <party id> ]
  10731. * 3 : Guild members of a specified guild ID.
  10732. * [ Parameters: <guild id> ]
  10733. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10734. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10735. * 5 : All players in the map.
  10736. * [ Parameters: "<map name>" ]
  10737. * Account ID: The specified account ID.
  10738. * <flag>:
  10739. * 0 : Players are always attached. (default)
  10740. * 1 : Players currently running another script will not be attached.
  10741. *-------------------------------------------------------------------------*/
  10742. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10743. {
  10744. int forceflag;
  10745. struct map_session_data *sd = (TBL_PC *)bl;
  10746. struct script_state* st;
  10747. st = va_arg(ap,struct script_state*);
  10748. forceflag = va_arg(ap,int);
  10749. if(!forceflag || !sd->st)
  10750. if(sd->status.account_id != st->rid)
  10751. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10752. return 0;
  10753. }
  10754. BUILDIN_FUNC(addrid)
  10755. {
  10756. struct s_mapiterator* iter;
  10757. struct block_list *bl;
  10758. TBL_PC *sd;
  10759. if(st->rid < 1) {
  10760. st->state = END;
  10761. bl = map_id2bl(st->oid);
  10762. } else
  10763. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10764. iter = mapit_getallusers();
  10765. switch(script_getnum(st,2)) {
  10766. case 0:
  10767. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10768. if(!script_getnum(st,3) || !sd->st)
  10769. if(sd->status.account_id != st->rid) //attached player already runs.
  10770. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10771. }
  10772. break;
  10773. case 1:
  10774. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10775. if(!script_getnum(st,3) || !sd->st)
  10776. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10777. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10778. }
  10779. break;
  10780. case 2:
  10781. if(script_getnum(st,4) == 0) {
  10782. script_pushint(st,0);
  10783. return SCRIPT_CMD_SUCCESS;
  10784. }
  10785. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10786. if(!script_getnum(st,3) || !sd->st)
  10787. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10788. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10789. }
  10790. break;
  10791. case 3:
  10792. if(script_getnum(st,4) == 0) {
  10793. script_pushint(st,0);
  10794. return SCRIPT_CMD_SUCCESS;
  10795. }
  10796. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10797. if(!script_getnum(st,3) || !sd->st)
  10798. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10799. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10800. }
  10801. break;
  10802. case 4:
  10803. map_foreachinallarea(buildin_addrid_sub,
  10804. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10805. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10806. break;
  10807. case 5:
  10808. if (script_getstr(st, 4) == NULL) {
  10809. script_pushint(st, 0);
  10810. return SCRIPT_CMD_FAILURE;
  10811. }
  10812. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10813. script_pushint(st, 0);
  10814. return SCRIPT_CMD_FAILURE;
  10815. }
  10816. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10817. break;
  10818. default:
  10819. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10820. script_pushint(st,0);
  10821. return SCRIPT_CMD_SUCCESS;
  10822. }
  10823. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10824. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10825. script_pushint(st,1);
  10826. }
  10827. return SCRIPT_CMD_SUCCESS;
  10828. }
  10829. mapit_free(iter);
  10830. script_pushint(st,1);
  10831. return SCRIPT_CMD_SUCCESS;
  10832. }
  10833. /*==========================================
  10834. * Attach sd char id to script and detach current one if any
  10835. *------------------------------------------*/
  10836. BUILDIN_FUNC(attachrid)
  10837. {
  10838. int rid = script_getnum(st,2);
  10839. if (map_id2sd(rid) != NULL) {
  10840. script_detach_rid(st);
  10841. st->rid = rid;
  10842. script_attach_state(st);
  10843. script_pushint(st,1);
  10844. } else
  10845. script_pushint(st,0);
  10846. return SCRIPT_CMD_SUCCESS;
  10847. }
  10848. /*==========================================
  10849. * Detach script to rid
  10850. *------------------------------------------*/
  10851. BUILDIN_FUNC(detachrid)
  10852. {
  10853. script_detach_rid(st);
  10854. return SCRIPT_CMD_SUCCESS;
  10855. }
  10856. /*==========================================
  10857. * Chk if account connected, (and charid from account if specified)
  10858. *------------------------------------------*/
  10859. BUILDIN_FUNC(isloggedin)
  10860. {
  10861. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10862. if (script_hasdata(st,3) && sd &&
  10863. sd->status.char_id != script_getnum(st,3))
  10864. sd = NULL;
  10865. push_val(st->stack,C_INT,sd!=NULL);
  10866. return SCRIPT_CMD_SUCCESS;
  10867. }
  10868. /*==========================================
  10869. *
  10870. *------------------------------------------*/
  10871. BUILDIN_FUNC(setmapflagnosave)
  10872. {
  10873. int16 m,x,y;
  10874. unsigned short mapindex;
  10875. const char *str,*str2;
  10876. str=script_getstr(st,2);
  10877. str2=script_getstr(st,3);
  10878. x=script_getnum(st,4);
  10879. y=script_getnum(st,5);
  10880. m = map_mapname2mapid(str);
  10881. mapindex = mapindex_name2id(str2);
  10882. if(m >= 0 && mapindex) {
  10883. map[m].flag.nosave=1;
  10884. map[m].save.map=mapindex;
  10885. map[m].save.x=x;
  10886. map[m].save.y=y;
  10887. }
  10888. return SCRIPT_CMD_SUCCESS;
  10889. }
  10890. BUILDIN_FUNC(getmapflag)
  10891. {
  10892. int16 m,i;
  10893. const char *str;
  10894. str=script_getstr(st,2);
  10895. i=script_getnum(st,3);
  10896. m = map_mapname2mapid(str);
  10897. if(m >= 0) {
  10898. switch(i) {
  10899. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10900. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10901. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10902. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10903. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10904. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10905. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10906. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10907. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10908. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10909. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10910. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10911. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10912. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10913. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10914. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10915. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10916. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10917. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10918. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10919. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10920. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10921. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10922. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10923. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10924. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10925. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10926. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10927. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10928. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10929. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10930. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10931. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10932. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10933. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10934. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10935. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10936. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10937. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10938. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10939. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10940. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10941. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10942. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10943. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10944. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10945. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10946. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10947. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10948. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10949. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10950. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10951. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10952. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10953. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10954. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10955. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10956. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10957. case MF_NOCOSTUME: script_pushint(st,map[m].flag.nocostume); break;
  10958. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10959. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10960. case MF_HIDEMOBHPBAR: script_pushint(st,map[m].flag.hidemobhpbar); break;
  10961. #ifdef ADJUST_SKILL_DAMAGE
  10962. case MF_SKILL_DAMAGE:
  10963. {
  10964. int ret_val=0, type=0;
  10965. FETCH(4,type);
  10966. switch (type) {
  10967. case 1: ret_val = map[m].adjust.damage.pc; break;
  10968. case 2: ret_val = map[m].adjust.damage.mob; break;
  10969. case 3: ret_val = map[m].adjust.damage.boss; break;
  10970. case 4: ret_val = map[m].adjust.damage.other; break;
  10971. case 5: ret_val = map[m].adjust.damage.caster; break;
  10972. default: ret_val = map[m].flag.skill_damage; break;
  10973. }
  10974. script_pushint(st,ret_val); break;
  10975. } break;
  10976. #endif
  10977. }
  10978. }
  10979. return SCRIPT_CMD_SUCCESS;
  10980. }
  10981. /* pvp timer handling */
  10982. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10983. TBL_PC* sd = (TBL_PC*)bl;
  10984. if (sd->pvp_timer == INVALID_TIMER) {
  10985. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10986. sd->pvp_rank = 0;
  10987. sd->pvp_lastusers = 0;
  10988. sd->pvp_point = 5;
  10989. sd->pvp_won = 0;
  10990. sd->pvp_lost = 0;
  10991. }
  10992. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE,SELF);
  10993. return 0;
  10994. }
  10995. BUILDIN_FUNC(setmapflag)
  10996. {
  10997. int16 m,i;
  10998. const char *str;
  10999. int val=0;
  11000. str=script_getstr(st,2);
  11001. i=script_getnum(st,3);
  11002. FETCH(4,val);
  11003. m = map_mapname2mapid(str);
  11004. if(m >= 0) {
  11005. switch(i) {
  11006. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  11007. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  11008. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  11009. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  11010. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  11011. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  11012. case MF_PVP:
  11013. map[m].flag.pvp = 1;
  11014. if( !battle_config.pk_mode ) {
  11015. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  11016. }
  11017. break;
  11018. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  11019. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  11020. case MF_GVG:
  11021. map[m].flag.gvg = 1;
  11022. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  11023. break;
  11024. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  11025. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  11026. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  11027. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  11028. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  11029. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  11030. case MF_SNOW: map[m].flag.snow = 1; break;
  11031. case MF_FOG: map[m].flag.fog = 1; break;
  11032. case MF_SAKURA: map[m].flag.sakura = 1; break;
  11033. case MF_LEAVES: map[m].flag.leaves = 1; break;
  11034. case MF_NOGO: map[m].flag.nogo = 1; break;
  11035. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  11036. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  11037. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  11038. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  11039. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  11040. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  11041. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  11042. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  11043. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  11044. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  11045. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  11046. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  11047. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  11048. case MF_RESTRICTED:
  11049. map[m].zone |= 1<<(val+1);
  11050. map[m].flag.restricted=1;
  11051. break;
  11052. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  11053. case MF_NODROP: map[m].flag.nodrop = 1; break;
  11054. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  11055. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  11056. case MF_NOVENDING: map[m].flag.novending = 1; break;
  11057. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  11058. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  11059. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  11060. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  11061. case MF_TOWN: map[m].flag.town = 1; break;
  11062. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  11063. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  11064. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  11065. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  11066. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  11067. case MF_RESET: map[m].flag.reset = 1; break;
  11068. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  11069. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  11070. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  11071. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  11072. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  11073. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  11074. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  11075. case MF_NOCOSTUME: map[m].flag.nocostume = 1; break;
  11076. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  11077. case MF_GVG_TE:
  11078. map[m].flag.gvg_te = 1;
  11079. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  11080. break;
  11081. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 1; break;
  11082. #ifdef ADJUST_SKILL_DAMAGE
  11083. case MF_SKILL_DAMAGE:
  11084. {
  11085. int type=0;
  11086. FETCH(5,type);
  11087. switch (type) {
  11088. case 1: map[m].adjust.damage.pc = val; break;
  11089. case 2: map[m].adjust.damage.mob = val; break;
  11090. case 3: map[m].adjust.damage.boss = val; break;
  11091. case 4: map[m].adjust.damage.other = val; break;
  11092. case 5: map[m].adjust.damage.caster = val; break;
  11093. }
  11094. map[m].flag.skill_damage = 1;
  11095. } break;
  11096. #endif
  11097. }
  11098. }
  11099. return SCRIPT_CMD_SUCCESS;
  11100. }
  11101. BUILDIN_FUNC(removemapflag)
  11102. {
  11103. int16 m,i;
  11104. const char *str;
  11105. int val=0;
  11106. str=script_getstr(st,2);
  11107. i=script_getnum(st,3);
  11108. FETCH(4,val);
  11109. m = map_mapname2mapid(str);
  11110. if(m >= 0) {
  11111. switch(i) {
  11112. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  11113. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  11114. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  11115. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  11116. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  11117. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  11118. case MF_PVP:
  11119. map[m].flag.pvp = 0;
  11120. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11121. break;
  11122. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  11123. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  11124. case MF_GVG:
  11125. map[m].flag.gvg = 0;
  11126. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11127. break;
  11128. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  11129. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  11130. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  11131. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  11132. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  11133. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  11134. case MF_SNOW: map[m].flag.snow = 0; break;
  11135. case MF_FOG: map[m].flag.fog = 0; break;
  11136. case MF_SAKURA: map[m].flag.sakura = 0; break;
  11137. case MF_LEAVES: map[m].flag.leaves = 0; break;
  11138. case MF_NOGO: map[m].flag.nogo = 0; break;
  11139. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  11140. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  11141. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  11142. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  11143. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  11144. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  11145. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  11146. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  11147. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  11148. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  11149. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  11150. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  11151. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  11152. case MF_RESTRICTED:
  11153. map[m].zone ^= 1<<(val+1);
  11154. if (map[m].zone == 0){
  11155. map[m].flag.restricted=0;
  11156. }
  11157. break;
  11158. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  11159. case MF_NODROP: map[m].flag.nodrop = 0; break;
  11160. case MF_JEXP: map[m].adjust.jexp = 0; break;
  11161. case MF_BEXP: map[m].adjust.bexp = 0; break;
  11162. case MF_NOVENDING: map[m].flag.novending = 0; break;
  11163. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  11164. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  11165. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  11166. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  11167. case MF_TOWN: map[m].flag.town = 0; break;
  11168. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  11169. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  11170. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  11171. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  11172. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  11173. case MF_RESET: map[m].flag.reset = 0; break;
  11174. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  11175. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  11176. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  11177. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  11178. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  11179. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  11180. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  11181. case MF_NOCOSTUME: map[m].flag.nocostume = 0; break;
  11182. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  11183. case MF_GVG_TE:
  11184. map[m].flag.gvg_te = 0;
  11185. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11186. break;
  11187. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 0; break;
  11188. #ifdef ADJUST_SKILL_DAMAGE
  11189. case MF_SKILL_DAMAGE:
  11190. {
  11191. map[m].flag.skill_damage = 0;
  11192. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  11193. if (map[m].skill_damage.count)
  11194. map_skill_damage_free(&map[m]);
  11195. } break;
  11196. #endif
  11197. }
  11198. }
  11199. return SCRIPT_CMD_SUCCESS;
  11200. }
  11201. BUILDIN_FUNC(pvpon)
  11202. {
  11203. int16 m;
  11204. const char *str;
  11205. TBL_PC* sd = NULL;
  11206. struct s_mapiterator* iter;
  11207. str = script_getstr(st,2);
  11208. m = map_mapname2mapid(str);
  11209. if( m < 0 || map[m].flag.pvp )
  11210. return SCRIPT_CMD_SUCCESS; // nothing to do
  11211. map[m].flag.pvp = 1;
  11212. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  11213. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  11214. return SCRIPT_CMD_SUCCESS;
  11215. iter = mapit_getallusers();
  11216. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11217. {
  11218. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  11219. continue; // not applicable
  11220. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  11221. sd->pvp_rank = 0;
  11222. sd->pvp_lastusers = 0;
  11223. sd->pvp_point = 5;
  11224. sd->pvp_won = 0;
  11225. sd->pvp_lost = 0;
  11226. }
  11227. mapit_free(iter);
  11228. return SCRIPT_CMD_SUCCESS;
  11229. }
  11230. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  11231. {
  11232. TBL_PC* sd = (TBL_PC*)bl;
  11233. clif_pvpset(sd, 0, 0, 2);
  11234. if (sd->pvp_timer != INVALID_TIMER) {
  11235. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  11236. sd->pvp_timer = INVALID_TIMER;
  11237. }
  11238. return 0;
  11239. }
  11240. BUILDIN_FUNC(pvpoff)
  11241. {
  11242. int16 m;
  11243. const char *str;
  11244. str=script_getstr(st,2);
  11245. m = map_mapname2mapid(str);
  11246. if(m < 0 || !map[m].flag.pvp)
  11247. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  11248. map[m].flag.pvp = 0;
  11249. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11250. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  11251. return SCRIPT_CMD_SUCCESS;
  11252. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  11253. return SCRIPT_CMD_SUCCESS;
  11254. }
  11255. BUILDIN_FUNC(gvgon)
  11256. {
  11257. int16 m;
  11258. const char *str;
  11259. str=script_getstr(st,2);
  11260. m = map_mapname2mapid(str);
  11261. if(m >= 0 && !map[m].flag.gvg) {
  11262. map[m].flag.gvg = 1;
  11263. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  11264. }
  11265. return SCRIPT_CMD_SUCCESS;
  11266. }
  11267. BUILDIN_FUNC(gvgoff)
  11268. {
  11269. int16 m;
  11270. const char *str;
  11271. str=script_getstr(st,2);
  11272. m = map_mapname2mapid(str);
  11273. if(m >= 0 && map[m].flag.gvg) {
  11274. map[m].flag.gvg = 0;
  11275. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11276. }
  11277. return SCRIPT_CMD_SUCCESS;
  11278. }
  11279. BUILDIN_FUNC(gvgon3)
  11280. {
  11281. int16 m;
  11282. const char *str;
  11283. str = script_getstr(st,2);
  11284. m = map_mapname2mapid(str);
  11285. if (m >= 0 && !map[m].flag.gvg_te) {
  11286. map[m].flag.gvg_te = 1;
  11287. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  11288. }
  11289. return SCRIPT_CMD_SUCCESS;
  11290. }
  11291. BUILDIN_FUNC(gvgoff3)
  11292. {
  11293. int16 m;
  11294. const char *str;
  11295. str = script_getstr(st,2);
  11296. m = map_mapname2mapid(str);
  11297. if (m >= 0 && map[m].flag.gvg_te) {
  11298. map[m].flag.gvg_te = 0;
  11299. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11300. }
  11301. return SCRIPT_CMD_SUCCESS;
  11302. }
  11303. /**
  11304. * Shows an emotion on top of a NPC by default or the given GID
  11305. * emotion <emotion ID>{,<target ID>};
  11306. */
  11307. BUILDIN_FUNC(emotion)
  11308. {
  11309. struct block_list *bl = NULL;
  11310. int type = script_getnum(st,2);
  11311. if (type < ET_SURPRISE || type >= ET_MAX) {
  11312. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", type, ET_SURPRISE, (ET_MAX-1));
  11313. return SCRIPT_CMD_FAILURE;
  11314. }
  11315. if (script_hasdata(st, 3) && !script_rid2bl(3, bl)) {
  11316. ShowWarning("buildin_emotion: Unknown game ID supplied %d.\n", script_getnum(st, 3));
  11317. return SCRIPT_CMD_FAILURE;
  11318. }
  11319. if (!bl)
  11320. bl = map_id2bl(st->oid);
  11321. clif_emotion(bl, type);
  11322. return SCRIPT_CMD_SUCCESS;
  11323. }
  11324. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11325. {
  11326. int16 m=va_arg(ap,int);
  11327. int g_id=va_arg(ap,int);
  11328. int flag=va_arg(ap,int);
  11329. if(!sd || sd->bl.m != m)
  11330. return 0;
  11331. if(
  11332. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11333. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11334. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11335. )
  11336. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11337. return 1;
  11338. }
  11339. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11340. {
  11341. struct mob_data *md=(struct mob_data *)bl;
  11342. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11343. status_kill(bl);
  11344. return 1;
  11345. }
  11346. /*
  11347. * Function to kick guild members out of a map and to their save points.
  11348. * m : mapid
  11349. * g_id : owner guild id
  11350. * flag & 1 : Warp out owners
  11351. * flag & 2 : Warp out outsiders
  11352. * flag & 4 : reserved for mobs
  11353. * */
  11354. BUILDIN_FUNC(maprespawnguildid)
  11355. {
  11356. const char *mapname=script_getstr(st,2);
  11357. int g_id=script_getnum(st,3);
  11358. int flag=script_getnum(st,4);
  11359. int16 m=map_mapname2mapid(mapname);
  11360. if(m == -1)
  11361. return SCRIPT_CMD_SUCCESS;
  11362. //Catch ALL players (in case some are 'between maps' on execution time)
  11363. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11364. if (flag&4) //Remove script mobs.
  11365. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11366. return SCRIPT_CMD_SUCCESS;
  11367. }
  11368. /// Siege commands
  11369. /**
  11370. * Start WoE:FE
  11371. * agitstart();
  11372. */
  11373. BUILDIN_FUNC(agitstart)
  11374. {
  11375. guild_agit_start();
  11376. return SCRIPT_CMD_SUCCESS;
  11377. }
  11378. /**
  11379. * End WoE:FE
  11380. * agitend();
  11381. */
  11382. BUILDIN_FUNC(agitend)
  11383. {
  11384. guild_agit_end();
  11385. return SCRIPT_CMD_SUCCESS;
  11386. }
  11387. /**
  11388. * Start WoE:SE
  11389. * agitstart2();
  11390. */
  11391. BUILDIN_FUNC(agitstart2)
  11392. {
  11393. guild_agit2_start();
  11394. return SCRIPT_CMD_SUCCESS;
  11395. }
  11396. /**
  11397. * End WoE:SE
  11398. * agitend();
  11399. */
  11400. BUILDIN_FUNC(agitend2)
  11401. {
  11402. guild_agit2_end();
  11403. return SCRIPT_CMD_SUCCESS;
  11404. }
  11405. /**
  11406. * Start WoE:TE
  11407. * agitstart3();
  11408. */
  11409. BUILDIN_FUNC(agitstart3)
  11410. {
  11411. guild_agit3_start();
  11412. return SCRIPT_CMD_SUCCESS;
  11413. }
  11414. /**
  11415. * End WoE:TE
  11416. * agitend3();
  11417. */
  11418. BUILDIN_FUNC(agitend3)
  11419. {
  11420. guild_agit3_end();
  11421. return SCRIPT_CMD_SUCCESS;
  11422. }
  11423. /**
  11424. * Returns whether WoE:FE is on or off.
  11425. * agitcheck();
  11426. */
  11427. BUILDIN_FUNC(agitcheck)
  11428. {
  11429. script_pushint(st, agit_flag);
  11430. return SCRIPT_CMD_SUCCESS;
  11431. }
  11432. /**
  11433. * Returns whether WoE:SE is on or off.
  11434. * agitcheck2();
  11435. */
  11436. BUILDIN_FUNC(agitcheck2)
  11437. {
  11438. script_pushint(st, agit2_flag);
  11439. return SCRIPT_CMD_SUCCESS;
  11440. }
  11441. /**
  11442. * Returns whether WoE:TE is on or off.
  11443. * agitcheck3();
  11444. */
  11445. BUILDIN_FUNC(agitcheck3)
  11446. {
  11447. script_pushint(st, agit3_flag);
  11448. return SCRIPT_CMD_SUCCESS;
  11449. }
  11450. /// Sets the guild_id of this npc.
  11451. ///
  11452. /// flagemblem <guild_id>;
  11453. BUILDIN_FUNC(flagemblem)
  11454. {
  11455. TBL_NPC* nd;
  11456. int g_id = script_getnum(st,2);
  11457. if(g_id < 0)
  11458. return SCRIPT_CMD_SUCCESS;
  11459. nd = (TBL_NPC*)map_id2nd(st->oid);
  11460. if( nd == NULL ) {
  11461. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11462. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11463. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11464. } else {
  11465. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11466. nd->u.scr.guild_id = g_id;
  11467. clif_guild_emblem_area(&nd->bl);
  11468. /* guild flag caching */
  11469. if( g_id ) /* adding a id */
  11470. guild_flag_add(nd);
  11471. else if( changed ) /* removing a flag */
  11472. guild_flag_remove(nd);
  11473. }
  11474. return SCRIPT_CMD_SUCCESS;
  11475. }
  11476. BUILDIN_FUNC(getcastlename)
  11477. {
  11478. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11479. struct guild_castle* gc = guild_mapname2gc(mapname);
  11480. const char* name = (gc) ? gc->castle_name : "";
  11481. script_pushstrcopy(st,name);
  11482. return SCRIPT_CMD_SUCCESS;
  11483. }
  11484. BUILDIN_FUNC(getcastledata)
  11485. {
  11486. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11487. int index = script_getnum(st,3);
  11488. struct guild_castle *gc = guild_mapname2gc(mapname);
  11489. if (gc == NULL) {
  11490. script_pushint(st,0);
  11491. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11492. return SCRIPT_CMD_FAILURE;
  11493. }
  11494. switch (index) {
  11495. case 1:
  11496. script_pushint(st,gc->guild_id); break;
  11497. case 2:
  11498. script_pushint(st,gc->economy); break;
  11499. case 3:
  11500. script_pushint(st,gc->defense); break;
  11501. case 4:
  11502. script_pushint(st,gc->triggerE); break;
  11503. case 5:
  11504. script_pushint(st,gc->triggerD); break;
  11505. case 6:
  11506. script_pushint(st,gc->nextTime); break;
  11507. case 7:
  11508. script_pushint(st,gc->payTime); break;
  11509. case 8:
  11510. script_pushint(st,gc->createTime); break;
  11511. case 9:
  11512. script_pushint(st,gc->visibleC); break;
  11513. default:
  11514. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  11515. script_pushint(st,gc->guardian[index-10].visible);
  11516. break;
  11517. }
  11518. script_pushint(st,0);
  11519. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11520. return SCRIPT_CMD_FAILURE;
  11521. }
  11522. return SCRIPT_CMD_SUCCESS;
  11523. }
  11524. BUILDIN_FUNC(setcastledata)
  11525. {
  11526. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11527. int index = script_getnum(st,3);
  11528. int value = script_getnum(st,4);
  11529. struct guild_castle *gc = guild_mapname2gc(mapname);
  11530. if (gc == NULL) {
  11531. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11532. return SCRIPT_CMD_FAILURE;
  11533. }
  11534. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  11535. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11536. return SCRIPT_CMD_FAILURE;
  11537. }
  11538. guild_castledatasave(gc->castle_id, index, value);
  11539. return SCRIPT_CMD_SUCCESS;
  11540. }
  11541. /* =====================================================================
  11542. * ---------------------------------------------------------------------*/
  11543. BUILDIN_FUNC(requestguildinfo)
  11544. {
  11545. int guild_id=script_getnum(st,2);
  11546. const char *event=NULL;
  11547. if( script_hasdata(st,3) ){
  11548. event=script_getstr(st,3);
  11549. check_event(st, event);
  11550. }
  11551. if(guild_id>0)
  11552. guild_npc_request_info(guild_id,event);
  11553. return SCRIPT_CMD_SUCCESS;
  11554. }
  11555. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11556. /// getequipcardcnt(<equipment slot>);
  11557. BUILDIN_FUNC(getequipcardcnt)
  11558. {
  11559. int i=-1,j,num;
  11560. TBL_PC *sd;
  11561. int count;
  11562. if( !script_rid2sd(sd) )
  11563. return SCRIPT_CMD_SUCCESS;
  11564. num=script_getnum(st,2);
  11565. if (equip_index_check(num))
  11566. i=pc_checkequip(sd,equip_bitmask[num]);
  11567. if (i < 0 || !sd->inventory_data[i]) {
  11568. script_pushint(st,0);
  11569. return SCRIPT_CMD_SUCCESS;
  11570. }
  11571. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11572. {
  11573. script_pushint(st,0);
  11574. return SCRIPT_CMD_SUCCESS;
  11575. }
  11576. count = 0;
  11577. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11578. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11579. count++;
  11580. script_pushint(st,count);
  11581. return SCRIPT_CMD_SUCCESS;
  11582. }
  11583. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11584. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11585. /// successremovecards <slot>;
  11586. BUILDIN_FUNC(successremovecards) {
  11587. int i=-1,c,cardflag=0;
  11588. TBL_PC* sd;
  11589. int num;
  11590. if( !script_rid2sd(sd) )
  11591. return SCRIPT_CMD_SUCCESS;
  11592. num = script_getnum(st,2);
  11593. if (equip_index_check(num))
  11594. i=pc_checkequip(sd,equip_bitmask[num]);
  11595. if (i < 0 || !sd->inventory_data[i]) {
  11596. return SCRIPT_CMD_SUCCESS;
  11597. }
  11598. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11599. return SCRIPT_CMD_SUCCESS;
  11600. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11601. 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
  11602. unsigned char flag = 0;
  11603. struct item item_tmp;
  11604. memset(&item_tmp,0,sizeof(item_tmp));
  11605. cardflag = 1;
  11606. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11607. item_tmp.identify = 1;
  11608. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11609. clif_additem(sd,0,0,flag);
  11610. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11611. }
  11612. }
  11613. }
  11614. if(cardflag == 1) {//if card was remove remplace item with no card
  11615. unsigned char flag = 0, j;
  11616. struct item item_tmp;
  11617. memset(&item_tmp,0,sizeof(item_tmp));
  11618. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11619. item_tmp.identify = 1;
  11620. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11621. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11622. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11623. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11624. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11625. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11626. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11627. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11628. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11629. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11630. }
  11631. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11632. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11633. clif_additem(sd,0,0,flag);
  11634. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11635. }
  11636. clif_misceffect(&sd->bl,3);
  11637. }
  11638. return SCRIPT_CMD_SUCCESS;
  11639. }
  11640. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11641. /// failedremovecards <slot>, <type>;
  11642. /// <type>=0 : will destroy both the item and the cards.
  11643. /// <type>=1 : will keep the item, but destroy the cards.
  11644. /// <type>=2 : will keep the cards, but destroy the item.
  11645. /// <type>=? : will just display the failure effect.
  11646. BUILDIN_FUNC(failedremovecards) {
  11647. int i=-1,c,cardflag=0;
  11648. TBL_PC* sd;
  11649. int num;
  11650. int typefail;
  11651. if( !script_rid2sd(sd) )
  11652. return SCRIPT_CMD_SUCCESS;
  11653. num = script_getnum(st,2);
  11654. typefail = script_getnum(st,3);
  11655. if (equip_index_check(num))
  11656. i=pc_checkequip(sd,equip_bitmask[num]);
  11657. if (i < 0 || !sd->inventory_data[i])
  11658. return SCRIPT_CMD_SUCCESS;
  11659. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11660. return SCRIPT_CMD_SUCCESS;
  11661. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11662. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11663. cardflag = 1;
  11664. if(typefail == 2) {// add cards to inventory, clear
  11665. unsigned char flag = 0;
  11666. struct item item_tmp;
  11667. memset(&item_tmp,0,sizeof(item_tmp));
  11668. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11669. item_tmp.identify = 1;
  11670. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11671. clif_additem(sd,0,0,flag);
  11672. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11673. }
  11674. }
  11675. }
  11676. }
  11677. if(cardflag == 1) {
  11678. if(typefail == 0 || typefail == 2){ // destroy the item
  11679. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11680. }else if(typefail == 1){ // destroy the card
  11681. unsigned char flag = 0, j;
  11682. struct item item_tmp;
  11683. memset(&item_tmp,0,sizeof(item_tmp));
  11684. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11685. item_tmp.identify = 1;
  11686. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11687. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11688. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11689. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11690. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11691. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11692. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11693. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11694. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11695. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11696. }
  11697. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11698. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11699. clif_additem(sd,0,0,flag);
  11700. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11701. }
  11702. }
  11703. clif_misceffect(&sd->bl,2);
  11704. }
  11705. return SCRIPT_CMD_SUCCESS;
  11706. }
  11707. /* ================================================================
  11708. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11709. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11710. * improved by [Lance]
  11711. * ================================================================*/
  11712. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11713. {
  11714. int x,y,m,check_val=0,check_ID=0,i=0;
  11715. struct guild *g = NULL;
  11716. struct party_data *p = NULL;
  11717. const char *str;
  11718. const char *mapname;
  11719. unsigned int index;
  11720. mapname=script_getstr(st,2);
  11721. str=script_getstr(st,3);
  11722. x=script_getnum(st,4);
  11723. y=script_getnum(st,5);
  11724. if(script_hasdata(st,7)){
  11725. check_val=script_getnum(st,6);
  11726. check_ID=script_getnum(st,7);
  11727. }
  11728. if((m=map_mapname2mapid(mapname))< 0)
  11729. return SCRIPT_CMD_SUCCESS;
  11730. if(!(index=mapindex_name2id(str)))
  11731. return SCRIPT_CMD_SUCCESS;
  11732. switch(check_val){
  11733. case 1:
  11734. g = guild_search(check_ID);
  11735. if (g){
  11736. for( i=0; i < g->max_member; i++)
  11737. {
  11738. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11739. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11740. }
  11741. }
  11742. }
  11743. break;
  11744. case 2:
  11745. p = party_search(check_ID);
  11746. if(p){
  11747. for(i=0;i<MAX_PARTY; i++){
  11748. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11749. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11750. }
  11751. }
  11752. }
  11753. break;
  11754. default:
  11755. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11756. break;
  11757. }
  11758. return SCRIPT_CMD_SUCCESS;
  11759. }
  11760. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11761. {
  11762. char *event=va_arg(ap,char *);
  11763. struct mob_data *md = ((struct mob_data *)bl);
  11764. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11765. return 1;
  11766. return SCRIPT_CMD_SUCCESS;
  11767. }
  11768. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11769. {
  11770. const char *mapname,*event;
  11771. int16 m;
  11772. mapname=script_getstr(st,2);
  11773. event=script_getstr(st,3);
  11774. if( strcmp(event, "all") == 0 )
  11775. event = NULL;
  11776. else
  11777. check_event(st, event);
  11778. if( strcmp(mapname, "this") == 0 ) {
  11779. struct map_session_data *sd;
  11780. if( script_rid2sd(sd) )
  11781. m = sd->bl.m;
  11782. else {
  11783. script_pushint(st,-1);
  11784. return SCRIPT_CMD_SUCCESS;
  11785. }
  11786. }
  11787. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11788. script_pushint(st,-1);
  11789. return SCRIPT_CMD_SUCCESS;
  11790. }
  11791. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11792. return SCRIPT_CMD_SUCCESS;
  11793. }
  11794. BUILDIN_FUNC(marriage)
  11795. {
  11796. TBL_PC *sd;
  11797. TBL_PC *p_sd;
  11798. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11799. script_pushint(st,0);
  11800. return SCRIPT_CMD_SUCCESS;
  11801. }
  11802. script_pushint(st,1);
  11803. return SCRIPT_CMD_SUCCESS;
  11804. }
  11805. BUILDIN_FUNC(wedding_effect)
  11806. {
  11807. TBL_PC *sd;
  11808. struct block_list *bl;
  11809. if(!script_rid2sd(sd)) {
  11810. bl=map_id2bl(st->oid);
  11811. } else
  11812. bl=&sd->bl;
  11813. clif_wedding_effect(bl);
  11814. return SCRIPT_CMD_SUCCESS;
  11815. }
  11816. /**
  11817. * divorce({<char_id>})
  11818. **/
  11819. BUILDIN_FUNC(divorce)
  11820. {
  11821. TBL_PC *sd;
  11822. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11823. script_pushint(st,0);
  11824. return SCRIPT_CMD_FAILURE;
  11825. }
  11826. script_pushint(st,1);
  11827. return SCRIPT_CMD_SUCCESS;
  11828. }
  11829. /**
  11830. * ispartneron({<char_id>})
  11831. **/
  11832. BUILDIN_FUNC(ispartneron)
  11833. {
  11834. TBL_PC *sd;
  11835. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11836. map_charid2sd(sd->status.partner_id) == NULL)
  11837. {
  11838. script_pushint(st,0);
  11839. return SCRIPT_CMD_FAILURE;
  11840. }
  11841. script_pushint(st,1);
  11842. return SCRIPT_CMD_SUCCESS;
  11843. }
  11844. /**
  11845. * getpartnerid({<char_id>})
  11846. **/
  11847. BUILDIN_FUNC(getpartnerid)
  11848. {
  11849. TBL_PC *sd;
  11850. if (!script_charid2sd(2,sd)) {
  11851. script_pushint(st,0);
  11852. return SCRIPT_CMD_FAILURE;
  11853. }
  11854. script_pushint(st,sd->status.partner_id);
  11855. return SCRIPT_CMD_SUCCESS;
  11856. }
  11857. /**
  11858. * getchildid({<char_id>})
  11859. **/
  11860. BUILDIN_FUNC(getchildid)
  11861. {
  11862. TBL_PC *sd;
  11863. if (!script_charid2sd(2,sd)) {
  11864. script_pushint(st,0);
  11865. return SCRIPT_CMD_FAILURE;
  11866. }
  11867. script_pushint(st,sd->status.child);
  11868. return SCRIPT_CMD_SUCCESS;
  11869. }
  11870. /**
  11871. * getmotherid({<char_id>})
  11872. **/
  11873. BUILDIN_FUNC(getmotherid)
  11874. {
  11875. TBL_PC *sd;
  11876. if (!script_charid2sd(2,sd)) {
  11877. script_pushint(st,0);
  11878. return SCRIPT_CMD_FAILURE;
  11879. }
  11880. script_pushint(st,sd->status.mother);
  11881. return SCRIPT_CMD_SUCCESS;
  11882. }
  11883. /**
  11884. * getfatherid({<char_id>})
  11885. **/
  11886. BUILDIN_FUNC(getfatherid)
  11887. {
  11888. TBL_PC *sd;
  11889. if (!script_charid2sd(2,sd)) {
  11890. script_pushint(st,0);
  11891. return SCRIPT_CMD_FAILURE;
  11892. }
  11893. script_pushint(st,sd->status.father);
  11894. return SCRIPT_CMD_SUCCESS;
  11895. }
  11896. BUILDIN_FUNC(warppartner)
  11897. {
  11898. int x,y;
  11899. unsigned short mapindex;
  11900. const char *str;
  11901. TBL_PC *sd;
  11902. TBL_PC *p_sd;
  11903. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  11904. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11905. script_pushint(st,0);
  11906. return SCRIPT_CMD_SUCCESS;
  11907. }
  11908. str=script_getstr(st,2);
  11909. x=script_getnum(st,3);
  11910. y=script_getnum(st,4);
  11911. mapindex = mapindex_name2id(str);
  11912. if (mapindex) {
  11913. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11914. script_pushint(st,1);
  11915. } else
  11916. script_pushint(st,0);
  11917. return SCRIPT_CMD_SUCCESS;
  11918. }
  11919. /*================================================
  11920. * Script for Displaying MOB Information [Valaris]
  11921. *------------------------------------------------*/
  11922. BUILDIN_FUNC(strmobinfo)
  11923. {
  11924. int num=script_getnum(st,2);
  11925. int class_=script_getnum(st,3);
  11926. if(!mobdb_checkid(class_))
  11927. {
  11928. if (num < 3) //requested a string
  11929. script_pushconststr(st,"");
  11930. else
  11931. script_pushint(st,0);
  11932. return SCRIPT_CMD_SUCCESS;
  11933. }
  11934. switch (num) {
  11935. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11936. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11937. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11938. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11939. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11940. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11941. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11942. default:
  11943. script_pushint(st,0);
  11944. break;
  11945. }
  11946. return SCRIPT_CMD_SUCCESS;
  11947. }
  11948. /*==========================================
  11949. * Summon guardians [Valaris]
  11950. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11951. *------------------------------------------*/
  11952. BUILDIN_FUNC(guardian)
  11953. {
  11954. int class_=0,x=0,y=0,guardian=0;
  11955. const char *str,*mapname,*evt="";
  11956. bool has_index = false;
  11957. mapname =script_getstr(st,2);
  11958. x =script_getnum(st,3);
  11959. y =script_getnum(st,4);
  11960. str =script_getstr(st,5);
  11961. class_=script_getnum(st,6);
  11962. if( script_hasdata(st,8) )
  11963. {// "<event label>",<guardian index>
  11964. evt=script_getstr(st,7);
  11965. guardian=script_getnum(st,8);
  11966. has_index = true;
  11967. } else if( script_hasdata(st,7) ){
  11968. struct script_data *data = script_getdata(st,7);
  11969. get_val(st,data);
  11970. if( data_isstring(data) )
  11971. {// "<event label>"
  11972. evt=script_getstr(st,7);
  11973. } else if( data_isint(data) )
  11974. {// <guardian index>
  11975. guardian=script_getnum(st,7);
  11976. has_index = true;
  11977. } else {
  11978. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11979. script_reportdata(data);
  11980. return SCRIPT_CMD_FAILURE;
  11981. }
  11982. }
  11983. check_event(st, evt);
  11984. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11985. return SCRIPT_CMD_SUCCESS;
  11986. }
  11987. /*==========================================
  11988. * Invisible Walls [Zephyrus]
  11989. *------------------------------------------*/
  11990. BUILDIN_FUNC(setwall)
  11991. {
  11992. const char *mapname, *name;
  11993. int x, y, m, size, dir;
  11994. bool shootable;
  11995. mapname = script_getstr(st,2);
  11996. x = script_getnum(st,3);
  11997. y = script_getnum(st,4);
  11998. size = script_getnum(st,5);
  11999. dir = script_getnum(st,6);
  12000. shootable = script_getnum(st,7) != 0;
  12001. name = script_getstr(st,8);
  12002. if( (m = map_mapname2mapid(mapname)) < 0 )
  12003. return SCRIPT_CMD_SUCCESS; // Invalid Map
  12004. map_iwall_set(m, x, y, size, dir, shootable, name);
  12005. return SCRIPT_CMD_SUCCESS;
  12006. }
  12007. BUILDIN_FUNC(delwall)
  12008. {
  12009. const char *name = script_getstr(st,2);
  12010. if( !map_iwall_remove(name) ){
  12011. ShowError( "buildin_delwall: wall \"%s\" does not exist.\n", name );
  12012. return SCRIPT_CMD_FAILURE;
  12013. }
  12014. return SCRIPT_CMD_SUCCESS;
  12015. }
  12016. /// Retrieves various information about the specified guardian.
  12017. ///
  12018. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  12019. /// type: 0 - whether it is deployed or not
  12020. /// 1 - maximum hp
  12021. /// 2 - current hp
  12022. ///
  12023. BUILDIN_FUNC(guardianinfo)
  12024. {
  12025. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  12026. int id = script_getnum(st,3);
  12027. int type = script_getnum(st,4);
  12028. struct guild_castle* gc = guild_mapname2gc(mapname);
  12029. struct mob_data* gd;
  12030. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  12031. {
  12032. script_pushint(st,-1);
  12033. return SCRIPT_CMD_SUCCESS;
  12034. }
  12035. if( type == 0 )
  12036. script_pushint(st, gc->guardian[id].visible);
  12037. else
  12038. if( !gc->guardian[id].visible )
  12039. script_pushint(st,-1);
  12040. else
  12041. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  12042. script_pushint(st,-1);
  12043. else
  12044. {
  12045. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  12046. else if( type == 2 ) script_pushint(st,gd->status.hp);
  12047. else
  12048. script_pushint(st,-1);
  12049. }
  12050. return SCRIPT_CMD_SUCCESS;
  12051. }
  12052. /*==========================================
  12053. * Get the item name by item_id or null
  12054. *------------------------------------------*/
  12055. BUILDIN_FUNC(getitemname)
  12056. {
  12057. unsigned short item_id = 0;
  12058. struct item_data *i_data;
  12059. char *item_name;
  12060. struct script_data *data;
  12061. data=script_getdata(st,2);
  12062. get_val(st,data);
  12063. if( data_isstring(data) ){
  12064. const char *name=conv_str(st,data);
  12065. struct item_data *item_data = itemdb_searchname(name);
  12066. if( item_data )
  12067. item_id=item_data->nameid;
  12068. }else
  12069. item_id=conv_num(st,data);
  12070. i_data = itemdb_exists(item_id);
  12071. if (i_data == NULL)
  12072. {
  12073. script_pushconststr(st,"null");
  12074. return SCRIPT_CMD_SUCCESS;
  12075. }
  12076. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  12077. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  12078. script_pushstr(st,item_name);
  12079. return SCRIPT_CMD_SUCCESS;
  12080. }
  12081. /*==========================================
  12082. * Returns number of slots an item has. [Skotlex]
  12083. *------------------------------------------*/
  12084. BUILDIN_FUNC(getitemslots)
  12085. {
  12086. unsigned short item_id;
  12087. struct item_data *i_data;
  12088. item_id=script_getnum(st,2);
  12089. i_data = itemdb_exists(item_id);
  12090. if (i_data)
  12091. script_pushint(st,i_data->slot);
  12092. else
  12093. script_pushint(st,-1);
  12094. return SCRIPT_CMD_SUCCESS;
  12095. }
  12096. // TODO: add matk here if needed/once we get rid of RENEWAL
  12097. /*==========================================
  12098. * Returns some values of an item [Lupus]
  12099. * Price, Weight, etc...
  12100. getiteminfo(itemID,n), where n
  12101. 0 value_buy;
  12102. 1 value_sell;
  12103. 2 type;
  12104. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  12105. if = 0, then monsters don't drop it at all (rare or a quest item)
  12106. if = -1, then this item is sold in NPC shops only
  12107. 4 sex;
  12108. 5 equip;
  12109. 6 weight;
  12110. 7 atk;
  12111. 8 def;
  12112. 9 range;
  12113. 10 slot;
  12114. 11 look;
  12115. 12 elv;
  12116. 13 wlv;
  12117. 14 view id
  12118. 15 eLvmax
  12119. 16 matk (renewal)
  12120. *------------------------------------------*/
  12121. BUILDIN_FUNC(getiteminfo)
  12122. {
  12123. unsigned short item_id,n;
  12124. struct item_data *i_data;
  12125. item_id = script_getnum(st,2);
  12126. n = script_getnum(st,3);
  12127. i_data = itemdb_exists(item_id);
  12128. if (i_data && n <= 16) {
  12129. int *item_arr = (int*)&i_data->value_buy;
  12130. #ifndef RENEWAL
  12131. if (n == 16)
  12132. script_pushint(st,0);
  12133. else
  12134. #endif
  12135. script_pushint(st,item_arr[n]);
  12136. } else
  12137. script_pushint(st,-1);
  12138. return SCRIPT_CMD_SUCCESS;
  12139. }
  12140. /*==========================================
  12141. * Set some values of an item [Lupus]
  12142. * Price, Weight, etc...
  12143. setiteminfo(itemID,n,Value), where n
  12144. 0 value_buy;
  12145. 1 value_sell;
  12146. 2 type;
  12147. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  12148. if = 0, then monsters don't drop it at all (rare or a quest item)
  12149. if = -1, then this item is sold in NPC shops only
  12150. 4 sex;
  12151. 5 equip;
  12152. 6 weight;
  12153. 7 atk;
  12154. 8 def;
  12155. 9 range;
  12156. 10 slot;
  12157. 11 look;
  12158. 12 elv;
  12159. 13 wlv;
  12160. 14 view id
  12161. * Returns Value or -1 if the wrong field's been set
  12162. *------------------------------------------*/
  12163. BUILDIN_FUNC(setiteminfo)
  12164. {
  12165. unsigned short item_id;
  12166. int n,value;
  12167. struct item_data *i_data;
  12168. item_id = script_getnum(st,2);
  12169. n = script_getnum(st,3);
  12170. value = script_getnum(st,4);
  12171. i_data = itemdb_exists(item_id);
  12172. if (i_data && n>=0 && n<=14) {
  12173. int *item_arr = (int*)&i_data->value_buy;
  12174. item_arr[n] = value;
  12175. script_pushint(st,value);
  12176. } else
  12177. script_pushint(st,-1);
  12178. return SCRIPT_CMD_SUCCESS;
  12179. }
  12180. /*==========================================
  12181. * Returns value from equipped item slot n [Lupus]
  12182. getequipcardid(num,slot)
  12183. where
  12184. num = eqip position slot
  12185. slot = 0,1,2,3 (Card Slot N)
  12186. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  12187. it's useful when you want to check item cards or if it's signed
  12188. Useful for such quests as "Sign this refined item with players name" etc
  12189. Hat[0] +4 -> Player's Hat[0] +4
  12190. *------------------------------------------*/
  12191. BUILDIN_FUNC(getequipcardid)
  12192. {
  12193. int i=-1,num,slot;
  12194. TBL_PC *sd;
  12195. if( !script_rid2sd(sd) )
  12196. return SCRIPT_CMD_SUCCESS;
  12197. num=script_getnum(st,2);
  12198. slot=script_getnum(st,3);
  12199. if (equip_index_check(num))
  12200. i=pc_checkequip(sd,equip_bitmask[num]);
  12201. if(i >= 0 && slot>=0 && slot<4)
  12202. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  12203. else
  12204. script_pushint(st,0);
  12205. return SCRIPT_CMD_SUCCESS;
  12206. }
  12207. /*==========================================
  12208. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  12209. *------------------------------------------*/
  12210. BUILDIN_FUNC(petskillbonus)
  12211. {
  12212. struct pet_data *pd;
  12213. TBL_PC *sd;
  12214. if(!script_rid2sd(sd) || sd->pd == NULL)
  12215. return SCRIPT_CMD_FAILURE;
  12216. pd = sd->pd;
  12217. if (pd->bonus)
  12218. { //Clear previous bonus
  12219. if (pd->bonus->timer != INVALID_TIMER)
  12220. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  12221. } else //init
  12222. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  12223. pd->bonus->type = script_getnum(st,2);
  12224. pd->bonus->val = script_getnum(st,3);
  12225. pd->bonus->duration = script_getnum(st,4);
  12226. pd->bonus->delay = script_getnum(st,5);
  12227. if (pd->state.skillbonus == 1)
  12228. pd->state.skillbonus = 0; // waiting state
  12229. // wait for timer to start
  12230. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12231. pd->bonus->timer = INVALID_TIMER;
  12232. else
  12233. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  12234. return SCRIPT_CMD_SUCCESS;
  12235. }
  12236. /*==========================================
  12237. * pet looting [Valaris] //Rewritten by [Skotlex]
  12238. *------------------------------------------*/
  12239. BUILDIN_FUNC(petloot)
  12240. {
  12241. int max;
  12242. struct pet_data *pd;
  12243. TBL_PC *sd;
  12244. if(!script_rid2sd(sd) || sd->pd==NULL)
  12245. return SCRIPT_CMD_SUCCESS;
  12246. max=script_getnum(st,2);
  12247. if(max < 1)
  12248. max = 1; //Let'em loot at least 1 item.
  12249. else if (max > MAX_PETLOOT_SIZE)
  12250. max = MAX_PETLOOT_SIZE;
  12251. pd = sd->pd;
  12252. if (pd->loot != NULL)
  12253. { //Release whatever was there already and reallocate memory
  12254. pet_lootitem_drop(pd, pd->master);
  12255. aFree(pd->loot->item);
  12256. }
  12257. else
  12258. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12259. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12260. pd->loot->max=max;
  12261. pd->loot->count = 0;
  12262. pd->loot->weight = 0;
  12263. return SCRIPT_CMD_SUCCESS;
  12264. }
  12265. /*==========================================
  12266. * Set arrays with info of all sd inventory :
  12267. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12268. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12269. * @inventorylist_card(0..3), @inventorylist_expire
  12270. * @inventorylist_count = scalar
  12271. *------------------------------------------*/
  12272. BUILDIN_FUNC(getinventorylist)
  12273. {
  12274. TBL_PC *sd;
  12275. char card_var[NAME_LENGTH], randopt_var[50];
  12276. int i,j=0,k;
  12277. if (!script_charid2sd(2,sd))
  12278. return SCRIPT_CMD_FAILURE;
  12279. for(i=0;i<MAX_INVENTORY;i++){
  12280. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12281. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12282. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12283. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12284. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12285. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12286. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12287. for (k = 0; k < MAX_SLOTS; k++)
  12288. {
  12289. sprintf(card_var, "@inventorylist_card%d",k+1);
  12290. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12291. }
  12292. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12293. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12294. for (k = 0; k < MAX_ITEM_RDM_OPT; k++)
  12295. {
  12296. sprintf(randopt_var, "@inventorylist_option_id%d",k+1);
  12297. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].id);
  12298. sprintf(randopt_var, "@inventorylist_option_value%d",k+1);
  12299. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].value);
  12300. sprintf(randopt_var, "@inventorylist_option_parameter%d",k+1);
  12301. pc_setreg(sd,reference_uid(add_str(randopt_var), j),sd->inventory.u.items_inventory[i].option[k].param);
  12302. }
  12303. j++;
  12304. }
  12305. }
  12306. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12307. return SCRIPT_CMD_SUCCESS;
  12308. }
  12309. /**
  12310. * getskilllist ({<char_id>});
  12311. **/
  12312. BUILDIN_FUNC(getskilllist)
  12313. {
  12314. TBL_PC *sd;
  12315. int i,j=0;
  12316. if (!script_charid2sd(2,sd))
  12317. return SCRIPT_CMD_FAILURE;
  12318. for(i=0;i<MAX_SKILL;i++){
  12319. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12320. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12321. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12322. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12323. j++;
  12324. }
  12325. }
  12326. pc_setreg(sd,add_str("@skilllist_count"),j);
  12327. return SCRIPT_CMD_SUCCESS;
  12328. }
  12329. /**
  12330. * clearitem ({<char_id>});
  12331. **/
  12332. BUILDIN_FUNC(clearitem)
  12333. {
  12334. TBL_PC *sd;
  12335. int i;
  12336. if (!script_charid2sd(2,sd))
  12337. return SCRIPT_CMD_FAILURE;
  12338. for (i=0; i<MAX_INVENTORY; i++) {
  12339. if (sd->inventory.u.items_inventory[i].amount) {
  12340. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12341. }
  12342. }
  12343. return SCRIPT_CMD_SUCCESS;
  12344. }
  12345. /**
  12346. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12347. * disguise <Monster ID>{,<char_id>};
  12348. **/
  12349. BUILDIN_FUNC(disguise)
  12350. {
  12351. int id;
  12352. TBL_PC* sd;
  12353. if (!script_charid2sd(3,sd))
  12354. return SCRIPT_CMD_FAILURE;
  12355. id = script_getnum(st,2);
  12356. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12357. pc_disguise(sd, id);
  12358. script_pushint(st,id);
  12359. } else
  12360. script_pushint(st,0);
  12361. return SCRIPT_CMD_SUCCESS;
  12362. }
  12363. /**
  12364. * Undisguise Player (returns 1 if success, 0 on fail)
  12365. * undisguise {<char_id>};
  12366. **/
  12367. BUILDIN_FUNC(undisguise)
  12368. {
  12369. TBL_PC* sd;
  12370. if (!script_charid2sd(2,sd))
  12371. return SCRIPT_CMD_FAILURE;
  12372. if (sd->disguise) {
  12373. pc_disguise(sd, 0);
  12374. script_pushint(st,0);
  12375. } else {
  12376. script_pushint(st,1);
  12377. }
  12378. return SCRIPT_CMD_SUCCESS;
  12379. }
  12380. /**
  12381. * Transform an NPC to another _class
  12382. *
  12383. * classchange(<view id>{,"<NPC name>","<flag>"});
  12384. * @param flag: Specify target
  12385. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12386. * BC_SELF - Sprite is sent only to player attached.
  12387. */
  12388. BUILDIN_FUNC(classchange)
  12389. {
  12390. int _class, type = 1;
  12391. struct npc_data* nd = NULL;
  12392. TBL_PC *sd = map_id2sd(st->rid);
  12393. send_target target = AREA;
  12394. _class = script_getnum(st,2);
  12395. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12396. nd = npc_name2id(script_getstr(st, 3));
  12397. else
  12398. nd = (struct npc_data *)map_id2bl(st->oid);
  12399. if (nd == NULL)
  12400. return SCRIPT_CMD_FAILURE;
  12401. if (script_hasdata(st, 4)) {
  12402. switch(script_getnum(st, 4)) {
  12403. case BC_SELF: target = SELF; break;
  12404. case BC_AREA:
  12405. default: target = AREA; break;
  12406. }
  12407. }
  12408. if (target != SELF)
  12409. clif_class_change(&nd->bl,_class,type);
  12410. else if (sd == NULL)
  12411. return SCRIPT_CMD_FAILURE;
  12412. else
  12413. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12414. return SCRIPT_CMD_SUCCESS;
  12415. }
  12416. /*==========================================
  12417. * Display an effect
  12418. *------------------------------------------*/
  12419. BUILDIN_FUNC(misceffect)
  12420. {
  12421. int type;
  12422. type=script_getnum(st,2);
  12423. if( type <= EF_NONE || type >= EF_MAX ){
  12424. ShowError( "buildin_misceffect: unsupported effect id %d\n", type );
  12425. return SCRIPT_CMD_FAILURE;
  12426. }
  12427. if(st->oid && st->oid != fake_nd->bl.id) {
  12428. struct block_list *bl = map_id2bl(st->oid);
  12429. if (bl)
  12430. clif_specialeffect(bl,type,AREA);
  12431. } else{
  12432. TBL_PC *sd;
  12433. if(script_rid2sd(sd))
  12434. clif_specialeffect(&sd->bl,type,AREA);
  12435. }
  12436. return SCRIPT_CMD_SUCCESS;
  12437. }
  12438. /*==========================================
  12439. * Play a BGM on a single client [Rikter/Yommy]
  12440. *------------------------------------------*/
  12441. BUILDIN_FUNC(playBGM)
  12442. {
  12443. struct map_session_data* sd;
  12444. if( script_rid2sd(sd) ) {
  12445. clif_playBGM(sd, script_getstr(st,2));
  12446. }
  12447. return SCRIPT_CMD_SUCCESS;
  12448. }
  12449. static int playBGM_sub(struct block_list* bl,va_list ap)
  12450. {
  12451. const char* name = va_arg(ap,const char*);
  12452. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12453. return 0;
  12454. }
  12455. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12456. {
  12457. const char* name = va_arg(args, const char*);
  12458. clif_playBGM(sd, name);
  12459. return 0;
  12460. }
  12461. /*==========================================
  12462. * Play a BGM on multiple client [Rikter/Yommy]
  12463. *------------------------------------------*/
  12464. BUILDIN_FUNC(playBGMall)
  12465. {
  12466. const char* name;
  12467. name = script_getstr(st,2);
  12468. if( script_hasdata(st,7) ) {// specified part of map
  12469. const char* mapname = script_getstr(st,3);
  12470. int x0 = script_getnum(st,4);
  12471. int y0 = script_getnum(st,5);
  12472. int x1 = script_getnum(st,6);
  12473. int y1 = script_getnum(st,7);
  12474. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12475. }
  12476. else if( script_hasdata(st,3) ) {// entire map
  12477. const char* mapname = script_getstr(st,3);
  12478. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12479. }
  12480. else {// entire server
  12481. map_foreachpc(&playBGM_foreachpc_sub, name);
  12482. }
  12483. return SCRIPT_CMD_SUCCESS;
  12484. }
  12485. /*==========================================
  12486. * Play a .wav sound for sd
  12487. *------------------------------------------*/
  12488. BUILDIN_FUNC(soundeffect)
  12489. {
  12490. TBL_PC* sd;
  12491. if(script_rid2sd(sd)){
  12492. const char* name = script_getstr(st,2);
  12493. int type = script_getnum(st,3);
  12494. clif_soundeffect(sd,&sd->bl,name,type);
  12495. }
  12496. return SCRIPT_CMD_SUCCESS;
  12497. }
  12498. int soundeffect_sub(struct block_list* bl,va_list ap)
  12499. {
  12500. char* name = va_arg(ap,char*);
  12501. int type = va_arg(ap,int);
  12502. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12503. return 0;
  12504. }
  12505. /*==========================================
  12506. * Play a sound effect (.wav) on multiple clients
  12507. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12508. *------------------------------------------*/
  12509. BUILDIN_FUNC(soundeffectall)
  12510. {
  12511. struct block_list* bl;
  12512. struct map_session_data* sd;
  12513. const char* name;
  12514. int type;
  12515. if( st->rid && script_rid2sd(sd) )
  12516. bl = &sd->bl;
  12517. else
  12518. bl = map_id2bl(st->oid);
  12519. if (!bl)
  12520. return SCRIPT_CMD_SUCCESS;
  12521. name = script_getstr(st,2);
  12522. type = script_getnum(st,3);
  12523. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12524. if(!script_hasdata(st,4))
  12525. { // area around
  12526. clif_soundeffectall(bl, name, type, AREA);
  12527. }
  12528. else
  12529. if(!script_hasdata(st,5))
  12530. { // entire map
  12531. const char* mapname = script_getstr(st,4);
  12532. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12533. }
  12534. else
  12535. if(script_hasdata(st,8))
  12536. { // specified part of map
  12537. const char* mapname = script_getstr(st,4);
  12538. int x0 = script_getnum(st,5);
  12539. int y0 = script_getnum(st,6);
  12540. int x1 = script_getnum(st,7);
  12541. int y1 = script_getnum(st,8);
  12542. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12543. }
  12544. else
  12545. {
  12546. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12547. }
  12548. return SCRIPT_CMD_SUCCESS;
  12549. }
  12550. /*==========================================
  12551. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12552. *------------------------------------------*/
  12553. BUILDIN_FUNC(petrecovery)
  12554. {
  12555. struct pet_data *pd;
  12556. TBL_PC *sd;
  12557. int sc;
  12558. if(!script_rid2sd(sd) || sd->pd == NULL)
  12559. return SCRIPT_CMD_FAILURE;
  12560. sc = script_getnum(st,2);
  12561. if (sc <= SC_NONE || sc >= SC_MAX) {
  12562. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12563. return SCRIPT_CMD_FAILURE;
  12564. }
  12565. pd = sd->pd;
  12566. if (pd->recovery)
  12567. { //Halt previous bonus
  12568. if (pd->recovery->timer != INVALID_TIMER)
  12569. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12570. } else //Init
  12571. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12572. pd->recovery->type = (sc_type)sc;
  12573. pd->recovery->delay = script_getnum(st,3);
  12574. pd->recovery->timer = INVALID_TIMER;
  12575. return SCRIPT_CMD_SUCCESS;
  12576. }
  12577. /*==========================================
  12578. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12579. *------------------------------------------*/
  12580. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12581. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12582. BUILDIN_FUNC(petskillattack)
  12583. {
  12584. struct pet_data *pd;
  12585. struct script_data *data;
  12586. TBL_PC *sd;
  12587. int id = 0;
  12588. if(!script_rid2sd(sd) || sd->pd == NULL)
  12589. return SCRIPT_CMD_FAILURE;
  12590. data = script_getdata(st, 2);
  12591. get_val(st, data);
  12592. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12593. if (!id) {
  12594. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12595. return SCRIPT_CMD_FAILURE;
  12596. }
  12597. pd = sd->pd;
  12598. if (pd->a_skill == NULL)
  12599. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12600. pd->a_skill->id = id;
  12601. pd->a_skill->damage = 0;
  12602. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12603. pd->a_skill->div_ = 0;
  12604. pd->a_skill->rate = script_getnum(st,4);
  12605. pd->a_skill->bonusrate = script_getnum(st,5);
  12606. return SCRIPT_CMD_SUCCESS;
  12607. }
  12608. /*==========================================
  12609. * pet attack skills [Valaris]
  12610. *------------------------------------------*/
  12611. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12612. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12613. BUILDIN_FUNC(petskillattack2)
  12614. {
  12615. struct pet_data *pd;
  12616. struct script_data *data;
  12617. TBL_PC *sd;
  12618. int id = 0;
  12619. if(!script_rid2sd(sd) || sd->pd == NULL)
  12620. return SCRIPT_CMD_FAILURE;
  12621. data = script_getdata(st, 2);
  12622. get_val(st, data);
  12623. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12624. if (!id) {
  12625. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12626. return SCRIPT_CMD_FAILURE;
  12627. }
  12628. pd = sd->pd;
  12629. if (pd->a_skill == NULL)
  12630. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12631. pd->a_skill->id = id;
  12632. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12633. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12634. pd->a_skill->div_ = script_getnum(st,4);
  12635. pd->a_skill->rate = script_getnum(st,5);
  12636. pd->a_skill->bonusrate = script_getnum(st,6);
  12637. return SCRIPT_CMD_SUCCESS;
  12638. }
  12639. /*==========================================
  12640. * pet support skills [Skotlex]
  12641. *------------------------------------------*/
  12642. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12643. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12644. BUILDIN_FUNC(petskillsupport)
  12645. {
  12646. struct pet_data *pd;
  12647. struct script_data *data;
  12648. TBL_PC *sd;
  12649. int id = 0;
  12650. if(!script_rid2sd(sd) || sd->pd == NULL)
  12651. return SCRIPT_CMD_FAILURE;
  12652. data = script_getdata(st, 2);
  12653. get_val(st, data);
  12654. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12655. if (!id) {
  12656. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12657. return SCRIPT_CMD_FAILURE;
  12658. }
  12659. pd = sd->pd;
  12660. if (pd->s_skill)
  12661. { //Clear previous skill
  12662. if (pd->s_skill->timer != INVALID_TIMER)
  12663. {
  12664. if (pd->s_skill->id)
  12665. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12666. else
  12667. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12668. }
  12669. } else //init memory
  12670. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12671. pd->s_skill->id = id;
  12672. pd->s_skill->lv = script_getnum(st,3);
  12673. pd->s_skill->delay = script_getnum(st,4);
  12674. pd->s_skill->hp = script_getnum(st,5);
  12675. pd->s_skill->sp = script_getnum(st,6);
  12676. //Use delay as initial offset to avoid skill/heal exploits
  12677. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12678. pd->s_skill->timer = INVALID_TIMER;
  12679. else
  12680. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12681. return SCRIPT_CMD_SUCCESS;
  12682. }
  12683. /*==========================================
  12684. * Scripted skill effects [Celest]
  12685. *------------------------------------------*/
  12686. /// skilleffect <skill id>,<level>
  12687. /// skilleffect "<skill name>",<level>
  12688. BUILDIN_FUNC(skilleffect)
  12689. {
  12690. TBL_PC *sd;
  12691. uint16 skill_id, skill_lv;
  12692. struct script_data *data;
  12693. if( !script_rid2sd(sd) )
  12694. return SCRIPT_CMD_SUCCESS;
  12695. data = script_getdata(st, 2);
  12696. get_val(st, data); // Convert into value in case of a variable
  12697. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12698. skill_lv = script_getnum(st,3);
  12699. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12700. * which leaves the server thinking it still is sitting. */
  12701. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12702. skill_sit(sd, 0);
  12703. clif_standing(&sd->bl);
  12704. }
  12705. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12706. return SCRIPT_CMD_SUCCESS;
  12707. }
  12708. /*==========================================
  12709. * NPC skill effects [Valaris]
  12710. *------------------------------------------*/
  12711. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12712. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12713. BUILDIN_FUNC(npcskilleffect)
  12714. {
  12715. struct block_list *bl= map_id2bl(st->oid);
  12716. uint16 skill_id, skill_lv;
  12717. int x, y;
  12718. struct script_data *data = script_getdata(st, 2);
  12719. get_val(st, data); // Convert into value in case of a variable
  12720. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12721. skill_lv=script_getnum(st,3);
  12722. x=script_getnum(st,4);
  12723. y=script_getnum(st,5);
  12724. if (bl)
  12725. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12726. return SCRIPT_CMD_SUCCESS;
  12727. }
  12728. /*==========================================
  12729. * Special effects [Valaris]
  12730. *------------------------------------------*/
  12731. BUILDIN_FUNC(specialeffect)
  12732. {
  12733. struct block_list *bl=map_id2bl(st->oid);
  12734. int type = script_getnum(st,2);
  12735. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12736. if(bl==NULL)
  12737. return SCRIPT_CMD_SUCCESS;
  12738. if( type <= EF_NONE || type >= EF_MAX ){
  12739. ShowError( "buildin_specialeffect: unsupported effect id %d\n", type );
  12740. return SCRIPT_CMD_FAILURE;
  12741. }
  12742. if( script_hasdata(st,4) )
  12743. {
  12744. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12745. if(nd)
  12746. clif_specialeffect(&nd->bl, type, target);
  12747. }
  12748. else
  12749. {
  12750. if (target == SELF) {
  12751. TBL_PC *sd;
  12752. if (script_rid2sd(sd))
  12753. clif_specialeffect_single(bl,type,sd->fd);
  12754. } else {
  12755. clif_specialeffect(bl, type, target);
  12756. }
  12757. }
  12758. return SCRIPT_CMD_SUCCESS;
  12759. }
  12760. BUILDIN_FUNC(specialeffect2)
  12761. {
  12762. TBL_PC *sd;
  12763. if( script_nick2sd(4,sd) ){
  12764. int type = script_getnum(st,2);
  12765. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12766. if( type <= EF_NONE || type >= EF_MAX ){
  12767. ShowError( "buildin_specialeffect2: unsupported effect id %d\n", type );
  12768. return SCRIPT_CMD_FAILURE;
  12769. }
  12770. clif_specialeffect(&sd->bl, type, target);
  12771. }
  12772. return SCRIPT_CMD_SUCCESS;
  12773. }
  12774. /**
  12775. * nude({<char_id>});
  12776. * @author [Valaris]
  12777. **/
  12778. BUILDIN_FUNC(nude)
  12779. {
  12780. TBL_PC *sd;
  12781. int i, calcflag = 0;
  12782. if (!script_charid2sd(2,sd))
  12783. return SCRIPT_CMD_FAILURE;
  12784. for( i = 0 ; i < EQI_MAX; i++ ) {
  12785. if( sd->equip_index[ i ] >= 0 ) {
  12786. if( !calcflag )
  12787. calcflag = 1;
  12788. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12789. }
  12790. }
  12791. if( calcflag )
  12792. status_calc_pc(sd,SCO_NONE);
  12793. return SCRIPT_CMD_SUCCESS;
  12794. }
  12795. int atcommand_sub(struct script_state* st,int type) {
  12796. TBL_PC *sd, dummy_sd;
  12797. int fd;
  12798. const char *cmd;
  12799. cmd = script_getstr(st,2);
  12800. if (st->rid) {
  12801. if( !script_rid2sd(sd) )
  12802. return SCRIPT_CMD_SUCCESS;
  12803. fd = sd->fd;
  12804. } else { //Use a dummy character.
  12805. sd = &dummy_sd;
  12806. fd = 0;
  12807. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12808. if (st->oid) {
  12809. struct block_list* bl = map_id2bl(st->oid);
  12810. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12811. if (bl->type == BL_NPC)
  12812. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12813. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12814. }
  12815. // Init Group ID, Level, & permissions
  12816. sd->group_id = sd->group_level = 99;
  12817. sd->permissions |= PC_PERM_ALLPERMISSION;
  12818. }
  12819. if (!is_atcommand(fd, sd, cmd, type)) {
  12820. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12821. script_reportsrc(st);
  12822. return SCRIPT_CMD_FAILURE;
  12823. }
  12824. return SCRIPT_CMD_SUCCESS;
  12825. }
  12826. /*==========================================
  12827. * gmcommand [MouseJstr]
  12828. *------------------------------------------*/
  12829. BUILDIN_FUNC(atcommand) {
  12830. return atcommand_sub(st,0);
  12831. }
  12832. /** Displays a message for the player only (like system messages like "you got an apple" )
  12833. * dispbottom("<message>"{,<color>{,<char_id>}})
  12834. * @param message
  12835. * @param color Hex color default (Green)
  12836. */
  12837. BUILDIN_FUNC(dispbottom)
  12838. {
  12839. TBL_PC *sd;
  12840. int color = 0;
  12841. const char *message;
  12842. if (!script_charid2sd(4,sd))
  12843. return SCRIPT_CMD_FAILURE;
  12844. message = script_getstr(st,2);
  12845. if (script_hasdata(st,3))
  12846. color = script_getnum(st,3); // <color>
  12847. if(sd) {
  12848. if (script_hasdata(st,3))
  12849. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12850. else
  12851. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12852. }
  12853. return SCRIPT_CMD_SUCCESS;
  12854. }
  12855. /*===================================
  12856. * Heal portion of recovery command
  12857. *-----------------------------------*/
  12858. int recovery_sub(struct map_session_data* sd, int revive)
  12859. {
  12860. if(revive&(1|4) && pc_isdead(sd)) {
  12861. status_revive(&sd->bl, 100, 100);
  12862. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12863. clif_specialeffect(&sd->bl, EF_RESURRECTION, AREA);
  12864. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12865. status_percent_heal(&sd->bl, 100, 100);
  12866. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12867. }
  12868. return SCRIPT_CMD_SUCCESS;
  12869. }
  12870. /*=========================================================================
  12871. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12872. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12873. * <type> determines <option>:
  12874. * 0 : char_id
  12875. * 1 : party_id
  12876. * 2 : guild_id
  12877. * 3 : map_name
  12878. * 4 : all characters
  12879. * <revive_flag>:
  12880. * 1 : Revive and heal all players (default)
  12881. * 2 : Heal living players only
  12882. * 4 : Revive dead players only
  12883. * <map name>:
  12884. * for types 1-2 : map_name (null = all maps)
  12885. *-------------------------------------------------------------------------*/
  12886. BUILDIN_FUNC(recovery)
  12887. {
  12888. TBL_PC *sd;
  12889. int map_idx = 0, type = 0, revive = 1;
  12890. type = script_getnum(st,2);
  12891. if(script_hasdata(st,4))
  12892. revive = script_getnum(st,4);
  12893. switch(type) {
  12894. case 0:
  12895. if(!script_charid2sd(3,sd))
  12896. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12897. recovery_sub(sd, revive);
  12898. break;
  12899. case 1:
  12900. {
  12901. struct party_data* p;
  12902. //When no party given, we use invoker party
  12903. int p_id = 0, i;
  12904. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12905. map_idx = map_mapname2mapid(script_getstr(st,5));
  12906. if(map_idx < 1) { //But we'll check anyways
  12907. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12908. return SCRIPT_CMD_FAILURE;
  12909. }
  12910. }
  12911. if(script_hasdata(st,3))
  12912. p_id = script_getnum(st,3);
  12913. else if(script_rid2sd(sd))
  12914. p_id = sd->status.party_id;
  12915. p = party_search(p_id);
  12916. if(p == NULL)
  12917. return SCRIPT_CMD_SUCCESS;
  12918. for (i = 0; i < MAX_PARTY; i++) {
  12919. struct map_session_data* pl_sd;
  12920. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12921. || (map_idx && pl_sd->bl.m != map_idx))
  12922. continue;
  12923. recovery_sub(pl_sd, revive);
  12924. }
  12925. break;
  12926. }
  12927. case 2:
  12928. {
  12929. struct guild* g;
  12930. //When no guild given, we use invoker guild
  12931. int g_id = 0, i;
  12932. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12933. map_idx = map_mapname2mapid(script_getstr(st,5));
  12934. if(map_idx < 1) { //But we'll check anyways
  12935. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12936. return SCRIPT_CMD_FAILURE;
  12937. }
  12938. }
  12939. if(script_hasdata(st,3))
  12940. g_id = script_getnum(st,3);
  12941. else if(script_rid2sd(sd))
  12942. g_id = sd->status.guild_id;
  12943. g = guild_search(g_id);
  12944. if(g == NULL)
  12945. return SCRIPT_CMD_SUCCESS;
  12946. for (i = 0; i < MAX_GUILD; i++) {
  12947. struct map_session_data* pl_sd;
  12948. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12949. || (map_idx && pl_sd->bl.m != map_idx))
  12950. continue;
  12951. recovery_sub(pl_sd, revive);
  12952. }
  12953. break;
  12954. }
  12955. case 3:
  12956. if(script_hasdata(st,3))
  12957. map_idx = map_mapname2mapid(script_getstr(st,3));
  12958. else if(script_rid2sd(sd))
  12959. map_idx = sd->bl.m;
  12960. if(map_idx < 1)
  12961. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12962. case 4:
  12963. {
  12964. struct s_mapiterator *iter;
  12965. struct script_data *data = script_getdata(st, 3);
  12966. get_val(st, data); // Convert into value in case of a variable
  12967. if(script_hasdata(st,3) && !data_isstring(data))
  12968. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12969. iter = mapit_getallusers();
  12970. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12971. if(type == 3 && sd->bl.m != map_idx)
  12972. continue;
  12973. recovery_sub(sd, revive);
  12974. }
  12975. mapit_free(iter);
  12976. break;
  12977. }
  12978. default:
  12979. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12980. script_pushint(st,-1);
  12981. return SCRIPT_CMD_FAILURE;
  12982. }
  12983. script_pushint(st,1); //Successfully executed without errors
  12984. return SCRIPT_CMD_SUCCESS;
  12985. }
  12986. /*==========================================
  12987. * Get your pet info: getpetinfo <type>{,<char_id>}
  12988. * n -> 0:pet_id 1:pet_class 2:pet_name
  12989. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12990. * 7:game id
  12991. *------------------------------------------*/
  12992. BUILDIN_FUNC(getpetinfo)
  12993. {
  12994. TBL_PC *sd;
  12995. TBL_PET *pd;
  12996. int type = script_getnum(st,2);
  12997. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12998. if (type == 2)
  12999. script_pushconststr(st,"null");
  13000. else
  13001. script_pushint(st,0);
  13002. return SCRIPT_CMD_SUCCESS;
  13003. }
  13004. switch(type){
  13005. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  13006. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  13007. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  13008. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  13009. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  13010. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  13011. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  13012. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  13013. case PETINFO_EGGID: script_pushint(st,pd->pet.egg_id); break;
  13014. case PETINFO_FOODID: script_pushint(st,pd->get_pet_db()->FoodID); break;
  13015. default:
  13016. script_pushint(st,0);
  13017. break;
  13018. }
  13019. return SCRIPT_CMD_SUCCESS;
  13020. }
  13021. /*==========================================
  13022. * Get your homunculus info: gethominfo <type>{,<char_id>};
  13023. * type -> 0:hom_id 1:class 2:name
  13024. * 3:friendly 4:hungry, 5: rename flag.
  13025. * 6: level, 7: game id
  13026. *------------------------------------------*/
  13027. BUILDIN_FUNC(gethominfo)
  13028. {
  13029. TBL_PC *sd;
  13030. TBL_HOM *hd;
  13031. int type=script_getnum(st,2);
  13032. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  13033. if (type == 2)
  13034. script_pushconststr(st,"null");
  13035. else
  13036. script_pushint(st,0);
  13037. return SCRIPT_CMD_SUCCESS;
  13038. }
  13039. switch(type){
  13040. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  13041. case 1: script_pushint(st,hd->homunculus.class_); break;
  13042. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  13043. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  13044. case 4: script_pushint(st,hd->homunculus.hunger); break;
  13045. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  13046. case 6: script_pushint(st,hd->homunculus.level); break;
  13047. case 7: script_pushint(st,hd->bl.id); break;
  13048. default:
  13049. script_pushint(st,0);
  13050. break;
  13051. }
  13052. return SCRIPT_CMD_SUCCESS;
  13053. }
  13054. /// Retrieves information about character's mercenary
  13055. /// getmercinfo <type>[,<char id>];
  13056. BUILDIN_FUNC(getmercinfo)
  13057. {
  13058. int type;
  13059. struct map_session_data* sd;
  13060. struct mercenary_data* md;
  13061. if( !script_charid2sd(3,sd) ){
  13062. script_pushnil(st);
  13063. return SCRIPT_CMD_FAILURE;
  13064. }
  13065. type = script_getnum(st,2);
  13066. md = sd->md;
  13067. if( md == NULL ){
  13068. if( type == 2 )
  13069. script_pushconststr(st,"");
  13070. else
  13071. script_pushint(st,0);
  13072. return SCRIPT_CMD_SUCCESS;
  13073. }
  13074. switch( type )
  13075. {
  13076. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  13077. case 1: script_pushint(st,md->mercenary.class_); break;
  13078. case 2: script_pushstrcopy(st,md->db->name); break;
  13079. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  13080. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  13081. case 5: script_pushint(st,md->mercenary.kill_count); break;
  13082. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  13083. case 7: script_pushint(st,md->db->lv); break;
  13084. case 8: script_pushint(st,md->bl.id); break;
  13085. default:
  13086. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  13087. script_pushnil(st);
  13088. return SCRIPT_CMD_FAILURE;
  13089. }
  13090. return SCRIPT_CMD_SUCCESS;
  13091. }
  13092. /*==========================================
  13093. * Shows wether your inventory(and equips) contain
  13094. selected card or not.
  13095. checkequipedcard(4001);
  13096. *------------------------------------------*/
  13097. BUILDIN_FUNC(checkequipedcard)
  13098. {
  13099. TBL_PC *sd;
  13100. if(script_rid2sd(sd)){
  13101. int n,i,c=0;
  13102. c=script_getnum(st,2);
  13103. for(i=0;i<MAX_INVENTORY;i++){
  13104. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  13105. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  13106. continue;
  13107. for(n=0;n<sd->inventory_data[i]->slot;n++){
  13108. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  13109. script_pushint(st,1);
  13110. return SCRIPT_CMD_SUCCESS;
  13111. }
  13112. }
  13113. }
  13114. }
  13115. }
  13116. script_pushint(st,0);
  13117. return SCRIPT_CMD_SUCCESS;
  13118. }
  13119. BUILDIN_FUNC(jump_zero)
  13120. {
  13121. int sel;
  13122. sel=script_getnum(st,2);
  13123. if(!sel) {
  13124. int pos;
  13125. if( !data_islabel(script_getdata(st,3)) ){
  13126. ShowError("script: jump_zero: not label !\n");
  13127. st->state=END;
  13128. return SCRIPT_CMD_FAILURE;
  13129. }
  13130. pos=script_getnum(st,3);
  13131. st->pos=pos;
  13132. st->state=GOTO;
  13133. }
  13134. return SCRIPT_CMD_SUCCESS;
  13135. }
  13136. /*==========================================
  13137. * movenpc [MouseJstr]
  13138. *------------------------------------------*/
  13139. BUILDIN_FUNC(movenpc)
  13140. {
  13141. TBL_NPC *nd = NULL;
  13142. const char *npc;
  13143. int x,y;
  13144. npc = script_getstr(st,2);
  13145. x = script_getnum(st,3);
  13146. y = script_getnum(st,4);
  13147. if ((nd = npc_name2id(npc)) == NULL){
  13148. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  13149. return -1;
  13150. }
  13151. if (script_hasdata(st,5))
  13152. nd->ud.dir = script_getnum(st,5) % 8;
  13153. npc_movenpc(nd, x, y);
  13154. return SCRIPT_CMD_SUCCESS;
  13155. }
  13156. /*==========================================
  13157. * message [MouseJstr]
  13158. *------------------------------------------*/
  13159. BUILDIN_FUNC(message)
  13160. {
  13161. const char *msg,*player;
  13162. TBL_PC *pl_sd = NULL;
  13163. player = script_getstr(st,2);
  13164. msg = script_getstr(st,3);
  13165. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  13166. return SCRIPT_CMD_SUCCESS;
  13167. clif_displaymessage(pl_sd->fd, msg);
  13168. return SCRIPT_CMD_SUCCESS;
  13169. }
  13170. /**
  13171. * npctalk("<message>"{,"<NPC name>","<flag>"});
  13172. * @param flag: Specify target
  13173. * BC_ALL - Broadcast message is sent server-wide.
  13174. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  13175. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  13176. * BC_SELF - Message is sent only to player attached.
  13177. */
  13178. BUILDIN_FUNC(npctalk)
  13179. {
  13180. struct npc_data* nd = NULL;
  13181. const char* str = script_getstr(st,2);
  13182. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  13183. nd = npc_name2id(script_getstr(st, 3));
  13184. else
  13185. nd = (struct npc_data *)map_id2bl(st->oid);
  13186. if (nd != NULL) {
  13187. send_target target = AREA;
  13188. char message[CHAT_SIZE_MAX];
  13189. if (script_hasdata(st, 4)) {
  13190. switch(script_getnum(st, 4)) {
  13191. case BC_ALL: target = ALL_CLIENT; break;
  13192. case BC_MAP: target = ALL_SAMEMAP; break;
  13193. case BC_SELF: target = SELF; break;
  13194. case BC_AREA:
  13195. default: target = AREA; break;
  13196. }
  13197. }
  13198. safesnprintf(message, sizeof(message), "%s", str);
  13199. if (target != SELF)
  13200. clif_messagecolor(&nd->bl, color_table[COLOR_WHITE], message, false, target);
  13201. else {
  13202. TBL_PC *sd = map_id2sd(st->rid);
  13203. if (sd == NULL)
  13204. return SCRIPT_CMD_FAILURE;
  13205. clif_messagecolor_target(&nd->bl, color_table[COLOR_WHITE], message, false, target, sd);
  13206. }
  13207. }
  13208. return SCRIPT_CMD_SUCCESS;
  13209. }
  13210. /**
  13211. * Sends a message to the waitingroom of the invoking NPC.
  13212. * chatmes "<message>"{,"<NPC name>"};
  13213. * @author Jey
  13214. */
  13215. BUILDIN_FUNC(chatmes)
  13216. {
  13217. struct npc_data* nd = NULL;
  13218. const char* str = script_getstr(st,2);
  13219. if (script_hasdata(st, 3))
  13220. nd = npc_name2id(script_getstr(st, 3));
  13221. else
  13222. nd = (struct npc_data *)map_id2bl(st->oid);
  13223. if (nd != NULL && nd->chat_id) {
  13224. char message[256];
  13225. safesnprintf(message, sizeof(message), "%s", str);
  13226. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  13227. }
  13228. return SCRIPT_CMD_SUCCESS;
  13229. }
  13230. // change npc walkspeed [Valaris]
  13231. BUILDIN_FUNC(npcspeed)
  13232. {
  13233. struct npc_data* nd;
  13234. int speed;
  13235. speed = script_getnum(st,2);
  13236. nd =(struct npc_data *)map_id2bl(st->oid);
  13237. if( nd ) {
  13238. nd->speed = speed;
  13239. nd->ud.state.speed_changed = 1;
  13240. }
  13241. return SCRIPT_CMD_SUCCESS;
  13242. }
  13243. // make an npc walk to a position [Valaris]
  13244. BUILDIN_FUNC(npcwalkto)
  13245. {
  13246. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13247. int x=0,y=0;
  13248. x=script_getnum(st,2);
  13249. y=script_getnum(st,3);
  13250. if(nd) {
  13251. if (!nd->status.hp)
  13252. status_calc_npc(nd, SCO_FIRST);
  13253. else
  13254. status_calc_npc(nd, SCO_NONE);
  13255. unit_walktoxy(&nd->bl,x,y,0);
  13256. }
  13257. return SCRIPT_CMD_SUCCESS;
  13258. }
  13259. // stop an npc's movement [Valaris]
  13260. BUILDIN_FUNC(npcstop)
  13261. {
  13262. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13263. if(nd) {
  13264. unit_stop_walking(&nd->bl,1|4);
  13265. }
  13266. return SCRIPT_CMD_SUCCESS;
  13267. }
  13268. /**
  13269. * getlook(<type>{,<char_id>})
  13270. **/
  13271. BUILDIN_FUNC(getlook)
  13272. {
  13273. int type,val;
  13274. TBL_PC *sd;
  13275. if (!script_charid2sd(3,sd)) {
  13276. script_pushint(st,-1);
  13277. return SCRIPT_CMD_FAILURE;
  13278. }
  13279. type=script_getnum(st,2);
  13280. val=-1;
  13281. switch(type) {
  13282. // TODO: implement LOOK_BASE as stated in script doc
  13283. case LOOK_HAIR: val=sd->status.hair; break; //1
  13284. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13285. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13286. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13287. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13288. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13289. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13290. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13291. case LOOK_SHOES: break; //9
  13292. case LOOK_ROBE: val=sd->status.robe; break; //12
  13293. case LOOK_BODY2: val=sd->status.body; break; //13
  13294. }
  13295. script_pushint(st,val);
  13296. return SCRIPT_CMD_SUCCESS;
  13297. }
  13298. /**
  13299. * getsavepoint(<information type>{,<char_id>})
  13300. * @param type 0- map name, 1- x, 2- y
  13301. **/
  13302. BUILDIN_FUNC(getsavepoint)
  13303. {
  13304. TBL_PC* sd;
  13305. int type;
  13306. if (!script_charid2sd(3,sd)) {
  13307. script_pushint(st,0);
  13308. return SCRIPT_CMD_SUCCESS;
  13309. }
  13310. type = script_getnum(st,2);
  13311. switch(type) {
  13312. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13313. case 1: script_pushint(st,sd->status.save_point.x); break;
  13314. case 2: script_pushint(st,sd->status.save_point.y); break;
  13315. default:
  13316. script_pushint(st,0);
  13317. break;
  13318. }
  13319. return SCRIPT_CMD_SUCCESS;
  13320. }
  13321. /**
  13322. * Get position for BL objects.
  13323. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13324. * @param mapname: String variable for output map name
  13325. * @param x: Integer variable for output coord X
  13326. * @param y: Integer variable for output coord Y
  13327. * @param type: Type of object
  13328. * UNITTYPE_PC - Character coord
  13329. * UNITTYPE_NPC - NPC coord
  13330. * UNITTYPE_PET - Pet coord
  13331. * UNITTYPE_HOM - Homun coord
  13332. * UNITTYPE_MER - Mercenary coord
  13333. * UNITTYPE_ELEM - Elemental coord
  13334. * @param charname: Name object. If empty or "this" use the current object
  13335. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13336. */
  13337. BUILDIN_FUNC(getmapxy)
  13338. {
  13339. struct block_list *bl = NULL;
  13340. TBL_PC *sd=NULL;
  13341. int64 num;
  13342. const char *name;
  13343. char prefix;
  13344. int x,y,type;
  13345. char mapname[MAP_NAME_LENGTH];
  13346. if( !data_isreference(script_getdata(st,2)) ) {
  13347. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13348. script_pushint(st,-1);
  13349. return SCRIPT_CMD_FAILURE;
  13350. }
  13351. if( !data_isreference(script_getdata(st,3)) ) {
  13352. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13353. script_pushint(st,-1);
  13354. return SCRIPT_CMD_FAILURE;
  13355. }
  13356. if( !data_isreference(script_getdata(st,4)) ) {
  13357. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13358. script_pushint(st,-1);
  13359. return SCRIPT_CMD_FAILURE;
  13360. }
  13361. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13362. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13363. script_pushint(st,-1);
  13364. return SCRIPT_CMD_FAILURE;
  13365. }
  13366. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13367. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13368. script_pushint(st,-1);
  13369. return SCRIPT_CMD_FAILURE;
  13370. }
  13371. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13372. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13373. script_pushint(st,-1);
  13374. return SCRIPT_CMD_FAILURE;
  13375. }
  13376. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13377. type=script_getnum(st,5);
  13378. switch (type) {
  13379. case UNITTYPE_PC: //Get Character Position
  13380. if( script_nick2sd(6,sd) )
  13381. bl = &sd->bl;
  13382. break;
  13383. case UNITTYPE_NPC: //Get NPC Position
  13384. if( script_hasdata(st,6) )
  13385. {
  13386. struct npc_data *nd;
  13387. nd=npc_name2id(script_getstr(st,6));
  13388. if (nd)
  13389. bl = &nd->bl;
  13390. } else //In case the origin is not an npc?
  13391. bl=map_id2bl(st->oid);
  13392. break;
  13393. case UNITTYPE_PET: //Get Pet Position
  13394. if( script_nick2sd(6, sd) && sd->pd )
  13395. bl = &sd->pd->bl;
  13396. break;
  13397. case UNITTYPE_HOM: //Get Homun Position
  13398. if( script_nick2sd(6, sd) && sd->hd )
  13399. bl = &sd->hd->bl;
  13400. break;
  13401. case UNITTYPE_MER: //Get Mercenary Position
  13402. if( script_nick2sd(6, sd) && sd->md )
  13403. bl = &sd->md->bl;
  13404. break;
  13405. case UNITTYPE_ELEM: //Get Elemental Position
  13406. if( script_nick2sd(6, sd) && sd->ed )
  13407. bl = &sd->ed->bl;
  13408. break;
  13409. default:
  13410. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13411. script_pushint(st,-1);
  13412. return SCRIPT_CMD_FAILURE;
  13413. }
  13414. if (!bl) { //No object found.
  13415. script_pushint(st,-1);
  13416. return SCRIPT_CMD_SUCCESS;
  13417. }
  13418. x= bl->x;
  13419. y= bl->y;
  13420. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  13421. //Set MapName$
  13422. num=st->stack->stack_data[st->start+2].u.num;
  13423. name=get_str(script_getvarid(num));
  13424. prefix=*name;
  13425. if(not_server_variable(prefix)){
  13426. if( !script_rid2sd(sd) ){
  13427. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13428. return SCRIPT_CMD_FAILURE;
  13429. }
  13430. }else
  13431. sd=NULL;
  13432. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  13433. //Set MapX
  13434. num=st->stack->stack_data[st->start+3].u.num;
  13435. name=get_str(script_getvarid(num));
  13436. prefix=*name;
  13437. if(not_server_variable(prefix)){
  13438. if( !script_rid2sd(sd) ){
  13439. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13440. return SCRIPT_CMD_FAILURE;
  13441. }
  13442. }else
  13443. sd=NULL;
  13444. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  13445. //Set MapY
  13446. num=st->stack->stack_data[st->start+4].u.num;
  13447. name=get_str(script_getvarid(num));
  13448. prefix=*name;
  13449. if(not_server_variable(prefix)){
  13450. if( !script_rid2sd(sd) ){
  13451. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13452. return SCRIPT_CMD_FAILURE;
  13453. }
  13454. }else
  13455. sd=NULL;
  13456. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  13457. //Return Success value
  13458. script_pushint(st,0);
  13459. return SCRIPT_CMD_SUCCESS;
  13460. }
  13461. /// Returns the map name of given map ID.
  13462. ///
  13463. /// mapid2name <map ID>;
  13464. BUILDIN_FUNC(mapid2name)
  13465. {
  13466. uint16 m = script_getnum(st, 2);
  13467. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  13468. script_pushconststr(st, "");
  13469. return SCRIPT_CMD_FAILURE;
  13470. }
  13471. script_pushstrcopy(st, map_mapid2mapname(m));
  13472. return SCRIPT_CMD_SUCCESS;
  13473. }
  13474. /*==========================================
  13475. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13476. *------------------------------------------*/
  13477. BUILDIN_FUNC(logmes)
  13478. {
  13479. const char *str;
  13480. TBL_PC* sd;
  13481. if( !script_rid2sd(sd) )
  13482. return SCRIPT_CMD_FAILURE;
  13483. str = script_getstr(st,2);
  13484. log_npc(sd,str);
  13485. return SCRIPT_CMD_SUCCESS;
  13486. }
  13487. BUILDIN_FUNC(summon)
  13488. {
  13489. int _class, timeout=0;
  13490. const char *str,*event="";
  13491. TBL_PC *sd;
  13492. struct mob_data *md;
  13493. int tick = gettick();
  13494. if (!script_rid2sd(sd))
  13495. return SCRIPT_CMD_SUCCESS;
  13496. str =script_getstr(st,2);
  13497. _class=script_getnum(st,3);
  13498. if( script_hasdata(st,4) )
  13499. timeout=script_getnum(st,4);
  13500. if( script_hasdata(st,5) ){
  13501. event=script_getstr(st,5);
  13502. check_event(st, event);
  13503. }
  13504. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13505. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13506. if (md) {
  13507. md->master_id=sd->bl.id;
  13508. md->special_state.ai = AI_ATTACK;
  13509. if( md->deletetimer != INVALID_TIMER )
  13510. delete_timer(md->deletetimer, mob_timer_delete);
  13511. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13512. mob_spawn (md); //Now it is ready for spawning.
  13513. clif_specialeffect(&md->bl,EF_ENTRY2,AREA);
  13514. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13515. }
  13516. script_pushint(st, md->bl.id);
  13517. return SCRIPT_CMD_SUCCESS;
  13518. }
  13519. /*==========================================
  13520. * Checks whether it is daytime/nighttime
  13521. *------------------------------------------*/
  13522. BUILDIN_FUNC(isnight)
  13523. {
  13524. script_pushint(st,(night_flag == 1));
  13525. return SCRIPT_CMD_SUCCESS;
  13526. }
  13527. BUILDIN_FUNC(isday)
  13528. {
  13529. script_pushint(st,(night_flag == 0));
  13530. return SCRIPT_CMD_SUCCESS;
  13531. }
  13532. /*================================================
  13533. * Check how many items/cards in the list are
  13534. * equipped - used for 2/15's cards patch [celest]
  13535. *------------------------------------------------*/
  13536. BUILDIN_FUNC(isequippedcnt)
  13537. {
  13538. TBL_PC *sd;
  13539. int i, id = 1;
  13540. int ret = 0;
  13541. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13542. script_pushint(st,0);
  13543. return SCRIPT_CMD_SUCCESS;
  13544. }
  13545. for (i=0; id!=0; i++) {
  13546. short j;
  13547. FETCH (i+2, id) else id = 0;
  13548. if (id <= 0)
  13549. continue;
  13550. for (j=0; j<EQI_MAX; j++) {
  13551. short index = sd->equip_index[j];
  13552. if(index < 0)
  13553. continue;
  13554. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13555. continue;
  13556. if(!sd->inventory_data[index])
  13557. continue;
  13558. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13559. if (sd->inventory_data[index]->nameid == id)
  13560. ret+= sd->inventory.u.items_inventory[index].amount;
  13561. } else { //Count cards.
  13562. short k;
  13563. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13564. continue; //No cards
  13565. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13566. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13567. ret++; //[Lupus]
  13568. }
  13569. }
  13570. }
  13571. }
  13572. script_pushint(st,ret);
  13573. return SCRIPT_CMD_SUCCESS;
  13574. }
  13575. /*================================================
  13576. * Check whether another card has been
  13577. * equipped - used for 2/15's cards patch [celest]
  13578. * -- Items checked cannot be reused in another
  13579. * card set to prevent exploits
  13580. *------------------------------------------------*/
  13581. BUILDIN_FUNC(isequipped)
  13582. {
  13583. TBL_PC *sd;
  13584. int i, id = 1;
  13585. int ret = -1;
  13586. //Original hash to reverse it when full check fails.
  13587. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13588. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13589. script_pushint(st,0);
  13590. return SCRIPT_CMD_SUCCESS;
  13591. }
  13592. setitem_hash = sd->bonus.setitem_hash;
  13593. setitem_hash2 = sd->bonus.setitem_hash2;
  13594. for (i=0; id!=0; i++) {
  13595. int flag = 0;
  13596. short j;
  13597. FETCH (i+2, id) else id = 0;
  13598. if (id <= 0)
  13599. continue;
  13600. for (j=0; j<EQI_MAX; j++) {
  13601. short index = sd->equip_index[j];
  13602. if(index < 0)
  13603. continue;
  13604. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13605. continue;
  13606. if(!sd->inventory_data[index])
  13607. continue;
  13608. if (itemdb_type(id) != IT_CARD) {
  13609. if (sd->inventory_data[index]->nameid != id)
  13610. continue;
  13611. flag = 1;
  13612. break;
  13613. } else { //Cards
  13614. short k;
  13615. if (sd->inventory_data[index]->slot == 0 ||
  13616. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13617. continue;
  13618. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13619. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13620. unsigned int hash = 0;
  13621. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13622. continue;
  13623. hash = 1<<((j<5?j:j-5)*4 + k);
  13624. // check if card is already used by another set
  13625. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13626. continue;
  13627. // We have found a match
  13628. flag = 1;
  13629. // Set hash so this card cannot be used by another
  13630. if (j<5)
  13631. sd->bonus.setitem_hash |= hash;
  13632. else
  13633. sd->bonus.setitem_hash2 |= hash;
  13634. break;
  13635. }
  13636. }
  13637. if (flag) break; //Card found
  13638. }
  13639. if (ret == -1)
  13640. ret = flag;
  13641. else
  13642. ret &= flag;
  13643. if (!ret) break;
  13644. }
  13645. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13646. sd->bonus.setitem_hash = setitem_hash;
  13647. sd->bonus.setitem_hash2 = setitem_hash2;
  13648. }
  13649. script_pushint(st,ret);
  13650. return SCRIPT_CMD_SUCCESS;
  13651. }
  13652. /*================================================
  13653. * Check how many given inserted cards in the CURRENT
  13654. * weapon - used for 2/15's cards patch [Lupus]
  13655. *------------------------------------------------*/
  13656. BUILDIN_FUNC(cardscnt)
  13657. {
  13658. TBL_PC *sd;
  13659. int i, k, id = 1;
  13660. int ret = 0;
  13661. int index;
  13662. if( !script_rid2sd(sd) ){
  13663. script_pushint(st,0);
  13664. return SCRIPT_CMD_SUCCESS;
  13665. }
  13666. for (i=0; id!=0; i++) {
  13667. FETCH (i+2, id) else id = 0;
  13668. if (id <= 0)
  13669. continue;
  13670. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13671. if(index < 0) continue;
  13672. if(!sd->inventory_data[index])
  13673. continue;
  13674. if(itemdb_type(id) != IT_CARD) {
  13675. if (sd->inventory_data[index]->nameid == id)
  13676. ret+= sd->inventory.u.items_inventory[index].amount;
  13677. } else {
  13678. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13679. continue;
  13680. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13681. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13682. ret++;
  13683. }
  13684. }
  13685. }
  13686. script_pushint(st,ret);
  13687. // script_pushint(st,current_equip_item_index);
  13688. return SCRIPT_CMD_SUCCESS;
  13689. }
  13690. /*=======================================================
  13691. * Returns the refined number of the current item, or an
  13692. * item with inventory index specified
  13693. *-------------------------------------------------------*/
  13694. BUILDIN_FUNC(getrefine)
  13695. {
  13696. TBL_PC *sd;
  13697. if (script_rid2sd(sd)){
  13698. if( current_equip_item_index == -1 ){
  13699. script_pushint(st, 0);
  13700. return SCRIPT_CMD_FAILURE;
  13701. }
  13702. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13703. }else
  13704. script_pushint(st,0);
  13705. return SCRIPT_CMD_SUCCESS;
  13706. }
  13707. /*=======================================================
  13708. * Day/Night controls
  13709. *-------------------------------------------------------*/
  13710. BUILDIN_FUNC(night)
  13711. {
  13712. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13713. return SCRIPT_CMD_SUCCESS;
  13714. }
  13715. BUILDIN_FUNC(day)
  13716. {
  13717. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13718. return SCRIPT_CMD_SUCCESS;
  13719. }
  13720. /**
  13721. * unequip <equipment slot>{,<char_id>};
  13722. * @author [Spectre]
  13723. **/
  13724. BUILDIN_FUNC(unequip) {
  13725. int pos;
  13726. TBL_PC *sd;
  13727. if (!script_charid2sd(3,sd))
  13728. return SCRIPT_CMD_FAILURE;
  13729. pos = script_getnum(st,2);
  13730. if (equip_index_check(pos)) {
  13731. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13732. if (i >= 0) {
  13733. pc_unequipitem(sd,i,1|2);
  13734. script_pushint(st, 1);
  13735. return SCRIPT_CMD_SUCCESS;
  13736. }
  13737. }
  13738. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13739. script_pushint(st, 0);
  13740. return SCRIPT_CMD_FAILURE;
  13741. }
  13742. /**
  13743. * equip <item id>{,<char_id>};
  13744. **/
  13745. BUILDIN_FUNC(equip) {
  13746. unsigned short nameid = 0;
  13747. TBL_PC *sd;
  13748. struct item_data *item_data;
  13749. if (!script_charid2sd(3,sd))
  13750. return SCRIPT_CMD_FAILURE;
  13751. nameid = script_getnum(st,2);
  13752. if ((item_data = itemdb_exists(nameid))) {
  13753. int i;
  13754. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13755. if (i < MAX_INVENTORY) {
  13756. pc_equipitem(sd,i,item_data->equip);
  13757. script_pushint(st,1);
  13758. return SCRIPT_CMD_SUCCESS;
  13759. }
  13760. }
  13761. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13762. script_pushint(st,0);
  13763. return SCRIPT_CMD_FAILURE;
  13764. }
  13765. BUILDIN_FUNC(autoequip)
  13766. {
  13767. unsigned short nameid;
  13768. int flag;
  13769. struct item_data *item_data;
  13770. nameid=script_getnum(st,2);
  13771. flag=script_getnum(st,3);
  13772. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13773. {
  13774. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13775. return SCRIPT_CMD_FAILURE;
  13776. }
  13777. if( !itemdb_isequip2(item_data) )
  13778. {
  13779. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13780. return SCRIPT_CMD_FAILURE;
  13781. }
  13782. item_data->flag.autoequip = flag>0?1:0;
  13783. return SCRIPT_CMD_SUCCESS;
  13784. }
  13785. /**
  13786. * Set the value of a given battle config
  13787. * setbattleflag "<battle flag>",<value>{,<reload>};
  13788. */
  13789. BUILDIN_FUNC(setbattleflag)
  13790. {
  13791. const char *flag, *value;
  13792. flag = script_getstr(st,2);
  13793. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13794. if (battle_set_value(flag, value) == 0)
  13795. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13796. else {
  13797. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13798. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  13799. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  13800. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  13801. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  13802. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  13803. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  13804. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  13805. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  13806. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  13807. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  13808. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  13809. };
  13810. uint8 i;
  13811. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  13812. if (!strcmpi(flag, expdrop_flags[i])) {
  13813. mob_reload();
  13814. break;
  13815. }
  13816. }
  13817. }
  13818. }
  13819. return SCRIPT_CMD_SUCCESS;
  13820. }
  13821. /**
  13822. * Get the value of a given battle config
  13823. * getbattleflag("<battle flag>")
  13824. */
  13825. BUILDIN_FUNC(getbattleflag)
  13826. {
  13827. const char *flag = script_getstr(st,2);
  13828. script_pushint(st,battle_get_value(flag));
  13829. return SCRIPT_CMD_SUCCESS;
  13830. }
  13831. //=======================================================
  13832. // strlen [Valaris]
  13833. //-------------------------------------------------------
  13834. BUILDIN_FUNC(getstrlen)
  13835. {
  13836. const char *str = script_getstr(st,2);
  13837. int len = (str) ? (int)strlen(str) : 0;
  13838. script_pushint(st,len);
  13839. return SCRIPT_CMD_SUCCESS;
  13840. }
  13841. //=======================================================
  13842. // isalpha [Valaris]
  13843. //-------------------------------------------------------
  13844. BUILDIN_FUNC(charisalpha)
  13845. {
  13846. const char *str=script_getstr(st,2);
  13847. int pos=script_getnum(st,3);
  13848. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13849. script_pushint(st,val);
  13850. return SCRIPT_CMD_SUCCESS;
  13851. }
  13852. //=======================================================
  13853. // charisupper <str>, <index>
  13854. //-------------------------------------------------------
  13855. BUILDIN_FUNC(charisupper)
  13856. {
  13857. const char *str = script_getstr(st,2);
  13858. int pos = script_getnum(st,3);
  13859. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13860. script_pushint(st,val);
  13861. return SCRIPT_CMD_SUCCESS;
  13862. }
  13863. //=======================================================
  13864. // charislower <str>, <index>
  13865. //-------------------------------------------------------
  13866. BUILDIN_FUNC(charislower)
  13867. {
  13868. const char *str = script_getstr(st,2);
  13869. int pos = script_getnum(st,3);
  13870. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13871. script_pushint(st,val);
  13872. return SCRIPT_CMD_SUCCESS;
  13873. }
  13874. //=======================================================
  13875. // charat <str>, <index>
  13876. //-------------------------------------------------------
  13877. BUILDIN_FUNC(charat) {
  13878. const char *str = script_getstr(st,2);
  13879. int pos = script_getnum(st,3);
  13880. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13881. char output[2];
  13882. output[0] = str[pos];
  13883. output[1] = '\0';
  13884. script_pushstrcopy(st, output);
  13885. } else
  13886. script_pushconststr(st, "");
  13887. return SCRIPT_CMD_SUCCESS;
  13888. }
  13889. //=======================================================
  13890. // setchar <string>, <char>, <index>
  13891. //-------------------------------------------------------
  13892. BUILDIN_FUNC(setchar)
  13893. {
  13894. const char *str = script_getstr(st,2);
  13895. const char *c = script_getstr(st,3);
  13896. int index = script_getnum(st,4);
  13897. char *output = aStrdup(str);
  13898. if(index >= 0 && index < strlen(output))
  13899. output[index] = *c;
  13900. script_pushstr(st, output);
  13901. return SCRIPT_CMD_SUCCESS;
  13902. }
  13903. //=======================================================
  13904. // insertchar <string>, <char>, <index>
  13905. //-------------------------------------------------------
  13906. BUILDIN_FUNC(insertchar)
  13907. {
  13908. const char *str = script_getstr(st,2);
  13909. const char *c = script_getstr(st,3);
  13910. int index = script_getnum(st,4);
  13911. char *output;
  13912. size_t len = strlen(str);
  13913. if(index < 0)
  13914. index = 0;
  13915. else if(index > len)
  13916. index = len;
  13917. output = (char*)aMalloc(len + 2);
  13918. memcpy(output, str, index);
  13919. output[index] = c[0];
  13920. memcpy(&output[index+1], &str[index], len - index);
  13921. output[len+1] = '\0';
  13922. script_pushstr(st, output);
  13923. return SCRIPT_CMD_SUCCESS;
  13924. }
  13925. //=======================================================
  13926. // delchar <string>, <index>
  13927. //-------------------------------------------------------
  13928. BUILDIN_FUNC(delchar)
  13929. {
  13930. const char *str = script_getstr(st,2);
  13931. int index = script_getnum(st,3);
  13932. char *output;
  13933. size_t len = strlen(str);
  13934. if(index < 0 || index > len) {
  13935. //return original
  13936. output = aStrdup(str);
  13937. script_pushstr(st, output);
  13938. return SCRIPT_CMD_SUCCESS;
  13939. }
  13940. output = (char*)aMalloc(len);
  13941. memcpy(output, str, index);
  13942. memcpy(&output[index], &str[index+1], len - index);
  13943. script_pushstr(st, output);
  13944. return SCRIPT_CMD_SUCCESS;
  13945. }
  13946. //=======================================================
  13947. // strtoupper <str>
  13948. //-------------------------------------------------------
  13949. BUILDIN_FUNC(strtoupper)
  13950. {
  13951. const char *str = script_getstr(st,2);
  13952. char *output = aStrdup(str);
  13953. char *cursor = output;
  13954. while (*cursor != '\0') {
  13955. *cursor = TOUPPER(*cursor);
  13956. cursor++;
  13957. }
  13958. script_pushstr(st, output);
  13959. return SCRIPT_CMD_SUCCESS;
  13960. }
  13961. //=======================================================
  13962. // strtolower <str>
  13963. //-------------------------------------------------------
  13964. BUILDIN_FUNC(strtolower)
  13965. {
  13966. const char *str = script_getstr(st,2);
  13967. char *output = aStrdup(str);
  13968. char *cursor = output;
  13969. while (*cursor != '\0') {
  13970. *cursor = TOLOWER(*cursor);
  13971. cursor++;
  13972. }
  13973. script_pushstr(st, output);
  13974. return SCRIPT_CMD_SUCCESS;
  13975. }
  13976. //=======================================================
  13977. // substr <str>, <start>, <end>
  13978. //-------------------------------------------------------
  13979. BUILDIN_FUNC(substr)
  13980. {
  13981. const char *str = script_getstr(st,2);
  13982. char *output;
  13983. int start = script_getnum(st,3);
  13984. int end = script_getnum(st,4);
  13985. int len = 0;
  13986. if(start >= 0 && end < strlen(str) && start <= end) {
  13987. len = end - start + 1;
  13988. output = (char*)aMalloc(len + 1);
  13989. memcpy(output, &str[start], len);
  13990. } else
  13991. output = (char*)aMalloc(1);
  13992. output[len] = '\0';
  13993. script_pushstr(st, output);
  13994. return SCRIPT_CMD_SUCCESS;
  13995. }
  13996. //=======================================================
  13997. // explode <dest_string_array>, <str>, <delimiter>
  13998. // Note: delimiter is limited to 1 char
  13999. //-------------------------------------------------------
  14000. BUILDIN_FUNC(explode)
  14001. {
  14002. struct script_data* data = script_getdata(st, 2);
  14003. const char *str = script_getstr(st,3);
  14004. const char delimiter = script_getstr(st, 4)[0];
  14005. int32 id;
  14006. size_t len = strlen(str);
  14007. int i = 0, j = 0;
  14008. int start;
  14009. char *temp;
  14010. const char* name;
  14011. TBL_PC* sd = NULL;
  14012. temp = (char*)aMalloc(len + 1);
  14013. if( !data_isreference(data) ) {
  14014. ShowError("script:explode: not a variable\n");
  14015. script_reportdata(data);
  14016. st->state = END;
  14017. return SCRIPT_CMD_FAILURE;// not a variable
  14018. }
  14019. id = reference_getid(data);
  14020. start = reference_getindex(data);
  14021. name = reference_getname(data);
  14022. if( !is_string_variable(name) ) {
  14023. ShowError("script:explode: not string array\n");
  14024. script_reportdata(data);
  14025. st->state = END;
  14026. return SCRIPT_CMD_FAILURE;// data type mismatch
  14027. }
  14028. if( not_server_variable(*name) ) {
  14029. if( !script_rid2sd(sd) )
  14030. return SCRIPT_CMD_SUCCESS;// no player attached
  14031. }
  14032. while(str[i] != '\0') {
  14033. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  14034. temp[j] = '\0';
  14035. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  14036. j = 0;
  14037. ++i;
  14038. } else {
  14039. temp[j++] = str[i++];
  14040. }
  14041. }
  14042. //set last string
  14043. temp[j] = '\0';
  14044. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  14045. aFree(temp);
  14046. return SCRIPT_CMD_SUCCESS;
  14047. }
  14048. //=======================================================
  14049. // implode <string_array>
  14050. // implode <string_array>, <glue>
  14051. //-------------------------------------------------------
  14052. BUILDIN_FUNC(implode)
  14053. {
  14054. struct script_data* data = script_getdata(st, 2);
  14055. const char *name;
  14056. uint32 glue_len = 0, array_size, id;
  14057. char *output;
  14058. TBL_PC* sd = NULL;
  14059. if( !data_isreference(data) ) {
  14060. ShowError("script:implode: not a variable\n");
  14061. script_reportdata(data);
  14062. st->state = END;
  14063. return SCRIPT_CMD_FAILURE;// not a variable
  14064. }
  14065. id = reference_getid(data);
  14066. name = reference_getname(data);
  14067. if( !is_string_variable(name) ) {
  14068. ShowError("script:implode: not string array\n");
  14069. script_reportdata(data);
  14070. st->state = END;
  14071. return SCRIPT_CMD_FAILURE;// data type mismatch
  14072. }
  14073. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  14074. return SCRIPT_CMD_SUCCESS;// no player attached
  14075. }
  14076. //count chars
  14077. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  14078. if(array_size == -1) { //empty array check (AmsTaff)
  14079. ShowWarning("script:implode: array length = 0\n");
  14080. output = (char*)aMalloc(sizeof(char)*5);
  14081. sprintf(output,"%s","NULL");
  14082. } else {
  14083. const char *glue = NULL, *temp;
  14084. size_t len = 0;
  14085. int i, k = 0;
  14086. for(i = 0; i <= array_size; ++i) {
  14087. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  14088. len += strlen(temp);
  14089. script_removetop(st, -1, 0);
  14090. }
  14091. //allocate mem
  14092. if( script_hasdata(st,3) ) {
  14093. glue = script_getstr(st,3);
  14094. glue_len = strlen(glue);
  14095. len += glue_len * (array_size);
  14096. }
  14097. output = (char*)aMalloc(len + 1);
  14098. //build output
  14099. for(i = 0; i < array_size; ++i) {
  14100. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  14101. len = strlen(temp);
  14102. memcpy(&output[k], temp, len);
  14103. k += len;
  14104. if(glue_len != 0) {
  14105. memcpy(&output[k], glue, glue_len);
  14106. k += glue_len;
  14107. }
  14108. script_removetop(st, -1, 0);
  14109. }
  14110. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  14111. len = strlen(temp);
  14112. memcpy(&output[k], temp, len);
  14113. k += len;
  14114. script_removetop(st, -1, 0);
  14115. output[k] = '\0';
  14116. }
  14117. script_pushstr(st, output);
  14118. return SCRIPT_CMD_SUCCESS;
  14119. }
  14120. //=======================================================
  14121. // sprintf(<format>, ...);
  14122. // Implements C sprintf, except format %n. The resulting string is
  14123. // returned, instead of being saved in variable by reference.
  14124. //-------------------------------------------------------
  14125. BUILDIN_FUNC(sprintf)
  14126. {
  14127. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  14128. const char* format;
  14129. char* p;
  14130. char* q;
  14131. char* buf = NULL;
  14132. char* buf2 = NULL;
  14133. struct script_data* data;
  14134. StringBuf final_buf;
  14135. // Fetch init data
  14136. format = script_getstr(st, 2);
  14137. argc = script_lastdata(st)-2;
  14138. len = strlen(format);
  14139. // Skip parsing, where no parsing is required.
  14140. if(len == 0) {
  14141. script_pushconststr(st,"");
  14142. return SCRIPT_CMD_SUCCESS;
  14143. }
  14144. // Pessimistic alloc
  14145. CREATE(buf, char, len+1);
  14146. // Need not be parsed, just solve stuff like %%.
  14147. if(argc == 0) {
  14148. memcpy(buf,format,len+1);
  14149. script_pushstrcopy(st, buf);
  14150. aFree(buf);
  14151. return SCRIPT_CMD_SUCCESS;
  14152. }
  14153. safestrncpy(buf, format, len+1);
  14154. // Issue sprintf for each parameter
  14155. StringBuf_Init(&final_buf);
  14156. q = buf;
  14157. while((p = strchr(q, '%')) != NULL) {
  14158. if(p != q) {
  14159. len = p - q + 1;
  14160. if(buf2_len < len) {
  14161. RECREATE(buf2, char, len);
  14162. buf2_len = len;
  14163. }
  14164. safestrncpy(buf2, q, len);
  14165. StringBuf_AppendStr(&final_buf, buf2);
  14166. q = p;
  14167. }
  14168. p = q + 1;
  14169. if(*p == '%') { // %%
  14170. StringBuf_AppendStr(&final_buf, "%");
  14171. q+=2;
  14172. continue;
  14173. }
  14174. if(*p == 'n') { // %n
  14175. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  14176. script_reportsrc(st);
  14177. q+=2;
  14178. continue;
  14179. }
  14180. if(arg >= argc) {
  14181. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  14182. if(buf) aFree(buf);
  14183. if(buf2) aFree(buf2);
  14184. StringBuf_Destroy(&final_buf);
  14185. script_pushconststr(st,"");
  14186. return SCRIPT_CMD_FAILURE;
  14187. }
  14188. if((p = strchr(q+1, '%')) == NULL)
  14189. p = strchr(q, 0); // EOS
  14190. len = p - q + 1;
  14191. if(buf2_len < len) {
  14192. RECREATE(buf2, char, len);
  14193. buf2_len = len;
  14194. }
  14195. safestrncpy(buf2, q, len);
  14196. q = p;
  14197. // Note: This assumes the passed value being the correct
  14198. // type to the current format specifier. If not, the server
  14199. // probably crashes or returns anything else, than expected,
  14200. // but it would behave in normal code the same way so it's
  14201. // the scripter's responsibility.
  14202. data = script_getdata(st, arg+3);
  14203. if(data_isstring(data)) // String
  14204. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14205. else if(data_isint(data)) // Number
  14206. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14207. else if(data_isreference(data)) { // Variable
  14208. char* name = reference_getname(data);
  14209. if(name[strlen(name)-1]=='$') // var Str
  14210. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  14211. else // var Int
  14212. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  14213. } else { // Unsupported type
  14214. ShowError("buildin_sprintf: Unknown argument type!\n");
  14215. if(buf) aFree(buf);
  14216. if(buf2) aFree(buf2);
  14217. StringBuf_Destroy(&final_buf);
  14218. script_pushconststr(st,"");
  14219. return SCRIPT_CMD_FAILURE;
  14220. }
  14221. arg++;
  14222. }
  14223. // Append anything left
  14224. if(*q)
  14225. StringBuf_AppendStr(&final_buf, q);
  14226. // Passed more, than needed
  14227. if(arg < argc) {
  14228. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  14229. script_reportsrc(st);
  14230. }
  14231. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  14232. if(buf) aFree(buf);
  14233. if(buf2) aFree(buf2);
  14234. StringBuf_Destroy(&final_buf);
  14235. return SCRIPT_CMD_SUCCESS;
  14236. }
  14237. //=======================================================
  14238. // sscanf(<str>, <format>, ...);
  14239. // Implements C sscanf.
  14240. //-------------------------------------------------------
  14241. BUILDIN_FUNC(sscanf){
  14242. unsigned int argc, arg = 0, len;
  14243. struct script_data* data;
  14244. struct map_session_data* sd = NULL;
  14245. const char* str;
  14246. const char* format;
  14247. const char* p;
  14248. const char* q;
  14249. char* buf = NULL;
  14250. char* buf_p;
  14251. char* ref_str = NULL;
  14252. int ref_int;
  14253. // Get data
  14254. str = script_getstr(st, 2);
  14255. format = script_getstr(st, 3);
  14256. argc = script_lastdata(st)-3;
  14257. len = strlen(format);
  14258. if (len != 0 && strlen(str) == 0) {
  14259. // If the source string is empty but the format string is not, we return -1
  14260. // according to the C specs. (if the format string is also empty, we shall
  14261. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  14262. script_pushint(st, -1);
  14263. return SCRIPT_CMD_SUCCESS;
  14264. }
  14265. CREATE(buf, char, len*2+1);
  14266. // Issue sscanf for each parameter
  14267. *buf = 0;
  14268. q = format;
  14269. while((p = strchr(q, '%'))){
  14270. if(p!=q){
  14271. strncat(buf, q, (size_t)(p-q));
  14272. q = p;
  14273. }
  14274. p = q+1;
  14275. if(*p=='*' || *p=='%'){ // Skip
  14276. strncat(buf, q, 2);
  14277. q+=2;
  14278. continue;
  14279. }
  14280. if(arg>=argc){
  14281. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14282. script_pushint(st, -1);
  14283. if(buf) aFree(buf);
  14284. if(ref_str) aFree(ref_str);
  14285. return SCRIPT_CMD_FAILURE;
  14286. }
  14287. if((p = strchr(q+1, '%'))==NULL){
  14288. p = strchr(q, 0); // EOS
  14289. }
  14290. len = p-q;
  14291. strncat(buf, q, len);
  14292. q = p;
  14293. // Validate output
  14294. data = script_getdata(st, arg+4);
  14295. if(!data_isreference(data) || !reference_tovariable(data)){
  14296. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14297. script_pushint(st, -1);
  14298. if(buf) aFree(buf);
  14299. if(ref_str) aFree(ref_str);
  14300. return SCRIPT_CMD_FAILURE;
  14301. }
  14302. buf_p = reference_getname(data);
  14303. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14304. script_pushint(st, -1);
  14305. if(buf) aFree(buf);
  14306. if(ref_str) aFree(ref_str);
  14307. return SCRIPT_CMD_SUCCESS;
  14308. }
  14309. // Save value if any
  14310. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14311. if(ref_str==NULL){
  14312. CREATE(ref_str, char, strlen(str)+1);
  14313. }
  14314. if(sscanf(str, buf, ref_str)==0){
  14315. break;
  14316. }
  14317. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  14318. } else { // Number
  14319. if(sscanf(str, buf, &ref_int)==0){
  14320. break;
  14321. }
  14322. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  14323. }
  14324. arg++;
  14325. // Disable used format (%... -> %*...)
  14326. buf_p = strchr(buf, 0);
  14327. memmove(buf_p-len+2, buf_p-len+1, len);
  14328. *(buf_p-len+1) = '*';
  14329. }
  14330. script_pushint(st, arg);
  14331. if(buf) aFree(buf);
  14332. if(ref_str) aFree(ref_str);
  14333. return SCRIPT_CMD_SUCCESS;
  14334. }
  14335. //=======================================================
  14336. // strpos(<haystack>, <needle>)
  14337. // strpos(<haystack>, <needle>, <offset>)
  14338. //
  14339. // Implements PHP style strpos. Adapted from code from
  14340. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14341. //-------------------------------------------------------
  14342. BUILDIN_FUNC(strpos) {
  14343. const char *haystack = script_getstr(st,2);
  14344. const char *needle = script_getstr(st,3);
  14345. int i;
  14346. size_t len;
  14347. if( script_hasdata(st,4) )
  14348. i = script_getnum(st,4);
  14349. else
  14350. i = 0;
  14351. if (needle[0] == '\0') {
  14352. script_pushint(st, -1);
  14353. return SCRIPT_CMD_SUCCESS;
  14354. }
  14355. len = strlen(haystack);
  14356. for ( ; i < len; ++i ) {
  14357. if ( haystack[i] == *needle ) {
  14358. // matched starting char -- loop through remaining chars
  14359. const char *h, *n;
  14360. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14361. if ( *h != *n ) {
  14362. break;
  14363. }
  14364. }
  14365. if ( !*n ) { // matched all of 'needle' to null termination
  14366. script_pushint(st, i);
  14367. return SCRIPT_CMD_SUCCESS;
  14368. }
  14369. }
  14370. }
  14371. script_pushint(st, -1);
  14372. return SCRIPT_CMD_SUCCESS;
  14373. }
  14374. //===============================================================
  14375. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14376. //
  14377. // Note: Finds all instances of <search> in <input> and replaces
  14378. // with <replace>. If specified will only replace as many
  14379. // instances as specified in <count>. By default will be case
  14380. // sensitive.
  14381. //---------------------------------------------------------------
  14382. BUILDIN_FUNC(replacestr)
  14383. {
  14384. const char *input = script_getstr(st, 2);
  14385. const char *find = script_getstr(st, 3);
  14386. const char *replace = script_getstr(st, 4);
  14387. size_t inputlen = strlen(input);
  14388. size_t findlen = strlen(find);
  14389. struct StringBuf output;
  14390. bool usecase = true;
  14391. int count = 0;
  14392. int numFinds = 0;
  14393. int i = 0, f = 0;
  14394. if(findlen == 0) {
  14395. ShowError("script:replacestr: Invalid search length.\n");
  14396. st->state = END;
  14397. return SCRIPT_CMD_FAILURE;
  14398. }
  14399. if(script_hasdata(st, 5)) {
  14400. struct script_data *data = script_getdata(st, 5);
  14401. get_val(st, data); // Convert into value in case of a variable
  14402. if( !data_isstring(data) )
  14403. usecase = script_getnum(st, 5) != 0;
  14404. else {
  14405. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14406. st->state = END;
  14407. return SCRIPT_CMD_FAILURE;
  14408. }
  14409. }
  14410. if(script_hasdata(st, 6)) {
  14411. count = script_getnum(st, 6);
  14412. if(count == 0) {
  14413. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14414. st->state = END;
  14415. return SCRIPT_CMD_FAILURE;
  14416. }
  14417. }
  14418. StringBuf_Init(&output);
  14419. for(; i < inputlen; i++) {
  14420. if(count && count == numFinds) { //found enough, stop looking
  14421. break;
  14422. }
  14423. for(f = 0; f <= findlen; f++) {
  14424. if(f == findlen) { //complete match
  14425. numFinds++;
  14426. StringBuf_AppendStr(&output, replace);
  14427. i += findlen - 1;
  14428. break;
  14429. } else {
  14430. if(usecase) {
  14431. if((i + f) > inputlen || input[i + f] != find[f]) {
  14432. StringBuf_Printf(&output, "%c", input[i]);
  14433. break;
  14434. }
  14435. } else {
  14436. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14437. StringBuf_Printf(&output, "%c", input[i]);
  14438. break;
  14439. }
  14440. }
  14441. }
  14442. }
  14443. }
  14444. //append excess after enough found
  14445. if(i < inputlen)
  14446. StringBuf_AppendStr(&output, &(input[i]));
  14447. script_pushstrcopy(st, StringBuf_Value(&output));
  14448. StringBuf_Destroy(&output);
  14449. return SCRIPT_CMD_SUCCESS;
  14450. }
  14451. //========================================================
  14452. // countstr <input>, <search>{, <usecase>}
  14453. //
  14454. // Note: Counts the number of times <search> occurs in
  14455. // <input>. By default will be case sensitive.
  14456. //--------------------------------------------------------
  14457. BUILDIN_FUNC(countstr)
  14458. {
  14459. const char *input = script_getstr(st, 2);
  14460. const char *find = script_getstr(st, 3);
  14461. size_t inputlen = strlen(input);
  14462. size_t findlen = strlen(find);
  14463. bool usecase = true;
  14464. int numFinds = 0;
  14465. int i = 0, f = 0;
  14466. if(findlen == 0) {
  14467. ShowError("script:countstr: Invalid search length.\n");
  14468. st->state = END;
  14469. return SCRIPT_CMD_FAILURE;
  14470. }
  14471. if(script_hasdata(st, 4)) {
  14472. struct script_data *data;
  14473. data = script_getdata(st, 4);
  14474. get_val(st, data); // Convert into value in case of a variable
  14475. if( !data_isstring(data) )
  14476. usecase = script_getnum(st, 4) != 0;
  14477. else {
  14478. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14479. st->state = END;
  14480. return SCRIPT_CMD_FAILURE;
  14481. }
  14482. }
  14483. for(; i < inputlen; i++) {
  14484. for(f = 0; f <= findlen; f++) {
  14485. if(f == findlen) { //complete match
  14486. numFinds++;
  14487. i += findlen - 1;
  14488. break;
  14489. } else {
  14490. if(usecase) {
  14491. if((i + f) > inputlen || input[i + f] != find[f]) {
  14492. break;
  14493. }
  14494. } else {
  14495. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14496. break;
  14497. }
  14498. }
  14499. }
  14500. }
  14501. }
  14502. script_pushint(st, numFinds);
  14503. return SCRIPT_CMD_SUCCESS;
  14504. }
  14505. /// Changes the display name and/or display class of the npc.
  14506. /// Returns 0 is successful, 1 if the npc does not exist.
  14507. ///
  14508. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14509. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14510. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14511. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14512. BUILDIN_FUNC(setnpcdisplay)
  14513. {
  14514. const char* name;
  14515. const char* newname = NULL;
  14516. int class_ = JT_FAKENPC, size = -1;
  14517. struct script_data* data;
  14518. struct npc_data* nd;
  14519. name = script_getstr(st,2);
  14520. data = script_getdata(st,3);
  14521. if( script_hasdata(st,4) )
  14522. class_ = script_getnum(st,4);
  14523. if( script_hasdata(st,5) )
  14524. size = script_getnum(st,5);
  14525. get_val(st, data);
  14526. if( data_isstring(data) )
  14527. newname = conv_str(st,data);
  14528. else if( data_isint(data) )
  14529. class_ = conv_num(st,data);
  14530. else
  14531. {
  14532. ShowError("script:setnpcdisplay: expected string or number\n");
  14533. script_reportdata(data);
  14534. return SCRIPT_CMD_FAILURE;
  14535. }
  14536. nd = npc_name2id(name);
  14537. if( nd == NULL )
  14538. {// not found
  14539. script_pushint(st,1);
  14540. return SCRIPT_CMD_SUCCESS;
  14541. }
  14542. // update npc
  14543. if( newname )
  14544. npc_setdisplayname(nd, newname);
  14545. if( size != -1 && size != (int)nd->size )
  14546. nd->size = size;
  14547. else
  14548. size = -1;
  14549. if( class_ != JT_FAKENPC && nd->class_ != class_ )
  14550. npc_setclass(nd, class_);
  14551. else if( size != -1 )
  14552. { // Required to update the visual size
  14553. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  14554. clif_spawn(&nd->bl);
  14555. }
  14556. script_pushint(st,0);
  14557. return SCRIPT_CMD_SUCCESS;
  14558. }
  14559. BUILDIN_FUNC(atoi)
  14560. {
  14561. const char *value;
  14562. value = script_getstr(st,2);
  14563. script_pushint(st,atoi(value));
  14564. return SCRIPT_CMD_SUCCESS;
  14565. }
  14566. BUILDIN_FUNC(axtoi)
  14567. {
  14568. const char *hex = script_getstr(st,2);
  14569. long value = strtol(hex, NULL, 16);
  14570. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14571. value = cap_value(value, INT_MIN, INT_MAX);
  14572. #endif
  14573. script_pushint(st, (int)value);
  14574. return SCRIPT_CMD_SUCCESS;
  14575. }
  14576. BUILDIN_FUNC(strtol)
  14577. {
  14578. const char *string = script_getstr(st, 2);
  14579. int base = script_getnum(st, 3);
  14580. long value = strtol(string, NULL, base);
  14581. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14582. value = cap_value(value, INT_MIN, INT_MAX);
  14583. #endif
  14584. script_pushint(st, (int)value);
  14585. return SCRIPT_CMD_SUCCESS;
  14586. }
  14587. // case-insensitive substring search [lordalfa]
  14588. BUILDIN_FUNC(compare)
  14589. {
  14590. const char *message;
  14591. const char *cmpstring;
  14592. message = script_getstr(st,2);
  14593. cmpstring = script_getstr(st,3);
  14594. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14595. return SCRIPT_CMD_SUCCESS;
  14596. }
  14597. BUILDIN_FUNC(strcmp)
  14598. {
  14599. const char *str1;
  14600. const char *str2;
  14601. str1 = script_getstr(st,2);
  14602. str2 = script_getstr(st,3);
  14603. script_pushint(st,strcmp(str1, str2));
  14604. return SCRIPT_CMD_SUCCESS;
  14605. }
  14606. // [zBuffer] List of mathematics commands --->
  14607. BUILDIN_FUNC(sqrt)
  14608. {
  14609. double i, a;
  14610. i = script_getnum(st,2);
  14611. a = sqrt(i);
  14612. script_pushint(st,(int)a);
  14613. return SCRIPT_CMD_SUCCESS;
  14614. }
  14615. BUILDIN_FUNC(pow)
  14616. {
  14617. double i, a, b;
  14618. a = script_getnum(st,2);
  14619. b = script_getnum(st,3);
  14620. i = pow(a,b);
  14621. script_pushint(st,(int)i);
  14622. return SCRIPT_CMD_SUCCESS;
  14623. }
  14624. BUILDIN_FUNC(distance)
  14625. {
  14626. int x0, y0, x1, y1;
  14627. x0 = script_getnum(st,2);
  14628. y0 = script_getnum(st,3);
  14629. x1 = script_getnum(st,4);
  14630. y1 = script_getnum(st,5);
  14631. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14632. return SCRIPT_CMD_SUCCESS;
  14633. }
  14634. // <--- [zBuffer] List of mathematics commands
  14635. BUILDIN_FUNC(md5)
  14636. {
  14637. const char *tmpstr;
  14638. char *md5str;
  14639. tmpstr = script_getstr(st,2);
  14640. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14641. MD5_String(tmpstr, md5str);
  14642. script_pushstr(st, md5str);
  14643. return SCRIPT_CMD_SUCCESS;
  14644. }
  14645. // [zBuffer] List of dynamic var commands --->
  14646. /**
  14647. * setd "<variable name>",<value>{,<char_id>};
  14648. **/
  14649. BUILDIN_FUNC(setd)
  14650. {
  14651. TBL_PC *sd = NULL;
  14652. char varname[100];
  14653. const char *buffer;
  14654. int elem;
  14655. buffer = script_getstr(st, 2);
  14656. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14657. elem = 0;
  14658. if( not_server_variable(*varname) ) {
  14659. if (!script_charid2sd(4,sd))
  14660. return SCRIPT_CMD_FAILURE;
  14661. }
  14662. if( is_string_variable(varname) ) {
  14663. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14664. } else {
  14665. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14666. }
  14667. return SCRIPT_CMD_SUCCESS;
  14668. }
  14669. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14670. {
  14671. int i, j;
  14672. TBL_PC* sd = NULL;
  14673. const char* query;
  14674. struct script_data* data;
  14675. const char* name;
  14676. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14677. int num_vars;
  14678. int num_cols;
  14679. // check target variables
  14680. for( i = 3; script_hasdata(st,i); ++i ) {
  14681. data = script_getdata(st, i);
  14682. if( data_isreference(data) ) { // it's a variable
  14683. name = reference_getname(data);
  14684. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14685. if( !script_rid2sd(sd) ) { // no player attached
  14686. script_reportdata(data);
  14687. st->state = END;
  14688. return SCRIPT_CMD_FAILURE;
  14689. }
  14690. }
  14691. } else {
  14692. ShowError("script:query_sql: not a variable\n");
  14693. script_reportdata(data);
  14694. st->state = END;
  14695. return SCRIPT_CMD_FAILURE;
  14696. }
  14697. }
  14698. num_vars = i - 3;
  14699. // Execute the query
  14700. query = script_getstr(st,2);
  14701. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14702. Sql_ShowDebug(handle);
  14703. script_pushint(st, -1);
  14704. return SCRIPT_CMD_FAILURE;
  14705. }
  14706. if( Sql_NumRows(handle) == 0 ) { // No data received
  14707. Sql_FreeResult(handle);
  14708. script_pushint(st, 0);
  14709. return SCRIPT_CMD_SUCCESS;
  14710. }
  14711. // Count the number of columns to store
  14712. num_cols = Sql_NumColumns(handle);
  14713. if( num_vars < num_cols ) {
  14714. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14715. script_reportsrc(st);
  14716. } else if( num_vars > num_cols ) {
  14717. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14718. script_reportsrc(st);
  14719. }
  14720. // Store data
  14721. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14722. for( j = 0; j < num_vars; ++j ) {
  14723. char* str = NULL;
  14724. if( j < num_cols )
  14725. Sql_GetData(handle, j, &str, NULL);
  14726. data = script_getdata(st, j+3);
  14727. name = reference_getname(data);
  14728. if( is_string_variable(name) )
  14729. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14730. else
  14731. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14732. }
  14733. }
  14734. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14735. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14736. script_reportsrc(st);
  14737. }
  14738. // Free data
  14739. Sql_FreeResult(handle);
  14740. script_pushint(st, i);
  14741. return SCRIPT_CMD_SUCCESS;
  14742. }
  14743. BUILDIN_FUNC(query_sql) {
  14744. #ifdef BETA_THREAD_TEST
  14745. if( st->state != RERUNLINE ) {
  14746. queryThread_add(st,false);
  14747. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14748. } else
  14749. st->state = RUN;
  14750. return SCRIPT_CMD_SUCCESS;
  14751. #else
  14752. return buildin_query_sql_sub(st, qsmysql_handle);
  14753. #endif
  14754. }
  14755. BUILDIN_FUNC(query_logsql) {
  14756. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14757. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14758. script_pushint(st,-1);
  14759. return SCRIPT_CMD_FAILURE;
  14760. }
  14761. #ifdef BETA_THREAD_TEST
  14762. if( st->state != RERUNLINE ) {
  14763. queryThread_add(st,true);
  14764. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14765. } else
  14766. st->state = RUN;
  14767. return SCRIPT_CMD_SUCCESS;
  14768. #else
  14769. return buildin_query_sql_sub(st, logmysql_handle);
  14770. #endif
  14771. }
  14772. //Allows escaping of a given string.
  14773. BUILDIN_FUNC(escape_sql)
  14774. {
  14775. const char *str;
  14776. char *esc_str;
  14777. size_t len;
  14778. str = script_getstr(st,2);
  14779. len = strlen(str);
  14780. esc_str = (char*)aMalloc(len*2+1);
  14781. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14782. script_pushstr(st, esc_str);
  14783. return SCRIPT_CMD_SUCCESS;
  14784. }
  14785. BUILDIN_FUNC(getd)
  14786. {
  14787. char varname[100];
  14788. const char *buffer;
  14789. int elem;
  14790. buffer = script_getstr(st, 2);
  14791. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14792. elem = 0;
  14793. // Push the 'pointer' so it's more flexible [Lance]
  14794. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14795. return SCRIPT_CMD_SUCCESS;
  14796. }
  14797. BUILDIN_FUNC(callshop)
  14798. {
  14799. TBL_PC *sd = NULL;
  14800. struct npc_data *nd;
  14801. const char *shopname;
  14802. int flag = 0;
  14803. if (!script_rid2sd(sd)) {
  14804. script_pushint(st,0);
  14805. return SCRIPT_CMD_SUCCESS;
  14806. }
  14807. shopname = script_getstr(st, 2);
  14808. if (script_hasdata(st,3))
  14809. flag = script_getnum(st,3);
  14810. nd = npc_name2id(shopname);
  14811. 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) ) {
  14812. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14813. script_pushint(st,0);
  14814. return SCRIPT_CMD_FAILURE;
  14815. }
  14816. if (nd->subtype == NPCTYPE_SHOP) {
  14817. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14818. sd->state.callshop = 1;
  14819. switch (flag) {
  14820. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14821. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14822. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14823. }
  14824. }
  14825. #if PACKETVER >= 20131223
  14826. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14827. unsigned short i;
  14828. for (i = 0; i < nd->u.shop.count; i++) {
  14829. if (nd->u.shop.shop_item[i].qty)
  14830. break;
  14831. }
  14832. if (i == nd->u.shop.count) {
  14833. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14834. return SCRIPT_CMD_FAILURE;
  14835. }
  14836. sd->npc_shopid = nd->bl.id;
  14837. clif_npc_market_open(sd, nd);
  14838. script_pushint(st,1);
  14839. return SCRIPT_CMD_SUCCESS;
  14840. }
  14841. #endif
  14842. else
  14843. clif_cashshop_show(sd, nd);
  14844. sd->npc_shopid = nd->bl.id;
  14845. script_pushint(st,1);
  14846. return SCRIPT_CMD_SUCCESS;
  14847. }
  14848. BUILDIN_FUNC(npcshopitem)
  14849. {
  14850. const char* npcname = script_getstr(st, 2);
  14851. struct npc_data* nd = npc_name2id(npcname);
  14852. int n, i;
  14853. int amount;
  14854. uint16 offs = 2;
  14855. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14856. script_pushint(st,0);
  14857. return SCRIPT_CMD_SUCCESS;
  14858. }
  14859. #if PACKETVER >= 20131223
  14860. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14861. offs = 3;
  14862. npc_market_delfromsql_(nd->exname, 0, true);
  14863. }
  14864. #endif
  14865. // get the count of new entries
  14866. amount = (script_lastdata(st)-2)/offs;
  14867. // generate new shop item list
  14868. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14869. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14870. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14871. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14872. #if PACKETVER >= 20131223
  14873. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14874. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14875. nd->u.shop.shop_item[n].flag = 1;
  14876. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14877. }
  14878. #endif
  14879. }
  14880. nd->u.shop.count = n;
  14881. script_pushint(st,1);
  14882. return SCRIPT_CMD_SUCCESS;
  14883. }
  14884. BUILDIN_FUNC(npcshopadditem)
  14885. {
  14886. const char* npcname = script_getstr(st,2);
  14887. struct npc_data* nd = npc_name2id(npcname);
  14888. int n, i;
  14889. uint16 offs = 2, amount;
  14890. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14891. script_pushint(st,0);
  14892. return SCRIPT_CMD_SUCCESS;
  14893. }
  14894. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14895. offs = 3;
  14896. // get the count of new entries
  14897. amount = (script_lastdata(st)-2)/offs;
  14898. #if PACKETVER >= 20131223
  14899. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14900. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14901. uint16 nameid = script_getnum(st,i), j;
  14902. // Check existing entries
  14903. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14904. if (j == nd->u.shop.count) {
  14905. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14906. j = nd->u.shop.count;
  14907. nd->u.shop.shop_item[j].flag = 1;
  14908. nd->u.shop.count++;
  14909. }
  14910. nd->u.shop.shop_item[j].nameid = nameid;
  14911. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14912. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14913. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14914. }
  14915. script_pushint(st,1);
  14916. return SCRIPT_CMD_SUCCESS;
  14917. }
  14918. #endif
  14919. // append new items to existing shop item list
  14920. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14921. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14922. {
  14923. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14924. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14925. }
  14926. nd->u.shop.count = n;
  14927. script_pushint(st,1);
  14928. return SCRIPT_CMD_SUCCESS;
  14929. }
  14930. BUILDIN_FUNC(npcshopdelitem)
  14931. {
  14932. const char* npcname = script_getstr(st,2);
  14933. struct npc_data* nd = npc_name2id(npcname);
  14934. int n, i, size;
  14935. unsigned short amount;
  14936. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14937. script_pushint(st,0);
  14938. return SCRIPT_CMD_SUCCESS;
  14939. }
  14940. amount = script_lastdata(st)-2;
  14941. size = nd->u.shop.count;
  14942. // remove specified items from the shop item list
  14943. for( i = 3; i < 3 + amount; i++ ) {
  14944. unsigned short nameid = script_getnum(st,i);
  14945. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14946. if( n < size ) {
  14947. if (n+1 != size)
  14948. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14949. #if PACKETVER >= 20131223
  14950. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14951. npc_market_delfromsql_(nd->exname, nameid, false);
  14952. #endif
  14953. size--;
  14954. }
  14955. }
  14956. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14957. nd->u.shop.count = size;
  14958. script_pushint(st,1);
  14959. return SCRIPT_CMD_SUCCESS;
  14960. }
  14961. /**
  14962. * Sets a script to attach to a shop npc.
  14963. * npcshopattach "<npc_name>";
  14964. **/
  14965. BUILDIN_FUNC(npcshopattach)
  14966. {
  14967. const char* npcname = script_getstr(st,2);
  14968. struct npc_data* nd = npc_name2id(npcname);
  14969. int flag = 1;
  14970. if( script_hasdata(st,3) )
  14971. flag = script_getnum(st,3);
  14972. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14973. script_pushint(st,0);
  14974. return SCRIPT_CMD_SUCCESS;
  14975. }
  14976. if (flag)
  14977. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14978. else
  14979. nd->master_nd = NULL;
  14980. script_pushint(st,1);
  14981. return SCRIPT_CMD_SUCCESS;
  14982. }
  14983. /*==========================================
  14984. * Returns some values of an item [Lupus]
  14985. * Price, Weight, etc...
  14986. setitemscript(itemID,"{new item bonus script}",[n]);
  14987. Where n:
  14988. 0 - script
  14989. 1 - Equip script
  14990. 2 - Unequip script
  14991. *------------------------------------------*/
  14992. BUILDIN_FUNC(setitemscript)
  14993. {
  14994. unsigned short item_id;
  14995. int n = 0;
  14996. const char *script;
  14997. struct item_data *i_data;
  14998. struct script_code **dstscript;
  14999. item_id = script_getnum(st,2);
  15000. script = script_getstr(st,3);
  15001. if( script_hasdata(st,4) )
  15002. n=script_getnum(st,4);
  15003. i_data = itemdb_exists(item_id);
  15004. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  15005. script_pushint(st,0);
  15006. return SCRIPT_CMD_SUCCESS;
  15007. }
  15008. switch (n) {
  15009. case 2:
  15010. dstscript = &i_data->unequip_script;
  15011. break;
  15012. case 1:
  15013. dstscript = &i_data->equip_script;
  15014. break;
  15015. default:
  15016. dstscript = &i_data->script;
  15017. break;
  15018. }
  15019. if(*dstscript)
  15020. script_free_code(*dstscript);
  15021. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  15022. script_pushint(st,1);
  15023. return SCRIPT_CMD_SUCCESS;
  15024. }
  15025. /*=======================================================
  15026. * Add or Update a mob drop temporarily [Akinari]
  15027. * Original Idea By: [Lupus]
  15028. *
  15029. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  15030. *
  15031. * If given an item the mob already drops, the rate
  15032. * is updated to the new rate. Rate cannot exceed 10000
  15033. * Returns 1 if succeeded (added/updated a mob drop)
  15034. *-------------------------------------------------------*/
  15035. BUILDIN_FUNC(addmonsterdrop)
  15036. {
  15037. struct mob_db *mob;
  15038. struct script_data *data;
  15039. unsigned short item_id;
  15040. int rate;
  15041. data = script_getdata(st, 2);
  15042. get_val(st, data); // Convert into value in case of a variable
  15043. if(data_isstring(data))
  15044. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  15045. else
  15046. mob = mob_db(script_getnum(st,2));
  15047. item_id=script_getnum(st,3);
  15048. rate=script_getnum(st,4);
  15049. if(!itemdb_exists(item_id)){
  15050. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  15051. return SCRIPT_CMD_FAILURE;
  15052. }
  15053. if(mob) { //We got a valid monster, check for available drop slot
  15054. unsigned char i, c = 0;
  15055. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15056. if(mob->dropitem[i].nameid) {
  15057. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  15058. c = i;
  15059. break;
  15060. }
  15061. continue;
  15062. }
  15063. if(!c) //Accept first available slot only
  15064. c = i;
  15065. }
  15066. if(c) { //Fill in the slot with the item and rate
  15067. mob->dropitem[c].nameid = item_id;
  15068. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  15069. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15070. script_pushint(st,1);
  15071. } else //No place to put the new drop
  15072. script_pushint(st,0);
  15073. } else {
  15074. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15075. return SCRIPT_CMD_FAILURE;
  15076. }
  15077. return SCRIPT_CMD_SUCCESS;
  15078. }
  15079. /*=======================================================
  15080. * Delete a mob drop temporarily [Akinari]
  15081. * Original Idea By: [Lupus]
  15082. *
  15083. * delmonsterdrop <mob_id or name>,<item_id>;
  15084. *
  15085. * Returns 1 if succeeded (deleted a mob drop)
  15086. *-------------------------------------------------------*/
  15087. BUILDIN_FUNC(delmonsterdrop)
  15088. {
  15089. struct mob_db *mob;
  15090. struct script_data *data;
  15091. unsigned short item_id;
  15092. data = script_getdata(st, 2);
  15093. get_val(st, data); // Convert into value in case of a variable
  15094. if(data_isstring(data))
  15095. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  15096. else
  15097. mob = mob_db(script_getnum(st,2));
  15098. item_id=script_getnum(st,3);
  15099. if(!itemdb_exists(item_id)){
  15100. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  15101. return SCRIPT_CMD_FAILURE;
  15102. }
  15103. if(mob) { //We got a valid monster, check for item drop on monster
  15104. unsigned char i;
  15105. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  15106. if(mob->dropitem[i].nameid == item_id) {
  15107. mob->dropitem[i].nameid = 0;
  15108. mob->dropitem[i].p = 0;
  15109. mob_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  15110. script_pushint(st,1);
  15111. return SCRIPT_CMD_SUCCESS;
  15112. }
  15113. }
  15114. //No drop on that monster
  15115. script_pushint(st,0);
  15116. } else {
  15117. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  15118. return SCRIPT_CMD_FAILURE;
  15119. }
  15120. return SCRIPT_CMD_SUCCESS;
  15121. }
  15122. /*==========================================
  15123. * Returns some values of a monster [Lupus]
  15124. * Name, Level, race, size, etc...
  15125. getmonsterinfo(monsterID,queryIndex);
  15126. *------------------------------------------*/
  15127. BUILDIN_FUNC(getmonsterinfo)
  15128. {
  15129. struct mob_db *mob;
  15130. int mob_id;
  15131. mob_id = script_getnum(st,2);
  15132. if (!mobdb_checkid(mob_id)) {
  15133. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  15134. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  15135. script_pushconststr(st,"null");
  15136. else
  15137. script_pushint(st,-1);
  15138. return SCRIPT_CMD_SUCCESS;
  15139. }
  15140. mob = mob_db(mob_id);
  15141. switch ( script_getnum(st,3) ) {
  15142. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  15143. case MOB_LV: script_pushint(st,mob->lv); break;
  15144. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  15145. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  15146. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  15147. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  15148. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  15149. case MOB_DEF: script_pushint(st,mob->status.def); break;
  15150. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  15151. case MOB_STR: script_pushint(st,mob->status.str); break;
  15152. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  15153. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  15154. case MOB_INT: script_pushint(st,mob->status.int_); break;
  15155. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  15156. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  15157. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  15158. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  15159. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  15160. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  15161. case MOB_RACE: script_pushint(st,mob->status.race); break;
  15162. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  15163. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  15164. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  15165. default: script_pushint(st,-1); //wrong Index
  15166. }
  15167. return SCRIPT_CMD_SUCCESS;
  15168. }
  15169. /**
  15170. * Check player's vending/buyingstore/autotrade state
  15171. * checkvending({"<Player Name>"})
  15172. * @author [Nab4]
  15173. */
  15174. BUILDIN_FUNC(checkvending) {
  15175. TBL_PC *sd = NULL;
  15176. if (!script_nick2sd(2,sd) ) {
  15177. script_pushint(st,0);
  15178. return SCRIPT_CMD_SUCCESS;
  15179. }
  15180. else {
  15181. int8 ret = 0;
  15182. if (sd->state.vending)
  15183. ret = 1;
  15184. else if (sd->state.buyingstore)
  15185. ret = 4;
  15186. if (sd->state.autotrade)
  15187. ret |= 2;
  15188. script_pushint(st, ret);
  15189. }
  15190. return SCRIPT_CMD_SUCCESS;
  15191. }
  15192. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  15193. {
  15194. TBL_PC *sd = NULL;
  15195. if( script_nick2sd(2,sd) )
  15196. script_pushint(st,(sd->chatID != 0));
  15197. else
  15198. script_pushint(st,0);
  15199. return SCRIPT_CMD_SUCCESS;
  15200. }
  15201. BUILDIN_FUNC(checkidle)
  15202. {
  15203. TBL_PC *sd = NULL;
  15204. if( script_nick2sd(2,sd) )
  15205. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  15206. else
  15207. script_pushint(st, 0);
  15208. return SCRIPT_CMD_SUCCESS;
  15209. }
  15210. BUILDIN_FUNC(searchitem)
  15211. {
  15212. struct script_data* data = script_getdata(st, 2);
  15213. const char *itemname = script_getstr(st,3);
  15214. struct item_data *items[MAX_SEARCH];
  15215. int count;
  15216. char* name;
  15217. int32 start;
  15218. int32 id;
  15219. int32 i;
  15220. TBL_PC* sd = NULL;
  15221. if ((items[0] = itemdb_exists(atoi(itemname))))
  15222. count = 1;
  15223. else
  15224. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  15225. if (!count) {
  15226. script_pushint(st, 0);
  15227. return SCRIPT_CMD_SUCCESS;
  15228. }
  15229. if( !data_isreference(data) )
  15230. {
  15231. ShowError("script:searchitem: not a variable\n");
  15232. script_reportdata(data);
  15233. st->state = END;
  15234. return SCRIPT_CMD_FAILURE;// not a variable
  15235. }
  15236. id = reference_getid(data);
  15237. start = reference_getindex(data);
  15238. name = reference_getname(data);
  15239. if( not_server_variable(*name) && !script_rid2sd(sd) )
  15240. {
  15241. return SCRIPT_CMD_SUCCESS;// no player attached
  15242. }
  15243. if( is_string_variable(name) )
  15244. {// string array
  15245. ShowError("script:searchitem: not an integer array reference\n");
  15246. script_reportdata(data);
  15247. st->state = END;
  15248. return SCRIPT_CMD_FAILURE;// not supported
  15249. }
  15250. for( i = 0; i < count; ++start, ++i )
  15251. {// Set array
  15252. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  15253. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  15254. }
  15255. script_pushint(st, count);
  15256. return SCRIPT_CMD_SUCCESS;
  15257. }
  15258. // [zBuffer] List of player cont commands --->
  15259. BUILDIN_FUNC(rid2name)
  15260. {
  15261. struct block_list *bl = NULL;
  15262. int rid = script_getnum(st,2);
  15263. if((bl = map_id2bl(rid)))
  15264. {
  15265. switch(bl->type) {
  15266. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15267. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15268. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15269. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15270. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15271. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  15272. default:
  15273. ShowError("buildin_rid2name: BL type unknown.\n");
  15274. script_pushconststr(st,"");
  15275. break;
  15276. }
  15277. } else {
  15278. ShowError("buildin_rid2name: invalid RID\n");
  15279. script_pushconststr(st,"(null)");
  15280. }
  15281. return SCRIPT_CMD_SUCCESS;
  15282. }
  15283. /**
  15284. * Toggle a unit from moving.
  15285. * pcblockmove(<unit_id>,<option>);
  15286. */
  15287. BUILDIN_FUNC(pcblockmove)
  15288. {
  15289. struct block_list *bl = NULL;
  15290. if (script_getnum(st, 2))
  15291. bl = map_id2bl(script_getnum(st,2));
  15292. else
  15293. bl = map_id2bl(st->rid);
  15294. if (bl) {
  15295. struct unit_data *ud = unit_bl2ud(bl);
  15296. if (ud)
  15297. ud->state.blockedmove = script_getnum(st,3) > 0;
  15298. }
  15299. return SCRIPT_CMD_SUCCESS;
  15300. }
  15301. /**
  15302. * Toggle a unit from casting skills.
  15303. * pcblockskill(<unit_id>,<option>);
  15304. */
  15305. BUILDIN_FUNC(pcblockskill)
  15306. {
  15307. struct block_list *bl = NULL;
  15308. if (script_getnum(st, 2))
  15309. bl = map_id2bl(script_getnum(st,2));
  15310. else
  15311. bl = map_id2bl(st->rid);
  15312. if (bl) {
  15313. struct unit_data *ud = unit_bl2ud(bl);
  15314. if (ud)
  15315. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15316. }
  15317. return SCRIPT_CMD_SUCCESS;
  15318. }
  15319. BUILDIN_FUNC(pcfollow)
  15320. {
  15321. TBL_PC *sd;
  15322. if( script_mapid2sd(2,sd) )
  15323. pc_follow(sd, script_getnum(st,3));
  15324. return SCRIPT_CMD_SUCCESS;
  15325. }
  15326. BUILDIN_FUNC(pcstopfollow)
  15327. {
  15328. TBL_PC *sd;
  15329. if(script_mapid2sd(2,sd))
  15330. pc_stop_following(sd);
  15331. return SCRIPT_CMD_SUCCESS;
  15332. }
  15333. // <--- [zBuffer] List of player cont commands
  15334. // [zBuffer] List of unit control commands --->
  15335. /// Checks to see if the unit exists.
  15336. ///
  15337. /// unitexists <unit id>;
  15338. BUILDIN_FUNC(unitexists)
  15339. {
  15340. struct block_list* bl;
  15341. bl = map_id2bl(script_getnum(st, 2));
  15342. if (!bl)
  15343. script_pushint(st, false);
  15344. else
  15345. script_pushint(st, true);
  15346. return SCRIPT_CMD_SUCCESS;
  15347. }
  15348. /// Gets the type of the given Game ID.
  15349. ///
  15350. /// getunittype <unit id>;
  15351. BUILDIN_FUNC(getunittype)
  15352. {
  15353. struct block_list* bl;
  15354. uint8 value = 0;
  15355. if(!script_rid2bl(2,bl))
  15356. {
  15357. script_pushint(st, -1);
  15358. return SCRIPT_CMD_FAILURE;
  15359. }
  15360. switch (bl->type) {
  15361. case BL_PC: value = UNITTYPE_PC; break;
  15362. case BL_NPC: value = UNITTYPE_NPC; break;
  15363. case BL_PET: value = UNITTYPE_PET; break;
  15364. case BL_MOB: value = UNITTYPE_MOB; break;
  15365. case BL_HOM: value = UNITTYPE_HOM; break;
  15366. case BL_MER: value = UNITTYPE_MER; break;
  15367. case BL_ELEM: value = UNITTYPE_ELEM; break;
  15368. default: value = -1; break;
  15369. }
  15370. script_pushint(st, value);
  15371. return SCRIPT_CMD_SUCCESS;
  15372. }
  15373. /// Gets specific live information of a bl.
  15374. ///
  15375. /// getunitdata <unit id>,<arrayname>;
  15376. BUILDIN_FUNC(getunitdata)
  15377. {
  15378. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15379. struct block_list* bl;
  15380. TBL_MOB* md = NULL;
  15381. TBL_HOM* hd = NULL;
  15382. TBL_MER* mc = NULL;
  15383. TBL_PET* pd = NULL;
  15384. TBL_ELEM* ed = NULL;
  15385. TBL_NPC* nd = NULL;
  15386. char* name;
  15387. struct script_data *data = script_getdata(st, 3);
  15388. if (!data_isreference(data)) {
  15389. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15390. return SCRIPT_CMD_FAILURE;
  15391. }
  15392. if(!script_rid2bl(2,bl))
  15393. {
  15394. script_pushint(st, -1);
  15395. return SCRIPT_CMD_FAILURE;
  15396. }
  15397. switch (bl->type) {
  15398. case BL_MOB: md = map_id2md(bl->id); break;
  15399. case BL_HOM: hd = map_id2hd(bl->id); break;
  15400. case BL_PET: pd = map_id2pd(bl->id); break;
  15401. case BL_MER: mc = map_id2mc(bl->id); break;
  15402. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15403. case BL_NPC: nd = map_id2nd(bl->id); break;
  15404. default:
  15405. ShowWarning("buildin_getunitdata: Invalid object type!\n");
  15406. return SCRIPT_CMD_FAILURE;
  15407. }
  15408. name = reference_getname(data);
  15409. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  15410. switch(bl->type) {
  15411. case BL_MOB:
  15412. if (!md) {
  15413. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  15414. return SCRIPT_CMD_FAILURE;
  15415. }
  15416. getunitdata_sub(UMOB_SIZE, md->status.size);
  15417. getunitdata_sub(UMOB_LEVEL, md->level);
  15418. getunitdata_sub(UMOB_HP, md->status.hp);
  15419. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  15420. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  15421. getunitdata_sub(UMOB_MAPID, md->bl.m);
  15422. getunitdata_sub(UMOB_X, md->bl.x);
  15423. getunitdata_sub(UMOB_Y, md->bl.y);
  15424. getunitdata_sub(UMOB_SPEED, md->status.speed);
  15425. getunitdata_sub(UMOB_MODE, md->status.mode);
  15426. getunitdata_sub(UMOB_AI, md->special_state.ai);
  15427. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  15428. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15429. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15430. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15431. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15432. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15433. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15434. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15435. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15436. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15437. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15438. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15439. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15440. getunitdata_sub(UMOB_STR, md->status.str);
  15441. getunitdata_sub(UMOB_AGI, md->status.agi);
  15442. getunitdata_sub(UMOB_VIT, md->status.vit);
  15443. getunitdata_sub(UMOB_INT, md->status.int_);
  15444. getunitdata_sub(UMOB_DEX, md->status.dex);
  15445. getunitdata_sub(UMOB_LUK, md->status.luk);
  15446. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15447. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15448. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15449. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15450. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15451. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15452. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15453. getunitdata_sub(UMOB_DEF, md->status.def);
  15454. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15455. getunitdata_sub(UMOB_HIT, md->status.hit);
  15456. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15457. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15458. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15459. getunitdata_sub(UMOB_RACE, md->status.race);
  15460. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15461. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15462. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15463. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15464. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15465. getunitdata_sub(UMOB_TARGETID, md->target_id);
  15466. break;
  15467. case BL_HOM:
  15468. if (!hd) {
  15469. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15470. return SCRIPT_CMD_FAILURE;
  15471. }
  15472. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15473. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15474. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15475. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15476. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15477. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15478. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15479. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15480. getunitdata_sub(UHOM_X, hd->bl.x);
  15481. getunitdata_sub(UHOM_Y, hd->bl.y);
  15482. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15483. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15484. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15485. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15486. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15487. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15488. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15489. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15490. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15491. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15492. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15493. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15494. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15495. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15496. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15497. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15498. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15499. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15500. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15501. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15502. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15503. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15504. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15505. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15506. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15507. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15508. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15509. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15510. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15511. getunitdata_sub(UHOM_TARGETID, hd->ud.target);
  15512. break;
  15513. case BL_PET:
  15514. if (!pd) {
  15515. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15516. return SCRIPT_CMD_FAILURE;
  15517. }
  15518. getunitdata_sub(UPET_SIZE, pd->status.size);
  15519. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15520. getunitdata_sub(UPET_HP, pd->status.hp);
  15521. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15522. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15523. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15524. getunitdata_sub(UPET_X, pd->bl.x);
  15525. getunitdata_sub(UPET_Y, pd->bl.y);
  15526. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15527. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15528. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15529. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15530. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15531. getunitdata_sub(UPET_STR, pd->status.str);
  15532. getunitdata_sub(UPET_AGI, pd->status.agi);
  15533. getunitdata_sub(UPET_VIT, pd->status.vit);
  15534. getunitdata_sub(UPET_INT, pd->status.int_);
  15535. getunitdata_sub(UPET_DEX, pd->status.dex);
  15536. getunitdata_sub(UPET_LUK, pd->status.luk);
  15537. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15538. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15539. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15540. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15541. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15542. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15543. getunitdata_sub(UPET_DEF, pd->status.def);
  15544. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15545. getunitdata_sub(UPET_HIT, pd->status.hit);
  15546. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15547. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15548. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15549. getunitdata_sub(UPET_RACE, pd->status.race);
  15550. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15551. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15552. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15553. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15554. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15555. break;
  15556. case BL_MER:
  15557. if (!mc) {
  15558. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15559. return SCRIPT_CMD_FAILURE;
  15560. }
  15561. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15562. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15563. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15564. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15565. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15566. getunitdata_sub(UMER_X, mc->bl.x);
  15567. getunitdata_sub(UMER_Y, mc->bl.y);
  15568. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15569. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15570. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15571. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15572. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15573. getunitdata_sub(UMER_STR, mc->base_status.str);
  15574. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15575. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15576. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15577. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15578. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15579. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15580. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15581. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15582. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15583. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15584. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15585. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15586. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15587. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15588. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15589. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15590. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15591. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15592. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15593. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15594. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15595. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15596. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15597. getunitdata_sub(UMER_TARGETID, mc->ud.target);
  15598. break;
  15599. case BL_ELEM:
  15600. if (!ed) {
  15601. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15602. return SCRIPT_CMD_FAILURE;
  15603. }
  15604. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15605. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15606. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15607. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15608. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15609. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15610. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15611. getunitdata_sub(UELE_X, ed->bl.x);
  15612. getunitdata_sub(UELE_Y, ed->bl.y);
  15613. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15614. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15615. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15616. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15617. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15618. getunitdata_sub(UELE_STR, ed->base_status.str);
  15619. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15620. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15621. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15622. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15623. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15624. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15625. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15626. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15627. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15628. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15629. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15630. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15631. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15632. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15633. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15634. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15635. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15636. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15637. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15638. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15639. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15640. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15641. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15642. getunitdata_sub(UELE_TARGETID, ed->ud.target);
  15643. break;
  15644. case BL_NPC:
  15645. if (!nd) {
  15646. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15647. return SCRIPT_CMD_FAILURE;
  15648. }
  15649. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15650. getunitdata_sub(UNPC_LEVEL, nd->level);
  15651. getunitdata_sub(UNPC_HP, nd->status.hp);
  15652. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15653. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15654. getunitdata_sub(UNPC_X, nd->bl.x);
  15655. getunitdata_sub(UNPC_Y, nd->bl.y);
  15656. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15657. getunitdata_sub(UNPC_STR, nd->status.str);
  15658. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15659. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15660. getunitdata_sub(UNPC_INT, nd->status.int_);
  15661. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15662. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15663. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15664. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15665. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15666. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15667. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15668. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15669. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15670. getunitdata_sub(UNPC_DEF, nd->status.def);
  15671. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15672. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15673. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15674. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15675. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15676. getunitdata_sub(UNPC_RACE, nd->status.race);
  15677. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15678. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15679. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15680. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15681. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15682. break;
  15683. default:
  15684. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15685. return SCRIPT_CMD_FAILURE;
  15686. }
  15687. #undef getunitdata_sub
  15688. return SCRIPT_CMD_SUCCESS;
  15689. }
  15690. /// Changes the live data of a bl.
  15691. ///
  15692. /// setunitdata <unit id>,<type>,<value>;
  15693. BUILDIN_FUNC(setunitdata)
  15694. {
  15695. struct block_list* bl = NULL;
  15696. struct script_data* data;
  15697. const char *mapname = NULL;
  15698. TBL_MOB* md = NULL;
  15699. TBL_HOM* hd = NULL;
  15700. TBL_MER* mc = NULL;
  15701. TBL_PET* pd = NULL;
  15702. TBL_ELEM* ed = NULL;
  15703. TBL_NPC* nd = NULL;
  15704. int type, value = 0;
  15705. bool calc_status = false;
  15706. if(!script_rid2bl(2,bl))
  15707. {
  15708. script_pushint(st, -1);
  15709. return SCRIPT_CMD_FAILURE;
  15710. }
  15711. switch (bl->type) {
  15712. case BL_MOB: md = map_id2md(bl->id); break;
  15713. case BL_HOM: hd = map_id2hd(bl->id); break;
  15714. case BL_PET: pd = map_id2pd(bl->id); break;
  15715. case BL_MER: mc = map_id2mc(bl->id); break;
  15716. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15717. case BL_NPC:
  15718. nd = map_id2nd(bl->id);
  15719. if (!nd->status.hp)
  15720. status_calc_npc(nd, SCO_FIRST);
  15721. else
  15722. status_calc_npc(nd, SCO_NONE);
  15723. break;
  15724. default:
  15725. ShowError("buildin_setunitdata: Invalid object!\n");
  15726. return SCRIPT_CMD_FAILURE;
  15727. }
  15728. type = script_getnum(st, 3);
  15729. data = script_getdata(st, 4);
  15730. get_val(st, data);
  15731. if (type == 5 && data_isstring(data))
  15732. mapname = conv_str(st, data);
  15733. else if (data_isint(data))
  15734. value = conv_num(st, data);
  15735. else {
  15736. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15737. return SCRIPT_CMD_FAILURE;
  15738. }
  15739. switch (bl->type) {
  15740. case BL_MOB:
  15741. if (!md) {
  15742. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15743. return SCRIPT_CMD_FAILURE;
  15744. }
  15745. if (!md->base_status) {
  15746. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  15747. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  15748. }
  15749. // Check if the view data will be modified
  15750. switch( type ){
  15751. case UMOB_SEX:
  15752. //case UMOB_CLASS: // Called by status_set_viewdata
  15753. case UMOB_HAIRSTYLE:
  15754. case UMOB_HAIRCOLOR:
  15755. case UMOB_HEADBOTTOM:
  15756. case UMOB_HEADMIDDLE:
  15757. case UMOB_HEADTOP:
  15758. case UMOB_CLOTHCOLOR:
  15759. case UMOB_SHIELD:
  15760. case UMOB_WEAPON:
  15761. mob_set_dynamic_viewdata( md );
  15762. break;
  15763. }
  15764. switch (type) {
  15765. case UMOB_SIZE: md->base_status->size = (unsigned char)value; calc_status = true; break;
  15766. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15767. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15768. 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;
  15769. case UMOB_MASTERAID: md->master_id = value; break;
  15770. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15771. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15772. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15773. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15774. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  15775. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15776. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15777. case UMOB_SEX: md->vd->sex = (char)value; clif_clearunit_area(bl, CLR_OUTSIGHT); clif_spawn(bl); break;
  15778. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); clif_clearunit_area(bl, CLR_OUTSIGHT); clif_spawn(bl); break;
  15779. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15780. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15781. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15782. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15783. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15784. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15785. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15786. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15787. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15788. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15789. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15790. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15791. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15792. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15793. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15794. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15795. case UMOB_SLAVECPYMSTRMD:
  15796. if (value > 0) {
  15797. TBL_MOB *md2 = NULL;
  15798. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15799. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15800. break;
  15801. }
  15802. md->base_status->mode = md2->status.mode;
  15803. md->state.copy_master_mode = 1;
  15804. } else
  15805. md->state.copy_master_mode = 0;
  15806. calc_status = true;
  15807. break;
  15808. case UMOB_DMGIMMUNE: md->ud.immune_attack = value > 0; break;
  15809. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  15810. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  15811. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15812. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  15813. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  15814. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  15815. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  15816. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  15817. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  15818. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  15819. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  15820. case UMOB_RACE: md->base_status->race = (unsigned char)value; calc_status = true; break;
  15821. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  15822. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  15823. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  15824. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  15825. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  15826. case UMOB_TARGETID: {
  15827. struct block_list* target = map_id2bl(value);
  15828. if (!target) {
  15829. ShowWarning("buildin_setunitdata: Error in finding target for BL_MOB!\n");
  15830. return SCRIPT_CMD_FAILURE;
  15831. }
  15832. mob_target(md,target,0);
  15833. break;
  15834. }
  15835. default:
  15836. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15837. return SCRIPT_CMD_FAILURE;
  15838. }
  15839. if (calc_status)
  15840. status_calc_bl(&md->bl, SCB_BATTLE);
  15841. break;
  15842. case BL_HOM:
  15843. if (!hd) {
  15844. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15845. return SCRIPT_CMD_FAILURE;
  15846. }
  15847. switch (type) {
  15848. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; calc_status = true; break;
  15849. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15850. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15851. case UHOM_MAXHP: hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15852. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15853. case UHOM_MAXSP: hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15854. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15855. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15856. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15857. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15858. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15859. 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;
  15860. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15861. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15862. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15863. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15864. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15865. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15866. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15867. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15868. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15869. case UHOM_DMGIMMUNE: hd->ud.immune_attack = value > 0; break;
  15870. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15871. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15872. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15873. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15874. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15875. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  15876. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  15877. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  15878. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  15879. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  15880. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  15881. case UHOM_RACE: hd->base_status.race = (unsigned char)value; calc_status = true; break;
  15882. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15883. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15884. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  15885. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  15886. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  15887. case UHOM_TARGETID: {
  15888. struct block_list* target = map_id2bl(value);
  15889. if (!target) {
  15890. ShowWarning("buildin_setunitdata: Error in finding target for BL_HOM!\n");
  15891. return SCRIPT_CMD_FAILURE;
  15892. }
  15893. unit_attack(&hd->bl, target->id, 1);
  15894. break;
  15895. }
  15896. default:
  15897. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15898. return SCRIPT_CMD_FAILURE;
  15899. }
  15900. if (calc_status)
  15901. status_calc_bl(&hd->bl, SCB_BATTLE);
  15902. break;
  15903. case BL_PET:
  15904. if (!pd) {
  15905. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15906. return SCRIPT_CMD_FAILURE;
  15907. }
  15908. switch (type) {
  15909. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15910. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15911. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15912. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15913. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15914. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15915. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15916. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15917. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15918. 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;
  15919. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15920. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15921. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15922. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15923. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15924. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15925. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15926. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15927. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15928. case UPET_DMGIMMUNE: pd->ud.immune_attack = value > 0; break;
  15929. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15930. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15931. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15932. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15933. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15934. case UPET_DEF: pd->status.def = (defType)value; break;
  15935. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15936. case UPET_HIT: pd->status.hit = (short)value; break;
  15937. case UPET_FLEE: pd->status.flee = (short)value; break;
  15938. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15939. case UPET_CRIT: pd->status.cri = (short)value; break;
  15940. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15941. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15942. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15943. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15944. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15945. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15946. default:
  15947. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15948. return SCRIPT_CMD_FAILURE;
  15949. }
  15950. break;
  15951. case BL_MER:
  15952. if (!mc) {
  15953. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15954. return SCRIPT_CMD_FAILURE;
  15955. }
  15956. switch (type) {
  15957. case UMER_SIZE: mc->base_status.size = (unsigned char)value; calc_status = true; break;
  15958. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15959. case UMER_MAXHP: mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15960. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15961. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15962. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15963. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15964. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15965. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15966. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15967. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15968. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15969. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15970. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15971. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15972. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15973. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15974. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15975. case UMER_DMGIMMUNE: mc->ud.immune_attack = value > 0; break;
  15976. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15977. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15978. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15979. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15980. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15981. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  15982. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  15983. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  15984. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  15985. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  15986. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  15987. case UMER_RACE: mc->base_status.race = (unsigned char)value; calc_status = true; break;
  15988. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15989. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15990. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  15991. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  15992. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  15993. case UMER_TARGETID: {
  15994. struct block_list* target = map_id2bl(value);
  15995. if (!target) {
  15996. ShowWarning("buildin_setunitdata: Error in finding target for BL_MER!\n");
  15997. return SCRIPT_CMD_FAILURE;
  15998. }
  15999. unit_attack(&mc->bl, target->id, 1);
  16000. break;
  16001. }
  16002. default:
  16003. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  16004. return SCRIPT_CMD_FAILURE;
  16005. }
  16006. if (calc_status)
  16007. status_calc_bl(&mc->bl, SCB_BATTLE);
  16008. break;
  16009. case BL_ELEM:
  16010. if (!ed) {
  16011. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  16012. return SCRIPT_CMD_FAILURE;
  16013. }
  16014. switch (type) {
  16015. case UELE_SIZE: ed->base_status.size = (unsigned char)value; calc_status = true; break;
  16016. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  16017. case UELE_MAXHP: ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  16018. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  16019. case UELE_MAXSP: ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  16020. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  16021. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16022. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  16023. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  16024. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  16025. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  16026. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16027. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16028. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  16029. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16030. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16031. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16032. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16033. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16034. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  16035. case UELE_DMGIMMUNE: ed->ud.immune_attack = value > 0; break;
  16036. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  16037. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  16038. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  16039. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  16040. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  16041. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  16042. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  16043. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  16044. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  16045. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  16046. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  16047. case UELE_RACE: ed->base_status.race = (unsigned char)value; calc_status = true; break;
  16048. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  16049. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  16050. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  16051. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  16052. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  16053. case UELE_TARGETID: {
  16054. struct block_list* target = map_id2bl(value);
  16055. if (!target) {
  16056. ShowWarning("buildin_setunitdata: Error in finding target for BL_ELEM!\n");
  16057. return SCRIPT_CMD_FAILURE;
  16058. }
  16059. elemental_change_mode(ed, static_cast<e_mode>(EL_MODE_AGGRESSIVE));
  16060. unit_attack(&ed->bl, target->id, 1);
  16061. break;
  16062. }
  16063. default:
  16064. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  16065. return SCRIPT_CMD_FAILURE;
  16066. }
  16067. if (calc_status)
  16068. status_calc_bl(&ed->bl, SCB_BATTLE);
  16069. break;
  16070. case BL_NPC:
  16071. if (!nd) {
  16072. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  16073. return SCRIPT_CMD_FAILURE;
  16074. }
  16075. switch (type) {
  16076. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  16077. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  16078. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  16079. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  16080. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  16081. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  16082. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  16083. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  16084. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16085. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16086. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16087. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16088. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16089. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  16090. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  16091. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  16092. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  16093. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  16094. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  16095. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  16096. case UNPC_DEF: nd->status.def = (defType)value; break;
  16097. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  16098. case UNPC_HIT: nd->status.hit = (short)value; break;
  16099. case UNPC_FLEE: nd->status.flee = (short)value; break;
  16100. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  16101. case UNPC_CRIT: nd->status.cri = (short)value; break;
  16102. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  16103. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  16104. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  16105. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  16106. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  16107. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  16108. default:
  16109. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  16110. return SCRIPT_CMD_FAILURE;
  16111. }
  16112. break;
  16113. default:
  16114. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  16115. return SCRIPT_CMD_FAILURE;
  16116. }
  16117. // Client information updates
  16118. switch (bl->type) {
  16119. case BL_HOM:
  16120. clif_send_homdata(hd->master, SP_ACK, 0);
  16121. break;
  16122. case BL_PET:
  16123. clif_send_petstatus(pd->master);
  16124. break;
  16125. case BL_MER:
  16126. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  16127. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  16128. break;
  16129. case BL_ELEM:
  16130. clif_elemental_info(ed->master);
  16131. break;
  16132. }
  16133. return SCRIPT_CMD_SUCCESS;
  16134. }
  16135. /// Gets the name of a bl.
  16136. /// Supported types are [MOB|HOM|PET|NPC].
  16137. /// MER and ELEM don't support custom names.
  16138. ///
  16139. /// getunitname <unit id>;
  16140. BUILDIN_FUNC(getunitname)
  16141. {
  16142. struct block_list* bl = NULL;
  16143. if(!script_rid2bl(2,bl)){
  16144. script_pushconststr(st, "Unknown");
  16145. return SCRIPT_CMD_FAILURE;
  16146. }
  16147. script_pushstrcopy(st, status_get_name(bl));
  16148. return SCRIPT_CMD_SUCCESS;
  16149. }
  16150. /// Changes the name of a bl.
  16151. /// Supported types are [MOB|HOM|PET].
  16152. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  16153. ///
  16154. /// setunitname <unit id>,<name>;
  16155. BUILDIN_FUNC(setunitname)
  16156. {
  16157. struct block_list* bl = NULL;
  16158. TBL_MOB* md = NULL;
  16159. TBL_HOM* hd = NULL;
  16160. TBL_PET* pd = NULL;
  16161. if(!script_rid2bl(2,bl))
  16162. {
  16163. script_pushconststr(st, "Unknown");
  16164. return SCRIPT_CMD_FAILURE;
  16165. }
  16166. switch (bl->type) {
  16167. case BL_MOB: md = map_id2md(bl->id); break;
  16168. case BL_HOM: hd = map_id2hd(bl->id); break;
  16169. case BL_PET: pd = map_id2pd(bl->id); break;
  16170. default:
  16171. ShowWarning("buildin_setunitname: Invalid object type!\n");
  16172. return SCRIPT_CMD_FAILURE;
  16173. }
  16174. switch (bl->type) {
  16175. case BL_MOB:
  16176. if (!md) {
  16177. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  16178. return SCRIPT_CMD_FAILURE;
  16179. }
  16180. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  16181. break;
  16182. case BL_HOM:
  16183. if (!hd) {
  16184. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  16185. return SCRIPT_CMD_FAILURE;
  16186. }
  16187. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  16188. break;
  16189. case BL_PET:
  16190. if (!pd) {
  16191. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  16192. return SCRIPT_CMD_FAILURE;
  16193. }
  16194. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  16195. break;
  16196. default:
  16197. ShowWarning("buildin_setunitname: Unknown object type!\n");
  16198. return SCRIPT_CMD_FAILURE;
  16199. }
  16200. clif_name_area(bl); // Send update to client.
  16201. return SCRIPT_CMD_SUCCESS;
  16202. }
  16203. /// Makes the unit walk to target position or map.
  16204. /// Returns if it was successful.
  16205. ///
  16206. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  16207. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  16208. BUILDIN_FUNC(unitwalk)
  16209. {
  16210. struct block_list* bl;
  16211. struct unit_data *ud = NULL;
  16212. const char *cmd = script_getfuncname(st), *done_label = "";
  16213. uint8 off = 5;
  16214. if(!script_rid2bl(2,bl))
  16215. {
  16216. script_pushint(st, 0);
  16217. return SCRIPT_CMD_FAILURE;
  16218. }
  16219. ud = unit_bl2ud(bl);
  16220. if (!strcmp(cmd,"unitwalk")) {
  16221. int x = script_getnum(st,3);
  16222. int y = script_getnum(st,4);
  16223. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  16224. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  16225. } else {
  16226. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  16227. if (!tbl) {
  16228. ShowError("buildin_unitwalk: Bad target destination.\n");
  16229. script_pushint(st, 0);
  16230. return SCRIPT_CMD_FAILURE;
  16231. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  16232. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  16233. off = 4;
  16234. }
  16235. if (ud && script_hasdata(st, off)) {
  16236. done_label = script_getstr(st, off);
  16237. check_event(st, done_label);
  16238. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  16239. }
  16240. return SCRIPT_CMD_SUCCESS;
  16241. }
  16242. /// Kills the unit.
  16243. ///
  16244. /// unitkill <unit_id>;
  16245. BUILDIN_FUNC(unitkill)
  16246. {
  16247. struct block_list* bl;
  16248. if(script_rid2bl(2,bl))
  16249. status_kill(bl);
  16250. return SCRIPT_CMD_SUCCESS;
  16251. }
  16252. /// Warps the unit to the target position in the target map.
  16253. /// Returns if it was successful.
  16254. ///
  16255. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  16256. BUILDIN_FUNC(unitwarp)
  16257. {
  16258. int map_idx;
  16259. short x;
  16260. short y;
  16261. struct block_list* bl;
  16262. const char *mapname;
  16263. mapname = script_getstr(st, 3);
  16264. x = (short)script_getnum(st,4);
  16265. y = (short)script_getnum(st,5);
  16266. if(!script_rid2bl(2,bl))
  16267. {
  16268. script_pushint(st, 0);
  16269. return SCRIPT_CMD_SUCCESS;
  16270. }
  16271. if (!strcmp(mapname,"this"))
  16272. map_idx = bl?bl->m:-1;
  16273. else
  16274. map_idx = map_mapname2mapid(mapname);
  16275. if (map_idx >= 0 && bl != NULL)
  16276. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  16277. else
  16278. script_pushint(st, 0);
  16279. return SCRIPT_CMD_SUCCESS;
  16280. }
  16281. /// Makes the unit attack the target.
  16282. /// If the unit is a player and <action type> is not 0, it does a continuous
  16283. /// attack instead of a single attack.
  16284. /// Returns if the request was successful.
  16285. ///
  16286. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  16287. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  16288. BUILDIN_FUNC(unitattack)
  16289. {
  16290. struct block_list* unit_bl;
  16291. struct block_list* target_bl = NULL;
  16292. struct script_data* data;
  16293. int actiontype = 0;
  16294. if (!script_rid2bl(2,unit_bl)) {
  16295. script_pushint(st, false);
  16296. return SCRIPT_CMD_FAILURE;
  16297. }
  16298. data = script_getdata(st, 3);
  16299. get_val(st, data);
  16300. if (data_isstring(data)) {
  16301. TBL_PC* sd = map_nick2sd(conv_str(st, data),false);
  16302. if( sd != NULL )
  16303. target_bl = &sd->bl;
  16304. } else
  16305. target_bl = map_id2bl(conv_num(st, data));
  16306. if (!target_bl) {
  16307. script_pushint(st, false);
  16308. return SCRIPT_CMD_FAILURE;
  16309. }
  16310. if (script_hasdata(st,4))
  16311. actiontype = script_getnum(st,4);
  16312. switch(unit_bl->type) {
  16313. case BL_PC: {
  16314. struct map_session_data* sd = (struct map_session_data*)unit_bl;
  16315. clif_parse_ActionRequest_sub(sd, actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  16316. script_pushint(st, sd->ud.target == target_bl->id);
  16317. return SCRIPT_CMD_SUCCESS;
  16318. }
  16319. case BL_MOB:
  16320. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  16321. break;
  16322. case BL_PET:
  16323. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  16324. break;
  16325. default:
  16326. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  16327. script_pushint(st, false);
  16328. return SCRIPT_CMD_FAILURE;
  16329. }
  16330. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  16331. return SCRIPT_CMD_SUCCESS;
  16332. }
  16333. /// Makes the unit stop attacking.
  16334. ///
  16335. /// unitstopattack <unit_id>;
  16336. BUILDIN_FUNC(unitstopattack)
  16337. {
  16338. struct block_list* bl;
  16339. if(script_rid2bl(2,bl))
  16340. {
  16341. unit_stop_attack(bl);
  16342. if (bl->type == BL_MOB)
  16343. ((TBL_MOB*)bl)->target_id = 0;
  16344. }
  16345. return SCRIPT_CMD_SUCCESS;
  16346. }
  16347. /// Makes the unit stop walking.
  16348. ///
  16349. /// unitstopwalk <unit_id>{,<flag>};
  16350. BUILDIN_FUNC(unitstopwalk)
  16351. {
  16352. struct block_list* bl;
  16353. int flag = USW_NONE;
  16354. if (script_hasdata(st, 3))
  16355. flag = script_getnum(st, 3);
  16356. if(script_rid2bl(2,bl))
  16357. unit_stop_walking(bl, flag);
  16358. return SCRIPT_CMD_SUCCESS;
  16359. }
  16360. /**
  16361. * Makes the unit say the given message.
  16362. *
  16363. * unittalk <unit_id>,"<message>"{,"<flag>"};
  16364. * @param flag: Specify target
  16365. * bc_area - Message is sent to players in the vicinity of the source (default).
  16366. * bc_self - Message is sent only to player attached.
  16367. */
  16368. BUILDIN_FUNC(unittalk)
  16369. {
  16370. const char* message;
  16371. struct block_list* bl;
  16372. message = script_getstr(st, 3);
  16373. if(script_rid2bl(2,bl))
  16374. {
  16375. send_target target = AREA;
  16376. struct StringBuf sbuf;
  16377. if (script_hasdata(st, 4)) {
  16378. if (script_getnum(st, 4) == BC_SELF) {
  16379. if (map_id2sd(bl->id) == NULL) {
  16380. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  16381. return SCRIPT_CMD_FAILURE;
  16382. }
  16383. target = SELF;
  16384. }
  16385. }
  16386. StringBuf_Init(&sbuf);
  16387. StringBuf_Printf(&sbuf, "%s", message);
  16388. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  16389. StringBuf_Destroy(&sbuf);
  16390. }
  16391. return SCRIPT_CMD_SUCCESS;
  16392. }
  16393. /// Makes the unit do an emotion.
  16394. ///
  16395. /// unitemote <unit_id>,<emotion>;
  16396. ///
  16397. /// @see ET_* in script_constants.h
  16398. BUILDIN_FUNC(unitemote)
  16399. {
  16400. int emotion;
  16401. struct block_list* bl;
  16402. emotion = script_getnum(st,3);
  16403. if (emotion < ET_SURPRISE || emotion >= ET_MAX) {
  16404. ShowWarning("buildin_emotion: Unknown emotion %d (min=%d, max=%d).\n", emotion, ET_SURPRISE, (ET_MAX-1));
  16405. return SCRIPT_CMD_FAILURE;
  16406. }
  16407. if (script_rid2bl(2,bl))
  16408. clif_emotion(bl, emotion);
  16409. return SCRIPT_CMD_SUCCESS;
  16410. }
  16411. /// Makes the unit cast the skill on the target or self if no target is specified.
  16412. ///
  16413. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  16414. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  16415. BUILDIN_FUNC(unitskilluseid)
  16416. {
  16417. int unit_id, target_id, casttime;
  16418. uint16 skill_id, skill_lv;
  16419. struct block_list* bl;
  16420. struct script_data *data;
  16421. unit_id = script_getnum(st,2);
  16422. data = script_getdata(st, 3);
  16423. get_val(st, data); // Convert into value in case of a variable
  16424. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  16425. skill_lv = script_getnum(st,4);
  16426. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  16427. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  16428. if(script_rid2bl(2,bl)){
  16429. if (bl->type == BL_NPC) {
  16430. if (!((TBL_NPC*)bl)->status.hp)
  16431. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16432. else
  16433. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16434. }
  16435. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  16436. }
  16437. return SCRIPT_CMD_SUCCESS;
  16438. }
  16439. /// Makes the unit cast the skill on the target position.
  16440. ///
  16441. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16442. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16443. BUILDIN_FUNC(unitskillusepos)
  16444. {
  16445. int skill_x, skill_y, casttime;
  16446. uint16 skill_id, skill_lv;
  16447. struct block_list* bl;
  16448. struct script_data *data;
  16449. data = script_getdata(st, 3);
  16450. get_val(st, data); // Convert into value in case of a variable
  16451. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  16452. skill_lv = script_getnum(st,4);
  16453. skill_x = script_getnum(st,5);
  16454. skill_y = script_getnum(st,6);
  16455. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16456. if(script_rid2bl(2,bl)){
  16457. if (bl->type == BL_NPC) {
  16458. if (!((TBL_NPC*)bl)->status.hp)
  16459. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16460. else
  16461. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16462. }
  16463. 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));
  16464. }
  16465. return SCRIPT_CMD_SUCCESS;
  16466. }
  16467. // <--- [zBuffer] List of unit control commands
  16468. /// Pauses the execution of the script, detaching the player
  16469. ///
  16470. /// sleep <milli seconds>;
  16471. BUILDIN_FUNC(sleep)
  16472. {
  16473. // First call(by function call)
  16474. if (st->sleep.tick == 0) {
  16475. int ticks;
  16476. ticks = script_getnum(st, 2);
  16477. if (ticks <= 0) {
  16478. ShowError("buildin_sleep: negative or zero amount('%d') of milli seconds is not supported\n", ticks);
  16479. return SCRIPT_CMD_FAILURE;
  16480. }
  16481. // detach the player
  16482. script_detach_rid(st);
  16483. // sleep for the target amount of time
  16484. st->state = RERUNLINE;
  16485. st->sleep.tick = ticks;
  16486. // Second call(by timer after sleeping time is over)
  16487. } else {
  16488. // Continue the script
  16489. st->state = RUN;
  16490. st->sleep.tick = 0;
  16491. }
  16492. return SCRIPT_CMD_SUCCESS;
  16493. }
  16494. /// Pauses the execution of the script, keeping the unit attached
  16495. /// Stops the script if no unit is attached
  16496. ///
  16497. /// sleep2(<milli seconds>)
  16498. BUILDIN_FUNC(sleep2)
  16499. {
  16500. // First call(by function call)
  16501. if (st->sleep.tick == 0) {
  16502. int ticks;
  16503. ticks = script_getnum(st, 2);
  16504. if (ticks <= 0) {
  16505. ShowError( "buildin_sleep2: negative or zero amount('%d') of milli seconds is not supported\n", ticks );
  16506. return SCRIPT_CMD_FAILURE;
  16507. }
  16508. if (map_id2bl(st->rid) == NULL) {
  16509. ShowError( "buildin_sleep2: no unit is attached\n" );
  16510. return SCRIPT_CMD_FAILURE;
  16511. }
  16512. // sleep for the target amount of time
  16513. st->state = RERUNLINE;
  16514. st->sleep.tick = ticks;
  16515. // Second call(by timer after sleeping time is over)
  16516. } else {
  16517. // Check if the unit is still attached
  16518. // NOTE: This should never happen, since run_script_timer already checks this
  16519. if (map_id2bl(st->rid) == NULL) {
  16520. // The unit is not attached anymore - terminate the script
  16521. st->rid = 0;
  16522. st->state = END;
  16523. } else {
  16524. // The unit is still attached - continue the script
  16525. st->state = RUN;
  16526. st->sleep.tick = 0;
  16527. }
  16528. }
  16529. return SCRIPT_CMD_SUCCESS;
  16530. }
  16531. /// Awakes all the sleep timers of the target npc
  16532. ///
  16533. /// awake "<npc name>";
  16534. BUILDIN_FUNC(awake)
  16535. {
  16536. DBIterator *iter;
  16537. struct script_state *tst;
  16538. struct npc_data* nd;
  16539. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  16540. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  16541. return SCRIPT_CMD_FAILURE;
  16542. }
  16543. iter = db_iterator(st_db);
  16544. for (tst = static_cast<script_state *>(dbi_first(iter)); dbi_exists(iter); tst = static_cast<script_state *>(dbi_next(iter))) {
  16545. if (tst->oid == nd->bl.id) {
  16546. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  16547. continue;
  16548. }
  16549. delete_timer(tst->sleep.timer, run_script_timer);
  16550. // Trigger the timer function
  16551. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  16552. }
  16553. }
  16554. dbi_destroy(iter);
  16555. return SCRIPT_CMD_SUCCESS;
  16556. }
  16557. /// Returns a reference to a variable of the target NPC.
  16558. /// Returns 0 if an error occurs.
  16559. ///
  16560. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  16561. BUILDIN_FUNC(getvariableofnpc)
  16562. {
  16563. struct script_data* data;
  16564. const char* name;
  16565. struct npc_data* nd;
  16566. data = script_getdata(st,2);
  16567. if( !data_isreference(data) )
  16568. {// Not a reference (aka varaible name)
  16569. ShowError("buildin_getvariableofnpc: not a variable\n");
  16570. script_reportdata(data);
  16571. script_pushnil(st);
  16572. st->state = END;
  16573. return SCRIPT_CMD_FAILURE;
  16574. }
  16575. name = reference_getname(data);
  16576. if( *name != '.' || name[1] == '@' )
  16577. {// not a npc variable
  16578. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  16579. script_reportdata(data);
  16580. script_pushnil(st);
  16581. st->state = END;
  16582. return SCRIPT_CMD_FAILURE;
  16583. }
  16584. nd = npc_name2id(script_getstr(st,3));
  16585. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  16586. {// NPC not found or has no script
  16587. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  16588. script_pushnil(st);
  16589. st->state = END;
  16590. return SCRIPT_CMD_FAILURE;
  16591. }
  16592. if (!nd->u.scr.script->local.vars)
  16593. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  16594. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  16595. return SCRIPT_CMD_SUCCESS;
  16596. }
  16597. /// Opens a warp portal.
  16598. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  16599. ///
  16600. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  16601. ///
  16602. /// @author blackhole89
  16603. BUILDIN_FUNC(warpportal)
  16604. {
  16605. int spx;
  16606. int spy;
  16607. unsigned short mapindex;
  16608. int tpx;
  16609. int tpy;
  16610. struct skill_unit_group* group;
  16611. struct block_list* bl;
  16612. bl = map_id2bl(st->oid);
  16613. if( bl == NULL ) {
  16614. ShowError("buildin_warpportal: NPC is needed\n");
  16615. return SCRIPT_CMD_FAILURE;
  16616. }
  16617. spx = script_getnum(st,2);
  16618. spy = script_getnum(st,3);
  16619. mapindex = mapindex_name2id(script_getstr(st, 4));
  16620. tpx = script_getnum(st,5);
  16621. tpy = script_getnum(st,6);
  16622. if( mapindex == 0 ) {
  16623. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  16624. return SCRIPT_CMD_FAILURE;
  16625. }
  16626. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  16627. if( group == NULL )
  16628. return SCRIPT_CMD_FAILURE;// failed
  16629. group->val1 = (group->val1<<16)|(short)0;
  16630. group->val2 = (tpx<<16) | tpy;
  16631. group->val3 = mapindex;
  16632. return SCRIPT_CMD_SUCCESS;
  16633. }
  16634. /**
  16635. * openmail({<char_id>});
  16636. **/
  16637. BUILDIN_FUNC(openmail)
  16638. {
  16639. TBL_PC* sd;
  16640. if (!script_charid2sd(2,sd))
  16641. return SCRIPT_CMD_FAILURE;
  16642. mail_openmail(sd);
  16643. return SCRIPT_CMD_SUCCESS;
  16644. }
  16645. /**
  16646. * openauction({<char_id>});
  16647. **/
  16648. BUILDIN_FUNC(openauction)
  16649. {
  16650. TBL_PC* sd;
  16651. if (!script_charid2sd(2,sd))
  16652. return SCRIPT_CMD_FAILURE;
  16653. if( !battle_config.feature_auction ) {
  16654. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  16655. return SCRIPT_CMD_SUCCESS;
  16656. }
  16657. clif_Auction_openwindow(sd);
  16658. return SCRIPT_CMD_SUCCESS;
  16659. }
  16660. /// Retrieves the value of the specified flag of the specified cell.
  16661. ///
  16662. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  16663. ///
  16664. /// @see cell_chk* constants in const.txt for the types
  16665. BUILDIN_FUNC(checkcell)
  16666. {
  16667. int16 m = map_mapname2mapid(script_getstr(st,2));
  16668. int16 x = script_getnum(st,3);
  16669. int16 y = script_getnum(st,4);
  16670. cell_chk type = (cell_chk)script_getnum(st,5);
  16671. script_pushint(st, map_getcell(m, x, y, type));
  16672. return SCRIPT_CMD_SUCCESS;
  16673. }
  16674. /// Modifies flags of cells in the specified area.
  16675. ///
  16676. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16677. ///
  16678. /// @see cell_* constants in const.txt for the types
  16679. BUILDIN_FUNC(setcell)
  16680. {
  16681. int16 m = map_mapname2mapid(script_getstr(st,2));
  16682. int16 x1 = script_getnum(st,3);
  16683. int16 y1 = script_getnum(st,4);
  16684. int16 x2 = script_getnum(st,5);
  16685. int16 y2 = script_getnum(st,6);
  16686. cell_t type = (cell_t)script_getnum(st,7);
  16687. bool flag = script_getnum(st,8) != 0;
  16688. int x,y;
  16689. if( x1 > x2 ) SWAP(x1,x2);
  16690. if( y1 > y2 ) SWAP(y1,y2);
  16691. for( y = y1; y <= y2; ++y )
  16692. for( x = x1; x <= x2; ++x )
  16693. map_setcell(m, x, y, type, flag);
  16694. return SCRIPT_CMD_SUCCESS;
  16695. }
  16696. /**
  16697. * Gets a free cell in the specified area.
  16698. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  16699. */
  16700. BUILDIN_FUNC(getfreecell)
  16701. {
  16702. const char *mapn = script_getstr(st, 2), *name;
  16703. char prefix;
  16704. struct map_session_data *sd;
  16705. int64 num;
  16706. int16 m, x = 0, y = 0;
  16707. int rx = -1, ry = -1, flag = 1;
  16708. sd = map_id2sd(st->rid);
  16709. if (!data_isreference(script_getdata(st, 3))) {
  16710. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  16711. script_pushint(st, -1);
  16712. return SCRIPT_CMD_FAILURE;
  16713. }
  16714. if (!data_isreference(script_getdata(st, 4))) {
  16715. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  16716. script_pushint(st, -1);
  16717. return SCRIPT_CMD_FAILURE;
  16718. }
  16719. if (is_string_variable(reference_getname(script_getdata(st, 3)))) {
  16720. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  16721. script_pushint(st, -1);
  16722. return SCRIPT_CMD_FAILURE;
  16723. }
  16724. if (is_string_variable(reference_getname(script_getdata(st, 4)))) {
  16725. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  16726. script_pushint(st, -1);
  16727. return SCRIPT_CMD_FAILURE;
  16728. }
  16729. if (script_hasdata(st, 5))
  16730. x = script_getnum(st, 5);
  16731. if (script_hasdata(st, 6))
  16732. y = script_getnum(st, 6);
  16733. if (script_hasdata(st, 7))
  16734. rx = script_getnum(st, 7);
  16735. if (script_hasdata(st, 8))
  16736. ry = script_getnum(st, 8);
  16737. if (script_hasdata(st, 9))
  16738. flag = script_getnum(st, 9);
  16739. if (sd && strcmp(mapn, "this") == 0)
  16740. m = sd->bl.m;
  16741. else
  16742. m = map_mapname2mapid(mapn);
  16743. map_search_freecell(NULL, m, &x, &y, rx, ry, flag);
  16744. // Set MapX
  16745. num = st->stack->stack_data[st->start + 3].u.num;
  16746. name = get_str(num&0x00ffffff);
  16747. prefix = *name;
  16748. if (not_server_variable(prefix)){
  16749. if( !script_rid2sd(sd) ){
  16750. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  16751. return SCRIPT_CMD_FAILURE;
  16752. }
  16753. }else
  16754. sd = NULL;
  16755. set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)x), script_getref(st, 3));
  16756. // Set MapY
  16757. num = st->stack->stack_data[st->start + 4].u.num;
  16758. name = get_str(num&0x00ffffff);
  16759. prefix = *name;
  16760. if (not_server_variable(prefix)){
  16761. if( !script_rid2sd(sd) ){
  16762. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  16763. return SCRIPT_CMD_FAILURE;
  16764. }
  16765. }else
  16766. sd = NULL;
  16767. set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)y), script_getref(st, 4));
  16768. return SCRIPT_CMD_SUCCESS;
  16769. }
  16770. /*==========================================
  16771. * Mercenary Commands
  16772. *------------------------------------------*/
  16773. BUILDIN_FUNC(mercenary_create)
  16774. {
  16775. struct map_session_data *sd;
  16776. int class_, contract_time;
  16777. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  16778. return SCRIPT_CMD_SUCCESS;
  16779. class_ = script_getnum(st,2);
  16780. if( !mercenary_db(class_) )
  16781. return SCRIPT_CMD_SUCCESS;
  16782. contract_time = script_getnum(st,3);
  16783. mercenary_create(sd, class_, contract_time);
  16784. return SCRIPT_CMD_SUCCESS;
  16785. }
  16786. BUILDIN_FUNC(mercenary_heal)
  16787. {
  16788. struct map_session_data *sd;
  16789. int hp, sp;
  16790. if( !script_rid2sd(sd) || sd->md == NULL )
  16791. return SCRIPT_CMD_SUCCESS;
  16792. hp = script_getnum(st,2);
  16793. sp = script_getnum(st,3);
  16794. status_heal(&sd->md->bl, hp, sp, 0);
  16795. return SCRIPT_CMD_SUCCESS;
  16796. }
  16797. BUILDIN_FUNC(mercenary_sc_start)
  16798. {
  16799. struct map_session_data *sd;
  16800. enum sc_type type;
  16801. int tick, val1;
  16802. if( !script_rid2sd(sd) || sd->md == NULL )
  16803. return SCRIPT_CMD_SUCCESS;
  16804. type = (sc_type)script_getnum(st,2);
  16805. tick = script_getnum(st,3);
  16806. val1 = script_getnum(st,4);
  16807. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16808. return SCRIPT_CMD_SUCCESS;
  16809. }
  16810. BUILDIN_FUNC(mercenary_get_calls)
  16811. {
  16812. struct map_session_data *sd;
  16813. int guild;
  16814. if( !script_rid2sd(sd) )
  16815. return SCRIPT_CMD_SUCCESS;
  16816. guild = script_getnum(st,2);
  16817. switch( guild )
  16818. {
  16819. case ARCH_MERC_GUILD:
  16820. script_pushint(st,sd->status.arch_calls);
  16821. break;
  16822. case SPEAR_MERC_GUILD:
  16823. script_pushint(st,sd->status.spear_calls);
  16824. break;
  16825. case SWORD_MERC_GUILD:
  16826. script_pushint(st,sd->status.sword_calls);
  16827. break;
  16828. default:
  16829. script_pushint(st,0);
  16830. break;
  16831. }
  16832. return SCRIPT_CMD_SUCCESS;
  16833. }
  16834. BUILDIN_FUNC(mercenary_set_calls)
  16835. {
  16836. struct map_session_data *sd;
  16837. int guild, value, *calls;
  16838. if( !script_rid2sd(sd) )
  16839. return SCRIPT_CMD_SUCCESS;
  16840. guild = script_getnum(st,2);
  16841. value = script_getnum(st,3);
  16842. switch( guild )
  16843. {
  16844. case ARCH_MERC_GUILD:
  16845. calls = &sd->status.arch_calls;
  16846. break;
  16847. case SPEAR_MERC_GUILD:
  16848. calls = &sd->status.spear_calls;
  16849. break;
  16850. case SWORD_MERC_GUILD:
  16851. calls = &sd->status.sword_calls;
  16852. break;
  16853. default:
  16854. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16855. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16856. }
  16857. *calls += value;
  16858. *calls = cap_value(*calls, 0, INT_MAX);
  16859. return SCRIPT_CMD_SUCCESS;
  16860. }
  16861. BUILDIN_FUNC(mercenary_get_faith)
  16862. {
  16863. struct map_session_data *sd;
  16864. int guild;
  16865. if( !script_rid2sd(sd) )
  16866. return SCRIPT_CMD_SUCCESS;
  16867. guild = script_getnum(st,2);
  16868. switch( guild )
  16869. {
  16870. case ARCH_MERC_GUILD:
  16871. script_pushint(st,sd->status.arch_faith);
  16872. break;
  16873. case SPEAR_MERC_GUILD:
  16874. script_pushint(st,sd->status.spear_faith);
  16875. break;
  16876. case SWORD_MERC_GUILD:
  16877. script_pushint(st,sd->status.sword_faith);
  16878. break;
  16879. default:
  16880. script_pushint(st,0);
  16881. break;
  16882. }
  16883. return SCRIPT_CMD_SUCCESS;
  16884. }
  16885. BUILDIN_FUNC(mercenary_set_faith)
  16886. {
  16887. struct map_session_data *sd;
  16888. int guild, value, *calls;
  16889. if( !script_rid2sd(sd) )
  16890. return SCRIPT_CMD_SUCCESS;
  16891. guild = script_getnum(st,2);
  16892. value = script_getnum(st,3);
  16893. switch( guild )
  16894. {
  16895. case ARCH_MERC_GUILD:
  16896. calls = &sd->status.arch_faith;
  16897. break;
  16898. case SPEAR_MERC_GUILD:
  16899. calls = &sd->status.spear_faith;
  16900. break;
  16901. case SWORD_MERC_GUILD:
  16902. calls = &sd->status.sword_faith;
  16903. break;
  16904. default:
  16905. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16906. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16907. }
  16908. *calls += value;
  16909. *calls = cap_value(*calls, 0, INT_MAX);
  16910. if( mercenary_get_guild(sd->md) == guild )
  16911. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16912. return SCRIPT_CMD_SUCCESS;
  16913. }
  16914. /*------------------------------------------
  16915. * Book Reading
  16916. *------------------------------------------*/
  16917. BUILDIN_FUNC(readbook)
  16918. {
  16919. struct map_session_data *sd;
  16920. int book_id, page;
  16921. if( !script_rid2sd(sd) )
  16922. return SCRIPT_CMD_SUCCESS;
  16923. book_id = script_getnum(st,2);
  16924. page = script_getnum(st,3);
  16925. clif_readbook(sd->fd, book_id, page);
  16926. return SCRIPT_CMD_SUCCESS;
  16927. }
  16928. /******************
  16929. Questlog script commands
  16930. *******************/
  16931. /**
  16932. * Add job criteria to questinfo
  16933. * @param qi Quest Info
  16934. * @param job
  16935. * @author [Cydh]
  16936. **/
  16937. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16938. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16939. qi->jobid[qi->jobid_count++] = job;
  16940. }
  16941. /**
  16942. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16943. **/
  16944. BUILDIN_FUNC(questinfo)
  16945. {
  16946. TBL_NPC* nd = map_id2nd(st->oid);
  16947. int quest_id, icon, color = 0;
  16948. struct questinfo qi, *q2;
  16949. if( nd == NULL || nd->bl.m == -1 ) {
  16950. ShowError("buildin_questinfo: No NPC attached.\n");
  16951. return SCRIPT_CMD_FAILURE;
  16952. }
  16953. quest_id = script_getnum(st, 2);
  16954. icon = script_getnum(st, 3);
  16955. #if PACKETVER >= 20120410
  16956. switch(icon){
  16957. case QTYPE_QUEST:
  16958. case QTYPE_QUEST2:
  16959. case QTYPE_JOB:
  16960. case QTYPE_JOB2:
  16961. case QTYPE_EVENT:
  16962. case QTYPE_EVENT2:
  16963. case QTYPE_WARG:
  16964. case QTYPE_WARG2:
  16965. // Leave everything as it is
  16966. break;
  16967. case QTYPE_NONE:
  16968. default:
  16969. // Default to nothing if icon id is invalid.
  16970. icon = QTYPE_NONE;
  16971. break;
  16972. }
  16973. #else
  16974. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16975. icon = QTYPE_QUEST;
  16976. else
  16977. icon = icon + 1;
  16978. #endif
  16979. qi.quest_id = quest_id;
  16980. qi.icon = (unsigned char)icon;
  16981. qi.nd = nd;
  16982. if( script_hasdata(st, 4) ) {
  16983. color = script_getnum(st, 4);
  16984. if( color < 0 || color > 3 ) {
  16985. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16986. script_reportfunc(st);
  16987. color = 0;
  16988. }
  16989. }
  16990. qi.color = (unsigned char)color;
  16991. qi.min_level = 1;
  16992. qi.max_level = MAX_LEVEL;
  16993. q2 = map_add_questinfo(nd->bl.m, &qi);
  16994. q2->req = NULL;
  16995. q2->req_count = 0;
  16996. q2->jobid = NULL;
  16997. q2->jobid_count = 0;
  16998. if(script_hasdata(st, 5)) {
  16999. int job = script_getnum(st, 5);
  17000. if (!pcdb_checkid(job))
  17001. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  17002. else {
  17003. buildin_questinfo_setjob(q2, job);
  17004. }
  17005. }
  17006. return SCRIPT_CMD_SUCCESS;
  17007. }
  17008. /**
  17009. * setquest <ID>{,<char_id>};
  17010. **/
  17011. BUILDIN_FUNC(setquest)
  17012. {
  17013. struct map_session_data *sd;
  17014. int quest_id;
  17015. quest_id = script_getnum(st, 2);
  17016. if (!script_charid2sd(3,sd))
  17017. return SCRIPT_CMD_FAILURE;
  17018. if( quest_add(sd, quest_id) == -1 ){
  17019. script_reportsrc(st);
  17020. script_reportfunc(st);
  17021. }
  17022. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  17023. pc_show_questinfo(sd);
  17024. return SCRIPT_CMD_SUCCESS;
  17025. }
  17026. /**
  17027. * erasequest <ID>{,<char_id>};
  17028. **/
  17029. BUILDIN_FUNC(erasequest)
  17030. {
  17031. struct map_session_data *sd;
  17032. if (!script_charid2sd(3,sd))
  17033. return SCRIPT_CMD_FAILURE;
  17034. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  17035. script_reportsrc(st);
  17036. script_reportfunc(st);
  17037. }
  17038. return SCRIPT_CMD_SUCCESS;
  17039. }
  17040. /**
  17041. * completequest <ID>{,<char_id>};
  17042. **/
  17043. BUILDIN_FUNC(completequest)
  17044. {
  17045. struct map_session_data *sd;
  17046. if (!script_charid2sd(3,sd))
  17047. return SCRIPT_CMD_FAILURE;
  17048. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  17049. //20120410 or 20090218
  17050. pc_show_questinfo(sd);
  17051. return SCRIPT_CMD_SUCCESS;
  17052. }
  17053. /**
  17054. * changequest <ID>,<ID2>{,<char_id>};
  17055. **/
  17056. BUILDIN_FUNC(changequest)
  17057. {
  17058. struct map_session_data *sd;
  17059. if (!script_charid2sd(4,sd))
  17060. return SCRIPT_CMD_FAILURE;
  17061. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  17062. script_reportsrc(st);
  17063. script_reportfunc(st);
  17064. }
  17065. //20120410 or 20090218
  17066. pc_show_questinfo(sd);
  17067. return SCRIPT_CMD_SUCCESS;
  17068. }
  17069. /**
  17070. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  17071. **/
  17072. BUILDIN_FUNC(checkquest)
  17073. {
  17074. struct map_session_data *sd;
  17075. enum quest_check_type type = HAVEQUEST;
  17076. if( script_hasdata(st, 3) )
  17077. type = (enum quest_check_type)script_getnum(st, 3);
  17078. if (!script_charid2sd(4,sd))
  17079. return SCRIPT_CMD_FAILURE;
  17080. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  17081. return SCRIPT_CMD_SUCCESS;
  17082. }
  17083. /**
  17084. * isbegin_quest(<ID>{,<char_id>})
  17085. **/
  17086. BUILDIN_FUNC(isbegin_quest)
  17087. {
  17088. struct map_session_data *sd;
  17089. int i;
  17090. if (!script_charid2sd(3,sd))
  17091. return SCRIPT_CMD_FAILURE;
  17092. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  17093. script_pushint(st, i + (i < 1));
  17094. return SCRIPT_CMD_SUCCESS;
  17095. }
  17096. /**
  17097. * showevent <icon>{,<mark color>{,<char_id>}}
  17098. **/
  17099. BUILDIN_FUNC(showevent)
  17100. {
  17101. TBL_PC *sd;
  17102. struct npc_data *nd = map_id2nd(st->oid);
  17103. int icon, color = 0;
  17104. if (!script_charid2sd(4,sd))
  17105. return SCRIPT_CMD_FAILURE;
  17106. if( sd == NULL || nd == NULL )
  17107. return SCRIPT_CMD_SUCCESS;
  17108. icon = script_getnum(st, 2);
  17109. if( script_hasdata(st, 3) ) {
  17110. color = script_getnum(st, 3);
  17111. if( color < 0 || color > 3 ) {
  17112. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  17113. script_reportfunc(st);
  17114. color = 0;
  17115. }
  17116. }
  17117. #if PACKETVER >= 20120410
  17118. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  17119. icon = 9999; // Default to nothing if icon id is invalid.
  17120. #else
  17121. if(icon < 0 || icon > 7)
  17122. icon = 0;
  17123. else
  17124. icon = icon + 1;
  17125. #endif
  17126. clif_quest_show_event(sd, &nd->bl, icon, color);
  17127. return SCRIPT_CMD_SUCCESS;
  17128. }
  17129. /*==========================================
  17130. * BattleGround System
  17131. *------------------------------------------*/
  17132. BUILDIN_FUNC(waitingroom2bg)
  17133. {
  17134. struct npc_data *nd;
  17135. struct chat_data *cd;
  17136. const char *map_name, *ev = "", *dev = "";
  17137. int x, y, mapindex = 0, bg_id;
  17138. unsigned char i,c=0;
  17139. if( script_hasdata(st,7) )
  17140. nd = npc_name2id(script_getstr(st,7));
  17141. else
  17142. nd = (struct npc_data *)map_id2bl(st->oid);
  17143. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  17144. {
  17145. script_pushint(st,0);
  17146. return SCRIPT_CMD_SUCCESS;
  17147. }
  17148. map_name = script_getstr(st,2);
  17149. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17150. { // Invalid Map
  17151. script_pushint(st, 0);
  17152. return SCRIPT_CMD_SUCCESS;
  17153. }
  17154. x = script_getnum(st,3);
  17155. y = script_getnum(st,4);
  17156. if(script_hasdata(st,5))
  17157. ev = script_getstr(st,5); // Logout Event
  17158. if(script_hasdata(st,6))
  17159. dev = script_getstr(st,6); // Die Event
  17160. check_event(st, ev);
  17161. check_event(st, dev);
  17162. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  17163. { // Creation failed
  17164. script_pushint(st,0);
  17165. return SCRIPT_CMD_SUCCESS;
  17166. }
  17167. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  17168. struct map_session_data *sd;
  17169. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  17170. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  17171. ++c;
  17172. }
  17173. }
  17174. mapreg_setreg(add_str("$@arenamembersnum"), c);
  17175. script_pushint(st,bg_id);
  17176. return SCRIPT_CMD_SUCCESS;
  17177. }
  17178. BUILDIN_FUNC(waitingroom2bg_single)
  17179. {
  17180. const char* map_name;
  17181. struct npc_data *nd;
  17182. struct chat_data *cd;
  17183. struct map_session_data *sd;
  17184. struct battleground_data *bg;
  17185. int x, y, mapindex, bg_id;
  17186. bg_id = script_getnum(st,2);
  17187. if ((bg = bg_team_search(bg_id)) == NULL) {
  17188. script_pushint(st, false);
  17189. return SCRIPT_CMD_SUCCESS;
  17190. }
  17191. if (script_hasdata(st, 3)) {
  17192. map_name = script_getstr(st, 3);
  17193. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17194. script_pushint(st, false);
  17195. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17196. }
  17197. x = script_getnum(st, 4);
  17198. y = script_getnum(st, 5);
  17199. }
  17200. else {
  17201. mapindex = bg->mapindex;
  17202. x = bg->x;
  17203. y = bg->y;
  17204. }
  17205. nd = npc_name2id(script_getstr(st,6));
  17206. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  17207. return SCRIPT_CMD_SUCCESS;
  17208. if( (sd = cd->usersd[0]) == NULL )
  17209. return SCRIPT_CMD_SUCCESS;
  17210. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17211. {
  17212. script_pushint(st, true);
  17213. }
  17214. else
  17215. script_pushint(st, false);
  17216. return SCRIPT_CMD_SUCCESS;
  17217. }
  17218. /// Creates an instance of battleground battle group.
  17219. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  17220. /// @author [secretdataz]
  17221. BUILDIN_FUNC(bg_create) {
  17222. const char *map_name, *ev = "", *dev = "";
  17223. int x, y, mapindex = 0;
  17224. map_name = script_getstr(st, 2);
  17225. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  17226. { // Invalid Map
  17227. script_pushint(st, 0);
  17228. return SCRIPT_CMD_SUCCESS;
  17229. }
  17230. x = script_getnum(st, 3);
  17231. y = script_getnum(st, 4);
  17232. if(script_hasdata(st, 5))
  17233. ev = script_getstr(st, 5); // Logout Event
  17234. if(script_hasdata(st, 6))
  17235. dev = script_getstr(st, 6); // Die Event
  17236. check_event(st, ev);
  17237. check_event(st, dev);
  17238. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  17239. return SCRIPT_CMD_SUCCESS;
  17240. }
  17241. /// Adds attached player or <char id> (if specified) to an existing
  17242. /// battleground group and warps it to the specified coordinates on
  17243. /// the given map.
  17244. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  17245. /// @author [secretdataz]
  17246. BUILDIN_FUNC(bg_join) {
  17247. const char* map_name;
  17248. struct map_session_data *sd;
  17249. struct battleground_data *bg;
  17250. int x, y, bg_id, mapindex;
  17251. bg_id = script_getnum(st, 2);
  17252. if ((bg = bg_team_search(bg_id)) == NULL) {
  17253. script_pushint(st, false);
  17254. return SCRIPT_CMD_SUCCESS;
  17255. }
  17256. if (script_hasdata(st, 3)) {
  17257. map_name = script_getstr(st, 3);
  17258. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  17259. script_pushint(st, false);
  17260. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17261. }
  17262. x = script_getnum(st, 4);
  17263. y = script_getnum(st, 5);
  17264. } else {
  17265. mapindex = bg->mapindex;
  17266. x = bg->x;
  17267. y = bg->y;
  17268. }
  17269. if (!script_charid2sd(6, sd)) {
  17270. script_pushint(st, false);
  17271. return SCRIPT_CMD_FAILURE;
  17272. }
  17273. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  17274. {
  17275. script_pushint(st, true);
  17276. }
  17277. else
  17278. script_pushint(st, false);
  17279. return SCRIPT_CMD_SUCCESS;
  17280. }
  17281. BUILDIN_FUNC(bg_team_setxy)
  17282. {
  17283. struct battleground_data *bg;
  17284. int bg_id;
  17285. bg_id = script_getnum(st,2);
  17286. if( (bg = bg_team_search(bg_id)) == NULL )
  17287. return SCRIPT_CMD_SUCCESS;
  17288. bg->x = script_getnum(st,3);
  17289. bg->y = script_getnum(st,4);
  17290. return SCRIPT_CMD_SUCCESS;
  17291. }
  17292. BUILDIN_FUNC(bg_warp)
  17293. {
  17294. int x, y, mapindex, bg_id;
  17295. const char* map_name;
  17296. bg_id = script_getnum(st,2);
  17297. map_name = script_getstr(st,3);
  17298. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  17299. return SCRIPT_CMD_SUCCESS; // Invalid Map
  17300. x = script_getnum(st,4);
  17301. y = script_getnum(st,5);
  17302. bg_team_warp(bg_id, mapindex, x, y);
  17303. return SCRIPT_CMD_SUCCESS;
  17304. }
  17305. BUILDIN_FUNC(bg_monster)
  17306. {
  17307. int class_ = 0, x = 0, y = 0, bg_id = 0;
  17308. const char *str,*mapname, *evt="";
  17309. bg_id = script_getnum(st,2);
  17310. mapname = script_getstr(st,3);
  17311. x = script_getnum(st,4);
  17312. y = script_getnum(st,5);
  17313. str = script_getstr(st,6);
  17314. class_ = script_getnum(st,7);
  17315. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  17316. check_event(st, evt);
  17317. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  17318. return SCRIPT_CMD_SUCCESS;
  17319. }
  17320. BUILDIN_FUNC(bg_monster_set_team)
  17321. {
  17322. struct mob_data *md;
  17323. struct block_list *mbl;
  17324. int id = script_getnum(st,2),
  17325. bg_id = script_getnum(st,3);
  17326. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  17327. return SCRIPT_CMD_SUCCESS;
  17328. md = (TBL_MOB *)mbl;
  17329. md->bg_id = bg_id;
  17330. mob_stop_attack(md);
  17331. mob_stop_walking(md, 0);
  17332. md->target_id = md->attacked_id = 0;
  17333. clif_name_area(&md->bl);
  17334. return SCRIPT_CMD_SUCCESS;
  17335. }
  17336. BUILDIN_FUNC(bg_leave)
  17337. {
  17338. struct map_session_data *sd = NULL;
  17339. if( !script_charid2sd(2,sd) || !sd->bg_id )
  17340. return SCRIPT_CMD_SUCCESS;
  17341. bg_team_leave(sd,0);
  17342. return SCRIPT_CMD_SUCCESS;
  17343. }
  17344. BUILDIN_FUNC(bg_destroy)
  17345. {
  17346. int bg_id = script_getnum(st,2);
  17347. bg_team_delete(bg_id);
  17348. return SCRIPT_CMD_SUCCESS;
  17349. }
  17350. BUILDIN_FUNC(bg_getareausers)
  17351. {
  17352. const char *str;
  17353. int16 m, x0, y0, x1, y1;
  17354. int bg_id;
  17355. int i = 0, c = 0;
  17356. struct battleground_data *bg = NULL;
  17357. bg_id = script_getnum(st,2);
  17358. str = script_getstr(st,3);
  17359. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  17360. {
  17361. script_pushint(st,0);
  17362. return SCRIPT_CMD_SUCCESS;
  17363. }
  17364. x0 = script_getnum(st,4);
  17365. y0 = script_getnum(st,5);
  17366. x1 = script_getnum(st,6);
  17367. y1 = script_getnum(st,7);
  17368. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  17369. {
  17370. struct map_session_data *sd;
  17371. if( (sd = bg->members[i].sd) == NULL )
  17372. continue;
  17373. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  17374. continue;
  17375. c++;
  17376. }
  17377. script_pushint(st,c);
  17378. return SCRIPT_CMD_SUCCESS;
  17379. }
  17380. BUILDIN_FUNC(bg_updatescore)
  17381. {
  17382. const char *str;
  17383. int16 m;
  17384. str = script_getstr(st,2);
  17385. if( (m = map_mapname2mapid(str)) < 0 )
  17386. return SCRIPT_CMD_SUCCESS;
  17387. map[m].bgscore_lion = script_getnum(st,3);
  17388. map[m].bgscore_eagle = script_getnum(st,4);
  17389. clif_bg_updatescore(m);
  17390. return SCRIPT_CMD_SUCCESS;
  17391. }
  17392. BUILDIN_FUNC(bg_get_data)
  17393. {
  17394. struct battleground_data *bg;
  17395. int bg_id = script_getnum(st,2),
  17396. type = script_getnum(st,3), i;
  17397. if( (bg = bg_team_search(bg_id)) == NULL )
  17398. {
  17399. script_pushint(st,0);
  17400. return SCRIPT_CMD_SUCCESS;
  17401. }
  17402. switch( type )
  17403. {
  17404. case 0: script_pushint(st, bg->count); break;
  17405. case 1:
  17406. for (i = 0; bg->members[i].sd != NULL; i++)
  17407. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), bg->members[i].sd->bl.id);
  17408. mapreg_setreg(add_str("$@arenamemberscount"), i);
  17409. script_pushint(st, i);
  17410. break;
  17411. default:
  17412. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  17413. break;
  17414. }
  17415. return SCRIPT_CMD_SUCCESS;
  17416. }
  17417. /*==========================================
  17418. * Instancing System
  17419. *------------------------------------------*/
  17420. //Returns an Instance ID
  17421. //Checks NPC first, then if player is attached we check
  17422. unsigned short script_instancegetid(struct script_state* st)
  17423. {
  17424. unsigned short instance_id = 0;
  17425. struct npc_data *nd;
  17426. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  17427. instance_id = nd->instance_id;
  17428. else {
  17429. struct map_session_data *sd = NULL;
  17430. struct party_data *pd = NULL;
  17431. struct guild *gd = NULL;
  17432. struct clan *cd = NULL;
  17433. if ((sd = map_id2sd(st->rid))) {
  17434. if (sd->instance_id)
  17435. instance_id = sd->instance_id;
  17436. if (instance_id == 0 && sd->status.party_id && (pd = party_search(sd->status.party_id)) != NULL && pd->instance_id)
  17437. instance_id = pd->instance_id;
  17438. if (instance_id == 0 && sd->status.guild_id && (gd = guild_search(sd->status.guild_id)) != NULL && gd->instance_id)
  17439. instance_id = gd->instance_id;
  17440. if (instance_id == 0 && sd->status.clan_id && (cd = clan_search(sd->status.clan_id)) != NULL && cd->instance_id)
  17441. instance_id = cd->instance_id;
  17442. }
  17443. }
  17444. return instance_id;
  17445. }
  17446. /*==========================================
  17447. * Creates the instance
  17448. * Returns Instance ID if created successfully
  17449. *------------------------------------------*/
  17450. BUILDIN_FUNC(instance_create)
  17451. {
  17452. enum instance_mode mode = IM_PARTY;
  17453. int owner_id = 0;
  17454. if (script_hasdata(st, 3)) {
  17455. mode = static_cast<instance_mode>(script_getnum(st, 3));
  17456. if (mode < IM_NONE || mode >= IM_MAX) {
  17457. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  17458. return SCRIPT_CMD_FAILURE;
  17459. }
  17460. }
  17461. if (script_hasdata(st, 4))
  17462. owner_id = script_getnum(st, 4);
  17463. else {
  17464. // If sd is NULL, instance_create will return -2.
  17465. struct map_session_data *sd = NULL;
  17466. switch(mode) {
  17467. case IM_NONE:
  17468. owner_id = st->oid;
  17469. break;
  17470. case IM_CHAR:
  17471. if (script_rid2sd(sd))
  17472. owner_id = sd->status.char_id;
  17473. break;
  17474. case IM_PARTY:
  17475. if (script_rid2sd(sd))
  17476. owner_id = sd->status.party_id;
  17477. break;
  17478. case IM_GUILD:
  17479. if (script_rid2sd(sd))
  17480. owner_id = sd->status.guild_id;
  17481. break;
  17482. case IM_CLAN:
  17483. if (script_rid2sd(sd))
  17484. owner_id = sd->status.clan_id;
  17485. break;
  17486. default:
  17487. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  17488. return SCRIPT_CMD_FAILURE;
  17489. }
  17490. }
  17491. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  17492. return SCRIPT_CMD_SUCCESS;
  17493. }
  17494. /*==========================================
  17495. * Destroys an instance (unofficial)
  17496. * Officially instances are only destroyed by timeout
  17497. *
  17498. * instance_destroy {<instance_id>};
  17499. *------------------------------------------*/
  17500. BUILDIN_FUNC(instance_destroy)
  17501. {
  17502. unsigned short instance_id;
  17503. if( script_hasdata(st,2) )
  17504. instance_id = script_getnum(st,2);
  17505. else
  17506. instance_id = script_instancegetid(st);
  17507. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  17508. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  17509. return SCRIPT_CMD_FAILURE;
  17510. }
  17511. instance_destroy(instance_id);
  17512. return SCRIPT_CMD_SUCCESS;
  17513. }
  17514. /*==========================================
  17515. * Warps player to instance
  17516. * Results:
  17517. * IE_OK: Success
  17518. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  17519. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  17520. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  17521. *------------------------------------------*/
  17522. BUILDIN_FUNC(instance_enter)
  17523. {
  17524. struct map_session_data *sd = NULL;
  17525. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  17526. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  17527. unsigned short instance_id;
  17528. if (script_hasdata(st, 6))
  17529. instance_id = script_getnum(st, 6);
  17530. else
  17531. instance_id = script_instancegetid(st);
  17532. if (!script_charid2sd(5,sd))
  17533. return SCRIPT_CMD_FAILURE;
  17534. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  17535. return SCRIPT_CMD_SUCCESS;
  17536. }
  17537. /*==========================================
  17538. * Returns the name of a duplicated NPC
  17539. *
  17540. * instance_npcname <npc_name>{,<instance_id>};
  17541. * <npc_name> is the full name of an NPC.
  17542. *------------------------------------------*/
  17543. BUILDIN_FUNC(instance_npcname)
  17544. {
  17545. const char *str;
  17546. unsigned short instance_id = 0;
  17547. struct npc_data *nd;
  17548. str = script_getstr(st,2);
  17549. if( script_hasdata(st,3) )
  17550. instance_id = script_getnum(st,3);
  17551. else
  17552. instance_id = script_instancegetid(st);
  17553. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  17554. static char npcname[NAME_LENGTH];
  17555. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  17556. script_pushconststr(st,npcname);
  17557. } else {
  17558. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  17559. st->state = END;
  17560. return SCRIPT_CMD_FAILURE;
  17561. }
  17562. return SCRIPT_CMD_SUCCESS;
  17563. }
  17564. /*==========================================
  17565. * Returns the name of a duplicated map
  17566. *
  17567. * instance_mapname <map_name>{,<instance_id>};
  17568. *------------------------------------------*/
  17569. BUILDIN_FUNC(instance_mapname)
  17570. {
  17571. const char *str;
  17572. int16 m;
  17573. unsigned short instance_id = 0;
  17574. str = script_getstr(st,2);
  17575. if( script_hasdata(st,3) )
  17576. instance_id = script_getnum(st,3);
  17577. else
  17578. instance_id = script_instancegetid(st);
  17579. // Check that instance mapname is a valid map
  17580. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  17581. script_pushconststr(st, "");
  17582. else
  17583. script_pushconststr(st, map[m].name);
  17584. return SCRIPT_CMD_SUCCESS;
  17585. }
  17586. /*==========================================
  17587. * Returns an Instance ID
  17588. *------------------------------------------*/
  17589. BUILDIN_FUNC(instance_id)
  17590. {
  17591. script_pushint(st, script_instancegetid(st));
  17592. return SCRIPT_CMD_SUCCESS;
  17593. }
  17594. /*==========================================
  17595. * Warps all players inside an instance
  17596. *
  17597. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  17598. *------------------------------------------*/
  17599. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  17600. {
  17601. unsigned int m = va_arg(ap,unsigned int);
  17602. int x = va_arg(ap,int);
  17603. int y = va_arg(ap,int);
  17604. unsigned short instance_id = va_arg(ap,unsigned int);
  17605. struct map_session_data *sd = NULL;
  17606. int owner_id = 0;
  17607. nullpo_retr(0, bl);
  17608. if (bl->type != BL_PC)
  17609. return 0;
  17610. sd = (TBL_PC *)bl;
  17611. owner_id = instance_data[instance_id].owner_id;
  17612. switch(instance_data[instance_id].mode) {
  17613. case IM_NONE:
  17614. break;
  17615. case IM_CHAR:
  17616. if (sd->status.char_id != owner_id)
  17617. return 0;
  17618. break;
  17619. case IM_PARTY:
  17620. if (sd->status.party_id != owner_id)
  17621. return 0;
  17622. break;
  17623. case IM_GUILD:
  17624. if (sd->status.guild_id != owner_id)
  17625. return 0;
  17626. case IM_CLAN:
  17627. if (sd->status.clan_id != owner_id)
  17628. return 0;
  17629. }
  17630. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  17631. return 1;
  17632. }
  17633. BUILDIN_FUNC(instance_warpall)
  17634. {
  17635. int16 m, i;
  17636. unsigned short instance_id;
  17637. const char *mapn;
  17638. int x, y;
  17639. mapn = script_getstr(st,2);
  17640. x = script_getnum(st,3);
  17641. y = script_getnum(st,4);
  17642. if( script_hasdata(st,5) )
  17643. instance_id = script_getnum(st,5);
  17644. else
  17645. instance_id = script_instancegetid(st);
  17646. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  17647. return SCRIPT_CMD_FAILURE;
  17648. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  17649. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  17650. return SCRIPT_CMD_SUCCESS;
  17651. }
  17652. /*==========================================
  17653. * Broadcasts to all maps inside an instance
  17654. *
  17655. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  17656. * Using 0 for <instance id> will auto-detect the id.
  17657. *------------------------------------------*/
  17658. BUILDIN_FUNC(instance_announce) {
  17659. unsigned short instance_id = script_getnum(st,2);
  17660. const char *mes = script_getstr(st,3);
  17661. int flag = script_getnum(st,4);
  17662. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  17663. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  17664. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  17665. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  17666. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  17667. int i;
  17668. if( instance_id == 0 ) {
  17669. instance_id = script_instancegetid(st);
  17670. }
  17671. if( !instance_id && &instance_data[instance_id] != NULL) {
  17672. ShowError("buildin_instance_announce: Intance is not found.\n");
  17673. return SCRIPT_CMD_FAILURE;
  17674. }
  17675. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  17676. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  17677. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  17678. return SCRIPT_CMD_SUCCESS;
  17679. }
  17680. /*==========================================
  17681. * instance_check_party [malufett]
  17682. * Values:
  17683. * party_id : Party ID of the invoking character. [Required Parameter]
  17684. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  17685. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17686. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17687. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  17688. * Example 2: instance_check_party (getcharid(1),1,1,99);
  17689. *------------------------------------------*/
  17690. BUILDIN_FUNC(instance_check_party)
  17691. {
  17692. int amount, min, max, i, party_id, c = 0;
  17693. struct party_data *p = NULL;
  17694. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  17695. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17696. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17697. if( min < 1 || min > MAX_LEVEL) {
  17698. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  17699. return SCRIPT_CMD_FAILURE;
  17700. } else if( max < 1 || max > MAX_LEVEL) {
  17701. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  17702. return SCRIPT_CMD_FAILURE;
  17703. }
  17704. if( script_hasdata(st,2) )
  17705. party_id = script_getnum(st,2);
  17706. else return SCRIPT_CMD_FAILURE;
  17707. if( !(p = party_search(party_id)) ) {
  17708. script_pushint(st, 0); // Returns false if party does not exist.
  17709. return SCRIPT_CMD_FAILURE;
  17710. }
  17711. for( i = 0; i < MAX_PARTY; i++ ) {
  17712. struct map_session_data *pl_sd;
  17713. if( (pl_sd = p->data[i].sd) )
  17714. if(map_id2bl(pl_sd->bl.id)) {
  17715. if(pl_sd->status.base_level < min) {
  17716. script_pushint(st, 0);
  17717. return SCRIPT_CMD_SUCCESS;
  17718. } else if(pl_sd->status.base_level > max) {
  17719. script_pushint(st, 0);
  17720. return SCRIPT_CMD_SUCCESS;
  17721. }
  17722. c++;
  17723. }
  17724. }
  17725. if(c < amount)
  17726. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  17727. else
  17728. script_pushint(st, 1);
  17729. return SCRIPT_CMD_SUCCESS;
  17730. }
  17731. /*==========================================
  17732. * instance_check_guild
  17733. * Values:
  17734. * guild_id : Guild ID of the invoking character. [Required Parameter]
  17735. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  17736. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17737. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17738. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  17739. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  17740. *------------------------------------------*/
  17741. BUILDIN_FUNC(instance_check_guild)
  17742. {
  17743. int amount, min, max, i, guild_id = 0, c = 0;
  17744. struct guild *g = NULL;
  17745. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  17746. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17747. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17748. if (min < 1 || min > MAX_LEVEL) {
  17749. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  17750. return SCRIPT_CMD_FAILURE;
  17751. } else if (max < 1 || max > MAX_LEVEL) {
  17752. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  17753. return SCRIPT_CMD_FAILURE;
  17754. }
  17755. if (script_hasdata(st,2))
  17756. guild_id = script_getnum(st,2);
  17757. else
  17758. return SCRIPT_CMD_FAILURE;
  17759. if (!(g = guild_search(guild_id))) {
  17760. script_pushint(st, 0); // Returns false if guild does not exist.
  17761. return SCRIPT_CMD_FAILURE;
  17762. }
  17763. for(i = 0; i < MAX_GUILD; i++) {
  17764. struct map_session_data *pl_sd;
  17765. if ((pl_sd = g->member[i].sd)) {
  17766. if (map_id2bl(pl_sd->bl.id)) {
  17767. if (pl_sd->status.base_level < min) {
  17768. script_pushint(st, 0);
  17769. return SCRIPT_CMD_SUCCESS;
  17770. } else if (pl_sd->status.base_level > max) {
  17771. script_pushint(st, 0);
  17772. return SCRIPT_CMD_SUCCESS;
  17773. }
  17774. c++;
  17775. }
  17776. }
  17777. }
  17778. if (c < amount)
  17779. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  17780. else
  17781. script_pushint(st, 1);
  17782. return SCRIPT_CMD_SUCCESS;
  17783. }
  17784. /*==========================================
  17785. * instance_check_clan
  17786. * Values:
  17787. * clan_id : Clan ID of the invoking character. [Required Parameter]
  17788. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  17789. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17790. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17791. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  17792. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  17793. *------------------------------------------*/
  17794. BUILDIN_FUNC(instance_check_clan)
  17795. {
  17796. int amount, min, max, i, clan_id = 0, c = 0;
  17797. struct clan *cd = NULL;
  17798. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  17799. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17800. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17801. if (min < 1 || min > MAX_LEVEL) {
  17802. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  17803. return SCRIPT_CMD_FAILURE;
  17804. } else if (max < 1 || max > MAX_LEVEL) {
  17805. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  17806. return SCRIPT_CMD_FAILURE;
  17807. }
  17808. if (script_hasdata(st,2))
  17809. clan_id = script_getnum(st,2);
  17810. else
  17811. return SCRIPT_CMD_FAILURE;
  17812. if (!(cd = clan_search(clan_id))) {
  17813. script_pushint(st, 0); // Returns false if clan does not exist.
  17814. return SCRIPT_CMD_FAILURE;
  17815. }
  17816. for(i = 0; i < MAX_CLAN; i++) {
  17817. struct map_session_data *pl_sd;
  17818. if ((pl_sd = cd->members[i])) {
  17819. if (map_id2bl(pl_sd->bl.id)) {
  17820. if (pl_sd->status.base_level < min) {
  17821. script_pushint(st, 0);
  17822. return SCRIPT_CMD_SUCCESS;
  17823. } else if (pl_sd->status.base_level > max) {
  17824. script_pushint(st, 0);
  17825. return SCRIPT_CMD_SUCCESS;
  17826. }
  17827. c++;
  17828. }
  17829. }
  17830. }
  17831. if (c < amount)
  17832. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  17833. else
  17834. script_pushint(st, 1);
  17835. return SCRIPT_CMD_SUCCESS;
  17836. }
  17837. /*==========================================
  17838. * instance_info
  17839. * Values:
  17840. * name : name of the instance you want to look up. [Required Parameter]
  17841. * type : type of information you want to look up for the specified instance. [Required Parameter]
  17842. * index : index of the map in the instance. [Optional Parameter]
  17843. *------------------------------------------*/
  17844. BUILDIN_FUNC(instance_info)
  17845. {
  17846. const char* name = script_getstr(st, 2);
  17847. int type = script_getnum(st, 3);
  17848. int index = 0;
  17849. struct instance_db *db = instance_searchname_db(name);
  17850. if( !db ){
  17851. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  17852. script_pushint(st, -1);
  17853. return SCRIPT_CMD_FAILURE;
  17854. }
  17855. switch( type ){
  17856. case IIT_ID:
  17857. script_pushint(st, db->id);
  17858. break;
  17859. case IIT_TIME_LIMIT:
  17860. script_pushint(st, db->limit);
  17861. break;
  17862. case IIT_IDLE_TIMEOUT:
  17863. script_pushint(st, db->timeout);
  17864. break;
  17865. case IIT_ENTER_MAP:
  17866. script_pushstrcopy(st, StringBuf_Value(db->enter.mapname));
  17867. break;
  17868. case IIT_ENTER_X:
  17869. script_pushint(st, db->enter.x);
  17870. break;
  17871. case IIT_ENTER_Y:
  17872. script_pushint(st, db->enter.y);
  17873. break;
  17874. case IIT_MAPCOUNT:
  17875. script_pushint(st, db->maplist_count);
  17876. break;
  17877. case IIT_MAP:
  17878. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  17879. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  17880. script_pushconststr(st, "");
  17881. return SCRIPT_CMD_FAILURE;
  17882. }
  17883. index = script_getnum(st, 4);
  17884. if( index < 0 ){
  17885. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  17886. script_pushconststr(st, "");
  17887. return SCRIPT_CMD_FAILURE;
  17888. }
  17889. if( index > UINT8_MAX ){
  17890. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  17891. script_pushconststr(st, "");
  17892. return SCRIPT_CMD_FAILURE;
  17893. }
  17894. script_pushstrcopy(st, StringBuf_Value(db->maplist[index]));
  17895. break;
  17896. default:
  17897. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  17898. script_pushint(st, -1);
  17899. return SCRIPT_CMD_FAILURE;
  17900. }
  17901. return SCRIPT_CMD_SUCCESS;
  17902. }
  17903. /*==========================================
  17904. * Custom Fonts
  17905. *------------------------------------------*/
  17906. BUILDIN_FUNC(setfont)
  17907. {
  17908. struct map_session_data *sd;
  17909. int font;
  17910. if( !script_rid2sd(sd) )
  17911. return SCRIPT_CMD_SUCCESS;
  17912. font = script_getnum(st,2);
  17913. if( sd->status.font != font )
  17914. sd->status.font = font;
  17915. else
  17916. sd->status.font = 0;
  17917. clif_font(sd);
  17918. return SCRIPT_CMD_SUCCESS;
  17919. }
  17920. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17921. {
  17922. TBL_MOB* md = (TBL_MOB*)bl;
  17923. struct block_list *tbl;
  17924. int mobid = va_arg(ap,int);
  17925. uint16 skill_id = va_arg(ap,int);
  17926. uint16 skill_lv = va_arg(ap,int);
  17927. int casttime = va_arg(ap,int);
  17928. int cancel = va_arg(ap,int);
  17929. int emotion = va_arg(ap,int);
  17930. int target = va_arg(ap,int);
  17931. if( md->mob_id != mobid )
  17932. return 0;
  17933. // 0:self, 1:target, 2:master, default:random
  17934. switch( target ) {
  17935. case 0: tbl = map_id2bl(md->bl.id); break;
  17936. case 1: tbl = map_id2bl(md->target_id); break;
  17937. case 2: tbl = map_id2bl(md->master_id); break;
  17938. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17939. }
  17940. if( !tbl )
  17941. return 0;
  17942. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17943. unit_skillcastcancel(bl,0);
  17944. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17945. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17946. else
  17947. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17948. clif_emotion(&md->bl, emotion);
  17949. return 1;
  17950. }
  17951. /*==========================================
  17952. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17953. *------------------------------------------*/
  17954. BUILDIN_FUNC(areamobuseskill)
  17955. {
  17956. struct block_list center;
  17957. struct script_data *data;
  17958. int16 m;
  17959. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17960. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17961. ShowError("areamobuseskill: invalid map name.\n");
  17962. return SCRIPT_CMD_SUCCESS;
  17963. }
  17964. center.m = m;
  17965. center.x = script_getnum(st,3);
  17966. center.y = script_getnum(st,4);
  17967. range = script_getnum(st,5);
  17968. mobid = script_getnum(st,6);
  17969. data = script_getdata(st, 7);
  17970. get_val(st, data); // Convert into value in case of a variable
  17971. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17972. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17973. skill_lv = battle_config.mob_max_skilllvl;
  17974. casttime = script_getnum(st,9);
  17975. cancel = script_getnum(st,10);
  17976. emotion = script_getnum(st,11);
  17977. target = script_getnum(st,12);
  17978. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17979. return SCRIPT_CMD_SUCCESS;
  17980. }
  17981. /**
  17982. * Display a progress bar above a character
  17983. * progressbar "<color>",<seconds>;
  17984. */
  17985. BUILDIN_FUNC(progressbar)
  17986. {
  17987. struct map_session_data * sd;
  17988. const char * color;
  17989. unsigned int second;
  17990. if( !script_rid2sd(sd) )
  17991. return SCRIPT_CMD_SUCCESS;
  17992. st->state = STOP;
  17993. color = script_getstr(st,2);
  17994. second = script_getnum(st,3);
  17995. sd->progressbar.npc_id = st->oid;
  17996. sd->progressbar.timeout = gettick() + second*1000;
  17997. sd->state.workinprogress = WIP_DISABLE_ALL;
  17998. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17999. return SCRIPT_CMD_SUCCESS;
  18000. }
  18001. /**
  18002. * Display a progress bar above an NPC
  18003. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  18004. */
  18005. BUILDIN_FUNC(progressbar_npc){
  18006. struct npc_data* nd = NULL;
  18007. if( script_hasdata(st, 4) ){
  18008. const char* name = script_getstr(st, 4);
  18009. nd = npc_name2id(name);
  18010. if( !nd ){
  18011. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  18012. return SCRIPT_CMD_FAILURE;
  18013. }
  18014. }else{
  18015. nd = map_id2nd(st->oid);
  18016. }
  18017. // First call(by function call)
  18018. if( !nd->progressbar.timeout ){
  18019. const char *color;
  18020. int second;
  18021. color = script_getstr(st, 2);
  18022. second = script_getnum(st, 3);
  18023. if( second < 0 ){
  18024. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  18025. return SCRIPT_CMD_FAILURE;
  18026. }
  18027. // detach the player
  18028. script_detach_rid(st);
  18029. // sleep for the target amount of time
  18030. st->state = RERUNLINE;
  18031. st->sleep.tick = second * 1000;
  18032. nd->progressbar.timeout = gettick() + second * 1000;
  18033. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  18034. clif_progressbar_npc_area(nd);
  18035. // Second call(by timer after sleeping time is over)
  18036. } else {
  18037. // Continue the script
  18038. st->state = RUN;
  18039. st->sleep.tick = 0;
  18040. nd->progressbar.timeout = nd->progressbar.color = 0;
  18041. }
  18042. return SCRIPT_CMD_SUCCESS;
  18043. }
  18044. BUILDIN_FUNC(pushpc)
  18045. {
  18046. uint8 dir;
  18047. int cells, dx, dy;
  18048. struct map_session_data* sd;
  18049. if(!script_rid2sd(sd))
  18050. {
  18051. return SCRIPT_CMD_SUCCESS;
  18052. }
  18053. dir = script_getnum(st,2);
  18054. cells = script_getnum(st,3);
  18055. if(dir >= DIR_MAX)
  18056. {
  18057. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  18058. script_reportsrc(st);
  18059. dir%= DIR_MAX; // trim spin-over
  18060. }
  18061. if(!cells)
  18062. {// zero distance
  18063. return SCRIPT_CMD_SUCCESS;
  18064. }
  18065. else if(cells<0)
  18066. {// pushing backwards
  18067. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  18068. cells = -cells;
  18069. }
  18070. dx = dirx[dir];
  18071. dy = diry[dir];
  18072. unit_blown(&sd->bl, dx, dy, cells, BLOWN_NONE);
  18073. return SCRIPT_CMD_SUCCESS;
  18074. }
  18075. /// Invokes buying store preparation window
  18076. /// buyingstore <slots>;
  18077. BUILDIN_FUNC(buyingstore)
  18078. {
  18079. struct map_session_data* sd;
  18080. if( !script_rid2sd(sd) )
  18081. {
  18082. return SCRIPT_CMD_SUCCESS;
  18083. }
  18084. if( npc_isnear(&sd->bl) ) {
  18085. char output[150];
  18086. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  18087. clif_displaymessage(sd->fd, output);
  18088. return SCRIPT_CMD_SUCCESS;
  18089. }
  18090. buyingstore_setup(sd, script_getnum(st,2));
  18091. return SCRIPT_CMD_SUCCESS;
  18092. }
  18093. /// Invokes search store info window
  18094. /// searchstores <uses>,<effect>;
  18095. BUILDIN_FUNC(searchstores)
  18096. {
  18097. unsigned short effect;
  18098. unsigned int uses;
  18099. struct map_session_data* sd;
  18100. if( !script_rid2sd(sd) )
  18101. {
  18102. return SCRIPT_CMD_SUCCESS;
  18103. }
  18104. uses = script_getnum(st,2);
  18105. effect = script_getnum(st,3);
  18106. if( !uses )
  18107. {
  18108. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  18109. return SCRIPT_CMD_FAILURE;
  18110. }
  18111. if( effect > 1 )
  18112. {
  18113. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  18114. return SCRIPT_CMD_FAILURE;
  18115. }
  18116. searchstore_open(sd, uses, effect);
  18117. return SCRIPT_CMD_SUCCESS;
  18118. }
  18119. /// Displays a number as large digital clock.
  18120. /// showdigit <value>[,<type>];
  18121. BUILDIN_FUNC(showdigit)
  18122. {
  18123. unsigned int type = 0;
  18124. int value;
  18125. struct map_session_data* sd;
  18126. if( !script_rid2sd(sd) )
  18127. {
  18128. return SCRIPT_CMD_SUCCESS;
  18129. }
  18130. value = script_getnum(st,2);
  18131. if( script_hasdata(st,3) ){
  18132. type = script_getnum(st,3);
  18133. }
  18134. switch( type ){
  18135. case 0:
  18136. break;
  18137. case 1:
  18138. case 2:
  18139. // Use absolute value and then the negative value of it as starting value
  18140. // This is what gravity's client does for these counters
  18141. value = -abs(value);
  18142. break;
  18143. case 3:
  18144. value = abs(value);
  18145. if( value > 99 ){
  18146. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  18147. script_reportsrc(st);
  18148. value = 99;
  18149. }
  18150. break;
  18151. default:
  18152. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  18153. return SCRIPT_CMD_FAILURE;
  18154. }
  18155. clif_showdigit(sd, (unsigned char)type, value);
  18156. return SCRIPT_CMD_SUCCESS;
  18157. }
  18158. /**
  18159. * Rune Knight
  18160. * makerune <% success bonus>{,<char_id>};
  18161. **/
  18162. BUILDIN_FUNC(makerune) {
  18163. TBL_PC* sd;
  18164. if (!script_charid2sd(3,sd))
  18165. return SCRIPT_CMD_FAILURE;
  18166. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  18167. sd->itemid = script_getnum(st,2);
  18168. return SCRIPT_CMD_SUCCESS;
  18169. }
  18170. /**
  18171. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  18172. **/
  18173. BUILDIN_FUNC(checkdragon) {
  18174. TBL_PC* sd;
  18175. if (!script_charid2sd(2,sd))
  18176. return SCRIPT_CMD_FAILURE;
  18177. if( pc_isridingdragon(sd) )
  18178. script_pushint(st,1);
  18179. else
  18180. script_pushint(st,0);
  18181. return SCRIPT_CMD_SUCCESS;
  18182. }
  18183. /**
  18184. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  18185. * - Toggles the dragon on a RK if he can mount;
  18186. * @param Color - when not provided uses the green dragon;
  18187. * - 1 : Green Dragon
  18188. * - 2 : Brown Dragon
  18189. * - 3 : Gray Dragon
  18190. * - 4 : Blue Dragon
  18191. * - 5 : Red Dragon
  18192. **/
  18193. BUILDIN_FUNC(setdragon) {
  18194. TBL_PC* sd;
  18195. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  18196. if (!script_charid2sd(3,sd))
  18197. return SCRIPT_CMD_FAILURE;
  18198. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  18199. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  18200. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  18201. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  18202. script_pushint(st,1);
  18203. } else {//Not mounted; Mount now.
  18204. unsigned int option = OPTION_DRAGON1;
  18205. if( color ) {
  18206. option = ( color == 1 ? OPTION_DRAGON1 :
  18207. color == 2 ? OPTION_DRAGON2 :
  18208. color == 3 ? OPTION_DRAGON3 :
  18209. color == 4 ? OPTION_DRAGON4 :
  18210. color == 5 ? OPTION_DRAGON5 : 0);
  18211. if( !option ) {
  18212. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  18213. option = OPTION_DRAGON1;
  18214. }
  18215. }
  18216. pc_setoption(sd, sd->sc.option|option);
  18217. script_pushint(st,1);
  18218. }
  18219. return SCRIPT_CMD_SUCCESS;
  18220. }
  18221. /**
  18222. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  18223. **/
  18224. BUILDIN_FUNC(ismounting) {
  18225. TBL_PC* sd;
  18226. if (!script_charid2sd(2,sd))
  18227. return SCRIPT_CMD_FAILURE;
  18228. if( sd->sc.data[SC_ALL_RIDING] )
  18229. script_pushint(st,1);
  18230. else
  18231. script_pushint(st,0);
  18232. return SCRIPT_CMD_SUCCESS;
  18233. }
  18234. /**
  18235. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  18236. * - Toggles new mounts on a player when he can mount
  18237. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  18238. * - Will unmount the player is he is already mounting
  18239. **/
  18240. BUILDIN_FUNC(setmounting) {
  18241. TBL_PC* sd;
  18242. if (!script_charid2sd(2,sd))
  18243. return SCRIPT_CMD_FAILURE;
  18244. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  18245. clif_msg(sd, NEED_REINS_OF_MOUNT);
  18246. script_pushint(st,0); //can't mount with one of these
  18247. } else {
  18248. if( sd->sc.data[SC_ALL_RIDING] )
  18249. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  18250. else
  18251. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  18252. script_pushint(st,1);//in both cases, return 1.
  18253. }
  18254. return SCRIPT_CMD_SUCCESS;
  18255. }
  18256. /**
  18257. * Retrieves quantity of arguments provided to callfunc/callsub.
  18258. * getargcount() -> amount of arguments received in a function
  18259. **/
  18260. BUILDIN_FUNC(getargcount) {
  18261. struct script_retinfo* ri;
  18262. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  18263. ShowError("script:getargcount: used out of function or callsub label!\n");
  18264. st->state = END;
  18265. return SCRIPT_CMD_FAILURE;
  18266. }
  18267. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  18268. script_pushint(st, ri->nargs);
  18269. return SCRIPT_CMD_SUCCESS;
  18270. }
  18271. /**
  18272. * getcharip(<account ID>/<character ID>/<character name>)
  18273. **/
  18274. BUILDIN_FUNC(getcharip)
  18275. {
  18276. struct map_session_data* sd = NULL;
  18277. /* check if a character name is specified */
  18278. if( script_hasdata(st, 2) )
  18279. {
  18280. struct script_data *data;
  18281. data = script_getdata(st, 2);
  18282. get_val(st, data); // Convert into value in case of a variable
  18283. if (data_isstring(data))
  18284. sd = map_nick2sd(script_getstr(st, 2),false);
  18285. else if (data_isint(data) || script_getnum(st, 2))
  18286. {
  18287. int id = 0;
  18288. id = script_getnum(st, 2);
  18289. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  18290. }
  18291. }
  18292. else
  18293. script_rid2sd(sd);
  18294. /* check for sd and IP */
  18295. if (!sd || !session[sd->fd]->client_addr)
  18296. {
  18297. script_pushconststr(st, "");
  18298. return SCRIPT_CMD_SUCCESS;
  18299. }
  18300. /* return the client ip_addr converted for output */
  18301. if (sd && sd->fd && session[sd->fd])
  18302. {
  18303. /* initiliaze */
  18304. const char *ip_addr = NULL;
  18305. uint32 ip;
  18306. /* set ip, ip_addr and convert to ip and push str */
  18307. ip = session[sd->fd]->client_addr;
  18308. ip_addr = ip2str(ip, NULL);
  18309. script_pushstrcopy(st, ip_addr);
  18310. }
  18311. return SCRIPT_CMD_SUCCESS;
  18312. }
  18313. /**
  18314. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  18315. **/
  18316. BUILDIN_FUNC(is_function) {
  18317. const char* str = script_getstr(st,2);
  18318. if( strdb_exists(userfunc_db, str) )
  18319. script_pushint(st,1);
  18320. else
  18321. script_pushint(st,0);
  18322. return SCRIPT_CMD_SUCCESS;
  18323. }
  18324. /**
  18325. * get_revision() -> retrieves the current svn revision (if available)
  18326. **/
  18327. BUILDIN_FUNC(get_revision) {
  18328. const char *svn = get_svn_revision();
  18329. if ( svn[0] != UNKNOWN_VERSION )
  18330. script_pushint(st,atoi(svn));
  18331. else
  18332. script_pushint(st,-1); //unknown
  18333. return SCRIPT_CMD_SUCCESS;
  18334. }
  18335. /* get_hash() -> retrieves the current git hash (if available)*/
  18336. BUILDIN_FUNC(get_githash) {
  18337. const char* git = get_git_hash();
  18338. char buf[CHAT_SIZE_MAX];
  18339. safestrncpy(buf,git,strlen(git)+1);
  18340. if ( git[0] != UNKNOWN_VERSION )
  18341. script_pushstr(st,buf);
  18342. else
  18343. script_pushconststr(st,"Unknown"); //unknown
  18344. return SCRIPT_CMD_SUCCESS;
  18345. }
  18346. /**
  18347. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  18348. **/
  18349. BUILDIN_FUNC(freeloop) {
  18350. if( script_hasdata(st,2) ) {
  18351. if( script_getnum(st,2) )
  18352. st->freeloop = 1;
  18353. else
  18354. st->freeloop = 0;
  18355. }
  18356. script_pushint(st, st->freeloop);
  18357. return SCRIPT_CMD_SUCCESS;
  18358. }
  18359. /**
  18360. * @commands (script based)
  18361. **/
  18362. BUILDIN_FUNC(bindatcmd) {
  18363. const char* atcmd;
  18364. const char* eventName;
  18365. int i, level = 0, level2 = 100;
  18366. bool create = false;
  18367. atcmd = script_getstr(st,2);
  18368. eventName = script_getstr(st,3);
  18369. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18370. atcmd++;
  18371. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  18372. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  18373. if( atcmd_binding_count == 0 ) {
  18374. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  18375. create = true;
  18376. } else {
  18377. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  18378. if( i < atcmd_binding_count ) {/* update existent entry */
  18379. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18380. atcmd_binding[i]->level = level;
  18381. atcmd_binding[i]->level2 = level2;
  18382. } else
  18383. create = true;
  18384. }
  18385. if( create ) {
  18386. i = atcmd_binding_count;
  18387. if( atcmd_binding_count++ != 0 )
  18388. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  18389. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  18390. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  18391. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18392. atcmd_binding[i]->level = level;
  18393. atcmd_binding[i]->level2 = level2;
  18394. }
  18395. return SCRIPT_CMD_SUCCESS;
  18396. }
  18397. BUILDIN_FUNC(unbindatcmd) {
  18398. const char* atcmd;
  18399. int i = 0;
  18400. atcmd = script_getstr(st, 2);
  18401. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18402. atcmd++;
  18403. if( atcmd_binding_count == 0 ) {
  18404. script_pushint(st, 0);
  18405. return SCRIPT_CMD_SUCCESS;
  18406. }
  18407. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  18408. if( i < atcmd_binding_count ) {
  18409. int cursor = 0;
  18410. aFree(atcmd_binding[i]);
  18411. atcmd_binding[i] = NULL;
  18412. /* compact the list now that we freed a slot somewhere */
  18413. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  18414. if( atcmd_binding[i] == NULL )
  18415. continue;
  18416. if( cursor != i ) {
  18417. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  18418. }
  18419. cursor++;
  18420. }
  18421. if( (atcmd_binding_count = cursor) == 0 )
  18422. aFree(atcmd_binding);
  18423. script_pushint(st, 1);
  18424. } else
  18425. script_pushint(st, 0);/* not found */
  18426. return SCRIPT_CMD_SUCCESS;
  18427. }
  18428. BUILDIN_FUNC(useatcmd) {
  18429. return atcommand_sub(st,3);
  18430. }
  18431. BUILDIN_FUNC(checkre)
  18432. {
  18433. int num;
  18434. num=script_getnum(st,2);
  18435. switch(num){
  18436. case 0:
  18437. #ifdef RENEWAL
  18438. script_pushint(st, 1);
  18439. #else
  18440. script_pushint(st, 0);
  18441. #endif
  18442. break;
  18443. case 1:
  18444. #ifdef RENEWAL_CAST
  18445. script_pushint(st, 1);
  18446. #else
  18447. script_pushint(st, 0);
  18448. #endif
  18449. break;
  18450. case 2:
  18451. #ifdef RENEWAL_DROP
  18452. script_pushint(st, 1);
  18453. #else
  18454. script_pushint(st, 0);
  18455. #endif
  18456. break;
  18457. case 3:
  18458. #ifdef RENEWAL_EXP
  18459. script_pushint(st, 1);
  18460. #else
  18461. script_pushint(st, 0);
  18462. #endif
  18463. break;
  18464. case 4:
  18465. #ifdef RENEWAL_LVDMG
  18466. script_pushint(st, 1);
  18467. #else
  18468. script_pushint(st, 0);
  18469. #endif
  18470. break;
  18471. case 5:
  18472. #ifdef RENEWAL_ASPD
  18473. script_pushint(st, 1);
  18474. #else
  18475. script_pushint(st, 0);
  18476. #endif
  18477. break;
  18478. default:
  18479. ShowWarning("buildin_checkre: unknown parameter.\n");
  18480. break;
  18481. }
  18482. return SCRIPT_CMD_SUCCESS;
  18483. }
  18484. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>{,<identify>{,<char_id>}}}} */
  18485. BUILDIN_FUNC(getrandgroupitem) {
  18486. TBL_PC* sd;
  18487. int i, get_count = 0, identify = 0;
  18488. uint16 group, qty = 0;
  18489. uint8 sub_group = 1;
  18490. struct item item_tmp;
  18491. struct s_item_group_entry *entry = NULL;
  18492. if (!script_charid2sd(6, sd))
  18493. return SCRIPT_CMD_SUCCESS;
  18494. group = script_getnum(st,2);
  18495. if (!group) {
  18496. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  18497. return SCRIPT_CMD_FAILURE;
  18498. }
  18499. FETCH(3, qty);
  18500. FETCH(4, sub_group);
  18501. FETCH(5, identify);
  18502. entry = itemdb_get_randgroupitem(group,sub_group);
  18503. if (!entry)
  18504. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  18505. memset(&item_tmp,0,sizeof(item_tmp));
  18506. item_tmp.nameid = entry->nameid;
  18507. item_tmp.identify = identify ? 1 : itemdb_isidentified(entry->nameid);
  18508. if (!qty)
  18509. qty = entry->amount;
  18510. //Check if it's stackable.
  18511. if (!itemdb_isstackable(entry->nameid)) {
  18512. item_tmp.amount = 1;
  18513. get_count = qty;
  18514. }
  18515. else {
  18516. item_tmp.amount = qty;
  18517. get_count = 1;
  18518. }
  18519. for (i = 0; i < get_count; i++) {
  18520. // if not pet egg
  18521. if (!pet_create_egg(sd, entry->nameid)) {
  18522. unsigned char flag = 0;
  18523. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  18524. clif_additem(sd,0,0,flag);
  18525. if (pc_candrop(sd,&item_tmp))
  18526. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  18527. }
  18528. }
  18529. }
  18530. return SCRIPT_CMD_SUCCESS;
  18531. }
  18532. /* getgroupitem <group_id>{,<identify>{,<char_id>}};
  18533. * Gives item(s) to the attached player based on item group contents
  18534. */
  18535. BUILDIN_FUNC(getgroupitem) {
  18536. TBL_PC *sd;
  18537. int group_id = script_getnum(st,2);
  18538. if (!script_charid2sd(4,sd))
  18539. return SCRIPT_CMD_SUCCESS;
  18540. if (itemdb_pc_get_itemgroup(group_id, (script_hasdata(st, 3) ? script_getnum(st, 3) != 0 : false), sd)) {
  18541. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  18542. return SCRIPT_CMD_FAILURE;
  18543. }
  18544. return SCRIPT_CMD_SUCCESS;
  18545. }
  18546. /* cleanmap <map_name>;
  18547. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  18548. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  18549. {
  18550. nullpo_ret(bl);
  18551. map_clearflooritem(bl);
  18552. return SCRIPT_CMD_SUCCESS;
  18553. }
  18554. BUILDIN_FUNC(cleanmap)
  18555. {
  18556. const char *mapname;
  18557. int16 m;
  18558. mapname = script_getstr(st, 2);
  18559. m = map_mapname2mapid(mapname);
  18560. if (!m)
  18561. return SCRIPT_CMD_FAILURE;
  18562. if ((script_lastdata(st) - 2) < 4) {
  18563. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  18564. } else {
  18565. int16 x0 = script_getnum(st, 3);
  18566. int16 y0 = script_getnum(st, 4);
  18567. int16 x1 = script_getnum(st, 5);
  18568. int16 y1 = script_getnum(st, 6);
  18569. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  18570. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  18571. } else {
  18572. ShowError("cleanarea: invalid coordinate defined!\n");
  18573. return SCRIPT_CMD_FAILURE;
  18574. }
  18575. }
  18576. return SCRIPT_CMD_SUCCESS;
  18577. }
  18578. /* Cast a skill on the attached player.
  18579. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  18580. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  18581. BUILDIN_FUNC(npcskill)
  18582. {
  18583. uint16 skill_id;
  18584. unsigned short skill_level;
  18585. unsigned int stat_point;
  18586. unsigned int npc_level;
  18587. struct npc_data *nd;
  18588. struct map_session_data *sd;
  18589. struct script_data *data;
  18590. if( !script_rid2sd(sd) )
  18591. return SCRIPT_CMD_SUCCESS;
  18592. data = script_getdata(st, 2);
  18593. get_val(st, data); // Convert into value in case of a variable
  18594. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  18595. skill_level = script_getnum(st, 3);
  18596. stat_point = script_getnum(st, 4);
  18597. npc_level = script_getnum(st, 5);
  18598. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  18599. if (stat_point > battle_config.max_third_parameter) {
  18600. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  18601. return SCRIPT_CMD_FAILURE;
  18602. }
  18603. if (npc_level > MAX_LEVEL) {
  18604. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  18605. return SCRIPT_CMD_FAILURE;
  18606. }
  18607. if (nd == NULL) { //ain't possible, but I don't trust people.
  18608. return SCRIPT_CMD_FAILURE;
  18609. }
  18610. nd->level = npc_level;
  18611. nd->stat_point = stat_point;
  18612. if (!nd->status.hp)
  18613. status_calc_npc(nd, SCO_FIRST);
  18614. else
  18615. status_calc_npc(nd, SCO_NONE);
  18616. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  18617. unit_skilluse_pos2(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level,0,0);
  18618. else
  18619. unit_skilluse_id2(&nd->bl, sd->bl.id, skill_id, skill_level,0,0);
  18620. return SCRIPT_CMD_SUCCESS;
  18621. }
  18622. /* Consumes an item.
  18623. * consumeitem <item id>{,<char_id>};
  18624. * consumeitem "<item name>"{,<char_id>};
  18625. * @param item: Item ID or name
  18626. */
  18627. BUILDIN_FUNC(consumeitem)
  18628. {
  18629. TBL_PC *sd;
  18630. struct script_data *data;
  18631. struct item_data *item_data;
  18632. if (!script_charid2sd(3, sd))
  18633. return SCRIPT_CMD_FAILURE;
  18634. data = script_getdata( st, 2 );
  18635. get_val( st, data );
  18636. if( data_isstring( data ) ){
  18637. const char *name = conv_str( st, data );
  18638. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  18639. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  18640. return SCRIPT_CMD_FAILURE;
  18641. }
  18642. }else if( data_isint( data ) ){
  18643. unsigned short nameid = conv_num( st, data );
  18644. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  18645. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  18646. return SCRIPT_CMD_FAILURE;
  18647. }
  18648. }else{
  18649. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  18650. return SCRIPT_CMD_FAILURE;
  18651. }
  18652. run_script( item_data->script, 0, sd->bl.id, 0 );
  18653. return SCRIPT_CMD_SUCCESS;
  18654. }
  18655. /** Makes a player sit/stand.
  18656. * sit {"<character name>"};
  18657. * stand {"<character name>"};
  18658. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  18659. * @param name: Player name that will be invoked
  18660. */
  18661. BUILDIN_FUNC(sit)
  18662. {
  18663. TBL_PC *sd;
  18664. if( !script_nick2sd(2,sd) )
  18665. return SCRIPT_CMD_FAILURE;
  18666. if( !pc_issit(sd) ) {
  18667. pc_setsit(sd);
  18668. skill_sit(sd, 1);
  18669. clif_sitting(&sd->bl);
  18670. }
  18671. return SCRIPT_CMD_SUCCESS;
  18672. }
  18673. /** Makes player to stand
  18674. * @param name: Player name that will be set
  18675. */
  18676. BUILDIN_FUNC(stand)
  18677. {
  18678. TBL_PC *sd;
  18679. if( !script_nick2sd(2,sd) )
  18680. return SCRIPT_CMD_FAILURE;
  18681. if( pc_issit(sd) && pc_setstand(sd, false)) {
  18682. skill_sit(sd, 0);
  18683. clif_standing(&sd->bl);
  18684. }
  18685. return SCRIPT_CMD_SUCCESS;
  18686. }
  18687. /** Creates an array of bounded item IDs
  18688. * countbound {<type>{,<char_id>}};
  18689. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  18690. * @return amt: Amount of items found
  18691. */
  18692. BUILDIN_FUNC(countbound)
  18693. {
  18694. int i, type, j = 0, k = 0;
  18695. TBL_PC *sd;
  18696. if (!script_charid2sd(3,sd))
  18697. return SCRIPT_CMD_FAILURE;
  18698. type = script_getnum(st,2);
  18699. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  18700. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  18701. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  18702. ))
  18703. {
  18704. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  18705. k++;
  18706. j += sd->inventory.u.items_inventory[i].amount;
  18707. }
  18708. }
  18709. script_pushint(st,j);
  18710. return SCRIPT_CMD_SUCCESS;
  18711. }
  18712. /** Creates new party
  18713. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  18714. * @param party_name: String of party name that will be created
  18715. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  18716. * @param item_share: 0-Each Take. 1-Party Share
  18717. * @param item_share_type: 0-Each Take. 1-Even Share
  18718. * @return val: Result value
  18719. * -3 - party name is exist
  18720. * -2 - player is in party already
  18721. * -1 - player is not found
  18722. * 0 - unknown error
  18723. * 1 - success, will return party id $@party_create_id
  18724. */
  18725. BUILDIN_FUNC(party_create)
  18726. {
  18727. char party_name[NAME_LENGTH];
  18728. int item1 = 0, item2 = 0;
  18729. TBL_PC *sd = NULL;
  18730. if (!script_charid2sd(3, sd)) {
  18731. script_pushint(st,-1);
  18732. return SCRIPT_CMD_FAILURE;
  18733. }
  18734. if( sd->status.party_id ) {
  18735. script_pushint(st,-2);
  18736. return SCRIPT_CMD_FAILURE;
  18737. }
  18738. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  18739. trim(party_name);
  18740. if( party_searchname(party_name) ) {
  18741. script_pushint(st,-3);
  18742. return SCRIPT_CMD_FAILURE;
  18743. }
  18744. if( script_getnum(st,4) )
  18745. item1 = 1;
  18746. if( script_getnum(st,5) )
  18747. item2 = 1;
  18748. party_create_byscript = 1;
  18749. script_pushint(st,party_create(sd,party_name,item1,item2));
  18750. return SCRIPT_CMD_SUCCESS;
  18751. }
  18752. /** Adds player to specified party
  18753. * party_addmember <party id>,<char id>;
  18754. * @param party_id: The party that will be entered by player
  18755. * @param char_id: Char id of player that will be joined to the party
  18756. * @return val: Result value
  18757. * -5 - another character of the same account is in the party
  18758. * -4 - party is full
  18759. * -3 - party is not found
  18760. * -2 - player is in party already
  18761. * -1 - player is not found
  18762. * 0 - unknown error
  18763. * 1 - success
  18764. */
  18765. BUILDIN_FUNC(party_addmember)
  18766. {
  18767. int party_id = script_getnum(st,2);
  18768. TBL_PC *sd;
  18769. struct party_data *party;
  18770. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  18771. script_pushint(st,-1);
  18772. return SCRIPT_CMD_FAILURE;
  18773. }
  18774. if( sd->status.party_id ) {
  18775. script_pushint(st,-2);
  18776. return SCRIPT_CMD_FAILURE;
  18777. }
  18778. if( !(party = party_search(party_id)) ) {
  18779. script_pushint(st,-3);
  18780. return SCRIPT_CMD_FAILURE;
  18781. }
  18782. if (battle_config.block_account_in_same_party) {
  18783. int i;
  18784. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  18785. if (i < MAX_PARTY) {
  18786. script_pushint(st,-5);
  18787. return SCRIPT_CMD_FAILURE;
  18788. }
  18789. }
  18790. if( party->party.count >= MAX_PARTY ) {
  18791. script_pushint(st,-4);
  18792. return SCRIPT_CMD_FAILURE;
  18793. }
  18794. sd->party_invite = party_id;
  18795. script_pushint(st,party_add_member(party_id,sd));
  18796. return SCRIPT_CMD_SUCCESS;
  18797. }
  18798. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  18799. * party_delmember {<char id>,<party_id>};
  18800. * @param: char_id
  18801. * @param: party_id
  18802. * @return val: Result value
  18803. * -3 - player is not in party
  18804. * -2 - party is not found
  18805. * -1 - player is not found
  18806. * 0 - unknown error
  18807. * 1 - success
  18808. */
  18809. BUILDIN_FUNC(party_delmember)
  18810. {
  18811. TBL_PC *sd = NULL;
  18812. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  18813. script_pushint(st,-1);
  18814. return SCRIPT_CMD_FAILURE;
  18815. }
  18816. if( sd || script_charid2sd(2,sd) )
  18817. script_pushint(st,party_removemember2(sd,0,0));
  18818. else
  18819. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  18820. return SCRIPT_CMD_SUCCESS;
  18821. }
  18822. /** Changes party leader of specified party (even the leader is offline)
  18823. * party_changeleader <party id>,<char id>;
  18824. * @param party_id: ID of party
  18825. * @param char_id: Char ID of new leader
  18826. * @return val: Result value
  18827. * -4 - player is party leader already
  18828. * -3 - player is not in this party
  18829. * -2 - player is not found
  18830. * -1 - party is not found
  18831. * 0 - unknown error
  18832. * 1 - success */
  18833. BUILDIN_FUNC(party_changeleader)
  18834. {
  18835. int i, party_id = script_getnum(st,2);
  18836. TBL_PC *sd = NULL;
  18837. TBL_PC *tsd = NULL;
  18838. struct party_data *party = NULL;
  18839. if( !(party = party_search(party_id)) ) {
  18840. script_pushint(st,-1);
  18841. return SCRIPT_CMD_FAILURE;
  18842. }
  18843. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  18844. script_pushint(st,-2);
  18845. return SCRIPT_CMD_FAILURE;
  18846. }
  18847. if( tsd->status.party_id != party_id ) {
  18848. script_pushint(st,-3);
  18849. return SCRIPT_CMD_FAILURE;
  18850. }
  18851. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18852. if( i >= MAX_PARTY ) { //this is should impossible!
  18853. script_pushint(st,0);
  18854. return SCRIPT_CMD_FAILURE;
  18855. }
  18856. if( party->data[i].sd == tsd ) {
  18857. script_pushint(st,-4);
  18858. return SCRIPT_CMD_FAILURE;
  18859. }
  18860. script_pushint(st,party_changeleader(sd,tsd,party));
  18861. return SCRIPT_CMD_SUCCESS;
  18862. }
  18863. /** Changes party option
  18864. * party_changeoption <party id>,<option>,<flag>;
  18865. * @param party_id: ID of party that will be changed
  18866. * @param option: Type of option
  18867. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  18868. */
  18869. BUILDIN_FUNC(party_changeoption)
  18870. {
  18871. struct party_data *party;
  18872. if( !(party = party_search(script_getnum(st,2))) ) {
  18873. script_pushint(st,-1);
  18874. return SCRIPT_CMD_FAILURE;
  18875. }
  18876. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  18877. return SCRIPT_CMD_SUCCESS;
  18878. }
  18879. /** Destroys party with party id.
  18880. * party_destroy <party id>;
  18881. * @param party_id: ID of party that will be destroyed
  18882. */
  18883. BUILDIN_FUNC(party_destroy)
  18884. {
  18885. int i;
  18886. struct party_data *party;
  18887. if( !(party = party_search(script_getnum(st,2))) ) {
  18888. script_pushint(st,0);
  18889. return SCRIPT_CMD_FAILURE;
  18890. }
  18891. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18892. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  18893. int j;
  18894. for( j = 0; j < MAX_PARTY; j++ ) {
  18895. TBL_PC *sd = party->data[j].sd;
  18896. if(sd)
  18897. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18898. else if( party->party.member[j].char_id )
  18899. 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);
  18900. }
  18901. party_broken(party->party.party_id);
  18902. script_pushint(st,1);
  18903. }
  18904. else //leader leave = party broken
  18905. script_pushint(st,party_leave(party->data[i].sd));
  18906. return SCRIPT_CMD_SUCCESS;
  18907. }
  18908. /** Returns various information about a player's VIP status. Need to enable VIP system
  18909. * vip_status <type>,{"<character name>"};
  18910. * @param type: Info type, see enum vip_status_type
  18911. * @param name: Character name (Optional)
  18912. */
  18913. BUILDIN_FUNC(vip_status) {
  18914. #ifdef VIP_ENABLE
  18915. TBL_PC *sd;
  18916. int type;
  18917. if( !script_nick2sd(3,sd) )
  18918. return SCRIPT_CMD_FAILURE;
  18919. type = script_getnum(st, 2);
  18920. switch(type) {
  18921. case VIP_STATUS_ACTIVE: // Get VIP status.
  18922. script_pushint(st, pc_isvip(sd));
  18923. break;
  18924. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  18925. if (pc_isvip(sd)) {
  18926. script_pushint(st, sd->vip.time);
  18927. } else
  18928. script_pushint(st, 0);
  18929. break;
  18930. case VIP_STATUS_REMAINING: // Get remaining time.
  18931. if (pc_isvip(sd)) {
  18932. script_pushint(st, sd->vip.time - time(NULL));
  18933. } else
  18934. script_pushint(st, 0);
  18935. break;
  18936. default:
  18937. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  18938. return SCRIPT_CMD_FAILURE;
  18939. }
  18940. #else
  18941. script_pushint(st, 0);
  18942. #endif
  18943. return SCRIPT_CMD_SUCCESS;
  18944. }
  18945. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18946. * vip_time <time in mn>,{"<character name>"};
  18947. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18948. * @param name: Character name (optional)
  18949. */
  18950. BUILDIN_FUNC(vip_time) {
  18951. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18952. TBL_PC *sd;
  18953. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18954. if( !script_nick2sd(3,sd) )
  18955. return SCRIPT_CMD_FAILURE;
  18956. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18957. #endif
  18958. return SCRIPT_CMD_SUCCESS;
  18959. }
  18960. /**
  18961. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18962. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18963. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18964. * @param monster: Monster ID or name
  18965. * @param duration: Transform duration in millisecond (ms)
  18966. * @param sc_type: Type of SC that will be affected during the transformation
  18967. * @param val1: Value for SC
  18968. * @param val2: Value for SC
  18969. * @param val3: Value for SC
  18970. * @param val4: Value for SC
  18971. * @author: malufett
  18972. */
  18973. BUILDIN_FUNC(montransform) {
  18974. TBL_PC *sd;
  18975. enum sc_type type;
  18976. int tick, mob_id, val1, val2, val3, val4;
  18977. struct script_data *data;
  18978. val1 = val2 = val3 = val4 = 0;
  18979. if( !script_rid2sd(sd) )
  18980. return SCRIPT_CMD_FAILURE;
  18981. data = script_getdata(st, 2);
  18982. get_val(st, data); // Convert into value in case of a variable
  18983. if( data_isstring(data) )
  18984. mob_id = mobdb_searchname(script_getstr(st, 2));
  18985. else
  18986. mob_id = mobdb_checkid(script_getnum(st, 2));
  18987. tick = script_getnum(st, 3);
  18988. if (script_hasdata(st, 4))
  18989. type = (sc_type)script_getnum(st, 4);
  18990. else
  18991. type = SC_NONE;
  18992. if (mob_id == 0) {
  18993. if( data_isstring(data) )
  18994. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18995. else
  18996. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18997. return SCRIPT_CMD_FAILURE;
  18998. }
  18999. if (mob_id == MOBID_EMPERIUM) {
  19000. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  19001. return SCRIPT_CMD_FAILURE;
  19002. }
  19003. if (!(type >= SC_NONE && type < SC_MAX)) {
  19004. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  19005. return SCRIPT_CMD_FAILURE;
  19006. }
  19007. if (script_hasdata(st, 5))
  19008. val1 = script_getnum(st, 5);
  19009. if (script_hasdata(st, 6))
  19010. val2 = script_getnum(st, 6);
  19011. if (script_hasdata(st, 7))
  19012. val3 = script_getnum(st, 7);
  19013. if (script_hasdata(st, 8))
  19014. val4 = script_getnum(st, 8);
  19015. if (tick != 0) {
  19016. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  19017. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  19018. return SCRIPT_CMD_FAILURE;
  19019. }
  19020. if (sd->disguise){
  19021. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  19022. return SCRIPT_CMD_FAILURE;
  19023. }
  19024. if (!strcmp(script_getfuncname(st), "active_transform")) {
  19025. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19026. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19027. } else {
  19028. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  19029. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  19030. }
  19031. if (type != SC_NONE)
  19032. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  19033. }
  19034. return SCRIPT_CMD_SUCCESS;
  19035. }
  19036. /**
  19037. * Attach script to player for certain duration
  19038. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  19039. * @param "script code"
  19040. * @param duration
  19041. * @param flag
  19042. * @param icon
  19043. * @param char_id
  19044. * @author [Cydh]
  19045. **/
  19046. BUILDIN_FUNC(bonus_script) {
  19047. uint16 flag = 0;
  19048. int16 icon = EFST_BLANK;
  19049. uint32 dur;
  19050. uint8 type = 0;
  19051. TBL_PC* sd;
  19052. const char *script_str = NULL;
  19053. struct s_bonus_script_entry *entry = NULL;
  19054. if ( !script_charid2sd(7,sd) )
  19055. return SCRIPT_CMD_FAILURE;
  19056. script_str = script_getstr(st,2);
  19057. dur = 1000 * abs(script_getnum(st,3));
  19058. FETCH(4, flag);
  19059. FETCH(5, type);
  19060. FETCH(6, icon);
  19061. // No Script string, No Duration!
  19062. if (script_str[0] == '\0' || !dur) {
  19063. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  19064. return SCRIPT_CMD_FAILURE;
  19065. }
  19066. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  19067. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  19068. return SCRIPT_CMD_FAILURE;
  19069. }
  19070. if (icon <= EFST_BLANK || icon >= EFST_MAX)
  19071. icon = EFST_BLANK;
  19072. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum efst_types)icon, flag, type))) {
  19073. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  19074. status_calc_pc(sd,SCO_NONE);
  19075. }
  19076. return SCRIPT_CMD_SUCCESS;
  19077. }
  19078. /**
  19079. * Removes all bonus script from player
  19080. * bonus_script_clear {<flag>,{<char_id>}};
  19081. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  19082. * @param char_id Clear script from this character
  19083. * @author [Cydh]
  19084. */
  19085. BUILDIN_FUNC(bonus_script_clear) {
  19086. TBL_PC* sd;
  19087. bool flag = false;
  19088. if (!script_charid2sd(3,sd))
  19089. return SCRIPT_CMD_FAILURE;
  19090. if (script_hasdata(st,2))
  19091. flag = script_getnum(st,2) != 0;
  19092. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  19093. return SCRIPT_CMD_SUCCESS;
  19094. }
  19095. /** Allows player to use atcommand while talking with NPC
  19096. * enable_command;
  19097. * @author [Cydh], [Kichi]
  19098. */
  19099. BUILDIN_FUNC(enable_command) {
  19100. TBL_PC* sd;
  19101. if (!script_rid2sd(sd))
  19102. return SCRIPT_CMD_FAILURE;
  19103. sd->state.disable_atcommand_on_npc = 0;
  19104. return SCRIPT_CMD_SUCCESS;
  19105. }
  19106. /** Prevents player to use atcommand while talking with NPC
  19107. * disable_command;
  19108. * @author [Cydh], [Kichi]
  19109. */
  19110. BUILDIN_FUNC(disable_command) {
  19111. TBL_PC* sd;
  19112. if (!script_rid2sd(sd))
  19113. return SCRIPT_CMD_FAILURE;
  19114. sd->state.disable_atcommand_on_npc = 1;
  19115. return SCRIPT_CMD_SUCCESS;
  19116. }
  19117. /** Get the information of the members of a guild by type.
  19118. * getguildmember <guild_id>{,<type>};
  19119. * @param guild_id: ID of guild
  19120. * @param type: Type of option (optional)
  19121. */
  19122. BUILDIN_FUNC(getguildmember)
  19123. {
  19124. struct guild *g = NULL;
  19125. uint8 j = 0;
  19126. g = guild_search(script_getnum(st,2));
  19127. if (g) {
  19128. uint8 i, type = 0;
  19129. struct script_data *data = NULL;
  19130. char *varname = NULL;
  19131. if (script_hasdata(st,3))
  19132. type = script_getnum(st,3);
  19133. if (script_hasdata(st,4)) {
  19134. data = script_getdata(st, 4);
  19135. if (!data_isreference(data)) {
  19136. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  19137. return SCRIPT_CMD_FAILURE;
  19138. }
  19139. varname = reference_getname(data);
  19140. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  19141. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  19142. return SCRIPT_CMD_FAILURE;
  19143. }
  19144. }
  19145. for (i = 0; i < MAX_GUILD; i++) {
  19146. if (g->member[i].account_id) {
  19147. switch (type) {
  19148. case 2:
  19149. if (data)
  19150. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  19151. else
  19152. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  19153. break;
  19154. case 1:
  19155. if (data)
  19156. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  19157. else
  19158. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  19159. break;
  19160. default:
  19161. if (data)
  19162. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  19163. else
  19164. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  19165. break;
  19166. }
  19167. j++;
  19168. }
  19169. }
  19170. }
  19171. mapreg_setreg(add_str("$@guildmembercount"), j);
  19172. script_pushint(st, j);
  19173. return SCRIPT_CMD_SUCCESS;
  19174. }
  19175. /** Adds spirit ball to player for 'duration' in milisecond
  19176. * addspiritball <count>,<duration>{,<char_id>};
  19177. * @param count How many spirit ball will be added
  19178. * @param duration How long spiritball is active until it disappears
  19179. * @param char_id Target player (Optional)
  19180. * @author [Cydh]
  19181. */
  19182. BUILDIN_FUNC(addspiritball) {
  19183. uint8 i, count;
  19184. uint16 duration;
  19185. struct map_session_data *sd = NULL;
  19186. if (script_hasdata(st,4)) {
  19187. if (!script_isstring(st,4))
  19188. sd = map_charid2sd(script_getnum(st,4));
  19189. else
  19190. sd = map_nick2sd(script_getstr(st,4),false);
  19191. }
  19192. else
  19193. script_rid2sd(sd);
  19194. if (!sd)
  19195. return SCRIPT_CMD_FAILURE;
  19196. count = script_getnum(st,2);
  19197. if (count == 0)
  19198. return SCRIPT_CMD_SUCCESS;
  19199. duration = script_getnum(st,3);
  19200. for (i = 0; i < count; i++)
  19201. pc_addspiritball(sd,duration,10);
  19202. return SCRIPT_CMD_SUCCESS;
  19203. }
  19204. /** Deletes the spirit ball(s) from player
  19205. * delspiritball <count>{,<char_id>};
  19206. * @param count How many spirit ball will be deleted
  19207. * @param char_id Target player (Optional)
  19208. * @author [Cydh]
  19209. */
  19210. BUILDIN_FUNC(delspiritball) {
  19211. uint8 count;
  19212. struct map_session_data *sd = NULL;
  19213. if (script_hasdata(st,3)) {
  19214. if (!script_isstring(st,3))
  19215. sd = map_charid2sd(script_getnum(st,3));
  19216. else
  19217. sd = map_nick2sd(script_getstr(st,3),false);
  19218. }
  19219. else
  19220. script_rid2sd(sd);
  19221. if (!sd)
  19222. return SCRIPT_CMD_FAILURE;
  19223. count = script_getnum(st,2);
  19224. if (count == 0)
  19225. count = 1;
  19226. pc_delspiritball(sd,count,0);
  19227. return SCRIPT_CMD_SUCCESS;
  19228. }
  19229. /** Counts the spirit ball that player has
  19230. * countspiritball {<char_id>};
  19231. * @param char_id Target player (Optional)
  19232. * @author [Cydh]
  19233. */
  19234. BUILDIN_FUNC(countspiritball) {
  19235. struct map_session_data *sd;
  19236. if (script_hasdata(st,2)) {
  19237. if (!script_isstring(st,2))
  19238. sd = map_charid2sd(script_getnum(st,2));
  19239. else
  19240. sd = map_nick2sd(script_getstr(st,2),false);
  19241. }
  19242. else
  19243. script_rid2sd(sd);
  19244. if (!sd)
  19245. return SCRIPT_CMD_FAILURE;
  19246. script_pushint(st,sd->spiritball);
  19247. return SCRIPT_CMD_SUCCESS;
  19248. }
  19249. /** Merges separated stackable items because of guid
  19250. * mergeitem {<char_id>};
  19251. * @param char_id Char ID (Optional)
  19252. * @author [Cydh]
  19253. */
  19254. BUILDIN_FUNC(mergeitem) {
  19255. struct map_session_data *sd;
  19256. if (!script_charid2sd(2, sd))
  19257. return SCRIPT_CMD_FAILURE;
  19258. clif_merge_item_open(sd);
  19259. return SCRIPT_CMD_SUCCESS;
  19260. }
  19261. /** Merges separated stackable items because of guid (Unofficial)
  19262. * mergeitem2 {<item_id>,{<char_id>}};
  19263. * mergeitem2 {"<item name>",{<char_id>}};
  19264. * @param item Item ID/Name for merging specific item (Optional)
  19265. * @author [Cydh]
  19266. */
  19267. BUILDIN_FUNC(mergeitem2) {
  19268. struct map_session_data *sd;
  19269. struct item *items = NULL;
  19270. uint16 i, count = 0, nameid = 0;
  19271. if (!script_charid2sd(3, sd))
  19272. return SCRIPT_CMD_FAILURE;
  19273. if (script_hasdata(st, 2)) {
  19274. struct script_data *data = script_getdata(st, 2);
  19275. get_val(st, data);
  19276. if (data_isstring(data)) {// "<item name>"
  19277. const char *name = conv_str(st,data);
  19278. struct item_data *id;
  19279. if (!(id = itemdb_searchname(name))) {
  19280. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  19281. script_pushint(st, count);
  19282. return SCRIPT_CMD_FAILURE;
  19283. }
  19284. nameid = id->nameid;
  19285. }
  19286. else if (data_isint(data)) {// <item id>
  19287. nameid = conv_num(st,data);
  19288. if (!itemdb_exists(nameid)) {
  19289. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  19290. script_pushint(st, count);
  19291. return SCRIPT_CMD_FAILURE;
  19292. }
  19293. }
  19294. }
  19295. for (i = 0; i < MAX_INVENTORY; i++) {
  19296. struct item *it = &sd->inventory.u.items_inventory[i];
  19297. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  19298. continue;
  19299. if ((!nameid || (nameid == it->nameid))) {
  19300. uint8 k;
  19301. if (!count) {
  19302. CREATE(items, struct item, 1);
  19303. memcpy(&items[count++], it, sizeof(struct item));
  19304. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19305. continue;
  19306. }
  19307. for (k = 0; k < count; k++) {
  19308. // Find Match
  19309. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  19310. items[k].amount += it->amount;
  19311. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19312. break;
  19313. }
  19314. }
  19315. if (k >= count) {
  19316. // New entry
  19317. RECREATE(items, struct item, count+1);
  19318. memcpy(&items[count++], it, sizeof(struct item));
  19319. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  19320. }
  19321. }
  19322. }
  19323. if (!items) // Nothing todo here
  19324. return SCRIPT_CMD_SUCCESS;
  19325. // Retrieve the items
  19326. for (i = 0; i < count; i++) {
  19327. uint8 flag = 0;
  19328. if (!&items[i])
  19329. continue;
  19330. items[i].id = 0;
  19331. items[i].unique_id = 0;
  19332. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  19333. clif_additem(sd, i, items[i].amount, flag);
  19334. }
  19335. aFree(items);
  19336. script_pushint(st, count);
  19337. return SCRIPT_CMD_SUCCESS;
  19338. }
  19339. /**
  19340. * Update an entry from NPC shop.
  19341. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  19342. **/
  19343. BUILDIN_FUNC(npcshopupdate) {
  19344. const char* npcname = script_getstr(st, 2);
  19345. struct npc_data* nd = npc_name2id(npcname);
  19346. uint16 nameid = script_getnum(st, 3);
  19347. int price = script_getnum(st, 4);
  19348. #if PACKETVER >= 20131223
  19349. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  19350. #endif
  19351. int i;
  19352. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  19353. script_pushint(st,0);
  19354. return SCRIPT_CMD_FAILURE;
  19355. }
  19356. if (!nd->u.shop.count) {
  19357. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  19358. script_pushint(st,0);
  19359. return SCRIPT_CMD_FAILURE;
  19360. }
  19361. for (i = 0; i < nd->u.shop.count; i++) {
  19362. if (nd->u.shop.shop_item[i].nameid == nameid) {
  19363. if (price != 0)
  19364. nd->u.shop.shop_item[i].value = price;
  19365. #if PACKETVER >= 20131223
  19366. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  19367. nd->u.shop.shop_item[i].qty = stock;
  19368. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  19369. }
  19370. #endif
  19371. }
  19372. }
  19373. script_pushint(st,1);
  19374. return SCRIPT_CMD_SUCCESS;
  19375. }
  19376. // Clan System
  19377. BUILDIN_FUNC(clan_join){
  19378. struct map_session_data *sd;
  19379. int clan_id = script_getnum(st,2);
  19380. if( !script_charid2sd( 3, sd ) ){
  19381. script_pushint(st, false);
  19382. return SCRIPT_CMD_FAILURE;
  19383. }
  19384. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  19385. script_pushint(st, true);
  19386. else
  19387. script_pushint(st, false);
  19388. return SCRIPT_CMD_SUCCESS;
  19389. }
  19390. BUILDIN_FUNC(clan_leave){
  19391. struct map_session_data *sd;
  19392. if( !script_charid2sd( 2, sd ) ){
  19393. script_pushint(st, false);
  19394. return SCRIPT_CMD_FAILURE;
  19395. }
  19396. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  19397. script_pushint(st, true);
  19398. else
  19399. script_pushint(st, false);
  19400. return SCRIPT_CMD_SUCCESS;
  19401. }
  19402. /**
  19403. * Get rid from running script.
  19404. * getattachedrid();
  19405. **/
  19406. BUILDIN_FUNC(getattachedrid) {
  19407. script_pushint(st, st->rid);
  19408. return SCRIPT_CMD_SUCCESS;
  19409. }
  19410. /**
  19411. * Get variable from a Player
  19412. * getvar <variable>,<char_id>;
  19413. */
  19414. BUILDIN_FUNC(getvar) {
  19415. int char_id = script_getnum(st, 3);
  19416. struct map_session_data *sd = map_charid2sd(char_id);
  19417. struct script_data *data = NULL;
  19418. const char *name = NULL;
  19419. if (!sd) {
  19420. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  19421. return SCRIPT_CMD_FAILURE;
  19422. }
  19423. data = script_getdata(st, 2);
  19424. if (!data_isreference(data)) {
  19425. ShowError("buildin_getvar: Not a variable\n");
  19426. script_reportdata(data);
  19427. script_pushnil(st);
  19428. st->state = END;
  19429. return SCRIPT_CMD_FAILURE;
  19430. }
  19431. name = reference_getname(data);
  19432. if (reference_toparam(data)) {
  19433. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  19434. script_reportdata(data);
  19435. script_pushnil(st);
  19436. st->state = END;
  19437. return SCRIPT_CMD_FAILURE;
  19438. }
  19439. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  19440. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  19441. script_reportdata(data);
  19442. script_pushnil(st);
  19443. st->state = END;
  19444. return SCRIPT_CMD_FAILURE;
  19445. }
  19446. get_val_(st, data, sd);
  19447. if (data_isint(data))
  19448. script_pushint(st, conv_num_(st, data, sd));
  19449. else
  19450. script_pushstrcopy(st, conv_str_(st, data, sd));
  19451. return SCRIPT_CMD_SUCCESS;
  19452. }
  19453. /**
  19454. * Display script message
  19455. * showscript "<message>"{,<GID>,<flag>};
  19456. * @param flag: Specify target
  19457. * AREA - Message is sent to players in the vicinity of the source (default).
  19458. * SELF - Message is sent only to player attached.
  19459. **/
  19460. BUILDIN_FUNC(showscript) {
  19461. struct block_list *bl = NULL;
  19462. const char *msg = script_getstr(st,2);
  19463. int id = 0;
  19464. send_target target = AREA;
  19465. if (script_hasdata(st,3)) {
  19466. id = script_getnum(st,3);
  19467. bl = map_id2bl(id);
  19468. }
  19469. else {
  19470. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  19471. }
  19472. if (!bl) {
  19473. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  19474. script_pushint(st,0);
  19475. return SCRIPT_CMD_FAILURE;
  19476. }
  19477. if (script_hasdata(st, 4)) {
  19478. target = static_cast<send_target>(script_getnum(st, 4));
  19479. if (target == SELF && map_id2sd(bl->id) == NULL) {
  19480. ShowWarning("script: showscript: self can't be used for non-players objects.\n");
  19481. return SCRIPT_CMD_FAILURE;
  19482. }
  19483. }
  19484. clif_showscript(bl, msg, target);
  19485. script_pushint(st,1);
  19486. return SCRIPT_CMD_SUCCESS;
  19487. }
  19488. /**
  19489. * Ignore the SECURE_NPCTIMEOUT function.
  19490. * ignoretimeout <flag>{,<char_id>};
  19491. */
  19492. BUILDIN_FUNC(ignoretimeout)
  19493. {
  19494. #ifdef SECURE_NPCTIMEOUT
  19495. struct map_session_data *sd = NULL;
  19496. if (script_hasdata(st,3)) {
  19497. if (!script_isstring(st,3))
  19498. sd = map_charid2sd(script_getnum(st,3));
  19499. else
  19500. sd = map_nick2sd(script_getstr(st,3),false);
  19501. } else
  19502. script_rid2sd(sd);
  19503. if (!sd)
  19504. return SCRIPT_CMD_FAILURE;
  19505. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  19506. #endif
  19507. return SCRIPT_CMD_SUCCESS;
  19508. }
  19509. /**
  19510. * geteleminfo <type>{,<char_id>};
  19511. **/
  19512. BUILDIN_FUNC(geteleminfo) {
  19513. TBL_ELEM *ed = NULL;
  19514. TBL_PC *sd = NULL;
  19515. int type = script_getnum(st,2);
  19516. if (!script_charid2sd(3, sd)) {
  19517. script_pushint(st, 0);
  19518. return SCRIPT_CMD_SUCCESS;
  19519. }
  19520. if (!(ed = sd->ed)) {
  19521. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  19522. script_pushint(st, 0);
  19523. return SCRIPT_CMD_SUCCESS;
  19524. }
  19525. switch (type) {
  19526. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  19527. case 1: script_pushint(st, ed->bl.id); break;
  19528. default:
  19529. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  19530. script_pushint(st, 0);
  19531. return SCRIPT_CMD_FAILURE;
  19532. }
  19533. return SCRIPT_CMD_SUCCESS;
  19534. }
  19535. /**
  19536. * Set the quest info of quest_id only showed on player in level range.
  19537. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  19538. * @author [Cydh]
  19539. **/
  19540. BUILDIN_FUNC(setquestinfo_level) {
  19541. TBL_NPC* nd = map_id2nd(st->oid);
  19542. int quest_id = script_getnum(st, 2);
  19543. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19544. if (!qi) {
  19545. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  19546. return SCRIPT_CMD_FAILURE;
  19547. }
  19548. qi->min_level = script_getnum(st, 3);
  19549. qi->max_level = script_getnum(st, 4);
  19550. if (!qi->max_level)
  19551. qi->max_level = MAX_LEVEL;
  19552. return SCRIPT_CMD_SUCCESS;
  19553. }
  19554. /**
  19555. * Set the quest info of quest_id only showed for player that has quest criteria
  19556. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  19557. * @author [Cydh]
  19558. **/
  19559. BUILDIN_FUNC(setquestinfo_req) {
  19560. TBL_NPC* nd = map_id2nd(st->oid);
  19561. int quest_id = script_getnum(st, 2);
  19562. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19563. uint8 i = 0;
  19564. uint8 num = script_lastdata(st);
  19565. if (!qi) {
  19566. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  19567. return SCRIPT_CMD_FAILURE;
  19568. }
  19569. if (quest_search(quest_id) == &quest_dummy) {
  19570. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  19571. return SCRIPT_CMD_FAILURE;
  19572. }
  19573. if (num%2) {
  19574. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  19575. return SCRIPT_CMD_FAILURE;
  19576. }
  19577. for (i = 3; i <= num; i += 2) {
  19578. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  19579. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  19580. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  19581. qi->req_count++;
  19582. }
  19583. return SCRIPT_CMD_SUCCESS;
  19584. }
  19585. /**
  19586. * Set the quest info of quest_id only showed for player that has specified Job
  19587. * setquestinfo_job <quest_id>,<job>{,<job>...};
  19588. * @author [Cydh]
  19589. **/
  19590. BUILDIN_FUNC(setquestinfo_job) {
  19591. TBL_NPC* nd = map_id2nd(st->oid);
  19592. int quest_id = script_getnum(st, 2);
  19593. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19594. int job_id = 0;
  19595. uint8 i = 0;
  19596. uint8 num = script_lastdata(st)+1;
  19597. if (!qi) {
  19598. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  19599. return SCRIPT_CMD_FAILURE;
  19600. }
  19601. for (i = 3; i < num; i++) {
  19602. job_id = script_getnum(st, i);
  19603. if (!pcdb_checkid(job_id)) {
  19604. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  19605. continue;
  19606. }
  19607. buildin_questinfo_setjob(qi, job_id);
  19608. }
  19609. return SCRIPT_CMD_SUCCESS;
  19610. }
  19611. /**
  19612. * opendressroom(<flag>{,<char_id>});
  19613. */
  19614. BUILDIN_FUNC(opendressroom)
  19615. {
  19616. #if PACKETVER >= 20150513
  19617. int flag = 1;
  19618. TBL_PC* sd;
  19619. if( script_hasdata(st,2) )
  19620. flag = script_getnum(st,2);
  19621. if (!script_charid2sd(3, sd))
  19622. return SCRIPT_CMD_FAILURE;
  19623. clif_dressing_room(sd, flag);
  19624. return SCRIPT_CMD_SUCCESS;
  19625. #else
  19626. return SCRIPT_CMD_FAILURE;
  19627. #endif
  19628. }
  19629. /**
  19630. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  19631. */
  19632. BUILDIN_FUNC(navigateto){
  19633. #if PACKETVER >= 20111010
  19634. TBL_PC* sd;
  19635. const char *mapname;
  19636. uint16 x = 0, y = 0, monster_id = 0;
  19637. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  19638. bool hideWindow = true;
  19639. mapname = script_getstr(st,2);
  19640. if( script_hasdata(st,3) )
  19641. x = script_getnum(st,3);
  19642. if( script_hasdata(st,4) )
  19643. y = script_getnum(st,4);
  19644. if( script_hasdata(st,5) )
  19645. flag = (uint8)script_getnum(st,5);
  19646. if( script_hasdata(st,6) )
  19647. hideWindow = script_getnum(st,6) ? true : false;
  19648. if( script_hasdata(st,7) )
  19649. monster_id = script_getnum(st,7);
  19650. if (!script_charid2sd(8, sd))
  19651. return SCRIPT_CMD_FAILURE;
  19652. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  19653. return SCRIPT_CMD_SUCCESS;
  19654. #else
  19655. return SCRIPT_CMD_FAILURE;
  19656. #endif
  19657. }
  19658. /**
  19659. * adopt("<parent_name>","<baby_name>");
  19660. * adopt(<parent_id>,<baby_id>);
  19661. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  19662. */
  19663. BUILDIN_FUNC(adopt)
  19664. {
  19665. TBL_PC *sd, *b_sd;
  19666. struct script_data *data;
  19667. enum adopt_responses response;
  19668. data = script_getdata(st, 2);
  19669. get_val(st, data);
  19670. if (data_isstring(data)) {
  19671. const char *name = conv_str(st, data);
  19672. sd = map_nick2sd(name,false);
  19673. if (sd == NULL) {
  19674. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  19675. return SCRIPT_CMD_FAILURE;
  19676. }
  19677. } else if (data_isint(data)) {
  19678. uint32 char_id = conv_num(st, data);
  19679. sd = map_charid2sd(char_id);
  19680. if (sd == NULL) {
  19681. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  19682. return SCRIPT_CMD_FAILURE;
  19683. }
  19684. } else {
  19685. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  19686. return SCRIPT_CMD_FAILURE;
  19687. }
  19688. data = script_getdata(st, 3);
  19689. get_val(st, data);
  19690. if (data_isstring(data)) {
  19691. const char *name = conv_str(st, data);
  19692. b_sd = map_nick2sd(name,false);
  19693. if (b_sd == NULL) {
  19694. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  19695. return SCRIPT_CMD_FAILURE;
  19696. }
  19697. } else if (data_isint(data)) {
  19698. uint32 char_id = conv_num(st, data);
  19699. b_sd = map_charid2sd(char_id);
  19700. if (b_sd == NULL) {
  19701. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  19702. return SCRIPT_CMD_FAILURE;
  19703. }
  19704. } else {
  19705. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  19706. return SCRIPT_CMD_FAILURE;
  19707. }
  19708. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  19709. if (response == ADOPT_ALLOWED) {
  19710. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  19711. b_sd->adopt_invite = sd->status.account_id;
  19712. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  19713. script_pushint(st, ADOPT_ALLOWED);
  19714. return SCRIPT_CMD_SUCCESS;
  19715. }
  19716. script_pushint(st, response);
  19717. return SCRIPT_CMD_FAILURE;
  19718. }
  19719. /**
  19720. * Returns the minimum or maximum of all the given parameters for integer variables.
  19721. *
  19722. * min( <value or array>{,value or array 2,...} );
  19723. * minimum( <value or array>{,value or array 2,...} );
  19724. * max( <value or array>{,value or array 2,...} );
  19725. * maximum( <value or array>{,value or array 2,...} );
  19726. */
  19727. BUILDIN_FUNC(minmax){
  19728. char *functionname;
  19729. unsigned int i;
  19730. int value;
  19731. // Function pointer for our comparison function (either min or max at the moment)
  19732. int32 (*func)(int32, int32);
  19733. // Get the real function name
  19734. functionname = script_getfuncname(st);
  19735. // Our data should start at offset 2
  19736. i = 2;
  19737. if( !script_hasdata( st, i ) ){
  19738. ShowError( "buildin_%s: no arguments given!\n", functionname );
  19739. st->state = END;
  19740. return SCRIPT_CMD_FAILURE;
  19741. }
  19742. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  19743. value = INT_MAX;
  19744. func = i32min;
  19745. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  19746. value = INT_MIN;
  19747. func = i32max;
  19748. }else{
  19749. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  19750. st->state = END;
  19751. return SCRIPT_CMD_FAILURE;
  19752. }
  19753. // As long as we have data on our script stack
  19754. while( script_hasdata(st,i) ){
  19755. struct script_data *data;
  19756. // Get the next piece of data from the script stack
  19757. data = script_getdata( st, i );
  19758. // Is the current parameter a single integer?
  19759. if( data_isint( data ) ){
  19760. value = func( value, script_getnum( st, i ) );
  19761. // Is the current parameter an array variable?
  19762. }else if( data_isreference( data ) ){
  19763. const char *name;
  19764. struct map_session_data* sd;
  19765. unsigned int start, end;
  19766. // Get the name of the variable
  19767. name = reference_getname(data);
  19768. // Check if it's a string variable
  19769. if( is_string_variable( name ) ){
  19770. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  19771. script_reportdata( data );
  19772. st->state = END;
  19773. return SCRIPT_CMD_FAILURE;
  19774. }
  19775. // Get the session data, if a player is attached
  19776. sd = st->rid ? map_id2sd(st->rid) : NULL;
  19777. // Try to find the array's source pointer
  19778. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  19779. ShowError( "buildin_%s: not a array!\n", functionname );
  19780. script_reportdata( data );
  19781. st->state = END;
  19782. return SCRIPT_CMD_FAILURE;
  19783. }
  19784. // Get the start and end indices of the array
  19785. start = reference_getindex( data );
  19786. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  19787. // Skip empty arrays
  19788. if( start < end ){
  19789. int id;
  19790. // For getting the values we need the id of the array
  19791. id = reference_getid( data );
  19792. // Loop through each value stored in the array
  19793. for( ; start < end; start++ ){
  19794. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  19795. script_removetop( st, -1, 0 );
  19796. }
  19797. }
  19798. }else{
  19799. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  19800. script_reportdata( data );
  19801. st->state = END;
  19802. return SCRIPT_CMD_FAILURE;
  19803. }
  19804. // Continue with the next stack entry
  19805. i++;
  19806. }
  19807. script_pushint( st, value );
  19808. return SCRIPT_CMD_SUCCESS;
  19809. }
  19810. /**
  19811. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  19812. * Unlike `getexp` that affected by some adjustments.
  19813. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  19814. * @author [Cydh]
  19815. **/
  19816. BUILDIN_FUNC(getexp2) {
  19817. TBL_PC *sd = NULL;
  19818. int base_exp = script_getnum(st, 2);
  19819. int job_exp = script_getnum(st, 3);
  19820. if (!script_charid2sd(4, sd))
  19821. return SCRIPT_CMD_FAILURE;
  19822. if (base_exp == 0 && job_exp == 0)
  19823. return SCRIPT_CMD_SUCCESS;
  19824. if (base_exp > 0)
  19825. pc_gainexp(sd, NULL, base_exp, 0, 2);
  19826. else if (base_exp < 0)
  19827. pc_lostexp(sd, base_exp * -1, 0);
  19828. if (job_exp > 0)
  19829. pc_gainexp(sd, NULL, 0, job_exp, 2);
  19830. else if (job_exp < 0)
  19831. pc_lostexp(sd, 0, job_exp * -1);
  19832. return SCRIPT_CMD_SUCCESS;
  19833. }
  19834. /**
  19835. * Force stat recalculation of sd
  19836. * recalculatestat;
  19837. * @author [secretdataz]
  19838. **/
  19839. BUILDIN_FUNC(recalculatestat) {
  19840. TBL_PC* sd;
  19841. if (!script_rid2sd(sd))
  19842. return SCRIPT_CMD_FAILURE;
  19843. status_calc_pc(sd, SCO_FORCE);
  19844. return SCRIPT_CMD_SUCCESS;
  19845. }
  19846. BUILDIN_FUNC(hateffect){
  19847. #if PACKETVER >= 20150513
  19848. struct map_session_data* sd;
  19849. bool enable;
  19850. int i, effectID;
  19851. if( !script_rid2sd(sd) )
  19852. return SCRIPT_CMD_FAILURE;
  19853. effectID = script_getnum(st,2);
  19854. enable = script_getnum(st,3) ? true : false;
  19855. if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){
  19856. ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID );
  19857. return SCRIPT_CMD_FAILURE;
  19858. }
  19859. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  19860. if( enable ){
  19861. if( i < sd->hatEffectCount ){
  19862. return SCRIPT_CMD_SUCCESS;
  19863. }
  19864. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  19865. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  19866. sd->hatEffectCount++;
  19867. }else{
  19868. if( i == sd->hatEffectCount ){
  19869. return SCRIPT_CMD_SUCCESS;
  19870. }
  19871. for( ; i < sd->hatEffectCount - 1; i++ ){
  19872. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  19873. }
  19874. sd->hatEffectCount--;
  19875. if( !sd->hatEffectCount ){
  19876. aFree(sd->hatEffectIDs);
  19877. sd->hatEffectIDs = NULL;
  19878. }
  19879. }
  19880. if( !sd->state.connect_new ){
  19881. clif_hat_effect_single( sd, effectID, enable );
  19882. }
  19883. #endif
  19884. return SCRIPT_CMD_SUCCESS;
  19885. }
  19886. /**
  19887. * Retrieves param of current random option. Intended for random option script only.
  19888. * getrandomoptinfo(<type>);
  19889. * @author [secretdataz]
  19890. **/
  19891. BUILDIN_FUNC(getrandomoptinfo) {
  19892. struct map_session_data *sd;
  19893. int val;
  19894. 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) {
  19895. int param = script_getnum(st, 2);
  19896. switch (param) {
  19897. case ROA_ID:
  19898. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  19899. break;
  19900. case ROA_VALUE:
  19901. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  19902. break;
  19903. case ROA_PARAM:
  19904. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  19905. break;
  19906. default:
  19907. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  19908. val = 0;
  19909. break;
  19910. }
  19911. script_pushint(st, val);
  19912. }
  19913. else {
  19914. script_pushint(st, 0);
  19915. }
  19916. return SCRIPT_CMD_SUCCESS;
  19917. }
  19918. /**
  19919. * Retrieves a random option on an equipped item.
  19920. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  19921. * @author [secretdataz]
  19922. */
  19923. BUILDIN_FUNC(getequiprandomoption) {
  19924. struct map_session_data *sd;
  19925. int val;
  19926. short i = -1;
  19927. int pos = script_getnum(st, 2);
  19928. int index = script_getnum(st, 3);
  19929. int type = script_getnum(st, 4);
  19930. if (!script_charid2sd(5, sd)) {
  19931. script_pushint(st, -1);
  19932. return SCRIPT_CMD_FAILURE;
  19933. }
  19934. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19935. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  19936. script_pushint(st, -1);
  19937. return SCRIPT_CMD_FAILURE;
  19938. }
  19939. if (equip_index_check(pos))
  19940. i = pc_checkequip(sd, equip_bitmask[pos]);
  19941. if (i < 0) {
  19942. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19943. script_pushint(st, -1);
  19944. return SCRIPT_CMD_FAILURE;
  19945. }
  19946. switch (type) {
  19947. case ROA_ID:
  19948. val = sd->inventory.u.items_inventory[i].option[index].id;
  19949. break;
  19950. case ROA_VALUE:
  19951. val = sd->inventory.u.items_inventory[i].option[index].value;
  19952. break;
  19953. case ROA_PARAM:
  19954. val = sd->inventory.u.items_inventory[i].option[index].param;
  19955. break;
  19956. default:
  19957. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19958. val = 0;
  19959. break;
  19960. }
  19961. script_pushint(st, val);
  19962. return SCRIPT_CMD_SUCCESS;
  19963. }
  19964. /**
  19965. * Adds a random option on a random option slot on an equipped item and overwrites
  19966. * existing random option in the process.
  19967. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19968. * @author [secretdataz]
  19969. */
  19970. BUILDIN_FUNC(setrandomoption) {
  19971. struct map_session_data *sd;
  19972. struct s_random_opt_data *opt;
  19973. int pos, index, id, value, param, ep;
  19974. int i = -1;
  19975. if (!script_charid2sd(7, sd))
  19976. return SCRIPT_CMD_FAILURE;
  19977. pos = script_getnum(st, 2);
  19978. index = script_getnum(st, 3);
  19979. id = script_getnum(st, 4);
  19980. value = script_getnum(st, 5);
  19981. param = script_getnum(st, 6);
  19982. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19983. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19984. script_pushint(st, 0);
  19985. return SCRIPT_CMD_FAILURE;
  19986. }
  19987. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19988. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19989. script_pushint(st, 0);
  19990. return SCRIPT_CMD_FAILURE;
  19991. }
  19992. if (equip_index_check(pos))
  19993. i = pc_checkequip(sd, equip_bitmask[pos]);
  19994. if (i >= 0) {
  19995. ep = sd->inventory.u.items_inventory[i].equip;
  19996. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19997. pc_unequipitem(sd, i, 2);
  19998. sd->inventory.u.items_inventory[i].option[index].id = id;
  19999. sd->inventory.u.items_inventory[i].option[index].value = value;
  20000. sd->inventory.u.items_inventory[i].option[index].param = param;
  20001. clif_delitem(sd, i, 1, 3);
  20002. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  20003. clif_additem(sd, i, 1, 0);
  20004. pc_equipitem(sd, i, ep);
  20005. script_pushint(st, 1);
  20006. return SCRIPT_CMD_SUCCESS;
  20007. }
  20008. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  20009. script_pushint(st, 0);
  20010. return SCRIPT_CMD_FAILURE;
  20011. }
  20012. /// Returns the number of stat points needed to change the specified stat by val.
  20013. /// If val is negative, returns the number of stat points that would be needed to
  20014. /// raise the specified stat from (current value - val) to current value.
  20015. /// *needed_status_point(<type>,<val>{,<char id>});
  20016. /// @author [secretdataz]
  20017. BUILDIN_FUNC(needed_status_point) {
  20018. struct map_session_data *sd;
  20019. int type, val;
  20020. if (!script_charid2sd(4, sd))
  20021. return SCRIPT_CMD_FAILURE;
  20022. type = script_getnum(st, 2);
  20023. val = script_getnum(st, 3);
  20024. script_pushint(st, pc_need_status_point(sd, type, val));
  20025. return SCRIPT_CMD_SUCCESS;
  20026. }
  20027. /**
  20028. * jobcanentermap("<mapname>"{,<JobID>});
  20029. * Check if (player with) JobID can enter the map.
  20030. * @param mapname Map name
  20031. * @param JobID Player's JobID (optional)
  20032. **/
  20033. BUILDIN_FUNC(jobcanentermap) {
  20034. const char *mapname = script_getstr(st, 2);
  20035. int mapidx = mapindex_name2id(mapname), m = -1;
  20036. int jobid = 0;
  20037. TBL_PC *sd = NULL;
  20038. if (!mapidx) {// Invalid map
  20039. script_pushint(st, false);
  20040. return SCRIPT_CMD_FAILURE;
  20041. }
  20042. m = map_mapindex2mapid(mapidx);
  20043. if (m == -1) { // Map is on different map server
  20044. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  20045. script_pushint(st, false);
  20046. return SCRIPT_CMD_FAILURE;
  20047. }
  20048. if (script_hasdata(st, 3)) {
  20049. jobid = script_getnum(st, 3);
  20050. } else {
  20051. if (!script_rid2sd(sd)) {
  20052. script_pushint(st, false);
  20053. return SCRIPT_CMD_FAILURE;
  20054. }
  20055. jobid = sd->status.class_;
  20056. }
  20057. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  20058. return SCRIPT_CMD_SUCCESS;
  20059. }
  20060. /**
  20061. * Return alliance information between the two guilds.
  20062. * getguildalliance(<guild id1>,<guild id2>);
  20063. * Return values:
  20064. * -2 - Guild ID1 does not exist
  20065. * -1 - Guild ID2 does not exist
  20066. * 0 - Both guilds have no relation OR guild ID aren't given
  20067. * 1 - Both guilds are allies
  20068. * 2 - Both guilds are antagonists
  20069. */
  20070. BUILDIN_FUNC(getguildalliance)
  20071. {
  20072. struct guild *guild_data1, *guild_data2;
  20073. int guild_id1, guild_id2, i = 0;
  20074. guild_id1 = script_getnum(st,2);
  20075. guild_id2 = script_getnum(st,3);
  20076. if (guild_id1 < 1 || guild_id2 < 1) {
  20077. script_pushint(st, 0);
  20078. return SCRIPT_CMD_SUCCESS;
  20079. }
  20080. if (guild_id1 == guild_id2) {
  20081. script_pushint(st, 1);
  20082. return SCRIPT_CMD_SUCCESS;
  20083. }
  20084. guild_data1 = guild_search(guild_id1);
  20085. guild_data2 = guild_search(guild_id2);
  20086. if (guild_data1 == NULL) {
  20087. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  20088. script_pushint(st, -2);
  20089. return SCRIPT_CMD_FAILURE;
  20090. }
  20091. if (guild_data2 == NULL) {
  20092. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  20093. script_pushint(st, -1);
  20094. return SCRIPT_CMD_FAILURE;
  20095. }
  20096. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  20097. if (i == MAX_GUILDALLIANCE) {
  20098. script_pushint(st, 0);
  20099. return SCRIPT_CMD_SUCCESS;
  20100. }
  20101. if (guild_data1->alliance[i].opposition)
  20102. script_pushint(st, 2);
  20103. else
  20104. script_pushint(st, 1);
  20105. return SCRIPT_CMD_SUCCESS;
  20106. }
  20107. /*
  20108. * openstorage2 <storage_id>,<mode>{,<account_id>}
  20109. * mode @see enum e_storage_mode
  20110. **/
  20111. BUILDIN_FUNC(openstorage2) {
  20112. int stor_id = script_getnum(st, 2);
  20113. TBL_PC *sd = NULL;
  20114. if (!script_accid2sd(4, sd)) {
  20115. script_pushint(st, 0);
  20116. return SCRIPT_CMD_FAILURE;
  20117. }
  20118. if (!storage_exists(stor_id)) {
  20119. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  20120. return SCRIPT_CMD_FAILURE;
  20121. }
  20122. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  20123. return SCRIPT_CMD_SUCCESS;
  20124. }
  20125. /**
  20126. * Create a new channel
  20127. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  20128. * @author [Cydh]
  20129. **/
  20130. BUILDIN_FUNC(channel_create) {
  20131. struct Channel tmp_chan, *ch = NULL;
  20132. const char *chname = script_getstr(st,2), *pass = NULL;
  20133. int i = channel_chk((char*)chname, NULL, 3);
  20134. TBL_PC *sd = NULL;
  20135. if (i != 0) {
  20136. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  20137. script_pushint(st,i);
  20138. return SCRIPT_CMD_FAILURE;
  20139. }
  20140. memset(&tmp_chan, 0, sizeof(struct Channel));
  20141. if (script_hasdata(st,8)) {
  20142. tmp_chan.char_id = script_getnum(st,8);
  20143. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  20144. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  20145. script_pushint(st,-5);
  20146. return SCRIPT_CMD_FAILURE;
  20147. }
  20148. tmp_chan.type = CHAN_TYPE_PRIVATE;
  20149. i = 1;
  20150. }
  20151. else {
  20152. tmp_chan.type = CHAN_TYPE_PUBLIC;
  20153. i = 0;
  20154. }
  20155. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  20156. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  20157. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  20158. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  20159. if (script_hasdata(st,5))
  20160. tmp_chan.opt = script_getnum(st,5);
  20161. else
  20162. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  20163. if (script_hasdata(st,6))
  20164. tmp_chan.msg_delay = script_getnum(st,6);
  20165. else
  20166. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  20167. if (script_hasdata(st,7))
  20168. tmp_chan.color = script_getnum(st,7);
  20169. else
  20170. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  20171. if (!(ch = channel_create(&tmp_chan))) {
  20172. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  20173. script_pushint(st,0);
  20174. return SCRIPT_CMD_FAILURE;
  20175. }
  20176. if (tmp_chan.char_id)
  20177. channel_join(ch, sd);
  20178. script_pushint(st,1);
  20179. return SCRIPT_CMD_SUCCESS;
  20180. }
  20181. /**
  20182. * Set channel option
  20183. * channel_setopt "<chname>",<option>,<value>;
  20184. * @author [Cydh]
  20185. **/
  20186. BUILDIN_FUNC(channel_setopt) {
  20187. struct Channel *ch = NULL;
  20188. const char *chname = script_getstr(st,2);
  20189. int opt = script_getnum(st,3), value = script_getnum(st,4);
  20190. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20191. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  20192. script_pushint(st,0);
  20193. return SCRIPT_CMD_FAILURE;
  20194. }
  20195. switch (opt) {
  20196. case CHAN_OPT_ANNOUNCE_SELF:
  20197. case CHAN_OPT_ANNOUNCE_JOIN:
  20198. case CHAN_OPT_ANNOUNCE_LEAVE:
  20199. case CHAN_OPT_COLOR_OVERRIDE:
  20200. case CHAN_OPT_CAN_CHAT:
  20201. case CHAN_OPT_CAN_LEAVE:
  20202. case CHAN_OPT_AUTOJOIN:
  20203. if (value)
  20204. ch->opt |= opt;
  20205. else
  20206. ch->opt &= ~opt;
  20207. break;
  20208. case CHAN_OPT_MSG_DELAY:
  20209. ch->msg_delay = value;
  20210. break;
  20211. default:
  20212. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  20213. script_pushint(st,0);
  20214. return SCRIPT_CMD_FAILURE;
  20215. }
  20216. script_pushint(st,1);
  20217. return SCRIPT_CMD_SUCCESS;
  20218. }
  20219. /**
  20220. * Set channel color
  20221. * channel_setcolor "<chname>",<color>;
  20222. * @author [Cydh]
  20223. **/
  20224. BUILDIN_FUNC(channel_setcolor) {
  20225. struct Channel *ch = NULL;
  20226. const char *chname = script_getstr(st,2);
  20227. int color = script_getnum(st,3);
  20228. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20229. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  20230. script_pushint(st,0);
  20231. return SCRIPT_CMD_FAILURE;
  20232. }
  20233. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  20234. script_pushint(st,1);
  20235. return SCRIPT_CMD_SUCCESS;
  20236. }
  20237. /**
  20238. * Set channel password
  20239. * channel_setpass "<chname>","<password>";
  20240. * @author [Cydh]
  20241. **/
  20242. BUILDIN_FUNC(channel_setpass) {
  20243. struct Channel *ch = NULL;
  20244. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  20245. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20246. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  20247. script_pushint(st,0);
  20248. return SCRIPT_CMD_FAILURE;
  20249. }
  20250. if (!passwd || !strcmpi(passwd,"null"))
  20251. memset(ch->pass, '\0', sizeof(ch->pass));
  20252. else
  20253. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  20254. script_pushint(st,1);
  20255. return SCRIPT_CMD_SUCCESS;
  20256. }
  20257. /**
  20258. * Set authorized groups
  20259. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  20260. * @author [Cydh]
  20261. **/
  20262. BUILDIN_FUNC(channel_setgroup) {
  20263. struct Channel *ch = NULL;
  20264. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  20265. int i, n = 0, group = 0;
  20266. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20267. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  20268. script_pushint(st,0);
  20269. return SCRIPT_CMD_FAILURE;
  20270. }
  20271. if (funcname[strlen(funcname)-1] == '2') {
  20272. struct script_data *data = script_getdata(st,3);
  20273. const char *varname = reference_getname(data);
  20274. int32 id, idx;
  20275. if (varname[strlen(varname)-1] == '$') {
  20276. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  20277. script_pushint(st,0);
  20278. return SCRIPT_CMD_FAILURE;
  20279. }
  20280. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  20281. if (n < 1) {
  20282. ShowError("buildin_channel_setgroup: No group id listed.\n");
  20283. script_pushint(st,0);
  20284. return SCRIPT_CMD_FAILURE;
  20285. }
  20286. if (ch->groups)
  20287. aFree(ch->groups);
  20288. ch->groups = NULL;
  20289. ch->group_count = 0;
  20290. id = reference_getid(data);
  20291. idx = reference_getindex(data);
  20292. for (i = 0; i < n; i++) {
  20293. group = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id,idx+i),reference_getref(data)));
  20294. if (group == 0) {
  20295. script_pushint(st,1);
  20296. return SCRIPT_CMD_SUCCESS;
  20297. }
  20298. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  20299. ch->groups[ch->group_count-1] = group;
  20300. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20301. }
  20302. }
  20303. else {
  20304. group = script_getnum(st,3);
  20305. n = script_lastdata(st)-1;
  20306. if (n < 1) {
  20307. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  20308. script_pushint(st,0);
  20309. return SCRIPT_CMD_FAILURE;
  20310. }
  20311. if (ch->groups)
  20312. aFree(ch->groups);
  20313. ch->groups = NULL;
  20314. ch->group_count = 0;
  20315. if (group == 0) { // Removed group list
  20316. script_pushint(st,1);
  20317. return SCRIPT_CMD_SUCCESS;
  20318. }
  20319. CREATE(ch->groups, unsigned short, n);
  20320. for (i = 3; i < n+2; i++) {
  20321. ch->groups[ch->group_count++] = script_getnum(st,i);
  20322. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  20323. }
  20324. }
  20325. script_pushint(st,n);
  20326. return SCRIPT_CMD_SUCCESS;
  20327. }
  20328. /**
  20329. * Send message on channel
  20330. * channel_chat "<chname>","<message>"{,<color>};
  20331. * @author [Cydh]
  20332. **/
  20333. BUILDIN_FUNC(channel_chat) {
  20334. struct Channel *ch = NULL;
  20335. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  20336. char output[CHAT_SIZE_MAX+1];
  20337. unsigned long color = 0;
  20338. // Check also local channels
  20339. if (chname[0] != '#') { // By Map
  20340. int m = mapindex_name2id(chname);
  20341. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  20342. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  20343. script_pushint(st,0);
  20344. return SCRIPT_CMD_FAILURE;
  20345. }
  20346. if (!(ch = map[m].channel)) {
  20347. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20348. script_pushint(st,0);
  20349. return SCRIPT_CMD_SUCCESS;
  20350. }
  20351. }
  20352. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  20353. TBL_NPC *nd = map_id2nd(st->oid);
  20354. if (!nd || nd->bl.m == -1) {
  20355. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  20356. script_pushint(st,0);
  20357. return SCRIPT_CMD_FAILURE;
  20358. }
  20359. if (!(ch = map[nd->bl.m].channel)) {
  20360. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20361. script_pushint(st,0);
  20362. return SCRIPT_CMD_SUCCESS;
  20363. }
  20364. }
  20365. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20366. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  20367. script_pushint(st,0);
  20368. return SCRIPT_CMD_FAILURE;
  20369. }
  20370. if (!ch) {
  20371. script_pushint(st,0);
  20372. return SCRIPT_CMD_FAILURE;
  20373. }
  20374. color = ch->color;
  20375. FETCH(4, color);
  20376. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  20377. clif_channel_msg(ch,output,color);
  20378. script_pushint(st,1);
  20379. return SCRIPT_CMD_SUCCESS;
  20380. }
  20381. /**
  20382. * Ban player from a channel
  20383. * channel_ban "<chname>",<char_id>;
  20384. * @author [Cydh]
  20385. **/
  20386. BUILDIN_FUNC(channel_ban) {
  20387. struct Channel *ch = NULL;
  20388. const char *chname = script_getstr(st,2);
  20389. unsigned int char_id = script_getnum(st,3);
  20390. TBL_PC *tsd;
  20391. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20392. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  20393. script_pushint(st,0);
  20394. return SCRIPT_CMD_FAILURE;
  20395. }
  20396. if (ch->char_id == char_id) {
  20397. script_pushint(st,0);
  20398. return SCRIPT_CMD_SUCCESS;
  20399. }
  20400. tsd = map_charid2sd(char_id);
  20401. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20402. script_pushint(st,0);
  20403. return SCRIPT_CMD_SUCCESS;
  20404. }
  20405. if (!idb_exists(ch->banned, char_id)) {
  20406. struct chan_banentry *cbe;
  20407. char output[CHAT_SIZE_MAX+1];
  20408. CREATE(cbe, struct chan_banentry, 1);
  20409. cbe->char_id = char_id;
  20410. if (tsd) {
  20411. strcpy(cbe->char_name,tsd->status.name);
  20412. channel_clean(ch,tsd,0);
  20413. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  20414. }
  20415. else
  20416. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  20417. idb_put(ch->banned, char_id, cbe);
  20418. clif_channel_msg(ch,output,ch->color);
  20419. }
  20420. script_pushint(st,1);
  20421. return SCRIPT_CMD_SUCCESS;
  20422. }
  20423. /**
  20424. * Ban player from a channel
  20425. * channel_unban "<chname>",<char_id>;
  20426. * @author [Cydh]
  20427. **/
  20428. BUILDIN_FUNC(channel_unban) {
  20429. struct Channel *ch = NULL;
  20430. const char *chname = script_getstr(st,2);
  20431. unsigned int char_id = script_getnum(st,3);
  20432. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20433. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  20434. script_pushint(st,0);
  20435. return SCRIPT_CMD_FAILURE;
  20436. }
  20437. if (ch->char_id == char_id) {
  20438. script_pushint(st,0);
  20439. return SCRIPT_CMD_SUCCESS;
  20440. }
  20441. if (idb_exists(ch->banned, char_id)) {
  20442. char output[CHAT_SIZE_MAX+1];
  20443. TBL_PC *tsd = map_charid2sd(char_id);
  20444. if (tsd)
  20445. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  20446. else
  20447. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  20448. idb_remove(ch->banned, char_id);
  20449. clif_channel_msg(ch,output,ch->color);
  20450. }
  20451. script_pushint(st,1);
  20452. return SCRIPT_CMD_SUCCESS;
  20453. }
  20454. /**
  20455. * Kick player from a channel
  20456. * channel_kick "<chname>",<char_id>;
  20457. * channel_kick "<chname>","<char_name>";
  20458. * @author [Cydh]
  20459. **/
  20460. BUILDIN_FUNC(channel_kick) {
  20461. struct Channel *ch = NULL;
  20462. const char *chname = script_getstr(st,2);
  20463. struct script_data *data = script_getdata(st,3);
  20464. TBL_PC *tsd = NULL;
  20465. int res = 1;
  20466. get_val(st, data);
  20467. if (data_isstring(data)) {
  20468. if (!(tsd = map_nick2sd(conv_str(st,data),false))) {
  20469. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", conv_str(st,data));
  20470. script_pushint(st,0);
  20471. return SCRIPT_CMD_FAILURE;
  20472. }
  20473. }
  20474. else {
  20475. if (!(tsd = map_charid2sd(conv_num(st,data)))) {
  20476. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", conv_num(st,data));
  20477. script_pushint(st,0);
  20478. return SCRIPT_CMD_FAILURE;
  20479. }
  20480. }
  20481. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  20482. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  20483. script_pushint(st,0);
  20484. return SCRIPT_CMD_FAILURE;
  20485. }
  20486. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20487. script_pushint(st,0);
  20488. return SCRIPT_CMD_SUCCESS;
  20489. }
  20490. switch(ch->type){
  20491. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  20492. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  20493. default: res = channel_clean(ch,tsd,0); break;
  20494. }
  20495. if (res == 0) {
  20496. char output[CHAT_SIZE_MAX+1];
  20497. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  20498. clif_channel_msg(ch,output,ch->color);
  20499. }
  20500. script_pushint(st,1);
  20501. return SCRIPT_CMD_SUCCESS;
  20502. }
  20503. /**
  20504. * Delete a channel
  20505. * channel_delete "<chname>";
  20506. * @author [Cydh]
  20507. **/
  20508. BUILDIN_FUNC(channel_delete) {
  20509. struct Channel *ch = NULL;
  20510. const char *chname = script_getstr(st,2);
  20511. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20512. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  20513. script_pushint(st,-1);
  20514. return SCRIPT_CMD_FAILURE;
  20515. }
  20516. script_pushint(st,channel_delete(ch,true));
  20517. return SCRIPT_CMD_SUCCESS;
  20518. }
  20519. BUILDIN_FUNC(unloadnpc) {
  20520. const char *name;
  20521. struct npc_data* nd;
  20522. name = script_getstr(st, 2);
  20523. nd = npc_name2id(name);
  20524. if( nd == NULL ){
  20525. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  20526. return SCRIPT_CMD_FAILURE;
  20527. } else if ( nd->bl.id == st->oid ) {
  20528. // Supporting self-unload isn't worth the problem it may cause. [Secret]
  20529. ShowError("buildin_unloadnpc: You cannot self-unload NPC '%s'.\n.", name);
  20530. return SCRIPT_CMD_FAILURE;
  20531. }
  20532. npc_unload_duplicates(nd);
  20533. npc_unload(nd, true);
  20534. npc_read_event_script();
  20535. return SCRIPT_CMD_SUCCESS;
  20536. }
  20537. /**
  20538. * Add an achievement to the player's log
  20539. * achievementadd(<achievement ID>{,<char ID>});
  20540. */
  20541. BUILDIN_FUNC(achievementadd) {
  20542. struct map_session_data *sd;
  20543. int achievement_id = script_getnum(st, 2);
  20544. if (!script_charid2sd(3, sd)) {
  20545. script_pushint(st, false);
  20546. return SCRIPT_CMD_FAILURE;
  20547. }
  20548. if (achievement_exists(achievement_id) == false) {
  20549. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  20550. script_pushint(st, false);
  20551. return SCRIPT_CMD_FAILURE;
  20552. }
  20553. if( !sd->state.pc_loaded ){
  20554. if( !running_npc_stat_calc_event ){
  20555. ShowError( "buildin_achievementadd: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20556. return SCRIPT_CMD_FAILURE;
  20557. }else{
  20558. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20559. return SCRIPT_CMD_SUCCESS;
  20560. }
  20561. }
  20562. if (achievement_add(sd, achievement_id))
  20563. script_pushint(st, true);
  20564. else
  20565. script_pushint(st, false);
  20566. return SCRIPT_CMD_SUCCESS;
  20567. }
  20568. /**
  20569. * Removes an achievement on a player.
  20570. * achievementremove(<achievement ID>{,<char ID>});
  20571. * Just for Atemo. ;)
  20572. */
  20573. BUILDIN_FUNC(achievementremove) {
  20574. struct map_session_data *sd;
  20575. int achievement_id = script_getnum(st, 2);
  20576. if (!script_charid2sd(3, sd)) {
  20577. script_pushint(st, false);
  20578. return SCRIPT_CMD_FAILURE;
  20579. }
  20580. if (achievement_exists(achievement_id) == false) {
  20581. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  20582. script_pushint(st, false);
  20583. return SCRIPT_CMD_SUCCESS;
  20584. }
  20585. if( !sd->state.pc_loaded ){
  20586. if( !running_npc_stat_calc_event ){
  20587. ShowError( "buildin_achievementremove: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20588. return SCRIPT_CMD_FAILURE;
  20589. }else{
  20590. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20591. return SCRIPT_CMD_SUCCESS;
  20592. }
  20593. }
  20594. if (achievement_remove(sd, achievement_id))
  20595. script_pushint(st, true);
  20596. else
  20597. script_pushint(st, false);
  20598. return SCRIPT_CMD_SUCCESS;
  20599. }
  20600. /**
  20601. * Returns achievement progress
  20602. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  20603. */
  20604. BUILDIN_FUNC(achievementinfo) {
  20605. struct map_session_data *sd;
  20606. int achievement_id = script_getnum(st, 2);
  20607. if (!script_charid2sd(4, sd)) {
  20608. script_pushint(st, false);
  20609. return SCRIPT_CMD_FAILURE;
  20610. }
  20611. if (achievement_exists(achievement_id) == false) {
  20612. ShowWarning("buildin_achievementinfo: Achievement '%d' doesn't exist.\n", achievement_id);
  20613. script_pushint(st, false);
  20614. return SCRIPT_CMD_FAILURE;
  20615. }
  20616. if( !sd->state.pc_loaded ){
  20617. script_pushint(st, false);
  20618. if( !running_npc_stat_calc_event ){
  20619. ShowError( "buildin_achievementinfo: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20620. return SCRIPT_CMD_FAILURE;
  20621. }else{
  20622. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20623. return SCRIPT_CMD_SUCCESS;
  20624. }
  20625. }
  20626. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  20627. return SCRIPT_CMD_SUCCESS;
  20628. }
  20629. /**
  20630. * Award an achievement; Ignores requirements
  20631. * achievementcomplete(<achievement ID>{,<char ID>});
  20632. */
  20633. BUILDIN_FUNC(achievementcomplete) {
  20634. struct map_session_data *sd;
  20635. int i, achievement_id = script_getnum(st, 2);
  20636. if (!script_charid2sd(3, sd)) {
  20637. script_pushint(st, false);
  20638. return SCRIPT_CMD_FAILURE;
  20639. }
  20640. if (achievement_exists(achievement_id) == false) {
  20641. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  20642. script_pushint(st, false);
  20643. return SCRIPT_CMD_FAILURE;
  20644. }
  20645. if( !sd->state.pc_loaded ){
  20646. if( !running_npc_stat_calc_event ){
  20647. ShowError( "buildin_achievementcomplete: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20648. return SCRIPT_CMD_FAILURE;
  20649. }else{
  20650. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20651. return SCRIPT_CMD_SUCCESS;
  20652. }
  20653. }
  20654. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20655. if (i == sd->achievement_data.count)
  20656. achievement_add(sd, achievement_id);
  20657. achievement_update_achievement(sd, achievement_id, true);
  20658. script_pushint(st, true);
  20659. return SCRIPT_CMD_SUCCESS;
  20660. }
  20661. /**
  20662. * Checks if the achievement exists on player.
  20663. * achievementexists(<achievement ID>{,<char ID>});
  20664. */
  20665. BUILDIN_FUNC(achievementexists) {
  20666. struct map_session_data *sd;
  20667. int i, achievement_id = script_getnum(st, 2);
  20668. if (!script_charid2sd(3, sd)) {
  20669. script_pushint(st, false);
  20670. return SCRIPT_CMD_FAILURE;
  20671. }
  20672. if (achievement_exists(achievement_id) == false) {
  20673. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  20674. script_pushint(st, false);
  20675. return SCRIPT_CMD_SUCCESS;
  20676. }
  20677. if( !sd->state.pc_loaded ){
  20678. script_pushint(st, false);
  20679. if( !running_npc_stat_calc_event ){
  20680. ShowError( "buildin_achievementexists: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20681. return SCRIPT_CMD_FAILURE;
  20682. }else{
  20683. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20684. return SCRIPT_CMD_SUCCESS;
  20685. }
  20686. }
  20687. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20688. script_pushint(st, i < sd->achievement_data.count ? true : false);
  20689. return SCRIPT_CMD_SUCCESS;
  20690. }
  20691. /**
  20692. * Updates an achievement's value.
  20693. * achievementupdate(<achievement ID>,<type>,<value>{,<char ID>});
  20694. */
  20695. BUILDIN_FUNC(achievementupdate) {
  20696. struct map_session_data *sd;
  20697. int i, achievement_id, type, value;
  20698. achievement_id = script_getnum(st, 2);
  20699. type = script_getnum(st, 3);
  20700. value = script_getnum(st, 4);
  20701. if (!script_charid2sd(5, sd)) {
  20702. script_pushint(st, false);
  20703. return SCRIPT_CMD_FAILURE;
  20704. }
  20705. if (achievement_exists(achievement_id) == false) {
  20706. ShowWarning("buildin_achievementupdate: Achievement '%d' doesn't exist.\n", achievement_id);
  20707. script_pushint(st, false);
  20708. return SCRIPT_CMD_FAILURE;
  20709. }
  20710. if( !sd->state.pc_loaded ){
  20711. if( !running_npc_stat_calc_event ){
  20712. ShowError( "buildin_achievementupdate: call was too early. Character %s(CID: '%u') was not loaded yet.\n", sd->status.name, sd->status.char_id );
  20713. return SCRIPT_CMD_FAILURE;
  20714. }else{
  20715. // Simply ignore it on the first call, because the status will be recalculated after loading anyway
  20716. return SCRIPT_CMD_SUCCESS;
  20717. }
  20718. }
  20719. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20720. if (i == sd->achievement_data.count)
  20721. achievement_add(sd, achievement_id);
  20722. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20723. if (i == sd->achievement_data.count) {
  20724. script_pushint(st, false);
  20725. return SCRIPT_CMD_SUCCESS;
  20726. }
  20727. if (type >= ACHIEVEINFO_COUNT1 && type <= ACHIEVEINFO_COUNT10)
  20728. sd->achievement_data.achievements[i].count[type - 1] = value;
  20729. else if (type == ACHIEVEINFO_COMPLETE || type == ACHIEVEINFO_COMPLETEDATE)
  20730. sd->achievement_data.achievements[i].completed = value;
  20731. else if (type == ACHIEVEINFO_GOTREWARD)
  20732. sd->achievement_data.achievements[i].rewarded = value;
  20733. else {
  20734. ShowWarning("buildin_achievementupdate: Unknown type '%d'.\n", type);
  20735. script_pushint(st, false);
  20736. return SCRIPT_CMD_FAILURE;
  20737. }
  20738. achievement_update_achievement(sd, achievement_id, false);
  20739. script_pushint(st, true);
  20740. return SCRIPT_CMD_SUCCESS;
  20741. }
  20742. /**
  20743. * Get an equipment's refine cost
  20744. * getequiprefinecost(<equipment slot>,<type>,<information>{,<char id>})
  20745. * returns -1 on fail
  20746. */
  20747. BUILDIN_FUNC(getequiprefinecost) {
  20748. int i = -1, slot, type, info;
  20749. map_session_data *sd;
  20750. slot = script_getnum(st, 2);
  20751. type = script_getnum(st, 3);
  20752. info = script_getnum(st, 4);
  20753. if (!script_charid2sd(5, sd)) {
  20754. script_pushint(st, 0);
  20755. return SCRIPT_CMD_FAILURE;
  20756. }
  20757. if (type < 0 || type >= REFINE_COST_MAX) {
  20758. script_pushint(st, -1);
  20759. return SCRIPT_CMD_SUCCESS;
  20760. }
  20761. if (equip_index_check(slot))
  20762. i = pc_checkequip(sd, equip_bitmask[slot]);
  20763. if (i < 0) {
  20764. script_pushint(st, -1);
  20765. return SCRIPT_CMD_SUCCESS;
  20766. }
  20767. int weapon_lv = sd->inventory_data[i]->wlv;
  20768. if (sd->inventory_data[i]->type == IT_SHADOWGEAR) {
  20769. if (sd->inventory_data[i]->equip == EQP_SHADOW_WEAPON)
  20770. weapon_lv = REFINE_TYPE_WEAPON4;
  20771. else
  20772. weapon_lv = REFINE_TYPE_SHADOW;
  20773. }
  20774. script_pushint(st, status_get_refine_cost(weapon_lv, type, info != 0));
  20775. return SCRIPT_CMD_SUCCESS;
  20776. }
  20777. /**
  20778. * Round, floor, ceiling a number to arbitrary integer precision.
  20779. * round(<number>,<precision>);
  20780. * ceil(<number>,<precision>);
  20781. * floor(<number>,<precision>);
  20782. */
  20783. BUILDIN_FUNC(round) {
  20784. int num = script_getnum(st, 2);
  20785. int precision = script_getnum(st, 3);
  20786. char* func = script_getfuncname(st);
  20787. if (precision <= 0) {
  20788. ShowError("buildin_round: Attempted to use zero or negative number as arbitrary precision.\n");
  20789. return SCRIPT_CMD_FAILURE;
  20790. }
  20791. if (strcasecmp(func, "floor") == 0) {
  20792. script_pushint(st, num - (num % precision));
  20793. }
  20794. else if (strcasecmp(func, "ceil") == 0) {
  20795. script_pushint(st, num + precision - (num % precision));
  20796. }
  20797. else {
  20798. script_pushint(st, (int)(round(num / (precision * 1.))) * precision);
  20799. }
  20800. return SCRIPT_CMD_SUCCESS;
  20801. }
  20802. BUILDIN_FUNC(getequiptradability) {
  20803. int i, num;
  20804. TBL_PC *sd;
  20805. num = script_getnum(st, 2);
  20806. if (!script_charid2sd(3, sd)) {
  20807. return SCRIPT_CMD_FAILURE;
  20808. }
  20809. if (equip_index_check(num))
  20810. i = pc_checkequip(sd, equip_bitmask[num]);
  20811. else{
  20812. ShowError("buildin_getequiptradability: Unknown equip index '%d'\n",num);
  20813. return SCRIPT_CMD_FAILURE;
  20814. }
  20815. if (i >= 0) {
  20816. bool tradable = (sd->inventory.u.items_inventory[i].expire_time == 0 &&
  20817. (!sd->inventory.u.items_inventory[i].bound || pc_can_give_bounded_items(sd)) &&
  20818. itemdb_cantrade(&sd->inventory.u.items_inventory[i], pc_get_group_level(sd), pc_get_group_level(sd))
  20819. );
  20820. script_pushint(st, tradable);
  20821. }
  20822. else
  20823. script_pushint(st, false);
  20824. return SCRIPT_CMD_SUCCESS;
  20825. }
  20826. 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 ){
  20827. const char *name;
  20828. // Check if it is a variable
  20829. if( !data_isreference(data) ){
  20830. ShowError("buildin_mail: argument %d is not a variable.\n", i );
  20831. return false;
  20832. }
  20833. name = reference_getname(data);
  20834. if( is_string_variable(name) ){
  20835. ShowError( "buildin_mail: variable \"%s\" is a string variable.\n", name );
  20836. return false;
  20837. }
  20838. // Check if the variable requires a player
  20839. if( not_server_variable(*name) && sd == NULL ){
  20840. // If no player is attached
  20841. if( !script_rid2sd(sd) ){
  20842. ShowError( "buildin_mail: variable \"%s\" was not a server variable, but no player was attached.\n", name );
  20843. return false;
  20844. }
  20845. }
  20846. // Try to find the array's source pointer
  20847. if( !script_array_src( st, sd, name, reference_getref(data) ) ){
  20848. ShowError( "buildin_mail: variable \"%s\" is not an array.\n" );
  20849. return false;
  20850. }
  20851. // Store the name for later usage
  20852. *out_name = name;
  20853. // Get the start and end indices of the array
  20854. *start = reference_getindex(data);
  20855. *end = script_array_highest_key( st, sd, name, reference_getref(data) );
  20856. // For getting the values we need the id of the array
  20857. *id = reference_getid(data);
  20858. return true;
  20859. }
  20860. BUILDIN_FUNC(mail){
  20861. const char *sender, *title, *body, *name;
  20862. struct mail_message msg;
  20863. struct script_data *data;
  20864. struct map_session_data *sd = NULL;
  20865. unsigned int i, j, k, num_items, start, end;
  20866. int32 id;
  20867. memset(&msg, 0, sizeof(struct mail_message));
  20868. msg.dest_id = script_getnum(st,2);
  20869. sender = script_getstr(st, 3);
  20870. if( strlen(sender) > NAME_LENGTH ){
  20871. ShowError( "buildin_mail: sender name can not be longer than %d characters.\n", NAME_LENGTH );
  20872. return SCRIPT_CMD_FAILURE;
  20873. }
  20874. safestrncpy(msg.send_name, sender, NAME_LENGTH);
  20875. title = script_getstr(st, 4);
  20876. if( strlen(title) > MAIL_TITLE_LENGTH ){
  20877. ShowError( "buildin_mail: title can not be longer than %d characters.\n", MAIL_TITLE_LENGTH );
  20878. return SCRIPT_CMD_FAILURE;
  20879. }
  20880. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  20881. body = script_getstr(st, 5);
  20882. if( strlen(body) > MAIL_BODY_LENGTH ){
  20883. ShowError( "buildin_mail: body can not be longer than %d characters.\n", MAIL_BODY_LENGTH );
  20884. return SCRIPT_CMD_FAILURE;
  20885. }
  20886. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  20887. if( script_hasdata(st,6) ){
  20888. int zeny = script_getnum(st, 6);
  20889. if( zeny < 0 ){
  20890. ShowError( "buildin_mail: a negative amount of zeny can not be sent.\n" );
  20891. return SCRIPT_CMD_FAILURE;
  20892. }else if( zeny > MAX_ZENY ){
  20893. ShowError( "buildin_mail: amount of zeny %u is exceeding maximum of %u. Capping...\n", zeny, MAX_ZENY );
  20894. zeny = MAX_ZENY;
  20895. }
  20896. msg.zeny = zeny;
  20897. }
  20898. // Items
  20899. num_items = 0;
  20900. while( script_hasdata(st,7) ){
  20901. data = script_getdata(st,7);
  20902. if( !mail_sub( st, data, sd, 7, &name, &start, &end, &id ) ){
  20903. return SCRIPT_CMD_FAILURE;
  20904. }
  20905. num_items = end - start;
  20906. if( num_items == 0 ){
  20907. ShowWarning( "buildin_mail: array \"%s\" contained no items.\n", name );
  20908. break;
  20909. }
  20910. if( num_items > MAIL_MAX_ITEM ){
  20911. ShowWarning( "buildin_mail: array \"%s\" contained %d items, capping to maximum of %d...\n", name, num_items, MAIL_MAX_ITEM );
  20912. num_items = MAIL_MAX_ITEM;
  20913. }
  20914. for( i = 0; i < num_items && start < end; i++, start++ ){
  20915. msg.item[i].nameid = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20916. msg.item[i].identify = 1;
  20917. script_removetop(st, -1, 0);
  20918. if( !itemdb_exists(msg.item[i].nameid) ){
  20919. ShowError( "buildin_mail: invalid item id %hu.\n", msg.item[i].nameid );
  20920. return SCRIPT_CMD_FAILURE;
  20921. }
  20922. }
  20923. // Amounts
  20924. if( !script_hasdata(st,8) ){
  20925. ShowError("buildin_mail: missing item count variable at position %d.\n", 8);
  20926. return SCRIPT_CMD_FAILURE;
  20927. }
  20928. data = script_getdata(st,8);
  20929. if( !mail_sub( st, data, sd, 8, &name, &start, &end, &id ) ){
  20930. return SCRIPT_CMD_FAILURE;
  20931. }
  20932. for( i = 0; i < num_items && start < end; i++, start++ ){
  20933. struct item_data *item = itemdb_exists(msg.item[i].nameid);
  20934. msg.item[i].amount = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20935. script_removetop(st, -1, 0);
  20936. if( msg.item[i].amount <= 0 ){
  20937. ShowError( "buildin_mail: amount %d for item %hu is invalid.\n", msg.item[i].amount, msg.item[i].nameid );
  20938. return SCRIPT_CMD_FAILURE;
  20939. }else if( itemdb_isstackable2(item) ){
  20940. uint16 max = item->stack.amount > 0 ? item->stack.amount : MAX_AMOUNT;
  20941. if( msg.item[i].amount > max ){
  20942. 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 );
  20943. msg.item[i].amount = max;
  20944. }
  20945. }else{
  20946. if( msg.item[i].amount > 1 ){
  20947. ShowWarning( "buildin_mail: amount %d is invalid for non-stackable item %hu.\n", msg.item[i].amount, msg.item[i].nameid );
  20948. msg.item[i].amount = 1;
  20949. }
  20950. }
  20951. }
  20952. // Cards
  20953. if( !script_hasdata(st,9) ){
  20954. break;
  20955. }
  20956. for( i = 0, j = 9; i < MAX_SLOTS && script_hasdata(st,j); i++, j++ ){
  20957. data = script_getdata(st,j);
  20958. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20959. return SCRIPT_CMD_FAILURE;
  20960. }
  20961. for( k = 0; k < num_items && start < end; k++, start++ ){
  20962. msg.item[k].card[i] = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20963. script_removetop(st, -1, 0);
  20964. if( msg.item[k].card[i] != 0 && !itemdb_exists(msg.item[k].card[i]) ){
  20965. ShowError( "buildin_mail: invalid card id %hu.\n", msg.item[k].card[i] );
  20966. return SCRIPT_CMD_FAILURE;
  20967. }
  20968. }
  20969. }
  20970. // Random Options
  20971. if( !script_hasdata(st,9 + MAX_SLOTS) ){
  20972. break;
  20973. }
  20974. 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++ ){
  20975. // Option IDs
  20976. data = script_getdata(st, j);
  20977. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20978. return SCRIPT_CMD_FAILURE;
  20979. }
  20980. for( k = 0; k < num_items && start < end; k++, start++ ){
  20981. msg.item[k].option[i].id = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20982. script_removetop(st, -1, 0);
  20983. }
  20984. j++;
  20985. // Option values
  20986. data = script_getdata(st, j);
  20987. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20988. return SCRIPT_CMD_FAILURE;
  20989. }
  20990. for( k = 0; k < num_items && start < end; k++, start++ ){
  20991. msg.item[k].option[i].value = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  20992. script_removetop(st, -1, 0);
  20993. }
  20994. j++;
  20995. // Option parameters
  20996. data = script_getdata(st, j);
  20997. if( !mail_sub( st, data, sd, j + 1, &name, &start, &end, &id ) ){
  20998. return SCRIPT_CMD_FAILURE;
  20999. }
  21000. for( k = 0; k < num_items && start < end; k++, start++ ){
  21001. msg.item[k].option[i].param = (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) );
  21002. script_removetop(st, -1, 0);
  21003. }
  21004. }
  21005. // Break the pseudo scope
  21006. break;
  21007. }
  21008. msg.status = MAIL_NEW;
  21009. msg.type = MAIL_INBOX_NORMAL;
  21010. msg.timestamp = time(NULL);
  21011. intif_Mail_send(0, &msg);
  21012. return SCRIPT_CMD_SUCCESS;
  21013. }
  21014. #include "../custom/script.inc"
  21015. // declarations that were supposed to be exported from npc_chat.c
  21016. #ifdef PCRE_SUPPORT
  21017. BUILDIN_FUNC(defpattern);
  21018. BUILDIN_FUNC(activatepset);
  21019. BUILDIN_FUNC(deactivatepset);
  21020. BUILDIN_FUNC(deletepset);
  21021. #endif
  21022. /** Regular expression matching
  21023. * preg_match(<pattern>,<string>{,<offset>})
  21024. */
  21025. BUILDIN_FUNC(preg_match) {
  21026. #ifdef PCRE_SUPPORT
  21027. pcre *re;
  21028. pcre_extra *pcreExtra;
  21029. const char *error;
  21030. int erroffset, r, offset = 0;
  21031. int subStrVec[30];
  21032. const char* pattern = script_getstr(st,2);
  21033. const char* subject = script_getstr(st,3);
  21034. if (script_hasdata(st,4))
  21035. offset = script_getnum(st,4);
  21036. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  21037. pcreExtra = pcre_study(re, 0, &error);
  21038. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  21039. pcre_free(re);
  21040. if (pcreExtra != NULL)
  21041. pcre_free(pcreExtra);
  21042. if (r < 0)
  21043. script_pushint(st,0);
  21044. else
  21045. script_pushint(st,(r > 0) ? r : 30 / 3);
  21046. return SCRIPT_CMD_SUCCESS;
  21047. #else
  21048. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  21049. script_pushint(st,0);
  21050. return SCRIPT_CMD_SUCCESS;
  21051. #endif
  21052. }
  21053. /// script command definitions
  21054. /// for an explanation on args, see add_buildin_func
  21055. struct script_function buildin_func[] = {
  21056. // NPC interaction
  21057. BUILDIN_DEF(mes,"s*"),
  21058. BUILDIN_DEF(next,""),
  21059. BUILDIN_DEF(clear,""),
  21060. BUILDIN_DEF(close,""),
  21061. BUILDIN_DEF(close2,""),
  21062. BUILDIN_DEF(menu,"sl*"),
  21063. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  21064. BUILDIN_DEF(prompt,"s*"),
  21065. //
  21066. BUILDIN_DEF(goto,"l"),
  21067. BUILDIN_DEF(callsub,"l*"),
  21068. BUILDIN_DEF(callfunc,"s*"),
  21069. BUILDIN_DEF(return,"?"),
  21070. BUILDIN_DEF(getarg,"i?"),
  21071. BUILDIN_DEF(jobchange,"i??"),
  21072. BUILDIN_DEF(jobname,"i"),
  21073. BUILDIN_DEF(input,"r??"),
  21074. BUILDIN_DEF(warp,"sii?"),
  21075. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  21076. BUILDIN_DEF(areawarp,"siiiisii??"),
  21077. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  21078. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  21079. BUILDIN_DEF(setlook,"ii?"),
  21080. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  21081. BUILDIN_DEF2(setr,"set","rv?"),
  21082. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  21083. BUILDIN_DEF(setarray,"rv*"),
  21084. BUILDIN_DEF(cleararray,"rvi"),
  21085. BUILDIN_DEF(copyarray,"rri"),
  21086. BUILDIN_DEF(getarraysize,"r"),
  21087. BUILDIN_DEF(deletearray,"r?"),
  21088. BUILDIN_DEF(getelementofarray,"ri"),
  21089. BUILDIN_DEF(inarray,"rv"),
  21090. BUILDIN_DEF(countinarray,"rr"),
  21091. BUILDIN_DEF(getitem,"vi?"),
  21092. BUILDIN_DEF(rentitem,"vi?"),
  21093. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  21094. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  21095. BUILDIN_DEF(getnameditem,"vv"),
  21096. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  21097. BUILDIN_DEF(makeitem,"visii"),
  21098. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  21099. BUILDIN_DEF(delitem,"vi?"),
  21100. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  21101. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  21102. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  21103. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  21104. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  21105. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  21106. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  21107. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  21108. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  21109. BUILDIN_DEF(cutin,"si"),
  21110. BUILDIN_DEF(viewpoint,"iiiii"),
  21111. BUILDIN_DEF(heal,"ii?"),
  21112. BUILDIN_DEF(itemheal,"ii?"),
  21113. BUILDIN_DEF(percentheal,"ii?"),
  21114. BUILDIN_DEF(rand,"i?"),
  21115. BUILDIN_DEF(countitem,"v?"),
  21116. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  21117. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  21118. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  21119. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  21120. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  21121. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  21122. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  21123. BUILDIN_DEF(checkweight,"vi*"),
  21124. BUILDIN_DEF(checkweight2,"rr"),
  21125. BUILDIN_DEF(readparam,"i?"),
  21126. BUILDIN_DEF(getcharid,"i?"),
  21127. BUILDIN_DEF(getnpcid,"i?"),
  21128. BUILDIN_DEF(getpartyname,"i"),
  21129. BUILDIN_DEF(getpartymember,"i??"),
  21130. BUILDIN_DEF(getpartyleader,"i?"),
  21131. BUILDIN_DEF(getguildname,"i"),
  21132. BUILDIN_DEF(getguildmaster,"i"),
  21133. BUILDIN_DEF(getguildmasterid,"i"),
  21134. BUILDIN_DEF(strcharinfo,"i?"),
  21135. BUILDIN_DEF(strnpcinfo,"i"),
  21136. BUILDIN_DEF(getequipid,"??"),
  21137. BUILDIN_DEF(getequipuniqueid,"i?"),
  21138. BUILDIN_DEF(getequipname,"i?"),
  21139. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  21140. BUILDIN_DEF(repair,"i?"), // [Valaris]
  21141. BUILDIN_DEF(repairall,"?"),
  21142. BUILDIN_DEF(getequipisequiped,"i?"),
  21143. BUILDIN_DEF(getequipisenableref,"i?"),
  21144. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  21145. BUILDIN_DEF(getequipweaponlv,"i?"),
  21146. BUILDIN_DEF(getequippercentrefinery,"i?"),
  21147. BUILDIN_DEF(successrefitem,"i??"),
  21148. BUILDIN_DEF(failedrefitem,"i?"),
  21149. BUILDIN_DEF(downrefitem,"i??"),
  21150. BUILDIN_DEF(statusup,"i?"),
  21151. BUILDIN_DEF(statusup2,"ii?"),
  21152. BUILDIN_DEF(bonus,"i?"),
  21153. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  21154. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  21155. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  21156. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  21157. BUILDIN_DEF(autobonus,"sii??"),
  21158. BUILDIN_DEF(autobonus2,"sii??"),
  21159. BUILDIN_DEF(autobonus3,"siiv?"),
  21160. BUILDIN_DEF(skill,"vi?"),
  21161. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  21162. BUILDIN_DEF(guildskill,"vi"),
  21163. BUILDIN_DEF(getskilllv,"v"),
  21164. BUILDIN_DEF(getgdskilllv,"iv"),
  21165. BUILDIN_DEF(basicskillcheck,""),
  21166. BUILDIN_DEF(getgmlevel,"?"),
  21167. BUILDIN_DEF(getgroupid,"?"),
  21168. BUILDIN_DEF(end,""),
  21169. BUILDIN_DEF(checkoption,"i?"),
  21170. BUILDIN_DEF(setoption,"i??"),
  21171. BUILDIN_DEF(setcart,"??"),
  21172. BUILDIN_DEF(checkcart,"?"),
  21173. BUILDIN_DEF(setfalcon,"??"),
  21174. BUILDIN_DEF(checkfalcon,"?"),
  21175. BUILDIN_DEF(setriding,"??"),
  21176. BUILDIN_DEF(checkriding,"?"),
  21177. BUILDIN_DEF(checkwug,"?"),
  21178. BUILDIN_DEF(checkmadogear,"?"),
  21179. BUILDIN_DEF(setmadogear,"??"),
  21180. BUILDIN_DEF2(savepoint,"save","sii???"),
  21181. BUILDIN_DEF(savepoint,"sii???"),
  21182. BUILDIN_DEF(gettimetick,"i"),
  21183. BUILDIN_DEF(gettime,"i"),
  21184. BUILDIN_DEF(gettimestr,"si?"),
  21185. BUILDIN_DEF(openstorage,""),
  21186. BUILDIN_DEF(guildopenstorage,""),
  21187. BUILDIN_DEF(itemskill,"vi?"),
  21188. BUILDIN_DEF(produce,"i"),
  21189. BUILDIN_DEF(cooking,"i"),
  21190. BUILDIN_DEF(monster,"siisii???"),
  21191. BUILDIN_DEF(getmobdrops,"i"),
  21192. BUILDIN_DEF(areamonster,"siiiisii???"),
  21193. BUILDIN_DEF(killmonster,"ss?"),
  21194. BUILDIN_DEF(killmonsterall,"s?"),
  21195. BUILDIN_DEF(clone,"siisi????"),
  21196. BUILDIN_DEF(doevent,"s"),
  21197. BUILDIN_DEF(donpcevent,"s"),
  21198. BUILDIN_DEF(cmdothernpc,"ss"),
  21199. BUILDIN_DEF(addtimer,"is"),
  21200. BUILDIN_DEF(deltimer,"s"),
  21201. BUILDIN_DEF(addtimercount,"is"),
  21202. BUILDIN_DEF(initnpctimer,"??"),
  21203. BUILDIN_DEF(stopnpctimer,"??"),
  21204. BUILDIN_DEF(startnpctimer,"??"),
  21205. BUILDIN_DEF(setnpctimer,"i?"),
  21206. BUILDIN_DEF(getnpctimer,"i?"),
  21207. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  21208. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  21209. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  21210. BUILDIN_DEF(announce,"si?????"),
  21211. BUILDIN_DEF(mapannounce,"ssi?????"),
  21212. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  21213. BUILDIN_DEF(getusers,"i"),
  21214. BUILDIN_DEF(getmapguildusers,"si"),
  21215. BUILDIN_DEF(getmapusers,"s"),
  21216. BUILDIN_DEF(getareausers,"siiii"),
  21217. BUILDIN_DEF(getareadropitem,"siiiiv"),
  21218. BUILDIN_DEF(enablenpc,"s"),
  21219. BUILDIN_DEF(disablenpc,"s"),
  21220. BUILDIN_DEF(hideoffnpc,"s"),
  21221. BUILDIN_DEF(hideonnpc,"s"),
  21222. BUILDIN_DEF(sc_start,"iii???"),
  21223. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  21224. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  21225. BUILDIN_DEF(sc_end,"i?"),
  21226. BUILDIN_DEF(sc_end_class,"??"),
  21227. BUILDIN_DEF(getstatus, "i??"),
  21228. BUILDIN_DEF(getscrate,"ii?"),
  21229. BUILDIN_DEF(debugmes,"s"),
  21230. BUILDIN_DEF2(catchpet,"pet","i"),
  21231. BUILDIN_DEF2(birthpet,"bpet",""),
  21232. BUILDIN_DEF(catchpet,"i"),
  21233. BUILDIN_DEF(birthpet,""),
  21234. BUILDIN_DEF(resetlvl,"i?"),
  21235. BUILDIN_DEF(resetstatus,"?"),
  21236. BUILDIN_DEF(resetskill,"?"),
  21237. BUILDIN_DEF(skillpointcount,"?"),
  21238. BUILDIN_DEF(changebase,"i?"),
  21239. BUILDIN_DEF(changesex,"?"),
  21240. BUILDIN_DEF(changecharsex,"?"),
  21241. BUILDIN_DEF(waitingroom,"si?????"),
  21242. BUILDIN_DEF(delwaitingroom,"?"),
  21243. BUILDIN_DEF(waitingroomkick,"ss"),
  21244. BUILDIN_DEF(getwaitingroomusers, "?"),
  21245. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  21246. BUILDIN_DEF(enablewaitingroomevent,"?"),
  21247. BUILDIN_DEF(disablewaitingroomevent,"?"),
  21248. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  21249. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  21250. BUILDIN_DEF(getwaitingroomstate,"i?"),
  21251. BUILDIN_DEF(warpwaitingpc,"sii?"),
  21252. BUILDIN_DEF(attachrid,"i"),
  21253. BUILDIN_DEF(addrid,"i?????"),
  21254. BUILDIN_DEF(detachrid,""),
  21255. BUILDIN_DEF(isloggedin,"i?"),
  21256. BUILDIN_DEF(setmapflagnosave,"ssii"),
  21257. BUILDIN_DEF(getmapflag,"si?"),
  21258. BUILDIN_DEF(setmapflag,"si??"),
  21259. BUILDIN_DEF(removemapflag,"si?"),
  21260. BUILDIN_DEF(pvpon,"s"),
  21261. BUILDIN_DEF(pvpoff,"s"),
  21262. BUILDIN_DEF(gvgon,"s"),
  21263. BUILDIN_DEF(gvgoff,"s"),
  21264. BUILDIN_DEF(emotion,"i?"),
  21265. BUILDIN_DEF(maprespawnguildid,"sii"),
  21266. BUILDIN_DEF(agitstart,""), // <Agit>
  21267. BUILDIN_DEF(agitend,""),
  21268. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  21269. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  21270. BUILDIN_DEF(getcastlename,"s"),
  21271. BUILDIN_DEF(getcastledata,"si"),
  21272. BUILDIN_DEF(setcastledata,"sii"),
  21273. BUILDIN_DEF(requestguildinfo,"i?"),
  21274. BUILDIN_DEF(getequipcardcnt,"i"),
  21275. BUILDIN_DEF(successremovecards,"i"),
  21276. BUILDIN_DEF(failedremovecards,"ii"),
  21277. BUILDIN_DEF(marriage,"s"),
  21278. BUILDIN_DEF2(wedding_effect,"wedding",""),
  21279. BUILDIN_DEF(divorce,"?"),
  21280. BUILDIN_DEF(ispartneron,"?"),
  21281. BUILDIN_DEF(getpartnerid,"?"),
  21282. BUILDIN_DEF(getchildid,"?"),
  21283. BUILDIN_DEF(getmotherid,"?"),
  21284. BUILDIN_DEF(getfatherid,"?"),
  21285. BUILDIN_DEF(warppartner,"sii"),
  21286. BUILDIN_DEF(getitemname,"v"),
  21287. BUILDIN_DEF(getitemslots,"i"),
  21288. BUILDIN_DEF(makepet,"i"),
  21289. BUILDIN_DEF(getexp,"ii?"),
  21290. BUILDIN_DEF(getinventorylist,"?"),
  21291. BUILDIN_DEF(getskilllist,"?"),
  21292. BUILDIN_DEF(clearitem,"?"),
  21293. BUILDIN_DEF(classchange,"i??"),
  21294. BUILDIN_DEF(misceffect,"i"),
  21295. BUILDIN_DEF(playBGM,"s"),
  21296. BUILDIN_DEF(playBGMall,"s?????"),
  21297. BUILDIN_DEF(soundeffect,"si"),
  21298. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  21299. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  21300. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  21301. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  21302. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  21303. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  21304. BUILDIN_DEF(petloot,"i"), // [Valaris]
  21305. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  21306. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  21307. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  21308. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  21309. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  21310. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  21311. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  21312. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  21313. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  21314. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  21315. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  21316. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  21317. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  21318. BUILDIN_DEF(npctalk,"s??"), // [Valaris]
  21319. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  21320. BUILDIN_DEF(mobcount,"ss"),
  21321. BUILDIN_DEF(getlook,"i?"),
  21322. BUILDIN_DEF(getsavepoint,"i?"),
  21323. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  21324. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  21325. BUILDIN_DEF(npcstop,""), // [Valaris]
  21326. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  21327. BUILDIN_DEF(mapid2name,"i"),
  21328. BUILDIN_DEF(checkoption1,"i?"),
  21329. BUILDIN_DEF(checkoption2,"i?"),
  21330. BUILDIN_DEF(guildgetexp,"i"),
  21331. BUILDIN_DEF(guildchangegm,"is"),
  21332. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  21333. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  21334. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  21335. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  21336. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  21337. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  21338. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  21339. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  21340. BUILDIN_DEF(night,""), // sets the server to night time
  21341. BUILDIN_DEF(day,""), // sets the server to day time
  21342. #ifdef PCRE_SUPPORT
  21343. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  21344. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  21345. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  21346. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  21347. #endif
  21348. BUILDIN_DEF(preg_match,"ss?"),
  21349. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  21350. BUILDIN_DEF(recovery,"i???"),
  21351. BUILDIN_DEF(getpetinfo,"i?"),
  21352. BUILDIN_DEF(gethominfo,"i?"),
  21353. BUILDIN_DEF(getmercinfo,"i?"),
  21354. BUILDIN_DEF(checkequipedcard,"i"),
  21355. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  21356. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  21357. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  21358. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  21359. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  21360. BUILDIN_DEF(charat,"si"),
  21361. BUILDIN_DEF(setchar,"ssi"),
  21362. BUILDIN_DEF(insertchar,"ssi"),
  21363. BUILDIN_DEF(delchar,"si"),
  21364. BUILDIN_DEF(strtoupper,"s"),
  21365. BUILDIN_DEF(strtolower,"s"),
  21366. BUILDIN_DEF(charisupper, "si"),
  21367. BUILDIN_DEF(charislower, "si"),
  21368. BUILDIN_DEF(substr,"sii"),
  21369. BUILDIN_DEF(explode, "rss"),
  21370. BUILDIN_DEF(implode, "r?"),
  21371. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  21372. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  21373. BUILDIN_DEF(strpos,"ss?"),
  21374. BUILDIN_DEF(replacestr,"sss??"),
  21375. BUILDIN_DEF(countstr,"ss?"),
  21376. BUILDIN_DEF(setnpcdisplay,"sv??"),
  21377. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  21378. BUILDIN_DEF(strcmp,"ss"),
  21379. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  21380. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  21381. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  21382. // [zBuffer] List of mathematics commands --->
  21383. BUILDIN_DEF(sqrt,"i"),
  21384. BUILDIN_DEF(pow,"ii"),
  21385. BUILDIN_DEF(distance,"iiii"),
  21386. BUILDIN_DEF2(minmax,"min", "*"),
  21387. BUILDIN_DEF2(minmax,"minimum", "*"),
  21388. BUILDIN_DEF2(minmax,"max", "*"),
  21389. BUILDIN_DEF2(minmax,"maximum", "*"),
  21390. // <--- [zBuffer] List of mathematics commands
  21391. BUILDIN_DEF(md5,"s"),
  21392. // [zBuffer] List of dynamic var commands --->
  21393. BUILDIN_DEF(getd,"s"),
  21394. BUILDIN_DEF(setd,"sv?"),
  21395. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  21396. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  21397. BUILDIN_DEF(npcshopadditem,"sii*"),
  21398. BUILDIN_DEF(npcshopdelitem,"si*"),
  21399. BUILDIN_DEF(npcshopattach,"s?"),
  21400. BUILDIN_DEF(equip,"i?"),
  21401. BUILDIN_DEF(autoequip,"ii"),
  21402. BUILDIN_DEF(setbattleflag,"si?"),
  21403. BUILDIN_DEF(getbattleflag,"s"),
  21404. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  21405. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  21406. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  21407. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  21408. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  21409. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  21410. BUILDIN_DEF(axtoi,"s"),
  21411. BUILDIN_DEF(query_sql,"s*"),
  21412. BUILDIN_DEF(query_logsql,"s*"),
  21413. BUILDIN_DEF(escape_sql,"v"),
  21414. BUILDIN_DEF(atoi,"s"),
  21415. BUILDIN_DEF(strtol,"si"),
  21416. // [zBuffer] List of player cont commands --->
  21417. BUILDIN_DEF(rid2name,"i"),
  21418. BUILDIN_DEF(pcfollow,"ii"),
  21419. BUILDIN_DEF(pcstopfollow,"i"),
  21420. BUILDIN_DEF(pcblockmove,"ii"),
  21421. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  21422. BUILDIN_DEF(pcblockskill,"ii"),
  21423. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  21424. // <--- [zBuffer] List of player cont commands
  21425. // [zBuffer] List of unit control commands --->
  21426. BUILDIN_DEF(unitexists,"i"),
  21427. BUILDIN_DEF(getunittype,"i"),
  21428. BUILDIN_DEF(getunitname,"i"),
  21429. BUILDIN_DEF(setunitname,"is"),
  21430. BUILDIN_DEF(getunitdata,"i*"),
  21431. BUILDIN_DEF(setunitdata,"iii"),
  21432. BUILDIN_DEF(unitwalk,"iii?"),
  21433. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  21434. BUILDIN_DEF(unitkill,"i"),
  21435. BUILDIN_DEF(unitwarp,"isii"),
  21436. BUILDIN_DEF(unitattack,"iv?"),
  21437. BUILDIN_DEF(unitstopattack,"i"),
  21438. BUILDIN_DEF(unitstopwalk,"i?"),
  21439. BUILDIN_DEF(unittalk,"is?"),
  21440. BUILDIN_DEF_DEPRECATED(unitemote,"ii","20170811"),
  21441. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  21442. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  21443. // <--- [zBuffer] List of unit control commands
  21444. BUILDIN_DEF(sleep,"i"),
  21445. BUILDIN_DEF(sleep2,"i"),
  21446. BUILDIN_DEF(awake,"s"),
  21447. BUILDIN_DEF(getvariableofnpc,"rs"),
  21448. BUILDIN_DEF(warpportal,"iisii"),
  21449. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  21450. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  21451. BUILDIN_DEF(morphembryo,""),
  21452. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  21453. BUILDIN_DEF(checkhomcall,""),
  21454. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  21455. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  21456. BUILDIN_DEF(checkvending,"?"),
  21457. BUILDIN_DEF(checkchatting,"?"),
  21458. BUILDIN_DEF(checkidle,"?"),
  21459. BUILDIN_DEF(openmail,"?"),
  21460. BUILDIN_DEF(openauction,"?"),
  21461. BUILDIN_DEF(checkcell,"siii"),
  21462. BUILDIN_DEF(setcell,"siiiiii"),
  21463. BUILDIN_DEF(getfreecell,"srr?????"),
  21464. BUILDIN_DEF(setwall,"siiiiis"),
  21465. BUILDIN_DEF(delwall,"s"),
  21466. BUILDIN_DEF(searchitem,"rs"),
  21467. BUILDIN_DEF(mercenary_create,"ii"),
  21468. BUILDIN_DEF(mercenary_heal,"ii"),
  21469. BUILDIN_DEF(mercenary_sc_start,"iii"),
  21470. BUILDIN_DEF(mercenary_get_calls,"i"),
  21471. BUILDIN_DEF(mercenary_get_faith,"i"),
  21472. BUILDIN_DEF(mercenary_set_calls,"ii"),
  21473. BUILDIN_DEF(mercenary_set_faith,"ii"),
  21474. BUILDIN_DEF(readbook,"ii"),
  21475. BUILDIN_DEF(setfont,"i"),
  21476. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  21477. BUILDIN_DEF(progressbar,"si"),
  21478. BUILDIN_DEF(progressbar_npc, "si?"),
  21479. BUILDIN_DEF(pushpc,"ii"),
  21480. BUILDIN_DEF(buyingstore,"i"),
  21481. BUILDIN_DEF(searchstores,"ii"),
  21482. BUILDIN_DEF(showdigit,"i?"),
  21483. // WoE SE
  21484. BUILDIN_DEF(agitstart2,""),
  21485. BUILDIN_DEF(agitend2,""),
  21486. BUILDIN_DEF(agitcheck2,""),
  21487. // BattleGround
  21488. BUILDIN_DEF(waitingroom2bg,"sii???"),
  21489. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  21490. BUILDIN_DEF(bg_team_setxy,"iii"),
  21491. BUILDIN_DEF(bg_warp,"isii"),
  21492. BUILDIN_DEF(bg_monster,"isiisi?"),
  21493. BUILDIN_DEF(bg_monster_set_team,"ii"),
  21494. BUILDIN_DEF(bg_leave,"?"),
  21495. BUILDIN_DEF(bg_destroy,"i"),
  21496. BUILDIN_DEF(areapercentheal,"siiiiii"),
  21497. BUILDIN_DEF(bg_get_data,"ii"),
  21498. BUILDIN_DEF(bg_getareausers,"isiiii"),
  21499. BUILDIN_DEF(bg_updatescore,"sii"),
  21500. BUILDIN_DEF(bg_join,"i????"),
  21501. BUILDIN_DEF(bg_create,"sii??"),
  21502. // Instancing
  21503. BUILDIN_DEF(instance_create,"s??"),
  21504. BUILDIN_DEF(instance_destroy,"?"),
  21505. BUILDIN_DEF(instance_id,""),
  21506. BUILDIN_DEF(instance_enter,"s????"),
  21507. BUILDIN_DEF(instance_npcname,"s?"),
  21508. BUILDIN_DEF(instance_mapname,"s?"),
  21509. BUILDIN_DEF(instance_warpall,"sii?"),
  21510. BUILDIN_DEF(instance_announce,"isi?????"),
  21511. BUILDIN_DEF(instance_check_party,"i???"),
  21512. BUILDIN_DEF(instance_check_guild,"i???"),
  21513. BUILDIN_DEF(instance_check_clan,"i???"),
  21514. BUILDIN_DEF(instance_info,"si?"),
  21515. /**
  21516. * 3rd-related
  21517. **/
  21518. BUILDIN_DEF(makerune,"i?"),
  21519. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  21520. BUILDIN_DEF(setdragon,"??"),//[Ind]
  21521. BUILDIN_DEF(ismounting,"?"),//[Ind]
  21522. BUILDIN_DEF(setmounting,"?"),//[Ind]
  21523. BUILDIN_DEF(checkre,"i"),
  21524. /**
  21525. * rAthena and beyond!
  21526. **/
  21527. BUILDIN_DEF(getargcount,""),
  21528. BUILDIN_DEF(getcharip,"?"),
  21529. BUILDIN_DEF(is_function,"s"),
  21530. BUILDIN_DEF(get_revision,""),
  21531. BUILDIN_DEF(get_githash,""),
  21532. BUILDIN_DEF(freeloop,"?"),
  21533. BUILDIN_DEF(getrandgroupitem,"i????"),
  21534. BUILDIN_DEF(cleanmap,"s"),
  21535. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  21536. BUILDIN_DEF(npcskill,"viii"),
  21537. BUILDIN_DEF(consumeitem,"v?"),
  21538. BUILDIN_DEF(delequip,"i?"),
  21539. BUILDIN_DEF(breakequip,"i?"),
  21540. BUILDIN_DEF(sit,"?"),
  21541. BUILDIN_DEF(stand,"?"),
  21542. //@commands (script based)
  21543. BUILDIN_DEF(bindatcmd, "ss??"),
  21544. BUILDIN_DEF(unbindatcmd, "s"),
  21545. BUILDIN_DEF(useatcmd, "s"),
  21546. //Quest Log System [Inkfish]
  21547. BUILDIN_DEF(questinfo, "ii??"),
  21548. BUILDIN_DEF(setquest, "i?"),
  21549. BUILDIN_DEF(erasequest, "i?"),
  21550. BUILDIN_DEF(completequest, "i?"),
  21551. BUILDIN_DEF(checkquest, "i??"),
  21552. BUILDIN_DEF(isbegin_quest,"i?"),
  21553. BUILDIN_DEF(changequest, "ii?"),
  21554. BUILDIN_DEF(showevent, "i??"),
  21555. //Bound items [Xantara] & [Akinari]
  21556. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  21557. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  21558. BUILDIN_DEF(countbound, "??"),
  21559. // Party related
  21560. BUILDIN_DEF(party_create,"s???"),
  21561. BUILDIN_DEF(party_addmember,"ii"),
  21562. BUILDIN_DEF(party_delmember,"??"),
  21563. BUILDIN_DEF(party_changeleader,"ii"),
  21564. BUILDIN_DEF(party_changeoption,"iii"),
  21565. BUILDIN_DEF(party_destroy,"i"),
  21566. // Clan system
  21567. BUILDIN_DEF(clan_join,"i?"),
  21568. BUILDIN_DEF(clan_leave,"?"),
  21569. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  21570. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  21571. BUILDIN_DEF(vip_status,"i?"),
  21572. BUILDIN_DEF(vip_time,"i?"),
  21573. BUILDIN_DEF(bonus_script,"si????"),
  21574. BUILDIN_DEF(bonus_script_clear,"??"),
  21575. BUILDIN_DEF(getgroupitem,"i??"),
  21576. BUILDIN_DEF(enable_command,""),
  21577. BUILDIN_DEF(disable_command,""),
  21578. BUILDIN_DEF(getguildmember,"i??"),
  21579. BUILDIN_DEF(addspiritball,"ii?"),
  21580. BUILDIN_DEF(delspiritball,"i?"),
  21581. BUILDIN_DEF(countspiritball,"?"),
  21582. BUILDIN_DEF(mergeitem,"?"),
  21583. BUILDIN_DEF(mergeitem2,"??"),
  21584. BUILDIN_DEF(npcshopupdate,"sii?"),
  21585. BUILDIN_DEF(getattachedrid,""),
  21586. BUILDIN_DEF(getvar,"vi"),
  21587. BUILDIN_DEF(showscript,"s??"),
  21588. BUILDIN_DEF(ignoretimeout,"i?"),
  21589. BUILDIN_DEF(geteleminfo,"i?"),
  21590. BUILDIN_DEF(setquestinfo_level,"iii"),
  21591. BUILDIN_DEF(setquestinfo_req,"iii*"),
  21592. BUILDIN_DEF(setquestinfo_job,"ii*"),
  21593. BUILDIN_DEF(opendressroom,"i?"),
  21594. BUILDIN_DEF(navigateto,"s???????"),
  21595. BUILDIN_DEF(getguildalliance,"ii"),
  21596. BUILDIN_DEF(adopt,"vv"),
  21597. BUILDIN_DEF(getexp2,"ii?"),
  21598. BUILDIN_DEF(recalculatestat,""),
  21599. BUILDIN_DEF(hateffect,"ii"),
  21600. BUILDIN_DEF(getrandomoptinfo, "i"),
  21601. BUILDIN_DEF(getequiprandomoption, "iii?"),
  21602. BUILDIN_DEF(setrandomoption,"iiiii?"),
  21603. BUILDIN_DEF(needed_status_point,"ii?"),
  21604. BUILDIN_DEF(jobcanentermap,"s?"),
  21605. BUILDIN_DEF(openstorage2,"ii?"),
  21606. BUILDIN_DEF(unloadnpc, "s"),
  21607. // WoE TE
  21608. BUILDIN_DEF(agitstart3,""),
  21609. BUILDIN_DEF(agitend3,""),
  21610. BUILDIN_DEF(agitcheck3,""),
  21611. BUILDIN_DEF(gvgon3,"s"),
  21612. BUILDIN_DEF(gvgoff3,"s"),
  21613. // Channel System
  21614. BUILDIN_DEF(channel_create,"ss?????"),
  21615. BUILDIN_DEF(channel_setopt,"sii"),
  21616. BUILDIN_DEF(channel_setcolor,"si"),
  21617. BUILDIN_DEF(channel_setpass,"ss"),
  21618. BUILDIN_DEF(channel_setgroup,"si*"),
  21619. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  21620. BUILDIN_DEF(channel_chat,"ss?"),
  21621. BUILDIN_DEF(channel_ban,"si"),
  21622. BUILDIN_DEF(channel_unban,"si"),
  21623. BUILDIN_DEF(channel_kick,"sv"),
  21624. BUILDIN_DEF(channel_delete,"s"),
  21625. // Item Random Option Extension [Cydh]
  21626. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  21627. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  21628. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  21629. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr"),
  21630. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  21631. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  21632. // Achievement System
  21633. BUILDIN_DEF(achievementinfo,"ii?"),
  21634. BUILDIN_DEF(achievementadd,"i?"),
  21635. BUILDIN_DEF(achievementremove,"i?"),
  21636. BUILDIN_DEF(achievementcomplete,"i?"),
  21637. BUILDIN_DEF(achievementexists,"i?"),
  21638. BUILDIN_DEF(achievementupdate,"iii?"),
  21639. BUILDIN_DEF(getequiprefinecost,"iii?"),
  21640. BUILDIN_DEF2(round, "round", "i"),
  21641. BUILDIN_DEF2(round, "ceil", "i"),
  21642. BUILDIN_DEF2(round, "floor", "i"),
  21643. BUILDIN_DEF(getequiptradability, "i?"),
  21644. BUILDIN_DEF(mail, "isss*"),
  21645. #include "../custom/script_def.inc"
  21646. {NULL,NULL,NULL},
  21647. };