script.c 355 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646
  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. struct Script_Config script_config = {
  182. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  183. 0, INT_MAX, // input_min_value/input_max_value
  184. "OnPCDieEvent", //die_event_name
  185. "OnPCKillEvent", //kill_pc_event_name
  186. "OnNPCKillEvent", //kill_mob_event_name
  187. "OnPCLoginEvent", //login_event_name
  188. "OnPCLogoutEvent", //logout_event_name
  189. "OnPCLoadMapEvent", //loadmap_event_name
  190. "OnPCBaseLvUpEvent", //baselvup_event_name
  191. "OnPCJobLvUpEvent", //joblvup_event_name
  192. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  193. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  194. };
  195. static jmp_buf error_jump;
  196. static char* error_msg;
  197. static const char* error_pos;
  198. static int error_report; // if the error should produce output
  199. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  200. // [Eoe / jA 1080, 1081, 1094, 1164]
  201. enum curly_type {
  202. TYPE_NULL = 0,
  203. TYPE_IF,
  204. TYPE_SWITCH,
  205. TYPE_WHILE,
  206. TYPE_FOR,
  207. TYPE_DO,
  208. TYPE_USERFUNC,
  209. TYPE_ARGLIST // function argument list
  210. };
  211. #define ARGLIST_UNDEFINED 0
  212. #define ARGLIST_NO_PAREN 1
  213. #define ARGLIST_PAREN 2
  214. static struct {
  215. struct {
  216. enum curly_type type;
  217. int index;
  218. int count;
  219. int flag;
  220. struct linkdb_node *case_label;
  221. } curly[256]; // 右カッコの情報
  222. int curly_count; // 右カッコの数
  223. int index; // スクリプト内で使用した構文の数
  224. } syntax;
  225. const char* parse_curly_close(const char* p);
  226. const char* parse_syntax_close(const char* p);
  227. const char* parse_syntax_close_sub(const char* p,int* flag);
  228. const char* parse_syntax(const char* p);
  229. static int parse_syntax_for_flag = 0;
  230. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  231. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  232. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  233. int potion_target=0;
  234. c_op get_com(unsigned char *script,int *pos);
  235. int get_num(unsigned char *script,int *pos);
  236. typedef struct script_function {
  237. int (*func)(struct script_state *st);
  238. const char *name;
  239. const char *arg;
  240. } script_function;
  241. extern script_function buildin_func[];
  242. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  243. uint32 crctab[256];
  244. /*==========================================
  245. * ローカルプロトタイプ宣言 (必要な物のみ)
  246. *------------------------------------------*/
  247. const char* parse_subexpr(const char* p,int limit);
  248. void push_val(struct script_stack *stack,int type,int val);
  249. int run_func(struct script_state *st);
  250. enum {
  251. MF_NOMEMO, //0
  252. MF_NOTELEPORT,
  253. MF_NOSAVE,
  254. MF_NOBRANCH,
  255. MF_NOPENALTY,
  256. MF_NOZENYPENALTY,
  257. MF_PVP,
  258. MF_PVP_NOPARTY,
  259. MF_PVP_NOGUILD,
  260. MF_GVG,
  261. MF_GVG_NOPARTY, //10
  262. MF_NOTRADE,
  263. MF_NOSKILL,
  264. MF_NOWARP,
  265. MF_PARTYLOCK,
  266. MF_NOICEWALL,
  267. MF_SNOW,
  268. MF_FOG,
  269. MF_SAKURA,
  270. MF_LEAVES,
  271. MF_RAIN, //20
  272. MF_INDOORS,
  273. MF_NOGO,
  274. MF_CLOUDS,
  275. MF_CLOUDS2,
  276. MF_FIREWORKS,
  277. MF_GVG_CASTLE,
  278. MF_GVG_DUNGEON,
  279. MF_NIGHTENABLED,
  280. MF_NOBASEEXP,
  281. MF_NOJOBEXP, //30
  282. MF_NOMOBLOOT,
  283. MF_NOMVPLOOT,
  284. MF_NORETURN,
  285. MF_NOWARPTO,
  286. MF_NIGHTMAREDROP,
  287. MF_RESTRICTED,
  288. MF_NOCOMMAND,
  289. MF_NODROP,
  290. MF_JEXP,
  291. MF_BEXP, //40
  292. MF_NOVENDING,
  293. MF_LOADEVENT,
  294. MF_NOCHAT,
  295. MF_NOEXPPENALTY,
  296. MF_GUILDLOCK
  297. };
  298. const char* script_op2name(int op)
  299. {
  300. #define RETURN_OP_NAME(type) case type: return #type
  301. switch( op )
  302. {
  303. RETURN_OP_NAME(C_NOP);
  304. RETURN_OP_NAME(C_POS);
  305. RETURN_OP_NAME(C_INT);
  306. RETURN_OP_NAME(C_PARAM);
  307. RETURN_OP_NAME(C_FUNC);
  308. RETURN_OP_NAME(C_STR);
  309. RETURN_OP_NAME(C_CONSTSTR);
  310. RETURN_OP_NAME(C_ARG);
  311. RETURN_OP_NAME(C_NAME);
  312. RETURN_OP_NAME(C_EOL);
  313. RETURN_OP_NAME(C_RETINFO);
  314. RETURN_OP_NAME(C_USERFUNC);
  315. RETURN_OP_NAME(C_USERFUNC_POS);
  316. // operators
  317. RETURN_OP_NAME(C_OP3);
  318. RETURN_OP_NAME(C_LOR);
  319. RETURN_OP_NAME(C_LAND);
  320. RETURN_OP_NAME(C_LE);
  321. RETURN_OP_NAME(C_LT);
  322. RETURN_OP_NAME(C_GE);
  323. RETURN_OP_NAME(C_GT);
  324. RETURN_OP_NAME(C_EQ);
  325. RETURN_OP_NAME(C_NE);
  326. RETURN_OP_NAME(C_XOR);
  327. RETURN_OP_NAME(C_OR);
  328. RETURN_OP_NAME(C_AND);
  329. RETURN_OP_NAME(C_ADD);
  330. RETURN_OP_NAME(C_SUB);
  331. RETURN_OP_NAME(C_MUL);
  332. RETURN_OP_NAME(C_DIV);
  333. RETURN_OP_NAME(C_MOD);
  334. RETURN_OP_NAME(C_NEG);
  335. RETURN_OP_NAME(C_LNOT);
  336. RETURN_OP_NAME(C_NOT);
  337. RETURN_OP_NAME(C_R_SHIFT);
  338. RETURN_OP_NAME(C_L_SHIFT);
  339. default:
  340. ShowDebug("script_op2name: unexpected op=%d\n", op);
  341. return "???";
  342. }
  343. #undef RETURN_OP_NAME
  344. }
  345. #ifdef DEBUG_DUMP_STACK
  346. static void script_dump_stack(struct script_state* st)
  347. {
  348. int i;
  349. ShowMessage("\tstart = %d\n", st->start);
  350. ShowMessage("\tend = %d\n", st->end);
  351. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  352. ShowMessage("\tsp = %d\n", st->stack->sp);
  353. for( i = 0; i < st->stack->sp; ++i )
  354. {
  355. struct script_data* data = &st->stack->stack_data[i];
  356. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  357. switch( data->type )
  358. {
  359. case C_INT:
  360. case C_POS:
  361. ShowMessage(" %d\n", data->u.num);
  362. break;
  363. case C_STR:
  364. case C_CONSTSTR:
  365. ShowMessage(" \"%s\"\n", data->u.str);
  366. break;
  367. case C_NAME:
  368. 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));
  369. break;
  370. case C_RETINFO:
  371. {
  372. struct script_retinfo* ri = data->u.ri;
  373. 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);
  374. }
  375. break;
  376. default:
  377. ShowMessage("\n");
  378. break;
  379. }
  380. }
  381. }
  382. #endif
  383. /// Reports on the console the src of a script error.
  384. static void script_reportsrc(struct script_state *st)
  385. {
  386. struct block_list* bl;
  387. if( st->oid == 0 )
  388. return; //Can't report source.
  389. bl = map_id2bl(st->oid);
  390. if( bl == NULL )
  391. return;
  392. switch( bl->type )
  393. {
  394. case BL_NPC:
  395. if( bl->m >= 0 )
  396. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  397. else
  398. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  399. break;
  400. default:
  401. if( bl->m >= 0 )
  402. 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);
  403. else
  404. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  405. break;
  406. }
  407. }
  408. /// Reports on the console information about the script data.
  409. static void script_reportdata(struct script_data* data)
  410. {
  411. if( data == NULL )
  412. return;
  413. switch( data->type )
  414. {
  415. case C_NOP:// no value
  416. ShowDebug("Data: nothing (nil)\n");
  417. break;
  418. case C_INT:// number
  419. ShowDebug("Data: number value=%d\n", data->u.num);
  420. break;
  421. case C_STR:
  422. case C_CONSTSTR:// string
  423. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  424. break;
  425. case C_NAME:// reference
  426. if( reference_tovariable(data) )
  427. {// variable
  428. const char* name = reference_getname(data);
  429. if( not_array_variable(*name) )
  430. ShowDebug("Data: variable name='%s'\n", name);
  431. else
  432. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  433. }
  434. else if( reference_toconstant(data) )
  435. {// constant
  436. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  437. }
  438. else if( reference_toparam(data) )
  439. {// param
  440. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  441. }
  442. else
  443. {// ???
  444. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  445. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  446. }
  447. break;
  448. case C_POS:// label
  449. ShowDebug("Data: label pos=%d\n", data->u.num);
  450. break;
  451. default:
  452. ShowDebug("Data: %s\n", script_op2name(data->type));
  453. break;
  454. }
  455. }
  456. /*==========================================
  457. * エラーメッセージ出力
  458. *------------------------------------------*/
  459. static void disp_error_message2(const char *mes,const char *pos,int report)
  460. {
  461. error_msg = aStrdup(mes);
  462. error_pos = pos;
  463. error_report = report;
  464. longjmp( error_jump, 1 );
  465. }
  466. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  467. /// Checks event parameter validity
  468. static void check_event(struct script_state *st, const char *evt)
  469. {
  470. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  471. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  472. script_reportsrc(st);
  473. }
  474. }
  475. /*==========================================
  476. * Hashes the input string
  477. *------------------------------------------*/
  478. static unsigned int calc_hash(const char* p)
  479. {
  480. unsigned int h;
  481. #if defined(SCRIPT_HASH_DJB2)
  482. h = 5381;
  483. while( *p ) // hash*33 + c
  484. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  485. #elif defined(SCRIPT_HASH_SDBM)
  486. h = 0;
  487. while( *p ) // hash*65599 + c
  488. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  489. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  490. h = 0;
  491. while( *p ){
  492. unsigned int g;
  493. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  494. g = h & 0xF0000000;
  495. if( g )
  496. {
  497. h ^= g >> 24;
  498. h &= ~g;
  499. }
  500. }
  501. #else // athena hash
  502. h = 0;
  503. while( *p )
  504. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  505. #endif
  506. return h % SCRIPT_HASH_SIZE;
  507. }
  508. /*==========================================
  509. * str_data manipulation functions
  510. *------------------------------------------*/
  511. /// Looks up string using the provided id.
  512. const char* get_str(int id)
  513. {
  514. Assert( id >= LABEL_START && id < str_size );
  515. return str_buf+str_data[id].str;
  516. }
  517. /// Returns the uid of the string, or -1.
  518. static int search_str(const char* p)
  519. {
  520. int i;
  521. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  522. if( strcasecmp(get_str(i),p) == 0 )
  523. return i;
  524. return -1;
  525. }
  526. /// Stores a copy of the string and returns its id.
  527. /// If an identical string is already present, returns its id instead.
  528. int add_str(const char* p)
  529. {
  530. int i, h;
  531. int len;
  532. h = calc_hash(p);
  533. if( str_hash[h] == 0 )
  534. {// empty bucket, add new node here
  535. str_hash[h] = str_num;
  536. }
  537. else
  538. {// scan for end of list, or occurence of identical string
  539. for( i = str_hash[h]; ; i = str_data[i].next )
  540. {
  541. if( strcasecmp(get_str(i),p) == 0 )
  542. return i; // string already in list
  543. if( str_data[i].next == 0 )
  544. break; // reached the end
  545. }
  546. // append node to end of list
  547. str_data[i].next = str_num;
  548. }
  549. // grow list if neccessary
  550. if( str_num >= str_data_size )
  551. {
  552. str_data_size += 128;
  553. RECREATE(str_data,struct str_data_struct,str_data_size);
  554. memset(str_data + (str_data_size - 128), '\0', 128);
  555. }
  556. len=(int)strlen(p);
  557. // grow string buffer if neccessary
  558. while( str_pos+len+1 >= str_size )
  559. {
  560. str_size += 256;
  561. RECREATE(str_buf,char,str_size);
  562. memset(str_buf + (str_size - 256), '\0', 256);
  563. }
  564. safestrncpy(str_buf+str_pos, p, len+1);
  565. str_data[str_num].type = C_NOP;
  566. str_data[str_num].str = str_pos;
  567. str_data[str_num].next = 0;
  568. str_data[str_num].func = NULL;
  569. str_data[str_num].backpatch = -1;
  570. str_data[str_num].label = -1;
  571. str_pos += len+1;
  572. return str_num++;
  573. }
  574. /// Appends 1 byte to the script buffer.
  575. static void add_scriptb(int a)
  576. {
  577. if( script_pos+1 >= script_size )
  578. {
  579. script_size += SCRIPT_BLOCK_SIZE;
  580. RECREATE(script_buf,unsigned char,script_size);
  581. }
  582. script_buf[script_pos++] = (uint8)(a);
  583. }
  584. /// Appends a c_op value to the script buffer.
  585. /// The value is variable-length encoded into 8-bit blocks.
  586. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  587. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  588. static void add_scriptc(int a)
  589. {
  590. while( a >= 0x40 )
  591. {
  592. add_scriptb((a&0x3f)|0x40);
  593. a = (a - 0x40) >> 6;
  594. }
  595. add_scriptb(a);
  596. }
  597. /// Appends an integer value to the script buffer.
  598. /// The value is variable-length encoded into 8-bit blocks.
  599. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  600. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  601. static void add_scripti(int a)
  602. {
  603. while( a >= 0x40 )
  604. {
  605. add_scriptb((a&0x3f)|0xc0);
  606. a = (a - 0x40) >> 6;
  607. }
  608. add_scriptb(a|0x80);
  609. }
  610. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  611. ///
  612. /// @param l The id of the str_data entry
  613. // 最大16Mまで
  614. static void add_scriptl(int l)
  615. {
  616. int backpatch = str_data[l].backpatch;
  617. switch(str_data[l].type){
  618. case C_POS:
  619. case C_USERFUNC_POS:
  620. add_scriptc(C_POS);
  621. add_scriptb(str_data[l].label);
  622. add_scriptb(str_data[l].label>>8);
  623. add_scriptb(str_data[l].label>>16);
  624. break;
  625. case C_NOP:
  626. case C_USERFUNC:
  627. // ラベルの可能性があるのでbackpatch用データ埋め込み
  628. add_scriptc(C_NAME);
  629. str_data[l].backpatch = script_pos;
  630. add_scriptb(backpatch);
  631. add_scriptb(backpatch>>8);
  632. add_scriptb(backpatch>>16);
  633. break;
  634. case C_INT:
  635. add_scripti(abs(str_data[l].val));
  636. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  637. add_scriptc(C_NEG);
  638. break;
  639. default: // assume C_NAME
  640. add_scriptc(C_NAME);
  641. add_scriptb(l);
  642. add_scriptb(l>>8);
  643. add_scriptb(l>>16);
  644. break;
  645. }
  646. }
  647. /*==========================================
  648. * ラベルを解決する
  649. *------------------------------------------*/
  650. void set_label(int l,int pos, const char* script_pos)
  651. {
  652. int i,next;
  653. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  654. { //Prevent overwriting constants values and parameters [Skotlex]
  655. disp_error_message("set_label: invalid label name",script_pos);
  656. return;
  657. }
  658. if(str_data[l].label!=-1){
  659. disp_error_message("set_label: dup label ",script_pos);
  660. return;
  661. }
  662. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  663. str_data[l].label=pos;
  664. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  665. next=GETVALUE(script_buf,i);
  666. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  667. SETVALUE(script_buf,i,pos);
  668. i=next;
  669. }
  670. }
  671. /// Skips spaces and/or comments.
  672. const char* skip_space(const char* p)
  673. {
  674. if( p == NULL )
  675. return NULL;
  676. for(;;)
  677. {
  678. while( ISSPACE(*p) )
  679. ++p;
  680. if( *p == '/' && p[1] == '/' )
  681. {// line comment
  682. while(*p && *p!='\n')
  683. ++p;
  684. }
  685. else if( *p == '/' && p[1] == '*' )
  686. {// block comment
  687. p += 2;
  688. for(;;)
  689. {
  690. if( *p == '\0' )
  691. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  692. if( *p == '*' && p[1] == '/' )
  693. {// end of block comment
  694. p += 2;
  695. break;
  696. }
  697. ++p;
  698. }
  699. }
  700. else
  701. break;
  702. }
  703. return p;
  704. }
  705. /// Skips a word.
  706. /// A word consists of undercores and/or alfanumeric characters,
  707. /// and valid variable prefixes/postfixes.
  708. static
  709. const char* skip_word(const char* p)
  710. {
  711. // prefix
  712. switch( *p )
  713. {
  714. case '@':// temporary char variable
  715. ++p; break;
  716. case '#':// account variable
  717. p += ( p[1] == '#' ? 2 : 1 ); break;
  718. case '\'':// instance variable
  719. ++p; break;
  720. case '.':// npc variable
  721. p += ( p[1] == '@' ? 2 : 1 ); break;
  722. case '$':// global variable
  723. p += ( p[1] == '@' ? 2 : 1 ); break;
  724. }
  725. while( ISALNUM(*p) || *p == '_' )
  726. ++p;
  727. // postfix
  728. if( *p == '$' )// string
  729. p++;
  730. return p;
  731. }
  732. /// Adds a word to str_data.
  733. /// @see skip_word
  734. /// @see add_str
  735. static
  736. int add_word(const char* p)
  737. {
  738. char* word;
  739. int len;
  740. int i;
  741. // Check for a word
  742. len = skip_word(p) - p;
  743. if( len == 0 )
  744. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  745. // Duplicate the word
  746. CREATE(word, char, len+1);
  747. memcpy(word, p, len);
  748. word[len] = 0;
  749. // add the word
  750. i = add_str(word);
  751. aFree(word);
  752. return i;
  753. }
  754. /// Parses a function call.
  755. /// The argument list can have parenthesis or not.
  756. /// The number of arguments is checked.
  757. static
  758. const char* parse_callfunc(const char* p, int require_paren)
  759. {
  760. const char* p2;
  761. const char* arg=NULL;
  762. int func;
  763. func = add_word(p);
  764. if( str_data[func].type == C_FUNC ){
  765. // buildin function
  766. add_scriptl(func);
  767. add_scriptc(C_ARG);
  768. arg = buildin_func[str_data[func].val].arg;
  769. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  770. // script defined function
  771. int callsub = search_str("callsub");
  772. add_scriptl(callsub);
  773. add_scriptc(C_ARG);
  774. add_scriptl(func);
  775. arg = buildin_func[str_data[callsub].val].arg;
  776. if( *arg == 0 )
  777. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  778. if( *arg != '*' )
  779. ++arg; // count func as argument
  780. } else
  781. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  782. p = skip_word(p);
  783. p = skip_space(p);
  784. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  785. syntax.curly[syntax.curly_count].count = 0;
  786. if( *p == ';' )
  787. {// <func name> ';'
  788. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  789. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  790. {// <func name> '(' ')'
  791. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  792. p = p2;
  793. /*
  794. } else if( 0 && require_paren && *p != '(' )
  795. {// <func name>
  796. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  797. */
  798. } else
  799. {// <func name> <arg list>
  800. if( require_paren ){
  801. if( *p != '(' )
  802. disp_error_message("need '('",p);
  803. ++p; // skip '('
  804. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  805. } else if( *p == '(' ){
  806. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  807. } else {
  808. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  809. }
  810. ++syntax.curly_count;
  811. while( *arg ) {
  812. p2=parse_subexpr(p,-1);
  813. if( p == p2 )
  814. break; // not an argument
  815. if( *arg != '*' )
  816. ++arg; // next argument
  817. p=skip_space(p2);
  818. if( *arg == 0 || *p != ',' )
  819. break; // no more arguments
  820. ++p; // skip comma
  821. }
  822. --syntax.curly_count;
  823. }
  824. if( *arg && *arg != '?' && *arg != '*' )
  825. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  826. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  827. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  828. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  829. if( *p != ')' )
  830. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  831. ++p;
  832. }
  833. add_scriptc(C_FUNC);
  834. return p;
  835. }
  836. /*==========================================
  837. * 項の解析
  838. *------------------------------------------*/
  839. const char* parse_simpleexpr(const char *p)
  840. {
  841. int i;
  842. p=skip_space(p);
  843. if(*p==';' || *p==',')
  844. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  845. if(*p=='('){
  846. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  847. ++syntax.curly[i].count;
  848. p=parse_subexpr(p+1,-1);
  849. p=skip_space(p);
  850. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  851. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  852. ){
  853. if( *p == ',' ){
  854. syntax.curly[i].flag = ARGLIST_PAREN;
  855. return p;
  856. } else
  857. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  858. }
  859. if( *p != ')' )
  860. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  861. ++p;
  862. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  863. char *np;
  864. i=strtoul(p,&np,0);
  865. add_scripti(i);
  866. p=np;
  867. } else if(*p=='"'){
  868. add_scriptc(C_STR);
  869. p++;
  870. while( *p && *p != '"' ){
  871. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  872. {
  873. char buf[8];
  874. size_t len = skip_escaped_c(p) - p;
  875. size_t n = sv_unescape_c(buf, p, len);
  876. if( n != 1 )
  877. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  878. p += len;
  879. add_scriptb(*buf);
  880. continue;
  881. }
  882. else if( *p == '\n' )
  883. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  884. add_scriptb(*p++);
  885. }
  886. if(!*p)
  887. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  888. add_scriptb(0);
  889. p++; //'"'
  890. } else {
  891. int l;
  892. // label , register , function etc
  893. if(skip_word(p)==p)
  894. disp_error_message("parse_simpleexpr: unexpected character",p);
  895. l=add_word(p);
  896. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  897. return parse_callfunc(p,1);
  898. p=skip_word(p);
  899. if( *p == '[' ){
  900. // array(name[i] => getelementofarray(name,i) )
  901. add_scriptl(search_str("getelementofarray"));
  902. add_scriptc(C_ARG);
  903. add_scriptl(l);
  904. p=parse_subexpr(p+1,-1);
  905. p=skip_space(p);
  906. if( *p != ']' )
  907. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  908. ++p;
  909. add_scriptc(C_FUNC);
  910. }else
  911. add_scriptl(l);
  912. }
  913. return p;
  914. }
  915. /*==========================================
  916. * 式の解析
  917. *------------------------------------------*/
  918. const char* parse_subexpr(const char* p,int limit)
  919. {
  920. int op,opl,len;
  921. const char* tmpp;
  922. p=skip_space(p);
  923. if(*p=='-'){
  924. tmpp=skip_space(p+1);
  925. if(*tmpp==';' || *tmpp==','){
  926. add_scriptl(LABEL_NEXTLINE);
  927. p++;
  928. return p;
  929. }
  930. }
  931. tmpp=p;
  932. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  933. p=parse_subexpr(p+1,10);
  934. add_scriptc(op);
  935. } else
  936. p=parse_simpleexpr(p);
  937. p=skip_space(p);
  938. while((
  939. (op=C_OP3,opl=0,len=1,*p=='?') ||
  940. (op=C_ADD,opl=8,len=1,*p=='+') ||
  941. (op=C_SUB,opl=8,len=1,*p=='-') ||
  942. (op=C_MUL,opl=9,len=1,*p=='*') ||
  943. (op=C_DIV,opl=9,len=1,*p=='/') ||
  944. (op=C_MOD,opl=9,len=1,*p=='%') ||
  945. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  946. (op=C_AND,opl=6,len=1,*p=='&') ||
  947. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  948. (op=C_OR,opl=5,len=1,*p=='|') ||
  949. (op=C_XOR,opl=4,len=1,*p=='^') ||
  950. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  951. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  952. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  953. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  954. (op=C_GT,opl=3,len=1,*p=='>') ||
  955. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  956. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  957. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  958. p+=len;
  959. if(op == C_OP3) {
  960. p=parse_subexpr(p,-1);
  961. p=skip_space(p);
  962. if( *(p++) != ':')
  963. disp_error_message("parse_subexpr: need ':'", p-1);
  964. p=parse_subexpr(p,-1);
  965. } else {
  966. p=parse_subexpr(p,opl);
  967. }
  968. add_scriptc(op);
  969. p=skip_space(p);
  970. }
  971. return p; /* return first untreated operator */
  972. }
  973. /*==========================================
  974. * 式の評価
  975. *------------------------------------------*/
  976. const char* parse_expr(const char *p)
  977. {
  978. switch(*p){
  979. case ')': case ';': case ':': case '[': case ']':
  980. case '}':
  981. disp_error_message("parse_expr: unexpected char",p);
  982. }
  983. p=parse_subexpr(p,-1);
  984. return p;
  985. }
  986. /*==========================================
  987. * 行の解析
  988. *------------------------------------------*/
  989. const char* parse_line(const char* p)
  990. {
  991. const char* p2;
  992. p=skip_space(p);
  993. if(*p==';') {
  994. // if(); for(); while(); のために閉じ判定
  995. p = parse_syntax_close(p + 1);
  996. return p;
  997. }
  998. if(*p==')' && parse_syntax_for_flag)
  999. return p+1;
  1000. p = skip_space(p);
  1001. if(p[0] == '{') {
  1002. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1003. syntax.curly[syntax.curly_count].count = -1;
  1004. syntax.curly[syntax.curly_count].index = -1;
  1005. syntax.curly_count++;
  1006. return p + 1;
  1007. } else if(p[0] == '}') {
  1008. return parse_curly_close(p);
  1009. }
  1010. // 構文関連の処理
  1011. p2 = parse_syntax(p);
  1012. if(p2 != NULL)
  1013. return p2;
  1014. p = parse_callfunc(p,0);
  1015. p = skip_space(p);
  1016. if(parse_syntax_for_flag) {
  1017. if( *p != ')' )
  1018. disp_error_message("parse_line: need ')'",p);
  1019. } else {
  1020. if( *p != ';' )
  1021. disp_error_message("parse_line: need ';'",p);
  1022. }
  1023. // if, for , while の閉じ判定
  1024. p = parse_syntax_close(p+1);
  1025. return p;
  1026. }
  1027. // { ... } の閉じ処理
  1028. const char* parse_curly_close(const char* p)
  1029. {
  1030. if(syntax.curly_count <= 0) {
  1031. disp_error_message("parse_curly_close: unexpected string",p);
  1032. return p + 1;
  1033. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1034. syntax.curly_count--;
  1035. // if, for , while の閉じ判定
  1036. p = parse_syntax_close(p + 1);
  1037. return p;
  1038. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1039. // switch() 閉じ判定
  1040. int pos = syntax.curly_count-1;
  1041. char label[256];
  1042. int l;
  1043. // 一時変数を消す
  1044. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1045. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1046. parse_line(label);
  1047. syntax.curly_count--;
  1048. // 無条件で終了ポインタに移動
  1049. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1050. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1051. parse_line(label);
  1052. syntax.curly_count--;
  1053. // 現在地のラベルを付ける
  1054. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1055. l=add_str(label);
  1056. set_label(l,script_pos, p);
  1057. if(syntax.curly[pos].flag) {
  1058. // default が存在する
  1059. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1060. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1061. parse_line(label);
  1062. syntax.curly_count--;
  1063. }
  1064. // 終了ラベルを付ける
  1065. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1066. l=add_str(label);
  1067. set_label(l,script_pos, p);
  1068. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1069. syntax.curly_count--;
  1070. return p+1;
  1071. } else {
  1072. disp_error_message("parse_curly_close: unexpected string",p);
  1073. return p + 1;
  1074. }
  1075. }
  1076. // 構文関連の処理
  1077. // break, case, continue, default, do, for, function,
  1078. // if, switch, while をこの内部で処理します。
  1079. const char* parse_syntax(const char* p)
  1080. {
  1081. const char *p2 = skip_word(p);
  1082. switch(*p) {
  1083. case 'B':
  1084. case 'b':
  1085. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1086. // break の処理
  1087. char label[256];
  1088. int pos = syntax.curly_count - 1;
  1089. while(pos >= 0) {
  1090. if(syntax.curly[pos].type == TYPE_DO) {
  1091. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1092. break;
  1093. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1094. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1095. break;
  1096. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1097. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1098. break;
  1099. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1100. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1101. break;
  1102. }
  1103. pos--;
  1104. }
  1105. if(pos < 0) {
  1106. disp_error_message("parse_syntax: unexpected 'break'",p);
  1107. } else {
  1108. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1109. parse_line(label);
  1110. syntax.curly_count--;
  1111. }
  1112. p = skip_space(p2);
  1113. if(*p != ';')
  1114. disp_error_message("parse_syntax: need ';'",p);
  1115. // if, for , while の閉じ判定
  1116. p = parse_syntax_close(p + 1);
  1117. return p;
  1118. }
  1119. break;
  1120. case 'c':
  1121. case 'C':
  1122. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1123. // case の処理
  1124. int pos = syntax.curly_count-1;
  1125. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1126. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1127. return p+1;
  1128. } else {
  1129. char label[256];
  1130. int l,v;
  1131. char *np;
  1132. if(syntax.curly[pos].count != 1) {
  1133. // FALLTHRU 用のジャンプ
  1134. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1135. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1136. parse_line(label);
  1137. syntax.curly_count--;
  1138. // 現在地のラベルを付ける
  1139. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1140. l=add_str(label);
  1141. set_label(l,script_pos, p);
  1142. }
  1143. // switch 判定文
  1144. p = skip_space(p2);
  1145. if(p == p2) {
  1146. disp_error_message("parse_syntax: expect space ' '",p);
  1147. }
  1148. // check whether case label is integer or not
  1149. v = strtol(p,&np,0);
  1150. if(np == p) { //Check for constants
  1151. p2 = skip_word(p);
  1152. v = p2-p; // length of word at p2
  1153. memcpy(label,p,v);
  1154. label[v]='\0';
  1155. v = search_str(label);
  1156. if (v < 0 || str_data[v].type != C_INT)
  1157. disp_error_message("parse_syntax: 'case' label not integer",p);
  1158. v = str_data[v].val;
  1159. p = skip_word(p);
  1160. } else { //Numeric value
  1161. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1162. p++;
  1163. p = skip_word(p);
  1164. if(np != p)
  1165. disp_error_message("parse_syntax: 'case' label not integer",np);
  1166. }
  1167. p = skip_space(p);
  1168. if(*p != ':')
  1169. disp_error_message("parse_syntax: expect ':'",p);
  1170. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1171. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1172. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1173. // 2回parse しないとダメ
  1174. p2 = parse_line(label);
  1175. parse_line(p2);
  1176. syntax.curly_count--;
  1177. if(syntax.curly[pos].count != 1) {
  1178. // FALLTHRU 終了後のラベル
  1179. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1180. l=add_str(label);
  1181. set_label(l,script_pos,p);
  1182. }
  1183. // check duplication of case label [Rayce]
  1184. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1185. disp_error_message("parse_syntax: dup 'case'",p);
  1186. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1187. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1188. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1189. parse_line(label);
  1190. syntax.curly_count--;
  1191. syntax.curly[pos].count++;
  1192. }
  1193. return p + 1;
  1194. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1195. // continue の処理
  1196. char label[256];
  1197. int pos = syntax.curly_count - 1;
  1198. while(pos >= 0) {
  1199. if(syntax.curly[pos].type == TYPE_DO) {
  1200. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1201. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1202. break;
  1203. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1204. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1205. break;
  1206. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1207. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1208. break;
  1209. }
  1210. pos--;
  1211. }
  1212. if(pos < 0) {
  1213. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1214. } else {
  1215. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1216. parse_line(label);
  1217. syntax.curly_count--;
  1218. }
  1219. p = skip_space(p2);
  1220. if(*p != ';')
  1221. disp_error_message("parse_syntax: need ';'",p);
  1222. // if, for , while の閉じ判定
  1223. p = parse_syntax_close(p + 1);
  1224. return p;
  1225. }
  1226. break;
  1227. case 'd':
  1228. case 'D':
  1229. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1230. // switch - default の処理
  1231. int pos = syntax.curly_count-1;
  1232. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1233. disp_error_message("parse_syntax: unexpected 'default'",p);
  1234. } else if(syntax.curly[pos].flag) {
  1235. disp_error_message("parse_syntax: dup 'default'",p);
  1236. } else {
  1237. char label[256];
  1238. int l;
  1239. // 現在地のラベルを付ける
  1240. p = skip_space(p2);
  1241. if(*p != ':') {
  1242. disp_error_message("parse_syntax: need ':'",p);
  1243. }
  1244. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1245. l=add_str(label);
  1246. set_label(l,script_pos,p);
  1247. // 無条件で次のリンクに飛ばす
  1248. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1249. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1250. parse_line(label);
  1251. syntax.curly_count--;
  1252. // default のラベルを付ける
  1253. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1254. l=add_str(label);
  1255. set_label(l,script_pos,p);
  1256. syntax.curly[syntax.curly_count - 1].flag = 1;
  1257. syntax.curly[pos].count++;
  1258. }
  1259. return p + 1;
  1260. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1261. int l;
  1262. char label[256];
  1263. p=skip_space(p2);
  1264. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1265. syntax.curly[syntax.curly_count].count = 1;
  1266. syntax.curly[syntax.curly_count].index = syntax.index++;
  1267. syntax.curly[syntax.curly_count].flag = 0;
  1268. // 現在地のラベル形成する
  1269. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1270. l=add_str(label);
  1271. set_label(l,script_pos,p);
  1272. syntax.curly_count++;
  1273. return p;
  1274. }
  1275. break;
  1276. case 'f':
  1277. case 'F':
  1278. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1279. int l;
  1280. char label[256];
  1281. int pos = syntax.curly_count;
  1282. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1283. syntax.curly[syntax.curly_count].count = 1;
  1284. syntax.curly[syntax.curly_count].index = syntax.index++;
  1285. syntax.curly[syntax.curly_count].flag = 0;
  1286. syntax.curly_count++;
  1287. p=skip_space(p2);
  1288. if(*p != '(')
  1289. disp_error_message("parse_syntax: need '('",p);
  1290. p++;
  1291. // 初期化文を実行する
  1292. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1293. p=parse_line(p);
  1294. syntax.curly_count--;
  1295. // 条件判断開始のラベル形成する
  1296. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1297. l=add_str(label);
  1298. set_label(l,script_pos,p);
  1299. p=skip_space(p);
  1300. if(*p == ';') {
  1301. // for(;;) のパターンなので必ず真
  1302. ;
  1303. } else {
  1304. // 条件が偽なら終了地点に飛ばす
  1305. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1306. add_scriptl(add_str("jump_zero"));
  1307. add_scriptc(C_ARG);
  1308. p=parse_expr(p);
  1309. p=skip_space(p);
  1310. add_scriptl(add_str(label));
  1311. add_scriptc(C_FUNC);
  1312. }
  1313. if(*p != ';')
  1314. disp_error_message("parse_syntax: need ';'",p);
  1315. p++;
  1316. // ループ開始に飛ばす
  1317. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1318. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1319. parse_line(label);
  1320. syntax.curly_count--;
  1321. // 次のループへのラベル形成する
  1322. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1323. l=add_str(label);
  1324. set_label(l,script_pos,p);
  1325. // 次のループに入る時の処理
  1326. // for 最後の ')' を ';' として扱うフラグ
  1327. parse_syntax_for_flag = 1;
  1328. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1329. p=parse_line(p);
  1330. syntax.curly_count--;
  1331. parse_syntax_for_flag = 0;
  1332. // 条件判定処理に飛ばす
  1333. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1334. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1335. parse_line(label);
  1336. syntax.curly_count--;
  1337. // ループ開始のラベル付け
  1338. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1339. l=add_str(label);
  1340. set_label(l,script_pos,p);
  1341. return p;
  1342. }
  1343. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1344. {// internal script function
  1345. const char *func_name;
  1346. func_name = skip_space(p2);
  1347. p = skip_word(func_name);
  1348. if( p == func_name )
  1349. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1350. p2 = skip_space(p);
  1351. if( *p2 == ';' )
  1352. {// function <name> ;
  1353. // function declaration - just register the name
  1354. int l;
  1355. l = add_word(func_name);
  1356. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1357. str_data[l].type = C_USERFUNC;
  1358. // if, for , while の閉じ判定
  1359. p = parse_syntax_close(p2 + 1);
  1360. return p; }
  1361. else if(*p2 == '{')
  1362. {// function <name> <line/block of code>
  1363. char label[256];
  1364. int l;
  1365. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1366. syntax.curly[syntax.curly_count].count = 1;
  1367. syntax.curly[syntax.curly_count].index = syntax.index++;
  1368. syntax.curly[syntax.curly_count].flag = 0;
  1369. ++syntax.curly_count;
  1370. // Jump over the function code
  1371. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1372. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1373. ++syntax.curly_count;
  1374. parse_line(label);
  1375. --syntax.curly_count;
  1376. // Set the position of the function (label)
  1377. l=add_word(func_name);
  1378. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1379. str_data[l].type = C_USERFUNC;
  1380. set_label(l, script_pos, p);
  1381. if( parse_options&SCRIPT_USE_LABEL_DB )
  1382. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1383. return skip_space(p);
  1384. }
  1385. else
  1386. {
  1387. disp_error_message("expect ';' or '{' at function syntax",p);
  1388. }
  1389. }
  1390. break;
  1391. case 'i':
  1392. case 'I':
  1393. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1394. // if() の処理
  1395. char label[256];
  1396. p=skip_space(p2);
  1397. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1398. disp_error_message("need '('",p);
  1399. }
  1400. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1401. syntax.curly[syntax.curly_count].count = 1;
  1402. syntax.curly[syntax.curly_count].index = syntax.index++;
  1403. syntax.curly[syntax.curly_count].flag = 0;
  1404. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1405. syntax.curly_count++;
  1406. add_scriptl(add_str("jump_zero"));
  1407. add_scriptc(C_ARG);
  1408. p=parse_expr(p);
  1409. p=skip_space(p);
  1410. add_scriptl(add_str(label));
  1411. add_scriptc(C_FUNC);
  1412. return p;
  1413. }
  1414. break;
  1415. case 's':
  1416. case 'S':
  1417. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1418. // switch() の処理
  1419. char label[256];
  1420. p=skip_space(p2);
  1421. if(*p != '(') {
  1422. disp_error_message("need '('",p);
  1423. }
  1424. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1425. syntax.curly[syntax.curly_count].count = 1;
  1426. syntax.curly[syntax.curly_count].index = syntax.index++;
  1427. syntax.curly[syntax.curly_count].flag = 0;
  1428. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1429. syntax.curly_count++;
  1430. add_scriptl(add_str("set"));
  1431. add_scriptc(C_ARG);
  1432. add_scriptl(add_str(label));
  1433. p=parse_expr(p);
  1434. p=skip_space(p);
  1435. if(*p != '{') {
  1436. disp_error_message("parse_syntax: need '{'",p);
  1437. }
  1438. add_scriptc(C_FUNC);
  1439. return p + 1;
  1440. }
  1441. break;
  1442. case 'w':
  1443. case 'W':
  1444. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1445. int l;
  1446. char label[256];
  1447. p=skip_space(p2);
  1448. if(*p != '(') {
  1449. disp_error_message("need '('",p);
  1450. }
  1451. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1452. syntax.curly[syntax.curly_count].count = 1;
  1453. syntax.curly[syntax.curly_count].index = syntax.index++;
  1454. syntax.curly[syntax.curly_count].flag = 0;
  1455. // 条件判断開始のラベル形成する
  1456. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1457. l=add_str(label);
  1458. set_label(l,script_pos,p);
  1459. // 条件が偽なら終了地点に飛ばす
  1460. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1461. syntax.curly_count++;
  1462. add_scriptl(add_str("jump_zero"));
  1463. add_scriptc(C_ARG);
  1464. p=parse_expr(p);
  1465. p=skip_space(p);
  1466. add_scriptl(add_str(label));
  1467. add_scriptc(C_FUNC);
  1468. return p;
  1469. }
  1470. break;
  1471. }
  1472. return NULL;
  1473. }
  1474. const char* parse_syntax_close(const char *p) {
  1475. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1476. int flag;
  1477. do {
  1478. p = parse_syntax_close_sub(p,&flag);
  1479. } while(flag);
  1480. return p;
  1481. }
  1482. // if, for , while , do の閉じ判定
  1483. // flag == 1 : 閉じられた
  1484. // flag == 0 : 閉じられない
  1485. const char* parse_syntax_close_sub(const char* p,int* flag)
  1486. {
  1487. char label[256];
  1488. int pos = syntax.curly_count - 1;
  1489. int l;
  1490. *flag = 1;
  1491. if(syntax.curly_count <= 0) {
  1492. *flag = 0;
  1493. return p;
  1494. } else if(syntax.curly[pos].type == TYPE_IF) {
  1495. const char *bp = p;
  1496. const char *p2;
  1497. // if 最終場所へ飛ばす
  1498. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1499. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1500. parse_line(label);
  1501. syntax.curly_count--;
  1502. // 現在地のラベルを付ける
  1503. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1504. l=add_str(label);
  1505. set_label(l,script_pos,p);
  1506. syntax.curly[pos].count++;
  1507. p = skip_space(p);
  1508. p2 = skip_word(p);
  1509. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1510. // else or else - if
  1511. p = skip_space(p2);
  1512. p2 = skip_word(p);
  1513. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1514. // else - if
  1515. p=skip_space(p2);
  1516. if(*p != '(') {
  1517. disp_error_message("need '('",p);
  1518. }
  1519. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1520. add_scriptl(add_str("jump_zero"));
  1521. add_scriptc(C_ARG);
  1522. p=parse_expr(p);
  1523. p=skip_space(p);
  1524. add_scriptl(add_str(label));
  1525. add_scriptc(C_FUNC);
  1526. *flag = 0;
  1527. return p;
  1528. } else {
  1529. // else
  1530. if(!syntax.curly[pos].flag) {
  1531. syntax.curly[pos].flag = 1;
  1532. *flag = 0;
  1533. return p;
  1534. }
  1535. }
  1536. }
  1537. // if 閉じ
  1538. syntax.curly_count--;
  1539. // 最終地のラベルを付ける
  1540. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1541. l=add_str(label);
  1542. set_label(l,script_pos,p);
  1543. if(syntax.curly[pos].flag == 1) {
  1544. // このifに対するelseじゃないのでポインタの位置は同じ
  1545. return bp;
  1546. }
  1547. return p;
  1548. } else if(syntax.curly[pos].type == TYPE_DO) {
  1549. int l;
  1550. char label[256];
  1551. const char *p2;
  1552. if(syntax.curly[pos].flag) {
  1553. // 現在地のラベル形成する(continue でここに来る)
  1554. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1555. l=add_str(label);
  1556. set_label(l,script_pos,p);
  1557. }
  1558. // 条件が偽なら終了地点に飛ばす
  1559. p = skip_space(p);
  1560. p2 = skip_word(p);
  1561. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1562. disp_error_message("parse_syntax: need 'while'",p);
  1563. p = skip_space(p2);
  1564. if(*p != '(') {
  1565. disp_error_message("need '('",p);
  1566. }
  1567. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1568. add_scriptl(add_str("jump_zero"));
  1569. add_scriptc(C_ARG);
  1570. p=parse_expr(p);
  1571. p=skip_space(p);
  1572. add_scriptl(add_str(label));
  1573. add_scriptc(C_FUNC);
  1574. // 開始地点に飛ばす
  1575. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1576. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1577. parse_line(label);
  1578. syntax.curly_count--;
  1579. // 条件終了地点のラベル形成する
  1580. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1581. l=add_str(label);
  1582. set_label(l,script_pos,p);
  1583. p = skip_space(p);
  1584. if(*p != ';') {
  1585. disp_error_message("parse_syntax: need ';'",p);
  1586. return p+1;
  1587. }
  1588. p++;
  1589. syntax.curly_count--;
  1590. return p;
  1591. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1592. // 次のループに飛ばす
  1593. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1594. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1595. parse_line(label);
  1596. syntax.curly_count--;
  1597. // for 終了のラベル付け
  1598. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1599. l=add_str(label);
  1600. set_label(l,script_pos,p);
  1601. syntax.curly_count--;
  1602. return p;
  1603. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1604. // while 条件判断へ飛ばす
  1605. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1606. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1607. parse_line(label);
  1608. syntax.curly_count--;
  1609. // while 終了のラベル付け
  1610. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1611. l=add_str(label);
  1612. set_label(l,script_pos,p);
  1613. syntax.curly_count--;
  1614. return p;
  1615. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1616. int pos = syntax.curly_count-1;
  1617. char label[256];
  1618. int l;
  1619. // 戻す
  1620. sprintf(label,"return;");
  1621. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1622. parse_line(label);
  1623. syntax.curly_count--;
  1624. // 現在地のラベルを付ける
  1625. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1626. l=add_str(label);
  1627. set_label(l,script_pos,p);
  1628. syntax.curly_count--;
  1629. return p;
  1630. } else {
  1631. *flag = 0;
  1632. return p;
  1633. }
  1634. }
  1635. /*==========================================
  1636. * 組み込み関数の追加
  1637. *------------------------------------------*/
  1638. static void add_buildin_func(void)
  1639. {
  1640. int i,n;
  1641. const char* p;
  1642. for( i = 0; buildin_func[i].func; i++ )
  1643. {
  1644. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1645. // 'v' - value (either string or int or reference)
  1646. // 's' - string
  1647. // 'i' - int
  1648. // 'r' - reference (of a variable)
  1649. // 'l' - label
  1650. // '?' - one optional parameter
  1651. // '*' - unknown number of optional parameters
  1652. p = buildin_func[i].arg;
  1653. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1654. while( *p == '?' ) ++p;
  1655. if( *p == '*' ) ++p;
  1656. if( *p != 0){
  1657. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1658. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1659. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1660. } else {
  1661. n = add_str(buildin_func[i].name);
  1662. str_data[n].type = C_FUNC;
  1663. str_data[n].val = i;
  1664. str_data[n].func = buildin_func[i].func;
  1665. }
  1666. }
  1667. }
  1668. /*==========================================
  1669. * 定数データベースの読み込み
  1670. *------------------------------------------*/
  1671. static void read_constdb(void)
  1672. {
  1673. FILE *fp;
  1674. char line[1024],name[1024],val[1024];
  1675. int n,type;
  1676. sprintf(line, "%s/const.txt", db_path);
  1677. fp=fopen(line, "r");
  1678. if(fp==NULL){
  1679. ShowError("can't read %s\n", line);
  1680. return ;
  1681. }
  1682. while(fgets(line, sizeof(line), fp))
  1683. {
  1684. if(line[0]=='/' && line[1]=='/')
  1685. continue;
  1686. type=0;
  1687. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1688. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1689. n=add_str(name);
  1690. if(type==0)
  1691. str_data[n].type=C_INT;
  1692. else
  1693. str_data[n].type=C_PARAM;
  1694. str_data[n].val= (int)strtol(val,NULL,0);
  1695. }
  1696. }
  1697. fclose(fp);
  1698. }
  1699. /*==========================================
  1700. * エラー表示
  1701. *------------------------------------------*/
  1702. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1703. {
  1704. int i;
  1705. if( p == NULL || !p[0] ) return NULL;
  1706. if( line < 0 )
  1707. StringBuf_Printf(buf, "*% 5d : ", -line);
  1708. else
  1709. StringBuf_Printf(buf, " % 5d : ", line);
  1710. for(i=0;p[i] && p[i] != '\n';i++){
  1711. if(p + i != mark)
  1712. StringBuf_Printf(buf, "%c", p[i]);
  1713. else
  1714. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1715. }
  1716. StringBuf_AppendStr(buf, "\n");
  1717. return p+i+(p[i] == '\n' ? 1 : 0);
  1718. }
  1719. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1720. {
  1721. // エラーが発生した行を求める
  1722. int j;
  1723. int line = start_line;
  1724. const char *p;
  1725. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1726. StringBuf buf;
  1727. for(p=src;p && *p;line++){
  1728. const char *lineend=strchr(p,'\n');
  1729. if(lineend==NULL || error_pos<lineend){
  1730. break;
  1731. }
  1732. for( j = 0; j < 4; j++ ) {
  1733. linestart[j] = linestart[j+1];
  1734. }
  1735. linestart[4] = p;
  1736. p=lineend+1;
  1737. }
  1738. StringBuf_Init(&buf);
  1739. StringBuf_AppendStr(&buf, "\a\n");
  1740. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1741. StringBuf_Printf(&buf, " %s\n", error_msg);
  1742. for(j = 0; j < 5; j++ ) {
  1743. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1744. }
  1745. p = script_print_line(&buf, p, error_pos, -line);
  1746. for(j = 0; j < 5; j++) {
  1747. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1748. }
  1749. ShowError("%s", StringBuf_Value(&buf));
  1750. StringBuf_Destroy(&buf);
  1751. }
  1752. /*==========================================
  1753. * スクリプトの解析
  1754. *------------------------------------------*/
  1755. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1756. {
  1757. const char *p,*tmpp;
  1758. int i;
  1759. struct script_code* code = NULL;
  1760. static int first=1;
  1761. char end;
  1762. if( src == NULL )
  1763. return NULL;// empty script
  1764. memset(&syntax,0,sizeof(syntax));
  1765. if(first){
  1766. add_buildin_func();
  1767. read_constdb();
  1768. first=0;
  1769. }
  1770. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1771. script_pos=0;
  1772. script_size=SCRIPT_BLOCK_SIZE;
  1773. str_data[LABEL_NEXTLINE].type=C_NOP;
  1774. str_data[LABEL_NEXTLINE].backpatch=-1;
  1775. str_data[LABEL_NEXTLINE].label=-1;
  1776. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1777. if( options&SCRIPT_USE_LABEL_DB )
  1778. scriptlabel_db->clear(scriptlabel_db, NULL);
  1779. parse_options = options;
  1780. if( setjmp( error_jump ) != 0 ) {
  1781. //Restore program state when script has problems. [from jA]
  1782. int i;
  1783. const int size = ARRAYLENGTH(syntax.curly);
  1784. if( error_report )
  1785. script_error(src,file,line,error_msg,error_pos);
  1786. aFree( error_msg );
  1787. aFree( script_buf );
  1788. script_pos = 0;
  1789. script_size = 0;
  1790. script_buf = NULL;
  1791. for(i=LABEL_START;i<str_num;i++)
  1792. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1793. for(i=0; i<size; i++)
  1794. linkdb_final(&syntax.curly[i].case_label);
  1795. return NULL;
  1796. }
  1797. parse_syntax_for_flag=0;
  1798. p=src;
  1799. p=skip_space(p);
  1800. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1801. {// does not require brackets around the script
  1802. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1803. {// empty script and can return NULL
  1804. aFree( script_buf );
  1805. script_pos = 0;
  1806. script_size = 0;
  1807. script_buf = NULL;
  1808. return NULL;
  1809. }
  1810. end = '\0';
  1811. }
  1812. else
  1813. {// requires brackets around the script
  1814. if( *p != '{' )
  1815. disp_error_message("not found '{'",p);
  1816. p = skip_space(p+1);
  1817. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1818. {// empty script and can return NULL
  1819. aFree( script_buf );
  1820. script_pos = 0;
  1821. script_size = 0;
  1822. script_buf = NULL;
  1823. return NULL;
  1824. }
  1825. end = '}';
  1826. }
  1827. // clear references of labels, variables and internal functions
  1828. for(i=LABEL_START;i<str_num;i++){
  1829. if(
  1830. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1831. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1832. ){
  1833. str_data[i].type=C_NOP;
  1834. str_data[i].backpatch=-1;
  1835. str_data[i].label=-1;
  1836. }
  1837. }
  1838. while( syntax.curly_count != 0 || *p != end )
  1839. {
  1840. if( *p == '\0' )
  1841. disp_error_message("unexpected end of script",p);
  1842. // labelだけ特殊処理
  1843. tmpp=skip_space(skip_word(p));
  1844. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1845. i=add_word(p);
  1846. set_label(i,script_pos,p);
  1847. if( parse_options&SCRIPT_USE_LABEL_DB )
  1848. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1849. p=tmpp+1;
  1850. p=skip_space(p);
  1851. continue;
  1852. }
  1853. // 他は全部一緒くた
  1854. p=parse_line(p);
  1855. p=skip_space(p);
  1856. add_scriptc(C_EOL);
  1857. set_label(LABEL_NEXTLINE,script_pos,p);
  1858. str_data[LABEL_NEXTLINE].type=C_NOP;
  1859. str_data[LABEL_NEXTLINE].backpatch=-1;
  1860. str_data[LABEL_NEXTLINE].label=-1;
  1861. }
  1862. add_scriptc(C_NOP);
  1863. // trim code to size
  1864. script_size = script_pos;
  1865. RECREATE(script_buf,unsigned char,script_pos);
  1866. // default unknown references to variables
  1867. for(i=LABEL_START;i<str_num;i++){
  1868. if(str_data[i].type==C_NOP){
  1869. int j,next;
  1870. str_data[i].type=C_NAME;
  1871. str_data[i].label=i;
  1872. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1873. next=GETVALUE(script_buf,j);
  1874. SETVALUE(script_buf,j,i);
  1875. j=next;
  1876. }
  1877. }
  1878. }
  1879. #ifdef DEBUG_DISP
  1880. for(i=0;i<script_pos;i++){
  1881. if((i&15)==0) printf("%04x : ",i);
  1882. ShowMessage("%02x ",script_buf[i]);
  1883. if((i&15)==15) printf("\n");
  1884. }
  1885. ShowMessage("\n");
  1886. #endif
  1887. #ifdef DEBUG_DISASM
  1888. {
  1889. int i = 0,j;
  1890. while(i < script_pos) {
  1891. ShowMessage("%06x ",i);
  1892. j = i;
  1893. switch(get_com(script_buf,&i)) {
  1894. case C_EOL: printf("C_EOL"); break;
  1895. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1896. case C_POS:
  1897. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1898. i += 3;
  1899. break;
  1900. case C_NAME:
  1901. j = (*(int*)(script_buf+i)&0xffffff);
  1902. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : get_str(j));
  1903. i += 3;
  1904. break;
  1905. case C_ARG: ShowMessage("C_ARG"); break;
  1906. case C_FUNC: ShowMessage("C_FUNC"); break;
  1907. case C_ADD: ShowMessage("C_ADD"); break;
  1908. case C_SUB: ShowMessage("C_SUB"); break;
  1909. case C_MUL: ShowMessage("C_MUL"); break;
  1910. case C_DIV: ShowMessage("C_DIV"); break;
  1911. case C_MOD: ShowMessage("C_MOD"); break;
  1912. case C_EQ: ShowMessage("C_EQ"); break;
  1913. case C_NE: ShowMessage("C_NE"); break;
  1914. case C_GT: ShowMessage("C_GT"); break;
  1915. case C_GE: ShowMessage("C_GE"); break;
  1916. case C_LT: ShowMessage("C_LT"); break;
  1917. case C_LE: ShowMessage("C_LE"); break;
  1918. case C_AND: ShowMessage("C_AND"); break;
  1919. case C_OR: ShowMessage("C_OR"); break;
  1920. case C_XOR: ShowMessage("C_XOR"); break;
  1921. case C_LAND: ShowMessage("C_LAND"); break;
  1922. case C_LOR: ShowMessage("C_LOR"); break;
  1923. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1924. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1925. case C_NEG: ShowMessage("C_NEG"); break;
  1926. case C_NOT: ShowMessage("C_NOT"); break;
  1927. case C_LNOT: ShowMessage("C_LNOT"); break;
  1928. case C_NOP: ShowMessage("C_NOP"); break;
  1929. case C_OP3: ShowMessage("C_OP3"); break;
  1930. case C_STR:
  1931. j = strlen(script_buf + i);
  1932. ShowMessage("C_STR %s",script_buf + i);
  1933. i+= j+1;
  1934. break;
  1935. default:
  1936. ShowMessage("unknown");
  1937. }
  1938. ShowMessage(CL_CLL "\n");
  1939. }
  1940. }
  1941. #endif
  1942. CREATE(code,struct script_code,1);
  1943. code->script_buf = script_buf;
  1944. code->script_size = script_size;
  1945. code->script_vars = NULL;
  1946. return code;
  1947. }
  1948. /// Returns the player attached to this script, identified by the rid.
  1949. /// If there is no player attached, the script is terminated.
  1950. TBL_PC *script_rid2sd(struct script_state *st)
  1951. {
  1952. TBL_PC *sd=map_id2sd(st->rid);
  1953. if(!sd){
  1954. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1955. script_reportsrc(st);
  1956. st->state = END;
  1957. }
  1958. return sd;
  1959. }
  1960. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1961. ///
  1962. /// @param st Script state
  1963. /// @param data Variable/constant
  1964. void get_val(struct script_state* st, struct script_data* data)
  1965. {
  1966. const char* name;
  1967. char prefix;
  1968. char postfix;
  1969. TBL_PC* sd = NULL;
  1970. if( !data_isreference(data) )
  1971. return;// not a variable/constant
  1972. name = reference_getname(data);
  1973. prefix = name[0];
  1974. postfix = name[strlen(name) - 1];
  1975. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1976. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1977. {
  1978. sd = script_rid2sd(st);
  1979. if( sd == NULL )
  1980. {// needs player attached
  1981. if( postfix == '$' )
  1982. {// string variable
  1983. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1984. data->type = C_CONSTSTR;
  1985. data->u.str = "";
  1986. }
  1987. else
  1988. {// integer variable
  1989. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1990. data->type = C_INT;
  1991. data->u.num = 0;
  1992. }
  1993. return;
  1994. }
  1995. }
  1996. if( postfix == '$' )
  1997. {// string variable
  1998. switch( prefix )
  1999. {
  2000. case '@':
  2001. data->u.str = pc_readregstr(sd, data->u.num);
  2002. break;
  2003. case '$':
  2004. data->u.str = mapreg_readregstr(data->u.num);
  2005. break;
  2006. case '#':
  2007. if( name[1] == '#' )
  2008. data->u.str = pc_readaccountreg2str(sd, name);// global
  2009. else
  2010. data->u.str = pc_readaccountregstr(sd, name);// local
  2011. break;
  2012. case '.':
  2013. {
  2014. struct linkdb_node** n =
  2015. data->ref ? data->ref:
  2016. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2017. &st->script->script_vars;// npc variable
  2018. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2019. }
  2020. break;
  2021. case '\'':
  2022. {
  2023. struct linkdb_node** n = NULL;
  2024. if( st->instance_id )
  2025. n = &instance[st->instance_id].svar;
  2026. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2027. }
  2028. break;
  2029. default:
  2030. data->u.str = pc_readglobalreg_str(sd, name);
  2031. break;
  2032. }
  2033. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2034. {// empty string
  2035. data->type = C_CONSTSTR;
  2036. data->u.str = "";
  2037. }
  2038. else
  2039. {// duplicate string
  2040. data->type = C_STR;
  2041. data->u.str = aStrdup(data->u.str);
  2042. }
  2043. }
  2044. else
  2045. {// integer variable
  2046. data->type = C_INT;
  2047. if( reference_toconstant(data) )
  2048. {
  2049. data->u.num = reference_getconstant(data);
  2050. }
  2051. else if( reference_toparam(data) )
  2052. {
  2053. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2054. }
  2055. else
  2056. switch( prefix )
  2057. {
  2058. case '@':
  2059. data->u.num = pc_readreg(sd, data->u.num);
  2060. break;
  2061. case '$':
  2062. data->u.num = mapreg_readreg(data->u.num);
  2063. break;
  2064. case '#':
  2065. if( name[1] == '#' )
  2066. data->u.num = pc_readaccountreg2(sd, name);// global
  2067. else
  2068. data->u.num = pc_readaccountreg(sd, name);// local
  2069. break;
  2070. case '.':
  2071. {
  2072. struct linkdb_node** n =
  2073. data->ref ? data->ref:
  2074. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2075. &st->script->script_vars;// npc variable
  2076. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2077. }
  2078. break;
  2079. case '\'':
  2080. {
  2081. struct linkdb_node** n = NULL;
  2082. if( st->instance_id )
  2083. n = &instance[st->instance_id].ivar;
  2084. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2085. }
  2086. break;
  2087. default:
  2088. data->u.num = pc_readglobalreg(sd, name);
  2089. break;
  2090. }
  2091. }
  2092. return;
  2093. }
  2094. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2095. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2096. /// The value is left in the top of the stack and needs to be removed manually.
  2097. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2098. {
  2099. struct script_data* data;
  2100. push_val2(st->stack, C_NAME, uid, ref);
  2101. data = script_getdatatop(st, -1);
  2102. get_val(st, data);
  2103. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2104. }
  2105. /*==========================================
  2106. * Stores the value of a script variable
  2107. * Return value is 0 on fail, 1 on success.
  2108. *------------------------------------------*/
  2109. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2110. {
  2111. char prefix = name[0];
  2112. if( is_string_variable(name) )
  2113. {// string variable
  2114. const char* str = (const char*)value;
  2115. switch (prefix) {
  2116. case '@':
  2117. return pc_setregstr(sd, num, str);
  2118. case '$':
  2119. return mapreg_setregstr(num, str);
  2120. case '#':
  2121. return (name[1] == '#') ?
  2122. pc_setaccountreg2str(sd, name, str) :
  2123. pc_setaccountregstr(sd, name, str);
  2124. case '.': {
  2125. char* p;
  2126. struct linkdb_node** n;
  2127. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2128. p = (char*)linkdb_erase(n, (void*)num);
  2129. if (p) aFree(p);
  2130. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2131. }
  2132. return 1;
  2133. case '\'': {
  2134. char *p;
  2135. struct linkdb_node** n = NULL;
  2136. if( st->instance_id )
  2137. n = &instance[st->instance_id].svar;
  2138. p = (char*)linkdb_erase(n, (void*)num);
  2139. if (p) aFree(p);
  2140. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2141. }
  2142. return 1;
  2143. default:
  2144. return pc_setglobalreg_str(sd, name, str);
  2145. }
  2146. }
  2147. else
  2148. {// integer variable
  2149. int val = (int)value;
  2150. if(str_data[num&0x00ffffff].type == C_PARAM)
  2151. {
  2152. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2153. {
  2154. if( st != NULL )
  2155. {
  2156. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2157. script_reportsrc(st);
  2158. st->state = END;
  2159. }
  2160. return 0;
  2161. }
  2162. return 1;
  2163. }
  2164. switch (prefix) {
  2165. case '@':
  2166. return pc_setreg(sd, num, val);
  2167. case '$':
  2168. return mapreg_setreg(num, val);
  2169. case '#':
  2170. return (name[1] == '#') ?
  2171. pc_setaccountreg2(sd, name, val) :
  2172. pc_setaccountreg(sd, name, val);
  2173. case '.': {
  2174. struct linkdb_node** n;
  2175. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2176. if (val == 0)
  2177. linkdb_erase(n, (void*)num);
  2178. else
  2179. linkdb_replace(n, (void*)num, (void*)val);
  2180. }
  2181. return 1;
  2182. case '\'':
  2183. {
  2184. struct linkdb_node** n = NULL;
  2185. if( st->instance_id )
  2186. n = &instance[st->instance_id].ivar;
  2187. if( val == 0 )
  2188. linkdb_erase(n, (void*)num);
  2189. else
  2190. linkdb_replace(n, (void*)num, (void*)val);
  2191. return 1;
  2192. }
  2193. default:
  2194. return pc_setglobalreg(sd, name, val);
  2195. }
  2196. }
  2197. }
  2198. int set_var(TBL_PC* sd, char* name, void* val)
  2199. {
  2200. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2201. }
  2202. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2203. {
  2204. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2205. }
  2206. /// Converts the data to a string
  2207. const char* conv_str(struct script_state* st, struct script_data* data)
  2208. {
  2209. char* p;
  2210. get_val(st, data);
  2211. if( data_isstring(data) )
  2212. {// nothing to convert
  2213. }
  2214. else if( data_isint(data) )
  2215. {// int -> string
  2216. CREATE(p, char, ITEM_NAME_LENGTH);
  2217. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2218. p[ITEM_NAME_LENGTH-1] = '\0';
  2219. data->type = C_STR;
  2220. data->u.str = p;
  2221. }
  2222. else if( data_isreference(data) )
  2223. {// reference -> string
  2224. //##TODO when does this happen (check get_val) [FlavioJS]
  2225. data->type = C_CONSTSTR;
  2226. data->u.str = reference_getname(data);
  2227. }
  2228. else
  2229. {// unsupported data type
  2230. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2231. script_reportdata(data);
  2232. script_reportsrc(st);
  2233. data->type = C_CONSTSTR;
  2234. data->u.str = "";
  2235. }
  2236. return data->u.str;
  2237. }
  2238. /// Converts the data to an int
  2239. int conv_num(struct script_state* st, struct script_data* data)
  2240. {
  2241. char* p;
  2242. long num;
  2243. get_val(st, data);
  2244. if( data_isint(data) )
  2245. {// nothing to convert
  2246. }
  2247. else if( data_isstring(data) )
  2248. {// string -> int
  2249. // the result does not overflow or underflow, it is capped instead
  2250. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2251. p = data->u.str;
  2252. errno = 0;
  2253. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2254. if( errno == ERANGE
  2255. #if LONG_MAX > INT_MAX
  2256. || num < INT_MIN || num > INT_MAX
  2257. #endif
  2258. )
  2259. {
  2260. if( num <= INT_MIN )
  2261. {
  2262. num = INT_MIN;
  2263. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2264. }
  2265. else//if( num >= INT_MAX )
  2266. {
  2267. num = INT_MAX;
  2268. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2269. }
  2270. script_reportdata(data);
  2271. script_reportsrc(st);
  2272. }
  2273. if( data->type == C_STR )
  2274. aFree(p);
  2275. data->type = C_INT;
  2276. data->u.num = (int)num;
  2277. }
  2278. #if 0
  2279. // FIXME this function is being used to retrieve the position of labels and
  2280. // probably other stuff [FlavioJS]
  2281. else
  2282. {// unsupported data type
  2283. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2284. script_reportdata(data);
  2285. script_reportsrc(st);
  2286. data->type = C_INT;
  2287. data->u.num = 0;
  2288. }
  2289. #endif
  2290. return data->u.num;
  2291. }
  2292. //
  2293. // Stack operations
  2294. //
  2295. /// Increases the size of the stack
  2296. void stack_expand(struct script_stack* stack)
  2297. {
  2298. stack->sp_max += 64;
  2299. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2300. stack->sp_max * sizeof(stack->stack_data[0]) );
  2301. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2302. 64 * sizeof(stack->stack_data[0]) );
  2303. }
  2304. /// Pushes a value into the stack
  2305. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2306. /// Pushes a value into the stack (with reference)
  2307. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2308. {
  2309. if( stack->sp >= stack->sp_max )
  2310. stack_expand(stack);
  2311. stack->stack_data[stack->sp].type = type;
  2312. stack->stack_data[stack->sp].u.num = val;
  2313. stack->stack_data[stack->sp].ref = ref;
  2314. stack->sp++;
  2315. return &stack->stack_data[stack->sp-1];
  2316. }
  2317. /// Pushes a string into the stack
  2318. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2319. {
  2320. if( stack->sp >= stack->sp_max )
  2321. stack_expand(stack);
  2322. stack->stack_data[stack->sp].type = type;
  2323. stack->stack_data[stack->sp].u.str = str;
  2324. stack->stack_data[stack->sp].ref = NULL;
  2325. stack->sp++;
  2326. return &stack->stack_data[stack->sp-1];
  2327. }
  2328. /// Pushes a retinfo into the stack
  2329. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2330. {
  2331. if( stack->sp >= stack->sp_max )
  2332. stack_expand(stack);
  2333. stack->stack_data[stack->sp].type = C_RETINFO;
  2334. stack->stack_data[stack->sp].u.ri = ri;
  2335. stack->stack_data[stack->sp].ref = NULL;
  2336. stack->sp++;
  2337. return &stack->stack_data[stack->sp-1];
  2338. }
  2339. /// Pushes a copy of the target position into the stack
  2340. struct script_data* push_copy(struct script_stack* stack, int pos)
  2341. {
  2342. switch( stack->stack_data[pos].type )
  2343. {
  2344. case C_CONSTSTR:
  2345. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2346. break;
  2347. case C_STR:
  2348. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2349. break;
  2350. case C_RETINFO:
  2351. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2352. exit(1);
  2353. break;
  2354. default:
  2355. return push_val2(
  2356. stack,stack->stack_data[pos].type,
  2357. stack->stack_data[pos].u.num,
  2358. stack->stack_data[pos].ref
  2359. );
  2360. break;
  2361. }
  2362. }
  2363. /// Removes the values in indexes [start,end[ from the stack.
  2364. /// Adjusts all stack pointers.
  2365. void pop_stack(struct script_state* st, int start, int end)
  2366. {
  2367. struct script_stack* stack = st->stack;
  2368. struct script_data* data;
  2369. int i;
  2370. if( start < 0 )
  2371. start = 0;
  2372. if( end > stack->sp )
  2373. end = stack->sp;
  2374. if( start >= end )
  2375. return;// nothing to pop
  2376. // free stack elements
  2377. for( i = start; i < end; i++ )
  2378. {
  2379. data = &stack->stack_data[i];
  2380. if( data->type == C_STR )
  2381. aFree(data->u.str);
  2382. if( data->type == C_RETINFO )
  2383. {
  2384. struct script_retinfo* ri = data->u.ri;
  2385. if( ri->var_function )
  2386. {
  2387. script_free_vars(ri->var_function);
  2388. aFree(ri->var_function);
  2389. }
  2390. aFree(ri);
  2391. }
  2392. data->type = C_NOP;
  2393. }
  2394. // move the rest of the elements
  2395. if( stack->sp > end )
  2396. {
  2397. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2398. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2399. stack->stack_data[i].type = C_NOP;
  2400. }
  2401. // adjust stack pointers
  2402. if( st->start > end ) st->start -= end - start;
  2403. else if( st->start > start ) st->start = start;
  2404. if( st->end > end ) st->end -= end - start;
  2405. else if( st->end > start ) st->end = start;
  2406. if( stack->defsp > end ) stack->defsp -= end - start;
  2407. else if( stack->defsp > start ) stack->defsp = start;
  2408. stack->sp -= end - start;
  2409. }
  2410. ///
  2411. ///
  2412. ///
  2413. /*==========================================
  2414. * スクリプト依存変数、関数依存変数の解放
  2415. *------------------------------------------*/
  2416. void script_free_vars(struct linkdb_node **node)
  2417. {
  2418. struct linkdb_node* n = *node;
  2419. while( n != NULL)
  2420. {
  2421. const char* name = get_str((int)(n->key)&0x00ffffff);
  2422. if( is_string_variable(name) )
  2423. aFree(n->data); // 文字型変数なので、データ削除
  2424. n = n->next;
  2425. }
  2426. linkdb_final( node );
  2427. }
  2428. void script_free_code(struct script_code* code)
  2429. {
  2430. script_free_vars( &code->script_vars );
  2431. aFree( code->script_buf );
  2432. aFree( code );
  2433. }
  2434. /// Creates a new script state.
  2435. ///
  2436. /// @param script Script code
  2437. /// @param pos Position in the code
  2438. /// @param rid Who is running the script (attached player)
  2439. /// @param oid Where the code is being run (npc 'object')
  2440. /// @return Script state
  2441. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2442. {
  2443. struct script_state* st;
  2444. CREATE(st, struct script_state, 1);
  2445. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2446. st->stack->sp = 0;
  2447. st->stack->sp_max = 64;
  2448. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2449. st->stack->defsp = st->stack->sp;
  2450. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2451. st->state = RUN;
  2452. st->script = script;
  2453. //st->scriptroot = script;
  2454. st->pos = pos;
  2455. st->rid = rid;
  2456. st->oid = oid;
  2457. st->sleep.timer = INVALID_TIMER;
  2458. return st;
  2459. }
  2460. /// Frees a script state.
  2461. ///
  2462. /// @param st Script state
  2463. void script_free_state(struct script_state* st)
  2464. {
  2465. if( st->sleep.timer != INVALID_TIMER )
  2466. delete_timer(st->sleep.timer, run_script_timer);
  2467. script_free_vars(st->stack->var_function);
  2468. aFree(st->stack->var_function);
  2469. pop_stack(st, 0, st->stack->sp);
  2470. aFree(st->stack->stack_data);
  2471. aFree(st->stack);
  2472. st->pos = -1;
  2473. aFree(st);
  2474. }
  2475. //
  2476. // 実行部main
  2477. //
  2478. /*==========================================
  2479. * コマンドの読み取り
  2480. *------------------------------------------*/
  2481. c_op get_com(unsigned char *script,int *pos)
  2482. {
  2483. int i = 0, j = 0;
  2484. if(script[*pos]>=0x80){
  2485. return C_INT;
  2486. }
  2487. while(script[*pos]>=0x40){
  2488. i=script[(*pos)++]<<j;
  2489. j+=6;
  2490. }
  2491. return (c_op)(i+(script[(*pos)++]<<j));
  2492. }
  2493. /*==========================================
  2494. * 数値の所得
  2495. *------------------------------------------*/
  2496. int get_num(unsigned char *script,int *pos)
  2497. {
  2498. int i,j;
  2499. i=0; j=0;
  2500. while(script[*pos]>=0xc0){
  2501. i+=(script[(*pos)++]&0x7f)<<j;
  2502. j+=6;
  2503. }
  2504. return i+((script[(*pos)++]&0x7f)<<j);
  2505. }
  2506. /*==========================================
  2507. * スタックから値を取り出す
  2508. *------------------------------------------*/
  2509. int pop_val(struct script_state* st)
  2510. {
  2511. if(st->stack->sp<=0)
  2512. return 0;
  2513. st->stack->sp--;
  2514. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2515. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2516. return st->stack->stack_data[st->stack->sp].u.num;
  2517. return 0;
  2518. }
  2519. /// Ternary operators
  2520. /// test ? if_true : if_false
  2521. void op_3(struct script_state* st, int op)
  2522. {
  2523. struct script_data* data;
  2524. int flag = 0;
  2525. data = script_getdatatop(st, -3);
  2526. get_val(st, data);
  2527. if( data_isstring(data) )
  2528. flag = data->u.str[0];// "" -> false
  2529. else if( data_isint(data) )
  2530. flag = data->u.num;// 0 -> false
  2531. else
  2532. {
  2533. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2534. script_reportdata(data);
  2535. script_reportsrc(st);
  2536. script_removetop(st, -3, 0);
  2537. script_pushnil(st);
  2538. return;
  2539. }
  2540. if( flag )
  2541. script_pushcopytop(st, -2);
  2542. else
  2543. script_pushcopytop(st, -1);
  2544. script_removetop(st, -4, -1);
  2545. }
  2546. /// Binary string operators
  2547. /// s1 EQ s2 -> i
  2548. /// s1 NE s2 -> i
  2549. /// s1 GT s2 -> i
  2550. /// s1 GE s2 -> i
  2551. /// s1 LT s2 -> i
  2552. /// s1 LE s2 -> i
  2553. /// s1 ADD s2 -> s
  2554. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2555. {
  2556. int a = 0;
  2557. switch(op){
  2558. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2559. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2560. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2561. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2562. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2563. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2564. case C_ADD:
  2565. {
  2566. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2567. strcpy(buf, s1);
  2568. strcat(buf, s2);
  2569. script_pushstr(st, buf);
  2570. return;
  2571. }
  2572. default:
  2573. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2574. script_reportsrc(st);
  2575. script_pushnil(st);
  2576. st->state = END;
  2577. return;
  2578. }
  2579. script_pushint(st,a);
  2580. }
  2581. /// Binary number operators
  2582. /// i OP i -> i
  2583. void op_2num(struct script_state* st, int op, int i1, int i2)
  2584. {
  2585. int ret;
  2586. double ret_double;
  2587. switch( op )
  2588. {
  2589. case C_AND: ret = i1 & i2; break;
  2590. case C_OR: ret = i1 | i2; break;
  2591. case C_XOR: ret = i1 ^ i2; break;
  2592. case C_LAND: ret = (i1 && i2); break;
  2593. case C_LOR: ret = (i1 || i2); break;
  2594. case C_EQ: ret = (i1 == i2); break;
  2595. case C_NE: ret = (i1 != i2); break;
  2596. case C_GT: ret = (i1 > i2); break;
  2597. case C_GE: ret = (i1 >= i2); break;
  2598. case C_LT: ret = (i1 < i2); break;
  2599. case C_LE: ret = (i1 <= i2); break;
  2600. case C_R_SHIFT: ret = i1>>i2; break;
  2601. case C_L_SHIFT: ret = i1<<i2; break;
  2602. case C_DIV:
  2603. case C_MOD:
  2604. if( i2 == 0 )
  2605. {
  2606. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2607. script_reportsrc(st);
  2608. script_pushnil(st);
  2609. st->state = END;
  2610. return;
  2611. }
  2612. else if( op == C_DIV )
  2613. ret = i1 / i2;
  2614. else//if( op == C_MOD )
  2615. ret = i1 % i2;
  2616. break;
  2617. default:
  2618. switch( op )
  2619. {// operators that can overflow/underflow
  2620. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2621. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2622. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2623. default:
  2624. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2625. script_reportsrc(st);
  2626. script_pushnil(st);
  2627. return;
  2628. }
  2629. if( ret_double < (double)INT_MIN )
  2630. {
  2631. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2632. script_reportsrc(st);
  2633. ret = INT_MIN;
  2634. }
  2635. else if( ret_double > (double)INT_MAX )
  2636. {
  2637. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2638. script_reportsrc(st);
  2639. ret = INT_MAX;
  2640. }
  2641. }
  2642. script_pushint(st, ret);
  2643. }
  2644. /// Binary operators
  2645. void op_2(struct script_state *st, int op)
  2646. {
  2647. struct script_data* left;
  2648. struct script_data* right;
  2649. left = script_getdatatop(st, -2);
  2650. right = script_getdatatop(st, -1);
  2651. get_val(st, left);
  2652. get_val(st, right);
  2653. // automatic conversions
  2654. switch( op )
  2655. {
  2656. case C_ADD:
  2657. if( data_isint(left) && data_isstring(right) )
  2658. {// convert int-string to string-string
  2659. conv_str(st, left);
  2660. }
  2661. else if( data_isstring(left) && data_isint(right) )
  2662. {// convert string-int to string-string
  2663. conv_str(st, right);
  2664. }
  2665. break;
  2666. }
  2667. if( data_isstring(left) && data_isstring(right) )
  2668. {// ss => op_2str
  2669. op_2str(st, op, left->u.str, right->u.str);
  2670. script_removetop(st, -3, -1);// pop the two values before the top one
  2671. }
  2672. else if( data_isint(left) && data_isint(right) )
  2673. {// ii => op_2num
  2674. int i1 = left->u.num;
  2675. int i2 = right->u.num;
  2676. script_removetop(st, -2, 0);
  2677. op_2num(st, op, i1, i2);
  2678. }
  2679. else
  2680. {// invalid argument
  2681. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2682. script_reportdata(left);
  2683. script_reportdata(right);
  2684. script_reportsrc(st);
  2685. script_removetop(st, -2, 0);
  2686. script_pushnil(st);
  2687. st->state = END;
  2688. }
  2689. }
  2690. /// Unary operators
  2691. /// NEG i -> i
  2692. /// NOT i -> i
  2693. /// LNOT i -> i
  2694. void op_1(struct script_state* st, int op)
  2695. {
  2696. struct script_data* data;
  2697. int i1;
  2698. data = script_getdatatop(st, -1);
  2699. get_val(st, data);
  2700. if( !data_isint(data) )
  2701. {// not a number
  2702. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2703. script_reportdata(data);
  2704. script_reportsrc(st);
  2705. script_pushnil(st);
  2706. st->state = END;
  2707. return;
  2708. }
  2709. i1 = data->u.num;
  2710. script_removetop(st, -1, 0);
  2711. switch( op )
  2712. {
  2713. case C_NEG: i1 = -i1; break;
  2714. case C_NOT: i1 = ~i1; break;
  2715. case C_LNOT: i1 = !i1; break;
  2716. default:
  2717. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2718. script_reportsrc(st);
  2719. script_pushnil(st);
  2720. st->state = END;
  2721. return;
  2722. }
  2723. script_pushint(st, i1);
  2724. }
  2725. /// Executes a buildin command.
  2726. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2727. int run_func(struct script_state *st)
  2728. {
  2729. struct script_data* data;
  2730. int i,start_sp,end_sp,func;
  2731. end_sp = st->stack->sp;// position after the last argument
  2732. for( i = end_sp-1; i > 0 ; --i )
  2733. if( st->stack->stack_data[i].type == C_ARG )
  2734. break;
  2735. if( i == 0 )
  2736. {
  2737. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2738. st->state = END;
  2739. script_reportsrc(st);
  2740. return 1;
  2741. }
  2742. start_sp = i-1;// C_NAME of the command
  2743. st->start = start_sp;
  2744. st->end = end_sp;
  2745. data = &st->stack->stack_data[st->start];
  2746. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2747. func = data->u.num;
  2748. else
  2749. {
  2750. ShowError("script:run_func: not a buildin command.\n");
  2751. script_reportdata(data);
  2752. script_reportsrc(st);
  2753. st->state = END;
  2754. return 1;
  2755. }
  2756. if(str_data[func].func){
  2757. if (str_data[func].func(st)) //Report error
  2758. script_reportsrc(st);
  2759. } else {
  2760. 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));
  2761. script_reportsrc(st);
  2762. st->state = END;
  2763. }
  2764. // Stack's datum are used when re-running functions [Eoe]
  2765. if( st->state == RERUNLINE )
  2766. return 0;
  2767. pop_stack(st, st->start, st->end);
  2768. if( st->state == RETFUNC )
  2769. {// return from a user-defined function
  2770. struct script_retinfo* ri;
  2771. int olddefsp = st->stack->defsp;
  2772. int nargs;
  2773. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2774. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2775. {
  2776. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2777. script_reportsrc(st);
  2778. st->state = END;
  2779. return 1;
  2780. }
  2781. script_free_vars( st->stack->var_function );
  2782. aFree(st->stack->var_function);
  2783. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2784. nargs = ri->nargs;
  2785. st->pos = ri->pos;
  2786. st->script = ri->script;
  2787. st->stack->var_function = ri->var_function;
  2788. st->stack->defsp = ri->defsp;
  2789. memset(ri, 0, sizeof(struct script_retinfo));
  2790. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2791. st->state = GOTO;
  2792. }
  2793. return 0;
  2794. }
  2795. /*==========================================
  2796. * script execution
  2797. *------------------------------------------*/
  2798. void run_script_main(struct script_state *st);
  2799. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2800. {
  2801. struct script_state *st;
  2802. if( rootscript == NULL || pos < 0 )
  2803. return;
  2804. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2805. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2806. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2807. st = script_alloc_state(rootscript, pos, rid, oid);
  2808. run_script_main(st);
  2809. }
  2810. void script_stop_sleeptimers(int id)
  2811. {
  2812. struct script_state* st;
  2813. for(;;)
  2814. {
  2815. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2816. if( st == NULL )
  2817. break; // no more sleep timers
  2818. script_free_state(st);
  2819. }
  2820. }
  2821. /*==========================================
  2822. * 指定ノードをsleep_dbから削除
  2823. *------------------------------------------*/
  2824. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2825. {
  2826. struct linkdb_node *retnode;
  2827. if( n == NULL)
  2828. return NULL;
  2829. if( n->prev == NULL )
  2830. sleep_db = n->next;
  2831. else
  2832. n->prev->next = n->next;
  2833. if( n->next )
  2834. n->next->prev = n->prev;
  2835. retnode = n->next;
  2836. aFree( n );
  2837. return retnode; // 次のノードを返す
  2838. }
  2839. /*==========================================
  2840. * sleep用タイマー関数
  2841. *------------------------------------------*/
  2842. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2843. {
  2844. struct script_state *st = (struct script_state *)data;
  2845. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2846. TBL_PC *sd = map_id2sd(st->rid);
  2847. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2848. { //Character mismatch. Cancel execution.
  2849. st->rid = 0;
  2850. st->state = END;
  2851. }
  2852. while( node && st->sleep.timer != -1 ) {
  2853. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2854. script_erase_sleepdb(node);
  2855. st->sleep.timer = INVALID_TIMER;
  2856. break;
  2857. }
  2858. node = node->next;
  2859. }
  2860. if(st->state != RERUNLINE)
  2861. st->sleep.tick = 0;
  2862. run_script_main(st);
  2863. return 0;
  2864. }
  2865. /*==========================================
  2866. * スクリプトの実行メイン部分
  2867. *------------------------------------------*/
  2868. void run_script_main(struct script_state *st)
  2869. {
  2870. int cmdcount=script_config.check_cmdcount;
  2871. int gotocount=script_config.check_gotocount;
  2872. TBL_PC *sd;
  2873. //For backing up purposes
  2874. struct script_state *bk_st = NULL;
  2875. int bk_npcid = 0;
  2876. struct script_stack *stack=st->stack;
  2877. struct npc_data *nd;
  2878. sd = map_id2sd(st->rid);
  2879. if(sd){
  2880. if(sd->st != st){
  2881. bk_st = sd->st;
  2882. bk_npcid = sd->npc_id;
  2883. }
  2884. sd->st = st;
  2885. sd->npc_id = st->oid;
  2886. }
  2887. nd = map_id2nd(st->oid);
  2888. if( nd && map[nd->bl.m].instance_id > 0 )
  2889. st->instance_id = map[nd->bl.m].instance_id;
  2890. if(st->state == RERUNLINE) {
  2891. run_func(st);
  2892. if(st->state == GOTO)
  2893. st->state = RUN;
  2894. } else if(st->state != END)
  2895. st->state = RUN;
  2896. while(st->state == RUN)
  2897. {
  2898. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2899. switch(c){
  2900. case C_EOL:
  2901. if( stack->defsp > stack->sp )
  2902. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  2903. else
  2904. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  2905. break;
  2906. case C_INT:
  2907. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2908. break;
  2909. case C_POS:
  2910. case C_NAME:
  2911. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2912. st->pos+=3;
  2913. break;
  2914. case C_ARG:
  2915. push_val(stack,c,0);
  2916. break;
  2917. case C_STR:
  2918. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2919. while(st->script->script_buf[st->pos++]);
  2920. break;
  2921. case C_FUNC:
  2922. run_func(st);
  2923. if(st->state==GOTO){
  2924. st->state = RUN;
  2925. if( gotocount>0 && (--gotocount)<=0 ){
  2926. ShowError("run_script: infinity loop !\n");
  2927. script_reportsrc(st);
  2928. st->state=END;
  2929. }
  2930. }
  2931. break;
  2932. case C_NEG:
  2933. case C_NOT:
  2934. case C_LNOT:
  2935. op_1(st ,c);
  2936. break;
  2937. case C_ADD:
  2938. case C_SUB:
  2939. case C_MUL:
  2940. case C_DIV:
  2941. case C_MOD:
  2942. case C_EQ:
  2943. case C_NE:
  2944. case C_GT:
  2945. case C_GE:
  2946. case C_LT:
  2947. case C_LE:
  2948. case C_AND:
  2949. case C_OR:
  2950. case C_XOR:
  2951. case C_LAND:
  2952. case C_LOR:
  2953. case C_R_SHIFT:
  2954. case C_L_SHIFT:
  2955. op_2(st, c);
  2956. break;
  2957. case C_OP3:
  2958. op_3(st, c);
  2959. break;
  2960. case C_NOP:
  2961. st->state=END;
  2962. break;
  2963. default:
  2964. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2965. st->state=END;
  2966. break;
  2967. }
  2968. if( cmdcount>0 && (--cmdcount)<=0 ){
  2969. ShowError("run_script: infinity loop !\n");
  2970. script_reportsrc(st);
  2971. st->state=END;
  2972. }
  2973. }
  2974. if(st->sleep.tick > 0) {
  2975. //Restore previous script
  2976. if (sd) {
  2977. sd->st = bk_st;
  2978. sd->npc_id = bk_npcid;
  2979. bk_st = NULL; //Remove tag for removal.
  2980. }
  2981. //Delay execution
  2982. sd = map_id2sd(st->rid); // Refresh sd since script might have attached someone while running. [Inkfish]
  2983. st->sleep.charid = sd?sd->status.char_id:0;
  2984. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2985. run_script_timer, st->sleep.charid, (intptr)st);
  2986. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2987. }
  2988. else if(st->state != END && sd){
  2989. //Resume later (st is already attached to player).
  2990. if(bk_st) {
  2991. ShowWarning("Unable to restore stack! Double continuation!\n");
  2992. //Report BOTH scripts to see if that can help somehow.
  2993. ShowDebug("Previous script (lost):\n");
  2994. script_reportsrc(bk_st);
  2995. ShowDebug("Current script:\n");
  2996. script_reportsrc(st);
  2997. }
  2998. } else {
  2999. //Dispose of script.
  3000. if (sd)
  3001. { //Restore previous stack and save char.
  3002. if(sd->state.using_fake_npc){
  3003. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  3004. sd->state.using_fake_npc = 0;
  3005. }
  3006. //Restore previous script if any.
  3007. sd->st = bk_st;
  3008. sd->npc_id = bk_npcid;
  3009. if (!bk_st)
  3010. npc_event_dequeue(sd);
  3011. else
  3012. bk_st = NULL; //Remove tag for removal.
  3013. if (sd->state.reg_dirty&2)
  3014. intif_saveregistry(sd,2);
  3015. if (sd->state.reg_dirty&1)
  3016. intif_saveregistry(sd,1);
  3017. }
  3018. script_free_state(st);
  3019. st = NULL;
  3020. }
  3021. if (bk_st)
  3022. { //Remove previous script
  3023. script_free_state(bk_st);
  3024. bk_st = NULL;
  3025. }
  3026. }
  3027. int script_config_read(char *cfgName)
  3028. {
  3029. int i;
  3030. char line[1024],w1[1024],w2[1024];
  3031. FILE *fp;
  3032. fp=fopen(cfgName,"r");
  3033. if(fp==NULL){
  3034. ShowError("file not found: [%s]\n", cfgName);
  3035. return 1;
  3036. }
  3037. while(fgets(line, sizeof(line), fp))
  3038. {
  3039. if(line[0] == '/' && line[1] == '/')
  3040. continue;
  3041. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3042. if(i!=2)
  3043. continue;
  3044. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3045. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3046. }
  3047. else if(strcmpi(w1,"check_cmdcount")==0) {
  3048. script_config.check_cmdcount = config_switch(w2);
  3049. }
  3050. else if(strcmpi(w1,"check_gotocount")==0) {
  3051. script_config.check_gotocount = config_switch(w2);
  3052. }
  3053. else if(strcmpi(w1,"input_min_value")==0) {
  3054. script_config.input_min_value = config_switch(w2);
  3055. }
  3056. else if(strcmpi(w1,"input_max_value")==0) {
  3057. script_config.input_max_value = config_switch(w2);
  3058. }
  3059. else if(strcmpi(w1,"import")==0){
  3060. script_config_read(w2);
  3061. }
  3062. }
  3063. fclose(fp);
  3064. return 0;
  3065. }
  3066. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3067. {
  3068. struct script_code *code = (struct script_code *)data;
  3069. if(code){
  3070. script_free_vars( &code->script_vars );
  3071. aFree( code->script_buf );
  3072. aFree( code );
  3073. }
  3074. return 0;
  3075. }
  3076. /*==========================================
  3077. * 終了
  3078. *------------------------------------------*/
  3079. int do_final_script()
  3080. {
  3081. #ifdef DEBUG_HASH
  3082. if (battle_config.etc_log)
  3083. {
  3084. FILE *fp = fopen("hash_dump.txt","wt");
  3085. if(fp) {
  3086. int i,count[SCRIPT_HASH_SIZE];
  3087. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3088. int n=0;
  3089. int min=INT_MAX,max=0,zero=0;
  3090. double mean=0.0f;
  3091. double median=0.0f;
  3092. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3093. memset(count, 0, sizeof(count));
  3094. fprintf(fp,"num : hash : data_name\n");
  3095. fprintf(fp,"---------------------------------------------------------------\n");
  3096. for(i=LABEL_START; i<str_num; i++) {
  3097. unsigned int h = calc_hash(get_str(i));
  3098. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3099. ++count[h];
  3100. }
  3101. fprintf(fp,"--------------------\n\n");
  3102. memset(count2, 0, sizeof(count2));
  3103. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3104. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3105. if(min > count[i])
  3106. min = count[i]; // minimun count of collision
  3107. if(max < count[i])
  3108. max = count[i]; // maximun count of collision
  3109. if(count[i] == 0)
  3110. zero++;
  3111. ++count2[count[i]];
  3112. }
  3113. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3114. for( i=min; i <= max; ++i ){
  3115. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3116. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3117. }
  3118. for( i=min; i <= max; ++i ){
  3119. n += count2[i];
  3120. if( n*2 >= SCRIPT_HASH_SIZE )
  3121. {
  3122. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3123. median = (i+i+1)/2.0f;
  3124. else
  3125. median = i;
  3126. break;
  3127. }
  3128. }
  3129. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3130. fclose(fp);
  3131. }
  3132. }
  3133. #endif
  3134. mapreg_final();
  3135. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3136. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3137. if(sleep_db) {
  3138. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3139. while(n) {
  3140. struct script_state *st = (struct script_state *)n->data;
  3141. script_free_state(st);
  3142. n = n->next;
  3143. }
  3144. linkdb_final(&sleep_db);
  3145. }
  3146. if (str_data)
  3147. aFree(str_data);
  3148. if (str_buf)
  3149. aFree(str_buf);
  3150. return 0;
  3151. }
  3152. /*==========================================
  3153. * 初期化
  3154. *------------------------------------------*/
  3155. int do_init_script()
  3156. {
  3157. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3158. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3159. mapreg_init();
  3160. return 0;
  3161. }
  3162. int script_reload()
  3163. {
  3164. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3165. scriptlabel_db->clear(scriptlabel_db, NULL);
  3166. if(sleep_db) {
  3167. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3168. while(n) {
  3169. struct script_state *st = (struct script_state *)n->data;
  3170. script_free_state(st);
  3171. n = n->next;
  3172. }
  3173. linkdb_final(&sleep_db);
  3174. }
  3175. mapreg_reload();
  3176. return 0;
  3177. }
  3178. //-----------------------------------------------------------------------------
  3179. // buildin functions
  3180. //
  3181. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3182. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3183. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3184. /////////////////////////////////////////////////////////////////////
  3185. // NPC interaction
  3186. //
  3187. /// Appends a message to the npc dialog.
  3188. /// If a dialog doesn't exist yet, one is created.
  3189. ///
  3190. /// mes "<message>";
  3191. BUILDIN_FUNC(mes)
  3192. {
  3193. TBL_PC* sd = script_rid2sd(st);
  3194. if( sd == NULL )
  3195. return 0;
  3196. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3197. return 0;
  3198. }
  3199. /// Displays the button 'next' in the npc dialog.
  3200. /// The dialog text is cleared and the script continues when the button is pressed.
  3201. ///
  3202. /// next;
  3203. BUILDIN_FUNC(next)
  3204. {
  3205. TBL_PC* sd;
  3206. sd = script_rid2sd(st);
  3207. if( sd == NULL )
  3208. return 0;
  3209. st->state = STOP;
  3210. clif_scriptnext(sd, st->oid);
  3211. return 0;
  3212. }
  3213. /// Ends the script and displays the button 'close' on the npc dialog.
  3214. /// The dialog is closed when the button is pressed.
  3215. ///
  3216. /// close;
  3217. BUILDIN_FUNC(close)
  3218. {
  3219. TBL_PC* sd;
  3220. sd = script_rid2sd(st);
  3221. if( sd == NULL )
  3222. return 0;
  3223. st->state = END;
  3224. clif_scriptclose(sd, st->oid);
  3225. return 0;
  3226. }
  3227. /// Displays the button 'close' on the npc dialog.
  3228. /// The dialog is closed and the script continues when the button is pressed.
  3229. ///
  3230. /// close2;
  3231. BUILDIN_FUNC(close2)
  3232. {
  3233. TBL_PC* sd;
  3234. sd = script_rid2sd(st);
  3235. if( sd == NULL )
  3236. return 0;
  3237. st->state = STOP;
  3238. clif_scriptclose(sd, st->oid);
  3239. return 0;
  3240. }
  3241. /// Counts the number of valid and total number of options in 'str'
  3242. /// If max_count > 0 the counting stops when that valid option is reached
  3243. /// total is incremented for each option (NULL is supported)
  3244. static int menu_countoptions(const char* str, int max_count, int* total)
  3245. {
  3246. int count = 0;
  3247. int bogus_total;
  3248. if( total == NULL )
  3249. total = &bogus_total;
  3250. ++(*total);
  3251. // initial empty options
  3252. while( *str == ':' )
  3253. {
  3254. ++str;
  3255. ++(*total);
  3256. }
  3257. // count menu options
  3258. while( *str != '\0' )
  3259. {
  3260. ++count;
  3261. --max_count;
  3262. if( max_count == 0 )
  3263. break;
  3264. while( *str != ':' && *str != '\0' )
  3265. ++str;
  3266. while( *str == ':' )
  3267. {
  3268. ++str;
  3269. ++(*total);
  3270. }
  3271. }
  3272. return count;
  3273. }
  3274. /// Displays a menu with options and goes to the target label.
  3275. /// The script is stopped if cancel is pressed.
  3276. /// Options with no text are not displayed in the client.
  3277. ///
  3278. /// Options can be grouped together, separated by the character ':' in the text:
  3279. /// ex: menu "A:B:C",L_target;
  3280. /// All these options go to the specified target label.
  3281. ///
  3282. /// The index of the selected option is put in the variable @menu.
  3283. /// Indexes start with 1 and are consistent with grouped and empty options.
  3284. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3285. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3286. ///
  3287. /// NOTE: the client closes the npc dialog when cancel is pressed
  3288. ///
  3289. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3290. BUILDIN_FUNC(menu)
  3291. {
  3292. int i;
  3293. const char* text;
  3294. TBL_PC* sd;
  3295. sd = script_rid2sd(st);
  3296. if( sd == NULL )
  3297. return 0;
  3298. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3299. if( sd->state.menu_or_input == 0 )
  3300. {
  3301. struct StringBuf buf;
  3302. struct script_data* data;
  3303. if( script_lastdata(st) % 2 == 0 )
  3304. {// argument count is not even (1st argument is at index 2)
  3305. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3306. st->state = END;
  3307. return 1;
  3308. }
  3309. StringBuf_Init(&buf);
  3310. sd->npc_menu = 0;
  3311. for( i = 2; i < script_lastdata(st); i += 2 )
  3312. {
  3313. // menu options
  3314. text = script_getstr(st, i);
  3315. // target label
  3316. data = script_getdata(st, i+1);
  3317. if( !data_islabel(data) )
  3318. {// not a label
  3319. StringBuf_Destroy(&buf);
  3320. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3321. script_reportdata(data);
  3322. st->state = END;
  3323. return 1;
  3324. }
  3325. // append option(s)
  3326. if( text[0] == '\0' )
  3327. continue;// empty string, ignore
  3328. if( sd->npc_menu > 0 )
  3329. StringBuf_AppendStr(&buf, ":");
  3330. StringBuf_AppendStr(&buf, text);
  3331. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3332. }
  3333. st->state = RERUNLINE;
  3334. sd->state.menu_or_input = 1;
  3335. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3336. StringBuf_Destroy(&buf);
  3337. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3338. }
  3339. else if( sd->npc_menu == 0xff )
  3340. {// Cancel was pressed
  3341. sd->state.menu_or_input = 0;
  3342. st->state = END;
  3343. }
  3344. else
  3345. {// goto target label
  3346. int menu = 0;
  3347. sd->state.menu_or_input = 0;
  3348. if( sd->npc_menu <= 0 )
  3349. {
  3350. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3351. st->state = END;
  3352. return 1;
  3353. }
  3354. // get target label
  3355. for( i = 2; i < script_lastdata(st); i += 2 )
  3356. {
  3357. text = script_getstr(st, i);
  3358. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3359. if( sd->npc_menu <= 0 )
  3360. break;// entry found
  3361. }
  3362. if( sd->npc_menu > 0 )
  3363. {// Invalid selection
  3364. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3365. st->state = END;
  3366. return 1;
  3367. }
  3368. if( !data_islabel(script_getdata(st, i + 1)) )
  3369. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3370. ShowError("script:menu: unexpected data in label argument\n");
  3371. script_reportdata(script_getdata(st, i + 1));
  3372. st->state = END;
  3373. return 1;
  3374. }
  3375. pc_setreg(sd, add_str("@menu"), menu);
  3376. st->pos = script_getnum(st, i + 1);
  3377. st->state = GOTO;
  3378. }
  3379. return 0;
  3380. }
  3381. /// Displays a menu with options and returns the selected option.
  3382. /// Behaves like 'menu' without the target labels.
  3383. ///
  3384. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3385. ///
  3386. /// @see menu
  3387. BUILDIN_FUNC(select)
  3388. {
  3389. int i;
  3390. const char* text;
  3391. TBL_PC* sd;
  3392. sd = script_rid2sd(st);
  3393. if( sd == NULL )
  3394. return 0;
  3395. if( sd->state.menu_or_input == 0 )
  3396. {
  3397. struct StringBuf buf;
  3398. StringBuf_Init(&buf);
  3399. sd->npc_menu = 0;
  3400. for( i = 2; i <= script_lastdata(st); ++i )
  3401. {
  3402. text = script_getstr(st, i);
  3403. if( sd->npc_menu > 0 )
  3404. StringBuf_AppendStr(&buf, ":");
  3405. StringBuf_AppendStr(&buf, text);
  3406. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3407. }
  3408. st->state = RERUNLINE;
  3409. sd->state.menu_or_input = 1;
  3410. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3411. StringBuf_Destroy(&buf);
  3412. }
  3413. else if( sd->npc_menu == 0xff )
  3414. {// Cancel was pressed
  3415. sd->state.menu_or_input = 0;
  3416. st->state = END;
  3417. }
  3418. else
  3419. {// return selected option
  3420. int menu = 0;
  3421. sd->state.menu_or_input = 0;
  3422. for( i = 2; i <= script_lastdata(st); ++i )
  3423. {
  3424. text = script_getstr(st, i);
  3425. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3426. if( sd->npc_menu <= 0 )
  3427. break;// entry found
  3428. }
  3429. pc_setreg(sd, add_str("@menu"), menu);
  3430. script_pushint(st, menu);
  3431. st->state = RUN;
  3432. }
  3433. return 0;
  3434. }
  3435. /// Displays a menu with options and returns the selected option.
  3436. /// Behaves like 'menu' without the target labels, except when cancel is
  3437. /// pressed.
  3438. /// When cancel is pressed, the script continues and 255 is returned.
  3439. ///
  3440. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3441. ///
  3442. /// @see menu
  3443. BUILDIN_FUNC(prompt)
  3444. {
  3445. int i;
  3446. const char *text;
  3447. TBL_PC* sd;
  3448. sd = script_rid2sd(st);
  3449. if( sd == NULL )
  3450. return 0;
  3451. if( sd->state.menu_or_input == 0 )
  3452. {
  3453. struct StringBuf buf;
  3454. StringBuf_Init(&buf);
  3455. sd->npc_menu = 0;
  3456. for( i = 2; i <= script_lastdata(st); ++i )
  3457. {
  3458. text = script_getstr(st, i);
  3459. if( sd->npc_menu > 0 )
  3460. StringBuf_AppendStr(&buf, ":");
  3461. StringBuf_AppendStr(&buf, text);
  3462. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3463. }
  3464. st->state = RERUNLINE;
  3465. sd->state.menu_or_input = 1;
  3466. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3467. StringBuf_Destroy(&buf);
  3468. }
  3469. else if( sd->npc_menu == 0xff )
  3470. {// Cancel was pressed
  3471. sd->state.menu_or_input = 0;
  3472. pc_setreg(sd, add_str("@menu"), 0xff);
  3473. script_pushint(st, 0xff);
  3474. st->state = RUN;
  3475. }
  3476. else
  3477. {// return selected option
  3478. int menu = 0;
  3479. sd->state.menu_or_input = 0;
  3480. for( i = 2; i <= script_lastdata(st); ++i )
  3481. {
  3482. text = script_getstr(st, i);
  3483. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3484. if( sd->npc_menu <= 0 )
  3485. break;// entry found
  3486. }
  3487. pc_setreg(sd, add_str("@menu"), menu);
  3488. script_pushint(st, menu);
  3489. st->state = RUN;
  3490. }
  3491. return 0;
  3492. }
  3493. /////////////////////////////////////////////////////////////////////
  3494. // ...
  3495. //
  3496. /// Jumps to the target script label.
  3497. ///
  3498. /// goto <label>;
  3499. BUILDIN_FUNC(goto)
  3500. {
  3501. if( !data_islabel(script_getdata(st,2)) )
  3502. {
  3503. ShowError("script:goto: not a label\n");
  3504. script_reportdata(script_getdata(st,2));
  3505. st->state = END;
  3506. return 1;
  3507. }
  3508. st->pos = script_getnum(st,2);
  3509. st->state = GOTO;
  3510. return 0;
  3511. }
  3512. /*==========================================
  3513. * user-defined function call
  3514. *------------------------------------------*/
  3515. BUILDIN_FUNC(callfunc)
  3516. {
  3517. int i, j;
  3518. struct script_retinfo* ri;
  3519. struct script_code* scr;
  3520. const char* str = script_getstr(st,2);
  3521. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3522. if( !scr )
  3523. {
  3524. ShowError("script:callfunc: function not found! [%s]\n", str);
  3525. st->state = END;
  3526. return 1;
  3527. }
  3528. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3529. {
  3530. struct script_data* data = push_copy(st->stack,i);
  3531. if( data_isreference(data) && !data->ref )
  3532. {
  3533. const char* name = reference_getname(data);
  3534. if( name[0] == '.' && name[1] == '@' )
  3535. data->ref = st->stack->var_function;
  3536. else if( name[0] == '.' )
  3537. data->ref = &st->script->script_vars;
  3538. }
  3539. }
  3540. CREATE(ri, struct script_retinfo, 1);
  3541. ri->script = st->script;// script code
  3542. ri->var_function = st->stack->var_function;// scope variables
  3543. ri->pos = st->pos;// script location
  3544. ri->nargs = j;// argument count
  3545. ri->defsp = st->stack->defsp;// default stack pointer
  3546. push_retinfo(st->stack, ri);
  3547. st->pos = 0;
  3548. st->script = scr;
  3549. st->stack->defsp = st->stack->sp;
  3550. st->state = GOTO;
  3551. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3552. return 0;
  3553. }
  3554. /*==========================================
  3555. * subroutine call
  3556. *------------------------------------------*/
  3557. BUILDIN_FUNC(callsub)
  3558. {
  3559. int i,j;
  3560. struct script_retinfo* ri;
  3561. int pos = script_getnum(st,2);
  3562. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3563. {
  3564. ShowError("script:callsub: argument is not a label\n");
  3565. script_reportdata(script_getdata(st,2));
  3566. st->state = END;
  3567. return 1;
  3568. }
  3569. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3570. {
  3571. struct script_data* data = push_copy(st->stack,i);
  3572. if( data_isreference(data) && !data->ref )
  3573. {
  3574. const char* name = reference_getname(data);
  3575. if( name[0] == '.' && name[1] == '@' )
  3576. data->ref = st->stack->var_function;
  3577. }
  3578. }
  3579. CREATE(ri, struct script_retinfo, 1);
  3580. ri->script = st->script;// script code
  3581. ri->var_function = st->stack->var_function;// scope variables
  3582. ri->pos = st->pos;// script location
  3583. ri->nargs = j;// argument count
  3584. ri->defsp = st->stack->defsp;// default stack pointer
  3585. push_retinfo(st->stack, ri);
  3586. st->pos = pos;
  3587. st->stack->defsp = st->stack->sp;
  3588. st->state = GOTO;
  3589. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3590. return 0;
  3591. }
  3592. /// Retrieves an argument provided to callfunc/callsub.
  3593. /// If the argument doesn't exist
  3594. ///
  3595. /// getarg(<index>{,<default_value>}) -> <value>
  3596. BUILDIN_FUNC(getarg)
  3597. {
  3598. struct script_retinfo* ri;
  3599. int idx;
  3600. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3601. {
  3602. ShowError("script:getarg: no callfunc or callsub!\n");
  3603. st->state = END;
  3604. return 1;
  3605. }
  3606. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3607. idx = script_getnum(st,2);
  3608. if( idx >= 0 && idx < ri->nargs )
  3609. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3610. else if( script_hasdata(st,3) )
  3611. script_pushcopy(st, 3);
  3612. else
  3613. {
  3614. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3615. st->state = END;
  3616. return 1;
  3617. }
  3618. return 0;
  3619. }
  3620. /// Returns from the current function, optionaly returning a value from the functions.
  3621. /// Don't use outside script functions.
  3622. ///
  3623. /// return;
  3624. /// return <value>;
  3625. BUILDIN_FUNC(return)
  3626. {
  3627. if( script_hasdata(st,2) )
  3628. {// return value
  3629. struct script_data* data;
  3630. script_pushcopy(st, 2);
  3631. data = script_getdatatop(st, -1);
  3632. if( data_isreference(data) )
  3633. {
  3634. const char* name = reference_getname(data);
  3635. if( name[0] == '.' && name[1] == '@' )
  3636. {// scope variable
  3637. if( !data->ref || data->ref == st->stack->var_function )
  3638. get_val(st, data);// current scope, convert to value
  3639. }
  3640. else if( name[0] == '.' && !data->ref )
  3641. {// script variable, link to current script
  3642. data->ref = &st->script->script_vars;
  3643. }
  3644. }
  3645. }
  3646. else
  3647. {// no return value
  3648. script_pushnil(st);
  3649. }
  3650. st->state = RETFUNC;
  3651. return 0;
  3652. }
  3653. /// Returns a random number from 0 to <range>-1.
  3654. /// Or returns a random number from <min> to <max>.
  3655. /// If <min> is greater than <max>, their numbers are switched.
  3656. /// rand(<range>) -> <int>
  3657. /// rand(<min>,<max>) -> <int>
  3658. BUILDIN_FUNC(rand)
  3659. {
  3660. int range;
  3661. int min;
  3662. int max;
  3663. if( script_hasdata(st,3) )
  3664. {// min,max
  3665. min = script_getnum(st,2);
  3666. max = script_getnum(st,3);
  3667. if( max < min )
  3668. swap(min, max);
  3669. range = max - min + 1;
  3670. }
  3671. else
  3672. {// range
  3673. min = 0;
  3674. range = script_getnum(st,2);
  3675. }
  3676. if( range <= 1 )
  3677. script_pushint(st, min);
  3678. else
  3679. script_pushint(st, rand()%range + min);
  3680. return 0;
  3681. }
  3682. /*==========================================
  3683. *
  3684. *------------------------------------------*/
  3685. BUILDIN_FUNC(warp)
  3686. {
  3687. int ret;
  3688. int x,y;
  3689. const char* str;
  3690. TBL_PC* sd;
  3691. sd = script_rid2sd(st);
  3692. if( sd == NULL )
  3693. return 0;
  3694. str = script_getstr(st,2);
  3695. x = script_getnum(st,3);
  3696. y = script_getnum(st,4);
  3697. if(strcmp(str,"Random")==0)
  3698. ret = pc_randomwarp(sd,3);
  3699. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3700. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3701. else
  3702. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3703. if( ret ) {
  3704. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3705. script_reportsrc(st);
  3706. }
  3707. return 0;
  3708. }
  3709. /*==========================================
  3710. * エリア指定ワープ
  3711. *------------------------------------------*/
  3712. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3713. {
  3714. int x,y;
  3715. unsigned int map;
  3716. map=va_arg(ap, unsigned int);
  3717. x=va_arg(ap,int);
  3718. y=va_arg(ap,int);
  3719. if(map == 0)
  3720. pc_randomwarp((TBL_PC *)bl,3);
  3721. else
  3722. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3723. return 0;
  3724. }
  3725. BUILDIN_FUNC(areawarp)
  3726. {
  3727. int x,y,m;
  3728. unsigned int index;
  3729. const char *str;
  3730. const char *mapname;
  3731. int x0,y0,x1,y1;
  3732. mapname=script_getstr(st,2);
  3733. x0=script_getnum(st,3);
  3734. y0=script_getnum(st,4);
  3735. x1=script_getnum(st,5);
  3736. y1=script_getnum(st,6);
  3737. str=script_getstr(st,7);
  3738. x=script_getnum(st,8);
  3739. y=script_getnum(st,9);
  3740. if( (m=map_mapname2mapid(mapname))< 0)
  3741. return 0;
  3742. if(strcmp(str,"Random")==0)
  3743. index = 0;
  3744. else if(!(index=mapindex_name2id(str)))
  3745. return 0;
  3746. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3747. return 0;
  3748. }
  3749. /*==========================================
  3750. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  3751. *------------------------------------------*/
  3752. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  3753. {
  3754. int hp, sp;
  3755. hp = va_arg(ap, int);
  3756. sp = va_arg(ap, int);
  3757. pc_percentheal((TBL_PC *)bl,hp,sp);
  3758. return 0;
  3759. }
  3760. BUILDIN_FUNC(areapercentheal)
  3761. {
  3762. int hp,sp,m;
  3763. const char *mapname;
  3764. int x0,y0,x1,y1;
  3765. mapname=script_getstr(st,2);
  3766. x0=script_getnum(st,3);
  3767. y0=script_getnum(st,4);
  3768. x1=script_getnum(st,5);
  3769. y1=script_getnum(st,6);
  3770. hp=script_getnum(st,7);
  3771. sp=script_getnum(st,8);
  3772. if( (m=map_mapname2mapid(mapname))< 0)
  3773. return 0;
  3774. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  3775. return 0;
  3776. }
  3777. /*==========================================
  3778. * warpchar [LuzZza]
  3779. * Useful for warp one player from
  3780. * another player npc-session.
  3781. * Using: warpchar "mapname",x,y,Char_ID;
  3782. *------------------------------------------*/
  3783. BUILDIN_FUNC(warpchar)
  3784. {
  3785. int x,y,a;
  3786. const char *str;
  3787. TBL_PC *sd;
  3788. str=script_getstr(st,2);
  3789. x=script_getnum(st,3);
  3790. y=script_getnum(st,4);
  3791. a=script_getnum(st,5);
  3792. sd = map_charid2sd(a);
  3793. if( sd == NULL )
  3794. return 0;
  3795. if(strcmp(str, "Random") == 0)
  3796. pc_randomwarp(sd, 3);
  3797. else
  3798. if(strcmp(str, "SavePoint") == 0)
  3799. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3800. else
  3801. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3802. return 0;
  3803. }
  3804. /*==========================================
  3805. * Warpparty - [Fredzilla]
  3806. * Syntax: warpparty "mapname",x,y,Party_ID;
  3807. *------------------------------------------*/
  3808. BUILDIN_FUNC(warpparty)
  3809. {
  3810. int x,y;
  3811. const char *str;
  3812. int p_id;
  3813. int i;
  3814. unsigned short mapindex;
  3815. TBL_PC *pl_sd;
  3816. struct party_data *p=NULL;
  3817. str=script_getstr(st,2);
  3818. x=script_getnum(st,3);
  3819. y=script_getnum(st,4);
  3820. p_id=script_getnum(st,5);
  3821. if(p_id < 1)
  3822. return 0;
  3823. p = party_search(p_id);
  3824. if (!p)
  3825. return 0;
  3826. if(strcmp(str,"Random")==0)
  3827. {
  3828. for (i = 0; i < MAX_PARTY; i++)
  3829. {
  3830. if ((pl_sd = p->data[i].sd))
  3831. {
  3832. if(map[pl_sd->bl.m].flag.nowarp)
  3833. continue;
  3834. pc_randomwarp(pl_sd,3);
  3835. }
  3836. }
  3837. }
  3838. else if(strcmp(str,"SavePointAll")==0)
  3839. {
  3840. for (i = 0; i < MAX_PARTY; i++)
  3841. {
  3842. if ((pl_sd = p->data[i].sd))
  3843. {
  3844. if(map[pl_sd->bl.m].flag.noreturn)
  3845. continue;
  3846. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3847. }
  3848. }
  3849. }
  3850. else if(strcmp(str,"SavePoint")==0)
  3851. {
  3852. pl_sd=script_rid2sd(st);
  3853. if (!pl_sd) return 0;
  3854. mapindex=pl_sd->status.save_point.map;
  3855. x=pl_sd->status.save_point.x;
  3856. y=pl_sd->status.save_point.y;
  3857. for (i = 0; i < MAX_PARTY; i++)
  3858. {
  3859. if ((pl_sd = p->data[i].sd))
  3860. {
  3861. if(map[pl_sd->bl.m].flag.noreturn)
  3862. continue;
  3863. pc_setpos(pl_sd,mapindex,x,y,3);
  3864. }
  3865. }
  3866. }
  3867. else if(strcmp(str,"Leader")==0)
  3868. {
  3869. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  3870. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  3871. return 0;
  3872. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  3873. return 0;
  3874. mapindex = p->data[i].sd->mapindex;
  3875. x = p->data[i].sd->bl.x;
  3876. y = p->data[i].sd->bl.y;
  3877. for (i = 0; i < MAX_PARTY; i++)
  3878. {
  3879. pl_sd = p->data[i].sd;
  3880. if (!pl_sd)
  3881. continue;
  3882. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3883. continue;
  3884. pc_setpos(pl_sd,mapindex,x,y,3);
  3885. }
  3886. }
  3887. else
  3888. {
  3889. mapindex = mapindex_name2id(str);
  3890. if (!mapindex) //Show source of npc error.
  3891. return 1;
  3892. for (i = 0; i < MAX_PARTY; i++)
  3893. {
  3894. if ((pl_sd = p->data[i].sd))
  3895. {
  3896. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3897. continue;
  3898. pc_setpos(pl_sd,mapindex,x,y,3);
  3899. }
  3900. }
  3901. }
  3902. return 0;
  3903. }
  3904. /*==========================================
  3905. * Warpguild - [Fredzilla]
  3906. * Syntax: warpguild "mapname",x,y,Guild_ID;
  3907. *------------------------------------------*/
  3908. BUILDIN_FUNC(warpguild)
  3909. {
  3910. TBL_PC *sd;
  3911. TBL_PC *pl_sd;
  3912. struct guild* g;
  3913. struct s_mapiterator* iter;
  3914. int type;
  3915. const char* str = script_getstr(st,2);
  3916. int x = script_getnum(st,3);
  3917. int y = script_getnum(st,4);
  3918. int gid = script_getnum(st,5);
  3919. sd=script_rid2sd(st);
  3920. if( sd == NULL )
  3921. return 0;
  3922. g = guild_search(gid);
  3923. if( g == NULL )
  3924. return 0;
  3925. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3926. return 0;
  3927. type = ( strcmp(str,"Random")==0 ) ? 0
  3928. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3929. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3930. : 3;
  3931. iter = mapit_getallusers();
  3932. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  3933. {
  3934. if( pl_sd->status.guild_id != gid )
  3935. continue;
  3936. switch( type )
  3937. {
  3938. case 0: // Random
  3939. if(!map[pl_sd->bl.m].flag.nowarp)
  3940. pc_randomwarp(pl_sd,3);
  3941. break;
  3942. case 1: // SavePointAll
  3943. if(!map[pl_sd->bl.m].flag.noreturn)
  3944. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3945. break;
  3946. case 2: // SavePoint
  3947. if(!map[pl_sd->bl.m].flag.noreturn)
  3948. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3949. break;
  3950. case 3: // m,x,y
  3951. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  3952. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  3953. break;
  3954. }
  3955. }
  3956. mapit_free(iter);
  3957. return 0;
  3958. }
  3959. /*==========================================
  3960. *
  3961. *------------------------------------------*/
  3962. BUILDIN_FUNC(heal)
  3963. {
  3964. TBL_PC *sd;
  3965. int hp,sp;
  3966. sd = script_rid2sd(st);
  3967. if (!sd) return 0;
  3968. hp=script_getnum(st,2);
  3969. sp=script_getnum(st,3);
  3970. status_heal(&sd->bl, hp, sp, 1);
  3971. return 0;
  3972. }
  3973. /*==========================================
  3974. *
  3975. *------------------------------------------*/
  3976. BUILDIN_FUNC(itemheal)
  3977. {
  3978. TBL_PC *sd;
  3979. int hp,sp;
  3980. hp=script_getnum(st,2);
  3981. sp=script_getnum(st,3);
  3982. if(potion_flag==1) {
  3983. potion_hp = hp;
  3984. potion_sp = sp;
  3985. return 0;
  3986. }
  3987. sd = script_rid2sd(st);
  3988. if (!sd) return 0;
  3989. pc_itemheal(sd,sd->itemid,hp,sp);
  3990. return 0;
  3991. }
  3992. /*==========================================
  3993. *
  3994. *------------------------------------------*/
  3995. BUILDIN_FUNC(percentheal)
  3996. {
  3997. int hp,sp;
  3998. TBL_PC* sd;
  3999. hp=script_getnum(st,2);
  4000. sp=script_getnum(st,3);
  4001. if(potion_flag==1) {
  4002. potion_per_hp = hp;
  4003. potion_per_sp = sp;
  4004. return 0;
  4005. }
  4006. sd = script_rid2sd(st);
  4007. if( sd == NULL )
  4008. return 0;
  4009. pc_percentheal(sd,hp,sp);
  4010. return 0;
  4011. }
  4012. /*==========================================
  4013. *
  4014. *------------------------------------------*/
  4015. BUILDIN_FUNC(jobchange)
  4016. {
  4017. int job, upper=-1;
  4018. job=script_getnum(st,2);
  4019. if( script_hasdata(st,3) )
  4020. upper=script_getnum(st,3);
  4021. if (pcdb_checkid(job))
  4022. {
  4023. TBL_PC* sd;
  4024. sd = script_rid2sd(st);
  4025. if( sd == NULL )
  4026. return 0;
  4027. pc_jobchange(sd, job, upper);
  4028. }
  4029. return 0;
  4030. }
  4031. /*==========================================
  4032. *
  4033. *------------------------------------------*/
  4034. BUILDIN_FUNC(jobname)
  4035. {
  4036. int class_=script_getnum(st,2);
  4037. script_pushconststr(st,job_name(class_));
  4038. return 0;
  4039. }
  4040. /// Get input from the player.
  4041. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4042. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4043. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4044. /// shorter than 'min' and 0 otherwise.
  4045. ///
  4046. /// input(<var>{,<min>{,<max>}}) -> <int>
  4047. BUILDIN_FUNC(input)
  4048. {
  4049. TBL_PC* sd;
  4050. struct script_data* data;
  4051. int uid;
  4052. const char* name;
  4053. int min;
  4054. int max;
  4055. sd = script_rid2sd(st);
  4056. if( sd == NULL )
  4057. return 0;
  4058. data = script_getdata(st,2);
  4059. if( !data_isreference(data) ){
  4060. ShowError("script:input: not a variable\n");
  4061. script_reportdata(data);
  4062. st->state = END;
  4063. return 1;
  4064. }
  4065. uid = reference_getuid(data);
  4066. name = reference_getname(data);
  4067. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4068. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4069. if( !sd->state.menu_or_input )
  4070. { // first invocation, display npc input box
  4071. sd->state.menu_or_input = 1;
  4072. st->state = RERUNLINE;
  4073. if( is_string_variable(name) )
  4074. clif_scriptinputstr(sd,st->oid);
  4075. else
  4076. clif_scriptinput(sd,st->oid);
  4077. }
  4078. else
  4079. { // take received text/value and store it in the designated variable
  4080. sd->state.menu_or_input = 0;
  4081. if( is_string_variable(name) )
  4082. {
  4083. int len = (int)strlen(sd->npc_str);
  4084. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4085. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4086. }
  4087. else
  4088. {
  4089. int amount = sd->npc_amount;
  4090. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4091. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4092. }
  4093. st->state = RUN;
  4094. }
  4095. return 0;
  4096. }
  4097. /// Sets the value of a variable.
  4098. /// The value is converted to the type of the variable.
  4099. ///
  4100. /// set(<variable>,<value>) -> <variable>
  4101. BUILDIN_FUNC(set)
  4102. {
  4103. TBL_PC* sd = NULL;
  4104. struct script_data* data;
  4105. int num;
  4106. const char* name;
  4107. char prefix;
  4108. data = script_getdata(st,2);
  4109. if( !data_isreference(data) )
  4110. {
  4111. ShowError("script:set: not a variable\n");
  4112. script_reportdata(script_getdata(st,2));
  4113. st->state = END;
  4114. return 1;
  4115. }
  4116. num = reference_getuid(data);
  4117. name = reference_getname(data);
  4118. prefix = *name;
  4119. if( not_server_variable(prefix) )
  4120. {
  4121. sd = script_rid2sd(st);
  4122. if( sd == NULL )
  4123. {
  4124. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4125. return 0;
  4126. }
  4127. }
  4128. if( is_string_variable(name) )
  4129. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4130. else
  4131. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4132. // return a copy of the variable reference
  4133. script_pushcopy(st,2);
  4134. return 0;
  4135. }
  4136. /////////////////////////////////////////////////////////////////////
  4137. /// Array variables
  4138. ///
  4139. /// Returns the size of the specified array
  4140. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4141. {
  4142. int32 ret = idx;
  4143. if( isstring )
  4144. {
  4145. for( ; idx < 128; ++idx )
  4146. {
  4147. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4148. if( str && *str )
  4149. ret = idx + 1;
  4150. script_removetop(st, -1, 0);
  4151. }
  4152. }
  4153. else
  4154. {
  4155. for( ; idx < 128; ++idx )
  4156. {
  4157. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4158. if( num )
  4159. ret = idx + 1;
  4160. script_removetop(st, -1, 0);
  4161. }
  4162. }
  4163. return ret;
  4164. }
  4165. /// Sets values of an array, from the starting index.
  4166. /// ex: setarray arr[1],1,2,3;
  4167. ///
  4168. /// setarray <array variable>,<value1>{,<value2>...};
  4169. BUILDIN_FUNC(setarray)
  4170. {
  4171. struct script_data* data;
  4172. const char* name;
  4173. int32 start;
  4174. int32 end;
  4175. int32 id;
  4176. int32 i;
  4177. TBL_PC* sd = NULL;
  4178. data = script_getdata(st, 2);
  4179. if( !data_isreference(data) )
  4180. {
  4181. ShowError("script:setarray: not a variable\n");
  4182. script_reportdata(data);
  4183. st->state = END;
  4184. return 1;// not a variable
  4185. }
  4186. id = reference_getid(data);
  4187. start = reference_getindex(data);
  4188. name = reference_getname(data);
  4189. if( not_array_variable(*name) )
  4190. {
  4191. ShowError("script:setarray: illegal scope\n");
  4192. script_reportdata(data);
  4193. st->state = END;
  4194. return 1;// not supported
  4195. }
  4196. if( not_server_variable(*name) )
  4197. {
  4198. sd = script_rid2sd(st);
  4199. if( sd == NULL )
  4200. return 0;// no player attached
  4201. }
  4202. end = start + script_lastdata(st) - 2;
  4203. if( end > 127 )
  4204. end = 127;
  4205. if( is_string_variable(name) )
  4206. {// string array
  4207. for( i = 3; start < end; ++start, ++i )
  4208. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4209. }
  4210. else
  4211. {// int array
  4212. for( i = 3; start < end; ++start, ++i )
  4213. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4214. }
  4215. return 0;
  4216. }
  4217. /// Sets count values of an array, from the starting index.
  4218. /// ex: cleararray arr[0],0,1;
  4219. ///
  4220. /// cleararray <array variable>,<value>,<count>;
  4221. BUILDIN_FUNC(cleararray)
  4222. {
  4223. struct script_data* data;
  4224. const char* name;
  4225. int32 start;
  4226. int32 end;
  4227. int32 id;
  4228. void* v;
  4229. TBL_PC* sd = NULL;
  4230. data = script_getdata(st, 2);
  4231. if( !data_isreference(data) )
  4232. {
  4233. ShowError("script:cleararray: not a variable\n");
  4234. script_reportdata(data);
  4235. st->state = END;
  4236. return 1;// not a variable
  4237. }
  4238. id = reference_getid(data);
  4239. start = reference_getindex(data);
  4240. name = reference_getname(data);
  4241. if( not_array_variable(*name) )
  4242. {
  4243. ShowError("script:cleararray: illegal scope\n");
  4244. script_reportdata(data);
  4245. st->state = END;
  4246. return 1;// not supported
  4247. }
  4248. if( not_server_variable(*name) )
  4249. {
  4250. sd = script_rid2sd(st);
  4251. if( sd == NULL )
  4252. return 0;// no player attached
  4253. }
  4254. if( is_string_variable(name) )
  4255. v = (void*)script_getstr(st, 3);
  4256. else
  4257. v = (void*)script_getnum(st, 3);
  4258. end = start + script_getnum(st, 4);
  4259. if( end > 127 )
  4260. end = 127;
  4261. for( ; start <= end; ++start )
  4262. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4263. return 0;
  4264. }
  4265. /// Copies data from one array to another.
  4266. /// ex: copyarray arr[0],arr[2],2;
  4267. ///
  4268. /// copyarray <destination array variable>,<source array variable>,<count>;
  4269. BUILDIN_FUNC(copyarray)
  4270. {
  4271. struct script_data* data1;
  4272. struct script_data* data2;
  4273. const char* name1;
  4274. const char* name2;
  4275. int32 idx1;
  4276. int32 idx2;
  4277. int32 id1;
  4278. int32 id2;
  4279. void* v;
  4280. int32 i;
  4281. int32 count;
  4282. TBL_PC* sd = NULL;
  4283. data1 = script_getdata(st, 2);
  4284. data2 = script_getdata(st, 3);
  4285. if( !data_isreference(data1) || !data_isreference(data2) )
  4286. {
  4287. ShowError("script:copyarray: not a variable\n");
  4288. script_reportdata(data1);
  4289. script_reportdata(data2);
  4290. st->state = END;
  4291. return 1;// not a variable
  4292. }
  4293. id1 = reference_getid(data1);
  4294. id2 = reference_getid(data2);
  4295. idx1 = reference_getindex(data1);
  4296. idx2 = reference_getindex(data2);
  4297. name1 = reference_getname(data1);
  4298. name2 = reference_getname(data2);
  4299. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4300. {
  4301. ShowError("script:copyarray: illegal scope\n");
  4302. script_reportdata(data1);
  4303. script_reportdata(data2);
  4304. st->state = END;
  4305. return 1;// not supported
  4306. }
  4307. if( is_string_variable(name1) != is_string_variable(name2) )
  4308. {
  4309. ShowError("script:copyarray: type mismatch\n");
  4310. script_reportdata(data1);
  4311. script_reportdata(data2);
  4312. st->state = END;
  4313. return 1;// data type mismatch
  4314. }
  4315. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4316. {
  4317. sd = script_rid2sd(st);
  4318. if( sd == NULL )
  4319. return 0;// no player attached
  4320. }
  4321. count = script_getnum(st, 4);
  4322. if( count > 128 - idx1 )
  4323. count = 128 - idx1;
  4324. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4325. return 0;// nothing to copy
  4326. if( id1 == id2 && idx1 > idx2 )
  4327. {// destination might be overlapping the source - copy in reverse order
  4328. for( i = count - 1; i >= 0; --i )
  4329. {
  4330. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4331. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4332. script_removetop(st, -1, 0);
  4333. }
  4334. }
  4335. else
  4336. {// normal copy
  4337. for( i = 0; i < count; ++i )
  4338. {
  4339. if( idx2 + i < 128 )
  4340. {
  4341. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4342. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4343. script_removetop(st, -1, 0);
  4344. }
  4345. else// out of range - assume ""/0
  4346. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4347. }
  4348. }
  4349. return 0;
  4350. }
  4351. /// Returns the size of the array.
  4352. /// Assumes that everything before the starting index exists.
  4353. /// ex: getarraysize(arr[3])
  4354. ///
  4355. /// getarraysize(<array variable>) -> <int>
  4356. BUILDIN_FUNC(getarraysize)
  4357. {
  4358. struct script_data* data;
  4359. const char* name;
  4360. data = script_getdata(st, 2);
  4361. if( !data_isreference(data) )
  4362. {
  4363. ShowError("script:getarraysize: not a variable\n");
  4364. script_reportdata(data);
  4365. script_pushnil(st);
  4366. st->state = END;
  4367. return 1;// not a variable
  4368. }
  4369. name = reference_getname(data);
  4370. if( not_array_variable(*name) )
  4371. {
  4372. ShowError("script:getarraysize: illegal scope\n");
  4373. script_reportdata(data);
  4374. script_pushnil(st);
  4375. st->state = END;
  4376. return 1;// not supported
  4377. }
  4378. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4379. return 0;
  4380. }
  4381. /// Deletes count or all the elements in an array, from the starting index.
  4382. /// ex: deletearray arr[4],2;
  4383. ///
  4384. /// deletearray <array variable>;
  4385. /// deletearray <array variable>,<count>;
  4386. BUILDIN_FUNC(deletearray)
  4387. {
  4388. struct script_data* data;
  4389. const char* name;
  4390. int start;
  4391. int end;
  4392. int id;
  4393. TBL_PC *sd = NULL;
  4394. data = script_getdata(st, 2);
  4395. if( !data_isreference(data) )
  4396. {
  4397. ShowError("script:deletearray: not a variable\n");
  4398. script_reportdata(data);
  4399. st->state = END;
  4400. return 1;// not a variable
  4401. }
  4402. id = reference_getid(data);
  4403. start = reference_getindex(data);
  4404. name = reference_getname(data);
  4405. if( not_array_variable(*name) )
  4406. {
  4407. ShowError("script:deletearray: illegal scope\n");
  4408. script_reportdata(data);
  4409. st->state = END;
  4410. return 1;// not supported
  4411. }
  4412. if( not_server_variable(*name) )
  4413. {
  4414. sd = script_rid2sd(st);
  4415. if( sd == NULL )
  4416. return 0;// no player attached
  4417. }
  4418. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4419. if( start >= end )
  4420. return 0;// nothing to free
  4421. if( script_hasdata(st,3) )
  4422. {
  4423. int count = script_getnum(st, 3);
  4424. if( count > end - start )
  4425. count = end - start;
  4426. if( count <= 0 )
  4427. return 0;// nothing to free
  4428. // move rest of the elements backward
  4429. for( ; start + count < end; ++start )
  4430. {
  4431. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4432. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4433. script_removetop(st, -1, 0);
  4434. }
  4435. }
  4436. // clear the rest of the array
  4437. if( is_string_variable(name) )
  4438. {
  4439. for( ; start < end; ++start )
  4440. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4441. }
  4442. else
  4443. {
  4444. for( ; start < end; ++start )
  4445. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4446. }
  4447. return 0;
  4448. }
  4449. /// Returns a reference to the target index of the array variable.
  4450. /// Equivalent to var[index].
  4451. ///
  4452. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4453. BUILDIN_FUNC(getelementofarray)
  4454. {
  4455. struct script_data* data;
  4456. const char* name;
  4457. int32 id;
  4458. int i;
  4459. data = script_getdata(st, 2);
  4460. if( !data_isreference(data) )
  4461. {
  4462. ShowError("script:getelementofarray: not a variable\n");
  4463. script_reportdata(data);
  4464. script_pushnil(st);
  4465. st->state = END;
  4466. return 1;// not a variable
  4467. }
  4468. id = reference_getid(data);
  4469. name = reference_getname(data);
  4470. if( not_array_variable(*name) )
  4471. {
  4472. ShowError("script:getelementofarray: illegal scope\n");
  4473. script_reportdata(data);
  4474. script_pushnil(st);
  4475. st->state = END;
  4476. return 1;// not supported
  4477. }
  4478. i = script_getnum(st, 3);
  4479. if( i < 0 || i >= 128 )
  4480. {
  4481. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4482. script_reportdata(data);
  4483. script_pushnil(st);
  4484. st->state = END;
  4485. return 1;// out of range
  4486. }
  4487. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4488. return 0;
  4489. }
  4490. /////////////////////////////////////////////////////////////////////
  4491. /// ...
  4492. ///
  4493. /*==========================================
  4494. *
  4495. *------------------------------------------*/
  4496. BUILDIN_FUNC(setlook)
  4497. {
  4498. int type,val;
  4499. TBL_PC* sd;
  4500. type=script_getnum(st,2);
  4501. val=script_getnum(st,3);
  4502. sd = script_rid2sd(st);
  4503. if( sd == NULL )
  4504. return 0;
  4505. pc_changelook(sd,type,val);
  4506. return 0;
  4507. }
  4508. BUILDIN_FUNC(changelook)
  4509. { // As setlook but only client side
  4510. int type,val;
  4511. TBL_PC* sd;
  4512. type=script_getnum(st,2);
  4513. val=script_getnum(st,3);
  4514. sd = script_rid2sd(st);
  4515. if( sd == NULL )
  4516. return 0;
  4517. clif_changelook(&sd->bl,type,val);
  4518. return 0;
  4519. }
  4520. /*==========================================
  4521. *
  4522. *------------------------------------------*/
  4523. BUILDIN_FUNC(cutin)
  4524. {
  4525. TBL_PC* sd;
  4526. sd = script_rid2sd(st);
  4527. if( sd == NULL )
  4528. return 0;
  4529. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4530. return 0;
  4531. }
  4532. /*==========================================
  4533. *
  4534. *------------------------------------------*/
  4535. BUILDIN_FUNC(viewpoint)
  4536. {
  4537. int type,x,y,id,color;
  4538. TBL_PC* sd;
  4539. type=script_getnum(st,2);
  4540. x=script_getnum(st,3);
  4541. y=script_getnum(st,4);
  4542. id=script_getnum(st,5);
  4543. color=script_getnum(st,6);
  4544. sd = script_rid2sd(st);
  4545. if( sd == NULL )
  4546. return 0;
  4547. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4548. return 0;
  4549. }
  4550. /*==========================================
  4551. *
  4552. *------------------------------------------*/
  4553. BUILDIN_FUNC(countitem)
  4554. {
  4555. int nameid, i;
  4556. int count = 0;
  4557. struct script_data* data;
  4558. TBL_PC* sd = script_rid2sd(st);
  4559. if (!sd) {
  4560. script_pushint(st,0);
  4561. return 0;
  4562. }
  4563. data = script_getdata(st,2);
  4564. get_val(st,data);
  4565. if( data_isstring(data) ) {
  4566. const char* name = conv_str(st,data);
  4567. struct item_data* item_data;
  4568. if((item_data = itemdb_searchname(name)) != NULL)
  4569. nameid = item_data->nameid;
  4570. else
  4571. nameid = 0;
  4572. } else
  4573. nameid = conv_num(st,data);
  4574. if (nameid < 500) {
  4575. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4576. script_reportsrc(st);
  4577. script_pushint(st,0);
  4578. return 1;
  4579. }
  4580. for(i = 0; i < MAX_INVENTORY; i++)
  4581. if(sd->status.inventory[i].nameid == nameid)
  4582. count += sd->status.inventory[i].amount;
  4583. script_pushint(st,count);
  4584. return 0;
  4585. }
  4586. /*==========================================
  4587. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4588. * returns number of items that meet the conditions
  4589. *------------------------------------------*/
  4590. BUILDIN_FUNC(countitem2)
  4591. {
  4592. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4593. int count = 0;
  4594. int i;
  4595. struct script_data* data;
  4596. TBL_PC* sd = script_rid2sd(st);
  4597. if (!sd) {
  4598. script_pushint(st,0);
  4599. return 0;
  4600. }
  4601. data = script_getdata(st,2);
  4602. get_val(st,data);
  4603. if( data_isstring(data) ) {
  4604. const char* name = conv_str(st,data);
  4605. struct item_data* item_data;
  4606. if((item_data = itemdb_searchname(name)) != NULL)
  4607. nameid = item_data->nameid;
  4608. else
  4609. nameid = 0;
  4610. } else
  4611. nameid = conv_num(st,data);
  4612. iden = script_getnum(st,3);
  4613. ref = script_getnum(st,4);
  4614. attr = script_getnum(st,5);
  4615. c1 = (short)script_getnum(st,6);
  4616. c2 = (short)script_getnum(st,7);
  4617. c3 = (short)script_getnum(st,8);
  4618. c4 = (short)script_getnum(st,9);
  4619. if (nameid < 500) {
  4620. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4621. script_pushint(st,0);
  4622. return 1;
  4623. }
  4624. for(i = 0; i < MAX_INVENTORY; i++)
  4625. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4626. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4627. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4628. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4629. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4630. sd->status.inventory[i].card[3] == c4
  4631. )
  4632. count += sd->status.inventory[i].amount;
  4633. script_pushint(st,count);
  4634. return 0;
  4635. }
  4636. /*==========================================
  4637. * 重量チェック
  4638. *------------------------------------------*/
  4639. BUILDIN_FUNC(checkweight)
  4640. {
  4641. int nameid=0,amount,i;
  4642. unsigned long weight;
  4643. TBL_PC *sd;
  4644. struct script_data *data;
  4645. sd = script_rid2sd(st);
  4646. if( sd == NULL )
  4647. return 0;
  4648. data=script_getdata(st,2);
  4649. get_val(st,data);
  4650. if( data_isstring(data) ){
  4651. const char *name=conv_str(st,data);
  4652. struct item_data *item_data = itemdb_searchname(name);
  4653. if( item_data )
  4654. nameid=item_data->nameid;
  4655. }else
  4656. nameid=conv_num(st,data);
  4657. amount=script_getnum(st,3);
  4658. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4659. script_pushint(st,0);
  4660. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4661. return 1;
  4662. }
  4663. weight = itemdb_weight(nameid)*amount;
  4664. if( amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight )
  4665. script_pushint(st,0);
  4666. else if( itemdb_isstackable(nameid) )
  4667. {
  4668. if( (i = pc_search_inventory(sd,nameid)) >= 0 )
  4669. script_pushint(st,amount + sd->status.inventory[i].amount > MAX_AMOUNT ? 0 : 1);
  4670. else
  4671. script_pushint(st,pc_search_inventory(sd,0) >= 0 ? 1 : 0);
  4672. }
  4673. else
  4674. {
  4675. for( i = 0; i < MAX_INVENTORY && amount; ++i )
  4676. if( sd->status.inventory[i].nameid == 0 )
  4677. amount--;
  4678. script_pushint(st,amount ? 0 : 1);
  4679. }
  4680. return 0;
  4681. }
  4682. /*==========================================
  4683. * getitem <item id>,<amount>{,<character ID>};
  4684. * getitem "<item name>",<amount>{,<character ID>};
  4685. *------------------------------------------*/
  4686. BUILDIN_FUNC(getitem)
  4687. {
  4688. int nameid,amount,get_count,i,flag = 0;
  4689. struct item it;
  4690. TBL_PC *sd;
  4691. struct script_data *data;
  4692. data=script_getdata(st,2);
  4693. get_val(st,data);
  4694. if( data_isstring(data) )
  4695. {// "<item name>"
  4696. const char *name=conv_str(st,data);
  4697. struct item_data *item_data = itemdb_searchname(name);
  4698. if( item_data == NULL ){
  4699. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4700. return 1; //No item created.
  4701. }
  4702. nameid=item_data->nameid;
  4703. } else if( data_isint(data) )
  4704. {// <item id>
  4705. nameid=conv_num(st,data);
  4706. //Violet Box, Blue Box, etc - random item pick
  4707. if( nameid < 0 ) {
  4708. nameid=itemdb_searchrandomid(-nameid);
  4709. flag = 1;
  4710. }
  4711. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4712. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4713. return 1; //No item created.
  4714. }
  4715. } else {
  4716. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4717. return 1;
  4718. }
  4719. // <amount>
  4720. if( (amount=script_getnum(st,3)) <= 0)
  4721. return 0; //return if amount <=0, skip the useles iteration
  4722. memset(&it,0,sizeof(it));
  4723. it.nameid=nameid;
  4724. if(!flag)
  4725. it.identify=1;
  4726. else
  4727. it.identify=itemdb_isidentified(nameid);
  4728. if( script_hasdata(st,4) )
  4729. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4730. else
  4731. sd=script_rid2sd(st); // Attached player
  4732. if( sd == NULL ) // no target
  4733. return 0;
  4734. //Check if it's stackable.
  4735. if (!itemdb_isstackable(nameid))
  4736. get_count = 1;
  4737. else
  4738. get_count = amount;
  4739. for (i = 0; i < amount; i += get_count)
  4740. {
  4741. // if not pet egg
  4742. if (!pet_create_egg(sd, nameid))
  4743. {
  4744. if ((flag = pc_additem(sd, &it, get_count)))
  4745. {
  4746. clif_additem(sd, 0, 0, flag);
  4747. if( pc_candrop(sd,&it) )
  4748. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4749. }
  4750. }
  4751. }
  4752. //Logs items, got from (N)PC scripts [Lupus]
  4753. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4754. log_pick_pc(sd, "N", nameid, amount, NULL);
  4755. return 0;
  4756. }
  4757. /*==========================================
  4758. *
  4759. *------------------------------------------*/
  4760. BUILDIN_FUNC(getitem2)
  4761. {
  4762. int nameid,amount,get_count,i,flag = 0;
  4763. int iden,ref,attr,c1,c2,c3,c4;
  4764. struct item_data *item_data;
  4765. struct item item_tmp;
  4766. TBL_PC *sd;
  4767. struct script_data *data;
  4768. if( script_hasdata(st,11) )
  4769. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4770. else
  4771. sd=script_rid2sd(st); // Attached player
  4772. if( sd == NULL ) // no target
  4773. return 0;
  4774. data=script_getdata(st,2);
  4775. get_val(st,data);
  4776. if( data_isstring(data) ){
  4777. const char *name=conv_str(st,data);
  4778. struct item_data *item_data = itemdb_searchname(name);
  4779. if( item_data )
  4780. nameid=item_data->nameid;
  4781. else
  4782. nameid=UNKNOWN_ITEM_ID;
  4783. }else
  4784. nameid=conv_num(st,data);
  4785. amount=script_getnum(st,3);
  4786. iden=script_getnum(st,4);
  4787. ref=script_getnum(st,5);
  4788. attr=script_getnum(st,6);
  4789. c1=(short)script_getnum(st,7);
  4790. c2=(short)script_getnum(st,8);
  4791. c3=(short)script_getnum(st,9);
  4792. c4=(short)script_getnum(st,10);
  4793. if(nameid<0) { // ランダム
  4794. nameid=itemdb_searchrandomid(-nameid);
  4795. flag = 1;
  4796. }
  4797. if(nameid > 0) {
  4798. memset(&item_tmp,0,sizeof(item_tmp));
  4799. item_data=itemdb_exists(nameid);
  4800. if (item_data == NULL)
  4801. return -1;
  4802. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4803. if(ref > 10) ref = 10;
  4804. }
  4805. else if(item_data->type==IT_PETEGG) {
  4806. iden = 1;
  4807. ref = 0;
  4808. }
  4809. else {
  4810. iden = 1;
  4811. ref = attr = 0;
  4812. }
  4813. item_tmp.nameid=nameid;
  4814. if(!flag)
  4815. item_tmp.identify=iden;
  4816. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4817. item_tmp.identify=0;
  4818. item_tmp.refine=ref;
  4819. item_tmp.attribute=attr;
  4820. item_tmp.card[0]=(short)c1;
  4821. item_tmp.card[1]=(short)c2;
  4822. item_tmp.card[2]=(short)c3;
  4823. item_tmp.card[3]=(short)c4;
  4824. //Check if it's stackable.
  4825. if (!itemdb_isstackable(nameid))
  4826. get_count = 1;
  4827. else
  4828. get_count = amount;
  4829. for (i = 0; i < amount; i += get_count)
  4830. {
  4831. // if not pet egg
  4832. if (!pet_create_egg(sd, nameid))
  4833. {
  4834. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4835. {
  4836. clif_additem(sd, 0, 0, flag);
  4837. if( pc_candrop(sd,&item_tmp) )
  4838. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4839. }
  4840. }
  4841. }
  4842. //Logs items, got from (N)PC scripts [Lupus]
  4843. if(log_config.enable_logs&0x40)
  4844. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4845. }
  4846. return 0;
  4847. }
  4848. /*==========================================
  4849. * rentitem <item id>
  4850. * rentitem "<item name>"
  4851. *------------------------------------------*/
  4852. BUILDIN_FUNC(rentitem)
  4853. {
  4854. struct map_session_data *sd;
  4855. struct script_data *data;
  4856. struct item it;
  4857. int seconds;
  4858. int nameid = 0, flag;
  4859. data = script_getdata(st,2);
  4860. get_val(st,data);
  4861. if( (sd = script_rid2sd(st)) == NULL )
  4862. return 0;
  4863. if( data_isstring(data) )
  4864. {
  4865. const char *name = conv_str(st,data);
  4866. struct item_data *itd = itemdb_searchname(name);
  4867. if( itd == NULL )
  4868. {
  4869. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  4870. return 1;
  4871. }
  4872. nameid = itd->nameid;
  4873. }
  4874. else if( data_isint(data) )
  4875. {
  4876. nameid = conv_num(st,data);
  4877. if( nameid <= 0 || !itemdb_exists(nameid) )
  4878. {
  4879. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  4880. return 1;
  4881. }
  4882. }
  4883. else
  4884. {
  4885. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  4886. return 1;
  4887. }
  4888. seconds = script_getnum(st,3);
  4889. memset(&it, 0, sizeof(it));
  4890. it.nameid = nameid;
  4891. it.identify = 1;
  4892. it.expire_time = (unsigned int)(time(NULL) + seconds);
  4893. if( (flag = pc_additem(sd, &it, 1)) )
  4894. {
  4895. clif_additem(sd, 0, 0, flag);
  4896. return 1;
  4897. }
  4898. clif_rental_time(sd->fd, nameid, seconds);
  4899. pc_inventory_rental_add(sd, seconds);
  4900. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  4901. log_pick_pc(sd, "N", nameid, 1, NULL);
  4902. return 0;
  4903. }
  4904. /*==========================================
  4905. * gets an item with someone's name inscribed [Skotlex]
  4906. * getinscribeditem item_num, character_name
  4907. * Returned Qty is always 1, only works on equip-able
  4908. * equipment
  4909. *------------------------------------------*/
  4910. BUILDIN_FUNC(getnameditem)
  4911. {
  4912. int nameid;
  4913. struct item item_tmp;
  4914. TBL_PC *sd, *tsd;
  4915. struct script_data *data;
  4916. sd = script_rid2sd(st);
  4917. if (sd == NULL)
  4918. { //Player not attached!
  4919. script_pushint(st,0);
  4920. return 0;
  4921. }
  4922. data=script_getdata(st,2);
  4923. get_val(st,data);
  4924. if( data_isstring(data) ){
  4925. const char *name=conv_str(st,data);
  4926. struct item_data *item_data = itemdb_searchname(name);
  4927. if( item_data == NULL)
  4928. { //Failed
  4929. script_pushint(st,0);
  4930. return 0;
  4931. }
  4932. nameid = item_data->nameid;
  4933. }else
  4934. nameid = conv_num(st,data);
  4935. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4936. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4937. script_pushint(st,0);
  4938. return 0;
  4939. }
  4940. data=script_getdata(st,3);
  4941. get_val(st,data);
  4942. if( data_isstring(data) ) //Char Name
  4943. tsd=map_nick2sd(conv_str(st,data));
  4944. else //Char Id was given
  4945. tsd=map_charid2sd(conv_num(st,data));
  4946. if( tsd == NULL )
  4947. { //Failed
  4948. script_pushint(st,0);
  4949. return 0;
  4950. }
  4951. memset(&item_tmp,0,sizeof(item_tmp));
  4952. item_tmp.nameid=nameid;
  4953. item_tmp.amount=1;
  4954. item_tmp.identify=1;
  4955. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  4956. item_tmp.card[2]=tsd->status.char_id;
  4957. item_tmp.card[3]=tsd->status.char_id >> 16;
  4958. if(pc_additem(sd,&item_tmp,1)) {
  4959. script_pushint(st,0);
  4960. return 0; //Failed to add item, we will not drop if they don't fit
  4961. }
  4962. //Logs items, got from (N)PC scripts [Lupus]
  4963. if(log_config.enable_logs&0x40)
  4964. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  4965. script_pushint(st,1);
  4966. return 0;
  4967. }
  4968. /*==========================================
  4969. * gets a random item ID from an item group [Skotlex]
  4970. * groupranditem group_num
  4971. *------------------------------------------*/
  4972. BUILDIN_FUNC(grouprandomitem)
  4973. {
  4974. int group;
  4975. group = script_getnum(st,2);
  4976. script_pushint(st,itemdb_searchrandomid(group));
  4977. return 0;
  4978. }
  4979. /*==========================================
  4980. *
  4981. *------------------------------------------*/
  4982. BUILDIN_FUNC(makeitem)
  4983. {
  4984. int nameid,amount,flag = 0;
  4985. int x,y,m;
  4986. const char *mapname;
  4987. struct item item_tmp;
  4988. struct script_data *data;
  4989. data=script_getdata(st,2);
  4990. get_val(st,data);
  4991. if( data_isstring(data) ){
  4992. const char *name=conv_str(st,data);
  4993. struct item_data *item_data = itemdb_searchname(name);
  4994. if( item_data )
  4995. nameid=item_data->nameid;
  4996. else
  4997. nameid=UNKNOWN_ITEM_ID;
  4998. }else
  4999. nameid=conv_num(st,data);
  5000. amount=script_getnum(st,3);
  5001. mapname =script_getstr(st,4);
  5002. x =script_getnum(st,5);
  5003. y =script_getnum(st,6);
  5004. if(strcmp(mapname,"this")==0)
  5005. {
  5006. TBL_PC *sd;
  5007. sd = script_rid2sd(st);
  5008. if (!sd) return 0; //Failed...
  5009. m=sd->bl.m;
  5010. } else
  5011. m=map_mapname2mapid(mapname);
  5012. if(nameid<0) { // ランダム
  5013. nameid=itemdb_searchrandomid(-nameid);
  5014. flag = 1;
  5015. }
  5016. if(nameid > 0) {
  5017. memset(&item_tmp,0,sizeof(item_tmp));
  5018. item_tmp.nameid=nameid;
  5019. if(!flag)
  5020. item_tmp.identify=1;
  5021. else
  5022. item_tmp.identify=itemdb_isidentified(nameid);
  5023. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5024. }
  5025. return 0;
  5026. }
  5027. /// Deletes items from the target/attached player.
  5028. /// Prioritizes ordinary items.
  5029. ///
  5030. /// delitem <item id>,<amount>{,<account id>}
  5031. /// delitem "<item name>",<amount>{,<account id>}
  5032. BUILDIN_FUNC(delitem)
  5033. {
  5034. int nameid=0,amount,i,important_item=0;
  5035. TBL_PC *sd;
  5036. struct script_data *data;
  5037. if( script_hasdata(st,4) )
  5038. {
  5039. int account_id = script_getnum(st,4);
  5040. sd = map_id2sd(account_id); // <account id>
  5041. if( sd == NULL )
  5042. {
  5043. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5044. st->state = END;
  5045. return 1;
  5046. }
  5047. }
  5048. else
  5049. {
  5050. sd = script_rid2sd(st);// attached player
  5051. if( sd == NULL )
  5052. return 0;
  5053. }
  5054. data = script_getdata(st,2);
  5055. get_val(st,data);
  5056. if( data_isstring(data) )
  5057. {
  5058. const char* item_name = conv_str(st,data);
  5059. struct item_data* id = itemdb_searchname(item_name);
  5060. if( id == NULL )
  5061. {
  5062. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5063. st->state = END;
  5064. return 1;
  5065. }
  5066. nameid = id->nameid;// "<item name>"
  5067. }
  5068. else
  5069. nameid = conv_num(st,data);// <item id>
  5070. amount=script_getnum(st,3);
  5071. if( amount <= 0 )
  5072. return 0;// nothing to do
  5073. //1st pass
  5074. //here we won't delete items with CARDS, named items but we count them
  5075. for(i=0;i<MAX_INVENTORY;i++){
  5076. //we don't delete wrong item or equipped item
  5077. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5078. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5079. continue;
  5080. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5081. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5082. sd->status.inventory[i].card[0] == CARD0_PET)
  5083. {
  5084. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5085. continue; //pet couldn't be sent for deletion.
  5086. } else
  5087. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5088. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5089. sd->status.inventory[i].card[0] ||
  5090. sd->status.inventory[i].refine) {
  5091. //this is important item, count it (except for pet eggs)
  5092. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5093. important_item++;
  5094. continue;
  5095. }
  5096. if(sd->status.inventory[i].amount>=amount){
  5097. //Logs items, got from (N)PC scripts [Lupus]
  5098. if(log_config.enable_logs&0x40)
  5099. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5100. pc_delitem(sd,i,amount,0);
  5101. return 0; //we deleted exact amount of items. now exit
  5102. } else {
  5103. amount-=sd->status.inventory[i].amount;
  5104. //Logs items, got from (N)PC scripts [Lupus]
  5105. if(log_config.enable_logs&0x40) {
  5106. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5107. }
  5108. //Logs
  5109. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5110. }
  5111. }
  5112. //2nd pass
  5113. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5114. if (important_item>0 && amount>0)
  5115. for(i=0;i<MAX_INVENTORY;i++){
  5116. //we don't delete wrong item
  5117. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5118. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5119. continue;
  5120. if(sd->status.inventory[i].amount>=amount){
  5121. //Logs items, got from (N)PC scripts [Lupus]
  5122. if(log_config.enable_logs&0x40)
  5123. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5124. pc_delitem(sd,i,amount,0);
  5125. return 0; //we deleted exact amount of items. now exit
  5126. } else {
  5127. amount-=sd->status.inventory[i].amount;
  5128. //Logs items, got from (N)PC scripts [Lupus]
  5129. if(log_config.enable_logs&0x40)
  5130. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5131. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5132. }
  5133. }
  5134. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5135. st->state = END;
  5136. return 1;
  5137. }
  5138. /// Deletes items from the target/attached player.
  5139. ///
  5140. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5141. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5142. BUILDIN_FUNC(delitem2)
  5143. {
  5144. int nameid=0,amount,i=0;
  5145. int iden,ref,attr,c1,c2,c3,c4;
  5146. TBL_PC *sd;
  5147. struct script_data *data;
  5148. if( script_hasdata(st,11) )
  5149. {
  5150. int account_id = script_getnum(st,11);
  5151. sd = map_id2sd(account_id); // <account id>
  5152. if( sd == NULL )
  5153. {
  5154. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5155. st->state = END;
  5156. return 1;
  5157. }
  5158. }
  5159. else
  5160. {
  5161. sd = script_rid2sd(st);// attached player
  5162. if( sd == NULL )
  5163. return 0;
  5164. }
  5165. data = script_getdata(st,2);
  5166. get_val(st,data);
  5167. if( data_isstring(data) )
  5168. {
  5169. const char* item_name = conv_str(st,data);
  5170. struct item_data* id = itemdb_searchname(item_name);
  5171. if( id == NULL )
  5172. {
  5173. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5174. st->state = END;
  5175. return 1;
  5176. }
  5177. nameid = id->nameid;// "<item name>"
  5178. }
  5179. else
  5180. nameid = conv_num(st,data);// <item id>
  5181. amount=script_getnum(st,3);
  5182. iden=script_getnum(st,4);
  5183. ref=script_getnum(st,5);
  5184. attr=script_getnum(st,6);
  5185. c1=(short)script_getnum(st,7);
  5186. c2=(short)script_getnum(st,8);
  5187. c3=(short)script_getnum(st,9);
  5188. c4=(short)script_getnum(st,10);
  5189. if( amount <= 0 )
  5190. return 0;// nothing to do
  5191. for(i=0;i<MAX_INVENTORY;i++){
  5192. //we don't delete wrong item or equipped item
  5193. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5194. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5195. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5196. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5197. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5198. sd->status.inventory[i].card[3]!=c4)
  5199. continue;
  5200. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5201. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5202. {
  5203. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5204. continue; //Failed to send delete the pet.
  5205. }
  5206. if(sd->status.inventory[i].amount>=amount){
  5207. //Logs items, got from (N)PC scripts [Lupus]
  5208. if(log_config.enable_logs&0x40)
  5209. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5210. pc_delitem(sd,i,amount,0);
  5211. return 0; //we deleted exact amount of items. now exit
  5212. } else {
  5213. amount-=sd->status.inventory[i].amount;
  5214. //Logs items, got from (N)PC scripts [Lupus]
  5215. if(log_config.enable_logs&0x40)
  5216. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5217. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5218. }
  5219. }
  5220. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5221. st->state = END;
  5222. return 1;
  5223. }
  5224. /*==========================================
  5225. * Enables/Disables use of items while in an NPC [Skotlex]
  5226. *------------------------------------------*/
  5227. BUILDIN_FUNC(enableitemuse)
  5228. {
  5229. TBL_PC *sd;
  5230. sd=script_rid2sd(st);
  5231. if (sd)
  5232. sd->npc_item_flag = st->oid;
  5233. return 0;
  5234. }
  5235. BUILDIN_FUNC(disableitemuse)
  5236. {
  5237. TBL_PC *sd;
  5238. sd=script_rid2sd(st);
  5239. if (sd)
  5240. sd->npc_item_flag = 0;
  5241. return 0;
  5242. }
  5243. /*==========================================
  5244. *キャラ関係のパラメータ取得
  5245. *------------------------------------------*/
  5246. BUILDIN_FUNC(readparam)
  5247. {
  5248. int type;
  5249. TBL_PC *sd;
  5250. type=script_getnum(st,2);
  5251. if( script_hasdata(st,3) )
  5252. sd=map_nick2sd(script_getstr(st,3));
  5253. else
  5254. sd=script_rid2sd(st);
  5255. if(sd==NULL){
  5256. script_pushint(st,-1);
  5257. return 0;
  5258. }
  5259. script_pushint(st,pc_readparam(sd,type));
  5260. return 0;
  5261. }
  5262. /*==========================================
  5263. *キャラ関係のID取得
  5264. *------------------------------------------*/
  5265. BUILDIN_FUNC(getcharid)
  5266. {
  5267. int num;
  5268. TBL_PC *sd;
  5269. num = script_getnum(st,2);
  5270. if( script_hasdata(st,3) )
  5271. sd=map_nick2sd(script_getstr(st,3));
  5272. else
  5273. sd=script_rid2sd(st);
  5274. if(sd==NULL){
  5275. script_pushint(st,0); //return 0, according docs
  5276. return 0;
  5277. }
  5278. switch( num ) {
  5279. case 0: script_pushint(st,sd->status.char_id); break;
  5280. case 1: script_pushint(st,sd->status.party_id); break;
  5281. case 2: script_pushint(st,sd->status.guild_id); break;
  5282. case 3: script_pushint(st,sd->status.account_id); break;
  5283. case 4: script_pushint(st,sd->state.bg_id); break;
  5284. default:
  5285. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5286. script_pushint(st,0);
  5287. break;
  5288. }
  5289. return 0;
  5290. }
  5291. /*==========================================
  5292. *指定IDのPT名取得
  5293. *------------------------------------------*/
  5294. char *buildin_getpartyname_sub(int party_id)
  5295. {
  5296. struct party_data *p;
  5297. p=party_search(party_id);
  5298. if(p!=NULL){
  5299. char *buf;
  5300. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5301. memcpy(buf, p->party.name, NAME_LENGTH);
  5302. buf[NAME_LENGTH-1] = '\0';
  5303. return buf;
  5304. }
  5305. return 0;
  5306. }
  5307. BUILDIN_FUNC(getpartyname)
  5308. {
  5309. char *name;
  5310. int party_id;
  5311. party_id=script_getnum(st,2);
  5312. name=buildin_getpartyname_sub(party_id);
  5313. if(name != NULL)
  5314. script_pushstr(st,name);
  5315. else
  5316. script_pushconststr(st,"null");
  5317. return 0;
  5318. }
  5319. /*==========================================
  5320. *指定IDのPT人数とメンバーID取得
  5321. *------------------------------------------*/
  5322. BUILDIN_FUNC(getpartymember)
  5323. {
  5324. struct party_data *p;
  5325. int i,j=0,type=0;
  5326. p=party_search(script_getnum(st,2));
  5327. if( script_hasdata(st,3) )
  5328. type=script_getnum(st,3);
  5329. if(p!=NULL){
  5330. for(i=0;i<MAX_PARTY;i++){
  5331. if(p->party.member[i].account_id){
  5332. switch (type) {
  5333. case 2:
  5334. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5335. break;
  5336. case 1:
  5337. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5338. break;
  5339. default:
  5340. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5341. }
  5342. j++;
  5343. }
  5344. }
  5345. }
  5346. mapreg_setreg(add_str("$@partymembercount"),j);
  5347. return 0;
  5348. }
  5349. /*==========================================
  5350. * Retrieves party leader. if flag is specified,
  5351. * return some of the leader data. Otherwise, return name.
  5352. *------------------------------------------*/
  5353. BUILDIN_FUNC(getpartyleader)
  5354. {
  5355. int party_id, type = 0, i=0;
  5356. struct party_data *p;
  5357. party_id=script_getnum(st,2);
  5358. if( script_hasdata(st,3) )
  5359. type=script_getnum(st,3);
  5360. p=party_search(party_id);
  5361. if (p) //Search leader
  5362. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5363. if (!p || i == MAX_PARTY) { //leader not found
  5364. if (type)
  5365. script_pushint(st,-1);
  5366. else
  5367. script_pushconststr(st,"null");
  5368. return 0;
  5369. }
  5370. switch (type) {
  5371. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5372. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5373. case 3: script_pushint(st,p->party.member[i].class_); break;
  5374. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5375. case 5: script_pushint(st,p->party.member[i].lv); break;
  5376. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5377. }
  5378. return 0;
  5379. }
  5380. /*==========================================
  5381. *指定IDのギルド名取得
  5382. *------------------------------------------*/
  5383. char *buildin_getguildname_sub(int guild_id)
  5384. {
  5385. struct guild *g=NULL;
  5386. g=guild_search(guild_id);
  5387. if(g!=NULL){
  5388. char *buf;
  5389. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5390. memcpy(buf, g->name, NAME_LENGTH);
  5391. buf[NAME_LENGTH-1] = '\0';
  5392. return buf;
  5393. }
  5394. return NULL;
  5395. }
  5396. BUILDIN_FUNC(getguildname)
  5397. {
  5398. char *name;
  5399. int guild_id=script_getnum(st,2);
  5400. name=buildin_getguildname_sub(guild_id);
  5401. if(name != NULL)
  5402. script_pushstr(st,name);
  5403. else
  5404. script_pushconststr(st,"null");
  5405. return 0;
  5406. }
  5407. /*==========================================
  5408. *指定IDのGuildMaster名取得
  5409. *------------------------------------------*/
  5410. char *buildin_getguildmaster_sub(int guild_id)
  5411. {
  5412. struct guild *g=NULL;
  5413. g=guild_search(guild_id);
  5414. if(g!=NULL){
  5415. char *buf;
  5416. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5417. memcpy(buf, g->master, NAME_LENGTH);
  5418. buf[NAME_LENGTH-1] = '\0';
  5419. return buf;
  5420. }
  5421. return 0;
  5422. }
  5423. BUILDIN_FUNC(getguildmaster)
  5424. {
  5425. char *master;
  5426. int guild_id=script_getnum(st,2);
  5427. master=buildin_getguildmaster_sub(guild_id);
  5428. if(master!=0)
  5429. script_pushstr(st,master);
  5430. else
  5431. script_pushconststr(st,"null");
  5432. return 0;
  5433. }
  5434. BUILDIN_FUNC(getguildmasterid)
  5435. {
  5436. char *master;
  5437. TBL_PC *sd=NULL;
  5438. int guild_id=script_getnum(st,2);
  5439. master=buildin_getguildmaster_sub(guild_id);
  5440. if(master!=0){
  5441. if((sd=map_nick2sd(master)) == NULL){
  5442. script_pushint(st,0);
  5443. return 0;
  5444. }
  5445. script_pushint(st,sd->status.char_id);
  5446. }else{
  5447. script_pushint(st,0);
  5448. }
  5449. return 0;
  5450. }
  5451. /*==========================================
  5452. * キャラクタの名前
  5453. *------------------------------------------*/
  5454. BUILDIN_FUNC(strcharinfo)
  5455. {
  5456. TBL_PC *sd;
  5457. int num;
  5458. char *buf;
  5459. sd=script_rid2sd(st);
  5460. if (!sd) { //Avoid crashing....
  5461. script_pushconststr(st,"");
  5462. return 0;
  5463. }
  5464. num=script_getnum(st,2);
  5465. switch(num){
  5466. case 0:
  5467. script_pushstrcopy(st,sd->status.name);
  5468. break;
  5469. case 1:
  5470. buf=buildin_getpartyname_sub(sd->status.party_id);
  5471. if(buf!=0)
  5472. script_pushstr(st,buf);
  5473. else
  5474. script_pushconststr(st,"");
  5475. break;
  5476. case 2:
  5477. buf=buildin_getguildname_sub(sd->status.guild_id);
  5478. if(buf != NULL)
  5479. script_pushstr(st,buf);
  5480. else
  5481. script_pushconststr(st,"");
  5482. break;
  5483. case 3:
  5484. script_pushconststr(st,map[sd->bl.m].name);
  5485. break;
  5486. default:
  5487. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5488. script_pushconststr(st,"");
  5489. break;
  5490. }
  5491. return 0;
  5492. }
  5493. /*==========================================
  5494. * 呼び出し元のNPC情報を取得する
  5495. *------------------------------------------*/
  5496. BUILDIN_FUNC(strnpcinfo)
  5497. {
  5498. TBL_NPC* nd;
  5499. int num;
  5500. char *buf,*name=NULL;
  5501. nd = map_id2nd(st->oid);
  5502. if (!nd) {
  5503. script_pushconststr(st, "");
  5504. return 0;
  5505. }
  5506. num = script_getnum(st,2);
  5507. switch(num){
  5508. case 0: // display name
  5509. name = aStrdup(nd->name);
  5510. break;
  5511. case 1: // visible part of display name name
  5512. if((buf = strchr(nd->name,'#')) != NULL)
  5513. {
  5514. name = aStrdup(nd->name);
  5515. name[buf - nd->name] = 0;
  5516. }
  5517. break;
  5518. case 2: // # fragment
  5519. if((buf = strchr(nd->name,'#')) != NULL)
  5520. name = aStrdup(buf+1);
  5521. break;
  5522. case 3: // unique name
  5523. name = aStrdup(nd->exname);
  5524. break;
  5525. }
  5526. if(name)
  5527. script_pushstr(st, name);
  5528. else
  5529. script_pushconststr(st, "");
  5530. return 0;
  5531. }
  5532. // aegis->athena slot position conversion table
  5533. 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};
  5534. /*==========================================
  5535. * GetEquipID(Pos); Pos: 1-10
  5536. *------------------------------------------*/
  5537. BUILDIN_FUNC(getequipid)
  5538. {
  5539. int i, num;
  5540. TBL_PC* sd;
  5541. struct item_data* item;
  5542. sd = script_rid2sd(st);
  5543. if( sd == NULL )
  5544. return 0;
  5545. num = script_getnum(st,2) - 1;
  5546. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5547. {
  5548. script_pushint(st,-1);
  5549. return 0;
  5550. }
  5551. // get inventory position of item
  5552. i = pc_checkequip(sd,equip[num]);
  5553. if( i < 0 )
  5554. {
  5555. script_pushint(st,-1);
  5556. return 0;
  5557. }
  5558. item = sd->inventory_data[i];
  5559. if( item != 0 )
  5560. script_pushint(st,item->nameid);
  5561. else
  5562. script_pushint(st,0);
  5563. return 0;
  5564. }
  5565. /*==========================================
  5566. * 装備名文字列(精錬メニュー用)
  5567. *------------------------------------------*/
  5568. BUILDIN_FUNC(getequipname)
  5569. {
  5570. int i, num;
  5571. TBL_PC* sd;
  5572. struct item_data* item;
  5573. sd = script_rid2sd(st);
  5574. if( sd == NULL )
  5575. return 0;
  5576. num = script_getnum(st,2) - 1;
  5577. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5578. {
  5579. script_pushconststr(st,"");
  5580. return 0;
  5581. }
  5582. // get inventory position of item
  5583. i = pc_checkequip(sd,equip[num]);
  5584. if( i < 0 )
  5585. {
  5586. script_pushint(st,-1);
  5587. return 0;
  5588. }
  5589. item = sd->inventory_data[i];
  5590. if( item != 0 )
  5591. script_pushstrcopy(st,item->jname);
  5592. else
  5593. script_pushconststr(st,"");
  5594. return 0;
  5595. }
  5596. /*==========================================
  5597. * getbrokenid [Valaris]
  5598. *------------------------------------------*/
  5599. BUILDIN_FUNC(getbrokenid)
  5600. {
  5601. int i,num,id=0,brokencounter=0;
  5602. TBL_PC *sd;
  5603. sd = script_rid2sd(st);
  5604. if( sd == NULL )
  5605. return 0;
  5606. num=script_getnum(st,2);
  5607. for(i=0; i<MAX_INVENTORY; i++) {
  5608. if(sd->status.inventory[i].attribute){
  5609. brokencounter++;
  5610. if(num==brokencounter){
  5611. id=sd->status.inventory[i].nameid;
  5612. break;
  5613. }
  5614. }
  5615. }
  5616. script_pushint(st,id);
  5617. return 0;
  5618. }
  5619. /*==========================================
  5620. * repair [Valaris]
  5621. *------------------------------------------*/
  5622. BUILDIN_FUNC(repair)
  5623. {
  5624. int i,num;
  5625. int repaircounter=0;
  5626. TBL_PC *sd;
  5627. sd = script_rid2sd(st);
  5628. if( sd == NULL )
  5629. return 0;
  5630. num=script_getnum(st,2);
  5631. for(i=0; i<MAX_INVENTORY; i++) {
  5632. if(sd->status.inventory[i].attribute){
  5633. repaircounter++;
  5634. if(num==repaircounter){
  5635. sd->status.inventory[i].attribute=0;
  5636. clif_equiplist(sd);
  5637. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5638. clif_misceffect(&sd->bl, 3);
  5639. break;
  5640. }
  5641. }
  5642. }
  5643. return 0;
  5644. }
  5645. /*==========================================
  5646. * 装備チェック
  5647. *------------------------------------------*/
  5648. BUILDIN_FUNC(getequipisequiped)
  5649. {
  5650. int i=-1,num;
  5651. TBL_PC *sd;
  5652. num=script_getnum(st,2);
  5653. sd = script_rid2sd(st);
  5654. if( sd == NULL )
  5655. return 0;
  5656. if (num > 0 && num <= ARRAYLENGTH(equip))
  5657. i=pc_checkequip(sd,equip[num-1]);
  5658. if(i >= 0)
  5659. script_pushint(st,1);
  5660. else
  5661. script_pushint(st,0);
  5662. return 0;
  5663. }
  5664. /*==========================================
  5665. * 装備品精錬可能チェック
  5666. *------------------------------------------*/
  5667. BUILDIN_FUNC(getequipisenableref)
  5668. {
  5669. int i=-1,num;
  5670. TBL_PC *sd;
  5671. num=script_getnum(st,2);
  5672. sd = script_rid2sd(st);
  5673. if( sd == NULL )
  5674. return 0;
  5675. if( num > 0 && num <= ARRAYLENGTH(equip) )
  5676. i = pc_checkequip(sd,equip[num-1]);
  5677. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  5678. script_pushint(st,1);
  5679. else
  5680. script_pushint(st,0);
  5681. return 0;
  5682. }
  5683. /*==========================================
  5684. * 装備品鑑定チェック
  5685. *------------------------------------------*/
  5686. BUILDIN_FUNC(getequipisidentify)
  5687. {
  5688. int i=-1,num;
  5689. TBL_PC *sd;
  5690. num=script_getnum(st,2);
  5691. sd = script_rid2sd(st);
  5692. if( sd == NULL )
  5693. return 0;
  5694. if (num > 0 && num <= ARRAYLENGTH(equip))
  5695. i=pc_checkequip(sd,equip[num-1]);
  5696. if(i >= 0)
  5697. script_pushint(st,sd->status.inventory[i].identify);
  5698. else
  5699. script_pushint(st,0);
  5700. return 0;
  5701. }
  5702. /*==========================================
  5703. * 装備品精錬度
  5704. *------------------------------------------*/
  5705. BUILDIN_FUNC(getequiprefinerycnt)
  5706. {
  5707. int i=-1,num;
  5708. TBL_PC *sd;
  5709. num=script_getnum(st,2);
  5710. sd = script_rid2sd(st);
  5711. if( sd == NULL )
  5712. return 0;
  5713. if (num > 0 && num <= ARRAYLENGTH(equip))
  5714. i=pc_checkequip(sd,equip[num-1]);
  5715. if(i >= 0)
  5716. script_pushint(st,sd->status.inventory[i].refine);
  5717. else
  5718. script_pushint(st,0);
  5719. return 0;
  5720. }
  5721. /*==========================================
  5722. * 装備品武器LV
  5723. *------------------------------------------*/
  5724. BUILDIN_FUNC(getequipweaponlv)
  5725. {
  5726. int i=-1,num;
  5727. TBL_PC *sd;
  5728. num=script_getnum(st,2);
  5729. sd = script_rid2sd(st);
  5730. if( sd == NULL )
  5731. return 0;
  5732. if (num > 0 && num <= ARRAYLENGTH(equip))
  5733. i=pc_checkequip(sd,equip[num-1]);
  5734. if(i >= 0 && sd->inventory_data[i])
  5735. script_pushint(st,sd->inventory_data[i]->wlv);
  5736. else
  5737. script_pushint(st,0);
  5738. return 0;
  5739. }
  5740. /*==========================================
  5741. * 装備品精錬成功率
  5742. *------------------------------------------*/
  5743. BUILDIN_FUNC(getequippercentrefinery)
  5744. {
  5745. int i=-1,num;
  5746. TBL_PC *sd;
  5747. num=script_getnum(st,2);
  5748. sd = script_rid2sd(st);
  5749. if( sd == NULL )
  5750. return 0;
  5751. if (num > 0 && num <= ARRAYLENGTH(equip))
  5752. i=pc_checkequip(sd,equip[num-1]);
  5753. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5754. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5755. else
  5756. script_pushint(st,0);
  5757. return 0;
  5758. }
  5759. /*==========================================
  5760. * 精錬成功
  5761. *------------------------------------------*/
  5762. BUILDIN_FUNC(successrefitem)
  5763. {
  5764. int i=-1,num,ep;
  5765. TBL_PC *sd;
  5766. num=script_getnum(st,2);
  5767. sd = script_rid2sd(st);
  5768. if( sd == NULL )
  5769. return 0;
  5770. if (num > 0 && num <= ARRAYLENGTH(equip))
  5771. i=pc_checkequip(sd,equip[num-1]);
  5772. if(i >= 0) {
  5773. ep=sd->status.inventory[i].equip;
  5774. //Logs items, got from (N)PC scripts [Lupus]
  5775. if(log_config.enable_logs&0x40)
  5776. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5777. sd->status.inventory[i].refine++;
  5778. pc_unequipitem(sd,i,2|4); // status calc will happen in pc_equipitem() below
  5779. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5780. clif_delitem(sd,i,1);
  5781. //Logs items, got from (N)PC scripts [Lupus]
  5782. if(log_config.enable_logs&0x40)
  5783. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5784. clif_additem(sd,i,1,0);
  5785. pc_equipitem(sd,i,ep);
  5786. clif_misceffect(&sd->bl,3);
  5787. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5788. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5789. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5790. ){ // Fame point system [DracoRPG]
  5791. switch (sd->inventory_data[i]->wlv){
  5792. case 1:
  5793. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5794. break;
  5795. case 2:
  5796. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5797. break;
  5798. case 3:
  5799. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5800. break;
  5801. }
  5802. }
  5803. }
  5804. return 0;
  5805. }
  5806. /*==========================================
  5807. * 精錬失敗
  5808. *------------------------------------------*/
  5809. BUILDIN_FUNC(failedrefitem)
  5810. {
  5811. int i=-1,num;
  5812. TBL_PC *sd;
  5813. num=script_getnum(st,2);
  5814. sd = script_rid2sd(st);
  5815. if( sd == NULL )
  5816. return 0;
  5817. if (num > 0 && num <= ARRAYLENGTH(equip))
  5818. i=pc_checkequip(sd,equip[num-1]);
  5819. if(i >= 0) {
  5820. //Logs items, got from (N)PC scripts [Lupus]
  5821. if(log_config.enable_logs&0x40)
  5822. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5823. sd->status.inventory[i].refine = 0;
  5824. pc_unequipitem(sd,i,3);
  5825. // 精錬失敗エフェクトのパケット
  5826. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5827. pc_delitem(sd,i,1,0);
  5828. // 他の人にも失敗を通知
  5829. clif_misceffect(&sd->bl,2);
  5830. }
  5831. return 0;
  5832. }
  5833. /*==========================================
  5834. *
  5835. *------------------------------------------*/
  5836. BUILDIN_FUNC(statusup)
  5837. {
  5838. int type;
  5839. TBL_PC *sd;
  5840. type=script_getnum(st,2);
  5841. sd = script_rid2sd(st);
  5842. if( sd == NULL )
  5843. return 0;
  5844. pc_statusup(sd,type);
  5845. return 0;
  5846. }
  5847. /*==========================================
  5848. *
  5849. *------------------------------------------*/
  5850. BUILDIN_FUNC(statusup2)
  5851. {
  5852. int type,val;
  5853. TBL_PC *sd;
  5854. type=script_getnum(st,2);
  5855. val=script_getnum(st,3);
  5856. sd = script_rid2sd(st);
  5857. if( sd == NULL )
  5858. return 0;
  5859. pc_statusup2(sd,type,val);
  5860. return 0;
  5861. }
  5862. /// See 'doc/item_bonus.txt'
  5863. ///
  5864. /// bonus <bonus type>,<val1>;
  5865. /// bonus2 <bonus type>,<val1>,<val2>;
  5866. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5867. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5868. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5869. BUILDIN_FUNC(bonus)
  5870. {
  5871. int type;
  5872. int val1;
  5873. int val2 = 0;
  5874. int val3 = 0;
  5875. int val4 = 0;
  5876. int val5 = 0;
  5877. TBL_PC* sd;
  5878. sd = script_rid2sd(st);
  5879. if( sd == NULL )
  5880. return 0; // no player attached
  5881. type = script_getnum(st,2);
  5882. switch( type )
  5883. {
  5884. case SP_AUTOSPELL:
  5885. case SP_AUTOSPELL_WHENHIT:
  5886. case SP_AUTOSPELL_ONSKILL:
  5887. case SP_SKILL_ATK:
  5888. case SP_SKILL_HEAL:
  5889. case SP_SKILL_HEAL2:
  5890. case SP_ADD_SKILL_BLOW:
  5891. case SP_CASTRATE:
  5892. case SP_ADDEFF_ONSKILL:
  5893. // these bonuses support skill names
  5894. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5895. break;
  5896. default:
  5897. val1 = script_getnum(st,3);
  5898. break;
  5899. }
  5900. switch( script_lastdata(st)-2 )
  5901. {
  5902. case 1:
  5903. pc_bonus(sd, type, val1);
  5904. break;
  5905. case 2:
  5906. val2 = script_getnum(st,4);
  5907. pc_bonus2(sd, type, val1, val2);
  5908. break;
  5909. case 3:
  5910. val2 = script_getnum(st,4);
  5911. val3 = script_getnum(st,5);
  5912. pc_bonus3(sd, type, val1, val2, val3);
  5913. break;
  5914. case 4:
  5915. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  5916. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  5917. else
  5918. val2 = script_getnum(st,4);
  5919. val3 = script_getnum(st,5);
  5920. val4 = script_getnum(st,6);
  5921. pc_bonus4(sd, type, val1, val2, val3, val4);
  5922. break;
  5923. case 5:
  5924. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  5925. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  5926. else
  5927. val2 = script_getnum(st,4);
  5928. val3 = script_getnum(st,5);
  5929. val4 = script_getnum(st,6);
  5930. val5 = script_getnum(st,7);
  5931. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  5932. break;
  5933. default:
  5934. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  5935. break;
  5936. }
  5937. return 0;
  5938. }
  5939. BUILDIN_FUNC(autobonus)
  5940. {
  5941. unsigned int dur;
  5942. short rate;
  5943. short atk_type = 0;
  5944. TBL_PC* sd;
  5945. struct script_code *bonus_script;
  5946. struct script_code *other_script = NULL;
  5947. sd = script_rid2sd(st);
  5948. if( sd == NULL )
  5949. return 0; // no player attached
  5950. if( sd->state.autocast )
  5951. return 0;
  5952. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  5953. return 0;
  5954. if( sd->state.script_parsed&sd->status.inventory[current_equip_item_index].equip )
  5955. return 0;
  5956. rate = script_getnum(st,3);
  5957. dur = script_getnum(st,4);
  5958. if( !rate || !dur )
  5959. return 0;
  5960. bonus_script = parse_script(script_getstr(st,2), "autobonus bonus", 0, 0);
  5961. if( !bonus_script )
  5962. return 0;
  5963. if( script_hasdata(st,5) )
  5964. atk_type = script_getnum(st,5);
  5965. if( script_hasdata(st,6) )
  5966. other_script = parse_script(script_getstr(st,6), "autobonus other", 0, 0);
  5967. if( !pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  5968. {
  5969. if( bonus_script )
  5970. script_free_code(bonus_script);
  5971. if( other_script )
  5972. script_free_code(other_script);
  5973. }
  5974. return 0;
  5975. }
  5976. BUILDIN_FUNC(autobonus2)
  5977. {
  5978. unsigned int dur;
  5979. short rate;
  5980. short atk_type = 0;
  5981. TBL_PC* sd;
  5982. struct script_code *bonus_script;
  5983. struct script_code *other_script = NULL;
  5984. sd = script_rid2sd(st);
  5985. if( sd == NULL )
  5986. return 0; // no player attached
  5987. if( sd->state.autocast )
  5988. return 0;
  5989. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  5990. return 0;
  5991. if( sd->state.script_parsed&sd->status.inventory[current_equip_item_index].equip )
  5992. return 0;
  5993. rate = script_getnum(st,3);
  5994. dur = script_getnum(st,4);
  5995. if( !rate || !dur )
  5996. return 0;
  5997. bonus_script = parse_script(script_getstr(st,2), "autobonus2 bonus", 0, 0);
  5998. if( !bonus_script )
  5999. return 0;
  6000. if( script_hasdata(st,5) )
  6001. atk_type = script_getnum(st,5);
  6002. if( script_hasdata(st,6) )
  6003. other_script = parse_script(script_getstr(st,6), "autobonus2 other", 0, 0);
  6004. if( !pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6005. {
  6006. if( bonus_script )
  6007. script_free_code(bonus_script);
  6008. if( other_script )
  6009. script_free_code(other_script);
  6010. }
  6011. return 0;
  6012. }
  6013. BUILDIN_FUNC(autobonus3)
  6014. {
  6015. unsigned int dur;
  6016. short rate,atk_type;
  6017. TBL_PC* sd;
  6018. struct script_code *bonus_script;
  6019. struct script_code *other_script = NULL;
  6020. sd = script_rid2sd(st);
  6021. if( sd == NULL )
  6022. return 0; // no player attached
  6023. if( sd->state.autocast )
  6024. return 0;
  6025. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6026. return 0;
  6027. if( sd->state.script_parsed&sd->status.inventory[current_equip_item_index].equip )
  6028. return 0;
  6029. rate = script_getnum(st,3);
  6030. dur = script_getnum(st,4);
  6031. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6032. if( !rate || !dur || !atk_type )
  6033. return 0;
  6034. bonus_script = parse_script(script_getstr(st,2), "autobonus3 bonus", 0, 0);
  6035. if( !bonus_script )
  6036. return 0;
  6037. if( script_hasdata(st,6) )
  6038. other_script = parse_script(script_getstr(st,6), "autobonus3 other", 0, 0);
  6039. if( !pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6040. {
  6041. if( bonus_script )
  6042. script_free_code(bonus_script);
  6043. if( other_script )
  6044. script_free_code(other_script);
  6045. }
  6046. return 0;
  6047. }
  6048. /// Changes the level of a player skill.
  6049. /// <flag> defaults to 1
  6050. /// <flag>=0 : set the level of the skill
  6051. /// <flag>=1 : set the temporary level of the skill
  6052. /// <flag>=2 : add to the level of the skill
  6053. ///
  6054. /// skill <skill id>,<level>,<flag>
  6055. /// skill <skill id>,<level>
  6056. /// skill "<skill name>",<level>,<flag>
  6057. /// skill "<skill name>",<level>
  6058. BUILDIN_FUNC(skill)
  6059. {
  6060. int id;
  6061. int level;
  6062. int flag = 1;
  6063. TBL_PC* sd;
  6064. sd = script_rid2sd(st);
  6065. if( sd == NULL )
  6066. return 0;// no player attached, report source
  6067. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6068. level = script_getnum(st,3);
  6069. if( script_hasdata(st,4) )
  6070. flag = script_getnum(st,4);
  6071. pc_skill(sd, id, level, flag);
  6072. return 0;
  6073. }
  6074. /// Changes the level of a player skill.
  6075. /// like skill, but <flag> defaults to 2
  6076. ///
  6077. /// addtoskill <skill id>,<amount>,<flag>
  6078. /// addtoskill <skill id>,<amount>
  6079. /// addtoskill "<skill name>",<amount>,<flag>
  6080. /// addtoskill "<skill name>",<amount>
  6081. ///
  6082. /// @see skill
  6083. BUILDIN_FUNC(addtoskill)
  6084. {
  6085. int id;
  6086. int level;
  6087. int flag = 2;
  6088. TBL_PC* sd;
  6089. sd = script_rid2sd(st);
  6090. if( sd == NULL )
  6091. return 0;// no player attached, report source
  6092. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6093. level = script_getnum(st,3);
  6094. if( script_hasdata(st,4) )
  6095. flag = script_getnum(st,4);
  6096. pc_skill(sd, id, level, flag);
  6097. return 0;
  6098. }
  6099. /// Increases the level of a guild skill.
  6100. ///
  6101. /// guildskill <skill id>,<amount>;
  6102. /// guildskill "<skill name>",<amount>;
  6103. BUILDIN_FUNC(guildskill)
  6104. {
  6105. int id;
  6106. int level;
  6107. TBL_PC* sd;
  6108. int i;
  6109. sd = script_rid2sd(st);
  6110. if( sd == NULL )
  6111. return 0;// no player attached, report source
  6112. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6113. level = script_getnum(st,3);
  6114. for( i=0; i < level; i++ )
  6115. guild_skillup(sd, id);
  6116. return 0;
  6117. }
  6118. /// Returns the level of the player skill.
  6119. ///
  6120. /// getskilllv(<skill id>) -> <level>
  6121. /// getskilllv("<skill name>") -> <level>
  6122. BUILDIN_FUNC(getskilllv)
  6123. {
  6124. int id;
  6125. TBL_PC* sd;
  6126. sd = script_rid2sd(st);
  6127. if( sd == NULL )
  6128. return 0;// no player attached, report source
  6129. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6130. script_pushint(st, pc_checkskill(sd,id));
  6131. return 0;
  6132. }
  6133. /// Returns the level of the guild skill.
  6134. ///
  6135. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6136. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6137. BUILDIN_FUNC(getgdskilllv)
  6138. {
  6139. int guild_id;
  6140. int skill_id;
  6141. struct guild* g;
  6142. guild_id = script_getnum(st,2);
  6143. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6144. g = guild_search(guild_id);
  6145. if( g == NULL )
  6146. script_pushint(st, -1);
  6147. else
  6148. script_pushint(st, guild_checkskill(g,skill_id));
  6149. return 0;
  6150. }
  6151. /// Returns the 'basic_skill_check' setting.
  6152. /// This config determines if the server checks the skill level of NV_BASIC
  6153. /// before allowing the basic actions.
  6154. ///
  6155. /// basicskillcheck() -> <bool>
  6156. BUILDIN_FUNC(basicskillcheck)
  6157. {
  6158. script_pushint(st, battle_config.basic_skill_check);
  6159. return 0;
  6160. }
  6161. /// Returns the GM level of the player.
  6162. ///
  6163. /// getgmlevel() -> <level>
  6164. BUILDIN_FUNC(getgmlevel)
  6165. {
  6166. TBL_PC* sd;
  6167. sd = script_rid2sd(st);
  6168. if( sd == NULL )
  6169. return 0;// no player attached, report source
  6170. script_pushint(st, pc_isGM(sd));
  6171. return 0;
  6172. }
  6173. /// Terminates the execution of this script instance.
  6174. ///
  6175. /// end
  6176. BUILDIN_FUNC(end)
  6177. {
  6178. st->state = END;
  6179. return 0;
  6180. }
  6181. /// Checks if the player has that effect state (option).
  6182. ///
  6183. /// checkoption(<option>) -> <bool>
  6184. BUILDIN_FUNC(checkoption)
  6185. {
  6186. int option;
  6187. TBL_PC* sd;
  6188. sd = script_rid2sd(st);
  6189. if( sd == NULL )
  6190. return 0;// no player attached, report source
  6191. option = script_getnum(st,2);
  6192. if( sd->sc.option&option )
  6193. script_pushint(st, 1);
  6194. else
  6195. script_pushint(st, 0);
  6196. return 0;
  6197. }
  6198. /// Checks if the player is in that body state (opt1).
  6199. ///
  6200. /// checkoption1(<opt1>) -> <bool>
  6201. BUILDIN_FUNC(checkoption1)
  6202. {
  6203. int opt1;
  6204. TBL_PC* sd;
  6205. sd = script_rid2sd(st);
  6206. if( sd == NULL )
  6207. return 0;// no player attached, report source
  6208. opt1 = script_getnum(st,2);
  6209. if( sd->sc.opt1 == opt1 )
  6210. script_pushint(st, 1);
  6211. else
  6212. script_pushint(st, 0);
  6213. return 0;
  6214. }
  6215. /// Checks if the player has that health state (opt2).
  6216. ///
  6217. /// checkoption2(<opt2>) -> <bool>
  6218. BUILDIN_FUNC(checkoption2)
  6219. {
  6220. int opt2;
  6221. TBL_PC* sd;
  6222. sd = script_rid2sd(st);
  6223. if( sd == NULL )
  6224. return 0;// no player attached, report source
  6225. opt2 = script_getnum(st,2);
  6226. if( sd->sc.opt2&opt2 )
  6227. script_pushint(st, 1);
  6228. else
  6229. script_pushint(st, 0);
  6230. return 0;
  6231. }
  6232. /// Changes the effect state (option) of the player.
  6233. /// <flag> defaults to 1
  6234. /// <flag>=0 : removes the option
  6235. /// <flag>=other : adds the option
  6236. ///
  6237. /// setoption <option>,<flag>;
  6238. /// setoption <option>;
  6239. BUILDIN_FUNC(setoption)
  6240. {
  6241. int option;
  6242. int flag = 1;
  6243. TBL_PC* sd;
  6244. sd = script_rid2sd(st);
  6245. if( sd == NULL )
  6246. return 0;// no player attached, report source
  6247. option = script_getnum(st,2);
  6248. if( script_hasdata(st,3) )
  6249. flag = script_getnum(st,3);
  6250. else if( !option ){// Request to remove everything.
  6251. flag = 0;
  6252. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6253. }
  6254. if( flag ){// Add option
  6255. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6256. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6257. pc_setoption(sd, sd->sc.option|option);
  6258. } else// Remove option
  6259. pc_setoption(sd, sd->sc.option&~option);
  6260. return 0;
  6261. }
  6262. /// Returns if the player has a cart.
  6263. ///
  6264. /// checkcart() -> <bool>
  6265. ///
  6266. /// @author Valaris
  6267. BUILDIN_FUNC(checkcart)
  6268. {
  6269. TBL_PC* sd;
  6270. sd = script_rid2sd(st);
  6271. if( sd == NULL )
  6272. return 0;// no player attached, report source
  6273. if( pc_iscarton(sd) )
  6274. script_pushint(st, 1);
  6275. else
  6276. script_pushint(st, 0);
  6277. return 0;
  6278. }
  6279. /// Sets the cart of the player.
  6280. /// <type> defaults to 1
  6281. /// <type>=0 : removes the cart
  6282. /// <type>=1 : Normal cart
  6283. /// <type>=2 : Wooden cart
  6284. /// <type>=3 : Covered cart with flowers and ferns
  6285. /// <type>=4 : Wooden cart with a Panda doll on the back
  6286. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6287. ///
  6288. /// setcart <type>;
  6289. /// setcart;
  6290. BUILDIN_FUNC(setcart)
  6291. {
  6292. int type = 1;
  6293. TBL_PC* sd;
  6294. sd = script_rid2sd(st);
  6295. if( sd == NULL )
  6296. return 0;// no player attached, report source
  6297. if( script_hasdata(st,2) )
  6298. type = script_getnum(st,2);
  6299. pc_setcart(sd, type);
  6300. return 0;
  6301. }
  6302. /// Returns if the player has a falcon.
  6303. ///
  6304. /// checkfalcon() -> <bool>
  6305. ///
  6306. /// @author Valaris
  6307. BUILDIN_FUNC(checkfalcon)
  6308. {
  6309. TBL_PC* sd;
  6310. sd = script_rid2sd(st);
  6311. if( sd == NULL )
  6312. return 0;// no player attached, report source
  6313. if( pc_isfalcon(sd) )
  6314. script_pushint(st, 1);
  6315. else
  6316. script_pushint(st, 0);
  6317. return 0;
  6318. }
  6319. /// Sets if the player has a falcon or not.
  6320. /// <flag> defaults to 1
  6321. ///
  6322. /// setfalcon <flag>;
  6323. /// setfalcon;
  6324. BUILDIN_FUNC(setfalcon)
  6325. {
  6326. int flag = 1;
  6327. TBL_PC* sd;
  6328. sd = script_rid2sd(st);
  6329. if( sd == NULL )
  6330. return 0;// no player attached, report source
  6331. if( script_hasdata(st,2) )
  6332. flag = script_getnum(st,2);
  6333. pc_setfalcon(sd, flag);
  6334. return 0;
  6335. }
  6336. /// Returns if the player is riding.
  6337. ///
  6338. /// checkriding() -> <bool>
  6339. ///
  6340. /// @author Valaris
  6341. BUILDIN_FUNC(checkriding)
  6342. {
  6343. TBL_PC* sd;
  6344. sd = script_rid2sd(st);
  6345. if( sd == NULL )
  6346. return 0;// no player attached, report source
  6347. if( pc_isriding(sd) )
  6348. script_pushint(st, 1);
  6349. else
  6350. script_pushint(st, 0);
  6351. return 0;
  6352. }
  6353. /// Sets if the player is riding.
  6354. /// <flag> defaults to 1
  6355. ///
  6356. /// setriding <flag>;
  6357. /// setriding;
  6358. BUILDIN_FUNC(setriding)
  6359. {
  6360. int flag = 1;
  6361. TBL_PC* sd;
  6362. sd = script_rid2sd(st);
  6363. if( sd == NULL )
  6364. return 0;// no player attached, report source
  6365. if( script_hasdata(st,2) )
  6366. flag = script_getnum(st,2);
  6367. pc_setriding(sd, flag);
  6368. return 0;
  6369. }
  6370. /// Sets the save point of the player.
  6371. ///
  6372. /// save "<map name>",<x>,<y>
  6373. /// savepoint "<map name>",<x>,<y>
  6374. BUILDIN_FUNC(savepoint)
  6375. {
  6376. int x;
  6377. int y;
  6378. short map;
  6379. const char* str;
  6380. TBL_PC* sd;
  6381. sd = script_rid2sd(st);
  6382. if( sd == NULL )
  6383. return 0;// no player attached, report source
  6384. str = script_getstr(st, 2);
  6385. x = script_getnum(st,3);
  6386. y = script_getnum(st,4);
  6387. map = mapindex_name2id(str);
  6388. if( map )
  6389. pc_setsavepoint(sd, map, x, y);
  6390. return 0;
  6391. }
  6392. /*==========================================
  6393. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6394. *------------------------------------------*/
  6395. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6396. {
  6397. int type;
  6398. time_t timer;
  6399. struct tm *t;
  6400. type=script_getnum(st,2);
  6401. switch(type){
  6402. case 2:
  6403. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6404. // from the system clock.)
  6405. script_pushint(st,(int)time(NULL));
  6406. break;
  6407. case 1:
  6408. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6409. time(&timer);
  6410. t=localtime(&timer);
  6411. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6412. break;
  6413. case 0:
  6414. default:
  6415. //type 0:(System Ticks)
  6416. script_pushint(st,gettick());
  6417. break;
  6418. }
  6419. return 0;
  6420. }
  6421. /*==========================================
  6422. * GetTime(Type);
  6423. * 1: Sec 2: Min 3: Hour
  6424. * 4: WeekDay 5: MonthDay 6: Month
  6425. * 7: Year
  6426. *------------------------------------------*/
  6427. BUILDIN_FUNC(gettime) /* Asgard Version */
  6428. {
  6429. int type;
  6430. time_t timer;
  6431. struct tm *t;
  6432. type=script_getnum(st,2);
  6433. time(&timer);
  6434. t=localtime(&timer);
  6435. switch(type){
  6436. case 1://Sec(0~59)
  6437. script_pushint(st,t->tm_sec);
  6438. break;
  6439. case 2://Min(0~59)
  6440. script_pushint(st,t->tm_min);
  6441. break;
  6442. case 3://Hour(0~23)
  6443. script_pushint(st,t->tm_hour);
  6444. break;
  6445. case 4://WeekDay(0~6)
  6446. script_pushint(st,t->tm_wday);
  6447. break;
  6448. case 5://MonthDay(01~31)
  6449. script_pushint(st,t->tm_mday);
  6450. break;
  6451. case 6://Month(01~12)
  6452. script_pushint(st,t->tm_mon+1);
  6453. break;
  6454. case 7://Year(20xx)
  6455. script_pushint(st,t->tm_year+1900);
  6456. break;
  6457. case 8://Year Day(01~366)
  6458. script_pushint(st,t->tm_yday+1);
  6459. break;
  6460. default://(format error)
  6461. script_pushint(st,-1);
  6462. break;
  6463. }
  6464. return 0;
  6465. }
  6466. /*==========================================
  6467. * GetTimeStr("TimeFMT", Length);
  6468. *------------------------------------------*/
  6469. BUILDIN_FUNC(gettimestr)
  6470. {
  6471. char *tmpstr;
  6472. const char *fmtstr;
  6473. int maxlen;
  6474. time_t now = time(NULL);
  6475. fmtstr=script_getstr(st,2);
  6476. maxlen=script_getnum(st,3);
  6477. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6478. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6479. tmpstr[maxlen]='\0';
  6480. script_pushstr(st,tmpstr);
  6481. return 0;
  6482. }
  6483. /*==========================================
  6484. * カプラ倉庫を開く
  6485. *------------------------------------------*/
  6486. BUILDIN_FUNC(openstorage)
  6487. {
  6488. TBL_PC* sd;
  6489. sd = script_rid2sd(st);
  6490. if( sd == NULL )
  6491. return 0;
  6492. storage_storageopen(sd);
  6493. return 0;
  6494. }
  6495. BUILDIN_FUNC(guildopenstorage)
  6496. {
  6497. TBL_PC* sd;
  6498. int ret;
  6499. sd = script_rid2sd(st);
  6500. if( sd == NULL )
  6501. return 0;
  6502. ret = storage_guild_storageopen(sd);
  6503. script_pushint(st,ret);
  6504. return 0;
  6505. }
  6506. /*==========================================
  6507. * アイテムによるスキル発動
  6508. *------------------------------------------*/
  6509. /// itemskill <skill id>,<level>
  6510. /// itemskill "<skill name>",<level>
  6511. BUILDIN_FUNC(itemskill)
  6512. {
  6513. int id;
  6514. int lv;
  6515. TBL_PC* sd;
  6516. sd = script_rid2sd(st);
  6517. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6518. return 0;
  6519. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6520. lv = script_getnum(st,3);
  6521. sd->skillitem=id;
  6522. sd->skillitemlv=lv;
  6523. clif_item_skill(sd,id,lv);
  6524. return 0;
  6525. }
  6526. /*==========================================
  6527. * アイテム作成
  6528. *------------------------------------------*/
  6529. BUILDIN_FUNC(produce)
  6530. {
  6531. int trigger;
  6532. TBL_PC* sd;
  6533. sd = script_rid2sd(st);
  6534. if( sd == NULL )
  6535. return 0;
  6536. trigger=script_getnum(st,2);
  6537. clif_skill_produce_mix_list(sd, trigger);
  6538. return 0;
  6539. }
  6540. /*==========================================
  6541. *
  6542. *------------------------------------------*/
  6543. BUILDIN_FUNC(cooking)
  6544. {
  6545. int trigger;
  6546. TBL_PC* sd;
  6547. sd = script_rid2sd(st);
  6548. if( sd == NULL )
  6549. return 0;
  6550. trigger=script_getnum(st,2);
  6551. clif_cooking_list(sd, trigger);
  6552. return 0;
  6553. }
  6554. /*==========================================
  6555. * NPCでペット作る
  6556. *------------------------------------------*/
  6557. BUILDIN_FUNC(makepet)
  6558. {
  6559. TBL_PC* sd;
  6560. int id,pet_id;
  6561. id=script_getnum(st,2);
  6562. sd = script_rid2sd(st);
  6563. if( sd == NULL )
  6564. return 0;
  6565. pet_id = search_petDB_index(id, PET_CLASS);
  6566. if (pet_id < 0)
  6567. pet_id = search_petDB_index(id, PET_EGG);
  6568. if (pet_id >= 0 && sd) {
  6569. sd->catch_target_class = pet_db[pet_id].class_;
  6570. intif_create_pet(
  6571. sd->status.account_id, sd->status.char_id,
  6572. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6573. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6574. 100, 0, 1, pet_db[pet_id].jname);
  6575. }
  6576. return 0;
  6577. }
  6578. /*==========================================
  6579. * NPCで経験値上げる
  6580. *------------------------------------------*/
  6581. BUILDIN_FUNC(getexp)
  6582. {
  6583. TBL_PC* sd;
  6584. int base=0,job=0;
  6585. double bonus;
  6586. sd = script_rid2sd(st);
  6587. if( sd == NULL )
  6588. return 0;
  6589. base=script_getnum(st,2);
  6590. job =script_getnum(st,3);
  6591. if(base<0 || job<0)
  6592. return 0;
  6593. // bonus for npc-given exp
  6594. bonus = battle_config.quest_exp_rate / 100.;
  6595. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6596. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6597. pc_gainexp(sd, NULL, base, job);
  6598. return 0;
  6599. }
  6600. /*==========================================
  6601. * Gain guild exp [Celest]
  6602. *------------------------------------------*/
  6603. BUILDIN_FUNC(guildgetexp)
  6604. {
  6605. TBL_PC* sd;
  6606. int exp;
  6607. sd = script_rid2sd(st);
  6608. if( sd == NULL )
  6609. return 0;
  6610. exp = script_getnum(st,2);
  6611. if(exp < 0)
  6612. return 0;
  6613. if(sd && sd->status.guild_id > 0)
  6614. guild_getexp (sd, exp);
  6615. return 0;
  6616. }
  6617. /*==========================================
  6618. * Changes the guild master of a guild [Skotlex]
  6619. *------------------------------------------*/
  6620. BUILDIN_FUNC(guildchangegm)
  6621. {
  6622. TBL_PC *sd;
  6623. int guild_id;
  6624. const char *name;
  6625. guild_id = script_getnum(st,2);
  6626. name = script_getstr(st,3);
  6627. sd=map_nick2sd(name);
  6628. if (!sd)
  6629. script_pushint(st,0);
  6630. else
  6631. script_pushint(st,guild_gm_change(guild_id, sd));
  6632. return 0;
  6633. }
  6634. /*==========================================
  6635. * モンスター発生
  6636. *------------------------------------------*/
  6637. BUILDIN_FUNC(monster)
  6638. {
  6639. const char* mapn = script_getstr(st,2);
  6640. int x = script_getnum(st,3);
  6641. int y = script_getnum(st,4);
  6642. const char* str = script_getstr(st,5);
  6643. int class_ = script_getnum(st,6);
  6644. int amount = script_getnum(st,7);
  6645. const char* event = "";
  6646. struct map_session_data* sd;
  6647. int m;
  6648. if( script_hasdata(st,8) )
  6649. {
  6650. event = script_getstr(st,8);
  6651. check_event(st, event);
  6652. }
  6653. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6654. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6655. return 1;
  6656. }
  6657. sd = map_id2sd(st->rid);
  6658. if( sd && strcmp(mapn,"this") == 0 )
  6659. m = sd->bl.m;
  6660. else
  6661. {
  6662. m = map_mapname2mapid(mapn);
  6663. if( map[m].flag.src4instance && st->instance_id )
  6664. { // Try to redirect to the instance map, not the src map
  6665. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6666. {
  6667. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6668. return 1;
  6669. }
  6670. }
  6671. }
  6672. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6673. return 0;
  6674. }
  6675. /*==========================================
  6676. * Request List of Monster Drops
  6677. *------------------------------------------*/
  6678. BUILDIN_FUNC(getmobdrops)
  6679. {
  6680. int class_ = script_getnum(st,2);
  6681. int i, j = 0;
  6682. struct item_data *i_data;
  6683. struct mob_db *mob;
  6684. if( !mobdb_checkid(class_) )
  6685. {
  6686. script_pushint(st, 0);
  6687. return 0;
  6688. }
  6689. mob = mob_db(class_);
  6690. for( i = 0; i < MAX_MOB_DROP; i++ )
  6691. {
  6692. if( mob->dropitem[i].nameid < 1 )
  6693. continue;
  6694. if( (i_data = itemdb_exists(mob->dropitem[i].nameid)) == NULL )
  6695. continue;
  6696. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6697. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6698. j++;
  6699. }
  6700. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6701. script_pushint(st, 1);
  6702. return 0;
  6703. }
  6704. /*==========================================
  6705. * モンスター発生
  6706. *------------------------------------------*/
  6707. BUILDIN_FUNC(areamonster)
  6708. {
  6709. const char* mapn = script_getstr(st,2);
  6710. int x0 = script_getnum(st,3);
  6711. int y0 = script_getnum(st,4);
  6712. int x1 = script_getnum(st,5);
  6713. int y1 = script_getnum(st,6);
  6714. const char* str = script_getstr(st,7);
  6715. int class_ = script_getnum(st,8);
  6716. int amount = script_getnum(st,9);
  6717. const char* event = "";
  6718. struct map_session_data* sd;
  6719. int m;
  6720. if( script_hasdata(st,10) )
  6721. {
  6722. event = script_getstr(st,10);
  6723. check_event(st, event);
  6724. }
  6725. sd = map_id2sd(st->rid);
  6726. if( sd && strcmp(mapn,"this") == 0 )
  6727. m = sd->bl.m;
  6728. else
  6729. {
  6730. m = map_mapname2mapid(mapn);
  6731. if( map[m].flag.src4instance && st->instance_id )
  6732. { // Try to redirect to the instance map, not the src map
  6733. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6734. {
  6735. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6736. return 1;
  6737. }
  6738. }
  6739. }
  6740. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6741. return 0;
  6742. }
  6743. /*==========================================
  6744. * モンスター削除
  6745. *------------------------------------------*/
  6746. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6747. { //same fix but with killmonster instead - stripping events from mobs.
  6748. TBL_MOB* md = (TBL_MOB*)bl;
  6749. char *event=va_arg(ap,char *);
  6750. int allflag=va_arg(ap,int);
  6751. md->state.npc_killmonster = 1;
  6752. if(!allflag){
  6753. if(strcmp(event,md->npc_event)==0)
  6754. status_kill(bl);
  6755. }else{
  6756. if(!md->spawn)
  6757. status_kill(bl);
  6758. }
  6759. md->state.npc_killmonster = 0;
  6760. return 0;
  6761. }
  6762. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6763. {
  6764. TBL_MOB* md = (TBL_MOB*)bl;
  6765. char *event=va_arg(ap,char *);
  6766. int allflag=va_arg(ap,int);
  6767. if(!allflag){
  6768. if(strcmp(event,md->npc_event)==0)
  6769. status_kill(bl);
  6770. }else{
  6771. if(!md->spawn)
  6772. status_kill(bl);
  6773. }
  6774. return 0;
  6775. }
  6776. BUILDIN_FUNC(killmonster)
  6777. {
  6778. const char *mapname,*event;
  6779. int m,allflag=0;
  6780. mapname=script_getstr(st,2);
  6781. event=script_getstr(st,3);
  6782. if(strcmp(event,"All")==0)
  6783. allflag = 1;
  6784. else
  6785. check_event(st, event);
  6786. if( (m=map_mapname2mapid(mapname))<0 )
  6787. return 0;
  6788. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6789. return 0;
  6790. if( script_hasdata(st,4) ) {
  6791. if ( script_getnum(st,4) == 1 ) {
  6792. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6793. return 0;
  6794. }
  6795. }
  6796. map_freeblock_lock();
  6797. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6798. map_freeblock_unlock();
  6799. return 0;
  6800. }
  6801. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6802. { //Strips the event from the mob if it's killed the old method.
  6803. struct mob_data *md;
  6804. md = BL_CAST(BL_MOB, bl);
  6805. if (md->npc_event[0])
  6806. md->npc_event[0] = 0;
  6807. status_kill(bl);
  6808. return 0;
  6809. }
  6810. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6811. {
  6812. status_kill(bl);
  6813. return 0;
  6814. }
  6815. BUILDIN_FUNC(killmonsterall)
  6816. {
  6817. const char *mapname;
  6818. int m;
  6819. mapname=script_getstr(st,2);
  6820. if( (m = map_mapname2mapid(mapname))<0 )
  6821. return 0;
  6822. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6823. return 0;
  6824. if( script_hasdata(st,3) ) {
  6825. if ( script_getnum(st,3) == 1 ) {
  6826. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6827. return 0;
  6828. }
  6829. }
  6830. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6831. return 0;
  6832. }
  6833. /*==========================================
  6834. * Creates a clone of a player.
  6835. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6836. *------------------------------------------*/
  6837. BUILDIN_FUNC(clone)
  6838. {
  6839. TBL_PC *sd, *msd=NULL;
  6840. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6841. unsigned int duration = 0;
  6842. const char *map,*event="";
  6843. map=script_getstr(st,2);
  6844. x=script_getnum(st,3);
  6845. y=script_getnum(st,4);
  6846. event=script_getstr(st,5);
  6847. char_id=script_getnum(st,6);
  6848. if( script_hasdata(st,7) )
  6849. master_id=script_getnum(st,7);
  6850. if( script_hasdata(st,8) )
  6851. mode=script_getnum(st,8);
  6852. if( script_hasdata(st,9) )
  6853. flag=script_getnum(st,9);
  6854. if( script_hasdata(st,10) )
  6855. duration=script_getnum(st,10);
  6856. check_event(st, event);
  6857. m = map_mapname2mapid(map);
  6858. if (m < 0) return 0;
  6859. sd = map_charid2sd(char_id);
  6860. if (master_id) {
  6861. msd = map_charid2sd(master_id);
  6862. if (msd)
  6863. master_id = msd->bl.id;
  6864. else
  6865. master_id = 0;
  6866. }
  6867. if (sd) //Return ID of newly crafted clone.
  6868. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6869. else //Failed to create clone.
  6870. script_pushint(st,0);
  6871. return 0;
  6872. }
  6873. /*==========================================
  6874. * イベント実行
  6875. *------------------------------------------*/
  6876. BUILDIN_FUNC(doevent)
  6877. {
  6878. const char* event = script_getstr(st,2);
  6879. check_event(st, event);
  6880. npc_event(map_id2sd(st->rid),event,0);
  6881. return 0;
  6882. }
  6883. /*==========================================
  6884. * NPC主体イベント実行
  6885. *------------------------------------------*/
  6886. BUILDIN_FUNC(donpcevent)
  6887. {
  6888. const char* event = script_getstr(st,2);
  6889. check_event(st, event);
  6890. npc_event_do(event);
  6891. return 0;
  6892. }
  6893. /// for Aegis compatibility
  6894. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6895. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6896. {
  6897. const char* npc = script_getstr(st,2);
  6898. const char* command = script_getstr(st,3);
  6899. char event[51];
  6900. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6901. check_event(st, event);
  6902. npc_event_do(event);
  6903. return 0;
  6904. }
  6905. /*==========================================
  6906. * イベントタイマー追加
  6907. *------------------------------------------*/
  6908. BUILDIN_FUNC(addtimer)
  6909. {
  6910. int tick = script_getnum(st,2);
  6911. const char* event = script_getstr(st, 3);
  6912. TBL_PC* sd;
  6913. check_event(st, event);
  6914. sd = script_rid2sd(st);
  6915. if( sd == NULL )
  6916. return 0;
  6917. pc_addeventtimer(sd,tick,event);
  6918. return 0;
  6919. }
  6920. /*==========================================
  6921. * イベントタイマー削除
  6922. *------------------------------------------*/
  6923. BUILDIN_FUNC(deltimer)
  6924. {
  6925. const char *event;
  6926. TBL_PC* sd;
  6927. event=script_getstr(st, 2);
  6928. sd = script_rid2sd(st);
  6929. if( sd == NULL )
  6930. return 0;
  6931. check_event(st, event);
  6932. pc_deleventtimer(sd,event);
  6933. return 0;
  6934. }
  6935. /*==========================================
  6936. * イベントタイマーのカウント値追加
  6937. *------------------------------------------*/
  6938. BUILDIN_FUNC(addtimercount)
  6939. {
  6940. const char *event;
  6941. int tick;
  6942. TBL_PC* sd;
  6943. event=script_getstr(st, 2);
  6944. tick=script_getnum(st,3);
  6945. sd = script_rid2sd(st);
  6946. if( sd == NULL )
  6947. return 0;
  6948. check_event(st, event);
  6949. pc_addeventtimercount(sd,event,tick);
  6950. return 0;
  6951. }
  6952. /*==========================================
  6953. * NPCタイマー初期化
  6954. *------------------------------------------*/
  6955. BUILDIN_FUNC(initnpctimer)
  6956. {
  6957. struct npc_data *nd;
  6958. int flag = 0;
  6959. if( script_hasdata(st,3) )
  6960. { //Two arguments: NPC name and attach flag.
  6961. nd = npc_name2id(script_getstr(st, 2));
  6962. flag = script_getnum(st,3);
  6963. }
  6964. else if( script_hasdata(st,2) )
  6965. { //Check if argument is numeric (flag) or string (npc name)
  6966. struct script_data *data;
  6967. data = script_getdata(st,2);
  6968. get_val(st,data);
  6969. if( data_isstring(data) ) //NPC name
  6970. nd = npc_name2id(conv_str(st, data));
  6971. else if( data_isint(data) ) //Flag
  6972. {
  6973. nd = (struct npc_data *)map_id2bl(st->oid);
  6974. flag = conv_num(st,data);
  6975. }
  6976. else
  6977. {
  6978. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6979. return 1;
  6980. }
  6981. }
  6982. else
  6983. nd = (struct npc_data *)map_id2bl(st->oid);
  6984. if( !nd )
  6985. return 0;
  6986. if( flag ) //Attach
  6987. {
  6988. TBL_PC* sd = script_rid2sd(st);
  6989. if( sd == NULL )
  6990. return 0;
  6991. nd->u.scr.rid = sd->bl.id;
  6992. }
  6993. npc_settimerevent_tick(nd,0);
  6994. npc_timerevent_start(nd, st->rid);
  6995. return 0;
  6996. }
  6997. /*==========================================
  6998. * NPCタイマー開始
  6999. *------------------------------------------*/
  7000. BUILDIN_FUNC(startnpctimer)
  7001. {
  7002. struct npc_data *nd;
  7003. int flag = 0;
  7004. if( script_hasdata(st,3) )
  7005. { //Two arguments: NPC name and attach flag.
  7006. nd = npc_name2id(script_getstr(st, 2));
  7007. flag = script_getnum(st,3);
  7008. }
  7009. else if( script_hasdata(st,2) )
  7010. { //Check if argument is numeric (flag) or string (npc name)
  7011. struct script_data *data;
  7012. data = script_getdata(st,2);
  7013. get_val(st,data);
  7014. if( data_isstring(data) ) //NPC name
  7015. nd = npc_name2id(conv_str(st, data));
  7016. else if( data_isint(data) ) //Flag
  7017. {
  7018. nd = (struct npc_data *)map_id2bl(st->oid);
  7019. flag = conv_num(st,data);
  7020. }
  7021. else
  7022. {
  7023. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7024. return 1;
  7025. }
  7026. }
  7027. else
  7028. nd=(struct npc_data *)map_id2bl(st->oid);
  7029. if( !nd )
  7030. return 0;
  7031. if( flag ) //Attach
  7032. {
  7033. TBL_PC* sd = script_rid2sd(st);
  7034. if( sd == NULL )
  7035. return 0;
  7036. nd->u.scr.rid = sd->bl.id;
  7037. }
  7038. npc_timerevent_start(nd, st->rid);
  7039. return 0;
  7040. }
  7041. /*==========================================
  7042. * NPCタイマー停止
  7043. *------------------------------------------*/
  7044. BUILDIN_FUNC(stopnpctimer)
  7045. {
  7046. struct npc_data *nd;
  7047. int flag = 0;
  7048. if( script_hasdata(st,3) )
  7049. { //Two arguments: NPC name and attach flag.
  7050. nd = npc_name2id(script_getstr(st, 2));
  7051. flag = script_getnum(st,3);
  7052. }
  7053. else if( script_hasdata(st,2) )
  7054. { //Check if argument is numeric (flag) or string (npc name)
  7055. struct script_data *data;
  7056. data = script_getdata(st,2);
  7057. get_val(st,data);
  7058. if( data_isstring(data) ) //NPC name
  7059. nd = npc_name2id(conv_str(st, data));
  7060. else if( data_isint(data) ) //Flag
  7061. {
  7062. nd = (struct npc_data *)map_id2bl(st->oid);
  7063. flag = conv_num(st,data);
  7064. }
  7065. else
  7066. {
  7067. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7068. return 1;
  7069. }
  7070. }
  7071. else
  7072. nd=(struct npc_data *)map_id2bl(st->oid);
  7073. if( !nd )
  7074. return 0;
  7075. if( flag ) //Detach
  7076. nd->u.scr.rid = 0;
  7077. npc_timerevent_stop(nd);
  7078. return 0;
  7079. }
  7080. /*==========================================
  7081. * NPCタイマー情報所得
  7082. *------------------------------------------*/
  7083. BUILDIN_FUNC(getnpctimer)
  7084. {
  7085. struct npc_data *nd;
  7086. TBL_PC *sd;
  7087. int type = script_getnum(st,2);
  7088. int val = 0;
  7089. if( script_hasdata(st,3) )
  7090. nd = npc_name2id(script_getstr(st,3));
  7091. else
  7092. nd = (struct npc_data *)map_id2bl(st->oid);
  7093. if( !nd || nd->bl.type != BL_NPC )
  7094. {
  7095. script_pushint(st,0);
  7096. ShowError("getnpctimer: Invalid NPC.\n");
  7097. return 1;
  7098. }
  7099. switch( type )
  7100. {
  7101. case 0: val = npc_gettimerevent_tick(nd); break;
  7102. case 1:
  7103. if( nd->u.scr.rid )
  7104. {
  7105. sd = map_id2sd(nd->u.scr.rid);
  7106. if( !sd )
  7107. {
  7108. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7109. break;
  7110. }
  7111. val = (sd->npc_timer_id != -1);
  7112. }
  7113. else
  7114. val = (nd->u.scr.timerid !=-1);
  7115. break;
  7116. case 2: val = nd->u.scr.timeramount; break;
  7117. }
  7118. script_pushint(st,val);
  7119. return 0;
  7120. }
  7121. /*==========================================
  7122. * NPCタイマー値設定
  7123. *------------------------------------------*/
  7124. BUILDIN_FUNC(setnpctimer)
  7125. {
  7126. int tick;
  7127. struct npc_data *nd;
  7128. tick = script_getnum(st,2);
  7129. if( script_hasdata(st,3) )
  7130. nd = npc_name2id(script_getstr(st,3));
  7131. else
  7132. nd = (struct npc_data *)map_id2bl(st->oid);
  7133. if( !nd || nd->bl.type != BL_NPC )
  7134. {
  7135. script_pushint(st,1);
  7136. ShowError("setnpctimer: Invalid NPC.\n");
  7137. return 1;
  7138. }
  7139. npc_settimerevent_tick(nd,tick);
  7140. script_pushint(st,0);
  7141. return 0;
  7142. }
  7143. /*==========================================
  7144. * attaches the player rid to the timer [Celest]
  7145. *------------------------------------------*/
  7146. BUILDIN_FUNC(attachnpctimer)
  7147. {
  7148. TBL_PC *sd;
  7149. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7150. if( !nd || nd->bl.type != BL_NPC )
  7151. {
  7152. script_pushint(st,1);
  7153. ShowError("setnpctimer: Invalid NPC.\n");
  7154. return 1;
  7155. }
  7156. if( script_hasdata(st,2) )
  7157. sd = map_nick2sd(script_getstr(st,2));
  7158. else
  7159. sd = script_rid2sd(st);
  7160. if( !sd )
  7161. {
  7162. script_pushint(st,1);
  7163. ShowWarning("attachnpctimer: Invalid player.\n");
  7164. return 1;
  7165. }
  7166. nd->u.scr.rid = sd->bl.id;
  7167. script_pushint(st,0);
  7168. return 0;
  7169. }
  7170. /*==========================================
  7171. * detaches a player rid from the timer [Celest]
  7172. *------------------------------------------*/
  7173. BUILDIN_FUNC(detachnpctimer)
  7174. {
  7175. struct npc_data *nd;
  7176. if( script_hasdata(st,2) )
  7177. nd = npc_name2id(script_getstr(st,2));
  7178. else
  7179. nd = (struct npc_data *)map_id2bl(st->oid);
  7180. if( !nd || nd->bl.type != BL_NPC )
  7181. {
  7182. script_pushint(st,1);
  7183. ShowError("detachnpctimer: Invalid NPC.\n");
  7184. return 1;
  7185. }
  7186. nd->u.scr.rid = 0;
  7187. script_pushint(st,0);
  7188. return 0;
  7189. }
  7190. /*==========================================
  7191. * To avoid "player not attached" script errors, this function is provided,
  7192. * it checks if there is a player attached to the current script. [Skotlex]
  7193. * If no, returns 0, if yes, returns the account_id of the attached player.
  7194. *------------------------------------------*/
  7195. BUILDIN_FUNC(playerattached)
  7196. {
  7197. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7198. script_pushint(st,0);
  7199. else
  7200. script_pushint(st,st->rid);
  7201. return 0;
  7202. }
  7203. /*==========================================
  7204. * 天の声アナウンス
  7205. *------------------------------------------*/
  7206. BUILDIN_FUNC(announce)
  7207. {
  7208. const char *mes = script_getstr(st,2);
  7209. int flag = script_getnum(st,3);
  7210. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7211. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7212. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7213. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7214. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7215. if (flag&0x0f) // Broadcast source or broadcast region defined
  7216. {
  7217. send_target target;
  7218. 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
  7219. if (bl == NULL)
  7220. return 0;
  7221. flag &= 0x07;
  7222. target = (flag == 1) ? ALL_SAMEMAP :
  7223. (flag == 2) ? AREA :
  7224. (flag == 3) ? SELF :
  7225. ALL_CLIENT;
  7226. if (fontColor)
  7227. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7228. else
  7229. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7230. }
  7231. else
  7232. {
  7233. if (fontColor)
  7234. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7235. else
  7236. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7237. }
  7238. return 0;
  7239. }
  7240. /*==========================================
  7241. * 天の声アナウンス(特定マップ)
  7242. *------------------------------------------*/
  7243. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7244. {
  7245. char *mes = va_arg(ap, char *);
  7246. int len = va_arg(ap, int);
  7247. int type = va_arg(ap, int);
  7248. char *fontColor = va_arg(ap, char *);
  7249. short fontType = (short)va_arg(ap, int);
  7250. short fontSize = (short)va_arg(ap, int);
  7251. short fontAlign = (short)va_arg(ap, int);
  7252. short fontY = (short)va_arg(ap, int);
  7253. if (fontColor)
  7254. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7255. else
  7256. clif_broadcast(bl, mes, len, type, SELF);
  7257. return 0;
  7258. }
  7259. BUILDIN_FUNC(mapannounce)
  7260. {
  7261. const char *mapname = script_getstr(st,2);
  7262. const char *mes = script_getstr(st,3);
  7263. int flag = script_getnum(st,4);
  7264. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7265. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7266. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7267. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7268. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7269. int m;
  7270. if ((m = map_mapname2mapid(mapname)) < 0)
  7271. return 0;
  7272. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7273. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7274. return 0;
  7275. }
  7276. /*==========================================
  7277. * 天の声アナウンス(特定エリア)
  7278. *------------------------------------------*/
  7279. BUILDIN_FUNC(areaannounce)
  7280. {
  7281. const char *mapname = script_getstr(st,2);
  7282. int x0 = script_getnum(st,3);
  7283. int y0 = script_getnum(st,4);
  7284. int x1 = script_getnum(st,5);
  7285. int y1 = script_getnum(st,6);
  7286. const char *mes = script_getstr(st,7);
  7287. int flag = script_getnum(st,8);
  7288. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7289. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7290. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7291. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7292. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7293. int m;
  7294. if ((m = map_mapname2mapid(mapname)) < 0)
  7295. return 0;
  7296. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7297. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7298. return 0;
  7299. }
  7300. /*==========================================
  7301. * ユーザー数所得
  7302. *------------------------------------------*/
  7303. BUILDIN_FUNC(getusers)
  7304. {
  7305. int flag=script_getnum(st,2);
  7306. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7307. int val=0;
  7308. switch(flag&0x07){
  7309. case 0: val=map[bl->m].users; break;
  7310. case 1: val=map_getusers(); break;
  7311. }
  7312. script_pushint(st,val);
  7313. return 0;
  7314. }
  7315. /*==========================================
  7316. * Works like @WHO - displays all online users names in window
  7317. *------------------------------------------*/
  7318. BUILDIN_FUNC(getusersname)
  7319. {
  7320. TBL_PC *sd, *pl_sd;
  7321. int disp_num=1;
  7322. struct s_mapiterator* iter;
  7323. sd = script_rid2sd(st);
  7324. if (!sd) return 0;
  7325. iter = mapit_getallusers();
  7326. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7327. {
  7328. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7329. continue; // skip hidden GMs
  7330. if((disp_num++)%10==0)
  7331. clif_scriptnext(sd,st->oid);
  7332. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7333. }
  7334. mapit_free(iter);
  7335. return 0;
  7336. }
  7337. /*==========================================
  7338. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7339. *------------------------------------------*/
  7340. BUILDIN_FUNC(getmapguildusers)
  7341. {
  7342. const char *str;
  7343. int m, gid;
  7344. int i=0,c=0;
  7345. struct guild *g = NULL;
  7346. str=script_getstr(st,2);
  7347. gid=script_getnum(st,3);
  7348. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7349. script_pushint(st,-1);
  7350. return 0;
  7351. }
  7352. g = guild_search(gid);
  7353. if (g){
  7354. for(i = 0; i < g->max_member; i++)
  7355. {
  7356. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7357. c++;
  7358. }
  7359. }
  7360. script_pushint(st,c);
  7361. return 0;
  7362. }
  7363. /*==========================================
  7364. * マップ指定ユーザー数所得
  7365. *------------------------------------------*/
  7366. BUILDIN_FUNC(getmapusers)
  7367. {
  7368. const char *str;
  7369. int m;
  7370. str=script_getstr(st,2);
  7371. if( (m=map_mapname2mapid(str))< 0){
  7372. script_pushint(st,-1);
  7373. return 0;
  7374. }
  7375. script_pushint(st,map[m].users);
  7376. return 0;
  7377. }
  7378. /*==========================================
  7379. * エリア指定ユーザー数所得
  7380. *------------------------------------------*/
  7381. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7382. {
  7383. int *users=va_arg(ap,int *);
  7384. (*users)++;
  7385. return 0;
  7386. }
  7387. BUILDIN_FUNC(getareausers)
  7388. {
  7389. const char *str;
  7390. int m,x0,y0,x1,y1,users=0;
  7391. str=script_getstr(st,2);
  7392. x0=script_getnum(st,3);
  7393. y0=script_getnum(st,4);
  7394. x1=script_getnum(st,5);
  7395. y1=script_getnum(st,6);
  7396. if( (m=map_mapname2mapid(str))< 0){
  7397. script_pushint(st,-1);
  7398. return 0;
  7399. }
  7400. map_foreachinarea(buildin_getareausers_sub,
  7401. m,x0,y0,x1,y1,BL_PC,&users);
  7402. script_pushint(st,users);
  7403. return 0;
  7404. }
  7405. /*==========================================
  7406. * エリア指定ドロップアイテム数所得
  7407. *------------------------------------------*/
  7408. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7409. {
  7410. int item=va_arg(ap,int);
  7411. int *amount=va_arg(ap,int *);
  7412. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7413. if(drop->item_data.nameid==item)
  7414. (*amount)+=drop->item_data.amount;
  7415. return 0;
  7416. }
  7417. BUILDIN_FUNC(getareadropitem)
  7418. {
  7419. const char *str;
  7420. int m,x0,y0,x1,y1,item,amount=0;
  7421. struct script_data *data;
  7422. str=script_getstr(st,2);
  7423. x0=script_getnum(st,3);
  7424. y0=script_getnum(st,4);
  7425. x1=script_getnum(st,5);
  7426. y1=script_getnum(st,6);
  7427. data=script_getdata(st,7);
  7428. get_val(st,data);
  7429. if( data_isstring(data) ){
  7430. const char *name=conv_str(st,data);
  7431. struct item_data *item_data = itemdb_searchname(name);
  7432. item=UNKNOWN_ITEM_ID;
  7433. if( item_data )
  7434. item=item_data->nameid;
  7435. }else
  7436. item=conv_num(st,data);
  7437. if( (m=map_mapname2mapid(str))< 0){
  7438. script_pushint(st,-1);
  7439. return 0;
  7440. }
  7441. map_foreachinarea(buildin_getareadropitem_sub,
  7442. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7443. script_pushint(st,amount);
  7444. return 0;
  7445. }
  7446. /*==========================================
  7447. * NPCの有効化
  7448. *------------------------------------------*/
  7449. BUILDIN_FUNC(enablenpc)
  7450. {
  7451. const char *str;
  7452. str=script_getstr(st,2);
  7453. npc_enable(str,1);
  7454. return 0;
  7455. }
  7456. /*==========================================
  7457. * NPCの無効化
  7458. *------------------------------------------*/
  7459. BUILDIN_FUNC(disablenpc)
  7460. {
  7461. const char *str;
  7462. str=script_getstr(st,2);
  7463. npc_enable(str,0);
  7464. return 0;
  7465. }
  7466. /*==========================================
  7467. * 隠れているNPCの表示
  7468. *------------------------------------------*/
  7469. BUILDIN_FUNC(hideoffnpc)
  7470. {
  7471. const char *str;
  7472. str=script_getstr(st,2);
  7473. npc_enable(str,2);
  7474. return 0;
  7475. }
  7476. /*==========================================
  7477. * NPCをハイディング
  7478. *------------------------------------------*/
  7479. BUILDIN_FUNC(hideonnpc)
  7480. {
  7481. const char *str;
  7482. str=script_getstr(st,2);
  7483. npc_enable(str,4);
  7484. return 0;
  7485. }
  7486. /// Starts a status effect on the target unit or on the attached player.
  7487. ///
  7488. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7489. BUILDIN_FUNC(sc_start)
  7490. {
  7491. struct block_list* bl;
  7492. enum sc_type type;
  7493. int tick;
  7494. int val1;
  7495. int val4 = 0;
  7496. type = (sc_type)script_getnum(st,2);
  7497. tick = script_getnum(st,3);
  7498. val1 = script_getnum(st,4);
  7499. if( script_hasdata(st,5) )
  7500. bl = map_id2bl(script_getnum(st,5));
  7501. else
  7502. bl = map_id2bl(st->rid);
  7503. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7504. {// When there isn't a duration specified, try to get it from the skill_db
  7505. tick = skill_get_time(status_sc2skill(type), val1);
  7506. }
  7507. if( potion_flag == 1 && potion_target )
  7508. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7509. bl = map_id2bl(potion_target);
  7510. tick /= 2;// Thrown potions only last half.
  7511. val4 = 1;// Mark that this was a thrown sc_effect
  7512. }
  7513. if( bl )
  7514. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7515. return 0;
  7516. }
  7517. /// Starts a status effect on the target unit or on the attached player.
  7518. ///
  7519. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7520. BUILDIN_FUNC(sc_start2)
  7521. {
  7522. struct block_list* bl;
  7523. enum sc_type type;
  7524. int tick;
  7525. int val1;
  7526. int val4 = 0;
  7527. int rate;
  7528. type = (sc_type)script_getnum(st,2);
  7529. tick = script_getnum(st,3);
  7530. val1 = script_getnum(st,4);
  7531. rate = script_getnum(st,5);
  7532. if( script_hasdata(st,6) )
  7533. bl = map_id2bl(script_getnum(st,6));
  7534. else
  7535. bl = map_id2bl(st->rid);
  7536. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7537. {// When there isn't a duration specified, try to get it from the skill_db
  7538. tick = skill_get_time(status_sc2skill(type), val1);
  7539. }
  7540. if( potion_flag == 1 && potion_target )
  7541. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7542. bl = map_id2bl(potion_target);
  7543. tick /= 2;// Thrown potions only last half.
  7544. val4 = 1;// Mark that this was a thrown sc_effect
  7545. }
  7546. if( bl )
  7547. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7548. return 0;
  7549. }
  7550. /// Starts a status effect on the target unit or on the attached player.
  7551. ///
  7552. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7553. BUILDIN_FUNC(sc_start4)
  7554. {
  7555. struct block_list* bl;
  7556. enum sc_type type;
  7557. int tick;
  7558. int val1;
  7559. int val2;
  7560. int val3;
  7561. int val4;
  7562. type = (sc_type)script_getnum(st,2);
  7563. tick = script_getnum(st,3);
  7564. val1 = script_getnum(st,4);
  7565. val2 = script_getnum(st,5);
  7566. val3 = script_getnum(st,6);
  7567. val4 = script_getnum(st,7);
  7568. if( script_hasdata(st,8) )
  7569. bl = map_id2bl(script_getnum(st,8));
  7570. else
  7571. bl = map_id2bl(st->rid);
  7572. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7573. {// When there isn't a duration specified, try to get it from the skill_db
  7574. tick = skill_get_time(status_sc2skill(type), val1);
  7575. }
  7576. if( potion_flag == 1 && potion_target )
  7577. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7578. bl = map_id2bl(potion_target);
  7579. tick /= 2;// Thrown potions only last half.
  7580. }
  7581. if( bl )
  7582. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7583. return 0;
  7584. }
  7585. /// Ends one or all status effects on the target unit or on the attached player.
  7586. ///
  7587. /// sc_end <effect_id>{,<unit_id>};
  7588. BUILDIN_FUNC(sc_end)
  7589. {
  7590. struct block_list* bl;
  7591. int type;
  7592. type = script_getnum(st,2);
  7593. if( script_hasdata(st,3) )
  7594. bl = map_id2bl(script_getnum(st,3));
  7595. else
  7596. bl = map_id2bl(st->rid);
  7597. if( potion_flag==1 && potion_target )
  7598. {//##TODO how does this work [FlavioJS]
  7599. bl = map_id2bl(potion_target);
  7600. }
  7601. if( !bl ) return 0;
  7602. if( type >= 0 && type < SC_MAX )
  7603. {
  7604. struct status_change *sc = status_get_sc(bl);
  7605. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7606. if (!sce) return 0;
  7607. //This should help status_change_end force disabling the SC in case it has no limit.
  7608. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7609. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7610. } else
  7611. status_change_clear(bl, 2);// remove all effects
  7612. return 0;
  7613. }
  7614. /*==========================================
  7615. * 状態異常耐性を計算した確率を返す
  7616. *------------------------------------------*/
  7617. BUILDIN_FUNC(getscrate)
  7618. {
  7619. struct block_list *bl;
  7620. int type,rate;
  7621. type=script_getnum(st,2);
  7622. rate=script_getnum(st,3);
  7623. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7624. bl = map_id2bl(script_getnum(st,4));
  7625. else
  7626. bl = map_id2bl(st->rid);
  7627. if (bl)
  7628. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7629. script_pushint(st,rate);
  7630. return 0;
  7631. }
  7632. /*==========================================
  7633. *
  7634. *------------------------------------------*/
  7635. BUILDIN_FUNC(debugmes)
  7636. {
  7637. const char *str;
  7638. str=script_getstr(st,2);
  7639. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7640. return 0;
  7641. }
  7642. /*==========================================
  7643. *捕獲アイテム使用
  7644. *------------------------------------------*/
  7645. BUILDIN_FUNC(catchpet)
  7646. {
  7647. int pet_id;
  7648. TBL_PC *sd;
  7649. pet_id= script_getnum(st,2);
  7650. sd=script_rid2sd(st);
  7651. if( sd == NULL )
  7652. return 0;
  7653. pet_catch_process1(sd,pet_id);
  7654. return 0;
  7655. }
  7656. /*==========================================
  7657. * [orn]
  7658. *------------------------------------------*/
  7659. BUILDIN_FUNC(homunculus_evolution)
  7660. {
  7661. TBL_PC *sd;
  7662. sd=script_rid2sd(st);
  7663. if( sd == NULL )
  7664. return 0;
  7665. if(merc_is_hom_active(sd->hd))
  7666. {
  7667. if (sd->hd->homunculus.intimacy > 91000)
  7668. merc_hom_evolution(sd->hd);
  7669. else
  7670. clif_emotion(&sd->hd->bl, 4) ; //swt
  7671. }
  7672. return 0;
  7673. }
  7674. // [Zephyrus]
  7675. BUILDIN_FUNC(homunculus_shuffle)
  7676. {
  7677. TBL_PC *sd;
  7678. sd=script_rid2sd(st);
  7679. if( sd == NULL )
  7680. return 0;
  7681. if(merc_is_hom_active(sd->hd))
  7682. merc_hom_shuffle(sd->hd);
  7683. return 0;
  7684. }
  7685. //These two functions bring the eA MAPID_* class functionality to scripts.
  7686. BUILDIN_FUNC(eaclass)
  7687. {
  7688. int class_;
  7689. if( script_hasdata(st,2) )
  7690. class_ = script_getnum(st,2);
  7691. else {
  7692. TBL_PC *sd;
  7693. sd=script_rid2sd(st);
  7694. if (!sd) {
  7695. script_pushint(st,-1);
  7696. return 0;
  7697. }
  7698. class_ = sd->status.class_;
  7699. }
  7700. script_pushint(st,pc_jobid2mapid(class_));
  7701. return 0;
  7702. }
  7703. BUILDIN_FUNC(roclass)
  7704. {
  7705. int class_ =script_getnum(st,2);
  7706. int sex;
  7707. if( script_hasdata(st,3) )
  7708. sex = script_getnum(st,3);
  7709. else {
  7710. TBL_PC *sd;
  7711. if (st->rid && (sd=script_rid2sd(st)))
  7712. sex = sd->status.sex;
  7713. else
  7714. sex = 1; //Just use male when not found.
  7715. }
  7716. script_pushint(st,pc_mapid2jobid(class_, sex));
  7717. return 0;
  7718. }
  7719. /*==========================================
  7720. *携帯卵孵化機使用
  7721. *------------------------------------------*/
  7722. BUILDIN_FUNC(birthpet)
  7723. {
  7724. TBL_PC *sd;
  7725. sd=script_rid2sd(st);
  7726. if( sd == NULL )
  7727. return 0;
  7728. clif_sendegg(sd);
  7729. return 0;
  7730. }
  7731. /*==========================================
  7732. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7733. *------------------------------------------*/
  7734. BUILDIN_FUNC(resetlvl)
  7735. {
  7736. TBL_PC *sd;
  7737. int type=script_getnum(st,2);
  7738. sd=script_rid2sd(st);
  7739. if( sd == NULL )
  7740. return 0;
  7741. pc_resetlvl(sd,type);
  7742. return 0;
  7743. }
  7744. /*==========================================
  7745. * ステータスリセット
  7746. *------------------------------------------*/
  7747. BUILDIN_FUNC(resetstatus)
  7748. {
  7749. TBL_PC *sd;
  7750. sd=script_rid2sd(st);
  7751. pc_resetstate(sd);
  7752. return 0;
  7753. }
  7754. /*==========================================
  7755. * script command resetskill
  7756. *------------------------------------------*/
  7757. BUILDIN_FUNC(resetskill)
  7758. {
  7759. TBL_PC *sd;
  7760. sd=script_rid2sd(st);
  7761. pc_resetskill(sd,1);
  7762. return 0;
  7763. }
  7764. /*==========================================
  7765. * Counts total amount of skill points.
  7766. *------------------------------------------*/
  7767. BUILDIN_FUNC(skillpointcount)
  7768. {
  7769. TBL_PC *sd;
  7770. sd=script_rid2sd(st);
  7771. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7772. return 0;
  7773. }
  7774. /*==========================================
  7775. *
  7776. *------------------------------------------*/
  7777. BUILDIN_FUNC(changebase)
  7778. {
  7779. TBL_PC *sd=NULL;
  7780. int vclass;
  7781. if( script_hasdata(st,3) )
  7782. sd=map_id2sd(script_getnum(st,3));
  7783. else
  7784. sd=script_rid2sd(st);
  7785. if(sd == NULL)
  7786. return 0;
  7787. vclass = script_getnum(st,2);
  7788. if(vclass == JOB_WEDDING)
  7789. {
  7790. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7791. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7792. )
  7793. return 0;
  7794. }
  7795. if(!sd->disguise && vclass != sd->vd.class_) {
  7796. status_set_viewdata(&sd->bl, vclass);
  7797. //Updated client view. Base, Weapon and Cloth Colors.
  7798. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7799. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7800. if (sd->vd.cloth_color)
  7801. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7802. clif_skillinfoblock(sd);
  7803. }
  7804. return 0;
  7805. }
  7806. /*==========================================
  7807. * 性別変換
  7808. *------------------------------------------*/
  7809. BUILDIN_FUNC(changesex)
  7810. {
  7811. TBL_PC *sd = NULL;
  7812. sd = script_rid2sd(st);
  7813. chrif_changesex(sd);
  7814. return 0;
  7815. }
  7816. /*==========================================
  7817. * Works like 'announce' but outputs in the common chat window
  7818. *------------------------------------------*/
  7819. BUILDIN_FUNC(globalmes)
  7820. {
  7821. struct block_list *bl = map_id2bl(st->oid);
  7822. struct npc_data *nd = (struct npc_data *)bl;
  7823. const char *name=NULL,*mes;
  7824. mes=script_getstr(st,2); // メッセージの取得
  7825. if(mes==NULL) return 0;
  7826. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7827. name=script_getstr(st,3);
  7828. } else {
  7829. name=nd->name;
  7830. }
  7831. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7832. return 0;
  7833. }
  7834. /////////////////////////////////////////////////////////////////////
  7835. // NPC waiting room (chat room)
  7836. //
  7837. /// Creates a waiting room (chat room) for this npc.
  7838. ///
  7839. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7840. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7841. /// waitingroom "<title>",<limit>,"<event>";
  7842. /// waitingroom "<title>",<limit>;
  7843. BUILDIN_FUNC(waitingroom)
  7844. {
  7845. struct npc_data* nd;
  7846. const char* title;
  7847. const char* ev = "";
  7848. int limit;
  7849. int trigger = 0;
  7850. int pub = 1;
  7851. title = script_getstr(st, 2);
  7852. limit = script_getnum(st, 3);
  7853. if( script_hasdata(st,5) )
  7854. {
  7855. struct script_data* last = script_getdata(st, 5);
  7856. get_val(st, last);
  7857. if( data_isstring(last) )
  7858. {// ,<trigger>,"<event>"
  7859. trigger = script_getnum(st, 4);
  7860. ev = script_getstr(st, 5);
  7861. }
  7862. else
  7863. {// ,"<event>",<trigger>
  7864. ev = script_getstr(st, 4);
  7865. trigger = script_getnum(st,5);
  7866. }
  7867. }
  7868. else if( script_hasdata(st,4) )
  7869. {// ,"<event>"
  7870. ev = script_getstr(st, 4);
  7871. trigger = limit;
  7872. }
  7873. nd = (struct npc_data *)map_id2bl(st->oid);
  7874. if( nd != NULL )
  7875. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7876. return 0;
  7877. }
  7878. /// Removes the waiting room of the current or target npc.
  7879. ///
  7880. /// delwaitingroom "<npc_name>";
  7881. /// delwaitingroom;
  7882. BUILDIN_FUNC(delwaitingroom)
  7883. {
  7884. struct npc_data* nd;
  7885. if( script_hasdata(st,2) )
  7886. nd = npc_name2id(script_getstr(st, 2));
  7887. else
  7888. nd = (struct npc_data *)map_id2bl(st->oid);
  7889. if( nd != NULL )
  7890. chat_deletenpcchat(nd);
  7891. return 0;
  7892. }
  7893. /// Kicks all the players from the waiting room of the current or target npc.
  7894. ///
  7895. /// kickwaitingroomall "<npc_name>";
  7896. /// kickwaitingroomall;
  7897. BUILDIN_FUNC(waitingroomkickall)
  7898. {
  7899. struct npc_data* nd;
  7900. struct chat_data* cd;
  7901. if( script_hasdata(st,2) )
  7902. nd = npc_name2id(script_getstr(st,2));
  7903. else
  7904. nd = (struct npc_data *)map_id2bl(st->oid);
  7905. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7906. chat_npckickall(cd);
  7907. return 0;
  7908. }
  7909. /// Enables the waiting room event of the current or target npc.
  7910. ///
  7911. /// enablewaitingroomevent "<npc_name>";
  7912. /// enablewaitingroomevent;
  7913. BUILDIN_FUNC(enablewaitingroomevent)
  7914. {
  7915. struct npc_data* nd;
  7916. struct chat_data* cd;
  7917. if( script_hasdata(st,2) )
  7918. nd = npc_name2id(script_getstr(st, 2));
  7919. else
  7920. nd = (struct npc_data *)map_id2bl(st->oid);
  7921. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7922. chat_enableevent(cd);
  7923. return 0;
  7924. }
  7925. /// Disables the waiting room event of the current or target npc.
  7926. ///
  7927. /// disablewaitingroomevent "<npc_name>";
  7928. /// disablewaitingroomevent;
  7929. BUILDIN_FUNC(disablewaitingroomevent)
  7930. {
  7931. struct npc_data *nd;
  7932. struct chat_data *cd;
  7933. if( script_hasdata(st,2) )
  7934. nd = npc_name2id(script_getstr(st, 2));
  7935. else
  7936. nd = (struct npc_data *)map_id2bl(st->oid);
  7937. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7938. chat_disableevent(cd);
  7939. return 0;
  7940. }
  7941. /// Returns info on the waiting room of the current or target npc.
  7942. /// Returns -1 if the type unknown
  7943. /// <type>=0 : current number of users
  7944. /// <type>=1 : maximum number of users allowed
  7945. /// <type>=2 : the number of users that trigger the event
  7946. /// <type>=3 : if the trigger is disabled
  7947. /// <type>=4 : the title of the waiting room
  7948. /// <type>=5 : the password of the waiting room
  7949. /// <type>=16 : the name of the waiting room event
  7950. /// <type>=32 : if the waiting room is full
  7951. /// <type>=33 : if there are enough users to trigger the event
  7952. ///
  7953. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7954. /// getwaitingroomstate(<type>) -> <info>
  7955. BUILDIN_FUNC(getwaitingroomstate)
  7956. {
  7957. struct npc_data *nd;
  7958. struct chat_data *cd;
  7959. int type;
  7960. type = script_getnum(st,2);
  7961. if( script_hasdata(st,3) )
  7962. nd = npc_name2id(script_getstr(st, 3));
  7963. else
  7964. nd = (struct npc_data *)map_id2bl(st->oid);
  7965. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7966. {
  7967. script_pushint(st, -1);
  7968. return 0;
  7969. }
  7970. switch(type)
  7971. {
  7972. case 0: script_pushint(st, cd->users); break;
  7973. case 1: script_pushint(st, cd->limit); break;
  7974. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7975. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7976. case 4: script_pushstrcopy(st, cd->title); break;
  7977. case 5: script_pushstrcopy(st, cd->pass); break;
  7978. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7979. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7980. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7981. default: script_pushint(st, -1); break;
  7982. }
  7983. return 0;
  7984. }
  7985. /// Warps the trigger or target amount of players to the target map and position.
  7986. /// Players are automatically removed from the waiting room.
  7987. /// Those waiting the longest will get warped first.
  7988. /// The target map can be "Random" for a random position in the current map,
  7989. /// and "SavePoint" for the savepoint map+position.
  7990. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7991. ///
  7992. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7993. /// The total number of teleported players is put into $@warpwaitingpcnum
  7994. ///
  7995. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7996. /// warpwaitingpc "<map name>",<x>,<y>;
  7997. BUILDIN_FUNC(warpwaitingpc)
  7998. {
  7999. int x;
  8000. int y;
  8001. int i;
  8002. int n;
  8003. const char* map_name;
  8004. struct npc_data* nd;
  8005. struct chat_data* cd;
  8006. TBL_PC* sd;
  8007. nd = (struct npc_data *)map_id2bl(st->oid);
  8008. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8009. return 0;
  8010. map_name = script_getstr(st,2);
  8011. x = script_getnum(st,3);
  8012. y = script_getnum(st,4);
  8013. n = cd->trigger&0x7f;
  8014. if( script_hasdata(st,5) )
  8015. n = script_getnum(st,5);
  8016. for( i = 0; i < n && cd->users > 0; i++ )
  8017. {
  8018. sd = cd->usersd[0];
  8019. if( sd == NULL )
  8020. {
  8021. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  8022. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8023. continue;// Broken npc chat room?
  8024. }
  8025. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8026. if( strcmp(map_name,"Random") == 0 )
  8027. pc_randomwarp(sd,3);
  8028. else if( strcmp(map_name,"SavePoint") == 0 )
  8029. {
  8030. if( map[sd->bl.m].flag.noteleport )
  8031. return 0;// can't teleport on this map
  8032. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  8033. }
  8034. else
  8035. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  8036. }
  8037. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8038. return 0;
  8039. }
  8040. /////////////////////////////////////////////////////////////////////
  8041. // ...
  8042. //
  8043. /*==========================================
  8044. * RIDのアタッチ
  8045. *------------------------------------------*/
  8046. BUILDIN_FUNC(attachrid)
  8047. {
  8048. st->rid=script_getnum(st,2);
  8049. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  8050. return 0;
  8051. }
  8052. /*==========================================
  8053. * RIDのデタッチ
  8054. *------------------------------------------*/
  8055. BUILDIN_FUNC(detachrid)
  8056. {
  8057. st->rid=0;
  8058. return 0;
  8059. }
  8060. /*==========================================
  8061. * 存在チェック
  8062. *------------------------------------------*/
  8063. BUILDIN_FUNC(isloggedin)
  8064. {
  8065. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8066. if (script_hasdata(st,3) && sd &&
  8067. sd->status.char_id != script_getnum(st,3))
  8068. sd = NULL;
  8069. push_val(st->stack,C_INT,sd!=NULL);
  8070. return 0;
  8071. }
  8072. /*==========================================
  8073. *
  8074. *------------------------------------------*/
  8075. BUILDIN_FUNC(setmapflagnosave)
  8076. {
  8077. int m,x,y;
  8078. unsigned short mapindex;
  8079. const char *str,*str2;
  8080. str=script_getstr(st,2);
  8081. str2=script_getstr(st,3);
  8082. x=script_getnum(st,4);
  8083. y=script_getnum(st,5);
  8084. m = map_mapname2mapid(str);
  8085. mapindex = mapindex_name2id(str2);
  8086. if(m >= 0 && mapindex) {
  8087. map[m].flag.nosave=1;
  8088. map[m].save.map=mapindex;
  8089. map[m].save.x=x;
  8090. map[m].save.y=y;
  8091. }
  8092. return 0;
  8093. }
  8094. BUILDIN_FUNC(getmapflag)
  8095. {
  8096. int m,i;
  8097. const char *str;
  8098. str=script_getstr(st,2);
  8099. i=script_getnum(st,3);
  8100. m = map_mapname2mapid(str);
  8101. if(m >= 0) {
  8102. switch(i) {
  8103. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8104. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8105. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8106. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8107. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8108. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8109. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8110. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8111. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8112. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8113. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8114. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8115. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8116. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8117. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8118. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8119. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8120. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8121. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8122. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8123. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8124. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8125. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8126. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8127. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8128. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  8129. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8130. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8131. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8132. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8133. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8134. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8135. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8136. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8137. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8138. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8139. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8140. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8141. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8142. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8143. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8144. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8145. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8146. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8147. }
  8148. }
  8149. return 0;
  8150. }
  8151. BUILDIN_FUNC(setmapflag)
  8152. {
  8153. int m,i;
  8154. const char *str;
  8155. const char *val=NULL;
  8156. str=script_getstr(st,2);
  8157. i=script_getnum(st,3);
  8158. if(script_hasdata(st,4)){
  8159. val=script_getstr(st,4);
  8160. }
  8161. m = map_mapname2mapid(str);
  8162. if(m >= 0) {
  8163. switch(i) {
  8164. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8165. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8166. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8167. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8168. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8169. case MF_PVP: map[m].flag.pvp=1; break;
  8170. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8171. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8172. case MF_GVG: map[m].flag.gvg=1; break;
  8173. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8174. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8175. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8176. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8177. case MF_NODROP: map[m].flag.nodrop=1; break;
  8178. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8179. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8180. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8181. case MF_SNOW: map[m].flag.snow=1; break;
  8182. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8183. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8184. case MF_FOG: map[m].flag.fog=1; break;
  8185. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8186. case MF_SAKURA: map[m].flag.sakura=1; break;
  8187. case MF_LEAVES: map[m].flag.leaves=1; break;
  8188. case MF_RAIN: map[m].flag.rain=1; break;
  8189. case MF_INDOORS: map[m].flag.indoors=1; break;
  8190. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8191. case MF_NOGO: map[m].flag.nogo=1; break;
  8192. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8193. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8194. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8195. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8196. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8197. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8198. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8199. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8200. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8201. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8202. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8203. case MF_NOVENDING: map[m].flag.novending=1; break;
  8204. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8205. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8206. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8207. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8208. }
  8209. }
  8210. return 0;
  8211. }
  8212. BUILDIN_FUNC(removemapflag)
  8213. {
  8214. int m,i;
  8215. const char *str;
  8216. str=script_getstr(st,2);
  8217. i=script_getnum(st,3);
  8218. m = map_mapname2mapid(str);
  8219. if(m >= 0) {
  8220. switch(i) {
  8221. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8222. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8223. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8224. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8225. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8226. case MF_PVP: map[m].flag.pvp=0; break;
  8227. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8228. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8229. case MF_GVG: map[m].flag.gvg=0; break;
  8230. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8231. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8232. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8233. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8234. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8235. case MF_NODROP: map[m].flag.nodrop=0; break;
  8236. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8237. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8238. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8239. case MF_SNOW: map[m].flag.snow=0; break;
  8240. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8241. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8242. case MF_FOG: map[m].flag.fog=0; break;
  8243. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8244. case MF_SAKURA: map[m].flag.sakura=0; break;
  8245. case MF_LEAVES: map[m].flag.leaves=0; break;
  8246. case MF_RAIN: map[m].flag.rain=0; break;
  8247. case MF_INDOORS: map[m].flag.indoors=0; break;
  8248. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8249. case MF_NOGO: map[m].flag.nogo=0; break;
  8250. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8251. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8252. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8253. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8254. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8255. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8256. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8257. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8258. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8259. case MF_JEXP: map[m].jexp=100; break;
  8260. case MF_BEXP: map[m].bexp=100; break;
  8261. case MF_NOVENDING: map[m].flag.novending=0; break;
  8262. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8263. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8264. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8265. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8266. }
  8267. }
  8268. return 0;
  8269. }
  8270. BUILDIN_FUNC(pvpon)
  8271. {
  8272. int m;
  8273. const char *str;
  8274. TBL_PC* sd = NULL;
  8275. struct s_mapiterator* iter;
  8276. str = script_getstr(st,2);
  8277. m = map_mapname2mapid(str);
  8278. if( m < 0 || map[m].flag.pvp )
  8279. return 0; // nothing to do
  8280. map[m].flag.pvp = 1;
  8281. clif_send0199(m,1);
  8282. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8283. return 0;
  8284. iter = mapit_getallusers();
  8285. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8286. {
  8287. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8288. continue; // not applicable
  8289. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8290. sd->pvp_rank = 0;
  8291. sd->pvp_lastusers = 0;
  8292. sd->pvp_point = 5;
  8293. sd->pvp_won = 0;
  8294. sd->pvp_lost = 0;
  8295. }
  8296. mapit_free(iter);
  8297. return 0;
  8298. }
  8299. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8300. {
  8301. TBL_PC* sd = (TBL_PC*)bl;
  8302. clif_pvpset(sd, 0, 0, 2);
  8303. if (sd->pvp_timer != -1) {
  8304. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8305. sd->pvp_timer = INVALID_TIMER;
  8306. }
  8307. return 0;
  8308. }
  8309. BUILDIN_FUNC(pvpoff)
  8310. {
  8311. int m;
  8312. const char *str;
  8313. str=script_getstr(st,2);
  8314. m = map_mapname2mapid(str);
  8315. if(m < 0 || !map[m].flag.pvp)
  8316. return 0; //fixed Lupus
  8317. map[m].flag.pvp = 0;
  8318. clif_send0199(m,0);
  8319. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8320. return 0;
  8321. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8322. return 0;
  8323. }
  8324. BUILDIN_FUNC(gvgon)
  8325. {
  8326. int m;
  8327. const char *str;
  8328. str=script_getstr(st,2);
  8329. m = map_mapname2mapid(str);
  8330. if(m >= 0 && !map[m].flag.gvg) {
  8331. map[m].flag.gvg = 1;
  8332. clif_send0199(m,3);
  8333. }
  8334. return 0;
  8335. }
  8336. BUILDIN_FUNC(gvgoff)
  8337. {
  8338. int m;
  8339. const char *str;
  8340. str=script_getstr(st,2);
  8341. m = map_mapname2mapid(str);
  8342. if(m >= 0 && map[m].flag.gvg) {
  8343. map[m].flag.gvg = 0;
  8344. clif_send0199(m,0);
  8345. }
  8346. return 0;
  8347. }
  8348. /*==========================================
  8349. * Shows an emoticon on top of the player/npc
  8350. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8351. *------------------------------------------*/
  8352. //Optional second parameter added by [Skotlex]
  8353. BUILDIN_FUNC(emotion)
  8354. {
  8355. int type;
  8356. int player=0;
  8357. type=script_getnum(st,2);
  8358. if(type < 0 || type > 100)
  8359. return 0;
  8360. if( script_hasdata(st,3) )
  8361. player=script_getnum(st,3);
  8362. if (player) {
  8363. TBL_PC *sd = script_rid2sd(st);
  8364. if (sd)
  8365. clif_emotion(&sd->bl,type);
  8366. } else
  8367. clif_emotion(map_id2bl(st->oid),type);
  8368. return 0;
  8369. }
  8370. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8371. {
  8372. int m=va_arg(ap,int);
  8373. int g_id=va_arg(ap,int);
  8374. int flag=va_arg(ap,int);
  8375. if(!sd || sd->bl.m != m)
  8376. return 0;
  8377. if(
  8378. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8379. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8380. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8381. )
  8382. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8383. return 1;
  8384. }
  8385. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8386. {
  8387. struct mob_data *md=(struct mob_data *)bl;
  8388. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8389. status_kill(bl);
  8390. return 0;
  8391. }
  8392. BUILDIN_FUNC(maprespawnguildid)
  8393. {
  8394. const char *mapname=script_getstr(st,2);
  8395. int g_id=script_getnum(st,3);
  8396. int flag=script_getnum(st,4);
  8397. int m=map_mapname2mapid(mapname);
  8398. if(m == -1)
  8399. return 0;
  8400. //Catch ALL players (in case some are 'between maps' on execution time)
  8401. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8402. if (flag&4) //Remove script mobs.
  8403. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8404. return 0;
  8405. }
  8406. BUILDIN_FUNC(agitstart)
  8407. {
  8408. if(agit_flag==1) return 0; // Agit already Start.
  8409. agit_flag=1;
  8410. guild_agit_start();
  8411. return 0;
  8412. }
  8413. BUILDIN_FUNC(agitend)
  8414. {
  8415. if(agit_flag==0) return 0; // Agit already End.
  8416. agit_flag=0;
  8417. guild_agit_end();
  8418. return 0;
  8419. }
  8420. BUILDIN_FUNC(agitstart2)
  8421. {
  8422. if(agit2_flag==1) return 0; // Agit2 already Start.
  8423. agit2_flag=1;
  8424. guild_agit2_start();
  8425. return 0;
  8426. }
  8427. BUILDIN_FUNC(agitend2)
  8428. {
  8429. if(agit2_flag==0) return 0; // Agit2 already End.
  8430. agit2_flag=0;
  8431. guild_agit2_end();
  8432. return 0;
  8433. }
  8434. /*==========================================
  8435. * Returns whether woe is on or off. // choice script
  8436. *------------------------------------------*/
  8437. BUILDIN_FUNC(agitcheck)
  8438. {
  8439. script_pushint(st,agit_flag);
  8440. return 0;
  8441. }
  8442. /*==========================================
  8443. * Returns whether woese is on or off. // choice script
  8444. *------------------------------------------*/
  8445. BUILDIN_FUNC(agitcheck2)
  8446. {
  8447. script_pushint(st,agit2_flag);
  8448. return 0;
  8449. }
  8450. /// Sets the guild_id of this npc.
  8451. ///
  8452. /// flagemblem <guild_id>;
  8453. BUILDIN_FUNC(flagemblem)
  8454. {
  8455. TBL_NPC* nd;
  8456. int g_id=script_getnum(st,2);
  8457. if(g_id < 0) return 0;
  8458. nd = (TBL_NPC*)map_id2nd(st->oid);
  8459. if( nd == NULL )
  8460. {
  8461. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8462. }
  8463. else if( nd->subtype != SCRIPT )
  8464. {
  8465. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8466. }
  8467. else
  8468. {
  8469. nd->u.scr.guild_id = g_id;
  8470. clif_guild_emblem_area(&nd->bl);
  8471. }
  8472. return 0;
  8473. }
  8474. BUILDIN_FUNC(getcastlename)
  8475. {
  8476. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8477. struct guild_castle* gc = guild_mapname2gc(mapname);
  8478. const char* name = (gc) ? gc->castle_name : "";
  8479. script_pushstrcopy(st,name);
  8480. return 0;
  8481. }
  8482. BUILDIN_FUNC(getcastledata)
  8483. {
  8484. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8485. int index = script_getnum(st,3);
  8486. struct guild_castle* gc = guild_mapname2gc(mapname);
  8487. if(script_hasdata(st,4) && index==0 && gc) {
  8488. const char* event = script_getstr(st,4);
  8489. check_event(st, event);
  8490. guild_addcastleinfoevent(gc->castle_id,17,event);
  8491. }
  8492. if(gc){
  8493. switch(index){
  8494. case 0: {
  8495. int i;
  8496. for(i=1;i<18;i++) // Initialize[AgitInit]
  8497. guild_castledataload(gc->castle_id,i);
  8498. } break;
  8499. case 1:
  8500. script_pushint(st,gc->guild_id); break;
  8501. case 2:
  8502. script_pushint(st,gc->economy); break;
  8503. case 3:
  8504. script_pushint(st,gc->defense); break;
  8505. case 4:
  8506. script_pushint(st,gc->triggerE); break;
  8507. case 5:
  8508. script_pushint(st,gc->triggerD); break;
  8509. case 6:
  8510. script_pushint(st,gc->nextTime); break;
  8511. case 7:
  8512. script_pushint(st,gc->payTime); break;
  8513. case 8:
  8514. script_pushint(st,gc->createTime); break;
  8515. case 9:
  8516. script_pushint(st,gc->visibleC); break;
  8517. case 10:
  8518. case 11:
  8519. case 12:
  8520. case 13:
  8521. case 14:
  8522. case 15:
  8523. case 16:
  8524. case 17:
  8525. script_pushint(st,gc->guardian[index-10].visible); break;
  8526. default:
  8527. script_pushint(st,0); break;
  8528. }
  8529. return 0;
  8530. }
  8531. script_pushint(st,0);
  8532. return 0;
  8533. }
  8534. BUILDIN_FUNC(setcastledata)
  8535. {
  8536. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8537. int index = script_getnum(st,3);
  8538. int value = script_getnum(st,4);
  8539. struct guild_castle* gc = guild_mapname2gc(mapname);
  8540. if(gc) {
  8541. // Save Data byself First
  8542. switch(index){
  8543. case 1:
  8544. gc->guild_id = value; break;
  8545. case 2:
  8546. gc->economy = value; break;
  8547. case 3:
  8548. gc->defense = value; break;
  8549. case 4:
  8550. gc->triggerE = value; break;
  8551. case 5:
  8552. gc->triggerD = value; break;
  8553. case 6:
  8554. gc->nextTime = value; break;
  8555. case 7:
  8556. gc->payTime = value; break;
  8557. case 8:
  8558. gc->createTime = value; break;
  8559. case 9:
  8560. gc->visibleC = value; break;
  8561. case 10:
  8562. case 11:
  8563. case 12:
  8564. case 13:
  8565. case 14:
  8566. case 15:
  8567. case 16:
  8568. case 17:
  8569. gc->guardian[index-10].visible = value; break;
  8570. default:
  8571. return 0;
  8572. }
  8573. guild_castledatasave(gc->castle_id,index,value);
  8574. }
  8575. return 0;
  8576. }
  8577. /* =====================================================================
  8578. * ギルド情報を要求する
  8579. * ---------------------------------------------------------------------*/
  8580. BUILDIN_FUNC(requestguildinfo)
  8581. {
  8582. int guild_id=script_getnum(st,2);
  8583. const char *event=NULL;
  8584. if( script_hasdata(st,3) ){
  8585. event=script_getstr(st,3);
  8586. check_event(st, event);
  8587. }
  8588. if(guild_id>0)
  8589. guild_npc_request_info(guild_id,event);
  8590. return 0;
  8591. }
  8592. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8593. /// getequipcardcnt(<equipment slot>);
  8594. BUILDIN_FUNC(getequipcardcnt)
  8595. {
  8596. int i=-1,j,num;
  8597. TBL_PC *sd;
  8598. int count;
  8599. num=script_getnum(st,2);
  8600. sd=script_rid2sd(st);
  8601. if (num > 0 && num <= ARRAYLENGTH(equip))
  8602. i=pc_checkequip(sd,equip[num-1]);
  8603. if (i < 0 || !sd->inventory_data[i]) {
  8604. script_pushint(st,0);
  8605. return 0;
  8606. }
  8607. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8608. {
  8609. script_pushint(st,0);
  8610. return 0;
  8611. }
  8612. count = 0;
  8613. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  8614. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8615. count++;
  8616. script_pushint(st,count);
  8617. return 0;
  8618. }
  8619. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8620. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8621. /// successremovecards <slot>;
  8622. BUILDIN_FUNC(successremovecards)
  8623. {
  8624. int i=-1,j,c,cardflag=0;
  8625. TBL_PC* sd = script_rid2sd(st);
  8626. int num = script_getnum(st,2);
  8627. if (num > 0 && num <= ARRAYLENGTH(equip))
  8628. i=pc_checkequip(sd,equip[num-1]);
  8629. if (i < 0 || !sd->inventory_data[i]) {
  8630. return 0;
  8631. }
  8632. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8633. return 0;
  8634. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8635. {
  8636. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8637. {// extract this card from the item
  8638. int flag;
  8639. struct item item_tmp;
  8640. cardflag = 1;
  8641. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8642. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8643. item_tmp.attribute=0,item_tmp.expire_time=0;
  8644. for (j = 0; j < MAX_SLOTS; j++)
  8645. item_tmp.card[j]=0;
  8646. //Logs items, got from (N)PC scripts [Lupus]
  8647. if(log_config.enable_logs&0x40)
  8648. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8649. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8650. clif_additem(sd,0,0,flag);
  8651. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8652. }
  8653. }
  8654. }
  8655. if(cardflag == 1)
  8656. { // カードを取り除いたアイテム所得
  8657. int flag;
  8658. struct item item_tmp;
  8659. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8660. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8661. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8662. for (j = 0; j < MAX_SLOTS; j++)
  8663. item_tmp.card[j]=0;
  8664. //Logs items, got from (N)PC scripts [Lupus]
  8665. if(log_config.enable_logs&0x40)
  8666. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8667. pc_delitem(sd,i,1,0);
  8668. //Logs items, got from (N)PC scripts [Lupus]
  8669. if(log_config.enable_logs&0x40)
  8670. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8671. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8672. clif_additem(sd,0,0,flag);
  8673. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8674. }
  8675. clif_misceffect(&sd->bl,3);
  8676. }
  8677. return 0;
  8678. }
  8679. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8680. /// failedremovecards <slot>, <type>;
  8681. /// <type>=0 : will destroy both the item and the cards.
  8682. /// <type>=1 : will keep the item, but destroy the cards.
  8683. /// <type>=2 : will keep the cards, but destroy the item.
  8684. /// <type>=? : will just display the failure effect.
  8685. BUILDIN_FUNC(failedremovecards)
  8686. {
  8687. int i=-1,j,c,cardflag=0;
  8688. TBL_PC* sd = script_rid2sd(st);
  8689. int num = script_getnum(st,2);
  8690. int typefail = script_getnum(st,3);
  8691. if (num > 0 && num <= ARRAYLENGTH(equip))
  8692. i=pc_checkequip(sd,equip[num-1]);
  8693. if (i < 0 || !sd->inventory_data[i])
  8694. return 0;
  8695. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8696. return 0;
  8697. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8698. {
  8699. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8700. {
  8701. cardflag = 1;
  8702. if(typefail == 2)
  8703. {// add cards to inventory, clear
  8704. int flag;
  8705. struct item item_tmp;
  8706. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8707. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8708. item_tmp.attribute=0,item_tmp.expire_time=0;
  8709. for (j = 0; j < MAX_SLOTS; j++)
  8710. item_tmp.card[j]=0;
  8711. //Logs items, got from (N)PC scripts [Lupus]
  8712. if(log_config.enable_logs&0x40)
  8713. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8714. if((flag=pc_additem(sd,&item_tmp,1))){
  8715. clif_additem(sd,0,0,flag);
  8716. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8717. }
  8718. }
  8719. }
  8720. }
  8721. if(cardflag == 1)
  8722. {
  8723. if(typefail == 0 || typefail == 2){ // 武具損失
  8724. //Logs items, got from (N)PC scripts [Lupus]
  8725. if(log_config.enable_logs&0x40)
  8726. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8727. pc_delitem(sd,i,1,0);
  8728. }
  8729. if(typefail == 1){ // カードのみ損失(武具を返す)
  8730. int flag;
  8731. struct item item_tmp;
  8732. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8733. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8734. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8735. //Logs items, got from (N)PC scripts [Lupus]
  8736. if(log_config.enable_logs&0x40)
  8737. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8738. for (j = 0; j < MAX_SLOTS; j++)
  8739. item_tmp.card[j]=0;
  8740. pc_delitem(sd,i,1,0);
  8741. //Logs items, got from (N)PC scripts [Lupus]
  8742. if(log_config.enable_logs&0x40)
  8743. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8744. if((flag=pc_additem(sd,&item_tmp,1))){
  8745. clif_additem(sd,0,0,flag);
  8746. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8747. }
  8748. }
  8749. clif_misceffect(&sd->bl,2);
  8750. }
  8751. return 0;
  8752. }
  8753. /* ================================================================
  8754. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8755. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8756. * improved by [Lance]
  8757. * ================================================================*/
  8758. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8759. {
  8760. int x,y,m,check_val=0,check_ID=0,i=0;
  8761. struct guild *g = NULL;
  8762. struct party_data *p = NULL;
  8763. const char *str;
  8764. const char *mapname;
  8765. unsigned int index;
  8766. mapname=script_getstr(st,2);
  8767. str=script_getstr(st,3);
  8768. x=script_getnum(st,4);
  8769. y=script_getnum(st,5);
  8770. if(script_hasdata(st,7)){
  8771. check_val=script_getnum(st,6);
  8772. check_ID=script_getnum(st,7);
  8773. }
  8774. if((m=map_mapname2mapid(mapname))< 0)
  8775. return 0;
  8776. if(!(index=mapindex_name2id(str)))
  8777. return 0;
  8778. switch(check_val){
  8779. case 1:
  8780. g = guild_search(check_ID);
  8781. if (g){
  8782. for( i=0; i < g->max_member; i++)
  8783. {
  8784. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8785. pc_setpos(g->member[i].sd,index,x,y,3);
  8786. }
  8787. }
  8788. }
  8789. break;
  8790. case 2:
  8791. p = party_search(check_ID);
  8792. if(p){
  8793. for(i=0;i<MAX_PARTY; i++){
  8794. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8795. pc_setpos(p->data[i].sd,index,x,y,3);
  8796. }
  8797. }
  8798. }
  8799. break;
  8800. default:
  8801. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8802. break;
  8803. }
  8804. return 0;
  8805. }
  8806. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8807. {
  8808. char *event=va_arg(ap,char *);
  8809. struct mob_data *md = ((struct mob_data *)bl);
  8810. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  8811. return 1;
  8812. return 0;
  8813. }
  8814. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8815. {
  8816. const char *mapname,*event;
  8817. int m;
  8818. mapname=script_getstr(st,2);
  8819. event=script_getstr(st,3);
  8820. check_event(st, event);
  8821. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  8822. script_pushint(st,-1);
  8823. return 0;
  8824. }
  8825. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  8826. {
  8827. script_pushint(st,-1);
  8828. return 0;
  8829. }
  8830. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8831. return 0;
  8832. }
  8833. BUILDIN_FUNC(marriage)
  8834. {
  8835. const char *partner=script_getstr(st,2);
  8836. TBL_PC *sd=script_rid2sd(st);
  8837. TBL_PC *p_sd=map_nick2sd(partner);
  8838. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8839. script_pushint(st,0);
  8840. return 0;
  8841. }
  8842. script_pushint(st,1);
  8843. return 0;
  8844. }
  8845. BUILDIN_FUNC(wedding_effect)
  8846. {
  8847. TBL_PC *sd=script_rid2sd(st);
  8848. struct block_list *bl;
  8849. if(sd==NULL) {
  8850. bl=map_id2bl(st->oid);
  8851. } else
  8852. bl=&sd->bl;
  8853. clif_wedding_effect(bl);
  8854. return 0;
  8855. }
  8856. BUILDIN_FUNC(divorce)
  8857. {
  8858. TBL_PC *sd=script_rid2sd(st);
  8859. if(sd==NULL || pc_divorce(sd) < 0){
  8860. script_pushint(st,0);
  8861. return 0;
  8862. }
  8863. script_pushint(st,1);
  8864. return 0;
  8865. }
  8866. BUILDIN_FUNC(ispartneron)
  8867. {
  8868. TBL_PC *sd=script_rid2sd(st);
  8869. TBL_PC *p_sd=NULL;
  8870. if(sd==NULL || !pc_ismarried(sd) ||
  8871. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8872. script_pushint(st,0);
  8873. return 0;
  8874. }
  8875. script_pushint(st,1);
  8876. return 0;
  8877. }
  8878. BUILDIN_FUNC(getpartnerid)
  8879. {
  8880. TBL_PC *sd=script_rid2sd(st);
  8881. if (sd == NULL) {
  8882. script_pushint(st,0);
  8883. return 0;
  8884. }
  8885. script_pushint(st,sd->status.partner_id);
  8886. return 0;
  8887. }
  8888. BUILDIN_FUNC(getchildid)
  8889. {
  8890. TBL_PC *sd=script_rid2sd(st);
  8891. if (sd == NULL) {
  8892. script_pushint(st,0);
  8893. return 0;
  8894. }
  8895. script_pushint(st,sd->status.child);
  8896. return 0;
  8897. }
  8898. BUILDIN_FUNC(getmotherid)
  8899. {
  8900. TBL_PC *sd=script_rid2sd(st);
  8901. if (sd == NULL) {
  8902. script_pushint(st,0);
  8903. return 0;
  8904. }
  8905. script_pushint(st,sd->status.mother);
  8906. return 0;
  8907. }
  8908. BUILDIN_FUNC(getfatherid)
  8909. {
  8910. TBL_PC *sd=script_rid2sd(st);
  8911. if (sd == NULL) {
  8912. script_pushint(st,0);
  8913. return 0;
  8914. }
  8915. script_pushint(st,sd->status.father);
  8916. return 0;
  8917. }
  8918. BUILDIN_FUNC(warppartner)
  8919. {
  8920. int x,y;
  8921. unsigned short mapindex;
  8922. const char *str;
  8923. TBL_PC *sd=script_rid2sd(st);
  8924. TBL_PC *p_sd=NULL;
  8925. if(sd==NULL || !pc_ismarried(sd) ||
  8926. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8927. script_pushint(st,0);
  8928. return 0;
  8929. }
  8930. str=script_getstr(st,2);
  8931. x=script_getnum(st,3);
  8932. y=script_getnum(st,4);
  8933. mapindex = mapindex_name2id(str);
  8934. if (mapindex) {
  8935. pc_setpos(p_sd,mapindex,x,y,0);
  8936. script_pushint(st,1);
  8937. } else
  8938. script_pushint(st,0);
  8939. return 0;
  8940. }
  8941. /*================================================
  8942. * Script for Displaying MOB Information [Valaris]
  8943. *------------------------------------------------*/
  8944. BUILDIN_FUNC(strmobinfo)
  8945. {
  8946. int num=script_getnum(st,2);
  8947. int class_=script_getnum(st,3);
  8948. if(!mobdb_checkid(class_))
  8949. {
  8950. script_pushint(st,0);
  8951. return 0;
  8952. }
  8953. switch (num) {
  8954. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8955. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8956. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8957. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8958. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8959. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8960. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8961. default:
  8962. script_pushint(st,0);
  8963. break;
  8964. }
  8965. return 0;
  8966. }
  8967. /*==========================================
  8968. * Summon guardians [Valaris]
  8969. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8970. *------------------------------------------*/
  8971. BUILDIN_FUNC(guardian)
  8972. {
  8973. int class_=0,x=0,y=0,guardian=0;
  8974. const char *str,*map,*evt="";
  8975. struct script_data *data;
  8976. bool has_index = false;
  8977. map =script_getstr(st,2);
  8978. x =script_getnum(st,3);
  8979. y =script_getnum(st,4);
  8980. str =script_getstr(st,5);
  8981. class_=script_getnum(st,6);
  8982. if( script_hasdata(st,8) )
  8983. {// "<event label>",<guardian index>
  8984. evt=script_getstr(st,7);
  8985. guardian=script_getnum(st,8);
  8986. has_index = true;
  8987. } else if( script_hasdata(st,7) ){
  8988. data=script_getdata(st,7);
  8989. get_val(st,data);
  8990. if( data_isstring(data) )
  8991. {// "<event label>"
  8992. evt=script_getstr(st,7);
  8993. } else if( data_isint(data) )
  8994. {// <guardian index>
  8995. guardian=script_getnum(st,7);
  8996. has_index = true;
  8997. } else {
  8998. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8999. script_reportdata(data);
  9000. return 1;
  9001. }
  9002. }
  9003. check_event(st, evt);
  9004. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9005. return 0;
  9006. }
  9007. /*==========================================
  9008. * Invisible Walls [Zephyrus]
  9009. *------------------------------------------*/
  9010. BUILDIN_FUNC(setwall)
  9011. {
  9012. const char *map, *name;
  9013. int x, y, m, size, dir;
  9014. bool shootable;
  9015. map = script_getstr(st,2);
  9016. x = script_getnum(st,3);
  9017. y = script_getnum(st,4);
  9018. size = script_getnum(st,5);
  9019. dir = script_getnum(st,6);
  9020. shootable = script_getnum(st,7);
  9021. name = script_getstr(st,8);
  9022. if( (m = map_mapname2mapid(map)) < 0 )
  9023. return 0; // Invalid Map
  9024. map_iwall_set(m, x, y, size, dir, shootable, name);
  9025. return 0;
  9026. }
  9027. BUILDIN_FUNC(delwall)
  9028. {
  9029. const char *name = script_getstr(st,2);
  9030. map_iwall_remove(name);
  9031. return 0;
  9032. }
  9033. /// Retrieves various information about the specified guardian.
  9034. ///
  9035. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9036. /// type: 0 - whether it is deployed or not
  9037. /// 1 - maximum hp
  9038. /// 2 - current hp
  9039. ///
  9040. BUILDIN_FUNC(guardianinfo)
  9041. {
  9042. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9043. int id = script_getnum(st,3);
  9044. int type = script_getnum(st,4);
  9045. struct guild_castle* gc = guild_mapname2gc(mapname);
  9046. struct mob_data* gd;
  9047. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9048. {
  9049. script_pushint(st,-1);
  9050. return 0;
  9051. }
  9052. if( type == 0 )
  9053. script_pushint(st, gc->guardian[id].visible);
  9054. else
  9055. if( !gc->guardian[id].visible )
  9056. script_pushint(st,-1);
  9057. else
  9058. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9059. script_pushint(st,-1);
  9060. else
  9061. {
  9062. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9063. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9064. else
  9065. script_pushint(st,-1);
  9066. }
  9067. return 0;
  9068. }
  9069. /*==========================================
  9070. * IDからItem名
  9071. *------------------------------------------*/
  9072. BUILDIN_FUNC(getitemname)
  9073. {
  9074. int item_id=0;
  9075. struct item_data *i_data;
  9076. char *item_name;
  9077. struct script_data *data;
  9078. data=script_getdata(st,2);
  9079. get_val(st,data);
  9080. if( data_isstring(data) ){
  9081. const char *name=conv_str(st,data);
  9082. struct item_data *item_data = itemdb_searchname(name);
  9083. if( item_data )
  9084. item_id=item_data->nameid;
  9085. }else
  9086. item_id=conv_num(st,data);
  9087. i_data = itemdb_exists(item_id);
  9088. if (i_data == NULL)
  9089. {
  9090. script_pushconststr(st,"null");
  9091. return 0;
  9092. }
  9093. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9094. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9095. script_pushstr(st,item_name);
  9096. return 0;
  9097. }
  9098. /*==========================================
  9099. * Returns number of slots an item has. [Skotlex]
  9100. *------------------------------------------*/
  9101. BUILDIN_FUNC(getitemslots)
  9102. {
  9103. int item_id;
  9104. struct item_data *i_data;
  9105. item_id=script_getnum(st,2);
  9106. i_data = itemdb_exists(item_id);
  9107. if (i_data)
  9108. script_pushint(st,i_data->slot);
  9109. else
  9110. script_pushint(st,-1);
  9111. return 0;
  9112. }
  9113. /*==========================================
  9114. * Returns some values of an item [Lupus]
  9115. * Price, Weight, etc...
  9116. getiteminfo(itemID,n), where n
  9117. 0 value_buy;
  9118. 1 value_sell;
  9119. 2 type;
  9120. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9121. if = 0, then monsters don't drop it at all (rare or a quest item)
  9122. if = 10000, then this item is sold in NPC shops only
  9123. 4 sex;
  9124. 5 equip;
  9125. 6 weight;
  9126. 7 atk;
  9127. 8 def;
  9128. 9 range;
  9129. 10 slot;
  9130. 11 look;
  9131. 12 elv;
  9132. 13 wlv;
  9133. 14 view id
  9134. *------------------------------------------*/
  9135. BUILDIN_FUNC(getiteminfo)
  9136. {
  9137. int item_id,n;
  9138. int *item_arr;
  9139. struct item_data *i_data;
  9140. item_id = script_getnum(st,2);
  9141. n = script_getnum(st,3);
  9142. i_data = itemdb_exists(item_id);
  9143. if (i_data && n>=0 && n<14) {
  9144. item_arr = (int*)&i_data->value_buy;
  9145. script_pushint(st,item_arr[n]);
  9146. } else
  9147. script_pushint(st,-1);
  9148. return 0;
  9149. }
  9150. /*==========================================
  9151. * Set some values of an item [Lupus]
  9152. * Price, Weight, etc...
  9153. setiteminfo(itemID,n,Value), where n
  9154. 0 value_buy;
  9155. 1 value_sell;
  9156. 2 type;
  9157. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9158. if = 0, then monsters don't drop it at all (rare or a quest item)
  9159. if = 10000, then this item is sold in NPC shops only
  9160. 4 sex;
  9161. 5 equip;
  9162. 6 weight;
  9163. 7 atk;
  9164. 8 def;
  9165. 9 range;
  9166. 10 slot;
  9167. 11 look;
  9168. 12 elv;
  9169. 13 wlv;
  9170. 14 view id
  9171. * Returns Value or -1 if the wrong field's been set
  9172. *------------------------------------------*/
  9173. BUILDIN_FUNC(setiteminfo)
  9174. {
  9175. int item_id,n,value;
  9176. int *item_arr;
  9177. struct item_data *i_data;
  9178. item_id = script_getnum(st,2);
  9179. n = script_getnum(st,3);
  9180. value = script_getnum(st,4);
  9181. i_data = itemdb_exists(item_id);
  9182. if (i_data && n>=0 && n<14) {
  9183. item_arr = (int*)&i_data->value_buy;
  9184. item_arr[n] = value;
  9185. script_pushint(st,value);
  9186. } else
  9187. script_pushint(st,-1);
  9188. return 0;
  9189. }
  9190. /*==========================================
  9191. * Returns value from equipped item slot n [Lupus]
  9192. getequipcardid(num,slot)
  9193. where
  9194. num = eqip position slot
  9195. slot = 0,1,2,3 (Card Slot N)
  9196. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9197. it's useful when you want to check item cards or if it's signed
  9198. Useful for such quests as "Sign this refined item with players name" etc
  9199. Hat[0] +4 -> Player's Hat[0] +4
  9200. *------------------------------------------*/
  9201. BUILDIN_FUNC(getequipcardid)
  9202. {
  9203. int i=-1,num,slot;
  9204. TBL_PC *sd;
  9205. num=script_getnum(st,2);
  9206. slot=script_getnum(st,3);
  9207. sd=script_rid2sd(st);
  9208. if (num > 0 && num <= ARRAYLENGTH(equip))
  9209. i=pc_checkequip(sd,equip[num-1]);
  9210. if(i >= 0 && slot>=0 && slot<4)
  9211. script_pushint(st,sd->status.inventory[i].card[slot]);
  9212. else
  9213. script_pushint(st,0);
  9214. return 0;
  9215. }
  9216. /*==========================================
  9217. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9218. *------------------------------------------*/
  9219. BUILDIN_FUNC(petskillbonus)
  9220. {
  9221. struct pet_data *pd;
  9222. TBL_PC *sd=script_rid2sd(st);
  9223. if(sd==NULL || sd->pd==NULL)
  9224. return 0;
  9225. pd=sd->pd;
  9226. if (pd->bonus)
  9227. { //Clear previous bonus
  9228. if (pd->bonus->timer != -1)
  9229. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9230. } else //init
  9231. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9232. pd->bonus->type=script_getnum(st,2);
  9233. pd->bonus->val=script_getnum(st,3);
  9234. pd->bonus->duration=script_getnum(st,4);
  9235. pd->bonus->delay=script_getnum(st,5);
  9236. if (pd->state.skillbonus == 1)
  9237. pd->state.skillbonus=0; // waiting state
  9238. // wait for timer to start
  9239. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9240. pd->bonus->timer = INVALID_TIMER;
  9241. else
  9242. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9243. return 0;
  9244. }
  9245. /*==========================================
  9246. * pet looting [Valaris] //Rewritten by [Skotlex]
  9247. *------------------------------------------*/
  9248. BUILDIN_FUNC(petloot)
  9249. {
  9250. int max;
  9251. struct pet_data *pd;
  9252. TBL_PC *sd=script_rid2sd(st);
  9253. if(sd==NULL || sd->pd==NULL)
  9254. return 0;
  9255. max=script_getnum(st,2);
  9256. if(max < 1)
  9257. max = 1; //Let'em loot at least 1 item.
  9258. else if (max > MAX_PETLOOT_SIZE)
  9259. max = MAX_PETLOOT_SIZE;
  9260. pd = sd->pd;
  9261. if (pd->loot != NULL)
  9262. { //Release whatever was there already and reallocate memory
  9263. pet_lootitem_drop(pd, pd->msd);
  9264. aFree(pd->loot->item);
  9265. }
  9266. else
  9267. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9268. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9269. pd->loot->max=max;
  9270. pd->loot->count = 0;
  9271. pd->loot->weight = 0;
  9272. return 0;
  9273. }
  9274. /*==========================================
  9275. * PCの所持品情報読み取り
  9276. *------------------------------------------*/
  9277. BUILDIN_FUNC(getinventorylist)
  9278. {
  9279. TBL_PC *sd=script_rid2sd(st);
  9280. char card_var[NAME_LENGTH];
  9281. int i,j=0,k;
  9282. if(!sd) return 0;
  9283. for(i=0;i<MAX_INVENTORY;i++){
  9284. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9285. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9286. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9287. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9288. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9289. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9290. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9291. for (k = 0; k < MAX_SLOTS; k++)
  9292. {
  9293. sprintf(card_var, "@inventorylist_card%d",k+1);
  9294. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9295. }
  9296. j++;
  9297. }
  9298. }
  9299. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9300. return 0;
  9301. }
  9302. BUILDIN_FUNC(getskilllist)
  9303. {
  9304. TBL_PC *sd=script_rid2sd(st);
  9305. int i,j=0;
  9306. if(!sd) return 0;
  9307. for(i=0;i<MAX_SKILL;i++){
  9308. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9309. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9310. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9311. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9312. j++;
  9313. }
  9314. }
  9315. pc_setreg(sd,add_str("@skilllist_count"),j);
  9316. return 0;
  9317. }
  9318. BUILDIN_FUNC(clearitem)
  9319. {
  9320. TBL_PC *sd=script_rid2sd(st);
  9321. int i;
  9322. if(sd==NULL) return 0;
  9323. for (i=0; i<MAX_INVENTORY; i++) {
  9324. if (sd->status.inventory[i].amount) {
  9325. //Logs items, got from (N)PC scripts [Lupus]
  9326. if(log_config.enable_logs&0x40)
  9327. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9328. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9329. }
  9330. }
  9331. return 0;
  9332. }
  9333. /*==========================================
  9334. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9335. *------------------------------------------*/
  9336. BUILDIN_FUNC(disguise)
  9337. {
  9338. int id;
  9339. TBL_PC* sd = script_rid2sd(st);
  9340. if (sd == NULL) return 0;
  9341. id = script_getnum(st,2);
  9342. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9343. pc_disguise(sd, id);
  9344. script_pushint(st,id);
  9345. } else
  9346. script_pushint(st,0);
  9347. return 0;
  9348. }
  9349. /*==========================================
  9350. * Undisguise Player (returns 1 if success, 0 on fail)
  9351. *------------------------------------------*/
  9352. BUILDIN_FUNC(undisguise)
  9353. {
  9354. TBL_PC* sd = script_rid2sd(st);
  9355. if (sd == NULL) return 0;
  9356. if (sd->disguise) {
  9357. pc_disguise(sd, 0);
  9358. script_pushint(st,0);
  9359. } else {
  9360. script_pushint(st,1);
  9361. }
  9362. return 0;
  9363. }
  9364. /*==========================================
  9365. * NPCクラスチェンジ
  9366. * classは変わりたいclass
  9367. * typeは通常0なのかな?
  9368. *------------------------------------------*/
  9369. BUILDIN_FUNC(classchange)
  9370. {
  9371. int _class,type;
  9372. struct block_list *bl=map_id2bl(st->oid);
  9373. if(bl==NULL) return 0;
  9374. _class=script_getnum(st,2);
  9375. type=script_getnum(st,3);
  9376. clif_class_change(bl,_class,type);
  9377. return 0;
  9378. }
  9379. /*==========================================
  9380. * NPCから発生するエフェクト
  9381. *------------------------------------------*/
  9382. BUILDIN_FUNC(misceffect)
  9383. {
  9384. int type;
  9385. type=script_getnum(st,2);
  9386. if(st->oid && st->oid != fake_nd->bl.id) {
  9387. struct block_list *bl = map_id2bl(st->oid);
  9388. if (bl)
  9389. clif_misceffect2(bl,type);
  9390. } else{
  9391. TBL_PC *sd=script_rid2sd(st);
  9392. if(sd)
  9393. clif_misceffect2(&sd->bl,type);
  9394. }
  9395. return 0;
  9396. }
  9397. /*==========================================
  9398. * サウンドエフェクト
  9399. *------------------------------------------*/
  9400. BUILDIN_FUNC(soundeffect)
  9401. {
  9402. TBL_PC* sd = script_rid2sd(st);
  9403. const char* name = script_getstr(st,2);
  9404. int type = script_getnum(st,3);
  9405. if(sd)
  9406. {
  9407. if(!st->rid)
  9408. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9409. else
  9410. clif_soundeffect(sd,&sd->bl,name,type);
  9411. }
  9412. return 0;
  9413. }
  9414. int soundeffect_sub(struct block_list* bl,va_list ap)
  9415. {
  9416. char* name = va_arg(ap,char*);
  9417. int type = va_arg(ap,int);
  9418. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9419. return 0;
  9420. }
  9421. /*==========================================
  9422. * Play a sound effect (.wav) on multiple clients
  9423. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9424. *------------------------------------------*/
  9425. BUILDIN_FUNC(soundeffectall)
  9426. {
  9427. struct block_list* bl;
  9428. const char* name;
  9429. int type;
  9430. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9431. if (!bl)
  9432. return 0;
  9433. name = script_getstr(st,2);
  9434. type = script_getnum(st,3);
  9435. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9436. if(!script_hasdata(st,4))
  9437. { // area around
  9438. clif_soundeffectall(bl, name, type, AREA);
  9439. }
  9440. else
  9441. if(!script_hasdata(st,5))
  9442. { // entire map
  9443. const char* map = script_getstr(st,4);
  9444. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9445. }
  9446. else
  9447. if(script_hasdata(st,8))
  9448. { // specified part of map
  9449. const char* map = script_getstr(st,4);
  9450. int x0 = script_getnum(st,5);
  9451. int y0 = script_getnum(st,6);
  9452. int x1 = script_getnum(st,7);
  9453. int y1 = script_getnum(st,8);
  9454. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9455. }
  9456. else
  9457. {
  9458. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9459. }
  9460. return 0;
  9461. }
  9462. /*==========================================
  9463. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9464. *------------------------------------------*/
  9465. BUILDIN_FUNC(petrecovery)
  9466. {
  9467. struct pet_data *pd;
  9468. TBL_PC *sd=script_rid2sd(st);
  9469. if(sd==NULL || sd->pd==NULL)
  9470. return 0;
  9471. pd=sd->pd;
  9472. if (pd->recovery)
  9473. { //Halt previous bonus
  9474. if (pd->recovery->timer != -1)
  9475. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9476. } else //Init
  9477. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9478. pd->recovery->type = (sc_type)script_getnum(st,2);
  9479. pd->recovery->delay = script_getnum(st,3);
  9480. pd->recovery->timer = INVALID_TIMER;
  9481. return 0;
  9482. }
  9483. /*==========================================
  9484. * pet healing [Valaris] //Rewritten by [Skotlex]
  9485. *------------------------------------------*/
  9486. BUILDIN_FUNC(petheal)
  9487. {
  9488. struct pet_data *pd;
  9489. TBL_PC *sd=script_rid2sd(st);
  9490. if(sd==NULL || sd->pd==NULL)
  9491. return 0;
  9492. pd=sd->pd;
  9493. if (pd->s_skill)
  9494. { //Clear previous skill
  9495. if (pd->s_skill->timer != -1)
  9496. {
  9497. if (pd->s_skill->id)
  9498. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9499. else
  9500. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9501. }
  9502. } else //init memory
  9503. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9504. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9505. //Use the lv as the amount to heal
  9506. pd->s_skill->lv=script_getnum(st,2);
  9507. pd->s_skill->delay=script_getnum(st,3);
  9508. pd->s_skill->hp=script_getnum(st,4);
  9509. pd->s_skill->sp=script_getnum(st,5);
  9510. //Use delay as initial offset to avoid skill/heal exploits
  9511. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9512. pd->s_skill->timer = INVALID_TIMER;
  9513. else
  9514. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9515. return 0;
  9516. }
  9517. /*==========================================
  9518. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9519. *------------------------------------------*/
  9520. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9521. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9522. BUILDIN_FUNC(petskillattack)
  9523. {
  9524. struct pet_data *pd;
  9525. TBL_PC *sd=script_rid2sd(st);
  9526. if(sd==NULL || sd->pd==NULL)
  9527. return 0;
  9528. pd=sd->pd;
  9529. if (pd->a_skill == NULL)
  9530. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9531. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9532. pd->a_skill->lv=script_getnum(st,3);
  9533. pd->a_skill->div_ = 0;
  9534. pd->a_skill->rate=script_getnum(st,4);
  9535. pd->a_skill->bonusrate=script_getnum(st,5);
  9536. return 0;
  9537. }
  9538. /*==========================================
  9539. * pet attack skills [Valaris]
  9540. *------------------------------------------*/
  9541. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9542. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9543. BUILDIN_FUNC(petskillattack2)
  9544. {
  9545. struct pet_data *pd;
  9546. TBL_PC *sd=script_rid2sd(st);
  9547. if(sd==NULL || sd->pd==NULL)
  9548. return 0;
  9549. pd=sd->pd;
  9550. if (pd->a_skill == NULL)
  9551. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9552. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9553. pd->a_skill->lv=script_getnum(st,3);
  9554. pd->a_skill->div_ = script_getnum(st,4);
  9555. pd->a_skill->rate=script_getnum(st,5);
  9556. pd->a_skill->bonusrate=script_getnum(st,6);
  9557. return 0;
  9558. }
  9559. /*==========================================
  9560. * pet support skills [Skotlex]
  9561. *------------------------------------------*/
  9562. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9563. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9564. BUILDIN_FUNC(petskillsupport)
  9565. {
  9566. struct pet_data *pd;
  9567. TBL_PC *sd=script_rid2sd(st);
  9568. if(sd==NULL || sd->pd==NULL)
  9569. return 0;
  9570. pd=sd->pd;
  9571. if (pd->s_skill)
  9572. { //Clear previous skill
  9573. if (pd->s_skill->timer != -1)
  9574. {
  9575. if (pd->s_skill->id)
  9576. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9577. else
  9578. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9579. }
  9580. } else //init memory
  9581. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9582. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9583. pd->s_skill->lv=script_getnum(st,3);
  9584. pd->s_skill->delay=script_getnum(st,4);
  9585. pd->s_skill->hp=script_getnum(st,5);
  9586. pd->s_skill->sp=script_getnum(st,6);
  9587. //Use delay as initial offset to avoid skill/heal exploits
  9588. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9589. pd->s_skill->timer = INVALID_TIMER;
  9590. else
  9591. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9592. return 0;
  9593. }
  9594. /*==========================================
  9595. * Scripted skill effects [Celest]
  9596. *------------------------------------------*/
  9597. /// skilleffect <skill id>,<level>
  9598. /// skilleffect "<skill name>",<level>
  9599. BUILDIN_FUNC(skilleffect)
  9600. {
  9601. TBL_PC *sd;
  9602. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9603. int skilllv=script_getnum(st,3);
  9604. sd=script_rid2sd(st);
  9605. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9606. return 0;
  9607. }
  9608. /*==========================================
  9609. * NPC skill effects [Valaris]
  9610. *------------------------------------------*/
  9611. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9612. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9613. BUILDIN_FUNC(npcskilleffect)
  9614. {
  9615. struct block_list *bl= map_id2bl(st->oid);
  9616. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9617. int skilllv=script_getnum(st,3);
  9618. int x=script_getnum(st,4);
  9619. int y=script_getnum(st,5);
  9620. if (bl)
  9621. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9622. return 0;
  9623. }
  9624. /*==========================================
  9625. * Special effects [Valaris]
  9626. *------------------------------------------*/
  9627. BUILDIN_FUNC(specialeffect)
  9628. {
  9629. struct block_list *bl=map_id2bl(st->oid);
  9630. int type = script_getnum(st,2);
  9631. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9632. if(bl==NULL)
  9633. return 0;
  9634. clif_specialeffect(bl, type, target);
  9635. return 0;
  9636. }
  9637. BUILDIN_FUNC(specialeffect2)
  9638. {
  9639. TBL_PC *sd=script_rid2sd(st);
  9640. int type = script_getnum(st,2);
  9641. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9642. if(sd==NULL)
  9643. return 0;
  9644. clif_specialeffect(&sd->bl, type, target);
  9645. return 0;
  9646. }
  9647. /*==========================================
  9648. * Nude [Valaris]
  9649. *------------------------------------------*/
  9650. BUILDIN_FUNC(nude)
  9651. {
  9652. TBL_PC *sd=script_rid2sd(st);
  9653. int i,calcflag=0;
  9654. if(sd==NULL)
  9655. return 0;
  9656. for(i=0;i<11;i++)
  9657. if(sd->equip_index[i] >= 0) {
  9658. if(!calcflag)
  9659. calcflag=1;
  9660. pc_unequipitem(sd,sd->equip_index[i],2);
  9661. }
  9662. if(calcflag)
  9663. status_calc_pc(sd,0);
  9664. return 0;
  9665. }
  9666. /*==========================================
  9667. * gmcommand [MouseJstr]
  9668. *
  9669. * suggested on the forums...
  9670. * splitted into atcommand & charcommand by [Skotlex]
  9671. *------------------------------------------*/
  9672. BUILDIN_FUNC(atcommand)
  9673. {
  9674. TBL_PC dummy_sd;
  9675. TBL_PC* sd;
  9676. int fd;
  9677. const char* cmd;
  9678. cmd = script_getstr(st,2);
  9679. if (st->rid) {
  9680. sd = script_rid2sd(st);
  9681. fd = sd->fd;
  9682. } else { //Use a dummy character.
  9683. sd = &dummy_sd;
  9684. fd = 0;
  9685. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9686. if (st->oid)
  9687. {
  9688. struct block_list* bl = map_id2bl(st->oid);
  9689. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9690. if (bl->type == BL_NPC)
  9691. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9692. }
  9693. }
  9694. // compatibility with previous implementation (deprecated!)
  9695. if(cmd[0] != atcommand_symbol)
  9696. {
  9697. cmd += strlen(sd->status.name);
  9698. while(*cmd != atcommand_symbol && *cmd != 0)
  9699. cmd++;
  9700. }
  9701. is_atcommand(fd, sd, cmd, 0);
  9702. return 0;
  9703. }
  9704. BUILDIN_FUNC(charcommand)
  9705. {
  9706. TBL_PC dummy_sd;
  9707. TBL_PC* sd;
  9708. int fd;
  9709. const char* cmd;
  9710. cmd = script_getstr(st,2);
  9711. if (st->rid) {
  9712. sd = script_rid2sd(st);
  9713. fd = sd->fd;
  9714. } else { //Use a dummy character.
  9715. sd = &dummy_sd;
  9716. fd = 0;
  9717. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9718. if (st->oid)
  9719. {
  9720. struct block_list* bl = map_id2bl(st->oid);
  9721. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9722. if (bl->type == BL_NPC)
  9723. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9724. }
  9725. }
  9726. if (*cmd != charcommand_symbol) {
  9727. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  9728. script_reportsrc(st);
  9729. return 1;
  9730. }
  9731. is_atcommand(fd, sd, cmd, 0);
  9732. return 0;
  9733. }
  9734. /*==========================================
  9735. * Displays a message for the player only (like system messages like "you got an apple" )
  9736. *------------------------------------------*/
  9737. BUILDIN_FUNC(dispbottom)
  9738. {
  9739. TBL_PC *sd=script_rid2sd(st);
  9740. const char *message;
  9741. message=script_getstr(st,2);
  9742. if(sd)
  9743. clif_disp_onlyself(sd,message,(int)strlen(message));
  9744. return 0;
  9745. }
  9746. /*==========================================
  9747. * All The Players Full Recovery
  9748. * (HP/SP full restore and resurrect if need)
  9749. *------------------------------------------*/
  9750. BUILDIN_FUNC(recovery)
  9751. {
  9752. TBL_PC* sd;
  9753. struct s_mapiterator* iter;
  9754. iter = mapit_getallusers();
  9755. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9756. {
  9757. if(pc_isdead(sd))
  9758. status_revive(&sd->bl, 100, 100);
  9759. else
  9760. status_percent_heal(&sd->bl, 100, 100);
  9761. clif_displaymessage(sd->fd,"You have been recovered!");
  9762. }
  9763. mapit_free(iter);
  9764. return 0;
  9765. }
  9766. /*==========================================
  9767. * Get your pet info: getpetinfo(n)
  9768. * n -> 0:pet_id 1:pet_class 2:pet_name
  9769. * 3:friendly 4:hungry, 5: rename flag.
  9770. *------------------------------------------*/
  9771. BUILDIN_FUNC(getpetinfo)
  9772. {
  9773. TBL_PC *sd=script_rid2sd(st);
  9774. TBL_PET *pd;
  9775. int type=script_getnum(st,2);
  9776. if(!sd || !sd->pd) {
  9777. if (type == 2)
  9778. script_pushconststr(st,"null");
  9779. else
  9780. script_pushint(st,0);
  9781. return 0;
  9782. }
  9783. pd = sd->pd;
  9784. switch(type){
  9785. case 0: script_pushint(st,pd->pet.pet_id); break;
  9786. case 1: script_pushint(st,pd->pet.class_); break;
  9787. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9788. case 3: script_pushint(st,pd->pet.intimate); break;
  9789. case 4: script_pushint(st,pd->pet.hungry); break;
  9790. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9791. default:
  9792. script_pushint(st,0);
  9793. break;
  9794. }
  9795. return 0;
  9796. }
  9797. /*==========================================
  9798. * Get your homunculus info: gethominfo(n)
  9799. * n -> 0:hom_id 1:class 2:name
  9800. * 3:friendly 4:hungry, 5: rename flag.
  9801. * 6: level
  9802. *------------------------------------------*/
  9803. BUILDIN_FUNC(gethominfo)
  9804. {
  9805. TBL_PC *sd=script_rid2sd(st);
  9806. TBL_HOM *hd;
  9807. int type=script_getnum(st,2);
  9808. hd = sd?sd->hd:NULL;
  9809. if(!merc_is_hom_active(hd))
  9810. {
  9811. if (type == 2)
  9812. script_pushconststr(st,"null");
  9813. else
  9814. script_pushint(st,0);
  9815. return 0;
  9816. }
  9817. switch(type){
  9818. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9819. case 1: script_pushint(st,hd->homunculus.class_); break;
  9820. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9821. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9822. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9823. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9824. case 6: script_pushint(st,hd->homunculus.level); break;
  9825. default:
  9826. script_pushint(st,0);
  9827. break;
  9828. }
  9829. return 0;
  9830. }
  9831. /*==========================================
  9832. * Shows wether your inventory(and equips) contain
  9833. selected card or not.
  9834. checkequipedcard(4001);
  9835. *------------------------------------------*/
  9836. BUILDIN_FUNC(checkequipedcard)
  9837. {
  9838. TBL_PC *sd=script_rid2sd(st);
  9839. int n,i,c=0;
  9840. c=script_getnum(st,2);
  9841. if(sd){
  9842. for(i=0;i<MAX_INVENTORY;i++){
  9843. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  9844. for(n=0;n<sd->inventory_data[i]->slot;n++){
  9845. if(sd->status.inventory[i].card[n]==c){
  9846. script_pushint(st,1);
  9847. return 0;
  9848. }
  9849. }
  9850. }
  9851. }
  9852. }
  9853. script_pushint(st,0);
  9854. return 0;
  9855. }
  9856. BUILDIN_FUNC(jump_zero)
  9857. {
  9858. int sel;
  9859. sel=script_getnum(st,2);
  9860. if(!sel) {
  9861. int pos;
  9862. if( !data_islabel(script_getdata(st,3)) ){
  9863. ShowError("script: jump_zero: not label !\n");
  9864. st->state=END;
  9865. return 1;
  9866. }
  9867. pos=script_getnum(st,3);
  9868. st->pos=pos;
  9869. st->state=GOTO;
  9870. // printf("script: jump_zero: jumpto : %d\n",pos);
  9871. } else {
  9872. // printf("script: jump_zero: fail\n");
  9873. }
  9874. return 0;
  9875. }
  9876. /*==========================================
  9877. * GetMapMobs
  9878. returns mob counts on a set map:
  9879. e.g. GetMapMobs("prontera")
  9880. use "this" - for player's map
  9881. *------------------------------------------*/
  9882. BUILDIN_FUNC(getmapmobs)
  9883. {
  9884. const char *str=NULL;
  9885. int m=-1,bx,by;
  9886. int count=0;
  9887. struct block_list *bl;
  9888. str=script_getstr(st,2);
  9889. if(strcmp(str,"this")==0){
  9890. TBL_PC *sd=script_rid2sd(st);
  9891. if(sd)
  9892. m=sd->bl.m;
  9893. else{
  9894. script_pushint(st,-1);
  9895. return 0;
  9896. }
  9897. }else
  9898. m=map_mapname2mapid(str);
  9899. if(m < 0){
  9900. script_pushint(st,-1);
  9901. return 0;
  9902. }
  9903. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9904. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9905. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9906. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9907. count++;
  9908. script_pushint(st,count);
  9909. return 0;
  9910. }
  9911. /*==========================================
  9912. * movenpc [MouseJstr]
  9913. *------------------------------------------*/
  9914. BUILDIN_FUNC(movenpc)
  9915. {
  9916. TBL_NPC *nd = NULL;
  9917. const char *npc;
  9918. int x,y;
  9919. npc = script_getstr(st,2);
  9920. x = script_getnum(st,3);
  9921. y = script_getnum(st,4);
  9922. if ((nd = npc_name2id(npc)) == NULL)
  9923. return -1;
  9924. npc_movenpc(nd, x, y);
  9925. return 0;
  9926. }
  9927. /*==========================================
  9928. * message [MouseJstr]
  9929. *------------------------------------------*/
  9930. BUILDIN_FUNC(message)
  9931. {
  9932. TBL_PC *sd;
  9933. const char *msg,*player;
  9934. TBL_PC *pl_sd = NULL;
  9935. sd = script_rid2sd(st);
  9936. player = script_getstr(st,2);
  9937. msg = script_getstr(st,3);
  9938. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9939. return 0;
  9940. clif_displaymessage(pl_sd->fd, msg);
  9941. return 0;
  9942. }
  9943. /*==========================================
  9944. * npctalk (sends message to surrounding area)
  9945. *------------------------------------------*/
  9946. BUILDIN_FUNC(npctalk)
  9947. {
  9948. const char* str;
  9949. char message[255];
  9950. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9951. str = script_getstr(st,2);
  9952. if(nd) {
  9953. memcpy(message, nd->name, NAME_LENGTH);
  9954. strtok(message, "#"); // discard extra name identifier if present
  9955. strcat(message, " : ");
  9956. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9957. clif_message(&(nd->bl), message);
  9958. }
  9959. return 0;
  9960. }
  9961. // change npc walkspeed [Valaris]
  9962. BUILDIN_FUNC(npcspeed)
  9963. {
  9964. struct npc_data* nd;
  9965. int speed;
  9966. speed = script_getnum(st,2);
  9967. nd =(struct npc_data *)map_id2bl(st->oid);
  9968. if( nd )
  9969. {
  9970. nd->speed = speed;
  9971. nd->ud.state.speed_changed = 1;
  9972. }
  9973. return 0;
  9974. }
  9975. // make an npc walk to a position [Valaris]
  9976. BUILDIN_FUNC(npcwalkto)
  9977. {
  9978. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9979. int x=0,y=0;
  9980. x=script_getnum(st,2);
  9981. y=script_getnum(st,3);
  9982. if(nd) {
  9983. unit_walktoxy(&nd->bl,x,y,0);
  9984. }
  9985. return 0;
  9986. }
  9987. // stop an npc's movement [Valaris]
  9988. BUILDIN_FUNC(npcstop)
  9989. {
  9990. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9991. if(nd) {
  9992. unit_stop_walking(&nd->bl,1|4);
  9993. }
  9994. return 0;
  9995. }
  9996. /*==========================================
  9997. * getlook char info. getlook(arg)
  9998. *------------------------------------------*/
  9999. BUILDIN_FUNC(getlook)
  10000. {
  10001. int type,val;
  10002. TBL_PC *sd;
  10003. sd=script_rid2sd(st);
  10004. type=script_getnum(st,2);
  10005. val=-1;
  10006. switch(type) {
  10007. case LOOK_HAIR: val=sd->status.hair; break; //1
  10008. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10009. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10010. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10011. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10012. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10013. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10014. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10015. case LOOK_SHOES: break; //9
  10016. }
  10017. script_pushint(st,val);
  10018. return 0;
  10019. }
  10020. /*==========================================
  10021. * get char save point. argument: 0- map name, 1- x, 2- y
  10022. *------------------------------------------*/
  10023. BUILDIN_FUNC(getsavepoint)
  10024. {
  10025. TBL_PC* sd;
  10026. int type;
  10027. sd = script_rid2sd(st);
  10028. if (sd == NULL) {
  10029. script_pushint(st,0);
  10030. return 0;
  10031. }
  10032. type = script_getnum(st,2);
  10033. switch(type) {
  10034. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10035. case 1: script_pushint(st,sd->status.save_point.x); break;
  10036. case 2: script_pushint(st,sd->status.save_point.y); break;
  10037. default:
  10038. script_pushint(st,0);
  10039. break;
  10040. }
  10041. return 0;
  10042. }
  10043. /*==========================================
  10044. * Get position for char/npc/pet/mob objects. Added by Lorky
  10045. *
  10046. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10047. * where type:
  10048. * MapName$ - String variable for output map name
  10049. * MapX - Integer variable for output coord X
  10050. * MapY - Integer variable for output coord Y
  10051. * type - type of object
  10052. * 0 - Character coord
  10053. * 1 - NPC coord
  10054. * 2 - Pet coord
  10055. * 3 - Mob coord (not released)
  10056. * 4 - Homun coord
  10057. * CharName$ - Name object. If miss or "this" the current object
  10058. *
  10059. * Return:
  10060. * 0 - success
  10061. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10062. *------------------------------------------*/
  10063. BUILDIN_FUNC(getmapxy)
  10064. {
  10065. struct block_list *bl = NULL;
  10066. TBL_PC *sd=NULL;
  10067. int num;
  10068. const char *name;
  10069. char prefix;
  10070. int x,y,type;
  10071. char mapname[MAP_NAME_LENGTH];
  10072. if( !data_isreference(script_getdata(st,2)) ){
  10073. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10074. script_pushint(st,-1);
  10075. return 1;
  10076. }
  10077. if( !data_isreference(script_getdata(st,3)) ){
  10078. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10079. script_pushint(st,-1);
  10080. return 1;
  10081. }
  10082. if( !data_isreference(script_getdata(st,4)) ){
  10083. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10084. script_pushint(st,-1);
  10085. return 1;
  10086. }
  10087. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10088. type=script_getnum(st,5);
  10089. switch (type){
  10090. case 0: //Get Character Position
  10091. if( script_hasdata(st,6) )
  10092. sd=map_nick2sd(script_getstr(st,6));
  10093. else
  10094. sd=script_rid2sd(st);
  10095. if (sd)
  10096. bl = &sd->bl;
  10097. break;
  10098. case 1: //Get NPC Position
  10099. if( script_hasdata(st,6) )
  10100. {
  10101. struct npc_data *nd;
  10102. nd=npc_name2id(script_getstr(st,6));
  10103. if (nd)
  10104. bl = &nd->bl;
  10105. } else //In case the origin is not an npc?
  10106. bl=map_id2bl(st->oid);
  10107. break;
  10108. case 2: //Get Pet Position
  10109. if(script_hasdata(st,6))
  10110. sd=map_nick2sd(script_getstr(st,6));
  10111. else
  10112. sd=script_rid2sd(st);
  10113. if (sd && sd->pd)
  10114. bl = &sd->pd->bl;
  10115. break;
  10116. case 3: //Get Mob Position
  10117. break; //Not supported?
  10118. case 4: //Get Homun Position
  10119. if(script_hasdata(st,6))
  10120. sd=map_nick2sd(script_getstr(st,6));
  10121. else
  10122. sd=script_rid2sd(st);
  10123. if (sd && sd->hd)
  10124. bl = &sd->hd->bl;
  10125. break;
  10126. }
  10127. if (!bl) { //No object found.
  10128. script_pushint(st,-1);
  10129. return 0;
  10130. }
  10131. x= bl->x;
  10132. y= bl->y;
  10133. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10134. //Set MapName$
  10135. num=st->stack->stack_data[st->start+2].u.num;
  10136. name=get_str(num&0x00ffffff);
  10137. prefix=*name;
  10138. if(not_server_variable(prefix))
  10139. sd=script_rid2sd(st);
  10140. else
  10141. sd=NULL;
  10142. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10143. //Set MapX
  10144. num=st->stack->stack_data[st->start+3].u.num;
  10145. name=get_str(num&0x00ffffff);
  10146. prefix=*name;
  10147. if(not_server_variable(prefix))
  10148. sd=script_rid2sd(st);
  10149. else
  10150. sd=NULL;
  10151. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10152. //Set MapY
  10153. num=st->stack->stack_data[st->start+4].u.num;
  10154. name=get_str(num&0x00ffffff);
  10155. prefix=*name;
  10156. if(not_server_variable(prefix))
  10157. sd=script_rid2sd(st);
  10158. else
  10159. sd=NULL;
  10160. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10161. //Return Success value
  10162. script_pushint(st,0);
  10163. return 0;
  10164. }
  10165. /*==========================================
  10166. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10167. *------------------------------------------*/
  10168. BUILDIN_FUNC(logmes)
  10169. {
  10170. const char *str;
  10171. TBL_PC* sd;
  10172. if( log_config.npc <= 0 )
  10173. return 0;
  10174. sd = script_rid2sd(st);
  10175. if( sd == NULL )
  10176. return 1;
  10177. str = script_getstr(st,2);
  10178. log_npc(sd,str);
  10179. return 0;
  10180. }
  10181. BUILDIN_FUNC(summon)
  10182. {
  10183. int _class, timeout=0;
  10184. const char *str,*event="";
  10185. TBL_PC *sd;
  10186. struct mob_data *md;
  10187. int tick = gettick();
  10188. sd=script_rid2sd(st);
  10189. if (!sd) return 0;
  10190. str =script_getstr(st,2);
  10191. _class=script_getnum(st,3);
  10192. if( script_hasdata(st,4) )
  10193. timeout=script_getnum(st,4);
  10194. if( script_hasdata(st,5) ){
  10195. event=script_getstr(st,5);
  10196. check_event(st, event);
  10197. }
  10198. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10199. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10200. if (md) {
  10201. md->master_id=sd->bl.id;
  10202. md->special_state.ai=1;
  10203. if( md->deletetimer != INVALID_TIMER )
  10204. delete_timer(md->deletetimer, mob_timer_delete);
  10205. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10206. mob_spawn (md); //Now it is ready for spawning.
  10207. clif_misceffect2(&md->bl,344);
  10208. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10209. }
  10210. return 0;
  10211. }
  10212. /*==========================================
  10213. * Checks whether it is daytime/nighttime
  10214. *------------------------------------------*/
  10215. BUILDIN_FUNC(isnight)
  10216. {
  10217. script_pushint(st,(night_flag == 1));
  10218. return 0;
  10219. }
  10220. BUILDIN_FUNC(isday)
  10221. {
  10222. script_pushint(st,(night_flag == 0));
  10223. return 0;
  10224. }
  10225. /*================================================
  10226. * Check whether another item/card has been
  10227. * equipped - used for 2/15's cards patch [celest]
  10228. *------------------------------------------------*/
  10229. // leave this here, just in case
  10230. #if 0
  10231. BUILDIN_FUNC(isequipped)
  10232. {
  10233. TBL_PC *sd;
  10234. int i, j, k, id = 1;
  10235. int ret = -1;
  10236. sd = script_rid2sd(st);
  10237. for (i=0; id!=0; i++) {
  10238. int flag = 0;
  10239. FETCH (i+2, id) else id = 0;
  10240. if (id <= 0)
  10241. continue;
  10242. for (j=0; j<EQI_MAX; j++) {
  10243. int index;
  10244. index = sd->equip_index[j];
  10245. if(index < 0) continue;
  10246. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10247. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10248. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10249. if(!sd->inventory_data[index])
  10250. continue;
  10251. if(itemdb_type(id) != IT_CARD) { //Non card
  10252. if (sd->inventory_data[index]->nameid != id)
  10253. continue;
  10254. flag = 1;
  10255. break;
  10256. } else { //Card
  10257. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10258. continue;
  10259. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10260. if (sd->status.inventory[index].card[k] == id)
  10261. {
  10262. flag = 1;
  10263. break;
  10264. }
  10265. }
  10266. }
  10267. if (flag) break;
  10268. }
  10269. if (ret == -1)
  10270. ret = flag;
  10271. else
  10272. ret &= flag;
  10273. if (!ret) break;
  10274. }
  10275. script_pushint(st,ret);
  10276. return 0;
  10277. }
  10278. #endif
  10279. /*================================================
  10280. * Check how many items/cards in the list are
  10281. * equipped - used for 2/15's cards patch [celest]
  10282. *------------------------------------------------*/
  10283. BUILDIN_FUNC(isequippedcnt)
  10284. {
  10285. TBL_PC *sd;
  10286. int i, j, k, id = 1;
  10287. int ret = 0;
  10288. sd = script_rid2sd(st);
  10289. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10290. script_pushint(st,0);
  10291. return 0;
  10292. }
  10293. for (i=0; id!=0; i++) {
  10294. FETCH (i+2, id) else id = 0;
  10295. if (id <= 0)
  10296. continue;
  10297. for (j=0; j<EQI_MAX; j++) {
  10298. int index;
  10299. index = sd->equip_index[j];
  10300. if(index < 0) continue;
  10301. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10302. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10303. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10304. if(!sd->inventory_data[index])
  10305. continue;
  10306. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10307. if (sd->inventory_data[index]->nameid == id)
  10308. ret+= sd->status.inventory[index].amount;
  10309. } else { //Count cards.
  10310. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10311. continue; //No cards
  10312. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10313. if (sd->status.inventory[index].card[k] == id)
  10314. ret++; //[Lupus]
  10315. }
  10316. }
  10317. }
  10318. }
  10319. script_pushint(st,ret);
  10320. return 0;
  10321. }
  10322. /*================================================
  10323. * Check whether another card has been
  10324. * equipped - used for 2/15's cards patch [celest]
  10325. * -- Items checked cannot be reused in another
  10326. * card set to prevent exploits
  10327. *------------------------------------------------*/
  10328. BUILDIN_FUNC(isequipped)
  10329. {
  10330. TBL_PC *sd;
  10331. int i, j, k, id = 1;
  10332. int index, flag;
  10333. int ret = -1;
  10334. //Original hash to reverse it when full check fails.
  10335. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10336. sd = script_rid2sd(st);
  10337. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10338. script_pushint(st,0);
  10339. return 0;
  10340. }
  10341. setitem_hash = sd->setitem_hash;
  10342. setitem_hash2 = sd->setitem_hash2;
  10343. for (i=0; id!=0; i++)
  10344. {
  10345. FETCH (i+2, id) else id = 0;
  10346. if (id <= 0)
  10347. continue;
  10348. flag = 0;
  10349. for (j=0; j<EQI_MAX; j++)
  10350. {
  10351. index = sd->equip_index[j];
  10352. if(index < 0) continue;
  10353. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10354. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10355. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10356. if(!sd->inventory_data[index])
  10357. continue;
  10358. if (itemdb_type(id) != IT_CARD) {
  10359. if (sd->inventory_data[index]->nameid != id)
  10360. continue;
  10361. flag = 1;
  10362. break;
  10363. } else { //Cards
  10364. if (sd->inventory_data[index]->slot == 0 ||
  10365. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10366. continue;
  10367. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10368. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10369. unsigned int hash = 0;
  10370. if (sd->status.inventory[index].card[k] != id)
  10371. continue;
  10372. hash = 1<<((j<5?j:j-5)*4 + k);
  10373. // check if card is already used by another set
  10374. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10375. continue;
  10376. // We have found a match
  10377. flag = 1;
  10378. // Set hash so this card cannot be used by another
  10379. if (j<5)
  10380. sd->setitem_hash |= hash;
  10381. else
  10382. sd->setitem_hash2 |= hash;
  10383. break;
  10384. }
  10385. }
  10386. if (flag) break; //Card found
  10387. }
  10388. if (ret == -1)
  10389. ret = flag;
  10390. else
  10391. ret &= flag;
  10392. if (!ret) break;
  10393. }
  10394. if (!ret)
  10395. { //When check fails, restore original hash values. [Skotlex]
  10396. sd->setitem_hash = setitem_hash;
  10397. sd->setitem_hash2 = setitem_hash2;
  10398. }
  10399. script_pushint(st,ret);
  10400. return 0;
  10401. }
  10402. /*================================================
  10403. * Check how many given inserted cards in the CURRENT
  10404. * weapon - used for 2/15's cards patch [Lupus]
  10405. *------------------------------------------------*/
  10406. BUILDIN_FUNC(cardscnt)
  10407. {
  10408. TBL_PC *sd;
  10409. int i, k, id = 1;
  10410. int ret = 0;
  10411. int index;
  10412. sd = script_rid2sd(st);
  10413. for (i=0; id!=0; i++) {
  10414. FETCH (i+2, id) else id = 0;
  10415. if (id <= 0)
  10416. continue;
  10417. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10418. if(index < 0) continue;
  10419. if(!sd->inventory_data[index])
  10420. continue;
  10421. if(itemdb_type(id) != IT_CARD) {
  10422. if (sd->inventory_data[index]->nameid == id)
  10423. ret+= sd->status.inventory[index].amount;
  10424. } else {
  10425. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10426. continue;
  10427. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10428. if (sd->status.inventory[index].card[k] == id)
  10429. ret++;
  10430. }
  10431. }
  10432. }
  10433. script_pushint(st,ret);
  10434. // script_pushint(st,current_equip_item_index);
  10435. return 0;
  10436. }
  10437. /*=======================================================
  10438. * Returns the refined number of the current item, or an
  10439. * item with inventory index specified
  10440. *-------------------------------------------------------*/
  10441. BUILDIN_FUNC(getrefine)
  10442. {
  10443. TBL_PC *sd;
  10444. if ((sd = script_rid2sd(st))!= NULL)
  10445. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10446. else
  10447. script_pushint(st,0);
  10448. return 0;
  10449. }
  10450. /*=======================================================
  10451. * Day/Night controls
  10452. *-------------------------------------------------------*/
  10453. BUILDIN_FUNC(night)
  10454. {
  10455. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10456. return 0;
  10457. }
  10458. BUILDIN_FUNC(day)
  10459. {
  10460. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10461. return 0;
  10462. }
  10463. //=======================================================
  10464. // Unequip [Spectre]
  10465. //-------------------------------------------------------
  10466. BUILDIN_FUNC(unequip)
  10467. {
  10468. int i;
  10469. size_t num;
  10470. TBL_PC *sd;
  10471. num = script_getnum(st,2);
  10472. sd = script_rid2sd(st);
  10473. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10474. {
  10475. i = pc_checkequip(sd,equip[num-1]);
  10476. if (i >= 0)
  10477. pc_unequipitem(sd,i,1|2);
  10478. }
  10479. return 0;
  10480. }
  10481. BUILDIN_FUNC(equip)
  10482. {
  10483. int nameid=0,i;
  10484. TBL_PC *sd;
  10485. struct item_data *item_data;
  10486. sd = script_rid2sd(st);
  10487. nameid=script_getnum(st,2);
  10488. if((item_data = itemdb_exists(nameid)) == NULL)
  10489. {
  10490. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10491. return 1;
  10492. }
  10493. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10494. if( i < MAX_INVENTORY )
  10495. pc_equipitem(sd,i,item_data->equip);
  10496. return 0;
  10497. }
  10498. BUILDIN_FUNC(autoequip)
  10499. {
  10500. int nameid, flag;
  10501. struct item_data *item_data;
  10502. nameid=script_getnum(st,2);
  10503. flag=script_getnum(st,3);
  10504. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10505. item_data->flag.autoequip = flag>0?1:0;
  10506. }
  10507. return 0;
  10508. }
  10509. BUILDIN_FUNC(setbattleflag)
  10510. {
  10511. const char *flag, *value;
  10512. flag = script_getstr(st,2);
  10513. value = script_getstr(st,3);
  10514. if (battle_set_value(flag, value) == 0)
  10515. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10516. else
  10517. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10518. return 0;
  10519. }
  10520. BUILDIN_FUNC(getbattleflag)
  10521. {
  10522. const char *flag;
  10523. flag = script_getstr(st,2);
  10524. script_pushint(st,battle_get_value(flag));
  10525. return 0;
  10526. }
  10527. //=======================================================
  10528. // strlen [Valaris]
  10529. //-------------------------------------------------------
  10530. BUILDIN_FUNC(getstrlen)
  10531. {
  10532. const char *str = script_getstr(st,2);
  10533. int len = (str) ? (int)strlen(str) : 0;
  10534. script_pushint(st,len);
  10535. return 0;
  10536. }
  10537. //=======================================================
  10538. // isalpha [Valaris]
  10539. //-------------------------------------------------------
  10540. BUILDIN_FUNC(charisalpha)
  10541. {
  10542. const char *str=script_getstr(st,2);
  10543. int pos=script_getnum(st,3);
  10544. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10545. script_pushint(st,val);
  10546. return 0;
  10547. }
  10548. /// Changes the display name and/or display class of the npc.
  10549. /// Returns 0 is successful, 1 if the npc does not exist.
  10550. ///
  10551. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10552. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10553. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10554. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10555. BUILDIN_FUNC(setnpcdisplay)
  10556. {
  10557. const char* name;
  10558. const char* newname = NULL;
  10559. int class_ = -1, size = -1;
  10560. struct script_data* data;
  10561. struct npc_data* nd;
  10562. name = script_getstr(st,2);
  10563. data = script_getdata(st,3);
  10564. if( script_hasdata(st,4) )
  10565. class_ = script_getnum(st,4);
  10566. if( script_hasdata(st,5) )
  10567. size = script_getnum(st,5);
  10568. get_val(st, data);
  10569. if( data_isstring(data) )
  10570. newname = conv_str(st,data);
  10571. else if( data_isint(data) )
  10572. class_ = conv_num(st,data);
  10573. else
  10574. {
  10575. ShowError("script:setnpcdisplay: expected a string or number\n");
  10576. script_reportdata(data);
  10577. return 1;
  10578. }
  10579. nd = npc_name2id(name);
  10580. if( nd == NULL )
  10581. {// not found
  10582. script_pushint(st,1);
  10583. return 0;
  10584. }
  10585. // update npc
  10586. if( newname )
  10587. npc_setdisplayname(nd, newname);
  10588. if( size != -1 && size != nd->size )
  10589. nd->size = size;
  10590. else
  10591. size = -1;
  10592. if( class_ != -1 && nd->class_ != class_ )
  10593. npc_setclass(nd, class_);
  10594. else if( size != -1 )
  10595. { // Required to update the visual size
  10596. clif_clearunit_area(&nd->bl, 0);
  10597. clif_spawn(&nd->bl);
  10598. }
  10599. script_pushint(st,0);
  10600. return 0;
  10601. }
  10602. BUILDIN_FUNC(atoi)
  10603. {
  10604. const char *value;
  10605. value = script_getstr(st,2);
  10606. script_pushint(st,atoi(value));
  10607. return 0;
  10608. }
  10609. // case-insensitive substring search [lordalfa]
  10610. BUILDIN_FUNC(compare)
  10611. {
  10612. const char *message;
  10613. const char *cmpstring;
  10614. message = script_getstr(st,2);
  10615. cmpstring = script_getstr(st,3);
  10616. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10617. return 0;
  10618. }
  10619. // [zBuffer] List of mathematics commands --->
  10620. BUILDIN_FUNC(sqrt)
  10621. {
  10622. double i, a;
  10623. i = script_getnum(st,2);
  10624. a = sqrt(i);
  10625. script_pushint(st,(int)a);
  10626. return 0;
  10627. }
  10628. BUILDIN_FUNC(pow)
  10629. {
  10630. double i, a, b;
  10631. a = script_getnum(st,2);
  10632. b = script_getnum(st,3);
  10633. i = pow(a,b);
  10634. script_pushint(st,(int)i);
  10635. return 0;
  10636. }
  10637. BUILDIN_FUNC(distance)
  10638. {
  10639. int x0, y0, x1, y1;
  10640. x0 = script_getnum(st,2);
  10641. y0 = script_getnum(st,3);
  10642. x1 = script_getnum(st,4);
  10643. y1 = script_getnum(st,5);
  10644. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10645. return 0;
  10646. }
  10647. // <--- [zBuffer] List of mathematics commands
  10648. BUILDIN_FUNC(md5)
  10649. {
  10650. const char *tmpstr;
  10651. char *md5str;
  10652. tmpstr = script_getstr(st,2);
  10653. md5str = (char *)aMallocA((32+1)*sizeof(char));
  10654. MD5_String(tmpstr, md5str);
  10655. script_pushstr(st, md5str);
  10656. return 0;
  10657. }
  10658. // [zBuffer] List of dynamic var commands --->
  10659. BUILDIN_FUNC(setd)
  10660. {
  10661. TBL_PC *sd=NULL;
  10662. char varname[100];
  10663. const char *value, *buffer;
  10664. int elem;
  10665. buffer = script_getstr(st, 2);
  10666. value = script_getstr(st, 3);
  10667. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10668. elem = 0;
  10669. if( not_server_variable(*varname) )
  10670. {
  10671. sd = script_rid2sd(st);
  10672. if( sd == NULL )
  10673. {
  10674. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10675. return 0;
  10676. }
  10677. }
  10678. if(varname[strlen(varname)-1] != '$') {
  10679. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10680. } else {
  10681. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10682. }
  10683. return 0;
  10684. }
  10685. #ifndef TXT_ONLY
  10686. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10687. {
  10688. int i, j;
  10689. TBL_PC* sd = NULL;
  10690. const char* query;
  10691. struct script_data* data;
  10692. const char* name;
  10693. int max_rows = 128;// maximum number of rows
  10694. int num_vars;
  10695. int num_cols;
  10696. // check target variables
  10697. for( i = 3; script_hasdata(st,i); ++i )
  10698. {
  10699. data = script_getdata(st, i);
  10700. if( data_isreference(data) && reference_tovariable(data) )
  10701. {// it's a variable
  10702. name = reference_getname(data);
  10703. if( not_server_variable(*name) && sd == NULL )
  10704. {// requires a player
  10705. sd = script_rid2sd(st);
  10706. if( sd == NULL )
  10707. {// no player attached
  10708. script_reportdata(data);
  10709. st->state = END;
  10710. return 1;
  10711. }
  10712. }
  10713. if( not_array_variable(*name) )
  10714. max_rows = 1;// not an array, limit to one row
  10715. }
  10716. else
  10717. {
  10718. ShowError("script:query_sql: not a variable\n");
  10719. script_reportdata(data);
  10720. st->state = END;
  10721. return 1;
  10722. }
  10723. }
  10724. num_vars = i - 3;
  10725. // Execute the query
  10726. query = script_getstr(st,2);
  10727. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10728. {
  10729. Sql_ShowDebug(handle);
  10730. script_pushint(st, 0);
  10731. return 1;
  10732. }
  10733. if( Sql_NumRows(handle) == 0 )
  10734. {// No data received
  10735. Sql_FreeResult(handle);
  10736. script_pushint(st, 0);
  10737. return 0;
  10738. }
  10739. // Count the number of columns to store
  10740. num_cols = Sql_NumColumns(handle);
  10741. if( num_vars < num_cols )
  10742. {
  10743. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10744. script_reportsrc(st);
  10745. }
  10746. else if( num_vars > num_cols )
  10747. {
  10748. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10749. script_reportsrc(st);
  10750. }
  10751. // Store data
  10752. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10753. {
  10754. for( j = 0; j < num_vars; ++j )
  10755. {
  10756. char* str = NULL;
  10757. if( j < num_cols )
  10758. Sql_GetData(handle, j, &str, NULL);
  10759. data = script_getdata(st, j+3);
  10760. name = reference_getname(data);
  10761. if( is_string_variable(name) )
  10762. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10763. else
  10764. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10765. }
  10766. }
  10767. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10768. {
  10769. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10770. script_reportsrc(st);
  10771. }
  10772. // Free data
  10773. Sql_FreeResult(handle);
  10774. script_pushint(st, i);
  10775. return 0;
  10776. }
  10777. #endif
  10778. BUILDIN_FUNC(query_sql)
  10779. {
  10780. #ifndef TXT_ONLY
  10781. return buildin_query_sql_sub(st, mmysql_handle);
  10782. #else
  10783. //for TXT version, we always return -1
  10784. script_pushint(st,-1);
  10785. return 0;
  10786. #endif
  10787. }
  10788. BUILDIN_FUNC(query_logsql)
  10789. {
  10790. #ifndef TXT_ONLY
  10791. return buildin_query_sql_sub(st, logmysql_handle);
  10792. #else
  10793. //for TXT version, we always return -1
  10794. script_pushint(st,-1);
  10795. return 0;
  10796. #endif
  10797. }
  10798. //Allows escaping of a given string.
  10799. BUILDIN_FUNC(escape_sql)
  10800. {
  10801. const char *str;
  10802. char *esc_str;
  10803. size_t len;
  10804. str = script_getstr(st,2);
  10805. len = strlen(str);
  10806. esc_str = (char*)aMallocA(len*2+1);
  10807. #if defined(TXT_ONLY)
  10808. jstrescapecpy(esc_str, str);
  10809. #else
  10810. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10811. #endif
  10812. script_pushstr(st, esc_str);
  10813. return 0;
  10814. }
  10815. BUILDIN_FUNC(getd)
  10816. {
  10817. char varname[100];
  10818. const char *buffer;
  10819. int elem;
  10820. buffer = script_getstr(st, 2);
  10821. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10822. elem = 0;
  10823. // Push the 'pointer' so it's more flexible [Lance]
  10824. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10825. return 0;
  10826. }
  10827. // <--- [zBuffer] List of dynamic var commands
  10828. // Pet stat [Lance]
  10829. BUILDIN_FUNC(petstat)
  10830. {
  10831. TBL_PC *sd = NULL;
  10832. struct pet_data *pd;
  10833. int flag = script_getnum(st,2);
  10834. sd = script_rid2sd(st);
  10835. if(!sd || !sd->status.pet_id || !sd->pd){
  10836. if(flag == 2)
  10837. script_pushconststr(st, "");
  10838. else
  10839. script_pushint(st,0);
  10840. return 0;
  10841. }
  10842. pd = sd->pd;
  10843. switch(flag){
  10844. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10845. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10846. case 3: script_pushint(st,(int)pd->pet.level); break;
  10847. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10848. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10849. default:
  10850. script_pushint(st,0);
  10851. break;
  10852. }
  10853. return 0;
  10854. }
  10855. BUILDIN_FUNC(callshop)
  10856. {
  10857. TBL_PC *sd = NULL;
  10858. struct npc_data *nd;
  10859. const char *shopname;
  10860. int flag = 0;
  10861. sd = script_rid2sd(st);
  10862. if (!sd) {
  10863. script_pushint(st,0);
  10864. return 0;
  10865. }
  10866. shopname = script_getstr(st, 2);
  10867. if( script_hasdata(st,3) )
  10868. flag = script_getnum(st,3);
  10869. nd = npc_name2id(shopname);
  10870. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  10871. {
  10872. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10873. script_pushint(st,0);
  10874. return 1;
  10875. }
  10876. if( nd->subtype == SHOP )
  10877. {
  10878. switch( flag )
  10879. {
  10880. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  10881. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  10882. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  10883. }
  10884. }
  10885. else
  10886. clif_cashshop_show(sd, nd);
  10887. sd->npc_shopid = nd->bl.id;
  10888. script_pushint(st,1);
  10889. return 0;
  10890. }
  10891. BUILDIN_FUNC(npcshopitem)
  10892. {
  10893. const char* npcname = script_getstr(st, 2);
  10894. struct npc_data* nd = npc_name2id(npcname);
  10895. int n, i;
  10896. int amount;
  10897. if( !nd || nd->subtype != SHOP )
  10898. { //Not found.
  10899. script_pushint(st,0);
  10900. return 0;
  10901. }
  10902. // get the count of new entries
  10903. amount = (script_lastdata(st)-2)/2;
  10904. // generate new shop item list
  10905. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10906. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10907. {
  10908. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10909. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10910. }
  10911. nd->u.shop.count = n;
  10912. script_pushint(st,1);
  10913. return 0;
  10914. }
  10915. BUILDIN_FUNC(npcshopadditem)
  10916. {
  10917. const char* npcname = script_getstr(st,2);
  10918. struct npc_data* nd = npc_name2id(npcname);
  10919. int n, i;
  10920. int amount;
  10921. if( !nd || nd->subtype != SHOP )
  10922. { //Not found.
  10923. script_pushint(st,0);
  10924. return 0;
  10925. }
  10926. // get the count of new entries
  10927. amount = (script_lastdata(st)-2)/2;
  10928. // append new items to existing shop item list
  10929. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10930. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10931. {
  10932. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10933. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10934. }
  10935. nd->u.shop.count = n;
  10936. script_pushint(st,1);
  10937. return 0;
  10938. }
  10939. BUILDIN_FUNC(npcshopdelitem)
  10940. {
  10941. const char* npcname = script_getstr(st,2);
  10942. struct npc_data* nd = npc_name2id(npcname);
  10943. int n, i;
  10944. int amount;
  10945. int size;
  10946. if( !nd || nd->subtype != SHOP )
  10947. { //Not found.
  10948. script_pushint(st,0);
  10949. return 0;
  10950. }
  10951. amount = script_lastdata(st)-2;
  10952. size = nd->u.shop.count;
  10953. // remove specified items from the shop item list
  10954. for( i = 3; i < 3 + amount; i++ )
  10955. {
  10956. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10957. if( n < size )
  10958. {
  10959. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10960. size--;
  10961. }
  10962. }
  10963. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10964. nd->u.shop.count = size;
  10965. script_pushint(st,1);
  10966. return 0;
  10967. }
  10968. //Sets a script to attach to a shop npc.
  10969. BUILDIN_FUNC(npcshopattach)
  10970. {
  10971. const char* npcname = script_getstr(st,2);
  10972. struct npc_data* nd = npc_name2id(npcname);
  10973. int flag = 1;
  10974. if( script_hasdata(st,3) )
  10975. flag = script_getnum(st,3);
  10976. if( !nd || nd->subtype != SHOP )
  10977. { //Not found.
  10978. script_pushint(st,0);
  10979. return 0;
  10980. }
  10981. if (flag)
  10982. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10983. else
  10984. nd->master_nd = NULL;
  10985. script_pushint(st,1);
  10986. return 0;
  10987. }
  10988. /*==========================================
  10989. * Returns some values of an item [Lupus]
  10990. * Price, Weight, etc...
  10991. setitemscript(itemID,"{new item bonus script}",[n]);
  10992. Where n:
  10993. 0 - script
  10994. 1 - Equip script
  10995. 2 - Unequip script
  10996. *------------------------------------------*/
  10997. BUILDIN_FUNC(setitemscript)
  10998. {
  10999. int item_id,n=0;
  11000. const char *script;
  11001. struct item_data *i_data;
  11002. struct script_code **dstscript;
  11003. item_id = script_getnum(st,2);
  11004. script = script_getstr(st,3);
  11005. if( script_hasdata(st,4) )
  11006. n=script_getnum(st,4);
  11007. i_data = itemdb_exists(item_id);
  11008. if (!i_data || script==NULL || script[0]!='{') {
  11009. script_pushint(st,0);
  11010. return 0;
  11011. }
  11012. switch (n) {
  11013. case 2:
  11014. dstscript = &i_data->unequip_script;
  11015. break;
  11016. case 1:
  11017. dstscript = &i_data->equip_script;
  11018. break;
  11019. default:
  11020. dstscript = &i_data->script;
  11021. break;
  11022. }
  11023. if(*dstscript)
  11024. script_free_code(*dstscript);
  11025. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11026. script_pushint(st,1);
  11027. return 0;
  11028. }
  11029. /* Work In Progress [Lupus]
  11030. BUILDIN_FUNC(addmonsterdrop)
  11031. {
  11032. int class_,item_id,chance;
  11033. class_=script_getnum(st,2);
  11034. item_id=script_getnum(st,3);
  11035. chance=script_getnum(st,4);
  11036. if(class_>1000 && item_id>500 && chance>0) {
  11037. script_pushint(st,1);
  11038. } else {
  11039. script_pushint(st,0);
  11040. }
  11041. }
  11042. BUILDIN_FUNC(delmonsterdrop)
  11043. {
  11044. int class_,item_id;
  11045. class_=script_getnum(st,2);
  11046. item_id=script_getnum(st,3);
  11047. if(class_>1000 && item_id>500) {
  11048. script_pushint(st,1);
  11049. } else {
  11050. script_pushint(st,0);
  11051. }
  11052. }
  11053. */
  11054. /*==========================================
  11055. * Returns some values of a monster [Lupus]
  11056. * Name, Level, race, size, etc...
  11057. getmonsterinfo(monsterID,queryIndex);
  11058. *------------------------------------------*/
  11059. BUILDIN_FUNC(getmonsterinfo)
  11060. {
  11061. struct mob_db *mob;
  11062. int mob_id;
  11063. mob_id = script_getnum(st,2);
  11064. if (!mobdb_checkid(mob_id)) {
  11065. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  11066. if ( !script_getnum(st,3) ) //requested a string
  11067. script_pushconststr(st,"null");
  11068. else
  11069. script_pushint(st,-1);
  11070. return -1;
  11071. }
  11072. mob = mob_db(mob_id);
  11073. switch ( script_getnum(st,3) ) {
  11074. case 0: script_pushstrcopy(st,mob->jname); break;
  11075. case 1: script_pushint(st,mob->lv); break;
  11076. case 2: script_pushint(st,mob->status.max_hp); break;
  11077. case 3: script_pushint(st,mob->base_exp); break;
  11078. case 4: script_pushint(st,mob->job_exp); break;
  11079. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11080. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11081. case 7: script_pushint(st,mob->status.def); break;
  11082. case 8: script_pushint(st,mob->status.mdef); break;
  11083. case 9: script_pushint(st,mob->status.str); break;
  11084. case 10: script_pushint(st,mob->status.agi); break;
  11085. case 11: script_pushint(st,mob->status.vit); break;
  11086. case 12: script_pushint(st,mob->status.int_); break;
  11087. case 13: script_pushint(st,mob->status.dex); break;
  11088. case 14: script_pushint(st,mob->status.luk); break;
  11089. case 15: script_pushint(st,mob->status.rhw.range); break;
  11090. case 16: script_pushint(st,mob->range2); break;
  11091. case 17: script_pushint(st,mob->range3); break;
  11092. case 18: script_pushint(st,mob->status.size); break;
  11093. case 19: script_pushint(st,mob->status.race); break;
  11094. case 20: script_pushint(st,mob->status.def_ele); break;
  11095. case 21: script_pushint(st,mob->status.mode); break;
  11096. default: script_pushint(st,-1); //wrong Index
  11097. }
  11098. return 0;
  11099. }
  11100. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11101. {
  11102. TBL_PC *sd = NULL;
  11103. if(script_hasdata(st,2))
  11104. sd = map_nick2sd(script_getstr(st,2));
  11105. else
  11106. sd = script_rid2sd(st);
  11107. if(sd)
  11108. script_pushint(st,(sd->vender_id != 0));
  11109. else
  11110. script_pushint(st,0);
  11111. return 0;
  11112. }
  11113. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11114. {
  11115. TBL_PC *sd = NULL;
  11116. if(script_hasdata(st,2))
  11117. sd = map_nick2sd(script_getstr(st,2));
  11118. else
  11119. sd = script_rid2sd(st);
  11120. if(sd)
  11121. script_pushint(st,(sd->chatID != 0));
  11122. else
  11123. script_pushint(st,0);
  11124. return 0;
  11125. }
  11126. BUILDIN_FUNC(searchitem)
  11127. {
  11128. struct script_data* data = script_getdata(st, 2);
  11129. const char *itemname = script_getstr(st,3);
  11130. struct item_data *items[MAX_SEARCH];
  11131. int count;
  11132. char* name;
  11133. int32 start;
  11134. int32 id;
  11135. int32 i;
  11136. TBL_PC* sd = NULL;
  11137. if ((items[0] = itemdb_exists(atoi(itemname))))
  11138. count = 1;
  11139. else {
  11140. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  11141. if (count > MAX_SEARCH) count = MAX_SEARCH;
  11142. }
  11143. if (!count) {
  11144. script_pushint(st, 0);
  11145. return 0;
  11146. }
  11147. if( !data_isreference(data) )
  11148. {
  11149. ShowError("script:searchitem: not a variable\n");
  11150. script_reportdata(data);
  11151. st->state = END;
  11152. return 1;// not a variable
  11153. }
  11154. id = reference_getid(data);
  11155. start = reference_getindex(data);
  11156. name = reference_getname(data);
  11157. if( not_array_variable(*name) )
  11158. {
  11159. ShowError("script:searchitem: illegal scope\n");
  11160. script_reportdata(data);
  11161. st->state = END;
  11162. return 1;// not supported
  11163. }
  11164. if( not_server_variable(*name) )
  11165. {
  11166. sd = script_rid2sd(st);
  11167. if( sd == NULL )
  11168. return 0;// no player attached
  11169. }
  11170. if( is_string_variable(name) )
  11171. {// string array
  11172. ShowError("script:searchitem: not an integer array reference\n");
  11173. script_reportdata(data);
  11174. st->state = END;
  11175. return 1;// not supported
  11176. }
  11177. for( i = 0; i < count; ++start, ++i )
  11178. {// Set array
  11179. void* v = (void*)items[i]->nameid;
  11180. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  11181. }
  11182. script_pushint(st, count);
  11183. return 0;
  11184. }
  11185. int axtoi(const char *hexStg)
  11186. {
  11187. int n = 0; // position in string
  11188. int m = 0; // position in digit[] to shift
  11189. int count; // loop index
  11190. int intValue = 0; // integer value of hex string
  11191. int digit[11]; // hold values to convert
  11192. while (n < 10) {
  11193. if (hexStg[n]=='\0')
  11194. break;
  11195. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11196. digit[n] = hexStg[n] & 0x0f; //convert to int
  11197. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11198. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11199. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11200. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11201. else break;
  11202. n++;
  11203. }
  11204. count = n;
  11205. m = n - 1;
  11206. n = 0;
  11207. while(n < count) {
  11208. // digit[n] is value of hex digit at position n
  11209. // (m << 2) is the number of positions to shift
  11210. // OR the bits into return value
  11211. intValue = intValue | (digit[n] << (m << 2));
  11212. m--; // adjust the position to set
  11213. n++; // next digit to process
  11214. }
  11215. return (intValue);
  11216. }
  11217. // [Lance] Hex string to integer converter
  11218. BUILDIN_FUNC(axtoi)
  11219. {
  11220. const char *hex = script_getstr(st,2);
  11221. script_pushint(st,axtoi(hex));
  11222. return 0;
  11223. }
  11224. // [zBuffer] List of player cont commands --->
  11225. BUILDIN_FUNC(rid2name)
  11226. {
  11227. struct block_list *bl = NULL;
  11228. int rid = script_getnum(st,2);
  11229. if((bl = map_id2bl(rid)))
  11230. {
  11231. switch(bl->type) {
  11232. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  11233. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  11234. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  11235. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  11236. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  11237. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  11238. default:
  11239. ShowError("buildin_rid2name: BL type unknown.\n");
  11240. script_pushconststr(st,"");
  11241. break;
  11242. }
  11243. } else {
  11244. ShowError("buildin_rid2name: invalid RID\n");
  11245. script_pushconststr(st,"(null)");
  11246. }
  11247. return 0;
  11248. }
  11249. BUILDIN_FUNC(pcblockmove)
  11250. {
  11251. int id, flag;
  11252. TBL_PC *sd = NULL;
  11253. id = script_getnum(st,2);
  11254. flag = script_getnum(st,3);
  11255. if(id)
  11256. sd = map_id2sd(id);
  11257. else
  11258. sd = script_rid2sd(st);
  11259. if(sd)
  11260. sd->state.blockedmove = flag > 0;
  11261. return 0;
  11262. }
  11263. BUILDIN_FUNC(pcfollow)
  11264. {
  11265. int id, targetid;
  11266. TBL_PC *sd = NULL;
  11267. id = script_getnum(st,2);
  11268. targetid = script_getnum(st,3);
  11269. if(id)
  11270. sd = map_id2sd(id);
  11271. else
  11272. sd = script_rid2sd(st);
  11273. if(sd)
  11274. pc_follow(sd, targetid);
  11275. return 0;
  11276. }
  11277. BUILDIN_FUNC(pcstopfollow)
  11278. {
  11279. int id;
  11280. TBL_PC *sd = NULL;
  11281. id = script_getnum(st,2);
  11282. if(id)
  11283. sd = map_id2sd(id);
  11284. else
  11285. sd = script_rid2sd(st);
  11286. if(sd)
  11287. pc_stop_following(sd);
  11288. return 0;
  11289. }
  11290. // <--- [zBuffer] List of player cont commands
  11291. // [zBuffer] List of mob control commands --->
  11292. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11293. /// Makes the unit walk to target position or map
  11294. /// Returns if it was successfull
  11295. ///
  11296. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11297. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11298. BUILDIN_FUNC(unitwalk)
  11299. {
  11300. struct block_list* bl;
  11301. bl = map_id2bl(script_getnum(st,2));
  11302. if( bl == NULL )
  11303. {
  11304. script_pushint(st, 0);
  11305. }
  11306. else if( script_hasdata(st,4) )
  11307. {
  11308. int x = script_getnum(st,3);
  11309. int y = script_getnum(st,4);
  11310. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11311. }
  11312. else
  11313. {
  11314. int map_id = script_getnum(st,3);
  11315. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11316. }
  11317. return 0;
  11318. }
  11319. /// Kills the unit
  11320. ///
  11321. /// unitkill <unit_id>;
  11322. BUILDIN_FUNC(unitkill)
  11323. {
  11324. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11325. if( bl != NULL )
  11326. status_kill(bl);
  11327. return 0;
  11328. }
  11329. /// Warps the unit to the target position in the target map
  11330. /// Returns if it was successfull
  11331. ///
  11332. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11333. BUILDIN_FUNC(unitwarp)
  11334. {
  11335. int unit_id;
  11336. int map;
  11337. short x;
  11338. short y;
  11339. struct block_list* bl;
  11340. unit_id = script_getnum(st,2);
  11341. map = map_mapname2mapid(script_getstr(st, 3));
  11342. x = (short)script_getnum(st,4);
  11343. y = (short)script_getnum(st,5);
  11344. bl = map_id2bl(unit_id);
  11345. if( map >= 0 && bl != NULL )
  11346. script_pushint(st, unit_warp(bl,map,x,y,0));
  11347. else
  11348. script_pushint(st, 0);
  11349. return 0;
  11350. }
  11351. /// Makes the unit attack the target.
  11352. /// If the unit is a player and <action type> is not 0, it does a continuous
  11353. /// attack instead of a single attack.
  11354. /// Returns if the request was successfull.
  11355. ///
  11356. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11357. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11358. BUILDIN_FUNC(unitattack)
  11359. {
  11360. struct block_list* unit_bl;
  11361. struct block_list* target_bl = NULL;
  11362. struct script_data* data;
  11363. int actiontype = 0;
  11364. // get unit
  11365. unit_bl = map_id2bl(script_getnum(st,2));
  11366. if( unit_bl == NULL ) {
  11367. script_pushint(st, 0);
  11368. return 0;
  11369. }
  11370. data = script_getdata(st, 3);
  11371. get_val(st, data);
  11372. if( data_isstring(data) )
  11373. {
  11374. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11375. if( sd != NULL )
  11376. target_bl = &sd->bl;
  11377. } else
  11378. target_bl = map_id2bl(conv_num(st, data));
  11379. // request the attack
  11380. if( target_bl == NULL )
  11381. {
  11382. script_pushint(st, 0);
  11383. return 0;
  11384. }
  11385. // get actiontype
  11386. if( script_hasdata(st,4) )
  11387. actiontype = script_getnum(st,4);
  11388. switch( unit_bl->type )
  11389. {
  11390. case BL_PC:
  11391. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11392. script_pushint(st, 1);
  11393. return 0;
  11394. case BL_MOB:
  11395. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11396. break;
  11397. case BL_PET:
  11398. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11399. break;
  11400. default:
  11401. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11402. script_pushint(st, 0);
  11403. return 1;
  11404. }
  11405. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11406. return 0;
  11407. }
  11408. /// Makes the unit stop attacking and moving
  11409. ///
  11410. /// unitstop <unit_id>;
  11411. BUILDIN_FUNC(unitstop)
  11412. {
  11413. int unit_id;
  11414. struct block_list* bl;
  11415. unit_id = script_getnum(st,2);
  11416. bl = map_id2bl(unit_id);
  11417. if( bl != NULL )
  11418. {
  11419. unit_stop_attack(bl);
  11420. unit_stop_walking(bl,4);
  11421. if( bl->type == BL_MOB )
  11422. ((TBL_MOB*)bl)->target_id = 0;
  11423. }
  11424. return 0;
  11425. }
  11426. /// Makes the unit say the message
  11427. ///
  11428. /// unittalk <unit_id>,"<message>";
  11429. BUILDIN_FUNC(unittalk)
  11430. {
  11431. int unit_id;
  11432. const char* message;
  11433. struct block_list* bl;
  11434. unit_id = script_getnum(st,2);
  11435. message = script_getstr(st, 3);
  11436. bl = map_id2bl(unit_id);
  11437. if( bl != NULL )
  11438. {
  11439. struct StringBuf sbuf;
  11440. StringBuf_Init(&sbuf);
  11441. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11442. clif_message(bl, StringBuf_Value(&sbuf));
  11443. if( bl->type == BL_PC )
  11444. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11445. StringBuf_Destroy(&sbuf);
  11446. }
  11447. return 0;
  11448. }
  11449. /// Makes the unit do an emotion
  11450. ///
  11451. /// unitemote <unit_id>,<emotion>;
  11452. ///
  11453. /// @see e_* in const.txt
  11454. BUILDIN_FUNC(unitemote)
  11455. {
  11456. int unit_id;
  11457. int emotion;
  11458. struct block_list* bl;
  11459. unit_id = script_getnum(st,2);
  11460. emotion = script_getnum(st,3);
  11461. bl = map_id2bl(unit_id);
  11462. if( bl != NULL )
  11463. clif_emotion(bl, emotion);
  11464. return 0;
  11465. }
  11466. /// Makes the unit cast the skill on the target or self if no target is specified
  11467. ///
  11468. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11469. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11470. BUILDIN_FUNC(unitskilluseid)
  11471. {
  11472. int unit_id;
  11473. int skill_id;
  11474. int skill_lv;
  11475. int target_id;
  11476. struct block_list* bl;
  11477. unit_id = script_getnum(st,2);
  11478. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11479. skill_lv = script_getnum(st,4);
  11480. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11481. bl = map_id2bl(unit_id);
  11482. if( bl != NULL )
  11483. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11484. return 0;
  11485. }
  11486. /// Makes the unit cast the skill on the target position.
  11487. ///
  11488. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11489. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11490. BUILDIN_FUNC(unitskillusepos)
  11491. {
  11492. int unit_id;
  11493. int skill_id;
  11494. int skill_lv;
  11495. int skill_x;
  11496. int skill_y;
  11497. struct block_list* bl;
  11498. unit_id = script_getnum(st,2);
  11499. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11500. skill_lv = script_getnum(st,4);
  11501. skill_x = script_getnum(st,5);
  11502. skill_y = script_getnum(st,6);
  11503. bl = map_id2bl(unit_id);
  11504. if( bl != NULL )
  11505. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11506. return 0;
  11507. }
  11508. // <--- [zBuffer] List of mob control commands
  11509. /// Pauses the execution of the script, detaching the player
  11510. ///
  11511. /// sleep <mili seconds>;
  11512. BUILDIN_FUNC(sleep)
  11513. {
  11514. int ticks;
  11515. TBL_PC* sd;
  11516. ticks = script_getnum(st,2);
  11517. sd = map_id2sd(st->rid);
  11518. // detach the player
  11519. if( sd && sd->npc_id == st->oid )
  11520. {
  11521. sd->npc_id = 0;
  11522. }
  11523. st->rid = 0;
  11524. if( ticks <= 0 )
  11525. {// do nothing
  11526. }
  11527. else if( st->sleep.tick == 0 )
  11528. {// sleep for the target amount of time
  11529. st->state = RERUNLINE;
  11530. st->sleep.tick = ticks;
  11531. }
  11532. else
  11533. {// sleep time is over
  11534. st->state = RUN;
  11535. st->sleep.tick = 0;
  11536. }
  11537. return 0;
  11538. }
  11539. /// Pauses the execution of the script, keeping the player attached
  11540. /// Returns if a player is still attached
  11541. ///
  11542. /// sleep2(<mili secconds>) -> <bool>
  11543. BUILDIN_FUNC(sleep2)
  11544. {
  11545. int ticks;
  11546. ticks = script_getnum(st,2);
  11547. if( ticks <= 0 )
  11548. {// do nothing
  11549. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11550. }
  11551. else if( !st->sleep.tick )
  11552. {// sleep for the target amount of time
  11553. st->state = RERUNLINE;
  11554. st->sleep.tick = ticks;
  11555. }
  11556. else
  11557. {// sleep time is over
  11558. st->state = RUN;
  11559. st->sleep.tick = 0;
  11560. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11561. }
  11562. return 0;
  11563. }
  11564. /// Awakes all the sleep timers of the target npc
  11565. ///
  11566. /// awake "<npc name>";
  11567. BUILDIN_FUNC(awake)
  11568. {
  11569. struct npc_data* nd;
  11570. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11571. nd = npc_name2id(script_getstr(st, 2));
  11572. if( nd == NULL ) {
  11573. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  11574. return 1;
  11575. }
  11576. while( node )
  11577. {
  11578. if( (int)node->key == nd->bl.id )
  11579. {// sleep timer for the npc
  11580. struct script_state* tst = (struct script_state*)node->data;
  11581. TBL_PC* sd = map_id2sd(tst->rid);
  11582. if( tst->sleep.timer == -1 )
  11583. {// already awake ???
  11584. node = node->next;
  11585. continue;
  11586. }
  11587. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11588. {// char not online anymore / another char of the same account is online - Cancel execution
  11589. tst->state = END;
  11590. tst->rid = 0;
  11591. }
  11592. delete_timer(tst->sleep.timer, run_script_timer);
  11593. node = script_erase_sleepdb(node);
  11594. tst->sleep.timer = INVALID_TIMER;
  11595. if(tst->state != RERUNLINE)
  11596. tst->sleep.tick = 0;
  11597. run_script_main(tst);
  11598. }
  11599. else
  11600. {
  11601. node = node->next;
  11602. }
  11603. }
  11604. return 0;
  11605. }
  11606. /// Returns a reference to a variable of the target NPC.
  11607. /// Returns 0 if an error occurs.
  11608. ///
  11609. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11610. BUILDIN_FUNC(getvariableofnpc)
  11611. {
  11612. struct script_data* data;
  11613. const char* name;
  11614. struct npc_data* nd;
  11615. data = script_getdata(st,2);
  11616. if( !data_isreference(data) )
  11617. {// Not a reference (aka varaible name)
  11618. ShowError("script:getvariableofnpc: not a variable\n");
  11619. script_reportdata(data);
  11620. script_pushnil(st);
  11621. st->state = END;
  11622. return 1;
  11623. }
  11624. name = reference_getname(data);
  11625. if( *name != '.' || name[1] == '@' )
  11626. {// not a npc variable
  11627. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11628. script_reportdata(data);
  11629. script_pushnil(st);
  11630. st->state = END;
  11631. return 1;
  11632. }
  11633. nd = npc_name2id(script_getstr(st,3));
  11634. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11635. {// NPC not found or has no script
  11636. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11637. script_pushnil(st);
  11638. st->state = END;
  11639. return 1;
  11640. }
  11641. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11642. return 0;
  11643. }
  11644. /// Opens a warp portal.
  11645. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11646. ///
  11647. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11648. ///
  11649. /// @author blackhole89
  11650. BUILDIN_FUNC(warpportal)
  11651. {
  11652. int spx;
  11653. int spy;
  11654. unsigned short mapindex;
  11655. int tpx;
  11656. int tpy;
  11657. struct skill_unit_group* group;
  11658. struct block_list* bl;
  11659. bl = map_id2bl(st->oid);
  11660. if( bl == NULL )
  11661. {
  11662. ShowError("script:warpportal: npc is needed\n");
  11663. return 1;
  11664. }
  11665. spx = script_getnum(st,2);
  11666. spy = script_getnum(st,3);
  11667. mapindex = mapindex_name2id(script_getstr(st, 4));
  11668. tpx = script_getnum(st,5);
  11669. tpy = script_getnum(st,6);
  11670. if( mapindex == 0 )
  11671. return 0;// map not found
  11672. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11673. if( group == NULL )
  11674. return 0;// failed
  11675. group->val2 = (tpx<<16) | tpy;
  11676. group->val3 = mapindex;
  11677. return 0;
  11678. }
  11679. BUILDIN_FUNC(openmail)
  11680. {
  11681. TBL_PC* sd;
  11682. sd = script_rid2sd(st);
  11683. if( sd == NULL )
  11684. return 0;
  11685. #ifndef TXT_ONLY
  11686. mail_openmail(sd);
  11687. #endif
  11688. return 0;
  11689. }
  11690. BUILDIN_FUNC(openauction)
  11691. {
  11692. TBL_PC* sd;
  11693. sd = script_rid2sd(st);
  11694. if( sd == NULL )
  11695. return 0;
  11696. #ifndef TXT_ONLY
  11697. clif_Auction_openwindow(sd);
  11698. #endif
  11699. return 0;
  11700. }
  11701. /// Retrieves the value of the specified flag of the specified cell.
  11702. ///
  11703. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11704. ///
  11705. /// @see cell_chk* constants in const.txt for the types
  11706. BUILDIN_FUNC(checkcell)
  11707. {
  11708. int m = map_mapname2mapid(script_getstr(st,2));
  11709. int x = script_getnum(st,3);
  11710. int y = script_getnum(st,4);
  11711. cell_chk type = (cell_chk)script_getnum(st,5);
  11712. script_pushint(st, map_getcell(m, x, y, type));
  11713. return 0;
  11714. }
  11715. /// Modifies flags of cells in the specified area.
  11716. ///
  11717. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11718. ///
  11719. /// @see cell_* constants in const.txt for the types
  11720. BUILDIN_FUNC(setcell)
  11721. {
  11722. int m = map_mapname2mapid(script_getstr(st,2));
  11723. int x1 = script_getnum(st,3);
  11724. int y1 = script_getnum(st,4);
  11725. int x2 = script_getnum(st,5);
  11726. int y2 = script_getnum(st,6);
  11727. cell_t type = (cell_t)script_getnum(st,7);
  11728. bool flag = (bool)script_getnum(st,8);
  11729. int x,y;
  11730. if( x1 > x2 ) swap(x1,x2);
  11731. if( y1 > y2 ) swap(y1,y2);
  11732. for( y = y1; y <= y2; ++y )
  11733. for( x = x1; x <= x2; ++x )
  11734. map_setcell(m, x, y, type, flag);
  11735. return 0;
  11736. }
  11737. /*==========================================
  11738. * Mercenary Commands
  11739. *------------------------------------------*/
  11740. BUILDIN_FUNC(mercenary_create)
  11741. {
  11742. struct map_session_data *sd;
  11743. int class_, contract_time;
  11744. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11745. return 0;
  11746. class_ = script_getnum(st,2);
  11747. if( !merc_class(class_) )
  11748. return 0;
  11749. contract_time = script_getnum(st,3);
  11750. merc_create(sd, class_, contract_time);
  11751. return 0;
  11752. }
  11753. BUILDIN_FUNC(mercenary_heal)
  11754. {
  11755. struct map_session_data *sd = script_rid2sd(st);
  11756. int hp, sp;
  11757. if( sd == NULL || sd->md == NULL )
  11758. return 0;
  11759. hp = script_getnum(st,2);
  11760. sp = script_getnum(st,3);
  11761. status_heal(&sd->md->bl, hp, sp, 0);
  11762. return 0;
  11763. }
  11764. BUILDIN_FUNC(mercenary_sc_start)
  11765. {
  11766. struct map_session_data *sd = script_rid2sd(st);
  11767. enum sc_type type;
  11768. int tick, val1;
  11769. if( sd == NULL || sd->md == NULL )
  11770. return 0;
  11771. type = (sc_type)script_getnum(st,2);
  11772. tick = script_getnum(st,3);
  11773. val1 = script_getnum(st,4);
  11774. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11775. return 0;
  11776. }
  11777. BUILDIN_FUNC(mercenary_get_calls)
  11778. {
  11779. struct map_session_data *sd = script_rid2sd(st);
  11780. int guild;
  11781. if( sd == NULL )
  11782. return 0;
  11783. guild = script_getnum(st,2);
  11784. switch( guild )
  11785. {
  11786. case ARCH_MERC_GUILD:
  11787. script_pushint(st,sd->status.arch_calls);
  11788. break;
  11789. case SPEAR_MERC_GUILD:
  11790. script_pushint(st,sd->status.spear_calls);
  11791. break;
  11792. case SWORD_MERC_GUILD:
  11793. script_pushint(st,sd->status.sword_calls);
  11794. break;
  11795. default:
  11796. script_pushint(st,0);
  11797. break;
  11798. }
  11799. return 0;
  11800. }
  11801. BUILDIN_FUNC(mercenary_set_calls)
  11802. {
  11803. struct map_session_data *sd = script_rid2sd(st);
  11804. int guild, value, *calls;
  11805. if( sd == NULL )
  11806. return 0;
  11807. guild = script_getnum(st,2);
  11808. value = script_getnum(st,3);
  11809. switch( guild )
  11810. {
  11811. case ARCH_MERC_GUILD:
  11812. calls = &sd->status.arch_calls;
  11813. break;
  11814. case SPEAR_MERC_GUILD:
  11815. calls = &sd->status.spear_calls;
  11816. break;
  11817. case SWORD_MERC_GUILD:
  11818. calls = &sd->status.sword_calls;
  11819. break;
  11820. default:
  11821. return 0; // Invalid Guild
  11822. }
  11823. *calls += value;
  11824. *calls = cap_value(*calls, 0, INT_MAX);
  11825. return 0;
  11826. }
  11827. BUILDIN_FUNC(mercenary_get_faith)
  11828. {
  11829. struct map_session_data *sd = script_rid2sd(st);
  11830. int guild;
  11831. if( sd == NULL )
  11832. return 0;
  11833. guild = script_getnum(st,2);
  11834. switch( guild )
  11835. {
  11836. case ARCH_MERC_GUILD:
  11837. script_pushint(st,sd->status.arch_faith);
  11838. break;
  11839. case SPEAR_MERC_GUILD:
  11840. script_pushint(st,sd->status.spear_faith);
  11841. break;
  11842. case SWORD_MERC_GUILD:
  11843. script_pushint(st,sd->status.sword_faith);
  11844. break;
  11845. default:
  11846. script_pushint(st,0);
  11847. break;
  11848. }
  11849. return 0;
  11850. }
  11851. BUILDIN_FUNC(mercenary_set_faith)
  11852. {
  11853. struct map_session_data *sd = script_rid2sd(st);
  11854. int guild, value, *calls;
  11855. if( sd == NULL )
  11856. return 0;
  11857. guild = script_getnum(st,2);
  11858. value = script_getnum(st,3);
  11859. switch( guild )
  11860. {
  11861. case ARCH_MERC_GUILD:
  11862. calls = &sd->status.arch_faith;
  11863. break;
  11864. case SPEAR_MERC_GUILD:
  11865. calls = &sd->status.spear_faith;
  11866. break;
  11867. case SWORD_MERC_GUILD:
  11868. calls = &sd->status.sword_faith;
  11869. break;
  11870. default:
  11871. return 0; // Invalid Guild
  11872. }
  11873. *calls += value;
  11874. *calls = cap_value(*calls, 0, INT_MAX);
  11875. if( mercenary_get_guild(sd->md) == guild )
  11876. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  11877. return 0;
  11878. }
  11879. /*------------------------------------------
  11880. * Book Reading
  11881. *------------------------------------------*/
  11882. BUILDIN_FUNC(readbook)
  11883. {
  11884. struct map_session_data *sd;
  11885. int book_id, page;
  11886. if( (sd = script_rid2sd(st)) == NULL )
  11887. return 0;
  11888. book_id = script_getnum(st,2);
  11889. page = script_getnum(st,3);
  11890. clif_readbook(sd->fd, book_id, page);
  11891. return 0;
  11892. }
  11893. /******************
  11894. Questlog script commands
  11895. *******************/
  11896. BUILDIN_FUNC(setquest)
  11897. {
  11898. TBL_PC * sd = script_rid2sd(st);
  11899. quest_add(sd, script_getnum(st, 2));
  11900. return 0;
  11901. }
  11902. BUILDIN_FUNC(erasequest)
  11903. {
  11904. TBL_PC * sd = script_rid2sd(st);
  11905. quest_delete(sd, script_getnum(st, 2));
  11906. return 0;
  11907. }
  11908. BUILDIN_FUNC(completequest)
  11909. {
  11910. TBL_PC * sd = script_rid2sd(st);
  11911. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  11912. return 0;
  11913. }
  11914. BUILDIN_FUNC(changequest)
  11915. {
  11916. TBL_PC * sd = script_rid2sd(st);
  11917. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  11918. return 0;
  11919. }
  11920. BUILDIN_FUNC(checkquest)
  11921. {
  11922. TBL_PC * sd = script_rid2sd(st);
  11923. quest_check_type type = HAVEQUEST;
  11924. if( script_hasdata(st, 3) )
  11925. type = (quest_check_type)script_getnum(st, 3);
  11926. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  11927. return 0;
  11928. }
  11929. BUILDIN_FUNC(showevent)
  11930. {
  11931. TBL_PC *sd = script_rid2sd(st);
  11932. struct npc_data *nd = map_id2nd(st->oid);
  11933. int state, color;
  11934. if( sd == NULL || nd == NULL )
  11935. return 0;
  11936. state = script_getnum(st, 2);
  11937. color = script_getnum(st, 3);
  11938. if( color < 0 || color > 4 )
  11939. color = 0; // set default color
  11940. clif_quest_show_event(sd, &nd->bl, state, color);
  11941. return 0;
  11942. }
  11943. /*==========================================
  11944. * BattleGround System
  11945. *------------------------------------------*/
  11946. BUILDIN_FUNC(waitingroom2bg)
  11947. {
  11948. struct npc_data *nd;
  11949. struct chat_data *cd;
  11950. const char *map_name, *ev = "", *dev = "";
  11951. int x, y, i, mapindex = 0, bg_id, n;
  11952. struct map_session_data *sd;
  11953. if( script_hasdata(st,7) )
  11954. nd = npc_name2id(script_getstr(st,7));
  11955. else
  11956. nd = (struct npc_data *)map_id2bl(st->oid);
  11957. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11958. {
  11959. script_pushint(st,0);
  11960. return 0;
  11961. }
  11962. map_name = script_getstr(st,2);
  11963. if( strcmp(map_name,"-") != 0 )
  11964. {
  11965. mapindex = mapindex_name2id(map_name);
  11966. if( mapindex == 0 )
  11967. { // Invalid Map
  11968. script_pushint(st,0);
  11969. return 0;
  11970. }
  11971. }
  11972. x = script_getnum(st,3);
  11973. y = script_getnum(st,4);
  11974. ev = script_getstr(st,5); // Logout Event
  11975. dev = script_getstr(st,6); // Die Event
  11976. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  11977. { // Creation failed
  11978. script_pushint(st,0);
  11979. return 0;
  11980. }
  11981. n = cd->users;
  11982. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  11983. {
  11984. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  11985. mapreg_setreg(add_str("$@arenamembers") + (i<<24), sd->bl.id);
  11986. else
  11987. mapreg_setreg(add_str("$@arenamembers") + (i<<24), 0);
  11988. }
  11989. mapreg_setreg(add_str("$@arenamembersnum"), i);
  11990. script_pushint(st,bg_id);
  11991. return 0;
  11992. }
  11993. BUILDIN_FUNC(waitingroom2bg_single)
  11994. {
  11995. const char* map_name;
  11996. struct npc_data *nd;
  11997. struct chat_data *cd;
  11998. struct map_session_data *sd;
  11999. int x, y, mapindex, bg_id;
  12000. bg_id = script_getnum(st,2);
  12001. map_name = script_getstr(st,3);
  12002. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12003. return 0; // Invalid Map
  12004. x = script_getnum(st,4);
  12005. y = script_getnum(st,5);
  12006. nd = npc_name2id(script_getstr(st,6));
  12007. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  12008. return 0;
  12009. if( (sd = cd->usersd[0]) == NULL )
  12010. return 0;
  12011. if( bg_team_join(bg_id, sd) )
  12012. {
  12013. pc_setpos(sd, mapindex, x, y, 3);
  12014. script_pushint(st,1);
  12015. }
  12016. else
  12017. script_pushint(st,0);
  12018. return 0;
  12019. }
  12020. BUILDIN_FUNC(bg_team_setxy)
  12021. {
  12022. struct battleground_data *bg;
  12023. int bg_id;
  12024. bg_id = script_getnum(st,2);
  12025. if( (bg = bg_team_search(bg_id)) == NULL )
  12026. return 0;
  12027. bg->x = script_getnum(st,3);
  12028. bg->y = script_getnum(st,4);
  12029. return 0;
  12030. }
  12031. BUILDIN_FUNC(bg_warp)
  12032. {
  12033. int x, y, mapindex, bg_id;
  12034. const char* map_name;
  12035. bg_id = script_getnum(st,2);
  12036. map_name = script_getstr(st,3);
  12037. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12038. return 0; // Invalid Map
  12039. x = script_getnum(st,4);
  12040. y = script_getnum(st,5);
  12041. bg_team_warp(bg_id, mapindex, x, y);
  12042. return 0;
  12043. }
  12044. BUILDIN_FUNC(bg_monster)
  12045. {
  12046. int class_ = 0, x = 0, y = 0, bg_id = 0;
  12047. const char *str,*map, *evt="";
  12048. bg_id = script_getnum(st,2);
  12049. map = script_getstr(st,3);
  12050. x = script_getnum(st,4);
  12051. y = script_getnum(st,5);
  12052. str = script_getstr(st,6);
  12053. class_ = script_getnum(st,7);
  12054. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  12055. check_event(st, evt);
  12056. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  12057. return 0;
  12058. }
  12059. BUILDIN_FUNC(bg_monster_set_team)
  12060. {
  12061. struct mob_data *md;
  12062. struct block_list *mbl;
  12063. int id = script_getnum(st,2),
  12064. bg_id = script_getnum(st,3);
  12065. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  12066. return 0;
  12067. md = (TBL_MOB *)mbl;
  12068. md->state.bg_id = bg_id;
  12069. mob_stop_attack(md);
  12070. mob_stop_walking(md, 0);
  12071. md->target_id = md->attacked_id = 0;
  12072. clif_charnameack(0, &md->bl);
  12073. return 0;
  12074. }
  12075. BUILDIN_FUNC(bg_leave)
  12076. {
  12077. struct map_session_data *sd = script_rid2sd(st);
  12078. if( sd == NULL || !sd->state.bg_id )
  12079. return 0;
  12080. bg_team_leave(sd,0);
  12081. return 0;
  12082. }
  12083. BUILDIN_FUNC(bg_destroy)
  12084. {
  12085. int bg_id = script_getnum(st,2);
  12086. bg_team_delete(bg_id);
  12087. return 0;
  12088. }
  12089. BUILDIN_FUNC(bg_getareausers)
  12090. {
  12091. const char *str;
  12092. int m, x0, y0, x1, y1, bg_id;
  12093. int i = 0, c = 0;
  12094. struct battleground_data *bg = NULL;
  12095. struct map_session_data *sd;
  12096. bg_id = script_getnum(st,2);
  12097. str = script_getstr(st,3);
  12098. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  12099. {
  12100. script_pushint(st,0);
  12101. return 0;
  12102. }
  12103. x0 = script_getnum(st,4);
  12104. y0 = script_getnum(st,5);
  12105. x1 = script_getnum(st,6);
  12106. y1 = script_getnum(st,7);
  12107. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  12108. {
  12109. if( (sd = bg->members[i].sd) == NULL )
  12110. continue;
  12111. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  12112. continue;
  12113. c++;
  12114. }
  12115. script_pushint(st,c);
  12116. return 0;
  12117. }
  12118. BUILDIN_FUNC(bg_updatescore)
  12119. {
  12120. const char *str;
  12121. int m;
  12122. str = script_getstr(st,2);
  12123. if( (m = map_mapname2mapid(str)) < 0 )
  12124. return 0;
  12125. map[m].bgscore_lion = script_getnum(st,3);
  12126. map[m].bgscore_eagle = script_getnum(st,4);
  12127. clif_bg_updatescore(m);
  12128. return 0;
  12129. }
  12130. BUILDIN_FUNC(bg_get_data)
  12131. {
  12132. struct battleground_data *bg;
  12133. int bg_id = script_getnum(st,2),
  12134. type = script_getnum(st,3);
  12135. if( (bg = bg_team_search(bg_id)) == NULL )
  12136. {
  12137. script_pushint(st,0);
  12138. return 0;
  12139. }
  12140. switch( type )
  12141. {
  12142. case 0: script_pushint(st, bg->count); break;
  12143. default:
  12144. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  12145. break;
  12146. }
  12147. return 0;
  12148. }
  12149. /*==========================================
  12150. * Instancing Script Commands
  12151. *------------------------------------------*/
  12152. BUILDIN_FUNC(instance_create)
  12153. {
  12154. const char *name;
  12155. int party_id, res;
  12156. name = script_getstr(st, 2);
  12157. party_id = script_getnum(st, 3);
  12158. res = instance_create(party_id, name);
  12159. if( res == -4 ) // Already exists
  12160. {
  12161. script_pushint(st, -1);
  12162. return 0;
  12163. }
  12164. else if( res < 0 )
  12165. {
  12166. char *err;
  12167. switch(res)
  12168. {
  12169. case -3: err = "No free instances"; break;
  12170. case -2: err = "Missing parameter"; break;
  12171. case -1: err = "Invalid type"; break;
  12172. default: err = "Unknown"; break;
  12173. }
  12174. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  12175. script_pushint(st, -2);
  12176. return 0;
  12177. }
  12178. script_pushint(st, res);
  12179. return 0;
  12180. }
  12181. BUILDIN_FUNC(instance_destroy)
  12182. {
  12183. int instance_id;
  12184. struct map_session_data *sd;
  12185. struct party_data *p;
  12186. if( script_hasdata(st, 2) )
  12187. instance_id = script_getnum(st, 2);
  12188. else if( st->instance_id )
  12189. instance_id = st->instance_id;
  12190. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12191. instance_id = p->instance_id;
  12192. else return 0;
  12193. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12194. {
  12195. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  12196. return 0;
  12197. }
  12198. instance_destroy(instance_id);
  12199. return 0;
  12200. }
  12201. BUILDIN_FUNC(instance_attachmap)
  12202. {
  12203. const char *name;
  12204. int m;
  12205. int instance_id;
  12206. bool usebasename = false;
  12207. name = script_getstr(st,2);
  12208. instance_id = script_getnum(st,3);
  12209. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  12210. usebasename = true;
  12211. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  12212. {
  12213. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  12214. script_pushconststr(st, "");
  12215. return 0;
  12216. }
  12217. script_pushconststr(st, map[m].name);
  12218. return 0;
  12219. }
  12220. BUILDIN_FUNC(instance_detachmap)
  12221. {
  12222. struct map_session_data *sd;
  12223. struct party_data *p;
  12224. const char *str;
  12225. int m, instance_id;
  12226. str = script_getstr(st, 2);
  12227. if( script_hasdata(st, 3) )
  12228. instance_id = script_getnum(st, 3);
  12229. else if( st->instance_id )
  12230. instance_id = st->instance_id;
  12231. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12232. instance_id = p->instance_id;
  12233. else return 0;
  12234. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  12235. {
  12236. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  12237. return 0;
  12238. }
  12239. instance_del_map(m);
  12240. return 0;
  12241. }
  12242. BUILDIN_FUNC(instance_attach)
  12243. {
  12244. int instance_id;
  12245. instance_id = script_getnum(st, 2);
  12246. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12247. return 0;
  12248. st->instance_id = instance_id;
  12249. return 0;
  12250. }
  12251. BUILDIN_FUNC(instance_id)
  12252. {
  12253. int type, instance_id;
  12254. struct map_session_data *sd;
  12255. struct party_data *p;
  12256. if( script_hasdata(st, 2) )
  12257. {
  12258. type = script_getnum(st, 2);
  12259. if( type == 0 )
  12260. instance_id = st->instance_id;
  12261. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  12262. instance_id = p->instance_id;
  12263. else
  12264. instance_id = 0;
  12265. }
  12266. else
  12267. instance_id = st->instance_id;
  12268. script_pushint(st, instance_id);
  12269. return 0;
  12270. }
  12271. BUILDIN_FUNC(instance_set_timeout)
  12272. {
  12273. int progress_timeout, idle_timeout;
  12274. int instance_id;
  12275. struct map_session_data *sd;
  12276. struct party_data *p;
  12277. progress_timeout = script_getnum(st, 2);
  12278. idle_timeout = script_getnum(st, 3);
  12279. if( script_hasdata(st, 4) )
  12280. instance_id = script_getnum(st, 4);
  12281. else if( st->instance_id )
  12282. instance_id = st->instance_id;
  12283. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12284. instance_id = p->instance_id;
  12285. else return 0;
  12286. if( instance_id > 0 )
  12287. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  12288. return 0;
  12289. }
  12290. BUILDIN_FUNC(instance_init)
  12291. {
  12292. int instance_id = script_getnum(st, 2);
  12293. instance_init(instance_id);
  12294. return 0;
  12295. }
  12296. BUILDIN_FUNC(instance_announce)
  12297. {
  12298. int instance_id = script_getnum(st,2);
  12299. const char *mes = script_getstr(st,3);
  12300. int flag = script_getnum(st,4);
  12301. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  12302. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  12303. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  12304. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  12305. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  12306. int i;
  12307. struct map_session_data *sd;
  12308. struct party_data *p;
  12309. if( instance_id == 0 )
  12310. {
  12311. if( st->instance_id )
  12312. instance_id = st->instance_id;
  12313. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12314. instance_id = p->instance_id;
  12315. else return 0;
  12316. }
  12317. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12318. return 0;
  12319. for( i = 0; i < instance[instance_id].num_map; i++ )
  12320. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  12321. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  12322. return 0;
  12323. }
  12324. BUILDIN_FUNC(instance_npcname)
  12325. {
  12326. const char *str;
  12327. int instance_id = 0;
  12328. struct map_session_data *sd;
  12329. struct party_data *p;
  12330. struct npc_data *nd;
  12331. str = script_getstr(st, 2);
  12332. if( script_hasdata(st, 3) )
  12333. instance_id = script_getnum(st, 3);
  12334. else if( st->instance_id )
  12335. instance_id = st->instance_id;
  12336. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12337. instance_id = p->instance_id;
  12338. if( instance_id && (nd = npc_name2id(str)) != NULL )
  12339. {
  12340. static char npcname[NAME_LENGTH];
  12341. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  12342. script_pushconststr(st,npcname);
  12343. }
  12344. else
  12345. script_pushconststr(st,"");
  12346. return 0;
  12347. }
  12348. BUILDIN_FUNC(has_instance)
  12349. {
  12350. struct map_session_data *sd;
  12351. struct party_data *p;
  12352. const char *str;
  12353. int m, instance_id = 0;
  12354. str = script_getstr(st, 2);
  12355. if( script_hasdata(st, 3) )
  12356. instance_id = script_getnum(st, 3);
  12357. else if( st->instance_id )
  12358. instance_id = st->instance_id;
  12359. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12360. instance_id = p->instance_id;
  12361. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  12362. {
  12363. script_pushconststr(st, "");
  12364. return 0;
  12365. }
  12366. script_pushconststr(st, map[m].name);
  12367. return 0;
  12368. }
  12369. BUILDIN_FUNC(instance_warpall)
  12370. {
  12371. struct map_session_data *pl_sd;
  12372. int m, i, instance_id;
  12373. const char *mapn;
  12374. int x, y;
  12375. unsigned short mapindex;
  12376. struct party_data *p = NULL;
  12377. mapn = script_getstr(st,2);
  12378. x = script_getnum(st,3);
  12379. y = script_getnum(st,4);
  12380. if( script_hasdata(st,5) )
  12381. instance_id = script_getnum(st,5);
  12382. else if( st->instance_id )
  12383. instance_id = st->instance_id;
  12384. 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 )
  12385. instance_id = p->instance_id;
  12386. else return 0;
  12387. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  12388. return 0;
  12389. if( !(p = party_search(instance[instance_id].party_id)) )
  12390. return 0;
  12391. mapindex = map_id2index(m);
  12392. for( i = 0; i < MAX_PARTY; i++ )
  12393. 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);
  12394. return 0;
  12395. }
  12396. /*==========================================
  12397. * Custom Fonts
  12398. *------------------------------------------*/
  12399. BUILDIN_FUNC(setfont)
  12400. {
  12401. struct map_session_data *sd = script_rid2sd(st);
  12402. int font = script_getnum(st,2);
  12403. if( sd == NULL )
  12404. return 0;
  12405. if( sd->state.user_font != font )
  12406. sd->state.user_font = font;
  12407. else
  12408. sd->state.user_font = 0;
  12409. clif_font_area(sd);
  12410. return 0;
  12411. }
  12412. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  12413. {
  12414. TBL_MOB* md = (TBL_MOB*)bl;
  12415. struct block_list *tbl;
  12416. int mobid = va_arg(ap,int);
  12417. int skillid = va_arg(ap,int);
  12418. int skilllv = va_arg(ap,int);
  12419. int casttime = va_arg(ap,int);
  12420. int cancel = va_arg(ap,int);
  12421. int emotion = va_arg(ap,int);
  12422. int target = va_arg(ap,int);
  12423. if( md->class_ != mobid )
  12424. return 0;
  12425. if( md->ud.skilltimer != -1 ) // Cancel the casting skill.
  12426. unit_skillcastcancel(bl,0);
  12427. // 0:self, 1:target, 2:master, default:random
  12428. switch( target )
  12429. {
  12430. case 0: tbl = map_id2bl(md->bl.id); break;
  12431. case 1: tbl = map_id2bl(md->target_id); break;
  12432. case 2: tbl = map_id2bl(md->master_id); break;
  12433. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  12434. }
  12435. if( !tbl )
  12436. return 0;
  12437. if( skill_get_casttype(skillid) == CAST_GROUND )
  12438. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  12439. else
  12440. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  12441. clif_emotion(&md->bl, emotion);
  12442. return 0;
  12443. }
  12444. /*==========================================
  12445. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  12446. *------------------------------------------*/
  12447. BUILDIN_FUNC(areamobuseskill)
  12448. {
  12449. struct block_list center;
  12450. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  12451. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  12452. {
  12453. ShowError("areamobuseskill: invalid map name.\n");
  12454. return 0;
  12455. }
  12456. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  12457. return 0;
  12458. center.m = m;
  12459. center.x = script_getnum(st,3);
  12460. center.y = script_getnum(st,4);
  12461. range = script_getnum(st,5);
  12462. mobid = script_getnum(st,6);
  12463. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  12464. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  12465. skilllv = battle_config.mob_max_skilllvl;
  12466. casttime = script_getnum(st,9);
  12467. cancel = script_getnum(st,10);
  12468. emotion = script_getnum(st,11);
  12469. target = script_getnum(st,12);
  12470. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  12471. return 0;
  12472. }
  12473. BUILDIN_FUNC(progressbar)
  12474. {
  12475. #if PACKETVER >= 20080318
  12476. struct map_session_data * sd = script_rid2sd(st);
  12477. const char * color;
  12478. unsigned int second;
  12479. if( !st || !sd )
  12480. return 0;
  12481. st->state = STOP;
  12482. color = script_getstr(st,2);
  12483. second = script_getnum(st,3);
  12484. sd->progressbar.npc_id = st->oid;
  12485. sd->progressbar.timeout = gettick() + second*1000;
  12486. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  12487. #endif
  12488. return 0;
  12489. }
  12490. // declarations that were supposed to be exported from npc_chat.c
  12491. #ifdef PCRE_SUPPORT
  12492. BUILDIN_FUNC(defpattern);
  12493. BUILDIN_FUNC(activatepset);
  12494. BUILDIN_FUNC(deactivatepset);
  12495. BUILDIN_FUNC(deletepset);
  12496. #endif
  12497. struct script_function buildin_func[] = {
  12498. // NPC interaction
  12499. BUILDIN_DEF(mes,"s"),
  12500. BUILDIN_DEF(next,""),
  12501. BUILDIN_DEF(close,""),
  12502. BUILDIN_DEF(close2,""),
  12503. BUILDIN_DEF(menu,"sl*"),
  12504. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  12505. BUILDIN_DEF(prompt,"s*"),
  12506. //
  12507. BUILDIN_DEF(goto,"l"),
  12508. BUILDIN_DEF(callsub,"i*"),
  12509. BUILDIN_DEF(callfunc,"s*"),
  12510. BUILDIN_DEF(return,"?"),
  12511. BUILDIN_DEF(getarg,"i?"),
  12512. BUILDIN_DEF(jobchange,"i*"),
  12513. BUILDIN_DEF(jobname,"i"),
  12514. BUILDIN_DEF(input,"v??"),
  12515. BUILDIN_DEF(warp,"sii"),
  12516. BUILDIN_DEF(areawarp,"siiiisii"),
  12517. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  12518. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  12519. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  12520. BUILDIN_DEF(setlook,"ii"),
  12521. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  12522. BUILDIN_DEF(set,"ii"),
  12523. BUILDIN_DEF(setarray,"rv*"),
  12524. BUILDIN_DEF(cleararray,"rvi"),
  12525. BUILDIN_DEF(copyarray,"rri"),
  12526. BUILDIN_DEF(getarraysize,"r"),
  12527. BUILDIN_DEF(deletearray,"r?"),
  12528. BUILDIN_DEF(getelementofarray,"ri"),
  12529. BUILDIN_DEF(getitem,"vi?"),
  12530. BUILDIN_DEF(rentitem,"vi"),
  12531. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  12532. BUILDIN_DEF(getnameditem,"is"),
  12533. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  12534. BUILDIN_DEF(makeitem,"iisii"),
  12535. BUILDIN_DEF(delitem,"ii?"),
  12536. BUILDIN_DEF(delitem2,"iiiiiiiii?"),
  12537. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  12538. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  12539. BUILDIN_DEF(cutin,"si"),
  12540. BUILDIN_DEF(viewpoint,"iiiii"),
  12541. BUILDIN_DEF(heal,"ii"),
  12542. BUILDIN_DEF(itemheal,"ii"),
  12543. BUILDIN_DEF(percentheal,"ii"),
  12544. BUILDIN_DEF(rand,"i?"),
  12545. BUILDIN_DEF(countitem,"i"),
  12546. BUILDIN_DEF(countitem2,"iiiiiiii"),
  12547. BUILDIN_DEF(checkweight,"ii"),
  12548. BUILDIN_DEF(readparam,"i*"),
  12549. BUILDIN_DEF(getcharid,"i*"),
  12550. BUILDIN_DEF(getpartyname,"i"),
  12551. BUILDIN_DEF(getpartymember,"i*"),
  12552. BUILDIN_DEF(getpartyleader,"i?"),
  12553. BUILDIN_DEF(getguildname,"i"),
  12554. BUILDIN_DEF(getguildmaster,"i"),
  12555. BUILDIN_DEF(getguildmasterid,"i"),
  12556. BUILDIN_DEF(strcharinfo,"i"),
  12557. BUILDIN_DEF(strnpcinfo,"i"),
  12558. BUILDIN_DEF(getequipid,"i"),
  12559. BUILDIN_DEF(getequipname,"i"),
  12560. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  12561. BUILDIN_DEF(repair,"i"), // [Valaris]
  12562. BUILDIN_DEF(getequipisequiped,"i"),
  12563. BUILDIN_DEF(getequipisenableref,"i"),
  12564. BUILDIN_DEF(getequipisidentify,"i"),
  12565. BUILDIN_DEF(getequiprefinerycnt,"i"),
  12566. BUILDIN_DEF(getequipweaponlv,"i"),
  12567. BUILDIN_DEF(getequippercentrefinery,"i"),
  12568. BUILDIN_DEF(successrefitem,"i"),
  12569. BUILDIN_DEF(failedrefitem,"i"),
  12570. BUILDIN_DEF(statusup,"i"),
  12571. BUILDIN_DEF(statusup2,"ii"),
  12572. BUILDIN_DEF(bonus,"iv"),
  12573. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  12574. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  12575. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  12576. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  12577. BUILDIN_DEF(autobonus,"sii??"),
  12578. BUILDIN_DEF(autobonus2,"sii??"),
  12579. BUILDIN_DEF(autobonus3,"siiv?"),
  12580. BUILDIN_DEF(skill,"vi?"),
  12581. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  12582. BUILDIN_DEF(guildskill,"vi"),
  12583. BUILDIN_DEF(getskilllv,"v"),
  12584. BUILDIN_DEF(getgdskilllv,"iv"),
  12585. BUILDIN_DEF(basicskillcheck,""),
  12586. BUILDIN_DEF(getgmlevel,""),
  12587. BUILDIN_DEF(end,""),
  12588. BUILDIN_DEF(checkoption,"i"),
  12589. BUILDIN_DEF(setoption,"i?"),
  12590. BUILDIN_DEF(setcart,"?"),
  12591. BUILDIN_DEF(checkcart,""),
  12592. BUILDIN_DEF(setfalcon,"?"),
  12593. BUILDIN_DEF(checkfalcon,""),
  12594. BUILDIN_DEF(setriding,"?"),
  12595. BUILDIN_DEF(checkriding,""),
  12596. BUILDIN_DEF2(savepoint,"save","sii"),
  12597. BUILDIN_DEF(savepoint,"sii"),
  12598. BUILDIN_DEF(gettimetick,"i"),
  12599. BUILDIN_DEF(gettime,"i"),
  12600. BUILDIN_DEF(gettimestr,"si"),
  12601. BUILDIN_DEF(openstorage,""),
  12602. BUILDIN_DEF(guildopenstorage,"*"),
  12603. BUILDIN_DEF(itemskill,"vi"),
  12604. BUILDIN_DEF(produce,"i"),
  12605. BUILDIN_DEF(cooking,"i"),
  12606. BUILDIN_DEF(monster,"siisii*"),
  12607. BUILDIN_DEF(getmobdrops,"i"),
  12608. BUILDIN_DEF(areamonster,"siiiisii*"),
  12609. BUILDIN_DEF(killmonster,"ss?"),
  12610. BUILDIN_DEF(killmonsterall,"s?"),
  12611. BUILDIN_DEF(clone,"siisi*"),
  12612. BUILDIN_DEF(doevent,"s"),
  12613. BUILDIN_DEF(donpcevent,"s"),
  12614. BUILDIN_DEF(cmdothernpc,"ss"),
  12615. BUILDIN_DEF(addtimer,"is"),
  12616. BUILDIN_DEF(deltimer,"s"),
  12617. BUILDIN_DEF(addtimercount,"si"),
  12618. BUILDIN_DEF(initnpctimer,"??"),
  12619. BUILDIN_DEF(stopnpctimer,"??"),
  12620. BUILDIN_DEF(startnpctimer,"??"),
  12621. BUILDIN_DEF(setnpctimer,"i?"),
  12622. BUILDIN_DEF(getnpctimer,"i?"),
  12623. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  12624. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  12625. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  12626. BUILDIN_DEF(announce,"si*"),
  12627. BUILDIN_DEF(mapannounce,"ssi*"),
  12628. BUILDIN_DEF(areaannounce,"siiiisi*"),
  12629. BUILDIN_DEF(getusers,"i"),
  12630. BUILDIN_DEF(getmapguildusers,"si"),
  12631. BUILDIN_DEF(getmapusers,"s"),
  12632. BUILDIN_DEF(getareausers,"siiii"),
  12633. BUILDIN_DEF(getareadropitem,"siiiii"),
  12634. BUILDIN_DEF(enablenpc,"s"),
  12635. BUILDIN_DEF(disablenpc,"s"),
  12636. BUILDIN_DEF(hideoffnpc,"s"),
  12637. BUILDIN_DEF(hideonnpc,"s"),
  12638. BUILDIN_DEF(sc_start,"iii?"),
  12639. BUILDIN_DEF(sc_start2,"iiii?"),
  12640. BUILDIN_DEF(sc_start4,"iiiiii?"),
  12641. BUILDIN_DEF(sc_end,"i?"),
  12642. BUILDIN_DEF(getscrate,"ii*"),
  12643. BUILDIN_DEF(debugmes,"s"),
  12644. BUILDIN_DEF2(catchpet,"pet","i"),
  12645. BUILDIN_DEF2(birthpet,"bpet",""),
  12646. BUILDIN_DEF(resetlvl,"i"),
  12647. BUILDIN_DEF(resetstatus,""),
  12648. BUILDIN_DEF(resetskill,""),
  12649. BUILDIN_DEF(skillpointcount,""),
  12650. BUILDIN_DEF(changebase,"i"),
  12651. BUILDIN_DEF(changesex,""),
  12652. BUILDIN_DEF(waitingroom,"si??"),
  12653. BUILDIN_DEF(delwaitingroom,"?"),
  12654. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  12655. BUILDIN_DEF(enablewaitingroomevent,"?"),
  12656. BUILDIN_DEF(disablewaitingroomevent,"?"),
  12657. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  12658. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  12659. BUILDIN_DEF(getwaitingroomstate,"i?"),
  12660. BUILDIN_DEF(warpwaitingpc,"sii?"),
  12661. BUILDIN_DEF(attachrid,"i"),
  12662. BUILDIN_DEF(detachrid,""),
  12663. BUILDIN_DEF(isloggedin,"i?"),
  12664. BUILDIN_DEF(setmapflagnosave,"ssii"),
  12665. BUILDIN_DEF(getmapflag,"si"),
  12666. BUILDIN_DEF(setmapflag,"si*"),
  12667. BUILDIN_DEF(removemapflag,"si"),
  12668. BUILDIN_DEF(pvpon,"s"),
  12669. BUILDIN_DEF(pvpoff,"s"),
  12670. BUILDIN_DEF(gvgon,"s"),
  12671. BUILDIN_DEF(gvgoff,"s"),
  12672. BUILDIN_DEF(emotion,"i*"),
  12673. BUILDIN_DEF(maprespawnguildid,"sii"),
  12674. BUILDIN_DEF(agitstart,""), // <Agit>
  12675. BUILDIN_DEF(agitend,""),
  12676. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  12677. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  12678. BUILDIN_DEF(getcastlename,"s"),
  12679. BUILDIN_DEF(getcastledata,"si*"),
  12680. BUILDIN_DEF(setcastledata,"sii"),
  12681. BUILDIN_DEF(requestguildinfo,"i*"),
  12682. BUILDIN_DEF(getequipcardcnt,"i"),
  12683. BUILDIN_DEF(successremovecards,"i"),
  12684. BUILDIN_DEF(failedremovecards,"ii"),
  12685. BUILDIN_DEF(marriage,"s"),
  12686. BUILDIN_DEF2(wedding_effect,"wedding",""),
  12687. BUILDIN_DEF(divorce,""),
  12688. BUILDIN_DEF(ispartneron,""),
  12689. BUILDIN_DEF(getpartnerid,""),
  12690. BUILDIN_DEF(getchildid,""),
  12691. BUILDIN_DEF(getmotherid,""),
  12692. BUILDIN_DEF(getfatherid,""),
  12693. BUILDIN_DEF(warppartner,"sii"),
  12694. BUILDIN_DEF(getitemname,"i"),
  12695. BUILDIN_DEF(getitemslots,"i"),
  12696. BUILDIN_DEF(makepet,"i"),
  12697. BUILDIN_DEF(getexp,"ii"),
  12698. BUILDIN_DEF(getinventorylist,""),
  12699. BUILDIN_DEF(getskilllist,""),
  12700. BUILDIN_DEF(clearitem,""),
  12701. BUILDIN_DEF(classchange,"ii"),
  12702. BUILDIN_DEF(misceffect,"i"),
  12703. BUILDIN_DEF(soundeffect,"si"),
  12704. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  12705. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  12706. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  12707. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  12708. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  12709. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  12710. BUILDIN_DEF(petloot,"i"), // [Valaris]
  12711. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  12712. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  12713. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  12714. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  12715. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  12716. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  12717. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  12718. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  12719. BUILDIN_DEF(nude,""), // nude command [Valaris]
  12720. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  12721. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  12722. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  12723. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  12724. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  12725. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  12726. BUILDIN_DEF(mobcount,"ss"),
  12727. BUILDIN_DEF(getlook,"i"),
  12728. BUILDIN_DEF(getsavepoint,"i"),
  12729. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  12730. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  12731. BUILDIN_DEF(npcstop,""), // [Valaris]
  12732. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  12733. BUILDIN_DEF(checkoption1,"i"),
  12734. BUILDIN_DEF(checkoption2,"i"),
  12735. BUILDIN_DEF(guildgetexp,"i"),
  12736. BUILDIN_DEF(guildchangegm,"is"),
  12737. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  12738. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  12739. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  12740. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  12741. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  12742. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  12743. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  12744. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  12745. BUILDIN_DEF(night,""), // sets the server to night time
  12746. BUILDIN_DEF(day,""), // sets the server to day time
  12747. #ifdef PCRE_SUPPORT
  12748. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  12749. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  12750. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  12751. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  12752. #endif
  12753. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  12754. BUILDIN_DEF(getusersname,"*"),
  12755. BUILDIN_DEF(recovery,""),
  12756. BUILDIN_DEF(getpetinfo,"i"),
  12757. BUILDIN_DEF(gethominfo,"i"),
  12758. BUILDIN_DEF(checkequipedcard,"i"),
  12759. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  12760. BUILDIN_DEF(globalmes,"s*"),
  12761. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  12762. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  12763. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  12764. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  12765. BUILDIN_DEF(setnpcdisplay,"sv??"),
  12766. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  12767. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  12768. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  12769. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  12770. // [zBuffer] List of mathematics commands --->
  12771. BUILDIN_DEF(sqrt,"i"),
  12772. BUILDIN_DEF(pow,"ii"),
  12773. BUILDIN_DEF(distance,"iiii"),
  12774. // <--- [zBuffer] List of mathematics commands
  12775. BUILDIN_DEF(md5,"s"),
  12776. // [zBuffer] List of dynamic var commands --->
  12777. BUILDIN_DEF(getd,"*"),
  12778. BUILDIN_DEF(setd,"*"),
  12779. // <--- [zBuffer] List of dynamic var commands
  12780. BUILDIN_DEF(petstat,"i"),
  12781. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  12782. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  12783. BUILDIN_DEF(npcshopadditem,"sii*"),
  12784. BUILDIN_DEF(npcshopdelitem,"si*"),
  12785. BUILDIN_DEF(npcshopattach,"s?"),
  12786. BUILDIN_DEF(equip,"i"),
  12787. BUILDIN_DEF(autoequip,"ii"),
  12788. BUILDIN_DEF(setbattleflag,"ss"),
  12789. BUILDIN_DEF(getbattleflag,"s"),
  12790. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  12791. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  12792. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  12793. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  12794. BUILDIN_DEF(axtoi,"s"),
  12795. BUILDIN_DEF(query_sql,"s*"),
  12796. BUILDIN_DEF(query_logsql,"s*"),
  12797. BUILDIN_DEF(escape_sql,"s"),
  12798. BUILDIN_DEF(atoi,"s"),
  12799. // [zBuffer] List of player cont commands --->
  12800. BUILDIN_DEF(rid2name,"i"),
  12801. BUILDIN_DEF(pcfollow,"ii"),
  12802. BUILDIN_DEF(pcstopfollow,"i"),
  12803. BUILDIN_DEF(pcblockmove,"ii"),
  12804. // <--- [zBuffer] List of player cont commands
  12805. // [zBuffer] List of mob control commands --->
  12806. BUILDIN_DEF(unitwalk,"ii?"),
  12807. BUILDIN_DEF(unitkill,"i"),
  12808. BUILDIN_DEF(unitwarp,"isii"),
  12809. BUILDIN_DEF(unitattack,"iv?"),
  12810. BUILDIN_DEF(unitstop,"i"),
  12811. BUILDIN_DEF(unittalk,"is"),
  12812. BUILDIN_DEF(unitemote,"ii"),
  12813. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  12814. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  12815. // <--- [zBuffer] List of mob control commands
  12816. BUILDIN_DEF(sleep,"i"),
  12817. BUILDIN_DEF(sleep2,"i"),
  12818. BUILDIN_DEF(awake,"s"),
  12819. BUILDIN_DEF(getvariableofnpc,"rs"),
  12820. BUILDIN_DEF(warpportal,"iisii"),
  12821. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  12822. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  12823. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  12824. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  12825. BUILDIN_DEF(checkvending,"*"),
  12826. BUILDIN_DEF(checkchatting,"*"),
  12827. BUILDIN_DEF(openmail,""),
  12828. BUILDIN_DEF(openauction,""),
  12829. BUILDIN_DEF(checkcell,"siii"),
  12830. BUILDIN_DEF(setcell,"siiiiii"),
  12831. BUILDIN_DEF(setwall,"siiiiis"),
  12832. BUILDIN_DEF(delwall,"s"),
  12833. BUILDIN_DEF(searchitem,"rs"),
  12834. BUILDIN_DEF(mercenary_create,"ii"),
  12835. BUILDIN_DEF(mercenary_heal,"ii"),
  12836. BUILDIN_DEF(mercenary_sc_start,"iii"),
  12837. BUILDIN_DEF(mercenary_get_calls,"i"),
  12838. BUILDIN_DEF(mercenary_get_faith,"i"),
  12839. BUILDIN_DEF(mercenary_set_calls,"ii"),
  12840. BUILDIN_DEF(mercenary_set_faith,"ii"),
  12841. BUILDIN_DEF(readbook,"ii"),
  12842. BUILDIN_DEF(setfont,"i"),
  12843. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  12844. BUILDIN_DEF(progressbar, "si"),
  12845. // WoE SE
  12846. BUILDIN_DEF(agitstart2,""),
  12847. BUILDIN_DEF(agitend2,""),
  12848. BUILDIN_DEF(agitcheck2,""),
  12849. // BattleGround
  12850. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  12851. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  12852. BUILDIN_DEF(bg_team_setxy,"iii"),
  12853. BUILDIN_DEF(bg_warp,"isii"),
  12854. BUILDIN_DEF(bg_monster,"isiisi*"),
  12855. BUILDIN_DEF(bg_monster_set_team,"ii"),
  12856. BUILDIN_DEF(bg_leave,""),
  12857. BUILDIN_DEF(bg_destroy,"i"),
  12858. BUILDIN_DEF(areapercentheal,"siiiiii"),
  12859. BUILDIN_DEF(bg_get_data,"ii"),
  12860. BUILDIN_DEF(bg_getareausers,"isiiii"),
  12861. BUILDIN_DEF(bg_updatescore,"sii"),
  12862. // Instancing
  12863. BUILDIN_DEF(instance_create,"si"),
  12864. BUILDIN_DEF(instance_destroy,"?"),
  12865. BUILDIN_DEF(instance_attachmap,"si?"),
  12866. BUILDIN_DEF(instance_detachmap,"s?"),
  12867. BUILDIN_DEF(instance_attach,"i"),
  12868. BUILDIN_DEF(instance_id,"?"),
  12869. BUILDIN_DEF(instance_set_timeout,"ii?"),
  12870. BUILDIN_DEF(instance_init,"i"),
  12871. BUILDIN_DEF(instance_announce,"isi*"),
  12872. BUILDIN_DEF(instance_npcname,"s?"),
  12873. BUILDIN_DEF(has_instance,"s?"),
  12874. BUILDIN_DEF(instance_warpall,"sii?"),
  12875. //Quest Log System [Inkfish]
  12876. BUILDIN_DEF(setquest, "i"),
  12877. BUILDIN_DEF(erasequest, "i"),
  12878. BUILDIN_DEF(completequest, "i"),
  12879. BUILDIN_DEF(checkquest, "i*"),
  12880. BUILDIN_DEF(changequest, "ii"),
  12881. BUILDIN_DEF(showevent, "ii"),
  12882. {NULL,NULL,NULL},
  12883. };