script.c 362 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892
  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 "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/timer.h"
  16. #include "../common/utils.h"
  17. #include "map.h"
  18. #include "path.h"
  19. #include "clif.h"
  20. #include "chrif.h"
  21. #include "itemdb.h"
  22. #include "pc.h"
  23. #include "status.h"
  24. #include "storage.h"
  25. #include "mob.h"
  26. #include "npc.h"
  27. #include "pet.h"
  28. #include "mapreg.h"
  29. #include "homunculus.h"
  30. #include "instance.h"
  31. #include "mercenary.h"
  32. #include "intif.h"
  33. #include "skill.h"
  34. #include "status.h"
  35. #include "chat.h"
  36. #include "battle.h"
  37. #include "battleground.h"
  38. #include "party.h"
  39. #include "guild.h"
  40. #include "atcommand.h"
  41. #include "log.h"
  42. #include "unit.h"
  43. #include "pet.h"
  44. #include "mail.h"
  45. #include "script.h"
  46. #include "quest.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #include <sys/time.h>
  53. #endif
  54. #include <time.h>
  55. #include <setjmp.h>
  56. #include <errno.h>
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //## TODO possible enhancements: [FlavioJS]
  59. // - 'callfunc' supporting labels in the current npc "::LabelName"
  60. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  61. // - 'function FuncName;' function declarations reverting to global functions
  62. // if local label isn't found
  63. // - join callfunc and callsub's functionality
  64. // - use script_op2name in the DEBUG_DISASM block
  65. // - remove dynamic allocation in add_word()
  66. // - remove GETVALUE / SETVALUE
  67. // - clean up the set_reg / set_val / setd_sub mess
  68. //
  69. // struct script_state* st;
  70. //
  71. /// Returns the script_data at the target index
  72. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  73. /// Returns if the stack contains data at the target index
  74. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  75. /// Returns the index of the last data in the stack
  76. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  77. /// Pushes an int into the stack
  78. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  79. /// Pushes a string into the stack (script engine frees it automatically)
  80. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  81. /// Pushes a copy of a string into the stack
  82. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  83. /// Pushes a constant string into the stack (must never change or be freed)
  84. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  85. /// Pushes a nil into the stack
  86. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  87. /// Pushes a copy of the data in the target index
  88. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  89. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  90. #define script_isint(st,i) data_isint(script_getdata(st,i))
  91. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  92. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  93. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  94. // Note: "top" functions/defines use indexes relative to the top of the stack
  95. // -1 is the index of the data at the top
  96. /// Returns the script_data at the target index relative to the top of the stack
  97. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  98. /// Pushes a copy of the data in the target index relative to the top of the stack
  99. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  100. /// Removes the range of values [start,end[ relative to the top of the stack
  101. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  102. //
  103. // struct script_data* data;
  104. //
  105. /// Returns if the script data is a string
  106. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  107. /// Returns if the script data is an int
  108. #define data_isint(data) ( (data)->type == C_INT )
  109. /// Returns if the script data is a reference
  110. #define data_isreference(data) ( (data)->type == C_NAME )
  111. /// Returns if the script data is a label
  112. #define data_islabel(data) ( (data)->type == C_POS )
  113. /// Returns if the script data is an internal script function label
  114. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  115. /// Returns if this is a reference to a constant
  116. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  117. /// Returns if this a reference to a param
  118. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  119. /// Returns if this a reference to a variable
  120. //##TODO confirm it's C_NAME [FlavioJS]
  121. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  122. /// Returns the unique id of the reference (id and index)
  123. #define reference_getuid(data) ( (data)->u.num )
  124. /// Returns the id of the reference
  125. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  126. /// Returns the array index of the reference
  127. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  128. /// Returns the name of the reference
  129. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  130. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  131. #define reference_getref(data) ( (data)->ref )
  132. /// Returns the value of the constant
  133. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  134. /// Returns the type of param
  135. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  136. /// Composes the uid of a reference from the id and the index
  137. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  138. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  139. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  140. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  141. #define FETCH(n, t) \
  142. if( script_hasdata(st,n) ) \
  143. (t)=script_getnum(st,n);
  144. #define SCRIPT_BLOCK_SIZE 512
  145. enum { LABEL_NEXTLINE=1,LABEL_START };
  146. /// temporary buffer for passing around compiled bytecode
  147. /// @see add_scriptb, set_label, parse_script
  148. static unsigned char* script_buf = NULL;
  149. static int script_pos = 0, script_size = 0;
  150. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  151. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  152. // String buffer structures.
  153. // str_data stores string information
  154. static struct str_data_struct {
  155. enum c_op type;
  156. int str;
  157. int backpatch;
  158. int label;
  159. int (*func)(struct script_state *st);
  160. int val;
  161. int next;
  162. } *str_data = NULL;
  163. static int str_data_size = 0; // size of the data
  164. static int str_num = LABEL_START; // next id to be assigned
  165. // str_buf holds the strings themselves
  166. static char *str_buf;
  167. static int str_size = 0; // size of the buffer
  168. static int str_pos = 0; // next position to be assigned
  169. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  170. #define SCRIPT_HASH_SIZE 1021
  171. int str_hash[SCRIPT_HASH_SIZE];
  172. // Specifies which string hashing method to use
  173. //#define SCRIPT_HASH_DJB2
  174. //#define SCRIPT_HASH_SDBM
  175. #define SCRIPT_HASH_ELF
  176. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  177. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  178. static int parse_options=0;
  179. DBMap* script_get_label_db(){ return scriptlabel_db; }
  180. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  181. // Caches compiled autoscript item code.
  182. // Note: This is not cleared when reloading itemdb.
  183. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  184. struct Script_Config script_config = {
  185. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  186. 0, INT_MAX, // input_min_value/input_max_value
  187. "OnPCDieEvent", //die_event_name
  188. "OnPCKillEvent", //kill_pc_event_name
  189. "OnNPCKillEvent", //kill_mob_event_name
  190. "OnPCLoginEvent", //login_event_name
  191. "OnPCLogoutEvent", //logout_event_name
  192. "OnPCLoadMapEvent", //loadmap_event_name
  193. "OnPCBaseLvUpEvent", //baselvup_event_name
  194. "OnPCJobLvUpEvent", //joblvup_event_name
  195. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  196. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  197. };
  198. static jmp_buf error_jump;
  199. static char* error_msg;
  200. static const char* error_pos;
  201. static int error_report; // if the error should produce output
  202. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  203. // [Eoe / jA 1080, 1081, 1094, 1164]
  204. enum curly_type {
  205. TYPE_NULL = 0,
  206. TYPE_IF,
  207. TYPE_SWITCH,
  208. TYPE_WHILE,
  209. TYPE_FOR,
  210. TYPE_DO,
  211. TYPE_USERFUNC,
  212. TYPE_ARGLIST // function argument list
  213. };
  214. #define ARGLIST_UNDEFINED 0
  215. #define ARGLIST_NO_PAREN 1
  216. #define ARGLIST_PAREN 2
  217. static struct {
  218. struct {
  219. enum curly_type type;
  220. int index;
  221. int count;
  222. int flag;
  223. struct linkdb_node *case_label;
  224. } curly[256]; // 右カッコの情報
  225. int curly_count; // 右カッコの数
  226. int index; // スクリプト内で使用した構文の数
  227. } syntax;
  228. const char* parse_curly_close(const char* p);
  229. const char* parse_syntax_close(const char* p);
  230. const char* parse_syntax_close_sub(const char* p,int* flag);
  231. const char* parse_syntax(const char* p);
  232. static int parse_syntax_for_flag = 0;
  233. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  234. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  235. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  236. int potion_target=0;
  237. c_op get_com(unsigned char *script,int *pos);
  238. int get_num(unsigned char *script,int *pos);
  239. typedef struct script_function {
  240. int (*func)(struct script_state *st);
  241. const char *name;
  242. const char *arg;
  243. } script_function;
  244. extern script_function buildin_func[];
  245. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  246. uint32 crctab[256];
  247. /*==========================================
  248. * ローカルプロトタイプ宣言 (必要な物のみ)
  249. *------------------------------------------*/
  250. const char* parse_subexpr(const char* p,int limit);
  251. void push_val(struct script_stack *stack,int type,int val);
  252. int run_func(struct script_state *st);
  253. enum {
  254. MF_NOMEMO, //0
  255. MF_NOTELEPORT,
  256. MF_NOSAVE,
  257. MF_NOBRANCH,
  258. MF_NOPENALTY,
  259. MF_NOZENYPENALTY,
  260. MF_PVP,
  261. MF_PVP_NOPARTY,
  262. MF_PVP_NOGUILD,
  263. MF_GVG,
  264. MF_GVG_NOPARTY, //10
  265. MF_NOTRADE,
  266. MF_NOSKILL,
  267. MF_NOWARP,
  268. MF_PARTYLOCK,
  269. MF_NOICEWALL,
  270. MF_SNOW,
  271. MF_FOG,
  272. MF_SAKURA,
  273. MF_LEAVES,
  274. MF_RAIN, //20
  275. // 21 free
  276. MF_NOGO = 22,
  277. MF_CLOUDS,
  278. MF_CLOUDS2,
  279. MF_FIREWORKS,
  280. MF_GVG_CASTLE,
  281. MF_GVG_DUNGEON,
  282. MF_NIGHTENABLED,
  283. MF_NOBASEEXP,
  284. MF_NOJOBEXP, //30
  285. MF_NOMOBLOOT,
  286. MF_NOMVPLOOT,
  287. MF_NORETURN,
  288. MF_NOWARPTO,
  289. MF_NIGHTMAREDROP,
  290. MF_RESTRICTED,
  291. MF_NOCOMMAND,
  292. MF_NODROP,
  293. MF_JEXP,
  294. MF_BEXP, //40
  295. MF_NOVENDING,
  296. MF_LOADEVENT,
  297. MF_NOCHAT,
  298. MF_NOEXPPENALTY,
  299. MF_GUILDLOCK,
  300. MF_TOWN,
  301. MF_AUTOTRADE,
  302. MF_ALLOWKS,
  303. MF_MONSTER_NOTELEPORT,
  304. MF_PVP_NOCALCRANK, //50
  305. MF_BATTLEGROUND
  306. };
  307. const char* script_op2name(int op)
  308. {
  309. #define RETURN_OP_NAME(type) case type: return #type
  310. switch( op )
  311. {
  312. RETURN_OP_NAME(C_NOP);
  313. RETURN_OP_NAME(C_POS);
  314. RETURN_OP_NAME(C_INT);
  315. RETURN_OP_NAME(C_PARAM);
  316. RETURN_OP_NAME(C_FUNC);
  317. RETURN_OP_NAME(C_STR);
  318. RETURN_OP_NAME(C_CONSTSTR);
  319. RETURN_OP_NAME(C_ARG);
  320. RETURN_OP_NAME(C_NAME);
  321. RETURN_OP_NAME(C_EOL);
  322. RETURN_OP_NAME(C_RETINFO);
  323. RETURN_OP_NAME(C_USERFUNC);
  324. RETURN_OP_NAME(C_USERFUNC_POS);
  325. // operators
  326. RETURN_OP_NAME(C_OP3);
  327. RETURN_OP_NAME(C_LOR);
  328. RETURN_OP_NAME(C_LAND);
  329. RETURN_OP_NAME(C_LE);
  330. RETURN_OP_NAME(C_LT);
  331. RETURN_OP_NAME(C_GE);
  332. RETURN_OP_NAME(C_GT);
  333. RETURN_OP_NAME(C_EQ);
  334. RETURN_OP_NAME(C_NE);
  335. RETURN_OP_NAME(C_XOR);
  336. RETURN_OP_NAME(C_OR);
  337. RETURN_OP_NAME(C_AND);
  338. RETURN_OP_NAME(C_ADD);
  339. RETURN_OP_NAME(C_SUB);
  340. RETURN_OP_NAME(C_MUL);
  341. RETURN_OP_NAME(C_DIV);
  342. RETURN_OP_NAME(C_MOD);
  343. RETURN_OP_NAME(C_NEG);
  344. RETURN_OP_NAME(C_LNOT);
  345. RETURN_OP_NAME(C_NOT);
  346. RETURN_OP_NAME(C_R_SHIFT);
  347. RETURN_OP_NAME(C_L_SHIFT);
  348. default:
  349. ShowDebug("script_op2name: unexpected op=%d\n", op);
  350. return "???";
  351. }
  352. #undef RETURN_OP_NAME
  353. }
  354. #ifdef DEBUG_DUMP_STACK
  355. static void script_dump_stack(struct script_state* st)
  356. {
  357. int i;
  358. ShowMessage("\tstart = %d\n", st->start);
  359. ShowMessage("\tend = %d\n", st->end);
  360. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  361. ShowMessage("\tsp = %d\n", st->stack->sp);
  362. for( i = 0; i < st->stack->sp; ++i )
  363. {
  364. struct script_data* data = &st->stack->stack_data[i];
  365. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  366. switch( data->type )
  367. {
  368. case C_INT:
  369. case C_POS:
  370. ShowMessage(" %d\n", data->u.num);
  371. break;
  372. case C_STR:
  373. case C_CONSTSTR:
  374. ShowMessage(" \"%s\"\n", data->u.str);
  375. break;
  376. case C_NAME:
  377. 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));
  378. break;
  379. case C_RETINFO:
  380. {
  381. struct script_retinfo* ri = data->u.ri;
  382. ShowMessage(" %p {var_function=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->var_function, ri->script, ri->pos, ri->nargs, ri->defsp);
  383. }
  384. break;
  385. default:
  386. ShowMessage("\n");
  387. break;
  388. }
  389. }
  390. }
  391. #endif
  392. /// Reports on the console the src of a script error.
  393. static void script_reportsrc(struct script_state *st)
  394. {
  395. struct block_list* bl;
  396. if( st->oid == 0 )
  397. return; //Can't report source.
  398. bl = map_id2bl(st->oid);
  399. if( bl == NULL )
  400. return;
  401. switch( bl->type )
  402. {
  403. case BL_NPC:
  404. if( bl->m >= 0 )
  405. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  406. else
  407. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  408. break;
  409. default:
  410. if( bl->m >= 0 )
  411. 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);
  412. else
  413. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  414. break;
  415. }
  416. }
  417. /// Reports on the console information about the script data.
  418. static void script_reportdata(struct script_data* data)
  419. {
  420. if( data == NULL )
  421. return;
  422. switch( data->type )
  423. {
  424. case C_NOP:// no value
  425. ShowDebug("Data: nothing (nil)\n");
  426. break;
  427. case C_INT:// number
  428. ShowDebug("Data: number value=%d\n", data->u.num);
  429. break;
  430. case C_STR:
  431. case C_CONSTSTR:// string
  432. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  433. break;
  434. case C_NAME:// reference
  435. if( reference_tovariable(data) )
  436. {// variable
  437. const char* name = reference_getname(data);
  438. if( not_array_variable(*name) )
  439. ShowDebug("Data: variable name='%s'\n", name);
  440. else
  441. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  442. }
  443. else if( reference_toconstant(data) )
  444. {// constant
  445. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  446. }
  447. else if( reference_toparam(data) )
  448. {// param
  449. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  450. }
  451. else
  452. {// ???
  453. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  454. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  455. }
  456. break;
  457. case C_POS:// label
  458. ShowDebug("Data: label pos=%d\n", data->u.num);
  459. break;
  460. default:
  461. ShowDebug("Data: %s\n", script_op2name(data->type));
  462. break;
  463. }
  464. }
  465. /*==========================================
  466. * エラーメッセージ出力
  467. *------------------------------------------*/
  468. static void disp_error_message2(const char *mes,const char *pos,int report)
  469. {
  470. error_msg = aStrdup(mes);
  471. error_pos = pos;
  472. error_report = report;
  473. longjmp( error_jump, 1 );
  474. }
  475. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  476. /// Checks event parameter validity
  477. static void check_event(struct script_state *st, const char *evt)
  478. {
  479. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  480. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  481. script_reportsrc(st);
  482. }
  483. }
  484. /*==========================================
  485. * Hashes the input string
  486. *------------------------------------------*/
  487. static unsigned int calc_hash(const char* p)
  488. {
  489. unsigned int h;
  490. #if defined(SCRIPT_HASH_DJB2)
  491. h = 5381;
  492. while( *p ) // hash*33 + c
  493. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  494. #elif defined(SCRIPT_HASH_SDBM)
  495. h = 0;
  496. while( *p ) // hash*65599 + c
  497. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  498. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  499. h = 0;
  500. while( *p ){
  501. unsigned int g;
  502. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  503. g = h & 0xF0000000;
  504. if( g )
  505. {
  506. h ^= g >> 24;
  507. h &= ~g;
  508. }
  509. }
  510. #else // athena hash
  511. h = 0;
  512. while( *p )
  513. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  514. #endif
  515. return h % SCRIPT_HASH_SIZE;
  516. }
  517. /*==========================================
  518. * str_data manipulation functions
  519. *------------------------------------------*/
  520. /// Looks up string using the provided id.
  521. const char* get_str(int id)
  522. {
  523. Assert( id >= LABEL_START && id < str_size );
  524. return str_buf+str_data[id].str;
  525. }
  526. /// Returns the uid of the string, or -1.
  527. static int search_str(const char* p)
  528. {
  529. int i;
  530. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  531. if( strcasecmp(get_str(i),p) == 0 )
  532. return i;
  533. return -1;
  534. }
  535. /// Stores a copy of the string and returns its id.
  536. /// If an identical string is already present, returns its id instead.
  537. int add_str(const char* p)
  538. {
  539. int i, h;
  540. int len;
  541. h = calc_hash(p);
  542. if( str_hash[h] == 0 )
  543. {// empty bucket, add new node here
  544. str_hash[h] = str_num;
  545. }
  546. else
  547. {// scan for end of list, or occurence of identical string
  548. for( i = str_hash[h]; ; i = str_data[i].next )
  549. {
  550. if( strcasecmp(get_str(i),p) == 0 )
  551. return i; // string already in list
  552. if( str_data[i].next == 0 )
  553. break; // reached the end
  554. }
  555. // append node to end of list
  556. str_data[i].next = str_num;
  557. }
  558. // grow list if neccessary
  559. if( str_num >= str_data_size )
  560. {
  561. str_data_size += 128;
  562. RECREATE(str_data,struct str_data_struct,str_data_size);
  563. memset(str_data + (str_data_size - 128), '\0', 128);
  564. }
  565. len=(int)strlen(p);
  566. // grow string buffer if neccessary
  567. while( str_pos+len+1 >= str_size )
  568. {
  569. str_size += 256;
  570. RECREATE(str_buf,char,str_size);
  571. memset(str_buf + (str_size - 256), '\0', 256);
  572. }
  573. safestrncpy(str_buf+str_pos, p, len+1);
  574. str_data[str_num].type = C_NOP;
  575. str_data[str_num].str = str_pos;
  576. str_data[str_num].next = 0;
  577. str_data[str_num].func = NULL;
  578. str_data[str_num].backpatch = -1;
  579. str_data[str_num].label = -1;
  580. str_pos += len+1;
  581. return str_num++;
  582. }
  583. /// Appends 1 byte to the script buffer.
  584. static void add_scriptb(int a)
  585. {
  586. if( script_pos+1 >= script_size )
  587. {
  588. script_size += SCRIPT_BLOCK_SIZE;
  589. RECREATE(script_buf,unsigned char,script_size);
  590. }
  591. script_buf[script_pos++] = (uint8)(a);
  592. }
  593. /// Appends a c_op value to the script buffer.
  594. /// The value is variable-length encoded into 8-bit blocks.
  595. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  596. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  597. static void add_scriptc(int a)
  598. {
  599. while( a >= 0x40 )
  600. {
  601. add_scriptb((a&0x3f)|0x40);
  602. a = (a - 0x40) >> 6;
  603. }
  604. add_scriptb(a);
  605. }
  606. /// Appends an integer value to the script buffer.
  607. /// The value is variable-length encoded into 8-bit blocks.
  608. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  609. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  610. static void add_scripti(int a)
  611. {
  612. while( a >= 0x40 )
  613. {
  614. add_scriptb((a&0x3f)|0xc0);
  615. a = (a - 0x40) >> 6;
  616. }
  617. add_scriptb(a|0x80);
  618. }
  619. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  620. ///
  621. /// @param l The id of the str_data entry
  622. // 最大16Mまで
  623. static void add_scriptl(int l)
  624. {
  625. int backpatch = str_data[l].backpatch;
  626. switch(str_data[l].type){
  627. case C_POS:
  628. case C_USERFUNC_POS:
  629. add_scriptc(C_POS);
  630. add_scriptb(str_data[l].label);
  631. add_scriptb(str_data[l].label>>8);
  632. add_scriptb(str_data[l].label>>16);
  633. break;
  634. case C_NOP:
  635. case C_USERFUNC:
  636. // ラベルの可能性があるのでbackpatch用データ埋め込み
  637. add_scriptc(C_NAME);
  638. str_data[l].backpatch = script_pos;
  639. add_scriptb(backpatch);
  640. add_scriptb(backpatch>>8);
  641. add_scriptb(backpatch>>16);
  642. break;
  643. case C_INT:
  644. add_scripti(abs(str_data[l].val));
  645. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  646. add_scriptc(C_NEG);
  647. break;
  648. default: // assume C_NAME
  649. add_scriptc(C_NAME);
  650. add_scriptb(l);
  651. add_scriptb(l>>8);
  652. add_scriptb(l>>16);
  653. break;
  654. }
  655. }
  656. /*==========================================
  657. * ラベルを解決する
  658. *------------------------------------------*/
  659. void set_label(int l,int pos, const char* script_pos)
  660. {
  661. int i,next;
  662. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  663. { //Prevent overwriting constants values and parameters [Skotlex]
  664. disp_error_message("set_label: invalid label name",script_pos);
  665. return;
  666. }
  667. if(str_data[l].label!=-1){
  668. disp_error_message("set_label: dup label ",script_pos);
  669. return;
  670. }
  671. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  672. str_data[l].label=pos;
  673. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  674. next=GETVALUE(script_buf,i);
  675. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  676. SETVALUE(script_buf,i,pos);
  677. i=next;
  678. }
  679. }
  680. /// Skips spaces and/or comments.
  681. const char* skip_space(const char* p)
  682. {
  683. if( p == NULL )
  684. return NULL;
  685. for(;;)
  686. {
  687. while( ISSPACE(*p) )
  688. ++p;
  689. if( *p == '/' && p[1] == '/' )
  690. {// line comment
  691. while(*p && *p!='\n')
  692. ++p;
  693. }
  694. else if( *p == '/' && p[1] == '*' )
  695. {// block comment
  696. p += 2;
  697. for(;;)
  698. {
  699. if( *p == '\0' )
  700. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  701. if( *p == '*' && p[1] == '/' )
  702. {// end of block comment
  703. p += 2;
  704. break;
  705. }
  706. ++p;
  707. }
  708. }
  709. else
  710. break;
  711. }
  712. return p;
  713. }
  714. /// Skips a word.
  715. /// A word consists of undercores and/or alfanumeric characters,
  716. /// and valid variable prefixes/postfixes.
  717. static
  718. const char* skip_word(const char* p)
  719. {
  720. // prefix
  721. switch( *p )
  722. {
  723. case '@':// temporary char variable
  724. ++p; break;
  725. case '#':// account variable
  726. p += ( p[1] == '#' ? 2 : 1 ); break;
  727. case '\'':// instance variable
  728. ++p; break;
  729. case '.':// npc variable
  730. p += ( p[1] == '@' ? 2 : 1 ); break;
  731. case '$':// global variable
  732. p += ( p[1] == '@' ? 2 : 1 ); break;
  733. }
  734. while( ISALNUM(*p) || *p == '_' )
  735. ++p;
  736. // postfix
  737. if( *p == '$' )// string
  738. p++;
  739. return p;
  740. }
  741. /// Adds a word to str_data.
  742. /// @see skip_word
  743. /// @see add_str
  744. static
  745. int add_word(const char* p)
  746. {
  747. char* word;
  748. int len;
  749. int i;
  750. // Check for a word
  751. len = skip_word(p) - p;
  752. if( len == 0 )
  753. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  754. // Duplicate the word
  755. CREATE(word, char, len+1);
  756. memcpy(word, p, len);
  757. word[len] = 0;
  758. // add the word
  759. i = add_str(word);
  760. aFree(word);
  761. return i;
  762. }
  763. /// Parses a function call.
  764. /// The argument list can have parenthesis or not.
  765. /// The number of arguments is checked.
  766. static
  767. const char* parse_callfunc(const char* p, int require_paren)
  768. {
  769. const char* p2;
  770. const char* arg=NULL;
  771. int func;
  772. func = add_word(p);
  773. if( str_data[func].type == C_FUNC ){
  774. // buildin function
  775. add_scriptl(func);
  776. add_scriptc(C_ARG);
  777. arg = buildin_func[str_data[func].val].arg;
  778. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  779. // script defined function
  780. int callsub = search_str("callsub");
  781. add_scriptl(callsub);
  782. add_scriptc(C_ARG);
  783. add_scriptl(func);
  784. arg = buildin_func[str_data[callsub].val].arg;
  785. if( *arg == 0 )
  786. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  787. if( *arg != '*' )
  788. ++arg; // count func as argument
  789. } else
  790. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  791. p = skip_word(p);
  792. p = skip_space(p);
  793. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  794. syntax.curly[syntax.curly_count].count = 0;
  795. if( *p == ';' )
  796. {// <func name> ';'
  797. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  798. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  799. {// <func name> '(' ')'
  800. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  801. p = p2;
  802. /*
  803. } else if( 0 && require_paren && *p != '(' )
  804. {// <func name>
  805. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  806. */
  807. } else
  808. {// <func name> <arg list>
  809. if( require_paren ){
  810. if( *p != '(' )
  811. disp_error_message("need '('",p);
  812. ++p; // skip '('
  813. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  814. } else if( *p == '(' ){
  815. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  816. } else {
  817. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  818. }
  819. ++syntax.curly_count;
  820. while( *arg ) {
  821. p2=parse_subexpr(p,-1);
  822. if( p == p2 )
  823. break; // not an argument
  824. if( *arg != '*' )
  825. ++arg; // next argument
  826. p=skip_space(p2);
  827. if( *arg == 0 || *p != ',' )
  828. break; // no more arguments
  829. ++p; // skip comma
  830. }
  831. --syntax.curly_count;
  832. }
  833. if( *arg && *arg != '?' && *arg != '*' )
  834. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  835. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  836. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  837. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  838. if( *p != ')' )
  839. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  840. ++p;
  841. }
  842. add_scriptc(C_FUNC);
  843. return p;
  844. }
  845. /*==========================================
  846. * 項の解析
  847. *------------------------------------------*/
  848. const char* parse_simpleexpr(const char *p)
  849. {
  850. int i;
  851. p=skip_space(p);
  852. if(*p==';' || *p==',')
  853. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  854. if(*p=='('){
  855. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  856. ++syntax.curly[i].count;
  857. p=parse_subexpr(p+1,-1);
  858. p=skip_space(p);
  859. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  860. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  861. ){
  862. if( *p == ',' ){
  863. syntax.curly[i].flag = ARGLIST_PAREN;
  864. return p;
  865. } else
  866. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  867. }
  868. if( *p != ')' )
  869. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  870. ++p;
  871. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  872. char *np;
  873. i=strtoul(p,&np,0);
  874. add_scripti(i);
  875. p=np;
  876. } else if(*p=='"'){
  877. add_scriptc(C_STR);
  878. p++;
  879. while( *p && *p != '"' ){
  880. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  881. {
  882. char buf[8];
  883. size_t len = skip_escaped_c(p) - p;
  884. size_t n = sv_unescape_c(buf, p, len);
  885. if( n != 1 )
  886. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  887. p += len;
  888. add_scriptb(*buf);
  889. continue;
  890. }
  891. else if( *p == '\n' )
  892. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  893. add_scriptb(*p++);
  894. }
  895. if(!*p)
  896. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  897. add_scriptb(0);
  898. p++; //'"'
  899. } else {
  900. int l;
  901. // label , register , function etc
  902. if(skip_word(p)==p)
  903. disp_error_message("parse_simpleexpr: unexpected character",p);
  904. l=add_word(p);
  905. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  906. return parse_callfunc(p,1);
  907. p=skip_word(p);
  908. if( *p == '[' ){
  909. // array(name[i] => getelementofarray(name,i) )
  910. add_scriptl(search_str("getelementofarray"));
  911. add_scriptc(C_ARG);
  912. add_scriptl(l);
  913. p=parse_subexpr(p+1,-1);
  914. p=skip_space(p);
  915. if( *p != ']' )
  916. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  917. ++p;
  918. add_scriptc(C_FUNC);
  919. }else
  920. add_scriptl(l);
  921. }
  922. return p;
  923. }
  924. /*==========================================
  925. * 式の解析
  926. *------------------------------------------*/
  927. const char* parse_subexpr(const char* p,int limit)
  928. {
  929. int op,opl,len;
  930. const char* tmpp;
  931. p=skip_space(p);
  932. if(*p=='-'){
  933. tmpp=skip_space(p+1);
  934. if(*tmpp==';' || *tmpp==','){
  935. add_scriptl(LABEL_NEXTLINE);
  936. p++;
  937. return p;
  938. }
  939. }
  940. tmpp=p;
  941. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  942. p=parse_subexpr(p+1,10);
  943. add_scriptc(op);
  944. } else
  945. p=parse_simpleexpr(p);
  946. p=skip_space(p);
  947. while((
  948. (op=C_OP3,opl=0,len=1,*p=='?') ||
  949. (op=C_ADD,opl=8,len=1,*p=='+') ||
  950. (op=C_SUB,opl=8,len=1,*p=='-') ||
  951. (op=C_MUL,opl=9,len=1,*p=='*') ||
  952. (op=C_DIV,opl=9,len=1,*p=='/') ||
  953. (op=C_MOD,opl=9,len=1,*p=='%') ||
  954. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  955. (op=C_AND,opl=6,len=1,*p=='&') ||
  956. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  957. (op=C_OR,opl=5,len=1,*p=='|') ||
  958. (op=C_XOR,opl=4,len=1,*p=='^') ||
  959. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  960. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  961. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  962. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  963. (op=C_GT,opl=3,len=1,*p=='>') ||
  964. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  965. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  966. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  967. p+=len;
  968. if(op == C_OP3) {
  969. p=parse_subexpr(p,-1);
  970. p=skip_space(p);
  971. if( *(p++) != ':')
  972. disp_error_message("parse_subexpr: need ':'", p-1);
  973. p=parse_subexpr(p,-1);
  974. } else {
  975. p=parse_subexpr(p,opl);
  976. }
  977. add_scriptc(op);
  978. p=skip_space(p);
  979. }
  980. return p; /* return first untreated operator */
  981. }
  982. /*==========================================
  983. * 式の評価
  984. *------------------------------------------*/
  985. const char* parse_expr(const char *p)
  986. {
  987. switch(*p){
  988. case ')': case ';': case ':': case '[': case ']':
  989. case '}':
  990. disp_error_message("parse_expr: unexpected char",p);
  991. }
  992. p=parse_subexpr(p,-1);
  993. return p;
  994. }
  995. /*==========================================
  996. * 行の解析
  997. *------------------------------------------*/
  998. const char* parse_line(const char* p)
  999. {
  1000. const char* p2;
  1001. p=skip_space(p);
  1002. if(*p==';') {
  1003. // if(); for(); while(); のために閉じ判定
  1004. p = parse_syntax_close(p + 1);
  1005. return p;
  1006. }
  1007. if(*p==')' && parse_syntax_for_flag)
  1008. return p+1;
  1009. p = skip_space(p);
  1010. if(p[0] == '{') {
  1011. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1012. syntax.curly[syntax.curly_count].count = -1;
  1013. syntax.curly[syntax.curly_count].index = -1;
  1014. syntax.curly_count++;
  1015. return p + 1;
  1016. } else if(p[0] == '}') {
  1017. return parse_curly_close(p);
  1018. }
  1019. // 構文関連の処理
  1020. p2 = parse_syntax(p);
  1021. if(p2 != NULL)
  1022. return p2;
  1023. p = parse_callfunc(p,0);
  1024. p = skip_space(p);
  1025. if(parse_syntax_for_flag) {
  1026. if( *p != ')' )
  1027. disp_error_message("parse_line: need ')'",p);
  1028. } else {
  1029. if( *p != ';' )
  1030. disp_error_message("parse_line: need ';'",p);
  1031. }
  1032. // if, for , while の閉じ判定
  1033. p = parse_syntax_close(p+1);
  1034. return p;
  1035. }
  1036. // { ... } の閉じ処理
  1037. const char* parse_curly_close(const char* p)
  1038. {
  1039. if(syntax.curly_count <= 0) {
  1040. disp_error_message("parse_curly_close: unexpected string",p);
  1041. return p + 1;
  1042. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1043. syntax.curly_count--;
  1044. // if, for , while の閉じ判定
  1045. p = parse_syntax_close(p + 1);
  1046. return p;
  1047. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1048. // switch() 閉じ判定
  1049. int pos = syntax.curly_count-1;
  1050. char label[256];
  1051. int l;
  1052. // 一時変数を消す
  1053. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1054. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1055. parse_line(label);
  1056. syntax.curly_count--;
  1057. // 無条件で終了ポインタに移動
  1058. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1059. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1060. parse_line(label);
  1061. syntax.curly_count--;
  1062. // 現在地のラベルを付ける
  1063. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1064. l=add_str(label);
  1065. set_label(l,script_pos, p);
  1066. if(syntax.curly[pos].flag) {
  1067. // default が存在する
  1068. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1069. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1070. parse_line(label);
  1071. syntax.curly_count--;
  1072. }
  1073. // 終了ラベルを付ける
  1074. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1075. l=add_str(label);
  1076. set_label(l,script_pos, p);
  1077. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1078. syntax.curly_count--;
  1079. return p+1;
  1080. } else {
  1081. disp_error_message("parse_curly_close: unexpected string",p);
  1082. return p + 1;
  1083. }
  1084. }
  1085. // 構文関連の処理
  1086. // break, case, continue, default, do, for, function,
  1087. // if, switch, while をこの内部で処理します。
  1088. const char* parse_syntax(const char* p)
  1089. {
  1090. const char *p2 = skip_word(p);
  1091. switch(*p) {
  1092. case 'B':
  1093. case 'b':
  1094. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1095. // break の処理
  1096. char label[256];
  1097. int pos = syntax.curly_count - 1;
  1098. while(pos >= 0) {
  1099. if(syntax.curly[pos].type == TYPE_DO) {
  1100. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1101. break;
  1102. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1103. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1104. break;
  1105. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1106. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1107. break;
  1108. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1109. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1110. break;
  1111. }
  1112. pos--;
  1113. }
  1114. if(pos < 0) {
  1115. disp_error_message("parse_syntax: unexpected 'break'",p);
  1116. } else {
  1117. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1118. parse_line(label);
  1119. syntax.curly_count--;
  1120. }
  1121. p = skip_space(p2);
  1122. if(*p != ';')
  1123. disp_error_message("parse_syntax: need ';'",p);
  1124. // if, for , while の閉じ判定
  1125. p = parse_syntax_close(p + 1);
  1126. return p;
  1127. }
  1128. break;
  1129. case 'c':
  1130. case 'C':
  1131. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1132. // case の処理
  1133. int pos = syntax.curly_count-1;
  1134. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1135. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1136. return p+1;
  1137. } else {
  1138. char label[256];
  1139. int l,v;
  1140. char *np;
  1141. if(syntax.curly[pos].count != 1) {
  1142. // FALLTHRU 用のジャンプ
  1143. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1144. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1145. parse_line(label);
  1146. syntax.curly_count--;
  1147. // 現在地のラベルを付ける
  1148. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1149. l=add_str(label);
  1150. set_label(l,script_pos, p);
  1151. }
  1152. // switch 判定文
  1153. p = skip_space(p2);
  1154. if(p == p2) {
  1155. disp_error_message("parse_syntax: expect space ' '",p);
  1156. }
  1157. // check whether case label is integer or not
  1158. v = strtol(p,&np,0);
  1159. if(np == p) { //Check for constants
  1160. p2 = skip_word(p);
  1161. v = p2-p; // length of word at p2
  1162. memcpy(label,p,v);
  1163. label[v]='\0';
  1164. v = search_str(label);
  1165. if (v < 0 || str_data[v].type != C_INT)
  1166. disp_error_message("parse_syntax: 'case' label not integer",p);
  1167. v = str_data[v].val;
  1168. p = skip_word(p);
  1169. } else { //Numeric value
  1170. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1171. p++;
  1172. p = skip_word(p);
  1173. if(np != p)
  1174. disp_error_message("parse_syntax: 'case' label not integer",np);
  1175. }
  1176. p = skip_space(p);
  1177. if(*p != ':')
  1178. disp_error_message("parse_syntax: expect ':'",p);
  1179. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1180. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1181. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1182. // 2回parse しないとダメ
  1183. p2 = parse_line(label);
  1184. parse_line(p2);
  1185. syntax.curly_count--;
  1186. if(syntax.curly[pos].count != 1) {
  1187. // FALLTHRU 終了後のラベル
  1188. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1189. l=add_str(label);
  1190. set_label(l,script_pos,p);
  1191. }
  1192. // check duplication of case label [Rayce]
  1193. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1194. disp_error_message("parse_syntax: dup 'case'",p);
  1195. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1196. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1197. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1198. parse_line(label);
  1199. syntax.curly_count--;
  1200. syntax.curly[pos].count++;
  1201. }
  1202. return p + 1;
  1203. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1204. // continue の処理
  1205. char label[256];
  1206. int pos = syntax.curly_count - 1;
  1207. while(pos >= 0) {
  1208. if(syntax.curly[pos].type == TYPE_DO) {
  1209. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1210. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1211. break;
  1212. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1213. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1214. break;
  1215. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1216. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1217. break;
  1218. }
  1219. pos--;
  1220. }
  1221. if(pos < 0) {
  1222. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1223. } else {
  1224. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1225. parse_line(label);
  1226. syntax.curly_count--;
  1227. }
  1228. p = skip_space(p2);
  1229. if(*p != ';')
  1230. disp_error_message("parse_syntax: need ';'",p);
  1231. // if, for , while の閉じ判定
  1232. p = parse_syntax_close(p + 1);
  1233. return p;
  1234. }
  1235. break;
  1236. case 'd':
  1237. case 'D':
  1238. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1239. // switch - default の処理
  1240. int pos = syntax.curly_count-1;
  1241. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1242. disp_error_message("parse_syntax: unexpected 'default'",p);
  1243. } else if(syntax.curly[pos].flag) {
  1244. disp_error_message("parse_syntax: dup 'default'",p);
  1245. } else {
  1246. char label[256];
  1247. int l;
  1248. // 現在地のラベルを付ける
  1249. p = skip_space(p2);
  1250. if(*p != ':') {
  1251. disp_error_message("parse_syntax: need ':'",p);
  1252. }
  1253. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1254. l=add_str(label);
  1255. set_label(l,script_pos,p);
  1256. // 無条件で次のリンクに飛ばす
  1257. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1258. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1259. parse_line(label);
  1260. syntax.curly_count--;
  1261. // default のラベルを付ける
  1262. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1263. l=add_str(label);
  1264. set_label(l,script_pos,p);
  1265. syntax.curly[syntax.curly_count - 1].flag = 1;
  1266. syntax.curly[pos].count++;
  1267. }
  1268. return p + 1;
  1269. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1270. int l;
  1271. char label[256];
  1272. p=skip_space(p2);
  1273. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1274. syntax.curly[syntax.curly_count].count = 1;
  1275. syntax.curly[syntax.curly_count].index = syntax.index++;
  1276. syntax.curly[syntax.curly_count].flag = 0;
  1277. // 現在地のラベル形成する
  1278. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1279. l=add_str(label);
  1280. set_label(l,script_pos,p);
  1281. syntax.curly_count++;
  1282. return p;
  1283. }
  1284. break;
  1285. case 'f':
  1286. case 'F':
  1287. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1288. int l;
  1289. char label[256];
  1290. int pos = syntax.curly_count;
  1291. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1292. syntax.curly[syntax.curly_count].count = 1;
  1293. syntax.curly[syntax.curly_count].index = syntax.index++;
  1294. syntax.curly[syntax.curly_count].flag = 0;
  1295. syntax.curly_count++;
  1296. p=skip_space(p2);
  1297. if(*p != '(')
  1298. disp_error_message("parse_syntax: need '('",p);
  1299. p++;
  1300. // 初期化文を実行する
  1301. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1302. p=parse_line(p);
  1303. syntax.curly_count--;
  1304. // 条件判断開始のラベル形成する
  1305. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1306. l=add_str(label);
  1307. set_label(l,script_pos,p);
  1308. p=skip_space(p);
  1309. if(*p == ';') {
  1310. // for(;;) のパターンなので必ず真
  1311. ;
  1312. } else {
  1313. // 条件が偽なら終了地点に飛ばす
  1314. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1315. add_scriptl(add_str("jump_zero"));
  1316. add_scriptc(C_ARG);
  1317. p=parse_expr(p);
  1318. p=skip_space(p);
  1319. add_scriptl(add_str(label));
  1320. add_scriptc(C_FUNC);
  1321. }
  1322. if(*p != ';')
  1323. disp_error_message("parse_syntax: need ';'",p);
  1324. p++;
  1325. // ループ開始に飛ばす
  1326. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1327. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1328. parse_line(label);
  1329. syntax.curly_count--;
  1330. // 次のループへのラベル形成する
  1331. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1332. l=add_str(label);
  1333. set_label(l,script_pos,p);
  1334. // 次のループに入る時の処理
  1335. // for 最後の ')' を ';' として扱うフラグ
  1336. parse_syntax_for_flag = 1;
  1337. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1338. p=parse_line(p);
  1339. syntax.curly_count--;
  1340. parse_syntax_for_flag = 0;
  1341. // 条件判定処理に飛ばす
  1342. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1343. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1344. parse_line(label);
  1345. syntax.curly_count--;
  1346. // ループ開始のラベル付け
  1347. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1348. l=add_str(label);
  1349. set_label(l,script_pos,p);
  1350. return p;
  1351. }
  1352. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1353. {// internal script function
  1354. const char *func_name;
  1355. func_name = skip_space(p2);
  1356. p = skip_word(func_name);
  1357. if( p == func_name )
  1358. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1359. p2 = skip_space(p);
  1360. if( *p2 == ';' )
  1361. {// function <name> ;
  1362. // function declaration - just register the name
  1363. int l;
  1364. l = add_word(func_name);
  1365. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1366. str_data[l].type = C_USERFUNC;
  1367. // if, for , while の閉じ判定
  1368. p = parse_syntax_close(p2 + 1);
  1369. return p; }
  1370. else if(*p2 == '{')
  1371. {// function <name> <line/block of code>
  1372. char label[256];
  1373. int l;
  1374. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1375. syntax.curly[syntax.curly_count].count = 1;
  1376. syntax.curly[syntax.curly_count].index = syntax.index++;
  1377. syntax.curly[syntax.curly_count].flag = 0;
  1378. ++syntax.curly_count;
  1379. // Jump over the function code
  1380. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1381. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1382. ++syntax.curly_count;
  1383. parse_line(label);
  1384. --syntax.curly_count;
  1385. // Set the position of the function (label)
  1386. l=add_word(func_name);
  1387. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1388. str_data[l].type = C_USERFUNC;
  1389. set_label(l, script_pos, p);
  1390. if( parse_options&SCRIPT_USE_LABEL_DB )
  1391. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1392. return skip_space(p);
  1393. }
  1394. else
  1395. {
  1396. disp_error_message("expect ';' or '{' at function syntax",p);
  1397. }
  1398. }
  1399. break;
  1400. case 'i':
  1401. case 'I':
  1402. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1403. // if() の処理
  1404. char label[256];
  1405. p=skip_space(p2);
  1406. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1407. disp_error_message("need '('",p);
  1408. }
  1409. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1410. syntax.curly[syntax.curly_count].count = 1;
  1411. syntax.curly[syntax.curly_count].index = syntax.index++;
  1412. syntax.curly[syntax.curly_count].flag = 0;
  1413. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1414. syntax.curly_count++;
  1415. add_scriptl(add_str("jump_zero"));
  1416. add_scriptc(C_ARG);
  1417. p=parse_expr(p);
  1418. p=skip_space(p);
  1419. add_scriptl(add_str(label));
  1420. add_scriptc(C_FUNC);
  1421. return p;
  1422. }
  1423. break;
  1424. case 's':
  1425. case 'S':
  1426. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1427. // switch() の処理
  1428. char label[256];
  1429. p=skip_space(p2);
  1430. if(*p != '(') {
  1431. disp_error_message("need '('",p);
  1432. }
  1433. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1434. syntax.curly[syntax.curly_count].count = 1;
  1435. syntax.curly[syntax.curly_count].index = syntax.index++;
  1436. syntax.curly[syntax.curly_count].flag = 0;
  1437. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1438. syntax.curly_count++;
  1439. add_scriptl(add_str("set"));
  1440. add_scriptc(C_ARG);
  1441. add_scriptl(add_str(label));
  1442. p=parse_expr(p);
  1443. p=skip_space(p);
  1444. if(*p != '{') {
  1445. disp_error_message("parse_syntax: need '{'",p);
  1446. }
  1447. add_scriptc(C_FUNC);
  1448. return p + 1;
  1449. }
  1450. break;
  1451. case 'w':
  1452. case 'W':
  1453. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1454. int l;
  1455. char label[256];
  1456. p=skip_space(p2);
  1457. if(*p != '(') {
  1458. disp_error_message("need '('",p);
  1459. }
  1460. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1461. syntax.curly[syntax.curly_count].count = 1;
  1462. syntax.curly[syntax.curly_count].index = syntax.index++;
  1463. syntax.curly[syntax.curly_count].flag = 0;
  1464. // 条件判断開始のラベル形成する
  1465. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1466. l=add_str(label);
  1467. set_label(l,script_pos,p);
  1468. // 条件が偽なら終了地点に飛ばす
  1469. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1470. syntax.curly_count++;
  1471. add_scriptl(add_str("jump_zero"));
  1472. add_scriptc(C_ARG);
  1473. p=parse_expr(p);
  1474. p=skip_space(p);
  1475. add_scriptl(add_str(label));
  1476. add_scriptc(C_FUNC);
  1477. return p;
  1478. }
  1479. break;
  1480. }
  1481. return NULL;
  1482. }
  1483. const char* parse_syntax_close(const char *p) {
  1484. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1485. int flag;
  1486. do {
  1487. p = parse_syntax_close_sub(p,&flag);
  1488. } while(flag);
  1489. return p;
  1490. }
  1491. // if, for , while , do の閉じ判定
  1492. // flag == 1 : 閉じられた
  1493. // flag == 0 : 閉じられない
  1494. const char* parse_syntax_close_sub(const char* p,int* flag)
  1495. {
  1496. char label[256];
  1497. int pos = syntax.curly_count - 1;
  1498. int l;
  1499. *flag = 1;
  1500. if(syntax.curly_count <= 0) {
  1501. *flag = 0;
  1502. return p;
  1503. } else if(syntax.curly[pos].type == TYPE_IF) {
  1504. const char *bp = p;
  1505. const char *p2;
  1506. // if 最終場所へ飛ばす
  1507. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1508. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1509. parse_line(label);
  1510. syntax.curly_count--;
  1511. // 現在地のラベルを付ける
  1512. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1513. l=add_str(label);
  1514. set_label(l,script_pos,p);
  1515. syntax.curly[pos].count++;
  1516. p = skip_space(p);
  1517. p2 = skip_word(p);
  1518. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1519. // else or else - if
  1520. p = skip_space(p2);
  1521. p2 = skip_word(p);
  1522. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1523. // else - if
  1524. p=skip_space(p2);
  1525. if(*p != '(') {
  1526. disp_error_message("need '('",p);
  1527. }
  1528. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1529. add_scriptl(add_str("jump_zero"));
  1530. add_scriptc(C_ARG);
  1531. p=parse_expr(p);
  1532. p=skip_space(p);
  1533. add_scriptl(add_str(label));
  1534. add_scriptc(C_FUNC);
  1535. *flag = 0;
  1536. return p;
  1537. } else {
  1538. // else
  1539. if(!syntax.curly[pos].flag) {
  1540. syntax.curly[pos].flag = 1;
  1541. *flag = 0;
  1542. return p;
  1543. }
  1544. }
  1545. }
  1546. // if 閉じ
  1547. syntax.curly_count--;
  1548. // 最終地のラベルを付ける
  1549. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1550. l=add_str(label);
  1551. set_label(l,script_pos,p);
  1552. if(syntax.curly[pos].flag == 1) {
  1553. // このifに対するelseじゃないのでポインタの位置は同じ
  1554. return bp;
  1555. }
  1556. return p;
  1557. } else if(syntax.curly[pos].type == TYPE_DO) {
  1558. int l;
  1559. char label[256];
  1560. const char *p2;
  1561. if(syntax.curly[pos].flag) {
  1562. // 現在地のラベル形成する(continue でここに来る)
  1563. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1564. l=add_str(label);
  1565. set_label(l,script_pos,p);
  1566. }
  1567. // 条件が偽なら終了地点に飛ばす
  1568. p = skip_space(p);
  1569. p2 = skip_word(p);
  1570. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1571. disp_error_message("parse_syntax: need 'while'",p);
  1572. p = skip_space(p2);
  1573. if(*p != '(') {
  1574. disp_error_message("need '('",p);
  1575. }
  1576. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1577. add_scriptl(add_str("jump_zero"));
  1578. add_scriptc(C_ARG);
  1579. p=parse_expr(p);
  1580. p=skip_space(p);
  1581. add_scriptl(add_str(label));
  1582. add_scriptc(C_FUNC);
  1583. // 開始地点に飛ばす
  1584. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1585. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1586. parse_line(label);
  1587. syntax.curly_count--;
  1588. // 条件終了地点のラベル形成する
  1589. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1590. l=add_str(label);
  1591. set_label(l,script_pos,p);
  1592. p = skip_space(p);
  1593. if(*p != ';') {
  1594. disp_error_message("parse_syntax: need ';'",p);
  1595. return p+1;
  1596. }
  1597. p++;
  1598. syntax.curly_count--;
  1599. return p;
  1600. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1601. // 次のループに飛ばす
  1602. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1603. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1604. parse_line(label);
  1605. syntax.curly_count--;
  1606. // for 終了のラベル付け
  1607. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1608. l=add_str(label);
  1609. set_label(l,script_pos,p);
  1610. syntax.curly_count--;
  1611. return p;
  1612. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1613. // while 条件判断へ飛ばす
  1614. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1615. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1616. parse_line(label);
  1617. syntax.curly_count--;
  1618. // while 終了のラベル付け
  1619. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1620. l=add_str(label);
  1621. set_label(l,script_pos,p);
  1622. syntax.curly_count--;
  1623. return p;
  1624. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1625. int pos = syntax.curly_count-1;
  1626. char label[256];
  1627. int l;
  1628. // 戻す
  1629. sprintf(label,"return;");
  1630. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1631. parse_line(label);
  1632. syntax.curly_count--;
  1633. // 現在地のラベルを付ける
  1634. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1635. l=add_str(label);
  1636. set_label(l,script_pos,p);
  1637. syntax.curly_count--;
  1638. return p;
  1639. } else {
  1640. *flag = 0;
  1641. return p;
  1642. }
  1643. }
  1644. /*==========================================
  1645. * 組み込み関数の追加
  1646. *------------------------------------------*/
  1647. static void add_buildin_func(void)
  1648. {
  1649. int i,n;
  1650. const char* p;
  1651. for( i = 0; buildin_func[i].func; i++ )
  1652. {
  1653. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1654. // 'v' - value (either string or int or reference)
  1655. // 's' - string
  1656. // 'i' - int
  1657. // 'r' - reference (of a variable)
  1658. // 'l' - label
  1659. // '?' - one optional parameter
  1660. // '*' - unknown number of optional parameters
  1661. p = buildin_func[i].arg;
  1662. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1663. while( *p == '?' ) ++p;
  1664. if( *p == '*' ) ++p;
  1665. if( *p != 0){
  1666. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1667. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1668. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1669. } else {
  1670. n = add_str(buildin_func[i].name);
  1671. str_data[n].type = C_FUNC;
  1672. str_data[n].val = i;
  1673. str_data[n].func = buildin_func[i].func;
  1674. }
  1675. }
  1676. }
  1677. /*==========================================
  1678. * 定数データベースの読み込み
  1679. *------------------------------------------*/
  1680. static void read_constdb(void)
  1681. {
  1682. FILE *fp;
  1683. char line[1024],name[1024],val[1024];
  1684. int n,type;
  1685. sprintf(line, "%s/const.txt", db_path);
  1686. fp=fopen(line, "r");
  1687. if(fp==NULL){
  1688. ShowError("can't read %s\n", line);
  1689. return ;
  1690. }
  1691. while(fgets(line, sizeof(line), fp))
  1692. {
  1693. if(line[0]=='/' && line[1]=='/')
  1694. continue;
  1695. type=0;
  1696. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1697. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1698. n=add_str(name);
  1699. if(type==0)
  1700. str_data[n].type=C_INT;
  1701. else
  1702. str_data[n].type=C_PARAM;
  1703. str_data[n].val= (int)strtol(val,NULL,0);
  1704. }
  1705. }
  1706. fclose(fp);
  1707. }
  1708. /*==========================================
  1709. * エラー表示
  1710. *------------------------------------------*/
  1711. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1712. {
  1713. int i;
  1714. if( p == NULL || !p[0] ) return NULL;
  1715. if( line < 0 )
  1716. StringBuf_Printf(buf, "*% 5d : ", -line);
  1717. else
  1718. StringBuf_Printf(buf, " % 5d : ", line);
  1719. for(i=0;p[i] && p[i] != '\n';i++){
  1720. if(p + i != mark)
  1721. StringBuf_Printf(buf, "%c", p[i]);
  1722. else
  1723. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1724. }
  1725. StringBuf_AppendStr(buf, "\n");
  1726. return p+i+(p[i] == '\n' ? 1 : 0);
  1727. }
  1728. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1729. {
  1730. // エラーが発生した行を求める
  1731. int j;
  1732. int line = start_line;
  1733. const char *p;
  1734. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1735. StringBuf buf;
  1736. for(p=src;p && *p;line++){
  1737. const char *lineend=strchr(p,'\n');
  1738. if(lineend==NULL || error_pos<lineend){
  1739. break;
  1740. }
  1741. for( j = 0; j < 4; j++ ) {
  1742. linestart[j] = linestart[j+1];
  1743. }
  1744. linestart[4] = p;
  1745. p=lineend+1;
  1746. }
  1747. StringBuf_Init(&buf);
  1748. StringBuf_AppendStr(&buf, "\a\n");
  1749. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1750. StringBuf_Printf(&buf, " %s\n", error_msg);
  1751. for(j = 0; j < 5; j++ ) {
  1752. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1753. }
  1754. p = script_print_line(&buf, p, error_pos, -line);
  1755. for(j = 0; j < 5; j++) {
  1756. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1757. }
  1758. ShowError("%s", StringBuf_Value(&buf));
  1759. StringBuf_Destroy(&buf);
  1760. }
  1761. /*==========================================
  1762. * スクリプトの解析
  1763. *------------------------------------------*/
  1764. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1765. {
  1766. const char *p,*tmpp;
  1767. int i;
  1768. struct script_code* code = NULL;
  1769. static int first=1;
  1770. char end;
  1771. if( src == NULL )
  1772. return NULL;// empty script
  1773. memset(&syntax,0,sizeof(syntax));
  1774. if(first){
  1775. add_buildin_func();
  1776. read_constdb();
  1777. first=0;
  1778. }
  1779. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1780. script_pos=0;
  1781. script_size=SCRIPT_BLOCK_SIZE;
  1782. str_data[LABEL_NEXTLINE].type=C_NOP;
  1783. str_data[LABEL_NEXTLINE].backpatch=-1;
  1784. str_data[LABEL_NEXTLINE].label=-1;
  1785. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1786. if( options&SCRIPT_USE_LABEL_DB )
  1787. scriptlabel_db->clear(scriptlabel_db, NULL);
  1788. parse_options = options;
  1789. if( setjmp( error_jump ) != 0 ) {
  1790. //Restore program state when script has problems. [from jA]
  1791. int i;
  1792. const int size = ARRAYLENGTH(syntax.curly);
  1793. if( error_report )
  1794. script_error(src,file,line,error_msg,error_pos);
  1795. aFree( error_msg );
  1796. aFree( script_buf );
  1797. script_pos = 0;
  1798. script_size = 0;
  1799. script_buf = NULL;
  1800. for(i=LABEL_START;i<str_num;i++)
  1801. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1802. for(i=0; i<size; i++)
  1803. linkdb_final(&syntax.curly[i].case_label);
  1804. return NULL;
  1805. }
  1806. parse_syntax_for_flag=0;
  1807. p=src;
  1808. p=skip_space(p);
  1809. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1810. {// does not require brackets around the script
  1811. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1812. {// empty script and can return NULL
  1813. aFree( script_buf );
  1814. script_pos = 0;
  1815. script_size = 0;
  1816. script_buf = NULL;
  1817. return NULL;
  1818. }
  1819. end = '\0';
  1820. }
  1821. else
  1822. {// requires brackets around the script
  1823. if( *p != '{' )
  1824. disp_error_message("not found '{'",p);
  1825. p = skip_space(p+1);
  1826. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1827. {// empty script and can return NULL
  1828. aFree( script_buf );
  1829. script_pos = 0;
  1830. script_size = 0;
  1831. script_buf = NULL;
  1832. return NULL;
  1833. }
  1834. end = '}';
  1835. }
  1836. // clear references of labels, variables and internal functions
  1837. for(i=LABEL_START;i<str_num;i++){
  1838. if(
  1839. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1840. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1841. ){
  1842. str_data[i].type=C_NOP;
  1843. str_data[i].backpatch=-1;
  1844. str_data[i].label=-1;
  1845. }
  1846. }
  1847. while( syntax.curly_count != 0 || *p != end )
  1848. {
  1849. if( *p == '\0' )
  1850. disp_error_message("unexpected end of script",p);
  1851. // labelだけ特殊処理
  1852. tmpp=skip_space(skip_word(p));
  1853. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1854. i=add_word(p);
  1855. set_label(i,script_pos,p);
  1856. if( parse_options&SCRIPT_USE_LABEL_DB )
  1857. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1858. p=tmpp+1;
  1859. p=skip_space(p);
  1860. continue;
  1861. }
  1862. // 他は全部一緒くた
  1863. p=parse_line(p);
  1864. p=skip_space(p);
  1865. add_scriptc(C_EOL);
  1866. set_label(LABEL_NEXTLINE,script_pos,p);
  1867. str_data[LABEL_NEXTLINE].type=C_NOP;
  1868. str_data[LABEL_NEXTLINE].backpatch=-1;
  1869. str_data[LABEL_NEXTLINE].label=-1;
  1870. }
  1871. add_scriptc(C_NOP);
  1872. // trim code to size
  1873. script_size = script_pos;
  1874. RECREATE(script_buf,unsigned char,script_pos);
  1875. // default unknown references to variables
  1876. for(i=LABEL_START;i<str_num;i++){
  1877. if(str_data[i].type==C_NOP){
  1878. int j,next;
  1879. str_data[i].type=C_NAME;
  1880. str_data[i].label=i;
  1881. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1882. next=GETVALUE(script_buf,j);
  1883. SETVALUE(script_buf,j,i);
  1884. j=next;
  1885. }
  1886. }
  1887. }
  1888. #ifdef DEBUG_DISP
  1889. for(i=0;i<script_pos;i++){
  1890. if((i&15)==0) printf("%04x : ",i);
  1891. ShowMessage("%02x ",script_buf[i]);
  1892. if((i&15)==15) printf("\n");
  1893. }
  1894. ShowMessage("\n");
  1895. #endif
  1896. #ifdef DEBUG_DISASM
  1897. {
  1898. int i = 0,j;
  1899. while(i < script_pos) {
  1900. ShowMessage("%06x ",i);
  1901. j = i;
  1902. switch(get_com(script_buf,&i)) {
  1903. case C_EOL: printf("C_EOL"); break;
  1904. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1905. case C_POS:
  1906. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1907. i += 3;
  1908. break;
  1909. case C_NAME:
  1910. j = (*(int*)(script_buf+i)&0xffffff);
  1911. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : get_str(j));
  1912. i += 3;
  1913. break;
  1914. case C_ARG: ShowMessage("C_ARG"); break;
  1915. case C_FUNC: ShowMessage("C_FUNC"); break;
  1916. case C_ADD: ShowMessage("C_ADD"); break;
  1917. case C_SUB: ShowMessage("C_SUB"); break;
  1918. case C_MUL: ShowMessage("C_MUL"); break;
  1919. case C_DIV: ShowMessage("C_DIV"); break;
  1920. case C_MOD: ShowMessage("C_MOD"); break;
  1921. case C_EQ: ShowMessage("C_EQ"); break;
  1922. case C_NE: ShowMessage("C_NE"); break;
  1923. case C_GT: ShowMessage("C_GT"); break;
  1924. case C_GE: ShowMessage("C_GE"); break;
  1925. case C_LT: ShowMessage("C_LT"); break;
  1926. case C_LE: ShowMessage("C_LE"); break;
  1927. case C_AND: ShowMessage("C_AND"); break;
  1928. case C_OR: ShowMessage("C_OR"); break;
  1929. case C_XOR: ShowMessage("C_XOR"); break;
  1930. case C_LAND: ShowMessage("C_LAND"); break;
  1931. case C_LOR: ShowMessage("C_LOR"); break;
  1932. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1933. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1934. case C_NEG: ShowMessage("C_NEG"); break;
  1935. case C_NOT: ShowMessage("C_NOT"); break;
  1936. case C_LNOT: ShowMessage("C_LNOT"); break;
  1937. case C_NOP: ShowMessage("C_NOP"); break;
  1938. case C_OP3: ShowMessage("C_OP3"); break;
  1939. case C_STR:
  1940. j = strlen(script_buf + i);
  1941. ShowMessage("C_STR %s",script_buf + i);
  1942. i+= j+1;
  1943. break;
  1944. default:
  1945. ShowMessage("unknown");
  1946. }
  1947. ShowMessage(CL_CLL "\n");
  1948. }
  1949. }
  1950. #endif
  1951. CREATE(code,struct script_code,1);
  1952. code->script_buf = script_buf;
  1953. code->script_size = script_size;
  1954. code->script_vars = NULL;
  1955. return code;
  1956. }
  1957. /// Returns the player attached to this script, identified by the rid.
  1958. /// If there is no player attached, the script is terminated.
  1959. TBL_PC *script_rid2sd(struct script_state *st)
  1960. {
  1961. TBL_PC *sd=map_id2sd(st->rid);
  1962. if(!sd){
  1963. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1964. script_reportsrc(st);
  1965. st->state = END;
  1966. }
  1967. return sd;
  1968. }
  1969. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1970. ///
  1971. /// @param st Script state
  1972. /// @param data Variable/constant
  1973. void get_val(struct script_state* st, struct script_data* data)
  1974. {
  1975. const char* name;
  1976. char prefix;
  1977. char postfix;
  1978. TBL_PC* sd = NULL;
  1979. if( !data_isreference(data) )
  1980. return;// not a variable/constant
  1981. name = reference_getname(data);
  1982. prefix = name[0];
  1983. postfix = name[strlen(name) - 1];
  1984. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1985. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1986. {
  1987. sd = script_rid2sd(st);
  1988. if( sd == NULL )
  1989. {// needs player attached
  1990. if( postfix == '$' )
  1991. {// string variable
  1992. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1993. data->type = C_CONSTSTR;
  1994. data->u.str = "";
  1995. }
  1996. else
  1997. {// integer variable
  1998. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1999. data->type = C_INT;
  2000. data->u.num = 0;
  2001. }
  2002. return;
  2003. }
  2004. }
  2005. if( postfix == '$' )
  2006. {// string variable
  2007. switch( prefix )
  2008. {
  2009. case '@':
  2010. data->u.str = pc_readregstr(sd, data->u.num);
  2011. break;
  2012. case '$':
  2013. data->u.str = mapreg_readregstr(data->u.num);
  2014. break;
  2015. case '#':
  2016. if( name[1] == '#' )
  2017. data->u.str = pc_readaccountreg2str(sd, name);// global
  2018. else
  2019. data->u.str = pc_readaccountregstr(sd, name);// local
  2020. break;
  2021. case '.':
  2022. {
  2023. struct linkdb_node** n =
  2024. data->ref ? data->ref:
  2025. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2026. &st->script->script_vars;// npc variable
  2027. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2028. }
  2029. break;
  2030. case '\'':
  2031. {
  2032. struct linkdb_node** n = NULL;
  2033. if( st->instance_id )
  2034. n = &instance[st->instance_id].svar;
  2035. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2036. }
  2037. break;
  2038. default:
  2039. data->u.str = pc_readglobalreg_str(sd, name);
  2040. break;
  2041. }
  2042. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2043. {// empty string
  2044. data->type = C_CONSTSTR;
  2045. data->u.str = "";
  2046. }
  2047. else
  2048. {// duplicate string
  2049. data->type = C_STR;
  2050. data->u.str = aStrdup(data->u.str);
  2051. }
  2052. }
  2053. else
  2054. {// integer variable
  2055. data->type = C_INT;
  2056. if( reference_toconstant(data) )
  2057. {
  2058. data->u.num = reference_getconstant(data);
  2059. }
  2060. else if( reference_toparam(data) )
  2061. {
  2062. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2063. }
  2064. else
  2065. switch( prefix )
  2066. {
  2067. case '@':
  2068. data->u.num = pc_readreg(sd, data->u.num);
  2069. break;
  2070. case '$':
  2071. data->u.num = mapreg_readreg(data->u.num);
  2072. break;
  2073. case '#':
  2074. if( name[1] == '#' )
  2075. data->u.num = pc_readaccountreg2(sd, name);// global
  2076. else
  2077. data->u.num = pc_readaccountreg(sd, name);// local
  2078. break;
  2079. case '.':
  2080. {
  2081. struct linkdb_node** n =
  2082. data->ref ? data->ref:
  2083. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2084. &st->script->script_vars;// npc variable
  2085. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2086. }
  2087. break;
  2088. case '\'':
  2089. {
  2090. struct linkdb_node** n = NULL;
  2091. if( st->instance_id )
  2092. n = &instance[st->instance_id].ivar;
  2093. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2094. }
  2095. break;
  2096. default:
  2097. data->u.num = pc_readglobalreg(sd, name);
  2098. break;
  2099. }
  2100. }
  2101. return;
  2102. }
  2103. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2104. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2105. /// The value is left in the top of the stack and needs to be removed manually.
  2106. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2107. {
  2108. struct script_data* data;
  2109. push_val2(st->stack, C_NAME, uid, ref);
  2110. data = script_getdatatop(st, -1);
  2111. get_val(st, data);
  2112. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2113. }
  2114. /*==========================================
  2115. * Stores the value of a script variable
  2116. * Return value is 0 on fail, 1 on success.
  2117. *------------------------------------------*/
  2118. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2119. {
  2120. char prefix = name[0];
  2121. if( is_string_variable(name) )
  2122. {// string variable
  2123. const char* str = (const char*)value;
  2124. switch (prefix) {
  2125. case '@':
  2126. return pc_setregstr(sd, num, str);
  2127. case '$':
  2128. return mapreg_setregstr(num, str);
  2129. case '#':
  2130. return (name[1] == '#') ?
  2131. pc_setaccountreg2str(sd, name, str) :
  2132. pc_setaccountregstr(sd, name, str);
  2133. case '.': {
  2134. char* p;
  2135. struct linkdb_node** n;
  2136. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2137. p = (char*)linkdb_erase(n, (void*)num);
  2138. if (p) aFree(p);
  2139. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2140. }
  2141. return 1;
  2142. case '\'': {
  2143. char *p;
  2144. struct linkdb_node** n = NULL;
  2145. if( st->instance_id )
  2146. n = &instance[st->instance_id].svar;
  2147. p = (char*)linkdb_erase(n, (void*)num);
  2148. if (p) aFree(p);
  2149. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2150. }
  2151. return 1;
  2152. default:
  2153. return pc_setglobalreg_str(sd, name, str);
  2154. }
  2155. }
  2156. else
  2157. {// integer variable
  2158. int val = (int)value;
  2159. if(str_data[num&0x00ffffff].type == C_PARAM)
  2160. {
  2161. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2162. {
  2163. if( st != NULL )
  2164. {
  2165. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2166. script_reportsrc(st);
  2167. st->state = END;
  2168. }
  2169. return 0;
  2170. }
  2171. return 1;
  2172. }
  2173. switch (prefix) {
  2174. case '@':
  2175. return pc_setreg(sd, num, val);
  2176. case '$':
  2177. return mapreg_setreg(num, val);
  2178. case '#':
  2179. return (name[1] == '#') ?
  2180. pc_setaccountreg2(sd, name, val) :
  2181. pc_setaccountreg(sd, name, val);
  2182. case '.': {
  2183. struct linkdb_node** n;
  2184. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2185. if (val == 0)
  2186. linkdb_erase(n, (void*)num);
  2187. else
  2188. linkdb_replace(n, (void*)num, (void*)val);
  2189. }
  2190. return 1;
  2191. case '\'':
  2192. {
  2193. struct linkdb_node** n = NULL;
  2194. if( st->instance_id )
  2195. n = &instance[st->instance_id].ivar;
  2196. if( val == 0 )
  2197. linkdb_erase(n, (void*)num);
  2198. else
  2199. linkdb_replace(n, (void*)num, (void*)val);
  2200. return 1;
  2201. }
  2202. default:
  2203. return pc_setglobalreg(sd, name, val);
  2204. }
  2205. }
  2206. }
  2207. int set_var(TBL_PC* sd, char* name, void* val)
  2208. {
  2209. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2210. }
  2211. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2212. {
  2213. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2214. }
  2215. /// Converts the data to a string
  2216. const char* conv_str(struct script_state* st, struct script_data* data)
  2217. {
  2218. char* p;
  2219. get_val(st, data);
  2220. if( data_isstring(data) )
  2221. {// nothing to convert
  2222. }
  2223. else if( data_isint(data) )
  2224. {// int -> string
  2225. CREATE(p, char, ITEM_NAME_LENGTH);
  2226. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2227. p[ITEM_NAME_LENGTH-1] = '\0';
  2228. data->type = C_STR;
  2229. data->u.str = p;
  2230. }
  2231. else if( data_isreference(data) )
  2232. {// reference -> string
  2233. //##TODO when does this happen (check get_val) [FlavioJS]
  2234. data->type = C_CONSTSTR;
  2235. data->u.str = reference_getname(data);
  2236. }
  2237. else
  2238. {// unsupported data type
  2239. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2240. script_reportdata(data);
  2241. script_reportsrc(st);
  2242. data->type = C_CONSTSTR;
  2243. data->u.str = "";
  2244. }
  2245. return data->u.str;
  2246. }
  2247. /// Converts the data to an int
  2248. int conv_num(struct script_state* st, struct script_data* data)
  2249. {
  2250. char* p;
  2251. long num;
  2252. get_val(st, data);
  2253. if( data_isint(data) )
  2254. {// nothing to convert
  2255. }
  2256. else if( data_isstring(data) )
  2257. {// string -> int
  2258. // the result does not overflow or underflow, it is capped instead
  2259. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2260. p = data->u.str;
  2261. errno = 0;
  2262. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2263. if( errno == ERANGE
  2264. #if LONG_MAX > INT_MAX
  2265. || num < INT_MIN || num > INT_MAX
  2266. #endif
  2267. )
  2268. {
  2269. if( num <= INT_MIN )
  2270. {
  2271. num = INT_MIN;
  2272. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2273. }
  2274. else//if( num >= INT_MAX )
  2275. {
  2276. num = INT_MAX;
  2277. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2278. }
  2279. script_reportdata(data);
  2280. script_reportsrc(st);
  2281. }
  2282. if( data->type == C_STR )
  2283. aFree(p);
  2284. data->type = C_INT;
  2285. data->u.num = (int)num;
  2286. }
  2287. #if 0
  2288. // FIXME this function is being used to retrieve the position of labels and
  2289. // probably other stuff [FlavioJS]
  2290. else
  2291. {// unsupported data type
  2292. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2293. script_reportdata(data);
  2294. script_reportsrc(st);
  2295. data->type = C_INT;
  2296. data->u.num = 0;
  2297. }
  2298. #endif
  2299. return data->u.num;
  2300. }
  2301. //
  2302. // Stack operations
  2303. //
  2304. /// Increases the size of the stack
  2305. void stack_expand(struct script_stack* stack)
  2306. {
  2307. stack->sp_max += 64;
  2308. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2309. stack->sp_max * sizeof(stack->stack_data[0]) );
  2310. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2311. 64 * sizeof(stack->stack_data[0]) );
  2312. }
  2313. /// Pushes a value into the stack
  2314. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2315. /// Pushes a value into the stack (with reference)
  2316. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2317. {
  2318. if( stack->sp >= stack->sp_max )
  2319. stack_expand(stack);
  2320. stack->stack_data[stack->sp].type = type;
  2321. stack->stack_data[stack->sp].u.num = val;
  2322. stack->stack_data[stack->sp].ref = ref;
  2323. stack->sp++;
  2324. return &stack->stack_data[stack->sp-1];
  2325. }
  2326. /// Pushes a string into the stack
  2327. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2328. {
  2329. if( stack->sp >= stack->sp_max )
  2330. stack_expand(stack);
  2331. stack->stack_data[stack->sp].type = type;
  2332. stack->stack_data[stack->sp].u.str = str;
  2333. stack->stack_data[stack->sp].ref = NULL;
  2334. stack->sp++;
  2335. return &stack->stack_data[stack->sp-1];
  2336. }
  2337. /// Pushes a retinfo into the stack
  2338. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2339. {
  2340. if( stack->sp >= stack->sp_max )
  2341. stack_expand(stack);
  2342. stack->stack_data[stack->sp].type = C_RETINFO;
  2343. stack->stack_data[stack->sp].u.ri = ri;
  2344. stack->stack_data[stack->sp].ref = NULL;
  2345. stack->sp++;
  2346. return &stack->stack_data[stack->sp-1];
  2347. }
  2348. /// Pushes a copy of the target position into the stack
  2349. struct script_data* push_copy(struct script_stack* stack, int pos)
  2350. {
  2351. switch( stack->stack_data[pos].type )
  2352. {
  2353. case C_CONSTSTR:
  2354. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2355. break;
  2356. case C_STR:
  2357. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2358. break;
  2359. case C_RETINFO:
  2360. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2361. exit(1);
  2362. break;
  2363. default:
  2364. return push_val2(
  2365. stack,stack->stack_data[pos].type,
  2366. stack->stack_data[pos].u.num,
  2367. stack->stack_data[pos].ref
  2368. );
  2369. break;
  2370. }
  2371. }
  2372. /// Removes the values in indexes [start,end[ from the stack.
  2373. /// Adjusts all stack pointers.
  2374. void pop_stack(struct script_state* st, int start, int end)
  2375. {
  2376. struct script_stack* stack = st->stack;
  2377. struct script_data* data;
  2378. int i;
  2379. if( start < 0 )
  2380. start = 0;
  2381. if( end > stack->sp )
  2382. end = stack->sp;
  2383. if( start >= end )
  2384. return;// nothing to pop
  2385. // free stack elements
  2386. for( i = start; i < end; i++ )
  2387. {
  2388. data = &stack->stack_data[i];
  2389. if( data->type == C_STR )
  2390. aFree(data->u.str);
  2391. if( data->type == C_RETINFO )
  2392. {
  2393. struct script_retinfo* ri = data->u.ri;
  2394. if( ri->var_function )
  2395. {
  2396. script_free_vars(ri->var_function);
  2397. aFree(ri->var_function);
  2398. }
  2399. aFree(ri);
  2400. }
  2401. data->type = C_NOP;
  2402. }
  2403. // move the rest of the elements
  2404. if( stack->sp > end )
  2405. {
  2406. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2407. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2408. stack->stack_data[i].type = C_NOP;
  2409. }
  2410. // adjust stack pointers
  2411. if( st->start > end ) st->start -= end - start;
  2412. else if( st->start > start ) st->start = start;
  2413. if( st->end > end ) st->end -= end - start;
  2414. else if( st->end > start ) st->end = start;
  2415. if( stack->defsp > end ) stack->defsp -= end - start;
  2416. else if( stack->defsp > start ) stack->defsp = start;
  2417. stack->sp -= end - start;
  2418. }
  2419. ///
  2420. ///
  2421. ///
  2422. /*==========================================
  2423. * スクリプト依存変数、関数依存変数の解放
  2424. *------------------------------------------*/
  2425. void script_free_vars(struct linkdb_node **node)
  2426. {
  2427. struct linkdb_node* n = *node;
  2428. while( n != NULL)
  2429. {
  2430. const char* name = get_str((int)(n->key)&0x00ffffff);
  2431. if( is_string_variable(name) )
  2432. aFree(n->data); // 文字型変数なので、データ削除
  2433. n = n->next;
  2434. }
  2435. linkdb_final( node );
  2436. }
  2437. void script_free_code(struct script_code* code)
  2438. {
  2439. script_free_vars( &code->script_vars );
  2440. aFree( code->script_buf );
  2441. aFree( code );
  2442. }
  2443. /// Creates a new script state.
  2444. ///
  2445. /// @param script Script code
  2446. /// @param pos Position in the code
  2447. /// @param rid Who is running the script (attached player)
  2448. /// @param oid Where the code is being run (npc 'object')
  2449. /// @return Script state
  2450. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2451. {
  2452. struct script_state* st;
  2453. CREATE(st, struct script_state, 1);
  2454. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2455. st->stack->sp = 0;
  2456. st->stack->sp_max = 64;
  2457. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2458. st->stack->defsp = st->stack->sp;
  2459. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2460. st->state = RUN;
  2461. st->script = script;
  2462. //st->scriptroot = script;
  2463. st->pos = pos;
  2464. st->rid = rid;
  2465. st->oid = oid;
  2466. st->sleep.timer = INVALID_TIMER;
  2467. return st;
  2468. }
  2469. /// Frees a script state.
  2470. ///
  2471. /// @param st Script state
  2472. void script_free_state(struct script_state* st)
  2473. {
  2474. if(st->bk_st)
  2475. {// backup was not restored
  2476. 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);
  2477. }
  2478. if( st->sleep.timer != INVALID_TIMER )
  2479. delete_timer(st->sleep.timer, run_script_timer);
  2480. script_free_vars(st->stack->var_function);
  2481. aFree(st->stack->var_function);
  2482. pop_stack(st, 0, st->stack->sp);
  2483. aFree(st->stack->stack_data);
  2484. aFree(st->stack);
  2485. st->pos = -1;
  2486. aFree(st);
  2487. }
  2488. //
  2489. // 実行部main
  2490. //
  2491. /*==========================================
  2492. * コマンドの読み取り
  2493. *------------------------------------------*/
  2494. c_op get_com(unsigned char *script,int *pos)
  2495. {
  2496. int i = 0, j = 0;
  2497. if(script[*pos]>=0x80){
  2498. return C_INT;
  2499. }
  2500. while(script[*pos]>=0x40){
  2501. i=script[(*pos)++]<<j;
  2502. j+=6;
  2503. }
  2504. return (c_op)(i+(script[(*pos)++]<<j));
  2505. }
  2506. /*==========================================
  2507. * 数値の所得
  2508. *------------------------------------------*/
  2509. int get_num(unsigned char *script,int *pos)
  2510. {
  2511. int i,j;
  2512. i=0; j=0;
  2513. while(script[*pos]>=0xc0){
  2514. i+=(script[(*pos)++]&0x7f)<<j;
  2515. j+=6;
  2516. }
  2517. return i+((script[(*pos)++]&0x7f)<<j);
  2518. }
  2519. /*==========================================
  2520. * スタックから値を取り出す
  2521. *------------------------------------------*/
  2522. int pop_val(struct script_state* st)
  2523. {
  2524. if(st->stack->sp<=0)
  2525. return 0;
  2526. st->stack->sp--;
  2527. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2528. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2529. return st->stack->stack_data[st->stack->sp].u.num;
  2530. return 0;
  2531. }
  2532. /// Ternary operators
  2533. /// test ? if_true : if_false
  2534. void op_3(struct script_state* st, int op)
  2535. {
  2536. struct script_data* data;
  2537. int flag = 0;
  2538. data = script_getdatatop(st, -3);
  2539. get_val(st, data);
  2540. if( data_isstring(data) )
  2541. flag = data->u.str[0];// "" -> false
  2542. else if( data_isint(data) )
  2543. flag = data->u.num;// 0 -> false
  2544. else
  2545. {
  2546. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2547. script_reportdata(data);
  2548. script_reportsrc(st);
  2549. script_removetop(st, -3, 0);
  2550. script_pushnil(st);
  2551. return;
  2552. }
  2553. if( flag )
  2554. script_pushcopytop(st, -2);
  2555. else
  2556. script_pushcopytop(st, -1);
  2557. script_removetop(st, -4, -1);
  2558. }
  2559. /// Binary string operators
  2560. /// s1 EQ s2 -> i
  2561. /// s1 NE s2 -> i
  2562. /// s1 GT s2 -> i
  2563. /// s1 GE s2 -> i
  2564. /// s1 LT s2 -> i
  2565. /// s1 LE s2 -> i
  2566. /// s1 ADD s2 -> s
  2567. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2568. {
  2569. int a = 0;
  2570. switch(op){
  2571. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2572. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2573. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2574. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2575. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2576. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2577. case C_ADD:
  2578. {
  2579. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2580. strcpy(buf, s1);
  2581. strcat(buf, s2);
  2582. script_pushstr(st, buf);
  2583. return;
  2584. }
  2585. default:
  2586. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2587. script_reportsrc(st);
  2588. script_pushnil(st);
  2589. st->state = END;
  2590. return;
  2591. }
  2592. script_pushint(st,a);
  2593. }
  2594. /// Binary number operators
  2595. /// i OP i -> i
  2596. void op_2num(struct script_state* st, int op, int i1, int i2)
  2597. {
  2598. int ret;
  2599. double ret_double;
  2600. switch( op )
  2601. {
  2602. case C_AND: ret = i1 & i2; break;
  2603. case C_OR: ret = i1 | i2; break;
  2604. case C_XOR: ret = i1 ^ i2; break;
  2605. case C_LAND: ret = (i1 && i2); break;
  2606. case C_LOR: ret = (i1 || i2); break;
  2607. case C_EQ: ret = (i1 == i2); break;
  2608. case C_NE: ret = (i1 != i2); break;
  2609. case C_GT: ret = (i1 > i2); break;
  2610. case C_GE: ret = (i1 >= i2); break;
  2611. case C_LT: ret = (i1 < i2); break;
  2612. case C_LE: ret = (i1 <= i2); break;
  2613. case C_R_SHIFT: ret = i1>>i2; break;
  2614. case C_L_SHIFT: ret = i1<<i2; break;
  2615. case C_DIV:
  2616. case C_MOD:
  2617. if( i2 == 0 )
  2618. {
  2619. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2620. script_reportsrc(st);
  2621. script_pushnil(st);
  2622. st->state = END;
  2623. return;
  2624. }
  2625. else if( op == C_DIV )
  2626. ret = i1 / i2;
  2627. else//if( op == C_MOD )
  2628. ret = i1 % i2;
  2629. break;
  2630. default:
  2631. switch( op )
  2632. {// operators that can overflow/underflow
  2633. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2634. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2635. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2636. default:
  2637. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2638. script_reportsrc(st);
  2639. script_pushnil(st);
  2640. return;
  2641. }
  2642. if( ret_double < (double)INT_MIN )
  2643. {
  2644. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2645. script_reportsrc(st);
  2646. ret = INT_MIN;
  2647. }
  2648. else if( ret_double > (double)INT_MAX )
  2649. {
  2650. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2651. script_reportsrc(st);
  2652. ret = INT_MAX;
  2653. }
  2654. }
  2655. script_pushint(st, ret);
  2656. }
  2657. /// Binary operators
  2658. void op_2(struct script_state *st, int op)
  2659. {
  2660. struct script_data* left;
  2661. struct script_data* right;
  2662. left = script_getdatatop(st, -2);
  2663. right = script_getdatatop(st, -1);
  2664. get_val(st, left);
  2665. get_val(st, right);
  2666. // automatic conversions
  2667. switch( op )
  2668. {
  2669. case C_ADD:
  2670. if( data_isint(left) && data_isstring(right) )
  2671. {// convert int-string to string-string
  2672. conv_str(st, left);
  2673. }
  2674. else if( data_isstring(left) && data_isint(right) )
  2675. {// convert string-int to string-string
  2676. conv_str(st, right);
  2677. }
  2678. break;
  2679. }
  2680. if( data_isstring(left) && data_isstring(right) )
  2681. {// ss => op_2str
  2682. op_2str(st, op, left->u.str, right->u.str);
  2683. script_removetop(st, -3, -1);// pop the two values before the top one
  2684. }
  2685. else if( data_isint(left) && data_isint(right) )
  2686. {// ii => op_2num
  2687. int i1 = left->u.num;
  2688. int i2 = right->u.num;
  2689. script_removetop(st, -2, 0);
  2690. op_2num(st, op, i1, i2);
  2691. }
  2692. else
  2693. {// invalid argument
  2694. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2695. script_reportdata(left);
  2696. script_reportdata(right);
  2697. script_reportsrc(st);
  2698. script_removetop(st, -2, 0);
  2699. script_pushnil(st);
  2700. st->state = END;
  2701. }
  2702. }
  2703. /// Unary operators
  2704. /// NEG i -> i
  2705. /// NOT i -> i
  2706. /// LNOT i -> i
  2707. void op_1(struct script_state* st, int op)
  2708. {
  2709. struct script_data* data;
  2710. int i1;
  2711. data = script_getdatatop(st, -1);
  2712. get_val(st, data);
  2713. if( !data_isint(data) )
  2714. {// not a number
  2715. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2716. script_reportdata(data);
  2717. script_reportsrc(st);
  2718. script_pushnil(st);
  2719. st->state = END;
  2720. return;
  2721. }
  2722. i1 = data->u.num;
  2723. script_removetop(st, -1, 0);
  2724. switch( op )
  2725. {
  2726. case C_NEG: i1 = -i1; break;
  2727. case C_NOT: i1 = ~i1; break;
  2728. case C_LNOT: i1 = !i1; break;
  2729. default:
  2730. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2731. script_reportsrc(st);
  2732. script_pushnil(st);
  2733. st->state = END;
  2734. return;
  2735. }
  2736. script_pushint(st, i1);
  2737. }
  2738. /// Executes a buildin command.
  2739. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2740. int run_func(struct script_state *st)
  2741. {
  2742. struct script_data* data;
  2743. int i,start_sp,end_sp,func;
  2744. end_sp = st->stack->sp;// position after the last argument
  2745. for( i = end_sp-1; i > 0 ; --i )
  2746. if( st->stack->stack_data[i].type == C_ARG )
  2747. break;
  2748. if( i == 0 )
  2749. {
  2750. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2751. st->state = END;
  2752. script_reportsrc(st);
  2753. return 1;
  2754. }
  2755. start_sp = i-1;// C_NAME of the command
  2756. st->start = start_sp;
  2757. st->end = end_sp;
  2758. data = &st->stack->stack_data[st->start];
  2759. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2760. func = data->u.num;
  2761. else
  2762. {
  2763. ShowError("script:run_func: not a buildin command.\n");
  2764. script_reportdata(data);
  2765. script_reportsrc(st);
  2766. st->state = END;
  2767. return 1;
  2768. }
  2769. if(str_data[func].func){
  2770. if (str_data[func].func(st)) //Report error
  2771. script_reportsrc(st);
  2772. } else {
  2773. 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));
  2774. script_reportsrc(st);
  2775. st->state = END;
  2776. }
  2777. // Stack's datum are used when re-running functions [Eoe]
  2778. if( st->state == RERUNLINE )
  2779. return 0;
  2780. pop_stack(st, st->start, st->end);
  2781. if( st->state == RETFUNC )
  2782. {// return from a user-defined function
  2783. struct script_retinfo* ri;
  2784. int olddefsp = st->stack->defsp;
  2785. int nargs;
  2786. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2787. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2788. {
  2789. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2790. script_reportsrc(st);
  2791. st->state = END;
  2792. return 1;
  2793. }
  2794. script_free_vars( st->stack->var_function );
  2795. aFree(st->stack->var_function);
  2796. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2797. nargs = ri->nargs;
  2798. st->pos = ri->pos;
  2799. st->script = ri->script;
  2800. st->stack->var_function = ri->var_function;
  2801. st->stack->defsp = ri->defsp;
  2802. memset(ri, 0, sizeof(struct script_retinfo));
  2803. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2804. st->state = GOTO;
  2805. }
  2806. return 0;
  2807. }
  2808. /*==========================================
  2809. * script execution
  2810. *------------------------------------------*/
  2811. void run_script_main(struct script_state *st);
  2812. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2813. {
  2814. struct script_state *st;
  2815. if( rootscript == NULL || pos < 0 )
  2816. return;
  2817. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2818. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2819. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2820. st = script_alloc_state(rootscript, pos, rid, oid);
  2821. run_script_main(st);
  2822. }
  2823. void script_stop_sleeptimers(int id)
  2824. {
  2825. struct script_state* st;
  2826. for(;;)
  2827. {
  2828. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2829. if( st == NULL )
  2830. break; // no more sleep timers
  2831. script_free_state(st);
  2832. }
  2833. }
  2834. /*==========================================
  2835. * 指定ノードをsleep_dbから削除
  2836. *------------------------------------------*/
  2837. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2838. {
  2839. struct linkdb_node *retnode;
  2840. if( n == NULL)
  2841. return NULL;
  2842. if( n->prev == NULL )
  2843. sleep_db = n->next;
  2844. else
  2845. n->prev->next = n->next;
  2846. if( n->next )
  2847. n->next->prev = n->prev;
  2848. retnode = n->next;
  2849. aFree( n );
  2850. return retnode; // 次のノードを返す
  2851. }
  2852. /*==========================================
  2853. * sleep用タイマー関数
  2854. *------------------------------------------*/
  2855. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2856. {
  2857. struct script_state *st = (struct script_state *)data;
  2858. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2859. TBL_PC *sd = map_id2sd(st->rid);
  2860. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2861. { //Character mismatch. Cancel execution.
  2862. st->rid = 0;
  2863. st->state = END;
  2864. }
  2865. while( node && st->sleep.timer != -1 ) {
  2866. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2867. script_erase_sleepdb(node);
  2868. st->sleep.timer = INVALID_TIMER;
  2869. break;
  2870. }
  2871. node = node->next;
  2872. }
  2873. if(st->state != RERUNLINE)
  2874. st->sleep.tick = 0;
  2875. run_script_main(st);
  2876. return 0;
  2877. }
  2878. /// Detaches script state from possibly attached character and restores it's previous script if any.
  2879. ///
  2880. /// @param st Script state to detach.
  2881. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  2882. static void script_detach_state(struct script_state* st, bool dequeue_event)
  2883. {
  2884. struct map_session_data* sd;
  2885. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  2886. {
  2887. sd->st = st->bk_st;
  2888. sd->npc_id = st->bk_npcid;
  2889. if(st->bk_st)
  2890. {
  2891. //Remove tag for removal.
  2892. st->bk_st = NULL;
  2893. st->bk_npcid = 0;
  2894. }
  2895. else if(dequeue_event)
  2896. {
  2897. npc_event_dequeue(sd);
  2898. }
  2899. }
  2900. else if(st->bk_st)
  2901. {// rid was set to 0, before detaching the script state
  2902. 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);
  2903. script_reportsrc(st->bk_st);
  2904. script_free_state(st->bk_st);
  2905. st->bk_st = NULL;
  2906. }
  2907. }
  2908. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  2909. ///
  2910. /// @param st Script state to attach.
  2911. static void script_attach_state(struct script_state* st)
  2912. {
  2913. struct map_session_data* sd;
  2914. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  2915. {
  2916. if(st!=sd->st)
  2917. {
  2918. if(st->bk_st)
  2919. {// there is already a backup
  2920. 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);
  2921. }
  2922. st->bk_st = sd->st;
  2923. st->bk_npcid = sd->npc_id;
  2924. }
  2925. sd->st = st;
  2926. sd->npc_id = st->oid;
  2927. }
  2928. }
  2929. /*==========================================
  2930. * スクリプトの実行メイン部分
  2931. *------------------------------------------*/
  2932. void run_script_main(struct script_state *st)
  2933. {
  2934. int cmdcount=script_config.check_cmdcount;
  2935. int gotocount=script_config.check_gotocount;
  2936. TBL_PC *sd;
  2937. struct script_stack *stack=st->stack;
  2938. struct npc_data *nd;
  2939. script_attach_state(st);
  2940. nd = map_id2nd(st->oid);
  2941. if( nd && map[nd->bl.m].instance_id > 0 )
  2942. st->instance_id = map[nd->bl.m].instance_id;
  2943. if(st->state == RERUNLINE) {
  2944. run_func(st);
  2945. if(st->state == GOTO)
  2946. st->state = RUN;
  2947. } else if(st->state != END)
  2948. st->state = RUN;
  2949. while(st->state == RUN)
  2950. {
  2951. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2952. switch(c){
  2953. case C_EOL:
  2954. if( stack->defsp > stack->sp )
  2955. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  2956. else
  2957. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  2958. break;
  2959. case C_INT:
  2960. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2961. break;
  2962. case C_POS:
  2963. case C_NAME:
  2964. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2965. st->pos+=3;
  2966. break;
  2967. case C_ARG:
  2968. push_val(stack,c,0);
  2969. break;
  2970. case C_STR:
  2971. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2972. while(st->script->script_buf[st->pos++]);
  2973. break;
  2974. case C_FUNC:
  2975. run_func(st);
  2976. if(st->state==GOTO){
  2977. st->state = RUN;
  2978. if( gotocount>0 && (--gotocount)<=0 ){
  2979. ShowError("run_script: infinity loop !\n");
  2980. script_reportsrc(st);
  2981. st->state=END;
  2982. }
  2983. }
  2984. break;
  2985. case C_NEG:
  2986. case C_NOT:
  2987. case C_LNOT:
  2988. op_1(st ,c);
  2989. break;
  2990. case C_ADD:
  2991. case C_SUB:
  2992. case C_MUL:
  2993. case C_DIV:
  2994. case C_MOD:
  2995. case C_EQ:
  2996. case C_NE:
  2997. case C_GT:
  2998. case C_GE:
  2999. case C_LT:
  3000. case C_LE:
  3001. case C_AND:
  3002. case C_OR:
  3003. case C_XOR:
  3004. case C_LAND:
  3005. case C_LOR:
  3006. case C_R_SHIFT:
  3007. case C_L_SHIFT:
  3008. op_2(st, c);
  3009. break;
  3010. case C_OP3:
  3011. op_3(st, c);
  3012. break;
  3013. case C_NOP:
  3014. st->state=END;
  3015. break;
  3016. default:
  3017. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3018. st->state=END;
  3019. break;
  3020. }
  3021. if( cmdcount>0 && (--cmdcount)<=0 ){
  3022. ShowError("run_script: infinity loop !\n");
  3023. script_reportsrc(st);
  3024. st->state=END;
  3025. }
  3026. }
  3027. if(st->sleep.tick > 0) {
  3028. //Restore previous script
  3029. script_detach_state(st, false);
  3030. //Delay execution
  3031. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3032. st->sleep.charid = sd?sd->status.char_id:0;
  3033. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3034. run_script_timer, st->sleep.charid, (intptr)st);
  3035. linkdb_insert(&sleep_db, (void*)st->oid, st);
  3036. }
  3037. else if(st->state != END && st->rid){
  3038. //Resume later (st is already attached to player).
  3039. if(st->bk_st) {
  3040. ShowWarning("Unable to restore stack! Double continuation!\n");
  3041. //Report BOTH scripts to see if that can help somehow.
  3042. ShowDebug("Previous script (lost):\n");
  3043. script_reportsrc(st->bk_st);
  3044. ShowDebug("Current script:\n");
  3045. script_reportsrc(st);
  3046. script_free_state(st->bk_st);
  3047. st->bk_st = NULL;
  3048. }
  3049. } else {
  3050. //Dispose of script.
  3051. if ((sd = map_id2sd(st->rid))!=NULL)
  3052. { //Restore previous stack and save char.
  3053. if(sd->state.using_fake_npc){
  3054. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  3055. sd->state.using_fake_npc = 0;
  3056. }
  3057. //Restore previous script if any.
  3058. script_detach_state(st, true);
  3059. if (sd->state.reg_dirty&2)
  3060. intif_saveregistry(sd,2);
  3061. if (sd->state.reg_dirty&1)
  3062. intif_saveregistry(sd,1);
  3063. }
  3064. script_free_state(st);
  3065. st = NULL;
  3066. }
  3067. }
  3068. int script_config_read(char *cfgName)
  3069. {
  3070. int i;
  3071. char line[1024],w1[1024],w2[1024];
  3072. FILE *fp;
  3073. fp=fopen(cfgName,"r");
  3074. if(fp==NULL){
  3075. ShowError("file not found: [%s]\n", cfgName);
  3076. return 1;
  3077. }
  3078. while(fgets(line, sizeof(line), fp))
  3079. {
  3080. if(line[0] == '/' && line[1] == '/')
  3081. continue;
  3082. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3083. if(i!=2)
  3084. continue;
  3085. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3086. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3087. }
  3088. else if(strcmpi(w1,"check_cmdcount")==0) {
  3089. script_config.check_cmdcount = config_switch(w2);
  3090. }
  3091. else if(strcmpi(w1,"check_gotocount")==0) {
  3092. script_config.check_gotocount = config_switch(w2);
  3093. }
  3094. else if(strcmpi(w1,"input_min_value")==0) {
  3095. script_config.input_min_value = config_switch(w2);
  3096. }
  3097. else if(strcmpi(w1,"input_max_value")==0) {
  3098. script_config.input_max_value = config_switch(w2);
  3099. }
  3100. else if(strcmpi(w1,"import")==0){
  3101. script_config_read(w2);
  3102. }
  3103. }
  3104. fclose(fp);
  3105. return 0;
  3106. }
  3107. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3108. {
  3109. struct script_code *code = (struct script_code *)data;
  3110. if(code){
  3111. script_free_vars( &code->script_vars );
  3112. aFree( code->script_buf );
  3113. aFree( code );
  3114. }
  3115. return 0;
  3116. }
  3117. static int do_final_autobonus_sub (DBKey key,void *data,va_list ap)
  3118. {
  3119. struct script_code *script = (struct script_code *)data;
  3120. if( script )
  3121. script_free_code(script);
  3122. return 0;
  3123. }
  3124. void script_run_autobonus(const char *autobonus, int id, int pos)
  3125. {
  3126. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3127. if( script )
  3128. {
  3129. current_equip_item_index = pos;
  3130. run_script(script,0,id,0);
  3131. }
  3132. }
  3133. void script_add_autobonus(const char *autobonus)
  3134. {
  3135. if( strdb_get(autobonus_db, autobonus) == NULL )
  3136. {
  3137. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3138. if( script )
  3139. strdb_put(autobonus_db, autobonus, script);
  3140. }
  3141. }
  3142. /*==========================================
  3143. * 終了
  3144. *------------------------------------------*/
  3145. int do_final_script()
  3146. {
  3147. #ifdef DEBUG_HASH
  3148. if (battle_config.etc_log)
  3149. {
  3150. FILE *fp = fopen("hash_dump.txt","wt");
  3151. if(fp) {
  3152. int i,count[SCRIPT_HASH_SIZE];
  3153. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3154. int n=0;
  3155. int min=INT_MAX,max=0,zero=0;
  3156. double mean=0.0f;
  3157. double median=0.0f;
  3158. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3159. memset(count, 0, sizeof(count));
  3160. fprintf(fp,"num : hash : data_name\n");
  3161. fprintf(fp,"---------------------------------------------------------------\n");
  3162. for(i=LABEL_START; i<str_num; i++) {
  3163. unsigned int h = calc_hash(get_str(i));
  3164. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3165. ++count[h];
  3166. }
  3167. fprintf(fp,"--------------------\n\n");
  3168. memset(count2, 0, sizeof(count2));
  3169. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3170. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3171. if(min > count[i])
  3172. min = count[i]; // minimun count of collision
  3173. if(max < count[i])
  3174. max = count[i]; // maximun count of collision
  3175. if(count[i] == 0)
  3176. zero++;
  3177. ++count2[count[i]];
  3178. }
  3179. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3180. for( i=min; i <= max; ++i ){
  3181. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3182. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3183. }
  3184. for( i=min; i <= max; ++i ){
  3185. n += count2[i];
  3186. if( n*2 >= SCRIPT_HASH_SIZE )
  3187. {
  3188. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3189. median = (i+i+1)/2.0f;
  3190. else
  3191. median = i;
  3192. break;
  3193. }
  3194. }
  3195. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3196. fclose(fp);
  3197. }
  3198. }
  3199. #endif
  3200. mapreg_final();
  3201. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3202. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3203. autobonus_db->destroy(autobonus_db, do_final_autobonus_sub);
  3204. if(sleep_db) {
  3205. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3206. while(n) {
  3207. struct script_state *st = (struct script_state *)n->data;
  3208. script_free_state(st);
  3209. n = n->next;
  3210. }
  3211. linkdb_final(&sleep_db);
  3212. }
  3213. if (str_data)
  3214. aFree(str_data);
  3215. if (str_buf)
  3216. aFree(str_buf);
  3217. return 0;
  3218. }
  3219. /*==========================================
  3220. * 初期化
  3221. *------------------------------------------*/
  3222. int do_init_script()
  3223. {
  3224. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3225. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3226. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3227. mapreg_init();
  3228. return 0;
  3229. }
  3230. int script_reload()
  3231. {
  3232. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3233. scriptlabel_db->clear(scriptlabel_db, NULL);
  3234. if(sleep_db) {
  3235. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3236. while(n) {
  3237. struct script_state *st = (struct script_state *)n->data;
  3238. script_free_state(st);
  3239. n = n->next;
  3240. }
  3241. linkdb_final(&sleep_db);
  3242. }
  3243. mapreg_reload();
  3244. return 0;
  3245. }
  3246. //-----------------------------------------------------------------------------
  3247. // buildin functions
  3248. //
  3249. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3250. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3251. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3252. /////////////////////////////////////////////////////////////////////
  3253. // NPC interaction
  3254. //
  3255. /// Appends a message to the npc dialog.
  3256. /// If a dialog doesn't exist yet, one is created.
  3257. ///
  3258. /// mes "<message>";
  3259. BUILDIN_FUNC(mes)
  3260. {
  3261. TBL_PC* sd = script_rid2sd(st);
  3262. if( sd == NULL )
  3263. return 0;
  3264. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3265. return 0;
  3266. }
  3267. /// Displays the button 'next' in the npc dialog.
  3268. /// The dialog text is cleared and the script continues when the button is pressed.
  3269. ///
  3270. /// next;
  3271. BUILDIN_FUNC(next)
  3272. {
  3273. TBL_PC* sd;
  3274. sd = script_rid2sd(st);
  3275. if( sd == NULL )
  3276. return 0;
  3277. st->state = STOP;
  3278. clif_scriptnext(sd, st->oid);
  3279. return 0;
  3280. }
  3281. /// Ends the script and displays the button 'close' on the npc dialog.
  3282. /// The dialog is closed when the button is pressed.
  3283. ///
  3284. /// close;
  3285. BUILDIN_FUNC(close)
  3286. {
  3287. TBL_PC* sd;
  3288. sd = script_rid2sd(st);
  3289. if( sd == NULL )
  3290. return 0;
  3291. st->state = END;
  3292. clif_scriptclose(sd, st->oid);
  3293. return 0;
  3294. }
  3295. /// Displays the button 'close' on the npc dialog.
  3296. /// The dialog is closed and the script continues when the button is pressed.
  3297. ///
  3298. /// close2;
  3299. BUILDIN_FUNC(close2)
  3300. {
  3301. TBL_PC* sd;
  3302. sd = script_rid2sd(st);
  3303. if( sd == NULL )
  3304. return 0;
  3305. st->state = STOP;
  3306. clif_scriptclose(sd, st->oid);
  3307. return 0;
  3308. }
  3309. /// Counts the number of valid and total number of options in 'str'
  3310. /// If max_count > 0 the counting stops when that valid option is reached
  3311. /// total is incremented for each option (NULL is supported)
  3312. static int menu_countoptions(const char* str, int max_count, int* total)
  3313. {
  3314. int count = 0;
  3315. int bogus_total;
  3316. if( total == NULL )
  3317. total = &bogus_total;
  3318. ++(*total);
  3319. // initial empty options
  3320. while( *str == ':' )
  3321. {
  3322. ++str;
  3323. ++(*total);
  3324. }
  3325. // count menu options
  3326. while( *str != '\0' )
  3327. {
  3328. ++count;
  3329. --max_count;
  3330. if( max_count == 0 )
  3331. break;
  3332. while( *str != ':' && *str != '\0' )
  3333. ++str;
  3334. while( *str == ':' )
  3335. {
  3336. ++str;
  3337. ++(*total);
  3338. }
  3339. }
  3340. return count;
  3341. }
  3342. /// Displays a menu with options and goes to the target label.
  3343. /// The script is stopped if cancel is pressed.
  3344. /// Options with no text are not displayed in the client.
  3345. ///
  3346. /// Options can be grouped together, separated by the character ':' in the text:
  3347. /// ex: menu "A:B:C",L_target;
  3348. /// All these options go to the specified target label.
  3349. ///
  3350. /// The index of the selected option is put in the variable @menu.
  3351. /// Indexes start with 1 and are consistent with grouped and empty options.
  3352. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3353. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3354. ///
  3355. /// NOTE: the client closes the npc dialog when cancel is pressed
  3356. ///
  3357. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3358. BUILDIN_FUNC(menu)
  3359. {
  3360. int i;
  3361. const char* text;
  3362. TBL_PC* sd;
  3363. sd = script_rid2sd(st);
  3364. if( sd == NULL )
  3365. return 0;
  3366. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3367. if( sd->state.menu_or_input == 0 )
  3368. {
  3369. struct StringBuf buf;
  3370. struct script_data* data;
  3371. if( script_lastdata(st) % 2 == 0 )
  3372. {// argument count is not even (1st argument is at index 2)
  3373. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3374. st->state = END;
  3375. return 1;
  3376. }
  3377. StringBuf_Init(&buf);
  3378. sd->npc_menu = 0;
  3379. for( i = 2; i < script_lastdata(st); i += 2 )
  3380. {
  3381. // menu options
  3382. text = script_getstr(st, i);
  3383. // target label
  3384. data = script_getdata(st, i+1);
  3385. if( !data_islabel(data) )
  3386. {// not a label
  3387. StringBuf_Destroy(&buf);
  3388. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3389. script_reportdata(data);
  3390. st->state = END;
  3391. return 1;
  3392. }
  3393. // append option(s)
  3394. if( text[0] == '\0' )
  3395. continue;// empty string, ignore
  3396. if( sd->npc_menu > 0 )
  3397. StringBuf_AppendStr(&buf, ":");
  3398. StringBuf_AppendStr(&buf, text);
  3399. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3400. }
  3401. st->state = RERUNLINE;
  3402. sd->state.menu_or_input = 1;
  3403. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3404. StringBuf_Destroy(&buf);
  3405. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3406. }
  3407. else if( sd->npc_menu == 0xff )
  3408. {// Cancel was pressed
  3409. sd->state.menu_or_input = 0;
  3410. st->state = END;
  3411. }
  3412. else
  3413. {// goto target label
  3414. int menu = 0;
  3415. sd->state.menu_or_input = 0;
  3416. if( sd->npc_menu <= 0 )
  3417. {
  3418. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3419. st->state = END;
  3420. return 1;
  3421. }
  3422. // get target label
  3423. for( i = 2; i < script_lastdata(st); i += 2 )
  3424. {
  3425. text = script_getstr(st, i);
  3426. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3427. if( sd->npc_menu <= 0 )
  3428. break;// entry found
  3429. }
  3430. if( sd->npc_menu > 0 )
  3431. {// Invalid selection
  3432. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3433. st->state = END;
  3434. return 1;
  3435. }
  3436. if( !data_islabel(script_getdata(st, i + 1)) )
  3437. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3438. ShowError("script:menu: unexpected data in label argument\n");
  3439. script_reportdata(script_getdata(st, i + 1));
  3440. st->state = END;
  3441. return 1;
  3442. }
  3443. pc_setreg(sd, add_str("@menu"), menu);
  3444. st->pos = script_getnum(st, i + 1);
  3445. st->state = GOTO;
  3446. }
  3447. return 0;
  3448. }
  3449. /// Displays a menu with options and returns the selected option.
  3450. /// Behaves like 'menu' without the target labels.
  3451. ///
  3452. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3453. ///
  3454. /// @see menu
  3455. BUILDIN_FUNC(select)
  3456. {
  3457. int i;
  3458. const char* text;
  3459. TBL_PC* sd;
  3460. sd = script_rid2sd(st);
  3461. if( sd == NULL )
  3462. return 0;
  3463. if( sd->state.menu_or_input == 0 )
  3464. {
  3465. struct StringBuf buf;
  3466. StringBuf_Init(&buf);
  3467. sd->npc_menu = 0;
  3468. for( i = 2; i <= script_lastdata(st); ++i )
  3469. {
  3470. text = script_getstr(st, i);
  3471. if( sd->npc_menu > 0 )
  3472. StringBuf_AppendStr(&buf, ":");
  3473. StringBuf_AppendStr(&buf, text);
  3474. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3475. }
  3476. st->state = RERUNLINE;
  3477. sd->state.menu_or_input = 1;
  3478. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3479. StringBuf_Destroy(&buf);
  3480. }
  3481. else if( sd->npc_menu == 0xff )
  3482. {// Cancel was pressed
  3483. sd->state.menu_or_input = 0;
  3484. st->state = END;
  3485. }
  3486. else
  3487. {// return selected option
  3488. int menu = 0;
  3489. sd->state.menu_or_input = 0;
  3490. for( i = 2; i <= script_lastdata(st); ++i )
  3491. {
  3492. text = script_getstr(st, i);
  3493. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3494. if( sd->npc_menu <= 0 )
  3495. break;// entry found
  3496. }
  3497. pc_setreg(sd, add_str("@menu"), menu);
  3498. script_pushint(st, menu);
  3499. st->state = RUN;
  3500. }
  3501. return 0;
  3502. }
  3503. /// Displays a menu with options and returns the selected option.
  3504. /// Behaves like 'menu' without the target labels, except when cancel is
  3505. /// pressed.
  3506. /// When cancel is pressed, the script continues and 255 is returned.
  3507. ///
  3508. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3509. ///
  3510. /// @see menu
  3511. BUILDIN_FUNC(prompt)
  3512. {
  3513. int i;
  3514. const char *text;
  3515. TBL_PC* sd;
  3516. sd = script_rid2sd(st);
  3517. if( sd == NULL )
  3518. return 0;
  3519. if( sd->state.menu_or_input == 0 )
  3520. {
  3521. struct StringBuf buf;
  3522. StringBuf_Init(&buf);
  3523. sd->npc_menu = 0;
  3524. for( i = 2; i <= script_lastdata(st); ++i )
  3525. {
  3526. text = script_getstr(st, i);
  3527. if( sd->npc_menu > 0 )
  3528. StringBuf_AppendStr(&buf, ":");
  3529. StringBuf_AppendStr(&buf, text);
  3530. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3531. }
  3532. st->state = RERUNLINE;
  3533. sd->state.menu_or_input = 1;
  3534. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3535. StringBuf_Destroy(&buf);
  3536. }
  3537. else if( sd->npc_menu == 0xff )
  3538. {// Cancel was pressed
  3539. sd->state.menu_or_input = 0;
  3540. pc_setreg(sd, add_str("@menu"), 0xff);
  3541. script_pushint(st, 0xff);
  3542. st->state = RUN;
  3543. }
  3544. else
  3545. {// return selected option
  3546. int menu = 0;
  3547. sd->state.menu_or_input = 0;
  3548. for( i = 2; i <= script_lastdata(st); ++i )
  3549. {
  3550. text = script_getstr(st, i);
  3551. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3552. if( sd->npc_menu <= 0 )
  3553. break;// entry found
  3554. }
  3555. pc_setreg(sd, add_str("@menu"), menu);
  3556. script_pushint(st, menu);
  3557. st->state = RUN;
  3558. }
  3559. return 0;
  3560. }
  3561. /////////////////////////////////////////////////////////////////////
  3562. // ...
  3563. //
  3564. /// Jumps to the target script label.
  3565. ///
  3566. /// goto <label>;
  3567. BUILDIN_FUNC(goto)
  3568. {
  3569. if( !data_islabel(script_getdata(st,2)) )
  3570. {
  3571. ShowError("script:goto: not a label\n");
  3572. script_reportdata(script_getdata(st,2));
  3573. st->state = END;
  3574. return 1;
  3575. }
  3576. st->pos = script_getnum(st,2);
  3577. st->state = GOTO;
  3578. return 0;
  3579. }
  3580. /*==========================================
  3581. * user-defined function call
  3582. *------------------------------------------*/
  3583. BUILDIN_FUNC(callfunc)
  3584. {
  3585. int i, j;
  3586. struct script_retinfo* ri;
  3587. struct script_code* scr;
  3588. const char* str = script_getstr(st,2);
  3589. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3590. if( !scr )
  3591. {
  3592. ShowError("script:callfunc: function not found! [%s]\n", str);
  3593. st->state = END;
  3594. return 1;
  3595. }
  3596. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3597. {
  3598. struct script_data* data = push_copy(st->stack,i);
  3599. if( data_isreference(data) && !data->ref )
  3600. {
  3601. const char* name = reference_getname(data);
  3602. if( name[0] == '.' && name[1] == '@' )
  3603. data->ref = st->stack->var_function;
  3604. else if( name[0] == '.' )
  3605. data->ref = &st->script->script_vars;
  3606. }
  3607. }
  3608. CREATE(ri, struct script_retinfo, 1);
  3609. ri->script = st->script;// script code
  3610. ri->var_function = st->stack->var_function;// scope variables
  3611. ri->pos = st->pos;// script location
  3612. ri->nargs = j;// argument count
  3613. ri->defsp = st->stack->defsp;// default stack pointer
  3614. push_retinfo(st->stack, ri);
  3615. st->pos = 0;
  3616. st->script = scr;
  3617. st->stack->defsp = st->stack->sp;
  3618. st->state = GOTO;
  3619. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3620. return 0;
  3621. }
  3622. /*==========================================
  3623. * subroutine call
  3624. *------------------------------------------*/
  3625. BUILDIN_FUNC(callsub)
  3626. {
  3627. int i,j;
  3628. struct script_retinfo* ri;
  3629. int pos = script_getnum(st,2);
  3630. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3631. {
  3632. ShowError("script:callsub: argument is not a label\n");
  3633. script_reportdata(script_getdata(st,2));
  3634. st->state = END;
  3635. return 1;
  3636. }
  3637. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3638. {
  3639. struct script_data* data = push_copy(st->stack,i);
  3640. if( data_isreference(data) && !data->ref )
  3641. {
  3642. const char* name = reference_getname(data);
  3643. if( name[0] == '.' && name[1] == '@' )
  3644. data->ref = st->stack->var_function;
  3645. }
  3646. }
  3647. CREATE(ri, struct script_retinfo, 1);
  3648. ri->script = st->script;// script code
  3649. ri->var_function = st->stack->var_function;// scope variables
  3650. ri->pos = st->pos;// script location
  3651. ri->nargs = j;// argument count
  3652. ri->defsp = st->stack->defsp;// default stack pointer
  3653. push_retinfo(st->stack, ri);
  3654. st->pos = pos;
  3655. st->stack->defsp = st->stack->sp;
  3656. st->state = GOTO;
  3657. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3658. return 0;
  3659. }
  3660. /// Retrieves an argument provided to callfunc/callsub.
  3661. /// If the argument doesn't exist
  3662. ///
  3663. /// getarg(<index>{,<default_value>}) -> <value>
  3664. BUILDIN_FUNC(getarg)
  3665. {
  3666. struct script_retinfo* ri;
  3667. int idx;
  3668. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3669. {
  3670. ShowError("script:getarg: no callfunc or callsub!\n");
  3671. st->state = END;
  3672. return 1;
  3673. }
  3674. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3675. idx = script_getnum(st,2);
  3676. if( idx >= 0 && idx < ri->nargs )
  3677. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3678. else if( script_hasdata(st,3) )
  3679. script_pushcopy(st, 3);
  3680. else
  3681. {
  3682. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3683. st->state = END;
  3684. return 1;
  3685. }
  3686. return 0;
  3687. }
  3688. /// Returns from the current function, optionaly returning a value from the functions.
  3689. /// Don't use outside script functions.
  3690. ///
  3691. /// return;
  3692. /// return <value>;
  3693. BUILDIN_FUNC(return)
  3694. {
  3695. if( script_hasdata(st,2) )
  3696. {// return value
  3697. struct script_data* data;
  3698. script_pushcopy(st, 2);
  3699. data = script_getdatatop(st, -1);
  3700. if( data_isreference(data) )
  3701. {
  3702. const char* name = reference_getname(data);
  3703. if( name[0] == '.' && name[1] == '@' )
  3704. {// scope variable
  3705. if( !data->ref || data->ref == st->stack->var_function )
  3706. get_val(st, data);// current scope, convert to value
  3707. }
  3708. else if( name[0] == '.' && !data->ref )
  3709. {// script variable, link to current script
  3710. data->ref = &st->script->script_vars;
  3711. }
  3712. }
  3713. }
  3714. else
  3715. {// no return value
  3716. script_pushnil(st);
  3717. }
  3718. st->state = RETFUNC;
  3719. return 0;
  3720. }
  3721. /// Returns a random number from 0 to <range>-1.
  3722. /// Or returns a random number from <min> to <max>.
  3723. /// If <min> is greater than <max>, their numbers are switched.
  3724. /// rand(<range>) -> <int>
  3725. /// rand(<min>,<max>) -> <int>
  3726. BUILDIN_FUNC(rand)
  3727. {
  3728. int range;
  3729. int min;
  3730. int max;
  3731. if( script_hasdata(st,3) )
  3732. {// min,max
  3733. min = script_getnum(st,2);
  3734. max = script_getnum(st,3);
  3735. if( max < min )
  3736. swap(min, max);
  3737. range = max - min + 1;
  3738. }
  3739. else
  3740. {// range
  3741. min = 0;
  3742. range = script_getnum(st,2);
  3743. }
  3744. if( range <= 1 )
  3745. script_pushint(st, min);
  3746. else
  3747. script_pushint(st, rand()%range + min);
  3748. return 0;
  3749. }
  3750. /*==========================================
  3751. *
  3752. *------------------------------------------*/
  3753. BUILDIN_FUNC(warp)
  3754. {
  3755. int ret;
  3756. int x,y;
  3757. const char* str;
  3758. TBL_PC* sd;
  3759. sd = script_rid2sd(st);
  3760. if( sd == NULL )
  3761. return 0;
  3762. str = script_getstr(st,2);
  3763. x = script_getnum(st,3);
  3764. y = script_getnum(st,4);
  3765. if(strcmp(str,"Random")==0)
  3766. ret = pc_randomwarp(sd,3);
  3767. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3768. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3769. else
  3770. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3771. if( ret ) {
  3772. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3773. script_reportsrc(st);
  3774. }
  3775. return 0;
  3776. }
  3777. /*==========================================
  3778. * エリア指定ワープ
  3779. *------------------------------------------*/
  3780. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3781. {
  3782. int x,y;
  3783. unsigned int map;
  3784. map=va_arg(ap, unsigned int);
  3785. x=va_arg(ap,int);
  3786. y=va_arg(ap,int);
  3787. if(map == 0)
  3788. pc_randomwarp((TBL_PC *)bl,3);
  3789. else
  3790. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3791. return 0;
  3792. }
  3793. BUILDIN_FUNC(areawarp)
  3794. {
  3795. int x,y,m;
  3796. unsigned int index;
  3797. const char *str;
  3798. const char *mapname;
  3799. int x0,y0,x1,y1;
  3800. mapname=script_getstr(st,2);
  3801. x0=script_getnum(st,3);
  3802. y0=script_getnum(st,4);
  3803. x1=script_getnum(st,5);
  3804. y1=script_getnum(st,6);
  3805. str=script_getstr(st,7);
  3806. x=script_getnum(st,8);
  3807. y=script_getnum(st,9);
  3808. if( (m=map_mapname2mapid(mapname))< 0)
  3809. return 0;
  3810. if(strcmp(str,"Random")==0)
  3811. index = 0;
  3812. else if(!(index=mapindex_name2id(str)))
  3813. return 0;
  3814. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3815. return 0;
  3816. }
  3817. /*==========================================
  3818. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  3819. *------------------------------------------*/
  3820. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  3821. {
  3822. int hp, sp;
  3823. hp = va_arg(ap, int);
  3824. sp = va_arg(ap, int);
  3825. pc_percentheal((TBL_PC *)bl,hp,sp);
  3826. return 0;
  3827. }
  3828. BUILDIN_FUNC(areapercentheal)
  3829. {
  3830. int hp,sp,m;
  3831. const char *mapname;
  3832. int x0,y0,x1,y1;
  3833. mapname=script_getstr(st,2);
  3834. x0=script_getnum(st,3);
  3835. y0=script_getnum(st,4);
  3836. x1=script_getnum(st,5);
  3837. y1=script_getnum(st,6);
  3838. hp=script_getnum(st,7);
  3839. sp=script_getnum(st,8);
  3840. if( (m=map_mapname2mapid(mapname))< 0)
  3841. return 0;
  3842. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  3843. return 0;
  3844. }
  3845. /*==========================================
  3846. * warpchar [LuzZza]
  3847. * Useful for warp one player from
  3848. * another player npc-session.
  3849. * Using: warpchar "mapname",x,y,Char_ID;
  3850. *------------------------------------------*/
  3851. BUILDIN_FUNC(warpchar)
  3852. {
  3853. int x,y,a;
  3854. const char *str;
  3855. TBL_PC *sd;
  3856. str=script_getstr(st,2);
  3857. x=script_getnum(st,3);
  3858. y=script_getnum(st,4);
  3859. a=script_getnum(st,5);
  3860. sd = map_charid2sd(a);
  3861. if( sd == NULL )
  3862. return 0;
  3863. if(strcmp(str, "Random") == 0)
  3864. pc_randomwarp(sd, 3);
  3865. else
  3866. if(strcmp(str, "SavePoint") == 0)
  3867. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3868. else
  3869. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3870. return 0;
  3871. }
  3872. /*==========================================
  3873. * Warpparty - [Fredzilla] [Paradox924X]
  3874. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  3875. * If 'from_mapname' is specified, only the party members on that map will be warped
  3876. *------------------------------------------*/
  3877. BUILDIN_FUNC(warpparty)
  3878. {
  3879. TBL_PC *sd;
  3880. TBL_PC *pl_sd;
  3881. struct party_data* p;
  3882. int type;
  3883. int mapindex;
  3884. int i, j;
  3885. const char* str = script_getstr(st,2);
  3886. int x = script_getnum(st,3);
  3887. int y = script_getnum(st,4);
  3888. int p_id = script_getnum(st,5);
  3889. const char* str2 = NULL;
  3890. if ( script_hasdata(st,6) )
  3891. str2 = script_getstr(st,6);
  3892. sd=script_rid2sd(st);
  3893. if( sd == NULL )
  3894. return 0;
  3895. p = party_search(p_id);
  3896. if(!p)
  3897. return 0;
  3898. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3899. return 0;
  3900. type = ( strcmp(str,"Random")==0 ) ? 0
  3901. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3902. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3903. : ( strcmp(str,"Leader")==0 ) ? 3
  3904. : 4;
  3905. for (i = 0; i < MAX_PARTY; i++)
  3906. {
  3907. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  3908. continue;
  3909. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  3910. continue;
  3911. if( pc_isdead(pl_sd) )
  3912. continue;
  3913. switch( type )
  3914. {
  3915. case 0: // Random
  3916. if(!map[pl_sd->bl.m].flag.nowarp)
  3917. pc_randomwarp(pl_sd,3);
  3918. break;
  3919. case 1: // SavePointAll
  3920. if(!map[pl_sd->bl.m].flag.noreturn)
  3921. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3922. break;
  3923. case 2: // SavePoint
  3924. if(!map[pl_sd->bl.m].flag.noreturn)
  3925. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3926. break;
  3927. case 3: // Leader
  3928. for(j = 0; j < MAX_PARTY && !p->party.member[j].leader; j++);
  3929. if (j == MAX_PARTY || !p->data[j].sd) //Leader not found / not online
  3930. return 0;
  3931. mapindex = p->data[j].sd->mapindex;
  3932. x = p->data[j].sd->bl.x;
  3933. y = p->data[j].sd->bl.y;
  3934. for (j = 0; j < MAX_PARTY; j++)
  3935. {
  3936. pl_sd = p->data[j].sd;
  3937. if (!pl_sd)
  3938. continue;
  3939. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3940. continue;
  3941. pc_setpos(pl_sd,mapindex,x,y,3);
  3942. }
  3943. break;
  3944. case 4: // m,x,y
  3945. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  3946. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  3947. break;
  3948. }
  3949. }
  3950. return 0;
  3951. }
  3952. /*==========================================
  3953. * Warpguild - [Fredzilla]
  3954. * Syntax: warpguild "mapname",x,y,Guild_ID;
  3955. *------------------------------------------*/
  3956. BUILDIN_FUNC(warpguild)
  3957. {
  3958. TBL_PC *sd;
  3959. TBL_PC *pl_sd;
  3960. struct guild* g;
  3961. struct s_mapiterator* iter;
  3962. int type;
  3963. const char* str = script_getstr(st,2);
  3964. int x = script_getnum(st,3);
  3965. int y = script_getnum(st,4);
  3966. int gid = script_getnum(st,5);
  3967. sd=script_rid2sd(st);
  3968. if( sd == NULL )
  3969. return 0;
  3970. g = guild_search(gid);
  3971. if( g == NULL )
  3972. return 0;
  3973. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3974. return 0;
  3975. type = ( strcmp(str,"Random")==0 ) ? 0
  3976. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3977. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3978. : 3;
  3979. iter = mapit_getallusers();
  3980. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  3981. {
  3982. if( pl_sd->status.guild_id != gid )
  3983. continue;
  3984. switch( type )
  3985. {
  3986. case 0: // Random
  3987. if(!map[pl_sd->bl.m].flag.nowarp)
  3988. pc_randomwarp(pl_sd,3);
  3989. break;
  3990. case 1: // SavePointAll
  3991. if(!map[pl_sd->bl.m].flag.noreturn)
  3992. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3993. break;
  3994. case 2: // SavePoint
  3995. if(!map[pl_sd->bl.m].flag.noreturn)
  3996. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3997. break;
  3998. case 3: // m,x,y
  3999. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4000. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  4001. break;
  4002. }
  4003. }
  4004. mapit_free(iter);
  4005. return 0;
  4006. }
  4007. /*==========================================
  4008. *
  4009. *------------------------------------------*/
  4010. BUILDIN_FUNC(heal)
  4011. {
  4012. TBL_PC *sd;
  4013. int hp,sp;
  4014. sd = script_rid2sd(st);
  4015. if (!sd) return 0;
  4016. hp=script_getnum(st,2);
  4017. sp=script_getnum(st,3);
  4018. status_heal(&sd->bl, hp, sp, 1);
  4019. return 0;
  4020. }
  4021. /*==========================================
  4022. *
  4023. *------------------------------------------*/
  4024. BUILDIN_FUNC(itemheal)
  4025. {
  4026. TBL_PC *sd;
  4027. int hp,sp;
  4028. hp=script_getnum(st,2);
  4029. sp=script_getnum(st,3);
  4030. if(potion_flag==1) {
  4031. potion_hp = hp;
  4032. potion_sp = sp;
  4033. return 0;
  4034. }
  4035. sd = script_rid2sd(st);
  4036. if (!sd) return 0;
  4037. pc_itemheal(sd,sd->itemid,hp,sp);
  4038. return 0;
  4039. }
  4040. /*==========================================
  4041. *
  4042. *------------------------------------------*/
  4043. BUILDIN_FUNC(percentheal)
  4044. {
  4045. int hp,sp;
  4046. TBL_PC* sd;
  4047. hp=script_getnum(st,2);
  4048. sp=script_getnum(st,3);
  4049. if(potion_flag==1) {
  4050. potion_per_hp = hp;
  4051. potion_per_sp = sp;
  4052. return 0;
  4053. }
  4054. sd = script_rid2sd(st);
  4055. if( sd == NULL )
  4056. return 0;
  4057. pc_percentheal(sd,hp,sp);
  4058. return 0;
  4059. }
  4060. /*==========================================
  4061. *
  4062. *------------------------------------------*/
  4063. BUILDIN_FUNC(jobchange)
  4064. {
  4065. int job, upper=-1;
  4066. job=script_getnum(st,2);
  4067. if( script_hasdata(st,3) )
  4068. upper=script_getnum(st,3);
  4069. if (pcdb_checkid(job))
  4070. {
  4071. TBL_PC* sd;
  4072. sd = script_rid2sd(st);
  4073. if( sd == NULL )
  4074. return 0;
  4075. pc_jobchange(sd, job, upper);
  4076. }
  4077. return 0;
  4078. }
  4079. /*==========================================
  4080. *
  4081. *------------------------------------------*/
  4082. BUILDIN_FUNC(jobname)
  4083. {
  4084. int class_=script_getnum(st,2);
  4085. script_pushconststr(st,job_name(class_));
  4086. return 0;
  4087. }
  4088. /// Get input from the player.
  4089. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4090. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4091. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4092. /// shorter than 'min' and 0 otherwise.
  4093. ///
  4094. /// input(<var>{,<min>{,<max>}}) -> <int>
  4095. BUILDIN_FUNC(input)
  4096. {
  4097. TBL_PC* sd;
  4098. struct script_data* data;
  4099. int uid;
  4100. const char* name;
  4101. int min;
  4102. int max;
  4103. sd = script_rid2sd(st);
  4104. if( sd == NULL )
  4105. return 0;
  4106. data = script_getdata(st,2);
  4107. if( !data_isreference(data) ){
  4108. ShowError("script:input: not a variable\n");
  4109. script_reportdata(data);
  4110. st->state = END;
  4111. return 1;
  4112. }
  4113. uid = reference_getuid(data);
  4114. name = reference_getname(data);
  4115. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4116. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4117. if( !sd->state.menu_or_input )
  4118. { // first invocation, display npc input box
  4119. sd->state.menu_or_input = 1;
  4120. st->state = RERUNLINE;
  4121. if( is_string_variable(name) )
  4122. clif_scriptinputstr(sd,st->oid);
  4123. else
  4124. clif_scriptinput(sd,st->oid);
  4125. }
  4126. else
  4127. { // take received text/value and store it in the designated variable
  4128. sd->state.menu_or_input = 0;
  4129. if( is_string_variable(name) )
  4130. {
  4131. int len = (int)strlen(sd->npc_str);
  4132. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4133. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4134. }
  4135. else
  4136. {
  4137. int amount = sd->npc_amount;
  4138. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4139. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4140. }
  4141. st->state = RUN;
  4142. }
  4143. return 0;
  4144. }
  4145. /// Sets the value of a variable.
  4146. /// The value is converted to the type of the variable.
  4147. ///
  4148. /// set(<variable>,<value>) -> <variable>
  4149. BUILDIN_FUNC(set)
  4150. {
  4151. TBL_PC* sd = NULL;
  4152. struct script_data* data;
  4153. int num;
  4154. const char* name;
  4155. char prefix;
  4156. data = script_getdata(st,2);
  4157. if( !data_isreference(data) )
  4158. {
  4159. ShowError("script:set: not a variable\n");
  4160. script_reportdata(script_getdata(st,2));
  4161. st->state = END;
  4162. return 1;
  4163. }
  4164. num = reference_getuid(data);
  4165. name = reference_getname(data);
  4166. prefix = *name;
  4167. if( not_server_variable(prefix) )
  4168. {
  4169. sd = script_rid2sd(st);
  4170. if( sd == NULL )
  4171. {
  4172. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4173. return 0;
  4174. }
  4175. }
  4176. if( is_string_variable(name) )
  4177. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4178. else
  4179. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4180. // return a copy of the variable reference
  4181. script_pushcopy(st,2);
  4182. return 0;
  4183. }
  4184. /////////////////////////////////////////////////////////////////////
  4185. /// Array variables
  4186. ///
  4187. /// Returns the size of the specified array
  4188. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4189. {
  4190. int32 ret = idx;
  4191. if( isstring )
  4192. {
  4193. for( ; idx < 128; ++idx )
  4194. {
  4195. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4196. if( str && *str )
  4197. ret = idx + 1;
  4198. script_removetop(st, -1, 0);
  4199. }
  4200. }
  4201. else
  4202. {
  4203. for( ; idx < 128; ++idx )
  4204. {
  4205. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4206. if( num )
  4207. ret = idx + 1;
  4208. script_removetop(st, -1, 0);
  4209. }
  4210. }
  4211. return ret;
  4212. }
  4213. /// Sets values of an array, from the starting index.
  4214. /// ex: setarray arr[1],1,2,3;
  4215. ///
  4216. /// setarray <array variable>,<value1>{,<value2>...};
  4217. BUILDIN_FUNC(setarray)
  4218. {
  4219. struct script_data* data;
  4220. const char* name;
  4221. int32 start;
  4222. int32 end;
  4223. int32 id;
  4224. int32 i;
  4225. TBL_PC* sd = NULL;
  4226. data = script_getdata(st, 2);
  4227. if( !data_isreference(data) )
  4228. {
  4229. ShowError("script:setarray: not a variable\n");
  4230. script_reportdata(data);
  4231. st->state = END;
  4232. return 1;// not a variable
  4233. }
  4234. id = reference_getid(data);
  4235. start = reference_getindex(data);
  4236. name = reference_getname(data);
  4237. if( not_array_variable(*name) )
  4238. {
  4239. ShowError("script:setarray: illegal scope\n");
  4240. script_reportdata(data);
  4241. st->state = END;
  4242. return 1;// not supported
  4243. }
  4244. if( not_server_variable(*name) )
  4245. {
  4246. sd = script_rid2sd(st);
  4247. if( sd == NULL )
  4248. return 0;// no player attached
  4249. }
  4250. end = start + script_lastdata(st) - 2;
  4251. if( end > 127 )
  4252. end = 127;
  4253. if( is_string_variable(name) )
  4254. {// string array
  4255. for( i = 3; start < end; ++start, ++i )
  4256. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4257. }
  4258. else
  4259. {// int array
  4260. for( i = 3; start < end; ++start, ++i )
  4261. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4262. }
  4263. return 0;
  4264. }
  4265. /// Sets count values of an array, from the starting index.
  4266. /// ex: cleararray arr[0],0,1;
  4267. ///
  4268. /// cleararray <array variable>,<value>,<count>;
  4269. BUILDIN_FUNC(cleararray)
  4270. {
  4271. struct script_data* data;
  4272. const char* name;
  4273. int32 start;
  4274. int32 end;
  4275. int32 id;
  4276. void* v;
  4277. TBL_PC* sd = NULL;
  4278. data = script_getdata(st, 2);
  4279. if( !data_isreference(data) )
  4280. {
  4281. ShowError("script:cleararray: not a variable\n");
  4282. script_reportdata(data);
  4283. st->state = END;
  4284. return 1;// not a variable
  4285. }
  4286. id = reference_getid(data);
  4287. start = reference_getindex(data);
  4288. name = reference_getname(data);
  4289. if( not_array_variable(*name) )
  4290. {
  4291. ShowError("script:cleararray: illegal scope\n");
  4292. script_reportdata(data);
  4293. st->state = END;
  4294. return 1;// not supported
  4295. }
  4296. if( not_server_variable(*name) )
  4297. {
  4298. sd = script_rid2sd(st);
  4299. if( sd == NULL )
  4300. return 0;// no player attached
  4301. }
  4302. if( is_string_variable(name) )
  4303. v = (void*)script_getstr(st, 3);
  4304. else
  4305. v = (void*)script_getnum(st, 3);
  4306. end = start + script_getnum(st, 4);
  4307. if( end > 127 )
  4308. end = 127;
  4309. for( ; start <= end; ++start )
  4310. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4311. return 0;
  4312. }
  4313. /// Copies data from one array to another.
  4314. /// ex: copyarray arr[0],arr[2],2;
  4315. ///
  4316. /// copyarray <destination array variable>,<source array variable>,<count>;
  4317. BUILDIN_FUNC(copyarray)
  4318. {
  4319. struct script_data* data1;
  4320. struct script_data* data2;
  4321. const char* name1;
  4322. const char* name2;
  4323. int32 idx1;
  4324. int32 idx2;
  4325. int32 id1;
  4326. int32 id2;
  4327. void* v;
  4328. int32 i;
  4329. int32 count;
  4330. TBL_PC* sd = NULL;
  4331. data1 = script_getdata(st, 2);
  4332. data2 = script_getdata(st, 3);
  4333. if( !data_isreference(data1) || !data_isreference(data2) )
  4334. {
  4335. ShowError("script:copyarray: not a variable\n");
  4336. script_reportdata(data1);
  4337. script_reportdata(data2);
  4338. st->state = END;
  4339. return 1;// not a variable
  4340. }
  4341. id1 = reference_getid(data1);
  4342. id2 = reference_getid(data2);
  4343. idx1 = reference_getindex(data1);
  4344. idx2 = reference_getindex(data2);
  4345. name1 = reference_getname(data1);
  4346. name2 = reference_getname(data2);
  4347. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4348. {
  4349. ShowError("script:copyarray: illegal scope\n");
  4350. script_reportdata(data1);
  4351. script_reportdata(data2);
  4352. st->state = END;
  4353. return 1;// not supported
  4354. }
  4355. if( is_string_variable(name1) != is_string_variable(name2) )
  4356. {
  4357. ShowError("script:copyarray: type mismatch\n");
  4358. script_reportdata(data1);
  4359. script_reportdata(data2);
  4360. st->state = END;
  4361. return 1;// data type mismatch
  4362. }
  4363. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4364. {
  4365. sd = script_rid2sd(st);
  4366. if( sd == NULL )
  4367. return 0;// no player attached
  4368. }
  4369. count = script_getnum(st, 4);
  4370. if( count > 128 - idx1 )
  4371. count = 128 - idx1;
  4372. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4373. return 0;// nothing to copy
  4374. if( id1 == id2 && idx1 > idx2 )
  4375. {// destination might be overlapping the source - copy in reverse order
  4376. for( i = count - 1; i >= 0; --i )
  4377. {
  4378. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4379. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4380. script_removetop(st, -1, 0);
  4381. }
  4382. }
  4383. else
  4384. {// normal copy
  4385. for( i = 0; i < count; ++i )
  4386. {
  4387. if( idx2 + i < 128 )
  4388. {
  4389. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4390. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4391. script_removetop(st, -1, 0);
  4392. }
  4393. else// out of range - assume ""/0
  4394. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4395. }
  4396. }
  4397. return 0;
  4398. }
  4399. /// Returns the size of the array.
  4400. /// Assumes that everything before the starting index exists.
  4401. /// ex: getarraysize(arr[3])
  4402. ///
  4403. /// getarraysize(<array variable>) -> <int>
  4404. BUILDIN_FUNC(getarraysize)
  4405. {
  4406. struct script_data* data;
  4407. const char* name;
  4408. data = script_getdata(st, 2);
  4409. if( !data_isreference(data) )
  4410. {
  4411. ShowError("script:getarraysize: not a variable\n");
  4412. script_reportdata(data);
  4413. script_pushnil(st);
  4414. st->state = END;
  4415. return 1;// not a variable
  4416. }
  4417. name = reference_getname(data);
  4418. if( not_array_variable(*name) )
  4419. {
  4420. ShowError("script:getarraysize: illegal scope\n");
  4421. script_reportdata(data);
  4422. script_pushnil(st);
  4423. st->state = END;
  4424. return 1;// not supported
  4425. }
  4426. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4427. return 0;
  4428. }
  4429. /// Deletes count or all the elements in an array, from the starting index.
  4430. /// ex: deletearray arr[4],2;
  4431. ///
  4432. /// deletearray <array variable>;
  4433. /// deletearray <array variable>,<count>;
  4434. BUILDIN_FUNC(deletearray)
  4435. {
  4436. struct script_data* data;
  4437. const char* name;
  4438. int start;
  4439. int end;
  4440. int id;
  4441. TBL_PC *sd = NULL;
  4442. data = script_getdata(st, 2);
  4443. if( !data_isreference(data) )
  4444. {
  4445. ShowError("script:deletearray: not a variable\n");
  4446. script_reportdata(data);
  4447. st->state = END;
  4448. return 1;// not a variable
  4449. }
  4450. id = reference_getid(data);
  4451. start = reference_getindex(data);
  4452. name = reference_getname(data);
  4453. if( not_array_variable(*name) )
  4454. {
  4455. ShowError("script:deletearray: illegal scope\n");
  4456. script_reportdata(data);
  4457. st->state = END;
  4458. return 1;// not supported
  4459. }
  4460. if( not_server_variable(*name) )
  4461. {
  4462. sd = script_rid2sd(st);
  4463. if( sd == NULL )
  4464. return 0;// no player attached
  4465. }
  4466. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4467. if( start >= end )
  4468. return 0;// nothing to free
  4469. if( script_hasdata(st,3) )
  4470. {
  4471. int count = script_getnum(st, 3);
  4472. if( count > end - start )
  4473. count = end - start;
  4474. if( count <= 0 )
  4475. return 0;// nothing to free
  4476. // move rest of the elements backward
  4477. for( ; start + count < end; ++start )
  4478. {
  4479. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4480. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4481. script_removetop(st, -1, 0);
  4482. }
  4483. }
  4484. // clear the rest of the array
  4485. if( is_string_variable(name) )
  4486. {
  4487. for( ; start < end; ++start )
  4488. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4489. }
  4490. else
  4491. {
  4492. for( ; start < end; ++start )
  4493. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4494. }
  4495. return 0;
  4496. }
  4497. /// Returns a reference to the target index of the array variable.
  4498. /// Equivalent to var[index].
  4499. ///
  4500. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4501. BUILDIN_FUNC(getelementofarray)
  4502. {
  4503. struct script_data* data;
  4504. const char* name;
  4505. int32 id;
  4506. int i;
  4507. data = script_getdata(st, 2);
  4508. if( !data_isreference(data) )
  4509. {
  4510. ShowError("script:getelementofarray: not a variable\n");
  4511. script_reportdata(data);
  4512. script_pushnil(st);
  4513. st->state = END;
  4514. return 1;// not a variable
  4515. }
  4516. id = reference_getid(data);
  4517. name = reference_getname(data);
  4518. if( not_array_variable(*name) )
  4519. {
  4520. ShowError("script:getelementofarray: illegal scope\n");
  4521. script_reportdata(data);
  4522. script_pushnil(st);
  4523. st->state = END;
  4524. return 1;// not supported
  4525. }
  4526. i = script_getnum(st, 3);
  4527. if( i < 0 || i >= 128 )
  4528. {
  4529. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4530. script_reportdata(data);
  4531. script_pushnil(st);
  4532. st->state = END;
  4533. return 1;// out of range
  4534. }
  4535. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4536. return 0;
  4537. }
  4538. /////////////////////////////////////////////////////////////////////
  4539. /// ...
  4540. ///
  4541. /*==========================================
  4542. *
  4543. *------------------------------------------*/
  4544. BUILDIN_FUNC(setlook)
  4545. {
  4546. int type,val;
  4547. TBL_PC* sd;
  4548. type=script_getnum(st,2);
  4549. val=script_getnum(st,3);
  4550. sd = script_rid2sd(st);
  4551. if( sd == NULL )
  4552. return 0;
  4553. pc_changelook(sd,type,val);
  4554. return 0;
  4555. }
  4556. BUILDIN_FUNC(changelook)
  4557. { // As setlook but only client side
  4558. int type,val;
  4559. TBL_PC* sd;
  4560. type=script_getnum(st,2);
  4561. val=script_getnum(st,3);
  4562. sd = script_rid2sd(st);
  4563. if( sd == NULL )
  4564. return 0;
  4565. clif_changelook(&sd->bl,type,val);
  4566. return 0;
  4567. }
  4568. /*==========================================
  4569. *
  4570. *------------------------------------------*/
  4571. BUILDIN_FUNC(cutin)
  4572. {
  4573. TBL_PC* sd;
  4574. sd = script_rid2sd(st);
  4575. if( sd == NULL )
  4576. return 0;
  4577. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4578. return 0;
  4579. }
  4580. /*==========================================
  4581. *
  4582. *------------------------------------------*/
  4583. BUILDIN_FUNC(viewpoint)
  4584. {
  4585. int type,x,y,id,color;
  4586. TBL_PC* sd;
  4587. type=script_getnum(st,2);
  4588. x=script_getnum(st,3);
  4589. y=script_getnum(st,4);
  4590. id=script_getnum(st,5);
  4591. color=script_getnum(st,6);
  4592. sd = script_rid2sd(st);
  4593. if( sd == NULL )
  4594. return 0;
  4595. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4596. return 0;
  4597. }
  4598. /*==========================================
  4599. *
  4600. *------------------------------------------*/
  4601. BUILDIN_FUNC(countitem)
  4602. {
  4603. int nameid, i;
  4604. int count = 0;
  4605. struct script_data* data;
  4606. TBL_PC* sd = script_rid2sd(st);
  4607. if (!sd) {
  4608. script_pushint(st,0);
  4609. return 0;
  4610. }
  4611. data = script_getdata(st,2);
  4612. get_val(st,data);
  4613. if( data_isstring(data) ) {
  4614. const char* name = conv_str(st,data);
  4615. struct item_data* item_data;
  4616. if((item_data = itemdb_searchname(name)) != NULL)
  4617. nameid = item_data->nameid;
  4618. else
  4619. nameid = 0;
  4620. } else
  4621. nameid = conv_num(st,data);
  4622. if (nameid < 500) {
  4623. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4624. script_reportsrc(st);
  4625. script_pushint(st,0);
  4626. return 1;
  4627. }
  4628. for(i = 0; i < MAX_INVENTORY; i++)
  4629. if(sd->status.inventory[i].nameid == nameid)
  4630. count += sd->status.inventory[i].amount;
  4631. script_pushint(st,count);
  4632. return 0;
  4633. }
  4634. /*==========================================
  4635. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4636. * returns number of items that meet the conditions
  4637. *------------------------------------------*/
  4638. BUILDIN_FUNC(countitem2)
  4639. {
  4640. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4641. int count = 0;
  4642. int i;
  4643. struct script_data* data;
  4644. TBL_PC* sd = script_rid2sd(st);
  4645. if (!sd) {
  4646. script_pushint(st,0);
  4647. return 0;
  4648. }
  4649. data = script_getdata(st,2);
  4650. get_val(st,data);
  4651. if( data_isstring(data) ) {
  4652. const char* name = conv_str(st,data);
  4653. struct item_data* item_data;
  4654. if((item_data = itemdb_searchname(name)) != NULL)
  4655. nameid = item_data->nameid;
  4656. else
  4657. nameid = 0;
  4658. } else
  4659. nameid = conv_num(st,data);
  4660. iden = script_getnum(st,3);
  4661. ref = script_getnum(st,4);
  4662. attr = script_getnum(st,5);
  4663. c1 = (short)script_getnum(st,6);
  4664. c2 = (short)script_getnum(st,7);
  4665. c3 = (short)script_getnum(st,8);
  4666. c4 = (short)script_getnum(st,9);
  4667. if (nameid < 500) {
  4668. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4669. script_pushint(st,0);
  4670. return 1;
  4671. }
  4672. for(i = 0; i < MAX_INVENTORY; i++)
  4673. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4674. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4675. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4676. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4677. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4678. sd->status.inventory[i].card[3] == c4
  4679. )
  4680. count += sd->status.inventory[i].amount;
  4681. script_pushint(st,count);
  4682. return 0;
  4683. }
  4684. /*==========================================
  4685. * 重量チェック
  4686. *------------------------------------------*/
  4687. BUILDIN_FUNC(checkweight)
  4688. {
  4689. int nameid=0,amount,i;
  4690. unsigned long weight;
  4691. TBL_PC *sd;
  4692. struct script_data *data;
  4693. sd = script_rid2sd(st);
  4694. if( sd == NULL )
  4695. return 0;
  4696. data=script_getdata(st,2);
  4697. get_val(st,data);
  4698. if( data_isstring(data) ){
  4699. const char *name=conv_str(st,data);
  4700. struct item_data *item_data = itemdb_searchname(name);
  4701. if( item_data )
  4702. nameid=item_data->nameid;
  4703. }else
  4704. nameid=conv_num(st,data);
  4705. amount=script_getnum(st,3);
  4706. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4707. script_pushint(st,0);
  4708. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4709. return 1;
  4710. }
  4711. weight = itemdb_weight(nameid)*amount;
  4712. if( amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight )
  4713. script_pushint(st,0);
  4714. else if( itemdb_isstackable(nameid) )
  4715. {
  4716. if( (i = pc_search_inventory(sd,nameid)) >= 0 )
  4717. script_pushint(st,amount + sd->status.inventory[i].amount > MAX_AMOUNT ? 0 : 1);
  4718. else
  4719. script_pushint(st,pc_search_inventory(sd,0) >= 0 ? 1 : 0);
  4720. }
  4721. else
  4722. {
  4723. for( i = 0; i < MAX_INVENTORY && amount; ++i )
  4724. if( sd->status.inventory[i].nameid == 0 )
  4725. amount--;
  4726. script_pushint(st,amount ? 0 : 1);
  4727. }
  4728. return 0;
  4729. }
  4730. /*==========================================
  4731. * getitem <item id>,<amount>{,<character ID>};
  4732. * getitem "<item name>",<amount>{,<character ID>};
  4733. *------------------------------------------*/
  4734. BUILDIN_FUNC(getitem)
  4735. {
  4736. int nameid,amount,get_count,i,flag = 0;
  4737. struct item it;
  4738. TBL_PC *sd;
  4739. struct script_data *data;
  4740. data=script_getdata(st,2);
  4741. get_val(st,data);
  4742. if( data_isstring(data) )
  4743. {// "<item name>"
  4744. const char *name=conv_str(st,data);
  4745. struct item_data *item_data = itemdb_searchname(name);
  4746. if( item_data == NULL ){
  4747. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4748. return 1; //No item created.
  4749. }
  4750. nameid=item_data->nameid;
  4751. } else if( data_isint(data) )
  4752. {// <item id>
  4753. nameid=conv_num(st,data);
  4754. //Violet Box, Blue Box, etc - random item pick
  4755. if( nameid < 0 ) {
  4756. nameid=itemdb_searchrandomid(-nameid);
  4757. flag = 1;
  4758. }
  4759. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4760. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4761. return 1; //No item created.
  4762. }
  4763. } else {
  4764. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4765. return 1;
  4766. }
  4767. // <amount>
  4768. if( (amount=script_getnum(st,3)) <= 0)
  4769. return 0; //return if amount <=0, skip the useles iteration
  4770. memset(&it,0,sizeof(it));
  4771. it.nameid=nameid;
  4772. if(!flag)
  4773. it.identify=1;
  4774. else
  4775. it.identify=itemdb_isidentified(nameid);
  4776. if( script_hasdata(st,4) )
  4777. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4778. else
  4779. sd=script_rid2sd(st); // Attached player
  4780. if( sd == NULL ) // no target
  4781. return 0;
  4782. //Check if it's stackable.
  4783. if (!itemdb_isstackable(nameid))
  4784. get_count = 1;
  4785. else
  4786. get_count = amount;
  4787. for (i = 0; i < amount; i += get_count)
  4788. {
  4789. // if not pet egg
  4790. if (!pet_create_egg(sd, nameid))
  4791. {
  4792. if ((flag = pc_additem(sd, &it, get_count)))
  4793. {
  4794. clif_additem(sd, 0, 0, flag);
  4795. if( pc_candrop(sd,&it) )
  4796. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4797. }
  4798. }
  4799. }
  4800. //Logs items, got from (N)PC scripts [Lupus]
  4801. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4802. log_pick_pc(sd, "N", nameid, amount, NULL);
  4803. return 0;
  4804. }
  4805. /*==========================================
  4806. *
  4807. *------------------------------------------*/
  4808. BUILDIN_FUNC(getitem2)
  4809. {
  4810. int nameid,amount,get_count,i,flag = 0;
  4811. int iden,ref,attr,c1,c2,c3,c4;
  4812. struct item_data *item_data;
  4813. struct item item_tmp;
  4814. TBL_PC *sd;
  4815. struct script_data *data;
  4816. if( script_hasdata(st,11) )
  4817. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4818. else
  4819. sd=script_rid2sd(st); // Attached player
  4820. if( sd == NULL ) // no target
  4821. return 0;
  4822. data=script_getdata(st,2);
  4823. get_val(st,data);
  4824. if( data_isstring(data) ){
  4825. const char *name=conv_str(st,data);
  4826. struct item_data *item_data = itemdb_searchname(name);
  4827. if( item_data )
  4828. nameid=item_data->nameid;
  4829. else
  4830. nameid=UNKNOWN_ITEM_ID;
  4831. }else
  4832. nameid=conv_num(st,data);
  4833. amount=script_getnum(st,3);
  4834. iden=script_getnum(st,4);
  4835. ref=script_getnum(st,5);
  4836. attr=script_getnum(st,6);
  4837. c1=(short)script_getnum(st,7);
  4838. c2=(short)script_getnum(st,8);
  4839. c3=(short)script_getnum(st,9);
  4840. c4=(short)script_getnum(st,10);
  4841. if(nameid<0) { // ランダム
  4842. nameid=itemdb_searchrandomid(-nameid);
  4843. flag = 1;
  4844. }
  4845. if(nameid > 0) {
  4846. memset(&item_tmp,0,sizeof(item_tmp));
  4847. item_data=itemdb_exists(nameid);
  4848. if (item_data == NULL)
  4849. return -1;
  4850. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4851. if(ref > 10) ref = 10;
  4852. }
  4853. else if(item_data->type==IT_PETEGG) {
  4854. iden = 1;
  4855. ref = 0;
  4856. }
  4857. else {
  4858. iden = 1;
  4859. ref = attr = 0;
  4860. }
  4861. item_tmp.nameid=nameid;
  4862. if(!flag)
  4863. item_tmp.identify=iden;
  4864. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4865. item_tmp.identify=0;
  4866. item_tmp.refine=ref;
  4867. item_tmp.attribute=attr;
  4868. item_tmp.card[0]=(short)c1;
  4869. item_tmp.card[1]=(short)c2;
  4870. item_tmp.card[2]=(short)c3;
  4871. item_tmp.card[3]=(short)c4;
  4872. //Check if it's stackable.
  4873. if (!itemdb_isstackable(nameid))
  4874. get_count = 1;
  4875. else
  4876. get_count = amount;
  4877. for (i = 0; i < amount; i += get_count)
  4878. {
  4879. // if not pet egg
  4880. if (!pet_create_egg(sd, nameid))
  4881. {
  4882. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4883. {
  4884. clif_additem(sd, 0, 0, flag);
  4885. if( pc_candrop(sd,&item_tmp) )
  4886. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4887. }
  4888. }
  4889. }
  4890. //Logs items, got from (N)PC scripts [Lupus]
  4891. if(log_config.enable_logs&0x40)
  4892. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4893. }
  4894. return 0;
  4895. }
  4896. /*==========================================
  4897. * rentitem <item id>
  4898. * rentitem "<item name>"
  4899. *------------------------------------------*/
  4900. BUILDIN_FUNC(rentitem)
  4901. {
  4902. struct map_session_data *sd;
  4903. struct script_data *data;
  4904. struct item it;
  4905. int seconds;
  4906. int nameid = 0, flag;
  4907. data = script_getdata(st,2);
  4908. get_val(st,data);
  4909. if( (sd = script_rid2sd(st)) == NULL )
  4910. return 0;
  4911. if( data_isstring(data) )
  4912. {
  4913. const char *name = conv_str(st,data);
  4914. struct item_data *itd = itemdb_searchname(name);
  4915. if( itd == NULL )
  4916. {
  4917. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  4918. return 1;
  4919. }
  4920. nameid = itd->nameid;
  4921. }
  4922. else if( data_isint(data) )
  4923. {
  4924. nameid = conv_num(st,data);
  4925. if( nameid <= 0 || !itemdb_exists(nameid) )
  4926. {
  4927. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  4928. return 1;
  4929. }
  4930. }
  4931. else
  4932. {
  4933. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  4934. return 1;
  4935. }
  4936. seconds = script_getnum(st,3);
  4937. memset(&it, 0, sizeof(it));
  4938. it.nameid = nameid;
  4939. it.identify = 1;
  4940. it.expire_time = (unsigned int)(time(NULL) + seconds);
  4941. if( (flag = pc_additem(sd, &it, 1)) )
  4942. {
  4943. clif_additem(sd, 0, 0, flag);
  4944. return 1;
  4945. }
  4946. clif_rental_time(sd->fd, nameid, seconds);
  4947. pc_inventory_rental_add(sd, seconds);
  4948. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  4949. log_pick_pc(sd, "N", nameid, 1, NULL);
  4950. return 0;
  4951. }
  4952. /*==========================================
  4953. * gets an item with someone's name inscribed [Skotlex]
  4954. * getinscribeditem item_num, character_name
  4955. * Returned Qty is always 1, only works on equip-able
  4956. * equipment
  4957. *------------------------------------------*/
  4958. BUILDIN_FUNC(getnameditem)
  4959. {
  4960. int nameid;
  4961. struct item item_tmp;
  4962. TBL_PC *sd, *tsd;
  4963. struct script_data *data;
  4964. sd = script_rid2sd(st);
  4965. if (sd == NULL)
  4966. { //Player not attached!
  4967. script_pushint(st,0);
  4968. return 0;
  4969. }
  4970. data=script_getdata(st,2);
  4971. get_val(st,data);
  4972. if( data_isstring(data) ){
  4973. const char *name=conv_str(st,data);
  4974. struct item_data *item_data = itemdb_searchname(name);
  4975. if( item_data == NULL)
  4976. { //Failed
  4977. script_pushint(st,0);
  4978. return 0;
  4979. }
  4980. nameid = item_data->nameid;
  4981. }else
  4982. nameid = conv_num(st,data);
  4983. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4984. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4985. script_pushint(st,0);
  4986. return 0;
  4987. }
  4988. data=script_getdata(st,3);
  4989. get_val(st,data);
  4990. if( data_isstring(data) ) //Char Name
  4991. tsd=map_nick2sd(conv_str(st,data));
  4992. else //Char Id was given
  4993. tsd=map_charid2sd(conv_num(st,data));
  4994. if( tsd == NULL )
  4995. { //Failed
  4996. script_pushint(st,0);
  4997. return 0;
  4998. }
  4999. memset(&item_tmp,0,sizeof(item_tmp));
  5000. item_tmp.nameid=nameid;
  5001. item_tmp.amount=1;
  5002. item_tmp.identify=1;
  5003. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5004. item_tmp.card[2]=tsd->status.char_id;
  5005. item_tmp.card[3]=tsd->status.char_id >> 16;
  5006. if(pc_additem(sd,&item_tmp,1)) {
  5007. script_pushint(st,0);
  5008. return 0; //Failed to add item, we will not drop if they don't fit
  5009. }
  5010. //Logs items, got from (N)PC scripts [Lupus]
  5011. if(log_config.enable_logs&0x40)
  5012. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5013. script_pushint(st,1);
  5014. return 0;
  5015. }
  5016. /*==========================================
  5017. * gets a random item ID from an item group [Skotlex]
  5018. * groupranditem group_num
  5019. *------------------------------------------*/
  5020. BUILDIN_FUNC(grouprandomitem)
  5021. {
  5022. int group;
  5023. group = script_getnum(st,2);
  5024. script_pushint(st,itemdb_searchrandomid(group));
  5025. return 0;
  5026. }
  5027. /*==========================================
  5028. *
  5029. *------------------------------------------*/
  5030. BUILDIN_FUNC(makeitem)
  5031. {
  5032. int nameid,amount,flag = 0;
  5033. int x,y,m;
  5034. const char *mapname;
  5035. struct item item_tmp;
  5036. struct script_data *data;
  5037. data=script_getdata(st,2);
  5038. get_val(st,data);
  5039. if( data_isstring(data) ){
  5040. const char *name=conv_str(st,data);
  5041. struct item_data *item_data = itemdb_searchname(name);
  5042. if( item_data )
  5043. nameid=item_data->nameid;
  5044. else
  5045. nameid=UNKNOWN_ITEM_ID;
  5046. }else
  5047. nameid=conv_num(st,data);
  5048. amount=script_getnum(st,3);
  5049. mapname =script_getstr(st,4);
  5050. x =script_getnum(st,5);
  5051. y =script_getnum(st,6);
  5052. if(strcmp(mapname,"this")==0)
  5053. {
  5054. TBL_PC *sd;
  5055. sd = script_rid2sd(st);
  5056. if (!sd) return 0; //Failed...
  5057. m=sd->bl.m;
  5058. } else
  5059. m=map_mapname2mapid(mapname);
  5060. if(nameid<0) { // ランダム
  5061. nameid=itemdb_searchrandomid(-nameid);
  5062. flag = 1;
  5063. }
  5064. if(nameid > 0) {
  5065. memset(&item_tmp,0,sizeof(item_tmp));
  5066. item_tmp.nameid=nameid;
  5067. if(!flag)
  5068. item_tmp.identify=1;
  5069. else
  5070. item_tmp.identify=itemdb_isidentified(nameid);
  5071. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5072. }
  5073. return 0;
  5074. }
  5075. /// Deletes items from the target/attached player.
  5076. /// Prioritizes ordinary items.
  5077. ///
  5078. /// delitem <item id>,<amount>{,<account id>}
  5079. /// delitem "<item name>",<amount>{,<account id>}
  5080. BUILDIN_FUNC(delitem)
  5081. {
  5082. int nameid=0,amount,i,important_item=0;
  5083. TBL_PC *sd;
  5084. struct script_data *data;
  5085. if( script_hasdata(st,4) )
  5086. {
  5087. int account_id = script_getnum(st,4);
  5088. sd = map_id2sd(account_id); // <account id>
  5089. if( sd == NULL )
  5090. {
  5091. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5092. st->state = END;
  5093. return 1;
  5094. }
  5095. }
  5096. else
  5097. {
  5098. sd = script_rid2sd(st);// attached player
  5099. if( sd == NULL )
  5100. return 0;
  5101. }
  5102. data = script_getdata(st,2);
  5103. get_val(st,data);
  5104. if( data_isstring(data) )
  5105. {
  5106. const char* item_name = conv_str(st,data);
  5107. struct item_data* id = itemdb_searchname(item_name);
  5108. if( id == NULL )
  5109. {
  5110. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5111. st->state = END;
  5112. return 1;
  5113. }
  5114. nameid = id->nameid;// "<item name>"
  5115. }
  5116. else
  5117. nameid = conv_num(st,data);// <item id>
  5118. amount=script_getnum(st,3);
  5119. if( amount <= 0 )
  5120. return 0;// nothing to do
  5121. //1st pass
  5122. //here we won't delete items with CARDS, named items but we count them
  5123. for(i=0;i<MAX_INVENTORY;i++){
  5124. //we don't delete wrong item or equipped item
  5125. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5126. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5127. continue;
  5128. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5129. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5130. sd->status.inventory[i].card[0] == CARD0_PET)
  5131. {
  5132. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5133. continue; //pet couldn't be sent for deletion.
  5134. } else
  5135. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5136. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5137. sd->status.inventory[i].card[0] ||
  5138. sd->status.inventory[i].refine) {
  5139. //this is important item, count it (except for pet eggs)
  5140. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5141. important_item++;
  5142. continue;
  5143. }
  5144. if(sd->status.inventory[i].amount>=amount){
  5145. //Logs items, got from (N)PC scripts [Lupus]
  5146. if(log_config.enable_logs&0x40)
  5147. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5148. pc_delitem(sd,i,amount,0,0);
  5149. return 0; //we deleted exact amount of items. now exit
  5150. } else {
  5151. amount-=sd->status.inventory[i].amount;
  5152. //Logs items, got from (N)PC scripts [Lupus]
  5153. if(log_config.enable_logs&0x40) {
  5154. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5155. }
  5156. //Logs
  5157. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5158. }
  5159. }
  5160. //2nd pass
  5161. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5162. if (important_item>0 && amount>0)
  5163. for(i=0;i<MAX_INVENTORY;i++){
  5164. //we don't delete wrong item
  5165. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5166. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5167. continue;
  5168. if(sd->status.inventory[i].amount>=amount){
  5169. //Logs items, got from (N)PC scripts [Lupus]
  5170. if(log_config.enable_logs&0x40)
  5171. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5172. pc_delitem(sd,i,amount,0,0);
  5173. return 0; //we deleted exact amount of items. now exit
  5174. } else {
  5175. amount-=sd->status.inventory[i].amount;
  5176. //Logs items, got from (N)PC scripts [Lupus]
  5177. if(log_config.enable_logs&0x40)
  5178. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5179. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5180. }
  5181. }
  5182. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5183. st->state = END;
  5184. clif_scriptclose(sd, st->oid);
  5185. return 1;
  5186. }
  5187. /// Deletes items from the target/attached player.
  5188. ///
  5189. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5190. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5191. BUILDIN_FUNC(delitem2)
  5192. {
  5193. int nameid=0,amount,i=0;
  5194. int iden,ref,attr,c1,c2,c3,c4;
  5195. TBL_PC *sd;
  5196. struct script_data *data;
  5197. if( script_hasdata(st,11) )
  5198. {
  5199. int account_id = script_getnum(st,11);
  5200. sd = map_id2sd(account_id); // <account id>
  5201. if( sd == NULL )
  5202. {
  5203. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5204. st->state = END;
  5205. return 1;
  5206. }
  5207. }
  5208. else
  5209. {
  5210. sd = script_rid2sd(st);// attached player
  5211. if( sd == NULL )
  5212. return 0;
  5213. }
  5214. data = script_getdata(st,2);
  5215. get_val(st,data);
  5216. if( data_isstring(data) )
  5217. {
  5218. const char* item_name = conv_str(st,data);
  5219. struct item_data* id = itemdb_searchname(item_name);
  5220. if( id == NULL )
  5221. {
  5222. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5223. st->state = END;
  5224. return 1;
  5225. }
  5226. nameid = id->nameid;// "<item name>"
  5227. }
  5228. else
  5229. nameid = conv_num(st,data);// <item id>
  5230. amount=script_getnum(st,3);
  5231. iden=script_getnum(st,4);
  5232. ref=script_getnum(st,5);
  5233. attr=script_getnum(st,6);
  5234. c1=(short)script_getnum(st,7);
  5235. c2=(short)script_getnum(st,8);
  5236. c3=(short)script_getnum(st,9);
  5237. c4=(short)script_getnum(st,10);
  5238. if( amount <= 0 )
  5239. return 0;// nothing to do
  5240. for(i=0;i<MAX_INVENTORY;i++){
  5241. //we don't delete wrong item or equipped item
  5242. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5243. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5244. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5245. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5246. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5247. sd->status.inventory[i].card[3]!=c4)
  5248. continue;
  5249. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5250. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5251. {
  5252. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5253. continue; //Failed to send delete the pet.
  5254. }
  5255. if(sd->status.inventory[i].amount>=amount){
  5256. //Logs items, got from (N)PC scripts [Lupus]
  5257. if(log_config.enable_logs&0x40)
  5258. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5259. pc_delitem(sd,i,amount,0,0);
  5260. return 0; //we deleted exact amount of items. now exit
  5261. } else {
  5262. amount-=sd->status.inventory[i].amount;
  5263. //Logs items, got from (N)PC scripts [Lupus]
  5264. if(log_config.enable_logs&0x40)
  5265. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5266. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5267. }
  5268. }
  5269. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5270. st->state = END;
  5271. clif_scriptclose(sd, st->oid);
  5272. return 1;
  5273. }
  5274. /*==========================================
  5275. * Enables/Disables use of items while in an NPC [Skotlex]
  5276. *------------------------------------------*/
  5277. BUILDIN_FUNC(enableitemuse)
  5278. {
  5279. TBL_PC *sd;
  5280. sd=script_rid2sd(st);
  5281. if (sd)
  5282. sd->npc_item_flag = st->oid;
  5283. return 0;
  5284. }
  5285. BUILDIN_FUNC(disableitemuse)
  5286. {
  5287. TBL_PC *sd;
  5288. sd=script_rid2sd(st);
  5289. if (sd)
  5290. sd->npc_item_flag = 0;
  5291. return 0;
  5292. }
  5293. /*==========================================
  5294. *キャラ関係のパラメータ取得
  5295. *------------------------------------------*/
  5296. BUILDIN_FUNC(readparam)
  5297. {
  5298. int type;
  5299. TBL_PC *sd;
  5300. type=script_getnum(st,2);
  5301. if( script_hasdata(st,3) )
  5302. sd=map_nick2sd(script_getstr(st,3));
  5303. else
  5304. sd=script_rid2sd(st);
  5305. if(sd==NULL){
  5306. script_pushint(st,-1);
  5307. return 0;
  5308. }
  5309. script_pushint(st,pc_readparam(sd,type));
  5310. return 0;
  5311. }
  5312. /*==========================================
  5313. *キャラ関係のID取得
  5314. *------------------------------------------*/
  5315. BUILDIN_FUNC(getcharid)
  5316. {
  5317. int num;
  5318. TBL_PC *sd;
  5319. num = script_getnum(st,2);
  5320. if( script_hasdata(st,3) )
  5321. sd=map_nick2sd(script_getstr(st,3));
  5322. else
  5323. sd=script_rid2sd(st);
  5324. if(sd==NULL){
  5325. script_pushint(st,0); //return 0, according docs
  5326. return 0;
  5327. }
  5328. switch( num ) {
  5329. case 0: script_pushint(st,sd->status.char_id); break;
  5330. case 1: script_pushint(st,sd->status.party_id); break;
  5331. case 2: script_pushint(st,sd->status.guild_id); break;
  5332. case 3: script_pushint(st,sd->status.account_id); break;
  5333. case 4: script_pushint(st,sd->state.bg_id); break;
  5334. default:
  5335. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5336. script_pushint(st,0);
  5337. break;
  5338. }
  5339. return 0;
  5340. }
  5341. /*==========================================
  5342. *指定IDのPT名取得
  5343. *------------------------------------------*/
  5344. char *buildin_getpartyname_sub(int party_id)
  5345. {
  5346. struct party_data *p;
  5347. p=party_search(party_id);
  5348. if(p!=NULL){
  5349. char *buf;
  5350. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5351. memcpy(buf, p->party.name, NAME_LENGTH);
  5352. buf[NAME_LENGTH-1] = '\0';
  5353. return buf;
  5354. }
  5355. return 0;
  5356. }
  5357. BUILDIN_FUNC(getpartyname)
  5358. {
  5359. char *name;
  5360. int party_id;
  5361. party_id=script_getnum(st,2);
  5362. name=buildin_getpartyname_sub(party_id);
  5363. if(name != NULL)
  5364. script_pushstr(st,name);
  5365. else
  5366. script_pushconststr(st,"null");
  5367. return 0;
  5368. }
  5369. /*==========================================
  5370. *指定IDのPT人数とメンバーID取得
  5371. *------------------------------------------*/
  5372. BUILDIN_FUNC(getpartymember)
  5373. {
  5374. struct party_data *p;
  5375. int i,j=0,type=0;
  5376. p=party_search(script_getnum(st,2));
  5377. if( script_hasdata(st,3) )
  5378. type=script_getnum(st,3);
  5379. if(p!=NULL){
  5380. for(i=0;i<MAX_PARTY;i++){
  5381. if(p->party.member[i].account_id){
  5382. switch (type) {
  5383. case 2:
  5384. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5385. break;
  5386. case 1:
  5387. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5388. break;
  5389. default:
  5390. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5391. }
  5392. j++;
  5393. }
  5394. }
  5395. }
  5396. mapreg_setreg(add_str("$@partymembercount"),j);
  5397. return 0;
  5398. }
  5399. /*==========================================
  5400. * Retrieves party leader. if flag is specified,
  5401. * return some of the leader data. Otherwise, return name.
  5402. *------------------------------------------*/
  5403. BUILDIN_FUNC(getpartyleader)
  5404. {
  5405. int party_id, type = 0, i=0;
  5406. struct party_data *p;
  5407. party_id=script_getnum(st,2);
  5408. if( script_hasdata(st,3) )
  5409. type=script_getnum(st,3);
  5410. p=party_search(party_id);
  5411. if (p) //Search leader
  5412. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5413. if (!p || i == MAX_PARTY) { //leader not found
  5414. if (type)
  5415. script_pushint(st,-1);
  5416. else
  5417. script_pushconststr(st,"null");
  5418. return 0;
  5419. }
  5420. switch (type) {
  5421. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5422. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5423. case 3: script_pushint(st,p->party.member[i].class_); break;
  5424. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5425. case 5: script_pushint(st,p->party.member[i].lv); break;
  5426. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5427. }
  5428. return 0;
  5429. }
  5430. /*==========================================
  5431. *指定IDのギルド名取得
  5432. *------------------------------------------*/
  5433. char *buildin_getguildname_sub(int guild_id)
  5434. {
  5435. struct guild *g=NULL;
  5436. g=guild_search(guild_id);
  5437. if(g!=NULL){
  5438. char *buf;
  5439. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5440. memcpy(buf, g->name, NAME_LENGTH);
  5441. buf[NAME_LENGTH-1] = '\0';
  5442. return buf;
  5443. }
  5444. return NULL;
  5445. }
  5446. BUILDIN_FUNC(getguildname)
  5447. {
  5448. char *name;
  5449. int guild_id=script_getnum(st,2);
  5450. name=buildin_getguildname_sub(guild_id);
  5451. if(name != NULL)
  5452. script_pushstr(st,name);
  5453. else
  5454. script_pushconststr(st,"null");
  5455. return 0;
  5456. }
  5457. /*==========================================
  5458. *指定IDのGuildMaster名取得
  5459. *------------------------------------------*/
  5460. char *buildin_getguildmaster_sub(int guild_id)
  5461. {
  5462. struct guild *g=NULL;
  5463. g=guild_search(guild_id);
  5464. if(g!=NULL){
  5465. char *buf;
  5466. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5467. memcpy(buf, g->master, NAME_LENGTH);
  5468. buf[NAME_LENGTH-1] = '\0';
  5469. return buf;
  5470. }
  5471. return 0;
  5472. }
  5473. BUILDIN_FUNC(getguildmaster)
  5474. {
  5475. char *master;
  5476. int guild_id=script_getnum(st,2);
  5477. master=buildin_getguildmaster_sub(guild_id);
  5478. if(master!=0)
  5479. script_pushstr(st,master);
  5480. else
  5481. script_pushconststr(st,"null");
  5482. return 0;
  5483. }
  5484. BUILDIN_FUNC(getguildmasterid)
  5485. {
  5486. char *master;
  5487. TBL_PC *sd=NULL;
  5488. int guild_id=script_getnum(st,2);
  5489. master=buildin_getguildmaster_sub(guild_id);
  5490. if(master!=0){
  5491. if((sd=map_nick2sd(master)) == NULL){
  5492. script_pushint(st,0);
  5493. return 0;
  5494. }
  5495. script_pushint(st,sd->status.char_id);
  5496. }else{
  5497. script_pushint(st,0);
  5498. }
  5499. return 0;
  5500. }
  5501. /*==========================================
  5502. * キャラクタの名前
  5503. *------------------------------------------*/
  5504. BUILDIN_FUNC(strcharinfo)
  5505. {
  5506. TBL_PC *sd;
  5507. int num;
  5508. char *buf;
  5509. sd=script_rid2sd(st);
  5510. if (!sd) { //Avoid crashing....
  5511. script_pushconststr(st,"");
  5512. return 0;
  5513. }
  5514. num=script_getnum(st,2);
  5515. switch(num){
  5516. case 0:
  5517. script_pushstrcopy(st,sd->status.name);
  5518. break;
  5519. case 1:
  5520. buf=buildin_getpartyname_sub(sd->status.party_id);
  5521. if(buf!=0)
  5522. script_pushstr(st,buf);
  5523. else
  5524. script_pushconststr(st,"");
  5525. break;
  5526. case 2:
  5527. buf=buildin_getguildname_sub(sd->status.guild_id);
  5528. if(buf != NULL)
  5529. script_pushstr(st,buf);
  5530. else
  5531. script_pushconststr(st,"");
  5532. break;
  5533. case 3:
  5534. script_pushconststr(st,map[sd->bl.m].name);
  5535. break;
  5536. default:
  5537. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5538. script_pushconststr(st,"");
  5539. break;
  5540. }
  5541. return 0;
  5542. }
  5543. /*==========================================
  5544. * 呼び出し元のNPC情報を取得する
  5545. *------------------------------------------*/
  5546. BUILDIN_FUNC(strnpcinfo)
  5547. {
  5548. TBL_NPC* nd;
  5549. int num;
  5550. char *buf,*name=NULL;
  5551. nd = map_id2nd(st->oid);
  5552. if (!nd) {
  5553. script_pushconststr(st, "");
  5554. return 0;
  5555. }
  5556. num = script_getnum(st,2);
  5557. switch(num){
  5558. case 0: // display name
  5559. name = aStrdup(nd->name);
  5560. break;
  5561. case 1: // visible part of display name
  5562. if((buf = strchr(nd->name,'#')) != NULL)
  5563. {
  5564. name = aStrdup(nd->name);
  5565. name[buf - nd->name] = 0;
  5566. } else // Return the name, there is no '#' present
  5567. name = aStrdup(nd->name);
  5568. break;
  5569. case 2: // # fragment
  5570. if((buf = strchr(nd->name,'#')) != NULL)
  5571. name = aStrdup(buf+1);
  5572. break;
  5573. case 3: // unique name
  5574. name = aStrdup(nd->exname);
  5575. break;
  5576. }
  5577. if(name)
  5578. script_pushstr(st, name);
  5579. else
  5580. script_pushconststr(st, "");
  5581. return 0;
  5582. }
  5583. // aegis->athena slot position conversion table
  5584. static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW};
  5585. /*==========================================
  5586. * GetEquipID(Pos); Pos: 1-10
  5587. *------------------------------------------*/
  5588. BUILDIN_FUNC(getequipid)
  5589. {
  5590. int i, num;
  5591. TBL_PC* sd;
  5592. struct item_data* item;
  5593. sd = script_rid2sd(st);
  5594. if( sd == NULL )
  5595. return 0;
  5596. num = script_getnum(st,2) - 1;
  5597. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5598. {
  5599. script_pushint(st,-1);
  5600. return 0;
  5601. }
  5602. // get inventory position of item
  5603. i = pc_checkequip(sd,equip[num]);
  5604. if( i < 0 )
  5605. {
  5606. script_pushint(st,-1);
  5607. return 0;
  5608. }
  5609. item = sd->inventory_data[i];
  5610. if( item != 0 )
  5611. script_pushint(st,item->nameid);
  5612. else
  5613. script_pushint(st,0);
  5614. return 0;
  5615. }
  5616. /*==========================================
  5617. * 装備名文字列(精錬メニュー用)
  5618. *------------------------------------------*/
  5619. BUILDIN_FUNC(getequipname)
  5620. {
  5621. int i, num;
  5622. TBL_PC* sd;
  5623. struct item_data* item;
  5624. sd = script_rid2sd(st);
  5625. if( sd == NULL )
  5626. return 0;
  5627. num = script_getnum(st,2) - 1;
  5628. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5629. {
  5630. script_pushconststr(st,"");
  5631. return 0;
  5632. }
  5633. // get inventory position of item
  5634. i = pc_checkequip(sd,equip[num]);
  5635. if( i < 0 )
  5636. {
  5637. script_pushint(st,-1);
  5638. return 0;
  5639. }
  5640. item = sd->inventory_data[i];
  5641. if( item != 0 )
  5642. script_pushstrcopy(st,item->jname);
  5643. else
  5644. script_pushconststr(st,"");
  5645. return 0;
  5646. }
  5647. /*==========================================
  5648. * getbrokenid [Valaris]
  5649. *------------------------------------------*/
  5650. BUILDIN_FUNC(getbrokenid)
  5651. {
  5652. int i,num,id=0,brokencounter=0;
  5653. TBL_PC *sd;
  5654. sd = script_rid2sd(st);
  5655. if( sd == NULL )
  5656. return 0;
  5657. num=script_getnum(st,2);
  5658. for(i=0; i<MAX_INVENTORY; i++) {
  5659. if(sd->status.inventory[i].attribute){
  5660. brokencounter++;
  5661. if(num==brokencounter){
  5662. id=sd->status.inventory[i].nameid;
  5663. break;
  5664. }
  5665. }
  5666. }
  5667. script_pushint(st,id);
  5668. return 0;
  5669. }
  5670. /*==========================================
  5671. * repair [Valaris]
  5672. *------------------------------------------*/
  5673. BUILDIN_FUNC(repair)
  5674. {
  5675. int i,num;
  5676. int repaircounter=0;
  5677. TBL_PC *sd;
  5678. sd = script_rid2sd(st);
  5679. if( sd == NULL )
  5680. return 0;
  5681. num=script_getnum(st,2);
  5682. for(i=0; i<MAX_INVENTORY; i++) {
  5683. if(sd->status.inventory[i].attribute){
  5684. repaircounter++;
  5685. if(num==repaircounter){
  5686. sd->status.inventory[i].attribute=0;
  5687. clif_equiplist(sd);
  5688. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5689. clif_misceffect(&sd->bl, 3);
  5690. break;
  5691. }
  5692. }
  5693. }
  5694. return 0;
  5695. }
  5696. /*==========================================
  5697. * 装備チェック
  5698. *------------------------------------------*/
  5699. BUILDIN_FUNC(getequipisequiped)
  5700. {
  5701. int i=-1,num;
  5702. TBL_PC *sd;
  5703. num=script_getnum(st,2);
  5704. sd = script_rid2sd(st);
  5705. if( sd == NULL )
  5706. return 0;
  5707. if (num > 0 && num <= ARRAYLENGTH(equip))
  5708. i=pc_checkequip(sd,equip[num-1]);
  5709. if(i >= 0)
  5710. script_pushint(st,1);
  5711. else
  5712. script_pushint(st,0);
  5713. return 0;
  5714. }
  5715. /*==========================================
  5716. * 装備品精錬可能チェック
  5717. *------------------------------------------*/
  5718. BUILDIN_FUNC(getequipisenableref)
  5719. {
  5720. int i=-1,num;
  5721. TBL_PC *sd;
  5722. num=script_getnum(st,2);
  5723. sd = script_rid2sd(st);
  5724. if( sd == NULL )
  5725. return 0;
  5726. if( num > 0 && num <= ARRAYLENGTH(equip) )
  5727. i = pc_checkequip(sd,equip[num-1]);
  5728. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  5729. script_pushint(st,1);
  5730. else
  5731. script_pushint(st,0);
  5732. return 0;
  5733. }
  5734. /*==========================================
  5735. * 装備品鑑定チェック
  5736. *------------------------------------------*/
  5737. BUILDIN_FUNC(getequipisidentify)
  5738. {
  5739. int i=-1,num;
  5740. TBL_PC *sd;
  5741. num=script_getnum(st,2);
  5742. sd = script_rid2sd(st);
  5743. if( sd == NULL )
  5744. return 0;
  5745. if (num > 0 && num <= ARRAYLENGTH(equip))
  5746. i=pc_checkequip(sd,equip[num-1]);
  5747. if(i >= 0)
  5748. script_pushint(st,sd->status.inventory[i].identify);
  5749. else
  5750. script_pushint(st,0);
  5751. return 0;
  5752. }
  5753. /*==========================================
  5754. * 装備品精錬度
  5755. *------------------------------------------*/
  5756. BUILDIN_FUNC(getequiprefinerycnt)
  5757. {
  5758. int i=-1,num;
  5759. TBL_PC *sd;
  5760. num=script_getnum(st,2);
  5761. sd = script_rid2sd(st);
  5762. if( sd == NULL )
  5763. return 0;
  5764. if (num > 0 && num <= ARRAYLENGTH(equip))
  5765. i=pc_checkequip(sd,equip[num-1]);
  5766. if(i >= 0)
  5767. script_pushint(st,sd->status.inventory[i].refine);
  5768. else
  5769. script_pushint(st,0);
  5770. return 0;
  5771. }
  5772. /*==========================================
  5773. * 装備品武器LV
  5774. *------------------------------------------*/
  5775. BUILDIN_FUNC(getequipweaponlv)
  5776. {
  5777. int i=-1,num;
  5778. TBL_PC *sd;
  5779. num=script_getnum(st,2);
  5780. sd = script_rid2sd(st);
  5781. if( sd == NULL )
  5782. return 0;
  5783. if (num > 0 && num <= ARRAYLENGTH(equip))
  5784. i=pc_checkequip(sd,equip[num-1]);
  5785. if(i >= 0 && sd->inventory_data[i])
  5786. script_pushint(st,sd->inventory_data[i]->wlv);
  5787. else
  5788. script_pushint(st,0);
  5789. return 0;
  5790. }
  5791. /*==========================================
  5792. * 装備品精錬成功率
  5793. *------------------------------------------*/
  5794. BUILDIN_FUNC(getequippercentrefinery)
  5795. {
  5796. int i=-1,num;
  5797. TBL_PC *sd;
  5798. num=script_getnum(st,2);
  5799. sd = script_rid2sd(st);
  5800. if( sd == NULL )
  5801. return 0;
  5802. if (num > 0 && num <= ARRAYLENGTH(equip))
  5803. i=pc_checkequip(sd,equip[num-1]);
  5804. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5805. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5806. else
  5807. script_pushint(st,0);
  5808. return 0;
  5809. }
  5810. /*==========================================
  5811. * 精錬成功
  5812. *------------------------------------------*/
  5813. BUILDIN_FUNC(successrefitem)
  5814. {
  5815. int i=-1,num,ep;
  5816. TBL_PC *sd;
  5817. num=script_getnum(st,2);
  5818. sd = script_rid2sd(st);
  5819. if( sd == NULL )
  5820. return 0;
  5821. if (num > 0 && num <= ARRAYLENGTH(equip))
  5822. i=pc_checkequip(sd,equip[num-1]);
  5823. if(i >= 0) {
  5824. ep=sd->status.inventory[i].equip;
  5825. //Logs items, got from (N)PC scripts [Lupus]
  5826. if(log_config.enable_logs&0x40)
  5827. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5828. sd->status.inventory[i].refine++;
  5829. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  5830. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5831. clif_delitem(sd,i,1,3);
  5832. //Logs items, got from (N)PC scripts [Lupus]
  5833. if(log_config.enable_logs&0x40)
  5834. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5835. clif_additem(sd,i,1,0);
  5836. pc_equipitem(sd,i,ep);
  5837. clif_misceffect(&sd->bl,3);
  5838. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5839. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5840. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5841. ){ // Fame point system [DracoRPG]
  5842. switch (sd->inventory_data[i]->wlv){
  5843. case 1:
  5844. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5845. break;
  5846. case 2:
  5847. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5848. break;
  5849. case 3:
  5850. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5851. break;
  5852. }
  5853. }
  5854. }
  5855. return 0;
  5856. }
  5857. /*==========================================
  5858. * 精錬失敗
  5859. *------------------------------------------*/
  5860. BUILDIN_FUNC(failedrefitem)
  5861. {
  5862. int i=-1,num;
  5863. TBL_PC *sd;
  5864. num=script_getnum(st,2);
  5865. sd = script_rid2sd(st);
  5866. if( sd == NULL )
  5867. return 0;
  5868. if (num > 0 && num <= ARRAYLENGTH(equip))
  5869. i=pc_checkequip(sd,equip[num-1]);
  5870. if(i >= 0) {
  5871. //Logs items, got from (N)PC scripts [Lupus]
  5872. if(log_config.enable_logs&0x40)
  5873. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5874. sd->status.inventory[i].refine = 0;
  5875. pc_unequipitem(sd,i,3);
  5876. // 精錬失敗エフェクトのパケット
  5877. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5878. pc_delitem(sd,i,1,0,2);
  5879. // 他の人にも失敗を通知
  5880. clif_misceffect(&sd->bl,2);
  5881. }
  5882. return 0;
  5883. }
  5884. /*==========================================
  5885. *
  5886. *------------------------------------------*/
  5887. BUILDIN_FUNC(statusup)
  5888. {
  5889. int type;
  5890. TBL_PC *sd;
  5891. type=script_getnum(st,2);
  5892. sd = script_rid2sd(st);
  5893. if( sd == NULL )
  5894. return 0;
  5895. pc_statusup(sd,type);
  5896. return 0;
  5897. }
  5898. /*==========================================
  5899. *
  5900. *------------------------------------------*/
  5901. BUILDIN_FUNC(statusup2)
  5902. {
  5903. int type,val;
  5904. TBL_PC *sd;
  5905. type=script_getnum(st,2);
  5906. val=script_getnum(st,3);
  5907. sd = script_rid2sd(st);
  5908. if( sd == NULL )
  5909. return 0;
  5910. pc_statusup2(sd,type,val);
  5911. return 0;
  5912. }
  5913. /// See 'doc/item_bonus.txt'
  5914. ///
  5915. /// bonus <bonus type>,<val1>;
  5916. /// bonus2 <bonus type>,<val1>,<val2>;
  5917. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5918. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5919. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5920. BUILDIN_FUNC(bonus)
  5921. {
  5922. int type;
  5923. int val1;
  5924. int val2 = 0;
  5925. int val3 = 0;
  5926. int val4 = 0;
  5927. int val5 = 0;
  5928. TBL_PC* sd;
  5929. sd = script_rid2sd(st);
  5930. if( sd == NULL )
  5931. return 0; // no player attached
  5932. type = script_getnum(st,2);
  5933. switch( type )
  5934. {
  5935. case SP_AUTOSPELL:
  5936. case SP_AUTOSPELL_WHENHIT:
  5937. case SP_AUTOSPELL_ONSKILL:
  5938. case SP_SKILL_ATK:
  5939. case SP_SKILL_HEAL:
  5940. case SP_SKILL_HEAL2:
  5941. case SP_ADD_SKILL_BLOW:
  5942. case SP_CASTRATE:
  5943. case SP_ADDEFF_ONSKILL:
  5944. // these bonuses support skill names
  5945. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5946. break;
  5947. default:
  5948. val1 = script_getnum(st,3);
  5949. break;
  5950. }
  5951. switch( script_lastdata(st)-2 )
  5952. {
  5953. case 1:
  5954. pc_bonus(sd, type, val1);
  5955. break;
  5956. case 2:
  5957. val2 = script_getnum(st,4);
  5958. pc_bonus2(sd, type, val1, val2);
  5959. break;
  5960. case 3:
  5961. val2 = script_getnum(st,4);
  5962. val3 = script_getnum(st,5);
  5963. pc_bonus3(sd, type, val1, val2, val3);
  5964. break;
  5965. case 4:
  5966. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  5967. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  5968. else
  5969. val2 = script_getnum(st,4);
  5970. val3 = script_getnum(st,5);
  5971. val4 = script_getnum(st,6);
  5972. pc_bonus4(sd, type, val1, val2, val3, val4);
  5973. break;
  5974. case 5:
  5975. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  5976. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  5977. else
  5978. val2 = script_getnum(st,4);
  5979. val3 = script_getnum(st,5);
  5980. val4 = script_getnum(st,6);
  5981. val5 = script_getnum(st,7);
  5982. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  5983. break;
  5984. default:
  5985. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  5986. break;
  5987. }
  5988. return 0;
  5989. }
  5990. BUILDIN_FUNC(autobonus)
  5991. {
  5992. unsigned int dur;
  5993. short rate;
  5994. short atk_type = 0;
  5995. TBL_PC* sd;
  5996. const char *bonus_script, *other_script = NULL;
  5997. sd = script_rid2sd(st);
  5998. if( sd == NULL )
  5999. return 0; // no player attached
  6000. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6001. return 0;
  6002. rate = script_getnum(st,3);
  6003. dur = script_getnum(st,4);
  6004. bonus_script = script_getstr(st,2);
  6005. if( !rate || !dur || !bonus_script )
  6006. return 0;
  6007. if( script_hasdata(st,5) )
  6008. atk_type = script_getnum(st,5);
  6009. if( script_hasdata(st,6) )
  6010. other_script = script_getstr(st,6);
  6011. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6012. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6013. {
  6014. script_add_autobonus(bonus_script);
  6015. if( other_script )
  6016. script_add_autobonus(other_script);
  6017. }
  6018. return 0;
  6019. }
  6020. BUILDIN_FUNC(autobonus2)
  6021. {
  6022. unsigned int dur;
  6023. short rate;
  6024. short atk_type = 0;
  6025. TBL_PC* sd;
  6026. const char *bonus_script, *other_script = NULL;
  6027. sd = script_rid2sd(st);
  6028. if( sd == NULL )
  6029. return 0; // no player attached
  6030. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6031. return 0;
  6032. rate = script_getnum(st,3);
  6033. dur = script_getnum(st,4);
  6034. bonus_script = script_getstr(st,2);
  6035. if( !rate || !dur || !bonus_script )
  6036. return 0;
  6037. if( script_hasdata(st,5) )
  6038. atk_type = script_getnum(st,5);
  6039. if( script_hasdata(st,6) )
  6040. other_script = script_getstr(st,6);
  6041. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6042. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6043. {
  6044. script_add_autobonus(bonus_script);
  6045. if( other_script )
  6046. script_add_autobonus(other_script);
  6047. }
  6048. return 0;
  6049. }
  6050. BUILDIN_FUNC(autobonus3)
  6051. {
  6052. unsigned int dur;
  6053. short rate,atk_type;
  6054. TBL_PC* sd;
  6055. const char *bonus_script, *other_script = NULL;
  6056. sd = script_rid2sd(st);
  6057. if( sd == NULL )
  6058. return 0; // no player attached
  6059. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6060. return 0;
  6061. rate = script_getnum(st,3);
  6062. dur = script_getnum(st,4);
  6063. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6064. bonus_script = script_getstr(st,2);
  6065. if( !rate || !dur || !atk_type || !bonus_script )
  6066. return 0;
  6067. if( script_hasdata(st,6) )
  6068. other_script = script_getstr(st,6);
  6069. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6070. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6071. {
  6072. script_add_autobonus(bonus_script);
  6073. if( other_script )
  6074. script_add_autobonus(other_script);
  6075. }
  6076. return 0;
  6077. }
  6078. /// Changes the level of a player skill.
  6079. /// <flag> defaults to 1
  6080. /// <flag>=0 : set the level of the skill
  6081. /// <flag>=1 : set the temporary level of the skill
  6082. /// <flag>=2 : add to the level of the skill
  6083. ///
  6084. /// skill <skill id>,<level>,<flag>
  6085. /// skill <skill id>,<level>
  6086. /// skill "<skill name>",<level>,<flag>
  6087. /// skill "<skill name>",<level>
  6088. BUILDIN_FUNC(skill)
  6089. {
  6090. int id;
  6091. int level;
  6092. int flag = 1;
  6093. TBL_PC* sd;
  6094. sd = script_rid2sd(st);
  6095. if( sd == NULL )
  6096. return 0;// no player attached, report source
  6097. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6098. level = script_getnum(st,3);
  6099. if( script_hasdata(st,4) )
  6100. flag = script_getnum(st,4);
  6101. pc_skill(sd, id, level, flag);
  6102. return 0;
  6103. }
  6104. /// Changes the level of a player skill.
  6105. /// like skill, but <flag> defaults to 2
  6106. ///
  6107. /// addtoskill <skill id>,<amount>,<flag>
  6108. /// addtoskill <skill id>,<amount>
  6109. /// addtoskill "<skill name>",<amount>,<flag>
  6110. /// addtoskill "<skill name>",<amount>
  6111. ///
  6112. /// @see skill
  6113. BUILDIN_FUNC(addtoskill)
  6114. {
  6115. int id;
  6116. int level;
  6117. int flag = 2;
  6118. TBL_PC* sd;
  6119. sd = script_rid2sd(st);
  6120. if( sd == NULL )
  6121. return 0;// no player attached, report source
  6122. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6123. level = script_getnum(st,3);
  6124. if( script_hasdata(st,4) )
  6125. flag = script_getnum(st,4);
  6126. pc_skill(sd, id, level, flag);
  6127. return 0;
  6128. }
  6129. /// Increases the level of a guild skill.
  6130. ///
  6131. /// guildskill <skill id>,<amount>;
  6132. /// guildskill "<skill name>",<amount>;
  6133. BUILDIN_FUNC(guildskill)
  6134. {
  6135. int id;
  6136. int level;
  6137. TBL_PC* sd;
  6138. int i;
  6139. sd = script_rid2sd(st);
  6140. if( sd == NULL )
  6141. return 0;// no player attached, report source
  6142. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6143. level = script_getnum(st,3);
  6144. for( i=0; i < level; i++ )
  6145. guild_skillup(sd, id);
  6146. return 0;
  6147. }
  6148. /// Returns the level of the player skill.
  6149. ///
  6150. /// getskilllv(<skill id>) -> <level>
  6151. /// getskilllv("<skill name>") -> <level>
  6152. BUILDIN_FUNC(getskilllv)
  6153. {
  6154. int id;
  6155. TBL_PC* sd;
  6156. sd = script_rid2sd(st);
  6157. if( sd == NULL )
  6158. return 0;// no player attached, report source
  6159. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6160. script_pushint(st, pc_checkskill(sd,id));
  6161. return 0;
  6162. }
  6163. /// Returns the level of the guild skill.
  6164. ///
  6165. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6166. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6167. BUILDIN_FUNC(getgdskilllv)
  6168. {
  6169. int guild_id;
  6170. int skill_id;
  6171. struct guild* g;
  6172. guild_id = script_getnum(st,2);
  6173. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6174. g = guild_search(guild_id);
  6175. if( g == NULL )
  6176. script_pushint(st, -1);
  6177. else
  6178. script_pushint(st, guild_checkskill(g,skill_id));
  6179. return 0;
  6180. }
  6181. /// Returns the 'basic_skill_check' setting.
  6182. /// This config determines if the server checks the skill level of NV_BASIC
  6183. /// before allowing the basic actions.
  6184. ///
  6185. /// basicskillcheck() -> <bool>
  6186. BUILDIN_FUNC(basicskillcheck)
  6187. {
  6188. script_pushint(st, battle_config.basic_skill_check);
  6189. return 0;
  6190. }
  6191. /// Returns the GM level of the player.
  6192. ///
  6193. /// getgmlevel() -> <level>
  6194. BUILDIN_FUNC(getgmlevel)
  6195. {
  6196. TBL_PC* sd;
  6197. sd = script_rid2sd(st);
  6198. if( sd == NULL )
  6199. return 0;// no player attached, report source
  6200. script_pushint(st, pc_isGM(sd));
  6201. return 0;
  6202. }
  6203. /// Terminates the execution of this script instance.
  6204. ///
  6205. /// end
  6206. BUILDIN_FUNC(end)
  6207. {
  6208. st->state = END;
  6209. return 0;
  6210. }
  6211. /// Checks if the player has that effect state (option).
  6212. ///
  6213. /// checkoption(<option>) -> <bool>
  6214. BUILDIN_FUNC(checkoption)
  6215. {
  6216. int option;
  6217. TBL_PC* sd;
  6218. sd = script_rid2sd(st);
  6219. if( sd == NULL )
  6220. return 0;// no player attached, report source
  6221. option = script_getnum(st,2);
  6222. if( sd->sc.option&option )
  6223. script_pushint(st, 1);
  6224. else
  6225. script_pushint(st, 0);
  6226. return 0;
  6227. }
  6228. /// Checks if the player is in that body state (opt1).
  6229. ///
  6230. /// checkoption1(<opt1>) -> <bool>
  6231. BUILDIN_FUNC(checkoption1)
  6232. {
  6233. int opt1;
  6234. TBL_PC* sd;
  6235. sd = script_rid2sd(st);
  6236. if( sd == NULL )
  6237. return 0;// no player attached, report source
  6238. opt1 = script_getnum(st,2);
  6239. if( sd->sc.opt1 == opt1 )
  6240. script_pushint(st, 1);
  6241. else
  6242. script_pushint(st, 0);
  6243. return 0;
  6244. }
  6245. /// Checks if the player has that health state (opt2).
  6246. ///
  6247. /// checkoption2(<opt2>) -> <bool>
  6248. BUILDIN_FUNC(checkoption2)
  6249. {
  6250. int opt2;
  6251. TBL_PC* sd;
  6252. sd = script_rid2sd(st);
  6253. if( sd == NULL )
  6254. return 0;// no player attached, report source
  6255. opt2 = script_getnum(st,2);
  6256. if( sd->sc.opt2&opt2 )
  6257. script_pushint(st, 1);
  6258. else
  6259. script_pushint(st, 0);
  6260. return 0;
  6261. }
  6262. /// Changes the effect state (option) of the player.
  6263. /// <flag> defaults to 1
  6264. /// <flag>=0 : removes the option
  6265. /// <flag>=other : adds the option
  6266. ///
  6267. /// setoption <option>,<flag>;
  6268. /// setoption <option>;
  6269. BUILDIN_FUNC(setoption)
  6270. {
  6271. int option;
  6272. int flag = 1;
  6273. TBL_PC* sd;
  6274. sd = script_rid2sd(st);
  6275. if( sd == NULL )
  6276. return 0;// no player attached, report source
  6277. option = script_getnum(st,2);
  6278. if( script_hasdata(st,3) )
  6279. flag = script_getnum(st,3);
  6280. else if( !option ){// Request to remove everything.
  6281. flag = 0;
  6282. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6283. }
  6284. if( flag ){// Add option
  6285. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6286. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6287. pc_setoption(sd, sd->sc.option|option);
  6288. } else// Remove option
  6289. pc_setoption(sd, sd->sc.option&~option);
  6290. return 0;
  6291. }
  6292. /// Returns if the player has a cart.
  6293. ///
  6294. /// checkcart() -> <bool>
  6295. ///
  6296. /// @author Valaris
  6297. BUILDIN_FUNC(checkcart)
  6298. {
  6299. TBL_PC* sd;
  6300. sd = script_rid2sd(st);
  6301. if( sd == NULL )
  6302. return 0;// no player attached, report source
  6303. if( pc_iscarton(sd) )
  6304. script_pushint(st, 1);
  6305. else
  6306. script_pushint(st, 0);
  6307. return 0;
  6308. }
  6309. /// Sets the cart of the player.
  6310. /// <type> defaults to 1
  6311. /// <type>=0 : removes the cart
  6312. /// <type>=1 : Normal cart
  6313. /// <type>=2 : Wooden cart
  6314. /// <type>=3 : Covered cart with flowers and ferns
  6315. /// <type>=4 : Wooden cart with a Panda doll on the back
  6316. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6317. ///
  6318. /// setcart <type>;
  6319. /// setcart;
  6320. BUILDIN_FUNC(setcart)
  6321. {
  6322. int type = 1;
  6323. TBL_PC* sd;
  6324. sd = script_rid2sd(st);
  6325. if( sd == NULL )
  6326. return 0;// no player attached, report source
  6327. if( script_hasdata(st,2) )
  6328. type = script_getnum(st,2);
  6329. pc_setcart(sd, type);
  6330. return 0;
  6331. }
  6332. /// Returns if the player has a falcon.
  6333. ///
  6334. /// checkfalcon() -> <bool>
  6335. ///
  6336. /// @author Valaris
  6337. BUILDIN_FUNC(checkfalcon)
  6338. {
  6339. TBL_PC* sd;
  6340. sd = script_rid2sd(st);
  6341. if( sd == NULL )
  6342. return 0;// no player attached, report source
  6343. if( pc_isfalcon(sd) )
  6344. script_pushint(st, 1);
  6345. else
  6346. script_pushint(st, 0);
  6347. return 0;
  6348. }
  6349. /// Sets if the player has a falcon or not.
  6350. /// <flag> defaults to 1
  6351. ///
  6352. /// setfalcon <flag>;
  6353. /// setfalcon;
  6354. BUILDIN_FUNC(setfalcon)
  6355. {
  6356. int flag = 1;
  6357. TBL_PC* sd;
  6358. sd = script_rid2sd(st);
  6359. if( sd == NULL )
  6360. return 0;// no player attached, report source
  6361. if( script_hasdata(st,2) )
  6362. flag = script_getnum(st,2);
  6363. pc_setfalcon(sd, flag);
  6364. return 0;
  6365. }
  6366. /// Returns if the player is riding.
  6367. ///
  6368. /// checkriding() -> <bool>
  6369. ///
  6370. /// @author Valaris
  6371. BUILDIN_FUNC(checkriding)
  6372. {
  6373. TBL_PC* sd;
  6374. sd = script_rid2sd(st);
  6375. if( sd == NULL )
  6376. return 0;// no player attached, report source
  6377. if( pc_isriding(sd) )
  6378. script_pushint(st, 1);
  6379. else
  6380. script_pushint(st, 0);
  6381. return 0;
  6382. }
  6383. /// Sets if the player is riding.
  6384. /// <flag> defaults to 1
  6385. ///
  6386. /// setriding <flag>;
  6387. /// setriding;
  6388. BUILDIN_FUNC(setriding)
  6389. {
  6390. int flag = 1;
  6391. TBL_PC* sd;
  6392. sd = script_rid2sd(st);
  6393. if( sd == NULL )
  6394. return 0;// no player attached, report source
  6395. if( script_hasdata(st,2) )
  6396. flag = script_getnum(st,2);
  6397. pc_setriding(sd, flag);
  6398. return 0;
  6399. }
  6400. /// Sets the save point of the player.
  6401. ///
  6402. /// save "<map name>",<x>,<y>
  6403. /// savepoint "<map name>",<x>,<y>
  6404. BUILDIN_FUNC(savepoint)
  6405. {
  6406. int x;
  6407. int y;
  6408. short map;
  6409. const char* str;
  6410. TBL_PC* sd;
  6411. sd = script_rid2sd(st);
  6412. if( sd == NULL )
  6413. return 0;// no player attached, report source
  6414. str = script_getstr(st, 2);
  6415. x = script_getnum(st,3);
  6416. y = script_getnum(st,4);
  6417. map = mapindex_name2id(str);
  6418. if( map )
  6419. pc_setsavepoint(sd, map, x, y);
  6420. return 0;
  6421. }
  6422. /*==========================================
  6423. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6424. *------------------------------------------*/
  6425. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6426. {
  6427. int type;
  6428. time_t timer;
  6429. struct tm *t;
  6430. type=script_getnum(st,2);
  6431. switch(type){
  6432. case 2:
  6433. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6434. // from the system clock.)
  6435. script_pushint(st,(int)time(NULL));
  6436. break;
  6437. case 1:
  6438. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6439. time(&timer);
  6440. t=localtime(&timer);
  6441. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6442. break;
  6443. case 0:
  6444. default:
  6445. //type 0:(System Ticks)
  6446. script_pushint(st,gettick());
  6447. break;
  6448. }
  6449. return 0;
  6450. }
  6451. /*==========================================
  6452. * GetTime(Type);
  6453. * 1: Sec 2: Min 3: Hour
  6454. * 4: WeekDay 5: MonthDay 6: Month
  6455. * 7: Year
  6456. *------------------------------------------*/
  6457. BUILDIN_FUNC(gettime) /* Asgard Version */
  6458. {
  6459. int type;
  6460. time_t timer;
  6461. struct tm *t;
  6462. type=script_getnum(st,2);
  6463. time(&timer);
  6464. t=localtime(&timer);
  6465. switch(type){
  6466. case 1://Sec(0~59)
  6467. script_pushint(st,t->tm_sec);
  6468. break;
  6469. case 2://Min(0~59)
  6470. script_pushint(st,t->tm_min);
  6471. break;
  6472. case 3://Hour(0~23)
  6473. script_pushint(st,t->tm_hour);
  6474. break;
  6475. case 4://WeekDay(0~6)
  6476. script_pushint(st,t->tm_wday);
  6477. break;
  6478. case 5://MonthDay(01~31)
  6479. script_pushint(st,t->tm_mday);
  6480. break;
  6481. case 6://Month(01~12)
  6482. script_pushint(st,t->tm_mon+1);
  6483. break;
  6484. case 7://Year(20xx)
  6485. script_pushint(st,t->tm_year+1900);
  6486. break;
  6487. case 8://Year Day(01~366)
  6488. script_pushint(st,t->tm_yday+1);
  6489. break;
  6490. default://(format error)
  6491. script_pushint(st,-1);
  6492. break;
  6493. }
  6494. return 0;
  6495. }
  6496. /*==========================================
  6497. * GetTimeStr("TimeFMT", Length);
  6498. *------------------------------------------*/
  6499. BUILDIN_FUNC(gettimestr)
  6500. {
  6501. char *tmpstr;
  6502. const char *fmtstr;
  6503. int maxlen;
  6504. time_t now = time(NULL);
  6505. fmtstr=script_getstr(st,2);
  6506. maxlen=script_getnum(st,3);
  6507. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6508. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6509. tmpstr[maxlen]='\0';
  6510. script_pushstr(st,tmpstr);
  6511. return 0;
  6512. }
  6513. /*==========================================
  6514. * カプラ倉庫を開く
  6515. *------------------------------------------*/
  6516. BUILDIN_FUNC(openstorage)
  6517. {
  6518. TBL_PC* sd;
  6519. sd = script_rid2sd(st);
  6520. if( sd == NULL )
  6521. return 0;
  6522. storage_storageopen(sd);
  6523. return 0;
  6524. }
  6525. BUILDIN_FUNC(guildopenstorage)
  6526. {
  6527. TBL_PC* sd;
  6528. int ret;
  6529. sd = script_rid2sd(st);
  6530. if( sd == NULL )
  6531. return 0;
  6532. ret = storage_guild_storageopen(sd);
  6533. script_pushint(st,ret);
  6534. return 0;
  6535. }
  6536. /*==========================================
  6537. * アイテムによるスキル発動
  6538. *------------------------------------------*/
  6539. /// itemskill <skill id>,<level>
  6540. /// itemskill "<skill name>",<level>
  6541. BUILDIN_FUNC(itemskill)
  6542. {
  6543. int id;
  6544. int lv;
  6545. TBL_PC* sd;
  6546. sd = script_rid2sd(st);
  6547. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6548. return 0;
  6549. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6550. lv = script_getnum(st,3);
  6551. sd->skillitem=id;
  6552. sd->skillitemlv=lv;
  6553. clif_item_skill(sd,id,lv);
  6554. return 0;
  6555. }
  6556. /*==========================================
  6557. * アイテム作成
  6558. *------------------------------------------*/
  6559. BUILDIN_FUNC(produce)
  6560. {
  6561. int trigger;
  6562. TBL_PC* sd;
  6563. sd = script_rid2sd(st);
  6564. if( sd == NULL )
  6565. return 0;
  6566. trigger=script_getnum(st,2);
  6567. clif_skill_produce_mix_list(sd, trigger);
  6568. return 0;
  6569. }
  6570. /*==========================================
  6571. *
  6572. *------------------------------------------*/
  6573. BUILDIN_FUNC(cooking)
  6574. {
  6575. int trigger;
  6576. TBL_PC* sd;
  6577. sd = script_rid2sd(st);
  6578. if( sd == NULL )
  6579. return 0;
  6580. trigger=script_getnum(st,2);
  6581. clif_cooking_list(sd, trigger);
  6582. return 0;
  6583. }
  6584. /*==========================================
  6585. * NPCでペット作る
  6586. *------------------------------------------*/
  6587. BUILDIN_FUNC(makepet)
  6588. {
  6589. TBL_PC* sd;
  6590. int id,pet_id;
  6591. id=script_getnum(st,2);
  6592. sd = script_rid2sd(st);
  6593. if( sd == NULL )
  6594. return 0;
  6595. pet_id = search_petDB_index(id, PET_CLASS);
  6596. if (pet_id < 0)
  6597. pet_id = search_petDB_index(id, PET_EGG);
  6598. if (pet_id >= 0 && sd) {
  6599. sd->catch_target_class = pet_db[pet_id].class_;
  6600. intif_create_pet(
  6601. sd->status.account_id, sd->status.char_id,
  6602. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6603. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6604. 100, 0, 1, pet_db[pet_id].jname);
  6605. }
  6606. return 0;
  6607. }
  6608. /*==========================================
  6609. * NPCで経験値上げる
  6610. *------------------------------------------*/
  6611. BUILDIN_FUNC(getexp)
  6612. {
  6613. TBL_PC* sd;
  6614. int base=0,job=0;
  6615. double bonus;
  6616. sd = script_rid2sd(st);
  6617. if( sd == NULL )
  6618. return 0;
  6619. base=script_getnum(st,2);
  6620. job =script_getnum(st,3);
  6621. if(base<0 || job<0)
  6622. return 0;
  6623. // bonus for npc-given exp
  6624. bonus = battle_config.quest_exp_rate / 100.;
  6625. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6626. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6627. pc_gainexp(sd, NULL, base, job, true);
  6628. return 0;
  6629. }
  6630. /*==========================================
  6631. * Gain guild exp [Celest]
  6632. *------------------------------------------*/
  6633. BUILDIN_FUNC(guildgetexp)
  6634. {
  6635. TBL_PC* sd;
  6636. int exp;
  6637. sd = script_rid2sd(st);
  6638. if( sd == NULL )
  6639. return 0;
  6640. exp = script_getnum(st,2);
  6641. if(exp < 0)
  6642. return 0;
  6643. if(sd && sd->status.guild_id > 0)
  6644. guild_getexp (sd, exp);
  6645. return 0;
  6646. }
  6647. /*==========================================
  6648. * Changes the guild master of a guild [Skotlex]
  6649. *------------------------------------------*/
  6650. BUILDIN_FUNC(guildchangegm)
  6651. {
  6652. TBL_PC *sd;
  6653. int guild_id;
  6654. const char *name;
  6655. guild_id = script_getnum(st,2);
  6656. name = script_getstr(st,3);
  6657. sd=map_nick2sd(name);
  6658. if (!sd)
  6659. script_pushint(st,0);
  6660. else
  6661. script_pushint(st,guild_gm_change(guild_id, sd));
  6662. return 0;
  6663. }
  6664. /*==========================================
  6665. * モンスター発生
  6666. *------------------------------------------*/
  6667. BUILDIN_FUNC(monster)
  6668. {
  6669. const char* mapn = script_getstr(st,2);
  6670. int x = script_getnum(st,3);
  6671. int y = script_getnum(st,4);
  6672. const char* str = script_getstr(st,5);
  6673. int class_ = script_getnum(st,6);
  6674. int amount = script_getnum(st,7);
  6675. const char* event = "";
  6676. struct map_session_data* sd;
  6677. int m;
  6678. if( script_hasdata(st,8) )
  6679. {
  6680. event = script_getstr(st,8);
  6681. check_event(st, event);
  6682. }
  6683. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6684. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6685. return 1;
  6686. }
  6687. sd = map_id2sd(st->rid);
  6688. if( sd && strcmp(mapn,"this") == 0 )
  6689. m = sd->bl.m;
  6690. else
  6691. {
  6692. m = map_mapname2mapid(mapn);
  6693. if( map[m].flag.src4instance && st->instance_id )
  6694. { // Try to redirect to the instance map, not the src map
  6695. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6696. {
  6697. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6698. return 1;
  6699. }
  6700. }
  6701. }
  6702. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6703. return 0;
  6704. }
  6705. /*==========================================
  6706. * Request List of Monster Drops
  6707. *------------------------------------------*/
  6708. BUILDIN_FUNC(getmobdrops)
  6709. {
  6710. int class_ = script_getnum(st,2);
  6711. int i, j = 0;
  6712. struct mob_db *mob;
  6713. if( !mobdb_checkid(class_) )
  6714. {
  6715. script_pushint(st, 0);
  6716. return 0;
  6717. }
  6718. mob = mob_db(class_);
  6719. for( i = 0; i < MAX_MOB_DROP; i++ )
  6720. {
  6721. if( mob->dropitem[i].nameid < 1 )
  6722. continue;
  6723. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  6724. continue;
  6725. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6726. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6727. j++;
  6728. }
  6729. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6730. script_pushint(st, 1);
  6731. return 0;
  6732. }
  6733. /*==========================================
  6734. * モンスター発生
  6735. *------------------------------------------*/
  6736. BUILDIN_FUNC(areamonster)
  6737. {
  6738. const char* mapn = script_getstr(st,2);
  6739. int x0 = script_getnum(st,3);
  6740. int y0 = script_getnum(st,4);
  6741. int x1 = script_getnum(st,5);
  6742. int y1 = script_getnum(st,6);
  6743. const char* str = script_getstr(st,7);
  6744. int class_ = script_getnum(st,8);
  6745. int amount = script_getnum(st,9);
  6746. const char* event = "";
  6747. struct map_session_data* sd;
  6748. int m;
  6749. if( script_hasdata(st,10) )
  6750. {
  6751. event = script_getstr(st,10);
  6752. check_event(st, event);
  6753. }
  6754. sd = map_id2sd(st->rid);
  6755. if( sd && strcmp(mapn,"this") == 0 )
  6756. m = sd->bl.m;
  6757. else
  6758. {
  6759. m = map_mapname2mapid(mapn);
  6760. if( map[m].flag.src4instance && st->instance_id )
  6761. { // Try to redirect to the instance map, not the src map
  6762. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6763. {
  6764. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6765. return 1;
  6766. }
  6767. }
  6768. }
  6769. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6770. return 0;
  6771. }
  6772. /*==========================================
  6773. * モンスター削除
  6774. *------------------------------------------*/
  6775. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6776. { //same fix but with killmonster instead - stripping events from mobs.
  6777. TBL_MOB* md = (TBL_MOB*)bl;
  6778. char *event=va_arg(ap,char *);
  6779. int allflag=va_arg(ap,int);
  6780. md->state.npc_killmonster = 1;
  6781. if(!allflag){
  6782. if(strcmp(event,md->npc_event)==0)
  6783. status_kill(bl);
  6784. }else{
  6785. if(!md->spawn)
  6786. status_kill(bl);
  6787. }
  6788. md->state.npc_killmonster = 0;
  6789. return 0;
  6790. }
  6791. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6792. {
  6793. TBL_MOB* md = (TBL_MOB*)bl;
  6794. char *event=va_arg(ap,char *);
  6795. int allflag=va_arg(ap,int);
  6796. if(!allflag){
  6797. if(strcmp(event,md->npc_event)==0)
  6798. status_kill(bl);
  6799. }else{
  6800. if(!md->spawn)
  6801. status_kill(bl);
  6802. }
  6803. return 0;
  6804. }
  6805. BUILDIN_FUNC(killmonster)
  6806. {
  6807. const char *mapname,*event;
  6808. int m,allflag=0;
  6809. mapname=script_getstr(st,2);
  6810. event=script_getstr(st,3);
  6811. if(strcmp(event,"All")==0)
  6812. allflag = 1;
  6813. else
  6814. check_event(st, event);
  6815. if( (m=map_mapname2mapid(mapname))<0 )
  6816. return 0;
  6817. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6818. return 0;
  6819. if( script_hasdata(st,4) ) {
  6820. if ( script_getnum(st,4) == 1 ) {
  6821. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6822. return 0;
  6823. }
  6824. }
  6825. map_freeblock_lock();
  6826. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6827. map_freeblock_unlock();
  6828. return 0;
  6829. }
  6830. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6831. { //Strips the event from the mob if it's killed the old method.
  6832. struct mob_data *md;
  6833. md = BL_CAST(BL_MOB, bl);
  6834. if (md->npc_event[0])
  6835. md->npc_event[0] = 0;
  6836. status_kill(bl);
  6837. return 0;
  6838. }
  6839. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6840. {
  6841. status_kill(bl);
  6842. return 0;
  6843. }
  6844. BUILDIN_FUNC(killmonsterall)
  6845. {
  6846. const char *mapname;
  6847. int m;
  6848. mapname=script_getstr(st,2);
  6849. if( (m = map_mapname2mapid(mapname))<0 )
  6850. return 0;
  6851. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6852. return 0;
  6853. if( script_hasdata(st,3) ) {
  6854. if ( script_getnum(st,3) == 1 ) {
  6855. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6856. return 0;
  6857. }
  6858. }
  6859. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6860. return 0;
  6861. }
  6862. /*==========================================
  6863. * Creates a clone of a player.
  6864. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6865. *------------------------------------------*/
  6866. BUILDIN_FUNC(clone)
  6867. {
  6868. TBL_PC *sd, *msd=NULL;
  6869. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6870. unsigned int duration = 0;
  6871. const char *map,*event="";
  6872. map=script_getstr(st,2);
  6873. x=script_getnum(st,3);
  6874. y=script_getnum(st,4);
  6875. event=script_getstr(st,5);
  6876. char_id=script_getnum(st,6);
  6877. if( script_hasdata(st,7) )
  6878. master_id=script_getnum(st,7);
  6879. if( script_hasdata(st,8) )
  6880. mode=script_getnum(st,8);
  6881. if( script_hasdata(st,9) )
  6882. flag=script_getnum(st,9);
  6883. if( script_hasdata(st,10) )
  6884. duration=script_getnum(st,10);
  6885. check_event(st, event);
  6886. m = map_mapname2mapid(map);
  6887. if (m < 0) return 0;
  6888. sd = map_charid2sd(char_id);
  6889. if (master_id) {
  6890. msd = map_charid2sd(master_id);
  6891. if (msd)
  6892. master_id = msd->bl.id;
  6893. else
  6894. master_id = 0;
  6895. }
  6896. if (sd) //Return ID of newly crafted clone.
  6897. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6898. else //Failed to create clone.
  6899. script_pushint(st,0);
  6900. return 0;
  6901. }
  6902. /*==========================================
  6903. * イベント実行
  6904. *------------------------------------------*/
  6905. BUILDIN_FUNC(doevent)
  6906. {
  6907. const char* event = script_getstr(st,2);
  6908. check_event(st, event);
  6909. npc_event(map_id2sd(st->rid),event,0);
  6910. return 0;
  6911. }
  6912. /*==========================================
  6913. * NPC主体イベント実行
  6914. *------------------------------------------*/
  6915. BUILDIN_FUNC(donpcevent)
  6916. {
  6917. const char* event = script_getstr(st,2);
  6918. check_event(st, event);
  6919. npc_event_do(event);
  6920. return 0;
  6921. }
  6922. /// for Aegis compatibility
  6923. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6924. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6925. {
  6926. const char* npc = script_getstr(st,2);
  6927. const char* command = script_getstr(st,3);
  6928. char event[51];
  6929. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6930. check_event(st, event);
  6931. npc_event_do(event);
  6932. return 0;
  6933. }
  6934. /*==========================================
  6935. * イベントタイマー追加
  6936. *------------------------------------------*/
  6937. BUILDIN_FUNC(addtimer)
  6938. {
  6939. int tick = script_getnum(st,2);
  6940. const char* event = script_getstr(st, 3);
  6941. TBL_PC* sd;
  6942. check_event(st, event);
  6943. sd = script_rid2sd(st);
  6944. if( sd == NULL )
  6945. return 0;
  6946. pc_addeventtimer(sd,tick,event);
  6947. return 0;
  6948. }
  6949. /*==========================================
  6950. * イベントタイマー削除
  6951. *------------------------------------------*/
  6952. BUILDIN_FUNC(deltimer)
  6953. {
  6954. const char *event;
  6955. TBL_PC* sd;
  6956. event=script_getstr(st, 2);
  6957. sd = script_rid2sd(st);
  6958. if( sd == NULL )
  6959. return 0;
  6960. check_event(st, event);
  6961. pc_deleventtimer(sd,event);
  6962. return 0;
  6963. }
  6964. /*==========================================
  6965. * イベントタイマーのカウント値追加
  6966. *------------------------------------------*/
  6967. BUILDIN_FUNC(addtimercount)
  6968. {
  6969. const char *event;
  6970. int tick;
  6971. TBL_PC* sd;
  6972. event=script_getstr(st, 2);
  6973. tick=script_getnum(st,3);
  6974. sd = script_rid2sd(st);
  6975. if( sd == NULL )
  6976. return 0;
  6977. check_event(st, event);
  6978. pc_addeventtimercount(sd,event,tick);
  6979. return 0;
  6980. }
  6981. /*==========================================
  6982. * NPCタイマー初期化
  6983. *------------------------------------------*/
  6984. BUILDIN_FUNC(initnpctimer)
  6985. {
  6986. struct npc_data *nd;
  6987. int flag = 0;
  6988. if( script_hasdata(st,3) )
  6989. { //Two arguments: NPC name and attach flag.
  6990. nd = npc_name2id(script_getstr(st, 2));
  6991. flag = script_getnum(st,3);
  6992. }
  6993. else if( script_hasdata(st,2) )
  6994. { //Check if argument is numeric (flag) or string (npc name)
  6995. struct script_data *data;
  6996. data = script_getdata(st,2);
  6997. get_val(st,data);
  6998. if( data_isstring(data) ) //NPC name
  6999. nd = npc_name2id(conv_str(st, data));
  7000. else if( data_isint(data) ) //Flag
  7001. {
  7002. nd = (struct npc_data *)map_id2bl(st->oid);
  7003. flag = conv_num(st,data);
  7004. }
  7005. else
  7006. {
  7007. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7008. return 1;
  7009. }
  7010. }
  7011. else
  7012. nd = (struct npc_data *)map_id2bl(st->oid);
  7013. if( !nd )
  7014. return 0;
  7015. if( flag ) //Attach
  7016. {
  7017. TBL_PC* sd = script_rid2sd(st);
  7018. if( sd == NULL )
  7019. return 0;
  7020. nd->u.scr.rid = sd->bl.id;
  7021. }
  7022. npc_settimerevent_tick(nd,0);
  7023. npc_timerevent_start(nd, st->rid);
  7024. return 0;
  7025. }
  7026. /*==========================================
  7027. * NPCタイマー開始
  7028. *------------------------------------------*/
  7029. BUILDIN_FUNC(startnpctimer)
  7030. {
  7031. struct npc_data *nd;
  7032. int flag = 0;
  7033. if( script_hasdata(st,3) )
  7034. { //Two arguments: NPC name and attach flag.
  7035. nd = npc_name2id(script_getstr(st, 2));
  7036. flag = script_getnum(st,3);
  7037. }
  7038. else if( script_hasdata(st,2) )
  7039. { //Check if argument is numeric (flag) or string (npc name)
  7040. struct script_data *data;
  7041. data = script_getdata(st,2);
  7042. get_val(st,data);
  7043. if( data_isstring(data) ) //NPC name
  7044. nd = npc_name2id(conv_str(st, data));
  7045. else if( data_isint(data) ) //Flag
  7046. {
  7047. nd = (struct npc_data *)map_id2bl(st->oid);
  7048. flag = conv_num(st,data);
  7049. }
  7050. else
  7051. {
  7052. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7053. return 1;
  7054. }
  7055. }
  7056. else
  7057. nd=(struct npc_data *)map_id2bl(st->oid);
  7058. if( !nd )
  7059. return 0;
  7060. if( flag ) //Attach
  7061. {
  7062. TBL_PC* sd = script_rid2sd(st);
  7063. if( sd == NULL )
  7064. return 0;
  7065. nd->u.scr.rid = sd->bl.id;
  7066. }
  7067. npc_timerevent_start(nd, st->rid);
  7068. return 0;
  7069. }
  7070. /*==========================================
  7071. * NPCタイマー停止
  7072. *------------------------------------------*/
  7073. BUILDIN_FUNC(stopnpctimer)
  7074. {
  7075. struct npc_data *nd;
  7076. int flag = 0;
  7077. if( script_hasdata(st,3) )
  7078. { //Two arguments: NPC name and attach flag.
  7079. nd = npc_name2id(script_getstr(st, 2));
  7080. flag = script_getnum(st,3);
  7081. }
  7082. else if( script_hasdata(st,2) )
  7083. { //Check if argument is numeric (flag) or string (npc name)
  7084. struct script_data *data;
  7085. data = script_getdata(st,2);
  7086. get_val(st,data);
  7087. if( data_isstring(data) ) //NPC name
  7088. nd = npc_name2id(conv_str(st, data));
  7089. else if( data_isint(data) ) //Flag
  7090. {
  7091. nd = (struct npc_data *)map_id2bl(st->oid);
  7092. flag = conv_num(st,data);
  7093. }
  7094. else
  7095. {
  7096. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7097. return 1;
  7098. }
  7099. }
  7100. else
  7101. nd=(struct npc_data *)map_id2bl(st->oid);
  7102. if( !nd )
  7103. return 0;
  7104. if( flag ) //Detach
  7105. nd->u.scr.rid = 0;
  7106. npc_timerevent_stop(nd);
  7107. return 0;
  7108. }
  7109. /*==========================================
  7110. * NPCタイマー情報所得
  7111. *------------------------------------------*/
  7112. BUILDIN_FUNC(getnpctimer)
  7113. {
  7114. struct npc_data *nd;
  7115. TBL_PC *sd;
  7116. int type = script_getnum(st,2);
  7117. int val = 0;
  7118. if( script_hasdata(st,3) )
  7119. nd = npc_name2id(script_getstr(st,3));
  7120. else
  7121. nd = (struct npc_data *)map_id2bl(st->oid);
  7122. if( !nd || nd->bl.type != BL_NPC )
  7123. {
  7124. script_pushint(st,0);
  7125. ShowError("getnpctimer: Invalid NPC.\n");
  7126. return 1;
  7127. }
  7128. switch( type )
  7129. {
  7130. case 0: val = npc_gettimerevent_tick(nd); break;
  7131. case 1:
  7132. if( nd->u.scr.rid )
  7133. {
  7134. sd = map_id2sd(nd->u.scr.rid);
  7135. if( !sd )
  7136. {
  7137. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7138. break;
  7139. }
  7140. val = (sd->npc_timer_id != -1);
  7141. }
  7142. else
  7143. val = (nd->u.scr.timerid !=-1);
  7144. break;
  7145. case 2: val = nd->u.scr.timeramount; break;
  7146. }
  7147. script_pushint(st,val);
  7148. return 0;
  7149. }
  7150. /*==========================================
  7151. * NPCタイマー値設定
  7152. *------------------------------------------*/
  7153. BUILDIN_FUNC(setnpctimer)
  7154. {
  7155. int tick;
  7156. struct npc_data *nd;
  7157. tick = script_getnum(st,2);
  7158. if( script_hasdata(st,3) )
  7159. nd = npc_name2id(script_getstr(st,3));
  7160. else
  7161. nd = (struct npc_data *)map_id2bl(st->oid);
  7162. if( !nd || nd->bl.type != BL_NPC )
  7163. {
  7164. script_pushint(st,1);
  7165. ShowError("setnpctimer: Invalid NPC.\n");
  7166. return 1;
  7167. }
  7168. npc_settimerevent_tick(nd,tick);
  7169. script_pushint(st,0);
  7170. return 0;
  7171. }
  7172. /*==========================================
  7173. * attaches the player rid to the timer [Celest]
  7174. *------------------------------------------*/
  7175. BUILDIN_FUNC(attachnpctimer)
  7176. {
  7177. TBL_PC *sd;
  7178. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7179. if( !nd || nd->bl.type != BL_NPC )
  7180. {
  7181. script_pushint(st,1);
  7182. ShowError("setnpctimer: Invalid NPC.\n");
  7183. return 1;
  7184. }
  7185. if( script_hasdata(st,2) )
  7186. sd = map_nick2sd(script_getstr(st,2));
  7187. else
  7188. sd = script_rid2sd(st);
  7189. if( !sd )
  7190. {
  7191. script_pushint(st,1);
  7192. ShowWarning("attachnpctimer: Invalid player.\n");
  7193. return 1;
  7194. }
  7195. nd->u.scr.rid = sd->bl.id;
  7196. script_pushint(st,0);
  7197. return 0;
  7198. }
  7199. /*==========================================
  7200. * detaches a player rid from the timer [Celest]
  7201. *------------------------------------------*/
  7202. BUILDIN_FUNC(detachnpctimer)
  7203. {
  7204. struct npc_data *nd;
  7205. if( script_hasdata(st,2) )
  7206. nd = npc_name2id(script_getstr(st,2));
  7207. else
  7208. nd = (struct npc_data *)map_id2bl(st->oid);
  7209. if( !nd || nd->bl.type != BL_NPC )
  7210. {
  7211. script_pushint(st,1);
  7212. ShowError("detachnpctimer: Invalid NPC.\n");
  7213. return 1;
  7214. }
  7215. nd->u.scr.rid = 0;
  7216. script_pushint(st,0);
  7217. return 0;
  7218. }
  7219. /*==========================================
  7220. * To avoid "player not attached" script errors, this function is provided,
  7221. * it checks if there is a player attached to the current script. [Skotlex]
  7222. * If no, returns 0, if yes, returns the account_id of the attached player.
  7223. *------------------------------------------*/
  7224. BUILDIN_FUNC(playerattached)
  7225. {
  7226. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7227. script_pushint(st,0);
  7228. else
  7229. script_pushint(st,st->rid);
  7230. return 0;
  7231. }
  7232. /*==========================================
  7233. * 天の声アナウンス
  7234. *------------------------------------------*/
  7235. BUILDIN_FUNC(announce)
  7236. {
  7237. const char *mes = script_getstr(st,2);
  7238. int flag = script_getnum(st,3);
  7239. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7240. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7241. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7242. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7243. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7244. if (flag&0x0f) // Broadcast source or broadcast region defined
  7245. {
  7246. send_target target;
  7247. struct block_list *bl = (flag&0x08) ? map_id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source
  7248. if (bl == NULL)
  7249. return 0;
  7250. flag &= 0x07;
  7251. target = (flag == 1) ? ALL_SAMEMAP :
  7252. (flag == 2) ? AREA :
  7253. (flag == 3) ? SELF :
  7254. ALL_CLIENT;
  7255. if (fontColor)
  7256. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7257. else
  7258. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7259. }
  7260. else
  7261. {
  7262. if (fontColor)
  7263. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7264. else
  7265. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7266. }
  7267. return 0;
  7268. }
  7269. /*==========================================
  7270. * 天の声アナウンス(特定マップ)
  7271. *------------------------------------------*/
  7272. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7273. {
  7274. char *mes = va_arg(ap, char *);
  7275. int len = va_arg(ap, int);
  7276. int type = va_arg(ap, int);
  7277. char *fontColor = va_arg(ap, char *);
  7278. short fontType = (short)va_arg(ap, int);
  7279. short fontSize = (short)va_arg(ap, int);
  7280. short fontAlign = (short)va_arg(ap, int);
  7281. short fontY = (short)va_arg(ap, int);
  7282. if (fontColor)
  7283. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7284. else
  7285. clif_broadcast(bl, mes, len, type, SELF);
  7286. return 0;
  7287. }
  7288. BUILDIN_FUNC(mapannounce)
  7289. {
  7290. const char *mapname = script_getstr(st,2);
  7291. const char *mes = script_getstr(st,3);
  7292. int flag = script_getnum(st,4);
  7293. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7294. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7295. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7296. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7297. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7298. int m;
  7299. if ((m = map_mapname2mapid(mapname)) < 0)
  7300. return 0;
  7301. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7302. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7303. return 0;
  7304. }
  7305. /*==========================================
  7306. * 天の声アナウンス(特定エリア)
  7307. *------------------------------------------*/
  7308. BUILDIN_FUNC(areaannounce)
  7309. {
  7310. const char *mapname = script_getstr(st,2);
  7311. int x0 = script_getnum(st,3);
  7312. int y0 = script_getnum(st,4);
  7313. int x1 = script_getnum(st,5);
  7314. int y1 = script_getnum(st,6);
  7315. const char *mes = script_getstr(st,7);
  7316. int flag = script_getnum(st,8);
  7317. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7318. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7319. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7320. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7321. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7322. int m;
  7323. if ((m = map_mapname2mapid(mapname)) < 0)
  7324. return 0;
  7325. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7326. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7327. return 0;
  7328. }
  7329. /*==========================================
  7330. * ユーザー数所得
  7331. *------------------------------------------*/
  7332. BUILDIN_FUNC(getusers)
  7333. {
  7334. int flag, val = 0;
  7335. struct map_session_data* sd;
  7336. struct block_list* bl = NULL;
  7337. flag = script_getnum(st,2);
  7338. if(flag&0x8)
  7339. {// npc
  7340. bl = map_id2bl(st->oid);
  7341. }
  7342. else if((sd = script_rid2sd(st))!=NULL)
  7343. {// pc
  7344. bl = &sd->bl;
  7345. }
  7346. switch(flag&0x07)
  7347. {
  7348. case 0:
  7349. if(bl)
  7350. {
  7351. val = map[bl->m].users;
  7352. }
  7353. break;
  7354. case 1:
  7355. val = map_getusers();
  7356. break;
  7357. default:
  7358. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7359. script_pushint(st,0);
  7360. return 1;
  7361. }
  7362. script_pushint(st,val);
  7363. return 0;
  7364. }
  7365. /*==========================================
  7366. * Works like @WHO - displays all online users names in window
  7367. *------------------------------------------*/
  7368. BUILDIN_FUNC(getusersname)
  7369. {
  7370. TBL_PC *sd, *pl_sd;
  7371. int disp_num=1;
  7372. struct s_mapiterator* iter;
  7373. sd = script_rid2sd(st);
  7374. if (!sd) return 0;
  7375. iter = mapit_getallusers();
  7376. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7377. {
  7378. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7379. continue; // skip hidden GMs
  7380. if((disp_num++)%10==0)
  7381. clif_scriptnext(sd,st->oid);
  7382. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7383. }
  7384. mapit_free(iter);
  7385. return 0;
  7386. }
  7387. /*==========================================
  7388. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7389. *------------------------------------------*/
  7390. BUILDIN_FUNC(getmapguildusers)
  7391. {
  7392. const char *str;
  7393. int m, gid;
  7394. int i=0,c=0;
  7395. struct guild *g = NULL;
  7396. str=script_getstr(st,2);
  7397. gid=script_getnum(st,3);
  7398. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7399. script_pushint(st,-1);
  7400. return 0;
  7401. }
  7402. g = guild_search(gid);
  7403. if (g){
  7404. for(i = 0; i < g->max_member; i++)
  7405. {
  7406. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7407. c++;
  7408. }
  7409. }
  7410. script_pushint(st,c);
  7411. return 0;
  7412. }
  7413. /*==========================================
  7414. * マップ指定ユーザー数所得
  7415. *------------------------------------------*/
  7416. BUILDIN_FUNC(getmapusers)
  7417. {
  7418. const char *str;
  7419. int m;
  7420. str=script_getstr(st,2);
  7421. if( (m=map_mapname2mapid(str))< 0){
  7422. script_pushint(st,-1);
  7423. return 0;
  7424. }
  7425. script_pushint(st,map[m].users);
  7426. return 0;
  7427. }
  7428. /*==========================================
  7429. * エリア指定ユーザー数所得
  7430. *------------------------------------------*/
  7431. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7432. {
  7433. int *users=va_arg(ap,int *);
  7434. (*users)++;
  7435. return 0;
  7436. }
  7437. BUILDIN_FUNC(getareausers)
  7438. {
  7439. const char *str;
  7440. int m,x0,y0,x1,y1,users=0;
  7441. str=script_getstr(st,2);
  7442. x0=script_getnum(st,3);
  7443. y0=script_getnum(st,4);
  7444. x1=script_getnum(st,5);
  7445. y1=script_getnum(st,6);
  7446. if( (m=map_mapname2mapid(str))< 0){
  7447. script_pushint(st,-1);
  7448. return 0;
  7449. }
  7450. map_foreachinarea(buildin_getareausers_sub,
  7451. m,x0,y0,x1,y1,BL_PC,&users);
  7452. script_pushint(st,users);
  7453. return 0;
  7454. }
  7455. /*==========================================
  7456. * エリア指定ドロップアイテム数所得
  7457. *------------------------------------------*/
  7458. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7459. {
  7460. int item=va_arg(ap,int);
  7461. int *amount=va_arg(ap,int *);
  7462. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7463. if(drop->item_data.nameid==item)
  7464. (*amount)+=drop->item_data.amount;
  7465. return 0;
  7466. }
  7467. BUILDIN_FUNC(getareadropitem)
  7468. {
  7469. const char *str;
  7470. int m,x0,y0,x1,y1,item,amount=0;
  7471. struct script_data *data;
  7472. str=script_getstr(st,2);
  7473. x0=script_getnum(st,3);
  7474. y0=script_getnum(st,4);
  7475. x1=script_getnum(st,5);
  7476. y1=script_getnum(st,6);
  7477. data=script_getdata(st,7);
  7478. get_val(st,data);
  7479. if( data_isstring(data) ){
  7480. const char *name=conv_str(st,data);
  7481. struct item_data *item_data = itemdb_searchname(name);
  7482. item=UNKNOWN_ITEM_ID;
  7483. if( item_data )
  7484. item=item_data->nameid;
  7485. }else
  7486. item=conv_num(st,data);
  7487. if( (m=map_mapname2mapid(str))< 0){
  7488. script_pushint(st,-1);
  7489. return 0;
  7490. }
  7491. map_foreachinarea(buildin_getareadropitem_sub,
  7492. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7493. script_pushint(st,amount);
  7494. return 0;
  7495. }
  7496. /*==========================================
  7497. * NPCの有効化
  7498. *------------------------------------------*/
  7499. BUILDIN_FUNC(enablenpc)
  7500. {
  7501. const char *str;
  7502. str=script_getstr(st,2);
  7503. npc_enable(str,1);
  7504. return 0;
  7505. }
  7506. /*==========================================
  7507. * NPCの無効化
  7508. *------------------------------------------*/
  7509. BUILDIN_FUNC(disablenpc)
  7510. {
  7511. const char *str;
  7512. str=script_getstr(st,2);
  7513. npc_enable(str,0);
  7514. return 0;
  7515. }
  7516. /*==========================================
  7517. * 隠れているNPCの表示
  7518. *------------------------------------------*/
  7519. BUILDIN_FUNC(hideoffnpc)
  7520. {
  7521. const char *str;
  7522. str=script_getstr(st,2);
  7523. npc_enable(str,2);
  7524. return 0;
  7525. }
  7526. /*==========================================
  7527. * NPCをハイディング
  7528. *------------------------------------------*/
  7529. BUILDIN_FUNC(hideonnpc)
  7530. {
  7531. const char *str;
  7532. str=script_getstr(st,2);
  7533. npc_enable(str,4);
  7534. return 0;
  7535. }
  7536. /// Starts a status effect on the target unit or on the attached player.
  7537. ///
  7538. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7539. BUILDIN_FUNC(sc_start)
  7540. {
  7541. struct block_list* bl;
  7542. enum sc_type type;
  7543. int tick;
  7544. int val1;
  7545. int val4 = 0;
  7546. type = (sc_type)script_getnum(st,2);
  7547. tick = script_getnum(st,3);
  7548. val1 = script_getnum(st,4);
  7549. if( script_hasdata(st,5) )
  7550. bl = map_id2bl(script_getnum(st,5));
  7551. else
  7552. bl = map_id2bl(st->rid);
  7553. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7554. {// When there isn't a duration specified, try to get it from the skill_db
  7555. tick = skill_get_time(status_sc2skill(type), val1);
  7556. }
  7557. if( potion_flag == 1 && potion_target )
  7558. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7559. bl = map_id2bl(potion_target);
  7560. tick /= 2;// Thrown potions only last half.
  7561. val4 = 1;// Mark that this was a thrown sc_effect
  7562. }
  7563. if( bl )
  7564. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7565. return 0;
  7566. }
  7567. /// Starts a status effect on the target unit or on the attached player.
  7568. ///
  7569. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7570. BUILDIN_FUNC(sc_start2)
  7571. {
  7572. struct block_list* bl;
  7573. enum sc_type type;
  7574. int tick;
  7575. int val1;
  7576. int val4 = 0;
  7577. int rate;
  7578. type = (sc_type)script_getnum(st,2);
  7579. tick = script_getnum(st,3);
  7580. val1 = script_getnum(st,4);
  7581. rate = script_getnum(st,5);
  7582. if( script_hasdata(st,6) )
  7583. bl = map_id2bl(script_getnum(st,6));
  7584. else
  7585. bl = map_id2bl(st->rid);
  7586. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7587. {// When there isn't a duration specified, try to get it from the skill_db
  7588. tick = skill_get_time(status_sc2skill(type), val1);
  7589. }
  7590. if( potion_flag == 1 && potion_target )
  7591. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7592. bl = map_id2bl(potion_target);
  7593. tick /= 2;// Thrown potions only last half.
  7594. val4 = 1;// Mark that this was a thrown sc_effect
  7595. }
  7596. if( bl )
  7597. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7598. return 0;
  7599. }
  7600. /// Starts a status effect on the target unit or on the attached player.
  7601. ///
  7602. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7603. BUILDIN_FUNC(sc_start4)
  7604. {
  7605. struct block_list* bl;
  7606. enum sc_type type;
  7607. int tick;
  7608. int val1;
  7609. int val2;
  7610. int val3;
  7611. int val4;
  7612. type = (sc_type)script_getnum(st,2);
  7613. tick = script_getnum(st,3);
  7614. val1 = script_getnum(st,4);
  7615. val2 = script_getnum(st,5);
  7616. val3 = script_getnum(st,6);
  7617. val4 = script_getnum(st,7);
  7618. if( script_hasdata(st,8) )
  7619. bl = map_id2bl(script_getnum(st,8));
  7620. else
  7621. bl = map_id2bl(st->rid);
  7622. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7623. {// When there isn't a duration specified, try to get it from the skill_db
  7624. tick = skill_get_time(status_sc2skill(type), val1);
  7625. }
  7626. if( potion_flag == 1 && potion_target )
  7627. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7628. bl = map_id2bl(potion_target);
  7629. tick /= 2;// Thrown potions only last half.
  7630. }
  7631. if( bl )
  7632. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7633. return 0;
  7634. }
  7635. /// Ends one or all status effects on the target unit or on the attached player.
  7636. ///
  7637. /// sc_end <effect_id>{,<unit_id>};
  7638. BUILDIN_FUNC(sc_end)
  7639. {
  7640. struct block_list* bl;
  7641. int type;
  7642. type = script_getnum(st,2);
  7643. if( script_hasdata(st,3) )
  7644. bl = map_id2bl(script_getnum(st,3));
  7645. else
  7646. bl = map_id2bl(st->rid);
  7647. if( potion_flag==1 && potion_target )
  7648. {//##TODO how does this work [FlavioJS]
  7649. bl = map_id2bl(potion_target);
  7650. }
  7651. if( !bl ) return 0;
  7652. if( type >= 0 && type < SC_MAX )
  7653. {
  7654. struct status_change *sc = status_get_sc(bl);
  7655. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7656. if (!sce) return 0;
  7657. //This should help status_change_end force disabling the SC in case it has no limit.
  7658. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7659. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7660. } else
  7661. status_change_clear(bl, 2);// remove all effects
  7662. return 0;
  7663. }
  7664. /*==========================================
  7665. * 状態異常耐性を計算した確率を返す
  7666. *------------------------------------------*/
  7667. BUILDIN_FUNC(getscrate)
  7668. {
  7669. struct block_list *bl;
  7670. int type,rate;
  7671. type=script_getnum(st,2);
  7672. rate=script_getnum(st,3);
  7673. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7674. bl = map_id2bl(script_getnum(st,4));
  7675. else
  7676. bl = map_id2bl(st->rid);
  7677. if (bl)
  7678. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7679. script_pushint(st,rate);
  7680. return 0;
  7681. }
  7682. /*==========================================
  7683. *
  7684. *------------------------------------------*/
  7685. BUILDIN_FUNC(debugmes)
  7686. {
  7687. const char *str;
  7688. str=script_getstr(st,2);
  7689. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7690. return 0;
  7691. }
  7692. /*==========================================
  7693. *捕獲アイテム使用
  7694. *------------------------------------------*/
  7695. BUILDIN_FUNC(catchpet)
  7696. {
  7697. int pet_id;
  7698. TBL_PC *sd;
  7699. pet_id= script_getnum(st,2);
  7700. sd=script_rid2sd(st);
  7701. if( sd == NULL )
  7702. return 0;
  7703. pet_catch_process1(sd,pet_id);
  7704. return 0;
  7705. }
  7706. /*==========================================
  7707. * [orn]
  7708. *------------------------------------------*/
  7709. BUILDIN_FUNC(homunculus_evolution)
  7710. {
  7711. TBL_PC *sd;
  7712. sd=script_rid2sd(st);
  7713. if( sd == NULL )
  7714. return 0;
  7715. if(merc_is_hom_active(sd->hd))
  7716. {
  7717. if (sd->hd->homunculus.intimacy > 91000)
  7718. merc_hom_evolution(sd->hd);
  7719. else
  7720. clif_emotion(&sd->hd->bl, 4) ; //swt
  7721. }
  7722. return 0;
  7723. }
  7724. // [Zephyrus]
  7725. BUILDIN_FUNC(homunculus_shuffle)
  7726. {
  7727. TBL_PC *sd;
  7728. sd=script_rid2sd(st);
  7729. if( sd == NULL )
  7730. return 0;
  7731. if(merc_is_hom_active(sd->hd))
  7732. merc_hom_shuffle(sd->hd);
  7733. return 0;
  7734. }
  7735. //These two functions bring the eA MAPID_* class functionality to scripts.
  7736. BUILDIN_FUNC(eaclass)
  7737. {
  7738. int class_;
  7739. if( script_hasdata(st,2) )
  7740. class_ = script_getnum(st,2);
  7741. else {
  7742. TBL_PC *sd;
  7743. sd=script_rid2sd(st);
  7744. if (!sd) {
  7745. script_pushint(st,-1);
  7746. return 0;
  7747. }
  7748. class_ = sd->status.class_;
  7749. }
  7750. script_pushint(st,pc_jobid2mapid(class_));
  7751. return 0;
  7752. }
  7753. BUILDIN_FUNC(roclass)
  7754. {
  7755. int class_ =script_getnum(st,2);
  7756. int sex;
  7757. if( script_hasdata(st,3) )
  7758. sex = script_getnum(st,3);
  7759. else {
  7760. TBL_PC *sd;
  7761. if (st->rid && (sd=script_rid2sd(st)))
  7762. sex = sd->status.sex;
  7763. else
  7764. sex = 1; //Just use male when not found.
  7765. }
  7766. script_pushint(st,pc_mapid2jobid(class_, sex));
  7767. return 0;
  7768. }
  7769. /*==========================================
  7770. *携帯卵孵化機使用
  7771. *------------------------------------------*/
  7772. BUILDIN_FUNC(birthpet)
  7773. {
  7774. TBL_PC *sd;
  7775. sd=script_rid2sd(st);
  7776. if( sd == NULL )
  7777. return 0;
  7778. clif_sendegg(sd);
  7779. return 0;
  7780. }
  7781. /*==========================================
  7782. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7783. *------------------------------------------*/
  7784. BUILDIN_FUNC(resetlvl)
  7785. {
  7786. TBL_PC *sd;
  7787. int type=script_getnum(st,2);
  7788. sd=script_rid2sd(st);
  7789. if( sd == NULL )
  7790. return 0;
  7791. pc_resetlvl(sd,type);
  7792. return 0;
  7793. }
  7794. /*==========================================
  7795. * ステータスリセット
  7796. *------------------------------------------*/
  7797. BUILDIN_FUNC(resetstatus)
  7798. {
  7799. TBL_PC *sd;
  7800. sd=script_rid2sd(st);
  7801. pc_resetstate(sd);
  7802. return 0;
  7803. }
  7804. /*==========================================
  7805. * script command resetskill
  7806. *------------------------------------------*/
  7807. BUILDIN_FUNC(resetskill)
  7808. {
  7809. TBL_PC *sd;
  7810. sd=script_rid2sd(st);
  7811. pc_resetskill(sd,1);
  7812. return 0;
  7813. }
  7814. /*==========================================
  7815. * Counts total amount of skill points.
  7816. *------------------------------------------*/
  7817. BUILDIN_FUNC(skillpointcount)
  7818. {
  7819. TBL_PC *sd;
  7820. sd=script_rid2sd(st);
  7821. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7822. return 0;
  7823. }
  7824. /*==========================================
  7825. *
  7826. *------------------------------------------*/
  7827. BUILDIN_FUNC(changebase)
  7828. {
  7829. TBL_PC *sd=NULL;
  7830. int vclass;
  7831. if( script_hasdata(st,3) )
  7832. sd=map_id2sd(script_getnum(st,3));
  7833. else
  7834. sd=script_rid2sd(st);
  7835. if(sd == NULL)
  7836. return 0;
  7837. vclass = script_getnum(st,2);
  7838. if(vclass == JOB_WEDDING)
  7839. {
  7840. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7841. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7842. )
  7843. return 0;
  7844. }
  7845. if(!sd->disguise && vclass != sd->vd.class_) {
  7846. status_set_viewdata(&sd->bl, vclass);
  7847. //Updated client view. Base, Weapon and Cloth Colors.
  7848. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7849. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7850. if (sd->vd.cloth_color)
  7851. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7852. clif_skillinfoblock(sd);
  7853. }
  7854. return 0;
  7855. }
  7856. /*==========================================
  7857. * 性別変換
  7858. *------------------------------------------*/
  7859. BUILDIN_FUNC(changesex)
  7860. {
  7861. TBL_PC *sd = NULL;
  7862. sd = script_rid2sd(st);
  7863. chrif_changesex(sd);
  7864. return 0;
  7865. }
  7866. /*==========================================
  7867. * Works like 'announce' but outputs in the common chat window
  7868. *------------------------------------------*/
  7869. BUILDIN_FUNC(globalmes)
  7870. {
  7871. struct block_list *bl = map_id2bl(st->oid);
  7872. struct npc_data *nd = (struct npc_data *)bl;
  7873. const char *name=NULL,*mes;
  7874. mes=script_getstr(st,2); // メッセージの取得
  7875. if(mes==NULL) return 0;
  7876. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7877. name=script_getstr(st,3);
  7878. } else {
  7879. name=nd->name;
  7880. }
  7881. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7882. return 0;
  7883. }
  7884. /////////////////////////////////////////////////////////////////////
  7885. // NPC waiting room (chat room)
  7886. //
  7887. /// Creates a waiting room (chat room) for this npc.
  7888. ///
  7889. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7890. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7891. /// waitingroom "<title>",<limit>,"<event>";
  7892. /// waitingroom "<title>",<limit>;
  7893. BUILDIN_FUNC(waitingroom)
  7894. {
  7895. struct npc_data* nd;
  7896. const char* title;
  7897. const char* ev = "";
  7898. int limit;
  7899. int trigger = 0;
  7900. int pub = 1;
  7901. title = script_getstr(st, 2);
  7902. limit = script_getnum(st, 3);
  7903. if( script_hasdata(st,5) )
  7904. {
  7905. struct script_data* last = script_getdata(st, 5);
  7906. get_val(st, last);
  7907. if( data_isstring(last) )
  7908. {// ,<trigger>,"<event>"
  7909. trigger = script_getnum(st, 4);
  7910. ev = script_getstr(st, 5);
  7911. }
  7912. else
  7913. {// ,"<event>",<trigger>
  7914. ev = script_getstr(st, 4);
  7915. trigger = script_getnum(st,5);
  7916. }
  7917. }
  7918. else if( script_hasdata(st,4) )
  7919. {// ,"<event>"
  7920. ev = script_getstr(st, 4);
  7921. trigger = limit;
  7922. }
  7923. nd = (struct npc_data *)map_id2bl(st->oid);
  7924. if( nd != NULL )
  7925. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7926. return 0;
  7927. }
  7928. /// Removes the waiting room of the current or target npc.
  7929. ///
  7930. /// delwaitingroom "<npc_name>";
  7931. /// delwaitingroom;
  7932. BUILDIN_FUNC(delwaitingroom)
  7933. {
  7934. struct npc_data* nd;
  7935. if( script_hasdata(st,2) )
  7936. nd = npc_name2id(script_getstr(st, 2));
  7937. else
  7938. nd = (struct npc_data *)map_id2bl(st->oid);
  7939. if( nd != NULL )
  7940. chat_deletenpcchat(nd);
  7941. return 0;
  7942. }
  7943. /// Kicks all the players from the waiting room of the current or target npc.
  7944. ///
  7945. /// kickwaitingroomall "<npc_name>";
  7946. /// kickwaitingroomall;
  7947. BUILDIN_FUNC(waitingroomkickall)
  7948. {
  7949. struct npc_data* nd;
  7950. struct chat_data* cd;
  7951. if( script_hasdata(st,2) )
  7952. nd = npc_name2id(script_getstr(st,2));
  7953. else
  7954. nd = (struct npc_data *)map_id2bl(st->oid);
  7955. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7956. chat_npckickall(cd);
  7957. return 0;
  7958. }
  7959. /// Enables the waiting room event of the current or target npc.
  7960. ///
  7961. /// enablewaitingroomevent "<npc_name>";
  7962. /// enablewaitingroomevent;
  7963. BUILDIN_FUNC(enablewaitingroomevent)
  7964. {
  7965. struct npc_data* nd;
  7966. struct chat_data* cd;
  7967. if( script_hasdata(st,2) )
  7968. nd = npc_name2id(script_getstr(st, 2));
  7969. else
  7970. nd = (struct npc_data *)map_id2bl(st->oid);
  7971. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7972. chat_enableevent(cd);
  7973. return 0;
  7974. }
  7975. /// Disables the waiting room event of the current or target npc.
  7976. ///
  7977. /// disablewaitingroomevent "<npc_name>";
  7978. /// disablewaitingroomevent;
  7979. BUILDIN_FUNC(disablewaitingroomevent)
  7980. {
  7981. struct npc_data *nd;
  7982. struct chat_data *cd;
  7983. if( script_hasdata(st,2) )
  7984. nd = npc_name2id(script_getstr(st, 2));
  7985. else
  7986. nd = (struct npc_data *)map_id2bl(st->oid);
  7987. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7988. chat_disableevent(cd);
  7989. return 0;
  7990. }
  7991. /// Returns info on the waiting room of the current or target npc.
  7992. /// Returns -1 if the type unknown
  7993. /// <type>=0 : current number of users
  7994. /// <type>=1 : maximum number of users allowed
  7995. /// <type>=2 : the number of users that trigger the event
  7996. /// <type>=3 : if the trigger is disabled
  7997. /// <type>=4 : the title of the waiting room
  7998. /// <type>=5 : the password of the waiting room
  7999. /// <type>=16 : the name of the waiting room event
  8000. /// <type>=32 : if the waiting room is full
  8001. /// <type>=33 : if there are enough users to trigger the event
  8002. ///
  8003. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8004. /// getwaitingroomstate(<type>) -> <info>
  8005. BUILDIN_FUNC(getwaitingroomstate)
  8006. {
  8007. struct npc_data *nd;
  8008. struct chat_data *cd;
  8009. int type;
  8010. type = script_getnum(st,2);
  8011. if( script_hasdata(st,3) )
  8012. nd = npc_name2id(script_getstr(st, 3));
  8013. else
  8014. nd = (struct npc_data *)map_id2bl(st->oid);
  8015. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8016. {
  8017. script_pushint(st, -1);
  8018. return 0;
  8019. }
  8020. switch(type)
  8021. {
  8022. case 0: script_pushint(st, cd->users); break;
  8023. case 1: script_pushint(st, cd->limit); break;
  8024. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8025. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8026. case 4: script_pushstrcopy(st, cd->title); break;
  8027. case 5: script_pushstrcopy(st, cd->pass); break;
  8028. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8029. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8030. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8031. default: script_pushint(st, -1); break;
  8032. }
  8033. return 0;
  8034. }
  8035. /// Warps the trigger or target amount of players to the target map and position.
  8036. /// Players are automatically removed from the waiting room.
  8037. /// Those waiting the longest will get warped first.
  8038. /// The target map can be "Random" for a random position in the current map,
  8039. /// and "SavePoint" for the savepoint map+position.
  8040. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8041. ///
  8042. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8043. /// The total number of teleported players is put into $@warpwaitingpcnum
  8044. ///
  8045. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8046. /// warpwaitingpc "<map name>",<x>,<y>;
  8047. BUILDIN_FUNC(warpwaitingpc)
  8048. {
  8049. int x;
  8050. int y;
  8051. int i;
  8052. int n;
  8053. const char* map_name;
  8054. struct npc_data* nd;
  8055. struct chat_data* cd;
  8056. TBL_PC* sd;
  8057. nd = (struct npc_data *)map_id2bl(st->oid);
  8058. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8059. return 0;
  8060. map_name = script_getstr(st,2);
  8061. x = script_getnum(st,3);
  8062. y = script_getnum(st,4);
  8063. n = cd->trigger&0x7f;
  8064. if( script_hasdata(st,5) )
  8065. n = script_getnum(st,5);
  8066. for( i = 0; i < n && cd->users > 0; i++ )
  8067. {
  8068. sd = cd->usersd[0];
  8069. if( sd == NULL )
  8070. {
  8071. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  8072. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8073. continue;// Broken npc chat room?
  8074. }
  8075. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8076. if( strcmp(map_name,"Random") == 0 )
  8077. pc_randomwarp(sd,3);
  8078. else if( strcmp(map_name,"SavePoint") == 0 )
  8079. {
  8080. if( map[sd->bl.m].flag.noteleport )
  8081. return 0;// can't teleport on this map
  8082. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  8083. }
  8084. else
  8085. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  8086. }
  8087. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8088. return 0;
  8089. }
  8090. /////////////////////////////////////////////////////////////////////
  8091. // ...
  8092. //
  8093. /// Detaches a character from a script.
  8094. ///
  8095. /// @param st Script state to detach the character from.
  8096. static void script_detach_rid(struct script_state* st)
  8097. {
  8098. if(st->rid)
  8099. {
  8100. script_detach_state(st, false);
  8101. st->rid = 0;
  8102. }
  8103. }
  8104. /*==========================================
  8105. * RIDのアタッチ
  8106. *------------------------------------------*/
  8107. BUILDIN_FUNC(attachrid)
  8108. {
  8109. int rid = script_getnum(st,2);
  8110. struct map_session_data* sd;
  8111. if ((sd = map_id2sd(rid))!=NULL) {
  8112. script_detach_rid(st);
  8113. st->rid = rid;
  8114. script_attach_state(st);
  8115. script_pushint(st,1);
  8116. } else
  8117. script_pushint(st,0);
  8118. return 0;
  8119. }
  8120. /*==========================================
  8121. * RIDのデタッチ
  8122. *------------------------------------------*/
  8123. BUILDIN_FUNC(detachrid)
  8124. {
  8125. script_detach_rid(st);
  8126. return 0;
  8127. }
  8128. /*==========================================
  8129. * 存在チェック
  8130. *------------------------------------------*/
  8131. BUILDIN_FUNC(isloggedin)
  8132. {
  8133. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8134. if (script_hasdata(st,3) && sd &&
  8135. sd->status.char_id != script_getnum(st,3))
  8136. sd = NULL;
  8137. push_val(st->stack,C_INT,sd!=NULL);
  8138. return 0;
  8139. }
  8140. /*==========================================
  8141. *
  8142. *------------------------------------------*/
  8143. BUILDIN_FUNC(setmapflagnosave)
  8144. {
  8145. int m,x,y;
  8146. unsigned short mapindex;
  8147. const char *str,*str2;
  8148. str=script_getstr(st,2);
  8149. str2=script_getstr(st,3);
  8150. x=script_getnum(st,4);
  8151. y=script_getnum(st,5);
  8152. m = map_mapname2mapid(str);
  8153. mapindex = mapindex_name2id(str2);
  8154. if(m >= 0 && mapindex) {
  8155. map[m].flag.nosave=1;
  8156. map[m].save.map=mapindex;
  8157. map[m].save.x=x;
  8158. map[m].save.y=y;
  8159. }
  8160. return 0;
  8161. }
  8162. BUILDIN_FUNC(getmapflag)
  8163. {
  8164. int m,i;
  8165. const char *str;
  8166. str=script_getstr(st,2);
  8167. i=script_getnum(st,3);
  8168. m = map_mapname2mapid(str);
  8169. if(m >= 0) {
  8170. switch(i) {
  8171. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8172. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8173. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8174. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8175. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8176. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8177. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8178. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8179. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8180. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8181. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8182. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8183. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8184. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8185. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8186. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8187. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8188. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8189. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8190. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8191. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8192. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8193. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8194. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8195. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8196. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8197. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8198. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8199. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8200. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8201. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8202. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8203. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8204. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8205. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8206. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8207. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8208. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8209. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8210. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8211. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8212. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8213. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8214. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8215. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8216. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8217. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8218. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8219. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8220. }
  8221. }
  8222. return 0;
  8223. }
  8224. BUILDIN_FUNC(setmapflag)
  8225. {
  8226. int m,i;
  8227. const char *str;
  8228. const char *val=NULL;
  8229. str=script_getstr(st,2);
  8230. i=script_getnum(st,3);
  8231. if(script_hasdata(st,4)){
  8232. val=script_getstr(st,4);
  8233. }
  8234. m = map_mapname2mapid(str);
  8235. if(m >= 0) {
  8236. switch(i) {
  8237. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8238. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8239. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8240. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8241. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8242. case MF_PVP: map[m].flag.pvp=1; break;
  8243. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8244. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8245. case MF_GVG: map[m].flag.gvg=1; break;
  8246. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8247. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8248. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8249. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8250. case MF_NODROP: map[m].flag.nodrop=1; break;
  8251. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8252. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8253. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8254. case MF_SNOW: map[m].flag.snow=1; break;
  8255. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8256. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8257. case MF_FOG: map[m].flag.fog=1; break;
  8258. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8259. case MF_SAKURA: map[m].flag.sakura=1; break;
  8260. case MF_LEAVES: map[m].flag.leaves=1; break;
  8261. case MF_RAIN: map[m].flag.rain=1; break;
  8262. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8263. case MF_NOGO: map[m].flag.nogo=1; break;
  8264. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8265. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8266. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8267. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8268. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8269. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8270. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8271. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8272. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8273. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8274. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8275. case MF_NOVENDING: map[m].flag.novending=1; break;
  8276. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8277. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8278. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8279. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8280. case MF_TOWN: map[m].flag.town=1; break;
  8281. case MF_AUTOTRADE: map[m].flag.autotrade=1; break;
  8282. case MF_ALLOWKS: map[m].flag.allowks=1; break;
  8283. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=1; break;
  8284. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=1; break;
  8285. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8286. }
  8287. }
  8288. return 0;
  8289. }
  8290. BUILDIN_FUNC(removemapflag)
  8291. {
  8292. int m,i;
  8293. const char *str;
  8294. str=script_getstr(st,2);
  8295. i=script_getnum(st,3);
  8296. m = map_mapname2mapid(str);
  8297. if(m >= 0) {
  8298. switch(i) {
  8299. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8300. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8301. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8302. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8303. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8304. case MF_PVP: map[m].flag.pvp=0; break;
  8305. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8306. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8307. case MF_GVG: map[m].flag.gvg=0; break;
  8308. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8309. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8310. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8311. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8312. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8313. case MF_NODROP: map[m].flag.nodrop=0; break;
  8314. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8315. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8316. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8317. case MF_SNOW: map[m].flag.snow=0; break;
  8318. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8319. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8320. case MF_FOG: map[m].flag.fog=0; break;
  8321. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8322. case MF_SAKURA: map[m].flag.sakura=0; break;
  8323. case MF_LEAVES: map[m].flag.leaves=0; break;
  8324. case MF_RAIN: map[m].flag.rain=0; break;
  8325. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8326. case MF_NOGO: map[m].flag.nogo=0; break;
  8327. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8328. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8329. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8330. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8331. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8332. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8333. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8334. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8335. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8336. case MF_JEXP: map[m].jexp=100; break;
  8337. case MF_BEXP: map[m].bexp=100; break;
  8338. case MF_NOVENDING: map[m].flag.novending=0; break;
  8339. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8340. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8341. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8342. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8343. case MF_TOWN: map[m].flag.town=0; break;
  8344. case MF_AUTOTRADE: map[m].flag.autotrade=0; break;
  8345. case MF_ALLOWKS: map[m].flag.allowks=0; break;
  8346. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=0; break;
  8347. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=0; break;
  8348. case MF_BATTLEGROUND: map[m].flag.battleground=0; break;
  8349. }
  8350. }
  8351. return 0;
  8352. }
  8353. BUILDIN_FUNC(pvpon)
  8354. {
  8355. int m;
  8356. const char *str;
  8357. TBL_PC* sd = NULL;
  8358. struct s_mapiterator* iter;
  8359. str = script_getstr(st,2);
  8360. m = map_mapname2mapid(str);
  8361. if( m < 0 || map[m].flag.pvp )
  8362. return 0; // nothing to do
  8363. map[m].flag.pvp = 1;
  8364. clif_send0199(m,1);
  8365. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8366. return 0;
  8367. iter = mapit_getallusers();
  8368. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8369. {
  8370. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8371. continue; // not applicable
  8372. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8373. sd->pvp_rank = 0;
  8374. sd->pvp_lastusers = 0;
  8375. sd->pvp_point = 5;
  8376. sd->pvp_won = 0;
  8377. sd->pvp_lost = 0;
  8378. }
  8379. mapit_free(iter);
  8380. return 0;
  8381. }
  8382. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8383. {
  8384. TBL_PC* sd = (TBL_PC*)bl;
  8385. clif_pvpset(sd, 0, 0, 2);
  8386. if (sd->pvp_timer != -1) {
  8387. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8388. sd->pvp_timer = INVALID_TIMER;
  8389. }
  8390. return 0;
  8391. }
  8392. BUILDIN_FUNC(pvpoff)
  8393. {
  8394. int m;
  8395. const char *str;
  8396. str=script_getstr(st,2);
  8397. m = map_mapname2mapid(str);
  8398. if(m < 0 || !map[m].flag.pvp)
  8399. return 0; //fixed Lupus
  8400. map[m].flag.pvp = 0;
  8401. clif_send0199(m,0);
  8402. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8403. return 0;
  8404. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8405. return 0;
  8406. }
  8407. BUILDIN_FUNC(gvgon)
  8408. {
  8409. int m;
  8410. const char *str;
  8411. str=script_getstr(st,2);
  8412. m = map_mapname2mapid(str);
  8413. if(m >= 0 && !map[m].flag.gvg) {
  8414. map[m].flag.gvg = 1;
  8415. clif_send0199(m,3);
  8416. }
  8417. return 0;
  8418. }
  8419. BUILDIN_FUNC(gvgoff)
  8420. {
  8421. int m;
  8422. const char *str;
  8423. str=script_getstr(st,2);
  8424. m = map_mapname2mapid(str);
  8425. if(m >= 0 && map[m].flag.gvg) {
  8426. map[m].flag.gvg = 0;
  8427. clif_send0199(m,0);
  8428. }
  8429. return 0;
  8430. }
  8431. /*==========================================
  8432. * Shows an emoticon on top of the player/npc
  8433. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8434. *------------------------------------------*/
  8435. //Optional second parameter added by [Skotlex]
  8436. BUILDIN_FUNC(emotion)
  8437. {
  8438. int type;
  8439. int player=0;
  8440. type=script_getnum(st,2);
  8441. if(type < 0 || type > 100)
  8442. return 0;
  8443. if( script_hasdata(st,3) )
  8444. player=script_getnum(st,3);
  8445. if (player) {
  8446. TBL_PC *sd = NULL;
  8447. if( script_hasdata(st,4) )
  8448. sd = map_nick2sd(script_getstr(st,4));
  8449. else
  8450. sd = script_rid2sd(st);
  8451. if (sd)
  8452. clif_emotion(&sd->bl,type);
  8453. } else
  8454. if( script_hasdata(st,4) )
  8455. {
  8456. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8457. if(nd)
  8458. clif_emotion(&nd->bl,type);
  8459. }
  8460. else
  8461. clif_emotion(map_id2bl(st->oid),type);
  8462. return 0;
  8463. }
  8464. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8465. {
  8466. int m=va_arg(ap,int);
  8467. int g_id=va_arg(ap,int);
  8468. int flag=va_arg(ap,int);
  8469. if(!sd || sd->bl.m != m)
  8470. return 0;
  8471. if(
  8472. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8473. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8474. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8475. )
  8476. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8477. return 1;
  8478. }
  8479. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8480. {
  8481. struct mob_data *md=(struct mob_data *)bl;
  8482. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8483. status_kill(bl);
  8484. return 0;
  8485. }
  8486. BUILDIN_FUNC(maprespawnguildid)
  8487. {
  8488. const char *mapname=script_getstr(st,2);
  8489. int g_id=script_getnum(st,3);
  8490. int flag=script_getnum(st,4);
  8491. int m=map_mapname2mapid(mapname);
  8492. if(m == -1)
  8493. return 0;
  8494. //Catch ALL players (in case some are 'between maps' on execution time)
  8495. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8496. if (flag&4) //Remove script mobs.
  8497. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8498. return 0;
  8499. }
  8500. BUILDIN_FUNC(agitstart)
  8501. {
  8502. if(agit_flag==1) return 0; // Agit already Start.
  8503. agit_flag=1;
  8504. guild_agit_start();
  8505. return 0;
  8506. }
  8507. BUILDIN_FUNC(agitend)
  8508. {
  8509. if(agit_flag==0) return 0; // Agit already End.
  8510. agit_flag=0;
  8511. guild_agit_end();
  8512. return 0;
  8513. }
  8514. BUILDIN_FUNC(agitstart2)
  8515. {
  8516. if(agit2_flag==1) return 0; // Agit2 already Start.
  8517. agit2_flag=1;
  8518. guild_agit2_start();
  8519. return 0;
  8520. }
  8521. BUILDIN_FUNC(agitend2)
  8522. {
  8523. if(agit2_flag==0) return 0; // Agit2 already End.
  8524. agit2_flag=0;
  8525. guild_agit2_end();
  8526. return 0;
  8527. }
  8528. /*==========================================
  8529. * Returns whether woe is on or off. // choice script
  8530. *------------------------------------------*/
  8531. BUILDIN_FUNC(agitcheck)
  8532. {
  8533. script_pushint(st,agit_flag);
  8534. return 0;
  8535. }
  8536. /*==========================================
  8537. * Returns whether woese is on or off. // choice script
  8538. *------------------------------------------*/
  8539. BUILDIN_FUNC(agitcheck2)
  8540. {
  8541. script_pushint(st,agit2_flag);
  8542. return 0;
  8543. }
  8544. /// Sets the guild_id of this npc.
  8545. ///
  8546. /// flagemblem <guild_id>;
  8547. BUILDIN_FUNC(flagemblem)
  8548. {
  8549. TBL_NPC* nd;
  8550. int g_id=script_getnum(st,2);
  8551. if(g_id < 0) return 0;
  8552. nd = (TBL_NPC*)map_id2nd(st->oid);
  8553. if( nd == NULL )
  8554. {
  8555. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8556. }
  8557. else if( nd->subtype != SCRIPT )
  8558. {
  8559. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8560. }
  8561. else
  8562. {
  8563. nd->u.scr.guild_id = g_id;
  8564. clif_guild_emblem_area(&nd->bl);
  8565. }
  8566. return 0;
  8567. }
  8568. BUILDIN_FUNC(getcastlename)
  8569. {
  8570. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8571. struct guild_castle* gc = guild_mapname2gc(mapname);
  8572. const char* name = (gc) ? gc->castle_name : "";
  8573. script_pushstrcopy(st,name);
  8574. return 0;
  8575. }
  8576. BUILDIN_FUNC(getcastledata)
  8577. {
  8578. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8579. int index = script_getnum(st,3);
  8580. struct guild_castle* gc = guild_mapname2gc(mapname);
  8581. if(script_hasdata(st,4) && index==0 && gc) {
  8582. const char* event = script_getstr(st,4);
  8583. check_event(st, event);
  8584. guild_addcastleinfoevent(gc->castle_id,17,event);
  8585. }
  8586. if(gc){
  8587. switch(index){
  8588. case 0: {
  8589. int i;
  8590. for(i=1;i<18;i++) // Initialize[AgitInit]
  8591. guild_castledataload(gc->castle_id,i);
  8592. } break;
  8593. case 1:
  8594. script_pushint(st,gc->guild_id); break;
  8595. case 2:
  8596. script_pushint(st,gc->economy); break;
  8597. case 3:
  8598. script_pushint(st,gc->defense); break;
  8599. case 4:
  8600. script_pushint(st,gc->triggerE); break;
  8601. case 5:
  8602. script_pushint(st,gc->triggerD); break;
  8603. case 6:
  8604. script_pushint(st,gc->nextTime); break;
  8605. case 7:
  8606. script_pushint(st,gc->payTime); break;
  8607. case 8:
  8608. script_pushint(st,gc->createTime); break;
  8609. case 9:
  8610. script_pushint(st,gc->visibleC); break;
  8611. case 10:
  8612. case 11:
  8613. case 12:
  8614. case 13:
  8615. case 14:
  8616. case 15:
  8617. case 16:
  8618. case 17:
  8619. script_pushint(st,gc->guardian[index-10].visible); break;
  8620. default:
  8621. script_pushint(st,0); break;
  8622. }
  8623. return 0;
  8624. }
  8625. script_pushint(st,0);
  8626. return 0;
  8627. }
  8628. BUILDIN_FUNC(setcastledata)
  8629. {
  8630. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8631. int index = script_getnum(st,3);
  8632. int value = script_getnum(st,4);
  8633. struct guild_castle* gc = guild_mapname2gc(mapname);
  8634. if(gc) {
  8635. // Save Data byself First
  8636. switch(index){
  8637. case 1:
  8638. gc->guild_id = value; break;
  8639. case 2:
  8640. gc->economy = value; break;
  8641. case 3:
  8642. gc->defense = value; break;
  8643. case 4:
  8644. gc->triggerE = value; break;
  8645. case 5:
  8646. gc->triggerD = value; break;
  8647. case 6:
  8648. gc->nextTime = value; break;
  8649. case 7:
  8650. gc->payTime = value; break;
  8651. case 8:
  8652. gc->createTime = value; break;
  8653. case 9:
  8654. gc->visibleC = value; break;
  8655. case 10:
  8656. case 11:
  8657. case 12:
  8658. case 13:
  8659. case 14:
  8660. case 15:
  8661. case 16:
  8662. case 17:
  8663. gc->guardian[index-10].visible = value; break;
  8664. default:
  8665. return 0;
  8666. }
  8667. guild_castledatasave(gc->castle_id,index,value);
  8668. }
  8669. return 0;
  8670. }
  8671. /* =====================================================================
  8672. * ギルド情報を要求する
  8673. * ---------------------------------------------------------------------*/
  8674. BUILDIN_FUNC(requestguildinfo)
  8675. {
  8676. int guild_id=script_getnum(st,2);
  8677. const char *event=NULL;
  8678. if( script_hasdata(st,3) ){
  8679. event=script_getstr(st,3);
  8680. check_event(st, event);
  8681. }
  8682. if(guild_id>0)
  8683. guild_npc_request_info(guild_id,event);
  8684. return 0;
  8685. }
  8686. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8687. /// getequipcardcnt(<equipment slot>);
  8688. BUILDIN_FUNC(getequipcardcnt)
  8689. {
  8690. int i=-1,j,num;
  8691. TBL_PC *sd;
  8692. int count;
  8693. num=script_getnum(st,2);
  8694. sd=script_rid2sd(st);
  8695. if (num > 0 && num <= ARRAYLENGTH(equip))
  8696. i=pc_checkequip(sd,equip[num-1]);
  8697. if (i < 0 || !sd->inventory_data[i]) {
  8698. script_pushint(st,0);
  8699. return 0;
  8700. }
  8701. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8702. {
  8703. script_pushint(st,0);
  8704. return 0;
  8705. }
  8706. count = 0;
  8707. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  8708. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8709. count++;
  8710. script_pushint(st,count);
  8711. return 0;
  8712. }
  8713. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8714. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8715. /// successremovecards <slot>;
  8716. BUILDIN_FUNC(successremovecards)
  8717. {
  8718. int i=-1,j,c,cardflag=0;
  8719. TBL_PC* sd = script_rid2sd(st);
  8720. int num = script_getnum(st,2);
  8721. if (num > 0 && num <= ARRAYLENGTH(equip))
  8722. i=pc_checkequip(sd,equip[num-1]);
  8723. if (i < 0 || !sd->inventory_data[i]) {
  8724. return 0;
  8725. }
  8726. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8727. return 0;
  8728. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8729. {
  8730. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8731. {// extract this card from the item
  8732. int flag;
  8733. struct item item_tmp;
  8734. cardflag = 1;
  8735. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8736. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8737. item_tmp.attribute=0,item_tmp.expire_time=0;
  8738. for (j = 0; j < MAX_SLOTS; j++)
  8739. item_tmp.card[j]=0;
  8740. //Logs items, got from (N)PC scripts [Lupus]
  8741. if(log_config.enable_logs&0x40)
  8742. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8743. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8744. clif_additem(sd,0,0,flag);
  8745. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8746. }
  8747. }
  8748. }
  8749. if(cardflag == 1)
  8750. { // カードを取り除いたアイテム所得
  8751. int flag;
  8752. struct item item_tmp;
  8753. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8754. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8755. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8756. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  8757. item_tmp.card[j]=0;
  8758. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  8759. item_tmp.card[j]=sd->status.inventory[i].card[j];
  8760. //Logs items, got from (N)PC scripts [Lupus]
  8761. if(log_config.enable_logs&0x40)
  8762. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8763. pc_delitem(sd,i,1,0,3);
  8764. //Logs items, got from (N)PC scripts [Lupus]
  8765. if(log_config.enable_logs&0x40)
  8766. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8767. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8768. clif_additem(sd,0,0,flag);
  8769. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8770. }
  8771. clif_misceffect(&sd->bl,3);
  8772. }
  8773. return 0;
  8774. }
  8775. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8776. /// failedremovecards <slot>, <type>;
  8777. /// <type>=0 : will destroy both the item and the cards.
  8778. /// <type>=1 : will keep the item, but destroy the cards.
  8779. /// <type>=2 : will keep the cards, but destroy the item.
  8780. /// <type>=? : will just display the failure effect.
  8781. BUILDIN_FUNC(failedremovecards)
  8782. {
  8783. int i=-1,j,c,cardflag=0;
  8784. TBL_PC* sd = script_rid2sd(st);
  8785. int num = script_getnum(st,2);
  8786. int typefail = script_getnum(st,3);
  8787. if (num > 0 && num <= ARRAYLENGTH(equip))
  8788. i=pc_checkequip(sd,equip[num-1]);
  8789. if (i < 0 || !sd->inventory_data[i])
  8790. return 0;
  8791. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8792. return 0;
  8793. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8794. {
  8795. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8796. {
  8797. cardflag = 1;
  8798. if(typefail == 2)
  8799. {// add cards to inventory, clear
  8800. int flag;
  8801. struct item item_tmp;
  8802. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8803. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8804. item_tmp.attribute=0,item_tmp.expire_time=0;
  8805. for (j = 0; j < MAX_SLOTS; j++)
  8806. item_tmp.card[j]=0;
  8807. //Logs items, got from (N)PC scripts [Lupus]
  8808. if(log_config.enable_logs&0x40)
  8809. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8810. if((flag=pc_additem(sd,&item_tmp,1))){
  8811. clif_additem(sd,0,0,flag);
  8812. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8813. }
  8814. }
  8815. }
  8816. }
  8817. if(cardflag == 1)
  8818. {
  8819. if(typefail == 0 || typefail == 2){ // 武具損失
  8820. //Logs items, got from (N)PC scripts [Lupus]
  8821. if(log_config.enable_logs&0x40)
  8822. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8823. pc_delitem(sd,i,1,0,2);
  8824. }
  8825. if(typefail == 1){ // カードのみ損失(武具を返す)
  8826. int flag;
  8827. struct item item_tmp;
  8828. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8829. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8830. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8831. //Logs items, got from (N)PC scripts [Lupus]
  8832. if(log_config.enable_logs&0x40)
  8833. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8834. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  8835. item_tmp.card[j]=0;
  8836. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  8837. item_tmp.card[j]=sd->status.inventory[i].card[j];
  8838. pc_delitem(sd,i,1,0,2);
  8839. //Logs items, got from (N)PC scripts [Lupus]
  8840. if(log_config.enable_logs&0x40)
  8841. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8842. if((flag=pc_additem(sd,&item_tmp,1))){
  8843. clif_additem(sd,0,0,flag);
  8844. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8845. }
  8846. }
  8847. clif_misceffect(&sd->bl,2);
  8848. }
  8849. return 0;
  8850. }
  8851. /* ================================================================
  8852. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8853. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8854. * improved by [Lance]
  8855. * ================================================================*/
  8856. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8857. {
  8858. int x,y,m,check_val=0,check_ID=0,i=0;
  8859. struct guild *g = NULL;
  8860. struct party_data *p = NULL;
  8861. const char *str;
  8862. const char *mapname;
  8863. unsigned int index;
  8864. mapname=script_getstr(st,2);
  8865. str=script_getstr(st,3);
  8866. x=script_getnum(st,4);
  8867. y=script_getnum(st,5);
  8868. if(script_hasdata(st,7)){
  8869. check_val=script_getnum(st,6);
  8870. check_ID=script_getnum(st,7);
  8871. }
  8872. if((m=map_mapname2mapid(mapname))< 0)
  8873. return 0;
  8874. if(!(index=mapindex_name2id(str)))
  8875. return 0;
  8876. switch(check_val){
  8877. case 1:
  8878. g = guild_search(check_ID);
  8879. if (g){
  8880. for( i=0; i < g->max_member; i++)
  8881. {
  8882. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8883. pc_setpos(g->member[i].sd,index,x,y,3);
  8884. }
  8885. }
  8886. }
  8887. break;
  8888. case 2:
  8889. p = party_search(check_ID);
  8890. if(p){
  8891. for(i=0;i<MAX_PARTY; i++){
  8892. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8893. pc_setpos(p->data[i].sd,index,x,y,3);
  8894. }
  8895. }
  8896. }
  8897. break;
  8898. default:
  8899. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8900. break;
  8901. }
  8902. return 0;
  8903. }
  8904. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8905. {
  8906. char *event=va_arg(ap,char *);
  8907. struct mob_data *md = ((struct mob_data *)bl);
  8908. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  8909. return 1;
  8910. return 0;
  8911. }
  8912. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8913. {
  8914. const char *mapname,*event;
  8915. int m;
  8916. mapname=script_getstr(st,2);
  8917. event=script_getstr(st,3);
  8918. check_event(st, event);
  8919. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  8920. script_pushint(st,-1);
  8921. return 0;
  8922. }
  8923. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  8924. {
  8925. script_pushint(st,-1);
  8926. return 0;
  8927. }
  8928. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8929. return 0;
  8930. }
  8931. BUILDIN_FUNC(marriage)
  8932. {
  8933. const char *partner=script_getstr(st,2);
  8934. TBL_PC *sd=script_rid2sd(st);
  8935. TBL_PC *p_sd=map_nick2sd(partner);
  8936. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8937. script_pushint(st,0);
  8938. return 0;
  8939. }
  8940. script_pushint(st,1);
  8941. return 0;
  8942. }
  8943. BUILDIN_FUNC(wedding_effect)
  8944. {
  8945. TBL_PC *sd=script_rid2sd(st);
  8946. struct block_list *bl;
  8947. if(sd==NULL) {
  8948. bl=map_id2bl(st->oid);
  8949. } else
  8950. bl=&sd->bl;
  8951. clif_wedding_effect(bl);
  8952. return 0;
  8953. }
  8954. BUILDIN_FUNC(divorce)
  8955. {
  8956. TBL_PC *sd=script_rid2sd(st);
  8957. if(sd==NULL || pc_divorce(sd) < 0){
  8958. script_pushint(st,0);
  8959. return 0;
  8960. }
  8961. script_pushint(st,1);
  8962. return 0;
  8963. }
  8964. BUILDIN_FUNC(ispartneron)
  8965. {
  8966. TBL_PC *sd=script_rid2sd(st);
  8967. if(sd==NULL || !pc_ismarried(sd) ||
  8968. map_charid2sd(sd->status.partner_id) == NULL) {
  8969. script_pushint(st,0);
  8970. return 0;
  8971. }
  8972. script_pushint(st,1);
  8973. return 0;
  8974. }
  8975. BUILDIN_FUNC(getpartnerid)
  8976. {
  8977. TBL_PC *sd=script_rid2sd(st);
  8978. if (sd == NULL) {
  8979. script_pushint(st,0);
  8980. return 0;
  8981. }
  8982. script_pushint(st,sd->status.partner_id);
  8983. return 0;
  8984. }
  8985. BUILDIN_FUNC(getchildid)
  8986. {
  8987. TBL_PC *sd=script_rid2sd(st);
  8988. if (sd == NULL) {
  8989. script_pushint(st,0);
  8990. return 0;
  8991. }
  8992. script_pushint(st,sd->status.child);
  8993. return 0;
  8994. }
  8995. BUILDIN_FUNC(getmotherid)
  8996. {
  8997. TBL_PC *sd=script_rid2sd(st);
  8998. if (sd == NULL) {
  8999. script_pushint(st,0);
  9000. return 0;
  9001. }
  9002. script_pushint(st,sd->status.mother);
  9003. return 0;
  9004. }
  9005. BUILDIN_FUNC(getfatherid)
  9006. {
  9007. TBL_PC *sd=script_rid2sd(st);
  9008. if (sd == NULL) {
  9009. script_pushint(st,0);
  9010. return 0;
  9011. }
  9012. script_pushint(st,sd->status.father);
  9013. return 0;
  9014. }
  9015. BUILDIN_FUNC(warppartner)
  9016. {
  9017. int x,y;
  9018. unsigned short mapindex;
  9019. const char *str;
  9020. TBL_PC *sd=script_rid2sd(st);
  9021. TBL_PC *p_sd=NULL;
  9022. if(sd==NULL || !pc_ismarried(sd) ||
  9023. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9024. script_pushint(st,0);
  9025. return 0;
  9026. }
  9027. str=script_getstr(st,2);
  9028. x=script_getnum(st,3);
  9029. y=script_getnum(st,4);
  9030. mapindex = mapindex_name2id(str);
  9031. if (mapindex) {
  9032. pc_setpos(p_sd,mapindex,x,y,0);
  9033. script_pushint(st,1);
  9034. } else
  9035. script_pushint(st,0);
  9036. return 0;
  9037. }
  9038. /*================================================
  9039. * Script for Displaying MOB Information [Valaris]
  9040. *------------------------------------------------*/
  9041. BUILDIN_FUNC(strmobinfo)
  9042. {
  9043. int num=script_getnum(st,2);
  9044. int class_=script_getnum(st,3);
  9045. if(!mobdb_checkid(class_))
  9046. {
  9047. script_pushint(st,0);
  9048. return 0;
  9049. }
  9050. switch (num) {
  9051. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9052. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9053. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9054. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9055. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9056. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9057. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9058. default:
  9059. script_pushint(st,0);
  9060. break;
  9061. }
  9062. return 0;
  9063. }
  9064. /*==========================================
  9065. * Summon guardians [Valaris]
  9066. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9067. *------------------------------------------*/
  9068. BUILDIN_FUNC(guardian)
  9069. {
  9070. int class_=0,x=0,y=0,guardian=0;
  9071. const char *str,*map,*evt="";
  9072. struct script_data *data;
  9073. bool has_index = false;
  9074. map =script_getstr(st,2);
  9075. x =script_getnum(st,3);
  9076. y =script_getnum(st,4);
  9077. str =script_getstr(st,5);
  9078. class_=script_getnum(st,6);
  9079. if( script_hasdata(st,8) )
  9080. {// "<event label>",<guardian index>
  9081. evt=script_getstr(st,7);
  9082. guardian=script_getnum(st,8);
  9083. has_index = true;
  9084. } else if( script_hasdata(st,7) ){
  9085. data=script_getdata(st,7);
  9086. get_val(st,data);
  9087. if( data_isstring(data) )
  9088. {// "<event label>"
  9089. evt=script_getstr(st,7);
  9090. } else if( data_isint(data) )
  9091. {// <guardian index>
  9092. guardian=script_getnum(st,7);
  9093. has_index = true;
  9094. } else {
  9095. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9096. script_reportdata(data);
  9097. return 1;
  9098. }
  9099. }
  9100. check_event(st, evt);
  9101. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9102. return 0;
  9103. }
  9104. /*==========================================
  9105. * Invisible Walls [Zephyrus]
  9106. *------------------------------------------*/
  9107. BUILDIN_FUNC(setwall)
  9108. {
  9109. const char *map, *name;
  9110. int x, y, m, size, dir;
  9111. bool shootable;
  9112. map = script_getstr(st,2);
  9113. x = script_getnum(st,3);
  9114. y = script_getnum(st,4);
  9115. size = script_getnum(st,5);
  9116. dir = script_getnum(st,6);
  9117. shootable = script_getnum(st,7);
  9118. name = script_getstr(st,8);
  9119. if( (m = map_mapname2mapid(map)) < 0 )
  9120. return 0; // Invalid Map
  9121. map_iwall_set(m, x, y, size, dir, shootable, name);
  9122. return 0;
  9123. }
  9124. BUILDIN_FUNC(delwall)
  9125. {
  9126. const char *name = script_getstr(st,2);
  9127. map_iwall_remove(name);
  9128. return 0;
  9129. }
  9130. /// Retrieves various information about the specified guardian.
  9131. ///
  9132. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9133. /// type: 0 - whether it is deployed or not
  9134. /// 1 - maximum hp
  9135. /// 2 - current hp
  9136. ///
  9137. BUILDIN_FUNC(guardianinfo)
  9138. {
  9139. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9140. int id = script_getnum(st,3);
  9141. int type = script_getnum(st,4);
  9142. struct guild_castle* gc = guild_mapname2gc(mapname);
  9143. struct mob_data* gd;
  9144. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9145. {
  9146. script_pushint(st,-1);
  9147. return 0;
  9148. }
  9149. if( type == 0 )
  9150. script_pushint(st, gc->guardian[id].visible);
  9151. else
  9152. if( !gc->guardian[id].visible )
  9153. script_pushint(st,-1);
  9154. else
  9155. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9156. script_pushint(st,-1);
  9157. else
  9158. {
  9159. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9160. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9161. else
  9162. script_pushint(st,-1);
  9163. }
  9164. return 0;
  9165. }
  9166. /*==========================================
  9167. * IDからItem名
  9168. *------------------------------------------*/
  9169. BUILDIN_FUNC(getitemname)
  9170. {
  9171. int item_id=0;
  9172. struct item_data *i_data;
  9173. char *item_name;
  9174. struct script_data *data;
  9175. data=script_getdata(st,2);
  9176. get_val(st,data);
  9177. if( data_isstring(data) ){
  9178. const char *name=conv_str(st,data);
  9179. struct item_data *item_data = itemdb_searchname(name);
  9180. if( item_data )
  9181. item_id=item_data->nameid;
  9182. }else
  9183. item_id=conv_num(st,data);
  9184. i_data = itemdb_exists(item_id);
  9185. if (i_data == NULL)
  9186. {
  9187. script_pushconststr(st,"null");
  9188. return 0;
  9189. }
  9190. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9191. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9192. script_pushstr(st,item_name);
  9193. return 0;
  9194. }
  9195. /*==========================================
  9196. * Returns number of slots an item has. [Skotlex]
  9197. *------------------------------------------*/
  9198. BUILDIN_FUNC(getitemslots)
  9199. {
  9200. int item_id;
  9201. struct item_data *i_data;
  9202. item_id=script_getnum(st,2);
  9203. i_data = itemdb_exists(item_id);
  9204. if (i_data)
  9205. script_pushint(st,i_data->slot);
  9206. else
  9207. script_pushint(st,-1);
  9208. return 0;
  9209. }
  9210. /*==========================================
  9211. * Returns some values of an item [Lupus]
  9212. * Price, Weight, etc...
  9213. getiteminfo(itemID,n), where n
  9214. 0 value_buy;
  9215. 1 value_sell;
  9216. 2 type;
  9217. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9218. if = 0, then monsters don't drop it at all (rare or a quest item)
  9219. if = 10000, then this item is sold in NPC shops only
  9220. 4 sex;
  9221. 5 equip;
  9222. 6 weight;
  9223. 7 atk;
  9224. 8 def;
  9225. 9 range;
  9226. 10 slot;
  9227. 11 look;
  9228. 12 elv;
  9229. 13 wlv;
  9230. 14 view id
  9231. *------------------------------------------*/
  9232. BUILDIN_FUNC(getiteminfo)
  9233. {
  9234. int item_id,n;
  9235. int *item_arr;
  9236. struct item_data *i_data;
  9237. item_id = script_getnum(st,2);
  9238. n = script_getnum(st,3);
  9239. i_data = itemdb_exists(item_id);
  9240. if (i_data && n>=0 && n<=14) {
  9241. item_arr = (int*)&i_data->value_buy;
  9242. script_pushint(st,item_arr[n]);
  9243. } else
  9244. script_pushint(st,-1);
  9245. return 0;
  9246. }
  9247. /*==========================================
  9248. * Set some values of an item [Lupus]
  9249. * Price, Weight, etc...
  9250. setiteminfo(itemID,n,Value), where n
  9251. 0 value_buy;
  9252. 1 value_sell;
  9253. 2 type;
  9254. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9255. if = 0, then monsters don't drop it at all (rare or a quest item)
  9256. if = 10000, then this item is sold in NPC shops only
  9257. 4 sex;
  9258. 5 equip;
  9259. 6 weight;
  9260. 7 atk;
  9261. 8 def;
  9262. 9 range;
  9263. 10 slot;
  9264. 11 look;
  9265. 12 elv;
  9266. 13 wlv;
  9267. 14 view id
  9268. * Returns Value or -1 if the wrong field's been set
  9269. *------------------------------------------*/
  9270. BUILDIN_FUNC(setiteminfo)
  9271. {
  9272. int item_id,n,value;
  9273. int *item_arr;
  9274. struct item_data *i_data;
  9275. item_id = script_getnum(st,2);
  9276. n = script_getnum(st,3);
  9277. value = script_getnum(st,4);
  9278. i_data = itemdb_exists(item_id);
  9279. if (i_data && n>=0 && n<=14) {
  9280. item_arr = (int*)&i_data->value_buy;
  9281. item_arr[n] = value;
  9282. script_pushint(st,value);
  9283. } else
  9284. script_pushint(st,-1);
  9285. return 0;
  9286. }
  9287. /*==========================================
  9288. * Returns value from equipped item slot n [Lupus]
  9289. getequipcardid(num,slot)
  9290. where
  9291. num = eqip position slot
  9292. slot = 0,1,2,3 (Card Slot N)
  9293. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9294. it's useful when you want to check item cards or if it's signed
  9295. Useful for such quests as "Sign this refined item with players name" etc
  9296. Hat[0] +4 -> Player's Hat[0] +4
  9297. *------------------------------------------*/
  9298. BUILDIN_FUNC(getequipcardid)
  9299. {
  9300. int i=-1,num,slot;
  9301. TBL_PC *sd;
  9302. num=script_getnum(st,2);
  9303. slot=script_getnum(st,3);
  9304. sd=script_rid2sd(st);
  9305. if (num > 0 && num <= ARRAYLENGTH(equip))
  9306. i=pc_checkequip(sd,equip[num-1]);
  9307. if(i >= 0 && slot>=0 && slot<4)
  9308. script_pushint(st,sd->status.inventory[i].card[slot]);
  9309. else
  9310. script_pushint(st,0);
  9311. return 0;
  9312. }
  9313. /*==========================================
  9314. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9315. *------------------------------------------*/
  9316. BUILDIN_FUNC(petskillbonus)
  9317. {
  9318. struct pet_data *pd;
  9319. TBL_PC *sd=script_rid2sd(st);
  9320. if(sd==NULL || sd->pd==NULL)
  9321. return 0;
  9322. pd=sd->pd;
  9323. if (pd->bonus)
  9324. { //Clear previous bonus
  9325. if (pd->bonus->timer != -1)
  9326. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9327. } else //init
  9328. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9329. pd->bonus->type=script_getnum(st,2);
  9330. pd->bonus->val=script_getnum(st,3);
  9331. pd->bonus->duration=script_getnum(st,4);
  9332. pd->bonus->delay=script_getnum(st,5);
  9333. if (pd->state.skillbonus == 1)
  9334. pd->state.skillbonus=0; // waiting state
  9335. // wait for timer to start
  9336. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9337. pd->bonus->timer = INVALID_TIMER;
  9338. else
  9339. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9340. return 0;
  9341. }
  9342. /*==========================================
  9343. * pet looting [Valaris] //Rewritten by [Skotlex]
  9344. *------------------------------------------*/
  9345. BUILDIN_FUNC(petloot)
  9346. {
  9347. int max;
  9348. struct pet_data *pd;
  9349. TBL_PC *sd=script_rid2sd(st);
  9350. if(sd==NULL || sd->pd==NULL)
  9351. return 0;
  9352. max=script_getnum(st,2);
  9353. if(max < 1)
  9354. max = 1; //Let'em loot at least 1 item.
  9355. else if (max > MAX_PETLOOT_SIZE)
  9356. max = MAX_PETLOOT_SIZE;
  9357. pd = sd->pd;
  9358. if (pd->loot != NULL)
  9359. { //Release whatever was there already and reallocate memory
  9360. pet_lootitem_drop(pd, pd->msd);
  9361. aFree(pd->loot->item);
  9362. }
  9363. else
  9364. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9365. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9366. pd->loot->max=max;
  9367. pd->loot->count = 0;
  9368. pd->loot->weight = 0;
  9369. return 0;
  9370. }
  9371. /*==========================================
  9372. * PCの所持品情報読み取り
  9373. *------------------------------------------*/
  9374. BUILDIN_FUNC(getinventorylist)
  9375. {
  9376. TBL_PC *sd=script_rid2sd(st);
  9377. char card_var[NAME_LENGTH];
  9378. int i,j=0,k;
  9379. if(!sd) return 0;
  9380. for(i=0;i<MAX_INVENTORY;i++){
  9381. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9382. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9383. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9384. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9385. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9386. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9387. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9388. for (k = 0; k < MAX_SLOTS; k++)
  9389. {
  9390. sprintf(card_var, "@inventorylist_card%d",k+1);
  9391. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9392. }
  9393. pc_setreg(sd,add_str("@inventorylist_expire")+(j<<24),sd->status.inventory[i].expire_time);
  9394. j++;
  9395. }
  9396. }
  9397. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9398. return 0;
  9399. }
  9400. BUILDIN_FUNC(getskilllist)
  9401. {
  9402. TBL_PC *sd=script_rid2sd(st);
  9403. int i,j=0;
  9404. if(!sd) return 0;
  9405. for(i=0;i<MAX_SKILL;i++){
  9406. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9407. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9408. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9409. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9410. j++;
  9411. }
  9412. }
  9413. pc_setreg(sd,add_str("@skilllist_count"),j);
  9414. return 0;
  9415. }
  9416. BUILDIN_FUNC(clearitem)
  9417. {
  9418. TBL_PC *sd=script_rid2sd(st);
  9419. int i;
  9420. if(sd==NULL) return 0;
  9421. for (i=0; i<MAX_INVENTORY; i++) {
  9422. if (sd->status.inventory[i].amount) {
  9423. //Logs items, got from (N)PC scripts [Lupus]
  9424. if(log_config.enable_logs&0x40)
  9425. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9426. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0);
  9427. }
  9428. }
  9429. return 0;
  9430. }
  9431. /*==========================================
  9432. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9433. *------------------------------------------*/
  9434. BUILDIN_FUNC(disguise)
  9435. {
  9436. int id;
  9437. TBL_PC* sd = script_rid2sd(st);
  9438. if (sd == NULL) return 0;
  9439. id = script_getnum(st,2);
  9440. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9441. pc_disguise(sd, id);
  9442. script_pushint(st,id);
  9443. } else
  9444. script_pushint(st,0);
  9445. return 0;
  9446. }
  9447. /*==========================================
  9448. * Undisguise Player (returns 1 if success, 0 on fail)
  9449. *------------------------------------------*/
  9450. BUILDIN_FUNC(undisguise)
  9451. {
  9452. TBL_PC* sd = script_rid2sd(st);
  9453. if (sd == NULL) return 0;
  9454. if (sd->disguise) {
  9455. pc_disguise(sd, 0);
  9456. script_pushint(st,0);
  9457. } else {
  9458. script_pushint(st,1);
  9459. }
  9460. return 0;
  9461. }
  9462. /*==========================================
  9463. * NPCクラスチェンジ
  9464. * classは変わりたいclass
  9465. * typeは通常0なのかな?
  9466. *------------------------------------------*/
  9467. BUILDIN_FUNC(classchange)
  9468. {
  9469. int _class,type;
  9470. struct block_list *bl=map_id2bl(st->oid);
  9471. if(bl==NULL) return 0;
  9472. _class=script_getnum(st,2);
  9473. type=script_getnum(st,3);
  9474. clif_class_change(bl,_class,type);
  9475. return 0;
  9476. }
  9477. /*==========================================
  9478. * NPCから発生するエフェクト
  9479. *------------------------------------------*/
  9480. BUILDIN_FUNC(misceffect)
  9481. {
  9482. int type;
  9483. type=script_getnum(st,2);
  9484. if(st->oid && st->oid != fake_nd->bl.id) {
  9485. struct block_list *bl = map_id2bl(st->oid);
  9486. if (bl)
  9487. clif_misceffect2(bl,type);
  9488. } else{
  9489. TBL_PC *sd=script_rid2sd(st);
  9490. if(sd)
  9491. clif_misceffect2(&sd->bl,type);
  9492. }
  9493. return 0;
  9494. }
  9495. /*==========================================
  9496. * Play a BGM on a single client [Rikter/Yommy]
  9497. *------------------------------------------*/
  9498. BUILDIN_FUNC(playBGM)
  9499. {
  9500. TBL_PC* sd = script_rid2sd(st);
  9501. const char* name = script_getstr(st,2);
  9502. if(sd)
  9503. {
  9504. if(!st->rid)
  9505. clif_playBGM(sd,map_id2bl(st->oid),name);
  9506. else
  9507. clif_playBGM(sd,&sd->bl,name);
  9508. }
  9509. return 0;
  9510. }
  9511. int playBGM_sub(struct block_list* bl,va_list ap)
  9512. {
  9513. char* name = va_arg(ap,char*);
  9514. clif_playBGM((TBL_PC *)bl, bl, name);
  9515. return 0;
  9516. }
  9517. /*==========================================
  9518. * Play a BGM on multiple client [Rikter/Yommy]
  9519. *------------------------------------------*/
  9520. BUILDIN_FUNC(playBGMall)
  9521. {
  9522. struct block_list* bl;
  9523. const char* name;
  9524. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9525. if (!bl)
  9526. return 0;
  9527. name = script_getstr(st,2);
  9528. if(script_hasdata(st,7))
  9529. { // specified part of map
  9530. const char* map = script_getstr(st,3);
  9531. int x0 = script_getnum(st,4);
  9532. int y0 = script_getnum(st,5);
  9533. int x1 = script_getnum(st,6);
  9534. int y1 = script_getnum(st,7);
  9535. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9536. }
  9537. else
  9538. if(!script_hasdata(st,7))
  9539. { // entire map
  9540. const char* map = script_getstr(st,3);
  9541. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9542. }
  9543. else
  9544. {
  9545. ShowError("buildin_playBGMall: insufficient arguments for specific area broadcast.\n");
  9546. }
  9547. return 0;
  9548. }
  9549. /*==========================================
  9550. * サウンドエフェクト
  9551. *------------------------------------------*/
  9552. BUILDIN_FUNC(soundeffect)
  9553. {
  9554. TBL_PC* sd = script_rid2sd(st);
  9555. const char* name = script_getstr(st,2);
  9556. int type = script_getnum(st,3);
  9557. if(sd)
  9558. {
  9559. if(!st->rid)
  9560. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9561. else
  9562. clif_soundeffect(sd,&sd->bl,name,type);
  9563. }
  9564. return 0;
  9565. }
  9566. int soundeffect_sub(struct block_list* bl,va_list ap)
  9567. {
  9568. char* name = va_arg(ap,char*);
  9569. int type = va_arg(ap,int);
  9570. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9571. return 0;
  9572. }
  9573. /*==========================================
  9574. * Play a sound effect (.wav) on multiple clients
  9575. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9576. *------------------------------------------*/
  9577. BUILDIN_FUNC(soundeffectall)
  9578. {
  9579. struct block_list* bl;
  9580. const char* name;
  9581. int type;
  9582. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9583. if (!bl)
  9584. return 0;
  9585. name = script_getstr(st,2);
  9586. type = script_getnum(st,3);
  9587. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9588. if(!script_hasdata(st,4))
  9589. { // area around
  9590. clif_soundeffectall(bl, name, type, AREA);
  9591. }
  9592. else
  9593. if(!script_hasdata(st,5))
  9594. { // entire map
  9595. const char* map = script_getstr(st,4);
  9596. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9597. }
  9598. else
  9599. if(script_hasdata(st,8))
  9600. { // specified part of map
  9601. const char* map = script_getstr(st,4);
  9602. int x0 = script_getnum(st,5);
  9603. int y0 = script_getnum(st,6);
  9604. int x1 = script_getnum(st,7);
  9605. int y1 = script_getnum(st,8);
  9606. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9607. }
  9608. else
  9609. {
  9610. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9611. }
  9612. return 0;
  9613. }
  9614. /*==========================================
  9615. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9616. *------------------------------------------*/
  9617. BUILDIN_FUNC(petrecovery)
  9618. {
  9619. struct pet_data *pd;
  9620. TBL_PC *sd=script_rid2sd(st);
  9621. if(sd==NULL || sd->pd==NULL)
  9622. return 0;
  9623. pd=sd->pd;
  9624. if (pd->recovery)
  9625. { //Halt previous bonus
  9626. if (pd->recovery->timer != -1)
  9627. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9628. } else //Init
  9629. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9630. pd->recovery->type = (sc_type)script_getnum(st,2);
  9631. pd->recovery->delay = script_getnum(st,3);
  9632. pd->recovery->timer = INVALID_TIMER;
  9633. return 0;
  9634. }
  9635. /*==========================================
  9636. * pet healing [Valaris] //Rewritten by [Skotlex]
  9637. *------------------------------------------*/
  9638. BUILDIN_FUNC(petheal)
  9639. {
  9640. struct pet_data *pd;
  9641. TBL_PC *sd=script_rid2sd(st);
  9642. if(sd==NULL || sd->pd==NULL)
  9643. return 0;
  9644. pd=sd->pd;
  9645. if (pd->s_skill)
  9646. { //Clear previous skill
  9647. if (pd->s_skill->timer != -1)
  9648. {
  9649. if (pd->s_skill->id)
  9650. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9651. else
  9652. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9653. }
  9654. } else //init memory
  9655. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9656. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9657. //Use the lv as the amount to heal
  9658. pd->s_skill->lv=script_getnum(st,2);
  9659. pd->s_skill->delay=script_getnum(st,3);
  9660. pd->s_skill->hp=script_getnum(st,4);
  9661. pd->s_skill->sp=script_getnum(st,5);
  9662. //Use delay as initial offset to avoid skill/heal exploits
  9663. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9664. pd->s_skill->timer = INVALID_TIMER;
  9665. else
  9666. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9667. return 0;
  9668. }
  9669. /*==========================================
  9670. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9671. *------------------------------------------*/
  9672. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9673. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9674. BUILDIN_FUNC(petskillattack)
  9675. {
  9676. struct pet_data *pd;
  9677. TBL_PC *sd=script_rid2sd(st);
  9678. if(sd==NULL || sd->pd==NULL)
  9679. return 0;
  9680. pd=sd->pd;
  9681. if (pd->a_skill == NULL)
  9682. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9683. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9684. pd->a_skill->lv=script_getnum(st,3);
  9685. pd->a_skill->div_ = 0;
  9686. pd->a_skill->rate=script_getnum(st,4);
  9687. pd->a_skill->bonusrate=script_getnum(st,5);
  9688. return 0;
  9689. }
  9690. /*==========================================
  9691. * pet attack skills [Valaris]
  9692. *------------------------------------------*/
  9693. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9694. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9695. BUILDIN_FUNC(petskillattack2)
  9696. {
  9697. struct pet_data *pd;
  9698. TBL_PC *sd=script_rid2sd(st);
  9699. if(sd==NULL || sd->pd==NULL)
  9700. return 0;
  9701. pd=sd->pd;
  9702. if (pd->a_skill == NULL)
  9703. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9704. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9705. pd->a_skill->lv=script_getnum(st,3);
  9706. pd->a_skill->div_ = script_getnum(st,4);
  9707. pd->a_skill->rate=script_getnum(st,5);
  9708. pd->a_skill->bonusrate=script_getnum(st,6);
  9709. return 0;
  9710. }
  9711. /*==========================================
  9712. * pet support skills [Skotlex]
  9713. *------------------------------------------*/
  9714. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9715. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9716. BUILDIN_FUNC(petskillsupport)
  9717. {
  9718. struct pet_data *pd;
  9719. TBL_PC *sd=script_rid2sd(st);
  9720. if(sd==NULL || sd->pd==NULL)
  9721. return 0;
  9722. pd=sd->pd;
  9723. if (pd->s_skill)
  9724. { //Clear previous skill
  9725. if (pd->s_skill->timer != -1)
  9726. {
  9727. if (pd->s_skill->id)
  9728. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9729. else
  9730. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9731. }
  9732. } else //init memory
  9733. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9734. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9735. pd->s_skill->lv=script_getnum(st,3);
  9736. pd->s_skill->delay=script_getnum(st,4);
  9737. pd->s_skill->hp=script_getnum(st,5);
  9738. pd->s_skill->sp=script_getnum(st,6);
  9739. //Use delay as initial offset to avoid skill/heal exploits
  9740. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9741. pd->s_skill->timer = INVALID_TIMER;
  9742. else
  9743. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9744. return 0;
  9745. }
  9746. /*==========================================
  9747. * Scripted skill effects [Celest]
  9748. *------------------------------------------*/
  9749. /// skilleffect <skill id>,<level>
  9750. /// skilleffect "<skill name>",<level>
  9751. BUILDIN_FUNC(skilleffect)
  9752. {
  9753. TBL_PC *sd;
  9754. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9755. int skilllv=script_getnum(st,3);
  9756. sd=script_rid2sd(st);
  9757. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9758. return 0;
  9759. }
  9760. /*==========================================
  9761. * NPC skill effects [Valaris]
  9762. *------------------------------------------*/
  9763. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9764. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9765. BUILDIN_FUNC(npcskilleffect)
  9766. {
  9767. struct block_list *bl= map_id2bl(st->oid);
  9768. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9769. int skilllv=script_getnum(st,3);
  9770. int x=script_getnum(st,4);
  9771. int y=script_getnum(st,5);
  9772. if (bl)
  9773. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9774. return 0;
  9775. }
  9776. /*==========================================
  9777. * Special effects [Valaris]
  9778. *------------------------------------------*/
  9779. BUILDIN_FUNC(specialeffect)
  9780. {
  9781. struct block_list *bl=map_id2bl(st->oid);
  9782. int type = script_getnum(st,2);
  9783. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9784. if(bl==NULL)
  9785. return 0;
  9786. if( script_hasdata(st,4) )
  9787. {
  9788. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9789. if(nd)
  9790. clif_specialeffect(&nd->bl, type, target);
  9791. }
  9792. else
  9793. clif_specialeffect(bl, type, target);
  9794. return 0;
  9795. }
  9796. BUILDIN_FUNC(specialeffect2)
  9797. {
  9798. TBL_PC *sd=script_rid2sd(st);
  9799. int type = script_getnum(st,2);
  9800. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9801. if( script_hasdata(st,4) )
  9802. sd = map_nick2sd(script_getstr(st,4));
  9803. if (sd)
  9804. clif_specialeffect(&sd->bl, type, target);
  9805. return 0;
  9806. }
  9807. /*==========================================
  9808. * Nude [Valaris]
  9809. *------------------------------------------*/
  9810. BUILDIN_FUNC(nude)
  9811. {
  9812. TBL_PC *sd=script_rid2sd(st);
  9813. int i,calcflag=0;
  9814. if(sd==NULL)
  9815. return 0;
  9816. for(i=0;i<11;i++)
  9817. if(sd->equip_index[i] >= 0) {
  9818. if(!calcflag)
  9819. calcflag=1;
  9820. pc_unequipitem(sd,sd->equip_index[i],2);
  9821. }
  9822. if(calcflag)
  9823. status_calc_pc(sd,0);
  9824. return 0;
  9825. }
  9826. /*==========================================
  9827. * gmcommand [MouseJstr]
  9828. *
  9829. * suggested on the forums...
  9830. * splitted into atcommand & charcommand by [Skotlex]
  9831. *------------------------------------------*/
  9832. BUILDIN_FUNC(atcommand)
  9833. {
  9834. TBL_PC dummy_sd;
  9835. TBL_PC* sd;
  9836. int fd;
  9837. const char* cmd;
  9838. cmd = script_getstr(st,2);
  9839. if (st->rid) {
  9840. sd = script_rid2sd(st);
  9841. fd = sd->fd;
  9842. } else { //Use a dummy character.
  9843. sd = &dummy_sd;
  9844. fd = 0;
  9845. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9846. if (st->oid)
  9847. {
  9848. struct block_list* bl = map_id2bl(st->oid);
  9849. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9850. if (bl->type == BL_NPC)
  9851. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9852. }
  9853. }
  9854. // compatibility with previous implementation (deprecated!)
  9855. if(cmd[0] != atcommand_symbol)
  9856. {
  9857. cmd += strlen(sd->status.name);
  9858. while(*cmd != atcommand_symbol && *cmd != 0)
  9859. cmd++;
  9860. }
  9861. is_atcommand(fd, sd, cmd, 0);
  9862. return 0;
  9863. }
  9864. BUILDIN_FUNC(charcommand)
  9865. {
  9866. TBL_PC dummy_sd;
  9867. TBL_PC* sd;
  9868. int fd;
  9869. const char* cmd;
  9870. cmd = script_getstr(st,2);
  9871. if (st->rid) {
  9872. sd = script_rid2sd(st);
  9873. fd = sd->fd;
  9874. } else { //Use a dummy character.
  9875. sd = &dummy_sd;
  9876. fd = 0;
  9877. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9878. if (st->oid)
  9879. {
  9880. struct block_list* bl = map_id2bl(st->oid);
  9881. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9882. if (bl->type == BL_NPC)
  9883. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9884. }
  9885. }
  9886. if (*cmd != charcommand_symbol) {
  9887. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  9888. script_reportsrc(st);
  9889. return 1;
  9890. }
  9891. is_atcommand(fd, sd, cmd, 0);
  9892. return 0;
  9893. }
  9894. /*==========================================
  9895. * Displays a message for the player only (like system messages like "you got an apple" )
  9896. *------------------------------------------*/
  9897. BUILDIN_FUNC(dispbottom)
  9898. {
  9899. TBL_PC *sd=script_rid2sd(st);
  9900. const char *message;
  9901. message=script_getstr(st,2);
  9902. if(sd)
  9903. clif_disp_onlyself(sd,message,(int)strlen(message));
  9904. return 0;
  9905. }
  9906. /*==========================================
  9907. * All The Players Full Recovery
  9908. * (HP/SP full restore and resurrect if need)
  9909. *------------------------------------------*/
  9910. BUILDIN_FUNC(recovery)
  9911. {
  9912. TBL_PC* sd;
  9913. struct s_mapiterator* iter;
  9914. iter = mapit_getallusers();
  9915. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9916. {
  9917. if(pc_isdead(sd))
  9918. status_revive(&sd->bl, 100, 100);
  9919. else
  9920. status_percent_heal(&sd->bl, 100, 100);
  9921. clif_displaymessage(sd->fd,"You have been recovered!");
  9922. }
  9923. mapit_free(iter);
  9924. return 0;
  9925. }
  9926. /*==========================================
  9927. * Get your pet info: getpetinfo(n)
  9928. * n -> 0:pet_id 1:pet_class 2:pet_name
  9929. * 3:friendly 4:hungry, 5: rename flag.
  9930. *------------------------------------------*/
  9931. BUILDIN_FUNC(getpetinfo)
  9932. {
  9933. TBL_PC *sd=script_rid2sd(st);
  9934. TBL_PET *pd;
  9935. int type=script_getnum(st,2);
  9936. if(!sd || !sd->pd) {
  9937. if (type == 2)
  9938. script_pushconststr(st,"null");
  9939. else
  9940. script_pushint(st,0);
  9941. return 0;
  9942. }
  9943. pd = sd->pd;
  9944. switch(type){
  9945. case 0: script_pushint(st,pd->pet.pet_id); break;
  9946. case 1: script_pushint(st,pd->pet.class_); break;
  9947. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9948. case 3: script_pushint(st,pd->pet.intimate); break;
  9949. case 4: script_pushint(st,pd->pet.hungry); break;
  9950. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9951. default:
  9952. script_pushint(st,0);
  9953. break;
  9954. }
  9955. return 0;
  9956. }
  9957. /*==========================================
  9958. * Get your homunculus info: gethominfo(n)
  9959. * n -> 0:hom_id 1:class 2:name
  9960. * 3:friendly 4:hungry, 5: rename flag.
  9961. * 6: level
  9962. *------------------------------------------*/
  9963. BUILDIN_FUNC(gethominfo)
  9964. {
  9965. TBL_PC *sd=script_rid2sd(st);
  9966. TBL_HOM *hd;
  9967. int type=script_getnum(st,2);
  9968. hd = sd?sd->hd:NULL;
  9969. if(!merc_is_hom_active(hd))
  9970. {
  9971. if (type == 2)
  9972. script_pushconststr(st,"null");
  9973. else
  9974. script_pushint(st,0);
  9975. return 0;
  9976. }
  9977. switch(type){
  9978. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9979. case 1: script_pushint(st,hd->homunculus.class_); break;
  9980. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9981. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9982. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9983. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9984. case 6: script_pushint(st,hd->homunculus.level); break;
  9985. default:
  9986. script_pushint(st,0);
  9987. break;
  9988. }
  9989. return 0;
  9990. }
  9991. /*==========================================
  9992. * Shows wether your inventory(and equips) contain
  9993. selected card or not.
  9994. checkequipedcard(4001);
  9995. *------------------------------------------*/
  9996. BUILDIN_FUNC(checkequipedcard)
  9997. {
  9998. TBL_PC *sd=script_rid2sd(st);
  9999. int n,i,c=0;
  10000. c=script_getnum(st,2);
  10001. if(sd){
  10002. for(i=0;i<MAX_INVENTORY;i++){
  10003. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10004. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10005. continue;
  10006. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10007. if(sd->status.inventory[i].card[n]==c){
  10008. script_pushint(st,1);
  10009. return 0;
  10010. }
  10011. }
  10012. }
  10013. }
  10014. }
  10015. script_pushint(st,0);
  10016. return 0;
  10017. }
  10018. BUILDIN_FUNC(jump_zero)
  10019. {
  10020. int sel;
  10021. sel=script_getnum(st,2);
  10022. if(!sel) {
  10023. int pos;
  10024. if( !data_islabel(script_getdata(st,3)) ){
  10025. ShowError("script: jump_zero: not label !\n");
  10026. st->state=END;
  10027. return 1;
  10028. }
  10029. pos=script_getnum(st,3);
  10030. st->pos=pos;
  10031. st->state=GOTO;
  10032. // printf("script: jump_zero: jumpto : %d\n",pos);
  10033. } else {
  10034. // printf("script: jump_zero: fail\n");
  10035. }
  10036. return 0;
  10037. }
  10038. /*==========================================
  10039. * GetMapMobs
  10040. returns mob counts on a set map:
  10041. e.g. GetMapMobs("prontera")
  10042. use "this" - for player's map
  10043. *------------------------------------------*/
  10044. BUILDIN_FUNC(getmapmobs)
  10045. {
  10046. const char *str=NULL;
  10047. int m=-1,bx,by;
  10048. int count=0;
  10049. struct block_list *bl;
  10050. str=script_getstr(st,2);
  10051. if(strcmp(str,"this")==0){
  10052. TBL_PC *sd=script_rid2sd(st);
  10053. if(sd)
  10054. m=sd->bl.m;
  10055. else{
  10056. script_pushint(st,-1);
  10057. return 0;
  10058. }
  10059. }else
  10060. m=map_mapname2mapid(str);
  10061. if(m < 0){
  10062. script_pushint(st,-1);
  10063. return 0;
  10064. }
  10065. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  10066. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  10067. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  10068. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10069. count++;
  10070. script_pushint(st,count);
  10071. return 0;
  10072. }
  10073. /*==========================================
  10074. * movenpc [MouseJstr]
  10075. *------------------------------------------*/
  10076. BUILDIN_FUNC(movenpc)
  10077. {
  10078. TBL_NPC *nd = NULL;
  10079. const char *npc;
  10080. int x,y;
  10081. npc = script_getstr(st,2);
  10082. x = script_getnum(st,3);
  10083. y = script_getnum(st,4);
  10084. if ((nd = npc_name2id(npc)) == NULL)
  10085. return -1;
  10086. npc_movenpc(nd, x, y);
  10087. return 0;
  10088. }
  10089. /*==========================================
  10090. * message [MouseJstr]
  10091. *------------------------------------------*/
  10092. BUILDIN_FUNC(message)
  10093. {
  10094. const char *msg,*player;
  10095. TBL_PC *pl_sd = NULL;
  10096. player = script_getstr(st,2);
  10097. msg = script_getstr(st,3);
  10098. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10099. return 0;
  10100. clif_displaymessage(pl_sd->fd, msg);
  10101. return 0;
  10102. }
  10103. /*==========================================
  10104. * npctalk (sends message to surrounding area)
  10105. *------------------------------------------*/
  10106. BUILDIN_FUNC(npctalk)
  10107. {
  10108. const char* str;
  10109. char message[255];
  10110. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10111. str = script_getstr(st,2);
  10112. if(nd) {
  10113. memcpy(message, nd->name, NAME_LENGTH);
  10114. strtok(message, "#"); // discard extra name identifier if present
  10115. strcat(message, " : ");
  10116. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  10117. clif_message(&(nd->bl), message);
  10118. }
  10119. return 0;
  10120. }
  10121. // change npc walkspeed [Valaris]
  10122. BUILDIN_FUNC(npcspeed)
  10123. {
  10124. struct npc_data* nd;
  10125. int speed;
  10126. speed = script_getnum(st,2);
  10127. nd =(struct npc_data *)map_id2bl(st->oid);
  10128. if( nd )
  10129. {
  10130. nd->speed = speed;
  10131. nd->ud.state.speed_changed = 1;
  10132. }
  10133. return 0;
  10134. }
  10135. // make an npc walk to a position [Valaris]
  10136. BUILDIN_FUNC(npcwalkto)
  10137. {
  10138. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10139. int x=0,y=0;
  10140. x=script_getnum(st,2);
  10141. y=script_getnum(st,3);
  10142. if(nd) {
  10143. unit_walktoxy(&nd->bl,x,y,0);
  10144. }
  10145. return 0;
  10146. }
  10147. // stop an npc's movement [Valaris]
  10148. BUILDIN_FUNC(npcstop)
  10149. {
  10150. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10151. if(nd) {
  10152. unit_stop_walking(&nd->bl,1|4);
  10153. }
  10154. return 0;
  10155. }
  10156. /*==========================================
  10157. * getlook char info. getlook(arg)
  10158. *------------------------------------------*/
  10159. BUILDIN_FUNC(getlook)
  10160. {
  10161. int type,val;
  10162. TBL_PC *sd;
  10163. sd=script_rid2sd(st);
  10164. type=script_getnum(st,2);
  10165. val=-1;
  10166. switch(type) {
  10167. case LOOK_HAIR: val=sd->status.hair; break; //1
  10168. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10169. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10170. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10171. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10172. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10173. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10174. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10175. case LOOK_SHOES: break; //9
  10176. }
  10177. script_pushint(st,val);
  10178. return 0;
  10179. }
  10180. /*==========================================
  10181. * get char save point. argument: 0- map name, 1- x, 2- y
  10182. *------------------------------------------*/
  10183. BUILDIN_FUNC(getsavepoint)
  10184. {
  10185. TBL_PC* sd;
  10186. int type;
  10187. sd = script_rid2sd(st);
  10188. if (sd == NULL) {
  10189. script_pushint(st,0);
  10190. return 0;
  10191. }
  10192. type = script_getnum(st,2);
  10193. switch(type) {
  10194. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10195. case 1: script_pushint(st,sd->status.save_point.x); break;
  10196. case 2: script_pushint(st,sd->status.save_point.y); break;
  10197. default:
  10198. script_pushint(st,0);
  10199. break;
  10200. }
  10201. return 0;
  10202. }
  10203. /*==========================================
  10204. * Get position for char/npc/pet/mob objects. Added by Lorky
  10205. *
  10206. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10207. * where type:
  10208. * MapName$ - String variable for output map name
  10209. * MapX - Integer variable for output coord X
  10210. * MapY - Integer variable for output coord Y
  10211. * type - type of object
  10212. * 0 - Character coord
  10213. * 1 - NPC coord
  10214. * 2 - Pet coord
  10215. * 3 - Mob coord (not released)
  10216. * 4 - Homun coord
  10217. * CharName$ - Name object. If miss or "this" the current object
  10218. *
  10219. * Return:
  10220. * 0 - success
  10221. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10222. *------------------------------------------*/
  10223. BUILDIN_FUNC(getmapxy)
  10224. {
  10225. struct block_list *bl = NULL;
  10226. TBL_PC *sd=NULL;
  10227. int num;
  10228. const char *name;
  10229. char prefix;
  10230. int x,y,type;
  10231. char mapname[MAP_NAME_LENGTH];
  10232. if( !data_isreference(script_getdata(st,2)) ){
  10233. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10234. script_pushint(st,-1);
  10235. return 1;
  10236. }
  10237. if( !data_isreference(script_getdata(st,3)) ){
  10238. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10239. script_pushint(st,-1);
  10240. return 1;
  10241. }
  10242. if( !data_isreference(script_getdata(st,4)) ){
  10243. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10244. script_pushint(st,-1);
  10245. return 1;
  10246. }
  10247. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10248. type=script_getnum(st,5);
  10249. switch (type){
  10250. case 0: //Get Character Position
  10251. if( script_hasdata(st,6) )
  10252. sd=map_nick2sd(script_getstr(st,6));
  10253. else
  10254. sd=script_rid2sd(st);
  10255. if (sd)
  10256. bl = &sd->bl;
  10257. break;
  10258. case 1: //Get NPC Position
  10259. if( script_hasdata(st,6) )
  10260. {
  10261. struct npc_data *nd;
  10262. nd=npc_name2id(script_getstr(st,6));
  10263. if (nd)
  10264. bl = &nd->bl;
  10265. } else //In case the origin is not an npc?
  10266. bl=map_id2bl(st->oid);
  10267. break;
  10268. case 2: //Get Pet Position
  10269. if(script_hasdata(st,6))
  10270. sd=map_nick2sd(script_getstr(st,6));
  10271. else
  10272. sd=script_rid2sd(st);
  10273. if (sd && sd->pd)
  10274. bl = &sd->pd->bl;
  10275. break;
  10276. case 3: //Get Mob Position
  10277. break; //Not supported?
  10278. case 4: //Get Homun Position
  10279. if(script_hasdata(st,6))
  10280. sd=map_nick2sd(script_getstr(st,6));
  10281. else
  10282. sd=script_rid2sd(st);
  10283. if (sd && sd->hd)
  10284. bl = &sd->hd->bl;
  10285. break;
  10286. default:
  10287. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10288. script_pushint(st,-1);
  10289. return 1;
  10290. }
  10291. if (!bl) { //No object found.
  10292. script_pushint(st,-1);
  10293. return 0;
  10294. }
  10295. x= bl->x;
  10296. y= bl->y;
  10297. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10298. //Set MapName$
  10299. num=st->stack->stack_data[st->start+2].u.num;
  10300. name=get_str(num&0x00ffffff);
  10301. prefix=*name;
  10302. if(not_server_variable(prefix))
  10303. sd=script_rid2sd(st);
  10304. else
  10305. sd=NULL;
  10306. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10307. //Set MapX
  10308. num=st->stack->stack_data[st->start+3].u.num;
  10309. name=get_str(num&0x00ffffff);
  10310. prefix=*name;
  10311. if(not_server_variable(prefix))
  10312. sd=script_rid2sd(st);
  10313. else
  10314. sd=NULL;
  10315. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10316. //Set MapY
  10317. num=st->stack->stack_data[st->start+4].u.num;
  10318. name=get_str(num&0x00ffffff);
  10319. prefix=*name;
  10320. if(not_server_variable(prefix))
  10321. sd=script_rid2sd(st);
  10322. else
  10323. sd=NULL;
  10324. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10325. //Return Success value
  10326. script_pushint(st,0);
  10327. return 0;
  10328. }
  10329. /*==========================================
  10330. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10331. *------------------------------------------*/
  10332. BUILDIN_FUNC(logmes)
  10333. {
  10334. const char *str;
  10335. TBL_PC* sd;
  10336. if( log_config.npc <= 0 )
  10337. return 0;
  10338. sd = script_rid2sd(st);
  10339. if( sd == NULL )
  10340. return 1;
  10341. str = script_getstr(st,2);
  10342. log_npc(sd,str);
  10343. return 0;
  10344. }
  10345. BUILDIN_FUNC(summon)
  10346. {
  10347. int _class, timeout=0;
  10348. const char *str,*event="";
  10349. TBL_PC *sd;
  10350. struct mob_data *md;
  10351. int tick = gettick();
  10352. sd=script_rid2sd(st);
  10353. if (!sd) return 0;
  10354. str =script_getstr(st,2);
  10355. _class=script_getnum(st,3);
  10356. if( script_hasdata(st,4) )
  10357. timeout=script_getnum(st,4);
  10358. if( script_hasdata(st,5) ){
  10359. event=script_getstr(st,5);
  10360. check_event(st, event);
  10361. }
  10362. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10363. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10364. if (md) {
  10365. md->master_id=sd->bl.id;
  10366. md->special_state.ai=1;
  10367. if( md->deletetimer != INVALID_TIMER )
  10368. delete_timer(md->deletetimer, mob_timer_delete);
  10369. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10370. mob_spawn (md); //Now it is ready for spawning.
  10371. clif_misceffect2(&md->bl,344);
  10372. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10373. }
  10374. return 0;
  10375. }
  10376. /*==========================================
  10377. * Checks whether it is daytime/nighttime
  10378. *------------------------------------------*/
  10379. BUILDIN_FUNC(isnight)
  10380. {
  10381. script_pushint(st,(night_flag == 1));
  10382. return 0;
  10383. }
  10384. BUILDIN_FUNC(isday)
  10385. {
  10386. script_pushint(st,(night_flag == 0));
  10387. return 0;
  10388. }
  10389. /*================================================
  10390. * Check whether another item/card has been
  10391. * equipped - used for 2/15's cards patch [celest]
  10392. *------------------------------------------------*/
  10393. // leave this here, just in case
  10394. #if 0
  10395. BUILDIN_FUNC(isequipped)
  10396. {
  10397. TBL_PC *sd;
  10398. int i, j, k, id = 1;
  10399. int ret = -1;
  10400. sd = script_rid2sd(st);
  10401. for (i=0; id!=0; i++) {
  10402. int flag = 0;
  10403. FETCH (i+2, id) else id = 0;
  10404. if (id <= 0)
  10405. continue;
  10406. for (j=0; j<EQI_MAX; j++) {
  10407. int index;
  10408. index = sd->equip_index[j];
  10409. if(index < 0) continue;
  10410. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10411. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10412. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10413. if(!sd->inventory_data[index])
  10414. continue;
  10415. if(itemdb_type(id) != IT_CARD) { //Non card
  10416. if (sd->inventory_data[index]->nameid != id)
  10417. continue;
  10418. flag = 1;
  10419. break;
  10420. } else { //Card
  10421. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10422. continue;
  10423. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10424. if (sd->status.inventory[index].card[k] == id)
  10425. {
  10426. flag = 1;
  10427. break;
  10428. }
  10429. }
  10430. }
  10431. if (flag) break;
  10432. }
  10433. if (ret == -1)
  10434. ret = flag;
  10435. else
  10436. ret &= flag;
  10437. if (!ret) break;
  10438. }
  10439. script_pushint(st,ret);
  10440. return 0;
  10441. }
  10442. #endif
  10443. /*================================================
  10444. * Check how many items/cards in the list are
  10445. * equipped - used for 2/15's cards patch [celest]
  10446. *------------------------------------------------*/
  10447. BUILDIN_FUNC(isequippedcnt)
  10448. {
  10449. TBL_PC *sd;
  10450. int i, j, k, id = 1;
  10451. int ret = 0;
  10452. sd = script_rid2sd(st);
  10453. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10454. script_pushint(st,0);
  10455. return 0;
  10456. }
  10457. for (i=0; id!=0; i++) {
  10458. FETCH (i+2, id) else id = 0;
  10459. if (id <= 0)
  10460. continue;
  10461. for (j=0; j<EQI_MAX; j++) {
  10462. int index;
  10463. index = sd->equip_index[j];
  10464. if(index < 0) continue;
  10465. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10466. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10467. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10468. if(!sd->inventory_data[index])
  10469. continue;
  10470. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10471. if (sd->inventory_data[index]->nameid == id)
  10472. ret+= sd->status.inventory[index].amount;
  10473. } else { //Count cards.
  10474. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10475. continue; //No cards
  10476. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10477. if (sd->status.inventory[index].card[k] == id)
  10478. ret++; //[Lupus]
  10479. }
  10480. }
  10481. }
  10482. }
  10483. script_pushint(st,ret);
  10484. return 0;
  10485. }
  10486. /*================================================
  10487. * Check whether another card has been
  10488. * equipped - used for 2/15's cards patch [celest]
  10489. * -- Items checked cannot be reused in another
  10490. * card set to prevent exploits
  10491. *------------------------------------------------*/
  10492. BUILDIN_FUNC(isequipped)
  10493. {
  10494. TBL_PC *sd;
  10495. int i, j, k, id = 1;
  10496. int index, flag;
  10497. int ret = -1;
  10498. //Original hash to reverse it when full check fails.
  10499. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10500. sd = script_rid2sd(st);
  10501. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10502. script_pushint(st,0);
  10503. return 0;
  10504. }
  10505. setitem_hash = sd->setitem_hash;
  10506. setitem_hash2 = sd->setitem_hash2;
  10507. for (i=0; id!=0; i++)
  10508. {
  10509. FETCH (i+2, id) else id = 0;
  10510. if (id <= 0)
  10511. continue;
  10512. flag = 0;
  10513. for (j=0; j<EQI_MAX; j++)
  10514. {
  10515. index = sd->equip_index[j];
  10516. if(index < 0) continue;
  10517. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10518. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10519. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10520. if(!sd->inventory_data[index])
  10521. continue;
  10522. if (itemdb_type(id) != IT_CARD) {
  10523. if (sd->inventory_data[index]->nameid != id)
  10524. continue;
  10525. flag = 1;
  10526. break;
  10527. } else { //Cards
  10528. if (sd->inventory_data[index]->slot == 0 ||
  10529. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10530. continue;
  10531. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10532. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10533. unsigned int hash = 0;
  10534. if (sd->status.inventory[index].card[k] != id)
  10535. continue;
  10536. hash = 1<<((j<5?j:j-5)*4 + k);
  10537. // check if card is already used by another set
  10538. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10539. continue;
  10540. // We have found a match
  10541. flag = 1;
  10542. // Set hash so this card cannot be used by another
  10543. if (j<5)
  10544. sd->setitem_hash |= hash;
  10545. else
  10546. sd->setitem_hash2 |= hash;
  10547. break;
  10548. }
  10549. }
  10550. if (flag) break; //Card found
  10551. }
  10552. if (ret == -1)
  10553. ret = flag;
  10554. else
  10555. ret &= flag;
  10556. if (!ret) break;
  10557. }
  10558. if (!ret)
  10559. { //When check fails, restore original hash values. [Skotlex]
  10560. sd->setitem_hash = setitem_hash;
  10561. sd->setitem_hash2 = setitem_hash2;
  10562. }
  10563. script_pushint(st,ret);
  10564. return 0;
  10565. }
  10566. /*================================================
  10567. * Check how many given inserted cards in the CURRENT
  10568. * weapon - used for 2/15's cards patch [Lupus]
  10569. *------------------------------------------------*/
  10570. BUILDIN_FUNC(cardscnt)
  10571. {
  10572. TBL_PC *sd;
  10573. int i, k, id = 1;
  10574. int ret = 0;
  10575. int index;
  10576. sd = script_rid2sd(st);
  10577. for (i=0; id!=0; i++) {
  10578. FETCH (i+2, id) else id = 0;
  10579. if (id <= 0)
  10580. continue;
  10581. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10582. if(index < 0) continue;
  10583. if(!sd->inventory_data[index])
  10584. continue;
  10585. if(itemdb_type(id) != IT_CARD) {
  10586. if (sd->inventory_data[index]->nameid == id)
  10587. ret+= sd->status.inventory[index].amount;
  10588. } else {
  10589. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10590. continue;
  10591. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10592. if (sd->status.inventory[index].card[k] == id)
  10593. ret++;
  10594. }
  10595. }
  10596. }
  10597. script_pushint(st,ret);
  10598. // script_pushint(st,current_equip_item_index);
  10599. return 0;
  10600. }
  10601. /*=======================================================
  10602. * Returns the refined number of the current item, or an
  10603. * item with inventory index specified
  10604. *-------------------------------------------------------*/
  10605. BUILDIN_FUNC(getrefine)
  10606. {
  10607. TBL_PC *sd;
  10608. if ((sd = script_rid2sd(st))!= NULL)
  10609. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10610. else
  10611. script_pushint(st,0);
  10612. return 0;
  10613. }
  10614. /*=======================================================
  10615. * Day/Night controls
  10616. *-------------------------------------------------------*/
  10617. BUILDIN_FUNC(night)
  10618. {
  10619. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10620. return 0;
  10621. }
  10622. BUILDIN_FUNC(day)
  10623. {
  10624. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10625. return 0;
  10626. }
  10627. //=======================================================
  10628. // Unequip [Spectre]
  10629. //-------------------------------------------------------
  10630. BUILDIN_FUNC(unequip)
  10631. {
  10632. int i;
  10633. size_t num;
  10634. TBL_PC *sd;
  10635. num = script_getnum(st,2);
  10636. sd = script_rid2sd(st);
  10637. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10638. {
  10639. i = pc_checkequip(sd,equip[num-1]);
  10640. if (i >= 0)
  10641. pc_unequipitem(sd,i,1|2);
  10642. }
  10643. return 0;
  10644. }
  10645. BUILDIN_FUNC(equip)
  10646. {
  10647. int nameid=0,i;
  10648. TBL_PC *sd;
  10649. struct item_data *item_data;
  10650. sd = script_rid2sd(st);
  10651. nameid=script_getnum(st,2);
  10652. if((item_data = itemdb_exists(nameid)) == NULL)
  10653. {
  10654. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10655. return 1;
  10656. }
  10657. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10658. if( i < MAX_INVENTORY )
  10659. pc_equipitem(sd,i,item_data->equip);
  10660. return 0;
  10661. }
  10662. BUILDIN_FUNC(autoequip)
  10663. {
  10664. int nameid, flag;
  10665. struct item_data *item_data;
  10666. nameid=script_getnum(st,2);
  10667. flag=script_getnum(st,3);
  10668. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10669. item_data->flag.autoequip = flag>0?1:0;
  10670. }
  10671. return 0;
  10672. }
  10673. BUILDIN_FUNC(setbattleflag)
  10674. {
  10675. const char *flag, *value;
  10676. flag = script_getstr(st,2);
  10677. value = script_getstr(st,3);
  10678. if (battle_set_value(flag, value) == 0)
  10679. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10680. else
  10681. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10682. return 0;
  10683. }
  10684. BUILDIN_FUNC(getbattleflag)
  10685. {
  10686. const char *flag;
  10687. flag = script_getstr(st,2);
  10688. script_pushint(st,battle_get_value(flag));
  10689. return 0;
  10690. }
  10691. //=======================================================
  10692. // strlen [Valaris]
  10693. //-------------------------------------------------------
  10694. BUILDIN_FUNC(getstrlen)
  10695. {
  10696. const char *str = script_getstr(st,2);
  10697. int len = (str) ? (int)strlen(str) : 0;
  10698. script_pushint(st,len);
  10699. return 0;
  10700. }
  10701. //=======================================================
  10702. // isalpha [Valaris]
  10703. //-------------------------------------------------------
  10704. BUILDIN_FUNC(charisalpha)
  10705. {
  10706. const char *str=script_getstr(st,2);
  10707. int pos=script_getnum(st,3);
  10708. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10709. script_pushint(st,val);
  10710. return 0;
  10711. }
  10712. /// Changes the display name and/or display class of the npc.
  10713. /// Returns 0 is successful, 1 if the npc does not exist.
  10714. ///
  10715. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10716. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10717. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10718. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10719. BUILDIN_FUNC(setnpcdisplay)
  10720. {
  10721. const char* name;
  10722. const char* newname = NULL;
  10723. int class_ = -1, size = -1;
  10724. struct script_data* data;
  10725. struct npc_data* nd;
  10726. name = script_getstr(st,2);
  10727. data = script_getdata(st,3);
  10728. if( script_hasdata(st,4) )
  10729. class_ = script_getnum(st,4);
  10730. if( script_hasdata(st,5) )
  10731. size = script_getnum(st,5);
  10732. get_val(st, data);
  10733. if( data_isstring(data) )
  10734. newname = conv_str(st,data);
  10735. else if( data_isint(data) )
  10736. class_ = conv_num(st,data);
  10737. else
  10738. {
  10739. ShowError("script:setnpcdisplay: expected a string or number\n");
  10740. script_reportdata(data);
  10741. return 1;
  10742. }
  10743. nd = npc_name2id(name);
  10744. if( nd == NULL )
  10745. {// not found
  10746. script_pushint(st,1);
  10747. return 0;
  10748. }
  10749. // update npc
  10750. if( newname )
  10751. npc_setdisplayname(nd, newname);
  10752. if( size != -1 && size != nd->size )
  10753. nd->size = size;
  10754. else
  10755. size = -1;
  10756. if( class_ != -1 && nd->class_ != class_ )
  10757. npc_setclass(nd, class_);
  10758. else if( size != -1 )
  10759. { // Required to update the visual size
  10760. clif_clearunit_area(&nd->bl, 0);
  10761. clif_spawn(&nd->bl);
  10762. }
  10763. script_pushint(st,0);
  10764. return 0;
  10765. }
  10766. BUILDIN_FUNC(atoi)
  10767. {
  10768. const char *value;
  10769. value = script_getstr(st,2);
  10770. script_pushint(st,atoi(value));
  10771. return 0;
  10772. }
  10773. // case-insensitive substring search [lordalfa]
  10774. BUILDIN_FUNC(compare)
  10775. {
  10776. const char *message;
  10777. const char *cmpstring;
  10778. message = script_getstr(st,2);
  10779. cmpstring = script_getstr(st,3);
  10780. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10781. return 0;
  10782. }
  10783. // [zBuffer] List of mathematics commands --->
  10784. BUILDIN_FUNC(sqrt)
  10785. {
  10786. double i, a;
  10787. i = script_getnum(st,2);
  10788. a = sqrt(i);
  10789. script_pushint(st,(int)a);
  10790. return 0;
  10791. }
  10792. BUILDIN_FUNC(pow)
  10793. {
  10794. double i, a, b;
  10795. a = script_getnum(st,2);
  10796. b = script_getnum(st,3);
  10797. i = pow(a,b);
  10798. script_pushint(st,(int)i);
  10799. return 0;
  10800. }
  10801. BUILDIN_FUNC(distance)
  10802. {
  10803. int x0, y0, x1, y1;
  10804. x0 = script_getnum(st,2);
  10805. y0 = script_getnum(st,3);
  10806. x1 = script_getnum(st,4);
  10807. y1 = script_getnum(st,5);
  10808. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10809. return 0;
  10810. }
  10811. // <--- [zBuffer] List of mathematics commands
  10812. BUILDIN_FUNC(md5)
  10813. {
  10814. const char *tmpstr;
  10815. char *md5str;
  10816. tmpstr = script_getstr(st,2);
  10817. md5str = (char *)aMallocA((32+1)*sizeof(char));
  10818. MD5_String(tmpstr, md5str);
  10819. script_pushstr(st, md5str);
  10820. return 0;
  10821. }
  10822. // [zBuffer] List of dynamic var commands --->
  10823. BUILDIN_FUNC(setd)
  10824. {
  10825. TBL_PC *sd=NULL;
  10826. char varname[100];
  10827. const char *value, *buffer;
  10828. int elem;
  10829. buffer = script_getstr(st, 2);
  10830. value = script_getstr(st, 3);
  10831. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10832. elem = 0;
  10833. if( not_server_variable(*varname) )
  10834. {
  10835. sd = script_rid2sd(st);
  10836. if( sd == NULL )
  10837. {
  10838. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10839. return 0;
  10840. }
  10841. }
  10842. if(varname[strlen(varname)-1] != '$') {
  10843. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10844. } else {
  10845. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10846. }
  10847. return 0;
  10848. }
  10849. #ifndef TXT_ONLY
  10850. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10851. {
  10852. int i, j;
  10853. TBL_PC* sd = NULL;
  10854. const char* query;
  10855. struct script_data* data;
  10856. const char* name;
  10857. int max_rows = 128;// maximum number of rows
  10858. int num_vars;
  10859. int num_cols;
  10860. // check target variables
  10861. for( i = 3; script_hasdata(st,i); ++i )
  10862. {
  10863. data = script_getdata(st, i);
  10864. if( data_isreference(data) && reference_tovariable(data) )
  10865. {// it's a variable
  10866. name = reference_getname(data);
  10867. if( not_server_variable(*name) && sd == NULL )
  10868. {// requires a player
  10869. sd = script_rid2sd(st);
  10870. if( sd == NULL )
  10871. {// no player attached
  10872. script_reportdata(data);
  10873. st->state = END;
  10874. return 1;
  10875. }
  10876. }
  10877. if( not_array_variable(*name) )
  10878. max_rows = 1;// not an array, limit to one row
  10879. }
  10880. else
  10881. {
  10882. ShowError("script:query_sql: not a variable\n");
  10883. script_reportdata(data);
  10884. st->state = END;
  10885. return 1;
  10886. }
  10887. }
  10888. num_vars = i - 3;
  10889. // Execute the query
  10890. query = script_getstr(st,2);
  10891. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10892. {
  10893. Sql_ShowDebug(handle);
  10894. script_pushint(st, 0);
  10895. return 1;
  10896. }
  10897. if( Sql_NumRows(handle) == 0 )
  10898. {// No data received
  10899. Sql_FreeResult(handle);
  10900. script_pushint(st, 0);
  10901. return 0;
  10902. }
  10903. // Count the number of columns to store
  10904. num_cols = Sql_NumColumns(handle);
  10905. if( num_vars < num_cols )
  10906. {
  10907. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10908. script_reportsrc(st);
  10909. }
  10910. else if( num_vars > num_cols )
  10911. {
  10912. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10913. script_reportsrc(st);
  10914. }
  10915. // Store data
  10916. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10917. {
  10918. for( j = 0; j < num_vars; ++j )
  10919. {
  10920. char* str = NULL;
  10921. if( j < num_cols )
  10922. Sql_GetData(handle, j, &str, NULL);
  10923. data = script_getdata(st, j+3);
  10924. name = reference_getname(data);
  10925. if( is_string_variable(name) )
  10926. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10927. else
  10928. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10929. }
  10930. }
  10931. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10932. {
  10933. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10934. script_reportsrc(st);
  10935. }
  10936. // Free data
  10937. Sql_FreeResult(handle);
  10938. script_pushint(st, i);
  10939. return 0;
  10940. }
  10941. #endif
  10942. BUILDIN_FUNC(query_sql)
  10943. {
  10944. #ifndef TXT_ONLY
  10945. return buildin_query_sql_sub(st, mmysql_handle);
  10946. #else
  10947. //for TXT version, we always return -1
  10948. script_pushint(st,-1);
  10949. return 0;
  10950. #endif
  10951. }
  10952. BUILDIN_FUNC(query_logsql)
  10953. {
  10954. #ifndef TXT_ONLY
  10955. return buildin_query_sql_sub(st, logmysql_handle);
  10956. #else
  10957. //for TXT version, we always return -1
  10958. script_pushint(st,-1);
  10959. return 0;
  10960. #endif
  10961. }
  10962. //Allows escaping of a given string.
  10963. BUILDIN_FUNC(escape_sql)
  10964. {
  10965. const char *str;
  10966. char *esc_str;
  10967. size_t len;
  10968. str = script_getstr(st,2);
  10969. len = strlen(str);
  10970. esc_str = (char*)aMallocA(len*2+1);
  10971. #if defined(TXT_ONLY)
  10972. jstrescapecpy(esc_str, str);
  10973. #else
  10974. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10975. #endif
  10976. script_pushstr(st, esc_str);
  10977. return 0;
  10978. }
  10979. BUILDIN_FUNC(getd)
  10980. {
  10981. char varname[100];
  10982. const char *buffer;
  10983. int elem;
  10984. buffer = script_getstr(st, 2);
  10985. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10986. elem = 0;
  10987. // Push the 'pointer' so it's more flexible [Lance]
  10988. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10989. return 0;
  10990. }
  10991. // <--- [zBuffer] List of dynamic var commands
  10992. // Pet stat [Lance]
  10993. BUILDIN_FUNC(petstat)
  10994. {
  10995. TBL_PC *sd = NULL;
  10996. struct pet_data *pd;
  10997. int flag = script_getnum(st,2);
  10998. sd = script_rid2sd(st);
  10999. if(!sd || !sd->status.pet_id || !sd->pd){
  11000. if(flag == 2)
  11001. script_pushconststr(st, "");
  11002. else
  11003. script_pushint(st,0);
  11004. return 0;
  11005. }
  11006. pd = sd->pd;
  11007. switch(flag){
  11008. case 1: script_pushint(st,(int)pd->pet.class_); break;
  11009. case 2: script_pushstrcopy(st, pd->pet.name); break;
  11010. case 3: script_pushint(st,(int)pd->pet.level); break;
  11011. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  11012. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  11013. default:
  11014. script_pushint(st,0);
  11015. break;
  11016. }
  11017. return 0;
  11018. }
  11019. BUILDIN_FUNC(callshop)
  11020. {
  11021. TBL_PC *sd = NULL;
  11022. struct npc_data *nd;
  11023. const char *shopname;
  11024. int flag = 0;
  11025. sd = script_rid2sd(st);
  11026. if (!sd) {
  11027. script_pushint(st,0);
  11028. return 0;
  11029. }
  11030. shopname = script_getstr(st, 2);
  11031. if( script_hasdata(st,3) )
  11032. flag = script_getnum(st,3);
  11033. nd = npc_name2id(shopname);
  11034. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  11035. {
  11036. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  11037. script_pushint(st,0);
  11038. return 1;
  11039. }
  11040. if( nd->subtype == SHOP )
  11041. {
  11042. switch( flag )
  11043. {
  11044. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  11045. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  11046. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  11047. }
  11048. }
  11049. else
  11050. clif_cashshop_show(sd, nd);
  11051. sd->npc_shopid = nd->bl.id;
  11052. script_pushint(st,1);
  11053. return 0;
  11054. }
  11055. BUILDIN_FUNC(npcshopitem)
  11056. {
  11057. const char* npcname = script_getstr(st, 2);
  11058. struct npc_data* nd = npc_name2id(npcname);
  11059. int n, i;
  11060. int amount;
  11061. if( !nd || nd->subtype != SHOP )
  11062. { //Not found.
  11063. script_pushint(st,0);
  11064. return 0;
  11065. }
  11066. // get the count of new entries
  11067. amount = (script_lastdata(st)-2)/2;
  11068. // generate new shop item list
  11069. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  11070. for( n = 0, i = 3; n < amount; n++, i+=2 )
  11071. {
  11072. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11073. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11074. }
  11075. nd->u.shop.count = n;
  11076. script_pushint(st,1);
  11077. return 0;
  11078. }
  11079. BUILDIN_FUNC(npcshopadditem)
  11080. {
  11081. const char* npcname = script_getstr(st,2);
  11082. struct npc_data* nd = npc_name2id(npcname);
  11083. int n, i;
  11084. int amount;
  11085. if( !nd || nd->subtype != SHOP )
  11086. { //Not found.
  11087. script_pushint(st,0);
  11088. return 0;
  11089. }
  11090. // get the count of new entries
  11091. amount = (script_lastdata(st)-2)/2;
  11092. // append new items to existing shop item list
  11093. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  11094. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  11095. {
  11096. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11097. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11098. }
  11099. nd->u.shop.count = n;
  11100. script_pushint(st,1);
  11101. return 0;
  11102. }
  11103. BUILDIN_FUNC(npcshopdelitem)
  11104. {
  11105. const char* npcname = script_getstr(st,2);
  11106. struct npc_data* nd = npc_name2id(npcname);
  11107. int n, i;
  11108. int amount;
  11109. int size;
  11110. if( !nd || nd->subtype != SHOP )
  11111. { //Not found.
  11112. script_pushint(st,0);
  11113. return 0;
  11114. }
  11115. amount = script_lastdata(st)-2;
  11116. size = nd->u.shop.count;
  11117. // remove specified items from the shop item list
  11118. for( i = 3; i < 3 + amount; i++ )
  11119. {
  11120. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  11121. if( n < size )
  11122. {
  11123. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  11124. size--;
  11125. }
  11126. }
  11127. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  11128. nd->u.shop.count = size;
  11129. script_pushint(st,1);
  11130. return 0;
  11131. }
  11132. //Sets a script to attach to a shop npc.
  11133. BUILDIN_FUNC(npcshopattach)
  11134. {
  11135. const char* npcname = script_getstr(st,2);
  11136. struct npc_data* nd = npc_name2id(npcname);
  11137. int flag = 1;
  11138. if( script_hasdata(st,3) )
  11139. flag = script_getnum(st,3);
  11140. if( !nd || nd->subtype != SHOP )
  11141. { //Not found.
  11142. script_pushint(st,0);
  11143. return 0;
  11144. }
  11145. if (flag)
  11146. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  11147. else
  11148. nd->master_nd = NULL;
  11149. script_pushint(st,1);
  11150. return 0;
  11151. }
  11152. /*==========================================
  11153. * Returns some values of an item [Lupus]
  11154. * Price, Weight, etc...
  11155. setitemscript(itemID,"{new item bonus script}",[n]);
  11156. Where n:
  11157. 0 - script
  11158. 1 - Equip script
  11159. 2 - Unequip script
  11160. *------------------------------------------*/
  11161. BUILDIN_FUNC(setitemscript)
  11162. {
  11163. int item_id,n=0;
  11164. const char *script;
  11165. struct item_data *i_data;
  11166. struct script_code **dstscript;
  11167. item_id = script_getnum(st,2);
  11168. script = script_getstr(st,3);
  11169. if( script_hasdata(st,4) )
  11170. n=script_getnum(st,4);
  11171. i_data = itemdb_exists(item_id);
  11172. if (!i_data || script==NULL || script[0]!='{') {
  11173. script_pushint(st,0);
  11174. return 0;
  11175. }
  11176. switch (n) {
  11177. case 2:
  11178. dstscript = &i_data->unequip_script;
  11179. break;
  11180. case 1:
  11181. dstscript = &i_data->equip_script;
  11182. break;
  11183. default:
  11184. dstscript = &i_data->script;
  11185. break;
  11186. }
  11187. if(*dstscript)
  11188. script_free_code(*dstscript);
  11189. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11190. script_pushint(st,1);
  11191. return 0;
  11192. }
  11193. /* Work In Progress [Lupus]
  11194. BUILDIN_FUNC(addmonsterdrop)
  11195. {
  11196. int class_,item_id,chance;
  11197. class_=script_getnum(st,2);
  11198. item_id=script_getnum(st,3);
  11199. chance=script_getnum(st,4);
  11200. if(class_>1000 && item_id>500 && chance>0) {
  11201. script_pushint(st,1);
  11202. } else {
  11203. script_pushint(st,0);
  11204. }
  11205. }
  11206. BUILDIN_FUNC(delmonsterdrop)
  11207. {
  11208. int class_,item_id;
  11209. class_=script_getnum(st,2);
  11210. item_id=script_getnum(st,3);
  11211. if(class_>1000 && item_id>500) {
  11212. script_pushint(st,1);
  11213. } else {
  11214. script_pushint(st,0);
  11215. }
  11216. }
  11217. */
  11218. /*==========================================
  11219. * Returns some values of a monster [Lupus]
  11220. * Name, Level, race, size, etc...
  11221. getmonsterinfo(monsterID,queryIndex);
  11222. *------------------------------------------*/
  11223. BUILDIN_FUNC(getmonsterinfo)
  11224. {
  11225. struct mob_db *mob;
  11226. int mob_id;
  11227. mob_id = script_getnum(st,2);
  11228. if (!mobdb_checkid(mob_id)) {
  11229. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  11230. if ( !script_getnum(st,3) ) //requested a string
  11231. script_pushconststr(st,"null");
  11232. else
  11233. script_pushint(st,-1);
  11234. return -1;
  11235. }
  11236. mob = mob_db(mob_id);
  11237. switch ( script_getnum(st,3) ) {
  11238. case 0: script_pushstrcopy(st,mob->jname); break;
  11239. case 1: script_pushint(st,mob->lv); break;
  11240. case 2: script_pushint(st,mob->status.max_hp); break;
  11241. case 3: script_pushint(st,mob->base_exp); break;
  11242. case 4: script_pushint(st,mob->job_exp); break;
  11243. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11244. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11245. case 7: script_pushint(st,mob->status.def); break;
  11246. case 8: script_pushint(st,mob->status.mdef); break;
  11247. case 9: script_pushint(st,mob->status.str); break;
  11248. case 10: script_pushint(st,mob->status.agi); break;
  11249. case 11: script_pushint(st,mob->status.vit); break;
  11250. case 12: script_pushint(st,mob->status.int_); break;
  11251. case 13: script_pushint(st,mob->status.dex); break;
  11252. case 14: script_pushint(st,mob->status.luk); break;
  11253. case 15: script_pushint(st,mob->status.rhw.range); break;
  11254. case 16: script_pushint(st,mob->range2); break;
  11255. case 17: script_pushint(st,mob->range3); break;
  11256. case 18: script_pushint(st,mob->status.size); break;
  11257. case 19: script_pushint(st,mob->status.race); break;
  11258. case 20: script_pushint(st,mob->status.def_ele); break;
  11259. case 21: script_pushint(st,mob->status.mode); break;
  11260. default: script_pushint(st,-1); //wrong Index
  11261. }
  11262. return 0;
  11263. }
  11264. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11265. {
  11266. TBL_PC *sd = NULL;
  11267. if(script_hasdata(st,2))
  11268. sd = map_nick2sd(script_getstr(st,2));
  11269. else
  11270. sd = script_rid2sd(st);
  11271. if(sd)
  11272. script_pushint(st,(sd->vender_id != 0));
  11273. else
  11274. script_pushint(st,0);
  11275. return 0;
  11276. }
  11277. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11278. {
  11279. TBL_PC *sd = NULL;
  11280. if(script_hasdata(st,2))
  11281. sd = map_nick2sd(script_getstr(st,2));
  11282. else
  11283. sd = script_rid2sd(st);
  11284. if(sd)
  11285. script_pushint(st,(sd->chatID != 0));
  11286. else
  11287. script_pushint(st,0);
  11288. return 0;
  11289. }
  11290. BUILDIN_FUNC(searchitem)
  11291. {
  11292. struct script_data* data = script_getdata(st, 2);
  11293. const char *itemname = script_getstr(st,3);
  11294. struct item_data *items[MAX_SEARCH];
  11295. int count;
  11296. char* name;
  11297. int32 start;
  11298. int32 id;
  11299. int32 i;
  11300. TBL_PC* sd = NULL;
  11301. if ((items[0] = itemdb_exists(atoi(itemname))))
  11302. count = 1;
  11303. else {
  11304. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  11305. if (count > MAX_SEARCH) count = MAX_SEARCH;
  11306. }
  11307. if (!count) {
  11308. script_pushint(st, 0);
  11309. return 0;
  11310. }
  11311. if( !data_isreference(data) )
  11312. {
  11313. ShowError("script:searchitem: not a variable\n");
  11314. script_reportdata(data);
  11315. st->state = END;
  11316. return 1;// not a variable
  11317. }
  11318. id = reference_getid(data);
  11319. start = reference_getindex(data);
  11320. name = reference_getname(data);
  11321. if( not_array_variable(*name) )
  11322. {
  11323. ShowError("script:searchitem: illegal scope\n");
  11324. script_reportdata(data);
  11325. st->state = END;
  11326. return 1;// not supported
  11327. }
  11328. if( not_server_variable(*name) )
  11329. {
  11330. sd = script_rid2sd(st);
  11331. if( sd == NULL )
  11332. return 0;// no player attached
  11333. }
  11334. if( is_string_variable(name) )
  11335. {// string array
  11336. ShowError("script:searchitem: not an integer array reference\n");
  11337. script_reportdata(data);
  11338. st->state = END;
  11339. return 1;// not supported
  11340. }
  11341. for( i = 0; i < count; ++start, ++i )
  11342. {// Set array
  11343. void* v = (void*)items[i]->nameid;
  11344. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  11345. }
  11346. script_pushint(st, count);
  11347. return 0;
  11348. }
  11349. int axtoi(const char *hexStg)
  11350. {
  11351. int n = 0; // position in string
  11352. int m = 0; // position in digit[] to shift
  11353. int count; // loop index
  11354. int intValue = 0; // integer value of hex string
  11355. int digit[11]; // hold values to convert
  11356. while (n < 10) {
  11357. if (hexStg[n]=='\0')
  11358. break;
  11359. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11360. digit[n] = hexStg[n] & 0x0f; //convert to int
  11361. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11362. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11363. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11364. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11365. else break;
  11366. n++;
  11367. }
  11368. count = n;
  11369. m = n - 1;
  11370. n = 0;
  11371. while(n < count) {
  11372. // digit[n] is value of hex digit at position n
  11373. // (m << 2) is the number of positions to shift
  11374. // OR the bits into return value
  11375. intValue = intValue | (digit[n] << (m << 2));
  11376. m--; // adjust the position to set
  11377. n++; // next digit to process
  11378. }
  11379. return (intValue);
  11380. }
  11381. // [Lance] Hex string to integer converter
  11382. BUILDIN_FUNC(axtoi)
  11383. {
  11384. const char *hex = script_getstr(st,2);
  11385. script_pushint(st,axtoi(hex));
  11386. return 0;
  11387. }
  11388. // [zBuffer] List of player cont commands --->
  11389. BUILDIN_FUNC(rid2name)
  11390. {
  11391. struct block_list *bl = NULL;
  11392. int rid = script_getnum(st,2);
  11393. if((bl = map_id2bl(rid)))
  11394. {
  11395. switch(bl->type) {
  11396. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  11397. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  11398. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  11399. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  11400. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  11401. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  11402. default:
  11403. ShowError("buildin_rid2name: BL type unknown.\n");
  11404. script_pushconststr(st,"");
  11405. break;
  11406. }
  11407. } else {
  11408. ShowError("buildin_rid2name: invalid RID\n");
  11409. script_pushconststr(st,"(null)");
  11410. }
  11411. return 0;
  11412. }
  11413. BUILDIN_FUNC(pcblockmove)
  11414. {
  11415. int id, flag;
  11416. TBL_PC *sd = NULL;
  11417. id = script_getnum(st,2);
  11418. flag = script_getnum(st,3);
  11419. if(id)
  11420. sd = map_id2sd(id);
  11421. else
  11422. sd = script_rid2sd(st);
  11423. if(sd)
  11424. sd->state.blockedmove = flag > 0;
  11425. return 0;
  11426. }
  11427. BUILDIN_FUNC(pcfollow)
  11428. {
  11429. int id, targetid;
  11430. TBL_PC *sd = NULL;
  11431. id = script_getnum(st,2);
  11432. targetid = script_getnum(st,3);
  11433. if(id)
  11434. sd = map_id2sd(id);
  11435. else
  11436. sd = script_rid2sd(st);
  11437. if(sd)
  11438. pc_follow(sd, targetid);
  11439. return 0;
  11440. }
  11441. BUILDIN_FUNC(pcstopfollow)
  11442. {
  11443. int id;
  11444. TBL_PC *sd = NULL;
  11445. id = script_getnum(st,2);
  11446. if(id)
  11447. sd = map_id2sd(id);
  11448. else
  11449. sd = script_rid2sd(st);
  11450. if(sd)
  11451. pc_stop_following(sd);
  11452. return 0;
  11453. }
  11454. // <--- [zBuffer] List of player cont commands
  11455. // [zBuffer] List of mob control commands --->
  11456. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11457. /// Makes the unit walk to target position or map
  11458. /// Returns if it was successfull
  11459. ///
  11460. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11461. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11462. BUILDIN_FUNC(unitwalk)
  11463. {
  11464. struct block_list* bl;
  11465. bl = map_id2bl(script_getnum(st,2));
  11466. if( bl == NULL )
  11467. {
  11468. script_pushint(st, 0);
  11469. }
  11470. else if( script_hasdata(st,4) )
  11471. {
  11472. int x = script_getnum(st,3);
  11473. int y = script_getnum(st,4);
  11474. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11475. }
  11476. else
  11477. {
  11478. int map_id = script_getnum(st,3);
  11479. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11480. }
  11481. return 0;
  11482. }
  11483. /// Kills the unit
  11484. ///
  11485. /// unitkill <unit_id>;
  11486. BUILDIN_FUNC(unitkill)
  11487. {
  11488. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11489. if( bl != NULL )
  11490. status_kill(bl);
  11491. return 0;
  11492. }
  11493. /// Warps the unit to the target position in the target map
  11494. /// Returns if it was successfull
  11495. ///
  11496. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11497. BUILDIN_FUNC(unitwarp)
  11498. {
  11499. int unit_id;
  11500. int map;
  11501. short x;
  11502. short y;
  11503. struct block_list* bl;
  11504. unit_id = script_getnum(st,2);
  11505. map = map_mapname2mapid(script_getstr(st, 3));
  11506. x = (short)script_getnum(st,4);
  11507. y = (short)script_getnum(st,5);
  11508. bl = map_id2bl(unit_id);
  11509. if( map >= 0 && bl != NULL )
  11510. script_pushint(st, unit_warp(bl,map,x,y,0));
  11511. else
  11512. script_pushint(st, 0);
  11513. return 0;
  11514. }
  11515. /// Makes the unit attack the target.
  11516. /// If the unit is a player and <action type> is not 0, it does a continuous
  11517. /// attack instead of a single attack.
  11518. /// Returns if the request was successfull.
  11519. ///
  11520. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11521. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11522. BUILDIN_FUNC(unitattack)
  11523. {
  11524. struct block_list* unit_bl;
  11525. struct block_list* target_bl = NULL;
  11526. struct script_data* data;
  11527. int actiontype = 0;
  11528. // get unit
  11529. unit_bl = map_id2bl(script_getnum(st,2));
  11530. if( unit_bl == NULL ) {
  11531. script_pushint(st, 0);
  11532. return 0;
  11533. }
  11534. data = script_getdata(st, 3);
  11535. get_val(st, data);
  11536. if( data_isstring(data) )
  11537. {
  11538. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11539. if( sd != NULL )
  11540. target_bl = &sd->bl;
  11541. } else
  11542. target_bl = map_id2bl(conv_num(st, data));
  11543. // request the attack
  11544. if( target_bl == NULL )
  11545. {
  11546. script_pushint(st, 0);
  11547. return 0;
  11548. }
  11549. // get actiontype
  11550. if( script_hasdata(st,4) )
  11551. actiontype = script_getnum(st,4);
  11552. switch( unit_bl->type )
  11553. {
  11554. case BL_PC:
  11555. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11556. script_pushint(st, 1);
  11557. return 0;
  11558. case BL_MOB:
  11559. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11560. break;
  11561. case BL_PET:
  11562. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11563. break;
  11564. default:
  11565. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11566. script_pushint(st, 0);
  11567. return 1;
  11568. }
  11569. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11570. return 0;
  11571. }
  11572. /// Makes the unit stop attacking and moving
  11573. ///
  11574. /// unitstop <unit_id>;
  11575. BUILDIN_FUNC(unitstop)
  11576. {
  11577. int unit_id;
  11578. struct block_list* bl;
  11579. unit_id = script_getnum(st,2);
  11580. bl = map_id2bl(unit_id);
  11581. if( bl != NULL )
  11582. {
  11583. unit_stop_attack(bl);
  11584. unit_stop_walking(bl,4);
  11585. if( bl->type == BL_MOB )
  11586. ((TBL_MOB*)bl)->target_id = 0;
  11587. }
  11588. return 0;
  11589. }
  11590. /// Makes the unit say the message
  11591. ///
  11592. /// unittalk <unit_id>,"<message>";
  11593. BUILDIN_FUNC(unittalk)
  11594. {
  11595. int unit_id;
  11596. const char* message;
  11597. struct block_list* bl;
  11598. unit_id = script_getnum(st,2);
  11599. message = script_getstr(st, 3);
  11600. bl = map_id2bl(unit_id);
  11601. if( bl != NULL )
  11602. {
  11603. struct StringBuf sbuf;
  11604. StringBuf_Init(&sbuf);
  11605. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11606. clif_message(bl, StringBuf_Value(&sbuf));
  11607. if( bl->type == BL_PC )
  11608. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11609. StringBuf_Destroy(&sbuf);
  11610. }
  11611. return 0;
  11612. }
  11613. /// Makes the unit do an emotion
  11614. ///
  11615. /// unitemote <unit_id>,<emotion>;
  11616. ///
  11617. /// @see e_* in const.txt
  11618. BUILDIN_FUNC(unitemote)
  11619. {
  11620. int unit_id;
  11621. int emotion;
  11622. struct block_list* bl;
  11623. unit_id = script_getnum(st,2);
  11624. emotion = script_getnum(st,3);
  11625. bl = map_id2bl(unit_id);
  11626. if( bl != NULL )
  11627. clif_emotion(bl, emotion);
  11628. return 0;
  11629. }
  11630. /// Makes the unit cast the skill on the target or self if no target is specified
  11631. ///
  11632. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11633. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11634. BUILDIN_FUNC(unitskilluseid)
  11635. {
  11636. int unit_id;
  11637. int skill_id;
  11638. int skill_lv;
  11639. int target_id;
  11640. struct block_list* bl;
  11641. unit_id = script_getnum(st,2);
  11642. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11643. skill_lv = script_getnum(st,4);
  11644. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11645. bl = map_id2bl(unit_id);
  11646. if( bl != NULL )
  11647. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11648. return 0;
  11649. }
  11650. /// Makes the unit cast the skill on the target position.
  11651. ///
  11652. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11653. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11654. BUILDIN_FUNC(unitskillusepos)
  11655. {
  11656. int unit_id;
  11657. int skill_id;
  11658. int skill_lv;
  11659. int skill_x;
  11660. int skill_y;
  11661. struct block_list* bl;
  11662. unit_id = script_getnum(st,2);
  11663. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11664. skill_lv = script_getnum(st,4);
  11665. skill_x = script_getnum(st,5);
  11666. skill_y = script_getnum(st,6);
  11667. bl = map_id2bl(unit_id);
  11668. if( bl != NULL )
  11669. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11670. return 0;
  11671. }
  11672. // <--- [zBuffer] List of mob control commands
  11673. /// Pauses the execution of the script, detaching the player
  11674. ///
  11675. /// sleep <mili seconds>;
  11676. BUILDIN_FUNC(sleep)
  11677. {
  11678. int ticks;
  11679. ticks = script_getnum(st,2);
  11680. // detach the player
  11681. script_detach_rid(st);
  11682. if( ticks <= 0 )
  11683. {// do nothing
  11684. }
  11685. else if( st->sleep.tick == 0 )
  11686. {// sleep for the target amount of time
  11687. st->state = RERUNLINE;
  11688. st->sleep.tick = ticks;
  11689. }
  11690. else
  11691. {// sleep time is over
  11692. st->state = RUN;
  11693. st->sleep.tick = 0;
  11694. }
  11695. return 0;
  11696. }
  11697. /// Pauses the execution of the script, keeping the player attached
  11698. /// Returns if a player is still attached
  11699. ///
  11700. /// sleep2(<mili secconds>) -> <bool>
  11701. BUILDIN_FUNC(sleep2)
  11702. {
  11703. int ticks;
  11704. ticks = script_getnum(st,2);
  11705. if( ticks <= 0 )
  11706. {// do nothing
  11707. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11708. }
  11709. else if( !st->sleep.tick )
  11710. {// sleep for the target amount of time
  11711. st->state = RERUNLINE;
  11712. st->sleep.tick = ticks;
  11713. }
  11714. else
  11715. {// sleep time is over
  11716. st->state = RUN;
  11717. st->sleep.tick = 0;
  11718. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11719. }
  11720. return 0;
  11721. }
  11722. /// Awakes all the sleep timers of the target npc
  11723. ///
  11724. /// awake "<npc name>";
  11725. BUILDIN_FUNC(awake)
  11726. {
  11727. struct npc_data* nd;
  11728. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11729. nd = npc_name2id(script_getstr(st, 2));
  11730. if( nd == NULL ) {
  11731. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  11732. return 1;
  11733. }
  11734. while( node )
  11735. {
  11736. if( (int)node->key == nd->bl.id )
  11737. {// sleep timer for the npc
  11738. struct script_state* tst = (struct script_state*)node->data;
  11739. TBL_PC* sd = map_id2sd(tst->rid);
  11740. if( tst->sleep.timer == -1 )
  11741. {// already awake ???
  11742. node = node->next;
  11743. continue;
  11744. }
  11745. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11746. {// char not online anymore / another char of the same account is online - Cancel execution
  11747. tst->state = END;
  11748. tst->rid = 0;
  11749. }
  11750. delete_timer(tst->sleep.timer, run_script_timer);
  11751. node = script_erase_sleepdb(node);
  11752. tst->sleep.timer = INVALID_TIMER;
  11753. if(tst->state != RERUNLINE)
  11754. tst->sleep.tick = 0;
  11755. run_script_main(tst);
  11756. }
  11757. else
  11758. {
  11759. node = node->next;
  11760. }
  11761. }
  11762. return 0;
  11763. }
  11764. /// Returns a reference to a variable of the target NPC.
  11765. /// Returns 0 if an error occurs.
  11766. ///
  11767. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11768. BUILDIN_FUNC(getvariableofnpc)
  11769. {
  11770. struct script_data* data;
  11771. const char* name;
  11772. struct npc_data* nd;
  11773. data = script_getdata(st,2);
  11774. if( !data_isreference(data) )
  11775. {// Not a reference (aka varaible name)
  11776. ShowError("script:getvariableofnpc: not a variable\n");
  11777. script_reportdata(data);
  11778. script_pushnil(st);
  11779. st->state = END;
  11780. return 1;
  11781. }
  11782. name = reference_getname(data);
  11783. if( *name != '.' || name[1] == '@' )
  11784. {// not a npc variable
  11785. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11786. script_reportdata(data);
  11787. script_pushnil(st);
  11788. st->state = END;
  11789. return 1;
  11790. }
  11791. nd = npc_name2id(script_getstr(st,3));
  11792. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11793. {// NPC not found or has no script
  11794. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11795. script_pushnil(st);
  11796. st->state = END;
  11797. return 1;
  11798. }
  11799. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11800. return 0;
  11801. }
  11802. /// Opens a warp portal.
  11803. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11804. ///
  11805. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11806. ///
  11807. /// @author blackhole89
  11808. BUILDIN_FUNC(warpportal)
  11809. {
  11810. int spx;
  11811. int spy;
  11812. unsigned short mapindex;
  11813. int tpx;
  11814. int tpy;
  11815. struct skill_unit_group* group;
  11816. struct block_list* bl;
  11817. bl = map_id2bl(st->oid);
  11818. if( bl == NULL )
  11819. {
  11820. ShowError("script:warpportal: npc is needed\n");
  11821. return 1;
  11822. }
  11823. spx = script_getnum(st,2);
  11824. spy = script_getnum(st,3);
  11825. mapindex = mapindex_name2id(script_getstr(st, 4));
  11826. tpx = script_getnum(st,5);
  11827. tpy = script_getnum(st,6);
  11828. if( mapindex == 0 )
  11829. return 0;// map not found
  11830. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11831. if( group == NULL )
  11832. return 0;// failed
  11833. group->val2 = (tpx<<16) | tpy;
  11834. group->val3 = mapindex;
  11835. return 0;
  11836. }
  11837. BUILDIN_FUNC(openmail)
  11838. {
  11839. TBL_PC* sd;
  11840. sd = script_rid2sd(st);
  11841. if( sd == NULL )
  11842. return 0;
  11843. #ifndef TXT_ONLY
  11844. mail_openmail(sd);
  11845. #endif
  11846. return 0;
  11847. }
  11848. BUILDIN_FUNC(openauction)
  11849. {
  11850. TBL_PC* sd;
  11851. sd = script_rid2sd(st);
  11852. if( sd == NULL )
  11853. return 0;
  11854. #ifndef TXT_ONLY
  11855. clif_Auction_openwindow(sd);
  11856. #endif
  11857. return 0;
  11858. }
  11859. /// Retrieves the value of the specified flag of the specified cell.
  11860. ///
  11861. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11862. ///
  11863. /// @see cell_chk* constants in const.txt for the types
  11864. BUILDIN_FUNC(checkcell)
  11865. {
  11866. int m = map_mapname2mapid(script_getstr(st,2));
  11867. int x = script_getnum(st,3);
  11868. int y = script_getnum(st,4);
  11869. cell_chk type = (cell_chk)script_getnum(st,5);
  11870. script_pushint(st, map_getcell(m, x, y, type));
  11871. return 0;
  11872. }
  11873. /// Modifies flags of cells in the specified area.
  11874. ///
  11875. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11876. ///
  11877. /// @see cell_* constants in const.txt for the types
  11878. BUILDIN_FUNC(setcell)
  11879. {
  11880. int m = map_mapname2mapid(script_getstr(st,2));
  11881. int x1 = script_getnum(st,3);
  11882. int y1 = script_getnum(st,4);
  11883. int x2 = script_getnum(st,5);
  11884. int y2 = script_getnum(st,6);
  11885. cell_t type = (cell_t)script_getnum(st,7);
  11886. bool flag = (bool)script_getnum(st,8);
  11887. int x,y;
  11888. if( x1 > x2 ) swap(x1,x2);
  11889. if( y1 > y2 ) swap(y1,y2);
  11890. for( y = y1; y <= y2; ++y )
  11891. for( x = x1; x <= x2; ++x )
  11892. map_setcell(m, x, y, type, flag);
  11893. return 0;
  11894. }
  11895. /*==========================================
  11896. * Mercenary Commands
  11897. *------------------------------------------*/
  11898. BUILDIN_FUNC(mercenary_create)
  11899. {
  11900. struct map_session_data *sd;
  11901. int class_, contract_time;
  11902. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11903. return 0;
  11904. class_ = script_getnum(st,2);
  11905. if( !merc_class(class_) )
  11906. return 0;
  11907. contract_time = script_getnum(st,3);
  11908. merc_create(sd, class_, contract_time);
  11909. return 0;
  11910. }
  11911. BUILDIN_FUNC(mercenary_heal)
  11912. {
  11913. struct map_session_data *sd = script_rid2sd(st);
  11914. int hp, sp;
  11915. if( sd == NULL || sd->md == NULL )
  11916. return 0;
  11917. hp = script_getnum(st,2);
  11918. sp = script_getnum(st,3);
  11919. status_heal(&sd->md->bl, hp, sp, 0);
  11920. return 0;
  11921. }
  11922. BUILDIN_FUNC(mercenary_sc_start)
  11923. {
  11924. struct map_session_data *sd = script_rid2sd(st);
  11925. enum sc_type type;
  11926. int tick, val1;
  11927. if( sd == NULL || sd->md == NULL )
  11928. return 0;
  11929. type = (sc_type)script_getnum(st,2);
  11930. tick = script_getnum(st,3);
  11931. val1 = script_getnum(st,4);
  11932. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11933. return 0;
  11934. }
  11935. BUILDIN_FUNC(mercenary_get_calls)
  11936. {
  11937. struct map_session_data *sd = script_rid2sd(st);
  11938. int guild;
  11939. if( sd == NULL )
  11940. return 0;
  11941. guild = script_getnum(st,2);
  11942. switch( guild )
  11943. {
  11944. case ARCH_MERC_GUILD:
  11945. script_pushint(st,sd->status.arch_calls);
  11946. break;
  11947. case SPEAR_MERC_GUILD:
  11948. script_pushint(st,sd->status.spear_calls);
  11949. break;
  11950. case SWORD_MERC_GUILD:
  11951. script_pushint(st,sd->status.sword_calls);
  11952. break;
  11953. default:
  11954. script_pushint(st,0);
  11955. break;
  11956. }
  11957. return 0;
  11958. }
  11959. BUILDIN_FUNC(mercenary_set_calls)
  11960. {
  11961. struct map_session_data *sd = script_rid2sd(st);
  11962. int guild, value, *calls;
  11963. if( sd == NULL )
  11964. return 0;
  11965. guild = script_getnum(st,2);
  11966. value = script_getnum(st,3);
  11967. switch( guild )
  11968. {
  11969. case ARCH_MERC_GUILD:
  11970. calls = &sd->status.arch_calls;
  11971. break;
  11972. case SPEAR_MERC_GUILD:
  11973. calls = &sd->status.spear_calls;
  11974. break;
  11975. case SWORD_MERC_GUILD:
  11976. calls = &sd->status.sword_calls;
  11977. break;
  11978. default:
  11979. return 0; // Invalid Guild
  11980. }
  11981. *calls += value;
  11982. *calls = cap_value(*calls, 0, INT_MAX);
  11983. return 0;
  11984. }
  11985. BUILDIN_FUNC(mercenary_get_faith)
  11986. {
  11987. struct map_session_data *sd = script_rid2sd(st);
  11988. int guild;
  11989. if( sd == NULL )
  11990. return 0;
  11991. guild = script_getnum(st,2);
  11992. switch( guild )
  11993. {
  11994. case ARCH_MERC_GUILD:
  11995. script_pushint(st,sd->status.arch_faith);
  11996. break;
  11997. case SPEAR_MERC_GUILD:
  11998. script_pushint(st,sd->status.spear_faith);
  11999. break;
  12000. case SWORD_MERC_GUILD:
  12001. script_pushint(st,sd->status.sword_faith);
  12002. break;
  12003. default:
  12004. script_pushint(st,0);
  12005. break;
  12006. }
  12007. return 0;
  12008. }
  12009. BUILDIN_FUNC(mercenary_set_faith)
  12010. {
  12011. struct map_session_data *sd = script_rid2sd(st);
  12012. int guild, value, *calls;
  12013. if( sd == NULL )
  12014. return 0;
  12015. guild = script_getnum(st,2);
  12016. value = script_getnum(st,3);
  12017. switch( guild )
  12018. {
  12019. case ARCH_MERC_GUILD:
  12020. calls = &sd->status.arch_faith;
  12021. break;
  12022. case SPEAR_MERC_GUILD:
  12023. calls = &sd->status.spear_faith;
  12024. break;
  12025. case SWORD_MERC_GUILD:
  12026. calls = &sd->status.sword_faith;
  12027. break;
  12028. default:
  12029. return 0; // Invalid Guild
  12030. }
  12031. *calls += value;
  12032. *calls = cap_value(*calls, 0, INT_MAX);
  12033. if( mercenary_get_guild(sd->md) == guild )
  12034. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  12035. return 0;
  12036. }
  12037. /*------------------------------------------
  12038. * Book Reading
  12039. *------------------------------------------*/
  12040. BUILDIN_FUNC(readbook)
  12041. {
  12042. struct map_session_data *sd;
  12043. int book_id, page;
  12044. if( (sd = script_rid2sd(st)) == NULL )
  12045. return 0;
  12046. book_id = script_getnum(st,2);
  12047. page = script_getnum(st,3);
  12048. clif_readbook(sd->fd, book_id, page);
  12049. return 0;
  12050. }
  12051. /******************
  12052. Questlog script commands
  12053. *******************/
  12054. BUILDIN_FUNC(setquest)
  12055. {
  12056. TBL_PC * sd = script_rid2sd(st);
  12057. quest_add(sd, script_getnum(st, 2));
  12058. return 0;
  12059. }
  12060. BUILDIN_FUNC(erasequest)
  12061. {
  12062. TBL_PC * sd = script_rid2sd(st);
  12063. quest_delete(sd, script_getnum(st, 2));
  12064. return 0;
  12065. }
  12066. BUILDIN_FUNC(completequest)
  12067. {
  12068. TBL_PC * sd = script_rid2sd(st);
  12069. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  12070. return 0;
  12071. }
  12072. BUILDIN_FUNC(changequest)
  12073. {
  12074. TBL_PC * sd = script_rid2sd(st);
  12075. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  12076. return 0;
  12077. }
  12078. BUILDIN_FUNC(checkquest)
  12079. {
  12080. TBL_PC * sd = script_rid2sd(st);
  12081. quest_check_type type = HAVEQUEST;
  12082. if( script_hasdata(st, 3) )
  12083. type = (quest_check_type)script_getnum(st, 3);
  12084. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  12085. return 0;
  12086. }
  12087. BUILDIN_FUNC(showevent)
  12088. {
  12089. TBL_PC *sd = script_rid2sd(st);
  12090. struct npc_data *nd = map_id2nd(st->oid);
  12091. int state, color;
  12092. if( sd == NULL || nd == NULL )
  12093. return 0;
  12094. state = script_getnum(st, 2);
  12095. color = script_getnum(st, 3);
  12096. if( color < 0 || color > 4 )
  12097. color = 0; // set default color
  12098. clif_quest_show_event(sd, &nd->bl, state, color);
  12099. return 0;
  12100. }
  12101. /*==========================================
  12102. * BattleGround System
  12103. *------------------------------------------*/
  12104. BUILDIN_FUNC(waitingroom2bg)
  12105. {
  12106. struct npc_data *nd;
  12107. struct chat_data *cd;
  12108. const char *map_name, *ev = "", *dev = "";
  12109. int x, y, i, mapindex = 0, bg_id, n;
  12110. struct map_session_data *sd;
  12111. if( script_hasdata(st,7) )
  12112. nd = npc_name2id(script_getstr(st,7));
  12113. else
  12114. nd = (struct npc_data *)map_id2bl(st->oid);
  12115. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  12116. {
  12117. script_pushint(st,0);
  12118. return 0;
  12119. }
  12120. map_name = script_getstr(st,2);
  12121. if( strcmp(map_name,"-") != 0 )
  12122. {
  12123. mapindex = mapindex_name2id(map_name);
  12124. if( mapindex == 0 )
  12125. { // Invalid Map
  12126. script_pushint(st,0);
  12127. return 0;
  12128. }
  12129. }
  12130. x = script_getnum(st,3);
  12131. y = script_getnum(st,4);
  12132. ev = script_getstr(st,5); // Logout Event
  12133. dev = script_getstr(st,6); // Die Event
  12134. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  12135. { // Creation failed
  12136. script_pushint(st,0);
  12137. return 0;
  12138. }
  12139. n = cd->users;
  12140. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  12141. {
  12142. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  12143. mapreg_setreg(add_str("$@arenamembers") + (i<<24), sd->bl.id);
  12144. else
  12145. mapreg_setreg(add_str("$@arenamembers") + (i<<24), 0);
  12146. }
  12147. mapreg_setreg(add_str("$@arenamembersnum"), i);
  12148. script_pushint(st,bg_id);
  12149. return 0;
  12150. }
  12151. BUILDIN_FUNC(waitingroom2bg_single)
  12152. {
  12153. const char* map_name;
  12154. struct npc_data *nd;
  12155. struct chat_data *cd;
  12156. struct map_session_data *sd;
  12157. int x, y, mapindex, bg_id;
  12158. bg_id = script_getnum(st,2);
  12159. map_name = script_getstr(st,3);
  12160. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12161. return 0; // Invalid Map
  12162. x = script_getnum(st,4);
  12163. y = script_getnum(st,5);
  12164. nd = npc_name2id(script_getstr(st,6));
  12165. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  12166. return 0;
  12167. if( (sd = cd->usersd[0]) == NULL )
  12168. return 0;
  12169. if( bg_team_join(bg_id, sd) )
  12170. {
  12171. pc_setpos(sd, mapindex, x, y, 3);
  12172. script_pushint(st,1);
  12173. }
  12174. else
  12175. script_pushint(st,0);
  12176. return 0;
  12177. }
  12178. BUILDIN_FUNC(bg_team_setxy)
  12179. {
  12180. struct battleground_data *bg;
  12181. int bg_id;
  12182. bg_id = script_getnum(st,2);
  12183. if( (bg = bg_team_search(bg_id)) == NULL )
  12184. return 0;
  12185. bg->x = script_getnum(st,3);
  12186. bg->y = script_getnum(st,4);
  12187. return 0;
  12188. }
  12189. BUILDIN_FUNC(bg_warp)
  12190. {
  12191. int x, y, mapindex, bg_id;
  12192. const char* map_name;
  12193. bg_id = script_getnum(st,2);
  12194. map_name = script_getstr(st,3);
  12195. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12196. return 0; // Invalid Map
  12197. x = script_getnum(st,4);
  12198. y = script_getnum(st,5);
  12199. bg_team_warp(bg_id, mapindex, x, y);
  12200. return 0;
  12201. }
  12202. BUILDIN_FUNC(bg_monster)
  12203. {
  12204. int class_ = 0, x = 0, y = 0, bg_id = 0;
  12205. const char *str,*map, *evt="";
  12206. bg_id = script_getnum(st,2);
  12207. map = script_getstr(st,3);
  12208. x = script_getnum(st,4);
  12209. y = script_getnum(st,5);
  12210. str = script_getstr(st,6);
  12211. class_ = script_getnum(st,7);
  12212. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  12213. check_event(st, evt);
  12214. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  12215. return 0;
  12216. }
  12217. BUILDIN_FUNC(bg_monster_set_team)
  12218. {
  12219. struct mob_data *md;
  12220. struct block_list *mbl;
  12221. int id = script_getnum(st,2),
  12222. bg_id = script_getnum(st,3);
  12223. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  12224. return 0;
  12225. md = (TBL_MOB *)mbl;
  12226. md->state.bg_id = bg_id;
  12227. mob_stop_attack(md);
  12228. mob_stop_walking(md, 0);
  12229. md->target_id = md->attacked_id = 0;
  12230. clif_charnameack(0, &md->bl);
  12231. return 0;
  12232. }
  12233. BUILDIN_FUNC(bg_leave)
  12234. {
  12235. struct map_session_data *sd = script_rid2sd(st);
  12236. if( sd == NULL || !sd->state.bg_id )
  12237. return 0;
  12238. bg_team_leave(sd,0);
  12239. return 0;
  12240. }
  12241. BUILDIN_FUNC(bg_destroy)
  12242. {
  12243. int bg_id = script_getnum(st,2);
  12244. bg_team_delete(bg_id);
  12245. return 0;
  12246. }
  12247. BUILDIN_FUNC(bg_getareausers)
  12248. {
  12249. const char *str;
  12250. int m, x0, y0, x1, y1, bg_id;
  12251. int i = 0, c = 0;
  12252. struct battleground_data *bg = NULL;
  12253. struct map_session_data *sd;
  12254. bg_id = script_getnum(st,2);
  12255. str = script_getstr(st,3);
  12256. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  12257. {
  12258. script_pushint(st,0);
  12259. return 0;
  12260. }
  12261. x0 = script_getnum(st,4);
  12262. y0 = script_getnum(st,5);
  12263. x1 = script_getnum(st,6);
  12264. y1 = script_getnum(st,7);
  12265. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  12266. {
  12267. if( (sd = bg->members[i].sd) == NULL )
  12268. continue;
  12269. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  12270. continue;
  12271. c++;
  12272. }
  12273. script_pushint(st,c);
  12274. return 0;
  12275. }
  12276. BUILDIN_FUNC(bg_updatescore)
  12277. {
  12278. const char *str;
  12279. int m;
  12280. str = script_getstr(st,2);
  12281. if( (m = map_mapname2mapid(str)) < 0 )
  12282. return 0;
  12283. map[m].bgscore_lion = script_getnum(st,3);
  12284. map[m].bgscore_eagle = script_getnum(st,4);
  12285. clif_bg_updatescore(m);
  12286. return 0;
  12287. }
  12288. BUILDIN_FUNC(bg_get_data)
  12289. {
  12290. struct battleground_data *bg;
  12291. int bg_id = script_getnum(st,2),
  12292. type = script_getnum(st,3);
  12293. if( (bg = bg_team_search(bg_id)) == NULL )
  12294. {
  12295. script_pushint(st,0);
  12296. return 0;
  12297. }
  12298. switch( type )
  12299. {
  12300. case 0: script_pushint(st, bg->count); break;
  12301. default:
  12302. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  12303. break;
  12304. }
  12305. return 0;
  12306. }
  12307. /*==========================================
  12308. * Instancing Script Commands
  12309. *------------------------------------------*/
  12310. BUILDIN_FUNC(instance_create)
  12311. {
  12312. const char *name;
  12313. int party_id, res;
  12314. name = script_getstr(st, 2);
  12315. party_id = script_getnum(st, 3);
  12316. res = instance_create(party_id, name);
  12317. if( res == -4 ) // Already exists
  12318. {
  12319. script_pushint(st, -1);
  12320. return 0;
  12321. }
  12322. else if( res < 0 )
  12323. {
  12324. char *err;
  12325. switch(res)
  12326. {
  12327. case -3: err = "No free instances"; break;
  12328. case -2: err = "Missing parameter"; break;
  12329. case -1: err = "Invalid type"; break;
  12330. default: err = "Unknown"; break;
  12331. }
  12332. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  12333. script_pushint(st, -2);
  12334. return 0;
  12335. }
  12336. script_pushint(st, res);
  12337. return 0;
  12338. }
  12339. BUILDIN_FUNC(instance_destroy)
  12340. {
  12341. int instance_id;
  12342. struct map_session_data *sd;
  12343. struct party_data *p;
  12344. if( script_hasdata(st, 2) )
  12345. instance_id = script_getnum(st, 2);
  12346. else if( st->instance_id )
  12347. instance_id = st->instance_id;
  12348. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12349. instance_id = p->instance_id;
  12350. else return 0;
  12351. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12352. {
  12353. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  12354. return 0;
  12355. }
  12356. instance_destroy(instance_id);
  12357. return 0;
  12358. }
  12359. BUILDIN_FUNC(instance_attachmap)
  12360. {
  12361. const char *name;
  12362. int m;
  12363. int instance_id;
  12364. bool usebasename = false;
  12365. name = script_getstr(st,2);
  12366. instance_id = script_getnum(st,3);
  12367. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  12368. usebasename = true;
  12369. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  12370. {
  12371. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  12372. script_pushconststr(st, "");
  12373. return 0;
  12374. }
  12375. script_pushconststr(st, map[m].name);
  12376. return 0;
  12377. }
  12378. BUILDIN_FUNC(instance_detachmap)
  12379. {
  12380. struct map_session_data *sd;
  12381. struct party_data *p;
  12382. const char *str;
  12383. int m, instance_id;
  12384. str = script_getstr(st, 2);
  12385. if( script_hasdata(st, 3) )
  12386. instance_id = script_getnum(st, 3);
  12387. else if( st->instance_id )
  12388. instance_id = st->instance_id;
  12389. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12390. instance_id = p->instance_id;
  12391. else return 0;
  12392. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  12393. {
  12394. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  12395. return 0;
  12396. }
  12397. instance_del_map(m);
  12398. return 0;
  12399. }
  12400. BUILDIN_FUNC(instance_attach)
  12401. {
  12402. int instance_id;
  12403. instance_id = script_getnum(st, 2);
  12404. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12405. return 0;
  12406. st->instance_id = instance_id;
  12407. return 0;
  12408. }
  12409. BUILDIN_FUNC(instance_id)
  12410. {
  12411. int type, instance_id;
  12412. struct map_session_data *sd;
  12413. struct party_data *p;
  12414. if( script_hasdata(st, 2) )
  12415. {
  12416. type = script_getnum(st, 2);
  12417. if( type == 0 )
  12418. instance_id = st->instance_id;
  12419. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  12420. instance_id = p->instance_id;
  12421. else
  12422. instance_id = 0;
  12423. }
  12424. else
  12425. instance_id = st->instance_id;
  12426. script_pushint(st, instance_id);
  12427. return 0;
  12428. }
  12429. BUILDIN_FUNC(instance_set_timeout)
  12430. {
  12431. int progress_timeout, idle_timeout;
  12432. int instance_id;
  12433. struct map_session_data *sd;
  12434. struct party_data *p;
  12435. progress_timeout = script_getnum(st, 2);
  12436. idle_timeout = script_getnum(st, 3);
  12437. if( script_hasdata(st, 4) )
  12438. instance_id = script_getnum(st, 4);
  12439. else if( st->instance_id )
  12440. instance_id = st->instance_id;
  12441. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12442. instance_id = p->instance_id;
  12443. else return 0;
  12444. if( instance_id > 0 )
  12445. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  12446. return 0;
  12447. }
  12448. BUILDIN_FUNC(instance_init)
  12449. {
  12450. int instance_id = script_getnum(st, 2);
  12451. if( instance[instance_id].state != INSTANCE_IDLE )
  12452. {
  12453. ShowError("instance_init: instance already initialized.\n");
  12454. return 0;
  12455. }
  12456. instance_init(instance_id);
  12457. return 0;
  12458. }
  12459. BUILDIN_FUNC(instance_announce)
  12460. {
  12461. int instance_id = script_getnum(st,2);
  12462. const char *mes = script_getstr(st,3);
  12463. int flag = script_getnum(st,4);
  12464. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  12465. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  12466. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  12467. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  12468. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  12469. int i;
  12470. struct map_session_data *sd;
  12471. struct party_data *p;
  12472. if( instance_id == 0 )
  12473. {
  12474. if( st->instance_id )
  12475. instance_id = st->instance_id;
  12476. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12477. instance_id = p->instance_id;
  12478. else return 0;
  12479. }
  12480. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12481. return 0;
  12482. for( i = 0; i < instance[instance_id].num_map; i++ )
  12483. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  12484. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  12485. return 0;
  12486. }
  12487. BUILDIN_FUNC(instance_npcname)
  12488. {
  12489. const char *str;
  12490. int instance_id = 0;
  12491. struct map_session_data *sd;
  12492. struct party_data *p;
  12493. struct npc_data *nd;
  12494. str = script_getstr(st, 2);
  12495. if( script_hasdata(st, 3) )
  12496. instance_id = script_getnum(st, 3);
  12497. else if( st->instance_id )
  12498. instance_id = st->instance_id;
  12499. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12500. instance_id = p->instance_id;
  12501. if( instance_id && (nd = npc_name2id(str)) != NULL )
  12502. {
  12503. static char npcname[NAME_LENGTH];
  12504. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  12505. script_pushconststr(st,npcname);
  12506. }
  12507. else
  12508. {
  12509. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  12510. st->state = END;
  12511. return 1;
  12512. }
  12513. return 0;
  12514. }
  12515. BUILDIN_FUNC(has_instance)
  12516. {
  12517. struct map_session_data *sd;
  12518. struct party_data *p;
  12519. const char *str;
  12520. int m, instance_id = 0;
  12521. str = script_getstr(st, 2);
  12522. if( script_hasdata(st, 3) )
  12523. instance_id = script_getnum(st, 3);
  12524. else if( st->instance_id )
  12525. instance_id = st->instance_id;
  12526. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12527. instance_id = p->instance_id;
  12528. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  12529. {
  12530. script_pushconststr(st, "");
  12531. return 0;
  12532. }
  12533. script_pushconststr(st, map[m].name);
  12534. return 0;
  12535. }
  12536. BUILDIN_FUNC(instance_warpall)
  12537. {
  12538. struct map_session_data *pl_sd;
  12539. int m, i, instance_id;
  12540. const char *mapn;
  12541. int x, y;
  12542. unsigned short mapindex;
  12543. struct party_data *p = NULL;
  12544. mapn = script_getstr(st,2);
  12545. x = script_getnum(st,3);
  12546. y = script_getnum(st,4);
  12547. if( script_hasdata(st,5) )
  12548. instance_id = script_getnum(st,5);
  12549. else if( st->instance_id )
  12550. instance_id = st->instance_id;
  12551. else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id )
  12552. instance_id = p->instance_id;
  12553. else return 0;
  12554. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  12555. return 0;
  12556. if( !(p = party_search(instance[instance_id].party_id)) )
  12557. return 0;
  12558. mapindex = map_id2index(m);
  12559. for( i = 0; i < MAX_PARTY; i++ )
  12560. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == st->instance_id ) pc_setpos(pl_sd,mapindex,x,y,3);
  12561. return 0;
  12562. }
  12563. /*==========================================
  12564. * Custom Fonts
  12565. *------------------------------------------*/
  12566. BUILDIN_FUNC(setfont)
  12567. {
  12568. struct map_session_data *sd = script_rid2sd(st);
  12569. int font = script_getnum(st,2);
  12570. if( sd == NULL )
  12571. return 0;
  12572. if( sd->state.user_font != font )
  12573. sd->state.user_font = font;
  12574. else
  12575. sd->state.user_font = 0;
  12576. clif_font(sd);
  12577. return 0;
  12578. }
  12579. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  12580. {
  12581. TBL_MOB* md = (TBL_MOB*)bl;
  12582. struct block_list *tbl;
  12583. int mobid = va_arg(ap,int);
  12584. int skillid = va_arg(ap,int);
  12585. int skilllv = va_arg(ap,int);
  12586. int casttime = va_arg(ap,int);
  12587. int cancel = va_arg(ap,int);
  12588. int emotion = va_arg(ap,int);
  12589. int target = va_arg(ap,int);
  12590. if( md->class_ != mobid )
  12591. return 0;
  12592. if( md->ud.skilltimer != -1 ) // Cancel the casting skill.
  12593. unit_skillcastcancel(bl,0);
  12594. // 0:self, 1:target, 2:master, default:random
  12595. switch( target )
  12596. {
  12597. case 0: tbl = map_id2bl(md->bl.id); break;
  12598. case 1: tbl = map_id2bl(md->target_id); break;
  12599. case 2: tbl = map_id2bl(md->master_id); break;
  12600. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  12601. }
  12602. if( !tbl )
  12603. return 0;
  12604. if( skill_get_casttype(skillid) == CAST_GROUND )
  12605. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  12606. else
  12607. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  12608. clif_emotion(&md->bl, emotion);
  12609. return 0;
  12610. }
  12611. /*==========================================
  12612. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  12613. *------------------------------------------*/
  12614. BUILDIN_FUNC(areamobuseskill)
  12615. {
  12616. struct block_list center;
  12617. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  12618. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  12619. {
  12620. ShowError("areamobuseskill: invalid map name.\n");
  12621. return 0;
  12622. }
  12623. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  12624. return 0;
  12625. center.m = m;
  12626. center.x = script_getnum(st,3);
  12627. center.y = script_getnum(st,4);
  12628. range = script_getnum(st,5);
  12629. mobid = script_getnum(st,6);
  12630. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  12631. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  12632. skilllv = battle_config.mob_max_skilllvl;
  12633. casttime = script_getnum(st,9);
  12634. cancel = script_getnum(st,10);
  12635. emotion = script_getnum(st,11);
  12636. target = script_getnum(st,12);
  12637. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  12638. return 0;
  12639. }
  12640. BUILDIN_FUNC(progressbar)
  12641. {
  12642. #if PACKETVER >= 20080318
  12643. struct map_session_data * sd = script_rid2sd(st);
  12644. const char * color;
  12645. unsigned int second;
  12646. if( !st || !sd )
  12647. return 0;
  12648. st->state = STOP;
  12649. color = script_getstr(st,2);
  12650. second = script_getnum(st,3);
  12651. sd->progressbar.npc_id = st->oid;
  12652. sd->progressbar.timeout = gettick() + second*1000;
  12653. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  12654. #endif
  12655. return 0;
  12656. }
  12657. BUILDIN_FUNC(pushpc)
  12658. {
  12659. int direction, cells, dx, dy;
  12660. struct map_session_data* sd;
  12661. if((sd = script_rid2sd(st))==NULL)
  12662. {
  12663. return 0;
  12664. }
  12665. direction = script_getnum(st,2);
  12666. cells = script_getnum(st,3);
  12667. if(direction<0 || direction>7)
  12668. {
  12669. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  12670. script_reportsrc(st);
  12671. direction%= 8; // trim spin-over
  12672. }
  12673. if(!cells)
  12674. {// zero distance
  12675. return 0;
  12676. }
  12677. else if(cells<0)
  12678. {// pushing backwards
  12679. direction = (direction+4)%8; // turn around
  12680. cells = -cells;
  12681. }
  12682. dx = dirx[direction];
  12683. dy = diry[direction];
  12684. unit_blown(&sd->bl, dx, dy, cells, 0);
  12685. return 0;
  12686. }
  12687. // declarations that were supposed to be exported from npc_chat.c
  12688. #ifdef PCRE_SUPPORT
  12689. BUILDIN_FUNC(defpattern);
  12690. BUILDIN_FUNC(activatepset);
  12691. BUILDIN_FUNC(deactivatepset);
  12692. BUILDIN_FUNC(deletepset);
  12693. #endif
  12694. struct script_function buildin_func[] = {
  12695. // NPC interaction
  12696. BUILDIN_DEF(mes,"s"),
  12697. BUILDIN_DEF(next,""),
  12698. BUILDIN_DEF(close,""),
  12699. BUILDIN_DEF(close2,""),
  12700. BUILDIN_DEF(menu,"sl*"),
  12701. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  12702. BUILDIN_DEF(prompt,"s*"),
  12703. //
  12704. BUILDIN_DEF(goto,"l"),
  12705. BUILDIN_DEF(callsub,"i*"),
  12706. BUILDIN_DEF(callfunc,"s*"),
  12707. BUILDIN_DEF(return,"?"),
  12708. BUILDIN_DEF(getarg,"i?"),
  12709. BUILDIN_DEF(jobchange,"i*"),
  12710. BUILDIN_DEF(jobname,"i"),
  12711. BUILDIN_DEF(input,"v??"),
  12712. BUILDIN_DEF(warp,"sii"),
  12713. BUILDIN_DEF(areawarp,"siiiisii"),
  12714. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  12715. BUILDIN_DEF(warpparty,"siii*"), // [Fredzilla] [Paradox924X]
  12716. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  12717. BUILDIN_DEF(setlook,"ii"),
  12718. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  12719. BUILDIN_DEF(set,"ii"),
  12720. BUILDIN_DEF(setarray,"rv*"),
  12721. BUILDIN_DEF(cleararray,"rvi"),
  12722. BUILDIN_DEF(copyarray,"rri"),
  12723. BUILDIN_DEF(getarraysize,"r"),
  12724. BUILDIN_DEF(deletearray,"r?"),
  12725. BUILDIN_DEF(getelementofarray,"ri"),
  12726. BUILDIN_DEF(getitem,"vi?"),
  12727. BUILDIN_DEF(rentitem,"vi"),
  12728. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  12729. BUILDIN_DEF(getnameditem,"is"),
  12730. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  12731. BUILDIN_DEF(makeitem,"iisii"),
  12732. BUILDIN_DEF(delitem,"ii?"),
  12733. BUILDIN_DEF(delitem2,"iiiiiiiii?"),
  12734. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  12735. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  12736. BUILDIN_DEF(cutin,"si"),
  12737. BUILDIN_DEF(viewpoint,"iiiii"),
  12738. BUILDIN_DEF(heal,"ii"),
  12739. BUILDIN_DEF(itemheal,"ii"),
  12740. BUILDIN_DEF(percentheal,"ii"),
  12741. BUILDIN_DEF(rand,"i?"),
  12742. BUILDIN_DEF(countitem,"i"),
  12743. BUILDIN_DEF(countitem2,"iiiiiiii"),
  12744. BUILDIN_DEF(checkweight,"ii"),
  12745. BUILDIN_DEF(readparam,"i*"),
  12746. BUILDIN_DEF(getcharid,"i*"),
  12747. BUILDIN_DEF(getpartyname,"i"),
  12748. BUILDIN_DEF(getpartymember,"i*"),
  12749. BUILDIN_DEF(getpartyleader,"i?"),
  12750. BUILDIN_DEF(getguildname,"i"),
  12751. BUILDIN_DEF(getguildmaster,"i"),
  12752. BUILDIN_DEF(getguildmasterid,"i"),
  12753. BUILDIN_DEF(strcharinfo,"i"),
  12754. BUILDIN_DEF(strnpcinfo,"i"),
  12755. BUILDIN_DEF(getequipid,"i"),
  12756. BUILDIN_DEF(getequipname,"i"),
  12757. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  12758. BUILDIN_DEF(repair,"i"), // [Valaris]
  12759. BUILDIN_DEF(getequipisequiped,"i"),
  12760. BUILDIN_DEF(getequipisenableref,"i"),
  12761. BUILDIN_DEF(getequipisidentify,"i"),
  12762. BUILDIN_DEF(getequiprefinerycnt,"i"),
  12763. BUILDIN_DEF(getequipweaponlv,"i"),
  12764. BUILDIN_DEF(getequippercentrefinery,"i"),
  12765. BUILDIN_DEF(successrefitem,"i"),
  12766. BUILDIN_DEF(failedrefitem,"i"),
  12767. BUILDIN_DEF(statusup,"i"),
  12768. BUILDIN_DEF(statusup2,"ii"),
  12769. BUILDIN_DEF(bonus,"iv"),
  12770. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  12771. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  12772. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  12773. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  12774. BUILDIN_DEF(autobonus,"sii??"),
  12775. BUILDIN_DEF(autobonus2,"sii??"),
  12776. BUILDIN_DEF(autobonus3,"siiv?"),
  12777. BUILDIN_DEF(skill,"vi?"),
  12778. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  12779. BUILDIN_DEF(guildskill,"vi"),
  12780. BUILDIN_DEF(getskilllv,"v"),
  12781. BUILDIN_DEF(getgdskilllv,"iv"),
  12782. BUILDIN_DEF(basicskillcheck,""),
  12783. BUILDIN_DEF(getgmlevel,""),
  12784. BUILDIN_DEF(end,""),
  12785. BUILDIN_DEF(checkoption,"i"),
  12786. BUILDIN_DEF(setoption,"i?"),
  12787. BUILDIN_DEF(setcart,"?"),
  12788. BUILDIN_DEF(checkcart,""),
  12789. BUILDIN_DEF(setfalcon,"?"),
  12790. BUILDIN_DEF(checkfalcon,""),
  12791. BUILDIN_DEF(setriding,"?"),
  12792. BUILDIN_DEF(checkriding,""),
  12793. BUILDIN_DEF2(savepoint,"save","sii"),
  12794. BUILDIN_DEF(savepoint,"sii"),
  12795. BUILDIN_DEF(gettimetick,"i"),
  12796. BUILDIN_DEF(gettime,"i"),
  12797. BUILDIN_DEF(gettimestr,"si"),
  12798. BUILDIN_DEF(openstorage,""),
  12799. BUILDIN_DEF(guildopenstorage,"*"),
  12800. BUILDIN_DEF(itemskill,"vi"),
  12801. BUILDIN_DEF(produce,"i"),
  12802. BUILDIN_DEF(cooking,"i"),
  12803. BUILDIN_DEF(monster,"siisii*"),
  12804. BUILDIN_DEF(getmobdrops,"i"),
  12805. BUILDIN_DEF(areamonster,"siiiisii*"),
  12806. BUILDIN_DEF(killmonster,"ss?"),
  12807. BUILDIN_DEF(killmonsterall,"s?"),
  12808. BUILDIN_DEF(clone,"siisi*"),
  12809. BUILDIN_DEF(doevent,"s"),
  12810. BUILDIN_DEF(donpcevent,"s"),
  12811. BUILDIN_DEF(cmdothernpc,"ss"),
  12812. BUILDIN_DEF(addtimer,"is"),
  12813. BUILDIN_DEF(deltimer,"s"),
  12814. BUILDIN_DEF(addtimercount,"si"),
  12815. BUILDIN_DEF(initnpctimer,"??"),
  12816. BUILDIN_DEF(stopnpctimer,"??"),
  12817. BUILDIN_DEF(startnpctimer,"??"),
  12818. BUILDIN_DEF(setnpctimer,"i?"),
  12819. BUILDIN_DEF(getnpctimer,"i?"),
  12820. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  12821. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  12822. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  12823. BUILDIN_DEF(announce,"si*"),
  12824. BUILDIN_DEF(mapannounce,"ssi*"),
  12825. BUILDIN_DEF(areaannounce,"siiiisi*"),
  12826. BUILDIN_DEF(getusers,"i"),
  12827. BUILDIN_DEF(getmapguildusers,"si"),
  12828. BUILDIN_DEF(getmapusers,"s"),
  12829. BUILDIN_DEF(getareausers,"siiii"),
  12830. BUILDIN_DEF(getareadropitem,"siiiii"),
  12831. BUILDIN_DEF(enablenpc,"s"),
  12832. BUILDIN_DEF(disablenpc,"s"),
  12833. BUILDIN_DEF(hideoffnpc,"s"),
  12834. BUILDIN_DEF(hideonnpc,"s"),
  12835. BUILDIN_DEF(sc_start,"iii?"),
  12836. BUILDIN_DEF(sc_start2,"iiii?"),
  12837. BUILDIN_DEF(sc_start4,"iiiiii?"),
  12838. BUILDIN_DEF(sc_end,"i?"),
  12839. BUILDIN_DEF(getscrate,"ii*"),
  12840. BUILDIN_DEF(debugmes,"s"),
  12841. BUILDIN_DEF2(catchpet,"pet","i"),
  12842. BUILDIN_DEF2(birthpet,"bpet",""),
  12843. BUILDIN_DEF(resetlvl,"i"),
  12844. BUILDIN_DEF(resetstatus,""),
  12845. BUILDIN_DEF(resetskill,""),
  12846. BUILDIN_DEF(skillpointcount,""),
  12847. BUILDIN_DEF(changebase,"i"),
  12848. BUILDIN_DEF(changesex,""),
  12849. BUILDIN_DEF(waitingroom,"si??"),
  12850. BUILDIN_DEF(delwaitingroom,"?"),
  12851. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  12852. BUILDIN_DEF(enablewaitingroomevent,"?"),
  12853. BUILDIN_DEF(disablewaitingroomevent,"?"),
  12854. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  12855. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  12856. BUILDIN_DEF(getwaitingroomstate,"i?"),
  12857. BUILDIN_DEF(warpwaitingpc,"sii?"),
  12858. BUILDIN_DEF(attachrid,"i"),
  12859. BUILDIN_DEF(detachrid,""),
  12860. BUILDIN_DEF(isloggedin,"i?"),
  12861. BUILDIN_DEF(setmapflagnosave,"ssii"),
  12862. BUILDIN_DEF(getmapflag,"si"),
  12863. BUILDIN_DEF(setmapflag,"si*"),
  12864. BUILDIN_DEF(removemapflag,"si"),
  12865. BUILDIN_DEF(pvpon,"s"),
  12866. BUILDIN_DEF(pvpoff,"s"),
  12867. BUILDIN_DEF(gvgon,"s"),
  12868. BUILDIN_DEF(gvgoff,"s"),
  12869. BUILDIN_DEF(emotion,"i*"),
  12870. BUILDIN_DEF(maprespawnguildid,"sii"),
  12871. BUILDIN_DEF(agitstart,""), // <Agit>
  12872. BUILDIN_DEF(agitend,""),
  12873. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  12874. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  12875. BUILDIN_DEF(getcastlename,"s"),
  12876. BUILDIN_DEF(getcastledata,"si*"),
  12877. BUILDIN_DEF(setcastledata,"sii"),
  12878. BUILDIN_DEF(requestguildinfo,"i*"),
  12879. BUILDIN_DEF(getequipcardcnt,"i"),
  12880. BUILDIN_DEF(successremovecards,"i"),
  12881. BUILDIN_DEF(failedremovecards,"ii"),
  12882. BUILDIN_DEF(marriage,"s"),
  12883. BUILDIN_DEF2(wedding_effect,"wedding",""),
  12884. BUILDIN_DEF(divorce,""),
  12885. BUILDIN_DEF(ispartneron,""),
  12886. BUILDIN_DEF(getpartnerid,""),
  12887. BUILDIN_DEF(getchildid,""),
  12888. BUILDIN_DEF(getmotherid,""),
  12889. BUILDIN_DEF(getfatherid,""),
  12890. BUILDIN_DEF(warppartner,"sii"),
  12891. BUILDIN_DEF(getitemname,"i"),
  12892. BUILDIN_DEF(getitemslots,"i"),
  12893. BUILDIN_DEF(makepet,"i"),
  12894. BUILDIN_DEF(getexp,"ii"),
  12895. BUILDIN_DEF(getinventorylist,""),
  12896. BUILDIN_DEF(getskilllist,""),
  12897. BUILDIN_DEF(clearitem,""),
  12898. BUILDIN_DEF(classchange,"ii"),
  12899. BUILDIN_DEF(misceffect,"i"),
  12900. BUILDIN_DEF(playBGM,"s"),
  12901. BUILDIN_DEF(playBGMall,"s*"),
  12902. BUILDIN_DEF(soundeffect,"si"),
  12903. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  12904. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  12905. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  12906. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  12907. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  12908. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  12909. BUILDIN_DEF(petloot,"i"), // [Valaris]
  12910. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  12911. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  12912. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  12913. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  12914. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  12915. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  12916. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  12917. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  12918. BUILDIN_DEF(nude,""), // nude command [Valaris]
  12919. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  12920. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  12921. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  12922. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  12923. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  12924. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  12925. BUILDIN_DEF(mobcount,"ss"),
  12926. BUILDIN_DEF(getlook,"i"),
  12927. BUILDIN_DEF(getsavepoint,"i"),
  12928. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  12929. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  12930. BUILDIN_DEF(npcstop,""), // [Valaris]
  12931. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  12932. BUILDIN_DEF(checkoption1,"i"),
  12933. BUILDIN_DEF(checkoption2,"i"),
  12934. BUILDIN_DEF(guildgetexp,"i"),
  12935. BUILDIN_DEF(guildchangegm,"is"),
  12936. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  12937. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  12938. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  12939. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  12940. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  12941. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  12942. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  12943. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  12944. BUILDIN_DEF(night,""), // sets the server to night time
  12945. BUILDIN_DEF(day,""), // sets the server to day time
  12946. #ifdef PCRE_SUPPORT
  12947. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  12948. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  12949. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  12950. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  12951. #endif
  12952. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  12953. BUILDIN_DEF(getusersname,"*"),
  12954. BUILDIN_DEF(recovery,""),
  12955. BUILDIN_DEF(getpetinfo,"i"),
  12956. BUILDIN_DEF(gethominfo,"i"),
  12957. BUILDIN_DEF(checkequipedcard,"i"),
  12958. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  12959. BUILDIN_DEF(globalmes,"s*"),
  12960. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  12961. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  12962. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  12963. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  12964. BUILDIN_DEF(setnpcdisplay,"sv??"),
  12965. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  12966. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  12967. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  12968. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  12969. // [zBuffer] List of mathematics commands --->
  12970. BUILDIN_DEF(sqrt,"i"),
  12971. BUILDIN_DEF(pow,"ii"),
  12972. BUILDIN_DEF(distance,"iiii"),
  12973. // <--- [zBuffer] List of mathematics commands
  12974. BUILDIN_DEF(md5,"s"),
  12975. // [zBuffer] List of dynamic var commands --->
  12976. BUILDIN_DEF(getd,"*"),
  12977. BUILDIN_DEF(setd,"*"),
  12978. // <--- [zBuffer] List of dynamic var commands
  12979. BUILDIN_DEF(petstat,"i"),
  12980. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  12981. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  12982. BUILDIN_DEF(npcshopadditem,"sii*"),
  12983. BUILDIN_DEF(npcshopdelitem,"si*"),
  12984. BUILDIN_DEF(npcshopattach,"s?"),
  12985. BUILDIN_DEF(equip,"i"),
  12986. BUILDIN_DEF(autoequip,"ii"),
  12987. BUILDIN_DEF(setbattleflag,"ss"),
  12988. BUILDIN_DEF(getbattleflag,"s"),
  12989. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  12990. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  12991. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  12992. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  12993. BUILDIN_DEF(axtoi,"s"),
  12994. BUILDIN_DEF(query_sql,"s*"),
  12995. BUILDIN_DEF(query_logsql,"s*"),
  12996. BUILDIN_DEF(escape_sql,"s"),
  12997. BUILDIN_DEF(atoi,"s"),
  12998. // [zBuffer] List of player cont commands --->
  12999. BUILDIN_DEF(rid2name,"i"),
  13000. BUILDIN_DEF(pcfollow,"ii"),
  13001. BUILDIN_DEF(pcstopfollow,"i"),
  13002. BUILDIN_DEF(pcblockmove,"ii"),
  13003. // <--- [zBuffer] List of player cont commands
  13004. // [zBuffer] List of mob control commands --->
  13005. BUILDIN_DEF(unitwalk,"ii?"),
  13006. BUILDIN_DEF(unitkill,"i"),
  13007. BUILDIN_DEF(unitwarp,"isii"),
  13008. BUILDIN_DEF(unitattack,"iv?"),
  13009. BUILDIN_DEF(unitstop,"i"),
  13010. BUILDIN_DEF(unittalk,"is"),
  13011. BUILDIN_DEF(unitemote,"ii"),
  13012. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  13013. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  13014. // <--- [zBuffer] List of mob control commands
  13015. BUILDIN_DEF(sleep,"i"),
  13016. BUILDIN_DEF(sleep2,"i"),
  13017. BUILDIN_DEF(awake,"s"),
  13018. BUILDIN_DEF(getvariableofnpc,"rs"),
  13019. BUILDIN_DEF(warpportal,"iisii"),
  13020. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  13021. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  13022. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  13023. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  13024. BUILDIN_DEF(checkvending,"*"),
  13025. BUILDIN_DEF(checkchatting,"*"),
  13026. BUILDIN_DEF(openmail,""),
  13027. BUILDIN_DEF(openauction,""),
  13028. BUILDIN_DEF(checkcell,"siii"),
  13029. BUILDIN_DEF(setcell,"siiiiii"),
  13030. BUILDIN_DEF(setwall,"siiiiis"),
  13031. BUILDIN_DEF(delwall,"s"),
  13032. BUILDIN_DEF(searchitem,"rs"),
  13033. BUILDIN_DEF(mercenary_create,"ii"),
  13034. BUILDIN_DEF(mercenary_heal,"ii"),
  13035. BUILDIN_DEF(mercenary_sc_start,"iii"),
  13036. BUILDIN_DEF(mercenary_get_calls,"i"),
  13037. BUILDIN_DEF(mercenary_get_faith,"i"),
  13038. BUILDIN_DEF(mercenary_set_calls,"ii"),
  13039. BUILDIN_DEF(mercenary_set_faith,"ii"),
  13040. BUILDIN_DEF(readbook,"ii"),
  13041. BUILDIN_DEF(setfont,"i"),
  13042. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  13043. BUILDIN_DEF(progressbar, "si"),
  13044. BUILDIN_DEF(pushpc,"ii"),
  13045. // WoE SE
  13046. BUILDIN_DEF(agitstart2,""),
  13047. BUILDIN_DEF(agitend2,""),
  13048. BUILDIN_DEF(agitcheck2,""),
  13049. // BattleGround
  13050. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  13051. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  13052. BUILDIN_DEF(bg_team_setxy,"iii"),
  13053. BUILDIN_DEF(bg_warp,"isii"),
  13054. BUILDIN_DEF(bg_monster,"isiisi*"),
  13055. BUILDIN_DEF(bg_monster_set_team,"ii"),
  13056. BUILDIN_DEF(bg_leave,""),
  13057. BUILDIN_DEF(bg_destroy,"i"),
  13058. BUILDIN_DEF(areapercentheal,"siiiiii"),
  13059. BUILDIN_DEF(bg_get_data,"ii"),
  13060. BUILDIN_DEF(bg_getareausers,"isiiii"),
  13061. BUILDIN_DEF(bg_updatescore,"sii"),
  13062. // Instancing
  13063. BUILDIN_DEF(instance_create,"si"),
  13064. BUILDIN_DEF(instance_destroy,"?"),
  13065. BUILDIN_DEF(instance_attachmap,"si?"),
  13066. BUILDIN_DEF(instance_detachmap,"s?"),
  13067. BUILDIN_DEF(instance_attach,"i"),
  13068. BUILDIN_DEF(instance_id,"?"),
  13069. BUILDIN_DEF(instance_set_timeout,"ii?"),
  13070. BUILDIN_DEF(instance_init,"i"),
  13071. BUILDIN_DEF(instance_announce,"isi*"),
  13072. BUILDIN_DEF(instance_npcname,"s?"),
  13073. BUILDIN_DEF(has_instance,"s?"),
  13074. BUILDIN_DEF(instance_warpall,"sii?"),
  13075. //Quest Log System [Inkfish]
  13076. BUILDIN_DEF(setquest, "i"),
  13077. BUILDIN_DEF(erasequest, "i"),
  13078. BUILDIN_DEF(completequest, "i"),
  13079. BUILDIN_DEF(checkquest, "i*"),
  13080. BUILDIN_DEF(changequest, "ii"),
  13081. BUILDIN_DEF(showevent, "ii"),
  13082. {NULL,NULL,NULL},
  13083. };